if_gre.c revision 128583
1123992Ssobomax/*	$NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2103026Ssobomax/*	 $FreeBSD: head/sys/net/if_gre.c 128583 2004-04-23 16:57:43Z andre $ */
3103026Ssobomax
4103026Ssobomax/*
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 *
11103026Ssobomax * Redistribution and use in source and binary forms, with or without
12103026Ssobomax * modification, are permitted provided that the following conditions
13103026Ssobomax * are met:
14103026Ssobomax * 1. Redistributions of source code must retain the above copyright
15103026Ssobomax *    notice, this list of conditions and the following disclaimer.
16103026Ssobomax * 2. Redistributions in binary form must reproduce the above copyright
17103026Ssobomax *    notice, this list of conditions and the following disclaimer in the
18103026Ssobomax *    documentation and/or other materials provided with the distribution.
19103026Ssobomax * 3. All advertising materials mentioning features or use of this software
20103026Ssobomax *    must display the following acknowledgement:
21103026Ssobomax *        This product includes software developed by the NetBSD
22103026Ssobomax *        Foundation, Inc. and its contributors.
23103026Ssobomax * 4. Neither the name of The NetBSD Foundation nor the names of its
24103026Ssobomax *    contributors may be used to endorse or promote products derived
25103026Ssobomax *    from this software without specific prior written permission.
26103026Ssobomax *
27103026Ssobomax * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28103026Ssobomax * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29103026Ssobomax * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30103026Ssobomax * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31103026Ssobomax * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32103026Ssobomax * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33103026Ssobomax * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34103026Ssobomax * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35103026Ssobomax * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36103026Ssobomax * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37103026Ssobomax * POSSIBILITY OF SUCH DAMAGE.
38103026Ssobomax */
39103026Ssobomax
40103026Ssobomax/*
41103026Ssobomax * Encapsulate L3 protocols into IP
42103026Ssobomax * See RFC 1701 and 1702 for more details.
43103026Ssobomax * If_gre is compatible with Cisco GRE tunnels, so you can
44103026Ssobomax * have a NetBSD box as the other end of a tunnel interface of a Cisco
45103026Ssobomax * router. See gre(4) for more details.
46103026Ssobomax * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
47103026Ssobomax */
48103026Ssobomax
49103394Sbde#include "opt_atalk.h"
50103026Ssobomax#include "opt_inet.h"
51122699Sbms#include "opt_inet6.h"
52103026Ssobomax
53103026Ssobomax#include <sys/param.h>
54103026Ssobomax#include <sys/kernel.h>
55103026Ssobomax#include <sys/malloc.h>
56103026Ssobomax#include <sys/mbuf.h>
57103026Ssobomax#include <sys/protosw.h>
58103026Ssobomax#include <sys/socket.h>
59103026Ssobomax#include <sys/sockio.h>
60103026Ssobomax#include <sys/sysctl.h>
61103344Sbde#include <sys/systm.h>
62103026Ssobomax
63103026Ssobomax#include <net/ethernet.h>
64103026Ssobomax#include <net/if.h>
65103026Ssobomax#include <net/if_types.h>
66103026Ssobomax#include <net/route.h>
67103026Ssobomax
68103026Ssobomax#ifdef INET
69103026Ssobomax#include <netinet/in.h>
70103026Ssobomax#include <netinet/in_systm.h>
71103026Ssobomax#include <netinet/in_var.h>
72103026Ssobomax#include <netinet/ip.h>
73103026Ssobomax#include <netinet/ip_gre.h>
74103026Ssobomax#include <netinet/ip_var.h>
75103026Ssobomax#include <netinet/ip_encap.h>
76103026Ssobomax#else
77103026Ssobomax#error "Huh? if_gre without inet?"
78103026Ssobomax#endif
79103026Ssobomax
80103026Ssobomax#include <net/bpf.h>
81103026Ssobomax
82103026Ssobomax#include <net/net_osdep.h>
83103026Ssobomax#include <net/if_gre.h>
84103026Ssobomax
85103026Ssobomax/*
86103026Ssobomax * It is not easy to calculate the right value for a GRE MTU.
87103026Ssobomax * We leave this task to the admin and use the same default that
88103026Ssobomax * other vendors use.
89103026Ssobomax */
90103026Ssobomax#define GREMTU	1476
91103026Ssobomax
92103026Ssobomax#define GRENAME	"gre"
93103026Ssobomax
94127307Srwatson/*
95127307Srwatson * gre_mtx protects all global variables in if_gre.c.
96127307Srwatson * XXX: gre_softc data not protected yet.
97127307Srwatson */
98127307Srwatsonstruct mtx gre_mtx;
99103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
100103026Ssobomax
101103026Ssobomaxstruct gre_softc_head gre_softc_list;
102103026Ssobomax
103105300Salfredstatic int	gre_clone_create(struct if_clone *, int);
104105300Salfredstatic void	gre_clone_destroy(struct ifnet *);
105103032Ssobomaxstatic int	gre_ioctl(struct ifnet *, u_long, caddr_t);
106103032Ssobomaxstatic int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
107103032Ssobomax		    struct rtentry *rt);
108103026Ssobomax
109103032Ssobomaxstatic struct if_clone gre_cloner =
110103026Ssobomax    IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy, 0, IF_MAXUNIT);
111103026Ssobomax
112103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc);
113103026Ssobomax
114105300Salfredstatic void	greattach(void);
115103026Ssobomax
116103026Ssobomax#ifdef INET
117103026Ssobomaxextern struct domain inetdomain;
118103026Ssobomaxstatic const struct protosw in_gre_protosw =
119103026Ssobomax{ SOCK_RAW,     &inetdomain,    IPPROTO_GRE,    PR_ATOMIC|PR_ADDR,
120103026Ssobomax  (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
121103026Ssobomax  0,
122103026Ssobomax  0,		0,		0,		0,
123103026Ssobomax  &rip_usrreqs
124103026Ssobomax};
125103026Ssobomaxstatic const struct protosw in_mobile_protosw =
126103026Ssobomax{ SOCK_RAW,     &inetdomain,    IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR,
127103026Ssobomax  (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
128103026Ssobomax  0,
129103026Ssobomax  0,		0,		0,		0,
130103026Ssobomax  &rip_usrreqs
131103026Ssobomax};
132103026Ssobomax#endif
133103026Ssobomax
134103026SsobomaxSYSCTL_DECL(_net_link);
135123338SbmsSYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
136103026Ssobomax    "Generic Routing Encapsulation");
137103026Ssobomax#ifndef MAX_GRE_NEST
138103026Ssobomax/*
139103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels.
140103026Ssobomax * Since, setting a large value to this macro with a careless configuration
141103026Ssobomax * may introduce system crash, we don't allow any nestings by default.
142103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro
143103026Ssobomax * in your kernel configuration file.  However, if you do so, please be
144103026Ssobomax * careful to configure the tunnels so that it won't make a loop.
145103026Ssobomax */
146103026Ssobomax#define MAX_GRE_NEST 1
147103026Ssobomax#endif
148103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST;
149103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
150103026Ssobomax    &max_gre_nesting, 0, "Max nested tunnels");
151103026Ssobomax
152103026Ssobomax/* ARGSUSED */
153103032Ssobomaxstatic void
154103026Ssobomaxgreattach(void)
155103026Ssobomax{
156103026Ssobomax
157127307Srwatson	mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
158103026Ssobomax	LIST_INIT(&gre_softc_list);
159103026Ssobomax	if_clone_attach(&gre_cloner);
160103026Ssobomax}
161103026Ssobomax
162103032Ssobomaxstatic int
163103026Ssobomaxgre_clone_create(ifc, unit)
164103026Ssobomax	struct if_clone *ifc;
165103026Ssobomax	int unit;
166103026Ssobomax{
167103026Ssobomax	struct gre_softc *sc;
168103026Ssobomax
169111119Simp	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK);
170103026Ssobomax	memset(sc, 0, sizeof(struct gre_softc));
171103026Ssobomax
172121816Sbrooks	if_initname(&sc->sc_if, ifc->ifc_name, unit);
173103026Ssobomax	sc->sc_if.if_softc = sc;
174103026Ssobomax	sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
175123338Sbms	sc->sc_if.if_type = IFT_TUNNEL;
176103026Ssobomax	sc->sc_if.if_addrlen = 0;
177103026Ssobomax	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
178103026Ssobomax	sc->sc_if.if_mtu = GREMTU;
179103026Ssobomax	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
180103026Ssobomax	sc->sc_if.if_output = gre_output;
181103026Ssobomax	sc->sc_if.if_ioctl = gre_ioctl;
182103026Ssobomax	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
183103026Ssobomax	sc->g_proto = IPPROTO_GRE;
184103026Ssobomax	sc->sc_if.if_flags |= IFF_LINK0;
185103026Ssobomax	sc->encap = NULL;
186103026Ssobomax	sc->called = 0;
187125024Ssobomax	sc->wccp_ver = WCCP_V1;
188103026Ssobomax	if_attach(&sc->sc_if);
189103026Ssobomax	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
190127307Srwatson	mtx_lock(&gre_mtx);
191103026Ssobomax	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
192127307Srwatson	mtx_unlock(&gre_mtx);
193103026Ssobomax	return (0);
194103026Ssobomax}
195103026Ssobomax
196103032Ssobomaxstatic void
197127307Srwatsongre_destroy(struct gre_softc *sc)
198103026Ssobomax{
199103026Ssobomax
200103026Ssobomax#ifdef INET
201103026Ssobomax	if (sc->encap != NULL)
202103026Ssobomax		encap_detach(sc->encap);
203103026Ssobomax#endif
204127307Srwatson	bpfdetach(&sc->sc_if);
205127307Srwatson	if_detach(&sc->sc_if);
206103026Ssobomax	free(sc, M_GRE);
207103026Ssobomax}
208103026Ssobomax
209127307Srwatsonstatic void
210127307Srwatsongre_clone_destroy(ifp)
211127307Srwatson	struct ifnet *ifp;
212127307Srwatson{
213127307Srwatson	struct gre_softc *sc = ifp->if_softc;
214127307Srwatson
215127307Srwatson	mtx_lock(&gre_mtx);
216127307Srwatson	LIST_REMOVE(sc, sc_list);
217127307Srwatson	mtx_unlock(&gre_mtx);
218127307Srwatson	gre_destroy(sc);
219127307Srwatson}
220127307Srwatson
221103026Ssobomax/*
222103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol
223103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004
224103026Ssobomax */
225103032Ssobomaxstatic int
226103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
227103026Ssobomax	   struct rtentry *rt)
228103026Ssobomax{
229103026Ssobomax	int error = 0;
230103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
231103026Ssobomax	struct greip *gh;
232103026Ssobomax	struct ip *ip;
233123992Ssobomax	u_int16_t etype = 0;
234103026Ssobomax	struct mobile_h mob_h;
235103026Ssobomax
236103026Ssobomax	/*
237103026Ssobomax	 * gre may cause infinite recursion calls when misconfigured.
238103026Ssobomax	 * We'll prevent this by introducing upper limit.
239103026Ssobomax	 */
240103026Ssobomax	if (++(sc->called) > max_gre_nesting) {
241103026Ssobomax		printf("%s: gre_output: recursively called too many "
242103026Ssobomax		       "times(%d)\n", if_name(&sc->sc_if), sc->called);
243103026Ssobomax		m_freem(m);
244103026Ssobomax		error = EIO;    /* is there better errno? */
245103026Ssobomax		goto end;
246103026Ssobomax	}
247103026Ssobomax
248103026Ssobomax	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
249103026Ssobomax	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
250103026Ssobomax		m_freem(m);
251103026Ssobomax		error = ENETDOWN;
252103026Ssobomax		goto end;
253103026Ssobomax	}
254103026Ssobomax
255103026Ssobomax	gh = NULL;
256103026Ssobomax	ip = NULL;
257103026Ssobomax
258103026Ssobomax	if (ifp->if_bpf) {
259103026Ssobomax		u_int32_t af = dst->sa_family;
260123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
261103026Ssobomax	}
262103026Ssobomax
263103026Ssobomax	m->m_flags &= ~(M_BCAST|M_MCAST);
264103026Ssobomax
265103026Ssobomax	if (sc->g_proto == IPPROTO_MOBILE) {
266103026Ssobomax		if (dst->sa_family == AF_INET) {
267103026Ssobomax			struct mbuf *m0;
268103026Ssobomax			int msiz;
269103026Ssobomax
270103026Ssobomax			ip = mtod(m, struct ip *);
271103026Ssobomax
272103026Ssobomax			/*
273103026Ssobomax			 * RFC2004 specifies that fragmented diagrams shouldn't
274103026Ssobomax			 * be encapsulated.
275103026Ssobomax			 */
276103026Ssobomax			if ((ip->ip_off & IP_MF) != 0) {
277103026Ssobomax				_IF_DROP(&ifp->if_snd);
278103026Ssobomax				m_freem(m);
279103026Ssobomax				error = EINVAL;    /* is there better errno? */
280103026Ssobomax				goto end;
281103026Ssobomax			}
282103026Ssobomax			memset(&mob_h, 0, MOB_H_SIZ_L);
283103026Ssobomax			mob_h.proto = (ip->ip_p) << 8;
284103026Ssobomax			mob_h.odst = ip->ip_dst.s_addr;
285103026Ssobomax			ip->ip_dst.s_addr = sc->g_dst.s_addr;
286103026Ssobomax
287103026Ssobomax			/*
288103026Ssobomax			 * If the packet comes from our host, we only change
289103026Ssobomax			 * the destination address in the IP header.
290103026Ssobomax			 * Else we also need to save and change the source
291103026Ssobomax			 */
292103026Ssobomax			if (in_hosteq(ip->ip_src, sc->g_src)) {
293103026Ssobomax				msiz = MOB_H_SIZ_S;
294103026Ssobomax			} else {
295103026Ssobomax				mob_h.proto |= MOB_H_SBIT;
296103026Ssobomax				mob_h.osrc = ip->ip_src.s_addr;
297103026Ssobomax				ip->ip_src.s_addr = sc->g_src.s_addr;
298103026Ssobomax				msiz = MOB_H_SIZ_L;
299103026Ssobomax			}
300103026Ssobomax			mob_h.proto = htons(mob_h.proto);
301123992Ssobomax			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
302103026Ssobomax
303103026Ssobomax			if ((m->m_data - msiz) < m->m_pktdat) {
304103026Ssobomax				/* need new mbuf */
305111119Simp				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
306103026Ssobomax				if (m0 == NULL) {
307103026Ssobomax					_IF_DROP(&ifp->if_snd);
308103026Ssobomax					m_freem(m);
309103026Ssobomax					error = ENOBUFS;
310103026Ssobomax					goto end;
311103026Ssobomax				}
312103026Ssobomax				m0->m_next = m;
313103026Ssobomax				m->m_data += sizeof(struct ip);
314103026Ssobomax				m->m_len -= sizeof(struct ip);
315103026Ssobomax				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
316103026Ssobomax				m0->m_len = msiz + sizeof(struct ip);
317103026Ssobomax				m0->m_data += max_linkhdr;
318103026Ssobomax				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
319103026Ssobomax				       sizeof(struct ip));
320103026Ssobomax				m = m0;
321103026Ssobomax			} else {  /* we have some space left in the old one */
322103026Ssobomax				m->m_data -= msiz;
323103026Ssobomax				m->m_len += msiz;
324103026Ssobomax				m->m_pkthdr.len += msiz;
325103026Ssobomax				bcopy(ip, mtod(m, caddr_t),
326103026Ssobomax					sizeof(struct ip));
327103026Ssobomax			}
328103026Ssobomax			ip = mtod(m, struct ip *);
329103026Ssobomax			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
330103026Ssobomax			ip->ip_len = ntohs(ip->ip_len) + msiz;
331103026Ssobomax		} else {  /* AF_INET */
332103026Ssobomax			_IF_DROP(&ifp->if_snd);
333103026Ssobomax			m_freem(m);
334103026Ssobomax			error = EINVAL;
335103026Ssobomax			goto end;
336103026Ssobomax		}
337103026Ssobomax	} else if (sc->g_proto == IPPROTO_GRE) {
338103026Ssobomax		switch (dst->sa_family) {
339103026Ssobomax		case AF_INET:
340103026Ssobomax			ip = mtod(m, struct ip *);
341103026Ssobomax			etype = ETHERTYPE_IP;
342103026Ssobomax			break;
343103026Ssobomax#ifdef NETATALK
344103026Ssobomax		case AF_APPLETALK:
345103026Ssobomax			etype = ETHERTYPE_ATALK;
346103026Ssobomax			break;
347103026Ssobomax#endif
348103026Ssobomax		default:
349103026Ssobomax			_IF_DROP(&ifp->if_snd);
350103026Ssobomax			m_freem(m);
351103026Ssobomax			error = EAFNOSUPPORT;
352103026Ssobomax			goto end;
353103026Ssobomax		}
354111119Simp		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
355103026Ssobomax	} else {
356103026Ssobomax		_IF_DROP(&ifp->if_snd);
357103026Ssobomax		m_freem(m);
358103026Ssobomax		error = EINVAL;
359103026Ssobomax		goto end;
360103026Ssobomax	}
361103026Ssobomax
362128580Sandre	if (m == NULL) {	/* mbuf allocation failed */
363103026Ssobomax		_IF_DROP(&ifp->if_snd);
364103026Ssobomax		error = ENOBUFS;
365103026Ssobomax		goto end;
366103026Ssobomax	}
367103026Ssobomax
368103026Ssobomax	gh = mtod(m, struct greip *);
369103026Ssobomax	if (sc->g_proto == IPPROTO_GRE) {
370103026Ssobomax		/* we don't have any GRE flags for now */
371125226Ssobomax		memset((void *)gh, 0, sizeof(struct greip));
372103026Ssobomax		gh->gi_ptype = htons(etype);
373103026Ssobomax	}
374103026Ssobomax
375103026Ssobomax	gh->gi_pr = sc->g_proto;
376103026Ssobomax	if (sc->g_proto != IPPROTO_MOBILE) {
377103026Ssobomax		gh->gi_src = sc->g_src;
378103026Ssobomax		gh->gi_dst = sc->g_dst;
379103026Ssobomax		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
380103032Ssobomax		((struct ip*)gh)->ip_ttl = GRE_TTL;
381103026Ssobomax		((struct ip*)gh)->ip_tos = ip->ip_tos;
382103026Ssobomax		((struct ip*)gh)->ip_id = ip->ip_id;
383125226Ssobomax		gh->gi_len = m->m_pkthdr.len;
384103026Ssobomax	}
385103026Ssobomax
386103026Ssobomax	ifp->if_opackets++;
387103026Ssobomax	ifp->if_obytes += m->m_pkthdr.len;
388128583Sandre	/*
389128583Sandre	 * Send it off and with IP_FORWARD flag to prevent it from
390128583Sandre	 * overwriting the ip_id again.  ip_id is already set to the
391128583Sandre	 * ip_id of the encapsulated packet.
392128583Sandre	 */
393128580Sandre	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
394123992Ssobomax	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
395103026Ssobomax  end:
396103026Ssobomax	sc->called = 0;
397103026Ssobomax	if (error)
398103026Ssobomax		ifp->if_oerrors++;
399103026Ssobomax	return (error);
400103026Ssobomax}
401103026Ssobomax
402103032Ssobomaxstatic int
403103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
404103026Ssobomax{
405103026Ssobomax	struct ifreq *ifr = (struct ifreq *)data;
406103026Ssobomax	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
407103026Ssobomax	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
408103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
409103026Ssobomax	int s;
410103026Ssobomax	struct sockaddr_in si;
411103026Ssobomax	struct sockaddr *sa = NULL;
412103026Ssobomax	int error;
413103026Ssobomax	struct sockaddr_in sp, sm, dp, dm;
414103026Ssobomax
415103026Ssobomax	error = 0;
416103026Ssobomax
417103026Ssobomax	s = splnet();
418103026Ssobomax	switch (cmd) {
419103026Ssobomax	case SIOCSIFADDR:
420103026Ssobomax		ifp->if_flags |= IFF_UP;
421103026Ssobomax		break;
422125020Ssobomax	case SIOCSIFDSTADDR:
423103026Ssobomax		break;
424103026Ssobomax	case SIOCSIFFLAGS:
425103026Ssobomax		if ((error = suser(curthread)) != 0)
426103026Ssobomax			break;
427103026Ssobomax		if ((ifr->ifr_flags & IFF_LINK0) != 0)
428103026Ssobomax			sc->g_proto = IPPROTO_GRE;
429103026Ssobomax		else
430103026Ssobomax			sc->g_proto = IPPROTO_MOBILE;
431125024Ssobomax		if ((ifr->ifr_flags & IFF_LINK2) != 0)
432125024Ssobomax			sc->wccp_ver = WCCP_V2;
433125024Ssobomax		else
434125024Ssobomax			sc->wccp_ver = WCCP_V1;
435103026Ssobomax		goto recompute;
436103026Ssobomax	case SIOCSIFMTU:
437103026Ssobomax		if ((error = suser(curthread)) != 0)
438103026Ssobomax			break;
439103026Ssobomax		if (ifr->ifr_mtu < 576) {
440103026Ssobomax			error = EINVAL;
441103026Ssobomax			break;
442103026Ssobomax		}
443103026Ssobomax		ifp->if_mtu = ifr->ifr_mtu;
444103026Ssobomax		break;
445103026Ssobomax	case SIOCGIFMTU:
446103026Ssobomax		ifr->ifr_mtu = sc->sc_if.if_mtu;
447103026Ssobomax		break;
448103026Ssobomax	case SIOCADDMULTI:
449103026Ssobomax	case SIOCDELMULTI:
450103026Ssobomax		if ((error = suser(curthread)) != 0)
451103026Ssobomax			break;
452103026Ssobomax		if (ifr == 0) {
453103026Ssobomax			error = EAFNOSUPPORT;
454103026Ssobomax			break;
455103026Ssobomax		}
456103026Ssobomax		switch (ifr->ifr_addr.sa_family) {
457103026Ssobomax#ifdef INET
458103026Ssobomax		case AF_INET:
459103026Ssobomax			break;
460103026Ssobomax#endif
461103026Ssobomax		default:
462103026Ssobomax			error = EAFNOSUPPORT;
463103026Ssobomax			break;
464103026Ssobomax		}
465103026Ssobomax		break;
466103026Ssobomax	case GRESPROTO:
467103026Ssobomax		if ((error = suser(curthread)) != 0)
468103026Ssobomax			break;
469103026Ssobomax		sc->g_proto = ifr->ifr_flags;
470103026Ssobomax		switch (sc->g_proto) {
471103026Ssobomax		case IPPROTO_GRE:
472103026Ssobomax			ifp->if_flags |= IFF_LINK0;
473103026Ssobomax			break;
474103026Ssobomax		case IPPROTO_MOBILE:
475103026Ssobomax			ifp->if_flags &= ~IFF_LINK0;
476103026Ssobomax			break;
477103026Ssobomax		default:
478103026Ssobomax			error = EPROTONOSUPPORT;
479103026Ssobomax			break;
480103026Ssobomax		}
481103026Ssobomax		goto recompute;
482103026Ssobomax	case GREGPROTO:
483103026Ssobomax		ifr->ifr_flags = sc->g_proto;
484103026Ssobomax		break;
485103026Ssobomax	case GRESADDRS:
486103026Ssobomax	case GRESADDRD:
487103026Ssobomax		if ((error = suser(curthread)) != 0)
488103026Ssobomax			break;
489103026Ssobomax		/*
490103026Ssobomax		 * set tunnel endpoints, compute a less specific route
491103026Ssobomax		 * to the remote end and mark if as up
492103026Ssobomax		 */
493103026Ssobomax		sa = &ifr->ifr_addr;
494103026Ssobomax		if (cmd == GRESADDRS)
495103026Ssobomax			sc->g_src = (satosin(sa))->sin_addr;
496103026Ssobomax		if (cmd == GRESADDRD)
497103026Ssobomax			sc->g_dst = (satosin(sa))->sin_addr;
498103026Ssobomax	recompute:
499103026Ssobomax#ifdef INET
500103026Ssobomax		if (sc->encap != NULL) {
501103026Ssobomax			encap_detach(sc->encap);
502103026Ssobomax			sc->encap = NULL;
503103026Ssobomax		}
504103026Ssobomax#endif
505103026Ssobomax		if ((sc->g_src.s_addr != INADDR_ANY) &&
506103026Ssobomax		    (sc->g_dst.s_addr != INADDR_ANY)) {
507103026Ssobomax			bzero(&sp, sizeof(sp));
508103026Ssobomax			bzero(&sm, sizeof(sm));
509103026Ssobomax			bzero(&dp, sizeof(dp));
510103026Ssobomax			bzero(&dm, sizeof(dm));
511103026Ssobomax			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
512103026Ssobomax			    sizeof(struct sockaddr_in);
513103026Ssobomax			sp.sin_family = sm.sin_family = dp.sin_family =
514103026Ssobomax			    dm.sin_family = AF_INET;
515103026Ssobomax			sp.sin_addr = sc->g_src;
516103026Ssobomax			dp.sin_addr = sc->g_dst;
517125020Ssobomax			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
518103026Ssobomax			    INADDR_BROADCAST;
519103026Ssobomax#ifdef INET
520103026Ssobomax			sc->encap = encap_attach(AF_INET, sc->g_proto,
521103026Ssobomax			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
522103026Ssobomax			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
523103026Ssobomax				&in_gre_protosw : &in_mobile_protosw, sc);
524103026Ssobomax			if (sc->encap == NULL)
525103026Ssobomax				printf("%s: unable to attach encap\n",
526103026Ssobomax				    if_name(&sc->sc_if));
527103026Ssobomax#endif
528103026Ssobomax			if (sc->route.ro_rt != 0) /* free old route */
529103026Ssobomax				RTFREE(sc->route.ro_rt);
530103026Ssobomax			if (gre_compute_route(sc) == 0)
531103026Ssobomax				ifp->if_flags |= IFF_RUNNING;
532103026Ssobomax			else
533103026Ssobomax				ifp->if_flags &= ~IFF_RUNNING;
534103026Ssobomax		}
535103026Ssobomax		break;
536103026Ssobomax	case GREGADDRS:
537103026Ssobomax		memset(&si, 0, sizeof(si));
538103026Ssobomax		si.sin_family = AF_INET;
539103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
540103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
541103026Ssobomax		sa = sintosa(&si);
542103026Ssobomax		ifr->ifr_addr = *sa;
543103026Ssobomax		break;
544103026Ssobomax	case GREGADDRD:
545103026Ssobomax		memset(&si, 0, sizeof(si));
546103026Ssobomax		si.sin_family = AF_INET;
547103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
548103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
549103026Ssobomax		sa = sintosa(&si);
550103026Ssobomax		ifr->ifr_addr = *sa;
551103026Ssobomax		break;
552103026Ssobomax	case SIOCSIFPHYADDR:
553103026Ssobomax		if ((error = suser(curthread)) != 0)
554103026Ssobomax			break;
555103026Ssobomax		if (aifr->ifra_addr.sin_family != AF_INET ||
556103026Ssobomax		    aifr->ifra_dstaddr.sin_family != AF_INET) {
557103026Ssobomax			error = EAFNOSUPPORT;
558103026Ssobomax			break;
559103026Ssobomax		}
560103026Ssobomax		if (aifr->ifra_addr.sin_len != sizeof(si) ||
561103026Ssobomax		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
562103026Ssobomax			error = EINVAL;
563103026Ssobomax			break;
564103026Ssobomax		}
565103026Ssobomax		sc->g_src = aifr->ifra_addr.sin_addr;
566103026Ssobomax		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
567103026Ssobomax		goto recompute;
568103026Ssobomax	case SIOCSLIFPHYADDR:
569103026Ssobomax		if ((error = suser(curthread)) != 0)
570103026Ssobomax			break;
571103026Ssobomax		if (lifr->addr.ss_family != AF_INET ||
572103026Ssobomax		    lifr->dstaddr.ss_family != AF_INET) {
573103026Ssobomax			error = EAFNOSUPPORT;
574103026Ssobomax			break;
575103026Ssobomax		}
576103026Ssobomax		if (lifr->addr.ss_len != sizeof(si) ||
577103026Ssobomax		    lifr->dstaddr.ss_len != sizeof(si)) {
578103026Ssobomax			error = EINVAL;
579103026Ssobomax			break;
580103026Ssobomax		}
581103026Ssobomax		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
582103026Ssobomax		sc->g_dst =
583103026Ssobomax		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
584103026Ssobomax		goto recompute;
585103026Ssobomax	case SIOCDIFPHYADDR:
586103026Ssobomax		if ((error = suser(curthread)) != 0)
587103026Ssobomax			break;
588103026Ssobomax		sc->g_src.s_addr = INADDR_ANY;
589103026Ssobomax		sc->g_dst.s_addr = INADDR_ANY;
590103026Ssobomax		goto recompute;
591103026Ssobomax	case SIOCGLIFPHYADDR:
592103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY ||
593103026Ssobomax		    sc->g_dst.s_addr == INADDR_ANY) {
594103026Ssobomax			error = EADDRNOTAVAIL;
595103026Ssobomax			break;
596103026Ssobomax		}
597103026Ssobomax		memset(&si, 0, sizeof(si));
598103026Ssobomax		si.sin_family = AF_INET;
599103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
600103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
601103026Ssobomax		memcpy(&lifr->addr, &si, sizeof(si));
602103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
603103026Ssobomax		memcpy(&lifr->dstaddr, &si, sizeof(si));
604103026Ssobomax		break;
605103026Ssobomax	case SIOCGIFPSRCADDR:
606122699Sbms#ifdef INET6
607122699Sbms	case SIOCGIFPSRCADDR_IN6:
608122699Sbms#endif
609103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY) {
610103026Ssobomax			error = EADDRNOTAVAIL;
611103026Ssobomax			break;
612103026Ssobomax		}
613103026Ssobomax		memset(&si, 0, sizeof(si));
614103026Ssobomax		si.sin_family = AF_INET;
615103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
616103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
617103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
618103026Ssobomax		break;
619103026Ssobomax	case SIOCGIFPDSTADDR:
620122699Sbms#ifdef INET6
621122699Sbms	case SIOCGIFPDSTADDR_IN6:
622122699Sbms#endif
623103026Ssobomax		if (sc->g_dst.s_addr == INADDR_ANY) {
624103026Ssobomax			error = EADDRNOTAVAIL;
625103026Ssobomax			break;
626103026Ssobomax		}
627103026Ssobomax		memset(&si, 0, sizeof(si));
628103026Ssobomax		si.sin_family = AF_INET;
629103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
630103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
631103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
632103026Ssobomax		break;
633103026Ssobomax	default:
634103026Ssobomax		error = EINVAL;
635103026Ssobomax		break;
636103026Ssobomax	}
637103026Ssobomax
638103026Ssobomax	splx(s);
639103026Ssobomax	return (error);
640103026Ssobomax}
641103026Ssobomax
642103026Ssobomax/*
643103026Ssobomax * computes a route to our destination that is not the one
644103026Ssobomax * which would be taken by ip_output(), as this one will loop back to
645103026Ssobomax * us. If the interface is p2p as  a--->b, then a routing entry exists
646103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here
647123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to
648103026Ssobomax * if_gre.
649103026Ssobomax * Goal here is to compute a route to b that is less specific than
650103026Ssobomax * a-->b. We know that this one exists as in normal operation we have
651103026Ssobomax * at least a default route which matches.
652103026Ssobomax */
653103032Ssobomaxstatic int
654103026Ssobomaxgre_compute_route(struct gre_softc *sc)
655103026Ssobomax{
656103026Ssobomax	struct route *ro;
657103026Ssobomax	u_int32_t a, b, c;
658103026Ssobomax
659103026Ssobomax	ro = &sc->route;
660103026Ssobomax
661103026Ssobomax	memset(ro, 0, sizeof(struct route));
662103026Ssobomax	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
663103026Ssobomax	ro->ro_dst.sa_family = AF_INET;
664103026Ssobomax	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
665103026Ssobomax
666103026Ssobomax	/*
667103026Ssobomax	 * toggle last bit, so our interface is not found, but a less
668103026Ssobomax	 * specific route. I'd rather like to specify a shorter mask,
669103026Ssobomax	 * but this is not possible. Should work though. XXX
670103026Ssobomax	 * there is a simpler way ...
671103026Ssobomax	 */
672103026Ssobomax	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
673103026Ssobomax		a = ntohl(sc->g_dst.s_addr);
674103026Ssobomax		b = a & 0x01;
675103026Ssobomax		c = a & 0xfffffffe;
676103026Ssobomax		b = b ^ 0x01;
677103026Ssobomax		a = b | c;
678103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
679103026Ssobomax		    = htonl(a);
680103026Ssobomax	}
681103026Ssobomax
682103026Ssobomax#ifdef DIAGNOSTIC
683123992Ssobomax	printf("%s: searching for a route to %s", if_name(&sc->sc_if),
684103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
685103026Ssobomax#endif
686103026Ssobomax
687103026Ssobomax	rtalloc(ro);
688103026Ssobomax
689103026Ssobomax	/*
690103026Ssobomax	 * check if this returned a route at all and this route is no
691103026Ssobomax	 * recursion to ourself
692103026Ssobomax	 */
693103026Ssobomax	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
694103026Ssobomax#ifdef DIAGNOSTIC
695103026Ssobomax		if (ro->ro_rt == NULL)
696103026Ssobomax			printf(" - no route found!\n");
697103026Ssobomax		else
698103026Ssobomax			printf(" - route loops back to ourself!\n");
699103026Ssobomax#endif
700103026Ssobomax		return EADDRNOTAVAIL;
701103026Ssobomax	}
702103026Ssobomax
703103026Ssobomax	/*
704103026Ssobomax	 * now change it back - else ip_output will just drop
705103026Ssobomax	 * the route and search one to this interface ...
706103026Ssobomax	 */
707103026Ssobomax	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
708103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
709103026Ssobomax
710103026Ssobomax#ifdef DIAGNOSTIC
711103026Ssobomax	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
712103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
713103026Ssobomax	printf("\n");
714103026Ssobomax#endif
715103026Ssobomax
716103026Ssobomax	return 0;
717103026Ssobomax}
718103026Ssobomax
719103026Ssobomax/*
720103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on
721103026Ssobomax * mbufs.
722103026Ssobomax */
723123992Ssobomaxu_int16_t
724123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len)
725103026Ssobomax{
726123992Ssobomax	u_int32_t sum = 0;
727103026Ssobomax	int nwords = len >> 1;
728103026Ssobomax
729103026Ssobomax	while (nwords-- != 0)
730103026Ssobomax		sum += *p++;
731103026Ssobomax
732103026Ssobomax	if (len & 1) {
733103026Ssobomax		union {
734103026Ssobomax			u_short w;
735103026Ssobomax			u_char c[2];
736103026Ssobomax		} u;
737103026Ssobomax		u.c[0] = *(u_char *)p;
738103026Ssobomax		u.c[1] = 0;
739103026Ssobomax		sum += u.w;
740103026Ssobomax	}
741103026Ssobomax
742103026Ssobomax	/* end-around-carry */
743103026Ssobomax	sum = (sum >> 16) + (sum & 0xffff);
744103026Ssobomax	sum += (sum >> 16);
745103026Ssobomax	return (~sum);
746103026Ssobomax}
747103026Ssobomax
748103026Ssobomaxstatic int
749103026Ssobomaxgremodevent(module_t mod, int type, void *data)
750103026Ssobomax{
751127307Srwatson	struct gre_softc *sc;
752103026Ssobomax
753103026Ssobomax	switch (type) {
754103026Ssobomax	case MOD_LOAD:
755103026Ssobomax		greattach();
756103026Ssobomax		break;
757103026Ssobomax	case MOD_UNLOAD:
758103026Ssobomax		if_clone_detach(&gre_cloner);
759103026Ssobomax
760127307Srwatson		mtx_lock(&gre_mtx);
761127307Srwatson		while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) {
762127307Srwatson			LIST_REMOVE(sc, sc_list);
763127307Srwatson			mtx_unlock(&gre_mtx);
764127307Srwatson			gre_destroy(sc);
765127307Srwatson			mtx_lock(&gre_mtx);
766127307Srwatson		}
767127307Srwatson		mtx_unlock(&gre_mtx);
768127307Srwatson		mtx_destroy(&gre_mtx);
769103026Ssobomax		break;
770103026Ssobomax	}
771103026Ssobomax	return 0;
772103026Ssobomax}
773103026Ssobomax
774103026Ssobomaxstatic moduledata_t gre_mod = {
775103026Ssobomax	"if_gre",
776103026Ssobomax	gremodevent,
777103026Ssobomax	0
778103026Ssobomax};
779103026Ssobomax
780103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
781103026SsobomaxMODULE_VERSION(if_gre, 1);
782