Skip to contentSkip to conversation

condition for 2.6.16

PATCH Massimiliano Hofer 9 participants 24 messages Apr 20, 2006 – Apr 29, 2006 netfilter

lore
Share & tools
lore
#0 lore raw

Hi,
I wrote here some time ago about condition and Krzysztof told me it wasn't supposed to work.
I tested it and it seemed to work, but reading the code I agree that it shouldn't have. :)

I find this extension very useful, so I did a complete porting to 2.6.16 complete with XT. I fixed various things (mostly fossils of the 2.4 era) in the process and it works in my test environment.
I am about to test it in a few high load machines, but I'd appreciate if someone else could have a look at it.

What are the next steps in order to have it included in the core extension and finally merged in the kernel?

Signature · 3 lines
Saluti,
   Massimiliano Hofer
        Nucleus
patchlets/condition/help +4 -2
diff -Nru patch-o-matic-ng-20060419/patchlets/condition/help patch-o-matic-ng-20060419.new/patchlets/condition/help
@@ -7,8 +7,10 @@
 iptables -A INPUT -p tcp -m condition --condition web_ok --dport 80 -j ACCEPT
 
 To allow this rule to match:
-echo 1 > /proc/net/ipt_condition/web_ok
+echo 1 > /proc/net/nf_condition/web_ok
 
 To disable this rule: 
-echo 0 > /proc/net/ipt_condition/web_ok
+echo 0 > /proc/net/nf_condition/web_ok
+
+NB: it was /proc/net/ipt_condition on 2.4.
 
patchlets/condition/info +2 -2
diff -Nru patch-o-matic-ng-20060419/patchlets/condition/info patch-o-matic-ng-20060419.new/patchlets/condition/info
@@ -1,4 +1,4 @@
-Author: Stephane Ouellette <[email protected]>
+Author: Stephane Ouellette <[email protected]> and Massimiliano Hofer <[email protected]>
 Status: ItWorksForMe(tm)
+Requires: linux == 2.4 || linux >= 2.6.16
 Repository: extra
-Requires: linux < 2.6.0
patchlets/condition/linux-2.6.16/Documentation/Configure.help.ladd +6 -0
diff -Nru patch-o-matic-ng-20060419/patchlets/condition/linux-2.6.16/Documentation/Configure.help.ladd patch-o-matic-ng-20060419.new/patchlets/condition/linux-2.6.16/Documentation/Configure.help.ladd
@@ -0,0 +1,6 @@
+NETFILTER_XT_MATCH_CONDITION
+  This option allows you to match firewall rules against condition
+  variables stored in the /proc/net/nf_condition directory.
+
+  If you want to compile it as a module, say M here and read
+  Documentation/modules.txt.  If unsure, say `N'.
patchlets/condition/linux-2.6.16/include/linux/netfilter/xt_condition.h +11 -0
diff -Nru patch-o-matic-ng-20060419/patchlets/condition/linux-2.6.16/include/linux/netfilter/xt_condition.h patch-o-matic-ng-20060419.new/patchlets/condition/linux-2.6.16/include/linux/netfilter/xt_condition.h
@@ -0,0 +1,11 @@
+#ifndef _XT_CONDITION_H
+#define _XT_CONDITION_H
+
+#define CONDITION_NAME_LEN  32
+
+struct condition_info {
+	char name[CONDITION_NAME_LEN];
+	int  invert;
+};
+
+#endif /* _XT_CONDITION_H */
patchlets/condition/linux-2.6.16/net/netfilter/Kconfig.ladd +9 -0
diff -Nru patch-o-matic-ng-20060419/patchlets/condition/linux-2.6.16/net/netfilter/Kconfig.ladd patch-o-matic-ng-20060419.new/patchlets/condition/linux-2.6.16/net/netfilter/Kconfig.ladd
@@ -0,0 +1,9 @@
+config NETFILTER_XT_MATCH_CONDITION
+        tristate  '"condition" match support'
+        depends on NETFILTER_XTABLES
+        help
+          This option allows you to match firewall rules against condition
+          variables stored in the /proc/net/ipt_condition directory.
+
+          If you want to compile it as a module, say M here and read
+          Documentation/modules.txt.  If unsure, say `N'.
patchlets/condition/linux-2.6.16/net/netfilter/Makefile.ladd +2 -0
diff -Nru patch-o-matic-ng-20060419/patchlets/condition/linux-2.6.16/net/netfilter/Makefile.ladd patch-o-matic-ng-20060419.new/patchlets/condition/linux-2.6.16/net/netfilter/Makefile.ladd
@@ -0,0 +1,2 @@
+obj-$(CONFIG_NETFILTER_XT_MATCH_PHYSDEV) += xt_physdev.o
+obj-$(CONFIG_NETFILTER_XT_MATCH_LENGTH) += xt_condition.o
patchlets/condition/linux-2.6.16/net/netfilter/xt_condition.c +318 -0

