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