libnftnl  1.1.7
nft-set-elem-del.c
1 /*
2  * (C) 2013 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  uint32_t portid, seq, family, data;
29  struct nftnl_set *s;
30  struct nftnl_set_elem *e;
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  s = nftnl_set_alloc();
39  if (s == 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], "bridge") == 0)
50  family = NFPROTO_BRIDGE;
51  else if (strcmp(argv[1], "arp") == 0)
52  family = NFPROTO_ARP;
53  else {
54  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
55  exit(EXIT_FAILURE);
56  }
57 
58  nftnl_set_set_str(s, NFTNL_SET_TABLE, argv[2]);
59  nftnl_set_set_str(s, NFTNL_SET_NAME, argv[3]);
60 
61  /* Add to dummy elements to set */
62  e = nftnl_set_elem_alloc();
63  if (e == NULL) {
64  perror("OOM");
65  exit(EXIT_FAILURE);
66  }
67 
68  data = 0x1;
69  nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &data, sizeof(data));
70  nftnl_set_elem_add(s, e);
71 
72  e = nftnl_set_elem_alloc();
73  if (e == NULL) {
74  perror("OOM");
75  exit(EXIT_FAILURE);
76  }
77  data = 0x2;
78  nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &data, sizeof(data));
79  nftnl_set_elem_add(s, e);
80 
81  nlh = nftnl_set_nlmsg_build_hdr(buf, NFT_MSG_DELSETELEM, family,
82  NLM_F_ACK, seq);
83  nftnl_set_elems_nlmsg_build_payload(nlh, s);
84  nftnl_set_free(s);
85 
86  nl = mnl_socket_open(NETLINK_NETFILTER);
87  if (nl == NULL) {
88  perror("mnl_socket_open");
89  exit(EXIT_FAILURE);
90  }
91 
92  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
93  perror("mnl_socket_bind");
94  exit(EXIT_FAILURE);
95  }
96  portid = mnl_socket_get_portid(nl);
97 
98  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
99  perror("mnl_socket_send");
100  exit(EXIT_FAILURE);
101  }
102 
103  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
104  while (ret > 0) {
105  ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
106  if (ret <= 0)
107  break;
108  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
109  }
110  if (ret == -1) {
111  perror("error");
112  exit(EXIT_FAILURE);
113  }
114  mnl_socket_close(nl);
115 
116  return EXIT_SUCCESS;
117 }