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