FreeBSD/Linux Kernel Cross Reference
sys/net/if_gif.c
1 /* $FreeBSD$ */
2 /* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */
3
4 /*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_mac.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/mac.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/module.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/protosw.h>
51 #include <sys/conf.h>
52 #include <machine/cpu.h>
53
54 #include <net/if.h>
55 #include <net/if_clone.h>
56 #include <net/if_types.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59 #include <net/bpf.h>
60
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #ifdef INET
65 #include <netinet/in_var.h>
66 #include <netinet/in_gif.h>
67 #include <netinet/ip_var.h>
68 #endif /* INET */
69
70 #ifdef INET6
71 #ifndef INET
72 #include <netinet/in.h>
73 #endif
74 #include <netinet6/in6_var.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/in6_gif.h>
78 #include <netinet6/ip6protosw.h>
79 #endif /* INET6 */
80
81 #include <netinet/ip_encap.h>
82 #include <net/if_gif.h>
83
84 #include <net/net_osdep.h>
85
86 #define GIFNAME "gif"
87
88 /*
89 * gif_mtx protects the global gif_softc_list.
90 * XXX: Per-softc locking is still required.
91 */
92 static struct mtx gif_mtx;
93 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
94 static LIST_HEAD(, gif_softc) gif_softc_list;
95
96 void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
97 void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
98 void (*ng_gif_attach_p)(struct ifnet *ifp);
99 void (*ng_gif_detach_p)(struct ifnet *ifp);
100
101 static int gif_clone_create(struct if_clone *, int);
102 static void gif_clone_destroy(struct ifnet *);
103
104 IFC_SIMPLE_DECLARE(gif, 0);
105
106 static int gifmodevent(module_t, int, void *);
107
108 SYSCTL_DECL(_net_link);
109 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
110 "Generic Tunnel Interface");
111 #ifndef MAX_GIF_NEST
112 /*
113 * This macro controls the default upper limitation on nesting of gif tunnels.
114 * Since, setting a large value to this macro with a careless configuration
115 * may introduce system crash, we don't allow any nestings by default.
116 * If you need to configure nested gif tunnels, you can define this macro
117 * in your kernel configuration file. However, if you do so, please be
118 * careful to configure the tunnels so that it won't make a loop.
119 */
120 #define MAX_GIF_NEST 1
121 #endif
122 static int max_gif_nesting = MAX_GIF_NEST;
123 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
124 &max_gif_nesting, 0, "Max nested tunnels");
125
126 /*
127 * By default, we disallow creation of multiple tunnels between the same
128 * pair of addresses. Some applications require this functionality so
129 * we allow control over this check here.
130 */
131 #ifdef XBONEHACK
132 static int parallel_tunnels = 1;
133 #else
134 static int parallel_tunnels = 0;
135 #endif
136 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
137 ¶llel_tunnels, 0, "Allow parallel tunnels?");
138
139 static int
140 gif_clone_create(ifc, unit)
141 struct if_clone *ifc;
142 int unit;
143 {
144 struct gif_softc *sc;
145
146 sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
147
148 sc->gif_if.if_softc = sc;
149 if_initname(&sc->gif_if, ifc->ifc_name, unit);
150
151 gifattach0(sc);
152
153 mtx_lock(&gif_mtx);
154 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
155 mtx_unlock(&gif_mtx);
156 return (0);
157 }
158
159 void
160 gifattach0(sc)
161 struct gif_softc *sc;
162 {
163
164 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
165
166 sc->gif_if.if_addrlen = 0;
167 sc->gif_if.if_mtu = GIF_MTU;
168 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
169 #if 0
170 /* turn off ingress filter */
171 sc->gif_if.if_flags |= IFF_LINK2;
172 #endif
173 sc->gif_if.if_ioctl = gif_ioctl;
174 sc->gif_if.if_output = gif_output;
175 sc->gif_if.if_type = IFT_GIF;
176 sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
177 if_attach(&sc->gif_if);
178 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
179 if (ng_gif_attach_p != NULL)
180 (*ng_gif_attach_p)(&sc->gif_if);
181 }
182
183 static void
184 gif_destroy(struct gif_softc *sc)
185 {
186 struct ifnet *ifp = &sc->gif_if;
187 int err;
188
189 gif_delete_tunnel(ifp);
190 #ifdef INET6
191 if (sc->encap_cookie6 != NULL) {
192 err = encap_detach(sc->encap_cookie6);
193 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
194 }
195 #endif
196 #ifdef INET
197 if (sc->encap_cookie4 != NULL) {
198 err = encap_detach(sc->encap_cookie4);
199 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
200 }
201 #endif
202
203 if (ng_gif_detach_p != NULL)
204 (*ng_gif_detach_p)(ifp);
205 bpfdetach(ifp);
206 if_detach(ifp);
207
208 free(sc, M_GIF);
209 }
210
211 static void
212 gif_clone_destroy(ifp)
213 struct ifnet *ifp;
214 {
215 struct gif_softc *sc = ifp->if_softc;
216
217 mtx_lock(&gif_mtx);
218 LIST_REMOVE(sc, gif_list);
219 mtx_unlock(&gif_mtx);
220 gif_destroy(sc);
221 }
222
223 static int
224 gifmodevent(mod, type, data)
225 module_t mod;
226 int type;
227 void *data;
228 {
229 struct gif_softc *sc;
230
231 switch (type) {
232 case MOD_LOAD:
233 mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
234 LIST_INIT(&gif_softc_list);
235 if_clone_attach(&gif_cloner);
236
237 #ifdef INET6
238 ip6_gif_hlim = GIF_HLIM;
239 #endif
240
241 break;
242 case MOD_UNLOAD:
243 if_clone_detach(&gif_cloner);
244
245 mtx_lock(&gif_mtx);
246 while ((sc = LIST_FIRST(&gif_softc_list)) != NULL) {
247 LIST_REMOVE(sc, gif_list);
248 mtx_unlock(&gif_mtx);
249 gif_destroy(sc);
250 mtx_lock(&gif_mtx);
251 }
252 mtx_unlock(&gif_mtx);
253 mtx_destroy(&gif_mtx);
254 #ifdef INET6
255 ip6_gif_hlim = 0;
256 #endif
257 break;
258 default:
259 return EOPNOTSUPP;
260 }
261 return 0;
262 }
263
264 static moduledata_t gif_mod = {
265 "if_gif",
266 gifmodevent,
267 0
268 };
269
270 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
271 MODULE_VERSION(if_gif, 1);
272
273 int
274 gif_encapcheck(m, off, proto, arg)
275 const struct mbuf *m;
276 int off;
277 int proto;
278 void *arg;
279 {
280 struct ip ip;
281 struct gif_softc *sc;
282
283 sc = (struct gif_softc *)arg;
284 if (sc == NULL)
285 return 0;
286
287 if ((sc->gif_if.if_flags & IFF_UP) == 0)
288 return 0;
289
290 /* no physical address */
291 if (!sc->gif_psrc || !sc->gif_pdst)
292 return 0;
293
294 switch (proto) {
295 #ifdef INET
296 case IPPROTO_IPV4:
297 break;
298 #endif
299 #ifdef INET6
300 case IPPROTO_IPV6:
301 break;
302 #endif
303 default:
304 return 0;
305 }
306
307 /* Bail on short packets */
308 if (m->m_pkthdr.len < sizeof(ip))
309 return 0;
310
311 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
312
313 switch (ip.ip_v) {
314 #ifdef INET
315 case 4:
316 if (sc->gif_psrc->sa_family != AF_INET ||
317 sc->gif_pdst->sa_family != AF_INET)
318 return 0;
319 return gif_encapcheck4(m, off, proto, arg);
320 #endif
321 #ifdef INET6
322 case 6:
323 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
324 return 0;
325 if (sc->gif_psrc->sa_family != AF_INET6 ||
326 sc->gif_pdst->sa_family != AF_INET6)
327 return 0;
328 return gif_encapcheck6(m, off, proto, arg);
329 #endif
330 default:
331 return 0;
332 }
333 }
334
335 int
336 gif_output(ifp, m, dst, rt)
337 struct ifnet *ifp;
338 struct mbuf *m;
339 struct sockaddr *dst;
340 struct rtentry *rt; /* added in net2 */
341 {
342 struct gif_softc *sc = (struct gif_softc*)ifp;
343 struct m_tag *mtag;
344 int error = 0;
345 int gif_called;
346
347 #ifdef MAC
348 error = mac_check_ifnet_transmit(ifp, m);
349 if (error) {
350 m_freem(m);
351 goto end;
352 }
353 #endif
354
355 /*
356 * gif may cause infinite recursion calls when misconfigured.
357 * We'll prevent this by detecting loops.
358 *
359 * High nesting level may cause stack exhaustion.
360 * We'll prevent this by introducing upper limit.
361 */
362 gif_called = 1;
363 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
364 while (mtag != NULL) {
365 if (*(struct ifnet **)(mtag + 1) == ifp) {
366 log(LOG_NOTICE,
367 "gif_output: loop detected on %s\n",
368 (*(struct ifnet **)(mtag + 1))->if_xname);
369 m_freem(m);
370 error = EIO; /* is there better errno? */
371 goto end;
372 }
373 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
374 gif_called++;
375 }
376 if (gif_called > max_gif_nesting) {
377 log(LOG_NOTICE,
378 "gif_output: recursively called too many times(%d)\n",
379 gif_called);
380 m_freem(m);
381 error = EIO; /* is there better errno? */
382 goto end;
383 }
384 mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
385 M_NOWAIT);
386 if (mtag == NULL) {
387 m_freem(m);
388 error = ENOMEM;
389 goto end;
390 }
391 *(struct ifnet **)(mtag + 1) = ifp;
392 m_tag_prepend(m, mtag);
393
394 m->m_flags &= ~(M_BCAST|M_MCAST);
395 if (!(ifp->if_flags & IFF_UP) ||
396 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
397 m_freem(m);
398 error = ENETDOWN;
399 goto end;
400 }
401
402 if (ifp->if_bpf) {
403 u_int32_t af = dst->sa_family;
404 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
405 }
406 ifp->if_opackets++;
407 ifp->if_obytes += m->m_pkthdr.len;
408
409 /* inner AF-specific encapsulation */
410
411 /* XXX should we check if our outer source is legal? */
412
413 /* dispatch to output logic based on outer AF */
414 switch (sc->gif_psrc->sa_family) {
415 #ifdef INET
416 case AF_INET:
417 error = in_gif_output(ifp, dst->sa_family, m);
418 break;
419 #endif
420 #ifdef INET6
421 case AF_INET6:
422 error = in6_gif_output(ifp, dst->sa_family, m);
423 break;
424 #endif
425 default:
426 m_freem(m);
427 error = ENETDOWN;
428 goto end;
429 }
430
431 end:
432 if (error)
433 ifp->if_oerrors++;
434 return error;
435 }
436
437 void
438 gif_input(m, af, ifp)
439 struct mbuf *m;
440 int af;
441 struct ifnet *ifp;
442 {
443 int isr;
444
445 if (ifp == NULL) {
446 /* just in case */
447 m_freem(m);
448 return;
449 }
450
451 m->m_pkthdr.rcvif = ifp;
452
453 #ifdef MAC
454 mac_create_mbuf_from_ifnet(ifp, m);
455 #endif
456
457 if (ifp->if_bpf) {
458 u_int32_t af1 = af;
459 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
460 }
461
462 if (ng_gif_input_p != NULL) {
463 (*ng_gif_input_p)(ifp, &m, af);
464 if (m == NULL)
465 return;
466 }
467
468 /*
469 * Put the packet to the network layer input queue according to the
470 * specified address family.
471 * Note: older versions of gif_input directly called network layer
472 * input functions, e.g. ip6_input, here. We changed the policy to
473 * prevent too many recursive calls of such input functions, which
474 * might cause kernel panic. But the change may introduce another
475 * problem; if the input queue is full, packets are discarded.
476 * The kernel stack overflow really happened, and we believed
477 * queue-full rarely occurs, so we changed the policy.
478 */
479 switch (af) {
480 #ifdef INET
481 case AF_INET:
482 isr = NETISR_IP;
483 break;
484 #endif
485 #ifdef INET6
486 case AF_INET6:
487 isr = NETISR_IPV6;
488 break;
489 #endif
490 default:
491 if (ng_gif_input_orphan_p != NULL)
492 (*ng_gif_input_orphan_p)(ifp, m, af);
493 else
494 m_freem(m);
495 return;
496 }
497
498 ifp->if_ipackets++;
499 ifp->if_ibytes += m->m_pkthdr.len;
500 netisr_dispatch(isr, m);
501 }
502
503 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
504 int
505 gif_ioctl(ifp, cmd, data)
506 struct ifnet *ifp;
507 u_long cmd;
508 caddr_t data;
509 {
510 struct gif_softc *sc = (struct gif_softc*)ifp;
511 struct ifreq *ifr = (struct ifreq*)data;
512 int error = 0, size;
513 struct sockaddr *dst, *src;
514 #ifdef SIOCSIFMTU /* xxx */
515 u_long mtu;
516 #endif
517
518 switch (cmd) {
519 case SIOCSIFADDR:
520 ifp->if_flags |= IFF_UP;
521 break;
522
523 case SIOCSIFDSTADDR:
524 break;
525
526 case SIOCADDMULTI:
527 case SIOCDELMULTI:
528 break;
529
530 #ifdef SIOCSIFMTU /* xxx */
531 case SIOCGIFMTU:
532 break;
533
534 case SIOCSIFMTU:
535 mtu = ifr->ifr_mtu;
536 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
537 return (EINVAL);
538 ifp->if_mtu = mtu;
539 break;
540 #endif /* SIOCSIFMTU */
541
542 #ifdef INET
543 case SIOCSIFPHYADDR:
544 #endif
545 #ifdef INET6
546 case SIOCSIFPHYADDR_IN6:
547 #endif /* INET6 */
548 case SIOCSLIFPHYADDR:
549 switch (cmd) {
550 #ifdef INET
551 case SIOCSIFPHYADDR:
552 src = (struct sockaddr *)
553 &(((struct in_aliasreq *)data)->ifra_addr);
554 dst = (struct sockaddr *)
555 &(((struct in_aliasreq *)data)->ifra_dstaddr);
556 break;
557 #endif
558 #ifdef INET6
559 case SIOCSIFPHYADDR_IN6:
560 src = (struct sockaddr *)
561 &(((struct in6_aliasreq *)data)->ifra_addr);
562 dst = (struct sockaddr *)
563 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
564 break;
565 #endif
566 case SIOCSLIFPHYADDR:
567 src = (struct sockaddr *)
568 &(((struct if_laddrreq *)data)->addr);
569 dst = (struct sockaddr *)
570 &(((struct if_laddrreq *)data)->dstaddr);
571 break;
572 default:
573 return EINVAL;
574 }
575
576 /* sa_family must be equal */
577 if (src->sa_family != dst->sa_family)
578 return EINVAL;
579
580 /* validate sa_len */
581 switch (src->sa_family) {
582 #ifdef INET
583 case AF_INET:
584 if (src->sa_len != sizeof(struct sockaddr_in))
585 return EINVAL;
586 break;
587 #endif
588 #ifdef INET6
589 case AF_INET6:
590 if (src->sa_len != sizeof(struct sockaddr_in6))
591 return EINVAL;
592 break;
593 #endif
594 default:
595 return EAFNOSUPPORT;
596 }
597 switch (dst->sa_family) {
598 #ifdef INET
599 case AF_INET:
600 if (dst->sa_len != sizeof(struct sockaddr_in))
601 return EINVAL;
602 break;
603 #endif
604 #ifdef INET6
605 case AF_INET6:
606 if (dst->sa_len != sizeof(struct sockaddr_in6))
607 return EINVAL;
608 break;
609 #endif
610 default:
611 return EAFNOSUPPORT;
612 }
613
614 /* check sa_family looks sane for the cmd */
615 switch (cmd) {
616 case SIOCSIFPHYADDR:
617 if (src->sa_family == AF_INET)
618 break;
619 return EAFNOSUPPORT;
620 #ifdef INET6
621 case SIOCSIFPHYADDR_IN6:
622 if (src->sa_family == AF_INET6)
623 break;
624 return EAFNOSUPPORT;
625 #endif /* INET6 */
626 case SIOCSLIFPHYADDR:
627 /* checks done in the above */
628 break;
629 }
630
631 error = gif_set_tunnel(&sc->gif_if, src, dst);
632 break;
633
634 #ifdef SIOCDIFPHYADDR
635 case SIOCDIFPHYADDR:
636 gif_delete_tunnel(&sc->gif_if);
637 break;
638 #endif
639
640 case SIOCGIFPSRCADDR:
641 #ifdef INET6
642 case SIOCGIFPSRCADDR_IN6:
643 #endif /* INET6 */
644 if (sc->gif_psrc == NULL) {
645 error = EADDRNOTAVAIL;
646 goto bad;
647 }
648 src = sc->gif_psrc;
649 switch (cmd) {
650 #ifdef INET
651 case SIOCGIFPSRCADDR:
652 dst = &ifr->ifr_addr;
653 size = sizeof(ifr->ifr_addr);
654 break;
655 #endif /* INET */
656 #ifdef INET6
657 case SIOCGIFPSRCADDR_IN6:
658 dst = (struct sockaddr *)
659 &(((struct in6_ifreq *)data)->ifr_addr);
660 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
661 break;
662 #endif /* INET6 */
663 default:
664 error = EADDRNOTAVAIL;
665 goto bad;
666 }
667 if (src->sa_len > size)
668 return EINVAL;
669 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
670 break;
671
672 case SIOCGIFPDSTADDR:
673 #ifdef INET6
674 case SIOCGIFPDSTADDR_IN6:
675 #endif /* INET6 */
676 if (sc->gif_pdst == NULL) {
677 error = EADDRNOTAVAIL;
678 goto bad;
679 }
680 src = sc->gif_pdst;
681 switch (cmd) {
682 #ifdef INET
683 case SIOCGIFPDSTADDR:
684 dst = &ifr->ifr_addr;
685 size = sizeof(ifr->ifr_addr);
686 break;
687 #endif /* INET */
688 #ifdef INET6
689 case SIOCGIFPDSTADDR_IN6:
690 dst = (struct sockaddr *)
691 &(((struct in6_ifreq *)data)->ifr_addr);
692 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
693 break;
694 #endif /* INET6 */
695 default:
696 error = EADDRNOTAVAIL;
697 goto bad;
698 }
699 if (src->sa_len > size)
700 return EINVAL;
701 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
702 break;
703
704 case SIOCGLIFPHYADDR:
705 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
706 error = EADDRNOTAVAIL;
707 goto bad;
708 }
709
710 /* copy src */
711 src = sc->gif_psrc;
712 dst = (struct sockaddr *)
713 &(((struct if_laddrreq *)data)->addr);
714 size = sizeof(((struct if_laddrreq *)data)->addr);
715 if (src->sa_len > size)
716 return EINVAL;
717 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
718
719 /* copy dst */
720 src = sc->gif_pdst;
721 dst = (struct sockaddr *)
722 &(((struct if_laddrreq *)data)->dstaddr);
723 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
724 if (src->sa_len > size)
725 return EINVAL;
726 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
727 break;
728
729 case SIOCSIFFLAGS:
730 /* if_ioctl() takes care of it */
731 break;
732
733 default:
734 error = EINVAL;
735 break;
736 }
737 bad:
738 return error;
739 }
740
741 /*
742 * XXXRW: There's a general event-ordering issue here: the code to check
743 * if a given tunnel is already present happens before we perform a
744 * potentially blocking setup of the tunnel. This code needs to be
745 * re-ordered so that the check and replacement can be atomic using
746 * a mutex.
747 */
748 int
749 gif_set_tunnel(ifp, src, dst)
750 struct ifnet *ifp;
751 struct sockaddr *src;
752 struct sockaddr *dst;
753 {
754 struct gif_softc *sc = (struct gif_softc *)ifp;
755 struct gif_softc *sc2;
756 struct sockaddr *osrc, *odst, *sa;
757 int s;
758 int error = 0;
759
760 s = splnet();
761
762 mtx_lock(&gif_mtx);
763 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
764 if (sc2 == sc)
765 continue;
766 if (!sc2->gif_pdst || !sc2->gif_psrc)
767 continue;
768 if (sc2->gif_pdst->sa_family != dst->sa_family ||
769 sc2->gif_pdst->sa_len != dst->sa_len ||
770 sc2->gif_psrc->sa_family != src->sa_family ||
771 sc2->gif_psrc->sa_len != src->sa_len)
772 continue;
773
774 /*
775 * Disallow parallel tunnels unless instructed
776 * otherwise.
777 */
778 if (!parallel_tunnels &&
779 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
780 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
781 error = EADDRNOTAVAIL;
782 mtx_unlock(&gif_mtx);
783 goto bad;
784 }
785
786 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
787 }
788 mtx_unlock(&gif_mtx);
789
790 /* XXX we can detach from both, but be polite just in case */
791 if (sc->gif_psrc)
792 switch (sc->gif_psrc->sa_family) {
793 #ifdef INET
794 case AF_INET:
795 (void)in_gif_detach(sc);
796 break;
797 #endif
798 #ifdef INET6
799 case AF_INET6:
800 (void)in6_gif_detach(sc);
801 break;
802 #endif
803 }
804
805 osrc = sc->gif_psrc;
806 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
807 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
808 sc->gif_psrc = sa;
809
810 odst = sc->gif_pdst;
811 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
812 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
813 sc->gif_pdst = sa;
814
815 switch (sc->gif_psrc->sa_family) {
816 #ifdef INET
817 case AF_INET:
818 error = in_gif_attach(sc);
819 break;
820 #endif
821 #ifdef INET6
822 case AF_INET6:
823 error = in6_gif_attach(sc);
824 break;
825 #endif
826 }
827 if (error) {
828 /* rollback */
829 free((caddr_t)sc->gif_psrc, M_IFADDR);
830 free((caddr_t)sc->gif_pdst, M_IFADDR);
831 sc->gif_psrc = osrc;
832 sc->gif_pdst = odst;
833 goto bad;
834 }
835
836 if (osrc)
837 free((caddr_t)osrc, M_IFADDR);
838 if (odst)
839 free((caddr_t)odst, M_IFADDR);
840
841 if (sc->gif_psrc && sc->gif_pdst)
842 ifp->if_flags |= IFF_RUNNING;
843 else
844 ifp->if_flags &= ~IFF_RUNNING;
845 splx(s);
846
847 return 0;
848
849 bad:
850 if (sc->gif_psrc && sc->gif_pdst)
851 ifp->if_flags |= IFF_RUNNING;
852 else
853 ifp->if_flags &= ~IFF_RUNNING;
854 splx(s);
855
856 return error;
857 }
858
859 void
860 gif_delete_tunnel(ifp)
861 struct ifnet *ifp;
862 {
863 struct gif_softc *sc = (struct gif_softc *)ifp;
864 int s;
865
866 s = splnet();
867
868 if (sc->gif_psrc) {
869 free((caddr_t)sc->gif_psrc, M_IFADDR);
870 sc->gif_psrc = NULL;
871 }
872 if (sc->gif_pdst) {
873 free((caddr_t)sc->gif_pdst, M_IFADDR);
874 sc->gif_pdst = NULL;
875 }
876 /* it is safe to detach from both */
877 #ifdef INET
878 (void)in_gif_detach(sc);
879 #endif
880 #ifdef INET6
881 (void)in6_gif_detach(sc);
882 #endif
883
884 if (sc->gif_psrc && sc->gif_pdst)
885 ifp->if_flags |= IFF_RUNNING;
886 else
887 ifp->if_flags &= ~IFF_RUNNING;
888 splx(s);
889 }
Cache object: baeb136f77a40e2c694d5ee14c72afee
|