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