loopback.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <net/netdev_lock.h>
  56. #include <linux/u64_stats_sync.h>
  57. /* blackhole_netdev - a device used for dsts that are marked expired!
  58. * This is global device (instead of per-net-ns) since it's not needed
  59. * to be per-ns and gets initialized at boot time.
  60. */
  61. struct net_device *blackhole_netdev;
  62. EXPORT_SYMBOL(blackhole_netdev);
  63. /* The higher levels take care of making this non-reentrant (it's
  64. * called with bh's disabled).
  65. */
  66. static netdev_tx_t loopback_xmit(struct sk_buff *skb,
  67. struct net_device *dev)
  68. {
  69. int len;
  70. skb_tx_timestamp(skb);
  71. /* do not fool net_timestamp_check() with various clock bases */
  72. skb_clear_tstamp(skb);
  73. skb_orphan(skb);
  74. /* Before queueing this packet to __netif_rx(),
  75. * make sure dst is refcounted.
  76. */
  77. skb_dst_force(skb);
  78. skb->protocol = eth_type_trans(skb, dev);
  79. len = skb->len;
  80. if (likely(__netif_rx(skb) == NET_RX_SUCCESS))
  81. dev_lstats_add(dev, len);
  82. return NETDEV_TX_OK;
  83. }
  84. void dev_lstats_read(struct net_device *dev, u64 *packets, u64 *bytes)
  85. {
  86. int i;
  87. *packets = 0;
  88. *bytes = 0;
  89. for_each_possible_cpu(i) {
  90. const struct pcpu_lstats *lb_stats;
  91. u64 tbytes, tpackets;
  92. unsigned int start;
  93. lb_stats = per_cpu_ptr(dev->lstats, i);
  94. do {
  95. start = u64_stats_fetch_begin(&lb_stats->syncp);
  96. tpackets = u64_stats_read(&lb_stats->packets);
  97. tbytes = u64_stats_read(&lb_stats->bytes);
  98. } while (u64_stats_fetch_retry(&lb_stats->syncp, start));
  99. *bytes += tbytes;
  100. *packets += tpackets;
  101. }
  102. }
  103. EXPORT_SYMBOL(dev_lstats_read);
  104. static void loopback_get_stats64(struct net_device *dev,
  105. struct rtnl_link_stats64 *stats)
  106. {
  107. u64 packets, bytes;
  108. dev_lstats_read(dev, &packets, &bytes);
  109. stats->rx_packets = packets;
  110. stats->tx_packets = packets;
  111. stats->rx_bytes = bytes;
  112. stats->tx_bytes = bytes;
  113. }
  114. static u32 always_on(struct net_device *dev)
  115. {
  116. return 1;
  117. }
  118. static const struct ethtool_ops loopback_ethtool_ops = {
  119. .get_link = always_on,
  120. .get_ts_info = ethtool_op_get_ts_info,
  121. };
  122. static int loopback_dev_init(struct net_device *dev)
  123. {
  124. netdev_lockdep_set_classes(dev);
  125. return 0;
  126. }
  127. static void loopback_dev_free(struct net_device *dev)
  128. {
  129. dev_net(dev)->loopback_dev = NULL;
  130. }
  131. static const struct net_device_ops loopback_ops = {
  132. .ndo_init = loopback_dev_init,
  133. .ndo_start_xmit = loopback_xmit,
  134. .ndo_get_stats64 = loopback_get_stats64,
  135. .ndo_set_mac_address = eth_mac_addr,
  136. };
  137. static void gen_lo_setup(struct net_device *dev,
  138. unsigned int mtu,
  139. const struct ethtool_ops *eth_ops,
  140. const struct header_ops *hdr_ops,
  141. const struct net_device_ops *dev_ops,
  142. void (*dev_destructor)(struct net_device *dev))
  143. {
  144. dev->mtu = mtu;
  145. dev->hard_header_len = ETH_HLEN; /* 14 */
  146. dev->min_header_len = ETH_HLEN; /* 14 */
  147. dev->addr_len = ETH_ALEN; /* 6 */
  148. dev->type = ARPHRD_LOOPBACK; /* 0x0001*/
  149. dev->flags = IFF_LOOPBACK;
  150. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
  151. dev->lltx = true;
  152. dev->netns_immutable = true;
  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_VLAN_CHALLENGED
  162. | NETIF_F_LOOPBACK;
  163. dev->ethtool_ops = eth_ops;
  164. dev->header_ops = hdr_ops;
  165. dev->netdev_ops = dev_ops;
  166. dev->needs_free_netdev = true;
  167. dev->pcpu_stat_type = NETDEV_PCPU_STAT_LSTATS;
  168. dev->priv_destructor = dev_destructor;
  169. netif_set_tso_max_size(dev, GSO_MAX_SIZE);
  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_PREDICTABLE, 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 int blackhole_neigh_output(struct neighbour *n, struct sk_buff *skb)
  215. {
  216. kfree_skb(skb);
  217. return 0;
  218. }
  219. static int blackhole_neigh_construct(struct net_device *dev,
  220. struct neighbour *n)
  221. {
  222. n->output = blackhole_neigh_output;
  223. return 0;
  224. }
  225. static const struct net_device_ops blackhole_netdev_ops = {
  226. .ndo_start_xmit = blackhole_netdev_xmit,
  227. .ndo_neigh_construct = blackhole_neigh_construct,
  228. };
  229. /* This is a dst-dummy device used specifically for invalidated
  230. * DSTs and unlike loopback, this is not per-ns.
  231. */
  232. static void blackhole_netdev_setup(struct net_device *dev)
  233. {
  234. gen_lo_setup(dev, ETH_MIN_MTU, NULL, NULL, &blackhole_netdev_ops, NULL);
  235. }
  236. /* Setup and register the blackhole_netdev. */
  237. static int __init blackhole_netdev_init(void)
  238. {
  239. blackhole_netdev = alloc_netdev(0, "blackhole_dev", NET_NAME_UNKNOWN,
  240. blackhole_netdev_setup);
  241. if (!blackhole_netdev)
  242. return -ENOMEM;
  243. rtnl_net_lock(&init_net);
  244. dev_init_scheduler(blackhole_netdev);
  245. dev_activate(blackhole_netdev);
  246. rtnl_net_unlock(&init_net);
  247. blackhole_netdev->flags |= IFF_UP | IFF_RUNNING;
  248. return 0;
  249. }
  250. device_initcall(blackhole_netdev_init);