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