if_loop.c revision 97289
1/* 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if_loop.c 8.2 (Berkeley) 1/9/95 34 * $FreeBSD: head/sys/net/if_loop.c 97289 2002-05-25 20:17:04Z brooks $ 35 */ 36 37/* 38 * Loopback interface driver for protocol testing and timing. 39 */ 40 41#include "opt_atalk.h" 42#include "opt_inet.h" 43#include "opt_inet6.h" 44#include "opt_ipx.h" 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/kernel.h> 49#include <sys/malloc.h> 50#include <sys/mbuf.h> 51#include <sys/module.h> 52#include <machine/bus.h> 53#include <sys/rman.h> 54#include <sys/socket.h> 55#include <sys/sockio.h> 56#include <sys/sysctl.h> 57 58#include <net/if.h> 59#include <net/if_types.h> 60#include <net/netisr.h> 61#include <net/route.h> 62#include <net/bpf.h> 63#include <net/bpfdesc.h> 64 65#ifdef INET 66#include <netinet/in.h> 67#include <netinet/in_var.h> 68#endif 69 70#ifdef IPX 71#include <netipx/ipx.h> 72#include <netipx/ipx_if.h> 73#endif 74 75#ifdef INET6 76#ifndef INET 77#include <netinet/in.h> 78#endif 79#include <netinet6/in6_var.h> 80#include <netinet/ip6.h> 81#endif 82 83#ifdef NS 84#include <netns/ns.h> 85#include <netns/ns_if.h> 86#endif 87 88#ifdef NETATALK 89#include <netatalk/at.h> 90#include <netatalk/at_var.h> 91#endif 92 93#ifdef TINY_LOMTU 94#define LOMTU (1024+512) 95#elif defined(LARGE_LOMTU) 96#define LOMTU 131072 97#else 98#define LOMTU 16384 99#endif 100 101#define LONAME "lo" 102 103struct lo_softc { 104 struct ifnet sc_if; /* network-visible interface */ 105 LIST_ENTRY(lo_softc) sc_next; 106}; 107 108int loioctl(struct ifnet *, u_long, caddr_t); 109static void lortrequest(int, struct rtentry *, struct rt_addrinfo *); 110int looutput(struct ifnet *ifp, struct mbuf *m, 111 struct sockaddr *dst, struct rtentry *rt); 112int lo_clone_create(struct if_clone *, int); 113void lo_clone_destroy(struct ifnet *); 114 115struct ifnet *loif = NULL; /* Used externally */ 116 117static MALLOC_DEFINE(M_LO, LONAME, "Loopback Interface"); 118 119static LIST_HEAD(lo_list, lo_softc) lo_list; 120 121struct if_clone lo_cloner = IF_CLONE_INITIALIZER(LONAME, 122 lo_clone_create, lo_clone_destroy, 1, IF_MAXUNIT); 123 124void 125lo_clone_destroy(ifp) 126 struct ifnet *ifp; 127{ 128 struct lo_softc *sc; 129 130 sc = ifp->if_softc; 131 132 /* XXX: destroying lo0 will lead to panics. */ 133 KASSERT(loif != ifp, ("%s: destroying lo0", __func__)); 134 135 bpfdetach(ifp); 136 if_detach(ifp); 137 LIST_REMOVE(sc, sc_next); 138 free(sc, M_LO); 139} 140 141int 142lo_clone_create(ifc, unit) 143 struct if_clone *ifc; 144 int unit; 145{ 146 struct lo_softc *sc; 147 148 MALLOC(sc, struct lo_softc *, sizeof(*sc), M_LO, M_WAITOK | M_ZERO); 149 150 sc->sc_if.if_name = LONAME; 151 sc->sc_if.if_unit = unit; 152 sc->sc_if.if_mtu = LOMTU; 153 sc->sc_if.if_flags = IFF_LOOPBACK | IFF_MULTICAST; 154 sc->sc_if.if_ioctl = loioctl; 155 sc->sc_if.if_output = looutput; 156 sc->sc_if.if_type = IFT_LOOP; 157 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen; 158 sc->sc_if.if_softc = sc; 159 if_attach(&sc->sc_if); 160 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int)); 161 LIST_INSERT_HEAD(&lo_list, sc, sc_next); 162 if (loif == NULL) 163 loif = &sc->sc_if; 164 165 return (0); 166} 167 168static int 169loop_modevent(module_t mod, int type, void *data) 170{ 171 switch (type) { 172 case MOD_LOAD: 173 LIST_INIT(&lo_list); 174 if_clone_attach(&lo_cloner); 175 break; 176 case MOD_UNLOAD: 177 printf("loop module unload - not possible for this module type\n"); 178 return EINVAL; 179 } 180 return 0; 181} 182 183static moduledata_t loop_mod = { 184 "loop", 185 loop_modevent, 186 0 187}; 188 189DECLARE_MODULE(loop, loop_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 190 191int 192looutput(ifp, m, dst, rt) 193 struct ifnet *ifp; 194 register struct mbuf *m; 195 struct sockaddr *dst; 196 register struct rtentry *rt; 197{ 198 if ((m->m_flags & M_PKTHDR) == 0) 199 panic("looutput no HDR"); 200 201 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 202 m_freem(m); 203 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 204 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 205 } 206 /* 207 * KAME requires that the packet to be contiguous on the 208 * mbuf. We need to make that sure. 209 * this kind of code should be avoided. 210 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 211 */ 212 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 213 struct mbuf *n; 214 215 MGETHDR(n, M_DONTWAIT, MT_HEADER); 216 if (!n) 217 goto contiguousfail; 218 MCLGET(n, M_DONTWAIT); 219 if (! (n->m_flags & M_EXT)) { 220 m_freem(n); 221 goto contiguousfail; 222 } 223 224 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t)); 225 n->m_pkthdr = m->m_pkthdr; 226 n->m_len = m->m_pkthdr.len; 227 n->m_pkthdr.aux = m->m_pkthdr.aux; 228 m->m_pkthdr.aux = (struct mbuf *)NULL; 229 m_freem(m); 230 m = n; 231 } 232 if (0) { 233contiguousfail: 234 printf("looutput: mbuf allocation failed\n"); 235 } 236 237 ifp->if_opackets++; 238 ifp->if_obytes += m->m_pkthdr.len; 239#if 1 /* XXX */ 240 switch (dst->sa_family) { 241 case AF_INET: 242 case AF_INET6: 243 case AF_IPX: 244 case AF_NS: 245 case AF_APPLETALK: 246 break; 247 default: 248 printf("looutput: af=%d unexpected\n", dst->sa_family); 249 m_freem(m); 250 return (EAFNOSUPPORT); 251 } 252#endif 253 return(if_simloop(ifp, m, dst->sa_family, 0)); 254} 255 256/* 257 * if_simloop() 258 * 259 * This function is to support software emulation of hardware loopback, 260 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't 261 * hear their own broadcasts, we create a copy of the packet that we 262 * would normally receive via a hardware loopback. 263 * 264 * This function expects the packet to include the media header of length hlen. 265 */ 266 267int 268if_simloop(ifp, m, af, hlen) 269 struct ifnet *ifp; 270 struct mbuf *m; 271 int af; 272 int hlen; 273{ 274 int isr; 275 struct ifqueue *inq = 0; 276 277 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR")); 278 m->m_pkthdr.rcvif = ifp; 279 280 /* BPF write needs to be handled specially */ 281 if (af == AF_UNSPEC) { 282 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len")); 283 af = *(mtod(m, int *)); 284 m->m_len -= sizeof(int); 285 m->m_pkthdr.len -= sizeof(int); 286 m->m_data += sizeof(int); 287 } 288 289 /* Let BPF see incoming packet */ 290 if (ifp->if_bpf) { 291 struct mbuf m0, *n = m; 292 293 if (ifp->if_bpf->bif_dlt == DLT_NULL) { 294 /* 295 * We need to prepend the address family as 296 * a four byte field. Cons up a dummy header 297 * to pacify bpf. This is safe because bpf 298 * will only read from the mbuf (i.e., it won't 299 * try to free it or keep a pointer a to it). 300 */ 301 m0.m_next = m; 302 m0.m_len = 4; 303 m0.m_data = (char *)⁡ 304 n = &m0; 305 } 306 bpf_mtap(ifp, n); 307 } 308 309 /* Strip away media header */ 310 if (hlen > 0) { 311 m_adj(m, hlen); 312#if defined(__alpha__) || defined(__ia64__) || defined(__sparc64__) 313 /* The alpha doesn't like unaligned data. 314 * We move data down in the first mbuf */ 315 if (mtod(m, vm_offset_t) & 3) { 316 KASSERT(hlen >= 3, ("if_simloop: hlen too small")); 317 bcopy(m->m_data, 318 (char *)(mtod(m, vm_offset_t) 319 - (mtod(m, vm_offset_t) & 3)), 320 m->m_len); 321 mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3); 322 } 323#endif 324 } 325 326 /* Deliver to upper layer protocol */ 327 switch (af) { 328#ifdef INET 329 case AF_INET: 330 inq = &ipintrq; 331 isr = NETISR_IP; 332 break; 333#endif 334#ifdef INET6 335 case AF_INET6: 336 m->m_flags |= M_LOOP; 337 inq = &ip6intrq; 338 isr = NETISR_IPV6; 339 break; 340#endif 341#ifdef IPX 342 case AF_IPX: 343 inq = &ipxintrq; 344 isr = NETISR_IPX; 345 break; 346#endif 347#ifdef NS 348 case AF_NS: 349 inq = &nsintrq; 350 isr = NETISR_NS; 351 break; 352#endif 353#ifdef NETATALK 354 case AF_APPLETALK: 355 inq = &atintrq2; 356 isr = NETISR_ATALK; 357 break; 358#endif 359 default: 360 printf("if_simloop: can't handle af=%d\n", af); 361 m_freem(m); 362 return (EAFNOSUPPORT); 363 } 364 ifp->if_ipackets++; 365 ifp->if_ibytes += m->m_pkthdr.len; 366 (void) IF_HANDOFF(inq, m, NULL); 367 schednetisr(isr); 368 return (0); 369} 370 371/* ARGSUSED */ 372static void 373lortrequest(cmd, rt, info) 374 int cmd; 375 struct rtentry *rt; 376 struct rt_addrinfo *info; 377{ 378 if (rt) { 379 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 380 /* 381 * For optimal performance, the send and receive buffers 382 * should be at least twice the MTU plus a little more for 383 * overhead. 384 */ 385 rt->rt_rmx.rmx_recvpipe = 386 rt->rt_rmx.rmx_sendpipe = 3 * LOMTU; 387 } 388} 389 390/* 391 * Process an ioctl request. 392 */ 393/* ARGSUSED */ 394int 395loioctl(ifp, cmd, data) 396 register struct ifnet *ifp; 397 u_long cmd; 398 caddr_t data; 399{ 400 register struct ifaddr *ifa; 401 register struct ifreq *ifr = (struct ifreq *)data; 402 register int error = 0; 403 404 switch (cmd) { 405 406 case SIOCSIFADDR: 407 ifp->if_flags |= IFF_UP | IFF_RUNNING; 408 ifa = (struct ifaddr *)data; 409 ifa->ifa_rtrequest = lortrequest; 410 /* 411 * Everything else is done at a higher level. 412 */ 413 break; 414 415 case SIOCADDMULTI: 416 case SIOCDELMULTI: 417 if (ifr == 0) { 418 error = EAFNOSUPPORT; /* XXX */ 419 break; 420 } 421 switch (ifr->ifr_addr.sa_family) { 422 423#ifdef INET 424 case AF_INET: 425 break; 426#endif 427#ifdef INET6 428 case AF_INET6: 429 break; 430#endif 431 432 default: 433 error = EAFNOSUPPORT; 434 break; 435 } 436 break; 437 438 case SIOCSIFMTU: 439 ifp->if_mtu = ifr->ifr_mtu; 440 break; 441 442 case SIOCSIFFLAGS: 443 break; 444 445 default: 446 error = EINVAL; 447 } 448 return (error); 449} 450