libnftnl  1.2.9
nft-set-get.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4  *
5  * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
6  */
7 
8 #include <stdlib.h>
9 #include <time.h>
10 #include <string.h>
11 #include <netinet/in.h>
12 
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/nf_tables.h>
15 
16 #include <libmnl/libmnl.h>
17 #include <libnftnl/set.h>
18 
19 static int set_cb(const struct nlmsghdr *nlh, void *data)
20 {
21  struct nftnl_set *t;
22  char buf[4096];
23  uint32_t *type = data;
24 
25  t = nftnl_set_alloc();
26  if (t == NULL) {
27  perror("OOM");
28  goto err;
29  }
30 
31  if (nftnl_set_nlmsg_parse(nlh, t) < 0) {
32  perror("nftnl_set_nlmsg_parse");
33  goto err_free;
34  }
35 
36  nftnl_set_snprintf(buf, sizeof(buf), t, *type, 0);
37  printf("%s\n", buf);
38 
39 err_free:
40  nftnl_set_free(t);
41 err:
42  return MNL_CB_OK;
43 }
44 
45 int main(int argc, char *argv[])
46 {
47  struct mnl_socket *nl;
48  char buf[MNL_SOCKET_BUFFER_SIZE];
49  struct nlmsghdr *nlh;
50  uint32_t portid, seq, family;
51  uint32_t type = NFTNL_OUTPUT_DEFAULT;
52  struct nftnl_set *t = NULL;
53  int ret;
54 
55  if (argc < 2 || argc > 3) {
56  fprintf(stderr, "%s <family>\n", argv[0]);
57  return EXIT_FAILURE;
58  }
59  t = nftnl_set_alloc();
60  if (t == NULL) {
61  perror("OOM");
62  exit(EXIT_FAILURE);
63  }
64  seq = time(NULL);
65  if (strcmp(argv[1], "ip") == 0)
66  family = NFPROTO_IPV4;
67  else if (strcmp(argv[1], "ip6") == 0)
68  family = NFPROTO_IPV6;
69  else if (strcmp(argv[1], "inet") == 0)
70  family = NFPROTO_INET;
71  else if (strcmp(argv[1], "bridge") == 0)
72  family = NFPROTO_BRIDGE;
73  else if (strcmp(argv[1], "arp") == 0)
74  family = NFPROTO_ARP;
75  else if (strcmp(argv[1], "unspec") == 0)
76  family = NFPROTO_UNSPEC;
77  else {
78  fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp, unspec\n");
79  exit(EXIT_FAILURE);
80  }
81 
82  nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETSET, family,
83  NLM_F_DUMP | NLM_F_ACK, seq);
84  /* Use this below if you want to obtain sets per table */
85 /* nftnl_set_set(t, NFT_SET_TABLE, argv[2]); */
86  nftnl_set_nlmsg_build_payload(nlh, t);
87  nftnl_set_free(t);
88 
89  nl = mnl_socket_open(NETLINK_NETFILTER);
90  if (nl == NULL) {
91  perror("mnl_socket_open");
92  exit(EXIT_FAILURE);
93  }
94 
95  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
96  perror("mnl_socket_bind");
97  exit(EXIT_FAILURE);
98  }
99  portid = mnl_socket_get_portid(nl);
100 
101  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
102  perror("mnl_socket_send");
103  exit(EXIT_FAILURE);
104  }
105 
106  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
107  while (ret > 0) {
108  ret = mnl_cb_run(buf, ret, seq, portid, set_cb, &type);
109  if (ret <= 0)
110  break;
111  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
112  }
113  if (ret == -1) {
114  perror("error");
115  exit(EXIT_FAILURE);
116  }
117  mnl_socket_close(nl);
118 
119  return EXIT_SUCCESS;
120 }