New target and match
Subsystems (2)
Hi all,
We created a match and target which I have attached to this mail that helps us to stop ongoing session when they were not longer allowed by the rule set, after a change in the rule set. This is very simple if your rule set is always evaluated for each packet and you are not using conntrack. If you are using conntrack you would like to accept packets form established, related sessions as quickly as possible, though killing not longer accepted ones. This patch adds one Confirmed bit to the conntrack table which can be set by the CONFIRMED target, and match with confirmed match. To clear the bit we did a "quick and dirty" hack that each time a rule is installed that contains the confirmed match all sessions in the conntrack gets the confirmed bit cleared. Since we use iptables-restore to change our rule set this works quite nice for us. The intention to submit this patch is to bring up the issue and look for better alternatives, though is totally functional and used in all our products.
Thanks for iptables,
/flavio
Signature · 5 lines
Flavio Pescuma MARA Systems AB P +46 8 681 0840 F +46 681 0841 M +46 733548833 [email protected] http://www.marasystems.com
include/linux/netfilter_ipv4/ip_conntrack.h +4 -0
@@ -46,6 +46,10 @@/* Connection is confirmed: originating packet has left box */ IPS_CONFIRMED_BIT =3 ,IPS_CONFIRMED = (1 <<IPS_CONFIRMED_BIT ), + +/* Confirm bit after re installing the rule set */ +IPS_CONFIRMED_RULE_BIT =4 , + IPS_CONFIRMED_RULE = (1 << IPS_CONFIRMED_RULE_BIT), };#include <linux/netfilter_ipv4/ip_conntrack_tcp.h>
net/ipv4/netfilter/ipt_confirmed.c +49 -0
49 lines not on this page · raw message
net/ipv4/netfilter/ipt_CONFIRMED.c +59 -0
59 lines not on this page · raw message
dep_tristate ' Connection state match support' CONFIG_IP_NF_MATCH_STATE $CONFIG_IP_NF_CONNTRACK $CONFIG_IP_NF_IPTABLES dep_tristate ' Connection confirm match support' CONFIG_IP_NF_MATCH_CONFIRMED $CONFIG_IP_NF_CONNTRACK $CONFIG_IP_NF_IPTABLES
dep_tristate ' LOG target support' CONFIG_IP_NF_TARGET_LOG $CONFIG_IP_NF_IPTABLES dep_tristate ' CONFIRMED target support' CONFIG_IP_NF_TARGET_CONFIRMED $CONFIG_IP_NF_IPTABLES
Author: Flavio Pescuma <[email protected]>
Status: works for me
This patch adds a new virtual conntrack state bit CONFIRMED, indicating that this connection has been confirmed by the ruleset.
Usage:
confirmed
This module matches connections having the CONFIRMED bit set.
CONFIRMED
This is used to set the CONFIRMED bit on the connection
To do:
The CONFIRMED bit should probably be automated to become an integral part of conntrack.
The clearing of the CONFIRMED bit should be moved up to iptables as an option to the iptables command
Unconfirmed EXPECTED should be seen as RELATED by the state match. Now they are being seen as CONNECTED if the RELATED connection has been established I think.
obj-$(CONFIG_IP_NF_MATCH_STATE) += ipt_state.o obj-$(CONFIG_IP_NF_MATCH_CONFIRMED) += ipt_confirmed.o
obj-$(CONFIG_IP_NF_TARGET_LOG) += ipt_LOG.o obj-$(CONFIG_IP_NF_TARGET_CONFIRMED) += ipt_CONFIRMED.o
/* Shared library add-on to iptables to add CONNMARK target support. */ #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <iptables.h>
#include <linux/netfilter_ipv4/ip_tables.h>
/* Function which prints out usage message. */
static void
help(void)
{
printf("CONFIRMED target v%s:\n"
"\n",IPTABLES_VERSION);
}
static struct option opts[] = {
{ 0 }
};/* Initialize the target. */
static voidinit(struct ipt_entry_target *t, unsigned int *nfcache) {
}
/* Function which parses command options; returns true if it
ate an option */
static int
parse(int c, char **argv, int invert, unsigned int *flags,
const struct ipt_entry *entry,
struct ipt_entry_target **target)
{
return 1;
}static voidfinal_check(unsigned int flags)
{
}
/* Prints out the targinfo. */
static void
print(const struct ipt_ip *ip,
const struct ipt_entry_target *target,
int numeric)
{
printf("CONFIRMED ");
}/* Saves the union ipt_targinfo in parsable form to stdout. */ static voidsave(const struct ipt_ip *ip, const struct ipt_entry_target *target) {
}
static
struct iptables_target confirmed
= { NULL,
"CONFIRMED",
IPTABLES_VERSION,
IPT_ALIGN(0),
IPT_ALIGN(0),
&help,
&init,
&parse,
&final_check,
&print,
&save,
opts
};void _init(void)
{
register_target(&confirmed);
}/* Shared library add-on to iptables to add CONNMARK matching support. */ #include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <iptables.h>
/* Function which prints out usage message. */
static void
help(void)
{
printf("CONFIRMED match v%s:\n"
"\n",IPTABLES_VERSION);
}
static struct option opts[] = {
{0}
};/* Initialize the match. */
static voidinit(struct ipt_entry_match *m, unsigned int *nfcache)
{
/* Can't cache this. */
*nfcache |= NFC_UNKNOWN;
}/* Function which parses command options; returns true if it
ate an option */
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)
{
return 1;
}/* Final check; must have specified --mark. */
static voidfinal_check(unsigned int flags)
{
}
/* Prints out the matchinfo. */
static void
print(const struct ipt_ip *ip,
const struct ipt_entry_match *match,
int numeric)
{
printf("CONFIRMED match");
}/* Saves the union ipt_matchinfo in parsable form to stdout. */ static voidsave(const struct ipt_ip *ip, const struct ipt_entry_match *match) {
}
static
struct iptables_match confirmed
= { NULL,
"confirmed",
IPTABLES_VERSION,
IPT_ALIGN(0),
IPT_ALIGN(0),
&help,
&init,
&parse,
&final_check,
&print,
&save,
opts
};void _init(void)
{
register_match(&confirmed);
}#! /bin/sh
[ -f $KERNEL_DIR/net/ipv4/netfilter/ipt_CONFIRMED.c ] && echo CONFIRMED
#! /bin/sh
[ -f $KERNEL_DIR/net/ipv4/netfilter/ipt_confirmed.c ] && echo confirmed