Skip to contentSkip to conversation

iptables -L --rules to print command line version of rules

PATCH In review Henrik Nordstrom 2 participants 5 messages May 11, 2008 – May 12, 2008 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iptables -L --rules to print command line version of rules

This patch makes iptables -L --rules capable of printing "save" format rules, same as iptables-save.

The patch adds the --rules option to ip(6)tables, and moves the functions for printing "save" format rules from ip(6)tables-save.c to ip(6)tables.c where it may be used by all commands and not only ip(6)tables-save.

Not sure if this should be an option to -L/--list, or a new command of it's own. Before looking at the code I thought an option to --list would be better, but after looking at the code I am no longer sure, perhaps it's better as a --list-rules command instead of an option to --list.

I am
Regards
Henrik

iptables.8.in +4 -0
Index: iptables.8.in
===================================================================
@@ -381,6 +381,10 @@
 When listing rules, add line numbers to the beginning of each rule,
 corresponding to that rule's position in the chain.
 .TP
+.B "--rules"
+When listing rules, print the actual iptables rule as it would be
+entered on the command line.
+.TP
 .B "--modprobe=command"
 When adding or inserting rules into a chain, use
 .B command
iptables-save.c +0 -205

211 lines not on this page · raw message

ip6tables.h +1 -0
Index: include/ip6tables.h
===================================================================
@@ -22,5 +22,6 @@
 extern int for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *), int verbose, int builtinstoo, ip6tc_handle_t *handle);
 extern int flush_entries(const ip6t_chainlabel chain, int verbose, ip6tc_handle_t *handle);
 extern int delete_chain(const ip6t_chainlabel chain, int verbose, ip6tc_handle_t *handle);
+void print_rule(const struct ip6t_entry *e, ip6tc_handle_t *h, const char *chain, int counters);
 
 #endif /*_IP6TABLES_USER_H*/
iptables.h +2 -0
Index: include/iptables.h
===================================================================
@@ -23,6 +23,8 @@
 			iptc_handle_t *handle);
 extern int for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
 		int verbose, int builtinstoo, iptc_handle_t *handle);
+extern void print_rule(const struct ipt_entry *e,
+		iptc_handle_t *handle, const char *chain, int counters);
 
 /* kernel revision handling */
 extern int kernel_version;
iptables.c +252 -17

326 lines not on this page · raw message

ip6tables.8.in +4 -0
Index: ip6tables.8.in
===================================================================
@@ -373,6 +373,10 @@
 When listing rules, add line numbers to the beginning of each rule,
 corresponding to that rule's position in the chain.
 .TP
+.B "--rules"
+When listing rules, print the actual iptables rule as it would be
+entered on the command line.
+.TP
 .B "--modprobe=command"
 When adding or inserting rules into a chain, use
 .B command
ip6tables-save.c +0 -193

199 lines not on this page · raw message

ip6tables.c +242 -17

316 lines not on this page · raw message

#1 lore raw
Quoted 8 lines · Henrik Nordstrom

This patch makes iptables -L --rules capable of printing "save" format rules, same as iptables-save.

The patch adds the --rules option to ip(6)tables, and moves the functions for printing "save" format rules from ip(6)tables-save.c to ip(6)tables.c where it may be used by all commands and not only ip(6)tables-save.

Quoted 4 lines

Not sure if this should be an option to -L/--list, or a new command of it's own. Before looking at the code I thought an option to --list would be better, but after looking at the code I am no longer sure, perhaps it's better as a --list-rules command instead of an option to --list.

CC ip6tables.o
../ip6tables.c: In function ‘do_command6’:
../ip6tables.c:1730: error: ‘struct ip6t_entry’ has no member named ‘ip’

Other than that, I do not see a real difference the patch makes over iptables-save.

10:35 yaguchi:../xtables-dev/obj # iptables -Z; ./iptables -nvL --rules; [0:0] -A INPUT -i lo -j ACCEPT
[0:0] -A INPUT -i rtl0 -j ACCEPT
[0:0] -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT [0:0] -A INPUT -i vmnet+ -j ACCEPT
[0:0] -A INPUT -s 84.213.68.203/32 -i tun+ -p tcp -m tcp --dport 22 -j ACCEPT [0:0] -A FORWARD -i rtl0 -j ACCEPT
[0:0] -A FORWARD -i sis0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT [0:0] -A FORWARD -i sis0 -p tcp -j REJECT --reject-with tcp-reset [0:0] -A FORWARD -i sis0 -j REJECT --reject-with icmp-port-unreachable

10:36 yaguchi:../xtables-dev/obj # iptables -Z; iptables-save -ct filter # Generated by iptables-save v1.4.0.77 on Mon May 12 10:36:14 2008 *filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
[0:0] -A INPUT -i lo -j ACCEPT
[0:0] -A INPUT -i rtl0 -j ACCEPT
[0:0] -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT [0:0] -A INPUT -i vmnet+ -j ACCEPT
[0:0] -A INPUT -s 84.213.68.203/32 -i tun+ -p tcp -m tcp --dport 22 -j ACCEPT [0:0] -A FORWARD -i rtl0 -j ACCEPT
[0:0] -A FORWARD -i sis0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT [0:0] -A FORWARD -i sis0 -p tcp -j REJECT --reject-with tcp-reset [0:0] -A FORWARD -i sis0 -j REJECT --reject-with icmp-port-unreachable COMMIT
# Completed on Mon May 12 10:36:14 2008

I would even argue it is inferior to iptables-save (which shows policy and counters for the chains).
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 3 lines · Jan Engelhardt

CC ip6tables.o
../ip6tables.c: In function ‘do_command6’:
../ip6tables.c:1730: error: ‘struct ip6t_entry’ has no member named ‘ip’

Sorry about that. Should be ipv6.

Quoted 2 lines

Other than that, I do not see a real difference the patch makes over iptables-save.

The difference is that iptables can dump a single chain, while iptables-save only a whole table.

The other difference is that this gives "save" like capability to iptables-restore when used in a pipe, makging iptables-restore a more complete batch interface for talking to iptables which was the main motivation behind this.

But you have a point in that chains and policies should be included in the output, just as they are in the human readable output.

Additionally the count output format should probably be using -c when invoked by the iptables command..

Regards
Henrik

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 4 lines · Henrik Nordstrom

The other difference is that this gives "save" like capability to iptables-restore when used in a pipe, makging iptables-restore a more complete batch interface for talking to iptables which was the main motivation behind this.

Example please :)

#4 lore raw
Quoted 8 lines · Jan Engelhardt
Quoted 4 lines · Henrik Nordstrom

The other difference is that this gives "save" like capability to iptables-restore when used in a pipe, makging iptables-restore a more complete batch interface for talking to iptables which was the main motivation behind this.

Example please :)

as you know iptables-restore is basically just a batch wrapper around iptables, so everything iptables can do you can also do with iptables-restore.

% iptables-restore -n
*filter
--list INPUT --rules
[caller reads the output to figure out what to modify] --insert OUTPUT 2 -d 1.2.3.4 -J REJECT --insert INPUT 4 -s 1.2.3.4 -J DROP
[caller wants to verify the result before commit] --list INPUT --rules
COMMIT

all in one single fetch/modify/upload operation.

Or with the new version I am just about to submit, implementing this as a new --list-rules (-S) command: (just need to split out some other cleanup first..)

% iptables-restore -n
*filter
--list-rules INPUT
--add INPUT -s 1.2.3.4 -J DROP
--list-rules INPUT
COMMIT

A new patch and a couple minor related ones coming shortly.

Regards
Henrik

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html