iptables-compat: nft: fix user chain addition, deletion and rename
Pablo Neira Ayuso
maintainerPATCH 1/4 iptables-compat: nft: fix user chain addition, deletion and rename
Add the glue code to use the chain batching for user chain commands.
Reported-by Giuseppe Longo <[email protected]>
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- iptables/nft.c | 87 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 27 deletions(-)
iptables/nft.c +60 -27
@@ -260,6 +260,8 @@ static void mnl_nft_batch_end(struct mnl_nlmsg_batch *batch, uint32_t seq)enum obj_update_type {NFT_COMPAT_TABLE_ADD ,NFT_COMPAT_CHAIN_ADD , +NFT_COMPAT_CHAIN_USER_ADD , +NFT_COMPAT_CHAIN_USER_DEL ,NFT_COMPAT_CHAIN_UPDATE ,NFT_COMPAT_RULE_APPEND ,NFT_COMPAT_RULE_INSERT ,
@@ -1304,8 +1306,6 @@ err:int nft_chain_user_add (struct nft_handle *h,const char *chain,const char *table) { -char buf[MNL_SOCKET_BUFFER_SIZE ]; -struct nlmsghdr *nlh;struct nft_chain *c;int ret;
@@ -1320,12 +1320,19 @@ int nft_chain_user_add(struct nft_handle *h, const char *chain, const char *tablnft_chain_attr_set (c,NFT_CHAIN_ATTR_TABLE , (char *)table);nft_chain_attr_set (c,NFT_CHAIN_ATTR_NAME , (char *)chain); - nlh =nft_chain_nlmsg_build_hdr (buf,NFT_MSG_NEWCHAIN , h->family, -NLM_F_ACK |NLM_F_EXCL , h->seq); -nft_chain_nlmsg_build_payload (nlh, c); -nft_chain_free (c); +if (h->batch_support) { + ret =batch_chain_add (h,NFT_COMPAT_CHAIN_USER_ADD , c); + }else { +char buf[MNL_SOCKET_BUFFER_SIZE ]; +struct nlmsghdr *nlh; - ret =mnl_talk (h, nlh,NULL ,NULL ); + nlh =nft_chain_nlmsg_build_hdr (buf,NFT_MSG_NEWCHAIN , + h->family, +NLM_F_ACK |NLM_F_EXCL , h->seq); +nft_chain_nlmsg_build_payload (nlh, c); +nft_chain_free (c); + ret =mnl_talk (h, nlh,NULL ,NULL ); + }/* the core expects 1 for success and 0 for error */ return ret ==0 ?1 :0 ;
@@ -1376,7 +1383,11 @@ int nft_chain_user_del(struct nft_handle *h, const char *chain, const char *tablif (chain !=NULL &&strcmp (chain, chain_name) !=0 ) goto next; - ret =__nft_chain_del (h, c); +if (h->batch_support) + ret =batch_chain_add (h,NFT_COMPAT_CHAIN_USER_DEL , c); +else + ret =__nft_chain_del (h, c); +if (ret <0 )break ;
@@ -1390,11 +1401,14 @@ next:nft_chain_list_iter_destroy (iter); err: -nft_chain_list_free (list); +if (!h->batch_support) +nft_chain_list_free (list);/* chain not found */ -if (ret <0 && deleted_ctr ==0 ) +if (deleted_ctr ==0 ) { + ret =-1 ; errno =ENOENT ; + }/* the core expects 1 for success and 0 for error */ return ret ==0 ?1 :0 ;
@@ -1448,8 +1462,6 @@ nft_chain_find(struct nft_handle *h, const char *table, const char *chain)int nft_chain_user_rename (struct nft_handle *h,const char *chain,const char *table,const char *newname) { -char buf[MNL_SOCKET_BUFFER_SIZE ]; -struct nlmsghdr *nlh;struct nft_chain *c;uint64_t handle;int ret;
@@ -1475,12 +1487,19 @@ int nft_chain_user_rename(struct nft_handle *h,const char *chain,nft_chain_attr_set (c,NFT_CHAIN_ATTR_NAME , (char *)newname);nft_chain_attr_set_u64 (c,NFT_CHAIN_ATTR_HANDLE , handle); - nlh =nft_chain_nlmsg_build_hdr (buf,NFT_MSG_NEWCHAIN , h->family, -NLM_F_ACK , h->seq); -nft_chain_nlmsg_build_payload (nlh, c); -nft_chain_free (c); +if (h->batch_support) { + ret =batch_chain_add (h,NFT_COMPAT_CHAIN_USER_ADD , c); + }else { +char buf[MNL_SOCKET_BUFFER_SIZE ]; +struct nlmsghdr *nlh; - ret =mnl_talk (h, nlh,NULL ,NULL ); + nlh =nft_chain_nlmsg_build_hdr (buf,NFT_MSG_NEWCHAIN , + h->family,NLM_F_ACK , h->seq); +nft_chain_nlmsg_build_payload (nlh, c); +nft_chain_free (c); + + ret =mnl_talk (h, nlh,NULL ,NULL ); + }/* the core expects 1 for success and 0 for error */ return ret ==0 ?1 :0 ;
@@ -2238,6 +2257,15 @@ static int nft_action(struct nft_handle *h, int action)NLM_F_CREATE , seq++, n->chain);break ; +case NFT_COMPAT_CHAIN_USER_ADD : +nft_compat_chain_batch_add (h,NFT_MSG_NEWCHAIN , +NLM_F_EXCL , seq++, + n->chain); +break ; +case NFT_COMPAT_CHAIN_USER_DEL : +nft_compat_chain_batch_add (h,NFT_MSG_DELCHAIN , +0 , seq++, n->chain); +break ;case NFT_COMPAT_CHAIN_UPDATE : nft_compat_chain_batch_add(h, NFT_MSG_NEWCHAIN, h->restore ?
@@ -2528,8 +2556,6 @@ int nft_chain_zero_counters(struct nft_handle *h, const char *chain,struct nft_chain_list *list;struct nft_chain_list_iter *iter;struct nft_chain *c; -struct nlmsghdr *nlh; -char buf[MNL_SOCKET_BUFFER_SIZE ];int ret =0 ; list =nft_chain_list_get (h);
@@ -2558,12 +2584,18 @@ int nft_chain_zero_counters(struct nft_handle *h, const char *chain,nft_chain_attr_unset (c,NFT_CHAIN_ATTR_HANDLE ); - nlh =nft_chain_nlmsg_build_hdr (buf,NFT_MSG_NEWCHAIN , - h->family,NLM_F_ACK , h->seq); - -nft_chain_nlmsg_build_payload (nlh, c); - - ret =mnl_talk (h, nlh,NULL ,NULL ); +if (h->batch_support) { + ret =batch_chain_add (h,NFT_COMPAT_CHAIN_ADD , c); + }else { +struct nlmsghdr *nlh; +char buf[MNL_SOCKET_BUFFER_SIZE ]; + + nlh =nft_chain_nlmsg_build_hdr (buf,NFT_MSG_NEWCHAIN , + h->family,NLM_F_ACK , + h->seq); +nft_chain_nlmsg_build_payload (nlh, c); + ret =mnl_talk (h, nlh,NULL ,NULL ); + }if (chain !=NULL )break ;
@@ -2571,11 +2603,12 @@ next: c =nft_chain_list_iter_next (iter); } +if (!h->batch_support) +nft_chain_list_free (list); +nft_chain_list_iter_destroy (iter); err: -nft_chain_list_free (list); -/* the core expects 1 for success and 0 for error */ return ret ==0 ?1 :0 ; }
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 2/4 iptables-compat: nft: fix error reporting
This fixes
# iptables-compat -X test4345 iptables: No chain/target/match by that name.
# iptables-compat -N test4345 # iptables-compat -N test4345 iptables: File exists.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- iptables/nft.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-)
iptables/nft.c +6 -16
@@ -187,17 +187,6 @@ static ssize_t mnl_nft_socket_sendmsg(const struct mnl_socket *nl)return ret; } -static int cb_err (const struct nlmsghdr *nlh,void *data) -{ -/* We can provide better error reporting than iptables-restore */ - errno =EINVAL ; -return MNL_CB_ERROR ; -} - -static mnl_cb_t cb_ctl_array[NLMSG_MIN_TYPE ] = { - [NLMSG_ERROR ] = cb_err, -}; -static int mnl_nft_batch_talk (struct nft_handle *h) {int ret, fd =mnl_socket_get_fd (h->nl);
@@ -226,11 +215,11 @@ static int mnl_nft_batch_talk(struct nft_handle *h)if (ret ==-1 )return -1 ; - ret =mnl_cb_run2 (rcv_buf, ret,0 , h->portid, -NULL ,NULL , cb_ctl_array, -MNL_ARRAY_SIZE (cb_ctl_array)); -/* Continue on error, make sure we get all acknoledgments */ -if (ret ==-1 ) + ret =mnl_cb_run (rcv_buf, ret,0 , h->portid,NULL ,NULL ); +/* Annotate first error and continue, make sure we get all +* acknoledgments. +*/ +if (!err && ret ==-1 ) err = errno; ret =select (fd+1 , &readfds,NULL ,NULL , &tv);
@@ -240,6 +229,7 @@ static int mnl_nft_batch_talk(struct nft_handle *h)FD_ZERO (&readfds);FD_SET (fd, &readfds); } + errno = err;return err ?-1 :0 ; }
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 4/4 arptables-compat: allow to not specify a target
In reply to Pablo Neira Ayusoarptables allows this:
# arptables -I INPUT
however, arptables-compat says:
arptables v1.4.21: No target provided or initalization failed Try `arptables -h' or 'arptables --help' for more information.
the compat utility must mimic the same behaviour.
Fix this by introducing the arptables_command_state abstraction that is already available in ip{6}tables.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- iptables/nft-arp.c | 166 +++++++++++++++-------------------------- iptables/nft-arp.h | 16 ++++ iptables/nft-shared.c | 2 +- iptables/nft-shared.h | 17 +---- iptables/xtables-arp.c | 181 +++++++++++++++++++-------------------------- iptables/xtables-events.c | 7 +- 6 files changed, 159 insertions(+), 230 deletions(-) create mode 100644 iptables/nft-arp.h
iptables/nft-arp.c +60 -106
@@ -25,6 +25,7 @@#include <linux/netfilter/nf_tables.h> #include "nft-shared.h" +#include "nft-arp.h" #include "nft.h" /* a few names */
@@ -160,11 +161,10 @@ static uint8_t arpt_to_ipt_flags(uint16_t invflags)static int nft_arp_add (struct nft_rule *r,void *data) { -struct arpt_entry *fw = data; +struct arptables_command_state *cs = data; +struct arpt_entry *fw = &cs->fw;uint8_t flags =arpt_to_ipt_flags (fw->arp.invflags); -struct xt_entry_target *t; -char *targname; -int ret; +int ret =0 ;if (fw->arp.iniface[0 ] !='\0' )add_iniface (r, fw->arp.iniface, flags);
@@ -221,20 +221,20 @@ static int nft_arp_add(struct nft_rule *r, void *data)if (add_counters (r, fw->counters.pcnt, fw->counters.bcnt) <0 )return -1 ; - t =nft_arp_get_target (fw); - targname = t->u.user.name; - -/* Standard target? */ -if (strcmp (targname,XTC_LABEL_ACCEPT ) ==0 ) - ret =add_verdict (r,NF_ACCEPT ); -else if (strcmp (targname,XTC_LABEL_DROP ) ==0 ) - ret =add_verdict (r,NF_DROP ); -else if (strcmp (targname,XTC_LABEL_RETURN ) ==0 ) - ret =add_verdict (r,NFT_RETURN ); -else if (xtables_find_target (targname,XTF_TRY_LOAD ) !=NULL ) - ret =add_target (r, t); -else - ret =add_jumpto (r, targname,NFT_JUMP ); +if (cs->target !=NULL ) { +/* Standard target? */ +if (strcmp (cs->jumpto,XTC_LABEL_ACCEPT ) ==0 ) + ret =add_verdict (r,NF_ACCEPT ); +else if (strcmp (cs->jumpto,XTC_LABEL_DROP ) ==0 ) + ret =add_verdict (r,NF_DROP ); +else if (strcmp (cs->jumpto,XTC_LABEL_RETURN ) ==0 ) + ret =add_verdict (r,NFT_RETURN ); +else + ret =add_target (r, cs->target->t); + }else if (strlen (cs->jumpto) >0 ) { +/* No goto in arptables */ + ret =add_jumpto (r, cs->jumpto,NFT_JUMP ); + }return ret; }
@@ -264,7 +264,8 @@ static uint16_t ipt_to_arpt_flags(uint8_t invflags)static void nft_arp_parse_meta (struct nft_xt_ctx *ctx,struct nft_rule_expr *e,void *data) { -struct arpt_entry *fw = data; +struct arptables_command_state *cs = data; +struct arpt_entry *fw = &cs->fw;uint8_t flags =0 ; parse_meta(e, ctx->meta.key, fw->arp.iniface, fw->arp.iniface_mask,
@@ -276,33 +277,17 @@ static void nft_arp_parse_meta(struct nft_xt_ctx *ctx, struct nft_rule_expr *e,static void nft_arp_parse_target (struct xtables_target *target,void *data) { -struct arpt_entry *fw = data; -struct xt_entry_target **t; +struct arptables_command_state *cs = data; - fw->target_offset = offsetof(struct arpt_entry , elems); - fw->next_offset = fw->target_offset + target->t->u.target_size; - - t = (void *) &fw->elems; - *t = target->t; + cs->target = target; }static void nft_arp_parse_immediate (const char *jumpto,bool nft_goto,void *data) { -struct xtables_target *target; -size_t size; - - target =xtables_find_target (XT_STANDARD_TARGET , -XTF_LOAD_MUST_SUCCEED ); +struct arptables_command_state *cs = data; - size =XT_ALIGN (sizeof (struct xt_entry_target )) + target->size; - - target->t =xtables_calloc (1 , size); - target->t->u.target_size = size; -strcpy (target->t->u.user.name, jumpto); - target->t->u.user.revision = target->revision; - -nft_arp_parse_target (target, data); + cs->jumpto = jumpto; }static void parse_mask_ipv4 (struct nft_xt_ctx *ctx,struct in_addr *mask)
@@ -313,7 +298,8 @@ static void parse_mask_ipv4(struct nft_xt_ctx *ctx, struct in_addr *mask)static void nft_arp_parse_payload (struct nft_xt_ctx *ctx,struct nft_rule_expr *e,void *data) { -struct arpt_entry *fw = data; +struct arptables_command_state *cs = data; +struct arpt_entry *fw = &cs->fw;struct in_addr addr;unsigned short int ar_hrd, ar_pro, ar_op, ar_hln;bool inv;
@@ -379,13 +365,14 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx, } } -void nft_rule_to_arpt_entry (struct nft_rule *r,struct arpt_entry *fw) +void nft_rule_to_arptables_command_state (struct nft_rule *r, +struct arptables_command_state *cs) {struct nft_rule_expr_iter *iter;struct nft_rule_expr *expr;int family =nft_rule_attr_get_u32 (r,NFT_RULE_ATTR_FAMILY );struct nft_xt_ctx ctx = { - .state.fw = fw, + .state.cs_arp = cs, .family = family, };
@@ -400,7 +387,7 @@ void nft_rule_to_arpt_entry(struct nft_rule *r, struct arpt_entry *fw)nft_rule_expr_get_str (expr,NFT_RULE_EXPR_ATTR_NAME );if (strcmp (name,"counter" ) ==0 ) -nft_parse_counter (expr, &ctx.state.fw->counters); +nft_parse_counter (expr, &ctx.state.cs_arp->fw.counters);else if (strcmp (name,"payload" ) ==0 )nft_parse_payload (&ctx, expr);else if (strcmp (name,"meta" ) ==0 )
@@ -418,25 +405,13 @@ void nft_rule_to_arpt_entry(struct nft_rule *r, struct arpt_entry *fw) }nft_rule_expr_iter_destroy (iter); -} - -static struct xtables_target -*get_target (struct arpt_entry *fw,unsigned int format) -{ -const char *targname; -struct xtables_target *target =NULL ; -const struct xt_entry_target *t; -if (!fw->target_offset) -return NULL ; - - t =nft_arp_get_target (fw); - targname = t->u.user.name; - target =xtables_find_target (targname,XTF_TRY_LOAD ); -if (!(format &FMT_NOTARGET )) -printf ("-j %s " , targname); - -return target; +if (cs->target !=NULL ) + cs->jumpto = cs->target->name; +else if (cs->jumpto !=NULL ) + cs->target =xtables_find_target (cs->jumpto,XTF_TRY_LOAD ); +else + cs->jumpto ="" ; }static void print_fw_details (struct arpt_entry *fw,unsigned int format)
@@ -584,29 +559,27 @@ static void nft_arp_print_firewall(struct nft_rule *r, unsigned int num, unsigned int format) { - struct arpt_entry fw = {}; -struct xtables_target *target =NULL ; -const struct xt_entry_target *t =NULL ; + struct arptables_command_state cs = {}; -nft_rule_to_arpt_entry (r, &fw); +nft_rule_to_arptables_command_state (r, &cs);if (format &FMT_LINENUMBERS )printf ("%u " , num); - target =get_target (&fw, format); -print_fw_details (&fw, format); +print_fw_details (&cs.fw, format); -if (target) { -if (target->print) +if (cs.target) { +if (cs.target->print)/* Print the target information. */ - target->FMT_NUMERIC ); + cs.target->FMT_NUMERIC ); }if (!(format &FMT_NOCOUNTS )) {printf (", pcnt=" ); -xtables_print_num (fw.counters.pcnt, format); +xtables_print_num (cs.fw.counters.pcnt, format);printf ("-- bcnt=" ); -xtables_print_num (fw.counters.bcnt, format); +xtables_print_num (cs.fw.counters.bcnt, format); }if (!(format &FMT_NONEWLINE ))
@@ -616,18 +589,16 @@ nft_arp_print_firewall(struct nft_rule *r, unsigned int num,static void nft_arp_save_firewall (const void *data,unsigned int format) { -const struct arpt_entry *fw = data; -struct xtables_target *target =NULL ; -const struct xt_entry_target *t =NULL ; +const struct arptables_command_state *cs = data; +const struct arpt_entry *fw = &cs->fw;print_fw_details ((struct arpt_entry *)fw, format); - target =get_target ((struct arpt_entry *)fw, format); - -if (target) { -if (target->print) +if (cs->target) { +if (cs->target->print)/* Print the target information. */ - target->FMT_NUMERIC ); + cs->target->FMT_NUMERIC ); }printf ("\n" ); }
@@ -661,45 +632,28 @@ static bool nft_arp_is_same(const void *data_a,static bool nft_arp_rule_find (struct nft_family_ops *ops,struct nft_rule *r,void *data) { -struct arpt_entry *fw = data; -struct xt_entry_target *t_fw, *t_this; -char *targname_fw, *targname_this; -struct arpt_entry this = {}; +const struct arptables_command_state *cs = data; +struct arptables_command_state this = {};/* Delete by matching rule case */ -nft_rule_to_arpt_entry (r, &this); +nft_rule_to_arptables_command_state (r, &this); -if (!ops->is_same (fw, &this)) +if (!nft_arp_is_same (cs, &this))return false; - t_fw =nft_arp_get_target (fw); - t_this =nft_arp_get_target (&this); - - targname_fw = t_fw->u.user.name; - targname_this = t_this->u.user.name; - -if (!strcmp (targname_fw, targname_this) && - (!strcmp (targname_fw,"mangle" ) || - !strcmp (targname_fw,"CLASSIFY" ))) { -if (memcmp (t_fw->data, t_this->data, - t_fw->u.user.target_size -sizeof (*t_fw)) !=0 ) { -DEBUGP ("Different target\n" ); -return false; - } -return true; - } +if (!compare_targets (cs->target, this.target)) +return false; -if (strcmp (targname_fw, targname_this) !=0 ) { -DEBUGP ("Different verdict\n" ); +if (strcmp (cs->jumpto, this.jumpto) !=0 )return false; - }return true; }static void nft_arp_save_counters (const void *data) { -const struct arpt_entry *fw = data; +const struct arptables_command_state *cs = data; +const struct arpt_entry *fw = &cs->fw;save_counters (fw->counters.pcnt, fw->counters.bcnt); }
iptables/nft-arp.h new file +16 -0
@@ -0,0 +1,16 @@ +#ifndef _NFT_ARP_H_ +#define _NFT_ARP_H_ + +extern char *opcodes[]; +#define NUMOPCODES 9 + +struct arptables_command_state { +struct arpt_entry fw; +struct xtables_target *target; +const char *jumpto; +}; + +void nft_rule_to_arptables_command_state (struct nft_rule *r, +struct arptables_command_state *cs); + +#endif
iptables/nft-shared.c +1 -1
@@ -303,7 +303,7 @@ static void *nft_get_data(struct nft_xt_ctx *ctx)case NFPROTO_IPV6 :return ctx->state.cs;case NFPROTO_ARP : -return ctx->state.fw; +return ctx->state.cs_arp;default :/* Should not happen */ return NULL ;
iptables/nft-shared.h +1 -16
@@ -47,7 +47,7 @@ enum {struct nft_xt_ctx {union {struct iptables_command_state *cs; -struct arpt_entry *fw; +struct arptables_command_state *cs_arp; } state;struct nft_rule_expr_iter *iter;int family;
@@ -204,19 +204,4 @@ struct xtables_args {#define CMD_ZERO_NUM 0x2000U#define CMD_CHECK 0x4000U -/* -* ARP -*/ -extern char *opcodes[]; -#define NUMOPCODES 9 - -static inline struct xt_entry_target *nft_arp_get_target (struct arpt_entry *fw) -{ -struct xt_entry_target **target; - - target = (void *) &fw->elems; - -return *target; -} -#endif
iptables/xtables-arp.c +77 -104
421 lines not on this page · raw message
iptables/xtables-events.c +4 -3
@@ -27,6 +27,7 @@#include "iptables.h" /* for xtables_globals */ #include "xtables-multi.h" #include "nft.h" +#include "nft-arp.h" static int table_cb (const struct nlmsghdr *nlh,int type) {
@@ -55,7 +56,7 @@ static bool counters;static int rule_cb (const struct nlmsghdr *nlh,int type) {struct iptables_command_state cs = {}; -struct arpt_entry fw_arp = {}; +struct arptables_command_state cs_arp = {};struct nft_rule *r;void *fw =NULL ;uint8_t family;
@@ -77,8 +78,8 @@ static int rule_cb(const struct nlmsghdr *nlh, int type)break ;case NFPROTO_ARP :printf ("-0 " ); -nft_rule_to_arpt_entry (r, &fw_arp); - fw = &fw_arp; +nft_rule_to_arptables_command_state (r, &cs_arp); + fw = &cs_arp;break ;default : goto err_free;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 3/4 arptables-compat: fix missing error reporting
In reply to Pablo Neira Ayuso# arptables-compat -D INPUT -j ACCEPT arptables: Bad rule (does a matching rule exist in that chain?)
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- iptables/xtables-arp-standalone.c | 5 +++++ 1 file changed, 5 insertions(+)
iptables/xtables-arp-standalone.c +5 -0
@@ -84,5 +84,10 @@ int xtables_arp_main(int argc, char *argv[])if (ret) ret =nft_commit (&h); +nft_fini (&h); + +if (!ret) +fprintf (stderr,"arptables: %s\n" ,nft_strerror (errno)); +exit (!ret); }
Signature · 1 line
1.7.10.4