chiark / gitweb /
libsystemd-dhcp: Fix checksum computation for buffer with odd size
authorPatrik Flykt <patrik.flykt@linux.intel.com>
Fri, 20 Dec 2013 15:16:15 +0000 (17:16 +0200)
committerTom Gundersen <teg@jklm.no>
Sun, 22 Dec 2013 13:28:00 +0000 (14:28 +0100)
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
src/libsystemd-dhcp/test-dhcp-client.c

index 7dc1546..68a3b1a 100644 (file)
@@ -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)
index d398510..7400cc6 100644 (file)
@@ -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)