1 /*-
2 * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "opt_inet.h"
28 #include "opt_inet6.h"
29
30 #include <sys/param.h>
31 #include <sys/jail.h>
32 #include <sys/systm.h>
33 #include <sys/socket.h>
34 #include <sys/socketvar.h>
35 #include <sys/sockio.h>
36 #include <sys/mbuf.h>
37 #include <sys/errno.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/proc.h>
42
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_private.h>
46 #include <net/vnet.h>
47
48 #include <netinet/in.h>
49 #ifdef INET
50 #include <net/ethernet.h>
51 #include <netinet/ip.h>
52 #endif
53 #include <netinet/in_pcb.h>
54 #include <netinet/ip_encap.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/ip6.h>
57 #include <netinet/udp.h>
58 #include <netinet/udp_var.h>
59 #include <netinet6/ip6_var.h>
60 #include <netinet6/in6_var.h>
61 #include <netinet6/scope6_var.h>
62 #include <net/if_gre.h>
63
64 VNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM;
65 #define V_ip6_gre_hlim VNET(ip6_gre_hlim)
66
67 SYSCTL_DECL(_net_inet6_ip6);
68 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, grehlim, CTLFLAG_VNET | CTLFLAG_RW,
69 &VNET_NAME(ip6_gre_hlim), 0, "Default hop limit for encapsulated packets");
70
71 struct in6_gre_socket {
72 struct gre_socket base;
73 struct in6_addr addr; /* scope zone id is embedded */
74 };
75 VNET_DEFINE_STATIC(struct gre_sockets *, ipv6_sockets) = NULL;
76 VNET_DEFINE_STATIC(struct gre_list *, ipv6_hashtbl) = NULL;
77 VNET_DEFINE_STATIC(struct gre_list *, ipv6_srchashtbl) = NULL;
78 #define V_ipv6_sockets VNET(ipv6_sockets)
79 #define V_ipv6_hashtbl VNET(ipv6_hashtbl)
80 #define V_ipv6_srchashtbl VNET(ipv6_srchashtbl)
81 #define GRE_HASH(src, dst) (V_ipv6_hashtbl[\
82 in6_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
83 #define GRE_SRCHASH(src) (V_ipv6_srchashtbl[\
84 fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
85 #define GRE_SOCKHASH(src) (V_ipv6_sockets[\
86 fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
87 #define GRE_HASH_SC(sc) GRE_HASH(&(sc)->gre_oip6.ip6_src,\
88 &(sc)->gre_oip6.ip6_dst)
89
90 static uint32_t
in6_gre_hashval(const struct in6_addr * src,const struct in6_addr * dst)91 in6_gre_hashval(const struct in6_addr *src, const struct in6_addr *dst)
92 {
93 uint32_t ret;
94
95 ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT);
96 return (fnv_32_buf(dst, sizeof(*dst), ret));
97 }
98
99 static struct gre_socket*
in6_gre_lookup_socket(const struct in6_addr * addr)100 in6_gre_lookup_socket(const struct in6_addr *addr)
101 {
102 struct gre_socket *gs;
103 struct in6_gre_socket *s;
104
105 CK_LIST_FOREACH(gs, &GRE_SOCKHASH(addr), chain) {
106 s = __containerof(gs, struct in6_gre_socket, base);
107 if (IN6_ARE_ADDR_EQUAL(&s->addr, addr))
108 break;
109 }
110 return (gs);
111 }
112
113 static int
in6_gre_checkdup(const struct gre_softc * sc,const struct in6_addr * src,const struct in6_addr * dst,uint32_t opts)114 in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src,
115 const struct in6_addr *dst, uint32_t opts)
116 {
117 struct gre_list *head;
118 struct gre_softc *tmp;
119 struct gre_socket *gs;
120
121 if (sc->gre_family == AF_INET6 &&
122 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, src) &&
123 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, dst) &&
124 (sc->gre_options & GRE_UDPENCAP) == (opts & GRE_UDPENCAP))
125 return (EEXIST);
126
127 if (opts & GRE_UDPENCAP) {
128 gs = in6_gre_lookup_socket(src);
129 if (gs == NULL)
130 return (0);
131 head = &gs->list;
132 } else
133 head = &GRE_HASH(src, dst);
134
135 CK_LIST_FOREACH(tmp, head, chain) {
136 if (tmp == sc)
137 continue;
138 if (IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_src, src) &&
139 IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_dst, dst))
140 return (EADDRNOTAVAIL);
141 }
142 return (0);
143 }
144
145 static int
in6_gre_lookup(const struct mbuf * m,int off,int proto,void ** arg)146 in6_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
147 {
148 const struct ip6_hdr *ip6;
149 struct gre_softc *sc;
150
151 if (V_ipv6_hashtbl == NULL)
152 return (0);
153
154 NET_EPOCH_ASSERT();
155 ip6 = mtod(m, const struct ip6_hdr *);
156 CK_LIST_FOREACH(sc, &GRE_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) {
157 /*
158 * This is an inbound packet, its ip6_dst is source address
159 * in softc.
160 */
161 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src,
162 &ip6->ip6_dst) &&
163 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst,
164 &ip6->ip6_src)) {
165 if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
166 return (0);
167 *arg = sc;
168 return (ENCAP_DRV_LOOKUP);
169 }
170 }
171 return (0);
172 }
173
174 /*
175 * Check that ingress address belongs to local host.
176 */
177 static void
in6_gre_set_running(struct gre_softc * sc)178 in6_gre_set_running(struct gre_softc *sc)
179 {
180
181 if (in6_localip(&sc->gre_oip6.ip6_src))
182 GRE2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
183 else
184 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
185 }
186
187 /*
188 * ifaddr_event handler.
189 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
190 * source address spoofing.
191 */
192 static void
in6_gre_srcaddr(void * arg __unused,const struct sockaddr * sa,int event __unused)193 in6_gre_srcaddr(void *arg __unused, const struct sockaddr *sa,
194 int event __unused)
195 {
196 const struct sockaddr_in6 *sin;
197 struct gre_softc *sc;
198
199 /* Check that VNET is ready */
200 if (V_ipv6_hashtbl == NULL)
201 return;
202
203 NET_EPOCH_ASSERT();
204 sin = (const struct sockaddr_in6 *)sa;
205 CK_LIST_FOREACH(sc, &GRE_SRCHASH(&sin->sin6_addr), srchash) {
206 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src,
207 &sin->sin6_addr) == 0)
208 continue;
209 in6_gre_set_running(sc);
210 }
211 }
212
213 static bool
in6_gre_udp_input(struct mbuf * m,int off,struct inpcb * inp,const struct sockaddr * sa,void * ctx)214 in6_gre_udp_input(struct mbuf *m, int off, struct inpcb *inp,
215 const struct sockaddr *sa, void *ctx)
216 {
217 struct gre_socket *gs;
218 struct gre_softc *sc;
219 struct sockaddr_in6 dst;
220
221 NET_EPOCH_ASSERT();
222
223 gs = (struct gre_socket *)ctx;
224 dst = *(const struct sockaddr_in6 *)sa;
225 if (sa6_embedscope(&dst, 0)) {
226 m_freem(m);
227 return (true);
228 }
229 CK_LIST_FOREACH(sc, &gs->list, chain) {
230 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, &dst.sin6_addr))
231 break;
232 }
233 if (sc != NULL && (GRE2IFP(sc)->if_flags & IFF_UP) != 0){
234 gre_input(m, off + sizeof(struct udphdr), IPPROTO_UDP, sc);
235 return (true);
236 }
237 m_freem(m);
238
239 return (true);
240 }
241
242 static int
in6_gre_setup_socket(struct gre_softc * sc)243 in6_gre_setup_socket(struct gre_softc *sc)
244 {
245 struct sockopt sopt;
246 struct sockaddr_in6 sin6;
247 struct in6_gre_socket *s;
248 struct gre_socket *gs;
249 int error, value;
250
251 /*
252 * NOTE: we are protected with gre_ioctl_sx lock.
253 *
254 * First check that socket is already configured.
255 * If so, check that source address was not changed.
256 * If address is different, check that there are no other tunnels
257 * and close socket.
258 */
259 gs = sc->gre_so;
260 if (gs != NULL) {
261 s = __containerof(gs, struct in6_gre_socket, base);
262 if (!IN6_ARE_ADDR_EQUAL(&s->addr, &sc->gre_oip6.ip6_src)) {
263 if (CK_LIST_EMPTY(&gs->list)) {
264 CK_LIST_REMOVE(gs, chain);
265 soclose(gs->so);
266 NET_EPOCH_CALL(gre_sofree, &gs->epoch_ctx);
267 }
268 gs = sc->gre_so = NULL;
269 }
270 }
271
272 if (gs == NULL) {
273 /*
274 * Check that socket for given address is already
275 * configured.
276 */
277 gs = in6_gre_lookup_socket(&sc->gre_oip6.ip6_src);
278 if (gs == NULL) {
279 s = malloc(sizeof(*s), M_GRE, M_WAITOK | M_ZERO);
280 s->addr = sc->gre_oip6.ip6_src;
281 gs = &s->base;
282
283 error = socreate(sc->gre_family, &gs->so,
284 SOCK_DGRAM, IPPROTO_UDP, curthread->td_ucred,
285 curthread);
286 if (error != 0) {
287 if_printf(GRE2IFP(sc),
288 "cannot create socket: %d\n", error);
289 free(s, M_GRE);
290 return (error);
291 }
292
293 error = udp_set_kernel_tunneling(gs->so,
294 in6_gre_udp_input, NULL, gs);
295 if (error != 0) {
296 if_printf(GRE2IFP(sc),
297 "cannot set UDP tunneling: %d\n", error);
298 goto fail;
299 }
300
301 memset(&sopt, 0, sizeof(sopt));
302 sopt.sopt_dir = SOPT_SET;
303 sopt.sopt_level = IPPROTO_IPV6;
304 sopt.sopt_name = IPV6_BINDANY;
305 sopt.sopt_val = &value;
306 sopt.sopt_valsize = sizeof(value);
307 value = 1;
308 error = sosetopt(gs->so, &sopt);
309 if (error != 0) {
310 if_printf(GRE2IFP(sc),
311 "cannot set IPV6_BINDANY opt: %d\n",
312 error);
313 goto fail;
314 }
315
316 memset(&sin6, 0, sizeof(sin6));
317 sin6.sin6_family = AF_INET6;
318 sin6.sin6_len = sizeof(sin6);
319 sin6.sin6_addr = sc->gre_oip6.ip6_src;
320 sin6.sin6_port = htons(GRE_UDPPORT);
321 error = sa6_recoverscope(&sin6);
322 if (error != 0) {
323 if_printf(GRE2IFP(sc),
324 "cannot determine scope zone id: %d\n",
325 error);
326 goto fail;
327 }
328 error = sobind(gs->so, (struct sockaddr *)&sin6,
329 curthread);
330 if (error != 0) {
331 if_printf(GRE2IFP(sc),
332 "cannot bind socket: %d\n", error);
333 goto fail;
334 }
335 /* Add socket to the chain */
336 CK_LIST_INSERT_HEAD(
337 &GRE_SOCKHASH(&sc->gre_oip6.ip6_src), gs, chain);
338 }
339 }
340
341 /* Add softc to the socket's list */
342 CK_LIST_INSERT_HEAD(&gs->list, sc, chain);
343 sc->gre_so = gs;
344 return (0);
345 fail:
346 soclose(gs->so);
347 free(s, M_GRE);
348 return (error);
349 }
350
351 static int
in6_gre_attach(struct gre_softc * sc)352 in6_gre_attach(struct gre_softc *sc)
353 {
354 struct grehdr *gh;
355 int error;
356
357 if (sc->gre_options & GRE_UDPENCAP) {
358 sc->gre_csumflags = CSUM_UDP_IPV6;
359 sc->gre_hlen = sizeof(struct greudp6);
360 sc->gre_oip6.ip6_nxt = IPPROTO_UDP;
361 gh = &sc->gre_udp6hdr->gi6_gre;
362 gre_update_udphdr(sc, &sc->gre_udp6,
363 in6_cksum_pseudo(&sc->gre_oip6, 0, 0, 0));
364 } else {
365 sc->gre_hlen = sizeof(struct greip6);
366 sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
367 gh = &sc->gre_ip6hdr->gi6_gre;
368 }
369 sc->gre_oip6.ip6_vfc = IPV6_VERSION;
370 gre_update_hdr(sc, gh);
371
372 /*
373 * If we return error, this means that sc is not linked,
374 * and caller should reset gre_family and free(sc->gre_hdr).
375 */
376 if (sc->gre_options & GRE_UDPENCAP) {
377 error = in6_gre_setup_socket(sc);
378 if (error != 0)
379 return (error);
380 } else
381 CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
382 CK_LIST_INSERT_HEAD(&GRE_SRCHASH(&sc->gre_oip6.ip6_src), sc, srchash);
383
384 /* Set IFF_DRV_RUNNING if interface is ready */
385 in6_gre_set_running(sc);
386 return (0);
387 }
388
389 int
in6_gre_setopts(struct gre_softc * sc,u_long cmd,uint32_t value)390 in6_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
391 {
392 int error;
393
394 /* NOTE: we are protected with gre_ioctl_sx lock */
395 MPASS(cmd == GRESKEY || cmd == GRESOPTS || cmd == GRESPORT);
396 MPASS(sc->gre_family == AF_INET6);
397
398 /*
399 * If we are going to change encapsulation protocol, do check
400 * for duplicate tunnels. Return EEXIST here to do not confuse
401 * user.
402 */
403 if (cmd == GRESOPTS &&
404 (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP) &&
405 in6_gre_checkdup(sc, &sc->gre_oip6.ip6_src,
406 &sc->gre_oip6.ip6_dst, value) == EADDRNOTAVAIL)
407 return (EEXIST);
408
409 CK_LIST_REMOVE(sc, chain);
410 CK_LIST_REMOVE(sc, srchash);
411 GRE_WAIT();
412 switch (cmd) {
413 case GRESKEY:
414 sc->gre_key = value;
415 break;
416 case GRESOPTS:
417 sc->gre_options = value;
418 break;
419 case GRESPORT:
420 sc->gre_port = value;
421 break;
422 }
423 error = in6_gre_attach(sc);
424 if (error != 0) {
425 sc->gre_family = 0;
426 free(sc->gre_hdr, M_GRE);
427 }
428 return (error);
429 }
430
431 int
in6_gre_ioctl(struct gre_softc * sc,u_long cmd,caddr_t data)432 in6_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
433 {
434 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
435 struct sockaddr_in6 *dst, *src;
436 struct ip6_hdr *ip6;
437 int error;
438
439 /* NOTE: we are protected with gre_ioctl_sx lock */
440 error = EINVAL;
441 switch (cmd) {
442 case SIOCSIFPHYADDR_IN6:
443 src = &((struct in6_aliasreq *)data)->ifra_addr;
444 dst = &((struct in6_aliasreq *)data)->ifra_dstaddr;
445
446 /* sanity checks */
447 if (src->sin6_family != dst->sin6_family ||
448 src->sin6_family != AF_INET6 ||
449 src->sin6_len != dst->sin6_len ||
450 src->sin6_len != sizeof(*src))
451 break;
452 if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) ||
453 IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) {
454 error = EADDRNOTAVAIL;
455 break;
456 }
457 /*
458 * Check validity of the scope zone ID of the
459 * addresses, and convert it into the kernel
460 * internal form if necessary.
461 */
462 if ((error = sa6_embedscope(src, 0)) != 0 ||
463 (error = sa6_embedscope(dst, 0)) != 0)
464 break;
465
466 if (V_ipv6_hashtbl == NULL) {
467 V_ipv6_hashtbl = gre_hashinit();
468 V_ipv6_srchashtbl = gre_hashinit();
469 V_ipv6_sockets = (struct gre_sockets *)gre_hashinit();
470 }
471 error = in6_gre_checkdup(sc, &src->sin6_addr,
472 &dst->sin6_addr, sc->gre_options);
473 if (error == EADDRNOTAVAIL)
474 break;
475 if (error == EEXIST) {
476 /* Addresses are the same. Just return. */
477 error = 0;
478 break;
479 }
480 ip6 = malloc(sizeof(struct greudp6) + 3 * sizeof(uint32_t),
481 M_GRE, M_WAITOK | M_ZERO);
482 ip6->ip6_src = src->sin6_addr;
483 ip6->ip6_dst = dst->sin6_addr;
484 if (sc->gre_family != 0) {
485 /* Detach existing tunnel first */
486 CK_LIST_REMOVE(sc, chain);
487 CK_LIST_REMOVE(sc, srchash);
488 GRE_WAIT();
489 free(sc->gre_hdr, M_GRE);
490 /* XXX: should we notify about link state change? */
491 }
492 sc->gre_family = AF_INET6;
493 sc->gre_hdr = ip6;
494 sc->gre_oseq = 0;
495 sc->gre_iseq = UINT32_MAX;
496 error = in6_gre_attach(sc);
497 if (error != 0) {
498 sc->gre_family = 0;
499 free(sc->gre_hdr, M_GRE);
500 }
501 break;
502 case SIOCGIFPSRCADDR_IN6:
503 case SIOCGIFPDSTADDR_IN6:
504 if (sc->gre_family != AF_INET6) {
505 error = EADDRNOTAVAIL;
506 break;
507 }
508 src = (struct sockaddr_in6 *)&ifr->ifr_addr;
509 memset(src, 0, sizeof(*src));
510 src->sin6_family = AF_INET6;
511 src->sin6_len = sizeof(*src);
512 src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ?
513 sc->gre_oip6.ip6_src: sc->gre_oip6.ip6_dst;
514 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
515 if (error == 0)
516 error = sa6_recoverscope(src);
517 if (error != 0)
518 memset(src, 0, sizeof(*src));
519 break;
520 }
521 return (error);
522 }
523
524 int
in6_gre_output(struct mbuf * m,int af __unused,int hlen __unused,uint32_t flowid)525 in6_gre_output(struct mbuf *m, int af __unused, int hlen __unused,
526 uint32_t flowid)
527 {
528 struct greip6 *gi6;
529
530 gi6 = mtod(m, struct greip6 *);
531 gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim;
532 gi6->gi6_ip6.ip6_flow |= flowid & IPV6_FLOWLABEL_MASK;
533 return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL));
534 }
535
536 static const struct srcaddrtab *ipv6_srcaddrtab = NULL;
537 static const struct encaptab *ecookie = NULL;
538 static const struct encap_config ipv6_encap_cfg = {
539 .proto = IPPROTO_GRE,
540 .min_length = sizeof(struct greip6) +
541 #ifdef INET
542 sizeof(struct ip),
543 #else
544 sizeof(struct ip6_hdr),
545 #endif
546 .exact_match = ENCAP_DRV_LOOKUP,
547 .lookup = in6_gre_lookup,
548 .input = gre_input
549 };
550
551 void
in6_gre_init(void)552 in6_gre_init(void)
553 {
554
555 if (!IS_DEFAULT_VNET(curvnet))
556 return;
557 ipv6_srcaddrtab = ip6_encap_register_srcaddr(in6_gre_srcaddr,
558 NULL, M_WAITOK);
559 ecookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK);
560 }
561
562 void
in6_gre_uninit(void)563 in6_gre_uninit(void)
564 {
565
566 if (IS_DEFAULT_VNET(curvnet)) {
567 ip6_encap_detach(ecookie);
568 ip6_encap_unregister_srcaddr(ipv6_srcaddrtab);
569 }
570 if (V_ipv6_hashtbl != NULL) {
571 gre_hashdestroy(V_ipv6_hashtbl);
572 V_ipv6_hashtbl = NULL;
573 GRE_WAIT();
574 gre_hashdestroy(V_ipv6_srchashtbl);
575 gre_hashdestroy((struct gre_list *)V_ipv6_sockets);
576 }
577 }
578