Home | History | Annotate | Line # | Download | only in net
if_gre.c revision 1.76
      1 /*	$NetBSD: if_gre.c,v 1.76 2006/11/16 22:58:00 dyoung 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.76 2006/11/16 22:58:00 dyoung 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/kthread.h>
     76 
     77 #include <machine/cpu.h>
     78 
     79 #include <net/ethertypes.h>
     80 #include <net/if.h>
     81 #include <net/if_types.h>
     82 #include <net/netisr.h>
     83 #include <net/route.h>
     84 
     85 #ifdef INET
     86 #include <netinet/in.h>
     87 #include <netinet/in_systm.h>
     88 #include <netinet/in_var.h>
     89 #include <netinet/ip.h>
     90 #include <netinet/ip_var.h>
     91 #else
     92 #error "Huh? if_gre without inet?"
     93 #endif
     94 
     95 
     96 #ifdef NETATALK
     97 #include <netatalk/at.h>
     98 #include <netatalk/at_var.h>
     99 #include <netatalk/at_extern.h>
    100 #endif
    101 
    102 #if NBPFILTER > 0
    103 #include <sys/time.h>
    104 #include <net/bpf.h>
    105 #endif
    106 
    107 #include <net/if_gre.h>
    108 
    109 /*
    110  * It is not easy to calculate the right value for a GRE MTU.
    111  * We leave this task to the admin and use the same default that
    112  * other vendors use.
    113  */
    114 #define GREMTU 1476
    115 
    116 #ifdef GRE_DEBUG
    117 #define	GRE_DPRINTF(__sc, __fmt, ...)				\
    118 	do {							\
    119 		if (((__sc)->sc_if.if_flags & IFF_DEBUG) != 0)	\
    120 			printf(__fmt, __VA_ARGS__);		\
    121 	} while (/*CONSTCOND*/0)
    122 #else
    123 #define	GRE_DPRINTF(__sc, __fmt, ...)	do { } while (/*CONSTCOND*/0)
    124 #endif /* GRE_DEBUG */
    125 
    126 struct gre_softc_head gre_softc_list;
    127 int ip_gre_ttl = GRE_TTL;
    128 
    129 static int	gre_clone_create(struct if_clone *, int);
    130 static int	gre_clone_destroy(struct ifnet *);
    131 
    132 static struct if_clone gre_cloner =
    133     IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
    134 
    135 static int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
    136 			   struct rtentry *);
    137 static int	gre_ioctl(struct ifnet *, u_long, caddr_t);
    138 
    139 static int	gre_compute_route(struct gre_softc *sc);
    140 
    141 static int gre_getsockname(struct socket *, struct mbuf *, struct lwp *);
    142 static int gre_getpeername(struct socket *, struct mbuf *, struct lwp *);
    143 static int gre_getnames(struct socket *, struct lwp *, struct sockaddr_in *,
    144     struct sockaddr_in *);
    145 
    146 static void
    147 gre_stop(int *running)
    148 {
    149 	*running = 0;
    150 	wakeup(running);
    151 }
    152 
    153 static void
    154 gre_join(int *running)
    155 {
    156 	int s;
    157 
    158 	s = splnet();
    159 	while (*running != 0) {
    160 		splx(s);
    161 		tsleep(running, PSOCK, "grejoin", 0);
    162 		s = splnet();
    163 	}
    164 	splx(s);
    165 }
    166 
    167 static void
    168 gre_wakeup(struct gre_softc *sc)
    169 {
    170 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    171 	sc->sc_waitchan = 1;
    172 	wakeup(&sc->sc_waitchan);
    173 }
    174 
    175 static int
    176 gre_clone_create(struct if_clone *ifc, int unit)
    177 {
    178 	struct gre_softc *sc;
    179 
    180 	sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
    181 	memset(sc, 0, sizeof(struct gre_softc));
    182 
    183 	snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
    184 	    ifc->ifc_name, unit);
    185 	sc->sc_if.if_softc = sc;
    186 	sc->sc_if.if_type = IFT_TUNNEL;
    187 	sc->sc_if.if_addrlen = 0;
    188 	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
    189 	sc->sc_if.if_dlt = DLT_NULL;
    190 	sc->sc_if.if_mtu = GREMTU;
    191 	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
    192 	sc->sc_if.if_output = gre_output;
    193 	sc->sc_if.if_ioctl = gre_ioctl;
    194 	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
    195 	sc->g_dstport = sc->g_srcport = 0;
    196 	sc->sc_proto = IPPROTO_GRE;
    197 	sc->sc_snd.ifq_maxlen = 256;
    198 	sc->sc_if.if_flags |= IFF_LINK0;
    199 	if_attach(&sc->sc_if);
    200 	if_alloc_sadl(&sc->sc_if);
    201 #if NBPFILTER > 0
    202 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
    203 #endif
    204 	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
    205 	return (0);
    206 }
    207 
    208 static int
    209 gre_clone_destroy(struct ifnet *ifp)
    210 {
    211 	struct gre_softc *sc = ifp->if_softc;
    212 
    213 	LIST_REMOVE(sc, sc_list);
    214 #if NBPFILTER > 0
    215 	bpfdetach(ifp);
    216 #endif
    217 	if_detach(ifp);
    218 	gre_wakeup(sc);
    219 	gre_join(&sc->sc_thread);
    220 	if (sc->sc_fp != NULL) {
    221 		closef(sc->sc_fp, curlwp);
    222 		sc->sc_fp = NULL;
    223 	}
    224 	free(sc, M_DEVBUF);
    225 
    226 	return (0);
    227 }
    228 
    229 static void
    230 gre_receive(struct socket *so, caddr_t arg, int waitflag)
    231 {
    232 	struct gre_softc *sc = (struct gre_softc *)arg;
    233 
    234 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    235 
    236 	gre_wakeup(sc);
    237 }
    238 
    239 static void
    240 gre_upcall_add(struct socket *so, caddr_t arg)
    241 {
    242 	/* XXX What if the kernel already set an upcall? */
    243 	so->so_upcallarg = arg;
    244 	so->so_upcall = gre_receive;
    245 	so->so_rcv.sb_flags |= SB_UPCALL;
    246 }
    247 
    248 static void
    249 gre_upcall_remove(struct socket *so)
    250 {
    251 	/* XXX What if the kernel already set an upcall? */
    252 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    253 	so->so_upcallarg = NULL;
    254 	so->so_upcall = NULL;
    255 }
    256 
    257 static void
    258 gre_sodestroy(struct socket **sop)
    259 {
    260 	gre_upcall_remove(*sop);
    261 	soshutdown(*sop, SHUT_RDWR);
    262 	soclose(*sop);
    263 	*sop = NULL;
    264 }
    265 
    266 static struct mbuf *
    267 gre_getsockmbuf(struct socket *so)
    268 {
    269 	struct mbuf *m;
    270 
    271 	m = m_get(M_WAIT, MT_SONAME);
    272 	if (m != NULL)
    273 		MCLAIM(m, so->so_mowner);
    274 	return m;
    275 }
    276 
    277 static int
    278 gre_socreate1(struct gre_softc *sc, struct lwp *l, struct gre_soparm *sp,
    279     struct socket **sop)
    280 {
    281 	int rc;
    282 	struct mbuf *m;
    283 	struct sockaddr_in *sin;
    284 	struct socket *so;
    285 
    286 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    287 	rc = socreate(AF_INET, sop, SOCK_DGRAM, IPPROTO_UDP, l);
    288 	if (rc != 0) {
    289 		GRE_DPRINTF(sc, "%s: socreate failed\n", __func__);
    290 		return rc;
    291 	}
    292 
    293 	so = *sop;
    294 
    295 	gre_upcall_add(so, (caddr_t)sc);
    296 	if ((m = gre_getsockmbuf(so)) == NULL) {
    297 		rc = ENOBUFS;
    298 		goto out;
    299 	}
    300 	sin = mtod(m, struct sockaddr_in *);
    301 	sin->sin_len = m->m_len = sizeof(struct sockaddr_in);
    302 	sin->sin_family = AF_INET;
    303 	sin->sin_addr = sc->g_src;
    304 	sin->sin_port = sc->g_srcport;
    305 
    306 	GRE_DPRINTF(sc, "%s: bind 0x%08" PRIx32 " port %d\n", __func__,
    307 	    sin->sin_addr.s_addr, ntohs(sin->sin_port));
    308 	if ((rc = sobind(so, m, l)) != 0) {
    309 		GRE_DPRINTF(sc, "%s: sobind failed\n", __func__);
    310 		goto out;
    311 	}
    312 
    313 	if (sc->g_srcport == 0) {
    314 		if ((rc = gre_getsockname(so, m, l)) != 0) {
    315 			GRE_DPRINTF(sc, "%s: gre_getsockname failed\n",
    316 			    __func__);
    317 			goto out;
    318 		}
    319 		sc->g_srcport = sin->sin_port;
    320 	}
    321 
    322 	sin->sin_addr = sc->g_dst;
    323 	sin->sin_port = sc->g_dstport;
    324 
    325 	if ((rc = soconnect(so, m, l)) != 0) {
    326 		GRE_DPRINTF(sc, "%s: soconnect failed\n", __func__);
    327 		goto out;
    328 	}
    329 
    330 	*mtod(m, int *) = ip_gre_ttl;
    331 	m->m_len = sizeof(int);
    332 	rc = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, IPPROTO_IP, IP_TTL,
    333 	    &m);
    334 	m = NULL;
    335 	if (rc != 0) {
    336 		printf("%s: setopt ttl failed\n", __func__);
    337 		rc = 0;
    338 	}
    339 out:
    340 	m_freem(m);
    341 
    342 	if (rc != 0)
    343 		gre_sodestroy(sop);
    344 	else
    345 		*sp = sc->sc_soparm;
    346 
    347 	return rc;
    348 }
    349 
    350 static void
    351 gre_thread1(struct gre_softc *sc, struct lwp *l)
    352 {
    353 	int flags, rc, s;
    354 	const struct gre_h *gh;
    355 	struct ifnet *ifp = &sc->sc_if;
    356 	struct mbuf *m;
    357 	struct socket *so = NULL;
    358 	struct uio uio;
    359 	struct gre_soparm sp;
    360 
    361 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    362 	s = splnet();
    363 
    364 	sc->sc_waitchan = 1;
    365 
    366 	memset(&sp, 0, sizeof(sp));
    367 	memset(&uio, 0, sizeof(uio));
    368 
    369 	ifp->if_flags |= IFF_RUNNING;
    370 
    371 	for (;;) {
    372 		while (sc->sc_waitchan == 0) {
    373 			splx(s);
    374 			GRE_DPRINTF(sc, "%s: sleeping\n", __func__);
    375 			tsleep(&sc->sc_waitchan, PSOCK, "grewait", 0);
    376 			s = splnet();
    377 		}
    378 		sc->sc_waitchan = 0;
    379 		GRE_DPRINTF(sc, "%s: awake\n", __func__);
    380 		if ((ifp->if_flags & IFF_UP) != IFF_UP) {
    381 			GRE_DPRINTF(sc, "%s: not up & running; exiting\n",
    382 			    __func__);
    383 			break;
    384 		}
    385 		if (sc->sc_proto != IPPROTO_UDP) {
    386 			GRE_DPRINTF(sc, "%s: not udp; exiting\n", __func__);
    387 			break;
    388 		}
    389 		/* XXX optimize */
    390 		if (so == NULL || memcmp(&sp, &sc->sc_soparm, sizeof(sp)) != 0){
    391 			GRE_DPRINTF(sc, "%s: parameters changed\n", __func__);
    392 
    393 			if (sp.sp_fp != NULL) {
    394 				FILE_UNUSE(sp.sp_fp, NULL);
    395 				sp.sp_fp = NULL;
    396 				so = NULL;
    397 			} else if (so != NULL)
    398 				gre_sodestroy(&so);
    399 
    400 			if (sc->sc_fp != NULL) {
    401 				so = (struct socket *)sc->sc_fp->f_data;
    402 				gre_upcall_add(so, (caddr_t)sc);
    403 				sp = sc->sc_soparm;
    404 				FILE_USE(sp.sp_fp);
    405 			} else if (gre_socreate1(sc, l, &sp, &so) != 0)
    406 				goto out;
    407 		}
    408 		for (;;) {
    409 			flags = MSG_DONTWAIT;
    410 			uio.uio_resid = 1000000;
    411 			rc = (*so->so_receive)(so, NULL, &uio, &m, NULL,
    412 			    &flags);
    413 			/* TBD Back off if ECONNREFUSED (indicates
    414 			 * ICMP Port Unreachable)?
    415 			 */
    416 			if (rc == EWOULDBLOCK) {
    417 				GRE_DPRINTF(sc, "%s: so_receive EWOULDBLOCK\n",
    418 				    __func__);
    419 				break;
    420 			} else if (rc != 0 || m == NULL) {
    421 				GRE_DPRINTF(sc, "%s: rc %d m %p\n",
    422 				    ifp->if_xname, rc, (void *)m);
    423 				continue;
    424 			} else
    425 				GRE_DPRINTF(sc, "%s: so_receive ok\n",
    426 				    __func__);
    427 			if (m->m_len < sizeof(*gh) &&
    428 			    (m = m_pullup(m, sizeof(*gh))) == NULL) {
    429 				GRE_DPRINTF(sc, "%s: m_pullup failed\n",
    430 				    __func__);
    431 				continue;
    432 			}
    433 			gh = mtod(m, const struct gre_h *);
    434 
    435 			if (gre_input3(sc, m, 0, IPPROTO_GRE, gh) == 0) {
    436 				GRE_DPRINTF(sc, "%s: dropping unsupported\n",
    437 				    __func__);
    438 				ifp->if_ierrors++;
    439 				m_freem(m);
    440 			}
    441 		}
    442 		for (;;) {
    443 			IF_DEQUEUE(&sc->sc_snd, m);
    444 			if (m == NULL)
    445 				break;
    446 			GRE_DPRINTF(sc, "%s: dequeue\n", __func__);
    447 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    448 				GRE_DPRINTF(sc, "%s: not connected\n",
    449 				    __func__);
    450 				m_freem(m);
    451 				continue;
    452 			}
    453 			rc = (*so->so_send)(so, NULL, NULL, m, NULL, 0, l);
    454 			/* XXX handle ENOBUFS? */
    455 			if (rc != 0)
    456 				GRE_DPRINTF(sc, "%s: so_send failed\n",
    457 				    __func__);
    458 		}
    459 		/* Give the software interrupt queues a chance to
    460 		 * run, or else when I send a ping from gre0 to gre1 on
    461 		 * the same host, gre0 will not wake for the reply.
    462 		 */
    463 		splx(s);
    464 		s = splnet();
    465 	}
    466 	if (sp.sp_fp != NULL) {
    467 		GRE_DPRINTF(sc, "%s: removing upcall\n", __func__);
    468 		gre_upcall_remove(so);
    469 		FILE_UNUSE(sp.sp_fp, NULL);
    470 		sp.sp_fp = NULL;
    471 	} else if (so != NULL)
    472 		gre_sodestroy(&so);
    473 out:
    474 	GRE_DPRINTF(sc, "%s: stopping\n", __func__);
    475 	if (sc->sc_proto == IPPROTO_UDP)
    476 		ifp->if_flags &= ~IFF_RUNNING;
    477 	while (!IF_IS_EMPTY(&sc->sc_snd)) {
    478 		IF_DEQUEUE(&sc->sc_snd, m);
    479 		m_freem(m);
    480 	}
    481 	gre_stop(&sc->sc_thread);
    482 	/* must not touch sc after this! */
    483 	GRE_DPRINTF(sc, "%s: restore ipl\n", __func__);
    484 	splx(s);
    485 }
    486 
    487 static void
    488 gre_thread(void *arg)
    489 {
    490 	struct gre_softc *sc = (struct gre_softc *)arg;
    491 
    492 	gre_thread1(sc, curlwp);
    493 	/* must not touch sc after this! */
    494 	kthread_exit(0);
    495 }
    496 
    497 int
    498 gre_input3(struct gre_softc *sc, struct mbuf *m, int hlen, u_char proto,
    499     const struct gre_h *gh)
    500 {
    501 	u_int16_t flags;
    502 #if NBPFILTER > 0
    503 	u_int32_t af = AF_INET;		/* af passed to BPF tap */
    504 #endif
    505 	int s, isr;
    506 	struct ifqueue *ifq;
    507 
    508 	sc->sc_if.if_ipackets++;
    509 	sc->sc_if.if_ibytes += m->m_pkthdr.len;
    510 
    511 	switch (proto) {
    512 	case IPPROTO_GRE:
    513 		hlen += sizeof(struct gre_h);
    514 
    515 		/* process GRE flags as packet can be of variable len */
    516 		flags = ntohs(gh->flags);
    517 
    518 		/* Checksum & Offset are present */
    519 		if ((flags & GRE_CP) | (flags & GRE_RP))
    520 			hlen += 4;
    521 		/* We don't support routing fields (variable length) */
    522 		if (flags & GRE_RP)
    523 			return (0);
    524 		if (flags & GRE_KP)
    525 			hlen += 4;
    526 		if (flags & GRE_SP)
    527 			hlen += 4;
    528 
    529 		switch (ntohs(gh->ptype)) { /* ethertypes */
    530 		case ETHERTYPE_IP: /* shouldn't need a schednetisr(), as */
    531 			ifq = &ipintrq;          /* we are in ip_input */
    532 			isr = NETISR_IP;
    533 			break;
    534 #ifdef NETATALK
    535 		case ETHERTYPE_ATALK:
    536 			ifq = &atintrq1;
    537 			isr = NETISR_ATALK;
    538 #if NBPFILTER > 0
    539 			af = AF_APPLETALK;
    540 #endif
    541 			break;
    542 #endif
    543 #ifdef INET6
    544 		case ETHERTYPE_IPV6:
    545 			GRE_DPRINTF(sc, "%s: IPv6 packet\n", __func__);
    546 			ifq = &ip6intrq;
    547 			isr = NETISR_IPV6;
    548 #if NBPFILTER > 0
    549 			af = AF_INET6;
    550 #endif
    551 			break;
    552 #endif
    553 		default:	   /* others not yet supported */
    554 			printf("%s: unhandled ethertype 0x%04x\n", __func__,
    555 			    ntohs(gh->ptype));
    556 			return (0);
    557 		}
    558 		break;
    559 	default:
    560 		/* others not yet supported */
    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 	s = splnet();		/* possible */
    579 	if (IF_QFULL(ifq)) {
    580 		IF_DROP(ifq);
    581 		m_freem(m);
    582 	} else {
    583 		IF_ENQUEUE(ifq, m);
    584 	}
    585 	/* we need schednetisr since the address family may change */
    586 	schednetisr(isr);
    587 	splx(s);
    588 
    589 	return (1);	/* packet is done, no further processing needed */
    590 }
    591 
    592 /*
    593  * The output routine. Takes a packet and encapsulates it in the protocol
    594  * given by sc->sc_proto. See also RFC 1701 and RFC 2004
    595  */
    596 static int
    597 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
    598 	   struct rtentry *rt)
    599 {
    600 	int error = 0, hlen;
    601 	struct gre_softc *sc = ifp->if_softc;
    602 	struct greip *gi;
    603 	struct gre_h *gh;
    604 	struct ip *eip, *ip;
    605 	u_int8_t ip_tos = 0;
    606 	u_int16_t etype = 0;
    607 	struct mobile_h mob_h;
    608 
    609 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
    610 	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
    611 		m_freem(m);
    612 		error = ENETDOWN;
    613 		goto end;
    614 	}
    615 
    616 	gi = NULL;
    617 	ip = NULL;
    618 
    619 #if NBPFILTER >0
    620 	if (ifp->if_bpf)
    621 		bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
    622 #endif
    623 
    624 	m->m_flags &= ~(M_BCAST|M_MCAST);
    625 
    626 	switch (sc->sc_proto) {
    627 	case IPPROTO_MOBILE:
    628 		if (dst->sa_family == AF_INET) {
    629 			int msiz;
    630 
    631 			if (M_UNWRITABLE(m, sizeof(*ip)) &&
    632 			    (m = m_pullup(m, sizeof(*ip))) == NULL) {
    633 				error = ENOBUFS;
    634 				goto end;
    635 			}
    636 			ip = mtod(m, struct ip *);
    637 
    638 			memset(&mob_h, 0, MOB_H_SIZ_L);
    639 			mob_h.proto = (ip->ip_p) << 8;
    640 			mob_h.odst = ip->ip_dst.s_addr;
    641 			ip->ip_dst.s_addr = sc->g_dst.s_addr;
    642 
    643 			/*
    644 			 * If the packet comes from our host, we only change
    645 			 * the destination address in the IP header.
    646 			 * Else we also need to save and change the source
    647 			 */
    648 			if (in_hosteq(ip->ip_src, sc->g_src)) {
    649 				msiz = MOB_H_SIZ_S;
    650 			} else {
    651 				mob_h.proto |= MOB_H_SBIT;
    652 				mob_h.osrc = ip->ip_src.s_addr;
    653 				ip->ip_src.s_addr = sc->g_src.s_addr;
    654 				msiz = MOB_H_SIZ_L;
    655 			}
    656 			HTONS(mob_h.proto);
    657 			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
    658 
    659 			M_PREPEND(m, msiz, M_DONTWAIT);
    660 			if (m == NULL) {
    661 				error = ENOBUFS;
    662 				goto end;
    663 			}
    664 			/* XXX Assuming that ip does not dangle after
    665 			 * M_PREPEND.  In practice, that's true, but
    666 			 * that's in M_PREPEND's contract.
    667 			 */
    668 			memmove(mtod(m, caddr_t), ip, sizeof(*ip));
    669 			ip = mtod(m, struct ip *);
    670 			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
    671 			ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
    672 		} else {  /* AF_INET */
    673 			IF_DROP(&ifp->if_snd);
    674 			m_freem(m);
    675 			error = EINVAL;
    676 			goto end;
    677 		}
    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 	} else {
    780 		error = ip_output(m, NULL, &sc->route, 0,
    781 		    (struct ip_moptions *)NULL, (struct socket *)NULL);
    782 	}
    783   end:
    784 	if (error)
    785 		ifp->if_oerrors++;
    786 	return (error);
    787 }
    788 
    789 /* gre_kick must be synchronized with network interrupts in order
    790  * to synchronize access to gre_softc members, so call it with
    791  * interrupt priority level set to IPL_NET or greater.
    792  */
    793 static int
    794 gre_kick(struct gre_softc *sc)
    795 {
    796 	int rc;
    797 	struct ifnet *ifp = &sc->sc_if;
    798 
    799 	if (sc->sc_proto == IPPROTO_UDP && (ifp->if_flags & IFF_UP) == IFF_UP &&
    800 	    !sc->sc_thread) {
    801 		sc->sc_thread = 1;
    802 		rc = kthread_create1(gre_thread, (void *)sc, NULL,
    803 		    ifp->if_xname);
    804 		if (rc != 0)
    805 			gre_stop(&sc->sc_thread);
    806 		return rc;
    807 	} else {
    808 		gre_wakeup(sc);
    809 		return 0;
    810 	}
    811 }
    812 
    813 static int
    814 gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
    815 {
    816 	int s, error;
    817 
    818 	s = splsoftnet();
    819 	error = (*so->so_proto->pr_usrreq)(so, req, (struct mbuf *)0,
    820 	    nam, (struct mbuf *)0, l);
    821 	splx(s);
    822 	return error;
    823 }
    824 
    825 static int
    826 gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
    827 {
    828 	return gre_getname(so, PRU_SOCKADDR, nam, l);
    829 }
    830 
    831 static int
    832 gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
    833 {
    834 	return gre_getname(so, PRU_PEERADDR, nam, l);
    835 }
    836 
    837 static int
    838 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_in *src,
    839     struct sockaddr_in *dst)
    840 {
    841 	struct mbuf *m;
    842 	struct sockaddr_in *sin;
    843 	int rc;
    844 
    845 	if ((m = gre_getsockmbuf(so)) == NULL)
    846 		return ENOBUFS;
    847 
    848 	sin = mtod(m, struct sockaddr_in *);
    849 
    850 	if ((rc = gre_getsockname(so, m, l)) != 0)
    851 		goto out;
    852 	if (sin->sin_family != AF_INET) {
    853 		rc = EAFNOSUPPORT;
    854 		goto out;
    855 	}
    856 	*src = *sin;
    857 
    858 	if ((rc = gre_getpeername(so, m, l)) != 0)
    859 		goto out;
    860 	if (sin->sin_family != AF_INET) {
    861 		rc = EAFNOSUPPORT;
    862 		goto out;
    863 	}
    864 	*dst = *sin;
    865 
    866 out:
    867 	m_freem(m);
    868 	return rc;
    869 }
    870 
    871 static int
    872 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    873 {
    874 	u_char oproto;
    875 	struct file *fp, *ofp;
    876 	struct socket *so;
    877 	struct sockaddr_in dst, src;
    878 	struct proc *p = curproc;	/* XXX */
    879 	struct lwp *l = curlwp;	/* XXX */
    880 	struct ifreq *ifr = (struct ifreq *)data;
    881 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
    882 	struct gre_softc *sc = ifp->if_softc;
    883 	int s;
    884 	struct sockaddr_in si;
    885 	struct sockaddr *sa = NULL;
    886 	int error;
    887 
    888 	switch (cmd) {
    889 	case SIOCSIFFLAGS:
    890 	case SIOCSIFMTU:
    891 	case GRESPROTO:
    892 	case GRESADDRD:
    893 	case GRESADDRS:
    894 	case GRESSOCK:
    895 	case GREDSOCK:
    896 	case SIOCSLIFPHYADDR:
    897 	case SIOCDIFPHYADDR:
    898 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
    899 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    900 		    NULL) != 0)
    901 			return (EPERM);
    902 		break;
    903 	default:
    904 		error = 0;
    905 		break;
    906 	}
    907 
    908 	s = splnet();
    909 	switch (cmd) {
    910 	case SIOCSIFADDR:
    911 		ifp->if_flags |= IFF_UP;
    912 		error = gre_kick(sc);
    913 		break;
    914 	case SIOCSIFDSTADDR:
    915 		break;
    916 	case SIOCSIFFLAGS:
    917 		oproto = sc->sc_proto;
    918 		switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
    919 		case IFF_LINK0|IFF_LINK2:
    920 			sc->sc_proto = IPPROTO_UDP;
    921 			if (oproto != IPPROTO_UDP)
    922 				ifp->if_flags &= ~IFF_RUNNING;
    923 			error = gre_kick(sc);
    924 			break;
    925 		case IFF_LINK0:
    926 			sc->sc_proto = IPPROTO_GRE;
    927 			gre_wakeup(sc);
    928 			goto recompute;
    929 		case 0:
    930 			sc->sc_proto = IPPROTO_MOBILE;
    931 			gre_wakeup(sc);
    932 			goto recompute;
    933 		}
    934 		break;
    935 	case SIOCSIFMTU:
    936 		if (ifr->ifr_mtu < 576) {
    937 			error = EINVAL;
    938 			break;
    939 		}
    940 		ifp->if_mtu = ifr->ifr_mtu;
    941 		break;
    942 	case SIOCGIFMTU:
    943 		ifr->ifr_mtu = sc->sc_if.if_mtu;
    944 		break;
    945 	case SIOCADDMULTI:
    946 	case SIOCDELMULTI:
    947 		if (ifr == 0) {
    948 			error = EAFNOSUPPORT;
    949 			break;
    950 		}
    951 		switch (ifr->ifr_addr.sa_family) {
    952 #ifdef INET
    953 		case AF_INET:
    954 			break;
    955 #endif
    956 #ifdef INET6
    957 		case AF_INET6:
    958 			break;
    959 #endif
    960 		default:
    961 			error = EAFNOSUPPORT;
    962 			break;
    963 		}
    964 		break;
    965 	case GRESPROTO:
    966 		oproto = sc->sc_proto;
    967 		sc->sc_proto = ifr->ifr_flags;
    968 		switch (sc->sc_proto) {
    969 		case IPPROTO_UDP:
    970 			ifp->if_flags |= IFF_LINK0|IFF_LINK2;
    971 			if (oproto != IPPROTO_UDP)
    972 				ifp->if_flags &= ~IFF_RUNNING;
    973 			error = gre_kick(sc);
    974 			break;
    975 		case IPPROTO_GRE:
    976 			ifp->if_flags |= IFF_LINK0;
    977 			ifp->if_flags &= ~IFF_LINK2;
    978 			goto recompute;
    979 		case IPPROTO_MOBILE:
    980 			ifp->if_flags &= ~(IFF_LINK0|IFF_LINK2);
    981 			goto recompute;
    982 		default:
    983 			error = EPROTONOSUPPORT;
    984 			break;
    985 		}
    986 		break;
    987 	case GREGPROTO:
    988 		ifr->ifr_flags = sc->sc_proto;
    989 		break;
    990 	case GRESADDRS:
    991 	case GRESADDRD:
    992 		/*
    993 		 * set tunnel endpoints, compute a less specific route
    994 		 * to the remote end and mark if as up
    995 		 */
    996 		sa = &ifr->ifr_addr;
    997 		if (cmd == GRESADDRS) {
    998 			sc->g_src = (satosin(sa))->sin_addr;
    999 			sc->g_srcport = satosin(sa)->sin_port;
   1000 		}
   1001 		if (cmd == GRESADDRD) {
   1002 			if (sc->sc_proto == IPPROTO_UDP &&
   1003 			    satosin(sa)->sin_port == 0) {
   1004 				error = EINVAL;
   1005 				break;
   1006 			}
   1007 			sc->g_dst = (satosin(sa))->sin_addr;
   1008 			sc->g_dstport = satosin(sa)->sin_port;
   1009 		}
   1010 	recompute:
   1011 		if (sc->sc_proto == IPPROTO_UDP ||
   1012 		    (sc->g_src.s_addr != INADDR_ANY &&
   1013 		     sc->g_dst.s_addr != INADDR_ANY)) {
   1014 			if (sc->sc_fp != NULL) {
   1015 				closef(sc->sc_fp, l);
   1016 				sc->sc_fp = NULL;
   1017 			}
   1018 			if (sc->route.ro_rt != NULL) {
   1019 				RTFREE(sc->route.ro_rt);
   1020 				sc->route.ro_rt = NULL;
   1021 			}
   1022 			if (sc->sc_proto == IPPROTO_UDP)
   1023 				error = gre_kick(sc);
   1024 			else if (gre_compute_route(sc) == 0)
   1025 				ifp->if_flags |= IFF_RUNNING;
   1026 			else
   1027 				ifp->if_flags &= ~IFF_RUNNING;
   1028 		}
   1029 		break;
   1030 	case GREGADDRS:
   1031 		memset(&si, 0, sizeof(si));
   1032 		si.sin_family = AF_INET;
   1033 		si.sin_len = sizeof(struct sockaddr_in);
   1034 		si.sin_addr.s_addr = sc->g_src.s_addr;
   1035 		sa = sintosa(&si);
   1036 		ifr->ifr_addr = *sa;
   1037 		break;
   1038 	case GREGADDRD:
   1039 		memset(&si, 0, sizeof(si));
   1040 		si.sin_family = AF_INET;
   1041 		si.sin_len = sizeof(struct sockaddr_in);
   1042 		si.sin_addr.s_addr = sc->g_dst.s_addr;
   1043 		sa = sintosa(&si);
   1044 		ifr->ifr_addr = *sa;
   1045 		break;
   1046 	case GREDSOCK:
   1047 		if (sc->sc_proto != IPPROTO_UDP)
   1048 			return EINVAL;
   1049 		if (sc->sc_fp != NULL) {
   1050 			closef(sc->sc_fp, l);
   1051 			sc->sc_fp = NULL;
   1052 			error = gre_kick(sc);
   1053 		}
   1054 		break;
   1055 	case GRESSOCK:
   1056 		if (sc->sc_proto != IPPROTO_UDP)
   1057 			return EINVAL;
   1058 		/* getsock() will FILE_USE() the descriptor for us */
   1059 		if ((error = getsock(p->p_fd, (int)ifr->ifr_value, &fp)) != 0)
   1060 			break;
   1061 		so = (struct socket *)fp->f_data;
   1062 		if (so->so_type != SOCK_DGRAM) {
   1063 			FILE_UNUSE(fp, NULL);
   1064 			error = EINVAL;
   1065 			break;
   1066 		}
   1067 		/* check address */
   1068 		if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0) {
   1069 			FILE_UNUSE(fp, NULL);
   1070 			break;
   1071 		}
   1072 
   1073 		fp->f_count++;
   1074 
   1075 		ofp = sc->sc_fp;
   1076 		sc->sc_fp = fp;
   1077 		if ((error = gre_kick(sc)) != 0) {
   1078 			closef(fp, l);
   1079 			sc->sc_fp = ofp;
   1080 			break;
   1081 		}
   1082 		sc->g_src = src.sin_addr;
   1083 		sc->g_srcport = src.sin_port;
   1084 		sc->g_dst = dst.sin_addr;
   1085 		sc->g_dstport = dst.sin_port;
   1086 		if (ofp != NULL)
   1087 			closef(ofp, l);
   1088 		break;
   1089 	case SIOCSLIFPHYADDR:
   1090 		if (lifr->addr.ss_family != AF_INET ||
   1091 		    lifr->dstaddr.ss_family != AF_INET) {
   1092 			error = EAFNOSUPPORT;
   1093 			break;
   1094 		}
   1095 		if (lifr->addr.ss_len != sizeof(si) ||
   1096 		    lifr->dstaddr.ss_len != sizeof(si)) {
   1097 			error = EINVAL;
   1098 			break;
   1099 		}
   1100 		sc->g_src = satosin(&lifr->addr)->sin_addr;
   1101 		sc->g_dst = satosin(&lifr->dstaddr)->sin_addr;
   1102 		sc->g_srcport = satosin(&lifr->addr)->sin_port;
   1103 		sc->g_dstport = satosin(&lifr->dstaddr)->sin_port;
   1104 		goto recompute;
   1105 	case SIOCDIFPHYADDR:
   1106 		sc->g_src.s_addr = INADDR_ANY;
   1107 		sc->g_dst.s_addr = INADDR_ANY;
   1108 		sc->g_srcport = 0;
   1109 		sc->g_dstport = 0;
   1110 		goto recompute;
   1111 	case SIOCGLIFPHYADDR:
   1112 		if (sc->g_src.s_addr == INADDR_ANY ||
   1113 		    sc->g_dst.s_addr == INADDR_ANY) {
   1114 			error = EADDRNOTAVAIL;
   1115 			break;
   1116 		}
   1117 		memset(&si, 0, sizeof(si));
   1118 		si.sin_family = AF_INET;
   1119 		si.sin_len = sizeof(struct sockaddr_in);
   1120 		si.sin_addr = sc->g_src;
   1121 		if (sc->sc_proto == IPPROTO_UDP)
   1122 			si.sin_port = sc->g_srcport;
   1123 		memcpy(&lifr->addr, &si, sizeof(si));
   1124 		si.sin_addr = sc->g_dst;
   1125 		if (sc->sc_proto == IPPROTO_UDP)
   1126 			si.sin_port = sc->g_dstport;
   1127 		memcpy(&lifr->dstaddr, &si, sizeof(si));
   1128 		break;
   1129 	default:
   1130 		error = EINVAL;
   1131 		break;
   1132 	}
   1133 	splx(s);
   1134 	return (error);
   1135 }
   1136 
   1137 /*
   1138  * computes a route to our destination that is not the one
   1139  * which would be taken by ip_output(), as this one will loop back to
   1140  * us. If the interface is p2p as  a--->b, then a routing entry exists
   1141  * If we now send a packet to b (e.g. ping b), this will come down here
   1142  * gets src=a, dst=b tacked on and would from ip_output() sent back to
   1143  * if_gre.
   1144  * Goal here is to compute a route to b that is less specific than
   1145  * a-->b. We know that this one exists as in normal operation we have
   1146  * at least a default route which matches.
   1147  */
   1148 static int
   1149 gre_compute_route(struct gre_softc *sc)
   1150 {
   1151 	struct route *ro;
   1152 
   1153 	ro = &sc->route;
   1154 
   1155 	memset(ro, 0, sizeof(struct route));
   1156 	satosin(&ro->ro_dst)->sin_addr = sc->g_dst;
   1157 	ro->ro_dst.sa_family = AF_INET;
   1158 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
   1159 
   1160 	/*
   1161 	 * toggle last bit, so our interface is not found, but a less
   1162 	 * specific route. I'd rather like to specify a shorter mask,
   1163 	 * but this is not possible. Should work though. XXX
   1164 	 * there is a simpler way ...
   1165 	 */
   1166 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
   1167 		satosin(&ro->ro_dst)->sin_addr.s_addr
   1168 		    = sc->g_dst.s_addr ^ htonl(0x1);
   1169 	}
   1170 
   1171 #ifdef DIAGNOSTIC
   1172 	printf("%s: searching for a route to %s", sc->sc_if.if_xname,
   1173 	    inet_ntoa(satosin(&ro->ro_dst)->sin_addr));
   1174 #endif
   1175 
   1176 	rtalloc(ro);
   1177 
   1178 	/*
   1179 	 * check if this returned a route at all and this route is no
   1180 	 * recursion to ourself
   1181 	 */
   1182 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
   1183 #ifdef DIAGNOSTIC
   1184 		if (ro->ro_rt == NULL)
   1185 			printf(" - no route found!\n");
   1186 		else
   1187 			printf(" - route loops back to ourself!\n");
   1188 #endif
   1189 		return EADDRNOTAVAIL;
   1190 	}
   1191 
   1192 	/*
   1193 	 * now change it back - else ip_output will just drop
   1194 	 * the route and search one to this interface ...
   1195 	 */
   1196 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
   1197 		satosin(&ro->ro_dst)->sin_addr = sc->g_dst;
   1198 
   1199 #ifdef DIAGNOSTIC
   1200 	printf(", choosing %s with gateway %s\n", ro->ro_rt->rt_ifp->if_xname,
   1201 	    inet_ntoa(satosin(ro->ro_rt->rt_gateway)->sin_addr));
   1202 #endif
   1203 
   1204 	return 0;
   1205 }
   1206 
   1207 /*
   1208  * do a checksum of a buffer - much like in_cksum, which operates on
   1209  * mbufs.
   1210  */
   1211 u_int16_t
   1212 gre_in_cksum(u_int16_t *p, u_int len)
   1213 {
   1214 	u_int32_t sum = 0;
   1215 	int nwords = len >> 1;
   1216 
   1217 	while (nwords-- != 0)
   1218 		sum += *p++;
   1219 
   1220 	if (len & 1) {
   1221 		union {
   1222 			u_short w;
   1223 			u_char c[2];
   1224 		} u;
   1225 		u.c[0] = *(u_char *)p;
   1226 		u.c[1] = 0;
   1227 		sum += u.w;
   1228 	}
   1229 
   1230 	/* end-around-carry */
   1231 	sum = (sum >> 16) + (sum & 0xffff);
   1232 	sum += (sum >> 16);
   1233 	return (~sum);
   1234 }
   1235 #endif
   1236 
   1237 void	greattach(int);
   1238 
   1239 /* ARGSUSED */
   1240 void
   1241 greattach(int count)
   1242 {
   1243 #ifdef INET
   1244 	LIST_INIT(&gre_softc_list);
   1245 	if_clone_attach(&gre_cloner);
   1246 #endif
   1247 }
   1248