From: Bin Meng <bmeng.cn@gmail.com>
To: qemu-devel@nongnu.org
Cc: Bin Meng <bin.meng@windriver.com>,
Yabing Liu <yabing.liu@windriver.com>,
Jason Wang <jasowang@redhat.com>,
Guishan Qin <guishan.qin@windriver.com>
Subject: [PATCH 2/3] net: checksum: Add IP header checksum calculation
Date: Sun, 6 Dec 2020 10:14:06 +0800 [thread overview]
Message-ID: <1607220847-24096-2-git-send-email-bmeng.cn@gmail.com> (raw)
In-Reply-To: <1607220847-24096-1-git-send-email-bmeng.cn@gmail.com>
From: Guishan Qin <guishan.qin@windriver.com>
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 chcksum
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>
---
net/checksum.c | 8 ++++++--
1 file 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)) {
--
2.7.4
next prev parent reply other threads:[~2020-12-06 2:17 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-06 2:14 [PATCH 1/3] net: checksum: Skip fragmented IP packets Bin Meng
2020-12-06 2:14 ` Bin Meng [this message]
2020-12-06 2:14 ` [PATCH 3/3] net: checksum: Introduce fine control over checksum type Bin Meng
2020-12-06 11:50 ` Philippe Mathieu-Daudé
2020-12-06 12:44 ` Bin Meng
2020-12-09 8:33 ` Cédric Le Goater
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1607220847-24096-2-git-send-email-bmeng.cn@gmail.com \
--to=bmeng.cn@gmail.com \
--cc=bin.meng@windriver.com \
--cc=guishan.qin@windriver.com \
--cc=jasowang@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=yabing.liu@windriver.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).