Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.24
      1 /*	$NetBSD: if_gif.c,v 1.24 2001/02/20 08:48:27 itojun Exp $	*/
      2 /*	$KAME: if_gif.c,v 1.42 2001/02/20 08:31:06 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 	bzero(sc, 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 	ifp->if_lastchange = time;
    302 	m->m_flags &= ~(M_BCAST|M_MCAST);
    303 	if (!(ifp->if_flags & IFF_UP) ||
    304 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    305 		m_freem(m);
    306 		error = ENETDOWN;
    307 		goto end;
    308 	}
    309 
    310 #if NBPFILTER > 0
    311 	if (ifp->if_bpf) {
    312 		/*
    313 		 * We need to prepend the address family as
    314 		 * a four byte field.  Cons up a dummy header
    315 		 * to pacify bpf.  This is safe because bpf
    316 		 * will only read from the mbuf (i.e., it won't
    317 		 * try to free it or keep a pointer a to it).
    318 		 */
    319 		struct mbuf m0;
    320 		u_int32_t af = dst->sa_family;
    321 
    322 		m0.m_next = m;
    323 		m0.m_len = 4;
    324 		m0.m_data = (char *)&af;
    325 
    326 #ifdef HAVE_OLD_BPF
    327 		bpf_mtap(ifp, &m0);
    328 #else
    329 		bpf_mtap(ifp->if_bpf, &m0);
    330 #endif
    331 	}
    332 #endif
    333 	ifp->if_opackets++;
    334 	ifp->if_obytes += m->m_pkthdr.len;
    335 
    336 	/* inner AF-specific encapsulation */
    337 	switch (dst->sa_family) {
    338 #ifdef ISO
    339 	case AF_ISO:
    340 		error = gif_eon_encap(&m);
    341 		if (error)
    342 			goto end;
    343 #endif
    344 	default:
    345 		break;
    346 	}
    347 
    348 	/* XXX should we check if our outer source is legal? */
    349 
    350 	/* dispatch to output logic based on outer AF */
    351 	switch (sc->gif_psrc->sa_family) {
    352 #ifdef INET
    353 	case AF_INET:
    354 		error = in_gif_output(ifp, dst->sa_family, m, rt);
    355 		break;
    356 #endif
    357 #ifdef INET6
    358 	case AF_INET6:
    359 		error = in6_gif_output(ifp, dst->sa_family, m, rt);
    360 		break;
    361 #endif
    362 	default:
    363 		m_freem(m);
    364 		error = ENETDOWN;
    365 	}
    366 
    367   end:
    368 	called = 0;		/* reset recursion counter */
    369 	if (error) ifp->if_oerrors++;
    370 	return error;
    371 }
    372 
    373 void
    374 gif_input(m, af, gifp)
    375 	struct mbuf *m;
    376 	int af;
    377 	struct ifnet *gifp;
    378 {
    379 	int s, isr;
    380 	struct ifqueue *ifq = 0;
    381 
    382 	if (gifp == NULL) {
    383 		/* just in case */
    384 		m_freem(m);
    385 		return;
    386 	}
    387 
    388 	m->m_pkthdr.rcvif = gifp;
    389 
    390 #if NBPFILTER > 0
    391 	if (gifp->if_bpf) {
    392 		/*
    393 		 * We need to prepend the address family as
    394 		 * a four byte field.  Cons up a dummy header
    395 		 * to pacify bpf.  This is safe because bpf
    396 		 * will only read from the mbuf (i.e., it won't
    397 		 * try to free it or keep a pointer a to it).
    398 		 */
    399 		struct mbuf m0;
    400 		u_int32_t af1 = af;
    401 
    402 		m0.m_next = m;
    403 		m0.m_len = 4;
    404 		m0.m_data = (char *)&af1;
    405 
    406 #ifdef HAVE_OLD_BPF
    407 		bpf_mtap(gifp, &m0);
    408 #else
    409 		bpf_mtap(gifp->if_bpf, &m0);
    410 #endif
    411 	}
    412 #endif /*NBPFILTER > 0*/
    413 
    414 	/*
    415 	 * Put the packet to the network layer input queue according to the
    416 	 * specified address family.
    417 	 * Note: older versions of gif_input directly called network layer
    418 	 * input functions, e.g. ip6_input, here. We changed the policy to
    419 	 * prevent too many recursive calls of such input functions, which
    420 	 * might cause kernel panic. But the change may introduce another
    421 	 * problem; if the input queue is full, packets are discarded.
    422 	 * We believed it rarely occurs and changed the policy. If we find
    423 	 * it occurs more times than we thought, we may change the policy
    424 	 * again.
    425 	 */
    426 	switch (af) {
    427 #ifdef INET
    428 	case AF_INET:
    429 		ifq = &ipintrq;
    430 		isr = NETISR_IP;
    431 		break;
    432 #endif
    433 #ifdef INET6
    434 	case AF_INET6:
    435 		ifq = &ip6intrq;
    436 		isr = NETISR_IPV6;
    437 		break;
    438 #endif
    439 #ifdef ISO
    440 	case AF_ISO:
    441 		if (gif_eon_decap(gifp, &m)) {
    442 			if (m)
    443 			       m_freem(m);
    444 			return;
    445 		}
    446 		ifq = &clnlintrq;
    447 		isr = NETISR_ISO;
    448 		break;
    449 #endif
    450 	default:
    451 		m_freem(m);
    452 		return;
    453 	}
    454 
    455 	s = splimp();
    456 	if (IF_QFULL(ifq)) {
    457 		IF_DROP(ifq);	/* update statistics */
    458 		m_freem(m);
    459 		splx(s);
    460 		return;
    461 	}
    462 	IF_ENQUEUE(ifq, m);
    463 	/* we need schednetisr since the address family may change */
    464 	schednetisr(isr);
    465 	gifp->if_ipackets++;
    466 	gifp->if_ibytes += m->m_pkthdr.len;
    467 	splx(s);
    468 
    469 	return;
    470 }
    471 
    472 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
    473 int
    474 gif_ioctl(ifp, cmd, data)
    475 	struct ifnet *ifp;
    476 	u_long cmd;
    477 	caddr_t data;
    478 {
    479 	struct proc *p = curproc;	/* XXX */
    480 	struct gif_softc *sc  = (struct gif_softc*)ifp;
    481 	struct ifreq     *ifr = (struct ifreq*)data;
    482 	int error = 0, size;
    483 	struct sockaddr *dst, *src;
    484 	struct sockaddr *sa;
    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 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    536 			break;
    537 		switch (cmd) {
    538 #ifdef INET
    539 		case SIOCSIFPHYADDR:
    540 			src = (struct sockaddr *)
    541 				&(((struct in_aliasreq *)data)->ifra_addr);
    542 			dst = (struct sockaddr *)
    543 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
    544 			if (src->sa_len != sizeof(struct sockaddr_in) ||
    545 			    dst->sa_len != sizeof(struct sockaddr_in))
    546 				return EINVAL;
    547 			if (src->sa_family != AF_INET ||
    548 			    dst->sa_family != AF_INET)
    549 				return EAFNOSUPPORT;
    550 			break;
    551 #endif
    552 #ifdef INET6
    553 		case SIOCSIFPHYADDR_IN6:
    554 			src = (struct sockaddr *)
    555 				&(((struct in6_aliasreq *)data)->ifra_addr);
    556 			dst = (struct sockaddr *)
    557 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    558 			if (src->sa_len != sizeof(struct sockaddr_in6) ||
    559 			    dst->sa_len != sizeof(struct sockaddr_in6))
    560 				return EINVAL;
    561 			if (src->sa_family != AF_INET6 ||
    562 			    dst->sa_family != AF_INET6)
    563 				return EAFNOSUPPORT;
    564 			break;
    565 #endif
    566 		}
    567 
    568 		for (sc2 = LIST_FIRST(&gif_softc_list); sc2 != NULL;
    569 		     sc2 = LIST_NEXT(sc2, gif_list)) {
    570 			if (sc2 == sc)
    571 				continue;
    572 			if (!sc2->gif_pdst || !sc2->gif_psrc)
    573 				continue;
    574 			if (sc2->gif_pdst->sa_family != dst->sa_family ||
    575 			    sc2->gif_pdst->sa_len != dst->sa_len ||
    576 			    sc2->gif_psrc->sa_family != src->sa_family ||
    577 			    sc2->gif_psrc->sa_len != src->sa_len)
    578 				continue;
    579 
    580 			/* can't configure same pair of address onto two gifs */
    581 			if (bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
    582 			    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
    583 				error = EADDRNOTAVAIL;
    584 				goto bad;
    585 			}
    586 
    587 			/* can't configure multiple multi-dest interfaces */
    588 #define multidest(x) \
    589 	(((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
    590 #ifdef INET6
    591 #define multidest6(x) \
    592 	(IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
    593 #endif
    594 			if (dst->sa_family == AF_INET &&
    595 			    multidest(dst) && multidest(sc2->gif_pdst)) {
    596 				error = EADDRNOTAVAIL;
    597 				goto bad;
    598 			}
    599 #ifdef INET6
    600 			if (dst->sa_family == AF_INET6 &&
    601 			    multidest6(dst) && multidest6(sc2->gif_pdst)) {
    602 				error = EADDRNOTAVAIL;
    603 				goto bad;
    604 			}
    605 #endif
    606 		}
    607 
    608 		if (src->sa_family != dst->sa_family ||
    609 		    src->sa_len != dst->sa_len) {
    610 			error = EINVAL;
    611 			break;
    612 		}
    613 		switch (src->sa_family) {
    614 #ifdef INET
    615 		case AF_INET:
    616 			size = sizeof(struct sockaddr_in);
    617 			break;
    618 #endif
    619 #ifdef INET6
    620 		case AF_INET6:
    621 			size = sizeof(struct sockaddr_in6);
    622 			break;
    623 #endif
    624 		default:
    625 			error = EAFNOSUPPORT;
    626 			goto bad;
    627 		}
    628 		if (src->sa_len != size) {
    629 			error = EINVAL;
    630 			break;
    631 		}
    632 
    633 		if (sc->gif_psrc)
    634 			free((caddr_t)sc->gif_psrc, M_IFADDR);
    635 		sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK);
    636 		bcopy((caddr_t)src, (caddr_t)sa, size);
    637 		sc->gif_psrc = sa;
    638 
    639 		if (sc->gif_pdst)
    640 			free((caddr_t)sc->gif_pdst, M_IFADDR);
    641 		sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK);
    642 		bcopy((caddr_t)dst, (caddr_t)sa, size);
    643 		sc->gif_pdst = sa;
    644 
    645 		ifp->if_flags |= IFF_UP;
    646 		if_up(ifp);		/* send up RTM_IFINFO */
    647 
    648 		error = 0;
    649 		break;
    650 
    651 #ifdef SIOCDIFPHYADDR
    652 	case SIOCDIFPHYADDR:
    653 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    654 			break;
    655 		gif_delete_tunnel(sc);
    656 		break;
    657 #endif
    658 
    659 	case SIOCGIFPSRCADDR:
    660 #ifdef INET6
    661 	case SIOCGIFPSRCADDR_IN6:
    662 #endif /* INET6 */
    663 		if (sc->gif_psrc == NULL) {
    664 			error = EADDRNOTAVAIL;
    665 			goto bad;
    666 		}
    667 		src = sc->gif_psrc;
    668 		switch (cmd) {
    669 #ifdef INET
    670 		case SIOCGIFPSRCADDR:
    671 			dst = &ifr->ifr_addr;
    672 			size = sizeof(ifr->ifr_addr);
    673 			break;
    674 #endif /* INET */
    675 #ifdef INET6
    676 		case SIOCGIFPSRCADDR_IN6:
    677 			dst = (struct sockaddr *)
    678 				&(((struct in6_ifreq *)data)->ifr_addr);
    679 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    680 			break;
    681 #endif /* INET6 */
    682 		default:
    683 			error = EADDRNOTAVAIL;
    684 			goto bad;
    685 		}
    686 		if (src->sa_len > size)
    687 			return EINVAL;
    688 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    689 		break;
    690 
    691 	case SIOCGIFPDSTADDR:
    692 #ifdef INET6
    693 	case SIOCGIFPDSTADDR_IN6:
    694 #endif /* INET6 */
    695 		if (sc->gif_pdst == NULL) {
    696 			error = EADDRNOTAVAIL;
    697 			goto bad;
    698 		}
    699 		src = sc->gif_pdst;
    700 		switch (cmd) {
    701 #ifdef INET
    702 		case SIOCGIFPDSTADDR:
    703 			dst = &ifr->ifr_addr;
    704 			size = sizeof(ifr->ifr_addr);
    705 			break;
    706 #endif /* INET */
    707 #ifdef INET6
    708 		case SIOCGIFPDSTADDR_IN6:
    709 			dst = (struct sockaddr *)
    710 				&(((struct in6_ifreq *)data)->ifr_addr);
    711 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    712 			break;
    713 #endif /* INET6 */
    714 		default:
    715 			error = EADDRNOTAVAIL;
    716 			goto bad;
    717 		}
    718 		if (src->sa_len > size)
    719 			return EINVAL;
    720 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    721 		break;
    722 
    723 	case SIOCSIFFLAGS:
    724 		/* if_ioctl() takes care of it */
    725 		break;
    726 
    727 	default:
    728 		error = EINVAL;
    729 		break;
    730 	}
    731  bad:
    732 	return error;
    733 }
    734 
    735 void
    736 gif_delete_tunnel(sc)
    737 	struct gif_softc *sc;
    738 {
    739 	int s;
    740 
    741 	s = splsoftnet();
    742 
    743 	if (sc->gif_psrc) {
    744 		free((caddr_t)sc->gif_psrc, M_IFADDR);
    745 		sc->gif_psrc = NULL;
    746 	}
    747 	if (sc->gif_pdst) {
    748 		free((caddr_t)sc->gif_pdst, M_IFADDR);
    749 		sc->gif_pdst = NULL;
    750 	}
    751 	/* change the IFF_UP flag as well? */
    752 
    753 	splx(s);
    754 }
    755 
    756 #ifdef ISO
    757 struct eonhdr {
    758 	u_int8_t version;
    759 	u_int8_t class;
    760 	u_int16_t cksum;
    761 } *ehdr;
    762 
    763 /*
    764  * prepend EON header to ISO PDU
    765  */
    766 static int
    767 gif_eon_encap(struct mbuf **m)
    768 {
    769 	struct eonhdr *ehdr;
    770 
    771 	M_PREPEND(*m, sizeof(*ehdr), M_DONTWAIT);
    772 	if (*m && (*m)->m_len < sizeof(*ehdr))
    773 		*m = m_pullup(*m, sizeof(*ehdr));
    774 	if (*m == NULL) {
    775 		printf("ENOBUFS in gif_eon_encap %d\n", __LINE__);
    776 		return ENOBUFS;
    777 	}
    778 	ehdr = mtod(*m, struct eonhdr *);
    779 	ehdr->version = 1;
    780 	ehdr->class = 0;		/* always unicast */
    781 #if 0
    782 	/* calculate the checksum of the eonhdr */
    783 	{
    784 		struct mbuf mhead;
    785 		memset(&mhead, 0, sizeof(mhead));
    786 		ehdr->cksum = 0;
    787 		mhead.m_data = (caddr_t)ehdr;
    788 		mhead.m_len = sizeof(*ehdr);
    789 		mhead.m_next = 0;
    790 		iso_gen_csum(&mhead, offsetof(struct eonhdr, cksum),
    791 		    mhead.m_len);
    792 	}
    793 #else
    794 	/* since the data is always constant we'll just plug the value in */
    795 	ehdr->cksum = htons(0xfc02);
    796 #endif
    797 	return (0);
    798 }
    799 
    800 /*
    801  * remove EON header and check checksum
    802  */
    803 static int
    804 gif_eon_decap(struct ifnet *gifp, struct mbuf **m)
    805 {
    806 	struct eonhdr *ehdr;
    807 
    808 	if ((*m)->m_len < sizeof(*ehdr) &&
    809 	    (*m = m_pullup(*m, sizeof(*ehdr))) == 0) {
    810 		gifp->if_ierrors++;
    811 		return (EINVAL);
    812 	}
    813 	if (iso_check_csum(*m, sizeof(struct eonhdr)))
    814 		return (EINVAL);
    815 	m_adj(*m, sizeof(*ehdr));
    816 	return (0);
    817 }
    818 #endif /*ISO*/
    819 #endif /*NGIF > 0*/
    820