]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nft_limit.c
netfilter: nft_limit: factor out shared code with per-byte limiting
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / nft_limit.c
CommitLineData
96518518 1/*
ef1f7df9 2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
96518518
PM
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 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/spinlock.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nf_tables.h>
18#include <net/netfilter/nf_tables.h>
19
20static DEFINE_SPINLOCK(limit_lock);
21
22struct nft_limit {
dba27ec1 23 u64 last;
96518518 24 u64 tokens;
dba27ec1 25 u64 tokens_max;
96518518 26 u64 rate;
dba27ec1 27 u64 nsecs;
96518518
PM
28};
29
f8d3a6bc 30static inline bool nft_limit_eval(struct nft_limit *limit, u64 cost)
96518518 31{
f8d3a6bc 32 u64 now, tokens;
dba27ec1 33 s64 delta;
96518518
PM
34
35 spin_lock_bh(&limit_lock);
dba27ec1 36 now = ktime_get_ns();
f8d3a6bc
PNA
37 tokens = limit->tokens + now - limit->last;
38 if (tokens > limit->tokens_max)
39 tokens = limit->tokens_max;
dba27ec1 40
f8d3a6bc 41 limit->last = now;
dba27ec1
PNA
42 delta = tokens - cost;
43 if (delta >= 0) {
f8d3a6bc 44 limit->tokens = delta;
96518518 45 spin_unlock_bh(&limit_lock);
f8d3a6bc 46 return false;
96518518 47 }
f8d3a6bc 48 limit->tokens = tokens;
96518518 49 spin_unlock_bh(&limit_lock);
f8d3a6bc 50 return true;
96518518
PM
51}
52
f8d3a6bc 53static int nft_limit_init(struct nft_limit *limit,
96518518
PM
54 const struct nlattr * const tb[])
55{
dba27ec1 56 u64 unit;
96518518
PM
57
58 if (tb[NFTA_LIMIT_RATE] == NULL ||
59 tb[NFTA_LIMIT_UNIT] == NULL)
60 return -EINVAL;
61
f8d3a6bc 62 limit->rate = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_RATE]));
dba27ec1 63 unit = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_UNIT]));
f8d3a6bc
PNA
64 limit->nsecs = unit * NSEC_PER_SEC;
65 if (limit->rate == 0 || limit->nsecs < unit)
dba27ec1 66 return -EOVERFLOW;
f8d3a6bc
PNA
67 limit->tokens = limit->tokens_max = limit->nsecs;
68 limit->last = ktime_get_ns();
69
96518518
PM
70 return 0;
71}
72
f8d3a6bc 73static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit *limit)
96518518 74{
f8d3a6bc 75 u64 secs = div_u64(limit->nsecs, NSEC_PER_SEC);
96518518 76
f8d3a6bc 77 if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(limit->rate)) ||
dba27ec1 78 nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs)))
96518518
PM
79 goto nla_put_failure;
80 return 0;
81
82nla_put_failure:
83 return -1;
84}
85
f8d3a6bc
PNA
86static void nft_limit_pkts_eval(const struct nft_expr *expr,
87 struct nft_regs *regs,
88 const struct nft_pktinfo *pkt)
89{
90 struct nft_limit *priv = nft_expr_priv(expr);
91
92 if (nft_limit_eval(priv, div_u64(priv->nsecs, priv->rate)))
93 regs->verdict.code = NFT_BREAK;
94}
95
96static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = {
97 [NFTA_LIMIT_RATE] = { .type = NLA_U64 },
98 [NFTA_LIMIT_UNIT] = { .type = NLA_U64 },
99};
100
101static int nft_limit_pkts_init(const struct nft_ctx *ctx,
102 const struct nft_expr *expr,
103 const struct nlattr * const tb[])
104{
105 struct nft_limit *priv = nft_expr_priv(expr);
106
107 return nft_limit_init(priv, tb);
108}
109
110static int nft_limit_pkts_dump(struct sk_buff *skb, const struct nft_expr *expr)
111{
112 const struct nft_limit *priv = nft_expr_priv(expr);
113
114 return nft_limit_dump(skb, priv);
115}
116
ef1f7df9 117static struct nft_expr_type nft_limit_type;
09e4e42a 118static const struct nft_expr_ops nft_limit_pkts_ops = {
ef1f7df9 119 .type = &nft_limit_type,
96518518 120 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit)),
09e4e42a 121 .eval = nft_limit_pkts_eval,
f8d3a6bc
PNA
122 .init = nft_limit_pkts_init,
123 .dump = nft_limit_pkts_dump,
ef1f7df9
PM
124};
125
126static struct nft_expr_type nft_limit_type __read_mostly = {
127 .name = "limit",
09e4e42a 128 .ops = &nft_limit_pkts_ops,
96518518
PM
129 .policy = nft_limit_policy,
130 .maxattr = NFTA_LIMIT_MAX,
151d799a 131 .flags = NFT_EXPR_STATEFUL,
ef1f7df9 132 .owner = THIS_MODULE,
96518518
PM
133};
134
135static int __init nft_limit_module_init(void)
136{
ef1f7df9 137 return nft_register_expr(&nft_limit_type);
96518518
PM
138}
139
140static void __exit nft_limit_module_exit(void)
141{
ef1f7df9 142 nft_unregister_expr(&nft_limit_type);
96518518
PM
143}
144
145module_init(nft_limit_module_init);
146module_exit(nft_limit_module_exit);
147
148MODULE_LICENSE("GPL");
149MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
150MODULE_ALIAS_NFT_EXPR("limit");