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