Skip to contentSkip to conversation

make multiple invocations of do_command() behave sane

PATCH In review Martin Josefsson 3 participants 5 messages Feb 1, 2004 – Feb 2, 2004 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 make multiple invocations of do_command() behave sane

Hi

While writing a small script for managing my firewallrules I ran into a small problem with iptables-restore.
The problem is that it doesn't respect the order of matches in a rule.

Example.
Run iptables-restore on the lines below:
*filter
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m tcp --dport 25 -m state --state NEW -j ACCEPT COMMIT

Then run iptables-save -t filter (irrelevant lines removed):
*filter
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT COMMIT

The order of the matches in the second rule as been adopted to match the order of the first rule. This is inconsistent which how the regular iptables command does, it doesn't impose any weird policy. (this doesn't happen if you split the above rules into two files and add each separately with --noflush (--noflush or not doesn't matter if both rules are loaded at the same time)

So what causes this... This is how it works in the regular iptables command:

We keep a list of loaded matches in iptables.c
All matches in this list have a field called 'used' that's always set to 0 in do_command(). When we find a match in a rule we lookup the match by calling find_match() which searches the iptables_matches linked-list and loads the match if it wasn't present and then it sets 'used' to 1 for the match.
This is repeated for each match in the rule. So in the end we have a linked-list with all the matches the rule contained.
Then generate_entry() is called which loops through this linked-list skipping the matches where 'used' is 0. (it's never 0 with the regular iptables command). So we find the matches in the correct order, just like we are used to.

Now comes iptables-restore which calls do_command() for each rule. Remember that do_command() zeroes the 'used' field for all matches. find_match() gets called for each match we find in the rule, for the first rule this goes exactly like above. But for the second and futher rules it's a bit diffrent. We call find_match() for the first match in the second rule, this time we find the match without having to load it and 'used' gets set to 1. This is repeated for all matches in the second rule. Then generate_entry() gets called, and guess what, it loops through iptables_matches in the order they were loaded, not the order the matches were found in this rule. So we end up with the correct matches but not in the order we specified them, we get the order they were loaded.

With the attached patch the behaviour passes my sane-o-meter. iptables-restore of the above rules and then iptables-save gives:
*filter
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m tcp --dport 25 -m state --state NEW -j ACCEPT COMMIT

Which is what I'd expect.

The patch is lightly tested, it seems to work just fine but as always, testers are needed :)
It adds some memory-allocations and linked-list management. I havn't benchmarked these changes, but I hope they don't increase the runtime too much.

If noone screams too loud I'm going to merge the patch into cvs sometime soon.

Signature · 1 line
/Martin
include/iptables.h renamed from cvs/dev/iptables/include/iptables.h +8 -2
diff -urN ../cvs/dev/iptables/include/iptables.h iptables/include/iptables.h
@@ -4,6 +4,13 @@
 #include "iptables_common.h"
 #include "libiptc/libiptc.h"
 
+struct iptables_rule_match
+{
+	struct iptables_rule_match *next;
+
+	struct iptables_match *match;
+};
+
 /* Include file for additions: new matches and targets. */
 struct iptables_match
 {
@@ -50,7 +57,6 @@
 	unsigned int option_offset;
 	struct ipt_entry_match *m;
 	unsigned int mflags;
-	unsigned int used;
 #ifdef NO_SHARED_LIBS
 	unsigned int loaded; /* simulate loading so options are merged properly */
 #endif
@@ -134,7 +140,7 @@
 };
 
 extern struct iptables_target *find_target(const char *name, enum ipt_tryload);
-extern struct iptables_match *find_match(const char *name, enum ipt_tryload);
+extern struct iptables_match *find_match(const char *name, enum ipt_tryload, struct iptables_rule_match **match);
 
 extern int delete_chain(const ipt_chainlabel chain, int verbose,
 			iptc_handle_t *handle);
iptables-save.c renamed from cvs/dev/iptables/iptables-save.c +1 -1
diff -urN ../cvs/dev/iptables/iptables-save.c iptables/iptables-save.c
@@ -111,7 +111,7 @@
 			const struct ipt_ip *ip)
 {
 	struct iptables_match *match
-		= find_match(e->u.user.name, TRY_LOAD);
+		= find_match(e->u.user.name, TRY_LOAD, NULL);
 
 	if (match) {
 		printf("-m %s ", e->u.user.name);
iptables.c renamed from cvs/dev/iptables/iptables.c +67 -61

295 lines not on this page · raw message

#1 lore raw
Quoted 5 lines · Martin Josefsson

Hi

While writing a small script for managing my firewallrules I ran into a small problem with iptables-restore.
The problem is that it doesn't respect the order of matches in a rule.

Thanks a lot, this is indeed an important issue. I wonder why nobody
has complained about this in the past.

Quoted 2 lines

If noone screams too loud I'm going to merge the patch into cvs sometime soon.

please do so.

Quoted 1 line

/Martin

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 9 lines · Harald Welte
Quoted 5 lines · Martin Josefsson

Hi

While writing a small script for managing my firewallrules I ran into a small problem with iptables-restore.
The problem is that it doesn't respect the order of matches in a rule.

Thanks a lot, this is indeed an important issue. I wonder why nobody
has complained about this in the past.

I wondered the same when I found the problem. I thought that someone must have noticed it before.

I noticed it since my script compares the running config (retrieved with iptables-save) with my config-files and updates the running config to match (updates rules, deletes and creates chains as neccessary, all atomically with iptables-restore and keeps all counters).

Running updates with a varied mix of rules gave me strange results, the second time I ran the script without any config-changes it wanted to update a lot of rules again to make them match the configfiles.

Quoted 4 lines
Quoted 2 lines

If noone screams too loud I'm going to merge the patch into cvs sometime soon.

please do so.

Since noone's screamed yet I'll merge it later today in order to give it wider testing, maybe then someone screams.

Signature · 1 line
/Martin
#3 lore raw
Quoted 2 lines · Martin Josefsson

I wondered the same when I found the problem. I thought that someone must have noticed it before.

Personally I am ashame for not having noticed this slight problem with iptables-restore. Have been using it since years back. But then we always have the same order of our matches so it is not that strangewe haven't noticed.

Quoted 2 lines

Since noone's screamed yet I'll merge it later today in order to give it wider testing, maybe then someone screams.

Please do. Will try it out tomorrow.

Regards
Henrik

#4 lore raw
Quoted 9 lines · Henrik Nordstrom
Quoted 2 lines · Martin Josefsson

I wondered the same when I found the problem. I thought that someone must have noticed it before.

Personally I am ashame for not having noticed this slight problem with iptables-restore. Have been using it since years back. But then we always have the same order of our matches so it is not that strangewe haven't noticed.

I first noticed it when I had rules like this:

*nat
-A PREROUTING -i eth0 -p tcp --dport 9310 -j DNAT --to-destination 192.168.1.2 COMMIT
*filter
-A INPUT -p tcp -m state --state NEW --dport 25 -j ACCEPT
COMMIT

Somehow --dport 25 came before --state NEW when I listed the rules after loading the above :)
The problem went away when I split it up into two files and loaded each table separately.

Quoted 4 lines
Quoted 2 lines

Since noone's screamed yet I'll merge it later today in order to give it wider testing, maybe then someone screams.

Please do. Will try it out tomorrow.

I've committed it for both ipv4 and ipv6.

It seems to be working here but I havn't tested it with large rulesets or with a lot of diffrent matches. I hope it won't break anything.

Signature · 1 line
/Martin