if_gre.c revision 148887
1227569Sphilip/*	$NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2227569Sphilip/*	 $FreeBSD: head/sys/net/if_gre.c 148887 2005-08-09 10:20:02Z rwatson $ */
3227569Sphilip
4227569Sphilip/*-
5227569Sphilip * Copyright (c) 1998 The NetBSD Foundation, Inc.
6227569Sphilip * All rights reserved.
7227569Sphilip *
8227569Sphilip * This code is derived from software contributed to The NetBSD Foundation
9227569Sphilip * by Heiko W.Rupp <hwr@pilhuhn.de>
10227569Sphilip *
11227569Sphilip * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12227569Sphilip *
13227569Sphilip * Redistribution and use in source and binary forms, with or without
14227569Sphilip * modification, are permitted provided that the following conditions
15227569Sphilip * are met:
16227569Sphilip * 1. Redistributions of source code must retain the above copyright
17227569Sphilip *    notice, this list of conditions and the following disclaimer.
18227569Sphilip * 2. Redistributions in binary form must reproduce the above copyright
19227569Sphilip *    notice, this list of conditions and the following disclaimer in the
20227569Sphilip *    documentation and/or other materials provided with the distribution.
21227569Sphilip * 3. All advertising materials mentioning features or use of this software
22227569Sphilip *    must display the following acknowledgement:
23227569Sphilip *        This product includes software developed by the NetBSD
24227569Sphilip *        Foundation, Inc. and its contributors.
25227569Sphilip * 4. Neither the name of The NetBSD Foundation nor the names of its
26228078Sphilip *    contributors may be used to endorse or promote products derived
27228078Sphilip *    from this software without specific prior written permission.
28228078Sphilip *
29227569Sphilip * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30227569Sphilip * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31227569Sphilip * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32227569Sphilip * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33227569Sphilip * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34227569Sphilip * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35227569Sphilip * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36227569Sphilip * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37227569Sphilip * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38227569Sphilip * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39227569Sphilip * POSSIBILITY OF SUCH DAMAGE.
40227569Sphilip */
41227569Sphilip
42227569Sphilip/*
43227569Sphilip * Encapsulate L3 protocols into IP
44227569Sphilip * See RFC 2784 (successor of RFC 1701 and 1702) for more details.
45227569Sphilip * If_gre is compatible with Cisco GRE tunnels, so you can
46227569Sphilip * have a NetBSD box as the other end of a tunnel interface of a Cisco
47227569Sphilip * router. See gre(4) for more details.
48227569Sphilip * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
49227569Sphilip */
50227569Sphilip
51227569Sphilip#include "opt_atalk.h"
52227569Sphilip#include "opt_inet.h"
53227569Sphilip#include "opt_inet6.h"
54227569Sphilip
55227569Sphilip#include <sys/param.h>
56227569Sphilip#include <sys/kernel.h>
57227569Sphilip#include <sys/malloc.h>
58227569Sphilip#include <sys/module.h>
59227569Sphilip#include <sys/mbuf.h>
60227569Sphilip#include <sys/protosw.h>
61227569Sphilip#include <sys/socket.h>
62227569Sphilip#include <sys/sockio.h>
63227569Sphilip#include <sys/sysctl.h>
64227569Sphilip#include <sys/systm.h>
65227569Sphilip
66227569Sphilip#include <net/ethernet.h>
67227569Sphilip#include <net/if.h>
68227569Sphilip#include <net/if_clone.h>
69227569Sphilip#include <net/if_types.h>
70227569Sphilip#include <net/route.h>
71227569Sphilip
72227569Sphilip#ifdef INET
73227569Sphilip#include <netinet/in.h>
74227569Sphilip#include <netinet/in_systm.h>
75227569Sphilip#include <netinet/in_var.h>
76227569Sphilip#include <netinet/ip.h>
77227569Sphilip#include <netinet/ip_gre.h>
78227569Sphilip#include <netinet/ip_var.h>
79227569Sphilip#include <netinet/ip_encap.h>
80227569Sphilip#else
81227569Sphilip#error "Huh? if_gre without inet?"
82227569Sphilip#endif
83227569Sphilip
84227569Sphilip#include <net/bpf.h>
85227569Sphilip
86227569Sphilip#include <net/net_osdep.h>
87227569Sphilip#include <net/if_gre.h>
88227569Sphilip
89227569Sphilip/*
90227569Sphilip * It is not easy to calculate the right value for a GRE MTU.
91227569Sphilip * We leave this task to the admin and use the same default that
92227569Sphilip * other vendors use.
93227569Sphilip */
94227569Sphilip#define GREMTU	1476
95227569Sphilip
96227569Sphilip#define GRENAME	"gre"
97227569Sphilip
98227569Sphilip/*
99227569Sphilip * gre_mtx protects all global variables in if_gre.c.
100227569Sphilip * XXX: gre_softc data not protected yet.
101227569Sphilip */
102227569Sphilipstruct mtx gre_mtx;
103227569Sphilipstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
104227569Sphilip
105227569Sphilipstruct gre_softc_head gre_softc_list;
106227569Sphilip
107227569Sphilipstatic int	gre_clone_create(struct if_clone *, int);
108227569Sphilipstatic void	gre_clone_destroy(struct ifnet *);
109227569Sphilipstatic int	gre_ioctl(struct ifnet *, u_long, caddr_t);
110227569Sphilipstatic int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
111227569Sphilip		    struct rtentry *rt);
112227569Sphilip
113227569SphilipIFC_SIMPLE_DECLARE(gre, 0);
114227569Sphilip
115227569Sphilipstatic int gre_compute_route(struct gre_softc *sc);
116227569Sphilip
117227569Sphilipstatic void	greattach(void);
118227569Sphilip
119227569Sphilip#ifdef INET
120227569Sphilipextern struct domain inetdomain;
121227569Sphilipstatic const struct protosw in_gre_protosw =
122227569Sphilip{ SOCK_RAW,     &inetdomain,    IPPROTO_GRE,    PR_ATOMIC|PR_ADDR,
123227569Sphilip  (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
124227569Sphilip  0,
125227569Sphilip  0,		0,		0,		0,
126227569Sphilip  &rip_usrreqs
127227569Sphilip};
128227569Sphilipstatic const struct protosw in_mobile_protosw =
129227569Sphilip{ SOCK_RAW,     &inetdomain,    IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR,
130227569Sphilip  (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
131227569Sphilip  0,
132227569Sphilip  0,		0,		0,		0,
133227569Sphilip  &rip_usrreqs
134227569Sphilip};
135227569Sphilip#endif
136227569Sphilip
137227569SphilipSYSCTL_DECL(_net_link);
138227569SphilipSYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
139227569Sphilip    "Generic Routing Encapsulation");
140227569Sphilip#ifndef MAX_GRE_NEST
141227569Sphilip/*
142227569Sphilip * This macro controls the default upper limitation on nesting of gre tunnels.
143227569Sphilip * Since, setting a large value to this macro with a careless configuration
144227569Sphilip * may introduce system crash, we don't allow any nestings by default.
145227569Sphilip * If you need to configure nested gre tunnels, you can define this macro
146227569Sphilip * in your kernel configuration file.  However, if you do so, please be
147227569Sphilip * careful to configure the tunnels so that it won't make a loop.
148227569Sphilip */
149227569Sphilip#define MAX_GRE_NEST 1
150227569Sphilip#endif
151227569Sphilipstatic int max_gre_nesting = MAX_GRE_NEST;
152227569SphilipSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
153227569Sphilip    &max_gre_nesting, 0, "Max nested tunnels");
154227569Sphilip
155227569Sphilip/* ARGSUSED */
156227569Sphilipstatic void
157227569Sphilipgreattach(void)
158227569Sphilip{
159227569Sphilip
160227569Sphilip	mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
161227569Sphilip	LIST_INIT(&gre_softc_list);
162227569Sphilip	if_clone_attach(&gre_cloner);
163227569Sphilip}
164227569Sphilip
165227569Sphilipstatic int
166227569Sphilipgre_clone_create(ifc, unit)
167227569Sphilip	struct if_clone *ifc;
168227569Sphilip	int unit;
169227569Sphilip{
170227569Sphilip	struct gre_softc *sc;
171227569Sphilip
172227569Sphilip	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
173227569Sphilip
174227569Sphilip	GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
175227569Sphilip	if (GRE2IFP(sc) == NULL) {
176227569Sphilip		free(sc, M_GRE);
177227569Sphilip		return (ENOSPC);
178227569Sphilip	}
179227569Sphilip
180227569Sphilip	GRE2IFP(sc)->if_softc = sc;
181227569Sphilip	if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
182227569Sphilip
183227569Sphilip	GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
184227569Sphilip	GRE2IFP(sc)->if_addrlen = 0;
185227569Sphilip	GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
186227569Sphilip	GRE2IFP(sc)->if_mtu = GREMTU;
187227569Sphilip	GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
188227569Sphilip	GRE2IFP(sc)->if_output = gre_output;
189227569Sphilip	GRE2IFP(sc)->if_ioctl = gre_ioctl;
190227569Sphilip	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
191227569Sphilip	sc->g_proto = IPPROTO_GRE;
192227569Sphilip	GRE2IFP(sc)->if_flags |= IFF_LINK0;
193227569Sphilip	sc->encap = NULL;
194227569Sphilip	sc->called = 0;
195227569Sphilip	sc->wccp_ver = WCCP_V1;
196227569Sphilip	if_attach(GRE2IFP(sc));
197227569Sphilip	bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
198227569Sphilip	mtx_lock(&gre_mtx);
199227569Sphilip	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
200227569Sphilip	mtx_unlock(&gre_mtx);
201227569Sphilip	return (0);
202227569Sphilip}
203227569Sphilip
204227569Sphilipstatic void
205227569Sphilipgre_destroy(struct gre_softc *sc)
206227569Sphilip{
207227569Sphilip
208227569Sphilip#ifdef INET
209227569Sphilip	if (sc->encap != NULL)
210227569Sphilip		encap_detach(sc->encap);
211227569Sphilip#endif
212227569Sphilip	bpfdetach(GRE2IFP(sc));
213227569Sphilip	if_detach(GRE2IFP(sc));
214227569Sphilip	if_free(GRE2IFP(sc));
215227569Sphilip	free(sc, M_GRE);
216227569Sphilip}
217227569Sphilip
218227569Sphilipstatic void
219227569Sphilipgre_clone_destroy(ifp)
220227569Sphilip	struct ifnet *ifp;
221227569Sphilip{
222227569Sphilip	struct gre_softc *sc = ifp->if_softc;
223227569Sphilip
224227569Sphilip	mtx_lock(&gre_mtx);
225227569Sphilip	LIST_REMOVE(sc, sc_list);
226227569Sphilip	mtx_unlock(&gre_mtx);
227227569Sphilip	gre_destroy(sc);
228227569Sphilip}
229227569Sphilip
230227569Sphilip/*
231227569Sphilip * The output routine. Takes a packet and encapsulates it in the protocol
232227569Sphilip * given by sc->g_proto. See also RFC 1701 and RFC 2004
233227569Sphilip */
234227569Sphilipstatic int
235227569Sphilipgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
236227569Sphilip	   struct rtentry *rt)
237227569Sphilip{
238227569Sphilip	int error = 0;
239227569Sphilip	struct gre_softc *sc = ifp->if_softc;
240227569Sphilip	struct greip *gh;
241227569Sphilip	struct ip *ip;
242227569Sphilip	u_short ip_id = 0;
243227569Sphilip	uint8_t ip_tos = 0;
244227569Sphilip	u_int16_t etype = 0;
245227569Sphilip	struct mobile_h mob_h;
246227569Sphilip	u_int32_t af;
247227569Sphilip
248227569Sphilip	/*
249227569Sphilip	 * gre may cause infinite recursion calls when misconfigured.
250227569Sphilip	 * We'll prevent this by introducing upper limit.
251227569Sphilip	 */
252227569Sphilip	if (++(sc->called) > max_gre_nesting) {
253227569Sphilip		printf("%s: gre_output: recursively called too many "
254227569Sphilip		       "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
255227569Sphilip		m_freem(m);
256227569Sphilip		error = EIO;    /* is there better errno? */
257227569Sphilip		goto end;
258227569Sphilip	}
259227569Sphilip
260227569Sphilip	if (!((ifp->if_flags & IFF_UP) &&
261227569Sphilip	    (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
262227569Sphilip	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
263227569Sphilip		m_freem(m);
264227569Sphilip		error = ENETDOWN;
265227569Sphilip		goto end;
266227569Sphilip	}
267227569Sphilip
268227569Sphilip	gh = NULL;
269227569Sphilip	ip = NULL;
270227569Sphilip
271227569Sphilip	/* BPF writes need to be handled specially. */
272227569Sphilip	if (dst->sa_family == AF_UNSPEC) {
273227569Sphilip		bcopy(dst->sa_data, &af, sizeof(af));
274227569Sphilip		dst->sa_family = af;
275227569Sphilip	}
276227569Sphilip
277227569Sphilip	if (ifp->if_bpf) {
278227569Sphilip		af = dst->sa_family;
279227569Sphilip		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
280227569Sphilip	}
281227569Sphilip
282227569Sphilip	m->m_flags &= ~(M_BCAST|M_MCAST);
283227569Sphilip
284227569Sphilip	if (sc->g_proto == IPPROTO_MOBILE) {
285227569Sphilip		if (dst->sa_family == AF_INET) {
286227569Sphilip			struct mbuf *m0;
287227569Sphilip			int msiz;
288227569Sphilip
289227569Sphilip			ip = mtod(m, struct ip *);
290227569Sphilip
291227569Sphilip			/*
292227569Sphilip			 * RFC2004 specifies that fragmented diagrams shouldn't
293227569Sphilip			 * be encapsulated.
294227569Sphilip			 */
295227569Sphilip			if ((ip->ip_off & IP_MF) != 0) {
296227569Sphilip				_IF_DROP(&ifp->if_snd);
297227569Sphilip				m_freem(m);
298227569Sphilip				error = EINVAL;    /* is there better errno? */
299227569Sphilip				goto end;
300227569Sphilip			}
301227569Sphilip			memset(&mob_h, 0, MOB_H_SIZ_L);
302227569Sphilip			mob_h.proto = (ip->ip_p) << 8;
303227569Sphilip			mob_h.odst = ip->ip_dst.s_addr;
304227569Sphilip			ip->ip_dst.s_addr = sc->g_dst.s_addr;
305227569Sphilip
306227569Sphilip			/*
307227569Sphilip			 * If the packet comes from our host, we only change
308227569Sphilip			 * the destination address in the IP header.
309227569Sphilip			 * Else we also need to save and change the source
310227569Sphilip			 */
311227569Sphilip			if (in_hosteq(ip->ip_src, sc->g_src)) {
312227569Sphilip				msiz = MOB_H_SIZ_S;
313227569Sphilip			} else {
314227569Sphilip				mob_h.proto |= MOB_H_SBIT;
315227569Sphilip				mob_h.osrc = ip->ip_src.s_addr;
316227569Sphilip				ip->ip_src.s_addr = sc->g_src.s_addr;
317227569Sphilip				msiz = MOB_H_SIZ_L;
318227569Sphilip			}
319227569Sphilip			mob_h.proto = htons(mob_h.proto);
320227569Sphilip			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
321227569Sphilip
322227569Sphilip			if ((m->m_data - msiz) < m->m_pktdat) {
323227569Sphilip				/* need new mbuf */
324227569Sphilip				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
325227569Sphilip				if (m0 == NULL) {
326227569Sphilip					_IF_DROP(&ifp->if_snd);
327227569Sphilip					m_freem(m);
328227569Sphilip					error = ENOBUFS;
329227569Sphilip					goto end;
330227569Sphilip				}
331227569Sphilip				m0->m_next = m;
332227569Sphilip				m->m_data += sizeof(struct ip);
333227569Sphilip				m->m_len -= sizeof(struct ip);
334227569Sphilip				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
335227569Sphilip				m0->m_len = msiz + sizeof(struct ip);
336227569Sphilip				m0->m_data += max_linkhdr;
337227569Sphilip				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
338227569Sphilip				       sizeof(struct ip));
339227569Sphilip				m = m0;
340227569Sphilip			} else {  /* we have some space left in the old one */
341227569Sphilip				m->m_data -= msiz;
342227569Sphilip				m->m_len += msiz;
343227569Sphilip				m->m_pkthdr.len += msiz;
344227569Sphilip				bcopy(ip, mtod(m, caddr_t),
345227569Sphilip					sizeof(struct ip));
346227569Sphilip			}
347227569Sphilip			ip = mtod(m, struct ip *);
348227569Sphilip			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
349227569Sphilip			ip->ip_len = ntohs(ip->ip_len) + msiz;
350227569Sphilip		} else {  /* AF_INET */
351227569Sphilip			_IF_DROP(&ifp->if_snd);
352227569Sphilip			m_freem(m);
353227569Sphilip			error = EINVAL;
354227569Sphilip			goto end;
355227569Sphilip		}
356227569Sphilip	} else if (sc->g_proto == IPPROTO_GRE) {
357227569Sphilip		switch (dst->sa_family) {
358		case AF_INET:
359			ip = mtod(m, struct ip *);
360			ip_tos = ip->ip_tos;
361			ip_id = ip->ip_id;
362			etype = ETHERTYPE_IP;
363			break;
364#ifdef INET6
365		case AF_INET6:
366			ip_id = ip_newid();
367			etype = ETHERTYPE_IPV6;
368			break;
369#endif
370#ifdef NETATALK
371		case AF_APPLETALK:
372			etype = ETHERTYPE_ATALK;
373			break;
374#endif
375		default:
376			_IF_DROP(&ifp->if_snd);
377			m_freem(m);
378			error = EAFNOSUPPORT;
379			goto end;
380		}
381		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
382	} else {
383		_IF_DROP(&ifp->if_snd);
384		m_freem(m);
385		error = EINVAL;
386		goto end;
387	}
388
389	if (m == NULL) {	/* mbuf allocation failed */
390		_IF_DROP(&ifp->if_snd);
391		error = ENOBUFS;
392		goto end;
393	}
394
395	gh = mtod(m, struct greip *);
396	if (sc->g_proto == IPPROTO_GRE) {
397		/* we don't have any GRE flags for now */
398		memset((void *)gh, 0, sizeof(struct greip));
399		gh->gi_ptype = htons(etype);
400	}
401
402	gh->gi_pr = sc->g_proto;
403	if (sc->g_proto != IPPROTO_MOBILE) {
404		gh->gi_src = sc->g_src;
405		gh->gi_dst = sc->g_dst;
406		((struct ip*)gh)->ip_v = IPPROTO_IPV4;
407		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
408		((struct ip*)gh)->ip_ttl = GRE_TTL;
409		((struct ip*)gh)->ip_tos = ip_tos;
410		((struct ip*)gh)->ip_id = ip_id;
411		gh->gi_len = m->m_pkthdr.len;
412	}
413
414	ifp->if_opackets++;
415	ifp->if_obytes += m->m_pkthdr.len;
416	/*
417	 * Send it off and with IP_FORWARD flag to prevent it from
418	 * overwriting the ip_id again.  ip_id is already set to the
419	 * ip_id of the encapsulated packet.
420	 */
421	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
422	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
423  end:
424	sc->called = 0;
425	if (error)
426		ifp->if_oerrors++;
427	return (error);
428}
429
430static int
431gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
432{
433	struct ifreq *ifr = (struct ifreq *)data;
434	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
435	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
436	struct gre_softc *sc = ifp->if_softc;
437	int s;
438	struct sockaddr_in si;
439	struct sockaddr *sa = NULL;
440	int error;
441	struct sockaddr_in sp, sm, dp, dm;
442
443	error = 0;
444
445	s = splnet();
446	switch (cmd) {
447	case SIOCSIFADDR:
448		ifp->if_flags |= IFF_UP;
449		break;
450	case SIOCSIFDSTADDR:
451		break;
452	case SIOCSIFFLAGS:
453		if ((error = suser(curthread)) != 0)
454			break;
455		if ((ifr->ifr_flags & IFF_LINK0) != 0)
456			sc->g_proto = IPPROTO_GRE;
457		else
458			sc->g_proto = IPPROTO_MOBILE;
459		if ((ifr->ifr_flags & IFF_LINK2) != 0)
460			sc->wccp_ver = WCCP_V2;
461		else
462			sc->wccp_ver = WCCP_V1;
463		goto recompute;
464	case SIOCSIFMTU:
465		if ((error = suser(curthread)) != 0)
466			break;
467		if (ifr->ifr_mtu < 576) {
468			error = EINVAL;
469			break;
470		}
471		ifp->if_mtu = ifr->ifr_mtu;
472		break;
473	case SIOCGIFMTU:
474		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
475		break;
476	case SIOCADDMULTI:
477	case SIOCDELMULTI:
478		if ((error = suser(curthread)) != 0)
479			break;
480		if (ifr == 0) {
481			error = EAFNOSUPPORT;
482			break;
483		}
484		switch (ifr->ifr_addr.sa_family) {
485#ifdef INET
486		case AF_INET:
487			break;
488#endif
489#ifdef INET6
490		case AF_INET6:
491			break;
492#endif
493		default:
494			error = EAFNOSUPPORT;
495			break;
496		}
497		break;
498	case GRESPROTO:
499		if ((error = suser(curthread)) != 0)
500			break;
501		sc->g_proto = ifr->ifr_flags;
502		switch (sc->g_proto) {
503		case IPPROTO_GRE:
504			ifp->if_flags |= IFF_LINK0;
505			break;
506		case IPPROTO_MOBILE:
507			ifp->if_flags &= ~IFF_LINK0;
508			break;
509		default:
510			error = EPROTONOSUPPORT;
511			break;
512		}
513		goto recompute;
514	case GREGPROTO:
515		ifr->ifr_flags = sc->g_proto;
516		break;
517	case GRESADDRS:
518	case GRESADDRD:
519		if ((error = suser(curthread)) != 0)
520			break;
521		/*
522		 * set tunnel endpoints, compute a less specific route
523		 * to the remote end and mark if as up
524		 */
525		sa = &ifr->ifr_addr;
526		if (cmd == GRESADDRS)
527			sc->g_src = (satosin(sa))->sin_addr;
528		if (cmd == GRESADDRD)
529			sc->g_dst = (satosin(sa))->sin_addr;
530	recompute:
531#ifdef INET
532		if (sc->encap != NULL) {
533			encap_detach(sc->encap);
534			sc->encap = NULL;
535		}
536#endif
537		if ((sc->g_src.s_addr != INADDR_ANY) &&
538		    (sc->g_dst.s_addr != INADDR_ANY)) {
539			bzero(&sp, sizeof(sp));
540			bzero(&sm, sizeof(sm));
541			bzero(&dp, sizeof(dp));
542			bzero(&dm, sizeof(dm));
543			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
544			    sizeof(struct sockaddr_in);
545			sp.sin_family = sm.sin_family = dp.sin_family =
546			    dm.sin_family = AF_INET;
547			sp.sin_addr = sc->g_src;
548			dp.sin_addr = sc->g_dst;
549			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
550			    INADDR_BROADCAST;
551#ifdef INET
552			sc->encap = encap_attach(AF_INET, sc->g_proto,
553			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
554			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
555				&in_gre_protosw : &in_mobile_protosw, sc);
556			if (sc->encap == NULL)
557				printf("%s: unable to attach encap\n",
558				    if_name(GRE2IFP(sc)));
559#endif
560			if (sc->route.ro_rt != 0) /* free old route */
561				RTFREE(sc->route.ro_rt);
562			if (gre_compute_route(sc) == 0)
563				ifp->if_drv_flags |= IFF_DRV_RUNNING;
564			else
565				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
566		}
567		break;
568	case GREGADDRS:
569		memset(&si, 0, sizeof(si));
570		si.sin_family = AF_INET;
571		si.sin_len = sizeof(struct sockaddr_in);
572		si.sin_addr.s_addr = sc->g_src.s_addr;
573		sa = sintosa(&si);
574		ifr->ifr_addr = *sa;
575		break;
576	case GREGADDRD:
577		memset(&si, 0, sizeof(si));
578		si.sin_family = AF_INET;
579		si.sin_len = sizeof(struct sockaddr_in);
580		si.sin_addr.s_addr = sc->g_dst.s_addr;
581		sa = sintosa(&si);
582		ifr->ifr_addr = *sa;
583		break;
584	case SIOCSIFPHYADDR:
585		if ((error = suser(curthread)) != 0)
586			break;
587		if (aifr->ifra_addr.sin_family != AF_INET ||
588		    aifr->ifra_dstaddr.sin_family != AF_INET) {
589			error = EAFNOSUPPORT;
590			break;
591		}
592		if (aifr->ifra_addr.sin_len != sizeof(si) ||
593		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
594			error = EINVAL;
595			break;
596		}
597		sc->g_src = aifr->ifra_addr.sin_addr;
598		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
599		goto recompute;
600	case SIOCSLIFPHYADDR:
601		if ((error = suser(curthread)) != 0)
602			break;
603		if (lifr->addr.ss_family != AF_INET ||
604		    lifr->dstaddr.ss_family != AF_INET) {
605			error = EAFNOSUPPORT;
606			break;
607		}
608		if (lifr->addr.ss_len != sizeof(si) ||
609		    lifr->dstaddr.ss_len != sizeof(si)) {
610			error = EINVAL;
611			break;
612		}
613		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
614		sc->g_dst =
615		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
616		goto recompute;
617	case SIOCDIFPHYADDR:
618		if ((error = suser(curthread)) != 0)
619			break;
620		sc->g_src.s_addr = INADDR_ANY;
621		sc->g_dst.s_addr = INADDR_ANY;
622		goto recompute;
623	case SIOCGLIFPHYADDR:
624		if (sc->g_src.s_addr == INADDR_ANY ||
625		    sc->g_dst.s_addr == INADDR_ANY) {
626			error = EADDRNOTAVAIL;
627			break;
628		}
629		memset(&si, 0, sizeof(si));
630		si.sin_family = AF_INET;
631		si.sin_len = sizeof(struct sockaddr_in);
632		si.sin_addr.s_addr = sc->g_src.s_addr;
633		memcpy(&lifr->addr, &si, sizeof(si));
634		si.sin_addr.s_addr = sc->g_dst.s_addr;
635		memcpy(&lifr->dstaddr, &si, sizeof(si));
636		break;
637	case SIOCGIFPSRCADDR:
638#ifdef INET6
639	case SIOCGIFPSRCADDR_IN6:
640#endif
641		if (sc->g_src.s_addr == INADDR_ANY) {
642			error = EADDRNOTAVAIL;
643			break;
644		}
645		memset(&si, 0, sizeof(si));
646		si.sin_family = AF_INET;
647		si.sin_len = sizeof(struct sockaddr_in);
648		si.sin_addr.s_addr = sc->g_src.s_addr;
649		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
650		break;
651	case SIOCGIFPDSTADDR:
652#ifdef INET6
653	case SIOCGIFPDSTADDR_IN6:
654#endif
655		if (sc->g_dst.s_addr == INADDR_ANY) {
656			error = EADDRNOTAVAIL;
657			break;
658		}
659		memset(&si, 0, sizeof(si));
660		si.sin_family = AF_INET;
661		si.sin_len = sizeof(struct sockaddr_in);
662		si.sin_addr.s_addr = sc->g_dst.s_addr;
663		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
664		break;
665	default:
666		error = EINVAL;
667		break;
668	}
669
670	splx(s);
671	return (error);
672}
673
674/*
675 * computes a route to our destination that is not the one
676 * which would be taken by ip_output(), as this one will loop back to
677 * us. If the interface is p2p as  a--->b, then a routing entry exists
678 * If we now send a packet to b (e.g. ping b), this will come down here
679 * gets src=a, dst=b tacked on and would from ip_output() sent back to
680 * if_gre.
681 * Goal here is to compute a route to b that is less specific than
682 * a-->b. We know that this one exists as in normal operation we have
683 * at least a default route which matches.
684 */
685static int
686gre_compute_route(struct gre_softc *sc)
687{
688	struct route *ro;
689	u_int32_t a, b, c;
690
691	ro = &sc->route;
692
693	memset(ro, 0, sizeof(struct route));
694	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
695	ro->ro_dst.sa_family = AF_INET;
696	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
697
698	/*
699	 * toggle last bit, so our interface is not found, but a less
700	 * specific route. I'd rather like to specify a shorter mask,
701	 * but this is not possible. Should work though. XXX
702	 * there is a simpler way ...
703	 */
704	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
705		a = ntohl(sc->g_dst.s_addr);
706		b = a & 0x01;
707		c = a & 0xfffffffe;
708		b = b ^ 0x01;
709		a = b | c;
710		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
711		    = htonl(a);
712	}
713
714#ifdef DIAGNOSTIC
715	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
716	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
717#endif
718
719	rtalloc(ro);
720
721	/*
722	 * check if this returned a route at all and this route is no
723	 * recursion to ourself
724	 */
725	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
726#ifdef DIAGNOSTIC
727		if (ro->ro_rt == NULL)
728			printf(" - no route found!\n");
729		else
730			printf(" - route loops back to ourself!\n");
731#endif
732		return EADDRNOTAVAIL;
733	}
734
735	/*
736	 * now change it back - else ip_output will just drop
737	 * the route and search one to this interface ...
738	 */
739	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
740		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
741
742#ifdef DIAGNOSTIC
743	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
744	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
745	printf("\n");
746#endif
747
748	return 0;
749}
750
751/*
752 * do a checksum of a buffer - much like in_cksum, which operates on
753 * mbufs.
754 */
755u_int16_t
756gre_in_cksum(u_int16_t *p, u_int len)
757{
758	u_int32_t sum = 0;
759	int nwords = len >> 1;
760
761	while (nwords-- != 0)
762		sum += *p++;
763
764	if (len & 1) {
765		union {
766			u_short w;
767			u_char c[2];
768		} u;
769		u.c[0] = *(u_char *)p;
770		u.c[1] = 0;
771		sum += u.w;
772	}
773
774	/* end-around-carry */
775	sum = (sum >> 16) + (sum & 0xffff);
776	sum += (sum >> 16);
777	return (~sum);
778}
779
780static int
781gremodevent(module_t mod, int type, void *data)
782{
783	struct gre_softc *sc;
784
785	switch (type) {
786	case MOD_LOAD:
787		greattach();
788		break;
789	case MOD_UNLOAD:
790		if_clone_detach(&gre_cloner);
791
792		mtx_lock(&gre_mtx);
793		while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) {
794			LIST_REMOVE(sc, sc_list);
795			mtx_unlock(&gre_mtx);
796			gre_destroy(sc);
797			mtx_lock(&gre_mtx);
798		}
799		mtx_unlock(&gre_mtx);
800		mtx_destroy(&gre_mtx);
801		break;
802	default:
803		return EOPNOTSUPP;
804	}
805	return 0;
806}
807
808static moduledata_t gre_mod = {
809	"if_gre",
810	gremodevent,
811	0
812};
813
814DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
815MODULE_VERSION(if_gre, 1);
816