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