xref: /freebsd/sys/netipsec/ipsec_mod.c (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
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/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/module.h>
41 #include <sys/priv.h>
42 #include <sys/rmlock.h>
43 #include <sys/socket.h>
44 #include <sys/sockopt.h>
45 #include <sys/syslog.h>
46 #include <sys/proc.h>
47 
48 #include <netinet/in.h>
49 #include <netinet/in_pcb.h>
50 
51 #include <netipsec/ipsec.h>
52 #include <netipsec/ipsec6.h>
53 #include <netipsec/key.h>
54 #include <netipsec/key_debug.h>
55 
56 #include <netipsec/ipsec_support.h>
57 
58 #ifdef INET
59 static const struct ipsec_methods ipv4_methods = {
60 	.input = ipsec4_input,
61 	.forward = ipsec4_forward,
62 	.output = ipsec4_output,
63 	.pcbctl = ipsec4_pcbctl,
64 	.capability = ipsec4_capability,
65 	.check_policy = ipsec4_in_reject,
66 	.ctlinput = ipsec4_ctlinput,
67 	.hdrsize = ipsec_hdrsiz_inpcb,
68 	.udp_input = udp_ipsec_input,
69 	.udp_pcbctl = udp_ipsec_pcbctl,
70 };
71 #ifndef KLD_MODULE
72 static const struct ipsec_support ipv4_ipsec = {
73 	.enabled = IPSEC_MODULE_ENABLED,
74 	.methods = &ipv4_methods
75 };
76 const struct ipsec_support * const ipv4_ipsec_support = &ipv4_ipsec;
77 #endif /* !KLD_MODULE */
78 #endif /* INET */
79 
80 #ifdef INET6
81 static const struct ipsec_methods ipv6_methods = {
82 	.input = ipsec6_input,
83 	.forward = ipsec6_forward,
84 	.output = ipsec6_output,
85 	.pcbctl = ipsec6_pcbctl,
86 	.capability = ipsec6_capability,
87 	.check_policy = ipsec6_in_reject,
88 	.ctlinput = ipsec6_ctlinput,
89 	.hdrsize = ipsec_hdrsiz_inpcb,
90 };
91 #ifndef KLD_MODULE
92 static const struct ipsec_support ipv6_ipsec = {
93 	.enabled = IPSEC_MODULE_ENABLED,
94 	.methods = &ipv6_methods
95 };
96 const struct ipsec_support * const ipv6_ipsec_support = &ipv6_ipsec;
97 #endif /* !KLD_MODULE */
98 #endif /* INET6 */
99 
100 /*
101  * Always register ipsec module.
102  * Even when IPsec is build in the kernel, we need to have
103  * module registered. This will prevent to load ipsec.ko.
104  */
105 static int
106 ipsec_modevent(module_t mod, int type, void *data)
107 {
108 
109 	switch (type) {
110 	case MOD_LOAD:
111 		/* All xforms are registered via SYSINIT */
112 		if (!ipsec_initialized())
113 			return (ENOMEM);
114 #ifdef KLD_MODULE
115 #ifdef INET
116 		ipsec_support_enable(ipv4_ipsec_support, &ipv4_methods);
117 #endif
118 #ifdef INET6
119 		ipsec_support_enable(ipv6_ipsec_support, &ipv6_methods);
120 #endif
121 #endif /* KLD_MODULE */
122 		break;
123 	case MOD_UNLOAD:
124 		/* All xforms are unregistered via SYSUNINIT */
125 #ifdef KLD_MODULE
126 #ifdef INET
127 		ipsec_support_disable(ipv4_ipsec_support);
128 #endif
129 #ifdef INET6
130 		ipsec_support_disable(ipv6_ipsec_support);
131 #endif
132 #endif /* KLD_MODULE */
133 		break;
134 	default:
135 		return (EOPNOTSUPP);
136 	}
137 	return (0);
138 }
139 
140 static moduledata_t ipsec_mod = {
141 	"ipsec",
142 	ipsec_modevent,
143 	0
144 };
145 
146 DECLARE_MODULE(ipsec, ipsec_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
147 MODULE_VERSION(ipsec, 1);
148 #ifdef KLD_MODULE
149 MODULE_DEPEND(ipsec, ipsec_support, 1, 1, 1);
150 #endif
151