Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.3
      1 /*	$NetBSD: if_gif.c,v 1.3 1999/12/02 07:18:44 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * gif.c
     34  */
     35 
     36 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
     37 #include "opt_inet.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/socket.h>
     45 #include <sys/sockio.h>
     46 #include <sys/errno.h>
     47 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
     48 #include <sys/ioctl.h>
     49 #endif
     50 #include <sys/time.h>
     51 #include <sys/syslog.h>
     52 #include <machine/cpu.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 #ifdef	INET
     61 #include <netinet/in.h>
     62 #include <netinet/in_systm.h>
     63 #include <netinet/in_var.h>
     64 #include <netinet/ip.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/in6_ifattach.h>
     77 #endif /* INET6 */
     78 
     79 #include <net/if_gif.h>
     80 
     81 #include "gif.h"
     82 #include "bpfilter.h"
     83 
     84 #if NGIF > 0
     85 
     86 void gifattach __P((void *));
     87 
     88 /*
     89  * gif global variable definitions
     90  */
     91 int ngif = NGIF;		/* number of interfaces */
     92 struct gif_softc *gif = 0;
     93 
     94 void
     95 gifattach(dummy)
     96 	void *dummy;
     97 {
     98 	register struct gif_softc *sc;
     99 	register int i;
    100 
    101 	gif = sc = malloc (ngif * sizeof(struct gif_softc), M_DEVBUF, M_WAIT);
    102 	bzero(sc, ngif * sizeof(struct gif_softc));
    103 	for (i = 0; i < ngif; sc++, i++) {
    104 		sprintf(sc->gif_if.if_xname, "gif%d", i);
    105 		sc->gif_if.if_mtu    = GIF_MTU;
    106 		sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
    107 		sc->gif_if.if_ioctl  = gif_ioctl;
    108 		sc->gif_if.if_output = gif_output;
    109 		sc->gif_if.if_type   = IFT_GIF;
    110 		if_attach(&sc->gif_if);
    111 #if NBPFILTER > 0
    112 		bpfattach(&sc->gif_if.if_bpf, &sc->gif_if, DLT_NULL, sizeof(u_int));
    113 #endif
    114 	}
    115 }
    116 
    117 #ifdef __FreeBSD__
    118 PSEUDO_SET(gifattach, if_gif);
    119 #endif
    120 
    121 int
    122 gif_output(ifp, m, dst, rt)
    123 	struct ifnet *ifp;
    124 	struct mbuf *m;
    125 	struct sockaddr *dst;
    126 	struct rtentry *rt;	/* added in net2 */
    127 {
    128 	register struct gif_softc *sc = (struct gif_softc*)ifp;
    129 	int error = 0;
    130 	static int called = 0;	/* XXX: MUTEX */
    131 	int calllimit = 10;	/* XXX: adhoc */
    132 
    133 	/*
    134 	 * gif may cause infinite recursion calls when misconfigured.
    135 	 * We'll prevent this by introducing upper limit.
    136 	 * XXX: this mechanism may introduce another problem about
    137 	 *      mutual exclusion of the variable CALLED, especially if we
    138 	 *      use kernel thread.
    139 	 */
    140 	if (++called >= calllimit) {
    141 		log(LOG_NOTICE,
    142 		    "gif_output: recursively called too many times(%d)\n",
    143 		    called);
    144 		m_freem(m);
    145 		error = EIO;	/* is there better errno? */
    146 		goto end;
    147 	}
    148 
    149 	ifp->if_lastchange = time;
    150 	m->m_flags &= ~(M_BCAST|M_MCAST);
    151 	if (!(ifp->if_flags & IFF_UP) ||
    152 #if 0
    153 	    sc->gif_flags & GIFF_INUSE ||
    154 #endif
    155 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    156 		m_freem(m);
    157 		error = ENETDOWN;
    158 		goto end;
    159 	}
    160 
    161 #if NBPFILTER > 0
    162 	if (ifp->if_bpf) {
    163 		/*
    164 		 * We need to prepend the address family as
    165 		 * a four byte field.  Cons up a dummy header
    166 		 * to pacify bpf.  This is safe because bpf
    167 		 * will only read from the mbuf (i.e., it won't
    168 		 * try to free it or keep a pointer a to it).
    169 		 */
    170 		struct mbuf m0;
    171 		u_int af = dst->sa_family;
    172 
    173 		m0.m_next = m;
    174 		m0.m_len = 4;
    175 		m0.m_data = (char *)&af;
    176 
    177 		bpf_mtap(ifp->if_bpf, &m0);
    178 	}
    179 #endif
    180 	ifp->if_opackets++;
    181 	ifp->if_obytes += m->m_pkthdr.len;
    182 #if 0
    183 	s = splnet();
    184 	sc->gif_flags |= GIFF_INUSE;
    185 #endif
    186 
    187 	switch (sc->gif_psrc->sa_family) {
    188 #ifdef INET
    189 	case AF_INET:
    190 		error = in_gif_output(ifp, dst->sa_family, m, rt);
    191 		break;
    192 #endif
    193 #ifdef INET6
    194 	case AF_INET6:
    195 		error = in6_gif_output(ifp, dst->sa_family, m, rt);
    196 		break;
    197 #endif
    198 	default:
    199 		m_freem(m);
    200 		error = ENETDOWN;
    201 	}
    202 #if 0
    203 	sc->gif_flags &= ~GIFF_INUSE;
    204 	splx(s);
    205 #endif
    206 
    207   end:
    208 	called = 0;		/* reset recursion counter */
    209 	if (error) ifp->if_oerrors++;
    210 	return error;
    211 }
    212 
    213 void
    214 gif_input(m, af, gifp)
    215 	struct mbuf *m;
    216 	int af;
    217 	struct ifnet *gifp;
    218 {
    219 	int s, isr;
    220 	register struct ifqueue *ifq = 0;
    221 
    222 	if (gifp == NULL) {
    223 		/* just in case */
    224 		m_freem(m);
    225 		return;
    226 	}
    227 
    228 	if (m->m_pkthdr.rcvif)
    229 		m->m_pkthdr.rcvif = gifp;
    230 
    231 #if NBPFILTER > 0
    232 	if (gifp->if_bpf) {
    233 		/*
    234 		 * We need to prepend the address family as
    235 		 * a four byte field.  Cons up a dummy header
    236 		 * to pacify bpf.  This is safe because bpf
    237 		 * will only read from the mbuf (i.e., it won't
    238 		 * try to free it or keep a pointer a to it).
    239 		 */
    240 		struct mbuf m0;
    241 		u_int af = AF_INET6;
    242 
    243 		m0.m_next = m;
    244 		m0.m_len = 4;
    245 		m0.m_data = (char *)&af;
    246 
    247 		bpf_mtap(gifp->if_bpf, &m0);
    248 	}
    249 #endif /*NBPFILTER > 0*/
    250 
    251 	/*
    252 	 * Put the packet to the network layer input queue according to the
    253 	 * specified address family.
    254 	 * Note: older versions of gif_input directly called network layer
    255 	 * input functions, e.g. ip6_input, here. We changed the policy to
    256 	 * prevent too many recursive calls of such input functions, which
    257 	 * might cause kernel panic. But the change may introduce another
    258 	 * problem; if the input queue is full, packets are discarded.
    259 	 * We believed it rarely occurs and changed the policy. If we find
    260 	 * it occurs more times than we thought, we may change the policy
    261 	 * again.
    262 	 */
    263 	switch (af) {
    264 #ifdef INET
    265 	case AF_INET:
    266 		ifq = &ipintrq;
    267 		isr = NETISR_IP;
    268 		break;
    269 #endif
    270 #ifdef INET6
    271 	case AF_INET6:
    272 		ifq = &ip6intrq;
    273 		isr = NETISR_IPV6;
    274 		break;
    275 #endif
    276 	default:
    277 		m_freem(m);
    278 		return;
    279 	}
    280 
    281 	s = splimp();
    282 	if (IF_QFULL(ifq)) {
    283 		IF_DROP(ifq);	/* update statistics */
    284 		m_freem(m);
    285 		splx(s);
    286 		return;
    287 	}
    288 	IF_ENQUEUE(ifq, m);
    289 	/* we need schednetisr since the address family may change */
    290 	schednetisr(isr);
    291 	gifp->if_ipackets++;
    292 	gifp->if_ibytes += m->m_pkthdr.len;
    293 	splx(s);
    294 
    295 	return;
    296 }
    297 
    298 
    299 int
    300 gif_ioctl(ifp, cmd, data)
    301 	struct ifnet *ifp;
    302 	u_long cmd;
    303 	caddr_t data;
    304 {
    305 	struct gif_softc *sc  = (struct gif_softc*)ifp;
    306 	struct ifreq     *ifr = (struct ifreq*)data;
    307 	int error = 0, size;
    308 	struct sockaddr *sa, *dst, *src;
    309 
    310 	switch (cmd) {
    311 	case SIOCSIFADDR:
    312 		break;
    313 
    314 	case SIOCSIFDSTADDR:
    315 		break;
    316 
    317 	case SIOCADDMULTI:
    318 	case SIOCDELMULTI:
    319 		switch (ifr->ifr_addr.sa_family) {
    320 #ifdef INET
    321 		case AF_INET:	/* IP supports Multicast */
    322 			break;
    323 #endif /* INET */
    324 #ifdef INET6
    325 		case AF_INET6:	/* IP6 supports Multicast */
    326 			break;
    327 #endif /* INET6 */
    328 		default:  /* Other protocols doesn't support Multicast */
    329 			error = EAFNOSUPPORT;
    330 			break;
    331 		}
    332 		break;
    333 
    334 #ifdef	SIOCSIFMTU /* xxx */
    335 	case SIOCGIFMTU:
    336 		break;
    337 	case SIOCSIFMTU:
    338 		{
    339 #ifdef __bsdi__
    340 			short mtu;
    341 			mtu = *(short *)ifr->ifr_data;
    342 #else
    343 			u_long mtu;
    344 			mtu = ifr->ifr_mtu;
    345 #endif
    346 			if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
    347 				return (EINVAL);
    348 			}
    349 			ifp->if_mtu = mtu;
    350 		}
    351 		break;
    352 #endif /* SIOCSIFMTU */
    353 
    354 	case SIOCSIFPHYADDR:
    355 #ifdef INET6
    356 	case SIOCSIFPHYADDR_IN6:
    357 #endif /* INET6 */
    358 #ifdef INET6
    359 		if (found_first_ifid)
    360 			in6_ifattach(ifp, IN6_IFT_P2P, NULL, 1);
    361 		else {
    362 			error = ENXIO; /* xxx */
    363 			goto bad;
    364 		}
    365 #endif /* INET6 */
    366 
    367 		switch (ifr->ifr_addr.sa_family) {
    368 #ifdef INET
    369 		case AF_INET:
    370 			src = (struct sockaddr *)
    371 				&(((struct in_aliasreq *)data)->ifra_addr);
    372 			dst = (struct sockaddr *)
    373 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
    374 
    375 			/* only one gif can have dst = INADDR_ANY */
    376 #define satosaddr(sa) (((struct sockaddr_in *)(sa))->sin_addr.s_addr)
    377 
    378 			if (satosaddr(dst) == INADDR_ANY) {
    379 				int i;
    380 				struct gif_softc *sc2;
    381 
    382 			  	for (i = 0, sc2 = gif; i < ngif; i++, sc2++) {
    383 					if (sc2 == sc) continue;
    384 					if (sc2->gif_pdst &&
    385 					    satosaddr(sc2->gif_pdst)
    386 						== INADDR_ANY) {
    387 					    error = EADDRNOTAVAIL;
    388 					    goto bad;
    389 					}
    390 				}
    391 			}
    392 			size = sizeof(struct sockaddr_in);
    393 			break;
    394 #endif /* INET */
    395 #ifdef INET6
    396 		case AF_INET6:
    397 			src = (struct sockaddr *)
    398 				&(((struct in6_aliasreq *)data)->ifra_addr);
    399 			dst = (struct sockaddr *)
    400 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    401 
    402 			/* only one gif can have dst = in6addr_any */
    403 #define satoin6(sa) (&((struct sockaddr_in6 *)(sa))->sin6_addr)
    404 
    405 			if (IN6_IS_ADDR_UNSPECIFIED(satoin6(dst))) {
    406 				int i;
    407 				struct gif_softc *sc2;
    408 
    409 			  	for (i = 0, sc2 = gif; i < ngif; i++, sc2++) {
    410 					if (sc2 == sc) continue;
    411 					if (sc2->gif_pdst &&
    412 					    IN6_IS_ADDR_UNSPECIFIED(
    413 						satoin6(sc2->gif_pdst)
    414 								    )) {
    415 					    error = EADDRNOTAVAIL;
    416 					    goto bad;
    417 					}
    418 				}
    419 			}
    420 			size = sizeof(struct sockaddr_in6);
    421 			break;
    422 #endif /* INET6 */
    423 		default:
    424 			error = EPROTOTYPE;
    425 			goto bad;
    426 			break;
    427 		}
    428 		if (sc->gif_psrc != NULL)
    429 			free((caddr_t)sc->gif_psrc, M_IFADDR);
    430 		if (sc->gif_pdst != NULL)
    431 			free((caddr_t)sc->gif_pdst, M_IFADDR);
    432 
    433 		sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK);
    434 		bzero((caddr_t)sa, size);
    435 		bcopy((caddr_t)src, (caddr_t)sa, size);
    436 		sc->gif_psrc = sa;
    437 
    438 		sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK);
    439 		bzero((caddr_t)sa, size);
    440 		bcopy((caddr_t)dst, (caddr_t)sa, size);
    441 		sc->gif_pdst = sa;
    442 
    443 		ifp->if_flags |= (IFF_UP|IFF_RUNNING);
    444 		if_up(ifp);		/* send up RTM_IFINFO */
    445 
    446 		break;
    447 
    448 	case SIOCGIFPSRCADDR:
    449 #ifdef INET6
    450 	case SIOCGIFPSRCADDR_IN6:
    451 #endif /* INET6 */
    452 		if (sc->gif_psrc == NULL) {
    453 			error = EADDRNOTAVAIL;
    454 			goto bad;
    455 		}
    456 		src = sc->gif_psrc;
    457 		switch (sc->gif_psrc->sa_family) {
    458 #ifdef INET
    459 		case AF_INET:
    460 			dst = &ifr->ifr_addr;
    461 			size = sizeof(struct sockaddr_in);
    462 			break;
    463 #endif /* INET */
    464 #ifdef INET6
    465 		case AF_INET6:
    466 			dst = (struct sockaddr *)
    467 				&(((struct in6_ifreq *)data)->ifr_addr);
    468 			size = sizeof(struct sockaddr_in6);
    469 			break;
    470 #endif /* INET6 */
    471 		default:
    472 			error = EADDRNOTAVAIL;
    473 			goto bad;
    474 		}
    475 		bcopy((caddr_t)src, (caddr_t)dst, size);
    476 		break;
    477 
    478 	case SIOCGIFPDSTADDR:
    479 #ifdef INET6
    480 	case SIOCGIFPDSTADDR_IN6:
    481 #endif /* INET6 */
    482 		if (sc->gif_pdst == NULL) {
    483 			error = EADDRNOTAVAIL;
    484 			goto bad;
    485 		}
    486 		src = sc->gif_pdst;
    487 		switch (sc->gif_pdst->sa_family) {
    488 #ifdef INET
    489 		case AF_INET:
    490 			dst = &ifr->ifr_addr;
    491 			size = sizeof(struct sockaddr_in);
    492 			break;
    493 #endif /* INET */
    494 #ifdef INET6
    495 		case AF_INET6:
    496 			dst = (struct sockaddr *)
    497 				&(((struct in6_ifreq *)data)->ifr_addr);
    498 			size = sizeof(struct sockaddr_in6);
    499 			break;
    500 #endif /* INET6 */
    501 		default:
    502 			error = EADDRNOTAVAIL;
    503 			goto bad;
    504 		}
    505 		bcopy((caddr_t)src, (caddr_t)dst, size);
    506 		break;
    507 
    508 	case SIOCSIFFLAGS:
    509 		break;
    510 
    511 	default:
    512 		error = EINVAL;
    513 		break;
    514 	}
    515  bad:
    516 	return error;
    517 }
    518 #endif /*NGIF > 0*/
    519