libnftnl  1.3.2
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
18 nftnl_expr_flow_set(struct nftnl_expr *e, uint16_t type,
19  const void *data, uint32_t data_len, uint32_t byteorder)
20 {
21  struct nftnl_expr_flow *flow = nftnl_expr_data(e);
22 
23  switch (type) {
24  case NFTNL_EXPR_FLOW_TABLE_NAME:
25  flow->table_name = strdup((const char *)data);
26  if (!flow->table_name)
27  return -1;
28  break;
29  }
30  return 0;
31 }
32 
33 static const void *nftnl_expr_flow_get(const struct nftnl_expr *e,
34  uint16_t type, uint32_t *data_len)
35 {
36  struct nftnl_expr_flow *flow = nftnl_expr_data(e);
37 
38  switch(type) {
39  case NFTNL_EXPR_FLOW_TABLE_NAME:
40  *data_len = strlen(flow->table_name) + 1;
41  return flow->table_name;
42  }
43  return NULL;
44 }
45 
46 static int nftnl_expr_flow_cb(const struct nlattr *attr, void *data)
47 {
48  const struct nlattr **tb = data;
49  int type = mnl_attr_get_type(attr);
50 
51  if (mnl_attr_type_valid(attr, NFTA_FLOW_MAX) < 0)
52  return MNL_CB_OK;
53 
54  switch(type) {
55  case NFTA_FLOW_TABLE_NAME:
56  if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
57  abi_breakage();
58  break;
59  }
60 
61  tb[type] = attr;
62  return MNL_CB_OK;
63 }
64 
65 static void nftnl_expr_flow_build(struct nlmsghdr *nlh,
66  const struct nftnl_expr *e)
67 {
68  struct nftnl_expr_flow *flow = nftnl_expr_data(e);
69 
70  if (e->flags & (1 << NFTNL_EXPR_FLOW_TABLE_NAME))
71  mnl_attr_put_strz(nlh, NFTA_FLOW_TABLE_NAME, flow->table_name);
72 }
73 
74 static int nftnl_expr_flow_parse(struct nftnl_expr *e, struct nlattr *attr)
75 {
76  struct nftnl_expr_flow *flow = nftnl_expr_data(e);
77  struct nlattr *tb[NFTA_FLOW_MAX+1] = {};
78  int ret = 0;
79 
80  if (mnl_attr_parse_nested(attr, nftnl_expr_flow_cb, tb) < 0)
81  return -1;
82 
83  if (nftnl_parse_str_attr(tb[NFTA_FLOW_TABLE_NAME],
84  NFTNL_EXPR_FLOW_TABLE_NAME,
85  (const char **)&flow->table_name,
86  &e->flags) < 0)
87  return -1;
88 
89  return ret;
90 }
91 
92 static int nftnl_expr_flow_snprintf(char *buf, size_t remain,
93  uint32_t flags, const struct nftnl_expr *e)
94 {
95  struct nftnl_expr_flow *l = nftnl_expr_data(e);
96  int offset = 0, ret;
97 
98  ret = snprintf(buf, remain, "flowtable %s ", l->table_name);
99  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
100 
101  return offset;
102 }
103 
104 static void nftnl_expr_flow_free(const struct nftnl_expr *e)
105 {
106  struct nftnl_expr_flow *flow = nftnl_expr_data(e);
107 
108  xfree(flow->table_name);
109 }
110 
111 static struct attr_policy flow_offload_attr_policy[__NFTNL_EXPR_FLOW_MAX] = {
112  [NFTNL_EXPR_FLOW_TABLE_NAME] = { .maxlen = NFT_NAME_MAXLEN },
113 };
114 
115 struct expr_ops expr_ops_flow = {
116  .name = "flow_offload",
117  .alloc_len = sizeof(struct nftnl_expr_flow),
118  .nftnl_max_attr = __NFTNL_EXPR_FLOW_MAX - 1,
119  .attr_policy = flow_offload_attr_policy,
120  .free = nftnl_expr_flow_free,
121  .set = nftnl_expr_flow_set,
122  .get = nftnl_expr_flow_get,
123  .parse = nftnl_expr_flow_parse,
124  .build = nftnl_expr_flow_build,
125  .output = nftnl_expr_flow_snprintf,
126 };