1 /* $NetBSD: if_loop.c,v 1.78 2014/05/20 19:53:50 pooka 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.78 2014/05/20 19:53:50 pooka 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 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 int s, isr = -1; 215 struct ifqueue *ifq = NULL; 216 int csum_flags; 217 218 MCLAIM(m, ifp->if_mowner); 219 KASSERT(KERNEL_LOCKED_P()); 220 221 if ((m->m_flags & M_PKTHDR) == 0) 222 panic("looutput: no header mbuf"); 223 if (ifp->if_flags & IFF_LOOPBACK) 224 bpf_mtap_af(ifp, dst->sa_family, m); 225 m->m_pkthdr.rcvif = ifp; 226 227 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 228 m_freem(m); 229 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 230 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 231 } 232 233 ifp->if_opackets++; 234 ifp->if_obytes += m->m_pkthdr.len; 235 236 #ifdef ALTQ 237 /* 238 * ALTQ on the loopback interface is just for debugging. It's 239 * used only for loopback interfaces, not for a simplex interface. 240 */ 241 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) && 242 ifp->if_start == lostart) { 243 struct altq_pktattr pktattr; 244 int error; 245 246 /* 247 * If the queueing discipline needs packet classification, 248 * do it before prepending the link headers. 249 */ 250 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr); 251 252 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT); 253 if (m == NULL) 254 return (ENOBUFS); 255 *(mtod(m, uint32_t *)) = dst->sa_family; 256 257 s = splnet(); 258 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error); 259 (*ifp->if_start)(ifp); 260 splx(s); 261 return (error); 262 } 263 #endif /* ALTQ */ 264 265 m_tag_delete_nonpersistent(m); 266 267 #ifdef MPLS 268 if (rt != NULL && rt_gettag(rt) != NULL && 269 rt_gettag(rt)->sa_family == AF_MPLS && 270 (m->m_flags & (M_MCAST | M_BCAST)) == 0) { 271 union mpls_shim msh; 272 msh.s_addr = MPLS_GETSADDR(rt); 273 if (msh.shim.label != MPLS_LABEL_IMPLNULL) { 274 ifq = &mplsintrq; 275 isr = NETISR_MPLS; 276 } 277 } 278 if (isr != NETISR_MPLS) 279 #endif 280 switch (dst->sa_family) { 281 282 #ifdef INET 283 case AF_INET: 284 csum_flags = m->m_pkthdr.csum_flags; 285 KASSERT((csum_flags & ~(M_CSUM_IPv4|M_CSUM_UDPv4)) == 0); 286 if (csum_flags != 0 && IN_LOOPBACK_NEED_CHECKSUM(csum_flags)) { 287 ip_undefer_csum(m, 0, csum_flags); 288 } 289 m->m_pkthdr.csum_flags = 0; 290 ifq = &ipintrq; 291 isr = NETISR_IP; 292 break; 293 #endif 294 #ifdef INET6 295 case AF_INET6: 296 csum_flags = m->m_pkthdr.csum_flags; 297 KASSERT((csum_flags & ~M_CSUM_UDPv6) == 0); 298 if (csum_flags != 0 && 299 IN6_LOOPBACK_NEED_CHECKSUM(csum_flags)) { 300 ip6_undefer_csum(m, 0, csum_flags); 301 } 302 m->m_pkthdr.csum_flags = 0; 303 m->m_flags |= M_LOOP; 304 ifq = &ip6intrq; 305 isr = NETISR_IPV6; 306 break; 307 #endif 308 #ifdef IPX 309 case AF_IPX: 310 ifq = &ipxintrq; 311 isr = NETISR_IPX; 312 break; 313 #endif 314 #ifdef NETATALK 315 case AF_APPLETALK: 316 ifq = &atintrq2; 317 isr = NETISR_ATALK; 318 break; 319 #endif 320 default: 321 printf("%s: can't handle af%d\n", ifp->if_xname, 322 dst->sa_family); 323 m_freem(m); 324 return (EAFNOSUPPORT); 325 } 326 s = splnet(); 327 if (IF_QFULL(ifq)) { 328 IF_DROP(ifq); 329 m_freem(m); 330 splx(s); 331 return (ENOBUFS); 332 } 333 IF_ENQUEUE(ifq, m); 334 schednetisr(isr); 335 ifp->if_ipackets++; 336 ifp->if_ibytes += m->m_pkthdr.len; 337 splx(s); 338 return (0); 339 } 340 341 #ifdef ALTQ 342 static void 343 lostart(struct ifnet *ifp) 344 { 345 struct ifqueue *ifq; 346 struct mbuf *m; 347 uint32_t af; 348 int s, isr; 349 350 for (;;) { 351 IFQ_DEQUEUE(&ifp->if_snd, m); 352 if (m == NULL) 353 return; 354 355 af = *(mtod(m, uint32_t *)); 356 m_adj(m, sizeof(uint32_t)); 357 358 switch (af) { 359 #ifdef INET 360 case AF_INET: 361 ifq = &ipintrq; 362 isr = NETISR_IP; 363 break; 364 #endif 365 #ifdef INET6 366 case AF_INET6: 367 m->m_flags |= M_LOOP; 368 ifq = &ip6intrq; 369 isr = NETISR_IPV6; 370 break; 371 #endif 372 #ifdef IPX 373 case AF_IPX: 374 ifq = &ipxintrq; 375 isr = NETISR_IPX; 376 break; 377 #endif 378 #ifdef NETATALK 379 case AF_APPLETALK: 380 ifq = &atintrq2; 381 isr = NETISR_ATALK; 382 break; 383 #endif 384 default: 385 printf("%s: can't handle af%d\n", ifp->if_xname, af); 386 m_freem(m); 387 return; 388 } 389 390 s = splnet(); 391 if (IF_QFULL(ifq)) { 392 IF_DROP(ifq); 393 splx(s); 394 m_freem(m); 395 return; 396 } 397 IF_ENQUEUE(ifq, m); 398 schednetisr(isr); 399 ifp->if_ipackets++; 400 ifp->if_ibytes += m->m_pkthdr.len; 401 splx(s); 402 } 403 } 404 #endif /* ALTQ */ 405 406 /* ARGSUSED */ 407 void 408 lortrequest(int cmd, struct rtentry *rt, 409 const struct rt_addrinfo *info) 410 { 411 412 if (rt) 413 rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu; 414 } 415 416 /* 417 * Process an ioctl request. 418 */ 419 /* ARGSUSED */ 420 int 421 loioctl(struct ifnet *ifp, u_long cmd, void *data) 422 { 423 struct ifaddr *ifa; 424 struct ifreq *ifr = data; 425 int error = 0; 426 427 switch (cmd) { 428 429 case SIOCINITIFADDR: 430 ifp->if_flags |= IFF_UP; 431 ifa = (struct ifaddr *)data; 432 if (ifa != NULL) 433 ifa->ifa_rtrequest = lortrequest; 434 /* 435 * Everything else is done at a higher level. 436 */ 437 break; 438 439 case SIOCSIFMTU: 440 if ((unsigned)ifr->ifr_mtu > LOMTU_MAX) 441 error = EINVAL; 442 else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET){ 443 error = 0; 444 } 445 break; 446 447 case SIOCADDMULTI: 448 case SIOCDELMULTI: 449 if (ifr == NULL) { 450 error = EAFNOSUPPORT; /* XXX */ 451 break; 452 } 453 switch (ifreq_getaddr(cmd, ifr)->sa_family) { 454 455 #ifdef INET 456 case AF_INET: 457 break; 458 #endif 459 #ifdef INET6 460 case AF_INET6: 461 break; 462 #endif 463 464 default: 465 error = EAFNOSUPPORT; 466 break; 467 } 468 break; 469 470 default: 471 error = ifioctl_common(ifp, cmd, data); 472 } 473 return (error); 474 } 475