[ovs-dev] [1.4 backports 3/6] datapath: Fix checksum update for actions on UDP packets.

Ben Pfaff blp at nicira.com
Wed Oct 24 19:47:39 UTC 2012


From: Jesse Gross <jesse at nicira.com>

When modifying IP addresses or ports on a UDP packet we don't
correctly follow the rules for unchecksummed packets.  This meant
that packets without a checksum can be given a incorrect new checksum
and packets with a checksum can become marked as being unchecksummed.
This fixes it to handle those requirements.

This is a crossport of commit 55ce87bcd542cc26def11000c9dee7690b7c3155
from master.

Bug #8937.
Signed-off-by: Jesse Gross <jesse at nicira.com>
Acked-by: Ben Pfaff <blp at nicira.com>
---
 datapath/actions.c                             |   45 +++++++++++++++++------
 datapath/linux/Modules.mk                      |    1 +
 datapath/linux/compat/include/linux/checksum.h |   10 +++++
 3 files changed, 44 insertions(+), 12 deletions(-)
 create mode 100644 datapath/linux/compat/include/linux/checksum.h

diff --git a/datapath/actions.c b/datapath/actions.c
index 824791d..57e6bcb 100644
--- a/datapath/actions.c
+++ b/datapath/actions.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007-2011 Nicira Networks.
+ * Copyright (c) 2007-2012 Nicira Networks.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public
@@ -147,9 +147,17 @@ static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
 			inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
 						 *addr, new_addr, 1);
 	} else if (nh->protocol == IPPROTO_UDP) {
-		if (likely(transport_len >= sizeof(struct udphdr)))
-			inet_proto_csum_replace4(&udp_hdr(skb)->check, skb,
-						 *addr, new_addr, 1);
+		if (likely(transport_len >= sizeof(struct udphdr))) {
+			struct udphdr *uh = udp_hdr(skb);
+
+			if (uh->check ||
+			    get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
+				inet_proto_csum_replace4(&uh->check, skb,
+							 *addr, new_addr, 1);
+				if (!uh->check)
+					uh->check = CSUM_MANGLED_0;
+			}
+		}
 	}
 
 	csum_replace4(&nh->check, *addr, new_addr);
@@ -199,8 +207,22 @@ static void set_tp_port(struct sk_buff *skb, __be16 *port,
 	skb_clear_rxhash(skb);
 }
 
-static int set_udp_port(struct sk_buff *skb,
-			const struct ovs_key_udp *udp_port_key)
+static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
+{
+	struct udphdr *uh = udp_hdr(skb);
+
+	if (uh->check && get_ip_summed(skb) != OVS_CSUM_PARTIAL) {
+		set_tp_port(skb, port, new_port, &uh->check);
+
+		if (!uh->check)
+			uh->check = CSUM_MANGLED_0;
+	} else {
+		*port = new_port;
+		skb_clear_rxhash(skb);
+	}
+}
+
+static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
 {
 	struct udphdr *uh;
 	int err;
@@ -212,16 +234,15 @@ static int set_udp_port(struct sk_buff *skb,
 
 	uh = udp_hdr(skb);
 	if (udp_port_key->udp_src != uh->source)
-		set_tp_port(skb, &uh->source, udp_port_key->udp_src, &uh->check);
+		set_udp_port(skb, &uh->source, udp_port_key->udp_src);
 
 	if (udp_port_key->udp_dst != uh->dest)
-		set_tp_port(skb, &uh->dest, udp_port_key->udp_dst, &uh->check);
+		set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
 
 	return 0;
 }
 
-static int set_tcp_port(struct sk_buff *skb,
-			const struct ovs_key_tcp *tcp_port_key)
+static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
 {
 	struct tcphdr *th;
 	int err;
@@ -334,11 +355,11 @@ static int execute_set_action(struct sk_buff *skb,
 		break;
 
 	case OVS_KEY_ATTR_TCP:
-		err = set_tcp_port(skb, nla_data(nested_attr));
+		err = set_tcp(skb, nla_data(nested_attr));
 		break;
 
 	case OVS_KEY_ATTR_UDP:
-		err = set_udp_port(skb, nla_data(nested_attr));
+		err = set_udp(skb, nla_data(nested_attr));
 		break;
 	}
 
diff --git a/datapath/linux/Modules.mk b/datapath/linux/Modules.mk
index 1f9973b..baaebb0 100644
--- a/datapath/linux/Modules.mk
+++ b/datapath/linux/Modules.mk
@@ -10,6 +10,7 @@ openvswitch_sources += \
 	linux/compat/skbuff-openvswitch.c \
 	linux/compat/time.c
 openvswitch_headers += \
+	linux/compat/include/linux/checksum.h \
 	linux/compat/include/linux/compiler.h \
 	linux/compat/include/linux/compiler-gcc.h \
 	linux/compat/include/linux/cpumask.h \
diff --git a/datapath/linux/compat/include/linux/checksum.h b/datapath/linux/compat/include/linux/checksum.h
new file mode 100644
index 0000000..1d4fefc
--- /dev/null
+++ b/datapath/linux/compat/include/linux/checksum.h
@@ -0,0 +1,10 @@
+#ifndef __LINUX_CHECKSUM_WRAPPER_H
+#define __LINUX_CHECKSUM_WRAPPER_H 1
+
+#include_next <linux/checksum.h>
+
+#ifndef CSUM_MANGLED_0
+#define CSUM_MANGLED_0 ((__force __sum16)0xffff)
+#endif
+
+#endif
-- 
1.7.2.5




More information about the dev mailing list