2 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
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.
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>
24 static inline bool nft_overquota(struct nft_quota
*priv
,
25 const struct sk_buff
*skb
)
27 return atomic64_add_return(skb
->len
, &priv
->consumed
) >= priv
->quota
;
30 static inline bool nft_quota_invert(struct nft_quota
*priv
)
32 return priv
->flags
& NFT_QUOTA_F_INV
;
35 static inline void nft_quota_do_eval(struct nft_quota
*priv
,
36 struct nft_regs
*regs
,
37 const struct nft_pktinfo
*pkt
)
39 if (nft_overquota(priv
, pkt
->skb
) ^ nft_quota_invert(priv
))
40 regs
->verdict
.code
= NFT_BREAK
;
43 static const struct nla_policy nft_quota_policy
[NFTA_QUOTA_MAX
+ 1] = {
44 [NFTA_QUOTA_BYTES
] = { .type
= NLA_U64
},
45 [NFTA_QUOTA_FLAGS
] = { .type
= NLA_U32
},
46 [NFTA_QUOTA_CONSUMED
] = { .type
= NLA_U64
},
49 #define NFT_QUOTA_DEPLETED_BIT 1 /* From NFT_QUOTA_F_DEPLETED. */
51 static void nft_quota_obj_eval(struct nft_object
*obj
,
52 struct nft_regs
*regs
,
53 const struct nft_pktinfo
*pkt
)
55 struct nft_quota
*priv
= nft_obj_data(obj
);
58 overquota
= nft_overquota(priv
, pkt
->skb
);
59 if (overquota
^ nft_quota_invert(priv
))
60 regs
->verdict
.code
= NFT_BREAK
;
63 !test_and_set_bit(NFT_QUOTA_DEPLETED_BIT
, &priv
->flags
))
64 nft_obj_notify(nft_net(pkt
), obj
->table
, obj
, 0, 0,
65 NFT_MSG_NEWOBJ
, nft_pf(pkt
), 0, GFP_ATOMIC
);
68 static int nft_quota_do_init(const struct nlattr
* const tb
[],
69 struct nft_quota
*priv
)
71 unsigned long flags
= 0;
72 u64 quota
, consumed
= 0;
74 if (!tb
[NFTA_QUOTA_BYTES
])
77 quota
= be64_to_cpu(nla_get_be64(tb
[NFTA_QUOTA_BYTES
]));
81 if (tb
[NFTA_QUOTA_CONSUMED
]) {
82 consumed
= be64_to_cpu(nla_get_be64(tb
[NFTA_QUOTA_CONSUMED
]));
87 if (tb
[NFTA_QUOTA_FLAGS
]) {
88 flags
= ntohl(nla_get_be32(tb
[NFTA_QUOTA_FLAGS
]));
89 if (flags
& ~NFT_QUOTA_F_INV
)
91 if (flags
& NFT_QUOTA_F_DEPLETED
)
97 atomic64_set(&priv
->consumed
, consumed
);
102 static int nft_quota_obj_init(const struct nft_ctx
*ctx
,
103 const struct nlattr
* const tb
[],
104 struct nft_object
*obj
)
106 struct nft_quota
*priv
= nft_obj_data(obj
);
108 return nft_quota_do_init(tb
, priv
);
111 static int nft_quota_do_dump(struct sk_buff
*skb
, struct nft_quota
*priv
,
114 u64 consumed
, consumed_cap
;
115 u32 flags
= priv
->flags
;
117 /* Since we inconditionally increment consumed quota for each packet
118 * that we see, don't go over the quota boundary in what we send to
121 consumed
= atomic64_read(&priv
->consumed
);
122 if (consumed
>= priv
->quota
) {
123 consumed_cap
= priv
->quota
;
124 flags
|= NFT_QUOTA_F_DEPLETED
;
126 consumed_cap
= consumed
;
129 if (nla_put_be64(skb
, NFTA_QUOTA_BYTES
, cpu_to_be64(priv
->quota
),
131 nla_put_be64(skb
, NFTA_QUOTA_CONSUMED
, cpu_to_be64(consumed_cap
),
133 nla_put_be32(skb
, NFTA_QUOTA_FLAGS
, htonl(flags
)))
134 goto nla_put_failure
;
137 atomic64_sub(consumed
, &priv
->consumed
);
138 clear_bit(NFT_QUOTA_DEPLETED_BIT
, &priv
->flags
);
146 static int nft_quota_obj_dump(struct sk_buff
*skb
, struct nft_object
*obj
,
149 struct nft_quota
*priv
= nft_obj_data(obj
);
151 return nft_quota_do_dump(skb
, priv
, reset
);
154 static struct nft_object_type nft_quota_obj __read_mostly
= {
155 .type
= NFT_OBJECT_QUOTA
,
156 .size
= sizeof(struct nft_quota
),
157 .maxattr
= NFTA_QUOTA_MAX
,
158 .policy
= nft_quota_policy
,
159 .init
= nft_quota_obj_init
,
160 .eval
= nft_quota_obj_eval
,
161 .dump
= nft_quota_obj_dump
,
162 .owner
= THIS_MODULE
,
165 static void nft_quota_eval(const struct nft_expr
*expr
,
166 struct nft_regs
*regs
,
167 const struct nft_pktinfo
*pkt
)
169 struct nft_quota
*priv
= nft_expr_priv(expr
);
171 nft_quota_do_eval(priv
, regs
, pkt
);
174 static int nft_quota_init(const struct nft_ctx
*ctx
,
175 const struct nft_expr
*expr
,
176 const struct nlattr
* const tb
[])
178 struct nft_quota
*priv
= nft_expr_priv(expr
);
180 return nft_quota_do_init(tb
, priv
);
183 static int nft_quota_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
185 struct nft_quota
*priv
= nft_expr_priv(expr
);
187 return nft_quota_do_dump(skb
, priv
, false);
190 static struct nft_expr_type nft_quota_type
;
191 static const struct nft_expr_ops nft_quota_ops
= {
192 .type
= &nft_quota_type
,
193 .size
= NFT_EXPR_SIZE(sizeof(struct nft_quota
)),
194 .eval
= nft_quota_eval
,
195 .init
= nft_quota_init
,
196 .dump
= nft_quota_dump
,
199 static struct nft_expr_type nft_quota_type __read_mostly
= {
201 .ops
= &nft_quota_ops
,
202 .policy
= nft_quota_policy
,
203 .maxattr
= NFTA_QUOTA_MAX
,
204 .flags
= NFT_EXPR_STATEFUL
,
205 .owner
= THIS_MODULE
,
208 static int __init
nft_quota_module_init(void)
212 err
= nft_register_obj(&nft_quota_obj
);
216 err
= nft_register_expr(&nft_quota_type
);
222 nft_unregister_obj(&nft_quota_obj
);
226 static void __exit
nft_quota_module_exit(void)
228 nft_unregister_expr(&nft_quota_type
);
229 nft_unregister_obj(&nft_quota_obj
);
232 module_init(nft_quota_module_init
);
233 module_exit(nft_quota_module_exit
);
235 MODULE_LICENSE("GPL");
236 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
237 MODULE_ALIAS_NFT_EXPR("quota");
238 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_QUOTA
);