libnftnl  1.0.6
table.c
1 /*
2  * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
10  */
11 #include "internal.h"
12 
13 #include <time.h>
14 #include <endian.h>
15 #include <stdint.h>
16 #include <limits.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <netinet/in.h>
20 #include <errno.h>
21 
22 #include <libmnl/libmnl.h>
23 #include <linux/netfilter/nfnetlink.h>
24 #include <linux/netfilter/nf_tables.h>
25 
26 #include <libnftnl/table.h>
27 #include <buffer.h>
28 
29 struct nftnl_table {
30  struct list_head head;
31 
32  const char *name;
33  uint32_t family;
34  uint32_t table_flags;
35  uint32_t use;
36  uint32_t flags;
37 };
38 
39 struct nftnl_table *nftnl_table_alloc(void)
40 {
41  return calloc(1, sizeof(struct nftnl_table));
42 }
43 EXPORT_SYMBOL_ALIAS(nftnl_table_alloc, nft_table_alloc);
44 
45 void nftnl_table_free(const struct nftnl_table *t)
46 {
47  if (t->flags & (1 << NFTNL_TABLE_NAME))
48  xfree(t->name);
49 
50  xfree(t);
51 }
52 EXPORT_SYMBOL_ALIAS(nftnl_table_free, nft_table_free);
53 
54 bool nftnl_table_is_set(const struct nftnl_table *t, uint16_t attr)
55 {
56  return t->flags & (1 << attr);
57 }
58 EXPORT_SYMBOL_ALIAS(nftnl_table_is_set, nft_table_attr_is_set);
59 
60 void nftnl_table_unset(struct nftnl_table *t, uint16_t attr)
61 {
62  if (!(t->flags & (1 << attr)))
63  return;
64 
65  switch (attr) {
66  case NFTNL_TABLE_NAME:
67  if (t->name) {
68  xfree(t->name);
69  t->name = NULL;
70  }
71  break;
72  case NFTNL_TABLE_FLAGS:
73  case NFTNL_TABLE_FAMILY:
74  break;
75  case NFTNL_TABLE_USE:
76  break;
77  }
78  t->flags &= ~(1 << attr);
79 }
80 EXPORT_SYMBOL_ALIAS(nftnl_table_unset, nft_table_attr_unset);
81 
82 static uint32_t nftnl_table_validate[NFTNL_TABLE_MAX + 1] = {
83  [NFTNL_TABLE_FLAGS] = sizeof(uint32_t),
84  [NFTNL_TABLE_FAMILY] = sizeof(uint32_t),
85 };
86 
87 void nftnl_table_set_data(struct nftnl_table *t, uint16_t attr,
88  const void *data, uint32_t data_len)
89 {
90  if (attr > NFTNL_TABLE_MAX)
91  return;
92 
93  nftnl_assert_validate(data, nftnl_table_validate, attr, data_len);
94 
95  switch (attr) {
96  case NFTNL_TABLE_NAME:
97  if (t->name)
98  xfree(t->name);
99 
100  t->name = strdup(data);
101  break;
102  case NFTNL_TABLE_FLAGS:
103  t->table_flags = *((uint32_t *)data);
104  break;
105  case NFTNL_TABLE_FAMILY:
106  t->family = *((uint32_t *)data);
107  break;
108  case NFTNL_TABLE_USE:
109  t->use = *((uint32_t *)data);
110  break;
111  }
112  t->flags |= (1 << attr);
113 }
114 EXPORT_SYMBOL_ALIAS(nftnl_table_set_data, nft_table_attr_set_data);
115 
116 void nftnl_table_set(struct nftnl_table *t, uint16_t attr, const void *data)
117 {
118  nftnl_table_set_data(t, attr, data, nftnl_table_validate[attr]);
119 }
120 EXPORT_SYMBOL_ALIAS(nftnl_table_set, nft_table_attr_set);
121 
122 void nftnl_table_set_u32(struct nftnl_table *t, uint16_t attr, uint32_t val)
123 {
124  nftnl_table_set_data(t, attr, &val, sizeof(uint32_t));
125 }
126 EXPORT_SYMBOL_ALIAS(nftnl_table_set_u32, nft_table_attr_set_u32);
127 
128 void nftnl_table_set_u8(struct nftnl_table *t, uint16_t attr, uint8_t val)
129 {
130  nftnl_table_set_data(t, attr, &val, sizeof(uint8_t));
131 }
132 EXPORT_SYMBOL_ALIAS(nftnl_table_set_u8, nft_table_attr_set_u8);
133 
134 void nftnl_table_set_str(struct nftnl_table *t, uint16_t attr, const char *str)
135 {
136  nftnl_table_set_data(t, attr, str, 0);
137 }
138 EXPORT_SYMBOL_ALIAS(nftnl_table_set_str, nft_table_attr_set_str);
139 
140 const void *nftnl_table_get_data(const struct nftnl_table *t, uint16_t attr,
141  uint32_t *data_len)
142 {
143  if (!(t->flags & (1 << attr)))
144  return NULL;
145 
146  switch(attr) {
147  case NFTNL_TABLE_NAME:
148  return t->name;
149  case NFTNL_TABLE_FLAGS:
150  *data_len = sizeof(uint32_t);
151  return &t->table_flags;
152  case NFTNL_TABLE_FAMILY:
153  *data_len = sizeof(uint32_t);
154  return &t->family;
155  case NFTNL_TABLE_USE:
156  *data_len = sizeof(uint32_t);
157  return &t->use;
158  }
159  return NULL;
160 }
161 EXPORT_SYMBOL_ALIAS(nftnl_table_get_data, nft_table_attr_get_data);
162 
163 const void *nftnl_table_get(const struct nftnl_table *t, uint16_t attr)
164 {
165  uint32_t data_len;
166  return nftnl_table_get_data(t, attr, &data_len);
167 }
168 EXPORT_SYMBOL_ALIAS(nftnl_table_get, nft_table_attr_get);
169 
170 uint32_t nftnl_table_get_u32(const struct nftnl_table *t, uint16_t attr)
171 {
172  const void *ret = nftnl_table_get(t, attr);
173  return ret == NULL ? 0 : *((uint32_t *)ret);
174 }
175 EXPORT_SYMBOL_ALIAS(nftnl_table_get_u32, nft_table_attr_get_u32);
176 
177 uint8_t nftnl_table_get_u8(const struct nftnl_table *t, uint16_t attr)
178 {
179  const void *ret = nftnl_table_get(t, attr);
180  return ret == NULL ? 0 : *((uint8_t *)ret);
181 }
182 EXPORT_SYMBOL_ALIAS(nftnl_table_get_u8, nft_table_attr_get_u8);
183 
184 const char *nftnl_table_get_str(const struct nftnl_table *t, uint16_t attr)
185 {
186  return nftnl_table_get(t, attr);
187 }
188 EXPORT_SYMBOL_ALIAS(nftnl_table_get_str, nft_table_attr_get_str);
189 
190 void nftnl_table_nlmsg_build_payload(struct nlmsghdr *nlh, const struct nftnl_table *t)
191 {
192  if (t->flags & (1 << NFTNL_TABLE_NAME))
193  mnl_attr_put_strz(nlh, NFTA_TABLE_NAME, t->name);
194  if (t->flags & (1 << NFTNL_TABLE_FLAGS))
195  mnl_attr_put_u32(nlh, NFTA_TABLE_FLAGS, htonl(t->table_flags));
196 }
197 EXPORT_SYMBOL_ALIAS(nftnl_table_nlmsg_build_payload, nft_table_nlmsg_build_payload);
198 
199 static int nftnl_table_parse_attr_cb(const struct nlattr *attr, void *data)
200 {
201  const struct nlattr **tb = data;
202  int type = mnl_attr_get_type(attr);
203 
204  if (mnl_attr_type_valid(attr, NFTA_TABLE_MAX) < 0)
205  return MNL_CB_OK;
206 
207  switch(type) {
208  case NFTA_TABLE_NAME:
209  if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
210  abi_breakage();
211  break;
212  case NFTA_TABLE_FLAGS:
213  case NFTA_TABLE_USE:
214  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
215  abi_breakage();
216  break;
217  }
218 
219  tb[type] = attr;
220  return MNL_CB_OK;
221 }
222 
223 int nftnl_table_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_table *t)
224 {
225  struct nlattr *tb[NFTA_TABLE_MAX+1] = {};
226  struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
227 
228  if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_table_parse_attr_cb, tb) < 0)
229  return -1;
230 
231  if (tb[NFTA_TABLE_NAME]) {
232  xfree(t->name);
233  t->name = strdup(mnl_attr_get_str(tb[NFTA_TABLE_NAME]));
234  t->flags |= (1 << NFTNL_TABLE_NAME);
235  }
236  if (tb[NFTA_TABLE_FLAGS]) {
237  t->table_flags = ntohl(mnl_attr_get_u32(tb[NFTA_TABLE_FLAGS]));
238  t->flags |= (1 << NFTNL_TABLE_FLAGS);
239  }
240  if (tb[NFTA_TABLE_USE]) {
241  t->use = ntohl(mnl_attr_get_u32(tb[NFTA_TABLE_USE]));
242  t->flags |= (1 << NFTNL_TABLE_USE);
243  }
244 
245  t->family = nfg->nfgen_family;
246  t->flags |= (1 << NFTNL_TABLE_FAMILY);
247 
248  return 0;
249 }
250 EXPORT_SYMBOL_ALIAS(nftnl_table_nlmsg_parse, nft_table_nlmsg_parse);
251 
252 #ifdef XML_PARSING
253 int nftnl_mxml_table_parse(mxml_node_t *tree, struct nftnl_table *t,
254  struct nftnl_parse_err *err)
255 {
256  const char *name;
257  int family;
258  uint32_t flags, use;
259 
260  name = nftnl_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST,
261  NFTNL_XML_MAND, err);
262  if (name != NULL)
263  nftnl_table_set_str(t, NFTNL_TABLE_NAME, name);
264 
265  family = nftnl_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST,
266  NFTNL_XML_MAND, err);
267  if (family >= 0)
268  nftnl_table_set_u32(t, NFTNL_TABLE_FAMILY, family);
269 
270  if (nftnl_mxml_num_parse(tree, "flags", MXML_DESCEND, BASE_DEC,
271  &flags, NFTNL_TYPE_U32, NFTNL_XML_MAND, err) == 0)
272  nftnl_table_set_u32(t, NFTNL_TABLE_FLAGS, flags);
273 
274  if (nftnl_mxml_num_parse(tree, "use", MXML_DESCEND, BASE_DEC,
275  &use, NFTNL_TYPE_U32, NFTNL_XML_MAND, err) == 0)
276  nftnl_table_set_u32(t, NFTNL_TABLE_USE, use);
277 
278  return 0;
279 }
280 #endif
281 
282 static int nftnl_table_xml_parse(struct nftnl_table *t, const void *data,
283  struct nftnl_parse_err *err,
284  enum nftnl_parse_input input)
285 {
286 #ifdef XML_PARSING
287  int ret;
288  mxml_node_t *tree = nftnl_mxml_build_tree(data, "table", err, input);
289  if (tree == NULL)
290  return -1;
291 
292  ret = nftnl_mxml_table_parse(tree, t, err);
293  mxmlDelete(tree);
294  return ret;
295 #else
296  errno = EOPNOTSUPP;
297  return -1;
298 #endif
299 }
300 
301 #ifdef JSON_PARSING
302 int nftnl_jansson_parse_table(struct nftnl_table *t, json_t *tree,
303  struct nftnl_parse_err *err)
304 {
305  json_t *root;
306  uint32_t flags, use;
307  const char *str;
308  int family;
309 
310  root = nftnl_jansson_get_node(tree, "table", err);
311  if (root == NULL)
312  return -1;
313 
314  str = nftnl_jansson_parse_str(root, "name", err);
315  if (str != NULL)
316  nftnl_table_set_str(t, NFTNL_TABLE_NAME, str);
317 
318  if (nftnl_jansson_parse_family(root, &family, err) == 0)
319  nftnl_table_set_u32(t, NFTNL_TABLE_FAMILY, family);
320 
321  if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags,
322  err) == 0)
323  nftnl_table_set_u32(t, NFTNL_TABLE_FLAGS, flags);
324 
325  if (nftnl_jansson_parse_val(root, "use", NFTNL_TYPE_U32, &use, err) == 0)
326  nftnl_table_set_u32(t, NFTNL_TABLE_USE, use);
327 
328  return 0;
329 }
330 #endif
331 
332 static int nftnl_table_json_parse(struct nftnl_table *t, const void *json,
333  struct nftnl_parse_err *err,
334  enum nftnl_parse_input input)
335 {
336 #ifdef JSON_PARSING
337  json_t *tree;
338  json_error_t error;
339  int ret;
340 
341  tree = nftnl_jansson_create_root(json, &error, err, input);
342  if (tree == NULL)
343  return -1;
344 
345  ret = nftnl_jansson_parse_table(t, tree, err);
346 
347  nftnl_jansson_free_root(tree);
348 
349  return ret;
350 #else
351  errno = EOPNOTSUPP;
352  return -1;
353 #endif
354 }
355 
356 static int nftnl_table_do_parse(struct nftnl_table *t, enum nftnl_parse_type type,
357  const void *data, struct nftnl_parse_err *err,
358  enum nftnl_parse_input input)
359 {
360  int ret;
361  struct nftnl_parse_err perr;
362 
363  switch (type) {
364  case NFTNL_PARSE_XML:
365  ret = nftnl_table_xml_parse(t, data, &perr, input);
366  break;
367  case NFTNL_PARSE_JSON:
368  ret = nftnl_table_json_parse(t, data, &perr, input);
369  break;
370  default:
371  ret = -1;
372  errno = EOPNOTSUPP;
373  break;
374  }
375 
376  if (err != NULL)
377  *err = perr;
378 
379  return ret;
380 }
381 
382 int nftnl_table_parse(struct nftnl_table *t, enum nftnl_parse_type type,
383  const char *data, struct nftnl_parse_err *err)
384 {
385  return nftnl_table_do_parse(t, type, data, err, NFTNL_PARSE_BUFFER);
386 }
387 EXPORT_SYMBOL_ALIAS(nftnl_table_parse, nft_table_parse);
388 
389 int nftnl_table_parse_file(struct nftnl_table *t, enum nftnl_parse_type type,
390  FILE *fp, struct nftnl_parse_err *err)
391 {
392  return nftnl_table_do_parse(t, type, fp, err, NFTNL_PARSE_FILE);
393 }
394 EXPORT_SYMBOL_ALIAS(nftnl_table_parse_file, nft_table_parse_file);
395 
396 static int nftnl_table_export(char *buf, size_t size,
397  const struct nftnl_table *t, int type)
398 {
399  NFTNL_BUF_INIT(b, buf, size);
400 
401  nftnl_buf_open(&b, type, TABLE);
402  if (t->flags & (1 << NFTNL_TABLE_NAME))
403  nftnl_buf_str(&b, type, t->name, NAME);
404  if (t->flags & (1 << NFTNL_TABLE_FAMILY))
405  nftnl_buf_str(&b, type, nftnl_family2str(t->family), FAMILY);
406  if (t->flags & (1 << NFTNL_TABLE_FLAGS))
407  nftnl_buf_u32(&b, type, t->table_flags, FLAGS);
408  if (t->flags & (1 << NFTNL_TABLE_USE))
409  nftnl_buf_u32(&b, type, t->use, USE);
410 
411  nftnl_buf_close(&b, type, TABLE);
412 
413  return nftnl_buf_done(&b);
414 }
415 
416 static int nftnl_table_snprintf_default(char *buf, size_t size,
417  const struct nftnl_table *t)
418 {
419  return snprintf(buf, size, "table %s %s flags %x use %d",
420  t->name, nftnl_family2str(t->family),
421  t->table_flags, t->use);
422 }
423 
424 static int nftnl_table_cmd_snprintf(char *buf, size_t size,
425  const struct nftnl_table *t, uint32_t cmd,
426  uint32_t type, uint32_t flags)
427 {
428  int ret, len = size, offset = 0;
429 
430  ret = nftnl_cmd_header_snprintf(buf + offset, len, cmd, type, flags);
431  SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
432 
433  switch (type) {
434  case NFTNL_OUTPUT_DEFAULT:
435  ret = nftnl_table_snprintf_default(buf+offset, len, t);
436  break;
437  case NFTNL_OUTPUT_XML:
438  case NFTNL_OUTPUT_JSON:
439  ret = nftnl_table_export(buf+offset, len, t, type);
440  break;
441  default:
442  return -1;
443  }
444  SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
445 
446  ret = nftnl_cmd_footer_snprintf(buf + offset, len, cmd, type, flags);
447  SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
448 
449  return offset;
450 }
451 
452 int nftnl_table_snprintf(char *buf, size_t size, const struct nftnl_table *t,
453  uint32_t type, uint32_t flags)
454 {
455  return nftnl_table_cmd_snprintf(buf, size, t, nftnl_flag2cmd(flags), type,
456  flags);
457 }
458 EXPORT_SYMBOL_ALIAS(nftnl_table_snprintf, nft_table_snprintf);
459 
460 static int nftnl_table_do_snprintf(char *buf, size_t size, const void *t,
461  uint32_t cmd, uint32_t type, uint32_t flags)
462 {
463  return nftnl_table_snprintf(buf, size, t, type, flags);
464 }
465 
466 int nftnl_table_fprintf(FILE *fp, const struct nftnl_table *t, uint32_t type,
467  uint32_t flags)
468 {
469  return nftnl_fprintf(fp, t, NFTNL_CMD_UNSPEC, type, flags,
470  nftnl_table_do_snprintf);
471 }
472 EXPORT_SYMBOL_ALIAS(nftnl_table_fprintf, nft_table_fprintf);
473 
475  struct list_head list;
476 };
477 
478 struct nftnl_table_list *nftnl_table_list_alloc(void)
479 {
480  struct nftnl_table_list *list;
481 
482  list = calloc(1, sizeof(struct nftnl_table_list));
483  if (list == NULL)
484  return NULL;
485 
486  INIT_LIST_HEAD(&list->list);
487 
488  return list;
489 }
490 EXPORT_SYMBOL_ALIAS(nftnl_table_list_alloc, nft_table_list_alloc);
491 
492 void nftnl_table_list_free(struct nftnl_table_list *list)
493 {
494  struct nftnl_table *r, *tmp;
495 
496  list_for_each_entry_safe(r, tmp, &list->list, head) {
497  list_del(&r->head);
498  nftnl_table_free(r);
499  }
500  xfree(list);
501 }
502 EXPORT_SYMBOL_ALIAS(nftnl_table_list_free, nft_table_list_free);
503 
504 int nftnl_table_list_is_empty(const struct nftnl_table_list *list)
505 {
506  return list_empty(&list->list);
507 }
508 EXPORT_SYMBOL_ALIAS(nftnl_table_list_is_empty, nft_table_list_is_empty);
509 
510 void nftnl_table_list_add(struct nftnl_table *r, struct nftnl_table_list *list)
511 {
512  list_add(&r->head, &list->list);
513 }
514 EXPORT_SYMBOL_ALIAS(nftnl_table_list_add, nft_table_list_add);
515 
516 void nftnl_table_list_add_tail(struct nftnl_table *r, struct nftnl_table_list *list)
517 {
518  list_add_tail(&r->head, &list->list);
519 }
520 EXPORT_SYMBOL_ALIAS(nftnl_table_list_add_tail, nft_table_list_add_tail);
521 
522 void nftnl_table_list_del(struct nftnl_table *t)
523 {
524  list_del(&t->head);
525 }
526 EXPORT_SYMBOL_ALIAS(nftnl_table_list_del, nft_table_list_del);
527 
528 int nftnl_table_list_foreach(struct nftnl_table_list *table_list,
529  int (*cb)(struct nftnl_table *t, void *data),
530  void *data)
531 {
532  struct nftnl_table *cur, *tmp;
533  int ret;
534 
535  list_for_each_entry_safe(cur, tmp, &table_list->list, head) {
536  ret = cb(cur, data);
537  if (ret < 0)
538  return ret;
539  }
540  return 0;
541 }
542 EXPORT_SYMBOL_ALIAS(nftnl_table_list_foreach, nft_table_list_foreach);
543 
545  struct nftnl_table_list *list;
546  struct nftnl_table *cur;
547 };
548 
549 struct nftnl_table_list_iter *nftnl_table_list_iter_create(struct nftnl_table_list *l)
550 {
551  struct nftnl_table_list_iter *iter;
552 
553  iter = calloc(1, sizeof(struct nftnl_table_list_iter));
554  if (iter == NULL)
555  return NULL;
556 
557  iter->list = l;
558  if (nftnl_table_list_is_empty(l))
559  iter->cur = NULL;
560  else
561  iter->cur = list_entry(l->list.next, struct nftnl_table, head);
562 
563  return iter;
564 }
565 EXPORT_SYMBOL_ALIAS(nftnl_table_list_iter_create, nft_table_list_iter_create);
566 
567 struct nftnl_table *nftnl_table_list_iter_next(struct nftnl_table_list_iter *iter)
568 {
569  struct nftnl_table *r = iter->cur;
570 
571  if (r == NULL)
572  return NULL;
573 
574  /* get next table, if any */
575  iter->cur = list_entry(iter->cur->head.next, struct nftnl_table, head);
576  if (&iter->cur->head == iter->list->list.next)
577  return NULL;
578 
579  return r;
580 }
581 EXPORT_SYMBOL_ALIAS(nftnl_table_list_iter_next, nft_table_list_iter_next);
582 
583 void nftnl_table_list_iter_destroy(const struct nftnl_table_list_iter *iter)
584 {
585  xfree(iter);
586 }
587 EXPORT_SYMBOL_ALIAS(nftnl_table_list_iter_destroy, nft_table_list_iter_destroy);