Skip to contentSkip to conversation

iptables-save: add --chain argument, limits output to a chain

PATCH In review [email protected] 2 participants 7 messages Feb 26, 2013 1 message not in the archive netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iptables-save: add --chain argument, limits output to a chain

From: Jonh Wendell <[email protected]>

Similar to the --table argument, if a --chain (or -C) argument is passed, we limit the output to rules of that chain.

---
 iptables/iptables-save.8 |    8 ++++++--
 iptables/iptables-save.c |   13 ++++++++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)
iptables/iptables-save.8 +6 -2
index c2e0a94..2f510d0 100644
@@ -1,4 +1,4 @@
-.TH IPTABLES-SAVE 8 "Jan 04, 2001" "" ""
+.TH IPTABLES-SAVE 8 "Feb 25, 2013" "" ""
 .\"
 .\" Man page written by Harald Welte <[email protected]>
 .\" It is based on the iptables man page.
@@ -22,7 +22,7 @@
 iptables-save \(em dump iptables rules to stdout
 .SH SYNOPSIS
 \fBiptables\-save\fP [\fB\-M\fP \fImodprobe\fP] [\fB\-c\fP]
-[\fB\-t\fP \fItable\fP]
+[\fB\-t\fP \fItable\fP] [\fB\-C\fP \fIchain\fP]
 .SH DESCRIPTION
 .PP
 .B iptables-save
@@ -39,6 +39,10 @@ include the current values of all packet and byte counters in the output
 \fB\-t\fR, \fB\-\-table\fR \fItablename\fP
 restrict output to only one table. If not specified, output includes all
 available tables.
+.TP
+\fB\-C\fR, \fB\-\-chain\fR \fIchainname\fP
+restrict output to only one chain. If not specified, output includes all
+available chains.
 .SH BUGS
 None known as of iptables-1.2.1 release
 .SH AUTHOR
iptables/iptables-save.c +12 -1
index e599fce..aae77b6 100644
@@ -22,12 +22,14 @@
 #endif
 
 static int show_counters = 0;
+static char *chainname = NULL;
 
 static const struct option options[] = {
 	{.name = "counters", .has_arg = false, .val = 'c'},
 	{.name = "dump",     .has_arg = false, .val = 'd'},
 	{.name = "table",    .has_arg = true,  .val = 't'},
 	{.name = "modprobe", .has_arg = true,  .val = 'M'},
+	{.name = "chain",    .has_arg = true,  .val = 'C'},
 	{NULL},
 };
 
@@ -85,6 +87,9 @@ static int do_output(const char *tablename)
 	     chain;
 	     chain = iptc_next_chain(h)) {
 
+		if (chainname && *chainname && strcmp(chain, chainname))
+			continue;
+
 		printf(":%s ", chain);
 		if (iptc_builtin(chain, h)) {
 			struct xt_counters count;
@@ -101,6 +106,9 @@ static int do_output(const char *tablename)
 	     chain = iptc_next_chain(h)) {
 		const struct ipt_entry *e;
 
+		if (chainname && *chainname && strcmp(chain, chainname))
+			continue;
+
 		/* Dump out rules */
 		e = iptc_first_rule(chain, h);
 		while(e) {
@@ -140,7 +148,7 @@ iptables_save_main(int argc, char *argv[])
 	init_extensions4();
 #endif
 
-	while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
+	while ((c = getopt_long(argc, argv, "bcdt:C:", options, NULL)) != -1) {
 		switch (c) {
 		case 'c':
 			show_counters = 1;
@@ -153,6 +161,9 @@ iptables_save_main(int argc, char *argv[])
 		case 'M':
 			xtables_modprobe_program = optarg;
 			break;
+		case 'C':
+			chainname = optarg;
+			break;
 		case 'd':
 			do_output(tablename);
 			exit(0);
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 4 lines · On Tuesday 2013-02-26 12:33, [email protected] wrote:

From: Jonh Wendell <[email protected]>

Similar to the --table argument, if a --chain (or -C) argument is passed, we limit the output to rules of that chain.

But we have iptables -S chain for that, don't we.

#2 lore raw

2013/2/26 Jan Engelhardt <[email protected]>

Quoted 9 lines · On Tuesday 2013-02-26 12:33, [email protected] wrote:
Quoted 5 lines

From: Jonh Wendell <[email protected]>

Similar to the --table argument, if a --chain (or -C) argument is passed, we limit the output to rules of that chain.

But we have iptables -S chain for that, don't we.

I'm afraid its output is not suitable for iptables-restore.

--
Jonh Wendell
http://www.bani.com.br

A message that is not in the archive (sent off-list or not yet received): CABXqP=jq3rEE6b1xpgA0QtuwAQ04U5b3voMdTsuGmLB-qrsDxg@mail.gmail.com

#4 lore raw
Quoted 8 lines · Jonh Wendell

2013/2/26 Jan Engelhardt <[email protected]>
On Tuesday 2013-02-26 12:33, [email protected] wrote:

>From: Jonh Wendell <[email protected]>
>
>Similar to the --table argument, if a --chain (or -C) argument >is passed, we limit the output to rules of that chain.

Quoted 4 lines

But we have iptables -S chain for that, don't we.

I'm afraid its output is not suitable for iptables-restore.

I thought you just wanted to have a single chain shown, for the purposes of debugging (because nobody can frankly read -L's output). If however you want to feed it to replace, can you elaborate on your use case? I would be interested in that.

#5 lore raw

2013/2/26 Jan Engelhardt <[email protected]>:

Quoted 13 lines · Jonh Wendell
Quoted 11 lines

2013/2/26 Jan Engelhardt <[email protected]>
On Tuesday 2013-02-26 12:33, [email protected] wrote:

>From: Jonh Wendell <[email protected]>
>
>Similar to the --table argument, if a --chain (or -C) argument >is passed, we limit the output to rules of that chain.

But we have iptables -S chain for that, don't we.

Quoted 6 lines
Quoted 1 line

I'm afraid its output is not suitable for iptables-restore.

I thought you just wanted to have a single chain shown, for the purposes of debugging (because nobody can frankly read -L's output). If however you want to feed it to replace, can you elaborate on your use case? I would be interested in that.

Hi!

My particular use case is: I want to flush all iptables rules except those ones from a specific chain.
So, I save them with iptables-save -C <chain-name>, flush, and then run iptables-restore on them.

I could do it without that -C flag, but I'd have to parse its output to get only the chain I'm interested in.

In other words, my use case could be reached with something like 'iptables -F ! <chain-name>'.

All in all, I think it's a good addition to iptables-save, it can be useful in other scenarios.

Thanks,

Signature · 2 lines
Jonh Wendell
http://www.bani.com.br
#6 lore raw
Quoted 5 lines · Jonh Wendell

My particular use case is: I want to flush all iptables rules except those ones from a specific chain.
So, I save them with iptables-save -C <chain-name>, flush, and then run iptables-restore on them.

But if chain-name is not a base chain, then you wipe out the main rules for all practical purposes.

#7 lore raw

2013/2/26 Jan Engelhardt <[email protected]>:

Quoted 10 lines · Jonh Wendell
Quoted 5 lines

My particular use case is: I want to flush all iptables rules except those ones from a specific chain.
So, I save them with iptables-save -C <chain-name>, flush, and then run iptables-restore on them.

But if chain-name is not a base chain, then you wipe out the main rules for all practical purposes.

actually, after flush all rules, I load a preset set of rules and only then I run iptables-restore.

Signature · 2 lines
Jonh Wendell
http://www.bani.com.br