netfilter: nfnetlink: add struct nfgenmsg to struct nfnl_info and use it
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v3 1/3 netfilter: nfnetlink: add struct nfgenmsg to struct nfnl_info and use it
Update the nfnl_info structure to add a pointer to the nfnetlink header. This simplifies the existing codebase since this header is usually accessed. Update existing clients to use this new field.
---
v3: use u8 family instead of in family in nf_tables.
include/linux/netfilter/nfnetlink.h | 1 + net/netfilter/nf_conntrack_netlink.c | 23 +++++------- net/netfilter/nf_tables_api.c | 55 ++++++++++------------------ net/netfilter/nfnetlink.c | 2 + net/netfilter/nfnetlink_log.c | 5 +-- net/netfilter/nfnetlink_queue.c | 9 ++--- net/netfilter/nft_compat.c | 17 +++------ 7 files changed, 42 insertions(+), 70 deletions(-)
include/linux/netfilter/nfnetlink.h +1 -0
@@ -11,6 +11,7 @@ struct nfnl_info {struct net *net;struct sock *sk;const struct nlmsghdr *nlh; +const struct nfgenmsg *nfmsg;struct netlink_ext_ack *extack; };
net/netfilter/nf_conntrack_netlink.c +9 -14
@@ -1528,7 +1528,7 @@ static int ctnetlink_del_conntrack(struct sk_buff *skb,const struct nfnl_info *info, const struct nlattr * const cda[]) { - struct nfgenmsg *nfmsg = nlmsg_data(info->nlh); + u8 family = info->nfmsg->nfgen_family;struct nf_conntrack_tuple_hash *h;struct nf_conntrack_tuple tuple;struct nf_conntrack_zone zone;
@@ -1541,12 +1541,12 @@ static int ctnetlink_del_conntrack(struct sk_buff *skb,if (cda[CTA_TUPLE_ORIG ]) err =ctnetlink_parse_tuple (cda, &tuple,CTA_TUPLE_ORIG , - nfmsg->nfgen_family, &zone); + family, &zone);else if (cda[CTA_TUPLE_REPLY ]) err =ctnetlink_parse_tuple (cda, &tuple,CTA_TUPLE_REPLY , - nfmsg->nfgen_family, &zone); + family, &zone);else { -u_int8_t u3 = nfmsg->version ? nfmsg->nfgen_family :AF_UNSPEC ; +u_int8_t u3 = info->nfmsg->version ? family :AF_UNSPEC ; return ctnetlink_flush_conntrack(info->net, cda, NETLINK_CB(skb).portid,
@@ -1586,8 +1586,7 @@ static int ctnetlink_get_conntrack(struct sk_buff *skb,const struct nfnl_info *info, const struct nlattr * const cda[]) { - struct nfgenmsg *nfmsg = nlmsg_data(info->nlh); -u_int8_t u3 = nfmsg->nfgen_family; + u_int8_t u3 = info->nfmsg->nfgen_family;struct nf_conntrack_tuple_hash *h;struct nf_conntrack_tuple tuple;struct nf_conntrack_zone zone;
@@ -2363,10 +2362,9 @@ static int ctnetlink_new_conntrack(struct sk_buff *skb,const struct nfnl_info *info, const struct nlattr * const cda[]) { - struct nfgenmsg *nfmsg = nlmsg_data(info->nlh); struct nf_conntrack_tuple otuple, rtuple;struct nf_conntrack_tuple_hash *h =NULL ; -u_int8_t u3 = nfmsg->nfgen_family; +u_int8_t u3 = info->nfmsg->nfgen_family;struct nf_conntrack_zone zone;struct nf_conn *ct;int err;
@@ -3259,8 +3257,7 @@ static int ctnetlink_get_expect(struct sk_buff *skb,const struct nfnl_info *info, const struct nlattr * const cda[]) { - struct nfgenmsg *nfmsg = nlmsg_data(info->nlh); -u_int8_t u3 = nfmsg->nfgen_family; + u_int8_t u3 = info->nfmsg->nfgen_family;struct nf_conntrack_tuple tuple;struct nf_conntrack_expect *exp;struct nf_conntrack_zone zone;
@@ -3349,8 +3346,7 @@ static int ctnetlink_del_expect(struct sk_buff *skb,const struct nfnl_info *info, const struct nlattr * const cda[]) { - struct nfgenmsg *nfmsg = nlmsg_data(info->nlh); -u_int8_t u3 = nfmsg->nfgen_family; + u_int8_t u3 = info->nfmsg->nfgen_family;struct nf_conntrack_expect *exp;struct nf_conntrack_tuple tuple;struct nf_conntrack_zone zone;
@@ -3601,8 +3597,7 @@ static int ctnetlink_new_expect(struct sk_buff *skb,const struct nfnl_info *info, const struct nlattr * const cda[]) { - struct nfgenmsg *nfmsg = nlmsg_data(info->nlh); -u_int8_t u3 = nfmsg->nfgen_family; + u_int8_t u3 = info->nfmsg->nfgen_family;struct nf_conntrack_tuple tuple;struct nf_conntrack_expect *exp;struct nf_conntrack_zone zone;
net/netfilter/nf_tables_api.c +19 -36
218 lines not on this page · raw message
net/netfilter/nfnetlink.c +2 -0
@@ -256,6 +256,7 @@ static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, .net = net, .sk = nfnlnet->nfnl, .nlh = nlh, + .nfmsg = nlmsg_data(nlh), .extack = extack, };
@@ -491,6 +492,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, .net = net, .sk = nfnlnet->nfnl, .nlh = nlh, + .nfmsg = nlmsg_data(nlh), .extack = &extack, };
net/netfilter/nfnetlink_log.c +2 -3
@@ -871,15 +871,14 @@ static int nfulnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info, const struct nlattr * const nfula[]) {struct nfnl_log_net *log =nfnl_log_pernet (info->net); -struct nfgenmsg *nfmsg =nlmsg_data (info->nlh); -u_int16_t group_num =ntohs (nfmsg->res_id); +u_int16_t group_num =ntohs (info->nfmsg->res_id);struct nfulnl_msg_config_cmd *cmd =NULL ;struct nfulnl_instance *inst;u16 flags =0 ;int ret =0 ;if (nfula[NFULA_CFG_CMD ]) { -u_int8_t pf = nfmsg->nfgen_family; +u_int8_t pf = info->nfmsg->nfgen_family; cmd =nla_data (nfula[NFULA_CFG_CMD ]);/* Commands without queue context */
net/netfilter/nfnetlink_queue.c +3 -6
@@ -1051,8 +1051,7 @@ static int nfqnl_recv_verdict_batch(struct sk_buff *skb, const struct nlattr * const nfqa[]) {struct nfnl_queue_net *q =nfnl_queue_pernet (info->net); -struct nfgenmsg *nfmsg =nlmsg_data (info->nlh); -u16 queue_num =ntohs (nfmsg->res_id); +u16 queue_num =ntohs (info->nfmsg->res_id);struct nf_queue_entry *entry, *tmp;struct nfqnl_msg_verdict_hdr *vhdr;struct nfqnl_instance *queue;
@@ -1160,8 +1159,7 @@ static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info, const struct nlattr * const nfqa[]) {struct nfnl_queue_net *q =nfnl_queue_pernet (info->net); -struct nfgenmsg *nfmsg =nlmsg_data (info->nlh); -u_int16_t queue_num =ntohs (nfmsg->res_id); +u_int16_t queue_num =ntohs (info->nfmsg->res_id);struct nfqnl_msg_verdict_hdr *vhdr;enum ip_conntrack_info ctinfo;struct nfqnl_instance *queue;
@@ -1243,8 +1241,7 @@ static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info, const struct nlattr * const nfqa[]) {struct nfnl_queue_net *q =nfnl_queue_pernet (info->net); -struct nfgenmsg *nfmsg =nlmsg_data (info->nlh); -u_int16_t queue_num =ntohs (nfmsg->res_id); +u_int16_t queue_num =ntohs (info->nfmsg->res_id);struct nfqnl_msg_config_cmd *cmd =NULL ;struct nfqnl_instance *queue;__u32 flags =0 , mask =0 ;
net/netfilter/nft_compat.c +6 -11
@@ -625,7 +625,7 @@ static int nfnl_compat_get_rcu(struct sk_buff *skb,const struct nfnl_info *info, const struct nlattr * const tb[]) { - struct nfgenmsg *nfmsg; + u8 family = info->nfmsg->nfgen_family;const char *name, *fmt;struct sk_buff *skb2;int ret =0 , target;
@@ -640,9 +640,7 @@ static int nfnl_compat_get_rcu(struct sk_buff *skb, rev =ntohl (nla_get_be32 (tb[NFTA_COMPAT_REV ])); target =ntohl (nla_get_be32 (tb[NFTA_COMPAT_TYPE ])); - nfmsg =nlmsg_data (info->nlh); - -switch (nfmsg->nfgen_family) { +switch (family) {case AF_INET : fmt ="ipt_%s" ;break ;
@@ -656,8 +654,7 @@ static int nfnl_compat_get_rcu(struct sk_buff *skb, fmt ="arpt_%s" ;break ;default : -pr_err ("nft_compat: unsupported protocol %d\n" , - nfmsg->nfgen_family); +pr_err ("nft_compat: unsupported protocol %d\n" , family);return -EINVAL ; }
@@ -665,9 +662,8 @@ static int nfnl_compat_get_rcu(struct sk_buff *skb,return -EINVAL ;rcu_read_unlock (); -try_then_request_module (xt_find_revision (nfmsg->nfgen_family, name, - rev, target, &ret), - fmt, name); +try_then_request_module (xt_find_revision (family, name, rev, target, &ret), + fmt, name);if (ret <0 ) goto out_put;
@@ -682,8 +678,7 @@ static int nfnl_compat_get_rcu(struct sk_buff *skb, info->nlh->nlmsg_seq, NFNL_MSG_TYPE(info->nlh->nlmsg_type), NFNL_MSG_COMPAT_GET, - nfmsg->nfgen_family, - name, ret, target) <= 0) { + family, name, ret, target) <= 0) {kfree_skb (skb2); goto out_put; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v3 2/3 netfilter: nf_tables: remove nft_ctx_init_from_elemattr()
Replace nft_ctx_init_from_elemattr() by nft_table_lookup() and set up the context structure right before it is really needed.
Moreover, nft_ctx_init_from_elemattr() is setting up the context structure for codepaths where this is not really needed at all.
This helper function is also not helping to consolidate code, removing it saves us 4 LoC.
---
v3: use u8 family instead of in family in nf_tables.
net/netfilter/nf_tables_api.c | 72 +++++++++++++++++------------------ 1 file changed, 34 insertions(+), 38 deletions(-)
net/netfilter/nf_tables_api.c +34 -38
@@ -4703,28 +4703,6 @@ static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, }; -static int nft_ctx_init_from_elemattr (struct nft_ctx *ctx,struct net *net, -const struct sk_buff *skb, -const struct nlmsghdr *nlh, -const struct nlattr *const nla[], -struct netlink_ext_ack *extack, -u8 genmask,u32 nlpid) -{ -const struct nfgenmsg *nfmsg =nlmsg_data (nlh); -int family = nfmsg->nfgen_family; -struct nft_table *table; - - table =nft_table_lookup (net, nla[NFTA_SET_ELEM_LIST_TABLE ], family, - genmask, nlpid); -if (IS_ERR (table)) { -NL_SET_BAD_ATTR (extack, nla[NFTA_SET_ELEM_LIST_TABLE ]); -return PTR_ERR (table); - } - -nft_ctx_init (ctx, net, skb, nlh, family, table,NULL , nla); -return 0 ; -} -static int nft_set_elem_expr_dump (struct sk_buff *skb,const struct nft_set *set,const struct nft_set_ext *ext)
@@ -5182,21 +5160,27 @@ static int nf_tables_getsetelem(struct sk_buff *skb, {struct netlink_ext_ack *extack = info->extack;u8 genmask =nft_genmask_cur (info->net); +u8 family = info->nfmsg->nfgen_family;struct net *net = info->net; +struct nft_table *table;struct nft_set *set;struct nlattr *attr;struct nft_ctx ctx;int rem, err =0 ; - err =nft_ctx_init_from_elemattr (&ctx, net, skb, info->nlh, nla, extack, - genmask,NETLINK_CB (skb).portid); -if (err <0 ) -return err; + table =nft_table_lookup (net, nla[NFTA_SET_ELEM_LIST_TABLE ], family, + genmask,NETLINK_CB (skb).portid); +if (IS_ERR (table)) { +NL_SET_BAD_ATTR (extack, nla[NFTA_SET_ELEM_LIST_TABLE ]); +return PTR_ERR (table); + } - set =nft_set_lookup (ctx.table, nla[NFTA_SET_ELEM_LIST_SET ], genmask); + set =nft_set_lookup (table, nla[NFTA_SET_ELEM_LIST_SET ], genmask);if (IS_ERR (set))return PTR_ERR (set); +nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla); +if (info->nlh->nlmsg_flags &NLM_F_DUMP ) {struct netlink_dump_control c = { .start = nf_tables_dump_set_start,
@@ -5965,8 +5949,10 @@ static int nf_tables_newsetelem(struct sk_buff *skb,struct nftables_pernet *nft_net =nft_pernet (info->net);struct netlink_ext_ack *extack = info->extack;u8 genmask =nft_genmask_next (info->net); +u8 family = info->nfmsg->nfgen_family;struct net *net = info->net;const struct nlattr *attr; +struct nft_table *table;struct nft_set *set;struct nft_ctx ctx;int rem, err;
@@ -5974,12 +5960,14 @@ static int nf_tables_newsetelem(struct sk_buff *skb,if (nla[NFTA_SET_ELEM_LIST_ELEMENTS ] ==NULL )return -EINVAL ; - err =nft_ctx_init_from_elemattr (&ctx, net, skb, info->nlh, nla, extack, - genmask,NETLINK_CB (skb).portid); -if (err <0 ) -return err; + table =nft_table_lookup (net, nla[NFTA_SET_ELEM_LIST_TABLE ], family, + genmask,NETLINK_CB (skb).portid); +if (IS_ERR (table)) { +NL_SET_BAD_ATTR (extack, nla[NFTA_SET_ELEM_LIST_TABLE ]); +return PTR_ERR (table); + } - set =nft_set_lookup_global (net, ctx.table, nla[NFTA_SET_ELEM_LIST_SET ], + set =nft_set_lookup_global (net, table, nla[NFTA_SET_ELEM_LIST_SET ], nla[NFTA_SET_ELEM_LIST_SET_ID ], genmask);if (IS_ERR (set))return PTR_ERR (set);
@@ -5987,6 +5975,8 @@ static int nf_tables_newsetelem(struct sk_buff *skb,if (!list_empty (&set->bindings) && set->flags &NFT_SET_CONSTANT )return -EBUSY ; +nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla); +nla_for_each_nested (attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS ], rem) { err =nft_add_set_elem (&ctx, set, attr, info->nlh->nlmsg_flags);if (err <0 )
@@ -5994,7 +5984,7 @@ static int nf_tables_newsetelem(struct sk_buff *skb, }if (nft_net->validate_state ==NFT_VALIDATE_DO ) -return nft_table_validate (net, ctx.table); +return nft_table_validate (net, table);return 0 ; }
@@ -6232,23 +6222,29 @@ static int nf_tables_delsetelem(struct sk_buff *skb, {struct netlink_ext_ack *extack = info->extack;u8 genmask =nft_genmask_next (info->net); +u8 family = info->nfmsg->nfgen_family;struct net *net = info->net;const struct nlattr *attr; +struct nft_table *table;struct nft_set *set;struct nft_ctx ctx;int rem, err =0 ; - err =nft_ctx_init_from_elemattr (&ctx, net, skb, info->nlh, nla, extack, - genmask,NETLINK_CB (skb).portid); -if (err <0 ) -return err; + table =nft_table_lookup (net, nla[NFTA_SET_ELEM_LIST_TABLE ], family, + genmask,NETLINK_CB (skb).portid); +if (IS_ERR (table)) { +NL_SET_BAD_ATTR (extack, nla[NFTA_SET_ELEM_LIST_TABLE ]); +return PTR_ERR (table); + } - set =nft_set_lookup (ctx.table, nla[NFTA_SET_ELEM_LIST_SET ], genmask); + set =nft_set_lookup (table, nla[NFTA_SET_ELEM_LIST_SET ], genmask);if (IS_ERR (set))return PTR_ERR (set);if (!list_empty (&set->bindings) && set->flags &NFT_SET_CONSTANT )return -EBUSY ; +nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla); +if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS ])return nft_set_flush (&ctx, set, genmask);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/3 netfilter: nf_tables: remove nft_ctx_init_from_setattr()
In reply to Pablo Neira AyusoReplace nft_ctx_init_from_setattr() by nft_table_lookup().
This patch also disentangles nf_tables_delset() where NFTA_SET_TABLE is required while nft_ctx_init_from_setattr() allows it to be optional.
From the nf_tables_delset() path, this also allows to set up the context structure when it is needed.
Removing this helper function saves us 14 LoC, so it is not helping to consolidate code.
---
v3: use u8 family instead of in family in nf_tables.
net/netfilter/nf_tables_api.c | 64 ++++++++++++++--------------------- 1 file changed, 25 insertions(+), 39 deletions(-)
net/netfilter/nf_tables_api.c +25 -39
@@ -3639,30 +3639,6 @@ static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED }, }; -static int nft_ctx_init_from_setattr (struct nft_ctx *ctx,struct net *net, -const struct sk_buff *skb, -const struct nlmsghdr *nlh, -const struct nlattr *const nla[], -struct netlink_ext_ack *extack, -u8 genmask,u32 nlpid) -{ -const struct nfgenmsg *nfmsg =nlmsg_data (nlh); -int family = nfmsg->nfgen_family; -struct nft_table *table =NULL ; - -if (nla[NFTA_SET_TABLE ] !=NULL ) { - table =nft_table_lookup (net, nla[NFTA_SET_TABLE ], family, - genmask, nlpid); -if (IS_ERR (table)) { -NL_SET_BAD_ATTR (extack, nla[NFTA_SET_TABLE ]); -return PTR_ERR (table); - } - } - -nft_ctx_init (ctx, net, skb, nlh, family, table,NULL , nla); -return 0 ; -} -static struct nft_set *nft_set_lookup (const struct nft_table *table,const struct nlattr *nla,u8 genmask) {
@@ -4044,17 +4020,24 @@ static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info, {struct netlink_ext_ack *extack = info->extack;u8 genmask =nft_genmask_cur (info->net); +u8 family = info->nfmsg->nfgen_family; +struct nft_table *table =NULL ;struct net *net = info->net;const struct nft_set *set;struct sk_buff *skb2;struct nft_ctx ctx;int err; -/* Verify existence before starting dump */ - err =nft_ctx_init_from_setattr (&ctx, net, skb, info->nlh, nla, extack, - genmask,0 ); -if (err <0 ) -return err; +if (nla[NFTA_SET_TABLE ]) { + table =nft_table_lookup (net, nla[NFTA_SET_TABLE ], family, + genmask,0 ); +if (IS_ERR (table)) { +NL_SET_BAD_ATTR (extack, nla[NFTA_SET_TABLE ]); +return PTR_ERR (table); + } + } + +nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla);if (info->nlh->nlmsg_flags &NLM_F_DUMP ) { struct netlink_dump_control c = {
@@ -4074,7 +4057,7 @@ static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info,if (!nla[NFTA_SET_TABLE ])return -EINVAL ; - set =nft_set_lookup (ctx.table, nla[NFTA_SET_NAME ], genmask); + set =nft_set_lookup (table, nla[NFTA_SET_NAME ], genmask);if (IS_ERR (set))return PTR_ERR (set);
@@ -4467,28 +4450,29 @@ static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info, {struct netlink_ext_ack *extack = info->extack;u8 genmask =nft_genmask_next (info->net); +u8 family = info->nfmsg->nfgen_family;struct net *net = info->net;const struct nlattr *attr; +struct nft_table *table;struct nft_set *set;struct nft_ctx ctx; -int err;if (info->nfmsg->nfgen_family ==NFPROTO_UNSPEC )return -EAFNOSUPPORT ; -if (nla[NFTA_SET_TABLE ] ==NULL ) -return -EINVAL ; - err =nft_ctx_init_from_setattr (&ctx, net, skb, info->nlh, nla, extack, - genmask,NETLINK_CB (skb).portid); -if (err <0 ) -return err; + table =nft_table_lookup (net, nla[NFTA_SET_TABLE ], family, + genmask,NETLINK_CB (skb).portid); +if (IS_ERR (table)) { +NL_SET_BAD_ATTR (extack, nla[NFTA_SET_TABLE ]); +return PTR_ERR (table); + }if (nla[NFTA_SET_HANDLE ]) { attr = nla[NFTA_SET_HANDLE ]; - set =nft_set_lookup_byhandle (ctx.table, attr, genmask); + set =nft_set_lookup_byhandle (table, attr, genmask); }else { attr = nla[NFTA_SET_NAME ]; - set =nft_set_lookup (ctx.table, attr, genmask); + set =nft_set_lookup (table, attr, genmask); }if (IS_ERR (set)) {
@@ -4502,6 +4486,8 @@ static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info,return -EBUSY ; } +nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla); +return nft_delset (&ctx, set); }
Signature · 1 line
2.30.2