2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Heiko W.Rupp <hwr@pilhuhn.de>
11 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $
37 #include <sys/cdefs.h>
39 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 #include <sys/systm.h>
58 #include <net/ethernet.h>
60 #include <net/if_var.h>
61 #include <net/if_private.h>
62 #include <net/if_clone.h>
63 #include <net/if_types.h>
64 #include <net/netisr.h>
66 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_pcb.h>
71 #include <netinet/in_var.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_var.h>
75 #include <netinet/in_rss.h>
80 #include <netinet/ip6.h>
81 #include <netinet6/in6_var.h>
82 #include <netinet6/ip6_var.h>
84 #include <netinet6/in6_rss.h>
88 #include <netinet/ip_encap.h>
89 #include <netinet/udp.h>
91 #include <net/if_gre.h>
93 #include <machine/in_cksum.h>
94 #include <security/mac/mac_framework.h>
98 static const char grename[] = "gre";
99 MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation");
101 static struct sx gre_ioctl_sx;
102 SX_SYSINIT(gre_ioctl_sx, &gre_ioctl_sx, "gre_ioctl");
104 static int gre_clone_create(struct if_clone *, int, caddr_t);
105 static void gre_clone_destroy(struct ifnet *);
106 VNET_DEFINE_STATIC(struct if_clone *, gre_cloner);
107 #define V_gre_cloner VNET(gre_cloner)
110 static void gre_reassign(struct ifnet *, struct vnet *, char *);
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 *);
117 static void gre_delete_tunnel(struct gre_softc *);
119 SYSCTL_DECL(_net_link);
120 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
121 "Generic Routing Encapsulation");
124 * This macro controls the default upper limitation on nesting of gre tunnels.
125 * Since, setting a large value to this macro with a careless configuration
126 * may introduce system crash, we don't allow any nestings by default.
127 * If you need to configure nested gre tunnels, you can define this macro
128 * in your kernel configuration file. However, if you do so, please be
129 * careful to configure the tunnels so that it won't make a loop.
131 #define MAX_GRE_NEST 1
134 VNET_DEFINE_STATIC(int, max_gre_nesting) = MAX_GRE_NEST;
135 #define V_max_gre_nesting VNET(max_gre_nesting)
136 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
137 &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels");
140 vnet_gre_init(const void *unused __unused)
143 V_gre_cloner = if_clone_simple(grename, gre_clone_create,
144 gre_clone_destroy, 0);
152 VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
153 vnet_gre_init, NULL);
156 vnet_gre_uninit(const void *unused __unused)
159 if_clone_detach(V_gre_cloner);
166 /* XXX: epoch_call drain */
168 VNET_SYSUNINIT(vnet_gre_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
169 vnet_gre_uninit, NULL);
172 gre_clone_create(struct if_clone *ifc, int unit, caddr_t params)
174 struct gre_softc *sc;
176 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
177 sc->gre_fibnum = curthread->td_proc->p_fibnum;
178 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
179 GRE2IFP(sc)->if_softc = sc;
180 if_initname(GRE2IFP(sc), grename, unit);
182 GRE2IFP(sc)->if_mtu = GREMTU;
183 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
184 GRE2IFP(sc)->if_output = gre_output;
185 GRE2IFP(sc)->if_ioctl = gre_ioctl;
186 GRE2IFP(sc)->if_transmit = gre_transmit;
187 GRE2IFP(sc)->if_qflush = gre_qflush;
189 GRE2IFP(sc)->if_reassign = gre_reassign;
191 GRE2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
192 GRE2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
193 if_attach(GRE2IFP(sc));
194 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
200 gre_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
201 char *unused __unused)
203 struct gre_softc *sc;
205 sx_xlock(&gre_ioctl_sx);
208 gre_delete_tunnel(sc);
209 sx_xunlock(&gre_ioctl_sx);
214 gre_clone_destroy(struct ifnet *ifp)
216 struct gre_softc *sc;
218 sx_xlock(&gre_ioctl_sx);
220 gre_delete_tunnel(sc);
223 ifp->if_softc = NULL;
224 sx_xunlock(&gre_ioctl_sx);
232 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
234 struct ifreq *ifr = (struct ifreq *)data;
235 struct gre_softc *sc;
242 if (ifr->ifr_mtu < 576)
244 ifp->if_mtu = ifr->ifr_mtu;
247 ifp->if_flags |= IFF_UP;
260 sx_xlock(&gre_ioctl_sx);
269 if (sc->gre_family == 0)
271 gre_delete_tunnel(sc);
275 case SIOCGIFPSRCADDR:
276 case SIOCGIFPDSTADDR:
277 error = in_gre_ioctl(sc, cmd, data);
281 case SIOCSIFPHYADDR_IN6:
282 case SIOCGIFPSRCADDR_IN6:
283 case SIOCGIFPDSTADDR_IN6:
284 error = in6_gre_ioctl(sc, cmd, data);
288 ifr->ifr_fib = sc->gre_fibnum;
291 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
293 if (ifr->ifr_fib >= rt_numfibs)
296 sc->gre_fibnum = ifr->ifr_fib;
301 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
303 if ((error = copyin(ifr_data_get_ptr(ifr), &opt,
306 if (cmd == GRESKEY) {
307 if (sc->gre_key == opt)
309 } else if (cmd == GRESOPTS) {
310 if (opt & ~GRE_OPTMASK) {
314 if (sc->gre_options == opt)
316 } else if (cmd == GRESPORT) {
317 if (opt != 0 && (opt < V_ipport_hifirstauto ||
318 opt > V_ipport_hilastauto)) {
322 if (sc->gre_port == opt)
324 if ((sc->gre_options & GRE_UDPENCAP) == 0) {
326 * UDP encapsulation is not enabled, thus
327 * there is no need to reattach softc.
333 switch (sc->gre_family) {
336 error = in_gre_setopts(sc, cmd, opt);
341 error = in6_gre_setopts(sc, cmd, opt);
346 * Tunnel is not yet configured.
347 * We can just change any parameters.
352 sc->gre_options = opt;
358 * XXX: Do we need to initiate change of interface
363 error = copyout(&sc->gre_key, ifr_data_get_ptr(ifr),
364 sizeof(sc->gre_key));
367 error = copyout(&sc->gre_options, ifr_data_get_ptr(ifr),
368 sizeof(sc->gre_options));
371 error = copyout(&sc->gre_port, ifr_data_get_ptr(ifr),
372 sizeof(sc->gre_port));
378 if (error == 0 && sc->gre_family != 0) {
381 cmd == SIOCSIFPHYADDR ||
384 cmd == SIOCSIFPHYADDR_IN6 ||
387 if_link_state_change(ifp, LINK_STATE_UP);
391 sx_xunlock(&gre_ioctl_sx);
396 gre_delete_tunnel(struct gre_softc *sc)
398 struct gre_socket *gs;
400 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
401 if (sc->gre_family != 0) {
402 CK_LIST_REMOVE(sc, chain);
403 CK_LIST_REMOVE(sc, srchash);
405 free(sc->gre_hdr, M_GRE);
409 * If this Tunnel was the last one that could use UDP socket,
410 * we should unlink socket from hash table and close it.
412 if ((gs = sc->gre_so) != NULL && CK_LIST_EMPTY(&gs->list)) {
413 CK_LIST_REMOVE(gs, chain);
415 NET_EPOCH_CALL(gre_sofree, &gs->epoch_ctx);
418 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
419 if_link_state_change(GRE2IFP(sc), LINK_STATE_DOWN);
425 struct gre_list *hash;
428 hash = malloc(sizeof(struct gre_list) * GRE_HASH_SIZE,
430 for (i = 0; i < GRE_HASH_SIZE; i++)
431 CK_LIST_INIT(&hash[i]);
437 gre_hashdestroy(struct gre_list *hash)
444 gre_sofree(epoch_context_t ctx)
446 struct gre_socket *gs;
448 gs = __containerof(ctx, struct gre_socket, epoch_ctx);
452 static __inline uint16_t
453 gre_cksum_add(uint16_t sum, uint16_t a)
458 return (res + (res < a));
462 gre_update_udphdr(struct gre_softc *sc, struct udphdr *udp, uint16_t csum)
465 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
466 MPASS(sc->gre_options & GRE_UDPENCAP);
468 udp->uh_dport = htons(GRE_UDPPORT);
469 udp->uh_sport = htons(sc->gre_port);
475 gre_update_hdr(struct gre_softc *sc, struct grehdr *gh)
480 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
484 if (sc->gre_options & GRE_ENABLE_CSUM) {
485 flags |= GRE_FLAGS_CP;
486 sc->gre_hlen += 2 * sizeof(uint16_t);
489 if (sc->gre_key != 0) {
490 flags |= GRE_FLAGS_KP;
491 sc->gre_hlen += sizeof(uint32_t);
492 *opts++ = htonl(sc->gre_key);
494 if (sc->gre_options & GRE_ENABLE_SEQ) {
495 flags |= GRE_FLAGS_SP;
496 sc->gre_hlen += sizeof(uint32_t);
500 gh->gre_flags = htons(flags);
504 gre_input(struct mbuf *m, int off, int proto, void *arg)
506 struct gre_softc *sc = arg;
517 hlen = off + sizeof(struct grehdr) + 4 * sizeof(uint32_t);
518 if (m->m_pkthdr.len < hlen)
520 if (m->m_len < hlen) {
521 m = m_pullup(m, hlen);
525 gh = (struct grehdr *)mtodo(m, off);
526 flags = ntohs(gh->gre_flags);
527 if (flags & ~GRE_FLAGS_MASK)
530 hlen = 2 * sizeof(uint16_t);
531 if (flags & GRE_FLAGS_CP) {
532 /* reserved1 field must be zero */
533 if (((uint16_t *)opts)[1] != 0)
535 if (in_cksum_skip(m, m->m_pkthdr.len, off) != 0)
537 hlen += 2 * sizeof(uint16_t);
540 if (flags & GRE_FLAGS_KP) {
543 * XXX: The current implementation uses the key only for outgoing
544 * packets. But we can check the key value here, or even in the
545 * encapcheck function.
549 hlen += sizeof(uint32_t);
556 if (sc->gre_key != 0 && (key != sc->gre_key || key != 0))
559 if (flags & GRE_FLAGS_SP) {
563 hlen += sizeof(uint32_t);
565 switch (ntohs(gh->gre_proto)) {
568 * For WCCP skip an additional 4 bytes if after GRE header
569 * doesn't follow an IP header.
571 if (flags == 0 && (*(uint8_t *)gh->gre_opts & 0xF0) != 0x40)
572 hlen += sizeof(uint32_t);
585 m_adj(m, off + hlen);
587 m->m_pkthdr.rcvif = ifp;
588 M_SETFIB(m, ifp->if_fib);
590 mac_ifnet_create_mbuf(ifp, m);
592 BPF_MTAP2(ifp, &af, sizeof(af), m);
593 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
594 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
595 if ((ifp->if_flags & IFF_MONITOR) != 0)
598 netisr_dispatch(isr, m);
599 return (IPPROTO_DONE);
601 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
603 return (IPPROTO_DONE);
607 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
612 if (dst->sa_family == AF_UNSPEC)
613 bcopy(dst->sa_data, &af, sizeof(af));
615 af = RO_GET_FAMILY(ro, dst);
617 * Now save the af in the inbound pkt csum data, this is a cheat since
618 * we are using the inbound csum_data field to carry the af over to
619 * the gre_transmit() routine, avoiding using yet another mtag.
621 m->m_pkthdr.csum_data = af;
622 return (ifp->if_transmit(ifp, m));
626 gre_setseqn(struct grehdr *gh, uint32_t seq)
632 flags = ntohs(gh->gre_flags);
633 KASSERT((flags & GRE_FLAGS_SP) != 0,
634 ("gre_setseqn called, but GRE_FLAGS_SP isn't set "));
635 if (flags & GRE_FLAGS_CP)
637 if (flags & GRE_FLAGS_KP)
643 gre_flowid(struct gre_softc *sc, struct mbuf *m, uint32_t af)
647 if ((sc->gre_options & GRE_UDPENCAP) == 0 || sc->gre_port != 0)
653 flowid = rss_hash_ip4_2tuple(mtod(m, struct ip *)->ip_src,
654 mtod(m, struct ip *)->ip_dst);
657 flowid = mtod(m, struct ip *)->ip_src.s_addr ^
658 mtod(m, struct ip *)->ip_dst.s_addr;
664 flowid = rss_hash_ip6_2tuple(
665 &mtod(m, struct ip6_hdr *)->ip6_src,
666 &mtod(m, struct ip6_hdr *)->ip6_dst);
669 flowid = mtod(m, struct ip6_hdr *)->ip6_src.s6_addr32[3] ^
670 mtod(m, struct ip6_hdr *)->ip6_dst.s6_addr32[3];
679 #define MTAG_GRE 1307983903
681 gre_transmit(struct ifnet *ifp, struct mbuf *m)
684 struct gre_softc *sc;
694 error = mac_ifnet_check_transmit(ifp, m);
702 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
703 (ifp->if_flags & IFF_UP) == 0 ||
704 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
705 sc->gre_family == 0 ||
706 (error = if_tunnel_check_nesting(ifp, m, MTAG_GRE,
707 V_max_gre_nesting)) != 0) {
711 af = m->m_pkthdr.csum_data;
712 BPF_MTAP2(ifp, &af, sizeof(af), m);
713 m->m_flags &= ~(M_BCAST|M_MCAST);
714 flowid = gre_flowid(sc, m, af);
715 M_SETFIB(m, sc->gre_fibnum);
716 M_PREPEND(m, sc->gre_hlen, M_NOWAIT);
721 bcopy(sc->gre_hdr, mtod(m, void *), sc->gre_hlen);
722 /* Determine GRE proto */
726 proto = htons(ETHERTYPE_IP);
731 proto = htons(ETHERTYPE_IPV6);
739 /* Determine offset of GRE header */
740 switch (sc->gre_family) {
743 len = sizeof(struct ip);
748 len = sizeof(struct ip6_hdr);
756 if (sc->gre_options & GRE_UDPENCAP) {
757 uh = (struct udphdr *)mtodo(m, len);
758 uh->uh_sport |= htons(V_ipport_hifirstauto) |
759 (flowid >> 16) | (flowid & 0xFFFF);
760 uh->uh_sport = htons(ntohs(uh->uh_sport) %
761 V_ipport_hilastauto);
762 uh->uh_ulen = htons(m->m_pkthdr.len - len);
763 uh->uh_sum = gre_cksum_add(uh->uh_sum,
764 htons(m->m_pkthdr.len - len + IPPROTO_UDP));
765 m->m_pkthdr.csum_flags = sc->gre_csumflags;
766 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
767 len += sizeof(struct udphdr);
769 gh = (struct grehdr *)mtodo(m, len);
770 gh->gre_proto = proto;
771 if (sc->gre_options & GRE_ENABLE_SEQ)
772 gre_setseqn(gh, sc->gre_oseq++);
773 if (sc->gre_options & GRE_ENABLE_CSUM) {
774 *(uint16_t *)gh->gre_opts = in_cksum_skip(m,
775 m->m_pkthdr.len, len);
777 len = m->m_pkthdr.len - len;
778 switch (sc->gre_family) {
781 error = in_gre_output(m, af, sc->gre_hlen);
786 error = in6_gre_output(m, af, sc->gre_hlen, flowid);
795 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
797 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
798 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
805 gre_qflush(struct ifnet *ifp __unused)
811 gremodevent(module_t mod, int type, void *data)
824 static moduledata_t gre_mod = {
830 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
831 MODULE_VERSION(if_gre, 1);