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