2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
3 * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org>
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Heiko W.Rupp <hwr@pilhuhn.de>
9 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 * $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 #include "opt_inet6.h"
41 #include <sys/param.h>
43 #include <sys/kernel.h>
45 #include <sys/libkern.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
51 #include <sys/protosw.h>
52 #include <sys/rmlock.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/syslog.h>
58 #include <sys/systm.h>
60 #include <net/ethernet.h>
62 #include <net/if_clone.h>
63 #include <net/if_var.h>
64 #include <net/if_types.h>
65 #include <net/netisr.h>
68 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/in_var.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_var.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/in6_var.h>
79 #include <netinet6/ip6_var.h>
80 #include <netinet6/scope6_var.h>
83 #include <netinet/ip_encap.h>
85 #include <net/if_gre.h>
87 #include <machine/in_cksum.h>
89 #include <security/mac/mac_framework.h>
91 static const char grename[] = "gre";
92 static MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation");
93 static VNET_DEFINE(struct mtx, gre_mtx);
94 #define V_gre_mtx VNET(gre_mtx)
95 #define GRE_LIST_LOCK_INIT(x) mtx_init(&V_gre_mtx, "gre_mtx", NULL, \
97 #define GRE_LIST_LOCK_DESTROY(x) mtx_destroy(&V_gre_mtx)
98 #define GRE_LIST_LOCK(x) mtx_lock(&V_gre_mtx)
99 #define GRE_LIST_UNLOCK(x) mtx_unlock(&V_gre_mtx)
101 static VNET_DEFINE(LIST_HEAD(, gre_softc), gre_softc_list);
102 #define V_gre_softc_list VNET(gre_softc_list)
103 static struct sx gre_ioctl_sx;
104 SX_SYSINIT(gre_ioctl_sx, &gre_ioctl_sx, "gre_ioctl");
106 static int gre_clone_create(struct if_clone *, int, caddr_t);
107 static void gre_clone_destroy(struct ifnet *);
108 static VNET_DEFINE(struct if_clone *, gre_cloner);
109 #define V_gre_cloner VNET(gre_cloner)
111 static void gre_qflush(struct ifnet *);
112 static int gre_transmit(struct ifnet *, struct mbuf *);
113 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
114 static int gre_output(struct ifnet *, struct mbuf *,
115 const struct sockaddr *, struct route *);
117 static void gre_updatehdr(struct gre_softc *);
118 static int gre_set_tunnel(struct ifnet *, struct sockaddr *,
120 static void gre_delete_tunnel(struct ifnet *);
122 int gre_input(struct mbuf **, int *, int);
124 extern int in_gre_attach(struct gre_softc *);
125 extern int in_gre_output(struct mbuf *, int, int);
128 extern int in6_gre_attach(struct gre_softc *);
129 extern int in6_gre_output(struct mbuf *, int, int);
132 SYSCTL_DECL(_net_link);
133 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
134 "Generic Routing Encapsulation");
137 * This macro controls the default upper limitation on nesting of gre tunnels.
138 * Since, setting a large value to this macro with a careless configuration
139 * may introduce system crash, we don't allow any nestings by default.
140 * If you need to configure nested gre tunnels, you can define this macro
141 * in your kernel configuration file. However, if you do so, please be
142 * careful to configure the tunnels so that it won't make a loop.
144 #define MAX_GRE_NEST 1
147 static VNET_DEFINE(int, max_gre_nesting) = MAX_GRE_NEST;
148 #define V_max_gre_nesting VNET(max_gre_nesting)
149 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
150 &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels");
153 vnet_gre_init(const void *unused __unused)
155 LIST_INIT(&V_gre_softc_list);
156 GRE_LIST_LOCK_INIT();
157 V_gre_cloner = if_clone_simple(grename, gre_clone_create,
158 gre_clone_destroy, 0);
160 VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
161 vnet_gre_init, NULL);
164 vnet_gre_uninit(const void *unused __unused)
167 if_clone_detach(V_gre_cloner);
168 GRE_LIST_LOCK_DESTROY();
170 VNET_SYSUNINIT(vnet_gre_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
171 vnet_gre_uninit, NULL);
174 gre_clone_create(struct if_clone *ifc, int unit, caddr_t params)
176 struct gre_softc *sc;
178 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
179 sc->gre_fibnum = curthread->td_proc->p_fibnum;
180 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
182 GRE2IFP(sc)->if_softc = sc;
183 if_initname(GRE2IFP(sc), grename, unit);
185 GRE2IFP(sc)->if_mtu = sc->gre_mtu = GREMTU;
186 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
187 GRE2IFP(sc)->if_output = gre_output;
188 GRE2IFP(sc)->if_ioctl = gre_ioctl;
189 GRE2IFP(sc)->if_transmit = gre_transmit;
190 GRE2IFP(sc)->if_qflush = gre_qflush;
191 if_attach(GRE2IFP(sc));
192 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
194 LIST_INSERT_HEAD(&V_gre_softc_list, sc, gre_list);
200 gre_clone_destroy(struct ifnet *ifp)
202 struct gre_softc *sc;
204 sx_xlock(&gre_ioctl_sx);
206 gre_delete_tunnel(ifp);
208 LIST_REMOVE(sc, gre_list);
212 ifp->if_softc = NULL;
213 sx_xunlock(&gre_ioctl_sx);
216 GRE_LOCK_DESTROY(sc);
221 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
224 struct ifreq *ifr = (struct ifreq *)data;
225 struct sockaddr *src, *dst;
226 struct gre_softc *sc;
228 struct sockaddr_in *sin = NULL;
231 struct sockaddr_in6 *sin6 = NULL;
239 if (ifr->ifr_mtu < 576)
243 ifp->if_flags |= IFF_UP;
257 sx_xlock(&gre_ioctl_sx);
267 sc->gre_mtu = ifr->ifr_mtu;
273 case SIOCSIFPHYADDR_IN6:
279 src = (struct sockaddr *)
280 &(((struct in_aliasreq *)data)->ifra_addr);
281 dst = (struct sockaddr *)
282 &(((struct in_aliasreq *)data)->ifra_dstaddr);
286 case SIOCSIFPHYADDR_IN6:
287 src = (struct sockaddr *)
288 &(((struct in6_aliasreq *)data)->ifra_addr);
289 dst = (struct sockaddr *)
290 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
294 error = EAFNOSUPPORT;
297 /* sa_family must be equal */
298 if (src->sa_family != dst->sa_family ||
299 src->sa_len != dst->sa_len)
302 /* validate sa_len */
303 switch (src->sa_family) {
306 if (src->sa_len != sizeof(struct sockaddr_in))
312 if (src->sa_len != sizeof(struct sockaddr_in6))
317 error = EAFNOSUPPORT;
320 /* check sa_family looks sane for the cmd */
321 error = EAFNOSUPPORT;
325 if (src->sa_family == AF_INET)
330 case SIOCSIFPHYADDR_IN6:
331 if (src->sa_family == AF_INET6)
336 error = EADDRNOTAVAIL;
337 switch (src->sa_family) {
340 if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
341 satosin(dst)->sin_addr.s_addr == INADDR_ANY)
347 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)
349 IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr))
352 * Check validity of the scope zone ID of the
353 * addresses, and convert it into the kernel
354 * internal form if necessary.
356 error = sa6_embedscope(satosin6(src), 0);
359 error = sa6_embedscope(satosin6(dst), 0);
364 error = gre_set_tunnel(ifp, src, dst);
367 gre_delete_tunnel(ifp);
369 case SIOCGIFPSRCADDR:
370 case SIOCGIFPDSTADDR:
372 case SIOCGIFPSRCADDR_IN6:
373 case SIOCGIFPDSTADDR_IN6:
375 if (sc->gre_family == 0) {
376 error = EADDRNOTAVAIL;
382 case SIOCGIFPSRCADDR:
383 case SIOCGIFPDSTADDR:
384 if (sc->gre_family != AF_INET) {
385 error = EADDRNOTAVAIL;
388 sin = (struct sockaddr_in *)&ifr->ifr_addr;
389 memset(sin, 0, sizeof(*sin));
390 sin->sin_family = AF_INET;
391 sin->sin_len = sizeof(*sin);
395 case SIOCGIFPSRCADDR_IN6:
396 case SIOCGIFPDSTADDR_IN6:
397 if (sc->gre_family != AF_INET6) {
398 error = EADDRNOTAVAIL;
401 sin6 = (struct sockaddr_in6 *)
402 &(((struct in6_ifreq *)data)->ifr_addr);
403 memset(sin6, 0, sizeof(*sin6));
404 sin6->sin6_family = AF_INET6;
405 sin6->sin6_len = sizeof(*sin6);
412 case SIOCGIFPSRCADDR:
413 sin->sin_addr = sc->gre_oip.ip_src;
415 case SIOCGIFPDSTADDR:
416 sin->sin_addr = sc->gre_oip.ip_dst;
420 case SIOCGIFPSRCADDR_IN6:
421 sin6->sin6_addr = sc->gre_oip6.ip6_src;
423 case SIOCGIFPDSTADDR_IN6:
424 sin6->sin6_addr = sc->gre_oip6.ip6_dst;
434 case SIOCGIFPSRCADDR:
435 case SIOCGIFPDSTADDR:
436 error = prison_if(curthread->td_ucred,
437 (struct sockaddr *)sin);
439 memset(sin, 0, sizeof(*sin));
443 case SIOCGIFPSRCADDR_IN6:
444 case SIOCGIFPDSTADDR_IN6:
445 error = prison_if(curthread->td_ucred,
446 (struct sockaddr *)sin6);
448 error = sa6_recoverscope(sin6);
450 memset(sin6, 0, sizeof(*sin6));
455 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
457 if ((error = copyin(ifr->ifr_data, &opt, sizeof(opt))) != 0)
459 if (sc->gre_key != opt) {
467 error = copyout(&sc->gre_key, ifr->ifr_data, sizeof(sc->gre_key));
470 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
472 if ((error = copyin(ifr->ifr_data, &opt, sizeof(opt))) != 0)
474 if (opt & ~GRE_OPTMASK)
477 if (sc->gre_options != opt) {
479 sc->gre_options = opt;
487 error = copyout(&sc->gre_options, ifr->ifr_data,
488 sizeof(sc->gre_options));
495 sx_xunlock(&gre_ioctl_sx);
500 gre_updatehdr(struct gre_softc *sc)
502 struct grehdr *gh = NULL;
506 GRE_WLOCK_ASSERT(sc);
507 switch (sc->gre_family) {
510 sc->gre_hlen = sizeof(struct greip);
511 sc->gre_oip.ip_v = IPPROTO_IPV4;
512 sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
513 sc->gre_oip.ip_p = IPPROTO_GRE;
514 gh = &sc->gre_gihdr->gi_gre;
519 sc->gre_hlen = sizeof(struct greip6);
520 sc->gre_oip6.ip6_vfc = IPV6_VERSION;
521 sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
522 gh = &sc->gre_gi6hdr->gi6_gre;
530 if (sc->gre_options & GRE_ENABLE_CSUM) {
531 flags |= GRE_FLAGS_CP;
532 sc->gre_hlen += 2 * sizeof(uint16_t);
535 if (sc->gre_key != 0) {
536 flags |= GRE_FLAGS_KP;
537 sc->gre_hlen += sizeof(uint32_t);
538 *opts++ = htonl(sc->gre_key);
540 if (sc->gre_options & GRE_ENABLE_SEQ) {
541 flags |= GRE_FLAGS_SP;
542 sc->gre_hlen += sizeof(uint32_t);
546 gh->gre_flags = htons(flags);
547 GRE2IFP(sc)->if_mtu = sc->gre_mtu - sc->gre_hlen;
551 gre_detach(struct gre_softc *sc)
554 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
555 if (sc->gre_ecookie != NULL)
556 encap_detach(sc->gre_ecookie);
557 sc->gre_ecookie = NULL;
561 gre_set_tunnel(struct ifnet *ifp, struct sockaddr *src,
562 struct sockaddr *dst)
564 struct gre_softc *sc, *tsc;
574 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
577 LIST_FOREACH(tsc, &V_gre_softc_list, gre_list) {
578 if (tsc == sc || tsc->gre_family != src->sa_family)
581 if (tsc->gre_family == AF_INET &&
582 tsc->gre_oip.ip_src.s_addr ==
583 satosin(src)->sin_addr.s_addr &&
584 tsc->gre_oip.ip_dst.s_addr ==
585 satosin(dst)->sin_addr.s_addr) {
587 return (EADDRNOTAVAIL);
591 if (tsc->gre_family == AF_INET6 &&
592 IN6_ARE_ADDR_EQUAL(&tsc->gre_oip6.ip6_src,
593 &satosin6(src)->sin6_addr) &&
594 IN6_ARE_ADDR_EQUAL(&tsc->gre_oip6.ip6_dst,
595 &satosin6(dst)->sin6_addr)) {
597 return (EADDRNOTAVAIL);
604 switch (src->sa_family) {
607 hdr = ip = malloc(sizeof(struct greip) +
608 3 * sizeof(uint32_t), M_GRE, M_WAITOK | M_ZERO);
609 ip->ip_src = satosin(src)->sin_addr;
610 ip->ip_dst = satosin(dst)->sin_addr;
615 hdr = ip6 = malloc(sizeof(struct greip6) +
616 3 * sizeof(uint32_t), M_GRE, M_WAITOK | M_ZERO);
617 ip6->ip6_src = satosin6(src)->sin6_addr;
618 ip6->ip6_dst = satosin6(dst)->sin6_addr;
622 return (EAFNOSUPPORT);
624 if (sc->gre_family != src->sa_family)
627 if (sc->gre_family != 0)
628 free(sc->gre_hdr, M_GRE);
629 sc->gre_family = src->sa_family;
632 sc->gre_iseq = UINT32_MAX;
636 switch (src->sa_family) {
639 error = in_gre_attach(sc);
644 error = in6_gre_attach(sc);
649 ifp->if_drv_flags |= IFF_DRV_RUNNING;
654 gre_delete_tunnel(struct ifnet *ifp)
656 struct gre_softc *sc = ifp->if_softc;
660 family = sc->gre_family;
665 free(sc->gre_hdr, M_GRE);
667 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
671 gre_input(struct mbuf **mp, int *offp, int proto)
673 struct gre_softc *sc;
682 sc = encap_getarg(m);
683 KASSERT(sc != NULL, ("encap_getarg returned NULL"));
686 gh = (struct grehdr *)mtodo(m, *offp);
687 flags = ntohs(gh->gre_flags);
688 if (flags & ~GRE_FLAGS_MASK)
691 hlen = 2 * sizeof(uint16_t);
692 if (flags & GRE_FLAGS_CP) {
693 /* reserved1 field must be zero */
694 if (((uint16_t *)opts)[1] != 0)
696 if (in_cksum_skip(m, m->m_pkthdr.len, *offp) != 0)
698 hlen += 2 * sizeof(uint16_t);
701 if (flags & GRE_FLAGS_KP) {
703 hlen += sizeof(uint32_t);
708 if (sc->gre_key != 0 && (key != sc->gre_key || key != 0))
711 if (flags & GRE_FLAGS_SP) {
712 /* seq = ntohl(*opts); */
713 hlen += sizeof(uint32_t);
715 switch (ntohs(gh->gre_proto)) {
718 * For WCCP skip an additional 4 bytes if after GRE header
719 * doesn't follow an IP header.
721 if (flags == 0 && (*(uint8_t *)gh->gre_opts & 0xF0) != 0x40)
722 hlen += sizeof(uint32_t);
735 m_adj(m, *offp + hlen);
737 m->m_pkthdr.rcvif = ifp;
738 M_SETFIB(m, sc->gre_fibnum);
740 mac_ifnet_create_mbuf(ifp, m);
742 BPF_MTAP2(ifp, &af, sizeof(af), m);
744 ifp->if_ibytes += m->m_pkthdr.len;
745 if ((ifp->if_flags & IFF_MONITOR) != 0)
748 netisr_dispatch(isr, m);
749 return (IPPROTO_DONE);
753 return (IPPROTO_DONE);
756 #define MTAG_GRE 1307983903
758 gre_check_nesting(struct ifnet *ifp, struct mbuf *m)
765 while ((mtag = m_tag_locate(m, MTAG_GRE, 0, mtag)) != NULL) {
766 if (*(struct ifnet **)(mtag + 1) == ifp) {
767 log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname);
772 if (count > V_max_gre_nesting) {
774 "%s: if_output recursively called too many times(%d)\n",
775 ifp->if_xname, count);
778 mtag = m_tag_alloc(MTAG_GRE, 0, sizeof(struct ifnet *), M_NOWAIT);
781 *(struct ifnet **)(mtag + 1) = ifp;
782 m_tag_prepend(m, mtag);
787 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
794 error = mac_ifnet_check_transmit(ifp, m);
798 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
799 (ifp->if_flags & IFF_UP) == 0) {
804 error = gre_check_nesting(ifp, m);
808 m->m_flags &= ~(M_BCAST|M_MCAST);
809 if (dst->sa_family == AF_UNSPEC)
810 bcopy(dst->sa_data, &af, sizeof(af));
813 BPF_MTAP2(ifp, &af, sizeof(af), m);
814 m->m_pkthdr.csum_data = af; /* save af for if_transmit */
815 return (ifp->if_transmit(ifp, m));
823 gre_setseqn(struct grehdr *gh, uint32_t seq)
829 flags = ntohs(gh->gre_flags);
830 KASSERT((flags & GRE_FLAGS_SP) != 0,
831 ("gre_setseqn called, but GRE_FLAGS_SP isn't set "));
832 if (flags & GRE_FLAGS_CP)
834 if (flags & GRE_FLAGS_KP)
840 gre_transmit(struct ifnet *ifp, struct mbuf *m)
843 struct gre_softc *sc;
845 uint32_t iaf, oaf, oseq;
846 int error, hlen, olen, plen;
847 int want_seq, want_csum;
857 if (sc->gre_family == 0) {
863 iaf = m->m_pkthdr.csum_data;
864 oaf = sc->gre_family;
866 want_seq = (sc->gre_options & GRE_ENABLE_SEQ) != 0;
868 oseq = sc->gre_oseq++;
870 oseq = 0; /* Make compiler happy. */
871 want_csum = (sc->gre_options & GRE_ENABLE_CSUM) != 0;
872 M_SETFIB(m, sc->gre_fibnum);
873 M_PREPEND(m, hlen, M_NOWAIT);
879 bcopy(sc->gre_hdr, mtod(m, void *), hlen);
884 olen = sizeof(struct ip);
889 olen = sizeof(struct ip6_hdr);
896 gh = (struct grehdr *)mtodo(m, olen);
900 gh->gre_proto = htons(ETHERTYPE_IP);
905 gh->gre_proto = htons(ETHERTYPE_IPV6);
913 gre_setseqn(gh, oseq);
915 *(uint16_t *)gh->gre_opts = in_cksum_skip(m,
916 m->m_pkthdr.len, olen);
918 plen = m->m_pkthdr.len - hlen;
922 error = in_gre_output(m, iaf, hlen);
927 error = in6_gre_output(m, iaf, hlen);
939 ifp->if_obytes += plen;
945 gre_qflush(struct ifnet *ifp __unused)
951 gremodevent(module_t mod, int type, void *data)
964 static moduledata_t gre_mod = {
970 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
971 MODULE_VERSION(if_gre, 1);