Skip to contentSkip to conversation

named expressions for nf_tables

RFC PATCH In review Pablo Neira Ayuso 3 participants 11 messages Apr 6, 2016 – Apr 20, 2016

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover named expressions for nf_tables

Hi,

This patchset introduces the named stateful expressions for nf_tables, that allows userspace to set a name for the stateful expression for several reasons:

* Provide a unique identifier to fetch and reset it internal state.
* Allow to update of their parameters and internal state.
* Allow to fetch and reset its internal state.
* Refer to the same stateful expression from one or more rules.

nf_tables currently supports to stateful expressions: limit and counter, therefore you can create named instances of this expressions through this new infrastructure.

This batch is composed of three patches:

1) Extend the nf_tables netlink interface to add, delete, dump and reset named expressions.

2) Add a new expression to dereference named expressions.

3) Support atomic dump and reset of named expressions.

Note that this provides a native replacement for the iptables' nfacct infrastructure, the use of nfacct is out of question due to the lack of integration with nftables netlink interface and its 2-phase commit protocol.

Several examples on how this would look from userspace:

* Add the 'tcp-counter' counter to the 'filter' table:

 # nft add counter filter tcp-counter

* Delete this counter (only possible if not dereferenced from a rule):

 # nft delete counter filter tcp-counter

* List existing named counters:

 # nft lists counters
 table ip filter {
        counter tcp-counter {
                packets 6086 bytes 6278052
        }
        counter udp-counter {
                packets 272 bytes 64690
        }
        counter icmp-counter {
                packets 10 bytes 840
        }
 }

* Atomically fetch and reset counters:

 # nft reset counters
 table ip filter {
        counter tcp-counter {
                packets 6086 bytes 6278052
        }
        counter udp-counter {
                packets 272 bytes 64690
        }
        counter icmp-counter {
                packets 10 bytes 840
        }
 }

This retrieves the existing counter values and reset them, a follow up listing confirms this:

 # nft list counters
 table ip filter {
        counter tcp-counter {
                packets 0 bytes 0
        }
        counter udp-counter {
                packets 0 bytes 0
        }
        counter icmp-counter {
                packets 0 bytes 0
        }
 }

The snippet below shows a simplistic configuration to account tcp, udp and icmp traffic through the named counter:

-o-

table ip filter {
        counter tcp-counter {
                packets 6086 bytes 6278052
        }

        counter udp-counter {
                packets 272 bytes 64690
        }

        counter icmp-counter {
                packets 10 bytes 840
        }

        chain input {
                type filter hook input priority 0; policy accept;
                ip protocol vmap { tcp : jump tcp-chain, icmp : jump icmp-chain, udp : jump udp-chain}
        }

        chain output {
                type filter hook output priority 0; policy accept;
        }

        chain tcp-chain {
                counter name tcp-counter
        }

        chain udp-chain {
                counter name udp-counter
        }

        chain icmp-chain {
                counter name icmp-counter
        }
}

-o-

So far, only counters are supported, but it should be possible to support named limits. I have another (imcomplete) patch that allows to update the named expressions parameters, this can be useful to dynamically update the ratelimiting policies, the command line should look like:

 # nft update limit name user01234 rate 250 mbytes/day

This example above updates the existing ratelimit 'user01234' to 250 mbytes/day.

Comments welcome,
Thanks.

Pablo Neira Ayuso (3):
netfilter: nf_tables: add stateful named expressions
netfilter: nf_tables: support for named expression reference
netfilter: nf_tables: support dump and reset for named expressions

 include/net/netfilter/nf_tables.h        |  32 +++
 include/uapi/linux/netfilter/nf_tables.h |  41 +++
 net/netfilter/Kconfig                    |   6 +
 net/netfilter/Makefile                   |   1 +
 net/netfilter/nf_tables_api.c            | 453 ++++++++++++++++++++++++++++++-
 net/netfilter/nft_counter.c              |  36 ++-
 net/netfilter/nft_nexpr.c                | 112 ++++++++
 7 files changed, 667 insertions(+), 14 deletions(-)
 create mode 100644 net/netfilter/nft_nexpr.c

--
2.1.4

