Skip to contentSkip to conversation

skb_checksum_setup and NAT

DISCUSSION Christophe Saout 3 participants 4 messages Sep 28, 2006 – Sep 30, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello,

skb_checksum_setup/proto_csum_blank are causing a terrible mess with NAT. I just spent several hours debugging why passive FTP ip/port packets arrived on the client with a wrong TCP checksum. This also happens for all subsequent packets where the tcp sequence has to be changed.

It turns out that ip_nat_mangle_tcp computes the whole checksum and then dev_queue_xmit decides to compute it a second time and messes it up.

My network setup in Dom0 is as follows:

DomU/vif#.0 - xenbr0 - vif0.0/int0 - NAT/routing - eth0 (real)

Apart from that I already had to add nf_reset to the loopback code (for vif0.0/int0) in order to get NAT working at all (see
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=746 ), the packets arriving at the NAT code have proto_csum_blank set all the way from the DomU.

Why do you need this mess? Isn't the normal ip_summed sufficient? The patches don't contain any annotations.

It's not funny having to mess around in the NAT code. I can fix it up by adding even more #ifdef CONFIG_XEN to ignore all fixups related to the anything else than the 12 byte tcpudp magic, but I don't think this is a nice solution at all.

#1 lore raw
Quoted 2 lines · Christophe Saout

Why do you need this mess? Isn't the normal ip_summed sufficient? The patches don't contain any annotations.

The Linux network stack doesn't expect to be able to receive packets that have their protocol-checksum field empty. So we have to add extra packet flags to signal this situation. It does create something of a mess unless all parts of the network stack know what's going on. I heard that the networking maintainers were working on a more generic scheme that we should be able to make use of. Apart from that, the stack needs to make sure that the proto_csum_blank field is cleared whenever it fills in the full checksum.

For now, if it causes you problems and you just want stuff to work, you can run ethtool on your interface in domU: 'ethtool -K eth0 tx off'.

-- Keir

#2 lore raw

Am Donnerstag, den 28.09.2006, 18:35 +0100 schrieb Keir Fraser:

Quoted 8 lines

The Linux network stack doesn't expect to be able to receive packets that have their protocol-checksum field empty. So we have to add extra packet flags to signal this situation. It does create something of a mess unless all parts of the network stack know what's going on. I heard that the networking maintainers were working on a more generic scheme that we should be able to make use of. Apart from that, the stack needs to make sure that the proto_csum_blank field is cleared whenever it fills in the full checksum.

Ok, ip_forward e.g. sets ip_summed to CHECKSUM_NONE, so it indeed assumes the checksum is correctly set. This should probably just be changed accordingly to set CHECKSUM_HW when the incoming packet didn't have a valid checksum set (although the packet was accepted), possibly by adding a new CHECKSUM_xxx define.

You are probably talking about the work by Patrick McHardy that is being merged for post-2.6.18:

http://www.kernel.org/git/?p=linux%2Fkernel%2Fgit%2Ftorvalds%2Flinux-2.6.git&a=search&h=HEAD&s=checksum

It looks promising though it doesn't seem to address this problem in particular (?).

Quoted 2 lines

For now, if it causes you problems and you just want stuff to work, you can run ethtool on your interface in domU: 'ethtool -K eth0 tx off'.

I just ifdef'ed all places where the NAT code changed the checksum. I'm having a lot of network traffic between DomU's and I'd like to keep CPU usage down. If someone is interested in the patch, it's attached (just the FTP NAT part tested though).

net/ipv4/netfilter/ip_nat_helper.c +29 -3
diff -urp linux-2.6.18-cs1-xenU.orig/net/ipv4/netfilter/ip_nat_helper.c linux-2.6.18-cs1-xenU/net/ipv4/netfilter/ip_nat_helper.c
@@ -184,9 +184,18 @@ ip_nat_mangle_tcp_packet(struct sk_buff 
 			match_offset, match_len, rep_buffer, rep_len);
 
 	datalen = (*pskb)->len - iph->ihl*4;
