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