2 * Common framework for low-level network console, dump, and debugger code
4 * Sep 8 2003 Matt Mackall <mpm@selenic.com>
6 * based on the netconsole code from:
8 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2002 Red Hat, Inc.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
40 * We maintain a small pool of fully-sized skbs, to make sure the
41 * message gets out even in extreme OOM situations.
44 #define MAX_UDP_CHUNK 1460
47 static struct sk_buff_head skb_pool;
49 static atomic_t trapped;
51 DEFINE_STATIC_SRCU(netpoll_srcu);
53 #define USEC_PER_POLL 50
55 #define MAX_SKB_SIZE \
56 (sizeof(struct ethhdr) + \
57 sizeof(struct iphdr) + \
58 sizeof(struct udphdr) + \
61 static void zap_completion_queue(void);
62 static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
63 static void netpoll_async_cleanup(struct work_struct *work);
65 static unsigned int carrier_timeout = 4;
66 module_param(carrier_timeout, uint, 0644);
68 #define np_info(np, fmt, ...) \
69 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
70 #define np_err(np, fmt, ...) \
71 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
72 #define np_notice(np, fmt, ...) \
73 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
75 static void queue_process(struct work_struct *work)
77 struct netpoll_info *npinfo =
78 container_of(work, struct netpoll_info, tx_work.work);
82 while ((skb = skb_dequeue(&npinfo->txq))) {
83 struct net_device *dev = skb->dev;
84 const struct net_device_ops *ops = dev->netdev_ops;
85 struct netdev_queue *txq;
87 if (!netif_device_present(dev) || !netif_running(dev)) {
92 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
94 local_irq_save(flags);
95 __netif_tx_lock(txq, smp_processor_id());
96 if (netif_xmit_frozen_or_stopped(txq) ||
97 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
98 skb_queue_head(&npinfo->txq, skb);
99 __netif_tx_unlock(txq);
100 local_irq_restore(flags);
102 schedule_delayed_work(&npinfo->tx_work, HZ/10);
105 __netif_tx_unlock(txq);
106 local_irq_restore(flags);
110 static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
111 unsigned short ulen, __be32 saddr, __be32 daddr)
115 if (uh->check == 0 || skb_csum_unnecessary(skb))
118 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
120 if (skb->ip_summed == CHECKSUM_COMPLETE &&
121 !csum_fold(csum_add(psum, skb->csum)))
126 return __skb_checksum_complete(skb);
130 * Check whether delayed processing was scheduled for our NIC. If so,
131 * we attempt to grab the poll lock and use ->poll() to pump the card.
132 * If this fails, either we've recursed in ->poll() or it's already
133 * running on another CPU.
135 * Note: we don't mask interrupts with this lock because we're using
136 * trylock here and interrupts are already disabled in the softirq
137 * case. Further, we test the poll_owner to avoid recursion on UP
138 * systems where the lock doesn't exist.
140 * In cases where there is bi-directional communications, reading only
141 * one message at a time can lead to packets being dropped by the
142 * network adapter, forcing superfluous retries and possibly timeouts.
143 * Thus, we set our budget to greater than 1.
145 static int poll_one_napi(struct napi_struct *napi, int budget)
149 /* net_rx_action's ->poll() invocations and our's are
150 * synchronized by this test which is only made while
151 * holding the napi->poll_lock.
153 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
156 set_bit(NAPI_STATE_NPSVC, &napi->state);
158 work = napi->poll(napi, budget);
159 WARN_ONCE(work > budget, "%pF exceeded budget in poll\n", napi->poll);
160 trace_napi_poll(napi);
162 clear_bit(NAPI_STATE_NPSVC, &napi->state);
164 return budget - work;
167 static void poll_napi(struct net_device *dev, int budget)
169 struct napi_struct *napi;
171 list_for_each_entry(napi, &dev->napi_list, dev_list) {
172 if (napi->poll_owner != smp_processor_id() &&
173 spin_trylock(&napi->poll_lock)) {
174 budget = poll_one_napi(napi, budget);
175 spin_unlock(&napi->poll_lock);
180 static void service_neigh_queue(struct netpoll_info *npi)
185 while ((skb = skb_dequeue(&npi->neigh_tx)))
186 netpoll_neigh_reply(skb, npi);
190 static void netpoll_poll_dev(struct net_device *dev)
192 const struct net_device_ops *ops;
193 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
194 bool rx_processing = netpoll_rx_processing(ni);
195 int budget = rx_processing? 16 : 0;
197 /* Don't do any rx activity if the dev_lock mutex is held
198 * the dev_open/close paths use this to block netpoll activity
199 * while changing device state
201 if (down_trylock(&ni->dev_lock))
204 if (!netif_running(dev)) {
210 atomic_inc(&trapped);
212 ops = dev->netdev_ops;
213 if (!ops->ndo_poll_controller) {
218 /* Process pending work on NIC */
219 ops->ndo_poll_controller(dev);
221 poll_napi(dev, budget);
224 atomic_dec(&trapped);
228 if (dev->flags & IFF_SLAVE) {
230 struct net_device *bond_dev;
232 struct netpoll_info *bond_ni;
234 bond_dev = netdev_master_upper_dev_get_rcu(dev);
235 bond_ni = rcu_dereference_bh(bond_dev->npinfo);
236 while ((skb = skb_dequeue(&ni->neigh_tx))) {
238 skb_queue_tail(&bond_ni->neigh_tx, skb);
243 service_neigh_queue(ni);
245 zap_completion_queue();
248 void netpoll_rx_disable(struct net_device *dev)
250 struct netpoll_info *ni;
253 idx = srcu_read_lock(&netpoll_srcu);
254 ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
257 srcu_read_unlock(&netpoll_srcu, idx);
259 EXPORT_SYMBOL(netpoll_rx_disable);
261 void netpoll_rx_enable(struct net_device *dev)
263 struct netpoll_info *ni;
265 ni = rcu_dereference(dev->npinfo);
270 EXPORT_SYMBOL(netpoll_rx_enable);
272 static void refill_skbs(void)
277 spin_lock_irqsave(&skb_pool.lock, flags);
278 while (skb_pool.qlen < MAX_SKBS) {
279 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
283 __skb_queue_tail(&skb_pool, skb);
285 spin_unlock_irqrestore(&skb_pool.lock, flags);
288 static void zap_completion_queue(void)
291 struct softnet_data *sd = &get_cpu_var(softnet_data);
293 if (sd->completion_queue) {
294 struct sk_buff *clist;
296 local_irq_save(flags);
297 clist = sd->completion_queue;
298 sd->completion_queue = NULL;
299 local_irq_restore(flags);
301 while (clist != NULL) {
302 struct sk_buff *skb = clist;
304 if (skb->destructor) {
305 atomic_inc(&skb->users);
306 dev_kfree_skb_any(skb); /* put this one back */
313 put_cpu_var(softnet_data);
316 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
321 zap_completion_queue();
325 skb = alloc_skb(len, GFP_ATOMIC);
327 skb = skb_dequeue(&skb_pool);
331 netpoll_poll_dev(np->dev);
337 atomic_set(&skb->users, 1);
338 skb_reserve(skb, reserve);
342 static int netpoll_owner_active(struct net_device *dev)
344 struct napi_struct *napi;
346 list_for_each_entry(napi, &dev->napi_list, dev_list) {
347 if (napi->poll_owner == smp_processor_id())
353 /* call with IRQ disabled */
354 void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
355 struct net_device *dev)
357 int status = NETDEV_TX_BUSY;
359 const struct net_device_ops *ops = dev->netdev_ops;
360 /* It is up to the caller to keep npinfo alive. */
361 struct netpoll_info *npinfo;
363 WARN_ON_ONCE(!irqs_disabled());
365 npinfo = rcu_dereference_bh(np->dev->npinfo);
366 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
371 /* don't get messages out of order, and no recursion */
372 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
373 struct netdev_queue *txq;
375 txq = netdev_pick_tx(dev, skb, NULL);
377 /* try until next clock tick */
378 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
379 tries > 0; --tries) {
380 if (__netif_tx_trylock(txq)) {
381 if (!netif_xmit_stopped(txq)) {
382 if (vlan_tx_tag_present(skb) &&
383 !vlan_hw_offload_capable(netif_skb_features(skb),
385 skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
386 if (unlikely(!skb)) {
387 /* This is actually a packet drop, but we
388 * don't want the code at the end of this
389 * function to try and re-queue a NULL skb.
391 status = NETDEV_TX_OK;
397 status = ops->ndo_start_xmit(skb, dev);
398 if (status == NETDEV_TX_OK)
399 txq_trans_update(txq);
402 __netif_tx_unlock(txq);
404 if (status == NETDEV_TX_OK)
409 /* tickle device maybe there is some cleanup */
410 netpoll_poll_dev(np->dev);
412 udelay(USEC_PER_POLL);
415 WARN_ONCE(!irqs_disabled(),
416 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
417 dev->name, ops->ndo_start_xmit);
421 if (status != NETDEV_TX_OK) {
422 skb_queue_tail(&npinfo->txq, skb);
423 schedule_delayed_work(&npinfo->tx_work,0);
426 EXPORT_SYMBOL(netpoll_send_skb_on_dev);
428 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
430 int total_len, ip_len, udp_len;
435 static atomic_t ip_ident;
436 struct ipv6hdr *ip6h;
438 udp_len = len + sizeof(*udph);
440 ip_len = udp_len + sizeof(*ip6h);
442 ip_len = udp_len + sizeof(*iph);
444 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
446 skb = find_skb(np, total_len + np->dev->needed_tailroom,
451 skb_copy_to_linear_data(skb, msg, len);
454 skb_push(skb, sizeof(*udph));
455 skb_reset_transport_header(skb);
457 udph->source = htons(np->local_port);
458 udph->dest = htons(np->remote_port);
459 udph->len = htons(udp_len);
463 udph->check = csum_ipv6_magic(&np->local_ip.in6,
465 udp_len, IPPROTO_UDP,
466 csum_partial(udph, udp_len, 0));
467 if (udph->check == 0)
468 udph->check = CSUM_MANGLED_0;
470 skb_push(skb, sizeof(*ip6h));
471 skb_reset_network_header(skb);
472 ip6h = ipv6_hdr(skb);
474 /* ip6h->version = 6; ip6h->priority = 0; */
475 put_unaligned(0x60, (unsigned char *)ip6h);
476 ip6h->flow_lbl[0] = 0;
477 ip6h->flow_lbl[1] = 0;
478 ip6h->flow_lbl[2] = 0;
480 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
481 ip6h->nexthdr = IPPROTO_UDP;
482 ip6h->hop_limit = 32;
483 ip6h->saddr = np->local_ip.in6;
484 ip6h->daddr = np->remote_ip.in6;
486 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
487 skb_reset_mac_header(skb);
488 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
491 udph->check = csum_tcpudp_magic(np->local_ip.ip,
493 udp_len, IPPROTO_UDP,
494 csum_partial(udph, udp_len, 0));
495 if (udph->check == 0)
496 udph->check = CSUM_MANGLED_0;
498 skb_push(skb, sizeof(*iph));
499 skb_reset_network_header(skb);
502 /* iph->version = 4; iph->ihl = 5; */
503 put_unaligned(0x45, (unsigned char *)iph);
505 put_unaligned(htons(ip_len), &(iph->tot_len));
506 iph->id = htons(atomic_inc_return(&ip_ident));
509 iph->protocol = IPPROTO_UDP;
511 put_unaligned(np->local_ip.ip, &(iph->saddr));
512 put_unaligned(np->remote_ip.ip, &(iph->daddr));
513 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
515 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
516 skb_reset_mac_header(skb);
517 skb->protocol = eth->h_proto = htons(ETH_P_IP);
520 ether_addr_copy(eth->h_source, np->dev->dev_addr);
521 ether_addr_copy(eth->h_dest, np->remote_mac);
525 netpoll_send_skb(np, skb);
527 EXPORT_SYMBOL(netpoll_send_udp);
529 static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
531 int size, type = ARPOP_REPLY;
534 struct sk_buff *send_skb;
535 struct netpoll *np, *tmp;
540 if (!netpoll_rx_processing(npinfo))
543 /* Before checking the packet, we do some early
544 inspection whether this is interesting at all */
545 spin_lock_irqsave(&npinfo->rx_lock, flags);
546 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
547 if (np->dev == skb->dev)
550 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
552 /* No netpoll struct is using this dev */
556 proto = ntohs(eth_hdr(skb)->h_proto);
557 if (proto == ETH_P_ARP) {
559 unsigned char *arp_ptr;
560 /* No arp on this interface */
561 if (skb->dev->flags & IFF_NOARP)
564 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
567 skb_reset_network_header(skb);
568 skb_reset_transport_header(skb);
571 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
572 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
573 arp->ar_pro != htons(ETH_P_IP) ||
574 arp->ar_op != htons(ARPOP_REQUEST))
577 arp_ptr = (unsigned char *)(arp+1);
578 /* save the location of the src hw addr */
580 arp_ptr += skb->dev->addr_len;
581 memcpy(&sip, arp_ptr, 4);
583 /* If we actually cared about dst hw addr,
584 it would get copied here */
585 arp_ptr += skb->dev->addr_len;
586 memcpy(&tip, arp_ptr, 4);
588 /* Should we ignore arp? */
589 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
592 size = arp_hdr_len(skb->dev);
594 spin_lock_irqsave(&npinfo->rx_lock, flags);
595 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
596 if (tip != np->local_ip.ip)
599 hlen = LL_RESERVED_SPACE(np->dev);
600 tlen = np->dev->needed_tailroom;
601 send_skb = find_skb(np, size + hlen + tlen, hlen);
605 skb_reset_network_header(send_skb);
606 arp = (struct arphdr *) skb_put(send_skb, size);
607 send_skb->dev = skb->dev;
608 send_skb->protocol = htons(ETH_P_ARP);
610 /* Fill the device header for the ARP frame */
611 if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
612 sha, np->dev->dev_addr,
613 send_skb->len) < 0) {
619 * Fill out the arp protocol part.
621 * we only support ethernet device type,
622 * which (according to RFC 1390) should
623 * always equal 1 (Ethernet).
626 arp->ar_hrd = htons(np->dev->type);
627 arp->ar_pro = htons(ETH_P_IP);
628 arp->ar_hln = np->dev->addr_len;
630 arp->ar_op = htons(type);
632 arp_ptr = (unsigned char *)(arp + 1);
633 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
634 arp_ptr += np->dev->addr_len;
635 memcpy(arp_ptr, &tip, 4);
637 memcpy(arp_ptr, sha, np->dev->addr_len);
638 arp_ptr += np->dev->addr_len;
639 memcpy(arp_ptr, &sip, 4);
641 netpoll_send_skb(np, send_skb);
643 /* If there are several rx_skb_hooks for the same
644 * address we're fine by sending a single reply
648 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
649 } else if( proto == ETH_P_IPV6) {
650 #if IS_ENABLED(CONFIG_IPV6)
654 struct icmp6hdr *icmp6h;
655 const struct in6_addr *saddr;
656 const struct in6_addr *daddr;
657 struct inet6_dev *in6_dev = NULL;
658 struct in6_addr *target;
660 in6_dev = in6_dev_get(skb->dev);
661 if (!in6_dev || !in6_dev->cnf.accept_ra)
664 if (!pskb_may_pull(skb, skb->len))
667 msg = (struct nd_msg *)skb_transport_header(skb);
669 __skb_push(skb, skb->data - skb_transport_header(skb));
671 if (ipv6_hdr(skb)->hop_limit != 255)
673 if (msg->icmph.icmp6_code != 0)
675 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
678 saddr = &ipv6_hdr(skb)->saddr;
679 daddr = &ipv6_hdr(skb)->daddr;
681 size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
683 spin_lock_irqsave(&npinfo->rx_lock, flags);
684 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
685 if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
688 hlen = LL_RESERVED_SPACE(np->dev);
689 tlen = np->dev->needed_tailroom;
690 send_skb = find_skb(np, size + hlen + tlen, hlen);
694 send_skb->protocol = htons(ETH_P_IPV6);
695 send_skb->dev = skb->dev;
697 skb_reset_network_header(send_skb);
698 hdr = (struct ipv6hdr *) skb_put(send_skb, sizeof(struct ipv6hdr));
699 *(__be32*)hdr = htonl(0x60000000);
700 hdr->payload_len = htons(size);
701 hdr->nexthdr = IPPROTO_ICMPV6;
702 hdr->hop_limit = 255;
706 icmp6h = (struct icmp6hdr *) skb_put(send_skb, sizeof(struct icmp6hdr));
707 icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
708 icmp6h->icmp6_router = 0;
709 icmp6h->icmp6_solicited = 1;
711 target = (struct in6_addr *) skb_put(send_skb, sizeof(struct in6_addr));
712 *target = msg->target;
713 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
718 if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
719 lladdr, np->dev->dev_addr,
720 send_skb->len) < 0) {
725 netpoll_send_skb(np, send_skb);
727 /* If there are several rx_skb_hooks for the same
728 * address, we're fine by sending a single reply
732 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
737 static bool pkt_is_ns(struct sk_buff *skb)
742 if (skb->protocol != htons(ETH_P_ARP))
744 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
747 msg = (struct nd_msg *)skb_transport_header(skb);
748 __skb_push(skb, skb->data - skb_transport_header(skb));
751 if (hdr->nexthdr != IPPROTO_ICMPV6)
753 if (hdr->hop_limit != 255)
755 if (msg->icmph.icmp6_code != 0)
757 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
763 int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
765 int proto, len, ulen, data_len;
766 int hits = 0, offset;
767 const struct iphdr *iph;
769 struct netpoll *np, *tmp;
772 if (!netpoll_rx_processing(npinfo))
775 if (skb->dev->type != ARPHRD_ETHER)
778 /* check if netpoll clients need ARP */
779 if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
780 skb_queue_tail(&npinfo->neigh_tx, skb);
782 } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
783 skb_queue_tail(&npinfo->neigh_tx, skb);
787 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
788 skb = vlan_untag(skb);
793 proto = ntohs(eth_hdr(skb)->h_proto);
794 if (proto != ETH_P_IP && proto != ETH_P_IPV6)
796 if (skb->pkt_type == PACKET_OTHERHOST)
801 if (proto == ETH_P_IP) {
802 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
804 iph = (struct iphdr *)skb->data;
805 if (iph->ihl < 5 || iph->version != 4)
807 if (!pskb_may_pull(skb, iph->ihl*4))
809 iph = (struct iphdr *)skb->data;
810 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
813 len = ntohs(iph->tot_len);
814 if (skb->len < len || len < iph->ihl*4)
818 * Our transport medium may have padded the buffer out.
819 * Now We trim to the true length of the frame.
821 if (pskb_trim_rcsum(skb, len))
824 iph = (struct iphdr *)skb->data;
825 if (iph->protocol != IPPROTO_UDP)
829 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
830 offset = (unsigned char *)(uh + 1) - skb->data;
831 ulen = ntohs(uh->len);
832 data_len = skb->len - offset;
833 source = ntohs(uh->source);
837 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
839 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
840 if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
842 if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
844 if (np->local_port && np->local_port != ntohs(uh->dest))
847 np->rx_skb_hook(np, source, skb, offset, data_len);
851 #if IS_ENABLED(CONFIG_IPV6)
852 const struct ipv6hdr *ip6h;
854 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
856 ip6h = (struct ipv6hdr *)skb->data;
857 if (ip6h->version != 6)
859 len = ntohs(ip6h->payload_len);
862 if (len + sizeof(struct ipv6hdr) > skb->len)
864 if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
866 ip6h = ipv6_hdr(skb);
867 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
870 offset = (unsigned char *)(uh + 1) - skb->data;
871 ulen = ntohs(uh->len);
872 data_len = skb->len - offset;
873 source = ntohs(uh->source);
874 if (ulen != skb->len)
876 if (udp6_csum_init(skb, uh, IPPROTO_UDP))
878 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
879 if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
881 if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
883 if (np->local_port && np->local_port != ntohs(uh->dest))
886 np->rx_skb_hook(np, source, skb, offset, data_len);
899 if (atomic_read(&trapped)) {
907 void netpoll_print_options(struct netpoll *np)
909 np_info(np, "local port %d\n", np->local_port);
911 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
913 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
914 np_info(np, "interface '%s'\n", np->dev_name);
915 np_info(np, "remote port %d\n", np->remote_port);
917 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
919 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
920 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
922 EXPORT_SYMBOL(netpoll_print_options);
924 static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
928 if (!strchr(str, ':') &&
929 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
933 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
934 #if IS_ENABLED(CONFIG_IPV6)
944 int netpoll_parse_options(struct netpoll *np, char *opt)
946 char *cur=opt, *delim;
948 bool ipversion_set = false;
951 if ((delim = strchr(cur, '@')) == NULL)
954 if (kstrtou16(cur, 10, &np->local_port))
961 ipversion_set = true;
962 if ((delim = strchr(cur, '/')) == NULL)
965 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
969 np->ipv6 = (bool)ipv6;
975 /* parse out dev name */
976 if ((delim = strchr(cur, ',')) == NULL)
979 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
986 if ((delim = strchr(cur, '@')) == NULL)
989 if (*cur == ' ' || *cur == '\t')
990 np_info(np, "warning: whitespace is not allowed\n");
991 if (kstrtou16(cur, 10, &np->remote_port))
998 if ((delim = strchr(cur, '/')) == NULL)
1001 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
1004 else if (ipversion_set && np->ipv6 != (bool)ipv6)
1007 np->ipv6 = (bool)ipv6;
1012 if (!mac_pton(cur, np->remote_mac))
1016 netpoll_print_options(np);
1021 np_info(np, "couldn't parse config at '%s'!\n", cur);
1024 EXPORT_SYMBOL(netpoll_parse_options);
1026 int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
1028 struct netpoll_info *npinfo;
1029 const struct net_device_ops *ops;
1030 unsigned long flags;
1034 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
1035 INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
1037 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
1038 !ndev->netdev_ops->ndo_poll_controller) {
1039 np_err(np, "%s doesn't support polling, aborting\n",
1045 if (!ndev->npinfo) {
1046 npinfo = kmalloc(sizeof(*npinfo), gfp);
1052 INIT_LIST_HEAD(&npinfo->rx_np);
1054 spin_lock_init(&npinfo->rx_lock);
1055 sema_init(&npinfo->dev_lock, 1);
1056 skb_queue_head_init(&npinfo->neigh_tx);
1057 skb_queue_head_init(&npinfo->txq);
1058 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
1060 atomic_set(&npinfo->refcnt, 1);
1062 ops = np->dev->netdev_ops;
1063 if (ops->ndo_netpoll_setup) {
1064 err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
1069 npinfo = rtnl_dereference(ndev->npinfo);
1070 atomic_inc(&npinfo->refcnt);
1073 npinfo->netpoll = np;
1075 if (np->rx_skb_hook) {
1076 spin_lock_irqsave(&npinfo->rx_lock, flags);
1077 list_add_tail(&np->rx, &npinfo->rx_np);
1078 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1081 /* last thing to do is link it to the net device structure */
1082 rcu_assign_pointer(ndev->npinfo, npinfo);
1091 EXPORT_SYMBOL_GPL(__netpoll_setup);
1093 int netpoll_setup(struct netpoll *np)
1095 struct net_device *ndev = NULL;
1096 struct in_device *in_dev;
1101 struct net *net = current->nsproxy->net_ns;
1102 ndev = __dev_get_by_name(net, np->dev_name);
1105 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
1111 if (netdev_master_upper_dev_get(ndev)) {
1112 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
1117 if (!netif_running(ndev)) {
1118 unsigned long atmost, atleast;
1120 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
1122 err = dev_open(ndev);
1125 np_err(np, "failed to open %s\n", ndev->name);
1130 atleast = jiffies + HZ/10;
1131 atmost = jiffies + carrier_timeout * HZ;
1132 while (!netif_carrier_ok(ndev)) {
1133 if (time_after(jiffies, atmost)) {
1134 np_notice(np, "timeout waiting for carrier\n");
1140 /* If carrier appears to come up instantly, we don't
1141 * trust it and pause so that we don't pump all our
1142 * queued console messages into the bitbucket.
1145 if (time_before(jiffies, atleast)) {
1146 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
1152 if (!np->local_ip.ip) {
1154 in_dev = __in_dev_get_rtnl(ndev);
1156 if (!in_dev || !in_dev->ifa_list) {
1157 np_err(np, "no IP address for %s, aborting\n",
1159 err = -EDESTADDRREQ;
1163 np->local_ip.ip = in_dev->ifa_list->ifa_local;
1164 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
1166 #if IS_ENABLED(CONFIG_IPV6)
1167 struct inet6_dev *idev;
1169 err = -EDESTADDRREQ;
1170 idev = __in6_dev_get(ndev);
1172 struct inet6_ifaddr *ifp;
1174 read_lock_bh(&idev->lock);
1175 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1176 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
1178 np->local_ip.in6 = ifp->addr;
1182 read_unlock_bh(&idev->lock);
1185 np_err(np, "no IPv6 address for %s, aborting\n",
1189 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
1191 np_err(np, "IPv6 is not supported %s, aborting\n",
1199 /* fill up the skb queue */
1202 err = __netpoll_setup(np, ndev, GFP_KERNEL);
1215 EXPORT_SYMBOL(netpoll_setup);
1217 static int __init netpoll_init(void)
1219 skb_queue_head_init(&skb_pool);
1222 core_initcall(netpoll_init);
1224 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
1226 struct netpoll_info *npinfo =
1227 container_of(rcu_head, struct netpoll_info, rcu);
1229 skb_queue_purge(&npinfo->neigh_tx);
1230 skb_queue_purge(&npinfo->txq);
1232 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
1233 cancel_delayed_work(&npinfo->tx_work);
1235 /* clean after last, unfinished work */
1236 __skb_queue_purge(&npinfo->txq);
1237 /* now cancel it again */
1238 cancel_delayed_work(&npinfo->tx_work);
1242 void __netpoll_cleanup(struct netpoll *np)
1244 struct netpoll_info *npinfo;
1245 unsigned long flags;
1247 /* rtnl_dereference would be preferable here but
1248 * rcu_cleanup_netpoll path can put us in here safely without
1249 * holding the rtnl, so plain rcu_dereference it is
1251 npinfo = rtnl_dereference(np->dev->npinfo);
1255 if (!list_empty(&npinfo->rx_np)) {
1256 spin_lock_irqsave(&npinfo->rx_lock, flags);
1258 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1261 synchronize_srcu(&netpoll_srcu);
1263 if (atomic_dec_and_test(&npinfo->refcnt)) {
1264 const struct net_device_ops *ops;
1266 ops = np->dev->netdev_ops;
1267 if (ops->ndo_netpoll_cleanup)
1268 ops->ndo_netpoll_cleanup(np->dev);
1270 rcu_assign_pointer(np->dev->npinfo, NULL);
1271 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
1274 EXPORT_SYMBOL_GPL(__netpoll_cleanup);
1276 static void netpoll_async_cleanup(struct work_struct *work)
1278 struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
1281 __netpoll_cleanup(np);
1286 void __netpoll_free_async(struct netpoll *np)
1288 schedule_work(&np->cleanup_work);
1290 EXPORT_SYMBOL_GPL(__netpoll_free_async);
1292 void netpoll_cleanup(struct netpoll *np)
1297 __netpoll_cleanup(np);
1303 EXPORT_SYMBOL(netpoll_cleanup);
1305 int netpoll_trap(void)
1307 return atomic_read(&trapped);
1309 EXPORT_SYMBOL(netpoll_trap);
1311 void netpoll_set_trap(int trap)
1314 atomic_inc(&trapped);
1316 atomic_dec(&trapped);
1318 EXPORT_SYMBOL(netpoll_set_trap);