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