if_gre.c revision 219206
1123992Ssobomax/*	$NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2103026Ssobomax/*	 $FreeBSD: head/sys/net/if_gre.c 219206 2011-03-02 21:39:08Z bz $ */
3103026Ssobomax
4139823Simp/*-
5103026Ssobomax * Copyright (c) 1998 The NetBSD Foundation, Inc.
6103026Ssobomax * All rights reserved.
7103026Ssobomax *
8103026Ssobomax * This code is derived from software contributed to The NetBSD Foundation
9103026Ssobomax * by Heiko W.Rupp <hwr@pilhuhn.de>
10103026Ssobomax *
11148613Sbz * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12148613Sbz *
13103026Ssobomax * Redistribution and use in source and binary forms, with or without
14103026Ssobomax * modification, are permitted provided that the following conditions
15103026Ssobomax * are met:
16103026Ssobomax * 1. Redistributions of source code must retain the above copyright
17103026Ssobomax *    notice, this list of conditions and the following disclaimer.
18103026Ssobomax * 2. Redistributions in binary form must reproduce the above copyright
19103026Ssobomax *    notice, this list of conditions and the following disclaimer in the
20103026Ssobomax *    documentation and/or other materials provided with the distribution.
21103026Ssobomax *
22103026Ssobomax * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23103026Ssobomax * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24103026Ssobomax * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25103026Ssobomax * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26103026Ssobomax * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27103026Ssobomax * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28103026Ssobomax * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29103026Ssobomax * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30103026Ssobomax * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31103026Ssobomax * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32103026Ssobomax * POSSIBILITY OF SUCH DAMAGE.
33103026Ssobomax */
34103026Ssobomax
35103026Ssobomax/*
36103026Ssobomax * Encapsulate L3 protocols into IP
37148613Sbz * See RFC 2784 (successor of RFC 1701 and 1702) for more details.
38103026Ssobomax * If_gre is compatible with Cisco GRE tunnels, so you can
39103026Ssobomax * have a NetBSD box as the other end of a tunnel interface of a Cisco
40103026Ssobomax * router. See gre(4) for more details.
41103026Ssobomax * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
42103026Ssobomax */
43103026Ssobomax
44103394Sbde#include "opt_atalk.h"
45103026Ssobomax#include "opt_inet.h"
46122699Sbms#include "opt_inet6.h"
47103026Ssobomax
48103026Ssobomax#include <sys/param.h>
49219206Sbz#include <sys/jail.h>
50103026Ssobomax#include <sys/kernel.h>
51103026Ssobomax#include <sys/malloc.h>
52129880Sphk#include <sys/module.h>
53103026Ssobomax#include <sys/mbuf.h>
54164033Srwatson#include <sys/priv.h>
55178888Sjulian#include <sys/proc.h>
56103026Ssobomax#include <sys/protosw.h>
57103026Ssobomax#include <sys/socket.h>
58103026Ssobomax#include <sys/sockio.h>
59103026Ssobomax#include <sys/sysctl.h>
60103344Sbde#include <sys/systm.h>
61103026Ssobomax
62103026Ssobomax#include <net/ethernet.h>
63103026Ssobomax#include <net/if.h>
64130933Sbrooks#include <net/if_clone.h>
65103026Ssobomax#include <net/if_types.h>
66103026Ssobomax#include <net/route.h>
67196019Srwatson#include <net/vnet.h>
68103026Ssobomax
69103026Ssobomax#ifdef INET
70103026Ssobomax#include <netinet/in.h>
71103026Ssobomax#include <netinet/in_systm.h>
72103026Ssobomax#include <netinet/in_var.h>
73103026Ssobomax#include <netinet/ip.h>
74103026Ssobomax#include <netinet/ip_gre.h>
75103026Ssobomax#include <netinet/ip_var.h>
76103026Ssobomax#include <netinet/ip_encap.h>
77103026Ssobomax#else
78103026Ssobomax#error "Huh? if_gre without inet?"
79103026Ssobomax#endif
80103026Ssobomax
81103026Ssobomax#include <net/bpf.h>
82103026Ssobomax
83103026Ssobomax#include <net/if_gre.h>
84103026Ssobomax
85103026Ssobomax/*
86103026Ssobomax * It is not easy to calculate the right value for a GRE MTU.
87103026Ssobomax * We leave this task to the admin and use the same default that
88103026Ssobomax * other vendors use.
89103026Ssobomax */
90103026Ssobomax#define GREMTU	1476
91103026Ssobomax
92103026Ssobomax#define GRENAME	"gre"
93103026Ssobomax
94127307Srwatson/*
95127307Srwatson * gre_mtx protects all global variables in if_gre.c.
96127307Srwatson * XXX: gre_softc data not protected yet.
97127307Srwatson */
98127307Srwatsonstruct mtx gre_mtx;
99103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
100103026Ssobomax
101103026Ssobomaxstruct gre_softc_head gre_softc_list;
102103026Ssobomax
103160195Ssamstatic int	gre_clone_create(struct if_clone *, int, caddr_t);
104105300Salfredstatic void	gre_clone_destroy(struct ifnet *);
105103032Ssobomaxstatic int	gre_ioctl(struct ifnet *, u_long, caddr_t);
106103032Ssobomaxstatic int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
107191148Skmacy		    struct route *ro);
108103026Ssobomax
109130933SbrooksIFC_SIMPLE_DECLARE(gre, 0);
110103026Ssobomax
111103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc);
112103026Ssobomax
113105300Salfredstatic void	greattach(void);
114103026Ssobomax
115103026Ssobomax#ifdef INET
116103026Ssobomaxextern struct domain inetdomain;
117152242Srustatic const struct protosw in_gre_protosw = {
118152242Sru	.pr_type =		SOCK_RAW,
119152242Sru	.pr_domain =		&inetdomain,
120152242Sru	.pr_protocol =		IPPROTO_GRE,
121152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
122154625Sbz	.pr_input =		gre_input,
123152242Sru	.pr_output =		(pr_output_t *)rip_output,
124152242Sru	.pr_ctlinput =		rip_ctlinput,
125152242Sru	.pr_ctloutput =		rip_ctloutput,
126152242Sru	.pr_usrreqs =		&rip_usrreqs
127103026Ssobomax};
128152242Srustatic const struct protosw in_mobile_protosw = {
129152242Sru	.pr_type =		SOCK_RAW,
130152242Sru	.pr_domain =		&inetdomain,
131152242Sru	.pr_protocol =		IPPROTO_MOBILE,
132152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
133154625Sbz	.pr_input =		gre_mobile_input,
134152242Sru	.pr_output =		(pr_output_t *)rip_output,
135152242Sru	.pr_ctlinput =		rip_ctlinput,
136152242Sru	.pr_ctloutput =		rip_ctloutput,
137152242Sru	.pr_usrreqs =		&rip_usrreqs
138103026Ssobomax};
139103026Ssobomax#endif
140103026Ssobomax
141103026SsobomaxSYSCTL_DECL(_net_link);
142123338SbmsSYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
143103026Ssobomax    "Generic Routing Encapsulation");
144103026Ssobomax#ifndef MAX_GRE_NEST
145103026Ssobomax/*
146103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels.
147103026Ssobomax * Since, setting a large value to this macro with a careless configuration
148103026Ssobomax * may introduce system crash, we don't allow any nestings by default.
149103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro
150103026Ssobomax * in your kernel configuration file.  However, if you do so, please be
151103026Ssobomax * careful to configure the tunnels so that it won't make a loop.
152103026Ssobomax */
153103026Ssobomax#define MAX_GRE_NEST 1
154103026Ssobomax#endif
155103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST;
156103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
157103026Ssobomax    &max_gre_nesting, 0, "Max nested tunnels");
158103026Ssobomax
159103026Ssobomax/* ARGSUSED */
160103032Ssobomaxstatic void
161103026Ssobomaxgreattach(void)
162103026Ssobomax{
163103026Ssobomax
164127307Srwatson	mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
165103026Ssobomax	LIST_INIT(&gre_softc_list);
166103026Ssobomax	if_clone_attach(&gre_cloner);
167103026Ssobomax}
168103026Ssobomax
169103032Ssobomaxstatic int
170160195Ssamgre_clone_create(ifc, unit, params)
171103026Ssobomax	struct if_clone *ifc;
172103026Ssobomax	int unit;
173160195Ssam	caddr_t params;
174103026Ssobomax{
175103026Ssobomax	struct gre_softc *sc;
176103026Ssobomax
177131673Sbms	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
178103026Ssobomax
179147643Sbz	GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
180147643Sbz	if (GRE2IFP(sc) == NULL) {
181147643Sbz		free(sc, M_GRE);
182147643Sbz		return (ENOSPC);
183147643Sbz	}
184147643Sbz
185147643Sbz	GRE2IFP(sc)->if_softc = sc;
186147256Sbrooks	if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
187147643Sbz
188207554Ssobomax	GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
189147256Sbrooks	GRE2IFP(sc)->if_addrlen = 0;
190147256Sbrooks	GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
191147256Sbrooks	GRE2IFP(sc)->if_mtu = GREMTU;
192147256Sbrooks	GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
193147256Sbrooks	GRE2IFP(sc)->if_output = gre_output;
194147256Sbrooks	GRE2IFP(sc)->if_ioctl = gre_ioctl;
195103026Ssobomax	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
196103026Ssobomax	sc->g_proto = IPPROTO_GRE;
197147256Sbrooks	GRE2IFP(sc)->if_flags |= IFF_LINK0;
198103026Ssobomax	sc->encap = NULL;
199103026Ssobomax	sc->called = 0;
200178888Sjulian	sc->gre_fibnum = curthread->td_proc->p_fibnum;
201125024Ssobomax	sc->wccp_ver = WCCP_V1;
202179894Sthompsa	sc->key = 0;
203147256Sbrooks	if_attach(GRE2IFP(sc));
204147256Sbrooks	bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
205127307Srwatson	mtx_lock(&gre_mtx);
206103026Ssobomax	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
207127307Srwatson	mtx_unlock(&gre_mtx);
208103026Ssobomax	return (0);
209103026Ssobomax}
210103026Ssobomax
211103032Ssobomaxstatic void
212127307Srwatsongre_clone_destroy(ifp)
213127307Srwatson	struct ifnet *ifp;
214127307Srwatson{
215127307Srwatson	struct gre_softc *sc = ifp->if_softc;
216127307Srwatson
217127307Srwatson	mtx_lock(&gre_mtx);
218127307Srwatson	LIST_REMOVE(sc, sc_list);
219127307Srwatson	mtx_unlock(&gre_mtx);
220151266Sthompsa
221151266Sthompsa#ifdef INET
222151266Sthompsa	if (sc->encap != NULL)
223151266Sthompsa		encap_detach(sc->encap);
224151266Sthompsa#endif
225151266Sthompsa	bpfdetach(ifp);
226151266Sthompsa	if_detach(ifp);
227151266Sthompsa	if_free(ifp);
228151266Sthompsa	free(sc, M_GRE);
229127307Srwatson}
230127307Srwatson
231103026Ssobomax/*
232103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol
233103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004
234103026Ssobomax */
235103032Ssobomaxstatic int
236103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
237191148Skmacy	   struct route *ro)
238103026Ssobomax{
239103026Ssobomax	int error = 0;
240103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
241103026Ssobomax	struct greip *gh;
242103026Ssobomax	struct ip *ip;
243180041Sjulian	u_short gre_ip_id = 0;
244180041Sjulian	uint8_t gre_ip_tos = 0;
245123992Ssobomax	u_int16_t etype = 0;
246103026Ssobomax	struct mobile_h mob_h;
247147611Sdwmalone	u_int32_t af;
248180639Sjulian	int extra = 0;
249103026Ssobomax
250103026Ssobomax	/*
251103026Ssobomax	 * gre may cause infinite recursion calls when misconfigured.
252103026Ssobomax	 * We'll prevent this by introducing upper limit.
253103026Ssobomax	 */
254103026Ssobomax	if (++(sc->called) > max_gre_nesting) {
255103026Ssobomax		printf("%s: gre_output: recursively called too many "
256147256Sbrooks		       "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
257103026Ssobomax		m_freem(m);
258103026Ssobomax		error = EIO;    /* is there better errno? */
259103026Ssobomax		goto end;
260103026Ssobomax	}
261103026Ssobomax
262148887Srwatson	if (!((ifp->if_flags & IFF_UP) &&
263148887Srwatson	    (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
264103026Ssobomax	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
265103026Ssobomax		m_freem(m);
266103026Ssobomax		error = ENETDOWN;
267103026Ssobomax		goto end;
268103026Ssobomax	}
269103026Ssobomax
270103026Ssobomax	gh = NULL;
271103026Ssobomax	ip = NULL;
272103026Ssobomax
273147611Sdwmalone	/* BPF writes need to be handled specially. */
274147611Sdwmalone	if (dst->sa_family == AF_UNSPEC) {
275147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
276147611Sdwmalone		dst->sa_family = af;
277147611Sdwmalone	}
278147611Sdwmalone
279159180Scsjp	if (bpf_peers_present(ifp->if_bpf)) {
280147611Sdwmalone		af = dst->sa_family;
281123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
282103026Ssobomax	}
283103026Ssobomax
284103026Ssobomax	m->m_flags &= ~(M_BCAST|M_MCAST);
285103026Ssobomax
286103026Ssobomax	if (sc->g_proto == IPPROTO_MOBILE) {
287103026Ssobomax		if (dst->sa_family == AF_INET) {
288103026Ssobomax			struct mbuf *m0;
289103026Ssobomax			int msiz;
290103026Ssobomax
291103026Ssobomax			ip = mtod(m, struct ip *);
292103026Ssobomax
293103026Ssobomax			/*
294103026Ssobomax			 * RFC2004 specifies that fragmented diagrams shouldn't
295103026Ssobomax			 * be encapsulated.
296103026Ssobomax			 */
297158416Shsu			if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
298103026Ssobomax				_IF_DROP(&ifp->if_snd);
299103026Ssobomax				m_freem(m);
300103026Ssobomax				error = EINVAL;    /* is there better errno? */
301103026Ssobomax				goto end;
302103026Ssobomax			}
303103026Ssobomax			memset(&mob_h, 0, MOB_H_SIZ_L);
304103026Ssobomax			mob_h.proto = (ip->ip_p) << 8;
305103026Ssobomax			mob_h.odst = ip->ip_dst.s_addr;
306103026Ssobomax			ip->ip_dst.s_addr = sc->g_dst.s_addr;
307103026Ssobomax
308103026Ssobomax			/*
309103026Ssobomax			 * If the packet comes from our host, we only change
310103026Ssobomax			 * the destination address in the IP header.
311103026Ssobomax			 * Else we also need to save and change the source
312103026Ssobomax			 */
313103026Ssobomax			if (in_hosteq(ip->ip_src, sc->g_src)) {
314103026Ssobomax				msiz = MOB_H_SIZ_S;
315103026Ssobomax			} else {
316103026Ssobomax				mob_h.proto |= MOB_H_SBIT;
317103026Ssobomax				mob_h.osrc = ip->ip_src.s_addr;
318103026Ssobomax				ip->ip_src.s_addr = sc->g_src.s_addr;
319103026Ssobomax				msiz = MOB_H_SIZ_L;
320103026Ssobomax			}
321103026Ssobomax			mob_h.proto = htons(mob_h.proto);
322123992Ssobomax			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
323103026Ssobomax
324103026Ssobomax			if ((m->m_data - msiz) < m->m_pktdat) {
325103026Ssobomax				/* need new mbuf */
326151967Sandre				MGETHDR(m0, M_DONTWAIT, MT_DATA);
327103026Ssobomax				if (m0 == NULL) {
328103026Ssobomax					_IF_DROP(&ifp->if_snd);
329103026Ssobomax					m_freem(m);
330103026Ssobomax					error = ENOBUFS;
331103026Ssobomax					goto end;
332103026Ssobomax				}
333103026Ssobomax				m0->m_next = m;
334103026Ssobomax				m->m_data += sizeof(struct ip);
335103026Ssobomax				m->m_len -= sizeof(struct ip);
336103026Ssobomax				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
337103026Ssobomax				m0->m_len = msiz + sizeof(struct ip);
338103026Ssobomax				m0->m_data += max_linkhdr;
339103026Ssobomax				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
340103026Ssobomax				       sizeof(struct ip));
341103026Ssobomax				m = m0;
342103026Ssobomax			} else {  /* we have some space left in the old one */
343103026Ssobomax				m->m_data -= msiz;
344103026Ssobomax				m->m_len += msiz;
345103026Ssobomax				m->m_pkthdr.len += msiz;
346103026Ssobomax				bcopy(ip, mtod(m, caddr_t),
347103026Ssobomax					sizeof(struct ip));
348103026Ssobomax			}
349103026Ssobomax			ip = mtod(m, struct ip *);
350103026Ssobomax			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
351103026Ssobomax			ip->ip_len = ntohs(ip->ip_len) + msiz;
352103026Ssobomax		} else {  /* AF_INET */
353103026Ssobomax			_IF_DROP(&ifp->if_snd);
354103026Ssobomax			m_freem(m);
355103026Ssobomax			error = EINVAL;
356103026Ssobomax			goto end;
357103026Ssobomax		}
358103026Ssobomax	} else if (sc->g_proto == IPPROTO_GRE) {
359103026Ssobomax		switch (dst->sa_family) {
360103026Ssobomax		case AF_INET:
361103026Ssobomax			ip = mtod(m, struct ip *);
362180041Sjulian			gre_ip_tos = ip->ip_tos;
363180041Sjulian			gre_ip_id = ip->ip_id;
364180639Sjulian			if (sc->wccp_ver == WCCP_V2) {
365180639Sjulian				extra = sizeof(uint32_t);
366180639Sjulian				etype =  WCCP_PROTOCOL_TYPE;
367180639Sjulian			} else {
368180639Sjulian				etype = ETHERTYPE_IP;
369180639Sjulian			}
370103026Ssobomax			break;
371148613Sbz#ifdef INET6
372148613Sbz		case AF_INET6:
373180041Sjulian			gre_ip_id = ip_newid();
374148613Sbz			etype = ETHERTYPE_IPV6;
375148613Sbz			break;
376148613Sbz#endif
377103026Ssobomax#ifdef NETATALK
378103026Ssobomax		case AF_APPLETALK:
379103026Ssobomax			etype = ETHERTYPE_ATALK;
380103026Ssobomax			break;
381103026Ssobomax#endif
382103026Ssobomax		default:
383103026Ssobomax			_IF_DROP(&ifp->if_snd);
384103026Ssobomax			m_freem(m);
385103026Ssobomax			error = EAFNOSUPPORT;
386103026Ssobomax			goto end;
387103026Ssobomax		}
388179894Sthompsa
389179894Sthompsa		/* Reserve space for GRE header + optional GRE key */
390180639Sjulian		int hdrlen = sizeof(struct greip) + extra;
391179894Sthompsa		if (sc->key)
392179894Sthompsa			hdrlen += sizeof(uint32_t);
393179894Sthompsa		M_PREPEND(m, hdrlen, M_DONTWAIT);
394103026Ssobomax	} else {
395103026Ssobomax		_IF_DROP(&ifp->if_snd);
396103026Ssobomax		m_freem(m);
397103026Ssobomax		error = EINVAL;
398103026Ssobomax		goto end;
399103026Ssobomax	}
400103026Ssobomax
401128580Sandre	if (m == NULL) {	/* mbuf allocation failed */
402103026Ssobomax		_IF_DROP(&ifp->if_snd);
403103026Ssobomax		error = ENOBUFS;
404103026Ssobomax		goto end;
405103026Ssobomax	}
406103026Ssobomax
407178888Sjulian	M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
408178888Sjulian
409103026Ssobomax	gh = mtod(m, struct greip *);
410103026Ssobomax	if (sc->g_proto == IPPROTO_GRE) {
411179894Sthompsa		uint32_t *options = gh->gi_options;
412179894Sthompsa
413180639Sjulian		memset((void *)gh, 0, sizeof(struct greip) + extra);
414103026Ssobomax		gh->gi_ptype = htons(etype);
415179894Sthompsa		gh->gi_flags = 0;
416179894Sthompsa
417179894Sthompsa		/* Add key option */
418179894Sthompsa		if (sc->key)
419179894Sthompsa		{
420179894Sthompsa			gh->gi_flags |= htons(GRE_KP);
421179894Sthompsa			*(options++) = htonl(sc->key);
422179894Sthompsa		}
423103026Ssobomax	}
424103026Ssobomax
425103026Ssobomax	gh->gi_pr = sc->g_proto;
426103026Ssobomax	if (sc->g_proto != IPPROTO_MOBILE) {
427103026Ssobomax		gh->gi_src = sc->g_src;
428103026Ssobomax		gh->gi_dst = sc->g_dst;
429133163Ssobomax		((struct ip*)gh)->ip_v = IPPROTO_IPV4;
430103026Ssobomax		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
431103032Ssobomax		((struct ip*)gh)->ip_ttl = GRE_TTL;
432180041Sjulian		((struct ip*)gh)->ip_tos = gre_ip_tos;
433180041Sjulian		((struct ip*)gh)->ip_id = gre_ip_id;
434125226Ssobomax		gh->gi_len = m->m_pkthdr.len;
435103026Ssobomax	}
436103026Ssobomax
437103026Ssobomax	ifp->if_opackets++;
438103026Ssobomax	ifp->if_obytes += m->m_pkthdr.len;
439128583Sandre	/*
440128583Sandre	 * Send it off and with IP_FORWARD flag to prevent it from
441128583Sandre	 * overwriting the ip_id again.  ip_id is already set to the
442128583Sandre	 * ip_id of the encapsulated packet.
443128583Sandre	 */
444128580Sandre	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
445123992Ssobomax	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
446103026Ssobomax  end:
447103026Ssobomax	sc->called = 0;
448103026Ssobomax	if (error)
449103026Ssobomax		ifp->if_oerrors++;
450103026Ssobomax	return (error);
451103026Ssobomax}
452103026Ssobomax
453103032Ssobomaxstatic int
454103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
455103026Ssobomax{
456103026Ssobomax	struct ifreq *ifr = (struct ifreq *)data;
457103026Ssobomax	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
458103026Ssobomax	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
459103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
460103026Ssobomax	int s;
461103026Ssobomax	struct sockaddr_in si;
462103026Ssobomax	struct sockaddr *sa = NULL;
463179894Sthompsa	int error, adj;
464103026Ssobomax	struct sockaddr_in sp, sm, dp, dm;
465179894Sthompsa	uint32_t key;
466103026Ssobomax
467103026Ssobomax	error = 0;
468179894Sthompsa	adj = 0;
469103026Ssobomax
470103026Ssobomax	s = splnet();
471103026Ssobomax	switch (cmd) {
472103026Ssobomax	case SIOCSIFADDR:
473103026Ssobomax		ifp->if_flags |= IFF_UP;
474103026Ssobomax		break;
475125020Ssobomax	case SIOCSIFDSTADDR:
476103026Ssobomax		break;
477103026Ssobomax	case SIOCSIFFLAGS:
478164033Srwatson		/*
479171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
480171056Srwatson		 * layer check?
481164033Srwatson		 */
482164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
483103026Ssobomax			break;
484103026Ssobomax		if ((ifr->ifr_flags & IFF_LINK0) != 0)
485103026Ssobomax			sc->g_proto = IPPROTO_GRE;
486103026Ssobomax		else
487103026Ssobomax			sc->g_proto = IPPROTO_MOBILE;
488125024Ssobomax		if ((ifr->ifr_flags & IFF_LINK2) != 0)
489125024Ssobomax			sc->wccp_ver = WCCP_V2;
490125024Ssobomax		else
491125024Ssobomax			sc->wccp_ver = WCCP_V1;
492103026Ssobomax		goto recompute;
493103026Ssobomax	case SIOCSIFMTU:
494164033Srwatson		/*
495171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
496171056Srwatson		 * layer check?
497164033Srwatson		 */
498164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
499103026Ssobomax			break;
500103026Ssobomax		if (ifr->ifr_mtu < 576) {
501103026Ssobomax			error = EINVAL;
502103026Ssobomax			break;
503103026Ssobomax		}
504103026Ssobomax		ifp->if_mtu = ifr->ifr_mtu;
505103026Ssobomax		break;
506103026Ssobomax	case SIOCGIFMTU:
507147256Sbrooks		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
508103026Ssobomax		break;
509103026Ssobomax	case SIOCADDMULTI:
510164033Srwatson		/*
511171056Srwatson		 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
512171056Srwatson		 * layer check?
513164033Srwatson		 */
514164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
515164033Srwatson			break;
516164033Srwatson		if (ifr == 0) {
517164033Srwatson			error = EAFNOSUPPORT;
518164033Srwatson			break;
519164033Srwatson		}
520164033Srwatson		switch (ifr->ifr_addr.sa_family) {
521164033Srwatson#ifdef INET
522164033Srwatson		case AF_INET:
523164033Srwatson			break;
524164033Srwatson#endif
525164033Srwatson#ifdef INET6
526164033Srwatson		case AF_INET6:
527164033Srwatson			break;
528164033Srwatson#endif
529164033Srwatson		default:
530164033Srwatson			error = EAFNOSUPPORT;
531164033Srwatson			break;
532164033Srwatson		}
533164033Srwatson		break;
534103026Ssobomax	case SIOCDELMULTI:
535164033Srwatson		/*
536171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
537171056Srwatson		 * layer check?
538164033Srwatson		 */
539164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
540103026Ssobomax			break;
541103026Ssobomax		if (ifr == 0) {
542103026Ssobomax			error = EAFNOSUPPORT;
543103026Ssobomax			break;
544103026Ssobomax		}
545103026Ssobomax		switch (ifr->ifr_addr.sa_family) {
546103026Ssobomax#ifdef INET
547103026Ssobomax		case AF_INET:
548103026Ssobomax			break;
549103026Ssobomax#endif
550148613Sbz#ifdef INET6
551148613Sbz		case AF_INET6:
552148613Sbz			break;
553148613Sbz#endif
554103026Ssobomax		default:
555103026Ssobomax			error = EAFNOSUPPORT;
556103026Ssobomax			break;
557103026Ssobomax		}
558103026Ssobomax		break;
559103026Ssobomax	case GRESPROTO:
560164033Srwatson		/*
561171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
562171056Srwatson		 * layer check?
563164033Srwatson		 */
564164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
565103026Ssobomax			break;
566103026Ssobomax		sc->g_proto = ifr->ifr_flags;
567103026Ssobomax		switch (sc->g_proto) {
568103026Ssobomax		case IPPROTO_GRE:
569103026Ssobomax			ifp->if_flags |= IFF_LINK0;
570103026Ssobomax			break;
571103026Ssobomax		case IPPROTO_MOBILE:
572103026Ssobomax			ifp->if_flags &= ~IFF_LINK0;
573103026Ssobomax			break;
574103026Ssobomax		default:
575103026Ssobomax			error = EPROTONOSUPPORT;
576103026Ssobomax			break;
577103026Ssobomax		}
578103026Ssobomax		goto recompute;
579103026Ssobomax	case GREGPROTO:
580103026Ssobomax		ifr->ifr_flags = sc->g_proto;
581103026Ssobomax		break;
582103026Ssobomax	case GRESADDRS:
583103026Ssobomax	case GRESADDRD:
584164033Srwatson		error = priv_check(curthread, PRIV_NET_GRE);
585164033Srwatson		if (error)
586164033Srwatson			return (error);
587103026Ssobomax		/*
588103026Ssobomax		 * set tunnel endpoints, compute a less specific route
589103026Ssobomax		 * to the remote end and mark if as up
590103026Ssobomax		 */
591103026Ssobomax		sa = &ifr->ifr_addr;
592103026Ssobomax		if (cmd == GRESADDRS)
593103026Ssobomax			sc->g_src = (satosin(sa))->sin_addr;
594103026Ssobomax		if (cmd == GRESADDRD)
595103026Ssobomax			sc->g_dst = (satosin(sa))->sin_addr;
596103026Ssobomax	recompute:
597103026Ssobomax#ifdef INET
598103026Ssobomax		if (sc->encap != NULL) {
599103026Ssobomax			encap_detach(sc->encap);
600103026Ssobomax			sc->encap = NULL;
601103026Ssobomax		}
602103026Ssobomax#endif
603103026Ssobomax		if ((sc->g_src.s_addr != INADDR_ANY) &&
604103026Ssobomax		    (sc->g_dst.s_addr != INADDR_ANY)) {
605103026Ssobomax			bzero(&sp, sizeof(sp));
606103026Ssobomax			bzero(&sm, sizeof(sm));
607103026Ssobomax			bzero(&dp, sizeof(dp));
608103026Ssobomax			bzero(&dm, sizeof(dm));
609103026Ssobomax			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
610103026Ssobomax			    sizeof(struct sockaddr_in);
611103026Ssobomax			sp.sin_family = sm.sin_family = dp.sin_family =
612103026Ssobomax			    dm.sin_family = AF_INET;
613103026Ssobomax			sp.sin_addr = sc->g_src;
614103026Ssobomax			dp.sin_addr = sc->g_dst;
615125020Ssobomax			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
616103026Ssobomax			    INADDR_BROADCAST;
617103026Ssobomax#ifdef INET
618103026Ssobomax			sc->encap = encap_attach(AF_INET, sc->g_proto,
619103026Ssobomax			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
620103026Ssobomax			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
621103026Ssobomax				&in_gre_protosw : &in_mobile_protosw, sc);
622103026Ssobomax			if (sc->encap == NULL)
623103026Ssobomax				printf("%s: unable to attach encap\n",
624147256Sbrooks				    if_name(GRE2IFP(sc)));
625103026Ssobomax#endif
626103026Ssobomax			if (sc->route.ro_rt != 0) /* free old route */
627103026Ssobomax				RTFREE(sc->route.ro_rt);
628103026Ssobomax			if (gre_compute_route(sc) == 0)
629148887Srwatson				ifp->if_drv_flags |= IFF_DRV_RUNNING;
630103026Ssobomax			else
631148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
632103026Ssobomax		}
633103026Ssobomax		break;
634103026Ssobomax	case GREGADDRS:
635103026Ssobomax		memset(&si, 0, sizeof(si));
636103026Ssobomax		si.sin_family = AF_INET;
637103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
638103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
639103026Ssobomax		sa = sintosa(&si);
640219206Sbz		error = prison_if(curthread->td_ucred, sa);
641219206Sbz		if (error != 0)
642219206Sbz			break;
643103026Ssobomax		ifr->ifr_addr = *sa;
644103026Ssobomax		break;
645103026Ssobomax	case GREGADDRD:
646103026Ssobomax		memset(&si, 0, sizeof(si));
647103026Ssobomax		si.sin_family = AF_INET;
648103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
649103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
650103026Ssobomax		sa = sintosa(&si);
651219206Sbz		error = prison_if(curthread->td_ucred, sa);
652219206Sbz		if (error != 0)
653219206Sbz			break;
654103026Ssobomax		ifr->ifr_addr = *sa;
655103026Ssobomax		break;
656103026Ssobomax	case SIOCSIFPHYADDR:
657164033Srwatson		/*
658171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
659171056Srwatson		 * layer check?
660164033Srwatson		 */
661164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
662103026Ssobomax			break;
663103026Ssobomax		if (aifr->ifra_addr.sin_family != AF_INET ||
664103026Ssobomax		    aifr->ifra_dstaddr.sin_family != AF_INET) {
665103026Ssobomax			error = EAFNOSUPPORT;
666103026Ssobomax			break;
667103026Ssobomax		}
668103026Ssobomax		if (aifr->ifra_addr.sin_len != sizeof(si) ||
669103026Ssobomax		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
670103026Ssobomax			error = EINVAL;
671103026Ssobomax			break;
672103026Ssobomax		}
673103026Ssobomax		sc->g_src = aifr->ifra_addr.sin_addr;
674103026Ssobomax		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
675103026Ssobomax		goto recompute;
676103026Ssobomax	case SIOCSLIFPHYADDR:
677164033Srwatson		/*
678171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
679171056Srwatson		 * layer check?
680164033Srwatson		 */
681164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
682103026Ssobomax			break;
683103026Ssobomax		if (lifr->addr.ss_family != AF_INET ||
684103026Ssobomax		    lifr->dstaddr.ss_family != AF_INET) {
685103026Ssobomax			error = EAFNOSUPPORT;
686103026Ssobomax			break;
687103026Ssobomax		}
688103026Ssobomax		if (lifr->addr.ss_len != sizeof(si) ||
689103026Ssobomax		    lifr->dstaddr.ss_len != sizeof(si)) {
690103026Ssobomax			error = EINVAL;
691103026Ssobomax			break;
692103026Ssobomax		}
693155440Sqingli		sc->g_src = (satosin(&lifr->addr))->sin_addr;
694103026Ssobomax		sc->g_dst =
695155440Sqingli		    (satosin(&lifr->dstaddr))->sin_addr;
696103026Ssobomax		goto recompute;
697103026Ssobomax	case SIOCDIFPHYADDR:
698164033Srwatson		/*
699171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
700171056Srwatson		 * layer check?
701164033Srwatson		 */
702164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
703103026Ssobomax			break;
704103026Ssobomax		sc->g_src.s_addr = INADDR_ANY;
705103026Ssobomax		sc->g_dst.s_addr = INADDR_ANY;
706103026Ssobomax		goto recompute;
707103026Ssobomax	case SIOCGLIFPHYADDR:
708103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY ||
709103026Ssobomax		    sc->g_dst.s_addr == INADDR_ANY) {
710103026Ssobomax			error = EADDRNOTAVAIL;
711103026Ssobomax			break;
712103026Ssobomax		}
713103026Ssobomax		memset(&si, 0, sizeof(si));
714103026Ssobomax		si.sin_family = AF_INET;
715103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
716103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
717219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
718219206Sbz		if (error != 0)
719219206Sbz			break;
720103026Ssobomax		memcpy(&lifr->addr, &si, sizeof(si));
721103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
722219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
723219206Sbz		if (error != 0)
724219206Sbz			break;
725103026Ssobomax		memcpy(&lifr->dstaddr, &si, sizeof(si));
726103026Ssobomax		break;
727103026Ssobomax	case SIOCGIFPSRCADDR:
728122699Sbms#ifdef INET6
729122699Sbms	case SIOCGIFPSRCADDR_IN6:
730122699Sbms#endif
731103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY) {
732103026Ssobomax			error = EADDRNOTAVAIL;
733103026Ssobomax			break;
734103026Ssobomax		}
735103026Ssobomax		memset(&si, 0, sizeof(si));
736103026Ssobomax		si.sin_family = AF_INET;
737103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
738103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
739219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
740219206Sbz		if (error != 0)
741219206Sbz			break;
742103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
743103026Ssobomax		break;
744103026Ssobomax	case SIOCGIFPDSTADDR:
745122699Sbms#ifdef INET6
746122699Sbms	case SIOCGIFPDSTADDR_IN6:
747122699Sbms#endif
748103026Ssobomax		if (sc->g_dst.s_addr == INADDR_ANY) {
749103026Ssobomax			error = EADDRNOTAVAIL;
750103026Ssobomax			break;
751103026Ssobomax		}
752103026Ssobomax		memset(&si, 0, sizeof(si));
753103026Ssobomax		si.sin_family = AF_INET;
754103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
755103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
756219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
757219206Sbz		if (error != 0)
758219206Sbz			break;
759103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
760103026Ssobomax		break;
761179894Sthompsa	case GRESKEY:
762179894Sthompsa		error = priv_check(curthread, PRIV_NET_GRE);
763179894Sthompsa		if (error)
764179894Sthompsa			break;
765179894Sthompsa		error = copyin(ifr->ifr_data, &key, sizeof(key));
766179894Sthompsa		if (error)
767179894Sthompsa			break;
768179894Sthompsa		/* adjust MTU for option header */
769179894Sthompsa		if (key == 0 && sc->key != 0)		/* clear */
770179894Sthompsa			adj += sizeof(key);
771179894Sthompsa		else if (key != 0 && sc->key == 0)	/* set */
772179894Sthompsa			adj -= sizeof(key);
773179894Sthompsa
774179894Sthompsa		if (ifp->if_mtu + adj < 576) {
775179894Sthompsa			error = EINVAL;
776179894Sthompsa			break;
777179894Sthompsa		}
778179894Sthompsa		ifp->if_mtu += adj;
779179894Sthompsa		sc->key = key;
780179894Sthompsa		break;
781179894Sthompsa	case GREGKEY:
782179894Sthompsa		error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
783179894Sthompsa		break;
784179894Sthompsa
785103026Ssobomax	default:
786103026Ssobomax		error = EINVAL;
787103026Ssobomax		break;
788103026Ssobomax	}
789103026Ssobomax
790103026Ssobomax	splx(s);
791103026Ssobomax	return (error);
792103026Ssobomax}
793103026Ssobomax
794103026Ssobomax/*
795103026Ssobomax * computes a route to our destination that is not the one
796103026Ssobomax * which would be taken by ip_output(), as this one will loop back to
797103026Ssobomax * us. If the interface is p2p as  a--->b, then a routing entry exists
798103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here
799123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to
800103026Ssobomax * if_gre.
801103026Ssobomax * Goal here is to compute a route to b that is less specific than
802103026Ssobomax * a-->b. We know that this one exists as in normal operation we have
803103026Ssobomax * at least a default route which matches.
804103026Ssobomax */
805103032Ssobomaxstatic int
806103026Ssobomaxgre_compute_route(struct gre_softc *sc)
807103026Ssobomax{
808103026Ssobomax	struct route *ro;
809103026Ssobomax
810103026Ssobomax	ro = &sc->route;
811103026Ssobomax
812103026Ssobomax	memset(ro, 0, sizeof(struct route));
813103026Ssobomax	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
814103026Ssobomax	ro->ro_dst.sa_family = AF_INET;
815103026Ssobomax	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
816103026Ssobomax
817103026Ssobomax	/*
818103026Ssobomax	 * toggle last bit, so our interface is not found, but a less
819103026Ssobomax	 * specific route. I'd rather like to specify a shorter mask,
820103026Ssobomax	 * but this is not possible. Should work though. XXX
821178888Sjulian	 * XXX MRT Use a different FIB for the tunnel to solve this problem.
822103026Ssobomax	 */
823147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
824177416Sjulian		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
825177416Sjulian		    htonl(0x01);
826103026Ssobomax	}
827103026Ssobomax
828103026Ssobomax#ifdef DIAGNOSTIC
829147256Sbrooks	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
830103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
831103026Ssobomax#endif
832103026Ssobomax
833178888Sjulian	rtalloc_fib(ro, sc->gre_fibnum);
834103026Ssobomax
835103026Ssobomax	/*
836103026Ssobomax	 * check if this returned a route at all and this route is no
837103026Ssobomax	 * recursion to ourself
838103026Ssobomax	 */
839103026Ssobomax	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
840103026Ssobomax#ifdef DIAGNOSTIC
841103026Ssobomax		if (ro->ro_rt == NULL)
842103026Ssobomax			printf(" - no route found!\n");
843103026Ssobomax		else
844103026Ssobomax			printf(" - route loops back to ourself!\n");
845103026Ssobomax#endif
846103026Ssobomax		return EADDRNOTAVAIL;
847103026Ssobomax	}
848103026Ssobomax
849103026Ssobomax	/*
850103026Ssobomax	 * now change it back - else ip_output will just drop
851103026Ssobomax	 * the route and search one to this interface ...
852103026Ssobomax	 */
853147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
854103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
855103026Ssobomax
856103026Ssobomax#ifdef DIAGNOSTIC
857103026Ssobomax	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
858103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
859103026Ssobomax	printf("\n");
860103026Ssobomax#endif
861103026Ssobomax
862103026Ssobomax	return 0;
863103026Ssobomax}
864103026Ssobomax
865103026Ssobomax/*
866103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on
867103026Ssobomax * mbufs.
868103026Ssobomax */
869123992Ssobomaxu_int16_t
870123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len)
871103026Ssobomax{
872123992Ssobomax	u_int32_t sum = 0;
873103026Ssobomax	int nwords = len >> 1;
874103026Ssobomax
875103026Ssobomax	while (nwords-- != 0)
876103026Ssobomax		sum += *p++;
877103026Ssobomax
878103026Ssobomax	if (len & 1) {
879103026Ssobomax		union {
880103026Ssobomax			u_short w;
881103026Ssobomax			u_char c[2];
882103026Ssobomax		} u;
883103026Ssobomax		u.c[0] = *(u_char *)p;
884103026Ssobomax		u.c[1] = 0;
885103026Ssobomax		sum += u.w;
886103026Ssobomax	}
887103026Ssobomax
888103026Ssobomax	/* end-around-carry */
889103026Ssobomax	sum = (sum >> 16) + (sum & 0xffff);
890103026Ssobomax	sum += (sum >> 16);
891103026Ssobomax	return (~sum);
892103026Ssobomax}
893103026Ssobomax
894103026Ssobomaxstatic int
895103026Ssobomaxgremodevent(module_t mod, int type, void *data)
896103026Ssobomax{
897103026Ssobomax
898103026Ssobomax	switch (type) {
899103026Ssobomax	case MOD_LOAD:
900103026Ssobomax		greattach();
901103026Ssobomax		break;
902103026Ssobomax	case MOD_UNLOAD:
903103026Ssobomax		if_clone_detach(&gre_cloner);
904127307Srwatson		mtx_destroy(&gre_mtx);
905103026Ssobomax		break;
906132199Sphk	default:
907132199Sphk		return EOPNOTSUPP;
908103026Ssobomax	}
909103026Ssobomax	return 0;
910103026Ssobomax}
911103026Ssobomax
912103026Ssobomaxstatic moduledata_t gre_mod = {
913103026Ssobomax	"if_gre",
914103026Ssobomax	gremodevent,
915103026Ssobomax	0
916103026Ssobomax};
917103026Ssobomax
918103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
919103026SsobomaxMODULE_VERSION(if_gre, 1);
920