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.21 2008/05/14 11:59:23 sephe 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/ifq_var.h>
58 #include <net/netisr.h>
59 #include <net/route.h>
61 #include <net/if_clone.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_gif.h>
69 #include <netinet/ip_var.h>
74 #include <netinet/in.h>
76 #include <netinet6/in6_var.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
79 #include <netinet6/in6_gif.h>
80 #include <netinet6/ip6protosw.h>
83 #include <netinet/ip_encap.h>
86 #include <net/net_osdep.h>
90 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
91 LIST_HEAD(, gif_softc) gif_softc_list;
93 int gif_clone_create (struct if_clone *, int, caddr_t);
94 int gif_clone_destroy (struct ifnet *);
96 struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif", gif_clone_create,
97 gif_clone_destroy, 0, IF_MAXUNIT);
99 static int gifmodevent (module_t, int, void *);
100 static void gif_clear_cache(struct gif_softc *sc);
102 SYSCTL_DECL(_net_link);
103 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
104 "Generic Tunnel Interface");
107 * This macro controls the default upper limitation on nesting of gif tunnels.
108 * Since, setting a large value to this macro with a careless configuration
109 * may introduce system crash, we don't allow any nestings by default.
110 * If you need to configure nested gif tunnels, you can define this macro
111 * in your kernel configuration file. However, if you do so, please be
112 * careful to configure the tunnels so that it won't make a loop.
114 #define MAX_GIF_NEST 1
116 static int max_gif_nesting = MAX_GIF_NEST;
117 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
118 &max_gif_nesting, 0, "Max nested tunnels");
121 * By default, we disallow creation of multiple tunnels between the same
122 * pair of addresses. Some applications require this functionality so
123 * we allow control over this check here.
126 static int parallel_tunnels = 1;
128 static int parallel_tunnels = 0;
130 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
131 ¶llel_tunnels, 0, "Allow parallel tunnels?");
134 gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
136 struct gif_softc *sc;
138 sc = kmalloc (sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
140 sc->gif_if.if_softc = sc;
141 if_initname(&(sc->gif_if), GIFNAME, unit);
145 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
150 gifattach0(struct gif_softc *sc)
153 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
155 sc->gif_if.if_addrlen = 0;
156 sc->gif_if.if_mtu = GIF_MTU;
157 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
159 /* turn off ingress filter */
160 sc->gif_if.if_flags |= IFF_LINK2;
162 sc->gif_if.if_ioctl = gif_ioctl;
163 sc->gif_if.if_output = gif_output;
164 sc->gif_if.if_type = IFT_GIF;
165 ifq_set_maxlen(&sc->gif_if.if_snd, IFQ_MAXLEN);
166 if_attach(&sc->gif_if, NULL);
167 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
171 gif_clone_destroy(struct ifnet *ifp)
173 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"));
201 gif_clear_cache(struct gif_softc *sc)
207 for (n = 0; n < ncpus; ++n) {
208 rt = sc->gif_ro[n].ro_rt;
210 * Routes need to be cleaned up in their CPU so migrate
211 * to it and return to the original CPU after completion.
214 if (rt && rt->rt_cpuid != mycpuid)
215 lwkt_migratecpu(rt->rt_cpuid);
219 if (sc->gif_ro[n].ro_rt) {
220 RTFREE(sc->gif_ro[n].ro_rt);
221 sc->gif_ro[n].ro_rt = NULL;
224 if (sc->gif_ro6[n].ro_rt) {
225 RTFREE(sc->gif_ro6[n].ro_rt);
226 sc->gif_ro6[n].ro_rt = NULL;
230 lwkt_migratecpu(origcpu);
235 gifmodevent(module_t mod, int type, void *data)
240 LIST_INIT(&gif_softc_list);
241 if_clone_attach(&gif_cloner);
244 ip6_gif_hlim = GIF_HLIM;
249 if_clone_detach(&gif_cloner);
251 while (!LIST_EMPTY(&gif_softc_list))
252 gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
262 static moduledata_t gif_mod = {
268 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
271 gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
274 struct gif_softc *sc;
276 sc = (struct gif_softc *)arg;
280 if ((sc->gif_if.if_flags & IFF_UP) == 0)
283 /* no physical address */
284 if (!sc->gif_psrc || !sc->gif_pdst)
300 /* Bail on short packets */
301 if (m->m_pkthdr.len < sizeof(ip))
304 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
309 if (sc->gif_psrc->sa_family != AF_INET ||
310 sc->gif_pdst->sa_family != AF_INET)
312 return gif_encapcheck4(m, off, proto, arg);
316 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
318 if (sc->gif_psrc->sa_family != AF_INET6 ||
319 sc->gif_pdst->sa_family != AF_INET6)
321 return gif_encapcheck6(m, off, proto, arg);
333 gif_output_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
336 struct gif_softc *sc = (struct gif_softc*)ifp;
338 static int called = 0; /* XXX: MUTEX */
341 * gif may cause infinite recursion calls when misconfigured.
342 * We'll prevent this by introducing upper limit.
343 * XXX: this mechanism may introduce another problem about
344 * mutual exclusion of the variable CALLED, especially if we
347 if (++called > max_gif_nesting) {
349 "gif_output: recursively called too many times(%d)\n",
352 error = EIO; /* is there better errno? */
356 m->m_flags &= ~(M_BCAST|M_MCAST);
357 if (!(ifp->if_flags & IFF_UP) ||
358 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
368 * We need to prepend the address family as
371 uint32_t af = dst->sa_family;
373 bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
377 IFNET_STAT_INC(ifp, opackets, 1);
378 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
380 /* inner AF-specific encapsulation */
382 /* XXX should we check if our outer source is legal? */
384 /* dispatch to output logic based on outer AF */
385 switch (sc->gif_psrc->sa_family) {
388 error = in_gif_output(ifp, dst->sa_family, m);
393 error = in6_gif_output(ifp, dst->sa_family, m);
403 called = 0; /* reset recursion counter */
405 IFNET_STAT_INC(ifp, oerrors, 1);
410 gif_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
413 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
416 ifsq_serialize_hw(ifsq);
417 error = gif_output_serialized(ifp, m, dst, rt);
418 ifsq_deserialize_hw(ifsq);
423 gif_input(struct mbuf *m, int af, struct ifnet *ifp)
433 m->m_pkthdr.rcvif = ifp;
439 * We need to prepend the address family as
444 bpf_ptap(ifp->if_bpf, m, &af1, sizeof(af1));
450 * Put the packet to the network layer input queue according to the
451 * specified address family.
452 * Note: older versions of gif_input directly called network layer
453 * input functions, e.g. ip6_input, here. We changed the policy to
454 * prevent too many recursive calls of such input functions, which
455 * might cause kernel panic. But the change may introduce another
456 * problem; if the input queue is full, packets are discarded.
457 * The kernel stack overflow really happened, and we believed
458 * queue-full rarely occurs, so we changed the policy.
476 IFNET_STAT_INC(ifp, ipackets, 1);
477 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
478 m->m_flags &= ~M_HASH;
479 netisr_queue(isr, m);
484 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
486 gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
488 struct gif_softc *sc = (struct gif_softc*)ifp;
489 struct ifreq *ifr = (struct ifreq*)data;
491 struct sockaddr *dst, *src;
492 #ifdef SIOCSIFMTU /* xxx */
498 ifp->if_flags |= IFF_UP;
508 #ifdef SIOCSIFMTU /* xxx */
514 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
518 #endif /* SIOCSIFMTU */
524 case SIOCSIFPHYADDR_IN6:
526 case SIOCSLIFPHYADDR:
530 src = (struct sockaddr *)
531 &(((struct in_aliasreq *)data)->ifra_addr);
532 dst = (struct sockaddr *)
533 &(((struct in_aliasreq *)data)->ifra_dstaddr);
537 case SIOCSIFPHYADDR_IN6:
538 src = (struct sockaddr *)
539 &(((struct in6_aliasreq *)data)->ifra_addr);
540 dst = (struct sockaddr *)
541 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
544 case SIOCSLIFPHYADDR:
545 src = (struct sockaddr *)
546 &(((struct if_laddrreq *)data)->addr);
547 dst = (struct sockaddr *)
548 &(((struct if_laddrreq *)data)->dstaddr);
554 /* sa_family must be equal */
555 if (src->sa_family != dst->sa_family)
558 /* validate sa_len */
559 switch (src->sa_family) {
562 if (src->sa_len != sizeof(struct sockaddr_in))
568 if (src->sa_len != sizeof(struct sockaddr_in6))
575 switch (dst->sa_family) {
578 if (dst->sa_len != sizeof(struct sockaddr_in))
584 if (dst->sa_len != sizeof(struct sockaddr_in6))
592 /* check sa_family looks sane for the cmd */
595 if (src->sa_family == AF_INET)
599 case SIOCSIFPHYADDR_IN6:
600 if (src->sa_family == AF_INET6)
604 case SIOCSLIFPHYADDR:
605 /* checks done in the above */
609 error = gif_set_tunnel(&sc->gif_if, src, dst);
612 #ifdef SIOCDIFPHYADDR
614 gif_delete_tunnel(&sc->gif_if);
618 case SIOCGIFPSRCADDR:
620 case SIOCGIFPSRCADDR_IN6:
622 if (sc->gif_psrc == NULL) {
623 error = EADDRNOTAVAIL;
629 case SIOCGIFPSRCADDR:
630 dst = &ifr->ifr_addr;
631 size = sizeof(ifr->ifr_addr);
635 case SIOCGIFPSRCADDR_IN6:
636 dst = (struct sockaddr *)
637 &(((struct in6_ifreq *)data)->ifr_addr);
638 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
642 error = EADDRNOTAVAIL;
645 if (src->sa_len > size)
647 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
650 case SIOCGIFPDSTADDR:
652 case SIOCGIFPDSTADDR_IN6:
654 if (sc->gif_pdst == NULL) {
655 error = EADDRNOTAVAIL;
661 case SIOCGIFPDSTADDR:
662 dst = &ifr->ifr_addr;
663 size = sizeof(ifr->ifr_addr);
667 case SIOCGIFPDSTADDR_IN6:
668 dst = (struct sockaddr *)
669 &(((struct in6_ifreq *)data)->ifr_addr);
670 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
674 error = EADDRNOTAVAIL;
677 if (src->sa_len > size)
679 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
682 case SIOCGLIFPHYADDR:
683 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
684 error = EADDRNOTAVAIL;
690 dst = (struct sockaddr *)
691 &(((struct if_laddrreq *)data)->addr);
692 size = sizeof(((struct if_laddrreq *)data)->addr);
693 if (src->sa_len > size)
695 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
699 dst = (struct sockaddr *)
700 &(((struct if_laddrreq *)data)->dstaddr);
701 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
702 if (src->sa_len > size)
704 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
708 /* if_ioctl() takes care of it */
720 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
722 struct gif_softc *sc = (struct gif_softc *)ifp;
723 struct gif_softc *sc2;
724 struct sockaddr *osrc, *odst, *sa;
729 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
732 if (!sc2->gif_pdst || !sc2->gif_psrc)
734 if (sc2->gif_pdst->sa_family != dst->sa_family ||
735 sc2->gif_pdst->sa_len != dst->sa_len ||
736 sc2->gif_psrc->sa_family != src->sa_family ||
737 sc2->gif_psrc->sa_len != src->sa_len)
741 * Disallow parallel tunnels unless instructed
744 if (!parallel_tunnels &&
745 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
746 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
747 error = EADDRNOTAVAIL;
751 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
754 /* XXX we can detach from both, but be polite just in case */
756 switch (sc->gif_psrc->sa_family) {
772 sa = (struct sockaddr *)kmalloc(src->sa_len, M_IFADDR, M_WAITOK);
773 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
777 sa = (struct sockaddr *)kmalloc(dst->sa_len, M_IFADDR, M_WAITOK);
778 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
781 switch (sc->gif_psrc->sa_family) {
784 error = in_gif_attach(sc);
789 error = in6_gif_attach(sc);
795 kfree((caddr_t)sc->gif_psrc, M_IFADDR);
796 kfree((caddr_t)sc->gif_pdst, M_IFADDR);
803 kfree((caddr_t)osrc, M_IFADDR);
805 kfree((caddr_t)odst, M_IFADDR);
807 if (sc->gif_psrc && sc->gif_pdst)
808 ifp->if_flags |= IFF_RUNNING;
810 ifp->if_flags &= ~IFF_RUNNING;
816 if (sc->gif_psrc && sc->gif_pdst)
817 ifp->if_flags |= IFF_RUNNING;
819 ifp->if_flags &= ~IFF_RUNNING;
826 gif_delete_tunnel(struct ifnet *ifp)
828 struct gif_softc *sc = (struct gif_softc *)ifp;
833 kfree((caddr_t)sc->gif_psrc, M_IFADDR);
837 kfree((caddr_t)sc->gif_pdst, M_IFADDR);
840 /* it is safe to detach from both */
849 if (sc->gif_psrc && sc->gif_pdst)
850 ifp->if_flags |= IFF_RUNNING;
852 ifp->if_flags &= ~IFF_RUNNING;