xref: /freebsd/sys/netinet/ip_gre.c (revision 780fb4a2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3  *
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Heiko W.Rupp <hwr@pilhuhn.de>
10  *
11  * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
21  *
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.
33  *
34  * $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_inet.h"
41 #include "opt_inet6.h"
42 
43 #include <sys/param.h>
44 #include <sys/jail.h>
45 #include <sys/systm.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/mbuf.h>
49 #include <sys/errno.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/malloc.h>
53 #include <sys/proc.h>
54 
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/vnet.h>
58 
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_encap.h>
63 #include <netinet/ip_var.h>
64 
65 #ifdef INET6
66 #include <netinet/ip6.h>
67 #endif
68 
69 #include <net/if_gre.h>
70 
71 #define	GRE_TTL			30
72 VNET_DEFINE(int, ip_gre_ttl) = GRE_TTL;
73 #define	V_ip_gre_ttl		VNET(ip_gre_ttl)
74 SYSCTL_INT(_net_inet_ip, OID_AUTO, grettl, CTLFLAG_VNET | CTLFLAG_RW,
75     &VNET_NAME(ip_gre_ttl), 0, "Default TTL value for encapsulated packets");
76 
77 static VNET_DEFINE(struct gre_list *, ipv4_hashtbl) = NULL;
78 #define	V_ipv4_hashtbl		VNET(ipv4_hashtbl)
79 #define	GRE_HASH(src, dst)	(V_ipv4_hashtbl[\
80     in_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
81 #define	GRE_HASH_SC(sc)		GRE_HASH((sc)->gre_oip.ip_src.s_addr,\
82     (sc)->gre_oip.ip_dst.s_addr)
83 
84 static uint32_t
85 in_gre_hashval(in_addr_t src, in_addr_t dst)
86 {
87 	uint32_t ret;
88 
89 	ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
90 	return (fnv_32_buf(&dst, sizeof(dst), ret));
91 }
92 
93 static int
94 in_gre_checkdup(const struct gre_softc *sc, in_addr_t src, in_addr_t dst)
95 {
96 	struct gre_softc *tmp;
97 
98 	if (sc->gre_family == AF_INET &&
99 	    sc->gre_oip.ip_src.s_addr == src &&
100 	    sc->gre_oip.ip_dst.s_addr == dst)
101 		return (EEXIST);
102 
103 	CK_LIST_FOREACH(tmp, &GRE_HASH(src, dst), chain) {
104 		if (tmp == sc)
105 			continue;
106 		if (tmp->gre_oip.ip_src.s_addr == src &&
107 		    tmp->gre_oip.ip_dst.s_addr == dst)
108 			return (EADDRNOTAVAIL);
109 	}
110 	return (0);
111 }
112 
113 static int
114 in_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
115 {
116 	const struct ip *ip;
117 	struct gre_softc *sc;
118 
119 	if (V_ipv4_hashtbl == NULL)
120 		return (0);
121 
122 	MPASS(in_epoch(net_epoch_preempt));
123 	ip = mtod(m, const struct ip *);
124 	CK_LIST_FOREACH(sc, &GRE_HASH(ip->ip_dst.s_addr,
125 	    ip->ip_src.s_addr), chain) {
126 		/*
127 		 * This is an inbound packet, its ip_dst is source address
128 		 * in softc.
129 		 */
130 		if (sc->gre_oip.ip_src.s_addr == ip->ip_dst.s_addr &&
131 		    sc->gre_oip.ip_dst.s_addr == ip->ip_src.s_addr) {
132 			if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
133 				return (0);
134 			*arg = sc;
135 			return (ENCAP_DRV_LOOKUP);
136 		}
137 	}
138 	return (0);
139 }
140 
141 static void
142 in_gre_attach(struct gre_softc *sc)
143 {
144 
145 	sc->gre_hlen = sizeof(struct greip);
146 	sc->gre_oip.ip_v = IPVERSION;
147 	sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
148 	sc->gre_oip.ip_p = IPPROTO_GRE;
149 	gre_updatehdr(sc, &sc->gre_gihdr->gi_gre);
150 	CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
151 }
152 
153 void
154 in_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
155 {
156 
157 	MPASS(cmd == GRESKEY || cmd == GRESOPTS);
158 
159 	/* NOTE: we are protected with gre_ioctl_sx lock */
160 	MPASS(sc->gre_family == AF_INET);
161 	CK_LIST_REMOVE(sc, chain);
162 	GRE_WAIT();
163 	if (cmd == GRESKEY)
164 		sc->gre_key = value;
165 	else
166 		sc->gre_options = value;
167 	in_gre_attach(sc);
168 }
169 
170 int
171 in_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
172 {
173 	struct ifreq *ifr = (struct ifreq *)data;
174 	struct sockaddr_in *dst, *src;
175 	struct ip *ip;
176 	int error;
177 
178 	/* NOTE: we are protected with gre_ioctl_sx lock */
179 	error = EINVAL;
180 	switch (cmd) {
181 	case SIOCSIFPHYADDR:
182 		src = &((struct in_aliasreq *)data)->ifra_addr;
183 		dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
184 
185 		/* sanity checks */
186 		if (src->sin_family != dst->sin_family ||
187 		    src->sin_family != AF_INET ||
188 		    src->sin_len != dst->sin_len ||
189 		    src->sin_len != sizeof(*src))
190 			break;
191 		if (src->sin_addr.s_addr == INADDR_ANY ||
192 		    dst->sin_addr.s_addr == INADDR_ANY) {
193 			error = EADDRNOTAVAIL;
194 			break;
195 		}
196 		if (V_ipv4_hashtbl == NULL)
197 			V_ipv4_hashtbl = gre_hashinit();
198 		error = in_gre_checkdup(sc, src->sin_addr.s_addr,
199 		    dst->sin_addr.s_addr);
200 		if (error == EADDRNOTAVAIL)
201 			break;
202 		if (error == EEXIST) {
203 			/* Addresses are the same. Just return. */
204 			error = 0;
205 			break;
206 		}
207 		ip = malloc(sizeof(struct greip) + 3 * sizeof(uint32_t),
208 		    M_GRE, M_WAITOK | M_ZERO);
209 		ip->ip_src.s_addr = src->sin_addr.s_addr;
210 		ip->ip_dst.s_addr = dst->sin_addr.s_addr;
211 		if (sc->gre_family != 0) {
212 			/* Detach existing tunnel first */
213 			CK_LIST_REMOVE(sc, chain);
214 			GRE_WAIT();
215 			free(sc->gre_hdr, M_GRE);
216 			/* XXX: should we notify about link state change? */
217 		}
218 		sc->gre_family = AF_INET;
219 		sc->gre_hdr = ip;
220 		sc->gre_oseq = 0;
221 		sc->gre_iseq = UINT32_MAX;
222 		in_gre_attach(sc);
223 		break;
224 	case SIOCGIFPSRCADDR:
225 	case SIOCGIFPDSTADDR:
226 		if (sc->gre_family != AF_INET) {
227 			error = EADDRNOTAVAIL;
228 			break;
229 		}
230 		src = (struct sockaddr_in *)&ifr->ifr_addr;
231 		memset(src, 0, sizeof(*src));
232 		src->sin_family = AF_INET;
233 		src->sin_len = sizeof(*src);
234 		src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
235 		    sc->gre_oip.ip_src: sc->gre_oip.ip_dst;
236 		error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
237 		if (error != 0)
238 			memset(src, 0, sizeof(*src));
239 		break;
240 	}
241 	return (error);
242 }
243 
244 int
245 in_gre_output(struct mbuf *m, int af, int hlen)
246 {
247 	struct greip *gi;
248 
249 	gi = mtod(m, struct greip *);
250 	switch (af) {
251 	case AF_INET:
252 		/*
253 		 * gre_transmit() has used M_PREPEND() that doesn't guarantee
254 		 * m_data is contiguous more than hlen bytes. Use m_copydata()
255 		 * here to avoid m_pullup().
256 		 */
257 		m_copydata(m, hlen + offsetof(struct ip, ip_tos),
258 		    sizeof(u_char), &gi->gi_ip.ip_tos);
259 		m_copydata(m, hlen + offsetof(struct ip, ip_id),
260 		    sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id);
261 		break;
262 #ifdef INET6
263 	case AF_INET6:
264 		gi->gi_ip.ip_tos = 0; /* XXX */
265 		ip_fillid(&gi->gi_ip);
266 		break;
267 #endif
268 	}
269 	gi->gi_ip.ip_ttl = V_ip_gre_ttl;
270 	gi->gi_ip.ip_len = htons(m->m_pkthdr.len);
271 	return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL));
272 }
273 
274 static const struct encaptab *ecookie = NULL;
275 static const struct encap_config ipv4_encap_cfg = {
276 	.proto = IPPROTO_GRE,
277 	.min_length = sizeof(struct greip) + sizeof(struct ip),
278 	.exact_match = ENCAP_DRV_LOOKUP,
279 	.lookup = in_gre_lookup,
280 	.input = gre_input
281 };
282 
283 void
284 in_gre_init(void)
285 {
286 
287 	if (!IS_DEFAULT_VNET(curvnet))
288 		return;
289 	ecookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
290 }
291 
292 void
293 in_gre_uninit(void)
294 {
295 
296 	if (IS_DEFAULT_VNET(curvnet))
297 		ip_encap_detach(ecookie);
298 	if (V_ipv4_hashtbl != NULL)
299 		gre_hashdestroy(V_ipv4_hashtbl);
300 }
301