Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.35
      1  1.35   itojun /*	$NetBSD: if_gif.c,v 1.35 2001/09/26 07:54:19 itojun Exp $	*/
      2  1.34   itojun /*	$KAME: if_gif.c,v 1.76 2001/08/20 02:01:02 kjc Exp $	*/
      3   1.3   itojun 
      4   1.2   itojun /*
      5   1.2   itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6   1.2   itojun  * All rights reserved.
      7   1.9   itojun  *
      8   1.2   itojun  * Redistribution and use in source and binary forms, with or without
      9   1.2   itojun  * modification, are permitted provided that the following conditions
     10   1.2   itojun  * are met:
     11   1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.2   itojun  *    documentation and/or other materials provided with the distribution.
     16   1.2   itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.2   itojun  *    may be used to endorse or promote products derived from this software
     18   1.2   itojun  *    without specific prior written permission.
     19   1.9   itojun  *
     20   1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.2   itojun  * SUCH DAMAGE.
     31   1.2   itojun  */
     32   1.2   itojun 
     33   1.2   itojun #include "opt_inet.h"
     34  1.24   itojun #include "opt_iso.h"
     35   1.2   itojun 
     36   1.2   itojun #include <sys/param.h>
     37   1.2   itojun #include <sys/systm.h>
     38   1.2   itojun #include <sys/kernel.h>
     39   1.2   itojun #include <sys/mbuf.h>
     40   1.2   itojun #include <sys/socket.h>
     41   1.2   itojun #include <sys/sockio.h>
     42   1.2   itojun #include <sys/errno.h>
     43   1.2   itojun #include <sys/ioctl.h>
     44   1.2   itojun #include <sys/time.h>
     45   1.2   itojun #include <sys/syslog.h>
     46  1.17   martin #include <sys/proc.h>
     47   1.9   itojun #include <sys/protosw.h>
     48   1.2   itojun #include <machine/cpu.h>
     49  1.33   itojun #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
     50  1.33   itojun #include <machine/intr.h>
     51  1.33   itojun #endif
     52   1.2   itojun 
     53   1.2   itojun #include <net/if.h>
     54   1.2   itojun #include <net/if_types.h>
     55   1.2   itojun #include <net/netisr.h>
     56   1.2   itojun #include <net/route.h>
     57   1.2   itojun #include <net/bpf.h>
     58   1.2   itojun 
     59   1.2   itojun #include <netinet/in.h>
     60   1.2   itojun #include <netinet/in_systm.h>
     61  1.15   itojun #include <netinet/ip.h>
     62  1.15   itojun #ifdef	INET
     63   1.2   itojun #include <netinet/in_var.h>
     64   1.2   itojun #include <netinet/in_gif.h>
     65   1.2   itojun #endif	/* INET */
     66   1.2   itojun 
     67   1.2   itojun #ifdef INET6
     68   1.2   itojun #ifndef INET
     69   1.2   itojun #include <netinet/in.h>
     70   1.2   itojun #endif
     71   1.2   itojun #include <netinet6/in6_var.h>
     72   1.2   itojun #include <netinet/ip6.h>
     73   1.2   itojun #include <netinet6/ip6_var.h>
     74   1.2   itojun #include <netinet6/in6_gif.h>
     75   1.9   itojun #include <netinet6/ip6protosw.h>
     76   1.2   itojun #endif /* INET6 */
     77   1.2   itojun 
     78  1.24   itojun #ifdef ISO
     79  1.24   itojun #include <netiso/iso.h>
     80  1.24   itojun #include <netiso/iso_var.h>
     81  1.24   itojun #endif
     82  1.24   itojun 
     83   1.9   itojun #include <netinet/ip_encap.h>
     84   1.2   itojun #include <net/if_gif.h>
     85   1.2   itojun 
     86   1.2   itojun #include "gif.h"
     87   1.2   itojun #include "bpfilter.h"
     88   1.2   itojun 
     89   1.4   itojun #include <net/net_osdep.h>
     90   1.4   itojun 
     91   1.2   itojun #if NGIF > 0
     92   1.2   itojun 
     93   1.4   itojun void gifattach __P((int));
     94  1.33   itojun #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
     95  1.33   itojun void gifnetisr __P((void));
     96  1.33   itojun #endif
     97  1.33   itojun void gifintr __P((void *));
     98  1.24   itojun #ifdef ISO
     99  1.33   itojun static struct mbuf *gif_eon_encap __P((struct mbuf *));
    100  1.33   itojun static struct mbuf *gif_eon_decap __P((struct ifnet *, struct mbuf *));
    101  1.24   itojun #endif
    102   1.2   itojun 
    103   1.2   itojun /*
    104   1.2   itojun  * gif global variable definitions
    105   1.2   itojun  */
    106  1.12  thorpej LIST_HEAD(, gif_softc) gif_softc_list;
    107  1.12  thorpej 
    108  1.12  thorpej int	gif_clone_create __P((struct if_clone *, int));
    109  1.12  thorpej void	gif_clone_destroy __P((struct ifnet *));
    110  1.12  thorpej 
    111  1.12  thorpej struct if_clone gif_cloner =
    112  1.12  thorpej     IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
    113  1.12  thorpej 
    114   1.9   itojun #ifndef MAX_GIF_NEST
    115   1.9   itojun /*
    116   1.9   itojun  * This macro controls the upper limitation on nesting of gif tunnels.
    117   1.9   itojun  * Since, setting a large value to this macro with a careless configuration
    118   1.9   itojun  * may introduce system crash, we don't allow any nestings by default.
    119   1.9   itojun  * If you need to configure nested gif tunnels, you can define this macro
    120  1.31   itojun  * in your kernel configuration file.  However, if you do so, please be
    121   1.9   itojun  * careful to configure the tunnels so that it won't make a loop.
    122   1.9   itojun  */
    123   1.9   itojun #define MAX_GIF_NEST 1
    124   1.9   itojun #endif
    125   1.9   itojun static int max_gif_nesting = MAX_GIF_NEST;
    126   1.2   itojun 
    127  1.12  thorpej /* ARGSUSED */
    128   1.2   itojun void
    129  1.12  thorpej gifattach(count)
    130  1.12  thorpej 	int count;
    131  1.12  thorpej {
    132  1.12  thorpej 
    133  1.12  thorpej 	LIST_INIT(&gif_softc_list);
    134  1.12  thorpej 	if_clone_attach(&gif_cloner);
    135  1.12  thorpej }
    136  1.12  thorpej 
    137  1.12  thorpej int
    138  1.12  thorpej gif_clone_create(ifc, unit)
    139  1.12  thorpej 	struct if_clone *ifc;
    140  1.12  thorpej 	int unit;
    141   1.2   itojun {
    142  1.12  thorpej 	struct gif_softc *sc;
    143  1.12  thorpej 
    144  1.12  thorpej 	sc = malloc(sizeof(struct gif_softc), M_DEVBUF, M_WAIT);
    145  1.30  thorpej 	memset(sc, 0, sizeof(struct gif_softc));
    146   1.2   itojun 
    147  1.12  thorpej 	sprintf(sc->gif_if.if_xname, "%s%d", ifc->ifc_name, unit);
    148   1.9   itojun 
    149  1.31   itojun 	gifattach0(sc);
    150  1.31   itojun 
    151  1.31   itojun 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
    152  1.31   itojun 	return (0);
    153  1.31   itojun }
    154  1.31   itojun 
    155  1.31   itojun void
    156  1.31   itojun gifattach0(sc)
    157  1.31   itojun 	struct gif_softc *sc;
    158  1.31   itojun {
    159  1.31   itojun 
    160  1.12  thorpej 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
    161   1.9   itojun 
    162  1.31   itojun 	sc->gif_if.if_addrlen = 0;
    163  1.12  thorpej 	sc->gif_if.if_mtu    = GIF_MTU;
    164  1.12  thorpej 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
    165  1.12  thorpej 	sc->gif_if.if_ioctl  = gif_ioctl;
    166  1.12  thorpej 	sc->gif_if.if_output = gif_output;
    167  1.12  thorpej 	sc->gif_if.if_type   = IFT_GIF;
    168  1.19  thorpej 	sc->gif_if.if_dlt    = DLT_NULL;
    169  1.34   itojun #ifdef ALTQ
    170  1.34   itojun 	IFQ_SET_READY(&sc->gif_if.if_snd);
    171  1.34   itojun #endif
    172  1.12  thorpej 	if_attach(&sc->gif_if);
    173  1.20  thorpej 	if_alloc_sadl(&sc->gif_if);
    174   1.2   itojun #if NBPFILTER > 0
    175  1.12  thorpej 	bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
    176   1.9   itojun #endif
    177  1.12  thorpej }
    178  1.12  thorpej 
    179  1.12  thorpej void
    180  1.12  thorpej gif_clone_destroy(ifp)
    181  1.12  thorpej 	struct ifnet *ifp;
    182  1.12  thorpej {
    183  1.12  thorpej 	struct gif_softc *sc = (void *) ifp;
    184  1.12  thorpej 
    185  1.31   itojun 	gif_delete_tunnel(&sc->gif_if);
    186  1.12  thorpej 	LIST_REMOVE(sc, gif_list);
    187  1.12  thorpej #ifdef INET6
    188  1.12  thorpej 	encap_detach(sc->encap_cookie6);
    189  1.12  thorpej #endif
    190  1.12  thorpej #ifdef INET
    191  1.12  thorpej 	encap_detach(sc->encap_cookie4);
    192  1.12  thorpej #endif
    193  1.12  thorpej 
    194  1.12  thorpej #if NBPFILTER > 0
    195  1.12  thorpej 	bpfdetach(ifp);
    196  1.12  thorpej #endif
    197  1.12  thorpej 	if_detach(ifp);
    198  1.12  thorpej 
    199  1.12  thorpej 	free(sc, M_DEVBUF);
    200   1.9   itojun }
    201   1.9   itojun 
    202  1.31   itojun int
    203   1.9   itojun gif_encapcheck(m, off, proto, arg)
    204   1.9   itojun 	const struct mbuf *m;
    205   1.9   itojun 	int off;
    206   1.9   itojun 	int proto;
    207   1.9   itojun 	void *arg;
    208   1.9   itojun {
    209   1.9   itojun 	struct ip ip;
    210   1.9   itojun 	struct gif_softc *sc;
    211   1.9   itojun 
    212   1.9   itojun 	sc = (struct gif_softc *)arg;
    213   1.9   itojun 	if (sc == NULL)
    214   1.9   itojun 		return 0;
    215   1.9   itojun 
    216   1.9   itojun 	if ((sc->gif_if.if_flags & IFF_UP) == 0)
    217   1.9   itojun 		return 0;
    218   1.9   itojun 
    219   1.9   itojun 	/* no physical address */
    220   1.9   itojun 	if (!sc->gif_psrc || !sc->gif_pdst)
    221   1.9   itojun 		return 0;
    222   1.9   itojun 
    223   1.9   itojun 	switch (proto) {
    224   1.9   itojun #ifdef INET
    225   1.9   itojun 	case IPPROTO_IPV4:
    226   1.9   itojun 		break;
    227   1.9   itojun #endif
    228   1.9   itojun #ifdef INET6
    229   1.9   itojun 	case IPPROTO_IPV6:
    230   1.9   itojun 		break;
    231   1.9   itojun #endif
    232  1.24   itojun #ifdef ISO
    233  1.24   itojun 	case IPPROTO_EON:
    234  1.24   itojun 		break;
    235  1.24   itojun #endif
    236   1.9   itojun 	default:
    237   1.9   itojun 		return 0;
    238   1.9   itojun 	}
    239   1.9   itojun 
    240   1.9   itojun 	/* LINTED const cast */
    241   1.9   itojun 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
    242   1.9   itojun 
    243   1.9   itojun 	switch (ip.ip_v) {
    244   1.9   itojun #ifdef INET
    245   1.9   itojun 	case 4:
    246   1.9   itojun 		if (sc->gif_psrc->sa_family != AF_INET ||
    247   1.9   itojun 		    sc->gif_pdst->sa_family != AF_INET)
    248   1.9   itojun 			return 0;
    249   1.9   itojun 		return gif_encapcheck4(m, off, proto, arg);
    250   1.9   itojun #endif
    251   1.9   itojun #ifdef INET6
    252   1.9   itojun 	case 6:
    253   1.9   itojun 		if (sc->gif_psrc->sa_family != AF_INET6 ||
    254   1.9   itojun 		    sc->gif_pdst->sa_family != AF_INET6)
    255   1.9   itojun 			return 0;
    256   1.9   itojun 		return gif_encapcheck6(m, off, proto, arg);
    257   1.9   itojun #endif
    258   1.9   itojun 	default:
    259   1.9   itojun 		return 0;
    260   1.2   itojun 	}
    261   1.2   itojun }
    262   1.2   itojun 
    263   1.2   itojun int
    264   1.2   itojun gif_output(ifp, m, dst, rt)
    265   1.2   itojun 	struct ifnet *ifp;
    266   1.2   itojun 	struct mbuf *m;
    267   1.2   itojun 	struct sockaddr *dst;
    268   1.2   itojun 	struct rtentry *rt;	/* added in net2 */
    269   1.2   itojun {
    270  1.21   itojun 	struct gif_softc *sc = (struct gif_softc*)ifp;
    271   1.2   itojun 	int error = 0;
    272   1.2   itojun 	static int called = 0;	/* XXX: MUTEX */
    273  1.34   itojun #ifdef ALTQ
    274  1.34   itojun 	struct altq_pktattr pktattr;
    275  1.34   itojun #endif
    276  1.33   itojun 	int s;
    277  1.33   itojun 
    278  1.34   itojun #ifdef ALTQ
    279  1.33   itojun 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    280  1.34   itojun #endif
    281   1.2   itojun 
    282   1.2   itojun 	/*
    283   1.2   itojun 	 * gif may cause infinite recursion calls when misconfigured.
    284   1.2   itojun 	 * We'll prevent this by introducing upper limit.
    285   1.2   itojun 	 * XXX: this mechanism may introduce another problem about
    286   1.2   itojun 	 *      mutual exclusion of the variable CALLED, especially if we
    287   1.2   itojun 	 *      use kernel thread.
    288   1.2   itojun 	 */
    289   1.9   itojun 	if (++called > max_gif_nesting) {
    290   1.2   itojun 		log(LOG_NOTICE,
    291   1.2   itojun 		    "gif_output: recursively called too many times(%d)\n",
    292   1.2   itojun 		    called);
    293   1.2   itojun 		m_freem(m);
    294   1.2   itojun 		error = EIO;	/* is there better errno? */
    295   1.2   itojun 		goto end;
    296   1.2   itojun 	}
    297   1.2   itojun 
    298   1.2   itojun 	m->m_flags &= ~(M_BCAST|M_MCAST);
    299   1.2   itojun 	if (!(ifp->if_flags & IFF_UP) ||
    300   1.2   itojun 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    301   1.2   itojun 		m_freem(m);
    302   1.2   itojun 		error = ENETDOWN;
    303   1.2   itojun 		goto end;
    304   1.2   itojun 	}
    305   1.2   itojun 
    306  1.24   itojun 	/* inner AF-specific encapsulation */
    307  1.24   itojun 	switch (dst->sa_family) {
    308  1.24   itojun #ifdef ISO
    309  1.24   itojun 	case AF_ISO:
    310  1.31   itojun 		m = gif_eon_encap(m);
    311  1.31   itojun 		if (!m) {
    312  1.31   itojun 			error = ENOBUFS;
    313  1.24   itojun 			goto end;
    314  1.31   itojun 		}
    315  1.31   itojun 		break;
    316  1.24   itojun #endif
    317  1.24   itojun 	default:
    318  1.24   itojun 		break;
    319  1.24   itojun 	}
    320  1.24   itojun 
    321   1.9   itojun 	/* XXX should we check if our outer source is legal? */
    322   1.2   itojun 
    323  1.33   itojun 	/* use DLT_NULL encapsulation here to pass inner af type */
    324  1.33   itojun 	M_PREPEND(m, sizeof(int), M_DONTWAIT);
    325  1.33   itojun 	if (!m) {
    326  1.33   itojun 		error = ENOBUFS;
    327  1.33   itojun 		goto end;
    328  1.33   itojun 	}
    329  1.33   itojun 	*mtod(m, int *) = dst->sa_family;
    330  1.33   itojun 
    331  1.33   itojun 	s = splnet();
    332  1.34   itojun #ifdef ALTQ
    333  1.33   itojun 	IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    334  1.33   itojun 	if (error) {
    335  1.33   itojun 		splx(s);
    336  1.31   itojun 		goto end;
    337   1.2   itojun 	}
    338  1.34   itojun #else
    339  1.34   itojun 	if (IF_QFULL(&ifp->if_snd)) {
    340  1.34   itojun 		m_freem(m);
    341  1.34   itojun 		error = ENOBUFS;
    342  1.34   itojun 		splx(s);
    343  1.34   itojun 		goto end;
    344  1.34   itojun 	}
    345  1.34   itojun 	IF_ENQUEUE(&ifp->if_snd, m);
    346  1.34   itojun #endif /* ALTQ */
    347  1.33   itojun 	splx(s);
    348  1.33   itojun 
    349  1.33   itojun #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    350  1.33   itojun 	softintr_schedule(sc->gif_si);
    351  1.33   itojun #else
    352  1.33   itojun 	/* XXX bad spl level? */
    353  1.33   itojun 	gifnetisr();
    354  1.33   itojun #endif
    355  1.33   itojun 	error = 0;
    356   1.2   itojun 
    357   1.2   itojun   end:
    358   1.2   itojun 	called = 0;		/* reset recursion counter */
    359  1.31   itojun 	if (error)
    360  1.31   itojun 		ifp->if_oerrors++;
    361   1.2   itojun 	return error;
    362   1.2   itojun }
    363   1.2   itojun 
    364  1.33   itojun #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
    365  1.33   itojun void
    366  1.33   itojun gifnetisr()
    367  1.33   itojun {
    368  1.33   itojun 	struct gif_softc *sc;
    369  1.33   itojun 
    370  1.33   itojun 	for (sc = LIST_FIRST(&gif_softc_list); sc != NULL;
    371  1.33   itojun 	     sc = LIST_NEXT(sc, gif_list)) {
    372  1.33   itojun 		gifintr(sc);
    373  1.33   itojun 	}
    374  1.33   itojun }
    375  1.33   itojun #endif
    376  1.33   itojun 
    377   1.2   itojun void
    378  1.33   itojun gifintr(arg)
    379  1.33   itojun 	void *arg;
    380  1.33   itojun {
    381  1.33   itojun 	struct gif_softc *sc;
    382  1.33   itojun 	struct ifnet *ifp;
    383  1.33   itojun 	struct mbuf *m;
    384  1.33   itojun 	int family;
    385  1.33   itojun 	int len;
    386  1.33   itojun 	int s;
    387  1.33   itojun 	int error;
    388  1.33   itojun 
    389  1.33   itojun 	sc = (struct gif_softc *)arg;
    390  1.33   itojun 	ifp = &sc->gif_if;
    391  1.33   itojun 
    392  1.33   itojun 	/* output processing */
    393  1.33   itojun 	while (1) {
    394  1.33   itojun 		s = splnet();
    395  1.34   itojun #ifdef ALTQ
    396  1.34   itojun 		IFQ_DEQUEUE(&sc->gif_if.if_snd, m);
    397  1.34   itojun #else
    398  1.33   itojun 		IF_DEQUEUE(&sc->gif_if.if_snd, m);
    399  1.34   itojun #endif
    400  1.33   itojun 		splx(s);
    401  1.33   itojun 		if (m == NULL)
    402  1.33   itojun 			break;
    403  1.33   itojun 
    404  1.33   itojun 		/* grab and chop off inner af type */
    405  1.33   itojun 		if (sizeof(int) > m->m_len) {
    406  1.33   itojun 			m = m_pullup(m, sizeof(int));
    407  1.33   itojun 			if (!m) {
    408  1.33   itojun 				ifp->if_oerrors++;
    409  1.33   itojun 				continue;
    410  1.33   itojun 			}
    411  1.33   itojun 		}
    412  1.33   itojun 		family = *mtod(m, int *);
    413  1.33   itojun #if NBPFILTER > 0
    414  1.33   itojun 		if (ifp->if_bpf) {
    415  1.33   itojun #ifdef HAVE_OLD_BPF
    416  1.33   itojun 			bpf_mtap(ifp, m);
    417  1.33   itojun #else
    418  1.33   itojun 			bpf_mtap(ifp->if_bpf, m);
    419  1.33   itojun #endif
    420  1.33   itojun 		}
    421  1.33   itojun #endif
    422  1.33   itojun 		m_adj(m, sizeof(int));
    423  1.33   itojun 
    424  1.33   itojun 		len = m->m_pkthdr.len;
    425  1.33   itojun 
    426  1.33   itojun 		/* dispatch to output logic based on outer AF */
    427  1.33   itojun 		switch (sc->gif_psrc->sa_family) {
    428  1.33   itojun #ifdef INET
    429  1.33   itojun 		case AF_INET:
    430  1.33   itojun 			error = in_gif_output(ifp, family, m);
    431  1.33   itojun 			break;
    432  1.33   itojun #endif
    433  1.33   itojun #ifdef INET6
    434  1.33   itojun 		case AF_INET6:
    435  1.33   itojun 			error = in6_gif_output(ifp, family, m);
    436  1.33   itojun 			break;
    437  1.33   itojun #endif
    438  1.33   itojun 		default:
    439  1.33   itojun 			m_freem(m);
    440  1.33   itojun 			error = ENETDOWN;
    441  1.33   itojun 			break;
    442  1.33   itojun 		}
    443  1.33   itojun 
    444  1.33   itojun 		if (error)
    445  1.33   itojun 			ifp->if_oerrors++;
    446  1.33   itojun 		else {
    447  1.33   itojun 			ifp->if_opackets++;
    448  1.33   itojun 			ifp->if_obytes += len;
    449  1.33   itojun 		}
    450  1.33   itojun 	}
    451  1.33   itojun }
    452  1.33   itojun 
    453  1.33   itojun void
    454  1.33   itojun gif_input(m, af, ifp)
    455   1.2   itojun 	struct mbuf *m;
    456   1.2   itojun 	int af;
    457  1.33   itojun 	struct ifnet *ifp;
    458   1.2   itojun {
    459   1.2   itojun 	int s, isr;
    460  1.33   itojun 	struct ifqueue *ifq = NULL;
    461   1.2   itojun 
    462  1.33   itojun 	if (ifp == NULL) {
    463   1.2   itojun 		/* just in case */
    464   1.2   itojun 		m_freem(m);
    465   1.2   itojun 		return;
    466   1.2   itojun 	}
    467   1.2   itojun 
    468  1.33   itojun 	m->m_pkthdr.rcvif = ifp;
    469   1.2   itojun 
    470   1.2   itojun #if NBPFILTER > 0
    471  1.33   itojun 	if (ifp->if_bpf) {
    472   1.2   itojun 		/*
    473   1.2   itojun 		 * We need to prepend the address family as
    474   1.2   itojun 		 * a four byte field.  Cons up a dummy header
    475   1.2   itojun 		 * to pacify bpf.  This is safe because bpf
    476   1.2   itojun 		 * will only read from the mbuf (i.e., it won't
    477   1.2   itojun 		 * try to free it or keep a pointer a to it).
    478   1.2   itojun 		 */
    479   1.2   itojun 		struct mbuf m0;
    480  1.22   itojun 		u_int32_t af1 = af;
    481   1.2   itojun 
    482   1.2   itojun 		m0.m_next = m;
    483   1.2   itojun 		m0.m_len = 4;
    484  1.22   itojun 		m0.m_data = (char *)&af1;
    485   1.2   itojun 
    486   1.9   itojun #ifdef HAVE_OLD_BPF
    487  1.33   itojun 		bpf_mtap(ifp, &m0);
    488   1.9   itojun #else
    489  1.33   itojun 		bpf_mtap(ifp->if_bpf, &m0);
    490   1.9   itojun #endif
    491   1.2   itojun 	}
    492   1.2   itojun #endif /*NBPFILTER > 0*/
    493   1.2   itojun 
    494   1.2   itojun 	/*
    495   1.2   itojun 	 * Put the packet to the network layer input queue according to the
    496   1.2   itojun 	 * specified address family.
    497   1.2   itojun 	 * Note: older versions of gif_input directly called network layer
    498  1.31   itojun 	 * input functions, e.g. ip6_input, here.  We changed the policy to
    499   1.2   itojun 	 * prevent too many recursive calls of such input functions, which
    500  1.31   itojun 	 * might cause kernel panic.  But the change may introduce another
    501   1.2   itojun 	 * problem; if the input queue is full, packets are discarded.
    502  1.31   itojun 	 * The kernel stack overflow really happened, and we believed
    503  1.31   itojun 	 * queue-full rarely occurs, so we changed the policy.
    504   1.2   itojun 	 */
    505   1.2   itojun 	switch (af) {
    506   1.2   itojun #ifdef INET
    507   1.2   itojun 	case AF_INET:
    508   1.2   itojun 		ifq = &ipintrq;
    509   1.2   itojun 		isr = NETISR_IP;
    510   1.2   itojun 		break;
    511   1.2   itojun #endif
    512   1.2   itojun #ifdef INET6
    513   1.2   itojun 	case AF_INET6:
    514   1.2   itojun 		ifq = &ip6intrq;
    515   1.2   itojun 		isr = NETISR_IPV6;
    516   1.2   itojun 		break;
    517   1.2   itojun #endif
    518  1.24   itojun #ifdef ISO
    519  1.24   itojun 	case AF_ISO:
    520  1.33   itojun 		m = gif_eon_decap(ifp, m);
    521  1.31   itojun 		if (!m)
    522  1.24   itojun 			return;
    523  1.24   itojun 		ifq = &clnlintrq;
    524  1.24   itojun 		isr = NETISR_ISO;
    525  1.24   itojun 		break;
    526  1.24   itojun #endif
    527   1.2   itojun 	default:
    528   1.2   itojun 		m_freem(m);
    529   1.2   itojun 		return;
    530   1.2   itojun 	}
    531   1.2   itojun 
    532  1.27  thorpej 	s = splnet();
    533   1.2   itojun 	if (IF_QFULL(ifq)) {
    534   1.2   itojun 		IF_DROP(ifq);	/* update statistics */
    535   1.2   itojun 		m_freem(m);
    536   1.2   itojun 		splx(s);
    537   1.2   itojun 		return;
    538   1.2   itojun 	}
    539  1.33   itojun 	ifp->if_ipackets++;
    540  1.33   itojun 	ifp->if_ibytes += m->m_pkthdr.len;
    541   1.2   itojun 	IF_ENQUEUE(ifq, m);
    542   1.2   itojun 	/* we need schednetisr since the address family may change */
    543   1.2   itojun 	schednetisr(isr);
    544   1.2   itojun 	splx(s);
    545   1.2   itojun }
    546   1.2   itojun 
    547   1.9   itojun /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
    548   1.2   itojun int
    549   1.2   itojun gif_ioctl(ifp, cmd, data)
    550   1.2   itojun 	struct ifnet *ifp;
    551   1.2   itojun 	u_long cmd;
    552   1.2   itojun 	caddr_t data;
    553   1.2   itojun {
    554  1.17   martin 	struct proc *p = curproc;	/* XXX */
    555   1.2   itojun 	struct gif_softc *sc  = (struct gif_softc*)ifp;
    556   1.2   itojun 	struct ifreq     *ifr = (struct ifreq*)data;
    557   1.2   itojun 	int error = 0, size;
    558   1.9   itojun 	struct sockaddr *dst, *src;
    559  1.31   itojun 
    560   1.2   itojun 	switch (cmd) {
    561   1.2   itojun 	case SIOCSIFADDR:
    562  1.32   itojun 		ifp->if_flags |= IFF_UP;
    563   1.2   itojun 		break;
    564   1.2   itojun 
    565   1.2   itojun 	case SIOCSIFDSTADDR:
    566   1.2   itojun 		break;
    567   1.2   itojun 
    568   1.2   itojun 	case SIOCADDMULTI:
    569   1.2   itojun 	case SIOCDELMULTI:
    570  1.17   martin 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    571  1.17   martin 			break;
    572   1.2   itojun 		switch (ifr->ifr_addr.sa_family) {
    573   1.2   itojun #ifdef INET
    574   1.2   itojun 		case AF_INET:	/* IP supports Multicast */
    575   1.2   itojun 			break;
    576   1.2   itojun #endif /* INET */
    577   1.2   itojun #ifdef INET6
    578   1.2   itojun 		case AF_INET6:	/* IP6 supports Multicast */
    579   1.2   itojun 			break;
    580   1.2   itojun #endif /* INET6 */
    581   1.2   itojun 		default:  /* Other protocols doesn't support Multicast */
    582   1.2   itojun 			error = EAFNOSUPPORT;
    583   1.2   itojun 			break;
    584   1.2   itojun 		}
    585   1.2   itojun 		break;
    586   1.2   itojun 
    587   1.2   itojun #ifdef	SIOCSIFMTU /* xxx */
    588   1.2   itojun 	case SIOCGIFMTU:
    589   1.2   itojun 		break;
    590  1.11   itojun 
    591   1.2   itojun 	case SIOCSIFMTU:
    592   1.2   itojun 		{
    593   1.2   itojun 			u_long mtu;
    594  1.17   martin 			if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    595  1.17   martin 				break;
    596   1.2   itojun 			mtu = ifr->ifr_mtu;
    597   1.2   itojun 			if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
    598   1.2   itojun 				return (EINVAL);
    599   1.2   itojun 			}
    600   1.2   itojun 			ifp->if_mtu = mtu;
    601   1.2   itojun 		}
    602   1.2   itojun 		break;
    603   1.2   itojun #endif /* SIOCSIFMTU */
    604   1.2   itojun 
    605  1.31   itojun #ifdef INET
    606   1.2   itojun 	case SIOCSIFPHYADDR:
    607  1.31   itojun #endif
    608   1.2   itojun #ifdef INET6
    609   1.2   itojun 	case SIOCSIFPHYADDR_IN6:
    610   1.2   itojun #endif /* INET6 */
    611  1.25   itojun 	case SIOCSLIFPHYADDR:
    612  1.17   martin 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    613  1.17   martin 			break;
    614  1.11   itojun 		switch (cmd) {
    615  1.15   itojun #ifdef INET
    616  1.11   itojun 		case SIOCSIFPHYADDR:
    617  1.11   itojun 			src = (struct sockaddr *)
    618  1.11   itojun 				&(((struct in_aliasreq *)data)->ifra_addr);
    619  1.11   itojun 			dst = (struct sockaddr *)
    620  1.11   itojun 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
    621  1.11   itojun 			break;
    622  1.15   itojun #endif
    623  1.11   itojun #ifdef INET6
    624  1.11   itojun 		case SIOCSIFPHYADDR_IN6:
    625  1.11   itojun 			src = (struct sockaddr *)
    626  1.11   itojun 				&(((struct in6_aliasreq *)data)->ifra_addr);
    627  1.11   itojun 			dst = (struct sockaddr *)
    628  1.11   itojun 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    629  1.25   itojun 			break;
    630  1.25   itojun #endif
    631  1.25   itojun 		case SIOCSLIFPHYADDR:
    632  1.25   itojun 			src = (struct sockaddr *)
    633  1.25   itojun 				&(((struct if_laddrreq *)data)->addr);
    634  1.25   itojun 			dst = (struct sockaddr *)
    635  1.25   itojun 				&(((struct if_laddrreq *)data)->dstaddr);
    636  1.31   itojun 			break;
    637  1.31   itojun 		default:
    638  1.31   itojun 			return EINVAL;
    639  1.25   itojun 		}
    640  1.25   itojun 
    641  1.25   itojun 		/* sa_family must be equal */
    642  1.25   itojun 		if (src->sa_family != dst->sa_family)
    643  1.25   itojun 			return EINVAL;
    644  1.25   itojun 
    645  1.25   itojun 		/* validate sa_len */
    646  1.25   itojun 		switch (src->sa_family) {
    647  1.25   itojun #ifdef INET
    648  1.25   itojun 		case AF_INET:
    649  1.25   itojun 			if (src->sa_len != sizeof(struct sockaddr_in))
    650  1.16   itojun 				return EINVAL;
    651  1.11   itojun 			break;
    652  1.11   itojun #endif
    653  1.25   itojun #ifdef INET6
    654  1.25   itojun 		case AF_INET6:
    655  1.25   itojun 			if (src->sa_len != sizeof(struct sockaddr_in6))
    656  1.25   itojun 				return EINVAL;
    657  1.25   itojun 			break;
    658  1.25   itojun #endif
    659  1.25   itojun 		default:
    660  1.25   itojun 			return EAFNOSUPPORT;
    661  1.25   itojun 		}
    662  1.25   itojun 		switch (dst->sa_family) {
    663  1.25   itojun #ifdef INET
    664  1.25   itojun 		case AF_INET:
    665  1.25   itojun 			if (dst->sa_len != sizeof(struct sockaddr_in))
    666  1.25   itojun 				return EINVAL;
    667  1.25   itojun 			break;
    668  1.25   itojun #endif
    669  1.25   itojun #ifdef INET6
    670  1.25   itojun 		case AF_INET6:
    671  1.25   itojun 			if (dst->sa_len != sizeof(struct sockaddr_in6))
    672  1.25   itojun 				return EINVAL;
    673  1.25   itojun 			break;
    674  1.25   itojun #endif
    675  1.25   itojun 		default:
    676  1.25   itojun 			return EAFNOSUPPORT;
    677  1.25   itojun 		}
    678  1.25   itojun 
    679  1.25   itojun 		/* check sa_family looks sane for the cmd */
    680  1.25   itojun 		switch (cmd) {
    681  1.25   itojun 		case SIOCSIFPHYADDR:
    682  1.25   itojun 			if (src->sa_family == AF_INET)
    683  1.25   itojun 				break;
    684  1.25   itojun 			return EAFNOSUPPORT;
    685  1.25   itojun #ifdef INET6
    686  1.25   itojun 		case SIOCSIFPHYADDR_IN6:
    687  1.25   itojun 			if (src->sa_family == AF_INET6)
    688  1.25   itojun 				break;
    689  1.25   itojun 			return EAFNOSUPPORT;
    690  1.25   itojun #endif /* INET6 */
    691  1.25   itojun 		case SIOCSLIFPHYADDR:
    692  1.25   itojun 			/* checks done in the above */
    693  1.25   itojun 			break;
    694  1.11   itojun 		}
    695  1.11   itojun 
    696  1.31   itojun 		error = gif_set_tunnel(&sc->gif_if, src, dst);
    697   1.9   itojun 		break;
    698   1.9   itojun 
    699   1.9   itojun #ifdef SIOCDIFPHYADDR
    700   1.9   itojun 	case SIOCDIFPHYADDR:
    701  1.17   martin 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    702  1.17   martin 			break;
    703  1.31   itojun 		gif_delete_tunnel(&sc->gif_if);
    704   1.2   itojun 		break;
    705   1.9   itojun #endif
    706   1.2   itojun 
    707   1.2   itojun 	case SIOCGIFPSRCADDR:
    708   1.2   itojun #ifdef INET6
    709   1.2   itojun 	case SIOCGIFPSRCADDR_IN6:
    710   1.2   itojun #endif /* INET6 */
    711   1.2   itojun 		if (sc->gif_psrc == NULL) {
    712   1.2   itojun 			error = EADDRNOTAVAIL;
    713   1.2   itojun 			goto bad;
    714   1.2   itojun 		}
    715   1.2   itojun 		src = sc->gif_psrc;
    716  1.16   itojun 		switch (cmd) {
    717   1.2   itojun #ifdef INET
    718  1.16   itojun 		case SIOCGIFPSRCADDR:
    719   1.2   itojun 			dst = &ifr->ifr_addr;
    720  1.16   itojun 			size = sizeof(ifr->ifr_addr);
    721   1.2   itojun 			break;
    722   1.2   itojun #endif /* INET */
    723   1.2   itojun #ifdef INET6
    724  1.16   itojun 		case SIOCGIFPSRCADDR_IN6:
    725   1.2   itojun 			dst = (struct sockaddr *)
    726   1.2   itojun 				&(((struct in6_ifreq *)data)->ifr_addr);
    727  1.16   itojun 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    728   1.2   itojun 			break;
    729   1.2   itojun #endif /* INET6 */
    730   1.2   itojun 		default:
    731   1.2   itojun 			error = EADDRNOTAVAIL;
    732   1.2   itojun 			goto bad;
    733   1.2   itojun 		}
    734  1.16   itojun 		if (src->sa_len > size)
    735  1.16   itojun 			return EINVAL;
    736  1.16   itojun 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    737   1.2   itojun 		break;
    738   1.2   itojun 
    739   1.2   itojun 	case SIOCGIFPDSTADDR:
    740   1.2   itojun #ifdef INET6
    741   1.2   itojun 	case SIOCGIFPDSTADDR_IN6:
    742   1.2   itojun #endif /* INET6 */
    743   1.2   itojun 		if (sc->gif_pdst == NULL) {
    744   1.2   itojun 			error = EADDRNOTAVAIL;
    745   1.2   itojun 			goto bad;
    746   1.2   itojun 		}
    747   1.2   itojun 		src = sc->gif_pdst;
    748  1.16   itojun 		switch (cmd) {
    749   1.2   itojun #ifdef INET
    750  1.16   itojun 		case SIOCGIFPDSTADDR:
    751   1.2   itojun 			dst = &ifr->ifr_addr;
    752  1.16   itojun 			size = sizeof(ifr->ifr_addr);
    753   1.2   itojun 			break;
    754   1.2   itojun #endif /* INET */
    755   1.2   itojun #ifdef INET6
    756  1.16   itojun 		case SIOCGIFPDSTADDR_IN6:
    757   1.2   itojun 			dst = (struct sockaddr *)
    758   1.2   itojun 				&(((struct in6_ifreq *)data)->ifr_addr);
    759  1.16   itojun 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    760   1.2   itojun 			break;
    761   1.2   itojun #endif /* INET6 */
    762   1.2   itojun 		default:
    763   1.2   itojun 			error = EADDRNOTAVAIL;
    764   1.2   itojun 			goto bad;
    765   1.2   itojun 		}
    766  1.25   itojun 		if (src->sa_len > size)
    767  1.25   itojun 			return EINVAL;
    768  1.25   itojun 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    769  1.25   itojun 		break;
    770  1.25   itojun 
    771  1.25   itojun 	case SIOCGLIFPHYADDR:
    772  1.25   itojun 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    773  1.25   itojun 			error = EADDRNOTAVAIL;
    774  1.25   itojun 			goto bad;
    775  1.25   itojun 		}
    776  1.25   itojun 
    777  1.25   itojun 		/* copy src */
    778  1.25   itojun 		src = sc->gif_psrc;
    779  1.25   itojun 		dst = (struct sockaddr *)
    780  1.25   itojun 			&(((struct if_laddrreq *)data)->addr);
    781  1.25   itojun 		size = sizeof(((struct if_laddrreq *)data)->addr);
    782  1.25   itojun 		if (src->sa_len > size)
    783  1.25   itojun 			return EINVAL;
    784  1.25   itojun 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    785  1.25   itojun 
    786  1.25   itojun 		/* copy dst */
    787  1.25   itojun 		src = sc->gif_pdst;
    788  1.25   itojun 		dst = (struct sockaddr *)
    789  1.25   itojun 			&(((struct if_laddrreq *)data)->dstaddr);
    790  1.25   itojun 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
    791  1.16   itojun 		if (src->sa_len > size)
    792  1.16   itojun 			return EINVAL;
    793  1.16   itojun 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    794   1.2   itojun 		break;
    795   1.2   itojun 
    796   1.2   itojun 	case SIOCSIFFLAGS:
    797   1.9   itojun 		/* if_ioctl() takes care of it */
    798   1.2   itojun 		break;
    799   1.2   itojun 
    800   1.2   itojun 	default:
    801   1.2   itojun 		error = EINVAL;
    802   1.2   itojun 		break;
    803   1.2   itojun 	}
    804   1.2   itojun  bad:
    805   1.2   itojun 	return error;
    806  1.12  thorpej }
    807  1.12  thorpej 
    808  1.31   itojun int
    809  1.31   itojun gif_set_tunnel(ifp, src, dst)
    810  1.31   itojun 	struct ifnet *ifp;
    811  1.31   itojun 	struct sockaddr *src;
    812  1.31   itojun 	struct sockaddr *dst;
    813  1.31   itojun {
    814  1.31   itojun 	struct gif_softc *sc = (struct gif_softc *)ifp;
    815  1.31   itojun 	struct gif_softc *sc2;
    816  1.31   itojun 	struct sockaddr *osrc, *odst, *sa;
    817  1.31   itojun 	int s;
    818  1.31   itojun 	int error;
    819  1.31   itojun 
    820  1.31   itojun 	s = splsoftnet();
    821  1.31   itojun 
    822  1.31   itojun 	for (sc2 = LIST_FIRST(&gif_softc_list); sc2 != NULL;
    823  1.31   itojun 	     sc2 = LIST_NEXT(sc2, gif_list)) {
    824  1.31   itojun 		if (sc2 == sc)
    825  1.31   itojun 			continue;
    826  1.31   itojun 		if (!sc2->gif_pdst || !sc2->gif_psrc)
    827  1.31   itojun 			continue;
    828  1.31   itojun 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
    829  1.31   itojun 		    sc2->gif_pdst->sa_len != dst->sa_len ||
    830  1.31   itojun 		    sc2->gif_psrc->sa_family != src->sa_family ||
    831  1.31   itojun 		    sc2->gif_psrc->sa_len != src->sa_len)
    832  1.31   itojun 			continue;
    833  1.31   itojun 		/* can't configure same pair of address onto two gifs */
    834  1.31   itojun 		if (bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
    835  1.31   itojun 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
    836  1.31   itojun 			error = EADDRNOTAVAIL;
    837  1.31   itojun 			goto bad;
    838  1.31   itojun 		}
    839  1.31   itojun 
    840  1.31   itojun 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
    841  1.31   itojun 	}
    842  1.31   itojun 
    843  1.31   itojun 	/* XXX we can detach from both, but be polite just in case */
    844  1.31   itojun 	if (sc->gif_psrc)
    845  1.31   itojun 		switch (sc->gif_psrc->sa_family) {
    846  1.31   itojun #ifdef INET
    847  1.31   itojun 		case AF_INET:
    848  1.31   itojun 			(void)in_gif_detach(sc);
    849  1.31   itojun 			break;
    850  1.31   itojun #endif
    851  1.31   itojun #ifdef INET6
    852  1.31   itojun 		case AF_INET6:
    853  1.31   itojun 			(void)in6_gif_detach(sc);
    854  1.31   itojun 			break;
    855  1.31   itojun #endif
    856  1.31   itojun 		}
    857  1.31   itojun 
    858  1.33   itojun #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    859  1.33   itojun 	sc->gif_si = softintr_establish(IPL_SOFTNET, gifintr, sc);
    860  1.33   itojun 	if (sc->gif_si == NULL) {
    861  1.33   itojun 		error = ENOMEM;
    862  1.33   itojun 		goto bad;
    863  1.33   itojun 	}
    864  1.33   itojun #endif
    865  1.33   itojun 
    866  1.31   itojun 	osrc = sc->gif_psrc;
    867  1.31   itojun 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
    868  1.31   itojun 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
    869  1.31   itojun 	sc->gif_psrc = sa;
    870  1.31   itojun 
    871  1.31   itojun 	odst = sc->gif_pdst;
    872  1.31   itojun 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
    873  1.31   itojun 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
    874  1.31   itojun 	sc->gif_pdst = sa;
    875  1.31   itojun 
    876  1.31   itojun 	switch (sc->gif_psrc->sa_family) {
    877  1.31   itojun #ifdef INET
    878  1.31   itojun 	case AF_INET:
    879  1.31   itojun 		error = in_gif_attach(sc);
    880  1.31   itojun 		break;
    881  1.31   itojun #endif
    882  1.31   itojun #ifdef INET6
    883  1.31   itojun 	case AF_INET6:
    884  1.31   itojun 		error = in6_gif_attach(sc);
    885  1.31   itojun 		break;
    886  1.31   itojun #endif
    887  1.31   itojun 	}
    888  1.31   itojun 	if (error) {
    889  1.31   itojun 		/* rollback */
    890  1.31   itojun 		free((caddr_t)sc->gif_psrc, M_IFADDR);
    891  1.31   itojun 		free((caddr_t)sc->gif_pdst, M_IFADDR);
    892  1.31   itojun 		sc->gif_psrc = osrc;
    893  1.31   itojun 		sc->gif_pdst = odst;
    894  1.31   itojun 		goto bad;
    895  1.31   itojun 	}
    896  1.31   itojun 
    897  1.31   itojun 	if (osrc)
    898  1.31   itojun 		free((caddr_t)osrc, M_IFADDR);
    899  1.31   itojun 	if (odst)
    900  1.31   itojun 		free((caddr_t)odst, M_IFADDR);
    901  1.31   itojun 
    902  1.33   itojun 	if (sc->gif_psrc && sc->gif_pdst)
    903  1.33   itojun 		ifp->if_flags |= IFF_RUNNING;
    904  1.33   itojun 	else
    905  1.33   itojun 		ifp->if_flags &= ~IFF_RUNNING;
    906  1.33   itojun 	splx(s);
    907  1.33   itojun 
    908  1.33   itojun 	return 0;
    909  1.31   itojun 
    910  1.31   itojun  bad:
    911  1.33   itojun #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    912  1.35   itojun 	if (sc->gif_si) {
    913  1.33   itojun 		softintr_disestablish(sc->gif_si);
    914  1.35   itojun 		sc->gif_si = NULL;
    915  1.35   itojun 	}
    916  1.33   itojun #endif
    917  1.31   itojun 	if (sc->gif_psrc && sc->gif_pdst)
    918  1.31   itojun 		ifp->if_flags |= IFF_RUNNING;
    919  1.31   itojun 	else
    920  1.31   itojun 		ifp->if_flags &= ~IFF_RUNNING;
    921  1.31   itojun 	splx(s);
    922  1.31   itojun 
    923  1.31   itojun 	return error;
    924  1.31   itojun }
    925  1.31   itojun 
    926  1.12  thorpej void
    927  1.31   itojun gif_delete_tunnel(ifp)
    928  1.31   itojun 	struct ifnet *ifp;
    929  1.12  thorpej {
    930  1.31   itojun 	struct gif_softc *sc = (struct gif_softc *)ifp;
    931  1.12  thorpej 	int s;
    932  1.12  thorpej 
    933  1.12  thorpej 	s = splsoftnet();
    934  1.12  thorpej 
    935  1.33   itojun #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    936  1.35   itojun 	if (sc->gif_si) {
    937  1.33   itojun 		softintr_disestablish(sc->gif_si);
    938  1.35   itojun 		sc->gif_si = NULL;
    939  1.35   itojun 	}
    940  1.33   itojun #endif
    941  1.12  thorpej 	if (sc->gif_psrc) {
    942  1.12  thorpej 		free((caddr_t)sc->gif_psrc, M_IFADDR);
    943  1.12  thorpej 		sc->gif_psrc = NULL;
    944  1.12  thorpej 	}
    945  1.12  thorpej 	if (sc->gif_pdst) {
    946  1.12  thorpej 		free((caddr_t)sc->gif_pdst, M_IFADDR);
    947  1.12  thorpej 		sc->gif_pdst = NULL;
    948  1.12  thorpej 	}
    949  1.31   itojun 	/* it is safe to detach from both */
    950  1.31   itojun #ifdef INET
    951  1.31   itojun 	(void)in_gif_detach(sc);
    952  1.31   itojun #endif
    953  1.31   itojun #ifdef INET6
    954  1.31   itojun 	(void)in6_gif_detach(sc);
    955  1.31   itojun #endif
    956  1.12  thorpej 
    957  1.31   itojun 	if (sc->gif_psrc && sc->gif_pdst)
    958  1.31   itojun 		ifp->if_flags |= IFF_RUNNING;
    959  1.31   itojun 	else
    960  1.31   itojun 		ifp->if_flags &= ~IFF_RUNNING;
    961  1.12  thorpej 	splx(s);
    962   1.2   itojun }
    963  1.24   itojun 
    964  1.24   itojun #ifdef ISO
    965  1.24   itojun struct eonhdr {
    966  1.24   itojun 	u_int8_t version;
    967  1.24   itojun 	u_int8_t class;
    968  1.24   itojun 	u_int16_t cksum;
    969  1.26   itojun };
    970  1.24   itojun 
    971  1.24   itojun /*
    972  1.24   itojun  * prepend EON header to ISO PDU
    973  1.24   itojun  */
    974  1.31   itojun static struct mbuf *
    975  1.31   itojun gif_eon_encap(struct mbuf *m)
    976  1.24   itojun {
    977  1.24   itojun 	struct eonhdr *ehdr;
    978  1.24   itojun 
    979  1.31   itojun 	M_PREPEND(m, sizeof(*ehdr), M_DONTWAIT);
    980  1.31   itojun 	if (m && m->m_len < sizeof(*ehdr))
    981  1.31   itojun 		m = m_pullup(m, sizeof(*ehdr));
    982  1.31   itojun 	if (m == NULL)
    983  1.31   itojun 		return NULL;
    984  1.31   itojun 	ehdr = mtod(m, struct eonhdr *);
    985  1.24   itojun 	ehdr->version = 1;
    986  1.24   itojun 	ehdr->class = 0;		/* always unicast */
    987  1.24   itojun #if 0
    988  1.24   itojun 	/* calculate the checksum of the eonhdr */
    989  1.24   itojun 	{
    990  1.24   itojun 		struct mbuf mhead;
    991  1.24   itojun 		memset(&mhead, 0, sizeof(mhead));
    992  1.24   itojun 		ehdr->cksum = 0;
    993  1.24   itojun 		mhead.m_data = (caddr_t)ehdr;
    994  1.24   itojun 		mhead.m_len = sizeof(*ehdr);
    995  1.24   itojun 		mhead.m_next = 0;
    996  1.24   itojun 		iso_gen_csum(&mhead, offsetof(struct eonhdr, cksum),
    997  1.24   itojun 		    mhead.m_len);
    998  1.24   itojun 	}
    999  1.24   itojun #else
   1000  1.24   itojun 	/* since the data is always constant we'll just plug the value in */
   1001  1.24   itojun 	ehdr->cksum = htons(0xfc02);
   1002  1.24   itojun #endif
   1003  1.31   itojun 	return m;
   1004  1.24   itojun }
   1005  1.24   itojun 
   1006  1.24   itojun /*
   1007  1.24   itojun  * remove EON header and check checksum
   1008  1.24   itojun  */
   1009  1.31   itojun static struct mbuf *
   1010  1.33   itojun gif_eon_decap(struct ifnet *ifp, struct mbuf *m)
   1011  1.24   itojun {
   1012  1.24   itojun 	struct eonhdr *ehdr;
   1013  1.24   itojun 
   1014  1.31   itojun 	if (m->m_len < sizeof(*ehdr) &&
   1015  1.31   itojun 	    (m = m_pullup(m, sizeof(*ehdr))) == NULL) {
   1016  1.33   itojun 		ifp->if_ierrors++;
   1017  1.31   itojun 		return NULL;
   1018  1.24   itojun 	}
   1019  1.31   itojun 	if (iso_check_csum(m, sizeof(struct eonhdr))) {
   1020  1.31   itojun 		m_freem(m);
   1021  1.31   itojun 		return NULL;
   1022  1.31   itojun 	}
   1023  1.31   itojun 	m_adj(m, sizeof(*ehdr));
   1024  1.31   itojun 	return m;
   1025  1.24   itojun }
   1026  1.24   itojun #endif /*ISO*/
   1027   1.2   itojun #endif /*NGIF > 0*/
   1028