Home | History | Annotate | Line # | Download | only in net
if_gre.c revision 1.59.4.1
      1  1.59.4.1    rpaulo /*	$NetBSD: if_gre.c,v 1.59.4.1 2006/09/09 02:58:06 rpaulo Exp $ */
      2       1.4   thorpej 
      3       1.1       hwr /*
      4       1.4   thorpej  * Copyright (c) 1998 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.1       hwr  * Redistribution and use in source and binary forms, with or without
     13       1.1       hwr  * modification, are permitted provided that the following conditions
     14       1.1       hwr  * are met:
     15       1.1       hwr  * 1. Redistributions of source code must retain the above copyright
     16       1.1       hwr  *    notice, this list of conditions and the following disclaimer.
     17       1.1       hwr  * 2. Redistributions in binary form must reproduce the above copyright
     18       1.1       hwr  *    notice, this list of conditions and the following disclaimer in the
     19       1.1       hwr  *    documentation and/or other materials provided with the distribution.
     20       1.1       hwr  * 3. All advertising materials mentioning features or use of this software
     21       1.1       hwr  *    must display the following acknowledgement:
     22       1.1       hwr  *        This product includes software developed by the NetBSD
     23       1.1       hwr  *        Foundation, Inc. and its contributors.
     24       1.1       hwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25       1.1       hwr  *    contributors may be used to endorse or promote products derived
     26       1.1       hwr  *    from this software without specific prior written permission.
     27       1.1       hwr  *
     28       1.1       hwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29       1.1       hwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30       1.1       hwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31       1.1       hwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32       1.1       hwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33       1.1       hwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34       1.1       hwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35       1.1       hwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36       1.1       hwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37       1.1       hwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38       1.1       hwr  * POSSIBILITY OF SUCH DAMAGE.
     39       1.1       hwr  */
     40       1.1       hwr 
     41       1.1       hwr /*
     42       1.1       hwr  * Encapsulate L3 protocols into IP
     43       1.1       hwr  * See RFC 1701 and 1702 for more details.
     44       1.1       hwr  * If_gre is compatible with Cisco GRE tunnels, so you can
     45       1.1       hwr  * have a NetBSD box as the other end of a tunnel interface of a Cisco
     46       1.1       hwr  * router. See gre(4) for more details.
     47       1.6       hwr  * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
     48       1.1       hwr  */
     49      1.22     lukem 
     50      1.22     lukem #include <sys/cdefs.h>
     51  1.59.4.1    rpaulo __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.59.4.1 2006/09/09 02:58:06 rpaulo Exp $");
     52       1.1       hwr 
     53  1.59.4.1    rpaulo #include "opt_gre.h"
     54       1.1       hwr #include "opt_inet.h"
     55       1.1       hwr #include "bpfilter.h"
     56       1.1       hwr 
     57      1.54  christos #ifdef INET
     58       1.1       hwr #include <sys/param.h>
     59  1.59.4.1    rpaulo #include <sys/file.h>
     60  1.59.4.1    rpaulo #include <sys/filedesc.h>
     61       1.1       hwr #include <sys/malloc.h>
     62       1.1       hwr #include <sys/mbuf.h>
     63      1.13    martin #include <sys/proc.h>
     64       1.1       hwr #include <sys/protosw.h>
     65       1.1       hwr #include <sys/socket.h>
     66  1.59.4.1    rpaulo #include <sys/socketvar.h>
     67       1.1       hwr #include <sys/ioctl.h>
     68      1.10   thorpej #include <sys/queue.h>
     69       1.1       hwr #if __NetBSD__
     70       1.1       hwr #include <sys/systm.h>
     71  1.59.4.1    rpaulo #include <sys/sysctl.h>
     72  1.59.4.1    rpaulo #include <sys/kauth.h>
     73       1.1       hwr #endif
     74       1.1       hwr 
     75  1.59.4.1    rpaulo #include <sys/kthread.h>
     76  1.59.4.1    rpaulo 
     77       1.1       hwr #include <machine/cpu.h>
     78       1.1       hwr 
     79       1.1       hwr #include <net/ethertypes.h>
     80       1.1       hwr #include <net/if.h>
     81       1.1       hwr #include <net/if_types.h>
     82       1.1       hwr #include <net/netisr.h>
     83       1.1       hwr #include <net/route.h>
     84       1.1       hwr 
     85       1.1       hwr #ifdef INET
     86       1.1       hwr #include <netinet/in.h>
     87       1.1       hwr #include <netinet/in_systm.h>
     88       1.1       hwr #include <netinet/in_var.h>
     89       1.1       hwr #include <netinet/ip.h>
     90       1.1       hwr #include <netinet/ip_var.h>
     91       1.1       hwr #else
     92       1.4   thorpej #error "Huh? if_gre without inet?"
     93       1.1       hwr #endif
     94       1.1       hwr 
     95       1.1       hwr 
     96       1.1       hwr #ifdef NETATALK
     97       1.1       hwr #include <netatalk/at.h>
     98       1.1       hwr #include <netatalk/at_var.h>
     99       1.1       hwr #include <netatalk/at_extern.h>
    100       1.1       hwr #endif
    101       1.1       hwr 
    102       1.1       hwr #if NBPFILTER > 0
    103       1.1       hwr #include <sys/time.h>
    104       1.1       hwr #include <net/bpf.h>
    105       1.1       hwr #endif
    106       1.1       hwr 
    107       1.1       hwr #include <net/if_gre.h>
    108       1.1       hwr 
    109      1.20    itojun /*
    110      1.27    martin  * It is not easy to calculate the right value for a GRE MTU.
    111      1.27    martin  * We leave this task to the admin and use the same default that
    112      1.27    martin  * other vendors use.
    113      1.20    itojun  */
    114      1.27    martin #define GREMTU 1476
    115       1.1       hwr 
    116  1.59.4.1    rpaulo #ifdef GRE_DEBUG
    117  1.59.4.1    rpaulo #define	GRE_DPRINTF(__sc, __fmt, ...)				\
    118  1.59.4.1    rpaulo 	do {							\
    119  1.59.4.1    rpaulo 		if (((__sc)->sc_if.if_flags & IFF_DEBUG) != 0)	\
    120  1.59.4.1    rpaulo 			printf(__fmt, __VA_ARGS__);		\
    121  1.59.4.1    rpaulo 	} while (/*CONSTCOND*/0)
    122  1.59.4.1    rpaulo #else
    123  1.59.4.1    rpaulo #define	GRE_DPRINTF(__sc, __fmt, ...)	do { } while (/*CONSTCOND*/0)
    124  1.59.4.1    rpaulo #endif /* GRE_DEBUG */
    125  1.59.4.1    rpaulo 
    126      1.11   thorpej struct gre_softc_head gre_softc_list;
    127      1.26    martin int ip_gre_ttl = GRE_TTL;
    128       1.1       hwr 
    129      1.59   thorpej static int	gre_clone_create(struct if_clone *, int);
    130      1.59   thorpej static int	gre_clone_destroy(struct ifnet *);
    131      1.10   thorpej 
    132      1.59   thorpej static struct if_clone gre_cloner =
    133      1.10   thorpej     IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
    134       1.1       hwr 
    135      1.59   thorpej static int	gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
    136      1.59   thorpej 			   struct rtentry *);
    137      1.59   thorpej static int	gre_ioctl(struct ifnet *, u_long, caddr_t);
    138       1.1       hwr 
    139      1.59   thorpej static int	gre_compute_route(struct gre_softc *sc);
    140      1.59   thorpej 
    141  1.59.4.1    rpaulo static int gre_getsockname(struct socket *, struct mbuf *, struct lwp *);
    142  1.59.4.1    rpaulo static int gre_getpeername(struct socket *, struct mbuf *, struct lwp *);
    143  1.59.4.1    rpaulo static int gre_getnames(struct socket *, struct lwp *, struct sockaddr_in *,
    144  1.59.4.1    rpaulo     struct sockaddr_in *);
    145  1.59.4.1    rpaulo 
    146  1.59.4.1    rpaulo static void
    147  1.59.4.1    rpaulo gre_stop(int *running)
    148  1.59.4.1    rpaulo {
    149  1.59.4.1    rpaulo 	*running = 0;
    150  1.59.4.1    rpaulo 	wakeup(running);
    151  1.59.4.1    rpaulo }
    152  1.59.4.1    rpaulo 
    153  1.59.4.1    rpaulo static void
    154  1.59.4.1    rpaulo gre_join(int *running)
    155  1.59.4.1    rpaulo {
    156  1.59.4.1    rpaulo 	int s;
    157  1.59.4.1    rpaulo 
    158  1.59.4.1    rpaulo 	s = splnet();
    159  1.59.4.1    rpaulo 	while (*running != 0) {
    160  1.59.4.1    rpaulo 		splx(s);
    161  1.59.4.1    rpaulo 		tsleep(running, PSOCK, "grejoin", 0);
    162  1.59.4.1    rpaulo 		s = splnet();
    163  1.59.4.1    rpaulo 	}
    164  1.59.4.1    rpaulo 	splx(s);
    165  1.59.4.1    rpaulo }
    166  1.59.4.1    rpaulo 
    167  1.59.4.1    rpaulo static void
    168  1.59.4.1    rpaulo gre_wakeup(struct gre_softc *sc)
    169  1.59.4.1    rpaulo {
    170  1.59.4.1    rpaulo 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    171  1.59.4.1    rpaulo 	sc->sc_waitchan = 1;
    172  1.59.4.1    rpaulo 	wakeup(&sc->sc_waitchan);
    173  1.59.4.1    rpaulo }
    174  1.59.4.1    rpaulo 
    175      1.59   thorpej static int
    176      1.59   thorpej gre_clone_create(struct if_clone *ifc, int unit)
    177       1.1       hwr {
    178       1.8  explorer 	struct gre_softc *sc;
    179       1.1       hwr 
    180      1.10   thorpej 	sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
    181      1.10   thorpej 	memset(sc, 0, sizeof(struct gre_softc));
    182      1.10   thorpej 
    183      1.50    itojun 	snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
    184      1.50    itojun 	    ifc->ifc_name, unit);
    185      1.10   thorpej 	sc->sc_if.if_softc = sc;
    186      1.51      tron 	sc->sc_if.if_type = IFT_TUNNEL;
    187      1.34    itojun 	sc->sc_if.if_addrlen = 0;
    188      1.10   thorpej 	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
    189      1.15   thorpej 	sc->sc_if.if_dlt = DLT_NULL;
    190      1.20    itojun 	sc->sc_if.if_mtu = GREMTU;
    191      1.10   thorpej 	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
    192      1.10   thorpej 	sc->sc_if.if_output = gre_output;
    193      1.10   thorpej 	sc->sc_if.if_ioctl = gre_ioctl;
    194      1.10   thorpej 	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
    195  1.59.4.1    rpaulo 	sc->g_dstport = sc->g_srcport = 0;
    196      1.10   thorpej 	sc->g_proto = IPPROTO_GRE;
    197  1.59.4.1    rpaulo 	sc->sc_snd.ifq_maxlen = 256;
    198      1.35    itojun 	sc->sc_if.if_flags |= IFF_LINK0;
    199      1.10   thorpej 	if_attach(&sc->sc_if);
    200      1.16   thorpej 	if_alloc_sadl(&sc->sc_if);
    201       1.1       hwr #if NBPFILTER > 0
    202      1.14   thorpej 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
    203       1.1       hwr #endif
    204      1.10   thorpej 	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
    205      1.10   thorpej 	return (0);
    206      1.10   thorpej }
    207       1.1       hwr 
    208      1.59   thorpej static int
    209      1.59   thorpej gre_clone_destroy(struct ifnet *ifp)
    210      1.10   thorpej {
    211      1.10   thorpej 	struct gre_softc *sc = ifp->if_softc;
    212      1.10   thorpej 
    213      1.10   thorpej 	LIST_REMOVE(sc, sc_list);
    214      1.10   thorpej #if NBPFILTER > 0
    215      1.10   thorpej 	bpfdetach(ifp);
    216      1.10   thorpej #endif
    217      1.10   thorpej 	if_detach(ifp);
    218  1.59.4.1    rpaulo 	gre_wakeup(sc);
    219  1.59.4.1    rpaulo 	gre_join(&sc->sc_thread);
    220  1.59.4.1    rpaulo 	if (sc->sc_fp != NULL) {
    221  1.59.4.1    rpaulo 		closef(sc->sc_fp, curlwp);
    222  1.59.4.1    rpaulo 		sc->sc_fp = NULL;
    223  1.59.4.1    rpaulo 	}
    224      1.10   thorpej 	free(sc, M_DEVBUF);
    225      1.53     peter 
    226      1.53     peter 	return (0);
    227       1.1       hwr }
    228       1.1       hwr 
    229  1.59.4.1    rpaulo static void
    230  1.59.4.1    rpaulo gre_receive(struct socket *so, caddr_t arg, int waitflag)
    231  1.59.4.1    rpaulo {
    232  1.59.4.1    rpaulo 	struct gre_softc *sc = (struct gre_softc *)arg;
    233  1.59.4.1    rpaulo 
    234  1.59.4.1    rpaulo 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    235  1.59.4.1    rpaulo 
    236  1.59.4.1    rpaulo 	gre_wakeup(sc);
    237  1.59.4.1    rpaulo }
    238  1.59.4.1    rpaulo 
    239  1.59.4.1    rpaulo static void
    240  1.59.4.1    rpaulo gre_upcall_add(struct socket *so, caddr_t arg)
    241  1.59.4.1    rpaulo {
    242  1.59.4.1    rpaulo 	/* XXX What if the kernel already set an upcall? */
    243  1.59.4.1    rpaulo 	so->so_upcallarg = arg;
    244  1.59.4.1    rpaulo 	so->so_upcall = gre_receive;
    245  1.59.4.1    rpaulo 	so->so_rcv.sb_flags |= SB_UPCALL;
    246  1.59.4.1    rpaulo }
    247  1.59.4.1    rpaulo 
    248  1.59.4.1    rpaulo static void
    249  1.59.4.1    rpaulo gre_upcall_remove(struct socket *so)
    250  1.59.4.1    rpaulo {
    251  1.59.4.1    rpaulo 	/* XXX What if the kernel already set an upcall? */
    252  1.59.4.1    rpaulo 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    253  1.59.4.1    rpaulo 	so->so_upcallarg = NULL;
    254  1.59.4.1    rpaulo 	so->so_upcall = NULL;
    255  1.59.4.1    rpaulo }
    256  1.59.4.1    rpaulo 
    257  1.59.4.1    rpaulo static void
    258  1.59.4.1    rpaulo gre_sodestroy(struct socket **sop)
    259  1.59.4.1    rpaulo {
    260  1.59.4.1    rpaulo 	gre_upcall_remove(*sop);
    261  1.59.4.1    rpaulo 	soshutdown(*sop, SHUT_RDWR);
    262  1.59.4.1    rpaulo 	soclose(*sop);
    263  1.59.4.1    rpaulo 	*sop = NULL;
    264  1.59.4.1    rpaulo }
    265  1.59.4.1    rpaulo 
    266  1.59.4.1    rpaulo static struct mbuf *
    267  1.59.4.1    rpaulo gre_getsockmbuf(struct socket *so)
    268  1.59.4.1    rpaulo {
    269  1.59.4.1    rpaulo 	struct mbuf *m;
    270  1.59.4.1    rpaulo 
    271  1.59.4.1    rpaulo 	m = m_get(M_WAIT, MT_SONAME);
    272  1.59.4.1    rpaulo 	if (m != NULL)
    273  1.59.4.1    rpaulo 		MCLAIM(m, so->so_mowner);
    274  1.59.4.1    rpaulo 	return m;
    275  1.59.4.1    rpaulo }
    276  1.59.4.1    rpaulo 
    277  1.59.4.1    rpaulo static int
    278  1.59.4.1    rpaulo gre_socreate1(struct gre_softc *sc, struct lwp *l, struct gre_soparm *sp,
    279  1.59.4.1    rpaulo     struct socket **sop)
    280  1.59.4.1    rpaulo {
    281  1.59.4.1    rpaulo 	int rc;
    282  1.59.4.1    rpaulo 	struct mbuf *m;
    283  1.59.4.1    rpaulo 	struct sockaddr_in *sin;
    284  1.59.4.1    rpaulo 	struct socket *so;
    285  1.59.4.1    rpaulo 
    286  1.59.4.1    rpaulo 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    287  1.59.4.1    rpaulo 	rc = socreate(AF_INET, sop, SOCK_DGRAM, IPPROTO_UDP, l);
    288  1.59.4.1    rpaulo 	if (rc != 0) {
    289  1.59.4.1    rpaulo 		GRE_DPRINTF(sc, "%s: socreate failed\n", __func__);
    290  1.59.4.1    rpaulo 		return rc;
    291  1.59.4.1    rpaulo 	}
    292  1.59.4.1    rpaulo 
    293  1.59.4.1    rpaulo 	so = *sop;
    294  1.59.4.1    rpaulo 
    295  1.59.4.1    rpaulo 	gre_upcall_add(so, (caddr_t)sc);
    296  1.59.4.1    rpaulo 	if ((m = gre_getsockmbuf(so)) == NULL) {
    297  1.59.4.1    rpaulo 		rc = ENOBUFS;
    298  1.59.4.1    rpaulo 		goto out;
    299  1.59.4.1    rpaulo 	}
    300  1.59.4.1    rpaulo 	sin = mtod(m, struct sockaddr_in *);
    301  1.59.4.1    rpaulo 	sin->sin_len = m->m_len = sizeof(struct sockaddr_in);
    302  1.59.4.1    rpaulo 	sin->sin_family = AF_INET;
    303  1.59.4.1    rpaulo 	sin->sin_addr = sc->g_src;
    304  1.59.4.1    rpaulo 	sin->sin_port = sc->g_srcport;
    305  1.59.4.1    rpaulo 
    306  1.59.4.1    rpaulo 	GRE_DPRINTF(sc, "%s: bind 0x%08" PRIx32 " port %d\n", __func__,
    307  1.59.4.1    rpaulo 	    sin->sin_addr.s_addr, ntohs(sin->sin_port));
    308  1.59.4.1    rpaulo 	if ((rc = sobind(so, m, l)) != 0) {
    309  1.59.4.1    rpaulo 		GRE_DPRINTF(sc, "%s: sobind failed\n", __func__);
    310  1.59.4.1    rpaulo 		goto out;
    311  1.59.4.1    rpaulo 	}
    312  1.59.4.1    rpaulo 
    313  1.59.4.1    rpaulo 	if (sc->g_srcport == 0) {
    314  1.59.4.1    rpaulo 		if (gre_getsockname(so, m, l) != 0) {
    315  1.59.4.1    rpaulo 			GRE_DPRINTF(sc, "%s: gre_getsockname failed\n",
    316  1.59.4.1    rpaulo 			    __func__);
    317  1.59.4.1    rpaulo 			goto out;
    318  1.59.4.1    rpaulo 		}
    319  1.59.4.1    rpaulo 		sc->g_srcport = sin->sin_port;
    320  1.59.4.1    rpaulo 	}
    321  1.59.4.1    rpaulo 
    322  1.59.4.1    rpaulo 	sin->sin_addr = sc->g_dst;
    323  1.59.4.1    rpaulo 	sin->sin_port = sc->g_dstport;
    324  1.59.4.1    rpaulo 
    325  1.59.4.1    rpaulo 	rc = soconnect(so, m, l);
    326  1.59.4.1    rpaulo 
    327  1.59.4.1    rpaulo 	if (rc != 0) {
    328  1.59.4.1    rpaulo 		GRE_DPRINTF(sc, "%s: soconnect failed\n", __func__);
    329  1.59.4.1    rpaulo 		goto out;
    330  1.59.4.1    rpaulo 	}
    331  1.59.4.1    rpaulo 
    332  1.59.4.1    rpaulo 	*mtod(m, int *) = ip_gre_ttl;
    333  1.59.4.1    rpaulo 	rc = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, IPPROTO_IP, IP_TTL,
    334  1.59.4.1    rpaulo 	    &m);
    335  1.59.4.1    rpaulo 	m = NULL;
    336  1.59.4.1    rpaulo 	if (rc != 0) {
    337  1.59.4.1    rpaulo 		printf("%s: setopt ttl failed\n", __func__);
    338  1.59.4.1    rpaulo 		rc = 0;
    339  1.59.4.1    rpaulo 	}
    340  1.59.4.1    rpaulo out:
    341  1.59.4.1    rpaulo 	m_freem(m);
    342  1.59.4.1    rpaulo 
    343  1.59.4.1    rpaulo 	if (rc != 0)
    344  1.59.4.1    rpaulo 		gre_sodestroy(sop);
    345  1.59.4.1    rpaulo 	else
    346  1.59.4.1    rpaulo 		*sp = sc->sc_soparm;
    347  1.59.4.1    rpaulo 
    348  1.59.4.1    rpaulo 	return rc;
    349  1.59.4.1    rpaulo }
    350  1.59.4.1    rpaulo 
    351  1.59.4.1    rpaulo static void
    352  1.59.4.1    rpaulo gre_thread1(struct gre_softc *sc, struct lwp *l)
    353  1.59.4.1    rpaulo {
    354  1.59.4.1    rpaulo 	int flags, rc, s;
    355  1.59.4.1    rpaulo 	const struct gre_h *gh;
    356  1.59.4.1    rpaulo 	struct ifnet *ifp = &sc->sc_if;
    357  1.59.4.1    rpaulo 	struct mbuf *m;
    358  1.59.4.1    rpaulo 	struct socket *so = NULL;
    359  1.59.4.1    rpaulo 	struct uio uio;
    360  1.59.4.1    rpaulo 	struct gre_soparm sp;
    361  1.59.4.1    rpaulo 
    362  1.59.4.1    rpaulo 	GRE_DPRINTF(sc, "%s: enter\n", __func__);
    363  1.59.4.1    rpaulo 	s = splnet();
    364  1.59.4.1    rpaulo 
    365  1.59.4.1    rpaulo 	sc->sc_waitchan = 1;
    366  1.59.4.1    rpaulo 
    367  1.59.4.1    rpaulo 	memset(&sp, 0, sizeof(sp));
    368  1.59.4.1    rpaulo 	memset(&uio, 0, sizeof(uio));
    369  1.59.4.1    rpaulo 
    370  1.59.4.1    rpaulo 	ifp->if_flags |= IFF_RUNNING;
    371  1.59.4.1    rpaulo 
    372  1.59.4.1    rpaulo 	for (;;) {
    373  1.59.4.1    rpaulo 		while (sc->sc_waitchan == 0) {
    374  1.59.4.1    rpaulo 			splx(s);
    375  1.59.4.1    rpaulo 			GRE_DPRINTF(sc, "%s: sleeping\n", __func__);
    376  1.59.4.1    rpaulo 			tsleep(&sc->sc_waitchan, PSOCK, "grewait", 0);
    377  1.59.4.1    rpaulo 			s = splnet();
    378  1.59.4.1    rpaulo 		}
    379  1.59.4.1    rpaulo 		sc->sc_waitchan = 0;
    380  1.59.4.1    rpaulo 		GRE_DPRINTF(sc, "%s: awake\n", __func__);
    381  1.59.4.1    rpaulo 		if ((ifp->if_flags & IFF_UP) != IFF_UP) {
    382  1.59.4.1    rpaulo 			GRE_DPRINTF(sc, "%s: not up & running; exiting\n",
    383  1.59.4.1    rpaulo 			    __func__);
    384  1.59.4.1    rpaulo 			break;
    385  1.59.4.1    rpaulo 		}
    386  1.59.4.1    rpaulo 		if (sc->g_proto != IPPROTO_UDP) {
    387  1.59.4.1    rpaulo 			GRE_DPRINTF(sc, "%s: not udp; exiting\n", __func__);
    388  1.59.4.1    rpaulo 			break;
    389  1.59.4.1    rpaulo 		}
    390  1.59.4.1    rpaulo 		/* XXX optimize */
    391  1.59.4.1    rpaulo 		if (memcmp(&sp, &sc->sc_soparm, sizeof(sp)) != 0) {
    392  1.59.4.1    rpaulo 			GRE_DPRINTF(sc, "%s: parameters changed\n", __func__);
    393  1.59.4.1    rpaulo 
    394  1.59.4.1    rpaulo 			if (sp.sp_fp != NULL) {
    395  1.59.4.1    rpaulo 				FILE_UNUSE(sp.sp_fp, NULL);
    396  1.59.4.1    rpaulo 				sp.sp_fp = NULL;
    397  1.59.4.1    rpaulo 				so = NULL;
    398  1.59.4.1    rpaulo 			} else if (so != NULL)
    399  1.59.4.1    rpaulo 				gre_sodestroy(&so);
    400  1.59.4.1    rpaulo 
    401  1.59.4.1    rpaulo 			if (sc->sc_fp != NULL) {
    402  1.59.4.1    rpaulo 				so = (struct socket *)sc->sc_fp->f_data;
    403  1.59.4.1    rpaulo 				gre_upcall_add(so, (caddr_t)sc);
    404  1.59.4.1    rpaulo 				sp = sc->sc_soparm;
    405  1.59.4.1    rpaulo 				FILE_USE(sp.sp_fp);
    406  1.59.4.1    rpaulo 			} else if (gre_socreate1(sc, l, &sp, &so) != 0)
    407  1.59.4.1    rpaulo 				goto out;
    408  1.59.4.1    rpaulo 		}
    409  1.59.4.1    rpaulo 		for (;;) {
    410  1.59.4.1    rpaulo 			flags = MSG_DONTWAIT;
    411  1.59.4.1    rpaulo 			uio.uio_resid = 1000000;
    412  1.59.4.1    rpaulo 			rc = (*so->so_receive)(so, NULL, &uio, &m, NULL,
    413  1.59.4.1    rpaulo 			    &flags);
    414  1.59.4.1    rpaulo 			/* TBD Back off if ECONNREFUSED (indicates
    415  1.59.4.1    rpaulo 			 * ICMP Port Unreachable)?
    416  1.59.4.1    rpaulo 			 */
    417  1.59.4.1    rpaulo 			if (rc == EWOULDBLOCK) {
    418  1.59.4.1    rpaulo 				GRE_DPRINTF(sc, "%s: so_receive EWOULDBLOCK\n",
    419  1.59.4.1    rpaulo 				    __func__);
    420  1.59.4.1    rpaulo 				break;
    421  1.59.4.1    rpaulo 			} else if (rc != 0 || m == NULL) {
    422  1.59.4.1    rpaulo 				GRE_DPRINTF(sc, "%s: rc %d m %p\n",
    423  1.59.4.1    rpaulo 				    ifp->if_xname, rc, (void *)m);
    424  1.59.4.1    rpaulo 				continue;
    425  1.59.4.1    rpaulo 			} else
    426  1.59.4.1    rpaulo 				GRE_DPRINTF(sc, "%s: so_receive ok\n",
    427  1.59.4.1    rpaulo 				    __func__);
    428  1.59.4.1    rpaulo 			if (m->m_len < sizeof(*gh) &&
    429  1.59.4.1    rpaulo 			    (m = m_pullup(m, sizeof(*gh))) == NULL) {
    430  1.59.4.1    rpaulo 				GRE_DPRINTF(sc, "%s: m_pullup failed\n",
    431  1.59.4.1    rpaulo 				    __func__);
    432  1.59.4.1    rpaulo 				continue;
    433  1.59.4.1    rpaulo 			}
    434  1.59.4.1    rpaulo 			gh = mtod(m, const struct gre_h *);
    435  1.59.4.1    rpaulo 
    436  1.59.4.1    rpaulo 			if (gre_input3(sc, m, 0, IPPROTO_GRE, gh) == 0) {
    437  1.59.4.1    rpaulo 				GRE_DPRINTF(sc, "%s: dropping unsupported\n",
    438  1.59.4.1    rpaulo 				    __func__);
    439  1.59.4.1    rpaulo 				ifp->if_ierrors++;
    440  1.59.4.1    rpaulo 				m_freem(m);
    441  1.59.4.1    rpaulo 			}
    442  1.59.4.1    rpaulo 		}
    443  1.59.4.1    rpaulo 		for (;;) {
    444  1.59.4.1    rpaulo 			IF_DEQUEUE(&sc->sc_snd, m);
    445  1.59.4.1    rpaulo 			if (m == NULL)
    446  1.59.4.1    rpaulo 				break;
    447  1.59.4.1    rpaulo 			GRE_DPRINTF(sc, "%s: dequeue\n", __func__);
    448  1.59.4.1    rpaulo 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    449  1.59.4.1    rpaulo 				GRE_DPRINTF(sc, "%s: not connected\n",
    450  1.59.4.1    rpaulo 				    __func__);
    451  1.59.4.1    rpaulo 				m_freem(m);
    452  1.59.4.1    rpaulo 				continue;
    453  1.59.4.1    rpaulo 			}
    454  1.59.4.1    rpaulo 			rc = (*so->so_send)(so, NULL, NULL, m, NULL, 0, l);
    455  1.59.4.1    rpaulo 			/* XXX handle ENOBUFS? */
    456  1.59.4.1    rpaulo 			if (rc != 0)
    457  1.59.4.1    rpaulo 				GRE_DPRINTF(sc, "%s: so_send failed\n",
    458  1.59.4.1    rpaulo 				    __func__);
    459  1.59.4.1    rpaulo 		}
    460  1.59.4.1    rpaulo 		/* Give the software interrupt queues a chance to
    461  1.59.4.1    rpaulo 		 * run, or else when I send a ping from gre0 to gre1 on
    462  1.59.4.1    rpaulo 		 * the same host, gre0 will not wake for the reply.
    463  1.59.4.1    rpaulo 		 */
    464  1.59.4.1    rpaulo 		splx(s);
    465  1.59.4.1    rpaulo 		s = splnet();
    466  1.59.4.1    rpaulo 	}
    467  1.59.4.1    rpaulo 	if (sp.sp_fp != NULL) {
    468  1.59.4.1    rpaulo 		GRE_DPRINTF(sc, "%s: removing upcall\n", __func__);
    469  1.59.4.1    rpaulo 		gre_upcall_remove(so);
    470  1.59.4.1    rpaulo 		FILE_UNUSE(sp.sp_fp, NULL);
    471  1.59.4.1    rpaulo 		sp.sp_fp = NULL;
    472  1.59.4.1    rpaulo 	} else
    473  1.59.4.1    rpaulo 		gre_sodestroy(&so);
    474  1.59.4.1    rpaulo out:
    475  1.59.4.1    rpaulo 	GRE_DPRINTF(sc, "%s: stopping\n", __func__);
    476  1.59.4.1    rpaulo 	if (sc->g_proto == IPPROTO_UDP)
    477  1.59.4.1    rpaulo 		ifp->if_flags &= ~IFF_RUNNING;
    478  1.59.4.1    rpaulo 	while (!IF_IS_EMPTY(&sc->sc_snd)) {
    479  1.59.4.1    rpaulo 		IF_DEQUEUE(&sc->sc_snd, m);
    480  1.59.4.1    rpaulo 		m_freem(m);
    481  1.59.4.1    rpaulo 	}
    482  1.59.4.1    rpaulo 	gre_stop(&sc->sc_thread);
    483  1.59.4.1    rpaulo 	/* must not touch sc after this! */
    484  1.59.4.1    rpaulo 	GRE_DPRINTF(sc, "%s: restore ipl\n", __func__);
    485  1.59.4.1    rpaulo 	splx(s);
    486  1.59.4.1    rpaulo }
    487  1.59.4.1    rpaulo 
    488  1.59.4.1    rpaulo static void
    489  1.59.4.1    rpaulo gre_thread(void *arg)
    490  1.59.4.1    rpaulo {
    491  1.59.4.1    rpaulo 	struct gre_softc *sc = (struct gre_softc *)arg;
    492  1.59.4.1    rpaulo 
    493  1.59.4.1    rpaulo 	gre_thread1(sc, curlwp);
    494  1.59.4.1    rpaulo 	/* must not touch sc after this! */
    495  1.59.4.1    rpaulo 	kthread_exit(0);
    496  1.59.4.1    rpaulo }
    497  1.59.4.1    rpaulo 
    498  1.59.4.1    rpaulo int
    499  1.59.4.1    rpaulo gre_input3(struct gre_softc *sc, struct mbuf *m, int hlen, u_char proto,
    500  1.59.4.1    rpaulo     const struct gre_h *gh)
    501  1.59.4.1    rpaulo {
    502  1.59.4.1    rpaulo 	u_int16_t flags;
    503  1.59.4.1    rpaulo #if NBPFILTER > 0
    504  1.59.4.1    rpaulo 	u_int32_t af = AF_INET;		/* af passed to BPF tap */
    505  1.59.4.1    rpaulo #endif
    506  1.59.4.1    rpaulo 	int s, isr;
    507  1.59.4.1    rpaulo 	struct ifqueue *ifq;
    508  1.59.4.1    rpaulo 
    509  1.59.4.1    rpaulo 	sc->sc_if.if_ipackets++;
    510  1.59.4.1    rpaulo 	sc->sc_if.if_ibytes += m->m_pkthdr.len;
    511  1.59.4.1    rpaulo 
    512  1.59.4.1    rpaulo 	switch (proto) {
    513  1.59.4.1    rpaulo 	case IPPROTO_GRE:
    514  1.59.4.1    rpaulo 		hlen += sizeof(struct gre_h);
    515  1.59.4.1    rpaulo 
    516  1.59.4.1    rpaulo 		/* process GRE flags as packet can be of variable len */
    517  1.59.4.1    rpaulo 		flags = ntohs(gh->flags);
    518  1.59.4.1    rpaulo 
    519  1.59.4.1    rpaulo 		/* Checksum & Offset are present */
    520  1.59.4.1    rpaulo 		if ((flags & GRE_CP) | (flags & GRE_RP))
    521  1.59.4.1    rpaulo 			hlen += 4;
    522  1.59.4.1    rpaulo 		/* We don't support routing fields (variable length) */
    523  1.59.4.1    rpaulo 		if (flags & GRE_RP)
    524  1.59.4.1    rpaulo 			return (0);
    525  1.59.4.1    rpaulo 		if (flags & GRE_KP)
    526  1.59.4.1    rpaulo 			hlen += 4;
    527  1.59.4.1    rpaulo 		if (flags & GRE_SP)
    528  1.59.4.1    rpaulo 			hlen += 4;
    529  1.59.4.1    rpaulo 
    530  1.59.4.1    rpaulo 		switch (ntohs(gh->ptype)) { /* ethertypes */
    531  1.59.4.1    rpaulo 		case ETHERTYPE_IP: /* shouldn't need a schednetisr(), as */
    532  1.59.4.1    rpaulo 			ifq = &ipintrq;          /* we are in ip_input */
    533  1.59.4.1    rpaulo 			isr = NETISR_IP;
    534  1.59.4.1    rpaulo 			break;
    535  1.59.4.1    rpaulo #ifdef NETATALK
    536  1.59.4.1    rpaulo 		case ETHERTYPE_ATALK:
    537  1.59.4.1    rpaulo 			ifq = &atintrq1;
    538  1.59.4.1    rpaulo 			isr = NETISR_ATALK;
    539  1.59.4.1    rpaulo #if NBPFILTER > 0
    540  1.59.4.1    rpaulo 			af = AF_APPLETALK;
    541  1.59.4.1    rpaulo #endif
    542  1.59.4.1    rpaulo 			break;
    543  1.59.4.1    rpaulo #endif
    544  1.59.4.1    rpaulo #ifdef INET6
    545  1.59.4.1    rpaulo 		case ETHERTYPE_IPV6:
    546  1.59.4.1    rpaulo 			GRE_DPRINTF(sc, "%s: IPv6 packet\n", __func__);
    547  1.59.4.1    rpaulo 			ifq = &ip6intrq;
    548  1.59.4.1    rpaulo 			isr = NETISR_IPV6;
    549  1.59.4.1    rpaulo #if NBPFILTER > 0
    550  1.59.4.1    rpaulo 			af = AF_INET6;
    551  1.59.4.1    rpaulo #endif
    552  1.59.4.1    rpaulo 			break;
    553  1.59.4.1    rpaulo #endif
    554  1.59.4.1    rpaulo 		default:	   /* others not yet supported */
    555  1.59.4.1    rpaulo 			printf("%s: unhandled ethertype 0x%04x\n", __func__,
    556  1.59.4.1    rpaulo 			    ntohs(gh->ptype));
    557  1.59.4.1    rpaulo 			return (0);
    558  1.59.4.1    rpaulo 		}
    559  1.59.4.1    rpaulo 		break;
    560  1.59.4.1    rpaulo 	default:
    561  1.59.4.1    rpaulo 		/* others not yet supported */
    562  1.59.4.1    rpaulo 		return (0);
    563  1.59.4.1    rpaulo 	}
    564  1.59.4.1    rpaulo 
    565  1.59.4.1    rpaulo 	if (hlen > m->m_pkthdr.len) {
    566  1.59.4.1    rpaulo 		m_freem(m);
    567  1.59.4.1    rpaulo 		sc->sc_if.if_ierrors++;
    568  1.59.4.1    rpaulo 		return (EINVAL);
    569  1.59.4.1    rpaulo 	}
    570  1.59.4.1    rpaulo 	m_adj(m, hlen);
    571  1.59.4.1    rpaulo 
    572  1.59.4.1    rpaulo #if NBPFILTER > 0
    573  1.59.4.1    rpaulo 	if (sc->sc_if.if_bpf != NULL)
    574  1.59.4.1    rpaulo 		bpf_mtap_af(sc->sc_if.if_bpf, af, m);
    575  1.59.4.1    rpaulo #endif /*NBPFILTER > 0*/
    576  1.59.4.1    rpaulo 
    577  1.59.4.1    rpaulo 	m->m_pkthdr.rcvif = &sc->sc_if;
    578  1.59.4.1    rpaulo 
    579  1.59.4.1    rpaulo 	s = splnet();		/* possible */
    580  1.59.4.1    rpaulo 	if (IF_QFULL(ifq)) {
    581  1.59.4.1    rpaulo 		IF_DROP(ifq);
    582  1.59.4.1    rpaulo 		m_freem(m);
    583  1.59.4.1    rpaulo 	} else {
    584  1.59.4.1    rpaulo 		IF_ENQUEUE(ifq, m);
    585  1.59.4.1    rpaulo 	}
    586  1.59.4.1    rpaulo 	/* we need schednetisr since the address family may change */
    587  1.59.4.1    rpaulo 	schednetisr(isr);
    588  1.59.4.1    rpaulo 	splx(s);
    589  1.59.4.1    rpaulo 
    590  1.59.4.1    rpaulo 	return (1);	/* packet is done, no further processing needed */
    591  1.59.4.1    rpaulo }
    592  1.59.4.1    rpaulo 
    593      1.20    itojun /*
    594       1.1       hwr  * The output routine. Takes a packet and encapsulates it in the protocol
    595       1.6       hwr  * given by sc->g_proto. See also RFC 1701 and RFC 2004
    596       1.1       hwr  */
    597      1.59   thorpej static int
    598       1.8  explorer gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
    599       1.8  explorer 	   struct rtentry *rt)
    600       1.1       hwr {
    601  1.59.4.1    rpaulo 	int error = 0, hlen;
    602      1.10   thorpej 	struct gre_softc *sc = ifp->if_softc;
    603  1.59.4.1    rpaulo 	struct greip *gi;
    604  1.59.4.1    rpaulo 	struct gre_h *gh;
    605  1.59.4.1    rpaulo 	struct ip *eip, *ip;
    606      1.56        is 	u_int8_t ip_tos = 0;
    607      1.48    itojun 	u_int16_t etype = 0;
    608      1.20    itojun 	struct mobile_h mob_h;
    609      1.24    martin 
    610      1.39    itojun 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
    611      1.37    itojun 	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
    612      1.37    itojun 		m_freem(m);
    613      1.38    itojun 		error = ENETDOWN;
    614      1.38    itojun 		goto end;
    615      1.37    itojun 	}
    616       1.1       hwr 
    617  1.59.4.1    rpaulo 	gi = NULL;
    618      1.41    itojun 	ip = NULL;
    619       1.1       hwr 
    620       1.1       hwr #if NBPFILTER >0
    621      1.52  christos 	if (ifp->if_bpf)
    622      1.52  christos 		bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
    623       1.1       hwr #endif
    624       1.1       hwr 
    625      1.26    martin 	m->m_flags &= ~(M_BCAST|M_MCAST);
    626       1.1       hwr 
    627  1.59.4.1    rpaulo 	switch (sc->g_proto) {
    628  1.59.4.1    rpaulo 	case IPPROTO_MOBILE:
    629       1.3       hwr 		if (dst->sa_family == AF_INET) {
    630       1.3       hwr 			int msiz;
    631       1.3       hwr 
    632  1.59.4.1    rpaulo 			if (M_UNWRITABLE(m, sizeof(*ip)) &&
    633  1.59.4.1    rpaulo 			    (m = m_pullup(m, sizeof(*ip))) == NULL) {
    634  1.59.4.1    rpaulo 				error = ENOBUFS;
    635  1.59.4.1    rpaulo 				goto end;
    636  1.59.4.1    rpaulo 			}
    637      1.41    itojun 			ip = mtod(m, struct ip *);
    638       1.3       hwr 
    639       1.8  explorer 			memset(&mob_h, 0, MOB_H_SIZ_L);
    640      1.41    itojun 			mob_h.proto = (ip->ip_p) << 8;
    641      1.41    itojun 			mob_h.odst = ip->ip_dst.s_addr;
    642      1.41    itojun 			ip->ip_dst.s_addr = sc->g_dst.s_addr;
    643       1.3       hwr 
    644       1.3       hwr 			/*
    645       1.8  explorer 			 * If the packet comes from our host, we only change
    646       1.8  explorer 			 * the destination address in the IP header.
    647       1.8  explorer 			 * Else we also need to save and change the source
    648       1.3       hwr 			 */
    649      1.41    itojun 			if (in_hosteq(ip->ip_src, sc->g_src)) {
    650       1.8  explorer 				msiz = MOB_H_SIZ_S;
    651       1.3       hwr 			} else {
    652       1.3       hwr 				mob_h.proto |= MOB_H_SBIT;
    653      1.41    itojun 				mob_h.osrc = ip->ip_src.s_addr;
    654      1.41    itojun 				ip->ip_src.s_addr = sc->g_src.s_addr;
    655       1.8  explorer 				msiz = MOB_H_SIZ_L;
    656       1.3       hwr 			}
    657       1.3       hwr 			HTONS(mob_h.proto);
    658      1.48    itojun 			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
    659       1.3       hwr 
    660  1.59.4.1    rpaulo 			M_PREPEND(m, msiz, M_DONTWAIT);
    661  1.59.4.1    rpaulo 			if (m == NULL) {
    662  1.59.4.1    rpaulo 				error = ENOBUFS;
    663  1.59.4.1    rpaulo 				goto end;
    664       1.3       hwr 			}
    665  1.59.4.1    rpaulo 			/* XXX Assuming that ip does not dangle after
    666  1.59.4.1    rpaulo 			 * M_PREPEND.  In practice, that's true, but
    667  1.59.4.1    rpaulo 			 * that's in M_PREPEND's contract.
    668  1.59.4.1    rpaulo 			 */
    669  1.59.4.1    rpaulo 			memmove(mtod(m, caddr_t), ip, sizeof(*ip));
    670      1.41    itojun 			ip = mtod(m, struct ip *);
    671      1.41    itojun 			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
    672      1.42    itojun 			ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
    673       1.3       hwr 		} else {  /* AF_INET */
    674       1.3       hwr 			IF_DROP(&ifp->if_snd);
    675       1.3       hwr 			m_freem(m);
    676      1.38    itojun 			error = EINVAL;
    677      1.38    itojun 			goto end;
    678       1.3       hwr 		}
    679  1.59.4.1    rpaulo 		break;
    680  1.59.4.1    rpaulo 	case IPPROTO_UDP:
    681  1.59.4.1    rpaulo 	case IPPROTO_GRE:
    682  1.59.4.1    rpaulo 		GRE_DPRINTF(sc, "%s: dst->sa_family=%d\n", __func__,
    683  1.59.4.1    rpaulo 		    dst->sa_family);
    684      1.20    itojun 		switch (dst->sa_family) {
    685       1.1       hwr 		case AF_INET:
    686      1.41    itojun 			ip = mtod(m, struct ip *);
    687      1.56        is 			ip_tos = ip->ip_tos;
    688       1.8  explorer 			etype = ETHERTYPE_IP;
    689       1.1       hwr 			break;
    690       1.1       hwr #ifdef NETATALK
    691       1.1       hwr 		case AF_APPLETALK:
    692       1.8  explorer 			etype = ETHERTYPE_ATALK;
    693       1.1       hwr 			break;
    694       1.1       hwr #endif
    695      1.56        is #ifdef INET6
    696      1.56        is 		case AF_INET6:
    697      1.56        is 			etype = ETHERTYPE_IPV6;
    698      1.56        is 			break;
    699      1.56        is #endif
    700       1.1       hwr 		default:
    701       1.1       hwr 			IF_DROP(&ifp->if_snd);
    702       1.1       hwr 			m_freem(m);
    703      1.38    itojun 			error = EAFNOSUPPORT;
    704      1.38    itojun 			goto end;
    705       1.1       hwr 		}
    706  1.59.4.1    rpaulo 		break;
    707  1.59.4.1    rpaulo 	default:
    708       1.1       hwr 		IF_DROP(&ifp->if_snd);
    709       1.1       hwr 		m_freem(m);
    710      1.38    itojun 		error = EINVAL;
    711      1.38    itojun 		goto end;
    712       1.1       hwr 	}
    713       1.1       hwr 
    714  1.59.4.1    rpaulo 	switch (sc->g_proto) {
    715  1.59.4.1    rpaulo 	case IPPROTO_GRE:
    716  1.59.4.1    rpaulo 		hlen = sizeof(struct greip);
    717  1.59.4.1    rpaulo 		break;
    718  1.59.4.1    rpaulo 	case IPPROTO_UDP:
    719  1.59.4.1    rpaulo 		hlen = sizeof(struct gre_h);
    720  1.59.4.1    rpaulo 		break;
    721  1.59.4.1    rpaulo 	default:
    722  1.59.4.1    rpaulo 		hlen = 0;
    723  1.59.4.1    rpaulo 		break;
    724  1.59.4.1    rpaulo 	}
    725  1.59.4.1    rpaulo 
    726  1.59.4.1    rpaulo 	M_PREPEND(m, hlen, M_DONTWAIT);
    727  1.59.4.1    rpaulo 
    728  1.59.4.1    rpaulo 	if (m == NULL) {
    729       1.1       hwr 		IF_DROP(&ifp->if_snd);
    730      1.38    itojun 		error = ENOBUFS;
    731      1.38    itojun 		goto end;
    732       1.1       hwr 	}
    733       1.1       hwr 
    734  1.59.4.1    rpaulo 	switch (sc->g_proto) {
    735  1.59.4.1    rpaulo 	case IPPROTO_UDP:
    736  1.59.4.1    rpaulo 		gh = mtod(m, struct gre_h *);
    737  1.59.4.1    rpaulo 		memset(gh, 0, sizeof(*gh));
    738  1.59.4.1    rpaulo 		gh->ptype = htons(etype);
    739  1.59.4.1    rpaulo 		/* XXX Need to handle IP ToS.  Look at how I handle IP TTL. */
    740  1.59.4.1    rpaulo 		break;
    741  1.59.4.1    rpaulo 	case IPPROTO_GRE:
    742  1.59.4.1    rpaulo 		gi = mtod(m, struct greip *);
    743  1.59.4.1    rpaulo 		gh = &gi->gi_g;
    744  1.59.4.1    rpaulo 		eip = &gi->gi_i;
    745       1.1       hwr 		/* we don't have any GRE flags for now */
    746  1.59.4.1    rpaulo 		memset(gh, 0, sizeof(*gh));
    747  1.59.4.1    rpaulo 		gh->ptype = htons(etype);
    748  1.59.4.1    rpaulo 		eip->ip_src = sc->g_src;
    749  1.59.4.1    rpaulo 		eip->ip_dst = sc->g_dst;
    750  1.59.4.1    rpaulo 		eip->ip_hl = (sizeof(struct ip)) >> 2;
    751  1.59.4.1    rpaulo 		eip->ip_ttl = ip_gre_ttl;
    752  1.59.4.1    rpaulo 		eip->ip_tos = ip_tos;
    753  1.59.4.1    rpaulo 		eip->ip_len = htons(m->m_pkthdr.len);
    754  1.59.4.1    rpaulo 		eip->ip_p = sc->g_proto;
    755  1.59.4.1    rpaulo 		break;
    756  1.59.4.1    rpaulo 	case IPPROTO_MOBILE:
    757  1.59.4.1    rpaulo 		eip = mtod(m, struct ip *);
    758  1.59.4.1    rpaulo 		eip->ip_p = sc->g_proto;
    759  1.59.4.1    rpaulo 		break;
    760  1.59.4.1    rpaulo 	default:
    761  1.59.4.1    rpaulo 		error = EPROTONOSUPPORT;
    762  1.59.4.1    rpaulo 		m_freem(m);
    763  1.59.4.1    rpaulo 		goto end;
    764       1.3       hwr 	}
    765       1.1       hwr 
    766       1.1       hwr 	ifp->if_opackets++;
    767       1.8  explorer 	ifp->if_obytes += m->m_pkthdr.len;
    768  1.59.4.1    rpaulo 
    769       1.1       hwr 	/* send it off */
    770  1.59.4.1    rpaulo 	if (sc->g_proto == IPPROTO_UDP) {
    771  1.59.4.1    rpaulo 		if (IF_QFULL(&sc->sc_snd)) {
    772  1.59.4.1    rpaulo 			IF_DROP(&sc->sc_snd);
    773  1.59.4.1    rpaulo 			error = ENOBUFS;
    774  1.59.4.1    rpaulo 			m_freem(m);
    775  1.59.4.1    rpaulo 		} else {
    776  1.59.4.1    rpaulo 			IF_ENQUEUE(&sc->sc_snd, m);
    777  1.59.4.1    rpaulo 			gre_wakeup(sc);
    778  1.59.4.1    rpaulo 			error = 0;
    779  1.59.4.1    rpaulo 		}
    780  1.59.4.1    rpaulo 	} else {
    781  1.59.4.1    rpaulo 		error = ip_output(m, NULL, &sc->route, 0,
    782  1.59.4.1    rpaulo 		    (struct ip_moptions *)NULL, (struct socket *)NULL);
    783  1.59.4.1    rpaulo 	}
    784      1.38    itojun   end:
    785       1.8  explorer 	if (error)
    786       1.1       hwr 		ifp->if_oerrors++;
    787       1.8  explorer 	return (error);
    788       1.1       hwr }
    789       1.1       hwr 
    790  1.59.4.1    rpaulo /* Must be called at IPL_NET. */
    791  1.59.4.1    rpaulo static int
    792  1.59.4.1    rpaulo gre_kick(struct gre_softc *sc)
    793  1.59.4.1    rpaulo {
    794  1.59.4.1    rpaulo 	int rc;
    795  1.59.4.1    rpaulo 	struct ifnet *ifp = &sc->sc_if;
    796  1.59.4.1    rpaulo 
    797  1.59.4.1    rpaulo 	if (sc->g_proto == IPPROTO_UDP && (ifp->if_flags & IFF_UP) == IFF_UP &&
    798  1.59.4.1    rpaulo 	    !sc->sc_thread) {
    799  1.59.4.1    rpaulo 		sc->sc_thread = 1;
    800  1.59.4.1    rpaulo 		rc = kthread_create1(gre_thread, (void *)sc, NULL,
    801  1.59.4.1    rpaulo 		    ifp->if_xname);
    802  1.59.4.1    rpaulo 		if (rc != 0)
    803  1.59.4.1    rpaulo 			gre_stop(&sc->sc_thread);
    804  1.59.4.1    rpaulo 		return rc;
    805  1.59.4.1    rpaulo 	} else {
    806  1.59.4.1    rpaulo 		gre_wakeup(sc);
    807  1.59.4.1    rpaulo 		return 0;
    808  1.59.4.1    rpaulo 	}
    809  1.59.4.1    rpaulo }
    810  1.59.4.1    rpaulo 
    811  1.59.4.1    rpaulo static int
    812  1.59.4.1    rpaulo gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
    813  1.59.4.1    rpaulo {
    814  1.59.4.1    rpaulo 	int s, error;
    815  1.59.4.1    rpaulo 
    816  1.59.4.1    rpaulo 	s = splsoftnet();
    817  1.59.4.1    rpaulo 	error = (*so->so_proto->pr_usrreq)(so, req, (struct mbuf *)0,
    818  1.59.4.1    rpaulo 	    nam, (struct mbuf *)0, l);
    819  1.59.4.1    rpaulo 	splx(s);
    820  1.59.4.1    rpaulo 	return error;
    821  1.59.4.1    rpaulo }
    822  1.59.4.1    rpaulo 
    823  1.59.4.1    rpaulo static int
    824  1.59.4.1    rpaulo gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
    825  1.59.4.1    rpaulo {
    826  1.59.4.1    rpaulo 	return gre_getname(so, PRU_SOCKADDR, nam, l);
    827  1.59.4.1    rpaulo }
    828  1.59.4.1    rpaulo 
    829  1.59.4.1    rpaulo static int
    830  1.59.4.1    rpaulo gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
    831  1.59.4.1    rpaulo {
    832  1.59.4.1    rpaulo 	return gre_getname(so, PRU_PEERADDR, nam, l);
    833  1.59.4.1    rpaulo }
    834  1.59.4.1    rpaulo 
    835  1.59.4.1    rpaulo static int
    836  1.59.4.1    rpaulo gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_in *src,
    837  1.59.4.1    rpaulo     struct sockaddr_in *dst)
    838  1.59.4.1    rpaulo {
    839  1.59.4.1    rpaulo 	struct mbuf *m;
    840  1.59.4.1    rpaulo 	struct sockaddr_in *sin;
    841  1.59.4.1    rpaulo 	int rc;
    842  1.59.4.1    rpaulo 
    843  1.59.4.1    rpaulo 	if ((m = gre_getsockmbuf(so)) == NULL)
    844  1.59.4.1    rpaulo 		return ENOBUFS;
    845  1.59.4.1    rpaulo 
    846  1.59.4.1    rpaulo 	sin = mtod(m, struct sockaddr_in *);
    847  1.59.4.1    rpaulo 
    848  1.59.4.1    rpaulo 	if ((rc = gre_getsockname(so, m, l)) != 0)
    849  1.59.4.1    rpaulo 		goto out;
    850  1.59.4.1    rpaulo 	if (sin->sin_family != AF_INET) {
    851  1.59.4.1    rpaulo 		rc = EAFNOSUPPORT;
    852  1.59.4.1    rpaulo 		goto out;
    853  1.59.4.1    rpaulo 	}
    854  1.59.4.1    rpaulo 	*src = *sin;
    855  1.59.4.1    rpaulo 
    856  1.59.4.1    rpaulo 	if ((rc = gre_getpeername(so, m, l)) != 0)
    857  1.59.4.1    rpaulo 		goto out;
    858  1.59.4.1    rpaulo 	if (sin->sin_family != AF_INET) {
    859  1.59.4.1    rpaulo 		rc = EAFNOSUPPORT;
    860  1.59.4.1    rpaulo 		goto out;
    861  1.59.4.1    rpaulo 	}
    862  1.59.4.1    rpaulo 	*dst = *sin;
    863  1.59.4.1    rpaulo 
    864  1.59.4.1    rpaulo out:
    865  1.59.4.1    rpaulo 	m_freem(m);
    866  1.59.4.1    rpaulo 	return rc;
    867  1.59.4.1    rpaulo }
    868  1.59.4.1    rpaulo 
    869      1.59   thorpej static int
    870       1.8  explorer gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    871       1.1       hwr {
    872  1.59.4.1    rpaulo 	u_char oproto;
    873  1.59.4.1    rpaulo 	struct file *fp, *ofp;
    874  1.59.4.1    rpaulo 	struct socket *so;
    875  1.59.4.1    rpaulo 	struct sockaddr_in dst, src;
    876      1.13    martin 	struct proc *p = curproc;	/* XXX */
    877  1.59.4.1    rpaulo 	struct lwp *l = curlwp;	/* XXX */
    878       1.8  explorer 	struct ifreq *ifr = (struct ifreq *)data;
    879      1.28    itojun 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
    880       1.8  explorer 	struct gre_softc *sc = ifp->if_softc;
    881       1.1       hwr 	int s;
    882       1.1       hwr 	struct sockaddr_in si;
    883       1.8  explorer 	struct sockaddr *sa = NULL;
    884       1.1       hwr 	int error;
    885       1.1       hwr 
    886  1.59.4.1    rpaulo 	switch (cmd) {
    887  1.59.4.1    rpaulo 	case SIOCSIFFLAGS:
    888  1.59.4.1    rpaulo 	case SIOCSIFMTU:
    889  1.59.4.1    rpaulo 	case GRESPROTO:
    890  1.59.4.1    rpaulo 	case GRESADDRD:
    891  1.59.4.1    rpaulo 	case GRESADDRS:
    892  1.59.4.1    rpaulo 	case GRESSOCK:
    893  1.59.4.1    rpaulo 	case GREDSOCK:
    894  1.59.4.1    rpaulo 	case SIOCSLIFPHYADDR:
    895  1.59.4.1    rpaulo 	case SIOCDIFPHYADDR:
    896  1.59.4.1    rpaulo 		if ((error = kauth_authorize_generic(l->l_cred,
    897  1.59.4.1    rpaulo 		    KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
    898  1.59.4.1    rpaulo 			return (error);
    899  1.59.4.1    rpaulo 		break;
    900  1.59.4.1    rpaulo 	default:
    901  1.59.4.1    rpaulo 		error = 0;
    902  1.59.4.1    rpaulo 		break;
    903  1.59.4.1    rpaulo 	}
    904       1.1       hwr 
    905      1.18   thorpej 	s = splnet();
    906      1.20    itojun 	switch (cmd) {
    907      1.20    itojun 	case SIOCSIFADDR:
    908      1.36    itojun 		ifp->if_flags |= IFF_UP;
    909  1.59.4.1    rpaulo 		error = gre_kick(sc);
    910      1.36    itojun 		break;
    911      1.55     perry 	case SIOCSIFDSTADDR:
    912       1.1       hwr 		break;
    913       1.1       hwr 	case SIOCSIFFLAGS:
    914  1.59.4.1    rpaulo 		oproto = sc->g_proto;
    915  1.59.4.1    rpaulo 		switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
    916  1.59.4.1    rpaulo 		case IFF_LINK0|IFF_LINK2:
    917  1.59.4.1    rpaulo 			sc->g_proto = IPPROTO_UDP;
    918  1.59.4.1    rpaulo 			if (oproto != IPPROTO_UDP)
    919  1.59.4.1    rpaulo 				ifp->if_flags &= ~IFF_RUNNING;
    920  1.59.4.1    rpaulo 			error = gre_kick(sc);
    921      1.13    martin 			break;
    922  1.59.4.1    rpaulo 		case IFF_LINK0:
    923      1.35    itojun 			sc->g_proto = IPPROTO_GRE;
    924  1.59.4.1    rpaulo 			gre_wakeup(sc);
    925  1.59.4.1    rpaulo 			goto recompute;
    926  1.59.4.1    rpaulo 		case 0:
    927      1.35    itojun 			sc->g_proto = IPPROTO_MOBILE;
    928  1.59.4.1    rpaulo 			gre_wakeup(sc);
    929  1.59.4.1    rpaulo 			goto recompute;
    930  1.59.4.1    rpaulo 		}
    931       1.1       hwr 		break;
    932      1.20    itojun 	case SIOCSIFMTU:
    933      1.27    martin 		if (ifr->ifr_mtu < 576) {
    934       1.1       hwr 			error = EINVAL;
    935       1.1       hwr 			break;
    936       1.1       hwr 		}
    937       1.1       hwr 		ifp->if_mtu = ifr->ifr_mtu;
    938       1.1       hwr 		break;
    939       1.1       hwr 	case SIOCGIFMTU:
    940       1.1       hwr 		ifr->ifr_mtu = sc->sc_if.if_mtu;
    941       1.1       hwr 		break;
    942       1.1       hwr 	case SIOCADDMULTI:
    943       1.1       hwr 	case SIOCDELMULTI:
    944       1.8  explorer 		if (ifr == 0) {
    945       1.1       hwr 			error = EAFNOSUPPORT;
    946       1.1       hwr 			break;
    947       1.1       hwr 		}
    948       1.8  explorer 		switch (ifr->ifr_addr.sa_family) {
    949       1.1       hwr #ifdef INET
    950       1.1       hwr 		case AF_INET:
    951       1.1       hwr 			break;
    952       1.1       hwr #endif
    953      1.56        is #ifdef INET6
    954      1.56        is 		case AF_INET6:
    955      1.56        is 			break;
    956      1.56        is #endif
    957       1.1       hwr 		default:
    958       1.1       hwr 			error = EAFNOSUPPORT;
    959       1.1       hwr 			break;
    960       1.1       hwr 		}
    961       1.1       hwr 		break;
    962       1.1       hwr 	case GRESPROTO:
    963  1.59.4.1    rpaulo 		oproto = sc->g_proto;
    964       1.1       hwr 		sc->g_proto = ifr->ifr_flags;
    965       1.1       hwr 		switch (sc->g_proto) {
    966  1.59.4.1    rpaulo 		case IPPROTO_UDP:
    967  1.59.4.1    rpaulo 			ifp->if_flags |= IFF_LINK0|IFF_LINK2;
    968  1.59.4.1    rpaulo 			if (oproto != IPPROTO_UDP)
    969  1.59.4.1    rpaulo 				ifp->if_flags &= ~IFF_RUNNING;
    970  1.59.4.1    rpaulo 			error = gre_kick(sc);
    971  1.59.4.1    rpaulo 			break;
    972      1.40    itojun 		case IPPROTO_GRE:
    973       1.3       hwr 			ifp->if_flags |= IFF_LINK0;
    974  1.59.4.1    rpaulo 			ifp->if_flags &= ~IFF_LINK2;
    975  1.59.4.1    rpaulo 			goto recompute;
    976      1.40    itojun 		case IPPROTO_MOBILE:
    977  1.59.4.1    rpaulo 			ifp->if_flags &= ~(IFF_LINK0|IFF_LINK2);
    978  1.59.4.1    rpaulo 			goto recompute;
    979       1.1       hwr 		default:
    980      1.40    itojun 			error = EPROTONOSUPPORT;
    981      1.35    itojun 			break;
    982       1.1       hwr 		}
    983       1.1       hwr 		break;
    984       1.1       hwr 	case GREGPROTO:
    985       1.1       hwr 		ifr->ifr_flags = sc->g_proto;
    986       1.1       hwr 		break;
    987       1.1       hwr 	case GRESADDRS:
    988       1.1       hwr 	case GRESADDRD:
    989       1.1       hwr 		/*
    990      1.20    itojun 		 * set tunnel endpoints, compute a less specific route
    991      1.20    itojun 		 * to the remote end and mark if as up
    992      1.20    itojun 		 */
    993       1.1       hwr 		sa = &ifr->ifr_addr;
    994  1.59.4.1    rpaulo 		if (cmd == GRESADDRS) {
    995       1.1       hwr 			sc->g_src = (satosin(sa))->sin_addr;
    996  1.59.4.1    rpaulo 			sc->g_srcport = satosin(sa)->sin_port;
    997  1.59.4.1    rpaulo 		}
    998  1.59.4.1    rpaulo 		if (cmd == GRESADDRD) {
    999  1.59.4.1    rpaulo 			if (sc->g_proto == IPPROTO_UDP &&
   1000  1.59.4.1    rpaulo 			    satosin(sa)->sin_port == 0) {
   1001  1.59.4.1    rpaulo 				error = EINVAL;
   1002  1.59.4.1    rpaulo 				break;
   1003  1.59.4.1    rpaulo 			}
   1004       1.1       hwr 			sc->g_dst = (satosin(sa))->sin_addr;
   1005  1.59.4.1    rpaulo 			sc->g_dstport = satosin(sa)->sin_port;
   1006  1.59.4.1    rpaulo 		}
   1007      1.30    itojun 	recompute:
   1008  1.59.4.1    rpaulo 		if (sc->g_proto == IPPROTO_UDP ||
   1009  1.59.4.1    rpaulo 		    (sc->g_src.s_addr != INADDR_ANY &&
   1010  1.59.4.1    rpaulo 		     sc->g_dst.s_addr != INADDR_ANY)) {
   1011  1.59.4.1    rpaulo 			if (sc->sc_fp != NULL) {
   1012  1.59.4.1    rpaulo 				closef(sc->sc_fp, l);
   1013  1.59.4.1    rpaulo 				sc->sc_fp = NULL;
   1014  1.59.4.1    rpaulo 			}
   1015  1.59.4.1    rpaulo 			if (sc->route.ro_rt != NULL) {
   1016       1.1       hwr 				RTFREE(sc->route.ro_rt);
   1017  1.59.4.1    rpaulo 				sc->route.ro_rt = NULL;
   1018  1.59.4.1    rpaulo 			}
   1019  1.59.4.1    rpaulo 			if (sc->g_proto == IPPROTO_UDP)
   1020  1.59.4.1    rpaulo 				error = gre_kick(sc);
   1021  1.59.4.1    rpaulo 			else if (gre_compute_route(sc) == 0)
   1022      1.39    itojun 				ifp->if_flags |= IFF_RUNNING;
   1023      1.39    itojun 			else
   1024      1.39    itojun 				ifp->if_flags &= ~IFF_RUNNING;
   1025       1.1       hwr 		}
   1026       1.1       hwr 		break;
   1027       1.1       hwr 	case GREGADDRS:
   1028      1.33    itojun 		memset(&si, 0, sizeof(si));
   1029      1.28    itojun 		si.sin_family = AF_INET;
   1030      1.28    itojun 		si.sin_len = sizeof(struct sockaddr_in);
   1031       1.1       hwr 		si.sin_addr.s_addr = sc->g_src.s_addr;
   1032       1.8  explorer 		sa = sintosa(&si);
   1033       1.1       hwr 		ifr->ifr_addr = *sa;
   1034       1.1       hwr 		break;
   1035       1.1       hwr 	case GREGADDRD:
   1036      1.33    itojun 		memset(&si, 0, sizeof(si));
   1037      1.28    itojun 		si.sin_family = AF_INET;
   1038      1.28    itojun 		si.sin_len = sizeof(struct sockaddr_in);
   1039       1.1       hwr 		si.sin_addr.s_addr = sc->g_dst.s_addr;
   1040       1.8  explorer 		sa = sintosa(&si);
   1041       1.1       hwr 		ifr->ifr_addr = *sa;
   1042      1.28    itojun 		break;
   1043  1.59.4.1    rpaulo 	case GREDSOCK:
   1044  1.59.4.1    rpaulo 		if (sc->g_proto != IPPROTO_UDP)
   1045  1.59.4.1    rpaulo 			return EINVAL;
   1046  1.59.4.1    rpaulo 		if (sc->sc_fp != NULL) {
   1047  1.59.4.1    rpaulo 			closef(sc->sc_fp, l);
   1048  1.59.4.1    rpaulo 			sc->sc_fp = NULL;
   1049  1.59.4.1    rpaulo 			error = gre_kick(sc);
   1050  1.59.4.1    rpaulo 		}
   1051  1.59.4.1    rpaulo 		break;
   1052  1.59.4.1    rpaulo 	case GRESSOCK:
   1053  1.59.4.1    rpaulo 		if (sc->g_proto != IPPROTO_UDP)
   1054  1.59.4.1    rpaulo 			return EINVAL;
   1055  1.59.4.1    rpaulo 		/* getsock() will FILE_USE() the descriptor for us */
   1056  1.59.4.1    rpaulo 		if ((error = getsock(p->p_fd, (int)ifr->ifr_value, &fp)) != 0)
   1057  1.59.4.1    rpaulo 			break;
   1058  1.59.4.1    rpaulo 		so = (struct socket *)fp->f_data;
   1059  1.59.4.1    rpaulo 		if (so->so_type != SOCK_DGRAM) {
   1060  1.59.4.1    rpaulo 			FILE_UNUSE(fp, NULL);
   1061  1.59.4.1    rpaulo 			error = EINVAL;
   1062  1.59.4.1    rpaulo 			break;
   1063  1.59.4.1    rpaulo 		}
   1064  1.59.4.1    rpaulo 		/* check address */
   1065  1.59.4.1    rpaulo 		if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0) {
   1066  1.59.4.1    rpaulo 			FILE_UNUSE(fp, NULL);
   1067      1.28    itojun 			break;
   1068  1.59.4.1    rpaulo 		}
   1069  1.59.4.1    rpaulo 
   1070  1.59.4.1    rpaulo 		fp->f_count++;
   1071  1.59.4.1    rpaulo 
   1072  1.59.4.1    rpaulo 		ofp = sc->sc_fp;
   1073  1.59.4.1    rpaulo 		sc->sc_fp = fp;
   1074  1.59.4.1    rpaulo 		if ((error = gre_kick(sc)) != 0) {
   1075  1.59.4.1    rpaulo 			closef(fp, l);
   1076  1.59.4.1    rpaulo 			sc->sc_fp = ofp;
   1077  1.59.4.1    rpaulo 			break;
   1078  1.59.4.1    rpaulo 		}
   1079  1.59.4.1    rpaulo 		sc->g_src = src.sin_addr;
   1080  1.59.4.1    rpaulo 		sc->g_srcport = src.sin_port;
   1081  1.59.4.1    rpaulo 		sc->g_dst = dst.sin_addr;
   1082  1.59.4.1    rpaulo 		sc->g_dstport = dst.sin_port;
   1083  1.59.4.1    rpaulo 		if (ofp != NULL)
   1084  1.59.4.1    rpaulo 			closef(ofp, l);
   1085  1.59.4.1    rpaulo 		break;
   1086  1.59.4.1    rpaulo 	case SIOCSLIFPHYADDR:
   1087      1.28    itojun 		if (lifr->addr.ss_family != AF_INET ||
   1088      1.31    itojun 		    lifr->dstaddr.ss_family != AF_INET) {
   1089      1.31    itojun 			error = EAFNOSUPPORT;
   1090      1.31    itojun 			break;
   1091      1.31    itojun 		}
   1092      1.28    itojun 		if (lifr->addr.ss_len != sizeof(si) ||
   1093      1.31    itojun 		    lifr->dstaddr.ss_len != sizeof(si)) {
   1094      1.31    itojun 			error = EINVAL;
   1095      1.31    itojun 			break;
   1096      1.31    itojun 		}
   1097  1.59.4.1    rpaulo 		sc->g_src = satosin(&lifr->addr)->sin_addr;
   1098  1.59.4.1    rpaulo 		sc->g_dst = satosin(&lifr->dstaddr)->sin_addr;
   1099  1.59.4.1    rpaulo 		sc->g_srcport = satosin(&lifr->addr)->sin_port;
   1100  1.59.4.1    rpaulo 		sc->g_dstport = satosin(&lifr->dstaddr)->sin_port;
   1101      1.30    itojun 		goto recompute;
   1102      1.28    itojun 	case SIOCDIFPHYADDR:
   1103      1.28    itojun 		sc->g_src.s_addr = INADDR_ANY;
   1104      1.28    itojun 		sc->g_dst.s_addr = INADDR_ANY;
   1105  1.59.4.1    rpaulo 		sc->g_srcport = 0;
   1106  1.59.4.1    rpaulo 		sc->g_dstport = 0;
   1107  1.59.4.1    rpaulo 		goto recompute;
   1108      1.28    itojun 	case SIOCGLIFPHYADDR:
   1109      1.28    itojun 		if (sc->g_src.s_addr == INADDR_ANY ||
   1110      1.28    itojun 		    sc->g_dst.s_addr == INADDR_ANY) {
   1111      1.28    itojun 			error = EADDRNOTAVAIL;
   1112      1.28    itojun 			break;
   1113      1.28    itojun 		}
   1114      1.33    itojun 		memset(&si, 0, sizeof(si));
   1115      1.28    itojun 		si.sin_family = AF_INET;
   1116      1.28    itojun 		si.sin_len = sizeof(struct sockaddr_in);
   1117  1.59.4.1    rpaulo 		si.sin_addr = sc->g_src;
   1118  1.59.4.1    rpaulo 		if (sc->g_proto == IPPROTO_UDP)
   1119  1.59.4.1    rpaulo 			si.sin_port = sc->g_srcport;
   1120      1.33    itojun 		memcpy(&lifr->addr, &si, sizeof(si));
   1121  1.59.4.1    rpaulo 		si.sin_addr = sc->g_dst;
   1122  1.59.4.1    rpaulo 		if (sc->g_proto == IPPROTO_UDP)
   1123  1.59.4.1    rpaulo 			si.sin_port = sc->g_dstport;
   1124      1.33    itojun 		memcpy(&lifr->dstaddr, &si, sizeof(si));
   1125       1.1       hwr 		break;
   1126       1.1       hwr 	default:
   1127       1.1       hwr 		error = EINVAL;
   1128      1.31    itojun 		break;
   1129       1.1       hwr 	}
   1130       1.1       hwr 	splx(s);
   1131       1.8  explorer 	return (error);
   1132       1.1       hwr }
   1133       1.1       hwr 
   1134      1.20    itojun /*
   1135       1.1       hwr  * computes a route to our destination that is not the one
   1136       1.1       hwr  * which would be taken by ip_output(), as this one will loop back to
   1137       1.1       hwr  * us. If the interface is p2p as  a--->b, then a routing entry exists
   1138       1.1       hwr  * If we now send a packet to b (e.g. ping b), this will come down here
   1139      1.43       wiz  * gets src=a, dst=b tacked on and would from ip_output() sent back to
   1140       1.1       hwr  * if_gre.
   1141       1.1       hwr  * Goal here is to compute a route to b that is less specific than
   1142       1.1       hwr  * a-->b. We know that this one exists as in normal operation we have
   1143       1.1       hwr  * at least a default route which matches.
   1144       1.1       hwr  */
   1145      1.59   thorpej static int
   1146       1.8  explorer gre_compute_route(struct gre_softc *sc)
   1147       1.1       hwr {
   1148       1.1       hwr 	struct route *ro;
   1149       1.8  explorer 	u_int32_t a, b, c;
   1150       1.1       hwr 
   1151       1.8  explorer 	ro = &sc->route;
   1152      1.20    itojun 
   1153       1.8  explorer 	memset(ro, 0, sizeof(struct route));
   1154       1.8  explorer 	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
   1155       1.8  explorer 	ro->ro_dst.sa_family = AF_INET;
   1156       1.8  explorer 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
   1157       1.8  explorer 
   1158       1.1       hwr 	/*
   1159       1.1       hwr 	 * toggle last bit, so our interface is not found, but a less
   1160      1.20    itojun 	 * specific route. I'd rather like to specify a shorter mask,
   1161      1.20    itojun 	 * but this is not possible. Should work though. XXX
   1162       1.1       hwr 	 * there is a simpler way ...
   1163      1.20    itojun 	 */
   1164       1.8  explorer 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
   1165       1.8  explorer 		a = ntohl(sc->g_dst.s_addr);
   1166       1.8  explorer 		b = a & 0x01;
   1167       1.8  explorer 		c = a & 0xfffffffe;
   1168       1.8  explorer 		b = b ^ 0x01;
   1169       1.8  explorer 		a = b | c;
   1170       1.8  explorer 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
   1171      1.20    itojun 		    = htonl(a);
   1172       1.8  explorer 	}
   1173       1.1       hwr 
   1174       1.1       hwr #ifdef DIAGNOSTIC
   1175      1.45     grant 	printf("%s: searching for a route to %s", sc->sc_if.if_xname,
   1176      1.19    itojun 	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
   1177       1.1       hwr #endif
   1178       1.1       hwr 
   1179       1.1       hwr 	rtalloc(ro);
   1180       1.1       hwr 
   1181       1.1       hwr 	/*
   1182      1.25    martin 	 * check if this returned a route at all and this route is no
   1183      1.25    martin 	 * recursion to ourself
   1184      1.25    martin 	 */
   1185      1.25    martin 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
   1186      1.25    martin #ifdef DIAGNOSTIC
   1187      1.25    martin 		if (ro->ro_rt == NULL)
   1188      1.25    martin 			printf(" - no route found!\n");
   1189      1.25    martin 		else
   1190      1.25    martin 			printf(" - route loops back to ourself!\n");
   1191      1.25    martin #endif
   1192      1.25    martin 		return EADDRNOTAVAIL;
   1193      1.25    martin 	}
   1194      1.25    martin 
   1195      1.25    martin 	/*
   1196      1.20    itojun 	 * now change it back - else ip_output will just drop
   1197      1.20    itojun 	 * the route and search one to this interface ...
   1198      1.20    itojun 	 */
   1199       1.8  explorer 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
   1200       1.8  explorer 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
   1201       1.1       hwr 
   1202       1.1       hwr #ifdef DIAGNOSTIC
   1203  1.59.4.1    rpaulo 	printf(", choosing %s with gateway %s\n", ro->ro_rt->rt_ifp->if_xname,
   1204      1.19    itojun 	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
   1205       1.1       hwr #endif
   1206      1.25    martin 
   1207      1.25    martin 	return 0;
   1208       1.1       hwr }
   1209       1.1       hwr 
   1210       1.3       hwr /*
   1211      1.20    itojun  * do a checksum of a buffer - much like in_cksum, which operates on
   1212      1.20    itojun  * mbufs.
   1213       1.3       hwr  */
   1214      1.48    itojun u_int16_t
   1215      1.48    itojun gre_in_cksum(u_int16_t *p, u_int len)
   1216       1.3       hwr {
   1217      1.48    itojun 	u_int32_t sum = 0;
   1218       1.3       hwr 	int nwords = len >> 1;
   1219      1.20    itojun 
   1220       1.3       hwr 	while (nwords-- != 0)
   1221       1.3       hwr 		sum += *p++;
   1222      1.20    itojun 
   1223      1.20    itojun 	if (len & 1) {
   1224      1.20    itojun 		union {
   1225      1.20    itojun 			u_short w;
   1226      1.20    itojun 			u_char c[2];
   1227      1.20    itojun 		} u;
   1228      1.20    itojun 		u.c[0] = *(u_char *)p;
   1229      1.20    itojun 		u.c[1] = 0;
   1230      1.20    itojun 		sum += u.w;
   1231      1.20    itojun 	}
   1232      1.20    itojun 
   1233      1.20    itojun 	/* end-around-carry */
   1234      1.20    itojun 	sum = (sum >> 16) + (sum & 0xffff);
   1235      1.20    itojun 	sum += (sum >> 16);
   1236      1.20    itojun 	return (~sum);
   1237       1.3       hwr }
   1238      1.54  christos #endif
   1239      1.54  christos 
   1240      1.59   thorpej void	greattach(int);
   1241      1.54  christos 
   1242      1.54  christos /* ARGSUSED */
   1243      1.54  christos void
   1244      1.59   thorpej greattach(int count)
   1245      1.54  christos {
   1246      1.54  christos #ifdef INET
   1247      1.54  christos 	LIST_INIT(&gre_softc_list);
   1248      1.54  christos 	if_clone_attach(&gre_cloner);
   1249      1.54  christos #endif
   1250      1.54  christos }
   1251