2 /* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 #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/module.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>
50 #include <sys/protosw.h>
52 #include <sys/vimage.h>
53 #include <machine/cpu.h>
56 #include <net/if_clone.h>
57 #include <net/if_types.h>
58 #include <net/netisr.h>
59 #include <net/route.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/scope6_var.h>
79 #include <netinet6/in6_gif.h>
80 #include <netinet6/ip6protosw.h>
83 #include <netinet/ip_encap.h>
84 #include <net/ethernet.h>
85 #include <net/if_bridgevar.h>
86 #include <net/if_gif.h>
88 #include <security/mac/mac_framework.h>
93 * gif_mtx protects the global gif_softc_list.
95 static struct mtx gif_mtx;
96 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
97 static LIST_HEAD(, gif_softc) gif_softc_list;
99 void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
100 void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
101 void (*ng_gif_attach_p)(struct ifnet *ifp);
102 void (*ng_gif_detach_p)(struct ifnet *ifp);
104 static void gif_start(struct ifnet *);
105 static int gif_clone_create(struct if_clone *, int, caddr_t);
106 static void gif_clone_destroy(struct ifnet *);
108 IFC_SIMPLE_DECLARE(gif, 0);
110 static int gifmodevent(module_t, int, void *);
112 SYSCTL_DECL(_net_link);
113 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
114 "Generic Tunnel Interface");
117 * This macro controls the default upper limitation on nesting of gif tunnels.
118 * Since, setting a large value to this macro with a careless configuration
119 * may introduce system crash, we don't allow any nestings by default.
120 * If you need to configure nested gif tunnels, you can define this macro
121 * in your kernel configuration file. However, if you do so, please be
122 * careful to configure the tunnels so that it won't make a loop.
124 #define MAX_GIF_NEST 1
126 static int max_gif_nesting = MAX_GIF_NEST;
127 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
128 &max_gif_nesting, 0, "Max nested tunnels");
131 * By default, we disallow creation of multiple tunnels between the same
132 * pair of addresses. Some applications require this functionality so
133 * we allow control over this check here.
136 static int parallel_tunnels = 1;
138 static int parallel_tunnels = 0;
140 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
141 ¶llel_tunnels, 0, "Allow parallel tunnels?");
143 /* copy from src/sys/net/if_ethersubr.c */
144 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
145 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
146 #ifndef ETHER_IS_BROADCAST
147 #define ETHER_IS_BROADCAST(addr) \
148 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
152 gif_clone_create(ifc, unit, params)
153 struct if_clone *ifc;
157 struct gif_softc *sc;
159 sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
160 sc->gif_fibnum = curthread->td_proc->p_fibnum;
161 GIF2IFP(sc) = if_alloc(IFT_GIF);
162 if (GIF2IFP(sc) == NULL) {
169 GIF2IFP(sc)->if_softc = sc;
170 if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
172 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
174 GIF2IFP(sc)->if_addrlen = 0;
175 GIF2IFP(sc)->if_mtu = GIF_MTU;
176 GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
178 /* turn off ingress filter */
179 GIF2IFP(sc)->if_flags |= IFF_LINK2;
181 GIF2IFP(sc)->if_ioctl = gif_ioctl;
182 GIF2IFP(sc)->if_start = gif_start;
183 GIF2IFP(sc)->if_output = gif_output;
184 GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
185 if_attach(GIF2IFP(sc));
186 bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
187 if (ng_gif_attach_p != NULL)
188 (*ng_gif_attach_p)(GIF2IFP(sc));
191 LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
192 mtx_unlock(&gif_mtx);
198 gif_clone_destroy(ifp)
202 struct gif_softc *sc = ifp->if_softc;
205 LIST_REMOVE(sc, gif_list);
206 mtx_unlock(&gif_mtx);
208 gif_delete_tunnel(ifp);
210 if (sc->encap_cookie6 != NULL) {
211 err = encap_detach(sc->encap_cookie6);
212 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
216 if (sc->encap_cookie4 != NULL) {
217 err = encap_detach(sc->encap_cookie4);
218 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
222 if (ng_gif_detach_p != NULL)
223 (*ng_gif_detach_p)(ifp);
228 GIF_LOCK_DESTROY(sc);
234 gifmodevent(mod, type, data)
242 mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
243 LIST_INIT(&V_gif_softc_list);
244 if_clone_attach(&gif_cloner);
247 V_ip6_gif_hlim = GIF_HLIM;
252 if_clone_detach(&gif_cloner);
253 mtx_destroy(&gif_mtx);
264 static moduledata_t gif_mod = {
270 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
271 MODULE_VERSION(if_gif, 1);
274 gif_encapcheck(m, off, proto, arg)
275 const struct mbuf *m;
281 struct gif_softc *sc;
283 sc = (struct gif_softc *)arg;
287 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
290 /* no physical address */
291 if (!sc->gif_psrc || !sc->gif_pdst)
303 case IPPROTO_ETHERIP:
310 /* Bail on short packets */
311 if (m->m_pkthdr.len < sizeof(ip))
314 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
319 if (sc->gif_psrc->sa_family != AF_INET ||
320 sc->gif_pdst->sa_family != AF_INET)
322 return gif_encapcheck4(m, off, proto, arg);
326 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
328 if (sc->gif_psrc->sa_family != AF_INET6 ||
329 sc->gif_pdst->sa_family != AF_INET6)
331 return gif_encapcheck6(m, off, proto, arg);
339 gif_start(struct ifnet *ifp)
341 struct gif_softc *sc;
346 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
348 IFQ_DEQUEUE(&ifp->if_snd, m);
352 gif_output(ifp, m, sc->gif_pdst, NULL);
355 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
361 gif_output(ifp, m, dst, rt)
364 struct sockaddr *dst;
365 struct rtentry *rt; /* added in net2 */
367 struct gif_softc *sc = ifp->if_softc;
374 error = mac_ifnet_check_transmit(ifp, m);
382 * gif may cause infinite recursion calls when misconfigured.
383 * We'll prevent this by detecting loops.
385 * High nesting level may cause stack exhaustion.
386 * We'll prevent this by introducing upper limit.
389 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
390 while (mtag != NULL) {
391 if (*(struct ifnet **)(mtag + 1) == ifp) {
393 "gif_output: loop detected on %s\n",
394 (*(struct ifnet **)(mtag + 1))->if_xname);
396 error = EIO; /* is there better errno? */
399 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
402 if (gif_called > V_max_gif_nesting) {
404 "gif_output: recursively called too many times(%d)\n",
407 error = EIO; /* is there better errno? */
410 mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
417 *(struct ifnet **)(mtag + 1) = ifp;
418 m_tag_prepend(m, mtag);
420 m->m_flags &= ~(M_BCAST|M_MCAST);
424 if (!(ifp->if_flags & IFF_UP) ||
425 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
432 /* BPF writes need to be handled specially. */
433 if (dst->sa_family == AF_UNSPEC) {
434 bcopy(dst->sa_data, &af, sizeof(af));
439 BPF_MTAP2(ifp, &af, sizeof(af), m);
441 ifp->if_obytes += m->m_pkthdr.len;
443 /* override to IPPROTO_ETHERIP for bridged traffic */
447 M_SETFIB(m, sc->gif_fibnum);
448 /* inner AF-specific encapsulation */
450 /* XXX should we check if our outer source is legal? */
452 /* dispatch to output logic based on outer AF */
453 switch (sc->gif_psrc->sa_family) {
456 error = in_gif_output(ifp, af, m);
461 error = in6_gif_output(ifp, af, m);
477 gif_input(m, af, ifp)
483 struct etherip_header *eip;
484 struct ether_header *eh;
485 struct ifnet *oldifp;
493 m->m_pkthdr.rcvif = ifp;
496 mac_ifnet_create_mbuf(ifp, m);
499 if (bpf_peers_present(ifp->if_bpf)) {
501 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
504 if (ng_gif_input_p != NULL) {
505 (*ng_gif_input_p)(ifp, &m, af);
511 * Put the packet to the network layer input queue according to the
512 * specified address family.
513 * Note: older versions of gif_input directly called network layer
514 * input functions, e.g. ip6_input, here. We changed the policy to
515 * prevent too many recursive calls of such input functions, which
516 * might cause kernel panic. But the change may introduce another
517 * problem; if the input queue is full, packets are discarded.
518 * The kernel stack overflow really happened, and we believed
519 * queue-full rarely occurs, so we changed the policy.
533 n = sizeof(struct etherip_header) + sizeof(struct ether_header);
542 eip = mtod(m, struct etherip_header *);
544 (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) {
545 /* discard unknown versions */
549 m_adj(m, sizeof(struct etherip_header));
551 m->m_flags &= ~(M_BCAST|M_MCAST);
552 m->m_pkthdr.rcvif = ifp;
554 if (ifp->if_bridge) {
556 eh = mtod(m, struct ether_header *);
557 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
558 if (ETHER_IS_BROADCAST(eh->ether_dhost))
559 m->m_flags |= M_BCAST;
561 m->m_flags |= M_MCAST;
564 BRIDGE_INPUT(ifp, m);
566 if (m != NULL && ifp != oldifp) {
568 * The bridge gave us back itself or one of the
569 * members for which the frame is addressed.
580 if (ng_gif_input_orphan_p != NULL)
581 (*ng_gif_input_orphan_p)(ifp, m, af);
588 ifp->if_ibytes += m->m_pkthdr.len;
589 netisr_dispatch(isr, m);
592 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
594 gif_ioctl(ifp, cmd, data)
599 struct gif_softc *sc = ifp->if_softc;
600 struct ifreq *ifr = (struct ifreq*)data;
602 struct sockaddr *dst, *src;
603 #ifdef SIOCSIFMTU /* xxx */
609 ifp->if_flags |= IFF_UP;
619 #ifdef SIOCSIFMTU /* xxx */
625 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
629 #endif /* SIOCSIFMTU */
635 case SIOCSIFPHYADDR_IN6:
637 case SIOCSLIFPHYADDR:
641 src = (struct sockaddr *)
642 &(((struct in_aliasreq *)data)->ifra_addr);
643 dst = (struct sockaddr *)
644 &(((struct in_aliasreq *)data)->ifra_dstaddr);
648 case SIOCSIFPHYADDR_IN6:
649 src = (struct sockaddr *)
650 &(((struct in6_aliasreq *)data)->ifra_addr);
651 dst = (struct sockaddr *)
652 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
655 case SIOCSLIFPHYADDR:
656 src = (struct sockaddr *)
657 &(((struct if_laddrreq *)data)->addr);
658 dst = (struct sockaddr *)
659 &(((struct if_laddrreq *)data)->dstaddr);
665 /* sa_family must be equal */
666 if (src->sa_family != dst->sa_family)
669 /* validate sa_len */
670 switch (src->sa_family) {
673 if (src->sa_len != sizeof(struct sockaddr_in))
679 if (src->sa_len != sizeof(struct sockaddr_in6))
686 switch (dst->sa_family) {
689 if (dst->sa_len != sizeof(struct sockaddr_in))
695 if (dst->sa_len != sizeof(struct sockaddr_in6))
703 /* check sa_family looks sane for the cmd */
706 if (src->sa_family == AF_INET)
710 case SIOCSIFPHYADDR_IN6:
711 if (src->sa_family == AF_INET6)
715 case SIOCSLIFPHYADDR:
716 /* checks done in the above */
720 error = gif_set_tunnel(GIF2IFP(sc), src, dst);
723 #ifdef SIOCDIFPHYADDR
725 gif_delete_tunnel(GIF2IFP(sc));
729 case SIOCGIFPSRCADDR:
731 case SIOCGIFPSRCADDR_IN6:
733 if (sc->gif_psrc == NULL) {
734 error = EADDRNOTAVAIL;
740 case SIOCGIFPSRCADDR:
741 dst = &ifr->ifr_addr;
742 size = sizeof(ifr->ifr_addr);
746 case SIOCGIFPSRCADDR_IN6:
747 dst = (struct sockaddr *)
748 &(((struct in6_ifreq *)data)->ifr_addr);
749 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
753 error = EADDRNOTAVAIL;
756 if (src->sa_len > size)
758 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
760 if (dst->sa_family == AF_INET6) {
761 error = sa6_recoverscope((struct sockaddr_in6 *)dst);
768 case SIOCGIFPDSTADDR:
770 case SIOCGIFPDSTADDR_IN6:
772 if (sc->gif_pdst == NULL) {
773 error = EADDRNOTAVAIL;
779 case SIOCGIFPDSTADDR:
780 dst = &ifr->ifr_addr;
781 size = sizeof(ifr->ifr_addr);
785 case SIOCGIFPDSTADDR_IN6:
786 dst = (struct sockaddr *)
787 &(((struct in6_ifreq *)data)->ifr_addr);
788 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
792 error = EADDRNOTAVAIL;
795 if (src->sa_len > size)
797 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
799 if (dst->sa_family == AF_INET6) {
800 error = sa6_recoverscope((struct sockaddr_in6 *)dst);
807 case SIOCGLIFPHYADDR:
808 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
809 error = EADDRNOTAVAIL;
815 dst = (struct sockaddr *)
816 &(((struct if_laddrreq *)data)->addr);
817 size = sizeof(((struct if_laddrreq *)data)->addr);
818 if (src->sa_len > size)
820 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
824 dst = (struct sockaddr *)
825 &(((struct if_laddrreq *)data)->dstaddr);
826 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
827 if (src->sa_len > size)
829 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
833 /* if_ioctl() takes care of it */
845 * XXXRW: There's a general event-ordering issue here: the code to check
846 * if a given tunnel is already present happens before we perform a
847 * potentially blocking setup of the tunnel. This code needs to be
848 * re-ordered so that the check and replacement can be atomic using
852 gif_set_tunnel(ifp, src, dst)
854 struct sockaddr *src;
855 struct sockaddr *dst;
857 struct gif_softc *sc = ifp->if_softc;
858 struct gif_softc *sc2;
859 struct sockaddr *osrc, *odst, *sa;
863 LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
866 if (!sc2->gif_pdst || !sc2->gif_psrc)
868 if (sc2->gif_pdst->sa_family != dst->sa_family ||
869 sc2->gif_pdst->sa_len != dst->sa_len ||
870 sc2->gif_psrc->sa_family != src->sa_family ||
871 sc2->gif_psrc->sa_len != src->sa_len)
875 * Disallow parallel tunnels unless instructed
878 if (!V_parallel_tunnels &&
879 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
880 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
881 error = EADDRNOTAVAIL;
882 mtx_unlock(&gif_mtx);
886 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
888 mtx_unlock(&gif_mtx);
890 /* XXX we can detach from both, but be polite just in case */
892 switch (sc->gif_psrc->sa_family) {
895 (void)in_gif_detach(sc);
900 (void)in6_gif_detach(sc);
906 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
907 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
911 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
912 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
915 switch (sc->gif_psrc->sa_family) {
918 error = in_gif_attach(sc);
924 * Check validity of the scope zone ID of the addresses, and
925 * convert it into the kernel internal form if necessary.
927 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
930 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
933 error = in6_gif_attach(sc);
939 free((caddr_t)sc->gif_psrc, M_IFADDR);
940 free((caddr_t)sc->gif_pdst, M_IFADDR);
947 free((caddr_t)osrc, M_IFADDR);
949 free((caddr_t)odst, M_IFADDR);
952 if (sc->gif_psrc && sc->gif_pdst)
953 ifp->if_drv_flags |= IFF_DRV_RUNNING;
955 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
961 gif_delete_tunnel(ifp)
964 struct gif_softc *sc = ifp->if_softc;
967 free((caddr_t)sc->gif_psrc, M_IFADDR);
971 free((caddr_t)sc->gif_pdst, M_IFADDR);
974 /* it is safe to detach from both */
976 (void)in_gif_detach(sc);
979 (void)in6_gif_detach(sc);
981 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;