Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.112
      1 /*	$NetBSD: if_gif.c,v 1.112 2016/06/24 06:32:47 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.112 2016/06/24 06:32:47 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 /*
     89  * gif global variable definitions
     90  */
     91 static LIST_HEAD(, gif_softc) gif_softc_list;
     92 
     93 static void	gifattach0(struct gif_softc *);
     94 static int	gif_output(struct ifnet *, struct mbuf *,
     95 			   const struct sockaddr *, const struct rtentry *);
     96 static void	gif_start(struct ifnet *);
     97 static int	gif_ioctl(struct ifnet *, u_long, void *);
     98 static int	gif_set_tunnel(struct ifnet *, struct sockaddr *,
     99 			       struct sockaddr *);
    100 static void	gif_delete_tunnel(struct ifnet *);
    101 
    102 static void	gif_sysctl_setup(struct sysctllog **);
    103 
    104 static int	gif_clone_create(struct if_clone *, int);
    105 static int	gif_clone_destroy(struct ifnet *);
    106 static int	gif_check_nesting(struct ifnet *, struct mbuf *);
    107 
    108 static int	gif_encap_attach(struct gif_softc *);
    109 static int	gif_encap_detach(struct gif_softc *);
    110 
    111 static struct if_clone gif_cloner =
    112     IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
    113 
    114 #ifndef MAX_GIF_NEST
    115 /*
    116  * This macro controls the upper limitation on nesting of gif tunnels.
    117  * Since, setting a large value to this macro with a careless configuration
    118  * may introduce system crash, we don't allow any nestings by default.
    119  * If you need to configure nested gif tunnels, you can define this macro
    120  * in your kernel configuration file.  However, if you do so, please be
    121  * careful to configure the tunnels so that it won't make a loop.
    122  */
    123 #define MAX_GIF_NEST 1
    124 #endif
    125 static int max_gif_nesting = MAX_GIF_NEST;
    126 
    127 static void
    128 gif_sysctl_setup(struct sysctllog **clog)
    129 {
    130 
    131 #ifdef INET
    132 	sysctl_createv(clog, 0, NULL, NULL,
    133 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    134 		       CTLTYPE_INT, "gifttl",
    135 		       SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
    136 		       NULL, 0, &ip_gif_ttl, 0,
    137 		       CTL_NET, PF_INET, IPPROTO_IP,
    138 		       IPCTL_GIF_TTL, CTL_EOL);
    139 #endif
    140 #ifdef INET6
    141 	sysctl_createv(clog, 0, NULL, NULL,
    142 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    143 		       CTLTYPE_INT, "gifhlim",
    144 		       SYSCTL_DESCR("Default hop limit for a gif tunnel datagram"),
    145 		       NULL, 0, &ip6_gif_hlim, 0,
    146 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
    147 		       IPV6CTL_GIF_HLIM, CTL_EOL);
    148 #endif
    149 }
    150 
    151 /* ARGSUSED */
    152 void
    153 gifattach(int count)
    154 {
    155 
    156 	LIST_INIT(&gif_softc_list);
    157 	if_clone_attach(&gif_cloner);
    158 
    159 	gif_sysctl_setup(NULL);
    160 }
    161 
    162 static int
    163 gif_clone_create(struct if_clone *ifc, int unit)
    164 {
    165 	struct gif_softc *sc;
    166 
    167 	sc = kmem_zalloc(sizeof(struct gif_softc), KM_SLEEP);
    168 	if (sc == NULL)
    169 		return ENOMEM;
    170 
    171 	if_initname(&sc->gif_if, ifc->ifc_name, unit);
    172 
    173 	gifattach0(sc);
    174 
    175 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
    176 	return (0);
    177 }
    178 
    179 static void
    180 gifattach0(struct gif_softc *sc)
    181 {
    182 
    183 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
    184 
    185 	sc->gif_if.if_addrlen = 0;
    186 	sc->gif_if.if_mtu    = GIF_MTU;
    187 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
    188 	sc->gif_if.if_ioctl  = gif_ioctl;
    189 	sc->gif_if.if_output = gif_output;
    190 	sc->gif_if.if_type   = IFT_GIF;
    191 	sc->gif_if.if_dlt    = DLT_NULL;
    192 	sc->gif_if.if_softc  = sc;
    193 	IFQ_SET_READY(&sc->gif_if.if_snd);
    194 	if_initialize(&sc->gif_if);
    195 	if_register(&sc->gif_if);
    196 	if_alloc_sadl(&sc->gif_if);
    197 	bpf_attach(&sc->gif_if, DLT_NULL, sizeof(u_int));
    198 }
    199 
    200 static int
    201 gif_clone_destroy(struct ifnet *ifp)
    202 {
    203 	struct gif_softc *sc = (void *) ifp;
    204 
    205 	LIST_REMOVE(sc, gif_list);
    206 
    207 	gif_delete_tunnel(&sc->gif_if);
    208 	bpf_detach(ifp);
    209 	if_detach(ifp);
    210 	rtcache_free(&sc->gif_ro);
    211 
    212 	kmem_free(sc, sizeof(struct gif_softc));
    213 
    214 	return (0);
    215 }
    216 
    217 #ifdef GIF_ENCAPCHECK
    218 int
    219 gif_encapcheck(struct mbuf *m, int off, int proto, void *arg)
    220 {
    221 	struct ip ip;
    222 	struct gif_softc *sc;
    223 
    224 	sc = arg;
    225 	if (sc == NULL)
    226 		return 0;
    227 
    228 	if ((sc->gif_if.if_flags & IFF_UP) == 0)
    229 		return 0;
    230 
    231 	/* no physical address */
    232 	if (!sc->gif_psrc || !sc->gif_pdst)
    233 		return 0;
    234 
    235 	switch (proto) {
    236 #ifdef INET
    237 	case IPPROTO_IPV4:
    238 		break;
    239 #endif
    240 #ifdef INET6
    241 	case IPPROTO_IPV6:
    242 		break;
    243 #endif
    244 	default:
    245 		return 0;
    246 	}
    247 
    248 	/* Bail on short packets */
    249 	KASSERT(m->m_flags & M_PKTHDR);
    250 	if (m->m_pkthdr.len < sizeof(ip))
    251 		return 0;
    252 
    253 	m_copydata(m, 0, sizeof(ip), &ip);
    254 
    255 	switch (ip.ip_v) {
    256 #ifdef INET
    257 	case 4:
    258 		if (sc->gif_psrc->sa_family != AF_INET ||
    259 		    sc->gif_pdst->sa_family != AF_INET)
    260 			return 0;
    261 		return gif_encapcheck4(m, off, proto, arg);
    262 #endif
    263 #ifdef INET6
    264 	case 6:
    265 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
    266 			return 0;
    267 		if (sc->gif_psrc->sa_family != AF_INET6 ||
    268 		    sc->gif_pdst->sa_family != AF_INET6)
    269 			return 0;
    270 		return gif_encapcheck6(m, off, proto, arg);
    271 #endif
    272 	default:
    273 		return 0;
    274 	}
    275 }
    276 #endif
    277 
    278 /*
    279  * gif may cause infinite recursion calls when misconfigured.
    280  * We'll prevent this by introducing upper limit.
    281  */
    282 static int
    283 gif_check_nesting(struct ifnet *ifp, struct mbuf *m)
    284 {
    285 	struct m_tag *mtag;
    286 	int *count;
    287 
    288 	mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO, NULL);
    289 	if (mtag != NULL) {
    290 		count = (int *)(mtag + 1);
    291 		if (++(*count) > max_gif_nesting) {
    292 			log(LOG_NOTICE,
    293 			    "%s: recursively called too many times(%d)\n",
    294 			    if_name(ifp),
    295 			    *count);
    296 			return EIO;
    297 		}
    298 	} else {
    299 		mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count),
    300 		    M_NOWAIT);
    301 		if (mtag != NULL) {
    302 			m_tag_prepend(m, mtag);
    303 			count = (int *)(mtag + 1);
    304 			*count = 0;
    305 		} else {
    306 			log(LOG_DEBUG,
    307 			    "%s: m_tag_get() failed, recursion calls are not prevented.\n",
    308 			    if_name(ifp));
    309 		}
    310 	}
    311 
    312 	return 0;
    313 }
    314 
    315 static int
    316 gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    317     const struct rtentry *rt)
    318 {
    319 	struct gif_softc *sc = ifp->if_softc;
    320 	int error = 0;
    321 	int s;
    322 
    323 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    324 
    325 	if ((error = gif_check_nesting(ifp, m)) != 0) {
    326 		m_free(m);
    327 		goto end;
    328 	}
    329 
    330 	m->m_flags &= ~(M_BCAST|M_MCAST);
    331 	if (!(ifp->if_flags & IFF_UP) ||
    332 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    333 		m_freem(m);
    334 		error = ENETDOWN;
    335 		goto end;
    336 	}
    337 
    338 	/* XXX should we check if our outer source is legal? */
    339 
    340 	/* use DLT_NULL encapsulation here to pass inner af type */
    341 	M_PREPEND(m, sizeof(int), M_DONTWAIT);
    342 	if (!m) {
    343 		error = ENOBUFS;
    344 		goto end;
    345 	}
    346 	*mtod(m, int *) = dst->sa_family;
    347 
    348 	/* Clear checksum-offload flags. */
    349 	m->m_pkthdr.csum_flags = 0;
    350 	m->m_pkthdr.csum_data = 0;
    351 
    352 	s = splnet();
    353 	IFQ_ENQUEUE(&ifp->if_snd, m, error);
    354 	if (error) {
    355 		splx(s);
    356 		goto end;
    357 	}
    358 	splx(s);
    359 
    360 	gif_start(ifp);
    361 
    362 	error = 0;
    363 
    364   end:
    365 	if (error)
    366 		ifp->if_oerrors++;
    367 	return error;
    368 }
    369 
    370 static void
    371 gif_start(struct ifnet *ifp)
    372 {
    373 	struct gif_softc *sc;
    374 	struct mbuf *m;
    375 	int family;
    376 	int len;
    377 	int s;
    378 	int error;
    379 
    380 	sc = ifp->if_softc;
    381 
    382 	/* output processing */
    383 	while (1) {
    384 		s = splnet();
    385 		IFQ_DEQUEUE(&sc->gif_if.if_snd, m);
    386 		splx(s);
    387 		if (m == NULL)
    388 			break;
    389 
    390 		/* grab and chop off inner af type */
    391 		if (sizeof(int) > m->m_len) {
    392 			m = m_pullup(m, sizeof(int));
    393 			if (!m) {
    394 				ifp->if_oerrors++;
    395 				continue;
    396 			}
    397 		}
    398 		family = *mtod(m, int *);
    399 		bpf_mtap(ifp, m);
    400 		m_adj(m, sizeof(int));
    401 
    402 		len = m->m_pkthdr.len;
    403 
    404 		/* dispatch to output logic based on outer AF */
    405 		switch (sc->gif_psrc->sa_family) {
    406 #ifdef INET
    407 		case AF_INET:
    408 			/* XXX
    409 			 * To add mutex_enter(softnet_lock) or
    410 			 * KASSERT(mutex_owned(softnet_lock)) here, we shold
    411 			 * coordinate softnet_lock between in6_if_up() and
    412 			 * in6_purgeif().
    413 			 */
    414 			error = in_gif_output(ifp, family, m);
    415 			break;
    416 #endif
    417 #ifdef INET6
    418 		case AF_INET6:
    419 			/* XXX
    420 			 * the same as in_gif_output()
    421 			 */
    422 			error = in6_gif_output(ifp, family, m);
    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_set_rcvif(m, 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 static 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 static 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 	int s;
    786 	int error;
    787 
    788 	s = splsoftnet();
    789 
    790 	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
    791 		if (sc2 == sc)
    792 			continue;
    793 		if (!sc2->gif_pdst || !sc2->gif_psrc)
    794 			continue;
    795 		/* can't configure same pair of address onto two gifs */
    796 		if (sockaddr_cmp(sc2->gif_pdst, dst) == 0 &&
    797 		    sockaddr_cmp(sc2->gif_psrc, src) == 0) {
    798 			/* continue to use the old configureation. */
    799 			splx(s);
    800 			return EADDRNOTAVAIL;
    801 		}
    802 
    803 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
    804 	}
    805 
    806 	if ((nsrc = sockaddr_dup(src, M_WAITOK)) == NULL) {
    807 		splx(s);
    808 		return ENOMEM;
    809 	}
    810 	if ((ndst = sockaddr_dup(dst, M_WAITOK)) == NULL) {
    811 		sockaddr_free(nsrc);
    812 		splx(s);
    813 		return ENOMEM;
    814 	}
    815 
    816 	/* Firstly, clear old configurations. */
    817 	/* XXX we can detach from both, but be polite just in case */
    818 	if (sc->gif_psrc)
    819 		(void)gif_encap_detach(sc);
    820 
    821 	/*
    822 	 * Secondly, try to set new configurations.
    823 	 * If the setup failed, rollback to old configurations.
    824 	 */
    825 	do {
    826 		osrc = sc->gif_psrc;
    827 		odst = sc->gif_pdst;
    828 		sc->gif_psrc = nsrc;
    829 		sc->gif_pdst = ndst;
    830 
    831 		error = gif_encap_attach(sc);
    832 		if (error) {
    833 			/* rollback to the last configuration. */
    834 			nsrc = osrc;
    835 			ndst = odst;
    836 			osrc = sc->gif_psrc;
    837 			odst = sc->gif_pdst;
    838 
    839 			continue;
    840 		}
    841 	} while (error != 0 && (nsrc != NULL && ndst != NULL));
    842 	/* Thirdly, even rollback failed, clear configurations. */
    843 	if (error) {
    844 		osrc = sc->gif_psrc;
    845 		odst = sc->gif_pdst;
    846 		sc->gif_psrc = NULL;
    847 		sc->gif_pdst = NULL;
    848 	}
    849 
    850 	if (osrc)
    851 		sockaddr_free(osrc);
    852 	if (odst)
    853 		sockaddr_free(odst);
    854 
    855 	if (sc->gif_psrc && sc->gif_pdst)
    856 		ifp->if_flags |= IFF_RUNNING;
    857 	else
    858 		ifp->if_flags &= ~IFF_RUNNING;
    859 
    860 	splx(s);
    861 	return error;
    862 }
    863 
    864 static void
    865 gif_delete_tunnel(struct ifnet *ifp)
    866 {
    867 	struct gif_softc *sc = ifp->if_softc;
    868 	int s;
    869 
    870 	s = splsoftnet();
    871 
    872 	if (sc->gif_psrc) {
    873 		sockaddr_free(sc->gif_psrc);
    874 		sc->gif_psrc = NULL;
    875 	}
    876 	if (sc->gif_pdst) {
    877 		sockaddr_free(sc->gif_pdst);
    878 		sc->gif_pdst = NULL;
    879 	}
    880 	/* it is safe to detach from both */
    881 #ifdef INET
    882 	(void)in_gif_detach(sc);
    883 #endif
    884 #ifdef INET6
    885 	(void)in6_gif_detach(sc);
    886 #endif
    887 
    888 	if (sc->gif_psrc && sc->gif_pdst)
    889 		ifp->if_flags |= IFF_RUNNING;
    890 	else
    891 		ifp->if_flags &= ~IFF_RUNNING;
    892 	splx(s);
    893 }
    894