if_gre.c revision 151967
1123992Ssobomax/*	$NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2103026Ssobomax/*	 $FreeBSD: head/sys/net/if_gre.c 151967 2005-11-02 13:46:32Z andre $ */
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_clone_destroy(ifp)
206127307Srwatson	struct ifnet *ifp;
207127307Srwatson{
208127307Srwatson	struct gre_softc *sc = ifp->if_softc;
209127307Srwatson
210127307Srwatson	mtx_lock(&gre_mtx);
211127307Srwatson	LIST_REMOVE(sc, sc_list);
212127307Srwatson	mtx_unlock(&gre_mtx);
213151266Sthompsa
214151266Sthompsa#ifdef INET
215151266Sthompsa	if (sc->encap != NULL)
216151266Sthompsa		encap_detach(sc->encap);
217151266Sthompsa#endif
218151266Sthompsa	bpfdetach(ifp);
219151266Sthompsa	if_detach(ifp);
220151266Sthompsa	if_free(ifp);
221151266Sthompsa	free(sc, M_GRE);
222127307Srwatson}
223127307Srwatson
224103026Ssobomax/*
225103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol
226103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004
227103026Ssobomax */
228103032Ssobomaxstatic int
229103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
230103026Ssobomax	   struct rtentry *rt)
231103026Ssobomax{
232103026Ssobomax	int error = 0;
233103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
234103026Ssobomax	struct greip *gh;
235103026Ssobomax	struct ip *ip;
236148613Sbz	u_short ip_id = 0;
237148613Sbz	uint8_t ip_tos = 0;
238123992Ssobomax	u_int16_t etype = 0;
239103026Ssobomax	struct mobile_h mob_h;
240147611Sdwmalone	u_int32_t af;
241103026Ssobomax
242103026Ssobomax	/*
243103026Ssobomax	 * gre may cause infinite recursion calls when misconfigured.
244103026Ssobomax	 * We'll prevent this by introducing upper limit.
245103026Ssobomax	 */
246103026Ssobomax	if (++(sc->called) > max_gre_nesting) {
247103026Ssobomax		printf("%s: gre_output: recursively called too many "
248147256Sbrooks		       "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
249103026Ssobomax		m_freem(m);
250103026Ssobomax		error = EIO;    /* is there better errno? */
251103026Ssobomax		goto end;
252103026Ssobomax	}
253103026Ssobomax
254148887Srwatson	if (!((ifp->if_flags & IFF_UP) &&
255148887Srwatson	    (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
256103026Ssobomax	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
257103026Ssobomax		m_freem(m);
258103026Ssobomax		error = ENETDOWN;
259103026Ssobomax		goto end;
260103026Ssobomax	}
261103026Ssobomax
262103026Ssobomax	gh = NULL;
263103026Ssobomax	ip = NULL;
264103026Ssobomax
265147611Sdwmalone	/* BPF writes need to be handled specially. */
266147611Sdwmalone	if (dst->sa_family == AF_UNSPEC) {
267147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
268147611Sdwmalone		dst->sa_family = af;
269147611Sdwmalone	}
270147611Sdwmalone
271103026Ssobomax	if (ifp->if_bpf) {
272147611Sdwmalone		af = dst->sa_family;
273123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
274103026Ssobomax	}
275103026Ssobomax
276103026Ssobomax	m->m_flags &= ~(M_BCAST|M_MCAST);
277103026Ssobomax
278103026Ssobomax	if (sc->g_proto == IPPROTO_MOBILE) {
279103026Ssobomax		if (dst->sa_family == AF_INET) {
280103026Ssobomax			struct mbuf *m0;
281103026Ssobomax			int msiz;
282103026Ssobomax
283103026Ssobomax			ip = mtod(m, struct ip *);
284103026Ssobomax
285103026Ssobomax			/*
286103026Ssobomax			 * RFC2004 specifies that fragmented diagrams shouldn't
287103026Ssobomax			 * be encapsulated.
288103026Ssobomax			 */
289103026Ssobomax			if ((ip->ip_off & IP_MF) != 0) {
290103026Ssobomax				_IF_DROP(&ifp->if_snd);
291103026Ssobomax				m_freem(m);
292103026Ssobomax				error = EINVAL;    /* is there better errno? */
293103026Ssobomax				goto end;
294103026Ssobomax			}
295103026Ssobomax			memset(&mob_h, 0, MOB_H_SIZ_L);
296103026Ssobomax			mob_h.proto = (ip->ip_p) << 8;
297103026Ssobomax			mob_h.odst = ip->ip_dst.s_addr;
298103026Ssobomax			ip->ip_dst.s_addr = sc->g_dst.s_addr;
299103026Ssobomax
300103026Ssobomax			/*
301103026Ssobomax			 * If the packet comes from our host, we only change
302103026Ssobomax			 * the destination address in the IP header.
303103026Ssobomax			 * Else we also need to save and change the source
304103026Ssobomax			 */
305103026Ssobomax			if (in_hosteq(ip->ip_src, sc->g_src)) {
306103026Ssobomax				msiz = MOB_H_SIZ_S;
307103026Ssobomax			} else {
308103026Ssobomax				mob_h.proto |= MOB_H_SBIT;
309103026Ssobomax				mob_h.osrc = ip->ip_src.s_addr;
310103026Ssobomax				ip->ip_src.s_addr = sc->g_src.s_addr;
311103026Ssobomax				msiz = MOB_H_SIZ_L;
312103026Ssobomax			}
313103026Ssobomax			mob_h.proto = htons(mob_h.proto);
314123992Ssobomax			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
315103026Ssobomax
316103026Ssobomax			if ((m->m_data - msiz) < m->m_pktdat) {
317103026Ssobomax				/* need new mbuf */
318151967Sandre				MGETHDR(m0, M_DONTWAIT, MT_DATA);
319103026Ssobomax				if (m0 == NULL) {
320103026Ssobomax					_IF_DROP(&ifp->if_snd);
321103026Ssobomax					m_freem(m);
322103026Ssobomax					error = ENOBUFS;
323103026Ssobomax					goto end;
324103026Ssobomax				}
325103026Ssobomax				m0->m_next = m;
326103026Ssobomax				m->m_data += sizeof(struct ip);
327103026Ssobomax				m->m_len -= sizeof(struct ip);
328103026Ssobomax				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
329103026Ssobomax				m0->m_len = msiz + sizeof(struct ip);
330103026Ssobomax				m0->m_data += max_linkhdr;
331103026Ssobomax				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
332103026Ssobomax				       sizeof(struct ip));
333103026Ssobomax				m = m0;
334103026Ssobomax			} else {  /* we have some space left in the old one */
335103026Ssobomax				m->m_data -= msiz;
336103026Ssobomax				m->m_len += msiz;
337103026Ssobomax				m->m_pkthdr.len += msiz;
338103026Ssobomax				bcopy(ip, mtod(m, caddr_t),
339103026Ssobomax					sizeof(struct ip));
340103026Ssobomax			}
341103026Ssobomax			ip = mtod(m, struct ip *);
342103026Ssobomax			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
343103026Ssobomax			ip->ip_len = ntohs(ip->ip_len) + msiz;
344103026Ssobomax		} else {  /* AF_INET */
345103026Ssobomax			_IF_DROP(&ifp->if_snd);
346103026Ssobomax			m_freem(m);
347103026Ssobomax			error = EINVAL;
348103026Ssobomax			goto end;
349103026Ssobomax		}
350103026Ssobomax	} else if (sc->g_proto == IPPROTO_GRE) {
351103026Ssobomax		switch (dst->sa_family) {
352103026Ssobomax		case AF_INET:
353103026Ssobomax			ip = mtod(m, struct ip *);
354148613Sbz			ip_tos = ip->ip_tos;
355148613Sbz			ip_id = ip->ip_id;
356103026Ssobomax			etype = ETHERTYPE_IP;
357103026Ssobomax			break;
358148613Sbz#ifdef INET6
359148613Sbz		case AF_INET6:
360148613Sbz			ip_id = ip_newid();
361148613Sbz			etype = ETHERTYPE_IPV6;
362148613Sbz			break;
363148613Sbz#endif
364103026Ssobomax#ifdef NETATALK
365103026Ssobomax		case AF_APPLETALK:
366103026Ssobomax			etype = ETHERTYPE_ATALK;
367103026Ssobomax			break;
368103026Ssobomax#endif
369103026Ssobomax		default:
370103026Ssobomax			_IF_DROP(&ifp->if_snd);
371103026Ssobomax			m_freem(m);
372103026Ssobomax			error = EAFNOSUPPORT;
373103026Ssobomax			goto end;
374103026Ssobomax		}
375111119Simp		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
376103026Ssobomax	} else {
377103026Ssobomax		_IF_DROP(&ifp->if_snd);
378103026Ssobomax		m_freem(m);
379103026Ssobomax		error = EINVAL;
380103026Ssobomax		goto end;
381103026Ssobomax	}
382103026Ssobomax
383128580Sandre	if (m == NULL) {	/* mbuf allocation failed */
384103026Ssobomax		_IF_DROP(&ifp->if_snd);
385103026Ssobomax		error = ENOBUFS;
386103026Ssobomax		goto end;
387103026Ssobomax	}
388103026Ssobomax
389103026Ssobomax	gh = mtod(m, struct greip *);
390103026Ssobomax	if (sc->g_proto == IPPROTO_GRE) {
391103026Ssobomax		/* we don't have any GRE flags for now */
392125226Ssobomax		memset((void *)gh, 0, sizeof(struct greip));
393103026Ssobomax		gh->gi_ptype = htons(etype);
394103026Ssobomax	}
395103026Ssobomax
396103026Ssobomax	gh->gi_pr = sc->g_proto;
397103026Ssobomax	if (sc->g_proto != IPPROTO_MOBILE) {
398103026Ssobomax		gh->gi_src = sc->g_src;
399103026Ssobomax		gh->gi_dst = sc->g_dst;
400133163Ssobomax		((struct ip*)gh)->ip_v = IPPROTO_IPV4;
401103026Ssobomax		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
402103032Ssobomax		((struct ip*)gh)->ip_ttl = GRE_TTL;
403148613Sbz		((struct ip*)gh)->ip_tos = ip_tos;
404148613Sbz		((struct ip*)gh)->ip_id = ip_id;
405125226Ssobomax		gh->gi_len = m->m_pkthdr.len;
406103026Ssobomax	}
407103026Ssobomax
408103026Ssobomax	ifp->if_opackets++;
409103026Ssobomax	ifp->if_obytes += m->m_pkthdr.len;
410128583Sandre	/*
411128583Sandre	 * Send it off and with IP_FORWARD flag to prevent it from
412128583Sandre	 * overwriting the ip_id again.  ip_id is already set to the
413128583Sandre	 * ip_id of the encapsulated packet.
414128583Sandre	 */
415128580Sandre	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
416123992Ssobomax	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
417103026Ssobomax  end:
418103026Ssobomax	sc->called = 0;
419103026Ssobomax	if (error)
420103026Ssobomax		ifp->if_oerrors++;
421103026Ssobomax	return (error);
422103026Ssobomax}
423103026Ssobomax
424103032Ssobomaxstatic int
425103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
426103026Ssobomax{
427103026Ssobomax	struct ifreq *ifr = (struct ifreq *)data;
428103026Ssobomax	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
429103026Ssobomax	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
430103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
431103026Ssobomax	int s;
432103026Ssobomax	struct sockaddr_in si;
433103026Ssobomax	struct sockaddr *sa = NULL;
434103026Ssobomax	int error;
435103026Ssobomax	struct sockaddr_in sp, sm, dp, dm;
436103026Ssobomax
437103026Ssobomax	error = 0;
438103026Ssobomax
439103026Ssobomax	s = splnet();
440103026Ssobomax	switch (cmd) {
441103026Ssobomax	case SIOCSIFADDR:
442103026Ssobomax		ifp->if_flags |= IFF_UP;
443103026Ssobomax		break;
444125020Ssobomax	case SIOCSIFDSTADDR:
445103026Ssobomax		break;
446103026Ssobomax	case SIOCSIFFLAGS:
447103026Ssobomax		if ((error = suser(curthread)) != 0)
448103026Ssobomax			break;
449103026Ssobomax		if ((ifr->ifr_flags & IFF_LINK0) != 0)
450103026Ssobomax			sc->g_proto = IPPROTO_GRE;
451103026Ssobomax		else
452103026Ssobomax			sc->g_proto = IPPROTO_MOBILE;
453125024Ssobomax		if ((ifr->ifr_flags & IFF_LINK2) != 0)
454125024Ssobomax			sc->wccp_ver = WCCP_V2;
455125024Ssobomax		else
456125024Ssobomax			sc->wccp_ver = WCCP_V1;
457103026Ssobomax		goto recompute;
458103026Ssobomax	case SIOCSIFMTU:
459103026Ssobomax		if ((error = suser(curthread)) != 0)
460103026Ssobomax			break;
461103026Ssobomax		if (ifr->ifr_mtu < 576) {
462103026Ssobomax			error = EINVAL;
463103026Ssobomax			break;
464103026Ssobomax		}
465103026Ssobomax		ifp->if_mtu = ifr->ifr_mtu;
466103026Ssobomax		break;
467103026Ssobomax	case SIOCGIFMTU:
468147256Sbrooks		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
469103026Ssobomax		break;
470103026Ssobomax	case SIOCADDMULTI:
471103026Ssobomax	case SIOCDELMULTI:
472103026Ssobomax		if ((error = suser(curthread)) != 0)
473103026Ssobomax			break;
474103026Ssobomax		if (ifr == 0) {
475103026Ssobomax			error = EAFNOSUPPORT;
476103026Ssobomax			break;
477103026Ssobomax		}
478103026Ssobomax		switch (ifr->ifr_addr.sa_family) {
479103026Ssobomax#ifdef INET
480103026Ssobomax		case AF_INET:
481103026Ssobomax			break;
482103026Ssobomax#endif
483148613Sbz#ifdef INET6
484148613Sbz		case AF_INET6:
485148613Sbz			break;
486148613Sbz#endif
487103026Ssobomax		default:
488103026Ssobomax			error = EAFNOSUPPORT;
489103026Ssobomax			break;
490103026Ssobomax		}
491103026Ssobomax		break;
492103026Ssobomax	case GRESPROTO:
493103026Ssobomax		if ((error = suser(curthread)) != 0)
494103026Ssobomax			break;
495103026Ssobomax		sc->g_proto = ifr->ifr_flags;
496103026Ssobomax		switch (sc->g_proto) {
497103026Ssobomax		case IPPROTO_GRE:
498103026Ssobomax			ifp->if_flags |= IFF_LINK0;
499103026Ssobomax			break;
500103026Ssobomax		case IPPROTO_MOBILE:
501103026Ssobomax			ifp->if_flags &= ~IFF_LINK0;
502103026Ssobomax			break;
503103026Ssobomax		default:
504103026Ssobomax			error = EPROTONOSUPPORT;
505103026Ssobomax			break;
506103026Ssobomax		}
507103026Ssobomax		goto recompute;
508103026Ssobomax	case GREGPROTO:
509103026Ssobomax		ifr->ifr_flags = sc->g_proto;
510103026Ssobomax		break;
511103026Ssobomax	case GRESADDRS:
512103026Ssobomax	case GRESADDRD:
513103026Ssobomax		if ((error = suser(curthread)) != 0)
514103026Ssobomax			break;
515103026Ssobomax		/*
516103026Ssobomax		 * set tunnel endpoints, compute a less specific route
517103026Ssobomax		 * to the remote end and mark if as up
518103026Ssobomax		 */
519103026Ssobomax		sa = &ifr->ifr_addr;
520103026Ssobomax		if (cmd == GRESADDRS)
521103026Ssobomax			sc->g_src = (satosin(sa))->sin_addr;
522103026Ssobomax		if (cmd == GRESADDRD)
523103026Ssobomax			sc->g_dst = (satosin(sa))->sin_addr;
524103026Ssobomax	recompute:
525103026Ssobomax#ifdef INET
526103026Ssobomax		if (sc->encap != NULL) {
527103026Ssobomax			encap_detach(sc->encap);
528103026Ssobomax			sc->encap = NULL;
529103026Ssobomax		}
530103026Ssobomax#endif
531103026Ssobomax		if ((sc->g_src.s_addr != INADDR_ANY) &&
532103026Ssobomax		    (sc->g_dst.s_addr != INADDR_ANY)) {
533103026Ssobomax			bzero(&sp, sizeof(sp));
534103026Ssobomax			bzero(&sm, sizeof(sm));
535103026Ssobomax			bzero(&dp, sizeof(dp));
536103026Ssobomax			bzero(&dm, sizeof(dm));
537103026Ssobomax			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
538103026Ssobomax			    sizeof(struct sockaddr_in);
539103026Ssobomax			sp.sin_family = sm.sin_family = dp.sin_family =
540103026Ssobomax			    dm.sin_family = AF_INET;
541103026Ssobomax			sp.sin_addr = sc->g_src;
542103026Ssobomax			dp.sin_addr = sc->g_dst;
543125020Ssobomax			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
544103026Ssobomax			    INADDR_BROADCAST;
545103026Ssobomax#ifdef INET
546103026Ssobomax			sc->encap = encap_attach(AF_INET, sc->g_proto,
547103026Ssobomax			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
548103026Ssobomax			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
549103026Ssobomax				&in_gre_protosw : &in_mobile_protosw, sc);
550103026Ssobomax			if (sc->encap == NULL)
551103026Ssobomax				printf("%s: unable to attach encap\n",
552147256Sbrooks				    if_name(GRE2IFP(sc)));
553103026Ssobomax#endif
554103026Ssobomax			if (sc->route.ro_rt != 0) /* free old route */
555103026Ssobomax				RTFREE(sc->route.ro_rt);
556103026Ssobomax			if (gre_compute_route(sc) == 0)
557148887Srwatson				ifp->if_drv_flags |= IFF_DRV_RUNNING;
558103026Ssobomax			else
559148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
560103026Ssobomax		}
561103026Ssobomax		break;
562103026Ssobomax	case GREGADDRS:
563103026Ssobomax		memset(&si, 0, sizeof(si));
564103026Ssobomax		si.sin_family = AF_INET;
565103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
566103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
567103026Ssobomax		sa = sintosa(&si);
568103026Ssobomax		ifr->ifr_addr = *sa;
569103026Ssobomax		break;
570103026Ssobomax	case GREGADDRD:
571103026Ssobomax		memset(&si, 0, sizeof(si));
572103026Ssobomax		si.sin_family = AF_INET;
573103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
574103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
575103026Ssobomax		sa = sintosa(&si);
576103026Ssobomax		ifr->ifr_addr = *sa;
577103026Ssobomax		break;
578103026Ssobomax	case SIOCSIFPHYADDR:
579103026Ssobomax		if ((error = suser(curthread)) != 0)
580103026Ssobomax			break;
581103026Ssobomax		if (aifr->ifra_addr.sin_family != AF_INET ||
582103026Ssobomax		    aifr->ifra_dstaddr.sin_family != AF_INET) {
583103026Ssobomax			error = EAFNOSUPPORT;
584103026Ssobomax			break;
585103026Ssobomax		}
586103026Ssobomax		if (aifr->ifra_addr.sin_len != sizeof(si) ||
587103026Ssobomax		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
588103026Ssobomax			error = EINVAL;
589103026Ssobomax			break;
590103026Ssobomax		}
591103026Ssobomax		sc->g_src = aifr->ifra_addr.sin_addr;
592103026Ssobomax		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
593103026Ssobomax		goto recompute;
594103026Ssobomax	case SIOCSLIFPHYADDR:
595103026Ssobomax		if ((error = suser(curthread)) != 0)
596103026Ssobomax			break;
597103026Ssobomax		if (lifr->addr.ss_family != AF_INET ||
598103026Ssobomax		    lifr->dstaddr.ss_family != AF_INET) {
599103026Ssobomax			error = EAFNOSUPPORT;
600103026Ssobomax			break;
601103026Ssobomax		}
602103026Ssobomax		if (lifr->addr.ss_len != sizeof(si) ||
603103026Ssobomax		    lifr->dstaddr.ss_len != sizeof(si)) {
604103026Ssobomax			error = EINVAL;
605103026Ssobomax			break;
606103026Ssobomax		}
607103026Ssobomax		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
608103026Ssobomax		sc->g_dst =
609103026Ssobomax		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
610103026Ssobomax		goto recompute;
611103026Ssobomax	case SIOCDIFPHYADDR:
612103026Ssobomax		if ((error = suser(curthread)) != 0)
613103026Ssobomax			break;
614103026Ssobomax		sc->g_src.s_addr = INADDR_ANY;
615103026Ssobomax		sc->g_dst.s_addr = INADDR_ANY;
616103026Ssobomax		goto recompute;
617103026Ssobomax	case SIOCGLIFPHYADDR:
618103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY ||
619103026Ssobomax		    sc->g_dst.s_addr == INADDR_ANY) {
620103026Ssobomax			error = EADDRNOTAVAIL;
621103026Ssobomax			break;
622103026Ssobomax		}
623103026Ssobomax		memset(&si, 0, sizeof(si));
624103026Ssobomax		si.sin_family = AF_INET;
625103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
626103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
627103026Ssobomax		memcpy(&lifr->addr, &si, sizeof(si));
628103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
629103026Ssobomax		memcpy(&lifr->dstaddr, &si, sizeof(si));
630103026Ssobomax		break;
631103026Ssobomax	case SIOCGIFPSRCADDR:
632122699Sbms#ifdef INET6
633122699Sbms	case SIOCGIFPSRCADDR_IN6:
634122699Sbms#endif
635103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY) {
636103026Ssobomax			error = EADDRNOTAVAIL;
637103026Ssobomax			break;
638103026Ssobomax		}
639103026Ssobomax		memset(&si, 0, sizeof(si));
640103026Ssobomax		si.sin_family = AF_INET;
641103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
642103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
643103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
644103026Ssobomax		break;
645103026Ssobomax	case SIOCGIFPDSTADDR:
646122699Sbms#ifdef INET6
647122699Sbms	case SIOCGIFPDSTADDR_IN6:
648122699Sbms#endif
649103026Ssobomax		if (sc->g_dst.s_addr == INADDR_ANY) {
650103026Ssobomax			error = EADDRNOTAVAIL;
651103026Ssobomax			break;
652103026Ssobomax		}
653103026Ssobomax		memset(&si, 0, sizeof(si));
654103026Ssobomax		si.sin_family = AF_INET;
655103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
656103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
657103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
658103026Ssobomax		break;
659103026Ssobomax	default:
660103026Ssobomax		error = EINVAL;
661103026Ssobomax		break;
662103026Ssobomax	}
663103026Ssobomax
664103026Ssobomax	splx(s);
665103026Ssobomax	return (error);
666103026Ssobomax}
667103026Ssobomax
668103026Ssobomax/*
669103026Ssobomax * computes a route to our destination that is not the one
670103026Ssobomax * which would be taken by ip_output(), as this one will loop back to
671103026Ssobomax * us. If the interface is p2p as  a--->b, then a routing entry exists
672103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here
673123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to
674103026Ssobomax * if_gre.
675103026Ssobomax * Goal here is to compute a route to b that is less specific than
676103026Ssobomax * a-->b. We know that this one exists as in normal operation we have
677103026Ssobomax * at least a default route which matches.
678103026Ssobomax */
679103032Ssobomaxstatic int
680103026Ssobomaxgre_compute_route(struct gre_softc *sc)
681103026Ssobomax{
682103026Ssobomax	struct route *ro;
683103026Ssobomax	u_int32_t a, b, c;
684103026Ssobomax
685103026Ssobomax	ro = &sc->route;
686103026Ssobomax
687103026Ssobomax	memset(ro, 0, sizeof(struct route));
688103026Ssobomax	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
689103026Ssobomax	ro->ro_dst.sa_family = AF_INET;
690103026Ssobomax	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
691103026Ssobomax
692103026Ssobomax	/*
693103026Ssobomax	 * toggle last bit, so our interface is not found, but a less
694103026Ssobomax	 * specific route. I'd rather like to specify a shorter mask,
695103026Ssobomax	 * but this is not possible. Should work though. XXX
696103026Ssobomax	 * there is a simpler way ...
697103026Ssobomax	 */
698147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
699103026Ssobomax		a = ntohl(sc->g_dst.s_addr);
700103026Ssobomax		b = a & 0x01;
701103026Ssobomax		c = a & 0xfffffffe;
702103026Ssobomax		b = b ^ 0x01;
703103026Ssobomax		a = b | c;
704103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
705103026Ssobomax		    = htonl(a);
706103026Ssobomax	}
707103026Ssobomax
708103026Ssobomax#ifdef DIAGNOSTIC
709147256Sbrooks	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
710103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
711103026Ssobomax#endif
712103026Ssobomax
713103026Ssobomax	rtalloc(ro);
714103026Ssobomax
715103026Ssobomax	/*
716103026Ssobomax	 * check if this returned a route at all and this route is no
717103026Ssobomax	 * recursion to ourself
718103026Ssobomax	 */
719103026Ssobomax	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
720103026Ssobomax#ifdef DIAGNOSTIC
721103026Ssobomax		if (ro->ro_rt == NULL)
722103026Ssobomax			printf(" - no route found!\n");
723103026Ssobomax		else
724103026Ssobomax			printf(" - route loops back to ourself!\n");
725103026Ssobomax#endif
726103026Ssobomax		return EADDRNOTAVAIL;
727103026Ssobomax	}
728103026Ssobomax
729103026Ssobomax	/*
730103026Ssobomax	 * now change it back - else ip_output will just drop
731103026Ssobomax	 * the route and search one to this interface ...
732103026Ssobomax	 */
733147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
734103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
735103026Ssobomax
736103026Ssobomax#ifdef DIAGNOSTIC
737103026Ssobomax	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
738103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
739103026Ssobomax	printf("\n");
740103026Ssobomax#endif
741103026Ssobomax
742103026Ssobomax	return 0;
743103026Ssobomax}
744103026Ssobomax
745103026Ssobomax/*
746103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on
747103026Ssobomax * mbufs.
748103026Ssobomax */
749123992Ssobomaxu_int16_t
750123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len)
751103026Ssobomax{
752123992Ssobomax	u_int32_t sum = 0;
753103026Ssobomax	int nwords = len >> 1;
754103026Ssobomax
755103026Ssobomax	while (nwords-- != 0)
756103026Ssobomax		sum += *p++;
757103026Ssobomax
758103026Ssobomax	if (len & 1) {
759103026Ssobomax		union {
760103026Ssobomax			u_short w;
761103026Ssobomax			u_char c[2];
762103026Ssobomax		} u;
763103026Ssobomax		u.c[0] = *(u_char *)p;
764103026Ssobomax		u.c[1] = 0;
765103026Ssobomax		sum += u.w;
766103026Ssobomax	}
767103026Ssobomax
768103026Ssobomax	/* end-around-carry */
769103026Ssobomax	sum = (sum >> 16) + (sum & 0xffff);
770103026Ssobomax	sum += (sum >> 16);
771103026Ssobomax	return (~sum);
772103026Ssobomax}
773103026Ssobomax
774103026Ssobomaxstatic int
775103026Ssobomaxgremodevent(module_t mod, int type, void *data)
776103026Ssobomax{
777127307Srwatson	struct gre_softc *sc;
778103026Ssobomax
779103026Ssobomax	switch (type) {
780103026Ssobomax	case MOD_LOAD:
781103026Ssobomax		greattach();
782103026Ssobomax		break;
783103026Ssobomax	case MOD_UNLOAD:
784103026Ssobomax		if_clone_detach(&gre_cloner);
785103026Ssobomax
786127307Srwatson		mtx_lock(&gre_mtx);
787127307Srwatson		while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) {
788127307Srwatson			mtx_unlock(&gre_mtx);
789151266Sthompsa			ifc_simple_destroy(&gre_cloner, GRE2IFP(sc));
790127307Srwatson			mtx_lock(&gre_mtx);
791127307Srwatson		}
792127307Srwatson		mtx_unlock(&gre_mtx);
793127307Srwatson		mtx_destroy(&gre_mtx);
794103026Ssobomax		break;
795132199Sphk	default:
796132199Sphk		return EOPNOTSUPP;
797103026Ssobomax	}
798103026Ssobomax	return 0;
799103026Ssobomax}
800103026Ssobomax
801103026Ssobomaxstatic moduledata_t gre_mod = {
802103026Ssobomax	"if_gre",
803103026Ssobomax	gremodevent,
804103026Ssobomax	0
805103026Ssobomax};
806103026Ssobomax
807103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
808103026SsobomaxMODULE_VERSION(if_gre, 1);
809