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 #include <linux/init.h> 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #include <asm/system.h> 451da177e4SLinus Torvalds #include <asm/uaccess.h> 461da177e4SLinus Torvalds #include <asm/io.h> 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #include <linux/inet.h> 491da177e4SLinus Torvalds #include <linux/netdevice.h> 501da177e4SLinus Torvalds #include <linux/etherdevice.h> 511da177e4SLinus Torvalds #include <linux/skbuff.h> 521da177e4SLinus Torvalds #include <linux/ethtool.h> 531da177e4SLinus Torvalds #include <net/sock.h> 541da177e4SLinus Torvalds #include <net/checksum.h> 551da177e4SLinus Torvalds #include <linux/if_ether.h> /* For the statistics structure. */ 561da177e4SLinus Torvalds #include <linux/if_arp.h> /* For ARPHRD_ETHER */ 571da177e4SLinus Torvalds #include <linux/ip.h> 581da177e4SLinus Torvalds #include <linux/tcp.h> 591da177e4SLinus Torvalds #include <linux/percpu.h> 602774c7abSEric W. Biederman #include <net/net_namespace.h> 611da177e4SLinus Torvalds 625175c378SEric Dumazet struct pcpu_lstats { 635175c378SEric Dumazet unsigned long packets; 645175c378SEric Dumazet unsigned long bytes; 655175c378SEric Dumazet }; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* 681da177e4SLinus Torvalds * The higher levels take care of making this non-reentrant (it's 691da177e4SLinus Torvalds * called with bh's disabled). 701da177e4SLinus Torvalds */ 711da177e4SLinus Torvalds static int loopback_xmit(struct sk_buff *skb, struct net_device *dev) 721da177e4SLinus Torvalds { 735f6d88b9SEric W. Biederman struct pcpu_lstats *pcpu_lstats, *lb_stats; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds skb_orphan(skb); 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb,dev); 781da177e4SLinus Torvalds 799e0db4b1SEric W. Biederman /* it's OK to use per_cpu_ptr() because BHs are off */ 80e3c50d5dSDavid S. Miller pcpu_lstats = dev->ml_priv; 815f6d88b9SEric W. Biederman lb_stats = per_cpu_ptr(pcpu_lstats, smp_processor_id()); 825175c378SEric Dumazet lb_stats->bytes += skb->len; 835175c378SEric Dumazet lb_stats->packets++; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds netif_rx(skb); 861da177e4SLinus Torvalds 8758f53974SEric Dumazet return 0; 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds static struct net_device_stats *get_stats(struct net_device *dev) 911da177e4SLinus Torvalds { 925f6d88b9SEric W. Biederman const struct pcpu_lstats *pcpu_lstats; 9333036807SEric Dumazet struct net_device_stats *stats = &dev->stats; 945175c378SEric Dumazet unsigned long bytes = 0; 955175c378SEric Dumazet unsigned long packets = 0; 961da177e4SLinus Torvalds int i; 971da177e4SLinus Torvalds 98e3c50d5dSDavid S. Miller pcpu_lstats = dev->ml_priv; 990fed4846SKAMEZAWA Hiroyuki for_each_possible_cpu(i) { 1005175c378SEric Dumazet const struct pcpu_lstats *lb_stats; 1011da177e4SLinus Torvalds 1025f6d88b9SEric W. Biederman lb_stats = per_cpu_ptr(pcpu_lstats, i); 1035175c378SEric Dumazet bytes += lb_stats->bytes; 1045175c378SEric Dumazet packets += lb_stats->packets; 1051da177e4SLinus Torvalds } 1065175c378SEric Dumazet stats->rx_packets = packets; 1075175c378SEric Dumazet stats->tx_packets = packets; 1085175c378SEric Dumazet stats->rx_bytes = bytes; 1095175c378SEric Dumazet stats->tx_bytes = bytes; 1101da177e4SLinus Torvalds return stats; 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1137fa6b066SStephen Hemminger static u32 always_on(struct net_device *dev) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds return 1; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1187282d491SJeff Garzik static const struct ethtool_ops loopback_ethtool_ops = { 1197fa6b066SStephen Hemminger .get_link = always_on, 1201da177e4SLinus Torvalds .set_tso = ethtool_op_set_tso, 1217fa6b066SStephen Hemminger .get_tx_csum = always_on, 1227fa6b066SStephen Hemminger .get_sg = always_on, 1237fa6b066SStephen Hemminger .get_rx_csum = always_on, 1241da177e4SLinus Torvalds }; 1251da177e4SLinus Torvalds 1265f6d88b9SEric W. Biederman static int loopback_dev_init(struct net_device *dev) 1275f6d88b9SEric W. Biederman { 1285f6d88b9SEric W. Biederman struct pcpu_lstats *lstats; 1295f6d88b9SEric W. Biederman 1305f6d88b9SEric W. Biederman lstats = alloc_percpu(struct pcpu_lstats); 1315f6d88b9SEric W. Biederman if (!lstats) 1325f6d88b9SEric W. Biederman return -ENOMEM; 1335f6d88b9SEric W. Biederman 134e3c50d5dSDavid S. Miller dev->ml_priv = lstats; 1355f6d88b9SEric W. Biederman return 0; 1365f6d88b9SEric W. Biederman } 1375f6d88b9SEric W. Biederman 1385f6d88b9SEric W. Biederman static void loopback_dev_free(struct net_device *dev) 1395f6d88b9SEric W. Biederman { 140e3c50d5dSDavid S. Miller struct pcpu_lstats *lstats = dev->ml_priv; 1415f6d88b9SEric W. Biederman 1425f6d88b9SEric W. Biederman free_percpu(lstats); 1435f6d88b9SEric W. Biederman free_netdev(dev); 1445f6d88b9SEric W. Biederman } 1455f6d88b9SEric W. Biederman 1467fa6b066SStephen Hemminger /* 1479e0db4b1SEric W. Biederman * The loopback device is special. There is only one instance 1489e0db4b1SEric W. Biederman * per network namespace. 1497fa6b066SStephen Hemminger */ 150854d8363SDaniel Lezcano static void loopback_setup(struct net_device *dev) 151854d8363SDaniel Lezcano { 152854d8363SDaniel Lezcano dev->get_stats = &get_stats; 153854d8363SDaniel Lezcano dev->mtu = (16 * 1024) + 20 + 20 + 12; 154854d8363SDaniel Lezcano dev->hard_start_xmit = loopback_xmit; 155854d8363SDaniel Lezcano dev->hard_header_len = ETH_HLEN; /* 14 */ 156854d8363SDaniel Lezcano dev->addr_len = ETH_ALEN; /* 6 */ 157854d8363SDaniel Lezcano dev->tx_queue_len = 0; 158854d8363SDaniel Lezcano dev->type = ARPHRD_LOOPBACK; /* 0x0001*/ 159854d8363SDaniel Lezcano dev->flags = IFF_LOOPBACK; 160854d8363SDaniel Lezcano dev->features = NETIF_F_SG | NETIF_F_FRAGLIST 161d2ae1d2fSChuck Ebbert | NETIF_F_TSO 162854d8363SDaniel Lezcano | NETIF_F_NO_CSUM 163854d8363SDaniel Lezcano | NETIF_F_HIGHDMA 164ce286d32SEric W. Biederman | NETIF_F_LLTX 1652d2c54e3SEmil Medve | NETIF_F_NETNS_LOCAL; 166854d8363SDaniel Lezcano dev->ethtool_ops = &loopback_ethtool_ops; 1673b04dddeSStephen Hemminger dev->header_ops = ð_header_ops; 1685f6d88b9SEric W. Biederman dev->init = loopback_dev_init; 1695f6d88b9SEric W. Biederman dev->destructor = loopback_dev_free; 170854d8363SDaniel Lezcano } 171de3cb747SDaniel Lezcano 17222783649SRalf Baechle /* Setup and register the loopback device. */ 1734665079cSPavel Emelyanov static __net_init int loopback_net_init(struct net *net) 1741da177e4SLinus Torvalds { 175854d8363SDaniel Lezcano struct net_device *dev; 176854d8363SDaniel Lezcano int err; 177aeed9e82SHerbert Xu 178854d8363SDaniel Lezcano err = -ENOMEM; 179854d8363SDaniel Lezcano dev = alloc_netdev(0, "lo", loopback_setup); 180854d8363SDaniel Lezcano if (!dev) 181854d8363SDaniel Lezcano goto out; 182854d8363SDaniel Lezcano 183c346dca1SYOSHIFUJI Hideaki dev_net_set(dev, net); 184854d8363SDaniel Lezcano err = register_netdev(dev); 185854d8363SDaniel Lezcano if (err) 186854d8363SDaniel Lezcano goto out_free_netdev; 187854d8363SDaniel Lezcano 1882774c7abSEric W. Biederman net->loopback_dev = dev; 1899d6dda32SPavel Emelyanov return 0; 190854d8363SDaniel Lezcano 1911da177e4SLinus Torvalds 192854d8363SDaniel Lezcano out_free_netdev: 193854d8363SDaniel Lezcano free_netdev(dev); 1949d6dda32SPavel Emelyanov out: 1959d6dda32SPavel Emelyanov if (net == &init_net) 1969d6dda32SPavel Emelyanov panic("loopback: Failed to register netdevice: %d\n", err); 1979d6dda32SPavel Emelyanov return err; 198854d8363SDaniel Lezcano } 19960903f2cSAdrian Bunk 2004665079cSPavel Emelyanov static __net_exit void loopback_net_exit(struct net *net) 2012774c7abSEric W. Biederman { 2022774c7abSEric W. Biederman struct net_device *dev = net->loopback_dev; 203854d8363SDaniel Lezcano 2042774c7abSEric W. Biederman unregister_netdev(dev); 2052774c7abSEric W. Biederman } 2062774c7abSEric W. Biederman 207*505d4f73SEric W. Biederman /* Registered in net/core/dev.c */ 208*505d4f73SEric W. Biederman struct pernet_operations __net_initdata loopback_net_ops = { 2092774c7abSEric W. Biederman .init = loopback_net_init, 2102774c7abSEric W. Biederman .exit = loopback_net_exit, 2112774c7abSEric W. Biederman }; 212