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