6#include <linux/netfilter.h>
7#include <linux/netfilter/nf_tables.h>
9#include <libmnl/libmnl.h>
10#include <libnftnl/flowtable.h>
12static int table_cb(
const struct nlmsghdr *nlh,
void *data)
16 uint32_t *type = data;
18 t = nftnl_flowtable_alloc();
24 if (nftnl_flowtable_nlmsg_parse(nlh, t) < 0) {
25 perror(
"nftnl_flowtable_nlmsg_parse");
29 nftnl_flowtable_snprintf(buf,
sizeof(buf), t, *type, 0);
33 nftnl_flowtable_free(t);
38int main(
int argc,
char *argv[])
40 struct mnl_socket *nl;
41 char buf[MNL_SOCKET_BUFFER_SIZE];
43 uint32_t portid, seq, type = NFTNL_OUTPUT_DEFAULT;
49 if (argc < 2 || argc > 5) {
50 fprintf(stderr,
"Usage: %s <family> [<table> <flowtable>]\n",
55 if (strcmp(argv[1],
"ip") == 0)
56 family = NFPROTO_IPV4;
57 else if (strcmp(argv[1],
"ip6") == 0)
58 family = NFPROTO_IPV6;
59 else if (strcmp(argv[1],
"inet") == 0)
60 family = NFPROTO_INET;
61 else if (strcmp(argv[1],
"bridge") == 0)
62 family = NFPROTO_BRIDGE;
63 else if (strcmp(argv[1],
"arp") == 0)
65 else if (strcmp(argv[1],
"unspec") == 0)
66 family = NFPROTO_UNSPEC;
68 fprintf(stderr,
"Unknown family: ip, ip6, inet, bridge, arp, unspec\n");
73 t = nftnl_flowtable_alloc();
78 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETFLOWTABLE, family,
80 nftnl_flowtable_set_str(t, NFTNL_FLOWTABLE_TABLE, argv[2]);
81 nftnl_flowtable_set_str(t, NFTNL_FLOWTABLE_NAME, argv[3]);
82 nftnl_flowtable_nlmsg_build_payload(nlh, t);
83 nftnl_flowtable_free(t);
84 }
else if (argc >= 2) {
85 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETFLOWTABLE, family,
89 nl = mnl_socket_open(NETLINK_NETFILTER);
91 perror(
"mnl_socket_open");
95 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
96 perror(
"mnl_socket_bind");
99 portid = mnl_socket_get_portid(nl);
101 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
102 perror(
"mnl_socket_send");
106 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
108 ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type);
111 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
117 mnl_socket_close(nl);