Skip to contentSkip to conversation

netfilter: nf_tables: Introduce stateful object update operation

PATCH In review Fernando Fernandez Mancera 2 participants 3 messages Aug 19, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nf_tables: Introduce stateful object update operation

This is a WIP patch version. I still having some issues in userspace but I would like to get feedback about the kernel-side patch. Thanks!

---
 include/net/netfilter/nf_tables.h |  6 +++
 net/netfilter/nf_tables_api.c     | 73 ++++++++++++++++++++++++++++---
 2 files changed, 72 insertions(+), 7 deletions(-)
include/net/netfilter/nf_tables.h +6 -0
index dc301e3d6739..dc4e32040ea9 100644
@@ -1123,6 +1123,9 @@ struct nft_object_ops {
 	int				(*dump)(struct sk_buff *skb,
 						struct nft_object *obj,
 						bool reset);
+	int				(*update)(const struct nft_ctx *ctx,
+						  const struct nlattr *const tb[],
+						  struct nft_object *obj);
 	const struct nft_object_type	*type;
 };
 
@@ -1405,10 +1408,13 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	bool				update;
 };
 
 #define nft_trans_obj(trans)	\
 	(((struct nft_trans_obj *)trans->data)->obj)
+#define nft_trans_obj_update(trans)	\
+	(((struct nft_trans_obj *)trans->data)->update)
 
 struct nft_trans_flowtable {
 	struct nft_flowtable		*flowtable;
net/netfilter/nf_tables_api.c +66 -7

97 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 50 lines · Fernando Fernandez Mancera

This is a WIP patch version. I still having some issues in userspace but I would like to get feedback about the kernel-side patch. Thanks!

---
 include/net/netfilter/nf_tables.h |  6 +++
 net/netfilter/nf_tables_api.c     | 73 ++++++++++++++++++++++++++++---
 2 files changed, 72 insertions(+), 7 deletions(-)
include/net/netfilter/nf_tables.h +6 -0
index dc301e3d6739..dc4e32040ea9 100644
@@ -1123,6 +1123,9 @@ struct nft_object_ops {
 	int				(*dump)(struct sk_buff *skb,
 						struct nft_object *obj,
 						bool reset);
+	int				(*update)(const struct nft_ctx *ctx,
+						  const struct nlattr *const tb[],
+						  struct nft_object *obj);
 	const struct nft_object_type	*type;
 };
 
@@ -1405,10 +1408,13 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	bool				update;
 };
 
 #define nft_trans_obj(trans)	\
 	(((struct nft_trans_obj *)trans->data)->obj)
+#define nft_trans_obj_update(trans)	\
+	(((struct nft_trans_obj *)trans->data)->update)
 
 struct nft_trans_flowtable {
 	struct nft_flowtable		*flowtable;
net/netfilter/nf_tables_api.c +5 -0
index fe3b7b0c6c66..d7b94904599c 100644
@@ -5122,6 +5122,48 @@ nft_obj_type_get(struct net *net, u32 objtype)
 	return ERR_PTR(-ENOENT);
 }
 
+static int nf_tables_updobj(const struct nft_ctx *ctx,
+			    const struct nft_object_type *type,
+			    const struct nlattr *attr,
+			    struct nft_object *obj)
+{

net/netfilter/nf_tables_api.c nft_obj_type_get(struct net *net, u32 objtype) · line 5139

Quoted 10 lines
net/netfilter/nf_tables_api.c +10 -0
+	struct nft_trans *trans;
+	struct nlattr **tb;
+	int err = -ENOMEM;
+
+	trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
+				sizeof(struct nft_trans_obj));
+	if (!trans)
+		return -ENOMEM;
+
+	tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);

You can use kcalloc here and then remove the memset().

net/netfilter/nf_tables_api.c nft_obj_type_get(struct net *net, u32 objtype) · line 5154

Quoted 3 lines
net/netfilter/nf_tables_api.c +3 -0
+	err = obj->ops->update(ctx, (const struct nlattr * const *)tb, obj);
+	if (err < 0)
+		goto err;

This looks wrong, see below.

Quoted 13 lines
net/netfilter/nf_tables_api.c +7 -1
@@ -5161,7 +5203,13 @@ static int nf_tables_newobj(struct net *net, struct sock *nlsk,
 			NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
 			return -EEXIST;
 		}
-		return 0;
+		if (nlh->nlmsg_flags & NLM_F_REPLACE)
+			return -EOPNOTSUPP;
+
+		type = nft_obj_type_get(net, objtype);
+		nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
+
+		return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj);
 	}

