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