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