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