Home | History | Annotate | Line # | Download | only in net
if_faith.c revision 1.26
      1 /*	$NetBSD: if_faith.c,v 1.26 2003/08/07 16:32:52 agc Exp $	*/
      2 /*	$KAME: if_faith.c,v 1.21 2001/02/20 07:59:26 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1982, 1986, 1993
      6  *	The Regents of the University of California.  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 University 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 REGENTS 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 REGENTS 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  * derived from
     34  *	@(#)if_loop.c	8.1 (Berkeley) 6/10/93
     35  * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
     36  */
     37 
     38 /*
     39  * Loopback interface driver for protocol testing and timing.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: if_faith.c,v 1.26 2003/08/07 16:32:52 agc Exp $");
     44 
     45 #include "opt_inet.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/socket.h>
     52 #include <sys/errno.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/time.h>
     55 #include <sys/queue.h>
     56 
     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 #include <net/if_faith.h>
     65 
     66 #ifdef	INET
     67 #include <netinet/in.h>
     68 #include <netinet/in_systm.h>
     69 #include <netinet/in_var.h>
     70 #include <netinet/ip.h>
     71 #endif
     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 #endif
     81 
     82 #include "bpfilter.h"
     83 
     84 #include <net/net_osdep.h>
     85 
     86 struct faith_softc {
     87 	struct ifnet sc_if;	/* must be first */
     88 	LIST_ENTRY(faith_softc) sc_list;
     89 };
     90 
     91 static int faithioctl __P((struct ifnet *, u_long, caddr_t));
     92 int faithoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
     93 	struct rtentry *));
     94 static void faithrtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
     95 
     96 void faithattach __P((int));
     97 
     98 LIST_HEAD(, faith_softc) faith_softc_list;
     99 
    100 int	faith_clone_create __P((struct if_clone *, int));
    101 void	faith_clone_destroy __P((struct ifnet *));
    102 
    103 struct if_clone faith_cloner =
    104     IF_CLONE_INITIALIZER("faith", faith_clone_create, faith_clone_destroy);
    105 
    106 #define	FAITHMTU	1500
    107 
    108 /* ARGSUSED */
    109 void
    110 faithattach(count)
    111 	int count;
    112 {
    113 
    114 	LIST_INIT(&faith_softc_list);
    115 	if_clone_attach(&faith_cloner);
    116 }
    117 
    118 int
    119 faith_clone_create(ifc, unit)
    120 	struct if_clone *ifc;
    121 	int unit;
    122 {
    123 	struct faith_softc *sc;
    124 
    125 	sc = malloc(sizeof(struct faith_softc), M_DEVBUF, M_WAITOK);
    126 	memset(sc, 0, sizeof(struct faith_softc));
    127 
    128 	sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
    129 
    130 	sc->sc_if.if_mtu = FAITHMTU;
    131 	/* Change to BROADCAST experimentaly to announce its prefix. */
    132 	sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
    133 	sc->sc_if.if_ioctl = faithioctl;
    134 	sc->sc_if.if_output = faithoutput;
    135 	sc->sc_if.if_type = IFT_FAITH;
    136 	sc->sc_if.if_hdrlen = 0;
    137 	sc->sc_if.if_addrlen = 0;
    138 	sc->sc_if.if_dlt = DLT_NULL;
    139 	if_attach(&sc->sc_if);
    140 	if_alloc_sadl(&sc->sc_if);
    141 #if NBPFILTER > 0
    142 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
    143 #endif
    144 	LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
    145 	return (0);
    146 }
    147 
    148 void
    149 faith_clone_destroy(ifp)
    150 	struct ifnet *ifp;
    151 {
    152 	struct faith_softc *sc = (void *) ifp;
    153 
    154 	LIST_REMOVE(sc, sc_list);
    155 #if NBPFILTER > 0
    156 	bpfdetach(ifp);
    157 #endif
    158 	if_detach(ifp);
    159 	free(sc, M_DEVBUF);
    160 }
    161 
    162 int
    163 faithoutput(ifp, m, dst, rt)
    164 	struct ifnet *ifp;
    165 	struct mbuf *m;
    166 	struct sockaddr *dst;
    167 	struct rtentry *rt;
    168 {
    169 	int s, isr;
    170 	struct ifqueue *ifq = 0;
    171 
    172 	if ((m->m_flags & M_PKTHDR) == 0)
    173 		panic("faithoutput no HDR");
    174 #if NBPFILTER > 0
    175 	/* BPF write needs to be handled specially */
    176 	if (dst->sa_family == AF_UNSPEC) {
    177 		dst->sa_family = *(mtod(m, int *));
    178 		m->m_len -= sizeof(int);
    179 		m->m_pkthdr.len -= sizeof(int);
    180 		m->m_data += sizeof(int);
    181 	}
    182 
    183 	if (ifp->if_bpf) {
    184 		/*
    185 		 * We need to prepend the address family as
    186 		 * a four byte field.  Cons up a faith header
    187 		 * to pacify bpf.  This is safe because bpf
    188 		 * will only read from the mbuf (i.e., it won't
    189 		 * try to free it or keep a pointer a to it).
    190 		 */
    191 		struct mbuf m0;
    192 		u_int32_t af = dst->sa_family;
    193 
    194 		m0.m_next = m;
    195 		m0.m_len = 4;
    196 		m0.m_data = (char *)&af;
    197 
    198 #ifdef HAVE_OLD_BPF
    199 		bpf_mtap(ifp, &m0);
    200 #else
    201 		bpf_mtap(ifp->if_bpf, &m0);
    202 #endif
    203 	}
    204 #endif
    205 
    206 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    207 		m_freem(m);
    208 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    209 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    210 	}
    211 	ifp->if_opackets++;
    212 	ifp->if_obytes += m->m_pkthdr.len;
    213 	switch (dst->sa_family) {
    214 #ifdef INET
    215 	case AF_INET:
    216 		ifq = &ipintrq;
    217 		isr = NETISR_IP;
    218 		break;
    219 #endif
    220 #ifdef INET6
    221 	case AF_INET6:
    222 		ifq = &ip6intrq;
    223 		isr = NETISR_IPV6;
    224 		break;
    225 #endif
    226 	default:
    227 		m_freem(m);
    228 		return EAFNOSUPPORT;
    229 	}
    230 
    231 	/* XXX do we need more sanity checks? */
    232 
    233 	m->m_pkthdr.rcvif = ifp;
    234 	s = splnet();
    235 	if (IF_QFULL(ifq)) {
    236 		IF_DROP(ifq);
    237 		m_freem(m);
    238 		splx(s);
    239 		return (ENOBUFS);
    240 	}
    241 	IF_ENQUEUE(ifq, m);
    242 	schednetisr(isr);
    243 	ifp->if_ipackets++;
    244 	ifp->if_ibytes += m->m_pkthdr.len;
    245 	splx(s);
    246 	return (0);
    247 }
    248 
    249 /* ARGSUSED */
    250 static void
    251 faithrtrequest(cmd, rt, info)
    252 	int cmd;
    253 	struct rtentry *rt;
    254 	struct rt_addrinfo *info;
    255 {
    256 	if (rt)
    257 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
    258 }
    259 
    260 /*
    261  * Process an ioctl request.
    262  */
    263 /* ARGSUSED */
    264 static int
    265 faithioctl(ifp, cmd, data)
    266 	struct ifnet *ifp;
    267 	u_long cmd;
    268 	caddr_t data;
    269 {
    270 	struct ifaddr *ifa;
    271 	struct ifreq *ifr = (struct ifreq *)data;
    272 	int error = 0;
    273 
    274 	switch (cmd) {
    275 
    276 	case SIOCSIFADDR:
    277 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
    278 		ifa = (struct ifaddr *)data;
    279 		ifa->ifa_rtrequest = faithrtrequest;
    280 		/*
    281 		 * Everything else is done at a higher level.
    282 		 */
    283 		break;
    284 
    285 	case SIOCADDMULTI:
    286 	case SIOCDELMULTI:
    287 		if (ifr == 0) {
    288 			error = EAFNOSUPPORT;		/* XXX */
    289 			break;
    290 		}
    291 		switch (ifr->ifr_addr.sa_family) {
    292 #ifdef INET
    293 		case AF_INET:
    294 			break;
    295 #endif
    296 #ifdef INET6
    297 		case AF_INET6:
    298 			break;
    299 #endif
    300 
    301 		default:
    302 			error = EAFNOSUPPORT;
    303 			break;
    304 		}
    305 		break;
    306 
    307 #ifdef SIOCSIFMTU
    308 	case SIOCSIFMTU:
    309 		ifp->if_mtu = ifr->ifr_mtu;
    310 		break;
    311 #endif
    312 
    313 	case SIOCSIFFLAGS:
    314 		break;
    315 
    316 	default:
    317 		error = EINVAL;
    318 	}
    319 	return (error);
    320 }
    321 
    322 /*
    323  * XXX could be slow
    324  * XXX could be layer violation to call sys/net from sys/netinet6
    325  */
    326 int
    327 faithprefix(in6)
    328 	struct in6_addr *in6;
    329 {
    330 	struct rtentry *rt;
    331 	struct sockaddr_in6 sin6;
    332 	int ret;
    333 
    334 	if (ip6_keepfaith == 0)
    335 		return 0;
    336 
    337 	memset(&sin6, 0, sizeof(sin6));
    338 	sin6.sin6_family = AF_INET6;
    339 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    340 	sin6.sin6_addr = *in6;
    341 	rt = rtalloc1((struct sockaddr *)&sin6, 0);
    342 	if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
    343 	    (rt->rt_ifp->if_flags & IFF_UP) != 0)
    344 		ret = 1;
    345 	else
    346 		ret = 0;
    347 	if (rt)
    348 		RTFREE(rt);
    349 	return ret;
    350 }
    351