author maintainer
#1 lore raw

RFC PATCH 1/3 netfilter: nf_tables: add stateful named expressions

Users can define named counters in iptables through the nfacct infrastructure. This extended accounting infrastructure provides a netlink interface to create counters, that are uniquely identified by a name, to fetch them from userspace; and to (atomically) fetch and reset them.

In nf_tables, the use of the existing nfacct infrastructure would not integrate nicely with its netlink interface and its 2-phase commit protocol. For that reason, the use of nfacct is out of question.

This patch extends the nf_tables netlink interface to allow the creation, deletion and dump of these stateful named expression from userspace.

This patch introduces a generic infrastructure for stateful named expressions, that allows userspace to set a name for the stateful expression for several reason: 1) to provide a unique identifier to fetch and reset it internal state, 2) to update of their parameters and internal state, 3) to fetch and reset its internal state, and
4) to refer to the same stateful expression from one or more rules.

---
 include/net/netfilter/nf_tables.h        |  25 ++
 include/uapi/linux/netfilter/nf_tables.h |  25 ++
 net/netfilter/nf_tables_api.c            | 393 +++++++++++++++++++++++++++++++
 3 files changed, 443 insertions(+)
include/net/netfilter/nf_tables.h +25 -0
index f6b1daf..0ba91ac 100644
@@ -653,6 +653,22 @@ struct nft_expr {
 	unsigned char			data[];
 };
 
