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
93103026Ssobomax#define GRENAME	"gre"
94103026Ssobomax
95223223Sbz#define	MTAG_COOKIE_GRE		1307983903
96223223Sbz#define	MTAG_GRE_NESTING	1
97223223Sbzstruct mtag_gre_nesting {
98223223Sbz	uint16_t	count;
99223223Sbz	uint16_t	max;
100223223Sbz	struct ifnet	*ifp[];
101223223Sbz};
102223223Sbz
103127307Srwatson/*
104127307Srwatson * gre_mtx protects all global variables in if_gre.c.
105127307Srwatson * XXX: gre_softc data not protected yet.
106127307Srwatson */
107127307Srwatsonstruct mtx gre_mtx;
108103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
109103026Ssobomax
110103026Ssobomaxstruct gre_softc_head gre_softc_list;
111103026Ssobomax
112160195Ssamstatic int	gre_clone_create(struct if_clone *, int, caddr_t);
113105300Salfredstatic void	gre_clone_destroy(struct ifnet *);
114103032Ssobomaxstatic int	gre_ioctl(struct ifnet *, u_long, caddr_t);
115103032Ssobomaxstatic int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
116191148Skmacy		    struct route *ro);
117103026Ssobomax
118130933SbrooksIFC_SIMPLE_DECLARE(gre, 0);
119103026Ssobomax
120103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc);
121103026Ssobomax
122105300Salfredstatic void	greattach(void);
123103026Ssobomax
124103026Ssobomax#ifdef INET
125103026Ssobomaxextern struct domain inetdomain;
126152242Srustatic const struct protosw in_gre_protosw = {
127152242Sru	.pr_type =		SOCK_RAW,
128152242Sru	.pr_domain =		&inetdomain,
129152242Sru	.pr_protocol =		IPPROTO_GRE,
130152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
131154625Sbz	.pr_input =		gre_input,
132152242Sru	.pr_output =		(pr_output_t *)rip_output,
133152242Sru	.pr_ctlinput =		rip_ctlinput,
134152242Sru	.pr_ctloutput =		rip_ctloutput,
135152242Sru	.pr_usrreqs =		&rip_usrreqs
136103026Ssobomax};
137152242Srustatic const struct protosw in_mobile_protosw = {
138152242Sru	.pr_type =		SOCK_RAW,
139152242Sru	.pr_domain =		&inetdomain,
140152242Sru	.pr_protocol =		IPPROTO_MOBILE,
141152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
142154625Sbz	.pr_input =		gre_mobile_input,
143152242Sru	.pr_output =		(pr_output_t *)rip_output,
144152242Sru	.pr_ctlinput =		rip_ctlinput,
145152242Sru	.pr_ctloutput =		rip_ctloutput,
146152242Sru	.pr_usrreqs =		&rip_usrreqs
147103026Ssobomax};
148103026Ssobomax#endif
149103026Ssobomax
150103026SsobomaxSYSCTL_DECL(_net_link);
151248085Smariusstatic SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
152103026Ssobomax    "Generic Routing Encapsulation");
153103026Ssobomax#ifndef MAX_GRE_NEST
154103026Ssobomax/*
155103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels.
156103026Ssobomax * Since, setting a large value to this macro with a careless configuration
157103026Ssobomax * may introduce system crash, we don't allow any nestings by default.
158103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro
159103026Ssobomax * in your kernel configuration file.  However, if you do so, please be
160103026Ssobomax * careful to configure the tunnels so that it won't make a loop.
161103026Ssobomax */
162103026Ssobomax#define MAX_GRE_NEST 1
163103026Ssobomax#endif
164103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST;
165103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
166103026Ssobomax    &max_gre_nesting, 0, "Max nested tunnels");
167103026Ssobomax
168103026Ssobomax/* ARGSUSED */
169103032Ssobomaxstatic void
170103026Ssobomaxgreattach(void)
171103026Ssobomax{
172103026Ssobomax
173127307Srwatson	mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
174103026Ssobomax	LIST_INIT(&gre_softc_list);
175103026Ssobomax	if_clone_attach(&gre_cloner);
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;
195147256Sbrooks	if_initname(GRE2IFP(sc), ifc->ifc_name, 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
244103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, 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. */
336147611Sdwmalone	if (dst->sa_family == AF_UNSPEC) {
337147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
338147611Sdwmalone		dst->sa_family = af;
339147611Sdwmalone	}
340147611Sdwmalone
341159180Scsjp	if (bpf_peers_present(ifp->if_bpf)) {
342147611Sdwmalone		af = dst->sa_family;
343123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
344103026Ssobomax	}
345103026Ssobomax
346252023Shrs	if ((ifp->if_flags & IFF_MONITOR) != 0) {
347252023Shrs		m_freem(m);
348252023Shrs		error = ENETDOWN;
349252023Shrs		goto end;
350252023Shrs	}
351252023Shrs
352103026Ssobomax	m->m_flags &= ~(M_BCAST|M_MCAST);
353103026Ssobomax
354103026Ssobomax	if (sc->g_proto == IPPROTO_MOBILE) {
355103026Ssobomax		if (dst->sa_family == AF_INET) {
356103026Ssobomax			struct mbuf *m0;
357103026Ssobomax			int msiz;
358103026Ssobomax
359103026Ssobomax			ip = mtod(m, struct ip *);
360103026Ssobomax
361103026Ssobomax			/*
362103026Ssobomax			 * RFC2004 specifies that fragmented diagrams shouldn't
363103026Ssobomax			 * be encapsulated.
364103026Ssobomax			 */
365158416Shsu			if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
366103026Ssobomax				_IF_DROP(&ifp->if_snd);
367103026Ssobomax				m_freem(m);
368103026Ssobomax				error = EINVAL;    /* is there better errno? */
369103026Ssobomax				goto end;
370103026Ssobomax			}
371103026Ssobomax			memset(&mob_h, 0, MOB_H_SIZ_L);
372103026Ssobomax			mob_h.proto = (ip->ip_p) << 8;
373103026Ssobomax			mob_h.odst = ip->ip_dst.s_addr;
374103026Ssobomax			ip->ip_dst.s_addr = sc->g_dst.s_addr;
375103026Ssobomax
376103026Ssobomax			/*
377103026Ssobomax			 * If the packet comes from our host, we only change
378103026Ssobomax			 * the destination address in the IP header.
379103026Ssobomax			 * Else we also need to save and change the source
380103026Ssobomax			 */
381103026Ssobomax			if (in_hosteq(ip->ip_src, sc->g_src)) {
382103026Ssobomax				msiz = MOB_H_SIZ_S;
383103026Ssobomax			} else {
384103026Ssobomax				mob_h.proto |= MOB_H_SBIT;
385103026Ssobomax				mob_h.osrc = ip->ip_src.s_addr;
386103026Ssobomax				ip->ip_src.s_addr = sc->g_src.s_addr;
387103026Ssobomax				msiz = MOB_H_SIZ_L;
388103026Ssobomax			}
389103026Ssobomax			mob_h.proto = htons(mob_h.proto);
390123992Ssobomax			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
391103026Ssobomax
392103026Ssobomax			if ((m->m_data - msiz) < m->m_pktdat) {
393103026Ssobomax				/* need new mbuf */
394151967Sandre				MGETHDR(m0, M_DONTWAIT, MT_DATA);
395103026Ssobomax				if (m0 == NULL) {
396103026Ssobomax					_IF_DROP(&ifp->if_snd);
397103026Ssobomax					m_freem(m);
398103026Ssobomax					error = ENOBUFS;
399103026Ssobomax					goto end;
400103026Ssobomax				}
401103026Ssobomax				m0->m_next = m;
402103026Ssobomax				m->m_data += sizeof(struct ip);
403103026Ssobomax				m->m_len -= sizeof(struct ip);
404103026Ssobomax				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
405103026Ssobomax				m0->m_len = msiz + sizeof(struct ip);
406103026Ssobomax				m0->m_data += max_linkhdr;
407103026Ssobomax				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
408103026Ssobomax				       sizeof(struct ip));
409103026Ssobomax				m = m0;
410103026Ssobomax			} else {  /* we have some space left in the old one */
411103026Ssobomax				m->m_data -= msiz;
412103026Ssobomax				m->m_len += msiz;
413103026Ssobomax				m->m_pkthdr.len += msiz;
414103026Ssobomax				bcopy(ip, mtod(m, caddr_t),
415103026Ssobomax					sizeof(struct ip));
416103026Ssobomax			}
417103026Ssobomax			ip = mtod(m, struct ip *);
418103026Ssobomax			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
419103026Ssobomax			ip->ip_len = ntohs(ip->ip_len) + msiz;
420103026Ssobomax		} else {  /* AF_INET */
421103026Ssobomax			_IF_DROP(&ifp->if_snd);
422103026Ssobomax			m_freem(m);
423103026Ssobomax			error = EINVAL;
424103026Ssobomax			goto end;
425103026Ssobomax		}
426103026Ssobomax	} else if (sc->g_proto == IPPROTO_GRE) {
427103026Ssobomax		switch (dst->sa_family) {
428103026Ssobomax		case AF_INET:
429103026Ssobomax			ip = mtod(m, struct ip *);
430180041Sjulian			gre_ip_tos = ip->ip_tos;
431180041Sjulian			gre_ip_id = ip->ip_id;
432180639Sjulian			if (sc->wccp_ver == WCCP_V2) {
433180639Sjulian				extra = sizeof(uint32_t);
434180639Sjulian				etype =  WCCP_PROTOCOL_TYPE;
435180639Sjulian			} else {
436180639Sjulian				etype = ETHERTYPE_IP;
437180639Sjulian			}
438103026Ssobomax			break;
439148613Sbz#ifdef INET6
440148613Sbz		case AF_INET6:
441180041Sjulian			gre_ip_id = ip_newid();
442148613Sbz			etype = ETHERTYPE_IPV6;
443148613Sbz			break;
444148613Sbz#endif
445103026Ssobomax#ifdef NETATALK
446103026Ssobomax		case AF_APPLETALK:
447103026Ssobomax			etype = ETHERTYPE_ATALK;
448103026Ssobomax			break;
449103026Ssobomax#endif
450103026Ssobomax		default:
451103026Ssobomax			_IF_DROP(&ifp->if_snd);
452103026Ssobomax			m_freem(m);
453103026Ssobomax			error = EAFNOSUPPORT;
454103026Ssobomax			goto end;
455103026Ssobomax		}
456179894Sthompsa
457179894Sthompsa		/* Reserve space for GRE header + optional GRE key */
458180639Sjulian		int hdrlen = sizeof(struct greip) + extra;
459179894Sthompsa		if (sc->key)
460179894Sthompsa			hdrlen += sizeof(uint32_t);
461179894Sthompsa		M_PREPEND(m, hdrlen, M_DONTWAIT);
462103026Ssobomax	} else {
463103026Ssobomax		_IF_DROP(&ifp->if_snd);
464103026Ssobomax		m_freem(m);
465103026Ssobomax		error = EINVAL;
466103026Ssobomax		goto end;
467103026Ssobomax	}
468103026Ssobomax
469128580Sandre	if (m == NULL) {	/* mbuf allocation failed */
470103026Ssobomax		_IF_DROP(&ifp->if_snd);
471103026Ssobomax		error = ENOBUFS;
472103026Ssobomax		goto end;
473103026Ssobomax	}
474103026Ssobomax
475178888Sjulian	M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
476178888Sjulian
477103026Ssobomax	gh = mtod(m, struct greip *);
478103026Ssobomax	if (sc->g_proto == IPPROTO_GRE) {
479179894Sthompsa		uint32_t *options = gh->gi_options;
480179894Sthompsa
481180639Sjulian		memset((void *)gh, 0, sizeof(struct greip) + extra);
482103026Ssobomax		gh->gi_ptype = htons(etype);
483179894Sthompsa		gh->gi_flags = 0;
484179894Sthompsa
485179894Sthompsa		/* Add key option */
486179894Sthompsa		if (sc->key)
487179894Sthompsa		{
488179894Sthompsa			gh->gi_flags |= htons(GRE_KP);
489179894Sthompsa			*(options++) = htonl(sc->key);
490179894Sthompsa		}
491103026Ssobomax	}
492103026Ssobomax
493103026Ssobomax	gh->gi_pr = sc->g_proto;
494103026Ssobomax	if (sc->g_proto != IPPROTO_MOBILE) {
495103026Ssobomax		gh->gi_src = sc->g_src;
496103026Ssobomax		gh->gi_dst = sc->g_dst;
497133163Ssobomax		((struct ip*)gh)->ip_v = IPPROTO_IPV4;
498103026Ssobomax		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
499103032Ssobomax		((struct ip*)gh)->ip_ttl = GRE_TTL;
500180041Sjulian		((struct ip*)gh)->ip_tos = gre_ip_tos;
501180041Sjulian		((struct ip*)gh)->ip_id = gre_ip_id;
502125226Ssobomax		gh->gi_len = m->m_pkthdr.len;
503103026Ssobomax	}
504103026Ssobomax
505103026Ssobomax	ifp->if_opackets++;
506103026Ssobomax	ifp->if_obytes += m->m_pkthdr.len;
507128583Sandre	/*
508128583Sandre	 * Send it off and with IP_FORWARD flag to prevent it from
509128583Sandre	 * overwriting the ip_id again.  ip_id is already set to the
510128583Sandre	 * ip_id of the encapsulated packet.
511128583Sandre	 */
512128580Sandre	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
513123992Ssobomax	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
514103026Ssobomax  end:
515103026Ssobomax	if (error)
516103026Ssobomax		ifp->if_oerrors++;
517103026Ssobomax	return (error);
518103026Ssobomax}
519103026Ssobomax
520103032Ssobomaxstatic int
521103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
522103026Ssobomax{
523103026Ssobomax	struct ifreq *ifr = (struct ifreq *)data;
524103026Ssobomax	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
525103026Ssobomax	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
526103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
527103026Ssobomax	int s;
528103026Ssobomax	struct sockaddr_in si;
529103026Ssobomax	struct sockaddr *sa = NULL;
530179894Sthompsa	int error, adj;
531103026Ssobomax	struct sockaddr_in sp, sm, dp, dm;
532179894Sthompsa	uint32_t key;
533103026Ssobomax
534103026Ssobomax	error = 0;
535179894Sthompsa	adj = 0;
536103026Ssobomax
537103026Ssobomax	s = splnet();
538103026Ssobomax	switch (cmd) {
539103026Ssobomax	case SIOCSIFADDR:
540103026Ssobomax		ifp->if_flags |= IFF_UP;
541103026Ssobomax		break;
542125020Ssobomax	case SIOCSIFDSTADDR:
543103026Ssobomax		break;
544103026Ssobomax	case SIOCSIFFLAGS:
545164033Srwatson		/*
546171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
547171056Srwatson		 * layer check?
548164033Srwatson		 */
549164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
550103026Ssobomax			break;
551103026Ssobomax		if ((ifr->ifr_flags & IFF_LINK0) != 0)
552103026Ssobomax			sc->g_proto = IPPROTO_GRE;
553103026Ssobomax		else
554103026Ssobomax			sc->g_proto = IPPROTO_MOBILE;
555125024Ssobomax		if ((ifr->ifr_flags & IFF_LINK2) != 0)
556125024Ssobomax			sc->wccp_ver = WCCP_V2;
557125024Ssobomax		else
558125024Ssobomax			sc->wccp_ver = WCCP_V1;
559103026Ssobomax		goto recompute;
560103026Ssobomax	case SIOCSIFMTU:
561164033Srwatson		/*
562171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
563171056Srwatson		 * layer check?
564164033Srwatson		 */
565164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
566103026Ssobomax			break;
567103026Ssobomax		if (ifr->ifr_mtu < 576) {
568103026Ssobomax			error = EINVAL;
569103026Ssobomax			break;
570103026Ssobomax		}
571103026Ssobomax		ifp->if_mtu = ifr->ifr_mtu;
572103026Ssobomax		break;
573103026Ssobomax	case SIOCGIFMTU:
574147256Sbrooks		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
575103026Ssobomax		break;
576103026Ssobomax	case SIOCADDMULTI:
577164033Srwatson		/*
578171056Srwatson		 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
579171056Srwatson		 * layer check?
580164033Srwatson		 */
581164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
582164033Srwatson			break;
583164033Srwatson		if (ifr == 0) {
584164033Srwatson			error = EAFNOSUPPORT;
585164033Srwatson			break;
586164033Srwatson		}
587164033Srwatson		switch (ifr->ifr_addr.sa_family) {
588164033Srwatson#ifdef INET
589164033Srwatson		case AF_INET:
590164033Srwatson			break;
591164033Srwatson#endif
592164033Srwatson#ifdef INET6
593164033Srwatson		case AF_INET6:
594164033Srwatson			break;
595164033Srwatson#endif
596164033Srwatson		default:
597164033Srwatson			error = EAFNOSUPPORT;
598164033Srwatson			break;
599164033Srwatson		}
600164033Srwatson		break;
601103026Ssobomax	case SIOCDELMULTI:
602164033Srwatson		/*
603171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
604171056Srwatson		 * layer check?
605164033Srwatson		 */
606164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
607103026Ssobomax			break;
608103026Ssobomax		if (ifr == 0) {
609103026Ssobomax			error = EAFNOSUPPORT;
610103026Ssobomax			break;
611103026Ssobomax		}
612103026Ssobomax		switch (ifr->ifr_addr.sa_family) {
613103026Ssobomax#ifdef INET
614103026Ssobomax		case AF_INET:
615103026Ssobomax			break;
616103026Ssobomax#endif
617148613Sbz#ifdef INET6
618148613Sbz		case AF_INET6:
619148613Sbz			break;
620148613Sbz#endif
621103026Ssobomax		default:
622103026Ssobomax			error = EAFNOSUPPORT;
623103026Ssobomax			break;
624103026Ssobomax		}
625103026Ssobomax		break;
626103026Ssobomax	case GRESPROTO:
627164033Srwatson		/*
628171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
629171056Srwatson		 * layer check?
630164033Srwatson		 */
631164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
632103026Ssobomax			break;
633103026Ssobomax		sc->g_proto = ifr->ifr_flags;
634103026Ssobomax		switch (sc->g_proto) {
635103026Ssobomax		case IPPROTO_GRE:
636103026Ssobomax			ifp->if_flags |= IFF_LINK0;
637103026Ssobomax			break;
638103026Ssobomax		case IPPROTO_MOBILE:
639103026Ssobomax			ifp->if_flags &= ~IFF_LINK0;
640103026Ssobomax			break;
641103026Ssobomax		default:
642103026Ssobomax			error = EPROTONOSUPPORT;
643103026Ssobomax			break;
644103026Ssobomax		}
645103026Ssobomax		goto recompute;
646103026Ssobomax	case GREGPROTO:
647103026Ssobomax		ifr->ifr_flags = sc->g_proto;
648103026Ssobomax		break;
649103026Ssobomax	case GRESADDRS:
650103026Ssobomax	case GRESADDRD:
651164033Srwatson		error = priv_check(curthread, PRIV_NET_GRE);
652164033Srwatson		if (error)
653164033Srwatson			return (error);
654103026Ssobomax		/*
655103026Ssobomax		 * set tunnel endpoints, compute a less specific route
656103026Ssobomax		 * to the remote end and mark if as up
657103026Ssobomax		 */
658103026Ssobomax		sa = &ifr->ifr_addr;
659103026Ssobomax		if (cmd == GRESADDRS)
660103026Ssobomax			sc->g_src = (satosin(sa))->sin_addr;
661103026Ssobomax		if (cmd == GRESADDRD)
662103026Ssobomax			sc->g_dst = (satosin(sa))->sin_addr;
663103026Ssobomax	recompute:
664103026Ssobomax#ifdef INET
665103026Ssobomax		if (sc->encap != NULL) {
666103026Ssobomax			encap_detach(sc->encap);
667103026Ssobomax			sc->encap = NULL;
668103026Ssobomax		}
669103026Ssobomax#endif
670103026Ssobomax		if ((sc->g_src.s_addr != INADDR_ANY) &&
671103026Ssobomax		    (sc->g_dst.s_addr != INADDR_ANY)) {
672103026Ssobomax			bzero(&sp, sizeof(sp));
673103026Ssobomax			bzero(&sm, sizeof(sm));
674103026Ssobomax			bzero(&dp, sizeof(dp));
675103026Ssobomax			bzero(&dm, sizeof(dm));
676103026Ssobomax			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
677103026Ssobomax			    sizeof(struct sockaddr_in);
678103026Ssobomax			sp.sin_family = sm.sin_family = dp.sin_family =
679103026Ssobomax			    dm.sin_family = AF_INET;
680103026Ssobomax			sp.sin_addr = sc->g_src;
681103026Ssobomax			dp.sin_addr = sc->g_dst;
682125020Ssobomax			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
683103026Ssobomax			    INADDR_BROADCAST;
684103026Ssobomax#ifdef INET
685103026Ssobomax			sc->encap = encap_attach(AF_INET, sc->g_proto,
686103026Ssobomax			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
687103026Ssobomax			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
688103026Ssobomax				&in_gre_protosw : &in_mobile_protosw, sc);
689103026Ssobomax			if (sc->encap == NULL)
690103026Ssobomax				printf("%s: unable to attach encap\n",
691147256Sbrooks				    if_name(GRE2IFP(sc)));
692103026Ssobomax#endif
693103026Ssobomax			if (sc->route.ro_rt != 0) /* free old route */
694103026Ssobomax				RTFREE(sc->route.ro_rt);
695103026Ssobomax			if (gre_compute_route(sc) == 0)
696148887Srwatson				ifp->if_drv_flags |= IFF_DRV_RUNNING;
697103026Ssobomax			else
698148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
699103026Ssobomax		}
700103026Ssobomax		break;
701103026Ssobomax	case GREGADDRS:
702103026Ssobomax		memset(&si, 0, sizeof(si));
703103026Ssobomax		si.sin_family = AF_INET;
704103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
705103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
706103026Ssobomax		sa = sintosa(&si);
707219206Sbz		error = prison_if(curthread->td_ucred, sa);
708219206Sbz		if (error != 0)
709219206Sbz			break;
710103026Ssobomax		ifr->ifr_addr = *sa;
711103026Ssobomax		break;
712103026Ssobomax	case GREGADDRD:
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_dst.s_addr;
717103026Ssobomax		sa = sintosa(&si);
718219206Sbz		error = prison_if(curthread->td_ucred, sa);
719219206Sbz		if (error != 0)
720219206Sbz			break;
721103026Ssobomax		ifr->ifr_addr = *sa;
722103026Ssobomax		break;
723103026Ssobomax	case SIOCSIFPHYADDR:
724164033Srwatson		/*
725171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
726171056Srwatson		 * layer check?
727164033Srwatson		 */
728164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
729103026Ssobomax			break;
730103026Ssobomax		if (aifr->ifra_addr.sin_family != AF_INET ||
731103026Ssobomax		    aifr->ifra_dstaddr.sin_family != AF_INET) {
732103026Ssobomax			error = EAFNOSUPPORT;
733103026Ssobomax			break;
734103026Ssobomax		}
735103026Ssobomax		if (aifr->ifra_addr.sin_len != sizeof(si) ||
736103026Ssobomax		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
737103026Ssobomax			error = EINVAL;
738103026Ssobomax			break;
739103026Ssobomax		}
740103026Ssobomax		sc->g_src = aifr->ifra_addr.sin_addr;
741103026Ssobomax		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
742103026Ssobomax		goto recompute;
743103026Ssobomax	case SIOCSLIFPHYADDR:
744164033Srwatson		/*
745171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
746171056Srwatson		 * layer check?
747164033Srwatson		 */
748164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
749103026Ssobomax			break;
750103026Ssobomax		if (lifr->addr.ss_family != AF_INET ||
751103026Ssobomax		    lifr->dstaddr.ss_family != AF_INET) {
752103026Ssobomax			error = EAFNOSUPPORT;
753103026Ssobomax			break;
754103026Ssobomax		}
755103026Ssobomax		if (lifr->addr.ss_len != sizeof(si) ||
756103026Ssobomax		    lifr->dstaddr.ss_len != sizeof(si)) {
757103026Ssobomax			error = EINVAL;
758103026Ssobomax			break;
759103026Ssobomax		}
760155440Sqingli		sc->g_src = (satosin(&lifr->addr))->sin_addr;
761103026Ssobomax		sc->g_dst =
762155440Sqingli		    (satosin(&lifr->dstaddr))->sin_addr;
763103026Ssobomax		goto recompute;
764103026Ssobomax	case SIOCDIFPHYADDR:
765164033Srwatson		/*
766171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
767171056Srwatson		 * layer check?
768164033Srwatson		 */
769164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
770103026Ssobomax			break;
771103026Ssobomax		sc->g_src.s_addr = INADDR_ANY;
772103026Ssobomax		sc->g_dst.s_addr = INADDR_ANY;
773103026Ssobomax		goto recompute;
774103026Ssobomax	case SIOCGLIFPHYADDR:
775103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY ||
776103026Ssobomax		    sc->g_dst.s_addr == INADDR_ANY) {
777103026Ssobomax			error = EADDRNOTAVAIL;
778103026Ssobomax			break;
779103026Ssobomax		}
780103026Ssobomax		memset(&si, 0, sizeof(si));
781103026Ssobomax		si.sin_family = AF_INET;
782103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
783103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
784219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
785219206Sbz		if (error != 0)
786219206Sbz			break;
787103026Ssobomax		memcpy(&lifr->addr, &si, sizeof(si));
788103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
789219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
790219206Sbz		if (error != 0)
791219206Sbz			break;
792103026Ssobomax		memcpy(&lifr->dstaddr, &si, sizeof(si));
793103026Ssobomax		break;
794103026Ssobomax	case SIOCGIFPSRCADDR:
795122699Sbms#ifdef INET6
796122699Sbms	case SIOCGIFPSRCADDR_IN6:
797122699Sbms#endif
798103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY) {
799103026Ssobomax			error = EADDRNOTAVAIL;
800103026Ssobomax			break;
801103026Ssobomax		}
802103026Ssobomax		memset(&si, 0, sizeof(si));
803103026Ssobomax		si.sin_family = AF_INET;
804103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
805103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
806219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
807219206Sbz		if (error != 0)
808219206Sbz			break;
809103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
810103026Ssobomax		break;
811103026Ssobomax	case SIOCGIFPDSTADDR:
812122699Sbms#ifdef INET6
813122699Sbms	case SIOCGIFPDSTADDR_IN6:
814122699Sbms#endif
815103026Ssobomax		if (sc->g_dst.s_addr == INADDR_ANY) {
816103026Ssobomax			error = EADDRNOTAVAIL;
817103026Ssobomax			break;
818103026Ssobomax		}
819103026Ssobomax		memset(&si, 0, sizeof(si));
820103026Ssobomax		si.sin_family = AF_INET;
821103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
822103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
823219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
824219206Sbz		if (error != 0)
825219206Sbz			break;
826103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
827103026Ssobomax		break;
828179894Sthompsa	case GRESKEY:
829179894Sthompsa		error = priv_check(curthread, PRIV_NET_GRE);
830179894Sthompsa		if (error)
831179894Sthompsa			break;
832179894Sthompsa		error = copyin(ifr->ifr_data, &key, sizeof(key));
833179894Sthompsa		if (error)
834179894Sthompsa			break;
835179894Sthompsa		/* adjust MTU for option header */
836179894Sthompsa		if (key == 0 && sc->key != 0)		/* clear */
837179894Sthompsa			adj += sizeof(key);
838179894Sthompsa		else if (key != 0 && sc->key == 0)	/* set */
839179894Sthompsa			adj -= sizeof(key);
840179894Sthompsa
841179894Sthompsa		if (ifp->if_mtu + adj < 576) {
842179894Sthompsa			error = EINVAL;
843179894Sthompsa			break;
844179894Sthompsa		}
845179894Sthompsa		ifp->if_mtu += adj;
846179894Sthompsa		sc->key = key;
847179894Sthompsa		break;
848179894Sthompsa	case GREGKEY:
849179894Sthompsa		error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
850179894Sthompsa		break;
851179894Sthompsa
852103026Ssobomax	default:
853103026Ssobomax		error = EINVAL;
854103026Ssobomax		break;
855103026Ssobomax	}
856103026Ssobomax
857103026Ssobomax	splx(s);
858103026Ssobomax	return (error);
859103026Ssobomax}
860103026Ssobomax
861103026Ssobomax/*
862103026Ssobomax * computes a route to our destination that is not the one
863103026Ssobomax * which would be taken by ip_output(), as this one will loop back to
864103026Ssobomax * us. If the interface is p2p as  a--->b, then a routing entry exists
865103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here
866123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to
867103026Ssobomax * if_gre.
868103026Ssobomax * Goal here is to compute a route to b that is less specific than
869103026Ssobomax * a-->b. We know that this one exists as in normal operation we have
870103026Ssobomax * at least a default route which matches.
871103026Ssobomax */
872103032Ssobomaxstatic int
873103026Ssobomaxgre_compute_route(struct gre_softc *sc)
874103026Ssobomax{
875103026Ssobomax	struct route *ro;
876103026Ssobomax
877103026Ssobomax	ro = &sc->route;
878103026Ssobomax
879103026Ssobomax	memset(ro, 0, sizeof(struct route));
880103026Ssobomax	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
881103026Ssobomax	ro->ro_dst.sa_family = AF_INET;
882103026Ssobomax	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
883103026Ssobomax
884103026Ssobomax	/*
885103026Ssobomax	 * toggle last bit, so our interface is not found, but a less
886103026Ssobomax	 * specific route. I'd rather like to specify a shorter mask,
887103026Ssobomax	 * but this is not possible. Should work though. XXX
888178888Sjulian	 * XXX MRT Use a different FIB for the tunnel to solve this problem.
889103026Ssobomax	 */
890147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
891177416Sjulian		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
892177416Sjulian		    htonl(0x01);
893103026Ssobomax	}
894103026Ssobomax
895103026Ssobomax#ifdef DIAGNOSTIC
896147256Sbrooks	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
897103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
898103026Ssobomax#endif
899103026Ssobomax
900178888Sjulian	rtalloc_fib(ro, sc->gre_fibnum);
901103026Ssobomax
902103026Ssobomax	/*
903103026Ssobomax	 * check if this returned a route at all and this route is no
904103026Ssobomax	 * recursion to ourself
905103026Ssobomax	 */
906103026Ssobomax	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
907103026Ssobomax#ifdef DIAGNOSTIC
908103026Ssobomax		if (ro->ro_rt == NULL)
909103026Ssobomax			printf(" - no route found!\n");
910103026Ssobomax		else
911103026Ssobomax			printf(" - route loops back to ourself!\n");
912103026Ssobomax#endif
913103026Ssobomax		return EADDRNOTAVAIL;
914103026Ssobomax	}
915103026Ssobomax
916103026Ssobomax	/*
917103026Ssobomax	 * now change it back - else ip_output will just drop
918103026Ssobomax	 * the route and search one to this interface ...
919103026Ssobomax	 */
920147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
921103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
922103026Ssobomax
923103026Ssobomax#ifdef DIAGNOSTIC
924103026Ssobomax	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
925103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
926103026Ssobomax	printf("\n");
927103026Ssobomax#endif
928103026Ssobomax
929103026Ssobomax	return 0;
930103026Ssobomax}
931103026Ssobomax
932103026Ssobomax/*
933103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on
934103026Ssobomax * mbufs.
935103026Ssobomax */
936123992Ssobomaxu_int16_t
937123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len)
938103026Ssobomax{
939123992Ssobomax	u_int32_t sum = 0;
940103026Ssobomax	int nwords = len >> 1;
941103026Ssobomax
942103026Ssobomax	while (nwords-- != 0)
943103026Ssobomax		sum += *p++;
944103026Ssobomax
945103026Ssobomax	if (len & 1) {
946103026Ssobomax		union {
947103026Ssobomax			u_short w;
948103026Ssobomax			u_char c[2];
949103026Ssobomax		} u;
950103026Ssobomax		u.c[0] = *(u_char *)p;
951103026Ssobomax		u.c[1] = 0;
952103026Ssobomax		sum += u.w;
953103026Ssobomax	}
954103026Ssobomax
955103026Ssobomax	/* end-around-carry */
956103026Ssobomax	sum = (sum >> 16) + (sum & 0xffff);
957103026Ssobomax	sum += (sum >> 16);
958103026Ssobomax	return (~sum);
959103026Ssobomax}
960103026Ssobomax
961103026Ssobomaxstatic int
962103026Ssobomaxgremodevent(module_t mod, int type, void *data)
963103026Ssobomax{
964103026Ssobomax
965103026Ssobomax	switch (type) {
966103026Ssobomax	case MOD_LOAD:
967103026Ssobomax		greattach();
968103026Ssobomax		break;
969103026Ssobomax	case MOD_UNLOAD:
970103026Ssobomax		if_clone_detach(&gre_cloner);
971127307Srwatson		mtx_destroy(&gre_mtx);
972103026Ssobomax		break;
973132199Sphk	default:
974132199Sphk		return EOPNOTSUPP;
975103026Ssobomax	}
976103026Ssobomax	return 0;
977103026Ssobomax}
978103026Ssobomax
979103026Ssobomaxstatic moduledata_t gre_mod = {
980103026Ssobomax	"if_gre",
981103026Ssobomax	gremodevent,
982103026Ssobomax	0
983103026Ssobomax};
984103026Ssobomax
985103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
986103026SsobomaxMODULE_VERSION(if_gre, 1);
987