Home | History | Annotate | Line # | Download | only in net
if_gre.c revision 1.65
      1 /*	$NetBSD: if_gre.c,v 1.65 2006/09/07 02:40:33 dogcow 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.65 2006/09/07 02:40:33 dogcow 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->g_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 (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 	rc = soconnect(so, m, l);
    326 
    327 	if (rc != 0) {
    328 		GRE_DPRINTF(sc, "%s: soconnect failed\n", __func__);
    329 		goto out;
    330 	}
    331 
    332 	*mtod(m, int *) = ip_gre_ttl;
    333 	rc = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, IPPROTO_IP, IP_TTL,
    334 	    &m);
    335 	m = NULL;
    336 	if (rc != 0) {
    337 		printf("%s: setopt ttl failed\n", __func__);
    338 		rc = 0;
    339 	}
    340 out:
    341 	m_freem(m);
    342 
    343 	if (rc != 0)
    344 		gre_sodestroy(sop);
    345 	else
    346 		*sp = sc->sc_soparm;
    347 
    348 	return rc;
    349 }
    350 
    351 static void
    352 gre_thread1(struct gre_softc *sc, struct lwp *l)
    353 {
    354 	int flags, rc, s;
    355 	const struct gre_h *gh;
    356 	struct ifnet *ifp = &sc->sc_if;
    357 	struct mbuf *m;
    358 	struct socket *so = NULL;
    359 	struct uio uio;
    360 	struct gre_soparm sp;
    361 
    362 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    363 	s = splnet();
    364 
    365 	sc->sc_waitchan = 1;
    366 
    367 	memset(&sp, 0, sizeof(sp));
    368 	memset(&uio, 0, sizeof(uio));
    369 
    370 	ifp->if_flags |= IFF_RUNNING;
    371 
    372 	for (;;) {
    373 		while (sc->sc_waitchan == 0) {
    374 			splx(s);
    375 			GRE_DPRINTF(sc, "%s: sleeping\n", __func__);
    376 			tsleep(&sc->sc_waitchan, PSOCK, "grewait", 0);
    377 			s = splnet();
    378 		}
    379 		sc->sc_waitchan = 0;
    380 		GRE_DPRINTF(sc, "%s: awake\n", __func__);
    381 		if ((ifp->if_flags & IFF_UP) != IFF_UP) {
    382 			GRE_DPRINTF(sc, "%s: not up & running; exiting\n",
    383 			    __func__);
    384 			break;
    385 		}
    386 		if (sc->g_proto != IPPROTO_UDP) {
    387 			GRE_DPRINTF(sc, "%s: not udp; exiting\n", __func__);
    388 			break;
    389 		}
    390 		/* XXX optimize */
    391 		if (memcmp(&sp, &sc->sc_soparm, sizeof(sp)) != 0) {
    392 			GRE_DPRINTF(sc, "%s: parameters changed\n", __func__);
    393 
    394 			if (sp.sp_fp != NULL) {
    395 				FILE_UNUSE(sp.sp_fp, NULL);
    396 				sp.sp_fp = NULL;
    397 				so = NULL;
    398 			} else if (so != NULL)
    399 				gre_sodestroy(&so);
    400 
    401 			if (sc->sc_fp != NULL) {
    402 				so = (struct socket *)sc->sc_fp->f_data;
    403 				gre_upcall_add(so, (caddr_t)sc);
    404 				sp = sc->sc_soparm;
    405 				FILE_USE(sp.sp_fp);
    406 			} else if (gre_socreate1(sc, l, &sp, &so) != 0)
    407 				goto out;
    408 		}
    409 		for (;;) {
    410 			flags = MSG_DONTWAIT;
    411 			uio.uio_resid = 1000000;
    412 			rc = (*so->so_receive)(so, NULL, &uio, &m, NULL,
    413 			    &flags);
    414 			/* TBD Back off if ECONNREFUSED (indicates
    415 			 * ICMP Port Unreachable)?
    416 			 */
    417 			if (rc == EWOULDBLOCK) {
    418 				GRE_DPRINTF(sc, "%s: so_receive EWOULDBLOCK\n",
    419 				    __func__);
    420 				break;
    421 			} else if (rc != 0 || m == NULL) {
    422 				GRE_DPRINTF(sc, "%s: rc %d m %p\n",
    423 				    ifp->if_xname, rc, (void *)m);
    424 				continue;
    425 			} else
    426 				GRE_DPRINTF(sc, "%s: so_receive ok\n",
    427 				    __func__);
    428 			if (m->m_len < sizeof(*gh) &&
    429 			    (m = m_pullup(m, sizeof(*gh))) == NULL) {
    430 				GRE_DPRINTF(sc, "%s: m_pullup failed\n",
    431 				    __func__);
    432 				continue;
    433 			}
    434 			gh = mtod(m, const struct gre_h *);
    435 
    436 			if (gre_input3(sc, m, 0, IPPROTO_GRE, gh) == 0) {
    437 				GRE_DPRINTF(sc, "%s: dropping unsupported\n",
    438 				    __func__);
    439 				ifp->if_ierrors++;
    440 				m_freem(m);
    441 			}
    442 		}
    443 		for (;;) {
    444 			IF_DEQUEUE(&sc->sc_snd, m);
    445 			if (m == NULL)
    446 				break;
    447 			GRE_DPRINTF(sc, "%s: dequeue\n", __func__);
    448 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    449 				GRE_DPRINTF(sc, "%s: not connected\n",
    450 				    __func__);
    451 				m_freem(m);
    452 				continue;
    453 			}
    454 			rc = (*so->so_send)(so, NULL, NULL, m, NULL, 0, l);
    455 			/* XXX handle ENOBUFS? */
    456 			if (rc != 0)
    457 				GRE_DPRINTF(sc, "%s: so_send failed\n",
    458 				    __func__);
    459 		}
    460 		/* Give the software interrupt queues a chance to
    461 		 * run, or else when I send a ping from gre0 to gre1 on
    462 		 * the same host, gre0 will not wake for the reply.
    463 		 */
    464 		splx(s);
    465 		s = splnet();
    466 	}
    467 	if (sp.sp_fp != NULL) {
    468 		GRE_DPRINTF(sc, "%s: removing upcall\n", __func__);
    469 		gre_upcall_remove(so);
    470 		FILE_UNUSE(sp.sp_fp, NULL);
    471 		sp.sp_fp = NULL;
    472 	} else
    473 		gre_sodestroy(&so);
    474 out:
    475 	GRE_DPRINTF(sc, "%s: stopping\n", __func__);
    476 	if (sc->g_proto == IPPROTO_UDP)
    477 		ifp->if_flags &= ~IFF_RUNNING;
    478 	while (!IF_IS_EMPTY(&sc->sc_snd)) {
    479 		IF_DEQUEUE(&sc->sc_snd, m);
    480 		m_freem(m);
    481 	}
    482 	gre_stop(&sc->sc_thread);
    483 	/* must not touch sc after this! */
    484 	GRE_DPRINTF(sc, "%s: restore ipl\n", __func__);
    485 	splx(s);
    486 }
    487 
    488 static void
    489 gre_thread(void *arg)
    490 {
    491 	struct gre_softc *sc = (struct gre_softc *)arg;
    492 
    493 	gre_thread1(sc, curlwp);
    494 	/* must not touch sc after this! */
    495 	kthread_exit(0);
    496 }
    497 
    498 int
    499 gre_input3(struct gre_softc *sc, struct mbuf *m, int hlen, u_char proto,
    500     const struct gre_h *gh)
    501 {
    502 	u_int16_t flags;
    503 #if NBPFILTER > 0
    504 	u_int32_t af = AF_INET;		/* af passed to BPF tap */
    505 #endif
    506 	int s, isr;
    507 	struct ifqueue *ifq;
    508 
    509 	sc->sc_if.if_ipackets++;
    510 	sc->sc_if.if_ibytes += m->m_pkthdr.len;
    511 
    512 	switch (proto) {
    513 	case IPPROTO_GRE:
    514 		hlen += sizeof(struct gre_h);
    515 
    516 		/* process GRE flags as packet can be of variable len */
    517 		flags = ntohs(gh->flags);
    518 
    519 		/* Checksum & Offset are present */
    520 		if ((flags & GRE_CP) | (flags & GRE_RP))
    521 			hlen += 4;
    522 		/* We don't support routing fields (variable length) */
    523 		if (flags & GRE_RP)
    524 			return (0);
    525 		if (flags & GRE_KP)
    526 			hlen += 4;
    527 		if (flags & GRE_SP)
    528 			hlen += 4;
    529 
    530 		switch (ntohs(gh->ptype)) { /* ethertypes */
    531 		case ETHERTYPE_IP: /* shouldn't need a schednetisr(), as */
    532 			ifq = &ipintrq;          /* we are in ip_input */
    533 			isr = NETISR_IP;
    534 			break;
    535 #ifdef NETATALK
    536 		case ETHERTYPE_ATALK:
    537 			ifq = &atintrq1;
    538 			isr = NETISR_ATALK;
    539 #if NBPFILTER > 0
    540 			af = AF_APPLETALK;
    541 #endif
    542 			break;
    543 #endif
    544 #ifdef INET6
    545 		case ETHERTYPE_IPV6:
    546 			GRE_DPRINTF(sc, "%s: IPv6 packet\n", __func__);
    547 			ifq = &ip6intrq;
    548 			isr = NETISR_IPV6;
    549 #if NBPFILTER > 0
    550 			af = AF_INET6;
    551 #endif
    552 			break;
    553 #endif
    554 		default:	   /* others not yet supported */
    555 			printf("%s: unhandled ethertype 0x%04x\n", __func__,
    556 			    ntohs(gh->ptype));
    557 			return (0);
    558 		}
    559 		break;
    560 	default:
    561 		/* others not yet supported */
    562 		return (0);
    563 	}
    564 
    565 	if (hlen > m->m_pkthdr.len) {
    566 		m_freem(m);
    567 		sc->sc_if.if_ierrors++;
    568 		return (EINVAL);
    569 	}
    570 	m_adj(m, hlen);
    571 
    572 #if NBPFILTER > 0
    573 	if (sc->sc_if.if_bpf != NULL)
    574 		bpf_mtap_af(sc->sc_if.if_bpf, af, m);
    575 #endif /*NBPFILTER > 0*/
    576 
    577 	m->m_pkthdr.rcvif = &sc->sc_if;
    578 
    579 	s = splnet();		/* possible */
    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 	splx(s);
    589 
    590 	return (1);	/* packet is done, no further processing needed */
    591 }
    592 
    593 /*
    594  * The output routine. Takes a packet and encapsulates it in the protocol
    595  * given by sc->g_proto. See also RFC 1701 and RFC 2004
    596  */
    597 static int
    598 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
    599 	   struct rtentry *rt)
    600 {
    601 	int error = 0, hlen;
    602 	struct gre_softc *sc = ifp->if_softc;
    603 	struct greip *gi;
    604 	struct gre_h *gh;
    605 	struct ip *eip, *ip;
    606 	u_int8_t ip_tos = 0;
    607 	u_int16_t etype = 0;
    608 	struct mobile_h mob_h;
    609 
    610 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
    611 	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
    612 		m_freem(m);
    613 		error = ENETDOWN;
    614 		goto end;
    615 	}
    616 
    617 	gi = NULL;
    618 	ip = NULL;
    619 
    620 #if NBPFILTER >0
    621 	if (ifp->if_bpf)
    622 		bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
    623 #endif
    624 
    625 	m->m_flags &= ~(M_BCAST|M_MCAST);
    626 
    627 	switch (sc->g_proto) {
    628 	case IPPROTO_MOBILE:
    629 		if (dst->sa_family == AF_INET) {
    630 			int msiz;
    631 
    632 			if (M_UNWRITABLE(m, sizeof(*ip)) &&
    633 			    (m = m_pullup(m, sizeof(*ip))) == NULL) {
    634 				error = ENOBUFS;
    635 				goto end;
    636 			}
    637 			ip = mtod(m, struct ip *);
    638 
    639 			memset(&mob_h, 0, MOB_H_SIZ_L);
    640 			mob_h.proto = (ip->ip_p) << 8;
    641 			mob_h.odst = ip->ip_dst.s_addr;
    642 			ip->ip_dst.s_addr = sc->g_dst.s_addr;
    643 
    644 			/*
    645 			 * If the packet comes from our host, we only change
    646 			 * the destination address in the IP header.
    647 			 * Else we also need to save and change the source
    648 			 */
    649 			if (in_hosteq(ip->ip_src, sc->g_src)) {
    650 				msiz = MOB_H_SIZ_S;
    651 			} else {
    652 				mob_h.proto |= MOB_H_SBIT;
    653 				mob_h.osrc = ip->ip_src.s_addr;
    654 				ip->ip_src.s_addr = sc->g_src.s_addr;
    655 				msiz = MOB_H_SIZ_L;
    656 			}
    657 			HTONS(mob_h.proto);
    658 			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
    659 
    660 			M_PREPEND(m, msiz, M_DONTWAIT);
    661 			if (m == NULL) {
    662 				error = ENOBUFS;
    663 				goto end;
    664 			}
    665 			/* XXX Assuming that ip does not dangle after
    666 			 * M_PREPEND.  In practice, that's true, but
    667 			 * that's in M_PREPEND's contract.
    668 			 */
    669 			memmove(mtod(m, caddr_t), ip, sizeof(*ip));
    670 			ip = mtod(m, struct ip *);
    671 			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
    672 			ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
    673 		} else {  /* AF_INET */
    674 			IF_DROP(&ifp->if_snd);
    675 			m_freem(m);
    676 			error = EINVAL;
    677 			goto end;
    678 		}
    679 		break;
    680 	case IPPROTO_UDP:
    681 	case IPPROTO_GRE:
    682 		GRE_DPRINTF(sc, "%s: dst->sa_family=%d\n", __func__,
    683 		    dst->sa_family);
    684 		switch (dst->sa_family) {
    685 		case AF_INET:
    686 			ip = mtod(m, struct ip *);
    687 			ip_tos = ip->ip_tos;
    688 			etype = ETHERTYPE_IP;
    689 			break;
    690 #ifdef NETATALK
    691 		case AF_APPLETALK:
    692 			etype = ETHERTYPE_ATALK;
    693 			break;
    694 #endif
    695 #ifdef INET6
    696 		case AF_INET6:
    697 			etype = ETHERTYPE_IPV6;
    698 			break;
    699 #endif
    700 		default:
    701 			IF_DROP(&ifp->if_snd);
    702 			m_freem(m);
    703 			error = EAFNOSUPPORT;
    704 			goto end;
    705 		}
    706 		break;
    707 	default:
    708 		IF_DROP(&ifp->if_snd);
    709 		m_freem(m);
    710 		error = EINVAL;
    711 		goto end;
    712 	}
    713 
    714 	switch (sc->g_proto) {
    715 	case IPPROTO_GRE:
    716 		hlen = sizeof(struct greip);
    717 		break;
    718 	case IPPROTO_UDP:
    719 		hlen = sizeof(struct gre_h);
    720 		break;
    721 	default:
    722 		hlen = 0;
    723 		break;
    724 	}
    725 
    726 	M_PREPEND(m, hlen, M_DONTWAIT);
    727 
    728 	if (m == NULL) {
    729 		IF_DROP(&ifp->if_snd);
    730 		error = ENOBUFS;
    731 		goto end;
    732 	}
    733 
    734 	switch (sc->g_proto) {
    735 	case IPPROTO_UDP:
    736 		gh = mtod(m, struct gre_h *);
    737 		memset(gh, 0, sizeof(*gh));
    738 		gh->ptype = htons(etype);
    739 		/* XXX Need to handle IP ToS.  Look at how I handle IP TTL. */
    740 		break;
    741 	case IPPROTO_GRE:
    742 		gi = mtod(m, struct greip *);
    743 		gh = &gi->gi_g;
    744 		eip = &gi->gi_i;
    745 		/* we don't have any GRE flags for now */
    746 		memset(gh, 0, sizeof(*gh));
    747 		gh->ptype = htons(etype);
    748 		eip->ip_src = sc->g_src;
    749 		eip->ip_dst = sc->g_dst;
    750 		eip->ip_hl = (sizeof(struct ip)) >> 2;
    751 		eip->ip_ttl = ip_gre_ttl;
    752 		eip->ip_tos = ip_tos;
    753 		eip->ip_len = htons(m->m_pkthdr.len);
    754 		eip->ip_p = sc->g_proto;
    755 		break;
    756 	case IPPROTO_MOBILE:
    757 		eip = mtod(m, struct ip *);
    758 		eip->ip_p = sc->g_proto;
    759 		break;
    760 	default:
    761 		error = EPROTONOSUPPORT;
    762 		m_freem(m);
    763 		goto end;
    764 	}
    765 
    766 	ifp->if_opackets++;
    767 	ifp->if_obytes += m->m_pkthdr.len;
    768 
    769 	/* send it off */
    770 	if (sc->g_proto == IPPROTO_UDP) {
    771 		if (IF_QFULL(&sc->sc_snd)) {
    772 			IF_DROP(&sc->sc_snd);
    773 			error = ENOBUFS;
    774 			m_freem(m);
    775 		} else {
    776 			IF_ENQUEUE(&sc->sc_snd, m);
    777 			gre_wakeup(sc);
    778 			error = 0;
    779 		}
    780 	} else {
    781 		error = ip_output(m, NULL, &sc->route, 0,
    782 		    (struct ip_moptions *)NULL, (struct socket *)NULL);
    783 	}
    784   end:
    785 	if (error)
    786 		ifp->if_oerrors++;
    787 	return (error);
    788 }
    789 
    790 /* Must be called at IPL_NET. */
    791 static int
    792 gre_kick(struct gre_softc *sc)
    793 {
    794 	int rc;
    795 	struct ifnet *ifp = &sc->sc_if;
    796 
    797 	if (sc->g_proto == IPPROTO_UDP && (ifp->if_flags & IFF_UP) == IFF_UP &&
    798 	    !sc->sc_thread) {
    799 		sc->sc_thread = 1;
    800 		rc = kthread_create1(gre_thread, (void *)sc, NULL,
    801 		    ifp->if_xname);
    802 		if (rc != 0)
    803 			gre_stop(&sc->sc_thread);
    804 		return rc;
    805 	} else {
    806 		gre_wakeup(sc);
    807 		return 0;
    808 	}
    809 }
    810 
    811 static int
    812 gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
    813 {
    814 	int s, error;
    815 
    816 	s = splsoftnet();
    817 	error = (*so->so_proto->pr_usrreq)(so, req, (struct mbuf *)0,
    818 	    nam, (struct mbuf *)0, l);
    819 	splx(s);
    820 	return error;
    821 }
    822 
    823 static int
    824 gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
    825 {
    826 	return gre_getname(so, PRU_SOCKADDR, nam, l);
    827 }
    828 
    829 static int
    830 gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
    831 {
    832 	return gre_getname(so, PRU_PEERADDR, nam, l);
    833 }
    834 
    835 static int
    836 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_in *src,
    837     struct sockaddr_in *dst)
    838 {
    839 	struct mbuf *m;
    840 	struct sockaddr_in *sin;
    841 	int rc;
    842 
    843 	if ((m = gre_getsockmbuf(so)) == NULL)
    844 		return ENOBUFS;
    845 
    846 	sin = mtod(m, struct sockaddr_in *);
    847 
    848 	if ((rc = gre_getsockname(so, m, l)) != 0)
    849 		goto out;
    850 	if (sin->sin_family != AF_INET) {
    851 		rc = EAFNOSUPPORT;
    852 		goto out;
    853 	}
    854 	*src = *sin;
    855 
    856 	if ((rc = gre_getpeername(so, m, l)) != 0)
    857 		goto out;
    858 	if (sin->sin_family != AF_INET) {
    859 		rc = EAFNOSUPPORT;
    860 		goto out;
    861 	}
    862 	*dst = *sin;
    863 
    864 out:
    865 	m_freem(m);
    866 	return rc;
    867 }
    868 
    869 static int
    870 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    871 {
    872 	u_char oproto;
    873 	struct file *fp, *ofp;
    874 	struct socket *so;
    875 	struct sockaddr_in dst, src;
    876 	struct proc *p = curproc;	/* XXX */
    877 	struct lwp *l = curlwp;	/* XXX */
    878 	struct ifreq *ifr = (struct ifreq *)data;
    879 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
    880 	struct gre_softc *sc = ifp->if_softc;
    881 	int s;
    882 	struct sockaddr_in si;
    883 	struct sockaddr *sa = NULL;
    884 	int error;
    885 
    886 	switch (cmd) {
    887 	case SIOCSIFFLAGS:
    888 	case SIOCSIFMTU:
    889 	case GRESPROTO:
    890 	case GRESADDRD:
    891 	case GRESADDRS:
    892 	case GRESSOCK:
    893 	case GREDSOCK:
    894 	case SIOCSLIFPHYADDR:
    895 	case SIOCDIFPHYADDR:
    896 		if ((error = kauth_authorize_generic(l->l_cred,
    897 		    KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
    898 			return (error);
    899 		break;
    900 	default:
    901 		error = 0;
    902 		break;
    903 	}
    904 
    905 	s = splnet();
    906 	switch (cmd) {
    907 	case SIOCSIFADDR:
    908 		ifp->if_flags |= IFF_UP;
    909 		error = gre_kick(sc);
    910 		break;
    911 	case SIOCSIFDSTADDR:
    912 		break;
    913 	case SIOCSIFFLAGS:
    914 		oproto = sc->g_proto;
    915 		switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
    916 		case IFF_LINK0|IFF_LINK2:
    917 			sc->g_proto = IPPROTO_UDP;
    918 			if (oproto != IPPROTO_UDP)
    919 				ifp->if_flags &= ~IFF_RUNNING;
    920 			error = gre_kick(sc);
    921 			break;
    922 		case IFF_LINK0:
    923 			sc->g_proto = IPPROTO_GRE;
    924 			gre_wakeup(sc);
    925 			goto recompute;
    926 		case 0:
    927 			sc->g_proto = IPPROTO_MOBILE;
    928 			gre_wakeup(sc);
    929 			goto recompute;
    930 		}
    931 		break;
    932 	case SIOCSIFMTU:
    933 		if (ifr->ifr_mtu < 576) {
    934 			error = EINVAL;
    935 			break;
    936 		}
    937 		ifp->if_mtu = ifr->ifr_mtu;
    938 		break;
    939 	case SIOCGIFMTU:
    940 		ifr->ifr_mtu = sc->sc_if.if_mtu;
    941 		break;
    942 	case SIOCADDMULTI:
    943 	case SIOCDELMULTI:
    944 		if (ifr == 0) {
    945 			error = EAFNOSUPPORT;
    946 			break;
    947 		}
    948 		switch (ifr->ifr_addr.sa_family) {
    949 #ifdef INET
    950 		case AF_INET:
    951 			break;
    952 #endif
    953 #ifdef INET6
    954 		case AF_INET6:
    955 			break;
    956 #endif
    957 		default:
    958 			error = EAFNOSUPPORT;
    959 			break;
    960 		}
    961 		break;
    962 	case GRESPROTO:
    963 		oproto = sc->g_proto;
    964 		sc->g_proto = ifr->ifr_flags;
    965 		switch (sc->g_proto) {
    966 		case IPPROTO_UDP:
    967 			ifp->if_flags |= IFF_LINK0|IFF_LINK2;
    968 			if (oproto != IPPROTO_UDP)
    969 				ifp->if_flags &= ~IFF_RUNNING;
    970 			error = gre_kick(sc);
    971 			break;
    972 		case IPPROTO_GRE:
    973 			ifp->if_flags |= IFF_LINK0;
    974 			ifp->if_flags &= ~IFF_LINK2;
    975 			goto recompute;
    976 		case IPPROTO_MOBILE:
    977 			ifp->if_flags &= ~(IFF_LINK0|IFF_LINK2);
    978 			goto recompute;
    979 		default:
    980 			error = EPROTONOSUPPORT;
    981 			break;
    982 		}
    983 		break;
    984 	case GREGPROTO:
    985 		ifr->ifr_flags = sc->g_proto;
    986 		break;
    987 	case GRESADDRS:
    988 	case GRESADDRD:
    989 		/*
    990 		 * set tunnel endpoints, compute a less specific route
    991 		 * to the remote end and mark if as up
    992 		 */
    993 		sa = &ifr->ifr_addr;
    994 		if (cmd == GRESADDRS) {
    995 			sc->g_src = (satosin(sa))->sin_addr;
    996 			sc->g_srcport = satosin(sa)->sin_port;
    997 		}
    998 		if (cmd == GRESADDRD) {
    999 			if (sc->g_proto == IPPROTO_UDP &&
   1000 			    satosin(sa)->sin_port == 0) {
   1001 				error = EINVAL;
   1002 				break;
   1003 			}
   1004 			sc->g_dst = (satosin(sa))->sin_addr;
   1005 			sc->g_dstport = satosin(sa)->sin_port;
   1006 		}
   1007 	recompute:
   1008 		if (sc->g_proto == IPPROTO_UDP ||
   1009 		    (sc->g_src.s_addr != INADDR_ANY &&
   1010 		     sc->g_dst.s_addr != INADDR_ANY)) {
   1011 			if (sc->sc_fp != NULL) {
   1012 				closef(sc->sc_fp, l);
   1013 				sc->sc_fp = NULL;
   1014 			}
   1015 			if (sc->route.ro_rt != NULL) {
   1016 				RTFREE(sc->route.ro_rt);
   1017 				sc->route.ro_rt = NULL;
   1018 			}
   1019 			if (sc->g_proto == IPPROTO_UDP)
   1020 				error = gre_kick(sc);
   1021 			else if (gre_compute_route(sc) == 0)
   1022 				ifp->if_flags |= IFF_RUNNING;
   1023 			else
   1024 				ifp->if_flags &= ~IFF_RUNNING;
   1025 		}
   1026 		break;
   1027 	case GREGADDRS:
   1028 		memset(&si, 0, sizeof(si));
   1029 		si.sin_family = AF_INET;
   1030 		si.sin_len = sizeof(struct sockaddr_in);
   1031 		si.sin_addr.s_addr = sc->g_src.s_addr;
   1032 		sa = sintosa(&si);
   1033 		ifr->ifr_addr = *sa;
   1034 		break;
   1035 	case GREGADDRD:
   1036 		memset(&si, 0, sizeof(si));
   1037 		si.sin_family = AF_INET;
   1038 		si.sin_len = sizeof(struct sockaddr_in);
   1039 		si.sin_addr.s_addr = sc->g_dst.s_addr;
   1040 		sa = sintosa(&si);
   1041 		ifr->ifr_addr = *sa;
   1042 		break;
   1043 	case GREDSOCK:
   1044 		if (sc->g_proto != IPPROTO_UDP)
   1045 			return EINVAL;
   1046 		if (sc->sc_fp != NULL) {
   1047 			closef(sc->sc_fp, l);
   1048 			sc->sc_fp = NULL;
   1049 			error = gre_kick(sc);
   1050 		}
   1051 		break;
   1052 	case GRESSOCK:
   1053 		if (sc->g_proto != IPPROTO_UDP)
   1054 			return EINVAL;
   1055 		/* getsock() will FILE_USE() the descriptor for us */
   1056 		if ((error = getsock(p->p_fd, (int)ifr->ifr_value, &fp)) != 0)
   1057 			break;
   1058 		so = (struct socket *)fp->f_data;
   1059 		if (so->so_type != SOCK_DGRAM) {
   1060 			FILE_UNUSE(fp, NULL);
   1061 			error = EINVAL;
   1062 			break;
   1063 		}
   1064 		/* check address */
   1065 		if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0) {
   1066 			FILE_UNUSE(fp, NULL);
   1067 			break;
   1068 		}
   1069 
   1070 		fp->f_count++;
   1071 
   1072 		ofp = sc->sc_fp;
   1073 		sc->sc_fp = fp;
   1074 		if ((error = gre_kick(sc)) != 0) {
   1075 			closef(fp, l);
   1076 			sc->sc_fp = ofp;
   1077 			break;
   1078 		}
   1079 		sc->g_src = src.sin_addr;
   1080 		sc->g_srcport = src.sin_port;
   1081 		sc->g_dst = dst.sin_addr;
   1082 		sc->g_dstport = dst.sin_port;
   1083 		if (ofp != NULL)
   1084 			closef(ofp, l);
   1085 		break;
   1086 	case SIOCSLIFPHYADDR:
   1087 		if (lifr->addr.ss_family != AF_INET ||
   1088 		    lifr->dstaddr.ss_family != AF_INET) {
   1089 			error = EAFNOSUPPORT;
   1090 			break;
   1091 		}
   1092 		if (lifr->addr.ss_len != sizeof(si) ||
   1093 		    lifr->dstaddr.ss_len != sizeof(si)) {
   1094 			error = EINVAL;
   1095 			break;
   1096 		}
   1097 		sc->g_src = satosin(&lifr->addr)->sin_addr;
   1098 		sc->g_dst = satosin(&lifr->dstaddr)->sin_addr;
   1099 		sc->g_srcport = satosin(&lifr->addr)->sin_port;
   1100 		sc->g_dstport = satosin(&lifr->dstaddr)->sin_port;
   1101 		goto recompute;
   1102 	case SIOCDIFPHYADDR:
   1103 		sc->g_src.s_addr = INADDR_ANY;
   1104 		sc->g_dst.s_addr = INADDR_ANY;
   1105 		sc->g_srcport = 0;
   1106 		sc->g_dstport = 0;
   1107 		goto recompute;
   1108 	case SIOCGLIFPHYADDR:
   1109 		if (sc->g_src.s_addr == INADDR_ANY ||
   1110 		    sc->g_dst.s_addr == INADDR_ANY) {
   1111 			error = EADDRNOTAVAIL;
   1112 			break;
   1113 		}
   1114 		memset(&si, 0, sizeof(si));
   1115 		si.sin_family = AF_INET;
   1116 		si.sin_len = sizeof(struct sockaddr_in);
   1117 		si.sin_addr = sc->g_src;
   1118 		if (sc->g_proto == IPPROTO_UDP)
   1119 			si.sin_port = sc->g_srcport;
   1120 		memcpy(&lifr->addr, &si, sizeof(si));
   1121 		si.sin_addr = sc->g_dst;
   1122 		if (sc->g_proto == IPPROTO_UDP)
   1123 			si.sin_port = sc->g_dstport;
   1124 		memcpy(&lifr->dstaddr, &si, sizeof(si));
   1125 		break;
   1126 	default:
   1127 		error = EINVAL;
   1128 		break;
   1129 	}
   1130 	splx(s);
   1131 	return (error);
   1132 }
   1133 
   1134 /*
   1135  * computes a route to our destination that is not the one
   1136  * which would be taken by ip_output(), as this one will loop back to
   1137  * us. If the interface is p2p as  a--->b, then a routing entry exists
   1138  * If we now send a packet to b (e.g. ping b), this will come down here
   1139  * gets src=a, dst=b tacked on and would from ip_output() sent back to
   1140  * if_gre.
   1141  * Goal here is to compute a route to b that is less specific than
   1142  * a-->b. We know that this one exists as in normal operation we have
   1143  * at least a default route which matches.
   1144  */
   1145 static int
   1146 gre_compute_route(struct gre_softc *sc)
   1147 {
   1148 	struct route *ro;
   1149 	u_int32_t a, b, c;
   1150 
   1151 	ro = &sc->route;
   1152 
   1153 	memset(ro, 0, sizeof(struct route));
   1154 	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
   1155 	ro->ro_dst.sa_family = AF_INET;
   1156 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
   1157 
   1158 	/*
   1159 	 * toggle last bit, so our interface is not found, but a less
   1160 	 * specific route. I'd rather like to specify a shorter mask,
   1161 	 * but this is not possible. Should work though. XXX
   1162 	 * there is a simpler way ...
   1163 	 */
   1164 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
   1165 		a = ntohl(sc->g_dst.s_addr);
   1166 		b = a & 0x01;
   1167 		c = a & 0xfffffffe;
   1168 		b = b ^ 0x01;
   1169 		a = b | c;
   1170 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
   1171 		    = htonl(a);
   1172 	}
   1173 
   1174 #ifdef DIAGNOSTIC
   1175 	printf("%s: searching for a route to %s", sc->sc_if.if_xname,
   1176 	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
   1177 #endif
   1178 
   1179 	rtalloc(ro);
   1180 
   1181 	/*
   1182 	 * check if this returned a route at all and this route is no
   1183 	 * recursion to ourself
   1184 	 */
   1185 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
   1186 #ifdef DIAGNOSTIC
   1187 		if (ro->ro_rt == NULL)
   1188 			printf(" - no route found!\n");
   1189 		else
   1190 			printf(" - route loops back to ourself!\n");
   1191 #endif
   1192 		return EADDRNOTAVAIL;
   1193 	}
   1194 
   1195 	/*
   1196 	 * now change it back - else ip_output will just drop
   1197 	 * the route and search one to this interface ...
   1198 	 */
   1199 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
   1200 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
   1201 
   1202 #ifdef DIAGNOSTIC
   1203 	printf(", choosing %s with gateway %s\n", ro->ro_rt->rt_ifp->if_xname,
   1204 	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
   1205 #endif
   1206 
   1207 	return 0;
   1208 }
   1209 
   1210 /*
   1211  * do a checksum of a buffer - much like in_cksum, which operates on
   1212  * mbufs.
   1213  */
   1214 u_int16_t
   1215 gre_in_cksum(u_int16_t *p, u_int len)
   1216 {
   1217 	u_int32_t sum = 0;
   1218 	int nwords = len >> 1;
   1219 
   1220 	while (nwords-- != 0)
   1221 		sum += *p++;
   1222 
   1223 	if (len & 1) {
   1224 		union {
   1225 			u_short w;
   1226 			u_char c[2];
   1227 		} u;
   1228 		u.c[0] = *(u_char *)p;
   1229 		u.c[1] = 0;
   1230 		sum += u.w;
   1231 	}
   1232 
   1233 	/* end-around-carry */
   1234 	sum = (sum >> 16) + (sum & 0xffff);
   1235 	sum += (sum >> 16);
   1236 	return (~sum);
   1237 }
   1238 #endif
   1239 
   1240 void	greattach(int);
   1241 
   1242 /* ARGSUSED */
   1243 void
   1244 greattach(int count)
   1245 {
   1246 #ifdef INET
   1247 	LIST_INIT(&gre_softc_list);
   1248 	if_clone_attach(&gre_cloner);
   1249 #endif
   1250 }
   1251