xref: /freebsd/sys/netipsec/ipsec_mod.c (revision 069ac18495ad8fde2748bc94b0f80a50250bb01d)
1 /*-
2  * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "opt_inet.h"
28 #include "opt_inet6.h"
29 #include "opt_ipsec.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/module.h>
38 #include <sys/priv.h>
39 #include <sys/rmlock.h>
40 #include <sys/socket.h>
41 #include <sys/sockopt.h>
42 #include <sys/syslog.h>
43 #include <sys/proc.h>
44 
45 #include <netinet/in.h>
46 #include <netinet/in_pcb.h>
47 
48 #include <netipsec/ipsec.h>
49 #include <netipsec/ipsec6.h>
50 #include <netipsec/key.h>
51 #include <netipsec/key_debug.h>
52 
53 #include <netipsec/ipsec_support.h>
54 
55 #ifdef INET
56 static const struct ipsec_methods ipv4_methods = {
57 	.input = ipsec4_input,
58 	.forward = ipsec4_forward,
59 	.output = ipsec4_output,
60 	.pcbctl = ipsec4_pcbctl,
61 	.capability = ipsec4_capability,
62 	.check_policy = ipsec4_in_reject,
63 	.ctlinput = ipsec4_ctlinput,
64 	.hdrsize = ipsec_hdrsiz_inpcb,
65 	.udp_input = udp_ipsec_input,
66 	.udp_pcbctl = udp_ipsec_pcbctl,
67 };
68 #ifndef KLD_MODULE
69 static const struct ipsec_support ipv4_ipsec = {
70 	.enabled = IPSEC_MODULE_ENABLED,
71 	.methods = &ipv4_methods
72 };
73 const struct ipsec_support * const ipv4_ipsec_support = &ipv4_ipsec;
74 #endif /* !KLD_MODULE */
75 #endif /* INET */
76 
77 #ifdef INET6
78 static const struct ipsec_methods ipv6_methods = {
79 	.input = ipsec6_input,
80 	.forward = ipsec6_forward,
81 	.output = ipsec6_output,
82 	.pcbctl = ipsec6_pcbctl,
83 	.capability = ipsec6_capability,
84 	.check_policy = ipsec6_in_reject,
85 	.ctlinput = ipsec6_ctlinput,
86 	.hdrsize = ipsec_hdrsiz_inpcb,
87 };
88 #ifndef KLD_MODULE
89 static const struct ipsec_support ipv6_ipsec = {
90 	.enabled = IPSEC_MODULE_ENABLED,
91 	.methods = &ipv6_methods
92 };
93 const struct ipsec_support * const ipv6_ipsec_support = &ipv6_ipsec;
94 #endif /* !KLD_MODULE */
95 #endif /* INET6 */
96 
97 /*
98  * Always register ipsec module.
99  * Even when IPsec is build in the kernel, we need to have
100  * module registered. This will prevent to load ipsec.ko.
101  */
102 static int
103 ipsec_modevent(module_t mod, int type, void *data)
104 {
105 
106 	switch (type) {
107 	case MOD_LOAD:
108 		/* All xforms are registered via SYSINIT */
109 		if (!ipsec_initialized())
110 			return (ENOMEM);
111 #ifdef KLD_MODULE
112 #ifdef INET
113 		ipsec_support_enable(ipv4_ipsec_support, &ipv4_methods);
114 #endif
115 #ifdef INET6
116 		ipsec_support_enable(ipv6_ipsec_support, &ipv6_methods);
117 #endif
118 #endif /* KLD_MODULE */
119 		break;
120 	case MOD_UNLOAD:
121 		/* All xforms are unregistered via SYSUNINIT */
122 #ifdef KLD_MODULE
123 #ifdef INET
124 		ipsec_support_disable(ipv4_ipsec_support);
125 #endif
126 #ifdef INET6
127 		ipsec_support_disable(ipv6_ipsec_support);
128 #endif
129 #endif /* KLD_MODULE */
130 		break;
131 	default:
132 		return (EOPNOTSUPP);
133 	}
134 	return (0);
135 }
136 
137 static moduledata_t ipsec_mod = {
138 	"ipsec",
139 	ipsec_modevent,
140 	0
141 };
142 
143 DECLARE_MODULE(ipsec, ipsec_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
144 MODULE_VERSION(ipsec, 1);
145 #ifdef KLD_MODULE
146 MODULE_DEPEND(ipsec, ipsec_support, 1, 1, 1);
147 #endif
148