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