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