xref: /freebsd/sys/net/if_gre.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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/priv.h>
61 #include <sys/proc.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/sockio.h>
65 #include <sys/sysctl.h>
66 #include <sys/systm.h>
67 
68 #include <net/ethernet.h>
69 #include <net/if.h>
70 #include <net/if_clone.h>
71 #include <net/if_types.h>
72 #include <net/route.h>
73 
74 #ifdef INET
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #include <netinet/ip_gre.h>
80 #include <netinet/ip_var.h>
81 #include <netinet/ip_encap.h>
82 #include <netinet/vinet.h>
83 #else
84 #error "Huh? if_gre without inet?"
85 #endif
86 
87 #include <net/bpf.h>
88 
89 #include <net/if_gre.h>
90 
91 /*
92  * It is not easy to calculate the right value for a GRE MTU.
93  * We leave this task to the admin and use the same default that
94  * other vendors use.
95  */
96 #define GREMTU	1476
97 
98 #define GRENAME	"gre"
99 
100 /*
101  * gre_mtx protects all global variables in if_gre.c.
102  * XXX: gre_softc data not protected yet.
103  */
104 struct mtx gre_mtx;
105 static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
106 
107 struct gre_softc_head gre_softc_list;
108 
109 static int	gre_clone_create(struct if_clone *, int, caddr_t);
110 static void	gre_clone_destroy(struct ifnet *);
111 static int	gre_ioctl(struct ifnet *, u_long, caddr_t);
112 static int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
113 		    struct rtentry *rt);
114 
115 IFC_SIMPLE_DECLARE(gre, 0);
116 
117 static int gre_compute_route(struct gre_softc *sc);
118 
119 static void	greattach(void);
120 
121 #ifdef INET
122 extern struct domain inetdomain;
123 static const struct protosw in_gre_protosw = {
124 	.pr_type =		SOCK_RAW,
125 	.pr_domain =		&inetdomain,
126 	.pr_protocol =		IPPROTO_GRE,
127 	.pr_flags =		PR_ATOMIC|PR_ADDR,
128 	.pr_input =		gre_input,
129 	.pr_output =		(pr_output_t *)rip_output,
130 	.pr_ctlinput =		rip_ctlinput,
131 	.pr_ctloutput =		rip_ctloutput,
132 	.pr_usrreqs =		&rip_usrreqs
133 };
134 static const struct protosw in_mobile_protosw = {
135 	.pr_type =		SOCK_RAW,
136 	.pr_domain =		&inetdomain,
137 	.pr_protocol =		IPPROTO_MOBILE,
138 	.pr_flags =		PR_ATOMIC|PR_ADDR,
139 	.pr_input =		gre_mobile_input,
140 	.pr_output =		(pr_output_t *)rip_output,
141 	.pr_ctlinput =		rip_ctlinput,
142 	.pr_ctloutput =		rip_ctloutput,
143 	.pr_usrreqs =		&rip_usrreqs
144 };
145 #endif
146 
147 SYSCTL_DECL(_net_link);
148 SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
149     "Generic Routing Encapsulation");
150 #ifndef MAX_GRE_NEST
151 /*
152  * This macro controls the default upper limitation on nesting of gre tunnels.
153  * Since, setting a large value to this macro with a careless configuration
154  * may introduce system crash, we don't allow any nestings by default.
155  * If you need to configure nested gre tunnels, you can define this macro
156  * in your kernel configuration file.  However, if you do so, please be
157  * careful to configure the tunnels so that it won't make a loop.
158  */
159 #define MAX_GRE_NEST 1
160 #endif
161 static int max_gre_nesting = MAX_GRE_NEST;
162 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
163     &max_gre_nesting, 0, "Max nested tunnels");
164 
165 /* ARGSUSED */
166 static void
167 greattach(void)
168 {
169 
170 	mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
171 	LIST_INIT(&gre_softc_list);
172 	if_clone_attach(&gre_cloner);
173 }
174 
175 static int
176 gre_clone_create(ifc, unit, params)
177 	struct if_clone *ifc;
178 	int unit;
179 	caddr_t params;
180 {
181 	struct gre_softc *sc;
182 
183 	sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
184 
185 	GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
186 	if (GRE2IFP(sc) == NULL) {
187 		free(sc, M_GRE);
188 		return (ENOSPC);
189 	}
190 
191 	GRE2IFP(sc)->if_softc = sc;
192 	if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
193 
194 	GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
195 	GRE2IFP(sc)->if_addrlen = 0;
196 	GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
197 	GRE2IFP(sc)->if_mtu = GREMTU;
198 	GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
199 	GRE2IFP(sc)->if_output = gre_output;
200 	GRE2IFP(sc)->if_ioctl = gre_ioctl;
201 	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
202 	sc->g_proto = IPPROTO_GRE;
203 	GRE2IFP(sc)->if_flags |= IFF_LINK0;
204 	sc->encap = NULL;
205 	sc->called = 0;
206 	sc->gre_fibnum = curthread->td_proc->p_fibnum;
207 	sc->wccp_ver = WCCP_V1;
208 	sc->key = 0;
209 	if_attach(GRE2IFP(sc));
210 	bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
211 	mtx_lock(&gre_mtx);
212 	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
213 	mtx_unlock(&gre_mtx);
214 	return (0);
215 }
216 
217 static void
218 gre_clone_destroy(ifp)
219 	struct ifnet *ifp;
220 {
221 	struct gre_softc *sc = ifp->if_softc;
222 
223 	mtx_lock(&gre_mtx);
224 	LIST_REMOVE(sc, sc_list);
225 	mtx_unlock(&gre_mtx);
226 
227 #ifdef INET
228 	if (sc->encap != NULL)
229 		encap_detach(sc->encap);
230 #endif
231 	bpfdetach(ifp);
232 	if_detach(ifp);
233 	if_free(ifp);
234 	free(sc, M_GRE);
235 }
236 
237 /*
238  * The output routine. Takes a packet and encapsulates it in the protocol
239  * given by sc->g_proto. See also RFC 1701 and RFC 2004
240  */
241 static int
242 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
243 	   struct rtentry *rt)
244 {
245 #ifdef INET6
246 	INIT_VNET_INET(ifp->if_vnet);
247 #endif
248 	int error = 0;
249 	struct gre_softc *sc = ifp->if_softc;
250 	struct greip *gh;
251 	struct ip *ip;
252 	u_short gre_ip_id = 0;
253 	uint8_t gre_ip_tos = 0;
254 	u_int16_t etype = 0;
255 	struct mobile_h mob_h;
256 	u_int32_t af;
257 	int extra = 0;
258 
259 	/*
260 	 * gre may cause infinite recursion calls when misconfigured.
261 	 * We'll prevent this by introducing upper limit.
262 	 */
263 	if (++(sc->called) > max_gre_nesting) {
264 		printf("%s: gre_output: recursively called too many "
265 		       "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
266 		m_freem(m);
267 		error = EIO;    /* is there better errno? */
268 		goto end;
269 	}
270 
271 	if (!((ifp->if_flags & IFF_UP) &&
272 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
273 	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
274 		m_freem(m);
275 		error = ENETDOWN;
276 		goto end;
277 	}
278 
279 	gh = NULL;
280 	ip = NULL;
281 
282 	/* BPF writes need to be handled specially. */
283 	if (dst->sa_family == AF_UNSPEC) {
284 		bcopy(dst->sa_data, &af, sizeof(af));
285 		dst->sa_family = af;
286 	}
287 
288 	if (bpf_peers_present(ifp->if_bpf)) {
289 		af = dst->sa_family;
290 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
291 	}
292 
293 	m->m_flags &= ~(M_BCAST|M_MCAST);
294 
295 	if (sc->g_proto == IPPROTO_MOBILE) {
296 		if (dst->sa_family == AF_INET) {
297 			struct mbuf *m0;
298 			int msiz;
299 
300 			ip = mtod(m, struct ip *);
301 
302 			/*
303 			 * RFC2004 specifies that fragmented diagrams shouldn't
304 			 * be encapsulated.
305 			 */
306 			if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
307 				_IF_DROP(&ifp->if_snd);
308 				m_freem(m);
309 				error = EINVAL;    /* is there better errno? */
310 				goto end;
311 			}
312 			memset(&mob_h, 0, MOB_H_SIZ_L);
313 			mob_h.proto = (ip->ip_p) << 8;
314 			mob_h.odst = ip->ip_dst.s_addr;
315 			ip->ip_dst.s_addr = sc->g_dst.s_addr;
316 
317 			/*
318 			 * If the packet comes from our host, we only change
319 			 * the destination address in the IP header.
320 			 * Else we also need to save and change the source
321 			 */
322 			if (in_hosteq(ip->ip_src, sc->g_src)) {
323 				msiz = MOB_H_SIZ_S;
324 			} else {
325 				mob_h.proto |= MOB_H_SBIT;
326 				mob_h.osrc = ip->ip_src.s_addr;
327 				ip->ip_src.s_addr = sc->g_src.s_addr;
328 				msiz = MOB_H_SIZ_L;
329 			}
330 			mob_h.proto = htons(mob_h.proto);
331 			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
332 
333 			if ((m->m_data - msiz) < m->m_pktdat) {
334 				/* need new mbuf */
335 				MGETHDR(m0, M_DONTWAIT, MT_DATA);
336 				if (m0 == NULL) {
337 					_IF_DROP(&ifp->if_snd);
338 					m_freem(m);
339 					error = ENOBUFS;
340 					goto end;
341 				}
342 				m0->m_next = m;
343 				m->m_data += sizeof(struct ip);
344 				m->m_len -= sizeof(struct ip);
345 				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
346 				m0->m_len = msiz + sizeof(struct ip);
347 				m0->m_data += max_linkhdr;
348 				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
349 				       sizeof(struct ip));
350 				m = m0;
351 			} else {  /* we have some space left in the old one */
352 				m->m_data -= msiz;
353 				m->m_len += msiz;
354 				m->m_pkthdr.len += msiz;
355 				bcopy(ip, mtod(m, caddr_t),
356 					sizeof(struct ip));
357 			}
358 			ip = mtod(m, struct ip *);
359 			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
360 			ip->ip_len = ntohs(ip->ip_len) + msiz;
361 		} else {  /* AF_INET */
362 			_IF_DROP(&ifp->if_snd);
363 			m_freem(m);
364 			error = EINVAL;
365 			goto end;
366 		}
367 	} else if (sc->g_proto == IPPROTO_GRE) {
368 		switch (dst->sa_family) {
369 		case AF_INET:
370 			ip = mtod(m, struct ip *);
371 			gre_ip_tos = ip->ip_tos;
372 			gre_ip_id = ip->ip_id;
373 			if (sc->wccp_ver == WCCP_V2) {
374 				extra = sizeof(uint32_t);
375 				etype =  WCCP_PROTOCOL_TYPE;
376 			} else {
377 				etype = ETHERTYPE_IP;
378 			}
379 			break;
380 #ifdef INET6
381 		case AF_INET6:
382 			gre_ip_id = ip_newid();
383 			etype = ETHERTYPE_IPV6;
384 			break;
385 #endif
386 #ifdef NETATALK
387 		case AF_APPLETALK:
388 			etype = ETHERTYPE_ATALK;
389 			break;
390 #endif
391 		default:
392 			_IF_DROP(&ifp->if_snd);
393 			m_freem(m);
394 			error = EAFNOSUPPORT;
395 			goto end;
396 		}
397 
398 		/* Reserve space for GRE header + optional GRE key */
399 		int hdrlen = sizeof(struct greip) + extra;
400 		if (sc->key)
401 			hdrlen += sizeof(uint32_t);
402 		M_PREPEND(m, hdrlen, M_DONTWAIT);
403 	} else {
404 		_IF_DROP(&ifp->if_snd);
405 		m_freem(m);
406 		error = EINVAL;
407 		goto end;
408 	}
409 
410 	if (m == NULL) {	/* mbuf allocation failed */
411 		_IF_DROP(&ifp->if_snd);
412 		error = ENOBUFS;
413 		goto end;
414 	}
415 
416 	M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */
417 
418 	gh = mtod(m, struct greip *);
419 	if (sc->g_proto == IPPROTO_GRE) {
420 		uint32_t *options = gh->gi_options;
421 
422 		memset((void *)gh, 0, sizeof(struct greip) + extra);
423 		gh->gi_ptype = htons(etype);
424 		gh->gi_flags = 0;
425 
426 		/* Add key option */
427 		if (sc->key)
428 		{
429 			gh->gi_flags |= htons(GRE_KP);
430 			*(options++) = htonl(sc->key);
431 		}
432 	}
433 
434 	gh->gi_pr = sc->g_proto;
435 	if (sc->g_proto != IPPROTO_MOBILE) {
436 		gh->gi_src = sc->g_src;
437 		gh->gi_dst = sc->g_dst;
438 		((struct ip*)gh)->ip_v = IPPROTO_IPV4;
439 		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
440 		((struct ip*)gh)->ip_ttl = GRE_TTL;
441 		((struct ip*)gh)->ip_tos = gre_ip_tos;
442 		((struct ip*)gh)->ip_id = gre_ip_id;
443 		gh->gi_len = m->m_pkthdr.len;
444 	}
445 
446 	ifp->if_opackets++;
447 	ifp->if_obytes += m->m_pkthdr.len;
448 	/*
449 	 * Send it off and with IP_FORWARD flag to prevent it from
450 	 * overwriting the ip_id again.  ip_id is already set to the
451 	 * ip_id of the encapsulated packet.
452 	 */
453 	error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
454 	    (struct ip_moptions *)NULL, (struct inpcb *)NULL);
455   end:
456 	sc->called = 0;
457 	if (error)
458 		ifp->if_oerrors++;
459 	return (error);
460 }
461 
462 static int
463 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
464 {
465 	struct ifreq *ifr = (struct ifreq *)data;
466 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
467 	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
468 	struct gre_softc *sc = ifp->if_softc;
469 	int s;
470 	struct sockaddr_in si;
471 	struct sockaddr *sa = NULL;
472 	int error, adj;
473 	struct sockaddr_in sp, sm, dp, dm;
474 	uint32_t key;
475 
476 	error = 0;
477 	adj = 0;
478 
479 	s = splnet();
480 	switch (cmd) {
481 	case SIOCSIFADDR:
482 		ifp->if_flags |= IFF_UP;
483 		break;
484 	case SIOCSIFDSTADDR:
485 		break;
486 	case SIOCSIFFLAGS:
487 		/*
488 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
489 		 * layer check?
490 		 */
491 		if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0)
492 			break;
493 		if ((ifr->ifr_flags & IFF_LINK0) != 0)
494 			sc->g_proto = IPPROTO_GRE;
495 		else
496 			sc->g_proto = IPPROTO_MOBILE;
497 		if ((ifr->ifr_flags & IFF_LINK2) != 0)
498 			sc->wccp_ver = WCCP_V2;
499 		else
500 			sc->wccp_ver = WCCP_V1;
501 		goto recompute;
502 	case SIOCSIFMTU:
503 		/*
504 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
505 		 * layer check?
506 		 */
507 		if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0)
508 			break;
509 		if (ifr->ifr_mtu < 576) {
510 			error = EINVAL;
511 			break;
512 		}
513 		ifp->if_mtu = ifr->ifr_mtu;
514 		break;
515 	case SIOCGIFMTU:
516 		ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
517 		break;
518 	case SIOCADDMULTI:
519 		/*
520 		 * XXXRW: Isn't this priv_checkr() redundant to the ifnet
521 		 * layer check?
522 		 */
523 		if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0)
524 			break;
525 		if (ifr == 0) {
526 			error = EAFNOSUPPORT;
527 			break;
528 		}
529 		switch (ifr->ifr_addr.sa_family) {
530 #ifdef INET
531 		case AF_INET:
532 			break;
533 #endif
534 #ifdef INET6
535 		case AF_INET6:
536 			break;
537 #endif
538 		default:
539 			error = EAFNOSUPPORT;
540 			break;
541 		}
542 		break;
543 	case SIOCDELMULTI:
544 		/*
545 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
546 		 * layer check?
547 		 */
548 		if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0)
549 			break;
550 		if (ifr == 0) {
551 			error = EAFNOSUPPORT;
552 			break;
553 		}
554 		switch (ifr->ifr_addr.sa_family) {
555 #ifdef INET
556 		case AF_INET:
557 			break;
558 #endif
559 #ifdef INET6
560 		case AF_INET6:
561 			break;
562 #endif
563 		default:
564 			error = EAFNOSUPPORT;
565 			break;
566 		}
567 		break;
568 	case GRESPROTO:
569 		/*
570 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
571 		 * layer check?
572 		 */
573 		if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
574 			break;
575 		sc->g_proto = ifr->ifr_flags;
576 		switch (sc->g_proto) {
577 		case IPPROTO_GRE:
578 			ifp->if_flags |= IFF_LINK0;
579 			break;
580 		case IPPROTO_MOBILE:
581 			ifp->if_flags &= ~IFF_LINK0;
582 			break;
583 		default:
584 			error = EPROTONOSUPPORT;
585 			break;
586 		}
587 		goto recompute;
588 	case GREGPROTO:
589 		ifr->ifr_flags = sc->g_proto;
590 		break;
591 	case GRESADDRS:
592 	case GRESADDRD:
593 		error = priv_check(curthread, PRIV_NET_GRE);
594 		if (error)
595 			return (error);
596 		/*
597 		 * set tunnel endpoints, compute a less specific route
598 		 * to the remote end and mark if as up
599 		 */
600 		sa = &ifr->ifr_addr;
601 		if (cmd == GRESADDRS)
602 			sc->g_src = (satosin(sa))->sin_addr;
603 		if (cmd == GRESADDRD)
604 			sc->g_dst = (satosin(sa))->sin_addr;
605 	recompute:
606 #ifdef INET
607 		if (sc->encap != NULL) {
608 			encap_detach(sc->encap);
609 			sc->encap = NULL;
610 		}
611 #endif
612 		if ((sc->g_src.s_addr != INADDR_ANY) &&
613 		    (sc->g_dst.s_addr != INADDR_ANY)) {
614 			bzero(&sp, sizeof(sp));
615 			bzero(&sm, sizeof(sm));
616 			bzero(&dp, sizeof(dp));
617 			bzero(&dm, sizeof(dm));
618 			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
619 			    sizeof(struct sockaddr_in);
620 			sp.sin_family = sm.sin_family = dp.sin_family =
621 			    dm.sin_family = AF_INET;
622 			sp.sin_addr = sc->g_src;
623 			dp.sin_addr = sc->g_dst;
624 			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
625 			    INADDR_BROADCAST;
626 #ifdef INET
627 			sc->encap = encap_attach(AF_INET, sc->g_proto,
628 			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
629 			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
630 				&in_gre_protosw : &in_mobile_protosw, sc);
631 			if (sc->encap == NULL)
632 				printf("%s: unable to attach encap\n",
633 				    if_name(GRE2IFP(sc)));
634 #endif
635 			if (sc->route.ro_rt != 0) /* free old route */
636 				RTFREE(sc->route.ro_rt);
637 			if (gre_compute_route(sc) == 0)
638 				ifp->if_drv_flags |= IFF_DRV_RUNNING;
639 			else
640 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
641 		}
642 		break;
643 	case GREGADDRS:
644 		memset(&si, 0, sizeof(si));
645 		si.sin_family = AF_INET;
646 		si.sin_len = sizeof(struct sockaddr_in);
647 		si.sin_addr.s_addr = sc->g_src.s_addr;
648 		sa = sintosa(&si);
649 		ifr->ifr_addr = *sa;
650 		break;
651 	case GREGADDRD:
652 		memset(&si, 0, sizeof(si));
653 		si.sin_family = AF_INET;
654 		si.sin_len = sizeof(struct sockaddr_in);
655 		si.sin_addr.s_addr = sc->g_dst.s_addr;
656 		sa = sintosa(&si);
657 		ifr->ifr_addr = *sa;
658 		break;
659 	case SIOCSIFPHYADDR:
660 		/*
661 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
662 		 * layer check?
663 		 */
664 		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
665 			break;
666 		if (aifr->ifra_addr.sin_family != AF_INET ||
667 		    aifr->ifra_dstaddr.sin_family != AF_INET) {
668 			error = EAFNOSUPPORT;
669 			break;
670 		}
671 		if (aifr->ifra_addr.sin_len != sizeof(si) ||
672 		    aifr->ifra_dstaddr.sin_len != sizeof(si)) {
673 			error = EINVAL;
674 			break;
675 		}
676 		sc->g_src = aifr->ifra_addr.sin_addr;
677 		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
678 		goto recompute;
679 	case SIOCSLIFPHYADDR:
680 		/*
681 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
682 		 * layer check?
683 		 */
684 		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
685 			break;
686 		if (lifr->addr.ss_family != AF_INET ||
687 		    lifr->dstaddr.ss_family != AF_INET) {
688 			error = EAFNOSUPPORT;
689 			break;
690 		}
691 		if (lifr->addr.ss_len != sizeof(si) ||
692 		    lifr->dstaddr.ss_len != sizeof(si)) {
693 			error = EINVAL;
694 			break;
695 		}
696 		sc->g_src = (satosin(&lifr->addr))->sin_addr;
697 		sc->g_dst =
698 		    (satosin(&lifr->dstaddr))->sin_addr;
699 		goto recompute;
700 	case SIOCDIFPHYADDR:
701 		/*
702 		 * XXXRW: Isn't this priv_check() redundant to the ifnet
703 		 * layer check?
704 		 */
705 		if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0)
706 			break;
707 		sc->g_src.s_addr = INADDR_ANY;
708 		sc->g_dst.s_addr = INADDR_ANY;
709 		goto recompute;
710 	case SIOCGLIFPHYADDR:
711 		if (sc->g_src.s_addr == INADDR_ANY ||
712 		    sc->g_dst.s_addr == INADDR_ANY) {
713 			error = EADDRNOTAVAIL;
714 			break;
715 		}
716 		memset(&si, 0, sizeof(si));
717 		si.sin_family = AF_INET;
718 		si.sin_len = sizeof(struct sockaddr_in);
719 		si.sin_addr.s_addr = sc->g_src.s_addr;
720 		memcpy(&lifr->addr, &si, sizeof(si));
721 		si.sin_addr.s_addr = sc->g_dst.s_addr;
722 		memcpy(&lifr->dstaddr, &si, sizeof(si));
723 		break;
724 	case SIOCGIFPSRCADDR:
725 #ifdef INET6
726 	case SIOCGIFPSRCADDR_IN6:
727 #endif
728 		if (sc->g_src.s_addr == INADDR_ANY) {
729 			error = EADDRNOTAVAIL;
730 			break;
731 		}
732 		memset(&si, 0, sizeof(si));
733 		si.sin_family = AF_INET;
734 		si.sin_len = sizeof(struct sockaddr_in);
735 		si.sin_addr.s_addr = sc->g_src.s_addr;
736 		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
737 		break;
738 	case SIOCGIFPDSTADDR:
739 #ifdef INET6
740 	case SIOCGIFPDSTADDR_IN6:
741 #endif
742 		if (sc->g_dst.s_addr == INADDR_ANY) {
743 			error = EADDRNOTAVAIL;
744 			break;
745 		}
746 		memset(&si, 0, sizeof(si));
747 		si.sin_family = AF_INET;
748 		si.sin_len = sizeof(struct sockaddr_in);
749 		si.sin_addr.s_addr = sc->g_dst.s_addr;
750 		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
751 		break;
752 	case GRESKEY:
753 		error = priv_check(curthread, PRIV_NET_GRE);
754 		if (error)
755 			break;
756 		error = copyin(ifr->ifr_data, &key, sizeof(key));
757 		if (error)
758 			break;
759 		/* adjust MTU for option header */
760 		if (key == 0 && sc->key != 0)		/* clear */
761 			adj += sizeof(key);
762 		else if (key != 0 && sc->key == 0)	/* set */
763 			adj -= sizeof(key);
764 
765 		if (ifp->if_mtu + adj < 576) {
766 			error = EINVAL;
767 			break;
768 		}
769 		ifp->if_mtu += adj;
770 		sc->key = key;
771 		break;
772 	case GREGKEY:
773 		error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key));
774 		break;
775 
776 	default:
777 		error = EINVAL;
778 		break;
779 	}
780 
781 	splx(s);
782 	return (error);
783 }
784 
785 /*
786  * computes a route to our destination that is not the one
787  * which would be taken by ip_output(), as this one will loop back to
788  * us. If the interface is p2p as  a--->b, then a routing entry exists
789  * If we now send a packet to b (e.g. ping b), this will come down here
790  * gets src=a, dst=b tacked on and would from ip_output() sent back to
791  * if_gre.
792  * Goal here is to compute a route to b that is less specific than
793  * a-->b. We know that this one exists as in normal operation we have
794  * at least a default route which matches.
795  */
796 static int
797 gre_compute_route(struct gre_softc *sc)
798 {
799 	struct route *ro;
800 
801 	ro = &sc->route;
802 
803 	memset(ro, 0, sizeof(struct route));
804 	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
805 	ro->ro_dst.sa_family = AF_INET;
806 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
807 
808 	/*
809 	 * toggle last bit, so our interface is not found, but a less
810 	 * specific route. I'd rather like to specify a shorter mask,
811 	 * but this is not possible. Should work though. XXX
812 	 * XXX MRT Use a different FIB for the tunnel to solve this problem.
813 	 */
814 	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
815 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^=
816 		    htonl(0x01);
817 	}
818 
819 #ifdef DIAGNOSTIC
820 	printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
821 	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
822 #endif
823 
824 	rtalloc_fib(ro, sc->gre_fibnum);
825 
826 	/*
827 	 * check if this returned a route at all and this route is no
828 	 * recursion to ourself
829 	 */
830 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
831 #ifdef DIAGNOSTIC
832 		if (ro->ro_rt == NULL)
833 			printf(" - no route found!\n");
834 		else
835 			printf(" - route loops back to ourself!\n");
836 #endif
837 		return EADDRNOTAVAIL;
838 	}
839 
840 	/*
841 	 * now change it back - else ip_output will just drop
842 	 * the route and search one to this interface ...
843 	 */
844 	if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
845 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
846 
847 #ifdef DIAGNOSTIC
848 	printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
849 	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
850 	printf("\n");
851 #endif
852 
853 	return 0;
854 }
855 
856 /*
857  * do a checksum of a buffer - much like in_cksum, which operates on
858  * mbufs.
859  */
860 u_int16_t
861 gre_in_cksum(u_int16_t *p, u_int len)
862 {
863 	u_int32_t sum = 0;
864 	int nwords = len >> 1;
865 
866 	while (nwords-- != 0)
867 		sum += *p++;
868 
869 	if (len & 1) {
870 		union {
871 			u_short w;
872 			u_char c[2];
873 		} u;
874 		u.c[0] = *(u_char *)p;
875 		u.c[1] = 0;
876 		sum += u.w;
877 	}
878 
879 	/* end-around-carry */
880 	sum = (sum >> 16) + (sum & 0xffff);
881 	sum += (sum >> 16);
882 	return (~sum);
883 }
884 
885 static int
886 gremodevent(module_t mod, int type, void *data)
887 {
888 
889 	switch (type) {
890 	case MOD_LOAD:
891 		greattach();
892 		break;
893 	case MOD_UNLOAD:
894 		if_clone_detach(&gre_cloner);
895 		mtx_destroy(&gre_mtx);
896 		break;
897 	default:
898 		return EOPNOTSUPP;
899 	}
900 	return 0;
901 }
902 
903 static moduledata_t gre_mod = {
904 	"if_gre",
905 	gremodevent,
906 	0
907 };
908 
909 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
910 MODULE_VERSION(if_gre, 1);
911