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