if_gre.c revision 178888
1123992Ssobomax/*	$NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2103026Ssobomax/*	 $FreeBSD: head/sys/net/if_gre.c 178888 2008-05-09 23:03:00Z julian $ */
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>
60164033Srwatson#include <sys/priv.h>
61178888Sjulian#include <sys/proc.h>
62103026Ssobomax#include <sys/protosw.h>
63103026Ssobomax#include <sys/socket.h>
64103026Ssobomax#include <sys/sockio.h>
65103026Ssobomax#include <sys/sysctl.h>
66103344Sbde#include <sys/systm.h>
67103026Ssobomax
68103026Ssobomax#include <net/ethernet.h>
69103026Ssobomax#include <net/if.h>
70130933Sbrooks#include <net/if_clone.h>
71103026Ssobomax#include <net/if_types.h>
72103026Ssobomax#include <net/route.h>
73103026Ssobomax
74103026Ssobomax#ifdef INET
75103026Ssobomax#include <netinet/in.h>
76103026Ssobomax#include <netinet/in_systm.h>
77103026Ssobomax#include <netinet/in_var.h>
78103026Ssobomax#include <netinet/ip.h>
79103026Ssobomax#include <netinet/ip_gre.h>
80103026Ssobomax#include <netinet/ip_var.h>
81103026Ssobomax#include <netinet/ip_encap.h>
82103026Ssobomax#else
83103026Ssobomax#error "Huh? if_gre without inet?"
84103026Ssobomax#endif
85103026Ssobomax
86103026Ssobomax#include <net/bpf.h>
87103026Ssobomax
88103026Ssobomax#include <net/if_gre.h>
89103026Ssobomax
90103026Ssobomax/*
91103026Ssobomax * It is not easy to calculate the right value for a GRE MTU.
92103026Ssobomax * We leave this task to the admin and use the same default that
93103026Ssobomax * other vendors use.
94103026Ssobomax */
95103026Ssobomax#define GREMTU	1476
96103026Ssobomax
97103026Ssobomax#define GRENAME	"gre"
98103026Ssobomax
99127307Srwatson/*
100127307Srwatson * gre_mtx protects all global variables in if_gre.c.
101127307Srwatson * XXX: gre_softc data not protected yet.
102127307Srwatson */
103127307Srwatsonstruct mtx gre_mtx;
104103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
105103026Ssobomax
106103026Ssobomaxstruct gre_softc_head gre_softc_list;
107103026Ssobomax
108160195Ssamstatic int	gre_clone_create(struct if_clone *, int, caddr_t);
109105300Salfredstatic void	gre_clone_destroy(struct ifnet *);
110103032Ssobomaxstatic int	gre_ioctl(struct ifnet *, u_long, caddr_t);
111103032Ssobomaxstatic int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
112103032Ssobomax		    struct rtentry *rt);
113103026Ssobomax
114130933SbrooksIFC_SIMPLE_DECLARE(gre, 0);
115103026Ssobomax
116103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc);
117103026Ssobomax
118105300Salfredstatic void	greattach(void);
119103026Ssobomax
120103026Ssobomax#ifdef INET
121103026Ssobomaxextern struct domain inetdomain;
122152242Srustatic const struct protosw in_gre_protosw = {
123152242Sru	.pr_type =		SOCK_RAW,
124152242Sru	.pr_domain =		&inetdomain,
125152242Sru	.pr_protocol =		IPPROTO_GRE,
126152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
127154625Sbz	.pr_input =		gre_input,
128152242Sru	.pr_output =		(pr_output_t *)rip_output,
129152242Sru	.pr_ctlinput =		rip_ctlinput,
130152242Sru	.pr_ctloutput =		rip_ctloutput,
131152242Sru	.pr_usrreqs =		&rip_usrreqs
132103026Ssobomax};
133152242Srustatic const struct protosw in_mobile_protosw = {
134152242Sru	.pr_type =		SOCK_RAW,
135152242Sru	.pr_domain =		&inetdomain,
136152242Sru	.pr_protocol =		IPPROTO_MOBILE,
137152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
138154625Sbz	.pr_input =		gre_mobile_input,
139152242Sru	.pr_output =		(pr_output_t *)rip_output,
140152242Sru	.pr_ctlinput =		rip_ctlinput,
141152242Sru	.pr_ctloutput =		rip_ctloutput,
142152242Sru	.pr_usrreqs =		&rip_usrreqs
143103026Ssobomax};
144103026Ssobomax#endif
145103026Ssobomax
146103026SsobomaxSYSCTL_DECL(_net_link);
147123338SbmsSYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
148103026Ssobomax    "Generic Routing Encapsulation");
149103026Ssobomax#ifndef MAX_GRE_NEST
150103026Ssobomax/*
151103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels.
152103026Ssobomax * Since, setting a large value to this macro with a careless configuration
153103026Ssobomax * may introduce system crash, we don't allow any nestings by default.
154103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro
155103026Ssobomax * in your kernel configuration file.  However, if you do so, please be
156103026Ssobomax * careful to configure the tunnels so that it won't make a loop.
157103026Ssobomax */
158103026Ssobomax#define MAX_GRE_NEST 1
159103026Ssobomax#endif
160103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST;
161103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
162103026Ssobomax    &max_gre_nesting, 0, "Max nested tunnels");
163103026Ssobomax
164103026Ssobomax/* ARGSUSED */
165103032Ssobomaxstatic void
166103026Ssobomaxgreattach(void)
167103026Ssobomax{
168103026Ssobomax
169127307Srwatson	mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
170103026Ssobomax	LIST_INIT(&gre_softc_list);
171103026Ssobomax	if_clone_attach(&gre_cloner);
172103026Ssobomax}
173103026Ssobomax
174103032Ssobomaxstatic int
175160195Ssamgre_clone_create(ifc, unit, params)
176103026Ssobomax	struct if_clone *ifc;
177103026Ssobomax	int unit;
178160195Ssam	caddr_t params;
179103026Ssobomax{
180103026Ssobomax	struct gre_softc *sc;
181103026Ssobomax
182131673Sbms	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
183103026Ssobomax
184147643Sbz	GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
185147643Sbz	if (GRE2IFP(sc) == NULL) {
186147643Sbz		free(sc, M_GRE);
187147643Sbz		return (ENOSPC);
188147643Sbz	}
189147643Sbz
190147643Sbz	GRE2IFP(sc)->if_softc = sc;
191147256Sbrooks	if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
192147643Sbz
193147256Sbrooks	GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
194147256Sbrooks	GRE2IFP(sc)->if_addrlen = 0;
195147256Sbrooks	GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
196147256Sbrooks	GRE2IFP(sc)->if_mtu = GREMTU;
197147256Sbrooks	GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
198147256Sbrooks	GRE2IFP(sc)->if_output = gre_output;
199147256Sbrooks	GRE2IFP(sc)->if_ioctl = gre_ioctl;
200103026Ssobomax	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
201103026Ssobomax	sc->g_proto = IPPROTO_GRE;
202147256Sbrooks	GRE2IFP(sc)->if_flags |= IFF_LINK0;
203103026Ssobomax	sc->encap = NULL;
204103026Ssobomax	sc->called = 0;
205178888Sjulian	sc->gre_fibnum = curthread->td_proc->p_fibnum;
206125024Ssobomax	sc->wccp_ver = WCCP_V1;
207147256Sbrooks	if_attach(GRE2IFP(sc));
208147256Sbrooks	bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
209127307Srwatson	mtx_lock(&gre_mtx);
210103026Ssobomax	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
211127307Srwatson	mtx_unlock(&gre_mtx);
212103026Ssobomax	return (0);
213103026Ssobomax}
214103026Ssobomax
215103032Ssobomaxstatic void
216127307Srwatsongre_clone_destroy(ifp)
217127307Srwatson	struct ifnet *ifp;
218127307Srwatson{
219127307Srwatson	struct gre_softc *sc = ifp->if_softc;
220127307Srwatson
221127307Srwatson	mtx_lock(&gre_mtx);
222127307Srwatson	LIST_REMOVE(sc, sc_list);
223127307Srwatson	mtx_unlock(&gre_mtx);
224151266Sthompsa
225151266Sthompsa#ifdef INET
226151266Sthompsa	if (sc->encap != NULL)
227151266Sthompsa		encap_detach(sc->encap);
228151266Sthompsa#endif
229151266Sthompsa	bpfdetach(ifp);
230151266Sthompsa	if_detach(ifp);
231151266Sthompsa	if_free(ifp);
232151266Sthompsa	free(sc, M_GRE);
233127307Srwatson}
234127307Srwatson
235103026Ssobomax/*
236103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol
237103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004
238103026Ssobomax */
239103032Ssobomaxstatic int
240103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
241103026Ssobomax	   struct rtentry *rt)
242103026Ssobomax{
243103026Ssobomax	int error = 0;
244103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
245103026Ssobomax	struct greip *gh;
246103026Ssobomax	struct ip *ip;
247148613Sbz	u_short ip_id = 0;
248148613Sbz	uint8_t ip_tos = 0;
249123992Ssobomax	u_int16_t etype = 0;
250103026Ssobomax	struct mobile_h mob_h;
251147611Sdwmalone	u_int32_t af;
252103026Ssobomax
253103026Ssobomax	/*
254103026Ssobomax	 * gre may cause infinite recursion calls when misconfigured.
255103026Ssobomax	 * We'll prevent this by introducing upper limit.
256103026Ssobomax	 */
257103026Ssobomax	if (++(sc->called) > max_gre_nesting) {
258103026Ssobomax		printf("%s: gre_output: recursively called too many "
259147256Sbrooks		       "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
260103026Ssobomax		m_freem(m);
261103026Ssobomax		error = EIO;    /* is there better errno? */
262103026Ssobomax		goto end;
263103026Ssobomax	}
264103026Ssobomax
265148887Srwatson	if (!((ifp->if_flags & IFF_UP) &&
266148887Srwatson	    (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
267103026Ssobomax	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
268103026Ssobomax		m_freem(m);
269103026Ssobomax		error = ENETDOWN;
270103026Ssobomax		goto end;
271103026Ssobomax	}
272103026Ssobomax
273103026Ssobomax	gh = NULL;
274103026Ssobomax	ip = NULL;
275103026Ssobomax
276147611Sdwmalone	/* BPF writes need to be handled specially. */
277147611Sdwmalone	if (dst->sa_family == AF_UNSPEC) {
278147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
279147611Sdwmalone		dst->sa_family = af;
280147611Sdwmalone	}
281147611Sdwmalone
282159180Scsjp	if (bpf_peers_present(ifp->if_bpf)) {
283147611Sdwmalone		af = dst->sa_family;
284123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
285103026Ssobomax	}
286103026Ssobomax
287103026Ssobomax	m->m_flags &= ~(M_BCAST|M_MCAST);
288103026Ssobomax
289103026Ssobomax	if (sc->g_proto == IPPROTO_MOBILE) {
290103026Ssobomax		if (dst->sa_family == AF_INET) {
291103026Ssobomax			struct mbuf *m0;
292103026Ssobomax			int msiz;
293103026Ssobomax
294103026Ssobomax			ip = mtod(m, struct ip *);
295103026Ssobomax
296103026Ssobomax			/*
297103026Ssobomax			 * RFC2004 specifies that fragmented diagrams shouldn't
298103026Ssobomax			 * be encapsulated.
299103026Ssobomax			 */
300158416Shsu			if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
301103026Ssobomax				_IF_DROP(&ifp->if_snd);
302103026Ssobomax				m_freem(m);
303103026Ssobomax				error = EINVAL;    /* is there better errno? */
304103026Ssobomax				goto end;
305103026Ssobomax			}
306103026Ssobomax			memset(&mob_h, 0, MOB_H_SIZ_L);
307103026Ssobomax			mob_h.proto = (ip->ip_p) << 8;
308103026Ssobomax			mob_h.odst = ip->ip_dst.s_addr;
309103026Ssobomax			ip->ip_dst.s_addr = sc->g_dst.s_addr;
310103026Ssobomax
311103026Ssobomax			/*
312103026Ssobomax			 * If the packet comes from our host, we only change
313103026Ssobomax			 * the destination address in the IP header.
314103026Ssobomax			 * Else we also need to save and change the source
315103026Ssobomax			 */
316103026Ssobomax			if (in_hosteq(ip->ip_src, sc->g_src)) {
317103026Ssobomax				msiz = MOB_H_SIZ_S;
318103026Ssobomax			} else {
319103026Ssobomax				mob_h.proto |= MOB_H_SBIT;
320103026Ssobomax				mob_h.osrc = ip->ip_src.s_addr;
321103026Ssobomax				ip->ip_src.s_addr = sc->g_src.s_addr;
322103026Ssobomax				msiz = MOB_H_SIZ_L;
323103026Ssobomax			}
324103026Ssobomax			mob_h.proto = htons(mob_h.proto);
325123992Ssobomax			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
326103026Ssobomax
327103026Ssobomax			if ((m->m_data - msiz) < m->m_pktdat) {
328103026Ssobomax				/* need new mbuf */
329151967Sandre				MGETHDR(m0, M_DONTWAIT, MT_DATA);
330103026Ssobomax				if (m0 == NULL) {
331103026Ssobomax					_IF_DROP(&ifp->if_snd);
332103026Ssobomax					m_freem(m);
333103026Ssobomax					error = ENOBUFS;
334103026Ssobomax					goto end;
335103026Ssobomax				}
336103026Ssobomax				m0->m_next = m;
337103026Ssobomax				m->m_data += sizeof(struct ip);
338103026Ssobomax				m->m_len -= sizeof(struct ip);
339103026Ssobomax				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
340103026Ssobomax				m0->m_len = msiz + sizeof(struct ip);
341103026Ssobomax				m0->m_data += max_linkhdr;
342103026Ssobomax				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
343103026Ssobomax				       sizeof(struct ip));
344103026Ssobomax				m = m0;
345103026Ssobomax			} else {  /* we have some space left in the old one */
346103026Ssobomax				m->m_data -= msiz;
347103026Ssobomax				m->m_len += msiz;
348103026Ssobomax				m->m_pkthdr.len += msiz;
349103026Ssobomax				bcopy(ip, mtod(m, caddr_t),
350103026Ssobomax					sizeof(struct ip));
351103026Ssobomax			}
352103026Ssobomax			ip = mtod(m, struct ip *);
353103026Ssobomax			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
354103026Ssobomax			ip->ip_len = ntohs(ip->ip_len) + msiz;
355103026Ssobomax		} else {  /* AF_INET */
356103026Ssobomax			_IF_DROP(&ifp->if_snd);
357103026Ssobomax			m_freem(m);
358103026Ssobomax			error = EINVAL;
359103026Ssobomax			goto end;
360103026Ssobomax		}
361103026Ssobomax	} else if (sc->g_proto == IPPROTO_GRE) {
362103026Ssobomax		switch (dst->sa_family) {
363103026Ssobomax		case AF_INET:
364103026Ssobomax			ip = mtod(m, struct ip *);
365148613Sbz			ip_tos = ip->ip_tos;
366148613Sbz			ip_id = ip->ip_id;
367103026Ssobomax			etype = ETHERTYPE_IP;
368103026Ssobomax			break;
369148613Sbz#ifdef INET6
370148613Sbz		case AF_INET6:
371148613Sbz			ip_id = ip_newid();
372148613Sbz			etype = ETHERTYPE_IPV6;
373148613Sbz			break;
374148613Sbz#endif
375103026Ssobomax#ifdef NETATALK
376103026Ssobomax		case AF_APPLETALK:
377103026Ssobomax			etype = ETHERTYPE_ATALK;
378103026Ssobomax			break;
379103026Ssobomax#endif
380103026Ssobomax		default:
381103026Ssobomax			_IF_DROP(&ifp->if_snd);
382103026Ssobomax			m_freem(m);
383103026Ssobomax			error = EAFNOSUPPORT;
384103026Ssobomax			goto end;
385103026Ssobomax		}
386111119Simp		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
387103026Ssobomax	} else {
388103026Ssobomax		_IF_DROP(&ifp->if_snd);
389103026Ssobomax		m_freem(m);
390103026Ssobomax		error = EINVAL;
391103026Ssobomax		goto end;
392103026Ssobomax	}
393103026Ssobomax
394128580Sandre	if (m == NULL) {	/* mbuf allocation failed */
395103026Ssobomax		_IF_DROP(&ifp->if_snd);
396103026Ssobomax		error = ENOBUFS;
397103026Ssobomax		goto end;
398103026Ssobomax	}
399103026Ssobomax
400178888Sjulian	M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
401178888Sjulian
402103026Ssobomax	gh = mtod(m, struct greip *);
403103026Ssobomax	if (sc->g_proto == IPPROTO_GRE) {
404103026Ssobomax		/* we don't have any GRE flags for now */
405125226Ssobomax		memset((void *)gh, 0, sizeof(struct greip));
406103026Ssobomax		gh->gi_ptype = htons(etype);
407103026Ssobomax	}
408103026Ssobomax
409103026Ssobomax	gh->gi_pr = sc->g_proto;
410103026Ssobomax	if (sc->g_proto != IPPROTO_MOBILE) {
411103026Ssobomax		gh->gi_src = sc->g_src;
412103026Ssobomax		gh->gi_dst = sc->g_dst;
413133163Ssobomax		((struct ip*)gh)->ip_v = IPPROTO_IPV4;
414103026Ssobomax		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
415103032Ssobomax		((struct ip*)gh)->ip_ttl = GRE_TTL;
416148613Sbz		((struct ip*)gh)->ip_tos = ip_tos;
417148613Sbz		((struct ip*)gh)->ip_id = ip_id;
418125226Ssobomax		gh->gi_len = m->m_pkthdr.len;
419103026Ssobomax	}
420103026Ssobomax
421103026Ssobomax	ifp->if_opackets++;
422103026Ssobomax	ifp->if_obytes += m->m_pkthdr.len;
423128583Sandre	/*
424128583Sandre	 * Send it off and with IP_FORWARD flag to prevent it from
425128583Sandre	 * overwriting the ip_id again.  ip_id is already set to the
426128583Sandre	 * ip_id of the encapsulated packet.
427128583Sandre	 */
428128580Sandre	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
429123992Ssobomax	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
430103026Ssobomax  end:
431103026Ssobomax	sc->called = 0;
432103026Ssobomax	if (error)
433103026Ssobomax		ifp->if_oerrors++;
434103026Ssobomax	return (error);
435103026Ssobomax}
436103026Ssobomax
437103032Ssobomaxstatic int
438103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
439103026Ssobomax{
440103026Ssobomax	struct ifreq *ifr = (struct ifreq *)data;
441103026Ssobomax	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
442103026Ssobomax	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
443103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
444103026Ssobomax	int s;
445103026Ssobomax	struct sockaddr_in si;
446103026Ssobomax	struct sockaddr *sa = NULL;
447103026Ssobomax	int error;
448103026Ssobomax	struct sockaddr_in sp, sm, dp, dm;
449103026Ssobomax
450103026Ssobomax	error = 0;
451103026Ssobomax
452103026Ssobomax	s = splnet();
453103026Ssobomax	switch (cmd) {
454103026Ssobomax	case SIOCSIFADDR:
455103026Ssobomax		ifp->if_flags |= IFF_UP;
456103026Ssobomax		break;
457125020Ssobomax	case SIOCSIFDSTADDR:
458103026Ssobomax		break;
459103026Ssobomax	case SIOCSIFFLAGS:
460164033Srwatson		/*
461171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
462171056Srwatson		 * layer check?
463164033Srwatson		 */
464164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
465103026Ssobomax			break;
466103026Ssobomax		if ((ifr->ifr_flags & IFF_LINK0) != 0)
467103026Ssobomax			sc->g_proto = IPPROTO_GRE;
468103026Ssobomax		else
469103026Ssobomax			sc->g_proto = IPPROTO_MOBILE;
470125024Ssobomax		if ((ifr->ifr_flags & IFF_LINK2) != 0)
471125024Ssobomax			sc->wccp_ver = WCCP_V2;
472125024Ssobomax		else
473125024Ssobomax			sc->wccp_ver = WCCP_V1;
474103026Ssobomax		goto recompute;
475103026Ssobomax	case SIOCSIFMTU:
476164033Srwatson		/*
477171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
478171056Srwatson		 * layer check?
479164033Srwatson		 */
480164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
481103026Ssobomax			break;
482103026Ssobomax		if (ifr->ifr_mtu < 576) {
483103026Ssobomax			error = EINVAL;
484103026Ssobomax			break;
485103026Ssobomax		}
486103026Ssobomax		ifp->if_mtu = ifr->ifr_mtu;
487103026Ssobomax		break;
488103026Ssobomax	case SIOCGIFMTU:
489147256Sbrooks		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
490103026Ssobomax		break;
491103026Ssobomax	case SIOCADDMULTI:
492164033Srwatson		/*
493171056Srwatson		 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
494171056Srwatson		 * layer check?
495164033Srwatson		 */
496164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
497164033Srwatson			break;
498164033Srwatson		if (ifr == 0) {
499164033Srwatson			error = EAFNOSUPPORT;
500164033Srwatson			break;
501164033Srwatson		}
502164033Srwatson		switch (ifr->ifr_addr.sa_family) {
503164033Srwatson#ifdef INET
504164033Srwatson		case AF_INET:
505164033Srwatson			break;
506164033Srwatson#endif
507164033Srwatson#ifdef INET6
508164033Srwatson		case AF_INET6:
509164033Srwatson			break;
510164033Srwatson#endif
511164033Srwatson		default:
512164033Srwatson			error = EAFNOSUPPORT;
513164033Srwatson			break;
514164033Srwatson		}
515164033Srwatson		break;
516103026Ssobomax	case SIOCDELMULTI:
517164033Srwatson		/*
518171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
519171056Srwatson		 * layer check?
520164033Srwatson		 */
521164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
522103026Ssobomax			break;
523103026Ssobomax		if (ifr == 0) {
524103026Ssobomax			error = EAFNOSUPPORT;
525103026Ssobomax			break;
526103026Ssobomax		}
527103026Ssobomax		switch (ifr->ifr_addr.sa_family) {
528103026Ssobomax#ifdef INET
529103026Ssobomax		case AF_INET:
530103026Ssobomax			break;
531103026Ssobomax#endif
532148613Sbz#ifdef INET6
533148613Sbz		case AF_INET6:
534148613Sbz			break;
535148613Sbz#endif
536103026Ssobomax		default:
537103026Ssobomax			error = EAFNOSUPPORT;
538103026Ssobomax			break;
539103026Ssobomax		}
540103026Ssobomax		break;
541103026Ssobomax	case GRESPROTO:
542164033Srwatson		/*
543171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
544171056Srwatson		 * layer check?
545164033Srwatson		 */
546164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
547103026Ssobomax			break;
548103026Ssobomax		sc->g_proto = ifr->ifr_flags;
549103026Ssobomax		switch (sc->g_proto) {
550103026Ssobomax		case IPPROTO_GRE:
551103026Ssobomax			ifp->if_flags |= IFF_LINK0;
552103026Ssobomax			break;
553103026Ssobomax		case IPPROTO_MOBILE:
554103026Ssobomax			ifp->if_flags &= ~IFF_LINK0;
555103026Ssobomax			break;
556103026Ssobomax		default:
557103026Ssobomax			error = EPROTONOSUPPORT;
558103026Ssobomax			break;
559103026Ssobomax		}
560103026Ssobomax		goto recompute;
561103026Ssobomax	case GREGPROTO:
562103026Ssobomax		ifr->ifr_flags = sc->g_proto;
563103026Ssobomax		break;
564103026Ssobomax	case GRESADDRS:
565103026Ssobomax	case GRESADDRD:
566164033Srwatson		error = priv_check(curthread, PRIV_NET_GRE);
567164033Srwatson		if (error)
568164033Srwatson			return (error);
569103026Ssobomax		/*
570103026Ssobomax		 * set tunnel endpoints, compute a less specific route
571103026Ssobomax		 * to the remote end and mark if as up
572103026Ssobomax		 */
573103026Ssobomax		sa = &ifr->ifr_addr;
574103026Ssobomax		if (cmd == GRESADDRS)
575103026Ssobomax			sc->g_src = (satosin(sa))->sin_addr;
576103026Ssobomax		if (cmd == GRESADDRD)
577103026Ssobomax			sc->g_dst = (satosin(sa))->sin_addr;
578103026Ssobomax	recompute:
579103026Ssobomax#ifdef INET
580103026Ssobomax		if (sc->encap != NULL) {
581103026Ssobomax			encap_detach(sc->encap);
582103026Ssobomax			sc->encap = NULL;
583103026Ssobomax		}
584103026Ssobomax#endif
585103026Ssobomax		if ((sc->g_src.s_addr != INADDR_ANY) &&
586103026Ssobomax		    (sc->g_dst.s_addr != INADDR_ANY)) {
587103026Ssobomax			bzero(&sp, sizeof(sp));
588103026Ssobomax			bzero(&sm, sizeof(sm));
589103026Ssobomax			bzero(&dp, sizeof(dp));
590103026Ssobomax			bzero(&dm, sizeof(dm));
591103026Ssobomax			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
592103026Ssobomax			    sizeof(struct sockaddr_in);
593103026Ssobomax			sp.sin_family = sm.sin_family = dp.sin_family =
594103026Ssobomax			    dm.sin_family = AF_INET;
595103026Ssobomax			sp.sin_addr = sc->g_src;
596103026Ssobomax			dp.sin_addr = sc->g_dst;
597125020Ssobomax			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
598103026Ssobomax			    INADDR_BROADCAST;
599103026Ssobomax#ifdef INET
600103026Ssobomax			sc->encap = encap_attach(AF_INET, sc->g_proto,
601103026Ssobomax			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
602103026Ssobomax			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
603103026Ssobomax				&in_gre_protosw : &in_mobile_protosw, sc);
604103026Ssobomax			if (sc->encap == NULL)
605103026Ssobomax				printf("%s: unable to attach encap\n",
606147256Sbrooks				    if_name(GRE2IFP(sc)));
607103026Ssobomax#endif
608103026Ssobomax			if (sc->route.ro_rt != 0) /* free old route */
609103026Ssobomax				RTFREE(sc->route.ro_rt);
610103026Ssobomax			if (gre_compute_route(sc) == 0)
611148887Srwatson				ifp->if_drv_flags |= IFF_DRV_RUNNING;
612103026Ssobomax			else
613148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
614103026Ssobomax		}
615103026Ssobomax		break;
616103026Ssobomax	case GREGADDRS:
617103026Ssobomax		memset(&si, 0, sizeof(si));
618103026Ssobomax		si.sin_family = AF_INET;
619103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
620103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
621103026Ssobomax		sa = sintosa(&si);
622103026Ssobomax		ifr->ifr_addr = *sa;
623103026Ssobomax		break;
624103026Ssobomax	case GREGADDRD:
625103026Ssobomax		memset(&si, 0, sizeof(si));
626103026Ssobomax		si.sin_family = AF_INET;
627103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
628103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
629103026Ssobomax		sa = sintosa(&si);
630103026Ssobomax		ifr->ifr_addr = *sa;
631103026Ssobomax		break;
632103026Ssobomax	case SIOCSIFPHYADDR:
633164033Srwatson		/*
634171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
635171056Srwatson		 * layer check?
636164033Srwatson		 */
637164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
638103026Ssobomax			break;
639103026Ssobomax		if (aifr->ifra_addr.sin_family != AF_INET ||
640103026Ssobomax		    aifr->ifra_dstaddr.sin_family != AF_INET) {
641103026Ssobomax			error = EAFNOSUPPORT;
642103026Ssobomax			break;
643103026Ssobomax		}
644103026Ssobomax		if (aifr->ifra_addr.sin_len != sizeof(si) ||
645103026Ssobomax		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
646103026Ssobomax			error = EINVAL;
647103026Ssobomax			break;
648103026Ssobomax		}
649103026Ssobomax		sc->g_src = aifr->ifra_addr.sin_addr;
650103026Ssobomax		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
651103026Ssobomax		goto recompute;
652103026Ssobomax	case SIOCSLIFPHYADDR:
653164033Srwatson		/*
654171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
655171056Srwatson		 * layer check?
656164033Srwatson		 */
657164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
658103026Ssobomax			break;
659103026Ssobomax		if (lifr->addr.ss_family != AF_INET ||
660103026Ssobomax		    lifr->dstaddr.ss_family != AF_INET) {
661103026Ssobomax			error = EAFNOSUPPORT;
662103026Ssobomax			break;
663103026Ssobomax		}
664103026Ssobomax		if (lifr->addr.ss_len != sizeof(si) ||
665103026Ssobomax		    lifr->dstaddr.ss_len != sizeof(si)) {
666103026Ssobomax			error = EINVAL;
667103026Ssobomax			break;
668103026Ssobomax		}
669155440Sqingli		sc->g_src = (satosin(&lifr->addr))->sin_addr;
670103026Ssobomax		sc->g_dst =
671155440Sqingli		    (satosin(&lifr->dstaddr))->sin_addr;
672103026Ssobomax		goto recompute;
673103026Ssobomax	case SIOCDIFPHYADDR:
674164033Srwatson		/*
675171056Srwatson		 * XXXRW: Isn't this priv_check() redundant to the ifnet
676171056Srwatson		 * layer check?
677164033Srwatson		 */
678164033Srwatson		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
679103026Ssobomax			break;
680103026Ssobomax		sc->g_src.s_addr = INADDR_ANY;
681103026Ssobomax		sc->g_dst.s_addr = INADDR_ANY;
682103026Ssobomax		goto recompute;
683103026Ssobomax	case SIOCGLIFPHYADDR:
684103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY ||
685103026Ssobomax		    sc->g_dst.s_addr == INADDR_ANY) {
686103026Ssobomax			error = EADDRNOTAVAIL;
687103026Ssobomax			break;
688103026Ssobomax		}
689103026Ssobomax		memset(&si, 0, sizeof(si));
690103026Ssobomax		si.sin_family = AF_INET;
691103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
692103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
693103026Ssobomax		memcpy(&lifr->addr, &si, sizeof(si));
694103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
695103026Ssobomax		memcpy(&lifr->dstaddr, &si, sizeof(si));
696103026Ssobomax		break;
697103026Ssobomax	case SIOCGIFPSRCADDR:
698122699Sbms#ifdef INET6
699122699Sbms	case SIOCGIFPSRCADDR_IN6:
700122699Sbms#endif
701103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY) {
702103026Ssobomax			error = EADDRNOTAVAIL;
703103026Ssobomax			break;
704103026Ssobomax		}
705103026Ssobomax		memset(&si, 0, sizeof(si));
706103026Ssobomax		si.sin_family = AF_INET;
707103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
708103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
709103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
710103026Ssobomax		break;
711103026Ssobomax	case SIOCGIFPDSTADDR:
712122699Sbms#ifdef INET6
713122699Sbms	case SIOCGIFPDSTADDR_IN6:
714122699Sbms#endif
715103026Ssobomax		if (sc->g_dst.s_addr == INADDR_ANY) {
716103026Ssobomax			error = EADDRNOTAVAIL;
717103026Ssobomax			break;
718103026Ssobomax		}
719103026Ssobomax		memset(&si, 0, sizeof(si));
720103026Ssobomax		si.sin_family = AF_INET;
721103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
722103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
723103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
724103026Ssobomax		break;
725103026Ssobomax	default:
726103026Ssobomax		error = EINVAL;
727103026Ssobomax		break;
728103026Ssobomax	}
729103026Ssobomax
730103026Ssobomax	splx(s);
731103026Ssobomax	return (error);
732103026Ssobomax}
733103026Ssobomax
734103026Ssobomax/*
735103026Ssobomax * computes a route to our destination that is not the one
736103026Ssobomax * which would be taken by ip_output(), as this one will loop back to
737103026Ssobomax * us. If the interface is p2p as  a--->b, then a routing entry exists
738103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here
739123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to
740103026Ssobomax * if_gre.
741103026Ssobomax * Goal here is to compute a route to b that is less specific than
742103026Ssobomax * a-->b. We know that this one exists as in normal operation we have
743103026Ssobomax * at least a default route which matches.
744103026Ssobomax */
745103032Ssobomaxstatic int
746103026Ssobomaxgre_compute_route(struct gre_softc *sc)
747103026Ssobomax{
748103026Ssobomax	struct route *ro;
749103026Ssobomax
750103026Ssobomax	ro = &sc->route;
751103026Ssobomax
752103026Ssobomax	memset(ro, 0, sizeof(struct route));
753103026Ssobomax	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
754103026Ssobomax	ro->ro_dst.sa_family = AF_INET;
755103026Ssobomax	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
756103026Ssobomax
757103026Ssobomax	/*
758103026Ssobomax	 * toggle last bit, so our interface is not found, but a less
759103026Ssobomax	 * specific route. I'd rather like to specify a shorter mask,
760103026Ssobomax	 * but this is not possible. Should work though. XXX
761178888Sjulian	 * XXX MRT Use a different FIB for the tunnel to solve this problem.
762103026Ssobomax	 */
763147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
764177416Sjulian		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
765177416Sjulian		    htonl(0x01);
766103026Ssobomax	}
767103026Ssobomax
768103026Ssobomax#ifdef DIAGNOSTIC
769147256Sbrooks	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
770103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
771103026Ssobomax#endif
772103026Ssobomax
773178888Sjulian	rtalloc_fib(ro, sc->gre_fibnum);
774103026Ssobomax
775103026Ssobomax	/*
776103026Ssobomax	 * check if this returned a route at all and this route is no
777103026Ssobomax	 * recursion to ourself
778103026Ssobomax	 */
779103026Ssobomax	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
780103026Ssobomax#ifdef DIAGNOSTIC
781103026Ssobomax		if (ro->ro_rt == NULL)
782103026Ssobomax			printf(" - no route found!\n");
783103026Ssobomax		else
784103026Ssobomax			printf(" - route loops back to ourself!\n");
785103026Ssobomax#endif
786103026Ssobomax		return EADDRNOTAVAIL;
787103026Ssobomax	}
788103026Ssobomax
789103026Ssobomax	/*
790103026Ssobomax	 * now change it back - else ip_output will just drop
791103026Ssobomax	 * the route and search one to this interface ...
792103026Ssobomax	 */
793147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
794103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
795103026Ssobomax
796103026Ssobomax#ifdef DIAGNOSTIC
797103026Ssobomax	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
798103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
799103026Ssobomax	printf("\n");
800103026Ssobomax#endif
801103026Ssobomax
802103026Ssobomax	return 0;
803103026Ssobomax}
804103026Ssobomax
805103026Ssobomax/*
806103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on
807103026Ssobomax * mbufs.
808103026Ssobomax */
809123992Ssobomaxu_int16_t
810123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len)
811103026Ssobomax{
812123992Ssobomax	u_int32_t sum = 0;
813103026Ssobomax	int nwords = len >> 1;
814103026Ssobomax
815103026Ssobomax	while (nwords-- != 0)
816103026Ssobomax		sum += *p++;
817103026Ssobomax
818103026Ssobomax	if (len & 1) {
819103026Ssobomax		union {
820103026Ssobomax			u_short w;
821103026Ssobomax			u_char c[2];
822103026Ssobomax		} u;
823103026Ssobomax		u.c[0] = *(u_char *)p;
824103026Ssobomax		u.c[1] = 0;
825103026Ssobomax		sum += u.w;
826103026Ssobomax	}
827103026Ssobomax
828103026Ssobomax	/* end-around-carry */
829103026Ssobomax	sum = (sum >> 16) + (sum & 0xffff);
830103026Ssobomax	sum += (sum >> 16);
831103026Ssobomax	return (~sum);
832103026Ssobomax}
833103026Ssobomax
834103026Ssobomaxstatic int
835103026Ssobomaxgremodevent(module_t mod, int type, void *data)
836103026Ssobomax{
837103026Ssobomax
838103026Ssobomax	switch (type) {
839103026Ssobomax	case MOD_LOAD:
840103026Ssobomax		greattach();
841103026Ssobomax		break;
842103026Ssobomax	case MOD_UNLOAD:
843103026Ssobomax		if_clone_detach(&gre_cloner);
844127307Srwatson		mtx_destroy(&gre_mtx);
845103026Ssobomax		break;
846132199Sphk	default:
847132199Sphk		return EOPNOTSUPP;
848103026Ssobomax	}
849103026Ssobomax	return 0;
850103026Ssobomax}
851103026Ssobomax
852103026Ssobomaxstatic moduledata_t gre_mod = {
853103026Ssobomax	"if_gre",
854103026Ssobomax	gremodevent,
855103026Ssobomax	0
856103026Ssobomax};
857103026Ssobomax
858103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
859103026SsobomaxMODULE_VERSION(if_gre, 1);
860