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