libnftnl  1.1.5
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 
12 static 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(t, NFTNL_FLOWTABLE_TABLE, argv[2]);
23  nftnl_flowtable_set(t, NFTNL_FLOWTABLE_NAME, argv[3]);
24 
25  return t;
26 }
27 
28 int 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], "bridge") == 0)
49  family = NFPROTO_BRIDGE;
50  else if (strcmp(argv[1], "arp") == 0)
51  family = NFPROTO_ARP;
52  else {
53  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
54  exit(EXIT_FAILURE);
55  }
56 
57  t = flowtable_del_parse(argc, argv);
58  if (t == NULL)
59  exit(EXIT_FAILURE);
60 
61  batching = nftnl_batch_is_supported();
62  if (batching < 0) {
63  perror("cannot talk to nfnetlink");
64  exit(EXIT_FAILURE);
65  }
66 
67  seq = time(NULL);
68  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
69 
70  if (batching) {
71  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
72  mnl_nlmsg_batch_next(batch);
73  }
74 
75  flowtable_seq = seq;
76  nlh = nftnl_flowtable_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
77  NFT_MSG_DELFLOWTABLE, family,
78  NLM_F_ACK, seq++);
79  nftnl_flowtable_nlmsg_build_payload(nlh, t);
80  nftnl_flowtable_free(t);
81  mnl_nlmsg_batch_next(batch);
82 
83  if (batching) {
84  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
85  mnl_nlmsg_batch_next(batch);
86  }
87 
88  nl = mnl_socket_open(NETLINK_NETFILTER);
89  if (nl == NULL) {
90  perror("mnl_socket_open");
91  exit(EXIT_FAILURE);
92  }
93 
94  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
95  perror("mnl_socket_bind");
96  exit(EXIT_FAILURE);
97  }
98  portid = mnl_socket_get_portid(nl);
99 
100  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
101  mnl_nlmsg_batch_size(batch)) < 0) {
102  perror("mnl_socket_send");
103  exit(EXIT_FAILURE);
104  }
105 
106  mnl_nlmsg_batch_stop(batch);
107 
108  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
109  while (ret > 0) {
110  ret = mnl_cb_run(buf, ret, flowtable_seq, portid, NULL, NULL);
111  if (ret <= 0)
112  break;
113  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
114  }
115  if (ret == -1) {
116  perror("error");
117  exit(EXIT_FAILURE);
118  }
119  mnl_socket_close(nl);
120 
121  return EXIT_SUCCESS;
122 }