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 /* KISS: just allocate small chunks and copy bits. 681da177e4SLinus Torvalds * 691da177e4SLinus Torvalds * So, in fact, this is documentation, explaining what we expect 701da177e4SLinus Torvalds * of largesending device modulo TCP checksum, which is ignored for loopback. 711da177e4SLinus Torvalds */ 721da177e4SLinus Torvalds 73d2ae1d2fSChuck Ebbert #ifdef LOOPBACK_TSO 741da177e4SLinus Torvalds static void emulate_large_send_offload(struct sk_buff *skb) 751da177e4SLinus Torvalds { 76eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph = ip_hdr(skb); 77d56f90a7SArnaldo Carvalho de Melo struct tcphdr *th = (struct tcphdr *)(skb_network_header(skb) + 78d56f90a7SArnaldo Carvalho de Melo (iph->ihl * 4)); 791da177e4SLinus Torvalds unsigned int doffset = (iph->ihl + th->doff) * 4; 807967168cSHerbert Xu unsigned int mtu = skb_shinfo(skb)->gso_size + doffset; 811da177e4SLinus Torvalds unsigned int offset = 0; 821da177e4SLinus Torvalds u32 seq = ntohl(th->seq); 831da177e4SLinus Torvalds u16 id = ntohs(iph->id); 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds while (offset + doffset < skb->len) { 861da177e4SLinus Torvalds unsigned int frag_size = min(mtu, skb->len - offset) - doffset; 871da177e4SLinus Torvalds struct sk_buff *nskb = alloc_skb(mtu + 32, GFP_ATOMIC); 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds if (!nskb) 901da177e4SLinus Torvalds break; 911da177e4SLinus Torvalds skb_reserve(nskb, 32); 9248d49d0cSArnaldo Carvalho de Melo skb_set_mac_header(nskb, -ETH_HLEN); 93c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(nskb); 94eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(nskb); 9527d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data(nskb, skb_network_header(skb), 9627d7ff46SArnaldo Carvalho de Melo doffset); 971da177e4SLinus Torvalds if (skb_copy_bits(skb, 981da177e4SLinus Torvalds doffset + offset, 991da177e4SLinus Torvalds nskb->data + doffset, 1001da177e4SLinus Torvalds frag_size)) 1011da177e4SLinus Torvalds BUG(); 1021da177e4SLinus Torvalds skb_put(nskb, doffset + frag_size); 1031da177e4SLinus Torvalds nskb->ip_summed = CHECKSUM_UNNECESSARY; 1041da177e4SLinus Torvalds nskb->dev = skb->dev; 1051da177e4SLinus Torvalds nskb->priority = skb->priority; 1061da177e4SLinus Torvalds nskb->protocol = skb->protocol; 1071da177e4SLinus Torvalds nskb->dst = dst_clone(skb->dst); 1081da177e4SLinus Torvalds memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); 1091da177e4SLinus Torvalds nskb->pkt_type = skb->pkt_type; 1101da177e4SLinus Torvalds 111d56f90a7SArnaldo Carvalho de Melo th = (struct tcphdr *)(skb_network_header(nskb) + iph->ihl * 4); 1121da177e4SLinus Torvalds iph->tot_len = htons(frag_size + doffset); 1131da177e4SLinus Torvalds iph->id = htons(id); 1141da177e4SLinus Torvalds iph->check = 0; 1151da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *) iph, iph->ihl); 1161da177e4SLinus Torvalds th->seq = htonl(seq); 1171da177e4SLinus Torvalds if (offset + doffset + frag_size < skb->len) 1181da177e4SLinus Torvalds th->fin = th->psh = 0; 1191da177e4SLinus Torvalds netif_rx(nskb); 1201da177e4SLinus Torvalds offset += frag_size; 1211da177e4SLinus Torvalds seq += frag_size; 1221da177e4SLinus Torvalds id++; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds dev_kfree_skb(skb); 1261da177e4SLinus Torvalds } 127d2ae1d2fSChuck Ebbert #endif /* LOOPBACK_TSO */ 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * The higher levels take care of making this non-reentrant (it's 1311da177e4SLinus Torvalds * called with bh's disabled). 1321da177e4SLinus Torvalds */ 1331da177e4SLinus Torvalds static int loopback_xmit(struct sk_buff *skb, struct net_device *dev) 1341da177e4SLinus Torvalds { 1355f6d88b9SEric W. Biederman struct pcpu_lstats *pcpu_lstats, *lb_stats; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds skb_orphan(skb); 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb,dev); 1401da177e4SLinus Torvalds #ifndef LOOPBACK_MUST_CHECKSUM 1411da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_UNNECESSARY; 1421da177e4SLinus Torvalds #endif 1431da177e4SLinus Torvalds 144d2ae1d2fSChuck Ebbert #ifdef LOOPBACK_TSO 14589114afdSHerbert Xu if (skb_is_gso(skb)) { 1461da177e4SLinus Torvalds BUG_ON(skb->protocol != htons(ETH_P_IP)); 147eddc9ec5SArnaldo Carvalho de Melo BUG_ON(ip_hdr(skb)->protocol != IPPROTO_TCP); 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds emulate_large_send_offload(skb); 1501da177e4SLinus Torvalds return 0; 1511da177e4SLinus Torvalds } 152d2ae1d2fSChuck Ebbert #endif 1531da177e4SLinus Torvalds dev->last_rx = jiffies; 1541da177e4SLinus Torvalds 1559e0db4b1SEric W. Biederman /* it's OK to use per_cpu_ptr() because BHs are off */ 1565f6d88b9SEric W. Biederman pcpu_lstats = netdev_priv(dev); 1575f6d88b9SEric W. Biederman lb_stats = per_cpu_ptr(pcpu_lstats, smp_processor_id()); 1585175c378SEric Dumazet lb_stats->bytes += skb->len; 1595175c378SEric Dumazet lb_stats->packets++; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds netif_rx(skb); 1621da177e4SLinus Torvalds 16358f53974SEric Dumazet return 0; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds static struct net_device_stats *get_stats(struct net_device *dev) 1671da177e4SLinus Torvalds { 1685f6d88b9SEric W. Biederman const struct pcpu_lstats *pcpu_lstats; 16933036807SEric Dumazet struct net_device_stats *stats = &dev->stats; 1705175c378SEric Dumazet unsigned long bytes = 0; 1715175c378SEric Dumazet unsigned long packets = 0; 1721da177e4SLinus Torvalds int i; 1731da177e4SLinus Torvalds 1745f6d88b9SEric W. Biederman pcpu_lstats = netdev_priv(dev); 1750fed4846SKAMEZAWA Hiroyuki for_each_possible_cpu(i) { 1765175c378SEric Dumazet const struct pcpu_lstats *lb_stats; 1771da177e4SLinus Torvalds 1785f6d88b9SEric W. Biederman lb_stats = per_cpu_ptr(pcpu_lstats, i); 1795175c378SEric Dumazet bytes += lb_stats->bytes; 1805175c378SEric Dumazet packets += lb_stats->packets; 1811da177e4SLinus Torvalds } 1825175c378SEric Dumazet stats->rx_packets = packets; 1835175c378SEric Dumazet stats->tx_packets = packets; 1845175c378SEric Dumazet stats->rx_bytes = bytes; 1855175c378SEric Dumazet stats->tx_bytes = bytes; 1861da177e4SLinus Torvalds return stats; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1897fa6b066SStephen Hemminger static u32 always_on(struct net_device *dev) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds return 1; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1947282d491SJeff Garzik static const struct ethtool_ops loopback_ethtool_ops = { 1957fa6b066SStephen Hemminger .get_link = always_on, 1961da177e4SLinus Torvalds .set_tso = ethtool_op_set_tso, 1977fa6b066SStephen Hemminger .get_tx_csum = always_on, 1987fa6b066SStephen Hemminger .get_sg = always_on, 1997fa6b066SStephen Hemminger .get_rx_csum = always_on, 2001da177e4SLinus Torvalds }; 2011da177e4SLinus Torvalds 2025f6d88b9SEric W. Biederman static int loopback_dev_init(struct net_device *dev) 2035f6d88b9SEric W. Biederman { 2045f6d88b9SEric W. Biederman struct pcpu_lstats *lstats; 2055f6d88b9SEric W. Biederman 2065f6d88b9SEric W. Biederman lstats = alloc_percpu(struct pcpu_lstats); 2075f6d88b9SEric W. Biederman if (!lstats) 2085f6d88b9SEric W. Biederman return -ENOMEM; 2095f6d88b9SEric W. Biederman 2105f6d88b9SEric W. Biederman dev->priv = lstats; 2115f6d88b9SEric W. Biederman return 0; 2125f6d88b9SEric W. Biederman } 2135f6d88b9SEric W. Biederman 2145f6d88b9SEric W. Biederman static void loopback_dev_free(struct net_device *dev) 2155f6d88b9SEric W. Biederman { 2165f6d88b9SEric W. Biederman struct pcpu_lstats *lstats = netdev_priv(dev); 2175f6d88b9SEric W. Biederman 2185f6d88b9SEric W. Biederman free_percpu(lstats); 2195f6d88b9SEric W. Biederman free_netdev(dev); 2205f6d88b9SEric W. Biederman } 2215f6d88b9SEric W. Biederman 2227fa6b066SStephen Hemminger /* 2239e0db4b1SEric W. Biederman * The loopback device is special. There is only one instance 2249e0db4b1SEric W. Biederman * per network namespace. 2257fa6b066SStephen Hemminger */ 226854d8363SDaniel Lezcano static void loopback_setup(struct net_device *dev) 227854d8363SDaniel Lezcano { 228854d8363SDaniel Lezcano dev->get_stats = &get_stats; 229854d8363SDaniel Lezcano dev->mtu = (16 * 1024) + 20 + 20 + 12; 230854d8363SDaniel Lezcano dev->hard_start_xmit = loopback_xmit; 231854d8363SDaniel Lezcano dev->hard_header_len = ETH_HLEN; /* 14 */ 232854d8363SDaniel Lezcano dev->addr_len = ETH_ALEN; /* 6 */ 233854d8363SDaniel Lezcano dev->tx_queue_len = 0; 234854d8363SDaniel Lezcano dev->type = ARPHRD_LOOPBACK; /* 0x0001*/ 235854d8363SDaniel Lezcano dev->flags = IFF_LOOPBACK; 236854d8363SDaniel Lezcano dev->features = NETIF_F_SG | NETIF_F_FRAGLIST 237d2ae1d2fSChuck Ebbert #ifdef LOOPBACK_TSO 238d2ae1d2fSChuck Ebbert | NETIF_F_TSO 239d2ae1d2fSChuck Ebbert #endif 240854d8363SDaniel Lezcano | NETIF_F_NO_CSUM 241854d8363SDaniel Lezcano | NETIF_F_HIGHDMA 242ce286d32SEric W. Biederman | NETIF_F_LLTX 2432d2c54e3SEmil Medve | NETIF_F_NETNS_LOCAL; 244854d8363SDaniel Lezcano dev->ethtool_ops = &loopback_ethtool_ops; 2453b04dddeSStephen Hemminger dev->header_ops = ð_header_ops; 2465f6d88b9SEric W. Biederman dev->init = loopback_dev_init; 2475f6d88b9SEric W. Biederman dev->destructor = loopback_dev_free; 248854d8363SDaniel Lezcano } 249de3cb747SDaniel Lezcano 25022783649SRalf Baechle /* Setup and register the loopback device. */ 2514665079cSPavel Emelyanov static __net_init int loopback_net_init(struct net *net) 2521da177e4SLinus Torvalds { 253854d8363SDaniel Lezcano struct net_device *dev; 254854d8363SDaniel Lezcano int err; 255aeed9e82SHerbert Xu 256854d8363SDaniel Lezcano err = -ENOMEM; 257854d8363SDaniel Lezcano dev = alloc_netdev(0, "lo", loopback_setup); 258854d8363SDaniel Lezcano if (!dev) 259854d8363SDaniel Lezcano goto out; 260854d8363SDaniel Lezcano 261*c346dca1SYOSHIFUJI Hideaki dev_net_set(dev, net); 262854d8363SDaniel Lezcano err = register_netdev(dev); 263854d8363SDaniel Lezcano if (err) 264854d8363SDaniel Lezcano goto out_free_netdev; 265854d8363SDaniel Lezcano 2662774c7abSEric W. Biederman net->loopback_dev = dev; 2679d6dda32SPavel Emelyanov return 0; 268854d8363SDaniel Lezcano 2691da177e4SLinus Torvalds 270854d8363SDaniel Lezcano out_free_netdev: 271854d8363SDaniel Lezcano free_netdev(dev); 2729d6dda32SPavel Emelyanov out: 2739d6dda32SPavel Emelyanov if (net == &init_net) 2749d6dda32SPavel Emelyanov panic("loopback: Failed to register netdevice: %d\n", err); 2759d6dda32SPavel Emelyanov return err; 276854d8363SDaniel Lezcano } 27760903f2cSAdrian Bunk 2784665079cSPavel Emelyanov static __net_exit void loopback_net_exit(struct net *net) 2792774c7abSEric W. Biederman { 2802774c7abSEric W. Biederman struct net_device *dev = net->loopback_dev; 281854d8363SDaniel Lezcano 2822774c7abSEric W. Biederman unregister_netdev(dev); 2832774c7abSEric W. Biederman } 2842774c7abSEric W. Biederman 285022cbae6SDenis V. Lunev static struct pernet_operations __net_initdata loopback_net_ops = { 2862774c7abSEric W. Biederman .init = loopback_net_init, 2872774c7abSEric W. Biederman .exit = loopback_net_exit, 2882774c7abSEric W. Biederman }; 2892774c7abSEric W. Biederman 2902774c7abSEric W. Biederman static int __init loopback_init(void) 2912774c7abSEric W. Biederman { 2922774c7abSEric W. Biederman return register_pernet_device(&loopback_net_ops); 2932774c7abSEric W. Biederman } 2942774c7abSEric W. Biederman 295070ac3a2SDenis V. Lunev /* Loopback is special. It should be initialized before any other network 296070ac3a2SDenis V. Lunev * device and network subsystem. 297070ac3a2SDenis V. Lunev */ 2982774c7abSEric W. Biederman fs_initcall(loopback_init); 299