+/**
+ *	struct nft_nexpr - nf_tables named expression
+ *
+ *	@list: table named expression list node
+ *	@name: name of this expression
+ *	@use: number of references to this named expression
+ *	@expr: pointer to expression object
+ */
+struct nft_nexpr {
+	struct list_head		list;
+	char				name[NFT_NEXPR_MAXNAMELEN];
+	u32				flags;
+	u32				use;
+	struct nft_expr			*expr;
+};
+
 static inline void *nft_expr_priv(const struct nft_expr *expr)
 {
 	return (void *)expr->data;
@@ -835,6 +851,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
  *	@list: used internally
  *	@chains: chains in the table
  *	@sets: sets in the table
+ *	@nexprs: named expression in the table
  *	@hgenerator: handle generator state
  *	@use: number of chain references to this table
  *	@flags: table flag (see enum nft_table_flags)
@@ -844,6 +861,7 @@ struct nft_table {
 	struct list_head		list;
 	struct list_head		chains;
 	struct list_head		sets;
+	struct list_head		nexprs;
 	u64				hgenerator;
 	u32				use;
 	u16				flags;
@@ -1088,4 +1106,11 @@ struct nft_trans_elem {
 #define nft_trans_elem(trans)	\
 	(((struct nft_trans_elem *)trans->data)->elem)
 
+struct nft_trans_nexpr {
+	struct nft_nexpr		*nexpr;
+};
+
+#define nft_trans_nexpr(trans)	\
+	(((struct nft_trans_nexpr *)trans->data)->nexpr)
+
 #endif /* _NET_NF_TABLES_H */
include/uapi/linux/netfilter/nf_tables.h +25 -0
index be41ffc..c1e19c3 100644
@@ -3,6 +3,7 @@
 
 #define NFT_TABLE_MAXNAMELEN	32
 #define NFT_CHAIN_MAXNAMELEN	32
+#define NFT_NEXPR_MAXNAMELEN	32
 #define NFT_USERDATA_MAXLEN	256
 
 /**
@@ -84,6 +85,9 @@ enum nft_verdicts {
  * @NFT_MSG_NEWGEN: announce a new generation, only for events (enum nft_gen_attributes)
  * @NFT_MSG_GETGEN: get the rule-set generation (enum nft_gen_attributes)
  * @NFT_MSG_TRACE: trace event (enum nft_trace_attributes)
+ * @NFT_MSG_NEWNEXPR: create a new named expression (enum nft_nexpr_attributes)
+ * @NFT_MSG_GETNEXPR: get a named expression (enum nft_nexpr_attributes)
+ * @NFT_MSG_DELNEXPR: delete a named expression (enum nft_nexpr_attributes)
  */
 enum nf_tables_msg_types {
 	NFT_MSG_NEWTABLE,
@@ -104,6 +108,9 @@ enum nf_tables_msg_types {
 	NFT_MSG_NEWGEN,
 	NFT_MSG_GETGEN,
 	NFT_MSG_TRACE,
+	NFT_MSG_NEWNEXPR,
+	NFT_MSG_GETNEXPR,
+	NFT_MSG_DELNEXPR,
 	NFT_MSG_MAX,
 };
 
@@ -416,6 +423,24 @@ enum nft_verdict_attributes {
 #define NFTA_VERDICT_MAX	(__NFTA_VERDICT_MAX - 1)
 
 /**
+ * enum nft_nexpr_attributes - nf_tables named expression netlink attributes
+ *
+ * @NFTA_NEXPR_TABLE: name of the table containing the expression (NLA_STRING)
+ * @NFTA_NEXPR_NAME: name of this expression type (NLA_STRING)
+ * @NFTA_NEXPR_EXPR: expression data (NLA_NESTED: nft_expr_attributes)
+ * @NFTA_NEXPR_USE: number of references to this expression (NLA_U32)
+ */
+enum nft_nexpr_attributes {
+	NFTA_NEXPR_UNSPEC,
+	NFTA_NEXPR_TABLE,
+	NFTA_NEXPR_NAME,
+	NFTA_NEXPR_EXPR,
+	NFTA_NEXPR_USE,
+	__NFTA_NEXPR_MAX
+};
+#define NFTA_NEXPR_MAX		(__NFTA_NEXPR_MAX - 1)
+
+/**
  * enum nft_expr_attributes - nf_tables expression netlink attributes
  *
  * @NFTA_EXPR_NAME: name of the expression type (NLA_STRING)
net/netfilter/nf_tables_api.c +393 -0

453 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#2 lore raw

RFC PATCH 2/3 netfilter: nf_tables: support for named expression reference

In reply to Pablo Neira Ayuso

This patch adds the 'nexpr' expression, this expression allows us to refer to existing named expressions. This generic expression can be used from rules and set elements.

This patch also adds nft_nexpr_lookup() to the core, as this new expression requires this function.

---
 include/net/netfilter/nf_tables.h        |   4 ++
 include/uapi/linux/netfilter/nf_tables.h |  14 ++++
 net/netfilter/Kconfig                    |   6 ++
 net/netfilter/Makefile                   |   1 +
 net/netfilter/nf_tables_api.c            |  24 +++++--
 net/netfilter/nft_nexpr.c                | 112 +++++++++++++++++++++++++++++++
 6 files changed, 154 insertions(+), 7 deletions(-)
 create mode 100644 net/netfilter/nft_nexpr.c
include/net/netfilter/nf_tables.h +4 -0
index 0ba91ac..a84f787 100644
@@ -669,6 +669,10 @@ struct nft_nexpr {
 	struct nft_expr			*expr;
 };
 
+struct nft_nexpr *nft_nexpr_lookup(const struct nft_table *table,
+				   const struct nlattr * const nla_name,
+				   const struct nlattr * const nla_type);
+
 static inline void *nft_expr_priv(const struct nft_expr *expr)
 {
 	return (void *)expr->data;
include/uapi/linux/netfilter/nf_tables.h +14 -0
index c1e19c3..f07b384 100644
@@ -1025,6 +1025,20 @@ enum nft_fwd_attributes {
 #define NFTA_FWD_MAX	(__NFTA_FWD_MAX - 1)
 
 /**
+ * enum nft_nexpr_ref_attributes - nf_tables named expression reference netlink attributes
+ *
+ * @NFTA_NEXPR_REF_NAME: name of this named expression (NLA_STRING)
+ * @NFTA_NEXPR_REF_TYPE: type of this named expression (NLA_STRING)
+ */
+enum nft_nexpr_ref_attributes {
+	NFTA_NEXPR_REF_UNSPEC,
+	NFTA_NEXPR_REF_NAME,
+	NFTA_NEXPR_REF_TYPE,
+	__NFTA_NEXPR_REF_MAX
+};
+#define NFTA_NEXPR_REF_MAX	(__NFTA_NEXPR_REF_MAX - 1)
+
+/**
  * enum nft_gen_attributes - nf_tables ruleset generation attributes
  *
  * @NFTA_GEN_ID: Ruleset generation ID (NLA_U32)
net/netfilter/Kconfig +6 -0
index 95e757c..251f9d2 100644
@@ -535,6 +535,12 @@ config NFT_NAT
 	  This option adds the "nat" expression that you can use to perform
 	  typical Network Address Translation (NAT) packet transformations.
 
+config NFT_NEXPR
+	tristate "Netfilter nf_tables named expression module"
+	help
+	  This option adds the "named" expression that you can use to
+	  refer an existing named expression.
+
 config NFT_QUEUE
 	depends on NETFILTER_NETLINK_QUEUE
 	tristate "Netfilter nf_tables queue module"
net/netfilter/Makefile +1 -0
index 6913454..2ba1388 100644
@@ -83,6 +83,7 @@ obj-$(CONFIG_NFT_META)		+= nft_meta.o
 obj-$(CONFIG_NFT_CT)		+= nft_ct.o
 obj-$(CONFIG_NFT_LIMIT)		+= nft_limit.o
 obj-$(CONFIG_NFT_NAT)		+= nft_nat.o
+obj-$(CONFIG_NFT_NEXPR)		+= nft_nexpr.o
 obj-$(CONFIG_NFT_QUEUE)		+= nft_queue.o
 obj-$(CONFIG_NFT_REJECT) 	+= nft_reject.o
 obj-$(CONFIG_NFT_REJECT_INET)	+= nft_reject_inet.o
net/netfilter/nf_tables_api.c +17 -7
index b542d20..e506dca 100644
@@ -3800,11 +3800,25 @@ err:
 	return err;
 }
 
+struct nft_nexpr *nft_nexpr_lookup(const struct nft_table *table,
+				   const struct nlattr * const nla_name,
+				   const struct nlattr * const nla_type)
+{
+	struct nft_nexpr *nexpr;
+
+	list_for_each_entry(nexpr, &table->nexprs, list) {
+		if (!nla_strcmp(nla_name, nexpr->name) &&
+		    !nla_strcmp(nla_type, nexpr->expr->ops->type->name))
+			return nexpr;
+	}
+	return ERR_PTR(-ENOENT);
+}
+EXPORT_SYMBOL_GPL(nft_nexpr_lookup);
+
 static struct nft_nexpr *nf_tables_nexpr_lookup(const struct nft_table *table,
 						const struct nlattr * const nla[])
 {
 	struct nlattr *tb[NFTA_EXPR_MAX + 1];
-	struct nft_nexpr *nexpr;
 	int err;
 
 	if (!nla[NFTA_NEXPR_NAME] ||
@@ -3819,12 +3833,8 @@ static struct nft_nexpr *nf_tables_nexpr_lookup(const struct nft_table *table,
 	if (!tb[NFTA_EXPR_NAME])
 		return ERR_PTR(-EINVAL);
 
-	list_for_each_entry(nexpr, &table->nexprs, list) {
-		if (!nla_strcmp(nla[NFTA_NEXPR_NAME], nexpr->name) &&
-		    !nla_strcmp(tb[NFTA_EXPR_NAME], nexpr->expr->ops->type->name))
-			return nexpr;
-	}
-	return ERR_PTR(-ENOENT);
+	return nft_nexpr_lookup(table, nla[NFTA_NEXPR_NAME],
+				tb[NFTA_EXPR_NAME]);
 }
 
 static const struct nla_policy nft_nexpr_policy[NFTA_NEXPR_MAX + 1] = {
net/netfilter/nft_nexpr.c new file +112 -0

112 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#3 lore raw

RFC PATCH 3/3 netfilter: nf_tables: support dump and reset for named expressions

In reply to Pablo Neira Ayuso

This patch adds a new NFT_MSG_GETNEXPR_RESET command to dump and to atomically reset the internal state of the named expression.

Stateful expressions may implement the new reset() interface to allow the reset of this named expressions.

This patch comes with the first client of it: the nft_counter expression.

---
 include/net/netfilter/nf_tables.h        |  3 ++
 include/uapi/linux/netfilter/nf_tables.h |  2 +
 net/netfilter/nf_tables_api.c            | 66 +++++++++++++++++++++++++-------
 net/netfilter/nft_counter.c              | 36 +++++++++++++----
 4 files changed, 85 insertions(+), 22 deletions(-)
include/net/netfilter/nf_tables.h +3 -0
index a84f787..8c39268 100644
@@ -611,6 +611,7 @@ struct nft_expr_type {
  *	@init: initialization function
  *	@destroy: destruction function
  *	@dump: function to dump parameters
+ *	@reset: function to dump parameters and to reset internal state
  *	@type: expression type
  *	@validate: validate expression, called during loop detection
  *	@data: extra data to attach to this expression operation
@@ -631,6 +632,8 @@ struct nft_expr_ops {
 						   const struct nft_expr *expr);
 	int				(*dump)(struct sk_buff *skb,
 						const struct nft_expr *expr);
+	int				(*reset)(struct sk_buff *skb,
+						 const struct nft_expr *expr);
 	int				(*validate)(const struct nft_ctx *ctx,
 						    const struct nft_expr *expr,
 						    const struct nft_data **data);
include/uapi/linux/netfilter/nf_tables.h +2 -0
index f07b384..644c1dd 100644
@@ -88,6 +88,7 @@ enum nft_verdicts {
  * @NFT_MSG_NEWNEXPR: create a new named expression (enum nft_nexpr_attributes)
  * @NFT_MSG_GETNEXPR: get a named expression (enum nft_nexpr_attributes)
  * @NFT_MSG_DELNEXPR: delete a named expression (enum nft_nexpr_attributes)
+ * @NFT_MSG_GETNEXPR_RESET: get and reset a named expression (enum nft_nexpr_attributes)
  */
 enum nf_tables_msg_types {
 	NFT_MSG_NEWTABLE,
@@ -111,6 +112,7 @@ enum nf_tables_msg_types {
 	NFT_MSG_NEWNEXPR,
 	NFT_MSG_GETNEXPR,
 	NFT_MSG_DELNEXPR,
+	NFT_MSG_GETNEXPR_RESET,
 	NFT_MSG_MAX,
 };
 
net/netfilter/nf_tables_api.c +52 -14

149 lines not on this page · raw message

net/netfilter/nft_counter.c +28 -8
index c9743f7..eec5d63 100644
@@ -47,21 +47,29 @@ static void nft_counter_eval(const struct nft_expr *expr,
 	local_bh_enable();
 }
 
-static void nft_counter_fetch(const struct nft_counter_percpu __percpu *counter,
-			      struct nft_counter *total)
+static void nft_counter_fetch(struct nft_counter_percpu __percpu *counter,
+			      struct nft_counter *total, bool reset)
 {
-	const struct nft_counter_percpu *cpu_stats;
+	struct nft_counter_percpu *cpu_stats;
 	u64 bytes, packets;
 	unsigned int seq;
 	int cpu;
 
 	memset(total, 0, sizeof(*total));
 	for_each_possible_cpu(cpu) {
+		if (reset)
+			bytes = packets = 0;
+
 		cpu_stats = per_cpu_ptr(counter, cpu);
 		do {
 			seq	= u64_stats_fetch_begin_irq(&cpu_stats->syncp);
-			bytes	= cpu_stats->counter.bytes;
-			packets	= cpu_stats->counter.packets;
+			if (reset) {
+				packets += xchg(&cpu_stats->counter.packets, 0);
+				bytes	+= xchg(&cpu_stats->counter.bytes, 0);
+			} else {
+				bytes	= cpu_stats->counter.bytes;
+				packets	= cpu_stats->counter.packets;
+			}
 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
 
 		total->packets += packets;
@@ -69,12 +77,13 @@ static void nft_counter_fetch(const struct nft_counter_percpu __percpu *counter,
 	}
 }
 
-static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
+static int __nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr,
+			      bool reset)
 {
 	struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
 	struct nft_counter total;
 
-	nft_counter_fetch(priv->counter, &total);
+	nft_counter_fetch(priv->counter, &total, reset);
 
 	if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)) ||
 	    nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets)))
@@ -85,6 +94,16 @@ nla_put_failure:
 	return -1;
 }
 
+static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
+{
+	return __nft_counter_dump(skb, expr, false);
+}
+
+static int nft_counter_reset(struct sk_buff *skb, const struct nft_expr *expr)
+{
+	return __nft_counter_dump(skb, expr, true);
+}
+
 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
 	[NFTA_COUNTER_PACKETS]	= { .type = NLA_U64 },
 	[NFTA_COUNTER_BYTES]	= { .type = NLA_U64 },
@@ -133,7 +152,7 @@ static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
 	struct nft_counter_percpu *this_cpu;
 	struct nft_counter total;
 
-	nft_counter_fetch(priv->counter, &total);
+	nft_counter_fetch(priv->counter, &total, false);
 
 	cpu_stats = __netdev_alloc_pcpu_stats(struct nft_counter_percpu,
 					      GFP_ATOMIC);
@@ -158,6 +177,7 @@ static const struct nft_expr_ops nft_counter_ops = {
 	.init		= nft_counter_init,
 	.destroy	= nft_counter_destroy,
 	.dump		= nft_counter_dump,
+	.reset		= nft_counter_reset,
 	.clone		= nft_counter_clone,
 };
 
Signature · 1 line
2.1.4
maintainer
#4 lore raw
In reply to Pablo Neira Ayuso
Quoted 9 lines · Pablo Neira Ayuso

This patchset introduces the named stateful expressions for nf_tables, that allows userspace to set a name for the stateful expression for several reasons:

* Provide a unique identifier to fetch and reset it internal state.
* Allow to update of their parameters and internal state.
* Allow to fetch and reset its internal state.
* Refer to the same stateful expression from one or more rules.

Can't all of this be achieved via custom chains already...?

Quoted 16 lines

nf_tables currently supports to stateful expressions: limit and counter, therefore you can create named instances of this expressions through this new infrastructure.

Several examples on how this would look from userspace:

* Add the 'tcp-counter' counter to the 'filter' table:

 # nft add counter filter tcp-counter

* Delete this counter (only possible if not dereferenced from a rule):

 # nft delete counter filter tcp-counter

* List existing named counters:

Quoted 12 lines
 # nft lists counters
 table ip filter {
        counter tcp-counter {
                packets 6086 bytes 6278052
        }
        counter udp-counter {
                packets 272 bytes 64690
        }
        counter icmp-counter {
                packets 10 bytes 840
        }
 }

Can't you already create a 'named counter' via

chain tcp-counter {
counter
}

?

Seems to me we only would have to teach nft list to allow filtering output on chain names, e.g.

nft list table filter \*-counter

to have something similar using exsiting feature set.

Quoted 7 lines

So far, only counters are supported, but it should be possible to support named limits. I have another (imcomplete) patch that allows to update the named expressions parameters, this can be useful to dynamically update the ratelimiting policies, the command line should look like:

 # nft update limit name user01234 rate 250 mbytes/day

Similar comment here, you could create

chain user01234 {
limit rate 100 mbytes/day
}

This feature seems strange to me, AFAIU it adds a container for single expressions, so it just seems to be a subset of what we already have (namely chains as containers of rules).

What am I missing? :)

Is there something that is too cumbersome to achive with a chain-based approach?

author maintainer
#5 lore raw
Quoted 32 lines · Florian Westphal
Quoted 12 lines · Pablo Neira Ayuso

Several examples on how this would look from userspace:

* Add the 'tcp-counter' counter to the 'filter' table:

 # nft add counter filter tcp-counter

* Delete this counter (only possible if not dereferenced from a rule):

 # nft delete counter filter tcp-counter

* List existing named counters:

Quoted 12 lines
 # nft lists counters
 table ip filter {
        counter tcp-counter {
                packets 6086 bytes 6278052
        }
        counter udp-counter {
                packets 272 bytes 64690
        }
        counter icmp-counter {
                packets 10 bytes 840
        }
 }

Can't you already create a 'named counter' via

chain tcp-counter {
counter
}

Quoted 8 lines

?

Seems to me we only would have to teach nft list to allow filtering output on chain names, e.g.

nft list table filter \*-counter

to have something similar using exsiting feature set.

Right, but we still need a way to uniquely identify this stateful expression for atomic dump'n'reset.

Quoted 6 lines
Quoted 6 lines

So far, only counters are supported, but it should be possible to support named limits. I have another (imcomplete) patch that allows to update the named expressions parameters, this can be useful to dynamically update the ratelimiting policies, the command line should look like:

Quoted 7 lines
Quoted 1 line
 # nft update limit name user01234 rate 250 mbytes/day

Similar comment here, you could create

chain user01234 {
limit rate 100 mbytes/day
}

Right, we can replace this rule via the handle, but I see two problems with this:

1) The user needs to track the rule handle, not so much of a problem since userspace can do this at the cost of a bit of more complexity.

