1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/atomic.h>
10 #include <linux/netlink.h>
11 #include <linux/netfilter.h>
12 #include <linux/netfilter/nf_tables.h>
13 #include <net/netfilter/nf_tables.h>
21 static inline bool nft_overquota(struct nft_quota
*priv
,
22 const struct sk_buff
*skb
)
24 return atomic64_add_return(skb
->len
, priv
->consumed
) >=
25 atomic64_read(&priv
->quota
);
28 static inline bool nft_quota_invert(struct nft_quota
*priv
)
30 return priv
->flags
& NFT_QUOTA_F_INV
;
33 static inline void nft_quota_do_eval(struct nft_quota
*priv
,
34 struct nft_regs
*regs
,
35 const struct nft_pktinfo
*pkt
)
37 if (nft_overquota(priv
, pkt
->skb
) ^ nft_quota_invert(priv
))
38 regs
->verdict
.code
= NFT_BREAK
;
41 static const struct nla_policy nft_quota_policy
[NFTA_QUOTA_MAX
+ 1] = {
42 [NFTA_QUOTA_BYTES
] = { .type
= NLA_U64
},
43 [NFTA_QUOTA_FLAGS
] = { .type
= NLA_U32
},
44 [NFTA_QUOTA_CONSUMED
] = { .type
= NLA_U64
},
47 #define NFT_QUOTA_DEPLETED_BIT 1 /* From NFT_QUOTA_F_DEPLETED. */
49 static void nft_quota_obj_eval(struct nft_object
*obj
,
50 struct nft_regs
*regs
,
51 const struct nft_pktinfo
*pkt
)
53 struct nft_quota
*priv
= nft_obj_data(obj
);
56 overquota
= nft_overquota(priv
, pkt
->skb
);
57 if (overquota
^ nft_quota_invert(priv
))
58 regs
->verdict
.code
= NFT_BREAK
;
61 !test_and_set_bit(NFT_QUOTA_DEPLETED_BIT
, &priv
->flags
))
62 nft_obj_notify(nft_net(pkt
), obj
->key
.table
, obj
, 0, 0,
63 NFT_MSG_NEWOBJ
, 0, nft_pf(pkt
), 0, GFP_ATOMIC
);
66 static int nft_quota_do_init(const struct nlattr
* const tb
[],
67 struct nft_quota
*priv
)
69 unsigned long flags
= 0;
70 u64 quota
, consumed
= 0;
72 if (!tb
[NFTA_QUOTA_BYTES
])
75 quota
= be64_to_cpu(nla_get_be64(tb
[NFTA_QUOTA_BYTES
]));
79 if (tb
[NFTA_QUOTA_CONSUMED
]) {
80 consumed
= be64_to_cpu(nla_get_be64(tb
[NFTA_QUOTA_CONSUMED
]));
85 if (tb
[NFTA_QUOTA_FLAGS
]) {
86 flags
= ntohl(nla_get_be32(tb
[NFTA_QUOTA_FLAGS
]));
87 if (flags
& ~NFT_QUOTA_F_INV
)
89 if (flags
& NFT_QUOTA_F_DEPLETED
)
93 priv
->consumed
= kmalloc(sizeof(*priv
->consumed
), GFP_KERNEL_ACCOUNT
);
97 atomic64_set(&priv
->quota
, quota
);
99 atomic64_set(priv
->consumed
, consumed
);
104 static void nft_quota_do_destroy(const struct nft_ctx
*ctx
,
105 struct nft_quota
*priv
)
107 kfree(priv
->consumed
);
110 static int nft_quota_obj_init(const struct nft_ctx
*ctx
,
111 const struct nlattr
* const tb
[],
112 struct nft_object
*obj
)
114 struct nft_quota
*priv
= nft_obj_data(obj
);
116 return nft_quota_do_init(tb
, priv
);
119 static void nft_quota_obj_update(struct nft_object
*obj
,
120 struct nft_object
*newobj
)
122 struct nft_quota
*newpriv
= nft_obj_data(newobj
);
123 struct nft_quota
*priv
= nft_obj_data(obj
);
126 newquota
= atomic64_read(&newpriv
->quota
);
127 atomic64_set(&priv
->quota
, newquota
);
128 priv
->flags
= newpriv
->flags
;
131 static int nft_quota_do_dump(struct sk_buff
*skb
, struct nft_quota
*priv
,
134 u64 consumed
, consumed_cap
, quota
;
135 u32 flags
= priv
->flags
;
137 /* Since we inconditionally increment consumed quota for each packet
138 * that we see, don't go over the quota boundary in what we send to
141 consumed
= atomic64_read(priv
->consumed
);
142 quota
= atomic64_read(&priv
->quota
);
143 if (consumed
>= quota
) {
144 consumed_cap
= quota
;
145 flags
|= NFT_QUOTA_F_DEPLETED
;
147 consumed_cap
= consumed
;
150 if (nla_put_be64(skb
, NFTA_QUOTA_BYTES
, cpu_to_be64(quota
),
152 nla_put_be64(skb
, NFTA_QUOTA_CONSUMED
, cpu_to_be64(consumed_cap
),
154 nla_put_be32(skb
, NFTA_QUOTA_FLAGS
, htonl(flags
)))
155 goto nla_put_failure
;
158 atomic64_sub(consumed
, priv
->consumed
);
159 clear_bit(NFT_QUOTA_DEPLETED_BIT
, &priv
->flags
);
167 static int nft_quota_obj_dump(struct sk_buff
*skb
, struct nft_object
*obj
,
170 struct nft_quota
*priv
= nft_obj_data(obj
);
172 return nft_quota_do_dump(skb
, priv
, reset
);
175 static void nft_quota_obj_destroy(const struct nft_ctx
*ctx
,
176 struct nft_object
*obj
)
178 struct nft_quota
*priv
= nft_obj_data(obj
);
180 return nft_quota_do_destroy(ctx
, priv
);
183 static struct nft_object_type nft_quota_obj_type
;
184 static const struct nft_object_ops nft_quota_obj_ops
= {
185 .type
= &nft_quota_obj_type
,
186 .size
= sizeof(struct nft_quota
),
187 .init
= nft_quota_obj_init
,
188 .destroy
= nft_quota_obj_destroy
,
189 .eval
= nft_quota_obj_eval
,
190 .dump
= nft_quota_obj_dump
,
191 .update
= nft_quota_obj_update
,
194 static struct nft_object_type nft_quota_obj_type __read_mostly
= {
195 .type
= NFT_OBJECT_QUOTA
,
196 .ops
= &nft_quota_obj_ops
,
197 .maxattr
= NFTA_QUOTA_MAX
,
198 .policy
= nft_quota_policy
,
199 .owner
= THIS_MODULE
,
202 static void nft_quota_eval(const struct nft_expr
*expr
,
203 struct nft_regs
*regs
,
204 const struct nft_pktinfo
*pkt
)
206 struct nft_quota
*priv
= nft_expr_priv(expr
);
208 nft_quota_do_eval(priv
, regs
, pkt
);
211 static int nft_quota_init(const struct nft_ctx
*ctx
,
212 const struct nft_expr
*expr
,
213 const struct nlattr
* const tb
[])
215 struct nft_quota
*priv
= nft_expr_priv(expr
);
217 return nft_quota_do_init(tb
, priv
);
220 static int nft_quota_dump(struct sk_buff
*skb
,
221 const struct nft_expr
*expr
, bool reset
)
223 struct nft_quota
*priv
= nft_expr_priv(expr
);
225 return nft_quota_do_dump(skb
, priv
, reset
);
228 static void nft_quota_destroy(const struct nft_ctx
*ctx
,
229 const struct nft_expr
*expr
)
231 struct nft_quota
*priv
= nft_expr_priv(expr
);
233 return nft_quota_do_destroy(ctx
, priv
);
236 static int nft_quota_clone(struct nft_expr
*dst
, const struct nft_expr
*src
, gfp_t gfp
)
238 struct nft_quota
*priv_dst
= nft_expr_priv(dst
);
239 struct nft_quota
*priv_src
= nft_expr_priv(src
);
241 priv_dst
->quota
= priv_src
->quota
;
242 priv_dst
->flags
= priv_src
->flags
;
244 priv_dst
->consumed
= kmalloc(sizeof(*priv_dst
->consumed
), gfp
);
245 if (!priv_dst
->consumed
)
248 *priv_dst
->consumed
= *priv_src
->consumed
;
253 static struct nft_expr_type nft_quota_type
;
254 static const struct nft_expr_ops nft_quota_ops
= {
255 .type
= &nft_quota_type
,
256 .size
= NFT_EXPR_SIZE(sizeof(struct nft_quota
)),
257 .eval
= nft_quota_eval
,
258 .init
= nft_quota_init
,
259 .destroy
= nft_quota_destroy
,
260 .clone
= nft_quota_clone
,
261 .dump
= nft_quota_dump
,
262 .reduce
= NFT_REDUCE_READONLY
,
265 static struct nft_expr_type nft_quota_type __read_mostly
= {
267 .ops
= &nft_quota_ops
,
268 .policy
= nft_quota_policy
,
269 .maxattr
= NFTA_QUOTA_MAX
,
270 .flags
= NFT_EXPR_STATEFUL
,
271 .owner
= THIS_MODULE
,
274 static int __init
nft_quota_module_init(void)
278 err
= nft_register_obj(&nft_quota_obj_type
);
282 err
= nft_register_expr(&nft_quota_type
);
288 nft_unregister_obj(&nft_quota_obj_type
);
292 static void __exit
nft_quota_module_exit(void)
294 nft_unregister_expr(&nft_quota_type
);
295 nft_unregister_obj(&nft_quota_obj_type
);
298 module_init(nft_quota_module_init
);
299 module_exit(nft_quota_module_exit
);
301 MODULE_LICENSE("GPL");
302 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
303 MODULE_ALIAS_NFT_EXPR("quota");
304 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_QUOTA
);
305 MODULE_DESCRIPTION("Netfilter nftables quota module");