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