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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)if_loop.c 8.2 (Berkeley) 1/9/95 30 * $FreeBSD$ 31 */ 32 33 /* 34 * Loopback interface driver for protocol testing and timing. 35 */ 36 37 #include "opt_atalk.h" 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 #include "opt_ipx.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/mbuf.h> 46 #include <sys/module.h> 47 #include <machine/bus.h> 48 #include <sys/rman.h> 49 #include <sys/socket.h> 50 #include <sys/sockio.h> 51 #include <sys/sysctl.h> 52 #include <sys/vimage.h> 53 54 #include <net/if.h> 55 #include <net/if_clone.h> 56 #include <net/if_types.h> 57 #include <net/netisr.h> 58 #include <net/route.h> 59 #include <net/bpf.h> 60 #include <net/vnet.h> 61 62 #ifdef INET 63 #include <netinet/in.h> 64 #include <netinet/in_var.h> 65 #endif 66 67 #ifdef IPX 68 #include <netipx/ipx.h> 69 #include <netipx/ipx_if.h> 70 #endif 71 72 #ifdef INET6 73 #ifndef INET 74 #include <netinet/in.h> 75 #endif 76 #include <netinet6/in6_var.h> 77 #include <netinet/ip6.h> 78 #endif 79 80 #ifdef NETATALK 81 #include <netatalk/at.h> 82 #include <netatalk/at_var.h> 83 #endif 84 85 #include <security/mac/mac_framework.h> 86 87 #ifdef TINY_LOMTU 88 #define LOMTU (1024+512) 89 #elif defined(LARGE_LOMTU) 90 #define LOMTU 131072 91 #else 92 #define LOMTU 16384 93 #endif 94 95 #define LO_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP) 96 #define LO_CSUM_SET (CSUM_DATA_VALID | CSUM_PSEUDO_HDR | \ 97 CSUM_IP_CHECKED | CSUM_IP_VALID | \ 98 CSUM_SCTP_VALID) 99 100 int loioctl(struct ifnet *, u_long, caddr_t); 101 static void lortrequest(int, struct rtentry *, struct rt_addrinfo *); 102 int looutput(struct ifnet *ifp, struct mbuf *m, 103 struct sockaddr *dst, struct route *ro); 104 static int lo_clone_create(struct if_clone *, int, caddr_t); 105 static void lo_clone_destroy(struct ifnet *); 106 static int vnet_loif_iattach(const void *); 107 #ifdef VIMAGE 108 static int vnet_loif_idetach(const void *); 109 #endif 110 111 #ifdef VIMAGE_GLOBALS 112 struct ifnet *loif; /* Used externally */ 113 #endif 114 115 #ifdef VIMAGE 116 MALLOC_DEFINE(M_LO_CLONER, "lo_cloner", "lo_cloner"); 117 #endif 118 119 #ifndef VIMAGE_GLOBALS 120 static const vnet_modinfo_t vnet_loif_modinfo = { 121 .vmi_id = VNET_MOD_LOIF, 122 .vmi_dependson = VNET_MOD_IF_CLONE, 123 .vmi_name = "loif", 124 .vmi_iattach = vnet_loif_iattach, 125 #ifdef VIMAGE 126 .vmi_idetach = vnet_loif_idetach 127 #endif 128 }; 129 #endif /* !VIMAGE_GLOBALS */ 130 131 IFC_SIMPLE_DECLARE(lo, 1); 132 133 static void 134 lo_clone_destroy(struct ifnet *ifp) 135 { 136 137 #ifndef VIMAGE 138 /* XXX: destroying lo0 will lead to panics. */ 139 KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__)); 140 #endif 141 142 bpfdetach(ifp); 143 if_detach(ifp); 144 if_free(ifp); 145 } 146 147 static int 148 lo_clone_create(struct if_clone *ifc, int unit, caddr_t params) 149 { 150 INIT_VNET_NET(curvnet); 151 struct ifnet *ifp; 152 153 ifp = if_alloc(IFT_LOOP); 154 if (ifp == NULL) 155 return (ENOSPC); 156 157 if_initname(ifp, ifc->ifc_name, unit); 158 ifp->if_mtu = LOMTU; 159 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 160 ifp->if_ioctl = loioctl; 161 ifp->if_output = looutput; 162 ifp->if_snd.ifq_maxlen = ifqmaxlen; 163 ifp->if_capabilities = ifp->if_capenable = IFCAP_HWCSUM; 164 ifp->if_hwassist = LO_CSUM_FEATURES; 165 if_attach(ifp); 166 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 167 if (V_loif == NULL) 168 V_loif = ifp; 169 170 return (0); 171 } 172 173 static int 174 vnet_loif_iattach(const void *unused __unused) 175 { 176 INIT_VNET_NET(curvnet); 177 178 V_loif = NULL; 179 180 #ifdef VIMAGE 181 V_lo_cloner = malloc(sizeof(*V_lo_cloner), M_LO_CLONER, 182 M_WAITOK | M_ZERO); 183 V_lo_cloner_data = malloc(sizeof(*V_lo_cloner_data), M_LO_CLONER, 184 M_WAITOK | M_ZERO); 185 bcopy(&lo_cloner, V_lo_cloner, sizeof(*V_lo_cloner)); 186 bcopy(lo_cloner.ifc_data, V_lo_cloner_data, sizeof(*V_lo_cloner_data)); 187 V_lo_cloner->ifc_data = V_lo_cloner_data; 188 if_clone_attach(V_lo_cloner); 189 #else 190 if_clone_attach(&lo_cloner); 191 #endif 192 return (0); 193 } 194 195 #ifdef VIMAGE 196 static int 197 vnet_loif_idetach(const void *unused __unused) 198 { 199 INIT_VNET_NET(curvnet); 200 201 if_clone_detach(V_lo_cloner); 202 free(V_lo_cloner, M_LO_CLONER); 203 free(V_lo_cloner_data, M_LO_CLONER); 204 V_loif = NULL; 205 206 return (0); 207 } 208 #endif 209 210 static int 211 loop_modevent(module_t mod, int type, void *data) 212 { 213 214 switch (type) { 215 case MOD_LOAD: 216 #ifndef VIMAGE_GLOBALS 217 vnet_mod_register(&vnet_loif_modinfo); 218 #else 219 vnet_loif_iattach(NULL); 220 #endif 221 break; 222 223 case MOD_UNLOAD: 224 printf("loop module unload - not possible for this module type\n"); 225 return (EINVAL); 226 227 default: 228 return (EOPNOTSUPP); 229 } 230 return (0); 231 } 232 233 static moduledata_t loop_mod = { 234 "loop", 235 loop_modevent, 236 0 237 }; 238 239 DECLARE_MODULE(loop, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY); 240 241 int 242 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 243 struct route *ro) 244 { 245 u_int32_t af; 246 struct rtentry *rt = NULL; 247 #ifdef MAC 248 int error; 249 #endif 250 251 M_ASSERTPKTHDR(m); /* check if we have the packet header */ 252 253 if (ro != NULL) 254 rt = ro->ro_rt; 255 #ifdef MAC 256 error = mac_ifnet_check_transmit(ifp, m); 257 if (error) { 258 m_freem(m); 259 return (error); 260 } 261 #endif 262 263 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 264 m_freem(m); 265 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 266 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 267 } 268 269 ifp->if_opackets++; 270 ifp->if_obytes += m->m_pkthdr.len; 271 272 /* BPF writes need to be handled specially. */ 273 if (dst->sa_family == AF_UNSPEC) { 274 bcopy(dst->sa_data, &af, sizeof(af)); 275 dst->sa_family = af; 276 } 277 278 #if 1 /* XXX */ 279 switch (dst->sa_family) { 280 case AF_INET: 281 if (ifp->if_capenable & IFCAP_RXCSUM) { 282 m->m_pkthdr.csum_data = 0xffff; 283 m->m_pkthdr.csum_flags = LO_CSUM_SET; 284 } 285 m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES; 286 case AF_INET6: 287 case AF_IPX: 288 case AF_APPLETALK: 289 break; 290 default: 291 printf("looutput: af=%d unexpected\n", dst->sa_family); 292 m_freem(m); 293 return (EAFNOSUPPORT); 294 } 295 #endif 296 return (if_simloop(ifp, m, dst->sa_family, 0)); 297 } 298 299 /* 300 * if_simloop() 301 * 302 * This function is to support software emulation of hardware loopback, 303 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't 304 * hear their own broadcasts, we create a copy of the packet that we 305 * would normally receive via a hardware loopback. 306 * 307 * This function expects the packet to include the media header of length hlen. 308 */ 309 int 310 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen) 311 { 312 INIT_VNET_NET(ifp->if_vnet); 313 int isr; 314 315 M_ASSERTPKTHDR(m); 316 m_tag_delete_nonpersistent(m); 317 m->m_pkthdr.rcvif = ifp; 318 319 #ifdef MAC 320 mac_ifnet_create_mbuf(ifp, m); 321 #endif 322 323 /* 324 * Let BPF see incoming packet in the following manner: 325 * - Emulated packet loopback for a simplex interface 326 * (net/if_ethersubr.c) 327 * -> passes it to ifp's BPF 328 * - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c) 329 * -> not passes it to any BPF 330 * - Normal packet loopback from myself to myself (net/if_loop.c) 331 * -> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0) 332 */ 333 if (hlen > 0) { 334 if (bpf_peers_present(ifp->if_bpf)) { 335 bpf_mtap(ifp->if_bpf, m); 336 } 337 } else { 338 if (bpf_peers_present(V_loif->if_bpf)) { 339 if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) { 340 /* XXX beware sizeof(af) != 4 */ 341 u_int32_t af1 = af; 342 343 /* 344 * We need to prepend the address family. 345 */ 346 bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m); 347 } 348 } 349 } 350 351 /* Strip away media header */ 352 if (hlen > 0) { 353 m_adj(m, hlen); 354 #ifndef __NO_STRICT_ALIGNMENT 355 /* 356 * Some archs do not like unaligned data, so 357 * we move data down in the first mbuf. 358 */ 359 if (mtod(m, vm_offset_t) & 3) { 360 KASSERT(hlen >= 3, ("if_simloop: hlen too small")); 361 bcopy(m->m_data, 362 (char *)(mtod(m, vm_offset_t) 363 - (mtod(m, vm_offset_t) & 3)), 364 m->m_len); 365 m->m_data -= (mtod(m,vm_offset_t) & 3); 366 } 367 #endif 368 } 369 370 /* Deliver to upper layer protocol */ 371 switch (af) { 372 #ifdef INET 373 case AF_INET: 374 isr = NETISR_IP; 375 break; 376 #endif 377 #ifdef INET6 378 case AF_INET6: 379 m->m_flags |= M_LOOP; 380 isr = NETISR_IPV6; 381 break; 382 #endif 383 #ifdef IPX 384 case AF_IPX: 385 isr = NETISR_IPX; 386 break; 387 #endif 388 #ifdef NETATALK 389 case AF_APPLETALK: 390 isr = NETISR_ATALK2; 391 break; 392 #endif 393 default: 394 printf("if_simloop: can't handle af=%d\n", af); 395 m_freem(m); 396 return (EAFNOSUPPORT); 397 } 398 ifp->if_ipackets++; 399 ifp->if_ibytes += m->m_pkthdr.len; 400 netisr_queue(isr, m); /* mbuf is free'd on failure. */ 401 return (0); 402 } 403 404 /* ARGSUSED */ 405 static void 406 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 407 { 408 409 RT_LOCK_ASSERT(rt); 410 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; 411 } 412 413 /* 414 * Process an ioctl request. 415 */ 416 /* ARGSUSED */ 417 int 418 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 419 { 420 struct ifaddr *ifa; 421 struct ifreq *ifr = (struct ifreq *)data; 422 int error = 0, mask; 423 424 switch (cmd) { 425 case SIOCSIFADDR: 426 ifp->if_flags |= IFF_UP; 427 ifp->if_drv_flags |= IFF_DRV_RUNNING; 428 ifa = (struct ifaddr *)data; 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 if (ifr == 0) { 438 error = EAFNOSUPPORT; /* XXX */ 439 break; 440 } 441 switch (ifr->ifr_addr.sa_family) { 442 443 #ifdef INET 444 case AF_INET: 445 break; 446 #endif 447 #ifdef INET6 448 case AF_INET6: 449 break; 450 #endif 451 452 default: 453 error = EAFNOSUPPORT; 454 break; 455 } 456 break; 457 458 case SIOCSIFMTU: 459 ifp->if_mtu = ifr->ifr_mtu; 460 break; 461 462 case SIOCSIFFLAGS: 463 break; 464 465 case SIOCSIFCAP: 466 mask = ifp->if_capenable ^ ifr->ifr_reqcap; 467 if ((mask & IFCAP_RXCSUM) != 0) 468 ifp->if_capenable ^= IFCAP_RXCSUM; 469 if ((mask & IFCAP_TXCSUM) != 0) 470 ifp->if_capenable ^= IFCAP_TXCSUM; 471 if (ifp->if_capenable & IFCAP_TXCSUM) 472 ifp->if_hwassist = LO_CSUM_FEATURES; 473 else 474 ifp->if_hwassist = 0; 475 break; 476 477 default: 478 error = EINVAL; 479 } 480 return (error); 481 } 482