Skip to contentSkip to conversation

Netlink bus descriptions

RFC PATCH In review Pablo Neira Ayuso 4 participants 26 messages Feb 7, 2018 – Apr 28, 2019

Subsystems (3)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover Netlink bus descriptions

Hi,

Modern messaging systems usually provide facilities that allows you to inquire about supported commands and message layouts. Netlink has no such facility so far, hence people end up probing for features, which is a bit sloppy. Sometimes there are also magic version numbers in place that gives a hint to userspace on what this kernel supports, so userspace makes assumptions based on this version number.

This patchset aims to improve this situation by adding a new NETLINK_DESC bus with two commands, one to fetch the list of existing commands and another one to describe supported objects. Command descriptions also indicate what netlink objects the interface expects to be used in the netlink message payload, thus, we can relate commands and objects. Objects are represented as compounds of attributes, some of these attributes are nesting other attributes. The netlink description exports the attribute tree and, for simplicity, a linear list of netlink objects identified via unique ID.

Patch 1 adds NLA_PAD, this datatype is needed by netlink description. Patch 2 adds the new generic netlink description bus NETLINK_DESC. Patch 3 adds support for netlink descriptions to nfnetlink. Patch 4 adds a netlink description for nf_tables, so you have an initial
client for this infrastructure to look at as reference.

Not covered by this patchset, but I think it should be possible to (fully?) generate the C file containing the description and the netlink headers based on some generic notation that describes the netlink interface, something like this:

    netlink description  ---> nldesc_compiler ---> .c and .h files
         notation

This would probably make the work of describing the netlink interface less error prone, given people may add a new attribute to the header file and forget about updating the description. I also think this description could be also useful to fuzz tests netlink interfaces, by interpreting the description that the kernel provides.

I've been looking into this since NetDev 2.1 in Canada - already one year ago - I think we need this in nftables so we can evolve more freely.

Comments welcome.

Pablo Neira Ayuso (4):
netlink: add NLA_PAD definition
netlink: add generic object description infrastructure
netfilter: nfnetlink: add support for netlink descriptions
netfilter: nf_tables: add netlink description

 include/linux/netfilter/nfnetlink.h           |   9 +
 include/net/net_namespace.h                   |   1 +
 include/net/netfilter/nf_tables.h             |   2 +
 include/net/netlink.h                         |   2 +
 include/net/nldesc.h                          | 163 +++++++++
 include/uapi/linux/netfilter/nf_tables_desc.h |  57 +++
 include/uapi/linux/netfilter/nfnetlink.h      |   7 +
 include/uapi/linux/netlink.h                  |  67 ++++
 net/netfilter/Makefile                        |   7 +-
 net/netfilter/nf_tables_api.c                 |   2 +
 net/netfilter/nf_tables_desc.c                | 471 ++++++++++++++++++++++++
 net/netfilter/nfnetlink.c                     | 108 ++++++
 net/netlink/Makefile                          |   2 +-
 net/netlink/desc.c                            | 499 ++++++++++++++++++++++++++
 14 files changed, 1393 insertions(+), 4 deletions(-)
 create mode 100644 include/net/nldesc.h
 create mode 100644 include/uapi/linux/netfilter/nf_tables_desc.h
 create mode 100644 net/netfilter/nf_tables_desc.c
 create mode 100644 net/netlink/desc.c
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

RFC PATCH 1/4 netlink: add NLA_PAD definition

The new generic netlink description infrastructure needs this new type to describe padding attributes.

---
 include/net/netlink.h | 2 ++
 1 file changed, 2 insertions(+)
include/net/netlink.h +2 -0
index 0c154f98e987..76e850ead593 100644
@@ -180,6 +180,7 @@ enum {
 	NLA_S32,
 	NLA_S64,
 	NLA_BITFIELD32,
+	NLA_PAD,
 	__NLA_TYPE_MAX,
 };
 
