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 #include "opt_route.h" 42 #include "opt_mac.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/mbuf.h> 48 #include <sys/module.h> 49 #include <machine/bus.h> 50 #include <sys/rman.h> 51 #include <sys/socket.h> 52 #include <sys/sockio.h> 53 #include <sys/sysctl.h> 54 #include <sys/vimage.h> 55 56 #include <net/if.h> 57 #include <net/if_clone.h> 58 #include <net/if_types.h> 59 #include <net/netisr.h> 60 #include <net/route.h> 61 #include <net/bpf.h> 62 #include <net/vnet.h> 63 64 #ifdef INET 65 #include <netinet/in.h> 66 #include <netinet/in_var.h> 67 #endif 68 69 #ifdef IPX 70 #include <netipx/ipx.h> 71 #include <netipx/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 NETATALK 83 #include <netatalk/at.h> 84 #include <netatalk/at_var.h> 85 #endif 86 87 #include <security/mac/mac_framework.h> 88 89 #ifdef TINY_LOMTU 90 #define LOMTU (1024+512) 91 #elif defined(LARGE_LOMTU) 92 #define LOMTU 131072 93 #else 94 #define LOMTU 16384 95 #endif 96 97 #define LO_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP) 98 #define LO_CSUM_SET (CSUM_DATA_VALID | CSUM_PSEUDO_HDR | \ 99 CSUM_IP_CHECKED | CSUM_IP_VALID | \ 100 CSUM_SCTP_VALID) 101 102 int loioctl(struct ifnet *, u_long, caddr_t); 103 static void lortrequest(int, struct rtentry *, struct rt_addrinfo *); 104 int looutput(struct ifnet *ifp, struct mbuf *m, 105 struct sockaddr *dst, struct route *ro); 106 static int lo_clone_create(struct if_clone *, int, caddr_t); 107 static void lo_clone_destroy(struct ifnet *); 108 static int vnet_loif_iattach(const void *); 109 110 #ifdef VIMAGE_GLOBALS 111 struct ifnet *loif; /* Used externally */ 112 #endif 113 114 #ifdef VIMAGE 115 MALLOC_DEFINE(M_LO_CLONER, "lo_cloner", "lo_cloner"); 116 #endif 117 118 #ifndef VIMAGE_GLOBALS 119 static const vnet_modinfo_t vnet_loif_modinfo = { 120 .vmi_id = VNET_MOD_LOIF, 121 .vmi_dependson = VNET_MOD_IF_CLONE, 122 .vmi_name = "loif", 123 .vmi_iattach = vnet_loif_iattach 124 }; 125 #endif /* !VIMAGE_GLOBALS */ 126 127 IFC_SIMPLE_DECLARE(lo, 1); 128 129 static void 130 lo_clone_destroy(struct ifnet *ifp) 131 { 132 #ifdef INVARIANTS 133 INIT_VNET_NET(ifp->if_vnet); 134 #endif 135 136 /* XXX: destroying lo0 will lead to panics. */ 137 KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__)); 138 139 bpfdetach(ifp); 140 if_detach(ifp); 141 if_free(ifp); 142 } 143 144 static int 145 lo_clone_create(struct if_clone *ifc, int unit, caddr_t params) 146 { 147 INIT_VNET_NET(curvnet); 148 struct ifnet *ifp; 149 150 ifp = if_alloc(IFT_LOOP); 151 if (ifp == NULL) 152 return (ENOSPC); 153 154 if_initname(ifp, ifc->ifc_name, unit); 155 ifp->if_mtu = LOMTU; 156 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 157 ifp->if_ioctl = loioctl; 158 ifp->if_output = looutput; 159 ifp->if_snd.ifq_maxlen = ifqmaxlen; 160 ifp->if_capabilities = ifp->if_capenable = IFCAP_HWCSUM; 161 ifp->if_hwassist = LO_CSUM_FEATURES; 162 if_attach(ifp); 163 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 164 if (V_loif == NULL) 165 V_loif = ifp; 166 167 return (0); 168 } 169 170 static int vnet_loif_iattach(const void *unused __unused) 171 { 172 INIT_VNET_NET(curvnet); 173 174 V_loif = NULL; 175 176 #ifdef VIMAGE 177 V_lo_cloner = malloc(sizeof(*V_lo_cloner), M_LO_CLONER, 178 M_WAITOK | M_ZERO); 179 bcopy(&lo_cloner, V_lo_cloner, sizeof(*V_lo_cloner)); 180 if_clone_attach(V_lo_cloner); 181 #else 182 if_clone_attach(&lo_cloner); 183 #endif 184 return (0); 185 } 186 187 static int 188 loop_modevent(module_t mod, int type, void *data) 189 { 190 191 switch (type) { 192 case MOD_LOAD: 193 #ifndef VIMAGE_GLOBALS 194 vnet_mod_register(&vnet_loif_modinfo); 195 #else 196 vnet_loif_iattach(NULL); 197 #endif 198 break; 199 200 case MOD_UNLOAD: 201 printf("loop module unload - not possible for this module type\n"); 202 return (EINVAL); 203 204 default: 205 return (EOPNOTSUPP); 206 } 207 return (0); 208 } 209 210 static moduledata_t loop_mod = { 211 "loop", 212 loop_modevent, 213 0 214 }; 215 216 DECLARE_MODULE(loop, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY); 217 218 int 219 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 220 struct route *ro) 221 { 222 u_int32_t af; 223 struct rtentry *rt = NULL; 224 #ifdef MAC 225 int error; 226 #endif 227 228 M_ASSERTPKTHDR(m); /* check if we have the packet header */ 229 230 if (ro != NULL) 231 rt = ro->ro_rt; 232 #ifdef MAC 233 error = mac_ifnet_check_transmit(ifp, m); 234 if (error) { 235 m_freem(m); 236 return (error); 237 } 238 #endif 239 240 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 241 m_freem(m); 242 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 243 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 244 } 245 246 ifp->if_opackets++; 247 ifp->if_obytes += m->m_pkthdr.len; 248 249 /* BPF writes need to be handled specially. */ 250 if (dst->sa_family == AF_UNSPEC) { 251 bcopy(dst->sa_data, &af, sizeof(af)); 252 dst->sa_family = af; 253 } 254 255 #if 1 /* XXX */ 256 switch (dst->sa_family) { 257 case AF_INET: 258 if (ifp->if_capenable & IFCAP_RXCSUM) { 259 m->m_pkthdr.csum_data = 0xffff; 260 m->m_pkthdr.csum_flags = LO_CSUM_SET; 261 } 262 m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES; 263 case AF_INET6: 264 case AF_IPX: 265 case AF_APPLETALK: 266 break; 267 default: 268 printf("looutput: af=%d unexpected\n", dst->sa_family); 269 m_freem(m); 270 return (EAFNOSUPPORT); 271 } 272 #endif 273 return (if_simloop(ifp, m, dst->sa_family, 0)); 274 } 275 276 /* 277 * if_simloop() 278 * 279 * This function is to support software emulation of hardware loopback, 280 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't 281 * hear their own broadcasts, we create a copy of the packet that we 282 * would normally receive via a hardware loopback. 283 * 284 * This function expects the packet to include the media header of length hlen. 285 */ 286 int 287 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen) 288 { 289 INIT_VNET_NET(ifp->if_vnet); 290 int isr; 291 292 M_ASSERTPKTHDR(m); 293 m_tag_delete_nonpersistent(m); 294 m->m_pkthdr.rcvif = ifp; 295 296 #ifdef MAC 297 mac_ifnet_create_mbuf(ifp, m); 298 #endif 299 300 /* 301 * Let BPF see incoming packet in the following manner: 302 * - Emulated packet loopback for a simplex interface 303 * (net/if_ethersubr.c) 304 * -> passes it to ifp's BPF 305 * - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c) 306 * -> not passes it to any BPF 307 * - Normal packet loopback from myself to myself (net/if_loop.c) 308 * -> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0) 309 */ 310 if (hlen > 0) { 311 if (bpf_peers_present(ifp->if_bpf)) { 312 bpf_mtap(ifp->if_bpf, m); 313 } 314 } else { 315 if (bpf_peers_present(V_loif->if_bpf)) { 316 if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) { 317 /* XXX beware sizeof(af) != 4 */ 318 u_int32_t af1 = af; 319 320 /* 321 * We need to prepend the address family. 322 */ 323 bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m); 324 } 325 } 326 } 327 328 /* Strip away media header */ 329 if (hlen > 0) { 330 m_adj(m, hlen); 331 #ifndef __NO_STRICT_ALIGNMENT 332 /* 333 * Some archs do not like unaligned data, so 334 * we move data down in the first mbuf. 335 */ 336 if (mtod(m, vm_offset_t) & 3) { 337 KASSERT(hlen >= 3, ("if_simloop: hlen too small")); 338 bcopy(m->m_data, 339 (char *)(mtod(m, vm_offset_t) 340 - (mtod(m, vm_offset_t) & 3)), 341 m->m_len); 342 m->m_data -= (mtod(m,vm_offset_t) & 3); 343 } 344 #endif 345 } 346 347 /* Deliver to upper layer protocol */ 348 switch (af) { 349 #ifdef INET 350 case AF_INET: 351 isr = NETISR_IP; 352 break; 353 #endif 354 #ifdef INET6 355 case AF_INET6: 356 m->m_flags |= M_LOOP; 357 isr = NETISR_IPV6; 358 break; 359 #endif 360 #ifdef IPX 361 case AF_IPX: 362 isr = NETISR_IPX; 363 break; 364 #endif 365 #ifdef NETATALK 366 case AF_APPLETALK: 367 isr = NETISR_ATALK2; 368 break; 369 #endif 370 default: 371 printf("if_simloop: can't handle af=%d\n", af); 372 m_freem(m); 373 return (EAFNOSUPPORT); 374 } 375 ifp->if_ipackets++; 376 ifp->if_ibytes += m->m_pkthdr.len; 377 netisr_queue(isr, m); /* mbuf is free'd on failure. */ 378 return (0); 379 } 380 381 /* ARGSUSED */ 382 static void 383 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 384 { 385 386 RT_LOCK_ASSERT(rt); 387 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; 388 } 389 390 /* 391 * Process an ioctl request. 392 */ 393 /* ARGSUSED */ 394 int 395 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 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; 404 ifp->if_drv_flags |= IFF_DRV_RUNNING; 405 ifa = (struct ifaddr *)data; 406 ifa->ifa_rtrequest = lortrequest; 407 /* 408 * Everything else is done at a higher level. 409 */ 410 break; 411 412 case SIOCADDMULTI: 413 case SIOCDELMULTI: 414 if (ifr == 0) { 415 error = EAFNOSUPPORT; /* XXX */ 416 break; 417 } 418 switch (ifr->ifr_addr.sa_family) { 419 420 #ifdef INET 421 case AF_INET: 422 break; 423 #endif 424 #ifdef INET6 425 case AF_INET6: 426 break; 427 #endif 428 429 default: 430 error = EAFNOSUPPORT; 431 break; 432 } 433 break; 434 435 case SIOCSIFMTU: 436 ifp->if_mtu = ifr->ifr_mtu; 437 break; 438 439 case SIOCSIFFLAGS: 440 break; 441 442 case SIOCSIFCAP: 443 mask = ifp->if_capenable ^ ifr->ifr_reqcap; 444 if ((mask & IFCAP_RXCSUM) != 0) 445 ifp->if_capenable ^= IFCAP_RXCSUM; 446 if ((mask & IFCAP_TXCSUM) != 0) 447 ifp->if_capenable ^= IFCAP_TXCSUM; 448 if (ifp->if_capenable & IFCAP_TXCSUM) 449 ifp->if_hwassist = LO_CSUM_FEATURES; 450 else 451 ifp->if_hwassist = 0; 452 break; 453 454 default: 455 error = EINVAL; 456 } 457 return (error); 458 } 459