aboutsummaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
authorGuishan Qin <guishan.qin@windriver.com>2020-12-11 17:35:11 +0800
committerJason Wang <jasowang@redhat.com>2021-01-25 17:04:56 +0800
commitd97f11590a0f60cd911ace8bb68180b5a09a068d (patch)
treed0a26af268b2758793c5ec8eec0050771a9f591c /net
parent0dcf0c0aeefd2bc1023c9fe7ab0f1b6bc993c360 (diff)
downloadqemu-d97f11590a0f60cd911ace8bb68180b5a09a068d.zip
qemu-d97f11590a0f60cd911ace8bb68180b5a09a068d.tar.gz
qemu-d97f11590a0f60cd911ace8bb68180b5a09a068d.tar.bz2
net: checksum: Add IP header checksum calculation
At present net_checksum_calculate() only calculates TCP/UDP checksum in an IP packet, but assumes the IP header checksum to be provided by the software, e.g.: Linux kernel always calculates the IP header checksum. However this might not always be the case, e.g.: for an IP checksum offload enabled stack like VxWorks, the IP header checksum can be zero. This adds the checksum calculation of the IP header. Signed-off-by: Guishan Qin <guishan.qin@windriver.com> Signed-off-by: Yabing Liu <yabing.liu@windriver.com> Signed-off-by: Bin Meng <bin.meng@windriver.com> Reviewed-by: Cédric Le Goater <clg@kaod.org> Signed-off-by: Jason Wang <jasowang@redhat.com>
Diffstat (limited to 'net')
-rw-r--r--net/checksum.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/net/checksum.c b/net/checksum.c
index 5cb8b2c..dabd290 100644
--- a/net/checksum.c
+++ b/net/checksum.c
@@ -61,6 +61,7 @@ void net_checksum_calculate(uint8_t *data, int length)
{
int mac_hdr_len, ip_len;
struct ip_header *ip;
+ uint16_t csum;
/*
* Note: We cannot assume "data" is aligned, so the all code uses
@@ -106,6 +107,11 @@ void net_checksum_calculate(uint8_t *data, int length)
return; /* not IPv4 */
}
+ /* Calculate IP checksum */
+ stw_he_p(&ip->ip_sum, 0);
+ csum = net_raw_checksum((uint8_t *)ip, IP_HDR_GET_LEN(ip));
+ stw_be_p(&ip->ip_sum, csum);
+
if (IP4_IS_FRAGMENT(ip)) {
return; /* a fragmented IP packet */
}
@@ -122,7 +128,6 @@ void net_checksum_calculate(uint8_t *data, int length)
switch (ip->ip_p) {
case IP_PROTO_TCP:
{
- uint16_t csum;
tcp_header *tcp = (tcp_header *)(ip + 1);
if (ip_len < sizeof(tcp_header)) {
@@ -143,7 +148,6 @@ void net_checksum_calculate(uint8_t *data, int length)
}
case IP_PROTO_UDP:
{
- uint16_t csum;
udp_header *udp = (udp_header *)(ip + 1);
if (ip_len < sizeof(udp_header)) {