xref: /freebsd-13-stable/sys/netinet/ip_gre.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Heiko W.Rupp <hwr@pilhuhn.de>
10  *
11  * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $
35  */
36 
37 #include <sys/cdefs.h>
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 
41 #include <sys/param.h>
42 #include <sys/jail.h>
43 #include <sys/systm.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sockio.h>
47 #include <sys/mbuf.h>
48 #include <sys/errno.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/malloc.h>
52 #include <sys/proc.h>
53 
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/vnet.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_encap.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/udp.h>
65 #include <netinet/udp_var.h>
66 
67 #ifdef INET6
68 #include <netinet/ip6.h>
69 #endif
70 
71 #include <net/if_gre.h>
72 #include <machine/in_cksum.h>
73 
74 #define	GRE_TTL			30
75 VNET_DEFINE(int, ip_gre_ttl) = GRE_TTL;
76 #define	V_ip_gre_ttl		VNET(ip_gre_ttl)
77 SYSCTL_INT(_net_inet_ip, OID_AUTO, grettl, CTLFLAG_VNET | CTLFLAG_RW,
78     &VNET_NAME(ip_gre_ttl), 0, "Default TTL value for encapsulated packets");
79 
80 struct in_gre_socket {
81 	struct gre_socket		base;
82 	in_addr_t			addr;
83 };
84 VNET_DEFINE_STATIC(struct gre_sockets *, ipv4_sockets) = NULL;
85 VNET_DEFINE_STATIC(struct gre_list *, ipv4_hashtbl) = NULL;
86 VNET_DEFINE_STATIC(struct gre_list *, ipv4_srchashtbl) = NULL;
87 #define	V_ipv4_sockets		VNET(ipv4_sockets)
88 #define	V_ipv4_hashtbl		VNET(ipv4_hashtbl)
89 #define	V_ipv4_srchashtbl	VNET(ipv4_srchashtbl)
90 #define	GRE_HASH(src, dst)	(V_ipv4_hashtbl[\
91     in_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
92 #define	GRE_SRCHASH(src)	(V_ipv4_srchashtbl[\
93     fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
94 #define	GRE_SOCKHASH(src)	(V_ipv4_sockets[\
95     fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
96 #define	GRE_HASH_SC(sc)		GRE_HASH((sc)->gre_oip.ip_src.s_addr,\
97     (sc)->gre_oip.ip_dst.s_addr)
98 
99 static uint32_t
in_gre_hashval(in_addr_t src,in_addr_t dst)100 in_gre_hashval(in_addr_t src, in_addr_t dst)
101 {
102 	uint32_t ret;
103 
104 	ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
105 	return (fnv_32_buf(&dst, sizeof(dst), ret));
106 }
107 
108 static struct gre_socket*
in_gre_lookup_socket(in_addr_t addr)109 in_gre_lookup_socket(in_addr_t addr)
110 {
111 	struct gre_socket *gs;
112 	struct in_gre_socket *s;
113 
114 	CK_LIST_FOREACH(gs, &GRE_SOCKHASH(addr), chain) {
115 		s = __containerof(gs, struct in_gre_socket, base);
116 		if (s->addr == addr)
117 			break;
118 	}
119 	return (gs);
120 }
121 
122 static int
in_gre_checkdup(const struct gre_softc * sc,in_addr_t src,in_addr_t dst,uint32_t opts)123 in_gre_checkdup(const struct gre_softc *sc, in_addr_t src, in_addr_t dst,
124     uint32_t opts)
125 {
126 	struct gre_list *head;
127 	struct gre_softc *tmp;
128 	struct gre_socket *gs;
129 
130 	if (sc->gre_family == AF_INET &&
131 	    sc->gre_oip.ip_src.s_addr == src &&
132 	    sc->gre_oip.ip_dst.s_addr == dst &&
133 	    (sc->gre_options & GRE_UDPENCAP) == (opts & GRE_UDPENCAP))
134 		return (EEXIST);
135 
136 	if (opts & GRE_UDPENCAP) {
137 		gs = in_gre_lookup_socket(src);
138 		if (gs == NULL)
139 			return (0);
140 		head = &gs->list;
141 	} else
142 		head = &GRE_HASH(src, dst);
143 
144 	CK_LIST_FOREACH(tmp, head, chain) {
145 		if (tmp == sc)
146 			continue;
147 		if (tmp->gre_oip.ip_src.s_addr == src &&
148 		    tmp->gre_oip.ip_dst.s_addr == dst)
149 			return (EADDRNOTAVAIL);
150 	}
151 	return (0);
152 }
153 
154 static int
in_gre_lookup(const struct mbuf * m,int off,int proto,void ** arg)155 in_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
156 {
157 	const struct ip *ip;
158 	struct gre_softc *sc;
159 
160 	if (V_ipv4_hashtbl == NULL)
161 		return (0);
162 
163 	NET_EPOCH_ASSERT();
164 	ip = mtod(m, const struct ip *);
165 	CK_LIST_FOREACH(sc, &GRE_HASH(ip->ip_dst.s_addr,
166 	    ip->ip_src.s_addr), chain) {
167 		/*
168 		 * This is an inbound packet, its ip_dst is source address
169 		 * in softc.
170 		 */
171 		if (sc->gre_oip.ip_src.s_addr == ip->ip_dst.s_addr &&
172 		    sc->gre_oip.ip_dst.s_addr == ip->ip_src.s_addr) {
173 			if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
174 				return (0);
175 			*arg = sc;
176 			return (ENCAP_DRV_LOOKUP);
177 		}
178 	}
179 	return (0);
180 }
181 
182 /*
183  * Check that ingress address belongs to local host.
184  */
185 static void
in_gre_set_running(struct gre_softc * sc)186 in_gre_set_running(struct gre_softc *sc)
187 {
188 
189 	if (in_localip(sc->gre_oip.ip_src))
190 		GRE2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
191 	else
192 		GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
193 }
194 
195 /*
196  * ifaddr_event handler.
197  * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
198  * source address spoofing.
199  */
200 static void
in_gre_srcaddr(void * arg __unused,const struct sockaddr * sa,int event __unused)201 in_gre_srcaddr(void *arg __unused, const struct sockaddr *sa,
202     int event __unused)
203 {
204 	const struct sockaddr_in *sin;
205 	struct gre_softc *sc;
206 
207 	/* Check that VNET is ready */
208 	if (V_ipv4_hashtbl == NULL)
209 		return;
210 
211 	NET_EPOCH_ASSERT();
212 	sin = (const struct sockaddr_in *)sa;
213 	CK_LIST_FOREACH(sc, &GRE_SRCHASH(sin->sin_addr.s_addr), srchash) {
214 		if (sc->gre_oip.ip_src.s_addr != sin->sin_addr.s_addr)
215 			continue;
216 		in_gre_set_running(sc);
217 	}
218 }
219 
220 static void
in_gre_udp_input(struct mbuf * m,int off,struct inpcb * inp,const struct sockaddr * sa,void * ctx)221 in_gre_udp_input(struct mbuf *m, int off, struct inpcb *inp,
222     const struct sockaddr *sa, void *ctx)
223 {
224 	struct epoch_tracker et;
225 	struct gre_socket *gs;
226 	struct gre_softc *sc;
227 	in_addr_t dst;
228 
229 	NET_EPOCH_ENTER(et);
230 	/*
231 	 * udp_append() holds reference to inp, it is safe to check
232 	 * inp_flags2 without INP_RLOCK().
233 	 * If socket was closed before we have entered NET_EPOCH section,
234 	 * INP_FREED flag should be set. Otherwise it should be safe to
235 	 * make access to ctx data, because gre_so will be freed by
236 	 * gre_sofree() via NET_EPOCH_CALL().
237 	 */
238 	if (__predict_false(inp->inp_flags2 & INP_FREED)) {
239 		NET_EPOCH_EXIT(et);
240 		m_freem(m);
241 		return;
242 	}
243 
244 	gs = (struct gre_socket *)ctx;
245 	dst = ((const struct sockaddr_in *)sa)->sin_addr.s_addr;
246 	CK_LIST_FOREACH(sc, &gs->list, chain) {
247 		if (sc->gre_oip.ip_dst.s_addr == dst)
248 			break;
249 	}
250 	if (sc != NULL && (GRE2IFP(sc)->if_flags & IFF_UP) != 0){
251 		gre_input(m, off + sizeof(struct udphdr), IPPROTO_UDP, sc);
252 		NET_EPOCH_EXIT(et);
253 		return;
254 	}
255 	m_freem(m);
256 	NET_EPOCH_EXIT(et);
257 }
258 
259 static int
in_gre_setup_socket(struct gre_softc * sc)260 in_gre_setup_socket(struct gre_softc *sc)
261 {
262 	struct sockopt sopt;
263 	struct sockaddr_in sin;
264 	struct in_gre_socket *s;
265 	struct gre_socket *gs;
266 	in_addr_t addr;
267 	int error, value;
268 
269 	/*
270 	 * NOTE: we are protected with gre_ioctl_sx lock.
271 	 *
272 	 * First check that socket is already configured.
273 	 * If so, check that source address was not changed.
274 	 * If address is different, check that there are no other tunnels
275 	 * and close socket.
276 	 */
277 	addr = sc->gre_oip.ip_src.s_addr;
278 	gs = sc->gre_so;
279 	if (gs != NULL) {
280 		s = __containerof(gs, struct in_gre_socket, base);
281 		if (s->addr != addr) {
282 			if (CK_LIST_EMPTY(&gs->list)) {
283 				CK_LIST_REMOVE(gs, chain);
284 				soclose(gs->so);
285 				NET_EPOCH_CALL(gre_sofree, &gs->epoch_ctx);
286 			}
287 			gs = sc->gre_so = NULL;
288 		}
289 	}
290 
291 	if (gs == NULL) {
292 		/*
293 		 * Check that socket for given address is already
294 		 * configured.
295 		 */
296 		gs = in_gre_lookup_socket(addr);
297 		if (gs == NULL) {
298 			s = malloc(sizeof(*s), M_GRE, M_WAITOK | M_ZERO);
299 			s->addr = addr;
300 			gs = &s->base;
301 
302 			error = socreate(sc->gre_family, &gs->so,
303 			    SOCK_DGRAM, IPPROTO_UDP, curthread->td_ucred,
304 			    curthread);
305 			if (error != 0) {
306 				if_printf(GRE2IFP(sc),
307 				    "cannot create socket: %d\n", error);
308 				free(s, M_GRE);
309 				return (error);
310 			}
311 
312 			error = udp_set_kernel_tunneling(gs->so,
313 			    in_gre_udp_input, NULL, gs);
314 			if (error != 0) {
315 				if_printf(GRE2IFP(sc),
316 				    "cannot set UDP tunneling: %d\n", error);
317 				goto fail;
318 			}
319 
320 			memset(&sopt, 0, sizeof(sopt));
321 			sopt.sopt_dir = SOPT_SET;
322 			sopt.sopt_level = IPPROTO_IP;
323 			sopt.sopt_name = IP_BINDANY;
324 			sopt.sopt_val = &value;
325 			sopt.sopt_valsize = sizeof(value);
326 			value = 1;
327 			error = sosetopt(gs->so, &sopt);
328 			if (error != 0) {
329 				if_printf(GRE2IFP(sc),
330 				    "cannot set IP_BINDANY opt: %d\n", error);
331 				goto fail;
332 			}
333 
334 			memset(&sin, 0, sizeof(sin));
335 			sin.sin_family = AF_INET;
336 			sin.sin_len = sizeof(sin);
337 			sin.sin_addr.s_addr = addr;
338 			sin.sin_port = htons(GRE_UDPPORT);
339 			error = sobind(gs->so, (struct sockaddr *)&sin,
340 			    curthread);
341 			if (error != 0) {
342 				if_printf(GRE2IFP(sc),
343 				    "cannot bind socket: %d\n", error);
344 				goto fail;
345 			}
346 			/* Add socket to the chain */
347 			CK_LIST_INSERT_HEAD(&GRE_SOCKHASH(addr), gs, chain);
348 		}
349 	}
350 
351 	/* Add softc to the socket's list */
352 	CK_LIST_INSERT_HEAD(&gs->list, sc, chain);
353 	sc->gre_so = gs;
354 	return (0);
355 fail:
356 	soclose(gs->so);
357 	free(s, M_GRE);
358 	return (error);
359 }
360 
361 static int
in_gre_attach(struct gre_softc * sc)362 in_gre_attach(struct gre_softc *sc)
363 {
364 	struct grehdr *gh;
365 	int error;
366 
367 	if (sc->gre_options & GRE_UDPENCAP) {
368 		sc->gre_csumflags = CSUM_UDP;
369 		sc->gre_hlen = sizeof(struct greudp);
370 		sc->gre_oip.ip_p = IPPROTO_UDP;
371 		gh = &sc->gre_udphdr->gi_gre;
372 		gre_update_udphdr(sc, &sc->gre_udp,
373 		    in_pseudo(sc->gre_oip.ip_src.s_addr,
374 		    sc->gre_oip.ip_dst.s_addr, 0));
375 	} else {
376 		sc->gre_hlen = sizeof(struct greip);
377 		sc->gre_oip.ip_p = IPPROTO_GRE;
378 		gh = &sc->gre_iphdr->gi_gre;
379 	}
380 	sc->gre_oip.ip_v = IPVERSION;
381 	sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
382 	gre_update_hdr(sc, gh);
383 
384 	/*
385 	 * If we return error, this means that sc is not linked,
386 	 * and caller should reset gre_family and free(sc->gre_hdr).
387 	 */
388 	if (sc->gre_options & GRE_UDPENCAP) {
389 		error = in_gre_setup_socket(sc);
390 		if (error != 0)
391 			return (error);
392 	} else
393 		CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
394 	CK_LIST_INSERT_HEAD(&GRE_SRCHASH(sc->gre_oip.ip_src.s_addr),
395 	    sc, srchash);
396 
397 	/* Set IFF_DRV_RUNNING if interface is ready */
398 	in_gre_set_running(sc);
399 	return (0);
400 }
401 
402 int
in_gre_setopts(struct gre_softc * sc,u_long cmd,uint32_t value)403 in_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
404 {
405 	int error;
406 
407 	/* NOTE: we are protected with gre_ioctl_sx lock */
408 	MPASS(cmd == GRESKEY || cmd == GRESOPTS || cmd == GRESPORT);
409 	MPASS(sc->gre_family == AF_INET);
410 
411 	/*
412 	 * If we are going to change encapsulation protocol, do check
413 	 * for duplicate tunnels. Return EEXIST here to do not confuse
414 	 * user.
415 	 */
416 	if (cmd == GRESOPTS &&
417 	    (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP) &&
418 	    in_gre_checkdup(sc, sc->gre_oip.ip_src.s_addr,
419 		sc->gre_oip.ip_dst.s_addr, value) == EADDRNOTAVAIL)
420 		return (EEXIST);
421 
422 	CK_LIST_REMOVE(sc, chain);
423 	CK_LIST_REMOVE(sc, srchash);
424 	GRE_WAIT();
425 	switch (cmd) {
426 	case GRESKEY:
427 		sc->gre_key = value;
428 		break;
429 	case GRESOPTS:
430 		sc->gre_options = value;
431 		break;
432 	case GRESPORT:
433 		sc->gre_port = value;
434 		break;
435 	}
436 	error = in_gre_attach(sc);
437 	if (error != 0) {
438 		sc->gre_family = 0;
439 		free(sc->gre_hdr, M_GRE);
440 	}
441 	return (error);
442 }
443 
444 int
in_gre_ioctl(struct gre_softc * sc,u_long cmd,caddr_t data)445 in_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
446 {
447 	struct ifreq *ifr = (struct ifreq *)data;
448 	struct sockaddr_in *dst, *src;
449 	struct ip *ip;
450 	int error;
451 
452 	/* NOTE: we are protected with gre_ioctl_sx lock */
453 	error = EINVAL;
454 	switch (cmd) {
455 	case SIOCSIFPHYADDR:
456 		src = &((struct in_aliasreq *)data)->ifra_addr;
457 		dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
458 
459 		/* sanity checks */
460 		if (src->sin_family != dst->sin_family ||
461 		    src->sin_family != AF_INET ||
462 		    src->sin_len != dst->sin_len ||
463 		    src->sin_len != sizeof(*src))
464 			break;
465 		if (src->sin_addr.s_addr == INADDR_ANY ||
466 		    dst->sin_addr.s_addr == INADDR_ANY) {
467 			error = EADDRNOTAVAIL;
468 			break;
469 		}
470 		if (V_ipv4_hashtbl == NULL) {
471 			V_ipv4_hashtbl = gre_hashinit();
472 			V_ipv4_srchashtbl = gre_hashinit();
473 			V_ipv4_sockets = (struct gre_sockets *)gre_hashinit();
474 		}
475 		error = in_gre_checkdup(sc, src->sin_addr.s_addr,
476 		    dst->sin_addr.s_addr, sc->gre_options);
477 		if (error == EADDRNOTAVAIL)
478 			break;
479 		if (error == EEXIST) {
480 			/* Addresses are the same. Just return. */
481 			error = 0;
482 			break;
483 		}
484 		ip = malloc(sizeof(struct greudp) + 3 * sizeof(uint32_t),
485 		    M_GRE, M_WAITOK | M_ZERO);
486 		ip->ip_src.s_addr = src->sin_addr.s_addr;
487 		ip->ip_dst.s_addr = dst->sin_addr.s_addr;
488 		if (sc->gre_family != 0) {
489 			/* Detach existing tunnel first */
490 			CK_LIST_REMOVE(sc, chain);
491 			CK_LIST_REMOVE(sc, srchash);
492 			GRE_WAIT();
493 			free(sc->gre_hdr, M_GRE);
494 			/* XXX: should we notify about link state change? */
495 		}
496 		sc->gre_family = AF_INET;
497 		sc->gre_hdr = ip;
498 		sc->gre_oseq = 0;
499 		sc->gre_iseq = UINT32_MAX;
500 		error = in_gre_attach(sc);
501 		if (error != 0) {
502 			sc->gre_family = 0;
503 			free(sc->gre_hdr, M_GRE);
504 		}
505 		break;
506 	case SIOCGIFPSRCADDR:
507 	case SIOCGIFPDSTADDR:
508 		if (sc->gre_family != AF_INET) {
509 			error = EADDRNOTAVAIL;
510 			break;
511 		}
512 		src = (struct sockaddr_in *)&ifr->ifr_addr;
513 		memset(src, 0, sizeof(*src));
514 		src->sin_family = AF_INET;
515 		src->sin_len = sizeof(*src);
516 		src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
517 		    sc->gre_oip.ip_src: sc->gre_oip.ip_dst;
518 		error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
519 		if (error != 0)
520 			memset(src, 0, sizeof(*src));
521 		break;
522 	}
523 	return (error);
524 }
525 
526 int
in_gre_output(struct mbuf * m,int af,int hlen)527 in_gre_output(struct mbuf *m, int af, int hlen)
528 {
529 	struct greip *gi;
530 
531 	gi = mtod(m, struct greip *);
532 	switch (af) {
533 	case AF_INET:
534 		/*
535 		 * gre_transmit() has used M_PREPEND() that doesn't guarantee
536 		 * m_data is contiguous more than hlen bytes. Use m_copydata()
537 		 * here to avoid m_pullup().
538 		 */
539 		m_copydata(m, hlen + offsetof(struct ip, ip_tos),
540 		    sizeof(u_char), &gi->gi_ip.ip_tos);
541 		m_copydata(m, hlen + offsetof(struct ip, ip_id),
542 		    sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id);
543 		break;
544 #ifdef INET6
545 	case AF_INET6:
546 		gi->gi_ip.ip_tos = 0; /* XXX */
547 		ip_fillid(&gi->gi_ip);
548 		break;
549 #endif
550 	}
551 	gi->gi_ip.ip_ttl = V_ip_gre_ttl;
552 	gi->gi_ip.ip_len = htons(m->m_pkthdr.len);
553 	return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL));
554 }
555 
556 static const struct srcaddrtab *ipv4_srcaddrtab = NULL;
557 static const struct encaptab *ecookie = NULL;
558 static const struct encap_config ipv4_encap_cfg = {
559 	.proto = IPPROTO_GRE,
560 	.min_length = sizeof(struct greip) + sizeof(struct ip),
561 	.exact_match = ENCAP_DRV_LOOKUP,
562 	.lookup = in_gre_lookup,
563 	.input = gre_input
564 };
565 
566 void
in_gre_init(void)567 in_gre_init(void)
568 {
569 
570 	if (!IS_DEFAULT_VNET(curvnet))
571 		return;
572 	ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gre_srcaddr,
573 	    NULL, M_WAITOK);
574 	ecookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
575 }
576 
577 void
in_gre_uninit(void)578 in_gre_uninit(void)
579 {
580 
581 	if (IS_DEFAULT_VNET(curvnet)) {
582 		ip_encap_detach(ecookie);
583 		ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
584 	}
585 	if (V_ipv4_hashtbl != NULL) {
586 		gre_hashdestroy(V_ipv4_hashtbl);
587 		V_ipv4_hashtbl = NULL;
588 		GRE_WAIT();
589 		gre_hashdestroy(V_ipv4_srchashtbl);
590 		gre_hashdestroy((struct gre_list *)V_ipv4_sockets);
591 	}
592 }
593