318 lines not on this page · raw message

#1 lore raw
Quoted 8 lines · Massimiliano Hofer

I find this extension very useful, so I did a complete porting to 2.6.16 complete with XT. I fixed various things (mostly fossils of the 2.4 era) in the process and it works in my test environment.
I am about to test it in a few high load machines, but I'd appreciate if someone else could have a look at it.

What are the next steps in order to have it included in the core extension and finally merged in the kernel?

We have already decided that the condition match will not be merged because the same thing can easily be done by adding/removing rules from userspace.

#2 lore raw
Quoted 3 lines · Patrick McHardy

We have already decided that the condition match will not be merged because the same thing can easily be done by adding/removing rules from userspace.

Sorry, I missed that discussion.
Is it still possible to include it in the external patches?

Anyway I think condition allows for a much more elegant solution in cases where adding and removing rules would lead to a messy and unpredictable sequence of overlapping rules, not to mention races between different modifications.
Just my humble opinion.

Signature · 3 lines
Saluti,
   Massimiliano Hofer
        Nucleus
#3 lore raw
Quoted 9 lines · Massimiliano Hofer
Quoted 5 lines · Patrick McHardy

We have already decided that the condition match will not be merged because the same thing can easily be done by adding/removing rules from userspace.

Quoted 7 lines

Sorry, I missed that discussion.
Is it still possible to include it in the external patches?

Anyway I think condition allows for a much more elegant solution in cases where adding and removing rules would lead to a messy and unpredictable sequence of overlapping rules, not to mention races between different modifications. Just my humble opinion.

I concur. The condition match is much more elegant then dynamically messing with the rules.

M4

#4 lore raw
In reply to Massimiliano Hofer
Quoted 8 lines · Massimiliano Hofer
Quoted 3 lines · Patrick McHardy

We have already decided that the condition match will not be merged because the same thing can easily be done by adding/removing rules from userspace.

Sorry, I missed that discussion.

It was discussed at the netfilter workshops, summaries are available at workshop.netfilter.org.

Quoted 1 line

Is it still possible to include it in the external patches?

You can set up a pomng repository and send us the URL to include in the sources.list file once we finish the pomng reorganization.

Quoted 5 lines

Anyway I think condition allows for a much more elegant solution in cases where adding and removing rules would lead to a messy and unpredictable sequence of overlapping rules, not to mention races between different modifications.
Just my humble opinion.

Thats true, mainly because were missing a better kernel-userspace interface and better userspace tools. This is also the reason why we don't want to include it, its just a workaround for these problems.

#5 lore raw
Quoted 2 lines · Patrick McHardy

It was discussed at the netfilter workshops, summaries are available at workshop.netfilter.org.

I did a cursory check of the site, but I only found a 5 or 6 lines summary for every conference. Google doesn't help either.
May you direct me to a more comprehensive report, please?

Quoted 2 lines

You can set up a pomng repository and send us the URL to include in the sources.list file once we finish the pomng reorganization.

I'll do it as soon as possible.

Quoted 4 lines

Thats true, mainly because were missing a better kernel-userspace interface and better userspace tools. This is also the reason why we don't want to include it, its just a workaround for these problems.

Are you talking about a grand plan for radically innovative user tools? Is there anything ready for testing or is it just in the design phase?

