libnftnl  1.2.1
nft-set-del.c
1 /*
2  * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
10  */
11 
12 #include <stdlib.h>
13 #include <time.h>
14 #include <string.h>
15 #include <netinet/in.h>
16 
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
19 
20 #include <libmnl/libmnl.h>
21 #include <libnftnl/set.h>
22 
23 int main(int argc, char *argv[])
24 {
25  struct mnl_socket *nl;
26  char buf[MNL_SOCKET_BUFFER_SIZE];
27  struct nlmsghdr *nlh;
28  struct mnl_nlmsg_batch *batch;
29  uint32_t portid, seq, family;
30  struct nftnl_set *t = NULL;
31  int ret;
32 
33  if (argc != 4) {
34  fprintf(stderr, "%s <family> <table> <set>\n", argv[0]);
35  exit(EXIT_FAILURE);
36  }
37 
38  t = nftnl_set_alloc();
39  if (t == NULL) {
40  perror("OOM");
41  exit(EXIT_FAILURE);
42  }
43 
44  seq = time(NULL);
45  if (strcmp(argv[1], "ip") == 0)
46  family = NFPROTO_IPV4;
47  else if (strcmp(argv[1], "ip6") == 0)
48  family = NFPROTO_IPV6;
49  else if (strcmp(argv[1], "inet") == 0)
50  family = NFPROTO_INET;
51  else if (strcmp(argv[1], "bridge") == 0)
52  family = NFPROTO_BRIDGE;
53  else if (strcmp(argv[1], "arp") == 0)
54  family = NFPROTO_ARP;
55  else {
56  fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
57  exit(EXIT_FAILURE);
58  }
59 
60  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
61 
62  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
63  mnl_nlmsg_batch_next(batch);
64 
65  nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
66  NFT_MSG_DELSET, family,
67  NLM_F_ACK, seq);
68  nftnl_set_set_str(t, NFTNL_SET_TABLE, argv[2]);
69  nftnl_set_set_str(t, NFTNL_SET_NAME, argv[3]);
70 
71  nftnl_set_nlmsg_build_payload(nlh, t);
72  nftnl_set_free(t);
73  mnl_nlmsg_batch_next(batch);
74 
75  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
76  mnl_nlmsg_batch_next(batch);
77 
78  nl = mnl_socket_open(NETLINK_NETFILTER);
79  if (nl == NULL) {
80  perror("mnl_socket_open");
81  exit(EXIT_FAILURE);
82  }
83 
84  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
85  perror("mnl_socket_bind");
86  exit(EXIT_FAILURE);
87  }
88  portid = mnl_socket_get_portid(nl);
89 
90  ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
91  mnl_nlmsg_batch_size(batch));
92  if (ret < 0) {
93  perror("mnl_socket_send");
94  exit(EXIT_FAILURE);
95  }
96 
97  mnl_nlmsg_batch_stop(batch);
98 
99  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
100  while (ret < 0) {
101  perror("mnl_socket_recvfrom");
102  exit(EXIT_FAILURE);
103  }
104 
105  ret = mnl_cb_run(buf, ret, 0, portid, NULL, NULL);
106  if (ret < 0) {
107  perror("mnl_cb_run");
108  exit(EXIT_FAILURE);
109  }
110  mnl_socket_close(nl);
111 
112  return EXIT_SUCCESS;
113 }