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