1 /* $NetBSD: if_loop.c,v 1.99 2017/11/17 07:37:12 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.99 2017/11/17 07:37:12 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 | IFF_RUNNING; 186 ifp->if_extflags = IFEF_MPSAFE; 187 ifp->if_ioctl = loioctl; 188 ifp->if_output = looutput; 189 #ifdef ALTQ 190 ifp->if_start = lostart; 191 #endif 192 ifp->if_type = IFT_LOOP; 193 ifp->if_hdrlen = 0; 194 ifp->if_addrlen = 0; 195 ifp->if_dlt = DLT_NULL; 196 IFQ_SET_READY(&ifp->if_snd); 197 if (unit == 0) 198 lo0ifp = ifp; 199 rv = if_attach(ifp); 200 if (rv != 0) { 201 if_free(ifp); 202 return rv; 203 } 204 if_alloc_sadl(ifp); 205 bpf_attach(ifp, DLT_NULL, sizeof(u_int)); 206 #ifdef MBUFTRACE 207 ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF, 208 M_WAITOK | M_ZERO); 209 strlcpy(ifp->if_mowner->mo_name, ifp->if_xname, 210 sizeof(ifp->if_mowner->mo_name)); 211 MOWNER_ATTACH(ifp->if_mowner); 212 #endif 213 214 return (0); 215 } 216 217 static int 218 loop_clone_destroy(struct ifnet *ifp) 219 { 220 221 if (ifp == lo0ifp) 222 return (EPERM); 223 224 #ifdef MBUFTRACE 225 MOWNER_DETACH(ifp->if_mowner); 226 free(ifp->if_mowner, M_DEVBUF); 227 #endif 228 229 bpf_detach(ifp); 230 if_detach(ifp); 231 232 if_free(ifp); 233 234 return (0); 235 } 236 237 int 238 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 239 const struct rtentry *rt) 240 { 241 pktqueue_t *pktq = NULL; 242 struct ifqueue *ifq = NULL; 243 int s, isr = -1; 244 int csum_flags; 245 int error = 0; 246 size_t pktlen; 247 248 MCLAIM(m, ifp->if_mowner); 249 250 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 251 252 if ((m->m_flags & M_PKTHDR) == 0) 253 panic("looutput: no header mbuf"); 254 if (ifp->if_flags & IFF_LOOPBACK) 255 bpf_mtap_af(ifp, dst->sa_family, m); 256 m_set_rcvif(m, ifp); 257 258 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 259 m_freem(m); 260 error = (rt->rt_flags & RTF_BLACKHOLE ? 0 : 261 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 262 goto out; 263 } 264 265 pktlen = m->m_pkthdr.len; 266 ifp->if_opackets++; 267 ifp->if_obytes += pktlen; 268 269 #ifdef ALTQ 270 /* 271 * ALTQ on the loopback interface is just for debugging. It's 272 * used only for loopback interfaces, not for a simplex interface. 273 */ 274 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) && 275 ifp->if_start == lostart) { 276 /* 277 * If the queueing discipline needs packet classification, 278 * do it before prepending the link headers. 279 */ 280 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family); 281 282 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT); 283 if (m == NULL) { 284 error = ENOBUFS; 285 goto out; 286 } 287 *(mtod(m, uint32_t *)) = dst->sa_family; 288 289 error = if_transmit_lock(ifp, m); 290 goto out; 291 } 292 #endif /* ALTQ */ 293 294 m_tag_delete_nonpersistent(m); 295 296 #ifdef MPLS 297 if (rt != NULL && rt_gettag(rt) != NULL && 298 rt_gettag(rt)->sa_family == AF_MPLS && 299 (m->m_flags & (M_MCAST | M_BCAST)) == 0) { 300 union mpls_shim msh; 301 msh.s_addr = MPLS_GETSADDR(rt); 302 if (msh.shim.label != MPLS_LABEL_IMPLNULL) { 303 ifq = &mplsintrq; 304 isr = NETISR_MPLS; 305 } 306 } 307 if (isr != NETISR_MPLS) 308 #endif 309 switch (dst->sa_family) { 310 311 #ifdef INET 312 case AF_INET: 313 csum_flags = m->m_pkthdr.csum_flags; 314 KASSERT((csum_flags & ~(M_CSUM_IPv4|M_CSUM_UDPv4)) == 0); 315 if (csum_flags != 0 && IN_LOOPBACK_NEED_CHECKSUM(csum_flags)) { 316 ip_undefer_csum(m, 0, csum_flags); 317 } 318 m->m_pkthdr.csum_flags = 0; 319 pktq = ip_pktq; 320 break; 321 #endif 322 #ifdef INET6 323 case AF_INET6: 324 csum_flags = m->m_pkthdr.csum_flags; 325 KASSERT((csum_flags & ~M_CSUM_UDPv6) == 0); 326 if (csum_flags != 0 && 327 IN6_LOOPBACK_NEED_CHECKSUM(csum_flags)) { 328 ip6_undefer_csum(m, 0, csum_flags); 329 } 330 m->m_pkthdr.csum_flags = 0; 331 m->m_flags |= M_LOOP; 332 pktq = ip6_pktq; 333 break; 334 #endif 335 #ifdef NETATALK 336 case AF_APPLETALK: 337 ifq = &atintrq2; 338 isr = NETISR_ATALK; 339 break; 340 #endif 341 default: 342 printf("%s: can't handle af%d\n", ifp->if_xname, 343 dst->sa_family); 344 m_freem(m); 345 error = EAFNOSUPPORT; 346 goto out; 347 } 348 349 s = splnet(); 350 if (__predict_true(pktq)) { 351 error = 0; 352 353 if (__predict_true(pktq_enqueue(pktq, m, 0))) { 354 ifp->if_ipackets++; 355 ifp->if_ibytes += pktlen; 356 } else { 357 m_freem(m); 358 error = ENOBUFS; 359 } 360 splx(s); 361 goto out; 362 } 363 if (IF_QFULL(ifq)) { 364 IF_DROP(ifq); 365 m_freem(m); 366 splx(s); 367 error = ENOBUFS; 368 goto out; 369 } 370 ifp->if_ipackets++; 371 ifp->if_ibytes += m->m_pkthdr.len; 372 IF_ENQUEUE(ifq, m); 373 schednetisr(isr); 374 splx(s); 375 out: 376 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 377 return error; 378 } 379 380 #ifdef ALTQ 381 static void 382 lostart(struct ifnet *ifp) 383 { 384 for (;;) { 385 pktqueue_t *pktq = NULL; 386 struct ifqueue *ifq = NULL; 387 struct mbuf *m; 388 size_t pktlen; 389 uint32_t af; 390 int s, isr = 0; 391 392 IFQ_DEQUEUE(&ifp->if_snd, m); 393 if (m == NULL) 394 return; 395 396 af = *(mtod(m, uint32_t *)); 397 m_adj(m, sizeof(uint32_t)); 398 399 switch (af) { 400 #ifdef INET 401 case AF_INET: 402 pktq = ip_pktq; 403 break; 404 #endif 405 #ifdef INET6 406 case AF_INET6: 407 m->m_flags |= M_LOOP; 408 pktq = ip6_pktq; 409 break; 410 #endif 411 #ifdef NETATALK 412 case AF_APPLETALK: 413 ifq = &atintrq2; 414 isr = NETISR_ATALK; 415 break; 416 #endif 417 default: 418 printf("%s: can't handle af%d\n", ifp->if_xname, af); 419 m_freem(m); 420 return; 421 } 422 pktlen = m->m_pkthdr.len; 423 424 s = splnet(); 425 if (__predict_true(pktq)) { 426 if (__predict_false(pktq_enqueue(pktq, m, 0))) { 427 m_freem(m); 428 splx(s); 429 return; 430 } 431 ifp->if_ipackets++; 432 ifp->if_ibytes += pktlen; 433 splx(s); 434 continue; 435 } 436 if (IF_QFULL(ifq)) { 437 IF_DROP(ifq); 438 splx(s); 439 m_freem(m); 440 return; 441 } 442 IF_ENQUEUE(ifq, m); 443 schednetisr(isr); 444 ifp->if_ipackets++; 445 ifp->if_ibytes += pktlen; 446 splx(s); 447 } 448 } 449 #endif /* ALTQ */ 450 451 /* ARGSUSED */ 452 static void 453 loop_rtrequest(int cmd, struct rtentry *rt, 454 const struct rt_addrinfo *info) 455 { 456 457 if (rt) 458 rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu; 459 } 460 461 /* 462 * Process an ioctl request. 463 */ 464 /* ARGSUSED */ 465 int 466 loioctl(struct ifnet *ifp, u_long cmd, void *data) 467 { 468 struct ifaddr *ifa; 469 struct ifreq *ifr = data; 470 int error = 0; 471 472 switch (cmd) { 473 474 case SIOCINITIFADDR: 475 ifp->if_flags |= IFF_UP; 476 ifa = (struct ifaddr *)data; 477 if (ifa != NULL) 478 ifa->ifa_rtrequest = loop_rtrequest; 479 /* 480 * Everything else is done at a higher level. 481 */ 482 break; 483 484 case SIOCSIFMTU: 485 if ((unsigned)ifr->ifr_mtu > LOMTU_MAX) 486 error = EINVAL; 487 else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET){ 488 error = 0; 489 } 490 break; 491 492 case SIOCADDMULTI: 493 case SIOCDELMULTI: 494 if (ifr == NULL) { 495 error = EAFNOSUPPORT; /* XXX */ 496 break; 497 } 498 switch (ifreq_getaddr(cmd, ifr)->sa_family) { 499 500 #ifdef INET 501 case AF_INET: 502 break; 503 #endif 504 #ifdef INET6 505 case AF_INET6: 506 break; 507 #endif 508 509 default: 510 error = EAFNOSUPPORT; 511 break; 512 } 513 break; 514 515 default: 516 error = ifioctl_common(ifp, cmd, data); 517 } 518 return (error); 519 } 520 521 /* 522 * Module infrastructure 523 */ 524 #include "if_module.h" 525 526 IF_MODULE(MODULE_CLASS_DRIVER, loop, "") 527