Skip to contentSkip to conversation

batctl: tcpdump: Fix endianness in ICMPv6 Echo Request/Reply parsing

PATCH In review Linus Lüssing 3 participants 3 messages Sep 13, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 batctl: tcpdump: Fix endianness in ICMPv6 Echo Request/Reply parsing

The ICMPv6 Echo Request/Reply sequence number and id as well as the IPv6 header length are two byte long fields and therefore might need a conversion on a little endian system. Otherwise the output will be broken on such a machine.

Fixes 35b37756f4a3 ("add IPv6 support to tcpdump parser")
---
 tcpdump.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
tcpdump.c +6 -4
index db93681..b9edc20 100644
@@ -589,13 +589,15 @@ static void dump_ipv6(unsigned char *packet_buff, ssize_t buff_len,
 			break;
 		case ICMP6_ECHO_REQUEST:
 			printf(" echo request, id: %d, seq: %d, length: %hu\n",
-			       icmphdr->icmp6_id, icmphdr->icmp6_seq,
-			       iphdr->ip6_plen);
+			       ntohs(icmphdr->icmp6_id),
+			       ntohs(icmphdr->icmp6_seq),
+			       ntohs(iphdr->ip6_plen));
 			break;
 		case ICMP6_ECHO_REPLY:
 			printf(" echo reply, id: %d, seq: %d, length: %hu\n",
-			       icmphdr->icmp6_id, icmphdr->icmp6_seq,
-			       iphdr->ip6_plen);
+			       ntohs(icmphdr->icmp6_id),
+			       ntohs(icmphdr->icmp6_seq),
+			       ntohs(iphdr->ip6_plen));
 			break;
 		case ICMP6_TIME_EXCEEDED:
 			printf(" time exceeded in-transit, length %zu\n",
Signature · 1 line
2.28.0
#1 lore raw

I wonder about the "_might_ need a conversion" as most cpu architectures run little endian. Especially x86.

Wouldn't it be broken since years if it needed big → little conversion?

kind regards
txt.file

Quoted 36 lines · Linus Lüssing

The ICMPv6 Echo Request/Reply sequence number and id as well as the IPv6 header length are two byte long fields and therefore might need a conversion on a little endian system. Otherwise the output will be broken on such a machine.

Fixes 35b37756f4a3 ("add IPv6 support to tcpdump parser")
---
  tcpdump.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
tcpdump.c +6 -4
index db93681..b9edc20 100644
@@ -589,13 +589,15 @@ static void dump_ipv6(unsigned char *packet_buff, ssize_t buff_len,
  			break;
  		case ICMP6_ECHO_REQUEST:
  			printf(" echo request, id: %d, seq: %d, length: %hu\n",
-			       icmphdr->icmp6_id, icmphdr->icmp6_seq,
-			       iphdr->ip6_plen);
+			       ntohs(icmphdr->icmp6_id),
+			       ntohs(icmphdr->icmp6_seq),
+			       ntohs(iphdr->ip6_plen));
  			break;
  		case ICMP6_ECHO_REPLY:
  			printf(" echo reply, id: %d, seq: %d, length: %hu\n",
-			       icmphdr->icmp6_id, icmphdr->icmp6_seq,
-			       iphdr->ip6_plen);
+			       ntohs(icmphdr->icmp6_id),
+			       ntohs(icmphdr->icmp6_seq),
+			       ntohs(iphdr->ip6_plen));
  			break;
  		case ICMP6_TIME_EXCEEDED:
  			printf(" time exceeded in-transit, length %zu\n",
#2 lore raw
Quoted 4 lines · On Mon, Sep 14, 2020 at 12:07:41AM +0200, [email protected] wrote:

I wonder about the "_might_ need a conversion" as most cpu architectures run little endian. Especially x86.

Wouldn't it be broken since years if it needed big → little conversion?

The tcpdump output for IPv6 echo request/reply was for sure broken for years on little endian targets.
Note that most old-school router platforms are big endian, hence it worked well on the common ubiquiti and tp-link devices.

Quoted 41 lines

kind regards
txt.file

Quoted 36 lines · Linus Lüssing

The ICMPv6 Echo Request/Reply sequence number and id as well as the IPv6 header length are two byte long fields and therefore might need a conversion on a little endian system. Otherwise the output will be broken on such a machine.

Fixes 35b37756f4a3 ("add IPv6 support to tcpdump parser")
---
  tcpdump.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
tcpdump.c +6 -4
index db93681..b9edc20 100644
@@ -589,13 +589,15 @@ static void dump_ipv6(unsigned char *packet_buff, ssize_t buff_len,
  			break;
  		case ICMP6_ECHO_REQUEST:
  			printf(" echo request, id: %d, seq: %d, length: %hu\n",
-			       icmphdr->icmp6_id, icmphdr->icmp6_seq,
-			       iphdr->ip6_plen);
+			       ntohs(icmphdr->icmp6_id),
+			       ntohs(icmphdr->icmp6_seq),
+			       ntohs(iphdr->ip6_plen));
  			break;
  		case ICMP6_ECHO_REPLY:
  			printf(" echo reply, id: %d, seq: %d, length: %hu\n",
-			       icmphdr->icmp6_id, icmphdr->icmp6_seq,
-			       iphdr->ip6_plen);
+			       ntohs(icmphdr->icmp6_id),
+			       ntohs(icmphdr->icmp6_seq),
+			       ntohs(iphdr->ip6_plen));
  			break;
  		case ICMP6_TIME_EXCEEDED:
  			printf(" time exceeded in-transit, length %zu\n",