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