diff mbox

[v3,2/5] net: handle optional VLAN header in checksum computation.

Message ID ba335b8b410d26197e423ca854f483a67e631d63.1462736067.git.jcd@tribudubois.net (mailing list archive)
State New, archived
Headers show

Commit Message

Jean-Christophe Dubois May 8, 2016, 7:41 p.m. UTC
Signed-off-by: Jean-Christophe Dubois <jcd@tribudubois.net>
---

Changes since v1:
 * Not present on v1
 
Changes since v2:
 * Not present on v2

 net/checksum.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)

Comments

Jason Wang May 18, 2016, 3:26 a.m. UTC | #1
On 2016?05?09? 03:41, Jean-Christophe Dubois wrote:
> Signed-off-by: Jean-Christophe Dubois <jcd@tribudubois.net>
> ---
>
> Changes since v1:
>   * Not present on v1
>   
> Changes since v2:
>   * Not present on v2
>
>   net/checksum.c | 35 +++++++++++++++++++++++++++++++----
>   1 file changed, 31 insertions(+), 4 deletions(-)
>
> diff --git a/net/checksum.c b/net/checksum.c
> index f62b18a..8acd997 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 ip_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:
> +        ip_hdr_len = sizeof(struct eth_header) +
> +                     sizeof(struct vlan_header);

Then "mac_hdr_len" seems much better than "ip_hdr_len".

> +        break;
> +    case ETH_P_DVLAN:
> +        if (lduw_be_p(&PKT_GET_VLAN_HDR(data)->h_proto) == ETH_P_VLAN) {
> +            ip_hdr_len = sizeof(struct eth_header) +
> +                         2 * sizeof(struct vlan_header);
> +        } else {
> +            ip_hdr_len = sizeof(struct eth_header) +
> +                         sizeof(struct vlan_header);
> +        }
> +        break;
> +    default:
> +        ip_hdr_len = sizeof(struct eth_header);
> +        break;
> +    }
> +
> +    length -= ip_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 + ip_hdr_len);
>   
>       if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
>           return; /* not IPv4 */
diff mbox

Patch

diff --git a/net/checksum.c b/net/checksum.c
index f62b18a..8acd997 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 ip_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:
+        ip_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) {
+            ip_hdr_len = sizeof(struct eth_header) +
+                         2 * sizeof(struct vlan_header);
+        } else {
+            ip_hdr_len = sizeof(struct eth_header) +
+                         sizeof(struct vlan_header);
+        }
+        break;
+    default:
+        ip_hdr_len = sizeof(struct eth_header);
+        break;
+    }
+
+    length -= ip_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 + ip_hdr_len);
 
     if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
         return; /* not IPv4 */