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"
43 #include <sys/param.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
51 #include <sys/socket.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_clone.h>
62 #include <net/if_types.h>
63 #include <net/netisr.h>
65 #include <net/route.h>
67 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/in6_var.h>
77 #include <netinet6/ip6_var.h>
80 #include <netinet/ip_encap.h>
82 #include <net/if_gre.h>
84 #include <machine/in_cksum.h>
85 #include <security/mac/mac_framework.h>
89 static const char grename[] = "gre";
90 MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation");
92 static struct sx gre_ioctl_sx;
93 SX_SYSINIT(gre_ioctl_sx, &gre_ioctl_sx, "gre_ioctl");
95 static int gre_clone_create(struct if_clone *, int, caddr_t);
96 static void gre_clone_destroy(struct ifnet *);
97 static VNET_DEFINE(struct if_clone *, gre_cloner);
98 #define V_gre_cloner VNET(gre_cloner)
100 static void gre_qflush(struct ifnet *);
101 static int gre_transmit(struct ifnet *, struct mbuf *);
102 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
103 static int gre_output(struct ifnet *, struct mbuf *,
104 const struct sockaddr *, struct route *);
105 static void gre_delete_tunnel(struct gre_softc *);
107 SYSCTL_DECL(_net_link);
108 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
109 "Generic Routing Encapsulation");
112 * This macro controls the default upper limitation on nesting of gre tunnels.
113 * Since, setting a large value to this macro with a careless configuration
114 * may introduce system crash, we don't allow any nestings by default.
115 * If you need to configure nested gre tunnels, you can define this macro
116 * in your kernel configuration file. However, if you do so, please be
117 * careful to configure the tunnels so that it won't make a loop.
119 #define MAX_GRE_NEST 1
122 static VNET_DEFINE(int, max_gre_nesting) = MAX_GRE_NEST;
123 #define V_max_gre_nesting VNET(max_gre_nesting)
124 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
125 &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels");
128 vnet_gre_init(const void *unused __unused)
131 V_gre_cloner = if_clone_simple(grename, gre_clone_create,
132 gre_clone_destroy, 0);
140 VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
141 vnet_gre_init, NULL);
144 vnet_gre_uninit(const void *unused __unused)
147 if_clone_detach(V_gre_cloner);
155 VNET_SYSUNINIT(vnet_gre_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
156 vnet_gre_uninit, NULL);
159 gre_clone_create(struct if_clone *ifc, int unit, caddr_t params)
161 struct gre_softc *sc;
163 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
164 sc->gre_fibnum = curthread->td_proc->p_fibnum;
165 GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
166 GRE2IFP(sc)->if_softc = sc;
167 if_initname(GRE2IFP(sc), grename, unit);
169 GRE2IFP(sc)->if_mtu = GREMTU;
170 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
171 GRE2IFP(sc)->if_output = gre_output;
172 GRE2IFP(sc)->if_ioctl = gre_ioctl;
173 GRE2IFP(sc)->if_transmit = gre_transmit;
174 GRE2IFP(sc)->if_qflush = gre_qflush;
175 GRE2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
176 GRE2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
177 if_attach(GRE2IFP(sc));
178 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
183 gre_clone_destroy(struct ifnet *ifp)
185 struct gre_softc *sc;
187 sx_xlock(&gre_ioctl_sx);
189 gre_delete_tunnel(sc);
192 ifp->if_softc = NULL;
193 sx_xunlock(&gre_ioctl_sx);
201 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
203 struct ifreq *ifr = (struct ifreq *)data;
204 struct gre_softc *sc;
211 if (ifr->ifr_mtu < 576)
213 ifp->if_mtu = ifr->ifr_mtu;
216 ifp->if_flags |= IFF_UP;
229 sx_xlock(&gre_ioctl_sx);
238 if (sc->gre_family == 0)
240 gre_delete_tunnel(sc);
244 case SIOCGIFPSRCADDR:
245 case SIOCGIFPDSTADDR:
246 error = in_gre_ioctl(sc, cmd, data);
250 case SIOCSIFPHYADDR_IN6:
251 case SIOCGIFPSRCADDR_IN6:
252 case SIOCGIFPDSTADDR_IN6:
253 error = in6_gre_ioctl(sc, cmd, data);
257 ifr->ifr_fib = sc->gre_fibnum;
260 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
262 if (ifr->ifr_fib >= rt_numfibs)
265 sc->gre_fibnum = ifr->ifr_fib;
269 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
271 if ((error = copyin(ifr_data_get_ptr(ifr), &opt,
274 if (cmd == GRESKEY) {
275 if (sc->gre_key == opt)
277 } else if (cmd == GRESOPTS) {
278 if (opt & ~GRE_OPTMASK) {
282 if (sc->gre_options == opt)
285 switch (sc->gre_family) {
288 in_gre_setopts(sc, cmd, opt);
293 in6_gre_setopts(sc, cmd, opt);
300 sc->gre_options = opt;
304 * XXX: Do we need to initiate change of interface
309 error = copyout(&sc->gre_key, ifr_data_get_ptr(ifr),
310 sizeof(sc->gre_key));
313 error = copyout(&sc->gre_options, ifr_data_get_ptr(ifr),
314 sizeof(sc->gre_options));
320 if (error == 0 && sc->gre_family != 0) {
323 cmd == SIOCSIFPHYADDR ||
326 cmd == SIOCSIFPHYADDR_IN6 ||
329 ifp->if_drv_flags |= IFF_DRV_RUNNING;
330 if_link_state_change(ifp, LINK_STATE_UP);
334 sx_xunlock(&gre_ioctl_sx);
339 gre_delete_tunnel(struct gre_softc *sc)
342 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
343 if (sc->gre_family != 0) {
344 CK_LIST_REMOVE(sc, chain);
346 free(sc->gre_hdr, M_GRE);
349 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
350 if_link_state_change(GRE2IFP(sc), LINK_STATE_DOWN);
356 struct gre_list *hash;
359 hash = malloc(sizeof(struct gre_list) * GRE_HASH_SIZE,
361 for (i = 0; i < GRE_HASH_SIZE; i++)
362 CK_LIST_INIT(&hash[i]);
368 gre_hashdestroy(struct gre_list *hash)
375 gre_updatehdr(struct gre_softc *sc, struct grehdr *gh)
380 sx_assert(&gre_ioctl_sx, SA_XLOCKED);
384 if (sc->gre_options & GRE_ENABLE_CSUM) {
385 flags |= GRE_FLAGS_CP;
386 sc->gre_hlen += 2 * sizeof(uint16_t);
389 if (sc->gre_key != 0) {
390 flags |= GRE_FLAGS_KP;
391 sc->gre_hlen += sizeof(uint32_t);
392 *opts++ = htonl(sc->gre_key);
394 if (sc->gre_options & GRE_ENABLE_SEQ) {
395 flags |= GRE_FLAGS_SP;
396 sc->gre_hlen += sizeof(uint32_t);
400 gh->gre_flags = htons(flags);
404 gre_input(struct mbuf *m, int off, int proto, void *arg)
406 struct gre_softc *sc = arg;
417 hlen = off + sizeof(struct grehdr) + 4 * sizeof(uint32_t);
418 if (m->m_pkthdr.len < hlen)
420 if (m->m_len < hlen) {
421 m = m_pullup(m, hlen);
425 gh = (struct grehdr *)mtodo(m, off);
426 flags = ntohs(gh->gre_flags);
427 if (flags & ~GRE_FLAGS_MASK)
430 hlen = 2 * sizeof(uint16_t);
431 if (flags & GRE_FLAGS_CP) {
432 /* reserved1 field must be zero */
433 if (((uint16_t *)opts)[1] != 0)
435 if (in_cksum_skip(m, m->m_pkthdr.len, off) != 0)
437 hlen += 2 * sizeof(uint16_t);
440 if (flags & GRE_FLAGS_KP) {
443 * XXX: The current implementation uses the key only for outgoing
444 * packets. But we can check the key value here, or even in the
445 * encapcheck function.
449 hlen += sizeof(uint32_t);
456 if (sc->gre_key != 0 && (key != sc->gre_key || key != 0))
459 if (flags & GRE_FLAGS_SP) {
463 hlen += sizeof(uint32_t);
465 switch (ntohs(gh->gre_proto)) {
468 * For WCCP skip an additional 4 bytes if after GRE header
469 * doesn't follow an IP header.
471 if (flags == 0 && (*(uint8_t *)gh->gre_opts & 0xF0) != 0x40)
472 hlen += sizeof(uint32_t);
485 m_adj(m, off + hlen);
487 m->m_pkthdr.rcvif = ifp;
488 M_SETFIB(m, ifp->if_fib);
490 mac_ifnet_create_mbuf(ifp, m);
492 BPF_MTAP2(ifp, &af, sizeof(af), m);
493 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
494 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
495 if ((ifp->if_flags & IFF_MONITOR) != 0)
498 netisr_dispatch(isr, m);
499 return (IPPROTO_DONE);
501 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
503 return (IPPROTO_DONE);
506 #define MTAG_GRE 1307983903
508 gre_check_nesting(struct ifnet *ifp, struct mbuf *m)
515 while ((mtag = m_tag_locate(m, MTAG_GRE, 0, mtag)) != NULL) {
516 if (*(struct ifnet **)(mtag + 1) == ifp) {
517 log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname);
522 if (count > V_max_gre_nesting) {
524 "%s: if_output recursively called too many times(%d)\n",
525 ifp->if_xname, count);
528 mtag = m_tag_alloc(MTAG_GRE, 0, sizeof(struct ifnet *), M_NOWAIT);
531 *(struct ifnet **)(mtag + 1) = ifp;
532 m_tag_prepend(m, mtag);
537 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
542 if (dst->sa_family == AF_UNSPEC)
543 bcopy(dst->sa_data, &af, sizeof(af));
547 * Now save the af in the inbound pkt csum data, this is a cheat since
548 * we are using the inbound csum_data field to carry the af over to
549 * the gre_transmit() routine, avoiding using yet another mtag.
551 m->m_pkthdr.csum_data = af;
552 return (ifp->if_transmit(ifp, m));
556 gre_setseqn(struct grehdr *gh, uint32_t seq)
562 flags = ntohs(gh->gre_flags);
563 KASSERT((flags & GRE_FLAGS_SP) != 0,
564 ("gre_setseqn called, but GRE_FLAGS_SP isn't set "));
565 if (flags & GRE_FLAGS_CP)
567 if (flags & GRE_FLAGS_KP)
573 gre_transmit(struct ifnet *ifp, struct mbuf *m)
575 struct gre_softc *sc;
583 error = mac_ifnet_check_transmit(ifp, m);
592 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
593 (ifp->if_flags & IFF_UP) == 0 ||
594 sc->gre_family == 0 ||
595 (error = gre_check_nesting(ifp, m)) != 0) {
599 af = m->m_pkthdr.csum_data;
600 M_SETFIB(m, sc->gre_fibnum);
601 M_PREPEND(m, sc->gre_hlen, M_NOWAIT);
606 bcopy(sc->gre_hdr, mtod(m, void *), sc->gre_hlen);
607 /* Determine GRE proto */
611 proto = htons(ETHERTYPE_IP);
616 proto = htons(ETHERTYPE_IPV6);
624 /* Determine offset of GRE header */
625 switch (sc->gre_family) {
628 len = sizeof(struct ip);
633 len = sizeof(struct ip6_hdr);
641 gh = (struct grehdr *)mtodo(m, len);
642 gh->gre_proto = proto;
643 if (sc->gre_options & GRE_ENABLE_SEQ)
644 gre_setseqn(gh, sc->gre_oseq++);
645 if (sc->gre_options & GRE_ENABLE_CSUM) {
646 *(uint16_t *)gh->gre_opts = in_cksum_skip(m,
647 m->m_pkthdr.len, len);
649 len = m->m_pkthdr.len - len;
650 switch (sc->gre_family) {
653 error = in_gre_output(m, af, sc->gre_hlen);
658 error = in6_gre_output(m, af, sc->gre_hlen);
667 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
669 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
670 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
677 gre_qflush(struct ifnet *ifp __unused)
683 gremodevent(module_t mod, int type, void *data)
696 static moduledata_t gre_mod = {
702 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
703 MODULE_VERSION(if_gre, 1);