loopback.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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, <waltje@uWalt.NL.Mugnet.ORG>
  13. * Donald Becker, <becker@scyld.com>
  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(&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(&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. netdev_lockdep_set_classes(dev);
  124. return 0;
  125. }
  126. static void loopback_dev_free(struct net_device *dev)
  127. {
  128. dev_net(dev)->loopback_dev = NULL;
  129. }
  130. static const struct net_device_ops loopback_ops = {
  131. .ndo_init = loopback_dev_init,
  132. .ndo_start_xmit = loopback_xmit,
  133. .ndo_get_stats64 = loopback_get_stats64,
  134. .ndo_set_mac_address = eth_mac_addr,
  135. };
  136. static void gen_lo_setup(struct net_device *dev,
  137. unsigned int mtu,
  138. const struct ethtool_ops *eth_ops,
  139. const struct header_ops *hdr_ops,
  140. const struct net_device_ops *dev_ops,
  141. void (*dev_destructor)(struct net_device *dev))
  142. {
  143. dev->mtu = mtu;
  144. dev->hard_header_len = ETH_HLEN; /* 14 */
  145. dev->min_header_len = ETH_HLEN; /* 14 */
  146. dev->addr_len = ETH_ALEN; /* 6 */
  147. dev->type = ARPHRD_LOOPBACK; /* 0x0001*/
  148. dev->flags = IFF_LOOPBACK;
  149. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
  150. dev->lltx = true;
  151. dev->netns_local = true;
  152. netif_keep_dst(dev);
  153. dev->hw_features = NETIF_F_GSO_SOFTWARE;
  154. dev->features = NETIF_F_SG | NETIF_F_FRAGLIST
  155. | NETIF_F_GSO_SOFTWARE
  156. | NETIF_F_HW_CSUM
  157. | NETIF_F_RXCSUM
  158. | NETIF_F_SCTP_CRC
  159. | NETIF_F_HIGHDMA
  160. | NETIF_F_VLAN_CHALLENGED
  161. | NETIF_F_LOOPBACK;
  162. dev->ethtool_ops = eth_ops;
  163. dev->header_ops = hdr_ops;
  164. dev->netdev_ops = dev_ops;
  165. dev->needs_free_netdev = true;
  166. dev->pcpu_stat_type = NETDEV_PCPU_STAT_LSTATS;
  167. dev->priv_destructor = dev_destructor;
  168. netif_set_tso_max_size(dev, GSO_MAX_SIZE);
  169. }
  170. /* The loopback device is special. There is only one instance
  171. * per network namespace.
  172. */
  173. static void loopback_setup(struct net_device *dev)
  174. {
  175. gen_lo_setup(dev, (64 * 1024), &loopback_ethtool_ops, &eth_header_ops,
  176. &loopback_ops, loopback_dev_free);
  177. }
  178. /* Setup and register the loopback device. */
  179. static __net_init int loopback_net_init(struct net *net)
  180. {
  181. struct net_device *dev;
  182. int err;
  183. err = -ENOMEM;
  184. dev = alloc_netdev(0, "lo", NET_NAME_PREDICTABLE, loopback_setup);
  185. if (!dev)
  186. goto out;
  187. dev_net_set(dev, net);
  188. err = register_netdev(dev);
  189. if (err)
  190. goto out_free_netdev;
  191. BUG_ON(dev->ifindex != LOOPBACK_IFINDEX);
  192. net->loopback_dev = dev;
  193. return 0;
  194. out_free_netdev:
  195. free_netdev(dev);
  196. out:
  197. if (net_eq(net, &init_net))
  198. panic("loopback: Failed to register netdevice: %d\n", err);
  199. return err;
  200. }
  201. /* Registered in net/core/dev.c */
  202. struct pernet_operations __net_initdata loopback_net_ops = {
  203. .init = loopback_net_init,
  204. };
  205. /* blackhole netdevice */
  206. static netdev_tx_t blackhole_netdev_xmit(struct sk_buff *skb,
  207. struct net_device *dev)
  208. {
  209. kfree_skb(skb);
  210. net_warn_ratelimited("%s(): Dropping skb.\n", __func__);
  211. return NETDEV_TX_OK;
  212. }
  213. static const struct net_device_ops blackhole_netdev_ops = {
  214. .ndo_start_xmit = blackhole_netdev_xmit,
  215. };
  216. /* This is a dst-dummy device used specifically for invalidated
  217. * DSTs and unlike loopback, this is not per-ns.
  218. */
  219. static void blackhole_netdev_setup(struct net_device *dev)
  220. {
  221. gen_lo_setup(dev, ETH_MIN_MTU, NULL, NULL, &blackhole_netdev_ops, NULL);
  222. }
  223. /* Setup and register the blackhole_netdev. */
  224. static int __init blackhole_netdev_init(void)
  225. {
  226. blackhole_netdev = alloc_netdev(0, "blackhole_dev", NET_NAME_UNKNOWN,
  227. blackhole_netdev_setup);
  228. if (!blackhole_netdev)
  229. return -ENOMEM;
  230. rtnl_lock();
  231. dev_init_scheduler(blackhole_netdev);
  232. dev_activate(blackhole_netdev);
  233. rtnl_unlock();
  234. blackhole_netdev->flags |= IFF_UP | IFF_RUNNING;
  235. dev_net_set(blackhole_netdev, &init_net);
  236. return 0;
  237. }
  238. device_initcall(blackhole_netdev_init);