libnftnl  1.1.7
nft-flowtable-get.c
1 #include <stdlib.h>
2 #include <time.h>
3 #include <string.h>
4 #include <netinet/in.h>
5 
6 #include <linux/netfilter.h>
7 #include <linux/netfilter/nf_tables.h>
8 
9 #include <libmnl/libmnl.h>
10 #include <libnftnl/flowtable.h>
11 
12 static int table_cb(const struct nlmsghdr *nlh, void *data)
13 {
14  struct nftnl_flowtable *t;
15  char buf[4096];
16  uint32_t *type = data;
17 
18  t = nftnl_flowtable_alloc();
19  if (t == NULL) {
20  perror("OOM");
21  goto err;
22  }
23 
24  if (nftnl_flowtable_nlmsg_parse(nlh, t) < 0) {
25  perror("nftnl_flowtable_nlmsg_parse");
26  goto err_free;
27  }
28 
29  nftnl_flowtable_snprintf(buf, sizeof(buf), t, *type, 0);
30  printf("%s\n", buf);
31 
32 err_free:
33  nftnl_flowtable_free(t);
34 err:
35  return MNL_CB_OK;
36 }
37 
38 int main(int argc, char *argv[])
39 {
40  struct mnl_socket *nl;
41  char buf[MNL_SOCKET_BUFFER_SIZE];
42  struct nlmsghdr *nlh;
43  uint32_t portid, seq, type = NFTNL_OUTPUT_DEFAULT;
44  struct nftnl_flowtable *t = NULL;
45  int ret, family;
46 
47  seq = time(NULL);
48 
49  if (argc < 2 || argc > 5) {
50  fprintf(stderr, "Usage: %s <family> [<table> <flowtable>]\n",
51  argv[0]);
52  exit(EXIT_FAILURE);
53  }
54 
55  if (strcmp(argv[1], "ip") == 0)
56  family = NFPROTO_IPV4;
57  else if (strcmp(argv[1], "ip6") == 0)
58  family = NFPROTO_IPV6;
59  else if (strcmp(argv[1], "bridge") == 0)
60  family = NFPROTO_BRIDGE;
61  else if (strcmp(argv[1], "arp") == 0)
62  family = NFPROTO_ARP;
63  else if (strcmp(argv[1], "unspec") == 0)
64  family = NFPROTO_UNSPEC;
65  else {
66  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp, unspec\n");
67  exit(EXIT_FAILURE);
68  }
69 
70  if (argc >= 4) {
71  t = nftnl_flowtable_alloc();
72  if (t == NULL) {
73  perror("OOM");
74  exit(EXIT_FAILURE);
75  }
76  nlh = nftnl_flowtable_nlmsg_build_hdr(buf, NFT_MSG_GETFLOWTABLE, family,
77  NLM_F_ACK, seq);
78  nftnl_flowtable_set_str(t, NFTNL_FLOWTABLE_TABLE, argv[2]);
79  nftnl_flowtable_set_str(t, NFTNL_FLOWTABLE_NAME, argv[3]);
80  nftnl_flowtable_nlmsg_build_payload(nlh, t);
81  nftnl_flowtable_free(t);
82  } else if (argc >= 2) {
83  nlh = nftnl_flowtable_nlmsg_build_hdr(buf, NFT_MSG_GETFLOWTABLE, family,
84  NLM_F_DUMP, seq);
85  }
86 
87  nl = mnl_socket_open(NETLINK_NETFILTER);
88  if (nl == NULL) {
89  perror("mnl_socket_open");
90  exit(EXIT_FAILURE);
91  }
92 
93  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
94  perror("mnl_socket_bind");
95  exit(EXIT_FAILURE);
96  }
97  portid = mnl_socket_get_portid(nl);
98 
99  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
100  perror("mnl_socket_send");
101  exit(EXIT_FAILURE);
102  }
103 
104  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
105  while (ret > 0) {
106  ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type);
107  if (ret <= 0)
108  break;
109  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
110  }
111  if (ret == -1) {
112  perror("error");
113  exit(EXIT_FAILURE);
114  }
115  mnl_socket_close(nl);
116 
117  return EXIT_SUCCESS;
118 }