aboutgitcodelistschat:MatrixIRC
path: root/cooker/parse.c
blob: 0a87088d3119c5910e3b041602a9a0b3d5562fe0 (plain) (blame)
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
// SPDX-License-Identifier: GPL-3.0-or-later

/* seitan - Syscall Expressive Interpreter, Transformer and Notifier
 *
 * cooker/parse.c - JSON recipe parsing
 *
 * Copyright 2023 Red Hat GmbH
 * Author: Stefano Brivio <sbrivio@redhat.com>
 */

#include "parson.h"
#include "calls.h"
#include "cooker.h"
#include "gluten.h"
#include "emit.h"
#include "util.h"

#include "calls/net.h"

struct rule_parser {
	const char *type;
	int (*fn)(struct gluten_ctx *g, JSON_Value *value);
};

static int parse_match_load(struct gluten_ctx *g, struct arg *a)
{
	if (!a->size || g->match_dst[a->pos].len)
		return 0;

	g->match_dst[a->pos].offset = gluten_alloc(g, a->size);
	g->match_dst[a->pos].len = a->size;

	emit_load(g, g->match_dst[a->pos].offset, a->pos, a->size);

	return 0;
}

static long long parse_match_expr_num(struct num *desc, JSON_Value *value)
{
	const char *s = NULL;
	long long n;

	if (desc) {
		s = json_value_get_string(value);
		for (; desc->name && s && strcmp(s, desc->name); desc++);
		if (s && !desc->name)
			die("   Invalid value %s", s);

		n = desc->value;
	}

	if (!s) {
		if (json_value_get_type(value) != JSONNumber)
			die("   Invalid value type");

		n = json_value_get_number(value);
	}

	return n;
}

static void parse_match_expr_value(union desc desc, enum type type,
				   JSON_Value *value, union value *out)
{
	if (TYPE_IS_NUM(type))
		out->v_num = parse_match_expr_num(desc.d_num, value);
}

/**
 * parse_match_select() - Get description and type for selected value
 * @s:		Possible selection choices
 * @v:		Selector value
 * @type:	Type of selected value, set on return
 * @desc:	Description of selected value, set on return
 */
static void parse_match_select(struct select *s, union value v,
			       enum type *type, union desc *desc)
{
	if (TYPE_IS_NUM(s->field->type)) {
		struct select_num *d_num;

		for (d_num = s->desc.d_num; d_num->type; d_num++) {
			if (d_num->value == v.v_num) {
				*type = d_num->type;
				*desc = d_num->desc;
				return;
			}
		}

		if (!d_num->type)
			die("   No match for numeric selector %i", v.v_num);
	}

	die("   not supported yet");
}

/*
 * parse_match_arg()
 *   load argument
 *   parse_match_key()
 *     compound types? demux, parse_match_expr
 *     parse_match_expr
 *       in/all/not/false-true array: demux, parse_match_expr_{num,string}
 *       parse_match_expr_{num,string}
 *
 * at terminal values
 *   store ref *pointers*! no copies
 *   emit additional bpf statement if syscall arg is also terminal
*/
static int parse_match_key(struct gluten_ctx *g, int index, enum type type,
			   union desc desc, JSON_Value *value)
{
	JSON_Object *tmp;
	const char *ref;

	if (type == SELECT) {
		struct gluten_offset base_offset, const_offset;
		struct select *select = desc.d_select;
		struct field *field = select->field;
		JSON_Value *selector;
		union value v;

		if (!(tmp = json_value_get_object(value)))
			die("   no object for compound value");

		if (!(selector = json_object_get_value(tmp, field->name)))
			die("   missing selector for '%s'", field->name);

		parse_match_expr_value(field->desc, field->type, selector, &v);

		base_offset = g->match_dst[index].offset;
		const_offset = emit_data(g, field->type, &v);
		emit_cmp_field(g, CMP_NE, field, base_offset, const_offset,
			       NEXT_BLOCK);

		parse_match_select(select, v, &type, &desc);
	}

