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