xref: /openbmc/linux/net/core/netpoll.c (revision 0a335b6d)
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 static atomic_t trapped;
50 
51 DEFINE_STATIC_SRCU(netpoll_srcu);
52 
53 #define USEC_PER_POLL	50
54 #define NETPOLL_RX_ENABLED  1
55 #define NETPOLL_RX_DROP     2
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 netpoll_info *npinfo,
148 			 struct napi_struct *napi, int budget)
149 {
150 	int work;
151 
152 	/* net_rx_action's ->poll() invocations and our's are
153 	 * synchronized by this test which is only made while
154 	 * holding the napi->poll_lock.
155 	 */
156 	if (!test_bit(NAPI_STATE_SCHED, &napi->state))
157 		return budget;
158 
159 	npinfo->rx_flags |= NETPOLL_RX_DROP;
160 	atomic_inc(&trapped);
161 	set_bit(NAPI_STATE_NPSVC, &napi->state);
162 
163 	work = napi->poll(napi, budget);
164 	trace_napi_poll(napi);
165 
166 	clear_bit(NAPI_STATE_NPSVC, &napi->state);
167 	atomic_dec(&trapped);
168 	npinfo->rx_flags &= ~NETPOLL_RX_DROP;
169 
170 	return budget - work;
171 }
172 
173 static void poll_napi(struct net_device *dev)
174 {
175 	struct napi_struct *napi;
176 	int budget = 16;
177 
178 	list_for_each_entry(napi, &dev->napi_list, dev_list) {
179 		if (napi->poll_owner != smp_processor_id() &&
180 		    spin_trylock(&napi->poll_lock)) {
181 			budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
182 					       napi, budget);
183 			spin_unlock(&napi->poll_lock);
184 
185 			if (!budget)
186 				break;
187 		}
188 	}
189 }
190 
191 static void service_neigh_queue(struct netpoll_info *npi)
192 {
193 	if (npi) {
194 		struct sk_buff *skb;
195 
196 		while ((skb = skb_dequeue(&npi->neigh_tx)))
197 			netpoll_neigh_reply(skb, npi);
198 	}
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 
206 	/* Don't do any rx activity if the dev_lock mutex is held
207 	 * the dev_open/close paths use this to block netpoll activity
208 	 * while changing device state
209 	 */
210 	if (down_trylock(&ni->dev_lock))
211 		return;
212 
213 	if (!netif_running(dev)) {
214 		up(&ni->dev_lock);
215 		return;
216 	}
217 
218 	ops = dev->netdev_ops;
219 	if (!ops->ndo_poll_controller) {
220 		up(&ni->dev_lock);
221 		return;
222 	}
223 
224 	/* Process pending work on NIC */
225 	ops->ndo_poll_controller(dev);
226 
227 	poll_napi(dev);
228 
229 	up(&ni->dev_lock);
230 
231 	if (dev->flags & IFF_SLAVE) {
232 		if (ni) {
233 			struct net_device *bond_dev;
234 			struct sk_buff *skb;
235 			struct netpoll_info *bond_ni;
236 
237 			bond_dev = netdev_master_upper_dev_get_rcu(dev);
238 			bond_ni = rcu_dereference_bh(bond_dev->npinfo);
239 			while ((skb = skb_dequeue(&ni->neigh_tx))) {
240 				skb->dev = bond_dev;
241 				skb_queue_tail(&bond_ni->neigh_tx, skb);
242 			}
243 		}
244 	}
245 
246 	service_neigh_queue(ni);
247 
248 	zap_completion_queue();
249 }
250 
251 void netpoll_rx_disable(struct net_device *dev)
252 {
253 	struct netpoll_info *ni;
254 	int idx;
255 	might_sleep();
256 	idx = srcu_read_lock(&netpoll_srcu);
257 	ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
258 	if (ni)
259 		down(&ni->dev_lock);
260 	srcu_read_unlock(&netpoll_srcu, idx);
261 }
262 EXPORT_SYMBOL(netpoll_rx_disable);
263 
264 void netpoll_rx_enable(struct net_device *dev)
265 {
266 	struct netpoll_info *ni;
267 	rcu_read_lock();
268 	ni = rcu_dereference(dev->npinfo);
269 	if (ni)
270 		up(&ni->dev_lock);
271 	rcu_read_unlock();
272 }
273 EXPORT_SYMBOL(netpoll_rx_enable);
274 
275 static void refill_skbs(void)
276 {
277 	struct sk_buff *skb;
278 	unsigned long flags;
279 
280 	spin_lock_irqsave(&skb_pool.lock, flags);
281 	while (skb_pool.qlen < MAX_SKBS) {
282 		skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
283 		if (!skb)
284 			break;
285 
286 		__skb_queue_tail(&skb_pool, skb);
287 	}
288 	spin_unlock_irqrestore(&skb_pool.lock, flags);
289 }
290 
291 static void zap_completion_queue(void)
292 {
293 	unsigned long flags;
294 	struct softnet_data *sd = &get_cpu_var(softnet_data);
295 
296 	if (sd->completion_queue) {
297 		struct sk_buff *clist;
298 
299 		local_irq_save(flags);
300 		clist = sd->completion_queue;
301 		sd->completion_queue = NULL;
302 		local_irq_restore(flags);
303 
304 		while (clist != NULL) {
305 			struct sk_buff *skb = clist;
306 			clist = clist->next;
307 			if (skb->destructor) {
308 				atomic_inc(&skb->users);
309 				dev_kfree_skb_any(skb); /* put this one back */
310 			} else {
311 				__kfree_skb(skb);
312 			}
313 		}
314 	}
315 
316 	put_cpu_var(softnet_data);
317 }
318 
319 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
320 {
321 	int count = 0;
322 	struct sk_buff *skb;
323 
324 	zap_completion_queue();
325 	refill_skbs();
326 repeat:
327 
328 	skb = alloc_skb(len, GFP_ATOMIC);
329 	if (!skb)
330 		skb = skb_dequeue(&skb_pool);
331 
332 	if (!skb) {
333 		if (++count < 10) {
334 			netpoll_poll_dev(np->dev);
335 			goto repeat;
336 		}
337 		return NULL;
338 	}
339 
340 	atomic_set(&skb->users, 1);
341 	skb_reserve(skb, reserve);
342 	return skb;
343 }
344 
345 static int netpoll_owner_active(struct net_device *dev)
346 {
347 	struct napi_struct *napi;
348 
349 	list_for_each_entry(napi, &dev->napi_list, dev_list) {
350 		if (napi->poll_owner == smp_processor_id())
351 			return 1;
352 	}
353 	return 0;
354 }
355 
356 /* call with IRQ disabled */
357 void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
358 			     struct net_device *dev)
359 {
360 	int status = NETDEV_TX_BUSY;
361 	unsigned long tries;
362 	const struct net_device_ops *ops = dev->netdev_ops;
363 	/* It is up to the caller to keep npinfo alive. */
364 	struct netpoll_info *npinfo;
365 
366 	WARN_ON_ONCE(!irqs_disabled());
367 
368 	npinfo = rcu_dereference_bh(np->dev->npinfo);
369 	if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
370 		__kfree_skb(skb);
371 		return;
372 	}
373 
374 	/* don't get messages out of order, and no recursion */
375 	if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
376 		struct netdev_queue *txq;
377 
378 		txq = netdev_pick_tx(dev, skb);
379 
380 		/* try until next clock tick */
381 		for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
382 		     tries > 0; --tries) {
383 			if (__netif_tx_trylock(txq)) {
384 				if (!netif_xmit_stopped(txq)) {
385 					if (vlan_tx_tag_present(skb) &&
386 					    !vlan_hw_offload_capable(netif_skb_features(skb),
387 								     skb->vlan_proto)) {
388 						skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
389 						if (unlikely(!skb))
390 							break;
391 						skb->vlan_tci = 0;
392 					}
393 
394 					status = ops->ndo_start_xmit(skb, dev);
395 					if (status == NETDEV_TX_OK)
396 						txq_trans_update(txq);
397 				}
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 	memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
517 	memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
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 (list_empty(&npinfo->rx_np))
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_hooks for the same address,
640 			   we're fine by sending a single reply */
641 			break;
642 		}
643 		spin_unlock_irqrestore(&npinfo->rx_lock, flags);
644 	} else if( proto == ETH_P_IPV6) {
645 #if IS_ENABLED(CONFIG_IPV6)
646 		struct nd_msg *msg;
647 		u8 *lladdr = NULL;
648 		struct ipv6hdr *hdr;
649 		struct icmp6hdr *icmp6h;
650 		const struct in6_addr *saddr;
651 		const struct in6_addr *daddr;
652 		struct inet6_dev *in6_dev = NULL;
653 		struct in6_addr *target;
654 
655 		in6_dev = in6_dev_get(skb->dev);
656 		if (!in6_dev || !in6_dev->cnf.accept_ra)
657 			return;
658 
659 		if (!pskb_may_pull(skb, skb->len))
660 			return;
661 
662 		msg = (struct nd_msg *)skb_transport_header(skb);
663 
664 		__skb_push(skb, skb->data - skb_transport_header(skb));
665 
666 		if (ipv6_hdr(skb)->hop_limit != 255)
667 			return;
668 		if (msg->icmph.icmp6_code != 0)
669 			return;
670 		if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
671 			return;
672 
673 		saddr = &ipv6_hdr(skb)->saddr;
674 		daddr = &ipv6_hdr(skb)->daddr;
675 
676 		size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
677 
678 		spin_lock_irqsave(&npinfo->rx_lock, flags);
679 		list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
680 			if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
681 				continue;
682 
683 			hlen = LL_RESERVED_SPACE(np->dev);
684 			tlen = np->dev->needed_tailroom;
685 			send_skb = find_skb(np, size + hlen + tlen, hlen);
686 			if (!send_skb)
687 				continue;
688 
689 			send_skb->protocol = htons(ETH_P_IPV6);
690 			send_skb->dev = skb->dev;
691 
692 			skb_reset_network_header(send_skb);
693 			hdr = (struct ipv6hdr *) skb_put(send_skb, sizeof(struct ipv6hdr));
694 			*(__be32*)hdr = htonl(0x60000000);
695 			hdr->payload_len = htons(size);
696 			hdr->nexthdr = IPPROTO_ICMPV6;
697 			hdr->hop_limit = 255;
698 			hdr->saddr = *saddr;
699 			hdr->daddr = *daddr;
700 
701 			icmp6h = (struct icmp6hdr *) skb_put(send_skb, sizeof(struct icmp6hdr));
702 			icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
703 			icmp6h->icmp6_router = 0;
704 			icmp6h->icmp6_solicited = 1;
705 
706 			target = (struct in6_addr *) skb_put(send_skb, sizeof(struct in6_addr));
707 			*target = msg->target;
708 			icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
709 							      IPPROTO_ICMPV6,
710 							      csum_partial(icmp6h,
711 									   size, 0));
712 
713 			if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
714 					    lladdr, np->dev->dev_addr,
715 					    send_skb->len) < 0) {
716 				kfree_skb(send_skb);
717 				continue;
718 			}
719 
720 			netpoll_send_skb(np, send_skb);
721 
722 			/* If there are several rx_hooks for the same address,
723 			   we're fine by sending a single reply */
724 			break;
725 		}
726 		spin_unlock_irqrestore(&npinfo->rx_lock, flags);
727 #endif
728 	}
729 }
730 
731 static bool pkt_is_ns(struct sk_buff *skb)
732 {
733 	struct nd_msg *msg;
734 	struct ipv6hdr *hdr;
735 
736 	if (skb->protocol != htons(ETH_P_ARP))
737 		return false;
738 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
739 		return false;
740 
741 	msg = (struct nd_msg *)skb_transport_header(skb);
742 	__skb_push(skb, skb->data - skb_transport_header(skb));
743 	hdr = ipv6_hdr(skb);
744 
745 	if (hdr->nexthdr != IPPROTO_ICMPV6)
746 		return false;
747 	if (hdr->hop_limit != 255)
748 		return false;
749 	if (msg->icmph.icmp6_code != 0)
750 		return false;
751 	if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
752 		return false;
753 
754 	return true;
755 }
756 
757 int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
758 {
759 	int proto, len, ulen;
760 	int hits = 0;
761 	const struct iphdr *iph;
762 	struct udphdr *uh;
763 	struct netpoll *np, *tmp;
764 
765 	if (list_empty(&npinfo->rx_np))
766 		goto out;
767 
768 	if (skb->dev->type != ARPHRD_ETHER)
769 		goto out;
770 
771 	/* check if netpoll clients need ARP */
772 	if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
773 		skb_queue_tail(&npinfo->neigh_tx, skb);
774 		return 1;
775 	} else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
776 		skb_queue_tail(&npinfo->neigh_tx, skb);
777 		return 1;
778 	}
779 
780 	if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
781 		skb = vlan_untag(skb);
782 		if (unlikely(!skb))
783 			goto out;
784 	}
785 
786 	proto = ntohs(eth_hdr(skb)->h_proto);
787 	if (proto != ETH_P_IP && proto != ETH_P_IPV6)
788 		goto out;
789 	if (skb->pkt_type == PACKET_OTHERHOST)
790 		goto out;
791 	if (skb_shared(skb))
792 		goto out;
793 
794 	if (proto == ETH_P_IP) {
795 		if (!pskb_may_pull(skb, sizeof(struct iphdr)))
796 			goto out;
797 		iph = (struct iphdr *)skb->data;
798 		if (iph->ihl < 5 || iph->version != 4)
799 			goto out;
800 		if (!pskb_may_pull(skb, iph->ihl*4))
801 			goto out;
802 		iph = (struct iphdr *)skb->data;
803 		if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
804 			goto out;
805 
806 		len = ntohs(iph->tot_len);
807 		if (skb->len < len || len < iph->ihl*4)
808 			goto out;
809 
810 		/*
811 		 * Our transport medium may have padded the buffer out.
812 		 * Now We trim to the true length of the frame.
813 		 */
814 		if (pskb_trim_rcsum(skb, len))
815 			goto out;
816 
817 		iph = (struct iphdr *)skb->data;
818 		if (iph->protocol != IPPROTO_UDP)
819 			goto out;
820 
821 		len -= iph->ihl*4;
822 		uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
823 		ulen = ntohs(uh->len);
824 
825 		if (ulen != len)
826 			goto out;
827 		if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
828 			goto out;
829 		list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
830 			if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
831 				continue;
832 			if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
833 				continue;
834 			if (np->local_port && np->local_port != ntohs(uh->dest))
835 				continue;
836 
837 			np->rx_hook(np, ntohs(uh->source),
838 				       (char *)(uh+1),
839 				       ulen - sizeof(struct udphdr));
840 			hits++;
841 		}
842 	} else {
843 #if IS_ENABLED(CONFIG_IPV6)
844 		const struct ipv6hdr *ip6h;
845 
846 		if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
847 			goto out;
848 		ip6h = (struct ipv6hdr *)skb->data;
849 		if (ip6h->version != 6)
850 			goto out;
851 		len = ntohs(ip6h->payload_len);
852 		if (!len)
853 			goto out;
854 		if (len + sizeof(struct ipv6hdr) > skb->len)
855 			goto out;
856 		if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
857 			goto out;
858 		ip6h = ipv6_hdr(skb);
859 		if (!pskb_may_pull(skb, sizeof(struct udphdr)))
860 			goto out;
861 		uh = udp_hdr(skb);
862 		ulen = ntohs(uh->len);
863 		if (ulen != skb->len)
864 			goto out;
865 		if (udp6_csum_init(skb, uh, IPPROTO_UDP))
866 			goto out;
867 		list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
868 			if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
869 				continue;
870 			if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
871 				continue;
872 			if (np->local_port && np->local_port != ntohs(uh->dest))
873 				continue;
874 
875 			np->rx_hook(np, ntohs(uh->source),
876 				       (char *)(uh+1),
877 				       ulen - sizeof(struct udphdr));
878 			hits++;
879 		}
880 #endif
881 	}
882 
883 	if (!hits)
884 		goto out;
885 
886 	kfree_skb(skb);
887 	return 1;
888 
889 out:
890 	if (atomic_read(&trapped)) {
891 		kfree_skb(skb);
892 		return 1;
893 	}
894 
895 	return 0;
896 }
897 
898 void netpoll_print_options(struct netpoll *np)
899 {
900 	np_info(np, "local port %d\n", np->local_port);
901 	if (np->ipv6)
902 		np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
903 	else
904 		np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
905 	np_info(np, "interface '%s'\n", np->dev_name);
906 	np_info(np, "remote port %d\n", np->remote_port);
907 	if (np->ipv6)
908 		np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
909 	else
910 		np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
911 	np_info(np, "remote ethernet address %pM\n", np->remote_mac);
912 }
913 EXPORT_SYMBOL(netpoll_print_options);
914 
915 static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
916 {
917 	const char *end;
918 
919 	if (!strchr(str, ':') &&
920 	    in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
921 		if (!*end)
922 			return 0;
923 	}
924 	if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
925 #if IS_ENABLED(CONFIG_IPV6)
926 		if (!*end)
927 			return 1;
928 #else
929 		return -1;
930 #endif
931 	}
932 	return -1;
933 }
934 
935 int netpoll_parse_options(struct netpoll *np, char *opt)
936 {
937 	char *cur=opt, *delim;
938 	int ipv6;
939 
940 	if (*cur != '@') {
941 		if ((delim = strchr(cur, '@')) == NULL)
942 			goto parse_failed;
943 		*delim = 0;
944 		if (kstrtou16(cur, 10, &np->local_port))
945 			goto parse_failed;
946 		cur = delim;
947 	}
948 	cur++;
949 
950 	if (*cur != '/') {
951 		if ((delim = strchr(cur, '/')) == NULL)
952 			goto parse_failed;
953 		*delim = 0;
954 		ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
955 		if (ipv6 < 0)
956 			goto parse_failed;
957 		else
958 			np->ipv6 = (bool)ipv6;
959 		cur = delim;
960 	}
961 	cur++;
962 
963 	if (*cur != ',') {
964 		/* parse out dev name */
965 		if ((delim = strchr(cur, ',')) == NULL)
966 			goto parse_failed;
967 		*delim = 0;
968 		strlcpy(np->dev_name, cur, sizeof(np->dev_name));
969 		cur = delim;
970 	}
971 	cur++;
972 
973 	if (*cur != '@') {
974 		/* dst port */
975 		if ((delim = strchr(cur, '@')) == NULL)
976 			goto parse_failed;
977 		*delim = 0;
978 		if (*cur == ' ' || *cur == '\t')
979 			np_info(np, "warning: whitespace is not allowed\n");
980 		if (kstrtou16(cur, 10, &np->remote_port))
981 			goto parse_failed;
982 		cur = delim;
983 	}
984 	cur++;
985 
986 	/* dst ip */
987 	if ((delim = strchr(cur, '/')) == NULL)
988 		goto parse_failed;
989 	*delim = 0;
990 	ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
991 	if (ipv6 < 0)
992 		goto parse_failed;
993 	else if (np->ipv6 != (bool)ipv6)
994 		goto parse_failed;
995 	else
996 		np->ipv6 = (bool)ipv6;
997 	cur = delim + 1;
998 
999 	if (*cur != 0) {
1000 		/* MAC address */
1001 		if (!mac_pton(cur, np->remote_mac))
1002 			goto parse_failed;
1003 	}
1004 
1005 	netpoll_print_options(np);
1006 
1007 	return 0;
1008 
1009  parse_failed:
1010 	np_info(np, "couldn't parse config at '%s'!\n", cur);
1011 	return -1;
1012 }
1013 EXPORT_SYMBOL(netpoll_parse_options);
1014 
1015 int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
1016 {
1017 	struct netpoll_info *npinfo;
1018 	const struct net_device_ops *ops;
1019 	unsigned long flags;
1020 	int err;
1021 
1022 	np->dev = ndev;
1023 	strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
1024 	INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
1025 
1026 	if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
1027 	    !ndev->netdev_ops->ndo_poll_controller) {
1028 		np_err(np, "%s doesn't support polling, aborting\n",
1029 		       np->dev_name);
1030 		err = -ENOTSUPP;
1031 		goto out;
1032 	}
1033 
1034 	if (!ndev->npinfo) {
1035 		npinfo = kmalloc(sizeof(*npinfo), gfp);
1036 		if (!npinfo) {
1037 			err = -ENOMEM;
1038 			goto out;
1039 		}
1040 
1041 		npinfo->rx_flags = 0;
1042 		INIT_LIST_HEAD(&npinfo->rx_np);
1043 
1044 		spin_lock_init(&npinfo->rx_lock);
1045 		sema_init(&npinfo->dev_lock, 1);
1046 		skb_queue_head_init(&npinfo->neigh_tx);
1047 		skb_queue_head_init(&npinfo->txq);
1048 		INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
1049 
1050 		atomic_set(&npinfo->refcnt, 1);
1051 
1052 		ops = np->dev->netdev_ops;
1053 		if (ops->ndo_netpoll_setup) {
1054 			err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
1055 			if (err)
1056 				goto free_npinfo;
1057 		}
1058 	} else {
1059 		npinfo = rtnl_dereference(ndev->npinfo);
1060 		atomic_inc(&npinfo->refcnt);
1061 	}
1062 
1063 	npinfo->netpoll = np;
1064 
1065 	if (np->rx_hook) {
1066 		spin_lock_irqsave(&npinfo->rx_lock, flags);
1067 		npinfo->rx_flags |= NETPOLL_RX_ENABLED;
1068 		list_add_tail(&np->rx, &npinfo->rx_np);
1069 		spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1070 	}
1071 
1072 	/* last thing to do is link it to the net device structure */
1073 	rcu_assign_pointer(ndev->npinfo, npinfo);
1074 
1075 	return 0;
1076 
1077 free_npinfo:
1078 	kfree(npinfo);
1079 out:
1080 	return err;
1081 }
1082 EXPORT_SYMBOL_GPL(__netpoll_setup);
1083 
1084 int netpoll_setup(struct netpoll *np)
1085 {
1086 	struct net_device *ndev = NULL;
1087 	struct in_device *in_dev;
1088 	int err;
1089 
1090 	rtnl_lock();
1091 	if (np->dev_name) {
1092 		struct net *net = current->nsproxy->net_ns;
1093 		ndev = __dev_get_by_name(net, np->dev_name);
1094 	}
1095 	if (!ndev) {
1096 		np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
1097 		err = -ENODEV;
1098 		goto unlock;
1099 	}
1100 	dev_hold(ndev);
1101 
1102 	if (netdev_master_upper_dev_get(ndev)) {
1103 		np_err(np, "%s is a slave device, aborting\n", np->dev_name);
1104 		err = -EBUSY;
1105 		goto put;
1106 	}
1107 
1108 	if (!netif_running(ndev)) {
1109 		unsigned long atmost, atleast;
1110 
1111 		np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
1112 
1113 		err = dev_open(ndev);
1114 
1115 		if (err) {
1116 			np_err(np, "failed to open %s\n", ndev->name);
1117 			goto put;
1118 		}
1119 
1120 		rtnl_unlock();
1121 		atleast = jiffies + HZ/10;
1122 		atmost = jiffies + carrier_timeout * HZ;
1123 		while (!netif_carrier_ok(ndev)) {
1124 			if (time_after(jiffies, atmost)) {
1125 				np_notice(np, "timeout waiting for carrier\n");
1126 				break;
1127 			}
1128 			msleep(1);
1129 		}
1130 
1131 		/* If carrier appears to come up instantly, we don't
1132 		 * trust it and pause so that we don't pump all our
1133 		 * queued console messages into the bitbucket.
1134 		 */
1135 
1136 		if (time_before(jiffies, atleast)) {
1137 			np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
1138 			msleep(4000);
1139 		}
1140 		rtnl_lock();
1141 	}
1142 
1143 	if (!np->local_ip.ip) {
1144 		if (!np->ipv6) {
1145 			in_dev = __in_dev_get_rtnl(ndev);
1146 
1147 			if (!in_dev || !in_dev->ifa_list) {
1148 				np_err(np, "no IP address for %s, aborting\n",
1149 				       np->dev_name);
1150 				err = -EDESTADDRREQ;
1151 				goto put;
1152 			}
1153 
1154 			np->local_ip.ip = in_dev->ifa_list->ifa_local;
1155 			np_info(np, "local IP %pI4\n", &np->local_ip.ip);
1156 		} else {
1157 #if IS_ENABLED(CONFIG_IPV6)
1158 			struct inet6_dev *idev;
1159 
1160 			err = -EDESTADDRREQ;
1161 			idev = __in6_dev_get(ndev);
1162 			if (idev) {
1163 				struct inet6_ifaddr *ifp;
1164 
1165 				read_lock_bh(&idev->lock);
1166 				list_for_each_entry(ifp, &idev->addr_list, if_list) {
1167 					if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
1168 						continue;
1169 					np->local_ip.in6 = ifp->addr;
1170 					err = 0;
1171 					break;
1172 				}
1173 				read_unlock_bh(&idev->lock);
1174 			}
1175 			if (err) {
1176 				np_err(np, "no IPv6 address for %s, aborting\n",
1177 				       np->dev_name);
1178 				goto put;
1179 			} else
1180 				np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
1181 #else
1182 			np_err(np, "IPv6 is not supported %s, aborting\n",
1183 			       np->dev_name);
1184 			err = -EINVAL;
1185 			goto put;
1186 #endif
1187 		}
1188 	}
1189 
1190 	/* fill up the skb queue */
1191 	refill_skbs();
1192 
1193 	err = __netpoll_setup(np, ndev, GFP_KERNEL);
1194 	if (err)
1195 		goto put;
1196 
1197 	rtnl_unlock();
1198 	return 0;
1199 
1200 put:
1201 	dev_put(ndev);
1202 unlock:
1203 	rtnl_unlock();
1204 	return err;
1205 }
1206 EXPORT_SYMBOL(netpoll_setup);
1207 
1208 static int __init netpoll_init(void)
1209 {
1210 	skb_queue_head_init(&skb_pool);
1211 	return 0;
1212 }
1213 core_initcall(netpoll_init);
1214 
1215 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
1216 {
1217 	struct netpoll_info *npinfo =
1218 			container_of(rcu_head, struct netpoll_info, rcu);
1219 
1220 	skb_queue_purge(&npinfo->neigh_tx);
1221 	skb_queue_purge(&npinfo->txq);
1222 
1223 	/* we can't call cancel_delayed_work_sync here, as we are in softirq */
1224 	cancel_delayed_work(&npinfo->tx_work);
1225 
1226 	/* clean after last, unfinished work */
1227 	__skb_queue_purge(&npinfo->txq);
1228 	/* now cancel it again */
1229 	cancel_delayed_work(&npinfo->tx_work);
1230 	kfree(npinfo);
1231 }
1232 
1233 void __netpoll_cleanup(struct netpoll *np)
1234 {
1235 	struct netpoll_info *npinfo;
1236 	unsigned long flags;
1237 
1238 	/* rtnl_dereference would be preferable here but
1239 	 * rcu_cleanup_netpoll path can put us in here safely without
1240 	 * holding the rtnl, so plain rcu_dereference it is
1241 	 */
1242 	npinfo = rtnl_dereference(np->dev->npinfo);
1243 	if (!npinfo)
1244 		return;
1245 
1246 	if (!list_empty(&npinfo->rx_np)) {
1247 		spin_lock_irqsave(&npinfo->rx_lock, flags);
1248 		list_del(&np->rx);
1249 		if (list_empty(&npinfo->rx_np))
1250 			npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
1251 		spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1252 	}
1253 
1254 	synchronize_srcu(&netpoll_srcu);
1255 
1256 	if (atomic_dec_and_test(&npinfo->refcnt)) {
1257 		const struct net_device_ops *ops;
1258 
1259 		ops = np->dev->netdev_ops;
1260 		if (ops->ndo_netpoll_cleanup)
1261 			ops->ndo_netpoll_cleanup(np->dev);
1262 
1263 		rcu_assign_pointer(np->dev->npinfo, NULL);
1264 		call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
1265 	}
1266 }
1267 EXPORT_SYMBOL_GPL(__netpoll_cleanup);
1268 
1269 static void netpoll_async_cleanup(struct work_struct *work)
1270 {
1271 	struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
1272 
1273 	rtnl_lock();
1274 	__netpoll_cleanup(np);
1275 	rtnl_unlock();
1276 	kfree(np);
1277 }
1278 
1279 void __netpoll_free_async(struct netpoll *np)
1280 {
1281 	schedule_work(&np->cleanup_work);
1282 }
1283 EXPORT_SYMBOL_GPL(__netpoll_free_async);
1284 
1285 void netpoll_cleanup(struct netpoll *np)
1286 {
1287 	rtnl_lock();
1288 	if (!np->dev)
1289 		goto out;
1290 	__netpoll_cleanup(np);
1291 	dev_put(np->dev);
1292 	np->dev = NULL;
1293 out:
1294 	rtnl_unlock();
1295 }
1296 EXPORT_SYMBOL(netpoll_cleanup);
1297 
1298 int netpoll_trap(void)
1299 {
1300 	return atomic_read(&trapped);
1301 }
1302 EXPORT_SYMBOL(netpoll_trap);
1303 
1304 void netpoll_set_trap(int trap)
1305 {
1306 	if (trap)
1307 		atomic_inc(&trapped);
1308 	else
1309 		atomic_dec(&trapped);
1310 }
1311 EXPORT_SYMBOL(netpoll_set_trap);
1312