Skip to contentSkip to conversation

netfilter: nf_tables: allocate table handle

PATCH Superseded Harsha Sharma 2 participants 4 messages Dec 23, 2017 – Dec 26, 2017

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nf_tables: allocate table handle

This patch adds code to allocate 'handle' in tables, which allow us to uniquely identify a table.

---
 include/net/netfilter/nf_tables.h        |  2 ++
 include/uapi/linux/netfilter/nf_tables.h |  2 ++
 net/netfilter/nf_tables_api.c            | 12 +++++++++++-
 3 files changed, 15 insertions(+), 1 deletion(-)
include/net/netfilter/nf_tables.h +2 -0
index 01570a8f2982..bbc7bfcc1a57 100644
@@ -949,6 +949,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
  *	@sets: sets in the table
  *	@objects: stateful objects in the table
  *	@hgenerator: handle generator state
+ *	@handle: table handle
  *	@use: number of chain references to this table
  *	@flags: table flag (see enum nft_table_flags)
  *	@genmask: generation mask
@@ -960,6 +961,7 @@ struct nft_table {
 	struct list_head		sets;
 	struct list_head		objects;
 	u64				hgenerator;
+	u64 				handle:42;
 	u32				use;
 	u16				flags:14,
 					genmask:2;
include/uapi/linux/netfilter/nf_tables.h +2 -0
index a3ee277b17a1..a6ab9a962eae 100644
@@ -168,6 +168,8 @@ enum nft_table_attributes {
 	NFTA_TABLE_NAME,
 	NFTA_TABLE_FLAGS,
 	NFTA_TABLE_USE,
+	NFTA_TABLE_HANDLE,
+	NFTA_TABLE_PAD,
 	__NFTA_TABLE_MAX
 };
 #define NFTA_TABLE_MAX		(__NFTA_TABLE_MAX - 1)
net/netfilter/nf_tables_api.c +11 -1
index d8327b43e4dc..4a4e247e6028 100644
@@ -22,6 +22,7 @@
 #include <net/net_namespace.h>
 #include <net/sock.h>
 
+static u64 table_handle;
 static LIST_HEAD(nf_tables_expressions);
 static LIST_HEAD(nf_tables_objects);
 
@@ -430,6 +431,7 @@ static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
 	[NFTA_TABLE_NAME]	= { .type = NLA_STRING,
 				    .len = NFT_TABLE_MAXNAMELEN - 1 },
 	[NFTA_TABLE_FLAGS]	= { .type = NLA_U32 },
+	[NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
 };
 
 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
@@ -451,7 +453,9 @@ static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
 
 	if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
 	    nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
-	    nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
+	    nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
+	    nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
+			 NFTA_TABLE_PAD))
 		goto nla_put_failure;
 
 	nlmsg_end(skb, nlh);
