net: handle optional VLAN header in checksum computation.
authorJean-Christophe Dubois <jcd@tribudubois.net>
Mon, 30 May 2016 17:25:48 +0000 (19:25 +0200)
committerJason Wang <jasowang@redhat.com>
Thu, 2 Jun 2016 02:42:46 +0000 (10:42 +0800)
Signed-off-by: Jean-Christophe Dubois <jcd@tribudubois.net>
Signed-off-by: Jason Wang <jasowang@redhat.com>
net/checksum.c

index 39ad73f4b4a547282b9dd1e076c38fe9220773ea..23323b07608a464b3d798646ca2a0184f4449bd2 100644 (file)
@@ -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 */