Home | History | Annotate | Line # | Download | only in net
if_gre.c revision 1.97
      1 /*	$NetBSD: if_gre.c,v 1.97 2007/05/30 21:02:03 christos Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Heiko W.Rupp <hwr (at) pilhuhn.de>
      9  *
     10  * IPv6-over-GRE contributed by Gert Doering <gert (at) greenie.muc.de>
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * Encapsulate L3 protocols into IP
     43  * See RFC 1701 and 1702 for more details.
     44  * If_gre is compatible with Cisco GRE tunnels, so you can
     45  * have a NetBSD box as the other end of a tunnel interface of a Cisco
     46  * router. See gre(4) for more details.
     47  * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.97 2007/05/30 21:02:03 christos Exp $");
     52 
     53 #include "opt_gre.h"
     54 #include "opt_inet.h"
     55 #include "bpfilter.h"
     56 
     57 #ifdef INET
     58 #include <sys/param.h>
     59 #include <sys/file.h>
     60 #include <sys/filedesc.h>
     61 #include <sys/malloc.h>
     62 #include <sys/mbuf.h>
     63 #include <sys/proc.h>
     64 #include <sys/protosw.h>
     65 #include <sys/socket.h>
     66 #include <sys/socketvar.h>
     67 #include <sys/ioctl.h>
     68 #include <sys/queue.h>
     69 #if __NetBSD__
     70 #include <sys/systm.h>
     71 #include <sys/sysctl.h>
     72 #include <sys/kauth.h>
     73 #endif
     74 
     75 #include <sys/kernel.h>
     76 #include <sys/mutex.h>
     77 #include <sys/condvar.h>
     78 #include <sys/kthread.h>
     79 
     80 #include <machine/cpu.h>
     81 
     82 #include <net/ethertypes.h>
     83 #include <net/if.h>
     84 #include <net/if_types.h>
     85 #include <net/netisr.h>
     86 #include <net/route.h>
     87 
     88 #ifdef INET
     89 #include <netinet/in.h>
     90 #include <netinet/in_systm.h>
     91 #include <netinet/in_var.h>
     92 #include <netinet/ip.h>
     93 #include <netinet/ip_var.h>
     94 #else
     95 #error "Huh? if_gre without inet?"
     96 #endif
     97 
     98 
     99 #ifdef NETATALK
    100 #include <netatalk/at.h>
    101 #include <netatalk/at_var.h>
    102 #include <netatalk/at_extern.h>
    103 #endif
    104 
    105 #if NBPFILTER > 0
    106 #include <sys/time.h>
    107 #include <net/bpf.h>
    108 #endif
    109 
    110 #include <net/if_gre.h>
    111 
    112 #include <compat/sys/sockio.h>
    113 /*
    114  * It is not easy to calculate the right value for a GRE MTU.
    115  * We leave this task to the admin and use the same default that
    116  * other vendors use.
    117  */
    118 #define GREMTU 1476
    119 
    120 #ifdef GRE_DEBUG
    121 #define	GRE_DPRINTF(__sc, __fmt, ...)				\
    122 	do {							\
    123 		if (((__sc)->sc_if.if_flags & IFF_DEBUG) != 0)	\
    124 			printf(__fmt, __VA_ARGS__);		\
    125 	} while (/*CONSTCOND*/0)
    126 #else
    127 #define	GRE_DPRINTF(__sc, __fmt, ...)	do { } while (/*CONSTCOND*/0)
    128 #endif /* GRE_DEBUG */
    129 
    130 struct gre_softc_head gre_softc_list;
    131 int ip_gre_ttl = GRE_TTL;
    132 
    133 static int	gre_clone_create(struct if_clone *, int);
    134 static int	gre_clone_destroy(struct ifnet *);
    135 
    136 static struct if_clone gre_cloner =
    137     IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
    138 
    139 static int	gre_output(struct ifnet *, struct mbuf *,
    140 			   const struct sockaddr *, struct rtentry *);
    141 static int	gre_ioctl(struct ifnet *, u_long, void *);
    142 
    143 static int	gre_compute_route(struct gre_softc *sc);
    144 
    145 static void gre_closef(struct file **, struct lwp *);
    146 static int gre_getsockname(struct socket *, struct mbuf *, struct lwp *);
    147 static int gre_getpeername(struct socket *, struct mbuf *, struct lwp *);
    148 static int gre_getnames(struct socket *, struct lwp *, struct sockaddr_in *,
    149     struct sockaddr_in *);
    150 
    151 /* Calling thread must hold sc->sc_mtx. */
    152 static void
    153 gre_stop(struct gre_softc *sc)
    154 {
    155 	sc->sc_running = 0;
    156 	cv_signal(&sc->sc_join_cv);
    157 }
    158 
    159 /* Calling thread must hold sc->sc_mtx. */
    160 static void
    161 gre_join(struct gre_softc *sc)
    162 {
    163 	while (sc->sc_running != 0)
    164 		cv_wait(&sc->sc_join_cv, &sc->sc_mtx);
    165 }
    166 
    167 /* Calling thread must hold sc->sc_mtx. */
    168 static void
    169 gre_wakeup(struct gre_softc *sc)
    170 {
    171 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    172 	sc->sc_haswork = 1;
    173 	cv_signal(&sc->sc_work_cv);
    174 }
    175 
    176 static int
    177 gre_clone_create(struct if_clone *ifc, int unit)
    178 {
    179 	struct gre_softc *sc;
    180 
    181 	sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
    182 	memset(sc, 0, sizeof(struct gre_softc));
    183 	mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_NET);
    184 	cv_init(&sc->sc_work_cv, "gre work");
    185 	cv_init(&sc->sc_join_cv, "gre join");
    186 	cv_init(&sc->sc_soparm_cv, "gre soparm");
    187 
    188 	snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
    189 	    ifc->ifc_name, unit);
    190 	sc->sc_if.if_softc = sc;
    191 	sc->sc_if.if_type = IFT_TUNNEL;
    192 	sc->sc_if.if_addrlen = 0;
    193 	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
    194 	sc->sc_if.if_dlt = DLT_NULL;
    195 	sc->sc_if.if_mtu = GREMTU;
    196 	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
    197 	sc->sc_if.if_output = gre_output;
    198 	sc->sc_if.if_ioctl = gre_ioctl;
    199 	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
    200 	sc->g_dstport = sc->g_srcport = 0;
    201 	sc->sc_proto = IPPROTO_GRE;
    202 	sc->sc_snd.ifq_maxlen = 256;
    203 	sc->sc_if.if_flags |= IFF_LINK0;
    204 	if_attach(&sc->sc_if);
    205 	if_alloc_sadl(&sc->sc_if);
    206 #if NBPFILTER > 0
    207 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
    208 #endif
    209 	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
    210 	return 0;
    211 }
    212 
    213 static int
    214 gre_clone_destroy(struct ifnet *ifp)
    215 {
    216 	struct gre_softc *sc = ifp->if_softc;
    217 
    218 	LIST_REMOVE(sc, sc_list);
    219 #if NBPFILTER > 0
    220 	bpfdetach(ifp);
    221 #endif
    222 	if_detach(ifp);
    223 	mutex_enter(&sc->sc_mtx);
    224 	gre_wakeup(sc);
    225 	gre_join(sc);
    226 	mutex_exit(&sc->sc_mtx);
    227 	rtcache_free(&sc->route);
    228 
    229 	cv_destroy(&sc->sc_soparm_cv);
    230 	cv_destroy(&sc->sc_join_cv);
    231 	cv_destroy(&sc->sc_work_cv);
    232 	mutex_destroy(&sc->sc_mtx);
    233 	free(sc, M_DEVBUF);
    234 
    235 	return 0;
    236 }
    237 
    238 static void
    239 gre_receive(struct socket *so, void *arg, int waitflag)
    240 {
    241 	struct gre_softc *sc = (struct gre_softc *)arg;
    242 
    243 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    244 
    245 	gre_wakeup(sc);
    246 }
    247 
    248 static void
    249 gre_upcall_add(struct socket *so, void *arg)
    250 {
    251 	/* XXX What if the kernel already set an upcall? */
    252 	so->so_upcallarg = arg;
    253 	so->so_upcall = gre_receive;
    254 	so->so_rcv.sb_flags |= SB_UPCALL;
    255 }
    256 
    257 static void
    258 gre_upcall_remove(struct socket *so)
    259 {
    260 	/* XXX What if the kernel already set an upcall? */
    261 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    262 	so->so_upcallarg = NULL;
    263 	so->so_upcall = NULL;
    264 }
    265 
    266 static void
    267 gre_sodestroy(struct socket **sop)
    268 {
    269 	gre_upcall_remove(*sop);
    270 	soshutdown(*sop, SHUT_RDWR);
    271 	soclose(*sop);
    272 	*sop = NULL;
    273 }
    274 
    275 static struct mbuf *
    276 gre_getsockmbuf(struct socket *so)
    277 {
    278 	struct mbuf *m;
    279 
    280 	m = m_get(M_WAIT, MT_SONAME);
    281 	if (m != NULL)
    282 		MCLAIM(m, so->so_mowner);
    283 	return m;
    284 }
    285 
    286 static int
    287 gre_socreate1(struct gre_softc *sc, struct lwp *l, struct gre_soparm *sp,
    288     struct socket **sop)
    289 {
    290 	int rc;
    291 	struct mbuf *m;
    292 	struct sockaddr_in *sin;
    293 	struct socket *so;
    294 
    295 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    296 	rc = socreate(AF_INET, sop, SOCK_DGRAM, IPPROTO_UDP, l);
    297 	if (rc != 0) {
    298 		GRE_DPRINTF(sc, "%s: socreate failed\n", __func__);
    299 		return rc;
    300 	}
    301 
    302 	so = *sop;
    303 
    304 	gre_upcall_add(so, sc);
    305 	if ((m = gre_getsockmbuf(so)) == NULL) {
    306 		rc = ENOBUFS;
    307 		goto out;
    308 	}
    309 	sin = mtod(m, struct sockaddr_in *);
    310 	sin->sin_len = m->m_len = sizeof(struct sockaddr_in);
    311 	sin->sin_family = AF_INET;
    312 	sin->sin_addr = sc->g_src;
    313 	sin->sin_port = sc->g_srcport;
    314 
    315 	GRE_DPRINTF(sc, "%s: bind 0x%08" PRIx32 " port %d\n", __func__,
    316 	    sin->sin_addr.s_addr, ntohs(sin->sin_port));
    317 	if ((rc = sobind(so, m, l)) != 0) {
    318 		GRE_DPRINTF(sc, "%s: sobind failed\n", __func__);
    319 		goto out;
    320 	}
    321 
    322 	if (sc->g_srcport == 0) {
    323 		if ((rc = gre_getsockname(so, m, l)) != 0) {
    324 			GRE_DPRINTF(sc, "%s: gre_getsockname failed\n",
    325 			    __func__);
    326 			goto out;
    327 		}
    328 		sc->g_srcport = sin->sin_port;
    329 	}
    330 
    331 	sin->sin_addr = sc->g_dst;
    332 	sin->sin_port = sc->g_dstport;
    333 
    334 	if ((rc = soconnect(so, m, l)) != 0) {
    335 		GRE_DPRINTF(sc, "%s: soconnect failed\n", __func__);
    336 		goto out;
    337 	}
    338 
    339 	*mtod(m, int *) = ip_gre_ttl;
    340 	m->m_len = sizeof(int);
    341 	rc = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, IPPROTO_IP, IP_TTL,
    342 	    &m);
    343 	m = NULL;
    344 	if (rc != 0) {
    345 		printf("%s: setopt ttl failed\n", __func__);
    346 		rc = 0;
    347 	}
    348 out:
    349 	m_freem(m);
    350 
    351 	if (rc != 0)
    352 		gre_sodestroy(sop);
    353 	else
    354 		*sp = sc->sc_soparm;
    355 
    356 	return rc;
    357 }
    358 
    359 static void
    360 gre_thread1(struct gre_softc *sc, struct lwp *l)
    361 {
    362 	int flags, rc;
    363 	const struct gre_h *gh;
    364 	struct ifnet *ifp = &sc->sc_if;
    365 	struct mbuf *m;
    366 	struct socket *so = NULL;
    367 	struct uio uio;
    368 	struct gre_soparm sp;
    369 	struct file *fp = NULL;
    370 
    371 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    372 	mutex_enter(&sc->sc_mtx);
    373 
    374 	sc->sc_haswork = 1;
    375 
    376 	memset(&sp, 0, sizeof(sp));
    377 	memset(&uio, 0, sizeof(uio));
    378 
    379 	ifp->if_flags |= IFF_RUNNING;
    380 
    381 	for (;;) {
    382 		while (sc->sc_haswork == 0) {
    383 			GRE_DPRINTF(sc, "%s: sleeping\n", __func__);
    384 			cv_wait(&sc->sc_work_cv, &sc->sc_mtx);
    385 		}
    386 		sc->sc_haswork = 0;
    387 		GRE_DPRINTF(sc, "%s: awake\n", __func__);
    388 		if ((ifp->if_flags & IFF_UP) != IFF_UP) {
    389 			GRE_DPRINTF(sc, "%s: not up & running; exiting\n",
    390 			    __func__);
    391 			break;
    392 		}
    393 		if (sc->sc_proto != IPPROTO_UDP) {
    394 			GRE_DPRINTF(sc, "%s: not udp; exiting\n", __func__);
    395 			break;
    396 		}
    397 		/* XXX optimize */
    398 		if (so == NULL || sc->sc_fp != NULL ||
    399 		    memcmp(&sp, &sc->sc_soparm, sizeof(sp)) != 0) {
    400 			GRE_DPRINTF(sc, "%s: parameters changed\n", __func__);
    401 
    402 			if (fp != NULL) {
    403 				gre_closef(&fp, curlwp);
    404 				so = NULL;
    405 			} else if (so != NULL)
    406 				gre_sodestroy(&so);
    407 
    408 			if (sc->sc_fp != NULL) {
    409 				fp = sc->sc_fp;
    410 				sc->sc_fp = NULL;
    411 				so = (struct socket *)fp->f_data;
    412 				gre_upcall_add(so, sc);
    413 				sp = sc->sc_soparm;
    414 			} else if (gre_socreate1(sc, l, &sp, &so) != 0)
    415 				goto out;
    416 		}
    417 		cv_signal(&sc->sc_soparm_cv);
    418 		for (;;) {
    419 			flags = MSG_DONTWAIT;
    420 			uio.uio_resid = 1000000;
    421 			rc = (*so->so_receive)(so, NULL, &uio, &m, NULL,
    422 			    &flags);
    423 			/* TBD Back off if ECONNREFUSED (indicates
    424 			 * ICMP Port Unreachable)?
    425 			 */
    426 			if (rc == EWOULDBLOCK) {
    427 				GRE_DPRINTF(sc, "%s: so_receive EWOULDBLOCK\n",
    428 				    __func__);
    429 				break;
    430 			} else if (rc != 0 || m == NULL) {
    431 				GRE_DPRINTF(sc, "%s: rc %d m %p\n",
    432 				    ifp->if_xname, rc, (void *)m);
    433 				continue;
    434 			} else
    435 				GRE_DPRINTF(sc, "%s: so_receive ok\n",
    436 				    __func__);
    437 			if (m->m_len < sizeof(*gh) &&
    438 			    (m = m_pullup(m, sizeof(*gh))) == NULL) {
    439 				GRE_DPRINTF(sc, "%s: m_pullup failed\n",
    440 				    __func__);
    441 				continue;
    442 			}
    443 			gh = mtod(m, const struct gre_h *);
    444 
    445 			if (gre_input3(sc, m, 0, gh, 1) == 0) {
    446 				GRE_DPRINTF(sc, "%s: dropping unsupported\n",
    447 				    __func__);
    448 				m_freem(m);
    449 			}
    450 		}
    451 		for (;;) {
    452 			IF_DEQUEUE(&sc->sc_snd, m);
    453 			if (m == NULL)
    454 				break;
    455 			GRE_DPRINTF(sc, "%s: dequeue\n", __func__);
    456 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    457 				GRE_DPRINTF(sc, "%s: not connected\n",
    458 				    __func__);
    459 				m_freem(m);
    460 				continue;
    461 			}
    462 			rc = (*so->so_send)(so, NULL, NULL, m, NULL, 0, l);
    463 			/* XXX handle ENOBUFS? */
    464 			if (rc != 0)
    465 				GRE_DPRINTF(sc, "%s: so_send failed\n",
    466 				    __func__);
    467 		}
    468 	}
    469 	if (fp != NULL) {
    470 		GRE_DPRINTF(sc, "%s: removing upcall\n", __func__);
    471 		gre_upcall_remove(so);
    472 	} else if (so != NULL)
    473 		gre_sodestroy(&so);
    474 out:
    475 	GRE_DPRINTF(sc, "%s: stopping\n", __func__);
    476 	if (fp != NULL)
    477 		gre_closef(&fp, curlwp);
    478 	if (sc->sc_proto == IPPROTO_UDP)
    479 		ifp->if_flags &= ~IFF_RUNNING;
    480 	while (!IF_IS_EMPTY(&sc->sc_snd)) {
    481 		IF_DEQUEUE(&sc->sc_snd, m);
    482 		m_freem(m);
    483 	}
    484 	gre_stop(sc);
    485 	/* must not touch sc after this! */
    486 	GRE_DPRINTF(sc, "%s: restore ipl\n", __func__);
    487 	mutex_exit(&sc->sc_mtx);
    488 }
    489 
    490 static void
    491 gre_thread(void *arg)
    492 {
    493 	struct gre_softc *sc = (struct gre_softc *)arg;
    494 
    495 	gre_thread1(sc, curlwp);
    496 	/* must not touch sc after this! */
    497 	kthread_exit(0);
    498 }
    499 
    500 /* Calling thread must hold sc->sc_mtx. */
    501 int
    502 gre_input3(struct gre_softc *sc, struct mbuf *m, int hlen,
    503     const struct gre_h *gh, int mtx_held)
    504 {
    505 	u_int16_t flags;
    506 #if NBPFILTER > 0
    507 	u_int32_t af = AF_INET;		/* af passed to BPF tap */
    508 #endif
    509 	int isr;
    510 	struct ifqueue *ifq;
    511 
    512 	sc->sc_if.if_ipackets++;
    513 	sc->sc_if.if_ibytes += m->m_pkthdr.len;
    514 
    515 	hlen += sizeof(struct gre_h);
    516 
    517 	/* process GRE flags as packet can be of variable len */
    518 	flags = ntohs(gh->flags);
    519 
    520 	/* Checksum & Offset are present */
    521 	if ((flags & GRE_CP) | (flags & GRE_RP))
    522 		hlen += 4;
    523 	/* We don't support routing fields (variable length) */
    524 	if (flags & GRE_RP) {
    525 		sc->sc_if.if_ierrors++;
    526 		return 0;
    527 	}
    528 	if (flags & GRE_KP)
    529 		hlen += 4;
    530 	if (flags & GRE_SP)
    531 		hlen += 4;
    532 
    533 	switch (ntohs(gh->ptype)) { /* ethertypes */
    534 	case ETHERTYPE_IP: /* shouldn't need a schednetisr(), as */
    535 		ifq = &ipintrq;          /* we are in ip_input */
    536 		isr = NETISR_IP;
    537 		break;
    538 #ifdef NETATALK
    539 	case ETHERTYPE_ATALK:
    540 		ifq = &atintrq1;
    541 		isr = NETISR_ATALK;
    542 #if NBPFILTER > 0
    543 		af = AF_APPLETALK;
    544 #endif
    545 		break;
    546 #endif
    547 #ifdef INET6
    548 	case ETHERTYPE_IPV6:
    549 		GRE_DPRINTF(sc, "%s: IPv6 packet\n", __func__);
    550 		ifq = &ip6intrq;
    551 		isr = NETISR_IPV6;
    552 #if NBPFILTER > 0
    553 		af = AF_INET6;
    554 #endif
    555 		break;
    556 #endif
    557 	default:	   /* others not yet supported */
    558 		GRE_DPRINTF(sc, "%s: unhandled ethertype 0x%04x\n", __func__,
    559 		    ntohs(gh->ptype));
    560 		sc->sc_if.if_noproto++;
    561 		return 0;
    562 	}
    563 
    564 	if (hlen > m->m_pkthdr.len) {
    565 		m_freem(m);
    566 		sc->sc_if.if_ierrors++;
    567 		return EINVAL;
    568 	}
    569 	m_adj(m, hlen);
    570 
    571 #if NBPFILTER > 0
    572 	if (sc->sc_if.if_bpf != NULL)
    573 		bpf_mtap_af(sc->sc_if.if_bpf, af, m);
    574 #endif /*NBPFILTER > 0*/
    575 
    576 	m->m_pkthdr.rcvif = &sc->sc_if;
    577 
    578 	if (!mtx_held)
    579 		mutex_enter(&sc->sc_mtx);
    580 	if (IF_QFULL(ifq)) {
    581 		IF_DROP(ifq);
    582 		m_freem(m);
    583 	} else {
    584 		IF_ENQUEUE(ifq, m);
    585 	}
    586 	/* we need schednetisr since the address family may change */
    587 	schednetisr(isr);
    588 	if (!mtx_held)
    589 		mutex_exit(&sc->sc_mtx);
    590 
    591 	return 1;	/* packet is done, no further processing needed */
    592 }
    593 
    594 /*
    595  * The output routine. Takes a packet and encapsulates it in the protocol
    596  * given by sc->sc_proto. See also RFC 1701 and RFC 2004
    597  */
    598 static int
    599 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    600 	   struct rtentry *rt)
    601 {
    602 	int error = 0, hlen, msiz;
    603 	struct gre_softc *sc = ifp->if_softc;
    604 	struct greip *gi;
    605 	struct gre_h *gh;
    606 	struct ip *eip, *ip;
    607 	u_int8_t ip_tos = 0;
    608 	u_int16_t etype = 0;
    609 	struct mobile_h mob_h;
    610 
    611 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
    612 	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
    613 		m_freem(m);
    614 		error = ENETDOWN;
    615 		goto end;
    616 	}
    617 
    618 	gi = NULL;
    619 	ip = NULL;
    620 
    621 #if NBPFILTER >0
    622 	if (ifp->if_bpf)
    623 		bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
    624 #endif
    625 
    626 	m->m_flags &= ~(M_BCAST|M_MCAST);
    627 
    628 	switch (sc->sc_proto) {
    629 	case IPPROTO_MOBILE:
    630 		if (dst->sa_family != AF_INET) {
    631 			IF_DROP(&ifp->if_snd);
    632 			m_freem(m);
    633 			error = EINVAL;
    634 			goto end;
    635 		}
    636 
    637 		if (M_UNWRITABLE(m, sizeof(*ip)) &&
    638 		    (m = m_pullup(m, sizeof(*ip))) == NULL) {
    639 			error = ENOBUFS;
    640 			goto end;
    641 		}
    642 		ip = mtod(m, struct ip *);
    643 
    644 		memset(&mob_h, 0, MOB_H_SIZ_L);
    645 		mob_h.proto = (ip->ip_p) << 8;
    646 		mob_h.odst = ip->ip_dst.s_addr;
    647 		ip->ip_dst.s_addr = sc->g_dst.s_addr;
    648 
    649 		/*
    650 		 * If the packet comes from our host, we only change
    651 		 * the destination address in the IP header.
    652 		 * Else we also need to save and change the source
    653 		 */
    654 		if (in_hosteq(ip->ip_src, sc->g_src)) {
    655 			msiz = MOB_H_SIZ_S;
    656 		} else {
    657 			mob_h.proto |= MOB_H_SBIT;
    658 			mob_h.osrc = ip->ip_src.s_addr;
    659 			ip->ip_src.s_addr = sc->g_src.s_addr;
    660 			msiz = MOB_H_SIZ_L;
    661 		}
    662 		HTONS(mob_h.proto);
    663 		mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
    664 
    665 		M_PREPEND(m, msiz, M_DONTWAIT);
    666 		if (m == NULL) {
    667 			error = ENOBUFS;
    668 			goto end;
    669 		}
    670 		/* XXX Assuming that ip does not dangle after
    671 		 * M_PREPEND.  In practice, that's true, but
    672 		 * that's not in M_PREPEND's contract.
    673 		 */
    674 		memmove(mtod(m, void *), ip, sizeof(*ip));
    675 		ip = mtod(m, struct ip *);
    676 		memcpy(ip + 1, &mob_h, (size_t)msiz);
    677 		ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
    678 		break;
    679 	case IPPROTO_UDP:
    680 	case IPPROTO_GRE:
    681 		GRE_DPRINTF(sc, "%s: dst->sa_family=%d\n", __func__,
    682 		    dst->sa_family);
    683 		switch (dst->sa_family) {
    684 		case AF_INET:
    685 			ip = mtod(m, struct ip *);
    686 			ip_tos = ip->ip_tos;
    687 			etype = ETHERTYPE_IP;
    688 			break;
    689 #ifdef NETATALK
    690 		case AF_APPLETALK:
    691 			etype = ETHERTYPE_ATALK;
    692 			break;
    693 #endif
    694 #ifdef INET6
    695 		case AF_INET6:
    696 			etype = ETHERTYPE_IPV6;
    697 			break;
    698 #endif
    699 		default:
    700 			IF_DROP(&ifp->if_snd);
    701 			m_freem(m);
    702 			error = EAFNOSUPPORT;
    703 			goto end;
    704 		}
    705 		break;
    706 	default:
    707 		IF_DROP(&ifp->if_snd);
    708 		m_freem(m);
    709 		error = EINVAL;
    710 		goto end;
    711 	}
    712 
    713 	switch (sc->sc_proto) {
    714 	case IPPROTO_GRE:
    715 		hlen = sizeof(struct greip);
    716 		break;
    717 	case IPPROTO_UDP:
    718 		hlen = sizeof(struct gre_h);
    719 		break;
    720 	default:
    721 		hlen = 0;
    722 		break;
    723 	}
    724 
    725 	M_PREPEND(m, hlen, M_DONTWAIT);
    726 
    727 	if (m == NULL) {
    728 		IF_DROP(&ifp->if_snd);
    729 		error = ENOBUFS;
    730 		goto end;
    731 	}
    732 
    733 	switch (sc->sc_proto) {
    734 	case IPPROTO_UDP:
    735 		gh = mtod(m, struct gre_h *);
    736 		memset(gh, 0, sizeof(*gh));
    737 		gh->ptype = htons(etype);
    738 		/* XXX Need to handle IP ToS.  Look at how I handle IP TTL. */
    739 		break;
    740 	case IPPROTO_GRE:
    741 		gi = mtod(m, struct greip *);
    742 		gh = &gi->gi_g;
    743 		eip = &gi->gi_i;
    744 		/* we don't have any GRE flags for now */
    745 		memset(gh, 0, sizeof(*gh));
    746 		gh->ptype = htons(etype);
    747 		eip->ip_src = sc->g_src;
    748 		eip->ip_dst = sc->g_dst;
    749 		eip->ip_hl = (sizeof(struct ip)) >> 2;
    750 		eip->ip_ttl = ip_gre_ttl;
    751 		eip->ip_tos = ip_tos;
    752 		eip->ip_len = htons(m->m_pkthdr.len);
    753 		eip->ip_p = sc->sc_proto;
    754 		break;
    755 	case IPPROTO_MOBILE:
    756 		eip = mtod(m, struct ip *);
    757 		eip->ip_p = sc->sc_proto;
    758 		break;
    759 	default:
    760 		error = EPROTONOSUPPORT;
    761 		m_freem(m);
    762 		goto end;
    763 	}
    764 
    765 	ifp->if_opackets++;
    766 	ifp->if_obytes += m->m_pkthdr.len;
    767 
    768 	/* send it off */
    769 	if (sc->sc_proto == IPPROTO_UDP) {
    770 		if (IF_QFULL(&sc->sc_snd)) {
    771 			IF_DROP(&sc->sc_snd);
    772 			error = ENOBUFS;
    773 			m_freem(m);
    774 		} else {
    775 			IF_ENQUEUE(&sc->sc_snd, m);
    776 			gre_wakeup(sc);
    777 			error = 0;
    778 		}
    779 		goto end;
    780 	}
    781 	if (sc->route.ro_rt == NULL)
    782 		rtcache_init(&sc->route);
    783 	else
    784 		rtcache_check(&sc->route);
    785 	if (sc->route.ro_rt == NULL) {
    786 		m_freem(m);
    787 		goto end;
    788 	}
    789 	if (sc->route.ro_rt->rt_ifp->if_softc == sc) {
    790 		rtcache_clear(&sc->route);
    791 		m_freem(m);
    792 	} else
    793 		error = ip_output(m, NULL, &sc->route, 0, NULL, NULL);
    794   end:
    795 	if (error)
    796 		ifp->if_oerrors++;
    797 	return error;
    798 }
    799 
    800 /* Calling thread must hold sc->sc_mtx. */
    801 static int
    802 gre_kick(struct gre_softc *sc)
    803 {
    804 	int rc;
    805 	struct ifnet *ifp = &sc->sc_if;
    806 
    807 	if (sc->sc_proto == IPPROTO_UDP && (ifp->if_flags & IFF_UP) == IFF_UP &&
    808 	    !sc->sc_running) {
    809 		sc->sc_running = 1;
    810 		mutex_exit(&sc->sc_mtx);
    811 		rc = kthread_create1(gre_thread, sc, NULL, ifp->if_xname);
    812 		mutex_enter(&sc->sc_mtx);
    813 		if (rc != 0)
    814 			gre_stop(sc);
    815 		return rc;
    816 	} else {
    817 		gre_wakeup(sc);
    818 		return 0;
    819 	}
    820 }
    821 
    822 /* Calling thread must hold sc->sc_mtx. */
    823 static int
    824 gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
    825 {
    826 	return (*so->so_proto->pr_usrreq)(so, req, NULL, nam, NULL, l);
    827 }
    828 
    829 /* Calling thread must hold sc->sc_mtx. */
    830 static int
    831 gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
    832 {
    833 	return gre_getname(so, PRU_SOCKADDR, nam, l);
    834 }
    835 
    836 /* Calling thread must hold sc->sc_mtx. */
    837 static int
    838 gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
    839 {
    840 	return gre_getname(so, PRU_PEERADDR, nam, l);
    841 }
    842 
    843 /* Calling thread must hold sc->sc_mtx. */
    844 static int
    845 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_in *src,
    846     struct sockaddr_in *dst)
    847 {
    848 	struct mbuf *m;
    849 	struct sockaddr_in *sin;
    850 	int rc;
    851 
    852 	if ((m = gre_getsockmbuf(so)) == NULL)
    853 		return ENOBUFS;
    854 
    855 	sin = mtod(m, struct sockaddr_in *);
    856 
    857 	if ((rc = gre_getsockname(so, m, l)) != 0)
    858 		goto out;
    859 	if (sin->sin_family != AF_INET) {
    860 		rc = EAFNOSUPPORT;
    861 		goto out;
    862 	}
    863 	*src = *sin;
    864 
    865 	if ((rc = gre_getpeername(so, m, l)) != 0)
    866 		goto out;
    867 	if (sin->sin_family != AF_INET) {
    868 		rc = EAFNOSUPPORT;
    869 		goto out;
    870 	}
    871 	*dst = *sin;
    872 
    873 out:
    874 	m_freem(m);
    875 	return rc;
    876 }
    877 
    878 static void
    879 gre_closef(struct file **fpp, struct lwp *l)
    880 {
    881 	struct file *fp = *fpp;
    882 
    883 	simple_lock(&fp->f_slock);
    884 	FILE_USE(fp);
    885 	closef(fp, l);
    886 	*fpp = NULL;
    887 }
    888 
    889 static int
    890 gre_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    891 {
    892 	u_char oproto;
    893 	struct file *fp;
    894 	struct socket *so;
    895 	struct sockaddr_in dst, src;
    896 	struct proc *p = curproc;	/* XXX */
    897 	struct lwp *l = curlwp;	/* XXX */
    898 	struct ifreq *ifr;
    899 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
    900 	struct gre_softc *sc = ifp->if_softc;
    901 	struct sockaddr_in si;
    902 	struct sockaddr *sa = NULL;
    903 	int error = 0;
    904 #ifdef COMPAT_OIFREQ
    905 	u_long ocmd = cmd;
    906 	struct oifreq *oifr = NULL;
    907 	struct ifreq ifrb;
    908 
    909 	cmd = cvtcmd(cmd);
    910 	if (cmd != ocmd) {
    911 		oifr = data;
    912 		data = ifr = &ifrb;
    913 		ifreqo2n(oifr, ifr);
    914 	} else
    915 #endif
    916 		ifr = data;
    917 
    918 	switch (cmd) {
    919 	case SIOCSIFFLAGS:
    920 	case SIOCSIFMTU:
    921 	case GRESPROTO:
    922 	case GRESADDRD:
    923 	case GRESADDRS:
    924 	case GRESSOCK:
    925 	case GREDSOCK:
    926 	case SIOCSLIFPHYADDR:
    927 	case SIOCDIFPHYADDR:
    928 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
    929 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    930 		    NULL) != 0)
    931 			return EPERM;
    932 		break;
    933 	default:
    934 		break;
    935 	}
    936 
    937 	mutex_enter(&sc->sc_mtx);
    938 	switch (cmd) {
    939 	case SIOCSIFADDR:
    940 		ifp->if_flags |= IFF_UP;
    941 		if ((error = gre_kick(sc)) != 0)
    942 			ifp->if_flags &= ~IFF_UP;
    943 		break;
    944 	case SIOCSIFDSTADDR:
    945 		break;
    946 	case SIOCSIFFLAGS:
    947 		oproto = sc->sc_proto;
    948 		switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
    949 		case IFF_LINK0|IFF_LINK2:
    950 			sc->sc_proto = IPPROTO_UDP;
    951 			if (oproto != IPPROTO_UDP)
    952 				ifp->if_flags &= ~IFF_RUNNING;
    953 			error = gre_kick(sc);
    954 			break;
    955 		case IFF_LINK0:
    956 			sc->sc_proto = IPPROTO_GRE;
    957 			gre_wakeup(sc);
    958 			goto recompute;
    959 		case 0:
    960 			sc->sc_proto = IPPROTO_MOBILE;
    961 			gre_wakeup(sc);
    962 			goto recompute;
    963 		}
    964 		break;
    965 	case SIOCSIFMTU:
    966 		if (ifr->ifr_mtu < 576) {
    967 			error = EINVAL;
    968 			break;
    969 		}
    970 		ifp->if_mtu = ifr->ifr_mtu;
    971 		break;
    972 	case SIOCGIFMTU:
    973 		ifr->ifr_mtu = sc->sc_if.if_mtu;
    974 		break;
    975 	case SIOCADDMULTI:
    976 	case SIOCDELMULTI:
    977 		if (ifr == 0) {
    978 			error = EAFNOSUPPORT;
    979 			break;
    980 		}
    981 		switch (ifr->ifr_addr.sa_family) {
    982 #ifdef INET
    983 		case AF_INET:
    984 			break;
    985 #endif
    986 #ifdef INET6
    987 		case AF_INET6:
    988 			break;
    989 #endif
    990 		default:
    991 			error = EAFNOSUPPORT;
    992 			break;
    993 		}
    994 		break;
    995 	case GRESPROTO:
    996 		oproto = sc->sc_proto;
    997 		sc->sc_proto = ifr->ifr_flags;
    998 		switch (sc->sc_proto) {
    999 		case IPPROTO_UDP:
   1000 			ifp->if_flags |= IFF_LINK0|IFF_LINK2;
   1001 			if (oproto != IPPROTO_UDP)
   1002 				ifp->if_flags &= ~IFF_RUNNING;
   1003 			error = gre_kick(sc);
   1004 			break;
   1005 		case IPPROTO_GRE:
   1006 			ifp->if_flags |= IFF_LINK0;
   1007 			ifp->if_flags &= ~IFF_LINK2;
   1008 			goto recompute;
   1009 		case IPPROTO_MOBILE:
   1010 			ifp->if_flags &= ~(IFF_LINK0|IFF_LINK2);
   1011 			goto recompute;
   1012 		default:
   1013 			error = EPROTONOSUPPORT;
   1014 			break;
   1015 		}
   1016 		break;
   1017 	case GREGPROTO:
   1018 		ifr->ifr_flags = sc->sc_proto;
   1019 		break;
   1020 	case GRESADDRS:
   1021 	case GRESADDRD:
   1022 		/*
   1023 		 * set tunnel endpoints, compute a less specific route
   1024 		 * to the remote end and mark if as up
   1025 		 */
   1026 		sa = &ifr->ifr_addr;
   1027 		if (cmd == GRESADDRS) {
   1028 			sc->g_src = (satosin(sa))->sin_addr;
   1029 			sc->g_srcport = satosin(sa)->sin_port;
   1030 		}
   1031 		if (cmd == GRESADDRD) {
   1032 			if (sc->sc_proto == IPPROTO_UDP &&
   1033 			    satosin(sa)->sin_port == 0) {
   1034 				error = EINVAL;
   1035 				break;
   1036 			}
   1037 			sc->g_dst = (satosin(sa))->sin_addr;
   1038 			sc->g_dstport = satosin(sa)->sin_port;
   1039 		}
   1040 	recompute:
   1041 		if (sc->sc_proto == IPPROTO_UDP ||
   1042 		    (sc->g_src.s_addr != INADDR_ANY &&
   1043 		     sc->g_dst.s_addr != INADDR_ANY)) {
   1044 			rtcache_free(&sc->route);
   1045 			if (sc->sc_proto == IPPROTO_UDP)
   1046 				error = gre_kick(sc);
   1047 			else if (gre_compute_route(sc) == 0)
   1048 				ifp->if_flags |= IFF_RUNNING;
   1049 			else
   1050 				ifp->if_flags &= ~IFF_RUNNING;
   1051 		}
   1052 		break;
   1053 	case GREGADDRS:
   1054 		memset(&si, 0, sizeof(si));
   1055 		si.sin_family = AF_INET;
   1056 		si.sin_len = sizeof(struct sockaddr_in);
   1057 		si.sin_addr.s_addr = sc->g_src.s_addr;
   1058 		sa = sintosa(&si);
   1059 		ifr->ifr_addr = *sa;
   1060 		break;
   1061 	case GREGADDRD:
   1062 		memset(&si, 0, sizeof(si));
   1063 		si.sin_family = AF_INET;
   1064 		si.sin_len = sizeof(struct sockaddr_in);
   1065 		si.sin_addr.s_addr = sc->g_dst.s_addr;
   1066 		sa = sintosa(&si);
   1067 		ifr->ifr_addr = *sa;
   1068 		break;
   1069 	case GREDSOCK:
   1070 		if (sc->sc_proto != IPPROTO_UDP) {
   1071 			error = EINVAL;
   1072 			break;
   1073 		}
   1074 		ifp->if_flags &= ~IFF_UP;
   1075 		gre_wakeup(sc);
   1076 		break;
   1077 	case GRESSOCK:
   1078 		if (sc->sc_proto != IPPROTO_UDP) {
   1079 			error = EINVAL;
   1080 			break;
   1081 		}
   1082 		/* getsock() will FILE_USE() and unlock the descriptor for us */
   1083 		if ((error = getsock(p->p_fd, (int)ifr->ifr_value, &fp)) != 0)
   1084 			break;
   1085 		so = (struct socket *)fp->f_data;
   1086 		if (so->so_type != SOCK_DGRAM) {
   1087 			FILE_UNUSE(fp, NULL);
   1088 			error = EINVAL;
   1089 			break;
   1090 		}
   1091 		/* check address */
   1092 		if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0) {
   1093 			FILE_UNUSE(fp, NULL);
   1094 			break;
   1095 		}
   1096 
   1097                 /* Increase reference count.  Now that our reference
   1098                  * to the file descriptor is counted, this thread
   1099                  * can release our "use" of the descriptor, but it
   1100                  * will not be destroyed by some other thread's
   1101                  * action.  This thread needs to release its use,
   1102                  * too, because one and only one thread can have
   1103                  * use of the descriptor at once.  The kernel thread
   1104                  * will pick up the use if it needs it.
   1105 		 */
   1106 
   1107 		fp->f_count++;
   1108 		FILE_UNUSE(fp, NULL);
   1109 
   1110 		while (sc->sc_fp != NULL && error == 0) {
   1111 			error = cv_timedwait_sig(&sc->sc_soparm_cv, &sc->sc_mtx,
   1112 					         MAX(1, hz / 2));
   1113 		}
   1114 		if (error == 0) {
   1115 			sc->sc_fp = fp;
   1116 			ifp->if_flags |= IFF_UP;
   1117 		}
   1118 
   1119 		if (error != 0 || (error = gre_kick(sc)) != 0) {
   1120 			gre_closef(&fp, l);
   1121 			break;
   1122 		}
   1123 		/* fp does not any longer belong to this thread. */
   1124 		sc->g_src = src.sin_addr;
   1125 		sc->g_srcport = src.sin_port;
   1126 		sc->g_dst = dst.sin_addr;
   1127 		sc->g_dstport = dst.sin_port;
   1128 		break;
   1129 	case SIOCSLIFPHYADDR:
   1130 		if (lifr->addr.ss_family != AF_INET ||
   1131 		    lifr->dstaddr.ss_family != AF_INET) {
   1132 			error = EAFNOSUPPORT;
   1133 			break;
   1134 		}
   1135 		if (lifr->addr.ss_len != sizeof(si) ||
   1136 		    lifr->dstaddr.ss_len != sizeof(si)) {
   1137 			error = EINVAL;
   1138 			break;
   1139 		}
   1140 		sc->g_src = satosin(&lifr->addr)->sin_addr;
   1141 		sc->g_dst = satosin(&lifr->dstaddr)->sin_addr;
   1142 		sc->g_srcport = satosin(&lifr->addr)->sin_port;
   1143 		sc->g_dstport = satosin(&lifr->dstaddr)->sin_port;
   1144 		goto recompute;
   1145 	case SIOCDIFPHYADDR:
   1146 		sc->g_src.s_addr = INADDR_ANY;
   1147 		sc->g_dst.s_addr = INADDR_ANY;
   1148 		sc->g_srcport = 0;
   1149 		sc->g_dstport = 0;
   1150 		goto recompute;
   1151 	case SIOCGLIFPHYADDR:
   1152 		if (sc->g_src.s_addr == INADDR_ANY ||
   1153 		    sc->g_dst.s_addr == INADDR_ANY) {
   1154 			error = EADDRNOTAVAIL;
   1155 			break;
   1156 		}
   1157 		memset(&si, 0, sizeof(si));
   1158 		si.sin_family = AF_INET;
   1159 		si.sin_len = sizeof(struct sockaddr_in);
   1160 		si.sin_addr = sc->g_src;
   1161 		if (sc->sc_proto == IPPROTO_UDP)
   1162 			si.sin_port = sc->g_srcport;
   1163 		memcpy(&lifr->addr, &si, sizeof(si));
   1164 		si.sin_addr = sc->g_dst;
   1165 		if (sc->sc_proto == IPPROTO_UDP)
   1166 			si.sin_port = sc->g_dstport;
   1167 		memcpy(&lifr->dstaddr, &si, sizeof(si));
   1168 		break;
   1169 	default:
   1170 		error = EINVAL;
   1171 		break;
   1172 	}
   1173 #ifdef COMPAT_OIFREQ
   1174 	if (cmd != ocmd)
   1175 		ifreqn2o(oifr, ifr);
   1176 #endif
   1177 	mutex_exit(&sc->sc_mtx);
   1178 	return error;
   1179 }
   1180 
   1181 /*
   1182  * Compute a route to our destination.
   1183  */
   1184 static int
   1185 gre_compute_route(struct gre_softc *sc)
   1186 {
   1187 	struct route *ro;
   1188 	union {
   1189 		struct sockaddr		dst;
   1190 		struct sockaddr_in	dst4;
   1191 	} u;
   1192 
   1193 	ro = &sc->route;
   1194 
   1195 	memset(ro, 0, sizeof(*ro));
   1196 	sockaddr_in_init(&u.dst4, &sc->g_dst, 0);
   1197 	rtcache_setdst(ro, &u.dst);
   1198 
   1199 	rtcache_init(ro);
   1200 
   1201 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
   1202 		GRE_DPRINTF(sc, "%s: route to %s %s\n", sc->sc_if.if_xname,
   1203 		    inet_ntoa(u.dst4.sin_addr),
   1204 		    (ro->ro_rt == NULL)
   1205 		        ?  "does not exist"
   1206 			: "loops back to ourself");
   1207 		rtcache_free(ro);
   1208 		return EADDRNOTAVAIL;
   1209 	}
   1210 
   1211 	return 0;
   1212 }
   1213 
   1214 /*
   1215  * do a checksum of a buffer - much like in_cksum, which operates on
   1216  * mbufs.
   1217  */
   1218 u_int16_t
   1219 gre_in_cksum(u_int16_t *p, u_int len)
   1220 {
   1221 	u_int32_t sum = 0;
   1222 	int nwords = len >> 1;
   1223 
   1224 	while (nwords-- != 0)
   1225 		sum += *p++;
   1226 
   1227 	if (len & 1) {
   1228 		union {
   1229 			u_short w;
   1230 			u_char c[2];
   1231 		} u;
   1232 		u.c[0] = *(u_char *)p;
   1233 		u.c[1] = 0;
   1234 		sum += u.w;
   1235 	}
   1236 
   1237 	/* end-around-carry */
   1238 	sum = (sum >> 16) + (sum & 0xffff);
   1239 	sum += (sum >> 16);
   1240 	return ~sum;
   1241 }
   1242 #endif
   1243 
   1244 void	greattach(int);
   1245 
   1246 /* ARGSUSED */
   1247 void
   1248 greattach(int count)
   1249 {
   1250 #ifdef INET
   1251 	LIST_INIT(&gre_softc_list);
   1252 	if_clone_attach(&gre_cloner);
   1253 #endif
   1254 }
   1255