libnftnl  1.3.1
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  if (obj->flags & (1 << NFTNL_OBJ_TUNNEL_OPTS)) {
59  nftnl_tunnel_opts_free(obj->data.tunnel.tun_opts);
60  xfree(obj->data.tunnel.tun_opts);
61  }
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 (nftnl_parse_str_attr(tb[NFTA_OBJ_TABLE], NFTNL_OBJ_TABLE,
348  &obj->table, &obj->flags) < 0)
349  return -1;
350  if (nftnl_parse_str_attr(tb[NFTA_OBJ_NAME], NFTNL_OBJ_NAME,
351  &obj->name, &obj->flags) < 0)
352  return -1;
353  if (tb[NFTA_OBJ_TYPE]) {
354  uint32_t type = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_TYPE]));
355 
356  obj->ops = nftnl_obj_ops_lookup(type);
357  if (obj->ops)
358  obj->flags |= (1 << NFTNL_OBJ_TYPE);
359  }
360  if (tb[NFTA_OBJ_DATA]) {
361  if (obj->ops) {
362  err = obj->ops->parse(obj, tb[NFTA_OBJ_DATA]);
363  if (err < 0)
364  return err;
365  }
366  }
367  if (tb[NFTA_OBJ_USE]) {
368  obj->use = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_USE]));
369  obj->flags |= (1 << NFTNL_OBJ_USE);
370  }
371  if (tb[NFTA_OBJ_HANDLE]) {
372  obj->handle = be64toh(mnl_attr_get_u64(tb[NFTA_OBJ_HANDLE]));
373  obj->flags |= (1 << NFTNL_OBJ_HANDLE);
374  }
375  if (tb[NFTA_OBJ_USERDATA]) {
376  nftnl_obj_set_data(obj, NFTNL_OBJ_USERDATA,
377  mnl_attr_get_payload(tb[NFTA_OBJ_USERDATA]),
378  mnl_attr_get_payload_len(tb[NFTA_OBJ_USERDATA]));
379  }
380 
381  obj->family = nfg->nfgen_family;
382  obj->flags |= (1 << NFTNL_OBJ_FAMILY);
383 
384  return 0;
385 }
386 
387 EXPORT_SYMBOL(nftnl_obj_parse);
388 int nftnl_obj_parse(struct nftnl_obj *obj, enum nftnl_parse_type type,
389  const char *data, struct nftnl_parse_err *err)
390 {
391  errno = EOPNOTSUPP;
392 
393  return -1;
394 }
395 
396 EXPORT_SYMBOL(nftnl_obj_parse_file);
397 int nftnl_obj_parse_file(struct nftnl_obj *obj, enum nftnl_parse_type type,
398  FILE *fp, struct nftnl_parse_err *err)
399 {
400  errno = EOPNOTSUPP;
401 
402  return -1;
403 }
404 
405 static int nftnl_obj_snprintf_dflt(char *buf, size_t remain,
406  const struct nftnl_obj *obj,
407  uint32_t type, uint32_t flags)
408 {
409  const char *name = obj->ops ? obj->ops->name : "(unknown)";
410  int ret, offset = 0;
411 
412  ret = snprintf(buf, remain, "table %s name %s use %u [ %s ",
413  obj->table, obj->name, obj->use, name);
414  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
415 
416  if (obj->ops) {
417  ret = obj->ops->output(buf + offset, remain, flags, obj);
418  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
419  }
420  ret = snprintf(buf + offset, remain, "]");
421  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
422 
423  return offset;
424 }
425 
426 static int nftnl_obj_cmd_snprintf(char *buf, size_t remain,
427  const struct nftnl_obj *obj, uint32_t cmd,
428  uint32_t type, uint32_t flags)
429 {
430  int ret, offset = 0;
431 
432  if (type != NFTNL_OUTPUT_DEFAULT)
433  return -1;
434 
435  ret = nftnl_obj_snprintf_dflt(buf + offset, remain, obj, type, flags);
436  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
437  return offset;
438 }
439 
440 EXPORT_SYMBOL(nftnl_obj_snprintf);
441 int nftnl_obj_snprintf(char *buf, size_t size, const struct nftnl_obj *obj,
442  uint32_t type, uint32_t flags)
443 {
444  if (size)
445  buf[0] = '\0';
446 
447  return nftnl_obj_cmd_snprintf(buf, size, obj, nftnl_flag2cmd(flags),
448  type, flags);
449 }
450 
451 static int nftnl_obj_do_snprintf(char *buf, size_t size, const void *obj,
452  uint32_t cmd, uint32_t type, uint32_t flags)
453 {
454  return nftnl_obj_snprintf(buf, size, obj, type, flags);
455 }
456 
457 EXPORT_SYMBOL(nftnl_obj_fprintf);
458 int nftnl_obj_fprintf(FILE *fp, const struct nftnl_obj *obj, uint32_t type,
459  uint32_t flags)
460 {
461  return nftnl_fprintf(fp, obj, NFTNL_CMD_UNSPEC, type, flags,
462  nftnl_obj_do_snprintf);
463 }
464 
466  struct list_head list;
467 };
468 
469 EXPORT_SYMBOL(nftnl_obj_list_alloc);
470 struct nftnl_obj_list *nftnl_obj_list_alloc(void)
471 {
472  struct nftnl_obj_list *list;
473 
474  list = calloc(1, sizeof(struct nftnl_obj_list));
475  if (list == NULL)
476  return NULL;
477 
478  INIT_LIST_HEAD(&list->list);
479 
480  return list;
481 }
482 
483 EXPORT_SYMBOL(nftnl_obj_list_free);
484 void nftnl_obj_list_free(struct nftnl_obj_list *list)
485 {
486  struct nftnl_obj *r, *tmp;
487 
488  list_for_each_entry_safe(r, tmp, &list->list, head) {
489  list_del(&r->head);
490  nftnl_obj_free(r);
491  }
492  xfree(list);
493 }
494 
495 EXPORT_SYMBOL(nftnl_obj_list_is_empty);
496 int nftnl_obj_list_is_empty(struct nftnl_obj_list *list)
497 {
498  return list_empty(&list->list);
499 }
500 
501 EXPORT_SYMBOL(nftnl_obj_list_add);
502 void nftnl_obj_list_add(struct nftnl_obj *r, struct nftnl_obj_list *list)
503 {
504  list_add(&r->head, &list->list);
505 }
506 
507 EXPORT_SYMBOL(nftnl_obj_list_add_tail);
508 void nftnl_obj_list_add_tail(struct nftnl_obj *r,
509  struct nftnl_obj_list *list)
510 {
511  list_add_tail(&r->head, &list->list);
512 }
513 
514 EXPORT_SYMBOL(nftnl_obj_list_del);
515 void nftnl_obj_list_del(struct nftnl_obj *t)
516 {
517  list_del(&t->head);
518 }
519 
520 EXPORT_SYMBOL(nftnl_obj_list_foreach);
521 int nftnl_obj_list_foreach(struct nftnl_obj_list *table_list,
522  int (*cb)(struct nftnl_obj *t, void *data),
523  void *data)
524 {
525  struct nftnl_obj *cur, *tmp;
526  int ret;
527 
528  list_for_each_entry_safe(cur, tmp, &table_list->list, head) {
529  ret = cb(cur, data);
530  if (ret < 0)
531  return ret;
532  }
533  return 0;
534 }
535 
537  struct nftnl_obj_list *list;
538  struct nftnl_obj *cur;
539 };
540 
541 EXPORT_SYMBOL(nftnl_obj_list_iter_create);
542 struct nftnl_obj_list_iter *
543 nftnl_obj_list_iter_create(struct nftnl_obj_list *l)
544 {
545  struct nftnl_obj_list_iter *iter;
546 
547  iter = calloc(1, sizeof(struct nftnl_obj_list_iter));
548  if (iter == NULL)
549  return NULL;
550 
551  iter->list = l;
552  if (nftnl_obj_list_is_empty(l))
553  iter->cur = NULL;
554  else
555  iter->cur = list_entry(l->list.next, struct nftnl_obj, head);
556 
557  return iter;
558 }
559 
560 EXPORT_SYMBOL(nftnl_obj_list_iter_next);
561 struct nftnl_obj *nftnl_obj_list_iter_next(struct nftnl_obj_list_iter *iter)
562 {
563  struct nftnl_obj *r = iter->cur;
564 
565  if (r == NULL)
566  return NULL;
567 
568  /* get next table, if any */
569  iter->cur = list_entry(iter->cur->head.next, struct nftnl_obj, head);
570  if (&iter->cur->head == iter->list->list.next)
571  return NULL;
572 
573  return r;
574 }
575 
576 EXPORT_SYMBOL(nftnl_obj_list_iter_destroy);
577 void nftnl_obj_list_iter_destroy(struct nftnl_obj_list_iter *iter)
578 {
579  xfree(iter);
580 }