1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2016 Yandex LLC |
---|
5 | * Copyright (c) 2016 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 ``AS IS'' AND ANY EXPRESS OR |
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | */ |
---|
29 | |
---|
30 | #include <sys/cdefs.h> |
---|
31 | __FBSDID("$FreeBSD$"); |
---|
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/kernel.h> |
---|
39 | #include <sys/fnv_hash.h> |
---|
40 | #include <sys/jail.h> |
---|
41 | #include <sys/lock.h> |
---|
42 | #include <sys/malloc.h> |
---|
43 | #include <sys/mbuf.h> |
---|
44 | #include <sys/module.h> |
---|
45 | #include <sys/rmlock.h> |
---|
46 | #include <sys/socket.h> |
---|
47 | #include <sys/sockio.h> |
---|
48 | #include <sys/sx.h> |
---|
49 | #include <sys/errno.h> |
---|
50 | #include <sys/sysctl.h> |
---|
51 | #include <sys/priv.h> |
---|
52 | #include <sys/proc.h> |
---|
53 | #include <sys/conf.h> |
---|
54 | |
---|
55 | #include <net/if.h> |
---|
56 | #include <net/if_var.h> |
---|
57 | #include <net/if_clone.h> |
---|
58 | #include <net/if_types.h> |
---|
59 | #include <net/bpf.h> |
---|
60 | #include <net/route.h> |
---|
61 | #include <net/vnet.h> |
---|
62 | |
---|
63 | #include <netinet/in.h> |
---|
64 | #include <netinet/in_var.h> |
---|
65 | #include <netinet/ip.h> |
---|
66 | |
---|
67 | #include <netinet/ip6.h> |
---|
68 | #include <netinet6/in6_var.h> |
---|
69 | #include <netinet6/scope6_var.h> |
---|
70 | |
---|
71 | #include <netipsec/ipsec.h> |
---|
72 | #ifdef INET6 |
---|
73 | #include <netipsec/ipsec6.h> |
---|
74 | #endif |
---|
75 | |
---|
76 | #include <net/if_ipsec.h> |
---|
77 | #include <netipsec/key.h> |
---|
78 | |
---|
79 | #include <security/mac/mac_framework.h> |
---|
80 | |
---|
81 | static MALLOC_DEFINE(M_IPSEC, "ipsec", "IPsec Virtual Tunnel Interface"); |
---|
82 | static const char ipsecname[] = "ipsec"; |
---|
83 | |
---|
84 | #if defined(INET) && defined(INET6) |
---|
85 | #define IPSEC_SPCOUNT 4 |
---|
86 | #else |
---|
87 | #define IPSEC_SPCOUNT 2 |
---|
88 | #endif |
---|
89 | |
---|
90 | struct ipsec_softc { |
---|
91 | struct ifnet *ifp; |
---|
92 | |
---|
93 | struct rmlock lock; |
---|
94 | struct secpolicy *sp[IPSEC_SPCOUNT]; |
---|
95 | |
---|
96 | uint32_t reqid; |
---|
97 | u_int family; |
---|
98 | u_int fibnum; |
---|
99 | LIST_ENTRY(ipsec_softc) chain; |
---|
100 | LIST_ENTRY(ipsec_softc) hash; |
---|
101 | }; |
---|
102 | |
---|
103 | #define IPSEC_LOCK_INIT(sc) rm_init(&(sc)->lock, "if_ipsec softc") |
---|
104 | #define IPSEC_LOCK_DESTROY(sc) rm_destroy(&(sc)->lock) |
---|
105 | #define IPSEC_RLOCK_TRACKER struct rm_priotracker ipsec_tracker |
---|
106 | #define IPSEC_RLOCK(sc) rm_rlock(&(sc)->lock, &ipsec_tracker) |
---|
107 | #define IPSEC_RUNLOCK(sc) rm_runlock(&(sc)->lock, &ipsec_tracker) |
---|
108 | #define IPSEC_RLOCK_ASSERT(sc) rm_assert(&(sc)->lock, RA_RLOCKED) |
---|
109 | #define IPSEC_WLOCK(sc) rm_wlock(&(sc)->lock) |
---|
110 | #define IPSEC_WUNLOCK(sc) rm_wunlock(&(sc)->lock) |
---|
111 | #define IPSEC_WLOCK_ASSERT(sc) rm_assert(&(sc)->lock, RA_WLOCKED) |
---|
112 | |
---|
113 | static struct rmlock ipsec_sc_lock; |
---|
114 | RM_SYSINIT(ipsec_sc_lock, &ipsec_sc_lock, "if_ipsec softc list"); |
---|
115 | |
---|
116 | #define IPSEC_SC_RLOCK_TRACKER struct rm_priotracker ipsec_sc_tracker |
---|
117 | #define IPSEC_SC_RLOCK() rm_rlock(&ipsec_sc_lock, &ipsec_sc_tracker) |
---|
118 | #define IPSEC_SC_RUNLOCK() rm_runlock(&ipsec_sc_lock, &ipsec_sc_tracker) |
---|
119 | #define IPSEC_SC_RLOCK_ASSERT() rm_assert(&ipsec_sc_lock, RA_RLOCKED) |
---|
120 | #define IPSEC_SC_WLOCK() rm_wlock(&ipsec_sc_lock) |
---|
121 | #define IPSEC_SC_WUNLOCK() rm_wunlock(&ipsec_sc_lock) |
---|
122 | #define IPSEC_SC_WLOCK_ASSERT() rm_assert(&ipsec_sc_lock, RA_WLOCKED) |
---|
123 | |
---|
124 | LIST_HEAD(ipsec_iflist, ipsec_softc); |
---|
125 | static VNET_DEFINE(struct ipsec_iflist, ipsec_sc_list); |
---|
126 | static VNET_DEFINE(struct ipsec_iflist *, ipsec_sc_htbl); |
---|
127 | static VNET_DEFINE(u_long, ipsec_sc_hmask); |
---|
128 | #define V_ipsec_sc_list VNET(ipsec_sc_list) |
---|
129 | #define V_ipsec_sc_htbl VNET(ipsec_sc_htbl) |
---|
130 | #define V_ipsec_sc_hmask VNET(ipsec_sc_hmask) |
---|
131 | |
---|
132 | static uint32_t |
---|
133 | ipsec_hash(uint32_t id) |
---|
134 | { |
---|
135 | |
---|
136 | return (fnv_32_buf(&id, sizeof(id), FNV1_32_INIT)); |
---|
137 | } |
---|
138 | |
---|
139 | #define SCHASH_NHASH_LOG2 5 |
---|
140 | #define SCHASH_NHASH (1 << SCHASH_NHASH_LOG2) |
---|
141 | #define SCHASH_HASHVAL(id) (ipsec_hash((id)) & V_ipsec_sc_hmask) |
---|
142 | #define SCHASH_HASH(id) &V_ipsec_sc_htbl[SCHASH_HASHVAL(id)] |
---|
143 | |
---|
144 | /* |
---|
145 | * ipsec_ioctl_sx protects from concurrent ioctls. |
---|
146 | */ |
---|
147 | static struct sx ipsec_ioctl_sx; |
---|
148 | SX_SYSINIT(ipsec_ioctl_sx, &ipsec_ioctl_sx, "ipsec_ioctl"); |
---|
149 | |
---|
150 | static int ipsec_init_reqid(struct ipsec_softc *); |
---|
151 | static int ipsec_set_tunnel(struct ipsec_softc *, struct sockaddr *, |
---|
152 | struct sockaddr *, uint32_t); |
---|
153 | static void ipsec_delete_tunnel(struct ifnet *, int); |
---|
154 | |
---|
155 | static int ipsec_set_addresses(struct ifnet *, struct sockaddr *, |
---|
156 | struct sockaddr *); |
---|
157 | static int ipsec_set_reqid(struct ifnet *, uint32_t); |
---|
158 | |
---|
159 | static int ipsec_ioctl(struct ifnet *, u_long, caddr_t); |
---|
160 | static int ipsec_transmit(struct ifnet *, struct mbuf *); |
---|
161 | static int ipsec_output(struct ifnet *, struct mbuf *, |
---|
162 | const struct sockaddr *, struct route *); |
---|
163 | static void ipsec_qflush(struct ifnet *); |
---|
164 | static int ipsec_clone_create(struct if_clone *, int, caddr_t); |
---|
165 | static void ipsec_clone_destroy(struct ifnet *); |
---|
166 | |
---|
167 | static VNET_DEFINE(struct if_clone *, ipsec_cloner); |
---|
168 | #define V_ipsec_cloner VNET(ipsec_cloner) |
---|
169 | |
---|
170 | static int |
---|
171 | ipsec_clone_create(struct if_clone *ifc, int unit, caddr_t params) |
---|
172 | { |
---|
173 | struct ipsec_softc *sc; |
---|
174 | struct ifnet *ifp; |
---|
175 | |
---|
176 | sc = malloc(sizeof(*sc), M_IPSEC, M_WAITOK | M_ZERO); |
---|
177 | #ifndef __rtems__ |
---|
178 | sc->fibnum = curthread->td_proc->p_fibnum; |
---|
179 | #else /* __rtems__ */ |
---|
180 | sc->fibnum = BSD_DEFAULT_FIB; |
---|
181 | #endif /* __rtems__ */ |
---|
182 | sc->ifp = ifp = if_alloc(IFT_TUNNEL); |
---|
183 | IPSEC_LOCK_INIT(sc); |
---|
184 | ifp->if_softc = sc; |
---|
185 | if_initname(ifp, ipsecname, unit); |
---|
186 | |
---|
187 | ifp->if_addrlen = 0; |
---|
188 | ifp->if_mtu = IPSEC_MTU; |
---|
189 | ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; |
---|
190 | ifp->if_ioctl = ipsec_ioctl; |
---|
191 | ifp->if_transmit = ipsec_transmit; |
---|
192 | ifp->if_qflush = ipsec_qflush; |
---|
193 | ifp->if_output = ipsec_output; |
---|
194 | if_attach(ifp); |
---|
195 | bpfattach(ifp, DLT_NULL, sizeof(uint32_t)); |
---|
196 | |
---|
197 | IPSEC_SC_WLOCK(); |
---|
198 | LIST_INSERT_HEAD(&V_ipsec_sc_list, sc, chain); |
---|
199 | IPSEC_SC_WUNLOCK(); |
---|
200 | return (0); |
---|
201 | } |
---|
202 | |
---|
203 | static void |
---|
204 | ipsec_clone_destroy(struct ifnet *ifp) |
---|
205 | { |
---|
206 | struct ipsec_softc *sc; |
---|
207 | |
---|
208 | sx_xlock(&ipsec_ioctl_sx); |
---|
209 | sc = ifp->if_softc; |
---|
210 | |
---|
211 | IPSEC_SC_WLOCK(); |
---|
212 | ipsec_delete_tunnel(ifp, 1); |
---|
213 | LIST_REMOVE(sc, chain); |
---|
214 | IPSEC_SC_WUNLOCK(); |
---|
215 | |
---|
216 | bpfdetach(ifp); |
---|
217 | if_detach(ifp); |
---|
218 | ifp->if_softc = NULL; |
---|
219 | sx_xunlock(&ipsec_ioctl_sx); |
---|
220 | |
---|
221 | if_free(ifp); |
---|
222 | IPSEC_LOCK_DESTROY(sc); |
---|
223 | free(sc, M_IPSEC); |
---|
224 | } |
---|
225 | |
---|
226 | static void |
---|
227 | vnet_ipsec_init(const void *unused __unused) |
---|
228 | { |
---|
229 | |
---|
230 | LIST_INIT(&V_ipsec_sc_list); |
---|
231 | V_ipsec_sc_htbl = hashinit(SCHASH_NHASH, M_IPSEC, &V_ipsec_sc_hmask); |
---|
232 | V_ipsec_cloner = if_clone_simple(ipsecname, ipsec_clone_create, |
---|
233 | ipsec_clone_destroy, 0); |
---|
234 | } |
---|
235 | VNET_SYSINIT(vnet_ipsec_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, |
---|
236 | vnet_ipsec_init, NULL); |
---|
237 | |
---|
238 | static void |
---|
239 | vnet_ipsec_uninit(const void *unused __unused) |
---|
240 | { |
---|
241 | |
---|
242 | if_clone_detach(V_ipsec_cloner); |
---|
243 | hashdestroy(V_ipsec_sc_htbl, M_IPSEC, V_ipsec_sc_hmask); |
---|
244 | } |
---|
245 | VNET_SYSUNINIT(vnet_ipsec_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, |
---|
246 | vnet_ipsec_uninit, NULL); |
---|
247 | |
---|
248 | static struct secpolicy * |
---|
249 | ipsec_getpolicy(struct ipsec_softc *sc, int dir, sa_family_t af) |
---|
250 | { |
---|
251 | |
---|
252 | switch (af) { |
---|
253 | #ifdef INET |
---|
254 | case AF_INET: |
---|
255 | return (sc->sp[(dir == IPSEC_DIR_INBOUND ? 0: 1)]); |
---|
256 | #endif |
---|
257 | #ifdef INET6 |
---|
258 | case AF_INET6: |
---|
259 | return (sc->sp[(dir == IPSEC_DIR_INBOUND ? 0: 1) |
---|
260 | #ifdef INET |
---|
261 | + 2 |
---|
262 | #endif |
---|
263 | ]); |
---|
264 | #endif |
---|
265 | } |
---|
266 | return (NULL); |
---|
267 | } |
---|
268 | |
---|
269 | static struct secasindex * |
---|
270 | ipsec_getsaidx(struct ipsec_softc *sc, int dir, sa_family_t af) |
---|
271 | { |
---|
272 | struct secpolicy *sp; |
---|
273 | |
---|
274 | sp = ipsec_getpolicy(sc, dir, af); |
---|
275 | if (sp == NULL) |
---|
276 | return (NULL); |
---|
277 | return (&sp->req[0]->saidx); |
---|
278 | } |
---|
279 | |
---|
280 | static int |
---|
281 | ipsec_transmit(struct ifnet *ifp, struct mbuf *m) |
---|
282 | { |
---|
283 | IPSEC_RLOCK_TRACKER; |
---|
284 | struct ipsec_softc *sc; |
---|
285 | struct secpolicy *sp; |
---|
286 | struct ip *ip; |
---|
287 | uint32_t af; |
---|
288 | int error; |
---|
289 | |
---|
290 | #ifdef MAC |
---|
291 | error = mac_ifnet_check_transmit(ifp, m); |
---|
292 | if (error) { |
---|
293 | m_freem(m); |
---|
294 | goto err; |
---|
295 | } |
---|
296 | #endif |
---|
297 | error = ENETDOWN; |
---|
298 | sc = ifp->if_softc; |
---|
299 | if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || |
---|
300 | (ifp->if_flags & IFF_MONITOR) != 0 || |
---|
301 | (ifp->if_flags & IFF_UP) == 0) { |
---|
302 | m_freem(m); |
---|
303 | goto err; |
---|
304 | } |
---|
305 | |
---|
306 | /* Determine address family to correctly handle packet in BPF */ |
---|
307 | ip = mtod(m, struct ip *); |
---|
308 | switch (ip->ip_v) { |
---|
309 | #ifdef INET |
---|
310 | case IPVERSION: |
---|
311 | af = AF_INET; |
---|
312 | break; |
---|
313 | #endif |
---|
314 | #ifdef INET6 |
---|
315 | case (IPV6_VERSION >> 4): |
---|
316 | af = AF_INET6; |
---|
317 | break; |
---|
318 | #endif |
---|
319 | default: |
---|
320 | error = EAFNOSUPPORT; |
---|
321 | m_freem(m); |
---|
322 | goto err; |
---|
323 | } |
---|
324 | |
---|
325 | /* |
---|
326 | * Loop prevention. |
---|
327 | * XXX: for now just check presence of IPSEC_OUT_DONE mbuf tag. |
---|
328 | * We can read full chain and compare destination address, |
---|
329 | * proto and mode from xform_history with values from softc. |
---|
330 | */ |
---|
331 | if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) { |
---|
332 | m_freem(m); |
---|
333 | goto err; |
---|
334 | } |
---|
335 | |
---|
336 | IPSEC_RLOCK(sc); |
---|
337 | if (sc->family == 0) { |
---|
338 | IPSEC_RUNLOCK(sc); |
---|
339 | m_freem(m); |
---|
340 | goto err; |
---|
341 | } |
---|
342 | sp = ipsec_getpolicy(sc, IPSEC_DIR_OUTBOUND, af); |
---|
343 | key_addref(sp); |
---|
344 | M_SETFIB(m, sc->fibnum); |
---|
345 | IPSEC_RUNLOCK(sc); |
---|
346 | |
---|
347 | BPF_MTAP2(ifp, &af, sizeof(af), m); |
---|
348 | if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); |
---|
349 | if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len); |
---|
350 | |
---|
351 | switch (af) { |
---|
352 | #ifdef INET |
---|
353 | case AF_INET: |
---|
354 | error = ipsec4_process_packet(m, sp, NULL); |
---|
355 | break; |
---|
356 | #endif |
---|
357 | #ifdef INET6 |
---|
358 | case AF_INET6: |
---|
359 | error = ipsec6_process_packet(m, sp, NULL); |
---|
360 | break; |
---|
361 | #endif |
---|
362 | default: |
---|
363 | panic("%s: unknown address family\n", __func__); |
---|
364 | } |
---|
365 | err: |
---|
366 | if (error != 0) |
---|
367 | if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); |
---|
368 | return (error); |
---|
369 | } |
---|
370 | |
---|
371 | static void |
---|
372 | ipsec_qflush(struct ifnet *ifp __unused) |
---|
373 | { |
---|
374 | |
---|
375 | } |
---|
376 | |
---|
377 | static int |
---|
378 | ipsec_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, |
---|
379 | struct route *ro) |
---|
380 | { |
---|
381 | |
---|
382 | return (ifp->if_transmit(ifp, m)); |
---|
383 | } |
---|
384 | |
---|
385 | int |
---|
386 | ipsec_if_input(struct mbuf *m, struct secasvar *sav, uint32_t af) |
---|
387 | { |
---|
388 | IPSEC_SC_RLOCK_TRACKER; |
---|
389 | struct secasindex *saidx; |
---|
390 | struct ipsec_softc *sc; |
---|
391 | struct ifnet *ifp; |
---|
392 | |
---|
393 | if (sav->state != SADB_SASTATE_MATURE && |
---|
394 | sav->state != SADB_SASTATE_DYING) { |
---|
395 | m_freem(m); |
---|
396 | return (ENETDOWN); |
---|
397 | } |
---|
398 | |
---|
399 | if (sav->sah->saidx.mode != IPSEC_MODE_TUNNEL || |
---|
400 | sav->sah->saidx.proto != IPPROTO_ESP) |
---|
401 | return (0); |
---|
402 | |
---|
403 | IPSEC_SC_RLOCK(); |
---|
404 | /* |
---|
405 | * We only acquire SC_RLOCK() while we are doing search in |
---|
406 | * ipsec_sc_htbl. It is safe, because removing softc or changing |
---|
407 | * of reqid/addresses requires removing from hash table. |
---|
408 | */ |
---|
409 | LIST_FOREACH(sc, SCHASH_HASH(sav->sah->saidx.reqid), hash) { |
---|
410 | saidx = ipsec_getsaidx(sc, IPSEC_DIR_INBOUND, |
---|
411 | sav->sah->saidx.src.sa.sa_family); |
---|
412 | /* SA's reqid should match reqid in SP */ |
---|
413 | if (saidx == NULL || |
---|
414 | sav->sah->saidx.reqid != saidx->reqid) |
---|
415 | continue; |
---|
416 | /* SAH's addresses should match tunnel endpoints. */ |
---|
417 | if (key_sockaddrcmp(&sav->sah->saidx.dst.sa, |
---|
418 | &saidx->dst.sa, 0) != 0) |
---|
419 | continue; |
---|
420 | if (key_sockaddrcmp(&sav->sah->saidx.src.sa, |
---|
421 | &saidx->src.sa, 0) == 0) |
---|
422 | break; |
---|
423 | } |
---|
424 | if (sc == NULL) { |
---|
425 | IPSEC_SC_RUNLOCK(); |
---|
426 | /* Tunnel was not found. Nothing to do. */ |
---|
427 | return (0); |
---|
428 | } |
---|
429 | ifp = sc->ifp; |
---|
430 | if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || |
---|
431 | (ifp->if_flags & IFF_UP) == 0) { |
---|
432 | IPSEC_SC_RUNLOCK(); |
---|
433 | m_freem(m); |
---|
434 | return (ENETDOWN); |
---|
435 | } |
---|
436 | /* |
---|
437 | * We found matching and working tunnel. |
---|
438 | * Set its ifnet as receiving interface. |
---|
439 | */ |
---|
440 | m->m_pkthdr.rcvif = ifp; |
---|
441 | IPSEC_SC_RUNLOCK(); |
---|
442 | |
---|
443 | /* m_clrprotoflags(m); */ |
---|
444 | M_SETFIB(m, ifp->if_fib); |
---|
445 | BPF_MTAP2(ifp, &af, sizeof(af), m); |
---|
446 | if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); |
---|
447 | if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); |
---|
448 | if ((ifp->if_flags & IFF_MONITOR) != 0) { |
---|
449 | m_freem(m); |
---|
450 | return (ENETDOWN); |
---|
451 | } |
---|
452 | return (0); |
---|
453 | } |
---|
454 | |
---|
455 | /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ |
---|
456 | int |
---|
457 | ipsec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) |
---|
458 | { |
---|
459 | IPSEC_RLOCK_TRACKER; |
---|
460 | struct ifreq *ifr = (struct ifreq*)data; |
---|
461 | struct sockaddr *dst, *src; |
---|
462 | struct ipsec_softc *sc; |
---|
463 | struct secasindex *saidx; |
---|
464 | #ifdef INET |
---|
465 | struct sockaddr_in *sin = NULL; |
---|
466 | #endif |
---|
467 | #ifdef INET6 |
---|
468 | struct sockaddr_in6 *sin6 = NULL; |
---|
469 | #endif |
---|
470 | uint32_t reqid; |
---|
471 | int error; |
---|
472 | |
---|
473 | switch (cmd) { |
---|
474 | case SIOCSIFADDR: |
---|
475 | ifp->if_flags |= IFF_UP; |
---|
476 | case SIOCADDMULTI: |
---|
477 | case SIOCDELMULTI: |
---|
478 | case SIOCGIFMTU: |
---|
479 | case SIOCSIFFLAGS: |
---|
480 | return (0); |
---|
481 | case SIOCSIFMTU: |
---|
482 | if (ifr->ifr_mtu < IPSEC_MTU_MIN || |
---|
483 | ifr->ifr_mtu > IPSEC_MTU_MAX) |
---|
484 | return (EINVAL); |
---|
485 | else |
---|
486 | ifp->if_mtu = ifr->ifr_mtu; |
---|
487 | return (0); |
---|
488 | } |
---|
489 | sx_xlock(&ipsec_ioctl_sx); |
---|
490 | sc = ifp->if_softc; |
---|
491 | /* Check that softc is still here */ |
---|
492 | if (sc == NULL) { |
---|
493 | error = ENXIO; |
---|
494 | goto bad; |
---|
495 | } |
---|
496 | error = 0; |
---|
497 | switch (cmd) { |
---|
498 | case SIOCSIFPHYADDR: |
---|
499 | #ifdef INET6 |
---|
500 | case SIOCSIFPHYADDR_IN6: |
---|
501 | #endif |
---|
502 | error = EINVAL; |
---|
503 | switch (cmd) { |
---|
504 | #ifdef INET |
---|
505 | case SIOCSIFPHYADDR: |
---|
506 | src = (struct sockaddr *) |
---|
507 | &(((struct in_aliasreq *)data)->ifra_addr); |
---|
508 | dst = (struct sockaddr *) |
---|
509 | &(((struct in_aliasreq *)data)->ifra_dstaddr); |
---|
510 | break; |
---|
511 | #endif |
---|
512 | #ifdef INET6 |
---|
513 | case SIOCSIFPHYADDR_IN6: |
---|
514 | src = (struct sockaddr *) |
---|
515 | &(((struct in6_aliasreq *)data)->ifra_addr); |
---|
516 | dst = (struct sockaddr *) |
---|
517 | &(((struct in6_aliasreq *)data)->ifra_dstaddr); |
---|
518 | break; |
---|
519 | #endif |
---|
520 | default: |
---|
521 | goto bad; |
---|
522 | } |
---|
523 | /* sa_family must be equal */ |
---|
524 | if (src->sa_family != dst->sa_family || |
---|
525 | src->sa_len != dst->sa_len) |
---|
526 | goto bad; |
---|
527 | |
---|
528 | /* validate sa_len */ |
---|
529 | switch (src->sa_family) { |
---|
530 | #ifdef INET |
---|
531 | case AF_INET: |
---|
532 | if (src->sa_len != sizeof(struct sockaddr_in)) |
---|
533 | goto bad; |
---|
534 | break; |
---|
535 | #endif |
---|
536 | #ifdef INET6 |
---|
537 | case AF_INET6: |
---|
538 | if (src->sa_len != sizeof(struct sockaddr_in6)) |
---|
539 | goto bad; |
---|
540 | break; |
---|
541 | #endif |
---|
542 | default: |
---|
543 | error = EAFNOSUPPORT; |
---|
544 | goto bad; |
---|
545 | } |
---|
546 | /* check sa_family looks sane for the cmd */ |
---|
547 | error = EAFNOSUPPORT; |
---|
548 | switch (cmd) { |
---|
549 | #ifdef INET |
---|
550 | case SIOCSIFPHYADDR: |
---|
551 | if (src->sa_family == AF_INET) |
---|
552 | break; |
---|
553 | goto bad; |
---|
554 | #endif |
---|
555 | #ifdef INET6 |
---|
556 | case SIOCSIFPHYADDR_IN6: |
---|
557 | if (src->sa_family == AF_INET6) |
---|
558 | break; |
---|
559 | goto bad; |
---|
560 | #endif |
---|
561 | } |
---|
562 | error = EADDRNOTAVAIL; |
---|
563 | switch (src->sa_family) { |
---|
564 | #ifdef INET |
---|
565 | case AF_INET: |
---|
566 | if (satosin(src)->sin_addr.s_addr == INADDR_ANY || |
---|
567 | satosin(dst)->sin_addr.s_addr == INADDR_ANY) |
---|
568 | goto bad; |
---|
569 | break; |
---|
570 | #endif |
---|
571 | #ifdef INET6 |
---|
572 | case AF_INET6: |
---|
573 | if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr) |
---|
574 | || |
---|
575 | IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr)) |
---|
576 | goto bad; |
---|
577 | /* |
---|
578 | * Check validity of the scope zone ID of the |
---|
579 | * addresses, and convert it into the kernel |
---|
580 | * internal form if necessary. |
---|
581 | */ |
---|
582 | error = sa6_embedscope(satosin6(src), 0); |
---|
583 | if (error != 0) |
---|
584 | goto bad; |
---|
585 | error = sa6_embedscope(satosin6(dst), 0); |
---|
586 | if (error != 0) |
---|
587 | goto bad; |
---|
588 | #endif |
---|
589 | }; |
---|
590 | error = ipsec_set_addresses(ifp, src, dst); |
---|
591 | break; |
---|
592 | case SIOCDIFPHYADDR: |
---|
593 | ipsec_delete_tunnel(ifp, 0); |
---|
594 | break; |
---|
595 | case SIOCGIFPSRCADDR: |
---|
596 | case SIOCGIFPDSTADDR: |
---|
597 | #ifdef INET6 |
---|
598 | case SIOCGIFPSRCADDR_IN6: |
---|
599 | case SIOCGIFPDSTADDR_IN6: |
---|
600 | #endif |
---|
601 | IPSEC_RLOCK(sc); |
---|
602 | if (sc->family == 0) { |
---|
603 | IPSEC_RUNLOCK(sc); |
---|
604 | error = EADDRNOTAVAIL; |
---|
605 | break; |
---|
606 | } |
---|
607 | saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family); |
---|
608 | switch (cmd) { |
---|
609 | #ifdef INET |
---|
610 | case SIOCGIFPSRCADDR: |
---|
611 | case SIOCGIFPDSTADDR: |
---|
612 | if (saidx->src.sa.sa_family != AF_INET) { |
---|
613 | error = EADDRNOTAVAIL; |
---|
614 | break; |
---|
615 | } |
---|
616 | sin = (struct sockaddr_in *)&ifr->ifr_addr; |
---|
617 | memset(sin, 0, sizeof(*sin)); |
---|
618 | sin->sin_family = AF_INET; |
---|
619 | sin->sin_len = sizeof(*sin); |
---|
620 | break; |
---|
621 | #endif |
---|
622 | #ifdef INET6 |
---|
623 | case SIOCGIFPSRCADDR_IN6: |
---|
624 | case SIOCGIFPDSTADDR_IN6: |
---|
625 | if (saidx->src.sa.sa_family != AF_INET6) { |
---|
626 | error = EADDRNOTAVAIL; |
---|
627 | break; |
---|
628 | } |
---|
629 | sin6 = (struct sockaddr_in6 *) |
---|
630 | &(((struct in6_ifreq *)data)->ifr_addr); |
---|
631 | memset(sin6, 0, sizeof(*sin6)); |
---|
632 | sin6->sin6_family = AF_INET6; |
---|
633 | sin6->sin6_len = sizeof(*sin6); |
---|
634 | break; |
---|
635 | #endif |
---|
636 | default: |
---|
637 | error = EAFNOSUPPORT; |
---|
638 | } |
---|
639 | if (error == 0) { |
---|
640 | switch (cmd) { |
---|
641 | #ifdef INET |
---|
642 | case SIOCGIFPSRCADDR: |
---|
643 | sin->sin_addr = saidx->src.sin.sin_addr; |
---|
644 | break; |
---|
645 | case SIOCGIFPDSTADDR: |
---|
646 | sin->sin_addr = saidx->dst.sin.sin_addr; |
---|
647 | break; |
---|
648 | #endif |
---|
649 | #ifdef INET6 |
---|
650 | case SIOCGIFPSRCADDR_IN6: |
---|
651 | sin6->sin6_addr = saidx->src.sin6.sin6_addr; |
---|
652 | break; |
---|
653 | case SIOCGIFPDSTADDR_IN6: |
---|
654 | sin6->sin6_addr = saidx->dst.sin6.sin6_addr; |
---|
655 | break; |
---|
656 | #endif |
---|
657 | } |
---|
658 | } |
---|
659 | IPSEC_RUNLOCK(sc); |
---|
660 | if (error != 0) |
---|
661 | break; |
---|
662 | switch (cmd) { |
---|
663 | #ifdef INET |
---|
664 | case SIOCGIFPSRCADDR: |
---|
665 | case SIOCGIFPDSTADDR: |
---|
666 | error = prison_if(curthread->td_ucred, |
---|
667 | (struct sockaddr *)sin); |
---|
668 | if (error != 0) |
---|
669 | memset(sin, 0, sizeof(*sin)); |
---|
670 | break; |
---|
671 | #endif |
---|
672 | #ifdef INET6 |
---|
673 | case SIOCGIFPSRCADDR_IN6: |
---|
674 | case SIOCGIFPDSTADDR_IN6: |
---|
675 | error = prison_if(curthread->td_ucred, |
---|
676 | (struct sockaddr *)sin6); |
---|
677 | if (error == 0) |
---|
678 | error = sa6_recoverscope(sin6); |
---|
679 | if (error != 0) |
---|
680 | memset(sin6, 0, sizeof(*sin6)); |
---|
681 | #endif |
---|
682 | } |
---|
683 | break; |
---|
684 | case SIOCGTUNFIB: |
---|
685 | ifr->ifr_fib = sc->fibnum; |
---|
686 | break; |
---|
687 | case SIOCSTUNFIB: |
---|
688 | if ((error = priv_check(curthread, PRIV_NET_SETIFFIB)) != 0) |
---|
689 | break; |
---|
690 | if (ifr->ifr_fib >= rt_numfibs) |
---|
691 | error = EINVAL; |
---|
692 | else |
---|
693 | sc->fibnum = ifr->ifr_fib; |
---|
694 | break; |
---|
695 | case IPSECGREQID: |
---|
696 | reqid = sc->reqid; |
---|
697 | error = copyout(&reqid, ifr->ifr_data, sizeof(reqid)); |
---|
698 | break; |
---|
699 | case IPSECSREQID: |
---|
700 | if ((error = priv_check(curthread, PRIV_NET_SETIFCAP)) != 0) |
---|
701 | break; |
---|
702 | error = copyin(ifr->ifr_data, &reqid, sizeof(reqid)); |
---|
703 | if (error != 0) |
---|
704 | break; |
---|
705 | error = ipsec_set_reqid(ifp, reqid); |
---|
706 | break; |
---|
707 | default: |
---|
708 | error = EINVAL; |
---|
709 | break; |
---|
710 | } |
---|
711 | bad: |
---|
712 | sx_xunlock(&ipsec_ioctl_sx); |
---|
713 | return (error); |
---|
714 | } |
---|
715 | |
---|
716 | /* |
---|
717 | * Allocate new private security policies for tunneling interface. |
---|
718 | * Each tunneling interface has following security policies for |
---|
719 | * both AF: |
---|
720 | * 0.0.0.0/0[any] 0.0.0.0/0[any] -P in \ |
---|
721 | * ipsec esp/tunnel/RemoteIP-LocalIP/unique:reqid |
---|
722 | * 0.0.0.0/0[any] 0.0.0.0/0[any] -P out \ |
---|
723 | * ipsec esp/tunnel/LocalIP-RemoteIP/unique:reqid |
---|
724 | */ |
---|
725 | static int |
---|
726 | ipsec_newpolicies(struct ipsec_softc *sc, struct secpolicy *sp[IPSEC_SPCOUNT], |
---|
727 | const struct sockaddr *src, const struct sockaddr *dst, uint32_t reqid) |
---|
728 | { |
---|
729 | struct ipsecrequest *isr; |
---|
730 | int i; |
---|
731 | |
---|
732 | memset(sp, 0, sizeof(struct secpolicy *) * IPSEC_SPCOUNT); |
---|
733 | for (i = 0; i < IPSEC_SPCOUNT; i++) { |
---|
734 | if ((sp[i] = key_newsp()) == NULL) |
---|
735 | goto fail; |
---|
736 | if ((isr = ipsec_newisr()) == NULL) |
---|
737 | goto fail; |
---|
738 | |
---|
739 | sp[i]->policy = IPSEC_POLICY_IPSEC; |
---|
740 | sp[i]->state = IPSEC_SPSTATE_DEAD; |
---|
741 | sp[i]->req[sp[i]->tcount++] = isr; |
---|
742 | sp[i]->created = time_second; |
---|
743 | /* Use priority field to store if_index */ |
---|
744 | sp[i]->priority = sc->ifp->if_index; |
---|
745 | isr->level = IPSEC_LEVEL_UNIQUE; |
---|
746 | isr->saidx.proto = IPPROTO_ESP; |
---|
747 | isr->saidx.mode = IPSEC_MODE_TUNNEL; |
---|
748 | isr->saidx.reqid = reqid; |
---|
749 | if (i % 2 == 0) { |
---|
750 | sp[i]->spidx.dir = IPSEC_DIR_INBOUND; |
---|
751 | bcopy(src, &isr->saidx.dst, src->sa_len); |
---|
752 | bcopy(dst, &isr->saidx.src, dst->sa_len); |
---|
753 | } else { |
---|
754 | sp[i]->spidx.dir = IPSEC_DIR_OUTBOUND; |
---|
755 | bcopy(src, &isr->saidx.src, src->sa_len); |
---|
756 | bcopy(dst, &isr->saidx.dst, dst->sa_len); |
---|
757 | } |
---|
758 | sp[i]->spidx.ul_proto = IPSEC_ULPROTO_ANY; |
---|
759 | #ifdef INET |
---|
760 | if (i < 2) { |
---|
761 | sp[i]->spidx.src.sa.sa_family = |
---|
762 | sp[i]->spidx.dst.sa.sa_family = AF_INET; |
---|
763 | sp[i]->spidx.src.sa.sa_len = |
---|
764 | sp[i]->spidx.dst.sa.sa_len = |
---|
765 | sizeof(struct sockaddr_in); |
---|
766 | continue; |
---|
767 | } |
---|
768 | #endif |
---|
769 | #ifdef INET6 |
---|
770 | sp[i]->spidx.src.sa.sa_family = |
---|
771 | sp[i]->spidx.dst.sa.sa_family = AF_INET6; |
---|
772 | sp[i]->spidx.src.sa.sa_len = |
---|
773 | sp[i]->spidx.dst.sa.sa_len = sizeof(struct sockaddr_in6); |
---|
774 | #endif |
---|
775 | } |
---|
776 | return (0); |
---|
777 | fail: |
---|
778 | for (i = 0; i < IPSEC_SPCOUNT; i++) |
---|
779 | key_freesp(&sp[i]); |
---|
780 | return (ENOMEM); |
---|
781 | } |
---|
782 | |
---|
783 | static int |
---|
784 | ipsec_check_reqid(uint32_t reqid) |
---|
785 | { |
---|
786 | struct ipsec_softc *sc; |
---|
787 | |
---|
788 | IPSEC_SC_RLOCK_ASSERT(); |
---|
789 | LIST_FOREACH(sc, &V_ipsec_sc_list, chain) { |
---|
790 | if (sc->reqid == reqid) |
---|
791 | return (EEXIST); |
---|
792 | } |
---|
793 | return (0); |
---|
794 | } |
---|
795 | |
---|
796 | /* |
---|
797 | * We use key_newreqid() to automatically obtain unique reqid. |
---|
798 | * Then we check that given id is unique, i.e. it is not used by |
---|
799 | * another if_ipsec(4) interface. This macro limits the number of |
---|
800 | * tries to get unique id. |
---|
801 | */ |
---|
802 | #define IPSEC_REQID_TRYCNT 64 |
---|
803 | static int |
---|
804 | ipsec_init_reqid(struct ipsec_softc *sc) |
---|
805 | { |
---|
806 | uint32_t reqid; |
---|
807 | int trycount; |
---|
808 | |
---|
809 | IPSEC_SC_RLOCK_ASSERT(); |
---|
810 | |
---|
811 | if (sc->reqid != 0) /* already initialized */ |
---|
812 | return (0); |
---|
813 | |
---|
814 | trycount = IPSEC_REQID_TRYCNT; |
---|
815 | while (--trycount > 0) { |
---|
816 | reqid = key_newreqid(); |
---|
817 | if (ipsec_check_reqid(reqid) == 0) |
---|
818 | break; |
---|
819 | } |
---|
820 | if (trycount == 0) |
---|
821 | return (EEXIST); |
---|
822 | sc->reqid = reqid; |
---|
823 | return (0); |
---|
824 | } |
---|
825 | |
---|
826 | /* |
---|
827 | * Set or update reqid for given tunneling interface. |
---|
828 | * When specified reqid is zero, generate new one. |
---|
829 | * We are protected by ioctl_sx lock from concurrent id generation. |
---|
830 | * Also softc would not disappear while we hold ioctl_sx lock. |
---|
831 | */ |
---|
832 | static int |
---|
833 | ipsec_set_reqid(struct ifnet *ifp, uint32_t reqid) |
---|
834 | { |
---|
835 | IPSEC_SC_RLOCK_TRACKER; |
---|
836 | struct ipsec_softc *sc; |
---|
837 | struct secasindex *saidx; |
---|
838 | |
---|
839 | sx_assert(&ipsec_ioctl_sx, SA_XLOCKED); |
---|
840 | |
---|
841 | sc = ifp->if_softc; |
---|
842 | if (sc->reqid == reqid && reqid != 0) |
---|
843 | return (0); |
---|
844 | |
---|
845 | IPSEC_SC_RLOCK(); |
---|
846 | if (reqid != 0) { |
---|
847 | /* Check that specified reqid doesn't exist */ |
---|
848 | if (ipsec_check_reqid(reqid) != 0) { |
---|
849 | IPSEC_SC_RUNLOCK(); |
---|
850 | return (EEXIST); |
---|
851 | } |
---|
852 | sc->reqid = reqid; |
---|
853 | } else { |
---|
854 | /* Generate new reqid */ |
---|
855 | if (ipsec_init_reqid(sc) != 0) { |
---|
856 | IPSEC_SC_RUNLOCK(); |
---|
857 | return (EEXIST); |
---|
858 | } |
---|
859 | } |
---|
860 | IPSEC_SC_RUNLOCK(); |
---|
861 | |
---|
862 | /* Tunnel isn't fully configured, just return. */ |
---|
863 | if (sc->family == 0) |
---|
864 | return (0); |
---|
865 | |
---|
866 | saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family); |
---|
867 | KASSERT(saidx != NULL, |
---|
868 | ("saidx is NULL, but family is %d", sc->family)); |
---|
869 | return (ipsec_set_tunnel(sc, &saidx->src.sa, &saidx->dst.sa, |
---|
870 | sc->reqid)); |
---|
871 | } |
---|
872 | |
---|
873 | /* |
---|
874 | * Set tunnel endpoints addresses. |
---|
875 | */ |
---|
876 | static int |
---|
877 | ipsec_set_addresses(struct ifnet *ifp, struct sockaddr *src, |
---|
878 | struct sockaddr *dst) |
---|
879 | { |
---|
880 | IPSEC_SC_RLOCK_TRACKER; |
---|
881 | struct ipsec_softc *sc, *tsc; |
---|
882 | struct secasindex *saidx; |
---|
883 | |
---|
884 | sx_assert(&ipsec_ioctl_sx, SA_XLOCKED); |
---|
885 | |
---|
886 | sc = ifp->if_softc; |
---|
887 | if (sc->family != 0) { |
---|
888 | saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, |
---|
889 | src->sa_family); |
---|
890 | if (saidx != NULL && saidx->reqid == sc->reqid && |
---|
891 | key_sockaddrcmp(&saidx->src.sa, src, 0) == 0 && |
---|
892 | key_sockaddrcmp(&saidx->dst.sa, dst, 0) == 0) |
---|
893 | return (0); /* Nothing has been changed. */ |
---|
894 | |
---|
895 | } |
---|
896 | /* |
---|
897 | * We cannot service IPsec tunnel when source address is |
---|
898 | * not our own. |
---|
899 | */ |
---|
900 | #ifdef INET |
---|
901 | if (src->sa_family == AF_INET && |
---|
902 | in_localip(satosin(src)->sin_addr) == 0) |
---|
903 | return (EADDRNOTAVAIL); |
---|
904 | #endif |
---|
905 | #ifdef INET6 |
---|
906 | /* |
---|
907 | * NOTE: IPv6 addresses are in kernel internal form with |
---|
908 | * embedded scope zone id. |
---|
909 | */ |
---|
910 | if (src->sa_family == AF_INET6 && |
---|
911 | in6_localip(&satosin6(src)->sin6_addr) == 0) |
---|
912 | return (EADDRNOTAVAIL); |
---|
913 | #endif |
---|
914 | /* Check that given addresses aren't already configured */ |
---|
915 | IPSEC_SC_RLOCK(); |
---|
916 | LIST_FOREACH(tsc, &V_ipsec_sc_list, chain) { |
---|
917 | if (tsc == sc || tsc->family != src->sa_family) |
---|
918 | continue; |
---|
919 | saidx = ipsec_getsaidx(tsc, IPSEC_DIR_OUTBOUND, tsc->family); |
---|
920 | if (key_sockaddrcmp(&saidx->src.sa, src, 0) == 0 && |
---|
921 | key_sockaddrcmp(&saidx->dst.sa, dst, 0) == 0) { |
---|
922 | /* We already have tunnel with such addresses */ |
---|
923 | IPSEC_SC_RUNLOCK(); |
---|
924 | return (EADDRNOTAVAIL); |
---|
925 | } |
---|
926 | } |
---|
927 | /* If reqid is not set, generate new one. */ |
---|
928 | if (ipsec_init_reqid(sc) != 0) { |
---|
929 | IPSEC_SC_RUNLOCK(); |
---|
930 | return (EEXIST); |
---|
931 | } |
---|
932 | IPSEC_SC_RUNLOCK(); |
---|
933 | return (ipsec_set_tunnel(sc, src, dst, sc->reqid)); |
---|
934 | } |
---|
935 | |
---|
936 | static int |
---|
937 | ipsec_set_tunnel(struct ipsec_softc *sc, struct sockaddr *src, |
---|
938 | struct sockaddr *dst, uint32_t reqid) |
---|
939 | { |
---|
940 | struct secpolicy *sp[IPSEC_SPCOUNT]; |
---|
941 | struct secpolicy *oldsp[IPSEC_SPCOUNT]; |
---|
942 | int i, f; |
---|
943 | |
---|
944 | sx_assert(&ipsec_ioctl_sx, SA_XLOCKED); |
---|
945 | |
---|
946 | /* Allocate SP with new addresses. */ |
---|
947 | if (ipsec_newpolicies(sc, sp, src, dst, reqid) == 0) { |
---|
948 | /* Add new policies to SPDB */ |
---|
949 | if (key_register_ifnet(sp, IPSEC_SPCOUNT) != 0) { |
---|
950 | for (i = 0; i < IPSEC_SPCOUNT; i++) |
---|
951 | key_freesp(&sp[i]); |
---|
952 | return (EAGAIN); |
---|
953 | } |
---|
954 | IPSEC_SC_WLOCK(); |
---|
955 | if ((f = sc->family) != 0) |
---|
956 | LIST_REMOVE(sc, hash); |
---|
957 | IPSEC_WLOCK(sc); |
---|
958 | for (i = 0; i < IPSEC_SPCOUNT; i++) { |
---|
959 | oldsp[i] = sc->sp[i]; |
---|
960 | sc->sp[i] = sp[i]; |
---|
961 | } |
---|
962 | sc->family = src->sa_family; |
---|
963 | IPSEC_WUNLOCK(sc); |
---|
964 | LIST_INSERT_HEAD(SCHASH_HASH(sc->reqid), sc, hash); |
---|
965 | IPSEC_SC_WUNLOCK(); |
---|
966 | } else { |
---|
967 | sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING; |
---|
968 | return (ENOMEM); |
---|
969 | } |
---|
970 | |
---|
971 | sc->ifp->if_drv_flags |= IFF_DRV_RUNNING; |
---|
972 | if (f != 0) { |
---|
973 | key_unregister_ifnet(oldsp, IPSEC_SPCOUNT); |
---|
974 | for (i = 0; i < IPSEC_SPCOUNT; i++) |
---|
975 | key_freesp(&oldsp[i]); |
---|
976 | } |
---|
977 | return (0); |
---|
978 | } |
---|
979 | |
---|
980 | static void |
---|
981 | ipsec_delete_tunnel(struct ifnet *ifp, int locked) |
---|
982 | { |
---|
983 | struct ipsec_softc *sc = ifp->if_softc; |
---|
984 | struct secpolicy *oldsp[IPSEC_SPCOUNT]; |
---|
985 | int i; |
---|
986 | |
---|
987 | sx_assert(&ipsec_ioctl_sx, SA_XLOCKED); |
---|
988 | |
---|
989 | ifp->if_drv_flags &= ~IFF_DRV_RUNNING; |
---|
990 | if (sc->family != 0) { |
---|
991 | if (!locked) |
---|
992 | IPSEC_SC_WLOCK(); |
---|
993 | /* Remove from hash table */ |
---|
994 | LIST_REMOVE(sc, hash); |
---|
995 | IPSEC_WLOCK(sc); |
---|
996 | for (i = 0; i < IPSEC_SPCOUNT; i++) { |
---|
997 | oldsp[i] = sc->sp[i]; |
---|
998 | sc->sp[i] = NULL; |
---|
999 | } |
---|
1000 | sc->family = 0; |
---|
1001 | IPSEC_WUNLOCK(sc); |
---|
1002 | if (!locked) |
---|
1003 | IPSEC_SC_WUNLOCK(); |
---|
1004 | key_unregister_ifnet(oldsp, IPSEC_SPCOUNT); |
---|
1005 | for (i = 0; i < IPSEC_SPCOUNT; i++) |
---|
1006 | key_freesp(&oldsp[i]); |
---|
1007 | } |
---|
1008 | } |
---|