summaryrefslogtreecommitdiffstats
path: root/src/table.c
blob: 09470e05ea526e4b4281e9f9dbaeedd0d05cce72 (plain)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
 * (C) 2012-2013 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 <limits.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>

#include <libmnl/libmnl.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nf_tables.h>

#include <libnftnl/table.h>
#include <buffer.h>

struct nft_table {
	struct list_head head;

	const char	*name;
	uint32_t	family;
	uint32_t	table_flags;
	uint32_t	use;
	uint32_t	flags;
};

struct nft_table *nft_table_alloc(void)
{
	return calloc(1, sizeof(struct nft_table));
}
EXPORT_SYMBOL(nft_table_alloc);

void nft_table_free(struct nft_table *t)
{
	if (t->flags & (1 << NFT_TABLE_ATTR_NAME))
		xfree(t->name);

	xfree(t);
}
EXPORT_SYMBOL(nft_table_free);

bool nft_table_attr_is_set(const struct nft_table *t, uint16_t attr)
{
	return t->flags & (1 << attr);
}
EXPORT_SYMBOL(nft_table_attr_is_set);

void nft_table_attr_unset(struct nft_table *t, uint16_t attr)
{
	if (!(t->flags & (1 << attr)))
		return;

	switch (attr) {
	case NFT_TABLE_ATTR_NAME:
		if (t->name) {
			xfree(t->name);
			t->name = NULL;
		}
		break;
	case NFT_TABLE_ATTR_FLAGS:
	case NFT_TABLE_ATTR_FAMILY:
		break;
	case NFT_TABLE_ATTR_USE:
		break;
	}
	t->flags &= ~(1 << attr);
}
EXPORT_SYMBOL(nft_table_attr_unset);

static uint32_t nft_table_attr_validate[NFT_TABLE_ATTR_MAX + 1] = {
	[NFT_TABLE_ATTR_FLAGS]	= sizeof(uint32_t),
	[NFT_TABLE_ATTR_FAMILY]	= sizeof(uint32_t),
};

void nft_table_attr_set_data(struct nft_table *t, uint16_t attr,
			     const void *data, uint32_t data_len)
{
	if (attr > NFT_TABLE_ATTR_MAX)
		return;

	nft_assert_validate(data, nft_table_attr_validate, attr, data_len);

	switch (attr) {
	case NFT_TABLE_ATTR_NAME:
		if (t->name)
			xfree(t->name);

		t->name = strdup(data);
		break;
	case NFT_TABLE_ATTR_FLAGS:
		t->table_flags = *((uint32_t *)data);
		break;
	case NFT_TABLE_ATTR_FAMILY:
		t->family = *((uint32_t *)data);
		break;
	case NFT_TABLE_ATTR_USE:
		t->use = *((uint32_t *)data);
		break;
	}
	t->flags |= (1 << attr);
}
EXPORT_SYMBOL(nft_table_attr_set_data);

void nft_table_attr_set(struct nft_table *t, uint16_t attr, const void *data)
{
	nft_table_attr_set_data(t, attr, data, nft_table_attr_validate[attr]);
}
EXPORT_SYMBOL(nft_table_attr_set);

void nft_table_attr_set_u32(struct nft_table *t, uint16_t attr, uint32_t val)
{
	nft_table_attr_set_data(t, attr, &val, sizeof(uint32_t));
}
EXPORT_SYMBOL(nft_table_attr_set_u32);

void nft_table_attr_set_u8(struct nft_table *t, uint16_t attr, uint8_t val)
{
	nft_table_attr_set_data(t, attr, &val, sizeof(uint8_t));
}
EXPORT_SYMBOL(nft_table_attr_set_u8);

void nft_table_attr_set_str(struct nft_table *t, uint16_t attr, const char *str)
{
	nft_table_attr_set_data(t, attr, str, 0);
}
EXPORT_SYMBOL(nft_table_attr_set_str);

const void *nft_table_attr_get_data(struct nft_table *t, uint16_t attr,
				    uint32_t *data_len)
{
	if (!(t->flags & (1 << attr)))
		return NULL;

	switch(attr) {
	case NFT_TABLE_ATTR_NAME:
		return t->name;
	case NFT_TABLE_ATTR_FLAGS:
		*data_len = sizeof(uint32_t);
		return &t->table_flags;
	case NFT_TABLE_ATTR_FAMILY:
		*data_len = sizeof(uint32_t);
		return &t->family;
	case NFT_TABLE_ATTR_USE:
		*data_len = sizeof(uint32_t);
		return &t->use;
	}
	return NULL;
}
EXPORT_SYMBOL(nft_table_attr_get_data);

