Home | History | Annotate | Line # | Download | only in net
if_gre.c revision 1.178
      1 /*	$NetBSD: if_gre.c,v 1.178 2021/02/12 19:57:49 roy 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.178 2021/02/12 19:57:49 roy 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 
    399 	/* If the GRE header is not aligned, slurp it up into a new
    400 	 * mbuf with space for link headers, in the event we forward
    401 	 * it.  Otherwise, if it is aligned, make sure the entire
    402 	 * base GRE header is in the first mbuf of the chain.
    403 	 */
    404 	if (GRE_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
    405 		if ((m = m_copyup(m, sizeof(struct gre_h),
    406 		    (max_linkhdr + 3) & ~3)) == NULL) {
    407 			/* XXXJRT new stat, please */
    408 			GRE_DPRINTF(sc, "m_copyup failed\n");
    409 			sc->sc_pullup_ev.ev_count++;
    410 			return;
    411 		}
    412 	} else if (__predict_false(m->m_len < sizeof(struct gre_h))) {
    413 		if ((m = m_pullup(m, sizeof(struct gre_h))) == NULL) {
    414 			GRE_DPRINTF(sc, "m_pullup failed\n");
    415 			sc->sc_pullup_ev.ev_count++;
    416 			return;
    417 		}
    418 	}
    419 	gh = mtod(m, const struct gre_h *);
    420 
    421 	if (gre_input(sc, m, gh) == 0) {
    422 		sc->sc_unsupp_ev.ev_count++;
    423 		GRE_DPRINTF(sc, "dropping unsupported\n");
    424 		m_freem(m);
    425 	}
    426 }
    427 
    428 static void
    429 gre_upcall_add(struct socket *so, void *arg)
    430 {
    431 	/* XXX What if the kernel already set an upcall? */
    432 	KASSERT((so->so_rcv.sb_flags & SB_UPCALL) == 0);
    433 	so->so_upcallarg = arg;
    434 	so->so_upcall = gre_receive;
    435 	so->so_rcv.sb_flags |= SB_UPCALL;
    436 }
    437 
    438 static void
    439 gre_upcall_remove(struct socket *so)
    440 {
    441 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    442 	so->so_upcallarg = NULL;
    443 	so->so_upcall = NULL;
    444 }
    445 
    446 static int
    447 gre_socreate(struct gre_softc *sc, const struct gre_soparm *sp, int *fdout)
    448 {
    449 	int fd, rc;
    450 	struct socket *so;
    451 	struct sockaddr_big sbig;
    452 	sa_family_t af;
    453 	int val;
    454 
    455 	GRE_DPRINTF(sc, "enter\n");
    456 
    457 	af = sp->sp_src.ss_family;
    458 	rc = fsocreate(af, NULL, sp->sp_type, sp->sp_proto, &fd);
    459 	if (rc != 0) {
    460 		GRE_DPRINTF(sc, "fsocreate failed\n");
    461 		return rc;
    462 	}
    463 
    464 	if ((rc = fd_getsock(fd, &so)) != 0)
    465 		return rc;
    466 
    467 	memcpy(&sbig, &sp->sp_src, sizeof(sp->sp_src));
    468 	if ((rc = sobind(so, (struct sockaddr *)&sbig, curlwp)) != 0) {
    469 		GRE_DPRINTF(sc, "sobind failed\n");
    470 		goto out;
    471 	}
    472 
    473 	memcpy(&sbig, &sp->sp_dst, sizeof(sp->sp_dst));
    474 	solock(so);
    475 	if ((rc = soconnect(so, (struct sockaddr *)&sbig, curlwp)) != 0) {
    476 		GRE_DPRINTF(sc, "soconnect failed\n");
    477 		sounlock(so);
    478 		goto out;
    479 	}
    480 	sounlock(so);
    481 
    482 	/* XXX convert to a (new) SOL_SOCKET call */
    483   	KASSERT(so->so_proto != NULL);
    484  	rc = so_setsockopt(curlwp, so, IPPROTO_IP, IP_TTL,
    485 	    &ip_gre_ttl, sizeof(ip_gre_ttl));
    486   	if (rc != 0) {
    487  		GRE_DPRINTF(sc, "so_setsockopt ttl failed\n");
    488   		rc = 0;
    489   	}
    490 
    491  	val = 1;
    492  	rc = so_setsockopt(curlwp, so, SOL_SOCKET, SO_NOHEADER,
    493 	    &val, sizeof(val));
    494   	if (rc != 0) {
    495  		GRE_DPRINTF(sc, "so_setsockopt SO_NOHEADER failed\n");
    496 		rc = 0;
    497 	}
    498 out:
    499 	if (rc != 0)
    500 		fd_close(fd);
    501 	else  {
    502 		fd_putfile(fd);
    503 		*fdout = fd;
    504 	}
    505 
    506 	return rc;
    507 }
    508 
    509 static int
    510 gre_sosend(struct socket *so, struct mbuf *top)
    511 {
    512 	struct proc	*p;
    513 	long		space, resid;
    514 	int		error;
    515 	struct lwp * const l = curlwp;
    516 
    517 	p = l->l_proc;
    518 
    519 	resid = top->m_pkthdr.len;
    520 	if (p)
    521 		l->l_ru.ru_msgsnd++;
    522 #define	snderr(errno)	{ error = errno; goto release; }
    523 
    524 	solock(so);
    525 	if ((error = sblock(&so->so_snd, M_NOWAIT)) != 0)
    526 		goto out;
    527 	if (so->so_state & SS_CANTSENDMORE)
    528 		snderr(EPIPE);
    529 	if (so->so_error) {
    530 		error = so->so_error;
    531 		so->so_error = 0;
    532 		goto release;
    533 	}
    534 	if ((so->so_state & SS_ISCONNECTED) == 0) {
    535 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    536 			snderr(ENOTCONN);
    537 		} else {
    538 			snderr(EDESTADDRREQ);
    539 		}
    540 	}
    541 	space = sbspace(&so->so_snd);
    542 	if (resid > so->so_snd.sb_hiwat)
    543 		snderr(EMSGSIZE);
    544 	if (space < resid)
    545 		snderr(EWOULDBLOCK);
    546 	/*
    547 	 * Data is prepackaged in "top".
    548 	 */
    549 	if (so->so_state & SS_CANTSENDMORE)
    550 		snderr(EPIPE);
    551 	error = (*so->so_proto->pr_usrreqs->pr_send)(so,
    552 	    top, NULL, NULL, l);
    553 	top = NULL;
    554  release:
    555 	sbunlock(&so->so_snd);
    556  out:
    557  	sounlock(so);
    558 	if (top != NULL)
    559 		m_freem(top);
    560 	return error;
    561 }
    562 
    563 /* This is a stripped-down version of soreceive() that will never
    564  * block.  It will support SOCK_DGRAM sockets.  It may also support
    565  * SOCK_SEQPACKET sockets.
    566  */
    567 static int
    568 gre_soreceive(struct socket *so, struct mbuf **mp0)
    569 {
    570 	struct mbuf *m, **mp;
    571 	int flags, len, error, type;
    572 	const struct protosw	*pr;
    573 	struct mbuf *nextrecord;
    574 
    575 	KASSERT(mp0 != NULL);
    576 
    577 	flags = MSG_DONTWAIT;
    578 	pr = so->so_proto;
    579 	mp = mp0;
    580 	type = 0;
    581 
    582 	*mp = NULL;
    583 
    584 	KASSERT(pr->pr_flags & PR_ATOMIC);
    585  restart:
    586 	if ((error = sblock(&so->so_rcv, M_NOWAIT)) != 0) {
    587 		return error;
    588 	}
    589 	m = so->so_rcv.sb_mb;
    590 	/*
    591 	 * If we have less data than requested, do not block awaiting more.
    592 	 */
    593 	if (m == NULL) {
    594 #ifdef DIAGNOSTIC
    595 		if (so->so_rcv.sb_cc)
    596 			panic("receive 1");
    597 #endif
    598 		if (so->so_error) {
    599 			error = so->so_error;
    600 			so->so_error = 0;
    601 		} else if (so->so_state & SS_CANTRCVMORE)
    602 			;
    603 		else if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0
    604 		      && (so->so_proto->pr_flags & PR_CONNREQUIRED))
    605 			error = ENOTCONN;
    606 		else
    607 			error = EWOULDBLOCK;
    608 		goto release;
    609 	}
    610 	/*
    611 	 * On entry here, m points to the first record of the socket buffer.
    612 	 * While we process the initial mbufs containing address and control
    613 	 * info, we save a copy of m->m_nextpkt into nextrecord.
    614 	 */
    615 	if (curlwp != NULL)
    616 		curlwp->l_ru.ru_msgrcv++;
    617 	KASSERT(m == so->so_rcv.sb_mb);
    618 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
    619 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
    620 	nextrecord = m->m_nextpkt;
    621 	if (pr->pr_flags & PR_ADDR) {
    622 #ifdef DIAGNOSTIC
    623 		if (m->m_type != MT_SONAME)
    624 			panic("receive 1a");
    625 #endif
    626 		sbfree(&so->so_rcv, m);
    627 		m = so->so_rcv.sb_mb = m_free(m);
    628 	}
    629 	while (m != NULL && m->m_type == MT_CONTROL && error == 0) {
    630 		sbfree(&so->so_rcv, m);
    631 		/*
    632 		 * Dispose of any SCM_RIGHTS message that went
    633 		 * through the read path rather than recv.
    634 		 */
    635 		if (pr->pr_domain->dom_dispose &&
    636 		    mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
    637 			(*pr->pr_domain->dom_dispose)(m);
    638 		m = so->so_rcv.sb_mb = m_free(m);
    639 	}
    640 
    641 	/*
    642 	 * If m is non-NULL, we have some data to read.  From now on,
    643 	 * make sure to keep sb_lastrecord consistent when working on
    644 	 * the last packet on the chain (nextrecord == NULL) and we
    645 	 * change m->m_nextpkt.
    646 	 */
    647 	if (m != NULL) {
    648 		m->m_nextpkt = nextrecord;
    649 		/*
    650 		 * If nextrecord == NULL (this is a single chain),
    651 		 * then sb_lastrecord may not be valid here if m
    652 		 * was changed earlier.
    653 		 */
    654 		if (nextrecord == NULL) {
    655 			KASSERT(so->so_rcv.sb_mb == m);
    656 			so->so_rcv.sb_lastrecord = m;
    657 		}
    658 		type = m->m_type;
    659 		if (type == MT_OOBDATA)
    660 			flags |= MSG_OOB;
    661 	} else {
    662 		KASSERT(so->so_rcv.sb_mb == m);
    663 		so->so_rcv.sb_mb = nextrecord;
    664 		SB_EMPTY_FIXUP(&so->so_rcv);
    665 	}
    666 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
    667 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
    668 
    669 	while (m != NULL) {
    670 		if (m->m_type == MT_OOBDATA) {
    671 			if (type != MT_OOBDATA)
    672 				break;
    673 		} else if (type == MT_OOBDATA)
    674 			break;
    675 #ifdef DIAGNOSTIC
    676 		else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
    677 			panic("receive 3");
    678 #endif
    679 		so->so_state &= ~SS_RCVATMARK;
    680 		if (so->so_oobmark != 0 && so->so_oobmark < m->m_len)
    681 			break;
    682 		len = m->m_len;
    683 		/*
    684 		 * mp is set, just pass back the mbufs.
    685 		 * Sockbuf must be consistent here (points to current mbuf,
    686 		 * it points to next record) when we drop priority;
    687 		 * we must note any additions to the sockbuf when we
    688 		 * block interrupts again.
    689 		 */
    690 		if (m->m_flags & M_EOR)
    691 			flags |= MSG_EOR;
    692 		nextrecord = m->m_nextpkt;
    693 		sbfree(&so->so_rcv, m);
    694 		*mp = m;
    695 		mp = &m->m_next;
    696 		so->so_rcv.sb_mb = m = m->m_next;
    697 		*mp = NULL;
    698 		/*
    699 		 * If m != NULL, we also know that
    700 		 * so->so_rcv.sb_mb != NULL.
    701 		 */
    702 		KASSERT(so->so_rcv.sb_mb == m);
    703 		if (m) {
    704 			m->m_nextpkt = nextrecord;
    705 			if (nextrecord == NULL)
    706 				so->so_rcv.sb_lastrecord = m;
    707 		} else {
    708 			so->so_rcv.sb_mb = nextrecord;
    709 			SB_EMPTY_FIXUP(&so->so_rcv);
    710 		}
    711 		SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
    712 		SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
    713 		if (so->so_oobmark) {
    714 			so->so_oobmark -= len;
    715 			if (so->so_oobmark == 0) {
    716 				so->so_state |= SS_RCVATMARK;
    717 				break;
    718 			}
    719 		}
    720 		if (flags & MSG_EOR)
    721 			break;
    722 	}
    723 
    724 	if (m != NULL) {
    725 		m_freem(*mp);
    726 		*mp = NULL;
    727 		error = ENOMEM;
    728 		(void) sbdroprecord(&so->so_rcv);
    729 	} else {
    730 		/*
    731 		 * First part is an inline SB_EMPTY_FIXUP().  Second
    732 		 * part makes sure sb_lastrecord is up-to-date if
    733 		 * there is still data in the socket buffer.
    734 		 */
    735 		so->so_rcv.sb_mb = nextrecord;
    736 		if (so->so_rcv.sb_mb == NULL) {
    737 			so->so_rcv.sb_mbtail = NULL;
    738 			so->so_rcv.sb_lastrecord = NULL;
    739 		} else if (nextrecord->m_nextpkt == NULL)
    740 			so->so_rcv.sb_lastrecord = nextrecord;
    741 	}
    742 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
    743 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
    744 	if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
    745 		(*pr->pr_usrreqs->pr_rcvd)(so, flags, curlwp);
    746 	if (*mp0 == NULL && (flags & MSG_EOR) == 0 &&
    747 	    (so->so_state & SS_CANTRCVMORE) == 0) {
    748 		sbunlock(&so->so_rcv);
    749 		goto restart;
    750 	}
    751 
    752  release:
    753 	sbunlock(&so->so_rcv);
    754 	return error;
    755 }
    756 
    757 static struct socket *
    758 gre_reconf(struct gre_softc *sc, const struct gre_soparm *newsoparm)
    759 {
    760 	struct ifnet *ifp = &sc->sc_if;
    761 
    762 	GRE_DPRINTF(sc, "enter\n");
    763 
    764 shutdown:
    765 	if (sc->sc_soparm.sp_so != NULL) {
    766 		GRE_DPRINTF(sc, "\n");
    767 		gre_upcall_remove(sc->sc_soparm.sp_so);
    768 		softint_disestablish(sc->sc_si);
    769 		sc->sc_si = NULL;
    770 		gre_fp_send(sc, GRE_M_DELFP, NULL);
    771 		gre_clearconf(&sc->sc_soparm, false);
    772 	}
    773 
    774 	if (newsoparm != NULL) {
    775 		GRE_DPRINTF(sc, "\n");
    776 		sc->sc_soparm = *newsoparm;
    777 		newsoparm = NULL;
    778 	}
    779 
    780 	if (sc->sc_soparm.sp_so != NULL) {
    781 		GRE_DPRINTF(sc, "\n");
    782 		sc->sc_si = softint_establish(SOFTINT_NET, greintr, sc);
    783 		gre_upcall_add(sc->sc_soparm.sp_so, sc);
    784 		if ((ifp->if_flags & IFF_UP) == 0) {
    785 			GRE_DPRINTF(sc, "down\n");
    786 			goto shutdown;
    787 		}
    788 	}
    789 
    790 	GRE_DPRINTF(sc, "\n");
    791 	if (sc->sc_soparm.sp_so != NULL)
    792 		sc->sc_if.if_flags |= IFF_RUNNING;
    793 	else {
    794 		gre_bufq_purge(&sc->sc_snd);
    795 		sc->sc_if.if_flags &= ~IFF_RUNNING;
    796 	}
    797 	return sc->sc_soparm.sp_so;
    798 }
    799 
    800 static int
    801 gre_input(struct gre_softc *sc, struct mbuf *m, const struct gre_h *gh)
    802 {
    803 	pktqueue_t *pktq = NULL;
    804 	struct ifqueue *ifq = NULL;
    805 	uint16_t flags;
    806 	uint32_t af;		/* af passed to BPF tap */
    807 	int isr = 0, s;
    808 	int hlen;
    809 
    810 	if_statadd2(&sc->sc_if, if_ipackets, 1, if_ibytes, m->m_pkthdr.len);
    811 
    812 	hlen = sizeof(struct gre_h);
    813 
    814 	/* process GRE flags as packet can be of variable len */
    815 	flags = ntohs(gh->flags);
    816 
    817 	/* Checksum & Offset are present */
    818 	if ((flags & GRE_CP) | (flags & GRE_RP))
    819 		hlen += 4;
    820 	/* We don't support routing fields (variable length) */
    821 	if (flags & GRE_RP) {
    822 		if_statinc(&sc->sc_if, if_ierrors);
    823 		return 0;
    824 	}
    825 	if (flags & GRE_KP)
    826 		hlen += 4;
    827 	if (flags & GRE_SP)
    828 		hlen += 4;
    829 
    830 	switch (ntohs(gh->ptype)) { /* ethertypes */
    831 #ifdef INET
    832 	case ETHERTYPE_IP:
    833 		pktq = ip_pktq;
    834 		af = AF_INET;
    835 		break;
    836 #endif
    837 #ifdef NETATALK
    838 	case ETHERTYPE_ATALK:
    839 		ifq = &atintrq1;
    840 		isr = NETISR_ATALK;
    841 		af = AF_APPLETALK;
    842 		break;
    843 #endif
    844 #ifdef INET6
    845 	case ETHERTYPE_IPV6:
    846 		pktq = ip6_pktq;
    847 		af = AF_INET6;
    848 		break;
    849 #endif
    850 #ifdef MPLS
    851 	case ETHERTYPE_MPLS:
    852 		ifq = &mplsintrq;
    853 		isr = NETISR_MPLS;
    854 		af = AF_MPLS;
    855 		break;
    856 #endif
    857 	default:	   /* others not yet supported */
    858 		GRE_DPRINTF(sc, "unhandled ethertype 0x%04x\n",
    859 		    ntohs(gh->ptype));
    860 		if_statinc(&sc->sc_if, if_noproto);
    861 		return 0;
    862 	}
    863 
    864 	if (hlen > m->m_pkthdr.len) {
    865 		m_freem(m);
    866 		if_statinc(&sc->sc_if, if_ierrors);
    867 		return 1;
    868 	}
    869 	m_adj(m, hlen);
    870 
    871 	bpf_mtap_af(&sc->sc_if, af, m, BPF_D_IN);
    872 
    873 	m_set_rcvif(m, &sc->sc_if);
    874 
    875 	if (__predict_true(pktq)) {
    876 		if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
    877 			m_freem(m);
    878 		}
    879 		return 1;
    880 	}
    881 
    882 	s = splnet();
    883 	if (IF_QFULL(ifq)) {
    884 		IF_DROP(ifq);
    885 		m_freem(m);
    886 	} else {
    887 		IF_ENQUEUE(ifq, m);
    888 	}
    889 	/* we need schednetisr since the address family may change */
    890 	schednetisr(isr);
    891 	splx(s);
    892 
    893 	return 1;	/* packet is done, no further processing needed */
    894 }
    895 
    896 /*
    897  * The output routine. Takes a packet and encapsulates it in the protocol
    898  * given by sc->sc_soparm.sp_proto. See also RFC 1701 and RFC 2004
    899  */
    900 static int
    901 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    902     const struct rtentry *rt)
    903 {
    904 	int error = 0;
    905 	struct gre_softc *sc = ifp->if_softc;
    906 	struct gre_h *gh;
    907 	uint16_t etype = 0;
    908 
    909 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    910 
    911 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
    912 		m_freem(m);
    913 		error = ENETDOWN;
    914 		goto end;
    915 	}
    916 
    917 	bpf_mtap_af(ifp, dst->sa_family, m, BPF_D_OUT);
    918 
    919 	m->m_flags &= ~(M_BCAST|M_MCAST);
    920 
    921 	GRE_DPRINTF(sc, "dst->sa_family=%d\n", dst->sa_family);
    922 	switch (dst->sa_family) {
    923 #ifdef INET
    924 	case AF_INET:
    925 		/*
    926 		 * TBD Extract the IP ToS field and set the
    927 		 * encapsulating protocol's ToS to suit.
    928 		 */
    929 		etype = htons(ETHERTYPE_IP);
    930 		break;
    931 #endif
    932 #ifdef NETATALK
    933 	case AF_APPLETALK:
    934 		etype = htons(ETHERTYPE_ATALK);
    935 		break;
    936 #endif
    937 #ifdef INET6
    938 	case AF_INET6:
    939 		etype = htons(ETHERTYPE_IPV6);
    940 		break;
    941 #endif
    942 	default:
    943 		IF_DROP(&ifp->if_snd);
    944 		m_freem(m);
    945 		error = EAFNOSUPPORT;
    946 		goto end;
    947 	}
    948 
    949 #ifdef MPLS
    950 	if (rt != NULL && rt_gettag(rt) != NULL) {
    951 		union mpls_shim msh;
    952 		msh.s_addr = MPLS_GETSADDR(rt);
    953 		if (msh.shim.label != MPLS_LABEL_IMPLNULL)
    954 			etype = htons(ETHERTYPE_MPLS);
    955 	}
    956 #endif
    957 
    958 	M_PREPEND(m, sizeof(*gh), M_DONTWAIT);
    959 	if (m == NULL) {
    960 		IF_DROP(&ifp->if_snd);
    961 		error = ENOBUFS;
    962 		goto end;
    963 	}
    964 
    965 	gh = mtod(m, struct gre_h *);
    966 	KASSERT(GRE_HDR_ALIGNED_P(gh));
    967 	gh->flags = 0;
    968 	gh->ptype = etype;
    969 	/* XXX Need to handle IP ToS.  Look at how I handle IP TTL. */
    970 
    971 	if_statadd2(ifp, if_opackets, 1, if_obytes, m->m_pkthdr.len);
    972 
    973 	/* Clear checksum-offload flags. */
    974 	m->m_pkthdr.csum_flags = 0;
    975 	m->m_pkthdr.csum_data = 0;
    976 
    977 	/* send it off */
    978 	if ((error = gre_bufq_enqueue(&sc->sc_snd, m)) != 0) {
    979 		sc->sc_oflow_ev.ev_count++;
    980 		m_freem(m);
    981 	} else {
    982 		kpreempt_disable();
    983 		softint_schedule(sc->sc_si);
    984 		kpreempt_enable();
    985 	}
    986 
    987 end:
    988 	if (error)
    989 		if_statinc(ifp, if_oerrors);
    990 	return error;
    991 }
    992 
    993 static int
    994 gre_getsockname(struct socket *so, struct sockaddr *nam)
    995 {
    996 	return (*so->so_proto->pr_usrreqs->pr_sockaddr)(so, nam);
    997 }
    998 
    999 static int
   1000 gre_getpeername(struct socket *so, struct sockaddr *nam)
   1001 {
   1002 	return (*so->so_proto->pr_usrreqs->pr_peeraddr)(so, nam);
   1003 }
   1004 
   1005 static int
   1006 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_storage *src,
   1007     struct sockaddr_storage *dst)
   1008 {
   1009 	struct sockaddr_storage ss;
   1010 	int rc;
   1011 
   1012 	solock(so);
   1013 	if ((rc = gre_getsockname(so, (struct sockaddr *)&ss)) != 0)
   1014 		goto out;
   1015 	*src = ss;
   1016 
   1017 	if ((rc = gre_getpeername(so, (struct sockaddr *)&ss)) != 0)
   1018 		goto out;
   1019 	*dst = ss;
   1020 out:
   1021 	sounlock(so);
   1022 	return rc;
   1023 }
   1024 
   1025 static void
   1026 gre_fp_recvloop(void *arg)
   1027 {
   1028 	struct gre_softc *sc = arg;
   1029 
   1030 	mutex_enter(&sc->sc_mtx);
   1031 	while (gre_fp_recv(sc))
   1032 		;
   1033 	mutex_exit(&sc->sc_mtx);
   1034 	kthread_exit(0);
   1035 }
   1036 
   1037 static bool
   1038 gre_fp_recv(struct gre_softc *sc)
   1039 {
   1040 	int fd, ofd, rc;
   1041 	file_t *fp;
   1042 
   1043 	fp = sc->sc_fp;
   1044 	ofd = sc->sc_fd;
   1045 	fd = -1;
   1046 
   1047 	switch (sc->sc_msg) {
   1048 	case GRE_M_STOP:
   1049 		cv_signal(&sc->sc_fp_condvar);
   1050 		return false;
   1051 	case GRE_M_SETFP:
   1052 		mutex_exit(&sc->sc_mtx);
   1053 		rc = fd_dup(fp, 0, &fd, 0);
   1054 		mutex_enter(&sc->sc_mtx);
   1055 		if (rc != 0) {
   1056 			sc->sc_msg = GRE_M_ERR;
   1057 			break;
   1058 		}
   1059 		/*FALLTHROUGH*/
   1060 	case GRE_M_DELFP:
   1061 		mutex_exit(&sc->sc_mtx);
   1062 		if (ofd != -1 && fd_getfile(ofd) != NULL)
   1063 			fd_close(ofd);
   1064 		mutex_enter(&sc->sc_mtx);
   1065 		sc->sc_fd = fd;
   1066 		sc->sc_msg = GRE_M_OK;
   1067 		break;
   1068 	default:
   1069 		gre_fp_wait(sc);
   1070 		return true;
   1071 	}
   1072 	cv_signal(&sc->sc_fp_condvar);
   1073 	return true;
   1074 }
   1075 
   1076 static bool
   1077 gre_fp_send(struct gre_softc *sc, enum gre_msg msg, file_t *fp)
   1078 {
   1079 	bool rc;
   1080 
   1081 	mutex_enter(&sc->sc_mtx);
   1082 	while (sc->sc_msg != GRE_M_NONE)
   1083 		gre_fp_wait(sc);
   1084 	sc->sc_fp = fp;
   1085 	sc->sc_msg = msg;
   1086 	cv_signal(&sc->sc_fp_condvar);
   1087 	while (sc->sc_msg != GRE_M_STOP && sc->sc_msg != GRE_M_OK &&
   1088 	            sc->sc_msg != GRE_M_ERR)
   1089 		gre_fp_wait(sc);
   1090 	rc = (sc->sc_msg != GRE_M_ERR);
   1091 	sc->sc_msg = GRE_M_NONE;
   1092 	cv_signal(&sc->sc_fp_condvar);
   1093 	mutex_exit(&sc->sc_mtx);
   1094 	return rc;
   1095 }
   1096 
   1097 static int
   1098 gre_ssock(struct ifnet *ifp, struct gre_soparm *sp, int fd)
   1099 {
   1100 	int error = 0;
   1101 	const struct protosw *pr;
   1102 	file_t *fp;
   1103 	struct gre_softc *sc = ifp->if_softc;
   1104 	struct socket *so;
   1105 	struct sockaddr_storage dst, src;
   1106 
   1107 	if ((fp = fd_getfile(fd)) == NULL)
   1108 		return EBADF;
   1109 	if (fp->f_type != DTYPE_SOCKET) {
   1110 		fd_putfile(fd);
   1111 		return ENOTSOCK;
   1112 	}
   1113 
   1114 	GRE_DPRINTF(sc, "\n");
   1115 
   1116 	so = fp->f_socket;
   1117 	pr = so->so_proto;
   1118 
   1119 	GRE_DPRINTF(sc, "type %d, proto %d\n", pr->pr_type, pr->pr_protocol);
   1120 
   1121 	if ((pr->pr_flags & PR_ATOMIC) == 0 ||
   1122 	    (sp->sp_type != 0 && pr->pr_type != sp->sp_type) ||
   1123 	    (sp->sp_proto != 0 && pr->pr_protocol != 0 &&
   1124 	     pr->pr_protocol != sp->sp_proto)) {
   1125 		error = EINVAL;
   1126 		goto err;
   1127 	}
   1128 
   1129 	GRE_DPRINTF(sc, "\n");
   1130 
   1131 	/* check address */
   1132 	if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0)
   1133 		goto err;
   1134 
   1135 	GRE_DPRINTF(sc, "\n");
   1136 
   1137 	if (!gre_fp_send(sc, GRE_M_SETFP, fp)) {
   1138 		error = EBUSY;
   1139 		goto err;
   1140 	}
   1141 
   1142 	GRE_DPRINTF(sc, "\n");
   1143 
   1144 	sp->sp_src = src;
   1145 	sp->sp_dst = dst;
   1146 
   1147 	sp->sp_so = so;
   1148 
   1149 err:
   1150 	fd_putfile(fd);
   1151 	return error;
   1152 }
   1153 
   1154 static bool
   1155 sockaddr_is_anyaddr(const struct sockaddr *sa)
   1156 {
   1157 	socklen_t anylen, salen;
   1158 	const void *anyaddr, *addr;
   1159 
   1160 	if ((anyaddr = sockaddr_anyaddr(sa, &anylen)) == NULL ||
   1161 	    (addr = sockaddr_const_addr(sa, &salen)) == NULL)
   1162 		return false;
   1163 
   1164 	if (salen > anylen)
   1165 		return false;
   1166 
   1167 	return memcmp(anyaddr, addr, MIN(anylen, salen)) == 0;
   1168 }
   1169 
   1170 static bool
   1171 gre_is_nullconf(const struct gre_soparm *sp)
   1172 {
   1173 	return sockaddr_is_anyaddr(sstocsa(&sp->sp_src)) ||
   1174 	       sockaddr_is_anyaddr(sstocsa(&sp->sp_dst));
   1175 }
   1176 
   1177 static void
   1178 gre_clearconf(struct gre_soparm *sp, bool force)
   1179 {
   1180 	if (sp->sp_bysock || force) {
   1181 		sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
   1182 		    sockaddr_any(sstosa(&sp->sp_src)));
   1183 		sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
   1184 		    sockaddr_any(sstosa(&sp->sp_dst)));
   1185 		sp->sp_bysock = false;
   1186 	}
   1187 	sp->sp_so = NULL; /* XXX */
   1188 }
   1189 
   1190 static int
   1191 gre_ioctl(struct ifnet *ifp, const u_long cmd, void *data)
   1192 {
   1193 	struct ifreq *ifr;
   1194 	struct ifaddr *ifa = (struct ifaddr *)data;
   1195 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
   1196 	struct gre_softc *sc = ifp->if_softc;
   1197 	struct gre_soparm *sp;
   1198 	int fd, error = 0, oproto, otype, s;
   1199 	struct gre_soparm sp0;
   1200 
   1201 	ifr = data;
   1202 
   1203 	GRE_DPRINTF(sc, "cmd %lu\n", cmd);
   1204 
   1205 	switch (cmd) {
   1206 	case GRESPROTO:
   1207 	case GRESADDRD:
   1208 	case GRESADDRS:
   1209 	case GRESSOCK:
   1210 	case GREDSOCK:
   1211 		if (kauth_authorize_network(curlwp->l_cred,
   1212 		    KAUTH_NETWORK_INTERFACE,
   1213 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
   1214 		    NULL) != 0)
   1215 			return EPERM;
   1216 		break;
   1217 	default:
   1218 		break;
   1219 	}
   1220 
   1221 	s = splnet();
   1222 
   1223 	sp0 = sc->sc_soparm;
   1224 	sp0.sp_so = NULL;
   1225 	sp = &sp0;
   1226 
   1227 	GRE_DPRINTF(sc, "\n");
   1228 
   1229 	switch (cmd) {
   1230 	case SIOCINITIFADDR:
   1231 		GRE_DPRINTF(sc, "\n");
   1232 		if ((ifp->if_flags & IFF_UP) != 0)
   1233 			break;
   1234 		gre_clearconf(sp, false);
   1235 		ifp->if_flags |= IFF_UP;
   1236 		ifa->ifa_rtrequest = p2p_rtrequest;
   1237 		goto mksocket;
   1238 	case SIOCSIFFLAGS:
   1239 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1240 			break;
   1241 		oproto = sp->sp_proto;
   1242 		otype = sp->sp_type;
   1243 		switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
   1244 		case IFF_LINK0|IFF_LINK2:
   1245 			sp->sp_proto = IPPROTO_UDP;
   1246 			sp->sp_type = SOCK_DGRAM;
   1247 			break;
   1248 		case IFF_LINK2:
   1249 			sp->sp_proto = 0;
   1250 			sp->sp_type = 0;
   1251 			break;
   1252 		case IFF_LINK0:
   1253 			sp->sp_proto = IPPROTO_GRE;
   1254 			sp->sp_type = SOCK_RAW;
   1255 			break;
   1256 		default:
   1257 			GRE_DPRINTF(sc, "\n");
   1258 			error = EINVAL;
   1259 			goto out;
   1260 		}
   1261 		GRE_DPRINTF(sc, "\n");
   1262 		gre_clearconf(sp, false);
   1263 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
   1264 		    (IFF_UP|IFF_RUNNING) &&
   1265 		    (oproto == sp->sp_proto || sp->sp_proto == 0) &&
   1266 		    (otype == sp->sp_type || sp->sp_type == 0))
   1267 			break;
   1268 		switch (sp->sp_proto) {
   1269 		case IPPROTO_UDP:
   1270 		case IPPROTO_GRE:
   1271 			goto mksocket;
   1272 		default:
   1273 			break;
   1274 		}
   1275 		break;
   1276 	case SIOCSIFMTU:
   1277 		/* XXX determine MTU automatically by probing w/
   1278 		 * XXX do-not-fragment packets?
   1279 		 */
   1280 		if (ifr->ifr_mtu < 576) {
   1281 			error = EINVAL;
   1282 			break;
   1283 		}
   1284 		/*FALLTHROUGH*/
   1285 	case SIOCGIFMTU:
   1286 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
   1287 			error = 0;
   1288 		break;
   1289 	case SIOCADDMULTI:
   1290 	case SIOCDELMULTI:
   1291 		if (ifr == NULL) {
   1292 			error = EAFNOSUPPORT;
   1293 			break;
   1294 		}
   1295 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
   1296 #ifdef INET
   1297 		case AF_INET:
   1298 			break;
   1299 #endif
   1300 #ifdef INET6
   1301 		case AF_INET6:
   1302 			break;
   1303 #endif
   1304 		default:
   1305 			error = EAFNOSUPPORT;
   1306 			break;
   1307 		}
   1308 		break;
   1309 	case GRESPROTO:
   1310 		gre_clearconf(sp, false);
   1311 		oproto = sp->sp_proto;
   1312 		otype = sp->sp_type;
   1313 		sp->sp_proto = ifr->ifr_flags;
   1314 		switch (sp->sp_proto) {
   1315 		case IPPROTO_UDP:
   1316 			ifp->if_flags |= IFF_LINK0|IFF_LINK2;
   1317 			sp->sp_type = SOCK_DGRAM;
   1318 			break;
   1319 		case IPPROTO_GRE:
   1320 			ifp->if_flags |= IFF_LINK0;
   1321 			ifp->if_flags &= ~IFF_LINK2;
   1322 			sp->sp_type = SOCK_RAW;
   1323 			break;
   1324 		case 0:
   1325 			ifp->if_flags &= ~IFF_LINK0;
   1326 			ifp->if_flags |= IFF_LINK2;
   1327 			sp->sp_type = 0;
   1328 			break;
   1329 		default:
   1330 			error = EPROTONOSUPPORT;
   1331 			break;
   1332 		}
   1333 		if ((oproto == sp->sp_proto || sp->sp_proto == 0) &&
   1334 		    (otype == sp->sp_type || sp->sp_type == 0))
   1335 			break;
   1336 		switch (sp->sp_proto) {
   1337 		case IPPROTO_UDP:
   1338 		case IPPROTO_GRE:
   1339 			goto mksocket;
   1340 		default:
   1341 			break;
   1342 		}
   1343 		break;
   1344 	case GREGPROTO:
   1345 		ifr->ifr_flags = sp->sp_proto;
   1346 		break;
   1347 	case GRESADDRS:
   1348 	case GRESADDRD:
   1349 		gre_clearconf(sp, false);
   1350 		/* set tunnel endpoints and mark interface as up */
   1351 		switch (cmd) {
   1352 		case GRESADDRS:
   1353 			sockaddr_copy(sstosa(&sp->sp_src),
   1354 			    sizeof(sp->sp_src), ifreq_getaddr(cmd, ifr));
   1355 			break;
   1356 		case GRESADDRD:
   1357 			sockaddr_copy(sstosa(&sp->sp_dst),
   1358 			    sizeof(sp->sp_dst), ifreq_getaddr(cmd, ifr));
   1359 			break;
   1360 		}
   1361 	checkaddr:
   1362 		if (sockaddr_any(sstosa(&sp->sp_src)) == NULL ||
   1363 		    sockaddr_any(sstosa(&sp->sp_dst)) == NULL) {
   1364 			error = EINVAL;
   1365 			break;
   1366 		}
   1367 		/* let gre_socreate() check the rest */
   1368 	mksocket:
   1369 		GRE_DPRINTF(sc, "\n");
   1370 		/* If we're administratively down, or the configuration
   1371 		 * is empty, there's no use creating a socket.
   1372 		 */
   1373 		if ((ifp->if_flags & IFF_UP) == 0 || gre_is_nullconf(sp))
   1374 			goto sendconf;
   1375 
   1376 		GRE_DPRINTF(sc, "\n");
   1377 		fd = 0;
   1378 		error = gre_socreate(sc, sp, &fd);
   1379 		if (error != 0)
   1380 			break;
   1381 
   1382 	setsock:
   1383 		GRE_DPRINTF(sc, "\n");
   1384 
   1385 		error = gre_ssock(ifp, sp, fd);
   1386 
   1387 		if (cmd != GRESSOCK) {
   1388 			GRE_DPRINTF(sc, "\n");
   1389 			/* XXX v. dodgy */
   1390 			if (fd_getfile(fd) != NULL)
   1391 				fd_close(fd);
   1392 		}
   1393 
   1394 		if (error == 0) {
   1395 	sendconf:
   1396 			GRE_DPRINTF(sc, "\n");
   1397 			ifp->if_flags &= ~IFF_RUNNING;
   1398 			gre_reconf(sc, sp);
   1399 		}
   1400 
   1401 		break;
   1402 	case GREGADDRS:
   1403 		ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_src));
   1404 		break;
   1405 	case GREGADDRD:
   1406 		ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_dst));
   1407 		break;
   1408 	case GREDSOCK:
   1409 		GRE_DPRINTF(sc, "\n");
   1410 		if (sp->sp_bysock)
   1411 			ifp->if_flags &= ~IFF_UP;
   1412 		gre_clearconf(sp, false);
   1413 		goto mksocket;
   1414 	case GRESSOCK:
   1415 		GRE_DPRINTF(sc, "\n");
   1416 		gre_clearconf(sp, true);
   1417 		fd = (int)ifr->ifr_value;
   1418 		sp->sp_bysock = true;
   1419 		ifp->if_flags |= IFF_UP;
   1420 		goto setsock;
   1421 	case SIOCSLIFPHYADDR:
   1422 		GRE_DPRINTF(sc, "\n");
   1423 		if (lifr->addr.ss_family != lifr->dstaddr.ss_family) {
   1424 			error = EAFNOSUPPORT;
   1425 			break;
   1426 		}
   1427 		sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
   1428 		    sstosa(&lifr->addr));
   1429 		sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
   1430 		    sstosa(&lifr->dstaddr));
   1431 		GRE_DPRINTF(sc, "\n");
   1432 		goto checkaddr;
   1433 	case SIOCDIFPHYADDR:
   1434 		GRE_DPRINTF(sc, "\n");
   1435 		gre_clearconf(sp, true);
   1436 		ifp->if_flags &= ~IFF_UP;
   1437 		goto mksocket;
   1438 	case SIOCGLIFPHYADDR:
   1439 		GRE_DPRINTF(sc, "\n");
   1440 		if (gre_is_nullconf(sp)) {
   1441 			error = EADDRNOTAVAIL;
   1442 			break;
   1443 		}
   1444 		sockaddr_copy(sstosa(&lifr->addr), sizeof(lifr->addr),
   1445 		    sstosa(&sp->sp_src));
   1446 		sockaddr_copy(sstosa(&lifr->dstaddr), sizeof(lifr->dstaddr),
   1447 		    sstosa(&sp->sp_dst));
   1448 		GRE_DPRINTF(sc, "\n");
   1449 		break;
   1450 	default:
   1451 		error = ifioctl_common(ifp, cmd, data);
   1452 		break;
   1453 	}
   1454 out:
   1455 	GRE_DPRINTF(sc, "\n");
   1456 	splx(s);
   1457 	return error;
   1458 }
   1459 
   1460 /* ARGSUSED */
   1461 void
   1462 greattach(int count)
   1463 {
   1464 
   1465 	/*
   1466 	 * Nothing to do here, initialization is handled by the
   1467 	 * module initialization code in greinit() below.
   1468 	 */
   1469 }
   1470 
   1471 static void
   1472 greinit(void)
   1473 {
   1474 	if_clone_attach(&gre_cloner);
   1475 }
   1476 
   1477 static int
   1478 gredetach(void)
   1479 {
   1480 	int error = 0;
   1481 
   1482 	if (gre_count != 0)
   1483 		error = EBUSY;
   1484 
   1485 	if (error == 0)
   1486 		if_clone_detach(&gre_cloner);
   1487 
   1488 	return error;
   1489 }
   1490 
   1491 /*
   1492  * Module infrastructure
   1493  */
   1494 #include "if_module.h"
   1495 
   1496 IF_MODULE(MODULE_CLASS_DRIVER, gre, NULL)
   1497