Anyway, for the kind of use I have in mind, some global state is needed. Part of that state is in the kernel by definition (that's were the rules are), so we can enrich that state (condition or something better) or we can maintain a second state in userspace and deal with the interactions.

You could say that registering userspace variables is not kernel business, but the alternative is just messy (with the tools available in the near future).

If your aim is a minimal set of targets and matches, you could slash many other extensions. ROUTE isn't necessary when you can use MARK and iproute2, but I think it is really convenient. In fact I use ROUTE and condition together in some installation with failover connections.

I think I'm missing your goal.

Signature · 3 lines
Saluti,
   Massimiliano Hofer
        Nucleus
#6 lore raw
Quoted 7 lines · Massimiliano Hofer
Quoted 2 lines · Patrick McHardy

It was discussed at the netfilter workshops, summaries are available at workshop.netfilter.org.

I did a cursory check of the site, but I only found a 5 or 6 lines summary for every conference. Google doesn't help either.
May you direct me to a more comprehensive report, please?

There is no big discussion about condition online but one line at
http://workshop.netfilter.org/2004/ :

6.2. Decisions about individual patches
...
condition Stays in POM, because it's ugly and ruleset updates are faster these days

I don't know more than that because I didn't attend that netfilter workshop.

I already had a discussion about the future of condition with Harald back in 2004 and he basically said the same as Patrick now:

http://thread.gmane.org/gmane.comp.security.firewalls.netfilter.devel/5694

I think something like condition is needed as temporary solution until the proposed fast and reliable way to change rules is available. I very much appreciate your work on porting it to 2.6, I already had planned doing that in the nex two or three month.

Kind regards,

Gerd

#7 lore raw
In reply to Massimiliano Hofer
Quoted 7 lines · Massimiliano Hofer
Quoted 2 lines · Patrick McHardy

It was discussed at the netfilter workshops, summaries are available at workshop.netfilter.org.

I did a cursory check of the site, but I only found a 5 or 6 lines summary for every conference. Google doesn't help either.
May you direct me to a more comprehensive report, please?

There is no big discussion about condition online but one line at
http://workshop.netfilter.org/2004/ :

6.2. Decisions about individual patches
...
condition Stays in POM, because it's ugly and ruleset updates are faster these days

I don't know more than that because I didn't attend that netfilter workshop.

I already had a discussion about the future of condition with Harald back in 2004 and he basically said the same as Patrick now:

http://thread.gmane.org/gmane.comp.security.firewalls.netfilter.devel/5694

I think something like condition is needed as temporary solution until the proposed fast and reliable way to change rules is available. I very much appreciate your work on porting it to 2.6, I already had planned doing that in the nex two or three month.

Kind regards,

Gerd

#8 lore raw
Quoted 12 lines · Gerd v. Egidy
Quoted 8 lines · Massimiliano Hofer
Quoted 2 lines · Patrick McHardy

It was discussed at the netfilter workshops, summaries are available at workshop.netfilter.org.

I did a cursory check of the site, but I only found a 5 or 6 lines summary for every conference. Google doesn't help either.
May you direct me to a more comprehensive report, please?

Quoted 5 lines

There is no big discussion about condition online but one line at http://workshop.netfilter.org/2004/ :

6.2. Decisions about individual patches
...
condition Stays in POM, because it's ugly and ruleset updates are faster these days

I like it's "ugliness"

Quoted 7 lines

I don't know more than that because I didn't attend that netfilter workshop.

I already had a discussion about the future of condition with Harald back in 2004 and he basically said the same as Patrick now:

http://thread.gmane.org/gmane.comp.security.firewalls.netfilter.devel/5694

I think something like condition is needed as temporary solution until the proposed fast and reliable way to change rules is available. I very much appreciate your work on porting it to 2.6, I already had planned doing that in the nex two or three month.

ipt_condition allows the abstraction of enabling/disabling features of an iptables ruleset.

If you remove a rule, you have destroyed the information as to where in the ruleset it belonged. Removing a rule is conceptually different from disabling it.

One exaple is use of ipt_redirect to a trasparent web proxy, the rule exists among a sequence of rules as not all users should have their traffic via the proxy depnding on source-ip/target-ip combinations as well as marks (from other rules) and then ipt_condition to enable or disable the interception.

To remove the intercept rule is to lose the prioritisation information. It could be implemented via sub-chains, which is to my mind more messy.

I will say that ipt_condition will not go away. The code is simple enough, the only objection I find is that one if its uses is to overcome a deficiency in iptables management tools. It may stay in pom, but it provides too useful an interface to go away.

Sam

#9 lore raw
In reply to Patrick McHardy
Quoted 3 lines · Patrick McHardy

We have already decided that the condition match will not be merged because the same thing can easily be done by adding/removing rules from userspace.

Conditions enable role separation between us admins.

No matter how good or fast the userspace tools are, there are cases where you simply do not want to (let others) run them, but it is acceptable to (let others) turn on/off some predefined blocks of rules.

Plus it is faster, less risky, and does not reset counters.

Simon

#10 lore raw
Quoted 8 lines · Simon Lodal
Quoted 3 lines · Patrick McHardy

We have already decided that the condition match will not be merged because the same thing can easily be done by adding/removing rules from userspace.

Quoted 7 lines

Conditions enable role separation between us admins.

No matter how good or fast the userspace tools are, there are cases where you simply do not want to (let others) run them, but it is acceptable to (let others) turn on/off some predefined blocks of rules.

Plus it is faster, less risky, and does not reset counters.

I'm not really buying that argument, this can all also be done in userspace. But a lot of people seem to consider it useful, so I might reconsider if someone cleans it up so it at least doesn't need to walk the list of conditions for every packet it matches .. but no promises.

#11 lore raw
Quoted 4 lines · Patrick McHardy

I'm not really buying that argument, this can all also be done in userspace. But a lot of people seem to consider it useful, so I might reconsider if someone cleans it up so it at least doesn't need to walk the list of conditions for every packet it matches .. but no promises.

I'll set to work on it. I'll need to change the userspace interface, though. The only O(1) way to do it is to store a pointer (or any other id) in the rule itself. I didn't do it in the previous version because I though this was really ugly. I can't find any other match doing a similar thing. Anyway I can do it.

On the other hand I can make it a guaranteed O(log n) or average O(1) without meddling the rule descriptor and with compatible userspace. What do you prefer?

Signature · 3 lines
Saluti,
   Massimiliano Hofer
        Nucleus
#12 lore raw
Quoted 9 lines · Massimiliano Hofer
Quoted 4 lines · Patrick McHardy

I'm not really buying that argument, this can all also be done in userspace. But a lot of people seem to consider it useful, so I might reconsider if someone cleans it up so it at least doesn't need to walk the list of conditions for every packet it matches .. but no promises.

Quoted 5 lines

I'll set to work on it. I'll need to change the userspace interface, though. The only O(1) way to do it is to store a pointer (or any other id) in the rule itself. I didn't do it in the previous version because I though this was really ugly. I can't find any other match doing a similar thing. Anyway I can do it.

Unfortunately its ugly, but this is a well-known limitation of iptables itself. Since its the only way to do certain things, I won't complain if this part is ugly :)

Quoted 3 lines

On the other hand I can make it a guaranteed O(log n) or average O(1) without meddling the rule descriptor and with compatible userspace. What do you prefer?

How would you achieve O(1) average?

#13 lore raw
Quoted 9 lines · Patrick McHardy
Quoted 5 lines

I'll set to work on it. I'll need to change the userspace interface, though. The only O(1) way to do it is to store a pointer (or any other id) in the rule itself. I didn't do it in the previous version because I though this was really ugly. I can't find any other match doing a similar thing. Anyway I can do it.

Unfortunately its ugly, but this is a well-known limitation of iptables itself. Since its the only way to do certain things, I won't complain if this part is ugly :)

OK. This time I warned you. :)

