1 /* $NetBSD: if_loop.c,v 1.47 2003/08/07 16:32:53 agc 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.47 2003/08/07 16:32:53 agc Exp $"); 69 70 #include "opt_inet.h" 71 #include "opt_atalk.h" 72 #include "opt_iso.h" 73 #include "opt_ns.h" 74 #include "opt_ipx.h" 75 #include "opt_mbuftrace.h" 76 77 #include "bpfilter.h" 78 #include "loop.h" 79 80 #include <sys/param.h> 81 #include <sys/systm.h> 82 #include <sys/kernel.h> 83 #include <sys/mbuf.h> 84 #include <sys/socket.h> 85 #include <sys/errno.h> 86 #include <sys/ioctl.h> 87 #include <sys/time.h> 88 89 #include <machine/cpu.h> 90 91 #include <net/if.h> 92 #include <net/if_types.h> 93 #include <net/netisr.h> 94 #include <net/route.h> 95 96 #ifdef INET 97 #include <netinet/in.h> 98 #include <netinet/in_systm.h> 99 #include <netinet/in_var.h> 100 #include <netinet/ip.h> 101 #endif 102 103 #ifdef INET6 104 #ifndef INET 105 #include <netinet/in.h> 106 #endif 107 #include <netinet6/in6_var.h> 108 #include <netinet/ip6.h> 109 #endif 110 111 #ifdef NS 112 #include <netns/ns.h> 113 #include <netns/ns_if.h> 114 #endif 115 116 #ifdef IPX 117 #include <netipx/ipx.h> 118 #include <netipx/ipx_if.h> 119 #endif 120 121 #ifdef ISO 122 #include <netiso/iso.h> 123 #include <netiso/iso_var.h> 124 #endif 125 126 #ifdef NETATALK 127 #include <netatalk/at.h> 128 #include <netatalk/at_var.h> 129 #endif 130 131 #if NBPFILTER > 0 132 #include <net/bpf.h> 133 #endif 134 135 #if defined(LARGE_LOMTU) 136 #define LOMTU (131072 + MHLEN + MLEN) 137 #else 138 #define LOMTU (32768 + MHLEN + MLEN) 139 #endif 140 141 struct ifnet loif[NLOOP]; 142 #ifdef MBUFTRACE 143 struct mowner lomowner[NLOOP]; 144 #endif 145 146 #ifdef ALTQ 147 void lostart(struct ifnet *); 148 #endif 149 150 void 151 loopattach(n) 152 int n; 153 { 154 int i; 155 struct ifnet *ifp; 156 157 for (i = 0; i < NLOOP; i++) { 158 ifp = &loif[i]; 159 sprintf(ifp->if_xname, "lo%d", i); 160 ifp->if_softc = NULL; 161 ifp->if_mtu = LOMTU; 162 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 163 ifp->if_ioctl = loioctl; 164 ifp->if_output = looutput; 165 #ifdef ALTQ 166 ifp->if_start = lostart; 167 #endif 168 ifp->if_type = IFT_LOOP; 169 ifp->if_hdrlen = 0; 170 ifp->if_addrlen = 0; 171 ifp->if_dlt = DLT_NULL; 172 IFQ_SET_READY(&ifp->if_snd); 173 if_attach(ifp); 174 if_alloc_sadl(ifp); 175 #if NBPFILTER > 0 176 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 177 #endif 178 #ifdef MBUFTRACE 179 ifp->if_mowner = &lomowner[i]; 180 strlcpy(ifp->if_mowner->mo_name, ifp->if_xname, 181 sizeof(ifp->if_mowner->mo_name)); 182 MOWNER_ATTACH(&lomowner[i]); 183 #endif 184 } 185 } 186 187 int 188 looutput(ifp, m, dst, rt) 189 struct ifnet *ifp; 190 struct mbuf *m; 191 struct sockaddr *dst; 192 struct rtentry *rt; 193 { 194 int s, isr; 195 struct ifqueue *ifq = 0; 196 197 MCLAIM(m, ifp->if_mowner); 198 if ((m->m_flags & M_PKTHDR) == 0) 199 panic("looutput: no header mbuf"); 200 #if NBPFILTER > 0 201 if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) { 202 /* 203 * We need to prepend the address family as 204 * a four byte field. Cons up a dummy header 205 * to pacify bpf. This is safe because bpf 206 * will only read from the mbuf (i.e., it won't 207 * try to free it or keep a pointer to it). 208 */ 209 struct mbuf m0; 210 u_int32_t af = dst->sa_family; 211 212 m0.m_flags = 0; 213 m0.m_next = m; 214 m0.m_len = 4; 215 m0.m_data = (char *)⁡ 216 217 bpf_mtap(ifp->if_bpf, &m0); 218 } 219 #endif 220 m->m_pkthdr.rcvif = ifp; 221 222 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 223 m_freem(m); 224 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 225 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 226 } 227 228 ifp->if_opackets++; 229 ifp->if_obytes += m->m_pkthdr.len; 230 231 #ifdef ALTQ 232 /* 233 * ALTQ on the loopback interface is just for debugging. It's 234 * used only for loopback interfaces, not for a simplex interface. 235 */ 236 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) && 237 ifp->if_start == lostart) { 238 struct altq_pktattr pktattr; 239 int error; 240 241 /* 242 * If the queueing discipline needs packet classification, 243 * do it before prepending the link headers. 244 */ 245 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr); 246 247 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT); 248 if (m == NULL) 249 return (ENOBUFS); 250 *(mtod(m, uint32_t *)) = dst->sa_family; 251 252 s = splnet(); 253 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error); 254 (*ifp->if_start)(ifp); 255 splx(s); 256 return (error); 257 } 258 #endif /* ALTQ */ 259 260 switch (dst->sa_family) { 261 262 #ifdef INET 263 case AF_INET: 264 ifq = &ipintrq; 265 isr = NETISR_IP; 266 break; 267 #endif 268 #ifdef INET6 269 case AF_INET6: 270 m->m_flags |= M_LOOP; 271 ifq = &ip6intrq; 272 isr = NETISR_IPV6; 273 break; 274 #endif 275 #ifdef NS 276 case AF_NS: 277 ifq = &nsintrq; 278 isr = NETISR_NS; 279 break; 280 #endif 281 #ifdef ISO 282 case AF_ISO: 283 ifq = &clnlintrq; 284 isr = NETISR_ISO; 285 break; 286 #endif 287 #ifdef IPX 288 case AF_IPX: 289 ifq = &ipxintrq; 290 isr = NETISR_IPX; 291 break; 292 #endif 293 #ifdef NETATALK 294 case AF_APPLETALK: 295 ifq = &atintrq2; 296 isr = NETISR_ATALK; 297 break; 298 #endif 299 default: 300 printf("%s: can't handle af%d\n", ifp->if_xname, 301 dst->sa_family); 302 m_freem(m); 303 return (EAFNOSUPPORT); 304 } 305 s = splnet(); 306 if (IF_QFULL(ifq)) { 307 IF_DROP(ifq); 308 m_freem(m); 309 splx(s); 310 return (ENOBUFS); 311 } 312 IF_ENQUEUE(ifq, m); 313 schednetisr(isr); 314 ifp->if_ipackets++; 315 ifp->if_ibytes += m->m_pkthdr.len; 316 splx(s); 317 return (0); 318 } 319 320 #ifdef ALTQ 321 void 322 lostart(struct ifnet *ifp) 323 { 324 struct ifqueue *ifq; 325 struct mbuf *m; 326 uint32_t af; 327 int s, isr; 328 329 for (;;) { 330 IFQ_DEQUEUE(&ifp->if_snd, m); 331 if (m == NULL) 332 return; 333 334 af = *(mtod(m, uint32_t *)); 335 m_adj(m, sizeof(uint32_t)); 336 337 switch (af) { 338 #ifdef INET 339 case AF_INET: 340 ifq = &ipintrq; 341 isr = NETISR_IP; 342 break; 343 #endif 344 #ifdef INET6 345 case AF_INET6: 346 m->m_flags |= M_LOOP; 347 ifq = &ip6intrq; 348 isr = NETISR_IPV6; 349 break; 350 #endif 351 #ifdef IPX 352 case AF_IPX: 353 ifq = &ipxintrq; 354 isr = NETISR_IPX; 355 break; 356 #endif 357 #ifdef NS 358 case AF_NS: 359 ifq = &nsintrq; 360 isr = NETISR_NS; 361 break; 362 #endif 363 #ifdef ISO 364 case AF_ISO: 365 ifq = &clnlintrq; 366 isr = NETISR_ISO; 367 break; 368 #endif 369 #ifdef NETATALK 370 case AF_APPLETALK: 371 ifq = &atintrq2; 372 isr = NETISR_ATALK; 373 break; 374 #endif 375 default: 376 printf("%s: can't handle af%d\n", ifp->if_xname, af); 377 m_freem(m); 378 return; 379 } 380 381 s = splnet(); 382 if (IF_QFULL(ifq)) { 383 IF_DROP(ifq); 384 splx(s); 385 m_freem(m); 386 return; 387 } 388 IF_ENQUEUE(ifq, m); 389 schednetisr(isr); 390 ifp->if_ipackets++; 391 ifp->if_ibytes += m->m_pkthdr.len; 392 splx(s); 393 } 394 } 395 #endif /* ALTQ */ 396 397 /* ARGSUSED */ 398 void 399 lortrequest(cmd, rt, info) 400 int cmd; 401 struct rtentry *rt; 402 struct rt_addrinfo *info; 403 { 404 405 if (rt) 406 rt->rt_rmx.rmx_mtu = LOMTU; 407 } 408 409 /* 410 * Process an ioctl request. 411 */ 412 /* ARGSUSED */ 413 int 414 loioctl(ifp, cmd, data) 415 struct ifnet *ifp; 416 u_long cmd; 417 caddr_t data; 418 { 419 struct ifaddr *ifa; 420 struct ifreq *ifr; 421 int error = 0; 422 423 switch (cmd) { 424 425 case SIOCSIFADDR: 426 ifp->if_flags |= IFF_UP; 427 ifa = (struct ifaddr *)data; 428 if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/) 429 ifa->ifa_rtrequest = lortrequest; 430 /* 431 * Everything else is done at a higher level. 432 */ 433 break; 434 435 case SIOCADDMULTI: 436 case SIOCDELMULTI: 437 ifr = (struct ifreq *)data; 438 if (ifr == 0) { 439 error = EAFNOSUPPORT; /* XXX */ 440 break; 441 } 442 switch (ifr->ifr_addr.sa_family) { 443 444 #ifdef INET 445 case AF_INET: 446 break; 447 #endif 448 #ifdef INET6 449 case AF_INET6: 450 break; 451 #endif 452 453 default: 454 error = EAFNOSUPPORT; 455 break; 456 } 457 break; 458 459 default: 460 error = EINVAL; 461 } 462 return (error); 463 } 464