1123992Ssobomax/*	$NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2103026Ssobomax/*	 $FreeBSD$ */
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>
51223223Sbz#include <sys/libkern.h>
52103026Ssobomax#include <sys/malloc.h>
53129880Sphk#include <sys/module.h>
54103026Ssobomax#include <sys/mbuf.h>
55164033Srwatson#include <sys/priv.h>
56178888Sjulian#include <sys/proc.h>
57103026Ssobomax#include <sys/protosw.h>
58103026Ssobomax#include <sys/socket.h>
59103026Ssobomax#include <sys/sockio.h>
60103026Ssobomax#include <sys/sysctl.h>
61103344Sbde#include <sys/systm.h>
62103026Ssobomax
63103026Ssobomax#include <net/ethernet.h>
64103026Ssobomax#include <net/if.h>
65130933Sbrooks#include <net/if_clone.h>
66103026Ssobomax#include <net/if_types.h>
67103026Ssobomax#include <net/route.h>
68196019Srwatson#include <net/vnet.h>
69103026Ssobomax
70103026Ssobomax#ifdef INET
71103026Ssobomax#include <netinet/in.h>
72103026Ssobomax#include <netinet/in_systm.h>
73103026Ssobomax#include <netinet/in_var.h>
74103026Ssobomax#include <netinet/ip.h>
75103026Ssobomax#include <netinet/ip_gre.h>
76103026Ssobomax#include <netinet/ip_var.h>
77103026Ssobomax#include <netinet/ip_encap.h>
78103026Ssobomax#else
79103026Ssobomax#error "Huh? if_gre without inet?"
80103026Ssobomax#endif
81103026Ssobomax
82103026Ssobomax#include <net/bpf.h>
83103026Ssobomax
84103026Ssobomax#include <net/if_gre.h>
85103026Ssobomax
86103026Ssobomax/*
87103026Ssobomax * It is not easy to calculate the right value for a GRE MTU.
88103026Ssobomax * We leave this task to the admin and use the same default that
89103026Ssobomax * other vendors use.
90103026Ssobomax */
91103026Ssobomax#define GREMTU	1476
92103026Ssobomax
93223223Sbz#define	MTAG_COOKIE_GRE		1307983903
94223223Sbz#define	MTAG_GRE_NESTING	1
95223223Sbzstruct mtag_gre_nesting {
96223223Sbz	uint16_t	count;
97223223Sbz	uint16_t	max;
98223223Sbz	struct ifnet	*ifp[];
99223223Sbz};
100223223Sbz
101127307Srwatson/*
102127307Srwatson * gre_mtx protects all global variables in if_gre.c.
103127307Srwatson * XXX: gre_softc data not protected yet.
104127307Srwatson */
105127307Srwatsonstruct mtx gre_mtx;
106241610Sglebiusstatic const char grename[] = "gre";
107241610Sglebiusstatic MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation");
108103026Ssobomax
109103026Ssobomaxstruct gre_softc_head gre_softc_list;
110103026Ssobomax
111160195Ssamstatic int	gre_clone_create(struct if_clone *, int, caddr_t);
112105300Salfredstatic void	gre_clone_destroy(struct ifnet *);
113241610Sglebiusstatic struct if_clone *gre_cloner;
114241610Sglebius
115103032Ssobomaxstatic int	gre_ioctl(struct ifnet *, u_long, caddr_t);
116249925Sglebiusstatic int	gre_output(struct ifnet *, struct mbuf *,
117249925Sglebius		    const struct sockaddr *, struct route *);
118103026Ssobomax
119103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc);
120103026Ssobomax
121105300Salfredstatic void	greattach(void);
122103026Ssobomax
123103026Ssobomax#ifdef INET
124103026Ssobomaxextern struct domain inetdomain;
125152242Srustatic const struct protosw in_gre_protosw = {
126152242Sru	.pr_type =		SOCK_RAW,
127152242Sru	.pr_domain =		&inetdomain,
128152242Sru	.pr_protocol =		IPPROTO_GRE,
129152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
130154625Sbz	.pr_input =		gre_input,
131152242Sru	.pr_output =		(pr_output_t *)rip_output,
132152242Sru	.pr_ctlinput =		rip_ctlinput,
133152242Sru	.pr_ctloutput =		rip_ctloutput,
134152242Sru	.pr_usrreqs =		&rip_usrreqs
135103026Ssobomax};
136152242Srustatic const struct protosw in_mobile_protosw = {
137152242Sru	.pr_type =		SOCK_RAW,
138152242Sru	.pr_domain =		&inetdomain,
139152242Sru	.pr_protocol =		IPPROTO_MOBILE,
140152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
141154625Sbz	.pr_input =		gre_mobile_input,
142152242Sru	.pr_output =		(pr_output_t *)rip_output,
143152242Sru	.pr_ctlinput =		rip_ctlinput,
144152242Sru	.pr_ctloutput =		rip_ctloutput,
145152242Sru	.pr_usrreqs =		&rip_usrreqs
146103026Ssobomax};
147103026Ssobomax#endif
148103026Ssobomax
149103026SsobomaxSYSCTL_DECL(_net_link);
150227309Sedstatic SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
151103026Ssobomax    "Generic Routing Encapsulation");
152103026Ssobomax#ifndef MAX_GRE_NEST
153103026Ssobomax/*
154103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels.
155103026Ssobomax * Since, setting a large value to this macro with a careless configuration
156103026Ssobomax * may introduce system crash, we don't allow any nestings by default.
157103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro
158103026Ssobomax * in your kernel configuration file.  However, if you do so, please be
159103026Ssobomax * careful to configure the tunnels so that it won't make a loop.
160103026Ssobomax */
161103026Ssobomax#define MAX_GRE_NEST 1
162103026Ssobomax#endif
163103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST;
164103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
165103026Ssobomax    &max_gre_nesting, 0, "Max nested tunnels");
166103026Ssobomax
167103026Ssobomax/* ARGSUSED */
168103032Ssobomaxstatic void
169103026Ssobomaxgreattach(void)
170103026Ssobomax{
171103026Ssobomax
172127307Srwatson	mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
173103026Ssobomax	LIST_INIT(&gre_softc_list);
174241610Sglebius	gre_cloner = if_clone_simple(grename, gre_clone_create,
175241610Sglebius	    gre_clone_destroy, 0);
176103026Ssobomax}
177103026Ssobomax
178103032Ssobomaxstatic int
179160195Ssamgre_clone_create(ifc, unit, params)
180103026Ssobomax	struct if_clone *ifc;
181103026Ssobomax	int unit;
182160195Ssam	caddr_t params;
183103026Ssobomax{
184103026Ssobomax	struct gre_softc *sc;
185103026Ssobomax
186131673Sbms	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
187103026Ssobomax
188147643Sbz	GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
189147643Sbz	if (GRE2IFP(sc) == NULL) {
190147643Sbz		free(sc, M_GRE);
191147643Sbz		return (ENOSPC);
192147643Sbz	}
193147643Sbz
194147643Sbz	GRE2IFP(sc)->if_softc = sc;
195241610Sglebius	if_initname(GRE2IFP(sc), grename, unit);
196147643Sbz
197207554Ssobomax	GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
198147256Sbrooks	GRE2IFP(sc)->if_addrlen = 0;
199147256Sbrooks	GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
200147256Sbrooks	GRE2IFP(sc)->if_mtu = GREMTU;
201147256Sbrooks	GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
202147256Sbrooks	GRE2IFP(sc)->if_output = gre_output;
203147256Sbrooks	GRE2IFP(sc)->if_ioctl = gre_ioctl;
204103026Ssobomax	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
205103026Ssobomax	sc->g_proto = IPPROTO_GRE;
206147256Sbrooks	GRE2IFP(sc)->if_flags |= IFF_LINK0;
207103026Ssobomax	sc->encap = NULL;
208178888Sjulian	sc->gre_fibnum = curthread->td_proc->p_fibnum;
209125024Ssobomax	sc->wccp_ver = WCCP_V1;
210179894Sthompsa	sc->key = 0;
211147256Sbrooks	if_attach(GRE2IFP(sc));
212147256Sbrooks	bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
213127307Srwatson	mtx_lock(&gre_mtx);
214103026Ssobomax	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
215127307Srwatson	mtx_unlock(&gre_mtx);
216103026Ssobomax	return (0);
217103026Ssobomax}
218103026Ssobomax
219103032Ssobomaxstatic void
220127307Srwatsongre_clone_destroy(ifp)
221127307Srwatson	struct ifnet *ifp;
222127307Srwatson{
223127307Srwatson	struct gre_softc *sc = ifp->if_softc;
224127307Srwatson
225127307Srwatson	mtx_lock(&gre_mtx);
226127307Srwatson	LIST_REMOVE(sc, sc_list);
227127307Srwatson	mtx_unlock(&gre_mtx);
228151266Sthompsa
229151266Sthompsa#ifdef INET
230151266Sthompsa	if (sc->encap != NULL)
231151266Sthompsa		encap_detach(sc->encap);
232151266Sthompsa#endif
233151266Sthompsa	bpfdetach(ifp);
234151266Sthompsa	if_detach(ifp);
235151266Sthompsa	if_free(ifp);
236151266Sthompsa	free(sc, M_GRE);
237127307Srwatson}
238127307Srwatson
239103026Ssobomax/*
240103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol
241103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004
242103026Ssobomax */
243103032Ssobomaxstatic int
244249925Sglebiusgre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
245191148Skmacy	   struct route *ro)
246103026Ssobomax{
247103026Ssobomax	int error = 0;
248103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
249103026Ssobomax	struct greip *gh;
250103026Ssobomax	struct ip *ip;
251223223Sbz	struct m_tag *mtag;
252223223Sbz	struct mtag_gre_nesting *gt;
253223223Sbz	size_t len;
254180041Sjulian	u_short gre_ip_id = 0;
255180041Sjulian	uint8_t gre_ip_tos = 0;
256123992Ssobomax	u_int16_t etype = 0;
257103026Ssobomax	struct mobile_h mob_h;
258147611Sdwmalone	u_int32_t af;
259223223Sbz	int extra = 0, max;
260103026Ssobomax
261103026Ssobomax	/*
262223223Sbz	 * gre may cause infinite recursion calls when misconfigured.  High
263223223Sbz	 * nesting level may cause stack exhaustion.  We'll prevent this by
264223223Sbz	 * detecting loops and by introducing upper limit.
265103026Ssobomax	 */
266223223Sbz	mtag = m_tag_locate(m, MTAG_COOKIE_GRE, MTAG_GRE_NESTING, NULL);
267223223Sbz	if (mtag != NULL) {
268223223Sbz		struct ifnet **ifp2;
269223223Sbz
270223223Sbz		gt = (struct mtag_gre_nesting *)(mtag + 1);
271223223Sbz		gt->count++;
272223223Sbz		if (gt->count > min(gt->max,max_gre_nesting)) {
273223223Sbz			printf("%s: hit maximum recursion limit %u on %s\n",
274223223Sbz				__func__, gt->count - 1, ifp->if_xname);
275223223Sbz			m_freem(m);
276223223Sbz			error = EIO;	/* is there better errno? */
277223223Sbz			goto end;
278223223Sbz		}
279223223Sbz
280223223Sbz		ifp2 = gt->ifp;
281223223Sbz		for (max = gt->count - 1; max > 0; max--) {
282223223Sbz			if (*ifp2 == ifp)
283223223Sbz				break;
284223223Sbz			ifp2++;
285223223Sbz		}
286223223Sbz		if (*ifp2 == ifp) {
287223223Sbz			printf("%s: detected loop with nexting %u on %s\n",
288223223Sbz				__func__, gt->count-1, ifp->if_xname);
289223223Sbz			m_freem(m);
290223223Sbz			error = EIO;	/* is there better errno? */
291223223Sbz			goto end;
292223223Sbz		}
293223223Sbz		*ifp2 = ifp;
294223223Sbz
295223223Sbz	} else {
296223223Sbz		/*
297223223Sbz		 * Given that people should NOT increase max_gre_nesting beyond
298223223Sbz		 * their real needs, we allocate once per packet rather than
299223223Sbz		 * allocating an mtag once per passing through gre.
300223223Sbz		 *
301223223Sbz		 * Note: the sysctl does not actually check for saneness, so we
302223223Sbz		 * limit the maximum numbers of possible recursions here.
303223223Sbz		 */
304223223Sbz		max = imin(max_gre_nesting, 256);
305223223Sbz		/* If someone sets the sysctl <= 0, we want at least 1. */
306223223Sbz		max = imax(max, 1);
307223223Sbz		len = sizeof(struct mtag_gre_nesting) +
308223223Sbz		    max * sizeof(struct ifnet *);
309223223Sbz		mtag = m_tag_alloc(MTAG_COOKIE_GRE, MTAG_GRE_NESTING, len,
310223223Sbz		    M_NOWAIT);
311223223Sbz		if (mtag == NULL) {
312223223Sbz			m_freem(m);
313223223Sbz			error = ENOMEM;
314223223Sbz			goto end;
315223223Sbz		}
316223223Sbz		gt = (struct mtag_gre_nesting *)(mtag + 1);
317223223Sbz		bzero(gt, len);
318223223Sbz		gt->count = 1;
319223223Sbz		gt->max = max;
320223223Sbz		*gt->ifp = ifp;
321223223Sbz		m_tag_prepend(m, mtag);
322103026Ssobomax	}
323103026Ssobomax
324148887Srwatson	if (!((ifp->if_flags & IFF_UP) &&
325148887Srwatson	    (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
326103026Ssobomax	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
327103026Ssobomax		m_freem(m);
328103026Ssobomax		error = ENETDOWN;
329103026Ssobomax		goto end;
330103026Ssobomax	}
331103026Ssobomax
332103026Ssobomax	gh = NULL;
333103026Ssobomax	ip = NULL;
334103026Ssobomax
335147611Sdwmalone	/* BPF writes need to be handled specially. */
336249925Sglebius	if (dst->sa_family == AF_UNSPEC)
337147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
338249925Sglebius	else
339249925Sglebius		af = dst->sa_family;
340147611Sdwmalone
341249925Sglebius	if (bpf_peers_present(ifp->if_bpf))
342123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
343103026Ssobomax
344250523Shrs	if ((ifp->if_flags & IFF_MONITOR) != 0) {
345250523Shrs		m_freem(m);
346250523Shrs		error = ENETDOWN;
347250523Shrs		goto end;
348250523Shrs	}
349250523Shrs
350103026Ssobomax	m->m_flags &= ~(M_BCAST|M_MCAST);
351103026Ssobomax
352103026Ssobomax	if (sc->g_proto == IPPROTO_MOBILE) {
353249925Sglebius		if (af == AF_INET) {
354103026Ssobomax			struct mbuf *m0;
355103026Ssobomax			int msiz;
356103026Ssobomax
357103026Ssobomax			ip = mtod(m, struct ip *);
358103026Ssobomax
359103026Ssobomax			/*
360103026Ssobomax			 * RFC2004 specifies that fragmented diagrams shouldn't
361103026Ssobomax			 * be encapsulated.
362103026Ssobomax			 */
363241913Sglebius			if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
364103026Ssobomax				_IF_DROP(&ifp->if_snd);
365103026Ssobomax				m_freem(m);
366103026Ssobomax				error = EINVAL;    /* is there better errno? */
367103026Ssobomax				goto end;
368103026Ssobomax			}
369103026Ssobomax			memset(&mob_h, 0, MOB_H_SIZ_L);
370103026Ssobomax			mob_h.proto = (ip->ip_p) << 8;
371103026Ssobomax			mob_h.odst = ip->ip_dst.s_addr;
372103026Ssobomax			ip->ip_dst.s_addr = sc->g_dst.s_addr;
373103026Ssobomax
374103026Ssobomax			/*
375103026Ssobomax			 * If the packet comes from our host, we only change
376103026Ssobomax			 * the destination address in the IP header.
377103026Ssobomax			 * Else we also need to save and change the source
378103026Ssobomax			 */
379103026Ssobomax			if (in_hosteq(ip->ip_src, sc->g_src)) {
380103026Ssobomax				msiz = MOB_H_SIZ_S;
381103026Ssobomax			} else {
382103026Ssobomax				mob_h.proto |= MOB_H_SBIT;
383103026Ssobomax				mob_h.osrc = ip->ip_src.s_addr;
384103026Ssobomax				ip->ip_src.s_addr = sc->g_src.s_addr;
385103026Ssobomax				msiz = MOB_H_SIZ_L;
386103026Ssobomax			}
387103026Ssobomax			mob_h.proto = htons(mob_h.proto);
388123992Ssobomax			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
389103026Ssobomax
390103026Ssobomax			if ((m->m_data - msiz) < m->m_pktdat) {
391248324Sglebius				m0 = m_gethdr(M_NOWAIT, MT_DATA);
392103026Ssobomax				if (m0 == NULL) {
393103026Ssobomax					_IF_DROP(&ifp->if_snd);
394103026Ssobomax					m_freem(m);
395103026Ssobomax					error = ENOBUFS;
396103026Ssobomax					goto end;
397103026Ssobomax				}
398103026Ssobomax				m0->m_next = m;
399103026Ssobomax				m->m_data += sizeof(struct ip);
400103026Ssobomax				m->m_len -= sizeof(struct ip);
401103026Ssobomax				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
402103026Ssobomax				m0->m_len = msiz + sizeof(struct ip);
403103026Ssobomax				m0->m_data += max_linkhdr;
404103026Ssobomax				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
405103026Ssobomax				       sizeof(struct ip));
406103026Ssobomax				m = m0;
407103026Ssobomax			} else {  /* we have some space left in the old one */
408103026Ssobomax				m->m_data -= msiz;
409103026Ssobomax				m->m_len += msiz;
410103026Ssobomax				m->m_pkthdr.len += msiz;
411103026Ssobomax				bcopy(ip, mtod(m, caddr_t),
412103026Ssobomax					sizeof(struct ip));
413103026Ssobomax			}
414103026Ssobomax			ip = mtod(m, struct ip *);
415103026Ssobomax			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
416241913Sglebius			ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
417103026Ssobomax		} else {  /* AF_INET */
418103026Ssobomax			_IF_DROP(&ifp->if_snd);
419103026Ssobomax			m_freem(m);
420103026Ssobomax			error = EINVAL;
421103026Ssobomax			goto end;
422103026Ssobomax		}
423103026Ssobomax	} else if (sc->g_proto == IPPROTO_GRE) {
424249925Sglebius		switch (af) {
425103026Ssobomax		case AF_INET:
426103026Ssobomax			ip = mtod(m, struct ip *);
427180041Sjulian			gre_ip_tos = ip->ip_tos;
428180041Sjulian			gre_ip_id = ip->ip_id;
429180639Sjulian			if (sc->wccp_ver == WCCP_V2) {
430180639Sjulian				extra = sizeof(uint32_t);
431180639Sjulian				etype =  WCCP_PROTOCOL_TYPE;
432180639Sjulian			} else {
433180639Sjulian				etype = ETHERTYPE_IP;
434180639Sjulian			}
435103026Ssobomax			break;
436148613Sbz#ifdef INET6
437148613Sbz		case AF_INET6:
438180041Sjulian			gre_ip_id = ip_newid();
439148613Sbz			etype = ETHERTYPE_IPV6;
440148613Sbz			break;
441148613Sbz#endif
442103026Ssobomax#ifdef NETATALK
443103026Ssobomax		case AF_APPLETALK:
444103026Ssobomax			etype = ETHERTYPE_ATALK;
445103026Ssobomax			break;
446103026Ssobomax#endif
447103026Ssobomax		default:
448103026Ssobomax			_IF_DROP(&ifp->if_snd);
449103026Ssobomax			m_freem(m);
450103026Ssobomax			error = EAFNOSUPPORT;
451103026Ssobomax			goto end;
452103026Ssobomax		}
453179894Sthompsa
454179894Sthompsa		/* Reserve space for GRE header + optional GRE key */
455180639Sjulian		int hdrlen = sizeof(struct greip) + extra;
456179894Sthompsa		if (sc->key)
457179894Sthompsa			hdrlen += sizeof(uint32_t);
458243882Sglebius		M_PREPEND(m, hdrlen, M_NOWAIT);
459103026Ssobomax	} else {
460103026Ssobomax		_IF_DROP(&ifp->if_snd);
461103026Ssobomax		m_freem(m);
462103026Ssobomax		error = EINVAL;
463103026Ssobomax		goto end;
464103026Ssobomax	}
465103026Ssobomax
466128580Sandre	if (m == NULL) {	/* mbuf allocation failed */
467103026Ssobomax		_IF_DROP(&ifp->if_snd);
468103026Ssobomax		error = ENOBUFS;
469103026Ssobomax		goto end;
470103026Ssobomax	}
471103026Ssobomax
472178888Sjulian	M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
473178888Sjulian
474103026Ssobomax	gh = mtod(m, struct greip *);
475103026Ssobomax	if (sc->g_proto == IPPROTO_GRE) {
476179894Sthompsa		uint32_t *options = gh->gi_options;
477179894Sthompsa
478180639Sjulian		memset((void *)gh, 0, sizeof(struct greip) + extra);
479103026Ssobomax		gh->gi_ptype = htons(etype);
480179894Sthompsa		gh->gi_flags = 0;
481179894Sthompsa
482179894Sthompsa		/* Add key option */
483179894Sthompsa		if (sc->key)
484179894Sthompsa		{
485179894Sthompsa			gh->gi_flags |= htons(GRE_KP);
486179894Sthompsa			*(options++) = htonl(sc->key);
487179894Sthompsa		}
488103026Ssobomax	}
489103026Ssobomax
490103026Ssobomax	gh->gi_pr = sc->g_proto;
491103026Ssobomax	if (sc->g_proto != IPPROTO_MOBILE) {
492103026Ssobomax		gh->gi_src = sc->g_src;
493103026Ssobomax		gh->gi_dst = sc->g_dst;
494133163Ssobomax		((struct ip*)gh)->ip_v = IPPROTO_IPV4;
495103026Ssobomax		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
496103032Ssobomax		((struct ip*)gh)->ip_ttl = GRE_TTL;
497180041Sjulian		((struct ip*)gh)->ip_tos = gre_ip_tos;
498180041Sjulian		((struct ip*)gh)->ip_id = gre_ip_id;
499241913Sglebius		gh->gi_len = htons(m->m_pkthdr.len);
500103026Ssobomax	}
501103026Ssobomax
502103026Ssobomax	ifp->if_opackets++;
503103026Ssobomax	ifp->if_obytes += m->m_pkthdr.len;
504128583Sandre	/*
505128583Sandre	 * Send it off and with IP_FORWARD flag to prevent it from
506128583Sandre	 * overwriting the ip_id again.  ip_id is already set to the
507128583Sandre	 * ip_id of the encapsulated packet.
508128583Sandre	 */
509128580Sandre	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
510123992Ssobomax	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
511103026Ssobomax  end:
512103026Ssobomax	if (error)
513103026Ssobomax		ifp->if_oerrors++;
514103026Ssobomax	return (error);
515103026Ssobomax}
516103026Ssobomax
517103032Ssobomaxstatic int
518103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
519103026Ssobomax{
520103026Ssobomax	struct ifreq *ifr = (struct ifreq *)data;
521103026Ssobomax	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
522103026Ssobomax	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
523103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
524103026Ssobomax	struct sockaddr_in si;
525103026Ssobomax	struct sockaddr *sa = NULL;
526179894Sthompsa	int error, adj;
527103026Ssobomax	struct sockaddr_in sp, sm, dp, dm;
528179894Sthompsa	uint32_t key;
529103026Ssobomax
530103026Ssobomax	error = 0;
531179894Sthompsa	adj = 0;
532103026Ssobomax
533103026Ssobomax	switch (cmd) {
534103026Ssobomax	case SIOCSIFADDR:
535103026Ssobomax		ifp->if_flags |= IFF_UP;
536103026Ssobomax		break;
537103026Ssobomax	case SIOCSIFFLAGS:
538164033Srwatson		/*
539171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
540171056Srwatson		 * layer check?
541164033Srwatson		 */
542164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
543103026Ssobomax			break;
544103026Ssobomax		if ((ifr->ifr_flags & IFF_LINK0) != 0)
545103026Ssobomax			sc->g_proto = IPPROTO_GRE;
546103026Ssobomax		else
547103026Ssobomax			sc->g_proto = IPPROTO_MOBILE;
548125024Ssobomax		if ((ifr->ifr_flags & IFF_LINK2) != 0)
549125024Ssobomax			sc->wccp_ver = WCCP_V2;
550125024Ssobomax		else
551125024Ssobomax			sc->wccp_ver = WCCP_V1;
552103026Ssobomax		goto recompute;
553103026Ssobomax	case SIOCSIFMTU:
554164033Srwatson		/*
555171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
556171056Srwatson		 * layer check?
557164033Srwatson		 */
558164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
559103026Ssobomax			break;
560103026Ssobomax		if (ifr->ifr_mtu < 576) {
561103026Ssobomax			error = EINVAL;
562103026Ssobomax			break;
563103026Ssobomax		}
564103026Ssobomax		ifp->if_mtu = ifr->ifr_mtu;
565103026Ssobomax		break;
566103026Ssobomax	case SIOCGIFMTU:
567147256Sbrooks		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
568103026Ssobomax		break;
569103026Ssobomax	case SIOCADDMULTI:
570164033Srwatson		/*
571171056Srwatson		 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
572171056Srwatson		 * layer check?
573164033Srwatson		 */
574164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
575164033Srwatson			break;
576164033Srwatson		if (ifr == 0) {
577164033Srwatson			error = EAFNOSUPPORT;
578164033Srwatson			break;
579164033Srwatson		}
580164033Srwatson		switch (ifr->ifr_addr.sa_family) {
581164033Srwatson#ifdef INET
582164033Srwatson		case AF_INET:
583164033Srwatson			break;
584164033Srwatson#endif
585164033Srwatson#ifdef INET6
586164033Srwatson		case AF_INET6:
587164033Srwatson			break;
588164033Srwatson#endif
589164033Srwatson		default:
590164033Srwatson			error = EAFNOSUPPORT;
591164033Srwatson			break;
592164033Srwatson		}
593164033Srwatson		break;
594103026Ssobomax	case SIOCDELMULTI:
595164033Srwatson		/*
596171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
597171056Srwatson		 * layer check?
598164033Srwatson		 */
599164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
600103026Ssobomax			break;
601103026Ssobomax		if (ifr == 0) {
602103026Ssobomax			error = EAFNOSUPPORT;
603103026Ssobomax			break;
604103026Ssobomax		}
605103026Ssobomax		switch (ifr->ifr_addr.sa_family) {
606103026Ssobomax#ifdef INET
607103026Ssobomax		case AF_INET:
608103026Ssobomax			break;
609103026Ssobomax#endif
610148613Sbz#ifdef INET6
611148613Sbz		case AF_INET6:
612148613Sbz			break;
613148613Sbz#endif
614103026Ssobomax		default:
615103026Ssobomax			error = EAFNOSUPPORT;
616103026Ssobomax			break;
617103026Ssobomax		}
618103026Ssobomax		break;
619103026Ssobomax	case GRESPROTO:
620164033Srwatson		/*
621171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
622171056Srwatson		 * layer check?
623164033Srwatson		 */
624164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
625103026Ssobomax			break;
626103026Ssobomax		sc->g_proto = ifr->ifr_flags;
627103026Ssobomax		switch (sc->g_proto) {
628103026Ssobomax		case IPPROTO_GRE:
629103026Ssobomax			ifp->if_flags |= IFF_LINK0;
630103026Ssobomax			break;
631103026Ssobomax		case IPPROTO_MOBILE:
632103026Ssobomax			ifp->if_flags &= ~IFF_LINK0;
633103026Ssobomax			break;
634103026Ssobomax		default:
635103026Ssobomax			error = EPROTONOSUPPORT;
636103026Ssobomax			break;
637103026Ssobomax		}
638103026Ssobomax		goto recompute;
639103026Ssobomax	case GREGPROTO:
640103026Ssobomax		ifr->ifr_flags = sc->g_proto;
641103026Ssobomax		break;
642103026Ssobomax	case GRESADDRS:
643103026Ssobomax	case GRESADDRD:
644164033Srwatson		error = priv_check(curthread, PRIV_NET_GRE);
645164033Srwatson		if (error)
646164033Srwatson			return (error);
647103026Ssobomax		/*
648103026Ssobomax		 * set tunnel endpoints, compute a less specific route
649103026Ssobomax		 * to the remote end and mark if as up
650103026Ssobomax		 */
651103026Ssobomax		sa = &ifr->ifr_addr;
652103026Ssobomax		if (cmd == GRESADDRS)
653103026Ssobomax			sc->g_src = (satosin(sa))->sin_addr;
654103026Ssobomax		if (cmd == GRESADDRD)
655103026Ssobomax			sc->g_dst = (satosin(sa))->sin_addr;
656103026Ssobomax	recompute:
657103026Ssobomax#ifdef INET
658103026Ssobomax		if (sc->encap != NULL) {
659103026Ssobomax			encap_detach(sc->encap);
660103026Ssobomax			sc->encap = NULL;
661103026Ssobomax		}
662103026Ssobomax#endif
663103026Ssobomax		if ((sc->g_src.s_addr != INADDR_ANY) &&
664103026Ssobomax		    (sc->g_dst.s_addr != INADDR_ANY)) {
665103026Ssobomax			bzero(&sp, sizeof(sp));
666103026Ssobomax			bzero(&sm, sizeof(sm));
667103026Ssobomax			bzero(&dp, sizeof(dp));
668103026Ssobomax			bzero(&dm, sizeof(dm));
669103026Ssobomax			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
670103026Ssobomax			    sizeof(struct sockaddr_in);
671103026Ssobomax			sp.sin_family = sm.sin_family = dp.sin_family =
672103026Ssobomax			    dm.sin_family = AF_INET;
673103026Ssobomax			sp.sin_addr = sc->g_src;
674103026Ssobomax			dp.sin_addr = sc->g_dst;
675125020Ssobomax			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
676103026Ssobomax			    INADDR_BROADCAST;
677103026Ssobomax#ifdef INET
678103026Ssobomax			sc->encap = encap_attach(AF_INET, sc->g_proto,
679103026Ssobomax			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
680103026Ssobomax			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
681103026Ssobomax				&in_gre_protosw : &in_mobile_protosw, sc);
682103026Ssobomax			if (sc->encap == NULL)
683103026Ssobomax				printf("%s: unable to attach encap\n",
684147256Sbrooks				    if_name(GRE2IFP(sc)));
685103026Ssobomax#endif
686103026Ssobomax			if (sc->route.ro_rt != 0) /* free old route */
687103026Ssobomax				RTFREE(sc->route.ro_rt);
688103026Ssobomax			if (gre_compute_route(sc) == 0)
689148887Srwatson				ifp->if_drv_flags |= IFF_DRV_RUNNING;
690103026Ssobomax			else
691148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
692103026Ssobomax		}
693103026Ssobomax		break;
694103026Ssobomax	case GREGADDRS:
695103026Ssobomax		memset(&si, 0, sizeof(si));
696103026Ssobomax		si.sin_family = AF_INET;
697103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
698103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
699103026Ssobomax		sa = sintosa(&si);
700219206Sbz		error = prison_if(curthread->td_ucred, sa);
701219206Sbz		if (error != 0)
702219206Sbz			break;
703103026Ssobomax		ifr->ifr_addr = *sa;
704103026Ssobomax		break;
705103026Ssobomax	case GREGADDRD:
706103026Ssobomax		memset(&si, 0, sizeof(si));
707103026Ssobomax		si.sin_family = AF_INET;
708103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
709103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
710103026Ssobomax		sa = sintosa(&si);
711219206Sbz		error = prison_if(curthread->td_ucred, sa);
712219206Sbz		if (error != 0)
713219206Sbz			break;
714103026Ssobomax		ifr->ifr_addr = *sa;
715103026Ssobomax		break;
716103026Ssobomax	case SIOCSIFPHYADDR:
717164033Srwatson		/*
718171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
719171056Srwatson		 * layer check?
720164033Srwatson		 */
721164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
722103026Ssobomax			break;
723103026Ssobomax		if (aifr->ifra_addr.sin_family != AF_INET ||
724103026Ssobomax		    aifr->ifra_dstaddr.sin_family != AF_INET) {
725103026Ssobomax			error = EAFNOSUPPORT;
726103026Ssobomax			break;
727103026Ssobomax		}
728103026Ssobomax		if (aifr->ifra_addr.sin_len != sizeof(si) ||
729103026Ssobomax		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
730103026Ssobomax			error = EINVAL;
731103026Ssobomax			break;
732103026Ssobomax		}
733103026Ssobomax		sc->g_src = aifr->ifra_addr.sin_addr;
734103026Ssobomax		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
735103026Ssobomax		goto recompute;
736103026Ssobomax	case SIOCSLIFPHYADDR:
737164033Srwatson		/*
738171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
739171056Srwatson		 * layer check?
740164033Srwatson		 */
741164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
742103026Ssobomax			break;
743103026Ssobomax		if (lifr->addr.ss_family != AF_INET ||
744103026Ssobomax		    lifr->dstaddr.ss_family != AF_INET) {
745103026Ssobomax			error = EAFNOSUPPORT;
746103026Ssobomax			break;
747103026Ssobomax		}
748103026Ssobomax		if (lifr->addr.ss_len != sizeof(si) ||
749103026Ssobomax		    lifr->dstaddr.ss_len != sizeof(si)) {
750103026Ssobomax			error = EINVAL;
751103026Ssobomax			break;
752103026Ssobomax		}
753155440Sqingli		sc->g_src = (satosin(&lifr->addr))->sin_addr;
754103026Ssobomax		sc->g_dst =
755155440Sqingli		    (satosin(&lifr->dstaddr))->sin_addr;
756103026Ssobomax		goto recompute;
757103026Ssobomax	case SIOCDIFPHYADDR:
758164033Srwatson		/*
759171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
760171056Srwatson		 * layer check?
761164033Srwatson		 */
762164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
763103026Ssobomax			break;
764103026Ssobomax		sc->g_src.s_addr = INADDR_ANY;
765103026Ssobomax		sc->g_dst.s_addr = INADDR_ANY;
766103026Ssobomax		goto recompute;
767103026Ssobomax	case SIOCGLIFPHYADDR:
768103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY ||
769103026Ssobomax		    sc->g_dst.s_addr == INADDR_ANY) {
770103026Ssobomax			error = EADDRNOTAVAIL;
771103026Ssobomax			break;
772103026Ssobomax		}
773103026Ssobomax		memset(&si, 0, sizeof(si));
774103026Ssobomax		si.sin_family = AF_INET;
775103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
776103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
777219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
778219206Sbz		if (error != 0)
779219206Sbz			break;
780103026Ssobomax		memcpy(&lifr->addr, &si, sizeof(si));
781103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
782219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
783219206Sbz		if (error != 0)
784219206Sbz			break;
785103026Ssobomax		memcpy(&lifr->dstaddr, &si, sizeof(si));
786103026Ssobomax		break;
787103026Ssobomax	case SIOCGIFPSRCADDR:
788122699Sbms#ifdef INET6
789122699Sbms	case SIOCGIFPSRCADDR_IN6:
790122699Sbms#endif
791103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY) {
792103026Ssobomax			error = EADDRNOTAVAIL;
793103026Ssobomax			break;
794103026Ssobomax		}
795103026Ssobomax		memset(&si, 0, sizeof(si));
796103026Ssobomax		si.sin_family = AF_INET;
797103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
798103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
799219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
800219206Sbz		if (error != 0)
801219206Sbz			break;
802103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
803103026Ssobomax		break;
804103026Ssobomax	case SIOCGIFPDSTADDR:
805122699Sbms#ifdef INET6
806122699Sbms	case SIOCGIFPDSTADDR_IN6:
807122699Sbms#endif
808103026Ssobomax		if (sc->g_dst.s_addr == INADDR_ANY) {
809103026Ssobomax			error = EADDRNOTAVAIL;
810103026Ssobomax			break;
811103026Ssobomax		}
812103026Ssobomax		memset(&si, 0, sizeof(si));
813103026Ssobomax		si.sin_family = AF_INET;
814103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
815103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
816219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
817219206Sbz		if (error != 0)
818219206Sbz			break;
819103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
820103026Ssobomax		break;
821179894Sthompsa	case GRESKEY:
822179894Sthompsa		error = priv_check(curthread, PRIV_NET_GRE);
823179894Sthompsa		if (error)
824179894Sthompsa			break;
825179894Sthompsa		error = copyin(ifr->ifr_data, &key, sizeof(key));
826179894Sthompsa		if (error)
827179894Sthompsa			break;
828179894Sthompsa		/* adjust MTU for option header */
829179894Sthompsa		if (key == 0 && sc->key != 0)		/* clear */
830179894Sthompsa			adj += sizeof(key);
831179894Sthompsa		else if (key != 0 && sc->key == 0)	/* set */
832179894Sthompsa			adj -= sizeof(key);
833179894Sthompsa
834179894Sthompsa		if (ifp->if_mtu + adj < 576) {
835179894Sthompsa			error = EINVAL;
836179894Sthompsa			break;
837179894Sthompsa		}
838179894Sthompsa		ifp->if_mtu += adj;
839179894Sthompsa		sc->key = key;
840179894Sthompsa		break;
841179894Sthompsa	case GREGKEY:
842179894Sthompsa		error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
843179894Sthompsa		break;
844179894Sthompsa
845103026Ssobomax	default:
846103026Ssobomax		error = EINVAL;
847103026Ssobomax		break;
848103026Ssobomax	}
849103026Ssobomax
850103026Ssobomax	return (error);
851103026Ssobomax}
852103026Ssobomax
853103026Ssobomax/*
854103026Ssobomax * computes a route to our destination that is not the one
855103026Ssobomax * which would be taken by ip_output(), as this one will loop back to
856103026Ssobomax * us. If the interface is p2p as  a--->b, then a routing entry exists
857103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here
858123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to
859103026Ssobomax * if_gre.
860103026Ssobomax * Goal here is to compute a route to b that is less specific than
861103026Ssobomax * a-->b. We know that this one exists as in normal operation we have
862103026Ssobomax * at least a default route which matches.
863103026Ssobomax */
864103032Ssobomaxstatic int
865103026Ssobomaxgre_compute_route(struct gre_softc *sc)
866103026Ssobomax{
867103026Ssobomax	struct route *ro;
868103026Ssobomax
869103026Ssobomax	ro = &sc->route;
870103026Ssobomax
871103026Ssobomax	memset(ro, 0, sizeof(struct route));
872103026Ssobomax	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
873103026Ssobomax	ro->ro_dst.sa_family = AF_INET;
874103026Ssobomax	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
875103026Ssobomax
876103026Ssobomax	/*
877103026Ssobomax	 * toggle last bit, so our interface is not found, but a less
878103026Ssobomax	 * specific route. I'd rather like to specify a shorter mask,
879103026Ssobomax	 * but this is not possible. Should work though. XXX
880178888Sjulian	 * XXX MRT Use a different FIB for the tunnel to solve this problem.
881103026Ssobomax	 */
882147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
883177416Sjulian		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
884177416Sjulian		    htonl(0x01);
885103026Ssobomax	}
886103026Ssobomax
887103026Ssobomax#ifdef DIAGNOSTIC
888147256Sbrooks	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
889103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
890103026Ssobomax#endif
891103026Ssobomax
892178888Sjulian	rtalloc_fib(ro, sc->gre_fibnum);
893103026Ssobomax
894103026Ssobomax	/*
895103026Ssobomax	 * check if this returned a route at all and this route is no
896103026Ssobomax	 * recursion to ourself
897103026Ssobomax	 */
898103026Ssobomax	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
899103026Ssobomax#ifdef DIAGNOSTIC
900103026Ssobomax		if (ro->ro_rt == NULL)
901103026Ssobomax			printf(" - no route found!\n");
902103026Ssobomax		else
903103026Ssobomax			printf(" - route loops back to ourself!\n");
904103026Ssobomax#endif
905103026Ssobomax		return EADDRNOTAVAIL;
906103026Ssobomax	}
907103026Ssobomax
908103026Ssobomax	/*
909103026Ssobomax	 * now change it back - else ip_output will just drop
910103026Ssobomax	 * the route and search one to this interface ...
911103026Ssobomax	 */
912147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
913103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
914103026Ssobomax
915103026Ssobomax#ifdef DIAGNOSTIC
916103026Ssobomax	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
917103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
918103026Ssobomax	printf("\n");
919103026Ssobomax#endif
920103026Ssobomax
921103026Ssobomax	return 0;
922103026Ssobomax}
923103026Ssobomax
924103026Ssobomax/*
925103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on
926103026Ssobomax * mbufs.
927103026Ssobomax */
928123992Ssobomaxu_int16_t
929123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len)
930103026Ssobomax{
931123992Ssobomax	u_int32_t sum = 0;
932103026Ssobomax	int nwords = len >> 1;
933103026Ssobomax
934103026Ssobomax	while (nwords-- != 0)
935103026Ssobomax		sum += *p++;
936103026Ssobomax
937103026Ssobomax	if (len & 1) {
938103026Ssobomax		union {
939103026Ssobomax			u_short w;
940103026Ssobomax			u_char c[2];
941103026Ssobomax		} u;
942103026Ssobomax		u.c[0] = *(u_char *)p;
943103026Ssobomax		u.c[1] = 0;
944103026Ssobomax		sum += u.w;
945103026Ssobomax	}
946103026Ssobomax
947103026Ssobomax	/* end-around-carry */
948103026Ssobomax	sum = (sum >> 16) + (sum & 0xffff);
949103026Ssobomax	sum += (sum >> 16);
950103026Ssobomax	return (~sum);
951103026Ssobomax}
952103026Ssobomax
953103026Ssobomaxstatic int
954103026Ssobomaxgremodevent(module_t mod, int type, void *data)
955103026Ssobomax{
956103026Ssobomax
957103026Ssobomax	switch (type) {
958103026Ssobomax	case MOD_LOAD:
959103026Ssobomax		greattach();
960103026Ssobomax		break;
961103026Ssobomax	case MOD_UNLOAD:
962241610Sglebius		if_clone_detach(gre_cloner);
963127307Srwatson		mtx_destroy(&gre_mtx);
964103026Ssobomax		break;
965132199Sphk	default:
966132199Sphk		return EOPNOTSUPP;
967103026Ssobomax	}
968103026Ssobomax	return 0;
969103026Ssobomax}
970103026Ssobomax
971103026Ssobomaxstatic moduledata_t gre_mod = {
972103026Ssobomax	"if_gre",
973103026Ssobomax	gremodevent,
974241394Skevlo	0
975103026Ssobomax};
976103026Ssobomax
977103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
978103026SsobomaxMODULE_VERSION(if_gre, 1);
979