nft: refactoring parse operations for more genericity
PATCH 1/1 nft: refactoring parse operations for more genericity
This will allow reusing nft_parse_* function for other family than IPv4 or IPv6.
--- iptables/nft-ipv4.c | 27 ++++++++++++++++++++++----- iptables/nft-ipv6.c | 26 +++++++++++++++++++++----- iptables/nft-shared.c | 44 +++++++++++++++++++++++++++----------------- iptables/nft-shared.h | 8 ++++---- 4 files changed, 74 insertions(+), 31 deletions(-)
iptables/nft-ipv4.c +22 -5
@@ -148,17 +148,20 @@ static const char *mask_to_str(uint32_t mask) }static void nft_ipv4_parse_meta (struct nft_rule_expr *e,uint8_t key, -struct iptables_command_state *cs) +void *data) { +struct iptables_command_state *cs = data; +parse_meta (e, key, cs->fw.ip.iniface, cs->fw.ip.iniface_mask, cs->fw.ip.outiface, cs->fw.ip.outiface_mask, &cs->fw.ip.invflags); }static void nft_ipv4_parse_payload (struct nft_rule_expr_iter *iter, -struct iptables_command_state *cs, -uint32_t offset) +uint32_t offset,void *data) { +struct iptables_command_state *cs = data; +switch (offset) {struct in_addr addr;uint8_t proto;
@@ -196,9 +199,15 @@ static void nft_ipv4_parse_payload(struct nft_rule_expr_iter *iter, } } -static void nft_ipv4_parse_immediate (struct iptables_command_state *cs) +static void nft_ipv4_parse_immediate (const char *jumpto,bool nft_goto, +void *data) { - cs->fw.ip.flags |=IPT_F_GOTO ; +struct iptables_command_state *cs = data; + + cs->jumpto = jumpto; + +if (nft_goto) + cs->fw.ip.flags |=IPT_F_GOTO ; } static void print_ipv4_addr(const struct iptables_command_state *cs,
@@ -351,6 +360,13 @@ static void nft_ipv4_post_parse(int command, " source or destination IP addresses"); } +static void nft_ipv4_parse_target (struct xtables_target *t,void *data) +{ +struct iptables_command_state *cs = data; + + cs->target = t; +} +struct nft_family_ops nft_family_ops_ipv4 = { .add = nft_ipv4_add, .is_same = nft_ipv4_is_same,
@@ -360,4 +376,5 @@ struct nft_family_ops nft_family_ops_ipv4 = { .print_firewall = nft_ipv4_print_firewall, .save_firewall = nft_ipv4_save_firewall, .post_parse = nft_ipv4_post_parse, + .parse_target = nft_ipv4_parse_target, };
iptables/nft-ipv6.c +21 -5
@@ -70,17 +70,19 @@ static bool nft_ipv6_is_same(const struct iptables_command_state *a, }static void nft_ipv6_parse_meta (struct nft_rule_expr *e,uint8_t key, -struct iptables_command_state *cs) +void *data) { +struct iptables_command_state *cs = data; +parse_meta (e, key, cs->fw6.ipv6.iniface, cs->fw6.ipv6.iniface_mask, cs->fw6.ipv6.outiface, cs->fw6.ipv6.outiface_mask, &cs->fw6.ipv6.invflags); }static void nft_ipv6_parse_payload (struct nft_rule_expr_iter *iter, -struct iptables_command_state *cs, -uint32_t offset) +uint32_t offset,void *data) { +struct iptables_command_state *cs = data;switch (offset) {struct in6_addr addr;uint8_t proto;
@@ -110,9 +112,15 @@ static void nft_ipv6_parse_payload(struct nft_rule_expr_iter *iter, } } -static void nft_ipv6_parse_immediate (struct iptables_command_state *cs) +static void nft_ipv6_parse_immediate (const char *jumpto,bool nft_goto, +void *data) { - cs->fw6.ipv6.flags |=IPT_F_GOTO ; +struct iptables_command_state *cs = data; + + cs->jumpto = jumpto; + +if (nft_goto) + cs->fw6.ipv6.flags |=IPT_F_GOTO ; } static void print_ipv6_addr(const struct iptables_command_state *cs,
@@ -274,6 +282,13 @@ static void nft_ipv6_post_parse(int command, struct iptables_command_state *cs, " source or destination IP addresses"); } +static void nft_ipv6_parse_target (struct xtables_target *t,void *data) +{ +struct iptables_command_state *cs = data; + + cs->target = t; +} +struct nft_family_ops nft_family_ops_ipv6 = { .add = nft_ipv6_add, .is_same = nft_ipv6_is_same,
@@ -283,4 +298,5 @@ struct nft_family_ops nft_family_ops_ipv6 = { .print_firewall = nft_ipv6_print_firewall, .save_firewall = nft_ipv6_save_firewall, .post_parse = nft_ipv6_post_parse, + .parse_target = nft_ipv6_parse_target, };
iptables/nft-shared.c +27 -17
106 lines not on this page · raw message
iptables/nft-shared.h +4 -4
@@ -43,17 +43,17 @@ struct nft_family_ops {void (*print_payload)(struct nft_rule_expr *e,struct nft_rule_expr_iter *iter);void (*parse_meta)(struct nft_rule_expr *e,uint8_t key, -struct iptables_command_state *cs); +void *data);void (*parse_payload)(struct nft_rule_expr_iter *iter, -struct iptables_command_state *cs, -uint32_t offset); -void (*parse_immediate)(struct iptables_command_state *cs); +uint32_t offset,void *data); +void (*parse_immediate)(const char *jumpto,bool nft_goto,void *data);void (*print_firewall)(struct nft_rule *r,unsigned int num,unsigned int format);uint8_t (*save_firewall)(const struct iptables_command_state *cs,unsigned int format);void (*post_parse)(int command,struct iptables_command_state *cs,struct xtables_args *args); +void (*parse_target)(struct xtables_target *t,void *data); };void add_meta (struct nft_rule *r,uint32_t key);
Signature · 1 line
1.7.8.6
Pablo Neira Ayuso
maintainerHi Giuseppe,
Quoted 2 lines · Giuseppe Longo
This will allow reusing nft_parse_* function for other family than IPv4 or IPv6.
I like that you send me patches to prepare the arptables support, but it seems to break things. If I run:
xtables -I INPUT -s 1.1.1.1/32 -d 2.2.2.2/32 -j LOG
the xtables-save says:
-A INPUT -s 1.1.1.1/32 -d 2.2.2.2/32
Note the missing -j target.
Please, fix and resend.
Hi Giuseppe,
iptables/nft-shared.c nft_parse_counter(struct nft_rule_expr *e, struct nft_rule_expr_iter *iter, · line 450
Quoted 5 lines
case
NFT_GOTO:iptables/nft-shared.c +2
-2- ops =nft_family_ops_lookup (family); - ops->parse_immediate (cs); + nft_goto = true; +break ;
You added a bogus "break" here: it should fall to NFT_JUMP to set the chain as the jumpto. That's the bug Pablo has found.
Tomasz