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