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