Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.104
      1  1.104  knakahar /*	$NetBSD: if_gif.c,v 1.104 2016/01/08 03:55:39 knakahara 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.36     lukem 
     33   1.36     lukem #include <sys/cdefs.h>
     34  1.104  knakahar __KERNEL_RCSID(0, "$NetBSD: if_gif.c,v 1.104 2016/01/08 03:55:39 knakahara Exp $");
     35    1.2    itojun 
     36   1.88     pooka #ifdef _KERNEL_OPT
     37    1.2    itojun #include "opt_inet.h"
     38   1.88     pooka #endif
     39    1.2    itojun 
     40    1.2    itojun #include <sys/param.h>
     41    1.2    itojun #include <sys/systm.h>
     42    1.2    itojun #include <sys/kernel.h>
     43    1.2    itojun #include <sys/mbuf.h>
     44    1.2    itojun #include <sys/socket.h>
     45    1.2    itojun #include <sys/sockio.h>
     46    1.2    itojun #include <sys/errno.h>
     47    1.2    itojun #include <sys/ioctl.h>
     48    1.2    itojun #include <sys/time.h>
     49   1.86    martin #include <sys/socketvar.h>
     50    1.2    itojun #include <sys/syslog.h>
     51   1.17    martin #include <sys/proc.h>
     52    1.9    itojun #include <sys/protosw.h>
     53   1.72        ad #include <sys/cpu.h>
     54   1.72        ad #include <sys/intr.h>
     55   1.98  knakahar #include <sys/kmem.h>
     56  1.104  knakahar #include <sys/sysctl.h>
     57    1.2    itojun 
     58    1.2    itojun #include <net/if.h>
     59    1.2    itojun #include <net/if_types.h>
     60    1.2    itojun #include <net/netisr.h>
     61    1.2    itojun #include <net/route.h>
     62    1.2    itojun #include <net/bpf.h>
     63    1.2    itojun 
     64    1.2    itojun #include <netinet/in.h>
     65    1.2    itojun #include <netinet/in_systm.h>
     66   1.15    itojun #include <netinet/ip.h>
     67   1.15    itojun #ifdef	INET
     68    1.2    itojun #include <netinet/in_var.h>
     69   1.57  christos #endif	/* INET */
     70    1.2    itojun #include <netinet/in_gif.h>
     71    1.2    itojun 
     72    1.2    itojun #ifdef INET6
     73    1.2    itojun #ifndef INET
     74    1.2    itojun #include <netinet/in.h>
     75    1.2    itojun #endif
     76    1.2    itojun #include <netinet6/in6_var.h>
     77    1.2    itojun #include <netinet/ip6.h>
     78    1.2    itojun #include <netinet6/ip6_var.h>
     79    1.2    itojun #include <netinet6/in6_gif.h>
     80    1.9    itojun #include <netinet6/ip6protosw.h>
     81    1.2    itojun #endif /* INET6 */
     82    1.2    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.87  christos #include <net/net_osdep.h>
     87    1.2    itojun 
     88   1.87  christos #include "ioconf.h"
     89    1.4    itojun 
     90   1.56   thorpej static void	gifintr(void *);
     91    1.2    itojun 
     92    1.2    itojun /*
     93    1.2    itojun  * gif global variable definitions
     94    1.2    itojun  */
     95   1.56   thorpej LIST_HEAD(, gif_softc) gif_softc_list;	/* XXX should be static */
     96   1.12   thorpej 
     97  1.104  knakahar static void	gif_sysctl_setup(struct sysctllog **);
     98  1.104  knakahar 
     99   1.56   thorpej static int	gif_clone_create(struct if_clone *, int);
    100   1.56   thorpej static int	gif_clone_destroy(struct ifnet *);
    101   1.95  knakahar static int	gif_check_nesting(struct ifnet *, struct mbuf *);
    102   1.12   thorpej 
    103   1.97  knakahar static int	gif_encap_attach(struct gif_softc *);
    104   1.97  knakahar static int	gif_encap_detach(struct gif_softc *);
    105   1.97  knakahar 
    106   1.56   thorpej static struct if_clone gif_cloner =
    107   1.12   thorpej     IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
    108   1.12   thorpej 
    109    1.9    itojun #ifndef MAX_GIF_NEST
    110    1.9    itojun /*
    111    1.9    itojun  * This macro controls the upper limitation on nesting of gif tunnels.
    112    1.9    itojun  * Since, setting a large value to this macro with a careless configuration
    113    1.9    itojun  * may introduce system crash, we don't allow any nestings by default.
    114    1.9    itojun  * If you need to configure nested gif tunnels, you can define this macro
    115   1.31    itojun  * in your kernel configuration file.  However, if you do so, please be
    116    1.9    itojun  * careful to configure the tunnels so that it won't make a loop.
    117    1.9    itojun  */
    118    1.9    itojun #define MAX_GIF_NEST 1
    119    1.9    itojun #endif
    120    1.9    itojun static int max_gif_nesting = MAX_GIF_NEST;
    121    1.2    itojun 
    122  1.104  knakahar static void
    123  1.104  knakahar gif_sysctl_setup(struct sysctllog **clog)
    124  1.104  knakahar {
    125  1.104  knakahar 
    126  1.104  knakahar #ifdef INET
    127  1.104  knakahar 	sysctl_createv(clog, 0, NULL, NULL,
    128  1.104  knakahar 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    129  1.104  knakahar 		       CTLTYPE_INT, "gifttl",
    130  1.104  knakahar 		       SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
    131  1.104  knakahar 		       NULL, 0, &ip_gif_ttl, 0,
    132  1.104  knakahar 		       CTL_NET, PF_INET, IPPROTO_IP,
    133  1.104  knakahar 		       IPCTL_GIF_TTL, CTL_EOL);
    134  1.104  knakahar #endif
    135  1.104  knakahar #ifdef INET6
    136  1.104  knakahar 	sysctl_createv(clog, 0, NULL, NULL,
    137  1.104  knakahar 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    138  1.104  knakahar 		       CTLTYPE_INT, "gifhlim",
    139  1.104  knakahar 		       SYSCTL_DESCR("Default hop limit for a gif tunnel datagram"),
    140  1.104  knakahar 		       NULL, 0, &ip6_gif_hlim, 0,
    141  1.104  knakahar 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
    142  1.104  knakahar 		       IPV6CTL_GIF_HLIM, CTL_EOL);
    143  1.104  knakahar #endif
    144  1.104  knakahar }
    145  1.104  knakahar 
    146   1.12   thorpej /* ARGSUSED */
    147    1.2    itojun void
    148   1.63  christos gifattach(int count)
    149   1.12   thorpej {
    150   1.12   thorpej 
    151   1.12   thorpej 	LIST_INIT(&gif_softc_list);
    152   1.12   thorpej 	if_clone_attach(&gif_cloner);
    153  1.104  knakahar 
    154  1.104  knakahar 	gif_sysctl_setup(NULL);
    155   1.12   thorpej }
    156   1.12   thorpej 
    157   1.56   thorpej static int
    158   1.56   thorpej gif_clone_create(struct if_clone *ifc, int unit)
    159    1.2    itojun {
    160   1.12   thorpej 	struct gif_softc *sc;
    161   1.12   thorpej 
    162   1.98  knakahar 	sc = kmem_zalloc(sizeof(struct gif_softc), KM_SLEEP);
    163  1.101  knakahar 	if (sc == NULL)
    164  1.101  knakahar 		return ENOMEM;
    165    1.2    itojun 
    166   1.75  christos 	if_initname(&sc->gif_if, ifc->ifc_name, unit);
    167    1.9    itojun 
    168   1.31    itojun 	gifattach0(sc);
    169   1.31    itojun 
    170   1.31    itojun 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
    171   1.31    itojun 	return (0);
    172   1.31    itojun }
    173   1.31    itojun 
    174   1.31    itojun void
    175   1.56   thorpej gifattach0(struct gif_softc *sc)
    176   1.31    itojun {
    177   1.31    itojun 
    178   1.12   thorpej 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
    179    1.9    itojun 
    180   1.31    itojun 	sc->gif_if.if_addrlen = 0;
    181   1.12   thorpej 	sc->gif_if.if_mtu    = GIF_MTU;
    182   1.12   thorpej 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
    183   1.12   thorpej 	sc->gif_if.if_ioctl  = gif_ioctl;
    184   1.12   thorpej 	sc->gif_if.if_output = gif_output;
    185   1.12   thorpej 	sc->gif_if.if_type   = IFT_GIF;
    186   1.19   thorpej 	sc->gif_if.if_dlt    = DLT_NULL;
    187   1.76    dyoung 	sc->gif_if.if_softc  = sc;
    188   1.34    itojun 	IFQ_SET_READY(&sc->gif_if.if_snd);
    189   1.12   thorpej 	if_attach(&sc->gif_if);
    190   1.20   thorpej 	if_alloc_sadl(&sc->gif_if);
    191   1.78     joerg 	bpf_attach(&sc->gif_if, DLT_NULL, sizeof(u_int));
    192   1.12   thorpej }
    193   1.12   thorpej 
    194   1.56   thorpej static int
    195   1.56   thorpej gif_clone_destroy(struct ifnet *ifp)
    196   1.12   thorpej {
    197   1.12   thorpej 	struct gif_softc *sc = (void *) ifp;
    198   1.12   thorpej 
    199   1.12   thorpej 	LIST_REMOVE(sc, gif_list);
    200   1.12   thorpej 
    201   1.94  knakahar 	gif_delete_tunnel(&sc->gif_if);
    202   1.78     joerg 	bpf_detach(ifp);
    203   1.69    dyoung 	if_detach(ifp);
    204   1.66    dyoung 	rtcache_free(&sc->gif_ro);
    205   1.12   thorpej 
    206   1.98  knakahar 	kmem_free(sc, sizeof(struct gif_softc));
    207   1.47     peter 
    208   1.47     peter 	return (0);
    209    1.9    itojun }
    210    1.9    itojun 
    211   1.42    itojun #ifdef GIF_ENCAPCHECK
    212   1.31    itojun int
    213   1.56   thorpej gif_encapcheck(struct mbuf *m, int off, int proto, void *arg)
    214    1.9    itojun {
    215    1.9    itojun 	struct ip ip;
    216    1.9    itojun 	struct gif_softc *sc;
    217    1.9    itojun 
    218   1.76    dyoung 	sc = arg;
    219    1.9    itojun 	if (sc == NULL)
    220    1.9    itojun 		return 0;
    221    1.9    itojun 
    222    1.9    itojun 	if ((sc->gif_if.if_flags & IFF_UP) == 0)
    223    1.9    itojun 		return 0;
    224    1.9    itojun 
    225    1.9    itojun 	/* no physical address */
    226    1.9    itojun 	if (!sc->gif_psrc || !sc->gif_pdst)
    227    1.9    itojun 		return 0;
    228    1.9    itojun 
    229    1.9    itojun 	switch (proto) {
    230    1.9    itojun #ifdef INET
    231    1.9    itojun 	case IPPROTO_IPV4:
    232    1.9    itojun 		break;
    233    1.9    itojun #endif
    234    1.9    itojun #ifdef INET6
    235    1.9    itojun 	case IPPROTO_IPV6:
    236    1.9    itojun 		break;
    237    1.9    itojun #endif
    238    1.9    itojun 	default:
    239    1.9    itojun 		return 0;
    240    1.9    itojun 	}
    241   1.40  christos 
    242   1.40  christos 	/* Bail on short packets */
    243   1.40  christos 	KASSERT(m->m_flags & M_PKTHDR);
    244   1.40  christos 	if (m->m_pkthdr.len < sizeof(ip))
    245   1.40  christos 		return 0;
    246    1.9    itojun 
    247   1.76    dyoung 	m_copydata(m, 0, sizeof(ip), &ip);
    248    1.9    itojun 
    249    1.9    itojun 	switch (ip.ip_v) {
    250    1.9    itojun #ifdef INET
    251    1.9    itojun 	case 4:
    252    1.9    itojun 		if (sc->gif_psrc->sa_family != AF_INET ||
    253    1.9    itojun 		    sc->gif_pdst->sa_family != AF_INET)
    254    1.9    itojun 			return 0;
    255    1.9    itojun 		return gif_encapcheck4(m, off, proto, arg);
    256    1.9    itojun #endif
    257    1.9    itojun #ifdef INET6
    258    1.9    itojun 	case 6:
    259   1.41    itojun 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
    260   1.41    itojun 			return 0;
    261    1.9    itojun 		if (sc->gif_psrc->sa_family != AF_INET6 ||
    262    1.9    itojun 		    sc->gif_pdst->sa_family != AF_INET6)
    263    1.9    itojun 			return 0;
    264    1.9    itojun 		return gif_encapcheck6(m, off, proto, arg);
    265    1.9    itojun #endif
    266    1.9    itojun 	default:
    267    1.9    itojun 		return 0;
    268    1.2    itojun 	}
    269    1.2    itojun }
    270   1.42    itojun #endif
    271    1.2    itojun 
    272   1.95  knakahar /*
    273   1.95  knakahar  * gif may cause infinite recursion calls when misconfigured.
    274   1.95  knakahar  * We'll prevent this by introducing upper limit.
    275   1.95  knakahar  */
    276   1.95  knakahar static int
    277   1.95  knakahar gif_check_nesting(struct ifnet *ifp, struct mbuf *m)
    278   1.95  knakahar {
    279   1.95  knakahar 	struct m_tag *mtag;
    280   1.95  knakahar 	int *count;
    281   1.95  knakahar 
    282   1.95  knakahar 	mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO, NULL);
    283   1.95  knakahar 	if (mtag != NULL) {
    284   1.95  knakahar 		count = (int *)(mtag + 1);
    285   1.95  knakahar 		if (++(*count) > max_gif_nesting) {
    286   1.95  knakahar 			log(LOG_NOTICE,
    287   1.95  knakahar 			    "%s: recursively called too many times(%d)\n",
    288   1.95  knakahar 			    if_name(ifp),
    289   1.95  knakahar 			    *count);
    290   1.95  knakahar 			return EIO;
    291   1.95  knakahar 		}
    292   1.95  knakahar 	} else {
    293   1.95  knakahar 		mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count),
    294   1.95  knakahar 		    M_NOWAIT);
    295   1.95  knakahar 		if (mtag != NULL) {
    296   1.95  knakahar 			m_tag_prepend(m, mtag);
    297   1.95  knakahar 			count = (int *)(mtag + 1);
    298   1.95  knakahar 			*count = 0;
    299   1.95  knakahar 		} else {
    300   1.95  knakahar 			log(LOG_DEBUG,
    301   1.95  knakahar 			    "%s: m_tag_get() failed, recursion calls are not prevented.\n",
    302   1.95  knakahar 			    if_name(ifp));
    303   1.95  knakahar 		}
    304   1.95  knakahar 	}
    305   1.95  knakahar 
    306   1.95  knakahar 	return 0;
    307   1.95  knakahar }
    308   1.95  knakahar 
    309    1.2    itojun int
    310   1.65    dyoung gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    311   1.63  christos     struct rtentry *rt)
    312    1.2    itojun {
    313   1.76    dyoung 	struct gif_softc *sc = ifp->if_softc;
    314    1.2    itojun 	int error = 0;
    315   1.39    itojun 	ALTQ_DECL(struct altq_pktattr pktattr;)
    316   1.33    itojun 	int s;
    317   1.33    itojun 
    318   1.33    itojun 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    319    1.2    itojun 
    320   1.95  knakahar 	if ((error = gif_check_nesting(ifp, m)) != 0) {
    321   1.95  knakahar 		m_free(m);
    322    1.2    itojun 		goto end;
    323    1.2    itojun 	}
    324    1.2    itojun 
    325    1.2    itojun 	m->m_flags &= ~(M_BCAST|M_MCAST);
    326    1.2    itojun 	if (!(ifp->if_flags & IFF_UP) ||
    327  1.102  knakahar 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL ||
    328  1.102  knakahar 	    sc->gif_si == NULL) {
    329    1.2    itojun 		m_freem(m);
    330    1.2    itojun 		error = ENETDOWN;
    331    1.2    itojun 		goto end;
    332    1.2    itojun 	}
    333    1.2    itojun 
    334    1.9    itojun 	/* XXX should we check if our outer source is legal? */
    335    1.2    itojun 
    336   1.33    itojun 	/* use DLT_NULL encapsulation here to pass inner af type */
    337   1.33    itojun 	M_PREPEND(m, sizeof(int), M_DONTWAIT);
    338   1.33    itojun 	if (!m) {
    339   1.33    itojun 		error = ENOBUFS;
    340   1.33    itojun 		goto end;
    341   1.33    itojun 	}
    342   1.33    itojun 	*mtod(m, int *) = dst->sa_family;
    343   1.33    itojun 
    344   1.79    dyoung 	/* Clear checksum-offload flags. */
    345   1.79    dyoung 	m->m_pkthdr.csum_flags = 0;
    346   1.79    dyoung 	m->m_pkthdr.csum_data = 0;
    347   1.79    dyoung 
    348   1.33    itojun 	s = splnet();
    349   1.33    itojun 	IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    350   1.33    itojun 	if (error) {
    351   1.33    itojun 		splx(s);
    352   1.31    itojun 		goto end;
    353    1.2    itojun 	}
    354  1.102  knakahar 
    355  1.102  knakahar 	/* softint_schedule() must be called with kpreempt_disabled() */
    356  1.102  knakahar 	softint_schedule(sc->gif_si);
    357   1.33    itojun 	splx(s);
    358   1.33    itojun 
    359   1.33    itojun 	error = 0;
    360    1.2    itojun 
    361    1.2    itojun   end:
    362   1.31    itojun 	if (error)
    363   1.31    itojun 		ifp->if_oerrors++;
    364    1.2    itojun 	return error;
    365    1.2    itojun }
    366    1.2    itojun 
    367   1.56   thorpej static void
    368   1.56   thorpej gifintr(void *arg)
    369   1.33    itojun {
    370   1.33    itojun 	struct gif_softc *sc;
    371   1.33    itojun 	struct ifnet *ifp;
    372   1.33    itojun 	struct mbuf *m;
    373   1.33    itojun 	int family;
    374   1.33    itojun 	int len;
    375   1.33    itojun 	int s;
    376   1.33    itojun 	int error;
    377   1.33    itojun 
    378   1.76    dyoung 	sc = arg;
    379   1.33    itojun 	ifp = &sc->gif_if;
    380   1.33    itojun 
    381  1.102  knakahar 	/*
    382  1.102  knakahar 	 * other CPUs does {set,delete}_tunnel after curcpu have done
    383  1.102  knakahar 	 * softint_schedule().
    384  1.102  knakahar 	 */
    385  1.102  knakahar 	if (sc->gif_pdst == NULL || sc->gif_psrc == NULL) {
    386  1.102  knakahar 		IFQ_PURGE(&ifp->if_snd);
    387  1.102  knakahar 		return;
    388  1.102  knakahar 	}
    389  1.102  knakahar 
    390   1.33    itojun 	/* output processing */
    391   1.33    itojun 	while (1) {
    392   1.33    itojun 		s = splnet();
    393   1.34    itojun 		IFQ_DEQUEUE(&sc->gif_if.if_snd, m);
    394   1.33    itojun 		splx(s);
    395   1.33    itojun 		if (m == NULL)
    396   1.33    itojun 			break;
    397   1.33    itojun 
    398   1.33    itojun 		/* grab and chop off inner af type */
    399   1.33    itojun 		if (sizeof(int) > m->m_len) {
    400   1.33    itojun 			m = m_pullup(m, sizeof(int));
    401   1.33    itojun 			if (!m) {
    402   1.33    itojun 				ifp->if_oerrors++;
    403   1.33    itojun 				continue;
    404   1.33    itojun 			}
    405   1.33    itojun 		}
    406   1.33    itojun 		family = *mtod(m, int *);
    407   1.78     joerg 		bpf_mtap(ifp, m);
    408   1.33    itojun 		m_adj(m, sizeof(int));
    409   1.33    itojun 
    410   1.33    itojun 		len = m->m_pkthdr.len;
    411   1.33    itojun 
    412   1.33    itojun 		/* dispatch to output logic based on outer AF */
    413   1.33    itojun 		switch (sc->gif_psrc->sa_family) {
    414   1.33    itojun #ifdef INET
    415   1.33    itojun 		case AF_INET:
    416   1.85  hsuenaga 			mutex_enter(softnet_lock);
    417   1.33    itojun 			error = in_gif_output(ifp, family, m);
    418   1.85  hsuenaga 			mutex_exit(softnet_lock);
    419   1.33    itojun 			break;
    420   1.33    itojun #endif
    421   1.33    itojun #ifdef INET6
    422   1.33    itojun 		case AF_INET6:
    423   1.85  hsuenaga 			mutex_enter(softnet_lock);
    424   1.33    itojun 			error = in6_gif_output(ifp, family, m);
    425   1.85  hsuenaga 			mutex_exit(softnet_lock);
    426   1.33    itojun 			break;
    427   1.33    itojun #endif
    428   1.33    itojun 		default:
    429   1.50     perry 			m_freem(m);
    430   1.33    itojun 			error = ENETDOWN;
    431   1.33    itojun 			break;
    432   1.33    itojun 		}
    433   1.33    itojun 
    434   1.33    itojun 		if (error)
    435   1.33    itojun 			ifp->if_oerrors++;
    436   1.33    itojun 		else {
    437   1.50     perry 			ifp->if_opackets++;
    438   1.33    itojun 			ifp->if_obytes += len;
    439   1.33    itojun 		}
    440   1.33    itojun 	}
    441   1.33    itojun }
    442   1.33    itojun 
    443   1.33    itojun void
    444   1.56   thorpej gif_input(struct mbuf *m, int af, struct ifnet *ifp)
    445    1.2    itojun {
    446   1.83     rmind 	pktqueue_t *pktq;
    447   1.83     rmind 	size_t pktlen;
    448   1.83     rmind 	int s;
    449    1.2    itojun 
    450   1.33    itojun 	if (ifp == NULL) {
    451    1.2    itojun 		/* just in case */
    452    1.2    itojun 		m_freem(m);
    453    1.2    itojun 		return;
    454    1.2    itojun 	}
    455    1.2    itojun 
    456   1.33    itojun 	m->m_pkthdr.rcvif = ifp;
    457   1.83     rmind 	pktlen = m->m_pkthdr.len;
    458   1.50     perry 
    459   1.78     joerg 	bpf_mtap_af(ifp, af, m);
    460    1.2    itojun 
    461    1.2    itojun 	/*
    462    1.2    itojun 	 * Put the packet to the network layer input queue according to the
    463   1.83     rmind 	 * specified address family.  Note: we avoid direct call to the
    464   1.83     rmind 	 * input function of the network layer in order to avoid recursion.
    465   1.83     rmind 	 * This may be revisited in the future.
    466    1.2    itojun 	 */
    467    1.2    itojun 	switch (af) {
    468    1.2    itojun #ifdef INET
    469    1.2    itojun 	case AF_INET:
    470   1.83     rmind 		pktq = ip_pktq;
    471    1.2    itojun 		break;
    472    1.2    itojun #endif
    473    1.2    itojun #ifdef INET6
    474    1.2    itojun 	case AF_INET6:
    475   1.83     rmind 		pktq = ip6_pktq;
    476    1.2    itojun 		break;
    477    1.2    itojun #endif
    478    1.2    itojun 	default:
    479    1.2    itojun 		m_freem(m);
    480    1.2    itojun 		return;
    481    1.2    itojun 	}
    482    1.2    itojun 
    483   1.27   thorpej 	s = splnet();
    484   1.83     rmind 	if (__predict_true(pktq_enqueue(pktq, m, 0))) {
    485   1.83     rmind 		ifp->if_ibytes += pktlen;
    486   1.83     rmind 		ifp->if_ipackets++;
    487   1.83     rmind 	} else {
    488    1.2    itojun 		m_freem(m);
    489    1.2    itojun 	}
    490    1.2    itojun 	splx(s);
    491    1.2    itojun }
    492    1.2    itojun 
    493    1.9    itojun /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
    494    1.2    itojun int
    495   1.67  christos gif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    496    1.2    itojun {
    497   1.76    dyoung 	struct gif_softc *sc  = ifp->if_softc;
    498    1.2    itojun 	struct ifreq     *ifr = (struct ifreq*)data;
    499   1.84       roy 	struct ifaddr    *ifa = (struct ifaddr*)data;
    500    1.2    itojun 	int error = 0, size;
    501    1.9    itojun 	struct sockaddr *dst, *src;
    502   1.31    itojun 
    503    1.2    itojun 	switch (cmd) {
    504   1.76    dyoung 	case SIOCINITIFADDR:
    505   1.32    itojun 		ifp->if_flags |= IFF_UP;
    506   1.84       roy 		ifa->ifa_rtrequest = p2p_rtrequest;
    507    1.2    itojun 		break;
    508   1.50     perry 
    509    1.2    itojun 	case SIOCADDMULTI:
    510    1.2    itojun 	case SIOCDELMULTI:
    511    1.2    itojun 		switch (ifr->ifr_addr.sa_family) {
    512    1.2    itojun #ifdef INET
    513    1.2    itojun 		case AF_INET:	/* IP supports Multicast */
    514    1.2    itojun 			break;
    515    1.2    itojun #endif /* INET */
    516    1.2    itojun #ifdef INET6
    517    1.2    itojun 		case AF_INET6:	/* IP6 supports Multicast */
    518    1.2    itojun 			break;
    519    1.2    itojun #endif /* INET6 */
    520    1.2    itojun 		default:  /* Other protocols doesn't support Multicast */
    521    1.2    itojun 			error = EAFNOSUPPORT;
    522    1.2    itojun 			break;
    523    1.2    itojun 		}
    524    1.2    itojun 		break;
    525    1.2    itojun 
    526    1.2    itojun 	case SIOCSIFMTU:
    527   1.73    dyoung 		if (ifr->ifr_mtu < GIF_MTU_MIN || ifr->ifr_mtu > GIF_MTU_MAX)
    528   1.73    dyoung 			return EINVAL;
    529   1.73    dyoung 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    530   1.73    dyoung 			error = 0;
    531    1.2    itojun 		break;
    532    1.2    itojun 
    533   1.31    itojun #ifdef INET
    534    1.2    itojun 	case SIOCSIFPHYADDR:
    535   1.31    itojun #endif
    536    1.2    itojun #ifdef INET6
    537    1.2    itojun 	case SIOCSIFPHYADDR_IN6:
    538    1.2    itojun #endif /* INET6 */
    539   1.25    itojun 	case SIOCSLIFPHYADDR:
    540   1.11    itojun 		switch (cmd) {
    541   1.15    itojun #ifdef INET
    542   1.11    itojun 		case SIOCSIFPHYADDR:
    543   1.11    itojun 			src = (struct sockaddr *)
    544   1.11    itojun 				&(((struct in_aliasreq *)data)->ifra_addr);
    545   1.11    itojun 			dst = (struct sockaddr *)
    546   1.11    itojun 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
    547   1.11    itojun 			break;
    548   1.15    itojun #endif
    549   1.11    itojun #ifdef INET6
    550   1.11    itojun 		case SIOCSIFPHYADDR_IN6:
    551   1.11    itojun 			src = (struct sockaddr *)
    552   1.11    itojun 				&(((struct in6_aliasreq *)data)->ifra_addr);
    553   1.11    itojun 			dst = (struct sockaddr *)
    554   1.11    itojun 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    555   1.25    itojun 			break;
    556   1.25    itojun #endif
    557   1.25    itojun 		case SIOCSLIFPHYADDR:
    558   1.25    itojun 			src = (struct sockaddr *)
    559   1.25    itojun 				&(((struct if_laddrreq *)data)->addr);
    560   1.25    itojun 			dst = (struct sockaddr *)
    561   1.25    itojun 				&(((struct if_laddrreq *)data)->dstaddr);
    562   1.31    itojun 			break;
    563   1.31    itojun 		default:
    564   1.31    itojun 			return EINVAL;
    565   1.25    itojun 		}
    566   1.25    itojun 
    567   1.25    itojun 		/* sa_family must be equal */
    568   1.25    itojun 		if (src->sa_family != dst->sa_family)
    569   1.25    itojun 			return EINVAL;
    570   1.25    itojun 
    571   1.25    itojun 		/* validate sa_len */
    572   1.25    itojun 		switch (src->sa_family) {
    573   1.25    itojun #ifdef INET
    574   1.25    itojun 		case AF_INET:
    575   1.25    itojun 			if (src->sa_len != sizeof(struct sockaddr_in))
    576   1.16    itojun 				return EINVAL;
    577   1.11    itojun 			break;
    578   1.11    itojun #endif
    579   1.25    itojun #ifdef INET6
    580   1.25    itojun 		case AF_INET6:
    581   1.25    itojun 			if (src->sa_len != sizeof(struct sockaddr_in6))
    582   1.25    itojun 				return EINVAL;
    583   1.25    itojun 			break;
    584   1.25    itojun #endif
    585   1.25    itojun 		default:
    586   1.25    itojun 			return EAFNOSUPPORT;
    587   1.25    itojun 		}
    588   1.25    itojun 		switch (dst->sa_family) {
    589   1.25    itojun #ifdef INET
    590   1.25    itojun 		case AF_INET:
    591   1.25    itojun 			if (dst->sa_len != sizeof(struct sockaddr_in))
    592   1.25    itojun 				return EINVAL;
    593   1.25    itojun 			break;
    594   1.25    itojun #endif
    595   1.25    itojun #ifdef INET6
    596   1.25    itojun 		case AF_INET6:
    597   1.25    itojun 			if (dst->sa_len != sizeof(struct sockaddr_in6))
    598   1.25    itojun 				return EINVAL;
    599   1.25    itojun 			break;
    600   1.25    itojun #endif
    601   1.25    itojun 		default:
    602   1.25    itojun 			return EAFNOSUPPORT;
    603   1.25    itojun 		}
    604   1.25    itojun 
    605   1.25    itojun 		/* check sa_family looks sane for the cmd */
    606   1.25    itojun 		switch (cmd) {
    607   1.25    itojun 		case SIOCSIFPHYADDR:
    608   1.25    itojun 			if (src->sa_family == AF_INET)
    609   1.25    itojun 				break;
    610   1.25    itojun 			return EAFNOSUPPORT;
    611   1.25    itojun #ifdef INET6
    612   1.25    itojun 		case SIOCSIFPHYADDR_IN6:
    613   1.25    itojun 			if (src->sa_family == AF_INET6)
    614   1.25    itojun 				break;
    615   1.25    itojun 			return EAFNOSUPPORT;
    616   1.25    itojun #endif /* INET6 */
    617   1.25    itojun 		case SIOCSLIFPHYADDR:
    618   1.25    itojun 			/* checks done in the above */
    619   1.25    itojun 			break;
    620   1.11    itojun 		}
    621   1.11    itojun 
    622   1.31    itojun 		error = gif_set_tunnel(&sc->gif_if, src, dst);
    623    1.9    itojun 		break;
    624    1.9    itojun 
    625    1.9    itojun #ifdef SIOCDIFPHYADDR
    626    1.9    itojun 	case SIOCDIFPHYADDR:
    627   1.31    itojun 		gif_delete_tunnel(&sc->gif_if);
    628    1.2    itojun 		break;
    629    1.9    itojun #endif
    630   1.50     perry 
    631    1.2    itojun 	case SIOCGIFPSRCADDR:
    632    1.2    itojun #ifdef INET6
    633    1.2    itojun 	case SIOCGIFPSRCADDR_IN6:
    634    1.2    itojun #endif /* INET6 */
    635    1.2    itojun 		if (sc->gif_psrc == NULL) {
    636    1.2    itojun 			error = EADDRNOTAVAIL;
    637    1.2    itojun 			goto bad;
    638    1.2    itojun 		}
    639    1.2    itojun 		src = sc->gif_psrc;
    640   1.16    itojun 		switch (cmd) {
    641    1.2    itojun #ifdef INET
    642   1.16    itojun 		case SIOCGIFPSRCADDR:
    643    1.2    itojun 			dst = &ifr->ifr_addr;
    644   1.16    itojun 			size = sizeof(ifr->ifr_addr);
    645    1.2    itojun 			break;
    646    1.2    itojun #endif /* INET */
    647    1.2    itojun #ifdef INET6
    648   1.16    itojun 		case SIOCGIFPSRCADDR_IN6:
    649    1.2    itojun 			dst = (struct sockaddr *)
    650    1.2    itojun 				&(((struct in6_ifreq *)data)->ifr_addr);
    651   1.16    itojun 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    652    1.2    itojun 			break;
    653    1.2    itojun #endif /* INET6 */
    654    1.2    itojun 		default:
    655    1.2    itojun 			error = EADDRNOTAVAIL;
    656    1.2    itojun 			goto bad;
    657    1.2    itojun 		}
    658   1.16    itojun 		if (src->sa_len > size)
    659   1.16    itojun 			return EINVAL;
    660   1.68    dyoung 		memcpy(dst, src, src->sa_len);
    661    1.2    itojun 		break;
    662   1.50     perry 
    663    1.2    itojun 	case SIOCGIFPDSTADDR:
    664    1.2    itojun #ifdef INET6
    665    1.2    itojun 	case SIOCGIFPDSTADDR_IN6:
    666    1.2    itojun #endif /* INET6 */
    667    1.2    itojun 		if (sc->gif_pdst == NULL) {
    668    1.2    itojun 			error = EADDRNOTAVAIL;
    669    1.2    itojun 			goto bad;
    670    1.2    itojun 		}
    671    1.2    itojun 		src = sc->gif_pdst;
    672   1.16    itojun 		switch (cmd) {
    673    1.2    itojun #ifdef INET
    674   1.16    itojun 		case SIOCGIFPDSTADDR:
    675    1.2    itojun 			dst = &ifr->ifr_addr;
    676   1.16    itojun 			size = sizeof(ifr->ifr_addr);
    677    1.2    itojun 			break;
    678    1.2    itojun #endif /* INET */
    679    1.2    itojun #ifdef INET6
    680   1.16    itojun 		case SIOCGIFPDSTADDR_IN6:
    681    1.2    itojun 			dst = (struct sockaddr *)
    682    1.2    itojun 				&(((struct in6_ifreq *)data)->ifr_addr);
    683   1.16    itojun 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    684    1.2    itojun 			break;
    685    1.2    itojun #endif /* INET6 */
    686    1.2    itojun 		default:
    687    1.2    itojun 			error = EADDRNOTAVAIL;
    688    1.2    itojun 			goto bad;
    689    1.2    itojun 		}
    690   1.25    itojun 		if (src->sa_len > size)
    691   1.25    itojun 			return EINVAL;
    692   1.68    dyoung 		memcpy(dst, src, src->sa_len);
    693   1.25    itojun 		break;
    694   1.25    itojun 
    695   1.25    itojun 	case SIOCGLIFPHYADDR:
    696   1.25    itojun 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    697   1.25    itojun 			error = EADDRNOTAVAIL;
    698   1.25    itojun 			goto bad;
    699   1.25    itojun 		}
    700   1.25    itojun 
    701   1.25    itojun 		/* copy src */
    702   1.25    itojun 		src = sc->gif_psrc;
    703   1.25    itojun 		dst = (struct sockaddr *)
    704   1.25    itojun 			&(((struct if_laddrreq *)data)->addr);
    705   1.25    itojun 		size = sizeof(((struct if_laddrreq *)data)->addr);
    706   1.25    itojun 		if (src->sa_len > size)
    707   1.25    itojun 			return EINVAL;
    708   1.68    dyoung 		memcpy(dst, src, src->sa_len);
    709   1.25    itojun 
    710   1.25    itojun 		/* copy dst */
    711   1.25    itojun 		src = sc->gif_pdst;
    712   1.25    itojun 		dst = (struct sockaddr *)
    713   1.25    itojun 			&(((struct if_laddrreq *)data)->dstaddr);
    714   1.25    itojun 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
    715   1.16    itojun 		if (src->sa_len > size)
    716   1.16    itojun 			return EINVAL;
    717   1.68    dyoung 		memcpy(dst, src, src->sa_len);
    718    1.2    itojun 		break;
    719    1.2    itojun 
    720    1.2    itojun 	default:
    721   1.76    dyoung 		return ifioctl_common(ifp, cmd, data);
    722    1.2    itojun 	}
    723    1.2    itojun  bad:
    724    1.2    itojun 	return error;
    725   1.12   thorpej }
    726   1.12   thorpej 
    727   1.97  knakahar static int
    728   1.97  knakahar gif_encap_attach(struct gif_softc *sc)
    729   1.97  knakahar {
    730   1.97  knakahar 	int error;
    731   1.97  knakahar 
    732   1.97  knakahar 	if (sc == NULL || sc->gif_psrc == NULL)
    733   1.97  knakahar 		return EINVAL;
    734   1.97  knakahar 
    735   1.97  knakahar 	switch (sc->gif_psrc->sa_family) {
    736   1.97  knakahar #ifdef INET
    737   1.97  knakahar 	case AF_INET:
    738   1.97  knakahar 		error = in_gif_attach(sc);
    739   1.97  knakahar 		break;
    740   1.97  knakahar #endif
    741   1.97  knakahar #ifdef INET6
    742   1.97  knakahar 	case AF_INET6:
    743   1.97  knakahar 		error = in6_gif_attach(sc);
    744   1.97  knakahar 		break;
    745   1.97  knakahar #endif
    746   1.97  knakahar 	default:
    747   1.97  knakahar 		error = EINVAL;
    748   1.97  knakahar 		break;
    749   1.97  knakahar 	}
    750   1.97  knakahar 
    751   1.97  knakahar 	return error;
    752   1.97  knakahar }
    753   1.97  knakahar 
    754   1.97  knakahar static int
    755   1.97  knakahar gif_encap_detach(struct gif_softc *sc)
    756   1.97  knakahar {
    757   1.97  knakahar 	int error;
    758   1.97  knakahar 
    759   1.97  knakahar 	if (sc == NULL || sc->gif_psrc == NULL)
    760   1.97  knakahar 		return EINVAL;
    761   1.97  knakahar 
    762   1.97  knakahar 	switch (sc->gif_psrc->sa_family) {
    763   1.97  knakahar #ifdef INET
    764   1.97  knakahar 	case AF_INET:
    765   1.97  knakahar 		error = in_gif_detach(sc);
    766   1.97  knakahar 		break;
    767   1.97  knakahar #endif
    768   1.97  knakahar #ifdef INET6
    769   1.97  knakahar 	case AF_INET6:
    770   1.97  knakahar 		error = in6_gif_detach(sc);
    771   1.97  knakahar 		break;
    772   1.97  knakahar #endif
    773   1.97  knakahar 	default:
    774   1.97  knakahar 		error = EINVAL;
    775   1.97  knakahar 		break;
    776   1.97  knakahar 	}
    777   1.97  knakahar 
    778   1.97  knakahar 	return error;
    779   1.97  knakahar }
    780   1.97  knakahar 
    781   1.31    itojun int
    782   1.56   thorpej gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
    783   1.31    itojun {
    784   1.76    dyoung 	struct gif_softc *sc = ifp->if_softc;
    785   1.31    itojun 	struct gif_softc *sc2;
    786   1.71    dyoung 	struct sockaddr *osrc, *odst;
    787   1.96  knakahar 	struct sockaddr *nsrc, *ndst;
    788  1.102  knakahar 	void *osi;
    789   1.31    itojun 	int s;
    790   1.31    itojun 	int error;
    791   1.31    itojun 
    792   1.31    itojun 	s = splsoftnet();
    793   1.31    itojun 
    794   1.68    dyoung 	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
    795   1.31    itojun 		if (sc2 == sc)
    796   1.31    itojun 			continue;
    797   1.31    itojun 		if (!sc2->gif_pdst || !sc2->gif_psrc)
    798   1.31    itojun 			continue;
    799   1.31    itojun 		/* can't configure same pair of address onto two gifs */
    800   1.71    dyoung 		if (sockaddr_cmp(sc2->gif_pdst, dst) == 0 &&
    801   1.71    dyoung 		    sockaddr_cmp(sc2->gif_psrc, src) == 0) {
    802   1.91  knakahar 			/* continue to use the old configureation. */
    803   1.96  knakahar 			splx(s);
    804   1.96  knakahar 			return EADDRNOTAVAIL;
    805   1.31    itojun 		}
    806   1.31    itojun 
    807   1.31    itojun 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
    808   1.31    itojun 	}
    809   1.31    itojun 
    810   1.96  knakahar 	if ((nsrc = sockaddr_dup(src, M_WAITOK)) == NULL) {
    811   1.96  knakahar 		splx(s);
    812   1.96  knakahar 		return ENOMEM;
    813   1.96  knakahar 	}
    814   1.96  knakahar 	if ((ndst = sockaddr_dup(dst, M_WAITOK)) == NULL) {
    815   1.96  knakahar 		sockaddr_free(nsrc);
    816   1.96  knakahar 		splx(s);
    817   1.96  knakahar 		return ENOMEM;
    818   1.96  knakahar 	}
    819   1.96  knakahar 
    820   1.96  knakahar 	/* Firstly, clear old configurations. */
    821   1.58   msaitoh 	if (sc->gif_si) {
    822  1.102  knakahar 		osrc = sc->gif_psrc;
    823  1.102  knakahar 		odst = sc->gif_pdst;
    824  1.102  knakahar 		osi = sc->gif_si;
    825  1.102  knakahar 		sc->gif_psrc = NULL;
    826  1.102  knakahar 		sc->gif_pdst = NULL;
    827   1.58   msaitoh 		sc->gif_si = NULL;
    828  1.102  knakahar 		/*
    829  1.102  knakahar 		 * At this point, gif_output() does not softint_schedule()
    830  1.103  knakahar 		 * any more. However, there are below 2 fears of other CPUs
    831  1.103  knakahar 		 * which would cause panic because of the race between
    832  1.103  knakahar 		 * softint_execute() and softint_disestablish().
    833  1.103  knakahar 		 *     (a) gif_output() has done softint_schedule(), and softint
    834  1.102  knakahar 		 *         (gifintr()) is waiting for execution
    835  1.103  knakahar 		 *         => This pattern is avoided by waiting SOFTINT_PENDING
    836  1.103  knakahar 		 *            CPUs in softint_disestablish()
    837  1.102  knakahar 		 *     (b) gifintr() is already running
    838  1.103  knakahar 		 *         => This pattern is avoided by waiting SOFTINT_ACTIVE
    839  1.103  knakahar 		 *            CPUs in softint_disestablish()
    840  1.102  knakahar 		 */
    841  1.102  knakahar 
    842  1.102  knakahar 		softint_disestablish(osi);
    843  1.102  knakahar 		sc->gif_psrc = osrc;
    844  1.102  knakahar 		sc->gif_pdst = odst;
    845  1.102  knakahar 		osrc = NULL;
    846  1.102  knakahar 		odst = NULL;
    847   1.58   msaitoh 	}
    848   1.31    itojun 	/* XXX we can detach from both, but be polite just in case */
    849   1.31    itojun 	if (sc->gif_psrc)
    850   1.97  knakahar 		(void)gif_encap_detach(sc);
    851   1.31    itojun 
    852   1.96  knakahar 	/*
    853   1.96  knakahar 	 * Secondly, try to set new configurations.
    854   1.96  knakahar 	 * If the setup failed, rollback to old configurations.
    855   1.96  knakahar 	 */
    856   1.96  knakahar 	do {
    857   1.96  knakahar 		osrc = sc->gif_psrc;
    858   1.96  knakahar 		odst = sc->gif_pdst;
    859   1.96  knakahar 		sc->gif_psrc = nsrc;
    860   1.96  knakahar 		sc->gif_pdst = ndst;
    861   1.33    itojun 
    862   1.97  knakahar 		error = gif_encap_attach(sc);
    863   1.96  knakahar 		if (error) {
    864   1.96  knakahar 			/* rollback to the last configuration. */
    865   1.96  knakahar 			nsrc = osrc;
    866   1.96  knakahar 			ndst = odst;
    867   1.96  knakahar 			osrc = sc->gif_psrc;
    868   1.96  knakahar 			odst = sc->gif_pdst;
    869   1.31    itojun 
    870   1.96  knakahar 			continue;
    871   1.96  knakahar 		}
    872   1.31    itojun 
    873   1.96  knakahar 		sc->gif_si = softint_establish(SOFTINT_NET, gifintr, sc);
    874   1.96  knakahar 		if (sc->gif_si == NULL) {
    875   1.97  knakahar 			(void)gif_encap_detach(sc);
    876   1.96  knakahar 
    877   1.96  knakahar 			/* rollback to the last configuration. */
    878   1.96  knakahar 			nsrc = osrc;
    879   1.96  knakahar 			ndst = odst;
    880   1.96  knakahar 			osrc = sc->gif_psrc;
    881   1.96  knakahar 			odst = sc->gif_pdst;
    882   1.96  knakahar 
    883   1.96  knakahar 			error = ENOMEM;
    884   1.96  knakahar 			continue;
    885   1.96  knakahar 		}
    886   1.96  knakahar 	} while (error != 0 && (nsrc != NULL && ndst != NULL));
    887   1.96  knakahar 	/* Thirdly, even rollback failed, clear configurations. */
    888   1.96  knakahar 	if (error) {
    889   1.96  knakahar 		osrc = sc->gif_psrc;
    890   1.96  knakahar 		odst = sc->gif_pdst;
    891   1.96  knakahar 		sc->gif_psrc = NULL;
    892   1.96  knakahar 		sc->gif_pdst = NULL;
    893   1.31    itojun 	}
    894   1.31    itojun 
    895   1.31    itojun 	if (osrc)
    896   1.71    dyoung 		sockaddr_free(osrc);
    897   1.31    itojun 	if (odst)
    898   1.71    dyoung 		sockaddr_free(odst);
    899   1.31    itojun 
    900   1.31    itojun 	if (sc->gif_psrc && sc->gif_pdst)
    901   1.31    itojun 		ifp->if_flags |= IFF_RUNNING;
    902   1.31    itojun 	else
    903   1.31    itojun 		ifp->if_flags &= ~IFF_RUNNING;
    904   1.92  knakahar 
    905   1.31    itojun 	splx(s);
    906   1.31    itojun 	return error;
    907   1.31    itojun }
    908   1.31    itojun 
    909   1.12   thorpej void
    910   1.56   thorpej gif_delete_tunnel(struct ifnet *ifp)
    911   1.12   thorpej {
    912   1.76    dyoung 	struct gif_softc *sc = ifp->if_softc;
    913  1.102  knakahar 	struct sockaddr *osrc, *odst;
    914  1.102  knakahar 	void *osi;
    915   1.12   thorpej 	int s;
    916   1.12   thorpej 
    917   1.12   thorpej 	s = splsoftnet();
    918   1.12   thorpej 
    919   1.35    itojun 	if (sc->gif_si) {
    920  1.102  knakahar 		osrc = sc->gif_psrc;
    921  1.102  knakahar 		odst = sc->gif_pdst;
    922  1.102  knakahar 		osi = sc->gif_si;
    923  1.102  knakahar 
    924  1.102  knakahar 		sc->gif_psrc = NULL;
    925  1.102  knakahar 		sc->gif_pdst = NULL;
    926   1.35    itojun 		sc->gif_si = NULL;
    927  1.102  knakahar 
    928  1.102  knakahar 		softint_disestablish(osi);
    929  1.102  knakahar 		sc->gif_psrc = osrc;
    930  1.102  knakahar 		sc->gif_pdst = odst;
    931   1.35    itojun 	}
    932   1.12   thorpej 	if (sc->gif_psrc) {
    933   1.71    dyoung 		sockaddr_free(sc->gif_psrc);
    934   1.12   thorpej 		sc->gif_psrc = NULL;
    935   1.12   thorpej 	}
    936   1.12   thorpej 	if (sc->gif_pdst) {
    937   1.71    dyoung 		sockaddr_free(sc->gif_pdst);
    938   1.12   thorpej 		sc->gif_pdst = NULL;
    939   1.12   thorpej 	}
    940   1.31    itojun 	/* it is safe to detach from both */
    941   1.31    itojun #ifdef INET
    942   1.31    itojun 	(void)in_gif_detach(sc);
    943   1.31    itojun #endif
    944   1.31    itojun #ifdef INET6
    945   1.31    itojun 	(void)in6_gif_detach(sc);
    946   1.31    itojun #endif
    947   1.12   thorpej 
    948   1.31    itojun 	if (sc->gif_psrc && sc->gif_pdst)
    949   1.31    itojun 		ifp->if_flags |= IFF_RUNNING;
    950   1.31    itojun 	else
    951   1.31    itojun 		ifp->if_flags &= ~IFF_RUNNING;
    952   1.12   thorpej 	splx(s);
    953    1.2    itojun }
    954