	if (json_value_get_type(value) == JSONObject &&
	    (tmp = json_value_get_object(value)) &&
	    (ref = json_object_get_string(tmp, "ref"))) {
		if (TYPE_IS_COMPOUND(type))
			die("Reference '%s' to compound value");

		debug("   setting reference '%s'", ref);
		value = json_object_get_value(tmp, "value");

		emit_load(g, gluten_alloc_type(g, type), index, type);
	}

	/* Nothing to match on: just store as reference */
	if (!value)
		return 0;

	switch (type) {
	case INTFLAGS:
	case LONGFLAGS:
	case U32FLAGS:
		/* fetch/combine expr algebra loop */
	case INTMASK:
		/* calculate mask first */
	case INT:
	case LONG:
	case U32:
		parse_match_expr_num(desc.d_num, value);
		//emit_cmp(...);
	default:
		;
	}

	return 0;
}

static int parse_match_arg(struct gluten_ctx *g, const char *name,
			   JSON_Value *value, struct arg *a)
{
	debug("  Parsing match argument %s", name);

	parse_match_load(g, a);
	parse_match_key(g, a->pos, a->type, a->desc, value);

	return 0;
}

static int parse_match(struct gluten_ctx *g, JSON_Object *obj, struct arg *args)
{
	unsigned count = 0;
	struct arg *a;

	for (a = args; a->name; a++) {
		JSON_Value *value;

		if ((value = json_object_get_value(obj, a->name))) {
			count++;
			parse_match_arg(g, a->name, value, a);
		}
	}

	if (count != json_object_get_count(obj))
		die("  Stray elements in match");

	return 0;
}

static int parse_matches(struct gluten_ctx *g, JSON_Value *value)
{
	JSON_Array *matches = json_value_get_array(value);
	unsigned i;

	for (i = 0; i < json_array_get_count(matches); i++) {
		JSON_Object *match, *args;
		struct call **set, *call;
		const char *name;

		g->lr = g->ip;

		match = json_array_get_object(matches, i);
		name = json_object_get_name(match, 0);
		args = json_object_get_object(match, name);
		debug(" Parsing match %i: %s", i, name);

		for (set = call_sets, call = set[0]; *set; call++) {
			if (!call->name) {
				set++;
				continue;
			}

			if (!strcmp(name, call->name)) {
				debug("  Found handler for %s", name);
				emit_nr(g, call->number);

				parse_match(g, args, call->args);
				break;
			}
		}

		if (!*set)
			die("  Unknown system call: %s", name);
	}

	return 0;
}

static int parse_call(struct gluten_ctx *g, JSON_Value *value)
{
	(void)g;
	(void)value;
	return 0;
}

static int parse_inject(struct gluten_ctx *g, JSON_Value *value)
{
	(void)g;
	(void)value;
	return 0;
}

struct rule_parser parsers[] = {
	{ "match", parse_matches },
	{ "call", parse_call },
	{ "inject", parse_inject },
	{ NULL, NULL },
};

static int parse_block(struct gluten_ctx *g, JSON_Object *block)
{
	unsigned i;

	for (i = 0; i < json_object_get_count(block); i++) {
		struct rule_parser *parser;
		JSON_Value *rule;
		const char *type;

		type = json_object_get_name(block, i);
		rule = json_object_get_value(block, type);

		for (parser = parsers; parser->type; parser++) {
			if (!strcmp(type, parser->type)) {
				parser->fn(g, rule);
				break;
			}
		}

		if (!parser->type)
			die(" Invalid rule type: \"%s\"", type);
	}

	return 0;
}

int parse_file(struct gluten_ctx *g, const char *path)
{
	JSON_Array *blocks;
	JSON_Value *root;
	JSON_Object *obj;
	unsigned i;

	root = json_parse_file_with_comments(path);
	if (json_value_get_type(root) != JSONArray)
		die("Invalid input file %s", path);

	blocks = json_value_get_array(root);
	for (i = 0; i < json_array_get_count(blocks); i++) {
		obj = json_array_get_object(blocks, i);

		debug("Parsing block %i", i);
		parse_block(g, obj);
	}

	return 0;
}