Home | History | Annotate | Line # | Download | only in net
if_faith.c revision 1.28
      1 /*	$NetBSD: if_faith.c,v 1.28 2004/08/19 20:58:23 christos 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.28 2004/08/19 20:58:23 christos 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 	snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
    129 	    ifc->ifc_name, unit);
    130 
    131 	sc->sc_if.if_mtu = FAITHMTU;
    132 	/* Change to BROADCAST experimentaly to announce its prefix. */
    133 	sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
    134 	sc->sc_if.if_ioctl = faithioctl;
    135 	sc->sc_if.if_output = faithoutput;
    136 	sc->sc_if.if_type = IFT_FAITH;
    137 	sc->sc_if.if_hdrlen = 0;
    138 	sc->sc_if.if_addrlen = 0;
    139 	sc->sc_if.if_dlt = DLT_NULL;
    140 	if_attach(&sc->sc_if);
    141 	if_alloc_sadl(&sc->sc_if);
    142 #if NBPFILTER > 0
    143 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
    144 #endif
    145 	LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
    146 	return (0);
    147 }
    148 
    149 void
    150 faith_clone_destroy(ifp)
    151 	struct ifnet *ifp;
    152 {
    153 	struct faith_softc *sc = (void *) ifp;
    154 
    155 	LIST_REMOVE(sc, sc_list);
    156 #if NBPFILTER > 0
    157 	bpfdetach(ifp);
    158 #endif
    159 	if_detach(ifp);
    160 	free(sc, M_DEVBUF);
    161 }
    162 
    163 int
    164 faithoutput(ifp, m, dst, rt)
    165 	struct ifnet *ifp;
    166 	struct mbuf *m;
    167 	struct sockaddr *dst;
    168 	struct rtentry *rt;
    169 {
    170 	int s, isr;
    171 	struct ifqueue *ifq = 0;
    172 
    173 	if ((m->m_flags & M_PKTHDR) == 0)
    174 		panic("faithoutput no HDR");
    175 #if NBPFILTER > 0
    176 	/* BPF write needs to be handled specially */
    177 	if (dst->sa_family == AF_UNSPEC) {
    178 		dst->sa_family = *(mtod(m, int *));
    179 		m->m_len -= sizeof(int);
    180 		m->m_pkthdr.len -= sizeof(int);
    181 		m->m_data += sizeof(int);
    182 	}
    183 
    184 	if (ifp->if_bpf)
    185 		bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
    186 
    187 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    188 		m_freem(m);
    189 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    190 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    191 	}
    192 	ifp->if_opackets++;
    193 	ifp->if_obytes += m->m_pkthdr.len;
    194 	switch (dst->sa_family) {
    195 #ifdef INET
    196 	case AF_INET:
    197 		ifq = &ipintrq;
    198 		isr = NETISR_IP;
    199 		break;
    200 #endif
    201 #ifdef INET6
    202 	case AF_INET6:
    203 		ifq = &ip6intrq;
    204 		isr = NETISR_IPV6;
    205 		break;
    206 #endif
    207 	default:
    208 		m_freem(m);
    209 		return EAFNOSUPPORT;
    210 	}
    211 
    212 	/* XXX do we need more sanity checks? */
    213 
    214 	m->m_pkthdr.rcvif = ifp;
    215 	s = splnet();
    216 	if (IF_QFULL(ifq)) {
    217 		IF_DROP(ifq);
    218 		m_freem(m);
    219 		splx(s);
    220 		return (ENOBUFS);
    221 	}
    222 	IF_ENQUEUE(ifq, m);
    223 	schednetisr(isr);
    224 	ifp->if_ipackets++;
    225 	ifp->if_ibytes += m->m_pkthdr.len;
    226 	splx(s);
    227 	return (0);
    228 }
    229 
    230 /* ARGSUSED */
    231 static void
    232 faithrtrequest(cmd, rt, info)
    233 	int cmd;
    234 	struct rtentry *rt;
    235 	struct rt_addrinfo *info;
    236 {
    237 	if (rt)
    238 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
    239 }
    240 
    241 /*
    242  * Process an ioctl request.
    243  */
    244 /* ARGSUSED */
    245 static int
    246 faithioctl(ifp, cmd, data)
    247 	struct ifnet *ifp;
    248 	u_long cmd;
    249 	caddr_t data;
    250 {
    251 	struct ifaddr *ifa;
    252 	struct ifreq *ifr = (struct ifreq *)data;
    253 	int error = 0;
    254 
    255 	switch (cmd) {
    256 
    257 	case SIOCSIFADDR:
    258 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
    259 		ifa = (struct ifaddr *)data;
    260 		ifa->ifa_rtrequest = faithrtrequest;
    261 		/*
    262 		 * Everything else is done at a higher level.
    263 		 */
    264 		break;
    265 
    266 	case SIOCADDMULTI:
    267 	case SIOCDELMULTI:
    268 		if (ifr == 0) {
    269 			error = EAFNOSUPPORT;		/* XXX */
    270 			break;
    271 		}
    272 		switch (ifr->ifr_addr.sa_family) {
    273 #ifdef INET
    274 		case AF_INET:
    275 			break;
    276 #endif
    277 #ifdef INET6
    278 		case AF_INET6:
    279 			break;
    280 #endif
    281 
    282 		default:
    283 			error = EAFNOSUPPORT;
    284 			break;
    285 		}
    286 		break;
    287 
    288 #ifdef SIOCSIFMTU
    289 	case SIOCSIFMTU:
    290 		ifp->if_mtu = ifr->ifr_mtu;
    291 		break;
    292 #endif
    293 
    294 	case SIOCSIFFLAGS:
    295 		break;
    296 
    297 	default:
    298 		error = EINVAL;
    299 	}
    300 	return (error);
    301 }
    302 
    303 /*
    304  * XXX could be slow
    305  * XXX could be layer violation to call sys/net from sys/netinet6
    306  */
    307 int
    308 faithprefix(in6)
    309 	struct in6_addr *in6;
    310 {
    311 	struct rtentry *rt;
    312 	struct sockaddr_in6 sin6;
    313 	int ret;
    314 
    315 	if (ip6_keepfaith == 0)
    316 		return 0;
    317 
    318 	memset(&sin6, 0, sizeof(sin6));
    319 	sin6.sin6_family = AF_INET6;
    320 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    321 	sin6.sin6_addr = *in6;
    322 	rt = rtalloc1((struct sockaddr *)&sin6, 0);
    323 	if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
    324 	    (rt->rt_ifp->if_flags & IFF_UP) != 0)
    325 		ret = 1;
    326 	else
    327 		ret = 0;
    328 	if (rt)
    329 		RTFREE(rt);
    330 	return ret;
    331 }
    332