blob: 793dc04d2f190748b3e4f25e870d035c349d4842 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common framework for low-level network console, dump, and debugger code
3 *
4 * Sep 8 2003 Matt Mackall <mpm@selenic.com>
5 *
6 * based on the netconsole code from:
7 *
8 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2002 Red Hat, Inc.
10 */
11
Joe Perchese6ec269352012-01-29 15:50:43 +000012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Anton Vorontsovbff38772009-07-08 11:10:56 -070014#include <linux/moduleparam.h>
Andy Shevchenko4cd57732013-06-04 19:46:26 +030015#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
18#include <linux/string.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020019#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/inetdevice.h>
21#include <linux/inet.h>
22#include <linux/interrupt.h>
23#include <linux/netpoll.h>
24#include <linux/sched.h>
25#include <linux/delay.h>
26#include <linux/rcupdate.h>
27#include <linux/workqueue.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040029#include <linux/export.h>
Amerigo Wang689971b2012-08-10 01:24:49 +000030#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/tcp.h>
32#include <net/udp.h>
Cong Wangb3d936f2013-01-07 20:52:41 +000033#include <net/addrconf.h>
34#include <net/ndisc.h>
35#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/unaligned.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -070037#include <trace/events/napi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * We maintain a small pool of fully-sized skbs, to make sure the
41 * message gets out even in extreme OOM situations.
42 */
43
44#define MAX_UDP_CHUNK 1460
45#define MAX_SKBS 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -080047static struct sk_buff_head skb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static atomic_t trapped;
50
Lai Jiangshan7f9421c2013-03-15 06:50:52 +000051DEFINE_STATIC_SRCU(netpoll_srcu);
Neil Hormanca99ca12013-02-05 08:05:43 +000052
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -070053#define USEC_PER_POLL 50
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Joe Perches6f706242012-01-29 15:50:44 +000055#define MAX_SKB_SIZE \
56 (sizeof(struct ethhdr) + \
57 sizeof(struct iphdr) + \
58 sizeof(struct udphdr) + \
59 MAX_UDP_CHUNK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
David S. Miller3578b0c2010-08-03 00:24:04 -070061static void zap_completion_queue(void);
Cong Wangb7394d22013-01-07 20:52:39 +000062static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
Neil Horman2cde6ac2013-02-11 10:25:30 +000063static void netpoll_async_cleanup(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Anton Vorontsovbff38772009-07-08 11:10:56 -070065static unsigned int carrier_timeout = 4;
66module_param(carrier_timeout, uint, 0644);
67
Joe Perchese6ec269352012-01-29 15:50:43 +000068#define np_info(np, fmt, ...) \
69 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
70#define np_err(np, fmt, ...) \
71 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
72#define np_notice(np, fmt, ...) \
73 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
74
David Howellsc4028952006-11-22 14:57:56 +000075static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
David Howells4c1ac1b2006-12-05 14:37:56 +000077 struct netpoll_info *npinfo =
78 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +010080 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070082 while ((skb = skb_dequeue(&npinfo->txq))) {
83 struct net_device *dev = skb->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -080084 const struct net_device_ops *ops = dev->netdev_ops;
David S. Millerfd2ea0a2008-07-17 01:56:23 -070085 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070087 if (!netif_device_present(dev) || !netif_running(dev)) {
88 __kfree_skb(skb);
89 continue;
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
David S. Millerfd2ea0a2008-07-17 01:56:23 -070092 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
93
Ingo Molnar36405432006-12-12 17:20:42 +010094 local_irq_save(flags);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070095 __netif_tx_lock(txq, smp_processor_id());
Tom Herbert734664982011-11-28 16:32:44 +000096 if (netif_xmit_frozen_or_stopped(txq) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080097 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070098 skb_queue_head(&npinfo->txq, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070099 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100100 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Jarek Poplawski25442ca2007-07-05 17:42:44 -0700102 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700103 return;
104 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700105 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100106 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108}
109
Al Virob51655b2006-11-14 21:40:42 -0800110static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
111 unsigned short ulen, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Al Virod6f5493c2006-11-14 21:26:08 -0800113 __wsum psum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800114
Herbert Xu60476372007-04-09 11:59:39 -0700115 if (uh->check == 0 || skb_csum_unnecessary(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117
Herbert Xufb286bb2005-11-10 13:01:24 -0800118 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Patrick McHardy84fa7932006-08-29 16:44:56 -0700120 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Al Virod3bc23e2006-11-14 21:24:49 -0800121 !csum_fold(csum_add(psum, skb->csum)))
Herbert Xufb286bb2005-11-10 13:01:24 -0800122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Herbert Xufb286bb2005-11-10 13:01:24 -0800124 skb->csum = psum;
125
126 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129/*
130 * Check whether delayed processing was scheduled for our NIC. If so,
131 * we attempt to grab the poll lock and use ->poll() to pump the card.
132 * If this fails, either we've recursed in ->poll() or it's already
133 * running on another CPU.
134 *
135 * Note: we don't mask interrupts with this lock because we're using
136 * trylock here and interrupts are already disabled in the softirq
137 * case. Further, we test the poll_owner to avoid recursion on UP
138 * systems where the lock doesn't exist.
139 *
140 * In cases where there is bi-directional communications, reading only
141 * one message at a time can lead to packets being dropped by the
142 * network adapter, forcing superfluous retries and possibly timeouts.
143 * Thus, we set our budget to greater than 1.
144 */
Eric W. Biedermanb249b512014-03-14 20:44:37 -0700145static int poll_one_napi(struct napi_struct *napi, int budget)
David S. Miller0a7606c2007-10-29 21:28:47 -0700146{
147 int work;
148
149 /* net_rx_action's ->poll() invocations and our's are
150 * synchronized by this test which is only made while
151 * holding the napi->poll_lock.
152 */
153 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
154 return budget;
155
Neil Horman7b363e42008-12-09 23:22:26 -0800156 set_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700157
158 work = napi->poll(napi, budget);
Eric W. Biedermane97dc3f2014-03-14 20:47:15 -0700159 WARN_ONCE(work > budget, "%pF exceeded budget in poll\n", napi->poll);
David S. Miller7d18f112009-05-21 23:30:09 -0700160 trace_napi_poll(napi);
David S. Miller0a7606c2007-10-29 21:28:47 -0700161
Neil Horman7b363e42008-12-09 23:22:26 -0800162 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700163
164 return budget - work;
165}
166
Eric W. Biederman9852fbe2014-03-14 20:45:17 -0700167static void poll_napi(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700169 struct napi_struct *napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Neil Hormanf13d4932010-10-19 07:04:26 +0000171 list_for_each_entry(napi, &dev->napi_list, dev_list) {
David S. Miller0a7606c2007-10-29 21:28:47 -0700172 if (napi->poll_owner != smp_processor_id() &&
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700173 spin_trylock(&napi->poll_lock)) {
Eric W. Biedermanb249b512014-03-14 20:44:37 -0700174 budget = poll_one_napi(napi, budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700175 spin_unlock(&napi->poll_lock);
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178}
179
Cong Wangb7394d22013-01-07 20:52:39 +0000180static void service_neigh_queue(struct netpoll_info *npi)
Neil Horman068c6e92006-06-26 00:04:27 -0700181{
Stephen Hemminger51069302007-11-19 19:18:11 -0800182 if (npi) {
183 struct sk_buff *skb;
Neil Horman068c6e92006-06-26 00:04:27 -0700184
Cong Wangb7394d22013-01-07 20:52:39 +0000185 while ((skb = skb_dequeue(&npi->neigh_tx)))
186 netpoll_neigh_reply(skb, npi);
Neil Horman068c6e92006-06-26 00:04:27 -0700187 }
Neil Horman068c6e92006-06-26 00:04:27 -0700188}
189
Joe Perches234b9212011-06-30 15:08:57 +0000190static void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000192 const struct net_device_ops *ops;
Amerigo Wang28996562012-08-10 01:24:42 +0000193 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
Eric W. Biedermanb6bacd52014-03-14 20:48:28 -0700194 bool rx_processing = netpoll_rx_processing(ni);
195 int budget = rx_processing? 16 : 0;
Stephen Hemminger51069302007-11-19 19:18:11 -0800196
Neil Hormanca99ca12013-02-05 08:05:43 +0000197 /* Don't do any rx activity if the dev_lock mutex is held
198 * the dev_open/close paths use this to block netpoll activity
199 * while changing device state
200 */
Dan Carpentera3dbbc22013-05-06 02:15:13 +0000201 if (down_trylock(&ni->dev_lock))
Neil Hormanca99ca12013-02-05 08:05:43 +0000202 return;
203
Neil Horman959d5fd2013-02-13 11:32:42 -0500204 if (!netif_running(dev)) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000205 up(&ni->dev_lock);
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000206 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500207 }
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000208
Eric W. Biedermanb6bacd52014-03-14 20:48:28 -0700209 if (rx_processing)
210 atomic_inc(&trapped);
Eric W. Biedermanb249b512014-03-14 20:44:37 -0700211
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000212 ops = dev->netdev_ops;
Neil Horman959d5fd2013-02-13 11:32:42 -0500213 if (!ops->ndo_poll_controller) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000214 up(&ni->dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /* Process pending work on NIC */
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800219 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Eric W. Biederman9852fbe2014-03-14 20:45:17 -0700221 poll_napi(dev, budget);
Stephen Hemminger51069302007-11-19 19:18:11 -0800222
Eric W. Biedermanb6bacd52014-03-14 20:48:28 -0700223 if (rx_processing)
224 atomic_dec(&trapped);
Eric W. Biedermanb249b512014-03-14 20:44:37 -0700225
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000226 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000227
Eric Dumazet58e05f32012-02-14 10:11:59 +0000228 if (dev->flags & IFF_SLAVE) {
Amerigo Wang28996562012-08-10 01:24:42 +0000229 if (ni) {
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000230 struct net_device *bond_dev;
Amerigo Wang5a698af2011-02-17 23:43:34 +0000231 struct sk_buff *skb;
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000232 struct netpoll_info *bond_ni;
233
234 bond_dev = netdev_master_upper_dev_get_rcu(dev);
235 bond_ni = rcu_dereference_bh(bond_dev->npinfo);
Cong Wangb7394d22013-01-07 20:52:39 +0000236 while ((skb = skb_dequeue(&ni->neigh_tx))) {
Amerigo Wang5a698af2011-02-17 23:43:34 +0000237 skb->dev = bond_dev;
Cong Wangb7394d22013-01-07 20:52:39 +0000238 skb_queue_tail(&bond_ni->neigh_tx, skb);
Amerigo Wang5a698af2011-02-17 23:43:34 +0000239 }
240 }
241 }
242
Cong Wangb7394d22013-01-07 20:52:39 +0000243 service_neigh_queue(ni);
Neil Horman068c6e92006-06-26 00:04:27 -0700244
David S. Miller3578b0c2010-08-03 00:24:04 -0700245 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
dingtianhongda6e3782013-05-27 19:53:31 +0000248void netpoll_rx_disable(struct net_device *dev)
Neil Hormanca99ca12013-02-05 08:05:43 +0000249{
250 struct netpoll_info *ni;
251 int idx;
252 might_sleep();
253 idx = srcu_read_lock(&netpoll_srcu);
254 ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
255 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000256 down(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000257 srcu_read_unlock(&netpoll_srcu, idx);
Neil Hormanca99ca12013-02-05 08:05:43 +0000258}
259EXPORT_SYMBOL(netpoll_rx_disable);
260
261void netpoll_rx_enable(struct net_device *dev)
262{
263 struct netpoll_info *ni;
264 rcu_read_lock();
265 ni = rcu_dereference(dev->npinfo);
266 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000267 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000268 rcu_read_unlock();
269}
270EXPORT_SYMBOL(netpoll_rx_enable);
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static void refill_skbs(void)
273{
274 struct sk_buff *skb;
275 unsigned long flags;
276
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800277 spin_lock_irqsave(&skb_pool.lock, flags);
278 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
280 if (!skb)
281 break;
282
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800283 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800285 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
David S. Miller3578b0c2010-08-03 00:24:04 -0700288static void zap_completion_queue(void)
289{
290 unsigned long flags;
291 struct softnet_data *sd = &get_cpu_var(softnet_data);
292
293 if (sd->completion_queue) {
294 struct sk_buff *clist;
295
296 local_irq_save(flags);
297 clist = sd->completion_queue;
298 sd->completion_queue = NULL;
299 local_irq_restore(flags);
300
301 while (clist != NULL) {
302 struct sk_buff *skb = clist;
303 clist = clist->next;
304 if (skb->destructor) {
305 atomic_inc(&skb->users);
306 dev_kfree_skb_any(skb); /* put this one back */
307 } else {
308 __kfree_skb(skb);
309 }
310 }
311 }
312
313 put_cpu_var(softnet_data);
314}
315
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800316static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800318 int count = 0;
319 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
David S. Miller3578b0c2010-08-03 00:24:04 -0700321 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800322 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800326 if (!skb)
327 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800330 if (++count < 10) {
Joe Perches2a49e002011-06-30 15:08:58 +0000331 netpoll_poll_dev(np->dev);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800332 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800334 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 atomic_set(&skb->users, 1);
338 skb_reserve(skb, reserve);
339 return skb;
340}
341
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700342static int netpoll_owner_active(struct net_device *dev)
343{
344 struct napi_struct *napi;
345
346 list_for_each_entry(napi, &dev->napi_list, dev_list) {
347 if (napi->poll_owner == smp_processor_id())
348 return 1;
349 }
350 return 0;
351}
352
Amerigo Wang28996562012-08-10 01:24:42 +0000353/* call with IRQ disabled */
Neil Hormanc2355e12010-10-13 16:01:49 +0000354void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
355 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700357 int status = NETDEV_TX_BUSY;
358 unsigned long tries;
Stephen Hemminger00829822008-11-20 20:14:53 -0800359 const struct net_device_ops *ops = dev->netdev_ops;
Herbert Xude85d992010-06-10 16:12:44 +0000360 /* It is up to the caller to keep npinfo alive. */
Amerigo Wang28996562012-08-10 01:24:42 +0000361 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Amerigo Wang28996562012-08-10 01:24:42 +0000363 WARN_ON_ONCE(!irqs_disabled());
364
365 npinfo = rcu_dereference_bh(np->dev->npinfo);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900366 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
367 __kfree_skb(skb);
368 return;
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700371 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700372 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700373 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800374
Jason Wangf663dd92014-01-10 16:18:26 +0800375 txq = netdev_pick_tx(dev, skb, NULL);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700376
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700377 /* try until next clock tick */
378 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
379 tries > 0; --tries) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700380 if (__netif_tx_trylock(txq)) {
Tom Herbert734664982011-11-28 16:32:44 +0000381 if (!netif_xmit_stopped(txq)) {
Amerigo Wang689971b2012-08-10 01:24:49 +0000382 if (vlan_tx_tag_present(skb) &&
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000383 !vlan_hw_offload_capable(netif_skb_features(skb),
384 skb->vlan_proto)) {
385 skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
David S. Milleraca5f582014-01-02 19:50:52 -0500386 if (unlikely(!skb)) {
387 /* This is actually a packet drop, but we
388 * don't want the code at the end of this
389 * function to try and re-queue a NULL skb.
390 */
391 status = NETDEV_TX_OK;
392 goto unlock_txq;
393 }
Amerigo Wang689971b2012-08-10 01:24:49 +0000394 skb->vlan_tci = 0;
395 }
396
Stephen Hemminger00829822008-11-20 20:14:53 -0800397 status = ops->ndo_start_xmit(skb, dev);
Eric Dumazet08baf562009-05-25 22:58:01 -0700398 if (status == NETDEV_TX_OK)
399 txq_trans_update(txq);
400 }
David S. Milleraca5f582014-01-02 19:50:52 -0500401 unlock_txq:
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700402 __netif_tx_unlock(txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700403
Andrew Mortone37b8d92006-12-09 14:01:49 -0800404 if (status == NETDEV_TX_OK)
405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Andrew Mortone37b8d92006-12-09 14:01:49 -0800407 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700408
409 /* tickle device maybe there is some cleanup */
Joe Perches2a49e002011-06-30 15:08:58 +0000410 netpoll_poll_dev(np->dev);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700411
412 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700413 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000414
415 WARN_ONCE(!irqs_disabled(),
Amerigo Wang28996562012-08-10 01:24:42 +0000416 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000417 dev->name, ops->ndo_start_xmit);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700421 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700422 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000423 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
Neil Hormanc2355e12010-10-13 16:01:49 +0000426EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
429{
Eric Dumazet954fba02012-06-12 19:30:21 +0000430 int total_len, ip_len, udp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct sk_buff *skb;
432 struct udphdr *udph;
433 struct iphdr *iph;
434 struct ethhdr *eth;
Eric Dumazetee130402012-08-24 01:47:26 +0000435 static atomic_t ip_ident;
Cong Wangb3d936f2013-01-07 20:52:41 +0000436 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 udp_len = len + sizeof(*udph);
Cong Wangb3d936f2013-01-07 20:52:41 +0000439 if (np->ipv6)
440 ip_len = udp_len + sizeof(*ip6h);
441 else
Cong Wangb7394d22013-01-07 20:52:39 +0000442 ip_len = udp_len + sizeof(*iph);
443
Eric Dumazet954fba02012-06-12 19:30:21 +0000444 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Eric Dumazet954fba02012-06-12 19:30:21 +0000446 skb = find_skb(np, total_len + np->dev->needed_tailroom,
447 total_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (!skb)
449 return;
450
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300451 skb_copy_to_linear_data(skb, msg, len);
Eric Dumazet954fba02012-06-12 19:30:21 +0000452 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300454 skb_push(skb, sizeof(*udph));
455 skb_reset_transport_header(skb);
456 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 udph->source = htons(np->local_port);
458 udph->dest = htons(np->remote_port);
459 udph->len = htons(udp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Cong Wangb3d936f2013-01-07 20:52:41 +0000461 if (np->ipv6) {
462 udph->check = 0;
463 udph->check = csum_ipv6_magic(&np->local_ip.in6,
464 &np->remote_ip.in6,
465 udp_len, IPPROTO_UDP,
466 csum_partial(udph, udp_len, 0));
467 if (udph->check == 0)
468 udph->check = CSUM_MANGLED_0;
469
470 skb_push(skb, sizeof(*ip6h));
471 skb_reset_network_header(skb);
472 ip6h = ipv6_hdr(skb);
473
474 /* ip6h->version = 6; ip6h->priority = 0; */
475 put_unaligned(0x60, (unsigned char *)ip6h);
476 ip6h->flow_lbl[0] = 0;
477 ip6h->flow_lbl[1] = 0;
478 ip6h->flow_lbl[2] = 0;
479
480 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
481 ip6h->nexthdr = IPPROTO_UDP;
482 ip6h->hop_limit = 32;
483 ip6h->saddr = np->local_ip.in6;
484 ip6h->daddr = np->remote_ip.in6;
485
486 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
487 skb_reset_mac_header(skb);
488 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
489 } else {
Cong Wangb7394d22013-01-07 20:52:39 +0000490 udph->check = 0;
491 udph->check = csum_tcpudp_magic(np->local_ip.ip,
492 np->remote_ip.ip,
493 udp_len, IPPROTO_UDP,
494 csum_partial(udph, udp_len, 0));
495 if (udph->check == 0)
496 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Cong Wangb7394d22013-01-07 20:52:39 +0000498 skb_push(skb, sizeof(*iph));
499 skb_reset_network_header(skb);
500 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Cong Wangb7394d22013-01-07 20:52:39 +0000502 /* iph->version = 4; iph->ihl = 5; */
503 put_unaligned(0x45, (unsigned char *)iph);
504 iph->tos = 0;
505 put_unaligned(htons(ip_len), &(iph->tot_len));
506 iph->id = htons(atomic_inc_return(&ip_ident));
507 iph->frag_off = 0;
508 iph->ttl = 64;
509 iph->protocol = IPPROTO_UDP;
510 iph->check = 0;
511 put_unaligned(np->local_ip.ip, &(iph->saddr));
512 put_unaligned(np->remote_ip.ip, &(iph->daddr));
513 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
514
515 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
516 skb_reset_mac_header(skb);
517 skb->protocol = eth->h_proto = htons(ETH_P_IP);
518 }
519
Joe Perchesc62326a2014-01-20 09:52:18 -0800520 ether_addr_copy(eth->h_source, np->dev->dev_addr);
521 ether_addr_copy(eth->h_dest, np->remote_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 skb->dev = np->dev;
524
525 netpoll_send_skb(np, skb);
526}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000527EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Cong Wangb7394d22013-01-07 20:52:39 +0000529static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Cong Wangb3d936f2013-01-07 20:52:41 +0000531 int size, type = ARPOP_REPLY;
Al Viro252e33462006-11-14 20:48:11 -0800532 __be32 sip, tip;
Neil Horman47bbec02006-12-08 00:05:55 -0800533 unsigned char *sha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct sk_buff *send_skb;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000535 struct netpoll *np, *tmp;
536 unsigned long flags;
Herbert Xuae641942011-11-18 02:20:04 +0000537 int hlen, tlen;
Cong Wangb7394d22013-01-07 20:52:39 +0000538 int hits = 0, proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Eric W. Biedermanff607632014-03-14 20:47:49 -0700540 if (!netpoll_rx_processing(npinfo))
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000541 return;
542
543 /* Before checking the packet, we do some early
544 inspection whether this is interesting at all */
545 spin_lock_irqsave(&npinfo->rx_lock, flags);
546 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
547 if (np->dev == skb->dev)
548 hits++;
549 }
550 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
551
552 /* No netpoll struct is using this dev */
553 if (!hits)
Jeff Moyer115c1d62005-06-22 22:05:31 -0700554 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Cong Wangb7394d22013-01-07 20:52:39 +0000556 proto = ntohs(eth_hdr(skb)->h_proto);
Sonic Zhangb0dd6632013-09-11 11:31:53 +0800557 if (proto == ETH_P_ARP) {
Cong Wangb3d936f2013-01-07 20:52:41 +0000558 struct arphdr *arp;
559 unsigned char *arp_ptr;
Cong Wangb7394d22013-01-07 20:52:39 +0000560 /* No arp on this interface */
561 if (skb->dev->flags & IFF_NOARP)
562 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Cong Wangb7394d22013-01-07 20:52:39 +0000564 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
565 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Cong Wangb7394d22013-01-07 20:52:39 +0000567 skb_reset_network_header(skb);
568 skb_reset_transport_header(skb);
569 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Cong Wangb7394d22013-01-07 20:52:39 +0000571 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
572 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
573 arp->ar_pro != htons(ETH_P_IP) ||
574 arp->ar_op != htons(ARPOP_REQUEST))
575 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Cong Wangb7394d22013-01-07 20:52:39 +0000577 arp_ptr = (unsigned char *)(arp+1);
578 /* save the location of the src hw addr */
579 sha = arp_ptr;
580 arp_ptr += skb->dev->addr_len;
581 memcpy(&sip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000582 arp_ptr += 4;
Cong Wangb7394d22013-01-07 20:52:39 +0000583 /* If we actually cared about dst hw addr,
584 it would get copied here */
585 arp_ptr += skb->dev->addr_len;
586 memcpy(&tip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000587
Cong Wangb7394d22013-01-07 20:52:39 +0000588 /* Should we ignore arp? */
589 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
590 return;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000591
Cong Wangb7394d22013-01-07 20:52:39 +0000592 size = arp_hdr_len(skb->dev);
593
594 spin_lock_irqsave(&npinfo->rx_lock, flags);
595 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
596 if (tip != np->local_ip.ip)
597 continue;
598
599 hlen = LL_RESERVED_SPACE(np->dev);
600 tlen = np->dev->needed_tailroom;
601 send_skb = find_skb(np, size + hlen + tlen, hlen);
602 if (!send_skb)
603 continue;
604
605 skb_reset_network_header(send_skb);
606 arp = (struct arphdr *) skb_put(send_skb, size);
607 send_skb->dev = skb->dev;
608 send_skb->protocol = htons(ETH_P_ARP);
609
610 /* Fill the device header for the ARP frame */
Cong Wangb3d936f2013-01-07 20:52:41 +0000611 if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
Cong Wangb7394d22013-01-07 20:52:39 +0000612 sha, np->dev->dev_addr,
613 send_skb->len) < 0) {
614 kfree_skb(send_skb);
615 continue;
616 }
617
618 /*
619 * Fill out the arp protocol part.
620 *
621 * we only support ethernet device type,
622 * which (according to RFC 1390) should
623 * always equal 1 (Ethernet).
624 */
625
626 arp->ar_hrd = htons(np->dev->type);
627 arp->ar_pro = htons(ETH_P_IP);
628 arp->ar_hln = np->dev->addr_len;
629 arp->ar_pln = 4;
630 arp->ar_op = htons(type);
631
632 arp_ptr = (unsigned char *)(arp + 1);
633 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
634 arp_ptr += np->dev->addr_len;
635 memcpy(arp_ptr, &tip, 4);
636 arp_ptr += 4;
637 memcpy(arp_ptr, sha, np->dev->addr_len);
638 arp_ptr += np->dev->addr_len;
639 memcpy(arp_ptr, &sip, 4);
640
641 netpoll_send_skb(np, send_skb);
642
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200643 /* If there are several rx_skb_hooks for the same
644 * address we're fine by sending a single reply
645 */
Cong Wangb7394d22013-01-07 20:52:39 +0000646 break;
647 }
648 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Cong Wangb3d936f2013-01-07 20:52:41 +0000649 } else if( proto == ETH_P_IPV6) {
650#if IS_ENABLED(CONFIG_IPV6)
651 struct nd_msg *msg;
652 u8 *lladdr = NULL;
653 struct ipv6hdr *hdr;
654 struct icmp6hdr *icmp6h;
655 const struct in6_addr *saddr;
656 const struct in6_addr *daddr;
657 struct inet6_dev *in6_dev = NULL;
658 struct in6_addr *target;
659
660 in6_dev = in6_dev_get(skb->dev);
661 if (!in6_dev || !in6_dev->cnf.accept_ra)
662 return;
663
664 if (!pskb_may_pull(skb, skb->len))
665 return;
666
667 msg = (struct nd_msg *)skb_transport_header(skb);
668
669 __skb_push(skb, skb->data - skb_transport_header(skb));
670
671 if (ipv6_hdr(skb)->hop_limit != 255)
672 return;
673 if (msg->icmph.icmp6_code != 0)
674 return;
675 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
676 return;
677
678 saddr = &ipv6_hdr(skb)->saddr;
679 daddr = &ipv6_hdr(skb)->daddr;
680
681 size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
682
683 spin_lock_irqsave(&npinfo->rx_lock, flags);
684 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
Cong Wangfaeed822013-01-27 15:55:20 +0000685 if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
Cong Wangb3d936f2013-01-07 20:52:41 +0000686 continue;
687
688 hlen = LL_RESERVED_SPACE(np->dev);
689 tlen = np->dev->needed_tailroom;
690 send_skb = find_skb(np, size + hlen + tlen, hlen);
691 if (!send_skb)
692 continue;
693
694 send_skb->protocol = htons(ETH_P_IPV6);
695 send_skb->dev = skb->dev;
696
697 skb_reset_network_header(send_skb);
Amerigo Wang00f97da2013-06-03 16:31:36 +0000698 hdr = (struct ipv6hdr *) skb_put(send_skb, sizeof(struct ipv6hdr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000699 *(__be32*)hdr = htonl(0x60000000);
Cong Wangb3d936f2013-01-07 20:52:41 +0000700 hdr->payload_len = htons(size);
701 hdr->nexthdr = IPPROTO_ICMPV6;
702 hdr->hop_limit = 255;
703 hdr->saddr = *saddr;
704 hdr->daddr = *daddr;
705
Amerigo Wang00f97da2013-06-03 16:31:36 +0000706 icmp6h = (struct icmp6hdr *) skb_put(send_skb, sizeof(struct icmp6hdr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000707 icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
708 icmp6h->icmp6_router = 0;
709 icmp6h->icmp6_solicited = 1;
Amerigo Wang00f97da2013-06-03 16:31:36 +0000710
711 target = (struct in6_addr *) skb_put(send_skb, sizeof(struct in6_addr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000712 *target = msg->target;
713 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
714 IPPROTO_ICMPV6,
715 csum_partial(icmp6h,
716 size, 0));
717
718 if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
719 lladdr, np->dev->dev_addr,
720 send_skb->len) < 0) {
721 kfree_skb(send_skb);
722 continue;
723 }
724
725 netpoll_send_skb(np, send_skb);
726
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200727 /* If there are several rx_skb_hooks for the same
728 * address, we're fine by sending a single reply
729 */
Cong Wangb3d936f2013-01-07 20:52:41 +0000730 break;
731 }
732 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
733#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Cong Wangb3d936f2013-01-07 20:52:41 +0000737static bool pkt_is_ns(struct sk_buff *skb)
738{
739 struct nd_msg *msg;
740 struct ipv6hdr *hdr;
741
742 if (skb->protocol != htons(ETH_P_ARP))
743 return false;
744 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
745 return false;
746
747 msg = (struct nd_msg *)skb_transport_header(skb);
748 __skb_push(skb, skb->data - skb_transport_header(skb));
749 hdr = ipv6_hdr(skb);
750
751 if (hdr->nexthdr != IPPROTO_ICMPV6)
752 return false;
753 if (hdr->hop_limit != 255)
754 return false;
755 if (msg->icmph.icmp6_code != 0)
756 return false;
757 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
758 return false;
759
760 return true;
761}
762
Amerigo Wang57c5d462012-08-10 01:24:40 +0000763int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200765 int proto, len, ulen, data_len;
766 int hits = 0, offset;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000767 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct udphdr *uh;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000769 struct netpoll *np, *tmp;
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200770 uint16_t source;
Neil Horman068c6e92006-06-26 00:04:27 -0700771
Eric W. Biedermanff607632014-03-14 20:47:49 -0700772 if (!netpoll_rx_processing(npinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 goto out;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (skb->dev->type != ARPHRD_ETHER)
776 goto out;
777
David S. Millerd9452e92008-03-04 12:28:49 -0800778 /* check if netpoll clients need ARP */
Cong Wangb3d936f2013-01-07 20:52:41 +0000779 if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
780 skb_queue_tail(&npinfo->neigh_tx, skb);
781 return 1;
782 } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
Cong Wangb7394d22013-01-07 20:52:39 +0000783 skb_queue_tail(&npinfo->neigh_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 1;
785 }
786
Amerigo Wang689971b2012-08-10 01:24:49 +0000787 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
788 skb = vlan_untag(skb);
789 if (unlikely(!skb))
790 goto out;
791 }
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 proto = ntohs(eth_hdr(skb)->h_proto);
Cong Wangb7394d22013-01-07 20:52:39 +0000794 if (proto != ETH_P_IP && proto != ETH_P_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 goto out;
796 if (skb->pkt_type == PACKET_OTHERHOST)
797 goto out;
798 if (skb_shared(skb))
799 goto out;
800
Cong Wangb7394d22013-01-07 20:52:39 +0000801 if (proto == ETH_P_IP) {
802 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
803 goto out;
804 iph = (struct iphdr *)skb->data;
805 if (iph->ihl < 5 || iph->version != 4)
806 goto out;
807 if (!pskb_may_pull(skb, iph->ihl*4))
808 goto out;
809 iph = (struct iphdr *)skb->data;
810 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
811 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Cong Wangb7394d22013-01-07 20:52:39 +0000813 len = ntohs(iph->tot_len);
814 if (skb->len < len || len < iph->ihl*4)
815 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Cong Wangb7394d22013-01-07 20:52:39 +0000817 /*
818 * Our transport medium may have padded the buffer out.
819 * Now We trim to the true length of the frame.
820 */
821 if (pskb_trim_rcsum(skb, len))
822 goto out;
Aubrey.Li5e7d7fa2007-04-17 12:40:20 -0700823
Cong Wangb7394d22013-01-07 20:52:39 +0000824 iph = (struct iphdr *)skb->data;
825 if (iph->protocol != IPPROTO_UDP)
826 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Cong Wangb7394d22013-01-07 20:52:39 +0000828 len -= iph->ihl*4;
829 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200830 offset = (unsigned char *)(uh + 1) - skb->data;
Cong Wangb7394d22013-01-07 20:52:39 +0000831 ulen = ntohs(uh->len);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200832 data_len = skb->len - offset;
833 source = ntohs(uh->source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Cong Wangb7394d22013-01-07 20:52:39 +0000835 if (ulen != len)
836 goto out;
837 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
838 goto out;
839 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
840 if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
841 continue;
842 if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
843 continue;
844 if (np->local_port && np->local_port != ntohs(uh->dest))
845 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200847 np->rx_skb_hook(np, source, skb, offset, data_len);
Cong Wangb7394d22013-01-07 20:52:39 +0000848 hits++;
849 }
Cong Wangb3d936f2013-01-07 20:52:41 +0000850 } else {
851#if IS_ENABLED(CONFIG_IPV6)
852 const struct ipv6hdr *ip6h;
853
854 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
855 goto out;
856 ip6h = (struct ipv6hdr *)skb->data;
857 if (ip6h->version != 6)
858 goto out;
859 len = ntohs(ip6h->payload_len);
860 if (!len)
861 goto out;
862 if (len + sizeof(struct ipv6hdr) > skb->len)
863 goto out;
864 if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
865 goto out;
866 ip6h = ipv6_hdr(skb);
867 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
868 goto out;
869 uh = udp_hdr(skb);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200870 offset = (unsigned char *)(uh + 1) - skb->data;
Cong Wangb3d936f2013-01-07 20:52:41 +0000871 ulen = ntohs(uh->len);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200872 data_len = skb->len - offset;
873 source = ntohs(uh->source);
Cong Wangb3d936f2013-01-07 20:52:41 +0000874 if (ulen != skb->len)
875 goto out;
876 if (udp6_csum_init(skb, uh, IPPROTO_UDP))
877 goto out;
878 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
Cong Wangfaeed822013-01-27 15:55:20 +0000879 if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
Cong Wangb3d936f2013-01-07 20:52:41 +0000880 continue;
Cong Wangfaeed822013-01-27 15:55:20 +0000881 if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
Cong Wangb3d936f2013-01-07 20:52:41 +0000882 continue;
883 if (np->local_port && np->local_port != ntohs(uh->dest))
884 continue;
885
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200886 np->rx_skb_hook(np, source, skb, offset, data_len);
Cong Wangb3d936f2013-01-07 20:52:41 +0000887 hits++;
888 }
889#endif
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000890 }
891
892 if (!hits)
893 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 kfree_skb(skb);
896 return 1;
897
898out:
899 if (atomic_read(&trapped)) {
900 kfree_skb(skb);
901 return 1;
902 }
903
904 return 0;
905}
906
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700907void netpoll_print_options(struct netpoll *np)
908{
Joe Perchese6ec269352012-01-29 15:50:43 +0000909 np_info(np, "local port %d\n", np->local_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000910 if (np->ipv6)
911 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
912 else
Cong Wangb7394d22013-01-07 20:52:39 +0000913 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000914 np_info(np, "interface '%s'\n", np->dev_name);
915 np_info(np, "remote port %d\n", np->remote_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000916 if (np->ipv6)
917 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
918 else
Cong Wangb7394d22013-01-07 20:52:39 +0000919 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000920 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700921}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000922EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700923
Cong Wangb7394d22013-01-07 20:52:39 +0000924static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
925{
926 const char *end;
927
928 if (!strchr(str, ':') &&
929 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
930 if (!*end)
931 return 0;
932 }
933 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
934#if IS_ENABLED(CONFIG_IPV6)
935 if (!*end)
936 return 1;
937#else
938 return -1;
939#endif
940 }
941 return -1;
942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944int netpoll_parse_options(struct netpoll *np, char *opt)
945{
946 char *cur=opt, *delim;
Cong Wangb7394d22013-01-07 20:52:39 +0000947 int ipv6;
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100948 bool ipversion_set = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
David S. Millerc68b9072006-11-14 20:40:49 -0800950 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if ((delim = strchr(cur, '@')) == NULL)
952 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800953 *delim = 0;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000954 if (kstrtou16(cur, 10, &np->local_port))
955 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800956 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
David S. Millerc68b9072006-11-14 20:40:49 -0800960 if (*cur != '/') {
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100961 ipversion_set = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if ((delim = strchr(cur, '/')) == NULL)
963 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800964 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000965 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
966 if (ipv6 < 0)
967 goto parse_failed;
968 else
969 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800970 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972 cur++;
973
David S. Millerc68b9072006-11-14 20:40:49 -0800974 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* parse out dev name */
976 if ((delim = strchr(cur, ',')) == NULL)
977 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800978 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800980 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 cur++;
983
David S. Millerc68b9072006-11-14 20:40:49 -0800984 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* dst port */
986 if ((delim = strchr(cur, '@')) == NULL)
987 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800988 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000989 if (*cur == ' ' || *cur == '\t')
Joe Perchese6ec269352012-01-29 15:50:43 +0000990 np_info(np, "warning: whitespace is not allowed\n");
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000991 if (kstrtou16(cur, 10, &np->remote_port))
992 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800993 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /* dst ip */
998 if ((delim = strchr(cur, '/')) == NULL)
999 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -08001000 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +00001001 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
1002 if (ipv6 < 0)
1003 goto parse_failed;
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +01001004 else if (ipversion_set && np->ipv6 != (bool)ipv6)
Cong Wangb7394d22013-01-07 20:52:39 +00001005 goto parse_failed;
1006 else
1007 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -08001008 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
David S. Millerc68b9072006-11-14 20:40:49 -08001010 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* MAC address */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001012 if (!mac_pton(cur, np->remote_mac))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto parse_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
Satyam Sharma0bcc1812007-08-10 15:35:05 -07001016 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 return 0;
1019
1020 parse_failed:
Joe Perchese6ec269352012-01-29 15:50:43 +00001021 np_info(np, "couldn't parse config at '%s'!\n", cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return -1;
1023}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001024EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Amerigo Wang47be03a22012-08-10 01:24:37 +00001026int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001027{
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001028 struct netpoll_info *npinfo;
1029 const struct net_device_ops *ops;
1030 unsigned long flags;
1031 int err;
1032
Jiri Pirko30fdd8a02012-07-17 05:22:35 +00001033 np->dev = ndev;
1034 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
Neil Horman2cde6ac2013-02-11 10:25:30 +00001035 INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
Jiri Pirko30fdd8a02012-07-17 05:22:35 +00001036
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001037 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
1038 !ndev->netdev_ops->ndo_poll_controller) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001039 np_err(np, "%s doesn't support polling, aborting\n",
1040 np->dev_name);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001041 err = -ENOTSUPP;
1042 goto out;
1043 }
1044
1045 if (!ndev->npinfo) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001046 npinfo = kmalloc(sizeof(*npinfo), gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001047 if (!npinfo) {
1048 err = -ENOMEM;
1049 goto out;
1050 }
1051
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001052 INIT_LIST_HEAD(&npinfo->rx_np);
1053
1054 spin_lock_init(&npinfo->rx_lock);
Neil Hormanbd7c4b62013-04-30 05:35:05 +00001055 sema_init(&npinfo->dev_lock, 1);
Cong Wangb7394d22013-01-07 20:52:39 +00001056 skb_queue_head_init(&npinfo->neigh_tx);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001057 skb_queue_head_init(&npinfo->txq);
1058 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
1059
1060 atomic_set(&npinfo->refcnt, 1);
1061
1062 ops = np->dev->netdev_ops;
1063 if (ops->ndo_netpoll_setup) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001064 err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001065 if (err)
1066 goto free_npinfo;
1067 }
1068 } else {
Neil Horman0790bbb2013-02-11 10:25:31 +00001069 npinfo = rtnl_dereference(ndev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001070 atomic_inc(&npinfo->refcnt);
1071 }
1072
1073 npinfo->netpoll = np;
1074
Antonio Quartulli8fb479a2013-10-23 23:36:30 +02001075 if (np->rx_skb_hook) {
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001076 spin_lock_irqsave(&npinfo->rx_lock, flags);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001077 list_add_tail(&np->rx, &npinfo->rx_np);
1078 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1079 }
1080
1081 /* last thing to do is link it to the net device structure */
Eric Dumazetcf778b02012-01-12 04:41:32 +00001082 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001083
1084 return 0;
1085
1086free_npinfo:
1087 kfree(npinfo);
1088out:
1089 return err;
1090}
1091EXPORT_SYMBOL_GPL(__netpoll_setup);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093int netpoll_setup(struct netpoll *np)
1094{
1095 struct net_device *ndev = NULL;
1096 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001097 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Cong Wangf92d3182013-01-14 23:34:06 +00001099 rtnl_lock();
Cong Wang556e6252013-01-27 15:55:21 +00001100 if (np->dev_name) {
1101 struct net *net = current->nsproxy->net_ns;
1102 ndev = __dev_get_by_name(net, np->dev_name);
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!ndev) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001105 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
Cong Wangf92d3182013-01-14 23:34:06 +00001106 err = -ENODEV;
1107 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Cong Wang5bd30d32013-01-17 12:21:08 +08001109 dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Jiri Pirko49bd8fb2013-01-03 22:48:55 +00001111 if (netdev_master_upper_dev_get(ndev)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001112 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
Dan Carpenter83fe32d2011-06-11 18:55:22 -07001113 err = -EBUSY;
1114 goto put;
WANG Cong0c1ad042011-06-09 00:28:13 -07001115 }
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!netif_running(ndev)) {
1118 unsigned long atmost, atleast;
1119
Joe Perchese6ec269352012-01-29 15:50:43 +00001120 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001122 err = dev_open(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001123
1124 if (err) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001125 np_err(np, "failed to open %s\n", ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +00001126 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Cong Wangf92d3182013-01-14 23:34:06 +00001129 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -07001131 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 while (!netif_carrier_ok(ndev)) {
1133 if (time_after(jiffies, atmost)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001134 np_notice(np, "timeout waiting for carrier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 break;
1136 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -07001137 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
1140 /* If carrier appears to come up instantly, we don't
1141 * trust it and pause so that we don't pump all our
1142 * queued console messages into the bitbucket.
1143 */
1144
1145 if (time_before(jiffies, atleast)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001146 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 msleep(4000);
1148 }
Cong Wangf92d3182013-01-14 23:34:06 +00001149 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
Cong Wangb7394d22013-01-07 20:52:39 +00001152 if (!np->local_ip.ip) {
1153 if (!np->ipv6) {
Cong Wangf92d3182013-01-14 23:34:06 +00001154 in_dev = __in_dev_get_rtnl(ndev);
Cong Wangb7394d22013-01-07 20:52:39 +00001155
1156 if (!in_dev || !in_dev->ifa_list) {
Cong Wangb7394d22013-01-07 20:52:39 +00001157 np_err(np, "no IP address for %s, aborting\n",
1158 np->dev_name);
1159 err = -EDESTADDRREQ;
1160 goto put;
1161 }
1162
1163 np->local_ip.ip = in_dev->ifa_list->ifa_local;
Cong Wangb7394d22013-01-07 20:52:39 +00001164 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
Cong Wangb3d936f2013-01-07 20:52:41 +00001165 } else {
1166#if IS_ENABLED(CONFIG_IPV6)
1167 struct inet6_dev *idev;
1168
1169 err = -EDESTADDRREQ;
Cong Wangb3d936f2013-01-07 20:52:41 +00001170 idev = __in6_dev_get(ndev);
1171 if (idev) {
1172 struct inet6_ifaddr *ifp;
1173
1174 read_lock_bh(&idev->lock);
1175 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1176 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
1177 continue;
1178 np->local_ip.in6 = ifp->addr;
1179 err = 0;
1180 break;
1181 }
1182 read_unlock_bh(&idev->lock);
1183 }
Cong Wangb3d936f2013-01-07 20:52:41 +00001184 if (err) {
1185 np_err(np, "no IPv6 address for %s, aborting\n",
1186 np->dev_name);
1187 goto put;
1188 } else
1189 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
1190#else
1191 np_err(np, "IPv6 is not supported %s, aborting\n",
1192 np->dev_name);
Cong Wange39363a2013-01-22 17:39:11 +00001193 err = -EINVAL;
Cong Wangb3d936f2013-01-07 20:52:41 +00001194 goto put;
1195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198
Herbert Xudbaa1542010-06-10 16:12:46 +00001199 /* fill up the skb queue */
1200 refill_skbs();
1201
Amerigo Wang47be03a22012-08-10 01:24:37 +00001202 err = __netpoll_setup(np, ndev, GFP_KERNEL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001203 if (err)
1204 goto put;
1205
Cong Wangf92d3182013-01-14 23:34:06 +00001206 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return 0;
1208
Jiri Slaby21edbb22010-03-16 05:29:54 +00001209put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 dev_put(ndev);
Cong Wangf92d3182013-01-14 23:34:06 +00001211unlock:
1212 rtnl_unlock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001215EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
David S. Millerc68b9072006-11-14 20:40:49 -08001217static int __init netpoll_init(void)
1218{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -08001219 skb_queue_head_init(&skb_pool);
1220 return 0;
1221}
1222core_initcall(netpoll_init);
1223
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001224static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
1225{
1226 struct netpoll_info *npinfo =
1227 container_of(rcu_head, struct netpoll_info, rcu);
1228
Cong Wangb7394d22013-01-07 20:52:39 +00001229 skb_queue_purge(&npinfo->neigh_tx);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001230 skb_queue_purge(&npinfo->txq);
1231
1232 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
1233 cancel_delayed_work(&npinfo->tx_work);
1234
1235 /* clean after last, unfinished work */
1236 __skb_queue_purge(&npinfo->txq);
1237 /* now cancel it again */
1238 cancel_delayed_work(&npinfo->tx_work);
1239 kfree(npinfo);
1240}
1241
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001242void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -07001244 struct netpoll_info *npinfo;
1245 unsigned long flags;
1246
Neil Horman0790bbb2013-02-11 10:25:31 +00001247 /* rtnl_dereference would be preferable here but
1248 * rcu_cleanup_netpoll path can put us in here safely without
1249 * holding the rtnl, so plain rcu_dereference it is
1250 */
1251 npinfo = rtnl_dereference(np->dev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001252 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +00001253 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -07001254
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001255 if (!list_empty(&npinfo->rx_np)) {
1256 spin_lock_irqsave(&npinfo->rx_lock, flags);
1257 list_del(&np->rx);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001258 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Jeff Moyer115c1d62005-06-22 22:05:31 -07001259 }
Herbert Xudbaa1542010-06-10 16:12:46 +00001260
Neil Hormanca99ca12013-02-05 08:05:43 +00001261 synchronize_srcu(&netpoll_srcu);
1262
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001263 if (atomic_dec_and_test(&npinfo->refcnt)) {
1264 const struct net_device_ops *ops;
1265
1266 ops = np->dev->netdev_ops;
1267 if (ops->ndo_netpoll_cleanup)
1268 ops->ndo_netpoll_cleanup(np->dev);
1269
Neil Horman2cde6ac2013-02-11 10:25:30 +00001270 rcu_assign_pointer(np->dev->npinfo, NULL);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001271 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
Herbert Xudbaa1542010-06-10 16:12:46 +00001272 }
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001273}
1274EXPORT_SYMBOL_GPL(__netpoll_cleanup);
1275
Neil Horman2cde6ac2013-02-11 10:25:30 +00001276static void netpoll_async_cleanup(struct work_struct *work)
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001277{
Neil Horman2cde6ac2013-02-11 10:25:30 +00001278 struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001279
Neil Horman2cde6ac2013-02-11 10:25:30 +00001280 rtnl_lock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001281 __netpoll_cleanup(np);
Neil Horman2cde6ac2013-02-11 10:25:30 +00001282 rtnl_unlock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001283 kfree(np);
1284}
1285
Neil Horman2cde6ac2013-02-11 10:25:30 +00001286void __netpoll_free_async(struct netpoll *np)
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001287{
Neil Horman2cde6ac2013-02-11 10:25:30 +00001288 schedule_work(&np->cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001289}
Neil Horman2cde6ac2013-02-11 10:25:30 +00001290EXPORT_SYMBOL_GPL(__netpoll_free_async);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001291
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001292void netpoll_cleanup(struct netpoll *np)
1293{
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001294 rtnl_lock();
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +02001295 if (!np->dev)
1296 goto out;
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001297 __netpoll_cleanup(np);
Herbert Xudbaa1542010-06-10 16:12:46 +00001298 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 np->dev = NULL;
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +02001300out:
1301 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001303EXPORT_SYMBOL(netpoll_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305int netpoll_trap(void)
1306{
1307 return atomic_read(&trapped);
1308}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001309EXPORT_SYMBOL(netpoll_trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311void netpoll_set_trap(int trap)
1312{
1313 if (trap)
1314 atomic_inc(&trapped);
1315 else
1316 atomic_dec(&trapped);
1317}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318EXPORT_SYMBOL(netpoll_set_trap);