From: Patrik Flykt Date: Fri, 20 Dec 2013 15:16:15 +0000 (+0200) Subject: libsystemd-dhcp: Fix checksum computation for buffer with odd size X-Git-Tag: v209~727 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=0c6a3c888abd720b5ab28162d1ba4eadffde5272;hp=77e8d29dd2ecfa7f11c31c8d96021d323ab3fe3a;ds=sidebyside libsystemd-dhcp: Fix checksum computation for buffer with odd size Fix off-by-one error and notice that summing may need more than one round for the result to be in the lower 16 bits. --- diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c index 7dc154612..68a3b1a1b 100644 --- a/src/libsystemd-dhcp/dhcp-client.c +++ b/src/libsystemd-dhcp/dhcp-client.c @@ -382,10 +382,13 @@ static uint16_t client_checksum(void *buf, int len) if (len & 0x01) { odd = buf; - sum += odd[len]; + sum += odd[len - 1]; } - return ~((sum & 0xffff) + (sum >> 16)); + while (sum >> 16) + sum = (sum & 0xffff) + (sum >> 16); + + return ~sum; } static void client_append_ip_headers(DHCPPacket *packet, uint16_t len) diff --git a/src/libsystemd-dhcp/test-dhcp-client.c b/src/libsystemd-dhcp/test-dhcp-client.c index d39851074..7400cc683 100644 --- a/src/libsystemd-dhcp/test-dhcp-client.c +++ b/src/libsystemd-dhcp/test-dhcp-client.c @@ -102,10 +102,13 @@ static uint16_t client_checksum(void *buf, int len) if (len & 0x01) { odd = buf; - sum += odd[len]; + sum += odd[len - 1]; } - return ~((sum & 0xffff) + (sum >> 16)); + while (sum >> 16) + sum = (sum & 0xffff) + (sum >> 16); + + return ~sum; } static void test_checksum(void)