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.9 2004/02/08 08:40:24 silby Exp $ 35 * $DragonFly: src/sys/net/if_loop.c,v 1.26 2008/10/04 11:21:10 sephe 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/lock.h> 52 #include <sys/mbuf.h> 53 #include <sys/socket.h> 54 #include <sys/sockio.h> 55 56 #include <net/if.h> 57 #include <net/if_types.h> 58 #include <net/ifq_var.h> 59 #include <net/netisr.h> 60 #include <net/route.h> 61 #include <net/bpf.h> 62 #include <net/bpfdesc.h> 63 64 #ifdef INET 65 #include <netinet/in.h> 66 #include <netinet/in_var.h> 67 #endif 68 69 #ifdef IPX 70 #include <netproto/ipx/ipx.h> 71 #include <netproto/ipx/ipx_if.h> 72 #endif 73 74 #ifdef INET6 75 #ifndef INET 76 #include <netinet/in.h> 77 #endif 78 #include <netinet6/in6_var.h> 79 #include <netinet/ip6.h> 80 #endif 81 82 #ifdef NS 83 #include <netns/ns.h> 84 #include <netns/ns_if.h> 85 #endif 86 87 #ifdef NETATALK 88 #include <netproto/atalk/at.h> 89 #include <netproto/atalk/at_var.h> 90 #endif 91 92 static void loopattach(void *); 93 static int looutput(struct ifnet *, struct mbuf *, struct sockaddr *, 94 struct rtentry *); 95 static int loioctl(struct ifnet *, u_long, caddr_t, struct ucred *); 96 static void lortrequest(int, struct rtentry *, struct rt_addrinfo *); 97 #ifdef ALTQ 98 static void lo_altqstart(struct ifnet *); 99 #endif 100 PSEUDO_SET(loopattach, if_loop); 101 102 #ifdef TINY_LOMTU 103 #define LOMTU (1024+512) 104 #elif defined(LARGE_LOMTU) 105 #define LOMTU 131072 106 #else 107 #define LOMTU 16384 108 #endif 109 110 #define LO_CSUM_FEATURES (CSUM_IP | CSUM_UDP | CSUM_TCP) 111 112 struct ifnet loif[NLOOP]; 113 114 /* ARGSUSED */ 115 static void 116 loopattach(void *dummy) 117 { 118 struct ifnet *ifp; 119 int i; 120 121 for (i = 0, ifp = loif; i < NLOOP; i++, ifp++) { 122 if_initname(ifp, "lo", i); 123 ifp->if_mtu = LOMTU; 124 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 125 ifp->if_capabilities = IFCAP_HWCSUM; 126 ifp->if_hwassist = LO_CSUM_FEATURES; 127 ifp->if_capenable = ifp->if_capabilities; 128 ifp->if_ioctl = loioctl; 129 ifp->if_output = looutput; 130 ifp->if_type = IFT_LOOP; 131 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 132 ifq_set_ready(&ifp->if_snd); 133 #ifdef ALTQ 134 ifp->if_start = lo_altqstart; 135 #endif 136 if_attach(ifp, NULL); 137 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 138 } 139 } 140 141 static int 142 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 143 struct rtentry *rt) 144 { 145 M_ASSERTPKTHDR(m); 146 147 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 148 m_freem(m); 149 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 150 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 151 } 152 153 ifp->if_opackets++; 154 ifp->if_obytes += m->m_pkthdr.len; 155 #if 1 /* XXX */ 156 switch (dst->sa_family) { 157 case AF_INET: 158 case AF_INET6: 159 case AF_IPX: 160 case AF_NS: 161 case AF_APPLETALK: 162 break; 163 default: 164 kprintf("looutput: af=%d unexpected\n", dst->sa_family); 165 m_freem(m); 166 return (EAFNOSUPPORT); 167 } 168 #endif 169 170 if (ifp->if_capenable & IFCAP_RXCSUM) { 171 int csum_flags = 0; 172 173 if (m->m_pkthdr.csum_flags & CSUM_IP) 174 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 175 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 176 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 177 178 m->m_pkthdr.csum_flags |= csum_flags; 179 if (csum_flags & CSUM_DATA_VALID) 180 m->m_pkthdr.csum_data = 0xffff; 181 } 182 return (if_simloop(ifp, m, dst->sa_family, 0)); 183 } 184 185 /* 186 * if_simloop() 187 * 188 * This function is to support software emulation of hardware loopback, 189 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't 190 * hear their own broadcasts, we create a copy of the packet that we 191 * would normally receive via a hardware loopback. 192 * 193 * This function expects the packet to include the media header of length hlen. 194 */ 195 int 196 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen) 197 { 198 int isr; 199 200 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR")); 201 m->m_pkthdr.rcvif = ifp; 202 203 /* BPF write needs to be handled specially */ 204 if (af == AF_UNSPEC) { 205 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len")); 206 af = *(mtod(m, int *)); 207 m->m_len -= sizeof(int); 208 m->m_pkthdr.len -= sizeof(int); 209 m->m_data += sizeof(int); 210 } 211 212 if (ifp->if_bpf) { 213 get_mplock(); 214 215 /* Re-check */ 216 if (ifp->if_bpf == NULL) 217 goto rel; 218 219 if (ifp->if_bpf->bif_dlt == DLT_NULL) { 220 uint32_t bpf_af = (uint32_t)af; 221 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4); 222 } else { 223 bpf_mtap(ifp->if_bpf, m); 224 } 225 rel: 226 rel_mplock(); 227 } 228 229 /* Strip away media header */ 230 if (hlen > 0) 231 m_adj(m, hlen); 232 233 #ifdef ALTQ 234 /* 235 * altq for loop is just for debugging. 236 * only used when called for loop interface (not for 237 * a simplex interface). 238 */ 239 if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) { 240 struct altq_pktattr pktattr; 241 int32_t *afp; 242 int error; 243 244 /* 245 * if the queueing discipline needs packet classification, 246 * do it before prepending link headers. 247 */ 248 ifq_classify(&ifp->if_snd, m, af, &pktattr); 249 250 M_PREPEND(m, sizeof(int32_t), MB_DONTWAIT); 251 if (m == 0) 252 return(ENOBUFS); 253 afp = mtod(m, int32_t *); 254 *afp = (int32_t)af; 255 256 /* 257 * A critical section is needed for subsystems protected by 258 * the MP lock, and the serializer is assumed to already 259 * be held for MPSAFE subsystems. 260 */ 261 crit_enter(); 262 error = ifq_enqueue(&ifp->if_snd, m, &pktattr); 263 lwkt_serialize_enter(ifp->if_serializer); 264 ifp->if_start(ifp); 265 lwkt_serialize_exit(ifp->if_serializer); 266 crit_exit(); 267 return (error); 268 } 269 #endif /* ALTQ */ 270 271 /* Deliver to upper layer protocol */ 272 switch (af) { 273 #ifdef INET 274 case AF_INET: 275 isr = NETISR_IP; 276 break; 277 #endif 278 #ifdef INET6 279 case AF_INET6: 280 m->m_flags |= M_LOOP; 281 isr = NETISR_IPV6; 282 break; 283 #endif 284 #ifdef IPX 285 case AF_IPX: 286 isr = NETISR_IPX; 287 break; 288 #endif 289 #ifdef NS 290 case AF_NS: 291 isr = NETISR_NS; 292 break; 293 #endif 294 #ifdef NETATALK 295 case AF_APPLETALK: 296 isr = NETISR_ATALK2; 297 break; 298 #endif 299 default: 300 kprintf("if_simloop: can't handle af=%d\n", af); 301 m_freem(m); 302 return (EAFNOSUPPORT); 303 } 304 305 ifp->if_ipackets++; 306 ifp->if_ibytes += m->m_pkthdr.len; 307 netisr_queue(isr, m); 308 return (0); 309 } 310 311 #ifdef ALTQ 312 static void 313 lo_altqstart(struct ifnet *ifp) 314 { 315 struct mbuf *m; 316 int32_t af, *afp; 317 int isr; 318 319 while (1) { 320 crit_enter(); 321 m = ifq_dequeue(&ifp->if_snd, NULL); 322 crit_exit(); 323 if (m == NULL) 324 return; 325 326 afp = mtod(m, int32_t *); 327 af = *afp; 328 m_adj(m, sizeof(int32_t)); 329 330 switch (af) { 331 #ifdef INET 332 case AF_INET: 333 isr = NETISR_IP; 334 break; 335 #endif 336 #ifdef INET6 337 case AF_INET6: 338 m->m_flags |= M_LOOP; 339 isr = NETISR_IPV6; 340 break; 341 #endif 342 #ifdef IPX 343 case AF_IPX: 344 isr = NETISR_IPX; 345 break; 346 #endif 347 #ifdef NS 348 case AF_NS: 349 isr = NETISR_NS; 350 break; 351 #endif 352 #ifdef ISO 353 case AF_ISO: 354 isr = NETISR_ISO; 355 break; 356 #endif 357 #ifdef NETATALK 358 case AF_APPLETALK: 359 isr = NETISR_ATALK2; 360 break; 361 #endif 362 default: 363 kprintf("lo_altqstart: can't handle af%d\n", af); 364 m_freem(m); 365 return; 366 } 367 368 ifp->if_ipackets++; 369 ifp->if_ibytes += m->m_pkthdr.len; 370 netisr_queue(isr, m); 371 } 372 } 373 #endif /* ALTQ */ 374 375 /* ARGSUSED */ 376 static void 377 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 378 { 379 if (rt) { 380 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 381 /* 382 * For optimal performance, the send and receive buffers 383 * should be at least twice the MTU plus a little more for 384 * overhead. 385 */ 386 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU; 387 } 388 } 389 390 /* 391 * Process an ioctl request. 392 */ 393 /* ARGSUSED */ 394 static int 395 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 396 { 397 struct ifaddr *ifa; 398 struct ifreq *ifr = (struct ifreq *)data; 399 int error = 0, mask; 400 401 switch (cmd) { 402 case SIOCSIFADDR: 403 ifp->if_flags |= IFF_UP | IFF_RUNNING; 404 ifa = (struct ifaddr *)data; 405 ifa->ifa_rtrequest = lortrequest; 406 /* 407 * Everything else is done at a higher level. 408 */ 409 break; 410 411 case SIOCADDMULTI: 412 case SIOCDELMULTI: 413 if (ifr == 0) { 414 error = EAFNOSUPPORT; /* XXX */ 415 break; 416 } 417 switch (ifr->ifr_addr.sa_family) { 418 419 #ifdef INET 420 case AF_INET: 421 break; 422 #endif 423 #ifdef INET6 424 case AF_INET6: 425 break; 426 #endif 427 428 default: 429 error = EAFNOSUPPORT; 430 break; 431 } 432 break; 433 434 case SIOCSIFMTU: 435 ifp->if_mtu = ifr->ifr_mtu; 436 break; 437 438 case SIOCSIFFLAGS: 439 break; 440 441 case SIOCSIFCAP: 442 mask = (ifr->ifr_reqcap ^ ifp->if_capenable) & IFCAP_HWCSUM; 443 if (mask) { 444 ifp->if_capenable ^= mask; 445 if (IFCAP_TXCSUM & ifp->if_capenable) 446 ifp->if_hwassist = LO_CSUM_FEATURES; 447 else 448 ifp->if_hwassist = 0; 449 } 450 break; 451 452 default: 453 error = EINVAL; 454 } 455 return (error); 456 } 457