Quoted 5 lines
Quoted 3 lines

On the other hand I can make it a guaranteed O(log n) or average O(1) without meddling the rule descriptor and with compatible userspace. What do you prefer?

How would you achieve O(1) average?

Hash. But it adds complexity to the code and unnecessary complexity is a form of ugliness.

While we're talking about varying degrees of ugliness, how bad would it be if I optionally allowed to keep a persistent state across rule removal and reinsertion (for example whene someone flushes the table and restarts the firewalling script)?
I concede that this would really be easy to do in userspace, so maybe I'm answering myself. :)

Signature · 3 lines
Saluti,
   Massimiliano Hofer
        Nucleus
#14 lore raw
Quoted 4 lines · Massimiliano Hofer
Quoted 1 line

How would you achieve O(1) average?

Hash. But it adds complexity to the code and unnecessary complexity is a form of ugliness.

Why don't you choose an array and its indices? Array size could be specified by module parameter if the default were not sufficient for someone.

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary
#15 lore raw
Quoted 5 lines · Jozsef Kadlecsik
Quoted 2 lines · Massimiliano Hofer
Quoted 1 line

How would you achieve O(1) average?

Quoted 7 lines
Quoted 2 lines

Hash. But it adds complexity to the code and unnecessary complexity is a form of ugliness.

