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