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