1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2006 The FreeBSD Project. |
---|
5 | * Copyright (c) 2015 Andrey V. Elsukov <ae@FreeBSD.org> |
---|
6 | * All rights reserved. |
---|
7 | * |
---|
8 | * Redistribution and use in source and binary forms, with or without |
---|
9 | * modification, are permitted provided that the following conditions |
---|
10 | * are met: |
---|
11 | * |
---|
12 | * 1. Redistributions of source code must retain the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer. |
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
15 | * notice, this list of conditions and the following disclaimer in the |
---|
16 | * documentation and/or other materials provided with the distribution. |
---|
17 | * |
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
28 | * SUCH DAMAGE. |
---|
29 | * |
---|
30 | * $FreeBSD$ |
---|
31 | */ |
---|
32 | |
---|
33 | #include <rtems/bsd/local/opt_inet.h> |
---|
34 | #include <rtems/bsd/local/opt_inet6.h> |
---|
35 | |
---|
36 | #include <sys/param.h> |
---|
37 | #include <sys/systm.h> |
---|
38 | #include <sys/hhook.h> |
---|
39 | #include <sys/kernel.h> |
---|
40 | #include <sys/malloc.h> |
---|
41 | #include <sys/mbuf.h> |
---|
42 | #include <sys/module.h> |
---|
43 | #include <machine/bus.h> |
---|
44 | #include <sys/rman.h> |
---|
45 | #include <sys/socket.h> |
---|
46 | #include <sys/sockio.h> |
---|
47 | #include <sys/sysctl.h> |
---|
48 | |
---|
49 | #include <net/if.h> |
---|
50 | #include <net/if_enc.h> |
---|
51 | #include <net/if_var.h> |
---|
52 | #include <net/if_clone.h> |
---|
53 | #include <net/if_types.h> |
---|
54 | #include <net/pfil.h> |
---|
55 | #include <net/route.h> |
---|
56 | #include <net/netisr.h> |
---|
57 | #include <net/bpf.h> |
---|
58 | #include <net/vnet.h> |
---|
59 | |
---|
60 | #include <netinet/in.h> |
---|
61 | #include <netinet/in_systm.h> |
---|
62 | #include <netinet/ip.h> |
---|
63 | #include <netinet/ip_var.h> |
---|
64 | #include <netinet/in_var.h> |
---|
65 | |
---|
66 | #ifdef INET6 |
---|
67 | #include <netinet/ip6.h> |
---|
68 | #include <netinet6/ip6_var.h> |
---|
69 | #endif |
---|
70 | |
---|
71 | #include <netipsec/ipsec.h> |
---|
72 | #include <netipsec/xform.h> |
---|
73 | |
---|
74 | #define ENCMTU (1024+512) |
---|
75 | |
---|
76 | /* XXX this define must have the same value as in OpenBSD */ |
---|
77 | #define M_CONF 0x0400 /* payload was encrypted (ESP-transport) */ |
---|
78 | #define M_AUTH 0x0800 /* payload was authenticated (AH or ESP auth) */ |
---|
79 | #define M_AUTH_AH 0x2000 /* header was authenticated (AH) */ |
---|
80 | |
---|
81 | struct enchdr { |
---|
82 | u_int32_t af; |
---|
83 | u_int32_t spi; |
---|
84 | u_int32_t flags; |
---|
85 | }; |
---|
86 | struct enc_softc { |
---|
87 | struct ifnet *sc_ifp; |
---|
88 | }; |
---|
89 | static VNET_DEFINE(struct enc_softc *, enc_sc); |
---|
90 | #define V_enc_sc VNET(enc_sc) |
---|
91 | static VNET_DEFINE(struct if_clone *, enc_cloner); |
---|
92 | #define V_enc_cloner VNET(enc_cloner) |
---|
93 | |
---|
94 | static int enc_ioctl(struct ifnet *, u_long, caddr_t); |
---|
95 | static int enc_output(struct ifnet *, struct mbuf *, |
---|
96 | const struct sockaddr *, struct route *); |
---|
97 | static int enc_clone_create(struct if_clone *, int, caddr_t); |
---|
98 | static void enc_clone_destroy(struct ifnet *); |
---|
99 | static int enc_add_hhooks(struct enc_softc *); |
---|
100 | static void enc_remove_hhooks(struct enc_softc *); |
---|
101 | |
---|
102 | static const char encname[] = "enc"; |
---|
103 | |
---|
104 | /* |
---|
105 | * Before and after are relative to when we are stripping the |
---|
106 | * outer IP header. |
---|
107 | */ |
---|
108 | static VNET_DEFINE(int, filter_mask_in) = IPSEC_ENC_BEFORE; |
---|
109 | static VNET_DEFINE(int, bpf_mask_in) = IPSEC_ENC_BEFORE; |
---|
110 | static VNET_DEFINE(int, filter_mask_out) = IPSEC_ENC_BEFORE; |
---|
111 | static VNET_DEFINE(int, bpf_mask_out) = IPSEC_ENC_BEFORE | IPSEC_ENC_AFTER; |
---|
112 | #define V_filter_mask_in VNET(filter_mask_in) |
---|
113 | #define V_bpf_mask_in VNET(bpf_mask_in) |
---|
114 | #define V_filter_mask_out VNET(filter_mask_out) |
---|
115 | #define V_bpf_mask_out VNET(bpf_mask_out) |
---|
116 | |
---|
117 | static SYSCTL_NODE(_net, OID_AUTO, enc, CTLFLAG_RW, 0, "enc sysctl"); |
---|
118 | static SYSCTL_NODE(_net_enc, OID_AUTO, in, CTLFLAG_RW, 0, "enc input sysctl"); |
---|
119 | static SYSCTL_NODE(_net_enc, OID_AUTO, out, CTLFLAG_RW, 0, "enc output sysctl"); |
---|
120 | SYSCTL_INT(_net_enc_in, OID_AUTO, ipsec_filter_mask, |
---|
121 | CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(filter_mask_in), 0, |
---|
122 | "IPsec input firewall filter mask"); |
---|
123 | SYSCTL_INT(_net_enc_in, OID_AUTO, ipsec_bpf_mask, |
---|
124 | CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(bpf_mask_in), 0, |
---|
125 | "IPsec input bpf mask"); |
---|
126 | SYSCTL_INT(_net_enc_out, OID_AUTO, ipsec_filter_mask, |
---|
127 | CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(filter_mask_out), 0, |
---|
128 | "IPsec output firewall filter mask"); |
---|
129 | SYSCTL_INT(_net_enc_out, OID_AUTO, ipsec_bpf_mask, |
---|
130 | CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(bpf_mask_out), 0, |
---|
131 | "IPsec output bpf mask"); |
---|
132 | |
---|
133 | static void |
---|
134 | enc_clone_destroy(struct ifnet *ifp) |
---|
135 | { |
---|
136 | struct enc_softc *sc; |
---|
137 | |
---|
138 | sc = ifp->if_softc; |
---|
139 | KASSERT(sc == V_enc_sc, ("sc != ifp->if_softc")); |
---|
140 | |
---|
141 | bpfdetach(ifp); |
---|
142 | if_detach(ifp); |
---|
143 | if_free(ifp); |
---|
144 | free(sc, M_DEVBUF); |
---|
145 | V_enc_sc = NULL; |
---|
146 | } |
---|
147 | |
---|
148 | static int |
---|
149 | enc_clone_create(struct if_clone *ifc, int unit, caddr_t params) |
---|
150 | { |
---|
151 | struct ifnet *ifp; |
---|
152 | struct enc_softc *sc; |
---|
153 | |
---|
154 | sc = malloc(sizeof(struct enc_softc), M_DEVBUF, |
---|
155 | M_WAITOK | M_ZERO); |
---|
156 | ifp = sc->sc_ifp = if_alloc(IFT_ENC); |
---|
157 | if (ifp == NULL) { |
---|
158 | free(sc, M_DEVBUF); |
---|
159 | return (ENOSPC); |
---|
160 | } |
---|
161 | if (V_enc_sc != NULL) { |
---|
162 | if_free(ifp); |
---|
163 | free(sc, M_DEVBUF); |
---|
164 | return (EEXIST); |
---|
165 | } |
---|
166 | V_enc_sc = sc; |
---|
167 | if_initname(ifp, encname, unit); |
---|
168 | ifp->if_mtu = ENCMTU; |
---|
169 | ifp->if_ioctl = enc_ioctl; |
---|
170 | ifp->if_output = enc_output; |
---|
171 | ifp->if_softc = sc; |
---|
172 | if_attach(ifp); |
---|
173 | bpfattach(ifp, DLT_ENC, sizeof(struct enchdr)); |
---|
174 | return (0); |
---|
175 | } |
---|
176 | |
---|
177 | static int |
---|
178 | enc_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, |
---|
179 | struct route *ro) |
---|
180 | { |
---|
181 | |
---|
182 | m_freem(m); |
---|
183 | return (0); |
---|
184 | } |
---|
185 | |
---|
186 | static int |
---|
187 | enc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) |
---|
188 | { |
---|
189 | |
---|
190 | if (cmd != SIOCSIFFLAGS) |
---|
191 | return (EINVAL); |
---|
192 | if (ifp->if_flags & IFF_UP) |
---|
193 | ifp->if_drv_flags |= IFF_DRV_RUNNING; |
---|
194 | else |
---|
195 | ifp->if_drv_flags &= ~IFF_DRV_RUNNING; |
---|
196 | return (0); |
---|
197 | } |
---|
198 | |
---|
199 | /* |
---|
200 | * One helper hook function is used by any hook points. |
---|
201 | * + from hhook_type we can determine the packet direction: |
---|
202 | * HHOOK_TYPE_IPSEC_IN or HHOOK_TYPE_IPSEC_OUT; |
---|
203 | * + from hhook_id we can determine address family: AF_INET or AF_INET6; |
---|
204 | * + udata contains pointer to enc_softc; |
---|
205 | * + ctx_data contains pointer to struct ipsec_ctx_data. |
---|
206 | */ |
---|
207 | static int |
---|
208 | enc_hhook(int32_t hhook_type, int32_t hhook_id, void *udata, void *ctx_data, |
---|
209 | void *hdata, struct osd *hosd) |
---|
210 | { |
---|
211 | struct enchdr hdr; |
---|
212 | struct ipsec_ctx_data *ctx; |
---|
213 | struct enc_softc *sc; |
---|
214 | struct ifnet *ifp, *rcvif; |
---|
215 | struct pfil_head *ph; |
---|
216 | int pdir; |
---|
217 | |
---|
218 | sc = (struct enc_softc *)udata; |
---|
219 | ifp = sc->sc_ifp; |
---|
220 | if ((ifp->if_flags & IFF_UP) == 0) |
---|
221 | return (0); |
---|
222 | |
---|
223 | ctx = (struct ipsec_ctx_data *)ctx_data; |
---|
224 | /* XXX: wrong hook point was used by caller? */ |
---|
225 | if (ctx->af != hhook_id) |
---|
226 | return (EPFNOSUPPORT); |
---|
227 | |
---|
228 | if (((hhook_type == HHOOK_TYPE_IPSEC_IN && |
---|
229 | (ctx->enc & V_bpf_mask_in) != 0) || |
---|
230 | (hhook_type == HHOOK_TYPE_IPSEC_OUT && |
---|
231 | (ctx->enc & V_bpf_mask_out) != 0)) && |
---|
232 | bpf_peers_present(ifp->if_bpf) != 0) { |
---|
233 | hdr.af = ctx->af; |
---|
234 | hdr.spi = ctx->sav->spi; |
---|
235 | hdr.flags = 0; |
---|
236 | if (ctx->sav->alg_enc != SADB_EALG_NONE) |
---|
237 | hdr.flags |= M_CONF; |
---|
238 | if (ctx->sav->alg_auth != SADB_AALG_NONE) |
---|
239 | hdr.flags |= M_AUTH; |
---|
240 | bpf_mtap2(ifp->if_bpf, &hdr, sizeof(hdr), *ctx->mp); |
---|
241 | } |
---|
242 | |
---|
243 | switch (hhook_type) { |
---|
244 | case HHOOK_TYPE_IPSEC_IN: |
---|
245 | if (ctx->enc == IPSEC_ENC_BEFORE) { |
---|
246 | /* Do accounting only once */ |
---|
247 | if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); |
---|
248 | if_inc_counter(ifp, IFCOUNTER_IBYTES, |
---|
249 | (*ctx->mp)->m_pkthdr.len); |
---|
250 | } |
---|
251 | if ((ctx->enc & V_filter_mask_in) == 0) |
---|
252 | return (0); /* skip pfil processing */ |
---|
253 | pdir = PFIL_IN; |
---|
254 | break; |
---|
255 | case HHOOK_TYPE_IPSEC_OUT: |
---|
256 | if (ctx->enc == IPSEC_ENC_BEFORE) { |
---|
257 | /* Do accounting only once */ |
---|
258 | if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); |
---|
259 | if_inc_counter(ifp, IFCOUNTER_OBYTES, |
---|
260 | (*ctx->mp)->m_pkthdr.len); |
---|
261 | } |
---|
262 | if ((ctx->enc & V_filter_mask_out) == 0) |
---|
263 | return (0); /* skip pfil processing */ |
---|
264 | pdir = PFIL_OUT; |
---|
265 | break; |
---|
266 | default: |
---|
267 | return (EINVAL); |
---|
268 | } |
---|
269 | |
---|
270 | switch (hhook_id) { |
---|
271 | #ifdef INET |
---|
272 | case AF_INET: |
---|
273 | ph = &V_inet_pfil_hook; |
---|
274 | break; |
---|
275 | #endif |
---|
276 | #ifdef INET6 |
---|
277 | case AF_INET6: |
---|
278 | ph = &V_inet6_pfil_hook; |
---|
279 | break; |
---|
280 | #endif |
---|
281 | default: |
---|
282 | ph = NULL; |
---|
283 | } |
---|
284 | if (ph == NULL || !PFIL_HOOKED(ph)) |
---|
285 | return (0); |
---|
286 | /* Make a packet looks like it was received on enc(4) */ |
---|
287 | rcvif = (*ctx->mp)->m_pkthdr.rcvif; |
---|
288 | (*ctx->mp)->m_pkthdr.rcvif = ifp; |
---|
289 | if (pfil_run_hooks(ph, ctx->mp, ifp, pdir, NULL) != 0 || |
---|
290 | *ctx->mp == NULL) { |
---|
291 | *ctx->mp = NULL; /* consumed by filter */ |
---|
292 | return (EACCES); |
---|
293 | } |
---|
294 | (*ctx->mp)->m_pkthdr.rcvif = rcvif; |
---|
295 | return (0); |
---|
296 | } |
---|
297 | |
---|
298 | static int |
---|
299 | enc_add_hhooks(struct enc_softc *sc) |
---|
300 | { |
---|
301 | struct hookinfo hki; |
---|
302 | int error; |
---|
303 | |
---|
304 | error = EPFNOSUPPORT; |
---|
305 | hki.hook_func = enc_hhook; |
---|
306 | hki.hook_helper = NULL; |
---|
307 | hki.hook_udata = sc; |
---|
308 | #ifdef INET |
---|
309 | hki.hook_id = AF_INET; |
---|
310 | hki.hook_type = HHOOK_TYPE_IPSEC_IN; |
---|
311 | error = hhook_add_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET], |
---|
312 | &hki, HHOOK_WAITOK); |
---|
313 | if (error != 0) |
---|
314 | return (error); |
---|
315 | hki.hook_type = HHOOK_TYPE_IPSEC_OUT; |
---|
316 | error = hhook_add_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET], |
---|
317 | &hki, HHOOK_WAITOK); |
---|
318 | if (error != 0) |
---|
319 | return (error); |
---|
320 | #endif |
---|
321 | #ifdef INET6 |
---|
322 | hki.hook_id = AF_INET6; |
---|
323 | hki.hook_type = HHOOK_TYPE_IPSEC_IN; |
---|
324 | error = hhook_add_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET6], |
---|
325 | &hki, HHOOK_WAITOK); |
---|
326 | if (error != 0) |
---|
327 | return (error); |
---|
328 | hki.hook_type = HHOOK_TYPE_IPSEC_OUT; |
---|
329 | error = hhook_add_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET6], |
---|
330 | &hki, HHOOK_WAITOK); |
---|
331 | if (error != 0) |
---|
332 | return (error); |
---|
333 | #endif |
---|
334 | return (error); |
---|
335 | } |
---|
336 | |
---|
337 | static void |
---|
338 | enc_remove_hhooks(struct enc_softc *sc) |
---|
339 | { |
---|
340 | struct hookinfo hki; |
---|
341 | |
---|
342 | hki.hook_func = enc_hhook; |
---|
343 | hki.hook_helper = NULL; |
---|
344 | hki.hook_udata = sc; |
---|
345 | #ifdef INET |
---|
346 | hki.hook_id = AF_INET; |
---|
347 | hki.hook_type = HHOOK_TYPE_IPSEC_IN; |
---|
348 | hhook_remove_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET], &hki); |
---|
349 | hki.hook_type = HHOOK_TYPE_IPSEC_OUT; |
---|
350 | hhook_remove_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET], &hki); |
---|
351 | #endif |
---|
352 | #ifdef INET6 |
---|
353 | hki.hook_id = AF_INET6; |
---|
354 | hki.hook_type = HHOOK_TYPE_IPSEC_IN; |
---|
355 | hhook_remove_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET6], &hki); |
---|
356 | hki.hook_type = HHOOK_TYPE_IPSEC_OUT; |
---|
357 | hhook_remove_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET6], &hki); |
---|
358 | #endif |
---|
359 | } |
---|
360 | |
---|
361 | static void |
---|
362 | vnet_enc_init(const void *unused __unused) |
---|
363 | { |
---|
364 | |
---|
365 | V_enc_sc = NULL; |
---|
366 | V_enc_cloner = if_clone_simple(encname, enc_clone_create, |
---|
367 | enc_clone_destroy, 1); |
---|
368 | } |
---|
369 | VNET_SYSINIT(vnet_enc_init, SI_SUB_PSEUDO, SI_ORDER_ANY, |
---|
370 | vnet_enc_init, NULL); |
---|
371 | |
---|
372 | static void |
---|
373 | vnet_enc_init_proto(void *unused __unused) |
---|
374 | { |
---|
375 | KASSERT(V_enc_sc != NULL, ("%s: V_enc_sc is %p\n", __func__, V_enc_sc)); |
---|
376 | |
---|
377 | if (enc_add_hhooks(V_enc_sc) != 0) |
---|
378 | enc_clone_destroy(V_enc_sc->sc_ifp); |
---|
379 | } |
---|
380 | VNET_SYSINIT(vnet_enc_init_proto, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, |
---|
381 | vnet_enc_init_proto, NULL); |
---|
382 | |
---|
383 | static void |
---|
384 | vnet_enc_uninit(const void *unused __unused) |
---|
385 | { |
---|
386 | KASSERT(V_enc_sc != NULL, ("%s: V_enc_sc is %p\n", __func__, V_enc_sc)); |
---|
387 | |
---|
388 | if_clone_detach(V_enc_cloner); |
---|
389 | } |
---|
390 | VNET_SYSUNINIT(vnet_enc_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, |
---|
391 | vnet_enc_uninit, NULL); |
---|
392 | |
---|
393 | /* |
---|
394 | * The hhook consumer needs to go before ip[6]_destroy are called on |
---|
395 | * SI_ORDER_THIRD. |
---|
396 | */ |
---|
397 | static void |
---|
398 | vnet_enc_uninit_hhook(const void *unused __unused) |
---|
399 | { |
---|
400 | KASSERT(V_enc_sc != NULL, ("%s: V_enc_sc is %p\n", __func__, V_enc_sc)); |
---|
401 | |
---|
402 | enc_remove_hhooks(V_enc_sc); |
---|
403 | } |
---|
404 | VNET_SYSUNINIT(vnet_enc_uninit_hhook, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, |
---|
405 | vnet_enc_uninit_hhook, NULL); |
---|
406 | |
---|
407 | static int |
---|
408 | enc_modevent(module_t mod, int type, void *data) |
---|
409 | { |
---|
410 | |
---|
411 | switch (type) { |
---|
412 | case MOD_LOAD: |
---|
413 | case MOD_UNLOAD: |
---|
414 | break; |
---|
415 | default: |
---|
416 | return (EOPNOTSUPP); |
---|
417 | } |
---|
418 | return (0); |
---|
419 | } |
---|
420 | |
---|
421 | static moduledata_t enc_mod = { |
---|
422 | "if_enc", |
---|
423 | enc_modevent, |
---|
424 | 0 |
---|
425 | }; |
---|
426 | |
---|
427 | DECLARE_MODULE(if_enc, enc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); |
---|
428 | MODULE_VERSION(if_enc, 1); |
---|