libnftnl 1.2.6
nft-flowtable-del.c
1#include <stdlib.h>
2#include <time.h>
3#include <string.h>
4#include <netinet/in.h>
5
6#include <linux/netfilter.h>
7#include <linux/netfilter/nf_tables.h>
8
9#include <libmnl/libmnl.h>
10#include <libnftnl/flowtable.h>
11
12static struct nftnl_flowtable *flowtable_del_parse(int argc, char *argv[])
13{
14 struct nftnl_flowtable *t;
15
16 t = nftnl_flowtable_alloc();
17 if (t == NULL) {
18 perror("OOM");
19 return NULL;
20 }
21
22 nftnl_flowtable_set_str(t, NFTNL_FLOWTABLE_TABLE, argv[2]);
23 nftnl_flowtable_set_str(t, NFTNL_FLOWTABLE_NAME, argv[3]);
24
25 return t;
26}
27
28int main(int argc, char *argv[])
29{
30 struct mnl_socket *nl;
31 struct mnl_nlmsg_batch *batch;
32 char buf[MNL_SOCKET_BUFFER_SIZE];
33 struct nlmsghdr *nlh;
34 uint32_t portid, seq, flowtable_seq;
35 struct nftnl_flowtable *t;
36 int ret, family;
37
38 if (argc != 4) {
39 fprintf(stderr, "Usage: %s <family> <table> <flowtable>\n",
40 argv[0]);
41 exit(EXIT_FAILURE);
42 }
43
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], "inet") == 0)
49 family = NFPROTO_INET;
50 else if (strcmp(argv[1], "bridge") == 0)
51 family = NFPROTO_BRIDGE;
52 else if (strcmp(argv[1], "arp") == 0)
53 family = NFPROTO_ARP;
54 else {
55 fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
56 exit(EXIT_FAILURE);
57 }
58
59 t = flowtable_del_parse(argc, argv);
60 if (t == NULL)
61 exit(EXIT_FAILURE);
62
63 seq = time(NULL);
64 batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
65
66 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
67 mnl_nlmsg_batch_next(batch);
68
69 flowtable_seq = seq;
70 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
71 NFT_MSG_DELFLOWTABLE, family,
72 NLM_F_ACK, seq++);
73 nftnl_flowtable_nlmsg_build_payload(nlh, t);
74 nftnl_flowtable_free(t);
75 mnl_nlmsg_batch_next(batch);
76
77 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
78 mnl_nlmsg_batch_next(batch);
79
80 nl = mnl_socket_open(NETLINK_NETFILTER);
81 if (nl == NULL) {
82 perror("mnl_socket_open");
83 exit(EXIT_FAILURE);
84 }
85
86 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
87 perror("mnl_socket_bind");
88 exit(EXIT_FAILURE);
89 }
90 portid = mnl_socket_get_portid(nl);
91
92 if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
93 mnl_nlmsg_batch_size(batch)) < 0) {
94 perror("mnl_socket_send");
95 exit(EXIT_FAILURE);
96 }
97
98 mnl_nlmsg_batch_stop(batch);
99
100 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
101 while (ret > 0) {
102 ret = mnl_cb_run(buf, ret, flowtable_seq, portid, NULL, NULL);
103 if (ret <= 0)
104 break;
105 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
106 }
107 if (ret == -1) {
108 perror("error");
109 exit(EXIT_FAILURE);
110 }
111 mnl_socket_close(nl);
112
113 return EXIT_SUCCESS;
114}