aboutsummaryrefslogtreecommitdiffstats
path: root/net/core/netpoll.c
diff options
context:
space:
mode:
authorCong Wang <amwang@redhat.com>2013-01-07 15:52:41 -0500
committerDavid S. Miller <davem@davemloft.net>2013-01-08 20:56:10 -0500
commitb3d936f3ea1c97c32680e0cd235474cf9dadb762 (patch)
tree55231f53fa50114417709a1cc2623f124bcba1f0 /net/core/netpoll.c
parentacb3e04119fbf9145eb6d6bb707f6fb662ab4d3b (diff)
netpoll: add IPv6 support
Currently, netpoll only supports IPv4. This patch adds IPv6 support to netpoll so that we can run netconsole over IPv6 network. Cc: David S. Miller <davem@davemloft.net> Signed-off-by: Cong Wang <amwang@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/core/netpoll.c')
-rw-r--r--net/core/netpoll.c246
1 files changed, 236 insertions, 10 deletions
diff --git a/net/core/netpoll.c b/net/core/netpoll.c
index 6bd073688f68..9f0506726101 100644
--- a/net/core/netpoll.c
+++ b/net/core/netpoll.c
@@ -29,6 +29,9 @@
29#include <linux/if_vlan.h> 29#include <linux/if_vlan.h>
30#include <net/tcp.h> 30#include <net/tcp.h>
31#include <net/udp.h> 31#include <net/udp.h>
32#include <net/addrconf.h>
33#include <net/ndisc.h>
34#include <net/ip6_checksum.h>
32#include <asm/unaligned.h> 35#include <asm/unaligned.h>
33#include <trace/events/napi.h> 36#include <trace/events/napi.h>
34 37
@@ -384,9 +387,12 @@ void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
384 struct iphdr *iph; 387 struct iphdr *iph;
385 struct ethhdr *eth; 388 struct ethhdr *eth;
386 static atomic_t ip_ident; 389 static atomic_t ip_ident;
390 struct ipv6hdr *ip6h;
387 391
388 udp_len = len + sizeof(*udph); 392 udp_len = len + sizeof(*udph);
389 if (!np->ipv6) 393 if (np->ipv6)
394 ip_len = udp_len + sizeof(*ip6h);
395 else
390 ip_len = udp_len + sizeof(*iph); 396 ip_len = udp_len + sizeof(*iph);
391 397
392 total_len = ip_len + LL_RESERVED_SPACE(np->dev); 398 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
@@ -406,7 +412,35 @@ void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
406 udph->dest = htons(np->remote_port); 412 udph->dest = htons(np->remote_port);
407 udph->len = htons(udp_len); 413 udph->len = htons(udp_len);
408 414
409 if (!np->ipv6) { 415 if (np->ipv6) {
416 udph->check = 0;
417 udph->check = csum_ipv6_magic(&np->local_ip.in6,
418 &np->remote_ip.in6,
419 udp_len, IPPROTO_UDP,
420 csum_partial(udph, udp_len, 0));
421 if (udph->check == 0)
422 udph->check = CSUM_MANGLED_0;
423
424 skb_push(skb, sizeof(*ip6h));
425 skb_reset_network_header(skb);
426 ip6h = ipv6_hdr(skb);
427
428 /* ip6h->version = 6; ip6h->priority = 0; */
429 put_unaligned(0x60, (unsigned char *)ip6h);
430 ip6h->flow_lbl[0] = 0;
431 ip6h->flow_lbl[1] = 0;
432 ip6h->flow_lbl[2] = 0;
433
434 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
435 ip6h->nexthdr = IPPROTO_UDP;
436 ip6h->hop_limit = 32;
437 ip6h->saddr = np->local_ip.in6;
438 ip6h->daddr = np->remote_ip.in6;
439
440 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
441 skb_reset_mac_header(skb);
442 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
443 } else {
410 udph->check = 0; 444 udph->check = 0;
411 udph->check = csum_tcpudp_magic(np->local_ip.ip, 445 udph->check = csum_tcpudp_magic(np->local_ip.ip,
412 np->remote_ip.ip, 446 np->remote_ip.ip,
@@ -448,9 +482,7 @@ EXPORT_SYMBOL(netpoll_send_udp);
448 482
449static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo) 483static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
450{ 484{
451 struct arphdr *arp; 485 int size, type = ARPOP_REPLY;
452 unsigned char *arp_ptr;
453 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
454 __be32 sip, tip; 486 __be32 sip, tip;
455 unsigned char *sha; 487 unsigned char *sha;
456 struct sk_buff *send_skb; 488 struct sk_buff *send_skb;
@@ -477,6 +509,8 @@ static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo
477 509
478 proto = ntohs(eth_hdr(skb)->h_proto); 510 proto = ntohs(eth_hdr(skb)->h_proto);
479 if (proto == ETH_P_IP) { 511 if (proto == ETH_P_IP) {
512 struct arphdr *arp;
513 unsigned char *arp_ptr;
480 /* No arp on this interface */ 514 /* No arp on this interface */
481 if (skb->dev->flags & IFF_NOARP) 515 if (skb->dev->flags & IFF_NOARP)
482 return; 516 return;
@@ -528,7 +562,7 @@ static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo
528 send_skb->protocol = htons(ETH_P_ARP); 562 send_skb->protocol = htons(ETH_P_ARP);
529 563
530 /* Fill the device header for the ARP frame */ 564 /* Fill the device header for the ARP frame */
531 if (dev_hard_header(send_skb, skb->dev, ptype, 565 if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
532 sha, np->dev->dev_addr, 566 sha, np->dev->dev_addr,
533 send_skb->len) < 0) { 567 send_skb->len) < 0) {
534 kfree_skb(send_skb); 568 kfree_skb(send_skb);
@@ -565,9 +599,124 @@ static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo
565 break; 599 break;
566 } 600 }
567 spin_unlock_irqrestore(&npinfo->rx_lock, flags); 601 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
602 } else if( proto == ETH_P_IPV6) {
603#if IS_ENABLED(CONFIG_IPV6)
604 struct nd_msg *msg;
605 u8 *lladdr = NULL;
606 struct ipv6hdr *hdr;
607 struct icmp6hdr *icmp6h;
608 const struct in6_addr *saddr;
609 const struct in6_addr *daddr;
610 struct inet6_dev *in6_dev = NULL;
611 struct in6_addr *target;
612
613 in6_dev = in6_dev_get(skb->dev);
614 if (!in6_dev || !in6_dev->cnf.accept_ra)
615 return;
616
617 if (!pskb_may_pull(skb, skb->len))
618 return;
619
620 msg = (struct nd_msg *)skb_transport_header(skb);
621
622 __skb_push(skb, skb->data - skb_transport_header(skb));
623
624 if (ipv6_hdr(skb)->hop_limit != 255)
625 return;
626 if (msg->icmph.icmp6_code != 0)
627 return;
628 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
629 return;
630
631 saddr = &ipv6_hdr(skb)->saddr;
632 daddr = &ipv6_hdr(skb)->daddr;
633
634 size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
635
636 spin_lock_irqsave(&npinfo->rx_lock, flags);
637 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
638 if (memcmp(daddr, &np->local_ip, sizeof(*daddr)))
639 continue;
640
641 hlen = LL_RESERVED_SPACE(np->dev);
642 tlen = np->dev->needed_tailroom;
643 send_skb = find_skb(np, size + hlen + tlen, hlen);
644 if (!send_skb)
645 continue;
646
647 send_skb->protocol = htons(ETH_P_IPV6);
648 send_skb->dev = skb->dev;
649
650 skb_reset_network_header(send_skb);
651 skb_put(send_skb, sizeof(struct ipv6hdr));
652 hdr = ipv6_hdr(send_skb);
653
654 *(__be32*)hdr = htonl(0x60000000);
655
656 hdr->payload_len = htons(size);
657 hdr->nexthdr = IPPROTO_ICMPV6;
658 hdr->hop_limit = 255;
659 hdr->saddr = *saddr;
660 hdr->daddr = *daddr;
661
662 send_skb->transport_header = send_skb->tail;
663 skb_put(send_skb, size);
664
665 icmp6h = (struct icmp6hdr *)skb_transport_header(skb);
666 icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
667 icmp6h->icmp6_router = 0;
668 icmp6h->icmp6_solicited = 1;
669 target = (struct in6_addr *)skb_transport_header(send_skb) + sizeof(struct icmp6hdr);
670 *target = msg->target;
671 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
672 IPPROTO_ICMPV6,
673 csum_partial(icmp6h,
674 size, 0));
675
676 if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
677 lladdr, np->dev->dev_addr,
678 send_skb->len) < 0) {
679 kfree_skb(send_skb);
680 continue;
681 }
682
683 netpoll_send_skb(np, send_skb);
684
685 /* If there are several rx_hooks for the same address,
686 we're fine by sending a single reply */
687 break;
688 }
689 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
690#endif
568 } 691 }
569} 692}
570 693
694static bool pkt_is_ns(struct sk_buff *skb)
695{
696 struct nd_msg *msg;
697 struct ipv6hdr *hdr;
698
699 if (skb->protocol != htons(ETH_P_ARP))
700 return false;
701 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
702 return false;
703
704 msg = (struct nd_msg *)skb_transport_header(skb);
705 __skb_push(skb, skb->data - skb_transport_header(skb));
706 hdr = ipv6_hdr(skb);
707
708 if (hdr->nexthdr != IPPROTO_ICMPV6)
709 return false;
710 if (hdr->hop_limit != 255)
711 return false;
712 if (msg->icmph.icmp6_code != 0)
713 return false;
714 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
715 return false;
716
717 return true;
718}
719
571int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo) 720int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
572{ 721{
573 int proto, len, ulen; 722 int proto, len, ulen;
@@ -583,8 +732,10 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
583 goto out; 732 goto out;
584 733
585 /* check if netpoll clients need ARP */ 734 /* check if netpoll clients need ARP */
586 if (skb->protocol == htons(ETH_P_ARP) && 735 if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
587 atomic_read(&trapped)) { 736 skb_queue_tail(&npinfo->neigh_tx, skb);
737 return 1;
738 } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
588 skb_queue_tail(&npinfo->neigh_tx, skb); 739 skb_queue_tail(&npinfo->neigh_tx, skb);
589 return 1; 740 return 1;
590 } 741 }
@@ -651,6 +802,45 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
651 ulen - sizeof(struct udphdr)); 802 ulen - sizeof(struct udphdr));
652 hits++; 803 hits++;
653 } 804 }
805 } else {
806#if IS_ENABLED(CONFIG_IPV6)
807 const struct ipv6hdr *ip6h;
808
809 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
810 goto out;
811 ip6h = (struct ipv6hdr *)skb->data;
812 if (ip6h->version != 6)
813 goto out;
814 len = ntohs(ip6h->payload_len);
815 if (!len)
816 goto out;
817 if (len + sizeof(struct ipv6hdr) > skb->len)
818 goto out;
819 if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
820 goto out;
821 ip6h = ipv6_hdr(skb);
822 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
823 goto out;
824 uh = udp_hdr(skb);
825 ulen = ntohs(uh->len);
826 if (ulen != skb->len)
827 goto out;
828 if (udp6_csum_init(skb, uh, IPPROTO_UDP))
829 goto out;
830 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
831 if (memcmp(&np->local_ip.in6, &ip6h->daddr, sizeof(struct in6_addr)) != 0)
832 continue;
833 if (memcmp(&np->remote_ip.in6, &ip6h->saddr, sizeof(struct in6_addr)) != 0)
834 continue;
835 if (np->local_port && np->local_port != ntohs(uh->dest))
836 continue;
837
838 np->rx_hook(np, ntohs(uh->source),
839 (char *)(uh+1),
840 ulen - sizeof(struct udphdr));
841 hits++;
842 }
843#endif
654 } 844 }
655 845
656 if (!hits) 846 if (!hits)
@@ -671,11 +861,15 @@ out:
671void netpoll_print_options(struct netpoll *np) 861void netpoll_print_options(struct netpoll *np)
672{ 862{
673 np_info(np, "local port %d\n", np->local_port); 863 np_info(np, "local port %d\n", np->local_port);
674 if (!np->ipv6) 864 if (np->ipv6)
865 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
866 else
675 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip); 867 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
676 np_info(np, "interface '%s'\n", np->dev_name); 868 np_info(np, "interface '%s'\n", np->dev_name);
677 np_info(np, "remote port %d\n", np->remote_port); 869 np_info(np, "remote port %d\n", np->remote_port);
678 if (!np->ipv6) 870 if (np->ipv6)
871 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
872 else
679 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip); 873 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
680 np_info(np, "remote ethernet address %pM\n", np->remote_mac); 874 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
681} 875}
@@ -919,6 +1113,38 @@ int netpoll_setup(struct netpoll *np)
919 np->local_ip.ip = in_dev->ifa_list->ifa_local; 1113 np->local_ip.ip = in_dev->ifa_list->ifa_local;
920 rcu_read_unlock(); 1114 rcu_read_unlock();
921 np_info(np, "local IP %pI4\n", &np->local_ip.ip); 1115 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
1116 } else {
1117#if IS_ENABLED(CONFIG_IPV6)
1118 struct inet6_dev *idev;
1119
1120 err = -EDESTADDRREQ;
1121 rcu_read_lock();
1122 idev = __in6_dev_get(ndev);
1123 if (idev) {
1124 struct inet6_ifaddr *ifp;
1125
1126 read_lock_bh(&idev->lock);
1127 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1128 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
1129 continue;
1130 np->local_ip.in6 = ifp->addr;
1131 err = 0;
1132 break;
1133 }
1134 read_unlock_bh(&idev->lock);
1135 }
1136 rcu_read_unlock();
1137 if (err) {
1138 np_err(np, "no IPv6 address for %s, aborting\n",
1139 np->dev_name);
1140 goto put;
1141 } else
1142 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
1143#else
1144 np_err(np, "IPv6 is not supported %s, aborting\n",
1145 np->dev_name);
1146 goto put;
1147#endif
922 } 1148 }
923 } 1149 }
924 1150