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 <machine/cpu.h>
55 #include <net/if_clone.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/scope6_var.h>
78 #include <netinet6/in6_gif.h>
79 #include <netinet6/ip6protosw.h>
82 #include <netinet/ip_encap.h>
83 #include <net/ethernet.h>
84 #include <net/if_bridgevar.h>
85 #include <net/if_gif.h>
87 #include <security/mac/mac_framework.h>
92 * gif_mtx protects the global gif_softc_list.
94 static struct mtx gif_mtx;
95 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
96 static LIST_HEAD(, gif_softc) gif_softc_list;
98 void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
99 void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
100 void (*ng_gif_attach_p)(struct ifnet *ifp);
101 void (*ng_gif_detach_p)(struct ifnet *ifp);
103 static void gif_start(struct ifnet *);
104 static int gif_clone_create(struct if_clone *, int, caddr_t);
105 static void gif_clone_destroy(struct ifnet *);
107 IFC_SIMPLE_DECLARE(gif, 0);
109 static int gifmodevent(module_t, int, void *);
111 SYSCTL_DECL(_net_link);
112 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
113 "Generic Tunnel Interface");
116 * This macro controls the default upper limitation on nesting of gif tunnels.
117 * Since, setting a large value to this macro with a careless configuration
118 * may introduce system crash, we don't allow any nestings by default.
119 * If you need to configure nested gif tunnels, you can define this macro
120 * in your kernel configuration file. However, if you do so, please be
121 * careful to configure the tunnels so that it won't make a loop.
123 #define MAX_GIF_NEST 1
125 static int max_gif_nesting = MAX_GIF_NEST;
126 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
127 &max_gif_nesting, 0, "Max nested tunnels");
130 * By default, we disallow creation of multiple tunnels between the same
131 * pair of addresses. Some applications require this functionality so
132 * we allow control over this check here.
135 static int parallel_tunnels = 1;
137 static int parallel_tunnels = 0;
139 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
140 ¶llel_tunnels, 0, "Allow parallel tunnels?");
142 /* copy from src/sys/net/if_ethersubr.c */
143 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
144 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
145 #ifndef ETHER_IS_BROADCAST
146 #define ETHER_IS_BROADCAST(addr) \
147 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
151 gif_clone_create(ifc, unit, params)
152 struct if_clone *ifc;
156 struct gif_softc *sc;
158 sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
159 sc->gif_fibnum = curthread->td_proc->p_fibnum;
160 GIF2IFP(sc) = if_alloc(IFT_GIF);
161 if (GIF2IFP(sc) == NULL) {
168 GIF2IFP(sc)->if_softc = sc;
169 if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
171 sc->encap_cookie4 = sc->encap_cookie6 = NULL;
173 GIF2IFP(sc)->if_addrlen = 0;
174 GIF2IFP(sc)->if_mtu = GIF_MTU;
175 GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
177 /* turn off ingress filter */
178 GIF2IFP(sc)->if_flags |= IFF_LINK2;
180 GIF2IFP(sc)->if_ioctl = gif_ioctl;
181 GIF2IFP(sc)->if_start = gif_start;
182 GIF2IFP(sc)->if_output = gif_output;
183 GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
184 if_attach(GIF2IFP(sc));
185 bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
186 if (ng_gif_attach_p != NULL)
187 (*ng_gif_attach_p)(GIF2IFP(sc));
190 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
191 mtx_unlock(&gif_mtx);
197 gif_clone_destroy(ifp)
201 struct gif_softc *sc = ifp->if_softc;
204 LIST_REMOVE(sc, gif_list);
205 mtx_unlock(&gif_mtx);
207 gif_delete_tunnel(ifp);
209 if (sc->encap_cookie6 != NULL) {
210 err = encap_detach(sc->encap_cookie6);
211 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
215 if (sc->encap_cookie4 != NULL) {
216 err = encap_detach(sc->encap_cookie4);
217 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
221 if (ng_gif_detach_p != NULL)
222 (*ng_gif_detach_p)(ifp);
227 GIF_LOCK_DESTROY(sc);
233 gifmodevent(mod, type, data)
241 mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
242 LIST_INIT(&gif_softc_list);
243 if_clone_attach(&gif_cloner);
246 ip6_gif_hlim = GIF_HLIM;
251 if_clone_detach(&gif_cloner);
252 mtx_destroy(&gif_mtx);
263 static moduledata_t gif_mod = {
269 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
270 MODULE_VERSION(if_gif, 1);
273 gif_encapcheck(m, off, proto, arg)
274 const struct mbuf *m;
280 struct gif_softc *sc;
282 sc = (struct gif_softc *)arg;
286 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
289 /* no physical address */
290 if (!sc->gif_psrc || !sc->gif_pdst)
302 case IPPROTO_ETHERIP:
309 /* Bail on short packets */
310 if (m->m_pkthdr.len < sizeof(ip))
313 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
318 if (sc->gif_psrc->sa_family != AF_INET ||
319 sc->gif_pdst->sa_family != AF_INET)
321 return gif_encapcheck4(m, off, proto, arg);
325 if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
327 if (sc->gif_psrc->sa_family != AF_INET6 ||
328 sc->gif_pdst->sa_family != AF_INET6)
330 return gif_encapcheck6(m, off, proto, arg);
338 gif_start(struct ifnet *ifp)
340 struct gif_softc *sc;
345 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
347 IFQ_DEQUEUE(&ifp->if_snd, m);
351 gif_output(ifp, m, sc->gif_pdst, NULL);
354 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
360 gif_output(ifp, m, dst, rt)
363 struct sockaddr *dst;
364 struct rtentry *rt; /* added in net2 */
366 struct gif_softc *sc = ifp->if_softc;
373 error = mac_check_ifnet_transmit(ifp, m);
381 * gif may cause infinite recursion calls when misconfigured.
382 * We'll prevent this by detecting loops.
384 * High nesting level may cause stack exhaustion.
385 * We'll prevent this by introducing upper limit.
388 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
389 while (mtag != NULL) {
390 if (*(struct ifnet **)(mtag + 1) == ifp) {
392 "gif_output: loop detected on %s\n",
393 (*(struct ifnet **)(mtag + 1))->if_xname);
395 error = EIO; /* is there better errno? */
398 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
401 if (gif_called > max_gif_nesting) {
403 "gif_output: recursively called too many times(%d)\n",
406 error = EIO; /* is there better errno? */
409 mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
416 *(struct ifnet **)(mtag + 1) = ifp;
417 m_tag_prepend(m, mtag);
419 m->m_flags &= ~(M_BCAST|M_MCAST);
423 if (!(ifp->if_flags & IFF_UP) ||
424 sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
431 /* BPF writes need to be handled specially. */
432 if (dst->sa_family == AF_UNSPEC) {
433 bcopy(dst->sa_data, &af, sizeof(af));
438 BPF_MTAP2(ifp, &af, sizeof(af), m);
440 ifp->if_obytes += m->m_pkthdr.len;
442 /* override to IPPROTO_ETHERIP for bridged traffic */
446 M_SETFIB(m, sc->gif_fibnum);
447 /* inner AF-specific encapsulation */
449 /* XXX should we check if our outer source is legal? */
451 /* dispatch to output logic based on outer AF */
452 switch (sc->gif_psrc->sa_family) {
455 error = in_gif_output(ifp, af, m);
460 error = in6_gif_output(ifp, af, m);
476 gif_input(m, af, ifp)
482 struct etherip_header *eip;
483 struct ether_header *eh;
484 struct ifnet *oldifp;
492 m->m_pkthdr.rcvif = ifp;
495 mac_create_mbuf_from_ifnet(ifp, m);
498 if (bpf_peers_present(ifp->if_bpf)) {
500 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
503 if (ng_gif_input_p != NULL) {
504 (*ng_gif_input_p)(ifp, &m, af);
510 * Put the packet to the network layer input queue according to the
511 * specified address family.
512 * Note: older versions of gif_input directly called network layer
513 * input functions, e.g. ip6_input, here. We changed the policy to
514 * prevent too many recursive calls of such input functions, which
515 * might cause kernel panic. But the change may introduce another
516 * problem; if the input queue is full, packets are discarded.
517 * The kernel stack overflow really happened, and we believed
518 * queue-full rarely occurs, so we changed the policy.
532 n = sizeof(struct etherip_header) + sizeof(struct ether_header);
541 eip = mtod(m, struct etherip_header *);
543 (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) {
544 /* discard unknown versions */
548 m_adj(m, sizeof(struct etherip_header));
550 m->m_flags &= ~(M_BCAST|M_MCAST);
551 m->m_pkthdr.rcvif = ifp;
553 if (ifp->if_bridge) {
555 eh = mtod(m, struct ether_header *);
556 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
557 if (ETHER_IS_BROADCAST(eh->ether_dhost))
558 m->m_flags |= M_BCAST;
560 m->m_flags |= M_MCAST;
563 BRIDGE_INPUT(ifp, m);
565 if (m != NULL && ifp != oldifp) {
567 * The bridge gave us back itself or one of the
568 * members for which the frame is addressed.
579 if (ng_gif_input_orphan_p != NULL)
580 (*ng_gif_input_orphan_p)(ifp, m, af);
587 ifp->if_ibytes += m->m_pkthdr.len;
588 netisr_dispatch(isr, m);
591 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
593 gif_ioctl(ifp, cmd, data)
598 struct gif_softc *sc = ifp->if_softc;
599 struct ifreq *ifr = (struct ifreq*)data;
601 struct sockaddr *dst, *src;
602 #ifdef SIOCSIFMTU /* xxx */
608 ifp->if_flags |= IFF_UP;
618 #ifdef SIOCSIFMTU /* xxx */
624 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
628 #endif /* SIOCSIFMTU */
634 case SIOCSIFPHYADDR_IN6:
636 case SIOCSLIFPHYADDR:
640 src = (struct sockaddr *)
641 &(((struct in_aliasreq *)data)->ifra_addr);
642 dst = (struct sockaddr *)
643 &(((struct in_aliasreq *)data)->ifra_dstaddr);
647 case SIOCSIFPHYADDR_IN6:
648 src = (struct sockaddr *)
649 &(((struct in6_aliasreq *)data)->ifra_addr);
650 dst = (struct sockaddr *)
651 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
654 case SIOCSLIFPHYADDR:
655 src = (struct sockaddr *)
656 &(((struct if_laddrreq *)data)->addr);
657 dst = (struct sockaddr *)
658 &(((struct if_laddrreq *)data)->dstaddr);
664 /* sa_family must be equal */
665 if (src->sa_family != dst->sa_family)
668 /* validate sa_len */
669 switch (src->sa_family) {
672 if (src->sa_len != sizeof(struct sockaddr_in))
678 if (src->sa_len != sizeof(struct sockaddr_in6))
685 switch (dst->sa_family) {
688 if (dst->sa_len != sizeof(struct sockaddr_in))
694 if (dst->sa_len != sizeof(struct sockaddr_in6))
702 /* check sa_family looks sane for the cmd */
705 if (src->sa_family == AF_INET)
709 case SIOCSIFPHYADDR_IN6:
710 if (src->sa_family == AF_INET6)
714 case SIOCSLIFPHYADDR:
715 /* checks done in the above */
719 error = gif_set_tunnel(GIF2IFP(sc), src, dst);
722 #ifdef SIOCDIFPHYADDR
724 gif_delete_tunnel(GIF2IFP(sc));
728 case SIOCGIFPSRCADDR:
730 case SIOCGIFPSRCADDR_IN6:
732 if (sc->gif_psrc == NULL) {
733 error = EADDRNOTAVAIL;
739 case SIOCGIFPSRCADDR:
740 dst = &ifr->ifr_addr;
741 size = sizeof(ifr->ifr_addr);
745 case SIOCGIFPSRCADDR_IN6:
746 dst = (struct sockaddr *)
747 &(((struct in6_ifreq *)data)->ifr_addr);
748 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
752 error = EADDRNOTAVAIL;
755 if (src->sa_len > size)
757 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
759 if (dst->sa_family == AF_INET6) {
760 error = sa6_recoverscope((struct sockaddr_in6 *)dst);
767 case SIOCGIFPDSTADDR:
769 case SIOCGIFPDSTADDR_IN6:
771 if (sc->gif_pdst == NULL) {
772 error = EADDRNOTAVAIL;
778 case SIOCGIFPDSTADDR:
779 dst = &ifr->ifr_addr;
780 size = sizeof(ifr->ifr_addr);
784 case SIOCGIFPDSTADDR_IN6:
785 dst = (struct sockaddr *)
786 &(((struct in6_ifreq *)data)->ifr_addr);
787 size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
791 error = EADDRNOTAVAIL;
794 if (src->sa_len > size)
796 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
798 if (dst->sa_family == AF_INET6) {
799 error = sa6_recoverscope((struct sockaddr_in6 *)dst);
806 case SIOCGLIFPHYADDR:
807 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
808 error = EADDRNOTAVAIL;
814 dst = (struct sockaddr *)
815 &(((struct if_laddrreq *)data)->addr);
816 size = sizeof(((struct if_laddrreq *)data)->addr);
817 if (src->sa_len > size)
819 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
823 dst = (struct sockaddr *)
824 &(((struct if_laddrreq *)data)->dstaddr);
825 size = sizeof(((struct if_laddrreq *)data)->dstaddr);
826 if (src->sa_len > size)
828 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
832 /* if_ioctl() takes care of it */
844 * XXXRW: There's a general event-ordering issue here: the code to check
845 * if a given tunnel is already present happens before we perform a
846 * potentially blocking setup of the tunnel. This code needs to be
847 * re-ordered so that the check and replacement can be atomic using
851 gif_set_tunnel(ifp, src, dst)
853 struct sockaddr *src;
854 struct sockaddr *dst;
856 struct gif_softc *sc = ifp->if_softc;
857 struct gif_softc *sc2;
858 struct sockaddr *osrc, *odst, *sa;
862 LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
865 if (!sc2->gif_pdst || !sc2->gif_psrc)
867 if (sc2->gif_pdst->sa_family != dst->sa_family ||
868 sc2->gif_pdst->sa_len != dst->sa_len ||
869 sc2->gif_psrc->sa_family != src->sa_family ||
870 sc2->gif_psrc->sa_len != src->sa_len)
874 * Disallow parallel tunnels unless instructed
877 if (!parallel_tunnels &&
878 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
879 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
880 error = EADDRNOTAVAIL;
881 mtx_unlock(&gif_mtx);
885 /* XXX both end must be valid? (I mean, not 0.0.0.0) */
887 mtx_unlock(&gif_mtx);
889 /* XXX we can detach from both, but be polite just in case */
891 switch (sc->gif_psrc->sa_family) {
894 (void)in_gif_detach(sc);
899 (void)in6_gif_detach(sc);
905 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
906 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
910 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
911 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
914 switch (sc->gif_psrc->sa_family) {
917 error = in_gif_attach(sc);
923 * Check validity of the scope zone ID of the addresses, and
924 * convert it into the kernel internal form if necessary.
926 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
929 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
932 error = in6_gif_attach(sc);
938 free((caddr_t)sc->gif_psrc, M_IFADDR);
939 free((caddr_t)sc->gif_pdst, M_IFADDR);
946 free((caddr_t)osrc, M_IFADDR);
948 free((caddr_t)odst, M_IFADDR);
951 if (sc->gif_psrc && sc->gif_pdst)
952 ifp->if_drv_flags |= IFF_DRV_RUNNING;
954 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
960 gif_delete_tunnel(ifp)
963 struct gif_softc *sc = ifp->if_softc;
966 free((caddr_t)sc->gif_psrc, M_IFADDR);
970 free((caddr_t)sc->gif_pdst, M_IFADDR);
973 /* it is safe to detach from both */
975 (void)in_gif_detach(sc);
978 (void)in6_gif_detach(sc);
980 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;