Why don't you choose an array and its indices? Array size could be specified by module parameter if the default were not sufficient for someone.

I just removed something similar from the nth match :) It used a hard-coded size, but either way I don't like introducing this limitation just to work around the small uglyness required to keep a pointer inside the per-instance match data. We already have multiple precedents for this.

#16 lore raw

Hi,

Quoted 4 lines · Patrick McHardy

hard-coded size, but either way I don't like introducing this limitation just to work around the small uglyness required to keep a pointer inside the per-instance match data. We already have multiple precedents for this.

Indeed, there are multiple precedents for this. For example: CLUSTERIP
(already in mainline) is by far the ugliest hack I've seen in any Netfilter code. :)

Signature · 2 lines
 Regards,
  Krisztian Kovacs
#17 lore raw
Quoted 10 lines · KOVACS Krisztian

Hi,

Quoted 4 lines · Patrick McHardy

hard-coded size, but either way I don't like introducing this limitation just to work around the small uglyness required to keep a pointer inside the per-instance match data. We already have multiple precedents for this.

Quoted 3 lines

Indeed, there are multiple precedents for this. For example: CLUSTERIP
(already in mainline) is by far the ugliest hack I've seen in any Netfilter code. :)

Hehe :) Yes, its not nice, but I refuse to add stupid limits just because the infrastructure can't cleanly support per-instance state (which is what makes it ugly, its not the shared state). And its not so bad, the uglyness comes down to two or three extra lines of code, one additional pointer in the data structure and one offsetof in userspace.

#18 lore raw
Quoted 6 lines · Patrick McHardy

Hehe :) Yes, its not nice, but I refuse to add stupid limits just because the infrastructure can't cleanly support per-instance state (which is what makes it ugly, its not the shared state). And its not so bad, the uglyness comes down to two or three extra lines of code, one additional pointer in the data structure and one offsetof in userspace.

Nontheless, I think modifying userspace because 2 kernel functions can't talk to each other is a bit silly.
It would be relatively easy to modify the kernel in order to support instance data (extensive, but simple and relatively safe).
Do you think there would be a strong resistance to a patch in mainline kernel?

Signature · 3 lines
Saluti,
   Massimiliano Hofer
        Nucleus
#19 lore raw
Quoted 11 lines · Massimiliano Hofer
Quoted 6 lines · Patrick McHardy

Hehe :) Yes, its not nice, but I refuse to add stupid limits just because the infrastructure can't cleanly support per-instance state (which is what makes it ugly, its not the shared state). And its not so bad, the uglyness comes down to two or three extra lines of code, one additional pointer in the data structure and one offsetof in userspace.

Quoted 5 lines

Nontheless, I think modifying userspace because 2 kernel functions can't talk to each other is a bit silly.
It would be relatively easy to modify the kernel in order to support instance data (extensive, but simple and relatively safe).
Do you think there would be a strong resistance to a patch in mainline kernel?

It is silly, but we have to live with it. I see two possibilities, one breaks userspace, one is extremly expensive. I like neither one. We should just add a clean netlink configuration interface modelled after the existing ones instead of building workarounds in this clearly broken interface. I mostly don't care about costs of a compatibility layer if we get a sane infrastructure in return.

