17 #include <netinet/in.h>
20 #include <libmnl/libmnl.h>
21 #include <linux/netfilter/nfnetlink.h>
22 #include <linux/netfilter/nf_tables.h>
24 #include <libnftnl/object.h>
28 static struct obj_ops *obj_ops[] = {
29 [NFT_OBJECT_COUNTER] = &obj_ops_counter,
30 [NFT_OBJECT_QUOTA] = &obj_ops_quota,
31 [NFT_OBJECT_CT_HELPER] = &obj_ops_ct_helper,
32 [NFT_OBJECT_LIMIT] = &obj_ops_limit,
33 [NFT_OBJECT_TUNNEL] = &obj_ops_tunnel,
34 [NFT_OBJECT_CT_TIMEOUT] = &obj_ops_ct_timeout,
35 [NFT_OBJECT_SECMARK] = &obj_ops_secmark,
38 static struct obj_ops *nftnl_obj_ops_lookup(uint32_t type)
40 if (type > NFT_OBJECT_MAX)
46 EXPORT_SYMBOL(nftnl_obj_alloc);
47 struct nftnl_obj *nftnl_obj_alloc(
void)
49 return calloc(1,
sizeof(
struct nftnl_obj));
52 EXPORT_SYMBOL(nftnl_obj_free);
53 void nftnl_obj_free(
const struct nftnl_obj *obj)
55 if (obj->flags & (1 << NFTNL_OBJ_TABLE))
57 if (obj->flags & (1 << NFTNL_OBJ_NAME))
63 EXPORT_SYMBOL(nftnl_obj_is_set);
64 bool nftnl_obj_is_set(
const struct nftnl_obj *obj, uint16_t attr)
66 return obj->flags & (1 << attr);
69 static uint32_t nftnl_obj_validate[NFTNL_OBJ_MAX + 1] = {
70 [NFTNL_OBJ_FAMILY] =
sizeof(uint32_t),
71 [NFTNL_OBJ_USE] =
sizeof(uint32_t),
72 [NFTNL_OBJ_HANDLE] =
sizeof(uint64_t),
75 EXPORT_SYMBOL(nftnl_obj_set_data);
76 void nftnl_obj_set_data(
struct nftnl_obj *obj, uint16_t attr,
77 const void *data, uint32_t data_len)
79 if (attr < NFTNL_OBJ_MAX)
80 nftnl_assert_validate(data, nftnl_obj_validate, attr, data_len);
85 obj->table = strdup(data);
89 obj->name = strdup(data);
92 obj->ops = nftnl_obj_ops_lookup(*((uint32_t *)data));
96 case NFTNL_OBJ_FAMILY:
97 memcpy(&obj->family, data,
sizeof(obj->family));
100 memcpy(&obj->use, data,
sizeof(obj->use));
102 case NFTNL_OBJ_HANDLE:
103 memcpy(&obj->handle, data,
sizeof(obj->handle));
107 obj->ops->set(obj, attr, data, data_len);
110 obj->flags |= (1 << attr);
113 EXPORT_SYMBOL(nftnl_obj_set);
114 void nftnl_obj_set(
struct nftnl_obj *obj, uint16_t attr,
const void *data)
116 nftnl_obj_set_data(obj, attr, data, nftnl_obj_validate[attr]);
119 EXPORT_SYMBOL(nftnl_obj_set_u8);
120 void nftnl_obj_set_u8(
struct nftnl_obj *obj, uint16_t attr, uint8_t val)
122 nftnl_obj_set_data(obj, attr, &val,
sizeof(uint8_t));
125 EXPORT_SYMBOL(nftnl_obj_set_u16);
126 void nftnl_obj_set_u16(
struct nftnl_obj *obj, uint16_t attr, uint16_t val)
128 nftnl_obj_set_data(obj, attr, &val,
sizeof(uint16_t));
131 EXPORT_SYMBOL(nftnl_obj_set_u32);
132 void nftnl_obj_set_u32(
struct nftnl_obj *obj, uint16_t attr, uint32_t val)
134 nftnl_obj_set_data(obj, attr, &val,
sizeof(uint32_t));
137 EXPORT_SYMBOL(nftnl_obj_set_u64);
138 void nftnl_obj_set_u64(
struct nftnl_obj *obj, uint16_t attr, uint64_t val)
140 nftnl_obj_set_data(obj, attr, &val,
sizeof(uint64_t));
143 EXPORT_SYMBOL(nftnl_obj_set_str);
144 void nftnl_obj_set_str(
struct nftnl_obj *obj, uint16_t attr,
const char *str)
146 nftnl_obj_set_data(obj, attr, str, 0);
149 EXPORT_SYMBOL(nftnl_obj_get_data);
150 const void *nftnl_obj_get_data(
struct nftnl_obj *obj, uint16_t attr,
153 if (!(obj->flags & (1 << attr)))
157 case NFTNL_OBJ_TABLE:
165 *data_len =
sizeof(uint32_t);
166 return &obj->ops->type;
167 case NFTNL_OBJ_FAMILY:
168 *data_len =
sizeof(uint32_t);
171 *data_len =
sizeof(uint32_t);
173 case NFTNL_OBJ_HANDLE:
174 *data_len =
sizeof(uint64_t);
178 return obj->ops->get(obj, attr, data_len);
184 EXPORT_SYMBOL(nftnl_obj_get);
185 const void *nftnl_obj_get(
struct nftnl_obj *obj, uint16_t attr)
188 return nftnl_obj_get_data(obj, attr, &data_len);
191 EXPORT_SYMBOL(nftnl_obj_get_u8);
192 uint8_t nftnl_obj_get_u8(
struct nftnl_obj *obj, uint16_t attr)
194 const void *ret = nftnl_obj_get(obj, attr);
195 return ret == NULL ? 0 : *((uint8_t *)ret);
198 EXPORT_SYMBOL(nftnl_obj_get_u16);
199 uint16_t nftnl_obj_get_u16(
struct nftnl_obj *obj, uint16_t attr)
201 const void *ret = nftnl_obj_get(obj, attr);
202 return ret == NULL ? 0 : *((uint16_t *)ret);
205 EXPORT_SYMBOL(nftnl_obj_get_u32);
206 uint32_t nftnl_obj_get_u32(
struct nftnl_obj *obj, uint16_t attr)
208 const void *ret = nftnl_obj_get(obj, attr);
209 return ret == NULL ? 0 : *((uint32_t *)ret);
212 EXPORT_SYMBOL(nftnl_obj_get_u64);
213 uint64_t nftnl_obj_get_u64(
struct nftnl_obj *obj, uint16_t attr)
215 const void *ret = nftnl_obj_get(obj, attr);
216 return ret == NULL ? 0 : *((uint64_t *)ret);
219 EXPORT_SYMBOL(nftnl_obj_get_str);
220 const char *nftnl_obj_get_str(
struct nftnl_obj *obj, uint16_t attr)
222 return nftnl_obj_get(obj, attr);
225 EXPORT_SYMBOL(nftnl_obj_nlmsg_build_payload);
226 void nftnl_obj_nlmsg_build_payload(
struct nlmsghdr *nlh,
227 const struct nftnl_obj *obj)
229 if (obj->flags & (1 << NFTNL_OBJ_TABLE))
230 mnl_attr_put_strz(nlh, NFTA_OBJ_TABLE, obj->table);
231 if (obj->flags & (1 << NFTNL_OBJ_NAME))
232 mnl_attr_put_strz(nlh, NFTA_OBJ_NAME, obj->name);
233 if (obj->flags & (1 << NFTNL_OBJ_TYPE))
234 mnl_attr_put_u32(nlh, NFTA_OBJ_TYPE, htonl(obj->ops->type));
235 if (obj->flags & (1 << NFTNL_OBJ_HANDLE))
236 mnl_attr_put_u64(nlh, NFTA_OBJ_HANDLE, htobe64(obj->handle));
238 struct nlattr *nest = mnl_attr_nest_start(nlh, NFTA_OBJ_DATA);
240 obj->ops->build(nlh, obj);
241 mnl_attr_nest_end(nlh, nest);
245 static int nftnl_obj_parse_attr_cb(
const struct nlattr *attr,
void *data)
247 const struct nlattr **tb = data;
248 int type = mnl_attr_get_type(attr);
250 if (mnl_attr_type_valid(attr, NFTA_OBJ_MAX) < 0)
256 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
259 case NFTA_OBJ_HANDLE:
260 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
264 if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
268 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
277 EXPORT_SYMBOL(nftnl_obj_nlmsg_parse);
278 int nftnl_obj_nlmsg_parse(
const struct nlmsghdr *nlh,
struct nftnl_obj *obj)
280 struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
281 struct nlattr *tb[NFTA_OBJ_MAX + 1] = {};
284 if (mnl_attr_parse(nlh,
sizeof(*nfg), nftnl_obj_parse_attr_cb, tb) < 0)
287 if (tb[NFTA_OBJ_TABLE]) {
288 obj->table = strdup(mnl_attr_get_str(tb[NFTA_OBJ_TABLE]));
289 obj->flags |= (1 << NFTNL_OBJ_TABLE);
291 if (tb[NFTA_OBJ_NAME]) {
292 obj->name = strdup(mnl_attr_get_str(tb[NFTA_OBJ_NAME]));
293 obj->flags |= (1 << NFTNL_OBJ_NAME);
295 if (tb[NFTA_OBJ_TYPE]) {
296 uint32_t type = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_TYPE]));
298 obj->ops = nftnl_obj_ops_lookup(type);
300 obj->flags |= (1 << NFTNL_OBJ_TYPE);
302 if (tb[NFTA_OBJ_DATA]) {
304 err = obj->ops->parse(obj, tb[NFTA_OBJ_DATA]);
309 if (tb[NFTA_OBJ_USE]) {
310 obj->use = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_USE]));
311 obj->flags |= (1 << NFTNL_OBJ_USE);
313 if (tb[NFTA_OBJ_HANDLE]) {
314 obj->handle = be64toh(mnl_attr_get_u64(tb[NFTA_OBJ_HANDLE]));
315 obj->flags |= (1 << NFTNL_OBJ_HANDLE);
318 obj->family = nfg->nfgen_family;
319 obj->flags |= (1 << NFTNL_OBJ_FAMILY);
324 static int nftnl_obj_do_parse(
struct nftnl_obj *obj,
enum nftnl_parse_type type,
325 const void *data,
struct nftnl_parse_err *err,
326 enum nftnl_parse_input input)
328 struct nftnl_parse_err perr = {};
332 case NFTNL_PARSE_JSON:
333 case NFTNL_PARSE_XML:
346 EXPORT_SYMBOL(nftnl_obj_parse);
347 int nftnl_obj_parse(
struct nftnl_obj *obj,
enum nftnl_parse_type type,
348 const char *data,
struct nftnl_parse_err *err)
350 return nftnl_obj_do_parse(obj, type, data, err, NFTNL_PARSE_BUFFER);
353 EXPORT_SYMBOL(nftnl_obj_parse_file);
354 int nftnl_obj_parse_file(
struct nftnl_obj *obj,
enum nftnl_parse_type type,
355 FILE *fp,
struct nftnl_parse_err *err)
357 return nftnl_obj_do_parse(obj, type, fp, err, NFTNL_PARSE_FILE);
360 static int nftnl_obj_snprintf_dflt(
char *buf,
size_t size,
361 const struct nftnl_obj *obj,
362 uint32_t type, uint32_t flags)
364 const char *name = obj->ops ? obj->ops->name :
"(unknown)";
365 int ret, remain = size, offset = 0;
367 ret = snprintf(buf, size,
"table %s name %s use %u [ %s ",
368 obj->table, obj->name, obj->use, name);
369 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
372 ret = obj->ops->snprintf(buf + offset, offset, type, flags,
374 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
376 ret = snprintf(buf + offset, offset,
"]");
377 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
382 static int nftnl_obj_cmd_snprintf(
char *buf,
size_t size,
383 const struct nftnl_obj *obj, uint32_t cmd,
384 uint32_t type, uint32_t flags)
386 int ret, remain = size, offset = 0;
389 case NFTNL_OUTPUT_DEFAULT:
390 ret = nftnl_obj_snprintf_dflt(buf + offset, remain, obj, type,
393 case NFTNL_OUTPUT_JSON:
394 case NFTNL_OUTPUT_XML:
398 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
403 EXPORT_SYMBOL(nftnl_obj_snprintf);
404 int nftnl_obj_snprintf(
char *buf,
size_t size,
const struct nftnl_obj *obj,
405 uint32_t type, uint32_t flags)
410 return nftnl_obj_cmd_snprintf(buf, size, obj, nftnl_flag2cmd(flags),
414 static int nftnl_obj_do_snprintf(
char *buf,
size_t size,
const void *obj,
415 uint32_t cmd, uint32_t type, uint32_t flags)
417 return nftnl_obj_snprintf(buf, size, obj, type, flags);
420 EXPORT_SYMBOL(nftnl_obj_fprintf);
421 int nftnl_obj_fprintf(FILE *fp,
const struct nftnl_obj *obj, uint32_t type,
424 return nftnl_fprintf(fp, obj, NFTNL_CMD_UNSPEC, type, flags,
425 nftnl_obj_do_snprintf);
429 struct list_head list;
432 EXPORT_SYMBOL(nftnl_obj_list_alloc);
441 INIT_LIST_HEAD(&list->list);
446 EXPORT_SYMBOL(nftnl_obj_list_free);
449 struct nftnl_obj *r, *tmp;
451 list_for_each_entry_safe(r, tmp, &list->list, head) {
458 EXPORT_SYMBOL(nftnl_obj_list_is_empty);
461 return list_empty(&list->list);
464 EXPORT_SYMBOL(nftnl_obj_list_add);
465 void nftnl_obj_list_add(
struct nftnl_obj *r,
struct nftnl_obj_list *list)
467 list_add(&r->head, &list->list);
470 EXPORT_SYMBOL(nftnl_obj_list_add_tail);
471 void nftnl_obj_list_add_tail(
struct nftnl_obj *r,
474 list_add_tail(&r->head, &list->list);
477 EXPORT_SYMBOL(nftnl_obj_list_del);
478 void nftnl_obj_list_del(
struct nftnl_obj *t)
483 EXPORT_SYMBOL(nftnl_obj_list_foreach);
485 int (*cb)(
struct nftnl_obj *t,
void *data),
488 struct nftnl_obj *cur, *tmp;
491 list_for_each_entry_safe(cur, tmp, &table_list->list, head) {
501 struct nftnl_obj *cur;
504 EXPORT_SYMBOL(nftnl_obj_list_iter_create);
515 if (nftnl_obj_list_is_empty(l))
518 iter->cur = list_entry(l->list.next,
struct nftnl_obj, head);
523 EXPORT_SYMBOL(nftnl_obj_list_iter_next);
526 struct nftnl_obj *r = iter->cur;
532 iter->cur = list_entry(iter->cur->head.next,
struct nftnl_obj, head);
533 if (&iter->cur->head == iter->list->list.next)
539 EXPORT_SYMBOL(nftnl_obj_list_iter_destroy);