blob: 35a9f0804b6f971a3982e2118a20369f80851493 [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
David S. Millerd9452e92008-03-04 12:28:49 -080054#define NETPOLL_RX_ENABLED 1
55#define NETPOLL_RX_DROP 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Joe Perches6f706242012-01-29 15:50:44 +000057#define MAX_SKB_SIZE \
58 (sizeof(struct ethhdr) + \
59 sizeof(struct iphdr) + \
60 sizeof(struct udphdr) + \
61 MAX_UDP_CHUNK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
David S. Miller3578b0c2010-08-03 00:24:04 -070063static void zap_completion_queue(void);
Cong Wangb7394d22013-01-07 20:52:39 +000064static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
Neil Horman2cde6ac2013-02-11 10:25:30 +000065static void netpoll_async_cleanup(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Anton Vorontsovbff38772009-07-08 11:10:56 -070067static unsigned int carrier_timeout = 4;
68module_param(carrier_timeout, uint, 0644);
69
Joe Perchese6ec269352012-01-29 15:50:43 +000070#define np_info(np, fmt, ...) \
71 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
72#define np_err(np, fmt, ...) \
73 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
74#define np_notice(np, fmt, ...) \
75 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
76
David Howellsc4028952006-11-22 14:57:56 +000077static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
David Howells4c1ac1b2006-12-05 14:37:56 +000079 struct netpoll_info *npinfo =
80 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +010082 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070084 while ((skb = skb_dequeue(&npinfo->txq))) {
85 struct net_device *dev = skb->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -080086 const struct net_device_ops *ops = dev->netdev_ops;
David S. Millerfd2ea0a2008-07-17 01:56:23 -070087 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070089 if (!netif_device_present(dev) || !netif_running(dev)) {
90 __kfree_skb(skb);
91 continue;
92 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
David S. Millerfd2ea0a2008-07-17 01:56:23 -070094 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
95
Ingo Molnar36405432006-12-12 17:20:42 +010096 local_irq_save(flags);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070097 __netif_tx_lock(txq, smp_processor_id());
Tom Herbert734664982011-11-28 16:32:44 +000098 if (netif_xmit_frozen_or_stopped(txq) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080099 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700100 skb_queue_head(&npinfo->txq, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700101 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100102 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jarek Poplawski25442ca2007-07-05 17:42:44 -0700104 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700105 return;
106 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700107 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100108 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110}
111
Al Virob51655b2006-11-14 21:40:42 -0800112static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
113 unsigned short ulen, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Al Virod6f5493c2006-11-14 21:26:08 -0800115 __wsum psum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800116
Herbert Xu60476372007-04-09 11:59:39 -0700117 if (uh->check == 0 || skb_csum_unnecessary(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119
Herbert Xufb286bb2005-11-10 13:01:24 -0800120 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Patrick McHardy84fa7932006-08-29 16:44:56 -0700122 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Al Virod3bc23e2006-11-14 21:24:49 -0800123 !csum_fold(csum_add(psum, skb->csum)))
Herbert Xufb286bb2005-11-10 13:01:24 -0800124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Herbert Xufb286bb2005-11-10 13:01:24 -0800126 skb->csum = psum;
127
128 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/*
132 * Check whether delayed processing was scheduled for our NIC. If so,
133 * we attempt to grab the poll lock and use ->poll() to pump the card.
134 * If this fails, either we've recursed in ->poll() or it's already
135 * running on another CPU.
136 *
137 * Note: we don't mask interrupts with this lock because we're using
138 * trylock here and interrupts are already disabled in the softirq
139 * case. Further, we test the poll_owner to avoid recursion on UP
140 * systems where the lock doesn't exist.
141 *
142 * In cases where there is bi-directional communications, reading only
143 * one message at a time can lead to packets being dropped by the
144 * network adapter, forcing superfluous retries and possibly timeouts.
145 * Thus, we set our budget to greater than 1.
146 */
David S. Miller0a7606c2007-10-29 21:28:47 -0700147static int poll_one_napi(struct netpoll_info *npinfo,
148 struct napi_struct *napi, int budget)
149{
150 int work;
151
152 /* net_rx_action's ->poll() invocations and our's are
153 * synchronized by this test which is only made while
154 * holding the napi->poll_lock.
155 */
156 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
157 return budget;
158
David S. Millerd9452e92008-03-04 12:28:49 -0800159 npinfo->rx_flags |= NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700160 atomic_inc(&trapped);
Neil Horman7b363e42008-12-09 23:22:26 -0800161 set_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700162
163 work = napi->poll(napi, budget);
David S. Miller7d18f112009-05-21 23:30:09 -0700164 trace_napi_poll(napi);
David S. Miller0a7606c2007-10-29 21:28:47 -0700165
Neil Horman7b363e42008-12-09 23:22:26 -0800166 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700167 atomic_dec(&trapped);
David S. Millerd9452e92008-03-04 12:28:49 -0800168 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700169
170 return budget - work;
171}
172
Stephen Hemminger51069302007-11-19 19:18:11 -0800173static void poll_napi(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700175 struct napi_struct *napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int budget = 16;
177
Neil Hormanf13d4932010-10-19 07:04:26 +0000178 list_for_each_entry(napi, &dev->napi_list, dev_list) {
David S. Miller0a7606c2007-10-29 21:28:47 -0700179 if (napi->poll_owner != smp_processor_id() &&
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700180 spin_trylock(&napi->poll_lock)) {
Amerigo Wang28996562012-08-10 01:24:42 +0000181 budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
182 napi, budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700183 spin_unlock(&napi->poll_lock);
David S. Miller0a7606c2007-10-29 21:28:47 -0700184
Amerigo Wang072a9c42012-08-24 21:41:11 +0000185 if (!budget)
David S. Miller0a7606c2007-10-29 21:28:47 -0700186 break;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189}
190
Cong Wangb7394d22013-01-07 20:52:39 +0000191static void service_neigh_queue(struct netpoll_info *npi)
Neil Horman068c6e92006-06-26 00:04:27 -0700192{
Stephen Hemminger51069302007-11-19 19:18:11 -0800193 if (npi) {
194 struct sk_buff *skb;
Neil Horman068c6e92006-06-26 00:04:27 -0700195
Cong Wangb7394d22013-01-07 20:52:39 +0000196 while ((skb = skb_dequeue(&npi->neigh_tx)))
197 netpoll_neigh_reply(skb, npi);
Neil Horman068c6e92006-06-26 00:04:27 -0700198 }
Neil Horman068c6e92006-06-26 00:04:27 -0700199}
200
Joe Perches234b9212011-06-30 15:08:57 +0000201static void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000203 const struct net_device_ops *ops;
Amerigo Wang28996562012-08-10 01:24:42 +0000204 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
Stephen Hemminger51069302007-11-19 19:18:11 -0800205
Neil Hormanca99ca12013-02-05 08:05:43 +0000206 /* Don't do any rx activity if the dev_lock mutex is held
207 * the dev_open/close paths use this to block netpoll activity
208 * while changing device state
209 */
Dan Carpentera3dbbc22013-05-06 02:15:13 +0000210 if (down_trylock(&ni->dev_lock))
Neil Hormanca99ca12013-02-05 08:05:43 +0000211 return;
212
Neil Horman959d5fd2013-02-13 11:32:42 -0500213 if (!netif_running(dev)) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000214 up(&ni->dev_lock);
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000215 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500216 }
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000217
218 ops = dev->netdev_ops;
Neil Horman959d5fd2013-02-13 11:32:42 -0500219 if (!ops->ndo_poll_controller) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000220 up(&ni->dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* Process pending work on NIC */
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800225 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Stephen Hemminger51069302007-11-19 19:18:11 -0800227 poll_napi(dev);
228
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000229 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000230
Eric Dumazet58e05f32012-02-14 10:11:59 +0000231 if (dev->flags & IFF_SLAVE) {
Amerigo Wang28996562012-08-10 01:24:42 +0000232 if (ni) {
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000233 struct net_device *bond_dev;
Amerigo Wang5a698af2011-02-17 23:43:34 +0000234 struct sk_buff *skb;
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000235 struct netpoll_info *bond_ni;
236
237 bond_dev = netdev_master_upper_dev_get_rcu(dev);
238 bond_ni = rcu_dereference_bh(bond_dev->npinfo);
Cong Wangb7394d22013-01-07 20:52:39 +0000239 while ((skb = skb_dequeue(&ni->neigh_tx))) {
Amerigo Wang5a698af2011-02-17 23:43:34 +0000240 skb->dev = bond_dev;
Cong Wangb7394d22013-01-07 20:52:39 +0000241 skb_queue_tail(&bond_ni->neigh_tx, skb);
Amerigo Wang5a698af2011-02-17 23:43:34 +0000242 }
243 }
244 }
245
Cong Wangb7394d22013-01-07 20:52:39 +0000246 service_neigh_queue(ni);
Neil Horman068c6e92006-06-26 00:04:27 -0700247
David S. Miller3578b0c2010-08-03 00:24:04 -0700248 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Neil Hormanca99ca12013-02-05 08:05:43 +0000251int netpoll_rx_disable(struct net_device *dev)
252{
253 struct netpoll_info *ni;
254 int idx;
255 might_sleep();
256 idx = srcu_read_lock(&netpoll_srcu);
257 ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
258 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000259 down(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000260 srcu_read_unlock(&netpoll_srcu, idx);
261 return 0;
262}
263EXPORT_SYMBOL(netpoll_rx_disable);
264
265void netpoll_rx_enable(struct net_device *dev)
266{
267 struct netpoll_info *ni;
268 rcu_read_lock();
269 ni = rcu_dereference(dev->npinfo);
270 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000271 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000272 rcu_read_unlock();
273}
274EXPORT_SYMBOL(netpoll_rx_enable);
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static void refill_skbs(void)
277{
278 struct sk_buff *skb;
279 unsigned long flags;
280
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800281 spin_lock_irqsave(&skb_pool.lock, flags);
282 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
284 if (!skb)
285 break;
286
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800287 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800289 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
David S. Miller3578b0c2010-08-03 00:24:04 -0700292static void zap_completion_queue(void)
293{
294 unsigned long flags;
295 struct softnet_data *sd = &get_cpu_var(softnet_data);
296
297 if (sd->completion_queue) {
298 struct sk_buff *clist;
299
300 local_irq_save(flags);
301 clist = sd->completion_queue;
302 sd->completion_queue = NULL;
303 local_irq_restore(flags);
304
305 while (clist != NULL) {
306 struct sk_buff *skb = clist;
307 clist = clist->next;
308 if (skb->destructor) {
309 atomic_inc(&skb->users);
310 dev_kfree_skb_any(skb); /* put this one back */
311 } else {
312 __kfree_skb(skb);
313 }
314 }
315 }
316
317 put_cpu_var(softnet_data);
318}
319
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800320static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800322 int count = 0;
323 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
David S. Miller3578b0c2010-08-03 00:24:04 -0700325 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800326 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800330 if (!skb)
331 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800334 if (++count < 10) {
Joe Perches2a49e002011-06-30 15:08:58 +0000335 netpoll_poll_dev(np->dev);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800336 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800338 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341 atomic_set(&skb->users, 1);
342 skb_reserve(skb, reserve);
343 return skb;
344}
345
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700346static int netpoll_owner_active(struct net_device *dev)
347{
348 struct napi_struct *napi;
349
350 list_for_each_entry(napi, &dev->napi_list, dev_list) {
351 if (napi->poll_owner == smp_processor_id())
352 return 1;
353 }
354 return 0;
355}
356
Amerigo Wang28996562012-08-10 01:24:42 +0000357/* call with IRQ disabled */
Neil Hormanc2355e12010-10-13 16:01:49 +0000358void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
359 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700361 int status = NETDEV_TX_BUSY;
362 unsigned long tries;
Stephen Hemminger00829822008-11-20 20:14:53 -0800363 const struct net_device_ops *ops = dev->netdev_ops;
Herbert Xude85d992010-06-10 16:12:44 +0000364 /* It is up to the caller to keep npinfo alive. */
Amerigo Wang28996562012-08-10 01:24:42 +0000365 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Amerigo Wang28996562012-08-10 01:24:42 +0000367 WARN_ON_ONCE(!irqs_disabled());
368
369 npinfo = rcu_dereference_bh(np->dev->npinfo);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900370 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
371 __kfree_skb(skb);
372 return;
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700375 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700376 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700377 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800378
Amerigo Wang8c4c49d2012-09-17 20:16:31 +0000379 txq = netdev_pick_tx(dev, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700380
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700381 /* try until next clock tick */
382 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
383 tries > 0; --tries) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700384 if (__netif_tx_trylock(txq)) {
Tom Herbert734664982011-11-28 16:32:44 +0000385 if (!netif_xmit_stopped(txq)) {
Amerigo Wang689971b2012-08-10 01:24:49 +0000386 if (vlan_tx_tag_present(skb) &&
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000387 !vlan_hw_offload_capable(netif_skb_features(skb),
388 skb->vlan_proto)) {
389 skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
Amerigo Wang689971b2012-08-10 01:24:49 +0000390 if (unlikely(!skb))
391 break;
392 skb->vlan_tci = 0;
393 }
394
Stephen Hemminger00829822008-11-20 20:14:53 -0800395 status = ops->ndo_start_xmit(skb, dev);
Eric Dumazet08baf562009-05-25 22:58:01 -0700396 if (status == NETDEV_TX_OK)
397 txq_trans_update(txq);
398 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700399 __netif_tx_unlock(txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700400
Andrew Mortone37b8d92006-12-09 14:01:49 -0800401 if (status == NETDEV_TX_OK)
402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Andrew Mortone37b8d92006-12-09 14:01:49 -0800404 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700405
406 /* tickle device maybe there is some cleanup */
Joe Perches2a49e002011-06-30 15:08:58 +0000407 netpoll_poll_dev(np->dev);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700408
409 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700410 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000411
412 WARN_ONCE(!irqs_disabled(),
Amerigo Wang28996562012-08-10 01:24:42 +0000413 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000414 dev->name, ops->ndo_start_xmit);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700418 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700419 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000420 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
Neil Hormanc2355e12010-10-13 16:01:49 +0000423EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
426{
Eric Dumazet954fba022012-06-12 19:30:21 +0000427 int total_len, ip_len, udp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct sk_buff *skb;
429 struct udphdr *udph;
430 struct iphdr *iph;
431 struct ethhdr *eth;
Eric Dumazetee130402012-08-24 01:47:26 +0000432 static atomic_t ip_ident;
Cong Wangb3d936f2013-01-07 20:52:41 +0000433 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 udp_len = len + sizeof(*udph);
Cong Wangb3d936f2013-01-07 20:52:41 +0000436 if (np->ipv6)
437 ip_len = udp_len + sizeof(*ip6h);
438 else
Cong Wangb7394d22013-01-07 20:52:39 +0000439 ip_len = udp_len + sizeof(*iph);
440
Eric Dumazet954fba022012-06-12 19:30:21 +0000441 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Eric Dumazet954fba022012-06-12 19:30:21 +0000443 skb = find_skb(np, total_len + np->dev->needed_tailroom,
444 total_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (!skb)
446 return;
447
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300448 skb_copy_to_linear_data(skb, msg, len);
Eric Dumazet954fba022012-06-12 19:30:21 +0000449 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300451 skb_push(skb, sizeof(*udph));
452 skb_reset_transport_header(skb);
453 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 udph->source = htons(np->local_port);
455 udph->dest = htons(np->remote_port);
456 udph->len = htons(udp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Cong Wangb3d936f2013-01-07 20:52:41 +0000458 if (np->ipv6) {
459 udph->check = 0;
460 udph->check = csum_ipv6_magic(&np->local_ip.in6,
461 &np->remote_ip.in6,
462 udp_len, IPPROTO_UDP,
463 csum_partial(udph, udp_len, 0));
464 if (udph->check == 0)
465 udph->check = CSUM_MANGLED_0;
466
467 skb_push(skb, sizeof(*ip6h));
468 skb_reset_network_header(skb);
469 ip6h = ipv6_hdr(skb);
470
471 /* ip6h->version = 6; ip6h->priority = 0; */
472 put_unaligned(0x60, (unsigned char *)ip6h);
473 ip6h->flow_lbl[0] = 0;
474 ip6h->flow_lbl[1] = 0;
475 ip6h->flow_lbl[2] = 0;
476
477 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
478 ip6h->nexthdr = IPPROTO_UDP;
479 ip6h->hop_limit = 32;
480 ip6h->saddr = np->local_ip.in6;
481 ip6h->daddr = np->remote_ip.in6;
482
483 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
484 skb_reset_mac_header(skb);
485 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
486 } else {
Cong Wangb7394d22013-01-07 20:52:39 +0000487 udph->check = 0;
488 udph->check = csum_tcpudp_magic(np->local_ip.ip,
489 np->remote_ip.ip,
490 udp_len, IPPROTO_UDP,
491 csum_partial(udph, udp_len, 0));
492 if (udph->check == 0)
493 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Cong Wangb7394d22013-01-07 20:52:39 +0000495 skb_push(skb, sizeof(*iph));
496 skb_reset_network_header(skb);
497 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Cong Wangb7394d22013-01-07 20:52:39 +0000499 /* iph->version = 4; iph->ihl = 5; */
500 put_unaligned(0x45, (unsigned char *)iph);
501 iph->tos = 0;
502 put_unaligned(htons(ip_len), &(iph->tot_len));
503 iph->id = htons(atomic_inc_return(&ip_ident));
504 iph->frag_off = 0;
505 iph->ttl = 64;
506 iph->protocol = IPPROTO_UDP;
507 iph->check = 0;
508 put_unaligned(np->local_ip.ip, &(iph->saddr));
509 put_unaligned(np->remote_ip.ip, &(iph->daddr));
510 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
511
512 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
513 skb_reset_mac_header(skb);
514 skb->protocol = eth->h_proto = htons(ETH_P_IP);
515 }
516
Stephen Hemminger09538642007-11-19 19:23:29 -0800517 memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
518 memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 skb->dev = np->dev;
521
522 netpoll_send_skb(np, skb);
523}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000524EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Cong Wangb7394d22013-01-07 20:52:39 +0000526static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Cong Wangb3d936f2013-01-07 20:52:41 +0000528 int size, type = ARPOP_REPLY;
Al Viro252e33462006-11-14 20:48:11 -0800529 __be32 sip, tip;
Neil Horman47bbec02006-12-08 00:05:55 -0800530 unsigned char *sha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct sk_buff *send_skb;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000532 struct netpoll *np, *tmp;
533 unsigned long flags;
Herbert Xuae641942011-11-18 02:20:04 +0000534 int hlen, tlen;
Cong Wangb7394d22013-01-07 20:52:39 +0000535 int hits = 0, proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000537 if (list_empty(&npinfo->rx_np))
538 return;
539
540 /* Before checking the packet, we do some early
541 inspection whether this is interesting at all */
542 spin_lock_irqsave(&npinfo->rx_lock, flags);
543 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
544 if (np->dev == skb->dev)
545 hits++;
546 }
547 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
548
549 /* No netpoll struct is using this dev */
550 if (!hits)
Jeff Moyer115c1d62005-06-22 22:05:31 -0700551 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Cong Wangb7394d22013-01-07 20:52:39 +0000553 proto = ntohs(eth_hdr(skb)->h_proto);
554 if (proto == ETH_P_IP) {
Cong Wangb3d936f2013-01-07 20:52:41 +0000555 struct arphdr *arp;
556 unsigned char *arp_ptr;
Cong Wangb7394d22013-01-07 20:52:39 +0000557 /* No arp on this interface */
558 if (skb->dev->flags & IFF_NOARP)
559 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Cong Wangb7394d22013-01-07 20:52:39 +0000561 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
562 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Cong Wangb7394d22013-01-07 20:52:39 +0000564 skb_reset_network_header(skb);
565 skb_reset_transport_header(skb);
566 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Cong Wangb7394d22013-01-07 20:52:39 +0000568 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
569 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
570 arp->ar_pro != htons(ETH_P_IP) ||
571 arp->ar_op != htons(ARPOP_REQUEST))
572 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Cong Wangb7394d22013-01-07 20:52:39 +0000574 arp_ptr = (unsigned char *)(arp+1);
575 /* save the location of the src hw addr */
576 sha = arp_ptr;
577 arp_ptr += skb->dev->addr_len;
578 memcpy(&sip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000579 arp_ptr += 4;
Cong Wangb7394d22013-01-07 20:52:39 +0000580 /* If we actually cared about dst hw addr,
581 it would get copied here */
582 arp_ptr += skb->dev->addr_len;
583 memcpy(&tip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000584
Cong Wangb7394d22013-01-07 20:52:39 +0000585 /* Should we ignore arp? */
586 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
587 return;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000588
Cong Wangb7394d22013-01-07 20:52:39 +0000589 size = arp_hdr_len(skb->dev);
590
591 spin_lock_irqsave(&npinfo->rx_lock, flags);
592 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
593 if (tip != np->local_ip.ip)
594 continue;
595
596 hlen = LL_RESERVED_SPACE(np->dev);
597 tlen = np->dev->needed_tailroom;
598 send_skb = find_skb(np, size + hlen + tlen, hlen);
599 if (!send_skb)
600 continue;
601
602 skb_reset_network_header(send_skb);
603 arp = (struct arphdr *) skb_put(send_skb, size);
604 send_skb->dev = skb->dev;
605 send_skb->protocol = htons(ETH_P_ARP);
606
607 /* Fill the device header for the ARP frame */
Cong Wangb3d936f2013-01-07 20:52:41 +0000608 if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
Cong Wangb7394d22013-01-07 20:52:39 +0000609 sha, np->dev->dev_addr,
610 send_skb->len) < 0) {
611 kfree_skb(send_skb);
612 continue;
613 }
614
615 /*
616 * Fill out the arp protocol part.
617 *
618 * we only support ethernet device type,
619 * which (according to RFC 1390) should
620 * always equal 1 (Ethernet).
621 */
622
623 arp->ar_hrd = htons(np->dev->type);
624 arp->ar_pro = htons(ETH_P_IP);
625 arp->ar_hln = np->dev->addr_len;
626 arp->ar_pln = 4;
627 arp->ar_op = htons(type);
628
629 arp_ptr = (unsigned char *)(arp + 1);
630 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
631 arp_ptr += np->dev->addr_len;
632 memcpy(arp_ptr, &tip, 4);
633 arp_ptr += 4;
634 memcpy(arp_ptr, sha, np->dev->addr_len);
635 arp_ptr += np->dev->addr_len;
636 memcpy(arp_ptr, &sip, 4);
637
638 netpoll_send_skb(np, send_skb);
639
640 /* If there are several rx_hooks for the same address,
641 we're fine by sending a single reply */
642 break;
643 }
644 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Cong Wangb3d936f2013-01-07 20:52:41 +0000645 } else if( proto == ETH_P_IPV6) {
646#if IS_ENABLED(CONFIG_IPV6)
647 struct nd_msg *msg;
648 u8 *lladdr = NULL;
649 struct ipv6hdr *hdr;
650 struct icmp6hdr *icmp6h;
651 const struct in6_addr *saddr;
652 const struct in6_addr *daddr;
653 struct inet6_dev *in6_dev = NULL;
654 struct in6_addr *target;
655
656 in6_dev = in6_dev_get(skb->dev);
657 if (!in6_dev || !in6_dev->cnf.accept_ra)
658 return;
659
660 if (!pskb_may_pull(skb, skb->len))
661 return;
662
663 msg = (struct nd_msg *)skb_transport_header(skb);
664
665 __skb_push(skb, skb->data - skb_transport_header(skb));
666
667 if (ipv6_hdr(skb)->hop_limit != 255)
668 return;
669 if (msg->icmph.icmp6_code != 0)
670 return;
671 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
672 return;
673
674 saddr = &ipv6_hdr(skb)->saddr;
675 daddr = &ipv6_hdr(skb)->daddr;
676
677 size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
678
679 spin_lock_irqsave(&npinfo->rx_lock, flags);
680 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
Cong Wangfaeed822013-01-27 15:55:20 +0000681 if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
Cong Wangb3d936f2013-01-07 20:52:41 +0000682 continue;
683
684 hlen = LL_RESERVED_SPACE(np->dev);
685 tlen = np->dev->needed_tailroom;
686 send_skb = find_skb(np, size + hlen + tlen, hlen);
687 if (!send_skb)
688 continue;
689
690 send_skb->protocol = htons(ETH_P_IPV6);
691 send_skb->dev = skb->dev;
692
693 skb_reset_network_header(send_skb);
694 skb_put(send_skb, sizeof(struct ipv6hdr));
695 hdr = ipv6_hdr(send_skb);
696
697 *(__be32*)hdr = htonl(0x60000000);
698
699 hdr->payload_len = htons(size);
700 hdr->nexthdr = IPPROTO_ICMPV6;
701 hdr->hop_limit = 255;
702 hdr->saddr = *saddr;
703 hdr->daddr = *daddr;
704
705 send_skb->transport_header = send_skb->tail;
706 skb_put(send_skb, size);
707
708 icmp6h = (struct icmp6hdr *)skb_transport_header(skb);
709 icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
710 icmp6h->icmp6_router = 0;
711 icmp6h->icmp6_solicited = 1;
Neil Horman959d5fd2013-02-13 11:32:42 -0500712 target = (struct in6_addr *)(skb_transport_header(send_skb) + sizeof(struct icmp6hdr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000713 *target = msg->target;
714 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
715 IPPROTO_ICMPV6,
716 csum_partial(icmp6h,
717 size, 0));
718
719 if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
720 lladdr, np->dev->dev_addr,
721 send_skb->len) < 0) {
722 kfree_skb(send_skb);
723 continue;
724 }
725
726 netpoll_send_skb(np, send_skb);
727
728 /* If there are several rx_hooks for the same address,
729 we're fine by sending a single reply */
730 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{
765 int proto, len, ulen;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000766 int hits = 0;
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;
Neil Horman068c6e92006-06-26 00:04:27 -0700770
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000771 if (list_empty(&npinfo->rx_np))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto out;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (skb->dev->type != ARPHRD_ETHER)
775 goto out;
776
David S. Millerd9452e92008-03-04 12:28:49 -0800777 /* check if netpoll clients need ARP */
Cong Wangb3d936f2013-01-07 20:52:41 +0000778 if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
779 skb_queue_tail(&npinfo->neigh_tx, skb);
780 return 1;
781 } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
Cong Wangb7394d22013-01-07 20:52:39 +0000782 skb_queue_tail(&npinfo->neigh_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 1;
784 }
785
Amerigo Wang689971b2012-08-10 01:24:49 +0000786 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
787 skb = vlan_untag(skb);
788 if (unlikely(!skb))
789 goto out;
790 }
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 proto = ntohs(eth_hdr(skb)->h_proto);
Cong Wangb7394d22013-01-07 20:52:39 +0000793 if (proto != ETH_P_IP && proto != ETH_P_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 goto out;
795 if (skb->pkt_type == PACKET_OTHERHOST)
796 goto out;
797 if (skb_shared(skb))
798 goto out;
799
Cong Wangb7394d22013-01-07 20:52:39 +0000800 if (proto == ETH_P_IP) {
801 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
802 goto out;
803 iph = (struct iphdr *)skb->data;
804 if (iph->ihl < 5 || iph->version != 4)
805 goto out;
806 if (!pskb_may_pull(skb, iph->ihl*4))
807 goto out;
808 iph = (struct iphdr *)skb->data;
809 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
810 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Cong Wangb7394d22013-01-07 20:52:39 +0000812 len = ntohs(iph->tot_len);
813 if (skb->len < len || len < iph->ihl*4)
814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Cong Wangb7394d22013-01-07 20:52:39 +0000816 /*
817 * Our transport medium may have padded the buffer out.
818 * Now We trim to the true length of the frame.
819 */
820 if (pskb_trim_rcsum(skb, len))
821 goto out;
Aubrey.Li5e7d7fa2007-04-17 12:40:20 -0700822
Cong Wangb7394d22013-01-07 20:52:39 +0000823 iph = (struct iphdr *)skb->data;
824 if (iph->protocol != IPPROTO_UDP)
825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Cong Wangb7394d22013-01-07 20:52:39 +0000827 len -= iph->ihl*4;
828 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
829 ulen = ntohs(uh->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Cong Wangb7394d22013-01-07 20:52:39 +0000831 if (ulen != len)
832 goto out;
833 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
834 goto out;
835 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
836 if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
837 continue;
838 if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
839 continue;
840 if (np->local_port && np->local_port != ntohs(uh->dest))
841 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Cong Wangb7394d22013-01-07 20:52:39 +0000843 np->rx_hook(np, ntohs(uh->source),
844 (char *)(uh+1),
845 ulen - sizeof(struct udphdr));
846 hits++;
847 }
Cong Wangb3d936f2013-01-07 20:52:41 +0000848 } else {
849#if IS_ENABLED(CONFIG_IPV6)
850 const struct ipv6hdr *ip6h;
851
852 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
853 goto out;
854 ip6h = (struct ipv6hdr *)skb->data;
855 if (ip6h->version != 6)
856 goto out;
857 len = ntohs(ip6h->payload_len);
858 if (!len)
859 goto out;
860 if (len + sizeof(struct ipv6hdr) > skb->len)
861 goto out;
862 if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
863 goto out;
864 ip6h = ipv6_hdr(skb);
865 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
866 goto out;
867 uh = udp_hdr(skb);
868 ulen = ntohs(uh->len);
869 if (ulen != skb->len)
870 goto out;
871 if (udp6_csum_init(skb, uh, IPPROTO_UDP))
872 goto out;
873 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
Cong Wangfaeed822013-01-27 15:55:20 +0000874 if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
Cong Wangb3d936f2013-01-07 20:52:41 +0000875 continue;
Cong Wangfaeed822013-01-27 15:55:20 +0000876 if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
Cong Wangb3d936f2013-01-07 20:52:41 +0000877 continue;
878 if (np->local_port && np->local_port != ntohs(uh->dest))
879 continue;
880
881 np->rx_hook(np, ntohs(uh->source),
882 (char *)(uh+1),
883 ulen - sizeof(struct udphdr));
884 hits++;
885 }
886#endif
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000887 }
888
889 if (!hits)
890 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 kfree_skb(skb);
893 return 1;
894
895out:
896 if (atomic_read(&trapped)) {
897 kfree_skb(skb);
898 return 1;
899 }
900
901 return 0;
902}
903
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700904void netpoll_print_options(struct netpoll *np)
905{
Joe Perchese6ec269352012-01-29 15:50:43 +0000906 np_info(np, "local port %d\n", np->local_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000907 if (np->ipv6)
908 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
909 else
Cong Wangb7394d22013-01-07 20:52:39 +0000910 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000911 np_info(np, "interface '%s'\n", np->dev_name);
912 np_info(np, "remote port %d\n", np->remote_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000913 if (np->ipv6)
914 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
915 else
Cong Wangb7394d22013-01-07 20:52:39 +0000916 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000917 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700918}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000919EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700920
Cong Wangb7394d22013-01-07 20:52:39 +0000921static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
922{
923 const char *end;
924
925 if (!strchr(str, ':') &&
926 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
927 if (!*end)
928 return 0;
929 }
930 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
931#if IS_ENABLED(CONFIG_IPV6)
932 if (!*end)
933 return 1;
934#else
935 return -1;
936#endif
937 }
938 return -1;
939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941int netpoll_parse_options(struct netpoll *np, char *opt)
942{
943 char *cur=opt, *delim;
Cong Wangb7394d22013-01-07 20:52:39 +0000944 int ipv6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
David S. Millerc68b9072006-11-14 20:40:49 -0800946 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if ((delim = strchr(cur, '@')) == NULL)
948 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800949 *delim = 0;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000950 if (kstrtou16(cur, 10, &np->local_port))
951 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800952 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
David S. Millerc68b9072006-11-14 20:40:49 -0800956 if (*cur != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if ((delim = strchr(cur, '/')) == NULL)
958 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800959 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000960 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
961 if (ipv6 < 0)
962 goto parse_failed;
963 else
964 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800965 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967 cur++;
968
David S. Millerc68b9072006-11-14 20:40:49 -0800969 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* parse out dev name */
971 if ((delim = strchr(cur, ',')) == NULL)
972 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800973 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800975 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977 cur++;
978
David S. Millerc68b9072006-11-14 20:40:49 -0800979 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* dst port */
981 if ((delim = strchr(cur, '@')) == NULL)
982 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800983 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000984 if (*cur == ' ' || *cur == '\t')
Joe Perchese6ec269352012-01-29 15:50:43 +0000985 np_info(np, "warning: whitespace is not allowed\n");
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000986 if (kstrtou16(cur, 10, &np->remote_port))
987 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800988 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 /* dst ip */
993 if ((delim = strchr(cur, '/')) == NULL)
994 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800995 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000996 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
997 if (ipv6 < 0)
998 goto parse_failed;
999 else if (np->ipv6 != (bool)ipv6)
1000 goto parse_failed;
1001 else
1002 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -08001003 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
David S. Millerc68b9072006-11-14 20:40:49 -08001005 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* MAC address */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001007 if (!mac_pton(cur, np->remote_mac))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto parse_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
Satyam Sharma0bcc1812007-08-10 15:35:05 -07001011 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 return 0;
1014
1015 parse_failed:
Joe Perchese6ec269352012-01-29 15:50:43 +00001016 np_info(np, "couldn't parse config at '%s'!\n", cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -1;
1018}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001019EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Amerigo Wang47be03a22012-08-10 01:24:37 +00001021int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001022{
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001023 struct netpoll_info *npinfo;
1024 const struct net_device_ops *ops;
1025 unsigned long flags;
1026 int err;
1027
Jiri Pirko30fdd8a02012-07-17 05:22:35 +00001028 np->dev = ndev;
1029 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
Neil Horman2cde6ac2013-02-11 10:25:30 +00001030 INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
Jiri Pirko30fdd8a02012-07-17 05:22:35 +00001031
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001032 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
1033 !ndev->netdev_ops->ndo_poll_controller) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001034 np_err(np, "%s doesn't support polling, aborting\n",
1035 np->dev_name);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001036 err = -ENOTSUPP;
1037 goto out;
1038 }
1039
1040 if (!ndev->npinfo) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001041 npinfo = kmalloc(sizeof(*npinfo), gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001042 if (!npinfo) {
1043 err = -ENOMEM;
1044 goto out;
1045 }
1046
1047 npinfo->rx_flags = 0;
1048 INIT_LIST_HEAD(&npinfo->rx_np);
1049
1050 spin_lock_init(&npinfo->rx_lock);
Neil Hormanbd7c4b62013-04-30 05:35:05 +00001051 sema_init(&npinfo->dev_lock, 1);
Cong Wangb7394d22013-01-07 20:52:39 +00001052 skb_queue_head_init(&npinfo->neigh_tx);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001053 skb_queue_head_init(&npinfo->txq);
1054 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
1055
1056 atomic_set(&npinfo->refcnt, 1);
1057
1058 ops = np->dev->netdev_ops;
1059 if (ops->ndo_netpoll_setup) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001060 err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001061 if (err)
1062 goto free_npinfo;
1063 }
1064 } else {
Neil Horman0790bbb2013-02-11 10:25:31 +00001065 npinfo = rtnl_dereference(ndev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001066 atomic_inc(&npinfo->refcnt);
1067 }
1068
1069 npinfo->netpoll = np;
1070
1071 if (np->rx_hook) {
1072 spin_lock_irqsave(&npinfo->rx_lock, flags);
1073 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
1074 list_add_tail(&np->rx, &npinfo->rx_np);
1075 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1076 }
1077
1078 /* last thing to do is link it to the net device structure */
Eric Dumazetcf778b02012-01-12 04:41:32 +00001079 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001080
1081 return 0;
1082
1083free_npinfo:
1084 kfree(npinfo);
1085out:
1086 return err;
1087}
1088EXPORT_SYMBOL_GPL(__netpoll_setup);
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090int netpoll_setup(struct netpoll *np)
1091{
1092 struct net_device *ndev = NULL;
1093 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001094 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Cong Wangf92d3182013-01-14 23:34:06 +00001096 rtnl_lock();
Cong Wang556e6252013-01-27 15:55:21 +00001097 if (np->dev_name) {
1098 struct net *net = current->nsproxy->net_ns;
1099 ndev = __dev_get_by_name(net, np->dev_name);
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (!ndev) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001102 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
Cong Wangf92d3182013-01-14 23:34:06 +00001103 err = -ENODEV;
1104 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
Cong Wang5bd30d32013-01-17 12:21:08 +08001106 dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jiri Pirko49bd8fb2013-01-03 22:48:55 +00001108 if (netdev_master_upper_dev_get(ndev)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001109 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
Dan Carpenter83fe32d2011-06-11 18:55:22 -07001110 err = -EBUSY;
1111 goto put;
WANG Cong0c1ad042011-06-09 00:28:13 -07001112 }
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!netif_running(ndev)) {
1115 unsigned long atmost, atleast;
1116
Joe Perchese6ec269352012-01-29 15:50:43 +00001117 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001119 err = dev_open(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001120
1121 if (err) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001122 np_err(np, "failed to open %s\n", ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +00001123 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Cong Wangf92d3182013-01-14 23:34:06 +00001126 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -07001128 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 while (!netif_carrier_ok(ndev)) {
1130 if (time_after(jiffies, atmost)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001131 np_notice(np, "timeout waiting for carrier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
1133 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -07001134 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
1137 /* If carrier appears to come up instantly, we don't
1138 * trust it and pause so that we don't pump all our
1139 * queued console messages into the bitbucket.
1140 */
1141
1142 if (time_before(jiffies, atleast)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001143 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 msleep(4000);
1145 }
Cong Wangf92d3182013-01-14 23:34:06 +00001146 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
Cong Wangb7394d22013-01-07 20:52:39 +00001149 if (!np->local_ip.ip) {
1150 if (!np->ipv6) {
Cong Wangf92d3182013-01-14 23:34:06 +00001151 in_dev = __in_dev_get_rtnl(ndev);
Cong Wangb7394d22013-01-07 20:52:39 +00001152
1153 if (!in_dev || !in_dev->ifa_list) {
Cong Wangb7394d22013-01-07 20:52:39 +00001154 np_err(np, "no IP address for %s, aborting\n",
1155 np->dev_name);
1156 err = -EDESTADDRREQ;
1157 goto put;
1158 }
1159
1160 np->local_ip.ip = in_dev->ifa_list->ifa_local;
Cong Wangb7394d22013-01-07 20:52:39 +00001161 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
Cong Wangb3d936f2013-01-07 20:52:41 +00001162 } else {
1163#if IS_ENABLED(CONFIG_IPV6)
1164 struct inet6_dev *idev;
1165
1166 err = -EDESTADDRREQ;
Cong Wangb3d936f2013-01-07 20:52:41 +00001167 idev = __in6_dev_get(ndev);
1168 if (idev) {
1169 struct inet6_ifaddr *ifp;
1170
1171 read_lock_bh(&idev->lock);
1172 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1173 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
1174 continue;
1175 np->local_ip.in6 = ifp->addr;
1176 err = 0;
1177 break;
1178 }
1179 read_unlock_bh(&idev->lock);
1180 }
Cong Wangb3d936f2013-01-07 20:52:41 +00001181 if (err) {
1182 np_err(np, "no IPv6 address for %s, aborting\n",
1183 np->dev_name);
1184 goto put;
1185 } else
1186 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
1187#else
1188 np_err(np, "IPv6 is not supported %s, aborting\n",
1189 np->dev_name);
Cong Wange39363a2013-01-22 17:39:11 +00001190 err = -EINVAL;
Cong Wangb3d936f2013-01-07 20:52:41 +00001191 goto put;
1192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
Herbert Xudbaa1542010-06-10 16:12:46 +00001196 /* fill up the skb queue */
1197 refill_skbs();
1198
Amerigo Wang47be03a22012-08-10 01:24:37 +00001199 err = __netpoll_setup(np, ndev, GFP_KERNEL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001200 if (err)
1201 goto put;
1202
Cong Wangf92d3182013-01-14 23:34:06 +00001203 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return 0;
1205
Jiri Slaby21edbb22010-03-16 05:29:54 +00001206put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 dev_put(ndev);
Cong Wangf92d3182013-01-14 23:34:06 +00001208unlock:
1209 rtnl_unlock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001210 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001212EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
David S. Millerc68b9072006-11-14 20:40:49 -08001214static int __init netpoll_init(void)
1215{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -08001216 skb_queue_head_init(&skb_pool);
1217 return 0;
1218}
1219core_initcall(netpoll_init);
1220
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001221static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
1222{
1223 struct netpoll_info *npinfo =
1224 container_of(rcu_head, struct netpoll_info, rcu);
1225
Cong Wangb7394d22013-01-07 20:52:39 +00001226 skb_queue_purge(&npinfo->neigh_tx);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001227 skb_queue_purge(&npinfo->txq);
1228
1229 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
1230 cancel_delayed_work(&npinfo->tx_work);
1231
1232 /* clean after last, unfinished work */
1233 __skb_queue_purge(&npinfo->txq);
1234 /* now cancel it again */
1235 cancel_delayed_work(&npinfo->tx_work);
1236 kfree(npinfo);
1237}
1238
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001239void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -07001241 struct netpoll_info *npinfo;
1242 unsigned long flags;
1243
Neil Horman0790bbb2013-02-11 10:25:31 +00001244 /* rtnl_dereference would be preferable here but
1245 * rcu_cleanup_netpoll path can put us in here safely without
1246 * holding the rtnl, so plain rcu_dereference it is
1247 */
1248 npinfo = rtnl_dereference(np->dev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001249 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +00001250 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -07001251
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001252 if (!list_empty(&npinfo->rx_np)) {
1253 spin_lock_irqsave(&npinfo->rx_lock, flags);
1254 list_del(&np->rx);
1255 if (list_empty(&npinfo->rx_np))
1256 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
1257 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Jeff Moyer115c1d62005-06-22 22:05:31 -07001258 }
Herbert Xudbaa1542010-06-10 16:12:46 +00001259
Neil Hormanca99ca12013-02-05 08:05:43 +00001260 synchronize_srcu(&netpoll_srcu);
1261
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001262 if (atomic_dec_and_test(&npinfo->refcnt)) {
1263 const struct net_device_ops *ops;
1264
1265 ops = np->dev->netdev_ops;
1266 if (ops->ndo_netpoll_cleanup)
1267 ops->ndo_netpoll_cleanup(np->dev);
1268
Neil Horman2cde6ac2013-02-11 10:25:30 +00001269 rcu_assign_pointer(np->dev->npinfo, NULL);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001270 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
Herbert Xudbaa1542010-06-10 16:12:46 +00001271 }
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001272}
1273EXPORT_SYMBOL_GPL(__netpoll_cleanup);
1274
Neil Horman2cde6ac2013-02-11 10:25:30 +00001275static void netpoll_async_cleanup(struct work_struct *work)
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001276{
Neil Horman2cde6ac2013-02-11 10:25:30 +00001277 struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001278
Neil Horman2cde6ac2013-02-11 10:25:30 +00001279 rtnl_lock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001280 __netpoll_cleanup(np);
Neil Horman2cde6ac2013-02-11 10:25:30 +00001281 rtnl_unlock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001282 kfree(np);
1283}
1284
Neil Horman2cde6ac2013-02-11 10:25:30 +00001285void __netpoll_free_async(struct netpoll *np)
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001286{
Neil Horman2cde6ac2013-02-11 10:25:30 +00001287 schedule_work(&np->cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001288}
Neil Horman2cde6ac2013-02-11 10:25:30 +00001289EXPORT_SYMBOL_GPL(__netpoll_free_async);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001290
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001291void netpoll_cleanup(struct netpoll *np)
1292{
1293 if (!np->dev)
1294 return;
1295
1296 rtnl_lock();
1297 __netpoll_cleanup(np);
1298 rtnl_unlock();
Herbert Xudbaa1542010-06-10 16:12:46 +00001299
1300 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 np->dev = NULL;
1302}
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);