chiark / gitweb /
libsystemd-dhcp: Fix checksum computation for buffer with odd size
[elogind.git] / src / libsystemd-dhcp / dhcp-client.c
index 7dc154612816ac3902070cce9bbecdba76efe5f4..68a3b1a1b64c319db8d186091c88530c4c2f1605 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)