19#include <netinet/in.h>
21#include <libmnl/libmnl.h>
22#include <linux/netfilter/nfnetlink.h>
23#include <linux/netfilter/nf_tables.h>
25#include <libnftnl/expr.h>
27EXPORT_SYMBOL(nftnl_expr_alloc);
28struct nftnl_expr *nftnl_expr_alloc(
const char *name)
30 struct nftnl_expr *expr;
33 ops = nftnl_expr_ops_lookup(name);
37 expr = calloc(1,
sizeof(
struct nftnl_expr) + ops->alloc_len);
42 expr->flags |= (1 << NFTNL_EXPR_NAME);
51EXPORT_SYMBOL(nftnl_expr_free);
52void nftnl_expr_free(
const struct nftnl_expr *expr)
55 expr->ops->free(expr);
60EXPORT_SYMBOL(nftnl_expr_is_set);
61bool nftnl_expr_is_set(
const struct nftnl_expr *expr, uint16_t type)
63 return expr->flags & (1 << type);
66EXPORT_SYMBOL(nftnl_expr_set);
67int nftnl_expr_set(
struct nftnl_expr *expr, uint16_t type,
68 const void *data, uint32_t data_len)
74 if (type < NFTNL_EXPR_BASE || type > expr->ops->nftnl_max_attr)
77 if (!expr->ops->attr_policy)
80 if (expr->ops->attr_policy[type].maxlen &&
81 expr->ops->attr_policy[type].maxlen < data_len)
84 if (expr->ops->set(expr, type, data, data_len) < 0)
87 expr->flags |= (1 << type);
91EXPORT_SYMBOL(nftnl_expr_set_u8);
93nftnl_expr_set_u8(
struct nftnl_expr *expr, uint16_t type, uint8_t data)
95 nftnl_expr_set(expr, type, &data,
sizeof(uint8_t));
98EXPORT_SYMBOL(nftnl_expr_set_u16);
100nftnl_expr_set_u16(
struct nftnl_expr *expr, uint16_t type, uint16_t data)
102 nftnl_expr_set(expr, type, &data,
sizeof(uint16_t));
105EXPORT_SYMBOL(nftnl_expr_set_u32);
107nftnl_expr_set_u32(
struct nftnl_expr *expr, uint16_t type, uint32_t data)
109 nftnl_expr_set(expr, type, &data,
sizeof(uint32_t));
112EXPORT_SYMBOL(nftnl_expr_set_u64);
114nftnl_expr_set_u64(
struct nftnl_expr *expr, uint16_t type, uint64_t data)
116 nftnl_expr_set(expr, type, &data,
sizeof(uint64_t));
119EXPORT_SYMBOL(nftnl_expr_set_str);
120int nftnl_expr_set_str(
struct nftnl_expr *expr, uint16_t type,
const char *str)
122 return nftnl_expr_set(expr, type, str, strlen(str) + 1);
125EXPORT_SYMBOL(nftnl_expr_get);
126const void *nftnl_expr_get(
const struct nftnl_expr *expr,
127 uint16_t type, uint32_t *data_len)
131 if (!(expr->flags & (1 << type)))
135 case NFTNL_EXPR_NAME:
136 *data_len = strlen(expr->ops->name) + 1;
137 ret = expr->ops->name;
140 ret = expr->ops->get(expr, type, data_len);
147EXPORT_SYMBOL(nftnl_expr_get_u8);
148uint8_t nftnl_expr_get_u8(
const struct nftnl_expr *expr, uint16_t type)
153 data = nftnl_expr_get(expr, type, &data_len);
157 if (data_len !=
sizeof(uint8_t))
160 return *((uint8_t *)data);
163EXPORT_SYMBOL(nftnl_expr_get_u16);
164uint16_t nftnl_expr_get_u16(
const struct nftnl_expr *expr, uint16_t type)
169 data = nftnl_expr_get(expr, type, &data_len);
173 if (data_len !=
sizeof(uint16_t))
176 return *((uint16_t *)data);
179EXPORT_SYMBOL(nftnl_expr_get_u32);
180uint32_t nftnl_expr_get_u32(
const struct nftnl_expr *expr, uint16_t type)
185 data = nftnl_expr_get(expr, type, &data_len);
189 if (data_len !=
sizeof(uint32_t))
192 return *((uint32_t *)data);
195EXPORT_SYMBOL(nftnl_expr_get_u64);
196uint64_t nftnl_expr_get_u64(
const struct nftnl_expr *expr, uint16_t type)
201 data = nftnl_expr_get(expr, type, &data_len);
205 if (data_len !=
sizeof(uint64_t))
208 return *((uint64_t *)data);
211EXPORT_SYMBOL(nftnl_expr_get_str);
212const char *nftnl_expr_get_str(
const struct nftnl_expr *expr, uint16_t type)
216 return (
const char *)nftnl_expr_get(expr, type, &data_len);
219EXPORT_SYMBOL(nftnl_expr_build_payload);
220void nftnl_expr_build_payload(
struct nlmsghdr *nlh,
struct nftnl_expr *expr)
224 mnl_attr_put_strz(nlh, NFTA_EXPR_NAME, expr->ops->name);
226 if (!expr->ops->build)
229 nest = mnl_attr_nest_start(nlh, NFTA_EXPR_DATA);
230 expr->ops->build(nlh, expr);
231 mnl_attr_nest_end(nlh, nest);
234static int nftnl_rule_parse_expr_cb(
const struct nlattr *attr,
void *data)
236 const struct nlattr **tb = data;
237 int type = mnl_attr_get_type(attr);
239 if (mnl_attr_type_valid(attr, NFTA_EXPR_MAX) < 0)
244 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
248 if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
257struct nftnl_expr *nftnl_expr_parse(
struct nlattr *attr)
259 struct nlattr *tb[NFTA_EXPR_MAX+1] = {};
260 struct nftnl_expr *expr;
262 if (mnl_attr_parse_nested(attr, nftnl_rule_parse_expr_cb, tb) < 0)
265 expr = nftnl_expr_alloc(mnl_attr_get_str(tb[NFTA_EXPR_NAME]));
269 if (tb[NFTA_EXPR_DATA] &&
271 expr->ops->parse(expr, tb[NFTA_EXPR_DATA]) < 0)
282EXPORT_SYMBOL(nftnl_expr_snprintf);
283int nftnl_expr_snprintf(
char *buf,
size_t remain,
const struct nftnl_expr *expr,
284 uint32_t type, uint32_t flags)
287 unsigned int offset = 0;
292 if (!expr->ops->output || type != NFTNL_OUTPUT_DEFAULT)
295 ret = expr->ops->output(buf + offset, remain, flags, expr);
296 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
301static int nftnl_expr_do_snprintf(
char *buf,
size_t size,
const void *e,
302 uint32_t cmd, uint32_t type, uint32_t flags)
304 return nftnl_expr_snprintf(buf, size, e, type, flags);
307EXPORT_SYMBOL(nftnl_expr_fprintf);
308int nftnl_expr_fprintf(FILE *fp,
const struct nftnl_expr *expr, uint32_t type,
311 return nftnl_fprintf(fp, expr, NFTNL_CMD_UNSPEC, type, flags,
312 nftnl_expr_do_snprintf);