Skip to contentSkip to conversation

Checksum of IP header

DISCUSSION John Que 4 participants 4 messages Jun 26, 2005 – Jun 28, 2005

Subsystems (2)
lore
Share & tools
lore

John Que

author
#0 lore raw

Hello,

I have the following problem

I am developing a server with filtering capabilities.

I had written a driver an I am using the netfilter API, using the nf_register_hook() and nf_unregister_hook() methods.

In certain occasions , when getting UDP packets on certain ports, I change the IP address of the destination in the iphdr of the socket buffer. (sk_buff)

Namely, what I change is :
skb->nh.iph->daddr = newIPAddress (in u32 ).

Everything works OK and the packets are sent to the new destination as I can see with the sniffer; except foo the csum.

I see in the sniffer that the csum of the ip header is wrong.

I tried to look in the /net/core and /net/ipv4/netfilter
folders to see which
csum methods to use.
There are plenty of methods: just for example , in skbuff.c there is
skb_checksum(),

       skb_copy_and_csum_bits(),skb_copy_and_csum_dev(),

and more; in ip_nat_core there is
ip_fast_csum(),csum_partial(),ip_compute_csum(),
and more. So I am quite confused.

I am not an expert on csum and nat , but this seems to me a task
that had been
done somewhere in the nat code (because as I understand part of what nat does is change the source ip addresses).

I made some tries but did not find a solution - all my tries ended in seeing a wrong csum in the ip header with the sniffer.

(I know that I can set the csum to 0 but I don't want to do it ).

Any ideas ?

Regards,
John

ReplyForward

YOSHIFUJI Hideaki / 吉藤英明
: > I see in the sniffer that the csum of the ip header is wrong. : > I made ...

	 	  4:59 am (2 hours ago)

John Que
to YOSHIFUJI Hide., netdev
More options 5:55 am (1 hour ago)
Thnks;

As I said I am sure that in the code somewhere there is
a method which does this but I cannot locate it because there are many csum methods.

If anybody knows a scenario like mine and can advice me
as to which methods to use it will be helpful.

John

#1 lore raw
Quoted 5 lines · John Que

Namely, what I change is :
skb->nh.iph->daddr = newIPAddress (in u32 ).

Everything works OK and the packets are sent to the new destination as I can see with the sniffer; except foo the csum.

Have a look at the ip_nat_cheat_check() function in ip_nat_core.c - it does exactly what you want.

//Marcus

Signature · 3 lines
---------------------------------------+--------------------------
  Marcus Sundberg <[email protected]>  | Firewalls with SIP & NAT
 Software Developer, Ingate Systems AB |  http://www.ingate.com/
#2 lore raw
In reply to John Que
Quoted 7 lines · John Que

Namely, what I change is :
skb->nh.iph->daddr = newIPAddress (in u32 ).

Everything works OK and the packets are sent to the new destination as I can see with the sniffer; except foo the csum.

I see in the sniffer that the csum of the ip header is wrong.

hi
i've the same problem not long ago, and i wrote the IPALTER patch, i think it can be usefull to you ;) i've attached it ;)
i think it can be extended to alter other thing in the ip header like tos, but currently i don't need it ;)

Quoted 2 lines

done somewhere in the nat code (because as I understand part of what nat does is change the source ip addresses).

but you must know, that the nat code doesn't know anything about what you have done, so the responses for the faked addresses doesn't get any usable data, use NETMAP, MASQUERADE for those purposes

my patch is only working for extreme situtatuions, where you must inform your clients that they mtu is too big, or a better route is existing(redirect) while you don't want to take(or can't) the router's ip ;)

sorry for my english ;)
best regards, kirk

extensions/.IPALTER-test +3 -0
diff -Naur iptables-1.3.1-orig/extensions/.IPALTER-test iptables-1.3.1/extensions/.IPALTER-test
@@ -0,0 +1,3 @@
+#!/bin/sh
+# True if IPALTER patch is applied.
+[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_IPALTER.h ] && echo IPALTER
extensions/libipt_IPALTER.c +146 -0

146 lines not on this page · raw message

