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> 601da177e4SLinus Torvalds 615175c378SEric Dumazet struct pcpu_lstats { 625175c378SEric Dumazet unsigned long packets; 635175c378SEric Dumazet unsigned long bytes; 645175c378SEric Dumazet }; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #define LOOPBACK_OVERHEAD (128 + MAX_HEADER + 16 + 16) 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* KISS: just allocate small chunks and copy bits. 691da177e4SLinus Torvalds * 701da177e4SLinus Torvalds * So, in fact, this is documentation, explaining what we expect 711da177e4SLinus Torvalds * of largesending device modulo TCP checksum, which is ignored for loopback. 721da177e4SLinus Torvalds */ 731da177e4SLinus Torvalds 74d2ae1d2fSChuck Ebbert #ifdef LOOPBACK_TSO 751da177e4SLinus Torvalds static void emulate_large_send_offload(struct sk_buff *skb) 761da177e4SLinus Torvalds { 77eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph = ip_hdr(skb); 78d56f90a7SArnaldo Carvalho de Melo struct tcphdr *th = (struct tcphdr *)(skb_network_header(skb) + 79d56f90a7SArnaldo Carvalho de Melo (iph->ihl * 4)); 801da177e4SLinus Torvalds unsigned int doffset = (iph->ihl + th->doff) * 4; 817967168cSHerbert Xu unsigned int mtu = skb_shinfo(skb)->gso_size + doffset; 821da177e4SLinus Torvalds unsigned int offset = 0; 831da177e4SLinus Torvalds u32 seq = ntohl(th->seq); 841da177e4SLinus Torvalds u16 id = ntohs(iph->id); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds while (offset + doffset < skb->len) { 871da177e4SLinus Torvalds unsigned int frag_size = min(mtu, skb->len - offset) - doffset; 881da177e4SLinus Torvalds struct sk_buff *nskb = alloc_skb(mtu + 32, GFP_ATOMIC); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds if (!nskb) 911da177e4SLinus Torvalds break; 921da177e4SLinus Torvalds skb_reserve(nskb, 32); 9348d49d0cSArnaldo Carvalho de Melo skb_set_mac_header(nskb, -ETH_HLEN); 94c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(nskb); 95eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(nskb); 9627d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data(nskb, skb_network_header(skb), 9727d7ff46SArnaldo Carvalho de Melo doffset); 981da177e4SLinus Torvalds if (skb_copy_bits(skb, 991da177e4SLinus Torvalds doffset + offset, 1001da177e4SLinus Torvalds nskb->data + doffset, 1011da177e4SLinus Torvalds frag_size)) 1021da177e4SLinus Torvalds BUG(); 1031da177e4SLinus Torvalds skb_put(nskb, doffset + frag_size); 1041da177e4SLinus Torvalds nskb->ip_summed = CHECKSUM_UNNECESSARY; 1051da177e4SLinus Torvalds nskb->dev = skb->dev; 1061da177e4SLinus Torvalds nskb->priority = skb->priority; 1071da177e4SLinus Torvalds nskb->protocol = skb->protocol; 1081da177e4SLinus Torvalds nskb->dst = dst_clone(skb->dst); 1091da177e4SLinus Torvalds memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); 1101da177e4SLinus Torvalds nskb->pkt_type = skb->pkt_type; 1111da177e4SLinus Torvalds 112d56f90a7SArnaldo Carvalho de Melo th = (struct tcphdr *)(skb_network_header(nskb) + iph->ihl * 4); 1131da177e4SLinus Torvalds iph->tot_len = htons(frag_size + doffset); 1141da177e4SLinus Torvalds iph->id = htons(id); 1151da177e4SLinus Torvalds iph->check = 0; 1161da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *) iph, iph->ihl); 1171da177e4SLinus Torvalds th->seq = htonl(seq); 1181da177e4SLinus Torvalds if (offset + doffset + frag_size < skb->len) 1191da177e4SLinus Torvalds th->fin = th->psh = 0; 1201da177e4SLinus Torvalds netif_rx(nskb); 1211da177e4SLinus Torvalds offset += frag_size; 1221da177e4SLinus Torvalds seq += frag_size; 1231da177e4SLinus Torvalds id++; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds dev_kfree_skb(skb); 1271da177e4SLinus Torvalds } 128d2ae1d2fSChuck Ebbert #endif /* LOOPBACK_TSO */ 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /* 1311da177e4SLinus Torvalds * The higher levels take care of making this non-reentrant (it's 1321da177e4SLinus Torvalds * called with bh's disabled). 1331da177e4SLinus Torvalds */ 1341da177e4SLinus Torvalds static int loopback_xmit(struct sk_buff *skb, struct net_device *dev) 1351da177e4SLinus Torvalds { 136*5f6d88b9SEric W. Biederman struct pcpu_lstats *pcpu_lstats, *lb_stats; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds skb_orphan(skb); 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb,dev); 1411da177e4SLinus Torvalds #ifndef LOOPBACK_MUST_CHECKSUM 1421da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_UNNECESSARY; 1431da177e4SLinus Torvalds #endif 1441da177e4SLinus Torvalds 145d2ae1d2fSChuck Ebbert #ifdef LOOPBACK_TSO 14689114afdSHerbert Xu if (skb_is_gso(skb)) { 1471da177e4SLinus Torvalds BUG_ON(skb->protocol != htons(ETH_P_IP)); 148eddc9ec5SArnaldo Carvalho de Melo BUG_ON(ip_hdr(skb)->protocol != IPPROTO_TCP); 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds emulate_large_send_offload(skb); 1511da177e4SLinus Torvalds return 0; 1521da177e4SLinus Torvalds } 153d2ae1d2fSChuck Ebbert #endif 1541da177e4SLinus Torvalds dev->last_rx = jiffies; 1551da177e4SLinus Torvalds 15658f53974SEric Dumazet /* it's OK to use __get_cpu_var() because BHs are off */ 157*5f6d88b9SEric W. Biederman pcpu_lstats = netdev_priv(dev); 158*5f6d88b9SEric W. Biederman lb_stats = per_cpu_ptr(pcpu_lstats, smp_processor_id()); 1595175c378SEric Dumazet lb_stats->bytes += skb->len; 1605175c378SEric Dumazet lb_stats->packets++; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds netif_rx(skb); 1631da177e4SLinus Torvalds 16458f53974SEric Dumazet return 0; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds static struct net_device_stats *get_stats(struct net_device *dev) 1681da177e4SLinus Torvalds { 169*5f6d88b9SEric W. Biederman const struct pcpu_lstats *pcpu_lstats; 17033036807SEric Dumazet struct net_device_stats *stats = &dev->stats; 1715175c378SEric Dumazet unsigned long bytes = 0; 1725175c378SEric Dumazet unsigned long packets = 0; 1731da177e4SLinus Torvalds int i; 1741da177e4SLinus Torvalds 175*5f6d88b9SEric W. Biederman pcpu_lstats = netdev_priv(dev); 1760fed4846SKAMEZAWA Hiroyuki for_each_possible_cpu(i) { 1775175c378SEric Dumazet const struct pcpu_lstats *lb_stats; 1781da177e4SLinus Torvalds 179*5f6d88b9SEric W. Biederman lb_stats = per_cpu_ptr(pcpu_lstats, i); 1805175c378SEric Dumazet bytes += lb_stats->bytes; 1815175c378SEric Dumazet packets += lb_stats->packets; 1821da177e4SLinus Torvalds } 1835175c378SEric Dumazet stats->rx_packets = packets; 1845175c378SEric Dumazet stats->tx_packets = packets; 1855175c378SEric Dumazet stats->rx_bytes = bytes; 1865175c378SEric Dumazet stats->tx_bytes = bytes; 1871da177e4SLinus Torvalds return stats; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1907fa6b066SStephen Hemminger static u32 always_on(struct net_device *dev) 1911da177e4SLinus Torvalds { 1921da177e4SLinus Torvalds return 1; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1957282d491SJeff Garzik static const struct ethtool_ops loopback_ethtool_ops = { 1967fa6b066SStephen Hemminger .get_link = always_on, 1971da177e4SLinus Torvalds .set_tso = ethtool_op_set_tso, 1987fa6b066SStephen Hemminger .get_tx_csum = always_on, 1997fa6b066SStephen Hemminger .get_sg = always_on, 2007fa6b066SStephen Hemminger .get_rx_csum = always_on, 2011da177e4SLinus Torvalds }; 2021da177e4SLinus Torvalds 203*5f6d88b9SEric W. Biederman static int loopback_dev_init(struct net_device *dev) 204*5f6d88b9SEric W. Biederman { 205*5f6d88b9SEric W. Biederman struct pcpu_lstats *lstats; 206*5f6d88b9SEric W. Biederman 207*5f6d88b9SEric W. Biederman lstats = alloc_percpu(struct pcpu_lstats); 208*5f6d88b9SEric W. Biederman if (!lstats) 209*5f6d88b9SEric W. Biederman return -ENOMEM; 210*5f6d88b9SEric W. Biederman 211*5f6d88b9SEric W. Biederman dev->priv = lstats; 212*5f6d88b9SEric W. Biederman return 0; 213*5f6d88b9SEric W. Biederman } 214*5f6d88b9SEric W. Biederman 215*5f6d88b9SEric W. Biederman static void loopback_dev_free(struct net_device *dev) 216*5f6d88b9SEric W. Biederman { 217*5f6d88b9SEric W. Biederman struct pcpu_lstats *lstats = netdev_priv(dev); 218*5f6d88b9SEric W. Biederman 219*5f6d88b9SEric W. Biederman free_percpu(lstats); 220*5f6d88b9SEric W. Biederman free_netdev(dev); 221*5f6d88b9SEric W. Biederman } 222*5f6d88b9SEric W. Biederman 2237fa6b066SStephen Hemminger /* 2247fa6b066SStephen Hemminger * The loopback device is special. There is only one instance and 2257fa6b066SStephen Hemminger * it is statically allocated. Don't do this for other devices. 2267fa6b066SStephen Hemminger */ 227854d8363SDaniel Lezcano static void loopback_setup(struct net_device *dev) 228854d8363SDaniel Lezcano { 229854d8363SDaniel Lezcano dev->get_stats = &get_stats; 230854d8363SDaniel Lezcano dev->mtu = (16 * 1024) + 20 + 20 + 12; 231854d8363SDaniel Lezcano dev->hard_start_xmit = loopback_xmit; 232854d8363SDaniel Lezcano dev->hard_header = eth_header; 233854d8363SDaniel Lezcano dev->hard_header_cache = eth_header_cache; 234854d8363SDaniel Lezcano dev->header_cache_update = eth_header_cache_update; 235854d8363SDaniel Lezcano dev->hard_header_len = ETH_HLEN; /* 14 */ 236854d8363SDaniel Lezcano dev->addr_len = ETH_ALEN; /* 6 */ 237854d8363SDaniel Lezcano dev->tx_queue_len = 0; 238854d8363SDaniel Lezcano dev->type = ARPHRD_LOOPBACK; /* 0x0001*/ 239854d8363SDaniel Lezcano dev->rebuild_header = eth_rebuild_header; 240854d8363SDaniel Lezcano dev->flags = IFF_LOOPBACK; 241854d8363SDaniel Lezcano dev->features = NETIF_F_SG | NETIF_F_FRAGLIST 242d2ae1d2fSChuck Ebbert #ifdef LOOPBACK_TSO 243d2ae1d2fSChuck Ebbert | NETIF_F_TSO 244d2ae1d2fSChuck Ebbert #endif 245854d8363SDaniel Lezcano | NETIF_F_NO_CSUM 246854d8363SDaniel Lezcano | NETIF_F_HIGHDMA 247ce286d32SEric W. Biederman | NETIF_F_LLTX 248ce286d32SEric W. Biederman | NETIF_F_NETNS_LOCAL, 249854d8363SDaniel Lezcano dev->ethtool_ops = &loopback_ethtool_ops; 250*5f6d88b9SEric W. Biederman dev->init = loopback_dev_init; 251*5f6d88b9SEric W. Biederman dev->destructor = loopback_dev_free; 252854d8363SDaniel Lezcano } 253de3cb747SDaniel Lezcano 25422783649SRalf Baechle /* Setup and register the loopback device. */ 25560903f2cSAdrian Bunk static int __init loopback_init(void) 2561da177e4SLinus Torvalds { 257854d8363SDaniel Lezcano struct net_device *dev; 258854d8363SDaniel Lezcano int err; 259aeed9e82SHerbert Xu 260854d8363SDaniel Lezcano err = -ENOMEM; 261854d8363SDaniel Lezcano dev = alloc_netdev(0, "lo", loopback_setup); 262854d8363SDaniel Lezcano if (!dev) 263854d8363SDaniel Lezcano goto out; 264854d8363SDaniel Lezcano 265854d8363SDaniel Lezcano err = register_netdev(dev); 266854d8363SDaniel Lezcano if (err) 267854d8363SDaniel Lezcano goto out_free_netdev; 268854d8363SDaniel Lezcano 269854d8363SDaniel Lezcano err = 0; 270854d8363SDaniel Lezcano loopback_dev = dev; 271854d8363SDaniel Lezcano 272854d8363SDaniel Lezcano out: 273aeed9e82SHerbert Xu if (err) 274aeed9e82SHerbert Xu panic("loopback: Failed to register netdevice: %d\n", err); 275aeed9e82SHerbert Xu return err; 2761da177e4SLinus Torvalds 277854d8363SDaniel Lezcano out_free_netdev: 278854d8363SDaniel Lezcano free_netdev(dev); 279854d8363SDaniel Lezcano goto out; 280854d8363SDaniel Lezcano } 28160903f2cSAdrian Bunk 282854d8363SDaniel Lezcano fs_initcall(loopback_init); 283854d8363SDaniel Lezcano 284854d8363SDaniel Lezcano struct net_device *loopback_dev; 2851da177e4SLinus Torvalds EXPORT_SYMBOL(loopback_dev); 286