libnftnl  1.2.6
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  char buf[MNL_SOCKET_BUFFER_SIZE];
26  struct mnl_nlmsg_batch *batch;
27  uint32_t portid, seq, family;
28  struct nftnl_set_elem *e;
29  struct mnl_socket *nl;
30  struct nlmsghdr *nlh;
31  struct nftnl_set *s;
32  uint16_t data;
33  int ret;
34 
35  if (argc != 4) {
36  fprintf(stderr, "%s <family> <table> <set>\n", argv[0]);
37  exit(EXIT_FAILURE);
38  }
39 
40  s = nftnl_set_alloc();
41  if (s == NULL) {
42  perror("OOM");
43  exit(EXIT_FAILURE);
44  }
45 
46  seq = time(NULL);
47  if (strcmp(argv[1], "ip") == 0)
48  family = NFPROTO_IPV4;
49  else if (strcmp(argv[1], "ip6") == 0)
50  family = NFPROTO_IPV6;
51  else if (strcmp(argv[1], "inet") == 0)
52  family = NFPROTO_INET;
53  else if (strcmp(argv[1], "bridge") == 0)
54  family = NFPROTO_BRIDGE;
55  else if (strcmp(argv[1], "arp") == 0)
56  family = NFPROTO_ARP;
57  else {
58  fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
59  exit(EXIT_FAILURE);
60  }
61 
62  nftnl_set_set_str(s, NFTNL_SET_TABLE, argv[2]);
63  nftnl_set_set_str(s, NFTNL_SET_NAME, argv[3]);
64 
65  /* Add to dummy elements to set */
66  e = nftnl_set_elem_alloc();
67  if (e == NULL) {
68  perror("OOM");
69  exit(EXIT_FAILURE);
70  }
71 
72  data = 0x1;
73  nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &data, sizeof(data));
74  nftnl_set_elem_add(s, e);
75 
76  e = nftnl_set_elem_alloc();
77  if (e == NULL) {
78  perror("OOM");
79  exit(EXIT_FAILURE);
80  }
81  data = 0x2;
82  nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &data, sizeof(data));
83  nftnl_set_elem_add(s, e);
84 
85  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
86 
87  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
88  mnl_nlmsg_batch_next(batch);
89 
90  nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
91  NFT_MSG_DELSETELEM, family, NLM_F_ACK, seq);
92  nftnl_set_elems_nlmsg_build_payload(nlh, s);
93  nftnl_set_free(s);
94  mnl_nlmsg_batch_next(batch);
95 
96  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
97  mnl_nlmsg_batch_next(batch);
98 
99  nl = mnl_socket_open(NETLINK_NETFILTER);
100  if (nl == NULL) {
101  perror("mnl_socket_open");
102  exit(EXIT_FAILURE);
103  }
104 
105  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
106  perror("mnl_socket_bind");
107  exit(EXIT_FAILURE);
108  }
109  portid = mnl_socket_get_portid(nl);
110 
111  ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
112  mnl_nlmsg_batch_size(batch));
113  if (ret == -1) {
114  perror("mnl_socket_sendto");
115  exit(EXIT_FAILURE);
116  }
117 
118  mnl_nlmsg_batch_stop(batch);
119 
120  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
121  while (ret > 0) {
122  ret = mnl_cb_run(buf, ret, 0, portid, NULL, NULL);
123  if (ret <= 0)
124  break;
125  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
126  }
127  if (ret == -1) {
128  perror("error");
129  exit(EXIT_FAILURE);
130  }
131  mnl_socket_close(nl);
132 
133  return EXIT_SUCCESS;
134 }