2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/sys/net/if_gif.c,v 1.4.2.15 2002/11/08 16:57:13 ume Exp $
30 * $DragonFly: src/sys/net/gif/if_gif.c,v 1.19 2008/01/05 14:02:38 swildner Exp $
31 * $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $
35 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/errno.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/protosw.h>
51 #include <sys/thread2.h>
53 #include <machine/cpu.h>
56 #include <net/if_types.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
65 #include <netinet/in_var.h>
66 #include <netinet/in_gif.h>
67 #include <netinet/ip_var.h>
72 #include <netinet/in.h>
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>
81 #include <netinet/ip_encap.h>
84 #include <net/net_osdep.h>
88 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
89 LIST_HEAD(, gif_softc) gif_softc_list;
91 int gif_clone_create (struct if_clone *, int);
92 void gif_clone_destroy (struct ifnet *);
94 struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif", gif_clone_create,
95 gif_clone_destroy, 0, IF_MAXUNIT);
97 static int gifmodevent (module_t, int, void *);
99 SYSCTL_DECL(_net_link);
100 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
101 "Generic Tunnel Interface");
104 * This macro controls the default upper limitation on nesting of gif tunnels.
105 * Since, setting a large value to this macro with a careless configuration
106 * may introduce system crash, we don't allow any nestings by default.
107 * If you need to configure nested gif tunnels, you can define this macro
108 * in your kernel configuration file. However, if you do so, please be
109 * careful to configure the tunnels so that it won't make a loop.
111 #define MAX_GIF_NEST 1
113 static int max_gif_nesting = MAX_GIF_NEST;
114 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
115 &max_gif_nesting, 0, "Max nested tunnels");
118 * By default, we disallow creation of multiple tunnels between the same
119 * pair of addresses. Some applications require this functionality so
120 * we allow control over this check here.
123 static int parallel_tunnels = 1;
125 static int parallel_tunnels = 0;
127 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
128 ¶llel_tunnels, 0, "Allow parallel tunnels?");
131 gif_clone_create(struct if_clone *ifc, int unit)
133 struct gif_softc *sc;
135 sc = kmalloc (sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
137 sc->gif_if.if_softc = sc;
138 if_initname(&(sc->gif_if), GIFNAME, unit);
142 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
147 gifattach0(struct gif_softc *sc)
150 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
152 sc->gif_if.if_addrlen = 0;
153 sc->gif_if.if_mtu = GIF_MTU;
154 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
156 /* turn off ingress filter */
157 sc->gif_if.if_flags |= IFF_LINK2;
159 sc->gif_if.if_ioctl = gif_ioctl;
160 sc->gif_if.if_output = gif_output;
161 sc->gif_if.if_type = IFT_GIF;
162 sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
163 if_attach(&sc->gif_if, NULL);
164 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
168 gif_clone_destroy(struct ifnet *ifp)
171 struct gif_softc *sc = ifp->if_softc;
173 gif_delete_tunnel(&sc->gif_if);
174 LIST_REMOVE(sc, gif_list);
176 if (sc->encap_cookie6 != NULL) {
177 err = encap_detach(sc->encap_cookie6);
178 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
182 if (sc->encap_cookie4 != NULL) {
183 err = encap_detach(sc->encap_cookie4);
184 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
195 gifmodevent(module_t mod, int type, void *data)
200 LIST_INIT(&gif_softc_list);
201 if_clone_attach(&gif_cloner);
204 ip6_gif_hlim = GIF_HLIM;
209 if_clone_detach(&gif_cloner);
211 while (!LIST_EMPTY(&gif_softc_list))
212 gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
222 static moduledata_t gif_mod = {
228 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
231 gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
234 struct gif_softc *sc;
236 sc = (struct gif_softc *)arg;
240 if ((sc->gif_if.if_flags & IFF_UP) == 0)
243 /* no physical address */
244 if (!sc->gif_psrc || !sc->gif_pdst)
260 /* Bail on short packets */
261 if (m->m_pkthdr.len < sizeof(ip))
264 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
269 if (sc->gif_psrc->sa_family != AF_INET ||
270 sc->gif_pdst->sa_family != AF_INET)
272 return gif_encapcheck4(m, off, proto, arg);
276 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
278 if (sc->gif_psrc->sa_family != AF_INET6 ||
279 sc->gif_pdst->sa_family != AF_INET6)
281 return gif_encapcheck6(m, off, proto, arg);
293 gif_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
296 struct gif_softc *sc = (struct gif_softc*)ifp;
298 static int called = 0; /* XXX: MUTEX */
301 * gif may cause infinite recursion calls when misconfigured.
302 * We'll prevent this by introducing upper limit.
303 * XXX: this mechanism may introduce another problem about
304 * mutual exclusion of the variable CALLED, especially if we
307 if (++called > max_gif_nesting) {
309 "gif_output: recursively called too many times(%d)\n",
312 error = EIO; /* is there better errno? */
316 m->m_flags &= ~(M_BCAST|M_MCAST);
317 if (!(ifp->if_flags & IFF_UP) ||
318 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
326 * We need to prepend the address family as
329 uint32_t af = dst->sa_family;
331 bpf_ptap(ifp->if_bpf, m, &af, sizeof(4));
334 ifp->if_obytes += m->m_pkthdr.len;
336 /* inner AF-specific encapsulation */
338 /* XXX should we check if our outer source is legal? */
340 /* dispatch to output logic based on outer AF */
341 switch (sc->gif_psrc->sa_family) {
344 error = in_gif_output(ifp, dst->sa_family, m);
349 error = in6_gif_output(ifp, dst->sa_family, m);
359 called = 0; /* reset recursion counter */
366 gif_input(struct mbuf *m, int af, struct ifnet *ifp)
376 m->m_pkthdr.rcvif = ifp;
380 * We need to prepend the address family as
385 bpf_ptap(ifp->if_bpf, m, &af1, sizeof(af1));
389 * Put the packet to the network layer input queue according to the
390 * specified address family.
391 * Note: older versions of gif_input directly called network layer
392 * input functions, e.g. ip6_input, here. We changed the policy to
393 * prevent too many recursive calls of such input functions, which
394 * might cause kernel panic. But the change may introduce another
395 * problem; if the input queue is full, packets are discarded.
396 * The kernel stack overflow really happened, and we believed
397 * queue-full rarely occurs, so we changed the policy.
416 ifp->if_ibytes += m->m_pkthdr.len;
417 netisr_dispatch(isr, m);
422 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
424 gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
426 struct gif_softc *sc = (struct gif_softc*)ifp;
427 struct ifreq *ifr = (struct ifreq*)data;
429 struct sockaddr *dst, *src;
430 #ifdef SIOCSIFMTU /* xxx */
436 ifp->if_flags |= IFF_UP;
446 #ifdef SIOCSIFMTU /* xxx */
452 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
456 #endif /* SIOCSIFMTU */
462 case SIOCSIFPHYADDR_IN6:
464 case SIOCSLIFPHYADDR:
468 src = (struct sockaddr *)
469 &(((struct in_aliasreq *)data)->ifra_addr);
470 dst = (struct sockaddr *)
471 &(((struct in_aliasreq *)data)->ifra_dstaddr);
475 case SIOCSIFPHYADDR_IN6:
476 src = (struct sockaddr *)
477 &(((struct in6_aliasreq *)data)->ifra_addr);
478 dst = (struct sockaddr *)
479 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
482 case SIOCSLIFPHYADDR:
483 src = (struct sockaddr *)
484 &(((struct if_laddrreq *)data)->addr);
485 dst = (struct sockaddr *)
486 &(((struct if_laddrreq *)data)->dstaddr);
492 /* sa_family must be equal */
493 if (src->sa_family != dst->sa_family)
496 /* validate sa_len */
497 switch (src->sa_family) {
500 if (src->sa_len != sizeof(struct sockaddr_in))
506 if (src->sa_len != sizeof(struct sockaddr_in6))
513 switch (dst->sa_family) {
516 if (dst->sa_len != sizeof(struct sockaddr_in))
522 if (dst->sa_len != sizeof(struct sockaddr_in6))
530 /* check sa_family looks sane for the cmd */
533 if (src->sa_family == AF_INET)
537 case SIOCSIFPHYADDR_IN6:
538 if (src->sa_family == AF_INET6)
542 case SIOCSLIFPHYADDR:
543 /* checks done in the above */
547 error = gif_set_tunnel(&sc->gif_if, src, dst);
550 #ifdef SIOCDIFPHYADDR
552 gif_delete_tunnel(&sc->gif_if);
556 case SIOCGIFPSRCADDR:
558 case SIOCGIFPSRCADDR_IN6:
560 if (sc->gif_psrc == NULL) {
561 error = EADDRNOTAVAIL;
567 case SIOCGIFPSRCADDR:
568 dst = &ifr->ifr_addr;
569 size = sizeof(ifr->ifr_addr);
573 case SIOCGIFPSRCADDR_IN6:
574 dst = (struct sockaddr *)
575 &(((struct in6_ifreq *)data)->ifr_addr);
576 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
580 error = EADDRNOTAVAIL;
583 if (src->sa_len > size)
585 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
588 case SIOCGIFPDSTADDR:
590 case SIOCGIFPDSTADDR_IN6:
592 if (sc->gif_pdst == NULL) {
593 error = EADDRNOTAVAIL;
599 case SIOCGIFPDSTADDR:
600 dst = &ifr->ifr_addr;
601 size = sizeof(ifr->ifr_addr);
605 case SIOCGIFPDSTADDR_IN6:
606 dst = (struct sockaddr *)
607 &(((struct in6_ifreq *)data)->ifr_addr);
608 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
612 error = EADDRNOTAVAIL;
615 if (src->sa_len > size)
617 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
620 case SIOCGLIFPHYADDR:
621 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
622 error = EADDRNOTAVAIL;
628 dst = (struct sockaddr *)
629 &(((struct if_laddrreq *)data)->addr);
630 size = sizeof(((struct if_laddrreq *)data)->addr);
631 if (src->sa_len > size)
633 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
637 dst = (struct sockaddr *)
638 &(((struct if_laddrreq *)data)->dstaddr);
639 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
640 if (src->sa_len > size)
642 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
646 /* if_ioctl() takes care of it */
658 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
660 struct gif_softc *sc = (struct gif_softc *)ifp;
661 struct gif_softc *sc2;
662 struct sockaddr *osrc, *odst, *sa;
667 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
670 if (!sc2->gif_pdst || !sc2->gif_psrc)
672 if (sc2->gif_pdst->sa_family != dst->sa_family ||
673 sc2->gif_pdst->sa_len != dst->sa_len ||
674 sc2->gif_psrc->sa_family != src->sa_family ||
675 sc2->gif_psrc->sa_len != src->sa_len)
679 * Disallow parallel tunnels unless instructed
682 if (!parallel_tunnels &&
683 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
684 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
685 error = EADDRNOTAVAIL;
689 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
692 /* XXX we can detach from both, but be polite just in case */
694 switch (sc->gif_psrc->sa_family) {
708 sa = (struct sockaddr *)kmalloc(src->sa_len, M_IFADDR, M_WAITOK);
709 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
713 sa = (struct sockaddr *)kmalloc(dst->sa_len, M_IFADDR, M_WAITOK);
714 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
717 switch (sc->gif_psrc->sa_family) {
720 error = in_gif_attach(sc);
725 error = in6_gif_attach(sc);
731 kfree((caddr_t)sc->gif_psrc, M_IFADDR);
732 kfree((caddr_t)sc->gif_pdst, M_IFADDR);
739 kfree((caddr_t)osrc, M_IFADDR);
741 kfree((caddr_t)odst, M_IFADDR);
743 if (sc->gif_psrc && sc->gif_pdst)
744 ifp->if_flags |= IFF_RUNNING;
746 ifp->if_flags &= ~IFF_RUNNING;
752 if (sc->gif_psrc && sc->gif_pdst)
753 ifp->if_flags |= IFF_RUNNING;
755 ifp->if_flags &= ~IFF_RUNNING;
762 gif_delete_tunnel(struct ifnet *ifp)
764 struct gif_softc *sc = (struct gif_softc *)ifp;
769 kfree((caddr_t)sc->gif_psrc, M_IFADDR);
773 kfree((caddr_t)sc->gif_pdst, M_IFADDR);
776 /* it is safe to detach from both */
784 if (sc->gif_psrc && sc->gif_pdst)
785 ifp->if_flags |= IFF_RUNNING;
787 ifp->if_flags &= ~IFF_RUNNING;