The checksum algorithm used by IPv4, TCP and UDP allows a zero value
to be represented by either 0x0000 and 0xFFFF. But per RFC 768, a zero
UDP checksum must be transmitted as 0xFFFF, as 0x0000 is a special
value meaning no checksum.
Substitute 0xFFFF whenever a checksum is computed as zero on a UDP
datagram. Doing this on IPv4 packets and TCP segments is unnecessary
but legal.
(While it is tempting to make the substitution in
net_checksum_finish(), that function is also used by receivers to
verify checksums, and in that case the expected value is always
0x0000.)
Signed-off-by: Ed Swierk <address@hidden>
---
hw/net/e1000.c | 5 +++--
hw/net/net_rx_pkt.c | 3 +++
hw/net/net_tx_pkt.c | 6 ++++++
hw/net/vmxnet3.c | 7 +++++--
4 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/hw/net/e1000.c b/hw/net/e1000.c
index d642314..97242a1 100644
--- a/hw/net/e1000.c
+++ b/hw/net/e1000.c
@@ -505,8 +505,9 @@ putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t
css, uint32_t cse)
if (cse && cse < n)
n = cse + 1;
if (sloc < n-1) {
- sum = net_checksum_add(n-css, data+css);
- stw_be_p(data + sloc, net_checksum_finish(sum));
+ sum = net_raw_checksum(data + css, n - css);
+ /* For UDP, zero checksum must be sent as 0xFFFF */
+ stw_be_p(data + sloc, sum ? sum : 0xffff);
}
}
diff --git a/hw/net/net_rx_pkt.c b/hw/net/net_rx_pkt.c
index 1019b50..e820132 100644
--- a/hw/net/net_rx_pkt.c
+++ b/hw/net/net_rx_pkt.c
@@ -588,6 +588,9 @@ bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt)
/* Calculate L4 checksum */
csum = cpu_to_be16(_net_rx_pkt_calc_l4_csum(pkt));
+ if (!csum) {
+ csum = 0xFFFF; /* For UDP, zero checksum must be sent as 0xFFFF */
+ }