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>
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_private.h>
64 #include <net/if_clone.h>
65 #include <net/if_types.h>
66 #include <net/netisr.h>
68 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <netinet/in_pcb.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_var.h>
77 #include <netinet/in_rss.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/in6_var.h>
84 #include <netinet6/ip6_var.h>
86 #include <netinet6/in6_rss.h>
90 #include <netinet/ip_encap.h>
91 #include <netinet/udp.h>
93 #include <net/if_gre.h>
95 #include <machine/in_cksum.h>
96 #include <security/mac/mac_framework.h>
100 static const char grename[] = "gre";
101 MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation");
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 VNET_DEFINE_STATIC(struct if_clone *, gre_cloner);
109 #define V_gre_cloner VNET(gre_cloner)
112 static void gre_reassign(struct ifnet *, struct vnet *, char *);
114 static void gre_qflush(struct ifnet *);
115 static int gre_transmit(struct ifnet *, struct mbuf *);
116 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
117 static int gre_output(struct ifnet *, struct mbuf *,
118 const struct sockaddr *, struct route *);
119 static void gre_delete_tunnel(struct gre_softc *);
121 SYSCTL_DECL(_net_link);
122 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
123 "Generic Routing Encapsulation");
126 * This macro controls the default upper limitation on nesting of gre tunnels.
127 * Since, setting a large value to this macro with a careless configuration
128 * may introduce system crash, we don't allow any nestings by default.
129 * If you need to configure nested gre tunnels, you can define this macro
130 * in your kernel configuration file. However, if you do so, please be
131 * careful to configure the tunnels so that it won't make a loop.
133 #define MAX_GRE_NEST 1
136 VNET_DEFINE_STATIC(int, max_gre_nesting) = MAX_GRE_NEST;
137 #define V_max_gre_nesting VNET(max_gre_nesting)
138 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
139 &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels");
142 vnet_gre_init(const void *unused __unused)
145 V_gre_cloner = if_clone_simple(grename, gre_clone_create,
146 gre_clone_destroy, 0);
154 VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
155 vnet_gre_init, NULL);
158 vnet_gre_uninit(const void *unused __unused)
161 if_clone_detach(V_gre_cloner);
168 /* XXX: epoch_call drain */
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);
181 GRE2IFP(sc)->if_softc = sc;
182 if_initname(GRE2IFP(sc), grename, unit);
184 GRE2IFP(sc)->if_mtu = GREMTU;
185 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
186 GRE2IFP(sc)->if_output = gre_output;
187 GRE2IFP(sc)->if_ioctl = gre_ioctl;
188 GRE2IFP(sc)->if_transmit = gre_transmit;
189 GRE2IFP(sc)->if_qflush = gre_qflush;
191 GRE2IFP(sc)->if_reassign = gre_reassign;
193 GRE2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
194 GRE2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
195 if_attach(GRE2IFP(sc));
196 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
202 gre_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
203 char *unused __unused)
205 struct gre_softc *sc;
207 sx_xlock(&gre_ioctl_sx);
210 gre_delete_tunnel(sc);
211 sx_xunlock(&gre_ioctl_sx);
216 gre_clone_destroy(struct ifnet *ifp)
218 struct gre_softc *sc;
220 sx_xlock(&gre_ioctl_sx);
222 gre_delete_tunnel(sc);
225 ifp->if_softc = NULL;
226 sx_xunlock(&gre_ioctl_sx);
234 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
236 struct ifreq *ifr = (struct ifreq *)data;
237 struct gre_softc *sc;
244 if (ifr->ifr_mtu < 576)
246 ifp->if_mtu = ifr->ifr_mtu;
249 ifp->if_flags |= IFF_UP;
262 sx_xlock(&gre_ioctl_sx);
271 if (sc->gre_family == 0)
273 gre_delete_tunnel(sc);
277 case SIOCGIFPSRCADDR:
278 case SIOCGIFPDSTADDR:
279 error = in_gre_ioctl(sc, cmd, data);
283 case SIOCSIFPHYADDR_IN6:
284 case SIOCGIFPSRCADDR_IN6:
285 case SIOCGIFPDSTADDR_IN6:
286 error = in6_gre_ioctl(sc, cmd, data);
290 ifr->ifr_fib = sc->gre_fibnum;
293 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
295 if (ifr->ifr_fib >= rt_numfibs)
298 sc->gre_fibnum = ifr->ifr_fib;
303 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
305 if ((error = copyin(ifr_data_get_ptr(ifr), &opt,
308 if (cmd == GRESKEY) {
309 if (sc->gre_key == opt)
311 } else if (cmd == GRESOPTS) {
312 if (opt & ~GRE_OPTMASK) {
316 if (sc->gre_options == opt)
318 } else if (cmd == GRESPORT) {
319 if (opt != 0 && (opt < V_ipport_hifirstauto ||
320 opt > V_ipport_hilastauto)) {
324 if (sc->gre_port == opt)
326 if ((sc->gre_options & GRE_UDPENCAP) == 0) {
328 * UDP encapsulation is not enabled, thus
329 * there is no need to reattach softc.
335 switch (sc->gre_family) {
338 error = in_gre_setopts(sc, cmd, opt);
343 error = in6_gre_setopts(sc, cmd, opt);
348 * Tunnel is not yet configured.
349 * We can just change any parameters.
354 sc->gre_options = opt;
360 * XXX: Do we need to initiate change of interface
365 error = copyout(&sc->gre_key, ifr_data_get_ptr(ifr),
366 sizeof(sc->gre_key));
369 error = copyout(&sc->gre_options, ifr_data_get_ptr(ifr),
370 sizeof(sc->gre_options));
373 error = copyout(&sc->gre_port, ifr_data_get_ptr(ifr),
374 sizeof(sc->gre_port));
380 if (error == 0 && sc->gre_family != 0) {
383 cmd == SIOCSIFPHYADDR ||
386 cmd == SIOCSIFPHYADDR_IN6 ||
389 if_link_state_change(ifp, LINK_STATE_UP);
393 sx_xunlock(&gre_ioctl_sx);
398 gre_delete_tunnel(struct gre_softc *sc)
400 struct gre_socket *gs;
402 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
403 if (sc->gre_family != 0) {
404 CK_LIST_REMOVE(sc, chain);
405 CK_LIST_REMOVE(sc, srchash);
407 free(sc->gre_hdr, M_GRE);
411 * If this Tunnel was the last one that could use UDP socket,
412 * we should unlink socket from hash table and close it.
414 if ((gs = sc->gre_so) != NULL && CK_LIST_EMPTY(&gs->list)) {
415 CK_LIST_REMOVE(gs, chain);
417 NET_EPOCH_CALL(gre_sofree, &gs->epoch_ctx);
420 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
421 if_link_state_change(GRE2IFP(sc), LINK_STATE_DOWN);
427 struct gre_list *hash;
430 hash = malloc(sizeof(struct gre_list) * GRE_HASH_SIZE,
432 for (i = 0; i < GRE_HASH_SIZE; i++)
433 CK_LIST_INIT(&hash[i]);
439 gre_hashdestroy(struct gre_list *hash)
446 gre_sofree(epoch_context_t ctx)
448 struct gre_socket *gs;
450 gs = __containerof(ctx, struct gre_socket, epoch_ctx);
454 static __inline uint16_t
455 gre_cksum_add(uint16_t sum, uint16_t a)
460 return (res + (res < a));
464 gre_update_udphdr(struct gre_softc *sc, struct udphdr *udp, uint16_t csum)
467 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
468 MPASS(sc->gre_options & GRE_UDPENCAP);
470 udp->uh_dport = htons(GRE_UDPPORT);
471 udp->uh_sport = htons(sc->gre_port);
477 gre_update_hdr(struct gre_softc *sc, struct grehdr *gh)
482 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
486 if (sc->gre_options & GRE_ENABLE_CSUM) {
487 flags |= GRE_FLAGS_CP;
488 sc->gre_hlen += 2 * sizeof(uint16_t);
491 if (sc->gre_key != 0) {
492 flags |= GRE_FLAGS_KP;
493 sc->gre_hlen += sizeof(uint32_t);
494 *opts++ = htonl(sc->gre_key);
496 if (sc->gre_options & GRE_ENABLE_SEQ) {
497 flags |= GRE_FLAGS_SP;
498 sc->gre_hlen += sizeof(uint32_t);
502 gh->gre_flags = htons(flags);
506 gre_input(struct mbuf *m, int off, int proto, void *arg)
508 struct gre_softc *sc = arg;
519 hlen = off + sizeof(struct grehdr) + 4 * sizeof(uint32_t);
520 if (m->m_pkthdr.len < hlen)
522 if (m->m_len < hlen) {
523 m = m_pullup(m, hlen);
527 gh = (struct grehdr *)mtodo(m, off);
528 flags = ntohs(gh->gre_flags);
529 if (flags & ~GRE_FLAGS_MASK)
532 hlen = 2 * sizeof(uint16_t);
533 if (flags & GRE_FLAGS_CP) {
534 /* reserved1 field must be zero */
535 if (((uint16_t *)opts)[1] != 0)
537 if (in_cksum_skip(m, m->m_pkthdr.len, off) != 0)
539 hlen += 2 * sizeof(uint16_t);
542 if (flags & GRE_FLAGS_KP) {
545 * XXX: The current implementation uses the key only for outgoing
546 * packets. But we can check the key value here, or even in the
547 * encapcheck function.
551 hlen += sizeof(uint32_t);
558 if (sc->gre_key != 0 && (key != sc->gre_key || key != 0))
561 if (flags & GRE_FLAGS_SP) {
565 hlen += sizeof(uint32_t);
567 switch (ntohs(gh->gre_proto)) {
570 * For WCCP skip an additional 4 bytes if after GRE header
571 * doesn't follow an IP header.
573 if (flags == 0 && (*(uint8_t *)gh->gre_opts & 0xF0) != 0x40)
574 hlen += sizeof(uint32_t);
587 m_adj(m, off + hlen);
589 m->m_pkthdr.rcvif = ifp;
590 M_SETFIB(m, ifp->if_fib);
592 mac_ifnet_create_mbuf(ifp, m);
594 BPF_MTAP2(ifp, &af, sizeof(af), m);
595 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
596 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
597 if ((ifp->if_flags & IFF_MONITOR) != 0)
600 netisr_dispatch(isr, m);
601 return (IPPROTO_DONE);
603 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
605 return (IPPROTO_DONE);
609 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
614 if (dst->sa_family == AF_UNSPEC)
615 bcopy(dst->sa_data, &af, sizeof(af));
617 af = RO_GET_FAMILY(ro, dst);
619 * Now save the af in the inbound pkt csum data, this is a cheat since
620 * we are using the inbound csum_data field to carry the af over to
621 * the gre_transmit() routine, avoiding using yet another mtag.
623 m->m_pkthdr.csum_data = af;
624 return (ifp->if_transmit(ifp, m));
628 gre_setseqn(struct grehdr *gh, uint32_t seq)
634 flags = ntohs(gh->gre_flags);
635 KASSERT((flags & GRE_FLAGS_SP) != 0,
636 ("gre_setseqn called, but GRE_FLAGS_SP isn't set "));
637 if (flags & GRE_FLAGS_CP)
639 if (flags & GRE_FLAGS_KP)
645 gre_flowid(struct gre_softc *sc, struct mbuf *m, uint32_t af)
649 if ((sc->gre_options & GRE_UDPENCAP) == 0 || sc->gre_port != 0)
655 flowid = rss_hash_ip4_2tuple(mtod(m, struct ip *)->ip_src,
656 mtod(m, struct ip *)->ip_dst);
659 flowid = mtod(m, struct ip *)->ip_src.s_addr ^
660 mtod(m, struct ip *)->ip_dst.s_addr;
666 flowid = rss_hash_ip6_2tuple(
667 &mtod(m, struct ip6_hdr *)->ip6_src,
668 &mtod(m, struct ip6_hdr *)->ip6_dst);
671 flowid = mtod(m, struct ip6_hdr *)->ip6_src.s6_addr32[3] ^
672 mtod(m, struct ip6_hdr *)->ip6_dst.s6_addr32[3];
681 #define MTAG_GRE 1307983903
683 gre_transmit(struct ifnet *ifp, struct mbuf *m)
686 struct gre_softc *sc;
696 error = mac_ifnet_check_transmit(ifp, m);
704 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
705 (ifp->if_flags & IFF_UP) == 0 ||
706 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
707 sc->gre_family == 0 ||
708 (error = if_tunnel_check_nesting(ifp, m, MTAG_GRE,
709 V_max_gre_nesting)) != 0) {
713 af = m->m_pkthdr.csum_data;
714 BPF_MTAP2(ifp, &af, sizeof(af), m);
715 m->m_flags &= ~(M_BCAST|M_MCAST);
716 flowid = gre_flowid(sc, m, af);
717 M_SETFIB(m, sc->gre_fibnum);
718 M_PREPEND(m, sc->gre_hlen, M_NOWAIT);
723 bcopy(sc->gre_hdr, mtod(m, void *), sc->gre_hlen);
724 /* Determine GRE proto */
728 proto = htons(ETHERTYPE_IP);
733 proto = htons(ETHERTYPE_IPV6);
741 /* Determine offset of GRE header */
742 switch (sc->gre_family) {
745 len = sizeof(struct ip);
750 len = sizeof(struct ip6_hdr);
758 if (sc->gre_options & GRE_UDPENCAP) {
759 uh = (struct udphdr *)mtodo(m, len);
760 uh->uh_sport |= htons(V_ipport_hifirstauto) |
761 (flowid >> 16) | (flowid & 0xFFFF);
762 uh->uh_sport = htons(ntohs(uh->uh_sport) %
763 V_ipport_hilastauto);
764 uh->uh_ulen = htons(m->m_pkthdr.len - len);
765 uh->uh_sum = gre_cksum_add(uh->uh_sum,
766 htons(m->m_pkthdr.len - len + IPPROTO_UDP));
767 m->m_pkthdr.csum_flags = sc->gre_csumflags;
768 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
769 len += sizeof(struct udphdr);
771 gh = (struct grehdr *)mtodo(m, len);
772 gh->gre_proto = proto;
773 if (sc->gre_options & GRE_ENABLE_SEQ)
774 gre_setseqn(gh, sc->gre_oseq++);
775 if (sc->gre_options & GRE_ENABLE_CSUM) {
776 *(uint16_t *)gh->gre_opts = in_cksum_skip(m,
777 m->m_pkthdr.len, len);
779 len = m->m_pkthdr.len - len;
780 switch (sc->gre_family) {
783 error = in_gre_output(m, af, sc->gre_hlen);
788 error = in6_gre_output(m, af, sc->gre_hlen, flowid);
797 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
799 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
800 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
807 gre_qflush(struct ifnet *ifp __unused)
813 gremodevent(module_t mod, int type, void *data)
826 static moduledata_t gre_mod = {
832 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
833 MODULE_VERSION(if_gre, 1);