if_gre.c revision 148613
1123992Ssobomax/*	$NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2103026Ssobomax/*	 $FreeBSD: head/sys/net/if_gre.c 148613 2005-08-01 08:14:21Z bz $ */
3103026Ssobomax
4139823Simp/*-
5103026Ssobomax * Copyright (c) 1998 The NetBSD Foundation, Inc.
6103026Ssobomax * All rights reserved.
7103026Ssobomax *
8103026Ssobomax * This code is derived from software contributed to The NetBSD Foundation
9103026Ssobomax * by Heiko W.Rupp <hwr@pilhuhn.de>
10103026Ssobomax *
11148613Sbz * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12148613Sbz *
13103026Ssobomax * Redistribution and use in source and binary forms, with or without
14103026Ssobomax * modification, are permitted provided that the following conditions
15103026Ssobomax * are met:
16103026Ssobomax * 1. Redistributions of source code must retain the above copyright
17103026Ssobomax *    notice, this list of conditions and the following disclaimer.
18103026Ssobomax * 2. Redistributions in binary form must reproduce the above copyright
19103026Ssobomax *    notice, this list of conditions and the following disclaimer in the
20103026Ssobomax *    documentation and/or other materials provided with the distribution.
21103026Ssobomax * 3. All advertising materials mentioning features or use of this software
22103026Ssobomax *    must display the following acknowledgement:
23103026Ssobomax *        This product includes software developed by the NetBSD
24103026Ssobomax *        Foundation, Inc. and its contributors.
25103026Ssobomax * 4. Neither the name of The NetBSD Foundation nor the names of its
26103026Ssobomax *    contributors may be used to endorse or promote products derived
27103026Ssobomax *    from this software without specific prior written permission.
28103026Ssobomax *
29103026Ssobomax * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30103026Ssobomax * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31103026Ssobomax * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32103026Ssobomax * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33103026Ssobomax * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34103026Ssobomax * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35103026Ssobomax * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36103026Ssobomax * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37103026Ssobomax * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38103026Ssobomax * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39103026Ssobomax * POSSIBILITY OF SUCH DAMAGE.
40103026Ssobomax */
41103026Ssobomax
42103026Ssobomax/*
43103026Ssobomax * Encapsulate L3 protocols into IP
44148613Sbz * See RFC 2784 (successor of RFC 1701 and 1702) for more details.
45103026Ssobomax * If_gre is compatible with Cisco GRE tunnels, so you can
46103026Ssobomax * have a NetBSD box as the other end of a tunnel interface of a Cisco
47103026Ssobomax * router. See gre(4) for more details.
48103026Ssobomax * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
49103026Ssobomax */
50103026Ssobomax
51103394Sbde#include "opt_atalk.h"
52103026Ssobomax#include "opt_inet.h"
53122699Sbms#include "opt_inet6.h"
54103026Ssobomax
55103026Ssobomax#include <sys/param.h>
56103026Ssobomax#include <sys/kernel.h>
57103026Ssobomax#include <sys/malloc.h>
58129880Sphk#include <sys/module.h>
59103026Ssobomax#include <sys/mbuf.h>
60103026Ssobomax#include <sys/protosw.h>
61103026Ssobomax#include <sys/socket.h>
62103026Ssobomax#include <sys/sockio.h>
63103026Ssobomax#include <sys/sysctl.h>
64103344Sbde#include <sys/systm.h>
65103026Ssobomax
66103026Ssobomax#include <net/ethernet.h>
67103026Ssobomax#include <net/if.h>
68130933Sbrooks#include <net/if_clone.h>
69103026Ssobomax#include <net/if_types.h>
70103026Ssobomax#include <net/route.h>
71103026Ssobomax
72103026Ssobomax#ifdef INET
73103026Ssobomax#include <netinet/in.h>
74103026Ssobomax#include <netinet/in_systm.h>
75103026Ssobomax#include <netinet/in_var.h>
76103026Ssobomax#include <netinet/ip.h>
77103026Ssobomax#include <netinet/ip_gre.h>
78103026Ssobomax#include <netinet/ip_var.h>
79103026Ssobomax#include <netinet/ip_encap.h>
80103026Ssobomax#else
81103026Ssobomax#error "Huh? if_gre without inet?"
82103026Ssobomax#endif
83103026Ssobomax
84103026Ssobomax#include <net/bpf.h>
85103026Ssobomax
86103026Ssobomax#include <net/net_osdep.h>
87103026Ssobomax#include <net/if_gre.h>
88103026Ssobomax
89103026Ssobomax/*
90103026Ssobomax * It is not easy to calculate the right value for a GRE MTU.
91103026Ssobomax * We leave this task to the admin and use the same default that
92103026Ssobomax * other vendors use.
93103026Ssobomax */
94103026Ssobomax#define GREMTU	1476
95103026Ssobomax
96103026Ssobomax#define GRENAME	"gre"
97103026Ssobomax
98127307Srwatson/*
99127307Srwatson * gre_mtx protects all global variables in if_gre.c.
100127307Srwatson * XXX: gre_softc data not protected yet.
101127307Srwatson */
102127307Srwatsonstruct mtx gre_mtx;
103103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
104103026Ssobomax
105103026Ssobomaxstruct gre_softc_head gre_softc_list;
106103026Ssobomax
107105300Salfredstatic int	gre_clone_create(struct if_clone *, int);
108105300Salfredstatic void	gre_clone_destroy(struct ifnet *);
109103032Ssobomaxstatic int	gre_ioctl(struct ifnet *, u_long, caddr_t);
110103032Ssobomaxstatic int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
111103032Ssobomax		    struct rtentry *rt);
112103026Ssobomax
113130933SbrooksIFC_SIMPLE_DECLARE(gre, 0);
114103026Ssobomax
115103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc);
116103026Ssobomax
117105300Salfredstatic void	greattach(void);
118103026Ssobomax
119103026Ssobomax#ifdef INET
120103026Ssobomaxextern struct domain inetdomain;
121103026Ssobomaxstatic const struct protosw in_gre_protosw =
122103026Ssobomax{ SOCK_RAW,     &inetdomain,    IPPROTO_GRE,    PR_ATOMIC|PR_ADDR,
123103026Ssobomax  (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
124103026Ssobomax  0,
125103026Ssobomax  0,		0,		0,		0,
126103026Ssobomax  &rip_usrreqs
127103026Ssobomax};
128103026Ssobomaxstatic const struct protosw in_mobile_protosw =
129103026Ssobomax{ SOCK_RAW,     &inetdomain,    IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR,
130103026Ssobomax  (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
131103026Ssobomax  0,
132103026Ssobomax  0,		0,		0,		0,
133103026Ssobomax  &rip_usrreqs
134103026Ssobomax};
135103026Ssobomax#endif
136103026Ssobomax
137103026SsobomaxSYSCTL_DECL(_net_link);
138123338SbmsSYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
139103026Ssobomax    "Generic Routing Encapsulation");
140103026Ssobomax#ifndef MAX_GRE_NEST
141103026Ssobomax/*
142103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels.
143103026Ssobomax * Since, setting a large value to this macro with a careless configuration
144103026Ssobomax * may introduce system crash, we don't allow any nestings by default.
145103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro
146103026Ssobomax * in your kernel configuration file.  However, if you do so, please be
147103026Ssobomax * careful to configure the tunnels so that it won't make a loop.
148103026Ssobomax */
149103026Ssobomax#define MAX_GRE_NEST 1
150103026Ssobomax#endif
151103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST;
152103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
153103026Ssobomax    &max_gre_nesting, 0, "Max nested tunnels");
154103026Ssobomax
155103026Ssobomax/* ARGSUSED */
156103032Ssobomaxstatic void
157103026Ssobomaxgreattach(void)
158103026Ssobomax{
159103026Ssobomax
160127307Srwatson	mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
161103026Ssobomax	LIST_INIT(&gre_softc_list);
162103026Ssobomax	if_clone_attach(&gre_cloner);
163103026Ssobomax}
164103026Ssobomax
165103032Ssobomaxstatic int
166103026Ssobomaxgre_clone_create(ifc, unit)
167103026Ssobomax	struct if_clone *ifc;
168103026Ssobomax	int unit;
169103026Ssobomax{
170103026Ssobomax	struct gre_softc *sc;
171103026Ssobomax
172131673Sbms	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
173103026Ssobomax
174147643Sbz	GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
175147643Sbz	if (GRE2IFP(sc) == NULL) {
176147643Sbz		free(sc, M_GRE);
177147643Sbz		return (ENOSPC);
178147643Sbz	}
179147643Sbz
180147643Sbz	GRE2IFP(sc)->if_softc = sc;
181147256Sbrooks	if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
182147643Sbz
183147256Sbrooks	GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
184147256Sbrooks	GRE2IFP(sc)->if_addrlen = 0;
185147256Sbrooks	GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
186147256Sbrooks	GRE2IFP(sc)->if_mtu = GREMTU;
187147256Sbrooks	GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
188147256Sbrooks	GRE2IFP(sc)->if_output = gre_output;
189147256Sbrooks	GRE2IFP(sc)->if_ioctl = gre_ioctl;
190103026Ssobomax	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
191103026Ssobomax	sc->g_proto = IPPROTO_GRE;
192147256Sbrooks	GRE2IFP(sc)->if_flags |= IFF_LINK0;
193103026Ssobomax	sc->encap = NULL;
194103026Ssobomax	sc->called = 0;
195125024Ssobomax	sc->wccp_ver = WCCP_V1;
196147256Sbrooks	if_attach(GRE2IFP(sc));
197147256Sbrooks	bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
198127307Srwatson	mtx_lock(&gre_mtx);
199103026Ssobomax	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
200127307Srwatson	mtx_unlock(&gre_mtx);
201103026Ssobomax	return (0);
202103026Ssobomax}
203103026Ssobomax
204103032Ssobomaxstatic void
205127307Srwatsongre_destroy(struct gre_softc *sc)
206103026Ssobomax{
207103026Ssobomax
208103026Ssobomax#ifdef INET
209103026Ssobomax	if (sc->encap != NULL)
210103026Ssobomax		encap_detach(sc->encap);
211103026Ssobomax#endif
212147256Sbrooks	bpfdetach(GRE2IFP(sc));
213147256Sbrooks	if_detach(GRE2IFP(sc));
214147256Sbrooks	if_free(GRE2IFP(sc));
215103026Ssobomax	free(sc, M_GRE);
216103026Ssobomax}
217103026Ssobomax
218127307Srwatsonstatic void
219127307Srwatsongre_clone_destroy(ifp)
220127307Srwatson	struct ifnet *ifp;
221127307Srwatson{
222127307Srwatson	struct gre_softc *sc = ifp->if_softc;
223127307Srwatson
224127307Srwatson	mtx_lock(&gre_mtx);
225127307Srwatson	LIST_REMOVE(sc, sc_list);
226127307Srwatson	mtx_unlock(&gre_mtx);
227127307Srwatson	gre_destroy(sc);
228127307Srwatson}
229127307Srwatson
230103026Ssobomax/*
231103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol
232103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004
233103026Ssobomax */
234103032Ssobomaxstatic int
235103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
236103026Ssobomax	   struct rtentry *rt)
237103026Ssobomax{
238103026Ssobomax	int error = 0;
239103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
240103026Ssobomax	struct greip *gh;
241103026Ssobomax	struct ip *ip;
242148613Sbz	u_short ip_id = 0;
243148613Sbz	uint8_t ip_tos = 0;
244123992Ssobomax	u_int16_t etype = 0;
245103026Ssobomax	struct mobile_h mob_h;
246147611Sdwmalone	u_int32_t af;
247103026Ssobomax
248103026Ssobomax	/*
249103026Ssobomax	 * gre may cause infinite recursion calls when misconfigured.
250103026Ssobomax	 * We'll prevent this by introducing upper limit.
251103026Ssobomax	 */
252103026Ssobomax	if (++(sc->called) > max_gre_nesting) {
253103026Ssobomax		printf("%s: gre_output: recursively called too many "
254147256Sbrooks		       "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
255103026Ssobomax		m_freem(m);
256103026Ssobomax		error = EIO;    /* is there better errno? */
257103026Ssobomax		goto end;
258103026Ssobomax	}
259103026Ssobomax
260103026Ssobomax	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
261103026Ssobomax	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
262103026Ssobomax		m_freem(m);
263103026Ssobomax		error = ENETDOWN;
264103026Ssobomax		goto end;
265103026Ssobomax	}
266103026Ssobomax
267103026Ssobomax	gh = NULL;
268103026Ssobomax	ip = NULL;
269103026Ssobomax
270147611Sdwmalone	/* BPF writes need to be handled specially. */
271147611Sdwmalone	if (dst->sa_family == AF_UNSPEC) {
272147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
273147611Sdwmalone		dst->sa_family = af;
274147611Sdwmalone	}
275147611Sdwmalone
276103026Ssobomax	if (ifp->if_bpf) {
277147611Sdwmalone		af = dst->sa_family;
278123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
279103026Ssobomax	}
280103026Ssobomax
281103026Ssobomax	m->m_flags &= ~(M_BCAST|M_MCAST);
282103026Ssobomax
283103026Ssobomax	if (sc->g_proto == IPPROTO_MOBILE) {
284103026Ssobomax		if (dst->sa_family == AF_INET) {
285103026Ssobomax			struct mbuf *m0;
286103026Ssobomax			int msiz;
287103026Ssobomax
288103026Ssobomax			ip = mtod(m, struct ip *);
289103026Ssobomax
290103026Ssobomax			/*
291103026Ssobomax			 * RFC2004 specifies that fragmented diagrams shouldn't
292103026Ssobomax			 * be encapsulated.
293103026Ssobomax			 */
294103026Ssobomax			if ((ip->ip_off & IP_MF) != 0) {
295103026Ssobomax				_IF_DROP(&ifp->if_snd);
296103026Ssobomax				m_freem(m);
297103026Ssobomax				error = EINVAL;    /* is there better errno? */
298103026Ssobomax				goto end;
299103026Ssobomax			}
300103026Ssobomax			memset(&mob_h, 0, MOB_H_SIZ_L);
301103026Ssobomax			mob_h.proto = (ip->ip_p) << 8;
302103026Ssobomax			mob_h.odst = ip->ip_dst.s_addr;
303103026Ssobomax			ip->ip_dst.s_addr = sc->g_dst.s_addr;
304103026Ssobomax
305103026Ssobomax			/*
306103026Ssobomax			 * If the packet comes from our host, we only change
307103026Ssobomax			 * the destination address in the IP header.
308103026Ssobomax			 * Else we also need to save and change the source
309103026Ssobomax			 */
310103026Ssobomax			if (in_hosteq(ip->ip_src, sc->g_src)) {
311103026Ssobomax				msiz = MOB_H_SIZ_S;
312103026Ssobomax			} else {
313103026Ssobomax				mob_h.proto |= MOB_H_SBIT;
314103026Ssobomax				mob_h.osrc = ip->ip_src.s_addr;
315103026Ssobomax				ip->ip_src.s_addr = sc->g_src.s_addr;
316103026Ssobomax				msiz = MOB_H_SIZ_L;
317103026Ssobomax			}
318103026Ssobomax			mob_h.proto = htons(mob_h.proto);
319123992Ssobomax			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
320103026Ssobomax
321103026Ssobomax			if ((m->m_data - msiz) < m->m_pktdat) {
322103026Ssobomax				/* need new mbuf */
323111119Simp				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
324103026Ssobomax				if (m0 == NULL) {
325103026Ssobomax					_IF_DROP(&ifp->if_snd);
326103026Ssobomax					m_freem(m);
327103026Ssobomax					error = ENOBUFS;
328103026Ssobomax					goto end;
329103026Ssobomax				}
330103026Ssobomax				m0->m_next = m;
331103026Ssobomax				m->m_data += sizeof(struct ip);
332103026Ssobomax				m->m_len -= sizeof(struct ip);
333103026Ssobomax				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
334103026Ssobomax				m0->m_len = msiz + sizeof(struct ip);
335103026Ssobomax				m0->m_data += max_linkhdr;
336103026Ssobomax				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
337103026Ssobomax				       sizeof(struct ip));
338103026Ssobomax				m = m0;
339103026Ssobomax			} else {  /* we have some space left in the old one */
340103026Ssobomax				m->m_data -= msiz;
341103026Ssobomax				m->m_len += msiz;
342103026Ssobomax				m->m_pkthdr.len += msiz;
343103026Ssobomax				bcopy(ip, mtod(m, caddr_t),
344103026Ssobomax					sizeof(struct ip));
345103026Ssobomax			}
346103026Ssobomax			ip = mtod(m, struct ip *);
347103026Ssobomax			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
348103026Ssobomax			ip->ip_len = ntohs(ip->ip_len) + msiz;
349103026Ssobomax		} else {  /* AF_INET */
350103026Ssobomax			_IF_DROP(&ifp->if_snd);
351103026Ssobomax			m_freem(m);
352103026Ssobomax			error = EINVAL;
353103026Ssobomax			goto end;
354103026Ssobomax		}
355103026Ssobomax	} else if (sc->g_proto == IPPROTO_GRE) {
356103026Ssobomax		switch (dst->sa_family) {
357103026Ssobomax		case AF_INET:
358103026Ssobomax			ip = mtod(m, struct ip *);
359148613Sbz			ip_tos = ip->ip_tos;
360148613Sbz			ip_id = ip->ip_id;
361103026Ssobomax			etype = ETHERTYPE_IP;
362103026Ssobomax			break;
363148613Sbz#ifdef INET6
364148613Sbz		case AF_INET6:
365148613Sbz			ip_id = ip_newid();
366148613Sbz			etype = ETHERTYPE_IPV6;
367148613Sbz			break;
368148613Sbz#endif
369103026Ssobomax#ifdef NETATALK
370103026Ssobomax		case AF_APPLETALK:
371103026Ssobomax			etype = ETHERTYPE_ATALK;
372103026Ssobomax			break;
373103026Ssobomax#endif
374103026Ssobomax		default:
375103026Ssobomax			_IF_DROP(&ifp->if_snd);
376103026Ssobomax			m_freem(m);
377103026Ssobomax			error = EAFNOSUPPORT;
378103026Ssobomax			goto end;
379103026Ssobomax		}
380111119Simp		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
381103026Ssobomax	} else {
382103026Ssobomax		_IF_DROP(&ifp->if_snd);
383103026Ssobomax		m_freem(m);
384103026Ssobomax		error = EINVAL;
385103026Ssobomax		goto end;
386103026Ssobomax	}
387103026Ssobomax
388128580Sandre	if (m == NULL) {	/* mbuf allocation failed */
389103026Ssobomax		_IF_DROP(&ifp->if_snd);
390103026Ssobomax		error = ENOBUFS;
391103026Ssobomax		goto end;
392103026Ssobomax	}
393103026Ssobomax
394103026Ssobomax	gh = mtod(m, struct greip *);
395103026Ssobomax	if (sc->g_proto == IPPROTO_GRE) {
396103026Ssobomax		/* we don't have any GRE flags for now */
397125226Ssobomax		memset((void *)gh, 0, sizeof(struct greip));
398103026Ssobomax		gh->gi_ptype = htons(etype);
399103026Ssobomax	}
400103026Ssobomax
401103026Ssobomax	gh->gi_pr = sc->g_proto;
402103026Ssobomax	if (sc->g_proto != IPPROTO_MOBILE) {
403103026Ssobomax		gh->gi_src = sc->g_src;
404103026Ssobomax		gh->gi_dst = sc->g_dst;
405133163Ssobomax		((struct ip*)gh)->ip_v = IPPROTO_IPV4;
406103026Ssobomax		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
407103032Ssobomax		((struct ip*)gh)->ip_ttl = GRE_TTL;
408148613Sbz		((struct ip*)gh)->ip_tos = ip_tos;
409148613Sbz		((struct ip*)gh)->ip_id = ip_id;
410125226Ssobomax		gh->gi_len = m->m_pkthdr.len;
411103026Ssobomax	}
412103026Ssobomax
413103026Ssobomax	ifp->if_opackets++;
414103026Ssobomax	ifp->if_obytes += m->m_pkthdr.len;
415128583Sandre	/*
416128583Sandre	 * Send it off and with IP_FORWARD flag to prevent it from
417128583Sandre	 * overwriting the ip_id again.  ip_id is already set to the
418128583Sandre	 * ip_id of the encapsulated packet.
419128583Sandre	 */
420128580Sandre	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
421123992Ssobomax	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
422103026Ssobomax  end:
423103026Ssobomax	sc->called = 0;
424103026Ssobomax	if (error)
425103026Ssobomax		ifp->if_oerrors++;
426103026Ssobomax	return (error);
427103026Ssobomax}
428103026Ssobomax
429103032Ssobomaxstatic int
430103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
431103026Ssobomax{
432103026Ssobomax	struct ifreq *ifr = (struct ifreq *)data;
433103026Ssobomax	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
434103026Ssobomax	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
435103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
436103026Ssobomax	int s;
437103026Ssobomax	struct sockaddr_in si;
438103026Ssobomax	struct sockaddr *sa = NULL;
439103026Ssobomax	int error;
440103026Ssobomax	struct sockaddr_in sp, sm, dp, dm;
441103026Ssobomax
442103026Ssobomax	error = 0;
443103026Ssobomax
444103026Ssobomax	s = splnet();
445103026Ssobomax	switch (cmd) {
446103026Ssobomax	case SIOCSIFADDR:
447103026Ssobomax		ifp->if_flags |= IFF_UP;
448103026Ssobomax		break;
449125020Ssobomax	case SIOCSIFDSTADDR:
450103026Ssobomax		break;
451103026Ssobomax	case SIOCSIFFLAGS:
452103026Ssobomax		if ((error = suser(curthread)) != 0)
453103026Ssobomax			break;
454103026Ssobomax		if ((ifr->ifr_flags & IFF_LINK0) != 0)
455103026Ssobomax			sc->g_proto = IPPROTO_GRE;
456103026Ssobomax		else
457103026Ssobomax			sc->g_proto = IPPROTO_MOBILE;
458125024Ssobomax		if ((ifr->ifr_flags & IFF_LINK2) != 0)
459125024Ssobomax			sc->wccp_ver = WCCP_V2;
460125024Ssobomax		else
461125024Ssobomax			sc->wccp_ver = WCCP_V1;
462103026Ssobomax		goto recompute;
463103026Ssobomax	case SIOCSIFMTU:
464103026Ssobomax		if ((error = suser(curthread)) != 0)
465103026Ssobomax			break;
466103026Ssobomax		if (ifr->ifr_mtu < 576) {
467103026Ssobomax			error = EINVAL;
468103026Ssobomax			break;
469103026Ssobomax		}
470103026Ssobomax		ifp->if_mtu = ifr->ifr_mtu;
471103026Ssobomax		break;
472103026Ssobomax	case SIOCGIFMTU:
473147256Sbrooks		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
474103026Ssobomax		break;
475103026Ssobomax	case SIOCADDMULTI:
476103026Ssobomax	case SIOCDELMULTI:
477103026Ssobomax		if ((error = suser(curthread)) != 0)
478103026Ssobomax			break;
479103026Ssobomax		if (ifr == 0) {
480103026Ssobomax			error = EAFNOSUPPORT;
481103026Ssobomax			break;
482103026Ssobomax		}
483103026Ssobomax		switch (ifr->ifr_addr.sa_family) {
484103026Ssobomax#ifdef INET
485103026Ssobomax		case AF_INET:
486103026Ssobomax			break;
487103026Ssobomax#endif
488148613Sbz#ifdef INET6
489148613Sbz		case AF_INET6:
490148613Sbz			break;
491148613Sbz#endif
492103026Ssobomax		default:
493103026Ssobomax			error = EAFNOSUPPORT;
494103026Ssobomax			break;
495103026Ssobomax		}
496103026Ssobomax		break;
497103026Ssobomax	case GRESPROTO:
498103026Ssobomax		if ((error = suser(curthread)) != 0)
499103026Ssobomax			break;
500103026Ssobomax		sc->g_proto = ifr->ifr_flags;
501103026Ssobomax		switch (sc->g_proto) {
502103026Ssobomax		case IPPROTO_GRE:
503103026Ssobomax			ifp->if_flags |= IFF_LINK0;
504103026Ssobomax			break;
505103026Ssobomax		case IPPROTO_MOBILE:
506103026Ssobomax			ifp->if_flags &= ~IFF_LINK0;
507103026Ssobomax			break;
508103026Ssobomax		default:
509103026Ssobomax			error = EPROTONOSUPPORT;
510103026Ssobomax			break;
511103026Ssobomax		}
512103026Ssobomax		goto recompute;
513103026Ssobomax	case GREGPROTO:
514103026Ssobomax		ifr->ifr_flags = sc->g_proto;
515103026Ssobomax		break;
516103026Ssobomax	case GRESADDRS:
517103026Ssobomax	case GRESADDRD:
518103026Ssobomax		if ((error = suser(curthread)) != 0)
519103026Ssobomax			break;
520103026Ssobomax		/*
521103026Ssobomax		 * set tunnel endpoints, compute a less specific route
522103026Ssobomax		 * to the remote end and mark if as up
523103026Ssobomax		 */
524103026Ssobomax		sa = &ifr->ifr_addr;
525103026Ssobomax		if (cmd == GRESADDRS)
526103026Ssobomax			sc->g_src = (satosin(sa))->sin_addr;
527103026Ssobomax		if (cmd == GRESADDRD)
528103026Ssobomax			sc->g_dst = (satosin(sa))->sin_addr;
529103026Ssobomax	recompute:
530103026Ssobomax#ifdef INET
531103026Ssobomax		if (sc->encap != NULL) {
532103026Ssobomax			encap_detach(sc->encap);
533103026Ssobomax			sc->encap = NULL;
534103026Ssobomax		}
535103026Ssobomax#endif
536103026Ssobomax		if ((sc->g_src.s_addr != INADDR_ANY) &&
537103026Ssobomax		    (sc->g_dst.s_addr != INADDR_ANY)) {
538103026Ssobomax			bzero(&sp, sizeof(sp));
539103026Ssobomax			bzero(&sm, sizeof(sm));
540103026Ssobomax			bzero(&dp, sizeof(dp));
541103026Ssobomax			bzero(&dm, sizeof(dm));
542103026Ssobomax			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
543103026Ssobomax			    sizeof(struct sockaddr_in);
544103026Ssobomax			sp.sin_family = sm.sin_family = dp.sin_family =
545103026Ssobomax			    dm.sin_family = AF_INET;
546103026Ssobomax			sp.sin_addr = sc->g_src;
547103026Ssobomax			dp.sin_addr = sc->g_dst;
548125020Ssobomax			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
549103026Ssobomax			    INADDR_BROADCAST;
550103026Ssobomax#ifdef INET
551103026Ssobomax			sc->encap = encap_attach(AF_INET, sc->g_proto,
552103026Ssobomax			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
553103026Ssobomax			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
554103026Ssobomax				&in_gre_protosw : &in_mobile_protosw, sc);
555103026Ssobomax			if (sc->encap == NULL)
556103026Ssobomax				printf("%s: unable to attach encap\n",
557147256Sbrooks				    if_name(GRE2IFP(sc)));
558103026Ssobomax#endif
559103026Ssobomax			if (sc->route.ro_rt != 0) /* free old route */
560103026Ssobomax				RTFREE(sc->route.ro_rt);
561103026Ssobomax			if (gre_compute_route(sc) == 0)
562103026Ssobomax				ifp->if_flags |= IFF_RUNNING;
563103026Ssobomax			else
564103026Ssobomax				ifp->if_flags &= ~IFF_RUNNING;
565103026Ssobomax		}
566103026Ssobomax		break;
567103026Ssobomax	case GREGADDRS:
568103026Ssobomax		memset(&si, 0, sizeof(si));
569103026Ssobomax		si.sin_family = AF_INET;
570103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
571103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
572103026Ssobomax		sa = sintosa(&si);
573103026Ssobomax		ifr->ifr_addr = *sa;
574103026Ssobomax		break;
575103026Ssobomax	case GREGADDRD:
576103026Ssobomax		memset(&si, 0, sizeof(si));
577103026Ssobomax		si.sin_family = AF_INET;
578103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
579103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
580103026Ssobomax		sa = sintosa(&si);
581103026Ssobomax		ifr->ifr_addr = *sa;
582103026Ssobomax		break;
583103026Ssobomax	case SIOCSIFPHYADDR:
584103026Ssobomax		if ((error = suser(curthread)) != 0)
585103026Ssobomax			break;
586103026Ssobomax		if (aifr->ifra_addr.sin_family != AF_INET ||
587103026Ssobomax		    aifr->ifra_dstaddr.sin_family != AF_INET) {
588103026Ssobomax			error = EAFNOSUPPORT;
589103026Ssobomax			break;
590103026Ssobomax		}
591103026Ssobomax		if (aifr->ifra_addr.sin_len != sizeof(si) ||
592103026Ssobomax		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
593103026Ssobomax			error = EINVAL;
594103026Ssobomax			break;
595103026Ssobomax		}
596103026Ssobomax		sc->g_src = aifr->ifra_addr.sin_addr;
597103026Ssobomax		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
598103026Ssobomax		goto recompute;
599103026Ssobomax	case SIOCSLIFPHYADDR:
600103026Ssobomax		if ((error = suser(curthread)) != 0)
601103026Ssobomax			break;
602103026Ssobomax		if (lifr->addr.ss_family != AF_INET ||
603103026Ssobomax		    lifr->dstaddr.ss_family != AF_INET) {
604103026Ssobomax			error = EAFNOSUPPORT;
605103026Ssobomax			break;
606103026Ssobomax		}
607103026Ssobomax		if (lifr->addr.ss_len != sizeof(si) ||
608103026Ssobomax		    lifr->dstaddr.ss_len != sizeof(si)) {
609103026Ssobomax			error = EINVAL;
610103026Ssobomax			break;
611103026Ssobomax		}
612103026Ssobomax		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
613103026Ssobomax		sc->g_dst =
614103026Ssobomax		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
615103026Ssobomax		goto recompute;
616103026Ssobomax	case SIOCDIFPHYADDR:
617103026Ssobomax		if ((error = suser(curthread)) != 0)
618103026Ssobomax			break;
619103026Ssobomax		sc->g_src.s_addr = INADDR_ANY;
620103026Ssobomax		sc->g_dst.s_addr = INADDR_ANY;
621103026Ssobomax		goto recompute;
622103026Ssobomax	case SIOCGLIFPHYADDR:
623103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY ||
624103026Ssobomax		    sc->g_dst.s_addr == INADDR_ANY) {
625103026Ssobomax			error = EADDRNOTAVAIL;
626103026Ssobomax			break;
627103026Ssobomax		}
628103026Ssobomax		memset(&si, 0, sizeof(si));
629103026Ssobomax		si.sin_family = AF_INET;
630103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
631103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
632103026Ssobomax		memcpy(&lifr->addr, &si, sizeof(si));
633103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
634103026Ssobomax		memcpy(&lifr->dstaddr, &si, sizeof(si));
635103026Ssobomax		break;
636103026Ssobomax	case SIOCGIFPSRCADDR:
637122699Sbms#ifdef INET6
638122699Sbms	case SIOCGIFPSRCADDR_IN6:
639122699Sbms#endif
640103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY) {
641103026Ssobomax			error = EADDRNOTAVAIL;
642103026Ssobomax			break;
643103026Ssobomax		}
644103026Ssobomax		memset(&si, 0, sizeof(si));
645103026Ssobomax		si.sin_family = AF_INET;
646103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
647103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
648103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
649103026Ssobomax		break;
650103026Ssobomax	case SIOCGIFPDSTADDR:
651122699Sbms#ifdef INET6
652122699Sbms	case SIOCGIFPDSTADDR_IN6:
653122699Sbms#endif
654103026Ssobomax		if (sc->g_dst.s_addr == INADDR_ANY) {
655103026Ssobomax			error = EADDRNOTAVAIL;
656103026Ssobomax			break;
657103026Ssobomax		}
658103026Ssobomax		memset(&si, 0, sizeof(si));
659103026Ssobomax		si.sin_family = AF_INET;
660103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
661103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
662103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
663103026Ssobomax		break;
664103026Ssobomax	default:
665103026Ssobomax		error = EINVAL;
666103026Ssobomax		break;
667103026Ssobomax	}
668103026Ssobomax
669103026Ssobomax	splx(s);
670103026Ssobomax	return (error);
671103026Ssobomax}
672103026Ssobomax
673103026Ssobomax/*
674103026Ssobomax * computes a route to our destination that is not the one
675103026Ssobomax * which would be taken by ip_output(), as this one will loop back to
676103026Ssobomax * us. If the interface is p2p as  a--->b, then a routing entry exists
677103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here
678123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to
679103026Ssobomax * if_gre.
680103026Ssobomax * Goal here is to compute a route to b that is less specific than
681103026Ssobomax * a-->b. We know that this one exists as in normal operation we have
682103026Ssobomax * at least a default route which matches.
683103026Ssobomax */
684103032Ssobomaxstatic int
685103026Ssobomaxgre_compute_route(struct gre_softc *sc)
686103026Ssobomax{
687103026Ssobomax	struct route *ro;
688103026Ssobomax	u_int32_t a, b, c;
689103026Ssobomax
690103026Ssobomax	ro = &sc->route;
691103026Ssobomax
692103026Ssobomax	memset(ro, 0, sizeof(struct route));
693103026Ssobomax	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
694103026Ssobomax	ro->ro_dst.sa_family = AF_INET;
695103026Ssobomax	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
696103026Ssobomax
697103026Ssobomax	/*
698103026Ssobomax	 * toggle last bit, so our interface is not found, but a less
699103026Ssobomax	 * specific route. I'd rather like to specify a shorter mask,
700103026Ssobomax	 * but this is not possible. Should work though. XXX
701103026Ssobomax	 * there is a simpler way ...
702103026Ssobomax	 */
703147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
704103026Ssobomax		a = ntohl(sc->g_dst.s_addr);
705103026Ssobomax		b = a & 0x01;
706103026Ssobomax		c = a & 0xfffffffe;
707103026Ssobomax		b = b ^ 0x01;
708103026Ssobomax		a = b | c;
709103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
710103026Ssobomax		    = htonl(a);
711103026Ssobomax	}
712103026Ssobomax
713103026Ssobomax#ifdef DIAGNOSTIC
714147256Sbrooks	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
715103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
716103026Ssobomax#endif
717103026Ssobomax
718103026Ssobomax	rtalloc(ro);
719103026Ssobomax
720103026Ssobomax	/*
721103026Ssobomax	 * check if this returned a route at all and this route is no
722103026Ssobomax	 * recursion to ourself
723103026Ssobomax	 */
724103026Ssobomax	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
725103026Ssobomax#ifdef DIAGNOSTIC
726103026Ssobomax		if (ro->ro_rt == NULL)
727103026Ssobomax			printf(" - no route found!\n");
728103026Ssobomax		else
729103026Ssobomax			printf(" - route loops back to ourself!\n");
730103026Ssobomax#endif
731103026Ssobomax		return EADDRNOTAVAIL;
732103026Ssobomax	}
733103026Ssobomax
734103026Ssobomax	/*
735103026Ssobomax	 * now change it back - else ip_output will just drop
736103026Ssobomax	 * the route and search one to this interface ...
737103026Ssobomax	 */
738147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
739103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
740103026Ssobomax
741103026Ssobomax#ifdef DIAGNOSTIC
742103026Ssobomax	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
743103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
744103026Ssobomax	printf("\n");
745103026Ssobomax#endif
746103026Ssobomax
747103026Ssobomax	return 0;
748103026Ssobomax}
749103026Ssobomax
750103026Ssobomax/*
751103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on
752103026Ssobomax * mbufs.
753103026Ssobomax */
754123992Ssobomaxu_int16_t
755123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len)
756103026Ssobomax{
757123992Ssobomax	u_int32_t sum = 0;
758103026Ssobomax	int nwords = len >> 1;
759103026Ssobomax
760103026Ssobomax	while (nwords-- != 0)
761103026Ssobomax		sum += *p++;
762103026Ssobomax
763103026Ssobomax	if (len & 1) {
764103026Ssobomax		union {
765103026Ssobomax			u_short w;
766103026Ssobomax			u_char c[2];
767103026Ssobomax		} u;
768103026Ssobomax		u.c[0] = *(u_char *)p;
769103026Ssobomax		u.c[1] = 0;
770103026Ssobomax		sum += u.w;
771103026Ssobomax	}
772103026Ssobomax
773103026Ssobomax	/* end-around-carry */
774103026Ssobomax	sum = (sum >> 16) + (sum & 0xffff);
775103026Ssobomax	sum += (sum >> 16);
776103026Ssobomax	return (~sum);
777103026Ssobomax}
778103026Ssobomax
779103026Ssobomaxstatic int
780103026Ssobomaxgremodevent(module_t mod, int type, void *data)
781103026Ssobomax{
782127307Srwatson	struct gre_softc *sc;
783103026Ssobomax
784103026Ssobomax	switch (type) {
785103026Ssobomax	case MOD_LOAD:
786103026Ssobomax		greattach();
787103026Ssobomax		break;
788103026Ssobomax	case MOD_UNLOAD:
789103026Ssobomax		if_clone_detach(&gre_cloner);
790103026Ssobomax
791127307Srwatson		mtx_lock(&gre_mtx);
792127307Srwatson		while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) {
793127307Srwatson			LIST_REMOVE(sc, sc_list);
794127307Srwatson			mtx_unlock(&gre_mtx);
795127307Srwatson			gre_destroy(sc);
796127307Srwatson			mtx_lock(&gre_mtx);
797127307Srwatson		}
798127307Srwatson		mtx_unlock(&gre_mtx);
799127307Srwatson		mtx_destroy(&gre_mtx);
800103026Ssobomax		break;
801132199Sphk	default:
802132199Sphk		return EOPNOTSUPP;
803103026Ssobomax	}
804103026Ssobomax	return 0;
805103026Ssobomax}
806103026Ssobomax
807103026Ssobomaxstatic moduledata_t gre_mod = {
808103026Ssobomax	"if_gre",
809103026Ssobomax	gremodevent,
810103026Ssobomax	0
811103026Ssobomax};
812103026Ssobomax
813103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
814103026SsobomaxMODULE_VERSION(if_gre, 1);
815