1 /* Code to save the xtables state, in human readable-form. */
2 /* (C) 1999 by Paul 'Rusty' Russell <rusty@rustcorp.com.au> and
3 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
6 * This code is distributed under the terms of GNU GPL v2
20 #include "libiptc/libiptc.h"
22 #include "xtables-multi.h"
24 #include "nft-cache.h"
26 #include <libnftnl/chain.h>
28 #ifndef NO_SHARED_LIBS
32 #define prog_name xtables_globals.program_name
33 #define prog_vers xtables_globals.program_version
35 static const char *ipt_save_optstring
= "bcdt:M:f:V";
36 static const struct option ipt_save_options
[] = {
37 {.name
= "counters", .has_arg
= false, .val
= 'c'},
38 {.name
= "version", .has_arg
= false, .val
= 'V'},
39 {.name
= "dump", .has_arg
= false, .val
= 'd'},
40 {.name
= "table", .has_arg
= true, .val
= 't'},
41 {.name
= "modprobe", .has_arg
= true, .val
= 'M'},
42 {.name
= "file", .has_arg
= true, .val
= 'f'},
46 static const char *arp_save_optstring
= "cM:V";
47 static const struct option arp_save_options
[] = {
48 {.name
= "counters", .has_arg
= false, .val
= 'c'},
49 {.name
= "version", .has_arg
= false, .val
= 'V'},
50 {.name
= "modprobe", .has_arg
= true, .val
= 'M'},
54 static const char *ebt_save_optstring
= "ct:M:V";
55 static const struct option ebt_save_options
[] = {
56 {.name
= "counters", .has_arg
= false, .val
= 'c'},
57 {.name
= "version", .has_arg
= false, .val
= 'V'},
58 {.name
= "table", .has_arg
= true, .val
= 't'},
59 {.name
= "modprobe", .has_arg
= true, .val
= 'M'},
63 struct do_output_data
{
69 __do_output(struct nft_handle
*h
, const char *tablename
, void *data
)
71 struct do_output_data
*d
= data
;
74 if (!nft_table_builtin_find(h
, tablename
))
77 if (!nft_is_table_compatible(h
, tablename
, NULL
)) {
78 printf("# Table `%s' is incompatible, use 'nft' tool.\n",
81 } else if (nft_is_table_tainted(h
, tablename
)) {
82 printf("# Table `%s' contains incompatible base-chains, use 'nft' tool to list them.\n",
87 printf("# Generated by %s v%s on %s", prog_name
,
88 prog_vers
, ctime(&now
));
90 printf("*%s\n", tablename
);
91 /* Dump out chain names first,
92 * thereby preventing dependency conflicts */
93 nft_cache_sort_chains(h
, tablename
);
94 nft_chain_foreach(h
, tablename
, nft_chain_save
, h
);
95 nft_rule_save(h
, tablename
, d
->format
);
100 printf("# Completed on %s", ctime(&now
));
105 do_output(struct nft_handle
*h
, const char *tablename
, struct do_output_data
*d
)
110 ret
= nft_for_each_table(h
, __do_output
, d
);
111 nft_check_xt_legacy(h
->family
, true);
115 if (!nft_table_find(h
, tablename
) &&
116 !nft_table_builtin_find(h
, tablename
)) {
117 fprintf(stderr
, "Table `%s' does not exist\n", tablename
);
121 ret
= __do_output(h
, tablename
, d
);
122 nft_check_xt_legacy(h
->family
, true);
127 * :Chain name POLICY packets bytes
131 xtables_save_main(int family
, int argc
, char *argv
[],
132 const char *optstring
, const struct option
*longopts
)
134 const char *tablename
= NULL
;
135 struct do_output_data d
= {
136 .format
= FMT_NOCOUNTS
,
143 xtables_globals
.program_name
= basename(*argv
);;
144 c
= xtables_init_all(&xtables_globals
, family
);
146 fprintf(stderr
, "%s/%s Failed to initialize xtables\n",
147 xtables_globals
.program_name
,
148 xtables_globals
.program_version
);
152 while ((c
= getopt_long(argc
, argv
, optstring
, longopts
, NULL
)) != -1) {
155 fprintf(stderr
, "-b/--binary option is not implemented\n");
158 d
.format
&= ~FMT_NOCOUNTS
;
162 /* Select specific table. */
166 xtables_modprobe_program
= optarg
;
169 file
= fopen(optarg
, "w");
171 fprintf(stderr
, "Failed to open file, error: %s\n",
175 ret
= dup2(fileno(file
), STDOUT_FILENO
);
177 fprintf(stderr
, "Failed to redirect stdout, error: %s\n",
187 printf("%s v%s\n", prog_name
, prog_vers
);
191 "Look at manual page `%s.8' for more information.\n",
198 fprintf(stderr
, "Unknown arguments found on commandline\n");
215 case NFPROTO_BRIDGE
: {
216 const char *ctr
= getenv("EBTABLES_SAVE_COUNTER");
218 if (!(d
.format
& FMT_NOCOUNTS
)) {
219 d
.format
|= FMT_EBT_SAVE
;
220 } else if (ctr
&& !strcmp(ctr
, "yes")) {
221 d
.format
&= ~FMT_NOCOUNTS
;
222 d
.format
|= FMT_C_COUNTS
| FMT_EBT_SAVE
;
228 fprintf(stderr
, "Unknown family %d\n", family
);
232 if (nft_init(&h
, family
) < 0) {
233 fprintf(stderr
, "%s/%s Failed to initialize nft: %s\n",
234 xtables_globals
.program_name
,
235 xtables_globals
.program_version
,
240 nft_cache_level_set(&h
, NFT_CL_RULES
, NULL
);
242 nft_xt_fake_builtin_chains(&h
, tablename
, NULL
);
244 ret
= do_output(&h
, tablename
, &d
);
253 int xtables_ip4_save_main(int argc
, char *argv
[])
255 return xtables_save_main(NFPROTO_IPV4
, argc
, argv
,
256 ipt_save_optstring
, ipt_save_options
);
259 int xtables_ip6_save_main(int argc
, char *argv
[])
261 return xtables_save_main(NFPROTO_IPV6
, argc
, argv
,
262 ipt_save_optstring
, ipt_save_options
);
265 int xtables_eb_save_main(int argc
, char *argv
[])
267 return xtables_save_main(NFPROTO_BRIDGE
, argc
, argv
,
268 ebt_save_optstring
, ebt_save_options
);
271 int xtables_arp_save_main(int argc
, char *argv
[])
273 return xtables_save_main(NFPROTO_ARP
, argc
, argv
,
274 arp_save_optstring
, arp_save_options
);