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