translations from iptables to nft
Pablo Neira Ayuso
maintainercover translations from iptables to nft
Hi,
This patchset provides the infrastructure and two new utilities to provide one-way iptables to nft command translations. It aims to be simple and fit into the existing nft/xtables compat userspace infrastructure. However, most likely you will still need adapt your rule-set to fully exploit the new nf_tables capabilities.
The new proposed utilities are:
1) iptables-restore-translate which basically takes a file that contains the ruleset in iptables-restore format and converts it to the nft syntax, eg.
% iptables-restore-translate -f ipt-ruleset > nft-ruleset
% cat nft-ruleset
# Translated by iptables-restore-translate v1.4.21 on Mon Apr 14 12:18:14 2014 add table ip filter
add chain ip filter INPUT { type filter hook input priority 0; } add chain ip filter FORWARD { type filter hook forward priority 0; } add chain ip filter OUTPUT { type filter hook output priority 0; } add rule ip filter INPUT iifname lo counter accept
# -t filter -A INPUT -m state --state INVALID -j LOG --log-prefix invalid:
...
The rules that cannot be translated are left commented out. Users should be able to run this to track down the nft progress to see at what point it can fully replace iptables and their filtering policy.
It should be possible to add some option to output the % of rules that can be translated with some nice stats graph for people not even willing to look into the details.
2) iptables-translate which suggests a translation for an iptables command:
$ iptables-translate -I OUTPUT -p udp -d 8.8.8.8 -j ACCEPT nft add rule filter OUTPUT ip protocol udp ip dst 8.8.8.8 counter accept
So you can inquire the new tool to suggest the nft command as a help utility.
This also patchset includes two example translations for the tcp and state matches. I think we can provide translations for ~40% of the existing extensions. We can progressively adds more translations as nft starts supporting more missing features.
Comments welcome.
Pablo Neira Ayuso (5):
nft: xtables: add generic parsing infrastructure to interpret commands
nft: xtables-restore: add generic parsing infrastructure
nft: xtables: add the infrastructure to translate from iptables to nft
extensions: libxt_tcp: add translation to nft
extensions: libxt_state: add translation to nft
extensions/libxt_conntrack.c | 38 ++++ extensions/libxt_tcp.c | 80 +++++++ include/xtables.h | 14 ++ iptables/Makefile.am | 3 + iptables/nft-ipv4.c | 64 +++++- iptables/nft-ipv6.c | 65 +++++- iptables/nft-shared.h | 52 +++++ iptables/nft.h | 10 + iptables/xtables-compat-multi.c | 4 + iptables/xtables-multi.h | 4 + iptables/xtables-restore.c | 271 +++++++++++++---------- iptables/xtables-translate.c | 459 +++++++++++++++++++++++++++++++++++++++ iptables/xtables.c | 445 +++++++++++++++++++------------------ libxtables/xtables.c | 51 +++++ 14 files changed, 1236 insertions(+), 324 deletions(-) create mode 100644 iptables/xtables-translate.c
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 1/5 nft: xtables: add generic parsing infrastructure to interpret commands
Split the code to parse arguments and to issue command so we reuse this for the iptables to nft translation infrastructure.
--- iptables/nft-shared.h | 15 ++ iptables/xtables.c | 445 ++++++++++++++++++++++++++----------------------- 2 files changed, 249 insertions(+), 211 deletions(-)
iptables/nft-shared.h +15 -0
@@ -175,6 +175,21 @@ struct xtables_args {#define CMD_ZERO_NUM 0x2000U#define CMD_CHECK 0x4000U +struct nft_xt_cmd_parse { +unsigned int command; +unsigned int rulenum; +char *table; +char *chain; +char *newname; +char *policy; +bool restore; +int verbose; +}; + +void do_parse (struct nft_handle *h,int argc,char *argv[], +struct nft_xt_cmd_parse *p,struct iptables_command_state *cs, +struct xtables_args *args); +/* * ARP */
iptables/xtables.c +234 -211
761 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 2/5 nft: xtables-restore: add generic parsing infrastructure
In reply to Pablo Neira AyusoThis allows us to reuse the xtables-restore parser code in the translation infrastructure.
--- iptables/nft-shared.h | 35 ++++++ iptables/xtables-restore.c | 271 ++++++++++++++++++++++++++------------------ 2 files changed, 195 insertions(+), 111 deletions(-)
iptables/nft-shared.h +35 -0
@@ -190,6 +190,41 @@ void do_parse(struct nft_handle *h, int argc, char *argv[], struct nft_xt_cmd_parse *p, struct iptables_command_state *cs, struct xtables_args *args); +struct nft_xt_restore_parse { +FILE *in; +int testing; +const char *tablename; +}; + +struct nft_chain_list ; + +struct nft_xt_restore_cb { +void (*table_new)(struct nft_handle *h,const char *table); +struct nft_chain_list *(*chain_list)(struct nft_handle *h); +int (*chains_purge)(struct nft_handle *h,const char *table, +struct nft_chain_list *clist); +void (*chain_del)(struct nft_chain_list *clist,const char *curtable, +const char *chain); +int (*chain_set)(struct nft_handle *h,const char *table, +const char *chain,const char *policy, +const struct xt_counters *counters); +int (*chain_user_add)(struct nft_handle *h,const char *chain, +const char *table); + +int (*rule_flush)(struct nft_handle *h,const char *chain,const char *table); + +int (*do_command)(struct nft_handle *h,int argc,char *argv[], +char **table,bool restore); + +int (*commit)(struct nft_handle *h); +int (*abort)(struct nft_handle *h); +}; + +void xtables_restore_parse (struct nft_handle *h, +struct nft_xt_restore_parse *p, +struct nft_xt_restore_cb *cb, +int argc,char *argv[]); +/* * ARP */
iptables/xtables-restore.c +160 -111
359 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 3/5 nft: xtables: add the infrastructure to translate from iptables to nft
In reply to Pablo Neira AyusoThis patch provides the infrastructure and two new utilities to translate iptables commands to nft, they are:
1) iptables-restore-translate which basically takes a file that contains the ruleset in iptables-restore format and converts it to the nft syntax, eg.
% iptables-restore-translate -f ipt-ruleset > nft-ruleset
% cat nft-ruleset
# Translated by iptables-restore-translate v1.4.21 on Mon Apr 14 12:18:14 2014 add table ip filter
add chain ip filter INPUT { type filter hook input priority 0; } add chain ip filter FORWARD { type filter hook forward priority 0; } add chain ip filter OUTPUT { type filter hook output priority 0; } add rule ip filter INPUT iifname lo counter accept
# -t filter -A INPUT -m state --state INVALID -j LOG --log-prefix invalid:
...
The rules that cannot be translated are left commented. Users should be able to run this to track down the nft progress to see at what point it can fully replace iptables and their filtering policy.
2) iptables-translate which suggests a translation for an iptables command:
$ iptables-translate -I OUTPUT -p udp -d 8.8.8.8 -j ACCEPT nft add rule filter OUTPUT ip protocol udp ip dst 8.8.8.8 counter accept
--- include/xtables.h | 14 ++ iptables/Makefile.am | 3 + iptables/nft-ipv4.c | 64 +++++- iptables/nft-ipv6.c | 65 +++++- iptables/nft-shared.h | 2 + iptables/nft.h | 10 + iptables/xtables-compat-multi.c | 4 + iptables/xtables-multi.h | 4 + iptables/xtables-translate.c | 459 +++++++++++++++++++++++++++++++++++++++ libxtables/xtables.c | 51 +++++ 10 files changed, 674 insertions(+), 2 deletions(-) create mode 100644 iptables/xtables-translate.c
include/xtables.h +14 -0
@@ -205,6 +205,8 @@ enum xtables_ext_flags { XTABLES_EXT_ALIAS = 1 << 0, }; +struct xt_buf ; +/* Include file for additions: new matches and targets. */ struct xtables_match {
@@ -269,6 +271,9 @@ struct xtables_matchvoid (*x6_fcheck)(struct xt_fcheck_call *);const struct xt_option_entry *x6_options; +/* Translate iptables to nft */ +int (*xlate)(const struct xt_entry_match *match,struct xt_buf *buf); +/* Size of per-extension instance extra "global" scratch space */ size_t udata_size;
@@ -346,6 +351,9 @@ struct xtables_targetvoid (*x6_fcheck)(struct xt_fcheck_call *);const struct xt_option_entry *x6_options; +/* Translate iptables to nft */ +int (*xlate)(const struct xt_entry_target *target,struct xt_buf *buf); +size_t udata_size;/* Ignore these men behind the curtain: */
@@ -548,6 +556,12 @@ extern void xtables_lmap_free(struct xtables_lmap *);extern int xtables_lmap_name2id (const struct xtables_lmap *,const char *);extern const char *xtables_lmap_id2name (const struct xtables_lmap *,int ); +/* generic buffer */ +struct xt_buf *xt_buf_alloc (int size); +void xt_buf_free (struct xt_buf *buf); +void xt_buf_add (struct xt_buf *buf,const char *fmt, ...); +const char *xt_buf_get (struct xt_buf *buf); +#ifdef XTABLES_INTERNAL /* Shipped modules rely on this... */
iptables/Makefile.am +3 -0
@@ -38,6 +38,7 @@ xtables_compat_multi_SOURCES += xtables-config-parser.y xtables-config-syntax.l xtables_compat_multi_SOURCES += xtables-save.c xtables-restore.c \ xtables-standalone.c xtables.c nft.c \ nft-shared.c nft-ipv4.c nft-ipv6.c nft-arp.c \ + xtables-translate.c \ xtables-config.c xtables-events.c \ xtables-arp-standalone.c xtables-arp.c xtables_compat_multi_LDADD += ${libmnl_LIBS} ${libnftnl_LIBS}
@@ -67,6 +68,8 @@ endif if ENABLE_NFTABLES x_sbin_links = iptables-compat iptables-compat-restore iptables-compat-save \ ip6tables-compat ip6tables-compat-restore ip6tables-compat-save \ + iptables-translate ip6tables-translate \ + iptables-restore-translate ip6tables-restore-translate \ arptables-compat xtables-config xtables-events endif
iptables/nft-ipv4.c +63 -1
84 lines not on this page · raw message
iptables/nft-ipv6.c +64 -1
85 lines not on this page · raw message
iptables/nft-shared.h +2 -0
@@ -35,6 +35,7 @@#define FMT (tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))struct xtables_args ; +struct xt_buf ;struct nft_family_ops {int (*add)(struct nft_rule *r,void *data);
@@ -57,6 +58,7 @@ struct nft_family_ops {void (*parse_target)(struct xtables_target *t,void *data);bool (*rule_find)(struct nft_family_ops *ops,struct nft_rule *r,void *data); +int (*xlate)(const void *data,struct xt_buf *buf); };void add_meta (struct nft_rule *r,uint32_t key);
iptables/nft.h +10 -0
@@ -158,6 +158,16 @@ enum {int nft_xtables_config_load (struct nft_handle *h,const char *filename,uint32_t flags);/* +* Translation from iptables to nft +*/ +struct xt_buf ; + +bool xlate_find_match (const struct iptables_command_state *cs,const char *p_name); +int xlate_matches (const struct iptables_command_state *cs,struct xt_buf *buf); +int xlate_action (const struct iptables_command_state *cs,bool goto_set, +struct xt_buf *buf); + +/* * ARP */
iptables/xtables-compat-multi.c +4 -0
@@ -26,6 +26,10 @@ static const struct subcommand multi_subcommands[] = { {"ip6tables-restore", xtables_ip6_restore_main}, {"ip6tables-compat-save", xtables_ip6_save_main}, {"ip6tables-compat-restore", xtables_ip6_restore_main}, + {"iptables-translate", xtables_ip4_xlate_main}, + {"ip6tables-translate", xtables_ip6_xlate_main}, + {"iptables-restore-translate", xtables_ip4_xlate_restore_main}, + {"ip6tables-restore-translate", xtables_ip6_xlate_restore_main}, {"arptables", xtables_arp_main}, {"arptables-compat", xtables_arp_main}, {"xtables-config", xtables_config_main},
iptables/xtables-multi.h +4 -0
@@ -9,6 +9,10 @@ extern int xtables_ip4_restore_main(int, char **);extern int xtables_ip6_main (int ,char **);extern int xtables_ip6_save_main (int ,char **);extern int xtables_ip6_restore_main (int ,char **); +extern int xtables_ip4_xlate_main (int ,char **); +extern int xtables_ip6_xlate_main (int ,char **); +extern int xtables_ip4_xlate_restore_main (int ,char **); +extern int xtables_ip6_xlate_restore_main (int ,char **);extern int xtables_arp_main (int ,char **);extern int xtables_config_main (int ,char **);extern int xtables_events_main (int ,char **);
iptables/xtables-translate.c new file +459 -0
459 lines not on this page · raw message
libxtables/xtables.c +51 -0
54 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 4/5 extensions: libxt_tcp: add translation to nft
In reply to Pablo Neira AyusoTranslation for the TCP option matching is not yet implemented as we don't have a way to match this yet.
--- extensions/libxt_tcp.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
extensions/libxt_tcp.c +80 -0
@@ -362,6 +362,85 @@ static void tcp_save(const void *ip, const struct xt_entry_match *match) } } +static const struct tcp_flag_names tcp_flag_names_xlate[] = { + {"fin" ,0x01 }, + {"syn" ,0x02 }, + {"rst" ,0x04 }, + {"psh" ,0x08 }, + {"ack" ,0x10 }, + {"urg" ,0x20 }, +}; + +static void print_tcp_xlate (struct xt_buf *buf,uint8_t flags) +{ +int have_flag =0 ; + +while (flags) { +unsigned int i; + +for (i =0 ; (flags & tcp_flag_names_xlate[i].flag) ==0 ; i++); + +if (have_flag) +xt_buf_add (buf,"|" ); + +xt_buf_add (buf,"%s" , tcp_flag_names_xlate[i].name); + have_flag =1 ; + + flags &= ~tcp_flag_names_xlate[i].flag; + } + +if (!have_flag) +xt_buf_add (buf,"none" ); +} + +static int tcp_xlate (const struct xt_entry_match *match,struct xt_buf *buf) +{ +const struct xt_tcp *tcpinfo = (const struct xt_tcp *)match->data; + +if (tcpinfo->spts[0 ] !=0 || tcpinfo->spts[1 ] !=0xffff ) { +if (tcpinfo->spts[0 ] != tcpinfo->spts[1 ]) { +xt_buf_add (buf,"tcp sport %s%u-%u " , + tcpinfo->invflags &XT_TCP_INV_SRCPT ? +"!= " :"" , + tcpinfo->spts[0 ], tcpinfo->spts[1 ]); + }else { +xt_buf_add (buf,"tcp sport %s%u " , + tcpinfo->invflags &XT_TCP_INV_SRCPT ? +"!= " :"" , + tcpinfo->spts[0 ]); + } + } + +if (tcpinfo->dpts[0 ] !=0 || tcpinfo->dpts[1 ] !=0xffff ) { +if (tcpinfo->dpts[0 ] != tcpinfo->dpts[1 ]) { +xt_buf_add (buf,"tcp dport %s%u-%u " , + tcpinfo->invflags &XT_TCP_INV_DSTPT ? +"!= " :"" , + tcpinfo->dpts[0 ], tcpinfo->dpts[1 ]); + }else { +xt_buf_add (buf,"tcp dport %s%u " , + tcpinfo->invflags &XT_TCP_INV_DSTPT ? +"!= " :"" , + tcpinfo->dpts[0 ]); + } + } + +/* XXX not yet implemented */ +if (tcpinfo->option || (tcpinfo->invflags &XT_TCP_INV_OPTION )) +return 0 ; + +if (tcpinfo->flg_mask || (tcpinfo->invflags &XT_TCP_INV_FLAGS )) { +xt_buf_add (buf,"tcp flags & " ); +print_tcp_xlate (buf, tcpinfo->flg_mask); +xt_buf_add (buf," %s " , + tcpinfo->invflags &XT_TCP_INV_FLAGS ?"!= " :"==" ); +print_tcp_xlate (buf, tcpinfo->flg_cmp); +xt_buf_add (buf," " ); + } + +return 1 ; +} +static struct xtables_match tcp_match = { .family =NFPROTO_UNSPEC , .name ="tcp" ,
@@ -374,6 +453,7 @@ static struct xtables_match tcp_match = { .print = tcp_print, .save = tcp_save, .extra_opts = tcp_opts, + .xlate = tcp_xlate, };void
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 5/5 extensions: libxt_state: add translation to nft
In reply to Pablo Neira Ayuso--- extensions/libxt_conntrack.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
extensions/libxt_conntrack.c +38 -0
@@ -1156,6 +1156,43 @@ static void state_save(const void *ip, const struct xt_entry_match *match)state_print_state (sinfo->statemask); } +static void state_xlate_print (struct xt_buf *buf,unsigned int statemask) +{ +const char *sep ="" ; + +if (statemask &XT_CONNTRACK_STATE_INVALID ) { +xt_buf_add (buf,"%s%s" , sep,"invalid" ); + sep ="," ; + } +if (statemask &XT_CONNTRACK_STATE_BIT (IP_CT_NEW )) { +xt_buf_add (buf,"%s%s" , sep,"new" ); + sep ="," ; + } +if (statemask &XT_CONNTRACK_STATE_BIT (IP_CT_RELATED )) { +xt_buf_add (buf,"%s%s" , sep,"related" ); + sep ="," ; + } +if (statemask &XT_CONNTRACK_STATE_BIT (IP_CT_ESTABLISHED )) { +xt_buf_add (buf,"%s%s" , sep,"established" ); + sep ="," ; + } +if (statemask &XT_CONNTRACK_STATE_UNTRACKED ) { +xt_buf_add (buf,"%s%s" , sep,"untracked" ); + sep ="," ; + } +} + +static int state_xlate (const struct xt_entry_match *match,struct xt_buf *buf) +{ +const struct xt_conntrack_mtinfo3 *sinfo = (const void *)match->data; + +xt_buf_add (buf,"ct state %s" , sinfo->invert_flags &XT_CONNTRACK_STATE ? +"!= " :"" ); +state_xlate_print (buf, sinfo->state_mask); +xt_buf_add (buf," " ); +return 1 ; +} +static struct xtables_match conntrack_mt_reg[] = { { .version =XTABLES_VERSION ,
@@ -1306,6 +1343,7 @@ static struct xtables_match conntrack_mt_reg[] = { .save = state_save, .x6_parse = state_ct23_parse, .x6_options = state_opts, + .xlate = state_xlate, }, { .family = NFPROTO_UNSPEC,
Signature · 1 line
1.7.10.4