Skip to content

Commit 1896531

Browse files
committed
netfilter: nft_quota: add depleted flag for objects
Notify on depleted quota objects. The NFT_QUOTA_F_DEPLETED flag indicates we have reached overquota. Add pointer to table from nft_object, so we can use it when sending the depletion notification to userspace. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 2599e98 commit 1896531

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ int nft_verdict_dump(struct sk_buff *skb, int type,
940940
* struct nft_object - nf_tables stateful object
941941
*
942942
* @list: table stateful object list node
943+
* @table: table this object belongs to
943944
* @type: pointer to object type
944945
* @data: pointer to object data
945946
* @name: name of this stateful object
@@ -950,6 +951,7 @@ int nft_verdict_dump(struct sk_buff *skb, int type,
950951
struct nft_object {
951952
struct list_head list;
952953
char name[NFT_OBJ_MAXNAMELEN];
954+
struct nft_table *table;
953955
u32 genmask:2,
954956
use:30;
955957
/* runtime data below here */

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ enum nft_queue_attributes {
983983

984984
enum nft_quota_flags {
985985
NFT_QUOTA_F_INV = (1 << 0),
986+
NFT_QUOTA_F_DEPLETED = (1 << 1),
986987
};
987988

988989
/**

net/netfilter/nf_tables_api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4075,6 +4075,7 @@ static int nf_tables_newobj(struct net *net, struct sock *nlsk,
40754075
err = PTR_ERR(obj);
40764076
goto err1;
40774077
}
4078+
obj->table = table;
40784079
nla_strlcpy(obj->name, nla[NFTA_OBJ_NAME], NFT_OBJ_MAXNAMELEN);
40794080

40804081
err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);

net/netfilter/nft_quota.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
struct nft_quota {
1919
u64 quota;
20-
bool invert;
20+
unsigned long flags;
2121
atomic64_t consumed;
2222
};
2323

@@ -27,11 +27,16 @@ static inline bool nft_overquota(struct nft_quota *priv,
2727
return atomic64_add_return(skb->len, &priv->consumed) >= priv->quota;
2828
}
2929

30+
static inline bool nft_quota_invert(struct nft_quota *priv)
31+
{
32+
return priv->flags & NFT_QUOTA_F_INV;
33+
}
34+
3035
static inline void nft_quota_do_eval(struct nft_quota *priv,
3136
struct nft_regs *regs,
3237
const struct nft_pktinfo *pkt)
3338
{
34-
if (nft_overquota(priv, pkt->skb) ^ priv->invert)
39+
if (nft_overquota(priv, pkt->skb) ^ nft_quota_invert(priv))
3540
regs->verdict.code = NFT_BREAK;
3641
}
3742

@@ -40,19 +45,29 @@ static const struct nla_policy nft_quota_policy[NFTA_QUOTA_MAX + 1] = {
4045
[NFTA_QUOTA_FLAGS] = { .type = NLA_U32 },
4146
};
4247

48+
#define NFT_QUOTA_DEPLETED_BIT 1 /* From NFT_QUOTA_F_DEPLETED. */
49+
4350
static void nft_quota_obj_eval(struct nft_object *obj,
4451
struct nft_regs *regs,
4552
const struct nft_pktinfo *pkt)
4653
{
4754
struct nft_quota *priv = nft_obj_data(obj);
55+
bool overquota;
4856

49-
nft_quota_do_eval(priv, regs, pkt);
57+
overquota = nft_overquota(priv, pkt->skb);
58+
if (overquota ^ nft_quota_invert(priv))
59+
regs->verdict.code = NFT_BREAK;
60+
61+
if (overquota &&
62+
!test_and_set_bit(NFT_QUOTA_DEPLETED_BIT, &priv->flags))
63+
nft_obj_notify(nft_net(pkt), obj->table, obj, 0, 0,
64+
NFT_MSG_NEWOBJ, nft_pf(pkt), 0, GFP_ATOMIC);
5065
}
5166

5267
static int nft_quota_do_init(const struct nlattr * const tb[],
5368
struct nft_quota *priv)
5469
{
55-
u32 flags = 0;
70+
unsigned long flags = 0;
5671
u64 quota;
5772

5873
if (!tb[NFTA_QUOTA_BYTES])
@@ -66,10 +81,12 @@ static int nft_quota_do_init(const struct nlattr * const tb[],
6681
flags = ntohl(nla_get_be32(tb[NFTA_QUOTA_FLAGS]));
6782
if (flags & ~NFT_QUOTA_F_INV)
6883
return -EINVAL;
84+
if (flags & NFT_QUOTA_F_DEPLETED)
85+
return -EOPNOTSUPP;
6986
}
7087

7188
priv->quota = quota;
72-
priv->invert = (flags & NFT_QUOTA_F_INV) ? true : false;
89+
priv->flags = flags;
7390
atomic64_set(&priv->consumed, 0);
7491

7592
return 0;
@@ -86,13 +103,16 @@ static int nft_quota_obj_init(const struct nlattr * const tb[],
86103
static int nft_quota_do_dump(struct sk_buff *skb, struct nft_quota *priv,
87104
bool reset)
88105
{
89-
u32 flags = priv->invert ? NFT_QUOTA_F_INV : 0;
106+
u32 flags = priv->flags;
90107
u64 consumed;
91108

92-
if (reset)
109+
if (reset) {
93110
consumed = atomic64_xchg(&priv->consumed, 0);
94-
else
111+
if (test_and_clear_bit(NFT_QUOTA_DEPLETED_BIT, &priv->flags))
112+
flags |= NFT_QUOTA_F_DEPLETED;
113+
} else {
95114
consumed = atomic64_read(&priv->consumed);
115+
}
96116

97117
/* Since we inconditionally increment consumed quota for each packet
98118
* that we see, don't go over the quota boundary in what we send to

0 commit comments

Comments
 (0)