include/linux/netfilter_ipv4/ipt_IPALTER.h +12 -0
diff -Naur linux-2.6.11.9-orig/include/linux/netfilter_ipv4/ipt_IPALTER.h linux-2.6.11.9/include/linux/netfilter_ipv4/ipt_IPALTER.h
@@ -0,0 +1,12 @@
+#ifndef _IPT_IPALTER_H_target
+#define _IPT_IPALTER_H_target
+
+//#include <linux/ip.h>
+#define	IPALTER_VERSION	"0.1"
+
+struct ipt_ipalter_target_info {
+	struct	iphdr	mask;
+	struct	iphdr	alter;
+};
+
+#endif /*_IPT_IPALTER_H_target*/
net/ipv4/netfilter/Kconfig +8 -0
diff -Naur linux-2.6.11.9-orig/net/ipv4/netfilter/Kconfig linux-2.6.11.9/net/ipv4/netfilter/Kconfig
@@ -566,6 +566,14 @@
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config IP_NF_TARGET_IPALTER
+	tristate "IPALTER target support"
+	depends on IP_NF_MANGLE
+	help
+	  This option adds a `IPALTER' target
+
+	  To compile it as a module, choose M here.  If unsure, say N.
+
 config IP_NF_TARGET_ECN
 	tristate "ECN target support"
 	depends on IP_NF_MANGLE
net/ipv4/netfilter/Makefile +1 -0
diff -Naur linux-2.6.11.9-orig/net/ipv4/netfilter/Makefile linux-2.6.11.9/net/ipv4/netfilter/Makefile
@@ -63,6 +63,7 @@
 # targets
 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
+obj-$(CONFIG_IP_NF_TARGET_IPALTER) += ipt_IPALTER.o
 obj-$(CONFIG_IP_NF_TARGET_ECN) += ipt_ECN.o
 obj-$(CONFIG_IP_NF_TARGET_DSCP) += ipt_DSCP.o
 obj-$(CONFIG_IP_NF_TARGET_MARK) += ipt_MARK.o
net/ipv4/netfilter/ipt_IPALTER.c +131 -0

131 lines not on this page · raw message

#3 lore raw
In reply to John Que
Quoted 7 lines · John Que

Namely, what I change is :
skb->nh.iph->daddr = newIPAddress (in u32 ).

Everything works OK and the packets are sent to the new destination as I can see with the sniffer; except foo the csum.

I see in the sniffer that the csum of the ip header is wrong.

hi
i've the same problem not long ago, and i wrote the IPALTER patch, i think it can be usefull to you ;) i've attached it ;)
i think it can be extended to alter other thing in the ip header like tos, but currently i don't need it ;)

Quoted 2 lines

done somewhere in the nat code (because as I understand part of what nat does is change the source ip addresses).

but you must know, that the nat code doesn't know anything about what you have done, so the responses for the faked addresses doesn't get any usable data, use NETMAP, MASQUERADE for those purposes

my patch is only working for extreme situtatuions, where you must inform your clients that they mtu is too big, or a better route is existing(redirect) while you don't want to take(or can't) the router's ip ;)

sorry for my english ;)
best regards, kirk

extensions/.IPALTER-test +3 -0
diff -Naur iptables-1.3.1-orig/extensions/.IPALTER-test iptables-1.3.1/extensions/.IPALTER-test
@@ -0,0 +1,3 @@
+#!/bin/sh
+# True if IPALTER patch is applied.
+[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_IPALTER.h ] && echo IPALTER
extensions/libipt_IPALTER.c +146 -0

146 lines not on this page · raw message

include/linux/netfilter_ipv4/ipt_IPALTER.h +12 -0
diff -Naur linux-2.6.11.9-orig/include/linux/netfilter_ipv4/ipt_IPALTER.h linux-2.6.11.9/include/linux/netfilter_ipv4/ipt_IPALTER.h
@@ -0,0 +1,12 @@
+#ifndef _IPT_IPALTER_H_target
+#define _IPT_IPALTER_H_target
+
+//#include <linux/ip.h>
+#define	IPALTER_VERSION	"0.1"
+
+struct ipt_ipalter_target_info {
+	struct	iphdr	mask;
+	struct	iphdr	alter;
+};
+
+#endif /*_IPT_IPALTER_H_target*/
net/ipv4/netfilter/Kconfig +8 -0
diff -Naur linux-2.6.11.9-orig/net/ipv4/netfilter/Kconfig linux-2.6.11.9/net/ipv4/netfilter/Kconfig
@@ -566,6 +566,14 @@
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config IP_NF_TARGET_IPALTER
+	tristate "IPALTER target support"
+	depends on IP_NF_MANGLE
+	help
+	  This option adds a `IPALTER' target
+
+	  To compile it as a module, choose M here.  If unsure, say N.
+
 config IP_NF_TARGET_ECN
 	tristate "ECN target support"
 	depends on IP_NF_MANGLE
net/ipv4/netfilter/Makefile +1 -0
diff -Naur linux-2.6.11.9-orig/net/ipv4/netfilter/Makefile linux-2.6.11.9/net/ipv4/netfilter/Makefile
@@ -63,6 +63,7 @@
 # targets
 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
+obj-$(CONFIG_IP_NF_TARGET_IPALTER) += ipt_IPALTER.o
 obj-$(CONFIG_IP_NF_TARGET_ECN) += ipt_ECN.o
 obj-$(CONFIG_IP_NF_TARGET_DSCP) += ipt_DSCP.o
 obj-$(CONFIG_IP_NF_TARGET_MARK) += ipt_MARK.o
net/ipv4/netfilter/ipt_IPALTER.c +131 -0

131 lines not on this page · raw message