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