Skip to contentSkip to conversation

Condition Match

NEW EXTENSION Stephane Ouellette 6 participants 9 messages Oct 29, 2002 – Nov 2, 2002

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

Folks,

I developped last week a new extension to Netfilter in order to
enable or disable a set of rules using /proc files.

As an example, I like to play Quake III Aerna with a few friends of
mine but I don't want to keep the firewall rules that make them able to connect to my computer active at all times. Adding and removing these specific rules is annoying...

So my solution is illustrated by the following rules:

iptables -t nat -A PREROUTING -p udp -m condition -i $INTERNET --dport 27960 --condition quake -j DNAT --to-destination $MYCOMPUTER iptables -t filter -A FORWARD -p udp -m condition -i $INTERNET -o $LAN --dport 27960 --condition quake -j ACCEPT iptables -t filter -A FORWARD -p udp -m condition -i $LAN -o $INTERNET --dport 27960 --condition quake -j ACCEPT

To enable the rules, issue: echo 1 > /proc/net/ipt_condition/quake
To disable the rules, issue: echo 0> /proc/net/ipt_condition/quake

The /proc files are created automagically when a new condition is defined and destroyed when no rule points to them anymore.

As of this writing, the condition match module supports only one condition per rule. I think I will add in a near future the possibility to match on many conditions at the same time, using the syntax: --condition file1,file2,file3. . .

- It was tested under kernel 2.4.20-pre10-ac2
- So far so good, the condition match module seems stable !

Any comments, bug reports or suggestions would be greatly appreciated.

Enjoy !

Stephane.

include/linux/netfilter_ipv4/ipt_condition.h +11 -0
diff -aruN linux-2.4.20-pre10-ac1-machiavel/include/linux/netfilter_ipv4/ipt_condition.h linux-2.4.20-pre10-ac1-machiavel-new/include/linux/netfilter_ipv4/ipt_condition.h
@@ -0,0 +1,11 @@
+#ifndef __IPT_CONDITION_MATCH__
+#define __IPT_CONDITION_MATCH__
+
+#define VARIABLE_NAME_LEN  32
+
+struct condition_info {
+  char	name[VARIABLE_NAME_LEN];
+  int   invert;
+};
+
+#endif
net/ipv4/netfilter/ipt_condition.c +227 -0

227 lines not on this page · raw message

dep_tristate ' TOS match support' CONFIG_IP_NF_MATCH_TOS $CONFIG_IP_NF_IPTABLES dep_tristate ' condition match support' CONFIG_IP_NF_MATCH_CONDITION $CONFIG_IP_NF_IPTABLES

CONFIG_IP_NF_MATCH_TOS
condition match support
CONFIG_IP_NF_MATCH_CONDITION
This option allows you to match firewall rules against condition variables stored in /proc files.

