libnftnl  1.0.8
nft-table-get.c
1 /*
2  * (C) 2012 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/table.h>
22 
23 static int table_cb(const struct nlmsghdr *nlh, void *data)
24 {
25  struct nftnl_table *t;
26  char buf[4096];
27  uint32_t *type = data;
28 
29  t = nftnl_table_alloc();
30  if (t == NULL) {
31  perror("OOM");
32  goto err;
33  }
34 
35  if (nftnl_table_nlmsg_parse(nlh, t) < 0) {
36  perror("nftnl_table_nlmsg_parse");
37  goto err_free;
38  }
39 
40  nftnl_table_snprintf(buf, sizeof(buf), t, *type, 0);
41  printf("%s\n", buf);
42 
43 err_free:
44  nftnl_table_free(t);
45 err:
46  return MNL_CB_OK;
47 }
48 
49 int main(int argc, char *argv[])
50 {
51  struct mnl_socket *nl;
52  char buf[MNL_SOCKET_BUFFER_SIZE];
53  struct nlmsghdr *nlh;
54  uint32_t portid, seq, family;
55  struct nftnl_table *t = NULL;
56  int ret;
57  uint32_t type = NFTNL_OUTPUT_DEFAULT;
58 
59  if (argc < 2 || argc > 4) {
60  fprintf(stderr, "%s <family> [<table>] [<default|json>]\n",
61  argv[0]);
62  return EXIT_FAILURE;
63  }
64 
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], "bridge") == 0)
70  family = NFPROTO_BRIDGE;
71  else if (strcmp(argv[1], "arp") == 0)
72  family = NFPROTO_ARP;
73  else if (strcmp(argv[1], "unspec") == 0)
74  family = NFPROTO_UNSPEC;
75  else {
76  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp, unspec\n");
77  exit(EXIT_FAILURE);
78  }
79 
80  if (strcmp(argv[argc-1], "json") == 0) {
81  type = NFTNL_OUTPUT_JSON;
82  argv[argc-1] = NULL;
83  argc--;
84  } else if (strcmp(argv[argc - 1], "default") == 0) {
85  argc--;
86  }
87 
88  if (argc == 3) {
89  t = nftnl_table_alloc();
90  if (t == NULL) {
91  perror("OOM");
92  exit(EXIT_FAILURE);
93  }
94  }
95 
96  seq = time(NULL);
97  if (t == NULL) {
98  nlh = nftnl_table_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family,
99  NLM_F_DUMP, seq);
100  } else {
101  nlh = nftnl_table_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family,
102  NLM_F_ACK, seq);
103  nftnl_table_set(t, NFTNL_TABLE_NAME, argv[2]);
104  nftnl_table_nlmsg_build_payload(nlh, t);
105  nftnl_table_free(t);
106  }
107 
108  nl = mnl_socket_open(NETLINK_NETFILTER);
109  if (nl == NULL) {
110  perror("mnl_socket_open");
111  exit(EXIT_FAILURE);
112  }
113 
114  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
115  perror("mnl_socket_bind");
116  exit(EXIT_FAILURE);
117  }
118  portid = mnl_socket_get_portid(nl);
119 
120  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
121  perror("mnl_socket_send");
122  exit(EXIT_FAILURE);
123  }
124 
125  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
126  while (ret > 0) {
127  ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type);
128  if (ret <= 0)
129  break;
130  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
131  }
132  if (ret == -1) {
133  perror("error");
134  exit(EXIT_FAILURE);
135  }
136  mnl_socket_close(nl);
137 
138  return EXIT_SUCCESS;
139 }