xref: /linux-6.15/drivers/net/loopback.c (revision de7d5084)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the  BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		Pseudo-driver for the loopback interface.
8  *
9  * Version:	@(#)loopback.c	1.0.4b	08/16/93
10  *
11  * Authors:	Ross Biro
12  *		Fred N. van Kempen, <[email protected]>
13  *		Donald Becker, <[email protected]>
14  *
15  *		Alan Cox	:	Fixed oddments for NET3.014
16  *		Alan Cox	:	Rejig for NET3.029 snap #3
17  *		Alan Cox	:	Fixed NET3.029 bugs and sped up
18  *		Larry McVoy	:	Tiny tweak to double performance
19  *		Alan Cox	:	Backed out LMV's tweak - the linux mm
20  *					can't take it...
21  *              Michael Griffith:       Don't bother computing the checksums
22  *                                      on packets received on the loopback
23  *                                      interface.
24  *		Alexey Kuznetsov:	Potential hang under some extreme
25  *					cases removed.
26  */
27 #include <linux/kernel.h>
28 #include <linux/jiffies.h>
29 #include <linux/module.h>
30 #include <linux/interrupt.h>
31 #include <linux/fs.h>
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/socket.h>
35 #include <linux/errno.h>
36 #include <linux/fcntl.h>
37 #include <linux/in.h>
38 
39 #include <linux/uaccess.h>
40 #include <linux/io.h>
41 
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include <linux/skbuff.h>
46 #include <linux/ethtool.h>
47 #include <net/sock.h>
48 #include <net/checksum.h>
49 #include <linux/if_ether.h>	/* For the statistics structure. */
50 #include <linux/if_arp.h>	/* For ARPHRD_ETHER */
51 #include <linux/ip.h>
52 #include <linux/tcp.h>
53 #include <linux/percpu.h>
54 #include <linux/net_tstamp.h>
55 #include <net/net_namespace.h>
56 #include <linux/u64_stats_sync.h>
57 
58 /* blackhole_netdev - a device used for dsts that are marked expired!
59  * This is global device (instead of per-net-ns) since it's not needed
60  * to be per-ns and gets initialized at boot time.
61  */
62 struct net_device *blackhole_netdev;
63 EXPORT_SYMBOL(blackhole_netdev);
64 
65 /* The higher levels take care of making this non-reentrant (it's
66  * called with bh's disabled).
67  */
68 static netdev_tx_t loopback_xmit(struct sk_buff *skb,
69 				 struct net_device *dev)
70 {
71 	struct pcpu_lstats *lb_stats;
72 	int len;
73 
74 	skb_tx_timestamp(skb);
75 
76 	/* do not fool net_timestamp_check() with various clock bases */
77 	skb->tstamp = 0;
78 
79 	skb_orphan(skb);
80 
81 	/* Before queueing this packet to netif_rx(),
82 	 * make sure dst is refcounted.
83 	 */
84 	skb_dst_force(skb);
85 
86 	skb->protocol = eth_type_trans(skb, dev);
87 
88 	/* it's OK to use per_cpu_ptr() because BHs are off */
89 	lb_stats = this_cpu_ptr(dev->lstats);
90 
91 	len = skb->len;
92 	if (likely(netif_rx(skb) == NET_RX_SUCCESS)) {
93 		u64_stats_update_begin(&lb_stats->syncp);
94 		lb_stats->bytes += len;
95 		lb_stats->packets++;
96 		u64_stats_update_end(&lb_stats->syncp);
97 	}
98 
99 	return NETDEV_TX_OK;
100 }
101 
102 void dev_lstats_read(struct net_device *dev, u64 *packets, u64 *bytes)
103 {
104 	int i;
105 
106 	*packets = 0;
107 	*bytes = 0;
108 
109 	for_each_possible_cpu(i) {
110 		const struct pcpu_lstats *lb_stats;
111 		u64 tbytes, tpackets;
112 		unsigned int start;
113 
114 		lb_stats = per_cpu_ptr(dev->lstats, i);
115 		do {
116 			start = u64_stats_fetch_begin_irq(&lb_stats->syncp);
117 			tpackets = lb_stats->packets;
118 			tbytes = lb_stats->bytes;
119 		} while (u64_stats_fetch_retry_irq(&lb_stats->syncp, start));
120 		*bytes   += tbytes;
121 		*packets += tpackets;
122 	}
123 }
124 EXPORT_SYMBOL(dev_lstats_read);
125 
126 static void loopback_get_stats64(struct net_device *dev,
127 				 struct rtnl_link_stats64 *stats)
128 {
129 	u64 packets, bytes;
130 
131 	dev_lstats_read(dev, &packets, &bytes);
132 
133 	stats->rx_packets = packets;
134 	stats->tx_packets = packets;
135 	stats->rx_bytes   = bytes;
136 	stats->tx_bytes   = bytes;
137 }
138 
139 static u32 always_on(struct net_device *dev)
140 {
141 	return 1;
142 }
143 
144 static const struct ethtool_ops loopback_ethtool_ops = {
145 	.get_link		= always_on,
146 	.get_ts_info		= ethtool_op_get_ts_info,
147 };
148 
149 static int loopback_dev_init(struct net_device *dev)
150 {
151 	dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
152 	if (!dev->lstats)
153 		return -ENOMEM;
154 	return 0;
155 }
156 
157 static void loopback_dev_free(struct net_device *dev)
158 {
159 	dev_net(dev)->loopback_dev = NULL;
160 	free_percpu(dev->lstats);
161 }
162 
163 static const struct net_device_ops loopback_ops = {
164 	.ndo_init        = loopback_dev_init,
165 	.ndo_start_xmit  = loopback_xmit,
166 	.ndo_get_stats64 = loopback_get_stats64,
167 	.ndo_set_mac_address = eth_mac_addr,
168 };
169 
170 static void gen_lo_setup(struct net_device *dev,
171 			 unsigned int mtu,
172 			 const struct ethtool_ops *eth_ops,
173 			 const struct header_ops *hdr_ops,
174 			 const struct net_device_ops *dev_ops,
175 			 void (*dev_destructor)(struct net_device *dev))
176 {
177 	dev->mtu		= mtu;
178 	dev->hard_header_len	= ETH_HLEN;	/* 14	*/
179 	dev->min_header_len	= ETH_HLEN;	/* 14	*/
180 	dev->addr_len		= ETH_ALEN;	/* 6	*/
181 	dev->type		= ARPHRD_LOOPBACK;	/* 0x0001*/
182 	dev->flags		= IFF_LOOPBACK;
183 	dev->priv_flags		|= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
184 	netif_keep_dst(dev);
185 	dev->hw_features	= NETIF_F_GSO_SOFTWARE;
186 	dev->features		= NETIF_F_SG | NETIF_F_FRAGLIST
187 		| NETIF_F_GSO_SOFTWARE
188 		| NETIF_F_HW_CSUM
189 		| NETIF_F_RXCSUM
190 		| NETIF_F_SCTP_CRC
191 		| NETIF_F_HIGHDMA
192 		| NETIF_F_LLTX
193 		| NETIF_F_NETNS_LOCAL
194 		| NETIF_F_VLAN_CHALLENGED
195 		| NETIF_F_LOOPBACK;
196 	dev->ethtool_ops	= eth_ops;
197 	dev->header_ops		= hdr_ops;
198 	dev->netdev_ops		= dev_ops;
199 	dev->needs_free_netdev	= true;
200 	dev->priv_destructor	= dev_destructor;
201 }
202 
203 /* The loopback device is special. There is only one instance
204  * per network namespace.
205  */
206 static void loopback_setup(struct net_device *dev)
207 {
208 	gen_lo_setup(dev, (64 * 1024), &loopback_ethtool_ops, &eth_header_ops,
209 		     &loopback_ops, loopback_dev_free);
210 }
211 
212 /* Setup and register the loopback device. */
213 static __net_init int loopback_net_init(struct net *net)
214 {
215 	struct net_device *dev;
216 	int err;
217 
218 	err = -ENOMEM;
219 	dev = alloc_netdev(0, "lo", NET_NAME_UNKNOWN, loopback_setup);
220 	if (!dev)
221 		goto out;
222 
223 	dev_net_set(dev, net);
224 	err = register_netdev(dev);
225 	if (err)
226 		goto out_free_netdev;
227 
228 	BUG_ON(dev->ifindex != LOOPBACK_IFINDEX);
229 	net->loopback_dev = dev;
230 	return 0;
231 
232 out_free_netdev:
233 	free_netdev(dev);
234 out:
235 	if (net_eq(net, &init_net))
236 		panic("loopback: Failed to register netdevice: %d\n", err);
237 	return err;
238 }
239 
240 /* Registered in net/core/dev.c */
241 struct pernet_operations __net_initdata loopback_net_ops = {
242 	.init = loopback_net_init,
243 };
244 
245 /* blackhole netdevice */
246 static netdev_tx_t blackhole_netdev_xmit(struct sk_buff *skb,
247 					 struct net_device *dev)
248 {
249 	kfree_skb(skb);
250 	net_warn_ratelimited("%s(): Dropping skb.\n", __func__);
251 	return NETDEV_TX_OK;
252 }
253 
254 static const struct net_device_ops blackhole_netdev_ops = {
255 	.ndo_start_xmit = blackhole_netdev_xmit,
256 };
257 
258 /* This is a dst-dummy device used specifically for invalidated
259  * DSTs and unlike loopback, this is not per-ns.
260  */
261 static void blackhole_netdev_setup(struct net_device *dev)
262 {
263 	gen_lo_setup(dev, ETH_MIN_MTU, NULL, NULL, &blackhole_netdev_ops, NULL);
264 }
265 
266 /* Setup and register the blackhole_netdev. */
267 static int __init blackhole_netdev_init(void)
268 {
269 	blackhole_netdev = alloc_netdev(0, "blackhole_dev", NET_NAME_UNKNOWN,
270 					blackhole_netdev_setup);
271 	if (!blackhole_netdev)
272 		return -ENOMEM;
273 
274 	rtnl_lock();
275 	dev_init_scheduler(blackhole_netdev);
276 	dev_activate(blackhole_netdev);
277 	rtnl_unlock();
278 
279 	blackhole_netdev->flags |= IFF_UP | IFF_RUNNING;
280 	dev_net_set(blackhole_netdev, &init_net);
281 
282 	return 0;
283 }
284 
285 device_initcall(blackhole_netdev_init);
286