If you want to compile it as a module, say M here and read Documentation/modules.txt. If unsure, say `N'.

Author: Stephane Ouellette <[email protected]>
Status: ItWorksForMe(tm)

This patch adds CONFIG_IP_NF_MATCH_CONDITION which allows you to match firewall rules against condition variables stored in /proc files.

obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o

obj-$(CONFIG_IP_NF_MATCH_CONDITION) += ipt_condition.o

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <iptables.h>

#include<linux/netfilter_ipv4/ip_tables.h>
#include<linux/netfilter_ipv4/ipt_condition.h>

static void help(void)
{
printf("condition match v%s options:\n"
"--condition [!] filename Match on boolean value stored in /proc file"
"\n", IPTABLES_VERSION);
}

static struct option opts[] = { { "condition", 1, 0, 'X' }, { 0 } };

static void init(struct ipt_entry_match *m, unsigned int *nfcache) {
*nfcache |= NFC_UNKNOWN;
}

static int parse(int c, char **argv, int invert, unsigned int *flags,
const struct ipt_entry *entry, unsigned int *nfcache, struct ipt_entry_match **match)
{
struct condition_info *info = (struct condition_info*)(*match)->data;

  check_inverse(optarg, &invert, &optind, 0);

  if(*flags)
    exit_error(PARAMETER_PROBLEM, "Can't specify multiple conditions");

  if(c == 'X')
  {
    if(strlen(argv[optind-1]) < VARIABLE_NAME_LEN)
      strcpy(info->name, argv[optind-1]);
    else
      exit_error(PARAMETER_PROBLEM, "File name too long");
    info->invert = invert;
    *flags = 1;
    return 1;

}

return 0;
}

static void final_check(unsigned int flags)
{
if(!flags)

    exit_error(PARAMETER_PROBLEM, "Condition match: must specify --condition");
}

static void print(const struct ipt_ip *ip,
const struct ipt_entry_match *match, int numeric)
{
const struct condition_info *info = (const struct condition_info*)match->data;

printf("condition %s%s ", (info->invert) ? "!" : "", info->name);
}

static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match) {
const struct condition_info *info = (const struct condition_info*)match->data;

printf("--condition %s%s ", (info->invert) ? "! " : "", info->name);
}

static struct iptables_match condition = {
NULL,
"condition",
IPTABLES_VERSION,
IPT_ALIGN(sizeof(struct condition_info)),
IPT_ALIGN(sizeof(struct condition_info)),
&help,
&init,
&parse,
&final_check,
&print,
&save,
opts
};

void _init(void)
{
register_match(&condition);
}

#1 lore raw

Mr. Oullette,

Quoted 20 lines · --- Stephane Ouellette

Folks,

I developped last week a new extension to Netfilter in order to
enable or disable a set of rules using /proc files.

As an example, I like to play Quake III Aerna with a few friends of
mine but I don't want to keep the firewall rules that make them able to connect to my computer active at all times. Adding and removing these specific rules is annoying...

So my solution is illustrated by the following rules:

iptables -t nat -A PREROUTING -p udp -m condition -i $INTERNET --dport 27960 --condition quake -j DNAT --to-destination $MYCOMPUTER iptables -t filter -A FORWARD -p udp -m condition -i $INTERNET -o $LAN --dport 27960 --condition quake -j ACCEPT
iptables -t filter -A FORWARD -p udp -m condition -i $LAN -o $INTERNET --dport 27960 --condition quake -j ACCEPT

Quoted 2 lines

To enable the rules, issue: echo 1 > /proc/net/ipt_condition/quake
To disable the rules, issue: echo 0> /proc/net/ipt_condition/quake

Useful. I can think of dozens of different uses for soemthing like this.

Quoted 9 lines

The /proc files are created automagically when a new condition is defined and destroyed when no rule points to them anymore.

As of this writing, the condition match module supports only one condition per rule. I think I will add in a near future the possibility to match on many conditions at the same time, using the syntax:
--condition file1,file2,file3. . .

IMO you should be careful here. Adding too many of these just clutters things up and makes it hard to understand.

Quoted 5 lines

- It was tested under kernel 2.4.20-pre10-ac2
- So far so good, the condition match module seems stable !

Any comments, bug reports or suggestions would be greatly appreciated.

One suggestion: take a look at iptables2 and code up a patch for this which uses netlink to check&control the conditions. Also, try a patch for IPv6, IMHO.

Quoted 5 lines

Enjoy !

Stephane.

Brad

=====
Brad Chapman

Permanent e-mail: [email protected]

__________________________________________________
Do you Yahoo!?
HotJobs - Search new jobs daily now
http://hotjobs.yahoo.com/

#2 lore raw
In reply to Stephane Ouellette
Quoted 2 lines · Stephane Ouellette

To enable the rules, issue: echo 1 > /proc/net/ipt_condition/quake
To disable the rules, issue: echo 0> /proc/net/ipt_condition/quake

Just one suggestion with this, is /proc/net the right part of /proc for this?
/proc/net seems to be statistics reporting only, with /proc/net/sys/ipv{4,6} being where options are controlled.

Quoted 2 lines

As of this writing, the condition match module supports only one condition per rule. I think I will add in a near future the possibility to match on many conditions at the same time, using the syntax:
--condition file1,file2,file3. . .

This may be getting somewhat complex, but would it be possible to have binary ops on the conditions?
eg: --condition file1 --conditionand file2 etc. But this MIGHT be getting a little complex IMHO ;-).

Other than that, it looks like a wonderful idea to be able to turn off/on rules as needed.

#3 lore raw
In reply to Stephane Ouellette
Quoted 2 lines · Stephane Ouellette

I developped last week a new extension to Netfilter in order to
enable or disable a set of rules using /proc files.

Yeah, as others have said, the idea is definitely cool.

I hate to admit that I might use it this way. God help me...

I'm having visions of debugging impossibly complex booboo's, and chasing after phantom problems that appear and disappear and not remembering that I or someone had done or could have done this.

Can you think of a way to implement far greater "manageability"

?

Or what are your thoughts about that ?

Maybe it is a "user-be-ware" thing best left to some odd application to keep track of.

Hmn...

But... the "rules" would appear though... right ?

With an "iptables -L" ?

And current "state" ?

-AEF

#4 lore raw
Quoted 8 lines · allen
Quoted 2 lines · Stephane Ouellette

I developped last week a new extension to Netfilter in order to
enable or disable a set of rules using /proc files.

Yeah, as others have said, the idea is definitely cool.

Though the idea is cool, I think we are solving a problem the wrong way. Why add complexity to the kernel for a problem which can be solved without any problem from userspace?

Where is the problem in having a couple of different rulesets (e.g. created with iptables-save) which are then loaded using an iptables-restore commandline or a script at the shell of the firewall?

Signature · 5 lines
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/
============================================================================
"If this were a dictatorship, it'd be a heck of a lot easier, just so long
 as I'm the dictator."  --  George W. Bush Dec 18, 2000
#5 lore raw
Quoted 6 lines · Harald Welte
Quoted 3 lines
Quoted 2 lines

I developped last week a new extension to Netfilter in order to
enable or disable a set of rules using /proc files.

Yeah, as others have said, the idea is definitely cool.

Though the idea is cool, I think we are solving a problem the wrong way. Why add complexity to the kernel for a problem which can be solved without any problem from userspace?

Well, the same would apply to time and pool matches, wouldn't it? They made it into patch-o-matic though.

Quoted 2 lines

Live long and prosper
- Harald Welte / [email protected] http://www.gnumonks.org/

Bye,

Peter Surda (Shurdeek) <[email protected]>, ICQ 10236103, +436505122023

--
Saving Private Ryan...
Private Ryan exists. Overwrite? (Y/N)

#6 lore raw
In reply to Harald Welte

Harald,

Quoted 10 lines · --- Harald Welte
Quoted 8 lines · allen
Quoted 2 lines · Stephane Ouellette

I developped last week a new extension to Netfilter in order to
enable or disable a set of rules using /proc files.

Yeah, as others have said, the idea is definitely cool.

Quoted 7 lines

Though the idea is cool, I think we are solving a problem the wrong way. Why add complexity to the kernel for a problem which can be solved without any problem from userspace?

Where is the problem in having a couple of different rulesets (e.g. created with iptables-save) which are then loaded using an iptables-restore commandline or a script at the shell of the firewall?

Because right now the method of reloading rules is still inefficient. What if you have a ruleset of 1000-10000 rules and only want to toggle 5? Removing those rules would require a full replacement of the ruleset for the table and chain the rules were placed in (IIRC). Doing it this way allows you to disable/enable rules without having to actually modify them.

Maybe once nfnetlink goes into the 2.7 kernel this will go away, but that's a long time IMHO.....

Quoted 5 lines
Signature · 3 lines
-- 
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/

Brad

=====
Brad Chapman

Permanent e-mail: [email protected]

__________________________________________________
Do you Yahoo!?
HotJobs - Search new jobs daily now
http://hotjobs.yahoo.com/

#7 lore raw
In reply to allen
Quoted 18 lines · allen
Quoted 4 lines · Stephane Ouellette

I developped last week a new extension to Netfilter in order to
enable or disable a set of rules using /proc files.

Yeah, as others have said, the idea is definitely cool.

I hate to admit that I might use it this way. God help me...

I'm having visions of debugging impossibly complex booboo's, and chasing after phantom problems that appear and disappear and not remembering that I or someone had done or could have done this.

Quoted 5 lines

Can you think of a way to implement far greater "manageability"

?

I'm not sure to understand your question... :-(

Quoted 13 lines

Or what are your thoughts about that ?

Maybe it is a "user-be-ware" thing best left to some odd application to keep track of.

Hmn...

But... the "rules" would appear though... right ?

With an "iptables -L" ?

The rules appear with "iptables -L" such as :

0 0 ACCEPT udp --- * * 0.0.0.0/0 0.0.0.0/0 condition quake udp dpt:27960

Quoted 2 lines

And current "state" ?

No, the current state is only available if you read the /proc file.

Quoted 4 lines

-AEF

#8 lore raw
In reply to Stephane Ouellette
Quoted 4 lines · Stephane Ouellette

Folks,

I developped last week a new extension to Netfilter in order to
enable or disable a set of rules using /proc files.

I've added it to patch-o-matic.

btw: you were missing the userspace/extensions/.condition-test script :)

Quoted 2 lines

Enjoy !
Stephane.

Signature · 5 lines
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/
============================================================================
"If this were a dictatorship, it'd be a heck of a lot easier, just so long
 as I'm the dictator."  --  George W. Bush Dec 18, 2000