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