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