libnftnl  1.1.4
flow_offload.c
1 #include "internal.h"
2 
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <string.h> /* for memcpy */
6 #include <arpa/inet.h>
7 #include <errno.h>
8 #include <libmnl/libmnl.h>
9 #include <linux/netfilter/nf_tables.h>
10 #include <libnftnl/rule.h>
11 #include <libnftnl/expr.h>
12 
14  char *table_name;
15 };
16 
17 static int nftnl_expr_flow_set(struct nftnl_expr *e, uint16_t type,
18  const void *data, uint32_t data_len)
19 {
20  struct nftnl_expr_flow *flow = nftnl_expr_data(e);
21 
22  switch (type) {
23  case NFTNL_EXPR_FLOW_TABLE_NAME:
24  flow->table_name = strdup((const char *)data);
25  if (!flow->table_name)
26  return -1;
27  break;
28  default:
29  return -1;
30  }
31  return 0;
32 }
33 
34 static const void *nftnl_expr_flow_get(const struct nftnl_expr *e,
35  uint16_t type, uint32_t *data_len)
36 {
37  struct nftnl_expr_flow *flow = nftnl_expr_data(e);
38 
39  switch(type) {
40  case NFTNL_EXPR_FLOW_TABLE_NAME:
41  *data_len = strlen(flow->table_name) + 1;
42  return flow->table_name;
43  }
44  return NULL;
45 }
46 
47 static int nftnl_expr_flow_cb(const struct nlattr *attr, void *data)
48 {
49  const struct nlattr **tb = data;
50  int type = mnl_attr_get_type(attr);
51 
52  if (mnl_attr_type_valid(attr, NFTA_FLOW_MAX) < 0)
53  return MNL_CB_OK;
54 
55  switch(type) {
56  case NFTA_FLOW_TABLE_NAME:
57  if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
58  abi_breakage();
59  break;
60  }
61 
62  tb[type] = attr;
63  return MNL_CB_OK;
64 }
65 
66 static void nftnl_expr_flow_build(struct nlmsghdr *nlh,
67  const struct nftnl_expr *e)
68 {
69  struct nftnl_expr_flow *flow = nftnl_expr_data(e);
70 
71  if (e->flags & (1 << NFTNL_EXPR_FLOW_TABLE_NAME))
72  mnl_attr_put_strz(nlh, NFTA_FLOW_TABLE_NAME, flow->table_name);
73 }
74 
75 static int nftnl_expr_flow_parse(struct nftnl_expr *e, struct nlattr *attr)
76 {
77  struct nftnl_expr_flow *flow = nftnl_expr_data(e);
78  struct nlattr *tb[NFTA_FLOW_MAX+1] = {};
79  int ret = 0;
80 
81  if (mnl_attr_parse_nested(attr, nftnl_expr_flow_cb, tb) < 0)
82  return -1;
83 
84  if (tb[NFTA_FLOW_TABLE_NAME]) {
85  flow->table_name =
86  strdup(mnl_attr_get_str(tb[NFTA_FLOW_TABLE_NAME]));
87  if (!flow->table_name)
88  return -1;
89  e->flags |= (1 << NFTNL_EXPR_FLOW_TABLE_NAME);
90  }
91 
92  return ret;
93 }
94 
95 static int nftnl_expr_flow_snprintf_default(char *buf, size_t size,
96  const struct nftnl_expr *e)
97 {
98  int remain = size, offset = 0, ret;
99  struct nftnl_expr_flow *l = nftnl_expr_data(e);
100 
101  ret = snprintf(buf, remain, "flowtable %s ", l->table_name);
102  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
103 
104  return offset;
105 }
106 
107 static int nftnl_expr_flow_snprintf(char *buf, size_t size, uint32_t type,
108  uint32_t flags, const struct nftnl_expr *e)
109 {
110  switch(type) {
111  case NFTNL_OUTPUT_DEFAULT:
112  return nftnl_expr_flow_snprintf_default(buf, size, e);
113  case NFTNL_OUTPUT_XML:
114  case NFTNL_OUTPUT_JSON:
115  default:
116  break;
117  }
118  return -1;
119 }
120 
121 static void nftnl_expr_flow_free(const struct nftnl_expr *e)
122 {
123  struct nftnl_expr_flow *flow = nftnl_expr_data(e);
124 
125  xfree(flow->table_name);
126 }
127 
128 struct expr_ops expr_ops_flow = {
129  .name = "flow_offload",
130  .alloc_len = sizeof(struct nftnl_expr_flow),
131  .max_attr = NFTA_FLOW_MAX,
132  .free = nftnl_expr_flow_free,
133  .set = nftnl_expr_flow_set,
134  .get = nftnl_expr_flow_get,
135  .parse = nftnl_expr_flow_parse,
136  .build = nftnl_expr_flow_build,
137  .snprintf = nftnl_expr_flow_snprintf,
138 };