15 #include <netinet/in.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
20 #include <libmnl/libmnl.h>
21 #include <libnftnl/table.h>
23 int main(
int argc,
char *argv[])
25 struct mnl_socket *nl;
26 char buf[MNL_SOCKET_BUFFER_SIZE];
28 uint32_t portid, seq, family, flags;
33 fprintf(stderr,
"%s <family> <name> <state>\n", argv[0]);
37 t = nftnl_table_alloc();
44 if (strcmp(argv[1],
"ip") == 0)
45 family = NFPROTO_IPV4;
46 else if (strcmp(argv[1],
"ip6") == 0)
47 family = NFPROTO_IPV6;
48 else if (strcmp(argv[1],
"bridge") == 0)
49 family = NFPROTO_BRIDGE;
50 else if (strcmp(argv[1],
"arp") == 0)
53 fprintf(stderr,
"Unknown family: ip, ip6, bridge, arp\n");
57 if (strcmp(argv[3],
"active") == 0)
59 else if (strcmp(argv[3],
"dormant") == 0)
60 flags = NFT_TABLE_F_DORMANT;
62 fprintf(stderr,
"Unknown state: active, dormant\n");
66 nftnl_table_set(t, NFTNL_TABLE_NAME, argv[2]);
67 nftnl_table_set_u32(t, NFTNL_TABLE_FLAGS, flags);
69 nlh = nftnl_table_nlmsg_build_hdr(buf, NFT_MSG_NEWTABLE, family,
71 nftnl_table_nlmsg_build_payload(nlh, t);
74 nl = mnl_socket_open(NETLINK_NETFILTER);
76 perror(
"mnl_socket_open");
80 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
81 perror(
"mnl_socket_bind");
84 portid = mnl_socket_get_portid(nl);
86 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
87 perror(
"mnl_socket_send");
91 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
93 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
96 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
102 mnl_socket_close(nl);