loopback.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/sock.h>
  46. #include <net/checksum.h>
  47. #include <linux/if_ether.h> /* For the statistics structure. */
  48. #include <linux/if_arp.h> /* For ARPHRD_ETHER */
  49. #include <linux/ip.h>
  50. #include <linux/tcp.h>
  51. #include <linux/percpu.h>
  52. #include <linux/net_tstamp.h>
  53. #include <net/net_namespace.h>
  54. #include <linux/u64_stats_sync.h>
  55. /* blackhole_netdev - a device used for dsts that are marked expired!
  56. * This is global device (instead of per-net-ns) since it's not needed
  57. * to be per-ns and gets initialized at boot time.
  58. */
  59. struct net_device *blackhole_netdev;
  60. EXPORT_SYMBOL(blackhole_netdev);
  61. /* The higher levels take care of making this non-reentrant (it's
  62. * called with bh's disabled).
  63. */
  64. static netdev_tx_t loopback_xmit(struct sk_buff *skb,
  65. struct net_device *dev)
  66. {
  67. struct pcpu_lstats *lb_stats;
  68. int len;
  69. skb_tx_timestamp(skb);
  70. /* do not fool net_timestamp_check() with various clock bases */
  71. skb->tstamp = 0;
  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. /* it's OK to use per_cpu_ptr() because BHs are off */
  79. lb_stats = this_cpu_ptr(dev->lstats);
  80. len = skb->len;
  81. if (likely(netif_rx(skb) == NET_RX_SUCCESS)) {
  82. u64_stats_update_begin(&lb_stats->syncp);
  83. lb_stats->bytes += len;
  84. lb_stats->packets++;
  85. u64_stats_update_end(&lb_stats->syncp);
  86. }
  87. return NETDEV_TX_OK;
  88. }
  89. static void loopback_get_stats64(struct net_device *dev,
  90. struct rtnl_link_stats64 *stats)
  91. {
  92. u64 bytes = 0;
  93. u64 packets = 0;
  94. int i;
  95. for_each_possible_cpu(i) {
  96. const struct pcpu_lstats *lb_stats;
  97. u64 tbytes, tpackets;
  98. unsigned int start;
  99. lb_stats = per_cpu_ptr(dev->lstats, i);
  100. do {
  101. start = u64_stats_fetch_begin_irq(&lb_stats->syncp);
  102. tbytes = lb_stats->bytes;
  103. tpackets = lb_stats->packets;
  104. } while (u64_stats_fetch_retry_irq(&lb_stats->syncp, start));
  105. bytes += tbytes;
  106. packets += tpackets;
  107. }
  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. }
  171. /* The loopback device is special. There is only one instance
  172. * per network namespace.
  173. */
  174. static void loopback_setup(struct net_device *dev)
  175. {
  176. gen_lo_setup(dev, (64 * 1024), &loopback_ethtool_ops, &eth_header_ops,
  177. &loopback_ops, loopback_dev_free);
  178. }
  179. /* Setup and register the loopback device. */
  180. static __net_init int loopback_net_init(struct net *net)
  181. {
  182. struct net_device *dev;
  183. int err;
  184. err = -ENOMEM;
  185. dev = alloc_netdev(0, "lo", NET_NAME_UNKNOWN, loopback_setup);
  186. if (!dev)
  187. goto out;
  188. dev_net_set(dev, net);
  189. err = register_netdev(dev);
  190. if (err)
  191. goto out_free_netdev;
  192. BUG_ON(dev->ifindex != LOOPBACK_IFINDEX);
  193. net->loopback_dev = dev;
  194. return 0;
  195. out_free_netdev:
  196. free_netdev(dev);
  197. out:
  198. if (net_eq(net, &init_net))
  199. panic("loopback: Failed to register netdevice: %d\n", err);
  200. return err;
  201. }
  202. /* Registered in net/core/dev.c */
  203. struct pernet_operations __net_initdata loopback_net_ops = {
  204. .init = loopback_net_init,
  205. };
  206. /* blackhole netdevice */
  207. static netdev_tx_t blackhole_netdev_xmit(struct sk_buff *skb,
  208. struct net_device *dev)
  209. {
  210. kfree_skb(skb);
  211. net_warn_ratelimited("%s(): Dropping skb.\n", __func__);
  212. return NETDEV_TX_OK;
  213. }
  214. static const struct net_device_ops blackhole_netdev_ops = {
  215. .ndo_start_xmit = blackhole_netdev_xmit,
  216. };
  217. /* This is a dst-dummy device used specifically for invalidated
  218. * DSTs and unlike loopback, this is not per-ns.
  219. */
  220. static void blackhole_netdev_setup(struct net_device *dev)
  221. {
  222. gen_lo_setup(dev, ETH_MIN_MTU, NULL, NULL, &blackhole_netdev_ops, NULL);
  223. }
  224. /* Setup and register the blackhole_netdev. */
  225. static int __init blackhole_netdev_init(void)
  226. {
  227. blackhole_netdev = alloc_netdev(0, "blackhole_dev", NET_NAME_UNKNOWN,
  228. blackhole_netdev_setup);
  229. if (!blackhole_netdev)
  230. return -ENOMEM;
  231. rtnl_lock();
  232. dev_init_scheduler(blackhole_netdev);
  233. dev_activate(blackhole_netdev);
  234. rtnl_unlock();
  235. blackhole_netdev->flags |= IFF_UP | IFF_RUNNING;
  236. dev_net_set(blackhole_netdev, &init_net);
  237. return 0;
  238. }
  239. device_initcall(blackhole_netdev_init);