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/netisr.h>
58 #include <net/route.h>
60 #include <net/if_clone.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
66 #include <netinet/in_var.h>
67 #include <netinet/in_gif.h>
68 #include <netinet/ip_var.h>
73 #include <netinet/in.h>
75 #include <netinet6/in6_var.h>
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/in6_gif.h>
79 #include <netinet6/ip6protosw.h>
82 #include <netinet/ip_encap.h>
85 #include <net/net_osdep.h>
89 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
90 LIST_HEAD(, gif_softc) gif_softc_list;
92 int gif_clone_create (struct if_clone *, int, caddr_t);
93 void gif_clone_destroy (struct ifnet *);
95 struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif", gif_clone_create,
96 gif_clone_destroy, 0, IF_MAXUNIT);
98 static int gifmodevent (module_t, int, void *);
99 static void gif_clear_cache(struct gif_softc *sc);
101 SYSCTL_DECL(_net_link);
102 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
103 "Generic Tunnel Interface");
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.
113 #define MAX_GIF_NEST 1
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");
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.
125 static int parallel_tunnels = 1;
127 static int parallel_tunnels = 0;
129 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
130 ¶llel_tunnels, 0, "Allow parallel tunnels?");
133 gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
135 struct gif_softc *sc;
137 sc = kmalloc (sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
139 sc->gif_if.if_softc = sc;
140 if_initname(&(sc->gif_if), GIFNAME, unit);
144 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
149 gifattach0(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, NULL);
166 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
170 gif_clone_destroy(struct ifnet *ifp)
172 struct gif_softc *sc = ifp->if_softc;
175 gif_delete_tunnel(&sc->gif_if);
176 LIST_REMOVE(sc, gif_list);
178 if (sc->encap_cookie6 != NULL) {
179 err = encap_detach(sc->encap_cookie6);
180 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
184 if (sc->encap_cookie4 != NULL) {
185 err = encap_detach(sc->encap_cookie4);
186 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
198 gif_clear_cache(struct gif_softc *sc)
202 for (n = 0; n < ncpus; ++n) {
203 if (sc->gif_ro[n].ro_rt) {
204 RTFREE(sc->gif_ro[n].ro_rt);
205 sc->gif_ro[n].ro_rt = NULL;
208 if (sc->gif_ro6[n].ro_rt) {
209 RTFREE(sc->gif_ro6[n].ro_rt);
210 sc->gif_ro6[n].ro_rt = NULL;
217 gifmodevent(module_t mod, int type, void *data)
222 LIST_INIT(&gif_softc_list);
223 if_clone_attach(&gif_cloner);
226 ip6_gif_hlim = GIF_HLIM;
231 if_clone_detach(&gif_cloner);
233 while (!LIST_EMPTY(&gif_softc_list))
234 gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
244 static moduledata_t gif_mod = {
250 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
253 gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
256 struct gif_softc *sc;
258 sc = (struct gif_softc *)arg;
262 if ((sc->gif_if.if_flags & IFF_UP) == 0)
265 /* no physical address */
266 if (!sc->gif_psrc || !sc->gif_pdst)
282 /* Bail on short packets */
283 if (m->m_pkthdr.len < sizeof(ip))
286 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
291 if (sc->gif_psrc->sa_family != AF_INET ||
292 sc->gif_pdst->sa_family != AF_INET)
294 return gif_encapcheck4(m, off, proto, arg);
298 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
300 if (sc->gif_psrc->sa_family != AF_INET6 ||
301 sc->gif_pdst->sa_family != AF_INET6)
303 return gif_encapcheck6(m, off, proto, arg);
315 gif_output_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
318 struct gif_softc *sc = (struct gif_softc*)ifp;
320 static int called = 0; /* XXX: MUTEX */
323 * gif may cause infinite recursion calls when misconfigured.
324 * We'll prevent this by introducing upper limit.
325 * XXX: this mechanism may introduce another problem about
326 * mutual exclusion of the variable CALLED, especially if we
329 if (++called > max_gif_nesting) {
331 "gif_output: recursively called too many times(%d)\n",
334 error = EIO; /* is there better errno? */
338 m->m_flags &= ~(M_BCAST|M_MCAST);
339 if (!(ifp->if_flags & IFF_UP) ||
340 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
348 * We need to prepend the address family as
351 uint32_t af = dst->sa_family;
353 bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
356 ifp->if_obytes += m->m_pkthdr.len;
358 /* inner AF-specific encapsulation */
360 /* XXX should we check if our outer source is legal? */
362 /* dispatch to output logic based on outer AF */
363 switch (sc->gif_psrc->sa_family) {
366 error = in_gif_output(ifp, dst->sa_family, m);
371 error = in6_gif_output(ifp, dst->sa_family, m);
381 called = 0; /* reset recursion counter */
388 gif_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
393 ifnet_serialize_tx(ifp);
394 error = gif_output_serialized(ifp, m, dst, rt);
395 ifnet_deserialize_tx(ifp);
400 gif_input(struct mbuf *m, int af, struct ifnet *ifp)
410 m->m_pkthdr.rcvif = ifp;
414 * We need to prepend the address family as
419 bpf_ptap(ifp->if_bpf, m, &af1, sizeof(af1));
423 * Put the packet to the network layer input queue according to the
424 * specified address family.
425 * Note: older versions of gif_input directly called network layer
426 * input functions, e.g. ip6_input, here. We changed the policy to
427 * prevent too many recursive calls of such input functions, which
428 * might cause kernel panic. But the change may introduce another
429 * problem; if the input queue is full, packets are discarded.
430 * The kernel stack overflow really happened, and we believed
431 * queue-full rarely occurs, so we changed the policy.
450 ifp->if_ibytes += m->m_pkthdr.len;
451 m->m_flags &= ~M_HASH;
452 netisr_queue(isr, m);
457 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
459 gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
461 struct gif_softc *sc = (struct gif_softc*)ifp;
462 struct ifreq *ifr = (struct ifreq*)data;
464 struct sockaddr *dst, *src;
465 #ifdef SIOCSIFMTU /* xxx */
471 ifp->if_flags |= IFF_UP;
481 #ifdef SIOCSIFMTU /* xxx */
487 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
491 #endif /* SIOCSIFMTU */
497 case SIOCSIFPHYADDR_IN6:
499 case SIOCSLIFPHYADDR:
503 src = (struct sockaddr *)
504 &(((struct in_aliasreq *)data)->ifra_addr);
505 dst = (struct sockaddr *)
506 &(((struct in_aliasreq *)data)->ifra_dstaddr);
510 case SIOCSIFPHYADDR_IN6:
511 src = (struct sockaddr *)
512 &(((struct in6_aliasreq *)data)->ifra_addr);
513 dst = (struct sockaddr *)
514 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
517 case SIOCSLIFPHYADDR:
518 src = (struct sockaddr *)
519 &(((struct if_laddrreq *)data)->addr);
520 dst = (struct sockaddr *)
521 &(((struct if_laddrreq *)data)->dstaddr);
527 /* sa_family must be equal */
528 if (src->sa_family != dst->sa_family)
531 /* validate sa_len */
532 switch (src->sa_family) {
535 if (src->sa_len != sizeof(struct sockaddr_in))
541 if (src->sa_len != sizeof(struct sockaddr_in6))
548 switch (dst->sa_family) {
551 if (dst->sa_len != sizeof(struct sockaddr_in))
557 if (dst->sa_len != sizeof(struct sockaddr_in6))
565 /* check sa_family looks sane for the cmd */
568 if (src->sa_family == AF_INET)
572 case SIOCSIFPHYADDR_IN6:
573 if (src->sa_family == AF_INET6)
577 case SIOCSLIFPHYADDR:
578 /* checks done in the above */
582 error = gif_set_tunnel(&sc->gif_if, src, dst);
585 #ifdef SIOCDIFPHYADDR
587 gif_delete_tunnel(&sc->gif_if);
591 case SIOCGIFPSRCADDR:
593 case SIOCGIFPSRCADDR_IN6:
595 if (sc->gif_psrc == NULL) {
596 error = EADDRNOTAVAIL;
602 case SIOCGIFPSRCADDR:
603 dst = &ifr->ifr_addr;
604 size = sizeof(ifr->ifr_addr);
608 case SIOCGIFPSRCADDR_IN6:
609 dst = (struct sockaddr *)
610 &(((struct in6_ifreq *)data)->ifr_addr);
611 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
615 error = EADDRNOTAVAIL;
618 if (src->sa_len > size)
620 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
623 case SIOCGIFPDSTADDR:
625 case SIOCGIFPDSTADDR_IN6:
627 if (sc->gif_pdst == NULL) {
628 error = EADDRNOTAVAIL;
634 case SIOCGIFPDSTADDR:
635 dst = &ifr->ifr_addr;
636 size = sizeof(ifr->ifr_addr);
640 case SIOCGIFPDSTADDR_IN6:
641 dst = (struct sockaddr *)
642 &(((struct in6_ifreq *)data)->ifr_addr);
643 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
647 error = EADDRNOTAVAIL;
650 if (src->sa_len > size)
652 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
655 case SIOCGLIFPHYADDR:
656 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
657 error = EADDRNOTAVAIL;
663 dst = (struct sockaddr *)
664 &(((struct if_laddrreq *)data)->addr);
665 size = sizeof(((struct if_laddrreq *)data)->addr);
666 if (src->sa_len > size)
668 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
672 dst = (struct sockaddr *)
673 &(((struct if_laddrreq *)data)->dstaddr);
674 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
675 if (src->sa_len > size)
677 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
681 /* if_ioctl() takes care of it */
693 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
695 struct gif_softc *sc = (struct gif_softc *)ifp;
696 struct gif_softc *sc2;
697 struct sockaddr *osrc, *odst, *sa;
702 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
705 if (!sc2->gif_pdst || !sc2->gif_psrc)
707 if (sc2->gif_pdst->sa_family != dst->sa_family ||
708 sc2->gif_pdst->sa_len != dst->sa_len ||
709 sc2->gif_psrc->sa_family != src->sa_family ||
710 sc2->gif_psrc->sa_len != src->sa_len)
714 * Disallow parallel tunnels unless instructed
717 if (!parallel_tunnels &&
718 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
719 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
720 error = EADDRNOTAVAIL;
724 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
727 /* XXX we can detach from both, but be polite just in case */
729 switch (sc->gif_psrc->sa_family) {
745 sa = (struct sockaddr *)kmalloc(src->sa_len, M_IFADDR, M_WAITOK);
746 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
750 sa = (struct sockaddr *)kmalloc(dst->sa_len, M_IFADDR, M_WAITOK);
751 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
754 switch (sc->gif_psrc->sa_family) {
757 error = in_gif_attach(sc);
762 error = in6_gif_attach(sc);
768 kfree((caddr_t)sc->gif_psrc, M_IFADDR);
769 kfree((caddr_t)sc->gif_pdst, M_IFADDR);
776 kfree((caddr_t)osrc, M_IFADDR);
778 kfree((caddr_t)odst, M_IFADDR);
780 if (sc->gif_psrc && sc->gif_pdst)
781 ifp->if_flags |= IFF_RUNNING;
783 ifp->if_flags &= ~IFF_RUNNING;
789 if (sc->gif_psrc && sc->gif_pdst)
790 ifp->if_flags |= IFF_RUNNING;
792 ifp->if_flags &= ~IFF_RUNNING;
799 gif_delete_tunnel(struct ifnet *ifp)
801 struct gif_softc *sc = (struct gif_softc *)ifp;
806 kfree((caddr_t)sc->gif_psrc, M_IFADDR);
810 kfree((caddr_t)sc->gif_pdst, M_IFADDR);
813 /* it is safe to detach from both */
822 if (sc->gif_psrc && sc->gif_pdst)
823 ifp->if_flags |= IFF_RUNNING;
825 ifp->if_flags &= ~IFF_RUNNING;