if_gre.c revision 103344
1103026Ssobomax/*	$NetBSD: if_gre.c,v 1.42 2002/08/14 00:23:27 itojun Exp $ */
2103026Ssobomax/*	 $FreeBSD: head/sys/net/if_gre.c 103344 2002-09-15 13:54:12Z bde $ */
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
49103026Ssobomax#include <sys/cdefs.h>
50103026Ssobomax__RCSID("@(#) $FreeBSD: head/sys/net/if_gre.c 103344 2002-09-15 13:54:12Z bde $");
51103026Ssobomax
52103026Ssobomax#include "opt_inet.h"
53103026Ssobomax#include "opt_ns.h"
54103026Ssobomax#include "bpf.h"
55103026Ssobomax
56103026Ssobomax#include <sys/param.h>
57103026Ssobomax#include <sys/kernel.h>
58103026Ssobomax#include <sys/malloc.h>
59103026Ssobomax#include <sys/mbuf.h>
60103026Ssobomax#include <sys/proc.h>
61103026Ssobomax#include <sys/protosw.h>
62103026Ssobomax#include <sys/socket.h>
63103026Ssobomax#include <sys/sockio.h>
64103026Ssobomax#include <sys/queue.h>
65103026Ssobomax#include <sys/sysctl.h>
66103344Sbde#include <sys/systm.h>
67103026Ssobomax
68103026Ssobomax#include <machine/cpu.h>
69103026Ssobomax
70103026Ssobomax#include <net/ethernet.h>
71103026Ssobomax#include <net/if.h>
72103026Ssobomax#include <net/if_types.h>
73103026Ssobomax#include <net/netisr.h>
74103026Ssobomax#include <net/route.h>
75103026Ssobomax
76103026Ssobomax#ifdef INET
77103026Ssobomax#include <netinet/in.h>
78103026Ssobomax#include <netinet/in_systm.h>
79103026Ssobomax#include <netinet/in_var.h>
80103026Ssobomax#include <netinet/ip.h>
81103026Ssobomax#include <netinet/ip_gre.h>
82103026Ssobomax#include <netinet/ip_var.h>
83103026Ssobomax#include <netinet/ip_encap.h>
84103026Ssobomax#else
85103026Ssobomax#error "Huh? if_gre without inet?"
86103026Ssobomax#endif
87103026Ssobomax
88103026Ssobomax#ifdef NS
89103026Ssobomax#include <netns/ns.h>
90103026Ssobomax#include <netns/ns_if.h>
91103026Ssobomax#endif
92103026Ssobomax
93103026Ssobomax#ifdef NETATALK
94103026Ssobomax#include <netatalk/at.h>
95103026Ssobomax#include <netatalk/at_var.h>
96103026Ssobomax#include <netatalk/at_extern.h>
97103026Ssobomax#endif
98103026Ssobomax
99103026Ssobomax#if NBPF > 0
100103026Ssobomax#include <sys/time.h>
101103026Ssobomax#include <net/bpf.h>
102103026Ssobomax#endif
103103026Ssobomax
104103026Ssobomax#include <net/net_osdep.h>
105103026Ssobomax#include <net/if_gre.h>
106103026Ssobomax
107103026Ssobomax/*
108103026Ssobomax * It is not easy to calculate the right value for a GRE MTU.
109103026Ssobomax * We leave this task to the admin and use the same default that
110103026Ssobomax * other vendors use.
111103026Ssobomax */
112103026Ssobomax#define GREMTU	1476
113103026Ssobomax
114103026Ssobomax#define GRENAME	"gre"
115103026Ssobomax
116103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
117103026Ssobomax
118103026Ssobomaxstruct gre_softc_head gre_softc_list;
119103026Ssobomax
120103032Ssobomaxstatic int	gre_clone_create __P((struct if_clone *, int));
121103032Ssobomaxstatic void	gre_clone_destroy __P((struct ifnet *));
122103032Ssobomaxstatic int	gre_ioctl(struct ifnet *, u_long, caddr_t);
123103032Ssobomaxstatic int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
124103032Ssobomax		    struct rtentry *rt);
125103026Ssobomax
126103032Ssobomaxstatic struct if_clone gre_cloner =
127103026Ssobomax    IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy, 0, IF_MAXUNIT);
128103026Ssobomax
129103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc);
130103026Ssobomax
131103032Ssobomaxstatic void	greattach __P((void));
132103026Ssobomax
133103026Ssobomax#ifdef INET
134103026Ssobomaxextern struct domain inetdomain;
135103026Ssobomaxstatic const struct protosw in_gre_protosw =
136103026Ssobomax{ SOCK_RAW,     &inetdomain,    IPPROTO_GRE,    PR_ATOMIC|PR_ADDR,
137103026Ssobomax  (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
138103026Ssobomax  0,
139103026Ssobomax  0,		0,		0,		0,
140103026Ssobomax  &rip_usrreqs
141103026Ssobomax};
142103026Ssobomaxstatic const struct protosw in_mobile_protosw =
143103026Ssobomax{ SOCK_RAW,     &inetdomain,    IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR,
144103026Ssobomax  (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
145103026Ssobomax  0,
146103026Ssobomax  0,		0,		0,		0,
147103026Ssobomax  &rip_usrreqs
148103026Ssobomax};
149103026Ssobomax#endif
150103026Ssobomax
151103026SsobomaxSYSCTL_DECL(_net_link);
152103026SsobomaxSYSCTL_NODE(_net_link, IFT_OTHER, gre, CTLFLAG_RW, 0,
153103026Ssobomax    "Generic Routing Encapsulation");
154103026Ssobomax#ifndef MAX_GRE_NEST
155103026Ssobomax/*
156103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels.
157103026Ssobomax * Since, setting a large value to this macro with a careless configuration
158103026Ssobomax * may introduce system crash, we don't allow any nestings by default.
159103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro
160103026Ssobomax * in your kernel configuration file.  However, if you do so, please be
161103026Ssobomax * careful to configure the tunnels so that it won't make a loop.
162103026Ssobomax */
163103026Ssobomax#define MAX_GRE_NEST 1
164103026Ssobomax#endif
165103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST;
166103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
167103026Ssobomax    &max_gre_nesting, 0, "Max nested tunnels");
168103026Ssobomax
169103026Ssobomax/* ARGSUSED */
170103032Ssobomaxstatic void
171103026Ssobomaxgreattach(void)
172103026Ssobomax{
173103026Ssobomax
174103026Ssobomax	LIST_INIT(&gre_softc_list);
175103026Ssobomax	if_clone_attach(&gre_cloner);
176103026Ssobomax}
177103026Ssobomax
178103032Ssobomaxstatic int
179103026Ssobomaxgre_clone_create(ifc, unit)
180103026Ssobomax	struct if_clone *ifc;
181103026Ssobomax	int unit;
182103026Ssobomax{
183103026Ssobomax	struct gre_softc *sc;
184103026Ssobomax
185103026Ssobomax	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK);
186103026Ssobomax	memset(sc, 0, sizeof(struct gre_softc));
187103026Ssobomax
188103026Ssobomax	sc->sc_if.if_name = GRENAME;
189103026Ssobomax	sc->sc_if.if_softc = sc;
190103026Ssobomax	sc->sc_if.if_unit = unit;
191103026Ssobomax	sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
192103026Ssobomax	sc->sc_if.if_type = IFT_OTHER;
193103026Ssobomax	sc->sc_if.if_addrlen = 0;
194103026Ssobomax	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
195103026Ssobomax	sc->sc_if.if_mtu = GREMTU;
196103026Ssobomax	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
197103026Ssobomax	sc->sc_if.if_output = gre_output;
198103026Ssobomax	sc->sc_if.if_ioctl = gre_ioctl;
199103026Ssobomax	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
200103026Ssobomax	sc->g_proto = IPPROTO_GRE;
201103026Ssobomax	sc->sc_if.if_flags |= IFF_LINK0;
202103026Ssobomax	sc->encap = NULL;
203103026Ssobomax	sc->called = 0;
204103026Ssobomax	if_attach(&sc->sc_if);
205103026Ssobomax#if NBPF
206103026Ssobomax	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
207103026Ssobomax#endif
208103026Ssobomax	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
209103026Ssobomax	return (0);
210103026Ssobomax}
211103026Ssobomax
212103032Ssobomaxstatic void
213103026Ssobomaxgre_clone_destroy(ifp)
214103026Ssobomax	struct ifnet *ifp;
215103026Ssobomax{
216103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
217103026Ssobomax
218103026Ssobomax#ifdef INET
219103026Ssobomax	if (sc->encap != NULL)
220103026Ssobomax		encap_detach(sc->encap);
221103026Ssobomax#endif
222103026Ssobomax	LIST_REMOVE(sc, sc_list);
223103026Ssobomax#if NBPF
224103026Ssobomax	bpfdetach(ifp);
225103026Ssobomax#endif
226103026Ssobomax	if_detach(ifp);
227103026Ssobomax	free(sc, M_GRE);
228103026Ssobomax}
229103026Ssobomax
230103026Ssobomax/*
231103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol
232103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004
233103026Ssobomax */
234103032Ssobomaxstatic int
235103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
236103026Ssobomax	   struct rtentry *rt)
237103026Ssobomax{
238103026Ssobomax	int error = 0;
239103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
240103026Ssobomax	struct greip *gh;
241103026Ssobomax	struct ip *ip;
242103026Ssobomax	u_char osrc;
243103026Ssobomax	u_short etype = 0;
244103026Ssobomax	struct mobile_h mob_h;
245103026Ssobomax
246103026Ssobomax	/*
247103026Ssobomax	 * gre may cause infinite recursion calls when misconfigured.
248103026Ssobomax	 * We'll prevent this by introducing upper limit.
249103026Ssobomax	 */
250103026Ssobomax	if (++(sc->called) > max_gre_nesting) {
251103026Ssobomax		printf("%s: gre_output: recursively called too many "
252103026Ssobomax		       "times(%d)\n", if_name(&sc->sc_if), sc->called);
253103026Ssobomax		m_freem(m);
254103026Ssobomax		error = EIO;    /* is there better errno? */
255103026Ssobomax		goto end;
256103026Ssobomax	}
257103026Ssobomax
258103026Ssobomax	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
259103026Ssobomax	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
260103026Ssobomax		m_freem(m);
261103026Ssobomax		error = ENETDOWN;
262103026Ssobomax		goto end;
263103026Ssobomax	}
264103026Ssobomax
265103026Ssobomax	gh = NULL;
266103026Ssobomax	ip = NULL;
267103026Ssobomax	osrc = 0;
268103026Ssobomax
269103026Ssobomax#if NBPF
270103026Ssobomax	if (ifp->if_bpf) {
271103026Ssobomax		/* see comment of other if_foo.c files */
272103026Ssobomax		struct mbuf m0;
273103026Ssobomax		u_int32_t af = dst->sa_family;
274103026Ssobomax
275103026Ssobomax		m0.m_next = m;
276103026Ssobomax		m0.m_len = 4;
277103026Ssobomax		m0.m_data = (char *)&af;
278103026Ssobomax
279103026Ssobomax		bpf_mtap(ifp, &m0);
280103026Ssobomax	}
281103026Ssobomax#endif
282103026Ssobomax
283103026Ssobomax	m->m_flags &= ~(M_BCAST|M_MCAST);
284103026Ssobomax
285103026Ssobomax	if (sc->g_proto == IPPROTO_MOBILE) {
286103026Ssobomax		if (dst->sa_family == AF_INET) {
287103026Ssobomax			struct mbuf *m0;
288103026Ssobomax			int msiz;
289103026Ssobomax
290103026Ssobomax			ip = mtod(m, struct ip *);
291103026Ssobomax
292103026Ssobomax			/*
293103026Ssobomax			 * RFC2004 specifies that fragmented diagrams shouldn't
294103026Ssobomax			 * be encapsulated.
295103026Ssobomax			 */
296103026Ssobomax			if ((ip->ip_off & IP_MF) != 0) {
297103026Ssobomax				_IF_DROP(&ifp->if_snd);
298103026Ssobomax				m_freem(m);
299103026Ssobomax				error = EINVAL;    /* is there better errno? */
300103026Ssobomax				goto end;
301103026Ssobomax			}
302103026Ssobomax			memset(&mob_h, 0, MOB_H_SIZ_L);
303103026Ssobomax			mob_h.proto = (ip->ip_p) << 8;
304103026Ssobomax			mob_h.odst = ip->ip_dst.s_addr;
305103026Ssobomax			ip->ip_dst.s_addr = sc->g_dst.s_addr;
306103026Ssobomax
307103026Ssobomax			/*
308103026Ssobomax			 * If the packet comes from our host, we only change
309103026Ssobomax			 * the destination address in the IP header.
310103026Ssobomax			 * Else we also need to save and change the source
311103026Ssobomax			 */
312103026Ssobomax			if (in_hosteq(ip->ip_src, sc->g_src)) {
313103026Ssobomax				msiz = MOB_H_SIZ_S;
314103026Ssobomax			} else {
315103026Ssobomax				mob_h.proto |= MOB_H_SBIT;
316103026Ssobomax				mob_h.osrc = ip->ip_src.s_addr;
317103026Ssobomax				ip->ip_src.s_addr = sc->g_src.s_addr;
318103026Ssobomax				msiz = MOB_H_SIZ_L;
319103026Ssobomax			}
320103026Ssobomax			mob_h.proto = htons(mob_h.proto);
321103026Ssobomax			mob_h.hcrc = gre_in_cksum((u_short *)&mob_h, msiz);
322103026Ssobomax
323103026Ssobomax			if ((m->m_data - msiz) < m->m_pktdat) {
324103026Ssobomax				/* need new mbuf */
325103026Ssobomax				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
326103026Ssobomax				if (m0 == NULL) {
327103026Ssobomax					_IF_DROP(&ifp->if_snd);
328103026Ssobomax					m_freem(m);
329103026Ssobomax					error = ENOBUFS;
330103026Ssobomax					goto end;
331103026Ssobomax				}
332103026Ssobomax				m0->m_next = m;
333103026Ssobomax				m->m_data += sizeof(struct ip);
334103026Ssobomax				m->m_len -= sizeof(struct ip);
335103026Ssobomax				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
336103026Ssobomax				m0->m_len = msiz + sizeof(struct ip);
337103026Ssobomax				m0->m_data += max_linkhdr;
338103026Ssobomax				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
339103026Ssobomax				       sizeof(struct ip));
340103026Ssobomax				m = m0;
341103026Ssobomax			} else {  /* we have some space left in the old one */
342103026Ssobomax				m->m_data -= msiz;
343103026Ssobomax				m->m_len += msiz;
344103026Ssobomax				m->m_pkthdr.len += msiz;
345103026Ssobomax				bcopy(ip, mtod(m, caddr_t),
346103026Ssobomax					sizeof(struct ip));
347103026Ssobomax			}
348103026Ssobomax			ip = mtod(m, struct ip *);
349103026Ssobomax			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
350103026Ssobomax			ip->ip_len = ntohs(ip->ip_len) + msiz;
351103026Ssobomax		} else {  /* AF_INET */
352103026Ssobomax			_IF_DROP(&ifp->if_snd);
353103026Ssobomax			m_freem(m);
354103026Ssobomax			error = EINVAL;
355103026Ssobomax			goto end;
356103026Ssobomax		}
357103026Ssobomax	} else if (sc->g_proto == IPPROTO_GRE) {
358103026Ssobomax		switch (dst->sa_family) {
359103026Ssobomax		case AF_INET:
360103026Ssobomax			ip = mtod(m, struct ip *);
361103026Ssobomax			etype = ETHERTYPE_IP;
362103026Ssobomax			break;
363103026Ssobomax#ifdef NETATALK
364103026Ssobomax		case AF_APPLETALK:
365103026Ssobomax			etype = ETHERTYPE_ATALK;
366103026Ssobomax			break;
367103026Ssobomax#endif
368103026Ssobomax#ifdef NS
369103026Ssobomax		case AF_NS:
370103026Ssobomax			etype = ETHERTYPE_NS;
371103026Ssobomax			break;
372103026Ssobomax#endif
373103026Ssobomax		default:
374103026Ssobomax			_IF_DROP(&ifp->if_snd);
375103026Ssobomax			m_freem(m);
376103026Ssobomax			error = EAFNOSUPPORT;
377103026Ssobomax			goto end;
378103026Ssobomax		}
379103026Ssobomax		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
380103026Ssobomax	} else {
381103026Ssobomax		_IF_DROP(&ifp->if_snd);
382103026Ssobomax		m_freem(m);
383103026Ssobomax		error = EINVAL;
384103026Ssobomax		goto end;
385103026Ssobomax	}
386103026Ssobomax
387103026Ssobomax	if (m == NULL) {	/* impossible */
388103026Ssobomax		_IF_DROP(&ifp->if_snd);
389103026Ssobomax		error = ENOBUFS;
390103026Ssobomax		goto end;
391103026Ssobomax	}
392103026Ssobomax
393103026Ssobomax	gh = mtod(m, struct greip *);
394103026Ssobomax	if (sc->g_proto == IPPROTO_GRE) {
395103026Ssobomax		/* we don't have any GRE flags for now */
396103026Ssobomax
397103026Ssobomax		memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
398103026Ssobomax		gh->gi_ptype = htons(etype);
399103026Ssobomax	}
400103026Ssobomax
401103026Ssobomax	gh->gi_pr = sc->g_proto;
402103026Ssobomax	if (sc->g_proto != IPPROTO_MOBILE) {
403103026Ssobomax		gh->gi_src = sc->g_src;
404103026Ssobomax		gh->gi_dst = sc->g_dst;
405103026Ssobomax		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
406103032Ssobomax		((struct ip*)gh)->ip_ttl = GRE_TTL;
407103026Ssobomax		((struct ip*)gh)->ip_tos = ip->ip_tos;
408103026Ssobomax		((struct ip*)gh)->ip_id = ip->ip_id;
409103026Ssobomax		gh->gi_len = m->m_pkthdr.len;
410103026Ssobomax	}
411103026Ssobomax
412103026Ssobomax	ifp->if_opackets++;
413103026Ssobomax	ifp->if_obytes += m->m_pkthdr.len;
414103026Ssobomax	/* send it off */
415103026Ssobomax	error = ip_output(m, NULL, &sc->route, 0, NULL);
416103026Ssobomax  end:
417103026Ssobomax	sc->called = 0;
418103026Ssobomax	if (error)
419103026Ssobomax		ifp->if_oerrors++;
420103026Ssobomax	return (error);
421103026Ssobomax}
422103026Ssobomax
423103032Ssobomaxstatic int
424103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
425103026Ssobomax{
426103026Ssobomax	struct ifreq *ifr = (struct ifreq *)data;
427103026Ssobomax	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
428103026Ssobomax	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
429103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
430103026Ssobomax	int s;
431103026Ssobomax	struct sockaddr_in si;
432103026Ssobomax	struct sockaddr *sa = NULL;
433103026Ssobomax	int error;
434103026Ssobomax	struct sockaddr_in sp, sm, dp, dm;
435103026Ssobomax
436103026Ssobomax	error = 0;
437103026Ssobomax
438103026Ssobomax	s = splnet();
439103026Ssobomax	switch (cmd) {
440103026Ssobomax	case SIOCSIFADDR:
441103026Ssobomax		ifp->if_flags |= IFF_UP;
442103026Ssobomax		break;
443103026Ssobomax	case SIOCSIFDSTADDR:
444103026Ssobomax		break;
445103026Ssobomax	case SIOCSIFFLAGS:
446103026Ssobomax		if ((error = suser(curthread)) != 0)
447103026Ssobomax			break;
448103026Ssobomax		if ((ifr->ifr_flags & IFF_LINK0) != 0)
449103026Ssobomax			sc->g_proto = IPPROTO_GRE;
450103026Ssobomax		else
451103026Ssobomax			sc->g_proto = IPPROTO_MOBILE;
452103026Ssobomax		goto recompute;
453103026Ssobomax	case SIOCSIFMTU:
454103026Ssobomax		if ((error = suser(curthread)) != 0)
455103026Ssobomax			break;
456103026Ssobomax		if (ifr->ifr_mtu < 576) {
457103026Ssobomax			error = EINVAL;
458103026Ssobomax			break;
459103026Ssobomax		}
460103026Ssobomax		ifp->if_mtu = ifr->ifr_mtu;
461103026Ssobomax		break;
462103026Ssobomax	case SIOCGIFMTU:
463103026Ssobomax		ifr->ifr_mtu = sc->sc_if.if_mtu;
464103026Ssobomax		break;
465103026Ssobomax	case SIOCADDMULTI:
466103026Ssobomax	case SIOCDELMULTI:
467103026Ssobomax		if ((error = suser(curthread)) != 0)
468103026Ssobomax			break;
469103026Ssobomax		if (ifr == 0) {
470103026Ssobomax			error = EAFNOSUPPORT;
471103026Ssobomax			break;
472103026Ssobomax		}
473103026Ssobomax		switch (ifr->ifr_addr.sa_family) {
474103026Ssobomax#ifdef INET
475103026Ssobomax		case AF_INET:
476103026Ssobomax			break;
477103026Ssobomax#endif
478103026Ssobomax		default:
479103026Ssobomax			error = EAFNOSUPPORT;
480103026Ssobomax			break;
481103026Ssobomax		}
482103026Ssobomax		break;
483103026Ssobomax	case GRESPROTO:
484103026Ssobomax		if ((error = suser(curthread)) != 0)
485103026Ssobomax			break;
486103026Ssobomax		sc->g_proto = ifr->ifr_flags;
487103026Ssobomax		switch (sc->g_proto) {
488103026Ssobomax		case IPPROTO_GRE:
489103026Ssobomax			ifp->if_flags |= IFF_LINK0;
490103026Ssobomax			break;
491103026Ssobomax		case IPPROTO_MOBILE:
492103026Ssobomax			ifp->if_flags &= ~IFF_LINK0;
493103026Ssobomax			break;
494103026Ssobomax		default:
495103026Ssobomax			error = EPROTONOSUPPORT;
496103026Ssobomax			break;
497103026Ssobomax		}
498103026Ssobomax		goto recompute;
499103026Ssobomax	case GREGPROTO:
500103026Ssobomax		ifr->ifr_flags = sc->g_proto;
501103026Ssobomax		break;
502103026Ssobomax	case GRESADDRS:
503103026Ssobomax	case GRESADDRD:
504103026Ssobomax		if ((error = suser(curthread)) != 0)
505103026Ssobomax			break;
506103026Ssobomax		/*
507103026Ssobomax		 * set tunnel endpoints, compute a less specific route
508103026Ssobomax		 * to the remote end and mark if as up
509103026Ssobomax		 */
510103026Ssobomax		sa = &ifr->ifr_addr;
511103026Ssobomax		if (cmd == GRESADDRS)
512103026Ssobomax			sc->g_src = (satosin(sa))->sin_addr;
513103026Ssobomax		if (cmd == GRESADDRD)
514103026Ssobomax			sc->g_dst = (satosin(sa))->sin_addr;
515103026Ssobomax	recompute:
516103026Ssobomax#ifdef INET
517103026Ssobomax		if (sc->encap != NULL) {
518103026Ssobomax			encap_detach(sc->encap);
519103026Ssobomax			sc->encap = NULL;
520103026Ssobomax		}
521103026Ssobomax#endif
522103026Ssobomax		if ((sc->g_src.s_addr != INADDR_ANY) &&
523103026Ssobomax		    (sc->g_dst.s_addr != INADDR_ANY)) {
524103026Ssobomax			bzero(&sp, sizeof(sp));
525103026Ssobomax			bzero(&sm, sizeof(sm));
526103026Ssobomax			bzero(&dp, sizeof(dp));
527103026Ssobomax			bzero(&dm, sizeof(dm));
528103026Ssobomax			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
529103026Ssobomax			    sizeof(struct sockaddr_in);
530103026Ssobomax			sp.sin_family = sm.sin_family = dp.sin_family =
531103026Ssobomax			    dm.sin_family = AF_INET;
532103026Ssobomax			sp.sin_addr = sc->g_src;
533103026Ssobomax			dp.sin_addr = sc->g_dst;
534103026Ssobomax			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
535103026Ssobomax			    INADDR_BROADCAST;
536103026Ssobomax#ifdef INET
537103026Ssobomax			sc->encap = encap_attach(AF_INET, sc->g_proto,
538103026Ssobomax			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
539103026Ssobomax			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
540103026Ssobomax				&in_gre_protosw : &in_mobile_protosw, sc);
541103026Ssobomax			if (sc->encap == NULL)
542103026Ssobomax				printf("%s: unable to attach encap\n",
543103026Ssobomax				    if_name(&sc->sc_if));
544103026Ssobomax#endif
545103026Ssobomax			if (sc->route.ro_rt != 0) /* free old route */
546103026Ssobomax				RTFREE(sc->route.ro_rt);
547103026Ssobomax			if (gre_compute_route(sc) == 0)
548103026Ssobomax				ifp->if_flags |= IFF_RUNNING;
549103026Ssobomax			else
550103026Ssobomax				ifp->if_flags &= ~IFF_RUNNING;
551103026Ssobomax		}
552103026Ssobomax		break;
553103026Ssobomax	case GREGADDRS:
554103026Ssobomax		memset(&si, 0, sizeof(si));
555103026Ssobomax		si.sin_family = AF_INET;
556103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
557103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
558103026Ssobomax		sa = sintosa(&si);
559103026Ssobomax		ifr->ifr_addr = *sa;
560103026Ssobomax		break;
561103026Ssobomax	case GREGADDRD:
562103026Ssobomax		memset(&si, 0, sizeof(si));
563103026Ssobomax		si.sin_family = AF_INET;
564103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
565103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
566103026Ssobomax		sa = sintosa(&si);
567103026Ssobomax		ifr->ifr_addr = *sa;
568103026Ssobomax		break;
569103026Ssobomax	case SIOCSIFPHYADDR:
570103026Ssobomax		if ((error = suser(curthread)) != 0)
571103026Ssobomax			break;
572103026Ssobomax		if (aifr->ifra_addr.sin_family != AF_INET ||
573103026Ssobomax		    aifr->ifra_dstaddr.sin_family != AF_INET) {
574103026Ssobomax			error = EAFNOSUPPORT;
575103026Ssobomax			break;
576103026Ssobomax		}
577103026Ssobomax		if (aifr->ifra_addr.sin_len != sizeof(si) ||
578103026Ssobomax		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
579103026Ssobomax			error = EINVAL;
580103026Ssobomax			break;
581103026Ssobomax		}
582103026Ssobomax		sc->g_src = aifr->ifra_addr.sin_addr;
583103026Ssobomax		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
584103026Ssobomax		goto recompute;
585103026Ssobomax	case SIOCSLIFPHYADDR:
586103026Ssobomax		if ((error = suser(curthread)) != 0)
587103026Ssobomax			break;
588103026Ssobomax		if (lifr->addr.ss_family != AF_INET ||
589103026Ssobomax		    lifr->dstaddr.ss_family != AF_INET) {
590103026Ssobomax			error = EAFNOSUPPORT;
591103026Ssobomax			break;
592103026Ssobomax		}
593103026Ssobomax		if (lifr->addr.ss_len != sizeof(si) ||
594103026Ssobomax		    lifr->dstaddr.ss_len != sizeof(si)) {
595103026Ssobomax			error = EINVAL;
596103026Ssobomax			break;
597103026Ssobomax		}
598103026Ssobomax		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
599103026Ssobomax		sc->g_dst =
600103026Ssobomax		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
601103026Ssobomax		goto recompute;
602103026Ssobomax	case SIOCDIFPHYADDR:
603103026Ssobomax		if ((error = suser(curthread)) != 0)
604103026Ssobomax			break;
605103026Ssobomax		sc->g_src.s_addr = INADDR_ANY;
606103026Ssobomax		sc->g_dst.s_addr = INADDR_ANY;
607103026Ssobomax		goto recompute;
608103026Ssobomax	case SIOCGLIFPHYADDR:
609103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY ||
610103026Ssobomax		    sc->g_dst.s_addr == INADDR_ANY) {
611103026Ssobomax			error = EADDRNOTAVAIL;
612103026Ssobomax			break;
613103026Ssobomax		}
614103026Ssobomax		memset(&si, 0, sizeof(si));
615103026Ssobomax		si.sin_family = AF_INET;
616103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
617103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
618103026Ssobomax		memcpy(&lifr->addr, &si, sizeof(si));
619103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
620103026Ssobomax		memcpy(&lifr->dstaddr, &si, sizeof(si));
621103026Ssobomax		break;
622103026Ssobomax	case SIOCGIFPSRCADDR:
623103026Ssobomax		if (sc->g_src.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_src.s_addr;
631103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
632103026Ssobomax		break;
633103026Ssobomax	case SIOCGIFPDSTADDR:
634103026Ssobomax		if (sc->g_dst.s_addr == INADDR_ANY) {
635103026Ssobomax			error = EADDRNOTAVAIL;
636103026Ssobomax			break;
637103026Ssobomax		}
638103026Ssobomax		memset(&si, 0, sizeof(si));
639103026Ssobomax		si.sin_family = AF_INET;
640103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
641103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
642103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
643103026Ssobomax		break;
644103026Ssobomax	default:
645103026Ssobomax		error = EINVAL;
646103026Ssobomax		break;
647103026Ssobomax	}
648103026Ssobomax
649103026Ssobomax	splx(s);
650103026Ssobomax	return (error);
651103026Ssobomax}
652103026Ssobomax
653103026Ssobomax/*
654103026Ssobomax * computes a route to our destination that is not the one
655103026Ssobomax * which would be taken by ip_output(), as this one will loop back to
656103026Ssobomax * us. If the interface is p2p as  a--->b, then a routing entry exists
657103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here
658103026Ssobomax * gets src=a, dst=b tacked on and would from ip_ouput() sent back to
659103026Ssobomax * if_gre.
660103026Ssobomax * Goal here is to compute a route to b that is less specific than
661103026Ssobomax * a-->b. We know that this one exists as in normal operation we have
662103026Ssobomax * at least a default route which matches.
663103026Ssobomax */
664103032Ssobomaxstatic int
665103026Ssobomaxgre_compute_route(struct gre_softc *sc)
666103026Ssobomax{
667103026Ssobomax	struct route *ro;
668103026Ssobomax	u_int32_t a, b, c;
669103026Ssobomax
670103026Ssobomax	ro = &sc->route;
671103026Ssobomax
672103026Ssobomax	memset(ro, 0, sizeof(struct route));
673103026Ssobomax	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
674103026Ssobomax	ro->ro_dst.sa_family = AF_INET;
675103026Ssobomax	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
676103026Ssobomax
677103026Ssobomax	/*
678103026Ssobomax	 * toggle last bit, so our interface is not found, but a less
679103026Ssobomax	 * specific route. I'd rather like to specify a shorter mask,
680103026Ssobomax	 * but this is not possible. Should work though. XXX
681103026Ssobomax	 * there is a simpler way ...
682103026Ssobomax	 */
683103026Ssobomax	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
684103026Ssobomax		a = ntohl(sc->g_dst.s_addr);
685103026Ssobomax		b = a & 0x01;
686103026Ssobomax		c = a & 0xfffffffe;
687103026Ssobomax		b = b ^ 0x01;
688103026Ssobomax		a = b | c;
689103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
690103026Ssobomax		    = htonl(a);
691103026Ssobomax	}
692103026Ssobomax
693103026Ssobomax#ifdef DIAGNOSTIC
694103026Ssobomax	printf("%s: searching a route to %s", if_name(&sc->sc_if),
695103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
696103026Ssobomax#endif
697103026Ssobomax
698103026Ssobomax	rtalloc(ro);
699103026Ssobomax
700103026Ssobomax	/*
701103026Ssobomax	 * check if this returned a route at all and this route is no
702103026Ssobomax	 * recursion to ourself
703103026Ssobomax	 */
704103026Ssobomax	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
705103026Ssobomax#ifdef DIAGNOSTIC
706103026Ssobomax		if (ro->ro_rt == NULL)
707103026Ssobomax			printf(" - no route found!\n");
708103026Ssobomax		else
709103026Ssobomax			printf(" - route loops back to ourself!\n");
710103026Ssobomax#endif
711103026Ssobomax		return EADDRNOTAVAIL;
712103026Ssobomax	}
713103026Ssobomax
714103026Ssobomax	/*
715103026Ssobomax	 * now change it back - else ip_output will just drop
716103026Ssobomax	 * the route and search one to this interface ...
717103026Ssobomax	 */
718103026Ssobomax	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
719103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
720103026Ssobomax
721103026Ssobomax#ifdef DIAGNOSTIC
722103026Ssobomax	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
723103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
724103026Ssobomax	printf("\n");
725103026Ssobomax#endif
726103026Ssobomax
727103026Ssobomax	return 0;
728103026Ssobomax}
729103026Ssobomax
730103026Ssobomax/*
731103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on
732103026Ssobomax * mbufs.
733103026Ssobomax */
734103026Ssobomaxu_short
735103026Ssobomaxgre_in_cksum(u_short *p, u_int len)
736103026Ssobomax{
737103026Ssobomax	u_int sum = 0;
738103026Ssobomax	int nwords = len >> 1;
739103026Ssobomax
740103026Ssobomax	while (nwords-- != 0)
741103026Ssobomax		sum += *p++;
742103026Ssobomax
743103026Ssobomax	if (len & 1) {
744103026Ssobomax		union {
745103026Ssobomax			u_short w;
746103026Ssobomax			u_char c[2];
747103026Ssobomax		} u;
748103026Ssobomax		u.c[0] = *(u_char *)p;
749103026Ssobomax		u.c[1] = 0;
750103026Ssobomax		sum += u.w;
751103026Ssobomax	}
752103026Ssobomax
753103026Ssobomax	/* end-around-carry */
754103026Ssobomax	sum = (sum >> 16) + (sum & 0xffff);
755103026Ssobomax	sum += (sum >> 16);
756103026Ssobomax	return (~sum);
757103026Ssobomax}
758103026Ssobomax
759103026Ssobomaxstatic int
760103026Ssobomaxgremodevent(module_t mod, int type, void *data)
761103026Ssobomax{
762103026Ssobomax
763103026Ssobomax	switch (type) {
764103026Ssobomax	case MOD_LOAD:
765103026Ssobomax		greattach();
766103026Ssobomax		break;
767103026Ssobomax	case MOD_UNLOAD:
768103026Ssobomax		if_clone_detach(&gre_cloner);
769103026Ssobomax
770103026Ssobomax		while (!LIST_EMPTY(&gre_softc_list))
771103026Ssobomax			gre_clone_destroy(&LIST_FIRST(&gre_softc_list)->sc_if);
772103026Ssobomax		break;
773103026Ssobomax	}
774103026Ssobomax	return 0;
775103026Ssobomax}
776103026Ssobomax
777103026Ssobomaxstatic moduledata_t gre_mod = {
778103026Ssobomax	"if_gre",
779103026Ssobomax	gremodevent,
780103026Ssobomax	0
781103026Ssobomax};
782103026Ssobomax
783103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
784103026SsobomaxMODULE_VERSION(if_gre, 1);
785