@@ -209,6 +210,7 @@ enum {
  *                         given type fits, using it verifies minimum length
  *                         just like "All other"
  *    NLA_BITFIELD32      A 32-bit bitmap/bitselector attribute
+ *    NLA_PAD		   Empty attribute to align next attribute to 64-bits
  *    All other            Minimum length of attribute payload
  *
  * Example:
Signature · 1 line
2.11.0
#2 lore raw

Re: [PATCH RFC 1/4] netlink: add NLA_PAD definition

Quoted 21 lines · Pablo Neira Ayuso

The new generic netlink description infrastructure needs this new type to describe padding attributes.

---
 include/net/netlink.h | 2 ++
 1 file changed, 2 insertions(+)
include/net/netlink.h +1 -0
index 0c154f98e987..76e850ead593 100644
@@ -180,6 +180,7 @@ enum {
 	NLA_S32,
 	NLA_S64,
 	NLA_BITFIELD32,
+	NLA_PAD,
 	__NLA_TYPE_MAX,
 };
 

include/net/netlink.h enum { · line 213

Quoted 4 lines
include/net/netlink.h +1 -0
@@ -209,6 +210,7 @@ enum {
  *                         given type fits, using it verifies minimum length
  *                         just like "All other"
  *    NLA_BITFIELD32      A 32-bit bitmap/bitselector attribute
+ *    NLA_PAD		   Empty attribute to align next attribute to 64-bits

I guess this should also move to uapi/linux/netlink.h.

johannes

author maintainer
#3 lore raw

RFC PATCH 2/4 netlink: add generic object description infrastructure

In reply to Pablo Neira Ayuso

This patch allows netlink busses to provide object descriptions to userspace, in terms of supported attributes and its corresponding datatypes.

Userspace sends a requests that looks like:

netlink header
NLA_DESC_REQ_BUS
NLA_DESC_REQ_DATA

Where NLA_DESC_REQ_BUS is the netlink bus/protocol number, eg. NETLINK_NETFILTER, and NLA_DESC_REQ_DATA is an attribute layout is specific to the bus that you are inspecting, this is useful for both nfnetlink and genetlink since they need to what subsystem in the bus specifically you're targeting to.

Then, the netlink description subsystem response via netlink dump looks like this:

	netlink header
	NLA_DESC_NUM_OBJS
	NLA_DESC_OBJS (nest)
		NLA_DESC_LIST_ITEM (nest)
			NLA_DESC_OBJ_ID
			NLA_DESC_OBJ_ATTRS_MAX
			NLA_DESC_OBJ_ATTRS (nest)
				NLA_DESC_LIST_ITEM (nest)
					NLA_DESC_ATTR_NUM
					NLA_DESC_ATTR_TYPE
					NLA_DESC_ATTR_LEN
					NLA_DESC_ATTR_MAXVAL
					NLA_DESC_ATTR_NEST_ID
		NLA_DESC_LIST_ITEM (nest)
			...

Each object definition is composed of an unique ID, the number of attributes and the list of attribute definitions.

The NETLINK_DESC bus provides a generic interface to retrieve the list of existing objects and its attributes via netlink dump. This new description family autoloads module dependencies based on what userspace requests.

Each bus needs to register a struct nl_desc_subsys definition, that provides the lookup and parse callbacks. These route the description requests to the corresponding backend subsystem for genetlink and nfnetlink. The lookup callback returns struct nl_desc_objs that provides the array of object descriptions.

---
 include/net/net_namespace.h  |   1 +
 include/net/nldesc.h         | 160 ++++++++++++++
 include/uapi/linux/netlink.h |  67 ++++++
 net/netlink/Makefile         |   2 +-
 net/netlink/desc.c           | 499 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 728 insertions(+), 1 deletion(-)
 create mode 100644 include/net/nldesc.h
 create mode 100644 net/netlink/desc.c
include/net/net_namespace.h +1 -0
index f8a84a2c2341..0921b1d7acfe 100644
@@ -78,6 +78,7 @@ struct net {
 
 	struct sock 		*rtnl;			/* rtnetlink socket */
 	struct sock		*genl_sock;
+	struct sock		*nl_desc_sock;
 
 	struct list_head 	dev_base_head;
 	struct hlist_head 	*dev_name_head;
include/net/nldesc.h new file +160 -0

160 lines not on this page · raw message

include/uapi/linux/netlink.h +67 -0

77 lines not on this page · raw message

net/netlink/Makefile +1 -1
index e837917f6c03..0e44f10a084b 100644
@@ -2,7 +2,7 @@
 # Makefile for the netlink driver.
 #
 
-obj-y  				:= af_netlink.o genetlink.o
+obj-y  				:= af_netlink.o genetlink.o desc.o
 
 obj-$(CONFIG_NETLINK_DIAG)	+= netlink_diag.o
 netlink_diag-y			:= diag.o
net/netlink/desc.c new file +499 -0

499 lines not on this page · raw message

Signature · 1 line
2.11.0
#4 lore raw

Re: [PATCH RFC 2/4] netlink: add generic object description infrastructure

Quoted 91 lines · Pablo Neira Ayuso

This patch allows netlink busses to provide object descriptions to userspace, in terms of supported attributes and its corresponding datatypes.

Userspace sends a requests that looks like:

netlink header
NLA_DESC_REQ_BUS
NLA_DESC_REQ_DATA

Where NLA_DESC_REQ_BUS is the netlink bus/protocol number, eg. NETLINK_NETFILTER, and NLA_DESC_REQ_DATA is an attribute layout is specific to the bus that you are inspecting, this is useful for both nfnetlink and genetlink since they need to what subsystem in the bus specifically you're targeting to.

Then, the netlink description subsystem response via netlink dump looks like this:

	netlink header
	NLA_DESC_NUM_OBJS
	NLA_DESC_OBJS (nest)
		NLA_DESC_LIST_ITEM (nest)
			NLA_DESC_OBJ_ID
			NLA_DESC_OBJ_ATTRS_MAX
			NLA_DESC_OBJ_ATTRS (nest)
				NLA_DESC_LIST_ITEM (nest)
					NLA_DESC_ATTR_NUM
					NLA_DESC_ATTR_TYPE
					NLA_DESC_ATTR_LEN
					NLA_DESC_ATTR_MAXVAL
					NLA_DESC_ATTR_NEST_ID
		NLA_DESC_LIST_ITEM (nest)
			...

Each object definition is composed of an unique ID, the number of attributes and the list of attribute definitions.

The NETLINK_DESC bus provides a generic interface to retrieve the list of existing objects and its attributes via netlink dump. This new description family autoloads module dependencies based on what userspace requests.

Each bus needs to register a struct nl_desc_subsys definition, that provides the lookup and parse callbacks. These route the description requests to the corresponding backend subsystem for genetlink and nfnetlink. The lookup callback returns struct nl_desc_objs that provides the array of object descriptions.

---
 include/net/net_namespace.h  |   1 +
 include/net/nldesc.h         | 160 ++++++++++++++
 include/uapi/linux/netlink.h |  67 ++++++
 net/netlink/Makefile         |   2 +-
 net/netlink/desc.c           | 499 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 728 insertions(+), 1 deletion(-)
 create mode 100644 include/net/nldesc.h
 create mode 100644 net/netlink/desc.c
include/net/nldesc.h new file +24 -0
new file mode 100644
index 000000000000..19306a648f10
@@ -0,0 +1,160 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __NET_NLDESC_H
+#define __NET_NLDESC_H
+
+#include <linux/types.h>
+
+struct nl_desc_cmd;
+struct nl_desc_obj;
+
+struct nl_desc_cmds {
+	int				max;
+	const struct nl_desc_cmd	*table;
+};
+
+struct nl_desc_objs {
+	int				max;
+	const struct nl_desc_obj	**table;
+};
+
+struct nl_desc_req {
+	u32				bus;
+};
+
+struct net;

include/net/nldesc.h line 64

Quoted 10 lines
include/net/nldesc.h new file +9 -0
+struct sk_buff;
+struct nlmsghdr;
+struct nlattr;
+
 
+
+/**
+ * struct nl_desc_obj - netlink object description
+ * @id: unique ID to identify this netlink object
+ * @max: number of attributes to describe this object
      @attr_max:

include/net/nldesc.h line 71

Quoted 7 lines

+ * @attrs: array of attribute descriptions
+ */

include/net/nldesc.h new file +5 -0
+struct nl_desc_obj {
+	u16				id;
+	u16				attr_max;
+	const struct nl_desc_attr	*attrs;
+};

Is there a test program for this?
Maybe add it to tools/testing/ ?

thanks,

Signature · 1 line
~Randy
author maintainer
#5 lore raw

Re: [PATCH RFC 2/4] netlink: add generic object description infrastructure

Hi Randy,

On Wed, Feb 07, 2018 at 05:28:20PM -0800, Randy Dunlap wrote:
[...]

Quoted 35 lines
Quoted 34 lines
include/net/nldesc.h new file +28 -0
new file mode 100644
index 000000000000..19306a648f10
@@ -0,0 +1,160 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __NET_NLDESC_H
+#define __NET_NLDESC_H
+
+#include <linux/types.h>
+
+struct nl_desc_cmd;
+struct nl_desc_obj;
+
+struct nl_desc_cmds {
+	int				max;
+	const struct nl_desc_cmd	*table;
+};
+
+struct nl_desc_objs {
+	int				max;
+	const struct nl_desc_obj	**table;
+};
+
+struct nl_desc_req {
+	u32				bus;
+};
+
+struct net;
+struct sk_buff;
+struct nlmsghdr;
+struct nlattr;
+
Quoted 7 lines

include/net/nldesc.h line 33

Quoted 5 lines
include/net/nldesc.h new file +5 -0
+
+/**
+ * struct nl_desc_obj - netlink object description
+ * @id: unique ID to identify this netlink object
+ * @max: number of attributes to describe this object
      @attr_max:

Thanks for spotting this.

Quoted 10 lines

include/net/nldesc.h line 40

Quoted 7 lines
include/net/nldesc.h new file +7 -0
+ * @attrs: array of attribute descriptions
+ */
+struct nl_desc_obj {
+	u16				id;
+	u16				attr_max;
+	const struct nl_desc_attr	*attrs;
+};

Is there a test program for this?

I'm attaching what I have used to test this. These files print the netlink bus description.

Quoted 1 line

Maybe add it to tools/testing/ ?

Yes, I can place it there, no problem. This userspace code depends on libmnl though.

I was planning to add infrastructure to libmnl to add a couple of helper functions that allows us to populate the nl_desc cache and to look up for presence of commands/attributes.

People that don't like libmnl for whatever reason can add similar code to their libraries too, of course.

Thanks!

Quoted 1 line

From 7826d6aa47d20bc09f7c8e33a457a5a338a8db55 Mon Sep 17 00:00:00 2001

From: Pablo Neira Ayuso <[email protected]>
Date: Tue, 16 Jan 2018 00:05:37 +0100
Subject: [PATCH libmnl] examples: add netlink bus description

Add nft-dump-desc-cmds.c and nft-dump-desc-obj.c to dump command and object descriptions.

---
 examples/Makefile.am          |  11 ++
 examples/nft-dump-desc-cmds.c | 177 ++++++++++++++++++++++++++++
 examples/nft-dump-desc-objs.c | 263 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 451 insertions(+)
 create mode 100644 examples/nft-dump-desc-cmds.c
 create mode 100644 examples/nft-dump-desc-objs.c
examples/Makefile.am +11 -0
index e5cb052b315c..a8d4ba50f5ad 100644
@@ -1 +1,12 @@
+include $(top_srcdir)/Make_global.am
+
 SUBDIRS = genl kobject netfilter rtnl
+
+check_PROGRAMS = nft-dump-desc-cmds \
+                 nft-dump-desc-objs
+
+nft_dump_desc_cmds_SOURCES = nft-dump-desc-cmds.c
+nft_dump_desc_cmds_LDADD = ../src/libmnl.la
+
+nft_dump_desc_objs_SOURCES = nft-dump-desc-objs.c
+nft_dump_desc_objs_LDADD = ../src/libmnl.la
examples/nft-dump-desc-cmds.c new file +177 -0

177 lines not on this page · raw message

examples/nft-dump-desc-objs.c new file +263 -0

263 lines not on this page · raw message

Signature · 1 line
2.11.0
#6 lore raw

Re: [PATCH RFC 2/4] netlink: add generic object description infrastructure

In reply to Pablo Neira Ayuso

include/net/net_namespace.h struct net { · line 81

Quoted 6 lines · Pablo Neira Ayuso

+++ b/include/net/net_namespace.h

include/net/net_namespace.h +1 -0
@@ -78,6 +78,7 @@ struct net {
 
 	struct sock 		*rtnl;			/* rtnetlink socket */
 	struct sock		*genl_sock;
+	struct sock		*nl_desc_sock;

Using genl would save that =)

include/uapi/linux/netlink.h struct nla_bitfield32 { · line 254

Quoted 3 lines
include/uapi/linux/netlink.h +3 -0
+enum {
+	NLDESC_GET_CMDS		= 16,
+	NLDESC_NEW_CMDS,

I would say all of this new API should be in a new header file.

Quoted 1 line

+enum nft_nldesc_req_attributes {

nft_ prefix also doesn't seem appropriate.

net/netlink/desc.c line 320

Quoted 2 lines
net/netlink/desc.c new file +2 -0
+static int nl_desc_handle_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
+			      struct netlink_ext_ack *extack)

From here on it's also mostly boilerplate code that using genl handles :-)

johannes

author maintainer
#7 lore raw

RFC PATCH 3/4 netfilter: nfnetlink: add support for netlink descriptions

In reply to Pablo Neira Ayuso

NETLINK_NETFILTER is shared by several netfilter subsystems, add new infrastructure to allow subsystems to register their own descriptions. Hence, nfnetlink routes description requests to the corresponding subsystem backend.

---
 include/linux/netfilter/nfnetlink.h      |   9 +++
 include/net/nldesc.h                     |   3 +
 include/uapi/linux/netfilter/nfnetlink.h |   7 ++
 net/netfilter/nfnetlink.c                | 108 +++++++++++++++++++++++++++++++
 4 files changed, 127 insertions(+)
include/linux/netfilter/nfnetlink.h +9 -0
index 495ba4dd9da5..87b3d9860444 100644
@@ -37,6 +37,15 @@ struct nfnetlink_subsystem {
 int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n);
 int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n);
 
+struct nfnl_desc_subsys {
+	u16				id;
+	const struct nl_desc_cmds	*cmds;
+	const struct nl_desc_objs	*objs;
+};
+
+int nfnl_desc_register_subsys(const struct nfnl_desc_subsys *subsys);
+void nfnl_desc_unregister_subsys(const struct nfnl_desc_subsys *subsys);
+
 int nfnetlink_has_listeners(struct net *net, unsigned int group);
 int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
 		   unsigned int group, int echo, gfp_t flags);
include/net/nldesc.h +3 -0
index 19306a648f10..0d232846005a 100644
@@ -19,6 +19,9 @@ struct nl_desc_objs {
 
 struct nl_desc_req {
 	u32				bus;
+	union {
+		u32			nf_subsys_id;
+	};
 };
 
 struct net;
include/uapi/linux/netfilter/nfnetlink.h +7 -0
index 5bc960f220b3..7dacf264e0b5 100644
@@ -62,6 +62,13 @@ struct nfgenmsg {
 #define NFNL_SUBSYS_NFT_COMPAT		11
 #define NFNL_SUBSYS_COUNT		12
 
+enum nfnl_desc_attr {
+	NFNL_DESC_REQ_UNSPEC,
+	NFNL_DESC_REQ_SUBSYS,
+	__NFNL_DESC_REQ_MAX
+};
+#define NFNL_DESC_REQ_MAX	(__NFNL_DESC_REQ_MAX - 1)
+
 /* Reserved control nfnetlink messages */
 #define NFNL_MSG_BATCH_BEGIN		NLMSG_MIN_TYPE
 #define NFNL_MSG_BATCH_END		NLMSG_MIN_TYPE+1
net/netfilter/nfnetlink.c +108 -0

141 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#8 lore raw

RFC PATCH 4/4 netfilter: nf_tables: add netlink description

In reply to Pablo Neira Ayuso

This patch adds the netlink description for nf_tables.

---
 include/net/netfilter/nf_tables.h             |   2 +
 include/uapi/linux/netfilter/nf_tables_desc.h |  57 ++++
 net/netfilter/Makefile                        |   7 +-
 net/netfilter/nf_tables_api.c                 |   2 +
 net/netfilter/nf_tables_desc.c                | 471 ++++++++++++++++++++++++++
 5 files changed, 536 insertions(+), 3 deletions(-)
 create mode 100644 include/uapi/linux/netfilter/nf_tables_desc.h
 create mode 100644 net/netfilter/nf_tables_desc.c
include/net/netfilter/nf_tables.h +2 -0
index 663b015dace5..91b52b365f7e 100644
@@ -1345,4 +1345,6 @@ struct nft_trans_flowtable {
 #define nft_trans_flowtable(trans)	\
 	(((struct nft_trans_flowtable *)trans->data)->flowtable)
 
+extern const struct nfnl_desc_subsys nft_nldesc;
+
 #endif /* _NET_NF_TABLES_H */
include/uapi/linux/netfilter/nf_tables_desc.h new file +57 -0

57 lines not on this page · raw message

net/netfilter/Makefile +4 -3
index 5d9b8b959e58..38e048ea7e42 100644
@@ -73,9 +73,10 @@ obj-$(CONFIG_NETFILTER_CONNCOUNT) += nf_conncount.o
 obj-$(CONFIG_NF_DUP_NETDEV)	+= nf_dup_netdev.o
 
 # nf_tables
-nf_tables-objs := nf_tables_core.o nf_tables_api.o nf_tables_trace.o \
-		  nft_immediate.o nft_cmp.o nft_range.o nft_bitwise.o \
-		  nft_byteorder.o nft_payload.o nft_lookup.o nft_dynset.o
+nf_tables-objs := nf_tables_core.o nf_tables_api.o nf_tables_desc.o \
+		  nf_tables_trace.o nft_immediate.o nft_cmp.o nft_range.o \
+		  nft_bitwise.o nft_byteorder.o nft_payload.o nft_lookup.o \
+		  nft_dynset.o
 
 obj-$(CONFIG_NF_TABLES)		+= nf_tables.o
 obj-$(CONFIG_NF_TABLES_INET)	+= nf_tables_inet.o
net/netfilter/nf_tables_api.c +2 -0
index 0791813a1e7d..cb500aeaa729 100644
@@ -6601,6 +6601,7 @@ static int __init nf_tables_module_init(void)
 	if (err < 0)
 		goto err3;
 
+	nfnl_desc_register_subsys(&nft_nldesc);
 	register_netdevice_notifier(&nf_tables_flowtable_notifier);
 
 	return register_pernet_subsys(&nf_tables_net_ops);
@@ -6617,6 +6618,7 @@ static void __exit nf_tables_module_exit(void)
 	unregister_pernet_subsys(&nf_tables_net_ops);
 	nfnetlink_subsys_unregister(&nf_tables_subsys);
 	unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
+	nfnl_desc_unregister_subsys(&nft_nldesc);
 	rcu_barrier();
 	nf_tables_core_module_exit();
 	kfree(info);
net/netfilter/nf_tables_desc.c new file +471 -0

471 lines not on this page · raw message

Signature · 1 line
2.11.0
#9 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

net/netfilter/nf_tables_desc.c line 18

Quoted 8 lines · Pablo Neira Ayuso
net/netfilter/nf_tables_desc.c new file +7 -0
+static const struct nl_desc_attr nft_nldesc_table_attrs[NFTA_TABLE_MAX + 1] = {
+	NLDESC_ATTR_STRING(NFTA_TABLE_NAME, NFT_NAME_MAXLEN - 1),
+	NLDESC_ATTR_U32_MAX(NFTA_TABLE_FLAGS, NFT_TABLE_F_DORMANT),
+	NLDESC_ATTR_U32(NFTA_TABLE_USE),
+	NLDESC_ATTR_U64(NFTA_TABLE_HANDLE),
+	NLDESC_ATTR_PAD(NFTA_TABLE_PAD),
+};

This part I don't really like so much. Yes, it's >1yo, so we didn't have all the stuff at the time.

But this is _lots_ of code, and most of it is already encoded in the nla_policy for all those attributes. I think we really need to find a way to unify the two sets of data.

I'd actually be happy to *just* expose the policy with all its nested elements/objects in there. That'd probably be also almost enough for you? And that way you don't need to maintain the same descriptions twice (and we *will* get it wrong if we do that).

For genl sub-families, I'd also say that the whole thing could be exposed within the genl family, so I think we'd want to express this as some kind of helper logic to export the policy data.

I'm not entirely sure what the whole story with your "objects" is though. Aren't those just nested attributes of sort?

I see e.g.

net/netfilter/nf_tables_desc.c line 342

Quoted 4 lines
net/netfilter/nf_tables_desc.c new file +3 -0
+static const struct nl_desc_attr nft_nldesc_set_desc_attrs[NFTA_SET_DESC_MAX + 1] = {
+       NLDESC_ATTR_U32(NFTA_SET_DESC_SIZE),
+};

This is policy data - like I said above we should unify the two.

net/netfilter/nf_tables_desc.c line 345

Quoted 2 lines
net/netfilter/nf_tables_desc.c new file +2 -0
+static const struct nl_desc_obj nft_nldesc_set_desc[] = {
+       NLDESC_OBJ(NFT_SET_DESC, nft_nldesc_set_desc_attrs, NFTA_SET_DESC_MAX),

This is some kind of "object", which I'm not sure I understand, but NFTA_SET_DESC_MAX is just the policy data of what the max attribute is for this thing.

Quoted 1 line

+static const struct nl_desc_attr nft_nldesc_set_attrs[NFTA_SET_MAX + 1] = {

...

Quoted 1 line

+ NLDESC_ATTR_NESTED(NFTA_SET_DESC, nft_nldesc_set_desc),

and here this is again just a policy thing, more or less.

So it seems to me that without even having an object enum, we'd get the same kind of data by

struct nla_policy nft_policy_set_desc[NFTA_SET_DESC_MAX + 1] = {
	[NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
};

struct nla_policy nft_policy_set[NFTA_SET_MAX + 1] = {
[NFTA_SET_DESC] = NLA_POLICY_NESTED(nft_policy_set_desc),
};

Except we don't have an "object ID" (and obviously also that we can use the same data to actually parse/validate messages, and so don't end up with bad situations of nldesc saying one thing and the policy another.)

So ... Assuming I write some code to expose the policy (which I plan on doing), what would this still be able to expose in addition?

johannes

author maintainer
#10 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 12 lines · Johannes Berg

net/netfilter/nf_tables_desc.c line 18

Quoted 8 lines · Pablo Neira Ayuso
net/netfilter/nf_tables_desc.c new file +7 -0
+static const struct nl_desc_attr nft_nldesc_table_attrs[NFTA_TABLE_MAX + 1] = {
+	NLDESC_ATTR_STRING(NFTA_TABLE_NAME, NFT_NAME_MAXLEN - 1),
+	NLDESC_ATTR_U32_MAX(NFTA_TABLE_FLAGS, NFT_TABLE_F_DORMANT),
+	NLDESC_ATTR_U32(NFTA_TABLE_USE),
+	NLDESC_ATTR_U64(NFTA_TABLE_HANDLE),
+	NLDESC_ATTR_PAD(NFTA_TABLE_PAD),
+};

This part I don't really like so much. Yes, it's >1yo, so we didn't have all the stuff at the time.

I'm glad to receive feedback, even 1 year later :-)

Quoted 3 lines

But this is _lots_ of code, and most of it is already encoded in the nla_policy for all those attributes. I think we really need to find a way to unify the two sets of data.

We could probably extract this from the nla_policy if we extend it. I can have a look...

Quoted 3 lines

I'd actually be happy to *just* expose the policy with all its nested elements/objects in there. That'd probably be also almost enough for you?

Exposing commands is also important, I know of people poking for commands to see if they are available or not.

I think we should full description to userspace, not only policies.

Quoted 39 lines

And that way you don't need to maintain the same descriptions twice (and we *will* get it wrong if we do that).

For genl sub-families, I'd also say that the whole thing could be exposed within the genl family, so I think we'd want to express this as some kind of helper logic to export the policy data.

I'm not entirely sure what the whole story with your "objects" is though. Aren't those just nested attributes of sort?

I see e.g.

net/netfilter/nf_tables_desc.c line 342

Quoted 4 lines
net/netfilter/nf_tables_desc.c new file +3 -0
+static const struct nl_desc_attr nft_nldesc_set_desc_attrs[NFTA_SET_DESC_MAX + 1] = {
+       NLDESC_ATTR_U32(NFTA_SET_DESC_SIZE),
+};

This is policy data - like I said above we should unify the two.

net/netfilter/nf_tables_desc.c line 345

Quoted 2 lines
net/netfilter/nf_tables_desc.c new file +2 -0
+static const struct nl_desc_obj nft_nldesc_set_desc[] = {
+       NLDESC_OBJ(NFT_SET_DESC, nft_nldesc_set_desc_attrs, NFTA_SET_DESC_MAX),

This is some kind of "object", which I'm not sure I understand, but NFTA_SET_DESC_MAX is just the policy data of what the max attribute is for this thing.

Quoted 1 line

+static const struct nl_desc_attr nft_nldesc_set_attrs[NFTA_SET_MAX + 1] = {

...

Quoted 1 line

+ NLDESC_ATTR_NESTED(NFTA_SET_DESC, nft_nldesc_set_desc),

and here this is again just a policy thing, more or less.

So it seems to me that without even having an object enum, we'd get the same kind of data by

struct nla_policy nft_policy_set_desc[NFTA_SET_DESC_MAX + 1] = {
	[NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
};
Quoted 7 lines

struct nla_policy nft_policy_set[NFTA_SET_MAX + 1] = {
[NFTA_SET_DESC] = NLA_POLICY_NESTED(nft_policy_set_desc),
};

Except we don't have an "object ID" (and obviously also that we can use the same data to actually parse/validate messages, and so don't end up with bad situations of nldesc saying one thing and the policy another.)

The object ID is needed by userspace, to build a flat table, and look up for the nested IDs, to express the graph. We can probably place this in the policy.

Quoted 2 lines

So ... Assuming I write some code to expose the policy (which I plan on doing), what would this still be able to expose in addition?

This would be incomplete, since commands are an essential part too, and relation of attributes with commands are also too.

#11 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Hi Pablo,

Quoted 1 line

I'm glad to receive feedback, even 1 year later :-)

:-)

Quoted 6 lines
Quoted 3 lines

But this is _lots_ of code, and most of it is already encoded in the nla_policy for all those attributes. I think we really need to find a way to unify the two sets of data.

We could probably extract this from the nla_policy if we extend it. I can have a look...

Please do, and please have a look at the patch(es) I sent out regarding this. I think we've extended the policy enough that most of this is really there, and we can certainly add more to the policy.

For example, I am now considering allowing NLA_POLICY_RANGE(NLA_BINARY, ...) to have an upper *and* lower length bound for an NLA_BINARY value, right now you can only have either of the two and we have uses cases where we need both.

Quoted 6 lines
Quoted 3 lines

I'd actually be happy to *just* expose the policy with all its nested elements/objects in there. That'd probably be also almost enough for you?

Exposing commands is also important, I know of people poking for commands to see if they are available or not.

Alright, well, generic netlink already exposes the commands :-)

Quoted 1 line

I think we should full description to userspace, not only policies.

I guess the way I see it, they're all pieces of the puzzle. The policy is good for both code in the kernel and for exposing it. The command list is already exposed.

What now we're missing, in a sense, is which commands take which attributes. This would actually *also* be a good thing for validation in the kernel, but I haven't really found a good way to encode it yet. Doing a bitmap isn't really feasible, and doing a list is difficult to type out.

Perhaps if we do end up going with your code generation idea then we can do this more easily.

Quoted 7 lines
Quoted 3 lines

Except we don't have an "object ID" (and obviously also that we can use the same data to actually parse/validate messages, and so don't end up with bad situations of nldesc saying one thing and the policy another.)

The object ID is needed by userspace, to build a flat table, and look up for the nested IDs, to express the graph. We can probably place this in the policy.

I don't really understand the object ID.

Say you have a top-level "filter" attribute (I'm completely making up things here).

Now this filter "object" can have a number of attributes, and that's captured by the nested policy there?

IOW - the code I wrote until now would expose

policy 0: attribute filter: nested with attrs from policy 1 policy 1: attribute abc: NLA_U8
policy 1: attribute xyz: NLA_U32

etc.

Quoted 2 lines

This would be incomplete, since commands are an essential part too, and relation of attributes with commands are also too.

Agree. Still, I think it solves much of the problem, like I said above - all are parts of the puzzle.

With generic netlink we have a list of commands already. That should be easy to add elsewhere, though generic netlink has the advantage of having the list of commands (and their handlers) as *data*, which I think is expressed *code* in rtnetlink now? But that can either be fixed or you just carry a separate list of commands there.

With the policy export I wrote we have the policy, and if you set it up properly with all the nested sub-policies for each attribute etc.

So I agree - we're missing "this command uses these attributes", but again I don't think the object description is actually the right solution here, because you *also* want to enforce that in some way.

I feel the generic netlink model where the command handlers etc. are not code but data is superior here because it lets us add things like that relatively easily, though of course we haven't actually done it now.

So basically - my main concern with this bus description stuff you've done here is that it duplicates all the data and needs to be maintained separately from what should be the same data used for enforcement.

johannes

author maintainer
#12 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 8 lines · Johannes Berg
Quoted 6 lines
Quoted 3 lines

I'd actually be happy to *just* expose the policy with all its nested elements/objects in there. That'd probably be also almost enough for you?

Exposing commands is also important, I know of people poking for commands to see if they are available or not.

Alright, well, generic netlink already exposes the commands :-)

How are you going to relate this with the policy description?

Could you post a schema of the netlink attribute hierarchy of your policy descriptions?

Quoted 19 lines
Quoted 1 line

I think we should full description to userspace, not only policies.

I guess the way I see it, they're all pieces of the puzzle. The policy is good for both code in the kernel and for exposing it. The command list is already exposed.

What now we're missing, in a sense, is which commands take which attributes. This would actually *also* be a good thing for validation in the kernel, but I haven't really found a good way to encode it yet. Doing a bitmap isn't really feasible, and doing a list is difficult to type out.

Perhaps if we do end up going with your code generation idea then we can do this more easily.

Quoted 4 lines
Quoted 3 lines

Except we don't have an "object ID" (and obviously also that we can use the same data to actually parse/validate messages, and so don't end up with bad situations of nldesc saying one thing and the policy another.)

Quoted 5 lines
Quoted 3 lines

The object ID is needed by userspace, to build a flat table, and look up for the nested IDs, to express the graph. We can probably place this in the policy.

I don't really understand the object ID.

This object ID is a policy nest ID.

The idea is to expose an array of nest IDs (object IDs), then this array contains the attributes that this nest contains and if there are nested attributes, then in case of nested attributed, the nest ID that describe this attribute.

The idea is to make it easy for userspace to build this table, and then userspace performs lookups based on this object ID (policy nest ID).

Quoted 33 lines

Say you have a top-level "filter" attribute (I'm completely making up things here).

Now this filter "object" can have a number of attributes, and that's captured by the nested policy there?

IOW - the code I wrote until now would expose

policy 0: attribute filter: nested with attrs from policy 1 policy 1: attribute abc: NLA_U8
policy 1: attribute xyz: NLA_U32

etc.

Quoted 2 lines

This would be incomplete, since commands are an essential part too, and relation of attributes with commands are also too.

Agree. Still, I think it solves much of the problem, like I said above - all are parts of the puzzle.

With generic netlink we have a list of commands already. That should be easy to add elsewhere, though generic netlink has the advantage of having the list of commands (and their handlers) as *data*, which I think is expressed *code* in rtnetlink now? But that can either be fixed or you just carry a separate list of commands there.

With the policy export I wrote we have the policy, and if you set it up properly with all the nested sub-policies for each attribute etc.

So I agree - we're missing "this command uses these attributes", but again I don't think the object description is actually the right solution here, because you *also* want to enforce that in some way.

Quoted 7 lines

I feel the generic netlink model where the command handlers etc. are not code but data is superior here because it lets us add things like that relatively easily, though of course we haven't actually done it now.

So basically - my main concern with this bus description stuff you've done here is that it duplicates all the data and needs to be maintained separately from what should be the same data used for enforcement.

My idea was to provide a description of netlink, then a compiler tool that would autogenerate the policy structures and the UAPI headers from this bus description. Yes, it sounds too futuristic to autogenerate code in the kernel, but other projects do already.

#13 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 11 lines · Pablo Neira Ayuso
Quoted 8 lines · Johannes Berg
Quoted 6 lines
Quoted 3 lines

I'd actually be happy to *just* expose the policy with all its nested elements/objects in there. That'd probably be also almost enough for you?

Exposing commands is also important, I know of people poking for commands to see if they are available or not.

Alright, well, generic netlink already exposes the commands :-)

How are you going to relate this with the policy description?

Well, I'm not actually relating this at all right now.

Since generic netlink already has a list of commands like

{
.cmd = XYZ,
.doit = do_xyz,
.dumpit = dump_xyz,
}

and I recently moved the policy to be for the whole *family*, not for each command, basically all we're missing is a list of attributes used by this command.

Ideally, we'd add this as

{
.cmd = XYZ,
.doit = do_xyz,
.dumpit = dump_xyz,
.attrs = { ATTR_A, ATTR_B, ATTR_C, ATTR_D },
}

but of course there's no good way to express this in C, you'd have to build an out-of-line array and point to it.

If we do this though, it's easy to expose, right? Just a list of attribute types that you can go look up in the policy description obtained separately.

Quoted 2 lines

Could you post a schema of the netlink attribute hierarchy of your policy descriptions?

The whole patch is in your inbox ;-)

But basically it looks like this:
FAMILY_SPECIFIC_POLICY_ATTRIBUTE = { // nested
[policy index] = { // nested, 0 = root policy

    [attribute type] = { // nested
      [NL_POLICY_TYPE_ATTR_TYPE] = U32(NL_ATTR_TYPE_*),

      // for signed limited values:
      [NL_POLICY_TYPE_ATTR_MIN_VALUE_S] = S64(...),
      [NL_POLICY_TYPE_ATTR_MAX_VALUE_S] = S64(...),

      // for unsigned limited values:
      [NL_POLICY_TYPE_ATTR_MIN_VALUE_U] = U64(...),
      [NL_POLICY_TYPE_ATTR_MAX_VALUE_U] = U64(...),

      // for binary min/max length:
      [NL_POLICY_TYPE_ATTR_MIN_LENGTH] = U32(...),
      [NL_POLICY_TYPE_ATTR_MAX_LENGTH] = U32(...),

      // for nested policy (nested or nested array):
      [NL_POLICY_TYPE_ATTR_POLICY_IDX] = U32(...),
      [NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE] = U32(...),

      // for bitfield32:
      [NL_POLICY_TYPE_ATTR_BITFIELD32_MASK] = U32(...),
    }

}
}

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

The object ID is needed by userspace, to build a flat table, and look up for the nested IDs, to express the graph. We can probably place this in the policy.

I don't really understand the object ID.

This object ID is a policy nest ID.

Ok. So basically I even already have the object ID, except that I don't really specifically give it one, I just build it based on the flattening algorithm I described in my other email..

Quoted 7 lines

The idea is to expose an array of nest IDs (object IDs), then this array contains the attributes that this nest contains and if there are nested attributes, then in case of nested attributed, the nest ID that describe this attribute.

The idea is to make it easy for userspace to build this table, and then userspace performs lookups based on this object ID (policy nest ID).

Right. I think I have that?

Quoted 8 lines
Quoted 3 lines

So basically - my main concern with this bus description stuff you've done here is that it duplicates all the data and needs to be maintained separately from what should be the same data used for enforcement.

My idea was to provide a description of netlink, then a compiler tool that would autogenerate the policy structures and the UAPI headers from this bus description. Yes, it sounds too futuristic to autogenerate code in the kernel, but other projects do already.

Yeah, ok, but I think you'll have a hard time convincing everyone to switch to that whole-sale, and ban the other approach. What I've done with the policy is far more agreeable to making progress without a complete rewrite. The policy exposure for generic netlink is mostly automatic, for example, as it could be for other netlink families I guess.

The issue with C I noted above of course does lend itself really well to expressing it in a DSL and then generating the C code, but even *then* I would still argue that having all of this duplicated is a waste of memory since we need to have the same data already.

Anyway. Please check out the patches I posted today. I do know that they cannot currently express the "which attribute belongs to which command" part, but I believe that we can add that in some way without rewriting the whole thing, and that making step-by-step progress is better.

So please check out the patches and tell me whether you see any fundamental problem with that, or if you just think that they're incomplete in this manner - I agree and really would like to address that, but need to also see how that feeds back into the validation (it must, after all) etc.

johannes

#14 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 10 lines · Johannes Berg

Ideally, we'd add this as

{
.cmd = XYZ,
.doit = do_xyz,
.dumpit = dump_xyz,
.attrs = { ATTR_A, ATTR_B, ATTR_C, ATTR_D },
}

Quoted 2 lines

but of course there's no good way to express this in C, you'd have to build an out-of-line array and point to it.

Actually, it's possibly even more complicated. After all, it is possible that you have an ATTR_N, that is nested, and that contains certain sub- attributes (ATTR_N_A, ATTR_N_B, ...) of which only some are valid for the operation X, but a different subset is valid for operation Y.

I'm sort of hoping we don't have enough of these cases to make it really something we want to express, because that'll be really hard to express in a way that we can validate and expose.

Quoted 4 lines

The issue with C I noted above of course does lend itself really well to expressing it in a DSL and then generating the C code, but even *then* I would still argue that having all of this duplicated is a waste of memory since we need to have the same data already.

And also, in addition to this, I just realized that we really don't want any sort of separate descriptions because we want to have the ability to validate everything that we express to userspace.

IOW - if we tell userspace "this is valid" then we should at the same time be able to use those description data structures to actually validate that userspace isn't sending us something out of this spec.

So I guess that's just one more reason I think that we fundamentally must provide to userspace exactly what we validate, and make the validation data structures (or language) expressive enough to be able to capture the real constraints.

johannes

author maintainer
#15 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 11 lines · Johannes Berg
Quoted 10 lines · Johannes Berg

Ideally, we'd add this as

{
.cmd = XYZ,
.doit = do_xyz,
.dumpit = dump_xyz,
.attrs = { ATTR_A, ATTR_B, ATTR_C, ATTR_D },
}

Quoted 7 lines
Quoted 2 lines

but of course there's no good way to express this in C, you'd have to build an out-of-line array and point to it.

Actually, it's possibly even more complicated. After all, it is possible that you have an ATTR_N, that is nested, and that contains certain sub- attributes (ATTR_N_A, ATTR_N_B, ...) of which only some are valid for the operation X, but a different subset is valid for operation Y.

I solved this in my patchset through the object ID. So each command points to an object ID, then such object ID comes with a list of attributes.

If we use the list policies that you propose, then it's just an extra enumeration to maintain for each command. And many commands will likely reuse the same object ID.

#16 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 15 lines · Pablo Neira Ayuso
Quoted 14 lines · Johannes Berg
Quoted 12 lines · Johannes Berg

Ideally, we'd add this as

{
.cmd = XYZ,
.doit = do_xyz,
.dumpit = dump_xyz,
.attrs = { ATTR_A, ATTR_B, ATTR_C, ATTR_D },
}

but of course there's no good way to express this in C, you'd have to build an out-of-line array and point to it.

Quoted 8 lines
Quoted 4 lines

Actually, it's possibly even more complicated. After all, it is possible that you have an ATTR_N, that is nested, and that contains certain sub- attributes (ATTR_N_A, ATTR_N_B, ...) of which only some are valid for the operation X, but a different subset is valid for operation Y.

I solved this in my patchset through the object ID. So each command points to an object ID, then such object ID comes with a list of attributes.

Yeah, ok. Each object you had is basically its own policy. I just *removed* having a separate policy for each command in generic netlink, as ;-)

What really I think we should have is a common policy, but only some attributes are valid in some commands.

I guess you can slice this in different ways. From a "how much space does this consume" and "can I reuse some code across different commands" I think having the same policy is a good idea though.

Quoted 3 lines

If we use the list policies that you propose, then it's just an extra enumeration to maintain for each command. And many commands will likely reuse the same object ID.

A policy pointer, really.

The list of policies is just built internally when you dump out a policy with its sub-policies for nested attributes/arrays.

johannes

author maintainer
#17 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 21 lines · Johannes Berg
Quoted 20 lines · Pablo Neira Ayuso
Quoted 18 lines · Johannes Berg
Quoted 10 lines · Johannes Berg

Ideally, we'd add this as

{
.cmd = XYZ,
.doit = do_xyz,
.dumpit = dump_xyz,
.attrs = { ATTR_A, ATTR_B, ATTR_C, ATTR_D },
}

Quoted 2 lines

but of course there's no good way to express this in C, you'd have to build an out-of-line array and point to it.

Actually, it's possibly even more complicated. After all, it is possible that you have an ATTR_N, that is nested, and that contains certain sub- attributes (ATTR_N_A, ATTR_N_B, ...) of which only some are valid for the operation X, but a different subset is valid for operation Y.

Quoted 5 lines
Quoted 3 lines

I solved this in my patchset through the object ID. So each command points to an object ID, then such object ID comes with a list of attributes.

Yeah, ok. Each object you had is basically its own policy.

Hm, not necessarily.

The object id is matching to the "root policy". Several commands may have the same "root policy" (or as I call it "object id"). For example, in netfilter we have commands to add and to delete rules, both would have the same "root policy".

Quoted 10 lines

I just *removed* having a separate policy for each command in generic netlink, as ;-)

What really I think we should have is a common policy, but only some attributes are valid in some commands.

I guess you can slice this in different ways. From a "how much space does this consume" and "can I reuse some code across different commands" I think having the same policy is a good idea though.

Quoted 5 lines
Quoted 3 lines

If we use the list policies that you propose, then it's just an extra enumeration to maintain for each command. And many commands will likely reuse the same object ID.

A policy pointer, really.

Sort of, but it has to be stable along time for userspace, right? Actually, it's an ID.

Quoted 2 lines

The list of policies is just built internally when you dump out a policy with its sub-policies for nested attributes/arrays.

If we expose these to userspace, we need that these object IDs are stable, hence the enum. So userspace results in a simple program that just makes look ups for the object ID that contains the description of the attributes that are available.

#18 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 4 lines · Pablo Neira Ayuso
Quoted 4 lines
Quoted 3 lines

I solved this in my patchset through the object ID. So each command points to an object ID, then such object ID comes with a list of attributes.

Quoted 8 lines
Quoted 1 line

Yeah, ok. Each object you had is basically its own policy.

Hm, not necessarily.

The object id is matching to the "root policy". Several commands may have the same "root policy" (or as I call it "object id"). For example, in netfilter we have commands to add and to delete rules, both would have the same "root policy".

Right, I didn't mean they necessarily had a different policy, just that they could.

So ... I guess I need to think about this a bit more.

My approach to this was that as long as you have a single enum (namespace) for attributes (e.g. enum nft_chain_attributes to pick a random example), you'd also want to have a single policy for these, describing the possibilities.

I do admit though that this doesn't cover the "only some of these attributes are valid for a given command" part, and I also admit that I had sort of punted that part.

We can solve that by having separate policies, so if you have two operations using NTFA_CHAIN_*, then perhaps an operation deletes a chain can only take a NFTA_CHAIN_HANDLE or NFTA_CHAIN_NAME, but an operations that sets things inside might take all the other attributes.

HOWEVER, having separate policies then opens up an easy avenue for (mistakenly or not) having, well, separate policies! Even for the same attribute type. And that's something we really *don't* want.

So I actually think that separating "type description", which is the policy today, from the "type validity description" is valuable, because ideally we do want each type (e.g. NFTA_CHAIN_HANDLE) to always be the same regardless of command, so that a hypothetical GET_STATS command won't take a U64 CHAIN_HANDLE while a (similarly hypothetical) DELETE_CHAIN command takes a U32 CHAIN_HANDLE!

Hence my thought of separating "this is the policy for the attributes (types)" and "this is the list of allowed types for this command". I do realize now that the latter becomes difficult with nested attributes though, but we're probably better off finding ways to express that than having entirely different policies (also, the data would be smaller).

[[[ total aside: we could do something like the "list of allowed types" being an array of
union valid_type {
u16 attr;
struct nested *nested;
};

with

struct nested {
u16 attr;
union valid_type *valid_nested_types;
};

I think we can probably safely assume that pointers are always >>MAX_U16 but if not we can either make that larger or find other ways of disambiguating, e.g. by alignment (and making ATTR_TO_PTR(attr) something like "(attr << 1) | 0x1" since "struct nested" requires 2-byte alignment.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

If we use the list policies that you propose, then it's just an extra enumeration to maintain for each command. And many commands will likely reuse the same object ID.

A policy pointer, really.

Sort of, but it has to be stable along time for userspace, right? Actually, it's an ID.

The ID in my policy export is not stable in any way. The only thing that it's used for is to identify which sub-policy is used while you're dumping it. It will be stable by virtue of the algorithm, but will change when you add different sub-policy links etc. You cannot rely on it being stable, but you also do not need to since it's only relevant that you are able to identify the nested policy while dumping.

Quoted 7 lines
Quoted 2 lines

The list of policies is just built internally when you dump out a policy with its sub-policies for nested attributes/arrays.

If we expose these to userspace, we need that these object IDs are stable, hence the enum. So userspace results in a simple program that just makes look ups for the object ID that contains the description of the attributes that are available.

Well, no.

You're now thinking of the "policy ID" I assigned for the wire format as the object ID, but really that's not what it is. The object ID that you're looking for is the attribute type of the nested attribute.

So if you have

struct nla_policy nested_policy[...] = { ... };

struct nla_policy policy[...] = {
[MY_ATTR] = NLA_POLICY_NESTED(nested_policy),
};

and you dump out starting from "policy" then yes, "policy" will have ID 0, and "nested_policy" will have ID 1, but those are only temporary identifiers for the dump. What's really relevant is the attribute type "MY_ATTR".

johannes

#19 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 4 lines · Johannes Berg

You're now thinking of the "policy ID" I assigned for the wire format as the object ID, but really that's not what it is. The object ID that you're looking for is the attribute type of the nested attribute.

Quoted 8 lines

So if you have

struct nla_policy nested_policy[...] = { ... };

struct nla_policy policy[...] = {
[MY_ATTR] = NLA_POLICY_NESTED(nested_policy),
};

So if we extend this, say like this:

struct nla_policy policy[...] = {
[MY_ATTR] = NLA_POLICY_NESTED(nested_policy), [MY_OTHER_ATTR] = NLA_POLICY_NESTED(nested_policy),
};

then you could perhaps argue that having an object ID makes sense, and assigning the same object ID to MY_ATTR and MY_OTHER_ATTR would make sense?

Of course, my could would assign this the same (temporary) policy ID, but there can be no reliance on the policy ID beyond what's needed at runtime to map the attribute to the nested policy.

You still see at runtime that these have the same policy (since they have the same policy ID), but at the same time presumably there was a reason to have MY_ATTR and MY_OTHER_ATTR, so perhaps the semantics are different even if the attributes are the same, as could perhaps be expected if you have a SET and a CLEAR attribute (MY_ATTR and MY_OTHER_ATTR respectively) and the contents you give has the same policy, but different logic?

Basically, I just didn't consider this case to be significant enough to manually and assign stable IDs of some sort, when we already have them in the form of the attribute type.

johannes

author maintainer
#20 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 37 lines · Johannes Berg
Quoted 4 lines · Johannes Berg

You're now thinking of the "policy ID" I assigned for the wire format as the object ID, but really that's not what it is. The object ID that you're looking for is the attribute type of the nested attribute.

Quoted 8 lines

So if you have

struct nla_policy nested_policy[...] = { ... };

struct nla_policy policy[...] = {
[MY_ATTR] = NLA_POLICY_NESTED(nested_policy),
};

So if we extend this, say like this:

struct nla_policy policy[...] = {
[MY_ATTR] = NLA_POLICY_NESTED(nested_policy), [MY_OTHER_ATTR] = NLA_POLICY_NESTED(nested_policy),
};

then you could perhaps argue that having an object ID makes sense, and assigning the same object ID to MY_ATTR and MY_OTHER_ATTR would make sense?

Of course, my could would assign this the same (temporary) policy ID, but there can be no reliance on the policy ID beyond what's needed at runtime to map the attribute to the nested policy.

You still see at runtime that these have the same policy (since they have the same policy ID), but at the same time presumably there was a reason to have MY_ATTR and MY_OTHER_ATTR, so perhaps the semantics are different even if the attributes are the same, as could perhaps be expected if you have a SET and a CLEAR attribute (MY_ATTR and MY_OTHER_ATTR respectively) and the contents you give has the same policy, but different logic?

Quoted 3 lines

Basically, I just didn't consider this case to be significant enough to manually and assign stable IDs of some sort, when we already have them in the form of the attribute type.

But they all point to the same nested_policy, ie. these nested atributes represent instances of the same object class.

I think this is meaningful to userspace in terms of providing a description of the interface, rather than making it look.

Without the ID, it is not possible from userspace to see that MY_ATTR and MY_OTHER_ATTR refer to the same object, right?

#21 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 2 lines · Pablo Neira Ayuso

But they all point to the same nested_policy, ie. these nested atributes represent instances of the same object class.

To some extent, yes.

Quoted 2 lines

I think this is meaningful to userspace in terms of providing a description of the interface, rather than making it look.

Sure.

Quoted 2 lines

Without the ID, it is not possible from userspace to see that MY_ATTR and MY_OTHER_ATTR refer to the same object, right?

There is an ID, and if you reference the same sub-policy multiple times for nested / nested array attribute types (even at different levels of nesting btw) then this sub-policy will only be dumped to userspace multiple times, given an ID, and be referenced by that ID from the appropriate attribute types in other root/sub-policies.

The only thing is that between kernel versions that ID may change as it's computed while walking the policy graph, and that graph may change and thus the walk may reach nodes in the graph in a different order and thereby label them differently.

johannes

author maintainer
#22 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

In reply to Johannes Berg
Quoted 2 lines · Johannes Berg

On Fri, 2019-04-26 at 21:20 +0200, Pablo Neira Ayuso wrote:

[...]

Quoted 20 lines

So ... I guess I need to think about this a bit more.

My approach to this was that as long as you have a single enum (namespace) for attributes (e.g. enum nft_chain_attributes to pick a random example), you'd also want to have a single policy for these, describing the possibilities.

I do admit though that this doesn't cover the "only some of these attributes are valid for a given command" part, and I also admit that I had sort of punted that part.

We can solve that by having separate policies, so if you have two operations using NTFA_CHAIN_*, then perhaps an operation deletes a chain can only take a NFTA_CHAIN_HANDLE or NFTA_CHAIN_NAME, but an operations that sets things inside might take all the other attributes.

HOWEVER, having separate policies then opens up an easy avenue for (mistakenly or not) having, well, separate policies! Even for the same attribute type. And that's something we really *don't* want.

Quoted 6 lines

So I actually think that separating "type description", which is the policy today, from the "type validity description" is valuable, because ideally we do want each type (e.g. NFTA_CHAIN_HANDLE) to always be the same regardless of command, so that a hypothetical GET_STATS command won't take a U64 CHAIN_HANDLE while a (similarly hypothetical) DELETE_CHAIN command takes a U32 CHAIN_HANDLE!

If we want to avoid the extra code for the netlink description, and reuse the existing policy objects, then we'll need policies for each command that express what attributes make sense per command as you describe above.

Quoted 5 lines

Hence my thought of separating "this is the policy for the attributes (types)" and "this is the list of allowed types for this command". I do realize now that the latter becomes difficult with nested attributes though, but we're probably better off finding ways to express that than having entirely different policies (also, the data would be smaller).

The netlink description is telling what attributes are available for each command and policies, so policy definitions are a subset of the netlink description.

If I understand well, your concern is that you would like that we use reuse / extend the existing policy structures to be used for the netlink description. That's very reasonable indeed and a good idea.

Quoted 44 lines

[[[ total aside: we could do something like the "list of allowed types" being an array of

union valid_type {
   u16 attr;
   struct nested *nested;
};

with

struct nested {
u16 attr;
union valid_type *valid_nested_types;
};

I think we can probably safely assume that pointers are always >>MAX_U16 but if not we can either make that larger or find other ways of disambiguating, e.g. by alignment (and making ATTR_TO_PTR(attr) something like "(attr << 1) | 0x1" since "struct nested" requires 2-byte alignment.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

If we use the list policies that you propose, then it's just an extra enumeration to maintain for each command. And many commands will likely reuse the same object ID.

A policy pointer, really.

Sort of, but it has to be stable along time for userspace, right? Actually, it's an ID.

The ID in my policy export is not stable in any way. The only thing that it's used for is to identify which sub-policy is used while you're dumping it. It will be stable by virtue of the algorithm, but will change when you add different sub-policy links etc. You cannot rely on it being stable, but you also do not need to since it's only relevant that you are able to identify the nested policy while dumping.

Quoted 7 lines
Quoted 2 lines

The list of policies is just built internally when you dump out a policy with its sub-policies for nested attributes/arrays.

If we expose these to userspace, we need that these object IDs are stable, hence the enum. So userspace results in a simple program that just makes look ups for the object ID that contains the description of the attributes that are available.

Quoted 5 lines

Well, no.

You're now thinking of the "policy ID" I assigned for the wire format as the object ID, but really that's not what it is. The object ID that you're looking for is the attribute type of the nested attribute.

I'm attaching userspace code for the netlink description infrastructure for libmnl. It should be easy to rework it to build a flat table with these IDs. The IDs are helping to provide a easy way to express this graph in a data structure that is easy to navigate from userspace.

My usecase is this: I don't want to probe the kernel to guess if a command / attribute is available for this kernel version or not. I just want to dump the netlink description, then navigate this table to search if that command / attribute is there.

Quoted 4 lines

So if you have

struct nla_policy nested_policy[...] = { ... };

Quoted 8 lines

struct nla_policy policy[...] = {
[MY_ATTR] = NLA_POLICY_NESTED(nested_policy),
};

and you dump out starting from "policy" then yes, "policy" will have ID 0, and "nested_policy" will have ID 1, but those are only temporary identifiers for the dump. What's really relevant is the attribute type "MY_ATTR".

Do you have userspace I can have a look? With runtime / dynamic ID generation, I cannot see yet how to navigate such a table or how the userspace representation would look like.

By using MY_ATTR as ID, I think you will have to build a graph structure in userspace, I was trying to provide a simple flat table representation for userspace.

Thanks!

commit 7826d6aa47d20bc09f7c8e33a457a5a338a8db55

Author: Pablo Neira Ayuso <[email protected]>
Date:   Tue Jan 16 00:05:37 2018 +0100

examples: add netlink bus description

Add nft-dump-desc-cmds.c and nft-dump-desc-obj.c to dump command and object descriptions.

examples/Makefile.am +11 -0
index e5cb052b315c..a8d4ba50f5ad 100644
@@ -1 +1,12 @@
+include $(top_srcdir)/Make_global.am
+
 SUBDIRS = genl kobject netfilter rtnl
+
+check_PROGRAMS = nft-dump-desc-cmds \
+                 nft-dump-desc-objs
+
+nft_dump_desc_cmds_SOURCES = nft-dump-desc-cmds.c
+nft_dump_desc_cmds_LDADD = ../src/libmnl.la
+
+nft_dump_desc_objs_SOURCES = nft-dump-desc-objs.c
+nft_dump_desc_objs_LDADD = ../src/libmnl.la
examples/nft-dump-desc-cmds.c new file +177 -0

177 lines not on this page · raw message

examples/nft-dump-desc-objs.c new file +263 -0

263 lines not on this page · raw message

#23 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 11 lines · Pablo Neira Ayuso
Quoted 6 lines

So I actually think that separating "type description", which is the policy today, from the "type validity description" is valuable, because ideally we do want each type (e.g. NFTA_CHAIN_HANDLE) to always be the same regardless of command, so that a hypothetical GET_STATS command won't take a U64 CHAIN_HANDLE while a (similarly hypothetical) DELETE_CHAIN command takes a U32 CHAIN_HANDLE!

If we want to avoid the extra code for the netlink description, and reuse the existing policy objects, then we'll need policies for each command that express what attributes make sense per command as you describe above.

Yes. Although when you say "policies", I'd argue that's not an "nla_policy" but rather some sort of "list of permitted attributes".

Quoted 6 lines
Quoted 5 lines

Hence my thought of separating "this is the policy for the attributes (types)" and "this is the list of allowed types for this command". I do realize now that the latter becomes difficult with nested attributes though, but we're probably better off finding ways to express that than having entirely different policies (also, the data would be smaller).

Quoted 7 lines

The netlink description is telling what attributes are available for each command and policies, so policy definitions are a subset of the netlink description.

If I understand well, your concern is that you would like that we use reuse / extend the existing policy structures to be used for the netlink description. That's very reasonable indeed and a good idea.

Yes, mostly. My bigger concern isn't really that we reuse it, my bigger concern is that if we *don't* we'll eventually end up with something like:

struct nla_policy policy_for_one_command[] = {
[NLA_MY_ATTR] = { .type = NLA_U32 },
...
};

struct nla_policy policy_for_another_command[] = {
[NLA_MY_ATTR] = { .type = NLA_U16 },
...
};

and then we just have a big mess. So I'd like to separate the "type" policy (what type is each attribute in a given netlink type enum) from the "permissible" policy (which attributes are valid in a given context like a command).

I've not actually started working on the second part at all yet, but because I strongly believe it makes sense to separate them, I don't see a need to interleave it with the rest of the patch series.

Quoted 9 lines
Quoted 3 lines

You're now thinking of the "policy ID" I assigned for the wire format as the object ID, but really that's not what it is. The object ID that you're looking for is the attribute type of the nested attribute.

I'm attaching userspace code for the netlink description infrastructure for libmnl. It should be easy to rework it to build a flat table with these IDs. The IDs are helping to provide a easy way to express this graph in a data structure that is easy to navigate from userspace.

Right.

Quoted 4 lines

My usecase is this: I don't want to probe the kernel to guess if a command / attribute is available for this kernel version or not. I just want to dump the netlink description, then navigate this table to search if that command / attribute is there.

Sure.

The only difference between the "object ID" that you manually assign and the "policy ID" that my code automatically assigns is that the latter is somewhat ephemeral - a new kernel version may change it.

So, let's say you have
struct nla_policy policy[...] = {
[NLA_MY_OBJECT] = NLA_POLICY_NESTED(sub_policy), [NLA_ANOTHER_OBJ] = NLA_POLICY_NESTED_ARRAY(sub_policy),
};

Then in one kernel version you might get reported to userspace:

NLA_MY_OBJECT: nested - policy 7
NLA_ANOTHER_OBJ: nested array - policy 7

and in a new kernel version the value "7" there might change to be 8 or 6 or whatever depending on changes in any possible nested policy for other attributes, but it would still always be the same number (unless you actually changed the fact that both link to "sub_policy" in the C code description), and the contents of that referenced policy would still all be the same as before. Just the ID that links them together at runtime would be more ephemeral than the object ID you proposed.

I don't, however, think that this is a problem as I don't see any value in the object ID itself. It's just an ID to tell you what kind of sub- attributes are allowed.

Quoted 8 lines
Quoted 8 lines

So if you have

struct nla_policy nested_policy[...] = { ... };

struct nla_policy policy[...] = {
[MY_ATTR] = NLA_POLICY_NESTED(nested_policy),
};

Quoted 8 lines
Quoted 4 lines

and you dump out starting from "policy" then yes, "policy" will have ID 0, and "nested_policy" will have ID 1, but those are only temporary identifiers for the dump. What's really relevant is the attribute type "MY_ATTR".

Do you have userspace I can have a look? With runtime / dynamic ID generation, I cannot see yet how to navigate such a table or how the userspace representation would look like.

I posted a very hacky patch before, let me see if I can find it...

https://p.sipsolutions.net/4c674acaf8d6ca71.txt

Quoted 3 lines

By using MY_ATTR as ID, I think you will have to build a graph structure in userspace, I was trying to provide a simple flat table representation for userspace.

I'm not using MY_ATTR as ID. I'm just making up an (ephemeral) object ID at runtime based on the policy pointer. See above though.

johannes

#24 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

In reply to Johannes Berg
Quoted 3 lines · Johannes Berg

Yeah, ok. Each object you had is basically its own policy. I just *removed* having a separate policy for each command in generic netlink, as ;-)

But that only affects genetlink users who let genetlink validate and parse messages for them. Those validating/parsing the messages themselves can still have different policy (and completely different set of attributes) for each command.

Michal

#25 lore raw

Re: [PATCH RFC 4/4] netfilter: nf_tables: add netlink description

Quoted 9 lines · Michal Kubecek
Quoted 3 lines · Johannes Berg

Yeah, ok. Each object you had is basically its own policy. I just *removed* having a separate policy for each command in generic netlink, as ;-)

But that only affects genetlink users who let genetlink validate and parse messages for them. Those validating/parsing the messages themselves can still have different policy (and completely different set of attributes) for each command.

Sure. I already argued elsewhere though that you should only have one policy for each set of attributes, but if you do in fact have different attributes then that's perfectly valid.

Not sure whether I think it all that reasonable, since you then burden userspace with having to know even more intricate detail and not being able to share code well between commands, but hey :-)

johannes