diff options
author | Elliott Hughes <enh@google.com> | 2018-12-20 11:25:26 -0800 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2018-12-21 15:22:04 -0600 |
commit | 40e162e58dff93bb941340d832bca8e6ce368600 (patch) | |
tree | 3ac80facb3831ae6ddeff6caec0a117670385ba0 /toys | |
parent | 4f5717dd140e8769f770191bcd1e6ee1f4bbc6d8 (diff) | |
download | toybox-40e162e58dff93bb941340d832bca8e6ce368600.tar.gz |
Fix UDP checksum verification error.
From RFC 768, if UDP packet checksum computation yields a result of
zero, change it to hex 0xFFFF. The current udhcpc checksum verification
would yield false positive for this case. A better way is to compute the
checksum with the original checksum field and the result should be zero
for good udp packet.
Signed-off-by: Yangchun Fu <yangchun@google.com>
Diffstat (limited to 'toys')
-rw-r--r-- | toys/pending/dhcp.c | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/toys/pending/dhcp.c b/toys/pending/dhcp.c index b99bb4c0..25f9b913 100644 --- a/toys/pending/dhcp.c +++ b/toys/pending/dhcp.c @@ -650,7 +650,6 @@ static int mode_app(void) static int read_raw(void) { dhcp_raw_t packet; - uint16_t check; int bytes = 0; memset(&packet, 0, sizeof(packet)); @@ -676,18 +675,19 @@ static int read_raw(void) dbg("\tUnrelated/bogus packet, ignoring\n"); return -2; } - // verify IP checksum - check = packet.iph.check; - packet.iph.check = 0; - if (check != dhcp_checksum(&packet.iph, sizeof(packet.iph))) { + // Verify IP checksum. + if (dhcp_checksum(&packet.iph, sizeof(packet.iph)) != 0) { dbg("\tBad IP header checksum, ignoring\n"); return -2; } + // Verify UDP checksum. From RFC 768, the UDP checksum is done over the IPv4 + // pseudo header, the UDP header and the UDP data. The IPv4 pseudo header + // includes saddr, daddr, protocol, and UDP length. The IP header has to be + // modified for this. memset(&packet.iph, 0, ((size_t) &((struct iphdr *)0)->protocol)); + packet.iph.check = 0; packet.iph.tot_len = packet.udph.len; - check = packet.udph.check; - packet.udph.check = 0; - if (check && check != dhcp_checksum(&packet, bytes)) { + if (packet.udph.check != 0 && dhcp_checksum(&packet, bytes) != 0) { dbg("\tPacket with bad UDP checksum received, ignoring\n"); return -2; } |