wpan: whitespace fix
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / core / netpoll.c
CommitLineData
1da177e4
LT
1/*
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
e6ec2693
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
bff38771 14#include <linux/moduleparam.h>
1da177e4
LT
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <linux/string.h>
14c85021 18#include <linux/if_arp.h>
1da177e4
LT
19#include <linux/inetdevice.h>
20#include <linux/inet.h>
21#include <linux/interrupt.h>
22#include <linux/netpoll.h>
23#include <linux/sched.h>
24#include <linux/delay.h>
25#include <linux/rcupdate.h>
26#include <linux/workqueue.h>
5a0e3ad6 27#include <linux/slab.h>
bc3b2d7f 28#include <linux/export.h>
689971b4 29#include <linux/if_vlan.h>
1da177e4
LT
30#include <net/tcp.h>
31#include <net/udp.h>
b3d936f3
CW
32#include <net/addrconf.h>
33#include <net/ndisc.h>
34#include <net/ip6_checksum.h>
1da177e4 35#include <asm/unaligned.h>
9cbc1cb8 36#include <trace/events/napi.h>
1da177e4
LT
37
38/*
39 * We maintain a small pool of fully-sized skbs, to make sure the
40 * message gets out even in extreme OOM situations.
41 */
42
43#define MAX_UDP_CHUNK 1460
44#define MAX_SKBS 32
1da177e4 45
a1bcfacd 46static struct sk_buff_head skb_pool;
1da177e4
LT
47
48static atomic_t trapped;
49
2bdfe0ba 50#define USEC_PER_POLL 50
d9452e9f
DM
51#define NETPOLL_RX_ENABLED 1
52#define NETPOLL_RX_DROP 2
1da177e4 53
6f706245
JP
54#define MAX_SKB_SIZE \
55 (sizeof(struct ethhdr) + \
56 sizeof(struct iphdr) + \
57 sizeof(struct udphdr) + \
58 MAX_UDP_CHUNK)
1da177e4 59
3578b0c8 60static void zap_completion_queue(void);
b7394d24 61static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
1da177e4 62
bff38771
AV
63static unsigned int carrier_timeout = 4;
64module_param(carrier_timeout, uint, 0644);
65
e6ec2693
JP
66#define np_info(np, fmt, ...) \
67 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
68#define np_err(np, fmt, ...) \
69 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
70#define np_notice(np, fmt, ...) \
71 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
72
c4028958 73static void queue_process(struct work_struct *work)
1da177e4 74{
4c1ac1b4
DH
75 struct netpoll_info *npinfo =
76 container_of(work, struct netpoll_info, tx_work.work);
1da177e4 77 struct sk_buff *skb;
3640543d 78 unsigned long flags;
1da177e4 79
6c43ff18
SH
80 while ((skb = skb_dequeue(&npinfo->txq))) {
81 struct net_device *dev = skb->dev;
00829823 82 const struct net_device_ops *ops = dev->netdev_ops;
fd2ea0a7 83 struct netdev_queue *txq;
1da177e4 84
6c43ff18
SH
85 if (!netif_device_present(dev) || !netif_running(dev)) {
86 __kfree_skb(skb);
87 continue;
88 }
1da177e4 89
fd2ea0a7
DM
90 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
91
3640543d 92 local_irq_save(flags);
fd2ea0a7 93 __netif_tx_lock(txq, smp_processor_id());
73466498 94 if (netif_xmit_frozen_or_stopped(txq) ||
00829823 95 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
6c43ff18 96 skb_queue_head(&npinfo->txq, skb);
fd2ea0a7 97 __netif_tx_unlock(txq);
3640543d 98 local_irq_restore(flags);
1da177e4 99
25442caf 100 schedule_delayed_work(&npinfo->tx_work, HZ/10);
6c43ff18
SH
101 return;
102 }
fd2ea0a7 103 __netif_tx_unlock(txq);
3640543d 104 local_irq_restore(flags);
1da177e4 105 }
1da177e4
LT
106}
107
b51655b9
AV
108static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
109 unsigned short ulen, __be32 saddr, __be32 daddr)
1da177e4 110{
d6f5493c 111 __wsum psum;
fb286bb2 112
60476372 113 if (uh->check == 0 || skb_csum_unnecessary(skb))
1da177e4
LT
114 return 0;
115
fb286bb2
HX
116 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
117
84fa7933 118 if (skb->ip_summed == CHECKSUM_COMPLETE &&
d3bc23e7 119 !csum_fold(csum_add(psum, skb->csum)))
fb286bb2 120 return 0;
1da177e4 121
fb286bb2 122 skb->csum = psum;
1da177e4 123
fb286bb2 124 return __skb_checksum_complete(skb);
1da177e4
LT
125}
126
127/*
128 * Check whether delayed processing was scheduled for our NIC. If so,
129 * we attempt to grab the poll lock and use ->poll() to pump the card.
130 * If this fails, either we've recursed in ->poll() or it's already
131 * running on another CPU.
132 *
133 * Note: we don't mask interrupts with this lock because we're using
134 * trylock here and interrupts are already disabled in the softirq
135 * case. Further, we test the poll_owner to avoid recursion on UP
136 * systems where the lock doesn't exist.
137 *
138 * In cases where there is bi-directional communications, reading only
139 * one message at a time can lead to packets being dropped by the
140 * network adapter, forcing superfluous retries and possibly timeouts.
141 * Thus, we set our budget to greater than 1.
142 */
0a7606c1
DM
143static int poll_one_napi(struct netpoll_info *npinfo,
144 struct napi_struct *napi, int budget)
145{
146 int work;
147
148 /* net_rx_action's ->poll() invocations and our's are
149 * synchronized by this test which is only made while
150 * holding the napi->poll_lock.
151 */
152 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
153 return budget;
154
d9452e9f 155 npinfo->rx_flags |= NETPOLL_RX_DROP;
0a7606c1 156 atomic_inc(&trapped);
7b363e44 157 set_bit(NAPI_STATE_NPSVC, &napi->state);
0a7606c1
DM
158
159 work = napi->poll(napi, budget);
7d18f114 160 trace_napi_poll(napi);
0a7606c1 161
7b363e44 162 clear_bit(NAPI_STATE_NPSVC, &napi->state);
0a7606c1 163 atomic_dec(&trapped);
d9452e9f 164 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
0a7606c1
DM
165
166 return budget - work;
167}
168
5106930b 169static void poll_napi(struct net_device *dev)
1da177e4 170{
bea3348e 171 struct napi_struct *napi;
1da177e4
LT
172 int budget = 16;
173
f13d493d 174 list_for_each_entry(napi, &dev->napi_list, dev_list) {
0a7606c1 175 if (napi->poll_owner != smp_processor_id() &&
bea3348e 176 spin_trylock(&napi->poll_lock)) {
2899656b
AW
177 budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
178 napi, budget);
bea3348e 179 spin_unlock(&napi->poll_lock);
0a7606c1 180
072a9c48 181 if (!budget)
0a7606c1 182 break;
bea3348e 183 }
1da177e4
LT
184 }
185}
186
b7394d24 187static void service_neigh_queue(struct netpoll_info *npi)
068c6e98 188{
5106930b
SH
189 if (npi) {
190 struct sk_buff *skb;
068c6e98 191
b7394d24
CW
192 while ((skb = skb_dequeue(&npi->neigh_tx)))
193 netpoll_neigh_reply(skb, npi);
068c6e98 194 }
068c6e98
NH
195}
196
234b921d 197static void netpoll_poll_dev(struct net_device *dev)
1da177e4 198{
5e392739 199 const struct net_device_ops *ops;
2899656b 200 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
5106930b 201
5e392739
PE
202 if (!dev || !netif_running(dev))
203 return;
204
205 ops = dev->netdev_ops;
206 if (!ops->ndo_poll_controller)
1da177e4
LT
207 return;
208
209 /* Process pending work on NIC */
d314774c 210 ops->ndo_poll_controller(dev);
5106930b
SH
211
212 poll_napi(dev);
1da177e4 213
58e05f35 214 if (dev->flags & IFF_SLAVE) {
2899656b 215 if (ni) {
49bd8fb0 216 struct net_device *bond_dev;
5a698af5 217 struct sk_buff *skb;
49bd8fb0
JP
218 struct netpoll_info *bond_ni;
219
220 bond_dev = netdev_master_upper_dev_get_rcu(dev);
221 bond_ni = rcu_dereference_bh(bond_dev->npinfo);
b7394d24 222 while ((skb = skb_dequeue(&ni->neigh_tx))) {
5a698af5 223 skb->dev = bond_dev;
b7394d24 224 skb_queue_tail(&bond_ni->neigh_tx, skb);
5a698af5
AW
225 }
226 }
227 }
228
b7394d24 229 service_neigh_queue(ni);
068c6e98 230
3578b0c8 231 zap_completion_queue();
1da177e4
LT
232}
233
234static void refill_skbs(void)
235{
236 struct sk_buff *skb;
237 unsigned long flags;
238
a1bcfacd
SH
239 spin_lock_irqsave(&skb_pool.lock, flags);
240 while (skb_pool.qlen < MAX_SKBS) {
1da177e4
LT
241 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
242 if (!skb)
243 break;
244
a1bcfacd 245 __skb_queue_tail(&skb_pool, skb);
1da177e4 246 }
a1bcfacd 247 spin_unlock_irqrestore(&skb_pool.lock, flags);
1da177e4
LT
248}
249
3578b0c8
DM
250static void zap_completion_queue(void)
251{
252 unsigned long flags;
253 struct softnet_data *sd = &get_cpu_var(softnet_data);
254
255 if (sd->completion_queue) {
256 struct sk_buff *clist;
257
258 local_irq_save(flags);
259 clist = sd->completion_queue;
260 sd->completion_queue = NULL;
261 local_irq_restore(flags);
262
263 while (clist != NULL) {
264 struct sk_buff *skb = clist;
265 clist = clist->next;
266 if (skb->destructor) {
267 atomic_inc(&skb->users);
268 dev_kfree_skb_any(skb); /* put this one back */
269 } else {
270 __kfree_skb(skb);
271 }
272 }
273 }
274
275 put_cpu_var(softnet_data);
276}
277
a1bcfacd 278static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
1da177e4 279{
a1bcfacd
SH
280 int count = 0;
281 struct sk_buff *skb;
1da177e4 282
3578b0c8 283 zap_completion_queue();
a1bcfacd 284 refill_skbs();
1da177e4 285repeat:
1da177e4
LT
286
287 skb = alloc_skb(len, GFP_ATOMIC);
a1bcfacd
SH
288 if (!skb)
289 skb = skb_dequeue(&skb_pool);
1da177e4
LT
290
291 if (!skb) {
a1bcfacd 292 if (++count < 10) {
2a49e001 293 netpoll_poll_dev(np->dev);
a1bcfacd 294 goto repeat;
1da177e4 295 }
a1bcfacd 296 return NULL;
1da177e4
LT
297 }
298
299 atomic_set(&skb->users, 1);
300 skb_reserve(skb, reserve);
301 return skb;
302}
303
bea3348e
SH
304static int netpoll_owner_active(struct net_device *dev)
305{
306 struct napi_struct *napi;
307
308 list_for_each_entry(napi, &dev->napi_list, dev_list) {
309 if (napi->poll_owner == smp_processor_id())
310 return 1;
311 }
312 return 0;
313}
314
2899656b 315/* call with IRQ disabled */
c2355e1a
NH
316void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
317 struct net_device *dev)
1da177e4 318{
2bdfe0ba
SH
319 int status = NETDEV_TX_BUSY;
320 unsigned long tries;
00829823 321 const struct net_device_ops *ops = dev->netdev_ops;
de85d99e 322 /* It is up to the caller to keep npinfo alive. */
2899656b 323 struct netpoll_info *npinfo;
2bdfe0ba 324
2899656b
AW
325 WARN_ON_ONCE(!irqs_disabled());
326
327 npinfo = rcu_dereference_bh(np->dev->npinfo);
4ec93edb
YH
328 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
329 __kfree_skb(skb);
330 return;
331 }
2bdfe0ba
SH
332
333 /* don't get messages out of order, and no recursion */
bea3348e 334 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
fd2ea0a7 335 struct netdev_queue *txq;
a49f99ff 336
8c4c49df 337 txq = netdev_pick_tx(dev, skb);
fd2ea0a7 338
0db3dc73
SH
339 /* try until next clock tick */
340 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
341 tries > 0; --tries) {
fd2ea0a7 342 if (__netif_tx_trylock(txq)) {
73466498 343 if (!netif_xmit_stopped(txq)) {
689971b4
AW
344 if (vlan_tx_tag_present(skb) &&
345 !(netif_skb_features(skb) & NETIF_F_HW_VLAN_TX)) {
346 skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
347 if (unlikely(!skb))
348 break;
349 skb->vlan_tci = 0;
350 }
351
00829823 352 status = ops->ndo_start_xmit(skb, dev);
08baf561
ED
353 if (status == NETDEV_TX_OK)
354 txq_trans_update(txq);
355 }
fd2ea0a7 356 __netif_tx_unlock(txq);
e37b8d93
AM
357
358 if (status == NETDEV_TX_OK)
359 break;
360
e37b8d93 361 }
0db3dc73
SH
362
363 /* tickle device maybe there is some cleanup */
2a49e001 364 netpoll_poll_dev(np->dev);
0db3dc73
SH
365
366 udelay(USEC_PER_POLL);
0db1d6fc 367 }
79b1bee8
DD
368
369 WARN_ONCE(!irqs_disabled(),
2899656b 370 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
79b1bee8
DD
371 dev->name, ops->ndo_start_xmit);
372
1da177e4 373 }
1da177e4 374
2bdfe0ba 375 if (status != NETDEV_TX_OK) {
5de4a473 376 skb_queue_tail(&npinfo->txq, skb);
4c1ac1b4 377 schedule_delayed_work(&npinfo->tx_work,0);
1da177e4 378 }
1da177e4 379}
c2355e1a 380EXPORT_SYMBOL(netpoll_send_skb_on_dev);
1da177e4
LT
381
382void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
383{
954fba02 384 int total_len, ip_len, udp_len;
1da177e4
LT
385 struct sk_buff *skb;
386 struct udphdr *udph;
387 struct iphdr *iph;
388 struct ethhdr *eth;
ee130409 389 static atomic_t ip_ident;
b3d936f3 390 struct ipv6hdr *ip6h;
1da177e4
LT
391
392 udp_len = len + sizeof(*udph);
b3d936f3
CW
393 if (np->ipv6)
394 ip_len = udp_len + sizeof(*ip6h);
395 else
b7394d24
CW
396 ip_len = udp_len + sizeof(*iph);
397
954fba02 398 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
1da177e4 399
954fba02
ED
400 skb = find_skb(np, total_len + np->dev->needed_tailroom,
401 total_len - len);
1da177e4
LT
402 if (!skb)
403 return;
404
27d7ff46 405 skb_copy_to_linear_data(skb, msg, len);
954fba02 406 skb_put(skb, len);
1da177e4 407
4bedb452
ACM
408 skb_push(skb, sizeof(*udph));
409 skb_reset_transport_header(skb);
410 udph = udp_hdr(skb);
1da177e4
LT
411 udph->source = htons(np->local_port);
412 udph->dest = htons(np->remote_port);
413 udph->len = htons(udp_len);
b7394d24 414
b3d936f3
CW
415 if (np->ipv6) {
416 udph->check = 0;
417 udph->check = csum_ipv6_magic(&np->local_ip.in6,
418 &np->remote_ip.in6,
419 udp_len, IPPROTO_UDP,
420 csum_partial(udph, udp_len, 0));
421 if (udph->check == 0)
422 udph->check = CSUM_MANGLED_0;
423
424 skb_push(skb, sizeof(*ip6h));
425 skb_reset_network_header(skb);
426 ip6h = ipv6_hdr(skb);
427
428 /* ip6h->version = 6; ip6h->priority = 0; */
429 put_unaligned(0x60, (unsigned char *)ip6h);
430 ip6h->flow_lbl[0] = 0;
431 ip6h->flow_lbl[1] = 0;
432 ip6h->flow_lbl[2] = 0;
433
434 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
435 ip6h->nexthdr = IPPROTO_UDP;
436 ip6h->hop_limit = 32;
437 ip6h->saddr = np->local_ip.in6;
438 ip6h->daddr = np->remote_ip.in6;
439
440 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
441 skb_reset_mac_header(skb);
442 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
443 } else {
b7394d24
CW
444 udph->check = 0;
445 udph->check = csum_tcpudp_magic(np->local_ip.ip,
446 np->remote_ip.ip,
447 udp_len, IPPROTO_UDP,
448 csum_partial(udph, udp_len, 0));
449 if (udph->check == 0)
450 udph->check = CSUM_MANGLED_0;
451
452 skb_push(skb, sizeof(*iph));
453 skb_reset_network_header(skb);
454 iph = ip_hdr(skb);
455
456 /* iph->version = 4; iph->ihl = 5; */
457 put_unaligned(0x45, (unsigned char *)iph);
458 iph->tos = 0;
459 put_unaligned(htons(ip_len), &(iph->tot_len));
460 iph->id = htons(atomic_inc_return(&ip_ident));
461 iph->frag_off = 0;
462 iph->ttl = 64;
463 iph->protocol = IPPROTO_UDP;
464 iph->check = 0;
465 put_unaligned(np->local_ip.ip, &(iph->saddr));
466 put_unaligned(np->remote_ip.ip, &(iph->daddr));
467 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
468
469 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
470 skb_reset_mac_header(skb);
471 skb->protocol = eth->h_proto = htons(ETH_P_IP);
472 }
473
09538641
SH
474 memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
475 memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
1da177e4
LT
476
477 skb->dev = np->dev;
478
479 netpoll_send_skb(np, skb);
480}
9e34a5b5 481EXPORT_SYMBOL(netpoll_send_udp);
1da177e4 482
b7394d24 483static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
1da177e4 484{
b3d936f3 485 int size, type = ARPOP_REPLY;
252e3346 486 __be32 sip, tip;
47bbec02 487 unsigned char *sha;
1da177e4 488 struct sk_buff *send_skb;
508e14b4
DB
489 struct netpoll *np, *tmp;
490 unsigned long flags;
ae641949 491 int hlen, tlen;
b7394d24 492 int hits = 0, proto;
508e14b4
DB
493
494 if (list_empty(&npinfo->rx_np))
495 return;
496
497 /* Before checking the packet, we do some early
498 inspection whether this is interesting at all */
499 spin_lock_irqsave(&npinfo->rx_lock, flags);
500 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
501 if (np->dev == skb->dev)
502 hits++;
503 }
504 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1da177e4 505
508e14b4
DB
506 /* No netpoll struct is using this dev */
507 if (!hits)
115c1d6e 508 return;
1da177e4 509
b7394d24
CW
510 proto = ntohs(eth_hdr(skb)->h_proto);
511 if (proto == ETH_P_IP) {
b3d936f3
CW
512 struct arphdr *arp;
513 unsigned char *arp_ptr;
b7394d24
CW
514 /* No arp on this interface */
515 if (skb->dev->flags & IFF_NOARP)
516 return;
1da177e4 517
b7394d24
CW
518 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
519 return;
1da177e4 520
b7394d24
CW
521 skb_reset_network_header(skb);
522 skb_reset_transport_header(skb);
523 arp = arp_hdr(skb);
1da177e4 524
b7394d24
CW
525 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
526 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
527 arp->ar_pro != htons(ETH_P_IP) ||
528 arp->ar_op != htons(ARPOP_REQUEST))
529 return;
1da177e4 530
b7394d24
CW
531 arp_ptr = (unsigned char *)(arp+1);
532 /* save the location of the src hw addr */
533 sha = arp_ptr;
534 arp_ptr += skb->dev->addr_len;
535 memcpy(&sip, arp_ptr, 4);
536 arp_ptr += 4;
537 /* If we actually cared about dst hw addr,
538 it would get copied here */
539 arp_ptr += skb->dev->addr_len;
540 memcpy(&tip, arp_ptr, 4);
1da177e4 541
b7394d24
CW
542 /* Should we ignore arp? */
543 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
544 return;
1da177e4 545
b7394d24 546 size = arp_hdr_len(skb->dev);
1da177e4 547
b7394d24
CW
548 spin_lock_irqsave(&npinfo->rx_lock, flags);
549 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
550 if (tip != np->local_ip.ip)
551 continue;
552
553 hlen = LL_RESERVED_SPACE(np->dev);
554 tlen = np->dev->needed_tailroom;
555 send_skb = find_skb(np, size + hlen + tlen, hlen);
556 if (!send_skb)
557 continue;
558
559 skb_reset_network_header(send_skb);
560 arp = (struct arphdr *) skb_put(send_skb, size);
561 send_skb->dev = skb->dev;
562 send_skb->protocol = htons(ETH_P_ARP);
563
564 /* Fill the device header for the ARP frame */
b3d936f3 565 if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
b7394d24
CW
566 sha, np->dev->dev_addr,
567 send_skb->len) < 0) {
568 kfree_skb(send_skb);
569 continue;
570 }
1da177e4 571
b7394d24
CW
572 /*
573 * Fill out the arp protocol part.
574 *
575 * we only support ethernet device type,
576 * which (according to RFC 1390) should
577 * always equal 1 (Ethernet).
578 */
579
580 arp->ar_hrd = htons(np->dev->type);
581 arp->ar_pro = htons(ETH_P_IP);
582 arp->ar_hln = np->dev->addr_len;
583 arp->ar_pln = 4;
584 arp->ar_op = htons(type);
585
586 arp_ptr = (unsigned char *)(arp + 1);
587 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
588 arp_ptr += np->dev->addr_len;
589 memcpy(arp_ptr, &tip, 4);
590 arp_ptr += 4;
591 memcpy(arp_ptr, sha, np->dev->addr_len);
592 arp_ptr += np->dev->addr_len;
593 memcpy(arp_ptr, &sip, 4);
594
595 netpoll_send_skb(np, send_skb);
596
597 /* If there are several rx_hooks for the same address,
598 we're fine by sending a single reply */
599 break;
508e14b4 600 }
b7394d24 601 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
b3d936f3
CW
602 } else if( proto == ETH_P_IPV6) {
603#if IS_ENABLED(CONFIG_IPV6)
604 struct nd_msg *msg;
605 u8 *lladdr = NULL;
606 struct ipv6hdr *hdr;
607 struct icmp6hdr *icmp6h;
608 const struct in6_addr *saddr;
609 const struct in6_addr *daddr;
610 struct inet6_dev *in6_dev = NULL;
611 struct in6_addr *target;
612
613 in6_dev = in6_dev_get(skb->dev);
614 if (!in6_dev || !in6_dev->cnf.accept_ra)
615 return;
616
617 if (!pskb_may_pull(skb, skb->len))
618 return;
619
620 msg = (struct nd_msg *)skb_transport_header(skb);
621
622 __skb_push(skb, skb->data - skb_transport_header(skb));
623
624 if (ipv6_hdr(skb)->hop_limit != 255)
625 return;
626 if (msg->icmph.icmp6_code != 0)
627 return;
628 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
629 return;
630
631 saddr = &ipv6_hdr(skb)->saddr;
632 daddr = &ipv6_hdr(skb)->daddr;
633
634 size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
635
636 spin_lock_irqsave(&npinfo->rx_lock, flags);
637 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
faeed828 638 if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
b3d936f3
CW
639 continue;
640
641 hlen = LL_RESERVED_SPACE(np->dev);
642 tlen = np->dev->needed_tailroom;
643 send_skb = find_skb(np, size + hlen + tlen, hlen);
644 if (!send_skb)
645 continue;
646
647 send_skb->protocol = htons(ETH_P_IPV6);
648 send_skb->dev = skb->dev;
649
650 skb_reset_network_header(send_skb);
651 skb_put(send_skb, sizeof(struct ipv6hdr));
652 hdr = ipv6_hdr(send_skb);
653
654 *(__be32*)hdr = htonl(0x60000000);
655
656 hdr->payload_len = htons(size);
657 hdr->nexthdr = IPPROTO_ICMPV6;
658 hdr->hop_limit = 255;
659 hdr->saddr = *saddr;
660 hdr->daddr = *daddr;
661
662 send_skb->transport_header = send_skb->tail;
663 skb_put(send_skb, size);
664
665 icmp6h = (struct icmp6hdr *)skb_transport_header(skb);
666 icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
667 icmp6h->icmp6_router = 0;
668 icmp6h->icmp6_solicited = 1;
669 target = (struct in6_addr *)skb_transport_header(send_skb) + sizeof(struct icmp6hdr);
670 *target = msg->target;
671 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
672 IPPROTO_ICMPV6,
673 csum_partial(icmp6h,
674 size, 0));
675
676 if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
677 lladdr, np->dev->dev_addr,
678 send_skb->len) < 0) {
679 kfree_skb(send_skb);
680 continue;
681 }
682
683 netpoll_send_skb(np, send_skb);
684
685 /* If there are several rx_hooks for the same address,
686 we're fine by sending a single reply */
687 break;
688 }
689 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
690#endif
508e14b4 691 }
1da177e4
LT
692}
693
b3d936f3
CW
694static bool pkt_is_ns(struct sk_buff *skb)
695{
696 struct nd_msg *msg;
697 struct ipv6hdr *hdr;
698
699 if (skb->protocol != htons(ETH_P_ARP))
700 return false;
701 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
702 return false;
703
704 msg = (struct nd_msg *)skb_transport_header(skb);
705 __skb_push(skb, skb->data - skb_transport_header(skb));
706 hdr = ipv6_hdr(skb);
707
708 if (hdr->nexthdr != IPPROTO_ICMPV6)
709 return false;
710 if (hdr->hop_limit != 255)
711 return false;
712 if (msg->icmph.icmp6_code != 0)
713 return false;
714 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
715 return false;
716
717 return true;
718}
719
57c5d461 720int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
1da177e4
LT
721{
722 int proto, len, ulen;
508e14b4 723 int hits = 0;
b71d1d42 724 const struct iphdr *iph;
1da177e4 725 struct udphdr *uh;
508e14b4 726 struct netpoll *np, *tmp;
068c6e98 727
508e14b4 728 if (list_empty(&npinfo->rx_np))
1da177e4 729 goto out;
508e14b4 730
1da177e4
LT
731 if (skb->dev->type != ARPHRD_ETHER)
732 goto out;
733
d9452e9f 734 /* check if netpoll clients need ARP */
b3d936f3
CW
735 if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
736 skb_queue_tail(&npinfo->neigh_tx, skb);
737 return 1;
738 } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
b7394d24 739 skb_queue_tail(&npinfo->neigh_tx, skb);
1da177e4
LT
740 return 1;
741 }
742
689971b4
AW
743 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
744 skb = vlan_untag(skb);
745 if (unlikely(!skb))
746 goto out;
747 }
748
1da177e4 749 proto = ntohs(eth_hdr(skb)->h_proto);
b7394d24 750 if (proto != ETH_P_IP && proto != ETH_P_IPV6)
1da177e4
LT
751 goto out;
752 if (skb->pkt_type == PACKET_OTHERHOST)
753 goto out;
754 if (skb_shared(skb))
755 goto out;
756
b7394d24
CW
757 if (proto == ETH_P_IP) {
758 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
759 goto out;
760 iph = (struct iphdr *)skb->data;
761 if (iph->ihl < 5 || iph->version != 4)
762 goto out;
763 if (!pskb_may_pull(skb, iph->ihl*4))
764 goto out;
765 iph = (struct iphdr *)skb->data;
766 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
767 goto out;
5e7d7fa5 768
b7394d24
CW
769 len = ntohs(iph->tot_len);
770 if (skb->len < len || len < iph->ihl*4)
771 goto out;
1da177e4 772
b7394d24
CW
773 /*
774 * Our transport medium may have padded the buffer out.
775 * Now We trim to the true length of the frame.
776 */
777 if (pskb_trim_rcsum(skb, len))
778 goto out;
1da177e4 779
b7394d24
CW
780 iph = (struct iphdr *)skb->data;
781 if (iph->protocol != IPPROTO_UDP)
782 goto out;
1da177e4 783
b7394d24
CW
784 len -= iph->ihl*4;
785 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
786 ulen = ntohs(uh->len);
508e14b4 787
b7394d24
CW
788 if (ulen != len)
789 goto out;
790 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
791 goto out;
792 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
793 if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
794 continue;
795 if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
796 continue;
797 if (np->local_port && np->local_port != ntohs(uh->dest))
798 continue;
799
800 np->rx_hook(np, ntohs(uh->source),
801 (char *)(uh+1),
802 ulen - sizeof(struct udphdr));
803 hits++;
804 }
b3d936f3
CW
805 } else {
806#if IS_ENABLED(CONFIG_IPV6)
807 const struct ipv6hdr *ip6h;
808
809 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
810 goto out;
811 ip6h = (struct ipv6hdr *)skb->data;
812 if (ip6h->version != 6)
813 goto out;
814 len = ntohs(ip6h->payload_len);
815 if (!len)
816 goto out;
817 if (len + sizeof(struct ipv6hdr) > skb->len)
818 goto out;
819 if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
820 goto out;
821 ip6h = ipv6_hdr(skb);
822 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
823 goto out;
824 uh = udp_hdr(skb);
825 ulen = ntohs(uh->len);
826 if (ulen != skb->len)
827 goto out;
828 if (udp6_csum_init(skb, uh, IPPROTO_UDP))
829 goto out;
830 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
faeed828 831 if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
b3d936f3 832 continue;
faeed828 833 if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
b3d936f3
CW
834 continue;
835 if (np->local_port && np->local_port != ntohs(uh->dest))
836 continue;
837
838 np->rx_hook(np, ntohs(uh->source),
839 (char *)(uh+1),
840 ulen - sizeof(struct udphdr));
841 hits++;
842 }
843#endif
508e14b4
DB
844 }
845
846 if (!hits)
847 goto out;
1da177e4
LT
848
849 kfree_skb(skb);
850 return 1;
851
852out:
853 if (atomic_read(&trapped)) {
854 kfree_skb(skb);
855 return 1;
856 }
857
858 return 0;
859}
860
0bcc1816
SS
861void netpoll_print_options(struct netpoll *np)
862{
e6ec2693 863 np_info(np, "local port %d\n", np->local_port);
b3d936f3
CW
864 if (np->ipv6)
865 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
866 else
b7394d24 867 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
e6ec2693
JP
868 np_info(np, "interface '%s'\n", np->dev_name);
869 np_info(np, "remote port %d\n", np->remote_port);
b3d936f3
CW
870 if (np->ipv6)
871 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
872 else
b7394d24 873 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
e6ec2693 874 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
0bcc1816 875}
9e34a5b5 876EXPORT_SYMBOL(netpoll_print_options);
0bcc1816 877
b7394d24
CW
878static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
879{
880 const char *end;
881
882 if (!strchr(str, ':') &&
883 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
884 if (!*end)
885 return 0;
886 }
887 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
888#if IS_ENABLED(CONFIG_IPV6)
889 if (!*end)
890 return 1;
891#else
892 return -1;
893#endif
894 }
895 return -1;
896}
897
1da177e4
LT
898int netpoll_parse_options(struct netpoll *np, char *opt)
899{
900 char *cur=opt, *delim;
b7394d24 901 int ipv6;
1da177e4 902
c68b9070 903 if (*cur != '@') {
1da177e4
LT
904 if ((delim = strchr(cur, '@')) == NULL)
905 goto parse_failed;
c68b9070 906 *delim = 0;
4b5511eb
AP
907 if (kstrtou16(cur, 10, &np->local_port))
908 goto parse_failed;
c68b9070 909 cur = delim;
1da177e4
LT
910 }
911 cur++;
1da177e4 912
c68b9070 913 if (*cur != '/') {
1da177e4
LT
914 if ((delim = strchr(cur, '/')) == NULL)
915 goto parse_failed;
c68b9070 916 *delim = 0;
b7394d24
CW
917 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
918 if (ipv6 < 0)
919 goto parse_failed;
920 else
921 np->ipv6 = (bool)ipv6;
c68b9070 922 cur = delim;
1da177e4
LT
923 }
924 cur++;
925
c68b9070 926 if (*cur != ',') {
1da177e4
LT
927 /* parse out dev name */
928 if ((delim = strchr(cur, ',')) == NULL)
929 goto parse_failed;
c68b9070 930 *delim = 0;
1da177e4 931 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
c68b9070 932 cur = delim;
1da177e4
LT
933 }
934 cur++;
935
c68b9070 936 if (*cur != '@') {
1da177e4
LT
937 /* dst port */
938 if ((delim = strchr(cur, '@')) == NULL)
939 goto parse_failed;
c68b9070 940 *delim = 0;
5fc05f87 941 if (*cur == ' ' || *cur == '\t')
e6ec2693 942 np_info(np, "warning: whitespace is not allowed\n");
4b5511eb
AP
943 if (kstrtou16(cur, 10, &np->remote_port))
944 goto parse_failed;
c68b9070 945 cur = delim;
1da177e4
LT
946 }
947 cur++;
1da177e4
LT
948
949 /* dst ip */
950 if ((delim = strchr(cur, '/')) == NULL)
951 goto parse_failed;
c68b9070 952 *delim = 0;
b7394d24
CW
953 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
954 if (ipv6 < 0)
955 goto parse_failed;
956 else if (np->ipv6 != (bool)ipv6)
957 goto parse_failed;
958 else
959 np->ipv6 = (bool)ipv6;
c68b9070 960 cur = delim + 1;
1da177e4 961
c68b9070 962 if (*cur != 0) {
1da177e4 963 /* MAC address */
4940fc88 964 if (!mac_pton(cur, np->remote_mac))
1da177e4 965 goto parse_failed;
1da177e4
LT
966 }
967
0bcc1816 968 netpoll_print_options(np);
1da177e4
LT
969
970 return 0;
971
972 parse_failed:
e6ec2693 973 np_info(np, "couldn't parse config at '%s'!\n", cur);
1da177e4
LT
974 return -1;
975}
9e34a5b5 976EXPORT_SYMBOL(netpoll_parse_options);
1da177e4 977
47be03a2 978int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
1da177e4 979{
115c1d6e 980 struct netpoll_info *npinfo;
4247e161 981 const struct net_device_ops *ops;
fbeec2e1 982 unsigned long flags;
b41848b6 983 int err;
1da177e4 984
30fdd8a0
JP
985 np->dev = ndev;
986 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
987
8fdd95ec
HX
988 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
989 !ndev->netdev_ops->ndo_poll_controller) {
e6ec2693
JP
990 np_err(np, "%s doesn't support polling, aborting\n",
991 np->dev_name);
8fdd95ec
HX
992 err = -ENOTSUPP;
993 goto out;
994 }
995
996 if (!ndev->npinfo) {
47be03a2 997 npinfo = kmalloc(sizeof(*npinfo), gfp);
8fdd95ec
HX
998 if (!npinfo) {
999 err = -ENOMEM;
1000 goto out;
1001 }
1002
1003 npinfo->rx_flags = 0;
1004 INIT_LIST_HEAD(&npinfo->rx_np);
1005
1006 spin_lock_init(&npinfo->rx_lock);
b7394d24 1007 skb_queue_head_init(&npinfo->neigh_tx);
8fdd95ec
HX
1008 skb_queue_head_init(&npinfo->txq);
1009 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
1010
1011 atomic_set(&npinfo->refcnt, 1);
1012
1013 ops = np->dev->netdev_ops;
1014 if (ops->ndo_netpoll_setup) {
47be03a2 1015 err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
8fdd95ec
HX
1016 if (err)
1017 goto free_npinfo;
1018 }
1019 } else {
1020 npinfo = ndev->npinfo;
1021 atomic_inc(&npinfo->refcnt);
1022 }
1023
1024 npinfo->netpoll = np;
1025
1026 if (np->rx_hook) {
1027 spin_lock_irqsave(&npinfo->rx_lock, flags);
1028 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
1029 list_add_tail(&np->rx, &npinfo->rx_np);
1030 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1031 }
1032
1033 /* last thing to do is link it to the net device structure */
cf778b00 1034 rcu_assign_pointer(ndev->npinfo, npinfo);
8fdd95ec
HX
1035
1036 return 0;
1037
1038free_npinfo:
1039 kfree(npinfo);
1040out:
1041 return err;
1042}
1043EXPORT_SYMBOL_GPL(__netpoll_setup);
1044
1045int netpoll_setup(struct netpoll *np)
1046{
1047 struct net_device *ndev = NULL;
1048 struct in_device *in_dev;
1049 int err;
1050
f92d3180 1051 rtnl_lock();
556e6256
CW
1052 if (np->dev_name) {
1053 struct net *net = current->nsproxy->net_ns;
1054 ndev = __dev_get_by_name(net, np->dev_name);
1055 }
1da177e4 1056 if (!ndev) {
e6ec2693 1057 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
f92d3180
CW
1058 err = -ENODEV;
1059 goto unlock;
1da177e4 1060 }
5bd30d39 1061 dev_hold(ndev);
1da177e4 1062
49bd8fb0 1063 if (netdev_master_upper_dev_get(ndev)) {
e6ec2693 1064 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
83fe32de
DC
1065 err = -EBUSY;
1066 goto put;
0c1ad04a
WC
1067 }
1068
1da177e4
LT
1069 if (!netif_running(ndev)) {
1070 unsigned long atmost, atleast;
1071
e6ec2693 1072 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
1da177e4 1073
b41848b6 1074 err = dev_open(ndev);
b41848b6
SH
1075
1076 if (err) {
e6ec2693 1077 np_err(np, "failed to open %s\n", ndev->name);
dbaa1541 1078 goto put;
1da177e4 1079 }
1da177e4 1080
f92d3180 1081 rtnl_unlock();
1da177e4 1082 atleast = jiffies + HZ/10;
bff38771 1083 atmost = jiffies + carrier_timeout * HZ;
1da177e4
LT
1084 while (!netif_carrier_ok(ndev)) {
1085 if (time_after(jiffies, atmost)) {
e6ec2693 1086 np_notice(np, "timeout waiting for carrier\n");
1da177e4
LT
1087 break;
1088 }
1b614fb9 1089 msleep(1);
1da177e4
LT
1090 }
1091
1092 /* If carrier appears to come up instantly, we don't
1093 * trust it and pause so that we don't pump all our
1094 * queued console messages into the bitbucket.
1095 */
1096
1097 if (time_before(jiffies, atleast)) {
e6ec2693 1098 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
1da177e4
LT
1099 msleep(4000);
1100 }
f92d3180 1101 rtnl_lock();
1da177e4
LT
1102 }
1103
b7394d24
CW
1104 if (!np->local_ip.ip) {
1105 if (!np->ipv6) {
f92d3180 1106 in_dev = __in_dev_get_rtnl(ndev);
b7394d24
CW
1107
1108 if (!in_dev || !in_dev->ifa_list) {
b7394d24
CW
1109 np_err(np, "no IP address for %s, aborting\n",
1110 np->dev_name);
1111 err = -EDESTADDRREQ;
1112 goto put;
1113 }
1114
1115 np->local_ip.ip = in_dev->ifa_list->ifa_local;
b7394d24 1116 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
b3d936f3
CW
1117 } else {
1118#if IS_ENABLED(CONFIG_IPV6)
1119 struct inet6_dev *idev;
1120
1121 err = -EDESTADDRREQ;
b3d936f3
CW
1122 idev = __in6_dev_get(ndev);
1123 if (idev) {
1124 struct inet6_ifaddr *ifp;
1125
1126 read_lock_bh(&idev->lock);
1127 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1128 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
1129 continue;
1130 np->local_ip.in6 = ifp->addr;
1131 err = 0;
1132 break;
1133 }
1134 read_unlock_bh(&idev->lock);
1135 }
b3d936f3
CW
1136 if (err) {
1137 np_err(np, "no IPv6 address for %s, aborting\n",
1138 np->dev_name);
1139 goto put;
1140 } else
1141 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
1142#else
1143 np_err(np, "IPv6 is not supported %s, aborting\n",
1144 np->dev_name);
e39363a9 1145 err = -EINVAL;
b3d936f3
CW
1146 goto put;
1147#endif
1da177e4 1148 }
1da177e4
LT
1149 }
1150
dbaa1541
HX
1151 /* fill up the skb queue */
1152 refill_skbs();
1153
47be03a2 1154 err = __netpoll_setup(np, ndev, GFP_KERNEL);
8fdd95ec
HX
1155 if (err)
1156 goto put;
1157
f92d3180 1158 rtnl_unlock();
1da177e4
LT
1159 return 0;
1160
21edbb22 1161put:
1da177e4 1162 dev_put(ndev);
f92d3180
CW
1163unlock:
1164 rtnl_unlock();
b41848b6 1165 return err;
1da177e4 1166}
9e34a5b5 1167EXPORT_SYMBOL(netpoll_setup);
1da177e4 1168
c68b9070
DM
1169static int __init netpoll_init(void)
1170{
a1bcfacd
SH
1171 skb_queue_head_init(&skb_pool);
1172 return 0;
1173}
1174core_initcall(netpoll_init);
1175
38e6bc18
AW
1176static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
1177{
1178 struct netpoll_info *npinfo =
1179 container_of(rcu_head, struct netpoll_info, rcu);
1180
b7394d24 1181 skb_queue_purge(&npinfo->neigh_tx);
38e6bc18
AW
1182 skb_queue_purge(&npinfo->txq);
1183
1184 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
1185 cancel_delayed_work(&npinfo->tx_work);
1186
1187 /* clean after last, unfinished work */
1188 __skb_queue_purge(&npinfo->txq);
1189 /* now cancel it again */
1190 cancel_delayed_work(&npinfo->tx_work);
1191 kfree(npinfo);
1192}
1193
8fdd95ec 1194void __netpoll_cleanup(struct netpoll *np)
1da177e4 1195{
fbeec2e1
JM
1196 struct netpoll_info *npinfo;
1197 unsigned long flags;
1198
8fdd95ec
HX
1199 npinfo = np->dev->npinfo;
1200 if (!npinfo)
dbaa1541 1201 return;
93ec2c72 1202
8fdd95ec
HX
1203 if (!list_empty(&npinfo->rx_np)) {
1204 spin_lock_irqsave(&npinfo->rx_lock, flags);
1205 list_del(&np->rx);
1206 if (list_empty(&npinfo->rx_np))
1207 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
1208 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1209 }
de85d99e 1210
8fdd95ec
HX
1211 if (atomic_dec_and_test(&npinfo->refcnt)) {
1212 const struct net_device_ops *ops;
de85d99e 1213
8fdd95ec
HX
1214 ops = np->dev->netdev_ops;
1215 if (ops->ndo_netpoll_cleanup)
1216 ops->ndo_netpoll_cleanup(np->dev);
de85d99e 1217
a9b3cd7f 1218 RCU_INIT_POINTER(np->dev->npinfo, NULL);
38e6bc18
AW
1219 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
1220 }
1221}
1222EXPORT_SYMBOL_GPL(__netpoll_cleanup);
de85d99e 1223
38e6bc18
AW
1224static void rcu_cleanup_netpoll(struct rcu_head *rcu_head)
1225{
1226 struct netpoll *np = container_of(rcu_head, struct netpoll, rcu);
93ec2c72 1227
38e6bc18
AW
1228 __netpoll_cleanup(np);
1229 kfree(np);
1230}
93ec2c72 1231
38e6bc18
AW
1232void __netpoll_free_rcu(struct netpoll *np)
1233{
1234 call_rcu_bh(&np->rcu, rcu_cleanup_netpoll);
8fdd95ec 1235}
38e6bc18 1236EXPORT_SYMBOL_GPL(__netpoll_free_rcu);
fbeec2e1 1237
8fdd95ec
HX
1238void netpoll_cleanup(struct netpoll *np)
1239{
1240 if (!np->dev)
1241 return;
dbaa1541 1242
8fdd95ec
HX
1243 rtnl_lock();
1244 __netpoll_cleanup(np);
1245 rtnl_unlock();
1246
1247 dev_put(np->dev);
1da177e4
LT
1248 np->dev = NULL;
1249}
9e34a5b5 1250EXPORT_SYMBOL(netpoll_cleanup);
1da177e4
LT
1251
1252int netpoll_trap(void)
1253{
1254 return atomic_read(&trapped);
1255}
9e34a5b5 1256EXPORT_SYMBOL(netpoll_trap);
1da177e4
LT
1257
1258void netpoll_set_trap(int trap)
1259{
1260 if (trap)
1261 atomic_inc(&trapped);
1262 else
1263 atomic_dec(&trapped);
1264}
1da177e4 1265EXPORT_SYMBOL(netpoll_set_trap);