15#include <netinet/in.h>
17#include <libmnl/libmnl.h>
18#include <linux/netfilter/nfnetlink.h>
19#include <linux/netfilter/nf_tables.h>
21#include <libnftnl/expr.h>
23EXPORT_SYMBOL(nftnl_expr_alloc);
24struct nftnl_expr *nftnl_expr_alloc(
const char *name)
26 struct nftnl_expr *expr;
29 ops = nftnl_expr_ops_lookup(name);
33 expr = calloc(1,
sizeof(
struct nftnl_expr) + ops->alloc_len);
38 expr->flags |= (1 << NFTNL_EXPR_NAME);
47EXPORT_SYMBOL(nftnl_expr_free);
48void nftnl_expr_free(
const struct nftnl_expr *expr)
51 expr->ops->free(expr);
56EXPORT_SYMBOL(nftnl_expr_is_set);
57bool nftnl_expr_is_set(
const struct nftnl_expr *expr, uint16_t type)
59 return expr->flags & (1 << type);
62EXPORT_SYMBOL(nftnl_expr_set);
63int nftnl_expr_set(
struct nftnl_expr *expr, uint16_t type,
64 const void *data, uint32_t data_len)
70 if (type < NFTNL_EXPR_BASE || type > expr->ops->nftnl_max_attr)
73 if (!expr->ops->attr_policy)
76 if (expr->ops->attr_policy[type].maxlen &&
77 expr->ops->attr_policy[type].maxlen < data_len)
80 if (expr->ops->set(expr, type, data, data_len) < 0)
83 expr->flags |= (1 << type);
87EXPORT_SYMBOL(nftnl_expr_set_u8);
89nftnl_expr_set_u8(
struct nftnl_expr *expr, uint16_t type, uint8_t data)
91 nftnl_expr_set(expr, type, &data,
sizeof(uint8_t));
94EXPORT_SYMBOL(nftnl_expr_set_u16);
96nftnl_expr_set_u16(
struct nftnl_expr *expr, uint16_t type, uint16_t data)
98 nftnl_expr_set(expr, type, &data,
sizeof(uint16_t));
101EXPORT_SYMBOL(nftnl_expr_set_u32);
103nftnl_expr_set_u32(
struct nftnl_expr *expr, uint16_t type, uint32_t data)
105 nftnl_expr_set(expr, type, &data,
sizeof(uint32_t));
108EXPORT_SYMBOL(nftnl_expr_set_u64);
110nftnl_expr_set_u64(
struct nftnl_expr *expr, uint16_t type, uint64_t data)
112 nftnl_expr_set(expr, type, &data,
sizeof(uint64_t));
115EXPORT_SYMBOL(nftnl_expr_set_str);
116int nftnl_expr_set_str(
struct nftnl_expr *expr, uint16_t type,
const char *str)
118 return nftnl_expr_set(expr, type, str, strlen(str) + 1);
121EXPORT_SYMBOL(nftnl_expr_get);
122const void *nftnl_expr_get(
const struct nftnl_expr *expr,
123 uint16_t type, uint32_t *data_len)
127 if (!(expr->flags & (1 << type)))
131 case NFTNL_EXPR_NAME:
132 *data_len = strlen(expr->ops->name) + 1;
133 ret = expr->ops->name;
136 ret = expr->ops->get(expr, type, data_len);
143EXPORT_SYMBOL(nftnl_expr_get_u8);
144uint8_t nftnl_expr_get_u8(
const struct nftnl_expr *expr, uint16_t type)
149 data = nftnl_expr_get(expr, type, &data_len);
153 if (data_len !=
sizeof(uint8_t))
156 return *((uint8_t *)data);
159EXPORT_SYMBOL(nftnl_expr_get_u16);
160uint16_t nftnl_expr_get_u16(
const struct nftnl_expr *expr, uint16_t type)
165 data = nftnl_expr_get(expr, type, &data_len);
169 if (data_len !=
sizeof(uint16_t))
172 return *((uint16_t *)data);
175EXPORT_SYMBOL(nftnl_expr_get_u32);
176uint32_t nftnl_expr_get_u32(
const struct nftnl_expr *expr, uint16_t type)
181 data = nftnl_expr_get(expr, type, &data_len);
185 if (data_len !=
sizeof(uint32_t))
188 return *((uint32_t *)data);
191EXPORT_SYMBOL(nftnl_expr_get_u64);
192uint64_t nftnl_expr_get_u64(
const struct nftnl_expr *expr, uint16_t type)
197 data = nftnl_expr_get(expr, type, &data_len);
201 if (data_len !=
sizeof(uint64_t))
204 return *((uint64_t *)data);
207EXPORT_SYMBOL(nftnl_expr_get_str);
208const char *nftnl_expr_get_str(
const struct nftnl_expr *expr, uint16_t type)
212 return (
const char *)nftnl_expr_get(expr, type, &data_len);
215EXPORT_SYMBOL(nftnl_expr_build_payload);
216void nftnl_expr_build_payload(
struct nlmsghdr *nlh,
struct nftnl_expr *expr)
220 mnl_attr_put_strz(nlh, NFTA_EXPR_NAME, expr->ops->name);
222 if (!expr->ops->build)
225 nest = mnl_attr_nest_start(nlh, NFTA_EXPR_DATA);
226 expr->ops->build(nlh, expr);
227 mnl_attr_nest_end(nlh, nest);
230static int nftnl_rule_parse_expr_cb(
const struct nlattr *attr,
void *data)
232 const struct nlattr **tb = data;
233 int type = mnl_attr_get_type(attr);
235 if (mnl_attr_type_valid(attr, NFTA_EXPR_MAX) < 0)
240 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
244 if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
253struct nftnl_expr *nftnl_expr_parse(
struct nlattr *attr)
255 struct nlattr *tb[NFTA_EXPR_MAX+1] = {};
256 struct nftnl_expr *expr;
258 if (mnl_attr_parse_nested(attr, nftnl_rule_parse_expr_cb, tb) < 0)
261 expr = nftnl_expr_alloc(mnl_attr_get_str(tb[NFTA_EXPR_NAME]));
265 if (tb[NFTA_EXPR_DATA] &&
267 expr->ops->parse(expr, tb[NFTA_EXPR_DATA]) < 0)
278EXPORT_SYMBOL(nftnl_expr_snprintf);
279int nftnl_expr_snprintf(
char *buf,
size_t remain,
const struct nftnl_expr *expr,
280 uint32_t type, uint32_t flags)
283 unsigned int offset = 0;
288 if (!expr->ops->output || type != NFTNL_OUTPUT_DEFAULT)
291 ret = expr->ops->output(buf + offset, remain, flags, expr);
292 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
297static int nftnl_expr_do_snprintf(
char *buf,
size_t size,
const void *e,
298 uint32_t cmd, uint32_t type, uint32_t flags)
300 return nftnl_expr_snprintf(buf, size, e, type, flags);
303EXPORT_SYMBOL(nftnl_expr_fprintf);
304int nftnl_expr_fprintf(FILE *fp,
const struct nftnl_expr *expr, uint32_t type,
307 return nftnl_fprintf(fp, expr, NFTNL_CMD_UNSPEC, type, flags,
308 nftnl_expr_do_snprintf);