nf_tables: add stateful objects
Subsystems (2)
Pablo Neira Ayuso
maintainercover nf_tables: add stateful objects
This patchset adds support for nf_tables stateful objects. Two object types are supported at this stage: counters and quotas. Stateful objects are uniquely identified by a user-defined name and you have to attach them to tables.
You can create a counter via:
# nft add table filter # nft add counter filter http-traffic
Then, list existing counters through:
# nft list counters
table ip filter {
counter http-traffic {
packets 0 bytes 0
}
}The counter and quota stateful object type definitions reside in nft_counter.c and nft_quota.c respectively, as they share code with these two stateful expressions. The new object reference (objref) expression allows us to refer to stateful objects from rules. Assuming you already have a base chain in place:
# nft add chain filter input { type filter hook input priority 0\; }You can refer to this counter from rules, eg.
# nft add rule filter input tcp dport 80 counter http-traffic
But adding one rule per object is expensive, so you can instead use our map infrastructure instead for fast lookups:
The following example shows how to use this through an anonymous map:
# nft add counter filter ftp-traffic
# nft add counter filter ssh-traffic
# nft add rule filter input counter name tcp dport map { \
20 : "ftp-traffic",
21 : "ftp-traffic",
22 : "ssh-traffic",
80 : "http-traffic",
443 : "http-traffic",
8080 : "http-traffic"
}The rule above update a given counter based on the destination tcp port.
The nf_tables codebase has been extended to add a new NFT_SET_OBJECT set flag that indicates that the set stores a mapping between any arbitrary key and an existing stateful object. There is also a new NFTA_SET_OBJTYPE attribute to indicate the stateful object type. Then, there is a new NFTA_SET_ELEM_OBJREF that allows us to specific the right hand side of the mapping using the string that uniquely identify the stateful object. The objref expression has been extended to take a map as parameter.
You also refer to stateful object from dynamic maps, eg.
# nft add map filter servers { type ipv4_addr . inet_service : counter \; }
# nft add rule filter input counter name ip daddr . tcp dport map @servers
# nft add counter filter www
# nft add counter filter ftp
# nft add element filter servers { 192.168.2.3 . 80 : "www" }
# nft add element filter servers { 192.168.2.4 . 20 : "ftp" }
# nft add element filter servers { 192.168.2.4 . 21 : "ftp" }You can also atomically dump-and-reset stateful objects through:
# nft reset counter filter www
table filter {
counter www {
packets 123489 bytes 748374399
}
}
# nft list counter filter www
table filter {
counter www {
packets 0 bytes 0
}
}As I said, this patch also comes with quota support, this also include new infrastructure to deliver event notifications to userspace via netlink whenever the quota has expired.
Comments welcome.
P.S: Limit stateful objects are not covered by this patchset, but it should be relatively easy to add them later.
Pablo Neira Ayuso (11):
netfilter: nf_tables: add stateful objects
netfilter: nft_counter: add stateful object type
netfilter: nft_quota: add stateful object type
netfilter: nf_tables: add stateful object reference expression
netfilter: nf_tables: atomic dump and reset for stateful objects
netfilter: nf_tables: notify internal updates of stateful objects
netfilter: nft_quota: dump consumed quota
netfilter: nft_quota: add depleted flag for objects
netfilter: nf_tables: add stateful object reference to set elements
netfilter: nft_objref: support for stateful object maps
netfilter: nf_tables: allow to filter stateful object dumps by type
include/net/netfilter/nf_tables.h | 91 +++++ include/uapi/linux/netfilter/nf_tables.h | 67 ++- net/netfilter/Kconfig | 6 + net/netfilter/Makefile | 1 + net/netfilter/nf_tables_api.c | 674 ++++++++++++++++++++++++++++++- net/netfilter/nft_counter.c | 138 +++++-- net/netfilter/nft_objref.c | 227 +++++++++++ net/netfilter/nft_quota.c | 116 +++++- 8 files changed, 1256 insertions(+), 64 deletions(-) create mode 100644 net/netfilter/nft_objref.c
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 1/11 netfilter: nf_tables: add stateful objects
This patch augments nf_tables to support stateful objects. This new infrastructure allows you to create, dump and delete stateful objects, that are identified by a user-defined name.
This patch adds the generic infrastructure, follow up patches add support for two stateful objects: counters and quotas.
This patch provides a native infrastructure for nf_tables to replace nfacct, the extended accounting infrastructure for iptables.
--- include/net/netfilter/nf_tables.h | 78 +++++ include/uapi/linux/netfilter/nf_tables.h | 29 ++ net/netfilter/nf_tables_api.c | 518 +++++++++++++++++++++++++++++++ 3 files changed, 625 insertions(+)
include/net/netfilter/nf_tables.h +78 -0
@@ -883,6 +883,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt);* @list: used internally * @chains: chains in the table * @sets: sets in the table +* @objects: stateful objects in the table * @hgenerator: handle generator state * @use: number of chain references to this table * @flags: table flag (see enum nft_table_flags)
@@ -893,6 +894,7 @@ struct nft_table {struct list_head list;struct list_head chains;struct list_head sets; +struct list_head objects;u64 hgenerator;u32 use; u16 flags:14,
@@ -943,6 +945,72 @@ int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v);/** +* struct nft_object - nf_tables stateful object +* +* @list: table stateful object list node +* @type: pointer to object type +* @data: pointer to object data +* @name: name of this stateful object +* @genmask: generation mask +* @use: number of references to this stateful object +* @data: object data, layout depends on type +*/ +struct nft_object { +struct list_head list; +const struct nft_object_type *type; +char name[NFT_OBJ_MAXNAMELEN ]; +u32 flags:30 , + genmask:2 ; +u32 use; +unsigned char data[]; +}; + +static inline void *nft_obj_data (struct nft_object *obj) +{ +return obj->data; +} + +#define nft_expr_obj (expr) *((struct nft_object **)nft_expr_priv(expr)) + +struct nft_object *nf_tables_obj_lookup (const struct nft_table *table, +const struct nlattr *nla,u32 objtype, +u8 genmask); + +/** +* struct nft_object_type - stateful object type +* +* @eval: stateful object evaluation function +* @list: list node in list of object types +* @type: stateful object numeric type +* @size: stateful object size +* @owner: module owner +* @maxattr: maximum netlink attribute +* @policy: netlink attribute policy +* @init: initialize object from netlink attributes +* @destroy: release existing stateful object +* @dump: netlink dump stateful object +*/ +struct nft_object_type { +void (*eval)(void *obj, +struct nft_regs *regs, +const struct nft_pktinfo *pkt); +struct list_head list; +u32 type; +unsigned int size; +unsigned int maxattr; +struct module *owner; +const struct nla_policy *policy; +int (*init)(const struct nlattr *const tb[], +void *obj); +void (*destroy)(void *obj); +int (*dump)(struct sk_buff *skb, +const void *obj); +}; + +int nft_register_object (struct nft_object_type *obj_type); +void nft_unregister_object (struct nft_object_type *obj_type); + +/** * struct nft_traceinfo - nft tracing information and state * * @pkt: pktinfo currently processed
@@ -991,6 +1059,9 @@ void nft_trace_notify(struct nft_traceinfo *info);#define MODULE_ALIAS_NFT_SET () \ MODULE_ALIAS("nft-set") +#define MODULE_ALIAS_NFT_OBJ (type) \ + MODULE_ALIAS("nft-obj-" __stringify(type)) +/* * The gencursor defines two generations, the currently active and the * next one. Objects contain a bitmask of 2 bits specifying the generations
@@ -1167,4 +1238,11 @@ struct nft_trans_elem {#define nft_trans_elem (trans) \ (((struct nft_trans_elem *)trans->data)->elem) +struct nft_trans_obj { +struct nft_object *obj; +}; + +#define nft_trans_obj (trans) \ + (((struct nft_trans_obj *)trans->data)->obj) +#endif /* _NET_NF_TABLES_H */
include/uapi/linux/netfilter/nf_tables.h +29 -0
@@ -4,6 +4,7 @@#define NFT_TABLE_MAXNAMELEN 32#define NFT_CHAIN_MAXNAMELEN 32#define NFT_SET_MAXNAMELEN 32 +#define NFT_OBJ_MAXNAMELEN 32#define NFT_USERDATA_MAXLEN 256/**
@@ -85,6 +86,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_NEWOBJ: create a stateful object (enum nft_obj_attributes) +* @NFT_MSG_GETOBJ: get a stateful object (enum nft_obj_attributes) +* @NFT_MSG_DELOBJ: delete a stateful object (enum nft_obj_attributes) */ enum nf_tables_msg_types {NFT_MSG_NEWTABLE ,
@@ -105,6 +109,9 @@ enum nf_tables_msg_types {NFT_MSG_NEWGEN ,NFT_MSG_GETGEN ,NFT_MSG_TRACE , +NFT_MSG_NEWOBJ , +NFT_MSG_GETOBJ , +NFT_MSG_DELOBJ , NFT_MSG_MAX, };
@@ -1207,6 +1214,28 @@ enum nft_fib_flags { NFTA_FIB_F_OIF = 1 << 4, /* restrict to oif */ }; +#define NFT_OBJECT_UNSPEC 0 + +/** +* enum nft_object_attributes - nf_tables stateful object netlink attributes +* +* @NFTA_OBJ_TABLE: name of the table containing the expression (NLA_STRING) +* @NFTA_OBJ_NAME: name of this expression type (NLA_STRING) +* @NFTA_OBJ_TYPE: stateful object type (NLA_U32) +* @NFTA_OBJ_DATA: stateful object data (NLA_NESTED) +* @NFTA_OBJ_USE: number of references to this expression (NLA_U32) +*/ +enum nft_object_attributes { +NFTA_OBJ_UNSPEC , +NFTA_OBJ_TABLE , +NFTA_OBJ_NAME , +NFTA_OBJ_TYPE , +NFTA_OBJ_DATA , +NFTA_OBJ_USE , + __NFTA_OBJ_MAX +}; +#define NFTA_OBJ_MAX (__NFTA_OBJ_MAX - 1) +/** * enum nft_trace_attributes - nf_tables trace netlink attributes *
net/netfilter/nf_tables_api.c +518 -0
596 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/11 netfilter: nft_counter: add stateful object type
In reply to Pablo Neira AyusoRegister a new percpu counter stateful object type into the stateful object infrastructure.
--- include/uapi/linux/netfilter/nf_tables.h | 1 + net/netfilter/nft_counter.c | 116 ++++++++++++++++++++++++------- 2 files changed, 90 insertions(+), 27 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +1 -0
@@ -1215,6 +1215,7 @@ enum nft_fib_flags { };#define NFT_OBJECT_UNSPEC 0 +#define NFT_OBJECT_COUNTER 1/** * enum nft_object_attributes - nf_tables stateful object netlink attributes
net/netfilter/nft_counter.c +89 -27
168 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 3/11 netfilter: nft_quota: add stateful object type
In reply to Pablo Neira AyusoRegister a new quota stateful object type into the new stateful object infrastructure.
--- include/uapi/linux/netfilter/nf_tables.h | 1 + net/netfilter/nft_quota.c | 76 +++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 12 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +1 -0
@@ -1216,6 +1216,7 @@ enum nft_fib_flags {#define NFT_OBJECT_UNSPEC 0#define NFT_OBJECT_COUNTER 1 +#define NFT_OBJECT_QUOTA 2/** * enum nft_object_attributes - nf_tables stateful object netlink attributes
net/netfilter/nft_quota.c +64 -12
112 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 4/11 netfilter: nf_tables: add stateful object reference expression
In reply to Pablo Neira AyusoThis new expression allows us to refer to existing stateful objects from rules.
--- include/uapi/linux/netfilter/nf_tables.h | 17 ++++- net/netfilter/Kconfig | 6 ++ net/netfilter/Makefile | 1 + net/netfilter/nft_objref.c | 113 +++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 net/netfilter/nft_objref.c
include/uapi/linux/netfilter/nf_tables.h +16 -1
@@ -458,7 +458,8 @@ enum nft_verdict_attributes {#define NFT_EXPR_REDIR 27#define NFT_EXPR_REJECT 28#define NFT_EXPR_RT 29 -#define NFT_EXPR_MAX 30 +#define NFT_EXPR_OBJREF 30 +#define NFT_EXPR_MAX 31/** * enum nft_expr_attributes - nf_tables expression netlink attributes
@@ -1167,6 +1168,20 @@ enum nft_fwd_attributes {#define NFTA_FWD_MAX (__NFTA_FWD_MAX - 1)/** +* enum nft_objref_attributes - nf_tables stateful object expression netlink attributes +* +* @NFTA_OBJREF_TYPE: source register of output interface (NLA_U32: nft_register) +* @NFTA_OBJREF_NAME: source register of output interface (NLA_STRING) +*/ +enum nft_objref_attributes { +NFTA_OBJREF_UNSPEC , +NFTA_OBJREF_TYPE , +NFTA_OBJREF_NAME , + __NFTA_OBJREF_MAX +}; +#define NFTA_OBJREF_MAX (__NFTA_OBJREF_MAX - 1) + +/** * enum nft_gen_attributes - nf_tables ruleset generation attributes * * @NFTA_GEN_ID: Ruleset generation ID (NLA_U32)
net/netfilter/Kconfig +6 -0
@@ -551,6 +551,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_OBJREF + tristate "Netfilter nf_tables stateful object reference module" + help + This option adds the "objref" expression that allows you to refer to + stateful objects, such as counters and quotas. + config NFT_QUEUE depends on NETFILTER_NETLINK_QUEUE tristate "Netfilter nf_tables queue module"
net/netfilter/Makefile +1 -0
@@ -89,6 +89,7 @@ obj-$(CONFIG_NFT_NUMGEN) += nft_numgen.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_OBJREF) += nft_objref.o obj-$(CONFIG_NFT_QUEUE) += nft_queue.o obj-$(CONFIG_NFT_QUOTA) += nft_quota.o obj-$(CONFIG_NFT_REJECT) += nft_reject.o
net/netfilter/nft_objref.c new file +113 -0
113 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 5/11 netfilter: nf_tables: atomic dump and reset for stateful objects
In reply to Pablo Neira AyusoThis patch adds a new NFT_MSG_GETOBJ_RESET command perform an atomic dump-and-reset of the stateful object. This also comes with add support for atomic dump and reset for counter and quota objects.
--- include/net/netfilter/nf_tables.h | 2 +- include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 50 ++++++++++++++++++++++++-------- net/netfilter/nft_counter.c | 28 +++++++++++------- net/netfilter/nft_quota.c | 12 ++++++-- 5 files changed, 68 insertions(+), 26 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -1004,7 +1004,7 @@ struct nft_object_type { void *obj);void (*destroy)(void *obj);int (*dump)(struct sk_buff *skb, -const void *obj); +void *obj,bool reset); };int nft_register_object (struct nft_object_type *obj_type);
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -89,6 +89,7 @@ enum nft_verdicts {* @NFT_MSG_NEWOBJ: create a stateful object (enum nft_obj_attributes) * @NFT_MSG_GETOBJ: get a stateful object (enum nft_obj_attributes) * @NFT_MSG_DELOBJ: delete a stateful object (enum nft_obj_attributes) +* @NFT_MSG_GETOBJ_RESET: get and reset a stateful object (enum nft_obj_attributes) */ enum nf_tables_msg_types {NFT_MSG_NEWTABLE ,
@@ -112,6 +113,7 @@ enum nf_tables_msg_types {NFT_MSG_NEWOBJ ,NFT_MSG_GETOBJ ,NFT_MSG_DELOBJ , +NFT_MSG_GETOBJ_RESET , NFT_MSG_MAX, };
net/netfilter/nf_tables_api.c +38 -12
118 lines not on this page · raw message
net/netfilter/nft_counter.c +18 -10
@@ -87,21 +87,29 @@ static void nft_counter_obj_destroy(void *obj)free_percpu (priv->counter); } -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;
@@ -109,12 +117,12 @@ static void nft_counter_fetch(const struct nft_counter_percpu __percpu *counter, } } -static int nft_counter_obj_dump (struct sk_buff *skb,const void *obj) +static int nft_counter_obj_dump (struct sk_buff *skb,void *obj,bool reset) { -const struct nft_counter_percpu_priv *priv = obj; +struct nft_counter_percpu_priv *priv = obj;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), NFTA_COUNTER_PAD) ||
@@ -157,7 +165,7 @@ static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr) {struct nft_counter_percpu_priv *priv =nft_expr_priv (expr); -return nft_counter_obj_dump (skb, priv); +return nft_counter_obj_dump (skb, priv, false); } static int nft_counter_init(const struct nft_ctx *ctx,
@@ -185,7 +193,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 );
net/netfilter/nft_quota.c +9 -3
@@ -75,10 +75,16 @@ static int nft_quota_obj_init(const struct nlattr * const tb[], void *obj)return 0 ; } -static int nft_quota_obj_dump (struct sk_buff *skb,const void *obj) +static int nft_quota_obj_dump (struct sk_buff *skb,void *obj,bool reset) { -const struct nft_quota *priv = obj; +struct nft_quota *priv = obj;u32 flags = priv->invert ?NFT_QUOTA_F_INV :0 ; +u64 consumed; + +if (reset) + consumed =atomic64_xchg (&priv->consumed,0 ); +else + consumed =atomic64_read (&priv->consumed); if (nla_put_be64(skb, NFTA_QUOTA_BYTES, cpu_to_be64(priv->quota), NFTA_QUOTA_PAD) ||
@@ -123,7 +129,7 @@ static int nft_quota_dump(struct sk_buff *skb, const struct nft_expr *expr) {const struct nft_quota *priv =nft_expr_priv (expr); -return nft_quota_obj_dump (skb, priv); +return nft_quota_obj_dump (skb, (struct nft_quota *)priv, false); }static struct nft_expr_type nft_quota_type;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 6/11 netfilter: nf_tables: notify internal updates of stateful objects
In reply to Pablo Neira AyusoIntroduce nf_tables_obj_notify() to notify internal state changes in stateful objects. This is used by the quota object to report depletion in a follow up patch.
--- include/net/netfilter/nf_tables.h | 4 ++++ net/netfilter/nf_tables_api.c | 31 +++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-)
include/net/netfilter/nf_tables.h +4 -0
@@ -976,6 +976,10 @@ struct nft_object *nf_tables_obj_lookup(const struct nft_table *table, const struct nlattr *nla, u32 objtype, u8 genmask); +int nft_obj_notify (struct net *net,struct nft_table *table, +struct nft_object *obj,u32 portid,u32 seq, +int event,int family,int report,gfp_t gfp); +/** * struct nft_object_type - stateful object type *
net/netfilter/nf_tables_api.c +19 -12
@@ -4317,38 +4317,45 @@ static int nf_tables_delobj(struct net *net, struct sock *nlsk,return nft_delobj (&ctx, obj); } -static int nf_tables_obj_notify (const struct nft_ctx *ctx, -struct nft_object *obj,int event) +int nft_obj_notify (struct net *net,struct nft_table *table, +struct nft_object *obj,u32 portid,u32 seq,int event, +int family,int report,gfp_t gfp) {struct sk_buff *skb;int err; -if (!ctx->report && - !nfnetlink_has_listeners (ctx->net,NFNLGRP_NFTABLES )) +if (!report && + !nfnetlink_has_listeners (net,NFNLGRP_NFTABLES ))return 0 ; err = -ENOBUFS ; - skb =nlmsg_new (NLMSG_GOODSIZE ,GFP_KERNEL ); + skb =nlmsg_new (NLMSG_GOODSIZE , gfp);if (skb ==NULL ) goto err; - err =nf_tables_fill_obj_info (skb, ctx->net, ctx->portid, ctx->seq, - event,0 , ctx->afi->family, ctx->table, - obj, false); + err =nf_tables_fill_obj_info (skb, net, portid, seq, event,0 , family, + table, obj, false);if (err <0 ) {kfree_skb (skb); goto err; } - err =nfnetlink_send (skb, ctx->net, ctx->portid,NFNLGRP_NFTABLES , - ctx->report,GFP_KERNEL ); + err =nfnetlink_send (skb, net, portid,NFNLGRP_NFTABLES , report, gfp); err:if (err <0 ) { -nfnetlink_set_err (ctx->net, ctx->portid,NFNLGRP_NFTABLES , - err); +nfnetlink_set_err (net, portid,NFNLGRP_NFTABLES , err); }return err; } +EXPORT_SYMBOL_GPL (nft_obj_notify); + +static int nf_tables_obj_notify (const struct nft_ctx *ctx, +struct nft_object *obj,int event) +{ +return nft_obj_notify (ctx->net, ctx->table, obj, ctx->portid, + ctx->seq, event, ctx->afi->family, ctx->report, +GFP_KERNEL ); +}static int nf_tables_fill_gen_info (struct sk_buff *skb,struct net *net,u32 portid,u32 seq)
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 7/11 netfilter: nft_quota: dump consumed quota
In reply to Pablo Neira AyusoAdd a new attribute NFTA_QUOTA_CONSUMED that displays the amount of quota that has been already consumed. This allows us to restore the internal state of the quota object between reboots as well as to monitor how wasted it is.
This patch changes the logic to account for the consumed bytes, instead of the bytes that remain to be consumed.
--- include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nft_quota.c | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -1020,12 +1020,14 @@ enum nft_quota_flags {* * @NFTA_QUOTA_BYTES: quota in bytes (NLA_U16) * @NFTA_QUOTA_FLAGS: flags (NLA_U32) +* @NFTA_QUOTA_CONSUMED: quota already consumed in bytes (NLA_U64) */ enum nft_quota_attributes {NFTA_QUOTA_UNSPEC ,NFTA_QUOTA_BYTES ,NFTA_QUOTA_FLAGS ,NFTA_QUOTA_PAD , +NFTA_QUOTA_CONSUMED , __NFTA_QUOTA_MAX };#define NFTA_QUOTA_MAX (__NFTA_QUOTA_MAX - 1)
net/netfilter/nft_quota.c +6 -3
@@ -18,13 +18,13 @@struct nft_quota {u64 quota;bool invert; -atomic64_t remain; +atomic64_t consumed; };static inline bool nft_overquota (struct nft_quota *priv,const struct nft_pktinfo *pkt) { -return atomic64_sub_return (pkt->skb->len, &priv->remain) <0 ; +return atomic64_add_return (pkt->skb->len, &priv->consumed) > priv->quota; } static inline void nft_quota_do_eval(struct nft_quota *priv,
@@ -70,7 +70,7 @@ static int nft_quota_obj_init(const struct nlattr * const tb[], void *obj) priv->quota = quota; priv->invert = (flags &NFT_QUOTA_F_INV ) ? true : false; -atomic64_set (&priv->remain, quota); +atomic64_set (&priv->consumed,0 );return 0 ; }
@@ -88,6 +88,9 @@ static int nft_quota_obj_dump(struct sk_buff *skb, void *obj, bool reset)if (nla_put_be64 (skb,NFTA_QUOTA_BYTES ,cpu_to_be64 (priv->quota),NFTA_QUOTA_PAD ) || +nla_put_be64 (skb,NFTA_QUOTA_CONSUMED , +cpu_to_be64 (atomic64_read (&priv->consumed)), +NFTA_QUOTA_PAD ) ||nla_put_be32 (skb,NFTA_QUOTA_FLAGS ,htonl (flags))) goto nla_put_failure;return 0 ;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 8/11 netfilter: nft_quota: add depleted flag for objects
In reply to Pablo Neira AyusoNotify on depleted quota objects. The NFT_QUOTA_F_DEPLETED flag indicates we have reached overquota.
--- include/uapi/linux/netfilter/nf_tables.h | 1 + net/netfilter/nft_quota.c | 33 ++++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +1 -0
@@ -1013,6 +1013,7 @@ enum nft_queue_attributes {enum nft_quota_flags {NFT_QUOTA_F_INV = (1 <<0 ), +NFT_QUOTA_F_DEPLETED = (1 <<1 ), };/**
net/netfilter/nft_quota.c +25 -8
@@ -17,8 +17,8 @@struct nft_quota {u64 quota; -bool invert;atomic64_t consumed; +unsigned long flags; }; static inline bool nft_overquota(struct nft_quota *priv,
@@ -27,11 +27,16 @@ static inline bool nft_overquota(struct nft_quota *priv,return atomic64_add_return (pkt->skb->len, &priv->consumed) > priv->quota; } +static inline bool nft_quota_invert (struct nft_quota *priv) +{ +return priv->flags &NFT_QUOTA_F_INV ; +} +static inline void nft_quota_do_eval (struct nft_quota *priv,struct nft_regs *regs,const struct nft_pktinfo *pkt) { -if (nft_overquota (priv, pkt) ^ priv->invert) +if (nft_overquota (priv, pkt) ^nft_quota_invert (priv)) regs->verdict.code =NFT_BREAK ; }
@@ -46,13 +51,20 @@ static void nft_quota_obj_eval(void *obj, {struct nft_quota *priv = obj; -nft_quota_do_eval (priv, regs, pkt); +if (nft_overquota (priv, pkt) ^nft_quota_invert (priv)) { +if (test_and_set_bit (NFT_QUOTA_F_DEPLETED , &priv->flags)) { +nft_obj_notify (nft_net (pkt),nft_chain (pkt)->table, + obj,0 ,0 ,NFT_MSG_NEWOBJ , +nft_pf (pkt),0 ,GFP_ATOMIC ); + } + regs->verdict.code =NFT_BREAK ; + } }static int nft_quota_obj_init (const struct nlattr *const tb[],void *obj) {struct nft_quota *priv = obj; -u32 flags =0 ; +unsigned long flags =0 ;u64 quota;if (!tb[NFTA_QUOTA_BYTES ])
@@ -66,10 +78,12 @@ static int nft_quota_obj_init(const struct nlattr * const tb[], void *obj) flags =ntohl (nla_get_be32 (tb[NFTA_QUOTA_FLAGS ]));if (flags & ~NFT_QUOTA_F_INV )return -EINVAL ; +if (flags &NFT_QUOTA_F_DEPLETED ) +return -EOPNOTSUPP ; } priv->quota = quota; - priv->invert = (flags &NFT_QUOTA_F_INV ) ? true : false; + priv->flags = flags;atomic64_set (&priv->consumed,0 );return 0 ;
@@ -78,13 +92,16 @@ static int nft_quota_obj_init(const struct nlattr * const tb[], void *obj)static int nft_quota_obj_dump (struct sk_buff *skb,void *obj,bool reset) {struct nft_quota *priv = obj; -u32 flags = priv->invert ?NFT_QUOTA_F_INV :0 ; +unsigned long flags = priv->flags;u64 consumed; -if (reset) +if (reset) { consumed =atomic64_xchg (&priv->consumed,0 ); -else +if (test_and_clear_bit (NFT_QUOTA_F_DEPLETED , &priv->flags)) + flags |=NFT_QUOTA_F_DEPLETED ; + }else { consumed =atomic64_read (&priv->consumed); + } if (nla_put_be64(skb, NFTA_QUOTA_BYTES, cpu_to_be64(priv->quota), NFTA_QUOTA_PAD) ||
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 9/11 netfilter: nf_tables: add stateful object reference to set elements
In reply to Pablo Neira AyusoThis patch allows you to refer to stateful objects from set elements. This provides the infrastructure to create maps where the right hand side of the mapping is a stateful object.
This allows us to build dictionaries of stateful objects, that you can use to perform fast lookups using any arbitrary key combination.
--- include/net/netfilter/nf_tables.h | 9 ++++ include/uapi/linux/netfilter/nf_tables.h | 8 ++++ net/netfilter/nf_tables_api.c | 70 +++++++++++++++++++++++++++----- 3 files changed, 77 insertions(+), 10 deletions(-)
include/net/netfilter/nf_tables.h +9 -0
@@ -335,6 +335,7 @@ void nft_unregister_set(struct nft_set_ops *ops);* @name: name of the set * @ktype: key type (numeric type defined by userspace, not used in the kernel) * @dtype: data type (verdict or numeric type defined by userspace) +* @objtype: object type (see NFT_OBJECT_* definitions) * @size: maximum set size * @nelems: number of elements * @ndeact: number of deactivated elements queued for removal
@@ -356,6 +357,7 @@ struct nft_set {char name[NFT_SET_MAXNAMELEN ];u32 ktype;u32 dtype; +u32 objtype;u32 size;atomic_t nelems;u32 ndeact;
@@ -425,6 +427,7 @@ void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,* @NFT_SET_EXT_EXPIRATION: element expiration time * @NFT_SET_EXT_USERDATA: user data associated with the element * @NFT_SET_EXT_EXPR: expression assiociated with the element +* @NFT_SET_EXT_OBJREF: stateful object reference associated with element * @NFT_SET_EXT_NUM: number of extension types */ enum nft_set_extensions {
@@ -435,6 +438,7 @@ enum nft_set_extensions {NFT_SET_EXT_EXPIRATION ,NFT_SET_EXT_USERDATA ,NFT_SET_EXT_EXPR , +NFT_SET_EXT_OBJREF , NFT_SET_EXT_NUM };
@@ -563,6 +567,11 @@ static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,return elem + set->ops->elemsize; } +static inline struct nft_object **nft_set_ext_obj (const struct nft_set_ext *ext) +{ +return nft_set_ext (ext,NFT_SET_EXT_OBJREF ); +} + void *nft_set_elem_init(const struct nft_set *set, const struct nft_set_ext_tmpl *tmpl, const u32 *key, const u32 *data,
include/uapi/linux/netfilter/nf_tables.h +8 -0
@@ -255,6 +255,7 @@ enum nft_rule_compat_attributes {* @NFT_SET_MAP: set is used as a dictionary * @NFT_SET_TIMEOUT: set uses timeouts * @NFT_SET_EVAL: set contains expressions for evaluation +* @NFT_SET_OBJECT: set contains stateful objects */ enum nft_set_flags {NFT_SET_ANONYMOUS =0x1 ,
@@ -263,6 +264,7 @@ enum nft_set_flags {NFT_SET_MAP =0x8 ,NFT_SET_TIMEOUT =0x10 ,NFT_SET_EVAL =0x20 , + NFT_SET_OBJECT = 0x40, };/**
@@ -304,6 +306,7 @@ enum nft_set_desc_attributes {* @NFTA_SET_TIMEOUT: default timeout value (NLA_U64) * @NFTA_SET_GC_INTERVAL: garbage collection interval (NLA_U32) * @NFTA_SET_USERDATA: user data (NLA_BINARY) +* @NFTA_SET_OBJ_TYPE: stateful object type (NLA_U32: NFT_OBJECT_*) */ enum nft_set_attributes {NFTA_SET_UNSPEC ,
@@ -321,6 +324,7 @@ enum nft_set_attributes {NFTA_SET_GC_INTERVAL ,NFTA_SET_USERDATA ,NFTA_SET_PAD , +NFTA_SET_OBJ_TYPE , __NFTA_SET_MAX };#define NFTA_SET_MAX (__NFTA_SET_MAX - 1)
@@ -344,6 +348,7 @@ enum nft_set_elem_flags {* @NFTA_SET_ELEM_EXPIRATION: expiration time (NLA_U64) * @NFTA_SET_ELEM_USERDATA: user data (NLA_BINARY) * @NFTA_SET_ELEM_EXPR: expression (NLA_NESTED: nft_expr_attributes) +* @NFTA_SET_ELEM_OBJREF: stateful object reference (NLA_STRING) */ enum nft_set_elem_attributes {NFTA_SET_ELEM_UNSPEC ,
@@ -355,6 +360,7 @@ enum nft_set_elem_attributes {NFTA_SET_ELEM_USERDATA ,NFTA_SET_ELEM_EXPR ,NFTA_SET_ELEM_PAD , +NFTA_SET_ELEM_OBJREF , __NFTA_SET_ELEM_MAX };#define NFTA_SET_ELEM_MAX (__NFTA_SET_ELEM_MAX - 1)
@@ -1237,6 +1243,8 @@ enum nft_fib_flags {#define NFT_OBJECT_UNSPEC 0#define NFT_OBJECT_COUNTER 1#define NFT_OBJECT_QUOTA 2 +#define __NFT_OBJECT_MAX 3 +#define NFT_OBJECT_MAX (__NFT_OBJECT_MAX - 1)/** * enum nft_object_attributes - nf_tables stateful object netlink attributes
net/netfilter/nf_tables_api.c +60 -10
154 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 10/11 netfilter: nft_objref: support for stateful object maps
In reply to Pablo Neira AyusoThis patch allows us to refer to stateful object dictionaries, the source register indicates the key data to be used to look up for the corresponding state object. We can refer to these maps through names or, alternatively, the map transaction id. This allows us to refer to both anonymous and named maps.
--- include/uapi/linux/netfilter/nf_tables.h | 6 ++ net/netfilter/nf_tables_api.c | 4 ++ net/netfilter/nft_objref.c | 116 ++++++++++++++++++++++++++++++- 3 files changed, 125 insertions(+), 1 deletion(-)
include/uapi/linux/netfilter/nf_tables.h +6 -0
@@ -1183,11 +1183,17 @@ enum nft_fwd_attributes {* * @NFTA_OBJREF_TYPE: source register of output interface (NLA_U32: nft_register) * @NFTA_OBJREF_NAME: source register of output interface (NLA_STRING) +* @NFTA_OBJREF_SREG: source register of the data to look for (NLA_U32: nft_registers) +* @NFTA_OBJREF_MAP: name of the map where to look for (NLA_STRING) +* @NFTA_OBJREF_MAP_ID: id of the map where to look for in this transaction (NLA_U32) */ enum nft_objref_attributes {NFTA_OBJREF_UNSPEC ,NFTA_OBJREF_TYPE ,NFTA_OBJREF_NAME , +NFTA_OBJREF_SREG , +NFTA_OBJREF_MAP , +NFTA_OBJREF_MAP_ID , __NFTA_OBJREF_MAX };#define NFTA_OBJREF_MAX (__NFTA_OBJREF_MAX - 1)
net/netfilter/nf_tables_api.c +4 -0
@@ -2532,6 +2532,7 @@ struct nft_set *nf_tables_set_lookup(const struct nft_table *table, }return ERR_PTR (-ENOENT ); } +EXPORT_SYMBOL_GPL (nf_tables_set_lookup); struct nft_set *nf_tables_set_lookup_byid(const struct net *net, const struct nlattr *nla,
@@ -2550,6 +2551,7 @@ struct nft_set *nf_tables_set_lookup_byid(const struct net *net, }return ERR_PTR (-ENOENT ); } +EXPORT_SYMBOL_GPL (nf_tables_set_lookup_byid);static int nf_tables_set_alloc_name (struct nft_ctx *ctx,struct nft_set *set,const char *name)
@@ -3146,6 +3148,7 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,list_add_tail_rcu (&binding->list, &set->bindings);return 0 ; } +EXPORT_SYMBOL_GPL (nf_tables_bind_set);void nf_tables_unbind_set (const struct nft_ctx *ctx,struct nft_set *set,struct nft_set_binding *binding)
@@ -3156,6 +3159,7 @@ void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, nft_is_active(ctx->net, set))nf_tables_set_destroy (ctx, set); } +EXPORT_SYMBOL_GPL (nf_tables_unbind_set);const struct nft_set_ext_type nft_set_ext_types[] = { [NFT_SET_EXT_KEY ] = {
net/netfilter/nft_objref.c +115 -1
130 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 11/11 netfilter: nf_tables: allow to filter stateful object dumps by type
In reply to Pablo Neira AyusoThis patch adds the netlink code to filter out dump of stateful objects, through the NFTA_OBJ_TYPE netlink attribute.
--- net/netfilter/nf_tables_api.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
net/netfilter/nf_tables_api.c +49 -0
@@ -4215,12 +4215,18 @@ static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,return -1 ; } +struct nft_obj_filter { +char table[NFT_OBJ_MAXNAMELEN ]; +u32 type; +}; +static int nf_tables_dump_obj (struct sk_buff *skb,struct netlink_callback *cb) {const struct nfgenmsg *nfmsg =nlmsg_data (cb->nlh);const struct nft_af_info *afi;const struct nft_table *table;unsigned int idx =0 , s_idx = cb->args[0 ]; +struct nft_obj_filter *filter = cb->data;struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family;struct nft_object *obj;
@@ -4245,6 +4251,12 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)if (idx > s_idx)memset (&cb->args[1 ],0 ,sizeof (cb->args) -sizeof (cb->args[0 ])); +if (filter->table[0 ] && +strcmp (filter->table, table->name)) +continue ; +if (filter->type !=NFT_OBJECT_UNSPEC && + obj->type->type != filter->type) +continue ; if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, NFT_MSG_NEWOBJ,
@@ -4265,6 +4277,31 @@ static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)return skb->len; } +static int nf_tables_dump_obj_done (struct netlink_callback *cb) +{ +kfree (cb->data); + +return 0 ; +} + +static struct nft_obj_filter * +nft_obj_filter_alloc (const struct nlattr *const nla[]) +{ +struct nft_obj_filter *filter; + + filter =kzalloc (sizeof (*filter),GFP_KERNEL ); +if (!filter) +return ERR_PTR (-ENOMEM ); + +if (nla[NFTA_OBJ_TABLE ]) +nla_strlcpy (filter->table, nla[NFTA_OBJ_TABLE ], +NFT_TABLE_MAXNAMELEN ); +if (nla[NFTA_OBJ_TYPE ]) + filter->type =ntohl (nla_get_be32 (nla[NFTA_OBJ_TYPE ])); + +return filter; +} +static int nf_tables_getobj (struct net *net,struct sock *nlsk,struct sk_buff *skb,const struct nlmsghdr *nlh,const struct nlattr *const nla[])
@@ -4283,7 +4320,19 @@ static int nf_tables_getobj(struct net *net, struct sock *nlsk,if (nlh->nlmsg_flags &NLM_F_DUMP ) {struct netlink_dump_control c = { .dump = nf_tables_dump_obj, + .done = nf_tables_dump_obj_done, }; + +if (nla[NFTA_OBJ_TABLE ] || + nla[NFTA_OBJ_TYPE ]) { +struct nft_obj_filter *filter; + + filter =nft_obj_filter_alloc (nla); +if (IS_ERR (filter)) +return -ENOMEM ; + + c.data = filter; + }return netlink_dump_start (nlsk, skb, nlh, &c); }
Signature · 1 line
2.1.4
Florian Westphal
maintainerRe: [PATCH nf-next 08/11] netfilter: nft_quota: add depleted flag for objects
In reply to Pablo Neira Ayusonet/netfilter/nft_quota.c static void nft_quota_obj_eval(void *obj, · line 58
Quoted 10 lines · Pablo Neira Ayuso
Notify on depleted quota objects. The
NFT_QUOTA_F_DEPLETEDflag indicates we have reached overquota.Signed-off-by Pablo Neira Ayuso <[email protected]>net/netfilter/nft_quota.c +5
-1-nft_quota_do_eval (priv, regs, pkt); +if (nft_overquota (priv, pkt) ^nft_quota_invert (priv)) { +if (test_and_set_bit (NFT_QUOTA_F_DEPLETED , &priv->flags)) { +nft_obj_notify (nft_net (pkt),nft_chain (pkt)->table, + obj,0 ,0 ,NFT_MSG_NEWOBJ , +nft_pf (pkt),0 ,GFP_ATOMIC );
I suspect this should be !test_and_set_bit()?
Or does this really want to call notify for every overquota hit?
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next 08/11] netfilter: nft_quota: add depleted flag for objects
Quoted 14 lines · Florian Westphal
net/netfilter/nft_quota.c static void nft_quota_obj_eval(void *obj, · line 58
Quoted 10 lines · Pablo Neira Ayuso
Notify on depleted quota objects. The
NFT_QUOTA_F_DEPLETEDflag indicates we have reached overquota.Signed-off-by Pablo Neira Ayuso <[email protected]>net/netfilter/nft_quota.c +5
-1-nft_quota_do_eval (priv, regs, pkt); +if (nft_overquota (priv, pkt) ^nft_quota_invert (priv)) { +if (test_and_set_bit (NFT_QUOTA_F_DEPLETED , &priv->flags)) { +nft_obj_notify (nft_net (pkt),nft_chain (pkt)->table, + obj,0 ,0 ,NFT_MSG_NEWOBJ , +nft_pf (pkt),0 ,GFP_ATOMIC );I suspect this should be !test_and_set_bit()?
Quoted 1 line
Or does this really want to call notify for every overquota hit?
That's right, my test here was just checking for the first event then stop. Will fix up this, thanks for reviewing.