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,
35 static struct obj_ops *nftnl_obj_ops_lookup(uint32_t type)
37 if (type > NFT_OBJECT_MAX)
43 struct nftnl_obj *nftnl_obj_alloc(
void)
45 return calloc(1,
sizeof(
struct nftnl_obj));
47 EXPORT_SYMBOL(nftnl_obj_alloc);
49 void nftnl_obj_free(
const struct nftnl_obj *obj)
51 if (obj->flags & (1 << NFTNL_OBJ_TABLE))
53 if (obj->flags & (1 << NFTNL_OBJ_NAME))
58 EXPORT_SYMBOL(nftnl_obj_free);
60 bool nftnl_obj_is_set(
const struct nftnl_obj *obj, uint16_t attr)
62 return obj->flags & (1 << attr);
64 EXPORT_SYMBOL(nftnl_obj_is_set);
66 static uint32_t nftnl_obj_validate[NFTNL_OBJ_MAX + 1] = {
67 [NFTNL_OBJ_FAMILY] =
sizeof(uint32_t),
68 [NFTNL_OBJ_USE] =
sizeof(uint32_t),
71 void nftnl_obj_set_data(
struct nftnl_obj *obj, uint16_t attr,
72 const void *data, uint32_t data_len)
74 if (attr < NFTNL_OBJ_MAX)
75 nftnl_assert_validate(data, nftnl_obj_validate, attr, data_len);
80 obj->table = strdup(data);
84 obj->name = strdup(data);
87 obj->ops = nftnl_obj_ops_lookup(*((uint32_t *)data));
91 case NFTNL_OBJ_FAMILY:
92 obj->family = *((uint32_t *)data);
95 obj->use = *((uint32_t *)data);
99 obj->ops->set(obj, attr, data, data_len);
102 obj->flags |= (1 << attr);
104 EXPORT_SYMBOL(nftnl_obj_set_data);
106 void nftnl_obj_set(
struct nftnl_obj *obj, uint16_t attr,
const void *data)
108 nftnl_obj_set_data(obj, attr, data, nftnl_obj_validate[attr]);
110 EXPORT_SYMBOL(nftnl_obj_set);
112 void nftnl_obj_set_u8(
struct nftnl_obj *obj, uint16_t attr, uint8_t val)
114 nftnl_obj_set_data(obj, attr, &val,
sizeof(uint8_t));
116 EXPORT_SYMBOL(nftnl_obj_set_u8);
118 void nftnl_obj_set_u16(
struct nftnl_obj *obj, uint16_t attr, uint16_t val)
120 nftnl_obj_set_data(obj, attr, &val,
sizeof(uint16_t));
122 EXPORT_SYMBOL(nftnl_obj_set_u16);
124 void nftnl_obj_set_u32(
struct nftnl_obj *obj, uint16_t attr, uint32_t val)
126 nftnl_obj_set_data(obj, attr, &val,
sizeof(uint32_t));
128 EXPORT_SYMBOL(nftnl_obj_set_u32);
130 void nftnl_obj_set_u64(
struct nftnl_obj *obj, uint16_t attr, uint64_t val)
132 nftnl_obj_set_data(obj, attr, &val,
sizeof(uint64_t));
134 EXPORT_SYMBOL(nftnl_obj_set_u64);
136 void nftnl_obj_set_str(
struct nftnl_obj *obj, uint16_t attr,
const char *str)
138 nftnl_obj_set_data(obj, attr, str, 0);
140 EXPORT_SYMBOL(nftnl_obj_set_str);
142 const void *nftnl_obj_get_data(
struct nftnl_obj *obj, uint16_t attr,
145 if (!(obj->flags & (1 << attr)))
149 case NFTNL_OBJ_TABLE:
157 *data_len =
sizeof(uint32_t);
158 return &obj->ops->type;
159 case NFTNL_OBJ_FAMILY:
160 *data_len =
sizeof(uint32_t);
163 *data_len =
sizeof(uint32_t);
167 return obj->ops->get(obj, attr, data_len);
172 EXPORT_SYMBOL(nftnl_obj_get_data);
174 const void *nftnl_obj_get(
struct nftnl_obj *obj, uint16_t attr)
177 return nftnl_obj_get_data(obj, attr, &data_len);
179 EXPORT_SYMBOL(nftnl_obj_get);
181 uint8_t nftnl_obj_get_u8(
struct nftnl_obj *obj, uint16_t attr)
183 const void *ret = nftnl_obj_get(obj, attr);
184 return ret == NULL ? 0 : *((uint8_t *)ret);
186 EXPORT_SYMBOL(nftnl_obj_get_u8);
188 uint16_t nftnl_obj_get_u16(
struct nftnl_obj *obj, uint16_t attr)
190 const void *ret = nftnl_obj_get(obj, attr);
191 return ret == NULL ? 0 : *((uint16_t *)ret);
193 EXPORT_SYMBOL(nftnl_obj_get_u16);
195 uint32_t nftnl_obj_get_u32(
struct nftnl_obj *obj, uint16_t attr)
197 const void *ret = nftnl_obj_get(obj, attr);
198 return ret == NULL ? 0 : *((uint32_t *)ret);
200 EXPORT_SYMBOL(nftnl_obj_get_u32);
202 uint64_t nftnl_obj_get_u64(
struct nftnl_obj *obj, uint16_t attr)
204 const void *ret = nftnl_obj_get(obj, attr);
205 return ret == NULL ? 0 : *((uint64_t *)ret);
207 EXPORT_SYMBOL(nftnl_obj_get_u64);
209 const char *nftnl_obj_get_str(
struct nftnl_obj *obj, uint16_t attr)
211 return nftnl_obj_get(obj, attr);
213 EXPORT_SYMBOL(nftnl_obj_get_str);
215 void nftnl_obj_nlmsg_build_payload(
struct nlmsghdr *nlh,
216 const struct nftnl_obj *obj)
218 if (obj->flags & (1 << NFTNL_OBJ_TABLE))
219 mnl_attr_put_strz(nlh, NFTA_OBJ_TABLE, obj->table);
220 if (obj->flags & (1 << NFTNL_OBJ_NAME))
221 mnl_attr_put_strz(nlh, NFTA_OBJ_NAME, obj->name);
222 if (obj->flags & (1 << NFTNL_OBJ_TYPE))
223 mnl_attr_put_u32(nlh, NFTA_OBJ_TYPE, htonl(obj->ops->type));
226 struct nlattr *nest = mnl_attr_nest_start(nlh, NFTA_OBJ_DATA);
228 obj->ops->build(nlh, obj);
229 mnl_attr_nest_end(nlh, nest);
232 EXPORT_SYMBOL(nftnl_obj_nlmsg_build_payload);
234 static int nftnl_obj_parse_attr_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_OBJ_MAX) < 0)
245 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
249 if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
253 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
262 int nftnl_obj_nlmsg_parse(
const struct nlmsghdr *nlh,
struct nftnl_obj *obj)
264 struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
265 struct nlattr *tb[NFTA_OBJ_MAX + 1] = {};
268 if (mnl_attr_parse(nlh,
sizeof(*nfg), nftnl_obj_parse_attr_cb, tb) < 0)
271 if (tb[NFTA_OBJ_TABLE]) {
272 obj->table = strdup(mnl_attr_get_str(tb[NFTA_OBJ_TABLE]));
273 obj->flags |= (1 << NFTNL_OBJ_TABLE);
275 if (tb[NFTA_OBJ_NAME]) {
276 obj->name = strdup(mnl_attr_get_str(tb[NFTA_OBJ_NAME]));
277 obj->flags |= (1 << NFTNL_OBJ_NAME);
279 if (tb[NFTA_OBJ_TYPE]) {
280 uint32_t type = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_TYPE]));
282 obj->ops = nftnl_obj_ops_lookup(type);
284 obj->flags |= (1 << NFTNL_OBJ_TYPE);
286 if (tb[NFTA_OBJ_DATA]) {
288 err = obj->ops->parse(obj, tb[NFTA_OBJ_DATA]);
293 if (tb[NFTA_OBJ_USE]) {
294 obj->use = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_USE]));
295 obj->flags |= (1 << NFTNL_OBJ_USE);
298 obj->family = nfg->nfgen_family;
299 obj->flags |= (1 << NFTNL_OBJ_FAMILY);
303 EXPORT_SYMBOL(nftnl_obj_nlmsg_parse);
306 static int nftnl_jansson_parse_obj(
struct nftnl_obj *t, json_t *tree,
307 struct nftnl_parse_err *err)
313 root = nftnl_jansson_get_node(tree,
"obj", err);
317 str = nftnl_jansson_parse_str(root,
"table", err);
319 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, str);
321 str = nftnl_jansson_parse_str(root,
"name", err);
323 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, str);
325 if (nftnl_jansson_parse_val(root,
"type", NFTNL_TYPE_U32, &type,
327 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, type);
333 static int nftnl_obj_json_parse(
struct nftnl_obj *t,
const void *json,
334 struct nftnl_parse_err *err,
335 enum nftnl_parse_input input)
342 tree = nftnl_jansson_create_root(json, &error, err, input);
346 ret = nftnl_jansson_parse_obj(t, tree, err);
348 nftnl_jansson_free_root(tree);
357 static int nftnl_obj_do_parse(
struct nftnl_obj *obj,
enum nftnl_parse_type type,
358 const void *data,
struct nftnl_parse_err *err,
359 enum nftnl_parse_input input)
361 struct nftnl_parse_err perr;
365 case NFTNL_PARSE_JSON:
366 ret = nftnl_obj_json_parse(obj, data, &perr, input);
368 case NFTNL_PARSE_XML:
381 int nftnl_obj_parse(
struct nftnl_obj *obj,
enum nftnl_parse_type type,
382 const char *data,
struct nftnl_parse_err *err)
384 return nftnl_obj_do_parse(obj, type, data, err, NFTNL_PARSE_BUFFER);
386 EXPORT_SYMBOL(nftnl_obj_parse);
388 int nftnl_obj_parse_file(
struct nftnl_obj *obj,
enum nftnl_parse_type type,
389 FILE *fp,
struct nftnl_parse_err *err)
391 return nftnl_obj_do_parse(obj, type, fp, err, NFTNL_PARSE_FILE);
393 EXPORT_SYMBOL(nftnl_obj_parse_file);
395 static int nftnl_obj_export(
char *buf,
size_t size,
396 const struct nftnl_obj *obj,
397 uint32_t type, uint32_t flags)
401 NFTNL_BUF_INIT(b, buf, size);
403 nftnl_buf_open(&b, type, TABLE);
404 if (obj->flags & (1 << NFTNL_OBJ_TABLE))
405 nftnl_buf_str(&b, type, obj->name, NAME);
406 if (obj->flags & (1 << NFTNL_OBJ_NAME))
407 nftnl_buf_str(&b, type, obj->name, NAME);
408 if (obj->flags & (1 << NFTNL_OBJ_FAMILY))
409 nftnl_buf_str(&b, type, nftnl_family2str(obj->family), FAMILY);
410 if (obj->flags & (1 << NFTNL_OBJ_USE))
411 nftnl_buf_u32(&b, type, obj->use, USE);
414 ret = obj->ops->snprintf(buf + b.len, size - b.len, type,
418 nftnl_buf_close(&b, type, TABLE);
420 return nftnl_buf_done(&b);
423 static int nftnl_obj_snprintf_dflt(
char *buf,
size_t size,
424 const struct nftnl_obj *obj,
425 uint32_t type, uint32_t flags)
427 const char *name = obj->ops ? obj->ops->name :
"(unknown)";
428 int ret, remain = size, offset = 0;
430 ret = snprintf(buf, size,
"table %s name %s use %u [ %s ",
431 obj->table, obj->name, obj->use, name);
432 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
435 ret = obj->ops->snprintf(buf + offset, offset, type, flags,
437 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
439 ret = snprintf(buf + offset, offset,
"]");
440 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
445 static int nftnl_obj_cmd_snprintf(
char *buf,
size_t size,
446 const struct nftnl_obj *obj, uint32_t cmd,
447 uint32_t type, uint32_t flags)
449 int ret, remain = size, offset = 0;
451 ret = nftnl_cmd_header_snprintf(buf + offset, remain, cmd, type, flags);
452 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
455 case NFTNL_OUTPUT_DEFAULT:
456 ret = nftnl_obj_snprintf_dflt(buf + offset, remain, obj, type,
459 case NFTNL_OUTPUT_JSON:
460 ret = nftnl_obj_export(buf + offset, remain, obj, type, flags);
462 case NFTNL_OUTPUT_XML:
466 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
468 ret = nftnl_cmd_footer_snprintf(buf + offset, remain, cmd, type, flags);
469 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
474 int nftnl_obj_snprintf(
char *buf,
size_t size,
const struct nftnl_obj *obj,
475 uint32_t type, uint32_t flags)
480 return nftnl_obj_cmd_snprintf(buf, size, obj, nftnl_flag2cmd(flags),
483 EXPORT_SYMBOL(nftnl_obj_snprintf);
485 static int nftnl_obj_do_snprintf(
char *buf,
size_t size,
const void *obj,
486 uint32_t cmd, uint32_t type, uint32_t flags)
488 return nftnl_obj_snprintf(buf, size, obj, type, flags);
491 int nftnl_obj_fprintf(FILE *fp,
const struct nftnl_obj *obj, uint32_t type,
494 return nftnl_fprintf(fp, obj, NFTNL_CMD_UNSPEC, type, flags,
495 nftnl_obj_do_snprintf);
497 EXPORT_SYMBOL(nftnl_obj_fprintf);
500 struct list_head list;
511 INIT_LIST_HEAD(&list->list);
515 EXPORT_SYMBOL(nftnl_obj_list_alloc);
519 struct nftnl_obj *r, *tmp;
521 list_for_each_entry_safe(r, tmp, &list->list, head) {
527 EXPORT_SYMBOL(nftnl_obj_list_free);
531 return list_empty(&list->list);
533 EXPORT_SYMBOL(nftnl_obj_list_is_empty);
535 void nftnl_obj_list_add(
struct nftnl_obj *r,
struct nftnl_obj_list *list)
537 list_add(&r->head, &list->list);
539 EXPORT_SYMBOL(nftnl_obj_list_add);
541 void nftnl_obj_list_add_tail(
struct nftnl_obj *r,
544 list_add_tail(&r->head, &list->list);
546 EXPORT_SYMBOL(nftnl_obj_list_add_tail);
548 void nftnl_obj_list_del(
struct nftnl_obj *t)
552 EXPORT_SYMBOL(nftnl_obj_list_del);
555 int (*cb)(
struct nftnl_obj *t,
void *data),
558 struct nftnl_obj *cur, *tmp;
561 list_for_each_entry_safe(cur, tmp, &table_list->list, head) {
568 EXPORT_SYMBOL(nftnl_obj_list_foreach);
572 struct nftnl_obj *cur;
585 if (nftnl_obj_list_is_empty(l))
588 iter->cur = list_entry(l->list.next,
struct nftnl_obj, head);
592 EXPORT_SYMBOL(nftnl_obj_list_iter_create);
596 struct nftnl_obj *r = iter->cur;
602 iter->cur = list_entry(iter->cur->head.next,
struct nftnl_obj, head);
603 if (&iter->cur->head == iter->list->list.next)
608 EXPORT_SYMBOL(nftnl_obj_list_iter_next);
614 EXPORT_SYMBOL(nftnl_obj_list_iter_destroy);