16 #include <arpa/inet.h>
18 #include <linux/netfilter/nf_tables.h>
21 #include <libmnl/libmnl.h>
22 #include <libnftnl/expr.h>
23 #include <libnftnl/rule.h>
31 nftnl_expr_limit_set(
struct nftnl_expr *e, uint16_t type,
32 const void *data, uint32_t data_len)
37 case NFTNL_EXPR_LIMIT_RATE:
38 limit->rate = *((uint64_t *)data);
40 case NFTNL_EXPR_LIMIT_UNIT:
41 limit->unit = *((uint64_t *)data);
50 nftnl_expr_limit_get(
const struct nftnl_expr *e, uint16_t type,
56 case NFTNL_EXPR_LIMIT_RATE:
57 *data_len =
sizeof(uint64_t);
59 case NFTNL_EXPR_LIMIT_UNIT:
60 *data_len =
sizeof(uint64_t);
66 static int nftnl_expr_limit_cb(
const struct nlattr *attr,
void *data)
68 const struct nlattr **tb = data;
69 int type = mnl_attr_get_type(attr);
71 if (mnl_attr_type_valid(attr, NFTA_LIMIT_MAX) < 0)
77 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
87 nftnl_expr_limit_build(
struct nlmsghdr *nlh,
struct nftnl_expr *e)
91 if (e->flags & (1 << NFTNL_EXPR_LIMIT_RATE))
92 mnl_attr_put_u64(nlh, NFTA_LIMIT_RATE, htobe64(limit->rate));
93 if (e->flags & (1 << NFTNL_EXPR_LIMIT_UNIT))
94 mnl_attr_put_u64(nlh, NFTA_LIMIT_UNIT, htobe64(limit->unit));
98 nftnl_expr_limit_parse(
struct nftnl_expr *e,
struct nlattr *attr)
101 struct nlattr *tb[NFTA_LIMIT_MAX+1] = {};
103 if (mnl_attr_parse_nested(attr, nftnl_expr_limit_cb, tb) < 0)
106 if (tb[NFTA_LIMIT_RATE]) {
107 limit->rate = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_RATE]));
108 e->flags |= (1 << NFTNL_EXPR_LIMIT_RATE);
110 if (tb[NFTA_LIMIT_UNIT]) {
111 limit->unit = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_UNIT]));
112 e->flags |= (1 << NFTNL_EXPR_LIMIT_UNIT);
118 static int nftnl_expr_limit_json_parse(
struct nftnl_expr *e, json_t *root,
119 struct nftnl_parse_err *err)
124 if (nftnl_jansson_parse_val(root,
"rate", NFTNL_TYPE_U64, &uval64, err) == 0)
125 nftnl_expr_set_u64(e, NFTNL_EXPR_LIMIT_RATE, uval64);
127 if (nftnl_jansson_parse_val(root,
"unit", NFTNL_TYPE_U64, &uval64, err) == 0)
128 nftnl_expr_set_u64(e, NFTNL_EXPR_LIMIT_UNIT, uval64);
137 static int nftnl_expr_limit_xml_parse(
struct nftnl_expr *e,
139 struct nftnl_parse_err *err)
144 if (nftnl_mxml_num_parse(tree,
"rate", MXML_DESCEND_FIRST, BASE_DEC,
145 &rate, NFTNL_TYPE_U64, NFTNL_XML_MAND, err) == 0)
146 nftnl_expr_set_u64(e, NFTNL_EXPR_LIMIT_RATE, rate);
148 if (nftnl_mxml_num_parse(tree,
"unit", MXML_DESCEND_FIRST, BASE_DEC,
149 &unit, NFTNL_TYPE_U64, NFTNL_XML_MAND, err) == 0)
150 nftnl_expr_set_u64(e, NFTNL_EXPR_LIMIT_UNIT, unit);
159 static const char *get_unit(uint64_t u)
162 case 1:
return "second";
163 case 60:
return "minute";
164 case 60 * 60:
return "hour";
165 case 60 * 60 * 24:
return "day";
166 case 60 * 60 * 24 * 7:
return "week";
171 static int nftnl_expr_limit_export(
char *buf,
size_t size,
172 struct nftnl_expr *e,
int type)
175 NFTNL_BUF_INIT(b, buf, size);
177 if (e->flags & (1 << NFTNL_EXPR_LIMIT_RATE))
178 nftnl_buf_u64(&b, type, limit->rate, RATE);
179 if (e->flags & (1 << NFTNL_EXPR_LIMIT_UNIT))
180 nftnl_buf_u64(&b, type, limit->unit, UNIT);
182 return nftnl_buf_done(&b);
185 static int nftnl_expr_limit_snprintf_default(
char *buf,
size_t len,
186 struct nftnl_expr *e)
190 return snprintf(buf, len,
"rate %"PRIu64
"/%s ",
191 limit->rate, get_unit(limit->unit));
195 nftnl_expr_limit_snprintf(
char *buf,
size_t len, uint32_t type,
196 uint32_t flags,
struct nftnl_expr *e)
200 case NFTNL_OUTPUT_DEFAULT:
201 return nftnl_expr_limit_snprintf_default(buf, len, e);
202 case NFTNL_OUTPUT_XML:
203 case NFTNL_OUTPUT_JSON:
204 return nftnl_expr_limit_export(buf, len, e, type);
211 struct expr_ops expr_ops_limit = {
214 .max_attr = NFTA_LIMIT_MAX,
215 .set = nftnl_expr_limit_set,
216 .get = nftnl_expr_limit_get,
217 .parse = nftnl_expr_limit_parse,
218 .build = nftnl_expr_limit_build,
219 .snprintf = nftnl_expr_limit_snprintf,
220 .xml_parse = nftnl_expr_limit_xml_parse,
221 .json_parse = nftnl_expr_limit_json_parse,