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