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