const void *nft_table_attr_get(struct nft_table *t, uint16_t attr)
{
	uint32_t data_len;
	return nft_table_attr_get_data(t, attr, &data_len);
}
EXPORT_SYMBOL(nft_table_attr_get);

uint32_t nft_table_attr_get_u32(struct nft_table *t, uint16_t attr)
{
	const void *ret = nft_table_attr_get(t, attr);
	return ret == NULL ? 0 : *((uint32_t *)ret);
}
EXPORT_SYMBOL(nft_table_attr_get_u32);

uint8_t nft_table_attr_get_u8(struct nft_table *t, uint16_t attr)
{
	const void *ret = nft_table_attr_get(t, attr);
	return ret == NULL ? 0 : *((uint8_t *)ret);
}
EXPORT_SYMBOL(nft_table_attr_get_u8);

const char *nft_table_attr_get_str(struct nft_table *t, uint16_t attr)
{
	return nft_table_attr_get(t, attr);
}
EXPORT_SYMBOL(nft_table_attr_get_str);

void nft_table_nlmsg_build_payload(struct nlmsghdr *nlh, const struct nft_table *t)
{
	if (t->flags & (1 << NFT_TABLE_ATTR_NAME))
		mnl_attr_put_strz(nlh, NFTA_TABLE_NAME, t->name);
	if (t->flags & (1 << NFT_TABLE_ATTR_FLAGS))
		mnl_attr_put_u32(nlh, NFTA_TABLE_FLAGS, htonl(t->table_flags));
}
EXPORT_SYMBOL(nft_table_nlmsg_build_payload);

static int nft_table_parse_attr_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_TABLE_MAX) < 0)
		return MNL_CB_OK;

	switch(type) {
	case NFTA_TABLE_NAME:
		if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
			abi_breakage();
		break;
	case NFTA_TABLE_FLAGS:
	case NFTA_TABLE_USE:
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
			abi_breakage();
		break;
	}

	tb[type] = attr;
	return MNL_CB_OK;
}

int nft_table_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_table *t)
{
	struct nlattr *tb[NFTA_TABLE_MAX+1] = {};
	struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);

	if (mnl_attr_parse(nlh, sizeof(*nfg), nft_table_parse_attr_cb, tb) < 0)
		return -1;

	if (tb[NFTA_TABLE_NAME]) {
		xfree(t->name);
		t->name = strdup(mnl_attr_get_str(tb[NFTA_TABLE_NAME]));
		t->flags |= (1 << NFT_TABLE_ATTR_NAME);
	}
	if (tb[NFTA_TABLE_FLAGS]) {
		t->table_flags = ntohl(mnl_attr_get_u32(tb[NFTA_TABLE_FLAGS]));
		t->flags |= (1 << NFT_TABLE_ATTR_FLAGS);
	}
	if (tb[NFTA_TABLE_USE]) {
		t->use = ntohl(mnl_attr_get_u32(tb[NFTA_TABLE_USE]));
		t->flags |= (1 << NFT_TABLE_ATTR_USE);
	}

	t->family = nfg->nfgen_family;
	t->flags |= (1 << NFT_TABLE_ATTR_FAMILY);

	return 0;
}
EXPORT_SYMBOL(nft_table_nlmsg_parse);

#ifdef XML_PARSING
int nft_mxml_table_parse(mxml_node_t *tree, struct nft_table *t,
			 struct nft_parse_err *err)
{
	const char *name;
	int family;
	uint32_t flags, use;

	name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST,
				  NFT_XML_MAND, err);
	if (name != NULL)
		nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, name);

	family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST,
				       NFT_XML_MAND, err);
	if (family >= 0)
		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);

	if (nft_mxml_num_parse(tree, "flags", MXML_DESCEND, BASE_DEC,
			       &flags, NFT_TYPE_U32, NFT_XML_MAND, err) == 0)
		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);

	if (nft_mxml_num_parse(tree, "use", MXML_DESCEND, BASE_DEC,
			       &use, NFT_TYPE_U32, NFT_XML_MAND, err) == 0)
		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_USE, use);

	return 0;
}
#endif

