1 /* $NetBSD: if_loop.c,v 1.101 2017/12/19 03:32:35 ozaki-r 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.101 2017/12/19 03:32:35 ozaki-r Exp $"); 69 70 #ifdef _KERNEL_OPT 71 #include "opt_inet.h" 72 #include "opt_atalk.h" 73 #include "opt_mbuftrace.h" 74 #include "opt_mpls.h" 75 #include "opt_net_mpsafe.h" 76 #endif 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 #include <sys/device.h> 87 #include <sys/module.h> 88 89 #include <sys/cpu.h> 90 91 #include <net/if.h> 92 #include <net/if_types.h> 93 #include <net/netisr.h> 94 #include <net/route.h> 95 96 #ifdef INET 97 #include <netinet/in.h> 98 #include <netinet/in_systm.h> 99 #include <netinet/in_var.h> 100 #include <netinet/in_offload.h> 101 #include <netinet/ip.h> 102 #endif 103 104 #ifdef INET6 105 #ifndef INET 106 #include <netinet/in.h> 107 #endif 108 #include <netinet6/in6_var.h> 109 #include <netinet6/in6_offload.h> 110 #include <netinet/ip6.h> 111 #endif 112 113 #ifdef MPLS 114 #include <netmpls/mpls.h> 115 #include <netmpls/mpls_var.h> 116 #endif 117 118 #ifdef NETATALK 119 #include <netatalk/at.h> 120 #include <netatalk/at_var.h> 121 #endif 122 123 #include <net/bpf.h> 124 125 #if defined(LARGE_LOMTU) 126 #define LOMTU (131072 + MHLEN + MLEN) 127 #define LOMTU_MAX LOMTU 128 #else 129 #define LOMTU (32768 + MHLEN + MLEN) 130 #define LOMTU_MAX (65536 + MHLEN + MLEN) 131 #endif 132 133 #ifdef ALTQ 134 static void lostart(struct ifnet *); 135 #endif 136 137 static int loop_clone_create(struct if_clone *, int); 138 static int loop_clone_destroy(struct ifnet *); 139 140 static void loop_rtrequest(int, struct rtentry *, const struct rt_addrinfo *); 141 142 static struct if_clone loop_cloner = 143 IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy); 144 145 void 146 loopattach(int n) 147 { 148 149 #ifndef _MODULE 150 loop_clone_create(&loop_cloner, 0); /* lo0 always exists */ 151 #endif 152 } 153 154 void 155 loopinit(void) 156 { 157 158 if (lo0ifp != NULL) /* can happen in rump kernel */ 159 return; 160 161 #ifdef _MODULE 162 loop_clone_create(&loop_cloner, 0); /* lo0 always exists */ 163 #endif 164 if_clone_attach(&loop_cloner); 165 } 166 167 static int 168 loopdetach(void) 169 { 170 /* no detach for now; we don't allow lo0 to be deleted */ 171 return EBUSY; 172 } 173 174 static int 175 loop_clone_create(struct if_clone *ifc, int unit) 176 { 177 struct ifnet *ifp; 178 int rv; 179 180 ifp = if_alloc(IFT_LOOP); 181 182 if_initname(ifp, ifc->ifc_name, unit); 183 184 ifp->if_mtu = LOMTU; 185 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 186 #ifdef NET_MPSAFE 187 ifp->if_extflags = IFEF_MPSAFE; 188 #endif 189 ifp->if_ioctl = loioctl; 190 ifp->if_output = looutput; 191 #ifdef ALTQ 192 ifp->if_start = lostart; 193 #endif 194 ifp->if_type = IFT_LOOP; 195 ifp->if_hdrlen = 0; 196 ifp->if_addrlen = 0; 197 ifp->if_dlt = DLT_NULL; 198 IFQ_SET_READY(&ifp->if_snd); 199 if (unit == 0) 200 lo0ifp = ifp; 201 rv = if_attach(ifp); 202 if (rv != 0) { 203 if_free(ifp); 204 return rv; 205 } 206 if_alloc_sadl(ifp); 207 bpf_attach(ifp, DLT_NULL, sizeof(u_int)); 208 #ifdef MBUFTRACE 209 ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF, 210 M_WAITOK | M_ZERO); 211 strlcpy(ifp->if_mowner->mo_name, ifp->if_xname, 212 sizeof(ifp->if_mowner->mo_name)); 213 MOWNER_ATTACH(ifp->if_mowner); 214 #endif 215 216 ifp->if_flags |= IFF_RUNNING; 217 218 return (0); 219 } 220 221 static int 222 loop_clone_destroy(struct ifnet *ifp) 223 { 224 225 if (ifp == lo0ifp) 226 return (EPERM); 227 228 ifp->if_flags &= ~IFF_RUNNING; 229 230 #ifdef MBUFTRACE 231 MOWNER_DETACH(ifp->if_mowner); 232 free(ifp->if_mowner, M_DEVBUF); 233 #endif 234 235 bpf_detach(ifp); 236 if_detach(ifp); 237 238 if_free(ifp); 239 240 return (0); 241 } 242 243 int 244 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 245 const struct rtentry *rt) 246 { 247 pktqueue_t *pktq = NULL; 248 struct ifqueue *ifq = NULL; 249 int s, isr = -1; 250 int csum_flags; 251 int error = 0; 252 size_t pktlen; 253 254 MCLAIM(m, ifp->if_mowner); 255 256 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 257 258 if ((m->m_flags & M_PKTHDR) == 0) 259 panic("looutput: no header mbuf"); 260 if (ifp->if_flags & IFF_LOOPBACK) 261 bpf_mtap_af(ifp, dst->sa_family, m); 262 m_set_rcvif(m, ifp); 263 264 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 265 m_freem(m); 266 error = (rt->rt_flags & RTF_BLACKHOLE ? 0 : 267 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 268 goto out; 269 } 270 271 pktlen = m->m_pkthdr.len; 272 ifp->if_opackets++; 273 ifp->if_obytes += pktlen; 274 275 #ifdef ALTQ 276 /* 277 * ALTQ on the loopback interface is just for debugging. It's 278 * used only for loopback interfaces, not for a simplex interface. 279 */ 280 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) && 281 ifp->if_start == lostart) { 282 /* 283 * If the queueing discipline needs packet classification, 284 * do it before prepending the link headers. 285 */ 286 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family); 287 288 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT); 289 if (m == NULL) { 290 error = ENOBUFS; 291 goto out; 292 } 293 *(mtod(m, uint32_t *)) = dst->sa_family; 294 295 error = if_transmit_lock(ifp, m); 296 goto out; 297 } 298 #endif /* ALTQ */ 299 300 m_tag_delete_nonpersistent(m); 301 302 #ifdef MPLS 303 if (rt != NULL && rt_gettag(rt) != NULL && 304 rt_gettag(rt)->sa_family == AF_MPLS && 305 (m->m_flags & (M_MCAST | M_BCAST)) == 0) { 306 union mpls_shim msh; 307 msh.s_addr = MPLS_GETSADDR(rt); 308 if (msh.shim.label != MPLS_LABEL_IMPLNULL) { 309 ifq = &mplsintrq; 310 isr = NETISR_MPLS; 311 } 312 } 313 if (isr != NETISR_MPLS) 314 #endif 315 switch (dst->sa_family) { 316 317 #ifdef INET 318 case AF_INET: 319 csum_flags = m->m_pkthdr.csum_flags; 320 KASSERT((csum_flags & ~(M_CSUM_IPv4|M_CSUM_UDPv4)) == 0); 321 if (csum_flags != 0 && IN_LOOPBACK_NEED_CHECKSUM(csum_flags)) { 322 ip_undefer_csum(m, 0, csum_flags); 323 } 324 m->m_pkthdr.csum_flags = 0; 325 pktq = ip_pktq; 326 break; 327 #endif 328 #ifdef INET6 329 case AF_INET6: 330 csum_flags = m->m_pkthdr.csum_flags; 331 KASSERT((csum_flags & ~M_CSUM_UDPv6) == 0); 332 if (csum_flags != 0 && 333 IN6_LOOPBACK_NEED_CHECKSUM(csum_flags)) { 334 ip6_undefer_csum(m, 0, csum_flags); 335 } 336 m->m_pkthdr.csum_flags = 0; 337 m->m_flags |= M_LOOP; 338 pktq = ip6_pktq; 339 break; 340 #endif 341 #ifdef NETATALK 342 case AF_APPLETALK: 343 ifq = &atintrq2; 344 isr = NETISR_ATALK; 345 break; 346 #endif 347 default: 348 printf("%s: can't handle af%d\n", ifp->if_xname, 349 dst->sa_family); 350 m_freem(m); 351 error = EAFNOSUPPORT; 352 goto out; 353 } 354 355 s = splnet(); 356 if (__predict_true(pktq)) { 357 error = 0; 358 359 if (__predict_true(pktq_enqueue(pktq, m, 0))) { 360 ifp->if_ipackets++; 361 ifp->if_ibytes += pktlen; 362 } else { 363 m_freem(m); 364 error = ENOBUFS; 365 } 366 splx(s); 367 goto out; 368 } 369 if (IF_QFULL(ifq)) { 370 IF_DROP(ifq); 371 m_freem(m); 372 splx(s); 373 error = ENOBUFS; 374 goto out; 375 } 376 ifp->if_ipackets++; 377 ifp->if_ibytes += m->m_pkthdr.len; 378 IF_ENQUEUE(ifq, m); 379 schednetisr(isr); 380 splx(s); 381 out: 382 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 383 return error; 384 } 385 386 #ifdef ALTQ 387 static void 388 lostart(struct ifnet *ifp) 389 { 390 for (;;) { 391 pktqueue_t *pktq = NULL; 392 struct ifqueue *ifq = NULL; 393 struct mbuf *m; 394 size_t pktlen; 395 uint32_t af; 396 int s, isr = 0; 397 398 IFQ_DEQUEUE(&ifp->if_snd, m); 399 if (m == NULL) 400 return; 401 402 af = *(mtod(m, uint32_t *)); 403 m_adj(m, sizeof(uint32_t)); 404 405 switch (af) { 406 #ifdef INET 407 case AF_INET: 408 pktq = ip_pktq; 409 break; 410 #endif 411 #ifdef INET6 412 case AF_INET6: 413 m->m_flags |= M_LOOP; 414 pktq = ip6_pktq; 415 break; 416 #endif 417 #ifdef NETATALK 418 case AF_APPLETALK: 419 ifq = &atintrq2; 420 isr = NETISR_ATALK; 421 break; 422 #endif 423 default: 424 printf("%s: can't handle af%d\n", ifp->if_xname, af); 425 m_freem(m); 426 return; 427 } 428 pktlen = m->m_pkthdr.len; 429 430 s = splnet(); 431 if (__predict_true(pktq)) { 432 if (__predict_false(pktq_enqueue(pktq, m, 0))) { 433 m_freem(m); 434 splx(s); 435 return; 436 } 437 ifp->if_ipackets++; 438 ifp->if_ibytes += pktlen; 439 splx(s); 440 continue; 441 } 442 if (IF_QFULL(ifq)) { 443 IF_DROP(ifq); 444 splx(s); 445 m_freem(m); 446 return; 447 } 448 IF_ENQUEUE(ifq, m); 449 schednetisr(isr); 450 ifp->if_ipackets++; 451 ifp->if_ibytes += pktlen; 452 splx(s); 453 } 454 } 455 #endif /* ALTQ */ 456 457 /* ARGSUSED */ 458 static void 459 loop_rtrequest(int cmd, struct rtentry *rt, 460 const struct rt_addrinfo *info) 461 { 462 463 if (rt) 464 rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu; 465 } 466 467 /* 468 * Process an ioctl request. 469 */ 470 /* ARGSUSED */ 471 int 472 loioctl(struct ifnet *ifp, u_long cmd, void *data) 473 { 474 struct ifaddr *ifa; 475 struct ifreq *ifr = data; 476 int error = 0; 477 478 switch (cmd) { 479 480 case SIOCINITIFADDR: 481 ifp->if_flags |= IFF_UP; 482 ifa = (struct ifaddr *)data; 483 if (ifa != NULL) 484 ifa->ifa_rtrequest = loop_rtrequest; 485 /* 486 * Everything else is done at a higher level. 487 */ 488 break; 489 490 case SIOCSIFMTU: 491 if ((unsigned)ifr->ifr_mtu > LOMTU_MAX) 492 error = EINVAL; 493 else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET){ 494 error = 0; 495 } 496 break; 497 498 case SIOCADDMULTI: 499 case SIOCDELMULTI: 500 if (ifr == NULL) { 501 error = EAFNOSUPPORT; /* XXX */ 502 break; 503 } 504 switch (ifreq_getaddr(cmd, ifr)->sa_family) { 505 506 #ifdef INET 507 case AF_INET: 508 break; 509 #endif 510 #ifdef INET6 511 case AF_INET6: 512 break; 513 #endif 514 515 default: 516 error = EAFNOSUPPORT; 517 break; 518 } 519 break; 520 521 default: 522 error = ifioctl_common(ifp, cmd, data); 523 } 524 return (error); 525 } 526 527 /* 528 * Module infrastructure 529 */ 530 #include "if_module.h" 531 532 IF_MODULE(MODULE_CLASS_DRIVER, loop, "") 533