Skip to contentSkip to conversation

Fix some issues with non-linear packets

PATCH In review Martin Josefsson 2 participants 3 messages Jan 4, 2005 – Jan 5, 2005

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

PATCH 1/1 Fix some issues with non-linear packets

Hi Rusty

I've been playing with your latest nfsim stuff and found some issues with the kernel.
You mentioned that you found some problems, so you might already have fixed these.

Please add these patches to your patch-queue.

First is a patch to make ip_nat_core.c and ip_nat_proto_udp.c skb_ip_make_writable() the correct amount of bytes, sizeof(pointer) isn't any good. This makes nfsim stop complaining about incorrect checksums.

Second and third are two patches to make ip_nat_ftp.c / ip_nat_irc.c use skb_header_pointer instead of trying to access the tcpheader directly. ip_nat_mangle_tcp_packet() calls skb_ip_make_writable(pskb, (*pskb)->len) but that's too late.

nfsim is great but can be a little annoying sometimes, you fix one bug and then it finds another... :)

Signature · 1 line
/Martin
net/ipv4/netfilter/ip_nat_core.c +1 -1
@@ -670,7 +670,7 @@
 	struct iphdr *iph;
 
 	(*pskb)->nfcache |= NFC_ALTERED;
-	if (!skb_ip_make_writable(pskb, iphdroff+sizeof(iph)))
+	if (!skb_ip_make_writable(pskb, iphdroff+sizeof(struct iphdr)))
 		return 0;
 
 	iph = (void *)(*pskb)->data + iphdroff;
net/ipv4/netfilter/ip_nat_proto_udp.c +1 -1
@@ -93,7 +93,7 @@
 	u_int32_t oldip;
 	u_int16_t *portptr;
 
-	if (!skb_ip_make_writable(pskb, hdroff + sizeof(hdr)))
+	if (!skb_ip_make_writable(pskb, hdroff + sizeof(struct udphdr)))
 		return 0;
 
 	hdr = (void *)(*pskb)->data + hdroff;
net/ipv4/netfilter/ip_nat_proto_icmp.c +1 -1
@@ -61,7 +61,7 @@
 	struct icmphdr *hdr;
 	unsigned int hdroff = iphdroff + iph->ihl*4;
 
-	if (!skb_ip_make_writable(pskb, hdroff + sizeof(*hdr)))
+	if (!skb_ip_make_writable(pskb, hdroff + sizeof(struct icmphdr)))
 		return 0;
 
 	hdr = (void *)(*pskb)->data + hdroff;
net/ipv4/netfilter/ip_nat_ftp.c +25 -13
@@ -171,17 +171,16 @@
 			  struct ip_conntrack *ct,
 			  struct sk_buff **pskb,
 			  enum ip_conntrack_info ctinfo,
-			  struct ip_conntrack_expect *expect)
+			  struct ip_conntrack_expect *expect,
+			  struct tcphdr *th)
 {
 	u_int32_t newip;
-	struct iphdr *iph = (*pskb)->nh.iph;
-	struct tcphdr *tcph = (void *)iph + iph->ihl*4;
 	u_int16_t port;
 	struct ip_conntrack_tuple newtuple;
 
 	DEBUGP("FTP_NAT: seq %u + %u in %u\n",
 	       expect->seq, exp_ftp_info->len,
-	       ntohl(tcph->seq));
+	       ntohl(th->seq));
 
 	/* Change address inside packet to match way we're mapping
 	   this connection. */
@@ -218,7 +217,7 @@
 		return 0;
 
 	if (!mangle[exp_ftp_info->ftptype](pskb, newip, port,
-					  expect->seq - ntohl(tcph->seq),
+					  expect->seq - ntohl(th->seq),
 					  exp_ftp_info->len, ct, ctinfo))
 		return 0;
 
@@ -233,8 +232,8 @@
 			 struct sk_buff **pskb)
 {
 	struct iphdr *iph = (*pskb)->nh.iph;
-	struct tcphdr *tcph = (void *)iph + iph->ihl*4;
-	unsigned int datalen;
+	struct tcphdr *th, _tcph;
+	unsigned int dataoff, datalen;
 	int dir;
 	struct ip_ct_ftp_expect *exp_ftp_info;
 
@@ -256,12 +255,25 @@
 		return NF_ACCEPT;
 	}
 
