loopback.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #include <linux/uaccess.h>
  39. #include <linux/io.h>
  40. #include <linux/inet.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/ethtool.h>
  45. #include <net/sch_generic.h>
  46. #include <net/sock.h>
  47. #include <net/checksum.h>
  48. #include <linux/if_ether.h> /* For the statistics structure. */
  49. #include <linux/if_arp.h> /* For ARPHRD_ETHER */
  50. #include <linux/ip.h>
  51. #include <linux/tcp.h>
  52. #include <linux/percpu.h>
  53. #include <linux/net_tstamp.h>
  54. #include <net/net_namespace.h>
  55. #include <linux/u64_stats_sync.h>
  56. /* blackhole_netdev - a device used for dsts that are marked expired!
  57. * This is global device (instead of per-net-ns) since it's not needed
  58. * to be per-ns and gets initialized at boot time.
  59. */
  60. struct net_device *blackhole_netdev;
  61. EXPORT_SYMBOL(blackhole_netdev);
  62. /* The higher levels take care of making this non-reentrant (it's
  63. * called with bh's disabled).
  64. */
  65. static netdev_tx_t loopback_xmit(struct sk_buff *skb,
  66. struct net_device *dev)
  67. {
  68. int len;
  69. skb_tx_timestamp(skb);
  70. /* do not fool net_timestamp_check() with various clock bases */
  71. skb_clear_tstamp(skb);
  72. skb_orphan(skb);
  73. /* Before queueing this packet to __netif_rx(),
  74. * make sure dst is refcounted.
  75. */
  76. skb_dst_force(skb);
  77. skb->protocol = eth_type_trans(skb, dev);
  78. len = skb->len;
  79. if (likely(__netif_rx(skb) == NET_RX_SUCCESS))
  80. dev_lstats_add(dev, len);
  81. return NETDEV_TX_OK;
  82. }
  83. void dev_lstats_read(struct net_device *dev, u64 *packets, u64 *bytes)
  84. {
  85. int i;
  86. *packets = 0;
  87. *bytes = 0;
  88. for_each_possible_cpu(i) {
  89. const struct pcpu_lstats *lb_stats;
  90. u64 tbytes, tpackets;
  91. unsigned int start;
  92. lb_stats = per_cpu_ptr(dev->lstats, i);
  93. do {
  94. start = u64_stats_fetch_begin_irq(&lb_stats->syncp);
  95. tpackets = u64_stats_read(&lb_stats->packets);
  96. tbytes = u64_stats_read(&lb_stats->bytes);
  97. } while (u64_stats_fetch_retry_irq(&lb_stats->syncp, start));
  98. *bytes += tbytes;
  99. *packets += tpackets;
  100. }
  101. }
  102. EXPORT_SYMBOL(dev_lstats_read);
  103. static void loopback_get_stats64(struct net_device *dev,
  104. struct rtnl_link_stats64 *stats)
  105. {
  106. u64 packets, bytes;
  107. dev_lstats_read(dev, &packets, &bytes);
  108. stats->rx_packets = packets;
  109. stats->tx_packets = packets;
  110. stats->rx_bytes = bytes;
  111. stats->tx_bytes = bytes;
  112. }
  113. static u32 always_on(struct net_device *dev)
  114. {
  115. return 1;
  116. }
  117. static const struct ethtool_ops loopback_ethtool_ops = {
  118. .get_link = always_on,
  119. .get_ts_info = ethtool_op_get_ts_info,
  120. };
  121. static int loopback_dev_init(struct net_device *dev)
  122. {
  123. dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
  124. if (!dev->lstats)
  125. return -ENOMEM;
  126. return 0;
  127. }
  128. static void loopback_dev_free(struct net_device *dev)
  129. {
  130. dev_net(dev)->loopback_dev = NULL;
  131. free_percpu(dev->lstats);
  132. }
  133. static const struct net_device_ops loopback_ops = {
  134. .ndo_init = loopback_dev_init,
  135. .ndo_start_xmit = loopback_xmit,
  136. .ndo_get_stats64 = loopback_get_stats64,
  137. .ndo_set_mac_address = eth_mac_addr,
  138. };
  139. static void gen_lo_setup(struct net_device *dev,
  140. unsigned int mtu,
  141. const struct ethtool_ops *eth_ops,
  142. const struct header_ops *hdr_ops,
  143. const struct net_device_ops *dev_ops,
  144. void (*dev_destructor)(struct net_device *dev))
  145. {
  146. dev->mtu = mtu;
  147. dev->hard_header_len = ETH_HLEN; /* 14 */
  148. dev->min_header_len = ETH_HLEN; /* 14 */
  149. dev->addr_len = ETH_ALEN; /* 6 */
  150. dev->type = ARPHRD_LOOPBACK; /* 0x0001*/
  151. dev->flags = IFF_LOOPBACK;
  152. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
  153. netif_keep_dst(dev);
  154. dev->hw_features = NETIF_F_GSO_SOFTWARE;
  155. dev->features = NETIF_F_SG | NETIF_F_FRAGLIST
  156. | NETIF_F_GSO_SOFTWARE
  157. | NETIF_F_HW_CSUM
  158. | NETIF_F_RXCSUM
  159. | NETIF_F_SCTP_CRC
  160. | NETIF_F_HIGHDMA
  161. | NETIF_F_LLTX
  162. | NETIF_F_NETNS_LOCAL
  163. | NETIF_F_VLAN_CHALLENGED
  164. | NETIF_F_LOOPBACK;
  165. dev->ethtool_ops = eth_ops;
  166. dev->header_ops = hdr_ops;
  167. dev->netdev_ops = dev_ops;
  168. dev->needs_free_netdev = true;
  169. dev->priv_destructor = dev_destructor;
  170. netif_set_tso_max_size(dev, GSO_MAX_SIZE);
  171. }
  172. /* The loopback device is special. There is only one instance
  173. * per network namespace.
  174. */
  175. static void loopback_setup(struct net_device *dev)
  176. {
  177. gen_lo_setup(dev, (64 * 1024), &loopback_ethtool_ops, &eth_header_ops,
  178. &loopback_ops, loopback_dev_free);
  179. }
  180. /* Setup and register the loopback device. */
  181. static __net_init int loopback_net_init(struct net *net)
  182. {
  183. struct net_device *dev;
  184. int err;
  185. err = -ENOMEM;
  186. dev = alloc_netdev(0, "lo", NET_NAME_PREDICTABLE, loopback_setup);
  187. if (!dev)
  188. goto out;
  189. dev_net_set(dev, net);
  190. err = register_netdev(dev);
  191. if (err)
  192. goto out_free_netdev;
  193. BUG_ON(dev->ifindex != LOOPBACK_IFINDEX);
  194. net->loopback_dev = dev;
  195. return 0;
  196. out_free_netdev:
  197. free_netdev(dev);
  198. out:
  199. if (net_eq(net, &init_net))
  200. panic("loopback: Failed to register netdevice: %d\n", err);
  201. return err;
  202. }
  203. /* Registered in net/core/dev.c */
  204. struct pernet_operations __net_initdata loopback_net_ops = {
  205. .init = loopback_net_init,
  206. };
  207. /* blackhole netdevice */
  208. static netdev_tx_t blackhole_netdev_xmit(struct sk_buff *skb,
  209. struct net_device *dev)
  210. {
  211. kfree_skb(skb);
  212. net_warn_ratelimited("%s(): Dropping skb.\n", __func__);
  213. return NETDEV_TX_OK;
  214. }
  215. static const struct net_device_ops blackhole_netdev_ops = {
  216. .ndo_start_xmit = blackhole_netdev_xmit,
  217. };
  218. /* This is a dst-dummy device used specifically for invalidated
  219. * DSTs and unlike loopback, this is not per-ns.
  220. */
  221. static void blackhole_netdev_setup(struct net_device *dev)
  222. {
  223. gen_lo_setup(dev, ETH_MIN_MTU, NULL, NULL, &blackhole_netdev_ops, NULL);
  224. }
  225. /* Setup and register the blackhole_netdev. */
  226. static int __init blackhole_netdev_init(void)
  227. {
  228. blackhole_netdev = alloc_netdev(0, "blackhole_dev", NET_NAME_UNKNOWN,
  229. blackhole_netdev_setup);
  230. if (!blackhole_netdev)
  231. return -ENOMEM;
  232. rtnl_lock();
  233. dev_init_scheduler(blackhole_netdev);
  234. dev_activate(blackhole_netdev);
  235. rtnl_unlock();
  236. blackhole_netdev->flags |= IFF_UP | IFF_RUNNING;
  237. dev_net_set(blackhole_netdev, &init_net);
  238. return 0;
  239. }
  240. device_initcall(blackhole_netdev_init);