xref: /freebsd/usr.sbin/iovctl/parse.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * Copyright (c) 2014-2015 Sandvine Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/iov.h>
30 #include <sys/nv.h>
31 #include <net/ethernet.h>
32 
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <regex.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ucl.h>
42 #include <unistd.h>
43 
44 #include "iovctl.h"
45 
46 static void
47 report_config_error(const char *key, const ucl_object_t *obj, const char *type)
48 {
49 
50 	errx(1, "Value '%s' of key '%s' is not of type %s",
51 	    ucl_object_tostring(obj), key, type);
52 }
53 
54 /*
55  * Verifies that the value specified in the config file is a boolean value, and
56  * then adds the value to the configuration.
57  */
58 static void
59 add_bool_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
60 {
61 	bool val;
62 
63 	if (!ucl_object_toboolean_safe(obj, &val))
64 		report_config_error(key, obj, "bool");
65 
66 	nvlist_add_bool(config, key, val);
67 }
68 
69 /*
70  * Verifies that the value specified in the config file is a string, and then
71  * adds the value to the configuration.
72  */
73 static void
74 add_string_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
75 {
76 	const char *val;
77 
78 	if (!ucl_object_tostring_safe(obj, &val))
79 		report_config_error(key, obj, "string");
80 
81 	nvlist_add_string(config, key, val);
82 }
83 
84 /*
85  * Verifies that the value specified in the config file is a integer value
86  * within the specified range, and then adds the value to the configuration.
87  */
88 static void
89 add_uint_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
90     const char *type, uint64_t max)
91 {
92 	int64_t val;
93 	uint64_t uval;
94 
95 	/* I must use a signed type here as libucl doesn't provide unsigned. */
96 	if (!ucl_object_toint_safe(obj, &val))
97 		report_config_error(key, obj, type);
98 
99 	if (val < 0)
100 		report_config_error(key, obj, type);
101 
102 	uval = val;
103 	if (uval > max)
104 		report_config_error(key, obj, type);
105 
106 	nvlist_add_number(config, key, uval);
107 }
108 
109 /*
110  * Verifies that the value specified in the config file is a unicast MAC
111  * address, and then adds the value to the configuration.
112  */
113 static void
114 add_unicast_mac_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
115 {
116 	uint8_t mac[ETHER_ADDR_LEN];
117 	const char *val, *token;
118 	char *parse, *orig_parse, *tokpos, *endpos;
119 	size_t len;
120 	u_long value;
121 	int i;
122 
123 	if (!ucl_object_tostring_safe(obj, &val))
124 		report_config_error(key, obj, "unicast-mac");
125 
126 	parse = strdup(val);
127 	orig_parse = parse;
128 
129 	i = 0;
130 	while ((token = strtok_r(parse, ":", &tokpos)) != NULL) {
131 		parse = NULL;
132 
133 		len = strlen(token);
134 		if (len < 1 || len > 2)
135 			report_config_error(key, obj, "unicast-mac");
136 
137 		value = strtoul(token, &endpos, 16);
138 
139 		if (*endpos != '\0')
140 			report_config_error(key, obj, "unicast-mac");
141 
142 		if (value > UINT8_MAX)
143 			report_config_error(key, obj, "unicast-mac");
144 
145 		if (i >= ETHER_ADDR_LEN)
146 			report_config_error(key, obj, "unicast-mac");
147 
148 		mac[i] = value;
149 		i++;
150 	}
151 
152 	free(orig_parse);
153 
154 	if (i != ETHER_ADDR_LEN)
155 		report_config_error(key, obj, "unicast-mac");
156 
157 	if (ETHER_IS_MULTICAST(mac))
158 		errx(1, "Value '%s' of key '%s' is a multicast address",
159 		    ucl_object_tostring(obj), key);
160 
161 	nvlist_add_binary(config, key, mac, ETHER_ADDR_LEN);
162 }
163 
164 /*
165  * Validates that the given configuration value has the right type as specified
166  * in the schema, and then adds the value to the configuration node.
167  */
168 static void
169 add_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
170     const nvlist_t *schema)
171 {
172 	const char *type;
173 
174 	type = nvlist_get_string(schema, TYPE_SCHEMA_NAME);
175 
176 	if (strcasecmp(type, "bool") == 0)
177 		add_bool_config(key, obj, config);
178 	else if (strcasecmp(type, "string") == 0)
179 		add_string_config(key, obj, config);
180 	else if (strcasecmp(type, "uint8_t") == 0)
181 		add_uint_config(key, obj, config, type, UINT8_MAX);
182 	else if (strcasecmp(type, "uint16_t") == 0)
183 		add_uint_config(key, obj, config, type, UINT16_MAX);
184 	else if (strcasecmp(type, "uint32_t") == 0)
185 		add_uint_config(key, obj, config, type, UINT32_MAX);
186 	else if (strcasecmp(type, "uint64_t") == 0)
187 		add_uint_config(key, obj, config, type, UINT64_MAX);
188 	else if (strcasecmp(type, "unicast-mac") == 0)
189 		add_unicast_mac_config(key, obj, config);
190 	else
191 		errx(1, "Unexpected type '%s' in schema", type);
192 }
193 
194 /*
195  * Parses all values specified in a device section in the configuration file,
196  * validates that the key/value pair is valid in the schema, and then adds
197  * the key/value pair to the correct subsystem in the config.
198  */
199 static void
200 parse_device_config(const ucl_object_t *top, nvlist_t *config,
201     const char *subsystem, const nvlist_t *schema)
202 {
203 	ucl_object_iter_t it;
204 	const ucl_object_t *obj;
205 	nvlist_t *subsystem_config, *driver_config, *iov_config;
206 	const nvlist_t *driver_schema, *iov_schema;
207 	const char *key;
208 
209 	if (nvlist_exists(config, subsystem))
210 		errx(1, "Multiple definitions of '%s' in config file",
211 		    subsystem);
212 
213 	driver_schema = nvlist_get_nvlist(schema, DRIVER_CONFIG_NAME);
214 	iov_schema = nvlist_get_nvlist(schema, IOV_CONFIG_NAME);
215 
216 	driver_config = nvlist_create(NV_FLAG_IGNORE_CASE);
217 	if (driver_config == NULL)
218 		err(1, "Could not allocate config nvlist");
219 
220 	iov_config = nvlist_create(NV_FLAG_IGNORE_CASE);
221 	if (iov_config == NULL)
222 		err(1, "Could not allocate config nvlist");
223 
224 	subsystem_config = nvlist_create(NV_FLAG_IGNORE_CASE);
225 	if (subsystem_config == NULL)
226 		err(1, "Could not allocate config nvlist");
227 
228 	it = NULL;
229 	while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
230 		key = ucl_object_key(obj);
231 
232 		if (nvlist_exists_nvlist(iov_schema, key))
233 			add_config(key, obj, iov_config,
234 			    nvlist_get_nvlist(iov_schema, key));
235 		else if (nvlist_exists_nvlist(driver_schema, key))
236 			add_config(key, obj, driver_config,
237 			    nvlist_get_nvlist(driver_schema, key));
238 		else
239 			errx(1, "%s: Invalid config key '%s'", subsystem, key);
240 	}
241 
242 	nvlist_move_nvlist(subsystem_config, DRIVER_CONFIG_NAME, driver_config);
243 	nvlist_move_nvlist(subsystem_config, IOV_CONFIG_NAME, iov_config);
244 	nvlist_move_nvlist(config, subsystem, subsystem_config);
245 }
246 
247 /*
248  * Parses the specified config file using the given schema, and returns an
249  * nvlist containing the configuration specified by the file.
250  *
251  * Exits with a message to stderr and an error if any config validation fails.
252  */
253 nvlist_t *
254 parse_config_file(const char *filename, const nvlist_t *schema)
255 {
256 	ucl_object_iter_t it;
257 	struct ucl_parser *parser;
258 	ucl_object_t *top;
259 	const ucl_object_t *obj;
260 	nvlist_t *config;
261 	const nvlist_t *pf_schema, *vf_schema;
262 	const char *errmsg, *key;
263 	regex_t vf_pat;
264 	int regex_err, processed_vf;
265 
266 	regex_err = regcomp(&vf_pat, "^"VF_PREFIX"([1-9][0-9]*|0)$",
267 	    REG_EXTENDED | REG_ICASE);
268 	if (regex_err != 0)
269 		errx(1, "Could not compile VF regex");
270 
271 	parser = ucl_parser_new(0);
272 	if (parser == NULL)
273 		err(1, "Could not allocate parser");
274 
275 	if (!ucl_parser_add_file(parser, filename))
276 		err(1, "Could not open '%s' for reading", filename);
277 
278 	errmsg = ucl_parser_get_error(parser);
279 	if (errmsg != NULL)
280 		errx(1, "Could not parse '%s': %s", filename, errmsg);
281 
282 	config = nvlist_create(NV_FLAG_IGNORE_CASE);
283 	if (config == NULL)
284 		err(1, "Could not allocate config nvlist");
285 
286 	pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
287 	vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
288 
289 	processed_vf = 0;
290 	top = ucl_parser_get_object(parser);
291 	it = NULL;
292 	while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
293 		key = ucl_object_key(obj);
294 
295 		if (strcasecmp(key, PF_CONFIG_NAME) == 0)
296 			parse_device_config(obj, config, key, pf_schema);
297 		else if (strcasecmp(key, DEFAULT_SCHEMA_NAME) == 0) {
298 			/*
299 			 * Enforce that the default section must come before all
300 			 * VF sections.  This will hopefully prevent confusing
301 			 * the user by having a default value apply to a VF
302 			 * that was declared earlier in the file.
303 			 *
304 			 * This also gives us the flexibility to extend the file
305 			 * format in the future to allow for multiple default
306 			 * sections that do only apply to subsequent VF
307 			 * sections.
308 			 */
309 			if (processed_vf)
310 				errx(1,
311 			"'default' section must precede all VF sections");
312 
313 			parse_device_config(obj, config, key, vf_schema);
314 		} else if (regexec(&vf_pat, key, 0, NULL, 0) == 0) {
315 			processed_vf = 1;
316 			parse_device_config(obj, config, key, vf_schema);
317 		} else
318 			errx(1, "Unexpected top-level node: %s", key);
319 	}
320 
321 	validate_config(config, schema, &vf_pat);
322 
323 	ucl_object_unref(top);
324 	ucl_parser_free(parser);
325 	regfree(&vf_pat);
326 
327 	return (config);
328 }
329 
330 /*
331  * Parse the PF configuration section for and return the value specified for
332  * the device parameter, or NULL if the device is not specified.
333  */
334 static const char *
335 find_pf_device(const ucl_object_t *pf)
336 {
337 	ucl_object_iter_t it;
338 	const ucl_object_t *obj;
339 	const char *key, *device;
340 
341 	it = NULL;
342 	while ((obj = ucl_iterate_object(pf, &it, true)) != NULL) {
343 		key = ucl_object_key(obj);
344 
345 		if (strcasecmp(key, "device") == 0) {
346 			if (!ucl_object_tostring_safe(obj, &device))
347 				err(1,
348 				    "Config PF.device must be a string");
349 
350 			return (device);
351 		}
352 	}
353 
354 	return (NULL);
355 }
356 
357 /*
358  * Manually parse the config file looking for the name of the PF device.  We
359  * have to do this separately because we need the config schema to call the
360  * normal config file parsing code, and we need to know the name of the PF
361  * device so that we can fetch the schema from it.
362  *
363  * This will always exit on failure, so if it returns then it is guaranteed to
364  * have returned a valid device name.
365  */
366 char *
367 find_device(const char *filename)
368 {
369 	char *device;
370 	const char *deviceName;
371 	ucl_object_iter_t it;
372 	struct ucl_parser *parser;
373 	ucl_object_t *top;
374 	const ucl_object_t *obj;
375 	const char *errmsg, *key;
376 	int error;
377 
378 	device = NULL;
379 	deviceName = NULL;
380 
381 	parser = ucl_parser_new(0);
382 	if (parser == NULL)
383 		err(1, "Could not allocate parser");
384 
385 	if (!ucl_parser_add_file(parser, filename))
386 		err(1, "Could not open '%s' for reading", filename);
387 
388 	errmsg = ucl_parser_get_error(parser);
389 	if (errmsg != NULL)
390 		errx(1, "Could not parse '%s': %s", filename, errmsg);
391 
392 	top = ucl_parser_get_object (parser);
393 	it = NULL;
394 	while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
395 		key = ucl_object_key(obj);
396 
397 		if (strcasecmp(key, PF_CONFIG_NAME) == 0) {
398 			deviceName = find_pf_device(obj);
399 			break;
400 		}
401 	}
402 
403 	if (deviceName == NULL)
404 		errx(1, "Config file does not specify device");
405 
406 	error = asprintf(&device, "/dev/iov/%s", deviceName);
407 	if (error < 0)
408 		err(1, "Could not allocate memory for device");
409 
410 	ucl_object_unref(top);
411 	ucl_parser_free(parser);
412 
413 	return (device);
414 }
415