libnftnl 1.2.6
nft-chain-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/chain.h>
22
23static int table_cb(const struct nlmsghdr *nlh, void *data)
24{
25 struct nftnl_chain *t;
26 char buf[4096];
27 uint32_t *type = data;
28
29 t = nftnl_chain_alloc();
30 if (t == NULL) {
31 perror("OOM");
32 goto err;
33 }
34
35 if (nftnl_chain_nlmsg_parse(nlh, t) < 0) {
36 perror("nftnl_chain_nlmsg_parse");
37 goto err_free;
38 }
39
40 nftnl_chain_snprintf(buf, sizeof(buf), t, *type, 0);
41 printf("%s\n", buf);
42
43err_free:
44 nftnl_chain_free(t);
45err:
46 return MNL_CB_OK;
47}
48
49int 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, type = NFTNL_OUTPUT_DEFAULT;
55 struct nftnl_chain *t = NULL;
56 int ret, family;
57
58 seq = time(NULL);
59
60 if (argc < 2 || argc > 5) {
61 fprintf(stderr, "Usage: %s <family> [<table> <chain>]\n",
62 argv[0]);
63 exit(EXIT_FAILURE);
64 }
65
66 if (strcmp(argv[1], "ip") == 0)
67 family = NFPROTO_IPV4;
68 else if (strcmp(argv[1], "ip6") == 0)
69 family = NFPROTO_IPV6;
70 else if (strcmp(argv[1], "inet") == 0)
71 family = NFPROTO_INET;
72 else if (strcmp(argv[1], "bridge") == 0)
73 family = NFPROTO_BRIDGE;
74 else if (strcmp(argv[1], "arp") == 0)
75 family = NFPROTO_ARP;
76 else if (strcmp(argv[1], "unspec") == 0)
77 family = NFPROTO_UNSPEC;
78 else {
79 fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp, unspec\n");
80 exit(EXIT_FAILURE);
81 }
82
83 if (argc >= 4) {
84 t = nftnl_chain_alloc();
85 if (t == NULL) {
86 perror("OOM");
87 exit(EXIT_FAILURE);
88 }
89 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETCHAIN, family,
90 NLM_F_ACK, seq);
91 nftnl_chain_set_str(t, NFTNL_CHAIN_TABLE, argv[2]);
92 nftnl_chain_set_str(t, NFTNL_CHAIN_NAME, argv[3]);
93 nftnl_chain_nlmsg_build_payload(nlh, t);
94 nftnl_chain_free(t);
95 } else if (argc >= 2) {
96 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETCHAIN, family,
97 NLM_F_DUMP, seq);
98 }
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 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
113 perror("mnl_socket_send");
114 exit(EXIT_FAILURE);
115 }
116
117 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
118 while (ret > 0) {
119 ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type);
120 if (ret <= 0)
121 break;
122 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
123 }
124 if (ret == -1) {
125 perror("error");
126 exit(EXIT_FAILURE);
127 }
128 mnl_socket_close(nl);
129
130 return EXIT_SUCCESS;
131}