loopback.c (371bb62158d53c1fc33e2fb9b6aeb9522caf6cf4) loopback.c (4de83b88c66a1e4dba426b29766fb68e61d93792)
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 *

--- 41 unchanged lines hidden (view full) ---

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
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 *

--- 41 unchanged lines hidden (view full) ---

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 */
62struct net_device *blackhole_netdev;
63EXPORT_SYMBOL(blackhole_netdev);
64
58/* The higher levels take care of making this non-reentrant (it's
59 * called with bh's disabled).
60 */
61static netdev_tx_t loopback_xmit(struct sk_buff *skb,
62 struct net_device *dev)
63{
64 struct pcpu_lstats *lb_stats;
65 int len;

--- 79 unchanged lines hidden (view full) ---

145
146static const struct net_device_ops loopback_ops = {
147 .ndo_init = loopback_dev_init,
148 .ndo_start_xmit = loopback_xmit,
149 .ndo_get_stats64 = loopback_get_stats64,
150 .ndo_set_mac_address = eth_mac_addr,
151};
152
65/* The higher levels take care of making this non-reentrant (it's
66 * called with bh's disabled).
67 */
68static netdev_tx_t loopback_xmit(struct sk_buff *skb,
69 struct net_device *dev)
70{
71 struct pcpu_lstats *lb_stats;
72 int len;

--- 79 unchanged lines hidden (view full) ---

152
153static const struct net_device_ops loopback_ops = {
154 .ndo_init = loopback_dev_init,
155 .ndo_start_xmit = loopback_xmit,
156 .ndo_get_stats64 = loopback_get_stats64,
157 .ndo_set_mac_address = eth_mac_addr,
158};
159
153/* The loopback device is special. There is only one instance
154 * per network namespace.
155 */
156static void loopback_setup(struct net_device *dev)
160static void gen_lo_setup(struct net_device *dev,
161 unsigned int mtu,
162 const struct ethtool_ops *eth_ops,
163 const struct header_ops *hdr_ops,
164 const struct net_device_ops *dev_ops,
165 void (*dev_destructor)(struct net_device *dev))
157{
166{
158 dev->mtu = 64 * 1024;
167 dev->mtu = mtu;
159 dev->hard_header_len = ETH_HLEN; /* 14 */
160 dev->min_header_len = ETH_HLEN; /* 14 */
161 dev->addr_len = ETH_ALEN; /* 6 */
162 dev->type = ARPHRD_LOOPBACK; /* 0x0001*/
163 dev->flags = IFF_LOOPBACK;
164 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
165 netif_keep_dst(dev);
166 dev->hw_features = NETIF_F_GSO_SOFTWARE;
167 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST
168 | NETIF_F_GSO_SOFTWARE
169 | NETIF_F_HW_CSUM
170 | NETIF_F_RXCSUM
171 | NETIF_F_SCTP_CRC
172 | NETIF_F_HIGHDMA
173 | NETIF_F_LLTX
174 | NETIF_F_NETNS_LOCAL
175 | NETIF_F_VLAN_CHALLENGED
176 | NETIF_F_LOOPBACK;
168 dev->hard_header_len = ETH_HLEN; /* 14 */
169 dev->min_header_len = ETH_HLEN; /* 14 */
170 dev->addr_len = ETH_ALEN; /* 6 */
171 dev->type = ARPHRD_LOOPBACK; /* 0x0001*/
172 dev->flags = IFF_LOOPBACK;
173 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
174 netif_keep_dst(dev);
175 dev->hw_features = NETIF_F_GSO_SOFTWARE;
176 dev->features = NETIF_F_SG | NETIF_F_FRAGLIST
177 | NETIF_F_GSO_SOFTWARE
178 | NETIF_F_HW_CSUM
179 | NETIF_F_RXCSUM
180 | NETIF_F_SCTP_CRC
181 | NETIF_F_HIGHDMA
182 | NETIF_F_LLTX
183 | NETIF_F_NETNS_LOCAL
184 | NETIF_F_VLAN_CHALLENGED
185 | NETIF_F_LOOPBACK;
177 dev->ethtool_ops = &loopback_ethtool_ops;
178 dev->header_ops = &eth_header_ops;
179 dev->netdev_ops = &loopback_ops;
186 dev->ethtool_ops = eth_ops;
187 dev->header_ops = hdr_ops;
188 dev->netdev_ops = dev_ops;
180 dev->needs_free_netdev = true;
189 dev->needs_free_netdev = true;
181 dev->priv_destructor = loopback_dev_free;
190 dev->priv_destructor = dev_destructor;
182}
183
191}
192
193/* The loopback device is special. There is only one instance
194 * per network namespace.
195 */
196static void loopback_setup(struct net_device *dev)
197{
198 gen_lo_setup(dev, (64 * 1024), &loopback_ethtool_ops, &eth_header_ops,
199 &loopback_ops, loopback_dev_free);
200}
201
184/* Setup and register the loopback device. */
185static __net_init int loopback_net_init(struct net *net)
186{
187 struct net_device *dev;
188 int err;
189
190 err = -ENOMEM;
191 dev = alloc_netdev(0, "lo", NET_NAME_UNKNOWN, loopback_setup);

--- 16 unchanged lines hidden (view full) ---

208 panic("loopback: Failed to register netdevice: %d\n", err);
209 return err;
210}
211
212/* Registered in net/core/dev.c */
213struct pernet_operations __net_initdata loopback_net_ops = {
214 .init = loopback_net_init,
215};
202/* Setup and register the loopback device. */
203static __net_init int loopback_net_init(struct net *net)
204{
205 struct net_device *dev;
206 int err;
207
208 err = -ENOMEM;
209 dev = alloc_netdev(0, "lo", NET_NAME_UNKNOWN, loopback_setup);

--- 16 unchanged lines hidden (view full) ---

226 panic("loopback: Failed to register netdevice: %d\n", err);
227 return err;
228}
229
230/* Registered in net/core/dev.c */
231struct pernet_operations __net_initdata loopback_net_ops = {
232 .init = loopback_net_init,
233};
234
235/* blackhole netdevice */
236static netdev_tx_t blackhole_netdev_xmit(struct sk_buff *skb,
237 struct net_device *dev)
238{
239 kfree_skb(skb);
240 net_warn_ratelimited("%s(): Dropping skb.\n", __func__);
241 return NETDEV_TX_OK;
242}
243
244static const struct net_device_ops blackhole_netdev_ops = {
245 .ndo_start_xmit = blackhole_netdev_xmit,
246};
247
248/* This is a dst-dummy device used specifically for invalidated
249 * DSTs and unlike loopback, this is not per-ns.
250 */
251static void blackhole_netdev_setup(struct net_device *dev)
252{
253 gen_lo_setup(dev, ETH_MIN_MTU, NULL, NULL, &blackhole_netdev_ops, NULL);
254}
255
256/* Setup and register the blackhole_netdev. */
257static int __init blackhole_netdev_init(void)
258{
259 blackhole_netdev = alloc_netdev(0, "blackhole_dev", NET_NAME_UNKNOWN,
260 blackhole_netdev_setup);
261 if (!blackhole_netdev)
262 return -ENOMEM;
263
264 dev_init_scheduler(blackhole_netdev);
265 dev_activate(blackhole_netdev);
266
267 blackhole_netdev->flags |= IFF_UP | IFF_RUNNING;
268 dev_net_set(blackhole_netdev, &init_net);
269
270 return 0;
271}
272
273device_initcall(blackhole_netdev_init);