libnftnl 1.2.6
object.c
1/*
2 * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9#include "internal.h"
10
11#include <time.h>
12#include <endian.h>
13#include <stdint.h>
14#include <limits.h>
15#include <stdlib.h>
16#include <string.h>
17#include <netinet/in.h>
18#include <errno.h>
19
20#include <libmnl/libmnl.h>
21#include <linux/netfilter/nfnetlink.h>
22#include <linux/netfilter/nf_tables.h>
23
24#include <libnftnl/object.h>
25#include "obj.h"
26
27static struct obj_ops *obj_ops[__NFT_OBJECT_MAX] = {
28 [NFT_OBJECT_COUNTER] = &obj_ops_counter,
29 [NFT_OBJECT_QUOTA] = &obj_ops_quota,
30 [NFT_OBJECT_CT_HELPER] = &obj_ops_ct_helper,
31 [NFT_OBJECT_LIMIT] = &obj_ops_limit,
32 [NFT_OBJECT_TUNNEL] = &obj_ops_tunnel,
33 [NFT_OBJECT_CT_TIMEOUT] = &obj_ops_ct_timeout,
34 [NFT_OBJECT_SECMARK] = &obj_ops_secmark,
35 [NFT_OBJECT_CT_EXPECT] = &obj_ops_ct_expect,
36 [NFT_OBJECT_SYNPROXY] = &obj_ops_synproxy,
37};
38
39static struct obj_ops *nftnl_obj_ops_lookup(uint32_t type)
40{
41 if (type > NFT_OBJECT_MAX)
42 return NULL;
43
44 return obj_ops[type];
45}
46
47EXPORT_SYMBOL(nftnl_obj_alloc);
48struct nftnl_obj *nftnl_obj_alloc(void)
49{
50 return calloc(1, sizeof(struct nftnl_obj));
51}
52
53EXPORT_SYMBOL(nftnl_obj_free);
54void nftnl_obj_free(const struct nftnl_obj *obj)
55{
56 if (obj->flags & (1 << NFTNL_OBJ_TABLE))
57 xfree(obj->table);
58 if (obj->flags & (1 << NFTNL_OBJ_NAME))
59 xfree(obj->name);
60 if (obj->flags & (1 << NFTNL_OBJ_USERDATA))
61 xfree(obj->user.data);
62
63 xfree(obj);
64}
65
66EXPORT_SYMBOL(nftnl_obj_is_set);
67bool nftnl_obj_is_set(const struct nftnl_obj *obj, uint16_t attr)
68{
69 return obj->flags & (1 << attr);
70}
71
72static uint32_t nftnl_obj_validate[NFTNL_OBJ_MAX + 1] = {
73 [NFTNL_OBJ_FAMILY] = sizeof(uint32_t),
74 [NFTNL_OBJ_USE] = sizeof(uint32_t),
75 [NFTNL_OBJ_HANDLE] = sizeof(uint64_t),
76};
77
78EXPORT_SYMBOL(nftnl_obj_set_data);
79void nftnl_obj_set_data(struct nftnl_obj *obj, uint16_t attr,
80 const void *data, uint32_t data_len)
81{
82 if (attr < NFTNL_OBJ_MAX)
83 nftnl_assert_validate(data, nftnl_obj_validate, attr, data_len);
84
85 switch (attr) {
86 case NFTNL_OBJ_TABLE:
87 xfree(obj->table);
88 obj->table = strdup(data);
89 break;
90 case NFTNL_OBJ_NAME:
91 xfree(obj->name);
92 obj->name = strdup(data);
93 break;
94 case NFTNL_OBJ_TYPE:
95 obj->ops = nftnl_obj_ops_lookup(*((uint32_t *)data));
96 if (!obj->ops)
97 return;
98 break;
99 case NFTNL_OBJ_FAMILY:
100 memcpy(&obj->family, data, sizeof(obj->family));
101 break;
102 case NFTNL_OBJ_USE:
103 memcpy(&obj->use, data, sizeof(obj->use));
104 break;
105 case NFTNL_OBJ_HANDLE:
106 memcpy(&obj->handle, data, sizeof(obj->handle));
107 break;
108 case NFTNL_OBJ_USERDATA:
109 if (obj->flags & (1 << NFTNL_OBJ_USERDATA))
110 xfree(obj->user.data);
111
112 obj->user.data = malloc(data_len);
113 if (!obj->user.data)
114 return;
115 memcpy(obj->user.data, data, data_len);
116 obj->user.len = data_len;
117 break;
118 default:
119 if (obj->ops)
120 obj->ops->set(obj, attr, data, data_len);
121 break;
122 }
123 obj->flags |= (1 << attr);
124}
125
126void nftnl_obj_set(struct nftnl_obj *obj, uint16_t attr, const void *data) __visible;
127void nftnl_obj_set(struct nftnl_obj *obj, uint16_t attr, const void *data)
128{
129 nftnl_obj_set_data(obj, attr, data, nftnl_obj_validate[attr]);
130}
131
132EXPORT_SYMBOL(nftnl_obj_set_u8);
133void nftnl_obj_set_u8(struct nftnl_obj *obj, uint16_t attr, uint8_t val)
134{
135 nftnl_obj_set_data(obj, attr, &val, sizeof(uint8_t));
136}
137
138EXPORT_SYMBOL(nftnl_obj_set_u16);
139void nftnl_obj_set_u16(struct nftnl_obj *obj, uint16_t attr, uint16_t val)
140{
141 nftnl_obj_set_data(obj, attr, &val, sizeof(uint16_t));
142}
143
144EXPORT_SYMBOL(nftnl_obj_set_u32);
145void nftnl_obj_set_u32(struct nftnl_obj *obj, uint16_t attr, uint32_t val)
146{
147 nftnl_obj_set_data(obj, attr, &val, sizeof(uint32_t));
148}
149
150EXPORT_SYMBOL(nftnl_obj_set_u64);
151void nftnl_obj_set_u64(struct nftnl_obj *obj, uint16_t attr, uint64_t val)
152{
153 nftnl_obj_set_data(obj, attr, &val, sizeof(uint64_t));
154}
155
156EXPORT_SYMBOL(nftnl_obj_set_str);
157void nftnl_obj_set_str(struct nftnl_obj *obj, uint16_t attr, const char *str)
158{
159 nftnl_obj_set_data(obj, attr, str, 0);
160}
161
162EXPORT_SYMBOL(nftnl_obj_get_data);
163const void *nftnl_obj_get_data(struct nftnl_obj *obj, uint16_t attr,
164 uint32_t *data_len)
165{
166 if (!(obj->flags & (1 << attr)))
167 return NULL;
168
169 switch(attr) {
170 case NFTNL_OBJ_TABLE:
171 return obj->table;
172 case NFTNL_OBJ_NAME:
173 return obj->name;
174 case NFTNL_OBJ_TYPE:
175 if (!obj->ops)
176 return NULL;
177
178 *data_len = sizeof(uint32_t);
179 return &obj->ops->type;
180 case NFTNL_OBJ_FAMILY:
181 *data_len = sizeof(uint32_t);
182 return &obj->family;
183 case NFTNL_OBJ_USE:
184 *data_len = sizeof(uint32_t);
185 return &obj->use;
186 case NFTNL_OBJ_HANDLE:
187 *data_len = sizeof(uint64_t);
188 return &obj->handle;
189 case NFTNL_OBJ_USERDATA:
190 *data_len = obj->user.len;
191 return obj->user.data;
192 default:
193 if (obj->ops)
194 return obj->ops->get(obj, attr, data_len);
195 break;
196 }
197 return NULL;
198}
199
200EXPORT_SYMBOL(nftnl_obj_get);
201const void *nftnl_obj_get(struct nftnl_obj *obj, uint16_t attr)
202{
203 uint32_t data_len;
204 return nftnl_obj_get_data(obj, attr, &data_len);
205}
206
207EXPORT_SYMBOL(nftnl_obj_get_u8);
208uint8_t nftnl_obj_get_u8(struct nftnl_obj *obj, uint16_t attr)
209{
210 const void *ret = nftnl_obj_get(obj, attr);
211 return ret == NULL ? 0 : *((uint8_t *)ret);
212}
213
214EXPORT_SYMBOL(nftnl_obj_get_u16);
215uint16_t nftnl_obj_get_u16(struct nftnl_obj *obj, uint16_t attr)
216{
217 const void *ret = nftnl_obj_get(obj, attr);
218 return ret == NULL ? 0 : *((uint16_t *)ret);
219}
220
221EXPORT_SYMBOL(nftnl_obj_get_u32);
222uint32_t nftnl_obj_get_u32(struct nftnl_obj *obj, uint16_t attr)
223{
224 const void *ret = nftnl_obj_get(obj, attr);
225 return ret == NULL ? 0 : *((uint32_t *)ret);
226}
227
228EXPORT_SYMBOL(nftnl_obj_get_u64);
229uint64_t nftnl_obj_get_u64(struct nftnl_obj *obj, uint16_t attr)
230{
231 const void *ret = nftnl_obj_get(obj, attr);
232 return ret == NULL ? 0 : *((uint64_t *)ret);
233}
234
235EXPORT_SYMBOL(nftnl_obj_get_str);
236const char *nftnl_obj_get_str(struct nftnl_obj *obj, uint16_t attr)
237{
238 return nftnl_obj_get(obj, attr);
239}
240
241EXPORT_SYMBOL(nftnl_obj_nlmsg_build_payload);
242void nftnl_obj_nlmsg_build_payload(struct nlmsghdr *nlh,
243 const struct nftnl_obj *obj)
244{
245 if (obj->flags & (1 << NFTNL_OBJ_TABLE))
246 mnl_attr_put_strz(nlh, NFTA_OBJ_TABLE, obj->table);
247 if (obj->flags & (1 << NFTNL_OBJ_NAME))
248 mnl_attr_put_strz(nlh, NFTA_OBJ_NAME, obj->name);
249 if (obj->flags & (1 << NFTNL_OBJ_TYPE))
250 mnl_attr_put_u32(nlh, NFTA_OBJ_TYPE, htonl(obj->ops->type));
251 if (obj->flags & (1 << NFTNL_OBJ_HANDLE))
252 mnl_attr_put_u64(nlh, NFTA_OBJ_HANDLE, htobe64(obj->handle));
253 if (obj->flags & (1 << NFTNL_OBJ_USERDATA))
254 mnl_attr_put(nlh, NFTA_OBJ_USERDATA, obj->user.len, obj->user.data);
255 if (obj->ops) {
256 struct nlattr *nest = mnl_attr_nest_start(nlh, NFTA_OBJ_DATA);
257
258 obj->ops->build(nlh, obj);
259 mnl_attr_nest_end(nlh, nest);
260 }
261}
262
263static int nftnl_obj_parse_attr_cb(const struct nlattr *attr, void *data)
264{
265 const struct nlattr **tb = data;
266 int type = mnl_attr_get_type(attr);
267
268 if (mnl_attr_type_valid(attr, NFTA_OBJ_MAX) < 0)
269 return MNL_CB_OK;
270
271 switch(type) {
272 case NFTA_OBJ_TABLE:
273 case NFTA_OBJ_NAME:
274 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
275 abi_breakage();
276 break;
277 case NFTA_OBJ_HANDLE:
278 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
279 abi_breakage();
280 break;
281 case NFTA_OBJ_DATA:
282 if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
283 abi_breakage();
284 break;
285 case NFTA_OBJ_USE:
286 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
287 abi_breakage();
288 break;
289 case NFTA_OBJ_USERDATA:
290 if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0)
291 abi_breakage();
292 break;
293 }
294
295 tb[type] = attr;
296 return MNL_CB_OK;
297}
298
299EXPORT_SYMBOL(nftnl_obj_nlmsg_parse);
300int nftnl_obj_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_obj *obj)
301{
302 struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
303 struct nlattr *tb[NFTA_OBJ_MAX + 1] = {};
304 int err;
305
306 if (mnl_attr_parse(nlh, sizeof(*nfg), nftnl_obj_parse_attr_cb, tb) < 0)
307 return -1;
308
309 if (tb[NFTA_OBJ_TABLE]) {
310 obj->table = strdup(mnl_attr_get_str(tb[NFTA_OBJ_TABLE]));
311 obj->flags |= (1 << NFTNL_OBJ_TABLE);
312 }
313 if (tb[NFTA_OBJ_NAME]) {
314 obj->name = strdup(mnl_attr_get_str(tb[NFTA_OBJ_NAME]));
315 obj->flags |= (1 << NFTNL_OBJ_NAME);
316 }
317 if (tb[NFTA_OBJ_TYPE]) {
318 uint32_t type = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_TYPE]));
319
320 obj->ops = nftnl_obj_ops_lookup(type);
321 if (obj->ops)
322 obj->flags |= (1 << NFTNL_OBJ_TYPE);
323 }
324 if (tb[NFTA_OBJ_DATA]) {
325 if (obj->ops) {
326 err = obj->ops->parse(obj, tb[NFTA_OBJ_DATA]);
327 if (err < 0)
328 return err;
329 }
330 }
331 if (tb[NFTA_OBJ_USE]) {
332 obj->use = ntohl(mnl_attr_get_u32(tb[NFTA_OBJ_USE]));
333 obj->flags |= (1 << NFTNL_OBJ_USE);
334 }
335 if (tb[NFTA_OBJ_HANDLE]) {
336 obj->handle = be64toh(mnl_attr_get_u64(tb[NFTA_OBJ_HANDLE]));
337 obj->flags |= (1 << NFTNL_OBJ_HANDLE);
338 }
339 if (tb[NFTA_OBJ_USERDATA]) {
340 nftnl_obj_set_data(obj, NFTNL_OBJ_USERDATA,
341 mnl_attr_get_payload(tb[NFTA_OBJ_USERDATA]),
342 mnl_attr_get_payload_len(tb[NFTA_OBJ_USERDATA]));
343 }
344
345 obj->family = nfg->nfgen_family;
346 obj->flags |= (1 << NFTNL_OBJ_FAMILY);
347
348 return 0;
349}
350
351static int nftnl_obj_do_parse(struct nftnl_obj *obj, enum nftnl_parse_type type,
352 const void *data, struct nftnl_parse_err *err,
353 enum nftnl_parse_input input)
354{
355 struct nftnl_parse_err perr = {};
356 int ret;
357
358 switch (type) {
359 case NFTNL_PARSE_JSON:
360 case NFTNL_PARSE_XML:
361 default:
362 ret = -1;
363 errno = EOPNOTSUPP;
364 break;
365 }
366
367 if (err != NULL)
368 *err = perr;
369
370 return ret;
371}
372
373EXPORT_SYMBOL(nftnl_obj_parse);
374int nftnl_obj_parse(struct nftnl_obj *obj, enum nftnl_parse_type type,
375 const char *data, struct nftnl_parse_err *err)
376{
377 return nftnl_obj_do_parse(obj, type, data, err, NFTNL_PARSE_BUFFER);
378}
379
380EXPORT_SYMBOL(nftnl_obj_parse_file);
381int nftnl_obj_parse_file(struct nftnl_obj *obj, enum nftnl_parse_type type,
382 FILE *fp, struct nftnl_parse_err *err)
383{
384 return nftnl_obj_do_parse(obj, type, fp, err, NFTNL_PARSE_FILE);
385}
386
387static int nftnl_obj_snprintf_dflt(char *buf, size_t remain,
388 const struct nftnl_obj *obj,
389 uint32_t type, uint32_t flags)
390{
391 const char *name = obj->ops ? obj->ops->name : "(unknown)";
392 int ret, offset = 0;
393
394 ret = snprintf(buf, remain, "table %s name %s use %u [ %s ",
395 obj->table, obj->name, obj->use, name);
396 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
397
398 if (obj->ops) {
399 ret = obj->ops->output(buf + offset, remain, flags, obj);
400 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
401 }
402 ret = snprintf(buf + offset, remain, "]");
403 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
404
405 return offset;
406}
407
408static int nftnl_obj_cmd_snprintf(char *buf, size_t remain,
409 const struct nftnl_obj *obj, uint32_t cmd,
410 uint32_t type, uint32_t flags)
411{
412 int ret, offset = 0;
413
414 if (type != NFTNL_OUTPUT_DEFAULT)
415 return -1;
416
417 ret = nftnl_obj_snprintf_dflt(buf + offset, remain, obj, type, flags);
418 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
419 return offset;
420}
421
422EXPORT_SYMBOL(nftnl_obj_snprintf);
423int nftnl_obj_snprintf(char *buf, size_t size, const struct nftnl_obj *obj,
424 uint32_t type, uint32_t flags)
425{
426 if (size)
427 buf[0] = '\0';
428
429 return nftnl_obj_cmd_snprintf(buf, size, obj, nftnl_flag2cmd(flags),
430 type, flags);
431}
432
433static int nftnl_obj_do_snprintf(char *buf, size_t size, const void *obj,
434 uint32_t cmd, uint32_t type, uint32_t flags)
435{
436 return nftnl_obj_snprintf(buf, size, obj, type, flags);
437}
438
439EXPORT_SYMBOL(nftnl_obj_fprintf);
440int nftnl_obj_fprintf(FILE *fp, const struct nftnl_obj *obj, uint32_t type,
441 uint32_t flags)
442{
443 return nftnl_fprintf(fp, obj, NFTNL_CMD_UNSPEC, type, flags,
444 nftnl_obj_do_snprintf);
445}
446
448 struct list_head list;
449};
450
451EXPORT_SYMBOL(nftnl_obj_list_alloc);
452struct nftnl_obj_list *nftnl_obj_list_alloc(void)
453{
454 struct nftnl_obj_list *list;
455
456 list = calloc(1, sizeof(struct nftnl_obj_list));
457 if (list == NULL)
458 return NULL;
459
460 INIT_LIST_HEAD(&list->list);
461
462 return list;
463}
464
465EXPORT_SYMBOL(nftnl_obj_list_free);
466void nftnl_obj_list_free(struct nftnl_obj_list *list)
467{
468 struct nftnl_obj *r, *tmp;
469
470 list_for_each_entry_safe(r, tmp, &list->list, head) {
471 list_del(&r->head);
472 nftnl_obj_free(r);
473 }
474 xfree(list);
475}
476
477EXPORT_SYMBOL(nftnl_obj_list_is_empty);
478int nftnl_obj_list_is_empty(struct nftnl_obj_list *list)
479{
480 return list_empty(&list->list);
481}
482
483EXPORT_SYMBOL(nftnl_obj_list_add);
484void nftnl_obj_list_add(struct nftnl_obj *r, struct nftnl_obj_list *list)
485{
486 list_add(&r->head, &list->list);
487}
488
489EXPORT_SYMBOL(nftnl_obj_list_add_tail);
490void nftnl_obj_list_add_tail(struct nftnl_obj *r,
491 struct nftnl_obj_list *list)
492{
493 list_add_tail(&r->head, &list->list);
494}
495
496EXPORT_SYMBOL(nftnl_obj_list_del);
497void nftnl_obj_list_del(struct nftnl_obj *t)
498{
499 list_del(&t->head);
500}
501
502EXPORT_SYMBOL(nftnl_obj_list_foreach);
503int nftnl_obj_list_foreach(struct nftnl_obj_list *table_list,
504 int (*cb)(struct nftnl_obj *t, void *data),
505 void *data)
506{
507 struct nftnl_obj *cur, *tmp;
508 int ret;
509
510 list_for_each_entry_safe(cur, tmp, &table_list->list, head) {
511 ret = cb(cur, data);
512 if (ret < 0)
513 return ret;
514 }
515 return 0;
516}
517
519 struct nftnl_obj_list *list;
520 struct nftnl_obj *cur;
521};
522
523EXPORT_SYMBOL(nftnl_obj_list_iter_create);
524struct nftnl_obj_list_iter *
525nftnl_obj_list_iter_create(struct nftnl_obj_list *l)
526{
527 struct nftnl_obj_list_iter *iter;
528
529 iter = calloc(1, sizeof(struct nftnl_obj_list_iter));
530 if (iter == NULL)
531 return NULL;
532
533 iter->list = l;
534 if (nftnl_obj_list_is_empty(l))
535 iter->cur = NULL;
536 else
537 iter->cur = list_entry(l->list.next, struct nftnl_obj, head);
538
539 return iter;
540}
541
542EXPORT_SYMBOL(nftnl_obj_list_iter_next);
543struct nftnl_obj *nftnl_obj_list_iter_next(struct nftnl_obj_list_iter *iter)
544{
545 struct nftnl_obj *r = iter->cur;
546
547 if (r == NULL)
548 return NULL;
549
550 /* get next table, if any */
551 iter->cur = list_entry(iter->cur->head.next, struct nftnl_obj, head);
552 if (&iter->cur->head == iter->list->list.next)
553 return NULL;
554
555 return r;
556}
557
558EXPORT_SYMBOL(nftnl_obj_list_iter_destroy);
559void nftnl_obj_list_iter_destroy(struct nftnl_obj_list_iter *iter)
560{
561 xfree(iter);
562}