static int nft_table_xml_parse(struct nft_table *t, const void *data,
			       struct nft_parse_err *err,
			       enum nft_parse_input input)
{
#ifdef XML_PARSING
	int ret;
	mxml_node_t *tree = nft_mxml_build_tree(data, "table", err, input);
	if (tree == NULL)
		return -1;

	ret = nft_mxml_table_parse(tree, t, err);
	mxmlDelete(tree);
	return ret;
#else
	errno = EOPNOTSUPP;
	return -1;
#endif
}

#ifdef JSON_PARSING
int nft_jansson_parse_table(struct nft_table *t, json_t *tree,
			    struct nft_parse_err *err)
{
	json_t *root;
	uint32_t flags, use;
	const char *str;
	int family;

	root = nft_jansson_get_node(tree, "table", err);
	if (root == NULL)
		return -1;

	str = nft_jansson_parse_str(root, "name", err);
	if (str != NULL)
		nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, str);

	if (nft_jansson_parse_family(root, &family, err) == 0)
		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);

	if (nft_jansson_parse_val(root, "flags", NFT_TYPE_U32, &flags,
				  err) == 0)
		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);

	if (nft_jansson_parse_val(root, "use", NFT_TYPE_U32, &use, err) == 0)
		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_USE, use);

	return 0;
}
#endif

static int nft_table_json_parse(struct nft_table *t, const void *json,
				struct nft_parse_err *err,
				enum nft_parse_input input)
{
#ifdef JSON_PARSING
	json_t *tree;
	json_error_t error;
	int ret;

	tree = nft_jansson_create_root(json, &error, err, input);
	if (tree == NULL)
		return -1;

	ret = nft_jansson_parse_table(t, tree, err);

	nft_jansson_free_root(tree);

	return ret;
#else
	errno = EOPNOTSUPP;
	return -1;
#endif
}

static int nft_table_do_parse(struct nft_table *t, enum nft_parse_type type,
			      const void *data, struct nft_parse_err *err,
			      enum nft_parse_input input)
{
	int ret;
	struct nft_parse_err perr;

	switch (type) {
	case NFT_PARSE_XML:
		ret = nft_table_xml_parse(t, data, &perr, input);
		break;
	case NFT_PARSE_JSON:
		ret = nft_table_json_parse(t, data, &perr, input);
		break;
	default:
		ret = -1;
		errno = EOPNOTSUPP;
		break;
	}

	if (err != NULL)
		*err = perr;

	return ret;
}

int nft_table_parse(struct nft_table *t, enum nft_parse_type type,
		    const char *data, struct nft_parse_err *err)
{
	return nft_table_do_parse(t, type, data, err, NFT_PARSE_BUFFER);
}
EXPORT_SYMBOL(nft_table_parse);

int nft_table_parse_file(struct nft_table *t, enum nft_parse_type type,
			 FILE *fp, struct nft_parse_err *err)
{
	return nft_table_do_parse(t, type, fp, err, NFT_PARSE_FILE);
}
EXPORT_SYMBOL(nft_table_parse_file);

static int nft_table_export(char *buf, size_t size, struct nft_table *t,
			    int type)
{
	NFT_BUF_INIT(b, buf, size);

	nft_buf_open(&b, type, TABLE);
	if (t->flags & (1 << NFT_TABLE_ATTR_NAME))
		nft_buf_str(&b, type, t->name, NAME);
	if (t->flags & (1 << NFT_TABLE_ATTR_FAMILY))
		nft_buf_str(&b, type, nft_family2str(t->family), FAMILY);
	if (t->flags & (1 << NFT_TABLE_ATTR_FLAGS))
		nft_buf_u32(&b, type, t->table_flags, FLAGS);
	if (t->flags & (1 << NFT_TABLE_ATTR_USE))
		nft_buf_u32(&b, type, t->use, USE);

	nft_buf_close(&b, type, TABLE);

	return nft_buf_done(&b);
}

static int nft_table_snprintf_default(char *buf, size_t size, struct nft_table *t)
{
	return snprintf(buf, size, "table %s %s flags %x use %d",
			t->name, nft_family2str(t->family),
			t->table_flags, t->use);
}

