Skip to contentSkip to conversation

6lowpan: udp compression/uncompression fix

PATCH In review Alexander Aring 2 participants 10 messages Dec 17, 2013

Subsystems (3)
lore
Share & tools
lore

Alexander Aring

author maintainer
#0 lore raw

v3 cover 6lowpan: udp compression/uncompression fix

The current 6LoWPAN udp compression/uncompression is completely broken. This patch series fix a lot of udp compression/uncompression issues and add support parsing with lowpan_fetch_skb/lowpan_push_hc_data functions.

I tested it in all cases of compressions in wireshark and a contiki sensornode.

Changes since v3:
- add patch to introduce lowpan_push_hc_data helper function.
- remove patch to fix nullpointer dereferencing and replace it with a patch which use the lowpan_push_hc_data function in compress_udp_header function. This fixes the nullpointer dereferencing problem also and is more readable.
- add missing newline in pr_debug_ratelimited.

Changes since v2:
- remove unnecessary casts and brackes, suggested by Werner Almesberger.
- use pr_debug_ratelimited instead of pr_debug on patch 4/7.

Alexander Aring (8):
6lowpan: introduce lowpan_push_hc_data function
6lowpan: udp use lowpan_push_hc_data function
6lowpan: fix udp compress ordering
6lowpan: fix udp byte ordering
6lowpan: add udp warning for elided checksum
6lowpan: udp use lowpan_fetch_skb function
6lowpan: udp use subtraction on both conditions
6lowpan: cleanup udp compress function

 net/ieee802154/6lowpan.h      |   8 +++
 net/ieee802154/6lowpan_iphc.c | 114 ++++++++++++++++++++++++------------------
 2 files changed, 74 insertions(+), 48 deletions(-)
Signature · 1 line
1.8.5.1
author maintainer
#1 lore raw

PATCH v3 1/8 6lowpan: introduce lowpan_push_hc_data function

This patch introduce the lowpan_push_hc_data function to set data in the iphc buffer.

It's a common case to set data and increase the buffer pointer. This helper function can be used many times in header_compress function to generate the iphc header.

---
 net/ieee802154/6lowpan.h | 7 +++++++
 1 file changed, 7 insertions(+)
net/ieee802154/6lowpan.h +7 -0
index 10909e5..4981bf8 100644
@@ -298,6 +298,13 @@ static inline bool lowpan_fetch_skb(struct sk_buff *skb,
 	return false;
 }
 
