if_gre.c revision 250523
1123992Ssobomax/*	$NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2103026Ssobomax/*	 $FreeBSD: head/sys/net/if_gre.c 250523 2013-05-11 19:05:38Z hrs $ */
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;
537125020Ssobomax	case SIOCSIFDSTADDR:
538103026Ssobomax		break;
539103026Ssobomax	case SIOCSIFFLAGS:
540164033Srwatson		/*
541171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
542171056Srwatson		 * layer check?
543164033Srwatson		 */
544164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
545103026Ssobomax			break;
546103026Ssobomax		if ((ifr->ifr_flags & IFF_LINK0) != 0)
547103026Ssobomax			sc->g_proto = IPPROTO_GRE;
548103026Ssobomax		else
549103026Ssobomax			sc->g_proto = IPPROTO_MOBILE;
550125024Ssobomax		if ((ifr->ifr_flags & IFF_LINK2) != 0)
551125024Ssobomax			sc->wccp_ver = WCCP_V2;
552125024Ssobomax		else
553125024Ssobomax			sc->wccp_ver = WCCP_V1;
554103026Ssobomax		goto recompute;
555103026Ssobomax	case SIOCSIFMTU:
556164033Srwatson		/*
557171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
558171056Srwatson		 * layer check?
559164033Srwatson		 */
560164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
561103026Ssobomax			break;
562103026Ssobomax		if (ifr->ifr_mtu < 576) {
563103026Ssobomax			error = EINVAL;
564103026Ssobomax			break;
565103026Ssobomax		}
566103026Ssobomax		ifp->if_mtu = ifr->ifr_mtu;
567103026Ssobomax		break;
568103026Ssobomax	case SIOCGIFMTU:
569147256Sbrooks		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
570103026Ssobomax		break;
571103026Ssobomax	case SIOCADDMULTI:
572164033Srwatson		/*
573171056Srwatson		 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
574171056Srwatson		 * layer check?
575164033Srwatson		 */
576164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
577164033Srwatson			break;
578164033Srwatson		if (ifr == 0) {
579164033Srwatson			error = EAFNOSUPPORT;
580164033Srwatson			break;
581164033Srwatson		}
582164033Srwatson		switch (ifr->ifr_addr.sa_family) {
583164033Srwatson#ifdef INET
584164033Srwatson		case AF_INET:
585164033Srwatson			break;
586164033Srwatson#endif
587164033Srwatson#ifdef INET6
588164033Srwatson		case AF_INET6:
589164033Srwatson			break;
590164033Srwatson#endif
591164033Srwatson		default:
592164033Srwatson			error = EAFNOSUPPORT;
593164033Srwatson			break;
594164033Srwatson		}
595164033Srwatson		break;
596103026Ssobomax	case SIOCDELMULTI:
597164033Srwatson		/*
598171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
599171056Srwatson		 * layer check?
600164033Srwatson		 */
601164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
602103026Ssobomax			break;
603103026Ssobomax		if (ifr == 0) {
604103026Ssobomax			error = EAFNOSUPPORT;
605103026Ssobomax			break;
606103026Ssobomax		}
607103026Ssobomax		switch (ifr->ifr_addr.sa_family) {
608103026Ssobomax#ifdef INET
609103026Ssobomax		case AF_INET:
610103026Ssobomax			break;
611103026Ssobomax#endif
612148613Sbz#ifdef INET6
613148613Sbz		case AF_INET6:
614148613Sbz			break;
615148613Sbz#endif
616103026Ssobomax		default:
617103026Ssobomax			error = EAFNOSUPPORT;
618103026Ssobomax			break;
619103026Ssobomax		}
620103026Ssobomax		break;
621103026Ssobomax	case GRESPROTO:
622164033Srwatson		/*
623171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
624171056Srwatson		 * layer check?
625164033Srwatson		 */
626164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
627103026Ssobomax			break;
628103026Ssobomax		sc->g_proto = ifr->ifr_flags;
629103026Ssobomax		switch (sc->g_proto) {
630103026Ssobomax		case IPPROTO_GRE:
631103026Ssobomax			ifp->if_flags |= IFF_LINK0;
632103026Ssobomax			break;
633103026Ssobomax		case IPPROTO_MOBILE:
634103026Ssobomax			ifp->if_flags &= ~IFF_LINK0;
635103026Ssobomax			break;
636103026Ssobomax		default:
637103026Ssobomax			error = EPROTONOSUPPORT;
638103026Ssobomax			break;
639103026Ssobomax		}
640103026Ssobomax		goto recompute;
641103026Ssobomax	case GREGPROTO:
642103026Ssobomax		ifr->ifr_flags = sc->g_proto;
643103026Ssobomax		break;
644103026Ssobomax	case GRESADDRS:
645103026Ssobomax	case GRESADDRD:
646164033Srwatson		error = priv_check(curthread, PRIV_NET_GRE);
647164033Srwatson		if (error)
648164033Srwatson			return (error);
649103026Ssobomax		/*
650103026Ssobomax		 * set tunnel endpoints, compute a less specific route
651103026Ssobomax		 * to the remote end and mark if as up
652103026Ssobomax		 */
653103026Ssobomax		sa = &ifr->ifr_addr;
654103026Ssobomax		if (cmd == GRESADDRS)
655103026Ssobomax			sc->g_src = (satosin(sa))->sin_addr;
656103026Ssobomax		if (cmd == GRESADDRD)
657103026Ssobomax			sc->g_dst = (satosin(sa))->sin_addr;
658103026Ssobomax	recompute:
659103026Ssobomax#ifdef INET
660103026Ssobomax		if (sc->encap != NULL) {
661103026Ssobomax			encap_detach(sc->encap);
662103026Ssobomax			sc->encap = NULL;
663103026Ssobomax		}
664103026Ssobomax#endif
665103026Ssobomax		if ((sc->g_src.s_addr != INADDR_ANY) &&
666103026Ssobomax		    (sc->g_dst.s_addr != INADDR_ANY)) {
667103026Ssobomax			bzero(&sp, sizeof(sp));
668103026Ssobomax			bzero(&sm, sizeof(sm));
669103026Ssobomax			bzero(&dp, sizeof(dp));
670103026Ssobomax			bzero(&dm, sizeof(dm));
671103026Ssobomax			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
672103026Ssobomax			    sizeof(struct sockaddr_in);
673103026Ssobomax			sp.sin_family = sm.sin_family = dp.sin_family =
674103026Ssobomax			    dm.sin_family = AF_INET;
675103026Ssobomax			sp.sin_addr = sc->g_src;
676103026Ssobomax			dp.sin_addr = sc->g_dst;
677125020Ssobomax			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
678103026Ssobomax			    INADDR_BROADCAST;
679103026Ssobomax#ifdef INET
680103026Ssobomax			sc->encap = encap_attach(AF_INET, sc->g_proto,
681103026Ssobomax			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
682103026Ssobomax			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
683103026Ssobomax				&in_gre_protosw : &in_mobile_protosw, sc);
684103026Ssobomax			if (sc->encap == NULL)
685103026Ssobomax				printf("%s: unable to attach encap\n",
686147256Sbrooks				    if_name(GRE2IFP(sc)));
687103026Ssobomax#endif
688103026Ssobomax			if (sc->route.ro_rt != 0) /* free old route */
689103026Ssobomax				RTFREE(sc->route.ro_rt);
690103026Ssobomax			if (gre_compute_route(sc) == 0)
691148887Srwatson				ifp->if_drv_flags |= IFF_DRV_RUNNING;
692103026Ssobomax			else
693148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
694103026Ssobomax		}
695103026Ssobomax		break;
696103026Ssobomax	case GREGADDRS:
697103026Ssobomax		memset(&si, 0, sizeof(si));
698103026Ssobomax		si.sin_family = AF_INET;
699103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
700103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
701103026Ssobomax		sa = sintosa(&si);
702219206Sbz		error = prison_if(curthread->td_ucred, sa);
703219206Sbz		if (error != 0)
704219206Sbz			break;
705103026Ssobomax		ifr->ifr_addr = *sa;
706103026Ssobomax		break;
707103026Ssobomax	case GREGADDRD:
708103026Ssobomax		memset(&si, 0, sizeof(si));
709103026Ssobomax		si.sin_family = AF_INET;
710103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
711103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
712103026Ssobomax		sa = sintosa(&si);
713219206Sbz		error = prison_if(curthread->td_ucred, sa);
714219206Sbz		if (error != 0)
715219206Sbz			break;
716103026Ssobomax		ifr->ifr_addr = *sa;
717103026Ssobomax		break;
718103026Ssobomax	case SIOCSIFPHYADDR:
719164033Srwatson		/*
720171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
721171056Srwatson		 * layer check?
722164033Srwatson		 */
723164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
724103026Ssobomax			break;
725103026Ssobomax		if (aifr->ifra_addr.sin_family != AF_INET ||
726103026Ssobomax		    aifr->ifra_dstaddr.sin_family != AF_INET) {
727103026Ssobomax			error = EAFNOSUPPORT;
728103026Ssobomax			break;
729103026Ssobomax		}
730103026Ssobomax		if (aifr->ifra_addr.sin_len != sizeof(si) ||
731103026Ssobomax		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
732103026Ssobomax			error = EINVAL;
733103026Ssobomax			break;
734103026Ssobomax		}
735103026Ssobomax		sc->g_src = aifr->ifra_addr.sin_addr;
736103026Ssobomax		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
737103026Ssobomax		goto recompute;
738103026Ssobomax	case SIOCSLIFPHYADDR:
739164033Srwatson		/*
740171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
741171056Srwatson		 * layer check?
742164033Srwatson		 */
743164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
744103026Ssobomax			break;
745103026Ssobomax		if (lifr->addr.ss_family != AF_INET ||
746103026Ssobomax		    lifr->dstaddr.ss_family != AF_INET) {
747103026Ssobomax			error = EAFNOSUPPORT;
748103026Ssobomax			break;
749103026Ssobomax		}
750103026Ssobomax		if (lifr->addr.ss_len != sizeof(si) ||
751103026Ssobomax		    lifr->dstaddr.ss_len != sizeof(si)) {
752103026Ssobomax			error = EINVAL;
753103026Ssobomax			break;
754103026Ssobomax		}
755155440Sqingli		sc->g_src = (satosin(&lifr->addr))->sin_addr;
756103026Ssobomax		sc->g_dst =
757155440Sqingli		    (satosin(&lifr->dstaddr))->sin_addr;
758103026Ssobomax		goto recompute;
759103026Ssobomax	case SIOCDIFPHYADDR:
760164033Srwatson		/*
761171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
762171056Srwatson		 * layer check?
763164033Srwatson		 */
764164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
765103026Ssobomax			break;
766103026Ssobomax		sc->g_src.s_addr = INADDR_ANY;
767103026Ssobomax		sc->g_dst.s_addr = INADDR_ANY;
768103026Ssobomax		goto recompute;
769103026Ssobomax	case SIOCGLIFPHYADDR:
770103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY ||
771103026Ssobomax		    sc->g_dst.s_addr == INADDR_ANY) {
772103026Ssobomax			error = EADDRNOTAVAIL;
773103026Ssobomax			break;
774103026Ssobomax		}
775103026Ssobomax		memset(&si, 0, sizeof(si));
776103026Ssobomax		si.sin_family = AF_INET;
777103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
778103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
779219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
780219206Sbz		if (error != 0)
781219206Sbz			break;
782103026Ssobomax		memcpy(&lifr->addr, &si, sizeof(si));
783103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
784219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
785219206Sbz		if (error != 0)
786219206Sbz			break;
787103026Ssobomax		memcpy(&lifr->dstaddr, &si, sizeof(si));
788103026Ssobomax		break;
789103026Ssobomax	case SIOCGIFPSRCADDR:
790122699Sbms#ifdef INET6
791122699Sbms	case SIOCGIFPSRCADDR_IN6:
792122699Sbms#endif
793103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY) {
794103026Ssobomax			error = EADDRNOTAVAIL;
795103026Ssobomax			break;
796103026Ssobomax		}
797103026Ssobomax		memset(&si, 0, sizeof(si));
798103026Ssobomax		si.sin_family = AF_INET;
799103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
800103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
801219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
802219206Sbz		if (error != 0)
803219206Sbz			break;
804103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
805103026Ssobomax		break;
806103026Ssobomax	case SIOCGIFPDSTADDR:
807122699Sbms#ifdef INET6
808122699Sbms	case SIOCGIFPDSTADDR_IN6:
809122699Sbms#endif
810103026Ssobomax		if (sc->g_dst.s_addr == INADDR_ANY) {
811103026Ssobomax			error = EADDRNOTAVAIL;
812103026Ssobomax			break;
813103026Ssobomax		}
814103026Ssobomax		memset(&si, 0, sizeof(si));
815103026Ssobomax		si.sin_family = AF_INET;
816103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
817103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
818219206Sbz		error = prison_if(curthread->td_ucred, (struct sockaddr *)&si);
819219206Sbz		if (error != 0)
820219206Sbz			break;
821103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
822103026Ssobomax		break;
823179894Sthompsa	case GRESKEY:
824179894Sthompsa		error = priv_check(curthread, PRIV_NET_GRE);
825179894Sthompsa		if (error)
826179894Sthompsa			break;
827179894Sthompsa		error = copyin(ifr->ifr_data, &key, sizeof(key));
828179894Sthompsa		if (error)
829179894Sthompsa			break;
830179894Sthompsa		/* adjust MTU for option header */
831179894Sthompsa		if (key == 0 && sc->key != 0)		/* clear */
832179894Sthompsa			adj += sizeof(key);
833179894Sthompsa		else if (key != 0 && sc->key == 0)	/* set */
834179894Sthompsa			adj -= sizeof(key);
835179894Sthompsa
836179894Sthompsa		if (ifp->if_mtu + adj < 576) {
837179894Sthompsa			error = EINVAL;
838179894Sthompsa			break;
839179894Sthompsa		}
840179894Sthompsa		ifp->if_mtu += adj;
841179894Sthompsa		sc->key = key;
842179894Sthompsa		break;
843179894Sthompsa	case GREGKEY:
844179894Sthompsa		error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
845179894Sthompsa		break;
846179894Sthompsa
847103026Ssobomax	default:
848103026Ssobomax		error = EINVAL;
849103026Ssobomax		break;
850103026Ssobomax	}
851103026Ssobomax
852103026Ssobomax	return (error);
853103026Ssobomax}
854103026Ssobomax
855103026Ssobomax/*
856103026Ssobomax * computes a route to our destination that is not the one
857103026Ssobomax * which would be taken by ip_output(), as this one will loop back to
858103026Ssobomax * us. If the interface is p2p as  a--->b, then a routing entry exists
859103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here
860123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to
861103026Ssobomax * if_gre.
862103026Ssobomax * Goal here is to compute a route to b that is less specific than
863103026Ssobomax * a-->b. We know that this one exists as in normal operation we have
864103026Ssobomax * at least a default route which matches.
865103026Ssobomax */
866103032Ssobomaxstatic int
867103026Ssobomaxgre_compute_route(struct gre_softc *sc)
868103026Ssobomax{
869103026Ssobomax	struct route *ro;
870103026Ssobomax
871103026Ssobomax	ro = &sc->route;
872103026Ssobomax
873103026Ssobomax	memset(ro, 0, sizeof(struct route));
874103026Ssobomax	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
875103026Ssobomax	ro->ro_dst.sa_family = AF_INET;
876103026Ssobomax	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
877103026Ssobomax
878103026Ssobomax	/*
879103026Ssobomax	 * toggle last bit, so our interface is not found, but a less
880103026Ssobomax	 * specific route. I'd rather like to specify a shorter mask,
881103026Ssobomax	 * but this is not possible. Should work though. XXX
882178888Sjulian	 * XXX MRT Use a different FIB for the tunnel to solve this problem.
883103026Ssobomax	 */
884147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
885177416Sjulian		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
886177416Sjulian		    htonl(0x01);
887103026Ssobomax	}
888103026Ssobomax
889103026Ssobomax#ifdef DIAGNOSTIC
890147256Sbrooks	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
891103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
892103026Ssobomax#endif
893103026Ssobomax
894178888Sjulian	rtalloc_fib(ro, sc->gre_fibnum);
895103026Ssobomax
896103026Ssobomax	/*
897103026Ssobomax	 * check if this returned a route at all and this route is no
898103026Ssobomax	 * recursion to ourself
899103026Ssobomax	 */
900103026Ssobomax	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
901103026Ssobomax#ifdef DIAGNOSTIC
902103026Ssobomax		if (ro->ro_rt == NULL)
903103026Ssobomax			printf(" - no route found!\n");
904103026Ssobomax		else
905103026Ssobomax			printf(" - route loops back to ourself!\n");
906103026Ssobomax#endif
907103026Ssobomax		return EADDRNOTAVAIL;
908103026Ssobomax	}
909103026Ssobomax
910103026Ssobomax	/*
911103026Ssobomax	 * now change it back - else ip_output will just drop
912103026Ssobomax	 * the route and search one to this interface ...
913103026Ssobomax	 */
914147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
915103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
916103026Ssobomax
917103026Ssobomax#ifdef DIAGNOSTIC
918103026Ssobomax	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
919103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
920103026Ssobomax	printf("\n");
921103026Ssobomax#endif
922103026Ssobomax
923103026Ssobomax	return 0;
924103026Ssobomax}
925103026Ssobomax
926103026Ssobomax/*
927103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on
928103026Ssobomax * mbufs.
929103026Ssobomax */
930123992Ssobomaxu_int16_t
931123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len)
932103026Ssobomax{
933123992Ssobomax	u_int32_t sum = 0;
934103026Ssobomax	int nwords = len >> 1;
935103026Ssobomax
936103026Ssobomax	while (nwords-- != 0)
937103026Ssobomax		sum += *p++;
938103026Ssobomax
939103026Ssobomax	if (len & 1) {
940103026Ssobomax		union {
941103026Ssobomax			u_short w;
942103026Ssobomax			u_char c[2];
943103026Ssobomax		} u;
944103026Ssobomax		u.c[0] = *(u_char *)p;
945103026Ssobomax		u.c[1] = 0;
946103026Ssobomax		sum += u.w;
947103026Ssobomax	}
948103026Ssobomax
949103026Ssobomax	/* end-around-carry */
950103026Ssobomax	sum = (sum >> 16) + (sum & 0xffff);
951103026Ssobomax	sum += (sum >> 16);
952103026Ssobomax	return (~sum);
953103026Ssobomax}
954103026Ssobomax
955103026Ssobomaxstatic int
956103026Ssobomaxgremodevent(module_t mod, int type, void *data)
957103026Ssobomax{
958103026Ssobomax
959103026Ssobomax	switch (type) {
960103026Ssobomax	case MOD_LOAD:
961103026Ssobomax		greattach();
962103026Ssobomax		break;
963103026Ssobomax	case MOD_UNLOAD:
964241610Sglebius		if_clone_detach(gre_cloner);
965127307Srwatson		mtx_destroy(&gre_mtx);
966103026Ssobomax		break;
967132199Sphk	default:
968132199Sphk		return EOPNOTSUPP;
969103026Ssobomax	}
970103026Ssobomax	return 0;
971103026Ssobomax}
972103026Ssobomax
973103026Ssobomaxstatic moduledata_t gre_mod = {
974103026Ssobomax	"if_gre",
975103026Ssobomax	gremodevent,
976241394Skevlo	0
977103026Ssobomax};
978103026Ssobomax
979103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
980103026SsobomaxMODULE_VERSION(if_gre, 1);
981