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
|
/*
* (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
* (C) 2013 by Arturo Borrero Gonzalez <arturo@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* 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 <internal.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <errno.h>
#include <inttypes.h>
#include <libnftnl/common.h>
#include <linux/netfilter.h>
#include <linux/netfilter/nf_tables.h>
static const char *const nftnl_family_str[NFPROTO_NUMPROTO] = {
[NFPROTO_INET] = "inet",
[NFPROTO_IPV4] = "ip",
[NFPROTO_ARP] = "arp",
[NFPROTO_NETDEV] = "netdev",
[NFPROTO_BRIDGE] = "bridge",
[NFPROTO_IPV6] = "ip6",
};
const char *nftnl_family2str(uint32_t family)
{
if (family >= NFPROTO_NUMPROTO || !nftnl_family_str[family])
return "unknown";
return nftnl_family_str[family];
}
int nftnl_str2family(const char *family)
{
int i;
for (i = 0; i < NFPROTO_NUMPROTO; i++) {
if (nftnl_family_str[i] == NULL)
continue;
if (strcmp(nftnl_family_str[i], family) == 0)
return i;
}
errno = EAFNOSUPPORT;
return -1;
}
static struct {
int len;
int64_t min;
uint64_t max;
} basetype[] = {
[NFTNL_TYPE_U8] = { .len = sizeof(uint8_t), .max = UINT8_MAX },
[NFTNL_TYPE_U16] = { .len = sizeof(uint16_t), .max = UINT16_MAX },
[NFTNL_TYPE_U32] = { .len = sizeof(uint32_t), .max = UINT32_MAX },
[NFTNL_TYPE_U64] = { .len = sizeof(uint64_t), .max = UINT64_MAX },
[NFTNL_TYPE_S8] = { .len = sizeof(int8_t), .min = INT8_MIN, .max = INT8_MAX },
[NFTNL_TYPE_S16] = { .len = sizeof(int16_t), .min = INT16_MIN, .max = INT16_MAX },
[NFTNL_TYPE_S32] = { .len = sizeof(int32_t), .min = INT32_MIN, .max = INT32_MAX },
[NFTNL_TYPE_S64] = { .len = sizeof(int64_t), .min = INT64_MIN, .max = INT64_MAX },
};
int nftnl_get_value(enum nftnl_type type, void *val, void *out)
{
union {
uint8_t u8;
uint16_t u16;
uint32_t u32;
int8_t s8;
int16_t s16;
int32_t s32;
} values;
void *valuep = NULL;
int64_t sval;
uint64_t uval;
switch (type) {
case NFTNL_TYPE_U8:
case NFTNL_TYPE_U16:
case NFTNL_TYPE_U32:
case NFTNL_TYPE_U64:
memcpy(&uval, val, sizeof(uval));
if (uval > basetype[type].max) {
errno = ERANGE;
return -1;
}
break;
case NFTNL_TYPE_S8:
case NFTNL_TYPE_S16:
case NFTNL_TYPE_S32:
case NFTNL_TYPE_S64:
memcpy(&sval, val, sizeof(sval));
if (sval < basetype[type].min ||
sval > (int64_t)basetype[type].max) {
errno = ERANGE;
return -1;
}
break;
}
switch (type) {
case NFTNL_TYPE_U8:
values.u8 = uval;
valuep = &values.u8;
break;
case NFTNL_TYPE_U16:
values.u16 = uval;
valuep = &values.u16;
break;
case NFTNL_TYPE_U32:
values.u32 = uval;
valuep = &values.u32;
break;
case NFTNL_TYPE_U64:
valuep = &uval;
break;
case NFTNL_TYPE_S8:
values.s8 = sval;
valuep = &values.s8;
break;
case NFTNL_TYPE_S16:
values.s16 = sval;
valuep = &values.s16;
break;
case NFTNL_TYPE_S32:
values.s32 = sval;
valuep = &values.s32;
break;
case NFTNL_TYPE_S64:
valuep = &sval;
break;
}
memcpy(out, valuep, basetype[type].len);
return 0;
}
int nftnl_strtoi(const char *string, int base, void *out, enum nftnl_type type)
{
int ret;
int64_t sval = 0;
uint64_t uval = -1;
char *endptr;
switch (type) {
case NFTNL_TYPE_U8:
case NFTNL_TYPE_U16:
case NFTNL_TYPE_U32:
case NFTNL_TYPE_U64:
uval = strtoll(string, &endptr, base);
ret = nftnl_get_value(type, &uval, out);
break;
case NFTNL_TYPE_S8:
case NFTNL_TYPE_S16:
case NFTNL_TYPE_S32:
case NFTNL_TYPE_S64:
sval = strtoull(string, &endptr, base);
ret = nftnl_get_value(type, &sval, out);
break;
default:
errno = EINVAL;
return -1;
}
if (*endptr) {
errno = EINVAL;
return -1;
}
return ret;
}
const char *nftnl_verdict2str(uint32_t verdict)
{
switch (verdict) {
case NF_ACCEPT:
return "accept";
case NF_DROP:
return "drop";
case NF_STOLEN:
return "stolen";
case NF_QUEUE:
return "queue";
case NF_REPEAT:
return "repeat";
case NF_STOP:
return "stop";
case NFT_RETURN:
return "return";
case NFT_JUMP:
return "jump";
case NFT_GOTO:
return "goto";
case NFT_CONTINUE:
return "continue";
case NFT_BREAK:
return "break";
default:
return "unknown";
}
}
int nftnl_str2verdict(const char *verdict, int *verdict_num)
{
if (strcmp(verdict, "accept") == 0) {
*verdict_num = NF_ACCEPT;
return 0;
} else if (strcmp(verdict, "drop") == 0) {
*verdict_num = NF_DROP;
return 0;
} else if (strcmp(verdict, "return") == 0) {
*verdict_num = NFT_RETURN;
return 0;
} else if (strcmp(verdict, "jump") == 0) {
*verdict_num = NFT_JUMP;
return 0;
} else if (strcmp(verdict, "goto") == 0) {
*verdict_num = NFT_GOTO;
return 0;
}
return -1;
}
enum nftnl_cmd_type nftnl_flag2cmd(uint32_t flags)
{
if (flags & NFTNL_OF_EVENT_NEW)
return NFTNL_CMD_ADD;
else if (flags & NFTNL_OF_EVENT_DEL)
return NFTNL_CMD_DELETE;
return NFTNL_CMD_UNSPEC;
}
static const char *cmd2tag[NFTNL_CMD_MAX] = {
[NFTNL_CMD_ADD] = "add",
[NFTNL_CMD_INSERT] = "insert",
[NFTNL_CMD_DELETE] = "delete",
[NFTNL_CMD_REPLACE] = "replace",
[NFTNL_CMD_FLUSH] = "flush",
};
const char *nftnl_cmd2tag(enum nftnl_cmd_type cmd)
{
if (cmd >= NFTNL_CMD_MAX)
return "unknown";
return cmd2tag[cmd];
}
uint32_t nftnl_str2cmd(const char *cmd)
{
if (strcmp(cmd, "add") == 0)
return NFTNL_CMD_ADD;
else if (strcmp(cmd, "insert") == 0)
return NFTNL_CMD_INSERT;
else if (strcmp(cmd, "delete") == 0)
return NFTNL_CMD_DELETE;
else if (strcmp(cmd, "replace") == 0)
return NFTNL_CMD_REPLACE;
else if (strcmp(cmd, "flush") == 0)
return NFTNL_CMD_FLUSH;
return NFTNL_CMD_UNSPEC;
}
int nftnl_fprintf(FILE *fp, const void *obj, uint32_t cmd, uint32_t type,
uint32_t flags,
int (*snprintf_cb)(char *buf, size_t bufsiz, const void *obj,
uint32_t cmd, uint32_t type,
uint32_t flags))
{
char _buf[NFTNL_SNPRINTF_BUFSIZ];
char *buf = _buf;
size_t bufsiz = sizeof(_buf);
int ret;
ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
if (ret <= 0)
goto out;
if (ret >= NFTNL_SNPRINTF_BUFSIZ) {
bufsiz = ret + 1;
buf = malloc(bufsiz);
if (buf == NULL)
return -1;
ret = snprintf_cb(buf, bufsiz, obj, cmd, type, flags);
if (ret <= 0)
goto out;
}
ret = fprintf(fp, "%s", buf);
out:
if (buf != _buf)
xfree(buf);
return ret;
}
void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max,
const char *filename, int line)
{
fprintf(stderr, "libnftnl: attribute %d > %d (maximum) assertion failed in %s:%d\n",
attr, attr_max, filename, line);
exit(EXIT_FAILURE);
}
void __nftnl_assert_fail(uint16_t attr, const char *filename, int line)
{
fprintf(stderr, "libnftnl: attribute %d assertion failed in %s:%d\n",
attr, filename, line);
exit(EXIT_FAILURE);
}
void __noreturn __abi_breakage(const char *file, int line, const char *reason)
{
fprintf(stderr, "nf_tables kernel ABI is broken, contact your vendor.\n"
"%s:%d reason: %s\n", file, line, reason);
exit(EXIT_FAILURE);
}
|