if_gre.c revision 147256
1123992Ssobomax/*	$NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
2103026Ssobomax/*	 $FreeBSD: head/sys/net/if_gre.c 147256 2005-06-10 16:49:24Z brooks $ */
3103026Ssobomax
4139823Simp/*-
5103026Ssobomax * Copyright (c) 1998 The NetBSD Foundation, Inc.
6103026Ssobomax * All rights reserved.
7103026Ssobomax *
8103026Ssobomax * This code is derived from software contributed to The NetBSD Foundation
9103026Ssobomax * by Heiko W.Rupp <hwr@pilhuhn.de>
10103026Ssobomax *
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>
56129880Sphk#include <sys/module.h>
57103026Ssobomax#include <sys/mbuf.h>
58103026Ssobomax#include <sys/protosw.h>
59103026Ssobomax#include <sys/socket.h>
60103026Ssobomax#include <sys/sockio.h>
61103026Ssobomax#include <sys/sysctl.h>
62103344Sbde#include <sys/systm.h>
63103026Ssobomax
64103026Ssobomax#include <net/ethernet.h>
65103026Ssobomax#include <net/if.h>
66130933Sbrooks#include <net/if_clone.h>
67103026Ssobomax#include <net/if_types.h>
68103026Ssobomax#include <net/route.h>
69103026Ssobomax
70103026Ssobomax#ifdef INET
71103026Ssobomax#include <netinet/in.h>
72103026Ssobomax#include <netinet/in_systm.h>
73103026Ssobomax#include <netinet/in_var.h>
74103026Ssobomax#include <netinet/ip.h>
75103026Ssobomax#include <netinet/ip_gre.h>
76103026Ssobomax#include <netinet/ip_var.h>
77103026Ssobomax#include <netinet/ip_encap.h>
78103026Ssobomax#else
79103026Ssobomax#error "Huh? if_gre without inet?"
80103026Ssobomax#endif
81103026Ssobomax
82103026Ssobomax#include <net/bpf.h>
83103026Ssobomax
84103026Ssobomax#include <net/net_osdep.h>
85103026Ssobomax#include <net/if_gre.h>
86103026Ssobomax
87103026Ssobomax/*
88103026Ssobomax * It is not easy to calculate the right value for a GRE MTU.
89103026Ssobomax * We leave this task to the admin and use the same default that
90103026Ssobomax * other vendors use.
91103026Ssobomax */
92103026Ssobomax#define GREMTU	1476
93103026Ssobomax
94103026Ssobomax#define GRENAME	"gre"
95103026Ssobomax
96127307Srwatson/*
97127307Srwatson * gre_mtx protects all global variables in if_gre.c.
98127307Srwatson * XXX: gre_softc data not protected yet.
99127307Srwatson */
100127307Srwatsonstruct mtx gre_mtx;
101103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
102103026Ssobomax
103103026Ssobomaxstruct gre_softc_head gre_softc_list;
104103026Ssobomax
105105300Salfredstatic int	gre_clone_create(struct if_clone *, int);
106105300Salfredstatic void	gre_clone_destroy(struct ifnet *);
107103032Ssobomaxstatic int	gre_ioctl(struct ifnet *, u_long, caddr_t);
108103032Ssobomaxstatic int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
109103032Ssobomax		    struct rtentry *rt);
110103026Ssobomax
111130933SbrooksIFC_SIMPLE_DECLARE(gre, 0);
112103026Ssobomax
113103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc);
114103026Ssobomax
115105300Salfredstatic void	greattach(void);
116103026Ssobomax
117103026Ssobomax#ifdef INET
118103026Ssobomaxextern struct domain inetdomain;
119103026Ssobomaxstatic const struct protosw in_gre_protosw =
120103026Ssobomax{ SOCK_RAW,     &inetdomain,    IPPROTO_GRE,    PR_ATOMIC|PR_ADDR,
121103026Ssobomax  (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
122103026Ssobomax  0,
123103026Ssobomax  0,		0,		0,		0,
124103026Ssobomax  &rip_usrreqs
125103026Ssobomax};
126103026Ssobomaxstatic const struct protosw in_mobile_protosw =
127103026Ssobomax{ SOCK_RAW,     &inetdomain,    IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR,
128103026Ssobomax  (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
129103026Ssobomax  0,
130103026Ssobomax  0,		0,		0,		0,
131103026Ssobomax  &rip_usrreqs
132103026Ssobomax};
133103026Ssobomax#endif
134103026Ssobomax
135103026SsobomaxSYSCTL_DECL(_net_link);
136123338SbmsSYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
137103026Ssobomax    "Generic Routing Encapsulation");
138103026Ssobomax#ifndef MAX_GRE_NEST
139103026Ssobomax/*
140103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels.
141103026Ssobomax * Since, setting a large value to this macro with a careless configuration
142103026Ssobomax * may introduce system crash, we don't allow any nestings by default.
143103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro
144103026Ssobomax * in your kernel configuration file.  However, if you do so, please be
145103026Ssobomax * careful to configure the tunnels so that it won't make a loop.
146103026Ssobomax */
147103026Ssobomax#define MAX_GRE_NEST 1
148103026Ssobomax#endif
149103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST;
150103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
151103026Ssobomax    &max_gre_nesting, 0, "Max nested tunnels");
152103026Ssobomax
153103026Ssobomax/* ARGSUSED */
154103032Ssobomaxstatic void
155103026Ssobomaxgreattach(void)
156103026Ssobomax{
157103026Ssobomax
158127307Srwatson	mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
159103026Ssobomax	LIST_INIT(&gre_softc_list);
160103026Ssobomax	if_clone_attach(&gre_cloner);
161103026Ssobomax}
162103026Ssobomax
163103032Ssobomaxstatic int
164103026Ssobomaxgre_clone_create(ifc, unit)
165103026Ssobomax	struct if_clone *ifc;
166103026Ssobomax	int unit;
167103026Ssobomax{
168103026Ssobomax	struct gre_softc *sc;
169103026Ssobomax
170131673Sbms	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
171103026Ssobomax
172147256Sbrooks	if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
173147256Sbrooks	GRE2IFP(sc)->if_softc = sc;
174147256Sbrooks	GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
175147256Sbrooks	GRE2IFP(sc)->if_type = IFT_TUNNEL;
176147256Sbrooks	GRE2IFP(sc)->if_addrlen = 0;
177147256Sbrooks	GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
178147256Sbrooks	GRE2IFP(sc)->if_mtu = GREMTU;
179147256Sbrooks	GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
180147256Sbrooks	GRE2IFP(sc)->if_output = gre_output;
181147256Sbrooks	GRE2IFP(sc)->if_ioctl = gre_ioctl;
182103026Ssobomax	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
183103026Ssobomax	sc->g_proto = IPPROTO_GRE;
184147256Sbrooks	GRE2IFP(sc)->if_flags |= IFF_LINK0;
185103026Ssobomax	sc->encap = NULL;
186103026Ssobomax	sc->called = 0;
187125024Ssobomax	sc->wccp_ver = WCCP_V1;
188147256Sbrooks	if_attach(GRE2IFP(sc));
189147256Sbrooks	bpfattach(GRE2IFP(sc), 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
204147256Sbrooks	bpfdetach(GRE2IFP(sc));
205147256Sbrooks	if_detach(GRE2IFP(sc));
206147256Sbrooks	if_free(GRE2IFP(sc));
207103026Ssobomax	free(sc, M_GRE);
208103026Ssobomax}
209103026Ssobomax
210127307Srwatsonstatic void
211127307Srwatsongre_clone_destroy(ifp)
212127307Srwatson	struct ifnet *ifp;
213127307Srwatson{
214127307Srwatson	struct gre_softc *sc = ifp->if_softc;
215127307Srwatson
216127307Srwatson	mtx_lock(&gre_mtx);
217127307Srwatson	LIST_REMOVE(sc, sc_list);
218127307Srwatson	mtx_unlock(&gre_mtx);
219127307Srwatson	gre_destroy(sc);
220127307Srwatson}
221127307Srwatson
222103026Ssobomax/*
223103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol
224103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004
225103026Ssobomax */
226103032Ssobomaxstatic int
227103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
228103026Ssobomax	   struct rtentry *rt)
229103026Ssobomax{
230103026Ssobomax	int error = 0;
231103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
232103026Ssobomax	struct greip *gh;
233103026Ssobomax	struct ip *ip;
234123992Ssobomax	u_int16_t etype = 0;
235103026Ssobomax	struct mobile_h mob_h;
236103026Ssobomax
237103026Ssobomax	/*
238103026Ssobomax	 * gre may cause infinite recursion calls when misconfigured.
239103026Ssobomax	 * We'll prevent this by introducing upper limit.
240103026Ssobomax	 */
241103026Ssobomax	if (++(sc->called) > max_gre_nesting) {
242103026Ssobomax		printf("%s: gre_output: recursively called too many "
243147256Sbrooks		       "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
244103026Ssobomax		m_freem(m);
245103026Ssobomax		error = EIO;    /* is there better errno? */
246103026Ssobomax		goto end;
247103026Ssobomax	}
248103026Ssobomax
249103026Ssobomax	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
250103026Ssobomax	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
251103026Ssobomax		m_freem(m);
252103026Ssobomax		error = ENETDOWN;
253103026Ssobomax		goto end;
254103026Ssobomax	}
255103026Ssobomax
256103026Ssobomax	gh = NULL;
257103026Ssobomax	ip = NULL;
258103026Ssobomax
259103026Ssobomax	if (ifp->if_bpf) {
260103026Ssobomax		u_int32_t af = dst->sa_family;
261123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
262103026Ssobomax	}
263103026Ssobomax
264103026Ssobomax	m->m_flags &= ~(M_BCAST|M_MCAST);
265103026Ssobomax
266103026Ssobomax	if (sc->g_proto == IPPROTO_MOBILE) {
267103026Ssobomax		if (dst->sa_family == AF_INET) {
268103026Ssobomax			struct mbuf *m0;
269103026Ssobomax			int msiz;
270103026Ssobomax
271103026Ssobomax			ip = mtod(m, struct ip *);
272103026Ssobomax
273103026Ssobomax			/*
274103026Ssobomax			 * RFC2004 specifies that fragmented diagrams shouldn't
275103026Ssobomax			 * be encapsulated.
276103026Ssobomax			 */
277103026Ssobomax			if ((ip->ip_off & IP_MF) != 0) {
278103026Ssobomax				_IF_DROP(&ifp->if_snd);
279103026Ssobomax				m_freem(m);
280103026Ssobomax				error = EINVAL;    /* is there better errno? */
281103026Ssobomax				goto end;
282103026Ssobomax			}
283103026Ssobomax			memset(&mob_h, 0, MOB_H_SIZ_L);
284103026Ssobomax			mob_h.proto = (ip->ip_p) << 8;
285103026Ssobomax			mob_h.odst = ip->ip_dst.s_addr;
286103026Ssobomax			ip->ip_dst.s_addr = sc->g_dst.s_addr;
287103026Ssobomax
288103026Ssobomax			/*
289103026Ssobomax			 * If the packet comes from our host, we only change
290103026Ssobomax			 * the destination address in the IP header.
291103026Ssobomax			 * Else we also need to save and change the source
292103026Ssobomax			 */
293103026Ssobomax			if (in_hosteq(ip->ip_src, sc->g_src)) {
294103026Ssobomax				msiz = MOB_H_SIZ_S;
295103026Ssobomax			} else {
296103026Ssobomax				mob_h.proto |= MOB_H_SBIT;
297103026Ssobomax				mob_h.osrc = ip->ip_src.s_addr;
298103026Ssobomax				ip->ip_src.s_addr = sc->g_src.s_addr;
299103026Ssobomax				msiz = MOB_H_SIZ_L;
300103026Ssobomax			}
301103026Ssobomax			mob_h.proto = htons(mob_h.proto);
302123992Ssobomax			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
303103026Ssobomax
304103026Ssobomax			if ((m->m_data - msiz) < m->m_pktdat) {
305103026Ssobomax				/* need new mbuf */
306111119Simp				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
307103026Ssobomax				if (m0 == NULL) {
308103026Ssobomax					_IF_DROP(&ifp->if_snd);
309103026Ssobomax					m_freem(m);
310103026Ssobomax					error = ENOBUFS;
311103026Ssobomax					goto end;
312103026Ssobomax				}
313103026Ssobomax				m0->m_next = m;
314103026Ssobomax				m->m_data += sizeof(struct ip);
315103026Ssobomax				m->m_len -= sizeof(struct ip);
316103026Ssobomax				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
317103026Ssobomax				m0->m_len = msiz + sizeof(struct ip);
318103026Ssobomax				m0->m_data += max_linkhdr;
319103026Ssobomax				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
320103026Ssobomax				       sizeof(struct ip));
321103026Ssobomax				m = m0;
322103026Ssobomax			} else {  /* we have some space left in the old one */
323103026Ssobomax				m->m_data -= msiz;
324103026Ssobomax				m->m_len += msiz;
325103026Ssobomax				m->m_pkthdr.len += msiz;
326103026Ssobomax				bcopy(ip, mtod(m, caddr_t),
327103026Ssobomax					sizeof(struct ip));
328103026Ssobomax			}
329103026Ssobomax			ip = mtod(m, struct ip *);
330103026Ssobomax			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
331103026Ssobomax			ip->ip_len = ntohs(ip->ip_len) + msiz;
332103026Ssobomax		} else {  /* AF_INET */
333103026Ssobomax			_IF_DROP(&ifp->if_snd);
334103026Ssobomax			m_freem(m);
335103026Ssobomax			error = EINVAL;
336103026Ssobomax			goto end;
337103026Ssobomax		}
338103026Ssobomax	} else if (sc->g_proto == IPPROTO_GRE) {
339103026Ssobomax		switch (dst->sa_family) {
340103026Ssobomax		case AF_INET:
341103026Ssobomax			ip = mtod(m, struct ip *);
342103026Ssobomax			etype = ETHERTYPE_IP;
343103026Ssobomax			break;
344103026Ssobomax#ifdef NETATALK
345103026Ssobomax		case AF_APPLETALK:
346103026Ssobomax			etype = ETHERTYPE_ATALK;
347103026Ssobomax			break;
348103026Ssobomax#endif
349103026Ssobomax		default:
350103026Ssobomax			_IF_DROP(&ifp->if_snd);
351103026Ssobomax			m_freem(m);
352103026Ssobomax			error = EAFNOSUPPORT;
353103026Ssobomax			goto end;
354103026Ssobomax		}
355111119Simp		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
356103026Ssobomax	} else {
357103026Ssobomax		_IF_DROP(&ifp->if_snd);
358103026Ssobomax		m_freem(m);
359103026Ssobomax		error = EINVAL;
360103026Ssobomax		goto end;
361103026Ssobomax	}
362103026Ssobomax
363128580Sandre	if (m == NULL) {	/* mbuf allocation failed */
364103026Ssobomax		_IF_DROP(&ifp->if_snd);
365103026Ssobomax		error = ENOBUFS;
366103026Ssobomax		goto end;
367103026Ssobomax	}
368103026Ssobomax
369103026Ssobomax	gh = mtod(m, struct greip *);
370103026Ssobomax	if (sc->g_proto == IPPROTO_GRE) {
371103026Ssobomax		/* we don't have any GRE flags for now */
372125226Ssobomax		memset((void *)gh, 0, sizeof(struct greip));
373103026Ssobomax		gh->gi_ptype = htons(etype);
374103026Ssobomax	}
375103026Ssobomax
376103026Ssobomax	gh->gi_pr = sc->g_proto;
377103026Ssobomax	if (sc->g_proto != IPPROTO_MOBILE) {
378103026Ssobomax		gh->gi_src = sc->g_src;
379103026Ssobomax		gh->gi_dst = sc->g_dst;
380133163Ssobomax		((struct ip*)gh)->ip_v = IPPROTO_IPV4;
381103026Ssobomax		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
382103032Ssobomax		((struct ip*)gh)->ip_ttl = GRE_TTL;
383103026Ssobomax		((struct ip*)gh)->ip_tos = ip->ip_tos;
384103026Ssobomax		((struct ip*)gh)->ip_id = ip->ip_id;
385125226Ssobomax		gh->gi_len = m->m_pkthdr.len;
386103026Ssobomax	}
387103026Ssobomax
388103026Ssobomax	ifp->if_opackets++;
389103026Ssobomax	ifp->if_obytes += m->m_pkthdr.len;
390128583Sandre	/*
391128583Sandre	 * Send it off and with IP_FORWARD flag to prevent it from
392128583Sandre	 * overwriting the ip_id again.  ip_id is already set to the
393128583Sandre	 * ip_id of the encapsulated packet.
394128583Sandre	 */
395128580Sandre	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
396123992Ssobomax	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
397103026Ssobomax  end:
398103026Ssobomax	sc->called = 0;
399103026Ssobomax	if (error)
400103026Ssobomax		ifp->if_oerrors++;
401103026Ssobomax	return (error);
402103026Ssobomax}
403103026Ssobomax
404103032Ssobomaxstatic int
405103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
406103026Ssobomax{
407103026Ssobomax	struct ifreq *ifr = (struct ifreq *)data;
408103026Ssobomax	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
409103026Ssobomax	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
410103026Ssobomax	struct gre_softc *sc = ifp->if_softc;
411103026Ssobomax	int s;
412103026Ssobomax	struct sockaddr_in si;
413103026Ssobomax	struct sockaddr *sa = NULL;
414103026Ssobomax	int error;
415103026Ssobomax	struct sockaddr_in sp, sm, dp, dm;
416103026Ssobomax
417103026Ssobomax	error = 0;
418103026Ssobomax
419103026Ssobomax	s = splnet();
420103026Ssobomax	switch (cmd) {
421103026Ssobomax	case SIOCSIFADDR:
422103026Ssobomax		ifp->if_flags |= IFF_UP;
423103026Ssobomax		break;
424125020Ssobomax	case SIOCSIFDSTADDR:
425103026Ssobomax		break;
426103026Ssobomax	case SIOCSIFFLAGS:
427103026Ssobomax		if ((error = suser(curthread)) != 0)
428103026Ssobomax			break;
429103026Ssobomax		if ((ifr->ifr_flags & IFF_LINK0) != 0)
430103026Ssobomax			sc->g_proto = IPPROTO_GRE;
431103026Ssobomax		else
432103026Ssobomax			sc->g_proto = IPPROTO_MOBILE;
433125024Ssobomax		if ((ifr->ifr_flags & IFF_LINK2) != 0)
434125024Ssobomax			sc->wccp_ver = WCCP_V2;
435125024Ssobomax		else
436125024Ssobomax			sc->wccp_ver = WCCP_V1;
437103026Ssobomax		goto recompute;
438103026Ssobomax	case SIOCSIFMTU:
439103026Ssobomax		if ((error = suser(curthread)) != 0)
440103026Ssobomax			break;
441103026Ssobomax		if (ifr->ifr_mtu < 576) {
442103026Ssobomax			error = EINVAL;
443103026Ssobomax			break;
444103026Ssobomax		}
445103026Ssobomax		ifp->if_mtu = ifr->ifr_mtu;
446103026Ssobomax		break;
447103026Ssobomax	case SIOCGIFMTU:
448147256Sbrooks		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
449103026Ssobomax		break;
450103026Ssobomax	case SIOCADDMULTI:
451103026Ssobomax	case SIOCDELMULTI:
452103026Ssobomax		if ((error = suser(curthread)) != 0)
453103026Ssobomax			break;
454103026Ssobomax		if (ifr == 0) {
455103026Ssobomax			error = EAFNOSUPPORT;
456103026Ssobomax			break;
457103026Ssobomax		}
458103026Ssobomax		switch (ifr->ifr_addr.sa_family) {
459103026Ssobomax#ifdef INET
460103026Ssobomax		case AF_INET:
461103026Ssobomax			break;
462103026Ssobomax#endif
463103026Ssobomax		default:
464103026Ssobomax			error = EAFNOSUPPORT;
465103026Ssobomax			break;
466103026Ssobomax		}
467103026Ssobomax		break;
468103026Ssobomax	case GRESPROTO:
469103026Ssobomax		if ((error = suser(curthread)) != 0)
470103026Ssobomax			break;
471103026Ssobomax		sc->g_proto = ifr->ifr_flags;
472103026Ssobomax		switch (sc->g_proto) {
473103026Ssobomax		case IPPROTO_GRE:
474103026Ssobomax			ifp->if_flags |= IFF_LINK0;
475103026Ssobomax			break;
476103026Ssobomax		case IPPROTO_MOBILE:
477103026Ssobomax			ifp->if_flags &= ~IFF_LINK0;
478103026Ssobomax			break;
479103026Ssobomax		default:
480103026Ssobomax			error = EPROTONOSUPPORT;
481103026Ssobomax			break;
482103026Ssobomax		}
483103026Ssobomax		goto recompute;
484103026Ssobomax	case GREGPROTO:
485103026Ssobomax		ifr->ifr_flags = sc->g_proto;
486103026Ssobomax		break;
487103026Ssobomax	case GRESADDRS:
488103026Ssobomax	case GRESADDRD:
489103026Ssobomax		if ((error = suser(curthread)) != 0)
490103026Ssobomax			break;
491103026Ssobomax		/*
492103026Ssobomax		 * set tunnel endpoints, compute a less specific route
493103026Ssobomax		 * to the remote end and mark if as up
494103026Ssobomax		 */
495103026Ssobomax		sa = &ifr->ifr_addr;
496103026Ssobomax		if (cmd == GRESADDRS)
497103026Ssobomax			sc->g_src = (satosin(sa))->sin_addr;
498103026Ssobomax		if (cmd == GRESADDRD)
499103026Ssobomax			sc->g_dst = (satosin(sa))->sin_addr;
500103026Ssobomax	recompute:
501103026Ssobomax#ifdef INET
502103026Ssobomax		if (sc->encap != NULL) {
503103026Ssobomax			encap_detach(sc->encap);
504103026Ssobomax			sc->encap = NULL;
505103026Ssobomax		}
506103026Ssobomax#endif
507103026Ssobomax		if ((sc->g_src.s_addr != INADDR_ANY) &&
508103026Ssobomax		    (sc->g_dst.s_addr != INADDR_ANY)) {
509103026Ssobomax			bzero(&sp, sizeof(sp));
510103026Ssobomax			bzero(&sm, sizeof(sm));
511103026Ssobomax			bzero(&dp, sizeof(dp));
512103026Ssobomax			bzero(&dm, sizeof(dm));
513103026Ssobomax			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
514103026Ssobomax			    sizeof(struct sockaddr_in);
515103026Ssobomax			sp.sin_family = sm.sin_family = dp.sin_family =
516103026Ssobomax			    dm.sin_family = AF_INET;
517103026Ssobomax			sp.sin_addr = sc->g_src;
518103026Ssobomax			dp.sin_addr = sc->g_dst;
519125020Ssobomax			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
520103026Ssobomax			    INADDR_BROADCAST;
521103026Ssobomax#ifdef INET
522103026Ssobomax			sc->encap = encap_attach(AF_INET, sc->g_proto,
523103026Ssobomax			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
524103026Ssobomax			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
525103026Ssobomax				&in_gre_protosw : &in_mobile_protosw, sc);
526103026Ssobomax			if (sc->encap == NULL)
527103026Ssobomax				printf("%s: unable to attach encap\n",
528147256Sbrooks				    if_name(GRE2IFP(sc)));
529103026Ssobomax#endif
530103026Ssobomax			if (sc->route.ro_rt != 0) /* free old route */
531103026Ssobomax				RTFREE(sc->route.ro_rt);
532103026Ssobomax			if (gre_compute_route(sc) == 0)
533103026Ssobomax				ifp->if_flags |= IFF_RUNNING;
534103026Ssobomax			else
535103026Ssobomax				ifp->if_flags &= ~IFF_RUNNING;
536103026Ssobomax		}
537103026Ssobomax		break;
538103026Ssobomax	case GREGADDRS:
539103026Ssobomax		memset(&si, 0, sizeof(si));
540103026Ssobomax		si.sin_family = AF_INET;
541103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
542103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
543103026Ssobomax		sa = sintosa(&si);
544103026Ssobomax		ifr->ifr_addr = *sa;
545103026Ssobomax		break;
546103026Ssobomax	case GREGADDRD:
547103026Ssobomax		memset(&si, 0, sizeof(si));
548103026Ssobomax		si.sin_family = AF_INET;
549103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
550103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
551103026Ssobomax		sa = sintosa(&si);
552103026Ssobomax		ifr->ifr_addr = *sa;
553103026Ssobomax		break;
554103026Ssobomax	case SIOCSIFPHYADDR:
555103026Ssobomax		if ((error = suser(curthread)) != 0)
556103026Ssobomax			break;
557103026Ssobomax		if (aifr->ifra_addr.sin_family != AF_INET ||
558103026Ssobomax		    aifr->ifra_dstaddr.sin_family != AF_INET) {
559103026Ssobomax			error = EAFNOSUPPORT;
560103026Ssobomax			break;
561103026Ssobomax		}
562103026Ssobomax		if (aifr->ifra_addr.sin_len != sizeof(si) ||
563103026Ssobomax		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
564103026Ssobomax			error = EINVAL;
565103026Ssobomax			break;
566103026Ssobomax		}
567103026Ssobomax		sc->g_src = aifr->ifra_addr.sin_addr;
568103026Ssobomax		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
569103026Ssobomax		goto recompute;
570103026Ssobomax	case SIOCSLIFPHYADDR:
571103026Ssobomax		if ((error = suser(curthread)) != 0)
572103026Ssobomax			break;
573103026Ssobomax		if (lifr->addr.ss_family != AF_INET ||
574103026Ssobomax		    lifr->dstaddr.ss_family != AF_INET) {
575103026Ssobomax			error = EAFNOSUPPORT;
576103026Ssobomax			break;
577103026Ssobomax		}
578103026Ssobomax		if (lifr->addr.ss_len != sizeof(si) ||
579103026Ssobomax		    lifr->dstaddr.ss_len != sizeof(si)) {
580103026Ssobomax			error = EINVAL;
581103026Ssobomax			break;
582103026Ssobomax		}
583103026Ssobomax		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
584103026Ssobomax		sc->g_dst =
585103026Ssobomax		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
586103026Ssobomax		goto recompute;
587103026Ssobomax	case SIOCDIFPHYADDR:
588103026Ssobomax		if ((error = suser(curthread)) != 0)
589103026Ssobomax			break;
590103026Ssobomax		sc->g_src.s_addr = INADDR_ANY;
591103026Ssobomax		sc->g_dst.s_addr = INADDR_ANY;
592103026Ssobomax		goto recompute;
593103026Ssobomax	case SIOCGLIFPHYADDR:
594103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY ||
595103026Ssobomax		    sc->g_dst.s_addr == INADDR_ANY) {
596103026Ssobomax			error = EADDRNOTAVAIL;
597103026Ssobomax			break;
598103026Ssobomax		}
599103026Ssobomax		memset(&si, 0, sizeof(si));
600103026Ssobomax		si.sin_family = AF_INET;
601103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
602103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
603103026Ssobomax		memcpy(&lifr->addr, &si, sizeof(si));
604103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
605103026Ssobomax		memcpy(&lifr->dstaddr, &si, sizeof(si));
606103026Ssobomax		break;
607103026Ssobomax	case SIOCGIFPSRCADDR:
608122699Sbms#ifdef INET6
609122699Sbms	case SIOCGIFPSRCADDR_IN6:
610122699Sbms#endif
611103026Ssobomax		if (sc->g_src.s_addr == INADDR_ANY) {
612103026Ssobomax			error = EADDRNOTAVAIL;
613103026Ssobomax			break;
614103026Ssobomax		}
615103026Ssobomax		memset(&si, 0, sizeof(si));
616103026Ssobomax		si.sin_family = AF_INET;
617103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
618103026Ssobomax		si.sin_addr.s_addr = sc->g_src.s_addr;
619103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
620103026Ssobomax		break;
621103026Ssobomax	case SIOCGIFPDSTADDR:
622122699Sbms#ifdef INET6
623122699Sbms	case SIOCGIFPDSTADDR_IN6:
624122699Sbms#endif
625103026Ssobomax		if (sc->g_dst.s_addr == INADDR_ANY) {
626103026Ssobomax			error = EADDRNOTAVAIL;
627103026Ssobomax			break;
628103026Ssobomax		}
629103026Ssobomax		memset(&si, 0, sizeof(si));
630103026Ssobomax		si.sin_family = AF_INET;
631103026Ssobomax		si.sin_len = sizeof(struct sockaddr_in);
632103026Ssobomax		si.sin_addr.s_addr = sc->g_dst.s_addr;
633103026Ssobomax		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
634103026Ssobomax		break;
635103026Ssobomax	default:
636103026Ssobomax		error = EINVAL;
637103026Ssobomax		break;
638103026Ssobomax	}
639103026Ssobomax
640103026Ssobomax	splx(s);
641103026Ssobomax	return (error);
642103026Ssobomax}
643103026Ssobomax
644103026Ssobomax/*
645103026Ssobomax * computes a route to our destination that is not the one
646103026Ssobomax * which would be taken by ip_output(), as this one will loop back to
647103026Ssobomax * us. If the interface is p2p as  a--->b, then a routing entry exists
648103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here
649123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to
650103026Ssobomax * if_gre.
651103026Ssobomax * Goal here is to compute a route to b that is less specific than
652103026Ssobomax * a-->b. We know that this one exists as in normal operation we have
653103026Ssobomax * at least a default route which matches.
654103026Ssobomax */
655103032Ssobomaxstatic int
656103026Ssobomaxgre_compute_route(struct gre_softc *sc)
657103026Ssobomax{
658103026Ssobomax	struct route *ro;
659103026Ssobomax	u_int32_t a, b, c;
660103026Ssobomax
661103026Ssobomax	ro = &sc->route;
662103026Ssobomax
663103026Ssobomax	memset(ro, 0, sizeof(struct route));
664103026Ssobomax	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
665103026Ssobomax	ro->ro_dst.sa_family = AF_INET;
666103026Ssobomax	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
667103026Ssobomax
668103026Ssobomax	/*
669103026Ssobomax	 * toggle last bit, so our interface is not found, but a less
670103026Ssobomax	 * specific route. I'd rather like to specify a shorter mask,
671103026Ssobomax	 * but this is not possible. Should work though. XXX
672103026Ssobomax	 * there is a simpler way ...
673103026Ssobomax	 */
674147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
675103026Ssobomax		a = ntohl(sc->g_dst.s_addr);
676103026Ssobomax		b = a & 0x01;
677103026Ssobomax		c = a & 0xfffffffe;
678103026Ssobomax		b = b ^ 0x01;
679103026Ssobomax		a = b | c;
680103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
681103026Ssobomax		    = htonl(a);
682103026Ssobomax	}
683103026Ssobomax
684103026Ssobomax#ifdef DIAGNOSTIC
685147256Sbrooks	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
686103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
687103026Ssobomax#endif
688103026Ssobomax
689103026Ssobomax	rtalloc(ro);
690103026Ssobomax
691103026Ssobomax	/*
692103026Ssobomax	 * check if this returned a route at all and this route is no
693103026Ssobomax	 * recursion to ourself
694103026Ssobomax	 */
695103026Ssobomax	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
696103026Ssobomax#ifdef DIAGNOSTIC
697103026Ssobomax		if (ro->ro_rt == NULL)
698103026Ssobomax			printf(" - no route found!\n");
699103026Ssobomax		else
700103026Ssobomax			printf(" - route loops back to ourself!\n");
701103026Ssobomax#endif
702103026Ssobomax		return EADDRNOTAVAIL;
703103026Ssobomax	}
704103026Ssobomax
705103026Ssobomax	/*
706103026Ssobomax	 * now change it back - else ip_output will just drop
707103026Ssobomax	 * the route and search one to this interface ...
708103026Ssobomax	 */
709147256Sbrooks	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
710103026Ssobomax		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
711103026Ssobomax
712103026Ssobomax#ifdef DIAGNOSTIC
713103026Ssobomax	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
714103026Ssobomax	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
715103026Ssobomax	printf("\n");
716103026Ssobomax#endif
717103026Ssobomax
718103026Ssobomax	return 0;
719103026Ssobomax}
720103026Ssobomax
721103026Ssobomax/*
722103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on
723103026Ssobomax * mbufs.
724103026Ssobomax */
725123992Ssobomaxu_int16_t
726123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len)
727103026Ssobomax{
728123992Ssobomax	u_int32_t sum = 0;
729103026Ssobomax	int nwords = len >> 1;
730103026Ssobomax
731103026Ssobomax	while (nwords-- != 0)
732103026Ssobomax		sum += *p++;
733103026Ssobomax
734103026Ssobomax	if (len & 1) {
735103026Ssobomax		union {
736103026Ssobomax			u_short w;
737103026Ssobomax			u_char c[2];
738103026Ssobomax		} u;
739103026Ssobomax		u.c[0] = *(u_char *)p;
740103026Ssobomax		u.c[1] = 0;
741103026Ssobomax		sum += u.w;
742103026Ssobomax	}
743103026Ssobomax
744103026Ssobomax	/* end-around-carry */
745103026Ssobomax	sum = (sum >> 16) + (sum & 0xffff);
746103026Ssobomax	sum += (sum >> 16);
747103026Ssobomax	return (~sum);
748103026Ssobomax}
749103026Ssobomax
750103026Ssobomaxstatic int
751103026Ssobomaxgremodevent(module_t mod, int type, void *data)
752103026Ssobomax{
753127307Srwatson	struct gre_softc *sc;
754103026Ssobomax
755103026Ssobomax	switch (type) {
756103026Ssobomax	case MOD_LOAD:
757103026Ssobomax		greattach();
758103026Ssobomax		break;
759103026Ssobomax	case MOD_UNLOAD:
760103026Ssobomax		if_clone_detach(&gre_cloner);
761103026Ssobomax
762127307Srwatson		mtx_lock(&gre_mtx);
763127307Srwatson		while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) {
764127307Srwatson			LIST_REMOVE(sc, sc_list);
765127307Srwatson			mtx_unlock(&gre_mtx);
766127307Srwatson			gre_destroy(sc);
767127307Srwatson			mtx_lock(&gre_mtx);
768127307Srwatson		}
769127307Srwatson		mtx_unlock(&gre_mtx);
770127307Srwatson		mtx_destroy(&gre_mtx);
771103026Ssobomax		break;
772132199Sphk	default:
773132199Sphk		return EOPNOTSUPP;
774103026Ssobomax	}
775103026Ssobomax	return 0;
776103026Ssobomax}
777103026Ssobomax
778103026Ssobomaxstatic moduledata_t gre_mod = {
779103026Ssobomax	"if_gre",
780103026Ssobomax	gremodevent,
781103026Ssobomax	0
782103026Ssobomax};
783103026Ssobomax
784103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
785103026SsobomaxMODULE_VERSION(if_gre, 1);
786