Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.26
      1 /*	$NetBSD: if_gif.c,v 1.26 2001/02/21 00:17:09 itojun Exp $	*/
      2 /*	$KAME: if_gif.c,v 1.43 2001/02/20 08:51:07 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 	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 		ifp->if_flags |= (IFF_UP | IFF_RUNNING);
    670 		if_up(ifp);		/* send up RTM_IFINFO */
    671 
    672 		error = 0;
    673 		break;
    674 
    675 #ifdef SIOCDIFPHYADDR
    676 	case SIOCDIFPHYADDR:
    677 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    678 			break;
    679 		gif_delete_tunnel(sc);
    680 		break;
    681 #endif
    682 
    683 	case SIOCGIFPSRCADDR:
    684 #ifdef INET6
    685 	case SIOCGIFPSRCADDR_IN6:
    686 #endif /* INET6 */
    687 		if (sc->gif_psrc == NULL) {
    688 			error = EADDRNOTAVAIL;
    689 			goto bad;
    690 		}
    691 		src = sc->gif_psrc;
    692 		switch (cmd) {
    693 #ifdef INET
    694 		case SIOCGIFPSRCADDR:
    695 			dst = &ifr->ifr_addr;
    696 			size = sizeof(ifr->ifr_addr);
    697 			break;
    698 #endif /* INET */
    699 #ifdef INET6
    700 		case SIOCGIFPSRCADDR_IN6:
    701 			dst = (struct sockaddr *)
    702 				&(((struct in6_ifreq *)data)->ifr_addr);
    703 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    704 			break;
    705 #endif /* INET6 */
    706 		default:
    707 			error = EADDRNOTAVAIL;
    708 			goto bad;
    709 		}
    710 		if (src->sa_len > size)
    711 			return EINVAL;
    712 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    713 		break;
    714 
    715 	case SIOCGIFPDSTADDR:
    716 #ifdef INET6
    717 	case SIOCGIFPDSTADDR_IN6:
    718 #endif /* INET6 */
    719 		if (sc->gif_pdst == NULL) {
    720 			error = EADDRNOTAVAIL;
    721 			goto bad;
    722 		}
    723 		src = sc->gif_pdst;
    724 		switch (cmd) {
    725 #ifdef INET
    726 		case SIOCGIFPDSTADDR:
    727 			dst = &ifr->ifr_addr;
    728 			size = sizeof(ifr->ifr_addr);
    729 			break;
    730 #endif /* INET */
    731 #ifdef INET6
    732 		case SIOCGIFPDSTADDR_IN6:
    733 			dst = (struct sockaddr *)
    734 				&(((struct in6_ifreq *)data)->ifr_addr);
    735 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    736 			break;
    737 #endif /* INET6 */
    738 		default:
    739 			error = EADDRNOTAVAIL;
    740 			goto bad;
    741 		}
    742 		if (src->sa_len > size)
    743 			return EINVAL;
    744 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    745 		break;
    746 
    747 	case SIOCGLIFPHYADDR:
    748 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    749 			error = EADDRNOTAVAIL;
    750 			goto bad;
    751 		}
    752 
    753 		/* copy src */
    754 		src = sc->gif_psrc;
    755 		dst = (struct sockaddr *)
    756 			&(((struct if_laddrreq *)data)->addr);
    757 		size = sizeof(((struct if_laddrreq *)data)->addr);
    758 		if (src->sa_len > size)
    759 			return EINVAL;
    760 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    761 
    762 		/* copy dst */
    763 		src = sc->gif_pdst;
    764 		dst = (struct sockaddr *)
    765 			&(((struct if_laddrreq *)data)->dstaddr);
    766 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
    767 		if (src->sa_len > size)
    768 			return EINVAL;
    769 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    770 		break;
    771 
    772 	case SIOCSIFFLAGS:
    773 		/* if_ioctl() takes care of it */
    774 		break;
    775 
    776 	default:
    777 		error = EINVAL;
    778 		break;
    779 	}
    780  bad:
    781 	return error;
    782 }
    783 
    784 void
    785 gif_delete_tunnel(sc)
    786 	struct gif_softc *sc;
    787 {
    788 	int s;
    789 
    790 	s = splsoftnet();
    791 
    792 	if (sc->gif_psrc) {
    793 		free((caddr_t)sc->gif_psrc, M_IFADDR);
    794 		sc->gif_psrc = NULL;
    795 	}
    796 	if (sc->gif_pdst) {
    797 		free((caddr_t)sc->gif_pdst, M_IFADDR);
    798 		sc->gif_pdst = NULL;
    799 	}
    800 	/* change the IFF_UP flag as well? */
    801 
    802 	splx(s);
    803 }
    804 
    805 #ifdef ISO
    806 struct eonhdr {
    807 	u_int8_t version;
    808 	u_int8_t class;
    809 	u_int16_t cksum;
    810 };
    811 
    812 /*
    813  * prepend EON header to ISO PDU
    814  */
    815 static int
    816 gif_eon_encap(struct mbuf **m)
    817 {
    818 	struct eonhdr *ehdr;
    819 
    820 	M_PREPEND(*m, sizeof(*ehdr), M_DONTWAIT);
    821 	if (*m && (*m)->m_len < sizeof(*ehdr))
    822 		*m = m_pullup(*m, sizeof(*ehdr));
    823 	if (*m == NULL) {
    824 		printf("ENOBUFS in gif_eon_encap %d\n", __LINE__);
    825 		return ENOBUFS;
    826 	}
    827 	ehdr = mtod(*m, struct eonhdr *);
    828 	ehdr->version = 1;
    829 	ehdr->class = 0;		/* always unicast */
    830 #if 0
    831 	/* calculate the checksum of the eonhdr */
    832 	{
    833 		struct mbuf mhead;
    834 		memset(&mhead, 0, sizeof(mhead));
    835 		ehdr->cksum = 0;
    836 		mhead.m_data = (caddr_t)ehdr;
    837 		mhead.m_len = sizeof(*ehdr);
    838 		mhead.m_next = 0;
    839 		iso_gen_csum(&mhead, offsetof(struct eonhdr, cksum),
    840 		    mhead.m_len);
    841 	}
    842 #else
    843 	/* since the data is always constant we'll just plug the value in */
    844 	ehdr->cksum = htons(0xfc02);
    845 #endif
    846 	return (0);
    847 }
    848 
    849 /*
    850  * remove EON header and check checksum
    851  */
    852 static int
    853 gif_eon_decap(struct ifnet *gifp, struct mbuf **m)
    854 {
    855 	struct eonhdr *ehdr;
    856 
    857 	if ((*m)->m_len < sizeof(*ehdr) &&
    858 	    (*m = m_pullup(*m, sizeof(*ehdr))) == 0) {
    859 		gifp->if_ierrors++;
    860 		return (EINVAL);
    861 	}
    862 	if (iso_check_csum(*m, sizeof(struct eonhdr)))
    863 		return (EINVAL);
    864 	m_adj(*m, sizeof(*ehdr));
    865 	return (0);
    866 }
    867 #endif /*ISO*/
    868 #endif /*NGIF > 0*/
    869