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