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