1c398230bSWarner Losh /*-
2b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
48e96e13eSMaxim Sobolev * Copyright (c) 1998 The NetBSD Foundation, Inc.
5a5185adeSAndrey V. Elsukov * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
68e96e13eSMaxim Sobolev * All rights reserved.
78e96e13eSMaxim Sobolev *
88e96e13eSMaxim Sobolev * This code is derived from software contributed to The NetBSD Foundation
98e96e13eSMaxim Sobolev * by Heiko W.Rupp <hwr@pilhuhn.de>
108e96e13eSMaxim Sobolev *
119e669156SBjoern A. Zeeb * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
129e669156SBjoern A. Zeeb *
138e96e13eSMaxim Sobolev * Redistribution and use in source and binary forms, with or without
148e96e13eSMaxim Sobolev * modification, are permitted provided that the following conditions
158e96e13eSMaxim Sobolev * are met:
168e96e13eSMaxim Sobolev * 1. Redistributions of source code must retain the above copyright
178e96e13eSMaxim Sobolev * notice, this list of conditions and the following disclaimer.
188e96e13eSMaxim Sobolev * 2. Redistributions in binary form must reproduce the above copyright
198e96e13eSMaxim Sobolev * notice, this list of conditions and the following disclaimer in the
208e96e13eSMaxim Sobolev * documentation and/or other materials provided with the distribution.
218e96e13eSMaxim Sobolev *
228e96e13eSMaxim Sobolev * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
238e96e13eSMaxim Sobolev * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
248e96e13eSMaxim Sobolev * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
258e96e13eSMaxim Sobolev * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
268e96e13eSMaxim Sobolev * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
278e96e13eSMaxim Sobolev * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
288e96e13eSMaxim Sobolev * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
298e96e13eSMaxim Sobolev * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
308e96e13eSMaxim Sobolev * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
318e96e13eSMaxim Sobolev * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
328e96e13eSMaxim Sobolev * POSSIBILITY OF SUCH DAMAGE.
33f325335cSAndrey V. Elsukov *
34f325335cSAndrey V. Elsukov * $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $
358e96e13eSMaxim Sobolev */
368e96e13eSMaxim Sobolev
374b421e2dSMike Silbersack #include <sys/cdefs.h>
388e96e13eSMaxim Sobolev #include "opt_inet.h"
399e669156SBjoern A. Zeeb #include "opt_inet6.h"
408e96e13eSMaxim Sobolev
418e96e13eSMaxim Sobolev #include <sys/param.h>
42a5185adeSAndrey V. Elsukov #include <sys/jail.h>
438e96e13eSMaxim Sobolev #include <sys/systm.h>
448e96e13eSMaxim Sobolev #include <sys/socket.h>
45aee793eeSAndrey V. Elsukov #include <sys/socketvar.h>
46a5185adeSAndrey V. Elsukov #include <sys/sockio.h>
47a5185adeSAndrey V. Elsukov #include <sys/mbuf.h>
488e96e13eSMaxim Sobolev #include <sys/errno.h>
498e96e13eSMaxim Sobolev #include <sys/kernel.h>
50f325335cSAndrey V. Elsukov #include <sys/sysctl.h>
51a5185adeSAndrey V. Elsukov #include <sys/malloc.h>
526573d758SMatt Macy #include <sys/proc.h>
53a5185adeSAndrey V. Elsukov
548e96e13eSMaxim Sobolev #include <net/if.h>
5576039bc8SGleb Smirnoff #include <net/if_var.h>
563d0d5b21SJustin Hibbits #include <net/if_private.h>
57f325335cSAndrey V. Elsukov #include <net/vnet.h>
588e96e13eSMaxim Sobolev
598e96e13eSMaxim Sobolev #include <netinet/in.h>
608e96e13eSMaxim Sobolev #include <netinet/in_var.h>
61aee793eeSAndrey V. Elsukov #include <netinet/in_pcb.h>
628e96e13eSMaxim Sobolev #include <netinet/ip.h>
63f325335cSAndrey V. Elsukov #include <netinet/ip_encap.h>
648e96e13eSMaxim Sobolev #include <netinet/ip_var.h>
65aee793eeSAndrey V. Elsukov #include <netinet/udp.h>
66aee793eeSAndrey V. Elsukov #include <netinet/udp_var.h>
67f325335cSAndrey V. Elsukov
68f325335cSAndrey V. Elsukov #ifdef INET6
69f325335cSAndrey V. Elsukov #include <netinet/ip6.h>
708e96e13eSMaxim Sobolev #endif
718e96e13eSMaxim Sobolev
728e96e13eSMaxim Sobolev #include <net/if_gre.h>
73aee793eeSAndrey V. Elsukov #include <machine/in_cksum.h>
748e96e13eSMaxim Sobolev
75f325335cSAndrey V. Elsukov #define GRE_TTL 30
76f325335cSAndrey V. Elsukov VNET_DEFINE(int, ip_gre_ttl) = GRE_TTL;
77f325335cSAndrey V. Elsukov #define V_ip_gre_ttl VNET(ip_gre_ttl)
78f325335cSAndrey V. Elsukov SYSCTL_INT(_net_inet_ip, OID_AUTO, grettl, CTLFLAG_VNET | CTLFLAG_RW,
796d8fdfa9SAndrey V. Elsukov &VNET_NAME(ip_gre_ttl), 0, "Default TTL value for encapsulated packets");
808e96e13eSMaxim Sobolev
81aee793eeSAndrey V. Elsukov struct in_gre_socket {
82aee793eeSAndrey V. Elsukov struct gre_socket base;
83aee793eeSAndrey V. Elsukov in_addr_t addr;
84aee793eeSAndrey V. Elsukov };
85aee793eeSAndrey V. Elsukov VNET_DEFINE_STATIC(struct gre_sockets *, ipv4_sockets) = NULL;
865f901c92SAndrew Turner VNET_DEFINE_STATIC(struct gre_list *, ipv4_hashtbl) = NULL;
8719873f47SAndrey V. Elsukov VNET_DEFINE_STATIC(struct gre_list *, ipv4_srchashtbl) = NULL;
88aee793eeSAndrey V. Elsukov #define V_ipv4_sockets VNET(ipv4_sockets)
89a5185adeSAndrey V. Elsukov #define V_ipv4_hashtbl VNET(ipv4_hashtbl)
9019873f47SAndrey V. Elsukov #define V_ipv4_srchashtbl VNET(ipv4_srchashtbl)
91a5185adeSAndrey V. Elsukov #define GRE_HASH(src, dst) (V_ipv4_hashtbl[\
92a5185adeSAndrey V. Elsukov in_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
9319873f47SAndrey V. Elsukov #define GRE_SRCHASH(src) (V_ipv4_srchashtbl[\
9419873f47SAndrey V. Elsukov fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
95aee793eeSAndrey V. Elsukov #define GRE_SOCKHASH(src) (V_ipv4_sockets[\
96aee793eeSAndrey V. Elsukov fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
97a5185adeSAndrey V. Elsukov #define GRE_HASH_SC(sc) GRE_HASH((sc)->gre_oip.ip_src.s_addr,\
98a5185adeSAndrey V. Elsukov (sc)->gre_oip.ip_dst.s_addr)
998e96e13eSMaxim Sobolev
100a5185adeSAndrey V. Elsukov static uint32_t
in_gre_hashval(in_addr_t src,in_addr_t dst)101a5185adeSAndrey V. Elsukov in_gre_hashval(in_addr_t src, in_addr_t dst)
102a5185adeSAndrey V. Elsukov {
103a5185adeSAndrey V. Elsukov uint32_t ret;
104a5185adeSAndrey V. Elsukov
105a5185adeSAndrey V. Elsukov ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
106a5185adeSAndrey V. Elsukov return (fnv_32_buf(&dst, sizeof(dst), ret));
107a5185adeSAndrey V. Elsukov }
108a5185adeSAndrey V. Elsukov
109aee793eeSAndrey V. Elsukov static struct gre_socket*
in_gre_lookup_socket(in_addr_t addr)110aee793eeSAndrey V. Elsukov in_gre_lookup_socket(in_addr_t addr)
111a5185adeSAndrey V. Elsukov {
112aee793eeSAndrey V. Elsukov struct gre_socket *gs;
113aee793eeSAndrey V. Elsukov struct in_gre_socket *s;
114aee793eeSAndrey V. Elsukov
115aee793eeSAndrey V. Elsukov CK_LIST_FOREACH(gs, &GRE_SOCKHASH(addr), chain) {
116aee793eeSAndrey V. Elsukov s = __containerof(gs, struct in_gre_socket, base);
117aee793eeSAndrey V. Elsukov if (s->addr == addr)
118aee793eeSAndrey V. Elsukov break;
119aee793eeSAndrey V. Elsukov }
120aee793eeSAndrey V. Elsukov return (gs);
121aee793eeSAndrey V. Elsukov }
122aee793eeSAndrey V. Elsukov
123aee793eeSAndrey V. Elsukov static int
in_gre_checkdup(const struct gre_softc * sc,in_addr_t src,in_addr_t dst,uint32_t opts)124aee793eeSAndrey V. Elsukov in_gre_checkdup(const struct gre_softc *sc, in_addr_t src, in_addr_t dst,
125aee793eeSAndrey V. Elsukov uint32_t opts)
126aee793eeSAndrey V. Elsukov {
127aee793eeSAndrey V. Elsukov struct gre_list *head;
128a5185adeSAndrey V. Elsukov struct gre_softc *tmp;
129aee793eeSAndrey V. Elsukov struct gre_socket *gs;
130a5185adeSAndrey V. Elsukov
131a5185adeSAndrey V. Elsukov if (sc->gre_family == AF_INET &&
132a5185adeSAndrey V. Elsukov sc->gre_oip.ip_src.s_addr == src &&
133aee793eeSAndrey V. Elsukov sc->gre_oip.ip_dst.s_addr == dst &&
134aee793eeSAndrey V. Elsukov (sc->gre_options & GRE_UDPENCAP) == (opts & GRE_UDPENCAP))
135a5185adeSAndrey V. Elsukov return (EEXIST);
136a5185adeSAndrey V. Elsukov
137aee793eeSAndrey V. Elsukov if (opts & GRE_UDPENCAP) {
138aee793eeSAndrey V. Elsukov gs = in_gre_lookup_socket(src);
139aee793eeSAndrey V. Elsukov if (gs == NULL)
140aee793eeSAndrey V. Elsukov return (0);
141aee793eeSAndrey V. Elsukov head = &gs->list;
142aee793eeSAndrey V. Elsukov } else
143aee793eeSAndrey V. Elsukov head = &GRE_HASH(src, dst);
144aee793eeSAndrey V. Elsukov
145aee793eeSAndrey V. Elsukov CK_LIST_FOREACH(tmp, head, chain) {
146a5185adeSAndrey V. Elsukov if (tmp == sc)
147a5185adeSAndrey V. Elsukov continue;
148a5185adeSAndrey V. Elsukov if (tmp->gre_oip.ip_src.s_addr == src &&
149a5185adeSAndrey V. Elsukov tmp->gre_oip.ip_dst.s_addr == dst)
150a5185adeSAndrey V. Elsukov return (EADDRNOTAVAIL);
151a5185adeSAndrey V. Elsukov }
152a5185adeSAndrey V. Elsukov return (0);
153a5185adeSAndrey V. Elsukov }
154a5185adeSAndrey V. Elsukov
155a5185adeSAndrey V. Elsukov static int
in_gre_lookup(const struct mbuf * m,int off,int proto,void ** arg)156a5185adeSAndrey V. Elsukov in_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
157a5185adeSAndrey V. Elsukov {
158a5185adeSAndrey V. Elsukov const struct ip *ip;
159a5185adeSAndrey V. Elsukov struct gre_softc *sc;
160a5185adeSAndrey V. Elsukov
1616e081509SAndrey V. Elsukov if (V_ipv4_hashtbl == NULL)
1626e081509SAndrey V. Elsukov return (0);
1636e081509SAndrey V. Elsukov
16497168be8SGleb Smirnoff NET_EPOCH_ASSERT();
165a5185adeSAndrey V. Elsukov ip = mtod(m, const struct ip *);
166a5185adeSAndrey V. Elsukov CK_LIST_FOREACH(sc, &GRE_HASH(ip->ip_dst.s_addr,
167a5185adeSAndrey V. Elsukov ip->ip_src.s_addr), chain) {
168a5185adeSAndrey V. Elsukov /*
169a5185adeSAndrey V. Elsukov * This is an inbound packet, its ip_dst is source address
170a5185adeSAndrey V. Elsukov * in softc.
171a5185adeSAndrey V. Elsukov */
172a5185adeSAndrey V. Elsukov if (sc->gre_oip.ip_src.s_addr == ip->ip_dst.s_addr &&
173a5185adeSAndrey V. Elsukov sc->gre_oip.ip_dst.s_addr == ip->ip_src.s_addr) {
174f325335cSAndrey V. Elsukov if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
175f325335cSAndrey V. Elsukov return (0);
176a5185adeSAndrey V. Elsukov *arg = sc;
177a5185adeSAndrey V. Elsukov return (ENCAP_DRV_LOOKUP);
178a5185adeSAndrey V. Elsukov }
179a5185adeSAndrey V. Elsukov }
180f325335cSAndrey V. Elsukov return (0);
1818e96e13eSMaxim Sobolev }
1828e96e13eSMaxim Sobolev
18319873f47SAndrey V. Elsukov /*
18419873f47SAndrey V. Elsukov * Check that ingress address belongs to local host.
18519873f47SAndrey V. Elsukov */
18619873f47SAndrey V. Elsukov static void
in_gre_set_running(struct gre_softc * sc)18719873f47SAndrey V. Elsukov in_gre_set_running(struct gre_softc *sc)
18819873f47SAndrey V. Elsukov {
18919873f47SAndrey V. Elsukov
19019873f47SAndrey V. Elsukov if (in_localip(sc->gre_oip.ip_src))
19119873f47SAndrey V. Elsukov GRE2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
19219873f47SAndrey V. Elsukov else
19319873f47SAndrey V. Elsukov GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
19419873f47SAndrey V. Elsukov }
19519873f47SAndrey V. Elsukov
19619873f47SAndrey V. Elsukov /*
19719873f47SAndrey V. Elsukov * ifaddr_event handler.
19819873f47SAndrey V. Elsukov * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
19919873f47SAndrey V. Elsukov * source address spoofing.
20019873f47SAndrey V. Elsukov */
20119873f47SAndrey V. Elsukov static void
in_gre_srcaddr(void * arg __unused,const struct sockaddr * sa,int event __unused)20219873f47SAndrey V. Elsukov in_gre_srcaddr(void *arg __unused, const struct sockaddr *sa,
20319873f47SAndrey V. Elsukov int event __unused)
20419873f47SAndrey V. Elsukov {
20519873f47SAndrey V. Elsukov const struct sockaddr_in *sin;
20619873f47SAndrey V. Elsukov struct gre_softc *sc;
20719873f47SAndrey V. Elsukov
2088796e291SAndrey V. Elsukov /* Check that VNET is ready */
2098796e291SAndrey V. Elsukov if (V_ipv4_hashtbl == NULL)
21019873f47SAndrey V. Elsukov return;
21119873f47SAndrey V. Elsukov
21297168be8SGleb Smirnoff NET_EPOCH_ASSERT();
21319873f47SAndrey V. Elsukov sin = (const struct sockaddr_in *)sa;
21419873f47SAndrey V. Elsukov CK_LIST_FOREACH(sc, &GRE_SRCHASH(sin->sin_addr.s_addr), srchash) {
21519873f47SAndrey V. Elsukov if (sc->gre_oip.ip_src.s_addr != sin->sin_addr.s_addr)
21619873f47SAndrey V. Elsukov continue;
21719873f47SAndrey V. Elsukov in_gre_set_running(sc);
21819873f47SAndrey V. Elsukov }
21919873f47SAndrey V. Elsukov }
22019873f47SAndrey V. Elsukov
221742e7210SKristof Provost static bool
in_gre_udp_input(struct mbuf * m,int off,struct inpcb * inp,const struct sockaddr * sa,void * ctx)222aee793eeSAndrey V. Elsukov in_gre_udp_input(struct mbuf *m, int off, struct inpcb *inp,
223aee793eeSAndrey V. Elsukov const struct sockaddr *sa, void *ctx)
224aee793eeSAndrey V. Elsukov {
225aee793eeSAndrey V. Elsukov struct gre_socket *gs;
226aee793eeSAndrey V. Elsukov struct gre_softc *sc;
227aee793eeSAndrey V. Elsukov in_addr_t dst;
228aee793eeSAndrey V. Elsukov
229db0ac6deSCy Schubert NET_EPOCH_ASSERT();
230aee793eeSAndrey V. Elsukov
231aee793eeSAndrey V. Elsukov gs = (struct gre_socket *)ctx;
232aee793eeSAndrey V. Elsukov dst = ((const struct sockaddr_in *)sa)->sin_addr.s_addr;
233aee793eeSAndrey V. Elsukov CK_LIST_FOREACH(sc, &gs->list, chain) {
234aee793eeSAndrey V. Elsukov if (sc->gre_oip.ip_dst.s_addr == dst)
235aee793eeSAndrey V. Elsukov break;
236aee793eeSAndrey V. Elsukov }
237aee793eeSAndrey V. Elsukov if (sc != NULL && (GRE2IFP(sc)->if_flags & IFF_UP) != 0){
238aee793eeSAndrey V. Elsukov gre_input(m, off + sizeof(struct udphdr), IPPROTO_UDP, sc);
239742e7210SKristof Provost return (true);
240aee793eeSAndrey V. Elsukov }
241aee793eeSAndrey V. Elsukov m_freem(m);
242742e7210SKristof Provost
243742e7210SKristof Provost return (true);
244aee793eeSAndrey V. Elsukov }
245aee793eeSAndrey V. Elsukov
246aee793eeSAndrey V. Elsukov static int
in_gre_setup_socket(struct gre_softc * sc)247aee793eeSAndrey V. Elsukov in_gre_setup_socket(struct gre_softc *sc)
248aee793eeSAndrey V. Elsukov {
249aee793eeSAndrey V. Elsukov struct sockopt sopt;
250aee793eeSAndrey V. Elsukov struct sockaddr_in sin;
251aee793eeSAndrey V. Elsukov struct in_gre_socket *s;
252aee793eeSAndrey V. Elsukov struct gre_socket *gs;
253aee793eeSAndrey V. Elsukov in_addr_t addr;
254aee793eeSAndrey V. Elsukov int error, value;
255aee793eeSAndrey V. Elsukov
256aee793eeSAndrey V. Elsukov /*
257aee793eeSAndrey V. Elsukov * NOTE: we are protected with gre_ioctl_sx lock.
258aee793eeSAndrey V. Elsukov *
259aee793eeSAndrey V. Elsukov * First check that socket is already configured.
260fa7de6dcSGordon Bergling * If so, check that source address was not changed.
261aee793eeSAndrey V. Elsukov * If address is different, check that there are no other tunnels
262aee793eeSAndrey V. Elsukov * and close socket.
263aee793eeSAndrey V. Elsukov */
264aee793eeSAndrey V. Elsukov addr = sc->gre_oip.ip_src.s_addr;
265aee793eeSAndrey V. Elsukov gs = sc->gre_so;
266aee793eeSAndrey V. Elsukov if (gs != NULL) {
267aee793eeSAndrey V. Elsukov s = __containerof(gs, struct in_gre_socket, base);
268aee793eeSAndrey V. Elsukov if (s->addr != addr) {
269aee793eeSAndrey V. Elsukov if (CK_LIST_EMPTY(&gs->list)) {
270aee793eeSAndrey V. Elsukov CK_LIST_REMOVE(gs, chain);
271aee793eeSAndrey V. Elsukov soclose(gs->so);
2722a4bd982SGleb Smirnoff NET_EPOCH_CALL(gre_sofree, &gs->epoch_ctx);
273aee793eeSAndrey V. Elsukov }
274aee793eeSAndrey V. Elsukov gs = sc->gre_so = NULL;
275aee793eeSAndrey V. Elsukov }
276aee793eeSAndrey V. Elsukov }
277aee793eeSAndrey V. Elsukov
278aee793eeSAndrey V. Elsukov if (gs == NULL) {
279aee793eeSAndrey V. Elsukov /*
280aee793eeSAndrey V. Elsukov * Check that socket for given address is already
281aee793eeSAndrey V. Elsukov * configured.
282aee793eeSAndrey V. Elsukov */
283aee793eeSAndrey V. Elsukov gs = in_gre_lookup_socket(addr);
284aee793eeSAndrey V. Elsukov if (gs == NULL) {
285aee793eeSAndrey V. Elsukov s = malloc(sizeof(*s), M_GRE, M_WAITOK | M_ZERO);
286aee793eeSAndrey V. Elsukov s->addr = addr;
287aee793eeSAndrey V. Elsukov gs = &s->base;
288aee793eeSAndrey V. Elsukov
289aee793eeSAndrey V. Elsukov error = socreate(sc->gre_family, &gs->so,
290aee793eeSAndrey V. Elsukov SOCK_DGRAM, IPPROTO_UDP, curthread->td_ucred,
291aee793eeSAndrey V. Elsukov curthread);
292aee793eeSAndrey V. Elsukov if (error != 0) {
293aee793eeSAndrey V. Elsukov if_printf(GRE2IFP(sc),
294aee793eeSAndrey V. Elsukov "cannot create socket: %d\n", error);
295aee793eeSAndrey V. Elsukov free(s, M_GRE);
296aee793eeSAndrey V. Elsukov return (error);
297aee793eeSAndrey V. Elsukov }
298aee793eeSAndrey V. Elsukov
299aee793eeSAndrey V. Elsukov error = udp_set_kernel_tunneling(gs->so,
300aee793eeSAndrey V. Elsukov in_gre_udp_input, NULL, gs);
301aee793eeSAndrey V. Elsukov if (error != 0) {
302aee793eeSAndrey V. Elsukov if_printf(GRE2IFP(sc),
303aee793eeSAndrey V. Elsukov "cannot set UDP tunneling: %d\n", error);
304aee793eeSAndrey V. Elsukov goto fail;
305aee793eeSAndrey V. Elsukov }
306aee793eeSAndrey V. Elsukov
307aee793eeSAndrey V. Elsukov memset(&sopt, 0, sizeof(sopt));
308aee793eeSAndrey V. Elsukov sopt.sopt_dir = SOPT_SET;
309aee793eeSAndrey V. Elsukov sopt.sopt_level = IPPROTO_IP;
310aee793eeSAndrey V. Elsukov sopt.sopt_name = IP_BINDANY;
311aee793eeSAndrey V. Elsukov sopt.sopt_val = &value;
312aee793eeSAndrey V. Elsukov sopt.sopt_valsize = sizeof(value);
313aee793eeSAndrey V. Elsukov value = 1;
314aee793eeSAndrey V. Elsukov error = sosetopt(gs->so, &sopt);
315aee793eeSAndrey V. Elsukov if (error != 0) {
316aee793eeSAndrey V. Elsukov if_printf(GRE2IFP(sc),
317aee793eeSAndrey V. Elsukov "cannot set IP_BINDANY opt: %d\n", error);
318aee793eeSAndrey V. Elsukov goto fail;
319aee793eeSAndrey V. Elsukov }
320aee793eeSAndrey V. Elsukov
321aee793eeSAndrey V. Elsukov memset(&sin, 0, sizeof(sin));
322aee793eeSAndrey V. Elsukov sin.sin_family = AF_INET;
323aee793eeSAndrey V. Elsukov sin.sin_len = sizeof(sin);
324aee793eeSAndrey V. Elsukov sin.sin_addr.s_addr = addr;
325aee793eeSAndrey V. Elsukov sin.sin_port = htons(GRE_UDPPORT);
326aee793eeSAndrey V. Elsukov error = sobind(gs->so, (struct sockaddr *)&sin,
327aee793eeSAndrey V. Elsukov curthread);
328aee793eeSAndrey V. Elsukov if (error != 0) {
329aee793eeSAndrey V. Elsukov if_printf(GRE2IFP(sc),
330aee793eeSAndrey V. Elsukov "cannot bind socket: %d\n", error);
331aee793eeSAndrey V. Elsukov goto fail;
332aee793eeSAndrey V. Elsukov }
333aee793eeSAndrey V. Elsukov /* Add socket to the chain */
334aee793eeSAndrey V. Elsukov CK_LIST_INSERT_HEAD(&GRE_SOCKHASH(addr), gs, chain);
335aee793eeSAndrey V. Elsukov }
336aee793eeSAndrey V. Elsukov }
337aee793eeSAndrey V. Elsukov
338aee793eeSAndrey V. Elsukov /* Add softc to the socket's list */
339aee793eeSAndrey V. Elsukov CK_LIST_INSERT_HEAD(&gs->list, sc, chain);
340aee793eeSAndrey V. Elsukov sc->gre_so = gs;
341aee793eeSAndrey V. Elsukov return (0);
342aee793eeSAndrey V. Elsukov fail:
343aee793eeSAndrey V. Elsukov soclose(gs->so);
344aee793eeSAndrey V. Elsukov free(s, M_GRE);
345aee793eeSAndrey V. Elsukov return (error);
346aee793eeSAndrey V. Elsukov }
347aee793eeSAndrey V. Elsukov
348aee793eeSAndrey V. Elsukov static int
in_gre_attach(struct gre_softc * sc)349a5185adeSAndrey V. Elsukov in_gre_attach(struct gre_softc *sc)
350a5185adeSAndrey V. Elsukov {
351c8ee75f2SGleb Smirnoff struct epoch_tracker et;
352aee793eeSAndrey V. Elsukov struct grehdr *gh;
353aee793eeSAndrey V. Elsukov int error;
354a5185adeSAndrey V. Elsukov
355aee793eeSAndrey V. Elsukov if (sc->gre_options & GRE_UDPENCAP) {
356aee793eeSAndrey V. Elsukov sc->gre_csumflags = CSUM_UDP;
357aee793eeSAndrey V. Elsukov sc->gre_hlen = sizeof(struct greudp);
358aee793eeSAndrey V. Elsukov sc->gre_oip.ip_p = IPPROTO_UDP;
359aee793eeSAndrey V. Elsukov gh = &sc->gre_udphdr->gi_gre;
360aee793eeSAndrey V. Elsukov gre_update_udphdr(sc, &sc->gre_udp,
361aee793eeSAndrey V. Elsukov in_pseudo(sc->gre_oip.ip_src.s_addr,
362aee793eeSAndrey V. Elsukov sc->gre_oip.ip_dst.s_addr, 0));
363aee793eeSAndrey V. Elsukov } else {
364a5185adeSAndrey V. Elsukov sc->gre_hlen = sizeof(struct greip);
365aee793eeSAndrey V. Elsukov sc->gre_oip.ip_p = IPPROTO_GRE;
366aee793eeSAndrey V. Elsukov gh = &sc->gre_iphdr->gi_gre;
367aee793eeSAndrey V. Elsukov }
368a5185adeSAndrey V. Elsukov sc->gre_oip.ip_v = IPVERSION;
369a5185adeSAndrey V. Elsukov sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
370aee793eeSAndrey V. Elsukov gre_update_hdr(sc, gh);
371aee793eeSAndrey V. Elsukov
372aee793eeSAndrey V. Elsukov /*
373aee793eeSAndrey V. Elsukov * If we return error, this means that sc is not linked,
374aee793eeSAndrey V. Elsukov * and caller should reset gre_family and free(sc->gre_hdr).
375aee793eeSAndrey V. Elsukov */
376aee793eeSAndrey V. Elsukov if (sc->gre_options & GRE_UDPENCAP) {
377aee793eeSAndrey V. Elsukov error = in_gre_setup_socket(sc);
378aee793eeSAndrey V. Elsukov if (error != 0)
379aee793eeSAndrey V. Elsukov return (error);
380aee793eeSAndrey V. Elsukov } else
381a5185adeSAndrey V. Elsukov CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
38219873f47SAndrey V. Elsukov CK_LIST_INSERT_HEAD(&GRE_SRCHASH(sc->gre_oip.ip_src.s_addr),
38319873f47SAndrey V. Elsukov sc, srchash);
384aee793eeSAndrey V. Elsukov
385aee793eeSAndrey V. Elsukov /* Set IFF_DRV_RUNNING if interface is ready */
386c8ee75f2SGleb Smirnoff NET_EPOCH_ENTER(et);
387aee793eeSAndrey V. Elsukov in_gre_set_running(sc);
388c8ee75f2SGleb Smirnoff NET_EPOCH_EXIT(et);
389aee793eeSAndrey V. Elsukov return (0);
390a5185adeSAndrey V. Elsukov }
391a5185adeSAndrey V. Elsukov
392aee793eeSAndrey V. Elsukov int
in_gre_setopts(struct gre_softc * sc,u_long cmd,uint32_t value)393a5185adeSAndrey V. Elsukov in_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
394a5185adeSAndrey V. Elsukov {
395aee793eeSAndrey V. Elsukov int error;
396a5185adeSAndrey V. Elsukov
397a5185adeSAndrey V. Elsukov /* NOTE: we are protected with gre_ioctl_sx lock */
398aee793eeSAndrey V. Elsukov MPASS(cmd == GRESKEY || cmd == GRESOPTS || cmd == GRESPORT);
399a5185adeSAndrey V. Elsukov MPASS(sc->gre_family == AF_INET);
400aee793eeSAndrey V. Elsukov
401aee793eeSAndrey V. Elsukov /*
402aee793eeSAndrey V. Elsukov * If we are going to change encapsulation protocol, do check
403aee793eeSAndrey V. Elsukov * for duplicate tunnels. Return EEXIST here to do not confuse
404aee793eeSAndrey V. Elsukov * user.
405aee793eeSAndrey V. Elsukov */
406aee793eeSAndrey V. Elsukov if (cmd == GRESOPTS &&
407aee793eeSAndrey V. Elsukov (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP) &&
408aee793eeSAndrey V. Elsukov in_gre_checkdup(sc, sc->gre_oip.ip_src.s_addr,
409aee793eeSAndrey V. Elsukov sc->gre_oip.ip_dst.s_addr, value) == EADDRNOTAVAIL)
410aee793eeSAndrey V. Elsukov return (EEXIST);
411aee793eeSAndrey V. Elsukov
412a5185adeSAndrey V. Elsukov CK_LIST_REMOVE(sc, chain);
41319873f47SAndrey V. Elsukov CK_LIST_REMOVE(sc, srchash);
414a5185adeSAndrey V. Elsukov GRE_WAIT();
415aee793eeSAndrey V. Elsukov switch (cmd) {
416aee793eeSAndrey V. Elsukov case GRESKEY:
417a5185adeSAndrey V. Elsukov sc->gre_key = value;
418aee793eeSAndrey V. Elsukov break;
419aee793eeSAndrey V. Elsukov case GRESOPTS:
420a5185adeSAndrey V. Elsukov sc->gre_options = value;
421aee793eeSAndrey V. Elsukov break;
422aee793eeSAndrey V. Elsukov case GRESPORT:
423aee793eeSAndrey V. Elsukov sc->gre_port = value;
424aee793eeSAndrey V. Elsukov break;
425aee793eeSAndrey V. Elsukov }
426aee793eeSAndrey V. Elsukov error = in_gre_attach(sc);
427aee793eeSAndrey V. Elsukov if (error != 0) {
428aee793eeSAndrey V. Elsukov sc->gre_family = 0;
429aee793eeSAndrey V. Elsukov free(sc->gre_hdr, M_GRE);
430aee793eeSAndrey V. Elsukov }
431aee793eeSAndrey V. Elsukov return (error);
432a5185adeSAndrey V. Elsukov }
433a5185adeSAndrey V. Elsukov
434a5185adeSAndrey V. Elsukov int
in_gre_ioctl(struct gre_softc * sc,u_long cmd,caddr_t data)435a5185adeSAndrey V. Elsukov in_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
436a5185adeSAndrey V. Elsukov {
437a5185adeSAndrey V. Elsukov struct ifreq *ifr = (struct ifreq *)data;
438a5185adeSAndrey V. Elsukov struct sockaddr_in *dst, *src;
439a5185adeSAndrey V. Elsukov struct ip *ip;
440a5185adeSAndrey V. Elsukov int error;
441a5185adeSAndrey V. Elsukov
442a5185adeSAndrey V. Elsukov /* NOTE: we are protected with gre_ioctl_sx lock */
443a5185adeSAndrey V. Elsukov error = EINVAL;
444a5185adeSAndrey V. Elsukov switch (cmd) {
445a5185adeSAndrey V. Elsukov case SIOCSIFPHYADDR:
446a5185adeSAndrey V. Elsukov src = &((struct in_aliasreq *)data)->ifra_addr;
447a5185adeSAndrey V. Elsukov dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
448a5185adeSAndrey V. Elsukov
449a5185adeSAndrey V. Elsukov /* sanity checks */
450a5185adeSAndrey V. Elsukov if (src->sin_family != dst->sin_family ||
451a5185adeSAndrey V. Elsukov src->sin_family != AF_INET ||
452a5185adeSAndrey V. Elsukov src->sin_len != dst->sin_len ||
453a5185adeSAndrey V. Elsukov src->sin_len != sizeof(*src))
454a5185adeSAndrey V. Elsukov break;
455a5185adeSAndrey V. Elsukov if (src->sin_addr.s_addr == INADDR_ANY ||
456a5185adeSAndrey V. Elsukov dst->sin_addr.s_addr == INADDR_ANY) {
457a5185adeSAndrey V. Elsukov error = EADDRNOTAVAIL;
458a5185adeSAndrey V. Elsukov break;
459a5185adeSAndrey V. Elsukov }
46019873f47SAndrey V. Elsukov if (V_ipv4_hashtbl == NULL) {
461a5185adeSAndrey V. Elsukov V_ipv4_hashtbl = gre_hashinit();
46219873f47SAndrey V. Elsukov V_ipv4_srchashtbl = gre_hashinit();
463aee793eeSAndrey V. Elsukov V_ipv4_sockets = (struct gre_sockets *)gre_hashinit();
46419873f47SAndrey V. Elsukov }
465a5185adeSAndrey V. Elsukov error = in_gre_checkdup(sc, src->sin_addr.s_addr,
466aee793eeSAndrey V. Elsukov dst->sin_addr.s_addr, sc->gre_options);
467a5185adeSAndrey V. Elsukov if (error == EADDRNOTAVAIL)
468a5185adeSAndrey V. Elsukov break;
469a5185adeSAndrey V. Elsukov if (error == EEXIST) {
470a5185adeSAndrey V. Elsukov /* Addresses are the same. Just return. */
471a5185adeSAndrey V. Elsukov error = 0;
472a5185adeSAndrey V. Elsukov break;
473a5185adeSAndrey V. Elsukov }
474aee793eeSAndrey V. Elsukov ip = malloc(sizeof(struct greudp) + 3 * sizeof(uint32_t),
475a5185adeSAndrey V. Elsukov M_GRE, M_WAITOK | M_ZERO);
476a5185adeSAndrey V. Elsukov ip->ip_src.s_addr = src->sin_addr.s_addr;
477a5185adeSAndrey V. Elsukov ip->ip_dst.s_addr = dst->sin_addr.s_addr;
478a5185adeSAndrey V. Elsukov if (sc->gre_family != 0) {
479a5185adeSAndrey V. Elsukov /* Detach existing tunnel first */
480a5185adeSAndrey V. Elsukov CK_LIST_REMOVE(sc, chain);
48119873f47SAndrey V. Elsukov CK_LIST_REMOVE(sc, srchash);
482a5185adeSAndrey V. Elsukov GRE_WAIT();
483a5185adeSAndrey V. Elsukov free(sc->gre_hdr, M_GRE);
484a5185adeSAndrey V. Elsukov /* XXX: should we notify about link state change? */
485a5185adeSAndrey V. Elsukov }
486a5185adeSAndrey V. Elsukov sc->gre_family = AF_INET;
487a5185adeSAndrey V. Elsukov sc->gre_hdr = ip;
488a5185adeSAndrey V. Elsukov sc->gre_oseq = 0;
489a5185adeSAndrey V. Elsukov sc->gre_iseq = UINT32_MAX;
490aee793eeSAndrey V. Elsukov error = in_gre_attach(sc);
491aee793eeSAndrey V. Elsukov if (error != 0) {
492aee793eeSAndrey V. Elsukov sc->gre_family = 0;
493aee793eeSAndrey V. Elsukov free(sc->gre_hdr, M_GRE);
494aee793eeSAndrey V. Elsukov }
495a5185adeSAndrey V. Elsukov break;
496a5185adeSAndrey V. Elsukov case SIOCGIFPSRCADDR:
497a5185adeSAndrey V. Elsukov case SIOCGIFPDSTADDR:
498a5185adeSAndrey V. Elsukov if (sc->gre_family != AF_INET) {
499a5185adeSAndrey V. Elsukov error = EADDRNOTAVAIL;
500a5185adeSAndrey V. Elsukov break;
501a5185adeSAndrey V. Elsukov }
502a5185adeSAndrey V. Elsukov src = (struct sockaddr_in *)&ifr->ifr_addr;
503a5185adeSAndrey V. Elsukov memset(src, 0, sizeof(*src));
504a5185adeSAndrey V. Elsukov src->sin_family = AF_INET;
505a5185adeSAndrey V. Elsukov src->sin_len = sizeof(*src);
506a5185adeSAndrey V. Elsukov src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
507a5185adeSAndrey V. Elsukov sc->gre_oip.ip_src: sc->gre_oip.ip_dst;
508a5185adeSAndrey V. Elsukov error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
509a5185adeSAndrey V. Elsukov if (error != 0)
510a5185adeSAndrey V. Elsukov memset(src, 0, sizeof(*src));
511a5185adeSAndrey V. Elsukov break;
512a5185adeSAndrey V. Elsukov }
513a5185adeSAndrey V. Elsukov return (error);
514a5185adeSAndrey V. Elsukov }
515a5185adeSAndrey V. Elsukov
516f325335cSAndrey V. Elsukov int
in_gre_output(struct mbuf * m,int af,int hlen)517f325335cSAndrey V. Elsukov in_gre_output(struct mbuf *m, int af, int hlen)
518f325335cSAndrey V. Elsukov {
519f325335cSAndrey V. Elsukov struct greip *gi;
52073d7ddbcSMaxim Sobolev
521f325335cSAndrey V. Elsukov gi = mtod(m, struct greip *);
522f325335cSAndrey V. Elsukov switch (af) {
523f325335cSAndrey V. Elsukov case AF_INET:
524f325335cSAndrey V. Elsukov /*
525f325335cSAndrey V. Elsukov * gre_transmit() has used M_PREPEND() that doesn't guarantee
526f325335cSAndrey V. Elsukov * m_data is contiguous more than hlen bytes. Use m_copydata()
527f325335cSAndrey V. Elsukov * here to avoid m_pullup().
528f325335cSAndrey V. Elsukov */
529f325335cSAndrey V. Elsukov m_copydata(m, hlen + offsetof(struct ip, ip_tos),
530f325335cSAndrey V. Elsukov sizeof(u_char), &gi->gi_ip.ip_tos);
531f325335cSAndrey V. Elsukov m_copydata(m, hlen + offsetof(struct ip, ip_id),
532f325335cSAndrey V. Elsukov sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id);
5338e96e13eSMaxim Sobolev break;
5349e669156SBjoern A. Zeeb #ifdef INET6
535f325335cSAndrey V. Elsukov case AF_INET6:
536f325335cSAndrey V. Elsukov gi->gi_ip.ip_tos = 0; /* XXX */
537*70703aa9Sacazuc ip_fillid(&gi->gi_ip, V_ip_random_id);
5389e669156SBjoern A. Zeeb break;
5399e669156SBjoern A. Zeeb #endif
5408e96e13eSMaxim Sobolev }
541f325335cSAndrey V. Elsukov gi->gi_ip.ip_ttl = V_ip_gre_ttl;
542f325335cSAndrey V. Elsukov gi->gi_ip.ip_len = htons(m->m_pkthdr.len);
543f325335cSAndrey V. Elsukov return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL));
5448e96e13eSMaxim Sobolev }
5458e96e13eSMaxim Sobolev
54619873f47SAndrey V. Elsukov static const struct srcaddrtab *ipv4_srcaddrtab = NULL;
547a5185adeSAndrey V. Elsukov static const struct encaptab *ecookie = NULL;
5486d8fdfa9SAndrey V. Elsukov static const struct encap_config ipv4_encap_cfg = {
5496d8fdfa9SAndrey V. Elsukov .proto = IPPROTO_GRE,
5506d8fdfa9SAndrey V. Elsukov .min_length = sizeof(struct greip) + sizeof(struct ip),
551a5185adeSAndrey V. Elsukov .exact_match = ENCAP_DRV_LOOKUP,
552a5185adeSAndrey V. Elsukov .lookup = in_gre_lookup,
5536d8fdfa9SAndrey V. Elsukov .input = gre_input
5546d8fdfa9SAndrey V. Elsukov };
5556d8fdfa9SAndrey V. Elsukov
556a5185adeSAndrey V. Elsukov void
in_gre_init(void)557a5185adeSAndrey V. Elsukov in_gre_init(void)
5588e96e13eSMaxim Sobolev {
5598e96e13eSMaxim Sobolev
560a5185adeSAndrey V. Elsukov if (!IS_DEFAULT_VNET(curvnet))
561a5185adeSAndrey V. Elsukov return;
56219873f47SAndrey V. Elsukov ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gre_srcaddr,
56319873f47SAndrey V. Elsukov NULL, M_WAITOK);
564a5185adeSAndrey V. Elsukov ecookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
565a5185adeSAndrey V. Elsukov }
566a5185adeSAndrey V. Elsukov
567a5185adeSAndrey V. Elsukov void
in_gre_uninit(void)568a5185adeSAndrey V. Elsukov in_gre_uninit(void)
569a5185adeSAndrey V. Elsukov {
570a5185adeSAndrey V. Elsukov
57119873f47SAndrey V. Elsukov if (IS_DEFAULT_VNET(curvnet)) {
572a5185adeSAndrey V. Elsukov ip_encap_detach(ecookie);
57319873f47SAndrey V. Elsukov ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
57419873f47SAndrey V. Elsukov }
57519873f47SAndrey V. Elsukov if (V_ipv4_hashtbl != NULL) {
576a5185adeSAndrey V. Elsukov gre_hashdestroy(V_ipv4_hashtbl);
5778796e291SAndrey V. Elsukov V_ipv4_hashtbl = NULL;
5788796e291SAndrey V. Elsukov GRE_WAIT();
57919873f47SAndrey V. Elsukov gre_hashdestroy(V_ipv4_srchashtbl);
580aee793eeSAndrey V. Elsukov gre_hashdestroy((struct gre_list *)V_ipv4_sockets);
58119873f47SAndrey V. Elsukov }
5828e96e13eSMaxim Sobolev }
583