1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
33 */
34
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/jail.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/mbuf.h>
44 #include <sys/errno.h>
45 #include <sys/kernel.h>
46 #include <sys/syslog.h>
47 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
49 #include <sys/proc.h>
50
51 #include <net/ethernet.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_private.h>
55 #include <net/route.h>
56 #include <net/vnet.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #ifdef INET
61 #include <netinet/ip.h>
62 #include <netinet/ip_ecn.h>
63 #endif
64 #include <netinet/ip_encap.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #include <netinet6/in6_var.h>
68 #include <netinet6/scope6_var.h>
69 #include <netinet6/ip6_ecn.h>
70 #include <netinet6/in6_fib.h>
71
72 #include <net/if_gif.h>
73
74 #define GIF_HLIM 30
75 VNET_DEFINE_STATIC(int, ip6_gif_hlim) = GIF_HLIM;
76 #define V_ip6_gif_hlim VNET(ip6_gif_hlim)
77
78 SYSCTL_DECL(_net_inet6_ip6);
79 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim,
80 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_gif_hlim), 0,
81 "Default hop limit for encapsulated packets");
82
83 /*
84 * We keep interfaces in a hash table using src+dst as key.
85 * Interfaces with GIF_IGNORE_SOURCE flag are linked into plain list.
86 */
87 VNET_DEFINE_STATIC(struct gif_list *, ipv6_hashtbl) = NULL;
88 VNET_DEFINE_STATIC(struct gif_list *, ipv6_srchashtbl) = NULL;
89 VNET_DEFINE_STATIC(struct gif_list, ipv6_list) = CK_LIST_HEAD_INITIALIZER();
90 #define V_ipv6_hashtbl VNET(ipv6_hashtbl)
91 #define V_ipv6_srchashtbl VNET(ipv6_srchashtbl)
92 #define V_ipv6_list VNET(ipv6_list)
93
94 #define GIF_HASH(src, dst) (V_ipv6_hashtbl[\
95 in6_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
96 #define GIF_SRCHASH(src) (V_ipv6_srchashtbl[\
97 fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GIF_HASH_SIZE - 1)])
98 #define GIF_HASH_SC(sc) GIF_HASH(&(sc)->gif_ip6hdr->ip6_src,\
99 &(sc)->gif_ip6hdr->ip6_dst)
100 static uint32_t
in6_gif_hashval(const struct in6_addr * src,const struct in6_addr * dst)101 in6_gif_hashval(const struct in6_addr *src, const struct in6_addr *dst)
102 {
103 uint32_t ret;
104
105 ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT);
106 return (fnv_32_buf(dst, sizeof(*dst), ret));
107 }
108
109 static int
in6_gif_checkdup(const struct gif_softc * sc,const struct in6_addr * src,const struct in6_addr * dst)110 in6_gif_checkdup(const struct gif_softc *sc, const struct in6_addr *src,
111 const struct in6_addr *dst)
112 {
113 struct gif_softc *tmp;
114
115 if (sc->gif_family == AF_INET6 &&
116 IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, src) &&
117 IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, dst))
118 return (EEXIST);
119
120 CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
121 if (tmp == sc)
122 continue;
123 if (IN6_ARE_ADDR_EQUAL(&tmp->gif_ip6hdr->ip6_src, src) &&
124 IN6_ARE_ADDR_EQUAL(&tmp->gif_ip6hdr->ip6_dst, dst))
125 return (EADDRNOTAVAIL);
126 }
127 return (0);
128 }
129
130 /*
131 * Check that ingress address belongs to local host.
132 */
133 static void
in6_gif_set_running(struct gif_softc * sc)134 in6_gif_set_running(struct gif_softc *sc)
135 {
136
137 if (in6_localip(&sc->gif_ip6hdr->ip6_src))
138 GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
139 else
140 GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
141 }
142
143 /*
144 * ifaddr_event handler.
145 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
146 * source address spoofing.
147 */
148 static void
in6_gif_srcaddr(void * arg __unused,const struct sockaddr * sa,int event)149 in6_gif_srcaddr(void *arg __unused, const struct sockaddr *sa, int event)
150 {
151 const struct sockaddr_in6 *sin;
152 struct gif_softc *sc;
153
154 /* Check that VNET is ready */
155 if (V_ipv6_hashtbl == NULL)
156 return;
157
158 NET_EPOCH_ASSERT();
159 sin = (const struct sockaddr_in6 *)sa;
160 CK_LIST_FOREACH(sc, &GIF_SRCHASH(&sin->sin6_addr), srchash) {
161 if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
162 &sin->sin6_addr) == 0)
163 continue;
164 in6_gif_set_running(sc);
165 }
166 }
167
168 static void
in6_gif_attach(struct gif_softc * sc)169 in6_gif_attach(struct gif_softc *sc)
170 {
171
172 if (sc->gif_options & GIF_IGNORE_SOURCE)
173 CK_LIST_INSERT_HEAD(&V_ipv6_list, sc, chain);
174 else
175 CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
176
177 CK_LIST_INSERT_HEAD(&GIF_SRCHASH(&sc->gif_ip6hdr->ip6_src),
178 sc, srchash);
179 }
180
181 int
in6_gif_setopts(struct gif_softc * sc,u_int options)182 in6_gif_setopts(struct gif_softc *sc, u_int options)
183 {
184
185 /* NOTE: we are protected with gif_ioctl_sx lock */
186 MPASS(sc->gif_family == AF_INET6);
187 MPASS(sc->gif_options != options);
188
189 if ((options & GIF_IGNORE_SOURCE) !=
190 (sc->gif_options & GIF_IGNORE_SOURCE)) {
191 CK_LIST_REMOVE(sc, srchash);
192 CK_LIST_REMOVE(sc, chain);
193 sc->gif_options = options;
194 in6_gif_attach(sc);
195 }
196
197 if ((options & GIF_NOCLAMP) !=
198 (sc->gif_options & GIF_NOCLAMP)) {
199 sc->gif_options = options;
200 }
201 return (0);
202 }
203
204 int
in6_gif_ioctl(struct gif_softc * sc,u_long cmd,caddr_t data)205 in6_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
206 {
207 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
208 struct sockaddr_in6 *dst, *src;
209 struct ip6_hdr *ip6;
210 int error;
211
212 /* NOTE: we are protected with gif_ioctl_sx lock */
213 error = EINVAL;
214 switch (cmd) {
215 case SIOCSIFPHYADDR_IN6:
216 src = &((struct in6_aliasreq *)data)->ifra_addr;
217 dst = &((struct in6_aliasreq *)data)->ifra_dstaddr;
218
219 /* sanity checks */
220 if (src->sin6_family != dst->sin6_family ||
221 src->sin6_family != AF_INET6 ||
222 src->sin6_len != dst->sin6_len ||
223 src->sin6_len != sizeof(*src))
224 break;
225 if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) ||
226 IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) {
227 error = EADDRNOTAVAIL;
228 break;
229 }
230 /*
231 * Check validity of the scope zone ID of the
232 * addresses, and convert it into the kernel
233 * internal form if necessary.
234 */
235 if ((error = sa6_embedscope(src, 0)) != 0 ||
236 (error = sa6_embedscope(dst, 0)) != 0)
237 break;
238
239 if (V_ipv6_hashtbl == NULL) {
240 V_ipv6_hashtbl = gif_hashinit();
241 V_ipv6_srchashtbl = gif_hashinit();
242 }
243 error = in6_gif_checkdup(sc, &src->sin6_addr,
244 &dst->sin6_addr);
245 if (error == EADDRNOTAVAIL)
246 break;
247 if (error == EEXIST) {
248 /* Addresses are the same. Just return. */
249 error = 0;
250 break;
251 }
252 ip6 = malloc(sizeof(*ip6), M_GIF, M_WAITOK | M_ZERO);
253 ip6->ip6_src = src->sin6_addr;
254 ip6->ip6_dst = dst->sin6_addr;
255 ip6->ip6_vfc = IPV6_VERSION;
256 if (sc->gif_family != 0) {
257 /* Detach existing tunnel first */
258 CK_LIST_REMOVE(sc, srchash);
259 CK_LIST_REMOVE(sc, chain);
260 GIF_WAIT();
261 free(sc->gif_hdr, M_GIF);
262 /* XXX: should we notify about link state change? */
263 }
264 sc->gif_family = AF_INET6;
265 sc->gif_ip6hdr = ip6;
266 in6_gif_attach(sc);
267 in6_gif_set_running(sc);
268 break;
269 case SIOCGIFPSRCADDR_IN6:
270 case SIOCGIFPDSTADDR_IN6:
271 if (sc->gif_family != AF_INET6) {
272 error = EADDRNOTAVAIL;
273 break;
274 }
275 src = (struct sockaddr_in6 *)&ifr->ifr_addr;
276 memset(src, 0, sizeof(*src));
277 src->sin6_family = AF_INET6;
278 src->sin6_len = sizeof(*src);
279 src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ?
280 sc->gif_ip6hdr->ip6_src: sc->gif_ip6hdr->ip6_dst;
281 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
282 if (error == 0)
283 error = sa6_recoverscope(src);
284 if (error != 0)
285 memset(src, 0, sizeof(*src));
286 break;
287 }
288 return (error);
289 }
290
291 int
in6_gif_output(struct ifnet * ifp,struct mbuf * m,int proto,uint8_t ecn)292 in6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
293 {
294 struct gif_softc *sc = ifp->if_softc;
295 struct ip6_hdr *ip6;
296 u_long mtu;
297
298 /* prepend new IP header */
299 NET_EPOCH_ASSERT();
300 M_PREPEND(m, sizeof(struct ip6_hdr), M_NOWAIT);
301 if (m == NULL)
302 return (ENOBUFS);
303
304 ip6 = mtod(m, struct ip6_hdr *);
305 MPASS(sc->gif_family == AF_INET6);
306 memcpy(ip6, sc->gif_ip6hdr, sizeof(struct ip6_hdr));
307
308 ip6->ip6_flow |= htonl((uint32_t)ecn << 20);
309 ip6->ip6_nxt = proto;
310 ip6->ip6_hlim = V_ip6_gif_hlim;
311 /*
312 * Enforce fragmentation to minimum MTU, even if the interface MTU
313 * is larger, to avoid path MTU discovery when NOCLAMP is not
314 * set (default). IPv6 does not allow fragmentation on intermediate
315 * router nodes, so it is too painful to ask for resend of inner
316 * packet, to achieve path MTU discovery for encapsulated packets.
317 */
318 mtu = ((sc->gif_options & GIF_NOCLAMP) == 0) ? IPV6_MINMTU : 0;
319
320 return (ip6_output(m, 0, NULL, mtu, 0, NULL, NULL));
321 }
322
323 static int
in6_gif_input(struct mbuf * m,int off,int proto,void * arg)324 in6_gif_input(struct mbuf *m, int off, int proto, void *arg)
325 {
326 struct gif_softc *sc = arg;
327 struct ifnet *gifp;
328 struct ip6_hdr *ip6;
329 uint8_t ecn;
330
331 NET_EPOCH_ASSERT();
332 if (sc == NULL) {
333 m_freem(m);
334 IP6STAT_INC(ip6s_nogif);
335 return (IPPROTO_DONE);
336 }
337 gifp = GIF2IFP(sc);
338 if ((gifp->if_flags & IFF_UP) != 0) {
339 ip6 = mtod(m, struct ip6_hdr *);
340 ecn = IPV6_TRAFFIC_CLASS(ip6);
341 m_adj(m, off);
342 gif_input(m, gifp, proto, ecn);
343 } else {
344 m_freem(m);
345 IP6STAT_INC(ip6s_nogif);
346 }
347 return (IPPROTO_DONE);
348 }
349
350 static int
in6_gif_lookup(const struct mbuf * m,int off,int proto,void ** arg)351 in6_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
352 {
353 const struct ip6_hdr *ip6;
354 struct gif_softc *sc;
355 int ret;
356
357 if (V_ipv6_hashtbl == NULL)
358 return (0);
359
360 NET_EPOCH_ASSERT();
361 /*
362 * NOTE: it is safe to iterate without any locking here, because softc
363 * can be reclaimed only when we are not within net_epoch_preempt
364 * section, but ip_encap lookup+input are executed in epoch section.
365 */
366 ip6 = mtod(m, const struct ip6_hdr *);
367 ret = 0;
368 CK_LIST_FOREACH(sc, &GIF_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) {
369 /*
370 * This is an inbound packet, its ip6_dst is source address
371 * in softc.
372 */
373 if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
374 &ip6->ip6_dst) &&
375 IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst,
376 &ip6->ip6_src)) {
377 ret = ENCAP_DRV_LOOKUP;
378 goto done;
379 }
380 }
381 /*
382 * No exact match.
383 * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
384 */
385 CK_LIST_FOREACH(sc, &V_ipv6_list, chain) {
386 if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
387 &ip6->ip6_dst)) {
388 ret = 128 + 8; /* src + proto */
389 goto done;
390 }
391 }
392 return (0);
393 done:
394 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
395 return (0);
396 /* ingress filters on outer source */
397 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
398 if (fib6_check_urpf(sc->gif_fibnum, &ip6->ip6_src,
399 ntohs(in6_getscope(&ip6->ip6_src)), NHR_NONE,
400 m->m_pkthdr.rcvif) == 0)
401 return (0);
402 }
403 *arg = sc;
404 return (ret);
405 }
406
407 static const struct srcaddrtab *ipv6_srcaddrtab;
408 static struct {
409 const struct encap_config encap;
410 const struct encaptab *cookie;
411 } ipv6_encap_cfg[] = {
412 #ifdef INET
413 {
414 .encap = {
415 .proto = IPPROTO_IPV4,
416 .min_length = sizeof(struct ip6_hdr) +
417 sizeof(struct ip),
418 .exact_match = ENCAP_DRV_LOOKUP,
419 .lookup = in6_gif_lookup,
420 .input = in6_gif_input
421 },
422 },
423 #endif
424 {
425 .encap = {
426 .proto = IPPROTO_IPV6,
427 .min_length = 2 * sizeof(struct ip6_hdr),
428 .exact_match = ENCAP_DRV_LOOKUP,
429 .lookup = in6_gif_lookup,
430 .input = in6_gif_input
431 },
432 },
433 {
434 .encap = {
435 .proto = IPPROTO_ETHERIP,
436 .min_length = sizeof(struct ip6_hdr) +
437 sizeof(struct etherip_header) +
438 sizeof(struct ether_header),
439 .exact_match = ENCAP_DRV_LOOKUP,
440 .lookup = in6_gif_lookup,
441 .input = in6_gif_input
442 },
443 }
444 };
445
446 void
in6_gif_init(void)447 in6_gif_init(void)
448 {
449 int i;
450
451 if (!IS_DEFAULT_VNET(curvnet))
452 return;
453
454 ipv6_srcaddrtab = ip6_encap_register_srcaddr(in6_gif_srcaddr,
455 NULL, M_WAITOK);
456 for (i = 0; i < nitems(ipv6_encap_cfg); i++)
457 ipv6_encap_cfg[i].cookie = ip6_encap_attach(
458 &ipv6_encap_cfg[i].encap, NULL, M_WAITOK);
459 }
460
461 void
in6_gif_uninit(void)462 in6_gif_uninit(void)
463 {
464 int i;
465
466 if (IS_DEFAULT_VNET(curvnet)) {
467 for (i = 0; i < nitems(ipv6_encap_cfg); i++)
468 ip6_encap_detach(ipv6_encap_cfg[i].cookie);
469 ip6_encap_unregister_srcaddr(ipv6_srcaddrtab);
470 }
471 if (V_ipv6_hashtbl != NULL) {
472 gif_hashdestroy(V_ipv6_hashtbl);
473 V_ipv6_hashtbl = NULL;
474 GIF_WAIT();
475 gif_hashdestroy(V_ipv6_srchashtbl);
476 }
477 }
478