1 From: Pablo Neira Ayuso <pablo@netfilter.org>
2 Date: Mon, 4 Dec 2017 13:28:25 +0100
3 Subject: [PATCH] src: support for flowtable listing
5 This patch allows you to dump existing flowtable.
10 hook ingress priority 10
11 devices = { eth0, tap0 }
15 You can also list existing flowtables via:
20 hook ingress priority 10
21 devices = { eth0, tap0 }
25 You need a Linux kernel >= 4.16-rc to test this new feature.
27 Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
30 --- a/include/linux/netfilter/nf_tables.h
31 +++ b/include/linux/netfilter/nf_tables.h
32 @@ -92,6 +92,9 @@ enum nft_verdicts {
33 * @NFT_MSG_GETOBJ: get a stateful object (enum nft_obj_attributes)
34 * @NFT_MSG_DELOBJ: delete a stateful object (enum nft_obj_attributes)
35 * @NFT_MSG_GETOBJ_RESET: get and reset a stateful object (enum nft_obj_attributes)
36 + * @NFT_MSG_NEWFLOWTABLE: add new flow table (enum nft_flowtable_attributes)
37 + * @NFT_MSG_GETFLOWTABLE: get flow table (enum nft_flowtable_attributes)
38 + * @NFT_MSG_DELFLOWTABLE: delete flow table (enum nft_flowtable_attributes)
40 enum nf_tables_msg_types {
42 @@ -116,6 +119,9 @@ enum nf_tables_msg_types {
46 + NFT_MSG_NEWFLOWTABLE,
47 + NFT_MSG_GETFLOWTABLE,
48 + NFT_MSG_DELFLOWTABLE,
54 @@ -89,6 +89,9 @@ int mnl_nft_obj_batch_add(struct nftnl_o
55 int mnl_nft_obj_batch_del(struct nftnl_obj *nln, struct nftnl_batch *batch,
56 unsigned int flags, uint32_t seqnum);
58 +struct nftnl_flowtable_list *
59 +mnl_nft_flowtable_dump(struct netlink_ctx *ctx, int family, const char *table);
61 struct nftnl_ruleset *mnl_nft_ruleset_dump(struct netlink_ctx *ctx,
63 int mnl_nft_event_listener(struct mnl_socket *nf_sock, unsigned int debug_mask,
64 --- a/include/netlink.h
65 +++ b/include/netlink.h
66 @@ -179,6 +179,10 @@ extern int netlink_add_obj(struct netlin
67 extern int netlink_delete_obj(struct netlink_ctx *ctx, const struct handle *h,
68 struct location *loc, uint32_t type);
70 +extern int netlink_list_flowtables(struct netlink_ctx *ctx,
71 + const struct handle *h,
72 + const struct location *loc);
74 extern void netlink_dump_chain(const struct nftnl_chain *nlc,
75 struct netlink_ctx *ctx);
76 extern void netlink_dump_rule(const struct nftnl_rule *nlr,
79 @@ -35,6 +35,7 @@ struct position_spec {
80 * @chain: chain name (chains and rules only)
81 * @set: set name (sets only)
82 * @obj: stateful object name (stateful object only)
83 + * @flowtable: flow table name (flow table only)
84 * @handle: rule handle (rules only)
85 * @position: rule position (rules only)
86 * @set_id: set ID (sets only)
87 @@ -45,6 +46,7 @@ struct handle {
91 + const char *flowtable;
92 struct handle_spec handle;
93 struct position_spec position;
95 @@ -98,6 +100,7 @@ enum table_flags {
96 * @chains: chains contained in the table
97 * @sets: sets contained in the table
98 * @objs: stateful objects contained in the table
99 + * @flowtables: flow tables contained in the table
100 * @flags: table flags
101 * @refcnt: table reference counter
103 @@ -109,6 +112,7 @@ struct table {
104 struct list_head chains;
105 struct list_head sets;
106 struct list_head objs;
107 + struct list_head flowtables;
108 enum table_flags flags;
111 @@ -315,6 +319,24 @@ void obj_print_plain(const struct obj *o
112 const char *obj_type_name(uint32_t type);
113 uint32_t obj_type_to_cmd(uint32_t type);
116 + struct list_head list;
117 + struct handle handle;
118 + struct location location;
119 + unsigned int hooknum;
121 + const char **dev_array;
123 + unsigned int refcnt;
126 +extern struct flowtable *flowtable_alloc(const struct location *loc);
127 +extern struct flowtable *flowtable_get(struct flowtable *flowtable);
128 +extern void flowtable_free(struct flowtable *flowtable);
129 +extern void flowtable_add_hash(struct flowtable *flowtable, struct table *table);
131 +void flowtable_print(const struct flowtable *n, struct output_ctx *octx);
134 * enum cmd_ops - command operations
136 @@ -373,6 +395,7 @@ enum cmd_ops {
137 * @CMD_OBJ_QUOTAS: multiple quotas
138 * @CMD_OBJ_LIMIT: limit
139 * @CMD_OBJ_LIMITS: multiple limits
140 + * @CMD_OBJ_FLOWTABLES: flow tables
144 @@ -399,6 +422,7 @@ enum cmd_obj {
148 + CMD_OBJ_FLOWTABLES,
154 @@ -3196,6 +3196,7 @@ static int cmd_evaluate_list(struct eval
155 case CMD_OBJ_CT_HELPERS:
158 + case CMD_OBJ_FLOWTABLES:
159 if (cmd->handle.table == NULL)
161 if (table_lookup(&cmd->handle, ctx->cache) == NULL)
165 #include <libnftnl/expr.h>
166 #include <libnftnl/set.h>
167 #include <libnftnl/object.h>
168 +#include <libnftnl/flowtable.h>
169 #include <libnftnl/batch.h>
171 #include <linux/netfilter/nfnetlink.h>
172 @@ -953,6 +954,63 @@ int mnl_nft_setelem_get(struct netlink_c
173 return nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, set_elem_cb, nls);
176 +static int flowtable_cb(const struct nlmsghdr *nlh, void *data)
178 + struct nftnl_flowtable_list *nln_list = data;
179 + struct nftnl_flowtable *n;
181 + if (check_genid(nlh) < 0)
182 + return MNL_CB_ERROR;
184 + n = nftnl_flowtable_alloc();
186 + memory_allocation_error();
188 + if (nftnl_flowtable_nlmsg_parse(nlh, n) < 0)
191 + nftnl_flowtable_list_add_tail(n, nln_list);
195 + nftnl_flowtable_free(n);
199 +struct nftnl_flowtable_list *
200 +mnl_nft_flowtable_dump(struct netlink_ctx *ctx, int family, const char *table)
202 + struct nftnl_flowtable_list *nln_list;
203 + char buf[MNL_SOCKET_BUFFER_SIZE];
204 + struct nftnl_flowtable *n;
205 + struct nlmsghdr *nlh;
208 + n = nftnl_flowtable_alloc();
210 + memory_allocation_error();
212 + nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETFLOWTABLE, family,
213 + NLM_F_DUMP | NLM_F_ACK, ctx->seqnum);
215 + nftnl_flowtable_set_str(n, NFTNL_FLOWTABLE_TABLE, table);
216 + nftnl_flowtable_nlmsg_build_payload(nlh, n);
217 + nftnl_flowtable_free(n);
219 + nln_list = nftnl_flowtable_list_alloc();
220 + if (nln_list == NULL)
221 + memory_allocation_error();
223 + ret = nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, flowtable_cb, nln_list);
229 + nftnl_flowtable_list_free(nln_list);
239 #include <libnftnl/expr.h>
240 #include <libnftnl/object.h>
241 #include <libnftnl/set.h>
242 +#include <libnftnl/flowtable.h>
243 #include <libnftnl/udata.h>
244 #include <libnftnl/ruleset.h>
245 #include <libnftnl/common.h>
246 @@ -1826,6 +1827,70 @@ int netlink_reset_objs(struct netlink_ct
250 +static struct flowtable *
251 +netlink_delinearize_flowtable(struct netlink_ctx *ctx,
252 + struct nftnl_flowtable *nlo)
254 + struct flowtable *flowtable;
255 + const char **dev_array;
258 + flowtable = flowtable_alloc(&netlink_location);
259 + flowtable->handle.family =
260 + nftnl_flowtable_get_u32(nlo, NFTNL_FLOWTABLE_FAMILY);
261 + flowtable->handle.table =
262 + xstrdup(nftnl_flowtable_get_str(nlo, NFTNL_FLOWTABLE_TABLE));
263 + flowtable->handle.flowtable =
264 + xstrdup(nftnl_flowtable_get_str(nlo, NFTNL_FLOWTABLE_NAME));
265 + dev_array = nftnl_flowtable_get_array(nlo, NFTNL_FLOWTABLE_DEVICES);
266 + while (dev_array[len] != '\0')
269 + flowtable->dev_array = calloc(1, len * sizeof(char *));
270 + for (i = 0; i < len; i++)
271 + flowtable->dev_array[i] = xstrdup(dev_array[i]);
273 + flowtable->dev_array_len = len;
275 + flowtable->priority =
276 + nftnl_flowtable_get_u32(nlo, NFTNL_FLOWTABLE_PRIO);
277 + flowtable->hooknum =
278 + nftnl_flowtable_get_u32(nlo, NFTNL_FLOWTABLE_HOOKNUM);
283 +static int list_flowtable_cb(struct nftnl_flowtable *nls, void *arg)
285 + struct netlink_ctx *ctx = arg;
286 + struct flowtable *flowtable;
288 + flowtable = netlink_delinearize_flowtable(ctx, nls);
289 + if (flowtable == NULL)
291 + list_add_tail(&flowtable->list, &ctx->list);
295 +int netlink_list_flowtables(struct netlink_ctx *ctx, const struct handle *h,
296 + const struct location *loc)
298 + struct nftnl_flowtable_list *flowtable_cache;
301 + flowtable_cache = mnl_nft_flowtable_dump(ctx, h->family, h->table);
302 + if (flowtable_cache == NULL) {
303 + if (errno == EINTR)
309 + err = nftnl_flowtable_list_foreach(flowtable_cache, list_flowtable_cb, ctx);
310 + nftnl_flowtable_list_free(flowtable_cache);
314 int netlink_batch_send(struct netlink_ctx *ctx, struct list_head *err_list)
316 return mnl_batch_talk(ctx, err_list);
317 --- a/src/parser_bison.y
318 +++ b/src/parser_bison.y
319 @@ -248,6 +248,8 @@ int nft_lex(void *, void *, void *);
321 %token METERS "meters"
323 +%token FLOWTABLES "flowtables"
325 %token <val> NUM "number"
326 %token <string> STRING "string"
327 %token <string> QUOTED_STRING "quoted string"
328 @@ -1104,6 +1106,10 @@ list_cmd : TABLE table_spec
330 $$ = cmd_alloc(CMD_LIST, CMD_OBJ_METER, &$2, &@$, NULL);
332 + | FLOWTABLES ruleset_spec
334 + $$ = cmd_alloc(CMD_LIST, CMD_OBJ_FLOWTABLES, &$2, &@$, NULL);
338 $$ = cmd_alloc(CMD_LIST, CMD_OBJ_MAPS, &$2, &@$, NULL);
341 @@ -95,6 +95,11 @@ static int cache_init_objects(struct net
343 list_splice_tail_init(&ctx->list, &table->chains);
345 + ret = netlink_list_flowtables(ctx, &table->handle, &internal_location);
348 + list_splice_tail_init(&ctx->list, &table->flowtables);
350 if (cmd != CMD_RESET) {
351 ret = netlink_list_objs(ctx, &table->handle, &internal_location);
353 @@ -722,6 +727,7 @@ struct table *table_alloc(void)
354 init_list_head(&table->chains);
355 init_list_head(&table->sets);
356 init_list_head(&table->objs);
357 + init_list_head(&table->flowtables);
358 init_list_head(&table->scope.symbols);
361 @@ -797,6 +803,7 @@ static void table_print_options(const st
363 static void table_print(const struct table *table, struct output_ctx *octx)
365 + struct flowtable *flowtable;
369 @@ -818,6 +825,11 @@ static void table_print(const struct tab
370 set_print(set, octx);
373 + list_for_each_entry(flowtable, &table->flowtables, list) {
374 + nft_print(octx, "%s", delim);
375 + flowtable_print(flowtable, octx);
378 list_for_each_entry(chain, &table->chains, list) {
379 nft_print(octx, "%s", delim);
380 chain_print(chain, octx);
381 @@ -1481,6 +1493,114 @@ static int do_list_obj(struct netlink_ct
385 +struct flowtable *flowtable_alloc(const struct location *loc)
387 + struct flowtable *flowtable;
389 + flowtable = xzalloc(sizeof(*flowtable));
391 + flowtable->location = *loc;
393 + flowtable->refcnt = 1;
397 +struct flowtable *flowtable_get(struct flowtable *flowtable)
399 + flowtable->refcnt++;
403 +void flowtable_free(struct flowtable *flowtable)
405 + if (--flowtable->refcnt > 0)
407 + handle_free(&flowtable->handle);
411 +void flowtable_add_hash(struct flowtable *flowtable, struct table *table)
413 + list_add_tail(&flowtable->list, &table->flowtables);
416 +static void flowtable_print_declaration(const struct flowtable *flowtable,
417 + struct print_fmt_options *opts,
418 + struct output_ctx *octx)
422 + nft_print(octx, "%sflowtable", opts->tab);
424 + if (opts->family != NULL)
425 + nft_print(octx, " %s", opts->family);
427 + if (opts->table != NULL)
428 + nft_print(octx, " %s", opts->table);
430 + nft_print(octx, " %s {%s", flowtable->handle.flowtable, opts->nl);
432 + nft_print(octx, "%s%shook %s priority %d%s",
433 + opts->tab, opts->tab, "ingress",
434 + flowtable->priority, opts->stmt_separator);
436 + nft_print(octx, "%s%sdevices = { ", opts->tab, opts->tab);
437 + for (i = 0; i < flowtable->dev_array_len; i++) {
438 + nft_print(octx, "%s", flowtable->dev_array[i]);
439 + if (i + 1 != flowtable->dev_array_len)
440 + nft_print(octx, ", ");
442 + nft_print(octx, " }%s", opts->stmt_separator);
445 +static void do_flowtable_print(const struct flowtable *flowtable,
446 + struct print_fmt_options *opts,
447 + struct output_ctx *octx)
449 + flowtable_print_declaration(flowtable, opts, octx);
450 + nft_print(octx, "%s}%s", opts->tab, opts->nl);
453 +void flowtable_print(const struct flowtable *s, struct output_ctx *octx)
455 + struct print_fmt_options opts = {
458 + .stmt_separator = "\n",
461 + do_flowtable_print(s, &opts, octx);
464 +static int do_list_flowtables(struct netlink_ctx *ctx, struct cmd *cmd)
466 + struct print_fmt_options opts = {
469 + .stmt_separator = "\n",
471 + struct flowtable *flowtable;
472 + struct table *table;
474 + list_for_each_entry(table, &ctx->cache->list, list) {
475 + if (cmd->handle.family != NFPROTO_UNSPEC &&
476 + cmd->handle.family != table->handle.family)
479 + nft_print(ctx->octx, "table %s %s {\n",
480 + family2str(table->handle.family),
481 + table->handle.table);
483 + list_for_each_entry(flowtable, &table->flowtables, list) {
484 + flowtable_print_declaration(flowtable, &opts, ctx->octx);
485 + nft_print(ctx->octx, "%s}%s", opts.tab, opts.nl);
488 + nft_print(ctx->octx, "}\n");
493 static int do_list_ruleset(struct netlink_ctx *ctx, struct cmd *cmd)
495 unsigned int family = cmd->handle.family;
496 @@ -1628,6 +1748,8 @@ static int do_command_list(struct netlin
499 return do_list_obj(ctx, cmd, NFT_OBJECT_LIMIT);
500 + case CMD_OBJ_FLOWTABLES:
501 + return do_list_flowtables(ctx, cmd);
503 BUG("invalid command object type %u\n", cmd->obj);
507 @@ -297,6 +297,8 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr
508 "meter" { return METER; }
509 "meters" { return METERS; }
511 +"flowtables" { return FLOWTABLES; }
513 "counter" { return COUNTER; }
514 "name" { return NAME; }
515 "packets" { return PACKETS; }