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