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>
67 #include <net/route.h>
69 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/in_var.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
78 #include <netinet/ip6.h>
79 #include <netinet6/in6_var.h>
80 #include <netinet6/ip6_var.h>
81 #include <netinet6/scope6_var.h>
84 #include <netinet/ip_encap.h>
86 #include <net/if_gre.h>
88 #include <machine/in_cksum.h>
90 #include <security/mac/mac_framework.h>
92 static const char grename[] = "gre";
93 static MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation");
94 static VNET_DEFINE(struct mtx, gre_mtx);
95 #define V_gre_mtx VNET(gre_mtx)
96 #define GRE_LIST_LOCK_INIT(x) mtx_init(&V_gre_mtx, "gre_mtx", NULL, \
98 #define GRE_LIST_LOCK_DESTROY(x) mtx_destroy(&V_gre_mtx)
99 #define GRE_LIST_LOCK(x) mtx_lock(&V_gre_mtx)
100 #define GRE_LIST_UNLOCK(x) mtx_unlock(&V_gre_mtx)
102 static VNET_DEFINE(LIST_HEAD(, gre_softc), gre_softc_list);
103 #define V_gre_softc_list VNET(gre_softc_list)
104 static struct sx gre_ioctl_sx;
105 SX_SYSINIT(gre_ioctl_sx, &gre_ioctl_sx, "gre_ioctl");
107 static int gre_clone_create(struct if_clone *, int, caddr_t);
108 static void gre_clone_destroy(struct ifnet *);
109 static VNET_DEFINE(struct if_clone *, gre_cloner);
110 #define V_gre_cloner VNET(gre_cloner)
112 static void gre_qflush(struct ifnet *);
113 static int gre_transmit(struct ifnet *, struct mbuf *);
114 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
115 static int gre_output(struct ifnet *, struct mbuf *,
116 const struct sockaddr *, struct route *);
118 static void gre_updatehdr(struct gre_softc *);
119 static int gre_set_tunnel(struct ifnet *, struct sockaddr *,
121 static void gre_delete_tunnel(struct ifnet *);
123 SYSCTL_DECL(_net_link);
124 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
125 "Generic Routing Encapsulation");
128 * This macro controls the default upper limitation on nesting of gre tunnels.
129 * Since, setting a large value to this macro with a careless configuration
130 * may introduce system crash, we don't allow any nestings by default.
131 * If you need to configure nested gre tunnels, you can define this macro
132 * in your kernel configuration file. However, if you do so, please be
133 * careful to configure the tunnels so that it won't make a loop.
135 #define MAX_GRE_NEST 1
138 static VNET_DEFINE(int, max_gre_nesting) = MAX_GRE_NEST;
139 #define V_max_gre_nesting VNET(max_gre_nesting)
140 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
141 &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels");
144 vnet_gre_init(const void *unused __unused)
146 LIST_INIT(&V_gre_softc_list);
147 GRE_LIST_LOCK_INIT();
148 V_gre_cloner = if_clone_simple(grename, gre_clone_create,
149 gre_clone_destroy, 0);
151 VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
152 vnet_gre_init, NULL);
155 vnet_gre_uninit(const void *unused __unused)
158 if_clone_detach(V_gre_cloner);
159 GRE_LIST_LOCK_DESTROY();
161 VNET_SYSUNINIT(vnet_gre_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
162 vnet_gre_uninit, NULL);
165 gre_clone_create(struct if_clone *ifc, int unit, caddr_t params)
167 struct gre_softc *sc;
169 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
170 sc->gre_fibnum = curthread->td_proc->p_fibnum;
171 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
173 GRE2IFP(sc)->if_softc = sc;
174 if_initname(GRE2IFP(sc), grename, unit);
176 GRE2IFP(sc)->if_mtu = sc->gre_mtu = GREMTU;
177 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
178 GRE2IFP(sc)->if_output = gre_output;
179 GRE2IFP(sc)->if_ioctl = gre_ioctl;
180 GRE2IFP(sc)->if_transmit = gre_transmit;
181 GRE2IFP(sc)->if_qflush = gre_qflush;
182 if_attach(GRE2IFP(sc));
183 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
185 LIST_INSERT_HEAD(&V_gre_softc_list, sc, gre_list);
191 gre_clone_destroy(struct ifnet *ifp)
193 struct gre_softc *sc;
195 sx_xlock(&gre_ioctl_sx);
197 gre_delete_tunnel(ifp);
199 LIST_REMOVE(sc, gre_list);
203 ifp->if_softc = NULL;
204 sx_xunlock(&gre_ioctl_sx);
207 GRE_LOCK_DESTROY(sc);
212 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
215 struct ifreq *ifr = (struct ifreq *)data;
216 struct sockaddr *src, *dst;
217 struct gre_softc *sc;
219 struct sockaddr_in *sin = NULL;
222 struct sockaddr_in6 *sin6 = NULL;
230 if (ifr->ifr_mtu < 576)
234 ifp->if_flags |= IFF_UP;
248 sx_xlock(&gre_ioctl_sx);
258 sc->gre_mtu = ifr->ifr_mtu;
264 case SIOCSIFPHYADDR_IN6:
270 src = (struct sockaddr *)
271 &(((struct in_aliasreq *)data)->ifra_addr);
272 dst = (struct sockaddr *)
273 &(((struct in_aliasreq *)data)->ifra_dstaddr);
277 case SIOCSIFPHYADDR_IN6:
278 src = (struct sockaddr *)
279 &(((struct in6_aliasreq *)data)->ifra_addr);
280 dst = (struct sockaddr *)
281 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
285 error = EAFNOSUPPORT;
288 /* sa_family must be equal */
289 if (src->sa_family != dst->sa_family ||
290 src->sa_len != dst->sa_len)
293 /* validate sa_len */
294 switch (src->sa_family) {
297 if (src->sa_len != sizeof(struct sockaddr_in))
303 if (src->sa_len != sizeof(struct sockaddr_in6))
308 error = EAFNOSUPPORT;
311 /* check sa_family looks sane for the cmd */
312 error = EAFNOSUPPORT;
316 if (src->sa_family == AF_INET)
321 case SIOCSIFPHYADDR_IN6:
322 if (src->sa_family == AF_INET6)
327 error = EADDRNOTAVAIL;
328 switch (src->sa_family) {
331 if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
332 satosin(dst)->sin_addr.s_addr == INADDR_ANY)
338 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)
340 IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr))
343 * Check validity of the scope zone ID of the
344 * addresses, and convert it into the kernel
345 * internal form if necessary.
347 error = sa6_embedscope(satosin6(src), 0);
350 error = sa6_embedscope(satosin6(dst), 0);
355 error = gre_set_tunnel(ifp, src, dst);
358 gre_delete_tunnel(ifp);
360 case SIOCGIFPSRCADDR:
361 case SIOCGIFPDSTADDR:
363 case SIOCGIFPSRCADDR_IN6:
364 case SIOCGIFPDSTADDR_IN6:
366 if (sc->gre_family == 0) {
367 error = EADDRNOTAVAIL;
373 case SIOCGIFPSRCADDR:
374 case SIOCGIFPDSTADDR:
375 if (sc->gre_family != AF_INET) {
376 error = EADDRNOTAVAIL;
379 sin = (struct sockaddr_in *)&ifr->ifr_addr;
380 memset(sin, 0, sizeof(*sin));
381 sin->sin_family = AF_INET;
382 sin->sin_len = sizeof(*sin);
386 case SIOCGIFPSRCADDR_IN6:
387 case SIOCGIFPDSTADDR_IN6:
388 if (sc->gre_family != AF_INET6) {
389 error = EADDRNOTAVAIL;
392 sin6 = (struct sockaddr_in6 *)
393 &(((struct in6_ifreq *)data)->ifr_addr);
394 memset(sin6, 0, sizeof(*sin6));
395 sin6->sin6_family = AF_INET6;
396 sin6->sin6_len = sizeof(*sin6);
403 case SIOCGIFPSRCADDR:
404 sin->sin_addr = sc->gre_oip.ip_src;
406 case SIOCGIFPDSTADDR:
407 sin->sin_addr = sc->gre_oip.ip_dst;
411 case SIOCGIFPSRCADDR_IN6:
412 sin6->sin6_addr = sc->gre_oip6.ip6_src;
414 case SIOCGIFPDSTADDR_IN6:
415 sin6->sin6_addr = sc->gre_oip6.ip6_dst;
425 case SIOCGIFPSRCADDR:
426 case SIOCGIFPDSTADDR:
427 error = prison_if(curthread->td_ucred,
428 (struct sockaddr *)sin);
430 memset(sin, 0, sizeof(*sin));
434 case SIOCGIFPSRCADDR_IN6:
435 case SIOCGIFPDSTADDR_IN6:
436 error = prison_if(curthread->td_ucred,
437 (struct sockaddr *)sin6);
439 error = sa6_recoverscope(sin6);
441 memset(sin6, 0, sizeof(*sin6));
446 ifr->ifr_fib = sc->gre_fibnum;
449 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
451 if (ifr->ifr_fib >= rt_numfibs)
454 sc->gre_fibnum = ifr->ifr_fib;
457 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
459 if ((error = copyin(ifr->ifr_data, &opt, sizeof(opt))) != 0)
461 if (sc->gre_key != opt) {
469 error = copyout(&sc->gre_key, ifr->ifr_data,
470 sizeof(sc->gre_key));
473 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
475 if ((error = copyin(ifr->ifr_data, &opt, sizeof(opt))) != 0)
477 if (opt & ~GRE_OPTMASK)
480 if (sc->gre_options != opt) {
482 sc->gre_options = opt;
490 error = copyout(&sc->gre_options, ifr->ifr_data,
491 sizeof(sc->gre_options));
498 sx_xunlock(&gre_ioctl_sx);
503 gre_updatehdr(struct gre_softc *sc)
505 struct grehdr *gh = NULL;
509 GRE_WLOCK_ASSERT(sc);
510 switch (sc->gre_family) {
513 sc->gre_hlen = sizeof(struct greip);
514 sc->gre_oip.ip_v = IPPROTO_IPV4;
515 sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
516 sc->gre_oip.ip_p = IPPROTO_GRE;
517 gh = &sc->gre_gihdr->gi_gre;
522 sc->gre_hlen = sizeof(struct greip6);
523 sc->gre_oip6.ip6_vfc = IPV6_VERSION;
524 sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
525 gh = &sc->gre_gi6hdr->gi6_gre;
533 if (sc->gre_options & GRE_ENABLE_CSUM) {
534 flags |= GRE_FLAGS_CP;
535 sc->gre_hlen += 2 * sizeof(uint16_t);
538 if (sc->gre_key != 0) {
539 flags |= GRE_FLAGS_KP;
540 sc->gre_hlen += sizeof(uint32_t);
541 *opts++ = htonl(sc->gre_key);
543 if (sc->gre_options & GRE_ENABLE_SEQ) {
544 flags |= GRE_FLAGS_SP;
545 sc->gre_hlen += sizeof(uint32_t);
549 gh->gre_flags = htons(flags);
550 GRE2IFP(sc)->if_mtu = sc->gre_mtu - sc->gre_hlen;
554 gre_detach(struct gre_softc *sc)
557 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
558 if (sc->gre_ecookie != NULL)
559 encap_detach(sc->gre_ecookie);
560 sc->gre_ecookie = NULL;
564 gre_set_tunnel(struct ifnet *ifp, struct sockaddr *src,
565 struct sockaddr *dst)
567 struct gre_softc *sc, *tsc;
577 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
580 LIST_FOREACH(tsc, &V_gre_softc_list, gre_list) {
581 if (tsc == sc || tsc->gre_family != src->sa_family)
584 if (tsc->gre_family == AF_INET &&
585 tsc->gre_oip.ip_src.s_addr ==
586 satosin(src)->sin_addr.s_addr &&
587 tsc->gre_oip.ip_dst.s_addr ==
588 satosin(dst)->sin_addr.s_addr) {
590 return (EADDRNOTAVAIL);
594 if (tsc->gre_family == AF_INET6 &&
595 IN6_ARE_ADDR_EQUAL(&tsc->gre_oip6.ip6_src,
596 &satosin6(src)->sin6_addr) &&
597 IN6_ARE_ADDR_EQUAL(&tsc->gre_oip6.ip6_dst,
598 &satosin6(dst)->sin6_addr)) {
600 return (EADDRNOTAVAIL);
607 switch (src->sa_family) {
610 hdr = ip = malloc(sizeof(struct greip) +
611 3 * sizeof(uint32_t), M_GRE, M_WAITOK | M_ZERO);
612 ip->ip_src = satosin(src)->sin_addr;
613 ip->ip_dst = satosin(dst)->sin_addr;
618 hdr = ip6 = malloc(sizeof(struct greip6) +
619 3 * sizeof(uint32_t), M_GRE, M_WAITOK | M_ZERO);
620 ip6->ip6_src = satosin6(src)->sin6_addr;
621 ip6->ip6_dst = satosin6(dst)->sin6_addr;
625 return (EAFNOSUPPORT);
627 if (sc->gre_family != src->sa_family)
630 if (sc->gre_family != 0)
631 free(sc->gre_hdr, M_GRE);
632 sc->gre_family = src->sa_family;
635 sc->gre_iseq = UINT32_MAX;
639 switch (src->sa_family) {
642 error = in_gre_attach(sc);
647 error = in6_gre_attach(sc);
652 ifp->if_drv_flags |= IFF_DRV_RUNNING;
657 gre_delete_tunnel(struct ifnet *ifp)
659 struct gre_softc *sc = ifp->if_softc;
663 family = sc->gre_family;
668 free(sc->gre_hdr, M_GRE);
670 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
674 gre_input(struct mbuf **mp, int *offp, int proto)
676 struct gre_softc *sc;
685 sc = encap_getarg(m);
686 KASSERT(sc != NULL, ("encap_getarg returned NULL"));
689 gh = (struct grehdr *)mtodo(m, *offp);
690 flags = ntohs(gh->gre_flags);
691 if (flags & ~GRE_FLAGS_MASK)
694 hlen = 2 * sizeof(uint16_t);
695 if (flags & GRE_FLAGS_CP) {
696 /* reserved1 field must be zero */
697 if (((uint16_t *)opts)[1] != 0)
699 if (in_cksum_skip(m, m->m_pkthdr.len, *offp) != 0)
701 hlen += 2 * sizeof(uint16_t);
704 if (flags & GRE_FLAGS_KP) {
706 hlen += sizeof(uint32_t);
711 if (sc->gre_key != 0 && (key != sc->gre_key || key != 0))
714 if (flags & GRE_FLAGS_SP) {
715 /* seq = ntohl(*opts); */
716 hlen += sizeof(uint32_t);
718 switch (ntohs(gh->gre_proto)) {
721 * For WCCP skip an additional 4 bytes if after GRE header
722 * doesn't follow an IP header.
724 if (flags == 0 && (*(uint8_t *)gh->gre_opts & 0xF0) != 0x40)
725 hlen += sizeof(uint32_t);
738 m_adj(m, *offp + hlen);
740 m->m_pkthdr.rcvif = ifp;
741 M_SETFIB(m, ifp->if_fib);
743 mac_ifnet_create_mbuf(ifp, m);
745 BPF_MTAP2(ifp, &af, sizeof(af), m);
747 ifp->if_ibytes += m->m_pkthdr.len;
748 if ((ifp->if_flags & IFF_MONITOR) != 0)
751 netisr_dispatch(isr, m);
752 return (IPPROTO_DONE);
756 return (IPPROTO_DONE);
759 #define MTAG_GRE 1307983903
761 gre_check_nesting(struct ifnet *ifp, struct mbuf *m)
768 while ((mtag = m_tag_locate(m, MTAG_GRE, 0, mtag)) != NULL) {
769 if (*(struct ifnet **)(mtag + 1) == ifp) {
770 log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname);
775 if (count > V_max_gre_nesting) {
777 "%s: if_output recursively called too many times(%d)\n",
778 ifp->if_xname, count);
781 mtag = m_tag_alloc(MTAG_GRE, 0, sizeof(struct ifnet *), M_NOWAIT);
784 *(struct ifnet **)(mtag + 1) = ifp;
785 m_tag_prepend(m, mtag);
790 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
797 error = mac_ifnet_check_transmit(ifp, m);
801 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
802 (ifp->if_flags & IFF_UP) == 0) {
807 error = gre_check_nesting(ifp, m);
811 m->m_flags &= ~(M_BCAST|M_MCAST);
812 if (dst->sa_family == AF_UNSPEC)
813 bcopy(dst->sa_data, &af, sizeof(af));
816 BPF_MTAP2(ifp, &af, sizeof(af), m);
817 m->m_pkthdr.csum_data = af; /* save af for if_transmit */
818 return (ifp->if_transmit(ifp, m));
826 gre_setseqn(struct grehdr *gh, uint32_t seq)
832 flags = ntohs(gh->gre_flags);
833 KASSERT((flags & GRE_FLAGS_SP) != 0,
834 ("gre_setseqn called, but GRE_FLAGS_SP isn't set "));
835 if (flags & GRE_FLAGS_CP)
837 if (flags & GRE_FLAGS_KP)
843 gre_transmit(struct ifnet *ifp, struct mbuf *m)
846 struct gre_softc *sc;
848 uint32_t iaf, oaf, oseq;
849 int error, hlen, olen, plen;
850 int want_seq, want_csum;
860 if (sc->gre_family == 0) {
866 iaf = m->m_pkthdr.csum_data;
867 oaf = sc->gre_family;
869 want_seq = (sc->gre_options & GRE_ENABLE_SEQ) != 0;
871 oseq = sc->gre_oseq++;
873 oseq = 0; /* Make compiler happy. */
874 want_csum = (sc->gre_options & GRE_ENABLE_CSUM) != 0;
875 M_SETFIB(m, sc->gre_fibnum);
876 M_PREPEND(m, hlen, M_NOWAIT);
882 bcopy(sc->gre_hdr, mtod(m, void *), hlen);
887 olen = sizeof(struct ip);
892 olen = sizeof(struct ip6_hdr);
899 gh = (struct grehdr *)mtodo(m, olen);
903 gh->gre_proto = htons(ETHERTYPE_IP);
908 gh->gre_proto = htons(ETHERTYPE_IPV6);
916 gre_setseqn(gh, oseq);
918 *(uint16_t *)gh->gre_opts = in_cksum_skip(m,
919 m->m_pkthdr.len, olen);
921 plen = m->m_pkthdr.len - hlen;
925 error = in_gre_output(m, iaf, hlen);
930 error = in6_gre_output(m, iaf, hlen);
942 ifp->if_obytes += plen;
948 gre_qflush(struct ifnet *ifp __unused)
954 gremodevent(module_t mod, int type, void *data)
967 static moduledata_t gre_mod = {
973 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
974 MODULE_VERSION(if_gre, 1);