xref: /freebsd/sys/net/if_gre.c (revision 9823d52705ad71f19ef2205aa729547ac396e3eb)
173d7ddbcSMaxim Sobolev /*	$NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
28e96e13eSMaxim Sobolev /*	 $FreeBSD$ */
38e96e13eSMaxim Sobolev 
4c398230bSWarner Losh /*-
58e96e13eSMaxim Sobolev  * Copyright (c) 1998 The NetBSD Foundation, Inc.
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.
338e96e13eSMaxim Sobolev  */
348e96e13eSMaxim Sobolev 
358e96e13eSMaxim Sobolev /*
368e96e13eSMaxim Sobolev  * Encapsulate L3 protocols into IP
379e669156SBjoern A. Zeeb  * See RFC 2784 (successor of RFC 1701 and 1702) for more details.
388e96e13eSMaxim Sobolev  * If_gre is compatible with Cisco GRE tunnels, so you can
398e96e13eSMaxim Sobolev  * have a NetBSD box as the other end of a tunnel interface of a Cisco
408e96e13eSMaxim Sobolev  * router. See gre(4) for more details.
418e96e13eSMaxim Sobolev  * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
428e96e13eSMaxim Sobolev  */
438e96e13eSMaxim Sobolev 
4412768622SBruce Evans #include "opt_atalk.h"
458e96e13eSMaxim Sobolev #include "opt_inet.h"
46f16770aeSBruce M Simpson #include "opt_inet6.h"
478e96e13eSMaxim Sobolev 
488e96e13eSMaxim Sobolev #include <sys/param.h>
49e3416ab0SBjoern A. Zeeb #include <sys/jail.h>
508e96e13eSMaxim Sobolev #include <sys/kernel.h>
5152dcd04bSBjoern A. Zeeb #include <sys/libkern.h>
528e96e13eSMaxim Sobolev #include <sys/malloc.h>
535dba30f1SPoul-Henning Kamp #include <sys/module.h>
548e96e13eSMaxim Sobolev #include <sys/mbuf.h>
55acd3428bSRobert Watson #include <sys/priv.h>
568b07e49aSJulian Elischer #include <sys/proc.h>
578e96e13eSMaxim Sobolev #include <sys/protosw.h>
588e96e13eSMaxim Sobolev #include <sys/socket.h>
598e96e13eSMaxim Sobolev #include <sys/sockio.h>
608e96e13eSMaxim Sobolev #include <sys/sysctl.h>
611b861caaSBruce Evans #include <sys/systm.h>
628e96e13eSMaxim Sobolev 
638e96e13eSMaxim Sobolev #include <net/ethernet.h>
648e96e13eSMaxim Sobolev #include <net/if.h>
65f889d2efSBrooks Davis #include <net/if_clone.h>
668e96e13eSMaxim Sobolev #include <net/if_types.h>
678e96e13eSMaxim Sobolev #include <net/route.h>
68530c0060SRobert Watson #include <net/vnet.h>
698e96e13eSMaxim Sobolev 
708e96e13eSMaxim Sobolev #ifdef INET
718e96e13eSMaxim Sobolev #include <netinet/in.h>
728e96e13eSMaxim Sobolev #include <netinet/in_systm.h>
738e96e13eSMaxim Sobolev #include <netinet/in_var.h>
748e96e13eSMaxim Sobolev #include <netinet/ip.h>
758e96e13eSMaxim Sobolev #include <netinet/ip_gre.h>
768e96e13eSMaxim Sobolev #include <netinet/ip_var.h>
778e96e13eSMaxim Sobolev #include <netinet/ip_encap.h>
788e96e13eSMaxim Sobolev #else
798e96e13eSMaxim Sobolev #error "Huh? if_gre without inet?"
808e96e13eSMaxim Sobolev #endif
818e96e13eSMaxim Sobolev 
828e96e13eSMaxim Sobolev #include <net/bpf.h>
838e96e13eSMaxim Sobolev 
848e96e13eSMaxim Sobolev #include <net/if_gre.h>
858e96e13eSMaxim Sobolev 
868e96e13eSMaxim Sobolev /*
878e96e13eSMaxim Sobolev  * It is not easy to calculate the right value for a GRE MTU.
888e96e13eSMaxim Sobolev  * We leave this task to the admin and use the same default that
898e96e13eSMaxim Sobolev  * other vendors use.
908e96e13eSMaxim Sobolev  */
918e96e13eSMaxim Sobolev #define GREMTU	1476
928e96e13eSMaxim Sobolev 
938e96e13eSMaxim Sobolev #define GRENAME	"gre"
948e96e13eSMaxim Sobolev 
9552dcd04bSBjoern A. Zeeb #define	MTAG_COOKIE_GRE		1307983903
9652dcd04bSBjoern A. Zeeb #define	MTAG_GRE_NESTING	1
9752dcd04bSBjoern A. Zeeb struct mtag_gre_nesting {
9852dcd04bSBjoern A. Zeeb 	uint16_t	count;
9952dcd04bSBjoern A. Zeeb 	uint16_t	max;
10052dcd04bSBjoern A. Zeeb 	struct ifnet	*ifp[];
10152dcd04bSBjoern A. Zeeb };
10252dcd04bSBjoern A. Zeeb 
103bdae44a8SRobert Watson /*
104bdae44a8SRobert Watson  * gre_mtx protects all global variables in if_gre.c.
105bdae44a8SRobert Watson  * XXX: gre_softc data not protected yet.
106bdae44a8SRobert Watson  */
107bdae44a8SRobert Watson struct mtx gre_mtx;
1088e96e13eSMaxim Sobolev static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
1098e96e13eSMaxim Sobolev 
1108e96e13eSMaxim Sobolev struct gre_softc_head gre_softc_list;
1118e96e13eSMaxim Sobolev 
1126b7330e2SSam Leffler static int	gre_clone_create(struct if_clone *, int, caddr_t);
1139ee35470SAlfred Perlstein static void	gre_clone_destroy(struct ifnet *);
114c23d234cSMaxim Sobolev static int	gre_ioctl(struct ifnet *, u_long, caddr_t);
115c23d234cSMaxim Sobolev static int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
116279aa3d4SKip Macy 		    struct route *ro);
1178e96e13eSMaxim Sobolev 
118f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(gre, 0);
1198e96e13eSMaxim Sobolev 
120c23d234cSMaxim Sobolev static int gre_compute_route(struct gre_softc *sc);
1218e96e13eSMaxim Sobolev 
1229ee35470SAlfred Perlstein static void	greattach(void);
1238e96e13eSMaxim Sobolev 
1248e96e13eSMaxim Sobolev #ifdef INET
1258e96e13eSMaxim Sobolev extern struct domain inetdomain;
126303989a2SRuslan Ermilov static const struct protosw in_gre_protosw = {
127303989a2SRuslan Ermilov 	.pr_type =		SOCK_RAW,
128303989a2SRuslan Ermilov 	.pr_domain =		&inetdomain,
129303989a2SRuslan Ermilov 	.pr_protocol =		IPPROTO_GRE,
130303989a2SRuslan Ermilov 	.pr_flags =		PR_ATOMIC|PR_ADDR,
1313f2e28feSBjoern A. Zeeb 	.pr_input =		gre_input,
132303989a2SRuslan Ermilov 	.pr_output =		(pr_output_t *)rip_output,
133303989a2SRuslan Ermilov 	.pr_ctlinput =		rip_ctlinput,
134303989a2SRuslan Ermilov 	.pr_ctloutput =		rip_ctloutput,
135303989a2SRuslan Ermilov 	.pr_usrreqs =		&rip_usrreqs
1368e96e13eSMaxim Sobolev };
137303989a2SRuslan Ermilov static const struct protosw in_mobile_protosw = {
138303989a2SRuslan Ermilov 	.pr_type =		SOCK_RAW,
139303989a2SRuslan Ermilov 	.pr_domain =		&inetdomain,
140303989a2SRuslan Ermilov 	.pr_protocol =		IPPROTO_MOBILE,
141303989a2SRuslan Ermilov 	.pr_flags =		PR_ATOMIC|PR_ADDR,
1423f2e28feSBjoern A. Zeeb 	.pr_input =		gre_mobile_input,
143303989a2SRuslan Ermilov 	.pr_output =		(pr_output_t *)rip_output,
144303989a2SRuslan Ermilov 	.pr_ctlinput =		rip_ctlinput,
145303989a2SRuslan Ermilov 	.pr_ctloutput =		rip_ctloutput,
146303989a2SRuslan Ermilov 	.pr_usrreqs =		&rip_usrreqs
1478e96e13eSMaxim Sobolev };
1488e96e13eSMaxim Sobolev #endif
1498e96e13eSMaxim Sobolev 
1508e96e13eSMaxim Sobolev SYSCTL_DECL(_net_link);
1516472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
1528e96e13eSMaxim Sobolev     "Generic Routing Encapsulation");
1538e96e13eSMaxim Sobolev #ifndef MAX_GRE_NEST
1548e96e13eSMaxim Sobolev /*
1558e96e13eSMaxim Sobolev  * This macro controls the default upper limitation on nesting of gre tunnels.
1568e96e13eSMaxim Sobolev  * Since, setting a large value to this macro with a careless configuration
1578e96e13eSMaxim Sobolev  * may introduce system crash, we don't allow any nestings by default.
1588e96e13eSMaxim Sobolev  * If you need to configure nested gre tunnels, you can define this macro
1598e96e13eSMaxim Sobolev  * in your kernel configuration file.  However, if you do so, please be
1608e96e13eSMaxim Sobolev  * careful to configure the tunnels so that it won't make a loop.
1618e96e13eSMaxim Sobolev  */
1628e96e13eSMaxim Sobolev #define MAX_GRE_NEST 1
1638e96e13eSMaxim Sobolev #endif
1648e96e13eSMaxim Sobolev static int max_gre_nesting = MAX_GRE_NEST;
1658e96e13eSMaxim Sobolev SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
1668e96e13eSMaxim Sobolev     &max_gre_nesting, 0, "Max nested tunnels");
1678e96e13eSMaxim Sobolev 
1688e96e13eSMaxim Sobolev /* ARGSUSED */
169c23d234cSMaxim Sobolev static void
1708e96e13eSMaxim Sobolev greattach(void)
1718e96e13eSMaxim Sobolev {
1728e96e13eSMaxim Sobolev 
173bdae44a8SRobert Watson 	mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
1748e96e13eSMaxim Sobolev 	LIST_INIT(&gre_softc_list);
1758e96e13eSMaxim Sobolev 	if_clone_attach(&gre_cloner);
1768e96e13eSMaxim Sobolev }
1778e96e13eSMaxim Sobolev 
178c23d234cSMaxim Sobolev static int
1796b7330e2SSam Leffler gre_clone_create(ifc, unit, params)
1808e96e13eSMaxim Sobolev 	struct if_clone *ifc;
1818e96e13eSMaxim Sobolev 	int unit;
1826b7330e2SSam Leffler 	caddr_t params;
1838e96e13eSMaxim Sobolev {
1848e96e13eSMaxim Sobolev 	struct gre_softc *sc;
1858e96e13eSMaxim Sobolev 
186b3c9a01eSBruce M Simpson 	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
1878e96e13eSMaxim Sobolev 
188066b192eSBjoern A. Zeeb 	GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
189066b192eSBjoern A. Zeeb 	if (GRE2IFP(sc) == NULL) {
190066b192eSBjoern A. Zeeb 		free(sc, M_GRE);
191066b192eSBjoern A. Zeeb 		return (ENOSPC);
192066b192eSBjoern A. Zeeb 	}
193066b192eSBjoern A. Zeeb 
194fc74a9f9SBrooks Davis 	GRE2IFP(sc)->if_softc = sc;
195066b192eSBjoern A. Zeeb 	if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
196066b192eSBjoern A. Zeeb 
197e50d35e6SMaxim Sobolev 	GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
198fc74a9f9SBrooks Davis 	GRE2IFP(sc)->if_addrlen = 0;
199fc74a9f9SBrooks Davis 	GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
200fc74a9f9SBrooks Davis 	GRE2IFP(sc)->if_mtu = GREMTU;
201fc74a9f9SBrooks Davis 	GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
202fc74a9f9SBrooks Davis 	GRE2IFP(sc)->if_output = gre_output;
203fc74a9f9SBrooks Davis 	GRE2IFP(sc)->if_ioctl = gre_ioctl;
2048e96e13eSMaxim Sobolev 	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
2058e96e13eSMaxim Sobolev 	sc->g_proto = IPPROTO_GRE;
206fc74a9f9SBrooks Davis 	GRE2IFP(sc)->if_flags |= IFF_LINK0;
2078e96e13eSMaxim Sobolev 	sc->encap = NULL;
2088b07e49aSJulian Elischer 	sc->gre_fibnum = curthread->td_proc->p_fibnum;
2097735aeb9SMaxim Sobolev 	sc->wccp_ver = WCCP_V1;
210131c55bcSAndrew Thompson 	sc->key = 0;
211fc74a9f9SBrooks Davis 	if_attach(GRE2IFP(sc));
212fc74a9f9SBrooks Davis 	bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
213bdae44a8SRobert Watson 	mtx_lock(&gre_mtx);
2148e96e13eSMaxim Sobolev 	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
215bdae44a8SRobert Watson 	mtx_unlock(&gre_mtx);
2168e96e13eSMaxim Sobolev 	return (0);
2178e96e13eSMaxim Sobolev }
2188e96e13eSMaxim Sobolev 
219c23d234cSMaxim Sobolev static void
2208e96e13eSMaxim Sobolev gre_clone_destroy(ifp)
2218e96e13eSMaxim Sobolev 	struct ifnet *ifp;
2228e96e13eSMaxim Sobolev {
2238e96e13eSMaxim Sobolev 	struct gre_softc *sc = ifp->if_softc;
2248e96e13eSMaxim Sobolev 
225bdae44a8SRobert Watson 	mtx_lock(&gre_mtx);
2268e96e13eSMaxim Sobolev 	LIST_REMOVE(sc, sc_list);
227bdae44a8SRobert Watson 	mtx_unlock(&gre_mtx);
228febd0759SAndrew Thompson 
229febd0759SAndrew Thompson #ifdef INET
230febd0759SAndrew Thompson 	if (sc->encap != NULL)
231febd0759SAndrew Thompson 		encap_detach(sc->encap);
232febd0759SAndrew Thompson #endif
233febd0759SAndrew Thompson 	bpfdetach(ifp);
234febd0759SAndrew Thompson 	if_detach(ifp);
235febd0759SAndrew Thompson 	if_free(ifp);
236febd0759SAndrew Thompson 	free(sc, M_GRE);
2378e96e13eSMaxim Sobolev }
2388e96e13eSMaxim Sobolev 
2398e96e13eSMaxim Sobolev /*
2408e96e13eSMaxim Sobolev  * The output routine. Takes a packet and encapsulates it in the protocol
2418e96e13eSMaxim Sobolev  * given by sc->g_proto. See also RFC 1701 and RFC 2004
2428e96e13eSMaxim Sobolev  */
243c23d234cSMaxim Sobolev static int
2448e96e13eSMaxim Sobolev gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
245279aa3d4SKip Macy 	   struct route *ro)
2468e96e13eSMaxim Sobolev {
2478e96e13eSMaxim Sobolev 	int error = 0;
2488e96e13eSMaxim Sobolev 	struct gre_softc *sc = ifp->if_softc;
2498e96e13eSMaxim Sobolev 	struct greip *gh;
2508e96e13eSMaxim Sobolev 	struct ip *ip;
25152dcd04bSBjoern A. Zeeb 	struct m_tag *mtag;
25252dcd04bSBjoern A. Zeeb 	struct mtag_gre_nesting *gt;
25352dcd04bSBjoern A. Zeeb 	size_t len;
254a54eadd8SJulian Elischer 	u_short gre_ip_id = 0;
255a54eadd8SJulian Elischer 	uint8_t gre_ip_tos = 0;
25673d7ddbcSMaxim Sobolev 	u_int16_t etype = 0;
2578e96e13eSMaxim Sobolev 	struct mobile_h mob_h;
25801399f34SDavid Malone 	u_int32_t af;
25952dcd04bSBjoern A. Zeeb 	int extra = 0, max;
2608e96e13eSMaxim Sobolev 
2618e96e13eSMaxim Sobolev 	/*
26252dcd04bSBjoern A. Zeeb 	 * gre may cause infinite recursion calls when misconfigured.  High
26352dcd04bSBjoern A. Zeeb 	 * nesting level may cause stack exhaustion.  We'll prevent this by
26452dcd04bSBjoern A. Zeeb 	 * detecting loops and by introducing upper limit.
2658e96e13eSMaxim Sobolev 	 */
26652dcd04bSBjoern A. Zeeb 	mtag = m_tag_locate(m, MTAG_COOKIE_GRE, MTAG_GRE_NESTING, NULL);
26752dcd04bSBjoern A. Zeeb 	if (mtag != NULL) {
26852dcd04bSBjoern A. Zeeb 		struct ifnet **ifp2;
26952dcd04bSBjoern A. Zeeb 
27052dcd04bSBjoern A. Zeeb 		gt = (struct mtag_gre_nesting *)(mtag + 1);
27152dcd04bSBjoern A. Zeeb 		gt->count++;
27252dcd04bSBjoern A. Zeeb 		if (gt->count > min(gt->max,max_gre_nesting)) {
27352dcd04bSBjoern A. Zeeb 			printf("%s: hit maximum recursion limit %u on %s\n",
27452dcd04bSBjoern A. Zeeb 				__func__, gt->count - 1, ifp->if_xname);
2758e96e13eSMaxim Sobolev 			m_freem(m);
2768e96e13eSMaxim Sobolev 			error = EIO;	/* is there better errno? */
2778e96e13eSMaxim Sobolev 			goto end;
2788e96e13eSMaxim Sobolev 		}
2798e96e13eSMaxim Sobolev 
28052dcd04bSBjoern A. Zeeb 		ifp2 = gt->ifp;
28152dcd04bSBjoern A. Zeeb 		for (max = gt->count - 1; max > 0; max--) {
28252dcd04bSBjoern A. Zeeb 			if (*ifp2 == ifp)
28352dcd04bSBjoern A. Zeeb 				break;
28452dcd04bSBjoern A. Zeeb 			ifp2++;
28552dcd04bSBjoern A. Zeeb 		}
28652dcd04bSBjoern A. Zeeb 		if (*ifp2 == ifp) {
28752dcd04bSBjoern A. Zeeb 			printf("%s: detected loop with nexting %u on %s\n",
28852dcd04bSBjoern A. Zeeb 				__func__, gt->count-1, ifp->if_xname);
28952dcd04bSBjoern A. Zeeb 			m_freem(m);
29052dcd04bSBjoern A. Zeeb 			error = EIO;	/* is there better errno? */
29152dcd04bSBjoern A. Zeeb 			goto end;
29252dcd04bSBjoern A. Zeeb 		}
29352dcd04bSBjoern A. Zeeb 		*ifp2 = ifp;
29452dcd04bSBjoern A. Zeeb 
29552dcd04bSBjoern A. Zeeb 	} else {
29652dcd04bSBjoern A. Zeeb 		/*
29752dcd04bSBjoern A. Zeeb 		 * Given that people should NOT increase max_gre_nesting beyond
29852dcd04bSBjoern A. Zeeb 		 * their real needs, we allocate once per packet rather than
29952dcd04bSBjoern A. Zeeb 		 * allocating an mtag once per passing through gre.
30052dcd04bSBjoern A. Zeeb 		 *
30152dcd04bSBjoern A. Zeeb 		 * Note: the sysctl does not actually check for saneness, so we
30252dcd04bSBjoern A. Zeeb 		 * limit the maximum numbers of possible recursions here.
30352dcd04bSBjoern A. Zeeb 		 */
30452dcd04bSBjoern A. Zeeb 		max = imin(max_gre_nesting, 256);
30552dcd04bSBjoern A. Zeeb 		/* If someone sets the sysctl <= 0, we want at least 1. */
30652dcd04bSBjoern A. Zeeb 		max = imax(max, 1);
30752dcd04bSBjoern A. Zeeb 		len = sizeof(struct mtag_gre_nesting) +
30852dcd04bSBjoern A. Zeeb 		    max * sizeof(struct ifnet *);
30952dcd04bSBjoern A. Zeeb 		mtag = m_tag_alloc(MTAG_COOKIE_GRE, MTAG_GRE_NESTING, len,
31052dcd04bSBjoern A. Zeeb 		    M_NOWAIT);
31152dcd04bSBjoern A. Zeeb 		if (mtag == NULL) {
31252dcd04bSBjoern A. Zeeb 			m_freem(m);
31352dcd04bSBjoern A. Zeeb 			error = ENOMEM;
31452dcd04bSBjoern A. Zeeb 			goto end;
31552dcd04bSBjoern A. Zeeb 		}
31652dcd04bSBjoern A. Zeeb 		gt = (struct mtag_gre_nesting *)(mtag + 1);
31752dcd04bSBjoern A. Zeeb 		bzero(gt, len);
31852dcd04bSBjoern A. Zeeb 		gt->count = 1;
31952dcd04bSBjoern A. Zeeb 		gt->max = max;
32052dcd04bSBjoern A. Zeeb 		*gt->ifp = ifp;
32152dcd04bSBjoern A. Zeeb 		m_tag_prepend(m, mtag);
32252dcd04bSBjoern A. Zeeb 	}
32352dcd04bSBjoern A. Zeeb 
32413f4c340SRobert Watson 	if (!((ifp->if_flags & IFF_UP) &&
32513f4c340SRobert Watson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
3268e96e13eSMaxim Sobolev 	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
3278e96e13eSMaxim Sobolev 		m_freem(m);
3288e96e13eSMaxim Sobolev 		error = ENETDOWN;
3298e96e13eSMaxim Sobolev 		goto end;
3308e96e13eSMaxim Sobolev 	}
3318e96e13eSMaxim Sobolev 
3328e96e13eSMaxim Sobolev 	gh = NULL;
3338e96e13eSMaxim Sobolev 	ip = NULL;
3348e96e13eSMaxim Sobolev 
33501399f34SDavid Malone 	/* BPF writes need to be handled specially. */
33601399f34SDavid Malone 	if (dst->sa_family == AF_UNSPEC) {
33701399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
33801399f34SDavid Malone 		dst->sa_family = af;
33901399f34SDavid Malone 	}
34001399f34SDavid Malone 
34116d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
34201399f34SDavid Malone 		af = dst->sa_family;
343437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
3448e96e13eSMaxim Sobolev 	}
3458e96e13eSMaxim Sobolev 
3468e96e13eSMaxim Sobolev 	m->m_flags &= ~(M_BCAST|M_MCAST);
3478e96e13eSMaxim Sobolev 
3488e96e13eSMaxim Sobolev 	if (sc->g_proto == IPPROTO_MOBILE) {
3498e96e13eSMaxim Sobolev 		if (dst->sa_family == AF_INET) {
3508e96e13eSMaxim Sobolev 			struct mbuf *m0;
3518e96e13eSMaxim Sobolev 			int msiz;
3528e96e13eSMaxim Sobolev 
3538e96e13eSMaxim Sobolev 			ip = mtod(m, struct ip *);
3548e96e13eSMaxim Sobolev 
3558e96e13eSMaxim Sobolev 			/*
3568e96e13eSMaxim Sobolev 			 * RFC2004 specifies that fragmented diagrams shouldn't
3578e96e13eSMaxim Sobolev 			 * be encapsulated.
3588e96e13eSMaxim Sobolev 			 */
359a393a28aSJeffrey Hsu 			if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
3608e96e13eSMaxim Sobolev 				_IF_DROP(&ifp->if_snd);
3618e96e13eSMaxim Sobolev 				m_freem(m);
3628e96e13eSMaxim Sobolev 				error = EINVAL;    /* is there better errno? */
3638e96e13eSMaxim Sobolev 				goto end;
3648e96e13eSMaxim Sobolev 			}
3658e96e13eSMaxim Sobolev 			memset(&mob_h, 0, MOB_H_SIZ_L);
3668e96e13eSMaxim Sobolev 			mob_h.proto = (ip->ip_p) << 8;
3678e96e13eSMaxim Sobolev 			mob_h.odst = ip->ip_dst.s_addr;
3688e96e13eSMaxim Sobolev 			ip->ip_dst.s_addr = sc->g_dst.s_addr;
3698e96e13eSMaxim Sobolev 
3708e96e13eSMaxim Sobolev 			/*
3718e96e13eSMaxim Sobolev 			 * If the packet comes from our host, we only change
3728e96e13eSMaxim Sobolev 			 * the destination address in the IP header.
3738e96e13eSMaxim Sobolev 			 * Else we also need to save and change the source
3748e96e13eSMaxim Sobolev 			 */
3758e96e13eSMaxim Sobolev 			if (in_hosteq(ip->ip_src, sc->g_src)) {
3768e96e13eSMaxim Sobolev 				msiz = MOB_H_SIZ_S;
3778e96e13eSMaxim Sobolev 			} else {
3788e96e13eSMaxim Sobolev 				mob_h.proto |= MOB_H_SBIT;
3798e96e13eSMaxim Sobolev 				mob_h.osrc = ip->ip_src.s_addr;
3808e96e13eSMaxim Sobolev 				ip->ip_src.s_addr = sc->g_src.s_addr;
3818e96e13eSMaxim Sobolev 				msiz = MOB_H_SIZ_L;
3828e96e13eSMaxim Sobolev 			}
3838e96e13eSMaxim Sobolev 			mob_h.proto = htons(mob_h.proto);
38473d7ddbcSMaxim Sobolev 			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
3858e96e13eSMaxim Sobolev 
3868e96e13eSMaxim Sobolev 			if ((m->m_data - msiz) < m->m_pktdat) {
3878e96e13eSMaxim Sobolev 				/* need new mbuf */
38834333b16SAndre Oppermann 				MGETHDR(m0, M_DONTWAIT, MT_DATA);
3898e96e13eSMaxim Sobolev 				if (m0 == NULL) {
3908e96e13eSMaxim Sobolev 					_IF_DROP(&ifp->if_snd);
3918e96e13eSMaxim Sobolev 					m_freem(m);
3928e96e13eSMaxim Sobolev 					error = ENOBUFS;
3938e96e13eSMaxim Sobolev 					goto end;
3948e96e13eSMaxim Sobolev 				}
3958e96e13eSMaxim Sobolev 				m0->m_next = m;
3968e96e13eSMaxim Sobolev 				m->m_data += sizeof(struct ip);
3978e96e13eSMaxim Sobolev 				m->m_len -= sizeof(struct ip);
3988e96e13eSMaxim Sobolev 				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
3998e96e13eSMaxim Sobolev 				m0->m_len = msiz + sizeof(struct ip);
4008e96e13eSMaxim Sobolev 				m0->m_data += max_linkhdr;
4018e96e13eSMaxim Sobolev 				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
4028e96e13eSMaxim Sobolev 				       sizeof(struct ip));
4038e96e13eSMaxim Sobolev 				m = m0;
4048e96e13eSMaxim Sobolev 			} else {  /* we have some space left in the old one */
4058e96e13eSMaxim Sobolev 				m->m_data -= msiz;
4068e96e13eSMaxim Sobolev 				m->m_len += msiz;
4078e96e13eSMaxim Sobolev 				m->m_pkthdr.len += msiz;
4088e96e13eSMaxim Sobolev 				bcopy(ip, mtod(m, caddr_t),
4098e96e13eSMaxim Sobolev 					sizeof(struct ip));
4108e96e13eSMaxim Sobolev 			}
4118e96e13eSMaxim Sobolev 			ip = mtod(m, struct ip *);
4128e96e13eSMaxim Sobolev 			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
4138e96e13eSMaxim Sobolev 			ip->ip_len = ntohs(ip->ip_len) + msiz;
4148e96e13eSMaxim Sobolev 		} else {  /* AF_INET */
4158e96e13eSMaxim Sobolev 			_IF_DROP(&ifp->if_snd);
4168e96e13eSMaxim Sobolev 			m_freem(m);
4178e96e13eSMaxim Sobolev 			error = EINVAL;
4188e96e13eSMaxim Sobolev 			goto end;
4198e96e13eSMaxim Sobolev 		}
4208e96e13eSMaxim Sobolev 	} else if (sc->g_proto == IPPROTO_GRE) {
4218e96e13eSMaxim Sobolev 		switch (dst->sa_family) {
4228e96e13eSMaxim Sobolev 		case AF_INET:
4238e96e13eSMaxim Sobolev 			ip = mtod(m, struct ip *);
424a54eadd8SJulian Elischer 			gre_ip_tos = ip->ip_tos;
425a54eadd8SJulian Elischer 			gre_ip_id = ip->ip_id;
42644554a6dSJulian Elischer 			if (sc->wccp_ver == WCCP_V2) {
42744554a6dSJulian Elischer 				extra = sizeof(uint32_t);
42844554a6dSJulian Elischer 				etype =  WCCP_PROTOCOL_TYPE;
42944554a6dSJulian Elischer 			} else {
4308e96e13eSMaxim Sobolev 				etype = ETHERTYPE_IP;
43144554a6dSJulian Elischer 			}
4328e96e13eSMaxim Sobolev 			break;
4339e669156SBjoern A. Zeeb #ifdef INET6
4349e669156SBjoern A. Zeeb 		case AF_INET6:
435a54eadd8SJulian Elischer 			gre_ip_id = ip_newid();
4369e669156SBjoern A. Zeeb 			etype = ETHERTYPE_IPV6;
4379e669156SBjoern A. Zeeb 			break;
4389e669156SBjoern A. Zeeb #endif
4398e96e13eSMaxim Sobolev #ifdef NETATALK
4408e96e13eSMaxim Sobolev 		case AF_APPLETALK:
4418e96e13eSMaxim Sobolev 			etype = ETHERTYPE_ATALK;
4428e96e13eSMaxim Sobolev 			break;
4438e96e13eSMaxim Sobolev #endif
4448e96e13eSMaxim Sobolev 		default:
4458e96e13eSMaxim Sobolev 			_IF_DROP(&ifp->if_snd);
4468e96e13eSMaxim Sobolev 			m_freem(m);
4478e96e13eSMaxim Sobolev 			error = EAFNOSUPPORT;
4488e96e13eSMaxim Sobolev 			goto end;
4498e96e13eSMaxim Sobolev 		}
450131c55bcSAndrew Thompson 
451131c55bcSAndrew Thompson 		/* Reserve space for GRE header + optional GRE key */
45244554a6dSJulian Elischer 		int hdrlen = sizeof(struct greip) + extra;
453131c55bcSAndrew Thompson 		if (sc->key)
454131c55bcSAndrew Thompson 			hdrlen += sizeof(uint32_t);
455131c55bcSAndrew Thompson 		M_PREPEND(m, hdrlen, M_DONTWAIT);
4568e96e13eSMaxim Sobolev 	} else {
4578e96e13eSMaxim Sobolev 		_IF_DROP(&ifp->if_snd);
4588e96e13eSMaxim Sobolev 		m_freem(m);
4598e96e13eSMaxim Sobolev 		error = EINVAL;
4608e96e13eSMaxim Sobolev 		goto end;
4618e96e13eSMaxim Sobolev 	}
4628e96e13eSMaxim Sobolev 
4635efdd80aSAndre Oppermann 	if (m == NULL) {	/* mbuf allocation failed */
4648e96e13eSMaxim Sobolev 		_IF_DROP(&ifp->if_snd);
4658e96e13eSMaxim Sobolev 		error = ENOBUFS;
4668e96e13eSMaxim Sobolev 		goto end;
4678e96e13eSMaxim Sobolev 	}
4688e96e13eSMaxim Sobolev 
4698b07e49aSJulian Elischer 	M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
4708b07e49aSJulian Elischer 
4718e96e13eSMaxim Sobolev 	gh = mtod(m, struct greip *);
4728e96e13eSMaxim Sobolev 	if (sc->g_proto == IPPROTO_GRE) {
473131c55bcSAndrew Thompson 		uint32_t *options = gh->gi_options;
474131c55bcSAndrew Thompson 
47544554a6dSJulian Elischer 		memset((void *)gh, 0, sizeof(struct greip) + extra);
4768e96e13eSMaxim Sobolev 		gh->gi_ptype = htons(etype);
477131c55bcSAndrew Thompson 		gh->gi_flags = 0;
478131c55bcSAndrew Thompson 
479131c55bcSAndrew Thompson 		/* Add key option */
480131c55bcSAndrew Thompson 		if (sc->key)
481131c55bcSAndrew Thompson 		{
482131c55bcSAndrew Thompson 			gh->gi_flags |= htons(GRE_KP);
483131c55bcSAndrew Thompson 			*(options++) = htonl(sc->key);
484131c55bcSAndrew Thompson 		}
4858e96e13eSMaxim Sobolev 	}
4868e96e13eSMaxim Sobolev 
4878e96e13eSMaxim Sobolev 	gh->gi_pr = sc->g_proto;
4888e96e13eSMaxim Sobolev 	if (sc->g_proto != IPPROTO_MOBILE) {
4898e96e13eSMaxim Sobolev 		gh->gi_src = sc->g_src;
4908e96e13eSMaxim Sobolev 		gh->gi_dst = sc->g_dst;
49197c4cd98SMaxim Sobolev 		((struct ip*)gh)->ip_v = IPPROTO_IPV4;
4928e96e13eSMaxim Sobolev 		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
493c23d234cSMaxim Sobolev 		((struct ip*)gh)->ip_ttl = GRE_TTL;
494a54eadd8SJulian Elischer 		((struct ip*)gh)->ip_tos = gre_ip_tos;
495a54eadd8SJulian Elischer 		((struct ip*)gh)->ip_id = gre_ip_id;
4964c837892SMaxim Sobolev 		gh->gi_len = m->m_pkthdr.len;
4978e96e13eSMaxim Sobolev 	}
4988e96e13eSMaxim Sobolev 
4998e96e13eSMaxim Sobolev 	ifp->if_opackets++;
5008e96e13eSMaxim Sobolev 	ifp->if_obytes += m->m_pkthdr.len;
5018b75eec1SAndre Oppermann 	/*
5028b75eec1SAndre Oppermann 	 * Send it off and with IP_FORWARD flag to prevent it from
5038b75eec1SAndre Oppermann 	 * overwriting the ip_id again.  ip_id is already set to the
5048b75eec1SAndre Oppermann 	 * ip_id of the encapsulated packet.
5058b75eec1SAndre Oppermann 	 */
5065efdd80aSAndre Oppermann 	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
50773d7ddbcSMaxim Sobolev 	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
5088e96e13eSMaxim Sobolev   end:
5098e96e13eSMaxim Sobolev 	if (error)
5108e96e13eSMaxim Sobolev 		ifp->if_oerrors++;
5118e96e13eSMaxim Sobolev 	return (error);
5128e96e13eSMaxim Sobolev }
5138e96e13eSMaxim Sobolev 
514c23d234cSMaxim Sobolev static int
5158e96e13eSMaxim Sobolev gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
5168e96e13eSMaxim Sobolev {
5178e96e13eSMaxim Sobolev 	struct ifreq *ifr = (struct ifreq *)data;
5188e96e13eSMaxim Sobolev 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
5198e96e13eSMaxim Sobolev 	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
5208e96e13eSMaxim Sobolev 	struct gre_softc *sc = ifp->if_softc;
5218e96e13eSMaxim Sobolev 	int s;
5228e96e13eSMaxim Sobolev 	struct sockaddr_in si;
5238e96e13eSMaxim Sobolev 	struct sockaddr *sa = NULL;
524131c55bcSAndrew Thompson 	int error, adj;
5258e96e13eSMaxim Sobolev 	struct sockaddr_in sp, sm, dp, dm;
526131c55bcSAndrew Thompson 	uint32_t key;
5278e96e13eSMaxim Sobolev 
5288e96e13eSMaxim Sobolev 	error = 0;
529131c55bcSAndrew Thompson 	adj = 0;
5308e96e13eSMaxim Sobolev 
5318e96e13eSMaxim Sobolev 	s = splnet();
5328e96e13eSMaxim Sobolev 	switch (cmd) {
5338e96e13eSMaxim Sobolev 	case SIOCSIFADDR:
5348e96e13eSMaxim Sobolev 		ifp->if_flags |= IFF_UP;
5358e96e13eSMaxim Sobolev 		break;
5368e96e13eSMaxim Sobolev 	case SIOCSIFDSTADDR:
5378e96e13eSMaxim Sobolev 		break;
5388e96e13eSMaxim Sobolev 	case SIOCSIFFLAGS:
539acd3428bSRobert Watson 		/*
540cc9bdf2aSRobert Watson 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
541cc9bdf2aSRobert Watson 		 * layer check?
542acd3428bSRobert Watson 		 */
543acd3428bSRobert Watson 		if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
5448e96e13eSMaxim Sobolev 			break;
5458e96e13eSMaxim Sobolev 		if ((ifr->ifr_flags & IFF_LINK0) != 0)
5468e96e13eSMaxim Sobolev 			sc->g_proto = IPPROTO_GRE;
5478e96e13eSMaxim Sobolev 		else
5488e96e13eSMaxim Sobolev 			sc->g_proto = IPPROTO_MOBILE;
5497735aeb9SMaxim Sobolev 		if ((ifr->ifr_flags & IFF_LINK2) != 0)
5507735aeb9SMaxim Sobolev 			sc->wccp_ver = WCCP_V2;
5517735aeb9SMaxim Sobolev 		else
5527735aeb9SMaxim Sobolev 			sc->wccp_ver = WCCP_V1;
5538e96e13eSMaxim Sobolev 		goto recompute;
5548e96e13eSMaxim Sobolev 	case SIOCSIFMTU:
555acd3428bSRobert Watson 		/*
556cc9bdf2aSRobert Watson 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
557cc9bdf2aSRobert Watson 		 * layer check?
558acd3428bSRobert Watson 		 */
559acd3428bSRobert Watson 		if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
5608e96e13eSMaxim Sobolev 			break;
5618e96e13eSMaxim Sobolev 		if (ifr->ifr_mtu < 576) {
5628e96e13eSMaxim Sobolev 			error = EINVAL;
5638e96e13eSMaxim Sobolev 			break;
5648e96e13eSMaxim Sobolev 		}
5658e96e13eSMaxim Sobolev 		ifp->if_mtu = ifr->ifr_mtu;
5668e96e13eSMaxim Sobolev 		break;
5678e96e13eSMaxim Sobolev 	case SIOCGIFMTU:
568fc74a9f9SBrooks Davis 		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
5698e96e13eSMaxim Sobolev 		break;
5708e96e13eSMaxim Sobolev 	case SIOCADDMULTI:
571acd3428bSRobert Watson 		/*
572cc9bdf2aSRobert Watson 		 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
573cc9bdf2aSRobert Watson 		 * layer check?
574acd3428bSRobert Watson 		 */
575acd3428bSRobert Watson 		if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
576acd3428bSRobert Watson 			break;
577acd3428bSRobert Watson 		if (ifr == 0) {
578acd3428bSRobert Watson 			error = EAFNOSUPPORT;
579acd3428bSRobert Watson 			break;
580acd3428bSRobert Watson 		}
581acd3428bSRobert Watson 		switch (ifr->ifr_addr.sa_family) {
582acd3428bSRobert Watson #ifdef INET
583acd3428bSRobert Watson 		case AF_INET:
584acd3428bSRobert Watson 			break;
585acd3428bSRobert Watson #endif
586acd3428bSRobert Watson #ifdef INET6
587acd3428bSRobert Watson 		case AF_INET6:
588acd3428bSRobert Watson 			break;
589acd3428bSRobert Watson #endif
590acd3428bSRobert Watson 		default:
591acd3428bSRobert Watson 			error = EAFNOSUPPORT;
592acd3428bSRobert Watson 			break;
593acd3428bSRobert Watson 		}
594acd3428bSRobert Watson 		break;
5958e96e13eSMaxim Sobolev 	case SIOCDELMULTI:
596acd3428bSRobert Watson 		/*
597cc9bdf2aSRobert Watson 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
598cc9bdf2aSRobert Watson 		 * layer check?
599acd3428bSRobert Watson 		 */
600acd3428bSRobert Watson 		if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
6018e96e13eSMaxim Sobolev 			break;
6028e96e13eSMaxim Sobolev 		if (ifr == 0) {
6038e96e13eSMaxim Sobolev 			error = EAFNOSUPPORT;
6048e96e13eSMaxim Sobolev 			break;
6058e96e13eSMaxim Sobolev 		}
6068e96e13eSMaxim Sobolev 		switch (ifr->ifr_addr.sa_family) {
6078e96e13eSMaxim Sobolev #ifdef INET
6088e96e13eSMaxim Sobolev 		case AF_INET:
6098e96e13eSMaxim Sobolev 			break;
6108e96e13eSMaxim Sobolev #endif
6119e669156SBjoern A. Zeeb #ifdef INET6
6129e669156SBjoern A. Zeeb 		case AF_INET6:
6139e669156SBjoern A. Zeeb 			break;
6149e669156SBjoern A. Zeeb #endif
6158e96e13eSMaxim Sobolev 		default:
6168e96e13eSMaxim Sobolev 			error = EAFNOSUPPORT;
6178e96e13eSMaxim Sobolev 			break;
6188e96e13eSMaxim Sobolev 		}
6198e96e13eSMaxim Sobolev 		break;
6208e96e13eSMaxim Sobolev 	case GRESPROTO:
621acd3428bSRobert Watson 		/*
622cc9bdf2aSRobert Watson 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
623cc9bdf2aSRobert Watson 		 * layer check?
624acd3428bSRobert Watson 		 */
625acd3428bSRobert Watson 		if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
6268e96e13eSMaxim Sobolev 			break;
6278e96e13eSMaxim Sobolev 		sc->g_proto = ifr->ifr_flags;
6288e96e13eSMaxim Sobolev 		switch (sc->g_proto) {
6298e96e13eSMaxim Sobolev 		case IPPROTO_GRE:
6308e96e13eSMaxim Sobolev 			ifp->if_flags |= IFF_LINK0;
6318e96e13eSMaxim Sobolev 			break;
6328e96e13eSMaxim Sobolev 		case IPPROTO_MOBILE:
6338e96e13eSMaxim Sobolev 			ifp->if_flags &= ~IFF_LINK0;
6348e96e13eSMaxim Sobolev 			break;
6358e96e13eSMaxim Sobolev 		default:
6368e96e13eSMaxim Sobolev 			error = EPROTONOSUPPORT;
6378e96e13eSMaxim Sobolev 			break;
6388e96e13eSMaxim Sobolev 		}
6398e96e13eSMaxim Sobolev 		goto recompute;
6408e96e13eSMaxim Sobolev 	case GREGPROTO:
6418e96e13eSMaxim Sobolev 		ifr->ifr_flags = sc->g_proto;
6428e96e13eSMaxim Sobolev 		break;
6438e96e13eSMaxim Sobolev 	case GRESADDRS:
6448e96e13eSMaxim Sobolev 	case GRESADDRD:
645acd3428bSRobert Watson 		error = priv_check(curthread, PRIV_NET_GRE);
646acd3428bSRobert Watson 		if (error)
647acd3428bSRobert Watson 			return (error);
6488e96e13eSMaxim Sobolev 		/*
6498e96e13eSMaxim Sobolev 		 * set tunnel endpoints, compute a less specific route
6508e96e13eSMaxim Sobolev 		 * to the remote end and mark if as up
6518e96e13eSMaxim Sobolev 		 */
6528e96e13eSMaxim Sobolev 		sa = &ifr->ifr_addr;
6538e96e13eSMaxim Sobolev 		if (cmd == GRESADDRS)
6548e96e13eSMaxim Sobolev 			sc->g_src = (satosin(sa))->sin_addr;
6558e96e13eSMaxim Sobolev 		if (cmd == GRESADDRD)
6568e96e13eSMaxim Sobolev 			sc->g_dst = (satosin(sa))->sin_addr;
6578e96e13eSMaxim Sobolev 	recompute:
6588e96e13eSMaxim Sobolev #ifdef INET
6598e96e13eSMaxim Sobolev 		if (sc->encap != NULL) {
6608e96e13eSMaxim Sobolev 			encap_detach(sc->encap);
6618e96e13eSMaxim Sobolev 			sc->encap = NULL;
6628e96e13eSMaxim Sobolev 		}
6638e96e13eSMaxim Sobolev #endif
6648e96e13eSMaxim Sobolev 		if ((sc->g_src.s_addr != INADDR_ANY) &&
6658e96e13eSMaxim Sobolev 		    (sc->g_dst.s_addr != INADDR_ANY)) {
6668e96e13eSMaxim Sobolev 			bzero(&sp, sizeof(sp));
6678e96e13eSMaxim Sobolev 			bzero(&sm, sizeof(sm));
6688e96e13eSMaxim Sobolev 			bzero(&dp, sizeof(dp));
6698e96e13eSMaxim Sobolev 			bzero(&dm, sizeof(dm));
6708e96e13eSMaxim Sobolev 			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
6718e96e13eSMaxim Sobolev 			    sizeof(struct sockaddr_in);
6728e96e13eSMaxim Sobolev 			sp.sin_family = sm.sin_family = dp.sin_family =
6738e96e13eSMaxim Sobolev 			    dm.sin_family = AF_INET;
6748e96e13eSMaxim Sobolev 			sp.sin_addr = sc->g_src;
6758e96e13eSMaxim Sobolev 			dp.sin_addr = sc->g_dst;
6768e96e13eSMaxim Sobolev 			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
6778e96e13eSMaxim Sobolev 			    INADDR_BROADCAST;
6788e96e13eSMaxim Sobolev #ifdef INET
6798e96e13eSMaxim Sobolev 			sc->encap = encap_attach(AF_INET, sc->g_proto,
6808e96e13eSMaxim Sobolev 			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
6818e96e13eSMaxim Sobolev 			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
6828e96e13eSMaxim Sobolev 				&in_gre_protosw : &in_mobile_protosw, sc);
6838e96e13eSMaxim Sobolev 			if (sc->encap == NULL)
6848e96e13eSMaxim Sobolev 				printf("%s: unable to attach encap\n",
685fc74a9f9SBrooks Davis 				    if_name(GRE2IFP(sc)));
6868e96e13eSMaxim Sobolev #endif
6878e96e13eSMaxim Sobolev 			if (sc->route.ro_rt != 0) /* free old route */
6888e96e13eSMaxim Sobolev 				RTFREE(sc->route.ro_rt);
6898e96e13eSMaxim Sobolev 			if (gre_compute_route(sc) == 0)
69013f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_RUNNING;
6918e96e13eSMaxim Sobolev 			else
69213f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
6938e96e13eSMaxim Sobolev 		}
6948e96e13eSMaxim Sobolev 		break;
6958e96e13eSMaxim Sobolev 	case GREGADDRS:
6968e96e13eSMaxim Sobolev 		memset(&si, 0, sizeof(si));
6978e96e13eSMaxim Sobolev 		si.sin_family = AF_INET;
6988e96e13eSMaxim Sobolev 		si.sin_len = sizeof(struct sockaddr_in);
6998e96e13eSMaxim Sobolev 		si.sin_addr.s_addr = sc->g_src.s_addr;
7008e96e13eSMaxim Sobolev 		sa = sintosa(&si);
701e3416ab0SBjoern A. Zeeb 		error = prison_if(curthread->td_ucred, sa);
702e3416ab0SBjoern A. Zeeb 		if (error != 0)
703e3416ab0SBjoern A. Zeeb 			break;
7048e96e13eSMaxim Sobolev 		ifr->ifr_addr = *sa;
7058e96e13eSMaxim Sobolev 		break;
7068e96e13eSMaxim Sobolev 	case GREGADDRD:
7078e96e13eSMaxim Sobolev 		memset(&si, 0, sizeof(si));
7088e96e13eSMaxim Sobolev 		si.sin_family = AF_INET;
7098e96e13eSMaxim Sobolev 		si.sin_len = sizeof(struct sockaddr_in);
7108e96e13eSMaxim Sobolev 		si.sin_addr.s_addr = sc->g_dst.s_addr;
7118e96e13eSMaxim Sobolev 		sa = sintosa(&si);
712e3416ab0SBjoern A. Zeeb 		error = prison_if(curthread->td_ucred, sa);
713e3416ab0SBjoern A. Zeeb 		if (error != 0)
714e3416ab0SBjoern A. Zeeb 			break;
7158e96e13eSMaxim Sobolev 		ifr->ifr_addr = *sa;
7168e96e13eSMaxim Sobolev 		break;
7178e96e13eSMaxim Sobolev 	case SIOCSIFPHYADDR:
718acd3428bSRobert Watson 		/*
719cc9bdf2aSRobert Watson 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
720cc9bdf2aSRobert Watson 		 * layer check?
721acd3428bSRobert Watson 		 */
722acd3428bSRobert Watson 		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
7238e96e13eSMaxim Sobolev 			break;
7248e96e13eSMaxim Sobolev 		if (aifr->ifra_addr.sin_family != AF_INET ||
7258e96e13eSMaxim Sobolev 		    aifr->ifra_dstaddr.sin_family != AF_INET) {
7268e96e13eSMaxim Sobolev 			error = EAFNOSUPPORT;
7278e96e13eSMaxim Sobolev 			break;
7288e96e13eSMaxim Sobolev 		}
7298e96e13eSMaxim Sobolev 		if (aifr->ifra_addr.sin_len != sizeof(si) ||
7308e96e13eSMaxim Sobolev 		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
7318e96e13eSMaxim Sobolev 			error = EINVAL;
7328e96e13eSMaxim Sobolev 			break;
7338e96e13eSMaxim Sobolev 		}
7348e96e13eSMaxim Sobolev 		sc->g_src = aifr->ifra_addr.sin_addr;
7358e96e13eSMaxim Sobolev 		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
7368e96e13eSMaxim Sobolev 		goto recompute;
7378e96e13eSMaxim Sobolev 	case SIOCSLIFPHYADDR:
738acd3428bSRobert Watson 		/*
739cc9bdf2aSRobert Watson 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
740cc9bdf2aSRobert Watson 		 * layer check?
741acd3428bSRobert Watson 		 */
742acd3428bSRobert Watson 		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
7438e96e13eSMaxim Sobolev 			break;
7448e96e13eSMaxim Sobolev 		if (lifr->addr.ss_family != AF_INET ||
7458e96e13eSMaxim Sobolev 		    lifr->dstaddr.ss_family != AF_INET) {
7468e96e13eSMaxim Sobolev 			error = EAFNOSUPPORT;
7478e96e13eSMaxim Sobolev 			break;
7488e96e13eSMaxim Sobolev 		}
7498e96e13eSMaxim Sobolev 		if (lifr->addr.ss_len != sizeof(si) ||
7508e96e13eSMaxim Sobolev 		    lifr->dstaddr.ss_len != sizeof(si)) {
7518e96e13eSMaxim Sobolev 			error = EINVAL;
7528e96e13eSMaxim Sobolev 			break;
7538e96e13eSMaxim Sobolev 		}
754d03e5467SQing Li 		sc->g_src = (satosin(&lifr->addr))->sin_addr;
7558e96e13eSMaxim Sobolev 		sc->g_dst =
756d03e5467SQing Li 		    (satosin(&lifr->dstaddr))->sin_addr;
7578e96e13eSMaxim Sobolev 		goto recompute;
7588e96e13eSMaxim Sobolev 	case SIOCDIFPHYADDR:
759acd3428bSRobert Watson 		/*
760cc9bdf2aSRobert Watson 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
761cc9bdf2aSRobert Watson 		 * layer check?
762acd3428bSRobert Watson 		 */
763acd3428bSRobert Watson 		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
7648e96e13eSMaxim Sobolev 			break;
7658e96e13eSMaxim Sobolev 		sc->g_src.s_addr = INADDR_ANY;
7668e96e13eSMaxim Sobolev 		sc->g_dst.s_addr = INADDR_ANY;
7678e96e13eSMaxim Sobolev 		goto recompute;
7688e96e13eSMaxim Sobolev 	case SIOCGLIFPHYADDR:
7698e96e13eSMaxim Sobolev 		if (sc->g_src.s_addr == INADDR_ANY ||
7708e96e13eSMaxim Sobolev 		    sc->g_dst.s_addr == INADDR_ANY) {
7718e96e13eSMaxim Sobolev 			error = EADDRNOTAVAIL;
7728e96e13eSMaxim Sobolev 			break;
7738e96e13eSMaxim Sobolev 		}
7748e96e13eSMaxim Sobolev 		memset(&si, 0, sizeof(si));
7758e96e13eSMaxim Sobolev 		si.sin_family = AF_INET;
7768e96e13eSMaxim Sobolev 		si.sin_len = sizeof(struct sockaddr_in);
7778e96e13eSMaxim Sobolev 		si.sin_addr.s_addr = sc->g_src.s_addr;
778e3416ab0SBjoern A. Zeeb 		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
779e3416ab0SBjoern A. Zeeb 		if (error != 0)
780e3416ab0SBjoern A. Zeeb 			break;
7818e96e13eSMaxim Sobolev 		memcpy(&lifr->addr, &si, sizeof(si));
7828e96e13eSMaxim Sobolev 		si.sin_addr.s_addr = sc->g_dst.s_addr;
783e3416ab0SBjoern A. Zeeb 		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
784e3416ab0SBjoern A. Zeeb 		if (error != 0)
785e3416ab0SBjoern A. Zeeb 			break;
7868e96e13eSMaxim Sobolev 		memcpy(&lifr->dstaddr, &si, sizeof(si));
7878e96e13eSMaxim Sobolev 		break;
7888e96e13eSMaxim Sobolev 	case SIOCGIFPSRCADDR:
789f16770aeSBruce M Simpson #ifdef INET6
790f16770aeSBruce M Simpson 	case SIOCGIFPSRCADDR_IN6:
791f16770aeSBruce M Simpson #endif
7928e96e13eSMaxim Sobolev 		if (sc->g_src.s_addr == INADDR_ANY) {
7938e96e13eSMaxim Sobolev 			error = EADDRNOTAVAIL;
7948e96e13eSMaxim Sobolev 			break;
7958e96e13eSMaxim Sobolev 		}
7968e96e13eSMaxim Sobolev 		memset(&si, 0, sizeof(si));
7978e96e13eSMaxim Sobolev 		si.sin_family = AF_INET;
7988e96e13eSMaxim Sobolev 		si.sin_len = sizeof(struct sockaddr_in);
7998e96e13eSMaxim Sobolev 		si.sin_addr.s_addr = sc->g_src.s_addr;
800e3416ab0SBjoern A. Zeeb 		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
801e3416ab0SBjoern A. Zeeb 		if (error != 0)
802e3416ab0SBjoern A. Zeeb 			break;
8038e96e13eSMaxim Sobolev 		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
8048e96e13eSMaxim Sobolev 		break;
8058e96e13eSMaxim Sobolev 	case SIOCGIFPDSTADDR:
806f16770aeSBruce M Simpson #ifdef INET6
807f16770aeSBruce M Simpson 	case SIOCGIFPDSTADDR_IN6:
808f16770aeSBruce M Simpson #endif
8098e96e13eSMaxim Sobolev 		if (sc->g_dst.s_addr == INADDR_ANY) {
8108e96e13eSMaxim Sobolev 			error = EADDRNOTAVAIL;
8118e96e13eSMaxim Sobolev 			break;
8128e96e13eSMaxim Sobolev 		}
8138e96e13eSMaxim Sobolev 		memset(&si, 0, sizeof(si));
8148e96e13eSMaxim Sobolev 		si.sin_family = AF_INET;
8158e96e13eSMaxim Sobolev 		si.sin_len = sizeof(struct sockaddr_in);
8168e96e13eSMaxim Sobolev 		si.sin_addr.s_addr = sc->g_dst.s_addr;
817e3416ab0SBjoern A. Zeeb 		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
818e3416ab0SBjoern A. Zeeb 		if (error != 0)
819e3416ab0SBjoern A. Zeeb 			break;
8208e96e13eSMaxim Sobolev 		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
8218e96e13eSMaxim Sobolev 		break;
822131c55bcSAndrew Thompson 	case GRESKEY:
823131c55bcSAndrew Thompson 		error = priv_check(curthread, PRIV_NET_GRE);
824131c55bcSAndrew Thompson 		if (error)
825131c55bcSAndrew Thompson 			break;
826131c55bcSAndrew Thompson 		error = copyin(ifr->ifr_data, &key, sizeof(key));
827131c55bcSAndrew Thompson 		if (error)
828131c55bcSAndrew Thompson 			break;
829131c55bcSAndrew Thompson 		/* adjust MTU for option header */
830131c55bcSAndrew Thompson 		if (key == 0 && sc->key != 0)		/* clear */
831131c55bcSAndrew Thompson 			adj += sizeof(key);
832131c55bcSAndrew Thompson 		else if (key != 0 && sc->key == 0)	/* set */
833131c55bcSAndrew Thompson 			adj -= sizeof(key);
834131c55bcSAndrew Thompson 
835131c55bcSAndrew Thompson 		if (ifp->if_mtu + adj < 576) {
836131c55bcSAndrew Thompson 			error = EINVAL;
837131c55bcSAndrew Thompson 			break;
838131c55bcSAndrew Thompson 		}
839131c55bcSAndrew Thompson 		ifp->if_mtu += adj;
840131c55bcSAndrew Thompson 		sc->key = key;
841131c55bcSAndrew Thompson 		break;
842131c55bcSAndrew Thompson 	case GREGKEY:
843131c55bcSAndrew Thompson 		error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
844131c55bcSAndrew Thompson 		break;
845131c55bcSAndrew Thompson 
8468e96e13eSMaxim Sobolev 	default:
8478e96e13eSMaxim Sobolev 		error = EINVAL;
8488e96e13eSMaxim Sobolev 		break;
8498e96e13eSMaxim Sobolev 	}
8508e96e13eSMaxim Sobolev 
8518e96e13eSMaxim Sobolev 	splx(s);
8528e96e13eSMaxim Sobolev 	return (error);
8538e96e13eSMaxim Sobolev }
8548e96e13eSMaxim Sobolev 
8558e96e13eSMaxim Sobolev /*
8568e96e13eSMaxim Sobolev  * computes a route to our destination that is not the one
8578e96e13eSMaxim Sobolev  * which would be taken by ip_output(), as this one will loop back to
8588e96e13eSMaxim Sobolev  * us. If the interface is p2p as  a--->b, then a routing entry exists
8598e96e13eSMaxim Sobolev  * If we now send a packet to b (e.g. ping b), this will come down here
86073d7ddbcSMaxim Sobolev  * gets src=a, dst=b tacked on and would from ip_output() sent back to
8618e96e13eSMaxim Sobolev  * if_gre.
8628e96e13eSMaxim Sobolev  * Goal here is to compute a route to b that is less specific than
8638e96e13eSMaxim Sobolev  * a-->b. We know that this one exists as in normal operation we have
8648e96e13eSMaxim Sobolev  * at least a default route which matches.
8658e96e13eSMaxim Sobolev  */
866c23d234cSMaxim Sobolev static int
8678e96e13eSMaxim Sobolev gre_compute_route(struct gre_softc *sc)
8688e96e13eSMaxim Sobolev {
8698e96e13eSMaxim Sobolev 	struct route *ro;
8708e96e13eSMaxim Sobolev 
8718e96e13eSMaxim Sobolev 	ro = &sc->route;
8728e96e13eSMaxim Sobolev 
8738e96e13eSMaxim Sobolev 	memset(ro, 0, sizeof(struct route));
8748e96e13eSMaxim Sobolev 	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
8758e96e13eSMaxim Sobolev 	ro->ro_dst.sa_family = AF_INET;
8768e96e13eSMaxim Sobolev 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
8778e96e13eSMaxim Sobolev 
8788e96e13eSMaxim Sobolev 	/*
8798e96e13eSMaxim Sobolev 	 * toggle last bit, so our interface is not found, but a less
8808e96e13eSMaxim Sobolev 	 * specific route. I'd rather like to specify a shorter mask,
8818e96e13eSMaxim Sobolev 	 * but this is not possible. Should work though. XXX
8828b07e49aSJulian Elischer 	 * XXX MRT Use a different FIB for the tunnel to solve this problem.
8838e96e13eSMaxim Sobolev 	 */
884fc74a9f9SBrooks Davis 	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
88529481f88SJulian Elischer 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
88629481f88SJulian Elischer 		    htonl(0x01);
8878e96e13eSMaxim Sobolev 	}
8888e96e13eSMaxim Sobolev 
8898e96e13eSMaxim Sobolev #ifdef DIAGNOSTIC
890fc74a9f9SBrooks Davis 	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
8918e96e13eSMaxim Sobolev 	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
8928e96e13eSMaxim Sobolev #endif
8938e96e13eSMaxim Sobolev 
8948b07e49aSJulian Elischer 	rtalloc_fib(ro, sc->gre_fibnum);
8958e96e13eSMaxim Sobolev 
8968e96e13eSMaxim Sobolev 	/*
8978e96e13eSMaxim Sobolev 	 * check if this returned a route at all and this route is no
8988e96e13eSMaxim Sobolev 	 * recursion to ourself
8998e96e13eSMaxim Sobolev 	 */
9008e96e13eSMaxim Sobolev 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
9018e96e13eSMaxim Sobolev #ifdef DIAGNOSTIC
9028e96e13eSMaxim Sobolev 		if (ro->ro_rt == NULL)
9038e96e13eSMaxim Sobolev 			printf(" - no route found!\n");
9048e96e13eSMaxim Sobolev 		else
9058e96e13eSMaxim Sobolev 			printf(" - route loops back to ourself!\n");
9068e96e13eSMaxim Sobolev #endif
9078e96e13eSMaxim Sobolev 		return EADDRNOTAVAIL;
9088e96e13eSMaxim Sobolev 	}
9098e96e13eSMaxim Sobolev 
9108e96e13eSMaxim Sobolev 	/*
9118e96e13eSMaxim Sobolev 	 * now change it back - else ip_output will just drop
9128e96e13eSMaxim Sobolev 	 * the route and search one to this interface ...
9138e96e13eSMaxim Sobolev 	 */
914fc74a9f9SBrooks Davis 	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
9158e96e13eSMaxim Sobolev 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
9168e96e13eSMaxim Sobolev 
9178e96e13eSMaxim Sobolev #ifdef DIAGNOSTIC
9188e96e13eSMaxim Sobolev 	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
9198e96e13eSMaxim Sobolev 	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
9208e96e13eSMaxim Sobolev 	printf("\n");
9218e96e13eSMaxim Sobolev #endif
9228e96e13eSMaxim Sobolev 
9238e96e13eSMaxim Sobolev 	return 0;
9248e96e13eSMaxim Sobolev }
9258e96e13eSMaxim Sobolev 
9268e96e13eSMaxim Sobolev /*
9278e96e13eSMaxim Sobolev  * do a checksum of a buffer - much like in_cksum, which operates on
9288e96e13eSMaxim Sobolev  * mbufs.
9298e96e13eSMaxim Sobolev  */
93073d7ddbcSMaxim Sobolev u_int16_t
93173d7ddbcSMaxim Sobolev gre_in_cksum(u_int16_t *p, u_int len)
9328e96e13eSMaxim Sobolev {
93373d7ddbcSMaxim Sobolev 	u_int32_t sum = 0;
9348e96e13eSMaxim Sobolev 	int nwords = len >> 1;
9358e96e13eSMaxim Sobolev 
9368e96e13eSMaxim Sobolev 	while (nwords-- != 0)
9378e96e13eSMaxim Sobolev 		sum += *p++;
9388e96e13eSMaxim Sobolev 
9398e96e13eSMaxim Sobolev 	if (len & 1) {
9408e96e13eSMaxim Sobolev 		union {
9418e96e13eSMaxim Sobolev 			u_short w;
9428e96e13eSMaxim Sobolev 			u_char c[2];
9438e96e13eSMaxim Sobolev 		} u;
9448e96e13eSMaxim Sobolev 		u.c[0] = *(u_char *)p;
9458e96e13eSMaxim Sobolev 		u.c[1] = 0;
9468e96e13eSMaxim Sobolev 		sum += u.w;
9478e96e13eSMaxim Sobolev 	}
9488e96e13eSMaxim Sobolev 
9498e96e13eSMaxim Sobolev 	/* end-around-carry */
9508e96e13eSMaxim Sobolev 	sum = (sum >> 16) + (sum & 0xffff);
9518e96e13eSMaxim Sobolev 	sum += (sum >> 16);
9528e96e13eSMaxim Sobolev 	return (~sum);
9538e96e13eSMaxim Sobolev }
9548e96e13eSMaxim Sobolev 
9558e96e13eSMaxim Sobolev static int
9568e96e13eSMaxim Sobolev gremodevent(module_t mod, int type, void *data)
9578e96e13eSMaxim Sobolev {
9588e96e13eSMaxim Sobolev 
9598e96e13eSMaxim Sobolev 	switch (type) {
9608e96e13eSMaxim Sobolev 	case MOD_LOAD:
9618e96e13eSMaxim Sobolev 		greattach();
9628e96e13eSMaxim Sobolev 		break;
9638e96e13eSMaxim Sobolev 	case MOD_UNLOAD:
9648e96e13eSMaxim Sobolev 		if_clone_detach(&gre_cloner);
965bdae44a8SRobert Watson 		mtx_destroy(&gre_mtx);
9668e96e13eSMaxim Sobolev 		break;
9673e019deaSPoul-Henning Kamp 	default:
9683e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
9698e96e13eSMaxim Sobolev 	}
9708e96e13eSMaxim Sobolev 	return 0;
9718e96e13eSMaxim Sobolev }
9728e96e13eSMaxim Sobolev 
9738e96e13eSMaxim Sobolev static moduledata_t gre_mod = {
9748e96e13eSMaxim Sobolev 	"if_gre",
9758e96e13eSMaxim Sobolev 	gremodevent,
976*9823d527SKevin Lo 	0
9778e96e13eSMaxim Sobolev };
9788e96e13eSMaxim Sobolev 
9798e96e13eSMaxim Sobolev DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
9808e96e13eSMaxim Sobolev MODULE_VERSION(if_gre, 1);
981