Home | History | Annotate | Line # | Download | only in net
if_faith.c revision 1.61.10.1
      1  1.61.10.1   thorpej /*	$NetBSD: if_faith.c,v 1.61.10.1 2021/06/17 04:46:35 thorpej 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.61.10.1   thorpej __KERNEL_RCSID(0, "$NetBSD: if_faith.c,v 1.61.10.1 2021/06/17 04:46:35 thorpej Exp $");
     44       1.22     lukem 
     45       1.54  christos #ifdef _KERNEL_OPT
     46        1.2    itojun #include "opt_inet.h"
     47       1.54  christos #endif
     48        1.2    itojun 
     49       1.24   thorpej #include <sys/param.h>
     50        1.2    itojun #include <sys/systm.h>
     51        1.2    itojun #include <sys/kernel.h>
     52        1.2    itojun #include <sys/mbuf.h>
     53        1.2    itojun #include <sys/socket.h>
     54        1.2    itojun #include <sys/errno.h>
     55        1.2    itojun #include <sys/ioctl.h>
     56        1.2    itojun #include <sys/time.h>
     57       1.12   thorpej #include <sys/queue.h>
     58       1.54  christos #include <sys/device.h>
     59       1.54  christos #include <sys/module.h>
     60       1.54  christos #include <sys/atomic.h>
     61       1.12   thorpej 
     62       1.40        ad #include <sys/cpu.h>
     63        1.2    itojun 
     64        1.2    itojun #include <net/if.h>
     65        1.2    itojun #include <net/if_types.h>
     66        1.2    itojun #include <net/netisr.h>
     67        1.2    itojun #include <net/route.h>
     68        1.2    itojun #include <net/bpf.h>
     69       1.19    itojun #include <net/if_faith.h>
     70        1.2    itojun 
     71        1.2    itojun #ifdef	INET
     72        1.2    itojun #include <netinet/in.h>
     73        1.2    itojun #include <netinet/in_systm.h>
     74        1.2    itojun #include <netinet/in_var.h>
     75        1.2    itojun #include <netinet/ip.h>
     76        1.2    itojun #endif
     77        1.2    itojun 
     78        1.2    itojun #ifdef INET6
     79        1.2    itojun #ifndef INET
     80        1.2    itojun #include <netinet/in.h>
     81        1.2    itojun #endif
     82        1.2    itojun #include <netinet6/in6_var.h>
     83       1.10    itojun #include <netinet/ip6.h>
     84       1.19    itojun #include <netinet6/ip6_var.h>
     85        1.2    itojun #endif
     86        1.2    itojun 
     87       1.51  christos #include "ioconf.h"
     88       1.51  christos 
     89       1.39  christos static int	faithioctl(struct ifnet *, u_long, void *);
     90       1.38    dyoung static int	faithoutput(struct ifnet *, struct mbuf *,
     91       1.52     ozaki 		            const struct sockaddr *, const struct rtentry *);
     92       1.44    dyoung static void	faithrtrequest(int, struct rtentry *,
     93       1.44    dyoung 		               const struct rt_addrinfo *);
     94        1.2    itojun 
     95       1.32   thorpej static int	faith_clone_create(struct if_clone *, int);
     96       1.32   thorpej static int	faith_clone_destroy(struct ifnet *);
     97       1.12   thorpej 
     98       1.32   thorpej static struct if_clone faith_cloner =
     99       1.12   thorpej     IF_CLONE_INITIALIZER("faith", faith_clone_create, faith_clone_destroy);
    100        1.2    itojun 
    101        1.2    itojun #define	FAITHMTU	1500
    102        1.2    itojun 
    103       1.54  christos static u_int faith_count;
    104       1.54  christos 
    105        1.2    itojun /* ARGSUSED */
    106        1.2    itojun void
    107       1.37  christos faithattach(int count)
    108        1.2    itojun {
    109        1.2    itojun 
    110       1.54  christos 	/*
    111       1.54  christos 	 * Nothing to do here, initialization is handled by the
    112       1.54  christos 	 * module initialization code in faithinit() below).
    113       1.54  christos 	 */
    114       1.54  christos }
    115       1.54  christos 
    116       1.54  christos static void
    117       1.54  christos faithinit(void)
    118       1.54  christos {
    119       1.12   thorpej 	if_clone_attach(&faith_cloner);
    120       1.12   thorpej }
    121       1.12   thorpej 
    122       1.32   thorpej static int
    123       1.54  christos faithdetach(void)
    124       1.54  christos {
    125       1.54  christos 	int error = 0;
    126       1.54  christos 
    127       1.54  christos 	if (faith_count != 0)
    128       1.54  christos 		error = EBUSY;
    129       1.54  christos 
    130       1.54  christos 	if (error == 0)
    131       1.54  christos 		if_clone_detach(&faith_cloner);
    132       1.54  christos 
    133       1.54  christos 	return error;
    134       1.54  christos }
    135       1.54  christos 
    136       1.54  christos static int
    137       1.32   thorpej faith_clone_create(struct if_clone *ifc, int unit)
    138       1.12   thorpej {
    139       1.35     peter 	struct ifnet *ifp;
    140       1.12   thorpej 
    141       1.43  christos 	ifp = if_alloc(IFT_FAITH);
    142       1.12   thorpej 
    143       1.42  christos 	if_initname(ifp, ifc->ifc_name, unit);
    144       1.12   thorpej 
    145       1.35     peter 	ifp->if_mtu = FAITHMTU;
    146       1.12   thorpej 	/* Change to BROADCAST experimentaly to announce its prefix. */
    147       1.35     peter 	ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
    148       1.35     peter 	ifp->if_ioctl = faithioctl;
    149       1.35     peter 	ifp->if_output = faithoutput;
    150       1.35     peter 	ifp->if_type = IFT_FAITH;
    151       1.35     peter 	ifp->if_hdrlen = 0;
    152       1.35     peter 	ifp->if_addrlen = 0;
    153       1.35     peter 	ifp->if_dlt = DLT_NULL;
    154  1.61.10.1   thorpej 	if_attach(ifp);
    155       1.35     peter 	if_alloc_sadl(ifp);
    156       1.47     joerg 	bpf_attach(ifp, DLT_NULL, sizeof(u_int));
    157       1.54  christos 	atomic_inc_uint(&faith_count);
    158       1.12   thorpej 	return (0);
    159       1.12   thorpej }
    160       1.12   thorpej 
    161       1.30     peter int
    162       1.32   thorpej faith_clone_destroy(struct ifnet *ifp)
    163       1.12   thorpej {
    164       1.12   thorpej 
    165       1.47     joerg 	bpf_detach(ifp);
    166       1.12   thorpej 	if_detach(ifp);
    167       1.50     ozaki 	if_free(ifp);
    168       1.30     peter 
    169       1.54  christos 	atomic_dec_uint(&faith_count);
    170       1.30     peter 	return (0);
    171        1.2    itojun }
    172        1.2    itojun 
    173       1.38    dyoung static int
    174       1.38    dyoung faithoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    175       1.52     ozaki     const struct rtentry *rt)
    176        1.2    itojun {
    177       1.48     rmind 	pktqueue_t *pktq;
    178       1.48     rmind 	size_t pktlen;
    179       1.48     rmind 	int s, error;
    180       1.38    dyoung 	uint32_t af;
    181        1.2    itojun 
    182        1.2    itojun 	if ((m->m_flags & M_PKTHDR) == 0)
    183        1.2    itojun 		panic("faithoutput no HDR");
    184       1.38    dyoung 	af = dst->sa_family;
    185        1.2    itojun 	/* BPF write needs to be handled specially */
    186       1.38    dyoung 	if (af == AF_UNSPEC) {
    187       1.38    dyoung 		af = *(mtod(m, int *));
    188       1.38    dyoung 		m_adj(m, sizeof(int));
    189        1.2    itojun 	}
    190        1.2    itojun 
    191       1.59   msaitoh 	bpf_mtap_af(ifp, af, m, BPF_D_OUT);
    192        1.2    itojun 
    193        1.2    itojun 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    194        1.2    itojun 		m_freem(m);
    195        1.2    itojun 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    196        1.2    itojun 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    197        1.2    itojun 	}
    198       1.48     rmind 	pktlen = m->m_pkthdr.len;
    199       1.61   thorpej 	if_statadd2(ifp, if_opackets, 1, if_obytes, pktlen);
    200       1.38    dyoung 	switch (af) {
    201        1.7    itojun #ifdef INET
    202        1.7    itojun 	case AF_INET:
    203       1.48     rmind 		pktq = ip_pktq;
    204        1.7    itojun 		break;
    205        1.7    itojun #endif
    206        1.2    itojun #ifdef INET6
    207        1.2    itojun 	case AF_INET6:
    208       1.48     rmind 		pktq = ip6_pktq;
    209        1.2    itojun 		break;
    210        1.2    itojun #endif
    211        1.2    itojun 	default:
    212        1.2    itojun 		m_freem(m);
    213        1.2    itojun 		return EAFNOSUPPORT;
    214        1.2    itojun 	}
    215        1.2    itojun 
    216        1.2    itojun 	/* XXX do we need more sanity checks? */
    217       1.48     rmind 	KASSERT(pktq != NULL);
    218       1.53     ozaki 	m_set_rcvif(m, ifp);
    219        1.2    itojun 
    220       1.18   thorpej 	s = splnet();
    221       1.48     rmind 	if (__predict_true(pktq_enqueue(pktq, m, 0))) {
    222       1.61   thorpej 		if_statadd2(ifp, if_ipackets, 1, if_ibytes, pktlen);
    223       1.48     rmind 		error = 0;
    224       1.48     rmind 	} else {
    225        1.2    itojun 		m_freem(m);
    226       1.48     rmind 		error = ENOBUFS;
    227        1.2    itojun 	}
    228        1.2    itojun 	splx(s);
    229       1.48     rmind 
    230       1.48     rmind 	return error;
    231        1.2    itojun }
    232        1.2    itojun 
    233        1.2    itojun /* ARGSUSED */
    234        1.2    itojun static void
    235       1.37  christos faithrtrequest(int cmd, struct rtentry *rt,
    236       1.44    dyoung     const struct rt_addrinfo *info)
    237        1.2    itojun {
    238       1.25    itojun 	if (rt)
    239        1.2    itojun 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
    240        1.2    itojun }
    241        1.2    itojun 
    242        1.2    itojun /*
    243        1.2    itojun  * Process an ioctl request.
    244        1.2    itojun  */
    245        1.2    itojun /* ARGSUSED */
    246        1.2    itojun static int
    247       1.39  christos faithioctl(struct ifnet *ifp, u_long cmd, void *data)
    248        1.2    itojun {
    249       1.11  augustss 	struct ifaddr *ifa;
    250       1.11  augustss 	struct ifreq *ifr = (struct ifreq *)data;
    251       1.11  augustss 	int error = 0;
    252        1.2    itojun 
    253        1.2    itojun 	switch (cmd) {
    254        1.2    itojun 
    255       1.45    dyoung 	case SIOCINITIFADDR:
    256        1.2    itojun 		ifa = (struct ifaddr *)data;
    257        1.2    itojun 		ifa->ifa_rtrequest = faithrtrequest;
    258       1.57     ozaki 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
    259        1.2    itojun 		/*
    260        1.2    itojun 		 * Everything else is done at a higher level.
    261        1.2    itojun 		 */
    262        1.2    itojun 		break;
    263        1.2    itojun 
    264        1.2    itojun 	case SIOCADDMULTI:
    265        1.2    itojun 	case SIOCDELMULTI:
    266        1.2    itojun 		if (ifr == 0) {
    267        1.2    itojun 			error = EAFNOSUPPORT;		/* XXX */
    268        1.2    itojun 			break;
    269        1.2    itojun 		}
    270        1.2    itojun 		switch (ifr->ifr_addr.sa_family) {
    271        1.7    itojun #ifdef INET
    272        1.7    itojun 		case AF_INET:
    273        1.7    itojun 			break;
    274        1.7    itojun #endif
    275        1.2    itojun #ifdef INET6
    276        1.2    itojun 		case AF_INET6:
    277        1.2    itojun 			break;
    278        1.2    itojun #endif
    279        1.2    itojun 
    280        1.2    itojun 		default:
    281        1.2    itojun 			error = EAFNOSUPPORT;
    282        1.2    itojun 			break;
    283        1.2    itojun 		}
    284        1.2    itojun 		break;
    285        1.2    itojun 
    286       1.45    dyoung 	default:
    287       1.41    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    288       1.41    dyoung 			error = 0;
    289        1.2    itojun 		break;
    290        1.2    itojun 	}
    291        1.2    itojun 	return (error);
    292       1.19    itojun }
    293       1.19    itojun 
    294       1.33  christos #ifdef INET6
    295       1.19    itojun /*
    296       1.19    itojun  * XXX could be slow
    297       1.19    itojun  * XXX could be layer violation to call sys/net from sys/netinet6
    298       1.19    itojun  */
    299       1.19    itojun int
    300       1.32   thorpej faithprefix(struct in6_addr *in6)
    301       1.19    itojun {
    302       1.19    itojun 	struct rtentry *rt;
    303       1.19    itojun 	struct sockaddr_in6 sin6;
    304       1.19    itojun 	int ret;
    305       1.19    itojun 
    306       1.19    itojun 	if (ip6_keepfaith == 0)
    307       1.19    itojun 		return 0;
    308       1.19    itojun 
    309       1.21   thorpej 	memset(&sin6, 0, sizeof(sin6));
    310       1.19    itojun 	sin6.sin6_family = AF_INET6;
    311       1.19    itojun 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    312       1.19    itojun 	sin6.sin6_addr = *in6;
    313       1.19    itojun 	rt = rtalloc1((struct sockaddr *)&sin6, 0);
    314       1.19    itojun 	if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
    315       1.19    itojun 	    (rt->rt_ifp->if_flags & IFF_UP) != 0)
    316       1.19    itojun 		ret = 1;
    317       1.19    itojun 	else
    318       1.19    itojun 		ret = 0;
    319       1.19    itojun 	if (rt)
    320       1.55     ozaki 		rt_unref(rt);
    321       1.19    itojun 	return ret;
    322        1.2    itojun }
    323       1.33  christos #endif
    324       1.54  christos 
    325       1.54  christos /*
    326       1.54  christos  * Module infrastructure
    327       1.54  christos  */
    328       1.54  christos #include "if_module.h"
    329       1.54  christos 
    330       1.60  pgoyette IF_MODULE(MODULE_CLASS_DRIVER, faith, NULL)
    331