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)
111 static void gre_reassign(struct ifnet *, struct vnet *, char *);
113 static void gre_qflush(struct ifnet *);
114 static int gre_transmit(struct ifnet *, struct mbuf *);
115 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
116 static int gre_output(struct ifnet *, struct mbuf *,
117 const struct sockaddr *, struct route *);
118 static void gre_delete_tunnel(struct gre_softc *);
120 SYSCTL_DECL(_net_link);
121 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
122 "Generic Routing Encapsulation");
125 * This macro controls the default upper limitation on nesting of gre tunnels.
126 * Since, setting a large value to this macro with a careless configuration
127 * may introduce system crash, we don't allow any nestings by default.
128 * If you need to configure nested gre tunnels, you can define this macro
129 * in your kernel configuration file. However, if you do so, please be
130 * careful to configure the tunnels so that it won't make a loop.
132 #define MAX_GRE_NEST 1
135 VNET_DEFINE_STATIC(int, max_gre_nesting) = MAX_GRE_NEST;
136 #define V_max_gre_nesting VNET(max_gre_nesting)
137 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
138 &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels");
141 vnet_gre_init(const void *unused __unused)
144 V_gre_cloner = if_clone_simple(grename, gre_clone_create,
145 gre_clone_destroy, 0);
153 VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
154 vnet_gre_init, NULL);
157 vnet_gre_uninit(const void *unused __unused)
160 if_clone_detach(V_gre_cloner);
167 /* XXX: epoch_call drain */
169 VNET_SYSUNINIT(vnet_gre_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
170 vnet_gre_uninit, NULL);
173 gre_clone_create(struct if_clone *ifc, int unit, caddr_t params)
175 struct gre_softc *sc;
177 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
178 sc->gre_fibnum = curthread->td_proc->p_fibnum;
179 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
180 GRE2IFP(sc)->if_softc = sc;
181 if_initname(GRE2IFP(sc), grename, unit);
183 GRE2IFP(sc)->if_mtu = GREMTU;
184 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
185 GRE2IFP(sc)->if_output = gre_output;
186 GRE2IFP(sc)->if_ioctl = gre_ioctl;
187 GRE2IFP(sc)->if_transmit = gre_transmit;
188 GRE2IFP(sc)->if_qflush = gre_qflush;
190 GRE2IFP(sc)->if_reassign = gre_reassign;
192 GRE2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
193 GRE2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
194 if_attach(GRE2IFP(sc));
195 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
201 gre_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
202 char *unused __unused)
204 struct gre_softc *sc;
206 sx_xlock(&gre_ioctl_sx);
209 gre_delete_tunnel(sc);
210 sx_xunlock(&gre_ioctl_sx);
215 gre_clone_destroy(struct ifnet *ifp)
217 struct gre_softc *sc;
219 sx_xlock(&gre_ioctl_sx);
221 gre_delete_tunnel(sc);
224 ifp->if_softc = NULL;
225 sx_xunlock(&gre_ioctl_sx);
233 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
235 struct ifreq *ifr = (struct ifreq *)data;
236 struct gre_softc *sc;
243 if (ifr->ifr_mtu < 576)
245 ifp->if_mtu = ifr->ifr_mtu;
248 ifp->if_flags |= IFF_UP;
261 sx_xlock(&gre_ioctl_sx);
270 if (sc->gre_family == 0)
272 gre_delete_tunnel(sc);
276 case SIOCGIFPSRCADDR:
277 case SIOCGIFPDSTADDR:
278 error = in_gre_ioctl(sc, cmd, data);
282 case SIOCSIFPHYADDR_IN6:
283 case SIOCGIFPSRCADDR_IN6:
284 case SIOCGIFPDSTADDR_IN6:
285 error = in6_gre_ioctl(sc, cmd, data);
289 ifr->ifr_fib = sc->gre_fibnum;
292 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
294 if (ifr->ifr_fib >= rt_numfibs)
297 sc->gre_fibnum = ifr->ifr_fib;
302 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
304 if ((error = copyin(ifr_data_get_ptr(ifr), &opt,
307 if (cmd == GRESKEY) {
308 if (sc->gre_key == opt)
310 } else if (cmd == GRESOPTS) {
311 if (opt & ~GRE_OPTMASK) {
315 if (sc->gre_options == opt)
317 } else if (cmd == GRESPORT) {
318 if (opt != 0 && (opt < V_ipport_hifirstauto ||
319 opt > V_ipport_hilastauto)) {
323 if (sc->gre_port == opt)
325 if ((sc->gre_options & GRE_UDPENCAP) == 0) {
327 * UDP encapsulation is not enabled, thus
328 * there is no need to reattach softc.
334 switch (sc->gre_family) {
337 error = in_gre_setopts(sc, cmd, opt);
342 error = in6_gre_setopts(sc, cmd, opt);
347 * Tunnel is not yet configured.
348 * We can just change any parameters.
353 sc->gre_options = opt;
359 * XXX: Do we need to initiate change of interface
364 error = copyout(&sc->gre_key, ifr_data_get_ptr(ifr),
365 sizeof(sc->gre_key));
368 error = copyout(&sc->gre_options, ifr_data_get_ptr(ifr),
369 sizeof(sc->gre_options));
372 error = copyout(&sc->gre_port, ifr_data_get_ptr(ifr),
373 sizeof(sc->gre_port));
379 if (error == 0 && sc->gre_family != 0) {
382 cmd == SIOCSIFPHYADDR ||
385 cmd == SIOCSIFPHYADDR_IN6 ||
388 if_link_state_change(ifp, LINK_STATE_UP);
392 sx_xunlock(&gre_ioctl_sx);
397 gre_delete_tunnel(struct gre_softc *sc)
399 struct gre_socket *gs;
401 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
402 if (sc->gre_family != 0) {
403 CK_LIST_REMOVE(sc, chain);
404 CK_LIST_REMOVE(sc, srchash);
406 free(sc->gre_hdr, M_GRE);
410 * If this Tunnel was the last one that could use UDP socket,
411 * we should unlink socket from hash table and close it.
413 if ((gs = sc->gre_so) != NULL && CK_LIST_EMPTY(&gs->list)) {
414 CK_LIST_REMOVE(gs, chain);
416 NET_EPOCH_CALL(gre_sofree, &gs->epoch_ctx);
419 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
420 if_link_state_change(GRE2IFP(sc), LINK_STATE_DOWN);
426 struct gre_list *hash;
429 hash = malloc(sizeof(struct gre_list) * GRE_HASH_SIZE,
431 for (i = 0; i < GRE_HASH_SIZE; i++)
432 CK_LIST_INIT(&hash[i]);
438 gre_hashdestroy(struct gre_list *hash)
445 gre_sofree(epoch_context_t ctx)
447 struct gre_socket *gs;
449 gs = __containerof(ctx, struct gre_socket, epoch_ctx);
453 static __inline uint16_t
454 gre_cksum_add(uint16_t sum, uint16_t a)
459 return (res + (res < a));
463 gre_update_udphdr(struct gre_softc *sc, struct udphdr *udp, uint16_t csum)
466 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
467 MPASS(sc->gre_options & GRE_UDPENCAP);
469 udp->uh_dport = htons(GRE_UDPPORT);
470 udp->uh_sport = htons(sc->gre_port);
476 gre_update_hdr(struct gre_softc *sc, struct grehdr *gh)
481 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
485 if (sc->gre_options & GRE_ENABLE_CSUM) {
486 flags |= GRE_FLAGS_CP;
487 sc->gre_hlen += 2 * sizeof(uint16_t);
490 if (sc->gre_key != 0) {
491 flags |= GRE_FLAGS_KP;
492 sc->gre_hlen += sizeof(uint32_t);
493 *opts++ = htonl(sc->gre_key);
495 if (sc->gre_options & GRE_ENABLE_SEQ) {
496 flags |= GRE_FLAGS_SP;
497 sc->gre_hlen += sizeof(uint32_t);
501 gh->gre_flags = htons(flags);
505 gre_input(struct mbuf *m, int off, int proto, void *arg)
507 struct gre_softc *sc = arg;
518 hlen = off + sizeof(struct grehdr) + 4 * sizeof(uint32_t);
519 if (m->m_pkthdr.len < hlen)
521 if (m->m_len < hlen) {
522 m = m_pullup(m, hlen);
526 gh = (struct grehdr *)mtodo(m, off);
527 flags = ntohs(gh->gre_flags);
528 if (flags & ~GRE_FLAGS_MASK)
531 hlen = 2 * sizeof(uint16_t);
532 if (flags & GRE_FLAGS_CP) {
533 /* reserved1 field must be zero */
534 if (((uint16_t *)opts)[1] != 0)
536 if (in_cksum_skip(m, m->m_pkthdr.len, off) != 0)
538 hlen += 2 * sizeof(uint16_t);
541 if (flags & GRE_FLAGS_KP) {
544 * XXX: The current implementation uses the key only for outgoing
545 * packets. But we can check the key value here, or even in the
546 * encapcheck function.
550 hlen += sizeof(uint32_t);
557 if (sc->gre_key != 0 && (key != sc->gre_key || key != 0))
560 if (flags & GRE_FLAGS_SP) {
564 hlen += sizeof(uint32_t);
566 switch (ntohs(gh->gre_proto)) {
569 * For WCCP skip an additional 4 bytes if after GRE header
570 * doesn't follow an IP header.
572 if (flags == 0 && (*(uint8_t *)gh->gre_opts & 0xF0) != 0x40)
573 hlen += sizeof(uint32_t);
586 m_adj(m, off + hlen);
588 m->m_pkthdr.rcvif = ifp;
589 M_SETFIB(m, ifp->if_fib);
591 mac_ifnet_create_mbuf(ifp, m);
593 BPF_MTAP2(ifp, &af, sizeof(af), m);
594 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
595 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
596 if ((ifp->if_flags & IFF_MONITOR) != 0)
599 netisr_dispatch(isr, m);
600 return (IPPROTO_DONE);
602 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
604 return (IPPROTO_DONE);
608 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
613 if (dst->sa_family == AF_UNSPEC)
614 bcopy(dst->sa_data, &af, sizeof(af));
618 * Now save the af in the inbound pkt csum data, this is a cheat since
619 * we are using the inbound csum_data field to carry the af over to
620 * the gre_transmit() routine, avoiding using yet another mtag.
622 m->m_pkthdr.csum_data = af;
623 return (ifp->if_transmit(ifp, m));
627 gre_setseqn(struct grehdr *gh, uint32_t seq)
633 flags = ntohs(gh->gre_flags);
634 KASSERT((flags & GRE_FLAGS_SP) != 0,
635 ("gre_setseqn called, but GRE_FLAGS_SP isn't set "));
636 if (flags & GRE_FLAGS_CP)
638 if (flags & GRE_FLAGS_KP)
644 gre_flowid(struct gre_softc *sc, struct mbuf *m, uint32_t af)
648 if ((sc->gre_options & GRE_UDPENCAP) == 0 || sc->gre_port != 0)
654 flowid = mtod(m, struct ip *)->ip_src.s_addr ^
655 mtod(m, struct ip *)->ip_dst.s_addr;
660 flowid = mtod(m, struct ip6_hdr *)->ip6_src.s6_addr32[3] ^
661 mtod(m, struct ip6_hdr *)->ip6_dst.s6_addr32[3];
671 flowid = rss_hash_ip4_2tuple(mtod(m, struct ip *)->ip_src,
672 mtod(m, struct ip *)->ip_dst);
677 flowid = rss_hash_ip6_2tuple(
678 &mtod(m, struct ip6_hdr *)->ip6_src,
679 &mtod(m, struct ip6_hdr *)->ip6_dst);
689 #define MTAG_GRE 1307983903
691 gre_transmit(struct ifnet *ifp, struct mbuf *m)
694 struct gre_softc *sc;
704 error = mac_ifnet_check_transmit(ifp, m);
712 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
713 (ifp->if_flags & IFF_UP) == 0 ||
714 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
715 sc->gre_family == 0 ||
716 (error = if_tunnel_check_nesting(ifp, m, MTAG_GRE,
717 V_max_gre_nesting)) != 0) {
721 af = m->m_pkthdr.csum_data;
722 BPF_MTAP2(ifp, &af, sizeof(af), m);
723 m->m_flags &= ~(M_BCAST|M_MCAST);
724 flowid = gre_flowid(sc, m, af);
725 M_SETFIB(m, sc->gre_fibnum);
726 M_PREPEND(m, sc->gre_hlen, M_NOWAIT);
731 bcopy(sc->gre_hdr, mtod(m, void *), sc->gre_hlen);
732 /* Determine GRE proto */
736 proto = htons(ETHERTYPE_IP);
741 proto = htons(ETHERTYPE_IPV6);
749 /* Determine offset of GRE header */
750 switch (sc->gre_family) {
753 len = sizeof(struct ip);
758 len = sizeof(struct ip6_hdr);
766 if (sc->gre_options & GRE_UDPENCAP) {
767 uh = (struct udphdr *)mtodo(m, len);
768 uh->uh_sport |= htons(V_ipport_hifirstauto) |
769 (flowid >> 16) | (flowid & 0xFFFF);
770 uh->uh_sport = htons(ntohs(uh->uh_sport) %
771 V_ipport_hilastauto);
772 uh->uh_ulen = htons(m->m_pkthdr.len - len);
773 uh->uh_sum = gre_cksum_add(uh->uh_sum,
774 htons(m->m_pkthdr.len - len + IPPROTO_UDP));
775 m->m_pkthdr.csum_flags = sc->gre_csumflags;
776 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
777 len += sizeof(struct udphdr);
779 gh = (struct grehdr *)mtodo(m, len);
780 gh->gre_proto = proto;
781 if (sc->gre_options & GRE_ENABLE_SEQ)
782 gre_setseqn(gh, sc->gre_oseq++);
783 if (sc->gre_options & GRE_ENABLE_CSUM) {
784 *(uint16_t *)gh->gre_opts = in_cksum_skip(m,
785 m->m_pkthdr.len, len);
787 len = m->m_pkthdr.len - len;
788 switch (sc->gre_family) {
791 error = in_gre_output(m, af, sc->gre_hlen);
796 error = in6_gre_output(m, af, sc->gre_hlen, flowid);
805 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
807 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
808 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
815 gre_qflush(struct ifnet *ifp __unused)
821 gremodevent(module_t mod, int type, void *data)
834 static moduledata_t gre_mod = {
840 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
841 MODULE_VERSION(if_gre, 1);