+static inline void lowpan_push_hc_data(u8 **hc_ptr, const void *data,
+				       const size_t len)
+{
+	memcpy(*hc_ptr, data, len);
+	*hc_ptr += len;
+}
+
 typedef int (*skb_delivery_cb)(struct sk_buff *skb, struct net_device *dev);
 
 int lowpan_process_data(struct sk_buff *skb, struct net_device *dev,
Signature · 1 line
1.8.5.1
author maintainer
#2 lore raw

PATCH v3 2/8 6lowpan: udp use lowpan_push_hc_data function

In reply to Alexander Aring

This patch uses the lowpan_push_hc_data to generate iphc header.

The current implementation has some wrong pointer arithmetic issues and works in a random case only.

---
 net/ieee802154/6lowpan_iphc.c | 37 ++++++++++++++++++++-----------------
 1 file changed, 20 insertions(+), 17 deletions(-)
net/ieee802154/6lowpan_iphc.c +20 -17
index 88e7da5..77c0366 100644
@@ -541,42 +541,45 @@ static u8 lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift,
 static void compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
 {
 	struct udphdr *uh = udp_hdr(skb);
+	u8 tmp;
 
 	if (((uh->source & LOWPAN_NHC_UDP_4BIT_MASK) ==
 				LOWPAN_NHC_UDP_4BIT_PORT) &&
 	    ((uh->dest & LOWPAN_NHC_UDP_4BIT_MASK) ==
 				LOWPAN_NHC_UDP_4BIT_PORT)) {
 		pr_debug("UDP header: both ports compression to 4 bits\n");
-		**hc06_ptr = LOWPAN_NHC_UDP_CS_P_11;
-		**(hc06_ptr + 1) = /* subtraction is faster */
+		tmp = LOWPAN_NHC_UDP_CS_P_11;
+		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
+		tmp = /* subtraction is faster */
 		   (u8)((uh->dest - LOWPAN_NHC_UDP_4BIT_PORT) +
 		       ((uh->source & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
-		*hc06_ptr += 2;
+		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
 	} else if ((uh->dest & LOWPAN_NHC_UDP_8BIT_MASK) ==
 			LOWPAN_NHC_UDP_8BIT_PORT) {
 		pr_debug("UDP header: remove 8 bits of dest\n");
-		**hc06_ptr = LOWPAN_NHC_UDP_CS_P_01;
-		memcpy(*hc06_ptr + 1, &uh->source, 2);
-		**(hc06_ptr + 3) = (u8)(uh->dest - LOWPAN_NHC_UDP_8BIT_PORT);
-		*hc06_ptr += 4;
+		tmp = LOWPAN_NHC_UDP_CS_P_01;
+		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
+		lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
+		tmp = (u8)(uh->dest - LOWPAN_NHC_UDP_8BIT_PORT);
+		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
 	} else if ((uh->source & LOWPAN_NHC_UDP_8BIT_MASK) ==
 			LOWPAN_NHC_UDP_8BIT_PORT) {
 		pr_debug("UDP header: remove 8 bits of source\n");
-		**hc06_ptr = LOWPAN_NHC_UDP_CS_P_10;
-		memcpy(*hc06_ptr + 1, &uh->dest, 2);
-		**(hc06_ptr + 3) = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
-		*hc06_ptr += 4;
+		tmp = LOWPAN_NHC_UDP_CS_P_10;
+		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
+		lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
+		tmp = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
+		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
 	} else {
 		pr_debug("UDP header: can't compress\n");
-		**hc06_ptr = LOWPAN_NHC_UDP_CS_P_00;
-		memcpy(*hc06_ptr + 1, &uh->source, 2);
-		memcpy(*hc06_ptr + 3, &uh->dest, 2);
-		*hc06_ptr += 5;
+		tmp = LOWPAN_NHC_UDP_CS_P_00;
+		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
+		lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
+		lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
 	}
 
 	/* checksum is always inline */
-	memcpy(*hc06_ptr, &uh->check, 2);
-	*hc06_ptr += 2;
+	lowpan_push_hc_data(hc06_ptr, &uh->check, sizeof(uh->check));
 
 	/* skip the UDP header */
 	skb_pull(skb, sizeof(struct udphdr));
Signature · 1 line
1.8.5.1
author maintainer
#3 lore raw

PATCH v3 3/8 6lowpan: fix udp compress ordering

In reply to Alexander Aring

In case ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) the order of uncompression is wrong. It's always first source port then destination port as second.

See:
http://tools.ietf.org/html/rfc6282#section-4.3.3

"Fields carried in-line (in part or in whole) appear in the same order as they do in the UDP header format"

---
 net/ieee802154/6lowpan_iphc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ieee802154/6lowpan_iphc.c +1 -1
index 77c0366..1933f5b 100644
@@ -567,9 +567,9 @@ static void compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
 		pr_debug("UDP header: remove 8 bits of source\n");
 		tmp = LOWPAN_NHC_UDP_CS_P_10;
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
-		lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
 		tmp = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
+		lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
 	} else {
 		pr_debug("UDP header: can't compress\n");
 		tmp = LOWPAN_NHC_UDP_CS_P_00;
Signature · 1 line
1.8.5.1
author maintainer
#4 lore raw

PATCH v3 4/8 6lowpan: fix udp byte ordering

In reply to Alexander Aring

The incoming udp header in lowpan_compress_udp_header function is already in network byte order.

Everytime we read this values for source and destination port we need to convert this value to host byte order.

In the outcoming header we need to set this value in network byte order which the upcoming process assumes.

---
 net/ieee802154/6lowpan_iphc.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
net/ieee802154/6lowpan_iphc.c +20 -19
index 1933f5b..02bf74d 100644
@@ -283,20 +283,21 @@ uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
 			break;
 		case LOWPAN_NHC_UDP_CS_P_01:
 			memcpy(&uh->source, &skb->data[0], 2);
-			uh->dest =
-			   skb->data[2] + LOWPAN_NHC_UDP_8BIT_PORT;
+			uh->dest = htons(skb->data[2] +
+					 LOWPAN_NHC_UDP_8BIT_PORT);
 			skb_pull(skb, 3);
 			break;
 		case LOWPAN_NHC_UDP_CS_P_10:
-			uh->source = skb->data[0] + LOWPAN_NHC_UDP_8BIT_PORT;
+			uh->source = htons(skb->data[0] +
+					   LOWPAN_NHC_UDP_8BIT_PORT);
 			memcpy(&uh->dest, &skb->data[1], 2);
 			skb_pull(skb, 3);
 			break;
 		case LOWPAN_NHC_UDP_CS_P_11:
-			uh->source =
-			   LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] >> 4);
-			uh->dest =
-			   LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] & 0x0f);
+			uh->source = htons(LOWPAN_NHC_UDP_4BIT_PORT +
+					   (skb->data[0] >> 4));
+			uh->dest = htons(LOWPAN_NHC_UDP_4BIT_PORT +
+					 (skb->data[0] & 0x0f));
 			skb_pull(skb, 1);
 			break;
 		default:
