iptables: add -C to check for existing rules
PATCH 1/1 iptables: add -C to check for existing rules
It is often useful to check whether a specific rule is already present in a chain without actually modifying the iptables config.
Services like fail2ban usually employ techniques like grepping through the output of "iptables -L" which is quite error prone.
This patch adds a new operation -C to the iptables command which mostly works like -D; it can detect and indicate the existence of the specified rule by modifying the exit code. The new operation TC_CHECK_ENTRY uses the same code as the -D operation, whose functions got a dry-run parameter appended.
--- include/libiptc/libiptc.h | 6 ++++ iptables.c | 66 ++++++++++++++++++++++++++++++++++++++++---- libiptc/libip4tc.c | 1 + libiptc/libiptc.c | 34 ++++++++++++++++++++--- 4 files changed, 97 insertions(+), 10 deletions(-)
include/libiptc/libiptc.h +6 -0
@@ -88,6 +88,12 @@ int iptc_append_entry(const ipt_chainlabel chain,const struct ipt_entry *e, struct iptc_handle *handle); +/* Check whether a rule mathching `e' exists */ +int iptc_check_entry (const ipt_chainlabel chain, +const struct ipt_entry *origfw, +unsigned char *matchmask, +struct iptc_handle *handle); +/* Delete the first rule in `chain' which matches `e', subject to matchmask (array of length == origfw) */ int iptc_delete_entry(const ipt_chainlabel chain,
iptables.c +60 -6
133 lines not on this page · raw message
libiptc/libip4tc.c +1 -0
@@ -76,6 +76,7 @@ typedef unsigned int socklen_t;#define TC_INSERT_ENTRY iptc_insert_entry#define TC_REPLACE_ENTRY iptc_replace_entry#define TC_APPEND_ENTRY iptc_append_entry +#define TC_CHECK_ENTRY iptc_check_entry#define TC_DELETE_ENTRY iptc_delete_entry#define TC_DELETE_NUM_ENTRY iptc_delete_num_entry#define TC_FLUSH_ENTRIES iptc_flush_entries
libiptc/libiptc.c +30 -4
@@ -1956,12 +1956,14 @@ is_same(const STRUCT_ENTRY *a,const STRUCT_ENTRY *b, unsigned char *matchmask); -/* Delete the first rule in `chain' which matches `fw'. */ -int -TC_DELETE_ENTRY (const IPT_CHAINLABEL chain, + +/* find the first rule in `chain' which matches `fw' and remove it unless dry_run is set */ +static int +delete_entry (const IPT_CHAINLABEL chain,const STRUCT_ENTRY *origfw,unsigned char *matchmask, -struct xtc_handle *handle) +struct xtc_handle *handle, +int dry_run) {struct chain_head *c;struct rule_head *r, *i;
@@ -2004,6 +2006,10 @@ TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,if (!target_same (r, i, mask))continue ; + +/* if we are just doing a dry run, we simply skip the rest */ +if (dry_run) +return 1 ;/* If we are about to delete the rule that is the * current iterator, move rule iterator back. next
@@ -2027,6 +2033,26 @@ TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,return 0 ; } +/* check whether a specified rule is present */ +int +TC_CHECK_ENTRY (const IPT_CHAINLABEL chain, +const STRUCT_ENTRY *origfw, +unsigned char *matchmask, +struct xtc_handle *handle) +{ +/* do a dry-run delete to find out whether a matching rule exists */ +return delete_entry (chain, origfw, matchmask, handle,1 ); +} + +/* Delete the first rule in `chain' which matches `fw'. */ +int +TC_DELETE_ENTRY (const IPT_CHAINLABEL chain, +const STRUCT_ENTRY *origfw, +unsigned char *matchmask, +struct xtc_handle *handle) +{ +return delete_entry (chain, origfw, matchmask, handle,0 ); +}/* Delete the rule in position `rulenum' in `chain'. */ int
Signature · 1 line
1.7.2.3
Am 08.03.2011 12:56, schrieb Stefan Tomanek:
Quoted 11 lines
It is often useful to check whether a specific rule is already present in a chain without actually modifying the iptables config.
Services like fail2ban usually employ techniques like grepping through the output of "iptables -L" which is quite error prone.
This patch adds a new operation -C to the iptables command which mostly works like -D; it can detect and indicate the existence of the specified rule by modifying the exit code. The new operation
TC_CHECK_ENTRYuses the same code as the -D operation, whose functions got a dry-run parameter appended.
Looks fine to me. I'll apply this if nobody else objects.
iptables.c static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] = · line 171
Quoted 7 lines · Stefan Tomanek
iptables.c +2
-1@@ -165,7 +167,8 @@ static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] = /*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}, /*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' ','x'}, /*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}, -/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x','x'} +/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x','x'}, +/*CHECK*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x',' '}
This would make it impossible to check for a rule that used the fragment test.
Does --lin even make sense with -C?
Quoted 9 lines
(quoted diff) +0
-0};static const int inverse_for_options[NUMBER_OF_OPT] =
iptables.c +1
-1@@ -221,7 +224,7 @@ static void exit_printhelp(const struct xtables_rule_match *matches) {printf ("%s v%s\n\n" -"Usage: %s -[AD] chain rule-specification [options]\n" +"Usage: %s -[ACD] chain rule-specification [options]\n"
Quoted 3 lines
" %s -I chain [rulenum] rule-specification [options]\n" " %s -R chain rulenum rule-specification [options]\n" " %s -D chain rulenum [options]\n"
The manpage is not updated.
Where is ip6tables support?
Dies schrieb Jan Engelhardt ([email protected]):
Quoted 11 lines
iptables.c static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] = · line 171
Quoted 6 lines
/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}, /*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' ','x'},
/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},iptables.c +2
-1-/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x','x'} +/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x','x'}, +/*CHECK*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x',' '}This would make it impossible to check for a rule that used the fragment test.
Does --lin even make sense with -C?
I'm not sure, I simply copied the line for DELETE to mimic the interface. In fact, -C does exactly the same thing as -D - except the actual removal of the rule.
Quoted 1 line
Where is ip6tables support?
It will probably follow soon, just like the missing paragraph of the man page :-)
PATCH 1/1 iptables: add -C to check for existing rules
In reply to Jan EngelhardtIt is often useful to check whether a specific rule is already present in a chain without actually modifying the iptables config.
Services like fail2ban usually employ techniques like grepping through the output of "iptables -L" which is quite error prone.
This patch adds a new operation -C to the iptables command which mostly works like -D; it can detect and indicate the existence of the specified rule by modifying the exit code. The new operation TC_CHECK_ENTRY uses the same code as the -D operation, whose functions got a dry-run parameter appended.
--- include/libiptc/libip6tc.h | 6 ++++ include/libiptc/libiptc.h | 6 ++++ ip6tables.8.in | 8 ++++- ip6tables.c | 66 ++++++++++++++++++++++++++++++++++++++++---- iptables.8.in | 8 ++++- iptables.c | 66 ++++++++++++++++++++++++++++++++++++++++---- libiptc/libip4tc.c | 1 + libiptc/libip6tc.c | 1 + libiptc/libiptc.c | 34 ++++++++++++++++++++-- 9 files changed, 178 insertions(+), 18 deletions(-)
include/libiptc/libip6tc.h +6 -0
@@ -80,6 +80,12 @@ int ip6tc_append_entry(const ip6t_chainlabel chain,const struct ip6t_entry *e, struct ip6tc_handle *handle); +/* Check whether a matching rule exists */ +int ip6tc_check_entry (const ip6t_chainlabel chain, +const struct ip6t_entry *origfw, +unsigned char *matchmask, +struct ip6tc_handle *handle); +/* Delete the first rule in `chain' which matches `fw'. */ int ip6tc_delete_entry(const ip6t_chainlabel chain, const struct ip6t_entry *origfw,
include/libiptc/libiptc.h +6 -0
@@ -88,6 +88,12 @@ int iptc_append_entry(const ipt_chainlabel chain,const struct ipt_entry *e, struct iptc_handle *handle); +/* Check whether a rule mathching `e' exists */ +int iptc_check_entry (const ipt_chainlabel chain, +const struct ipt_entry *origfw, +unsigned char *matchmask, +struct iptc_handle *handle); +/* Delete the first rule in `chain' which matches `e', subject to matchmask (array of length == origfw) */ int iptc_delete_entry(const ipt_chainlabel chain,
ip6tables.8.in +7 -1
@@ -27,7 +27,7 @@ .SH NAME ip6tables \(em IPv6 packet filter administration .SH SYNOPSIS -\fBip6tables\fP [\fB\-t\fP \fItable\fP] {\fB\-A\fP|\fB\-D\fP} \fIchain +\fBip6tables\fP [\fB\-t\fP \fItable\fP] {\fB\-A\fP|\fB\-D\fP|\fB\-C\fP} \fIchain rule-specification\fP [\fIoptions...\fP] .PP \fBip6tables\fP [\fB\-t\fP \fItable\fP] \fB\-I\fP \fIchain\fP [\fIrulenum\fP]
@@ -147,6 +147,12 @@ Delete one or more rules from the selected chain. There are two versions of this command: the rule can be specified as a number in the chain (starting at 1 for the first rule) or a rule to match. .TP +\fB\-C\fP, \fB\-\-check\fP \fIchain rule-specification\fP +Check whether a rule matching the specification does exist in the +selected chain. This command uses the same logic as \fB\-D\fP to +find a matching entry, but does not alter the existing iptables +configuration and uses its exit code to indicate success or failure. +.TP \fB\-I\fP, \fB\-\-insert\fP \fIchain\fP [\fIrulenum\fP] \fIrule-specification\fP Insert one or more rules in the selected chain as the given rule number. So, if the rule number is 1, the rule or rules are inserted
ip6tables.c +60 -6
133 lines not on this page · raw message
iptables.8.in +7 -1
@@ -25,7 +25,7 @@ .SH NAME iptables \(em administration tool for IPv4 packet filtering and NAT .SH SYNOPSIS -\fBiptables\fP [\fB\-t\fP \fItable\fP] {\fB\-A\fP|\fB\-D\fP} \fIchain\fP \fIrule-specification\fP +\fBiptables\fP [\fB\-t\fP \fItable\fP] {\fB\-A\fP|\fB\-D\fP|\fB\-C\fP} \fIchain\fP \fIrule-specification\fP .PP \fBiptables\fP [\fB\-t\fP \fItable\fP] \fB\-I\fP \fIchain\fP [\fIrulenum\fP] \fIrule-specification\fP .PP
@@ -152,6 +152,12 @@ Delete one or more rules from the selected chain. There are two versions of this command: the rule can be specified as a number in the chain (starting at 1 for the first rule) or a rule to match. .TP +\fB\-C\fP, \fB\-\-check\fP \fIchain rule-specification\fP +Check whether a rule matching the specification does exist in the +selected chain. This command uses the same logic as \fB\-D\fP to +find a matching entry, but does not alter the existing iptables +configuration and uses its exit code to indicate success or failure. +.TP \fB\-I\fP, \fB\-\-insert\fP \fIchain\fP [\fIrulenum\fP] \fIrule-specification\fP Insert one or more rules in the selected chain as the given rule number. So, if the rule number is 1, the rule or rules are inserted
iptables.c +60 -6
133 lines not on this page · raw message
libiptc/libip4tc.c +1 -0
@@ -76,6 +76,7 @@ typedef unsigned int socklen_t;#define TC_INSERT_ENTRY iptc_insert_entry#define TC_REPLACE_ENTRY iptc_replace_entry#define TC_APPEND_ENTRY iptc_append_entry +#define TC_CHECK_ENTRY iptc_check_entry#define TC_DELETE_ENTRY iptc_delete_entry#define TC_DELETE_NUM_ENTRY iptc_delete_num_entry#define TC_FLUSH_ENTRIES iptc_flush_entries
libiptc/libip6tc.c +1 -0
@@ -71,6 +71,7 @@ typedef unsigned int socklen_t;#define TC_INSERT_ENTRY ip6tc_insert_entry#define TC_REPLACE_ENTRY ip6tc_replace_entry#define TC_APPEND_ENTRY ip6tc_append_entry +#define TC_CHECK_ENTRY ip6tc_check_entry#define TC_DELETE_ENTRY ip6tc_delete_entry#define TC_DELETE_NUM_ENTRY ip6tc_delete_num_entry#define TC_FLUSH_ENTRIES ip6tc_flush_entries
libiptc/libiptc.c +30 -4
@@ -1956,12 +1956,14 @@ is_same(const STRUCT_ENTRY *a,const STRUCT_ENTRY *b, unsigned char *matchmask); -/* Delete the first rule in `chain' which matches `fw'. */ -int -TC_DELETE_ENTRY (const IPT_CHAINLABEL chain, + +/* find the first rule in `chain' which matches `fw' and remove it unless dry_run is set */ +static int +delete_entry (const IPT_CHAINLABEL chain,const STRUCT_ENTRY *origfw,unsigned char *matchmask, -struct xtc_handle *handle) +struct xtc_handle *handle, +int dry_run) {struct chain_head *c;struct rule_head *r, *i;
@@ -2005,6 +2007,10 @@ TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,if (!target_same (r, i, mask))continue ; +/* if we are just doing a dry run, we simply skip the rest */ +if (dry_run) +return 1 ; +/* If we are about to delete the rule that is the * current iterator, move rule iterator back. next * pointer will then point to real next node */
@@ -2027,6 +2033,26 @@ TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,return 0 ; } +/* check whether a specified rule is present */ +int +TC_CHECK_ENTRY (const IPT_CHAINLABEL chain, +const STRUCT_ENTRY *origfw, +unsigned char *matchmask, +struct xtc_handle *handle) +{ +/* do a dry-run delete to find out whether a matching rule exists */ +return delete_entry (chain, origfw, matchmask, handle,1 ); +} + +/* Delete the first rule in `chain' which matches `fw'. */ +int +TC_DELETE_ENTRY (const IPT_CHAINLABEL chain, +const STRUCT_ENTRY *origfw, +unsigned char *matchmask, +struct xtc_handle *handle) +{ +return delete_entry (chain, origfw, matchmask, handle,0 ); +}/* Delete the rule in position `rulenum' in `chain'. */ int
Signature · 1 line
1.7.2.3
Quoted 11 lines · Stefan Tomanek
It is often useful to check whether a specific rule is already present in a chain without actually modifying the iptables config.
Services like fail2ban usually employ techniques like grepping through the output of "iptables -L" which is quite error prone.
This patch adds a new operation -C to the iptables command which mostly works like -D; it can detect and indicate the existence of the specified rule by modifying the exit code. The new operation
TC_CHECK_ENTRYuses the same code as the -D operation, whose functions got a dry-run parameter appended.
I picked this up, sorted -C in and ironed out a few style issues. Thanks.