2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
38 __FBSDID("$FreeBSD$");
41 #include "opt_inet6.h"
44 #include <sys/param.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.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_var.h>
63 #include <net/if_clone.h>
64 #include <net/if_types.h>
65 #include <net/netisr.h>
67 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/in_pcb.h>
72 #include <netinet/in_var.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
76 #include <netinet/in_rss.h>
81 #include <netinet/ip6.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet6/ip6_var.h>
85 #include <netinet6/in6_rss.h>
89 #include <netinet/ip_encap.h>
90 #include <netinet/udp.h>
92 #include <net/if_gre.h>
94 #include <machine/in_cksum.h>
95 #include <security/mac/mac_framework.h>
99 static const char grename[] = "gre";
100 MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation");
102 static struct sx gre_ioctl_sx;
103 SX_SYSINIT(gre_ioctl_sx, &gre_ioctl_sx, "gre_ioctl");
105 static int gre_clone_create(struct if_clone *, int, caddr_t);
106 static void gre_clone_destroy(struct ifnet *);
107 VNET_DEFINE_STATIC(struct if_clone *, gre_cloner);
108 #define V_gre_cloner VNET(gre_cloner)
110 static void gre_qflush(struct ifnet *);
111 static int gre_transmit(struct ifnet *, struct mbuf *);
112 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
113 static int gre_output(struct ifnet *, struct mbuf *,
114 const struct sockaddr *, struct route *);
115 static void gre_delete_tunnel(struct gre_softc *);
117 SYSCTL_DECL(_net_link);
118 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
119 "Generic Routing Encapsulation");
122 * This macro controls the default upper limitation on nesting of gre tunnels.
123 * Since, setting a large value to this macro with a careless configuration
124 * may introduce system crash, we don't allow any nestings by default.
125 * If you need to configure nested gre tunnels, you can define this macro
126 * in your kernel configuration file. However, if you do so, please be
127 * careful to configure the tunnels so that it won't make a loop.
129 #define MAX_GRE_NEST 1
132 VNET_DEFINE_STATIC(int, max_gre_nesting) = MAX_GRE_NEST;
133 #define V_max_gre_nesting VNET(max_gre_nesting)
134 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
135 &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels");
138 vnet_gre_init(const void *unused __unused)
141 V_gre_cloner = if_clone_simple(grename, gre_clone_create,
142 gre_clone_destroy, 0);
150 VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
151 vnet_gre_init, NULL);
154 vnet_gre_uninit(const void *unused __unused)
157 if_clone_detach(V_gre_cloner);
164 /* XXX: epoch_call drain */
166 VNET_SYSUNINIT(vnet_gre_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
167 vnet_gre_uninit, NULL);
170 gre_clone_create(struct if_clone *ifc, int unit, caddr_t params)
172 struct gre_softc *sc;
174 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
175 sc->gre_fibnum = curthread->td_proc->p_fibnum;
176 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
177 GRE2IFP(sc)->if_softc = sc;
178 if_initname(GRE2IFP(sc), grename, unit);
180 GRE2IFP(sc)->if_mtu = GREMTU;
181 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
182 GRE2IFP(sc)->if_output = gre_output;
183 GRE2IFP(sc)->if_ioctl = gre_ioctl;
184 GRE2IFP(sc)->if_transmit = gre_transmit;
185 GRE2IFP(sc)->if_qflush = gre_qflush;
186 GRE2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
187 GRE2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
188 if_attach(GRE2IFP(sc));
189 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
194 gre_clone_destroy(struct ifnet *ifp)
196 struct gre_softc *sc;
198 sx_xlock(&gre_ioctl_sx);
200 gre_delete_tunnel(sc);
203 ifp->if_softc = NULL;
204 sx_xunlock(&gre_ioctl_sx);
212 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
214 struct ifreq *ifr = (struct ifreq *)data;
215 struct gre_softc *sc;
222 if (ifr->ifr_mtu < 576)
224 ifp->if_mtu = ifr->ifr_mtu;
227 ifp->if_flags |= IFF_UP;
240 sx_xlock(&gre_ioctl_sx);
249 if (sc->gre_family == 0)
251 gre_delete_tunnel(sc);
255 case SIOCGIFPSRCADDR:
256 case SIOCGIFPDSTADDR:
257 error = in_gre_ioctl(sc, cmd, data);
261 case SIOCSIFPHYADDR_IN6:
262 case SIOCGIFPSRCADDR_IN6:
263 case SIOCGIFPDSTADDR_IN6:
264 error = in6_gre_ioctl(sc, cmd, data);
268 ifr->ifr_fib = sc->gre_fibnum;
271 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
273 if (ifr->ifr_fib >= rt_numfibs)
276 sc->gre_fibnum = ifr->ifr_fib;
281 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
283 if ((error = copyin(ifr_data_get_ptr(ifr), &opt,
286 if (cmd == GRESKEY) {
287 if (sc->gre_key == opt)
289 } else if (cmd == GRESOPTS) {
290 if (opt & ~GRE_OPTMASK) {
294 if (sc->gre_options == opt)
296 } else if (cmd == GRESPORT) {
297 if (opt != 0 && (opt < V_ipport_hifirstauto ||
298 opt > V_ipport_hilastauto)) {
302 if (sc->gre_port == opt)
304 if ((sc->gre_options & GRE_UDPENCAP) == 0) {
306 * UDP encapsulation is not enabled, thus
307 * there is no need to reattach softc.
313 switch (sc->gre_family) {
316 error = in_gre_setopts(sc, cmd, opt);
321 error = in6_gre_setopts(sc, cmd, opt);
326 * Tunnel is not yet configured.
327 * We can just change any parameters.
332 sc->gre_options = opt;
338 * XXX: Do we need to initiate change of interface
343 error = copyout(&sc->gre_key, ifr_data_get_ptr(ifr),
344 sizeof(sc->gre_key));
347 error = copyout(&sc->gre_options, ifr_data_get_ptr(ifr),
348 sizeof(sc->gre_options));
351 error = copyout(&sc->gre_port, ifr_data_get_ptr(ifr),
352 sizeof(sc->gre_port));
358 if (error == 0 && sc->gre_family != 0) {
361 cmd == SIOCSIFPHYADDR ||
364 cmd == SIOCSIFPHYADDR_IN6 ||
367 if_link_state_change(ifp, LINK_STATE_UP);
371 sx_xunlock(&gre_ioctl_sx);
376 gre_delete_tunnel(struct gre_softc *sc)
378 struct gre_socket *gs;
380 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
381 if (sc->gre_family != 0) {
382 CK_LIST_REMOVE(sc, chain);
383 CK_LIST_REMOVE(sc, srchash);
385 free(sc->gre_hdr, M_GRE);
389 * If this Tunnel was the last one that could use UDP socket,
390 * we should unlink socket from hash table and close it.
392 if ((gs = sc->gre_so) != NULL && CK_LIST_EMPTY(&gs->list)) {
393 CK_LIST_REMOVE(gs, chain);
395 epoch_call(net_epoch_preempt, &gs->epoch_ctx, gre_sofree);
398 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
399 if_link_state_change(GRE2IFP(sc), LINK_STATE_DOWN);
405 struct gre_list *hash;
408 hash = malloc(sizeof(struct gre_list) * GRE_HASH_SIZE,
410 for (i = 0; i < GRE_HASH_SIZE; i++)
411 CK_LIST_INIT(&hash[i]);
417 gre_hashdestroy(struct gre_list *hash)
424 gre_sofree(epoch_context_t ctx)
426 struct gre_socket *gs;
428 gs = __containerof(ctx, struct gre_socket, epoch_ctx);
432 static __inline uint16_t
433 gre_cksum_add(uint16_t sum, uint16_t a)
438 return (res + (res < a));
442 gre_update_udphdr(struct gre_softc *sc, struct udphdr *udp, uint16_t csum)
445 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
446 MPASS(sc->gre_options & GRE_UDPENCAP);
448 udp->uh_dport = htons(GRE_UDPPORT);
449 udp->uh_sport = htons(sc->gre_port);
455 gre_update_hdr(struct gre_softc *sc, struct grehdr *gh)
460 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
464 if (sc->gre_options & GRE_ENABLE_CSUM) {
465 flags |= GRE_FLAGS_CP;
466 sc->gre_hlen += 2 * sizeof(uint16_t);
469 if (sc->gre_key != 0) {
470 flags |= GRE_FLAGS_KP;
471 sc->gre_hlen += sizeof(uint32_t);
472 *opts++ = htonl(sc->gre_key);
474 if (sc->gre_options & GRE_ENABLE_SEQ) {
475 flags |= GRE_FLAGS_SP;
476 sc->gre_hlen += sizeof(uint32_t);
480 gh->gre_flags = htons(flags);
484 gre_input(struct mbuf *m, int off, int proto, void *arg)
486 struct gre_softc *sc = arg;
497 hlen = off + sizeof(struct grehdr) + 4 * sizeof(uint32_t);
498 if (m->m_pkthdr.len < hlen)
500 if (m->m_len < hlen) {
501 m = m_pullup(m, hlen);
505 gh = (struct grehdr *)mtodo(m, off);
506 flags = ntohs(gh->gre_flags);
507 if (flags & ~GRE_FLAGS_MASK)
510 hlen = 2 * sizeof(uint16_t);
511 if (flags & GRE_FLAGS_CP) {
512 /* reserved1 field must be zero */
513 if (((uint16_t *)opts)[1] != 0)
515 if (in_cksum_skip(m, m->m_pkthdr.len, off) != 0)
517 hlen += 2 * sizeof(uint16_t);
520 if (flags & GRE_FLAGS_KP) {
523 * XXX: The current implementation uses the key only for outgoing
524 * packets. But we can check the key value here, or even in the
525 * encapcheck function.
529 hlen += sizeof(uint32_t);
536 if (sc->gre_key != 0 && (key != sc->gre_key || key != 0))
539 if (flags & GRE_FLAGS_SP) {
543 hlen += sizeof(uint32_t);
545 switch (ntohs(gh->gre_proto)) {
548 * For WCCP skip an additional 4 bytes if after GRE header
549 * doesn't follow an IP header.
551 if (flags == 0 && (*(uint8_t *)gh->gre_opts & 0xF0) != 0x40)
552 hlen += sizeof(uint32_t);
565 m_adj(m, off + hlen);
567 m->m_pkthdr.rcvif = ifp;
568 M_SETFIB(m, ifp->if_fib);
570 mac_ifnet_create_mbuf(ifp, m);
572 BPF_MTAP2(ifp, &af, sizeof(af), m);
573 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
574 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
575 if ((ifp->if_flags & IFF_MONITOR) != 0)
578 netisr_dispatch(isr, m);
579 return (IPPROTO_DONE);
581 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
583 return (IPPROTO_DONE);
587 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
592 if (dst->sa_family == AF_UNSPEC)
593 bcopy(dst->sa_data, &af, sizeof(af));
597 * Now save the af in the inbound pkt csum data, this is a cheat since
598 * we are using the inbound csum_data field to carry the af over to
599 * the gre_transmit() routine, avoiding using yet another mtag.
601 m->m_pkthdr.csum_data = af;
602 return (ifp->if_transmit(ifp, m));
606 gre_setseqn(struct grehdr *gh, uint32_t seq)
612 flags = ntohs(gh->gre_flags);
613 KASSERT((flags & GRE_FLAGS_SP) != 0,
614 ("gre_setseqn called, but GRE_FLAGS_SP isn't set "));
615 if (flags & GRE_FLAGS_CP)
617 if (flags & GRE_FLAGS_KP)
623 gre_flowid(struct gre_softc *sc, struct mbuf *m, uint32_t af)
627 if ((sc->gre_options & GRE_UDPENCAP) == 0 || sc->gre_port != 0)
633 flowid = mtod(m, struct ip *)->ip_src.s_addr ^
634 mtod(m, struct ip *)->ip_dst.s_addr;
639 flowid = mtod(m, struct ip6_hdr *)->ip6_src.s6_addr32[3] ^
640 mtod(m, struct ip6_hdr *)->ip6_dst.s6_addr32[3];
650 flowid = rss_hash_ip4_2tuple(mtod(m, struct ip *)->ip_src,
651 mtod(m, struct ip *)->ip_dst);
656 flowid = rss_hash_ip6_2tuple(
657 &mtod(m, struct ip6_hdr *)->ip6_src,
658 &mtod(m, struct ip6_hdr *)->ip6_dst);
668 #define MTAG_GRE 1307983903
670 gre_transmit(struct ifnet *ifp, struct mbuf *m)
673 struct gre_softc *sc;
683 error = mac_ifnet_check_transmit(ifp, m);
691 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
692 (ifp->if_flags & IFF_UP) == 0 ||
693 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
694 sc->gre_family == 0 ||
695 (error = if_tunnel_check_nesting(ifp, m, MTAG_GRE,
696 V_max_gre_nesting)) != 0) {
700 af = m->m_pkthdr.csum_data;
701 BPF_MTAP2(ifp, &af, sizeof(af), m);
702 m->m_flags &= ~(M_BCAST|M_MCAST);
703 flowid = gre_flowid(sc, m, af);
704 M_SETFIB(m, sc->gre_fibnum);
705 M_PREPEND(m, sc->gre_hlen, M_NOWAIT);
710 bcopy(sc->gre_hdr, mtod(m, void *), sc->gre_hlen);
711 /* Determine GRE proto */
715 proto = htons(ETHERTYPE_IP);
720 proto = htons(ETHERTYPE_IPV6);
728 /* Determine offset of GRE header */
729 switch (sc->gre_family) {
732 len = sizeof(struct ip);
737 len = sizeof(struct ip6_hdr);
745 if (sc->gre_options & GRE_UDPENCAP) {
746 uh = (struct udphdr *)mtodo(m, len);
747 uh->uh_sport |= htons(V_ipport_hifirstauto) |
748 (flowid >> 16) | (flowid & 0xFFFF);
749 uh->uh_sport = htons(ntohs(uh->uh_sport) %
750 V_ipport_hilastauto);
751 uh->uh_ulen = htons(m->m_pkthdr.len - len);
752 uh->uh_sum = gre_cksum_add(uh->uh_sum,
753 htons(m->m_pkthdr.len - len + IPPROTO_UDP));
754 m->m_pkthdr.csum_flags = sc->gre_csumflags;
755 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
756 len += sizeof(struct udphdr);
758 gh = (struct grehdr *)mtodo(m, len);
759 gh->gre_proto = proto;
760 if (sc->gre_options & GRE_ENABLE_SEQ)
761 gre_setseqn(gh, sc->gre_oseq++);
762 if (sc->gre_options & GRE_ENABLE_CSUM) {
763 *(uint16_t *)gh->gre_opts = in_cksum_skip(m,
764 m->m_pkthdr.len, len);
766 len = m->m_pkthdr.len - len;
767 switch (sc->gre_family) {
770 error = in_gre_output(m, af, sc->gre_hlen);
775 error = in6_gre_output(m, af, sc->gre_hlen, flowid);
784 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
786 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
787 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
794 gre_qflush(struct ifnet *ifp __unused)
800 gremodevent(module_t mod, int type, void *data)
813 static moduledata_t gre_mod = {
819 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
820 MODULE_VERSION(if_gre, 1);