2) When replacing the rule, the new expression starts from no history as it will be a new expression, we'll be basically reseting it.

Using 'nft update limit...' I think we should be capable of upgrading the ratelimit by keeping around the quota that the user has already consumed.

The idea is, if we know the previous cost of every byte/packets in terms of tokens, given that tokens are generated per nanosecond and that know how many tokens we have already consumed, then we can recalculate the quota that has been already consumed and add this to the limit update, eg. if limit rate is 100 mbytes/day but the user already consumed 50 mbytes, when calling:

nft update limit rate 200 mbytes/day

we can keep the 50 mbytes that were already consumed around.

Let me know, thanks.

maintainer
#6 lore raw
Quoted 2 lines · Pablo Neira Ayuso

2) When replacing the rule, the new expression starts from no history as it will be a new expression, we'll be basically reseting it.

You are right, I was only thinking of counter (where userspace can set packets/bytes), but limit would lose its state as most of that is internal only.

So I think #2 makes sense.

I have more concerns (sorry!) wrt user representation, I'll reply to this in a new email.

maintainer
#7 lore raw
In reply to Pablo Neira Ayuso
Quoted 14 lines · Pablo Neira Ayuso

* List existing named counters:

 # nft lists counters
 table ip filter {
        counter tcp-counter {
                packets 6086 bytes 6278052
        }
        counter udp-counter {
                packets 272 bytes 64690
        }
        counter icmp-counter {
                packets 10 bytes 840
        }
 }

