Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.30
      1 /*	$NetBSD: if_gif.c,v 1.30 2001/07/18 16:43:09 thorpej Exp $	*/
      2 /*	$KAME: if_gif.c,v 1.49 2001/06/04 12:03:41 itojun 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 "opt_inet.h"
     34 #include "opt_iso.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/mbuf.h>
     40 #include <sys/socket.h>
     41 #include <sys/sockio.h>
     42 #include <sys/errno.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/time.h>
     45 #include <sys/syslog.h>
     46 #include <sys/proc.h>
     47 #include <sys/protosw.h>
     48 #include <machine/cpu.h>
     49 
     50 #include <net/if.h>
     51 #include <net/if_types.h>
     52 #include <net/netisr.h>
     53 #include <net/route.h>
     54 #include <net/bpf.h>
     55 
     56 #include <netinet/in.h>
     57 #include <netinet/in_systm.h>
     58 #include <netinet/ip.h>
     59 #ifdef	INET
     60 #include <netinet/in_var.h>
     61 #include <netinet/in_gif.h>
     62 #endif	/* INET */
     63 
     64 #ifdef INET6
     65 #ifndef INET
     66 #include <netinet/in.h>
     67 #endif
     68 #include <netinet6/in6_var.h>
     69 #include <netinet/ip6.h>
     70 #include <netinet6/ip6_var.h>
     71 #include <netinet6/in6_gif.h>
     72 #include <netinet6/ip6protosw.h>
     73 #endif /* INET6 */
     74 
     75 #ifdef ISO
     76 #include <netiso/iso.h>
     77 #include <netiso/iso_var.h>
     78 #endif
     79 
     80 #include <netinet/ip_encap.h>
     81 #include <net/if_gif.h>
     82 
     83 #include "gif.h"
     84 #include "bpfilter.h"
     85 
     86 #include <net/net_osdep.h>
     87 
     88 #if NGIF > 0
     89 
     90 void gifattach __P((int));
     91 static int gif_encapcheck __P((const struct mbuf *, int, int, void *));
     92 #ifdef INET
     93 extern struct protosw in_gif_protosw;
     94 #endif
     95 #ifdef INET6
     96 extern struct ip6protosw in6_gif_protosw;
     97 #endif
     98 #ifdef ISO
     99 static int gif_eon_encap(struct mbuf **);
    100 static int 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;
    107 
    108 int	gif_clone_create __P((struct if_clone *, int));
    109 void	gif_clone_destroy __P((struct ifnet *));
    110 
    111 struct if_clone gif_cloner =
    112     IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
    113 
    114 void	gif_delete_tunnel __P((struct gif_softc *));
    115 
    116 #ifndef MAX_GIF_NEST
    117 /*
    118  * This macro controls the upper limitation on nesting of gif tunnels.
    119  * Since, setting a large value to this macro with a careless configuration
    120  * may introduce system crash, we don't allow any nestings by default.
    121  * If you need to configure nested gif tunnels, you can define this macro
    122  * in your kernel configuration file. However, if you do so, please be
    123  * careful to configure the tunnels so that it won't make a loop.
    124  */
    125 #define MAX_GIF_NEST 1
    126 #endif
    127 static int max_gif_nesting = MAX_GIF_NEST;
    128 
    129 /* ARGSUSED */
    130 void
    131 gifattach(count)
    132 	int count;
    133 {
    134 
    135 	LIST_INIT(&gif_softc_list);
    136 	if_clone_attach(&gif_cloner);
    137 }
    138 
    139 int
    140 gif_clone_create(ifc, unit)
    141 	struct if_clone *ifc;
    142 	int unit;
    143 {
    144 	struct gif_softc *sc;
    145 
    146 	sc = malloc(sizeof(struct gif_softc), M_DEVBUF, M_WAIT);
    147 	memset(sc, 0, sizeof(struct gif_softc));
    148 
    149 	sprintf(sc->gif_if.if_xname, "%s%d", ifc->ifc_name, unit);
    150 
    151 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
    152 #ifdef INET
    153 	sc->encap_cookie4 = encap_attach_func(AF_INET, -1,
    154 	    gif_encapcheck, &in_gif_protosw, sc);
    155 	if (sc->encap_cookie4 == NULL) {
    156 		printf("%s: unable to attach encap4\n", if_name(&sc->gif_if));
    157 		free(sc, M_DEVBUF);
    158 		return (EIO);	/* XXX */
    159 	}
    160 #endif
    161 #ifdef INET6
    162 	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1,
    163 	    gif_encapcheck, (struct protosw *)&in6_gif_protosw, sc);
    164 	if (sc->encap_cookie6 == NULL) {
    165 		if (sc->encap_cookie4) {
    166 			encap_detach(sc->encap_cookie4);
    167 			sc->encap_cookie4 = NULL;
    168 		}
    169 		printf("%s: unable to attach encap6\n", if_name(&sc->gif_if));
    170 		free(sc, M_DEVBUF);
    171 		return (EIO);	/* XXX */
    172 	}
    173 #endif
    174 
    175 	sc->gif_if.if_mtu    = GIF_MTU;
    176 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
    177 	sc->gif_if.if_ioctl  = gif_ioctl;
    178 	sc->gif_if.if_output = gif_output;
    179 	sc->gif_if.if_type   = IFT_GIF;
    180 	sc->gif_if.if_dlt    = DLT_NULL;
    181 	if_attach(&sc->gif_if);
    182 	if_alloc_sadl(&sc->gif_if);
    183 #if NBPFILTER > 0
    184 	bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
    185 #endif
    186 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
    187 	return (0);
    188 }
    189 
    190 void
    191 gif_clone_destroy(ifp)
    192 	struct ifnet *ifp;
    193 {
    194 	struct gif_softc *sc = (void *) ifp;
    195 
    196 	gif_delete_tunnel(sc);
    197 	LIST_REMOVE(sc, gif_list);
    198 #ifdef INET6
    199 	encap_detach(sc->encap_cookie6);
    200 #endif
    201 #ifdef INET
    202 	encap_detach(sc->encap_cookie4);
    203 #endif
    204 
    205 #if NBPFILTER > 0
    206 	bpfdetach(ifp);
    207 #endif
    208 	if_detach(ifp);
    209 
    210 	free(sc, M_DEVBUF);
    211 }
    212 
    213 static int
    214 gif_encapcheck(m, off, proto, arg)
    215 	const struct mbuf *m;
    216 	int off;
    217 	int proto;
    218 	void *arg;
    219 {
    220 	struct ip ip;
    221 	struct gif_softc *sc;
    222 
    223 	sc = (struct gif_softc *)arg;
    224 	if (sc == NULL)
    225 		return 0;
    226 
    227 	if ((sc->gif_if.if_flags & IFF_UP) == 0)
    228 		return 0;
    229 
    230 	/* no physical address */
    231 	if (!sc->gif_psrc || !sc->gif_pdst)
    232 		return 0;
    233 
    234 	switch (proto) {
    235 #ifdef INET
    236 	case IPPROTO_IPV4:
    237 		break;
    238 #endif
    239 #ifdef INET6
    240 	case IPPROTO_IPV6:
    241 		break;
    242 #endif
    243 #ifdef ISO
    244 	case IPPROTO_EON:
    245 		break;
    246 #endif
    247 	default:
    248 		return 0;
    249 	}
    250 
    251 	/* LINTED const cast */
    252 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
    253 
    254 	switch (ip.ip_v) {
    255 #ifdef INET
    256 	case 4:
    257 		if (sc->gif_psrc->sa_family != AF_INET ||
    258 		    sc->gif_pdst->sa_family != AF_INET)
    259 			return 0;
    260 		return gif_encapcheck4(m, off, proto, arg);
    261 #endif
    262 #ifdef INET6
    263 	case 6:
    264 		if (sc->gif_psrc->sa_family != AF_INET6 ||
    265 		    sc->gif_pdst->sa_family != AF_INET6)
    266 			return 0;
    267 		return gif_encapcheck6(m, off, proto, arg);
    268 #endif
    269 	default:
    270 		return 0;
    271 	}
    272 }
    273 
    274 int
    275 gif_output(ifp, m, dst, rt)
    276 	struct ifnet *ifp;
    277 	struct mbuf *m;
    278 	struct sockaddr *dst;
    279 	struct rtentry *rt;	/* added in net2 */
    280 {
    281 	struct gif_softc *sc = (struct gif_softc*)ifp;
    282 	int error = 0;
    283 	static int called = 0;	/* XXX: MUTEX */
    284 
    285 	/*
    286 	 * gif may cause infinite recursion calls when misconfigured.
    287 	 * We'll prevent this by introducing upper limit.
    288 	 * XXX: this mechanism may introduce another problem about
    289 	 *      mutual exclusion of the variable CALLED, especially if we
    290 	 *      use kernel thread.
    291 	 */
    292 	if (++called > max_gif_nesting) {
    293 		log(LOG_NOTICE,
    294 		    "gif_output: recursively called too many times(%d)\n",
    295 		    called);
    296 		m_freem(m);
    297 		error = EIO;	/* is there better errno? */
    298 		goto end;
    299 	}
    300 
    301 	m->m_flags &= ~(M_BCAST|M_MCAST);
    302 	if (!(ifp->if_flags & IFF_UP) ||
    303 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    304 		m_freem(m);
    305 		error = ENETDOWN;
    306 		goto end;
    307 	}
    308 
    309 #if NBPFILTER > 0
    310 	if (ifp->if_bpf) {
    311 		/*
    312 		 * We need to prepend the address family as
    313 		 * a four byte field.  Cons up a dummy header
    314 		 * to pacify bpf.  This is safe because bpf
    315 		 * will only read from the mbuf (i.e., it won't
    316 		 * try to free it or keep a pointer a to it).
    317 		 */
    318 		struct mbuf m0;
    319 		u_int32_t af = dst->sa_family;
    320 
    321 		m0.m_next = m;
    322 		m0.m_len = 4;
    323 		m0.m_data = (char *)&af;
    324 
    325 #ifdef HAVE_OLD_BPF
    326 		bpf_mtap(ifp, &m0);
    327 #else
    328 		bpf_mtap(ifp->if_bpf, &m0);
    329 #endif
    330 	}
    331 #endif
    332 	ifp->if_opackets++;
    333 	ifp->if_obytes += m->m_pkthdr.len;
    334 
    335 	/* inner AF-specific encapsulation */
    336 	switch (dst->sa_family) {
    337 #ifdef ISO
    338 	case AF_ISO:
    339 		error = gif_eon_encap(&m);
    340 		if (error)
    341 			goto end;
    342 #endif
    343 	default:
    344 		break;
    345 	}
    346 
    347 	/* XXX should we check if our outer source is legal? */
    348 
    349 	/* dispatch to output logic based on outer AF */
    350 	switch (sc->gif_psrc->sa_family) {
    351 #ifdef INET
    352 	case AF_INET:
    353 		error = in_gif_output(ifp, dst->sa_family, m, rt);
    354 		break;
    355 #endif
    356 #ifdef INET6
    357 	case AF_INET6:
    358 		error = in6_gif_output(ifp, dst->sa_family, m, rt);
    359 		break;
    360 #endif
    361 	default:
    362 		m_freem(m);
    363 		error = ENETDOWN;
    364 	}
    365 
    366   end:
    367 	called = 0;		/* reset recursion counter */
    368 	if (error) ifp->if_oerrors++;
    369 	return error;
    370 }
    371 
    372 void
    373 gif_input(m, af, gifp)
    374 	struct mbuf *m;
    375 	int af;
    376 	struct ifnet *gifp;
    377 {
    378 	int s, isr;
    379 	struct ifqueue *ifq = 0;
    380 
    381 	if (gifp == NULL) {
    382 		/* just in case */
    383 		m_freem(m);
    384 		return;
    385 	}
    386 
    387 	m->m_pkthdr.rcvif = gifp;
    388 
    389 #if NBPFILTER > 0
    390 	if (gifp->if_bpf) {
    391 		/*
    392 		 * We need to prepend the address family as
    393 		 * a four byte field.  Cons up a dummy header
    394 		 * to pacify bpf.  This is safe because bpf
    395 		 * will only read from the mbuf (i.e., it won't
    396 		 * try to free it or keep a pointer a to it).
    397 		 */
    398 		struct mbuf m0;
    399 		u_int32_t af1 = af;
    400 
    401 		m0.m_next = m;
    402 		m0.m_len = 4;
    403 		m0.m_data = (char *)&af1;
    404 
    405 #ifdef HAVE_OLD_BPF
    406 		bpf_mtap(gifp, &m0);
    407 #else
    408 		bpf_mtap(gifp->if_bpf, &m0);
    409 #endif
    410 	}
    411 #endif /*NBPFILTER > 0*/
    412 
    413 	/*
    414 	 * Put the packet to the network layer input queue according to the
    415 	 * specified address family.
    416 	 * Note: older versions of gif_input directly called network layer
    417 	 * input functions, e.g. ip6_input, here. We changed the policy to
    418 	 * prevent too many recursive calls of such input functions, which
    419 	 * might cause kernel panic. But the change may introduce another
    420 	 * problem; if the input queue is full, packets are discarded.
    421 	 * We believed it rarely occurs and changed the policy. If we find
    422 	 * it occurs more times than we thought, we may change the policy
    423 	 * again.
    424 	 */
    425 	switch (af) {
    426 #ifdef INET
    427 	case AF_INET:
    428 		ifq = &ipintrq;
    429 		isr = NETISR_IP;
    430 		break;
    431 #endif
    432 #ifdef INET6
    433 	case AF_INET6:
    434 		ifq = &ip6intrq;
    435 		isr = NETISR_IPV6;
    436 		break;
    437 #endif
    438 #ifdef ISO
    439 	case AF_ISO:
    440 		if (gif_eon_decap(gifp, &m)) {
    441 			if (m)
    442 			       m_freem(m);
    443 			return;
    444 		}
    445 		ifq = &clnlintrq;
    446 		isr = NETISR_ISO;
    447 		break;
    448 #endif
    449 	default:
    450 		m_freem(m);
    451 		return;
    452 	}
    453 
    454 	s = splnet();
    455 	if (IF_QFULL(ifq)) {
    456 		IF_DROP(ifq);	/* update statistics */
    457 		m_freem(m);
    458 		splx(s);
    459 		return;
    460 	}
    461 	IF_ENQUEUE(ifq, m);
    462 	/* we need schednetisr since the address family may change */
    463 	schednetisr(isr);
    464 	gifp->if_ipackets++;
    465 	gifp->if_ibytes += m->m_pkthdr.len;
    466 	splx(s);
    467 
    468 	return;
    469 }
    470 
    471 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
    472 int
    473 gif_ioctl(ifp, cmd, data)
    474 	struct ifnet *ifp;
    475 	u_long cmd;
    476 	caddr_t data;
    477 {
    478 	struct proc *p = curproc;	/* XXX */
    479 	struct gif_softc *sc  = (struct gif_softc*)ifp;
    480 	struct ifreq     *ifr = (struct ifreq*)data;
    481 	int error = 0, size;
    482 	struct sockaddr *dst, *src;
    483 	struct sockaddr *sa;
    484 	int s;
    485 	struct gif_softc *sc2;
    486 
    487 	switch (cmd) {
    488 	case SIOCSIFADDR:
    489 		break;
    490 
    491 	case SIOCSIFDSTADDR:
    492 		break;
    493 
    494 	case SIOCADDMULTI:
    495 	case SIOCDELMULTI:
    496 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    497 			break;
    498 		switch (ifr->ifr_addr.sa_family) {
    499 #ifdef INET
    500 		case AF_INET:	/* IP supports Multicast */
    501 			break;
    502 #endif /* INET */
    503 #ifdef INET6
    504 		case AF_INET6:	/* IP6 supports Multicast */
    505 			break;
    506 #endif /* INET6 */
    507 		default:  /* Other protocols doesn't support Multicast */
    508 			error = EAFNOSUPPORT;
    509 			break;
    510 		}
    511 		break;
    512 
    513 #ifdef	SIOCSIFMTU /* xxx */
    514 	case SIOCGIFMTU:
    515 		break;
    516 
    517 	case SIOCSIFMTU:
    518 		{
    519 			u_long mtu;
    520 			if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    521 				break;
    522 			mtu = ifr->ifr_mtu;
    523 			if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
    524 				return (EINVAL);
    525 			}
    526 			ifp->if_mtu = mtu;
    527 		}
    528 		break;
    529 #endif /* SIOCSIFMTU */
    530 
    531 	case SIOCSIFPHYADDR:
    532 #ifdef INET6
    533 	case SIOCSIFPHYADDR_IN6:
    534 #endif /* INET6 */
    535 	case SIOCSLIFPHYADDR:
    536 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    537 			break;
    538 		switch (cmd) {
    539 #ifdef INET
    540 		case SIOCSIFPHYADDR:
    541 			src = (struct sockaddr *)
    542 				&(((struct in_aliasreq *)data)->ifra_addr);
    543 			dst = (struct sockaddr *)
    544 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
    545 			break;
    546 #endif
    547 #ifdef INET6
    548 		case SIOCSIFPHYADDR_IN6:
    549 			src = (struct sockaddr *)
    550 				&(((struct in6_aliasreq *)data)->ifra_addr);
    551 			dst = (struct sockaddr *)
    552 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    553 			break;
    554 #endif
    555 		case SIOCSLIFPHYADDR:
    556 			src = (struct sockaddr *)
    557 				&(((struct if_laddrreq *)data)->addr);
    558 			dst = (struct sockaddr *)
    559 				&(((struct if_laddrreq *)data)->dstaddr);
    560 		}
    561 
    562 		/* sa_family must be equal */
    563 		if (src->sa_family != dst->sa_family)
    564 			return EINVAL;
    565 
    566 		/* validate sa_len */
    567 		switch (src->sa_family) {
    568 #ifdef INET
    569 		case AF_INET:
    570 			if (src->sa_len != sizeof(struct sockaddr_in))
    571 				return EINVAL;
    572 			break;
    573 #endif
    574 #ifdef INET6
    575 		case AF_INET6:
    576 			if (src->sa_len != sizeof(struct sockaddr_in6))
    577 				return EINVAL;
    578 			break;
    579 #endif
    580 		default:
    581 			return EAFNOSUPPORT;
    582 		}
    583 		switch (dst->sa_family) {
    584 #ifdef INET
    585 		case AF_INET:
    586 			if (dst->sa_len != sizeof(struct sockaddr_in))
    587 				return EINVAL;
    588 			break;
    589 #endif
    590 #ifdef INET6
    591 		case AF_INET6:
    592 			if (dst->sa_len != sizeof(struct sockaddr_in6))
    593 				return EINVAL;
    594 			break;
    595 #endif
    596 		default:
    597 			return EAFNOSUPPORT;
    598 		}
    599 
    600 		/* check sa_family looks sane for the cmd */
    601 		switch (cmd) {
    602 		case SIOCSIFPHYADDR:
    603 			if (src->sa_family == AF_INET)
    604 				break;
    605 			return EAFNOSUPPORT;
    606 #ifdef INET6
    607 		case SIOCSIFPHYADDR_IN6:
    608 			if (src->sa_family == AF_INET6)
    609 				break;
    610 			return EAFNOSUPPORT;
    611 #endif /* INET6 */
    612 		case SIOCSLIFPHYADDR:
    613 			/* checks done in the above */
    614 			break;
    615 		}
    616 
    617 		for (sc2 = LIST_FIRST(&gif_softc_list); sc2 != NULL;
    618 		     sc2 = LIST_NEXT(sc2, gif_list)) {
    619 			if (sc2 == sc)
    620 				continue;
    621 			if (!sc2->gif_pdst || !sc2->gif_psrc)
    622 				continue;
    623 			if (sc2->gif_pdst->sa_family != dst->sa_family ||
    624 			    sc2->gif_pdst->sa_len != dst->sa_len ||
    625 			    sc2->gif_psrc->sa_family != src->sa_family ||
    626 			    sc2->gif_psrc->sa_len != src->sa_len)
    627 				continue;
    628 
    629 			/* can't configure same pair of address onto two gifs */
    630 			if (bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
    631 			    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
    632 				error = EADDRNOTAVAIL;
    633 				goto bad;
    634 			}
    635 
    636 			/* can't configure multiple multi-dest interfaces */
    637 #define multidest(x) \
    638 	(((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
    639 #ifdef INET6
    640 #define multidest6(x) \
    641 	(IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
    642 #endif
    643 			if (dst->sa_family == AF_INET &&
    644 			    multidest(dst) && multidest(sc2->gif_pdst)) {
    645 				error = EADDRNOTAVAIL;
    646 				goto bad;
    647 			}
    648 #ifdef INET6
    649 			if (dst->sa_family == AF_INET6 &&
    650 			    multidest6(dst) && multidest6(sc2->gif_pdst)) {
    651 				error = EADDRNOTAVAIL;
    652 				goto bad;
    653 			}
    654 #endif
    655 		}
    656 
    657 		if (sc->gif_psrc)
    658 			free((caddr_t)sc->gif_psrc, M_IFADDR);
    659 		sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
    660 		bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
    661 		sc->gif_psrc = sa;
    662 
    663 		if (sc->gif_pdst)
    664 			free((caddr_t)sc->gif_pdst, M_IFADDR);
    665 		sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
    666 		bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
    667 		sc->gif_pdst = sa;
    668 
    669 		s = splsoftnet();
    670 		ifp->if_flags |= IFF_RUNNING;
    671 		if_up(ifp);		/* send up RTM_IFINFO */
    672 		splx(s);
    673 
    674 		error = 0;
    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);
    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 void
    787 gif_delete_tunnel(sc)
    788 	struct gif_softc *sc;
    789 {
    790 	int s;
    791 
    792 	s = splsoftnet();
    793 
    794 	if (sc->gif_psrc) {
    795 		free((caddr_t)sc->gif_psrc, M_IFADDR);
    796 		sc->gif_psrc = NULL;
    797 	}
    798 	if (sc->gif_pdst) {
    799 		free((caddr_t)sc->gif_pdst, M_IFADDR);
    800 		sc->gif_pdst = NULL;
    801 	}
    802 	/* change the IFF_UP flag as well? */
    803 
    804 	splx(s);
    805 }
    806 
    807 #ifdef ISO
    808 struct eonhdr {
    809 	u_int8_t version;
    810 	u_int8_t class;
    811 	u_int16_t cksum;
    812 };
    813 
    814 /*
    815  * prepend EON header to ISO PDU
    816  */
    817 static int
    818 gif_eon_encap(struct mbuf **m)
    819 {
    820 	struct eonhdr *ehdr;
    821 
    822 	M_PREPEND(*m, sizeof(*ehdr), M_DONTWAIT);
    823 	if (*m && (*m)->m_len < sizeof(*ehdr))
    824 		*m = m_pullup(*m, sizeof(*ehdr));
    825 	if (*m == NULL) {
    826 		printf("ENOBUFS in gif_eon_encap %d\n", __LINE__);
    827 		return ENOBUFS;
    828 	}
    829 	ehdr = mtod(*m, struct eonhdr *);
    830 	ehdr->version = 1;
    831 	ehdr->class = 0;		/* always unicast */
    832 #if 0
    833 	/* calculate the checksum of the eonhdr */
    834 	{
    835 		struct mbuf mhead;
    836 		memset(&mhead, 0, sizeof(mhead));
    837 		ehdr->cksum = 0;
    838 		mhead.m_data = (caddr_t)ehdr;
    839 		mhead.m_len = sizeof(*ehdr);
    840 		mhead.m_next = 0;
    841 		iso_gen_csum(&mhead, offsetof(struct eonhdr, cksum),
    842 		    mhead.m_len);
    843 	}
    844 #else
    845 	/* since the data is always constant we'll just plug the value in */
    846 	ehdr->cksum = htons(0xfc02);
    847 #endif
    848 	return (0);
    849 }
    850 
    851 /*
    852  * remove EON header and check checksum
    853  */
    854 static int
    855 gif_eon_decap(struct ifnet *gifp, struct mbuf **m)
    856 {
    857 	struct eonhdr *ehdr;
    858 
    859 	if ((*m)->m_len < sizeof(*ehdr) &&
    860 	    (*m = m_pullup(*m, sizeof(*ehdr))) == 0) {
    861 		gifp->if_ierrors++;
    862 		return (EINVAL);
    863 	}
    864 	if (iso_check_csum(*m, sizeof(struct eonhdr)))
    865 		return (EINVAL);
    866 	m_adj(*m, sizeof(*ehdr));
    867 	return (0);
    868 }
    869 #endif /*ISO*/
    870 #endif /*NGIF > 0*/
    871