From patchwork Thu Jun 2 06:48:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 629075 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rKzLC411Tz9t3m for ; Thu, 2 Jun 2016 17:26:27 +1000 (AEST) Received: from localhost ([::1]:45792 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8N1N-0000xq-LD for incoming@patchwork.ozlabs.org; Thu, 02 Jun 2016 03:26:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47555) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8MRx-0004SH-Sx for qemu-devel@nongnu.org; Thu, 02 Jun 2016 02:49:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8MRv-0005Yj-L4 for qemu-devel@nongnu.org; Thu, 02 Jun 2016 02:49:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58144) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8MRv-0005YT-Ev for qemu-devel@nongnu.org; Thu, 02 Jun 2016 02:49:47 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0677685364; Thu, 2 Jun 2016 06:49:47 +0000 (UTC) Received: from jason-ThinkPad-T450s.redhat.com (vpn1-4-234.pek2.redhat.com [10.72.4.234]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u526mQG9005724; Thu, 2 Jun 2016 02:49:45 -0400 From: Jason Wang To: peter.maydell@linaro.org, qemu-devel@nongnu.org Date: Thu, 2 Jun 2016 14:48:14 +0800 Message-Id: <1464850102-17829-24-git-send-email-jasowang@redhat.com> In-Reply-To: <1464850102-17829-1-git-send-email-jasowang@redhat.com> References: <1464850102-17829-1-git-send-email-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 02 Jun 2016 06:49:47 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL V4 23/31] net: handle optional VLAN header in checksum computation. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang , Jean-Christophe Dubois Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Jean-Christophe Dubois Signed-off-by: Jean-Christophe Dubois Signed-off-by: Jason Wang --- net/checksum.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/net/checksum.c b/net/checksum.c index 39ad73f..23323b0 100644 --- a/net/checksum.c +++ b/net/checksum.c @@ -55,7 +55,7 @@ uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, void net_checksum_calculate(uint8_t *data, int length) { - int ip_len; + int mac_hdr_len, ip_len; struct ip_header *ip; /* @@ -64,12 +64,39 @@ void net_checksum_calculate(uint8_t *data, int length) * struct members (just in case). */ - /* Ensure data has complete L2 & L3 headers. */ - if (length < (sizeof(struct eth_header) + sizeof(struct ip_header))) { + /* Ensure we have at least an Eth header */ + if (length < sizeof(struct eth_header)) { return; } - ip = (struct ip_header *)(data + sizeof(struct eth_header)); + /* Handle the optionnal VLAN headers */ + switch (lduw_be_p(&PKT_GET_ETH_HDR(data)->h_proto)) { + case ETH_P_VLAN: + mac_hdr_len = sizeof(struct eth_header) + + sizeof(struct vlan_header); + break; + case ETH_P_DVLAN: + if (lduw_be_p(&PKT_GET_VLAN_HDR(data)->h_proto) == ETH_P_VLAN) { + mac_hdr_len = sizeof(struct eth_header) + + 2 * sizeof(struct vlan_header); + } else { + mac_hdr_len = sizeof(struct eth_header) + + sizeof(struct vlan_header); + } + break; + default: + mac_hdr_len = sizeof(struct eth_header); + break; + } + + length -= mac_hdr_len; + + /* Now check we have an IP header (with an optionnal VLAN header) */ + if (length < sizeof(struct ip_header)) { + return; + } + + ip = (struct ip_header *)(data + mac_hdr_len); if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) { return; /* not IPv4 */