netfilter: nftables: add helper function to set the base sequence number
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/2 netfilter: nftables: add helper function to set the base sequence number
This patch adds a helper function to calculate the base sequence number field that is stored in the nfnetlink header. Use the helper function whenever possible.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- net/netfilter/nf_tables_api.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-)
net/netfilter/nf_tables_api.c +14 -9
@@ -652,6 +652,11 @@ nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla,return ERR_PTR (-ENOENT ); } +static __be16 nft_base_seq (const struct net *net) +{ +return htons (net->nft.base_seq &0xffff ); +} +static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX +1 ] = { [NFTA_TABLE_NAME ] = { .type =NLA_STRING , .len =NFT_TABLE_MAXNAMELEN -1 },
@@ -676,7 +681,7 @@ static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, nfmsg =nlmsg_data (nlh); nfmsg->nfgen_family = family; nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (net->nft.base_seq &0xffff ); + nfmsg->res_id =nft_base_seq (net); if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
@@ -1448,7 +1453,7 @@ static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, nfmsg =nlmsg_data (nlh); nfmsg->nfgen_family = family; nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (net->nft.base_seq &0xffff ); + nfmsg->res_id =nft_base_seq (net);if (nla_put_string (skb,NFTA_CHAIN_TABLE , table->name)) goto nla_put_failure;
@@ -2819,7 +2824,7 @@ static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, nfmsg =nlmsg_data (nlh); nfmsg->nfgen_family = family; nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (net->nft.base_seq &0xffff ); + nfmsg->res_id =nft_base_seq (net);if (nla_put_string (skb,NFTA_RULE_TABLE , table->name)) goto nla_put_failure;
@@ -3819,7 +3824,7 @@ static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, nfmsg =nlmsg_data (nlh); nfmsg->nfgen_family = ctx->family; nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (ctx->net->nft.base_seq &0xffff ); + nfmsg->res_id =nft_base_seq (ctx->net);if (nla_put_string (skb,NFTA_SET_TABLE , ctx->table->name)) goto nla_put_failure;
@@ -4842,7 +4847,7 @@ static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) nfmsg =nlmsg_data (nlh); nfmsg->nfgen_family = table->family; nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (net->nft.base_seq &0xffff ); + nfmsg->res_id =nft_base_seq (net);if (nla_put_string (skb,NFTA_SET_ELEM_LIST_TABLE , table->name)) goto nla_put_failure;
@@ -4914,7 +4919,7 @@ static int nf_tables_fill_setelem_info(struct sk_buff *skb, nfmsg =nlmsg_data (nlh); nfmsg->nfgen_family = ctx->family; nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (ctx->net->nft.base_seq &0xffff ); + nfmsg->res_id =nft_base_seq (ctx->net);if (nla_put_string (skb,NFTA_SET_TABLE , ctx->table->name)) goto nla_put_failure;
@@ -6256,7 +6261,7 @@ static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, nfmsg =nlmsg_data (nlh); nfmsg->nfgen_family = family; nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (net->nft.base_seq &0xffff ); + nfmsg->res_id =nft_base_seq (net); if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
@@ -7170,7 +7175,7 @@ static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, nfmsg =nlmsg_data (nlh); nfmsg->nfgen_family = family; nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (net->nft.base_seq &0xffff ); + nfmsg->res_id =nft_base_seq (net); if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
@@ -7427,7 +7432,7 @@ static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, nfmsg =nlmsg_data (nlh); nfmsg->nfgen_family =AF_UNSPEC ; nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (net->nft.base_seq &0xffff ); + nfmsg->res_id =nft_base_seq (net); if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)) || nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/2 netfilter: add helper function to set up the nfnetlink header and use it
This patch adds a helper function to set up the netlink and nfnetlink headers. Update existing codebase to use it.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- include/linux/netfilter/nfnetlink.h | 27 +++++++ net/netfilter/ipset/ip_set_core.c | 17 +---- net/netfilter/nf_conntrack_netlink.c | 77 ++++++-------------- net/netfilter/nf_tables_api.c | 102 +++++++-------------------- net/netfilter/nf_tables_trace.c | 9 +-- net/netfilter/nfnetlink_acct.c | 11 +-- net/netfilter/nfnetlink_cthelper.c | 11 +-- net/netfilter/nfnetlink_cttimeout.c | 21 ++---- net/netfilter/nfnetlink_log.c | 11 +-- net/netfilter/nfnetlink_queue.c | 12 ++-- net/netfilter/nft_compat.c | 11 +-- 11 files changed, 102 insertions(+), 207 deletions(-)
include/linux/netfilter/nfnetlink.h +27 -0
@@ -57,6 +57,33 @@ static inline u16 nfnl_msg_type(u8 subsys, u8 msg_type)return subsys <<8 | msg_type; } +static inline void nfnl_fill_hdr (struct nlmsghdr *nlh,u8 family,u8 version, +__be16 res_id) +{ +struct nfgenmsg *nfmsg; + + nfmsg =nlmsg_data (nlh); + nfmsg->nfgen_family = family; + nfmsg->version = version; + nfmsg->res_id = res_id; +} + +static inline struct nlmsghdr *nfnl_msg_put (struct sk_buff *skb,u32 portid, +u32 seq,int type,int flags, +u8 family,u8 version, +__be16 res_id) +{ +struct nlmsghdr *nlh; + + nlh =nlmsg_put (skb, portid, seq, type,sizeof (struct nfgenmsg ), flags); +if (!nlh) +return NULL ; + +nfnl_fill_hdr (nlh, family, version, res_id); + +return nlh; +} +void nfnl_lock (__u8 subsys_id);void nfnl_unlock (__u8 subsys_id);#ifdef CONFIG_PROVE_LOCKING
net/netfilter/ipset/ip_set_core.c +3 -14
@@ -963,20 +963,9 @@ static struct nlmsghdr * start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags, enum ipset_cmd cmd) { - struct nlmsghdr *nlh; -struct nfgenmsg *nfmsg; - - nlh =nlmsg_put (skb, portid, seq,nfnl_msg_type (NFNL_SUBSYS_IPSET , cmd), -sizeof (*nfmsg), flags); -if (!nlh) -return NULL ; - - nfmsg =nlmsg_data (nlh); - nfmsg->nfgen_family =NFPROTO_IPV4 ; - nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =0 ; - -return nlh; + return nfnl_msg_put(skb, portid, seq, + nfnl_msg_type(NFNL_SUBSYS_IPSET ,cmd ), + flags, NFPROTO_IPV4, NFNETLINK_V0, 0); }/* Create a set */
net/netfilter/nf_conntrack_netlink.c +21 -56
166 lines not on this page · raw message
net/netfilter/nf_tables_api.c +27 -75
209 lines not on this page · raw message
net/netfilter/nf_tables_trace.c +2 -7
@@ -183,7 +183,6 @@ static bool nft_trace_have_verdict_chain(struct nft_traceinfo *info)void nft_trace_notify (struct nft_traceinfo *info) {const struct nft_pktinfo *pkt = info->pkt; -struct nfgenmsg *nfmsg;struct nlmsghdr *nlh;struct sk_buff *skb;unsigned int size;
@@ -219,15 +218,11 @@ void nft_trace_notify(struct nft_traceinfo *info)return ; event =nfnl_msg_type (NFNL_SUBSYS_NFTABLES ,NFT_MSG_TRACE ); - nlh =nlmsg_put (skb,0 ,0 , event,sizeof (struct nfgenmsg ),0 ); + nlh =nfnl_msg_put (skb,0 ,0 , event,0 , info->basechain->type->family, +NFNETLINK_V0 ,0 );if (!nlh) goto nla_put_failure; - nfmsg =nlmsg_data (nlh); - nfmsg->nfgen_family = info->basechain->type->family; - nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =0 ; -if (nla_put_be32 (skb,NFTA_TRACE_NFPROTO ,htonl (nft_pf (pkt)))) goto nla_put_failure;
net/netfilter/nfnetlink_acct.c +3 -8
@@ -145,21 +145,16 @@ nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, int event, struct nf_acct *acct) { struct nlmsghdr *nlh; -struct nfgenmsg *nfmsg;unsigned int flags = portid ?NLM_F_MULTI :0 ;u64 pkts, bytes;u32 old_flags; event =nfnl_msg_type (NFNL_SUBSYS_ACCT , event); - nlh =nlmsg_put (skb, portid, seq, event,sizeof (*nfmsg), flags); -if (nlh ==NULL ) + nlh =nfnl_msg_put (skb, portid, seq, event, flags,AF_UNSPEC , +NFNETLINK_V0 ,0 ); +if (!nlh) goto nlmsg_failure; - nfmsg =nlmsg_data (nlh); - nfmsg->nfgen_family =AF_UNSPEC ; - nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =0 ; -if (nla_put_string (skb,NFACCT_NAME , acct->name)) goto nla_put_failure;
net/netfilter/nfnetlink_cthelper.c +3 -8
@@ -526,20 +526,15 @@ nfnl_cthelper_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, int event, struct nf_conntrack_helper *helper) { struct nlmsghdr *nlh; -struct nfgenmsg *nfmsg;unsigned int flags = portid ?NLM_F_MULTI :0 ;int status; event =nfnl_msg_type (NFNL_SUBSYS_CTHELPER , event); - nlh =nlmsg_put (skb, portid, seq, event,sizeof (*nfmsg), flags); -if (nlh ==NULL ) + nlh =nfnl_msg_put (skb, portid, seq, event, flags,AF_UNSPEC , +NFNETLINK_V0 ,0 ); +if (!nlh) goto nlmsg_failure; - nfmsg =nlmsg_data (nlh); - nfmsg->nfgen_family =AF_UNSPEC ; - nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =0 ; -if (nla_put_string (skb,NFCTH_NAME , helper->name)) goto nla_put_failure;
net/netfilter/nfnetlink_cttimeout.c +6 -15
@@ -167,15 +167,11 @@ ctnl_timeout_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,int ret; event =nfnl_msg_type (NFNL_SUBSYS_CTNETLINK_TIMEOUT , event); - nlh =nlmsg_put (skb, portid, seq, event,sizeof (*nfmsg), flags); -if (nlh ==NULL ) + nlh =nfnl_msg_put (skb, portid, seq, event, flags,AF_UNSPEC , +NFNETLINK_V0 ,0 ); +if (!nlh) goto nlmsg_failure; - nfmsg =nlmsg_data (nlh); - nfmsg->nfgen_family =AF_UNSPEC ; - nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =0 ; - if (nla_put_string(skb, CTA_TIMEOUT_NAME, timeout->name) || nla_put_be16(skb, CTA_TIMEOUT_L3PROTO, htons(timeout->timeout.l3num)) ||
@@ -382,21 +378,16 @@ cttimeout_default_fill_info(struct net *net, struct sk_buff *skb, u32 portid, const unsigned int *timeouts) {struct nlmsghdr *nlh; -struct nfgenmsg *nfmsg;unsigned int flags = portid ?NLM_F_MULTI :0 ;struct nlattr *nest_parms;int ret; event =nfnl_msg_type (NFNL_SUBSYS_CTNETLINK_TIMEOUT , event); - nlh =nlmsg_put (skb, portid, seq, event,sizeof (*nfmsg), flags); -if (nlh ==NULL ) + nlh =nfnl_msg_put (skb, portid, seq, event, flags,AF_UNSPEC , +NFNETLINK_V0 ,0 ); +if (!nlh) goto nlmsg_failure; - nfmsg =nlmsg_data (nlh); - nfmsg->nfgen_family =AF_UNSPEC ; - nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =0 ; -if (nla_put_be16 (skb,CTA_TIMEOUT_L3PROTO ,htons (l3num)) ||nla_put_u8 (skb,CTA_TIMEOUT_L4PROTO , l4proto->l4proto)) goto nla_put_failure;
net/netfilter/nfnetlink_log.c +3 -8
@@ -456,20 +456,15 @@ __build_packet_message(struct nfnl_log_net *log, {struct nfulnl_msg_packet_hdr pmsg;struct nlmsghdr *nlh; -struct nfgenmsg *nfmsg;sk_buff_data_t old_tail = inst->skb->tail;struct sock *sk;const unsigned char *hwhdrp; - nlh =nlmsg_put (inst->skb,0 ,0 , -nfnl_msg_type (NFNL_SUBSYS_ULOG ,NFULNL_MSG_PACKET ), -sizeof (struct nfgenmsg ),0 ); + nlh =nfnl_msg_put (inst->skb,0 ,0 , +nfnl_msg_type (NFNL_SUBSYS_ULOG ,NFULNL_MSG_PACKET ), +0 , pf,NFNETLINK_V0 ,htons (inst->group_num));if (!nlh)return -1 ; - nfmsg =nlmsg_data (nlh); - nfmsg->nfgen_family = pf; - nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (inst->group_num);memset (&pmsg,0 ,sizeof (pmsg)); pmsg.hw_protocol = skb->protocol;
net/netfilter/nfnetlink_queue.c +4 -8
@@ -383,7 +383,6 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,struct nlattr *nla;struct nfqnl_msg_packet_hdr *pmsg;struct nlmsghdr *nlh; -struct nfgenmsg *nfmsg;struct sk_buff *entskb = entry->skb;struct net_device *indev;struct net_device *outdev;
@@ -471,18 +470,15 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue, goto nlmsg_failure; } - nlh =nlmsg_put (skb,0 ,0 , -nfnl_msg_type (NFNL_SUBSYS_QUEUE ,NFQNL_MSG_PACKET ), -sizeof (struct nfgenmsg ),0 ); + nlh =nfnl_msg_put (skb,0 ,0 , +nfnl_msg_type (NFNL_SUBSYS_QUEUE ,NFQNL_MSG_PACKET ), +0 , entry->state.pf,NFNETLINK_V0 , +htons (queue->queue_num));if (!nlh) {skb_tx_error (entskb);kfree_skb (skb); goto nlmsg_failure; } - nfmsg =nlmsg_data (nlh); - nfmsg->nfgen_family = entry->state.pf; - nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =htons (queue->queue_num); nla =__nla_reserve (skb,NFQA_PACKET_HDR ,sizeof (*pmsg)); pmsg =nla_data (nla);
net/netfilter/nft_compat.c +3 -8
@@ -591,19 +591,14 @@ nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, int rev, int target) {struct nlmsghdr *nlh; -struct nfgenmsg *nfmsg;unsigned int flags = portid ?NLM_F_MULTI :0 ; event =nfnl_msg_type (NFNL_SUBSYS_NFT_COMPAT , event); - nlh =nlmsg_put (skb, portid, seq, event,sizeof (*nfmsg), flags); -if (nlh ==NULL ) + nlh =nfnl_msg_put (skb, portid, seq, event, flags, family, +NFNETLINK_V0 ,0 ); +if (!nlh) goto nlmsg_failure; - nfmsg =nlmsg_data (nlh); - nfmsg->nfgen_family = family; - nfmsg->version =NFNETLINK_V0 ; - nfmsg->res_id =0 ; -if (nla_put_string (skb,NFTA_COMPAT_NAME , name) ||nla_put_be32 (skb,NFTA_COMPAT_REV ,htonl (rev)) ||nla_put_be32 (skb,NFTA_COMPAT_TYPE ,htonl (target)))
Signature · 1 line
2.30.2