libnftnl 1.2.6
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
23static 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
43err_free:
44 nftnl_rule_free(t);
45err:
46 return MNL_CB_OK;
47}
48
49static 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
74int 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], "inet") == 0)
95 family = NFPROTO_INET;
96 else if (strcmp(argv[1], "bridge") == 0)
97 family = NFPROTO_BRIDGE;
98 else if (strcmp(argv[1], "arp") == 0)
99 family = NFPROTO_ARP;
100 else if (strcmp(argv[1], "unspec") == 0)
101 family = NFPROTO_UNSPEC;
102 else {
103 fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp, unspec\n");
104 exit(EXIT_FAILURE);
105 }
106
107 /* at least [<table> <chain>] specified */
108 if (argc >= 4) {
109 table = argv[2];
110 chain = argv[3];
111 }
112
113 seq = time(NULL);
114 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETRULE, family,
115 NLM_F_DUMP, seq);
116
117 r = setup_rule(family, table, chain, NULL);
118 if (!r) {
119 perror("setup_rule");
120 exit(EXIT_FAILURE);
121 }
122 nftnl_rule_nlmsg_build_payload(nlh, r);
123
124 nl = mnl_socket_open(NETLINK_NETFILTER);
125 if (nl == NULL) {
126 perror("mnl_socket_open");
127 exit(EXIT_FAILURE);
128 }
129
130 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
131 perror("mnl_socket_bind");
132 exit(EXIT_FAILURE);
133 }
134 portid = mnl_socket_get_portid(nl);
135
136 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
137 perror("mnl_socket_send");
138 exit(EXIT_FAILURE);
139 }
140
141 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
142 while (ret > 0) {
143 ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type);
144 if (ret <= 0)
145 break;
146 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
147 }
148 if (ret == -1) {
149 perror("error");
150 exit(EXIT_FAILURE);
151 }
152 mnl_socket_close(nl);
153
154 return EXIT_SUCCESS;
155}