summaryrefslogtreecommitdiffstats
path: root/src/jansson.c
blob: 5107eb54976425d802bc4704acbe69f6d04e3eda (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
/*
 * (C) 2013 by Álvaro Neira Ayuso <alvaroneay@gmail.com>
 *
 * 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 <limits.h>
#include <stdint.h>
#include <stdbool.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include "expr_ops.h"
#include <libnftnl/set.h>

#include <libnftnl/expr.h>
#include <linux/netfilter/nf_tables.h>

#ifdef JSON_PARSING

static int nft_jansson_load_int_node(json_t *root, const char *node_name,
				      json_int_t *val, struct nft_parse_err *err)
{
	json_t *node;

	node = json_object_get(root, node_name);
	if (node == NULL) {
		err->error = NFT_PARSE_EMISSINGNODE;
		err->node_name = node_name;
		errno = EINVAL;
		return -1;
	}

	if (!json_is_integer(node)) {
		err->error = NFT_PARSE_EBADTYPE;
		err->node_name = node_name;
		errno = ERANGE;
		return -1;
	}
	*val = json_integer_value(node);

	return 0;
}

const char *nft_jansson_parse_str(json_t *root, const char *node_name,
				  struct nft_parse_err *err)
{
	json_t *node;
	const char *val;

	node = json_object_get(root, node_name);
	if (node == NULL) {
		err->error = NFT_PARSE_EMISSINGNODE;
		err->node_name = node_name;
		errno = EINVAL;
		return NULL;
	}

	val = json_string_value(node);
	if (val == NULL) {
		err->error = NFT_PARSE_EBADTYPE;
		err->node_name = node_name;
	}

	return val;
}

int nft_jansson_parse_val(json_t *root, const char *node_name, int type,
			  void *out, struct nft_parse_err *err)
{
	json_int_t val;

	if (nft_jansson_load_int_node(root, node_name, &val, err) == -1)
		return -1;

	if (nft_get_value(type, &val, out) == -1)
		return -1;

	return 0;
}

bool nft_jansson_node_exist(json_t *root, const char *node_name)
{
	return json_object_get(root, node_name) != NULL;
}

json_t *nft_jansson_create_root(const void *json, json_error_t *error,
				struct nft_parse_err *err, enum nft_parse_input input)
{
	json_t *root;

	switch (input) {
	case NFT_PARSE_BUFFER:
		root = json_loadb(json, strlen(json), 0, error);
		break;
	case NFT_PARSE_FILE:
		root = json_loadf((FILE *)json, 0, error);
		break;
	default:
		goto err;
	}

	if (root == NULL) {
		err->error = NFT_PARSE_EBADINPUT;
		err->line = error->line;
		err->column = error->column;
		err->node_name = error->source;
		goto err;
	}

	return root;
err:
	errno = EINVAL;
	return NULL;
}

json_t *nft_jansson_get_node(json_t *root, const char *node_name,
			     struct nft_parse_err *err)
{
	json_t *node;

	node = json_object_get(root, node_name);
	if (node == NULL) {
		err->error = NFT_PARSE_EMISSINGNODE;
		err->node_name = node_name;
		errno = EINVAL;
		return NULL;
	}

	return node;
}

void nft_jansson_free_root(json_t *root)
{
	json_decref(root);
}

int nft_jansson_parse_family(json_t *root, void *out, struct nft_parse_err *err)
{
	const char *str;
	int family;

	str = nft_jansson_parse_str(root, "family", err);
	if (str == NULL)
		return -1;

	family = nft_str2family(str);
	if (family < 0) {
		err->node_name = "family";
		errno = EINVAL;
		return -1;
	}

	memcpy(out, &family, sizeof(family));
	return 0;
}

int nft_jansson_parse_reg(json_t *root, const char *node_name, int type,
			  void *out, struct nft_parse_err *err)
{
	if (nft_jansson_parse_val(root, node_name, type, out, err) < 0)
		return -1;

	if (*((uint32_t *)out) > NFT_REG_MAX){
		errno = ERANGE;
		return -1;
	}

	return 0;
}

int nft_jansson_str2num(json_t *root, const char *node_name, int base,
			void *out, enum nft_type type, struct nft_parse_err *err)
{
	const char *str;

	str = nft_jansson_parse_str(root, node_name, err);
	if (str == NULL)
		return -1;

	return nft_strtoi(str, base, out, type);
}

struct nft_rule_expr *nft_jansson_expr_parse(json_t *root,
					     struct nft_parse_err *err)
{
	struct nft_rule_expr *e;
	const char *type;
	int ret;

	type = nft_jansson_parse_str(root, "type", err);
	if (type == NULL)
		return NULL;

	e = nft_rule_expr_alloc(type);
	if (e == NULL) {
		err->node_name = "type";
		return NULL;
	}

	ret = e->ops->json_parse(e, root, err);

	return ret < 0 ? NULL : e;
}

int nft_jansson_data_reg_parse(json_t *root, const char *node_name,
			       union nft_data_reg *data_reg,
			       struct nft_parse_err *err)
{
	json_t *data;
	int ret;

	data = json_object_get(root, node_name);
	if (data == NULL) {
		err->error = NFT_PARSE_EMISSINGNODE;
		err->node_name = node_name;
		errno = EINVAL;
		return -1;
	}

	data = json_object_get(data, "data_reg");
	if (data == NULL) {
		err->error = NFT_PARSE_EMISSINGNODE;
		err->node_name = "data_reg";
		errno = EINVAL;
		return -1;
	}

	ret = nft_data_reg_json_parse(data_reg, data, err);
	if (ret == DATA_NONE) {
		errno = EINVAL;
		return -1;
	}

	return ret;
}

int nft_jansson_set_elem_parse(struct nft_set_elem *e, json_t *root,
			       struct nft_parse_err *err)
{
	uint32_t uval32;
	int set_elem_data;

	if (nft_jansson_parse_val(root, "flags", NFT_TYPE_U32, &uval32, err) < 0)
		return -1;

	nft_set_elem_attr_set_u32(e, NFT_SET_ELEM_ATTR_FLAGS, uval32);

	if (nft_jansson_data_reg_parse(root, "key", &e->key, err) != DATA_VALUE)
		return -1;

	e->flags |= (1 << NFT_SET_ELEM_ATTR_KEY);

	if (nft_jansson_node_exist(root, "data")) {
		set_elem_data = nft_jansson_data_reg_parse(root, "data",
							   &e->data, err);
		switch (set_elem_data) {
		case DATA_VALUE:
			e->flags |= (1 << NFT_SET_ELEM_ATTR_DATA);
			break;
		case DATA_VERDICT:
			e->flags |= (1 << NFT_SET_ELEM_ATTR_VERDICT);
			if (e->data.chain != NULL)
				e->flags |= (1 << NFT_SET_ELEM_ATTR_CHAIN);

			break;
		case DATA_NONE:
		default:
			return -1;
		}
	}

	return 0;
}
#endif