libnftnl 1.2.4
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, batching;
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 batching = nftnl_batch_is_supported();
64 if (batching < 0) {
65 perror("cannot talk to nfnetlink");
66 exit(EXIT_FAILURE);
67 }
68
69 seq = time(NULL);
70 batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
71
72 if (batching) {
73 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
74 mnl_nlmsg_batch_next(batch);
75 }
76
77 flowtable_seq = seq;
78 nlh = nftnl_flowtable_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
79 NFT_MSG_DELFLOWTABLE, family,
80 NLM_F_ACK, seq++);
81 nftnl_flowtable_nlmsg_build_payload(nlh, t);
82 nftnl_flowtable_free(t);
83 mnl_nlmsg_batch_next(batch);
84
85 if (batching) {
86 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
87 mnl_nlmsg_batch_next(batch);
88 }
89
90 nl = mnl_socket_open(NETLINK_NETFILTER);
91 if (nl == NULL) {
92 perror("mnl_socket_open");
93 exit(EXIT_FAILURE);
94 }
95
96 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
97 perror("mnl_socket_bind");
98 exit(EXIT_FAILURE);
99 }
100 portid = mnl_socket_get_portid(nl);
101
102 if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
103 mnl_nlmsg_batch_size(batch)) < 0) {
104 perror("mnl_socket_send");
105 exit(EXIT_FAILURE);
106 }
107
108 mnl_nlmsg_batch_stop(batch);
109
110 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
111 while (ret > 0) {
112 ret = mnl_cb_run(buf, ret, flowtable_seq, portid, NULL, NULL);
113 if (ret <= 0)
114 break;
115 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
116 }
117 if (ret == -1) {
118 perror("error");
119 exit(EXIT_FAILURE);
120 }
121 mnl_socket_close(nl);
122
123 return EXIT_SUCCESS;
124}