Skip to contentSkip to conversation

Condition variable match module

RELEASE Stephane Ouellette 2 participants 2 messages Feb 11, 2003 – Feb 13, 2003

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

Folks,

here's the second version of the condition variable match module.
There are two separate sets of patches in order to support ip6tables.

Regards,

Stephane Ouellette.

include/linux/netfilter_ipv4/ipt_condition.h +11 -0
diff -aruN linux-2.4.21-pre4-ac3-orig/include/linux/netfilter_ipv4/ipt_condition.h linux-2.4.21-pre4-ac3/include/linux/netfilter_ipv4/ipt_condition.h
@@ -0,0 +1,11 @@
+#ifndef __IPT_CONDITION_MATCH__
+#define __IPT_CONDITION_MATCH__
+
+#define CONDITION_NAME_LEN  32
+
+struct condition_info {
+  char	name[CONDITION_NAME_LEN];
+  int   invert;
+};
+
+#endif
net/ipv4/netfilter/ipt_condition.c +247 -0

247 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 variable match support CONFIG_IP_NF_MATCH_CONDITION
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'.

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 the /proc/net/ipt_condition directory. Multiple rules can match on a single condition variable.

Example:
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

To disable this rule:
echo 0 > /proc/net/ipt_condition/web_ok

obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o obj-$(CONFIG_IP_NF_MATCH_CONDITION) += ipt_condition.o

include/linux/netfilter_ipv6/ip6t_condition.h +11 -0
diff -aruN linux-2.4.21-pre4-ac3-orig/include/linux/netfilter_ipv6/ip6t_condition.h linux-2.4.21-pre4-ac3/include/linux/netfilter_ipv6/ip6t_condition.h
@@ -0,0 +1,11 @@
+#ifndef __IP6T_CONDITION_MATCH__
+#define __IP6T_CONDITION_MATCH__
+
+#define CONDITION6_NAME_LEN  32
+
+struct condition6_info {
+  char	name[CONDITION6_NAME_LEN];
+  int   invert;
+};
+
+#endif
net/ipv6/netfilter/ip6t_condition.c +243 -0

243 lines not on this page · raw message

dep_tristate ' MAC address match support' CONFIG_IP6_NF_MATCH_MAC $CONFIG_IP6_NF_IPTABLES dep_tristate ' Condition variable match support' CONFIG_IP6_NF_MATCH_CONDITION $CONFIG_IP6_NF_IPTABLES

CONFIG_IP6_NF_MATCH_MARK
Condition variable match support CONFIG_IP6_NF_MATCH_CONDITION
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'.

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

This patch adds CONFIG_IP6_NF_MATCH_CONDITION which allows you to match firewall rules against condition variables stored in the /proc/net/ip6t_condition directory. Multiple rules can match on a single condition variable.

Example:
ip6tables -A INPUT -p tcp -m condition --condition web_ok --dport 80 -j ACCEPT

To allow this rule to match:
echo 1 > /proc/net/ip6t_condition/web_ok

To disable this rule:
echo 0 > /proc/net/ip6t_condition/web_ok

obj-$(CONFIG_IP6_NF_MATCH_MARK) += ip6t_mark.o obj-$(CONFIG_IP6_NF_MATCH_CONDITION) += ip6t_condition.o

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

#include<linux/netfilter_ipv6/ip6_tables.h>
#include<linux/netfilter_ipv6/ip6t_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[] = {
{ .name = "condition", .has_arg = 1, .flag = 0, .val = 'X' }, { .name = 0 }
};

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

static int parse(int c, char **argv, int invert, unsigned int *flags,
const struct ip6t_entry *entry, unsigned int *nfcache, struct ip6t_entry_match **match)
{
struct condition6_info *info = (struct condition6_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]) < CONDITION6_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 ip6t_ip6 *ip, const struct ip6t_entry_match *match, int numeric) {
const struct condition6_info *info = (const struct condition6_info*)match->data;

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

static void save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match) {
const struct condition6_info *info = (const struct condition6_info*)match->data;

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

static struct ip6tables_match condition = {

  .next          = NULL,
  .name          = "condition",
  .version       = IPTABLES_VERSION,
  .size          = IP6T_ALIGN(sizeof(struct condition6_info)),
  .userspacesize = IP6T_ALIGN(sizeof(struct condition6_info)),
  .help          = &help,
  .init          = &init,
  .parse         = &parse,
  .final_check   = &final_check,
  .print         = &print,
  .save          = &save,
  .extra_opts    = opts

};

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

#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[] = {
{ .name = "condition", .has_arg = 1, .flag = 0, .val = 'X' }, { .name = 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]) < CONDITION_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 = {

  .next          = NULL,
  .name          = "condition",
  .version       = IPTABLES_VERSION,
  .size          = IPT_ALIGN(sizeof(struct condition_info)),
  .userspacesize = IPT_ALIGN(sizeof(struct condition_info)),
  .help          = &help,
  .init          = &init,
  .parse         = &parse,
  .final_check   = &final_check,
  .print         = &print,
  .save          = &save,
  .extra_opts    = opts

};

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

#!/bin/sh
# True if condition is applied.
[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_condition.h ] && echo condition

#!/bin/sh
# True if condition6 is applied.
[ -f $KERNEL_DIR/include/linux/netfilter_ipv6/ip6t_condition.h ] && echo condition

#1 lore raw
Quoted 4 lines · Stephane Ouellette

Folks,

here's the second version of the condition variable match module.
There are two separate sets of patches in order to support ip6tables.

Nice work. I'm still not decided on whether this is really something useful or rather consider it as some 'hack' ;)

Anyway, I'll put it in patch-o-matic, if a couple of issues are resolved:

- adhere to /usr/src/linux/Documentation/CodingStyle, esp.
- indentation
- opening braces '{' not at seperate line
- No C++ comments in C code
- if and for are not functions, space before '('
- return is not a function, no () around retval.
- indicate on top of the sourcecode that the code is released under the terms of GNU GPL

Quoted 2 lines

Regards,
Stephane Ouellette.

Signature · 4 lines
- 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