checksum: Fix TCP/UDP checksum computation on big endian arches
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.
Signed-off-by Alin Nastac <[email protected]>
--- src/extra/checksum.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
src/extra/checksum.c +8 -2
@@ -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
Quoted 2 lines · Alin Nastac
On big endian arches UDP/TCP checksum is incorrectly computed when payload length is odd.
Applied, thanks Alin.