libnftnl  1.2.6
flowtable.c
1 #include "internal.h"
2 
3 #include <time.h>
4 #include <endian.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <limits.h>
8 #include <string.h>
9 #include <netinet/in.h>
10 #include <errno.h>
11 #include <inttypes.h>
12 
13 #include <libmnl/libmnl.h>
14 #include <linux/netfilter/nfnetlink.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_arp.h>
18 
19 #include <libnftnl/flowtable.h>
20 
22  struct list_head head;
23  const char *name;
24  const char *table;
25  int family;
26  uint32_t hooknum;
27  int32_t prio;
28  uint32_t size;
29  const char **dev_array;
30  uint32_t dev_array_len;
31  uint32_t ft_flags;
32  uint32_t use;
33  uint32_t flags;
34  uint64_t handle;
35 };
36 
37 EXPORT_SYMBOL(nftnl_flowtable_alloc);
38 struct nftnl_flowtable *nftnl_flowtable_alloc(void)
39 {
40  return calloc(1, sizeof(struct nftnl_flowtable));
41 }
42 
43 EXPORT_SYMBOL(nftnl_flowtable_free);
44 void nftnl_flowtable_free(const struct nftnl_flowtable *c)
45 {
46  int i;
47 
48  if (c->flags & (1 << NFTNL_FLOWTABLE_NAME))
49  xfree(c->name);
50  if (c->flags & (1 << NFTNL_FLOWTABLE_TABLE))
51  xfree(c->table);
52  if (c->flags & (1 << NFTNL_FLOWTABLE_DEVICES)) {
53  for (i = 0; i < c->dev_array_len; i++)
54  xfree(c->dev_array[i]);
55 
56  xfree(c->dev_array);
57  }
58  xfree(c);
59 }
60 
61 EXPORT_SYMBOL(nftnl_flowtable_is_set);
62 bool nftnl_flowtable_is_set(const struct nftnl_flowtable *c, uint16_t attr)
63 {
64  return c->flags & (1 << attr);
65 }
66 
67 EXPORT_SYMBOL(nftnl_flowtable_unset);
68 void nftnl_flowtable_unset(struct nftnl_flowtable *c, uint16_t attr)
69 {
70  int i;
71 
72  if (!(c->flags & (1 << attr)))
73  return;
74 
75  switch (attr) {
76  case NFTNL_FLOWTABLE_NAME:
77  xfree(c->name);
78  break;
79  case NFTNL_FLOWTABLE_TABLE:
80  xfree(c->table);
81  break;
82  case NFTNL_FLOWTABLE_HOOKNUM:
83  case NFTNL_FLOWTABLE_PRIO:
84  case NFTNL_FLOWTABLE_USE:
85  case NFTNL_FLOWTABLE_FAMILY:
86  case NFTNL_FLOWTABLE_FLAGS:
87  case NFTNL_FLOWTABLE_HANDLE:
88  break;
89  case NFTNL_FLOWTABLE_DEVICES:
90  for (i = 0; i < c->dev_array_len; i++)
91  xfree(c->dev_array[i]);
92  xfree(c->dev_array);
93  break;
94  default:
95  return;
96  }
97 
98  c->flags &= ~(1 << attr);
99 }
100 
101 static uint32_t nftnl_flowtable_validate[NFTNL_FLOWTABLE_MAX + 1] = {
102  [NFTNL_FLOWTABLE_HOOKNUM] = sizeof(uint32_t),
103  [NFTNL_FLOWTABLE_PRIO] = sizeof(int32_t),
104  [NFTNL_FLOWTABLE_FAMILY] = sizeof(uint32_t),
105  [NFTNL_FLOWTABLE_FLAGS] = sizeof(uint32_t),
106  [NFTNL_FLOWTABLE_HANDLE] = sizeof(uint64_t),
107 };
108 
109 EXPORT_SYMBOL(nftnl_flowtable_set_data);
110 int nftnl_flowtable_set_data(struct nftnl_flowtable *c, uint16_t attr,
111  const void *data, uint32_t data_len)
112 {
113  const char **dev_array;
114  int len = 0, i;
115 
116  nftnl_assert_attr_exists(attr, NFTNL_FLOWTABLE_MAX);
117  nftnl_assert_validate(data, nftnl_flowtable_validate, attr, data_len);
118 
119  switch(attr) {
120  case NFTNL_FLOWTABLE_NAME:
121  if (c->flags & (1 << NFTNL_FLOWTABLE_NAME))
122  xfree(c->name);
123 
124  c->name = strdup(data);
125  if (!c->name)
126  return -1;
127  break;
128  case NFTNL_FLOWTABLE_TABLE:
129  if (c->flags & (1 << NFTNL_FLOWTABLE_TABLE))
130  xfree(c->table);
131 
132  c->table = strdup(data);
133  if (!c->table)
134  return -1;
135  break;
136  case NFTNL_FLOWTABLE_HOOKNUM:
137  memcpy(&c->hooknum, data, sizeof(c->hooknum));
138  break;
139  case NFTNL_FLOWTABLE_PRIO:
140  memcpy(&c->prio, data, sizeof(c->prio));
141  break;
142  case NFTNL_FLOWTABLE_FAMILY:
143  memcpy(&c->family, data, sizeof(c->family));
144  break;
145  case NFTNL_FLOWTABLE_DEVICES:
146  dev_array = (const char **)data;
147  while (dev_array[len] != NULL)
148  len++;
149 
150  if (c->flags & (1 << NFTNL_FLOWTABLE_DEVICES)) {
151  for (i = 0; i < c->dev_array_len; i++)
152  xfree(c->dev_array[i]);
153  xfree(c->dev_array);
154  }
155 
156  c->dev_array = calloc(len + 1, sizeof(char *));
157  if (!c->dev_array)
158  return -1;
159 
160  for (i = 0; i < len; i++)
161  c->dev_array[i] = strdup(dev_array[i]);
162 
163  c->dev_array_len = len;
164  break;
165  case NFTNL_FLOWTABLE_SIZE:
166  memcpy(&c->size, data, sizeof(c->size));
167  break;
168  case NFTNL_FLOWTABLE_FLAGS:
169  memcpy(&c->ft_flags, data, sizeof(c->ft_flags));
170  break;
171  case NFTNL_FLOWTABLE_HANDLE:
172  memcpy(&c->handle, data, sizeof(c->handle));
173  break;
174  }
175  c->flags |= (1 << attr);
176  return 0;
177 }
178 
179 void nftnl_flowtable_set(struct nftnl_flowtable *c, uint16_t attr, const void *data) __visible;
180 void nftnl_flowtable_set(struct nftnl_flowtable *c, uint16_t attr, const void *data)
181 {
182  nftnl_flowtable_set_data(c, attr, data, nftnl_flowtable_validate[attr]);
183 }
184 
185 EXPORT_SYMBOL(nftnl_flowtable_set_u32);
186 void nftnl_flowtable_set_u32(struct nftnl_flowtable *c, uint16_t attr, uint32_t data)
187 {
188  nftnl_flowtable_set_data(c, attr, &data, sizeof(uint32_t));
189 }
190 
191 EXPORT_SYMBOL(nftnl_flowtable_set_s32);
192 void nftnl_flowtable_set_s32(struct nftnl_flowtable *c, uint16_t attr, int32_t data)
193 {
194  nftnl_flowtable_set_data(c, attr, &data, sizeof(int32_t));
195 }
196 
197 EXPORT_SYMBOL(nftnl_flowtable_set_str);
198 int nftnl_flowtable_set_str(struct nftnl_flowtable *c, uint16_t attr, const char *str)
199 {
200  return nftnl_flowtable_set_data(c, attr, str, strlen(str) + 1);
201 }
202 
203 EXPORT_SYMBOL(nftnl_flowtable_set_u64);
204 void nftnl_flowtable_set_u64(struct nftnl_flowtable *c, uint16_t attr, uint64_t data)
205 {
206  nftnl_flowtable_set_data(c, attr, &data, sizeof(uint64_t));
207 }
208 
209 EXPORT_SYMBOL(nftnl_flowtable_set_array);
210 int nftnl_flowtable_set_array(struct nftnl_flowtable *c, uint16_t attr,
211  const char **data)
212 {
213  return nftnl_flowtable_set_data(c, attr, data, 0);
214 }
215 
216 EXPORT_SYMBOL(nftnl_flowtable_get_data);
217 const void *nftnl_flowtable_get_data(const struct nftnl_flowtable *c,
218  uint16_t attr, uint32_t *data_len)
219 {
220  if (!(c->flags & (1 << attr)))
221  return NULL;
222 
223  switch(attr) {
224  case NFTNL_FLOWTABLE_NAME:
225  *data_len = strlen(c->name) + 1;
226  return c->name;
227  case NFTNL_FLOWTABLE_TABLE:
228  *data_len = strlen(c->table) + 1;
229  return c->table;
230  case NFTNL_FLOWTABLE_HOOKNUM:
231  *data_len = sizeof(uint32_t);
232  return &c->hooknum;
233  case NFTNL_FLOWTABLE_PRIO:
234  *data_len = sizeof(int32_t);
235  return &c->prio;
236  case NFTNL_FLOWTABLE_FAMILY:
237  *data_len = sizeof(int32_t);
238  return &c->family;
239  case NFTNL_FLOWTABLE_DEVICES:
240  *data_len = 0;
241  return &c->dev_array[0];
242  case NFTNL_FLOWTABLE_SIZE:
243  *data_len = sizeof(int32_t);
244  return &c->size;
245  case NFTNL_FLOWTABLE_FLAGS:
246  *data_len = sizeof(int32_t);
247  return &c->ft_flags;
248  case NFTNL_FLOWTABLE_HANDLE:
249  *data_len = sizeof(uint64_t);
250  return &c->handle;
251  }
252  return NULL;
253 }
254 
255 EXPORT_SYMBOL(nftnl_flowtable_get);
256 const void *nftnl_flowtable_get(const struct nftnl_flowtable *c, uint16_t attr)
257 {
258  uint32_t data_len;
259  return nftnl_flowtable_get_data(c, attr, &data_len);
260 }
261 
262 EXPORT_SYMBOL(nftnl_flowtable_get_str);
263 const char *nftnl_flowtable_get_str(const struct nftnl_flowtable *c, uint16_t attr)
264 {
265  return nftnl_flowtable_get(c, attr);
266 }
267 
268 EXPORT_SYMBOL(nftnl_flowtable_get_u32);
269 uint32_t nftnl_flowtable_get_u32(const struct nftnl_flowtable *c, uint16_t attr)
270 {
271  uint32_t data_len = 0;
272  const uint32_t *val = nftnl_flowtable_get_data(c, attr, &data_len);
273 
274  nftnl_assert(val, attr, data_len == sizeof(uint32_t));
275 
276  return val ? *val : 0;
277 }
278 
279 EXPORT_SYMBOL(nftnl_flowtable_get_u64);
280 uint64_t nftnl_flowtable_get_u64(const struct nftnl_flowtable *c, uint16_t attr)
281 {
282  uint32_t data_len = 0;
283  const uint64_t *val = nftnl_flowtable_get_data(c, attr, &data_len);
284 
285  nftnl_assert(val, attr, data_len == sizeof(uint64_t));
286 
287  return val ? *val : 0;
288 }
289 
290 EXPORT_SYMBOL(nftnl_flowtable_get_s32);
291 int32_t nftnl_flowtable_get_s32(const struct nftnl_flowtable *c, uint16_t attr)
292 {
293  uint32_t data_len = 0;
294  const int32_t *val = nftnl_flowtable_get_data(c, attr, &data_len);
295 
296  nftnl_assert(val, attr, data_len == sizeof(int32_t));
297 
298  return val ? *val : 0;
299 }
300 
301 EXPORT_SYMBOL(nftnl_flowtable_get_array);
302 const char *const *nftnl_flowtable_get_array(const struct nftnl_flowtable *c, uint16_t attr)
303 {
304  uint32_t data_len;
305  const char * const *val = nftnl_flowtable_get_data(c, attr, &data_len);
306 
307  nftnl_assert(val, attr, attr == NFTNL_FLOWTABLE_DEVICES);
308 
309  return val;
310 }
311 
312 EXPORT_SYMBOL(nftnl_flowtable_nlmsg_build_payload);
313 void nftnl_flowtable_nlmsg_build_payload(struct nlmsghdr *nlh,
314  const struct nftnl_flowtable *c)
315 {
316  struct nlattr *nest = NULL;
317  int i;
318 
319  if (c->flags & (1 << NFTNL_FLOWTABLE_TABLE))
320  mnl_attr_put_strz(nlh, NFTA_FLOWTABLE_TABLE, c->table);
321  if (c->flags & (1 << NFTNL_FLOWTABLE_NAME))
322  mnl_attr_put_strz(nlh, NFTA_FLOWTABLE_NAME, c->name);
323 
324  if (c->flags & (1 << NFTNL_FLOWTABLE_HOOKNUM) ||
325  c->flags & (1 << NFTNL_FLOWTABLE_PRIO) ||
326  c->flags & (1 << NFTNL_FLOWTABLE_DEVICES))
327  nest = mnl_attr_nest_start(nlh, NFTA_FLOWTABLE_HOOK);
328 
329  if (c->flags & (1 << NFTNL_FLOWTABLE_HOOKNUM))
330  mnl_attr_put_u32(nlh, NFTA_FLOWTABLE_HOOK_NUM, htonl(c->hooknum));
331  if (c->flags & (1 << NFTNL_FLOWTABLE_PRIO))
332  mnl_attr_put_u32(nlh, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(c->prio));
333 
334  if (c->flags & (1 << NFTNL_FLOWTABLE_DEVICES)) {
335  struct nlattr *nest_dev;
336 
337  nest_dev = mnl_attr_nest_start(nlh, NFTA_FLOWTABLE_HOOK_DEVS);
338  for (i = 0; i < c->dev_array_len; i++) {
339  mnl_attr_put_strz(nlh, NFTA_DEVICE_NAME,
340  c->dev_array[i]);
341  }
342  mnl_attr_nest_end(nlh, nest_dev);
343  }
344 
345  if (nest)
346  mnl_attr_nest_end(nlh, nest);
347 
348  if (c->flags & (1 << NFTNL_FLOWTABLE_FLAGS))
349  mnl_attr_put_u32(nlh, NFTA_FLOWTABLE_FLAGS, htonl(c->ft_flags));
350  if (c->flags & (1 << NFTNL_FLOWTABLE_USE))
351  mnl_attr_put_u32(nlh, NFTA_FLOWTABLE_USE, htonl(c->use));
352  if (c->flags & (1 << NFTNL_FLOWTABLE_HANDLE))
353  mnl_attr_put_u64(nlh, NFTA_FLOWTABLE_HANDLE, htobe64(c->handle));
354 }
355 
356 static int nftnl_flowtable_parse_attr_cb(const struct nlattr *attr, void *data)
357 {
358  const struct nlattr **tb = data;
359  int type = mnl_attr_get_type(attr);
360 
361  if (mnl_attr_type_valid(attr, NFTA_FLOWTABLE_MAX) < 0)
362  return MNL_CB_OK;
363 
364  switch(type) {
365  case NFTA_FLOWTABLE_NAME:
366  case NFTA_FLOWTABLE_TABLE:
367  if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
368  abi_breakage();
369  break;
370  case NFTA_FLOWTABLE_HOOK:
371  if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
372  abi_breakage();
373  break;
374  case NFTA_FLOWTABLE_FLAGS:
375  case NFTA_FLOWTABLE_USE:
376  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
377  abi_breakage();
378  break;
379  case NFTA_FLOWTABLE_HANDLE:
380  if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
381  abi_breakage();
382  break;
383  }
384 
385  tb[type] = attr;
386  return MNL_CB_OK;
387 }
388 
389 static int nftnl_flowtable_parse_hook_cb(const struct nlattr *attr, void *data)
390 {
391  const struct nlattr **tb = data;
392  int type = mnl_attr_get_type(attr);
393 
394  if (mnl_attr_type_valid(attr, NFTA_FLOWTABLE_HOOK_MAX) < 0)
395  return MNL_CB_OK;
396 
397  switch(type) {
398  case NFTA_FLOWTABLE_HOOK_NUM:
399  case NFTA_FLOWTABLE_HOOK_PRIORITY:
400  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
401  abi_breakage();
402  break;
403  case NFTA_FLOWTABLE_HOOK_DEVS:
404  if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
405  abi_breakage();
406  break;
407  }
408 
409  tb[type] = attr;
410  return MNL_CB_OK;
411 }
412 
413 static int nftnl_flowtable_parse_devs(struct nlattr *nest,
414  struct nftnl_flowtable *c)
415 {
416  const char **dev_array, **tmp;
417  int len = 0, size = 8;
418  struct nlattr *attr;
419 
420  dev_array = calloc(8, sizeof(char *));
421  if (!dev_array)
422  return -1;
423 
424  mnl_attr_for_each_nested(attr, nest) {
425  if (mnl_attr_get_type(attr) != NFTA_DEVICE_NAME)
426  goto err;
427  dev_array[len++] = strdup(mnl_attr_get_str(attr));
428  if (len >= size) {
429  tmp = realloc(dev_array, size * 2 * sizeof(char *));
430  if (!tmp)
431  goto err;
432 
433  size *= 2;
434  memset(&tmp[len], 0, (size - len) * sizeof(char *));
435  dev_array = tmp;
436  }
437  }
438 
439  c->dev_array = dev_array;
440  c->dev_array_len = len;
441 
442  return 0;
443 err:
444  while (len--)
445  xfree(dev_array[len]);
446  xfree(dev_array);
447  return -1;
448 }
449 
450 static int nftnl_flowtable_parse_hook(struct nlattr *attr, struct nftnl_flowtable *c)
451 {
452  struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1] = {};
453  int ret;
454 
455  if (mnl_attr_parse_nested(attr, nftnl_flowtable_parse_hook_cb, tb) < 0)
456  return -1;
457 
458  if (tb[NFTA_FLOWTABLE_HOOK_NUM]) {
459  c->hooknum = ntohl(mnl_attr_get_u32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
460  c->flags |= (1 << NFTNL_FLOWTABLE_HOOKNUM);
461  }
462  if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
463  c->prio = ntohl(mnl_attr_get_u32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
464  c->flags |= (1 << NFTNL_FLOWTABLE_PRIO);
465  }
466  if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) {
467  ret = nftnl_flowtable_parse_devs(tb[NFTA_FLOWTABLE_HOOK_DEVS], c);
468  if (ret < 0)
469  return -1;
470  c->flags |= (1 << NFTNL_FLOWTABLE_DEVICES);
471  }
472 
473  return 0;
474 }
475 
476 EXPORT_SYMBOL(nftnl_flowtable_nlmsg_parse);
477 int nftnl_flowtable_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_flowtable *c)
478 {
479  struct nlattr *tb[NFTA_FLOWTABLE_MAX + 1] = {};
480  struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
481  int ret = 0;
482 
483  if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_flowtable_parse_attr_cb, tb) < 0)
484  return -1;
485 
486  if (tb[NFTA_FLOWTABLE_NAME]) {
487  if (c->flags & (1 << NFTNL_FLOWTABLE_NAME))
488  xfree(c->name);
489  c->name = strdup(mnl_attr_get_str(tb[NFTA_FLOWTABLE_NAME]));
490  if (!c->name)
491  return -1;
492  c->flags |= (1 << NFTNL_FLOWTABLE_NAME);
493  }
494  if (tb[NFTA_FLOWTABLE_TABLE]) {
495  if (c->flags & (1 << NFTNL_FLOWTABLE_TABLE))
496  xfree(c->table);
497  c->table = strdup(mnl_attr_get_str(tb[NFTA_FLOWTABLE_TABLE]));
498  if (!c->table)
499  return -1;
500  c->flags |= (1 << NFTNL_FLOWTABLE_TABLE);
501  }
502  if (tb[NFTA_FLOWTABLE_HOOK]) {
503  ret = nftnl_flowtable_parse_hook(tb[NFTA_FLOWTABLE_HOOK], c);
504  if (ret < 0)
505  return ret;
506  }
507  if (tb[NFTA_FLOWTABLE_FLAGS]) {
508  c->ft_flags = ntohl(mnl_attr_get_u32(tb[NFTA_FLOWTABLE_FLAGS]));
509  c->flags |= (1 << NFTNL_FLOWTABLE_FLAGS);
510  }
511  if (tb[NFTA_FLOWTABLE_USE]) {
512  c->use = ntohl(mnl_attr_get_u32(tb[NFTA_FLOWTABLE_USE]));
513  c->flags |= (1 << NFTNL_FLOWTABLE_USE);
514  }
515  if (tb[NFTA_FLOWTABLE_HANDLE]) {
516  c->handle = be64toh(mnl_attr_get_u64(tb[NFTA_FLOWTABLE_HANDLE]));
517  c->flags |= (1 << NFTNL_FLOWTABLE_HANDLE);
518  }
519 
520  c->family = nfg->nfgen_family;
521  c->flags |= (1 << NFTNL_FLOWTABLE_FAMILY);
522 
523  return ret;
524 }
525 
526 static const char *nftnl_hooknum2str(int family, int hooknum)
527 {
528  switch (family) {
529  case NFPROTO_IPV4:
530  case NFPROTO_IPV6:
531  case NFPROTO_INET:
532  case NFPROTO_BRIDGE:
533  switch (hooknum) {
534  case NF_INET_PRE_ROUTING:
535  return "prerouting";
536  case NF_INET_LOCAL_IN:
537  return "input";
538  case NF_INET_FORWARD:
539  return "forward";
540  case NF_INET_LOCAL_OUT:
541  return "output";
542  case NF_INET_POST_ROUTING:
543  return "postrouting";
544  }
545  break;
546  case NFPROTO_ARP:
547  switch (hooknum) {
548  case NF_ARP_IN:
549  return "input";
550  case NF_ARP_OUT:
551  return "output";
552  case NF_ARP_FORWARD:
553  return "forward";
554  }
555  break;
556  case NFPROTO_NETDEV:
557  switch (hooknum) {
558  case NF_NETDEV_INGRESS:
559  return "ingress";
560  }
561  break;
562  }
563  return "unknown";
564 }
565 
566 static inline int nftnl_str2hooknum(int family, const char *hook)
567 {
568  int hooknum;
569 
570  for (hooknum = 0; hooknum < NF_INET_NUMHOOKS; hooknum++) {
571  if (strcmp(hook, nftnl_hooknum2str(family, hooknum)) == 0)
572  return hooknum;
573  }
574  return -1;
575 }
576 
577 EXPORT_SYMBOL(nftnl_flowtable_parse);
578 int nftnl_flowtable_parse(struct nftnl_flowtable *c, enum nftnl_parse_type type,
579  const char *data, struct nftnl_parse_err *err)
580 {
581  errno = EOPNOTSUPP;
582  return -1;
583 }
584 
585 EXPORT_SYMBOL(nftnl_flowtable_parse_file);
586 int nftnl_flowtable_parse_file(struct nftnl_flowtable *c,
587  enum nftnl_parse_type type,
588  FILE *fp, struct nftnl_parse_err *err)
589 {
590  errno = EOPNOTSUPP;
591  return -1;
592 }
593 
594 static int nftnl_flowtable_snprintf_default(char *buf, size_t remain,
595  const struct nftnl_flowtable *c)
596 {
597  int ret, offset = 0, i;
598 
599  ret = snprintf(buf, remain, "flow table %s %s use %u size %u flags %x",
600  c->table, c->name, c->use, c->size, c->ft_flags);
601  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
602 
603  if (c->flags & (1 << NFTNL_FLOWTABLE_HOOKNUM)) {
604  ret = snprintf(buf + offset, remain, " hook %s prio %d ",
605  nftnl_hooknum2str(c->family, c->hooknum),
606  c->prio);
607  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
608 
609  if (c->flags & (1 << NFTNL_FLOWTABLE_DEVICES)) {
610  ret = snprintf(buf + offset, remain, " dev { ");
611  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
612 
613  for (i = 0; i < c->dev_array_len; i++) {
614  ret = snprintf(buf + offset, remain, " %s ",
615  c->dev_array[i]);
616  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
617  }
618  ret = snprintf(buf + offset, remain, " } ");
619  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
620  }
621  }
622 
623  return offset;
624 }
625 
626 static int nftnl_flowtable_cmd_snprintf(char *buf, size_t remain,
627  const struct nftnl_flowtable *c,
628  uint32_t cmd, uint32_t type,
629  uint32_t flags)
630 {
631  int ret, offset = 0;
632 
633  if (type != NFTNL_OUTPUT_DEFAULT)
634  return -1;
635 
636  ret = nftnl_flowtable_snprintf_default(buf + offset, remain, c);
637  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
638  return offset;
639 }
640 
641 EXPORT_SYMBOL(nftnl_flowtable_snprintf);
642 int nftnl_flowtable_snprintf(char *buf, size_t size, const struct nftnl_flowtable *c,
643  uint32_t type, uint32_t flags)
644 {
645  if (size)
646  buf[0] = '\0';
647 
648  return nftnl_flowtable_cmd_snprintf(buf, size, c, nftnl_flag2cmd(flags),
649  type, flags);
650 }
651 
652 static int nftnl_flowtable_do_snprintf(char *buf, size_t size, const void *c,
653  uint32_t cmd, uint32_t type, uint32_t flags)
654 {
655  return nftnl_flowtable_snprintf(buf, size, c, type, flags);
656 }
657 
658 EXPORT_SYMBOL(nftnl_flowtable_fprintf);
659 int nftnl_flowtable_fprintf(FILE *fp, const struct nftnl_flowtable *c,
660  uint32_t type, uint32_t flags)
661 {
662  return nftnl_fprintf(fp, c, NFTNL_CMD_UNSPEC, type, flags,
663  nftnl_flowtable_do_snprintf);
664 }
665 
667  struct list_head list;
668 };
669 
670 EXPORT_SYMBOL(nftnl_flowtable_list_alloc);
671 struct nftnl_flowtable_list *nftnl_flowtable_list_alloc(void)
672 {
673  struct nftnl_flowtable_list *list;
674 
675  list = calloc(1, sizeof(struct nftnl_flowtable_list));
676  if (list == NULL)
677  return NULL;
678 
679  INIT_LIST_HEAD(&list->list);
680 
681  return list;
682 }
683 
684 EXPORT_SYMBOL(nftnl_flowtable_list_free);
685 void nftnl_flowtable_list_free(struct nftnl_flowtable_list *list)
686 {
687  struct nftnl_flowtable *s, *tmp;
688 
689  list_for_each_entry_safe(s, tmp, &list->list, head) {
690  list_del(&s->head);
691  nftnl_flowtable_free(s);
692  }
693  xfree(list);
694 }
695 
696 EXPORT_SYMBOL(nftnl_flowtable_list_is_empty);
697 int nftnl_flowtable_list_is_empty(const struct nftnl_flowtable_list *list)
698 {
699  return list_empty(&list->list);
700 }
701 
702 EXPORT_SYMBOL(nftnl_flowtable_list_add);
703 void nftnl_flowtable_list_add(struct nftnl_flowtable *s,
704  struct nftnl_flowtable_list *list)
705 {
706  list_add(&s->head, &list->list);
707 }
708 
709 EXPORT_SYMBOL(nftnl_flowtable_list_add_tail);
710 void nftnl_flowtable_list_add_tail(struct nftnl_flowtable *s,
711  struct nftnl_flowtable_list *list)
712 {
713  list_add_tail(&s->head, &list->list);
714 }
715 
716 EXPORT_SYMBOL(nftnl_flowtable_list_del);
717 void nftnl_flowtable_list_del(struct nftnl_flowtable *s)
718 {
719  list_del(&s->head);
720 }
721 
722 EXPORT_SYMBOL(nftnl_flowtable_list_foreach);
723 int nftnl_flowtable_list_foreach(struct nftnl_flowtable_list *flowtable_list,
724  int (*cb)(struct nftnl_flowtable *t, void *data), void *data)
725 {
726  struct nftnl_flowtable *cur, *tmp;
727  int ret;
728 
729  list_for_each_entry_safe(cur, tmp, &flowtable_list->list, head) {
730  ret = cb(cur, data);
731  if (ret < 0)
732  return ret;
733  }
734  return 0;
735 }