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/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/string.h>
18 #include <linux/if_arp.h>
19 #include <linux/inetdevice.h>
20 #include <linux/inet.h>
21 #include <linux/interrupt.h>
22 #include <linux/netpoll.h>
23 #include <linux/sched.h>
24 #include <linux/delay.h>
25 #include <linux/rcupdate.h>
26 #include <linux/workqueue.h>
27 #include <linux/slab.h>
28 #include <linux/export.h>
29 #include <linux/if_vlan.h>
32 #include <net/addrconf.h>
33 #include <net/ndisc.h>
34 #include <net/ip6_checksum.h>
35 #include <asm/unaligned.h>
36 #include <trace/events/napi.h>
39 * We maintain a small pool of fully-sized skbs, to make sure the
40 * message gets out even in extreme OOM situations.
43 #define MAX_UDP_CHUNK 1460
46 static struct sk_buff_head skb_pool
;
48 static atomic_t trapped
;
50 DEFINE_STATIC_SRCU(netpoll_srcu
);
52 #define USEC_PER_POLL 50
53 #define NETPOLL_RX_ENABLED 1
54 #define NETPOLL_RX_DROP 2
56 #define MAX_SKB_SIZE \
57 (sizeof(struct ethhdr) + \
58 sizeof(struct iphdr) + \
59 sizeof(struct udphdr) + \
62 static void zap_completion_queue(void);
63 static void netpoll_neigh_reply(struct sk_buff
*skb
, struct netpoll_info
*npinfo
);
64 static void netpoll_async_cleanup(struct work_struct
*work
);
66 static unsigned int carrier_timeout
= 4;
67 module_param(carrier_timeout
, uint
, 0644);
69 #define np_info(np, fmt, ...) \
70 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
71 #define np_err(np, fmt, ...) \
72 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
73 #define np_notice(np, fmt, ...) \
74 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
76 static void queue_process(struct work_struct
*work
)
78 struct netpoll_info
*npinfo
=
79 container_of(work
, struct netpoll_info
, tx_work
.work
);
83 while ((skb
= skb_dequeue(&npinfo
->txq
))) {
84 struct net_device
*dev
= skb
->dev
;
85 const struct net_device_ops
*ops
= dev
->netdev_ops
;
86 struct netdev_queue
*txq
;
88 if (!netif_device_present(dev
) || !netif_running(dev
)) {
93 txq
= netdev_get_tx_queue(dev
, skb_get_queue_mapping(skb
));
95 local_irq_save(flags
);
96 __netif_tx_lock(txq
, smp_processor_id());
97 if (netif_xmit_frozen_or_stopped(txq
) ||
98 ops
->ndo_start_xmit(skb
, dev
) != NETDEV_TX_OK
) {
99 skb_queue_head(&npinfo
->txq
, skb
);
100 __netif_tx_unlock(txq
);
101 local_irq_restore(flags
);
103 schedule_delayed_work(&npinfo
->tx_work
, HZ
/10);
106 __netif_tx_unlock(txq
);
107 local_irq_restore(flags
);
111 static __sum16
checksum_udp(struct sk_buff
*skb
, struct udphdr
*uh
,
112 unsigned short ulen
, __be32 saddr
, __be32 daddr
)
116 if (uh
->check
== 0 || skb_csum_unnecessary(skb
))
119 psum
= csum_tcpudp_nofold(saddr
, daddr
, ulen
, IPPROTO_UDP
, 0);
121 if (skb
->ip_summed
== CHECKSUM_COMPLETE
&&
122 !csum_fold(csum_add(psum
, skb
->csum
)))
127 return __skb_checksum_complete(skb
);
131 * Check whether delayed processing was scheduled for our NIC. If so,
132 * we attempt to grab the poll lock and use ->poll() to pump the card.
133 * If this fails, either we've recursed in ->poll() or it's already
134 * running on another CPU.
136 * Note: we don't mask interrupts with this lock because we're using
137 * trylock here and interrupts are already disabled in the softirq
138 * case. Further, we test the poll_owner to avoid recursion on UP
139 * systems where the lock doesn't exist.
141 * In cases where there is bi-directional communications, reading only
142 * one message at a time can lead to packets being dropped by the
143 * network adapter, forcing superfluous retries and possibly timeouts.
144 * Thus, we set our budget to greater than 1.
146 static int poll_one_napi(struct netpoll_info
*npinfo
,
147 struct napi_struct
*napi
, int budget
)
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.
155 if (!test_bit(NAPI_STATE_SCHED
, &napi
->state
))
158 npinfo
->rx_flags
|= NETPOLL_RX_DROP
;
159 atomic_inc(&trapped
);
160 set_bit(NAPI_STATE_NPSVC
, &napi
->state
);
162 work
= napi
->poll(napi
, budget
);
163 trace_napi_poll(napi
);
165 clear_bit(NAPI_STATE_NPSVC
, &napi
->state
);
166 atomic_dec(&trapped
);
167 npinfo
->rx_flags
&= ~NETPOLL_RX_DROP
;
169 return budget
- work
;
172 static void poll_napi(struct net_device
*dev
)
174 struct napi_struct
*napi
;
177 list_for_each_entry(napi
, &dev
->napi_list
, dev_list
) {
178 if (napi
->poll_owner
!= smp_processor_id() &&
179 spin_trylock(&napi
->poll_lock
)) {
180 budget
= poll_one_napi(rcu_dereference_bh(dev
->npinfo
),
182 spin_unlock(&napi
->poll_lock
);
190 static void service_neigh_queue(struct netpoll_info
*npi
)
195 while ((skb
= skb_dequeue(&npi
->neigh_tx
)))
196 netpoll_neigh_reply(skb
, npi
);
200 static void netpoll_poll_dev(struct net_device
*dev
)
202 const struct net_device_ops
*ops
;
203 struct netpoll_info
*ni
= rcu_dereference_bh(dev
->npinfo
);
205 /* Don't do any rx activity if the dev_lock mutex is held
206 * the dev_open/close paths use this to block netpoll activity
207 * while changing device state
209 if (!mutex_trylock(&ni
->dev_lock
))
212 if (!netif_running(dev
)) {
213 mutex_unlock(&ni
->dev_lock
);
217 ops
= dev
->netdev_ops
;
218 if (!ops
->ndo_poll_controller
) {
219 mutex_unlock(&ni
->dev_lock
);
223 /* Process pending work on NIC */
224 ops
->ndo_poll_controller(dev
);
228 mutex_unlock(&ni
->dev_lock
);
230 if (dev
->flags
& IFF_SLAVE
) {
232 struct net_device
*bond_dev
;
234 struct netpoll_info
*bond_ni
;
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
))) {
240 skb_queue_tail(&bond_ni
->neigh_tx
, skb
);
245 service_neigh_queue(ni
);
247 zap_completion_queue();
250 int netpoll_rx_disable(struct net_device
*dev
)
252 struct netpoll_info
*ni
;
255 idx
= srcu_read_lock(&netpoll_srcu
);
256 ni
= srcu_dereference(dev
->npinfo
, &netpoll_srcu
);
258 mutex_lock(&ni
->dev_lock
);
259 srcu_read_unlock(&netpoll_srcu
, idx
);
262 EXPORT_SYMBOL(netpoll_rx_disable
);
264 void netpoll_rx_enable(struct net_device
*dev
)
266 struct netpoll_info
*ni
;
268 ni
= rcu_dereference(dev
->npinfo
);
270 mutex_unlock(&ni
->dev_lock
);
273 EXPORT_SYMBOL(netpoll_rx_enable
);
275 static void refill_skbs(void)
280 spin_lock_irqsave(&skb_pool
.lock
, flags
);
281 while (skb_pool
.qlen
< MAX_SKBS
) {
282 skb
= alloc_skb(MAX_SKB_SIZE
, GFP_ATOMIC
);
286 __skb_queue_tail(&skb_pool
, skb
);
288 spin_unlock_irqrestore(&skb_pool
.lock
, flags
);
291 static void zap_completion_queue(void)
294 struct softnet_data
*sd
= &get_cpu_var(softnet_data
);
296 if (sd
->completion_queue
) {
297 struct sk_buff
*clist
;
299 local_irq_save(flags
);
300 clist
= sd
->completion_queue
;
301 sd
->completion_queue
= NULL
;
302 local_irq_restore(flags
);
304 while (clist
!= NULL
) {
305 struct sk_buff
*skb
= clist
;
307 if (skb
->destructor
) {
308 atomic_inc(&skb
->users
);
309 dev_kfree_skb_any(skb
); /* put this one back */
316 put_cpu_var(softnet_data
);
319 static struct sk_buff
*find_skb(struct netpoll
*np
, int len
, int reserve
)
324 zap_completion_queue();
328 skb
= alloc_skb(len
, GFP_ATOMIC
);
330 skb
= skb_dequeue(&skb_pool
);
334 netpoll_poll_dev(np
->dev
);
340 atomic_set(&skb
->users
, 1);
341 skb_reserve(skb
, reserve
);
345 static int netpoll_owner_active(struct net_device
*dev
)
347 struct napi_struct
*napi
;
349 list_for_each_entry(napi
, &dev
->napi_list
, dev_list
) {
350 if (napi
->poll_owner
== smp_processor_id())
356 /* call with IRQ disabled */
357 void netpoll_send_skb_on_dev(struct netpoll
*np
, struct sk_buff
*skb
,
358 struct net_device
*dev
)
360 int status
= NETDEV_TX_BUSY
;
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
;
366 WARN_ON_ONCE(!irqs_disabled());
368 npinfo
= rcu_dereference_bh(np
->dev
->npinfo
);
369 if (!npinfo
|| !netif_running(dev
) || !netif_device_present(dev
)) {
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
;
378 txq
= netdev_pick_tx(dev
, skb
);
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 !(netif_skb_features(skb
) & NETIF_F_HW_VLAN_CTAG_TX
)) {
387 skb
= __vlan_put_tag(skb
, vlan_tx_tag_get(skb
));
393 status
= ops
->ndo_start_xmit(skb
, dev
);
394 if (status
== NETDEV_TX_OK
)
395 txq_trans_update(txq
);
397 __netif_tx_unlock(txq
);
399 if (status
== NETDEV_TX_OK
)
404 /* tickle device maybe there is some cleanup */
405 netpoll_poll_dev(np
->dev
);
407 udelay(USEC_PER_POLL
);
410 WARN_ONCE(!irqs_disabled(),
411 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
412 dev
->name
, ops
->ndo_start_xmit
);
416 if (status
!= NETDEV_TX_OK
) {
417 skb_queue_tail(&npinfo
->txq
, skb
);
418 schedule_delayed_work(&npinfo
->tx_work
,0);
421 EXPORT_SYMBOL(netpoll_send_skb_on_dev
);
423 void netpoll_send_udp(struct netpoll
*np
, const char *msg
, int len
)
425 int total_len
, ip_len
, udp_len
;
430 static atomic_t ip_ident
;
431 struct ipv6hdr
*ip6h
;
433 udp_len
= len
+ sizeof(*udph
);
435 ip_len
= udp_len
+ sizeof(*ip6h
);
437 ip_len
= udp_len
+ sizeof(*iph
);
439 total_len
= ip_len
+ LL_RESERVED_SPACE(np
->dev
);
441 skb
= find_skb(np
, total_len
+ np
->dev
->needed_tailroom
,
446 skb_copy_to_linear_data(skb
, msg
, len
);
449 skb_push(skb
, sizeof(*udph
));
450 skb_reset_transport_header(skb
);
452 udph
->source
= htons(np
->local_port
);
453 udph
->dest
= htons(np
->remote_port
);
454 udph
->len
= htons(udp_len
);
458 udph
->check
= csum_ipv6_magic(&np
->local_ip
.in6
,
460 udp_len
, IPPROTO_UDP
,
461 csum_partial(udph
, udp_len
, 0));
462 if (udph
->check
== 0)
463 udph
->check
= CSUM_MANGLED_0
;
465 skb_push(skb
, sizeof(*ip6h
));
466 skb_reset_network_header(skb
);
467 ip6h
= ipv6_hdr(skb
);
469 /* ip6h->version = 6; ip6h->priority = 0; */
470 put_unaligned(0x60, (unsigned char *)ip6h
);
471 ip6h
->flow_lbl
[0] = 0;
472 ip6h
->flow_lbl
[1] = 0;
473 ip6h
->flow_lbl
[2] = 0;
475 ip6h
->payload_len
= htons(sizeof(struct udphdr
) + len
);
476 ip6h
->nexthdr
= IPPROTO_UDP
;
477 ip6h
->hop_limit
= 32;
478 ip6h
->saddr
= np
->local_ip
.in6
;
479 ip6h
->daddr
= np
->remote_ip
.in6
;
481 eth
= (struct ethhdr
*) skb_push(skb
, ETH_HLEN
);
482 skb_reset_mac_header(skb
);
483 skb
->protocol
= eth
->h_proto
= htons(ETH_P_IPV6
);
486 udph
->check
= csum_tcpudp_magic(np
->local_ip
.ip
,
488 udp_len
, IPPROTO_UDP
,
489 csum_partial(udph
, udp_len
, 0));
490 if (udph
->check
== 0)
491 udph
->check
= CSUM_MANGLED_0
;
493 skb_push(skb
, sizeof(*iph
));
494 skb_reset_network_header(skb
);
497 /* iph->version = 4; iph->ihl = 5; */
498 put_unaligned(0x45, (unsigned char *)iph
);
500 put_unaligned(htons(ip_len
), &(iph
->tot_len
));
501 iph
->id
= htons(atomic_inc_return(&ip_ident
));
504 iph
->protocol
= IPPROTO_UDP
;
506 put_unaligned(np
->local_ip
.ip
, &(iph
->saddr
));
507 put_unaligned(np
->remote_ip
.ip
, &(iph
->daddr
));
508 iph
->check
= ip_fast_csum((unsigned char *)iph
, iph
->ihl
);
510 eth
= (struct ethhdr
*) skb_push(skb
, ETH_HLEN
);
511 skb_reset_mac_header(skb
);
512 skb
->protocol
= eth
->h_proto
= htons(ETH_P_IP
);
515 memcpy(eth
->h_source
, np
->dev
->dev_addr
, ETH_ALEN
);
516 memcpy(eth
->h_dest
, np
->remote_mac
, ETH_ALEN
);
520 netpoll_send_skb(np
, skb
);
522 EXPORT_SYMBOL(netpoll_send_udp
);
524 static void netpoll_neigh_reply(struct sk_buff
*skb
, struct netpoll_info
*npinfo
)
526 int size
, type
= ARPOP_REPLY
;
529 struct sk_buff
*send_skb
;
530 struct netpoll
*np
, *tmp
;
535 if (list_empty(&npinfo
->rx_np
))
538 /* Before checking the packet, we do some early
539 inspection whether this is interesting at all */
540 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
541 list_for_each_entry_safe(np
, tmp
, &npinfo
->rx_np
, rx
) {
542 if (np
->dev
== skb
->dev
)
545 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
547 /* No netpoll struct is using this dev */
551 proto
= ntohs(eth_hdr(skb
)->h_proto
);
552 if (proto
== ETH_P_IP
) {
554 unsigned char *arp_ptr
;
555 /* No arp on this interface */
556 if (skb
->dev
->flags
& IFF_NOARP
)
559 if (!pskb_may_pull(skb
, arp_hdr_len(skb
->dev
)))
562 skb_reset_network_header(skb
);
563 skb_reset_transport_header(skb
);
566 if ((arp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
567 arp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
568 arp
->ar_pro
!= htons(ETH_P_IP
) ||
569 arp
->ar_op
!= htons(ARPOP_REQUEST
))
572 arp_ptr
= (unsigned char *)(arp
+1);
573 /* save the location of the src hw addr */
575 arp_ptr
+= skb
->dev
->addr_len
;
576 memcpy(&sip
, arp_ptr
, 4);
578 /* If we actually cared about dst hw addr,
579 it would get copied here */
580 arp_ptr
+= skb
->dev
->addr_len
;
581 memcpy(&tip
, arp_ptr
, 4);
583 /* Should we ignore arp? */
584 if (ipv4_is_loopback(tip
) || ipv4_is_multicast(tip
))
587 size
= arp_hdr_len(skb
->dev
);
589 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
590 list_for_each_entry_safe(np
, tmp
, &npinfo
->rx_np
, rx
) {
591 if (tip
!= np
->local_ip
.ip
)
594 hlen
= LL_RESERVED_SPACE(np
->dev
);
595 tlen
= np
->dev
->needed_tailroom
;
596 send_skb
= find_skb(np
, size
+ hlen
+ tlen
, hlen
);
600 skb_reset_network_header(send_skb
);
601 arp
= (struct arphdr
*) skb_put(send_skb
, size
);
602 send_skb
->dev
= skb
->dev
;
603 send_skb
->protocol
= htons(ETH_P_ARP
);
605 /* Fill the device header for the ARP frame */
606 if (dev_hard_header(send_skb
, skb
->dev
, ETH_P_ARP
,
607 sha
, np
->dev
->dev_addr
,
608 send_skb
->len
) < 0) {
614 * Fill out the arp protocol part.
616 * we only support ethernet device type,
617 * which (according to RFC 1390) should
618 * always equal 1 (Ethernet).
621 arp
->ar_hrd
= htons(np
->dev
->type
);
622 arp
->ar_pro
= htons(ETH_P_IP
);
623 arp
->ar_hln
= np
->dev
->addr_len
;
625 arp
->ar_op
= htons(type
);
627 arp_ptr
= (unsigned char *)(arp
+ 1);
628 memcpy(arp_ptr
, np
->dev
->dev_addr
, np
->dev
->addr_len
);
629 arp_ptr
+= np
->dev
->addr_len
;
630 memcpy(arp_ptr
, &tip
, 4);
632 memcpy(arp_ptr
, sha
, np
->dev
->addr_len
);
633 arp_ptr
+= np
->dev
->addr_len
;
634 memcpy(arp_ptr
, &sip
, 4);
636 netpoll_send_skb(np
, send_skb
);
638 /* If there are several rx_hooks for the same address,
639 we're fine by sending a single reply */
642 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
643 } else if( proto
== ETH_P_IPV6
) {
644 #if IS_ENABLED(CONFIG_IPV6)
648 struct icmp6hdr
*icmp6h
;
649 const struct in6_addr
*saddr
;
650 const struct in6_addr
*daddr
;
651 struct inet6_dev
*in6_dev
= NULL
;
652 struct in6_addr
*target
;
654 in6_dev
= in6_dev_get(skb
->dev
);
655 if (!in6_dev
|| !in6_dev
->cnf
.accept_ra
)
658 if (!pskb_may_pull(skb
, skb
->len
))
661 msg
= (struct nd_msg
*)skb_transport_header(skb
);
663 __skb_push(skb
, skb
->data
- skb_transport_header(skb
));
665 if (ipv6_hdr(skb
)->hop_limit
!= 255)
667 if (msg
->icmph
.icmp6_code
!= 0)
669 if (msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
672 saddr
= &ipv6_hdr(skb
)->saddr
;
673 daddr
= &ipv6_hdr(skb
)->daddr
;
675 size
= sizeof(struct icmp6hdr
) + sizeof(struct in6_addr
);
677 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
678 list_for_each_entry_safe(np
, tmp
, &npinfo
->rx_np
, rx
) {
679 if (!ipv6_addr_equal(daddr
, &np
->local_ip
.in6
))
682 hlen
= LL_RESERVED_SPACE(np
->dev
);
683 tlen
= np
->dev
->needed_tailroom
;
684 send_skb
= find_skb(np
, size
+ hlen
+ tlen
, hlen
);
688 send_skb
->protocol
= htons(ETH_P_IPV6
);
689 send_skb
->dev
= skb
->dev
;
691 skb_reset_network_header(send_skb
);
692 skb_put(send_skb
, sizeof(struct ipv6hdr
));
693 hdr
= ipv6_hdr(send_skb
);
695 *(__be32
*)hdr
= htonl(0x60000000);
697 hdr
->payload_len
= htons(size
);
698 hdr
->nexthdr
= IPPROTO_ICMPV6
;
699 hdr
->hop_limit
= 255;
703 send_skb
->transport_header
= send_skb
->tail
;
704 skb_put(send_skb
, size
);
706 icmp6h
= (struct icmp6hdr
*)skb_transport_header(skb
);
707 icmp6h
->icmp6_type
= NDISC_NEIGHBOUR_ADVERTISEMENT
;
708 icmp6h
->icmp6_router
= 0;
709 icmp6h
->icmp6_solicited
= 1;
710 target
= (struct in6_addr
*)(skb_transport_header(send_skb
) + sizeof(struct icmp6hdr
));
711 *target
= msg
->target
;
712 icmp6h
->icmp6_cksum
= csum_ipv6_magic(saddr
, daddr
, size
,
717 if (dev_hard_header(send_skb
, skb
->dev
, ETH_P_IPV6
,
718 lladdr
, np
->dev
->dev_addr
,
719 send_skb
->len
) < 0) {
724 netpoll_send_skb(np
, send_skb
);
726 /* If there are several rx_hooks for the same address,
727 we're fine by sending a single reply */
730 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
735 static bool pkt_is_ns(struct sk_buff
*skb
)
740 if (skb
->protocol
!= htons(ETH_P_ARP
))
742 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
) + sizeof(struct nd_msg
)))
745 msg
= (struct nd_msg
*)skb_transport_header(skb
);
746 __skb_push(skb
, skb
->data
- skb_transport_header(skb
));
749 if (hdr
->nexthdr
!= IPPROTO_ICMPV6
)
751 if (hdr
->hop_limit
!= 255)
753 if (msg
->icmph
.icmp6_code
!= 0)
755 if (msg
->icmph
.icmp6_type
!= NDISC_NEIGHBOUR_SOLICITATION
)
761 int __netpoll_rx(struct sk_buff
*skb
, struct netpoll_info
*npinfo
)
763 int proto
, len
, ulen
;
765 const struct iphdr
*iph
;
767 struct netpoll
*np
, *tmp
;
769 if (list_empty(&npinfo
->rx_np
))
772 if (skb
->dev
->type
!= ARPHRD_ETHER
)
775 /* check if netpoll clients need ARP */
776 if (skb
->protocol
== htons(ETH_P_ARP
) && atomic_read(&trapped
)) {
777 skb_queue_tail(&npinfo
->neigh_tx
, skb
);
779 } else if (pkt_is_ns(skb
) && atomic_read(&trapped
)) {
780 skb_queue_tail(&npinfo
->neigh_tx
, skb
);
784 if (skb
->protocol
== cpu_to_be16(ETH_P_8021Q
)) {
785 skb
= vlan_untag(skb
);
790 proto
= ntohs(eth_hdr(skb
)->h_proto
);
791 if (proto
!= ETH_P_IP
&& proto
!= ETH_P_IPV6
)
793 if (skb
->pkt_type
== PACKET_OTHERHOST
)
798 if (proto
== ETH_P_IP
) {
799 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
801 iph
= (struct iphdr
*)skb
->data
;
802 if (iph
->ihl
< 5 || iph
->version
!= 4)
804 if (!pskb_may_pull(skb
, iph
->ihl
*4))
806 iph
= (struct iphdr
*)skb
->data
;
807 if (ip_fast_csum((u8
*)iph
, iph
->ihl
) != 0)
810 len
= ntohs(iph
->tot_len
);
811 if (skb
->len
< len
|| len
< iph
->ihl
*4)
815 * Our transport medium may have padded the buffer out.
816 * Now We trim to the true length of the frame.
818 if (pskb_trim_rcsum(skb
, len
))
821 iph
= (struct iphdr
*)skb
->data
;
822 if (iph
->protocol
!= IPPROTO_UDP
)
826 uh
= (struct udphdr
*)(((char *)iph
) + iph
->ihl
*4);
827 ulen
= ntohs(uh
->len
);
831 if (checksum_udp(skb
, uh
, ulen
, iph
->saddr
, iph
->daddr
))
833 list_for_each_entry_safe(np
, tmp
, &npinfo
->rx_np
, rx
) {
834 if (np
->local_ip
.ip
&& np
->local_ip
.ip
!= iph
->daddr
)
836 if (np
->remote_ip
.ip
&& np
->remote_ip
.ip
!= iph
->saddr
)
838 if (np
->local_port
&& np
->local_port
!= ntohs(uh
->dest
))
841 np
->rx_hook(np
, ntohs(uh
->source
),
843 ulen
- sizeof(struct udphdr
));
847 #if IS_ENABLED(CONFIG_IPV6)
848 const struct ipv6hdr
*ip6h
;
850 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
852 ip6h
= (struct ipv6hdr
*)skb
->data
;
853 if (ip6h
->version
!= 6)
855 len
= ntohs(ip6h
->payload_len
);
858 if (len
+ sizeof(struct ipv6hdr
) > skb
->len
)
860 if (pskb_trim_rcsum(skb
, len
+ sizeof(struct ipv6hdr
)))
862 ip6h
= ipv6_hdr(skb
);
863 if (!pskb_may_pull(skb
, sizeof(struct udphdr
)))
866 ulen
= ntohs(uh
->len
);
867 if (ulen
!= skb
->len
)
869 if (udp6_csum_init(skb
, uh
, IPPROTO_UDP
))
871 list_for_each_entry_safe(np
, tmp
, &npinfo
->rx_np
, rx
) {
872 if (!ipv6_addr_equal(&np
->local_ip
.in6
, &ip6h
->daddr
))
874 if (!ipv6_addr_equal(&np
->remote_ip
.in6
, &ip6h
->saddr
))
876 if (np
->local_port
&& np
->local_port
!= ntohs(uh
->dest
))
879 np
->rx_hook(np
, ntohs(uh
->source
),
881 ulen
- sizeof(struct udphdr
));
894 if (atomic_read(&trapped
)) {
902 void netpoll_print_options(struct netpoll
*np
)
904 np_info(np
, "local port %d\n", np
->local_port
);
906 np_info(np
, "local IPv6 address %pI6c\n", &np
->local_ip
.in6
);
908 np_info(np
, "local IPv4 address %pI4\n", &np
->local_ip
.ip
);
909 np_info(np
, "interface '%s'\n", np
->dev_name
);
910 np_info(np
, "remote port %d\n", np
->remote_port
);
912 np_info(np
, "remote IPv6 address %pI6c\n", &np
->remote_ip
.in6
);
914 np_info(np
, "remote IPv4 address %pI4\n", &np
->remote_ip
.ip
);
915 np_info(np
, "remote ethernet address %pM\n", np
->remote_mac
);
917 EXPORT_SYMBOL(netpoll_print_options
);
919 static int netpoll_parse_ip_addr(const char *str
, union inet_addr
*addr
)
923 if (!strchr(str
, ':') &&
924 in4_pton(str
, -1, (void *)addr
, -1, &end
) > 0) {
928 if (in6_pton(str
, -1, addr
->in6
.s6_addr
, -1, &end
) > 0) {
929 #if IS_ENABLED(CONFIG_IPV6)
939 int netpoll_parse_options(struct netpoll
*np
, char *opt
)
941 char *cur
=opt
, *delim
;
945 if ((delim
= strchr(cur
, '@')) == NULL
)
948 if (kstrtou16(cur
, 10, &np
->local_port
))
955 if ((delim
= strchr(cur
, '/')) == NULL
)
958 ipv6
= netpoll_parse_ip_addr(cur
, &np
->local_ip
);
962 np
->ipv6
= (bool)ipv6
;
968 /* parse out dev name */
969 if ((delim
= strchr(cur
, ',')) == NULL
)
972 strlcpy(np
->dev_name
, cur
, sizeof(np
->dev_name
));
979 if ((delim
= strchr(cur
, '@')) == NULL
)
982 if (*cur
== ' ' || *cur
== '\t')
983 np_info(np
, "warning: whitespace is not allowed\n");
984 if (kstrtou16(cur
, 10, &np
->remote_port
))
991 if ((delim
= strchr(cur
, '/')) == NULL
)
994 ipv6
= netpoll_parse_ip_addr(cur
, &np
->remote_ip
);
997 else if (np
->ipv6
!= (bool)ipv6
)
1000 np
->ipv6
= (bool)ipv6
;
1005 if (!mac_pton(cur
, np
->remote_mac
))
1009 netpoll_print_options(np
);
1014 np_info(np
, "couldn't parse config at '%s'!\n", cur
);
1017 EXPORT_SYMBOL(netpoll_parse_options
);
1019 int __netpoll_setup(struct netpoll
*np
, struct net_device
*ndev
, gfp_t gfp
)
1021 struct netpoll_info
*npinfo
;
1022 const struct net_device_ops
*ops
;
1023 unsigned long flags
;
1027 strlcpy(np
->dev_name
, ndev
->name
, IFNAMSIZ
);
1028 INIT_WORK(&np
->cleanup_work
, netpoll_async_cleanup
);
1030 if ((ndev
->priv_flags
& IFF_DISABLE_NETPOLL
) ||
1031 !ndev
->netdev_ops
->ndo_poll_controller
) {
1032 np_err(np
, "%s doesn't support polling, aborting\n",
1038 if (!ndev
->npinfo
) {
1039 npinfo
= kmalloc(sizeof(*npinfo
), gfp
);
1045 npinfo
->rx_flags
= 0;
1046 INIT_LIST_HEAD(&npinfo
->rx_np
);
1048 spin_lock_init(&npinfo
->rx_lock
);
1049 mutex_init(&npinfo
->dev_lock
);
1050 skb_queue_head_init(&npinfo
->neigh_tx
);
1051 skb_queue_head_init(&npinfo
->txq
);
1052 INIT_DELAYED_WORK(&npinfo
->tx_work
, queue_process
);
1054 atomic_set(&npinfo
->refcnt
, 1);
1056 ops
= np
->dev
->netdev_ops
;
1057 if (ops
->ndo_netpoll_setup
) {
1058 err
= ops
->ndo_netpoll_setup(ndev
, npinfo
, gfp
);
1063 npinfo
= rtnl_dereference(ndev
->npinfo
);
1064 atomic_inc(&npinfo
->refcnt
);
1067 npinfo
->netpoll
= np
;
1070 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
1071 npinfo
->rx_flags
|= NETPOLL_RX_ENABLED
;
1072 list_add_tail(&np
->rx
, &npinfo
->rx_np
);
1073 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
1076 /* last thing to do is link it to the net device structure */
1077 rcu_assign_pointer(ndev
->npinfo
, npinfo
);
1086 EXPORT_SYMBOL_GPL(__netpoll_setup
);
1088 int netpoll_setup(struct netpoll
*np
)
1090 struct net_device
*ndev
= NULL
;
1091 struct in_device
*in_dev
;
1096 struct net
*net
= current
->nsproxy
->net_ns
;
1097 ndev
= __dev_get_by_name(net
, np
->dev_name
);
1100 np_err(np
, "%s doesn't exist, aborting\n", np
->dev_name
);
1106 if (netdev_master_upper_dev_get(ndev
)) {
1107 np_err(np
, "%s is a slave device, aborting\n", np
->dev_name
);
1112 if (!netif_running(ndev
)) {
1113 unsigned long atmost
, atleast
;
1115 np_info(np
, "device %s not up yet, forcing it\n", np
->dev_name
);
1117 err
= dev_open(ndev
);
1120 np_err(np
, "failed to open %s\n", ndev
->name
);
1125 atleast
= jiffies
+ HZ
/10;
1126 atmost
= jiffies
+ carrier_timeout
* HZ
;
1127 while (!netif_carrier_ok(ndev
)) {
1128 if (time_after(jiffies
, atmost
)) {
1129 np_notice(np
, "timeout waiting for carrier\n");
1135 /* If carrier appears to come up instantly, we don't
1136 * trust it and pause so that we don't pump all our
1137 * queued console messages into the bitbucket.
1140 if (time_before(jiffies
, atleast
)) {
1141 np_notice(np
, "carrier detect appears untrustworthy, waiting 4 seconds\n");
1147 if (!np
->local_ip
.ip
) {
1149 in_dev
= __in_dev_get_rtnl(ndev
);
1151 if (!in_dev
|| !in_dev
->ifa_list
) {
1152 np_err(np
, "no IP address for %s, aborting\n",
1154 err
= -EDESTADDRREQ
;
1158 np
->local_ip
.ip
= in_dev
->ifa_list
->ifa_local
;
1159 np_info(np
, "local IP %pI4\n", &np
->local_ip
.ip
);
1161 #if IS_ENABLED(CONFIG_IPV6)
1162 struct inet6_dev
*idev
;
1164 err
= -EDESTADDRREQ
;
1165 idev
= __in6_dev_get(ndev
);
1167 struct inet6_ifaddr
*ifp
;
1169 read_lock_bh(&idev
->lock
);
1170 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
1171 if (ipv6_addr_type(&ifp
->addr
) & IPV6_ADDR_LINKLOCAL
)
1173 np
->local_ip
.in6
= ifp
->addr
;
1177 read_unlock_bh(&idev
->lock
);
1180 np_err(np
, "no IPv6 address for %s, aborting\n",
1184 np_info(np
, "local IPv6 %pI6c\n", &np
->local_ip
.in6
);
1186 np_err(np
, "IPv6 is not supported %s, aborting\n",
1194 /* fill up the skb queue */
1197 err
= __netpoll_setup(np
, ndev
, GFP_KERNEL
);
1210 EXPORT_SYMBOL(netpoll_setup
);
1212 static int __init
netpoll_init(void)
1214 skb_queue_head_init(&skb_pool
);
1217 core_initcall(netpoll_init
);
1219 static void rcu_cleanup_netpoll_info(struct rcu_head
*rcu_head
)
1221 struct netpoll_info
*npinfo
=
1222 container_of(rcu_head
, struct netpoll_info
, rcu
);
1224 skb_queue_purge(&npinfo
->neigh_tx
);
1225 skb_queue_purge(&npinfo
->txq
);
1227 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
1228 cancel_delayed_work(&npinfo
->tx_work
);
1230 /* clean after last, unfinished work */
1231 __skb_queue_purge(&npinfo
->txq
);
1232 /* now cancel it again */
1233 cancel_delayed_work(&npinfo
->tx_work
);
1237 void __netpoll_cleanup(struct netpoll
*np
)
1239 struct netpoll_info
*npinfo
;
1240 unsigned long flags
;
1242 /* rtnl_dereference would be preferable here but
1243 * rcu_cleanup_netpoll path can put us in here safely without
1244 * holding the rtnl, so plain rcu_dereference it is
1246 npinfo
= rtnl_dereference(np
->dev
->npinfo
);
1250 if (!list_empty(&npinfo
->rx_np
)) {
1251 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
1253 if (list_empty(&npinfo
->rx_np
))
1254 npinfo
->rx_flags
&= ~NETPOLL_RX_ENABLED
;
1255 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
1258 synchronize_srcu(&netpoll_srcu
);
1260 if (atomic_dec_and_test(&npinfo
->refcnt
)) {
1261 const struct net_device_ops
*ops
;
1263 ops
= np
->dev
->netdev_ops
;
1264 if (ops
->ndo_netpoll_cleanup
)
1265 ops
->ndo_netpoll_cleanup(np
->dev
);
1267 rcu_assign_pointer(np
->dev
->npinfo
, NULL
);
1268 call_rcu_bh(&npinfo
->rcu
, rcu_cleanup_netpoll_info
);
1271 EXPORT_SYMBOL_GPL(__netpoll_cleanup
);
1273 static void netpoll_async_cleanup(struct work_struct
*work
)
1275 struct netpoll
*np
= container_of(work
, struct netpoll
, cleanup_work
);
1278 __netpoll_cleanup(np
);
1283 void __netpoll_free_async(struct netpoll
*np
)
1285 schedule_work(&np
->cleanup_work
);
1287 EXPORT_SYMBOL_GPL(__netpoll_free_async
);
1289 void netpoll_cleanup(struct netpoll
*np
)
1295 __netpoll_cleanup(np
);
1301 EXPORT_SYMBOL(netpoll_cleanup
);
1303 int netpoll_trap(void)
1305 return atomic_read(&trapped
);
1307 EXPORT_SYMBOL(netpoll_trap
);
1309 void netpoll_set_trap(int trap
)
1312 atomic_inc(&trapped
);
1314 atomic_dec(&trapped
);
1316 EXPORT_SYMBOL(netpoll_set_trap
);