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 611da177e4SLinus Torvalds static DEFINE_PER_CPU(struct net_device_stats, loopback_stats); 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds #define LOOPBACK_OVERHEAD (128 + MAX_HEADER + 16 + 16) 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* KISS: just allocate small chunks and copy bits. 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * So, in fact, this is documentation, explaining what we expect 681da177e4SLinus Torvalds * of largesending device modulo TCP checksum, which is ignored for loopback. 691da177e4SLinus Torvalds */ 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds static void emulate_large_send_offload(struct sk_buff *skb) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds struct iphdr *iph = skb->nh.iph; 741da177e4SLinus Torvalds struct tcphdr *th = (struct tcphdr*)(skb->nh.raw + (iph->ihl * 4)); 751da177e4SLinus Torvalds unsigned int doffset = (iph->ihl + th->doff) * 4; 761da177e4SLinus Torvalds unsigned int mtu = skb_shinfo(skb)->tso_size + doffset; 771da177e4SLinus Torvalds unsigned int offset = 0; 781da177e4SLinus Torvalds u32 seq = ntohl(th->seq); 791da177e4SLinus Torvalds u16 id = ntohs(iph->id); 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds while (offset + doffset < skb->len) { 821da177e4SLinus Torvalds unsigned int frag_size = min(mtu, skb->len - offset) - doffset; 831da177e4SLinus Torvalds struct sk_buff *nskb = alloc_skb(mtu + 32, GFP_ATOMIC); 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds if (!nskb) 861da177e4SLinus Torvalds break; 871da177e4SLinus Torvalds skb_reserve(nskb, 32); 881da177e4SLinus Torvalds nskb->mac.raw = nskb->data - 14; 891da177e4SLinus Torvalds nskb->nh.raw = nskb->data; 901da177e4SLinus Torvalds iph = nskb->nh.iph; 911da177e4SLinus Torvalds memcpy(nskb->data, skb->nh.raw, doffset); 921da177e4SLinus Torvalds if (skb_copy_bits(skb, 931da177e4SLinus Torvalds doffset + offset, 941da177e4SLinus Torvalds nskb->data + doffset, 951da177e4SLinus Torvalds frag_size)) 961da177e4SLinus Torvalds BUG(); 971da177e4SLinus Torvalds skb_put(nskb, doffset + frag_size); 981da177e4SLinus Torvalds nskb->ip_summed = CHECKSUM_UNNECESSARY; 991da177e4SLinus Torvalds nskb->dev = skb->dev; 1001da177e4SLinus Torvalds nskb->priority = skb->priority; 1011da177e4SLinus Torvalds nskb->protocol = skb->protocol; 1021da177e4SLinus Torvalds nskb->dst = dst_clone(skb->dst); 1031da177e4SLinus Torvalds memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); 1041da177e4SLinus Torvalds nskb->pkt_type = skb->pkt_type; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds th = (struct tcphdr*)(nskb->nh.raw + iph->ihl*4); 1071da177e4SLinus Torvalds iph->tot_len = htons(frag_size + doffset); 1081da177e4SLinus Torvalds iph->id = htons(id); 1091da177e4SLinus Torvalds iph->check = 0; 1101da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *) iph, iph->ihl); 1111da177e4SLinus Torvalds th->seq = htonl(seq); 1121da177e4SLinus Torvalds if (offset + doffset + frag_size < skb->len) 1131da177e4SLinus Torvalds th->fin = th->psh = 0; 1141da177e4SLinus Torvalds netif_rx(nskb); 1151da177e4SLinus Torvalds offset += frag_size; 1161da177e4SLinus Torvalds seq += frag_size; 1171da177e4SLinus Torvalds id++; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds dev_kfree_skb(skb); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* 1241da177e4SLinus Torvalds * The higher levels take care of making this non-reentrant (it's 1251da177e4SLinus Torvalds * called with bh's disabled). 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds static int loopback_xmit(struct sk_buff *skb, struct net_device *dev) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds struct net_device_stats *lb_stats; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds skb_orphan(skb); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds skb->protocol=eth_type_trans(skb,dev); 1341da177e4SLinus Torvalds skb->dev=dev; 1351da177e4SLinus Torvalds #ifndef LOOPBACK_MUST_CHECKSUM 1361da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_UNNECESSARY; 1371da177e4SLinus Torvalds #endif 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds if (skb_shinfo(skb)->tso_size) { 1401da177e4SLinus Torvalds BUG_ON(skb->protocol != htons(ETH_P_IP)); 1411da177e4SLinus Torvalds BUG_ON(skb->nh.iph->protocol != IPPROTO_TCP); 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds emulate_large_send_offload(skb); 1441da177e4SLinus Torvalds return 0; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds dev->last_rx = jiffies; 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds lb_stats = &per_cpu(loopback_stats, get_cpu()); 1501da177e4SLinus Torvalds lb_stats->rx_bytes += skb->len; 1511da177e4SLinus Torvalds lb_stats->tx_bytes += skb->len; 1521da177e4SLinus Torvalds lb_stats->rx_packets++; 1531da177e4SLinus Torvalds lb_stats->tx_packets++; 1541da177e4SLinus Torvalds put_cpu(); 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds netif_rx(skb); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds return(0); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds static struct net_device_stats *get_stats(struct net_device *dev) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds struct net_device_stats *stats = dev->priv; 1641da177e4SLinus Torvalds int i; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds if (!stats) { 1671da177e4SLinus Torvalds return NULL; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds memset(stats, 0, sizeof(struct net_device_stats)); 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds for (i=0; i < NR_CPUS; i++) { 1731da177e4SLinus Torvalds struct net_device_stats *lb_stats; 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds if (!cpu_possible(i)) 1761da177e4SLinus Torvalds continue; 1771da177e4SLinus Torvalds lb_stats = &per_cpu(loopback_stats, i); 1781da177e4SLinus Torvalds stats->rx_bytes += lb_stats->rx_bytes; 1791da177e4SLinus Torvalds stats->tx_bytes += lb_stats->tx_bytes; 1801da177e4SLinus Torvalds stats->rx_packets += lb_stats->rx_packets; 1811da177e4SLinus Torvalds stats->tx_packets += lb_stats->tx_packets; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds return stats; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds static u32 loopback_get_link(struct net_device *dev) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds return 1; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds static struct ethtool_ops loopback_ethtool_ops = { 1931da177e4SLinus Torvalds .get_link = loopback_get_link, 1941da177e4SLinus Torvalds .get_tso = ethtool_op_get_tso, 1951da177e4SLinus Torvalds .set_tso = ethtool_op_set_tso, 1961da177e4SLinus Torvalds }; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds struct net_device loopback_dev = { 1991da177e4SLinus Torvalds .name = "lo", 2001da177e4SLinus Torvalds .mtu = (16 * 1024) + 20 + 20 + 12, 2011da177e4SLinus Torvalds .hard_start_xmit = loopback_xmit, 2021da177e4SLinus Torvalds .hard_header = eth_header, 2031da177e4SLinus Torvalds .hard_header_cache = eth_header_cache, 2041da177e4SLinus Torvalds .header_cache_update = eth_header_cache_update, 2051da177e4SLinus Torvalds .hard_header_len = ETH_HLEN, /* 14 */ 2061da177e4SLinus Torvalds .addr_len = ETH_ALEN, /* 6 */ 2071da177e4SLinus Torvalds .tx_queue_len = 0, 2081da177e4SLinus Torvalds .type = ARPHRD_LOOPBACK, /* 0x0001*/ 2091da177e4SLinus Torvalds .rebuild_header = eth_rebuild_header, 2101da177e4SLinus Torvalds .flags = IFF_LOOPBACK, 2111da177e4SLinus Torvalds .features = NETIF_F_SG|NETIF_F_FRAGLIST 2121da177e4SLinus Torvalds |NETIF_F_NO_CSUM|NETIF_F_HIGHDMA 2131da177e4SLinus Torvalds |NETIF_F_LLTX, 2141da177e4SLinus Torvalds .ethtool_ops = &loopback_ethtool_ops, 2151da177e4SLinus Torvalds }; 2161da177e4SLinus Torvalds 217*22783649SRalf Baechle /* Setup and register the loopback device. */ 2181da177e4SLinus Torvalds int __init loopback_init(void) 2191da177e4SLinus Torvalds { 2201da177e4SLinus Torvalds struct net_device_stats *stats; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* Can survive without statistics */ 2231da177e4SLinus Torvalds stats = kmalloc(sizeof(struct net_device_stats), GFP_KERNEL); 2241da177e4SLinus Torvalds if (stats) { 2251da177e4SLinus Torvalds memset(stats, 0, sizeof(struct net_device_stats)); 2261da177e4SLinus Torvalds loopback_dev.priv = stats; 2271da177e4SLinus Torvalds loopback_dev.get_stats = &get_stats; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds return register_netdev(&loopback_dev); 2311da177e4SLinus Torvalds }; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds EXPORT_SYMBOL(loopback_dev); 234