if_loop.c revision 115360
1193326Sed/* 2193326Sed * Copyright (c) 1982, 1986, 1993 3193326Sed * The Regents of the University of California. All rights reserved. 4193326Sed * 5193326Sed * Redistribution and use in source and binary forms, with or without 6193326Sed * modification, are permitted provided that the following conditions 7193326Sed * are met: 8193326Sed * 1. Redistributions of source code must retain the above copyright 9193326Sed * notice, this list of conditions and the following disclaimer. 10193326Sed * 2. Redistributions in binary form must reproduce the above copyright 11193326Sed * notice, this list of conditions and the following disclaimer in the 12193326Sed * documentation and/or other materials provided with the distribution. 13193326Sed * 3. All advertising materials mentioning features or use of this software 14208600Srdivacky * must display the following acknowledgement: 15208600Srdivacky * This product includes software developed by the University of 16193326Sed * California, Berkeley and its contributors. 17193326Sed * 4. Neither the name of the University nor the names of its contributors 18212904Sdim * may be used to endorse or promote products derived from this software 19198954Srdivacky * without specific prior written permission. 20193326Sed * 21193326Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22193326Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23218893Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24207619Srdivacky * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25193326Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26193326Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27193326Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28193326Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29193326Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30193326Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31198092Srdivacky * SUCH DAMAGE. 32193326Sed * 33193326Sed * @(#)if_loop.c 8.2 (Berkeley) 1/9/95 34193326Sed * $FreeBSD: head/sys/net/if_loop.c 115360 2003-05-28 02:04:33Z silby $ 35193326Sed */ 36193326Sed 37193326Sed/* 38193326Sed * Loopback interface driver for protocol testing and timing. 39193326Sed */ 40193326Sed 41193326Sed#include "opt_atalk.h" 42193326Sed#include "opt_inet.h" 43193326Sed#include "opt_inet6.h" 44193326Sed#include "opt_ipx.h" 45193326Sed#include "opt_mac.h" 46193326Sed 47193326Sed#include <sys/param.h> 48198092Srdivacky#include <sys/systm.h> 49193326Sed#include <sys/kernel.h> 50193326Sed#include <sys/mac.h> 51193326Sed#include <sys/malloc.h> 52193326Sed#include <sys/mbuf.h> 53193326Sed#include <sys/module.h> 54193326Sed#include <machine/bus.h> 55193326Sed#include <sys/rman.h> 56234353Sdim#include <sys/socket.h> 57199990Srdivacky#include <sys/sockio.h> 58199990Srdivacky#include <sys/sysctl.h> 59199990Srdivacky 60199990Srdivacky#include <net/if.h> 61199990Srdivacky#include <net/if_types.h> 62202379Srdivacky#include <net/netisr.h> 63202379Srdivacky#include <net/route.h> 64199990Srdivacky#include <net/bpf.h> 65199990Srdivacky#include <net/bpfdesc.h> 66202379Srdivacky 67234353Sdim#ifdef INET 68234353Sdim#include <netinet/in.h> 69234353Sdim#include <netinet/in_var.h> 70234353Sdim#endif 71202379Srdivacky 72202379Srdivacky#ifdef IPX 73202379Srdivacky#include <netipx/ipx.h> 74199990Srdivacky#include <netipx/ipx_if.h> 75199990Srdivacky#endif 76203955Srdivacky 77203955Srdivacky#ifdef INET6 78203955Srdivacky#ifndef INET 79203955Srdivacky#include <netinet/in.h> 80203955Srdivacky#endif 81198954Srdivacky#include <netinet6/in6_var.h> 82203955Srdivacky#include <netinet/ip6.h> 83198954Srdivacky#endif 84198954Srdivacky 85203955Srdivacky#ifdef NETATALK 86203955Srdivacky#include <netatalk/at.h> 87203955Srdivacky#include <netatalk/at_var.h> 88203955Srdivacky#endif 89203955Srdivacky 90203955Srdivacky#ifdef TINY_LOMTU 91203955Srdivacky#define LOMTU (1024+512) 92203955Srdivacky#elif defined(LARGE_LOMTU) 93193326Sed#define LOMTU 131072 94198954Srdivacky#else 95198954Srdivacky#define LOMTU 16384 96198954Srdivacky#endif 97198954Srdivacky 98198954Srdivacky#define LONAME "lo" 99203955Srdivacky 100203955Srdivackystruct lo_softc { 101218893Sdim struct ifnet sc_if; /* network-visible interface */ 102218893Sdim LIST_ENTRY(lo_softc) sc_next; 103198954Srdivacky}; 104198954Srdivacky 105198954Srdivackyint loioctl(struct ifnet *, u_long, caddr_t); 106198954Srdivackystatic void lortrequest(int, struct rtentry *, struct rt_addrinfo *); 107198954Srdivackyint looutput(struct ifnet *ifp, struct mbuf *m, 108203955Srdivacky struct sockaddr *dst, struct rtentry *rt); 109203955Srdivackyint lo_clone_create(struct if_clone *, int); 110198954Srdivackyvoid lo_clone_destroy(struct ifnet *); 111198954Srdivacky 112198954Srdivackystruct ifnet *loif = NULL; /* Used externally */ 113198954Srdivacky 114198954Srdivackystatic MALLOC_DEFINE(M_LO, LONAME, "Loopback Interface"); 115203955Srdivacky 116203955Srdivackystatic LIST_HEAD(lo_list, lo_softc) lo_list; 117203955Srdivacky 118203955Srdivackystruct if_clone lo_cloner = IF_CLONE_INITIALIZER(LONAME, 119203955Srdivacky lo_clone_create, lo_clone_destroy, 1, IF_MAXUNIT); 120198954Srdivacky 121198954Srdivackyvoid 122203955Srdivackylo_clone_destroy(ifp) 123203955Srdivacky struct ifnet *ifp; 124199990Srdivacky{ 125199990Srdivacky struct lo_softc *sc; 126203955Srdivacky 127203955Srdivacky sc = ifp->if_softc; 128198954Srdivacky 129198954Srdivacky /* XXX: destroying lo0 will lead to panics. */ 130203955Srdivacky KASSERT(loif != ifp, ("%s: destroying lo0", __func__)); 131198954Srdivacky 132234353Sdim bpfdetach(ifp); 133234353Sdim if_detach(ifp); 134193326Sed LIST_REMOVE(sc, sc_next); 135193326Sed free(sc, M_LO); 136263508Sdim} 137263508Sdim 138263508Sdimint 139263508Sdimlo_clone_create(ifc, unit) 140263508Sdim struct if_clone *ifc; 141263508Sdim int unit; 142263508Sdim{ 143263508Sdim struct lo_softc *sc; 144263508Sdim 145263508Sdim MALLOC(sc, struct lo_softc *, sizeof(*sc), M_LO, M_WAITOK | M_ZERO); 146263508Sdim 147263508Sdim sc->sc_if.if_name = LONAME; 148263508Sdim sc->sc_if.if_unit = unit; 149263508Sdim sc->sc_if.if_mtu = LOMTU; 150263508Sdim sc->sc_if.if_flags = IFF_LOOPBACK | IFF_MULTICAST; 151263508Sdim sc->sc_if.if_ioctl = loioctl; 152263508Sdim sc->sc_if.if_output = looutput; 153263508Sdim sc->sc_if.if_type = IFT_LOOP; 154263508Sdim sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen; 155263508Sdim sc->sc_if.if_softc = sc; 156263508Sdim if_attach(&sc->sc_if); 157263508Sdim bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int)); 158263508Sdim LIST_INSERT_HEAD(&lo_list, sc, sc_next); 159263508Sdim if (loif == NULL) 160263508Sdim loif = &sc->sc_if; 161263508Sdim 162263508Sdim return (0); 163263508Sdim} 164263508Sdim 165263508Sdimstatic int 166263508Sdimloop_modevent(module_t mod, int type, void *data) 167263508Sdim{ 168263508Sdim switch (type) { 169263508Sdim case MOD_LOAD: 170263508Sdim LIST_INIT(&lo_list); 171263508Sdim if_clone_attach(&lo_cloner); 172263508Sdim break; 173263508Sdim case MOD_UNLOAD: 174263508Sdim printf("loop module unload - not possible for this module type\n"); 175263508Sdim return EINVAL; 176263508Sdim } 177263508Sdim return 0; 178263508Sdim} 179263508Sdim 180263508Sdimstatic moduledata_t loop_mod = { 181263508Sdim "loop", 182263508Sdim loop_modevent, 183263508Sdim 0 184263508Sdim}; 185263508Sdim 186263508SdimDECLARE_MODULE(loop, loop_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 187263508Sdim 188263508Sdimint 189263508Sdimlooutput(ifp, m, dst, rt) 190263508Sdim struct ifnet *ifp; 191263508Sdim register struct mbuf *m; 192263508Sdim struct sockaddr *dst; 193263508Sdim register struct rtentry *rt; 194263508Sdim{ 195263508Sdim struct mbuf *n; 196193326Sed 197193326Sed M_ASSERTPKTHDR(m); /* check if we have the packet header */ 198193326Sed 199193326Sed if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 200193326Sed m_freem(m); 201193326Sed return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 202193326Sed rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 203193326Sed } 204193326Sed /* 205193326Sed * KAME requires that the packet to be contiguous on the 206198092Srdivacky * mbuf. We need to make that sure. 207193326Sed * this kind of code should be avoided. 208193326Sed * 209198092Srdivacky * XXX: KAME may no longer need contiguous packets. Once 210193326Sed * that has been verified, the following code _should_ be 211193326Sed * removed. 212198092Srdivacky */ 213193326Sed 214193326Sed if (m && m->m_next != NULL) { 215199990Srdivacky 216199990Srdivacky n = m_defrag(m, M_DONTWAIT); 217199990Srdivacky 218193326Sed if (n == NULL) { 219193326Sed m_freem(m); 220193326Sed return (ENOBUFS); 221193326Sed } else { 222193326Sed m = n; 223198092Srdivacky } 224193326Sed } 225193326Sed 226193326Sed ifp->if_opackets++; 227193326Sed ifp->if_obytes += m->m_pkthdr.len; 228193326Sed#if 1 /* XXX */ 229193326Sed switch (dst->sa_family) { 230193326Sed case AF_INET: 231193326Sed case AF_INET6: 232226633Sdim case AF_IPX: 233193326Sed case AF_APPLETALK: 234193326Sed break; 235202379Srdivacky default: 236202379Srdivacky printf("looutput: af=%d unexpected\n", dst->sa_family); 237202379Srdivacky m_freem(m); 238202379Srdivacky return (EAFNOSUPPORT); 239202379Srdivacky } 240193326Sed#endif 241207619Srdivacky return(if_simloop(ifp, m, dst->sa_family, 0)); 242207619Srdivacky} 243263508Sdim 244207619Srdivacky/* 245207619Srdivacky * if_simloop() 246207619Srdivacky * 247193326Sed * This function is to support software emulation of hardware loopback, 248193326Sed * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't 249193326Sed * hear their own broadcasts, we create a copy of the packet that we 250193326Sed * would normally receive via a hardware loopback. 251193326Sed * 252193326Sed * This function expects the packet to include the media header of length hlen. 253193326Sed */ 254193326Sed 255193326Sedint 256198092Srdivackyif_simloop(ifp, m, af, hlen) 257193326Sed struct ifnet *ifp; 258193326Sed struct mbuf *m; 259193326Sed int af; 260193326Sed int hlen; 261193326Sed{ 262193326Sed int isr; 263193326Sed 264199990Srdivacky M_ASSERTPKTHDR(m); 265199990Srdivacky m->m_pkthdr.rcvif = ifp; 266199990Srdivacky 267199990Srdivacky /* BPF write needs to be handled specially */ 268199990Srdivacky if (af == AF_UNSPEC) { 269199990Srdivacky KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len")); 270199990Srdivacky af = *(mtod(m, int *)); 271239462Sdim m->m_len -= sizeof(int); 272193326Sed m->m_pkthdr.len -= sizeof(int); 273193326Sed m->m_data += sizeof(int); 274239462Sdim } 275193326Sed 276193326Sed /* Let BPF see incoming packet */ 277193326Sed if (ifp->if_bpf) { 278193326Sed struct mbuf m0, *n = m; 279193326Sed 280193326Sed if (ifp->if_bpf->bif_dlt == DLT_NULL) { 281193326Sed /* 282193326Sed * We need to prepend the address family as 283193326Sed * a four byte field. Cons up a dummy header 284199990Srdivacky * to pacify bpf. This is safe because bpf 285234353Sdim * will only read from the mbuf (i.e., it won't 286199990Srdivacky * try to free it or keep a pointer a to it). 287193326Sed */ 288226633Sdim m0.m_next = m; 289193326Sed m0.m_len = 4; 290193326Sed m0.m_data = (char *)⁡ 291193326Sed n = &m0; 292193326Sed } 293193326Sed BPF_MTAP(ifp, n); 294193326Sed } 295193326Sed 296193326Sed /* Strip away media header */ 297193326Sed if (hlen > 0) { 298193326Sed m_adj(m, hlen); 299193326Sed#if defined(__alpha__) || defined(__ia64__) || defined(__sparc64__) 300193326Sed /* The alpha doesn't like unaligned data. 301193326Sed * We move data down in the first mbuf */ 302193326Sed if (mtod(m, vm_offset_t) & 3) { 303193326Sed KASSERT(hlen >= 3, ("if_simloop: hlen too small")); 304193326Sed bcopy(m->m_data, 305193326Sed (char *)(mtod(m, vm_offset_t) 306193326Sed - (mtod(m, vm_offset_t) & 3)), 307193326Sed m->m_len); 308199990Srdivacky mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3); 309234353Sdim } 310199990Srdivacky#endif 311199990Srdivacky } 312193326Sed 313226633Sdim /* Deliver to upper layer protocol */ 314193326Sed switch (af) { 315193326Sed#ifdef INET 316193326Sed case AF_INET: 317193326Sed isr = NETISR_IP; 318193326Sed break; 319200583Srdivacky#endif 320193326Sed#ifdef INET6 321193326Sed case AF_INET6: 322193326Sed m->m_flags |= M_LOOP; 323193326Sed isr = NETISR_IPV6; 324193326Sed break; 325193326Sed#endif 326193326Sed#ifdef IPX 327193326Sed case AF_IPX: 328199482Srdivacky isr = NETISR_IPX; 329263508Sdim break; 330199482Srdivacky#endif 331199482Srdivacky#ifdef NETATALK 332218893Sdim case AF_APPLETALK: 333193326Sed isr = NETISR_ATALK2; 334202379Srdivacky break; 335193326Sed#endif 336193326Sed default: 337208600Srdivacky printf("if_simloop: can't handle af=%d\n", af); 338193326Sed m_freem(m); 339198092Srdivacky return (EAFNOSUPPORT); 340193326Sed } 341193326Sed ifp->if_ipackets++; 342193326Sed ifp->if_ibytes += m->m_pkthdr.len; 343193326Sed netisr_dispatch(isr, m); 344193326Sed return (0); 345193326Sed} 346202379Srdivacky 347193326Sed/* ARGSUSED */ 348208600Srdivackystatic void 349208600Srdivackylortrequest(cmd, rt, info) 350208600Srdivacky int cmd; 351193326Sed struct rtentry *rt; 352202379Srdivacky struct rt_addrinfo *info; 353202379Srdivacky{ 354193326Sed if (rt) { 355193326Sed rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 356249423Sdim /* 357249423Sdim * For optimal performance, the send and receive buffers 358249423Sdim * should be at least twice the MTU plus a little more for 359249423Sdim * overhead. 360249423Sdim */ 361249423Sdim rt->rt_rmx.rmx_recvpipe = 362249423Sdim rt->rt_rmx.rmx_sendpipe = 3 * LOMTU; 363249423Sdim } 364249423Sdim} 365249423Sdim 366198092Srdivacky/* 367249423Sdim * Process an ioctl request. 368249423Sdim */ 369249423Sdim/* ARGSUSED */ 370249423Sdimint 371249423Sdimloioctl(ifp, cmd, data) 372198092Srdivacky register struct ifnet *ifp; 373198092Srdivacky u_long cmd; 374193326Sed caddr_t data; 375193326Sed{ 376193326Sed register struct ifaddr *ifa; 377198092Srdivacky register struct ifreq *ifr = (struct ifreq *)data; 378193326Sed register int error = 0; 379193326Sed 380193326Sed switch (cmd) { 381193326Sed 382198092Srdivacky case SIOCSIFADDR: 383193326Sed ifp->if_flags |= IFF_UP | IFF_RUNNING; 384198092Srdivacky ifa = (struct ifaddr *)data; 385193326Sed ifa->ifa_rtrequest = lortrequest; 386193326Sed /* 387193326Sed * Everything else is done at a higher level. 388198092Srdivacky */ 389193326Sed break; 390193326Sed 391193326Sed case SIOCADDMULTI: 392193326Sed case SIOCDELMULTI: 393193326Sed if (ifr == 0) { 394193326Sed error = EAFNOSUPPORT; /* XXX */ 395193326Sed break; 396193326Sed } 397193326Sed switch (ifr->ifr_addr.sa_family) { 398193326Sed 399193326Sed#ifdef INET 400193326Sed case AF_INET: 401193326Sed break; 402193326Sed#endif 403193326Sed#ifdef INET6 404193326Sed case AF_INET6: 405193326Sed break; 406208600Srdivacky#endif 407193326Sed 408193326Sed default: 409193326Sed error = EAFNOSUPPORT; 410193326Sed break; 411193326Sed } 412193326Sed break; 413193326Sed 414193326Sed case SIOCSIFMTU: 415198092Srdivacky ifp->if_mtu = ifr->ifr_mtu; 416193326Sed break; 417193326Sed 418193326Sed case SIOCSIFFLAGS: 419193326Sed break; 420199990Srdivacky 421199990Srdivacky default: 422202379Srdivacky error = EINVAL; 423202379Srdivacky } 424202379Srdivacky return (error); 425202379Srdivacky} 426202379Srdivacky