Home | History | Annotate | Line # | Download | only in net
if_faith.c revision 1.41.10.2
      1  1.41.10.2      yamt /*	$NetBSD: if_faith.c,v 1.41.10.2 2010/03/11 15:04:26 yamt Exp $	*/
      2       1.19    itojun /*	$KAME: if_faith.c,v 1.21 2001/02/20 07:59:26 itojun Exp $	*/
      3        1.6    itojun 
      4        1.2    itojun /*
      5        1.2    itojun  * Copyright (c) 1982, 1986, 1993
      6        1.2    itojun  *	The Regents of the University of California.  All rights reserved.
      7        1.2    itojun  *
      8        1.2    itojun  * Redistribution and use in source and binary forms, with or without
      9        1.2    itojun  * modification, are permitted provided that the following conditions
     10        1.2    itojun  * are met:
     11        1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12        1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13        1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     15        1.2    itojun  *    documentation and/or other materials provided with the distribution.
     16       1.26       agc  * 3. Neither the name of the University nor the names of its contributors
     17        1.2    itojun  *    may be used to endorse or promote products derived from this software
     18        1.2    itojun  *    without specific prior written permission.
     19        1.2    itojun  *
     20        1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21        1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22        1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23        1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24        1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25        1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26        1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27        1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28        1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29        1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30        1.2    itojun  * SUCH DAMAGE.
     31        1.2    itojun  */
     32        1.2    itojun /*
     33       1.19    itojun  * derived from
     34        1.2    itojun  *	@(#)if_loop.c	8.1 (Berkeley) 6/10/93
     35        1.2    itojun  * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
     36        1.2    itojun  */
     37        1.2    itojun 
     38        1.2    itojun /*
     39       1.34    rpaulo  * IPv6-to-IPv4 TCP relay capturing interface
     40        1.2    itojun  */
     41       1.22     lukem 
     42       1.22     lukem #include <sys/cdefs.h>
     43  1.41.10.2      yamt __KERNEL_RCSID(0, "$NetBSD: if_faith.c,v 1.41.10.2 2010/03/11 15:04:26 yamt Exp $");
     44       1.22     lukem 
     45        1.2    itojun #include "opt_inet.h"
     46        1.2    itojun 
     47       1.24   thorpej #include <sys/param.h>
     48        1.2    itojun #include <sys/systm.h>
     49        1.2    itojun #include <sys/kernel.h>
     50        1.2    itojun #include <sys/mbuf.h>
     51        1.2    itojun #include <sys/socket.h>
     52        1.2    itojun #include <sys/errno.h>
     53        1.2    itojun #include <sys/ioctl.h>
     54        1.2    itojun #include <sys/time.h>
     55       1.12   thorpej #include <sys/queue.h>
     56       1.12   thorpej 
     57       1.40        ad #include <sys/cpu.h>
     58        1.2    itojun 
     59        1.2    itojun #include <net/if.h>
     60        1.2    itojun #include <net/if_types.h>
     61        1.2    itojun #include <net/netisr.h>
     62        1.2    itojun #include <net/route.h>
     63        1.2    itojun #include <net/bpf.h>
     64       1.19    itojun #include <net/if_faith.h>
     65        1.2    itojun 
     66        1.2    itojun #ifdef	INET
     67        1.2    itojun #include <netinet/in.h>
     68        1.2    itojun #include <netinet/in_systm.h>
     69        1.2    itojun #include <netinet/in_var.h>
     70        1.2    itojun #include <netinet/ip.h>
     71        1.2    itojun #endif
     72        1.2    itojun 
     73        1.2    itojun #ifdef INET6
     74        1.2    itojun #ifndef INET
     75        1.2    itojun #include <netinet/in.h>
     76        1.2    itojun #endif
     77        1.2    itojun #include <netinet6/in6_var.h>
     78       1.10    itojun #include <netinet/ip6.h>
     79       1.19    itojun #include <netinet6/ip6_var.h>
     80        1.2    itojun #endif
     81        1.2    itojun 
     82        1.2    itojun 
     83        1.7    itojun #include <net/net_osdep.h>
     84        1.7    itojun 
     85       1.39  christos static int	faithioctl(struct ifnet *, u_long, void *);
     86       1.38    dyoung static int	faithoutput(struct ifnet *, struct mbuf *,
     87       1.38    dyoung 		            const struct sockaddr *, struct rtentry *);
     88  1.41.10.1      yamt static void	faithrtrequest(int, struct rtentry *,
     89  1.41.10.1      yamt 		               const struct rt_addrinfo *);
     90        1.2    itojun 
     91       1.32   thorpej void	faithattach(int);
     92        1.2    itojun 
     93       1.32   thorpej static int	faith_clone_create(struct if_clone *, int);
     94       1.32   thorpej static int	faith_clone_destroy(struct ifnet *);
     95       1.12   thorpej 
     96       1.32   thorpej static struct if_clone faith_cloner =
     97       1.12   thorpej     IF_CLONE_INITIALIZER("faith", faith_clone_create, faith_clone_destroy);
     98        1.2    itojun 
     99        1.2    itojun #define	FAITHMTU	1500
    100        1.2    itojun 
    101        1.2    itojun /* ARGSUSED */
    102        1.2    itojun void
    103       1.37  christos faithattach(int count)
    104        1.2    itojun {
    105        1.2    itojun 
    106       1.12   thorpej 	if_clone_attach(&faith_cloner);
    107       1.12   thorpej }
    108       1.12   thorpej 
    109       1.32   thorpej static int
    110       1.32   thorpej faith_clone_create(struct if_clone *ifc, int unit)
    111       1.12   thorpej {
    112       1.35     peter 	struct ifnet *ifp;
    113       1.12   thorpej 
    114  1.41.10.1      yamt 	ifp = if_alloc(IFT_FAITH);
    115       1.12   thorpej 
    116  1.41.10.1      yamt 	if_initname(ifp, ifc->ifc_name, unit);
    117       1.12   thorpej 
    118       1.35     peter 	ifp->if_mtu = FAITHMTU;
    119       1.12   thorpej 	/* Change to BROADCAST experimentaly to announce its prefix. */
    120       1.35     peter 	ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
    121       1.35     peter 	ifp->if_ioctl = faithioctl;
    122       1.35     peter 	ifp->if_output = faithoutput;
    123       1.35     peter 	ifp->if_type = IFT_FAITH;
    124       1.35     peter 	ifp->if_hdrlen = 0;
    125       1.35     peter 	ifp->if_addrlen = 0;
    126       1.35     peter 	ifp->if_dlt = DLT_NULL;
    127       1.35     peter 	if_attach(ifp);
    128       1.35     peter 	if_alloc_sadl(ifp);
    129  1.41.10.2      yamt 	bpf_ops->bpf_attach(ifp, DLT_NULL, sizeof(u_int), &ifp->if_bpf);
    130       1.12   thorpej 	return (0);
    131       1.12   thorpej }
    132       1.12   thorpej 
    133       1.30     peter int
    134       1.32   thorpej faith_clone_destroy(struct ifnet *ifp)
    135       1.12   thorpej {
    136       1.12   thorpej 
    137  1.41.10.2      yamt 	bpf_ops->bpf_detach(ifp);
    138       1.12   thorpej 	if_detach(ifp);
    139       1.35     peter 	free(ifp, M_DEVBUF);
    140       1.30     peter 
    141       1.30     peter 	return (0);
    142        1.2    itojun }
    143        1.2    itojun 
    144       1.38    dyoung static int
    145       1.38    dyoung faithoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    146       1.32   thorpej     struct rtentry *rt)
    147        1.2    itojun {
    148        1.2    itojun 	int s, isr;
    149       1.38    dyoung 	uint32_t af;
    150       1.11  augustss 	struct ifqueue *ifq = 0;
    151        1.2    itojun 
    152        1.2    itojun 	if ((m->m_flags & M_PKTHDR) == 0)
    153        1.2    itojun 		panic("faithoutput no HDR");
    154       1.38    dyoung 	af = dst->sa_family;
    155        1.2    itojun 	/* BPF write needs to be handled specially */
    156       1.38    dyoung 	if (af == AF_UNSPEC) {
    157       1.38    dyoung 		af = *(mtod(m, int *));
    158       1.38    dyoung 		m_adj(m, sizeof(int));
    159        1.2    itojun 	}
    160        1.2    itojun 
    161       1.28  christos 	if (ifp->if_bpf)
    162  1.41.10.2      yamt 		bpf_ops->bpf_mtap_af(ifp->if_bpf, af, m);
    163        1.2    itojun 
    164        1.2    itojun 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    165        1.2    itojun 		m_freem(m);
    166        1.2    itojun 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    167        1.2    itojun 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    168        1.2    itojun 	}
    169        1.2    itojun 	ifp->if_opackets++;
    170        1.2    itojun 	ifp->if_obytes += m->m_pkthdr.len;
    171       1.38    dyoung 	switch (af) {
    172        1.7    itojun #ifdef INET
    173        1.7    itojun 	case AF_INET:
    174        1.7    itojun 		ifq = &ipintrq;
    175        1.7    itojun 		isr = NETISR_IP;
    176        1.7    itojun 		break;
    177        1.7    itojun #endif
    178        1.2    itojun #ifdef INET6
    179        1.2    itojun 	case AF_INET6:
    180        1.2    itojun 		ifq = &ip6intrq;
    181        1.2    itojun 		isr = NETISR_IPV6;
    182        1.2    itojun 		break;
    183        1.2    itojun #endif
    184        1.2    itojun 	default:
    185        1.2    itojun 		m_freem(m);
    186        1.2    itojun 		return EAFNOSUPPORT;
    187        1.2    itojun 	}
    188        1.2    itojun 
    189        1.2    itojun 	/* XXX do we need more sanity checks? */
    190        1.2    itojun 
    191        1.2    itojun 	m->m_pkthdr.rcvif = ifp;
    192       1.18   thorpej 	s = splnet();
    193        1.2    itojun 	if (IF_QFULL(ifq)) {
    194        1.2    itojun 		IF_DROP(ifq);
    195        1.2    itojun 		m_freem(m);
    196        1.2    itojun 		splx(s);
    197        1.2    itojun 		return (ENOBUFS);
    198        1.2    itojun 	}
    199        1.2    itojun 	IF_ENQUEUE(ifq, m);
    200        1.2    itojun 	schednetisr(isr);
    201        1.2    itojun 	ifp->if_ipackets++;
    202        1.2    itojun 	ifp->if_ibytes += m->m_pkthdr.len;
    203        1.2    itojun 	splx(s);
    204        1.2    itojun 	return (0);
    205        1.2    itojun }
    206        1.2    itojun 
    207        1.2    itojun /* ARGSUSED */
    208        1.2    itojun static void
    209       1.37  christos faithrtrequest(int cmd, struct rtentry *rt,
    210  1.41.10.1      yamt     const struct rt_addrinfo *info)
    211        1.2    itojun {
    212       1.25    itojun 	if (rt)
    213        1.2    itojun 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
    214        1.2    itojun }
    215        1.2    itojun 
    216        1.2    itojun /*
    217        1.2    itojun  * Process an ioctl request.
    218        1.2    itojun  */
    219        1.2    itojun /* ARGSUSED */
    220        1.2    itojun static int
    221       1.39  christos faithioctl(struct ifnet *ifp, u_long cmd, void *data)
    222        1.2    itojun {
    223       1.11  augustss 	struct ifaddr *ifa;
    224       1.11  augustss 	struct ifreq *ifr = (struct ifreq *)data;
    225       1.11  augustss 	int error = 0;
    226        1.2    itojun 
    227        1.2    itojun 	switch (cmd) {
    228        1.2    itojun 
    229  1.41.10.1      yamt 	case SIOCINITIFADDR:
    230        1.2    itojun 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
    231        1.2    itojun 		ifa = (struct ifaddr *)data;
    232        1.2    itojun 		ifa->ifa_rtrequest = faithrtrequest;
    233        1.2    itojun 		/*
    234        1.2    itojun 		 * Everything else is done at a higher level.
    235        1.2    itojun 		 */
    236        1.2    itojun 		break;
    237        1.2    itojun 
    238        1.2    itojun 	case SIOCADDMULTI:
    239        1.2    itojun 	case SIOCDELMULTI:
    240        1.2    itojun 		if (ifr == 0) {
    241        1.2    itojun 			error = EAFNOSUPPORT;		/* XXX */
    242        1.2    itojun 			break;
    243        1.2    itojun 		}
    244        1.2    itojun 		switch (ifr->ifr_addr.sa_family) {
    245        1.7    itojun #ifdef INET
    246        1.7    itojun 		case AF_INET:
    247        1.7    itojun 			break;
    248        1.7    itojun #endif
    249        1.2    itojun #ifdef INET6
    250        1.2    itojun 		case AF_INET6:
    251        1.2    itojun 			break;
    252        1.2    itojun #endif
    253        1.2    itojun 
    254        1.2    itojun 		default:
    255        1.2    itojun 			error = EAFNOSUPPORT;
    256        1.2    itojun 			break;
    257        1.2    itojun 		}
    258        1.2    itojun 		break;
    259        1.2    itojun 
    260  1.41.10.1      yamt 	default:
    261       1.41    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    262       1.41    dyoung 			error = 0;
    263        1.2    itojun 		break;
    264        1.2    itojun 	}
    265        1.2    itojun 	return (error);
    266       1.19    itojun }
    267       1.19    itojun 
    268       1.33  christos #ifdef INET6
    269       1.19    itojun /*
    270       1.19    itojun  * XXX could be slow
    271       1.19    itojun  * XXX could be layer violation to call sys/net from sys/netinet6
    272       1.19    itojun  */
    273       1.19    itojun int
    274       1.32   thorpej faithprefix(struct in6_addr *in6)
    275       1.19    itojun {
    276       1.19    itojun 	struct rtentry *rt;
    277       1.19    itojun 	struct sockaddr_in6 sin6;
    278       1.19    itojun 	int ret;
    279       1.19    itojun 
    280       1.19    itojun 	if (ip6_keepfaith == 0)
    281       1.19    itojun 		return 0;
    282       1.19    itojun 
    283       1.21   thorpej 	memset(&sin6, 0, sizeof(sin6));
    284       1.19    itojun 	sin6.sin6_family = AF_INET6;
    285       1.19    itojun 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    286       1.19    itojun 	sin6.sin6_addr = *in6;
    287       1.19    itojun 	rt = rtalloc1((struct sockaddr *)&sin6, 0);
    288       1.19    itojun 	if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
    289       1.19    itojun 	    (rt->rt_ifp->if_flags & IFF_UP) != 0)
    290       1.19    itojun 		ret = 1;
    291       1.19    itojun 	else
    292       1.19    itojun 		ret = 0;
    293       1.19    itojun 	if (rt)
    294       1.19    itojun 		RTFREE(rt);
    295       1.19    itojun 	return ret;
    296        1.2    itojun }
    297       1.33  christos #endif
    298