static int nft_table_cmd_snprintf(char *buf, size_t size, struct nft_table *t,
				  uint32_t cmd, uint32_t type, uint32_t flags)
{
	int ret, len = size, offset = 0;

	ret = nft_cmd_header_snprintf(buf + offset, len, cmd, type, flags);
	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);

	switch (type) {
	case NFT_OUTPUT_DEFAULT:
		ret = nft_table_snprintf_default(buf+offset, len, t);
		break;
	case NFT_OUTPUT_XML:
	case NFT_OUTPUT_JSON:
		ret = nft_table_export(buf+offset, len, t, type);
		break;
	default:
		return -1;
	}
	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);

	ret = nft_cmd_footer_snprintf(buf + offset, len, cmd, type, flags);
	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);

	return offset;
}

int nft_table_snprintf(char *buf, size_t size, struct nft_table *t,
		       uint32_t type, uint32_t flags)
{
	return nft_table_cmd_snprintf(buf, size, t, nft_flag2cmd(flags), type,
				      flags);
}
EXPORT_SYMBOL(nft_table_snprintf);

static inline int nft_table_do_snprintf(char *buf, size_t size, void *t,
					uint32_t cmd, uint32_t type,
					uint32_t flags)
{
	return nft_table_snprintf(buf, size, t, type, flags);
}

int nft_table_fprintf(FILE *fp, struct nft_table *t, uint32_t type,
		      uint32_t flags)
{
	return nft_fprintf(fp, t, NFT_CMD_UNSPEC, type, flags,
			   nft_table_do_snprintf);
}
EXPORT_SYMBOL(nft_table_fprintf);

struct nft_table_list {
	struct list_head list;
};

struct nft_table_list *nft_table_list_alloc(void)
{
	struct nft_table_list *list;

	list = calloc(1, sizeof(struct nft_table_list));
	if (list == NULL)
		return NULL;

	INIT_LIST_HEAD(&list->list);

	return list;
}
EXPORT_SYMBOL(nft_table_list_alloc);

void nft_table_list_free(struct nft_table_list *list)
{
	struct nft_table *r, *tmp;

	list_for_each_entry_safe(r, tmp, &list->list, head) {
		list_del(&r->head);
		nft_table_free(r);
	}
	xfree(list);
}
EXPORT_SYMBOL(nft_table_list_free);

int nft_table_list_is_empty(struct nft_table_list *list)
{
	return list_empty(&list->list);
}
EXPORT_SYMBOL(nft_table_list_is_empty);

void nft_table_list_add(struct nft_table *r, struct nft_table_list *list)
{
	list_add(&r->head, &list->list);
}
EXPORT_SYMBOL(nft_table_list_add);

void nft_table_list_add_tail(struct nft_table *r, struct nft_table_list *list)
{
	list_add_tail(&r->head, &list->list);
}
EXPORT_SYMBOL(nft_table_list_add_tail);

void nft_table_list_del(struct nft_table *t)
{
	list_del(&t->head);
}
EXPORT_SYMBOL(nft_table_list_del);

int nft_table_list_foreach(struct nft_table_list *table_list,
			   int (*cb)(struct nft_table *t, void *data),
			   void *data)
{
	struct nft_table *cur, *tmp;
	int ret;

	list_for_each_entry_safe(cur, tmp, &table_list->list, head) {
		ret = cb(cur, data);
		if (ret < 0)
			return ret;
	}
	return 0;
}
EXPORT_SYMBOL(nft_table_list_foreach);

struct nft_table_list_iter {
	struct nft_table_list	*list;
	struct nft_table	*cur;
};

struct nft_table_list_iter *nft_table_list_iter_create(struct nft_table_list *l)
{
	struct nft_table_list_iter *iter;

	iter = calloc(1, sizeof(struct nft_table_list_iter));
	if (iter == NULL)
		return NULL;

	iter->list = l;
	if (nft_table_list_is_empty(l))
		iter->cur = NULL;
	else
		iter->cur = list_entry(l->list.next, struct nft_table, head);

	return iter;
}
EXPORT_SYMBOL(nft_table_list_iter_create);

struct nft_table *nft_table_list_iter_next(struct nft_table_list_iter *iter)
{
	struct nft_table *r = iter->cur;

	if (r == NULL)
		return NULL;

	/* get next table, if any */
	iter->cur = list_entry(iter->cur->head.next, struct nft_table, head);
	if (&iter->cur->head == iter->list->list.next)
		return NULL;

	return r;
}
EXPORT_SYMBOL(nft_table_list_iter_next);

void nft_table_list_iter_destroy(struct nft_table_list_iter *iter)
{
	xfree(iter);
}
EXPORT_SYMBOL(nft_table_list_iter_destroy);