1 /* $NetBSD: if_loop.c,v 1.58 2006/09/07 02:40:33 dogcow Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)if_loop.c 8.2 (Berkeley) 1/9/95 61 */ 62 63 /* 64 * Loopback interface driver for protocol testing and timing. 65 */ 66 67 #include <sys/cdefs.h> 68 __KERNEL_RCSID(0, "$NetBSD: if_loop.c,v 1.58 2006/09/07 02:40:33 dogcow Exp $"); 69 70 #include "opt_inet.h" 71 #include "opt_atalk.h" 72 #include "opt_iso.h" 73 #include "opt_ipx.h" 74 #include "opt_mbuftrace.h" 75 76 #include "bpfilter.h" 77 78 #include <sys/param.h> 79 #include <sys/systm.h> 80 #include <sys/kernel.h> 81 #include <sys/mbuf.h> 82 #include <sys/socket.h> 83 #include <sys/errno.h> 84 #include <sys/ioctl.h> 85 #include <sys/time.h> 86 87 #include <machine/cpu.h> 88 89 #include <net/if.h> 90 #include <net/if_types.h> 91 #include <net/netisr.h> 92 #include <net/route.h> 93 94 #ifdef INET 95 #include <netinet/in.h> 96 #include <netinet/in_systm.h> 97 #include <netinet/in_var.h> 98 #include <netinet/ip.h> 99 #endif 100 101 #ifdef INET6 102 #ifndef INET 103 #include <netinet/in.h> 104 #endif 105 #include <netinet6/in6_var.h> 106 #include <netinet/ip6.h> 107 #endif 108 109 110 #ifdef IPX 111 #include <netipx/ipx.h> 112 #include <netipx/ipx_if.h> 113 #endif 114 115 #ifdef ISO 116 #include <netiso/iso.h> 117 #include <netiso/iso_var.h> 118 #endif 119 120 #ifdef NETATALK 121 #include <netatalk/at.h> 122 #include <netatalk/at_var.h> 123 #endif 124 125 #if NBPFILTER > 0 126 #include <net/bpf.h> 127 #endif 128 129 #if defined(LARGE_LOMTU) 130 #define LOMTU (131072 + MHLEN + MLEN) 131 #define LOMTU_MAX LOMTU 132 #else 133 #define LOMTU (32768 + MHLEN + MLEN) 134 #define LOMTU_MAX (65536 + MHLEN + MLEN) 135 #endif 136 137 #ifdef ALTQ 138 static void lostart(struct ifnet *); 139 #endif 140 141 struct loop_softc { 142 LIST_ENTRY(loop_softc) sc_list; 143 struct ifnet sc_if; 144 }; 145 146 static LIST_HEAD(, loop_softc) loop_softc_list; 147 148 static int loop_clone_create(struct if_clone *, int); 149 static int loop_clone_destroy(struct ifnet *); 150 151 static struct if_clone loop_cloner = 152 IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy); 153 154 void 155 loopattach(int n) 156 { 157 LIST_INIT(&loop_softc_list); 158 159 (void)loop_clone_create(&loop_cloner, 0); /* lo0 always exists */ 160 if_clone_attach(&loop_cloner); 161 } 162 163 static int 164 loop_clone_create(struct if_clone *ifc, int unit) 165 { 166 struct loop_softc *sc; 167 168 sc = malloc(sizeof(struct loop_softc), M_DEVBUF, M_WAITOK | M_ZERO); 169 170 snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d", 171 ifc->ifc_name, unit); 172 173 sc->sc_if.if_softc = sc; 174 sc->sc_if.if_mtu = LOMTU; 175 sc->sc_if.if_flags = IFF_LOOPBACK | IFF_MULTICAST; 176 sc->sc_if.if_ioctl = loioctl; 177 sc->sc_if.if_output = looutput; 178 #ifdef ALTQ 179 sc->sc_if.if_start = lostart; 180 #endif 181 sc->sc_if.if_type = IFT_LOOP; 182 sc->sc_if.if_hdrlen = 0; 183 sc->sc_if.if_addrlen = 0; 184 sc->sc_if.if_dlt = DLT_NULL; 185 IFQ_SET_READY(&sc->sc_if.if_snd); 186 if (unit == 0) 187 lo0ifp = &sc->sc_if; 188 if_attach(&sc->sc_if); 189 if_alloc_sadl(&sc->sc_if); 190 #if NBPFILTER > 0 191 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int)); 192 #endif 193 #ifdef MBUFTRACE 194 sc->sc_if.if_mowner = malloc(sizeof(struct mowner), M_DEVBUF, 195 M_WAITOK | M_ZERO); 196 strlcpy(sc->sc_if.if_mowner->mo_name, sc->sc_if.if_xname, 197 sizeof(sc->sc_if.if_mowner->mo_name)); 198 MOWNER_ATTACH(sc->sc_if.if_mowner); 199 #endif 200 LIST_INSERT_HEAD(&loop_softc_list, sc, sc_list); 201 202 return (0); 203 } 204 205 static int 206 loop_clone_destroy(struct ifnet *ifp) 207 { 208 struct loop_softc *sc = ifp->if_softc; 209 210 if (ifp == lo0ifp) 211 return (EPERM); 212 213 #ifdef MBUFTRACE 214 MOWNER_DETACH(ifp->if_mowner); 215 free(ifp->if_mowner, M_DEVBUF); 216 #endif 217 218 #if NBPFILTER > 0 219 bpfdetach(ifp); 220 #endif 221 if_detach(ifp); 222 223 LIST_REMOVE(sc, sc_list); 224 free(sc, M_DEVBUF); 225 226 return (0); 227 } 228 229 int 230 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 231 struct rtentry *rt) 232 { 233 int s, isr; 234 struct ifqueue *ifq = NULL; 235 236 MCLAIM(m, ifp->if_mowner); 237 if ((m->m_flags & M_PKTHDR) == 0) 238 panic("looutput: no header mbuf"); 239 #if NBPFILTER > 0 240 if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) 241 bpf_mtap_af(ifp->if_bpf, dst->sa_family, m); 242 #endif 243 m->m_pkthdr.rcvif = ifp; 244 245 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 246 m_freem(m); 247 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 248 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 249 } 250 251 ifp->if_opackets++; 252 ifp->if_obytes += m->m_pkthdr.len; 253 254 #ifdef ALTQ 255 /* 256 * ALTQ on the loopback interface is just for debugging. It's 257 * used only for loopback interfaces, not for a simplex interface. 258 */ 259 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) && 260 ifp->if_start == lostart) { 261 struct altq_pktattr pktattr; 262 int error; 263 264 /* 265 * If the queueing discipline needs packet classification, 266 * do it before prepending the link headers. 267 */ 268 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr); 269 270 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT); 271 if (m == NULL) 272 return (ENOBUFS); 273 *(mtod(m, uint32_t *)) = dst->sa_family; 274 275 s = splnet(); 276 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error); 277 (*ifp->if_start)(ifp); 278 splx(s); 279 return (error); 280 } 281 #endif /* ALTQ */ 282 283 m_tag_delete_nonpersistent(m); 284 285 switch (dst->sa_family) { 286 287 #ifdef INET 288 case AF_INET: 289 ifq = &ipintrq; 290 isr = NETISR_IP; 291 break; 292 #endif 293 #ifdef INET6 294 case AF_INET6: 295 m->m_flags |= M_LOOP; 296 ifq = &ip6intrq; 297 isr = NETISR_IPV6; 298 break; 299 #endif 300 #ifdef ISO 301 case AF_ISO: 302 ifq = &clnlintrq; 303 isr = NETISR_ISO; 304 break; 305 #endif 306 #ifdef IPX 307 case AF_IPX: 308 ifq = &ipxintrq; 309 isr = NETISR_IPX; 310 break; 311 #endif 312 #ifdef NETATALK 313 case AF_APPLETALK: 314 ifq = &atintrq2; 315 isr = NETISR_ATALK; 316 break; 317 #endif 318 default: 319 printf("%s: can't handle af%d\n", ifp->if_xname, 320 dst->sa_family); 321 m_freem(m); 322 return (EAFNOSUPPORT); 323 } 324 s = splnet(); 325 if (IF_QFULL(ifq)) { 326 IF_DROP(ifq); 327 m_freem(m); 328 splx(s); 329 return (ENOBUFS); 330 } 331 IF_ENQUEUE(ifq, m); 332 schednetisr(isr); 333 ifp->if_ipackets++; 334 ifp->if_ibytes += m->m_pkthdr.len; 335 splx(s); 336 return (0); 337 } 338 339 #ifdef ALTQ 340 static void 341 lostart(struct ifnet *ifp) 342 { 343 struct ifqueue *ifq; 344 struct mbuf *m; 345 uint32_t af; 346 int s, isr; 347 348 for (;;) { 349 IFQ_DEQUEUE(&ifp->if_snd, m); 350 if (m == NULL) 351 return; 352 353 af = *(mtod(m, uint32_t *)); 354 m_adj(m, sizeof(uint32_t)); 355 356 switch (af) { 357 #ifdef INET 358 case AF_INET: 359 ifq = &ipintrq; 360 isr = NETISR_IP; 361 break; 362 #endif 363 #ifdef INET6 364 case AF_INET6: 365 m->m_flags |= M_LOOP; 366 ifq = &ip6intrq; 367 isr = NETISR_IPV6; 368 break; 369 #endif 370 #ifdef IPX 371 case AF_IPX: 372 ifq = &ipxintrq; 373 isr = NETISR_IPX; 374 break; 375 #endif 376 #ifdef ISO 377 case AF_ISO: 378 ifq = &clnlintrq; 379 isr = NETISR_ISO; 380 break; 381 #endif 382 #ifdef NETATALK 383 case AF_APPLETALK: 384 ifq = &atintrq2; 385 isr = NETISR_ATALK; 386 break; 387 #endif 388 default: 389 printf("%s: can't handle af%d\n", ifp->if_xname, af); 390 m_freem(m); 391 return; 392 } 393 394 s = splnet(); 395 if (IF_QFULL(ifq)) { 396 IF_DROP(ifq); 397 splx(s); 398 m_freem(m); 399 return; 400 } 401 IF_ENQUEUE(ifq, m); 402 schednetisr(isr); 403 ifp->if_ipackets++; 404 ifp->if_ibytes += m->m_pkthdr.len; 405 splx(s); 406 } 407 } 408 #endif /* ALTQ */ 409 410 /* ARGSUSED */ 411 void 412 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 413 { 414 415 if (rt) 416 rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu; 417 } 418 419 /* 420 * Process an ioctl request. 421 */ 422 /* ARGSUSED */ 423 int 424 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 425 { 426 struct ifaddr *ifa; 427 struct ifreq *ifr; 428 int error = 0; 429 430 switch (cmd) { 431 432 case SIOCSIFADDR: 433 ifp->if_flags |= IFF_UP; 434 ifa = (struct ifaddr *)data; 435 if (ifa != NULL /*&& ifa->ifa_addr->sa_family == AF_ISO*/) 436 ifa->ifa_rtrequest = lortrequest; 437 /* 438 * Everything else is done at a higher level. 439 */ 440 break; 441 442 case SIOCSIFMTU: 443 ifr = (struct ifreq *)data; 444 if ((unsigned)ifr->ifr_mtu > LOMTU_MAX) 445 error = EINVAL; 446 else { 447 /* XXX update rt mtu for AF_ISO? */ 448 ifp->if_mtu = ifr->ifr_mtu; 449 } 450 break; 451 452 case SIOCADDMULTI: 453 case SIOCDELMULTI: 454 ifr = (struct ifreq *)data; 455 if (ifr == NULL) { 456 error = EAFNOSUPPORT; /* XXX */ 457 break; 458 } 459 switch (ifr->ifr_addr.sa_family) { 460 461 #ifdef INET 462 case AF_INET: 463 break; 464 #endif 465 #ifdef INET6 466 case AF_INET6: 467 break; 468 #endif 469 470 default: 471 error = EAFNOSUPPORT; 472 break; 473 } 474 break; 475 476 default: 477 error = EINVAL; 478 } 479 return (error); 480 } 481