12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket
51da177e4SLinus Torvalds * interface as the means of communication with the user level.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Pseudo-driver for the loopback interface.
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Version: @(#)loopback.c 1.0.4b 08/16/93
101da177e4SLinus Torvalds *
1102c30a84SJesper Juhl * Authors: Ross Biro
121da177e4SLinus Torvalds * Fred N. van Kempen, <[email protected]>
131da177e4SLinus Torvalds * Donald Becker, <[email protected]>
141da177e4SLinus Torvalds *
151da177e4SLinus Torvalds * Alan Cox : Fixed oddments for NET3.014
161da177e4SLinus Torvalds * Alan Cox : Rejig for NET3.029 snap #3
171da177e4SLinus Torvalds * Alan Cox : Fixed NET3.029 bugs and sped up
181da177e4SLinus Torvalds * Larry McVoy : Tiny tweak to double performance
191da177e4SLinus Torvalds * Alan Cox : Backed out LMV's tweak - the linux mm
201da177e4SLinus Torvalds * can't take it...
211da177e4SLinus Torvalds * Michael Griffith: Don't bother computing the checksums
221da177e4SLinus Torvalds * on packets received on the loopback
231da177e4SLinus Torvalds * interface.
241da177e4SLinus Torvalds * Alexey Kuznetsov: Potential hang under some extreme
251da177e4SLinus Torvalds * cases removed.
261da177e4SLinus Torvalds */
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/jiffies.h>
291da177e4SLinus Torvalds #include <linux/module.h>
301da177e4SLinus Torvalds #include <linux/interrupt.h>
311da177e4SLinus Torvalds #include <linux/fs.h>
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/string.h>
341da177e4SLinus Torvalds #include <linux/socket.h>
351da177e4SLinus Torvalds #include <linux/errno.h>
361da177e4SLinus Torvalds #include <linux/fcntl.h>
371da177e4SLinus Torvalds #include <linux/in.h>
381da177e4SLinus Torvalds
397c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
40b3407c8eSEzequiel Lara Gomez #include <linux/io.h>
411da177e4SLinus Torvalds
421da177e4SLinus Torvalds #include <linux/inet.h>
431da177e4SLinus Torvalds #include <linux/netdevice.h>
441da177e4SLinus Torvalds #include <linux/etherdevice.h>
451da177e4SLinus Torvalds #include <linux/skbuff.h>
461da177e4SLinus Torvalds #include <linux/ethtool.h>
47b6459415SJakub Kicinski #include <net/sch_generic.h>
481da177e4SLinus Torvalds #include <net/sock.h>
491da177e4SLinus Torvalds #include <net/checksum.h>
501da177e4SLinus Torvalds #include <linux/if_ether.h> /* For the statistics structure. */
511da177e4SLinus Torvalds #include <linux/if_arp.h> /* For ARPHRD_ETHER */
521da177e4SLinus Torvalds #include <linux/ip.h>
531da177e4SLinus Torvalds #include <linux/tcp.h>
541da177e4SLinus Torvalds #include <linux/percpu.h>
556df014cfSEzequiel Lara Gomez #include <linux/net_tstamp.h>
562774c7abSEric W. Biederman #include <net/net_namespace.h>
57*8ef890dfSJakub Kicinski #include <net/netdev_lock.h>
585eaa0bd8SEric Dumazet #include <linux/u64_stats_sync.h>
591da177e4SLinus Torvalds
604de83b88SMahesh Bandewar /* blackhole_netdev - a device used for dsts that are marked expired!
614de83b88SMahesh Bandewar * This is global device (instead of per-net-ns) since it's not needed
624de83b88SMahesh Bandewar * to be per-ns and gets initialized at boot time.
634de83b88SMahesh Bandewar */
644de83b88SMahesh Bandewar struct net_device *blackhole_netdev;
654de83b88SMahesh Bandewar EXPORT_SYMBOL(blackhole_netdev);
664de83b88SMahesh Bandewar
67b3407c8eSEzequiel Lara Gomez /* The higher levels take care of making this non-reentrant (it's
681da177e4SLinus Torvalds * called with bh's disabled).
691da177e4SLinus Torvalds */
loopback_xmit(struct sk_buff * skb,struct net_device * dev)7061357325SStephen Hemminger static netdev_tx_t loopback_xmit(struct sk_buff *skb,
7161357325SStephen Hemminger struct net_device *dev)
721da177e4SLinus Torvalds {
737eebb0b2SEric Dumazet int len;
741da177e4SLinus Torvalds
756df014cfSEzequiel Lara Gomez skb_tx_timestamp(skb);
764c16128bSEric Dumazet
774c16128bSEric Dumazet /* do not fool net_timestamp_check() with various clock bases */
78de799101SMartin KaFai Lau skb_clear_tstamp(skb);
794c16128bSEric Dumazet
801da177e4SLinus Torvalds skb_orphan(skb);
811da177e4SLinus Torvalds
82baebdf48SSebastian Andrzej Siewior /* Before queueing this packet to __netif_rx(),
83794ed393SEric Dumazet * make sure dst is refcounted.
84794ed393SEric Dumazet */
85794ed393SEric Dumazet skb_dst_force(skb);
86794ed393SEric Dumazet
871da177e4SLinus Torvalds skb->protocol = eth_type_trans(skb, dev);
881da177e4SLinus Torvalds
897eebb0b2SEric Dumazet len = skb->len;
90baebdf48SSebastian Andrzej Siewior if (likely(__netif_rx(skb) == NET_RX_SUCCESS))
91dd5382a0SEric Dumazet dev_lstats_add(dev, len);
921da177e4SLinus Torvalds
936ed10654SPatrick McHardy return NETDEV_TX_OK;
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds
dev_lstats_read(struct net_device * dev,u64 * packets,u64 * bytes)96de7d5084SEric Dumazet void dev_lstats_read(struct net_device *dev, u64 *packets, u64 *bytes)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds int i;
991da177e4SLinus Torvalds
100de7d5084SEric Dumazet *packets = 0;
101de7d5084SEric Dumazet *bytes = 0;
102de7d5084SEric Dumazet
1030fed4846SKAMEZAWA Hiroyuki for_each_possible_cpu(i) {
1045175c378SEric Dumazet const struct pcpu_lstats *lb_stats;
1055eaa0bd8SEric Dumazet u64 tbytes, tpackets;
1065eaa0bd8SEric Dumazet unsigned int start;
1071da177e4SLinus Torvalds
108a7855c78SEric Dumazet lb_stats = per_cpu_ptr(dev->lstats, i);
1095eaa0bd8SEric Dumazet do {
110068c38adSThomas Gleixner start = u64_stats_fetch_begin(&lb_stats->syncp);
111fd2f4737SEric Dumazet tpackets = u64_stats_read(&lb_stats->packets);
112fd2f4737SEric Dumazet tbytes = u64_stats_read(&lb_stats->bytes);
113068c38adSThomas Gleixner } while (u64_stats_fetch_retry(&lb_stats->syncp, start));
114de7d5084SEric Dumazet *bytes += tbytes;
115de7d5084SEric Dumazet *packets += tpackets;
1161da177e4SLinus Torvalds }
117de7d5084SEric Dumazet }
118de7d5084SEric Dumazet EXPORT_SYMBOL(dev_lstats_read);
119de7d5084SEric Dumazet
loopback_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)120de7d5084SEric Dumazet static void loopback_get_stats64(struct net_device *dev,
121de7d5084SEric Dumazet struct rtnl_link_stats64 *stats)
122de7d5084SEric Dumazet {
123de7d5084SEric Dumazet u64 packets, bytes;
124de7d5084SEric Dumazet
125de7d5084SEric Dumazet dev_lstats_read(dev, &packets, &bytes);
126de7d5084SEric Dumazet
1275175c378SEric Dumazet stats->rx_packets = packets;
1285175c378SEric Dumazet stats->tx_packets = packets;
1295175c378SEric Dumazet stats->rx_bytes = bytes;
1305175c378SEric Dumazet stats->tx_bytes = bytes;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds
always_on(struct net_device * dev)1337fa6b066SStephen Hemminger static u32 always_on(struct net_device *dev)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds return 1;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds
1387282d491SJeff Garzik static const struct ethtool_ops loopback_ethtool_ops = {
1397fa6b066SStephen Hemminger .get_link = always_on,
140af730342SJulian Wiedmann .get_ts_info = ethtool_op_get_ts_info,
1411da177e4SLinus Torvalds };
1421da177e4SLinus Torvalds
loopback_dev_init(struct net_device * dev)1435f6d88b9SEric W. Biederman static int loopback_dev_init(struct net_device *dev)
1445f6d88b9SEric W. Biederman {
1450bef5120SEric Dumazet netdev_lockdep_set_classes(dev);
1465f6d88b9SEric W. Biederman return 0;
1475f6d88b9SEric W. Biederman }
1485f6d88b9SEric W. Biederman
loopback_dev_free(struct net_device * dev)1495f6d88b9SEric W. Biederman static void loopback_dev_free(struct net_device *dev)
1505f6d88b9SEric W. Biederman {
151e05e9070SEric W. Biederman dev_net(dev)->loopback_dev = NULL;
1525f6d88b9SEric W. Biederman }
1535f6d88b9SEric W. Biederman
154c02373bfSStephen Hemminger static const struct net_device_ops loopback_ops = {
155c02373bfSStephen Hemminger .ndo_init = loopback_dev_init,
15600829823SStephen Hemminger .ndo_start_xmit = loopback_xmit,
1576b10de38SEric Dumazet .ndo_get_stats64 = loopback_get_stats64,
15825f929fbSWANG Cong .ndo_set_mac_address = eth_mac_addr,
159c02373bfSStephen Hemminger };
160c02373bfSStephen Hemminger
gen_lo_setup(struct net_device * dev,unsigned int mtu,const struct ethtool_ops * eth_ops,const struct header_ops * hdr_ops,const struct net_device_ops * dev_ops,void (* dev_destructor)(struct net_device * dev))1614de83b88SMahesh Bandewar static void gen_lo_setup(struct net_device *dev,
1624de83b88SMahesh Bandewar unsigned int mtu,
1634de83b88SMahesh Bandewar const struct ethtool_ops *eth_ops,
1644de83b88SMahesh Bandewar const struct header_ops *hdr_ops,
1654de83b88SMahesh Bandewar const struct net_device_ops *dev_ops,
1664de83b88SMahesh Bandewar void (*dev_destructor)(struct net_device *dev))
167854d8363SDaniel Lezcano {
1684de83b88SMahesh Bandewar dev->mtu = mtu;
169854d8363SDaniel Lezcano dev->hard_header_len = ETH_HLEN; /* 14 */
170217e6fa2SWillem de Bruijn dev->min_header_len = ETH_HLEN; /* 14 */
171854d8363SDaniel Lezcano dev->addr_len = ETH_ALEN; /* 6 */
172854d8363SDaniel Lezcano dev->type = ARPHRD_LOOPBACK; /* 0x0001*/
173854d8363SDaniel Lezcano dev->flags = IFF_LOOPBACK;
174e65db2b7SPhil Sutter dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
17500d066a4SAlexander Lobakin dev->lltx = true;
1760c493da8SNicolas Dichtel dev->netns_immutable = true;
17702875878SEric Dumazet netif_keep_dst(dev);
178f6c382fcSMarcelo Ricardo Leitner dev->hw_features = NETIF_F_GSO_SOFTWARE;
179854d8363SDaniel Lezcano dev->features = NETIF_F_SG | NETIF_F_FRAGLIST
180f6c382fcSMarcelo Ricardo Leitner | NETIF_F_GSO_SOFTWARE
18134324dc2SMichał Mirosław | NETIF_F_HW_CSUM
182cf0bdefdSMichał Mirosław | NETIF_F_RXCSUM
18353692b1dSTom Herbert | NETIF_F_SCTP_CRC
184854d8363SDaniel Lezcano | NETIF_F_HIGHDMA
185eed2a12fSMahesh Bandewar | NETIF_F_VLAN_CHALLENGED
186eed2a12fSMahesh Bandewar | NETIF_F_LOOPBACK;
1874de83b88SMahesh Bandewar dev->ethtool_ops = eth_ops;
1884de83b88SMahesh Bandewar dev->header_ops = hdr_ops;
1894de83b88SMahesh Bandewar dev->netdev_ops = dev_ops;
190cf124db5SDavid S. Miller dev->needs_free_netdev = true;
1913b5933e9SBreno Leitao dev->pcpu_stat_type = NETDEV_PCPU_STAT_LSTATS;
1924de83b88SMahesh Bandewar dev->priv_destructor = dev_destructor;
193d6f938ceSEric Dumazet
194d6f938ceSEric Dumazet netif_set_tso_max_size(dev, GSO_MAX_SIZE);
1954de83b88SMahesh Bandewar }
1964de83b88SMahesh Bandewar
1974de83b88SMahesh Bandewar /* The loopback device is special. There is only one instance
1984de83b88SMahesh Bandewar * per network namespace.
1994de83b88SMahesh Bandewar */
loopback_setup(struct net_device * dev)2004de83b88SMahesh Bandewar static void loopback_setup(struct net_device *dev)
2014de83b88SMahesh Bandewar {
2024de83b88SMahesh Bandewar gen_lo_setup(dev, (64 * 1024), &loopback_ethtool_ops, ð_header_ops,
2034de83b88SMahesh Bandewar &loopback_ops, loopback_dev_free);
204854d8363SDaniel Lezcano }
205de3cb747SDaniel Lezcano
20622783649SRalf Baechle /* Setup and register the loopback device. */
loopback_net_init(struct net * net)2074665079cSPavel Emelyanov static __net_init int loopback_net_init(struct net *net)
2081da177e4SLinus Torvalds {
209854d8363SDaniel Lezcano struct net_device *dev;
210854d8363SDaniel Lezcano int err;
211aeed9e82SHerbert Xu
212854d8363SDaniel Lezcano err = -ENOMEM;
21331d929deSRasmus Villemoes dev = alloc_netdev(0, "lo", NET_NAME_PREDICTABLE, loopback_setup);
214854d8363SDaniel Lezcano if (!dev)
215854d8363SDaniel Lezcano goto out;
216854d8363SDaniel Lezcano
217c346dca1SYOSHIFUJI Hideaki dev_net_set(dev, net);
218854d8363SDaniel Lezcano err = register_netdev(dev);
219854d8363SDaniel Lezcano if (err)
220854d8363SDaniel Lezcano goto out_free_netdev;
221854d8363SDaniel Lezcano
2221fb9489bSPavel Emelyanov BUG_ON(dev->ifindex != LOOPBACK_IFINDEX);
2232774c7abSEric W. Biederman net->loopback_dev = dev;
2249d6dda32SPavel Emelyanov return 0;
225854d8363SDaniel Lezcano
226854d8363SDaniel Lezcano out_free_netdev:
227854d8363SDaniel Lezcano free_netdev(dev);
2289d6dda32SPavel Emelyanov out:
22909ad9bc7SOctavian Purdila if (net_eq(net, &init_net))
2309d6dda32SPavel Emelyanov panic("loopback: Failed to register netdevice: %d\n", err);
2319d6dda32SPavel Emelyanov return err;
232854d8363SDaniel Lezcano }
23360903f2cSAdrian Bunk
234505d4f73SEric W. Biederman /* Registered in net/core/dev.c */
235505d4f73SEric W. Biederman struct pernet_operations __net_initdata loopback_net_ops = {
2362774c7abSEric W. Biederman .init = loopback_net_init,
2372774c7abSEric W. Biederman };
2384de83b88SMahesh Bandewar
2394de83b88SMahesh Bandewar /* blackhole netdevice */
blackhole_netdev_xmit(struct sk_buff * skb,struct net_device * dev)2404de83b88SMahesh Bandewar static netdev_tx_t blackhole_netdev_xmit(struct sk_buff *skb,
2414de83b88SMahesh Bandewar struct net_device *dev)
2424de83b88SMahesh Bandewar {
2434de83b88SMahesh Bandewar kfree_skb(skb);
2444de83b88SMahesh Bandewar net_warn_ratelimited("%s(): Dropping skb.\n", __func__);
2454de83b88SMahesh Bandewar return NETDEV_TX_OK;
2464de83b88SMahesh Bandewar }
2474de83b88SMahesh Bandewar
blackhole_neigh_output(struct neighbour * n,struct sk_buff * skb)2480e4427f8SIdo Schimmel static int blackhole_neigh_output(struct neighbour *n, struct sk_buff *skb)
2490e4427f8SIdo Schimmel {
2500e4427f8SIdo Schimmel kfree_skb(skb);
2510e4427f8SIdo Schimmel return 0;
2520e4427f8SIdo Schimmel }
2530e4427f8SIdo Schimmel
blackhole_neigh_construct(struct net_device * dev,struct neighbour * n)2540e4427f8SIdo Schimmel static int blackhole_neigh_construct(struct net_device *dev,
2550e4427f8SIdo Schimmel struct neighbour *n)
2560e4427f8SIdo Schimmel {
2570e4427f8SIdo Schimmel n->output = blackhole_neigh_output;
2580e4427f8SIdo Schimmel return 0;
2590e4427f8SIdo Schimmel }
2600e4427f8SIdo Schimmel
2614de83b88SMahesh Bandewar static const struct net_device_ops blackhole_netdev_ops = {
2624de83b88SMahesh Bandewar .ndo_start_xmit = blackhole_netdev_xmit,
2630e4427f8SIdo Schimmel .ndo_neigh_construct = blackhole_neigh_construct,
2644de83b88SMahesh Bandewar };
2654de83b88SMahesh Bandewar
2664de83b88SMahesh Bandewar /* This is a dst-dummy device used specifically for invalidated
2674de83b88SMahesh Bandewar * DSTs and unlike loopback, this is not per-ns.
2684de83b88SMahesh Bandewar */
blackhole_netdev_setup(struct net_device * dev)2694de83b88SMahesh Bandewar static void blackhole_netdev_setup(struct net_device *dev)
2704de83b88SMahesh Bandewar {
2714de83b88SMahesh Bandewar gen_lo_setup(dev, ETH_MIN_MTU, NULL, NULL, &blackhole_netdev_ops, NULL);
2724de83b88SMahesh Bandewar }
2734de83b88SMahesh Bandewar
2744de83b88SMahesh Bandewar /* Setup and register the blackhole_netdev. */
blackhole_netdev_init(void)2754de83b88SMahesh Bandewar static int __init blackhole_netdev_init(void)
2764de83b88SMahesh Bandewar {
2774de83b88SMahesh Bandewar blackhole_netdev = alloc_netdev(0, "blackhole_dev", NET_NAME_UNKNOWN,
2784de83b88SMahesh Bandewar blackhole_netdev_setup);
2794de83b88SMahesh Bandewar if (!blackhole_netdev)
2804de83b88SMahesh Bandewar return -ENOMEM;
2814de83b88SMahesh Bandewar
2822248c053SKuniyuki Iwashima rtnl_net_lock(&init_net);
2834de83b88SMahesh Bandewar dev_init_scheduler(blackhole_netdev);
2844de83b88SMahesh Bandewar dev_activate(blackhole_netdev);
2852248c053SKuniyuki Iwashima rtnl_net_unlock(&init_net);
2864de83b88SMahesh Bandewar
2874de83b88SMahesh Bandewar blackhole_netdev->flags |= IFF_UP | IFF_RUNNING;
2884de83b88SMahesh Bandewar
2894de83b88SMahesh Bandewar return 0;
2904de83b88SMahesh Bandewar }
2914de83b88SMahesh Bandewar
2924de83b88SMahesh Bandewar device_initcall(blackhole_netdev_init);
293