#20 lore raw
Quoted 7 lines · Patrick McHardy
Quoted 6 lines

Nontheless, I think modifying userspace because 2 kernel functions can't talk to each other is a bit silly.
It would be relatively easy to modify the kernel in order to support instance data (extensive, but simple and relatively safe). Do you think there would be a strong resistance to a patch in mainline kernel?

Quoted 6 lines

It is silly, but we have to live with it. I see two possibilities, one breaks userspace, one is extremly expensive. I like neither one. We should just add a clean netlink configuration interface modelled after the existing ones instead of building workarounds in this clearly broken interface. I mostly don't care about costs of a compatibility layer if we get a sane infrastructure in return.

I agree about the 2 non invasive (with respect to the mainline kernel) solutions.

If I could choose freely I would change these structures (and all corresponding references and initialization code) as follows:

struct xt_match
{
...
int (*match)(const struct sk_buff *skb,
const struct net_device *in, const struct net_device *out, const void *matchinfo, int offset,
unsigned int protoff, int *hotdrop,
void *instance_data);

int (*checkentry)(const char *tablename,
const void *ip, void *matchinfo, unsigned int matchinfosize, unsigned int hook_mask, void **instance_data);

void (*destroy)(void *matchinfo,
unsigned int matchinfosize, void *instance_data);
...
};

struct ipt_entry_match
{
...

	void *instance_data;
        unsigned char data[0];
};

This would solve the general problem of rule specific data (tracking, state, accounting, whatever).
It gives more expressiveness to the API in general and would have little impact on the parts of the code that do not use it (instance_data is initialized as NULL and never used).
The performance penalty would be negligible (4 or 8 more bytes in every descriptor and in every function call to the match function).

We could minimize impact on the existing code introducing a "normal" and an "extended" match funciton prototype, but this would crete complexity and its only purpose is if we don't want to upgrade everything.

The problem with my solution is:
- it's work (I could do it, so it's not your problem :));
- the kernel people may reject it or delay it for an extended period of time;
- while we the patch is available and the kernel people does not accept it we'll be stuck with 2 sets of incompatible netfilter APIs with all kinds of inconveniences while everyone updates their function declarations (we already survived one such change with 2.6.16).

I ask you these questions:
- is my proposal a desirable improvement?
- is there any other way or any additional improvement that we can do?
- would you support it for inclusion in mainline?
- would the kernel people like it?

If you think this is worth the trouble and promise to support the patch for inclusion in mainline (at least after we agree to the implemetation details) I will start working on it.

Signature · 3 lines
Saluti,
   Massimiliano Hofer
        Nucleus
#21 lore raw
In reply to Jozsef Kadlecsik
Quoted 3 lines · Jozsef Kadlecsik

Why don't you choose an array and its indices? Array size could be specified by module parameter if the default were not sufficient for someone.

I would need a way to get the index.
If I put information back in the rule descriptor I can just store a pointer (the ultimate array index). If I don't touch the descriptor I'm stuck with a pointer to the descriptor and any information I can obtain with it, but no unique identifier that is immediately be used as an index in an array.

Signature · 3 lines
Saluti,
   Massimiliano Hofer
        Nucleus
#22 lore raw
In reply to Massimiliano Hofer
Quoted 6 lines · Massimiliano Hofer

While we're talking about varying degrees of ugliness, how bad would it be if I optionally allowed to keep a persistent state across rule removal and reinsertion (for example whene someone flushes the table and restarts the firewalling script)?
I concede that this would really be easy to do in userspace, so maybe I'm answering myself. :)

Now we're talking about _really_ ugly :) How and when would you remove these? An iptables rule state garbage collector? :) I think this really should be done in userspace.

#23 lore raw
Quoted 3 lines · Patrick McHardy

Now we're talking about _really_ ugly :) How and when would you remove these? An iptables rule state garbage collector? :) I think this really should be done in userspace.

rm /proc/net/nf_condition/x?

Uglier than ever! :)

Signature · 3 lines
Saluti,
   Massimiliano Hofer
        Nucleus