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