Patch for graphing a rule-set
Hi all,
I've attached a patch against iptables v1.3.5 that implements graphing for the iptables command. This patch adds the "--graph" ( or -r) qualifier to the normal list (-L) command. When specified, the output is in dot format, as used by graphviz:
http://www.graphviz.org/
I wanted a nice way to document some firewall rules and this (rather simple) patch allowed that. With some PHP, I'd imagine one could graph-on-demand the current firewall state of a machine, if that's of any use.
To get a sample output do:
sudo ./iptables -L -r | fdp -Tsvg -oiptables.svg; inkview iptables.svg
or
sudo ./iptables -L -r | fdp -Tps -oiptables.ps; gv iptables.ps
or ...
I've put an example (in svg and ps format) here:
http://ppewww.ph.gla.ac.uk/~paul/iptables-graph.svg
http://ppewww.ph.gla.ac.uk/~paul/iptables-graph.ps
The code's rather cosmetic, fairly hacky and somewhat rough-and-ready: do with it what you will! I get a message
iptables: Incompatible with this kernel
which might be a result of the patch. I've not spent much time tracking this down further.
Cheers,
Paul.
iptables.c renamed from iptables.c.org +235 -21
@@ -98,11 +98,20 @@#define OPT_VIANAMEIN 0x00080U#define OPT_VIANAMEOUT 0x00100U#define OPT_FRAGMENT 0x00200U -#define OPT_LINENUMBERS 0x00400U -#define OPT_COUNTERS 0x00800U -#define NUMBER_OF_OPT 12 +#define OPT_GRAPH 0x00400U +#define OPT_LINENUMBERS 0x00800U +#define OPT_COUNTERS 0x01000U +#define NUMBER_OF_OPT 13static const char optflags[NUMBER_OF_OPT ] -= {'n' ,'s' ,'d' ,'p' ,'j' ,'v' ,'x' ,'i' ,'o' ,'f' ,'0' ,'c' }; += {'n' ,'s' ,'d' ,'p' ,'j' ,'v' ,'x' ,'i' ,'o' ,'f' ,'r' ,'0' ,'c' }; + +static const char *builtin_chains[] = { +"INPUT" , +"FORWARD" , +"OUTPUT" , +"PREROUTING" , +"POSTROUTING" +};static struct option original_opts[] = { {"append" ,1 ,0 ,'A' },
@@ -136,6 +145,7 @@ { "modprobe", 1, 0, 'M' }, { "set-counters", 1, 0, 'c' }, { "goto", 1, 0, 'g' }, + { "graph", 0, 0, 'r' }, { 0 } };
@@ -160,20 +170,20 @@static char commands_v_options[NUMBER_OF_CMD ][NUMBER_OF_OPT ] =/* Well, it's better than "Re: Linux vs FreeBSD" */ { -/* -n -s -d -p -j -v -x -i -o -f --line */ -/*INSERT*/ {'x' ,' ' ,' ' ,' ' ,' ' ,' ' ,'x' ,' ' ,' ' ,' ' ,'x' }, -/*DELETE*/ {'x' ,' ' ,' ' ,' ' ,' ' ,' ' ,'x' ,' ' ,' ' ,' ' ,'x' }, -/*DELETE_NUM*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*REPLACE*/ {'x' ,' ' ,' ' ,' ' ,' ' ,' ' ,'x' ,' ' ,' ' ,' ' ,'x' }, -/*APPEND*/ {'x' ,' ' ,' ' ,' ' ,' ' ,' ' ,'x' ,' ' ,' ' ,' ' ,'x' }, -/*LIST*/ {' ' ,'x' ,'x' ,'x' ,'x' ,' ' ,' ' ,'x' ,'x' ,'x' ,' ' }, -/*FLUSH*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*ZERO*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*NEW_CHAIN*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*DEL_CHAIN*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*SET_POLICY*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' }, -/*CHECK*/ {'x' ,'+' ,'+' ,'+' ,'x' ,' ' ,'x' ,' ' ,' ' ,' ' ,'x' }, -/*RENAME*/ {'x' ,'x' ,'x' ,'x' ,'x' ,' ' ,'x' ,'x' ,'x' ,'x' ,'x' } +/* -n -s -d -p -j -v -x -i -o -f -r --line */ +/*INSERT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IPT_INV_FRAG , +/* -r */ 0 , /*--line*/ 0 };
@@ -410,6 +421,7 @@" --match -m match\n" " extended match (may load extension)\n" " --numeric -n numeric output of addresses and ports\n" +" --graph -r produce graph instead of text output\n" " --out-interface -o [!] output name[+]\n" " network interface name ([+] for wildcard)\n" " --table -t table table to manipulate (default: `filter')\n"
@@ -1420,7 +1432,7 @@printf ("[goto] " );#endif -IPT_MATCH_ITERATE (fw, print_match, &fw->ip, format &FMT_NUMERIC ); +IPT_MATCH_ITERATE (fw, print_match, &fw->ip,FMT_NUMERIC );if (target) {if (target->print)
@@ -1649,14 +1661,209 @@return iptc_delete_chain (chain, handle); } + +int +is_builtin (const char *name) +{ +int i; + +for ( i =0 ; i <sizeof ( builtin_chains) /sizeof (char *); i++) +if ( !strcmp ( builtin_chains[i], name)) +return 1 ; + +return 0 ; +} + + +void +print_label (const struct ipt_entry *entry,iptc_handle_t *handle) +{ +const char *targname, *t; +char buf[40 ]; +struct iptables_target *target =NULL ; +u_int8_t flags; +int have_printed=0 ; + + targname =iptc_get_target (entry, handle); + +if (!iptc_is_chain (targname, *handle)) + target =find_target (targname,TRY_LOAD ); +else + target =find_target (IPT_STANDARD_TARGET ,LOAD_MUST_SUCCEED ); + + t =ipt_get_target ((struct ipt_entry *)entry); + flags = entry->ip.flags; + +char *pname =proto_to_name ( entry->ip.proto,0 ); +if (pname) { +if (strcmp ( pname,"all" )) { + have_printed=1 ; +printf ("%s " , pname); + } + } +else { +printf ("%hu " , entry->ip.proto); + have_printed=1 ; + } + + +char iface[IFNAMSIZ +2 ]; + +if (entry->ip.invflags &IPT_INV_VIA_IN ) { + iface[0 ] ='!' ; + iface[1 ] ='\0' ; + } +else iface[0 ] ='\0' ; + +if (entry->ip.iniface[0 ] !='\0' ) { +strcat (iface, entry->ip.iniface); +printf ("in=%s " , iface); + have_printed=1 ; + } + +if (entry->ip.invflags &IPT_INV_VIA_OUT ) { + iface[0 ] ='!' ; + iface[1 ] ='\0' ; + } +else iface[0 ] ='\0' ; + +if (entry->ip.outiface[0 ] !='\0' ) { +strcat (iface, entry->ip.outiface); +printf ("out=%s " , iface); + have_printed=1 ; + } + + +fputc (entry->ip.invflags &IPT_INV_SRCIP ?'!' :' ' , stdout); + +if ( entry->ip.smsk.s_addr !=0L ) { +sprintf (buf,"%s" ,addr_to_dotted (&(entry->ip.src))); +strcat (buf,mask_to_dotted (&(entry->ip.smsk))); +printf ("src=%s" , buf); + have_printed=1 ; + } + + +fputc (entry->ip.invflags &IPT_INV_DSTIP ?'!' :' ' , stdout); + +if ( entry->ip.dmsk.s_addr !=0L ) { +sprintf ( buf,"%s" ,addr_to_dotted (&(entry->ip.dst))); +strcat ( buf,mask_to_dotted (&(entry->ip.dmsk))); +printf ("dst=%s " , buf); + have_printed=1 ; + } + + +IPT_MATCH_ITERATE (entry, print_match, &entry->ip,FMT_NUMERIC ); + + +if ( !iptc_is_chain (targname, *handle)) { +printf ("[%s]" , targname); + have_printed =1 ; + } + +if ( !have_printed) +printf ("all" ); +} + +static int -list_entries (const ipt_chainlabel chain,int verbose,int numeric, +graph_entries (const ipt_chainlabel chain,iptc_handle_t *handle) +{ +const char *this; +const char *pol, *target; +struct ipt_counters counters; +const struct ipt_entry *i; +int n; + +/* Print header info */ +printf ("digraph iptables {\n" +" graph [\n" +" rankdir = \"LR\"\n" +" ratio = 1.414\n" +" splines = \"true\"\n" +" ];\n" +" node [\n" +" shape = \"record\"\n" +" fillcolor = \"#e0d0e0\"\n" +" style=filled\n" +" ]\n" +" edge [\n" +" weight = 0.1\n" +" ]\n" + ); + +/* Print all chains */ +for (this =iptc_first_chain (handle); + this; + this =iptc_next_chain (handle)) { + + pol =iptc_get_policy (this, &counters, handle); + +printf ("\n \"%s\" [\n" , this); + +if (is_builtin ( this)) +printf (" fillcolor = \"#d0e0d0\"\n" ); + + +printf (" label = \"{<1>\\N" ); +if ( pol) +printf (" [%s]" , pol); + + n=2 ; +for ( i =iptc_first_rule (this, handle); + i; + i =iptc_next_rule (i, handle)) { +printf (" | <%u> " , n++); +print_label ( i, handle); + } + +printf ("}\"\n" );/* End of label */ + +printf (" ]\n" ); + } + + +/* Print all the links */ +for (this =iptc_first_chain (handle); + this; + this =iptc_next_chain (handle)) { + + n=2 ; +for ( i =iptc_first_rule (this, handle); + i; + i =iptc_next_rule (i, handle)) { + + target =iptc_get_target ( i, handle); + +if ( !is_builtin ( target) &&iptc_is_chain ( target, *handle)) +printf ("\"%s\":%u -> \"%s\":1\n" , + this, + n, + target); + n++; + } +if ( n >2 ) +printf ("\n" ); + } + +printf ("}\n" ); + +return 0 ; +} + + +static int +list_entries (const ipt_chainlabel chain,int graph,int verbose,int numeric,int expanded,int linenumbers,iptc_handle_t *handle) {int found =0 ;unsigned int format;const char *this; +if ( graph) +return graph_entries ( chain, handle); + format =FMT_OPTIONS ;if (!verbose) format |=FMT_NOCOUNTS ;
@@ -1877,7 +2084,7 @@ opterr =0 ;while ((c =getopt_long (argc, argv, -"-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:" , +"-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:r" , opts,NULL )) !=-1 ) {switch (c) {/*
@@ -2189,6 +2396,11 @@break ; +case 'r' :/* Graph output */ +set_option (&options,OPT_GRAPH , &fw.ip.invflags, + invert); +break ; +case 1 :/* non option */ if (optarg[0 ] =='!' && optarg[1 ] =='\0' ) {
@@ -2449,6 +2661,7 @@break ;case CMD_LIST : ret = list_entries(chain, + options&OPT_GRAPH, options&OPT_VERBOSE, options&OPT_NUMERIC, options&OPT_EXPANDED,
@@ -2463,6 +2676,7 @@break ;case CMD_LIST |CMD_ZERO : ret = list_entries(chain, + options&OPT_GRAPH, options&OPT_VERBOSE, options&OPT_NUMERIC, options&OPT_EXPANDED,