@@ -306,7 +307,7 @@ uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
 		}
 
 		pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
-			 uh->source, uh->dest);
+			 ntohs(uh->source), ntohs(uh->dest));
 
 		/* copy checksum */
 		memcpy(&uh->check, &skb->data[0], 2);
@@ -318,7 +319,7 @@ uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
 		 * frame
 		 */
 		uh->len = htons(skb->len + sizeof(struct udphdr));
-		pr_debug("uncompressed UDP length: src = %d", uh->len);
+		pr_debug("uncompressed UDP length: src = %d", ntohs(uh->len));
 	} else {
 		pr_debug("ERROR: unsupported NH format\n");
 		goto err;
@@ -543,31 +544,31 @@ static void compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
 	struct udphdr *uh = udp_hdr(skb);
 	u8 tmp;
 
-	if (((uh->source & LOWPAN_NHC_UDP_4BIT_MASK) ==
-				LOWPAN_NHC_UDP_4BIT_PORT) &&
-	    ((uh->dest & LOWPAN_NHC_UDP_4BIT_MASK) ==
-				LOWPAN_NHC_UDP_4BIT_PORT)) {
+	if (((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_MASK) ==
+	     LOWPAN_NHC_UDP_4BIT_PORT) &&
+	    ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
+	     LOWPAN_NHC_UDP_4BIT_PORT)) {
 		pr_debug("UDP header: both ports compression to 4 bits\n");
 		tmp = LOWPAN_NHC_UDP_CS_P_11;
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
 		tmp = /* subtraction is faster */
-		   (u8)((uh->dest - LOWPAN_NHC_UDP_4BIT_PORT) +
-		       ((uh->source & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
+		   (u8)((ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT) +
+		       ((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
-	} else if ((uh->dest & LOWPAN_NHC_UDP_8BIT_MASK) ==
+	} else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
 			LOWPAN_NHC_UDP_8BIT_PORT) {
 		pr_debug("UDP header: remove 8 bits of dest\n");
 		tmp = LOWPAN_NHC_UDP_CS_P_01;
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
 		lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
-		tmp = (u8)(uh->dest - LOWPAN_NHC_UDP_8BIT_PORT);
+		tmp = (u8)(ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT);
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
-	} else if ((uh->source & LOWPAN_NHC_UDP_8BIT_MASK) ==
+	} else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
 			LOWPAN_NHC_UDP_8BIT_PORT) {
 		pr_debug("UDP header: remove 8 bits of source\n");
 		tmp = LOWPAN_NHC_UDP_CS_P_10;
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
-		tmp = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
+		tmp = (u8)(ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT);
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
 		lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
 	} else {
Signature · 1 line
1.8.5.1
author maintainer
#5 lore raw

PATCH v3 5/8 6lowpan: add udp warning for elided checksum

In reply to Alexander Aring

Bit 5 of "UDP LOWPAN_NHC Format" indicate that the checksum can be elided.
The host need to calculate the udp checksum afterwards but this isn't supported right now.

See:
http://tools.ietf.org/html/rfc6282#section-4.3.3

---
 net/ieee802154/6lowpan.h      |  1 +
 net/ieee802154/6lowpan_iphc.c | 11 ++++++++---
 2 files changed, 9 insertions(+), 3 deletions(-)
net/ieee802154/6lowpan.h +1 -0
index 4981bf8..2b835db 100644
@@ -231,6 +231,7 @@
 #define LOWPAN_NHC_UDP_CS_P_10	0xF2 /* source = 0xF0 + 8bit inline,
 					dest = 16 bit inline */
 #define LOWPAN_NHC_UDP_CS_P_11	0xF3 /* source & dest = 0xF0B + 4bit inline */
+#define LOWPAN_NHC_UDP_CS_C	0x04 /* checksum elided */
 
 #ifdef DEBUG
 /* print data in line */
net/ieee802154/6lowpan_iphc.c +8 -3
index 02bf74d..a70fa66 100644
@@ -309,9 +309,14 @@ uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
 		pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
 			 ntohs(uh->source), ntohs(uh->dest));
 
-		/* copy checksum */
-		memcpy(&uh->check, &skb->data[0], 2);
-		skb_pull(skb, 2);
+		/* checksum */
+		if (tmp & LOWPAN_NHC_UDP_CS_C) {
+			pr_debug_ratelimited("checksum elided currently not supported\n");
+			goto err;
+		} else {
+			memcpy(&uh->check, &skb->data[0], 2);
+			skb_pull(skb, 2);
+		}
 
 		/*
 		 * UDP lenght needs to be infered from the lower layers
Signature · 1 line
1.8.5.1
author maintainer
#6 lore raw

PATCH v3 6/8 6lowpan: udp use lowpan_fetch_skb function

In reply to Alexander Aring

Cleanup the lowpan_uncompress_udp_header function to use the lowpan_fetch_skb function.

---
 net/ieee802154/6lowpan_iphc.c | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)
net/ieee802154/6lowpan_iphc.c +18 -19
index a70fa66..8857285 100644
@@ -265,40 +265,37 @@ lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
 static int
 uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
 {
-	u8 tmp;
+	bool fail;
+	u8 tmp = 0, val = 0;
 
 	if (!uh)
 		goto err;
 
-	if (lowpan_fetch_skb_u8(skb, &tmp))
-		goto err;
+	fail = lowpan_fetch_skb(skb, &tmp, 1);
 
 	if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
 		pr_debug("UDP header uncompression\n");
 		switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
 		case LOWPAN_NHC_UDP_CS_P_00:
-			memcpy(&uh->source, &skb->data[0], 2);
-			memcpy(&uh->dest, &skb->data[2], 2);
-			skb_pull(skb, 4);
+			fail |= lowpan_fetch_skb(skb, &uh->source, 2);
+			fail |= lowpan_fetch_skb(skb, &uh->dest, 2);
 			break;
 		case LOWPAN_NHC_UDP_CS_P_01:
-			memcpy(&uh->source, &skb->data[0], 2);
-			uh->dest = htons(skb->data[2] +
-					 LOWPAN_NHC_UDP_8BIT_PORT);
-			skb_pull(skb, 3);
+			fail |= lowpan_fetch_skb(skb, &uh->source, 2);
+			fail |= lowpan_fetch_skb(skb, &val, 1);
+			uh->dest = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
 			break;
 		case LOWPAN_NHC_UDP_CS_P_10:
-			uh->source = htons(skb->data[0] +
-					   LOWPAN_NHC_UDP_8BIT_PORT);
-			memcpy(&uh->dest, &skb->data[1], 2);
-			skb_pull(skb, 3);
+			fail |= lowpan_fetch_skb(skb, &val, 1);
+			uh->source = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
+			fail |= lowpan_fetch_skb(skb, &uh->dest, 2);
 			break;
 		case LOWPAN_NHC_UDP_CS_P_11:
+			fail |= lowpan_fetch_skb(skb, &val, 1);
 			uh->source = htons(LOWPAN_NHC_UDP_4BIT_PORT +
-					   (skb->data[0] >> 4));
+					   (val >> 4));
 			uh->dest = htons(LOWPAN_NHC_UDP_4BIT_PORT +
-					 (skb->data[0] & 0x0f));
-			skb_pull(skb, 1);
+					 (val & 0x0f));
 			break;
 		default:
 			pr_debug("ERROR: unknown UDP format\n");
@@ -314,8 +311,7 @@ uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
 			pr_debug_ratelimited("checksum elided currently not supported\n");
 			goto err;
 		} else {
-			memcpy(&uh->check, &skb->data[0], 2);
-			skb_pull(skb, 2);
+			fail |= lowpan_fetch_skb(skb, &uh->check, 2);
 		}
 
 		/*
@@ -330,6 +326,9 @@ uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
 		goto err;
 	}
 
+	if (fail)
+		goto err;
+
 	return 0;
 err:
 	return -EINVAL;
Signature · 1 line
1.8.5.1
author maintainer
#7 lore raw

PATCH v3 7/8 6lowpan: udp use subtraction on both conditions

In reply to Alexander Aring

Cleanup code to handle both calculation in the same way.

---
 net/ieee802154/6lowpan_iphc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ieee802154/6lowpan_iphc.c +1 -1
index 8857285..b298bfc 100644
@@ -557,7 +557,7 @@ static void compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
 		tmp = /* subtraction is faster */
 		   (u8)((ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT) +
-		       ((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
+		       ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4));
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
 	} else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
 			LOWPAN_NHC_UDP_8BIT_PORT) {
Signature · 1 line
1.8.5.1
author maintainer
#8 lore raw

PATCH v3 8/8 6lowpan: cleanup udp compress function

In reply to Alexander Aring

This patch remove unnecessary casts and brackets in compress_udp_header function.

---
 net/ieee802154/6lowpan_iphc.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
net/ieee802154/6lowpan_iphc.c +15 -5
index b298bfc..11840f9 100644
@@ -553,33 +553,43 @@ static void compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
 	    ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
 	     LOWPAN_NHC_UDP_4BIT_PORT)) {
 		pr_debug("UDP header: both ports compression to 4 bits\n");
+		/* compression value */
 		tmp = LOWPAN_NHC_UDP_CS_P_11;
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
-		tmp = /* subtraction is faster */
-		   (u8)((ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT) +
-		       ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4));
+		/* source and destination port */
+		tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT +
+		      ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4);
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
 	} else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
 			LOWPAN_NHC_UDP_8BIT_PORT) {
 		pr_debug("UDP header: remove 8 bits of dest\n");
+		/* compression value */
 		tmp = LOWPAN_NHC_UDP_CS_P_01;
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
+		/* source port */
 		lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
-		tmp = (u8)(ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT);
+		/* destination port */
+		tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT;
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
 	} else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
 			LOWPAN_NHC_UDP_8BIT_PORT) {
 		pr_debug("UDP header: remove 8 bits of source\n");
+		/* compression value */
 		tmp = LOWPAN_NHC_UDP_CS_P_10;
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
-		tmp = (u8)(ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT);
+		/* source port */
+		tmp = ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT;
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
+		/* destination port */
 		lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
 	} else {
 		pr_debug("UDP header: can't compress\n");
+		/* compression value */
 		tmp = LOWPAN_NHC_UDP_CS_P_00;
 		lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
+		/* source port */
 		lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
+		/* destination port */
 		lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
 	}
 
