1 /* $OpenBSD: if_loop.c,v 1.44 2008/05/07 12:58:54 norby 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 NETATALK 142 #include <netinet/if_ether.h> 143 #include <netatalk/at.h> 144 #include <netatalk/at_var.h> 145 #endif 146 147 #ifdef MPLS 148 #include <netmpls/mpls.h> 149 #endif 150 151 #if NBPFILTER > 0 152 #include <net/bpf.h> 153 #endif 154 155 #if defined(LARGE_LOMTU) 156 #define LOMTU (131072 + MHLEN + MLEN) 157 #else 158 #define LOMTU (32768 + MHLEN + MLEN) 159 #endif 160 161 #ifdef ALTQ 162 static void lo_altqstart(struct ifnet *); 163 #endif 164 165 int loop_clone_create(struct if_clone *, int); 166 int loop_clone_destroy(struct ifnet *); 167 168 struct if_clone loop_cloner = 169 IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy); 170 171 /* ARGSUSED */ 172 void 173 loopattach(n) 174 int n; 175 { 176 (void) loop_clone_create(&loop_cloner, 0); 177 if_clone_attach(&loop_cloner); 178 } 179 180 int 181 loop_clone_create(ifc, unit) 182 struct if_clone *ifc; 183 int unit; 184 { 185 struct ifnet *ifp; 186 187 ifp = malloc(sizeof(*ifp), M_DEVBUF, M_NOWAIT|M_ZERO); 188 if (ifp == NULL) 189 return (ENOMEM); 190 191 snprintf(ifp->if_xname, sizeof ifp->if_xname, "lo%d", unit); 192 ifp->if_softc = NULL; 193 ifp->if_mtu = LOMTU; 194 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 195 ifp->if_ioctl = loioctl; 196 ifp->if_output = looutput; 197 ifp->if_type = IFT_LOOP; 198 ifp->if_hdrlen = sizeof(u_int32_t); 199 ifp->if_addrlen = 0; 200 IFQ_SET_READY(&ifp->if_snd); 201 #ifdef ALTQ 202 ifp->if_start = lo_altqstart; 203 #endif 204 if (unit == 0) { 205 lo0ifp = ifp; 206 if_attachhead(ifp); 207 if_addgroup(lo0ifp, ifc->ifc_name); 208 } else 209 if_attach(ifp); 210 if_alloc_sadl(ifp); 211 #if NBPFILTER > 0 212 bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t)); 213 #endif 214 return (0); 215 } 216 217 int 218 loop_clone_destroy(ifp) 219 struct ifnet *ifp; 220 { 221 if (ifp == lo0ifp) 222 return (EPERM); 223 224 if_detach(ifp); 225 226 free(ifp, M_DEVBUF); 227 return (0); 228 } 229 230 int 231 looutput(ifp, m, dst, rt) 232 struct ifnet *ifp; 233 struct mbuf *m; 234 struct sockaddr *dst; 235 struct rtentry *rt; 236 { 237 int s, isr; 238 struct ifqueue *ifq = 0; 239 240 if ((m->m_flags & M_PKTHDR) == 0) 241 panic("looutput: no header mbuf"); 242 #if NBPFILTER > 0 243 /* 244 * only send packets to bpf if they are real loopback packets; 245 * looutput() is also called for SIMPLEX interfaces to duplicate 246 * packets for local use. But don't dup them to bpf. 247 */ 248 if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) 249 bpf_mtap_af(ifp->if_bpf, htonl(dst->sa_family), m, 250 BPF_DIRECTION_OUT); 251 #endif 252 m->m_pkthdr.rcvif = ifp; 253 254 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 255 m_freem(m); 256 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 257 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 258 } 259 260 ifp->if_opackets++; 261 ifp->if_obytes += m->m_pkthdr.len; 262 #ifdef ALTQ 263 /* 264 * altq for loop is just for debugging. 265 * only used when called for loop interface (not for 266 * a simplex interface). 267 */ 268 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) 269 && ifp->if_start == lo_altqstart) { 270 int32_t *afp; 271 int error; 272 273 M_PREPEND(m, sizeof(int32_t), M_DONTWAIT); 274 if (m == 0) 275 return (ENOBUFS); 276 afp = mtod(m, int32_t *); 277 *afp = (int32_t)dst->sa_family; 278 279 s = splnet(); 280 IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error); 281 (*ifp->if_start)(ifp); 282 splx(s); 283 return (error); 284 } 285 #endif /* ALTQ */ 286 switch (dst->sa_family) { 287 288 #ifdef INET 289 case AF_INET: 290 ifq = &ipintrq; 291 isr = NETISR_IP; 292 break; 293 #endif 294 #ifdef INET6 295 case AF_INET6: 296 ifq = &ip6intrq; 297 isr = NETISR_IPV6; 298 break; 299 #endif /* INET6 */ 300 #ifdef NETATALK 301 case AF_APPLETALK: 302 ifq = &atintrq2; 303 isr = NETISR_ATALK; 304 break; 305 #endif /* NETATALK */ 306 #ifdef MPLS 307 case AF_MPLS: 308 ifq = &mplsintrq; 309 isr = NETISR_MPLS; 310 break; 311 #endif /* MPLS */ 312 default: 313 printf("%s: can't handle af%d\n", ifp->if_xname, 314 dst->sa_family); 315 m_freem(m); 316 return (EAFNOSUPPORT); 317 } 318 s = splnet(); 319 if (IF_QFULL(ifq)) { 320 IF_DROP(ifq); 321 m_freem(m); 322 splx(s); 323 return (ENOBUFS); 324 } 325 IF_ENQUEUE(ifq, m); 326 schednetisr(isr); 327 ifp->if_ipackets++; 328 ifp->if_ibytes += m->m_pkthdr.len; 329 splx(s); 330 return (0); 331 } 332 333 #ifdef ALTQ 334 static void 335 lo_altqstart(ifp) 336 struct ifnet *ifp; 337 { 338 struct ifqueue *ifq; 339 struct mbuf *m; 340 int32_t af, *afp; 341 int s, isr; 342 343 while (1) { 344 s = splnet(); 345 IFQ_DEQUEUE(&ifp->if_snd, m); 346 splx(s); 347 if (m == NULL) 348 return; 349 350 afp = mtod(m, int32_t *); 351 af = *afp; 352 m_adj(m, sizeof(int32_t)); 353 354 switch (af) { 355 #ifdef INET 356 case AF_INET: 357 ifq = &ipintrq; 358 isr = NETISR_IP; 359 break; 360 #endif 361 #ifdef INET6 362 case AF_INET6: 363 m->m_flags |= M_LOOP; 364 ifq = &ip6intrq; 365 isr = NETISR_IPV6; 366 break; 367 #endif 368 #ifdef MPLS 369 case AF_MPLS: 370 ifq = &mplsintrq; 371 isr = NETISR_MPLS; 372 break; 373 #endif 374 #ifdef NETATALK 375 case AF_APPLETALK: 376 ifq = &atintrq2; 377 isr = NETISR_ATALK; 378 break; 379 #endif /* NETATALK */ 380 default: 381 printf("lo_altqstart: can't handle af%d\n", af); 382 m_freem(m); 383 return; 384 } 385 386 s = splnet(); 387 if (IF_QFULL(ifq)) { 388 IF_DROP(ifq); 389 m_freem(m); 390 splx(s); 391 return; 392 } 393 IF_ENQUEUE(ifq, m); 394 schednetisr(isr); 395 ifp->if_ipackets++; 396 ifp->if_ibytes += m->m_pkthdr.len; 397 splx(s); 398 } 399 } 400 #endif /* ALTQ */ 401 402 /* ARGSUSED */ 403 void 404 lortrequest(cmd, rt, info) 405 int cmd; 406 struct rtentry *rt; 407 struct rt_addrinfo *info; 408 { 409 410 if (rt) 411 rt->rt_rmx.rmx_mtu = LOMTU; 412 } 413 414 /* 415 * Process an ioctl request. 416 */ 417 /* ARGSUSED */ 418 int 419 loioctl(ifp, cmd, data) 420 struct ifnet *ifp; 421 u_long cmd; 422 caddr_t data; 423 { 424 struct ifaddr *ifa; 425 struct ifreq *ifr; 426 int error = 0; 427 428 switch (cmd) { 429 430 case SIOCSIFADDR: 431 ifp->if_flags |= IFF_UP | IFF_RUNNING; 432 ifa = (struct ifaddr *)data; 433 if (ifa != 0) 434 ifa->ifa_rtrequest = lortrequest; 435 /* 436 * Everything else is done at a higher level. 437 */ 438 break; 439 440 case SIOCADDMULTI: 441 case SIOCDELMULTI: 442 ifr = (struct ifreq *)data; 443 if (ifr == 0) { 444 error = EAFNOSUPPORT; /* XXX */ 445 break; 446 } 447 switch (ifr->ifr_addr.sa_family) { 448 449 #ifdef INET 450 case AF_INET: 451 break; 452 #endif 453 #ifdef INET6 454 case AF_INET6: 455 break; 456 #endif /* INET6 */ 457 458 default: 459 error = EAFNOSUPPORT; 460 break; 461 } 462 break; 463 464 case SIOCSIFMTU: 465 ifr = (struct ifreq *)data; 466 ifp->if_mtu = ifr->ifr_mtu; 467 break; 468 469 default: 470 error = ENOTTY; 471 } 472 return (error); 473 } 474