Skip to contentSkip to conversation

netfilter: nf_tables: Return info of added rules back to user space

PATCH In review Phil Sutter 2 participants 3 messages May 4, 2017

Subsystems (2)
lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 netfilter: nf_tables: Return info of added rules back to user space

This allows user space to reliably match kernel generated handles with added rules for reference.

---
 net/netfilter/nf_tables_api.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +18 -1
index 1c6482d2c4dcf..71bce5d024409 100644
@@ -2142,6 +2142,7 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,
 	struct nft_userdata *udata;
 	struct nft_trans *trans = NULL;
 	struct nft_expr *expr;
+	struct sk_buff *skb2;
 	struct nft_ctx ctx;
 	struct nlattr *tmp;
 	unsigned int size, i, n, ulen = 0, usize = 0;
@@ -2281,8 +2282,24 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,
 		goto err3;
 	}
 	chain->use++;
-	return 0;
 
+	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
+	if (!skb2) {
+		err = -ENOMEM;
+		goto err4;
+	}
+	err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
+				       nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
+				       nfmsg->nfgen_family, table, chain, rule);
+	if (err < 0)
+		goto err5;
+
+	return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
+
+err5:
+	kfree_skb(skb2);
+err4:
+	chain->use--;
 err3:
 	list_del_rcu(&rule->list);
 err2:
Signature · 1 line
2.11.0
#1 lore raw
Quoted 28 lines · Phil Sutter

This allows user space to reliably match kernel generated handles with added rules for reference.

---
 net/netfilter/nf_tables_api.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +3 -1
index 1c6482d2c4dcf..71bce5d024409 100644
@@ -2142,6 +2142,7 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,
 	struct nft_userdata *udata;
 	struct nft_trans *trans = NULL;
 	struct nft_expr *expr;
+	struct sk_buff *skb2;
 	struct nft_ctx ctx;
 	struct nlattr *tmp;
 	unsigned int size, i, n, ulen = 0, usize = 0;
@@ -2281,8 +2282,24 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,
 		goto err3;
 	}
 	chain->use++;
-	return 0;
 
+	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
+	if (!skb2) {

net/netfilter/nf_tables_api.c static int nf_tables_newrule(struct net *net, struct sock *nlsk, · line 2297

Quoted 10 lines
net/netfilter/nf_tables_api.c +10 -0
+		err = -ENOMEM;
+		goto err4;
+	}
+	err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
+				       nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
+				       nfmsg->nfgen_family, table, chain, rule);
+	if (err < 0)
+		goto err5;
+
+	return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);

You can achieve this already via NLM_F_ECHO.

author reviewer
#2 lore raw
Quoted 27 lines · Pablo Neira Ayuso
Quoted 26 lines · Phil Sutter

This allows user space to reliably match kernel generated handles with added rules for reference.

---
 net/netfilter/nf_tables_api.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +1 -1
index 1c6482d2c4dcf..71bce5d024409 100644
@@ -2142,6 +2142,7 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,
 	struct nft_userdata *udata;
 	struct nft_trans *trans = NULL;
 	struct nft_expr *expr;
+	struct sk_buff *skb2;
 	struct nft_ctx ctx;
 	struct nlattr *tmp;
 	unsigned int size, i, n, ulen = 0, usize = 0;
@@ -2281,8 +2282,24 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,
 		goto err3;
 	}
 	chain->use++;
-	return 0;
 
Quoted 14 lines

net/netfilter/nf_tables_api.c static int nf_tables_newrule(struct net *net, struct sock *nlsk, · line 2297

Quoted 12 lines
net/netfilter/nf_tables_api.c +12 -0
+	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
+	if (!skb2) {
+		err = -ENOMEM;
+		goto err4;
+	}
+	err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
+				       nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
+				       nfmsg->nfgen_family, table, chain, rule);
+	if (err < 0)
+		goto err5;
+
+	return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);

You can achieve this already via NLM_F_ECHO.

Oh, thanks for the pointer!

Cheers, Phil