if_gre.c revision 103026
1/*	$NetBSD: if_gre.c,v 1.42 2002/08/14 00:23:27 itojun Exp $ */
2/*	 $FreeBSD: head/sys/net/if_gre.c 103026 2002-09-06 17:12:50Z sobomax $ */
3
4/*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Heiko W.Rupp <hwr@pilhuhn.de>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *        This product includes software developed by the NetBSD
22 *        Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*
41 * Encapsulate L3 protocols into IP
42 * See RFC 1701 and 1702 for more details.
43 * If_gre is compatible with Cisco GRE tunnels, so you can
44 * have a NetBSD box as the other end of a tunnel interface of a Cisco
45 * router. See gre(4) for more details.
46 * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
47 */
48
49#include <sys/cdefs.h>
50__RCSID("@(#) $FreeBSD: head/sys/net/if_gre.c 103026 2002-09-06 17:12:50Z sobomax $");
51
52#include "opt_inet.h"
53#include "opt_ns.h"
54#include "bpf.h"
55
56#include <sys/param.h>
57#include <sys/kernel.h>
58#include <sys/malloc.h>
59#include <sys/mbuf.h>
60#include <sys/proc.h>
61#include <sys/protosw.h>
62#include <sys/socket.h>
63#include <sys/sockio.h>
64#include <sys/queue.h>
65#include <sys/sysctl.h>
66
67#include <machine/cpu.h>
68
69#include <net/ethernet.h>
70#include <net/if.h>
71#include <net/if_types.h>
72#include <net/netisr.h>
73#include <net/route.h>
74
75#ifdef INET
76#include <netinet/in.h>
77#include <netinet/in_systm.h>
78#include <netinet/in_var.h>
79#include <netinet/ip.h>
80#include <netinet/ip_gre.h>
81#include <netinet/ip_var.h>
82#include <netinet/ip_encap.h>
83#else
84#error "Huh? if_gre without inet?"
85#endif
86
87#ifdef NS
88#include <netns/ns.h>
89#include <netns/ns_if.h>
90#endif
91
92#ifdef NETATALK
93#include <netatalk/at.h>
94#include <netatalk/at_var.h>
95#include <netatalk/at_extern.h>
96#endif
97
98#if NBPF > 0
99#include <sys/time.h>
100#include <net/bpf.h>
101#endif
102
103#include <net/net_osdep.h>
104#include <net/if_gre.h>
105
106/*
107 * It is not easy to calculate the right value for a GRE MTU.
108 * We leave this task to the admin and use the same default that
109 * other vendors use.
110 */
111#define GREMTU	1476
112
113#define GRENAME	"gre"
114
115static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
116
117struct gre_softc_head gre_softc_list;
118int ip_gre_ttl = GRE_TTL;
119
120int	gre_clone_create __P((struct if_clone *, int));
121void	gre_clone_destroy __P((struct ifnet *));
122
123struct if_clone gre_cloner =
124    IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy, 0, IF_MAXUNIT);
125
126int gre_compute_route(struct gre_softc *sc);
127
128void	greattach __P((void));
129
130#ifdef INET
131extern struct domain inetdomain;
132static const struct protosw in_gre_protosw =
133{ SOCK_RAW,     &inetdomain,    IPPROTO_GRE,    PR_ATOMIC|PR_ADDR,
134  (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
135  0,
136  0,		0,		0,		0,
137  &rip_usrreqs
138};
139static const struct protosw in_mobile_protosw =
140{ SOCK_RAW,     &inetdomain,    IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR,
141  (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
142  0,
143  0,		0,		0,		0,
144  &rip_usrreqs
145};
146#endif
147
148SYSCTL_DECL(_net_link);
149SYSCTL_NODE(_net_link, IFT_OTHER, gre, CTLFLAG_RW, 0,
150    "Generic Routing Encapsulation");
151#ifndef MAX_GRE_NEST
152/*
153 * This macro controls the default upper limitation on nesting of gre tunnels.
154 * Since, setting a large value to this macro with a careless configuration
155 * may introduce system crash, we don't allow any nestings by default.
156 * If you need to configure nested gre tunnels, you can define this macro
157 * in your kernel configuration file.  However, if you do so, please be
158 * careful to configure the tunnels so that it won't make a loop.
159 */
160#define MAX_GRE_NEST 1
161#endif
162static int max_gre_nesting = MAX_GRE_NEST;
163SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
164    &max_gre_nesting, 0, "Max nested tunnels");
165
166/* ARGSUSED */
167void
168greattach(void)
169{
170
171	LIST_INIT(&gre_softc_list);
172	if_clone_attach(&gre_cloner);
173}
174
175int
176gre_clone_create(ifc, unit)
177	struct if_clone *ifc;
178	int unit;
179{
180	struct gre_softc *sc;
181
182	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK);
183	memset(sc, 0, sizeof(struct gre_softc));
184
185	sc->sc_if.if_name = GRENAME;
186	sc->sc_if.if_softc = sc;
187	sc->sc_if.if_unit = unit;
188	sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
189	sc->sc_if.if_type = IFT_OTHER;
190	sc->sc_if.if_addrlen = 0;
191	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
192	sc->sc_if.if_mtu = GREMTU;
193	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
194	sc->sc_if.if_output = gre_output;
195	sc->sc_if.if_ioctl = gre_ioctl;
196	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
197	sc->g_proto = IPPROTO_GRE;
198	sc->sc_if.if_flags |= IFF_LINK0;
199	sc->encap = NULL;
200	sc->called = 0;
201	if_attach(&sc->sc_if);
202#if NBPF
203	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
204#endif
205	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
206	return (0);
207}
208
209void
210gre_clone_destroy(ifp)
211	struct ifnet *ifp;
212{
213	struct gre_softc *sc = ifp->if_softc;
214
215#ifdef INET
216	if (sc->encap != NULL)
217		encap_detach(sc->encap);
218#endif
219	LIST_REMOVE(sc, sc_list);
220#if NBPF
221	bpfdetach(ifp);
222#endif
223	if_detach(ifp);
224	free(sc, M_GRE);
225}
226
227/*
228 * The output routine. Takes a packet and encapsulates it in the protocol
229 * given by sc->g_proto. See also RFC 1701 and RFC 2004
230 */
231int
232gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
233	   struct rtentry *rt)
234{
235	int error = 0;
236	struct gre_softc *sc = ifp->if_softc;
237	struct greip *gh;
238	struct ip *ip;
239	u_char osrc;
240	u_short etype = 0;
241	struct mobile_h mob_h;
242
243	/*
244	 * gre may cause infinite recursion calls when misconfigured.
245	 * We'll prevent this by introducing upper limit.
246	 */
247	if (++(sc->called) > max_gre_nesting) {
248		printf("%s: gre_output: recursively called too many "
249		       "times(%d)\n", if_name(&sc->sc_if), sc->called);
250		m_freem(m);
251		error = EIO;    /* is there better errno? */
252		goto end;
253	}
254
255	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
256	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
257		m_freem(m);
258		error = ENETDOWN;
259		goto end;
260	}
261
262	gh = NULL;
263	ip = NULL;
264	osrc = 0;
265
266#if NBPF
267	if (ifp->if_bpf) {
268		/* see comment of other if_foo.c files */
269		struct mbuf m0;
270		u_int32_t af = dst->sa_family;
271
272		m0.m_next = m;
273		m0.m_len = 4;
274		m0.m_data = (char *)&af;
275
276		bpf_mtap(ifp, &m0);
277	}
278#endif
279
280	m->m_flags &= ~(M_BCAST|M_MCAST);
281
282	if (sc->g_proto == IPPROTO_MOBILE) {
283		if (dst->sa_family == AF_INET) {
284			struct mbuf *m0;
285			int msiz;
286
287			ip = mtod(m, struct ip *);
288
289			/*
290			 * RFC2004 specifies that fragmented diagrams shouldn't
291			 * be encapsulated.
292			 */
293			if ((ip->ip_off & IP_MF) != 0) {
294				_IF_DROP(&ifp->if_snd);
295				m_freem(m);
296				error = EINVAL;    /* is there better errno? */
297				goto end;
298			}
299			memset(&mob_h, 0, MOB_H_SIZ_L);
300			mob_h.proto = (ip->ip_p) << 8;
301			mob_h.odst = ip->ip_dst.s_addr;
302			ip->ip_dst.s_addr = sc->g_dst.s_addr;
303
304			/*
305			 * If the packet comes from our host, we only change
306			 * the destination address in the IP header.
307			 * Else we also need to save and change the source
308			 */
309			if (in_hosteq(ip->ip_src, sc->g_src)) {
310				msiz = MOB_H_SIZ_S;
311			} else {
312				mob_h.proto |= MOB_H_SBIT;
313				mob_h.osrc = ip->ip_src.s_addr;
314				ip->ip_src.s_addr = sc->g_src.s_addr;
315				msiz = MOB_H_SIZ_L;
316			}
317			mob_h.proto = htons(mob_h.proto);
318			mob_h.hcrc = gre_in_cksum((u_short *)&mob_h, msiz);
319
320			if ((m->m_data - msiz) < m->m_pktdat) {
321				/* need new mbuf */
322				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
323				if (m0 == NULL) {
324					_IF_DROP(&ifp->if_snd);
325					m_freem(m);
326					error = ENOBUFS;
327					goto end;
328				}
329				m0->m_next = m;
330				m->m_data += sizeof(struct ip);
331				m->m_len -= sizeof(struct ip);
332				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
333				m0->m_len = msiz + sizeof(struct ip);
334				m0->m_data += max_linkhdr;
335				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
336				       sizeof(struct ip));
337				m = m0;
338			} else {  /* we have some space left in the old one */
339				m->m_data -= msiz;
340				m->m_len += msiz;
341				m->m_pkthdr.len += msiz;
342				bcopy(ip, mtod(m, caddr_t),
343					sizeof(struct ip));
344			}
345			ip = mtod(m, struct ip *);
346			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
347			ip->ip_len = ntohs(ip->ip_len) + msiz;
348		} else {  /* AF_INET */
349			_IF_DROP(&ifp->if_snd);
350			m_freem(m);
351			error = EINVAL;
352			goto end;
353		}
354	} else if (sc->g_proto == IPPROTO_GRE) {
355		switch (dst->sa_family) {
356		case AF_INET:
357			ip = mtod(m, struct ip *);
358			etype = ETHERTYPE_IP;
359			break;
360#ifdef NETATALK
361		case AF_APPLETALK:
362			etype = ETHERTYPE_ATALK;
363			break;
364#endif
365#ifdef NS
366		case AF_NS:
367			etype = ETHERTYPE_NS;
368			break;
369#endif
370		default:
371			_IF_DROP(&ifp->if_snd);
372			m_freem(m);
373			error = EAFNOSUPPORT;
374			goto end;
375		}
376		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
377	} else {
378		_IF_DROP(&ifp->if_snd);
379		m_freem(m);
380		error = EINVAL;
381		goto end;
382	}
383
384	if (m == NULL) {	/* impossible */
385		_IF_DROP(&ifp->if_snd);
386		error = ENOBUFS;
387		goto end;
388	}
389
390	gh = mtod(m, struct greip *);
391	if (sc->g_proto == IPPROTO_GRE) {
392		/* we don't have any GRE flags for now */
393
394		memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
395		gh->gi_ptype = htons(etype);
396	}
397
398	gh->gi_pr = sc->g_proto;
399	if (sc->g_proto != IPPROTO_MOBILE) {
400		gh->gi_src = sc->g_src;
401		gh->gi_dst = sc->g_dst;
402		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
403		((struct ip*)gh)->ip_ttl = ip_gre_ttl;
404		((struct ip*)gh)->ip_tos = ip->ip_tos;
405		((struct ip*)gh)->ip_id = ip->ip_id;
406		gh->gi_len = m->m_pkthdr.len;
407	}
408
409	ifp->if_opackets++;
410	ifp->if_obytes += m->m_pkthdr.len;
411	/* send it off */
412	error = ip_output(m, NULL, &sc->route, 0, NULL);
413  end:
414	sc->called = 0;
415	if (error)
416		ifp->if_oerrors++;
417	return (error);
418}
419
420int
421gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
422{
423	struct ifreq *ifr = (struct ifreq *)data;
424	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
425	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
426	struct gre_softc *sc = ifp->if_softc;
427	int s;
428	struct sockaddr_in si;
429	struct sockaddr *sa = NULL;
430	int error;
431	struct sockaddr_in sp, sm, dp, dm;
432
433	error = 0;
434
435	s = splnet();
436	switch (cmd) {
437	case SIOCSIFADDR:
438		ifp->if_flags |= IFF_UP;
439		break;
440	case SIOCSIFDSTADDR:
441		break;
442	case SIOCSIFFLAGS:
443		if ((error = suser(curthread)) != 0)
444			break;
445		if ((ifr->ifr_flags & IFF_LINK0) != 0)
446			sc->g_proto = IPPROTO_GRE;
447		else
448			sc->g_proto = IPPROTO_MOBILE;
449		goto recompute;
450	case SIOCSIFMTU:
451		if ((error = suser(curthread)) != 0)
452			break;
453		if (ifr->ifr_mtu < 576) {
454			error = EINVAL;
455			break;
456		}
457		ifp->if_mtu = ifr->ifr_mtu;
458		break;
459	case SIOCGIFMTU:
460		ifr->ifr_mtu = sc->sc_if.if_mtu;
461		break;
462	case SIOCADDMULTI:
463	case SIOCDELMULTI:
464		if ((error = suser(curthread)) != 0)
465			break;
466		if (ifr == 0) {
467			error = EAFNOSUPPORT;
468			break;
469		}
470		switch (ifr->ifr_addr.sa_family) {
471#ifdef INET
472		case AF_INET:
473			break;
474#endif
475		default:
476			error = EAFNOSUPPORT;
477			break;
478		}
479		break;
480	case GRESPROTO:
481		if ((error = suser(curthread)) != 0)
482			break;
483		sc->g_proto = ifr->ifr_flags;
484		switch (sc->g_proto) {
485		case IPPROTO_GRE:
486			ifp->if_flags |= IFF_LINK0;
487			break;
488		case IPPROTO_MOBILE:
489			ifp->if_flags &= ~IFF_LINK0;
490			break;
491		default:
492			error = EPROTONOSUPPORT;
493			break;
494		}
495		goto recompute;
496	case GREGPROTO:
497		ifr->ifr_flags = sc->g_proto;
498		break;
499	case GRESADDRS:
500	case GRESADDRD:
501		if ((error = suser(curthread)) != 0)
502			break;
503		/*
504		 * set tunnel endpoints, compute a less specific route
505		 * to the remote end and mark if as up
506		 */
507		sa = &ifr->ifr_addr;
508		if (cmd == GRESADDRS)
509			sc->g_src = (satosin(sa))->sin_addr;
510		if (cmd == GRESADDRD)
511			sc->g_dst = (satosin(sa))->sin_addr;
512	recompute:
513#ifdef INET
514		if (sc->encap != NULL) {
515			encap_detach(sc->encap);
516			sc->encap = NULL;
517		}
518#endif
519		if ((sc->g_src.s_addr != INADDR_ANY) &&
520		    (sc->g_dst.s_addr != INADDR_ANY)) {
521			bzero(&sp, sizeof(sp));
522			bzero(&sm, sizeof(sm));
523			bzero(&dp, sizeof(dp));
524			bzero(&dm, sizeof(dm));
525			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
526			    sizeof(struct sockaddr_in);
527			sp.sin_family = sm.sin_family = dp.sin_family =
528			    dm.sin_family = AF_INET;
529			sp.sin_addr = sc->g_src;
530			dp.sin_addr = sc->g_dst;
531			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
532			    INADDR_BROADCAST;
533#ifdef INET
534			sc->encap = encap_attach(AF_INET, sc->g_proto,
535			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
536			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
537				&in_gre_protosw : &in_mobile_protosw, sc);
538			if (sc->encap == NULL)
539				printf("%s: unable to attach encap\n",
540				    if_name(&sc->sc_if));
541#endif
542			if (sc->route.ro_rt != 0) /* free old route */
543				RTFREE(sc->route.ro_rt);
544			if (gre_compute_route(sc) == 0)
545				ifp->if_flags |= IFF_RUNNING;
546			else
547				ifp->if_flags &= ~IFF_RUNNING;
548		}
549		break;
550	case GREGADDRS:
551		memset(&si, 0, sizeof(si));
552		si.sin_family = AF_INET;
553		si.sin_len = sizeof(struct sockaddr_in);
554		si.sin_addr.s_addr = sc->g_src.s_addr;
555		sa = sintosa(&si);
556		ifr->ifr_addr = *sa;
557		break;
558	case GREGADDRD:
559		memset(&si, 0, sizeof(si));
560		si.sin_family = AF_INET;
561		si.sin_len = sizeof(struct sockaddr_in);
562		si.sin_addr.s_addr = sc->g_dst.s_addr;
563		sa = sintosa(&si);
564		ifr->ifr_addr = *sa;
565		break;
566	case SIOCSIFPHYADDR:
567		if ((error = suser(curthread)) != 0)
568			break;
569		if (aifr->ifra_addr.sin_family != AF_INET ||
570		    aifr->ifra_dstaddr.sin_family != AF_INET) {
571			error = EAFNOSUPPORT;
572			break;
573		}
574		if (aifr->ifra_addr.sin_len != sizeof(si) ||
575		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
576			error = EINVAL;
577			break;
578		}
579		sc->g_src = aifr->ifra_addr.sin_addr;
580		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
581		goto recompute;
582	case SIOCSLIFPHYADDR:
583		if ((error = suser(curthread)) != 0)
584			break;
585		if (lifr->addr.ss_family != AF_INET ||
586		    lifr->dstaddr.ss_family != AF_INET) {
587			error = EAFNOSUPPORT;
588			break;
589		}
590		if (lifr->addr.ss_len != sizeof(si) ||
591		    lifr->dstaddr.ss_len != sizeof(si)) {
592			error = EINVAL;
593			break;
594		}
595		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
596		sc->g_dst =
597		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
598		goto recompute;
599	case SIOCDIFPHYADDR:
600		if ((error = suser(curthread)) != 0)
601			break;
602		sc->g_src.s_addr = INADDR_ANY;
603		sc->g_dst.s_addr = INADDR_ANY;
604		goto recompute;
605	case SIOCGLIFPHYADDR:
606		if (sc->g_src.s_addr == INADDR_ANY ||
607		    sc->g_dst.s_addr == INADDR_ANY) {
608			error = EADDRNOTAVAIL;
609			break;
610		}
611		memset(&si, 0, sizeof(si));
612		si.sin_family = AF_INET;
613		si.sin_len = sizeof(struct sockaddr_in);
614		si.sin_addr.s_addr = sc->g_src.s_addr;
615		memcpy(&lifr->addr, &si, sizeof(si));
616		si.sin_addr.s_addr = sc->g_dst.s_addr;
617		memcpy(&lifr->dstaddr, &si, sizeof(si));
618		break;
619	case SIOCGIFPSRCADDR:
620		if (sc->g_src.s_addr == INADDR_ANY) {
621			error = EADDRNOTAVAIL;
622			break;
623		}
624		memset(&si, 0, sizeof(si));
625		si.sin_family = AF_INET;
626		si.sin_len = sizeof(struct sockaddr_in);
627		si.sin_addr.s_addr = sc->g_src.s_addr;
628		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
629		break;
630	case SIOCGIFPDSTADDR:
631		if (sc->g_dst.s_addr == INADDR_ANY) {
632			error = EADDRNOTAVAIL;
633			break;
634		}
635		memset(&si, 0, sizeof(si));
636		si.sin_family = AF_INET;
637		si.sin_len = sizeof(struct sockaddr_in);
638		si.sin_addr.s_addr = sc->g_dst.s_addr;
639		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
640		break;
641	default:
642		error = EINVAL;
643		break;
644	}
645
646	splx(s);
647	return (error);
648}
649
650/*
651 * computes a route to our destination that is not the one
652 * which would be taken by ip_output(), as this one will loop back to
653 * us. If the interface is p2p as  a--->b, then a routing entry exists
654 * If we now send a packet to b (e.g. ping b), this will come down here
655 * gets src=a, dst=b tacked on and would from ip_ouput() sent back to
656 * if_gre.
657 * Goal here is to compute a route to b that is less specific than
658 * a-->b. We know that this one exists as in normal operation we have
659 * at least a default route which matches.
660 */
661int
662gre_compute_route(struct gre_softc *sc)
663{
664	struct route *ro;
665	u_int32_t a, b, c;
666
667	ro = &sc->route;
668
669	memset(ro, 0, sizeof(struct route));
670	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
671	ro->ro_dst.sa_family = AF_INET;
672	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
673
674	/*
675	 * toggle last bit, so our interface is not found, but a less
676	 * specific route. I'd rather like to specify a shorter mask,
677	 * but this is not possible. Should work though. XXX
678	 * there is a simpler way ...
679	 */
680	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
681		a = ntohl(sc->g_dst.s_addr);
682		b = a & 0x01;
683		c = a & 0xfffffffe;
684		b = b ^ 0x01;
685		a = b | c;
686		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
687		    = htonl(a);
688	}
689
690#ifdef DIAGNOSTIC
691	printf("%s: searching a route to %s", if_name(&sc->sc_if),
692	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
693#endif
694
695	rtalloc(ro);
696
697	/*
698	 * check if this returned a route at all and this route is no
699	 * recursion to ourself
700	 */
701	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
702#ifdef DIAGNOSTIC
703		if (ro->ro_rt == NULL)
704			printf(" - no route found!\n");
705		else
706			printf(" - route loops back to ourself!\n");
707#endif
708		return EADDRNOTAVAIL;
709	}
710
711	/*
712	 * now change it back - else ip_output will just drop
713	 * the route and search one to this interface ...
714	 */
715	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
716		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
717
718#ifdef DIAGNOSTIC
719	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
720	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
721	printf("\n");
722#endif
723
724	return 0;
725}
726
727/*
728 * do a checksum of a buffer - much like in_cksum, which operates on
729 * mbufs.
730 */
731u_short
732gre_in_cksum(u_short *p, u_int len)
733{
734	u_int sum = 0;
735	int nwords = len >> 1;
736
737	while (nwords-- != 0)
738		sum += *p++;
739
740	if (len & 1) {
741		union {
742			u_short w;
743			u_char c[2];
744		} u;
745		u.c[0] = *(u_char *)p;
746		u.c[1] = 0;
747		sum += u.w;
748	}
749
750	/* end-around-carry */
751	sum = (sum >> 16) + (sum & 0xffff);
752	sum += (sum >> 16);
753	return (~sum);
754}
755
756static int
757gremodevent(module_t mod, int type, void *data)
758{
759
760	switch (type) {
761	case MOD_LOAD:
762		greattach();
763		break;
764	case MOD_UNLOAD:
765		if_clone_detach(&gre_cloner);
766
767		while (!LIST_EMPTY(&gre_softc_list))
768			gre_clone_destroy(&LIST_FIRST(&gre_softc_list)->sc_if);
769		break;
770	}
771	return 0;
772}
773
774static moduledata_t gre_mod = {
775	"if_gre",
776	gremodevent,
777	0
778};
779
780DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
781MODULE_VERSION(if_gre, 1);
782