11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Pseudo-driver for the loopback interface. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Version: @(#)loopback.c 1.0.4b 08/16/93 91da177e4SLinus Torvalds * 1002c30a84SJesper Juhl * Authors: Ross Biro 111da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 121da177e4SLinus Torvalds * Donald Becker, <becker@scyld.com> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Alan Cox : Fixed oddments for NET3.014 151da177e4SLinus Torvalds * Alan Cox : Rejig for NET3.029 snap #3 161da177e4SLinus Torvalds * Alan Cox : Fixed NET3.029 bugs and sped up 171da177e4SLinus Torvalds * Larry McVoy : Tiny tweak to double performance 181da177e4SLinus Torvalds * Alan Cox : Backed out LMV's tweak - the linux mm 191da177e4SLinus Torvalds * can't take it... 201da177e4SLinus Torvalds * Michael Griffith: Don't bother computing the checksums 211da177e4SLinus Torvalds * on packets received on the loopback 221da177e4SLinus Torvalds * interface. 231da177e4SLinus Torvalds * Alexey Kuznetsov: Potential hang under some extreme 241da177e4SLinus Torvalds * cases removed. 251da177e4SLinus Torvalds * 261da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 271da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 281da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 291da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 301da177e4SLinus Torvalds */ 311da177e4SLinus Torvalds #include <linux/kernel.h> 321da177e4SLinus Torvalds #include <linux/jiffies.h> 331da177e4SLinus Torvalds #include <linux/module.h> 341da177e4SLinus Torvalds #include <linux/interrupt.h> 351da177e4SLinus Torvalds #include <linux/fs.h> 361da177e4SLinus Torvalds #include <linux/types.h> 371da177e4SLinus Torvalds #include <linux/string.h> 381da177e4SLinus Torvalds #include <linux/socket.h> 391da177e4SLinus Torvalds #include <linux/errno.h> 401da177e4SLinus Torvalds #include <linux/fcntl.h> 411da177e4SLinus Torvalds #include <linux/in.h> 421da177e4SLinus Torvalds 437c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 44b3407c8eSEzequiel Lara Gomez #include <linux/io.h> 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #include <linux/inet.h> 471da177e4SLinus Torvalds #include <linux/netdevice.h> 481da177e4SLinus Torvalds #include <linux/etherdevice.h> 491da177e4SLinus Torvalds #include <linux/skbuff.h> 501da177e4SLinus Torvalds #include <linux/ethtool.h> 511da177e4SLinus Torvalds #include <net/sock.h> 521da177e4SLinus Torvalds #include <net/checksum.h> 531da177e4SLinus Torvalds #include <linux/if_ether.h> /* For the statistics structure. */ 541da177e4SLinus Torvalds #include <linux/if_arp.h> /* For ARPHRD_ETHER */ 551da177e4SLinus Torvalds #include <linux/ip.h> 561da177e4SLinus Torvalds #include <linux/tcp.h> 571da177e4SLinus Torvalds #include <linux/percpu.h> 586df014cfSEzequiel Lara Gomez #include <linux/net_tstamp.h> 592774c7abSEric W. Biederman #include <net/net_namespace.h> 605eaa0bd8SEric Dumazet #include <linux/u64_stats_sync.h> 611da177e4SLinus Torvalds 62b3407c8eSEzequiel Lara Gomez /* The higher levels take care of making this non-reentrant (it's 631da177e4SLinus Torvalds * called with bh's disabled). 641da177e4SLinus Torvalds */ 6561357325SStephen Hemminger static netdev_tx_t loopback_xmit(struct sk_buff *skb, 6661357325SStephen Hemminger struct net_device *dev) 671da177e4SLinus Torvalds { 6847d74275STejun Heo struct pcpu_lstats *lb_stats; 697eebb0b2SEric Dumazet int len; 701da177e4SLinus Torvalds 716df014cfSEzequiel Lara Gomez skb_tx_timestamp(skb); 72*4c16128bSEric Dumazet 73*4c16128bSEric Dumazet /* do not fool net_timestamp_check() with various clock bases */ 74*4c16128bSEric Dumazet skb->tstamp = 0; 75*4c16128bSEric Dumazet 761da177e4SLinus Torvalds skb_orphan(skb); 771da177e4SLinus Torvalds 78794ed393SEric Dumazet /* Before queueing this packet to netif_rx(), 79794ed393SEric Dumazet * make sure dst is refcounted. 80794ed393SEric Dumazet */ 81794ed393SEric Dumazet skb_dst_force(skb); 82794ed393SEric Dumazet 831da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, dev); 841da177e4SLinus Torvalds 859e0db4b1SEric W. Biederman /* it's OK to use per_cpu_ptr() because BHs are off */ 86a7855c78SEric Dumazet lb_stats = this_cpu_ptr(dev->lstats); 871da177e4SLinus Torvalds 887eebb0b2SEric Dumazet len = skb->len; 897eebb0b2SEric Dumazet if (likely(netif_rx(skb) == NET_RX_SUCCESS)) { 905eaa0bd8SEric Dumazet u64_stats_update_begin(&lb_stats->syncp); 917eebb0b2SEric Dumazet lb_stats->bytes += len; 927eebb0b2SEric Dumazet lb_stats->packets++; 935eaa0bd8SEric Dumazet u64_stats_update_end(&lb_stats->syncp); 94caf586e5SEric Dumazet } 951da177e4SLinus Torvalds 966ed10654SPatrick McHardy return NETDEV_TX_OK; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds 99bc1f4470Sstephen hemminger static void loopback_get_stats64(struct net_device *dev, 10028172739SEric Dumazet struct rtnl_link_stats64 *stats) 1011da177e4SLinus Torvalds { 1026b10de38SEric Dumazet u64 bytes = 0; 1036b10de38SEric Dumazet u64 packets = 0; 1041da177e4SLinus Torvalds int i; 1051da177e4SLinus Torvalds 1060fed4846SKAMEZAWA Hiroyuki for_each_possible_cpu(i) { 1075175c378SEric Dumazet const struct pcpu_lstats *lb_stats; 1085eaa0bd8SEric Dumazet u64 tbytes, tpackets; 1095eaa0bd8SEric Dumazet unsigned int start; 1101da177e4SLinus Torvalds 111a7855c78SEric Dumazet lb_stats = per_cpu_ptr(dev->lstats, i); 1125eaa0bd8SEric Dumazet do { 11357a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&lb_stats->syncp); 1145eaa0bd8SEric Dumazet tbytes = lb_stats->bytes; 1155eaa0bd8SEric Dumazet tpackets = lb_stats->packets; 11657a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&lb_stats->syncp, start)); 1175eaa0bd8SEric Dumazet bytes += tbytes; 1185eaa0bd8SEric Dumazet packets += tpackets; 1191da177e4SLinus Torvalds } 1205175c378SEric Dumazet stats->rx_packets = packets; 1215175c378SEric Dumazet stats->tx_packets = packets; 1225175c378SEric Dumazet stats->rx_bytes = bytes; 1235175c378SEric Dumazet stats->tx_bytes = bytes; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1267fa6b066SStephen Hemminger static u32 always_on(struct net_device *dev) 1271da177e4SLinus Torvalds { 1281da177e4SLinus Torvalds return 1; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1316df014cfSEzequiel Lara Gomez static int loopback_get_ts_info(struct net_device *netdev, 1326df014cfSEzequiel Lara Gomez struct ethtool_ts_info *ts_info) 1336df014cfSEzequiel Lara Gomez { 1346df014cfSEzequiel Lara Gomez ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | 1356df014cfSEzequiel Lara Gomez SOF_TIMESTAMPING_RX_SOFTWARE | 1366df014cfSEzequiel Lara Gomez SOF_TIMESTAMPING_SOFTWARE; 1376df014cfSEzequiel Lara Gomez 1386df014cfSEzequiel Lara Gomez ts_info->phc_index = -1; 1396df014cfSEzequiel Lara Gomez 1406df014cfSEzequiel Lara Gomez return 0; 1416df014cfSEzequiel Lara Gomez }; 1426df014cfSEzequiel Lara Gomez 1437282d491SJeff Garzik static const struct ethtool_ops loopback_ethtool_ops = { 1447fa6b066SStephen Hemminger .get_link = always_on, 1456df014cfSEzequiel Lara Gomez .get_ts_info = loopback_get_ts_info, 1461da177e4SLinus Torvalds }; 1471da177e4SLinus Torvalds 1485f6d88b9SEric W. Biederman static int loopback_dev_init(struct net_device *dev) 1495f6d88b9SEric W. Biederman { 1501c213bd2SWANG Cong dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats); 151a7855c78SEric Dumazet if (!dev->lstats) 1525f6d88b9SEric W. Biederman return -ENOMEM; 1535f6d88b9SEric W. Biederman return 0; 1545f6d88b9SEric W. Biederman } 1555f6d88b9SEric W. Biederman 1565f6d88b9SEric W. Biederman static void loopback_dev_free(struct net_device *dev) 1575f6d88b9SEric W. Biederman { 158e05e9070SEric W. Biederman dev_net(dev)->loopback_dev = NULL; 159a7855c78SEric Dumazet free_percpu(dev->lstats); 1605f6d88b9SEric W. Biederman } 1615f6d88b9SEric W. Biederman 162c02373bfSStephen Hemminger static const struct net_device_ops loopback_ops = { 163c02373bfSStephen Hemminger .ndo_init = loopback_dev_init, 16400829823SStephen Hemminger .ndo_start_xmit = loopback_xmit, 1656b10de38SEric Dumazet .ndo_get_stats64 = loopback_get_stats64, 16625f929fbSWANG Cong .ndo_set_mac_address = eth_mac_addr, 167c02373bfSStephen Hemminger }; 168c02373bfSStephen Hemminger 169b3407c8eSEzequiel Lara Gomez /* The loopback device is special. There is only one instance 1709e0db4b1SEric W. Biederman * per network namespace. 1717fa6b066SStephen Hemminger */ 172854d8363SDaniel Lezcano static void loopback_setup(struct net_device *dev) 173854d8363SDaniel Lezcano { 1740cf833aeSEric Dumazet dev->mtu = 64 * 1024; 175854d8363SDaniel Lezcano dev->hard_header_len = ETH_HLEN; /* 14 */ 176217e6fa2SWillem de Bruijn dev->min_header_len = ETH_HLEN; /* 14 */ 177854d8363SDaniel Lezcano dev->addr_len = ETH_ALEN; /* 6 */ 178854d8363SDaniel Lezcano dev->type = ARPHRD_LOOPBACK; /* 0x0001*/ 179854d8363SDaniel Lezcano dev->flags = IFF_LOOPBACK; 180e65db2b7SPhil Sutter dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; 18102875878SEric Dumazet netif_keep_dst(dev); 182f6c382fcSMarcelo Ricardo Leitner dev->hw_features = NETIF_F_GSO_SOFTWARE; 183854d8363SDaniel Lezcano dev->features = NETIF_F_SG | NETIF_F_FRAGLIST 184f6c382fcSMarcelo Ricardo Leitner | NETIF_F_GSO_SOFTWARE 18534324dc2SMichał Mirosław | NETIF_F_HW_CSUM 186cf0bdefdSMichał Mirosław | NETIF_F_RXCSUM 18753692b1dSTom Herbert | NETIF_F_SCTP_CRC 188854d8363SDaniel Lezcano | NETIF_F_HIGHDMA 189ce286d32SEric W. Biederman | NETIF_F_LLTX 1900553c891SKrishna Kumar | NETIF_F_NETNS_LOCAL 191eed2a12fSMahesh Bandewar | NETIF_F_VLAN_CHALLENGED 192eed2a12fSMahesh Bandewar | NETIF_F_LOOPBACK; 193854d8363SDaniel Lezcano dev->ethtool_ops = &loopback_ethtool_ops; 1943b04dddeSStephen Hemminger dev->header_ops = ð_header_ops; 195c02373bfSStephen Hemminger dev->netdev_ops = &loopback_ops; 196cf124db5SDavid S. Miller dev->needs_free_netdev = true; 197cf124db5SDavid S. Miller dev->priv_destructor = loopback_dev_free; 198854d8363SDaniel Lezcano } 199de3cb747SDaniel Lezcano 20022783649SRalf Baechle /* Setup and register the loopback device. */ 2014665079cSPavel Emelyanov static __net_init int loopback_net_init(struct net *net) 2021da177e4SLinus Torvalds { 203854d8363SDaniel Lezcano struct net_device *dev; 204854d8363SDaniel Lezcano int err; 205aeed9e82SHerbert Xu 206854d8363SDaniel Lezcano err = -ENOMEM; 207c835a677STom Gundersen dev = alloc_netdev(0, "lo", NET_NAME_UNKNOWN, loopback_setup); 208854d8363SDaniel Lezcano if (!dev) 209854d8363SDaniel Lezcano goto out; 210854d8363SDaniel Lezcano 211c346dca1SYOSHIFUJI Hideaki dev_net_set(dev, net); 212854d8363SDaniel Lezcano err = register_netdev(dev); 213854d8363SDaniel Lezcano if (err) 214854d8363SDaniel Lezcano goto out_free_netdev; 215854d8363SDaniel Lezcano 2161fb9489bSPavel Emelyanov BUG_ON(dev->ifindex != LOOPBACK_IFINDEX); 2172774c7abSEric W. Biederman net->loopback_dev = dev; 2189d6dda32SPavel Emelyanov return 0; 219854d8363SDaniel Lezcano 220854d8363SDaniel Lezcano out_free_netdev: 221854d8363SDaniel Lezcano free_netdev(dev); 2229d6dda32SPavel Emelyanov out: 22309ad9bc7SOctavian Purdila if (net_eq(net, &init_net)) 2249d6dda32SPavel Emelyanov panic("loopback: Failed to register netdevice: %d\n", err); 2259d6dda32SPavel Emelyanov return err; 226854d8363SDaniel Lezcano } 22760903f2cSAdrian Bunk 228505d4f73SEric W. Biederman /* Registered in net/core/dev.c */ 229505d4f73SEric W. Biederman struct pernet_operations __net_initdata loopback_net_ops = { 2302774c7abSEric W. Biederman .init = loopback_net_init, 2312774c7abSEric W. Biederman }; 232