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