14#include <linux/netfilter/nf_tables.h>
17#include <libmnl/libmnl.h>
18#include <libnftnl/expr.h>
19#include <libnftnl/rule.h>
25 enum nft_limit_type type;
30nftnl_expr_limit_set(
struct nftnl_expr *e, uint16_t type,
31 const void *data, uint32_t data_len)
36 case NFTNL_EXPR_LIMIT_RATE:
37 memcpy(&limit->rate, data, data_len);
39 case NFTNL_EXPR_LIMIT_UNIT:
40 memcpy(&limit->unit, data, data_len);
42 case NFTNL_EXPR_LIMIT_BURST:
43 memcpy(&limit->burst, data, data_len);
45 case NFTNL_EXPR_LIMIT_TYPE:
46 memcpy(&limit->type, data, data_len);
48 case NFTNL_EXPR_LIMIT_FLAGS:
49 memcpy(&limit->flags, data, data_len);
56nftnl_expr_limit_get(
const struct nftnl_expr *e, uint16_t type,
62 case NFTNL_EXPR_LIMIT_RATE:
63 *data_len =
sizeof(uint64_t);
65 case NFTNL_EXPR_LIMIT_UNIT:
66 *data_len =
sizeof(uint64_t);
68 case NFTNL_EXPR_LIMIT_BURST:
69 *data_len =
sizeof(uint32_t);
71 case NFTNL_EXPR_LIMIT_TYPE:
72 *data_len =
sizeof(uint32_t);
74 case NFTNL_EXPR_LIMIT_FLAGS:
75 *data_len =
sizeof(uint32_t);
81static int nftnl_expr_limit_cb(
const struct nlattr *attr,
void *data)
83 const struct nlattr **tb = data;
84 int type = mnl_attr_get_type(attr);
86 if (mnl_attr_type_valid(attr, NFTA_LIMIT_MAX) < 0)
92 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
95 case NFTA_LIMIT_BURST:
97 case NFTA_LIMIT_FLAGS:
98 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
108nftnl_expr_limit_build(
struct nlmsghdr *nlh,
const struct nftnl_expr *e)
112 if (e->flags & (1 << NFTNL_EXPR_LIMIT_RATE))
113 mnl_attr_put_u64(nlh, NFTA_LIMIT_RATE, htobe64(limit->rate));
114 if (e->flags & (1 << NFTNL_EXPR_LIMIT_UNIT))
115 mnl_attr_put_u64(nlh, NFTA_LIMIT_UNIT, htobe64(limit->unit));
116 if (e->flags & (1 << NFTNL_EXPR_LIMIT_BURST))
117 mnl_attr_put_u32(nlh, NFTA_LIMIT_BURST, htonl(limit->burst));
118 if (e->flags & (1 << NFTNL_EXPR_LIMIT_TYPE))
119 mnl_attr_put_u32(nlh, NFTA_LIMIT_TYPE, htonl(limit->type));
120 if (e->flags & (1 << NFTNL_EXPR_LIMIT_FLAGS))
121 mnl_attr_put_u32(nlh, NFTA_LIMIT_FLAGS, htonl(limit->flags));
125nftnl_expr_limit_parse(
struct nftnl_expr *e,
struct nlattr *attr)
128 struct nlattr *tb[NFTA_LIMIT_MAX+1] = {};
130 if (mnl_attr_parse_nested(attr, nftnl_expr_limit_cb, tb) < 0)
133 if (tb[NFTA_LIMIT_RATE]) {
134 limit->rate = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_RATE]));
135 e->flags |= (1 << NFTNL_EXPR_LIMIT_RATE);
137 if (tb[NFTA_LIMIT_UNIT]) {
138 limit->unit = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_UNIT]));
139 e->flags |= (1 << NFTNL_EXPR_LIMIT_UNIT);
141 if (tb[NFTA_LIMIT_BURST]) {
142 limit->burst = ntohl(mnl_attr_get_u32(tb[NFTA_LIMIT_BURST]));
143 e->flags |= (1 << NFTNL_EXPR_LIMIT_BURST);
145 if (tb[NFTA_LIMIT_TYPE]) {
146 limit->type = ntohl(mnl_attr_get_u32(tb[NFTA_LIMIT_TYPE]));
147 e->flags |= (1 << NFTNL_EXPR_LIMIT_TYPE);
149 if (tb[NFTA_LIMIT_FLAGS]) {
150 limit->flags = ntohl(mnl_attr_get_u32(tb[NFTA_LIMIT_FLAGS]));
151 e->flags |= (1 << NFTNL_EXPR_LIMIT_FLAGS);
157static const char *get_time(uint64_t seconds, uint64_t *val)
159 static const struct {
171 for (i = 1; i < array_size(units); i++) {
172 if (seconds % units[i].size)
174 seconds /= units[i].size;
178 return units[i - 1].name;
181static const char *limit_to_type(
enum nft_limit_type type)
187 case NFT_LIMIT_PKT_BYTES:
193nftnl_expr_limit_snprintf(
char *buf,
size_t len,
194 uint32_t flags,
const struct nftnl_expr *e)
197 unsigned int offset = 0;
198 const char *time_unit;
202 ret = snprintf(buf, len,
"rate %"PRIu64
"/", limit->rate);
203 SNPRINTF_BUFFER_SIZE(ret, len, offset);
205 time_unit = get_time(limit->unit, &time_val);
207 ret = snprintf(buf + offset, len,
"%"PRIu64
" ", time_val);
208 SNPRINTF_BUFFER_SIZE(ret, len, offset);
211 ret = snprintf(buf + offset, len,
"%s burst %u type %s flags 0x%x ",
212 time_unit, limit->burst, limit_to_type(limit->type),
214 SNPRINTF_BUFFER_SIZE(ret, len, offset);
219static struct attr_policy limit_attr_policy[__NFTNL_EXPR_LIMIT_MAX] = {
220 [NFTNL_EXPR_LIMIT_RATE] = { .maxlen =
sizeof(uint64_t) },
221 [NFTNL_EXPR_LIMIT_UNIT] = { .maxlen =
sizeof(uint64_t) },
222 [NFTNL_EXPR_LIMIT_BURST] = { .maxlen =
sizeof(uint32_t) },
223 [NFTNL_EXPR_LIMIT_TYPE] = { .maxlen =
sizeof(uint32_t) },
224 [NFTNL_EXPR_LIMIT_FLAGS] = { .maxlen =
sizeof(uint32_t) },
227struct expr_ops expr_ops_limit = {
230 .nftnl_max_attr = __NFTNL_EXPR_LIMIT_MAX - 1,
231 .attr_policy = limit_attr_policy,
232 .set = nftnl_expr_limit_set,
233 .get = nftnl_expr_limit_get,
234 .parse = nftnl_expr_limit_parse,
235 .build = nftnl_expr_limit_build,
236 .output = nftnl_expr_limit_snprintf,