2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
3 * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org>
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Heiko W.Rupp <hwr@pilhuhn.de>
9 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 * $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/protosw.h>
47 #include <sys/errno.h>
49 #include <sys/kernel.h>
51 #include <sys/rmlock.h>
52 #include <sys/sysctl.h>
53 #include <net/ethernet.h>
55 #include <net/if_var.h>
57 #include <net/raw_cb.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_encap.h>
64 #include <netinet/ip_var.h>
65 #include <machine/in_cksum.h>
68 #include <netinet/ip6.h>
71 /* Needs IP headers. */
72 #include <net/if_gre.h>
73 #include <machine/stdarg.h>
75 extern struct domain inetdomain;
76 static void gre_input10(struct mbuf *, int);
77 static const struct protosw in_gre_protosw = {
79 .pr_domain = &inetdomain,
80 .pr_protocol = IPPROTO_GRE,
81 .pr_flags = PR_ATOMIC|PR_ADDR,
82 .pr_input = gre_input10,
83 .pr_output = (pr_output_t *)rip_output,
84 .pr_ctlinput = rip_ctlinput,
85 .pr_ctloutput = rip_ctloutput,
86 .pr_usrreqs = &rip_usrreqs
90 VNET_DEFINE(int, ip_gre_ttl) = GRE_TTL;
91 #define V_ip_gre_ttl VNET(ip_gre_ttl)
92 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, grettl, CTLFLAG_RW,
93 &VNET_NAME(ip_gre_ttl), 0, "");
96 gre_input10(struct mbuf *m, int off)
100 proto = (mtod(m, struct ip *))->ip_p;
101 gre_input(&m, &off, proto);
105 in_gre_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
108 struct gre_softc *sc;
111 sc = (struct gre_softc *)arg;
112 if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
117 * We expect that payload contains at least IPv4
120 if (m->m_pkthdr.len < sizeof(struct greip) + sizeof(struct ip))
124 if (sc->gre_family == 0)
127 KASSERT(sc->gre_family == AF_INET,
128 ("wrong gre_family: %d", sc->gre_family));
130 ip = mtod(m, struct ip *);
131 if (sc->gre_oip.ip_src.s_addr != ip->ip_dst.s_addr ||
132 sc->gre_oip.ip_dst.s_addr != ip->ip_src.s_addr)
143 in_gre_output(struct mbuf *m, int af, int hlen)
147 gi = mtod(m, struct greip *);
151 * gre_transmit() has used M_PREPEND() that doesn't guarantee
152 * m_data is contiguous more than hlen bytes. Use m_copydata()
153 * here to avoid m_pullup().
155 m_copydata(m, hlen + offsetof(struct ip, ip_tos),
156 sizeof(u_char), &gi->gi_ip.ip_tos);
157 m_copydata(m, hlen + offsetof(struct ip, ip_id),
158 sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id);
162 gi->gi_ip.ip_tos = 0; /* XXX */
163 gi->gi_ip.ip_id = ip_newid();
167 gi->gi_ip.ip_ttl = V_ip_gre_ttl;
168 gi->gi_ip.ip_len = htons(m->m_pkthdr.len);
169 return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL));
173 in_gre_attach(struct gre_softc *sc)
176 KASSERT(sc->gre_ecookie == NULL, ("gre_ecookie isn't NULL"));
177 sc->gre_ecookie = encap_attach_func(AF_INET, IPPROTO_GRE,
178 in_gre_encapcheck, &in_gre_protosw, sc);
179 if (sc->gre_ecookie == NULL)