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