Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.4
      1 /*	$NetBSD: if_gif.c,v 1.4 1999/12/13 15:17:19 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 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
     44 #include <sys/malloc.h>
     45 #endif
     46 #include <sys/mbuf.h>
     47 #include <sys/socket.h>
     48 #include <sys/sockio.h>
     49 #include <sys/errno.h>
     50 #if defined(__FreeBSD__) || __FreeBSD__ >= 3
     51 /*nothing*/
     52 #else
     53 #include <sys/ioctl.h>
     54 #endif
     55 #include <sys/time.h>
     56 #include <sys/syslog.h>
     57 #include <machine/cpu.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_types.h>
     61 #include <net/netisr.h>
     62 #include <net/route.h>
     63 #include <net/bpf.h>
     64 
     65 #ifdef	INET
     66 #include <netinet/in.h>
     67 #include <netinet/in_systm.h>
     68 #include <netinet/in_var.h>
     69 #include <netinet/ip.h>
     70 #include <netinet/in_gif.h>
     71 #endif	/* INET */
     72 
     73 #ifdef INET6
     74 #ifndef INET
     75 #include <netinet/in.h>
     76 #endif
     77 #include <netinet6/in6_var.h>
     78 #include <netinet/ip6.h>
     79 #include <netinet6/ip6_var.h>
     80 #include <netinet6/in6_gif.h>
     81 #endif /* INET6 */
     82 
     83 #include <net/if_gif.h>
     84 
     85 #include "gif.h"
     86 #include "bpfilter.h"
     87 
     88 #include <net/net_osdep.h>
     89 
     90 #if NGIF > 0
     91 
     92 #ifdef __FreeBSD__
     93 void gifattach __P((void *));
     94 #else
     95 void gifattach __P((int));
     96 #endif
     97 
     98 /*
     99  * gif global variable definitions
    100  */
    101 int ngif = NGIF;		/* number of interfaces */
    102 struct gif_softc *gif = 0;
    103 
    104 void
    105 gifattach(dummy)
    106 #ifdef __FreeBSD__
    107 	void *dummy;
    108 #else
    109 	int dummy;
    110 #endif
    111 {
    112 	register struct gif_softc *sc;
    113 	register int i;
    114 
    115 	gif = sc = malloc (ngif * sizeof(struct gif_softc), M_DEVBUF, M_WAIT);
    116 	bzero(sc, ngif * sizeof(struct gif_softc));
    117 	for (i = 0; i < ngif; sc++, i++) {
    118 		sprintf(sc->gif_if.if_xname, "gif%d", i);
    119 		sc->gif_if.if_mtu    = GIF_MTU;
    120 		sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
    121 		sc->gif_if.if_ioctl  = gif_ioctl;
    122 		sc->gif_if.if_output = gif_output;
    123 		sc->gif_if.if_type   = IFT_GIF;
    124 		if_attach(&sc->gif_if);
    125 #if NBPFILTER > 0
    126 		bpfattach(&sc->gif_if.if_bpf, &sc->gif_if, DLT_NULL, sizeof(u_int));
    127 #endif
    128 	}
    129 }
    130 
    131 #ifdef __FreeBSD__
    132 PSEUDO_SET(gifattach, if_gif);
    133 #endif
    134 
    135 int
    136 gif_output(ifp, m, dst, rt)
    137 	struct ifnet *ifp;
    138 	struct mbuf *m;
    139 	struct sockaddr *dst;
    140 	struct rtentry *rt;	/* added in net2 */
    141 {
    142 	register struct gif_softc *sc = (struct gif_softc*)ifp;
    143 	int error = 0;
    144 	static int called = 0;	/* XXX: MUTEX */
    145 	int calllimit = 10;	/* XXX: adhoc */
    146 
    147 	/*
    148 	 * gif may cause infinite recursion calls when misconfigured.
    149 	 * We'll prevent this by introducing upper limit.
    150 	 * XXX: this mechanism may introduce another problem about
    151 	 *      mutual exclusion of the variable CALLED, especially if we
    152 	 *      use kernel thread.
    153 	 */
    154 	if (++called >= calllimit) {
    155 		log(LOG_NOTICE,
    156 		    "gif_output: recursively called too many times(%d)\n",
    157 		    called);
    158 		m_freem(m);
    159 		error = EIO;	/* is there better errno? */
    160 		goto end;
    161 	}
    162 
    163 	ifp->if_lastchange = time;
    164 	m->m_flags &= ~(M_BCAST|M_MCAST);
    165 	if (!(ifp->if_flags & IFF_UP) ||
    166 #if 0
    167 	    sc->gif_flags & GIFF_INUSE ||
    168 #endif
    169 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    170 		m_freem(m);
    171 		error = ENETDOWN;
    172 		goto end;
    173 	}
    174 
    175 #if NBPFILTER > 0
    176 	if (ifp->if_bpf) {
    177 		/*
    178 		 * We need to prepend the address family as
    179 		 * a four byte field.  Cons up a dummy header
    180 		 * to pacify bpf.  This is safe because bpf
    181 		 * will only read from the mbuf (i.e., it won't
    182 		 * try to free it or keep a pointer a to it).
    183 		 */
    184 		struct mbuf m0;
    185 		u_int af = dst->sa_family;
    186 
    187 		m0.m_next = m;
    188 		m0.m_len = 4;
    189 		m0.m_data = (char *)&af;
    190 
    191 		bpf_mtap(ifp->if_bpf, &m0);
    192 	}
    193 #endif
    194 	ifp->if_opackets++;
    195 	ifp->if_obytes += m->m_pkthdr.len;
    196 #if 0
    197 	s = splnet();
    198 	sc->gif_flags |= GIFF_INUSE;
    199 #endif
    200 
    201 	switch (sc->gif_psrc->sa_family) {
    202 #ifdef INET
    203 	case AF_INET:
    204 		error = in_gif_output(ifp, dst->sa_family, m, rt);
    205 		break;
    206 #endif
    207 #ifdef INET6
    208 	case AF_INET6:
    209 		error = in6_gif_output(ifp, dst->sa_family, m, rt);
    210 		break;
    211 #endif
    212 	default:
    213 		m_freem(m);
    214 		error = ENETDOWN;
    215 	}
    216 #if 0
    217 	sc->gif_flags &= ~GIFF_INUSE;
    218 	splx(s);
    219 #endif
    220 
    221   end:
    222 	called = 0;		/* reset recursion counter */
    223 	if (error) ifp->if_oerrors++;
    224 	return error;
    225 }
    226 
    227 void
    228 gif_input(m, af, gifp)
    229 	struct mbuf *m;
    230 	int af;
    231 	struct ifnet *gifp;
    232 {
    233 	int s, isr;
    234 	register struct ifqueue *ifq = 0;
    235 
    236 	if (gifp == NULL) {
    237 		/* just in case */
    238 		m_freem(m);
    239 		return;
    240 	}
    241 
    242 	if (m->m_pkthdr.rcvif)
    243 		m->m_pkthdr.rcvif = gifp;
    244 
    245 #if NBPFILTER > 0
    246 	if (gifp->if_bpf) {
    247 		/*
    248 		 * We need to prepend the address family as
    249 		 * a four byte field.  Cons up a dummy header
    250 		 * to pacify bpf.  This is safe because bpf
    251 		 * will only read from the mbuf (i.e., it won't
    252 		 * try to free it or keep a pointer a to it).
    253 		 */
    254 		struct mbuf m0;
    255 		u_int af = AF_INET6;
    256 
    257 		m0.m_next = m;
    258 		m0.m_len = 4;
    259 		m0.m_data = (char *)&af;
    260 
    261 		bpf_mtap(gifp->if_bpf, &m0);
    262 	}
    263 #endif /*NBPFILTER > 0*/
    264 
    265 	/*
    266 	 * Put the packet to the network layer input queue according to the
    267 	 * specified address family.
    268 	 * Note: older versions of gif_input directly called network layer
    269 	 * input functions, e.g. ip6_input, here. We changed the policy to
    270 	 * prevent too many recursive calls of such input functions, which
    271 	 * might cause kernel panic. But the change may introduce another
    272 	 * problem; if the input queue is full, packets are discarded.
    273 	 * We believed it rarely occurs and changed the policy. If we find
    274 	 * it occurs more times than we thought, we may change the policy
    275 	 * again.
    276 	 */
    277 	switch (af) {
    278 #ifdef INET
    279 	case AF_INET:
    280 		ifq = &ipintrq;
    281 		isr = NETISR_IP;
    282 		break;
    283 #endif
    284 #ifdef INET6
    285 	case AF_INET6:
    286 		ifq = &ip6intrq;
    287 		isr = NETISR_IPV6;
    288 		break;
    289 #endif
    290 	default:
    291 		m_freem(m);
    292 		return;
    293 	}
    294 
    295 	s = splimp();
    296 	if (IF_QFULL(ifq)) {
    297 		IF_DROP(ifq);	/* update statistics */
    298 		m_freem(m);
    299 		splx(s);
    300 		return;
    301 	}
    302 	IF_ENQUEUE(ifq, m);
    303 	/* we need schednetisr since the address family may change */
    304 	schednetisr(isr);
    305 	gifp->if_ipackets++;
    306 	gifp->if_ibytes += m->m_pkthdr.len;
    307 	splx(s);
    308 
    309 	return;
    310 }
    311 
    312 
    313 int
    314 gif_ioctl(ifp, cmd, data)
    315 	struct ifnet *ifp;
    316 	u_long cmd;
    317 	caddr_t data;
    318 {
    319 	struct gif_softc *sc  = (struct gif_softc*)ifp;
    320 	struct ifreq     *ifr = (struct ifreq*)data;
    321 	int error = 0, size;
    322 	struct sockaddr *sa, *dst, *src;
    323 
    324 	switch (cmd) {
    325 	case SIOCSIFADDR:
    326 		break;
    327 
    328 	case SIOCSIFDSTADDR:
    329 		break;
    330 
    331 	case SIOCADDMULTI:
    332 	case SIOCDELMULTI:
    333 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
    334 		switch (ifr->ifr_addr.sa_family) {
    335 #ifdef INET
    336 		case AF_INET:	/* IP supports Multicast */
    337 			break;
    338 #endif /* INET */
    339 #ifdef INET6
    340 		case AF_INET6:	/* IP6 supports Multicast */
    341 			break;
    342 #endif /* INET6 */
    343 		default:  /* Other protocols doesn't support Multicast */
    344 			error = EAFNOSUPPORT;
    345 			break;
    346 		}
    347 #endif /*not FreeBSD3*/
    348 		break;
    349 
    350 #ifdef	SIOCSIFMTU /* xxx */
    351 #ifndef __OpenBSD__
    352 	case SIOCGIFMTU:
    353 		break;
    354 	case SIOCSIFMTU:
    355 		{
    356 #ifdef __bsdi__
    357 			short mtu;
    358 			mtu = *(short *)ifr->ifr_data;
    359 #else
    360 			u_long mtu;
    361 			mtu = ifr->ifr_mtu;
    362 #endif
    363 			if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
    364 				return (EINVAL);
    365 			}
    366 			ifp->if_mtu = mtu;
    367 		}
    368 		break;
    369 #endif
    370 #endif /* SIOCSIFMTU */
    371 
    372 	case SIOCSIFPHYADDR:
    373 #ifdef INET6
    374 	case SIOCSIFPHYADDR_IN6:
    375 #endif /* INET6 */
    376 		switch (ifr->ifr_addr.sa_family) {
    377 #ifdef INET
    378 		case AF_INET:
    379 			src = (struct sockaddr *)
    380 				&(((struct in_aliasreq *)data)->ifra_addr);
    381 			dst = (struct sockaddr *)
    382 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
    383 
    384 			/* only one gif can have dst = INADDR_ANY */
    385 #define satosaddr(sa) (((struct sockaddr_in *)(sa))->sin_addr.s_addr)
    386 
    387 			if (satosaddr(dst) == INADDR_ANY) {
    388 				int i;
    389 				struct gif_softc *sc2;
    390 
    391 			  	for (i = 0, sc2 = gif; i < ngif; i++, sc2++) {
    392 					if (sc2 == sc) continue;
    393 					if (sc2->gif_pdst &&
    394 					    satosaddr(sc2->gif_pdst)
    395 						== INADDR_ANY) {
    396 					    error = EADDRNOTAVAIL;
    397 					    goto bad;
    398 					}
    399 				}
    400 			}
    401 			size = sizeof(struct sockaddr_in);
    402 			break;
    403 #endif /* INET */
    404 #ifdef INET6
    405 		case AF_INET6:
    406 			src = (struct sockaddr *)
    407 				&(((struct in6_aliasreq *)data)->ifra_addr);
    408 			dst = (struct sockaddr *)
    409 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    410 
    411 			/* only one gif can have dst = in6addr_any */
    412 #define satoin6(sa) (&((struct sockaddr_in6 *)(sa))->sin6_addr)
    413 
    414 			if (IN6_IS_ADDR_UNSPECIFIED(satoin6(dst))) {
    415 				int i;
    416 				struct gif_softc *sc2;
    417 
    418 			  	for (i = 0, sc2 = gif; i < ngif; i++, sc2++) {
    419 					if (sc2 == sc) continue;
    420 					if (sc2->gif_pdst &&
    421 					    IN6_IS_ADDR_UNSPECIFIED(
    422 						satoin6(sc2->gif_pdst)
    423 								    )) {
    424 					    error = EADDRNOTAVAIL;
    425 					    goto bad;
    426 					}
    427 				}
    428 			}
    429 			size = sizeof(struct sockaddr_in6);
    430 			break;
    431 #endif /* INET6 */
    432 		default:
    433 			error = EPROTOTYPE;
    434 			goto bad;
    435 			break;
    436 		}
    437 		if (sc->gif_psrc != NULL)
    438 			free((caddr_t)sc->gif_psrc, M_IFADDR);
    439 		if (sc->gif_pdst != NULL)
    440 			free((caddr_t)sc->gif_pdst, M_IFADDR);
    441 
    442 		sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK);
    443 		bzero((caddr_t)sa, size);
    444 		bcopy((caddr_t)src, (caddr_t)sa, size);
    445 		sc->gif_psrc = sa;
    446 
    447 		sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK);
    448 		bzero((caddr_t)sa, size);
    449 		bcopy((caddr_t)dst, (caddr_t)sa, size);
    450 		sc->gif_pdst = sa;
    451 
    452 		ifp->if_flags |= (IFF_UP|IFF_RUNNING);
    453 		if_up(ifp);		/* send up RTM_IFINFO */
    454 
    455 		break;
    456 
    457 	case SIOCGIFPSRCADDR:
    458 #ifdef INET6
    459 	case SIOCGIFPSRCADDR_IN6:
    460 #endif /* INET6 */
    461 		if (sc->gif_psrc == NULL) {
    462 			error = EADDRNOTAVAIL;
    463 			goto bad;
    464 		}
    465 		src = sc->gif_psrc;
    466 		switch (sc->gif_psrc->sa_family) {
    467 #ifdef INET
    468 		case AF_INET:
    469 			dst = &ifr->ifr_addr;
    470 			size = sizeof(struct sockaddr_in);
    471 			break;
    472 #endif /* INET */
    473 #ifdef INET6
    474 		case AF_INET6:
    475 			dst = (struct sockaddr *)
    476 				&(((struct in6_ifreq *)data)->ifr_addr);
    477 			size = sizeof(struct sockaddr_in6);
    478 			break;
    479 #endif /* INET6 */
    480 		default:
    481 			error = EADDRNOTAVAIL;
    482 			goto bad;
    483 		}
    484 		bcopy((caddr_t)src, (caddr_t)dst, size);
    485 		break;
    486 
    487 	case SIOCGIFPDSTADDR:
    488 #ifdef INET6
    489 	case SIOCGIFPDSTADDR_IN6:
    490 #endif /* INET6 */
    491 		if (sc->gif_pdst == NULL) {
    492 			error = EADDRNOTAVAIL;
    493 			goto bad;
    494 		}
    495 		src = sc->gif_pdst;
    496 		switch (sc->gif_pdst->sa_family) {
    497 #ifdef INET
    498 		case AF_INET:
    499 			dst = &ifr->ifr_addr;
    500 			size = sizeof(struct sockaddr_in);
    501 			break;
    502 #endif /* INET */
    503 #ifdef INET6
    504 		case AF_INET6:
    505 			dst = (struct sockaddr *)
    506 				&(((struct in6_ifreq *)data)->ifr_addr);
    507 			size = sizeof(struct sockaddr_in6);
    508 			break;
    509 #endif /* INET6 */
    510 		default:
    511 			error = EADDRNOTAVAIL;
    512 			goto bad;
    513 		}
    514 		bcopy((caddr_t)src, (caddr_t)dst, size);
    515 		break;
    516 
    517 	case SIOCSIFFLAGS:
    518 		break;
    519 
    520 	default:
    521 		error = EINVAL;
    522 		break;
    523 	}
    524  bad:
    525 	return error;
    526 }
    527 #endif /*NGIF > 0*/
    528