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