1 /* $NetBSD: if_loop.c,v 1.38 2001/04/13 23:30:14 thorpej 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. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by the University of 47 * California, Berkeley and its contributors. 48 * 4. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 * 64 * @(#)if_loop.c 8.2 (Berkeley) 1/9/95 65 */ 66 67 /* 68 * Loopback interface driver for protocol testing and timing. 69 */ 70 71 #include "opt_inet.h" 72 #include "opt_atalk.h" 73 #include "opt_iso.h" 74 #include "opt_ns.h" 75 76 #include "bpfilter.h" 77 #include "loop.h" 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/kernel.h> 82 #include <sys/mbuf.h> 83 #include <sys/socket.h> 84 #include <sys/errno.h> 85 #include <sys/ioctl.h> 86 #include <sys/time.h> 87 88 #include <machine/cpu.h> 89 90 #include <net/if.h> 91 #include <net/if_types.h> 92 #include <net/netisr.h> 93 #include <net/route.h> 94 95 #ifdef INET 96 #include <netinet/in.h> 97 #include <netinet/in_systm.h> 98 #include <netinet/in_var.h> 99 #include <netinet/ip.h> 100 #endif 101 102 #ifdef INET6 103 #ifndef INET 104 #include <netinet/in.h> 105 #endif 106 #include <netinet6/in6_var.h> 107 #include <netinet/ip6.h> 108 #endif 109 110 #ifdef NS 111 #include <netns/ns.h> 112 #include <netns/ns_if.h> 113 #endif 114 115 #ifdef IPX 116 #include <netipx/ipx.h> 117 #include <netipx/ipx_if.h> 118 #endif 119 120 #ifdef ISO 121 #include <netiso/iso.h> 122 #include <netiso/iso_var.h> 123 #endif 124 125 #ifdef NETATALK 126 #include <netatalk/at.h> 127 #include <netatalk/at_var.h> 128 #endif 129 130 #if NBPFILTER > 0 131 #include <net/bpf.h> 132 #endif 133 134 #if defined(LARGE_LOMTU) 135 #define LOMTU (131072 + MHLEN + MLEN) 136 #else 137 #define LOMTU (32768 + MHLEN + MLEN) 138 #endif 139 140 struct ifnet loif[NLOOP]; 141 142 #ifdef ALTQ 143 void lostart(struct ifnet *); 144 #endif 145 146 void 147 loopattach(n) 148 int n; 149 { 150 int i; 151 struct ifnet *ifp; 152 153 for (i = 0; i < NLOOP; i++) { 154 ifp = &loif[i]; 155 sprintf(ifp->if_xname, "lo%d", i); 156 ifp->if_softc = NULL; 157 ifp->if_mtu = LOMTU; 158 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 159 ifp->if_ioctl = loioctl; 160 ifp->if_output = looutput; 161 #ifdef ALTQ 162 ifp->if_start = lostart; 163 #endif 164 ifp->if_type = IFT_LOOP; 165 ifp->if_hdrlen = 0; 166 ifp->if_addrlen = 0; 167 ifp->if_dlt = DLT_NULL; 168 IFQ_SET_READY(&ifp->if_snd); 169 if_attach(ifp); 170 if_alloc_sadl(ifp); 171 #if NBPFILTER > 0 172 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 173 #endif 174 } 175 } 176 177 int 178 looutput(ifp, m, dst, rt) 179 struct ifnet *ifp; 180 struct mbuf *m; 181 struct sockaddr *dst; 182 struct rtentry *rt; 183 { 184 int s, isr; 185 struct ifqueue *ifq = 0; 186 187 if ((m->m_flags & M_PKTHDR) == 0) 188 panic("looutput: no header mbuf"); 189 ifp->if_lastchange = time; 190 #if NBPFILTER > 0 191 if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) { 192 /* 193 * We need to prepend the address family as 194 * a four byte field. Cons up a dummy header 195 * to pacify bpf. This is safe because bpf 196 * will only read from the mbuf (i.e., it won't 197 * try to free it or keep a pointer to it). 198 */ 199 struct mbuf m0; 200 u_int32_t af = dst->sa_family; 201 202 m0.m_next = m; 203 m0.m_len = 4; 204 m0.m_data = (char *)⁡ 205 206 bpf_mtap(ifp->if_bpf, &m0); 207 } 208 #endif 209 m->m_pkthdr.rcvif = ifp; 210 211 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 212 m_freem(m); 213 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 214 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 215 } 216 217 #ifndef PULLDOWN_TEST 218 /* 219 * KAME requires that the packet to be contiguous on the 220 * mbuf. We need to make that sure. 221 * this kind of code should be avoided. 222 * XXX other conditions to avoid running this part? 223 */ 224 if (m->m_len != m->m_pkthdr.len) { 225 struct mbuf *n = NULL; 226 int maxlen; 227 228 MGETHDR(n, M_DONTWAIT, MT_HEADER); 229 maxlen = MHLEN; 230 if (n) 231 M_COPY_PKTHDR(n, m); 232 if (n && m->m_pkthdr.len > maxlen) { 233 MCLGET(n, M_DONTWAIT); 234 maxlen = MCLBYTES; 235 if ((n->m_flags & M_EXT) == 0) { 236 m_free(n); 237 n = NULL; 238 } 239 } 240 if (!n) { 241 printf("looutput: mbuf allocation failed\n"); 242 m_freem(m); 243 return ENOBUFS; 244 } 245 246 if (m->m_pkthdr.len <= maxlen) { 247 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t)); 248 n->m_len = m->m_pkthdr.len; 249 n->m_next = NULL; 250 m_freem(m); 251 } else { 252 m_copydata(m, 0, maxlen, mtod(n, caddr_t)); 253 m_adj(m, maxlen); 254 n->m_len = maxlen; 255 n->m_next = m; 256 m->m_flags &= ~M_PKTHDR; 257 } 258 m = n; 259 } 260 #if 0 261 if (m && m->m_next != NULL) { 262 printf("loop: not contiguous...\n"); 263 m_freem(m); 264 return ENOBUFS; 265 } 266 #endif 267 #endif 268 269 ifp->if_opackets++; 270 ifp->if_obytes += m->m_pkthdr.len; 271 272 #ifdef ALTQ 273 /* 274 * ALTQ on the loopback interface is just for debugging. It's 275 * used only for loopback interfaces, not for a simplex interface. 276 */ 277 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) && 278 ifp->if_start == lostart) { 279 struct altq_pktattr pktattr; 280 int error; 281 282 /* 283 * If the queueing discipline needs packet classification, 284 * do it before prepending the link headers. 285 */ 286 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr); 287 288 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT); 289 if (m == NULL) 290 return (ENOBUFS); 291 *(mtod(m, uint32_t *)) = dst->sa_family; 292 293 s = splnet(); 294 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error); 295 (*ifp->if_start)(ifp); 296 splx(s); 297 return (error); 298 } 299 #endif /* ALTQ */ 300 301 switch (dst->sa_family) { 302 303 #ifdef INET 304 case AF_INET: 305 ifq = &ipintrq; 306 isr = NETISR_IP; 307 break; 308 #endif 309 #ifdef INET6 310 case AF_INET6: 311 m->m_flags |= M_LOOP; 312 ifq = &ip6intrq; 313 isr = NETISR_IPV6; 314 break; 315 #endif 316 #ifdef NS 317 case AF_NS: 318 ifq = &nsintrq; 319 isr = NETISR_NS; 320 break; 321 #endif 322 #ifdef ISO 323 case AF_ISO: 324 ifq = &clnlintrq; 325 isr = NETISR_ISO; 326 break; 327 #endif 328 #ifdef IPX 329 case AF_IPX: 330 ifq = &ipxintrq; 331 isr = NETISR_IPX; 332 break; 333 #endif 334 #ifdef NETATALK 335 case AF_APPLETALK: 336 ifq = &atintrq2; 337 isr = NETISR_ATALK; 338 break; 339 #endif 340 default: 341 printf("%s: can't handle af%d\n", ifp->if_xname, 342 dst->sa_family); 343 m_freem(m); 344 return (EAFNOSUPPORT); 345 } 346 s = splnet(); 347 if (IF_QFULL(ifq)) { 348 IF_DROP(ifq); 349 m_freem(m); 350 splx(s); 351 return (ENOBUFS); 352 } 353 IF_ENQUEUE(ifq, m); 354 schednetisr(isr); 355 ifp->if_ipackets++; 356 ifp->if_ibytes += m->m_pkthdr.len; 357 splx(s); 358 return (0); 359 } 360 361 #ifdef ALTQ 362 void 363 lostart(struct ifnet *ifp) 364 { 365 struct ifqueue *ifq; 366 struct mbuf *m; 367 uint32_t af; 368 int s, isr; 369 370 for (;;) { 371 IFQ_DEQUEUE(&ifp->if_snd, m); 372 if (m == NULL) 373 return; 374 375 af = *(mtod(m, uint32_t *)); 376 m_adj(m, sizeof(uint32_t)); 377 378 switch (af) { 379 #ifdef INET 380 case AF_INET: 381 ifq = &ipintrq; 382 isr = NETISR_IP; 383 break; 384 #endif 385 #ifdef INET6 386 case AF_INET6: 387 m->m_flags |= M_LOOP; 388 ifq = &ip6intrq; 389 isr = NETISR_IPV6; 390 break; 391 #endif 392 #ifdef IPX 393 case AF_IPX: 394 ifq = &ipxintrq; 395 isr = NETISR_IPX; 396 break; 397 #endif 398 #ifdef NS 399 case AF_NS: 400 ifq = &nsintrq; 401 isr = NETISR_NS; 402 break; 403 #endif 404 #ifdef ISO 405 case AF_ISO: 406 ifq = &clnlintrq; 407 isr = NETISR_ISO; 408 break; 409 #endif 410 #ifdef NETATALK 411 case AF_APPLETALK: 412 ifq = &atintrq2; 413 isr = NETISR_ATALK; 414 break; 415 #endif 416 default: 417 printf("%s: can't handle af%d\n", ifp->if_xname, af); 418 m_freem(m); 419 return; 420 } 421 422 s = splnet(); 423 if (IF_QFULL(ifq)) { 424 IF_DROP(ifq); 425 splx(s); 426 m_freem(m); 427 return; 428 } 429 IF_ENQUEUE(ifq, m); 430 schednetisr(isr); 431 ifp->if_ipackets++; 432 ifp->if_ibytes += m->m_pkthdr.len; 433 splx(s); 434 } 435 } 436 #endif /* ALTQ */ 437 438 /* ARGSUSED */ 439 void 440 lortrequest(cmd, rt, info) 441 int cmd; 442 struct rtentry *rt; 443 struct rt_addrinfo *info; 444 { 445 446 if (rt) 447 rt->rt_rmx.rmx_mtu = LOMTU; 448 } 449 450 /* 451 * Process an ioctl request. 452 */ 453 /* ARGSUSED */ 454 int 455 loioctl(ifp, cmd, data) 456 struct ifnet *ifp; 457 u_long cmd; 458 caddr_t data; 459 { 460 struct ifaddr *ifa; 461 struct ifreq *ifr; 462 int error = 0; 463 464 switch (cmd) { 465 466 case SIOCSIFADDR: 467 ifp->if_flags |= IFF_UP; 468 ifa = (struct ifaddr *)data; 469 if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/) 470 ifa->ifa_rtrequest = lortrequest; 471 /* 472 * Everything else is done at a higher level. 473 */ 474 break; 475 476 case SIOCADDMULTI: 477 case SIOCDELMULTI: 478 ifr = (struct ifreq *)data; 479 if (ifr == 0) { 480 error = EAFNOSUPPORT; /* XXX */ 481 break; 482 } 483 switch (ifr->ifr_addr.sa_family) { 484 485 #ifdef INET 486 case AF_INET: 487 break; 488 #endif 489 #ifdef INET6 490 case AF_INET6: 491 break; 492 #endif 493 494 default: 495 error = EAFNOSUPPORT; 496 break; 497 } 498 break; 499 500 default: 501 error = EINVAL; 502 } 503 return (error); 504 } 505