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