xref: /freebsd/usr.sbin/iovctl/parse.c (revision c57c26179033f64c2011a2d2a904ee3fa62e826a)
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
report_config_error(const char * key,const ucl_object_t * obj,const char * type)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
add_bool_config(const char * key,const ucl_object_t * obj,nvlist_t * config)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
add_string_config(const char * key,const ucl_object_t * obj,nvlist_t * config)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
add_uint_config(const char * key,const ucl_object_t * obj,nvlist_t * config,const char * type,uint64_t max)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
add_unicast_mac_config(const char * key,const ucl_object_t * obj,nvlist_t * config)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 static void
add_vlan_config(const char * key,const ucl_object_t * obj,nvlist_t * config)164 add_vlan_config(const char *key, const ucl_object_t *obj, nvlist_t *config)
165 {
166 	int64_t val;
167 	const char *strVal = "";
168 
169 	if(ucl_object_tostring_safe(obj, &strVal)) {
170 		if (strcasecmp(strVal, "trunk") == 0) {
171 			nvlist_add_number(config, key, VF_VLAN_TRUNK);
172 			return;
173 		}
174 		report_config_error(key, obj, "vlan");
175 	}
176 
177 	if (!ucl_object_toint_safe(obj, &val))
178 		report_config_error(key, obj, "vlan");
179 
180 	if (val < 0 || val > 4095)
181 		report_config_error(key, obj, "vlan");
182 
183 	nvlist_add_number(config, key, val);
184 }
185 
186 /*
187  * Validates that the given configuration value has the right type as specified
188  * in the schema, and then adds the value to the configuration node.
189  */
190 static void
add_config(const char * key,const ucl_object_t * obj,nvlist_t * config,const nvlist_t * schema)191 add_config(const char *key, const ucl_object_t *obj, nvlist_t *config,
192     const nvlist_t *schema)
193 {
194 	const char *type;
195 
196 	type = nvlist_get_string(schema, TYPE_SCHEMA_NAME);
197 
198 	if (strcasecmp(type, "bool") == 0)
199 		add_bool_config(key, obj, config);
200 	else if (strcasecmp(type, "string") == 0)
201 		add_string_config(key, obj, config);
202 	else if (strcasecmp(type, "uint8_t") == 0)
203 		add_uint_config(key, obj, config, type, UINT8_MAX);
204 	else if (strcasecmp(type, "uint16_t") == 0)
205 		add_uint_config(key, obj, config, type, UINT16_MAX);
206 	else if (strcasecmp(type, "uint32_t") == 0)
207 		add_uint_config(key, obj, config, type, UINT32_MAX);
208 	else if (strcasecmp(type, "uint64_t") == 0)
209 		add_uint_config(key, obj, config, type, UINT64_MAX);
210 	else if (strcasecmp(type, "unicast-mac") == 0)
211 		add_unicast_mac_config(key, obj, config);
212 	else if (strcasecmp(type, "vlan") == 0)
213 		add_vlan_config(key, obj, config);
214 	else
215 		errx(1, "Unexpected type '%s' in schema", type);
216 }
217 
218 /*
219  * Parses all values specified in a device section in the configuration file,
220  * validates that the key/value pair is valid in the schema, and then adds
221  * the key/value pair to the correct subsystem in the config.
222  */
223 static void
parse_device_config(const ucl_object_t * top,nvlist_t * config,const char * subsystem,const nvlist_t * schema)224 parse_device_config(const ucl_object_t *top, nvlist_t *config,
225     const char *subsystem, const nvlist_t *schema)
226 {
227 	ucl_object_iter_t it;
228 	const ucl_object_t *obj;
229 	nvlist_t *subsystem_config, *driver_config, *iov_config;
230 	const nvlist_t *driver_schema, *iov_schema;
231 	const char *key;
232 
233 	if (nvlist_exists(config, subsystem))
234 		errx(1, "Multiple definitions of '%s' in config file",
235 		    subsystem);
236 
237 	driver_schema = nvlist_get_nvlist(schema, DRIVER_CONFIG_NAME);
238 	iov_schema = nvlist_get_nvlist(schema, IOV_CONFIG_NAME);
239 
240 	driver_config = nvlist_create(NV_FLAG_IGNORE_CASE);
241 	if (driver_config == NULL)
242 		err(1, "Could not allocate config nvlist");
243 
244 	iov_config = nvlist_create(NV_FLAG_IGNORE_CASE);
245 	if (iov_config == NULL)
246 		err(1, "Could not allocate config nvlist");
247 
248 	subsystem_config = nvlist_create(NV_FLAG_IGNORE_CASE);
249 	if (subsystem_config == NULL)
250 		err(1, "Could not allocate config nvlist");
251 
252 	it = NULL;
253 	while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
254 		key = ucl_object_key(obj);
255 
256 		if (nvlist_exists_nvlist(iov_schema, key))
257 			add_config(key, obj, iov_config,
258 			    nvlist_get_nvlist(iov_schema, key));
259 		else if (nvlist_exists_nvlist(driver_schema, key))
260 			add_config(key, obj, driver_config,
261 			    nvlist_get_nvlist(driver_schema, key));
262 		else
263 			errx(1, "%s: Invalid config key '%s'", subsystem, key);
264 	}
265 
266 	nvlist_move_nvlist(subsystem_config, DRIVER_CONFIG_NAME, driver_config);
267 	nvlist_move_nvlist(subsystem_config, IOV_CONFIG_NAME, iov_config);
268 	nvlist_move_nvlist(config, subsystem, subsystem_config);
269 }
270 
271 /*
272  * Parses the specified config file using the given schema, and returns an
273  * nvlist containing the configuration specified by the file.
274  *
275  * Exits with a message to stderr and an error if any config validation fails.
276  */
277 nvlist_t *
parse_config_file(const char * filename,const nvlist_t * schema)278 parse_config_file(const char *filename, const nvlist_t *schema)
279 {
280 	ucl_object_iter_t it;
281 	struct ucl_parser *parser;
282 	ucl_object_t *top;
283 	const ucl_object_t *obj;
284 	nvlist_t *config;
285 	const nvlist_t *pf_schema, *vf_schema;
286 	const char *errmsg, *key;
287 	regex_t vf_pat;
288 	int regex_err, processed_vf;
289 
290 	regex_err = regcomp(&vf_pat, "^"VF_PREFIX"([1-9][0-9]*|0)$",
291 	    REG_EXTENDED | REG_ICASE);
292 	if (regex_err != 0)
293 		errx(1, "Could not compile VF regex");
294 
295 	parser = ucl_parser_new(0);
296 	if (parser == NULL)
297 		err(1, "Could not allocate parser");
298 
299 	if (!ucl_parser_add_file(parser, filename))
300 		err(1, "Could not open '%s' for reading", filename);
301 
302 	errmsg = ucl_parser_get_error(parser);
303 	if (errmsg != NULL)
304 		errx(1, "Could not parse '%s': %s", filename, errmsg);
305 
306 	config = nvlist_create(NV_FLAG_IGNORE_CASE);
307 	if (config == NULL)
308 		err(1, "Could not allocate config nvlist");
309 
310 	pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
311 	vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
312 
313 	processed_vf = 0;
314 	top = ucl_parser_get_object(parser);
315 	it = NULL;
316 	while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
317 		key = ucl_object_key(obj);
318 
319 		if (strcasecmp(key, PF_CONFIG_NAME) == 0)
320 			parse_device_config(obj, config, key, pf_schema);
321 		else if (strcasecmp(key, DEFAULT_SCHEMA_NAME) == 0) {
322 			/*
323 			 * Enforce that the default section must come before all
324 			 * VF sections.  This will hopefully prevent confusing
325 			 * the user by having a default value apply to a VF
326 			 * that was declared earlier in the file.
327 			 *
328 			 * This also gives us the flexibility to extend the file
329 			 * format in the future to allow for multiple default
330 			 * sections that do only apply to subsequent VF
331 			 * sections.
332 			 */
333 			if (processed_vf)
334 				errx(1,
335 			"'default' section must precede all VF sections");
336 
337 			parse_device_config(obj, config, key, vf_schema);
338 		} else if (regexec(&vf_pat, key, 0, NULL, 0) == 0) {
339 			processed_vf = 1;
340 			parse_device_config(obj, config, key, vf_schema);
341 		} else
342 			errx(1, "Unexpected top-level node: %s", key);
343 	}
344 
345 	validate_config(config, schema, &vf_pat);
346 
347 	ucl_object_unref(top);
348 	ucl_parser_free(parser);
349 	regfree(&vf_pat);
350 
351 	return (config);
352 }
353 
354 /*
355  * Parse the PF configuration section for and return the value specified for
356  * the device parameter, or NULL if the device is not specified.
357  */
358 static const char *
find_pf_device(const ucl_object_t * pf)359 find_pf_device(const ucl_object_t *pf)
360 {
361 	ucl_object_iter_t it;
362 	const ucl_object_t *obj;
363 	const char *key, *device;
364 
365 	it = NULL;
366 	while ((obj = ucl_iterate_object(pf, &it, true)) != NULL) {
367 		key = ucl_object_key(obj);
368 
369 		if (strcasecmp(key, "device") == 0) {
370 			if (!ucl_object_tostring_safe(obj, &device))
371 				err(1,
372 				    "Config PF.device must be a string");
373 
374 			return (device);
375 		}
376 	}
377 
378 	return (NULL);
379 }
380 
381 /*
382  * Manually parse the config file looking for the name of the PF device.  We
383  * have to do this separately because we need the config schema to call the
384  * normal config file parsing code, and we need to know the name of the PF
385  * device so that we can fetch the schema from it.
386  *
387  * This will always exit on failure, so if it returns then it is guaranteed to
388  * have returned a valid device name.
389  */
390 char *
find_device(const char * filename)391 find_device(const char *filename)
392 {
393 	char *device;
394 	const char *deviceName;
395 	ucl_object_iter_t it;
396 	struct ucl_parser *parser;
397 	ucl_object_t *top;
398 	const ucl_object_t *obj;
399 	const char *errmsg, *key;
400 	int error;
401 
402 	device = NULL;
403 	deviceName = NULL;
404 
405 	parser = ucl_parser_new(0);
406 	if (parser == NULL)
407 		err(1, "Could not allocate parser");
408 
409 	if (!ucl_parser_add_file(parser, filename))
410 		err(1, "Could not open '%s' for reading", filename);
411 
412 	errmsg = ucl_parser_get_error(parser);
413 	if (errmsg != NULL)
414 		errx(1, "Could not parse '%s': %s", filename, errmsg);
415 
416 	top = ucl_parser_get_object (parser);
417 	it = NULL;
418 	while ((obj = ucl_iterate_object(top, &it, true)) != NULL) {
419 		key = ucl_object_key(obj);
420 
421 		if (strcasecmp(key, PF_CONFIG_NAME) == 0) {
422 			deviceName = find_pf_device(obj);
423 			break;
424 		}
425 	}
426 
427 	if (deviceName == NULL)
428 		errx(1, "Config file does not specify device");
429 
430 	error = asprintf(&device, "/dev/iov/%s", deviceName);
431 	if (error < 0)
432 		err(1, "Could not allocate memory for device");
433 
434 	ucl_object_unref(top);
435 	ucl_parser_free(parser);
436 
437 	return (device);
438 }
439