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$ 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 103 struct lo_softc { 104 struct ifnet sc_if; /* network-visible interface */ 105 LIST_ENTRY(lo_softc) sc_next; 106 }; 107 108 int loioctl(struct ifnet *, u_long, caddr_t); 109 static void lortrequest(int, struct rtentry *, struct rt_addrinfo *); 110 int looutput(struct ifnet *ifp, struct mbuf *m, 111 struct sockaddr *dst, struct rtentry *rt); 112 int lo_clone_create(struct if_clone *, int); 113 void lo_clone_destroy(struct ifnet *); 114 115 struct ifnet *loif = NULL; /* Used externally */ 116 117 static MALLOC_DEFINE(M_LO, LONAME, "Loopback Interface"); 118 119 static LIST_HEAD(lo_list, lo_softc) lo_list; 120 121 struct if_clone lo_cloner = IF_CLONE_INITIALIZER(LONAME, 122 lo_clone_create, lo_clone_destroy, 1, IF_MAXUNIT); 123 124 void 125 lo_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 141 int 142 lo_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 168 static int 169 loop_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 183 static moduledata_t loop_mod = { 184 "loop", 185 loop_modevent, 186 0 187 }; 188 189 DECLARE_MODULE(loop, loop_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 190 191 int 192 looutput(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 SLIST_INIT(&m->m_pkthdr.tags); 228 m_freem(m); 229 m = n; 230 } 231 if (0) { 232 contiguousfail: 233 printf("looutput: mbuf allocation failed\n"); 234 } 235 236 ifp->if_opackets++; 237 ifp->if_obytes += m->m_pkthdr.len; 238 #if 1 /* XXX */ 239 switch (dst->sa_family) { 240 case AF_INET: 241 case AF_INET6: 242 case AF_IPX: 243 case AF_NS: 244 case AF_APPLETALK: 245 break; 246 default: 247 printf("looutput: af=%d unexpected\n", dst->sa_family); 248 m_freem(m); 249 return (EAFNOSUPPORT); 250 } 251 #endif 252 return(if_simloop(ifp, m, dst->sa_family, 0)); 253 } 254 255 /* 256 * if_simloop() 257 * 258 * This function is to support software emulation of hardware loopback, 259 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't 260 * hear their own broadcasts, we create a copy of the packet that we 261 * would normally receive via a hardware loopback. 262 * 263 * This function expects the packet to include the media header of length hlen. 264 */ 265 266 int 267 if_simloop(ifp, m, af, hlen) 268 struct ifnet *ifp; 269 struct mbuf *m; 270 int af; 271 int hlen; 272 { 273 int isr; 274 struct ifqueue *inq = 0; 275 276 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR")); 277 m->m_pkthdr.rcvif = ifp; 278 279 /* BPF write needs to be handled specially */ 280 if (af == AF_UNSPEC) { 281 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len")); 282 af = *(mtod(m, int *)); 283 m->m_len -= sizeof(int); 284 m->m_pkthdr.len -= sizeof(int); 285 m->m_data += sizeof(int); 286 } 287 288 /* Let BPF see incoming packet */ 289 if (ifp->if_bpf) { 290 struct mbuf m0, *n = m; 291 292 if (ifp->if_bpf->bif_dlt == DLT_NULL) { 293 /* 294 * We need to prepend the address family as 295 * a four byte field. Cons up a dummy header 296 * to pacify bpf. This is safe because bpf 297 * will only read from the mbuf (i.e., it won't 298 * try to free it or keep a pointer a to it). 299 */ 300 m0.m_next = m; 301 m0.m_len = 4; 302 m0.m_data = (char *)⁡ 303 n = &m0; 304 } 305 bpf_mtap(ifp, n); 306 } 307 308 /* Strip away media header */ 309 if (hlen > 0) { 310 m_adj(m, hlen); 311 #if defined(__alpha__) || defined(__ia64__) || defined(__sparc64__) 312 /* The alpha doesn't like unaligned data. 313 * We move data down in the first mbuf */ 314 if (mtod(m, vm_offset_t) & 3) { 315 KASSERT(hlen >= 3, ("if_simloop: hlen too small")); 316 bcopy(m->m_data, 317 (char *)(mtod(m, vm_offset_t) 318 - (mtod(m, vm_offset_t) & 3)), 319 m->m_len); 320 mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3); 321 } 322 #endif 323 } 324 325 /* Deliver to upper layer protocol */ 326 switch (af) { 327 #ifdef INET 328 case AF_INET: 329 inq = &ipintrq; 330 isr = NETISR_IP; 331 break; 332 #endif 333 #ifdef INET6 334 case AF_INET6: 335 m->m_flags |= M_LOOP; 336 inq = &ip6intrq; 337 isr = NETISR_IPV6; 338 break; 339 #endif 340 #ifdef IPX 341 case AF_IPX: 342 inq = &ipxintrq; 343 isr = NETISR_IPX; 344 break; 345 #endif 346 #ifdef NS 347 case AF_NS: 348 inq = &nsintrq; 349 isr = NETISR_NS; 350 break; 351 #endif 352 #ifdef NETATALK 353 case AF_APPLETALK: 354 inq = &atintrq2; 355 isr = NETISR_ATALK; 356 break; 357 #endif 358 default: 359 printf("if_simloop: can't handle af=%d\n", af); 360 m_freem(m); 361 return (EAFNOSUPPORT); 362 } 363 ifp->if_ipackets++; 364 ifp->if_ibytes += m->m_pkthdr.len; 365 (void) IF_HANDOFF(inq, m, NULL); 366 schednetisr(isr); 367 return (0); 368 } 369 370 /* ARGSUSED */ 371 static void 372 lortrequest(cmd, rt, info) 373 int cmd; 374 struct rtentry *rt; 375 struct rt_addrinfo *info; 376 { 377 if (rt) { 378 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 379 /* 380 * For optimal performance, the send and receive buffers 381 * should be at least twice the MTU plus a little more for 382 * overhead. 383 */ 384 rt->rt_rmx.rmx_recvpipe = 385 rt->rt_rmx.rmx_sendpipe = 3 * LOMTU; 386 } 387 } 388 389 /* 390 * Process an ioctl request. 391 */ 392 /* ARGSUSED */ 393 int 394 loioctl(ifp, cmd, data) 395 register struct ifnet *ifp; 396 u_long cmd; 397 caddr_t data; 398 { 399 register struct ifaddr *ifa; 400 register struct ifreq *ifr = (struct ifreq *)data; 401 register int error = 0; 402 403 switch (cmd) { 404 405 case SIOCSIFADDR: 406 ifp->if_flags |= IFF_UP | IFF_RUNNING; 407 ifa = (struct ifaddr *)data; 408 ifa->ifa_rtrequest = lortrequest; 409 /* 410 * Everything else is done at a higher level. 411 */ 412 break; 413 414 case SIOCADDMULTI: 415 case SIOCDELMULTI: 416 if (ifr == 0) { 417 error = EAFNOSUPPORT; /* XXX */ 418 break; 419 } 420 switch (ifr->ifr_addr.sa_family) { 421 422 #ifdef INET 423 case AF_INET: 424 break; 425 #endif 426 #ifdef INET6 427 case AF_INET6: 428 break; 429 #endif 430 431 default: 432 error = EAFNOSUPPORT; 433 break; 434 } 435 break; 436 437 case SIOCSIFMTU: 438 ifp->if_mtu = ifr->ifr_mtu; 439 break; 440 441 case SIOCSIFFLAGS: 442 break; 443 444 default: 445 error = EINVAL; 446 } 447 return (error); 448 } 449