net/netfilter/nf_tables_api.c static int nf_tables_newobj(struct net *net, struct sock *nlsk,

Quoted 10 lines
net/netfilter/nf_tables_api.c +4 -4
 
 		case NFT_MSG_NEWOBJ:
-			nft_clear(net, nft_trans_obj(trans));
-			nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
-					     NFT_MSG_NEWOBJ);
-			nft_trans_destroy(trans);
+			if (nft_trans_obj_update(trans)) {
+				nf_tables_obj_notify(&trans->ctx,
+						     nft_trans_obj(trans),
+						     NFT_MSG_NEWOBJ);

I would have expected the ->update() here, when committing the batch. Under what conditions can an update() fail?

#2 lore raw

Hi Florian,

Quoted 90 lines · Florian Westphal
Quoted 1 line · Fernando Fernandez Mancera

This is a WIP patch version. I still having some issues in userspace

but I

Quoted 6 lines

would like to get feedback about the kernel-side patch. Thanks!

---
 include/net/netfilter/nf_tables.h |  6 +++
 net/netfilter/nf_tables_api.c     | 73

++++++++++++++++++++++++++++---

Review

Quoted 3 lines
 2 files changed, 72 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/include/net/netfilter/nf_tables.h

Review

Quoted 28 lines

index dc301e3d6739..dc4e32040ea9 100644

include/net/netfilter/nf_tables.h +6 -0
@@ -1123,6 +1123,9 @@ struct nft_object_ops {
 	int				(*dump)(struct sk_buff *skb,
 						struct nft_object *obj,
 						bool reset);
+	int				(*update)(const struct nft_ctx *ctx,
+						  const struct nlattr *const tb[],
+						  struct nft_object *obj);
 	const struct nft_object_type	*type;
 };
 
@@ -1405,10 +1408,13 @@ struct nft_trans_elem {
 
 struct nft_trans_obj {
 	struct nft_object		*obj;
+	bool				update;
 };
 
 #define nft_trans_obj(trans)	\
 	(((struct nft_trans_obj *)trans->data)->obj)
+#define nft_trans_obj_update(trans)	\
+	(((struct nft_trans_obj *)trans->data)->update)
 
 struct nft_trans_flowtable {
 	struct nft_flowtable		*flowtable;
(quoted diff) +0 -0

b/net/netfilter/nf_tables_api.c

Quoted 12 lines

index fe3b7b0c6c66..d7b94904599c 100644

net/netfilter/nf_tables_api.c +5 -0
@@ -5122,6 +5122,48 @@ nft_obj_type_get(struct net *net, u32 objtype)
 	return ERR_PTR(-ENOENT);
 }
 
+static int nf_tables_updobj(const struct nft_ctx *ctx,
+			    const struct nft_object_type *type,
+			    const struct nlattr *attr,
+			    struct nft_object *obj)
+{

net/netfilter/nf_tables_api.c nft_obj_type_get(struct net *net, u32 objtype) · line 5139

Quoted 10 lines
net/netfilter/nf_tables_api.c +10 -0
+	struct nft_trans *trans;
+	struct nlattr **tb;
+	int err = -ENOMEM;
+
+	trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
+				sizeof(struct nft_trans_obj));
+	if (!trans)
+		return -ENOMEM;
+
+	tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);

You can use kcalloc here and then remove the memset(). p

net/netfilter/nf_tables_api.c nft_obj_type_get(struct net *net, u32 objtype) · line 5140

Quoted 1 line
net/netfilter/nf_tables_api.c +1 -0
+	err = obj->ops->update(ctx, (const struct nlattr * const *)tb,

obj);

net/netfilter/nf_tables_api.c nft_obj_type_get(struct net *net, u32 objtype) · line 5142

Quoted 2 lines
net/netfilter/nf_tables_api.c +2 -0
+	if (err < 0)
+		goto err;

This looks wrong, see below.

net/netfilter/nf_tables_api.c static int nf_tables_newobj(struct net *net,

Quoted 1 line
net/netfilter/nf_tables_api.c +0 -0
@@ -5161,7 +5203,13 @@ static int nf_tables_newobj(struct net *net,

struct sock *nlsk,

Quoted 13 lines
net/netfilter/nf_tables_api.c +7 -1
 			NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
 			return -EEXIST;
 		}
-		return 0;
+		if (nlh->nlmsg_flags & NLM_F_REPLACE)
+			return -EOPNOTSUPP;
+
+		type = nft_obj_type_get(net, objtype);
+		nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
+
+		return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj);
 	}
 
Quoted 12 lines

net/netfilter/nf_tables_api.c static int nf_tables_commit(struct net *net, struct sk_buff *skb) · line 6840

Quoted 9 lines
net/netfilter/nf_tables_api.c +4 -4
 		case NFT_MSG_NEWOBJ:
-			nft_clear(net, nft_trans_obj(trans));
-			nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
-					     NFT_MSG_NEWOBJ);
-			nft_trans_destroy(trans);
+			if (nft_trans_obj_update(trans)) {
+				nf_tables_obj_notify(&trans->ctx,
+						     nft_trans_obj(trans),
+						     NFT_MSG_NEWOBJ);

I would have expected the ->update() here, when committing the batch. Under what conditions can an update() fail?

It depends on the object type. In the quota case it can fail if the quota have invalid values.