Skip to content

Commit 3d2f30a

Browse files
committed
netfilter: nf_tables: add quota expression
This patch adds the quota expression. This new stateful expression integrate easily into the dynset expression to build 'hashquota' flow tables. Arguably, we could use instead "counter bytes > 1000" instead, but this approach has several problems: 1) We only support for one single stateful expression in dynamic set definitions, and the expression above is a composite of two expressions: get counter + comparison. 2) We would need to restore the packed counter representation (that we used to have) based on seqlock to synchronize this, since per-cpu is not suitable for this. So instead of bloating the counter expression back with the seqlock representation and extending the existing set infrastructure to make it more complex for the composite described above, let's follow the more simple approach of adding a quota expression that we can plug into our existing infrastructure. Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 2567c4e commit 3d2f30a

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,25 @@ enum nft_queue_attributes {
900900
#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02 /* use current CPU (no hashing) */
901901
#define NFT_QUEUE_FLAG_MASK 0x03
902902

903+
enum nft_quota_flags {
904+
NFT_QUOTA_F_INV = (1 << 0),
905+
};
906+
907+
/**
908+
* enum nft_quota_attributes - nf_tables quota expression netlink attributes
909+
*
910+
* @NFTA_QUOTA_BYTES: quota in bytes (NLA_U16)
911+
* @NFTA_QUOTA_FLAGS: flags (NLA_U32)
912+
*/
913+
enum nft_quota_attributes {
914+
NFTA_QUOTA_UNSPEC,
915+
NFTA_QUOTA_BYTES,
916+
NFTA_QUOTA_FLAGS,
917+
NFTA_QUOTA_PAD,
918+
__NFTA_QUOTA_MAX
919+
};
920+
#define NFTA_QUOTA_MAX (__NFTA_QUOTA_MAX - 1)
921+
903922
/**
904923
* enum nft_reject_types - nf_tables reject expression reject types
905924
*

net/netfilter/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,12 @@ config NFT_QUEUE
542542
This is required if you intend to use the userspace queueing
543543
infrastructure (also known as NFQUEUE) from nftables.
544544

545+
config NFT_QUOTA
546+
tristate "Netfilter nf_tables quota module"
547+
help
548+
This option adds the "quota" expression that you can use to match
549+
enforce bytes quotas.
550+
545551
config NFT_REJECT
546552
default m if NETFILTER_ADVANCED=n
547553
tristate "Netfilter nf_tables reject support"

net/netfilter/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ obj-$(CONFIG_NFT_CT) += nft_ct.o
8484
obj-$(CONFIG_NFT_LIMIT) += nft_limit.o
8585
obj-$(CONFIG_NFT_NAT) += nft_nat.o
8686
obj-$(CONFIG_NFT_QUEUE) += nft_queue.o
87+
obj-$(CONFIG_NFT_QUOTA) += nft_quota.o
8788
obj-$(CONFIG_NFT_REJECT) += nft_reject.o
8889
obj-$(CONFIG_NFT_REJECT_INET) += nft_reject_inet.o
8990
obj-$(CONFIG_NFT_SET_RBTREE) += nft_set_rbtree.o

net/netfilter/nft_quota.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (c) 2016 Pablo Neira Ayuso <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*/
8+
9+
#include <linux/kernel.h>
10+
#include <linux/init.h>
11+
#include <linux/module.h>
12+
#include <linux/atomic.h>
13+
#include <linux/netlink.h>
14+
#include <linux/netfilter.h>
15+
#include <linux/netfilter/nf_tables.h>
16+
#include <net/netfilter/nf_tables.h>
17+
18+
struct nft_quota {
19+
u64 quota;
20+
bool invert;
21+
atomic64_t remain;
22+
};
23+
24+
static inline long nft_quota(struct nft_quota *priv,
25+
const struct nft_pktinfo *pkt)
26+
{
27+
return atomic64_sub_return(pkt->skb->len, &priv->remain);
28+
}
29+
30+
static void nft_quota_eval(const struct nft_expr *expr,
31+
struct nft_regs *regs,
32+
const struct nft_pktinfo *pkt)
33+
{
34+
struct nft_quota *priv = nft_expr_priv(expr);
35+
36+
if (nft_quota(priv, pkt) < 0 && !priv->invert)
37+
regs->verdict.code = NFT_BREAK;
38+
}
39+
40+
static const struct nla_policy nft_quota_policy[NFTA_QUOTA_MAX + 1] = {
41+
[NFTA_QUOTA_BYTES] = { .type = NLA_U64 },
42+
[NFTA_QUOTA_FLAGS] = { .type = NLA_U32 },
43+
};
44+
45+
static int nft_quota_init(const struct nft_ctx *ctx,
46+
const struct nft_expr *expr,
47+
const struct nlattr * const tb[])
48+
{
49+
struct nft_quota *priv = nft_expr_priv(expr);
50+
u32 flags = 0;
51+
u64 quota;
52+
53+
if (!tb[NFTA_QUOTA_BYTES])
54+
return -EINVAL;
55+
56+
quota = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_BYTES]));
57+
if (quota > S64_MAX)
58+
return -EOVERFLOW;
59+
60+
if (tb[NFTA_QUOTA_FLAGS]) {
61+
flags = ntohl(nla_get_be32(tb[NFTA_QUOTA_FLAGS]));
62+
if (flags & ~NFT_QUOTA_F_INV)
63+
return -EINVAL;
64+
}
65+
66+
priv->quota = quota;
67+
priv->invert = (flags & NFT_QUOTA_F_INV) ? true : false;
68+
atomic64_set(&priv->remain, quota);
69+
70+
return 0;
71+
}
72+
73+
static int nft_quota_dump(struct sk_buff *skb, const struct nft_expr *expr)
74+
{
75+
const struct nft_quota *priv = nft_expr_priv(expr);
76+
u32 flags = priv->invert ? NFT_QUOTA_F_INV : 0;
77+
78+
if (nla_put_be64(skb, NFTA_QUOTA_BYTES, cpu_to_be64(priv->quota),
79+
NFTA_QUOTA_PAD) ||
80+
nla_put_be32(skb, NFTA_QUOTA_FLAGS, htonl(flags)))
81+
goto nla_put_failure;
82+
return 0;
83+
84+
nla_put_failure:
85+
return -1;
86+
}
87+
88+
static struct nft_expr_type nft_quota_type;
89+
static const struct nft_expr_ops nft_quota_ops = {
90+
.type = &nft_quota_type,
91+
.size = NFT_EXPR_SIZE(sizeof(struct nft_quota)),
92+
.eval = nft_quota_eval,
93+
.init = nft_quota_init,
94+
.dump = nft_quota_dump,
95+
};
96+
97+
static struct nft_expr_type nft_quota_type __read_mostly = {
98+
.name = "quota",
99+
.ops = &nft_quota_ops,
100+
.policy = nft_quota_policy,
101+
.maxattr = NFTA_QUOTA_MAX,
102+
.flags = NFT_EXPR_STATEFUL,
103+
.owner = THIS_MODULE,
104+
};
105+
106+
static int __init nft_quota_module_init(void)
107+
{
108+
return nft_register_expr(&nft_quota_type);
109+
}
110+
111+
static void __exit nft_quota_module_exit(void)
112+
{
113+
nft_unregister_expr(&nft_quota_type);
114+
}
115+
116+
module_init(nft_quota_module_init);
117+
module_exit(nft_quota_module_exit);
118+
119+
MODULE_LICENSE("GPL");
120+
MODULE_AUTHOR("Pablo Neira Ayuso <[email protected]>");
121+
MODULE_ALIAS_NFT_EXPR("quota");

0 commit comments

Comments
 (0)