[systemd-devel] [PATCH 06/11] libsystemd-dhcp: Fix checksum computation for buffer with odd size

Patrik Flykt patrik.flykt at linux.intel.com
Fri Dec 20 07:16:15 PST 2013


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.
---
 src/libsystemd-dhcp/dhcp-client.c      |    7 +++++--
 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 7dc1546..68a3b1a 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 d398510..7400cc6 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)
-- 
1.7.10.4



More information about the systemd-devel mailing list