xref: /dragonfly/sys/net/gre/if_gre.c (revision dfbadd37)
1 /*	$NetBSD: if_gre.c,v 1.42 2002/08/14 00:23:27 itojun Exp $ */
2 /*	$FreeBSD: src/sys/net/if_gre.c,v 1.9.2.3 2003/01/23 21:06:44 sam Exp $ */
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 "opt_inet.h"
50 
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/bus.h>
54 #include <sys/malloc.h>
55 #include <sys/mbuf.h>
56 #include <sys/proc.h>
57 #include <sys/caps.h>
58 #include <sys/protosw.h>
59 #include <sys/socket.h>
60 #include <sys/sockio.h>
61 #include <sys/sysctl.h>
62 #include <sys/systm.h>
63 #include <sys/thread2.h>
64 
65 #include <net/ethernet.h>
66 #include <net/if.h>
67 #include <net/if_types.h>
68 #include <net/ifq_var.h>
69 #include <net/route.h>
70 #include <net/if_clone.h>
71 #include <net/netmsg2.h>
72 #include <net/netisr2.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 #else
83 #error "Huh? if_gre without inet?"
84 #endif
85 
86 #include <net/bpf.h>
87 
88 #include <net/net_osdep.h>
89 #include "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 static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
101 
102 struct gre_softc_head gre_softc_list;
103 
104 static int	gre_clone_create(struct if_clone *, int, caddr_t, caddr_t);
105 static int	gre_clone_destroy(struct ifnet *);
106 static int	gre_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
107 static int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
108 		    struct rtentry *rt);
109 
110 static struct if_clone gre_cloner = IF_CLONE_INITIALIZER("gre",
111     gre_clone_create, gre_clone_destroy, 0, IF_MAXUNIT);
112 
113 static int	gre_compute_route(struct gre_softc *sc, struct route *);
114 static int	gre_check_route(struct gre_softc *sc);
115 
116 static void	greattach(void);
117 
118 #ifdef INET
119 
120 extern struct domain inetdomain;
121 
122 static const struct protosw in_gre_protosw =
123     {
124 	.pr_type = SOCK_RAW,
125 	.pr_domain = &inetdomain,
126 	.pr_protocol = IPPROTO_GRE,
127 	.pr_flags = PR_ATOMIC|PR_ADDR,
128 
129 	.pr_input = gre_input,
130 	.pr_output = rip_output,
131 	.pr_ctlinput = NULL,
132 	.pr_ctloutput = rip_ctloutput,
133 
134 	.pr_ctlport = NULL,
135 	.pr_usrreqs = &rip_usrreqs
136     };
137 
138 static const struct protosw in_mobile_protosw =
139     {
140 	.pr_type = SOCK_RAW,
141 	.pr_domain = &inetdomain,
142 	.pr_protocol = IPPROTO_MOBILE,
143 	.pr_flags = PR_ATOMIC|PR_ADDR,
144 
145 	.pr_input = gre_mobile_input,
146 	.pr_output = rip_output,
147 	.pr_ctlinput = NULL,
148 	.pr_ctloutput = rip_ctloutput,
149 
150 	.pr_ctlport = NULL,
151 	.pr_usrreqs = &rip_usrreqs
152     };
153 
154 #endif
155 
156 SYSCTL_DECL(_net_link);
157 SYSCTL_NODE(_net_link, IFT_OTHER, gre, CTLFLAG_RW, 0,
158     "Generic Routing Encapsulation");
159 #ifndef MAX_GRE_NEST
160 /*
161  * This macro controls the default upper limitation on nesting of gre tunnels.
162  * Since, setting a large value to this macro with a careless configuration
163  * may introduce system crash, we don't allow any nestings by default.
164  * If you need to configure nested gre tunnels, you can define this macro
165  * in your kernel configuration file.  However, if you do so, please be
166  * careful to configure the tunnels so that it won't make a loop.
167  */
168 #define MAX_GRE_NEST 1
169 #endif
170 static int max_gre_nesting = MAX_GRE_NEST;
171 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
172     &max_gre_nesting, 0, "Max nested tunnels");
173 
174 /* ARGSUSED */
175 static void
176 greattach(void)
177 {
178 
179 	LIST_INIT(&gre_softc_list);
180 	if_clone_attach(&gre_cloner);
181 }
182 
183 static int
184 gre_clone_create(struct if_clone *ifc, int unit,
185 		 caddr_t params __unused, caddr_t data __unused)
186 {
187 	struct gre_softc *sc;
188 
189 	sc = kmalloc(sizeof(struct gre_softc), M_GRE, M_WAITOK);
190 	memset(sc, 0, sizeof(struct gre_softc));
191 
192 	sc->sc_if.if_softc = sc;
193 	if_initname(&(sc->sc_if), GRENAME, unit);
194 	ifq_set_maxlen(&sc->sc_if.if_snd, IFQ_MAXLEN);
195 	sc->sc_if.if_type = IFT_OTHER;
196 	sc->sc_if.if_addrlen = 0;
197 	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
198 	sc->sc_if.if_mtu = GREMTU;
199 	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
200 	sc->sc_if.if_output = gre_output;
201 	sc->sc_if.if_ioctl = gre_ioctl;
202 	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
203 	sc->g_proto = IPPROTO_GRE;
204 	sc->sc_if.if_flags |= IFF_LINK0;
205 	sc->encap = NULL;
206 	sc->route_pcpu = kmalloc(netisr_ncpus * sizeof(struct route), M_GRE,
207 	    M_WAITOK | M_ZERO);
208 	if_attach(&sc->sc_if, NULL);
209 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
210 	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
211 	return (0);
212 }
213 
214 static int
215 gre_clone_destroy(struct ifnet *ifp)
216 {
217 	struct gre_softc *sc = ifp->if_softc;
218 	int cpu;
219 
220 #ifdef INET
221 	if (sc->encap != NULL)
222 		encap_detach(sc->encap);
223 #endif
224 	LIST_REMOVE(sc, sc_list);
225 	bpfdetach(ifp);
226 	if_detach(ifp);
227 
228 	for (cpu = 0; cpu < netisr_ncpus; ++cpu) {
229 		if (sc->route_pcpu[cpu].ro_rt != NULL) {
230 			rtfree_async(sc->route_pcpu[cpu].ro_rt);
231 			sc->route_pcpu[cpu].ro_rt = NULL;
232 		}
233 	}
234 	kfree(sc->route_pcpu, M_GRE);
235 	kfree(sc, M_GRE);
236 
237 	return 0;
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_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
246 		      struct rtentry *rt)
247 {
248 	int error = 0;
249 	struct gre_softc *sc = ifp->if_softc;
250 	struct greip *gh;
251 	struct ip *ip;
252 	u_short etype = 0;
253 	struct mobile_h mob_h;
254 	struct route *ro;
255 	struct sockaddr_in *ro_dst;
256 
257 	ASSERT_NETISR_NCPUS(mycpuid);
258 
259 	/*
260 	 * gre may cause infinite recursion calls when misconfigured.
261 	 * We'll prevent this by introducing upper limit.
262 	 */
263 	if (++m->m_pkthdr.loop_cnt > max_gre_nesting) {
264 		kprintf("%s: gre_output: packet looped too many times (%d)\n",
265 			if_name(&sc->sc_if), m->m_pkthdr.loop_cnt);
266 		m_freem(m);
267 		error = ELOOP;
268 		goto end;
269 	}
270 
271 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
272 	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
273 		m_freem(m);
274 		error = ENETDOWN;
275 		goto end;
276 	}
277 
278 	ro = &sc->route_pcpu[mycpuid];
279 	ro_dst = (struct sockaddr_in *)&ro->ro_dst;
280 	if (ro->ro_rt != NULL &&
281 	    ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
282 	     ro_dst->sin_addr.s_addr != sc->g_dst.s_addr)) {
283 		RTFREE(ro->ro_rt);
284 		ro->ro_rt = NULL;
285 	}
286 	if (ro->ro_rt == NULL) {
287 		error = gre_compute_route(sc, ro);
288 		if (error) {
289 			m_freem(m);
290 			goto end;
291 		}
292 	}
293 
294 	gh = NULL;
295 	ip = NULL;
296 
297 	if (ifp->if_bpf) {
298 		bpf_gettoken();
299 		if (ifp->if_bpf) {
300 			uint32_t af = dst->sa_family;
301 
302 			bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
303 		}
304 		bpf_reltoken();
305 	}
306 
307 	m->m_flags &= ~(M_BCAST|M_MCAST);
308 
309 	if (sc->g_proto == IPPROTO_MOBILE) {
310 		if (dst->sa_family == AF_INET) {
311 			struct mbuf *m0;
312 			int msiz;
313 
314 			ip = mtod(m, struct ip *);
315 
316 			/*
317 			 * RFC2004 specifies that fragmented datagrams shouldn't
318 			 * be encapsulated.
319 			 */
320 			if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
321 				m_freem(m);
322 				error = EINVAL;    /* is there better errno? */
323 				goto end;
324 			}
325 			memset(&mob_h, 0, MOB_H_SIZ_L);
326 			mob_h.proto = (ip->ip_p) << 8;
327 			mob_h.odst = ip->ip_dst.s_addr;
328 			ip->ip_dst.s_addr = sc->g_dst.s_addr;
329 
330 			/*
331 			 * If the packet comes from our host, we only change
332 			 * the destination address in the IP header.
333 			 * Else we also need to save and change the source
334 			 */
335 			if (in_hosteq(ip->ip_src, sc->g_src)) {
336 				msiz = MOB_H_SIZ_S;
337 			} else {
338 				mob_h.proto |= MOB_H_SBIT;
339 				mob_h.osrc = ip->ip_src.s_addr;
340 				ip->ip_src.s_addr = sc->g_src.s_addr;
341 				msiz = MOB_H_SIZ_L;
342 			}
343 			mob_h.proto = htons(mob_h.proto);
344 			mob_h.hcrc = gre_in_cksum((u_short *)&mob_h, msiz);
345 
346 			if ((m->m_data - msiz) < m->m_pktdat) {
347 				/* need new mbuf */
348 				MGETHDR(m0, M_NOWAIT, MT_HEADER);
349 				if (m0 == NULL) {
350 					m_freem(m);
351 					error = ENOBUFS;
352 					goto end;
353 				}
354 				m0->m_next = m;
355 				m->m_data += sizeof(struct ip);
356 				m->m_len -= sizeof(struct ip);
357 				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
358 				m0->m_len = msiz + sizeof(struct ip);
359 				m0->m_data += max_linkhdr;
360 				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
361 				       sizeof(struct ip));
362 				m = m0;
363 			} else {  /* we have some space left in the old one */
364 				m->m_data -= msiz;
365 				m->m_len += msiz;
366 				m->m_pkthdr.len += msiz;
367 				bcopy(ip, mtod(m, caddr_t),
368 					sizeof(struct ip));
369 			}
370 			ip = mtod(m, struct ip *);
371 			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
372 			ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
373 		} else {  /* AF_INET */
374 			m_freem(m);
375 			error = EINVAL;
376 			goto end;
377 		}
378 	} else if (sc->g_proto == IPPROTO_GRE) {
379 		switch (dst->sa_family) {
380 		case AF_INET:
381 			ip = mtod(m, struct ip *);
382 			etype = ETHERTYPE_IP;
383 			break;
384 		default:
385 			m_freem(m);
386 			error = EAFNOSUPPORT;
387 			goto end;
388 		}
389 		M_PREPEND(m, sizeof(struct greip), M_NOWAIT);
390 	} else {
391 		m_freem(m);
392 		error = EINVAL;
393 		goto end;
394 	}
395 
396 	if (m == NULL) {	/* impossible */
397 		error = ENOBUFS;
398 		goto end;
399 	}
400 
401 	gh = mtod(m, struct greip *);
402 	if (sc->g_proto == IPPROTO_GRE) {
403 		/* we don't have any GRE flags for now */
404 
405 		memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
406 		gh->gi_ptype = htons(etype);
407 	}
408 
409 	gh->gi_pr = sc->g_proto;
410 	if (sc->g_proto != IPPROTO_MOBILE) {
411 		gh->gi_src = sc->g_src;
412 		gh->gi_dst = sc->g_dst;
413 		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
414 		((struct ip*)gh)->ip_ttl = GRE_TTL;
415 		((struct ip*)gh)->ip_tos = ip->ip_tos;
416 		((struct ip*)gh)->ip_id = ip->ip_id;
417 		gh->gi_len = htons(m->m_pkthdr.len);
418 	}
419 
420 	IFNET_STAT_INC(ifp, opackets, 1);
421 	IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
422 	/* send it off */
423 	error = ip_output(m, NULL, ro, IP_DEBUGROUTE, NULL, NULL);
424   end:
425 	if (error)
426 		IFNET_STAT_INC(ifp, oerrors, 1);
427 	return (error);
428 }
429 
430 static int
431 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
432 	   struct rtentry *rt)
433 {
434 	struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
435 	int error;
436 
437 	ifsq_serialize_hw(ifsq);
438 	error = gre_output_serialized(ifp, m, dst, rt);
439 	ifsq_deserialize_hw(ifsq);
440 
441 	return error;
442 }
443 
444 static int
445 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
446 {
447 	struct ifreq *ifr = (struct ifreq *)data;
448 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
449 	struct in_aliasreq *aifr = (struct in_aliasreq *)data;
450 	struct gre_softc *sc = ifp->if_softc;
451 	struct sockaddr_in si;
452 	struct sockaddr *sa = NULL;
453 	int error;
454 	struct sockaddr_in sp, sm, dp, dm;
455 
456 	error = 0;
457 
458 	crit_enter();
459 	switch (cmd) {
460 	case SIOCSIFADDR:
461 		ifp->if_flags |= IFF_UP;
462 		break;
463 	case SIOCSIFDSTADDR:
464 		break;
465 	case SIOCSIFFLAGS:
466 		error = caps_priv_check(cr, SYSCAP_RESTRICTEDROOT |
467 					    __SYSCAP_NULLCRED);
468 		if (error)
469 			break;
470 		if ((ifr->ifr_flags & IFF_LINK0) != 0)
471 			sc->g_proto = IPPROTO_GRE;
472 		else
473 			sc->g_proto = IPPROTO_MOBILE;
474 		goto recompute;
475 	case SIOCSIFMTU:
476 		error = caps_priv_check(cr, SYSCAP_RESTRICTEDROOT |
477 					    __SYSCAP_NULLCRED);
478 		if (error)
479 			break;
480 		if (ifr->ifr_mtu < 576) {
481 			error = EINVAL;
482 			break;
483 		}
484 		ifp->if_mtu = ifr->ifr_mtu;
485 		break;
486 	case SIOCGIFMTU:
487 		ifr->ifr_mtu = sc->sc_if.if_mtu;
488 		break;
489 	case SIOCADDMULTI:
490 	case SIOCDELMULTI:
491 		error = caps_priv_check(cr, SYSCAP_RESTRICTEDROOT |
492 					    __SYSCAP_NULLCRED);
493 		if (error)
494 			break;
495 		if (ifr == NULL) {
496 			error = EAFNOSUPPORT;
497 			break;
498 		}
499 		switch (ifr->ifr_addr.sa_family) {
500 #ifdef INET
501 		case AF_INET:
502 			break;
503 #endif
504 		default:
505 			error = EAFNOSUPPORT;
506 			break;
507 		}
508 		break;
509 	case GRESPROTO:
510 		error = caps_priv_check(cr, SYSCAP_RESTRICTEDROOT |
511 					    __SYSCAP_NULLCRED);
512 		if (error)
513 			break;
514 		sc->g_proto = ifr->ifr_flags;
515 		switch (sc->g_proto) {
516 		case IPPROTO_GRE:
517 			ifp->if_flags |= IFF_LINK0;
518 			break;
519 		case IPPROTO_MOBILE:
520 			ifp->if_flags &= ~IFF_LINK0;
521 			break;
522 		default:
523 			error = EPROTONOSUPPORT;
524 			break;
525 		}
526 		goto recompute;
527 	case GREGPROTO:
528 		ifr->ifr_flags = sc->g_proto;
529 		break;
530 	case GRESADDRS:
531 	case GRESADDRD:
532 		error = caps_priv_check(cr, SYSCAP_RESTRICTEDROOT |
533 					    __SYSCAP_NULLCRED);
534 		if (error)
535 			break;
536 		/*
537 		 * set tunnel endpoints, compute a less specific route
538 		 * to the remote end and mark if as up
539 		 */
540 		sa = &ifr->ifr_addr;
541 		if (cmd == GRESADDRS)
542 			sc->g_src = (satosin(sa))->sin_addr;
543 		if (cmd == GRESADDRD)
544 			sc->g_dst = (satosin(sa))->sin_addr;
545 	recompute:
546 #ifdef INET
547 		if (sc->encap != NULL) {
548 			encap_detach(sc->encap);
549 			sc->encap = NULL;
550 		}
551 #endif
552 		if ((sc->g_src.s_addr != INADDR_ANY) &&
553 		    (sc->g_dst.s_addr != INADDR_ANY)) {
554 			bzero(&sp, sizeof(sp));
555 			bzero(&sm, sizeof(sm));
556 			bzero(&dp, sizeof(dp));
557 			bzero(&dm, sizeof(dm));
558 			sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
559 			    sizeof(struct sockaddr_in);
560 			sp.sin_family = sm.sin_family = dp.sin_family =
561 			    dm.sin_family = AF_INET;
562 			sp.sin_addr = sc->g_src;
563 			dp.sin_addr = sc->g_dst;
564 			sm.sin_addr.s_addr = dm.sin_addr.s_addr =
565 			    INADDR_BROADCAST;
566 #ifdef INET
567 			sc->encap = encap_attach(AF_INET, sc->g_proto,
568 			    sintosa(&sp), sintosa(&sm), sintosa(&dp),
569 			    sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
570 				&in_gre_protosw : &in_mobile_protosw, sc);
571 			if (sc->encap == NULL)
572 				kprintf("%s: unable to attach encap\n",
573 				    if_name(&sc->sc_if));
574 #endif
575 			ifnet_deserialize_all(ifp);
576 			error = gre_check_route(sc);
577 			ifnet_serialize_all(ifp);
578 			if (!error)
579 				ifp->if_flags |= IFF_RUNNING;
580 			else
581 				ifp->if_flags &= ~IFF_RUNNING;
582 		}
583 		break;
584 	case GREGADDRS:
585 		memset(&si, 0, sizeof(si));
586 		si.sin_family = AF_INET;
587 		si.sin_len = sizeof(struct sockaddr_in);
588 		si.sin_addr.s_addr = sc->g_src.s_addr;
589 		sa = sintosa(&si);
590 		ifr->ifr_addr = *sa;
591 		break;
592 	case GREGADDRD:
593 		memset(&si, 0, sizeof(si));
594 		si.sin_family = AF_INET;
595 		si.sin_len = sizeof(struct sockaddr_in);
596 		si.sin_addr.s_addr = sc->g_dst.s_addr;
597 		sa = sintosa(&si);
598 		ifr->ifr_addr = *sa;
599 		break;
600 	case SIOCSIFPHYADDR:
601 		error = caps_priv_check(cr, SYSCAP_RESTRICTEDROOT |
602 					    __SYSCAP_NULLCRED);
603 		if (error)
604 			break;
605 		if (aifr->ifra_addr.sin_family != AF_INET ||
606 		    aifr->ifra_dstaddr.sin_family != AF_INET)
607 		{
608 			error = EAFNOSUPPORT;
609 			break;
610 		}
611 		if (aifr->ifra_addr.sin_len != sizeof(si) ||
612 		    aifr->ifra_dstaddr.sin_len != sizeof(si))
613 		{
614 			error = EINVAL;
615 			break;
616 		}
617 		sc->g_src = aifr->ifra_addr.sin_addr;
618 		sc->g_dst = aifr->ifra_dstaddr.sin_addr;
619 		goto recompute;
620 	case SIOCSLIFPHYADDR:
621 		error = caps_priv_check(cr, SYSCAP_RESTRICTEDROOT |
622 					    __SYSCAP_NULLCRED);
623 		if (error)
624 			break;
625 		if (lifr->addr.ss_family != AF_INET ||
626 		    lifr->dstaddr.ss_family != AF_INET) {
627 			error = EAFNOSUPPORT;
628 			break;
629 		}
630 		if (lifr->addr.ss_len != sizeof(si) ||
631 		    lifr->dstaddr.ss_len != sizeof(si)) {
632 			error = EINVAL;
633 			break;
634 		}
635 		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
636 		sc->g_dst =
637 		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
638 		goto recompute;
639 	case SIOCDIFPHYADDR:
640 		error = caps_priv_check(cr, SYSCAP_RESTRICTEDROOT |
641 					    __SYSCAP_NULLCRED);
642 		if (error)
643 			break;
644 		sc->g_src.s_addr = INADDR_ANY;
645 		sc->g_dst.s_addr = INADDR_ANY;
646 		goto recompute;
647 	case SIOCGLIFPHYADDR:
648 		if (sc->g_src.s_addr == INADDR_ANY ||
649 		    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_src.s_addr;
657 		memcpy(&lifr->addr, &si, sizeof(si));
658 		si.sin_addr.s_addr = sc->g_dst.s_addr;
659 		memcpy(&lifr->dstaddr, &si, sizeof(si));
660 		break;
661 	case SIOCGIFPSRCADDR:
662 		if (sc->g_src.s_addr == INADDR_ANY) {
663 			error = EADDRNOTAVAIL;
664 			break;
665 		}
666 		memset(&si, 0, sizeof(si));
667 		si.sin_family = AF_INET;
668 		si.sin_len = sizeof(struct sockaddr_in);
669 		si.sin_addr.s_addr = sc->g_src.s_addr;
670 		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
671 		break;
672 	case SIOCGIFPDSTADDR:
673 		if (sc->g_dst.s_addr == INADDR_ANY) {
674 			error = EADDRNOTAVAIL;
675 			break;
676 		}
677 		memset(&si, 0, sizeof(si));
678 		si.sin_family = AF_INET;
679 		si.sin_len = sizeof(struct sockaddr_in);
680 		si.sin_addr.s_addr = sc->g_dst.s_addr;
681 		bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
682 		break;
683 	default:
684 		error = EINVAL;
685 		break;
686 	}
687 
688 	crit_exit();
689 	return (error);
690 }
691 
692 /*
693  * computes a route to our destination that is not the one
694  * which would be taken by ip_output(), as this one will loop back to
695  * us. If the interface is p2p as  a--->b, then a routing entry exists
696  * If we now send a packet to b (e.g. ping b), this will come down here
697  * gets src=a, dst=b tacked on and would from ip_ouput() sent back to
698  * if_gre.
699  * Goal here is to compute a route to b that is less specific than
700  * a-->b. We know that this one exists as in normal operation we have
701  * at least a default route which matches.
702  */
703 static int
704 gre_compute_route(struct gre_softc *sc, struct route *ro)
705 {
706 #ifdef DIAGNOSTIC
707 	char abuf[INET_ADDRSTRLEN];
708 #endif
709 	u_int32_t a, b, c;
710 
711 	ASSERT_NETISR_NCPUS(mycpuid);
712 	KASSERT(ro == &sc->route_pcpu[mycpuid], ("route mismatch"));
713 	KASSERT(ro->ro_rt == NULL, ("rtentry not freed"));
714 
715 	memset(ro, 0, sizeof(struct route));
716 	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
717 	ro->ro_dst.sa_family = AF_INET;
718 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
719 
720 	/*
721 	 * toggle last bit, so our interface is not found, but a less
722 	 * specific route. I'd rather like to specify a shorter mask,
723 	 * but this is not possible. Should work though. XXX
724 	 * there is a simpler way ...
725 	 */
726 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
727 		a = ntohl(sc->g_dst.s_addr);
728 		b = a & 0x01;
729 		c = a & 0xfffffffe;
730 		b = b ^ 0x01;
731 		a = b | c;
732 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
733 		    = htonl(a);
734 	}
735 
736 #ifdef DIAGNOSTIC
737 	kprintf("%s: searching a route to %s", if_name(&sc->sc_if),
738 	    kinet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr, abuf));
739 #endif
740 
741 	rtalloc(ro);
742 
743 	/*
744 	 * check if this returned a route at all and this route is no
745 	 * recursion to ourself
746 	 */
747 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
748 #ifdef DIAGNOSTIC
749 		if (ro->ro_rt == NULL)
750 			kprintf(" - no route found!\n");
751 		else
752 			kprintf(" - route loops back to ourself!\n");
753 #endif
754 		return EADDRNOTAVAIL;
755 	}
756 
757 	/*
758 	 * now change it back - else ip_output will just drop
759 	 * the route and search one to this interface ...
760 	 */
761 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
762 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
763 
764 #ifdef DIAGNOSTIC
765 	kprintf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
766 	    kinet_ntoa(
767 	        ((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr, abuf));
768 	kprintf("\n");
769 #endif
770 
771 	return 0;
772 }
773 
774 static void
775 gre_check_route_handler(netmsg_t msg)
776 {
777 	struct gre_softc *sc = msg->base.lmsg.u.ms_resultp;
778 	struct route *ro;
779 	int error;
780 
781 	ASSERT_NETISR0;
782 
783 	ro = &sc->route_pcpu[mycpuid];
784 	if (ro->ro_rt != NULL) {
785 		RTFREE(ro->ro_rt);
786 		ro->ro_rt = NULL;
787 	}
788 	error = gre_compute_route(sc, ro);
789 
790 	netisr_replymsg(&msg->base, error);
791 }
792 
793 static int
794 gre_check_route(struct gre_softc *sc)
795 {
796 	struct netmsg_base msg;
797 
798 	netmsg_init(&msg, NULL, &curthread->td_msgport, MSGF_PRIORITY,
799 	    gre_check_route_handler);
800 	msg.lmsg.u.ms_resultp = sc;
801 
802 	return (netisr_domsg(&msg, 0));
803 }
804 
805 /*
806  * do a checksum of a buffer - much like in_cksum, which operates on
807  * mbufs.
808  */
809 u_short
810 gre_in_cksum(u_short *p, u_int len)
811 {
812 	u_int sum = 0;
813 	int nwords = len >> 1;
814 
815 	while (nwords-- != 0)
816 		sum += *p++;
817 
818 	if (len & 1) {
819 		union {
820 			u_short w;
821 			u_char c[2];
822 		} u;
823 		u.c[0] = *(u_char *)p;
824 		u.c[1] = 0;
825 		sum += u.w;
826 	}
827 
828 	/* end-around-carry */
829 	sum = (sum >> 16) + (sum & 0xffff);
830 	sum += (sum >> 16);
831 	return (~sum);
832 }
833 
834 static int
835 gremodevent(module_t mod, int type, void *data)
836 {
837 
838 	switch (type) {
839 	case MOD_LOAD:
840 		greattach();
841 		break;
842 	case MOD_UNLOAD:
843 		if_clone_detach(&gre_cloner);
844 
845 		while (!LIST_EMPTY(&gre_softc_list))
846 			gre_clone_destroy(&LIST_FIRST(&gre_softc_list)->sc_if);
847 
848 		break;
849 	}
850 	return 0;
851 }
852 
853 static moduledata_t gre_mod = {
854 	"if_gre",
855 	gremodevent,
856 	0
857 };
858 
859 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
860 MODULE_VERSION(if_gre, 1);
861