Signature · 1 line
1.8.5.1
maintainer
#9 lore raw
In reply to Alexander Aring

Hi Alex,

Quoted 29 lines

The current 6LoWPAN udp compression/uncompression is completely broken. This patch series fix a lot of udp compression/uncompression issues and add support parsing with lowpan_fetch_skb/lowpan_push_hc_data functions.

I tested it in all cases of compressions in wireshark and a contiki sensornode.

Changes since v3:
- add patch to introduce lowpan_push_hc_data helper function.
- remove patch to fix nullpointer dereferencing and replace it with a patch which use the lowpan_push_hc_data function in compress_udp_header function. This fixes the nullpointer dereferencing problem also and is more readable.
- add missing newline in pr_debug_ratelimited.

Changes since v2:
- remove unnecessary casts and brackes, suggested by Werner Almesberger.
- use pr_debug_ratelimited instead of pr_debug on patch 4/7.

Alexander Aring (8):
6lowpan: introduce lowpan_push_hc_data function
6lowpan: udp use lowpan_push_hc_data function
6lowpan: fix udp compress ordering
6lowpan: fix udp byte ordering
6lowpan: add udp warning for elided checksum
6lowpan: udp use lowpan_fetch_skb function
6lowpan: udp use subtraction on both conditions
6lowpan: cleanup udp compress function

Quoted 3 lines

net/ieee802154/6lowpan.h | 8 +++
net/ieee802154/6lowpan_iphc.c | 114 ++++++++++++++++++++++++------------------

2 files changed, 74 insertions(+), 48 deletions(-)

all 8 patches have been applied to bluetooth-next tree.

Regards

Marcel