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