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