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