@@ -734,6 +738,12 @@ static int nf_tables_newtable(struct net *net, struct sock *nlsk,
 	INIT_LIST_HEAD(&table->sets);
 	INIT_LIST_HEAD(&table->objects);
 	table->flags = flags;
+	if (nla[NFTA_TABLE_HANDLE]) {
+		table->handle = be64_to_cpu(nla_get_be64(nla[NFTA_TABLE_HANDLE]));
+	} else {
+		table_handle++;
+		table->handle = table_handle;
+	}
 
 	nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
 	err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
Signature · 1 line
2.11.0
#1 lore raw

Hi Harsha,

Quoted 48 lines · Harsha Sharma

This patch adds code to allocate 'handle' in tables, which allow us to uniquely identify a table.

---
 include/net/netfilter/nf_tables.h        |  2 ++
 include/uapi/linux/netfilter/nf_tables.h |  2 ++
 net/netfilter/nf_tables_api.c            | 12 +++++++++++-
 3 files changed, 15 insertions(+), 1 deletion(-)
include/net/netfilter/nf_tables.h +2 -0
index 01570a8f2982..bbc7bfcc1a57 100644
@@ -949,6 +949,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
  *	@sets: sets in the table
  *	@objects: stateful objects in the table
  *	@hgenerator: handle generator state
+ *	@handle: table handle
  *	@use: number of chain references to this table
  *	@flags: table flag (see enum nft_table_flags)
  *	@genmask: generation mask
@@ -960,6 +961,7 @@ struct nft_table {
 	struct list_head		sets;
 	struct list_head		objects;
 	u64				hgenerator;
+	u64 				handle:42;
 	u32				use;
 	u16				flags:14,
 					genmask:2;
include/uapi/linux/netfilter/nf_tables.h +2 -0
index a3ee277b17a1..a6ab9a962eae 100644
@@ -168,6 +168,8 @@ enum nft_table_attributes {
 	NFTA_TABLE_NAME,
 	NFTA_TABLE_FLAGS,
 	NFTA_TABLE_USE,
+	NFTA_TABLE_HANDLE,
+	NFTA_TABLE_PAD,
 	__NFTA_TABLE_MAX
 };
 #define NFTA_TABLE_MAX		(__NFTA_TABLE_MAX - 1)
net/netfilter/nf_tables_api.c +0 -0
index d8327b43e4dc..4a4e247e6028 100644

net/netfilter/nf_tables_api.c line 25

Quoted 4 lines
net/netfilter/nf_tables_api.c +1 -0
@@ -22,6 +22,7 @@
 #include <net/net_namespace.h>
 #include <net/sock.h>
 
+static u64 table_handle;

You need to place this handle number in struct nft_table.

net/netfilter/nf_tables_api.c static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { · line 434

Quoted 8 lines
net/netfilter/nf_tables_api.c +1 -0
 static LIST_HEAD(nf_tables_expressions);
 static LIST_HEAD(nf_tables_objects);
 
@@ -430,6 +431,7 @@ static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
 	[NFTA_TABLE_NAME]	= { .type = NLA_STRING,
 				    .len = NFT_TABLE_MAXNAMELEN - 1 },
 	[NFTA_TABLE_FLAGS]	= { .type = NLA_U32 },
+	[NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
                          ^^^

missing tab indent here.

Quoted 15 lines
net/netfilter/nf_tables_api.c +3 -1
 };
 
 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
@@ -451,7 +453,9 @@ static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
 
 	if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
 	    nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
-	    nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
+	    nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
+	    nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
+			 NFTA_TABLE_PAD))
 		goto nla_put_failure;
 
 	nlmsg_end(skb, nlh);

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

Quoted 5 lines
net/netfilter/nf_tables_api.c +2 -0
@@ -734,6 +738,12 @@ static int nf_tables_newtable(struct net *net, struct sock *nlsk,
 	INIT_LIST_HEAD(&table->sets);
 	INIT_LIST_HEAD(&table->objects);
 	table->flags = flags;
+	if (nla[NFTA_TABLE_HANDLE]) {
+		table->handle = be64_to_cpu(nla_get_be64(nla[NFTA_TABLE_HANDLE]));

We don't allow to restore the handle, please remove this line.

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

Quoted 7 lines
net/netfilter/nf_tables_api.c +4 -0
+	} else {
+		table_handle++;
+		table->handle = table_handle;
+	}
 
 	nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
 	err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);

We need to implement deletion via handle too in nf_tables_deltable(), same thing for nf_table_gettable().

Thus, the unique handle becomes useful.

Thanks.

#2 lore raw
Quoted 54 lines · Pablo Neira Ayuso

Hi Harsha,

Quoted 51 lines · Harsha Sharma

This patch adds code to allocate 'handle' in tables, which allow us to uniquely identify a table.

---
 include/net/netfilter/nf_tables.h        |  2 ++
 include/uapi/linux/netfilter/nf_tables.h |  2 ++
 net/netfilter/nf_tables_api.c            | 12 +++++++++++-
 3 files changed, 15 insertions(+), 1 deletion(-)
include/net/netfilter/nf_tables.h +2 -0
index 01570a8f2982..bbc7bfcc1a57 100644
@@ -949,6 +949,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
  *   @sets: sets in the table
  *   @objects: stateful objects in the table
  *   @hgenerator: handle generator state
+ *   @handle: table handle
  *   @use: number of chain references to this table
  *   @flags: table flag (see enum nft_table_flags)
  *   @genmask: generation mask
@@ -960,6 +961,7 @@ struct nft_table {
      struct list_head                sets;
      struct list_head                objects;
      u64                             hgenerator;
+     u64                             handle:42;
      u32                             use;
      u16                             flags:14,
                                      genmask:2;
include/uapi/linux/netfilter/nf_tables.h +2 -0
index a3ee277b17a1..a6ab9a962eae 100644
@@ -168,6 +168,8 @@ enum nft_table_attributes {
      NFTA_TABLE_NAME,
      NFTA_TABLE_FLAGS,
      NFTA_TABLE_USE,
+     NFTA_TABLE_HANDLE,
+     NFTA_TABLE_PAD,
      __NFTA_TABLE_MAX
 };
 #define NFTA_TABLE_MAX               (__NFTA_TABLE_MAX - 1)
net/netfilter/nf_tables_api.c +0 -0
index d8327b43e4dc..4a4e247e6028 100644
@@ -22,6 +22,7 @@
 #include <net/net_namespace.h>
 #include <net/sock.h>
 
Quoted 4 lines

net/netfilter/nf_tables_api.c line 25

Quoted 1 line
net/netfilter/nf_tables_api.c +1 -0
+static u64 table_handle;

You need to place this handle number in struct nft_table.

This table_handle is passed to the struct nft_table in nf_tables_newtable().

Quoted 24 lines

net/netfilter/nf_tables_api.c static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { · line 434

Quoted 8 lines
net/netfilter/nf_tables_api.c +1 -0
 static LIST_HEAD(nf_tables_expressions);
 static LIST_HEAD(nf_tables_objects);
 
@@ -430,6 +431,7 @@ static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
      [NFTA_TABLE_NAME]       = { .type = NLA_STRING,
                                  .len = NFT_TABLE_MAXNAMELEN - 1 },
      [NFTA_TABLE_FLAGS]      = { .type = NLA_U32 },
+     [NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
                          ^^^

missing tab indent here.

Quoted 13 lines
net/netfilter/nf_tables_api.c +3 -1
 };
 
 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
@@ -451,7 +453,9 @@ static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
 
      if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
          nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
-         nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
+         nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
+         nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
+                      NFTA_TABLE_PAD))
              goto nla_put_failure;
 
Quoted 10 lines

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

Quoted 7 lines
net/netfilter/nf_tables_api.c +2 -0
      nlmsg_end(skb, nlh);
@@ -734,6 +738,12 @@ static int nf_tables_newtable(struct net *net, struct sock *nlsk,
      INIT_LIST_HEAD(&table->sets);
      INIT_LIST_HEAD(&table->objects);
      table->flags = flags;
+     if (nla[NFTA_TABLE_HANDLE]) {
+             table->handle = be64_to_cpu(nla_get_be64(nla[NFTA_TABLE_HANDLE]));

We don't allow to restore the handle, please remove this line.

OK. I'll remove this.

Quoted 5 lines
Quoted 5 lines
net/netfilter/nf_tables_api.c +4 -0
+     } else {
+             table_handle++;
+             table->handle = table_handle;
+     }
 
Quoted 8 lines

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

Quoted 2 lines
net/netfilter/nf_tables_api.c +0 -0
      nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
      err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);

We need to implement deletion via handle too in nf_tables_deltable(), same thing for nf_table_gettable().

Thus, the unique handle becomes useful.

OK. Other than these changes, can I work on implementing deletion via table handle ?
Thanks for the review .
Merry Christmas !!

Regards,
Harsha Sharma

Quoted 1 line

Thanks.

#3 lore raw
Quoted 105 lines · Harsha Sharma
Quoted 54 lines · Pablo Neira Ayuso

Hi Harsha,

Quoted 51 lines · Harsha Sharma

This patch adds code to allocate 'handle' in tables, which allow us to uniquely identify a table.

---
 include/net/netfilter/nf_tables.h        |  2 ++
 include/uapi/linux/netfilter/nf_tables.h |  2 ++
 net/netfilter/nf_tables_api.c            | 12 +++++++++++-
 3 files changed, 15 insertions(+), 1 deletion(-)
include/net/netfilter/nf_tables.h +2 -0
index 01570a8f2982..bbc7bfcc1a57 100644
@@ -949,6 +949,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
  *   @sets: sets in the table
  *   @objects: stateful objects in the table
  *   @hgenerator: handle generator state
+ *   @handle: table handle
  *   @use: number of chain references to this table
  *   @flags: table flag (see enum nft_table_flags)
  *   @genmask: generation mask
@@ -960,6 +961,7 @@ struct nft_table {
      struct list_head                sets;
      struct list_head                objects;
      u64                             hgenerator;
+     u64                             handle:42;
      u32                             use;
      u16                             flags:14,
                                      genmask:2;
include/uapi/linux/netfilter/nf_tables.h +2 -0
index a3ee277b17a1..a6ab9a962eae 100644
@@ -168,6 +168,8 @@ enum nft_table_attributes {
      NFTA_TABLE_NAME,
      NFTA_TABLE_FLAGS,
      NFTA_TABLE_USE,
+     NFTA_TABLE_HANDLE,
+     NFTA_TABLE_PAD,
      __NFTA_TABLE_MAX
 };
 #define NFTA_TABLE_MAX               (__NFTA_TABLE_MAX - 1)
net/netfilter/nf_tables_api.c +0 -0
index d8327b43e4dc..4a4e247e6028 100644
@@ -22,6 +22,7 @@
 #include <net/net_namespace.h>
 #include <net/sock.h>
 
Quoted 4 lines

net/netfilter/nf_tables_api.c line 25

Quoted 1 line
net/netfilter/nf_tables_api.c +1 -0
+static u64 table_handle;

You need to place this handle number in struct nft_table.

This table_handle is passed to the struct nft_table in nf_tables_newtable().

Quoted 24 lines

net/netfilter/nf_tables_api.c static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { · line 434

Quoted 8 lines
net/netfilter/nf_tables_api.c +1 -0
 static LIST_HEAD(nf_tables_expressions);
 static LIST_HEAD(nf_tables_objects);
 
@@ -430,6 +431,7 @@ static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
      [NFTA_TABLE_NAME]       = { .type = NLA_STRING,
                                  .len = NFT_TABLE_MAXNAMELEN - 1 },
      [NFTA_TABLE_FLAGS]      = { .type = NLA_U32 },
+     [NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
                          ^^^

missing tab indent here.

Quoted 13 lines
net/netfilter/nf_tables_api.c +3 -1
 };
 
 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
@@ -451,7 +453,9 @@ static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
 
      if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
          nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
-         nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
+         nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
+         nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
+                      NFTA_TABLE_PAD))
              goto nla_put_failure;
 
Quoted 10 lines

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

Quoted 7 lines
net/netfilter/nf_tables_api.c +2 -0
      nlmsg_end(skb, nlh);
@@ -734,6 +738,12 @@ static int nf_tables_newtable(struct net *net, struct sock *nlsk,
      INIT_LIST_HEAD(&table->sets);
      INIT_LIST_HEAD(&table->objects);
      table->flags = flags;
+     if (nla[NFTA_TABLE_HANDLE]) {
+             table->handle = be64_to_cpu(nla_get_be64(nla[NFTA_TABLE_HANDLE]));

We don't allow to restore the handle, please remove this line.

OK. I'll remove this.

Quoted 8 lines
Quoted 7 lines
net/netfilter/nf_tables_api.c +4 -0
+     } else {
+             table_handle++;
+             table->handle = table_handle;
+     }
 
      nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
      err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
Quoted 7 lines
Quoted 5 lines

We need to implement deletion via handle too in nf_tables_deltable(), same thing for nf_table_gettable().

Thus, the unique handle becomes useful.

OK. Other than these changes, can I work on implementing deletion via table handle ?

Yes, deletion/get via handle is required, otherwise this handle is not very useful.

Thanks!