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 #include "opt_mac.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/mac.h> 51 #include <sys/malloc.h> 52 #include <sys/mbuf.h> 53 #include <sys/module.h> 54 #include <machine/bus.h> 55 #include <sys/rman.h> 56 #include <sys/socket.h> 57 #include <sys/sockio.h> 58 #include <sys/sysctl.h> 59 60 #include <net/if.h> 61 #include <net/if_types.h> 62 #include <net/netisr.h> 63 #include <net/route.h> 64 #include <net/bpf.h> 65 #include <net/bpfdesc.h> 66 67 #ifdef INET 68 #include <netinet/in.h> 69 #include <netinet/in_var.h> 70 #endif 71 72 #ifdef IPX 73 #include <netipx/ipx.h> 74 #include <netipx/ipx_if.h> 75 #endif 76 77 #ifdef INET6 78 #ifndef INET 79 #include <netinet/in.h> 80 #endif 81 #include <netinet6/in6_var.h> 82 #include <netinet/ip6.h> 83 #endif 84 85 #ifdef NETATALK 86 #include <netatalk/at.h> 87 #include <netatalk/at_var.h> 88 #endif 89 90 #ifdef TINY_LOMTU 91 #define LOMTU (1024+512) 92 #elif defined(LARGE_LOMTU) 93 #define LOMTU 131072 94 #else 95 #define LOMTU 16384 96 #endif 97 98 #define LONAME "lo" 99 100 struct lo_softc { 101 struct ifnet sc_if; /* network-visible interface */ 102 LIST_ENTRY(lo_softc) sc_next; 103 }; 104 105 int loioctl(struct ifnet *, u_long, caddr_t); 106 static void lortrequest(int, struct rtentry *, struct rt_addrinfo *); 107 int looutput(struct ifnet *ifp, struct mbuf *m, 108 struct sockaddr *dst, struct rtentry *rt); 109 int lo_clone_create(struct if_clone *, int); 110 void lo_clone_destroy(struct ifnet *); 111 112 struct ifnet *loif = NULL; /* Used externally */ 113 114 static MALLOC_DEFINE(M_LO, LONAME, "Loopback Interface"); 115 116 static LIST_HEAD(lo_list, lo_softc) lo_list; 117 118 struct if_clone lo_cloner = IF_CLONE_INITIALIZER(LONAME, 119 lo_clone_create, lo_clone_destroy, 1, IF_MAXUNIT); 120 121 void 122 lo_clone_destroy(ifp) 123 struct ifnet *ifp; 124 { 125 struct lo_softc *sc; 126 127 sc = ifp->if_softc; 128 129 /* XXX: destroying lo0 will lead to panics. */ 130 KASSERT(loif != ifp, ("%s: destroying lo0", __func__)); 131 132 bpfdetach(ifp); 133 if_detach(ifp); 134 LIST_REMOVE(sc, sc_next); 135 free(sc, M_LO); 136 } 137 138 int 139 lo_clone_create(ifc, unit) 140 struct if_clone *ifc; 141 int unit; 142 { 143 struct lo_softc *sc; 144 145 MALLOC(sc, struct lo_softc *, sizeof(*sc), M_LO, M_WAITOK | M_ZERO); 146 147 sc->sc_if.if_name = LONAME; 148 sc->sc_if.if_unit = unit; 149 sc->sc_if.if_mtu = LOMTU; 150 sc->sc_if.if_flags = IFF_LOOPBACK | IFF_MULTICAST; 151 sc->sc_if.if_ioctl = loioctl; 152 sc->sc_if.if_output = looutput; 153 sc->sc_if.if_type = IFT_LOOP; 154 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen; 155 sc->sc_if.if_softc = sc; 156 if_attach(&sc->sc_if); 157 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int)); 158 LIST_INSERT_HEAD(&lo_list, sc, sc_next); 159 if (loif == NULL) 160 loif = &sc->sc_if; 161 162 return (0); 163 } 164 165 static int 166 loop_modevent(module_t mod, int type, void *data) 167 { 168 switch (type) { 169 case MOD_LOAD: 170 LIST_INIT(&lo_list); 171 if_clone_attach(&lo_cloner); 172 break; 173 case MOD_UNLOAD: 174 printf("loop module unload - not possible for this module type\n"); 175 return EINVAL; 176 } 177 return 0; 178 } 179 180 static moduledata_t loop_mod = { 181 "loop", 182 loop_modevent, 183 0 184 }; 185 186 DECLARE_MODULE(loop, loop_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 187 188 int 189 looutput(ifp, m, dst, rt) 190 struct ifnet *ifp; 191 register struct mbuf *m; 192 struct sockaddr *dst; 193 register struct rtentry *rt; 194 { 195 if ((m->m_flags & M_PKTHDR) == 0) 196 panic("looutput no HDR"); 197 198 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 199 m_freem(m); 200 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 201 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 202 } 203 /* 204 * KAME requires that the packet to be contiguous on the 205 * mbuf. We need to make that sure. 206 * this kind of code should be avoided. 207 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 208 */ 209 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 210 struct mbuf *n; 211 212 /* XXX MT_HEADER should be m->m_type */ 213 MGETHDR(n, M_DONTWAIT, MT_HEADER); 214 if (!n) 215 goto contiguousfail; 216 M_MOVE_PKTHDR(n, m); 217 #ifdef MAC 218 /* 219 * XXXMAC: Once we put labels in tags and proper 220 * primitives are used for relocating mbuf header 221 * data, this will no longer be required. 222 */ 223 m->m_pkthdr.label.l_flags &= ~MAC_FLAG_INITIALIZED; 224 #endif 225 MCLGET(n, M_DONTWAIT); 226 if (! (n->m_flags & M_EXT)) { 227 m_freem(n); 228 goto contiguousfail; 229 } 230 231 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 232 n->m_len = n->m_pkthdr.len; 233 m_freem(m); 234 m = n; 235 } 236 if (0) { 237 contiguousfail: 238 printf("looutput: mbuf allocation failed\n"); 239 } 240 241 ifp->if_opackets++; 242 ifp->if_obytes += m->m_pkthdr.len; 243 #if 1 /* XXX */ 244 switch (dst->sa_family) { 245 case AF_INET: 246 case AF_INET6: 247 case AF_IPX: 248 case AF_APPLETALK: 249 break; 250 default: 251 printf("looutput: af=%d unexpected\n", dst->sa_family); 252 m_freem(m); 253 return (EAFNOSUPPORT); 254 } 255 #endif 256 return(if_simloop(ifp, m, dst->sa_family, 0)); 257 } 258 259 /* 260 * if_simloop() 261 * 262 * This function is to support software emulation of hardware loopback, 263 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't 264 * hear their own broadcasts, we create a copy of the packet that we 265 * would normally receive via a hardware loopback. 266 * 267 * This function expects the packet to include the media header of length hlen. 268 */ 269 270 int 271 if_simloop(ifp, m, af, hlen) 272 struct ifnet *ifp; 273 struct mbuf *m; 274 int af; 275 int hlen; 276 { 277 int isr; 278 279 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR")); 280 m->m_pkthdr.rcvif = ifp; 281 282 /* BPF write needs to be handled specially */ 283 if (af == AF_UNSPEC) { 284 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len")); 285 af = *(mtod(m, int *)); 286 m->m_len -= sizeof(int); 287 m->m_pkthdr.len -= sizeof(int); 288 m->m_data += sizeof(int); 289 } 290 291 /* Let BPF see incoming packet */ 292 if (ifp->if_bpf) { 293 struct mbuf m0, *n = m; 294 295 if (ifp->if_bpf->bif_dlt == DLT_NULL) { 296 /* 297 * We need to prepend the address family as 298 * a four byte field. Cons up a dummy header 299 * to pacify bpf. This is safe because bpf 300 * will only read from the mbuf (i.e., it won't 301 * try to free it or keep a pointer a to it). 302 */ 303 m0.m_next = m; 304 m0.m_len = 4; 305 m0.m_data = (char *)⁡ 306 n = &m0; 307 } 308 BPF_MTAP(ifp, n); 309 } 310 311 /* Strip away media header */ 312 if (hlen > 0) { 313 m_adj(m, hlen); 314 #if defined(__alpha__) || defined(__ia64__) || defined(__sparc64__) 315 /* The alpha doesn't like unaligned data. 316 * We move data down in the first mbuf */ 317 if (mtod(m, vm_offset_t) & 3) { 318 KASSERT(hlen >= 3, ("if_simloop: hlen too small")); 319 bcopy(m->m_data, 320 (char *)(mtod(m, vm_offset_t) 321 - (mtod(m, vm_offset_t) & 3)), 322 m->m_len); 323 mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3); 324 } 325 #endif 326 } 327 328 /* Deliver to upper layer protocol */ 329 switch (af) { 330 #ifdef INET 331 case AF_INET: 332 isr = NETISR_IP; 333 break; 334 #endif 335 #ifdef INET6 336 case AF_INET6: 337 m->m_flags |= M_LOOP; 338 isr = NETISR_IPV6; 339 break; 340 #endif 341 #ifdef IPX 342 case AF_IPX: 343 isr = NETISR_IPX; 344 break; 345 #endif 346 #ifdef NETATALK 347 case AF_APPLETALK: 348 isr = NETISR_ATALK2; 349 break; 350 #endif 351 default: 352 printf("if_simloop: can't handle af=%d\n", af); 353 m_freem(m); 354 return (EAFNOSUPPORT); 355 } 356 ifp->if_ipackets++; 357 ifp->if_ibytes += m->m_pkthdr.len; 358 netisr_dispatch(isr, m); 359 return (0); 360 } 361 362 /* ARGSUSED */ 363 static void 364 lortrequest(cmd, rt, info) 365 int cmd; 366 struct rtentry *rt; 367 struct rt_addrinfo *info; 368 { 369 if (rt) { 370 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 371 /* 372 * For optimal performance, the send and receive buffers 373 * should be at least twice the MTU plus a little more for 374 * overhead. 375 */ 376 rt->rt_rmx.rmx_recvpipe = 377 rt->rt_rmx.rmx_sendpipe = 3 * LOMTU; 378 } 379 } 380 381 /* 382 * Process an ioctl request. 383 */ 384 /* ARGSUSED */ 385 int 386 loioctl(ifp, cmd, data) 387 register struct ifnet *ifp; 388 u_long cmd; 389 caddr_t data; 390 { 391 register struct ifaddr *ifa; 392 register struct ifreq *ifr = (struct ifreq *)data; 393 register int error = 0; 394 395 switch (cmd) { 396 397 case SIOCSIFADDR: 398 ifp->if_flags |= IFF_UP | IFF_RUNNING; 399 ifa = (struct ifaddr *)data; 400 ifa->ifa_rtrequest = lortrequest; 401 /* 402 * Everything else is done at a higher level. 403 */ 404 break; 405 406 case SIOCADDMULTI: 407 case SIOCDELMULTI: 408 if (ifr == 0) { 409 error = EAFNOSUPPORT; /* XXX */ 410 break; 411 } 412 switch (ifr->ifr_addr.sa_family) { 413 414 #ifdef INET 415 case AF_INET: 416 break; 417 #endif 418 #ifdef INET6 419 case AF_INET6: 420 break; 421 #endif 422 423 default: 424 error = EAFNOSUPPORT; 425 break; 426 } 427 break; 428 429 case SIOCSIFMTU: 430 ifp->if_mtu = ifr->ifr_mtu; 431 break; 432 433 case SIOCSIFFLAGS: 434 break; 435 436 default: 437 error = EINVAL; 438 } 439 return (error); 440 } 441