diff options
author | Patrik Flykt <patrik.flykt@linux.intel.com> | 2013-12-20 17:16:15 +0200 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2013-12-22 14:28:00 +0100 |
commit | 0c6a3c888abd720b5ab28162d1ba4eadffde5272 (patch) | |
tree | 9f3ecc81db7317b071caf74d6a9563abb5c88860 | |
parent | 77e8d29dd2ecfa7f11c31c8d96021d323ab3fe3a (diff) |
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.
-rw-r--r-- | src/libsystemd-dhcp/dhcp-client.c | 7 | ||||
-rw-r--r-- | src/libsystemd-dhcp/test-dhcp-client.c | 7 |
2 files changed, 10 insertions, 4 deletions
diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c index 7dc1546128..68a3b1a1b6 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 d398510745..7400cc6837 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) |