-	datalen = (*pskb)->len - iph->ihl * 4 - tcph->doff * 4;
+
+	th = skb_header_pointer(*pskb, iph->ihl*4,
+				sizeof(_tcph), &_tcph);
+	if (th == NULL)
+		return NF_ACCEPT;
+
+	dataoff = iph->ihl*4 + th->doff*4;
+	/* No data? */
+	if (dataoff >= (*pskb)->len) {
+		DEBUGP("ftp: skblen = %u\n", (*pskb)->len);
+		return NF_ACCEPT;
+	}       
+	datalen = (*pskb)->len - dataoff;
+
 	/* If it's in the right range... */
 	if (between(exp->seq + exp_ftp_info->len,
-		    ntohl(tcph->seq),
-		    ntohl(tcph->seq) + datalen)) {
-		if (!ftp_data_fixup(exp_ftp_info, ct, pskb, ctinfo, exp))
+		    ntohl(th->seq),
+		    ntohl(th->seq) + datalen)) {
+		if (!ftp_data_fixup(exp_ftp_info, ct, pskb, ctinfo, exp, th))
 			return NF_DROP;
 	} else {
 		/* Half a match?  This means a partial retransmisison.
@@ -269,8 +281,8 @@
 		if (net_ratelimit()) {
 			printk("FTP_NAT: partial packet %u/%u in %u/%u\n",
 			       exp->seq, exp_ftp_info->len,
-			       ntohl(tcph->seq),
-			       ntohl(tcph->seq) + datalen);
+			       ntohl(th->seq),
+			       ntohl(th->seq) + datalen);
 		}
 		return NF_DROP;
 	}
net/ipv4/netfilter/ip_nat_irc.c +24 -13
@@ -89,12 +89,11 @@
 			  struct ip_conntrack *ct,
 			  struct sk_buff **pskb,
 			  enum ip_conntrack_info ctinfo,
-			  struct ip_conntrack_expect *expect)
+			  struct ip_conntrack_expect *expect,
+			  struct tcphdr *th)
 {
 	u_int32_t newip;
 	struct ip_conntrack_tuple t;
-	struct iphdr *iph = (*pskb)->nh.iph;
-	struct tcphdr *tcph = (void *) iph + iph->ihl * 4;
 	u_int16_t port;
 
 	/* "4294967296 65635 " */
@@ -102,7 +101,7 @@
 
 	DEBUGP("IRC_NAT: info (seq %u + %u) in %u\n",
 	       expect->seq, exp_irc_info->len,
-	       ntohl(tcph->seq));
+	       ntohl(th->seq));
 
 	newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
 
@@ -138,7 +137,7 @@
 	       buffer, NIPQUAD(newip), port);
 
 	return ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, 
-					expect->seq - ntohl(tcph->seq),
+					expect->seq - ntohl(th->seq),
 					exp_irc_info->len, buffer, 
 					strlen(buffer));
 }
@@ -151,8 +150,8 @@
 			 struct sk_buff **pskb)
 {
 	struct iphdr *iph = (*pskb)->nh.iph;
-	struct tcphdr *tcph = (void *) iph + iph->ihl * 4;
-	unsigned int datalen;
+	struct tcphdr *th, _tcph;
+	unsigned int dataoff, datalen;
 	int dir;
 	struct ip_ct_irc_expect *exp_irc_info;
 
@@ -175,12 +174,24 @@
 	}
 	DEBUGP("got beyond not touching\n");
 
-	datalen = (*pskb)->len - iph->ihl * 4 - tcph->doff * 4;
+	th = skb_header_pointer(*pskb, iph->ihl*4,
+				sizeof(_tcph), &_tcph);
+	if (th == NULL)
+		return NF_ACCEPT;
+
+	dataoff = iph->ihl*4 + th->doff*4;
+	/* No data? */
+	if (dataoff >= (*pskb)->len) {
+		DEBUGP("ftp: skblen = %u\n", (*pskb)->len);
+		return NF_ACCEPT;
+	}
+	datalen = (*pskb)->len - dataoff;
+
 	/* Check whether the whole IP/address pattern is carried in the payload */
 	if (between(exp->seq + exp_irc_info->len,
-		    ntohl(tcph->seq),
-		    ntohl(tcph->seq) + datalen)) {
-		if (!irc_data_fixup(exp_irc_info, ct, pskb, ctinfo, exp))
+		    ntohl(th->seq),
+		    ntohl(th->seq) + datalen)) {
+		if (!irc_data_fixup(exp_irc_info, ct, pskb, ctinfo, exp, th))
 			return NF_DROP;
 	} else { 
 		/* Half a match?  This means a partial retransmisison.
@@ -189,8 +200,8 @@
 			printk
 			    ("IRC_NAT: partial packet %u/%u in %u/%u\n",
 			     exp->seq, exp_irc_info->len,
-			     ntohl(tcph->seq),
-			     ntohl(tcph->seq) + datalen);
+			     ntohl(th->seq),
+			     ntohl(th->seq) + datalen);
 		}
 		return NF_DROP;
 	}
#1 lore raw
Quoted 6 lines · Martin Josefsson

Hi Rusty

I've been playing with your latest nfsim stuff and found some issues with the kernel.
You mentioned that you found some problems, so you might already have fixed these.

Yes, I think so. I was too tired to send them out last night, sorry.

Please re-examine, I'm about to send out 18 patches.

Rusty.

Signature · 1 line
A bad analogy is like a leaky screwdriver -- Richard Braakman
#2 lore raw
Quoted 4 lines · Rusty Russell
Quoted 2 lines

You mentioned that you found some problems, so you might already have fixed these.

Yes, I think so. I was too tired to send them out last night, sorry.

Great,

Quoted 1 line

Please re-examine, I'm about to send out 18 patches.

Didn't look very closely but they look good from what I saw. Just the issue of maybe add inverting to multiport r1 as noted by Phil.

Signature · 1 line
/Martin