1 /* $OpenBSD: if_loop.c,v 1.29 2003/06/02 23:28:12 millert Exp $ */ 2 /* $NetBSD: if_loop.c,v 1.15 1996/05/07 02:40:33 thorpej Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93 62 */ 63 64 /* 65 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 66 * 67 * NRL grants permission for redistribution and use in source and binary 68 * forms, with or without modification, of the software and documentation 69 * created at NRL provided that the following conditions are met: 70 * 71 * 1. Redistributions of source code must retain the above copyright 72 * notice, this list of conditions and the following disclaimer. 73 * 2. Redistributions in binary form must reproduce the above copyright 74 * notice, this list of conditions and the following disclaimer in the 75 * documentation and/or other materials provided with the distribution. 76 * 3. All advertising materials mentioning features or use of this software 77 * must display the following acknowledgements: 78 * This product includes software developed by the University of 79 * California, Berkeley and its contributors. 80 * This product includes software developed at the Information 81 * Technology Division, US Naval Research Laboratory. 82 * 4. Neither the name of the NRL nor the names of its contributors 83 * may be used to endorse or promote products derived from this software 84 * without specific prior written permission. 85 * 86 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 87 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 88 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 89 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 90 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 91 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 92 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 93 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 94 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 95 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 96 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 97 * 98 * The views and conclusions contained in the software and documentation 99 * are those of the authors and should not be interpreted as representing 100 * official policies, either expressed or implied, of the US Naval 101 * Research Laboratory (NRL). 102 */ 103 104 /* 105 * Loopback interface driver for protocol testing and timing. 106 */ 107 108 #include "bpfilter.h" 109 110 #include <sys/param.h> 111 #include <sys/systm.h> 112 #include <sys/kernel.h> 113 #include <sys/mbuf.h> 114 #include <sys/socket.h> 115 #include <sys/errno.h> 116 #include <sys/ioctl.h> 117 #include <sys/time.h> 118 119 #include <machine/cpu.h> 120 121 #include <net/if.h> 122 #include <net/if_types.h> 123 #include <net/netisr.h> 124 #include <net/route.h> 125 126 #ifdef INET 127 #include <netinet/in.h> 128 #include <netinet/in_systm.h> 129 #include <netinet/in_var.h> 130 #include <netinet/ip.h> 131 #endif 132 133 #ifdef INET6 134 #ifndef INET 135 #include <netinet/in.h> 136 #endif 137 #include <netinet6/in6_var.h> 138 #include <netinet/ip6.h> 139 #endif 140 141 #ifdef NS 142 #include <netns/ns.h> 143 #include <netns/ns_if.h> 144 #endif 145 146 #ifdef IPX 147 #include <netipx/ipx.h> 148 #include <netipx/ipx_if.h> 149 #endif 150 151 #ifdef ISO 152 #include <netiso/iso.h> 153 #include <netiso/iso_var.h> 154 #endif 155 156 #ifdef NETATALK 157 #include <netinet/if_ether.h> 158 #include <netatalk/at.h> 159 #include <netatalk/at_var.h> 160 #endif 161 162 #if NBPFILTER > 0 163 #include <net/bpf.h> 164 #endif 165 166 #if defined(LARGE_LOMTU) 167 #define LOMTU (131072 + MHLEN + MLEN) 168 #else 169 #define LOMTU (32768 + MHLEN + MLEN) 170 #endif 171 172 #ifdef ALTQ 173 static void lo_altqstart(struct ifnet *); 174 #endif 175 176 void 177 loopattach(n) 178 int n; 179 { 180 register int i; 181 register struct ifnet *ifp; 182 183 for (i = n; i--; ) { 184 MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_DEVBUF, M_NOWAIT); 185 if (ifp == NULL) 186 return; 187 bzero(ifp, sizeof(struct ifnet)); 188 if (i == 0) 189 lo0ifp = ifp; 190 snprintf(ifp->if_xname, sizeof ifp->if_xname, "lo%d", i); 191 ifp->if_softc = NULL; 192 ifp->if_mtu = LOMTU; 193 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 194 ifp->if_ioctl = loioctl; 195 ifp->if_output = looutput; 196 ifp->if_type = IFT_LOOP; 197 ifp->if_hdrlen = sizeof(u_int32_t); 198 ifp->if_addrlen = 0; 199 IFQ_SET_READY(&ifp->if_snd); 200 #ifdef ALTQ 201 ifp->if_start = lo_altqstart; 202 #endif 203 if_attachhead(ifp); 204 if_alloc_sadl(ifp); 205 #if NBPFILTER > 0 206 bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t)); 207 #endif 208 } 209 } 210 211 int 212 looutput(ifp, m, dst, rt) 213 struct ifnet *ifp; 214 register struct mbuf *m; 215 struct sockaddr *dst; 216 register struct rtentry *rt; 217 { 218 int s, isr; 219 register struct ifqueue *ifq = 0; 220 221 if ((m->m_flags & M_PKTHDR) == 0) 222 panic("looutput: no header mbuf"); 223 #if NBPFILTER > 0 224 /* 225 * only send packets to bpf if they are real loopback packets; 226 * looutput() is also called for SIMPLEX interfaces to duplicate 227 * packets for local use. But don't dup them to bpf. 228 */ 229 if (ifp->if_bpf && (ifp->if_flags&IFF_LOOPBACK)) { 230 /* 231 * We need to prepend the address family as 232 * a four byte field. Cons up a dummy header 233 * to pacify bpf. This is safe because bpf 234 * will only read from the mbuf (i.e., it won't 235 * try to free it or keep a pointer to it). 236 */ 237 struct mbuf m0; 238 u_int32_t af = htonl(dst->sa_family); 239 240 m0.m_flags = 0; 241 m0.m_next = m; 242 m0.m_len = sizeof(af); 243 m0.m_data = (char *)⁡ 244 245 bpf_mtap(ifp->if_bpf, &m0); 246 } 247 #endif 248 m->m_pkthdr.rcvif = ifp; 249 250 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 251 m_freem(m); 252 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 253 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 254 } 255 256 ifp->if_opackets++; 257 ifp->if_obytes += m->m_pkthdr.len; 258 #ifdef ALTQ 259 /* 260 * altq for loop is just for debugging. 261 * only used when called for loop interface (not for 262 * a simplex interface). 263 */ 264 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) 265 && ifp->if_start == lo_altqstart) { 266 int32_t *afp; 267 int error; 268 269 M_PREPEND(m, sizeof(int32_t), M_DONTWAIT); 270 if (m == 0) 271 return (ENOBUFS); 272 afp = mtod(m, int32_t *); 273 *afp = (int32_t)dst->sa_family; 274 275 s = splimp(); 276 IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error); 277 (*ifp->if_start)(ifp); 278 splx(s); 279 return (error); 280 } 281 #endif /* ALTQ */ 282 switch (dst->sa_family) { 283 284 #ifdef INET 285 case AF_INET: 286 ifq = &ipintrq; 287 isr = NETISR_IP; 288 break; 289 #endif 290 #ifdef INET6 291 case AF_INET6: 292 ifq = &ip6intrq; 293 isr = NETISR_IPV6; 294 break; 295 #endif /* INET6 */ 296 #ifdef NS 297 case AF_NS: 298 ifq = &nsintrq; 299 isr = NETISR_NS; 300 break; 301 #endif 302 #ifdef IPX 303 case AF_IPX: 304 ifq = &ipxintrq; 305 isr = NETISR_IPX; 306 break; 307 #endif 308 #ifdef ISO 309 case AF_ISO: 310 ifq = &clnlintrq; 311 isr = NETISR_ISO; 312 break; 313 #endif 314 #ifdef NETATALK 315 case AF_APPLETALK: 316 ifq = &atintrq2; 317 isr = NETISR_ATALK; 318 break; 319 #endif /* NETATALK */ 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 = splimp(); 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 lo_altqstart(ifp) 344 struct ifnet *ifp; 345 { 346 struct ifqueue *ifq; 347 struct mbuf *m; 348 int32_t af, *afp; 349 int s, isr; 350 351 while (1) { 352 s = splimp(); 353 IFQ_DEQUEUE(&ifp->if_snd, m); 354 splx(s); 355 if (m == NULL) 356 return; 357 358 afp = mtod(m, int32_t *); 359 af = *afp; 360 m_adj(m, sizeof(int32_t)); 361 362 switch (af) { 363 #ifdef INET 364 case AF_INET: 365 ifq = &ipintrq; 366 isr = NETISR_IP; 367 break; 368 #endif 369 #ifdef INET6 370 case AF_INET6: 371 m->m_flags |= M_LOOP; 372 ifq = &ip6intrq; 373 isr = NETISR_IPV6; 374 break; 375 #endif 376 #ifdef IPX 377 case AF_IPX: 378 ifq = &ipxintrq; 379 isr = NETISR_IPX; 380 break; 381 #endif 382 #ifdef NS 383 case AF_NS: 384 ifq = &nsintrq; 385 isr = NETISR_NS; 386 break; 387 #endif 388 #ifdef ISO 389 case AF_ISO: 390 ifq = &clnlintrq; 391 isr = NETISR_ISO; 392 break; 393 #endif 394 #ifdef NETATALK 395 case AF_APPLETALK: 396 ifq = &atintrq2; 397 isr = NETISR_ATALK; 398 break; 399 #endif /* NETATALK */ 400 default: 401 printf("lo_altqstart: can't handle af%d\n", af); 402 m_freem(m); 403 return; 404 } 405 406 s = splimp(); 407 if (IF_QFULL(ifq)) { 408 IF_DROP(ifq); 409 m_freem(m); 410 splx(s); 411 return; 412 } 413 IF_ENQUEUE(ifq, m); 414 schednetisr(isr); 415 ifp->if_ipackets++; 416 ifp->if_ibytes += m->m_pkthdr.len; 417 splx(s); 418 } 419 } 420 #endif /* ALTQ */ 421 422 /* ARGSUSED */ 423 void 424 lortrequest(cmd, rt, info) 425 int cmd; 426 struct rtentry *rt; 427 struct rt_addrinfo *info; 428 { 429 430 if (rt) 431 rt->rt_rmx.rmx_mtu = LOMTU; 432 } 433 434 /* 435 * Process an ioctl request. 436 */ 437 /* ARGSUSED */ 438 int 439 loioctl(ifp, cmd, data) 440 register struct ifnet *ifp; 441 u_long cmd; 442 caddr_t data; 443 { 444 register struct ifaddr *ifa; 445 register struct ifreq *ifr; 446 register int error = 0; 447 448 switch (cmd) { 449 450 case SIOCSIFADDR: 451 ifp->if_flags |= IFF_UP | IFF_RUNNING; 452 ifa = (struct ifaddr *)data; 453 if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/) 454 ifa->ifa_rtrequest = lortrequest; 455 /* 456 * Everything else is done at a higher level. 457 */ 458 break; 459 460 case SIOCADDMULTI: 461 case SIOCDELMULTI: 462 ifr = (struct ifreq *)data; 463 if (ifr == 0) { 464 error = EAFNOSUPPORT; /* XXX */ 465 break; 466 } 467 switch (ifr->ifr_addr.sa_family) { 468 469 #ifdef INET 470 case AF_INET: 471 break; 472 #endif 473 #ifdef INET6 474 case AF_INET6: 475 break; 476 #endif /* INET6 */ 477 478 default: 479 error = EAFNOSUPPORT; 480 break; 481 } 482 break; 483 484 case SIOCSIFMTU: 485 ifr = (struct ifreq *)data; 486 ifp->if_mtu = ifr->ifr_mtu; 487 break; 488 489 default: 490 error = EINVAL; 491 } 492 return (error); 493 } 494