Home | History | Annotate | Line # | Download | only in net
if_gre.c revision 1.102.2.4
      1  1.102.2.4      matt /*	if_gre.c,v 1.102.2.3 2008/01/09 01:57:10 matt 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.1       hwr  */
     48       1.22     lukem 
     49       1.22     lukem #include <sys/cdefs.h>
     50  1.102.2.4      matt __KERNEL_RCSID(0, "if_gre.c,v 1.102.2.3 2008/01/09 01:57:10 matt Exp");
     51        1.1       hwr 
     52       1.62    dyoung #include "opt_gre.h"
     53        1.1       hwr #include "opt_inet.h"
     54        1.1       hwr #include "bpfilter.h"
     55        1.1       hwr 
     56       1.54  christos #ifdef INET
     57        1.1       hwr #include <sys/param.h>
     58       1.62    dyoung #include <sys/file.h>
     59       1.62    dyoung #include <sys/filedesc.h>
     60        1.1       hwr #include <sys/malloc.h>
     61  1.102.2.1      matt #include <sys/mallocvar.h>
     62        1.1       hwr #include <sys/mbuf.h>
     63       1.13    martin #include <sys/proc.h>
     64  1.102.2.1      matt #include <sys/domain.h>
     65        1.1       hwr #include <sys/protosw.h>
     66        1.1       hwr #include <sys/socket.h>
     67       1.62    dyoung #include <sys/socketvar.h>
     68        1.1       hwr #include <sys/ioctl.h>
     69       1.10   thorpej #include <sys/queue.h>
     70  1.102.2.2      matt #include <sys/intr.h>
     71        1.1       hwr #if __NetBSD__
     72        1.1       hwr #include <sys/systm.h>
     73       1.62    dyoung #include <sys/sysctl.h>
     74       1.60      elad #include <sys/kauth.h>
     75        1.1       hwr #endif
     76        1.1       hwr 
     77       1.93    dyoung #include <sys/kernel.h>
     78       1.93    dyoung #include <sys/mutex.h>
     79       1.93    dyoung #include <sys/condvar.h>
     80       1.62    dyoung #include <sys/kthread.h>
     81       1.62    dyoung 
     82  1.102.2.1      matt #include <sys/cpu.h>
     83        1.1       hwr 
     84        1.1       hwr #include <net/ethertypes.h>
     85        1.1       hwr #include <net/if.h>
     86        1.1       hwr #include <net/if_types.h>
     87        1.1       hwr #include <net/netisr.h>
     88        1.1       hwr #include <net/route.h>
     89        1.1       hwr 
     90        1.1       hwr #ifdef INET
     91        1.1       hwr #include <netinet/in.h>
     92        1.1       hwr #include <netinet/in_systm.h>
     93        1.1       hwr #include <netinet/in_var.h>
     94        1.1       hwr #include <netinet/ip.h>
     95        1.1       hwr #include <netinet/ip_var.h>
     96        1.1       hwr #else
     97        1.4   thorpej #error "Huh? if_gre without inet?"
     98        1.1       hwr #endif
     99        1.1       hwr 
    100        1.1       hwr 
    101        1.1       hwr #ifdef NETATALK
    102        1.1       hwr #include <netatalk/at.h>
    103        1.1       hwr #include <netatalk/at_var.h>
    104        1.1       hwr #include <netatalk/at_extern.h>
    105        1.1       hwr #endif
    106        1.1       hwr 
    107        1.1       hwr #if NBPFILTER > 0
    108        1.1       hwr #include <sys/time.h>
    109        1.1       hwr #include <net/bpf.h>
    110        1.1       hwr #endif
    111        1.1       hwr 
    112        1.1       hwr #include <net/if_gre.h>
    113        1.1       hwr 
    114      1.101       skd #include <compat/sys/socket.h>
    115       1.94  christos #include <compat/sys/sockio.h>
    116       1.20    itojun /*
    117       1.27    martin  * It is not easy to calculate the right value for a GRE MTU.
    118       1.27    martin  * We leave this task to the admin and use the same default that
    119       1.27    martin  * other vendors use.
    120       1.20    itojun  */
    121       1.27    martin #define GREMTU 1476
    122        1.1       hwr 
    123       1.62    dyoung #ifdef GRE_DEBUG
    124      1.102    dyoung int gre_debug = 0;
    125       1.62    dyoung #define	GRE_DPRINTF(__sc, __fmt, ...)				\
    126       1.62    dyoung 	do {							\
    127  1.102.2.1      matt 		if (__predict_false(gre_debug ||		\
    128  1.102.2.1      matt 		    ((__sc)->sc_if.if_flags & IFF_DEBUG) != 0))	\
    129       1.62    dyoung 			printf(__fmt, __VA_ARGS__);		\
    130       1.62    dyoung 	} while (/*CONSTCOND*/0)
    131       1.62    dyoung #else
    132       1.62    dyoung #define	GRE_DPRINTF(__sc, __fmt, ...)	do { } while (/*CONSTCOND*/0)
    133       1.62    dyoung #endif /* GRE_DEBUG */
    134       1.62    dyoung 
    135       1.26    martin int ip_gre_ttl = GRE_TTL;
    136  1.102.2.1      matt MALLOC_DEFINE(M_GRE_BUFQ, "gre_bufq", "gre mbuf queue");
    137        1.1       hwr 
    138  1.102.2.1      matt static int gre_clone_create(struct if_clone *, int);
    139  1.102.2.1      matt static int gre_clone_destroy(struct ifnet *);
    140       1.10   thorpej 
    141       1.59   thorpej static struct if_clone gre_cloner =
    142       1.10   thorpej     IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
    143        1.1       hwr 
    144  1.102.2.1      matt static int gre_input(struct gre_softc *, struct mbuf *, int,
    145  1.102.2.1      matt     const struct gre_h *);
    146  1.102.2.1      matt static bool gre_is_nullconf(const struct gre_soparm *);
    147  1.102.2.1      matt static int gre_output(struct ifnet *, struct mbuf *,
    148       1.84    dyoung 			   const struct sockaddr *, struct rtentry *);
    149  1.102.2.1      matt static int gre_ioctl(struct ifnet *, u_long, void *);
    150       1.93    dyoung static void gre_closef(struct file **, struct lwp *);
    151       1.62    dyoung static int gre_getsockname(struct socket *, struct mbuf *, struct lwp *);
    152       1.62    dyoung static int gre_getpeername(struct socket *, struct mbuf *, struct lwp *);
    153  1.102.2.1      matt static int gre_getnames(struct socket *, struct lwp *,
    154  1.102.2.1      matt     struct sockaddr_storage *, struct sockaddr_storage *);
    155  1.102.2.1      matt static void gre_clearconf(struct gre_soparm *, bool);
    156  1.102.2.1      matt static int gre_soreceive(struct socket *, struct mbuf **);
    157  1.102.2.1      matt static int gre_sosend(struct socket *, struct mbuf *, struct lwp *);
    158  1.102.2.1      matt static struct socket *gre_reconf(struct gre_softc *, struct socket *, lwp_t *,
    159  1.102.2.1      matt     const struct gre_soparm *);
    160  1.102.2.1      matt 
    161  1.102.2.1      matt static int
    162  1.102.2.1      matt nearest_pow2(size_t len0)
    163  1.102.2.1      matt {
    164  1.102.2.1      matt 	size_t len, mid;
    165  1.102.2.1      matt 
    166  1.102.2.1      matt 	if (len0 == 0)
    167  1.102.2.1      matt 		return 1;
    168  1.102.2.1      matt 
    169  1.102.2.1      matt 	for (len = len0; (len & (len - 1)) != 0; len &= len - 1)
    170  1.102.2.1      matt 		;
    171  1.102.2.1      matt 
    172  1.102.2.1      matt 	mid = len | (len >> 1);
    173  1.102.2.1      matt 
    174  1.102.2.1      matt 	/* avoid overflow */
    175  1.102.2.1      matt 	if ((len << 1) < len)
    176  1.102.2.1      matt 		return len;
    177  1.102.2.1      matt 	if (len0 >= mid)
    178  1.102.2.1      matt 		return len << 1;
    179  1.102.2.1      matt 	return len;
    180  1.102.2.1      matt }
    181  1.102.2.1      matt 
    182  1.102.2.1      matt static struct gre_bufq *
    183  1.102.2.1      matt gre_bufq_init(struct gre_bufq *bq, size_t len0)
    184  1.102.2.1      matt {
    185  1.102.2.1      matt 	size_t len;
    186  1.102.2.1      matt 
    187  1.102.2.1      matt 	len = nearest_pow2(len0);
    188  1.102.2.1      matt 
    189  1.102.2.1      matt 	memset(bq, 0, sizeof(*bq));
    190  1.102.2.1      matt 	bq->bq_buf = malloc(len * sizeof(struct mbuf *), M_GRE_BUFQ, M_WAITOK);
    191  1.102.2.1      matt 	bq->bq_len = len;
    192  1.102.2.1      matt 	bq->bq_lenmask = len - 1;
    193  1.102.2.1      matt 
    194  1.102.2.1      matt 	return bq;
    195  1.102.2.1      matt }
    196  1.102.2.1      matt 
    197  1.102.2.1      matt static bool
    198  1.102.2.1      matt gre_bufq_empty(struct gre_bufq *bq)
    199  1.102.2.1      matt {
    200  1.102.2.1      matt 	return bq->bq_prodidx == bq->bq_considx;
    201  1.102.2.1      matt }
    202  1.102.2.1      matt 
    203  1.102.2.1      matt static struct mbuf *
    204  1.102.2.1      matt gre_bufq_dequeue(struct gre_bufq *bq)
    205  1.102.2.1      matt {
    206  1.102.2.1      matt 	struct mbuf *m;
    207  1.102.2.1      matt 
    208  1.102.2.1      matt 	if (gre_bufq_empty(bq))
    209  1.102.2.1      matt 		return NULL;
    210  1.102.2.1      matt 
    211  1.102.2.1      matt 	m = bq->bq_buf[bq->bq_considx];
    212  1.102.2.1      matt 	bq->bq_considx = (bq->bq_considx + 1) & bq->bq_lenmask;
    213  1.102.2.1      matt 
    214  1.102.2.1      matt 	return m;
    215  1.102.2.1      matt }
    216       1.62    dyoung 
    217       1.62    dyoung static void
    218  1.102.2.1      matt gre_bufq_purge(struct gre_bufq *bq)
    219       1.62    dyoung {
    220  1.102.2.1      matt 	struct mbuf *m;
    221  1.102.2.1      matt 
    222  1.102.2.1      matt 	while ((m = gre_bufq_dequeue(bq)) != NULL)
    223  1.102.2.1      matt 		m_freem(m);
    224  1.102.2.1      matt }
    225  1.102.2.1      matt 
    226  1.102.2.1      matt static int
    227  1.102.2.1      matt gre_bufq_enqueue(struct gre_bufq *bq, struct mbuf *m)
    228  1.102.2.1      matt {
    229  1.102.2.1      matt 	int next;
    230  1.102.2.1      matt 
    231  1.102.2.1      matt 	next = (bq->bq_prodidx + 1) & bq->bq_lenmask;
    232  1.102.2.1      matt 
    233  1.102.2.1      matt 	if (next == bq->bq_considx) {
    234  1.102.2.1      matt 		bq->bq_drops++;
    235  1.102.2.1      matt 		return ENOBUFS;
    236  1.102.2.1      matt 	}
    237  1.102.2.1      matt 
    238  1.102.2.1      matt 	bq->bq_buf[bq->bq_prodidx] = m;
    239  1.102.2.1      matt 	bq->bq_prodidx = next;
    240  1.102.2.1      matt 	return 0;
    241       1.62    dyoung }
    242       1.62    dyoung 
    243       1.62    dyoung static void
    244  1.102.2.1      matt greintr(void *arg)
    245       1.62    dyoung {
    246  1.102.2.1      matt 	struct gre_softc *sc = (struct gre_softc *)arg;
    247  1.102.2.1      matt 	struct socket *so = sc->sc_so;
    248  1.102.2.1      matt 	lwp_t *l = curlwp;
    249  1.102.2.1      matt 	int rc;
    250  1.102.2.1      matt 	struct mbuf *m;
    251  1.102.2.1      matt 
    252  1.102.2.1      matt 	KASSERT(sc->sc_so != NULL);
    253  1.102.2.1      matt 
    254  1.102.2.1      matt 	sc->sc_send_ev.ev_count++;
    255       1.62    dyoung 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    256  1.102.2.1      matt 	while ((m = gre_bufq_dequeue(&sc->sc_snd)) != NULL) {
    257  1.102.2.1      matt 		/* XXX handle ENOBUFS? */
    258  1.102.2.1      matt 		if ((rc = gre_sosend(so, m, l)) != 0) {
    259  1.102.2.1      matt 			GRE_DPRINTF(sc, "%s: gre_sosend failed %d\n", __func__,
    260  1.102.2.1      matt 			    rc);
    261  1.102.2.1      matt 		}
    262  1.102.2.1      matt 	}
    263  1.102.2.1      matt }
    264  1.102.2.1      matt 
    265  1.102.2.1      matt /* Caller must hold sc->sc_mtx. */
    266  1.102.2.1      matt static void
    267  1.102.2.1      matt gre_wait(struct gre_softc *sc)
    268  1.102.2.1      matt {
    269  1.102.2.1      matt 	sc->sc_waiters++;
    270  1.102.2.1      matt 	cv_wait(&sc->sc_condvar, &sc->sc_mtx);
    271  1.102.2.1      matt 	sc->sc_waiters--;
    272  1.102.2.1      matt }
    273  1.102.2.1      matt 
    274  1.102.2.1      matt /* Caller must hold sc->sc_mtx. */
    275  1.102.2.1      matt static void
    276  1.102.2.1      matt gre_join(struct gre_softc *sc)
    277  1.102.2.1      matt {
    278  1.102.2.1      matt 	while (sc->sc_waiters > 0)
    279  1.102.2.1      matt 		cv_wait(&sc->sc_condvar, &sc->sc_mtx);
    280  1.102.2.1      matt }
    281  1.102.2.1      matt 
    282  1.102.2.1      matt static void
    283  1.102.2.1      matt gre_evcnt_detach(struct gre_softc *sc)
    284  1.102.2.1      matt {
    285  1.102.2.1      matt 	evcnt_detach(&sc->sc_unsupp_ev);
    286  1.102.2.1      matt 	evcnt_detach(&sc->sc_pullup_ev);
    287  1.102.2.1      matt 	evcnt_detach(&sc->sc_error_ev);
    288  1.102.2.1      matt 	evcnt_detach(&sc->sc_block_ev);
    289  1.102.2.1      matt 	evcnt_detach(&sc->sc_recv_ev);
    290  1.102.2.1      matt 
    291  1.102.2.1      matt 	evcnt_detach(&sc->sc_oflow_ev);
    292  1.102.2.1      matt 	evcnt_detach(&sc->sc_send_ev);
    293  1.102.2.1      matt }
    294  1.102.2.1      matt 
    295  1.102.2.1      matt static void
    296  1.102.2.1      matt gre_evcnt_attach(struct gre_softc *sc)
    297  1.102.2.1      matt {
    298  1.102.2.1      matt 	evcnt_attach_dynamic(&sc->sc_recv_ev, EVCNT_TYPE_MISC,
    299  1.102.2.1      matt 	    NULL, sc->sc_if.if_xname, "recv");
    300  1.102.2.1      matt 	evcnt_attach_dynamic(&sc->sc_block_ev, EVCNT_TYPE_MISC,
    301  1.102.2.1      matt 	    &sc->sc_recv_ev, sc->sc_if.if_xname, "would block");
    302  1.102.2.1      matt 	evcnt_attach_dynamic(&sc->sc_error_ev, EVCNT_TYPE_MISC,
    303  1.102.2.1      matt 	    &sc->sc_recv_ev, sc->sc_if.if_xname, "error");
    304  1.102.2.1      matt 	evcnt_attach_dynamic(&sc->sc_pullup_ev, EVCNT_TYPE_MISC,
    305  1.102.2.1      matt 	    &sc->sc_recv_ev, sc->sc_if.if_xname, "pullup failed");
    306  1.102.2.1      matt 	evcnt_attach_dynamic(&sc->sc_unsupp_ev, EVCNT_TYPE_MISC,
    307  1.102.2.1      matt 	    &sc->sc_recv_ev, sc->sc_if.if_xname, "unsupported");
    308  1.102.2.1      matt 
    309  1.102.2.1      matt 	evcnt_attach_dynamic(&sc->sc_send_ev, EVCNT_TYPE_MISC,
    310  1.102.2.1      matt 	    NULL, sc->sc_if.if_xname, "send");
    311  1.102.2.1      matt 	evcnt_attach_dynamic(&sc->sc_oflow_ev, EVCNT_TYPE_MISC,
    312  1.102.2.1      matt 	    &sc->sc_send_ev, sc->sc_if.if_xname, "overflow");
    313       1.62    dyoung }
    314       1.62    dyoung 
    315       1.59   thorpej static int
    316       1.59   thorpej gre_clone_create(struct if_clone *ifc, int unit)
    317        1.1       hwr {
    318  1.102.2.1      matt 	struct gre_soparm sp;
    319        1.8  explorer 	struct gre_softc *sc;
    320        1.1       hwr 
    321  1.102.2.1      matt 	sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK|M_ZERO);
    322  1.102.2.1      matt 	mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_SOFTNET);
    323  1.102.2.1      matt 	cv_init(&sc->sc_condvar, "gre wait");
    324       1.10   thorpej 
    325       1.50    itojun 	snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
    326       1.50    itojun 	    ifc->ifc_name, unit);
    327       1.10   thorpej 	sc->sc_if.if_softc = sc;
    328       1.51      tron 	sc->sc_if.if_type = IFT_TUNNEL;
    329       1.34    itojun 	sc->sc_if.if_addrlen = 0;
    330  1.102.2.1      matt 	sc->sc_if.if_hdrlen = sizeof(struct ip) + sizeof(struct gre_h);
    331       1.15   thorpej 	sc->sc_if.if_dlt = DLT_NULL;
    332       1.20    itojun 	sc->sc_if.if_mtu = GREMTU;
    333       1.10   thorpej 	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
    334       1.10   thorpej 	sc->sc_if.if_output = gre_output;
    335       1.10   thorpej 	sc->sc_if.if_ioctl = gre_ioctl;
    336  1.102.2.1      matt 	sockaddr_copy(sstosa(&sp.sp_dst), sizeof(sp.sp_dst), sintocsa(&in_any));
    337  1.102.2.1      matt 	sockaddr_copy(sstosa(&sp.sp_src), sizeof(sp.sp_src), sintocsa(&in_any));
    338  1.102.2.1      matt 	sp.sp_proto = IPPROTO_GRE;
    339  1.102.2.1      matt 	sp.sp_type = SOCK_RAW;
    340  1.102.2.1      matt 	sp.sp_bysock = 0;
    341  1.102.2.1      matt 	sp.sp_fd = -1;
    342  1.102.2.1      matt 	sc->sc_soparm = sp;
    343  1.102.2.1      matt 
    344  1.102.2.1      matt 	gre_evcnt_attach(sc);
    345  1.102.2.1      matt 
    346  1.102.2.1      matt 	gre_bufq_init(&sc->sc_snd, 17);
    347       1.35    itojun 	sc->sc_if.if_flags |= IFF_LINK0;
    348       1.10   thorpej 	if_attach(&sc->sc_if);
    349       1.16   thorpej 	if_alloc_sadl(&sc->sc_if);
    350        1.1       hwr #if NBPFILTER > 0
    351  1.102.2.4      matt 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(uint32_t));
    352        1.1       hwr #endif
    353  1.102.2.1      matt 	sc->sc_lwp = &lwp0;
    354  1.102.2.1      matt 	sc->sc_state = GRE_S_IDLE;
    355       1.87    dyoung 	return 0;
    356       1.10   thorpej }
    357        1.1       hwr 
    358       1.59   thorpej static int
    359       1.59   thorpej gre_clone_destroy(struct ifnet *ifp)
    360       1.10   thorpej {
    361  1.102.2.3      matt 	int s;
    362       1.10   thorpej 	struct gre_softc *sc = ifp->if_softc;
    363       1.10   thorpej 
    364  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
    365  1.102.2.1      matt 
    366       1.10   thorpej #if NBPFILTER > 0
    367       1.10   thorpej 	bpfdetach(ifp);
    368       1.10   thorpej #endif
    369  1.102.2.3      matt 	s = splnet();
    370       1.93    dyoung 	if_detach(ifp);
    371  1.102.2.1      matt 
    372  1.102.2.3      matt 	/* Some LWPs may still wait in gre_ioctl_lock(), however,
    373  1.102.2.3      matt 	 * no new LWP will enter gre_ioctl_lock(), because ifunit()
    374  1.102.2.3      matt 	 * cannot locate the interface any longer.
    375  1.102.2.3      matt 	 */
    376       1.93    dyoung 	mutex_enter(&sc->sc_mtx);
    377  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
    378  1.102.2.1      matt 	while (sc->sc_state != GRE_S_IDLE)
    379  1.102.2.1      matt 		gre_wait(sc);
    380  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
    381  1.102.2.1      matt 	sc->sc_state = GRE_S_DIE;
    382  1.102.2.1      matt 	cv_broadcast(&sc->sc_condvar);
    383       1.93    dyoung 	gre_join(sc);
    384  1.102.2.3      matt 	/* At this point, no other LWP will access the gre_softc, so
    385  1.102.2.3      matt 	 * we can release the mutex.
    386  1.102.2.3      matt 	 */
    387       1.93    dyoung 	mutex_exit(&sc->sc_mtx);
    388  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
    389  1.102.2.3      matt 	/* Note that we must not hold the mutex while we call gre_reconf(). */
    390  1.102.2.3      matt 	sc->sc_so = gre_reconf(sc, sc->sc_so, sc->sc_lwp, NULL);
    391  1.102.2.3      matt 	splx(s);
    392       1.93    dyoung 
    393  1.102.2.1      matt 	cv_destroy(&sc->sc_condvar);
    394       1.93    dyoung 	mutex_destroy(&sc->sc_mtx);
    395  1.102.2.1      matt 	gre_evcnt_detach(sc);
    396       1.10   thorpej 	free(sc, M_DEVBUF);
    397       1.53     peter 
    398       1.87    dyoung 	return 0;
    399        1.1       hwr }
    400        1.1       hwr 
    401       1.62    dyoung static void
    402       1.88  christos gre_receive(struct socket *so, void *arg, int waitflag)
    403       1.62    dyoung {
    404       1.62    dyoung 	struct gre_softc *sc = (struct gre_softc *)arg;
    405  1.102.2.1      matt 	int rc;
    406  1.102.2.1      matt 	const struct gre_h *gh;
    407  1.102.2.1      matt 	struct mbuf *m;
    408       1.62    dyoung 
    409       1.62    dyoung 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    410       1.62    dyoung 
    411  1.102.2.1      matt 	sc->sc_recv_ev.ev_count++;
    412  1.102.2.1      matt 
    413  1.102.2.1      matt 	rc = gre_soreceive(so, &m);
    414  1.102.2.1      matt 	/* TBD Back off if ECONNREFUSED (indicates
    415  1.102.2.1      matt 	 * ICMP Port Unreachable)?
    416  1.102.2.1      matt 	 */
    417  1.102.2.1      matt 	if (rc == EWOULDBLOCK) {
    418  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: EWOULDBLOCK\n", __func__);
    419  1.102.2.1      matt 		sc->sc_block_ev.ev_count++;
    420  1.102.2.1      matt 		return;
    421  1.102.2.1      matt 	} else if (rc != 0 || m == NULL) {
    422  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: rc %d m %p\n",
    423  1.102.2.1      matt 		    sc->sc_if.if_xname, rc, (void *)m);
    424  1.102.2.1      matt 		sc->sc_error_ev.ev_count++;
    425  1.102.2.1      matt 		return;
    426  1.102.2.1      matt 	}
    427  1.102.2.3      matt 	if (m->m_len < sizeof(*gh) && (m = m_pullup(m, sizeof(*gh))) == NULL) {
    428  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: m_pullup failed\n", __func__);
    429  1.102.2.1      matt 		sc->sc_pullup_ev.ev_count++;
    430  1.102.2.1      matt 		return;
    431  1.102.2.1      matt 	}
    432  1.102.2.1      matt 	gh = mtod(m, const struct gre_h *);
    433  1.102.2.1      matt 
    434  1.102.2.1      matt 	if (gre_input(sc, m, 0, gh) == 0) {
    435  1.102.2.1      matt 		sc->sc_unsupp_ev.ev_count++;
    436  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: dropping unsupported\n", __func__);
    437  1.102.2.1      matt 		m_freem(m);
    438  1.102.2.1      matt 	}
    439       1.62    dyoung }
    440       1.62    dyoung 
    441       1.62    dyoung static void
    442       1.88  christos gre_upcall_add(struct socket *so, void *arg)
    443       1.62    dyoung {
    444       1.62    dyoung 	/* XXX What if the kernel already set an upcall? */
    445      1.102    dyoung 	KASSERT((so->so_rcv.sb_flags & SB_UPCALL) == 0);
    446       1.62    dyoung 	so->so_upcallarg = arg;
    447       1.62    dyoung 	so->so_upcall = gre_receive;
    448       1.62    dyoung 	so->so_rcv.sb_flags |= SB_UPCALL;
    449       1.62    dyoung }
    450       1.62    dyoung 
    451       1.62    dyoung static void
    452       1.62    dyoung gre_upcall_remove(struct socket *so)
    453       1.62    dyoung {
    454       1.62    dyoung 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    455       1.62    dyoung 	so->so_upcallarg = NULL;
    456       1.62    dyoung 	so->so_upcall = NULL;
    457       1.62    dyoung }
    458       1.62    dyoung 
    459       1.62    dyoung static int
    460  1.102.2.1      matt gre_socreate(struct gre_softc *sc, struct lwp *l,
    461  1.102.2.1      matt     struct gre_soparm *sp, int *fdout)
    462       1.62    dyoung {
    463  1.102.2.1      matt 	const struct protosw *pr;
    464  1.102.2.1      matt 	int fd, rc;
    465       1.62    dyoung 	struct mbuf *m;
    466  1.102.2.1      matt 	struct sockaddr *sa;
    467  1.102.2.1      matt 	sa_family_t af;
    468       1.62    dyoung 	struct socket *so;
    469       1.62    dyoung 
    470       1.62    dyoung 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    471  1.102.2.1      matt 
    472  1.102.2.1      matt 	af = sp->sp_src.ss_family;
    473  1.102.2.1      matt 	rc = fsocreate(af, &so, sp->sp_type, sp->sp_proto, l, &fd);
    474       1.62    dyoung 	if (rc != 0) {
    475  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: fsocreate failed\n", __func__);
    476       1.62    dyoung 		return rc;
    477       1.62    dyoung 	}
    478       1.62    dyoung 
    479  1.102.2.3      matt 	if ((m = getsombuf(so, MT_SONAME)) == NULL) {
    480       1.62    dyoung 		rc = ENOBUFS;
    481       1.62    dyoung 		goto out;
    482       1.62    dyoung 	}
    483  1.102.2.1      matt 	sa = mtod(m, struct sockaddr *);
    484  1.102.2.1      matt 	sockaddr_copy(sa, MIN(MLEN, sizeof(sp->sp_src)), sstosa(&sp->sp_src));
    485  1.102.2.1      matt 	m->m_len = sp->sp_src.ss_len;
    486       1.62    dyoung 
    487  1.102.2.1      matt #if 0
    488  1.102.2.1      matt 	/* XXX */
    489       1.62    dyoung 	GRE_DPRINTF(sc, "%s: bind 0x%08" PRIx32 " port %d\n", __func__,
    490       1.62    dyoung 	    sin->sin_addr.s_addr, ntohs(sin->sin_port));
    491  1.102.2.1      matt #endif
    492       1.62    dyoung 	if ((rc = sobind(so, m, l)) != 0) {
    493       1.62    dyoung 		GRE_DPRINTF(sc, "%s: sobind failed\n", __func__);
    494       1.62    dyoung 		goto out;
    495       1.62    dyoung 	}
    496       1.62    dyoung 
    497  1.102.2.1      matt 	if ((rc = gre_getsockname(so, m, l)) != 0) {
    498  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: gre_getsockname\n", __func__);
    499  1.102.2.1      matt 		goto out;
    500       1.62    dyoung 	}
    501  1.102.2.1      matt 	sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src), sa);
    502       1.62    dyoung 
    503  1.102.2.1      matt 	sockaddr_copy(sa, MIN(MLEN, sizeof(sp->sp_dst)), sstosa(&sp->sp_dst));
    504  1.102.2.1      matt 	m->m_len = sp->sp_dst.ss_len;
    505       1.62    dyoung 
    506       1.68    dyoung 	if ((rc = soconnect(so, m, l)) != 0) {
    507       1.62    dyoung 		GRE_DPRINTF(sc, "%s: soconnect failed\n", __func__);
    508       1.62    dyoung 		goto out;
    509       1.62    dyoung 	}
    510       1.62    dyoung 
    511  1.102.2.1      matt 	/* XXX convert to a (new) SOL_SOCKET call */
    512       1.62    dyoung 	*mtod(m, int *) = ip_gre_ttl;
    513       1.76    dyoung 	m->m_len = sizeof(int);
    514  1.102.2.1      matt 	pr = so->so_proto;
    515  1.102.2.1      matt 	KASSERT(pr != NULL);
    516  1.102.2.1      matt 	rc = sosetopt(so, IPPROTO_IP, IP_TTL, m);
    517       1.62    dyoung 	m = NULL;
    518       1.62    dyoung 	if (rc != 0) {
    519  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: sosetopt ttl failed\n", __func__);
    520  1.102.2.1      matt 		rc = 0;
    521  1.102.2.1      matt 	}
    522  1.102.2.1      matt 	rc = sosetopt(so, SOL_SOCKET, SO_NOHEADER, m_intopt(so, 1));
    523  1.102.2.1      matt 	if (rc != 0) {
    524  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: sosetopt SO_NOHEADER failed\n", __func__);
    525       1.62    dyoung 		rc = 0;
    526       1.62    dyoung 	}
    527       1.62    dyoung out:
    528       1.62    dyoung 	m_freem(m);
    529       1.62    dyoung 
    530       1.62    dyoung 	if (rc != 0)
    531  1.102.2.1      matt 		fdrelease(l, fd);
    532  1.102.2.1      matt 	else
    533  1.102.2.1      matt 		*fdout = fd;
    534       1.62    dyoung 
    535       1.62    dyoung 	return rc;
    536       1.62    dyoung }
    537       1.62    dyoung 
    538  1.102.2.1      matt static int
    539  1.102.2.1      matt gre_sosend(struct socket *so, struct mbuf *top, struct lwp *l)
    540      1.102    dyoung {
    541  1.102.2.1      matt 	struct mbuf	**mp;
    542  1.102.2.1      matt 	struct proc	*p;
    543  1.102.2.1      matt 	long		space, resid;
    544  1.102.2.1      matt 	int		error, s;
    545      1.102    dyoung 
    546  1.102.2.1      matt 	p = l->l_proc;
    547  1.102.2.1      matt 
    548  1.102.2.1      matt 	resid = top->m_pkthdr.len;
    549  1.102.2.1      matt 	if (p)
    550  1.102.2.1      matt 		p->p_stats->p_ru.ru_msgsnd++;
    551  1.102.2.1      matt #define	snderr(errno)	{ error = errno; splx(s); goto release; }
    552  1.102.2.1      matt 
    553  1.102.2.1      matt 	if ((error = sblock(&so->so_snd, M_NOWAIT)) != 0)
    554  1.102.2.1      matt 		goto out;
    555  1.102.2.1      matt 	s = splsoftnet();
    556  1.102.2.1      matt 	if (so->so_state & SS_CANTSENDMORE)
    557  1.102.2.1      matt 		snderr(EPIPE);
    558  1.102.2.1      matt 	if (so->so_error) {
    559  1.102.2.1      matt 		error = so->so_error;
    560  1.102.2.1      matt 		so->so_error = 0;
    561  1.102.2.1      matt 		splx(s);
    562  1.102.2.1      matt 		goto release;
    563  1.102.2.1      matt 	}
    564  1.102.2.1      matt 	if ((so->so_state & SS_ISCONNECTED) == 0) {
    565  1.102.2.1      matt 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    566  1.102.2.1      matt 			if ((so->so_state & SS_ISCONFIRMING) == 0)
    567  1.102.2.1      matt 				snderr(ENOTCONN);
    568  1.102.2.1      matt 		} else
    569  1.102.2.1      matt 			snderr(EDESTADDRREQ);
    570      1.102    dyoung 	}
    571  1.102.2.1      matt 	space = sbspace(&so->so_snd);
    572  1.102.2.1      matt 	if (resid > so->so_snd.sb_hiwat)
    573  1.102.2.1      matt 		snderr(EMSGSIZE);
    574  1.102.2.1      matt 	if (space < resid)
    575  1.102.2.1      matt 		snderr(EWOULDBLOCK);
    576  1.102.2.1      matt 	splx(s);
    577  1.102.2.1      matt 	mp = &top;
    578  1.102.2.1      matt 	/*
    579  1.102.2.1      matt 	 * Data is prepackaged in "top".
    580  1.102.2.1      matt 	 */
    581  1.102.2.1      matt 	s = splsoftnet();
    582  1.102.2.1      matt 
    583  1.102.2.1      matt 	if (so->so_state & SS_CANTSENDMORE)
    584  1.102.2.1      matt 		snderr(EPIPE);
    585  1.102.2.1      matt 
    586  1.102.2.1      matt 	error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, top, NULL, NULL, l);
    587  1.102.2.1      matt 	splx(s);
    588  1.102.2.1      matt 
    589  1.102.2.1      matt 	top = NULL;
    590  1.102.2.1      matt 	mp = &top;
    591  1.102.2.1      matt 	if (error != 0)
    592  1.102.2.1      matt 		goto release;
    593  1.102.2.1      matt 
    594  1.102.2.1      matt  release:
    595  1.102.2.1      matt 	sbunlock(&so->so_snd);
    596  1.102.2.1      matt  out:
    597  1.102.2.1      matt 	if (top != NULL)
    598  1.102.2.1      matt 		m_freem(top);
    599  1.102.2.1      matt 	return error;
    600      1.102    dyoung }
    601      1.102    dyoung 
    602  1.102.2.1      matt /* This is a stripped-down version of soreceive() that will never
    603  1.102.2.1      matt  * block.  It will support SOCK_DGRAM sockets.  It may also support
    604  1.102.2.1      matt  * SOCK_SEQPACKET sockets.
    605  1.102.2.1      matt  */
    606  1.102.2.1      matt static int
    607  1.102.2.1      matt gre_soreceive(struct socket *so, struct mbuf **mp0)
    608      1.102    dyoung {
    609  1.102.2.1      matt 	struct lwp *l = curlwp;
    610  1.102.2.1      matt 	struct mbuf *m, **mp;
    611  1.102.2.1      matt 	int flags, len, error, s, type;
    612  1.102.2.1      matt 	const struct protosw	*pr;
    613  1.102.2.1      matt 	struct mbuf *nextrecord;
    614  1.102.2.1      matt 
    615  1.102.2.1      matt 	KASSERT(mp0 != NULL);
    616  1.102.2.1      matt 
    617  1.102.2.1      matt 	flags = MSG_DONTWAIT;
    618  1.102.2.1      matt 	pr = so->so_proto;
    619  1.102.2.1      matt 	mp = mp0;
    620  1.102.2.1      matt 	type = 0;
    621  1.102.2.1      matt 
    622  1.102.2.1      matt 	*mp = NULL;
    623  1.102.2.1      matt 
    624  1.102.2.1      matt 	KASSERT(pr->pr_flags & PR_ATOMIC);
    625  1.102.2.1      matt 
    626  1.102.2.1      matt 	if (so->so_state & SS_ISCONFIRMING)
    627  1.102.2.1      matt 		(*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l);
    628  1.102.2.1      matt 
    629  1.102.2.1      matt  restart:
    630  1.102.2.1      matt 	if ((error = sblock(&so->so_rcv, M_NOWAIT)) != 0)
    631  1.102.2.1      matt 		return error;
    632  1.102.2.1      matt 	s = splsoftnet();
    633  1.102.2.1      matt 
    634  1.102.2.1      matt 	m = so->so_rcv.sb_mb;
    635  1.102.2.1      matt 	/*
    636  1.102.2.1      matt 	 * If we have less data than requested, do not block awaiting more.
    637  1.102.2.1      matt 	 */
    638  1.102.2.1      matt 	if (m == NULL) {
    639  1.102.2.1      matt #ifdef DIAGNOSTIC
    640  1.102.2.1      matt 		if (so->so_rcv.sb_cc)
    641  1.102.2.1      matt 			panic("receive 1");
    642  1.102.2.1      matt #endif
    643  1.102.2.1      matt 		if (so->so_error) {
    644  1.102.2.1      matt 			error = so->so_error;
    645  1.102.2.1      matt 			so->so_error = 0;
    646  1.102.2.1      matt 		} else if (so->so_state & SS_CANTRCVMORE)
    647  1.102.2.1      matt 			;
    648  1.102.2.1      matt 		else if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0
    649  1.102.2.1      matt 		      && (so->so_proto->pr_flags & PR_CONNREQUIRED))
    650  1.102.2.1      matt 			error = ENOTCONN;
    651  1.102.2.1      matt 		else
    652  1.102.2.1      matt 			error = EWOULDBLOCK;
    653  1.102.2.1      matt 		goto release;
    654  1.102.2.1      matt 	}
    655  1.102.2.1      matt 	/*
    656  1.102.2.1      matt 	 * On entry here, m points to the first record of the socket buffer.
    657  1.102.2.1      matt 	 * While we process the initial mbufs containing address and control
    658  1.102.2.1      matt 	 * info, we save a copy of m->m_nextpkt into nextrecord.
    659  1.102.2.1      matt 	 */
    660  1.102.2.1      matt 	if (l != NULL)
    661  1.102.2.1      matt 		l->l_proc->p_stats->p_ru.ru_msgrcv++;
    662  1.102.2.1      matt 	KASSERT(m == so->so_rcv.sb_mb);
    663  1.102.2.1      matt 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
    664  1.102.2.1      matt 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
    665  1.102.2.1      matt 	nextrecord = m->m_nextpkt;
    666  1.102.2.1      matt 	if (pr->pr_flags & PR_ADDR) {
    667  1.102.2.1      matt #ifdef DIAGNOSTIC
    668  1.102.2.1      matt 		if (m->m_type != MT_SONAME)
    669  1.102.2.1      matt 			panic("receive 1a");
    670  1.102.2.1      matt #endif
    671  1.102.2.1      matt 		sbfree(&so->so_rcv, m);
    672  1.102.2.1      matt 		MFREE(m, so->so_rcv.sb_mb);
    673  1.102.2.1      matt 		m = so->so_rcv.sb_mb;
    674  1.102.2.1      matt 	}
    675  1.102.2.1      matt 	while (m != NULL && m->m_type == MT_CONTROL && error == 0) {
    676  1.102.2.1      matt 		sbfree(&so->so_rcv, m);
    677  1.102.2.1      matt 		/*
    678  1.102.2.1      matt 		 * Dispose of any SCM_RIGHTS message that went
    679  1.102.2.1      matt 		 * through the read path rather than recv.
    680  1.102.2.1      matt 		 */
    681  1.102.2.1      matt 		if (pr->pr_domain->dom_dispose &&
    682  1.102.2.1      matt 		    mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
    683  1.102.2.1      matt 			(*pr->pr_domain->dom_dispose)(m);
    684  1.102.2.1      matt 		MFREE(m, so->so_rcv.sb_mb);
    685  1.102.2.1      matt 		m = so->so_rcv.sb_mb;
    686  1.102.2.1      matt 	}
    687  1.102.2.1      matt 
    688  1.102.2.1      matt 	/*
    689  1.102.2.1      matt 	 * If m is non-NULL, we have some data to read.  From now on,
    690  1.102.2.1      matt 	 * make sure to keep sb_lastrecord consistent when working on
    691  1.102.2.1      matt 	 * the last packet on the chain (nextrecord == NULL) and we
    692  1.102.2.1      matt 	 * change m->m_nextpkt.
    693  1.102.2.1      matt 	 */
    694  1.102.2.1      matt 	if (m != NULL) {
    695  1.102.2.1      matt 		m->m_nextpkt = nextrecord;
    696  1.102.2.1      matt 		/*
    697  1.102.2.1      matt 		 * If nextrecord == NULL (this is a single chain),
    698  1.102.2.1      matt 		 * then sb_lastrecord may not be valid here if m
    699  1.102.2.1      matt 		 * was changed earlier.
    700  1.102.2.1      matt 		 */
    701  1.102.2.1      matt 		if (nextrecord == NULL) {
    702  1.102.2.1      matt 			KASSERT(so->so_rcv.sb_mb == m);
    703  1.102.2.1      matt 			so->so_rcv.sb_lastrecord = m;
    704  1.102.2.1      matt 		}
    705  1.102.2.1      matt 		type = m->m_type;
    706  1.102.2.1      matt 		if (type == MT_OOBDATA)
    707  1.102.2.1      matt 			flags |= MSG_OOB;
    708  1.102.2.1      matt 	} else {
    709  1.102.2.1      matt 		KASSERT(so->so_rcv.sb_mb == m);
    710  1.102.2.1      matt 		so->so_rcv.sb_mb = nextrecord;
    711  1.102.2.1      matt 		SB_EMPTY_FIXUP(&so->so_rcv);
    712  1.102.2.1      matt 	}
    713  1.102.2.1      matt 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
    714  1.102.2.1      matt 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
    715  1.102.2.1      matt 
    716  1.102.2.1      matt 	while (m != NULL) {
    717  1.102.2.1      matt 		if (m->m_type == MT_OOBDATA) {
    718  1.102.2.1      matt 			if (type != MT_OOBDATA)
    719  1.102.2.1      matt 				break;
    720  1.102.2.1      matt 		} else if (type == MT_OOBDATA)
    721  1.102.2.1      matt 			break;
    722  1.102.2.1      matt #ifdef DIAGNOSTIC
    723  1.102.2.1      matt 		else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
    724  1.102.2.1      matt 			panic("receive 3");
    725  1.102.2.1      matt #endif
    726  1.102.2.1      matt 		so->so_state &= ~SS_RCVATMARK;
    727  1.102.2.1      matt 		if (so->so_oobmark != 0 && so->so_oobmark < m->m_len)
    728  1.102.2.1      matt 			break;
    729  1.102.2.1      matt 		len = m->m_len;
    730  1.102.2.1      matt 		/*
    731  1.102.2.1      matt 		 * mp is set, just pass back the mbufs.
    732  1.102.2.1      matt 		 * Sockbuf must be consistent here (points to current mbuf,
    733  1.102.2.1      matt 		 * it points to next record) when we drop priority;
    734  1.102.2.1      matt 		 * we must note any additions to the sockbuf when we
    735  1.102.2.1      matt 		 * block interrupts again.
    736  1.102.2.1      matt 		 */
    737  1.102.2.1      matt 		if (m->m_flags & M_EOR)
    738  1.102.2.1      matt 			flags |= MSG_EOR;
    739  1.102.2.1      matt 		nextrecord = m->m_nextpkt;
    740  1.102.2.1      matt 		sbfree(&so->so_rcv, m);
    741  1.102.2.1      matt 		*mp = m;
    742  1.102.2.1      matt 		mp = &m->m_next;
    743  1.102.2.1      matt 		so->so_rcv.sb_mb = m = m->m_next;
    744  1.102.2.1      matt 		*mp = NULL;
    745  1.102.2.1      matt 		/*
    746  1.102.2.1      matt 		 * If m != NULL, we also know that
    747  1.102.2.1      matt 		 * so->so_rcv.sb_mb != NULL.
    748  1.102.2.1      matt 		 */
    749  1.102.2.1      matt 		KASSERT(so->so_rcv.sb_mb == m);
    750  1.102.2.1      matt 		if (m) {
    751  1.102.2.1      matt 			m->m_nextpkt = nextrecord;
    752  1.102.2.1      matt 			if (nextrecord == NULL)
    753  1.102.2.1      matt 				so->so_rcv.sb_lastrecord = m;
    754  1.102.2.1      matt 		} else {
    755  1.102.2.1      matt 			so->so_rcv.sb_mb = nextrecord;
    756  1.102.2.1      matt 			SB_EMPTY_FIXUP(&so->so_rcv);
    757      1.102    dyoung 		}
    758  1.102.2.1      matt 		SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
    759  1.102.2.1      matt 		SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
    760  1.102.2.1      matt 		if (so->so_oobmark) {
    761  1.102.2.1      matt 			so->so_oobmark -= len;
    762  1.102.2.1      matt 			if (so->so_oobmark == 0) {
    763  1.102.2.1      matt 				so->so_state |= SS_RCVATMARK;
    764  1.102.2.1      matt 				break;
    765  1.102.2.1      matt 			}
    766  1.102.2.1      matt 		}
    767  1.102.2.1      matt 		if (flags & MSG_EOR)
    768  1.102.2.1      matt 			break;
    769      1.102    dyoung 	}
    770  1.102.2.1      matt 
    771  1.102.2.1      matt 	if (m != NULL) {
    772  1.102.2.1      matt 		m_freem(*mp);
    773  1.102.2.1      matt 		*mp = NULL;
    774  1.102.2.1      matt 		error = ENOMEM;
    775  1.102.2.1      matt 		(void) sbdroprecord(&so->so_rcv);
    776  1.102.2.1      matt 	} else {
    777  1.102.2.1      matt 		/*
    778  1.102.2.1      matt 		 * First part is an inline SB_EMPTY_FIXUP().  Second
    779  1.102.2.1      matt 		 * part makes sure sb_lastrecord is up-to-date if
    780  1.102.2.1      matt 		 * there is still data in the socket buffer.
    781  1.102.2.1      matt 		 */
    782  1.102.2.1      matt 		so->so_rcv.sb_mb = nextrecord;
    783  1.102.2.1      matt 		if (so->so_rcv.sb_mb == NULL) {
    784  1.102.2.1      matt 			so->so_rcv.sb_mbtail = NULL;
    785  1.102.2.1      matt 			so->so_rcv.sb_lastrecord = NULL;
    786  1.102.2.1      matt 		} else if (nextrecord->m_nextpkt == NULL)
    787  1.102.2.1      matt 			so->so_rcv.sb_lastrecord = nextrecord;
    788  1.102.2.1      matt 	}
    789  1.102.2.1      matt 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
    790  1.102.2.1      matt 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
    791  1.102.2.1      matt 	if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
    792  1.102.2.1      matt 		(*pr->pr_usrreq)(so, PRU_RCVD, NULL,
    793  1.102.2.1      matt 		    (struct mbuf *)(long)flags, NULL, l);
    794  1.102.2.1      matt 	if (*mp0 == NULL && (flags & MSG_EOR) == 0 &&
    795  1.102.2.1      matt 	    (so->so_state & SS_CANTRCVMORE) == 0) {
    796  1.102.2.1      matt 		sbunlock(&so->so_rcv);
    797  1.102.2.1      matt 		splx(s);
    798  1.102.2.1      matt 		goto restart;
    799  1.102.2.1      matt 	}
    800  1.102.2.1      matt 
    801  1.102.2.1      matt  release:
    802  1.102.2.1      matt 	sbunlock(&so->so_rcv);
    803  1.102.2.1      matt 	splx(s);
    804  1.102.2.1      matt 	return error;
    805      1.102    dyoung }
    806      1.102    dyoung 
    807      1.102    dyoung static struct socket *
    808  1.102.2.1      matt gre_reconf(struct gre_softc *sc, struct socket *so, lwp_t *l,
    809  1.102.2.1      matt     const struct gre_soparm *newsoparm)
    810      1.102    dyoung {
    811  1.102.2.1      matt 	int rc;
    812  1.102.2.1      matt 	struct file *fp;
    813      1.102    dyoung 	struct ifnet *ifp = &sc->sc_if;
    814      1.102    dyoung 
    815      1.102    dyoung 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    816      1.102    dyoung 
    817      1.102    dyoung shutdown:
    818  1.102.2.1      matt 	if (sc->sc_soparm.sp_fd != -1) {
    819  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
    820      1.102    dyoung 		gre_upcall_remove(so);
    821  1.102.2.2      matt 		softint_disestablish(sc->sc_si);
    822  1.102.2.1      matt 		sc->sc_si = NULL;
    823  1.102.2.1      matt 		fdrelease(l, sc->sc_soparm.sp_fd);
    824  1.102.2.1      matt 		gre_clearconf(&sc->sc_soparm, false);
    825  1.102.2.1      matt 		sc->sc_soparm.sp_fd = -1;
    826      1.102    dyoung 		so = NULL;
    827  1.102.2.1      matt 	}
    828  1.102.2.1      matt 
    829  1.102.2.1      matt 	if (newsoparm != NULL) {
    830  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
    831  1.102.2.1      matt 		sc->sc_soparm = *newsoparm;
    832  1.102.2.1      matt 	}
    833      1.102    dyoung 
    834  1.102.2.1      matt 	if (sc->sc_soparm.sp_fd != -1) {
    835  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
    836  1.102.2.1      matt 		rc = getsock(l->l_proc->p_fd, sc->sc_soparm.sp_fd, &fp);
    837  1.102.2.1      matt 		if (rc != 0)
    838  1.102.2.1      matt 			goto shutdown;
    839  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
    840  1.102.2.1      matt 		FILE_UNUSE(fp, NULL);
    841  1.102.2.1      matt 		so = (struct socket *)fp->f_data;
    842  1.102.2.2      matt 		sc->sc_si = softint_establish(SOFTINT_NET, greintr, sc);
    843      1.102    dyoung 		gre_upcall_add(so, sc);
    844  1.102.2.1      matt 		if ((ifp->if_flags & IFF_UP) == 0) {
    845  1.102.2.1      matt 			GRE_DPRINTF(sc, "%s: down\n", __func__);
    846  1.102.2.1      matt 			goto shutdown;
    847  1.102.2.1      matt 		}
    848      1.102    dyoung 	}
    849  1.102.2.1      matt 
    850  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
    851      1.102    dyoung 	if (so != NULL)
    852      1.102    dyoung 		sc->sc_if.if_flags |= IFF_RUNNING;
    853  1.102.2.1      matt 	else {
    854  1.102.2.1      matt 		gre_bufq_purge(&sc->sc_snd);
    855      1.102    dyoung 		sc->sc_if.if_flags &= ~IFF_RUNNING;
    856      1.102    dyoung 	}
    857      1.102    dyoung 	return so;
    858      1.102    dyoung }
    859      1.102    dyoung 
    860  1.102.2.1      matt static int
    861  1.102.2.1      matt gre_input(struct gre_softc *sc, struct mbuf *m, int hlen,
    862  1.102.2.1      matt     const struct gre_h *gh)
    863       1.62    dyoung {
    864  1.102.2.4      matt 	uint16_t flags;
    865  1.102.2.4      matt 	uint32_t af;		/* af passed to BPF tap */
    866  1.102.2.1      matt 	int isr, s;
    867       1.62    dyoung 	struct ifqueue *ifq;
    868       1.62    dyoung 
    869       1.62    dyoung 	sc->sc_if.if_ipackets++;
    870       1.62    dyoung 	sc->sc_if.if_ibytes += m->m_pkthdr.len;
    871       1.62    dyoung 
    872       1.89    dyoung 	hlen += sizeof(struct gre_h);
    873       1.62    dyoung 
    874       1.89    dyoung 	/* process GRE flags as packet can be of variable len */
    875       1.89    dyoung 	flags = ntohs(gh->flags);
    876       1.62    dyoung 
    877       1.89    dyoung 	/* Checksum & Offset are present */
    878       1.89    dyoung 	if ((flags & GRE_CP) | (flags & GRE_RP))
    879       1.89    dyoung 		hlen += 4;
    880       1.89    dyoung 	/* We don't support routing fields (variable length) */
    881       1.89    dyoung 	if (flags & GRE_RP) {
    882       1.89    dyoung 		sc->sc_if.if_ierrors++;
    883       1.89    dyoung 		return 0;
    884       1.89    dyoung 	}
    885       1.89    dyoung 	if (flags & GRE_KP)
    886       1.89    dyoung 		hlen += 4;
    887       1.89    dyoung 	if (flags & GRE_SP)
    888       1.89    dyoung 		hlen += 4;
    889       1.89    dyoung 
    890       1.89    dyoung 	switch (ntohs(gh->ptype)) { /* ethertypes */
    891  1.102.2.1      matt 	case ETHERTYPE_IP:
    892  1.102.2.1      matt 		ifq = &ipintrq;
    893       1.89    dyoung 		isr = NETISR_IP;
    894  1.102.2.1      matt 		af = AF_INET;
    895       1.89    dyoung 		break;
    896       1.62    dyoung #ifdef NETATALK
    897       1.89    dyoung 	case ETHERTYPE_ATALK:
    898       1.89    dyoung 		ifq = &atintrq1;
    899       1.89    dyoung 		isr = NETISR_ATALK;
    900       1.89    dyoung 		af = AF_APPLETALK;
    901       1.89    dyoung 		break;
    902       1.62    dyoung #endif
    903       1.62    dyoung #ifdef INET6
    904       1.89    dyoung 	case ETHERTYPE_IPV6:
    905       1.89    dyoung 		ifq = &ip6intrq;
    906       1.89    dyoung 		isr = NETISR_IPV6;
    907       1.89    dyoung 		af = AF_INET6;
    908       1.89    dyoung 		break;
    909       1.62    dyoung #endif
    910       1.89    dyoung 	default:	   /* others not yet supported */
    911       1.90    dyoung 		GRE_DPRINTF(sc, "%s: unhandled ethertype 0x%04x\n", __func__,
    912       1.89    dyoung 		    ntohs(gh->ptype));
    913       1.89    dyoung 		sc->sc_if.if_noproto++;
    914       1.87    dyoung 		return 0;
    915       1.62    dyoung 	}
    916       1.62    dyoung 
    917       1.62    dyoung 	if (hlen > m->m_pkthdr.len) {
    918       1.62    dyoung 		m_freem(m);
    919       1.62    dyoung 		sc->sc_if.if_ierrors++;
    920       1.87    dyoung 		return EINVAL;
    921       1.62    dyoung 	}
    922       1.62    dyoung 	m_adj(m, hlen);
    923       1.62    dyoung 
    924       1.62    dyoung #if NBPFILTER > 0
    925       1.62    dyoung 	if (sc->sc_if.if_bpf != NULL)
    926       1.62    dyoung 		bpf_mtap_af(sc->sc_if.if_bpf, af, m);
    927       1.62    dyoung #endif /*NBPFILTER > 0*/
    928       1.62    dyoung 
    929       1.62    dyoung 	m->m_pkthdr.rcvif = &sc->sc_if;
    930       1.62    dyoung 
    931  1.102.2.1      matt 	s = splnet();
    932       1.62    dyoung 	if (IF_QFULL(ifq)) {
    933       1.62    dyoung 		IF_DROP(ifq);
    934       1.62    dyoung 		m_freem(m);
    935       1.62    dyoung 	} else {
    936       1.62    dyoung 		IF_ENQUEUE(ifq, m);
    937       1.62    dyoung 	}
    938       1.62    dyoung 	/* we need schednetisr since the address family may change */
    939       1.62    dyoung 	schednetisr(isr);
    940  1.102.2.1      matt 	splx(s);
    941       1.62    dyoung 
    942       1.87    dyoung 	return 1;	/* packet is done, no further processing needed */
    943       1.62    dyoung }
    944       1.62    dyoung 
    945       1.20    itojun /*
    946        1.1       hwr  * The output routine. Takes a packet and encapsulates it in the protocol
    947  1.102.2.1      matt  * given by sc->sc_soparm.sp_proto. See also RFC 1701 and RFC 2004
    948        1.1       hwr  */
    949       1.59   thorpej static int
    950       1.84    dyoung gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    951       1.74  christos 	   struct rtentry *rt)
    952        1.1       hwr {
    953  1.102.2.1      matt 	int error = 0;
    954       1.10   thorpej 	struct gre_softc *sc = ifp->if_softc;
    955       1.62    dyoung 	struct gre_h *gh;
    956  1.102.2.1      matt 	struct ip *ip;
    957  1.102.2.4      matt 	uint8_t ip_tos = 0;
    958  1.102.2.4      matt 	uint16_t etype = 0;
    959       1.24    martin 
    960  1.102.2.1      matt 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
    961       1.37    itojun 		m_freem(m);
    962       1.38    itojun 		error = ENETDOWN;
    963       1.38    itojun 		goto end;
    964       1.37    itojun 	}
    965        1.1       hwr 
    966  1.102.2.1      matt #if NBPFILTER > 0
    967  1.102.2.1      matt 	if (ifp->if_bpf != NULL)
    968       1.52  christos 		bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
    969        1.1       hwr #endif
    970        1.1       hwr 
    971       1.26    martin 	m->m_flags &= ~(M_BCAST|M_MCAST);
    972        1.1       hwr 
    973  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: dst->sa_family=%d\n", __func__, dst->sa_family);
    974  1.102.2.1      matt 	switch (dst->sa_family) {
    975  1.102.2.1      matt 	case AF_INET:
    976       1.89    dyoung 		ip = mtod(m, struct ip *);
    977  1.102.2.1      matt 		ip_tos = ip->ip_tos;
    978  1.102.2.1      matt 		etype = htons(ETHERTYPE_IP);
    979       1.62    dyoung 		break;
    980        1.1       hwr #ifdef NETATALK
    981  1.102.2.1      matt 	case AF_APPLETALK:
    982  1.102.2.1      matt 		etype = htons(ETHERTYPE_ATALK);
    983  1.102.2.1      matt 		break;
    984        1.1       hwr #endif
    985       1.56        is #ifdef INET6
    986  1.102.2.1      matt 	case AF_INET6:
    987  1.102.2.1      matt 		etype = htons(ETHERTYPE_IPV6);
    988       1.62    dyoung 		break;
    989  1.102.2.1      matt #endif
    990       1.62    dyoung 	default:
    991        1.1       hwr 		IF_DROP(&ifp->if_snd);
    992        1.1       hwr 		m_freem(m);
    993  1.102.2.1      matt 		error = EAFNOSUPPORT;
    994       1.38    itojun 		goto end;
    995        1.1       hwr 	}
    996        1.1       hwr 
    997  1.102.2.1      matt 	M_PREPEND(m, sizeof(*gh), M_DONTWAIT);
    998       1.62    dyoung 
    999       1.62    dyoung 	if (m == NULL) {
   1000        1.1       hwr 		IF_DROP(&ifp->if_snd);
   1001       1.38    itojun 		error = ENOBUFS;
   1002       1.38    itojun 		goto end;
   1003        1.1       hwr 	}
   1004        1.1       hwr 
   1005  1.102.2.1      matt 	gh = mtod(m, struct gre_h *);
   1006  1.102.2.1      matt 	gh->flags = 0;
   1007  1.102.2.1      matt 	gh->ptype = etype;
   1008  1.102.2.1      matt 	/* XXX Need to handle IP ToS.  Look at how I handle IP TTL. */
   1009        1.1       hwr 
   1010        1.1       hwr 	ifp->if_opackets++;
   1011        1.8  explorer 	ifp->if_obytes += m->m_pkthdr.len;
   1012       1.62    dyoung 
   1013        1.1       hwr 	/* send it off */
   1014  1.102.2.1      matt 	if ((error = gre_bufq_enqueue(&sc->sc_snd, m)) != 0) {
   1015  1.102.2.1      matt 		sc->sc_oflow_ev.ev_count++;
   1016       1.91    dyoung 		m_freem(m);
   1017       1.91    dyoung 	} else
   1018  1.102.2.2      matt 		softint_schedule(sc->sc_si);
   1019       1.38    itojun   end:
   1020        1.8  explorer 	if (error)
   1021        1.1       hwr 		ifp->if_oerrors++;
   1022       1.87    dyoung 	return error;
   1023        1.1       hwr }
   1024        1.1       hwr 
   1025       1.62    dyoung static int
   1026       1.62    dyoung gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
   1027       1.62    dyoung {
   1028       1.93    dyoung 	return (*so->so_proto->pr_usrreq)(so, req, NULL, nam, NULL, l);
   1029       1.62    dyoung }
   1030       1.62    dyoung 
   1031       1.62    dyoung static int
   1032       1.62    dyoung gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
   1033       1.62    dyoung {
   1034       1.62    dyoung 	return gre_getname(so, PRU_SOCKADDR, nam, l);
   1035       1.62    dyoung }
   1036       1.62    dyoung 
   1037       1.62    dyoung static int
   1038       1.62    dyoung gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
   1039       1.62    dyoung {
   1040       1.62    dyoung 	return gre_getname(so, PRU_PEERADDR, nam, l);
   1041       1.62    dyoung }
   1042       1.62    dyoung 
   1043       1.62    dyoung static int
   1044  1.102.2.1      matt gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_storage *src,
   1045  1.102.2.1      matt     struct sockaddr_storage *dst)
   1046       1.62    dyoung {
   1047       1.62    dyoung 	struct mbuf *m;
   1048  1.102.2.1      matt 	struct sockaddr_storage *ss;
   1049       1.62    dyoung 	int rc;
   1050       1.62    dyoung 
   1051  1.102.2.3      matt 	if ((m = getsombuf(so, MT_SONAME)) == NULL)
   1052       1.62    dyoung 		return ENOBUFS;
   1053       1.62    dyoung 
   1054  1.102.2.1      matt 	ss = mtod(m, struct sockaddr_storage *);
   1055       1.62    dyoung 
   1056       1.62    dyoung 	if ((rc = gre_getsockname(so, m, l)) != 0)
   1057       1.62    dyoung 		goto out;
   1058  1.102.2.1      matt 	*src = *ss;
   1059       1.62    dyoung 
   1060       1.62    dyoung 	if ((rc = gre_getpeername(so, m, l)) != 0)
   1061       1.62    dyoung 		goto out;
   1062  1.102.2.1      matt 	*dst = *ss;
   1063       1.62    dyoung 
   1064       1.62    dyoung out:
   1065       1.62    dyoung 	m_freem(m);
   1066       1.62    dyoung 	return rc;
   1067       1.62    dyoung }
   1068       1.62    dyoung 
   1069       1.93    dyoung static void
   1070       1.93    dyoung gre_closef(struct file **fpp, struct lwp *l)
   1071       1.93    dyoung {
   1072       1.93    dyoung 	struct file *fp = *fpp;
   1073       1.93    dyoung 
   1074  1.102.2.1      matt 	mutex_enter(&fp->f_lock);
   1075       1.93    dyoung 	FILE_USE(fp);
   1076       1.93    dyoung 	closef(fp, l);
   1077       1.93    dyoung 	*fpp = NULL;
   1078       1.93    dyoung }
   1079       1.93    dyoung 
   1080       1.59   thorpej static int
   1081  1.102.2.1      matt gre_ssock(struct ifnet *ifp, struct gre_soparm *sp, int fd)
   1082        1.1       hwr {
   1083  1.102.2.1      matt 	int error, kfd;
   1084  1.102.2.1      matt 	const struct protosw *pr;
   1085       1.93    dyoung 	struct file *fp;
   1086  1.102.2.1      matt 	struct filedesc	*fdp;
   1087  1.102.2.1      matt 	struct gre_softc *sc = ifp->if_softc;
   1088  1.102.2.1      matt 	struct lwp *l = curlwp;
   1089  1.102.2.1      matt 	struct proc *kp, *p = curproc;
   1090       1.62    dyoung 	struct socket *so;
   1091  1.102.2.1      matt 	struct sockaddr_storage dst, src;
   1092  1.102.2.1      matt 
   1093  1.102.2.1      matt 	/* getsock() will FILE_USE() and unlock the descriptor for us */
   1094  1.102.2.1      matt 	if ((error = getsock(p->p_fd, fd, &fp)) != 0) {
   1095  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1096  1.102.2.1      matt 		return EINVAL;
   1097  1.102.2.1      matt 	}
   1098  1.102.2.1      matt 
   1099  1.102.2.1      matt 	/* Increase reference count.  Now that our reference to
   1100  1.102.2.1      matt 	 * the file descriptor is counted, this thread can release
   1101  1.102.2.1      matt 	 * our "use" of the descriptor, but it will not be destroyed
   1102  1.102.2.1      matt 	 * by some other thread's action.  This thread needs to
   1103  1.102.2.1      matt 	 * release its use, too, because one and only one thread
   1104  1.102.2.1      matt 	 * can have use of the descriptor at once.  The kernel
   1105  1.102.2.1      matt 	 * thread will pick up the use if it needs it.
   1106  1.102.2.1      matt 	 */
   1107  1.102.2.1      matt 	fp->f_count++;
   1108  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d f_count %d\n", __func__, __LINE__,
   1109  1.102.2.1      matt 	    fp->f_count);
   1110  1.102.2.1      matt 	FILE_UNUSE(fp, NULL);
   1111  1.102.2.1      matt 
   1112  1.102.2.1      matt 	kp = sc->sc_lwp->l_proc;
   1113  1.102.2.1      matt 	while ((error = fdalloc(kp, 0, &kfd)) != 0 && error == ENOSPC)
   1114  1.102.2.1      matt 		fdexpand(kp);
   1115  1.102.2.1      matt 	if (error != 0)
   1116  1.102.2.1      matt 		goto closef;
   1117  1.102.2.1      matt 	fdp = kp->p_fd;
   1118  1.102.2.1      matt 	rw_enter(&fdp->fd_lock, RW_WRITER);
   1119  1.102.2.1      matt 	fdp->fd_ofiles[kfd] = fp;
   1120  1.102.2.1      matt 	rw_exit(&fdp->fd_lock);
   1121  1.102.2.1      matt 
   1122  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1123  1.102.2.1      matt 
   1124  1.102.2.1      matt 	so = (struct socket *)fp->f_data;
   1125  1.102.2.1      matt 	pr = so->so_proto;
   1126  1.102.2.1      matt 	if ((pr->pr_flags & PR_ATOMIC) == 0 ||
   1127  1.102.2.1      matt 	    (sp->sp_type != 0 && pr->pr_type != sp->sp_type) ||
   1128  1.102.2.1      matt 	    (sp->sp_proto != 0 && pr->pr_protocol != 0 &&
   1129  1.102.2.1      matt 	     pr->pr_protocol != sp->sp_proto)) {
   1130  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d, type %d, proto %d\n", __func__,
   1131  1.102.2.1      matt 		    __LINE__, pr->pr_type, pr->pr_protocol);
   1132  1.102.2.1      matt 		error = EINVAL;
   1133  1.102.2.1      matt 		goto release;
   1134  1.102.2.1      matt 	}
   1135  1.102.2.1      matt 
   1136  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1137  1.102.2.1      matt 
   1138  1.102.2.1      matt 	/* check address */
   1139  1.102.2.1      matt 	if ((error = gre_getnames(so, l, &src, &dst)) != 0)
   1140  1.102.2.1      matt 		goto release;
   1141  1.102.2.1      matt 
   1142  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1143  1.102.2.1      matt 
   1144  1.102.2.1      matt 	if (error != 0)
   1145  1.102.2.1      matt 		goto release;
   1146  1.102.2.1      matt 
   1147  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1148  1.102.2.1      matt 
   1149  1.102.2.1      matt 	sp->sp_src = src;
   1150  1.102.2.1      matt 	sp->sp_dst = dst;
   1151  1.102.2.1      matt 	/* fp does not any longer belong to this thread. */
   1152  1.102.2.1      matt 	sp->sp_fd = kfd;
   1153  1.102.2.1      matt 
   1154  1.102.2.1      matt 	/* XXX print src & dst */
   1155  1.102.2.1      matt 
   1156  1.102.2.1      matt 	return 0;
   1157  1.102.2.1      matt release:
   1158  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1159  1.102.2.1      matt 	fdrelease(sc->sc_lwp, kfd);
   1160  1.102.2.1      matt 	return error;
   1161  1.102.2.1      matt closef:
   1162  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1163  1.102.2.1      matt 	gre_closef(&fp, l);
   1164  1.102.2.1      matt 	return error;
   1165  1.102.2.1      matt }
   1166  1.102.2.1      matt 
   1167  1.102.2.1      matt static bool
   1168  1.102.2.1      matt sockaddr_is_anyaddr(const struct sockaddr *sa)
   1169  1.102.2.1      matt {
   1170  1.102.2.1      matt 	socklen_t anylen, salen;
   1171  1.102.2.1      matt 	const void *anyaddr, *addr;
   1172  1.102.2.1      matt 
   1173  1.102.2.1      matt 	if ((anyaddr = sockaddr_anyaddr(sa, &anylen)) == NULL ||
   1174  1.102.2.1      matt 	    (addr = sockaddr_const_addr(sa, &salen)) == NULL)
   1175  1.102.2.1      matt 		return false;
   1176  1.102.2.1      matt 
   1177  1.102.2.1      matt 	if (salen > anylen)
   1178  1.102.2.1      matt 		return false;
   1179  1.102.2.1      matt 
   1180  1.102.2.1      matt 	return memcmp(anyaddr, addr, MIN(anylen, salen)) == 0;
   1181  1.102.2.1      matt }
   1182  1.102.2.1      matt 
   1183  1.102.2.1      matt static bool
   1184  1.102.2.1      matt gre_is_nullconf(const struct gre_soparm *sp)
   1185  1.102.2.1      matt {
   1186  1.102.2.1      matt 	return sockaddr_is_anyaddr(sstocsa(&sp->sp_src)) ||
   1187  1.102.2.1      matt 	       sockaddr_is_anyaddr(sstocsa(&sp->sp_dst));
   1188  1.102.2.1      matt }
   1189  1.102.2.1      matt 
   1190  1.102.2.1      matt static void
   1191  1.102.2.1      matt gre_clearconf(struct gre_soparm *sp, bool force)
   1192  1.102.2.1      matt {
   1193  1.102.2.1      matt 	if (sp->sp_bysock || force) {
   1194  1.102.2.1      matt 		sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
   1195  1.102.2.1      matt 		    sockaddr_any(sstosa(&sp->sp_src)));
   1196  1.102.2.1      matt 		sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
   1197  1.102.2.1      matt 		    sockaddr_any(sstosa(&sp->sp_dst)));
   1198  1.102.2.1      matt 		sp->sp_bysock = 0;
   1199  1.102.2.1      matt 	}
   1200  1.102.2.1      matt 	sp->sp_fd = -1;
   1201  1.102.2.1      matt }
   1202  1.102.2.1      matt 
   1203  1.102.2.1      matt static int
   1204  1.102.2.3      matt gre_ioctl_lock(struct gre_softc *sc)
   1205  1.102.2.3      matt {
   1206  1.102.2.3      matt 	mutex_enter(&sc->sc_mtx);
   1207  1.102.2.3      matt 
   1208  1.102.2.3      matt 	while (sc->sc_state == GRE_S_IOCTL)
   1209  1.102.2.3      matt 		gre_wait(sc);
   1210  1.102.2.3      matt 
   1211  1.102.2.3      matt 	if (sc->sc_state != GRE_S_IDLE) {
   1212  1.102.2.3      matt 		cv_signal(&sc->sc_condvar);
   1213  1.102.2.3      matt 		mutex_exit(&sc->sc_mtx);
   1214  1.102.2.3      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1215  1.102.2.3      matt 		return ENXIO;
   1216  1.102.2.3      matt 	}
   1217  1.102.2.3      matt 
   1218  1.102.2.3      matt 	sc->sc_state = GRE_S_IOCTL;
   1219  1.102.2.3      matt 
   1220  1.102.2.3      matt 	mutex_exit(&sc->sc_mtx);
   1221  1.102.2.3      matt 	return 0;
   1222  1.102.2.3      matt }
   1223  1.102.2.3      matt 
   1224  1.102.2.3      matt static void
   1225  1.102.2.3      matt gre_ioctl_unlock(struct gre_softc *sc)
   1226  1.102.2.3      matt {
   1227  1.102.2.3      matt 	mutex_enter(&sc->sc_mtx);
   1228  1.102.2.3      matt 
   1229  1.102.2.3      matt 	KASSERT(sc->sc_state == GRE_S_IOCTL);
   1230  1.102.2.3      matt 	sc->sc_state = GRE_S_IDLE;
   1231  1.102.2.3      matt 	cv_signal(&sc->sc_condvar);
   1232  1.102.2.3      matt 
   1233  1.102.2.3      matt 	mutex_exit(&sc->sc_mtx);
   1234  1.102.2.3      matt }
   1235  1.102.2.3      matt 
   1236  1.102.2.3      matt static int
   1237  1.102.2.1      matt gre_ioctl(struct ifnet *ifp, const u_long cmd, void *data)
   1238  1.102.2.1      matt {
   1239  1.102.2.1      matt 	struct lwp *l = curlwp;
   1240       1.94  christos 	struct ifreq *ifr;
   1241       1.28    itojun 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
   1242        1.8  explorer 	struct gre_softc *sc = ifp->if_softc;
   1243  1.102.2.1      matt 	struct gre_soparm *sp;
   1244  1.102.2.3      matt 	int fd, error = 0, oproto, otype, s;
   1245  1.102.2.1      matt 	struct gre_soparm sp0;
   1246  1.102.2.1      matt 
   1247  1.102.2.1      matt 	ifr = data;
   1248  1.102.2.1      matt 
   1249  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d, cmd %lu\n", __func__, __LINE__, cmd);
   1250  1.102.2.1      matt 
   1251       1.61        ad 	switch (cmd) {
   1252       1.61        ad 	case SIOCSIFFLAGS:
   1253       1.61        ad 	case SIOCSIFMTU:
   1254       1.61        ad 	case GRESPROTO:
   1255       1.61        ad 	case GRESADDRD:
   1256       1.61        ad 	case GRESADDRS:
   1257       1.62    dyoung 	case GRESSOCK:
   1258       1.62    dyoung 	case GREDSOCK:
   1259       1.61        ad 	case SIOCSLIFPHYADDR:
   1260       1.61        ad 	case SIOCDIFPHYADDR:
   1261       1.70      elad 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
   1262       1.70      elad 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
   1263       1.70      elad 		    NULL) != 0)
   1264       1.87    dyoung 			return EPERM;
   1265       1.61        ad 		break;
   1266       1.61        ad 	default:
   1267       1.61        ad 		break;
   1268       1.61        ad 	}
   1269        1.1       hwr 
   1270  1.102.2.3      matt 	if ((error = gre_ioctl_lock(sc)) != 0) {
   1271  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1272  1.102.2.3      matt 		return error;
   1273  1.102.2.1      matt 	}
   1274  1.102.2.3      matt 	s = splnet();
   1275  1.102.2.1      matt 
   1276  1.102.2.1      matt 	sp0 = sc->sc_soparm;
   1277  1.102.2.1      matt 	sp0.sp_fd = -1;
   1278  1.102.2.1      matt 	sp = &sp0;
   1279  1.102.2.1      matt 
   1280  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1281  1.102.2.1      matt 
   1282       1.20    itojun 	switch (cmd) {
   1283       1.20    itojun 	case SIOCSIFADDR:
   1284  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1285  1.102.2.1      matt 		if ((ifp->if_flags & IFF_UP) != 0)
   1286  1.102.2.1      matt 			break;
   1287  1.102.2.1      matt 		gre_clearconf(sp, false);
   1288       1.36    itojun 		ifp->if_flags |= IFF_UP;
   1289  1.102.2.1      matt 		goto mksocket;
   1290       1.55     perry 	case SIOCSIFDSTADDR:
   1291        1.1       hwr 		break;
   1292        1.1       hwr 	case SIOCSIFFLAGS:
   1293  1.102.2.1      matt 		oproto = sp->sp_proto;
   1294  1.102.2.1      matt 		otype = sp->sp_type;
   1295       1.62    dyoung 		switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
   1296       1.62    dyoung 		case IFF_LINK0|IFF_LINK2:
   1297  1.102.2.1      matt 			sp->sp_proto = IPPROTO_UDP;
   1298  1.102.2.1      matt 			sp->sp_type = SOCK_DGRAM;
   1299  1.102.2.1      matt 			break;
   1300  1.102.2.1      matt 		case IFF_LINK2:
   1301  1.102.2.1      matt 			sp->sp_proto = 0;
   1302  1.102.2.1      matt 			sp->sp_type = 0;
   1303       1.62    dyoung 			break;
   1304       1.62    dyoung 		case IFF_LINK0:
   1305  1.102.2.1      matt 			sp->sp_proto = IPPROTO_GRE;
   1306  1.102.2.1      matt 			sp->sp_type = SOCK_RAW;
   1307  1.102.2.1      matt 			break;
   1308  1.102.2.1      matt 		default:
   1309  1.102.2.1      matt 			GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1310  1.102.2.1      matt 			error = EINVAL;
   1311  1.102.2.1      matt 			goto out;
   1312  1.102.2.1      matt 		}
   1313  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1314  1.102.2.1      matt 		gre_clearconf(sp, false);
   1315  1.102.2.1      matt 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
   1316  1.102.2.1      matt 		    (IFF_UP|IFF_RUNNING) &&
   1317  1.102.2.1      matt 		    (oproto == sp->sp_proto || sp->sp_proto == 0) &&
   1318  1.102.2.1      matt 		    (otype == sp->sp_type || sp->sp_type == 0))
   1319  1.102.2.1      matt 			break;
   1320  1.102.2.1      matt 		switch (sp->sp_proto) {
   1321  1.102.2.1      matt 		case IPPROTO_UDP:
   1322  1.102.2.1      matt 		case IPPROTO_GRE:
   1323  1.102.2.1      matt 			goto mksocket;
   1324  1.102.2.1      matt 		default:
   1325  1.102.2.1      matt 			break;
   1326       1.62    dyoung 		}
   1327        1.1       hwr 		break;
   1328       1.20    itojun 	case SIOCSIFMTU:
   1329  1.102.2.1      matt 		/* XXX determine MTU automatically by probing w/
   1330  1.102.2.1      matt 		 * XXX do-not-fragment packets?
   1331  1.102.2.1      matt 		 */
   1332       1.27    martin 		if (ifr->ifr_mtu < 576) {
   1333        1.1       hwr 			error = EINVAL;
   1334        1.1       hwr 			break;
   1335        1.1       hwr 		}
   1336  1.102.2.4      matt 		/*FALLTHROUGH*/
   1337        1.1       hwr 	case SIOCGIFMTU:
   1338  1.102.2.4      matt 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
   1339  1.102.2.4      matt 			error = 0;
   1340        1.1       hwr 		break;
   1341        1.1       hwr 	case SIOCADDMULTI:
   1342        1.1       hwr 	case SIOCDELMULTI:
   1343  1.102.2.1      matt 		if (ifr == NULL) {
   1344        1.1       hwr 			error = EAFNOSUPPORT;
   1345        1.1       hwr 			break;
   1346        1.1       hwr 		}
   1347  1.102.2.1      matt 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
   1348        1.1       hwr #ifdef INET
   1349        1.1       hwr 		case AF_INET:
   1350        1.1       hwr 			break;
   1351        1.1       hwr #endif
   1352       1.56        is #ifdef INET6
   1353       1.56        is 		case AF_INET6:
   1354       1.56        is 			break;
   1355       1.56        is #endif
   1356        1.1       hwr 		default:
   1357        1.1       hwr 			error = EAFNOSUPPORT;
   1358        1.1       hwr 			break;
   1359        1.1       hwr 		}
   1360        1.1       hwr 		break;
   1361        1.1       hwr 	case GRESPROTO:
   1362  1.102.2.1      matt 		gre_clearconf(sp, false);
   1363  1.102.2.1      matt 		oproto = sp->sp_proto;
   1364  1.102.2.1      matt 		otype = sp->sp_type;
   1365  1.102.2.1      matt 		sp->sp_proto = ifr->ifr_flags;
   1366  1.102.2.1      matt 		switch (sp->sp_proto) {
   1367       1.62    dyoung 		case IPPROTO_UDP:
   1368       1.62    dyoung 			ifp->if_flags |= IFF_LINK0|IFF_LINK2;
   1369  1.102.2.1      matt 			sp->sp_type = SOCK_DGRAM;
   1370       1.62    dyoung 			break;
   1371       1.40    itojun 		case IPPROTO_GRE:
   1372        1.3       hwr 			ifp->if_flags |= IFF_LINK0;
   1373       1.62    dyoung 			ifp->if_flags &= ~IFF_LINK2;
   1374  1.102.2.1      matt 			sp->sp_type = SOCK_RAW;
   1375  1.102.2.1      matt 			break;
   1376  1.102.2.1      matt 		case 0:
   1377  1.102.2.1      matt 			ifp->if_flags &= ~IFF_LINK0;
   1378  1.102.2.1      matt 			ifp->if_flags |= IFF_LINK2;
   1379  1.102.2.1      matt 			sp->sp_type = 0;
   1380  1.102.2.1      matt 			break;
   1381        1.1       hwr 		default:
   1382       1.40    itojun 			error = EPROTONOSUPPORT;
   1383       1.35    itojun 			break;
   1384        1.1       hwr 		}
   1385  1.102.2.1      matt 		if ((oproto == sp->sp_proto || sp->sp_proto == 0) &&
   1386  1.102.2.1      matt 		    (otype == sp->sp_type || sp->sp_type == 0))
   1387  1.102.2.1      matt 			break;
   1388  1.102.2.1      matt 		switch (sp->sp_proto) {
   1389  1.102.2.1      matt 		case IPPROTO_UDP:
   1390  1.102.2.1      matt 		case IPPROTO_GRE:
   1391  1.102.2.1      matt 			goto mksocket;
   1392  1.102.2.1      matt 		default:
   1393  1.102.2.1      matt 			break;
   1394  1.102.2.1      matt 		}
   1395        1.1       hwr 		break;
   1396        1.1       hwr 	case GREGPROTO:
   1397  1.102.2.1      matt 		ifr->ifr_flags = sp->sp_proto;
   1398        1.1       hwr 		break;
   1399        1.1       hwr 	case GRESADDRS:
   1400        1.1       hwr 	case GRESADDRD:
   1401  1.102.2.1      matt 		gre_clearconf(sp, false);
   1402        1.1       hwr 		/*
   1403       1.20    itojun 		 * set tunnel endpoints, compute a less specific route
   1404       1.20    itojun 		 * to the remote end and mark if as up
   1405       1.20    itojun 		 */
   1406  1.102.2.1      matt 		switch (cmd) {
   1407  1.102.2.1      matt 		case GRESADDRS:
   1408  1.102.2.1      matt 			sockaddr_copy(sstosa(&sp->sp_src),
   1409  1.102.2.1      matt 			    sizeof(sp->sp_src), ifreq_getaddr(cmd, ifr));
   1410  1.102.2.1      matt 			break;
   1411  1.102.2.1      matt 		case GRESADDRD:
   1412  1.102.2.1      matt 			sockaddr_copy(sstosa(&sp->sp_dst),
   1413  1.102.2.1      matt 			    sizeof(sp->sp_dst), ifreq_getaddr(cmd, ifr));
   1414       1.62    dyoung 			break;
   1415      1.102    dyoung 		}
   1416  1.102.2.1      matt 	checkaddr:
   1417  1.102.2.1      matt 		if (sockaddr_any(sstosa(&sp->sp_src)) == NULL ||
   1418  1.102.2.1      matt 		    sockaddr_any(sstosa(&sp->sp_dst)) == NULL) {
   1419       1.62    dyoung 			error = EINVAL;
   1420       1.62    dyoung 			break;
   1421       1.62    dyoung 		}
   1422  1.102.2.1      matt 		/* let gre_socreate() check the rest */
   1423  1.102.2.1      matt 	mksocket:
   1424  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1425  1.102.2.1      matt 		/* If we're administratively down, or the configuration
   1426  1.102.2.1      matt 		 * is empty, there's no use creating a socket.
   1427  1.102.2.1      matt 		 */
   1428  1.102.2.1      matt 		if ((ifp->if_flags & IFF_UP) == 0 || gre_is_nullconf(sp))
   1429  1.102.2.1      matt 			goto sendconf;
   1430  1.102.2.1      matt 
   1431  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1432  1.102.2.1      matt 		error = gre_socreate(sc, l, sp, &fd);
   1433  1.102.2.1      matt 
   1434  1.102.2.1      matt 		if (error != 0)
   1435       1.62    dyoung 			break;
   1436       1.62    dyoung 
   1437  1.102.2.1      matt 	setsock:
   1438  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1439       1.93    dyoung 
   1440  1.102.2.1      matt 		error = gre_ssock(ifp, sp, fd);
   1441       1.93    dyoung 
   1442  1.102.2.1      matt 		if (cmd != GRESSOCK) {
   1443      1.102    dyoung 			GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1444  1.102.2.1      matt 			fdrelease(l, fd);
   1445       1.93    dyoung 		}
   1446  1.102.2.1      matt 
   1447       1.93    dyoung 		if (error == 0) {
   1448  1.102.2.1      matt 	sendconf:
   1449      1.102    dyoung 			GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1450  1.102.2.1      matt 			ifp->if_flags &= ~IFF_RUNNING;
   1451  1.102.2.1      matt 			sc->sc_so = gre_reconf(sc, sc->sc_so, sc->sc_lwp, sp);
   1452       1.93    dyoung 		}
   1453       1.62    dyoung 
   1454       1.62    dyoung 		break;
   1455  1.102.2.1      matt 	case GREGADDRS:
   1456  1.102.2.1      matt 		ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_src));
   1457  1.102.2.1      matt 		break;
   1458  1.102.2.1      matt 	case GREGADDRD:
   1459  1.102.2.1      matt 		ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_dst));
   1460  1.102.2.1      matt 		break;
   1461  1.102.2.1      matt 	case GREDSOCK:
   1462  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1463  1.102.2.1      matt 		if (sp->sp_bysock)
   1464  1.102.2.1      matt 			ifp->if_flags &= ~IFF_UP;
   1465  1.102.2.1      matt 		gre_clearconf(sp, false);
   1466  1.102.2.1      matt 		goto mksocket;
   1467  1.102.2.1      matt 	case GRESSOCK:
   1468  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1469  1.102.2.1      matt 		gre_clearconf(sp, true);
   1470  1.102.2.1      matt 		fd = (int)ifr->ifr_value;
   1471  1.102.2.1      matt 		sp->sp_bysock = 1;
   1472  1.102.2.1      matt 		ifp->if_flags |= IFF_UP;
   1473  1.102.2.1      matt 		goto setsock;
   1474       1.28    itojun 	case SIOCSLIFPHYADDR:
   1475  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1476  1.102.2.1      matt 		if (lifr->addr.ss_family != lifr->dstaddr.ss_family) {
   1477       1.31    itojun 			error = EAFNOSUPPORT;
   1478       1.31    itojun 			break;
   1479       1.31    itojun 		}
   1480  1.102.2.1      matt 		sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
   1481  1.102.2.1      matt 		    sstosa(&lifr->addr));
   1482  1.102.2.1      matt 		sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
   1483  1.102.2.1      matt 		    sstosa(&lifr->dstaddr));
   1484  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1485  1.102.2.1      matt 		goto checkaddr;
   1486       1.28    itojun 	case SIOCDIFPHYADDR:
   1487  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1488  1.102.2.1      matt 		gre_clearconf(sp, true);
   1489  1.102.2.1      matt 		ifp->if_flags &= ~IFF_UP;
   1490  1.102.2.1      matt 		goto mksocket;
   1491       1.28    itojun 	case SIOCGLIFPHYADDR:
   1492  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1493  1.102.2.1      matt 		if (gre_is_nullconf(sp)) {
   1494       1.28    itojun 			error = EADDRNOTAVAIL;
   1495       1.28    itojun 			break;
   1496       1.28    itojun 		}
   1497  1.102.2.1      matt 		sockaddr_copy(sstosa(&lifr->addr), sizeof(lifr->addr),
   1498  1.102.2.1      matt 		    sstosa(&sp->sp_src));
   1499  1.102.2.1      matt 		sockaddr_copy(sstosa(&lifr->dstaddr), sizeof(lifr->dstaddr),
   1500  1.102.2.1      matt 		    sstosa(&sp->sp_dst));
   1501  1.102.2.1      matt 		GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1502        1.1       hwr 		break;
   1503        1.1       hwr 	default:
   1504        1.1       hwr 		error = EINVAL;
   1505       1.31    itojun 		break;
   1506        1.1       hwr 	}
   1507  1.102.2.1      matt out:
   1508  1.102.2.1      matt 	GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
   1509  1.102.2.3      matt 	splx(s);
   1510  1.102.2.3      matt 	gre_ioctl_unlock(sc);
   1511       1.87    dyoung 	return error;
   1512        1.1       hwr }
   1513        1.1       hwr 
   1514       1.54  christos #endif
   1515       1.54  christos 
   1516       1.59   thorpej void	greattach(int);
   1517       1.54  christos 
   1518       1.54  christos /* ARGSUSED */
   1519       1.54  christos void
   1520       1.74  christos greattach(int count)
   1521       1.54  christos {
   1522       1.54  christos #ifdef INET
   1523       1.54  christos 	if_clone_attach(&gre_cloner);
   1524       1.54  christos #endif
   1525       1.54  christos }
   1526