So if we extend this scheme to all (stateful) expressions we'll have to add 'nft list limits' (or whatever).

Do you think it makes sense to represent this in a more generic fashion?

nft list tables
nft list chains

Maybe add
nft list expressions

?

Quoted 8 lines

The snippet below shows a simplistic configuration to account tcp, udp and icmp traffic through the named counter:

-o-

table ip filter {
        counter tcp-counter {
                packets 6086 bytes 6278052
        }

So this could f.e. look like

expression tcp-counter {
	counter packets 6086 bytes 6278052
}

(and tcp-counter is just some identifier).

Quoted 6 lines

support named limits. I have another (imcomplete) patch that allows to update the named expressions parameters, this can be useful to dynamically update the ratelimiting policies, the command line should look like:

 # nft update limit name user01234 rate 250 mbytes/day

nft update expression user01234 rate 250 mbytes/day

(or perhaps

nft update expression user01234 limit rate 250 mbytes/day

not sure if we want to allow replacing the (internal) expression stored in a named expression with a different one....)

#8 lore raw
In reply to Pablo Neira Ayuso

Hi,

Quoted 10 lines · Pablo Neira Ayuso

Hi,

This patchset introduces the named stateful expressions for nf_tables, that allows userspace to set a name for the stateful expression for several reasons:

* Provide a unique identifier to fetch and reset it internal state.
* Allow to update of their parameters and internal state.
* Allow to fetch and reset its internal state.
* Refer to the same stateful expression from one or more rules.

Would this support to have rules based on the value of a counter and/or the current rate (like quotas in nfacct)?

something like this:

nft add rule filter tcp-chain counter name tcp-counter bytes > 10000 jump deny

A more realistic setup might more look like:

table ip filter {
         counter counter-user1234 {
                 packets 6086 bytes 6278052
         }

         chain chain-user1234 {
                 counter name counter-user1234
		 counter name counter-user1234 > 10000000 goto chain-user1234-overlimit
		 counter name counter-user1234 > 500000 goto rate-limit
		 accept
         }

         chain chain-user1234-overlimit {
                 do-once notify userspace somehow
		 reject
         }
}

As far as I know there is currently no mechanism in nft that could do the "do-once notify userspace somehow", or is there???

The other issue I have with such a scheme that it requires lots of chains per client and might limit the number of clients that could be supported.

Regards
Andreas

author maintainer
#9 lore raw

Hi Andreas,

Quoted 9 lines · Andreas Schultz

Would this support to have rules based on the value of a counter and/or the current rate (like quotas in nfacct)?

something like this:

nft add rule filter tcp-chain counter name tcp-counter bytes > 10000 jump deny

A more realistic setup might more look like:

Quoted 16 lines
table ip filter {
         counter counter-user1234 {
                 packets 6086 bytes 6278052
         }

         chain chain-user1234 {
                 counter name counter-user1234
		 counter name counter-user1234 > 10000000 goto chain-user1234-overlimit
		 counter name counter-user1234 > 500000 goto rate-limit
		 accept
         }

         chain chain-user1234-overlimit {
                 do-once notify userspace somehow
		 reject
         }

I think we can express this with:

        nft add limit counter-user1234 rate over 100 mbytes/day
        nft add rule filter input \
                limit name counter-user1234 \
                log prefix "user1234" group 10 \
                reject

The idea is to create a 'counter-user1234' limit. Then refer to this from the rule.

BTW, currently the 'reject' statement will rely on icmp unreach to reject this. Probably you want a plain 'drop' here.

Quoted 2 lines

As far as I know there is currently no mechanism in nft that could do the "do-once notify userspace somehow", or is there???

You can do this through the log statement, and then use libnetfilter_log for your application.

Quoted 2 lines

The other issue I have with such a scheme that it requires lots of chains per client and might limit the number of clients that could be supported.

I think we can skip the extra non-base chains in the example above. The question is if you would be OK by specifying the quota using ratelimit, opposed to counter.

The idea is that you can reset limits with something like:

nft reset limit name user1234

so this basically resets the quota. Or even upgrade via:

nft update limit name user1234 over 1000 mbytes/day

(this would keep what it's been consumed already, so statefulness is preserved).

author maintainer
#10 lore raw
Quoted 27 lines · Pablo Neira Ayuso
Quoted 9 lines · Andreas Schultz

Would this support to have rules based on the value of a counter and/or the current rate (like quotas in nfacct)?

something like this:

nft add rule filter tcp-chain counter name tcp-counter bytes > 10000 jump deny

A more realistic setup might more look like:

Quoted 16 lines
table ip filter {
         counter counter-user1234 {
                 packets 6086 bytes 6278052
         }

         chain chain-user1234 {
                 counter name counter-user1234
		 counter name counter-user1234 > 10000000 goto chain-user1234-overlimit
		 counter name counter-user1234 > 500000 goto rate-limit
		 accept
         }

         chain chain-user1234-overlimit {
                 do-once notify userspace somehow
		 reject
         }
Quoted 7 lines

I think we can express this with:

        nft add limit counter-user1234 rate over 100 mbytes/day
        nft add rule filter input \
                limit name counter-user1234 \
                log prefix "user1234" group 10 \
                reject

Actually using the limit rate policer will not work for quota-like stuff since the budget gets refilled for each packet that is received.

Quoted 11 lines

The idea is to create a 'counter-user1234' limit. Then refer to this from the rule.

BTW, currently the 'reject' statement will rely on icmp unreach to reject this. Probably you want a plain 'drop' here.

Quoted 2 lines

As far as I know there is currently no mechanism in nft that could do the "do-once notify userspace somehow", or is there???

You can do this through the log statement, and then use libnetfilter_log for your application.

libnetfilter_log will keep spamming userspace after going overlimit. You most likely want a single report event notification to userspace.

Anyway, I understand your use case, will come back with an update on this.