Skip to contentSkip to conversation

checksum: Fix TCP/UDP checksum computation on big endian arches

PATCH Applied Alin Nastac 2 participants 2 messages Oct 8, 2019 – Oct 9, 2019 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checksum: Fix TCP/UDP checksum computation on big endian arches

On big endian arches UDP/TCP checksum is incorrectly computed when payload length is odd.

---
 src/extra/checksum.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
src/extra/checksum.c +8 -2
index 4d52a99..42389aa 100644
@@ -11,6 +11,7 @@
 
 #include <stdio.h>
 #include <stdbool.h>
+#include <endian.h>
 #include <arpa/inet.h>
 #include <netinet/ip.h>
 #include <netinet/ip6.h>
@@ -26,8 +27,13 @@ uint16_t nfq_checksum(uint32_t sum, uint16_t *buf, int size)
 		sum += *buf++;
 		size -= sizeof(uint16_t);
 	}
-	if (size)
-		sum += *(uint8_t *)buf;
+	if (size) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+		sum += (uint16_t)*(uint8_t *)buf << 8;
+#else
+		sum += (uint16_t)*(uint8_t *)buf;
+#endif
+	}
 
 	sum = (sum >> 16) + (sum & 0xffff);
 	sum += (sum >>16);
Signature · 1 line
2.7.4
maintainer Applied
#1 lore raw
Quoted 2 lines · Alin Nastac

On big endian arches UDP/TCP checksum is incorrectly computed when payload length is odd.

Applied, thanks Alin.