libnftnl  1.1.7
nft-rule-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/rule.h>
22 
23 static int table_cb(const struct nlmsghdr *nlh, void *data)
24 {
25  struct nftnl_rule *t;
26  char buf[4096];
27  uint32_t *type = data;
28 
29  t = nftnl_rule_alloc();
30  if (t == NULL) {
31  perror("OOM");
32  goto err;
33  }
34 
35  if (nftnl_rule_nlmsg_parse(nlh, t) < 0) {
36  perror("nftnl_rule_nlmsg_parse");
37  goto err_free;
38  }
39 
40  nftnl_rule_snprintf(buf, sizeof(buf), t, *type, 0);
41  printf("%s\n", buf);
42 
43 err_free:
44  nftnl_rule_free(t);
45 err:
46  return MNL_CB_OK;
47 }
48 
49 static struct nftnl_rule *setup_rule(uint8_t family, const char *table,
50  const char *chain, const char *handle)
51 {
52  struct nftnl_rule *r;
53  uint64_t handle_num;
54 
55  r = nftnl_rule_alloc();
56  if (r == NULL)
57  return NULL;
58 
59  if (table != NULL)
60  nftnl_rule_set_str(r, NFTNL_RULE_TABLE, table);
61  if (chain != NULL)
62  nftnl_rule_set_str(r, NFTNL_RULE_CHAIN, chain);
63 
64  nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family);
65 
66  if (handle != NULL) {
67  handle_num = atoll(handle);
68  nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, handle_num);
69  }
70 
71  return r;
72 }
73 
74 int main(int argc, char *argv[])
75 {
76  struct mnl_socket *nl;
77  char buf[MNL_SOCKET_BUFFER_SIZE];
78  struct nlmsghdr *nlh;
79  uint32_t portid, seq, type = NFTNL_OUTPUT_DEFAULT;
80  const char *table = NULL, *chain = NULL;
81  struct nftnl_rule *r;
82  int ret, family;
83 
84  if (argc < 2 || argc > 5) {
85  fprintf(stderr, "Usage: %s <family> [<table> <chain>]\n",
86  argv[0]);
87  exit(EXIT_FAILURE);
88  }
89 
90  if (strcmp(argv[1], "ip") == 0)
91  family = NFPROTO_IPV4;
92  else if (strcmp(argv[1], "ip6") == 0)
93  family = NFPROTO_IPV6;
94  else if (strcmp(argv[1], "bridge") == 0)
95  family = NFPROTO_BRIDGE;
96  else if (strcmp(argv[1], "arp") == 0)
97  family = NFPROTO_ARP;
98  else if (strcmp(argv[1], "unspec") == 0)
99  family = NFPROTO_UNSPEC;
100  else {
101  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp, unspec\n");
102  exit(EXIT_FAILURE);
103  }
104 
105  /* at least [<table> <chain>] specified */
106  if (argc >= 4) {
107  table = argv[2];
108  chain = argv[3];
109  }
110 
111  seq = time(NULL);
112  nlh = nftnl_rule_nlmsg_build_hdr(buf, NFT_MSG_GETRULE, family,
113  NLM_F_DUMP, seq);
114 
115  r = setup_rule(family, table, chain, NULL);
116  if (!r) {
117  perror("setup_rule");
118  exit(EXIT_FAILURE);
119  }
120  nftnl_rule_nlmsg_build_payload(nlh, r);
121 
122  nl = mnl_socket_open(NETLINK_NETFILTER);
123  if (nl == NULL) {
124  perror("mnl_socket_open");
125  exit(EXIT_FAILURE);
126  }
127 
128  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
129  perror("mnl_socket_bind");
130  exit(EXIT_FAILURE);
131  }
132  portid = mnl_socket_get_portid(nl);
133 
134  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
135  perror("mnl_socket_send");
136  exit(EXIT_FAILURE);
137  }
138 
139  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
140  while (ret > 0) {
141  ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type);
142  if (ret <= 0)
143  break;
144  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
145  }
146  if (ret == -1) {
147  perror("error");
148  exit(EXIT_FAILURE);
149  }
150  mnl_socket_close(nl);
151 
152  return EXIT_SUCCESS;
153 }