1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/*
* Copyright (c) 2013 Arturo Borrero Gonzalez <arturo@debian.org>
*
* based on previous code from:
*
* Copyright (c) 2013 Pablo Neira Ayuso <pablo@netfilter.org>
*
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#include <linux/netfilter.h>
#include <linux/netfilter/nf_tables.h>
#include <libmnl/libmnl.h>
#include <libnftnl/common.h>
#include <libnftnl/ruleset.h>
#include <libnftnl/table.h>
#include <libnftnl/chain.h>
#include <libnftnl/set.h>
#include <libnftnl/rule.h>
static int seq;
static void memory_allocation_error(void)
{
perror("OOM");
exit(EXIT_FAILURE);
}
static int
mnl_talk(struct mnl_socket *nf_sock, const void *data, unsigned int len,
int (*cb)(const struct nlmsghdr *nlh, void *data), void *cb_data)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
uint32_t portid = mnl_socket_get_portid(nf_sock);
int ret;
if (mnl_socket_sendto(nf_sock, data, len) < 0)
return -1;
ret = mnl_socket_recvfrom(nf_sock, buf, sizeof(buf));
while (ret > 0) {
ret = mnl_cb_run(buf, ret, seq, portid, cb, cb_data);
if (ret <= 0)
goto out;
ret = mnl_socket_recvfrom(nf_sock, buf, sizeof(buf));
}
out:
if (ret < 0 && errno == EAGAIN)
return 0;
return ret;
}
/*
* Rule
*/
static int rule_cb(const struct nlmsghdr *nlh, void *data)
{
struct nftnl_rule_list *nlr_list = data;
struct nftnl_rule *r;
r = nftnl_rule_alloc();
if (r == NULL)
memory_allocation_error();
if (nftnl_rule_nlmsg_parse(nlh, r) < 0)
goto err_free;
nftnl_rule_list_add_tail(r, nlr_list);
return MNL_CB_OK;
err_free:
nftnl_rule_free(r);
return MNL_CB_OK;
}
static struct nftnl_rule_list *mnl_rule_dump(struct mnl_socket *nf_sock,
int family)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
struct nftnl_rule_list *nlr_list;
int ret;
nlr_list = nftnl_rule_list_alloc();
if (nlr_list == NULL)
memory_allocation_error();
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETRULE, family,
NLM_F_DUMP, seq);
ret = mnl_talk(nf_sock, nlh, nlh->nlmsg_len, rule_cb, nlr_list);
if (ret < 0)
goto err;
return nlr_list;
err:
nftnl_rule_list_free(nlr_list);
return NULL;
}
/*
* Chain
*/
static int chain_cb(const struct nlmsghdr *nlh, void *data)
{
struct nftnl_chain_list *nlc_list = data;
struct nftnl_chain *c;
c = nftnl_chain_alloc();
if (c == NULL)
memory_allocation_error();
if (nftnl_chain_nlmsg_parse(nlh, c) < 0)
goto err_free;
nftnl_chain_list_add_tail(c, nlc_list);
return MNL_CB_OK;
err_free:
nftnl_chain_free(c);
return MNL_CB_OK;
}
static struct nftnl_chain_list *mnl_chain_dump(struct mnl_socket *nf_sock,
int family)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
struct nftnl_chain_list *nlc_list;
int ret;
nlc_list = nftnl_chain_list_alloc();
if (nlc_list == NULL)
memory_allocation_error();
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETCHAIN, family,
NLM_F_DUMP, seq);
ret = mnl_talk(nf_sock, nlh, nlh->nlmsg_len, chain_cb, nlc_list);
if (ret < 0)
goto err;
return nlc_list;
err:
nftnl_chain_list_free(nlc_list);
return NULL;
}
/*
* Table
*/
static int table_cb(const struct nlmsghdr *nlh, void *data)
{
struct nftnl_table_list *nlt_list = data;
struct nftnl_table *t;
t = nftnl_table_alloc();
if (t == NULL)
memory_allocation_error();
if (nftnl_table_nlmsg_parse(nlh, t) < 0)
goto err_free;
nftnl_table_list_add_tail(t, nlt_list);
return MNL_CB_OK;
err_free:
nftnl_table_free(t);
return MNL_CB_OK;
}
static struct nftnl_table_list *mnl_table_dump(struct mnl_socket *nf_sock,
int family)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
struct nftnl_table_list *nlt_list;
int ret;
nlt_list = nftnl_table_list_alloc();
if (nlt_list == NULL)
memory_allocation_error();
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family,
NLM_F_DUMP, seq);
ret = mnl_talk(nf_sock, nlh, nlh->nlmsg_len, table_cb, nlt_list);
if (ret < 0)
goto err;
return nlt_list;
err:
nftnl_table_list_free(nlt_list);
return NULL;
}
/*
* Set elements
*/
static int set_elem_cb(const struct nlmsghdr *nlh, void *data)
{
nftnl_set_elems_nlmsg_parse(nlh, data);
return MNL_CB_OK;
}
static int mnl_setelem_get(struct mnl_socket *nf_sock, struct nftnl_set *nls)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
uint32_t family = nftnl_set_get_u32(nls, NFTNL_SET_FAMILY);
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETSETELEM, family,
NLM_F_DUMP | NLM_F_ACK, seq);
nftnl_set_nlmsg_build_payload(nlh, nls);
return mnl_talk(nf_sock, nlh, nlh->nlmsg_len, set_elem_cb, nls);
}
/*
* Set
*/
static int set_cb(const struct nlmsghdr *nlh, void *data)
{
struct nftnl_set_list *nls_list = data;
struct nftnl_set *s;
s = nftnl_set_alloc();
if (s == NULL)
memory_allocation_error();
if (nftnl_set_nlmsg_parse(nlh, s) < 0)
goto err_free;
nftnl_set_list_add_tail(s, nls_list);
return MNL_CB_OK;
err_free:
nftnl_set_free(s);
return MNL_CB_OK;
}
static struct nftnl_set_list *
mnl_set_dump(struct mnl_socket *nf_sock, int family)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
struct nftnl_set *s;
struct nftnl_set_list *nls_list;
struct nftnl_set *si;
struct nftnl_set_list_iter *i;
int ret;
s = nftnl_set_alloc();
if (s == NULL)
memory_allocation_error();
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETSET, family,
NLM_F_DUMP | NLM_F_ACK, seq);
nftnl_set_nlmsg_build_payload(nlh, s);
nftnl_set_free(s);
nls_list = nftnl_set_list_alloc();
if (nls_list == NULL)
memory_allocation_error();
ret = mnl_talk(nf_sock, nlh, nlh->nlmsg_len, set_cb, nls_list);
if (ret < 0)
goto err;
i = nftnl_set_list_iter_create(nls_list);
if (i == NULL)
memory_allocation_error();
si = nftnl_set_list_iter_next(i);
while (si != NULL) {
if (mnl_setelem_get(nf_sock, si) != 0) {
perror("E: Unable to get set elements");
nftnl_set_list_iter_destroy(i);
goto err;
}
si = nftnl_set_list_iter_next(i);
}
nftnl_set_list_iter_destroy(i);
return nls_list;
err:
nftnl_set_list_free(nls_list);
return NULL;
}
/*
* ruleset
*/
static struct nftnl_ruleset *mnl_ruleset_dump(struct mnl_socket *nf_sock)
{
struct nftnl_ruleset *rs;
struct nftnl_table_list *t;
struct nftnl_chain_list *c;
struct nftnl_set_list *s;
struct nftnl_rule_list *r;
rs = nftnl_ruleset_alloc();
if (rs == NULL)
memory_allocation_error();
t = mnl_table_dump(nf_sock, NFPROTO_UNSPEC);
if (t != NULL)
nftnl_ruleset_set(rs, NFTNL_RULESET_TABLELIST, t);
c = mnl_chain_dump(nf_sock, NFPROTO_UNSPEC);
if (c != NULL)
nftnl_ruleset_set(rs, NFTNL_RULESET_CHAINLIST, c);
s = mnl_set_dump(nf_sock, NFPROTO_UNSPEC);
if (s != NULL)
nftnl_ruleset_set(rs, NFTNL_RULESET_SETLIST, s);
r = mnl_rule_dump(nf_sock, NFPROTO_UNSPEC);
if (r != NULL)
nftnl_ruleset_set(rs, NFTNL_RULESET_RULELIST, r);
return rs;
}
int main(int argc, char *argv[])
{
struct mnl_socket *nl;
uint32_t type = NFTNL_OUTPUT_DEFAULT;
struct nftnl_ruleset *rs;
int ret;
if (argc > 2) {
fprintf(stderr, "%s\n", argv[0]);
exit(EXIT_FAILURE);
}
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
perror("mnl_socket_open");
exit(EXIT_FAILURE);
}
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
perror("mnl_socket_bind");
exit(EXIT_FAILURE);
}
seq = time(NULL);
rs = mnl_ruleset_dump(nl);
if (rs == NULL) {
perror("ruleset_dump");
exit(EXIT_FAILURE);
}
ret = nftnl_ruleset_fprintf(stdout, rs, type, 0);
fprintf(stdout, "\n");
if (ret == -1)
perror("E: Error during fprintf operations");
return 0;
}
|