1 From: Pablo Neira Ayuso <pablo@netfilter.org>
2 Date: Sun, 3 Dec 2017 21:05:54 +0100
3 Subject: [PATCH] expr: add flow offload expression
5 This patch adds the new "flow_offload" expression to select what flows
6 are offloaded to an existing flowtable.
8 Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 create mode 100644 src/expr/flow_offload.c
12 --- a/include/libnftnl/expr.h
13 +++ b/include/libnftnl/expr.h
14 @@ -221,6 +221,10 @@ enum {
18 + NFTNL_EXPR_FLOW_TABLE_NAME = NFTNL_EXPR_BASE,
22 NFTNL_EXPR_FWD_SREG_DEV = NFTNL_EXPR_BASE,
25 --- a/include/linux/netfilter/nf_tables.h
26 +++ b/include/linux/netfilter/nf_tables.h
27 @@ -952,6 +952,17 @@ enum nft_ct_attributes {
29 #define NFTA_CT_MAX (__NFTA_CT_MAX - 1)
32 + * enum nft_flow_attributes - ct offload expression attributes
33 + * @NFTA_FLOW_TABLE_NAME: flow table name (NLA_STRING)
35 +enum nft_offload_attributes {
37 + NFTA_FLOW_TABLE_NAME,
40 +#define NFTA_FLOW_MAX (__NFTA_FLOW_MAX - 1)
47 @@ -32,6 +32,7 @@ libnftnl_la_SOURCES = utils.c \
51 + expr/flow_offload.c \
56 +++ b/src/expr/flow_offload.c
58 +#include "internal.h"
62 +#include <string.h> /* for memcpy */
63 +#include <arpa/inet.h>
65 +#include <libmnl/libmnl.h>
66 +#include <linux/netfilter/nf_tables.h>
67 +#include <libnftnl/rule.h>
68 +#include <libnftnl/expr.h>
70 +struct nftnl_expr_flow {
74 +static int nftnl_expr_flow_set(struct nftnl_expr *e, uint16_t type,
75 + const void *data, uint32_t data_len)
77 + struct nftnl_expr_flow *flow = nftnl_expr_data(e);
80 + case NFTNL_EXPR_FLOW_TABLE_NAME:
81 + flow->table_name = strdup((const char *)data);
82 + if (!flow->table_name)
91 +static const void *nftnl_expr_flow_get(const struct nftnl_expr *e,
92 + uint16_t type, uint32_t *data_len)
94 + struct nftnl_expr_flow *flow = nftnl_expr_data(e);
97 + case NFTNL_EXPR_FLOW_TABLE_NAME:
98 + *data_len = strlen(flow->table_name) + 1;
99 + return flow->table_name;
104 +static int nftnl_expr_flow_cb(const struct nlattr *attr, void *data)
106 + const struct nlattr **tb = data;
107 + int type = mnl_attr_get_type(attr);
109 + if (mnl_attr_type_valid(attr, NFTA_FLOW_MAX) < 0)
113 + case NFTA_FLOW_TABLE_NAME:
114 + if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
123 +static void nftnl_expr_flow_build(struct nlmsghdr *nlh,
124 + const struct nftnl_expr *e)
126 + struct nftnl_expr_flow *flow = nftnl_expr_data(e);
128 + if (e->flags & (1 << NFTNL_EXPR_FLOW_TABLE_NAME))
129 + mnl_attr_put_strz(nlh, NFTA_FLOW_TABLE_NAME, flow->table_name);
132 +static int nftnl_expr_flow_parse(struct nftnl_expr *e, struct nlattr *attr)
134 + struct nftnl_expr_flow *flow = nftnl_expr_data(e);
135 + struct nlattr *tb[NFTA_FLOW_MAX+1] = {};
138 + if (mnl_attr_parse_nested(attr, nftnl_expr_flow_cb, tb) < 0)
141 + if (tb[NFTA_FLOW_TABLE_NAME]) {
143 + strdup(mnl_attr_get_str(tb[NFTA_FLOW_TABLE_NAME]));
144 + if (!flow->table_name)
146 + e->flags |= (1 << NFTNL_EXPR_FLOW_TABLE_NAME);
153 +nftnl_expr_flow_json_parse(struct nftnl_expr *e, json_t *root,
154 + struct nftnl_parse_err *err)
157 + const char *table_name;
159 + table_name = nftnl_jansson_parse_str(root, "flowtable", err);
160 + if (table_name != NULL)
161 + nftnl_expr_set_str(e, NFTNL_EXPR_FLOW_TABLE_NAME, table_name);
165 + errno = EOPNOTSUPP;
170 +static int nftnl_expr_flow_export(char *buf, size_t size,
171 + const struct nftnl_expr *e, int type)
173 + struct nftnl_expr_flow *l = nftnl_expr_data(e);
174 + NFTNL_BUF_INIT(b, buf, size);
176 + if (e->flags & (1 << NFTNL_EXPR_FLOW_TABLE_NAME))
177 + nftnl_buf_str(&b, type, l->table_name, SET);
179 + return nftnl_buf_done(&b);
182 +static int nftnl_expr_flow_snprintf_default(char *buf, size_t size,
183 + const struct nftnl_expr *e)
185 + int remain = size, offset = 0, ret;
186 + struct nftnl_expr_flow *l = nftnl_expr_data(e);
188 + ret = snprintf(buf, remain, "flowtable %s ", l->table_name);
189 + SNPRINTF_BUFFER_SIZE(ret, remain, offset);
194 +static int nftnl_expr_flow_snprintf(char *buf, size_t size, uint32_t type,
195 + uint32_t flags, const struct nftnl_expr *e)
198 + case NFTNL_OUTPUT_DEFAULT:
199 + return nftnl_expr_flow_snprintf_default(buf, size, e);
200 + case NFTNL_OUTPUT_XML:
201 + case NFTNL_OUTPUT_JSON:
202 + return nftnl_expr_flow_export(buf, size, e, type);
209 +static void nftnl_expr_flow_free(const struct nftnl_expr *e)
211 + struct nftnl_expr_flow *flow = nftnl_expr_data(e);
213 + xfree(flow->table_name);
216 +static bool nftnl_expr_flow_cmp(const struct nftnl_expr *e1,
217 + const struct nftnl_expr *e2)
219 + struct nftnl_expr_flow *l1 = nftnl_expr_data(e1);
220 + struct nftnl_expr_flow *l2 = nftnl_expr_data(e2);
223 + if (e1->flags & (1 << NFTNL_EXPR_FLOW_TABLE_NAME))
224 + eq &= !strcmp(l1->table_name, l2->table_name);
229 +struct expr_ops expr_ops_flow = {
230 + .name = "flow_offload",
231 + .alloc_len = sizeof(struct nftnl_expr_flow),
232 + .max_attr = NFTA_FLOW_MAX,
233 + .free = nftnl_expr_flow_free,
234 + .cmp = nftnl_expr_flow_cmp,
235 + .set = nftnl_expr_flow_set,
236 + .get = nftnl_expr_flow_get,
237 + .parse = nftnl_expr_flow_parse,
238 + .build = nftnl_expr_flow_build,
239 + .snprintf = nftnl_expr_flow_snprintf,
240 + .json_parse = nftnl_expr_flow_json_parse,
244 @@ -33,6 +33,7 @@ extern struct expr_ops expr_ops_target;
245 extern struct expr_ops expr_ops_dynset;
246 extern struct expr_ops expr_ops_hash;
247 extern struct expr_ops expr_ops_fib;
248 +extern struct expr_ops expr_ops_flow;
250 static struct expr_ops expr_ops_notrack = {
252 @@ -69,6 +70,7 @@ static struct expr_ops *expr_ops[] = {