libnftnl  1.1.4
object.c
1 /*
2  * (C) 2012-2016 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 #include "internal.h"
10 
11 #include <time.h>
12 #include <endian.h>
13 #include <stdint.h>
14 #include <limits.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <netinet/in.h>
18 #include <errno.h>
19 
20 #include <libmnl/libmnl.h>
21 #include <linux/netfilter/nfnetlink.h>
22 #include <linux/netfilter/nf_tables.h>
23 
24 #include <libnftnl/object.h>
25 #include <buffer.h>
26 #include "obj.h"
27 
28 static struct obj_ops *obj_ops[__NFT_OBJECT_MAX] = {
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,
36  [NFT_OBJECT_CT_EXPECT] = &obj_ops_ct_expect,
37 };
38 
39 static struct obj_ops *nftnl_obj_ops_lookup(uint32_t type)
40 {
41  if (type > NFT_OBJECT_MAX)
42  return NULL;
43 
44  return obj_ops[type];
45 }
46 
47 EXPORT_SYMBOL(nftnl_obj_alloc);
48 struct nftnl_obj *nftnl_obj_alloc(void)
49 {
50  return calloc(1, sizeof(struct nftnl_obj));
51 }
52 
53 EXPORT_SYMBOL(nftnl_obj_free);
54 void nftnl_obj_free(const struct nftnl_obj *obj)
55 {
56  if (obj->flags & (1 << NFTNL_OBJ_TABLE))
57  xfree(obj->table);
58  if (obj->flags & (1 << NFTNL_OBJ_NAME))
59  xfree(obj->name);
60 
61  xfree(obj);
62 }
63 
64 EXPORT_SYMBOL(nftnl_obj_is_set);
65 bool nftnl_obj_is_set(const struct nftnl_obj *obj, uint16_t attr)
66 {
67  return obj->flags & (1 << attr);
68 }
69 
70 static uint32_t nftnl_obj_validate[NFTNL_OBJ_MAX + 1] = {
71  [NFTNL_OBJ_FAMILY] = sizeof(uint32_t),
72  [NFTNL_OBJ_USE] = sizeof(uint32_t),
73  [NFTNL_OBJ_HANDLE] = sizeof(uint64_t),
74 };
75 
76 EXPORT_SYMBOL(nftnl_obj_set_data);
77 void nftnl_obj_set_data(struct nftnl_obj *obj, uint16_t attr,
78  const void *data, uint32_t data_len)
79 {
80  if (attr < NFTNL_OBJ_MAX)
81  nftnl_assert_validate(data, nftnl_obj_validate, attr, data_len);
82 
83  switch (attr) {
84  case NFTNL_OBJ_TABLE:
85  xfree(obj->table);
86  obj->table = strdup(data);
87  break;
88  case NFTNL_OBJ_NAME:
89  xfree(obj->name);
90  obj->name = strdup(data);
91  break;
92  case NFTNL_OBJ_TYPE:
93  obj->ops = nftnl_obj_ops_lookup(*((uint32_t *)data));
94  if (!obj->ops)
95  return;
96  break;
97  case NFTNL_OBJ_FAMILY:
98  memcpy(&obj->family, data, sizeof(obj->family));
99  break;
100  case NFTNL_OBJ_USE:
101  memcpy(&obj->use, data, sizeof(obj->use));
102  break;
103  case NFTNL_OBJ_HANDLE:
104  memcpy(&obj->handle, data, sizeof(obj->handle));
105  break;
106  default:
107  if (obj->ops)
108  obj->ops->set(obj, attr, data, data_len);
109  break;
110  }
111  obj->flags |= (1 << attr);
112 }
113 
114 EXPORT_SYMBOL(nftnl_obj_set);
115 void nftnl_obj_set(struct nftnl_obj *obj, uint16_t attr, const void *data)
116 {
117  nftnl_obj_set_data(obj, attr, data, nftnl_obj_validate[attr]);
118 }
119 
120 EXPORT_SYMBOL(nftnl_obj_set_u8);
121 void nftnl_obj_set_u8(struct nftnl_obj *obj, uint16_t attr, uint8_t val)
122 {
123  nftnl_obj_set_data(obj, attr, &val, sizeof(uint8_t));
124 }
125 
126 EXPORT_SYMBOL(nftnl_obj_set_u16);
127 void nftnl_obj_set_u16(struct nftnl_obj *obj, uint16_t attr, uint16_t val)
128 {
129  nftnl_obj_set_data(obj, attr, &val, sizeof(uint16_t));
130 }
131 
132 EXPORT_SYMBOL(nftnl_obj_set_u32);
133 void nftnl_obj_set_u32(struct nftnl_obj *obj, uint16_t attr, uint32_t val)
134 {
135  nftnl_obj_set_data(obj, attr, &val, sizeof(uint32_t));
136 }
137 
138 EXPORT_SYMBOL(nftnl_obj_set_u64);
139 void nftnl_obj_set_u64(struct nftnl_obj *obj, uint16_t attr, uint64_t val)
140 {
141  nftnl_obj_set_data(obj, attr, &val, sizeof(uint64_t));
142 }
143 
144 EXPORT_SYMBOL(nftnl_obj_set_str);
145 void nftnl_obj_set_str(struct nftnl_obj *obj, uint16_t attr, const char *str)
146 {
147  nftnl_obj_set_data(obj, attr, str, 0);
148 }
149 
150 EXPORT_SYMBOL(nftnl_obj_get_data);
151 const void *nftnl_obj_get_data(struct nftnl_obj *obj, uint16_t attr,
152  uint32_t *data_len)
153 {
154  if (!(obj->flags & (1 << attr)))
155  return NULL;
156 
157  switch(attr) {
158  case NFTNL_OBJ_TABLE:
159  return obj->table;
160  case NFTNL_OBJ_NAME:
161  return obj->name;
162  case NFTNL_OBJ_TYPE:
163  if (!obj->ops)
164  return NULL;
165 
166  *data_len = sizeof(uint32_t);
167  return &obj->ops->type;
168  case NFTNL_OBJ_FAMILY:
169  *data_len = sizeof(uint32_t);
170  return &obj->family;
171  case NFTNL_OBJ_USE:
172  *data_len = sizeof(uint32_t);
173  return &obj->use;
174  case NFTNL_OBJ_HANDLE:
175  *data_len = sizeof(uint64_t);
176  return &obj->handle;
177  default:
178  if (obj->ops)
179  return obj->ops->get(obj, attr, data_len);
180  break;
181  }
182  return NULL;
183 }
184 
185 EXPORT_SYMBOL(nftnl_obj_get);
186 const void *nftnl_obj_get(struct nftnl_obj *obj, uint16_t attr)
187 {
188  uint32_t data_len;
189  return nftnl_obj_get_data(obj, attr, &data_len);
190 }
191 
192 EXPORT_SYMBOL(nftnl_obj_get_u8);
193 uint8_t nftnl_obj_get_u8(struct nftnl_obj *obj, uint16_t attr)
194 {
195  const void *ret = nftnl_obj_get(obj, attr);
196  return ret == NULL ? 0 : *((uint8_t *)ret);
197 }
198 
199 EXPORT_SYMBOL(nftnl_obj_get_u16);
200 uint16_t nftnl_obj_get_u16(struct nftnl_obj *obj, uint16_t attr)
201 {
202  const void *ret = nftnl_obj_get(obj, attr);
203  return ret == NULL ? 0 : *((uint16_t *)ret);
204 }
205 
206 EXPORT_SYMBOL(nftnl_obj_get_u32);
207 uint32_t nftnl_obj_get_u32(struct nftnl_obj *obj, uint16_t attr)
208 {
209  const void *ret = nftnl_obj_get(obj, attr);
210  return ret == NULL ? 0 : *((uint32_t *)ret);
211 }
212 
213 EXPORT_SYMBOL(nftnl_obj_get_u64);
214 uint64_t nftnl_obj_get_u64(struct nftnl_obj *obj, uint16_t attr)
215 {
216  const void *ret = nftnl_obj_get(obj, attr);
217  return ret == NULL ? 0 : *((uint64_t *)ret);
218 }
219 
220 EXPORT_SYMBOL(nftnl_obj_get_str);
221 const char *nftnl_obj_get_str(struct nftnl_obj *obj, uint16_t attr)
222 {
223  return nftnl_obj_get(obj, attr);
224 }
225 
226 EXPORT_SYMBOL(nftnl_obj_nlmsg_build_payload);
227 void nftnl_obj_nlmsg_build_payload(struct nlmsghdr *nlh,
228  const struct nftnl_obj *obj)
229 {
230  if (obj->flags & (1 << NFTNL_OBJ_TABLE))
231  mnl_attr_put_strz(nlh, NFTA_OBJ_TABLE, obj->table);
232  if (obj->flags & (1 << NFTNL_OBJ_NAME))
233  mnl_attr_put_strz(nlh, NFTA_OBJ_NAME, obj->name);
234  if (obj->flags & (1 << NFTNL_OBJ_TYPE))
235  mnl_attr_put_u32(nlh, NFTA_OBJ_TYPE, htonl(obj->ops->type));
236  if (obj->flags & (1 << NFTNL_OBJ_HANDLE))
237  mnl_attr_put_u64(nlh, NFTA_OBJ_HANDLE, htobe64(obj->handle));
238  if (obj->ops) {
239  struct nlattr *nest = mnl_attr_nest_start(nlh, NFTA_OBJ_DATA);
240 
241  obj->ops->build(nlh, obj);
242  mnl_attr_nest_end(nlh, nest);
243  }
244 }
245 
246 static int nftnl_obj_parse_attr_cb(const struct nlattr *attr, void *data)
247 {
248  const struct nlattr **tb = data;
249  int type = mnl_attr_get_type(attr);
250 
251  if (mnl_attr_type_valid(attr, NFTA_OBJ_MAX) < 0)
252  return MNL_CB_OK;
253 
254  switch(type) {
255  case NFTA_OBJ_TABLE:
256  case NFTA_OBJ_NAME:
257  if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
258  abi_breakage();
259  break;
260  case NFTA_OBJ_HANDLE:
261  if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
262  abi_breakage();
263  break;
264  case NFTA_OBJ_DATA:
265  if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
266  abi_breakage();
267  break;
268  case NFTA_OBJ_USE:
269  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
270  abi_breakage();
271  break;
272  }
273 
274  tb[type] = attr;
275  return MNL_CB_OK;
276 }
277 
278 EXPORT_SYMBOL(nftnl_obj_nlmsg_parse);
279 int nftnl_obj_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_obj *obj)
280 {
281  struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
282  struct nlattr *tb[NFTA_OBJ_MAX + 1] = {};
283  int err;
284 
285  if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_obj_parse_attr_cb, tb) < 0)
286  return -1;
287 
288  if (tb[NFTA_OBJ_TABLE]) {
289  obj->table = strdup(mnl_attr_get_str(tb[NFTA_OBJ_TABLE]));
290  obj->flags |= (1 << NFTNL_OBJ_TABLE);
291  }
292  if (tb[NFTA_OBJ_NAME]) {
293  obj->name = strdup(mnl_attr_get_str(tb[NFTA_OBJ_NAME]));
294  obj->flags |= (1 << NFTNL_OBJ_NAME);
295  }
296  if (tb[NFTA_OBJ_TYPE]) {
297  uint32_t type = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_TYPE]));
298 
299  obj->ops = nftnl_obj_ops_lookup(type);
300  if (obj->ops)
301  obj->flags |= (1 << NFTNL_OBJ_TYPE);
302  }
303  if (tb[NFTA_OBJ_DATA]) {
304  if (obj->ops) {
305  err = obj->ops->parse(obj, tb[NFTA_OBJ_DATA]);
306  if (err < 0)
307  return err;
308  }
309  }
310  if (tb[NFTA_OBJ_USE]) {
311  obj->use = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_USE]));
312  obj->flags |= (1 << NFTNL_OBJ_USE);
313  }
314  if (tb[NFTA_OBJ_HANDLE]) {
315  obj->handle = be64toh(mnl_attr_get_u64(tb[NFTA_OBJ_HANDLE]));
316  obj->flags |= (1 << NFTNL_OBJ_HANDLE);
317  }
318 
319  obj->family = nfg->nfgen_family;
320  obj->flags |= (1 << NFTNL_OBJ_FAMILY);
321 
322  return 0;
323 }
324 
325 static int nftnl_obj_do_parse(struct nftnl_obj *obj, enum nftnl_parse_type type,
326  const void *data, struct nftnl_parse_err *err,
327  enum nftnl_parse_input input)
328 {
329  struct nftnl_parse_err perr = {};
330  int ret;
331 
332  switch (type) {
333  case NFTNL_PARSE_JSON:
334  case NFTNL_PARSE_XML:
335  default:
336  ret = -1;
337  errno = EOPNOTSUPP;
338  break;
339  }
340 
341  if (err != NULL)
342  *err = perr;
343 
344  return ret;
345 }
346 
347 EXPORT_SYMBOL(nftnl_obj_parse);
348 int nftnl_obj_parse(struct nftnl_obj *obj, enum nftnl_parse_type type,
349  const char *data, struct nftnl_parse_err *err)
350 {
351  return nftnl_obj_do_parse(obj, type, data, err, NFTNL_PARSE_BUFFER);
352 }
353 
354 EXPORT_SYMBOL(nftnl_obj_parse_file);
355 int nftnl_obj_parse_file(struct nftnl_obj *obj, enum nftnl_parse_type type,
356  FILE *fp, struct nftnl_parse_err *err)
357 {
358  return nftnl_obj_do_parse(obj, type, fp, err, NFTNL_PARSE_FILE);
359 }
360 
361 static int nftnl_obj_snprintf_dflt(char *buf, size_t size,
362  const struct nftnl_obj *obj,
363  uint32_t type, uint32_t flags)
364 {
365  const char *name = obj->ops ? obj->ops->name : "(unknown)";
366  int ret, remain = size, offset = 0;
367 
368  ret = snprintf(buf, size, "table %s name %s use %u [ %s ",
369  obj->table, obj->name, obj->use, name);
370  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
371 
372  if (obj->ops) {
373  ret = obj->ops->snprintf(buf + offset, offset, type, flags,
374  obj);
375  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
376  }
377  ret = snprintf(buf + offset, offset, "]");
378  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
379 
380  return offset;
381 }
382 
383 static int nftnl_obj_cmd_snprintf(char *buf, size_t size,
384  const struct nftnl_obj *obj, uint32_t cmd,
385  uint32_t type, uint32_t flags)
386 {
387  int ret, remain = size, offset = 0;
388 
389  switch (type) {
390  case NFTNL_OUTPUT_DEFAULT:
391  ret = nftnl_obj_snprintf_dflt(buf + offset, remain, obj, type,
392  flags);
393  break;
394  case NFTNL_OUTPUT_JSON:
395  case NFTNL_OUTPUT_XML:
396  default:
397  return -1;
398  }
399  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
400 
401  return offset;
402 }
403 
404 EXPORT_SYMBOL(nftnl_obj_snprintf);
405 int nftnl_obj_snprintf(char *buf, size_t size, const struct nftnl_obj *obj,
406  uint32_t type, uint32_t flags)
407 {
408  if (size)
409  buf[0] = '\0';
410 
411  return nftnl_obj_cmd_snprintf(buf, size, obj, nftnl_flag2cmd(flags),
412  type, flags);
413 }
414 
415 static int nftnl_obj_do_snprintf(char *buf, size_t size, const void *obj,
416  uint32_t cmd, uint32_t type, uint32_t flags)
417 {
418  return nftnl_obj_snprintf(buf, size, obj, type, flags);
419 }
420 
421 EXPORT_SYMBOL(nftnl_obj_fprintf);
422 int nftnl_obj_fprintf(FILE *fp, const struct nftnl_obj *obj, uint32_t type,
423  uint32_t flags)
424 {
425  return nftnl_fprintf(fp, obj, NFTNL_CMD_UNSPEC, type, flags,
426  nftnl_obj_do_snprintf);
427 }
428 
430  struct list_head list;
431 };
432 
433 EXPORT_SYMBOL(nftnl_obj_list_alloc);
434 struct nftnl_obj_list *nftnl_obj_list_alloc(void)
435 {
436  struct nftnl_obj_list *list;
437 
438  list = calloc(1, sizeof(struct nftnl_obj_list));
439  if (list == NULL)
440  return NULL;
441 
442  INIT_LIST_HEAD(&list->list);
443 
444  return list;
445 }
446 
447 EXPORT_SYMBOL(nftnl_obj_list_free);
448 void nftnl_obj_list_free(struct nftnl_obj_list *list)
449 {
450  struct nftnl_obj *r, *tmp;
451 
452  list_for_each_entry_safe(r, tmp, &list->list, head) {
453  list_del(&r->head);
454  nftnl_obj_free(r);
455  }
456  xfree(list);
457 }
458 
459 EXPORT_SYMBOL(nftnl_obj_list_is_empty);
460 int nftnl_obj_list_is_empty(struct nftnl_obj_list *list)
461 {
462  return list_empty(&list->list);
463 }
464 
465 EXPORT_SYMBOL(nftnl_obj_list_add);
466 void nftnl_obj_list_add(struct nftnl_obj *r, struct nftnl_obj_list *list)
467 {
468  list_add(&r->head, &list->list);
469 }
470 
471 EXPORT_SYMBOL(nftnl_obj_list_add_tail);
472 void nftnl_obj_list_add_tail(struct nftnl_obj *r,
473  struct nftnl_obj_list *list)
474 {
475  list_add_tail(&r->head, &list->list);
476 }
477 
478 EXPORT_SYMBOL(nftnl_obj_list_del);
479 void nftnl_obj_list_del(struct nftnl_obj *t)
480 {
481  list_del(&t->head);
482 }
483 
484 EXPORT_SYMBOL(nftnl_obj_list_foreach);
485 int nftnl_obj_list_foreach(struct nftnl_obj_list *table_list,
486  int (*cb)(struct nftnl_obj *t, void *data),
487  void *data)
488 {
489  struct nftnl_obj *cur, *tmp;
490  int ret;
491 
492  list_for_each_entry_safe(cur, tmp, &table_list->list, head) {
493  ret = cb(cur, data);
494  if (ret < 0)
495  return ret;
496  }
497  return 0;
498 }
499 
501  struct nftnl_obj_list *list;
502  struct nftnl_obj *cur;
503 };
504 
505 EXPORT_SYMBOL(nftnl_obj_list_iter_create);
506 struct nftnl_obj_list_iter *
507 nftnl_obj_list_iter_create(struct nftnl_obj_list *l)
508 {
509  struct nftnl_obj_list_iter *iter;
510 
511  iter = calloc(1, sizeof(struct nftnl_obj_list_iter));
512  if (iter == NULL)
513  return NULL;
514 
515  iter->list = l;
516  if (nftnl_obj_list_is_empty(l))
517  iter->cur = NULL;
518  else
519  iter->cur = list_entry(l->list.next, struct nftnl_obj, head);
520 
521  return iter;
522 }
523 
524 EXPORT_SYMBOL(nftnl_obj_list_iter_next);
525 struct nftnl_obj *nftnl_obj_list_iter_next(struct nftnl_obj_list_iter *iter)
526 {
527  struct nftnl_obj *r = iter->cur;
528 
529  if (r == NULL)
530  return NULL;
531 
532  /* get next table, if any */
533  iter->cur = list_entry(iter->cur->head.next, struct nftnl_obj, head);
534  if (&iter->cur->head == iter->list->list.next)
535  return NULL;
536 
537  return r;
538 }
539 
540 EXPORT_SYMBOL(nftnl_obj_list_iter_destroy);
541 void nftnl_obj_list_iter_destroy(struct nftnl_obj_list_iter *iter)
542 {
543  xfree(iter);
544 }