libnftnl  1.1.9
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_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
91  NFT_MSG_DELSETELEM, family,
92  NLM_F_ACK, seq);
93  nftnl_set_elems_nlmsg_build_payload(nlh, s);
94  nftnl_set_free(s);
95  mnl_nlmsg_batch_next(batch);
96 
97  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
98  mnl_nlmsg_batch_next(batch);
99 
100  nl = mnl_socket_open(NETLINK_NETFILTER);
101  if (nl == NULL) {
102  perror("mnl_socket_open");
103  exit(EXIT_FAILURE);
104  }
105 
106  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
107  perror("mnl_socket_bind");
108  exit(EXIT_FAILURE);
109  }
110  portid = mnl_socket_get_portid(nl);
111 
112  ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
113  mnl_nlmsg_batch_size(batch));
114  if (ret == -1) {
115  perror("mnl_socket_sendto");
116  exit(EXIT_FAILURE);
117  }
118 
119  mnl_nlmsg_batch_stop(batch);
120 
121  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
122  while (ret > 0) {
123  ret = mnl_cb_run(buf, ret, 0, portid, NULL, NULL);
124  if (ret <= 0)
125  break;
126  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
127  }
128  if (ret == -1) {
129  perror("error");
130  exit(EXIT_FAILURE);
131  }
132  mnl_socket_close(nl);
133 
134  return EXIT_SUCCESS;
135 }