-	tcph->check = 0;
-	tcph->check = tcp_v4_check(tcph, datalen, iph->saddr, iph->daddr,
-				   csum_partial((char *)tcph, datalen, 0));
+#ifdef CONFIG_XEN
+	if (!(*pskb)->proto_csum_blank) {
+#endif
+		tcph->check = 0;
+		tcph->check = tcp_v4_check(tcph, datalen, iph->saddr, iph->daddr,
+					   csum_partial((char *)tcph, datalen, 0));
+#ifdef CONFIG_XEN
+	} else if (rep_len != match_len)
+		tcph->check =
+			ip_nat_cheat_check(htons(datalen - rep_len + match_len),
+			                   htons(datalen) ^ 0xFFFF, tcph->check);
+#endif
 
 	if (rep_len != match_len) {
 		set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
@@ -245,6 +254,9 @@ ip_nat_mangle_udp_packet(struct sk_buff 
 	udph->len = htons((*pskb)->len - iph->ihl*4);
 
 	/* fix udp checksum if udp checksum was previously calculated */
+#ifdef CONFIG_XEN
+	if (!(*pskb)->proto_csum_blank) {
+#endif
 	if (udph->check) {
 		int datalen = (*pskb)->len - iph->ihl * 4;
 		udph->check = 0;
@@ -253,6 +265,14 @@ ip_nat_mangle_udp_packet(struct sk_buff 
 		                                csum_partial((char *)udph,
 		                                             datalen, 0));
 	}
+#ifdef CONFIG_XEN
+	} else if (rep_len != match_len && udph->check) {
+		int datalen = (*pskb)->len - iph->ihl * 4;
+		udph->check =
+			ip_nat_cheat_check(htons(datalen - rep_len + match_len),
+			                   htons(datalen) ^ 0xFFFF, udph->check);
+	}
+#endif
 
 	return 1;
 }
@@ -293,6 +313,9 @@ sack_adjust(struct sk_buff *skb,
 			ntohl(sack->start_seq), new_start_seq,
 			ntohl(sack->end_seq), new_end_seq);
 
+#ifdef CONFIG_XEN
+		if (!skb->proto_csum_blank)
+#endif
 		tcph->check = 
 			ip_nat_cheat_check(~sack->start_seq, new_start_seq,
 					   ip_nat_cheat_check(~sack->end_seq, 
@@ -381,6 +404,9 @@ ip_nat_seq_adjust(struct sk_buff **pskb,
 		newack = ntohl(tcph->ack_seq) - other_way->offset_before;
 	newack = htonl(newack);
 
+#ifdef CONFIG_XEN
+	if (!(*pskb)->proto_csum_blank)
+#endif
 	tcph->check = ip_nat_cheat_check(~tcph->seq, newseq,
 					 ip_nat_cheat_check(~tcph->ack_seq, 
 					 		    newack, 
net/ipv4/netfilter/ipt_ECN.c +3 -0
diff -urp linux-2.6.18-cs1-xenU.orig/net/ipv4/netfilter/ipt_ECN.c linux-2.6.18-cs1-xenU/net/ipv4/netfilter/ipt_ECN.c
@@ -82,6 +82,9 @@ set_ect_tcp(struct sk_buff **pskb, const
 	diffs[1] = ((u_int16_t *)tcph)[6];
 	diffs[0] = diffs[0] ^ 0xFFFF;
 
+#ifdef CONFIG_XEN
+	if (!(*pskb)->proto_csum_blank)
+#endif
 	if ((*pskb)->ip_summed != CHECKSUM_UNNECESSARY)
 		tcph->check = csum_fold(csum_partial((char *)diffs,
 						     sizeof(diffs),
net/ipv4/netfilter/ipt_REJECT.c +4 -0
diff -urp linux-2.6.18-cs1-xenU.orig/net/ipv4/netfilter/ipt_REJECT.c linux-2.6.18-cs1-xenU/net/ipv4/netfilter/ipt_REJECT.c
@@ -183,6 +183,10 @@ static void send_reset(struct sk_buff *o
 	tcph->window = 0;
 	tcph->urg_ptr = 0;
 
+#ifdef CONFIG_XEN
+	nskb->proto_csum_blank = 0;
+#endif
+
 	/* Adjust TCP checksum */
 	tcph->check = 0;
 	tcph->check = tcp_v4_check(tcph, sizeof(struct tcphdr),

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw
Quoted 8 lines · Christophe Saout

You are probably talking about the work by Patrick McHardy that is being merged for post-2.6.18:

http://www.kernel.org/git/?p=linux%2Fkernel%2Fgit%2Ftorvalds%2Flinux-2.6.git&a=search&h=HEAD&s=checksum

It looks promising though it doesn't seem to address this problem in particular (?).

This is the basis for a solution. Once CHECKSUM_HW is split into the TX and RX variants, you can then use them directly in the stack which allows us to accept packets with partial checksums.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt