Skip to contentSkip to conversation

patch for conntrack expectations

DISCUSSION Harald Welte 3 participants 25 messages Feb 17, 2004 – Mar 9, 2004 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 5 lines · Pablo Neira

Hi harald!
So I decided to implement the function:

int ip_conntrack_alloc_expect(struct ip_conntrack_expect **new)

Quoted 8 lines

This function is exported. It's called from the help(...) function when the pattern is found.
It creates an expectation which will be manipulated by the helper and which will be inserted by ip_conntrack_expect_related(...).

This way we don't need to work with a fake expectation and pass it to ip_conntrack_expect_related which will copy the information to the real ip_conntrack_expect_related.

This is indeed a true observation and good suggestion.

Hoever, I'm not sure whether it would make any measurable difference. The expect was allocated on the stack [which is cheap], and now we have one additional (inter-module) function call instead of the allocation of a couple of bytes on the stack. I'm not sure which one is really worse.

Also, 2.4.x kernel series are heading for a freeze pretty soon. So if at all, such a patch would be accepted for 2.6.x. But then I would like to ask you to change all the helpers (esp. the ones in patch-o-matic-ng), too.

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
#2 lore raw
Quoted 8 lines · Harald Welte

This is indeed a true observation and good suggestion.

Hoever, I'm not sure whether it would make any measurable difference. The expect was allocated on the stack [which is cheap], and now we have one additional (inter-module) function call instead of the allocation of a couple of bytes on the stack. I'm not sure which one is really worse.

well, sorry because I'm going to be a bit repetitive but this way everyone's going to understand my patch clearly.

in some steps:

1) helper function is called
2) a pointer to a ip_conntrack_expect structure is allocated on the stack
3) if the pattern which we are looking for is *not* found
-> return NF_ACCEPT
4) else
-> a ip_conntrack_expect structure is allocated in memory
5) all the fields of the expectation are fulfilled
6) helper calls ip_conntrack_expect_related to insert the expectation but it passes a pointer to a structure allocated in memory.

what's the different between this approach and the current one?

If the pattern which we are looking for is not found, nothing will change, we just allocated less memory on the stack but of course this doesn't improve the performance, so it's the same.

By other hand, in the current approach if the pattern is found, the function ip_conntrack_expect_related does a memcpy of a structure of 116 bytes (expectation) from the stack to memory. This is not needed anymore since with the new approach the helper works with the future expectation already allocated in memory.

I also think that this will result in a cleaner API to manage the expectations. well, maybe you could also use this to allocate an expectation for the connection tracking replication (well... i just started talking so much).

Quoted 3 lines

Also, 2.4.x kernel series are heading for a freeze pretty soon.

yes it's true

Quoted 5 lines

So if at all, such a patch would be accepted for 2.6.x. But then I would like to ask you to change all the helpers (esp. the ones in patch-o-matic-ng), too.

I had a look at almost all the helpers which are in patch-o-matic-ng (even the pptp helper). I have no problem about modifying them. BTW, the documentation should be updated as well if this change is applied.

Pablo

#3 lore raw
Quoted 5 lines · Pablo Neira

By other hand, in the current approach if the pattern is found, the function ip_conntrack_expect_related does a memcpy of a structure of 116 bytes (expectation) from the stack to memory. This is not needed anymore since with the new approach the helper works with the future expectation already allocated in memory.

Ok, I missed that part.

Quoted 4 lines

I also think that this will result in a cleaner API to manage the expectations. well, maybe you could also use this to allocate an expectation for the connection tracking replication (well... i just started talking so much).

Yes, ct_sync would also need an allocation function anyway.

Quoted 3 lines

I had a look at almost all the helpers which are in patch-o-matic-ng (even the pptp helper). I have no problem about modifying them. BTW, the documentation should be updated as well if this change is applied.

Yes, indeed.

Would you be willing to submit a 2.6.x merge, together with updates to the existing helpers?

Thanks.

Quoted 1 line

Pablo

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
#4 lore raw
Quoted 4 lines · Harald Welte

Would you be willing to submit a 2.6.x merge, together with updates to the existing helpers?

yes, no problem. I'll work on those patches this weekend. More news soon.

best regards,
Pablo

#5 lore raw
In reply to Harald Welte

Hi Harald and list!

I finished my patch to add an ip_conntrack_expect_alloc(...) function. I also patched all the current conntrack helpers available in the stable branch and patch-o-matic-ng.

Some considerations:

- I tested the irc, tftp and ftp helpers, they work fine for me. I haven't tested the others since I don't have access to all those protocols (I mean, I would need more time to install some programs and do some testing), but I have no problem about fixing problems related to those patches.

- I saw that Patrick submitted recently a patch to clean up the amanda helper (BTW, nice work!), I tried to patch my kernel with his patch available in patch-o-matic but it failed. So I patched the current. I also have no problem about patching the new one.

BTW, I know that there's a skeleton of a helper in the documention, due to they mostly look the same but I think that I could add it more details.

- There's a memset in expect_alloc (see expect.patch) after the allocation is done, I don't like it anyway but I just added to keep backward compatibility because some helper usually do it. I think that it's better setting all the fields of the new expectation in the helper (this implies being a bit more restrictive with the programmer but well, this is kernel programming anyway, it's restrictive :-)).

I could delete that memset and have a look at all the helpers to make sure that all the fields are correctly set.

- I didn't exported the expect_insert function, but you could do it later to use it in the ct_sync code.

- I found a condition where expect_related could succesfully insert an expectaction when removing an old one and return EPERM (see expect_related-newnat14.patch). I think that this doesn't make sense. Am I forgetting something? That patch fix that problem.

- I also have no problem about updating the documentation to add info about expect_alloc.

cheers,
Pablo

net/ipv4/netfilter/ip_conntrack_core.c +49 -31
diff -Nru --exclude .depend --exclude '*.o' --exclude '*.ko' --exclude '*.ver' --exclude '.*.flags' --exclude '*.orig' --exclude '*.rej' --exclude '*.cmd' --exclude '*.mod.c' --exclude '*~' linux-2.6.3-old/net/ipv4/netfilter/ip_conntrack_core.c linux-2.6.3/net/ipv4/netfilter/ip_conntrack_core.c
@@ -917,11 +917,53 @@
 	WRITE_UNLOCK(&ip_conntrack_lock);
 }
 
+int
+ip_conntrack_expect_alloc(struct ip_conntrack_expect **new)
+{
+        *new = (struct ip_conntrack_expect *)
+              kmalloc(sizeof(struct ip_conntrack_expect), GFP_ATOMIC);
+        if (!*new) {
+                DEBUGP("expect_related: OOM allocating expect\n");
+                return -ENOMEM;
+        }
+
+	/* is this important? */
+	memset(*new, 0, sizeof(struct ip_conntrack_expect));
+
+        return 1;
+}
+
+void
+ip_conntrack_expect_insert(struct ip_conntrack_expect *new,
+               struct ip_conntrack *related_to)
+{
+       DEBUGP("new expectation %p of conntrack %p\n", new, related_to);
+       new->expectant = related_to;
+       new->sibling = NULL;
+       atomic_set(&new->use, 1);
+
+       /* add to expected list for this connection */
+       list_add(&new->expected_list, &related_to->sibling_list);
+       /* add to global list of expectations */
+
+       list_prepend(&ip_conntrack_expect_list, &new->list);
+       /* add and start timer if required */
+       if (related_to->helper->timeout) {
+               init_timer(&new->timeout);
+               new->timeout.data = (unsigned long)new;
+               new->timeout.function = expectation_timed_out;
+               new->timeout.expires = jiffies +
+                                       related_to->helper->timeout * HZ;
+               add_timer(&new->timeout);
+       }
+       related_to->expecting++;
+}
+
 /* Add a related connection. */
 int ip_conntrack_expect_related(struct ip_conntrack *related_to,
 				struct ip_conntrack_expect *expect)
 {
-	struct ip_conntrack_expect *old, *new;
+	struct ip_conntrack_expect *old;
 	int ret = 0;
 
 	WRITE_LOCK(&ip_conntrack_lock);
@@ -953,6 +995,7 @@
 
 		if (old) {
 			WRITE_UNLOCK(&ip_conntrack_lock);
+			kfree(expect);
 			return -EEXIST;
 		}
 	} else if (related_to->helper->max_expected && 
@@ -971,6 +1014,7 @@
 				       related_to->helper->name,
  		    	       	       NIPQUAD(related_to->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip),
  		    	       	       NIPQUAD(related_to->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip));
+			kfree(expect);
 			return -EPERM;
 		}
 		DEBUGP("ip_conntrack: max number of expected "
@@ -1004,43 +1048,17 @@
 		 * related_to->expecting. 
 		 */
 		unexpect_related(old);
-		ret = -EPERM;
 	} else if (LIST_FIND(&ip_conntrack_expect_list, expect_clash,
 			     struct ip_conntrack_expect *, &expect->tuple, 
 			     &expect->mask)) {
 		WRITE_UNLOCK(&ip_conntrack_lock);
 		DEBUGP("expect_related: busy!\n");
+
+		kfree(expect);
 		return -EBUSY;
 	}
-	
-	new = (struct ip_conntrack_expect *) 
-	      kmalloc(sizeof(struct ip_conntrack_expect), GFP_ATOMIC);
-	if (!new) {
-		WRITE_UNLOCK(&ip_conntrack_lock);
-		DEBUGP("expect_relaed: OOM allocating expect\n");
-		return -ENOMEM;
-	}
-	
-	DEBUGP("new expectation %p of conntrack %p\n", new, related_to);
-	memcpy(new, expect, sizeof(*expect));
-	new->expectant = related_to;
-	new->sibling = NULL;
-	atomic_set(&new->use, 1);
-	
-	/* add to expected list for this connection */	
-	list_add(&new->expected_list, &related_to->sibling_list);
-	/* add to global list of expectations */
-	list_prepend(&ip_conntrack_expect_list, &new->list);
-	/* add and start timer if required */
-	if (related_to->helper->timeout) {
-		init_timer(&new->timeout);
-		new->timeout.data = (unsigned long)new;
-		new->timeout.function = expectation_timed_out;
-		new->timeout.expires = jiffies + 
-					related_to->helper->timeout * HZ;
-		add_timer(&new->timeout);
-	}
-	related_to->expecting++;
+
+	ip_conntrack_expect_insert(expect, related_to);
 
 	WRITE_UNLOCK(&ip_conntrack_lock);
 
net/ipv4/netfilter/ip_conntrack_standalone.c +1 -0
diff -Nru --exclude .depend --exclude '*.o' --exclude '*.ko' --exclude '*.ver' --exclude '.*.flags' --exclude '*.orig' --exclude '*.rej' --exclude '*.cmd' --exclude '*.mod.c' --exclude '*~' linux-2.6.3-old/net/ipv4/netfilter/ip_conntrack_standalone.c linux-2.6.3/net/ipv4/netfilter/ip_conntrack_standalone.c
@@ -497,6 +497,7 @@
 EXPORT_SYMBOL(ip_ct_find_proto);
 EXPORT_SYMBOL(__ip_ct_find_proto);
 EXPORT_SYMBOL(ip_ct_find_helper);
+EXPORT_SYMBOL_GPL(ip_conntrack_expect_alloc);
 EXPORT_SYMBOL(ip_conntrack_expect_related);
 EXPORT_SYMBOL(ip_conntrack_change_expect);
 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
include/linux/netfilter_ipv4/ip_conntrack_helper.h +4 -0
diff -Nru --exclude .depend --exclude '*.o' --exclude '*.ko' --exclude '*.ver' --exclude '.*.flags' --exclude '*.orig' --exclude '*.rej' --exclude '*.cmd' --exclude '*.mod.c' --exclude '*~' linux-2.6.3-old/include/linux/netfilter_ipv4/ip_conntrack_helper.h linux-2.6.3/include/linux/netfilter_ipv4/ip_conntrack_helper.h
@@ -35,6 +35,10 @@
 
 extern struct ip_conntrack_helper *ip_ct_find_helper(const struct ip_conntrack_tuple *tuple);
 
+
+/* Allocate space for an expectation: this is mandatory before calling 
+   ip_conntrack_expect_related. */
+extern int ip_conntrack_expect_alloc(struct ip_conntrack_expect **new);
 /* Add an expected connection: can have more than one per connection */
 extern int ip_conntrack_expect_related(struct ip_conntrack *related_to,
 				       struct ip_conntrack_expect *exp);
ip_conntrack_core.c renamed from ip_conntrack_core.old +0 -1
@@ -1004,7 +1004,6 @@
 		 * related_to->expecting. 
 		 */
 		unexpect_related(old);
-		ret = -EPERM;
 	} else if (LIST_FIND(&ip_conntrack_expect_list, expect_clash,
 			     struct ip_conntrack_expect *, &expect->tuple, 
 			     &expect->mask)) {
net/ipv4/netfilter/ip_conntrack_amanda.c +15 -13
diff -Nru --exclude .depend --exclude '*.o' --exclude '*.ko' --exclude '*.ver' --exclude '.*.flags' --exclude '*.orig' --exclude '*.rej' --exclude '*.cmd' --exclude '*.mod.c' --exclude '*~' linux-2.6.3-old/net/ipv4/netfilter/ip_conntrack_amanda.c linux-2.6.3/net/ipv4/netfilter/ip_conntrack_amanda.c
@@ -104,18 +104,20 @@
 		char *match = strstr(data, conns[i]);
 		if (match) {
 			char *portchr;
-			struct ip_conntrack_expect expect;
-			struct ip_ct_amanda_expect *exp_amanda_info =
-				&expect.help.exp_amanda_info;
+			struct ip_conntrack_expect *expect;
+			struct ip_ct_amanda_expect *exp_amanda_info;
 
-			memset(&expect, 0, sizeof(expect));
+			if (ip_conntrack_expect_alloc(&expect) < 0)
+				return -ENOMEM;
 
+			exp_amanda_info = &expect->help.exp_amanda_info;
+			
 			data += strlen(conns[i]);
 			/* this is not really tcp, but let's steal an
 			 * idea from a tcp stream helper :-) */
 			// XXX expect.seq = data - amanda_buffer;
 			exp_amanda_info->offset = data - amanda_buffer;
-// XXX DEBUGP("expect.seq = %p - %p = %d\n", data, amanda_buffer, expect.seq);
+// XXX DEBUGP("expect->seq = %p - %p = %d\n", data, amanda_buffer, expect->seq);
 DEBUGP("exp_amanda_info->offset = %p - %p = %d\n", data, amanda_buffer, exp_amanda_info->offset);
 			portchr = data;
 			exp_amanda_info->port = simple_strtoul(data, &data,10);
@@ -129,26 +131,26 @@
 			       "%u found\n", conns[i],
 			       exp_amanda_info->port);
 
-			expect.tuple = ((struct ip_conntrack_tuple)
+			expect->tuple = ((struct ip_conntrack_tuple)
 				{ { ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip,
 				    { 0 } },
 				  { ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip,
 				    { htons(exp_amanda_info->port) },
 				    IPPROTO_TCP }});
-			expect.mask = ((struct ip_conntrack_tuple)
+			expect->mask = ((struct ip_conntrack_tuple)
 				{ { 0, { 0 } },
 				  { 0xFFFFFFFF, { 0xFFFF }, 0xFFFF }});
 
-			expect.expectfn = NULL;
+			expect->expectfn = NULL;
 
 			DEBUGP ("ip_conntrack_amanda_help: "
 				"expect_related: %u.%u.%u.%u:%u - "
 				"%u.%u.%u.%u:%u\n",
-				NIPQUAD(expect.tuple.src.ip),
-				ntohs(expect.tuple.src.u.tcp.port),
-				NIPQUAD(expect.tuple.dst.ip),
-				ntohs(expect.tuple.dst.u.tcp.port));
-			if (ip_conntrack_expect_related(ct, &expect)
+				NIPQUAD(expect->tuple.src.ip),
+				ntohs(expect->tuple.src.u.tcp.port),
+				NIPQUAD(expect->tuple.dst.ip),
+				ntohs(expect->tuple.dst.u.tcp.port));
+			if (ip_conntrack_expect_related(ct, expect)
 			    == -EEXIST) {
 				;
 				/* this must be a packet being resent */
ip_conntrack_egg.c renamed from ip_conntrack_egg.old +11 -10
@@ -87,7 +87,7 @@
 	u_int32_t datalen = tcplen - tcph->doff * 4;
 	int dir = CTINFO2DIR(ctinfo);
 	int bytes_scanned = 0;
-	struct ip_conntrack_expect exp;
+	struct ip_conntrack_expect *exp;
 
 	u_int32_t egg_ip;
 	u_int16_t egg_port;
@@ -142,7 +142,8 @@
 			return NF_ACCEPT;
 		}
 
-		memset(&exp, 0, sizeof(exp));
+		if (ip_conntrack_expect_alloc(&exp) < 0)
+			return -ENOMEM;
 
 		if (ct->tuplehash[dir].tuple.src.ip != htonl(egg_ip)) {
 			if (net_ratelimit())
@@ -153,20 +154,20 @@
 			return NF_ACCEPT;
 		}
 
-		exp.tuple.src.ip = iph->daddr;
-		exp.tuple.src.u.tcp.port = 0;
-		exp.tuple.dst.ip = htonl(egg_ip);
-		exp.tuple.dst.u.tcp.port = htons(egg_port);
-		exp.tuple.dst.protonum = IPPROTO_TCP;
+		exp->tuple.src.ip = iph->daddr;
+		exp->tuple.src.u.tcp.port = 0;
+		exp->tuple.dst.ip = htonl(egg_ip);
+		exp->tuple.dst.u.tcp.port = htons(egg_port);
+		exp->tuple.dst.protonum = IPPROTO_TCP;
 
-		exp.mask.dst.u.tcp.port = 0xffff;
-		exp.mask.dst.protonum = 0xffff;
+		exp->mask.dst.u.tcp.port = 0xffff;
+		exp->mask.dst.protonum = 0xffff;
 
 		DEBUGP("expect_related %u.%u.%u.%u:%u - %u.%u.%u.%u:%u\n",
 			NIPQUAD(t.src.ip), ntohs(t.src.u.tcp.port),
 			NIPQUAD(t.dst.ip), ntohs(t.dst.u.tcp.port));
 
-		ip_conntrack_expect_related(ct, &exp);
+		ip_conntrack_expect_related(ct, exp);
 		break;
 	}
 	return NF_ACCEPT;
net/ipv4/netfilter/ip_conntrack_ftp.c +9 -5
diff -Nru --exclude .depend --exclude '*.o' --exclude '*.ko' --exclude '*.ver' --exclude '.*.flags' --exclude '*.orig' --exclude '*.rej' --exclude '*.cmd' --exclude '*.mod.c' --exclude '*~' linux-2.6.3-old/net/ipv4/netfilter/ip_conntrack_ftp.c linux-2.6.3/net/ipv4/netfilter/ip_conntrack_ftp.c
@@ -256,8 +256,8 @@
 	int dir = CTINFO2DIR(ctinfo);
 	unsigned int matchlen, matchoff;
 	struct ip_ct_ftp_master *ct_ftp_info = &ct->help.ct_ftp_info;
-	struct ip_conntrack_expect expect, *exp = &expect;
-	struct ip_ct_ftp_expect *exp_ftp_info = &exp->help.exp_ftp_info;
+	struct ip_conntrack_expect *exp;
+	struct ip_ct_ftp_expect *exp_ftp_info;
 
 	unsigned int i;
 	int found = 0;
@@ -346,8 +346,12 @@
 	DEBUGP("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
 	       (int)matchlen, data + matchoff,
 	       matchlen, ntohl(tcph.seq) + matchoff);
-	       
-	memset(&expect, 0, sizeof(expect));
+
+	/* Allocate expectation which will be inserted */
+	if (ip_conntrack_expect_alloc(&exp) < 0)
+		return -ENOMEM;
+
+	exp_ftp_info = &exp->help.exp_ftp_info;
 
 	/* Update the ftp info */
 	if (htonl((array[0] << 24) | (array[1] << 16) | (array[2] << 8) | array[3])
@@ -389,7 +393,7 @@
 	exp->expectfn = NULL;
 
 	/* Ignore failure; should only happen with NAT */
-	ip_conntrack_expect_related(ct, &expect);
+	ip_conntrack_expect_related(ct, exp);
 	ret = NF_ACCEPT;
  out:
 	UNLOCK_BH(&ip_ftp_lock);
ip_conntrack_h323.c renamed from ip_conntrack_h323.old +14 -6
@@ -47,8 +47,8 @@
 	u_int32_t datalen = tcplen - tcph->doff * 4;
 	int dir = CTINFO2DIR(ctinfo);
 	struct ip_ct_h225_master *info = &ct->help.ct_h225_info;
-	struct ip_conntrack_expect expect, *exp = &expect;
-	struct ip_ct_h225_expect *exp_info = &exp->help.exp_h225_info;
+	struct ip_conntrack_expect *exp;
+	struct ip_ct_h225_expect *exp_info = NULL;
 	u_int16_t data_port;
 	u_int32_t data_ip;
 	unsigned int i;
@@ -91,7 +91,12 @@
 		data_ip = *((u_int32_t *)data);
 		if (data_ip == iph->saddr) {
 			data_port = *((u_int16_t *)(data + 4));
-			memset(&expect, 0, sizeof(expect));
+
+			if (ip_conntrack_expect_alloc(&exp) < 0)
+				return -ENOMEM;
+
+			exp_info = &exp->help.exp_h225_info;
+			
 			/* update the H.225 info */
 			DEBUGP("ct_h245_help: new RTCP/RTP requested %u.%u.%u.%u:->%u.%u.%u.%u:%u\n",
 				NIPQUAD(ct->tuplehash[!dir].tuple.src.ip),
@@ -164,8 +169,8 @@
 	u_int32_t datalen = tcplen - tcph->doff * 4;
 	int dir = CTINFO2DIR(ctinfo);
 	struct ip_ct_h225_master *info = &ct->help.ct_h225_info;
-	struct ip_conntrack_expect expect, *exp = &expect;
-	struct ip_ct_h225_expect *exp_info = &exp->help.exp_h225_info;
+	struct ip_conntrack_expect *exp;
+	struct ip_ct_h225_expect *exp_info = NULL;
 	u_int16_t data_port;
 	u_int32_t data_ip;
 	unsigned int i;
@@ -222,7 +227,10 @@
 				UNLOCK_BH(&ip_h323_lock);
 #endif
 			} else {
-				memset(&expect, 0, sizeof(expect));
+				if (ip_conntrack_expect_alloc(&exp) < 0)
+					return -ENOMEM;
+
+				exp_info = &exp->help.exp_h225_info;
 
 				/* update the H.225 info */
 				LOCK_BH(&ip_h323_lock);
net/ipv4/netfilter/ip_conntrack_irc.c +8 -5
diff -Nru --exclude .depend --exclude '*.o' --exclude '*.ko' --exclude '*.ver' --exclude '.*.flags' --exclude '*.orig' --exclude '*.rej' --exclude '*.cmd' --exclude '*.mod.c' --exclude '*~' linux-2.6.3-old/net/ipv4/netfilter/ip_conntrack_irc.c linux-2.6.3/net/ipv4/netfilter/ip_conntrack_irc.c
@@ -106,8 +106,8 @@
 	struct tcphdr tcph;
 	char *data, *data_limit;
 	int dir = CTINFO2DIR(ctinfo);
-	struct ip_conntrack_expect expect, *exp = &expect;
-	struct ip_ct_irc_expect *exp_irc_info = &exp->help.exp_irc_info;
+	struct ip_conntrack_expect *exp;
+	struct ip_ct_irc_expect *exp_irc_info = NULL;
 
 	u_int32_t dcc_ip;
 	u_int16_t dcc_port;
@@ -190,8 +190,11 @@
 
 				continue;
 			}
-			
-			memset(&expect, 0, sizeof(expect));
+
+			if (ip_conntrack_expect_alloc(&exp) < 0)
+				return -ENOMEM;
+
+			exp_irc_info = &exp->help.exp_irc_info;
 
 			/* save position of address in dcc string,
 			 * necessary for NAT */
@@ -218,7 +221,7 @@
 				NIPQUAD(exp->tuple.dst.ip),
 				ntohs(exp->tuple.dst.u.tcp.port));
 
-			ip_conntrack_expect_related(ct, &expect);
+			ip_conntrack_expect_related(ct, exp);
 
 			goto out;
 		} /* for .. NUM_DCCPROTO */
ip_conntrack_mms.c renamed from ip_conntrack_mms.old +7 -4
@@ -150,8 +150,8 @@
 	unsigned int tcplen = len - iph->ihl * 4;
 	unsigned int datalen = tcplen - tcph->doff * 4;
 	int dir = CTINFO2DIR(ctinfo);
-	struct ip_conntrack_expect expect, *exp = &expect; 
-	struct ip_ct_mms_expect *exp_mms_info = &exp->help.exp_mms_info;
+	struct ip_conntrack_expect *exp; 
+	struct ip_ct_mms_expect *exp_mms_info = NULL;
 	
 	u_int32_t mms_ip;
 	u_int16_t mms_proto;
@@ -207,7 +207,10 @@
 				printk(KERN_WARNING
 				       "ip_conntrack_mms: Unable to parse data payload\n");
 
-		memset(&expect, 0, sizeof(expect));
+		if (ip_conntrack_expect_alloc(&exp) < 0)
+			return -ENOMEM;
+
+		exp_mms_info = &exp->help.exp_mms_info;
 
 		sprintf(mms_proto_string, "(%u)", mms_proto);
 		DEBUGP("ip_conntrack_mms: adding %s expectation %u.%u.%u.%u -> %u.%u.%u.%u:%u\n",
@@ -243,7 +246,7 @@
 		             { { 0xFFFFFFFF, { 0 } },
 		               { 0xFFFFFFFF, { .tcp = { 0xFFFF } }, 0xFFFF }});
 		exp->expectfn = NULL;
-		ip_conntrack_expect_related(ct, &expect);
+		ip_conntrack_expect_related(ct, exp);
 		UNLOCK_BH(&ip_mms_lock);
 	}
 
ip_conntrack_pptp.c renamed from ip_conntrack_pptp.old +41 -39

113 lines not on this page · raw message

ip_conntrack_quake3.c renamed from ip_conntrack_quake3.old +7 -6
@@ -58,7 +58,7 @@
 {
 	struct udphdr *udph = (void *)iph + iph->ihl * 4;
 	int dir = CTINFO2DIR(ctinfo);
-	struct ip_conntrack_expect exp;
+	struct ip_conntrack_expect *exp;
 	int i;
 	
         /* Until there's been traffic both ways, don't look in packets. note: it's UDP ! */
@@ -77,20 +77,21 @@
 			       NIPQUAD( (u_int32_t) *( (u_int32_t *)( (int)udph + i ) ) ), 
 			       ntohs((__u16) *( (__u16 *)( (int)udph + i + 4 ) ) ) );
 
-			memset(&exp, 0, sizeof(exp));
+			if (ip_conntrack_expect_alloc(&exp) < 0)
+				return -ENOMEM;
 
-			exp.tuple = ((struct ip_conntrack_tuple)
+			exp->tuple = ((struct ip_conntrack_tuple)
 			             { { ct->tuplehash[!dir].tuple.src.ip, { 0 } },
 			               { (u_int32_t) *((u_int32_t *)((int)udph + i)), 
 			               { .udp = { (__u16) *((__u16 *)((int)udph+i+4)) } }, 
 			                 IPPROTO_UDP } }
 			            );
-			exp.mask  = ((struct ip_conntrack_tuple)
+			exp->mask  = ((struct ip_conntrack_tuple)
 			             { { 0xFFFFFFFF, { 0 } },
 		                       { 0xFFFFFFFF, { .udp = { 0xFFFF } }, 0xFFFF }});
-			exp.expectfn = NULL;
+			exp->expectfn = NULL;
 
-			ip_conntrack_expect_related(ct, &exp);
+			ip_conntrack_expect_related(ct, exp);
 		}
 
 	}
ip_conntrack_rtsp.old renamed from ip_conntrack_rtsp.c +23 -22

79 lines not on this page · raw message

ip_conntrack_talk.c renamed from ip_conntrack_talk.old +8 -5
@@ -94,8 +94,8 @@
 		              struct talk_addr *addr)
 {
 	int dir = CTINFO2DIR(ctinfo);
-	struct ip_conntrack_expect expect, *exp = &expect;
-	struct ip_ct_talk_expect *exp_talk_info = &exp->help.exp_talk_info;
+	struct ip_conntrack_expect *exp;
+	struct ip_ct_talk_expect *exp_talk_info = NULL;
 
 	DEBUGP("ip_ct_talk_help_response: %u.%u.%u.%u:%u, type %d answer %d\n",
 		NIPQUAD(addr->ta_addr), ntohs(addr->ta_port),
@@ -103,8 +103,11 @@
 
 	if (!(answer == SUCCESS && type == mode))
 		return NF_ACCEPT;
-	
-	memset(&expect, 0, sizeof(expect));
+
+	if (ip_conntrack_expect_alloc(&exp) < 0)
+		return -ENOMEM;
+
+	exp_talk_info = &exp->help.exp_talk_info;
 	
 	if (type == ANNOUNCE) {
 
@@ -132,7 +135,7 @@
 		       NIPQUAD(exp->tuple.dst.ip), ntohs(exp->tuple.dst.u.udp.port));
 
 		/* Ignore failure; should only happen with NAT */
-		ip_conntrack_expect_related(ct, &expect);
+		ip_conntrack_expect_related(ct, exp);
 		UNLOCK_BH(&ip_talk_lock);
 	}
 	if (type == LOOK_UP) {
net/ipv4/netfilter/ip_conntrack_tftp.c +13 -11
diff -Nru --exclude .depend --exclude '*.o' --exclude '*.ko' --exclude '*.ver' --exclude '.*.flags' --exclude '*.orig' --exclude '*.rej' --exclude '*.cmd' --exclude '*.mod.c' --exclude '*~' linux-2.6.3-old/net/ipv4/netfilter/ip_conntrack_tftp.c linux-2.6.3/net/ipv4/netfilter/ip_conntrack_tftp.c
@@ -44,7 +44,7 @@
 		     enum ip_conntrack_info ctinfo)
 {
 	struct tftphdr tftph;
-	struct ip_conntrack_expect exp;
+	struct ip_conntrack_expect *exp;
 
 	if (skb_copy_bits(skb, skb->nh.iph->ihl * 4 + sizeof(struct udphdr),
 			  &tftph, sizeof(tftph)) != 0)
@@ -57,19 +57,21 @@
 		DEBUGP("");
 		DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
 		DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
-		memset(&exp, 0, sizeof(exp));
 
-		exp.tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
-		exp.mask.src.ip = 0xffffffff;
-		exp.mask.dst.ip = 0xffffffff;
-		exp.mask.dst.u.udp.port = 0xffff;
-		exp.mask.dst.protonum = 0xffff;
-		exp.expectfn = NULL;
+		if (ip_conntrack_expect_alloc(&exp) < 0)
+			return -ENOMEM;
+
+		exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
+		exp->mask.src.ip = 0xffffffff;
+		exp->mask.dst.ip = 0xffffffff;
+		exp->mask.dst.u.udp.port = 0xffff;
+		exp->mask.dst.protonum = 0xffff;
+		exp->expectfn = NULL;
 
 		DEBUGP("expect: ");
-		DUMP_TUPLE(&exp.tuple);
-		DUMP_TUPLE(&exp.mask);
-		ip_conntrack_expect_related(ct, &exp);
+		DUMP_TUPLE(&exp->tuple);
+		DUMP_TUPLE(&exp->mask);
+		ip_conntrack_expect_related(ct, exp);
 		break;
 	default:
 		DEBUGP("Unknown opcode\n");
#6 lore raw
Quoted 1 line · Pablo Neira

Hi Harald and list!

Thanks pablo.

I am fine with that patch, and I'd like to put it in patch-o-matic for now. As the changes are fairly obvious, I hope we won't get too much trouble with it ;) However, please see my following comments.

Quoted 3 lines

I finished my patch to add an ip_conntrack_expect_alloc(...) function. I also patched all the current conntrack helpers available in the stable branch and patch-o-matic-ng.

thanks. This means that once we get it into the core kernel, we should definitely have the first pom-ng release out, otherwise people will still use the old API.

This makes me think of another issue. We should either rename the
function, reorder arguments, do whatever. We cannot just make all old
(unpatched) code compile without any warning, and still have it crash :( I think it's best to exchange the order of the two arguments in ip_conntrack_expect_related(). This way a compiler would complain if some old helper was compiled with the new core.

Quoted 4 lines

- I saw that Patrick submitted recently a patch to clean up the amanda helper (BTW, nice work!), I tried to patch my kernel with his patch available in patch-o-matic but it failed. So I patched the current. I also have no problem about patching the new one.

I thought this was included already in the stock kernel. At least in 2.4.x the patch went in. Patrick, can you comment on this?

Quoted 2 lines

BTW, I know that there's a skeleton of a helper in the documention, due to they mostly look the same but I think that I could add it more details.

yes, please. Use the SGML master file, don't edit the HTML

Quoted 6 lines

- There's a memset in expect_alloc (see expect.patch) after the allocation is done, I don't like it anyway but I just added to keep backward compatibility because some helper usually do it. I think that it's better setting all the fields of the new expectation in the helper (this implies being a bit more restrictive with the programmer but well, this is kernel programming anyway, it's restrictive :-)).

yes, sometimes code like this is included mainly for debugging purpose and then left behind.

Quoted 2 lines

I could delete that memset and have a look at all the helpers to make sure that all the fields are correctly set.

The issue is not very easy in this case. Assume that tuple->dst.u is 32bits wide (like after applying the pptp patch). This in turn means that every helper has to explicitly take care not to initialize tuple->dst.u with a 16bit tcp/udp port number, since it could end up in the 'other' 16bit half. Rather, they have to assign tuple->dst.u.tcp.port. Those cases should all be fixed by now.

Now look what tuple_cmp does. It memcmp's the whole union, not just the individual fields. This means that a tuple always has to be cleanly initialized with zero, even in those parts that don't contain any useful information.

So for now, I'd like to keep that memset until all the code is audited with that regard.

Quoted 2 lines

- I didn't exported the expect_insert function, but you could do it later to use it in the ct_sync code.

that's fine.

Quoted 8 lines

- I found a condition where expect_related could succesfully insert an expectaction when removing an old one and return EPERM (see expect_related-newnat14.patch). I think that this doesn't make sense. Am I forgetting something? That patch fix that problem.

- I also have no problem about updating the documentation to add info about expect_alloc.

ok, please do so.

One issue with your patch: It doesn't seem to use tab's for indentation, but rather 8 spaces. Please clean this up.

Also, we cannot export that symbol GPL only, since it doesn't add a new API but rather replaces an old one.

Quoted 2 lines

cheers,
Pablo

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
#7 lore raw
Quoted 8 lines · Harald Welte
Quoted 2 lines · Pablo Neira

- I saw that Patrick submitted recently a patch to clean up the amanda
helper (BTW, nice work!), I tried to patch my kernel with his patch available in patch-o-matic but it failed. So I patched the current. I also have no problem about patching the new one.

I thought this was included already in the stock kernel. At least in 2.4.x the patch went in. Patrick, can you comment on this?

The 2.4 patch went in, the 2.6 didn't. Some of the problems got fixed in 2.6 when removing skb_linearize, but some didn't. Do you want me to resubmit the patch to you ?

Regards
Patrick

#8 lore raw
Quoted 3 lines · Patrick McHardy

The 2.4 patch went in, the 2.6 didn't. Some of the problems got fixed in 2.6 when removing skb_linearize, but some didn't. Do you want me to resubmit the patch to you ?

yes, please.

Quoted 2 lines

Regards
Patrick

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
#9 lore raw
Quoted 9 lines · Harald Welte
Quoted 3 lines · Patrick McHardy

The 2.4 patch went in, the 2.6 didn't. Some of the problems got fixed in 2.6 when removing skb_linearize, but some didn't. Do you want me to resubmit the patch to you ?

yes, please.

Old patch attached, it still applies cleanly.

Quoted 5 lines
Quoted 3 lines

Regards
Patrick

# This is a BitKeeper generated patch for the following project:
# Project Name: Linux kernel tree
# This patch format is intended for GNU patch command version 2.5 or higher. # This patch includes the following deltas:
# ChangeSet 1.1356 -> 1.1357
# include/linux/netfilter_ipv4/ip_conntrack_amanda.h 1.2 -> 1.3
# net/ipv4/netfilter/ip_nat_amanda.c 1.5 -> 1.6
# net/ipv4/netfilter/ip_conntrack_amanda.c 1.4 -> 1.5
#
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 03/09/28 [email protected] 1.1357
# [NETFILTER]: Fix amanda helpers
# --------------------------------------------
#

include/linux/netfilter_ipv4/ip_conntrack_amanda.h +2 -11
diff -Nru a/include/linux/netfilter_ipv4/ip_conntrack_amanda.h b/include/linux/netfilter_ipv4/ip_conntrack_amanda.h
@@ -2,20 +2,11 @@
 #define _IP_CONNTRACK_AMANDA_H
 /* AMANDA tracking. */
 
-#ifdef __KERNEL__
-
-#include <linux/netfilter_ipv4/lockhelp.h>
-
-/* Protects amanda part of conntracks */
-DECLARE_LOCK_EXTERN(ip_amanda_lock);
-
-#endif
-
 struct ip_ct_amanda_expect
 {
 	u_int16_t port;		/* port number of this expectation */
-	u_int16_t offset;	/* offset of the port specification in ctrl packet */
-	u_int16_t len;		/* the length of the port number specification */
+	u_int16_t offset;	/* offset of port in ctrl packet */
+	u_int16_t len;		/* length of the port number string */
 };
 
 #endif /* _IP_CONNTRACK_AMANDA_H */
net/ipv4/netfilter/ip_conntrack_amanda.c +39 -109

208 lines not on this page · raw message

net/ipv4/netfilter/ip_nat_amanda.c +41 -118

251 lines not on this page · raw message

#23 lore raw
In reply to Harald Welte

Hi harald,

attached with this email last version of patches and some comments.

Quoted 8 lines · Harald Welte

Thanks pablo.

I am fine with that patch, and I'd like to put it in patch-o-matic for now. As the changes are fairly obvious, I hope we won't get too much trouble with it ;) However, please see my following comments.

ok, feedback is always welcome. :-)

Quoted 8 lines
Quoted 3 lines

I finished my patch to add an ip_conntrack_expect_alloc(...) function. I also patched all the current conntrack helpers available in the stable branch and patch-o-matic-ng.

thanks. This means that once we get it into the core kernel, we should definitely have the first pom-ng release out, otherwise people will still use the old API.

Quoted 9 lines

This makes me think of another issue. We should either rename the
function, reorder arguments, do whatever. We cannot just make all old
(unpatched) code compile without any warning, and still have it crash :( I think it's best to exchange the order of the two arguments in ip_conntrack_expect_related(). This way a compiler would complain if some old helper was compiled with the new core.

As you suggested, I modified the order of the arguments in expect_related.

Quoted 7 lines
Quoted 3 lines

BTW, I know that there's a skeleton of a helper in the documention, due to they mostly look the same but I think that I could add it more details.

yes, please. Use the SGML master file, don't edit the HTML

ok! I will.

Quoted 21 lines
Quoted 4 lines

- There's a memset in expect_alloc (see expect.patch) after the
allocation is done, I don't like it anyway but I just added to keep backward compatibility because some helper usually do it. I think that it's better setting all the fields of the new expectation in the helper (this implies being a bit more restrictive with the programmer but well, this is kernel programming anyway, it's restrictive :-)).

yes, sometimes code like this is included mainly for debugging purpose and then left behind.

Quoted 3 lines

I could delete that memset and have a look at all the helpers to make sure that all the fields are correctly set.

The issue is not very easy in this case. Assume that tuple->dst.u is 32bits wide (like after applying the pptp patch). This in turn means that every helper has to explicitly take care not to initialize tuple->dst.u with a 16bit tcp/udp port number, since it could end up in the 'other' 16bit half. Rather, they have to assign tuple->dst.u.tcp.port. Those cases should all be fixed by now.

Quoted 9 lines

Now look what tuple_cmp does. It memcmp's the whole union, not just the individual fields. This means that a tuple always has to be cleanly initialized with zero, even in those parts that don't contain any useful information.

So for now, I'd like to keep that memset until all the code is audited with that regard.

ok, i'll study this issue calmly.

Quoted 8 lines
Quoted 4 lines

- I found a condition where expect_related could succesfully insert an
expectaction when removing an old one and return EPERM (see expect_related-newnat14.patch). I think that this doesn't make sense. Am I forgetting something? That patch fix that problem.

Quoted 8 lines
Quoted 4 lines

- I also have no problem about updating the documentation to add info about expect_alloc.

ok, please do so.

ok! I will.

Quoted 4 lines

One issue with your patch: It doesn't seem to use tab's for indentation, but rather 8 spaces. Please clean this up.

it's true, I think that it was because of some copy&paste issues related to vim and the use of cat. I'll pay attention to this kind of stuff in future since it seems that they are not sensible to indentation. I think these patches don't have this problem.

Quoted 5 lines

Also, we cannot export that symbol GPL only, since it doesn't add a new API but rather replaces an old one.

also fixed!

If these patches are ok, I'll also prepare the patches for the pom-ng helpers.

Pablo

P.D: BTW, so should I patch the amanda helper against patrick patch?

net/ipv4/netfilter/ip_conntrack_amanda.c +15 -13
@@ -104,18 +104,20 @@
 		char *match = strstr(data, conns[i]);
 		if (match) {
 			char *portchr;
-			struct ip_conntrack_expect expect;
-			struct ip_ct_amanda_expect *exp_amanda_info =
-				&expect.help.exp_amanda_info;
+			struct ip_conntrack_expect *expect;
+			struct ip_ct_amanda_expect *exp_amanda_info;
 
-			memset(&expect, 0, sizeof(expect));
+			if (ip_conntrack_expect_alloc(&expect) < 0)
+				return -ENOMEM;
 
+			exp_amanda_info = &expect->help.exp_amanda_info;
+			
 			data += strlen(conns[i]);
 			/* this is not really tcp, but let's steal an
 			 * idea from a tcp stream helper :-) */
 			// XXX expect.seq = data - amanda_buffer;
 			exp_amanda_info->offset = data - amanda_buffer;
-// XXX DEBUGP("expect.seq = %p - %p = %d\n", data, amanda_buffer, expect.seq);
+// XXX DEBUGP("expect->seq = %p - %p = %d\n", data, amanda_buffer, expect->seq);
 DEBUGP("exp_amanda_info->offset = %p - %p = %d\n", data, amanda_buffer, exp_amanda_info->offset);
 			portchr = data;
 			exp_amanda_info->port = simple_strtoul(data, &data,10);
@@ -129,26 +131,26 @@
 			       "%u found\n", conns[i],
 			       exp_amanda_info->port);
 
-			expect.tuple = ((struct ip_conntrack_tuple)
+			expect->tuple = ((struct ip_conntrack_tuple)
 				{ { ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip,
 				    { 0 } },
 				  { ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip,
 				    { htons(exp_amanda_info->port) },
 				    IPPROTO_TCP }});
-			expect.mask = ((struct ip_conntrack_tuple)
+			expect->mask = ((struct ip_conntrack_tuple)
 				{ { 0, { 0 } },
 				  { 0xFFFFFFFF, { 0xFFFF }, 0xFFFF }});
 
-			expect.expectfn = NULL;
+			expect->expectfn = NULL;
 
 			DEBUGP ("ip_conntrack_amanda_help: "
 				"expect_related: %u.%u.%u.%u:%u - "
 				"%u.%u.%u.%u:%u\n",
-				NIPQUAD(expect.tuple.src.ip),
-				ntohs(expect.tuple.src.u.tcp.port),
-				NIPQUAD(expect.tuple.dst.ip),
-				ntohs(expect.tuple.dst.u.tcp.port));
-			if (ip_conntrack_expect_related(ct, &expect)
+				NIPQUAD(expect->tuple.src.ip),
+				ntohs(expect->tuple.src.u.tcp.port),
+				NIPQUAD(expect->tuple.dst.ip),
+				ntohs(expect->tuple.dst.u.tcp.port));
+			if (ip_conntrack_expect_related(expect, ct)
 			    == -EEXIST) {
 				;
 				/* this must be a packet being resent */
net/ipv4/netfilter/ip_conntrack_ftp.c +9 -5
@@ -256,8 +256,8 @@
 	int dir = CTINFO2DIR(ctinfo);
 	unsigned int matchlen, matchoff;
 	struct ip_ct_ftp_master *ct_ftp_info = &ct->help.ct_ftp_info;
-	struct ip_conntrack_expect expect, *exp = &expect;
-	struct ip_ct_ftp_expect *exp_ftp_info = &exp->help.exp_ftp_info;
+	struct ip_conntrack_expect *exp;
+	struct ip_ct_ftp_expect *exp_ftp_info;
 
 	unsigned int i;
 	int found = 0;
@@ -346,8 +346,12 @@
 	DEBUGP("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
 	       (int)matchlen, data + matchoff,
 	       matchlen, ntohl(tcph.seq) + matchoff);
-	       
-	memset(&expect, 0, sizeof(expect));
+
+	/* Allocate expectation which will be inserted */
+	if (ip_conntrack_expect_alloc(&exp) < 0)
+		return -ENOMEM;
+
+	exp_ftp_info = &exp->help.exp_ftp_info;
 
 	/* Update the ftp info */
 	if (htonl((array[0] << 24) | (array[1] << 16) | (array[2] << 8) | array[3])
@@ -389,7 +393,7 @@
 	exp->expectfn = NULL;
 
 	/* Ignore failure; should only happen with NAT */
-	ip_conntrack_expect_related(ct, &expect);
+	ip_conntrack_expect_related(exp, ct);
 	ret = NF_ACCEPT;
  out:
 	UNLOCK_BH(&ip_ftp_lock);
net/ipv4/netfilter/ip_conntrack_irc.c +8 -5
@@ -106,8 +106,8 @@
 	struct tcphdr tcph;
 	char *data, *data_limit;
 	int dir = CTINFO2DIR(ctinfo);
-	struct ip_conntrack_expect expect, *exp = &expect;
-	struct ip_ct_irc_expect *exp_irc_info = &exp->help.exp_irc_info;
+	struct ip_conntrack_expect *exp;
+	struct ip_ct_irc_expect *exp_irc_info = NULL;
 
 	u_int32_t dcc_ip;
 	u_int16_t dcc_port;
@@ -190,8 +190,11 @@
 
 				continue;
 			}
-			
-			memset(&expect, 0, sizeof(expect));
+
+			if (ip_conntrack_expect_alloc(&exp) < 0)
+				return -ENOMEM;
+
+			exp_irc_info = &exp->help.exp_irc_info;
 
 			/* save position of address in dcc string,
 			 * necessary for NAT */
@@ -218,7 +221,7 @@
 				NIPQUAD(exp->tuple.dst.ip),
 				ntohs(exp->tuple.dst.u.tcp.port));
 
-			ip_conntrack_expect_related(ct, &expect);
+			ip_conntrack_expect_related(exp, ct);
 
 			goto out;
 		} /* for .. NUM_DCCPROTO */
net/ipv4/netfilter/ip_conntrack_tftp.c +13 -11
@@ -44,7 +44,7 @@
 		     enum ip_conntrack_info ctinfo)
 {
 	struct tftphdr tftph;
-	struct ip_conntrack_expect exp;
+	struct ip_conntrack_expect *exp;
 
 	if (skb_copy_bits(skb, skb->nh.iph->ihl * 4 + sizeof(struct udphdr),
 			  &tftph, sizeof(tftph)) != 0)
@@ -57,19 +57,21 @@
 		DEBUGP("");
 		DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
 		DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
-		memset(&exp, 0, sizeof(exp));
 
-		exp.tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
-		exp.mask.src.ip = 0xffffffff;
-		exp.mask.dst.ip = 0xffffffff;
-		exp.mask.dst.u.udp.port = 0xffff;
-		exp.mask.dst.protonum = 0xffff;
-		exp.expectfn = NULL;
+		if (ip_conntrack_expect_alloc(&exp) < 0)
+			return -ENOMEM;
+
+		exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
+		exp->mask.src.ip = 0xffffffff;
+		exp->mask.dst.ip = 0xffffffff;
+		exp->mask.dst.u.udp.port = 0xffff;
+		exp->mask.dst.protonum = 0xffff;
+		exp->expectfn = NULL;
 
 		DEBUGP("expect: ");
-		DUMP_TUPLE(&exp.tuple);
-		DUMP_TUPLE(&exp.mask);
-		ip_conntrack_expect_related(ct, &exp);
+		DUMP_TUPLE(&exp->tuple);
+		DUMP_TUPLE(&exp->mask);
+		ip_conntrack_expect_related(exp, ct);
 		break;
 	default:
 		DEBUGP("Unknown opcode\n");
net/ipv4/netfilter/ip_conntrack_core.c +51 -32

111 lines not on this page · raw message

net/ipv4/netfilter/ip_conntrack_standalone.c +1 -0
@@ -497,6 +497,7 @@
 EXPORT_SYMBOL(ip_ct_find_proto);
 EXPORT_SYMBOL(__ip_ct_find_proto);
 EXPORT_SYMBOL(ip_ct_find_helper);
+EXPORT_SYMBOL(ip_conntrack_expect_alloc);
 EXPORT_SYMBOL(ip_conntrack_expect_related);
 EXPORT_SYMBOL(ip_conntrack_change_expect);
 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
include/linux/netfilter_ipv4/ip_conntrack_helper.h +6 -2
@@ -35,9 +35,13 @@
 
 extern struct ip_conntrack_helper *ip_ct_find_helper(const struct ip_conntrack_tuple *tuple);
 
+
+/* Allocate space for an expectation: this is mandatory before calling 
+   ip_conntrack_expect_related. */
+extern int ip_conntrack_expect_alloc(struct ip_conntrack_expect **new);
 /* Add an expected connection: can have more than one per connection */
-extern int ip_conntrack_expect_related(struct ip_conntrack *related_to,
-				       struct ip_conntrack_expect *exp);
+extern int ip_conntrack_expect_related(struct ip_conntrack_expect *exp,
+				       struct ip_conntrack *related_to);
 extern int ip_conntrack_change_expect(struct ip_conntrack_expect *expect,
 				      struct ip_conntrack_tuple *newtuple);
 extern void ip_conntrack_unexpect_related(struct ip_conntrack_expect *exp);
#10 lore raw
In reply to Patrick McHardy

Attached my patch for the patrick's amanda helper to use the new expect_alloc api.

Pablo

Quoted 526 lines · Patrick McHardy
Quoted 10 lines · Harald Welte
Quoted 3 lines · Patrick McHardy

The 2.4 patch went in, the 2.6 didn't. Some of the problems got fixed in 2.6 when removing skb_linearize, but some didn't. Do you want me to resubmit the patch to you ?

yes, please.

Old patch attached, it still applies cleanly.

Quoted 6 lines
Quoted 3 lines

Regards
Patrick

------------------------------------------------------------------------

# This is a BitKeeper generated patch for the following project:
# Project Name: Linux kernel tree
# This patch format is intended for GNU patch command version 2.5 or higher.
# This patch includes the following deltas:
# ChangeSet 1.1356 -> 1.1357 # include/linux/netfilter_ipv4/ip_conntrack_amanda.h 1.2 -> 1.3 # net/ipv4/netfilter/ip_nat_amanda.c 1.5 -> 1.6 # net/ipv4/netfilter/ip_conntrack_amanda.c 1.4 -> 1.5 #
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 03/09/28 [email protected] 1.1357
# [NETFILTER]: Fix amanda helpers
# --------------------------------------------
#
diff -Nru a/include/linux/netfilter_ipv4/ip_conntrack_amanda.h b/include/linux/netfilter_ipv4/ip_conntrack_amanda.h

include/linux/netfilter_ipv4/ip_conntrack_amanda.h +0 -9
@@ -2,20 +2,11 @@
 #define _IP_CONNTRACK_AMANDA_H
 /* AMANDA tracking. */
 
-#ifdef __KERNEL__
-
-#include <linux/netfilter_ipv4/lockhelp.h>
-
-/* Protects amanda part of conntracks */
-DECLARE_LOCK_EXTERN(ip_amanda_lock);
-
-#endif
-

struct ip_ct_amanda_expect
{
u_int16_t port; /* port number of this expectation */

include/linux/netfilter_ipv4/ip_conntrack_amanda.h +2 -2
-	u_int16_t offset;	/* offset of the port specification in ctrl packet */
-	u_int16_t len;		/* the length of the port number specification */
+	u_int16_t offset;	/* offset of port in ctrl packet */
+	u_int16_t len;		/* length of the port number string */

};

#endif /* _IP_CONNTRACK_AMANDA_H */
diff -Nru a/net/ipv4/netfilter/ip_conntrack_amanda.c b/net/ipv4/netfilter/ip_conntrack_amanda.c

net/ipv4/netfilter/ip_conntrack_amanda.c +39 -109
@@ -18,6 +18,7 @@
 *
 */
 
+#include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/netfilter.h>
 #include <linux/ip.h>
@@ -36,50 +37,37 @@
 MODULE_PARM(master_timeout, "i");
 MODULE_PARM_DESC(master_timeout, "timeout for the master connection");
 
-DECLARE_LOCK(ip_amanda_lock);
-
-char *conns[] = { "DATA ", "MESG ", "INDEX " };
-
-#if 0
-#define DEBUGP printk
-#else
-#define DEBUGP(format, args...)
-#endif
+static char *conns[] = { "DATA ", "MESG ", "INDEX " };
 
 /* This is slow, but it's simple. --RR */
 static char amanda_buffer[65536];
+static DECLARE_LOCK(amanda_buffer_lock);
 
 static int help(struct sk_buff *skb,
-		struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
+                struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
 {
-	char *data, *data_limit;
-	int dir = CTINFO2DIR(ctinfo);
+	struct ip_conntrack_expect exp;
+	struct ip_ct_amanda_expect *exp_amanda_info;
+	char *data, *data_limit, *tmp;
 	unsigned int dataoff, i;
-	struct ip_ct_amanda *info =
-				(struct ip_ct_amanda *)&ct->help.ct_ftp_info;
 
-	/* Can't track connections formed before we registered */
-	if (!info)
+	/* Only look at packets from the Amanda server */
+	if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
 		return NF_ACCEPT;
 
 	/* increase the UDP timeout of the master connection as replies from
 	 * Amanda clients to the server can be quite delayed */
 	ip_ct_refresh(ct, master_timeout * HZ);
 
-	/* If packet is coming from Amanda server */
-	if (dir == IP_CT_DIR_ORIGINAL)
-		return NF_ACCEPT;
-
 	/* No data? */
 	dataoff = skb->nh.iph->ihl*4 + sizeof(struct udphdr);
 	if (dataoff >= skb->len) {
 		if (net_ratelimit())
-			printk("ip_conntrack_amanda_help: skblen = %u\n",
-			       (unsigned)skb->len);
+			printk("amanda_help: skblen = %u\n", skb->len);
 		return NF_ACCEPT;
 	}
 
-	LOCK_BH(&ip_amanda_lock);
+	LOCK_BH(&amanda_buffer_lock);
 	skb_copy_bits(skb, dataoff, amanda_buffer, skb->len - dataoff);
 	data = amanda_buffer;
 	data_limit = amanda_buffer + skb->len - dataoff;
@@ -89,84 +77,39 @@
 	data = strstr(data, "CONNECT ");
 	if (!data)
 		goto out;
-
-	DEBUGP("ip_conntrack_amanda_help: CONNECT found in connection "
-		   "%u.%u.%u.%u:%u %u.%u.%u.%u:%u\n",
-		   NIPQUAD(iph->saddr), htons(udph->source),
-		   NIPQUAD(iph->daddr), htons(udph->dest));
 	data += strlen("CONNECT ");
 
+	memset(&exp, 0, sizeof(exp));
+	exp.tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
+	exp.tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
+	exp.tuple.dst.protonum = IPPROTO_TCP;
+	exp.mask.src.ip = 0xFFFFFFFF;
+	exp.mask.dst.ip = 0xFFFFFFFF;
+	exp.mask.dst.protonum = 0xFFFF;
+	exp.mask.dst.u.tcp.port = 0xFFFF;
+
 	/* Only search first line. */	
-	if (strchr(data, '\n'))
-		*strchr(data, '\n') = '\0';
+	if ((tmp = strchr(data, '\n')))
+		*tmp = '\0';
 
+	exp_amanda_info = &exp.help.exp_amanda_info;
 	for (i = 0; i < ARRAY_SIZE(conns); i++) {
 		char *match = strstr(data, conns[i]);
-		if (match) {
-			char *portchr;
-			struct ip_conntrack_expect expect;
-			struct ip_ct_amanda_expect *exp_amanda_info =
-				&expect.help.exp_amanda_info;
-
-			memset(&expect, 0, sizeof(expect));
-
-			data += strlen(conns[i]);
-			/* this is not really tcp, but let's steal an
-			 * idea from a tcp stream helper :-) */
-			// XXX expect.seq = data - amanda_buffer;
-			exp_amanda_info->offset = data - amanda_buffer;
-// XXX DEBUGP("expect.seq = %p - %p = %d\n", data, amanda_buffer, expect.seq);
-DEBUGP("exp_amanda_info->offset = %p - %p = %d\n", data, amanda_buffer, exp_amanda_info->offset);
-			portchr = data;
-			exp_amanda_info->port = simple_strtoul(data, &data,10);
-			exp_amanda_info->len = data - portchr;
-
-			/* eat whitespace */
-			while (*data == ' ')
-				data++;
-			DEBUGP("ip_conntrack_amanda_help: "
-			       "CONNECT %s request with port "
-			       "%u found\n", conns[i],
-			       exp_amanda_info->port);
-
-			expect.tuple = ((struct ip_conntrack_tuple)
-				{ { ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip,
-				    { 0 } },
-				  { ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip,
-				    { htons(exp_amanda_info->port) },
-				    IPPROTO_TCP }});
-			expect.mask = ((struct ip_conntrack_tuple)
-				{ { 0, { 0 } },
-				  { 0xFFFFFFFF, { 0xFFFF }, 0xFFFF }});
-
-			expect.expectfn = NULL;
-
-			DEBUGP ("ip_conntrack_amanda_help: "
-				"expect_related: %u.%u.%u.%u:%u - "
-				"%u.%u.%u.%u:%u\n",
-				NIPQUAD(expect.tuple.src.ip),
-				ntohs(expect.tuple.src.u.tcp.port),
-				NIPQUAD(expect.tuple.dst.ip),
-				ntohs(expect.tuple.dst.u.tcp.port));
-			if (ip_conntrack_expect_related(ct, &expect)
-			    == -EEXIST) {
-				;
-				/* this must be a packet being resent */
-				/* XXX - how do I get the
-				 *       ip_conntrack_expect that
-				 *       already exists so that I can
-				 *       update the .seq so that the
-				 *       nat module rewrites the port
-				 *       numbers?
-				 *       Perhaps I should use the
-				 *       exp_amanda_info instead of
-				 *       .seq.
-				 */
-			}
-		}
+		if (!match)
+			continue;
+		tmp = data = match + strlen(conns[i]);
+		exp_amanda_info->offset = data - amanda_buffer;
+		exp_amanda_info->port   = simple_strtoul(data, &data, 10);
+		exp_amanda_info->len    = data - tmp;
+		if (exp_amanda_info->port == 0 || exp_amanda_info->len > 5)
+			break;
+
+		exp.tuple.dst.u.tcp.port = htons(exp_amanda_info->port);
+		ip_conntrack_expect_related(ct, &exp);
 	}
- out:
-	UNLOCK_BH(&ip_amanda_lock);
+
+out:
+	UNLOCK_BH(&amanda_buffer_lock);
 	return NF_ACCEPT;
 }
 
@@ -186,29 +129,16 @@
 	},
 };
 
-static void fini(void)
+static void __exit fini(void)
 {
-	DEBUGP("ip_ct_amanda: unregistering helper for port 10080\n");
 	ip_conntrack_helper_unregister(&amanda_helper);
 }
 
 static int __init init(void)
 {
-	int ret;
-
-	DEBUGP("ip_ct_amanda: registering helper for port 10080\n");
-	ret = ip_conntrack_helper_register(&amanda_helper);
-
-	if (ret) {
-		printk("ip_ct_amanda: ERROR registering helper\n");
-		fini();
-		return -EBUSY;
-	}
-	return 0;
+	return ip_conntrack_helper_register(&amanda_helper);
 }
 
 PROVIDES_CONNTRACK(amanda);
-EXPORT_SYMBOL(ip_amanda_lock);
-
 module_init(init);
 module_exit(fini);

diff -Nru a/net/ipv4/netfilter/ip_nat_amanda.c b/net/ipv4/netfilter/ip_nat_amanda.c

net/ipv4/netfilter/ip_nat_amanda.c +4 -14
@@ -11,69 +11,45 @@
 * 	insmod ip_nat_amanda.o
 */
 
+#include <linux/kernel.h>
 #include <linux/module.h>
-#include <linux/netfilter_ipv4.h>
+#include <linux/netfilter.h>
+#include <linux/skbuff.h>
 #include <linux/ip.h>
 #include <linux/udp.h>
-#include <linux/kernel.h>
 #include <net/tcp.h>
 #include <net/udp.h>
 
+#include <linux/netfilter_ipv4.h>
 #include <linux/netfilter_ipv4/ip_nat.h>
 #include <linux/netfilter_ipv4/ip_nat_helper.h>
-#include <linux/netfilter_ipv4/ip_nat_rule.h>
 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
 
 
-#if 0
-#define DEBUGP printk
-#define DUMP_OFFSET(x)	printk("offset_before=%d, offset_after=%d, correction_pos=%u\n", x->offset_before, x->offset_after, x->correction_pos);
-#else
-#define DEBUGP(format, args...)
-#define DUMP_OFFSET(x)
-#endif
-
 MODULE_AUTHOR("Brian J. Murrell <[email protected]>");
 MODULE_DESCRIPTION("Amanda NAT helper");
 MODULE_LICENSE("GPL");
 
-/* protects amanda part of conntracks */
-DECLARE_LOCK_EXTERN(ip_amanda_lock);
-

static unsigned int
amanda_nat_expected(struct sk_buff **pskb,

net/ipv4/netfilter/ip_nat_amanda.c +3 -3
-		 unsigned int hooknum,
-		 struct ip_conntrack *ct,
-		 struct ip_nat_info *info)
+                    unsigned int hooknum,
+                    struct ip_conntrack *ct,
+                    struct ip_nat_info *info)

{

net/ipv4/netfilter/ip_nat_amanda.c +0 -4
-	struct ip_nat_multi_range mr;
-	u_int32_t newdstip, newsrcip, newip;
-	u_int16_t port;
-	struct ip_ct_amanda_expect *exp_info;
	struct ip_conntrack *master = master_ct(ct);
net/ipv4/netfilter/ip_nat_amanda.c +3 -0
+	struct ip_ct_amanda_expect *exp_amanda_info;
+	struct ip_nat_multi_range mr;
+	u_int32_t newip;
	IP_NF_ASSERT(info);
	IP_NF_ASSERT(master);
-
	IP_NF_ASSERT(!(info->initialized & (1 << HOOK2MANIP(hooknum))));
net/ipv4/netfilter/ip_nat_amanda.c +0 -10
-	DEBUGP("nat_expected: We have a connection!\n");
-	exp_info = &ct->master->help.exp_amanda_info;
-
-	newdstip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
-	newsrcip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
-	DEBUGP("nat_expected: %u.%u.%u.%u->%u.%u.%u.%u\n",
-	       NIPQUAD(newsrcip), NIPQUAD(newdstip));
-
-	port = exp_info->port;
-
	if (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC)
net/ipv4/netfilter/ip_nat_amanda.c +1 -1
-		newip = newsrcip;
+		newip = master->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;

else

net/ipv4/netfilter/ip_nat_amanda.c +1 -3
-		newip = newdstip;
-
-	DEBUGP("nat_expected: IP to %u.%u.%u.%u\n", NIPQUAD(newip));
+		newip = master->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
	mr.rangesize = 1;
	/* We don't want to manip the per-protocol, just the IPs. */
net/ipv4/netfilter/ip_nat_amanda.c +28 -76
@@ -81,121 +57,79 @@
 	mr.range[0].min_ip = mr.range[0].max_ip = newip;
 
 	if (HOOK2MANIP(hooknum) == IP_NAT_MANIP_DST) {
+		exp_amanda_info = &ct->master->help.exp_amanda_info;
 		mr.range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
 		mr.range[0].min = mr.range[0].max
 			= ((union ip_conntrack_manip_proto)
-				{ .udp = { htons(port) } });
+				{ .udp = { htons(exp_amanda_info->port) } });
 	}
 
 	return ip_nat_setup_info(ct, &mr, hooknum);
 }
 
 static int amanda_data_fixup(struct ip_conntrack *ct,
-			  struct sk_buff **pskb,
-			  enum ip_conntrack_info ctinfo,
-			  struct ip_conntrack_expect *expect)
+                             struct sk_buff **pskb,
+                             enum ip_conntrack_info ctinfo,
+                             struct ip_conntrack_expect *exp)
 {
-	u_int32_t newip;
-	/* DATA 99999 MESG 99999 INDEX 99999 */
-	char buffer[6];
-	struct ip_conntrack_expect *exp = expect;
-	struct ip_ct_amanda_expect *ct_amanda_info = &exp->help.exp_amanda_info;
+	struct ip_ct_amanda_expect *exp_amanda_info;
 	struct ip_conntrack_tuple t = exp->tuple;
+	char buffer[sizeof("65535")];
 	u_int16_t port;
 
-	MUST_BE_LOCKED(&ip_amanda_lock);
-
-	newip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
-	DEBUGP ("ip_nat_amanda_help: newip = %u.%u.%u.%u\n", NIPQUAD(newip));
-
 	/* Alter conntrack's expectations. */
-
-	/* We can read expect here without conntrack lock, since it's
-	   only set in ip_conntrack_amanda, with ip_amanda_lock held
-	   writable */
-
-	t.dst.ip = newip;
-	for (port = ct_amanda_info->port; port != 0; port++) {
+	exp_amanda_info = &exp->help.exp_amanda_info;
+	t.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
+	for (port = exp_amanda_info->port; port != 0; port++) {
 		t.dst.u.tcp.port = htons(port);
 		if (ip_conntrack_change_expect(exp, &t) == 0)
 			break;
 	}
-
 	if (port == 0)
 		return 0;
 
 	sprintf(buffer, "%u", port);
-
-	return ip_nat_mangle_udp_packet(pskb, ct, ctinfo, /* XXX exp->seq */ ct_amanda_info->offset, 
-					ct_amanda_info->len, buffer, strlen(buffer));
+	return ip_nat_mangle_udp_packet(pskb, ct, ctinfo,
+	                                exp_amanda_info->offset,
+	                                exp_amanda_info->len,
+	                                buffer, strlen(buffer));
 }
 
 static unsigned int help(struct ip_conntrack *ct,
-			 struct ip_conntrack_expect *exp,
-			 struct ip_nat_info *info,
-			 enum ip_conntrack_info ctinfo,
-			 unsigned int hooknum,
-			 struct sk_buff **pskb)
+                         struct ip_conntrack_expect *exp,
+                         struct ip_nat_info *info,
+                         enum ip_conntrack_info ctinfo,
+                         unsigned int hooknum,
+                         struct sk_buff **pskb)
 {
-	int dir;
+	int dir = CTINFO2DIR(ctinfo);
+	int ret = NF_ACCEPT;
 
-	if (!exp)
-		DEBUGP("ip_nat_amanda: no exp!!");
-		
 	/* Only mangle things once: original direction in POST_ROUTING
 	   and reply direction on PRE_ROUTING. */
-	dir = CTINFO2DIR(ctinfo);
 	if (!((hooknum == NF_IP_POST_ROUTING && dir == IP_CT_DIR_ORIGINAL)
-	      || (hooknum == NF_IP_PRE_ROUTING && dir == IP_CT_DIR_REPLY))) {
-		DEBUGP("ip_nat_amanda_help: Not touching dir %s at hook %s\n",
-		       dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY",
-		       hooknum == NF_IP_POST_ROUTING ? "POSTROUTING"
-		       : hooknum == NF_IP_PRE_ROUTING ? "PREROUTING"
-		       : hooknum == NF_IP_LOCAL_OUT ? "OUTPUT"
-		       : hooknum == NF_IP_LOCAL_IN ? "INPUT" : "???");
+	      || (hooknum == NF_IP_PRE_ROUTING && dir == IP_CT_DIR_REPLY)))
 		return NF_ACCEPT;
-	}
-	DEBUGP("ip_nat_amanda_help: got beyond not touching: dir %s at hook %s for expect: ",
-		   dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY",
-		   hooknum == NF_IP_POST_ROUTING ? "POSTROUTING"
-		   : hooknum == NF_IP_PRE_ROUTING ? "PREROUTING"
-		     : hooknum == NF_IP_LOCAL_OUT ? "OUTPUT"
-		       : hooknum == NF_IP_LOCAL_IN ? "INPUT" : "???");
-	DUMP_TUPLE(&exp->tuple);
 
-	LOCK_BH(&ip_amanda_lock);
-// XXX	if (exp->seq != 0)
+	/* if this exectation has a "offset" the packet needs to be mangled */
 	if (exp->help.exp_amanda_info.offset != 0)
-		/*  if this packet has a "seq" it needs to have it's content mangled */
-		if (!amanda_data_fixup(ct, pskb, ctinfo, exp)) {
-			UNLOCK_BH(&ip_amanda_lock);
-			DEBUGP("ip_nat_amanda: NF_DROP\n");
-			return NF_DROP;
-		}
+		if (!amanda_data_fixup(ct, pskb, ctinfo, exp))
+			ret = NF_DROP;
 	exp->help.exp_amanda_info.offset = 0;
-	UNLOCK_BH(&ip_amanda_lock);
 
-	DEBUGP("ip_nat_amanda: NF_ACCEPT\n");
-	return NF_ACCEPT;
+	return ret;
 }
 
 static struct ip_nat_helper ip_nat_amanda_helper;
 
-/* This function is intentionally _NOT_ defined as  __exit, because
- * it is needed by init() */
-static void fini(void)
+static void __exit fini(void)
 {
-	DEBUGP("ip_nat_amanda: unregistering nat helper\n");
 	ip_nat_helper_unregister(&ip_nat_amanda_helper);
 }
 
 static int __init init(void)
 {
-	int ret = 0;
-	struct ip_nat_helper *hlpr;
-
-	hlpr = &ip_nat_amanda_helper;
-	memset(hlpr, 0, sizeof(struct ip_nat_helper));
+	struct ip_nat_helper *hlpr = &ip_nat_amanda_helper;
 
 	hlpr->tuple.dst.protonum = IPPROTO_UDP;
 	hlpr->tuple.src.u.udp.port = htons(10080);
@@ -205,20 +139,9 @@
 	hlpr->flags = 0;
 	hlpr->me = THIS_MODULE;
 	hlpr->expect = amanda_nat_expected;
-
 	hlpr->name = "amanda";
 
-	DEBUGP
-	    ("ip_nat_amanda: Trying to register nat helper\n");
-	ret = ip_nat_helper_register(hlpr);
-
-	if (ret) {

net/ipv4/netfilter/ip_nat_amanda.c line 147

Quoted 10 lines
net/ipv4/netfilter/ip_nat_amanda.c +1 -6
-		printk
-		    ("ip_nat_amanda: error registering nat helper\n");
-		fini();
-		return 1;
-	}
-	return ret;
+	return ip_nat_helper_register(hlpr);
 }
 
 NEEDS_CONNTRACK(amanda);
net/ipv4/netfilter/ip_conntrack_amanda.c +14 -12
@@ -46,7 +46,7 @@
 static int help(struct sk_buff *skb,
                 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
 {
-	struct ip_conntrack_expect exp;
+	struct ip_conntrack_expect *exp;
 	struct ip_ct_amanda_expect *exp_amanda_info;
 	char *data, *data_limit, *tmp;
 	unsigned int dataoff, i;
@@ -79,20 +79,22 @@
 		goto out;
 	data += strlen("CONNECT ");
 
-	memset(&exp, 0, sizeof(exp));
-	exp.tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
-	exp.tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
-	exp.tuple.dst.protonum = IPPROTO_TCP;
-	exp.mask.src.ip = 0xFFFFFFFF;
-	exp.mask.dst.ip = 0xFFFFFFFF;
-	exp.mask.dst.protonum = 0xFFFF;
-	exp.mask.dst.u.tcp.port = 0xFFFF;
+	if (ip_conntrack_expect_alloc(&exp) < 0)
+		return -ENOMEM;
+	
+	exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
+	exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
+	exp->tuple.dst.protonum = IPPROTO_TCP;
+	exp->mask.src.ip = 0xFFFFFFFF;
+	exp->mask.dst.ip = 0xFFFFFFFF;
+	exp->mask.dst.protonum = 0xFFFF;
+	exp->mask.dst.u.tcp.port = 0xFFFF;
 
 	/* Only search first line. */	
 	if ((tmp = strchr(data, '\n')))
 		*tmp = '\0';
 
-	exp_amanda_info = &exp.help.exp_amanda_info;
+	exp_amanda_info = &exp->help.exp_amanda_info;
 	for (i = 0; i < ARRAY_SIZE(conns); i++) {
 		char *match = strstr(data, conns[i]);
 		if (!match)
@@ -104,8 +106,8 @@
 		if (exp_amanda_info->port == 0 || exp_amanda_info->len > 5)
 			break;
 
-		exp.tuple.dst.u.tcp.port = htons(exp_amanda_info->port);
-		ip_conntrack_expect_related(ct, &exp);
+		exp->tuple.dst.u.tcp.port = htons(exp_amanda_info->port);
+		ip_conntrack_expect_related(exp, ct);
 	}
 
 out:
#11 lore raw
Quoted 1 line · Pablo Neira

Attached my patch for the patrick's amanda helper to use the new expect_alloc api.

The patch has already been merged by davem, can you send a diff between the fixed version and your one ?

Thanks
Patrick

Quoted 2 lines

Pablo

#12 lore raw
Quoted 7 lines · Patrick McHardy
Quoted 1 line · Pablo Neira

Attached my patch for the patrick's amanda helper to use the new expect_alloc api.

The patch has already been merged by davem, can you send a diff between the fixed version and your one ?

Oops, my mistake, I thought the quoted patch was the real one.

Regards,
Patrick

#24 lore raw
In reply to Harald Welte

Hi Harald and list,

I modified the handling of old expectations in the expect_related function, some clean ups. Please see line 64 of this patch.

I merged this change to my lastest patch since I think that it's related stuff. anyway if you want me to keep this different patch, i won't have any problem to do it.

cheers,
Pablo

net/ipv4/netfilter/ip_conntrack_core.c +55 -37
@@ -917,11 +917,53 @@
 	WRITE_UNLOCK(&ip_conntrack_lock);
 }
 
+int
+ip_conntrack_expect_alloc(struct ip_conntrack_expect **new)
+{
+	*new = (struct ip_conntrack_expect *)
+		kmalloc(sizeof(struct ip_conntrack_expect), GFP_ATOMIC);
+	if (!*new) {
+		DEBUGP("expect_related: OOM allocating expect\n");
+		return -ENOMEM;
+	}
+
+	/* is this important? */
+	memset(*new, 0, sizeof(struct ip_conntrack_expect));
+
+	return 1;
+}
+
+void
+ip_conntrack_expect_insert(struct ip_conntrack_expect *new,
+               struct ip_conntrack *related_to)
+{
+	DEBUGP("new expectation %p of conntrack %p\n", new, related_to);
+	new->expectant = related_to;
+	new->sibling = NULL;
+	atomic_set(&new->use, 1);
+
+	/* add to expected list for this connection */
+	list_add(&new->expected_list, &related_to->sibling_list);
+	/* add to global list of expectations */
+
+	list_prepend(&ip_conntrack_expect_list, &new->list);
+	/* add and start timer if required */
+	if (related_to->helper->timeout) {
+		init_timer(&new->timeout);
+		new->timeout.data = (unsigned long)new;
+		new->timeout.function = expectation_timed_out;
+		new->timeout.expires = jiffies +
+					related_to->helper->timeout * HZ;
+		add_timer(&new->timeout);
+	}
+	related_to->expecting++;
+}
+
 /* Add a related connection. */
-int ip_conntrack_expect_related(struct ip_conntrack *related_to,
-				struct ip_conntrack_expect *expect)
+int ip_conntrack_expect_related(struct ip_conntrack_expect *expect,
+				struct ip_conntrack *related_to)
 {
-	struct ip_conntrack_expect *old, *new;
+	struct ip_conntrack_expect *old;
 	int ret = 0;
 
 	WRITE_LOCK(&ip_conntrack_lock);
@@ -943,7 +985,7 @@
 		if (related_to->helper->timeout) {
 			if (!del_timer(&old->timeout)) {
 				/* expectation is dying. Fall through */
-				old = NULL;
+				goto out;
 			} else {
 				old->timeout.expires = jiffies + 
 					related_to->helper->timeout * HZ;
@@ -951,10 +993,10 @@
 			}
 		}
 
-		if (old) {
-			WRITE_UNLOCK(&ip_conntrack_lock);
-			return -EEXIST;
-		}
+		WRITE_UNLOCK(&ip_conntrack_lock);
+		kfree(expect);
+		return -EEXIST;
+
 	} else if (related_to->helper->max_expected && 
 		   related_to->expecting >= related_to->helper->max_expected) {
 		struct list_head *cur_item;
@@ -971,6 +1013,7 @@
 				       related_to->helper->name,
  		    	       	       NIPQUAD(related_to->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip),
  		    	       	       NIPQUAD(related_to->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip));
+			kfree(expect);
 			return -EPERM;
 		}
 		DEBUGP("ip_conntrack: max number of expected "
@@ -1010,37 +1053,12 @@
 			     &expect->mask)) {
 		WRITE_UNLOCK(&ip_conntrack_lock);
 		DEBUGP("expect_related: busy!\n");
+
+		kfree(expect);
 		return -EBUSY;
 	}
-	
-	new = (struct ip_conntrack_expect *) 
-	      kmalloc(sizeof(struct ip_conntrack_expect), GFP_ATOMIC);
-	if (!new) {
-		WRITE_UNLOCK(&ip_conntrack_lock);
-		DEBUGP("expect_relaed: OOM allocating expect\n");
-		return -ENOMEM;
-	}
-	
-	DEBUGP("new expectation %p of conntrack %p\n", new, related_to);
-	memcpy(new, expect, sizeof(*expect));
-	new->expectant = related_to;
-	new->sibling = NULL;
-	atomic_set(&new->use, 1);
-	
-	/* add to expected list for this connection */	
-	list_add(&new->expected_list, &related_to->sibling_list);
-	/* add to global list of expectations */
-	list_prepend(&ip_conntrack_expect_list, &new->list);
-	/* add and start timer if required */
-	if (related_to->helper->timeout) {
-		init_timer(&new->timeout);
-		new->timeout.data = (unsigned long)new;
-		new->timeout.function = expectation_timed_out;
-		new->timeout.expires = jiffies + 
-					related_to->helper->timeout * HZ;
-		add_timer(&new->timeout);
-	}
-	related_to->expecting++;
+
+out:	ip_conntrack_expect_insert(expect, related_to);
 
 	WRITE_UNLOCK(&ip_conntrack_lock);
 
net/ipv4/netfilter/ip_conntrack_standalone.c +1 -0
@@ -497,6 +497,7 @@
 EXPORT_SYMBOL(ip_ct_find_proto);
 EXPORT_SYMBOL(__ip_ct_find_proto);
 EXPORT_SYMBOL(ip_ct_find_helper);
+EXPORT_SYMBOL(ip_conntrack_expect_alloc);
 EXPORT_SYMBOL(ip_conntrack_expect_related);
 EXPORT_SYMBOL(ip_conntrack_change_expect);
 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
include/linux/netfilter_ipv4/ip_conntrack_helper.h +6 -2
@@ -35,9 +35,13 @@
 
 extern struct ip_conntrack_helper *ip_ct_find_helper(const struct ip_conntrack_tuple *tuple);
 
+
+/* Allocate space for an expectation: this is mandatory before calling 
+   ip_conntrack_expect_related. */
+extern int ip_conntrack_expect_alloc(struct ip_conntrack_expect **new);
 /* Add an expected connection: can have more than one per connection */
-extern int ip_conntrack_expect_related(struct ip_conntrack *related_to,
-				       struct ip_conntrack_expect *exp);
+extern int ip_conntrack_expect_related(struct ip_conntrack_expect *exp,
+				       struct ip_conntrack *related_to);
 extern int ip_conntrack_change_expect(struct ip_conntrack_expect *expect,
 				      struct ip_conntrack_tuple *newtuple);
 extern void ip_conntrack_unexpect_related(struct ip_conntrack_expect *exp);
#13 lore raw
In reply to Pablo Neira

Hi Pablo,
this patch is not correct as far as I can see. With your new API, ip_contrack_expect_related owns the memory after beeing called. The cleaned up amanda helper reuses the same memory for all expectations and expects ip_conntrack_expect_related to allocate new memory and copy the data, so this will result in corruption. It also leaks memory if the loop is left early. One thing that seems to affect all changed helpers, you return -ENOMEM when ip_conntrack_expect_alloc() fails, in this case while holding a lock. You should return NF_DROP instead.

Can you please send a new patch ?

Regards
Patrick

Quoted 58 lines · Pablo Neira

Attached my patch for the patrick's amanda helper to use the new expect_alloc api.

Pablo

net/ipv4/netfilter/ip_conntrack_amanda.c +14 -12
@@ -46,7 +46,7 @@
 static int help(struct sk_buff *skb,
                 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
 {
-	struct ip_conntrack_expect exp;
+	struct ip_conntrack_expect *exp;
 	struct ip_ct_amanda_expect *exp_amanda_info;
 	char *data, *data_limit, *tmp;
 	unsigned int dataoff, i;
@@ -79,20 +79,22 @@
 		goto out;
 	data += strlen("CONNECT ");
 
-	memset(&exp, 0, sizeof(exp));
-	exp.tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
-	exp.tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
-	exp.tuple.dst.protonum = IPPROTO_TCP;
-	exp.mask.src.ip = 0xFFFFFFFF;
-	exp.mask.dst.ip = 0xFFFFFFFF;
-	exp.mask.dst.protonum = 0xFFFF;
-	exp.mask.dst.u.tcp.port = 0xFFFF;
+	if (ip_conntrack_expect_alloc(&exp) < 0)
+		return -ENOMEM;
+	
+	exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
+	exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
+	exp->tuple.dst.protonum = IPPROTO_TCP;
+	exp->mask.src.ip = 0xFFFFFFFF;
+	exp->mask.dst.ip = 0xFFFFFFFF;
+	exp->mask.dst.protonum = 0xFFFF;
+	exp->mask.dst.u.tcp.port = 0xFFFF;
 
 	/* Only search first line. */	
 	if ((tmp = strchr(data, '\n')))
 		*tmp = '\0';
 
-	exp_amanda_info = &exp.help.exp_amanda_info;
+	exp_amanda_info = &exp->help.exp_amanda_info;
 	for (i = 0; i < ARRAY_SIZE(conns); i++) {
 		char *match = strstr(data, conns[i]);
 		if (!match)
@@ -104,8 +106,8 @@
 		if (exp_amanda_info->port == 0 || exp_amanda_info->len > 5)
 			break;
 
-		exp.tuple.dst.u.tcp.port = htons(exp_amanda_info->port);
-		ip_conntrack_expect_related(ct, &exp);
+		exp->tuple.dst.u.tcp.port = htons(exp_amanda_info->port);
+		ip_conntrack_expect_related(exp, ct);
 	}
 
 out:
#14 lore raw

Hi Patrick,

Quoted 3 lines · Patrick McHardy

One thing that
seems to affect all changed helpers, you return -ENOMEM when ip_conntrack_expect_alloc() fails

actually i was thinking about this issue today, since I also posted in a recent email (please see: comments about ip_conntrack_in) that i didn't find a helper which returns -1, then I realized that my patch returned -ENOMEM.

I'll submit a new patch tomorrow.

Thanks,
Pablo

#16 lore raw
In reply to Patrick McHardy

One more suggestion, sorry for not bringing this up earlier. ip_conntrack_expect_alloc() takes a pointer to the pointer to assign the memory to and returns -ENOMEM/1, why not just return the pointer ? Besides being what I would expect from an _alloc-function it helps gcc generate better code because it assumes comparisons with NULL unlikely to be true.

Regards,
Patrick

Quoted 21 lines · Patrick McHardy

Hi Pablo,
this patch is not correct as far as I can see. With your new API, ip_contrack_expect_related owns the memory after beeing called. The cleaned up amanda helper reuses the same memory for all expectations and expects ip_conntrack_expect_related to allocate new memory and copy the data, so this will result in corruption. It also leaks memory if the loop is left early. One thing that seems to affect all changed helpers, you return -ENOMEM when ip_conntrack_expect_alloc() fails, in this case while holding a lock. You should return NF_DROP instead.

Can you please send a new patch ?

Regards
Patrick

Quoted 3 lines · Pablo Neira

Attached my patch for the patrick's amanda helper to use the new expect_alloc api.

Pablo

#15 lore raw
In reply to Pablo Neira
Quoted 11 lines · Pablo Neira

Hi Patrick,

Quoted 3 lines · Patrick McHardy

One thing that
seems to affect all changed helpers, you return -ENOMEM when ip_conntrack_expect_alloc() fails

actually i was thinking about this issue today, since I also posted in a recent email (please see: comments about ip_conntrack_in) that i didn't find a helper which returns -1, then I realized that my patch returned -ENOMEM.

Sorry, I'm not entirely up-to-date with my mails as you can see from my late comments ;) I'll read up on it now.

Quoted 2 lines

I'll submit a new patch tomorrow.

Thanks.

Regards,
Patrick

Quoted 4 lines

Thanks,
Pablo

#18 lore raw
In reply to Patrick McHardy

Hi Patrick,

Quoted 7 lines · Patrick McHardy

Hi Pablo,
this patch is not correct as far as I can see. With your new API, ip_contrack_expect_related owns the memory after beeing called. The cleaned up amanda helper reuses the same memory for all expectations and expects ip_conntrack_expect_related to allocate new memory and copy the data, so this will result in corruption. It also leaks memory if the loop is left early.

oops, i'm sorry, I didn't realize that the structure of the amanda helper changed a bit. Yes, my patch for the amanda helper sucks a bit !:-). I know how to fix it, I'll submit the patch tomorrow.

thanks,
Pablo

#17 lore raw
In reply to Patrick McHardy

Hi again Patrick,

Quoted 3 lines · Patrick McHardy

Besides being what I would expect from
an _alloc-function it helps gcc generate better code because it assumes comparisons with NULL unlikely to be true.

yes, I agree with you, thanks for the suggestion. I'll modify the current prototype.

thanks for the feedback,
Pablo

#19 lore raw
In reply to Patrick McHardy

Hi Patrick,

I attached to this email a new patch for the amanda helper with the new API to allocate memory for an expectation. As you could see, I also modified the API following your suggestion. Please have a look at it, if you are fine with it I'll send the updates for expect-optimize patch in pom-ng to Harald ASAP.

best regards,
Pablo

net/ipv4/netfilter/ip_conntrack_amanda.c +25 -17
@@ -46,10 +46,11 @@
 static int help(struct sk_buff *skb,
                 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
 {
-	struct ip_conntrack_expect exp;
+	struct ip_conntrack_expect *exp;
 	struct ip_ct_amanda_expect *exp_amanda_info;
 	char *data, *data_limit, *tmp;
 	unsigned int dataoff, i;
+	u_int16_t port, len;
 
 	/* Only look at packets from the Amanda server */
 	if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
@@ -79,33 +80,40 @@
 		goto out;
 	data += strlen("CONNECT ");
 
-	memset(&exp, 0, sizeof(exp));
-	exp.tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
-	exp.tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
-	exp.tuple.dst.protonum = IPPROTO_TCP;
-	exp.mask.src.ip = 0xFFFFFFFF;
-	exp.mask.dst.ip = 0xFFFFFFFF;
-	exp.mask.dst.protonum = 0xFFFF;
-	exp.mask.dst.u.tcp.port = 0xFFFF;
-
 	/* Only search first line. */	
 	if ((tmp = strchr(data, '\n')))
 		*tmp = '\0';
 
-	exp_amanda_info = &exp.help.exp_amanda_info;
 	for (i = 0; i < ARRAY_SIZE(conns); i++) {
 		char *match = strstr(data, conns[i]);
 		if (!match)
 			continue;
 		tmp = data = match + strlen(conns[i]);
-		exp_amanda_info->offset = data - amanda_buffer;
-		exp_amanda_info->port   = simple_strtoul(data, &data, 10);
-		exp_amanda_info->len    = data - tmp;
-		if (exp_amanda_info->port == 0 || exp_amanda_info->len > 5)
+		port = simple_strtoul(data, &data, 10);
+		len = data - tmp;
+		if (port == 0 || len > 5)
 			break;
 
-		exp.tuple.dst.u.tcp.port = htons(exp_amanda_info->port);
-		ip_conntrack_expect_related(ct, &exp);
+		exp = ip_conntrack_expect_alloc();
+		if (exp == NULL)
+			return NF_DROP;
+
+		exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
+		exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
+		exp->tuple.dst.protonum = IPPROTO_TCP;
+		exp->mask.src.ip = 0xFFFFFFFF;
+		exp->mask.dst.ip = 0xFFFFFFFF;
+		exp->mask.dst.protonum = 0xFFFF;
+		exp->mask.dst.u.tcp.port = 0xFFFF;
+
+		exp_amanda_info = &exp->help.exp_amanda_info;
+		exp_amanda_info->offset = data - amanda_buffer;
+		exp_amanda_info->port   = port;
+		exp_amanda_info->len    = len;
+
+		exp->tuple.dst.u.tcp.port = htons(port);
+
+		ip_conntrack_expect_related(exp, ct);
 	}
 
 out:
#20 lore raw

Hi Pablo,

Quoted 3 lines · Pablo Neira

Hi Patrick,

I attached to this email a new patch for the amanda helper with the new API to allocate memory for an expectation. As you could see, I also modified the API following your suggestion. Please have a look at it, if you are fine with it I'll send the updates for expect-optimize patch in pom-ng to Harald ASAP.

The patch looks fine, except for one thing, you return from the function after a failed memory allocation without dropping amanda_buffer_lock. My advice of returning NF_DROP from a helper in case of memory allocation wasn't right anyway, connection tracking should not drop packets. Just replace that return with a break and it's fine.

Regards
Patrick

Quoted 79 lines

best regards,
Pablo

------------------------------------------------------------------------

net/ipv4/netfilter/ip_conntrack_amanda.c +25 -17
@@ -46,10 +46,11 @@
 static int help(struct sk_buff *skb,
                 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
 {
-	struct ip_conntrack_expect exp;
+	struct ip_conntrack_expect *exp;
 	struct ip_ct_amanda_expect *exp_amanda_info;
 	char *data, *data_limit, *tmp;
 	unsigned int dataoff, i;
+	u_int16_t port, len;
 
 	/* Only look at packets from the Amanda server */
 	if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
@@ -79,33 +80,40 @@
 		goto out;
 	data += strlen("CONNECT ");
 
-	memset(&exp, 0, sizeof(exp));
-	exp.tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
-	exp.tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
-	exp.tuple.dst.protonum = IPPROTO_TCP;
-	exp.mask.src.ip = 0xFFFFFFFF;
-	exp.mask.dst.ip = 0xFFFFFFFF;
-	exp.mask.dst.protonum = 0xFFFF;
-	exp.mask.dst.u.tcp.port = 0xFFFF;
-
 	/* Only search first line. */	
 	if ((tmp = strchr(data, '\n')))
 		*tmp = '\0';
 
-	exp_amanda_info = &exp.help.exp_amanda_info;
 	for (i = 0; i < ARRAY_SIZE(conns); i++) {
 		char *match = strstr(data, conns[i]);
 		if (!match)
 			continue;
 		tmp = data = match + strlen(conns[i]);
-		exp_amanda_info->offset = data - amanda_buffer;
-		exp_amanda_info->port   = simple_strtoul(data, &data, 10);
-		exp_amanda_info->len    = data - tmp;
-		if (exp_amanda_info->port == 0 || exp_amanda_info->len > 5)
+		port = simple_strtoul(data, &data, 10);
+		len = data - tmp;
+		if (port == 0 || len > 5)
 			break;
 
-		exp.tuple.dst.u.tcp.port = htons(exp_amanda_info->port);
-		ip_conntrack_expect_related(ct, &exp);
+		exp = ip_conntrack_expect_alloc();
+		if (exp == NULL)
+			return NF_DROP;
+
+		exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
+		exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
+		exp->tuple.dst.protonum = IPPROTO_TCP;
+		exp->mask.src.ip = 0xFFFFFFFF;
+		exp->mask.dst.ip = 0xFFFFFFFF;
+		exp->mask.dst.protonum = 0xFFFF;
+		exp->mask.dst.u.tcp.port = 0xFFFF;
+
+		exp_amanda_info = &exp->help.exp_amanda_info;
+		exp_amanda_info->offset = data - amanda_buffer;
+		exp_amanda_info->port   = port;
+		exp_amanda_info->len    = len;
+
+		exp->tuple.dst.u.tcp.port = htons(port);
+
+		ip_conntrack_expect_related(exp, ct);
 	}
 
 out:
#21 lore raw
Quoted 2 lines · Patrick McHardy

The patch looks fine, except for one thing, you return from the function after a failed memory allocation without dropping amanda_buffer_lock.

ok, I fixed this problem.

Quoted 4 lines

My
advice of returning NF_DROP from a helper in case of memory allocation wasn't right anyway, connection tracking should not drop packets. Just replace that return with a break and it's fine.

Also fixed. Attached last modification. Thanks patrick.

Best regards,
Pablo

net/ipv4/netfilter/ip_conntrack_amanda.c +25 -17
@@ -46,10 +46,11 @@
 static int help(struct sk_buff *skb,
                 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
 {
-	struct ip_conntrack_expect exp;
+	struct ip_conntrack_expect *exp;
 	struct ip_ct_amanda_expect *exp_amanda_info;
 	char *data, *data_limit, *tmp;
 	unsigned int dataoff, i;
+	u_int16_t port, len;
 
 	/* Only look at packets from the Amanda server */
 	if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
@@ -79,33 +80,40 @@
 		goto out;
 	data += strlen("CONNECT ");
 
-	memset(&exp, 0, sizeof(exp));
-	exp.tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
-	exp.tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
-	exp.tuple.dst.protonum = IPPROTO_TCP;
-	exp.mask.src.ip = 0xFFFFFFFF;
-	exp.mask.dst.ip = 0xFFFFFFFF;
-	exp.mask.dst.protonum = 0xFFFF;
-	exp.mask.dst.u.tcp.port = 0xFFFF;
-
 	/* Only search first line. */	
 	if ((tmp = strchr(data, '\n')))
 		*tmp = '\0';
 
-	exp_amanda_info = &exp.help.exp_amanda_info;
 	for (i = 0; i < ARRAY_SIZE(conns); i++) {
 		char *match = strstr(data, conns[i]);
 		if (!match)
 			continue;
 		tmp = data = match + strlen(conns[i]);
-		exp_amanda_info->offset = data - amanda_buffer;
-		exp_amanda_info->port   = simple_strtoul(data, &data, 10);
-		exp_amanda_info->len    = data - tmp;
-		if (exp_amanda_info->port == 0 || exp_amanda_info->len > 5)
+		port = simple_strtoul(data, &data, 10);
+		len = data - tmp;
+		if (port == 0 || len > 5)
 			break;
 
-		exp.tuple.dst.u.tcp.port = htons(exp_amanda_info->port);
-		ip_conntrack_expect_related(ct, &exp);
+		exp = ip_conntrack_expect_alloc();
+		if (exp == NULL)
+			goto out;
+
+		exp->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
+		exp->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
+		exp->tuple.dst.protonum = IPPROTO_TCP;
+		exp->mask.src.ip = 0xFFFFFFFF;
+		exp->mask.dst.ip = 0xFFFFFFFF;
+		exp->mask.dst.protonum = 0xFFFF;
+		exp->mask.dst.u.tcp.port = 0xFFFF;
+
+		exp_amanda_info = &exp->help.exp_amanda_info;
+		exp_amanda_info->offset = data - amanda_buffer;
+		exp_amanda_info->port   = port;
+		exp_amanda_info->len    = len;
+
+		exp->tuple.dst.u.tcp.port = htons(port);
+
+		ip_conntrack_expect_related(exp, ct);
 	}
 
 out:
#22 lore raw
Quoted 1 line · Pablo Neira

Also fixed. Attached last modification. Thanks patrick.

Looks good, thanks.

Regards
Patrick

Quoted 3 lines

Best regards,
Pablo

#25 lore raw
In reply to Harald Welte

Hi,

I already sent the updates patches for expect-optimize in pom-ng to Harald but I forgot redirecting them to the maillist.

Please use these ones instead since they are the last version.

regards,
Pablo

net/ipv4/netfilter/ip_conntrack_core.c +57 -37
@@ -917,11 +917,55 @@
 	WRITE_UNLOCK(&ip_conntrack_lock);
 }
 
+struct ip_conntrack_expect *
+ip_conntrack_expect_alloc()
+{
+	struct ip_conntrack_expect *new;
+	
+	new = (struct ip_conntrack_expect *)
+		kmalloc(sizeof(struct ip_conntrack_expect), GFP_ATOMIC);
+	if (!new) {
+		DEBUGP("expect_related: OOM allocating expect\n");
+		return NULL;
+	}
+
+	/* tuple_cmp compares whole union, we have to initialized cleanly */
+	memset(new, 0, sizeof(struct ip_conntrack_expect));
+
+	return new;
+}
+
+static void
+ip_conntrack_expect_insert(struct ip_conntrack_expect *new,
+			   struct ip_conntrack *related_to)
+{
+	DEBUGP("new expectation %p of conntrack %p\n", new, related_to);
+	new->expectant = related_to;
+	new->sibling = NULL;
+	atomic_set(&new->use, 1);
+
+	/* add to expected list for this connection */
+	list_add(&new->expected_list, &related_to->sibling_list);
+	/* add to global list of expectations */
+
+	list_prepend(&ip_conntrack_expect_list, &new->list);
+	/* add and start timer if required */
+	if (related_to->helper->timeout) {
+		init_timer(&new->timeout);
+		new->timeout.data = (unsigned long)new;
+		new->timeout.function = expectation_timed_out;
+		new->timeout.expires = jiffies +
+					related_to->helper->timeout * HZ;
+		add_timer(&new->timeout);
+	}
+	related_to->expecting++;
+}
+
 /* Add a related connection. */
-int ip_conntrack_expect_related(struct ip_conntrack *related_to,
-				struct ip_conntrack_expect *expect)
+int ip_conntrack_expect_related(struct ip_conntrack_expect *expect,
+				struct ip_conntrack *related_to)
 {
-	struct ip_conntrack_expect *old, *new;
+	struct ip_conntrack_expect *old;
 	int ret = 0;
 
 	WRITE_LOCK(&ip_conntrack_lock);
@@ -943,7 +987,7 @@
 		if (related_to->helper->timeout) {
 			if (!del_timer(&old->timeout)) {
 				/* expectation is dying. Fall through */
-				old = NULL;
+				goto out;
 			} else {
 				old->timeout.expires = jiffies + 
 					related_to->helper->timeout * HZ;
@@ -951,10 +995,10 @@
 			}
 		}
 
-		if (old) {
-			WRITE_UNLOCK(&ip_conntrack_lock);
-			return -EEXIST;
-		}
+		WRITE_UNLOCK(&ip_conntrack_lock);
+		kfree(expect);
+		return -EEXIST;
+
 	} else if (related_to->helper->max_expected && 
 		   related_to->expecting >= related_to->helper->max_expected) {
 		struct list_head *cur_item;
@@ -971,6 +1015,7 @@
 				       related_to->helper->name,
  		    	       	       NIPQUAD(related_to->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip),
  		    	       	       NIPQUAD(related_to->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip));
+			kfree(expect);
 			return -EPERM;
 		}
 		DEBUGP("ip_conntrack: max number of expected "
@@ -1010,37 +1055,12 @@
 			     &expect->mask)) {
 		WRITE_UNLOCK(&ip_conntrack_lock);
 		DEBUGP("expect_related: busy!\n");
+
+		kfree(expect);
 		return -EBUSY;
 	}
-	
-	new = (struct ip_conntrack_expect *) 
-	      kmalloc(sizeof(struct ip_conntrack_expect), GFP_ATOMIC);
-	if (!new) {
-		WRITE_UNLOCK(&ip_conntrack_lock);
-		DEBUGP("expect_relaed: OOM allocating expect\n");
-		return -ENOMEM;
-	}
-	
-	DEBUGP("new expectation %p of conntrack %p\n", new, related_to);
-	memcpy(new, expect, sizeof(*expect));
-	new->expectant = related_to;
-	new->sibling = NULL;
-	atomic_set(&new->use, 1);
-	
-	/* add to expected list for this connection */	
-	list_add(&new->expected_list, &related_to->sibling_list);
-	/* add to global list of expectations */
-	list_prepend(&ip_conntrack_expect_list, &new->list);
-	/* add and start timer if required */
-	if (related_to->helper->timeout) {
-		init_timer(&new->timeout);
-		new->timeout.data = (unsigned long)new;
-		new->timeout.function = expectation_timed_out;
-		new->timeout.expires = jiffies + 
-					related_to->helper->timeout * HZ;
-		add_timer(&new->timeout);
-	}
-	related_to->expecting++;
+
+out:	ip_conntrack_expect_insert(expect, related_to);
 
 	WRITE_UNLOCK(&ip_conntrack_lock);
 
net/ipv4/netfilter/ip_conntrack_standalone.c +1 -0
@@ -497,6 +497,7 @@
 EXPORT_SYMBOL(ip_ct_find_proto);
 EXPORT_SYMBOL(__ip_ct_find_proto);
 EXPORT_SYMBOL(ip_ct_find_helper);
+EXPORT_SYMBOL(ip_conntrack_expect_alloc);
 EXPORT_SYMBOL(ip_conntrack_expect_related);
 EXPORT_SYMBOL(ip_conntrack_change_expect);
 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
include/linux/netfilter_ipv4/ip_conntrack_helper.h +6 -2
@@ -35,9 +35,13 @@
 
 extern struct ip_conntrack_helper *ip_ct_find_helper(const struct ip_conntrack_tuple *tuple);
 
+
+/* Allocate space for an expectation: this is mandatory before calling 
+   ip_conntrack_expect_related. */
+extern struct ip_conntrack_expect *ip_conntrack_expect_alloc(void);
 /* Add an expected connection: can have more than one per connection */
-extern int ip_conntrack_expect_related(struct ip_conntrack *related_to,
-				       struct ip_conntrack_expect *exp);
+extern int ip_conntrack_expect_related(struct ip_conntrack_expect *exp,
+				       struct ip_conntrack *related_to);
 extern int ip_conntrack_change_expect(struct ip_conntrack_expect *expect,
 				      struct ip_conntrack_tuple *newtuple);
 extern void ip_conntrack_unexpect_related(struct ip_conntrack_expect *exp);
net/ipv4/netfilter/ip_conntrack_tftp.c +14 -11
@@ -44,7 +44,7 @@
 		     enum ip_conntrack_info ctinfo)
 {
 	struct tftphdr tftph;
-	struct ip_conntrack_expect exp;
+	struct ip_conntrack_expect *exp;
 
 	if (skb_copy_bits(skb, skb->nh.iph->ihl * 4 + sizeof(struct udphdr),
 			  &tftph, sizeof(tftph)) != 0)
@@ -57,19 +57,22 @@
 		DEBUGP("");
 		DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
 		DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
-		memset(&exp, 0, sizeof(exp));
 
-		exp.tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
-		exp.mask.src.ip = 0xffffffff;
-		exp.mask.dst.ip = 0xffffffff;
-		exp.mask.dst.u.udp.port = 0xffff;
-		exp.mask.dst.protonum = 0xffff;
-		exp.expectfn = NULL;
+		exp = ip_conntrack_expect_alloc();
+		if (exp == NULL)
+			return NF_ACCEPT;
+
+		exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
+		exp->mask.src.ip = 0xffffffff;
+		exp->mask.dst.ip = 0xffffffff;
+		exp->mask.dst.u.udp.port = 0xffff;
+		exp->mask.dst.protonum = 0xffff;
+		exp->expectfn = NULL;
 
 		DEBUGP("expect: ");
-		DUMP_TUPLE(&exp.tuple);
-		DUMP_TUPLE(&exp.mask);
-		ip_conntrack_expect_related(ct, &exp);
+		DUMP_TUPLE(&exp->tuple);
+		DUMP_TUPLE(&exp->mask);
+		ip_conntrack_expect_related(exp, ct);
 		break;
 	default:
 		DEBUGP("Unknown opcode\n");
net/ipv4/netfilter/ip_conntrack_irc.c +9 -5
@@ -106,8 +106,8 @@
 	struct tcphdr tcph;
 	char *data, *data_limit;
 	int dir = CTINFO2DIR(ctinfo);
-	struct ip_conntrack_expect expect, *exp = &expect;
-	struct ip_ct_irc_expect *exp_irc_info = &exp->help.exp_irc_info;
+	struct ip_conntrack_expect *exp;
+	struct ip_ct_irc_expect *exp_irc_info = NULL;
 
 	u_int32_t dcc_ip;
 	u_int16_t dcc_port;
@@ -190,8 +190,12 @@
 
 				continue;
 			}
-			
-			memset(&expect, 0, sizeof(expect));
+
+			exp = ip_conntrack_expect_alloc();
+			if (exp == NULL)
+				goto out;
+
+			exp_irc_info = &exp->help.exp_irc_info;
 
 			/* save position of address in dcc string,
 			 * necessary for NAT */
@@ -218,7 +222,7 @@
 				NIPQUAD(exp->tuple.dst.ip),
 				ntohs(exp->tuple.dst.u.tcp.port));
 
-			ip_conntrack_expect_related(ct, &expect);
+			ip_conntrack_expect_related(exp, ct);
 
 			goto out;
 		} /* for .. NUM_DCCPROTO */
net/ipv4/netfilter/ip_conntrack_ftp.c +12 -5
@@ -256,8 +256,8 @@
 	int dir = CTINFO2DIR(ctinfo);
 	unsigned int matchlen, matchoff;
 	struct ip_ct_ftp_master *ct_ftp_info = &ct->help.ct_ftp_info;
-	struct ip_conntrack_expect expect, *exp = &expect;
-	struct ip_ct_ftp_expect *exp_ftp_info = &exp->help.exp_ftp_info;
+	struct ip_conntrack_expect *exp;
+	struct ip_ct_ftp_expect *exp_ftp_info;
 
 	unsigned int i;
 	int found = 0;
@@ -346,8 +346,15 @@
 	DEBUGP("conntrack_ftp: match `%.*s' (%u bytes at %u)\n",
 	       (int)matchlen, data + matchoff,
 	       matchlen, ntohl(tcph.seq) + matchoff);
-	       
-	memset(&expect, 0, sizeof(expect));
+
+	/* Allocate expectation which will be inserted */
+	exp = ip_conntrack_expect_alloc();
+	if (exp == NULL) {
+		ret = NF_ACCEPT;
+		goto out;
+	}
+
+	exp_ftp_info = &exp->help.exp_ftp_info;
 
 	/* Update the ftp info */
 	if (htonl((array[0] << 24) | (array[1] << 16) | (array[2] << 8) | array[3])
@@ -389,7 +396,7 @@
 	exp->expectfn = NULL;
 
 	/* Ignore failure; should only happen with NAT */
-	ip_conntrack_expect_related(ct, &expect);
+	ip_conntrack_expect_related(exp, ct);
 	ret = NF_ACCEPT;
  out:
 	UNLOCK_BH(&ip_ftp_lock);
net/ipv4/netfilter/ip_conntrack_amanda.c +25 -17

68 lines not on this page · raw message

expect-06032004.patch renamed from home/pablo/CVS/patch-o-matic-ng/expect-optimize/linux-2.6.patch +75 -50

274 lines not on this page · raw message