1 /* $FreeBSD: src/sys/net/if_gif.c,v 1.4.2.15 2002/11/08 16:57:13 ume Exp $ */
2 /* $DragonFly: src/sys/net/gif/if_gif.c,v 1.11 2005/01/26 00:37:39 joerg Exp $ */
3 /* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/errno.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
48 #include <sys/protosw.h>
50 #include <machine/bus.h> /* XXX: Shouldn't really be required! */
51 #include <machine/cpu.h>
54 #include <net/if_types.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
63 #include <netinet/in_var.h>
64 #include <netinet/in_gif.h>
65 #include <netinet/ip_var.h>
70 #include <netinet/in.h>
72 #include <netinet6/in6_var.h>
73 #include <netinet/ip6.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/in6_gif.h>
76 #include <netinet6/ip6protosw.h>
79 #include <netinet/ip_encap.h>
82 #include <net/net_osdep.h>
86 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
87 LIST_HEAD(, gif_softc) gif_softc_list;
89 int gif_clone_create (struct if_clone *, int);
90 void gif_clone_destroy (struct ifnet *);
92 struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif", gif_clone_create,
93 gif_clone_destroy, 0, IF_MAXUNIT);
95 static int gifmodevent (module_t, int, void *);
97 SYSCTL_DECL(_net_link);
98 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
99 "Generic Tunnel Interface");
102 * This macro controls the default upper limitation on nesting of gif tunnels.
103 * Since, setting a large value to this macro with a careless configuration
104 * may introduce system crash, we don't allow any nestings by default.
105 * If you need to configure nested gif tunnels, you can define this macro
106 * in your kernel configuration file. However, if you do so, please be
107 * careful to configure the tunnels so that it won't make a loop.
109 #define MAX_GIF_NEST 1
111 static int max_gif_nesting = MAX_GIF_NEST;
112 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
113 &max_gif_nesting, 0, "Max nested tunnels");
116 * By default, we disallow creation of multiple tunnels between the same
117 * pair of addresses. Some applications require this functionality so
118 * we allow control over this check here.
121 static int parallel_tunnels = 1;
123 static int parallel_tunnels = 0;
125 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
126 ¶llel_tunnels, 0, "Allow parallel tunnels?");
129 gif_clone_create(ifc, unit)
130 struct if_clone *ifc;
133 struct gif_softc *sc;
135 sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK);
136 bzero(sc, sizeof(struct gif_softc));
138 sc->gif_if.if_softc = sc;
139 if_initname(&(sc->gif_if), GIFNAME, unit);
143 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
149 struct gif_softc *sc;
152 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
154 sc->gif_if.if_addrlen = 0;
155 sc->gif_if.if_mtu = GIF_MTU;
156 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
158 /* turn off ingress filter */
159 sc->gif_if.if_flags |= IFF_LINK2;
161 sc->gif_if.if_ioctl = gif_ioctl;
162 sc->gif_if.if_output = gif_output;
163 sc->gif_if.if_type = IFT_GIF;
164 sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
165 if_attach(&sc->gif_if);
166 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
170 gif_clone_destroy(ifp)
174 struct gif_softc *sc = ifp->if_softc;
176 gif_delete_tunnel(&sc->gif_if);
177 LIST_REMOVE(sc, gif_list);
179 if (sc->encap_cookie6 != NULL) {
180 err = encap_detach(sc->encap_cookie6);
181 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
185 if (sc->encap_cookie4 != NULL) {
186 err = encap_detach(sc->encap_cookie4);
187 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
198 gifmodevent(mod, type, data)
206 LIST_INIT(&gif_softc_list);
207 if_clone_attach(&gif_cloner);
210 ip6_gif_hlim = GIF_HLIM;
215 if_clone_detach(&gif_cloner);
217 while (!LIST_EMPTY(&gif_softc_list))
218 gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
228 static moduledata_t gif_mod = {
234 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
237 gif_encapcheck(m, off, proto, arg)
238 const struct mbuf *m;
244 struct gif_softc *sc;
246 sc = (struct gif_softc *)arg;
250 if ((sc->gif_if.if_flags & IFF_UP) == 0)
253 /* no physical address */
254 if (!sc->gif_psrc || !sc->gif_pdst)
270 /* Bail on short packets */
271 if (m->m_pkthdr.len < sizeof(ip))
274 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
279 if (sc->gif_psrc->sa_family != AF_INET ||
280 sc->gif_pdst->sa_family != AF_INET)
282 return gif_encapcheck4(m, off, proto, arg);
286 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
288 if (sc->gif_psrc->sa_family != AF_INET6 ||
289 sc->gif_pdst->sa_family != AF_INET6)
291 return gif_encapcheck6(m, off, proto, arg);
299 gif_output(ifp, m, dst, rt)
302 struct sockaddr *dst;
303 struct rtentry *rt; /* added in net2 */
305 struct gif_softc *sc = (struct gif_softc*)ifp;
307 static int called = 0; /* XXX: MUTEX */
310 * gif may cause infinite recursion calls when misconfigured.
311 * We'll prevent this by introducing upper limit.
312 * XXX: this mechanism may introduce another problem about
313 * mutual exclusion of the variable CALLED, especially if we
316 if (++called > max_gif_nesting) {
318 "gif_output: recursively called too many times(%d)\n",
321 error = EIO; /* is there better errno? */
325 m->m_flags &= ~(M_BCAST|M_MCAST);
326 if (!(ifp->if_flags & IFF_UP) ||
327 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
335 * We need to prepend the address family as
338 uint32_t af = dst->sa_family;
340 bpf_ptap(ifp->if_bpf, m, &af, sizeof(4));
343 ifp->if_obytes += m->m_pkthdr.len;
345 /* inner AF-specific encapsulation */
347 /* XXX should we check if our outer source is legal? */
349 /* dispatch to output logic based on outer AF */
350 switch (sc->gif_psrc->sa_family) {
353 error = in_gif_output(ifp, dst->sa_family, m);
358 error = in6_gif_output(ifp, dst->sa_family, m);
368 called = 0; /* reset recursion counter */
375 gif_input(m, af, ifp)
388 m->m_pkthdr.rcvif = ifp;
392 * We need to prepend the address family as
397 bpf_ptap(ifp->if_bpf, m, &af1, sizeof(af1));
401 * Put the packet to the network layer input queue according to the
402 * specified address family.
403 * Note: older versions of gif_input directly called network layer
404 * input functions, e.g. ip6_input, here. We changed the policy to
405 * prevent too many recursive calls of such input functions, which
406 * might cause kernel panic. But the change may introduce another
407 * problem; if the input queue is full, packets are discarded.
408 * The kernel stack overflow really happened, and we believed
409 * queue-full rarely occurs, so we changed the policy.
428 ifp->if_ibytes += m->m_pkthdr.len;
429 netisr_dispatch(isr, m);
434 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
436 gif_ioctl(ifp, cmd, data, cr)
442 struct gif_softc *sc = (struct gif_softc*)ifp;
443 struct ifreq *ifr = (struct ifreq*)data;
445 struct sockaddr *dst, *src;
446 #ifdef SIOCSIFMTU /* xxx */
452 ifp->if_flags |= IFF_UP;
462 #ifdef SIOCSIFMTU /* xxx */
468 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
472 #endif /* SIOCSIFMTU */
478 case SIOCSIFPHYADDR_IN6:
480 case SIOCSLIFPHYADDR:
484 src = (struct sockaddr *)
485 &(((struct in_aliasreq *)data)->ifra_addr);
486 dst = (struct sockaddr *)
487 &(((struct in_aliasreq *)data)->ifra_dstaddr);
491 case SIOCSIFPHYADDR_IN6:
492 src = (struct sockaddr *)
493 &(((struct in6_aliasreq *)data)->ifra_addr);
494 dst = (struct sockaddr *)
495 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
498 case SIOCSLIFPHYADDR:
499 src = (struct sockaddr *)
500 &(((struct if_laddrreq *)data)->addr);
501 dst = (struct sockaddr *)
502 &(((struct if_laddrreq *)data)->dstaddr);
508 /* sa_family must be equal */
509 if (src->sa_family != dst->sa_family)
512 /* validate sa_len */
513 switch (src->sa_family) {
516 if (src->sa_len != sizeof(struct sockaddr_in))
522 if (src->sa_len != sizeof(struct sockaddr_in6))
529 switch (dst->sa_family) {
532 if (dst->sa_len != sizeof(struct sockaddr_in))
538 if (dst->sa_len != sizeof(struct sockaddr_in6))
546 /* check sa_family looks sane for the cmd */
549 if (src->sa_family == AF_INET)
553 case SIOCSIFPHYADDR_IN6:
554 if (src->sa_family == AF_INET6)
558 case SIOCSLIFPHYADDR:
559 /* checks done in the above */
563 error = gif_set_tunnel(&sc->gif_if, src, dst);
566 #ifdef SIOCDIFPHYADDR
568 gif_delete_tunnel(&sc->gif_if);
572 case SIOCGIFPSRCADDR:
574 case SIOCGIFPSRCADDR_IN6:
576 if (sc->gif_psrc == NULL) {
577 error = EADDRNOTAVAIL;
583 case SIOCGIFPSRCADDR:
584 dst = &ifr->ifr_addr;
585 size = sizeof(ifr->ifr_addr);
589 case SIOCGIFPSRCADDR_IN6:
590 dst = (struct sockaddr *)
591 &(((struct in6_ifreq *)data)->ifr_addr);
592 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
596 error = EADDRNOTAVAIL;
599 if (src->sa_len > size)
601 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
604 case SIOCGIFPDSTADDR:
606 case SIOCGIFPDSTADDR_IN6:
608 if (sc->gif_pdst == NULL) {
609 error = EADDRNOTAVAIL;
615 case SIOCGIFPDSTADDR:
616 dst = &ifr->ifr_addr;
617 size = sizeof(ifr->ifr_addr);
621 case SIOCGIFPDSTADDR_IN6:
622 dst = (struct sockaddr *)
623 &(((struct in6_ifreq *)data)->ifr_addr);
624 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
628 error = EADDRNOTAVAIL;
631 if (src->sa_len > size)
633 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
636 case SIOCGLIFPHYADDR:
637 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
638 error = EADDRNOTAVAIL;
644 dst = (struct sockaddr *)
645 &(((struct if_laddrreq *)data)->addr);
646 size = sizeof(((struct if_laddrreq *)data)->addr);
647 if (src->sa_len > size)
649 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
653 dst = (struct sockaddr *)
654 &(((struct if_laddrreq *)data)->dstaddr);
655 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
656 if (src->sa_len > size)
658 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
662 /* if_ioctl() takes care of it */
674 gif_set_tunnel(ifp, src, dst)
676 struct sockaddr *src;
677 struct sockaddr *dst;
679 struct gif_softc *sc = (struct gif_softc *)ifp;
680 struct gif_softc *sc2;
681 struct sockaddr *osrc, *odst, *sa;
687 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
690 if (!sc2->gif_pdst || !sc2->gif_psrc)
692 if (sc2->gif_pdst->sa_family != dst->sa_family ||
693 sc2->gif_pdst->sa_len != dst->sa_len ||
694 sc2->gif_psrc->sa_family != src->sa_family ||
695 sc2->gif_psrc->sa_len != src->sa_len)
699 * Disallow parallel tunnels unless instructed
702 if (!parallel_tunnels &&
703 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
704 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
705 error = EADDRNOTAVAIL;
709 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
712 /* XXX we can detach from both, but be polite just in case */
714 switch (sc->gif_psrc->sa_family) {
717 (void)in_gif_detach(sc);
722 (void)in6_gif_detach(sc);
728 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
729 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
733 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
734 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
737 switch (sc->gif_psrc->sa_family) {
740 error = in_gif_attach(sc);
745 error = in6_gif_attach(sc);
751 free((caddr_t)sc->gif_psrc, M_IFADDR);
752 free((caddr_t)sc->gif_pdst, M_IFADDR);
759 free((caddr_t)osrc, M_IFADDR);
761 free((caddr_t)odst, M_IFADDR);
763 if (sc->gif_psrc && sc->gif_pdst)
764 ifp->if_flags |= IFF_RUNNING;
766 ifp->if_flags &= ~IFF_RUNNING;
772 if (sc->gif_psrc && sc->gif_pdst)
773 ifp->if_flags |= IFF_RUNNING;
775 ifp->if_flags &= ~IFF_RUNNING;
782 gif_delete_tunnel(ifp)
785 struct gif_softc *sc = (struct gif_softc *)ifp;
791 free((caddr_t)sc->gif_psrc, M_IFADDR);
795 free((caddr_t)sc->gif_pdst, M_IFADDR);
798 /* it is safe to detach from both */
800 (void)in_gif_detach(sc);
803 (void)in6_gif_detach(sc);
806 if (sc->gif_psrc && sc->gif_pdst)
807 ifp->if_flags |= IFF_RUNNING;
809 ifp->if_flags &= ~IFF_RUNNING;