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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

Signed-off-by: Alin Nastac <alin.nastac@xxxxxxxxx>
---
 src/extra/checksum.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/extra/checksum.c b/src/extra/checksum.c
index 4d52a99..42389aa 100644
--- a/src/extra/checksum.c
+++ b/src/extra/checksum.c
@@ -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);
-- 
2.7.4




[Index of Archives]     [Netfitler Users]     [Berkeley Packet Filter]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux