1 /* 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93 34 * $FreeBSD: src/sys/net/if_loop.c,v 1.47.2.8 2003/06/01 01:46:11 silby Exp $ 35 * $DragonFly: src/sys/net/if_loop.c,v 1.15 2005/02/12 01:24:34 joerg Exp $ 36 */ 37 38 /* 39 * Loopback interface driver for protocol testing and timing. 40 */ 41 #include "use_loop.h" 42 43 #include "opt_atalk.h" 44 #include "opt_inet.h" 45 #include "opt_inet6.h" 46 #include "opt_ipx.h" 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/mbuf.h> 52 #include <sys/socket.h> 53 #include <sys/sockio.h> 54 55 #include <net/if.h> 56 #include <net/if_types.h> 57 #include <net/ifq_var.h> 58 #include <net/netisr.h> 59 #include <net/route.h> 60 #include <net/bpf.h> 61 #include <net/bpfdesc.h> 62 63 #ifdef INET 64 #include <netinet/in.h> 65 #include <netinet/in_var.h> 66 #endif 67 68 #ifdef IPX 69 #include <netproto/ipx/ipx.h> 70 #include <netproto/ipx/ipx_if.h> 71 #endif 72 73 #ifdef INET6 74 #ifndef INET 75 #include <netinet/in.h> 76 #endif 77 #include <netinet6/in6_var.h> 78 #include <netinet/ip6.h> 79 #endif 80 81 #ifdef NS 82 #include <netns/ns.h> 83 #include <netns/ns_if.h> 84 #endif 85 86 #ifdef NETATALK 87 #include <netproto/atalk/at.h> 88 #include <netproto/atalk/at_var.h> 89 #endif 90 91 int loioctl (struct ifnet *, u_long, caddr_t, struct ucred *); 92 static void lortrequest (int, struct rtentry *, struct rt_addrinfo *); 93 94 static void loopattach (void *); 95 #ifdef ALTQ 96 static void lo_altqstart(struct ifnet *); 97 #endif 98 PSEUDO_SET(loopattach, if_loop); 99 100 int looutput (struct ifnet *ifp, 101 struct mbuf *m, struct sockaddr *dst, struct rtentry *rt); 102 103 #ifdef TINY_LOMTU 104 #define LOMTU (1024+512) 105 #elif defined(LARGE_LOMTU) 106 #define LOMTU 131072 107 #else 108 #define LOMTU 16384 109 #endif 110 111 struct ifnet loif[NLOOP]; 112 113 /* ARGSUSED */ 114 static void 115 loopattach(void *dummy) 116 { 117 struct ifnet *ifp; 118 int i; 119 120 for (i = 0, ifp = loif; i < NLOOP; i++, ifp++) { 121 if_initname(ifp, "lo", i); 122 ifp->if_mtu = LOMTU; 123 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 124 ifp->if_ioctl = loioctl; 125 ifp->if_output = looutput; 126 ifp->if_type = IFT_LOOP; 127 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 128 ifq_set_ready(&ifp->if_snd); 129 #ifdef ALTQ 130 ifp->if_start = lo_altqstart; 131 #endif 132 if_attach(ifp); 133 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 134 } 135 } 136 137 int 138 looutput( 139 struct ifnet *ifp, 140 struct mbuf *m, 141 struct sockaddr *dst, 142 struct rtentry *rt) 143 { 144 struct mbuf *n; 145 146 if ((m->m_flags & M_PKTHDR) == 0) 147 panic("looutput no HDR"); 148 149 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 150 m_freem(m); 151 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 152 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 153 } 154 /* 155 * KAME requires that the packet to be contiguous on the 156 * mbuf. We need to make that sure. 157 * this kind of code should be avoided. 158 * 159 * XXX: KAME may no longer need contiguous packets. Once 160 * that has been verified, the following code _should_ be 161 * removed. 162 */ 163 if (m && m->m_next != NULL) { 164 165 n = m_defrag(m, MB_DONTWAIT); 166 167 if (n == NULL) { 168 m_freem(m); 169 return (ENOBUFS); 170 } else { 171 m = n; 172 } 173 } 174 175 ifp->if_opackets++; 176 ifp->if_obytes += m->m_pkthdr.len; 177 #if 1 /* XXX */ 178 switch (dst->sa_family) { 179 case AF_INET: 180 case AF_INET6: 181 case AF_IPX: 182 case AF_NS: 183 case AF_APPLETALK: 184 break; 185 default: 186 printf("looutput: af=%d unexpected\n", dst->sa_family); 187 m_freem(m); 188 return (EAFNOSUPPORT); 189 } 190 #endif 191 return (if_simloop(ifp, m, dst->sa_family, 0)); 192 } 193 194 /* 195 * if_simloop() 196 * 197 * This function is to support software emulation of hardware loopback, 198 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't 199 * hear their own broadcasts, we create a copy of the packet that we 200 * would normally receive via a hardware loopback. 201 * 202 * This function expects the packet to include the media header of length hlen. 203 */ 204 int 205 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen) 206 { 207 int isr; 208 209 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR")); 210 m->m_pkthdr.rcvif = ifp; 211 212 /* BPF write needs to be handled specially */ 213 if (af == AF_UNSPEC) { 214 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len")); 215 af = *(mtod(m, int *)); 216 m->m_len -= sizeof(int); 217 m->m_pkthdr.len -= sizeof(int); 218 m->m_data += sizeof(int); 219 } 220 221 if (ifp->if_bpf) { 222 if (ifp->if_bpf->bif_dlt == DLT_NULL) { 223 uint32_t bpf_af = (uint32_t)af; 224 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4); 225 } 226 else { 227 bpf_mtap(ifp->if_bpf, m); 228 } 229 } 230 231 /* Strip away media header */ 232 if (hlen > 0) { 233 m_adj(m, hlen); 234 #ifdef __alpha__ 235 /* The alpha doesn't like unaligned data. 236 * We move data down in the first mbuf */ 237 if (mtod(m, vm_offset_t) & 3) { 238 KASSERT(hlen >= 3, ("if_simloop: hlen too small")); 239 bcopy(m->m_data, 240 (char *)(mtod(m, vm_offset_t) 241 - (mtod(m, vm_offset_t) & 3)), 242 m->m_len); 243 mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3); 244 } 245 #endif 246 } 247 248 #ifdef ALTQ 249 /* 250 * altq for loop is just for debugging. 251 * only used when called for loop interface (not for 252 * a simplex interface). 253 */ 254 if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) { 255 struct altq_pktattr pktattr; 256 int32_t *afp; 257 int error, s; 258 259 /* 260 * if the queueing discipline needs packet classification, 261 * do it before prepending link headers. 262 */ 263 ifq_classify(&ifp->if_snd, m, af, &pktattr); 264 265 M_PREPEND(m, sizeof(int32_t), MB_DONTWAIT); 266 if (m == 0) 267 return(ENOBUFS); 268 afp = mtod(m, int32_t *); 269 *afp = (int32_t)af; 270 271 s = splimp(); 272 error = ifq_enqueue(&ifp->if_snd, m, &pktattr); 273 (*ifp->if_start)(ifp); 274 splx(s); 275 return (error); 276 } 277 #endif /* ALTQ */ 278 279 /* Deliver to upper layer protocol */ 280 switch (af) { 281 #ifdef INET 282 case AF_INET: 283 isr = NETISR_IP; 284 break; 285 #endif 286 #ifdef INET6 287 case AF_INET6: 288 m->m_flags |= M_LOOP; 289 isr = NETISR_IPV6; 290 break; 291 #endif 292 #ifdef IPX 293 case AF_IPX: 294 isr = NETISR_IPX; 295 break; 296 #endif 297 #ifdef NS 298 case AF_NS: 299 isr = NETISR_NS; 300 break; 301 #endif 302 #ifdef NETATALK 303 case AF_APPLETALK: 304 isr = NETISR_ATALK2; 305 break; 306 #endif 307 default: 308 printf("if_simloop: can't handle af=%d\n", af); 309 m_freem(m); 310 return (EAFNOSUPPORT); 311 } 312 313 ifp->if_ipackets++; 314 ifp->if_ibytes += m->m_pkthdr.len; 315 netisr_queue(isr, m); 316 return (0); 317 } 318 319 #ifdef ALTQ 320 static void 321 lo_altqstart(struct ifnet *ifp) 322 { 323 struct mbuf *m; 324 int32_t af, *afp; 325 int s, isr; 326 327 while (1) { 328 s = splimp(); 329 m = ifq_dequeue(&ifp->if_snd); 330 splx(s); 331 if (m == NULL) 332 return; 333 334 afp = mtod(m, int32_t *); 335 af = *afp; 336 m_adj(m, sizeof(int32_t)); 337 338 switch (af) { 339 #ifdef INET 340 case AF_INET: 341 isr = NETISR_IP; 342 break; 343 #endif 344 #ifdef INET6 345 case AF_INET6: 346 m->m_flags |= M_LOOP; 347 isr = NETISR_IPV6; 348 break; 349 #endif 350 #ifdef IPX 351 case AF_IPX: 352 isr = NETISR_IPX; 353 break; 354 #endif 355 #ifdef NS 356 case AF_NS: 357 isr = NETISR_NS; 358 break; 359 #endif 360 #ifdef ISO 361 case AF_ISO: 362 isr = NETISR_ISO; 363 break; 364 #endif 365 #ifdef NETATALK 366 case AF_APPLETALK: 367 isr = NETISR_ATALK2; 368 break; 369 #endif 370 default: 371 printf("lo_altqstart: can't handle af%d\n", af); 372 m_freem(m); 373 return; 374 } 375 376 ifp->if_ipackets++; 377 ifp->if_ibytes += m->m_pkthdr.len; 378 netisr_queue(isr, m); 379 } 380 } 381 #endif /* ALTQ */ 382 383 /* ARGSUSED */ 384 static void 385 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 386 { 387 if (rt) { 388 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 389 /* 390 * For optimal performance, the send and receive buffers 391 * should be at least twice the MTU plus a little more for 392 * overhead. 393 */ 394 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU; 395 } 396 } 397 398 /* 399 * Process an ioctl request. 400 */ 401 /* ARGSUSED */ 402 int 403 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 404 { 405 struct ifaddr *ifa; 406 struct ifreq *ifr = (struct ifreq *)data; 407 int error = 0; 408 409 switch (cmd) { 410 411 case SIOCSIFADDR: 412 ifp->if_flags |= IFF_UP | IFF_RUNNING; 413 ifa = (struct ifaddr *)data; 414 ifa->ifa_rtrequest = lortrequest; 415 /* 416 * Everything else is done at a higher level. 417 */ 418 break; 419 420 case SIOCADDMULTI: 421 case SIOCDELMULTI: 422 if (ifr == 0) { 423 error = EAFNOSUPPORT; /* XXX */ 424 break; 425 } 426 switch (ifr->ifr_addr.sa_family) { 427 428 #ifdef INET 429 case AF_INET: 430 break; 431 #endif 432 #ifdef INET6 433 case AF_INET6: 434 break; 435 #endif 436 437 default: 438 error = EAFNOSUPPORT; 439 break; 440 } 441 break; 442 443 case SIOCSIFMTU: 444 ifp->if_mtu = ifr->ifr_mtu; 445 break; 446 447 case SIOCSIFFLAGS: 448 break; 449 450 default: 451 error = EINVAL; 452 } 453 return (error); 454 } 455