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
|
/*
* (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.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.
*
* This code has been sponsored by Sophos Astaro <http://www.sophos.com>
*/
#include "internal.h"
#include <time.h>
#include <endian.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <libmnl/libmnl.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nf_tables.h>
#include <libnftnl/expr.h>
struct nftnl_expr *nftnl_expr_alloc(const char *name)
{
struct nftnl_expr *expr;
struct expr_ops *ops;
ops = nftnl_expr_ops_lookup(name);
if (ops == NULL)
return NULL;
expr = calloc(1, sizeof(struct nftnl_expr) + ops->alloc_len);
if (expr == NULL)
return NULL;
/* Manually set expression name attribute */
expr->flags |= (1 << NFTNL_EXPR_NAME);
expr->ops = ops;
return expr;
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_alloc, nft_rule_expr_alloc);
void nftnl_expr_free(const struct nftnl_expr *expr)
{
if (expr->ops->free)
expr->ops->free(expr);
xfree(expr);
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_free, nft_rule_expr_free);
bool nftnl_expr_is_set(const struct nftnl_expr *expr, uint16_t type)
{
return expr->flags & (1 << type);
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_is_set, nft_rule_expr_is_set);
int nftnl_expr_set(struct nftnl_expr *expr, uint16_t type,
const void *data, uint32_t data_len)
{
switch(type) {
case NFTNL_EXPR_NAME: /* cannot be modified */
return 0;
default:
if (expr->ops->set(expr, type, data, data_len) < 0)
return -1;
}
expr->flags |= (1 << type);
return 0;
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_set, nft_rule_expr_set);
void
nftnl_expr_set_u8(struct nftnl_expr *expr, uint16_t type, uint8_t data)
{
nftnl_expr_set(expr, type, &data, sizeof(uint8_t));
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_set_u8, nft_rule_expr_set_u8);
void
nftnl_expr_set_u16(struct nftnl_expr *expr, uint16_t type, uint16_t data)
{
nftnl_expr_set(expr, type, &data, sizeof(uint16_t));
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_set_u16, nft_rule_expr_set_u16);
void
nftnl_expr_set_u32(struct nftnl_expr *expr, uint16_t type, uint32_t data)
{
nftnl_expr_set(expr, type, &data, sizeof(uint32_t));
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_set_u32, nft_rule_expr_set_u32);
void
nftnl_expr_set_u64(struct nftnl_expr *expr, uint16_t type, uint64_t data)
{
nftnl_expr_set(expr, type, &data, sizeof(uint64_t));
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_set_u64, nft_rule_expr_set_u64);
int nftnl_expr_set_str(struct nftnl_expr *expr, uint16_t type, const char *str)
{
return nftnl_expr_set(expr, type, str, strlen(str) + 1);
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_set_str, nft_rule_expr_set_str);
const void *nftnl_expr_get(const struct nftnl_expr *expr,
uint16_t type, uint32_t *data_len)
{
const void *ret;
if (!(expr->flags & (1 << type)))
return NULL;
switch(type) {
case NFTNL_EXPR_NAME:
ret = expr->ops->name;
break;
default:
ret = expr->ops->get(expr, type, data_len);
break;
}
return ret;
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_get, nft_rule_expr_get);
uint8_t nftnl_expr_get_u8(const struct nftnl_expr *expr, uint16_t type)
{
const void *data;
uint32_t data_len;
data = nftnl_expr_get(expr, type, &data_len);
if (data == NULL)
return 0;
if (data_len != sizeof(uint8_t))
return 0;
return *((uint8_t *)data);
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_get_u8, nft_rule_expr_get_u8);
uint16_t nftnl_expr_get_u16(const struct nftnl_expr *expr, uint16_t type)
{
const void *data;
uint32_t data_len;
data = nftnl_expr_get(expr, type, &data_len);
if (data == NULL)
return 0;
if (data_len != sizeof(uint16_t))
return 0;
return *((uint16_t *)data);
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_get_u16, nft_rule_expr_get_u16);
uint32_t nftnl_expr_get_u32(const struct nftnl_expr *expr, uint16_t type)
{
const void *data;
uint32_t data_len;
data = nftnl_expr_get(expr, type, &data_len);
if (data == NULL)
return 0;
if (data_len != sizeof(uint32_t))
return 0;
return *((uint32_t *)data);
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_get_u32, nft_rule_expr_get_u32);
uint64_t nftnl_expr_get_u64(const struct nftnl_expr *expr, uint16_t type)
{
const void *data;
uint32_t data_len;
data = nftnl_expr_get(expr, type, &data_len);
if (data == NULL)
return 0;
if (data_len != sizeof(uint64_t))
return 0;
return *((uint64_t *)data);
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_get_u64, nft_rule_expr_get_u64);
const char *nftnl_expr_get_str(const struct nftnl_expr *expr, uint16_t type)
{
uint32_t data_len;
return (const char *)nftnl_expr_get(expr, type, &data_len);
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_get_str, nft_rule_expr_get_str);
void
nftnl_expr_build_payload(struct nlmsghdr *nlh, struct nftnl_expr *expr)
{
struct nlattr *nest;
mnl_attr_put_strz(nlh, NFTA_EXPR_NAME, expr->ops->name);
nest = mnl_attr_nest_start(nlh, NFTA_EXPR_DATA);
expr->ops->build(nlh, expr);
mnl_attr_nest_end(nlh, nest);
}
static int nftnl_rule_parse_expr_cb(const struct nlattr *attr, void *data)
{
const struct nlattr **tb = data;
int type = mnl_attr_get_type(attr);
if (mnl_attr_type_valid(attr, NFTA_EXPR_MAX) < 0)
return MNL_CB_OK;
switch (type) {
case NFTA_EXPR_NAME:
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
abi_breakage();
break;
case NFTA_EXPR_DATA:
if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
abi_breakage();
break;
}
tb[type] = attr;
return MNL_CB_OK;
}
struct nftnl_expr *nftnl_expr_parse(struct nlattr *attr)
{
struct nlattr *tb[NFTA_EXPR_MAX+1] = {};
struct nftnl_expr *expr;
if (mnl_attr_parse_nested(attr, nftnl_rule_parse_expr_cb, tb) < 0)
goto err1;
expr = nftnl_expr_alloc(mnl_attr_get_str(tb[NFTA_EXPR_NAME]));
if (expr == NULL)
goto err1;
if (tb[NFTA_EXPR_DATA] &&
expr->ops->parse(expr, tb[NFTA_EXPR_DATA]) < 0)
goto err2;
return expr;
err2:
xfree(expr);
err1:
return NULL;
}
int nftnl_expr_snprintf(char *buf, size_t size, const struct nftnl_expr *expr,
uint32_t type, uint32_t flags)
{
int ret;
unsigned int offset = 0, len = size;
ret = expr->ops->snprintf(buf+offset, len, type, flags, expr);
SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
return offset;
}
EXPORT_SYMBOL_ALIAS(nftnl_expr_snprintf, nft_rule_expr_snprintf);
|