net: Transmit zero UDP checksum as 0xFFFF

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 because 0x0000 is a special
value meaning no checksum.

Substitute 0xFFFF whenever a checksum is computed as zero when
modifying a UDP datagram header. Doing this on IPv4 and TCP checksums
is unnecessary but legal. Add a wrapper for net_checksum_finish() that
makes the substitution.

(We can't just change net_checksum_finish(), as 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 <eswierk@skyportsystems.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
This commit is contained in:
Ed Swierk 2017-11-16 06:06:06 -08:00 committed by Jason Wang
parent ebc2327f07
commit 0dacea92d2
5 changed files with 11 additions and 4 deletions

View File

@ -503,7 +503,7 @@ putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse)
n = cse + 1; n = cse + 1;
if (sloc < n-1) { if (sloc < n-1) {
sum = net_checksum_add(n-css, data+css); sum = net_checksum_add(n-css, data+css);
stw_be_p(data + sloc, net_checksum_finish(sum)); stw_be_p(data + sloc, net_checksum_finish_nozero(sum));
} }
} }

View File

@ -518,7 +518,7 @@ _net_rx_pkt_calc_l4_csum(struct NetRxPkt *pkt)
cntr += net_checksum_add_iov(pkt->vec, pkt->vec_len, cntr += net_checksum_add_iov(pkt->vec, pkt->vec_len,
pkt->l4hdr_off, csl, cso); pkt->l4hdr_off, csl, cso);
csum = net_checksum_finish(cntr); csum = net_checksum_finish_nozero(cntr);
trace_net_rx_pkt_l4_csum_calc_csum(pkt->l4hdr_off, csl, cntr, csum); trace_net_rx_pkt_l4_csum_calc_csum(pkt->l4hdr_off, csl, cntr, csum);

View File

@ -486,7 +486,7 @@ static void net_tx_pkt_do_sw_csum(struct NetTxPkt *pkt)
net_checksum_add_iov(iov, iov_len, pkt->virt_hdr.csum_start, csl, cso); net_checksum_add_iov(iov, iov_len, pkt->virt_hdr.csum_start, csl, cso);
/* Put the checksum obtained into the packet */ /* Put the checksum obtained into the packet */
csum = cpu_to_be16(net_checksum_finish(csum_cntr)); csum = cpu_to_be16(net_checksum_finish_nozero(csum_cntr));
iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum); iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum);
} }

View File

@ -972,7 +972,8 @@ static void vmxnet3_rx_need_csum_calculate(struct NetRxPkt *pkt,
data = (uint8_t *)pkt_data + vhdr->csum_start; data = (uint8_t *)pkt_data + vhdr->csum_start;
len = pkt_len - vhdr->csum_start; len = pkt_len - vhdr->csum_start;
/* Put the checksum obtained into the packet */ /* Put the checksum obtained into the packet */
stw_be_p(data + vhdr->csum_offset, net_raw_checksum(data, len)); stw_be_p(data + vhdr->csum_offset,
net_checksum_finish_nozero(net_checksum_add(len, data)));
vhdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; vhdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
vhdr->flags |= VIRTIO_NET_HDR_F_DATA_VALID; vhdr->flags |= VIRTIO_NET_HDR_F_DATA_VALID;

View File

@ -33,6 +33,12 @@ net_checksum_add(int len, uint8_t *buf)
return net_checksum_add_cont(len, buf, 0); return net_checksum_add_cont(len, buf, 0);
} }
static inline uint16_t
net_checksum_finish_nozero(uint32_t sum)
{
return net_checksum_finish(sum) ?: 0xFFFF;
}
static inline uint16_t static inline uint16_t
net_raw_checksum(uint8_t *data, int length) net_raw_checksum(uint8_t *data, int length)
{ {