xref: /dragonfly/contrib/dhcpcd/src/if-options.c (revision 944cd60c)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2019 Roy Marples <roy@marples.name>
5  * All rights reserved
6 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 
33 #include <arpa/inet.h>
34 
35 #include <ctype.h>
36 #include <errno.h>
37 #include <getopt.h>
38 #include <grp.h>
39 #include <inttypes.h>
40 #include <limits.h>
41 #include <paths.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <time.h>
47 
48 #include "config.h"
49 #include "common.h"
50 #include "dhcp.h"
51 #include "dhcp6.h"
52 #include "dhcpcd-embedded.h"
53 #include "if.h"
54 #include "if-options.h"
55 #include "ipv4.h"
56 #include "logerr.h"
57 #include "sa.h"
58 
59 /* These options only make sense in the config file, so don't use any
60    valid short options for them */
61 #define O_BASE			MAX('z', 'Z') + 1
62 #define O_ARPING		O_BASE + 1
63 #define O_FALLBACK		O_BASE + 2
64 #define O_DESTINATION		O_BASE + 3
65 #define O_IPV6RS		O_BASE + 4
66 #define O_NOIPV6RS		O_BASE + 5
67 #define O_IPV6RA_FORK		O_BASE + 6
68 #define O_LINK_RCVBUF		O_BASE + 7
69 // unused			O_BASE + 8
70 #define O_NOALIAS		O_BASE + 9
71 #define O_IA_NA			O_BASE + 10
72 #define O_IA_TA			O_BASE + 11
73 #define O_IA_PD			O_BASE + 12
74 #define O_HOSTNAME_SHORT	O_BASE + 13
75 #define O_DEV			O_BASE + 14
76 #define O_NODEV			O_BASE + 15
77 #define O_NOIPV4		O_BASE + 16
78 #define O_NOIPV6		O_BASE + 17
79 #define O_IAID			O_BASE + 18
80 #define O_DEFINE		O_BASE + 19
81 #define O_DEFINE6		O_BASE + 20
82 #define O_EMBED			O_BASE + 21
83 #define O_ENCAP			O_BASE + 22
84 #define O_VENDOPT		O_BASE + 23
85 #define O_VENDCLASS		O_BASE + 24
86 #define O_AUTHPROTOCOL		O_BASE + 25
87 #define O_AUTHTOKEN		O_BASE + 26
88 #define O_AUTHNOTREQUIRED	O_BASE + 27
89 #define O_NODHCP		O_BASE + 28
90 #define O_NODHCP6		O_BASE + 29
91 #define O_DHCP			O_BASE + 30
92 #define O_DHCP6			O_BASE + 31
93 #define O_IPV4			O_BASE + 32
94 #define O_IPV6			O_BASE + 33
95 #define O_CONTROLGRP		O_BASE + 34
96 #define O_SLAAC			O_BASE + 35
97 #define O_GATEWAY		O_BASE + 36
98 #define O_NOUP			O_BASE + 37
99 #define O_IPV6RA_AUTOCONF	O_BASE + 38
100 #define O_IPV6RA_NOAUTOCONF	O_BASE + 39
101 #define O_REJECT		O_BASE + 40
102 #define O_BOOTP			O_BASE + 42
103 #define O_DEFINEND		O_BASE + 43
104 #define O_NODELAY		O_BASE + 44
105 #define O_INFORM6		O_BASE + 45
106 #define O_LASTLEASE_EXTEND	O_BASE + 46
107 #define O_INACTIVE		O_BASE + 47
108 #define	O_MUDURL		O_BASE + 48
109 
110 const struct option cf_options[] = {
111 	{"background",      no_argument,       NULL, 'b'},
112 	{"script",          required_argument, NULL, 'c'},
113 	{"debug",           no_argument,       NULL, 'd'},
114 	{"env",             required_argument, NULL, 'e'},
115 	{"config",          required_argument, NULL, 'f'},
116 	{"reconfigure",     no_argument,       NULL, 'g'},
117 	{"hostname",        optional_argument, NULL, 'h'},
118 	{"vendorclassid",   optional_argument, NULL, 'i'},
119 	{"logfile",         required_argument, NULL, 'j'},
120 	{"release",         no_argument,       NULL, 'k'},
121 	{"leasetime",       required_argument, NULL, 'l'},
122 	{"metric",          required_argument, NULL, 'm'},
123 	{"rebind",          no_argument,       NULL, 'n'},
124 	{"option",          required_argument, NULL, 'o'},
125 	{"persistent",      no_argument,       NULL, 'p'},
126 	{"quiet",           no_argument,       NULL, 'q'},
127 	{"request",         optional_argument, NULL, 'r'},
128 	{"inform",          optional_argument, NULL, 's'},
129 	{"inform6",         optional_argument, NULL, O_INFORM6},
130 	{"timeout",         required_argument, NULL, 't'},
131 	{"userclass",       required_argument, NULL, 'u'},
132 	{"vendor",          required_argument, NULL, 'v'},
133 	{"waitip",          optional_argument, NULL, 'w'},
134 	{"exit",            no_argument,       NULL, 'x'},
135 	{"allowinterfaces", required_argument, NULL, 'z'},
136 	{"reboot",          required_argument, NULL, 'y'},
137 	{"noarp",           no_argument,       NULL, 'A'},
138 	{"nobackground",    no_argument,       NULL, 'B'},
139 	{"nohook",          required_argument, NULL, 'C'},
140 	{"duid",            no_argument,       NULL, 'D'},
141 	{"lastlease",       no_argument,       NULL, 'E'},
142 	{"fqdn",            optional_argument, NULL, 'F'},
143 	{"nogateway",       no_argument,       NULL, 'G'},
144 	{"xidhwaddr",       no_argument,       NULL, 'H'},
145 	{"clientid",        optional_argument, NULL, 'I'},
146 	{"broadcast",       no_argument,       NULL, 'J'},
147 	{"nolink",          no_argument,       NULL, 'K'},
148 	{"noipv4ll",        no_argument,       NULL, 'L'},
149 	{"master",          no_argument,       NULL, 'M'},
150 	{"renew",           no_argument,       NULL, 'N'},
151 	{"nooption",        required_argument, NULL, 'O'},
152 	{"printpidfile",    no_argument,       NULL, 'P'},
153 	{"require",         required_argument, NULL, 'Q'},
154 	{"static",          required_argument, NULL, 'S'},
155 	{"test",            no_argument,       NULL, 'T'},
156 	{"dumplease",       no_argument,       NULL, 'U'},
157 	{"variables",       no_argument,       NULL, 'V'},
158 	{"whitelist",       required_argument, NULL, 'W'},
159 	{"blacklist",       required_argument, NULL, 'X'},
160 	{"denyinterfaces",  required_argument, NULL, 'Z'},
161 	{"oneshot",         no_argument,       NULL, '1'},
162 	{"ipv4only",        no_argument,       NULL, '4'},
163 	{"ipv6only",        no_argument,       NULL, '6'},
164 	{"arping",          required_argument, NULL, O_ARPING},
165 	{"destination",     required_argument, NULL, O_DESTINATION},
166 	{"fallback",        required_argument, NULL, O_FALLBACK},
167 	{"ipv6rs",          no_argument,       NULL, O_IPV6RS},
168 	{"noipv6rs",        no_argument,       NULL, O_NOIPV6RS},
169 	{"ipv6ra_autoconf", no_argument,       NULL, O_IPV6RA_AUTOCONF},
170 	{"ipv6ra_noautoconf", no_argument,     NULL, O_IPV6RA_NOAUTOCONF},
171 	{"ipv6ra_fork",     no_argument,       NULL, O_IPV6RA_FORK},
172 	{"ipv4",            no_argument,       NULL, O_IPV4},
173 	{"noipv4",          no_argument,       NULL, O_NOIPV4},
174 	{"ipv6",            no_argument,       NULL, O_IPV6},
175 	{"noipv6",          no_argument,       NULL, O_NOIPV6},
176 	{"noalias",         no_argument,       NULL, O_NOALIAS},
177 	{"iaid",            required_argument, NULL, O_IAID},
178 	{"ia_na",           no_argument,       NULL, O_IA_NA},
179 	{"ia_ta",           no_argument,       NULL, O_IA_TA},
180 	{"ia_pd",           no_argument,       NULL, O_IA_PD},
181 	{"hostname_short",  no_argument,       NULL, O_HOSTNAME_SHORT},
182 	{"dev",             required_argument, NULL, O_DEV},
183 	{"nodev",           no_argument,       NULL, O_NODEV},
184 	{"define",          required_argument, NULL, O_DEFINE},
185 	{"definend",        required_argument, NULL, O_DEFINEND},
186 	{"define6",         required_argument, NULL, O_DEFINE6},
187 	{"embed",           required_argument, NULL, O_EMBED},
188 	{"encap",           required_argument, NULL, O_ENCAP},
189 	{"vendopt",         required_argument, NULL, O_VENDOPT},
190 	{"vendclass",       required_argument, NULL, O_VENDCLASS},
191 	{"authprotocol",    required_argument, NULL, O_AUTHPROTOCOL},
192 	{"authtoken",       required_argument, NULL, O_AUTHTOKEN},
193 	{"noauthrequired",  no_argument,       NULL, O_AUTHNOTREQUIRED},
194 	{"dhcp",            no_argument,       NULL, O_DHCP},
195 	{"nodhcp",          no_argument,       NULL, O_NODHCP},
196 	{"dhcp6",           no_argument,       NULL, O_DHCP6},
197 	{"nodhcp6",         no_argument,       NULL, O_NODHCP6},
198 	{"controlgroup",    required_argument, NULL, O_CONTROLGRP},
199 	{"slaac",           required_argument, NULL, O_SLAAC},
200 	{"gateway",         no_argument,       NULL, O_GATEWAY},
201 	{"reject",          required_argument, NULL, O_REJECT},
202 	{"bootp",           no_argument,       NULL, O_BOOTP},
203 	{"nodelay",         no_argument,       NULL, O_NODELAY},
204 	{"noup",            no_argument,       NULL, O_NOUP},
205 	{"lastleaseextend", no_argument,       NULL, O_LASTLEASE_EXTEND},
206 	{"inactive",        no_argument,       NULL, O_INACTIVE},
207 	{"mudurl",          required_argument, NULL, O_MUDURL},
208 	{"link_rcvbuf",     required_argument, NULL, O_LINK_RCVBUF},
209 	{NULL,              0,                 NULL, '\0'}
210 };
211 
212 static const char *default_script = SCRIPT;
213 
214 static char *
215 add_environ(char ***array, const char *value, int uniq)
216 {
217 	char **newlist, **list = *array;
218 	size_t i = 0, l, lv;
219 	char *match = NULL, *p, *n;
220 
221 	match = strdup(value);
222 	if (match == NULL) {
223 		logerr(__func__);
224 		return NULL;
225 	}
226 	p = strchr(match, '=');
227 	if (p == NULL) {
228 		logerrx("%s: no assignment: %s", __func__, value);
229 		free(match);
230 		return NULL;
231 	}
232 	*p++ = '\0';
233 	l = strlen(match);
234 
235 	while (list && list[i]) {
236 		if (match && strncmp(list[i], match, l) == 0) {
237 			if (uniq) {
238 				n = strdup(value);
239 				if (n == NULL) {
240 					logerr(__func__);
241 					free(match);
242 					return NULL;
243 				}
244 				free(list[i]);
245 				list[i] = n;
246 			} else {
247 				/* Append a space and the value to it */
248 				l = strlen(list[i]);
249 				lv = strlen(p);
250 				n = realloc(list[i], l + lv + 2);
251 				if (n == NULL) {
252 					logerr(__func__);
253 					free(match);
254 					return NULL;
255 				}
256 				list[i] = n;
257 				list[i][l] = ' ';
258 				memcpy(list[i] + l + 1, p, lv);
259 				list[i][l + lv + 1] = '\0';
260 			}
261 			free(match);
262 			return list[i];
263 		}
264 		i++;
265 	}
266 
267 	free(match);
268 	n = strdup(value);
269 	if (n == NULL) {
270 		logerr(__func__);
271 		return NULL;
272 	}
273 	newlist = reallocarray(list, i + 2, sizeof(char *));
274 	if (newlist == NULL) {
275 		logerr(__func__);
276 		free(n);
277 		return NULL;
278 	}
279 	newlist[i] = n;
280 	newlist[i + 1] = NULL;
281 	*array = newlist;
282 	return newlist[i];
283 }
284 
285 #define PARSE_STRING		0
286 #define PARSE_STRING_NULL	1
287 #define PARSE_HWADDR		2
288 #define parse_string(a, b, c) parse_str((a), (b), (c), PARSE_STRING)
289 #define parse_hwaddr(a, b, c) parse_str((a), (b), (c), PARSE_HWADDR)
290 static ssize_t
291 parse_str(char *sbuf, size_t slen, const char *str, int flags)
292 {
293 	size_t l;
294 	const char *p, *end;
295 	int i;
296 	char c[4], cmd;
297 
298 	end = str + strlen(str);
299 	/* If surrounded by quotes then it's a string */
300 	if (*str == '"') {
301 		p = end - 1;
302 		if (*p == '"') {
303 			str++;
304 			end = p;
305 		}
306 	} else {
307 		l = (size_t)hwaddr_aton(NULL, str);
308 		if ((ssize_t) l != -1 && l > 1) {
309 			if (l > slen) {
310 				errno = ENOBUFS;
311 				return -1;
312 			}
313 			hwaddr_aton((uint8_t *)sbuf, str);
314 			return (ssize_t)l;
315 		}
316 	}
317 
318 	/* Process escapes */
319 	l = 0;
320 	/* If processing a string on the clientid, first byte should be
321 	 * 0 to indicate a non hardware type */
322 	if (flags == PARSE_HWADDR && *str) {
323 		if (sbuf)
324 			*sbuf++ = 0;
325 		l++;
326 	}
327 	c[3] = '\0';
328 	while (str < end) {
329 		if (++l > slen && sbuf) {
330 			errno = ENOBUFS;
331 			return -1;
332 		}
333 		if (*str == '\\') {
334 			str++;
335 			switch((cmd = *str++)) {
336 			case '\0':
337 				str--;
338 				break;
339 			case 'b':
340 				if (sbuf)
341 					*sbuf++ = '\b';
342 				break;
343 			case 'n':
344 				if (sbuf)
345 					*sbuf++ = '\n';
346 				break;
347 			case 'r':
348 				if (sbuf)
349 					*sbuf++ = '\r';
350 				break;
351 			case 't':
352 				if (sbuf)
353 					*sbuf++ = '\t';
354 				break;
355 			case 'x':
356 				/* Grab a hex code */
357 				c[1] = '\0';
358 				for (i = 0; i < 2; i++) {
359 					if (isxdigit((unsigned char)*str) == 0)
360 						break;
361 					c[i] = *str++;
362 				}
363 				if (c[1] != '\0' && sbuf) {
364 					c[2] = '\0';
365 					*sbuf++ = (char)strtol(c, NULL, 16);
366 				} else
367 					l--;
368 				break;
369 			case '0':
370 				/* Grab an octal code */
371 				c[2] = '\0';
372 				for (i = 0; i < 3; i++) {
373 					if (*str < '0' || *str > '7')
374 						break;
375 					c[i] = *str++;
376 				}
377 				if (c[2] != '\0' && sbuf) {
378 					i = (int)strtol(c, NULL, 8);
379 					if (i > 255)
380 						i = 255;
381 					*sbuf ++= (char)i;
382 				} else
383 					l--;
384 				break;
385 			default:
386 				if (sbuf)
387 					*sbuf++ = cmd;
388 				break;
389 			}
390 		} else {
391 			if (sbuf)
392 				*sbuf++ = *str;
393 			str++;
394 		}
395 	}
396 	if (flags == PARSE_STRING_NULL && sbuf)
397 		*sbuf = '\0';
398 	return (ssize_t)l;
399 }
400 
401 static int
402 parse_iaid1(uint8_t *iaid, const char *arg, size_t len, int n)
403 {
404 	int e;
405 	uint32_t narg;
406 	ssize_t s;
407 
408 	narg = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e);
409 	if (e == 0) {
410 		if (n)
411 			narg = htonl(narg);
412 		memcpy(iaid, &narg, sizeof(narg));
413 		return 0;
414 	}
415 
416 	if ((s = parse_string((char *)iaid, len, arg)) < 1)
417 		return -1;
418 	if (s < 4)
419 		iaid[3] = '\0';
420 	if (s < 3)
421 		iaid[2] = '\0';
422 	if (s < 2)
423 		iaid[1] = '\0';
424 	return 0;
425 }
426 
427 static int
428 parse_iaid(uint8_t *iaid, const char *arg, size_t len)
429 {
430 
431 	return parse_iaid1(iaid, arg, len, 1);
432 }
433 
434 #ifdef AUTH
435 static int
436 parse_uint32(uint32_t *i, const char *arg)
437 {
438 
439 	return parse_iaid1((uint8_t *)i, arg, sizeof(uint32_t), 0);
440 }
441 #endif
442 
443 static char **
444 splitv(int *argc, char **argv, const char *arg)
445 {
446 	char **n, **v = argv;
447 	char *o = strdup(arg), *p, *t, *nt;
448 
449 	if (o == NULL) {
450 		logerr(__func__);
451 		return v;
452 	}
453 	p = o;
454 	while ((t = strsep(&p, ", "))) {
455 		nt = strdup(t);
456 		if (nt == NULL) {
457 			logerr(__func__);
458 			free(o);
459 			return v;
460 		}
461 		n = reallocarray(v, (size_t)(*argc) + 1, sizeof(char *));
462 		if (n == NULL) {
463 			logerr(__func__);
464 			free(o);
465 			free(nt);
466 			return v;
467 		}
468 		v = n;
469 		v[(*argc)++] = nt;
470 	}
471 	free(o);
472 	return v;
473 }
474 
475 #ifdef INET
476 static int
477 parse_addr(struct in_addr *addr, struct in_addr *net, const char *arg)
478 {
479 	char *p;
480 
481 	if (arg == NULL || *arg == '\0') {
482 		if (addr != NULL)
483 			addr->s_addr = 0;
484 		if (net != NULL)
485 			net->s_addr = 0;
486 		return 0;
487 	}
488 	if ((p = strchr(arg, '/')) != NULL) {
489 		int e;
490 		intmax_t i;
491 
492 		*p++ = '\0';
493 		i = strtoi(p, NULL, 10, 0, 32, &e);
494 		if (e != 0 ||
495 		    (net != NULL && inet_cidrtoaddr((int)i, net) != 0))
496 		{
497 			logerrx("`%s' is not a valid CIDR", p);
498 			return -1;
499 		}
500 	}
501 
502 	if (addr != NULL && inet_aton(arg, addr) == 0) {
503 		logerrx("`%s' is not a valid IP address", arg);
504 		return -1;
505 	}
506 	if (p != NULL)
507 		*--p = '/';
508 	else if (net != NULL && addr != NULL)
509 		net->s_addr = ipv4_getnetmask(addr->s_addr);
510 	return 0;
511 }
512 #else
513 static int
514 parse_addr(__unused struct in_addr *addr, __unused struct in_addr *net,
515     __unused const char *arg)
516 {
517 
518 	logerrx("No IPv4 support");
519 	return -1;
520 }
521 #endif
522 
523 static const char *
524 set_option_space(struct dhcpcd_ctx *ctx,
525     const char *arg,
526     const struct dhcp_opt **d, size_t *dl,
527     const struct dhcp_opt **od, size_t *odl,
528     struct if_options *ifo,
529     uint8_t *request[], uint8_t *require[], uint8_t *no[], uint8_t *reject[])
530 {
531 
532 #if !defined(INET) && !defined(INET6)
533 	UNUSED(ctx);
534 #endif
535 
536 #ifdef INET6
537 	if (strncmp(arg, "nd_", strlen("nd_")) == 0) {
538 		*d = ctx->nd_opts;
539 		*dl = ctx->nd_opts_len;
540 		*od = ifo->nd_override;
541 		*odl = ifo->nd_override_len;
542 		*request = ifo->requestmasknd;
543 		*require = ifo->requiremasknd;
544 		*no = ifo->nomasknd;
545 		*reject = ifo->rejectmasknd;
546 		return arg + strlen("nd_");
547 	}
548 
549 #ifdef DHCP6
550 	if (strncmp(arg, "dhcp6_", strlen("dhcp6_")) == 0) {
551 		*d = ctx->dhcp6_opts;
552 		*dl = ctx->dhcp6_opts_len;
553 		*od = ifo->dhcp6_override;
554 		*odl = ifo->dhcp6_override_len;
555 		*request = ifo->requestmask6;
556 		*require = ifo->requiremask6;
557 		*no = ifo->nomask6;
558 		*reject = ifo->rejectmask6;
559 		return arg + strlen("dhcp6_");
560 	}
561 #endif
562 #endif
563 
564 #ifdef INET
565 	*d = ctx->dhcp_opts;
566 	*dl = ctx->dhcp_opts_len;
567 	*od = ifo->dhcp_override;
568 	*odl = ifo->dhcp_override_len;
569 #else
570 	*d = NULL;
571 	*dl = 0;
572 	*od = NULL;
573 	*odl = 0;
574 #endif
575 	*request = ifo->requestmask;
576 	*require = ifo->requiremask;
577 	*no = ifo->nomask;
578 	*reject = ifo->rejectmask;
579 	return arg;
580 }
581 
582 void
583 free_dhcp_opt_embenc(struct dhcp_opt *opt)
584 {
585 	size_t i;
586 	struct dhcp_opt *o;
587 
588 	free(opt->var);
589 
590 	for (i = 0, o = opt->embopts; i < opt->embopts_len; i++, o++)
591 		free_dhcp_opt_embenc(o);
592 	free(opt->embopts);
593 	opt->embopts_len = 0;
594 	opt->embopts = NULL;
595 
596 	for (i = 0, o = opt->encopts; i < opt->encopts_len; i++, o++)
597 		free_dhcp_opt_embenc(o);
598 	free(opt->encopts);
599 	opt->encopts_len = 0;
600 	opt->encopts = NULL;
601 }
602 
603 static char *
604 strwhite(const char *s)
605 {
606 
607 	if (s == NULL)
608 		return NULL;
609 	while (*s != ' ' && *s != '\t') {
610 		if (*s == '\0')
611 			return NULL;
612 		s++;
613 	}
614 	return UNCONST(s);
615 }
616 
617 static char *
618 strskipwhite(const char *s)
619 {
620 
621 	if (s == NULL || *s == '\0')
622 		return NULL;
623 	while (*s == ' ' || *s == '\t') {
624 		s++;
625 		if (*s == '\0')
626 			return NULL;
627 	}
628 	return UNCONST(s);
629 }
630 
631 #ifdef AUTH
632 /* Find the end pointer of a string. */
633 static char *
634 strend(const char *s)
635 {
636 
637 	s = strskipwhite(s);
638 	if (s == NULL)
639 		return NULL;
640 	if (*s != '"')
641 		return strchr(s, ' ');
642 	s++;
643 	for (; *s != '"' ; s++) {
644 		if (*s == '\0')
645 			return NULL;
646 		if (*s == '\\') {
647 			if (*(++s) == '\0')
648 				return NULL;
649 		}
650 	}
651 	return UNCONST(++s);
652 }
653 #endif
654 
655 static int
656 parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
657     int opt, const char *arg, struct dhcp_opt **ldop, struct dhcp_opt **edop)
658 {
659 	int e, i, t;
660 	long l;
661 	unsigned long u;
662 	char *p = NULL, *bp, *fp, *np;
663 	ssize_t s;
664 	struct in_addr addr, addr2;
665 	in_addr_t *naddr;
666 	struct rt *rt;
667 	const struct dhcp_opt *d, *od;
668 	uint8_t *request, *require, *no, *reject;
669 	struct dhcp_opt **dop, *ndop;
670 	size_t *dop_len, dl, odl;
671 	struct vivco *vivco;
672 	struct group *grp;
673 #ifdef AUTH
674 	struct token *token;
675 #endif
676 #ifdef _REENTRANT
677 	struct group grpbuf;
678 #endif
679 #ifdef DHCP6
680 	size_t sl;
681 	struct if_ia *ia;
682 	uint8_t iaid[4];
683 #ifndef SMALL
684 	struct if_sla *sla, *slap;
685 #endif
686 #endif
687 
688 	dop = NULL;
689 	dop_len = NULL;
690 #ifdef INET6
691 	i = 0;
692 #endif
693 
694 /* Add a guard for static analysers.
695  * This should not be needed really because of the argument_required option
696  * in the options declaration above. */
697 #define ARG_REQUIRED if (arg == NULL) goto arg_required
698 
699 	switch(opt) {
700 	case 'f': /* FALLTHROUGH */
701 	case 'g': /* FALLTHROUGH */
702 	case 'n': /* FALLTHROUGH */
703 	case 'q': /* FALLTHROUGH */
704 	case 'x': /* FALLTHROUGH */
705 	case 'N': /* FALLTHROUGH */
706 	case 'P': /* FALLTHROUGH */
707 	case 'T': /* FALLTHROUGH */
708 	case 'U': /* FALLTHROUGH */
709 	case 'V': /* We need to handle non interface options */
710 		break;
711 	case 'b':
712 		ifo->options |= DHCPCD_BACKGROUND;
713 		break;
714 	case 'c':
715 		ARG_REQUIRED;
716 		if (ifo->script != default_script)
717 			free(ifo->script);
718 		s = parse_str(NULL, 0, arg, PARSE_STRING_NULL);
719 		if (s == 0) {
720 			ifo->script = NULL;
721 			break;
722 		}
723 		dl = (size_t)s;
724 		if (s == -1 || (ifo->script = malloc(dl)) == NULL) {
725 			ifo->script = NULL;
726 			logerr(__func__);
727 			return -1;
728 		}
729 		s = parse_str(ifo->script, dl, arg, PARSE_STRING_NULL);
730 		if (s == -1 ||
731 		    ifo->script[0] == '\0' ||
732 		    strcmp(ifo->script, "/dev/null") == 0)
733 		{
734 			free(ifo->script);
735 			ifo->script = NULL;
736 		}
737 		break;
738 	case 'd':
739 		ifo->options |= DHCPCD_DEBUG;
740 		break;
741 	case 'e':
742 		ARG_REQUIRED;
743 		add_environ(&ifo->environ, arg, 1);
744 		break;
745 	case 'h':
746 		if (!arg) {
747 			ifo->options |= DHCPCD_HOSTNAME;
748 			break;
749 		}
750 		s = parse_string(ifo->hostname, HOSTNAME_MAX_LEN, arg);
751 		if (s == -1) {
752 			logerr("%s: hostname", __func__);
753 			return -1;
754 		}
755 		if (s != 0 && ifo->hostname[0] == '.') {
756 			logerrx("hostname cannot begin with .");
757 			return -1;
758 		}
759 		ifo->hostname[s] = '\0';
760 		if (ifo->hostname[0] == '\0')
761 			ifo->options &= ~DHCPCD_HOSTNAME;
762 		else
763 			ifo->options |= DHCPCD_HOSTNAME;
764 		break;
765 	case 'i':
766 		if (arg)
767 			s = parse_string((char *)ifo->vendorclassid + 1,
768 			    VENDORCLASSID_MAX_LEN, arg);
769 		else
770 			s = 0;
771 		if (s == -1) {
772 			logerr("vendorclassid");
773 			return -1;
774 		}
775 		*ifo->vendorclassid = (uint8_t)s;
776 		break;
777 	case 'j':
778 		ARG_REQUIRED;
779 		/* per interface logging is not supported
780 		 * don't want to overide the commandline */
781 		if (ifname == NULL && ctx->logfile == NULL) {
782 			logclose();
783 			ctx->logfile = strdup(arg);
784 			logopen(ctx->logfile);
785 		}
786 		break;
787 	case 'k':
788 		ifo->options |= DHCPCD_RELEASE;
789 		break;
790 	case 'l':
791 		ARG_REQUIRED;
792 		ifo->leasetime = (uint32_t)strtou(arg, NULL,
793 		    0, 0, UINT32_MAX, &e);
794 		if (e) {
795 			logerrx("failed to convert leasetime %s", arg);
796 			return -1;
797 		}
798 		break;
799 	case 'm':
800 		ARG_REQUIRED;
801 		ifo->metric = (int)strtoi(arg, NULL, 0, 0, INT32_MAX, &e);
802 		if (e) {
803 			logerrx("failed to convert metric %s", arg);
804 			return -1;
805 		}
806 		break;
807 	case 'o':
808 		ARG_REQUIRED;
809 		arg = set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
810 		    &request, &require, &no, &reject);
811 		if (make_option_mask(d, dl, od, odl, request, arg, 1) != 0 ||
812 		    make_option_mask(d, dl, od, odl, no, arg, -1) != 0 ||
813 		    make_option_mask(d, dl, od, odl, reject, arg, -1) != 0)
814 		{
815 			logerrx("unknown option `%s'", arg);
816 			return -1;
817 		}
818 		break;
819 	case O_REJECT:
820 		ARG_REQUIRED;
821 		arg = set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
822 		    &request, &require, &no, &reject);
823 		if (make_option_mask(d, dl, od, odl, reject, arg, 1) != 0 ||
824 		    make_option_mask(d, dl, od, odl, request, arg, -1) != 0 ||
825 		    make_option_mask(d, dl, od, odl, require, arg, -1) != 0)
826 		{
827 			logerrx("unknown option `%s'", arg);
828 			return -1;
829 		}
830 		break;
831 	case 'p':
832 		ifo->options |= DHCPCD_PERSISTENT;
833 		break;
834 	case 'r':
835 		if (parse_addr(&ifo->req_addr, NULL, arg) != 0)
836 			return -1;
837 		ifo->options |= DHCPCD_REQUEST;
838 		ifo->req_mask.s_addr = 0;
839 		break;
840 	case 's':
841 		if (arg && *arg != '\0') {
842 			/* Strip out a broadcast address */
843 			p = strchr(arg, '/');
844 			if (p != NULL) {
845 				p = strchr(p + 1, '/');
846 				if (p != NULL)
847 					*p = '\0';
848 			}
849 			i = parse_addr(&ifo->req_addr, &ifo->req_mask, arg);
850 			if (p != NULL) {
851 				/* Ensure the original string is preserved */
852 				*p++ = '/';
853 				if (i == 0)
854 					i = parse_addr(&ifo->req_brd, NULL, p);
855 			}
856 			if (i != 0)
857 				return -1;
858 		} else {
859 			ifo->req_addr.s_addr = 0;
860 			ifo->req_mask.s_addr = 0;
861 		}
862 		ifo->options |= DHCPCD_INFORM | DHCPCD_PERSISTENT;
863 		ifo->options &= ~DHCPCD_STATIC;
864 		break;
865 	case O_INFORM6:
866 		ifo->options |= DHCPCD_INFORM6;
867 		break;
868 	case 't':
869 		ARG_REQUIRED;
870 		ifo->timeout = (time_t)strtoi(arg, NULL, 0, 0, INT32_MAX, &e);
871 		if (e) {
872 			logerrx("failed to convert timeout %s", arg);
873 			return -1;
874 		}
875 		break;
876 	case 'u':
877 		s = USERCLASS_MAX_LEN - ifo->userclass[0] - 1;
878 		s = parse_string((char *)ifo->userclass +
879 		    ifo->userclass[0] + 2, (size_t)s, arg);
880 		if (s == -1) {
881 			logerr("userclass");
882 			return -1;
883 		}
884 		if (s != 0) {
885 			ifo->userclass[ifo->userclass[0] + 1] = (uint8_t)s;
886 			ifo->userclass[0] = (uint8_t)(ifo->userclass[0] + s +1);
887 		}
888 		break;
889 	case 'v':
890 		ARG_REQUIRED;
891 		p = strchr(arg, ',');
892 		if (!p || !p[1]) {
893 			logerrx("invalid vendor format: %s", arg);
894 			return -1;
895 		}
896 
897 		/* If vendor starts with , then it is not encapsulated */
898 		if (p == arg) {
899 			arg++;
900 			s = parse_string((char *)ifo->vendor + 1,
901 			    VENDOR_MAX_LEN, arg);
902 			if (s == -1) {
903 				logerr("vendor");
904 				return -1;
905 			}
906 			ifo->vendor[0] = (uint8_t)s;
907 			ifo->options |= DHCPCD_VENDORRAW;
908 			break;
909 		}
910 
911 		/* Encapsulated vendor options */
912 		if (ifo->options & DHCPCD_VENDORRAW) {
913 			ifo->options &= ~DHCPCD_VENDORRAW;
914 			ifo->vendor[0] = 0;
915 		}
916 
917 		/* Strip and preserve the comma */
918 		*p = '\0';
919 		i = (int)strtoi(arg, NULL, 0, 1, 254, &e);
920 		*p = ',';
921 		if (e) {
922 			logerrx("vendor option should be between"
923 			    " 1 and 254 inclusive");
924 			return -1;
925 		}
926 
927 		arg = p + 1;
928 		s = VENDOR_MAX_LEN - ifo->vendor[0] - 2;
929 		if (inet_aton(arg, &addr) == 1) {
930 			if (s < 6) {
931 				s = -1;
932 				errno = ENOBUFS;
933 			} else {
934 				memcpy(ifo->vendor + ifo->vendor[0] + 3,
935 				    &addr.s_addr, sizeof(addr.s_addr));
936 				s = sizeof(addr.s_addr);
937 			}
938 		} else {
939 			s = parse_string((char *)ifo->vendor +
940 			    ifo->vendor[0] + 3, (size_t)s, arg);
941 		}
942 		if (s == -1) {
943 			logerr("vendor");
944 			return -1;
945 		}
946 		if (s != 0) {
947 			ifo->vendor[ifo->vendor[0] + 1] = (uint8_t)i;
948 			ifo->vendor[ifo->vendor[0] + 2] = (uint8_t)s;
949 			ifo->vendor[0] = (uint8_t)(ifo->vendor[0] + s + 2);
950 		}
951 		break;
952 	case 'w':
953 		ifo->options |= DHCPCD_WAITIP;
954 		if (arg != NULL && arg[0] != '\0') {
955 			if (arg[0] == '4' || arg[1] == '4')
956 				ifo->options |= DHCPCD_WAITIP4;
957 			if (arg[0] == '6' || arg[1] == '6')
958 				ifo->options |= DHCPCD_WAITIP6;
959 		}
960 		break;
961 	case 'y':
962 		ARG_REQUIRED;
963 		ifo->reboot = (time_t)strtoi(arg, NULL, 0, 0, UINT32_MAX, &e);
964 		if (e) {
965 			logerr("failed to convert reboot %s", arg);
966 			return -1;
967 		}
968 		break;
969 	case 'z':
970 		ARG_REQUIRED;
971 		if (ifname == NULL)
972 			ctx->ifav = splitv(&ctx->ifac, ctx->ifav, arg);
973 		break;
974 	case 'A':
975 		ifo->options &= ~DHCPCD_ARP;
976 		/* IPv4LL requires ARP */
977 		ifo->options &= ~DHCPCD_IPV4LL;
978 		break;
979 	case 'B':
980 		ifo->options &= ~DHCPCD_DAEMONISE;
981 		break;
982 	case 'C':
983 		ARG_REQUIRED;
984 		/* Commas to spaces for shell */
985 		while ((p = strchr(arg, ',')))
986 			*p = ' ';
987 		dl = strlen("skip_hooks=") + strlen(arg) + 1;
988 		p = malloc(sizeof(char) * dl);
989 		if (p == NULL) {
990 			logerr(__func__);
991 			return -1;
992 		}
993 		snprintf(p, dl, "skip_hooks=%s", arg);
994 		add_environ(&ifo->environ, p, 0);
995 		free(p);
996 		break;
997 	case 'D':
998 		ifo->options |= DHCPCD_CLIENTID | DHCPCD_DUID;
999 		break;
1000 	case 'E':
1001 		ifo->options |= DHCPCD_LASTLEASE;
1002 		break;
1003 	case 'F':
1004 		if (!arg) {
1005 			ifo->fqdn = FQDN_BOTH;
1006 			break;
1007 		}
1008 		if (strcmp(arg, "none") == 0)
1009 			ifo->fqdn = FQDN_NONE;
1010 		else if (strcmp(arg, "ptr") == 0)
1011 			ifo->fqdn = FQDN_PTR;
1012 		else if (strcmp(arg, "both") == 0)
1013 			ifo->fqdn = FQDN_BOTH;
1014 		else if (strcmp(arg, "disable") == 0)
1015 			ifo->fqdn = FQDN_DISABLE;
1016 		else {
1017 			logerrx("invalid value `%s' for FQDN", arg);
1018 			return -1;
1019 		}
1020 		break;
1021 	case 'G':
1022 		ifo->options &= ~DHCPCD_GATEWAY;
1023 		break;
1024 	case 'H':
1025 		ifo->options |= DHCPCD_XID_HWADDR;
1026 		break;
1027 	case 'I':
1028 		/* Strings have a type of 0 */;
1029 		ifo->clientid[1] = 0;
1030 		if (arg)
1031 			s = parse_hwaddr((char *)ifo->clientid + 1,
1032 			    CLIENTID_MAX_LEN, arg);
1033 		else
1034 			s = 0;
1035 		if (s == -1) {
1036 			logerr("clientid");
1037 			return -1;
1038 		}
1039 		ifo->options |= DHCPCD_CLIENTID;
1040 		ifo->clientid[0] = (uint8_t)s;
1041 		break;
1042 	case 'J':
1043 		ifo->options |= DHCPCD_BROADCAST;
1044 		break;
1045 	case 'K':
1046 		ifo->options &= ~DHCPCD_LINK;
1047 		break;
1048 	case 'L':
1049 		ifo->options &= ~DHCPCD_IPV4LL;
1050 		break;
1051 	case 'M':
1052 		ifo->options |= DHCPCD_MASTER;
1053 		break;
1054 	case 'O':
1055 		ARG_REQUIRED;
1056 		arg = set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
1057 		    &request, &require, &no, &reject);
1058 		if (make_option_mask(d, dl, od, odl, request, arg, -1) != 0 ||
1059 		    make_option_mask(d, dl, od, odl, require, arg, -1) != 0 ||
1060 		    make_option_mask(d, dl, od, odl, no, arg, 1) != 0)
1061 		{
1062 			logerrx("unknown option `%s'", arg);
1063 			return -1;
1064 		}
1065 		break;
1066 	case 'Q':
1067 		ARG_REQUIRED;
1068 		arg = set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
1069 		    &request, &require, &no, &reject);
1070 		if (make_option_mask(d, dl, od, odl, require, arg, 1) != 0 ||
1071 		    make_option_mask(d, dl, od, odl, request, arg, 1) != 0 ||
1072 		    make_option_mask(d, dl, od, odl, no, arg, -1) != 0 ||
1073 		    make_option_mask(d, dl, od, odl, reject, arg, -1) != 0)
1074 		{
1075 			logerrx("unknown option `%s'", arg);
1076 			return -1;
1077 		}
1078 		break;
1079 	case 'S':
1080 		ARG_REQUIRED;
1081 		p = strchr(arg, '=');
1082 		if (p == NULL) {
1083 			logerrx("static assignment required");
1084 			return -1;
1085 		}
1086 		p++;
1087 		if (strncmp(arg, "ip_address=", strlen("ip_address=")) == 0) {
1088 			if (parse_addr(&ifo->req_addr,
1089 			    ifo->req_mask.s_addr == 0 ? &ifo->req_mask : NULL,
1090 			    p) != 0)
1091 				return -1;
1092 
1093 			ifo->options |= DHCPCD_STATIC;
1094 			ifo->options &= ~DHCPCD_INFORM;
1095 		} else if (strncmp(arg, "subnet_mask=",
1096 		    strlen("subnet_mask=")) == 0)
1097 		{
1098 			if (parse_addr(&ifo->req_mask, NULL, p) != 0)
1099 				return -1;
1100 		} else if (strncmp(arg, "broadcast_address=",
1101 		    strlen("broadcast_address=")) == 0)
1102 		{
1103 			if (parse_addr(&ifo->req_brd, NULL, p) != 0)
1104 				return -1;
1105 		} else if (strncmp(arg, "routes=", strlen("routes=")) == 0 ||
1106 		    strncmp(arg, "static_routes=",
1107 		        strlen("static_routes=")) == 0 ||
1108 		    strncmp(arg, "classless_static_routes=",
1109 		        strlen("classless_static_routes=")) == 0 ||
1110 		    strncmp(arg, "ms_classless_static_routes=",
1111 		        strlen("ms_classless_static_routes=")) == 0)
1112 		{
1113 			struct in_addr addr3;
1114 
1115 			fp = np = strwhite(p);
1116 			if (np == NULL) {
1117 				logerrx("all routes need a gateway");
1118 				return -1;
1119 			}
1120 			*np++ = '\0';
1121 			np = strskipwhite(np);
1122 			if (parse_addr(&addr, &addr2, p) == -1 ||
1123 			    parse_addr(&addr3, NULL, np) == -1)
1124 			{
1125 				*fp = ' ';
1126 				return -1;
1127 			}
1128 			*fp = ' ';
1129 			if ((rt = rt_new0(ctx)) == NULL)
1130 				return -1;
1131 			sa_in_init(&rt->rt_dest, &addr);
1132 			sa_in_init(&rt->rt_netmask, &addr2);
1133 			sa_in_init(&rt->rt_gateway, &addr3);
1134 			if (rt_proto_add_ctx(&ifo->routes, rt, ctx))
1135 				add_environ(&ifo->config, arg, 0);
1136 		} else if (strncmp(arg, "routers=", strlen("routers=")) == 0) {
1137 			if (parse_addr(&addr, NULL, p) == -1)
1138 				return -1;
1139 			if ((rt = rt_new0(ctx)) == NULL)
1140 				return -1;
1141 			addr2.s_addr = INADDR_ANY;
1142 			sa_in_init(&rt->rt_dest, &addr2);
1143 			sa_in_init(&rt->rt_netmask, &addr2);
1144 			sa_in_init(&rt->rt_gateway, &addr);
1145 			if (rt_proto_add_ctx(&ifo->routes, rt, ctx))
1146 				add_environ(&ifo->config, arg, 0);
1147 		} else if (strncmp(arg, "interface_mtu=",
1148 		    strlen("interface_mtu=")) == 0 ||
1149 		    strncmp(arg, "mtu=", strlen("mtu=")) == 0)
1150 		{
1151 			ifo->mtu = (unsigned int)strtou(p, NULL, 0,
1152 			    MTU_MIN, MTU_MAX, &e);
1153 			if (e) {
1154 				logerrx("invalid MTU %s", p);
1155 				return -1;
1156 			}
1157 		} else if (strncmp(arg, "ip6_address=", strlen("ip6_address=")) == 0) {
1158 			np = strchr(p, '/');
1159 			if (np)
1160 				*np++ = '\0';
1161 			if (inet_pton(AF_INET6, p, &ifo->req_addr6) == 1) {
1162 				if (np) {
1163 					ifo->req_prefix_len = (uint8_t)strtou(np,
1164 					    NULL, 0, 0, 128, &e);
1165 					if (e) {
1166 						logerrx("%s: failed to "
1167 						    "convert prefix len",
1168 						    ifname);
1169 						return -1;
1170 					}
1171 				} else
1172 					ifo->req_prefix_len = 128;
1173 			}
1174 		} else
1175 			add_environ(&ifo->config, arg, 1);
1176 		break;
1177 	case 'W':
1178 		if (parse_addr(&addr, &addr2, arg) != 0)
1179 			return -1;
1180 		if (strchr(arg, '/') == NULL)
1181 			addr2.s_addr = INADDR_BROADCAST;
1182 		naddr = reallocarray(ifo->whitelist,
1183 		    ifo->whitelist_len + 2, sizeof(in_addr_t));
1184 		if (naddr == NULL) {
1185 			logerr(__func__);
1186 			return -1;
1187 		}
1188 		ifo->whitelist = naddr;
1189 		ifo->whitelist[ifo->whitelist_len++] = addr.s_addr;
1190 		ifo->whitelist[ifo->whitelist_len++] = addr2.s_addr;
1191 		break;
1192 	case 'X':
1193 		if (parse_addr(&addr, &addr2, arg) != 0)
1194 			return -1;
1195 		if (strchr(arg, '/') == NULL)
1196 			addr2.s_addr = INADDR_BROADCAST;
1197 		naddr = reallocarray(ifo->blacklist,
1198 		    ifo->blacklist_len + 2, sizeof(in_addr_t));
1199 		if (naddr == NULL) {
1200 			logerr(__func__);
1201 			return -1;
1202 		}
1203 		ifo->blacklist = naddr;
1204 		ifo->blacklist[ifo->blacklist_len++] = addr.s_addr;
1205 		ifo->blacklist[ifo->blacklist_len++] = addr2.s_addr;
1206 		break;
1207 	case 'Z':
1208 		ARG_REQUIRED;
1209 		if (ifname == NULL)
1210 			ctx->ifdv = splitv(&ctx->ifdc, ctx->ifdv, arg);
1211 		break;
1212 	case '1':
1213 		ifo->options |= DHCPCD_ONESHOT;
1214 		break;
1215 	case '4':
1216 		ifo->options &= ~DHCPCD_IPV6;
1217 		ifo->options |= DHCPCD_IPV4;
1218 		break;
1219 	case '6':
1220 		ifo->options &= ~DHCPCD_IPV4;
1221 		ifo->options |= DHCPCD_IPV6;
1222 		break;
1223 	case O_IPV4:
1224 		ifo->options |= DHCPCD_IPV4;
1225 		break;
1226 	case O_NOIPV4:
1227 		ifo->options &= ~DHCPCD_IPV4;
1228 		break;
1229 	case O_IPV6:
1230 		ifo->options |= DHCPCD_IPV6;
1231 		break;
1232 	case O_NOIPV6:
1233 		ifo->options &= ~DHCPCD_IPV6;
1234 		break;
1235 #ifdef INET
1236 	case O_ARPING:
1237 		while (arg != NULL) {
1238 			fp = strwhite(arg);
1239 			if (fp)
1240 				*fp++ = '\0';
1241 			if (parse_addr(&addr, NULL, arg) != 0)
1242 				return -1;
1243 			naddr = reallocarray(ifo->arping,
1244 			    (size_t)ifo->arping_len + 1, sizeof(in_addr_t));
1245 			if (naddr == NULL) {
1246 				logerr(__func__);
1247 				return -1;
1248 			}
1249 			ifo->arping = naddr;
1250 			ifo->arping[ifo->arping_len++] = addr.s_addr;
1251 			arg = strskipwhite(fp);
1252 		}
1253 		break;
1254 	case O_DESTINATION:
1255 		ARG_REQUIRED;
1256 		arg = set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
1257 		    &request, &require, &no, &reject);
1258 		if (make_option_mask(d, dl, od, odl,
1259 		    ifo->dstmask, arg, 2) != 0)
1260 		{
1261 			if (errno == EINVAL)
1262 				logerrx("option `%s' does not take"
1263 				    " an IPv4 address", arg);
1264 			else
1265 				logerrx("unknown option `%s'", arg);
1266 			return -1;
1267 		}
1268 		break;
1269 	case O_FALLBACK:
1270 		ARG_REQUIRED;
1271 		free(ifo->fallback);
1272 		ifo->fallback = strdup(arg);
1273 		if (ifo->fallback == NULL) {
1274 			logerrx(__func__);
1275 			return -1;
1276 		}
1277 		break;
1278 #endif
1279 	case O_IAID:
1280 		ARG_REQUIRED;
1281 		if (ifname == NULL) {
1282 			logerrx("IAID must belong in an interface block");
1283 			return -1;
1284 		}
1285 		if (parse_iaid(ifo->iaid, arg, sizeof(ifo->iaid)) == -1) {
1286 			logerrx("invalid IAID %s", arg);
1287 			return -1;
1288 		}
1289 		ifo->options |= DHCPCD_IAID;
1290 		break;
1291 	case O_IPV6RS:
1292 		ifo->options |= DHCPCD_IPV6RS;
1293 		break;
1294 	case O_NOIPV6RS:
1295 		ifo->options &= ~DHCPCD_IPV6RS;
1296 		break;
1297 	case O_IPV6RA_FORK:
1298 		ifo->options &= ~DHCPCD_IPV6RA_REQRDNSS;
1299 		break;
1300 	case O_IPV6RA_AUTOCONF:
1301 		ifo->options |= DHCPCD_IPV6RA_AUTOCONF;
1302 		break;
1303 	case O_IPV6RA_NOAUTOCONF:
1304 		ifo->options &= ~DHCPCD_IPV6RA_AUTOCONF;
1305 		break;
1306 	case O_NOALIAS:
1307 		ifo->options |= DHCPCD_NOALIAS;
1308 		break;
1309 #ifdef DHCP6
1310 	case O_IA_NA:
1311 		i = D6_OPTION_IA_NA;
1312 		/* FALLTHROUGH */
1313 	case O_IA_TA:
1314 		if (i == 0)
1315 			i = D6_OPTION_IA_TA;
1316 		/* FALLTHROUGH */
1317 	case O_IA_PD:
1318 		if (i == 0) {
1319 #ifdef SMALL
1320 			logwarnx("%s: IA_PD not compiled in", ifname);
1321 			return -1;
1322 #else
1323 			if (ifname == NULL) {
1324 				logerrx("IA PD must belong in an "
1325 				    "interface block");
1326 				return -1;
1327 			}
1328 			i = D6_OPTION_IA_PD;
1329 #endif
1330 		}
1331 		if (ifname == NULL && arg) {
1332 			logerrx("IA with IAID must belong in an "
1333 			    "interface block");
1334 			return -1;
1335 		}
1336 		ifo->options |= DHCPCD_IA_FORCED;
1337 		fp = strwhite(arg);
1338 		if (fp) {
1339 			*fp++ = '\0';
1340 			fp = strskipwhite(fp);
1341 		}
1342 		if (arg) {
1343 			p = strchr(arg, '/');
1344 			if (p)
1345 				*p++ = '\0';
1346 			if (parse_iaid(iaid, arg, sizeof(iaid)) == -1) {
1347 				logerr("invalid IAID: %s", arg);
1348 				return -1;
1349 			}
1350 		}
1351 		ia = NULL;
1352 		for (sl = 0; sl < ifo->ia_len; sl++) {
1353 			if ((arg == NULL && !ifo->ia[sl].iaid_set) ||
1354 			    (arg != NULL && ifo->ia[sl].iaid_set &&
1355 			    ifo->ia[sl].ia_type == (uint16_t)i &&
1356 			    ifo->ia[sl].iaid[0] == iaid[0] &&
1357 			    ifo->ia[sl].iaid[1] == iaid[1] &&
1358 			    ifo->ia[sl].iaid[2] == iaid[2] &&
1359 			    ifo->ia[sl].iaid[3] == iaid[3]))
1360 			{
1361 			        ia = &ifo->ia[sl];
1362 				break;
1363 			}
1364 		}
1365 		if (ia == NULL) {
1366 			ia = reallocarray(ifo->ia,
1367 			    ifo->ia_len + 1, sizeof(*ifo->ia));
1368 			if (ia == NULL) {
1369 				logerr(__func__);
1370 				return -1;
1371 			}
1372 			ifo->ia = ia;
1373 			ia = &ifo->ia[ifo->ia_len++];
1374 			ia->ia_type = (uint16_t)i;
1375 			if (arg) {
1376 				ia->iaid[0] = iaid[0];
1377 				ia->iaid[1] = iaid[1];
1378 				ia->iaid[2] = iaid[2];
1379 				ia->iaid[3] = iaid[3];
1380 				ia->iaid_set = 1;
1381 			} else
1382 				ia->iaid_set = 0;
1383 			if (!ia->iaid_set ||
1384 			    p == NULL ||
1385 			    ia->ia_type == D6_OPTION_IA_TA)
1386 			{
1387 				memset(&ia->addr, 0, sizeof(ia->addr));
1388 				ia->prefix_len = 0;
1389 			} else {
1390 				arg = p;
1391 				p = strchr(arg, '/');
1392 				if (p)
1393 					*p++ = '\0';
1394 				if (inet_pton(AF_INET6, arg, &ia->addr) == -1) {
1395 					logerr("%s", arg);
1396 					memset(&ia->addr, 0, sizeof(ia->addr));
1397 				}
1398 				if (p && ia->ia_type == D6_OPTION_IA_PD) {
1399 					ia->prefix_len = (uint8_t)strtou(p,
1400 					    NULL, 0, 8, 120, &e);
1401 					if (e) {
1402 						logerrx("%s: failed to convert"
1403 						    " prefix len",
1404 						    p);
1405 						ia->prefix_len = 0;
1406 					}
1407 				}
1408 			}
1409 #ifndef SMALL
1410 			ia->sla_max = 0;
1411 			ia->sla_len = 0;
1412 			ia->sla = NULL;
1413 #endif
1414 		}
1415 
1416 #ifdef SMALL
1417 		break;
1418 #else
1419 		if (ia->ia_type != D6_OPTION_IA_PD)
1420 			break;
1421 
1422 		for (p = fp; p; p = fp) {
1423 			fp = strwhite(p);
1424 			if (fp) {
1425 				*fp++ = '\0';
1426 				fp = strskipwhite(fp);
1427 			}
1428 			sla = reallocarray(ia->sla,
1429 			    ia->sla_len + 1, sizeof(*ia->sla));
1430 			if (sla == NULL) {
1431 				logerr(__func__);
1432 				return -1;
1433 			}
1434 			ia->sla = sla;
1435 			sla = &ia->sla[ia->sla_len++];
1436 			np = strchr(p, '/');
1437 			if (np)
1438 				*np++ = '\0';
1439 			if (strlcpy(sla->ifname, p,
1440 			    sizeof(sla->ifname)) >= sizeof(sla->ifname))
1441 			{
1442 				logerrx("%s: interface name too long", arg);
1443 				goto err_sla;
1444 			}
1445 			sla->sla_set = 0;
1446 			sla->prefix_len = 0;
1447 			sla->suffix = 1;
1448 			p = np;
1449 			if (p) {
1450 				np = strchr(p, '/');
1451 				if (np)
1452 					*np++ = '\0';
1453 				if (*p != '\0') {
1454 					sla->sla = (uint32_t)strtou(p, NULL,
1455 					    0, 0, UINT32_MAX, &e);
1456 					sla->sla_set = 1;
1457 					if (e) {
1458 						logerrx("%s: failed to convert "
1459 						    "sla",
1460 						    ifname);
1461 						goto err_sla;
1462 					}
1463 				}
1464 				p = np;
1465 			}
1466 			if (p) {
1467 				np = strchr(p, '/');
1468 				if (np)
1469 					*np++ = '\0';
1470 				if (*p != '\0') {
1471 					sla->prefix_len = (uint8_t)strtou(p,
1472 				    NULL, 0, 0, 120, &e);
1473 					if (e) {
1474 						logerrx("%s: failed to "
1475 						    "convert prefix len",
1476 						    ifname);
1477 						goto err_sla;
1478 					}
1479 				}
1480 				p = np;
1481 			}
1482 			if (p) {
1483 				np = strchr(p, '/');
1484 				if (np)
1485 					*np = '\0';
1486 				if (*p != '\0') {
1487 					sla->suffix = (uint64_t)strtou(p, NULL,
1488 					    0, 0, UINT64_MAX, &e);
1489 					if (e) {
1490 						logerrx("%s: failed to "
1491 						    "convert suffix",
1492 						    ifname);
1493 						goto err_sla;
1494 					}
1495 				}
1496 			}
1497 			/* Sanity check */
1498 			for (sl = 0; sl < ia->sla_len - 1; sl++) {
1499 				slap = &ia->sla[sl];
1500 				if (slap->sla_set != sla->sla_set) {
1501 					logerrx("%s: cannot mix automatic "
1502 					    "and fixed SLA",
1503 					    sla->ifname);
1504 					goto err_sla;
1505 				}
1506 				if (ia->prefix_len &&
1507 				    (sla->prefix_len == ia->prefix_len ||
1508 				    slap->prefix_len == ia->prefix_len))
1509 				{
1510 					logerrx("%s: cannot delegte the same"
1511 					    "prefix length more than once",
1512 					    sla->ifname);
1513 					goto err_sla;
1514 				}
1515 				if (sla->sla_set == 0 &&
1516 				    strcmp(slap->ifname, sla->ifname) == 0)
1517 				{
1518 					logwarnx("%s: cannot specify the "
1519 					    "same interface twice with "
1520 					    "an automatic SLA",
1521 					    sla->ifname);
1522 					goto err_sla;
1523 				}
1524 				if (slap->sla_set && sla->sla_set &&
1525 				    slap->sla == sla->sla)
1526 				{
1527 					logerrx("%s: cannot"
1528 					    " assign the same SLA %u"
1529 					    " more than once",
1530 					    sla->ifname, sla->sla);
1531 					goto err_sla;
1532 				}
1533 			}
1534 			if (sla->sla_set && sla->sla > ia->sla_max)
1535 				ia->sla_max = sla->sla;
1536 		}
1537 		break;
1538 err_sla:
1539 		ia->sla_len--;
1540 		return -1;
1541 #endif
1542 #endif
1543 	case O_HOSTNAME_SHORT:
1544 		ifo->options |= DHCPCD_HOSTNAME | DHCPCD_HOSTNAME_SHORT;
1545 		break;
1546 	case O_DEV:
1547 		ARG_REQUIRED;
1548 #ifdef PLUGIN_DEV
1549 		if (ctx->dev_load)
1550 			free(ctx->dev_load);
1551 		ctx->dev_load = strdup(arg);
1552 #endif
1553 		break;
1554 	case O_NODEV:
1555 		ifo->options &= ~DHCPCD_DEV;
1556 		break;
1557 	case O_DEFINE:
1558 		dop = &ifo->dhcp_override;
1559 		dop_len = &ifo->dhcp_override_len;
1560 		/* FALLTHROUGH */
1561 	case O_DEFINEND:
1562 		if (dop == NULL) {
1563 			dop = &ifo->nd_override;
1564 			dop_len = &ifo->nd_override_len;
1565 		}
1566 		/* FALLTHROUGH */
1567 	case O_DEFINE6:
1568 		if (dop == NULL) {
1569 			dop = &ifo->dhcp6_override;
1570 			dop_len = &ifo->dhcp6_override_len;
1571 		}
1572 		/* FALLTHROUGH */
1573 	case O_VENDOPT:
1574 		if (dop == NULL) {
1575 			dop = &ifo->vivso_override;
1576 			dop_len = &ifo->vivso_override_len;
1577 		}
1578 		*edop = *ldop = NULL;
1579 		/* FALLTHROUGH */
1580 	case O_EMBED:
1581 		if (dop == NULL) {
1582 			if (*edop) {
1583 				dop = &(*edop)->embopts;
1584 				dop_len = &(*edop)->embopts_len;
1585 			} else if (ldop) {
1586 				dop = &(*ldop)->embopts;
1587 				dop_len = &(*ldop)->embopts_len;
1588 			} else {
1589 				logerrx("embed must be after a define "
1590 				    "or encap");
1591 				return -1;
1592 			}
1593 		}
1594 		/* FALLTHROUGH */
1595 	case O_ENCAP:
1596 		ARG_REQUIRED;
1597 		if (dop == NULL) {
1598 			if (*ldop == NULL) {
1599 				logerrx("encap must be after a define");
1600 				return -1;
1601 			}
1602 			dop = &(*ldop)->encopts;
1603 			dop_len = &(*ldop)->encopts_len;
1604 		}
1605 
1606 		/* Shared code for define, define6, embed and encap */
1607 
1608 		/* code */
1609 		if (opt == O_EMBED) /* Embedded options don't have codes */
1610 			u = 0;
1611 		else {
1612 			fp = strwhite(arg);
1613 			if (fp == NULL) {
1614 				logerrx("invalid syntax: %s", arg);
1615 				return -1;
1616 			}
1617 			*fp++ = '\0';
1618 			u = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e);
1619 			if (e) {
1620 				logerrx("invalid code: %s", arg);
1621 				return -1;
1622 			}
1623 			arg = strskipwhite(fp);
1624 			if (arg == NULL) {
1625 				logerrx("invalid syntax");
1626 				return -1;
1627 			}
1628 		}
1629 		/* type */
1630 		fp = strwhite(arg);
1631 		if (fp)
1632 			*fp++ = '\0';
1633 		np = strchr(arg, ':');
1634 		/* length */
1635 		if (np) {
1636 			*np++ = '\0';
1637 			bp = NULL; /* No bitflag */
1638 			l = (long)strtou(np, NULL, 0, 0, LONG_MAX, &e);
1639 			if (e) {
1640 				logerrx("failed to convert length");
1641 				return -1;
1642 			}
1643 		} else {
1644 			l = 0;
1645 			bp = strchr(arg, '='); /* bitflag assignment */
1646 			if (bp)
1647 				*bp++ = '\0';
1648 		}
1649 		t = 0;
1650 		if (strcasecmp(arg, "request") == 0) {
1651 			t |= OT_REQUEST;
1652 			arg = strskipwhite(fp);
1653 			fp = strwhite(arg);
1654 			if (fp == NULL) {
1655 				logerrx("incomplete request type");
1656 				return -1;
1657 			}
1658 			*fp++ = '\0';
1659 		} else if (strcasecmp(arg, "norequest") == 0) {
1660 			t |= OT_NOREQ;
1661 			arg = strskipwhite(fp);
1662 			fp = strwhite(arg);
1663 			if (fp == NULL) {
1664 				logerrx("incomplete request type");
1665 				return -1;
1666 			}
1667 			*fp++ = '\0';
1668 		}
1669 		if (strcasecmp(arg, "optional") == 0) {
1670 			t |= OT_OPTIONAL;
1671 			arg = strskipwhite(fp);
1672 			fp = strwhite(arg);
1673 			if (fp == NULL) {
1674 				logerrx("incomplete optional type");
1675 				return -1;
1676 			}
1677 			*fp++ = '\0';
1678 		}
1679 		if (strcasecmp(arg, "index") == 0) {
1680 			t |= OT_INDEX;
1681 			arg = strskipwhite(fp);
1682 			fp = strwhite(arg);
1683 			if (fp == NULL) {
1684 				logerrx("incomplete index type");
1685 				return -1;
1686 			}
1687 			*fp++ = '\0';
1688 		}
1689 		if (strcasecmp(arg, "array") == 0) {
1690 			t |= OT_ARRAY;
1691 			arg = strskipwhite(fp);
1692 			fp = strwhite(arg);
1693 			if (fp == NULL) {
1694 				logerrx("incomplete array type");
1695 				return -1;
1696 			}
1697 			*fp++ = '\0';
1698 		}
1699 		if (strcasecmp(arg, "ipaddress") == 0)
1700 			t |= OT_ADDRIPV4;
1701 		else if (strcasecmp(arg, "ip6address") == 0)
1702 			t |= OT_ADDRIPV6;
1703 		else if (strcasecmp(arg, "string") == 0)
1704 			t |= OT_STRING;
1705 		else if (strcasecmp(arg, "byte") == 0)
1706 			t |= OT_UINT8;
1707 		else if (strcasecmp(arg, "bitflags") == 0)
1708 			t |= OT_BITFLAG;
1709 		else if (strcasecmp(arg, "uint8") == 0)
1710 			t |= OT_UINT8;
1711 		else if (strcasecmp(arg, "int8") == 0)
1712 			t |= OT_INT8;
1713 		else if (strcasecmp(arg, "uint16") == 0)
1714 			t |= OT_UINT16;
1715 		else if (strcasecmp(arg, "int16") == 0)
1716 			t |= OT_INT16;
1717 		else if (strcasecmp(arg, "uint32") == 0)
1718 			t |= OT_UINT32;
1719 		else if (strcasecmp(arg, "int32") == 0)
1720 			t |= OT_INT32;
1721 		else if (strcasecmp(arg, "flag") == 0)
1722 			t |= OT_FLAG;
1723 		else if (strcasecmp(arg, "raw") == 0)
1724 			t |= OT_STRING | OT_RAW;
1725 		else if (strcasecmp(arg, "ascii") == 0)
1726 			t |= OT_STRING | OT_ASCII;
1727 		else if (strcasecmp(arg, "domain") == 0)
1728 			t |= OT_STRING | OT_DOMAIN | OT_RFC1035;
1729 		else if (strcasecmp(arg, "dname") == 0)
1730 			t |= OT_STRING | OT_DOMAIN;
1731 		else if (strcasecmp(arg, "binhex") == 0)
1732 			t |= OT_STRING | OT_BINHEX;
1733 		else if (strcasecmp(arg, "embed") == 0)
1734 			t |= OT_EMBED;
1735 		else if (strcasecmp(arg, "encap") == 0)
1736 			t |= OT_ENCAP;
1737 		else if (strcasecmp(arg, "rfc3361") ==0)
1738 			t |= OT_STRING | OT_RFC3361;
1739 		else if (strcasecmp(arg, "rfc3442") ==0)
1740 			t |= OT_STRING | OT_RFC3442;
1741 		else if (strcasecmp(arg, "option") == 0)
1742 			t |= OT_OPTION;
1743 		else {
1744 			logerrx("unknown type: %s", arg);
1745 			return -1;
1746 		}
1747 		if (l && !(t & (OT_STRING | OT_BINHEX))) {
1748 			logwarnx("ignoring length for type `%s'", arg);
1749 			l = 0;
1750 		}
1751 		if (t & OT_ARRAY && t & (OT_STRING | OT_BINHEX) &&
1752 		    !(t & (OT_RFC1035 | OT_DOMAIN)))
1753 		{
1754 			logwarnx("ignoring array for strings");
1755 			t &= ~OT_ARRAY;
1756 		}
1757 		if (t & OT_BITFLAG) {
1758 			if (bp == NULL)
1759 				logwarnx("missing bitflag assignment");
1760 		}
1761 		/* variable */
1762 		if (!fp) {
1763 			if (!(t & OT_OPTION)) {
1764 			        logerrx("type %s requires a variable name",
1765 				    arg);
1766 				return -1;
1767 			}
1768 			np = NULL;
1769 		} else {
1770 			arg = strskipwhite(fp);
1771 			fp = strwhite(arg);
1772 			if (fp)
1773 				*fp++ = '\0';
1774 			if (strcasecmp(arg, "reserved")) {
1775 				np = strdup(arg);
1776 				if (np == NULL) {
1777 					logerr(__func__);
1778 					return -1;
1779 				}
1780 			} else {
1781 				np = NULL;
1782 				t |= OT_RESERVED;
1783 			}
1784 		}
1785 		if (opt != O_EMBED) {
1786 			for (dl = 0, ndop = *dop; dl < *dop_len; dl++, ndop++)
1787 			{
1788 				/* type 0 seems freshly malloced struct
1789 				 * for us to use */
1790 				if (ndop->option == u || ndop->type == 0)
1791 					break;
1792 			}
1793 			if (dl == *dop_len)
1794 				ndop = NULL;
1795 		} else
1796 			ndop = NULL;
1797 		if (ndop == NULL) {
1798 			ndop = reallocarray(*dop, *dop_len + 1, sizeof(**dop));
1799 			if (ndop == NULL) {
1800 				logerr(__func__);
1801 				free(np);
1802 				return -1;
1803 			}
1804 			*dop = ndop;
1805 			ndop = &(*dop)[(*dop_len)++];
1806 			ndop->embopts = NULL;
1807 			ndop->embopts_len = 0;
1808 			ndop->encopts = NULL;
1809 			ndop->encopts_len = 0;
1810 		} else
1811 			free_dhcp_opt_embenc(ndop);
1812 		ndop->option = (uint32_t)u; /* could have been 0 */
1813 		ndop->type = t;
1814 		ndop->len = (size_t)l;
1815 		ndop->var = np;
1816 		if (bp) {
1817 			dl = strlen(bp);
1818 			memcpy(ndop->bitflags, bp, dl);
1819 			memset(ndop->bitflags + dl, 0,
1820 			    sizeof(ndop->bitflags) - dl);
1821 		} else
1822 			memset(ndop->bitflags, 0, sizeof(ndop->bitflags));
1823 		/* Save the define for embed and encap options */
1824 		switch (opt) {
1825 		case O_DEFINE:
1826 		case O_DEFINEND:
1827 		case O_DEFINE6:
1828 		case O_VENDOPT:
1829 			*ldop = ndop;
1830 			break;
1831 		case O_ENCAP:
1832 			*edop = ndop;
1833 			break;
1834 		}
1835 		break;
1836 	case O_VENDCLASS:
1837 		ARG_REQUIRED;
1838 		fp = strwhite(arg);
1839 		if (fp)
1840 			*fp++ = '\0';
1841 		u = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e);
1842 		if (e) {
1843 			logerrx("invalid code: %s", arg);
1844 			return -1;
1845 		}
1846 		fp = strskipwhite(fp);
1847 		if (fp) {
1848 			s = parse_string(NULL, 0, fp);
1849 			if (s == -1) {
1850 				logerr(__func__);
1851 				return -1;
1852 			}
1853 			dl = (size_t)s;
1854 			if (dl + (sizeof(uint16_t) * 2) > UINT16_MAX) {
1855 				logerrx("vendor class is too big");
1856 				return -1;
1857 			}
1858 			np = malloc(dl);
1859 			if (np == NULL) {
1860 				logerr(__func__);
1861 				return -1;
1862 			}
1863 			parse_string(np, dl, fp);
1864 		} else {
1865 			dl = 0;
1866 			np = NULL;
1867 		}
1868 		vivco = reallocarray(ifo->vivco,
1869 		    ifo->vivco_len + 1, sizeof(*ifo->vivco));
1870 		if (vivco == NULL) {
1871 			logerr( __func__);
1872 			free(np);
1873 			return -1;
1874 		}
1875 		ifo->vivco = vivco;
1876 		ifo->vivco_en = (uint32_t)u;
1877 		vivco = &ifo->vivco[ifo->vivco_len++];
1878 		vivco->len = dl;
1879 		vivco->data = (uint8_t *)np;
1880 		break;
1881 	case O_AUTHPROTOCOL:
1882 		ARG_REQUIRED;
1883 #ifdef AUTH
1884 		fp = strwhite(arg);
1885 		if (fp)
1886 			*fp++ = '\0';
1887 		if (strcasecmp(arg, "token") == 0)
1888 			ifo->auth.protocol = AUTH_PROTO_TOKEN;
1889 		else if (strcasecmp(arg, "delayed") == 0)
1890 			ifo->auth.protocol = AUTH_PROTO_DELAYED;
1891 		else if (strcasecmp(arg, "delayedrealm") == 0)
1892 			ifo->auth.protocol = AUTH_PROTO_DELAYEDREALM;
1893 		else {
1894 			logerrx("%s: unsupported protocol", arg);
1895 			return -1;
1896 		}
1897 		arg = strskipwhite(fp);
1898 		fp = strwhite(arg);
1899 		if (arg == NULL) {
1900 			ifo->auth.options |= DHCPCD_AUTH_SEND;
1901 			if (ifo->auth.protocol == AUTH_PROTO_TOKEN)
1902 				ifo->auth.protocol = AUTH_ALG_NONE;
1903 			else
1904 				ifo->auth.algorithm = AUTH_ALG_HMAC_MD5;
1905 			ifo->auth.rdm = AUTH_RDM_MONOTONIC;
1906 			break;
1907 		}
1908 		if (fp)
1909 			*fp++ = '\0';
1910 		if (ifo->auth.protocol == AUTH_PROTO_TOKEN) {
1911 			np = strchr(arg, '/');
1912 			if (np) {
1913 				if (fp == NULL || np < fp)
1914 					*np++ = '\0';
1915 				else
1916 					np = NULL;
1917 			}
1918 			if (parse_uint32(&ifo->auth.token_snd_secretid,
1919 			    arg) == -1)
1920 				logerrx("%s: not a number", arg);
1921 			else
1922 				ifo->auth.token_rcv_secretid =
1923 				    ifo->auth.token_snd_secretid;
1924 			if (np &&
1925 			    parse_uint32(&ifo->auth.token_rcv_secretid,
1926 			    np) == -1)
1927 				logerrx("%s: not a number", arg);
1928 		} else {
1929 			if (strcasecmp(arg, "hmacmd5") == 0 ||
1930 			    strcasecmp(arg, "hmac-md5") == 0)
1931 				ifo->auth.algorithm = AUTH_ALG_HMAC_MD5;
1932 			else {
1933 				logerrx("%s: unsupported algorithm", arg);
1934 				return 1;
1935 			}
1936 		}
1937 		arg = fp;
1938 		if (arg == NULL) {
1939 			ifo->auth.options |= DHCPCD_AUTH_SEND;
1940 			ifo->auth.rdm = AUTH_RDM_MONOTONIC;
1941 			break;
1942 		}
1943 		if (strcasecmp(arg, "monocounter") == 0) {
1944 			ifo->auth.rdm = AUTH_RDM_MONOTONIC;
1945 			ifo->auth.options |= DHCPCD_AUTH_RDM_COUNTER;
1946 		} else if (strcasecmp(arg, "monotonic") ==0 ||
1947 		    strcasecmp(arg, "monotime") == 0)
1948 			ifo->auth.rdm = AUTH_RDM_MONOTONIC;
1949 		else {
1950 			logerrx("%s: unsupported RDM", arg);
1951 			return -1;
1952 		}
1953 		ifo->auth.options |= DHCPCD_AUTH_SEND;
1954 		break;
1955 #else
1956 		logerrx("no authentication support");
1957 		return -1;
1958 #endif
1959 	case O_AUTHTOKEN:
1960 		ARG_REQUIRED;
1961 #ifdef AUTH
1962 		fp = strwhite(arg);
1963 		if (fp == NULL) {
1964 			logerrx("authtoken requires a realm");
1965 			return -1;
1966 		}
1967 		*fp++ = '\0';
1968 		token = malloc(sizeof(*token));
1969 		if (token == NULL) {
1970 			logerr(__func__);
1971 			free(token);
1972 			return -1;
1973 		}
1974 		if (parse_uint32(&token->secretid, arg) == -1) {
1975 			logerrx("%s: not a number", arg);
1976 			free(token);
1977 			return -1;
1978 		}
1979 		arg = fp;
1980 		fp = strend(arg);
1981 		if (fp == NULL) {
1982 			logerrx("authtoken requies an a key");
1983 			free(token);
1984 			return -1;
1985 		}
1986 		*fp++ = '\0';
1987 		s = parse_string(NULL, 0, arg);
1988 		if (s == -1) {
1989 			logerr("realm_len");
1990 			free(token);
1991 			return -1;
1992 		}
1993 		if (s) {
1994 			token->realm_len = (size_t)s;
1995 			token->realm = malloc(token->realm_len);
1996 			if (token->realm == NULL) {
1997 				logerr(__func__);
1998 				free(token);
1999 				return -1;
2000 			}
2001 			parse_string((char *)token->realm, token->realm_len,
2002 			    arg);
2003 		} else {
2004 			token->realm_len = 0;
2005 			token->realm = NULL;
2006 		}
2007 		arg = fp;
2008 		fp = strend(arg);
2009 		if (fp == NULL) {
2010 			logerrx("authtoken requies an expiry date");
2011 			free(token->realm);
2012 			free(token);
2013 			return -1;
2014 		}
2015 		*fp++ = '\0';
2016 		if (*arg == '"') {
2017 			arg++;
2018 			np = strchr(arg, '"');
2019 			if (np)
2020 				*np = '\0';
2021 		}
2022 		if (strcmp(arg, "0") == 0 || strcasecmp(arg, "forever") == 0)
2023 			token->expire =0;
2024 		else {
2025 			struct tm tm;
2026 
2027 			memset(&tm, 0, sizeof(tm));
2028 			if (strptime(arg, "%Y-%m-%d %H:%M", &tm) == NULL) {
2029 				logerrx("%s: invalid date time", arg);
2030 				free(token->realm);
2031 				free(token);
2032 				return -1;
2033 			}
2034 			if ((token->expire = mktime(&tm)) == (time_t)-1) {
2035 				logerr("%s: mktime", __func__);
2036 				free(token->realm);
2037 				free(token);
2038 				return -1;
2039 			}
2040 		}
2041 		arg = fp;
2042 		s = parse_string(NULL, 0, arg);
2043 		if (s == -1 || s == 0) {
2044 			if (s == -1)
2045 				logerr("token_len");
2046 			else
2047 				logerrx("authtoken needs a key");
2048 			free(token->realm);
2049 			free(token);
2050 			return -1;
2051 		}
2052 		token->key_len = (size_t)s;
2053 		token->key = malloc(token->key_len);
2054 		parse_string((char *)token->key, token->key_len, arg);
2055 		TAILQ_INSERT_TAIL(&ifo->auth.tokens, token, next);
2056 #else
2057 		logerrx("no authentication support");
2058 		return -1;
2059 #endif
2060 		break;
2061 	case O_AUTHNOTREQUIRED:
2062 		ifo->auth.options &= ~DHCPCD_AUTH_REQUIRE;
2063 		break;
2064 	case O_DHCP:
2065 		ifo->options |= DHCPCD_DHCP | DHCPCD_WANTDHCP | DHCPCD_IPV4;
2066 		break;
2067 	case O_NODHCP:
2068 		ifo->options &= ~DHCPCD_DHCP;
2069 		break;
2070 	case O_DHCP6:
2071 		ifo->options |= DHCPCD_DHCP6 | DHCPCD_IPV6;
2072 		break;
2073 	case O_NODHCP6:
2074 		ifo->options &= ~DHCPCD_DHCP6;
2075 		break;
2076 	case O_CONTROLGRP:
2077 		ARG_REQUIRED;
2078 #ifdef _REENTRANT
2079 		l = sysconf(_SC_GETGR_R_SIZE_MAX);
2080 		if (l == -1)
2081 			dl = 1024;
2082 		else
2083 			dl = (size_t)l;
2084 		p = malloc(dl);
2085 		if (p == NULL) {
2086 			logerr(__func__);
2087 			return -1;
2088 		}
2089 		while ((i = getgrnam_r(arg, &grpbuf, p, (size_t)l, &grp)) ==
2090 		    ERANGE)
2091 		{
2092 			size_t nl = dl * 2;
2093 			if (nl < dl) {
2094 				logerrx("control_group: out of buffer");
2095 				free(p);
2096 				return -1;
2097 			}
2098 			dl = nl;
2099 			np = realloc(p, dl);
2100 			if (np == NULL) {
2101 				logerr(__func__);
2102 				free(p);
2103 				return -1;
2104 			}
2105 			p = np;
2106 		}
2107 		if (i != 0) {
2108 			errno = i;
2109 			logerr("getgrnam_r");
2110 			free(p);
2111 			return -1;
2112 		}
2113 		if (grp == NULL) {
2114 			logerrx("controlgroup: %s: not found", arg);
2115 			free(p);
2116 			return -1;
2117 		}
2118 		ctx->control_group = grp->gr_gid;
2119 		free(p);
2120 #else
2121 		grp = getgrnam(arg);
2122 		if (grp == NULL) {
2123 			logerrx("controlgroup: %s: not found", arg);
2124 			return -1;
2125 		}
2126 		ctx->control_group = grp->gr_gid;
2127 #endif
2128 		break;
2129 	case O_GATEWAY:
2130 		ifo->options |= DHCPCD_GATEWAY;
2131 		break;
2132 	case O_NOUP:
2133 		ifo->options &= ~DHCPCD_IF_UP;
2134 		break;
2135 	case O_SLAAC:
2136 		ARG_REQUIRED;
2137 		if (strcmp(arg, "private") == 0 ||
2138 		    strcmp(arg, "stableprivate") == 0 ||
2139 		    strcmp(arg, "stable") == 0)
2140 			ifo->options |= DHCPCD_SLAACPRIVATE;
2141 		else
2142 			ifo->options &= ~DHCPCD_SLAACPRIVATE;
2143 		break;
2144 	case O_BOOTP:
2145 		ifo->options |= DHCPCD_BOOTP;
2146 		break;
2147 	case O_NODELAY:
2148 		ifo->options &= ~DHCPCD_INITIAL_DELAY;
2149 		break;
2150 	case O_LASTLEASE_EXTEND:
2151 		ifo->options |= DHCPCD_LASTLEASE | DHCPCD_LASTLEASE_EXTEND;
2152 		break;
2153 	case O_INACTIVE:
2154 		ifo->options |= DHCPCD_INACTIVE;
2155 		break;
2156 	case O_MUDURL:
2157 		ARG_REQUIRED;
2158 		s = parse_string((char *)ifo->mudurl + 1, MUDURL_MAX_LEN, arg);
2159 		if (s == -1) {
2160 			logerr("mudurl");
2161 			return -1;
2162 		}
2163 		*ifo->mudurl = (uint8_t)s;
2164 		break;
2165 	case O_LINK_RCVBUF:
2166 #ifndef SMALL
2167 		ARG_REQUIRED;
2168 		ctx->link_rcvbuf = (int)strtoi(arg, NULL, 0, 0, INT32_MAX, &e);
2169 		if (e) {
2170 			logerrx("failed to convert link_rcvbuf %s", arg);
2171 			return -1;
2172 		}
2173 #endif
2174 		break;
2175 	default:
2176 		return 0;
2177 	}
2178 
2179 	return 1;
2180 
2181 #ifdef ARG_REQUIRED
2182 arg_required:
2183 	logerrx("option %d requires an argument", opt);
2184 	return -1;
2185 #undef ARG_REQUIRED
2186 #endif
2187 }
2188 
2189 static int
2190 parse_config_line(struct dhcpcd_ctx *ctx, const char *ifname,
2191     struct if_options *ifo, const char *opt, char *line,
2192     struct dhcp_opt **ldop, struct dhcp_opt **edop)
2193 {
2194 	unsigned int i;
2195 
2196 	for (i = 0; i < sizeof(cf_options) / sizeof(cf_options[0]); i++) {
2197 		if (!cf_options[i].name ||
2198 		    strcmp(cf_options[i].name, opt) != 0)
2199 			continue;
2200 
2201 		if (cf_options[i].has_arg == required_argument && !line) {
2202 			logerrx("option requires an argument -- %s", opt);
2203 			return -1;
2204 		}
2205 
2206 		return parse_option(ctx, ifname, ifo, cf_options[i].val, line,
2207 		    ldop, edop);
2208 	}
2209 
2210 	logerrx("unknown option: %s", opt);
2211 	return -1;
2212 }
2213 
2214 static void
2215 finish_config(struct if_options *ifo)
2216 {
2217 
2218 	/* Terminate the encapsulated options */
2219 	if (ifo->vendor[0] && !(ifo->options & DHCPCD_VENDORRAW)) {
2220 		ifo->vendor[0]++;
2221 		ifo->vendor[ifo->vendor[0]] = DHO_END;
2222 		/* We are called twice.
2223 		 * This should be fixed, but in the meantime, this
2224 		 * guard should suffice */
2225 		ifo->options |= DHCPCD_VENDORRAW;
2226 	}
2227 }
2228 
2229 /* Handy routine to read very long lines in text files.
2230  * This means we read the whole line and avoid any nasty buffer overflows.
2231  * We strip leading space and avoid comment lines, making the code that calls
2232  * us smaller. */
2233 static char *
2234 get_line(char ** __restrict buf, size_t * __restrict buflen,
2235     FILE * __restrict fp)
2236 {
2237 	char *p, *c;
2238 	ssize_t bytes;
2239 	int quoted;
2240 
2241 	do {
2242 		bytes = getline(buf, buflen, fp);
2243 		if (bytes == -1)
2244 			return NULL;
2245 		for (p = *buf; *p == ' ' || *p == '\t'; p++)
2246 			;
2247 	} while (*p == '\0' || *p == '\n' || *p == '#' || *p == ';');
2248 	if ((*buf)[--bytes] == '\n')
2249 		(*buf)[bytes] = '\0';
2250 
2251 	/* Strip embedded comments unless in a quoted string or escaped */
2252 	quoted = 0;
2253 	for (c = p; *c != '\0'; c++) {
2254 		if (*c == '\\') {
2255 			c++; /* escaped */
2256 			continue;
2257 		}
2258 		if (*c == '"')
2259 			quoted = !quoted;
2260 		else if (*c == '#' && !quoted) {
2261 			*c = '\0';
2262 			break;
2263 		}
2264 	}
2265 	return p;
2266 }
2267 
2268 struct if_options *
2269 default_config(struct dhcpcd_ctx *ctx)
2270 {
2271 	struct if_options *ifo;
2272 
2273 	/* Seed our default options */
2274 	if ((ifo = calloc(1, sizeof(*ifo))) == NULL) {
2275 		logerr(__func__);
2276 		return NULL;
2277 	}
2278 	ifo->options |= DHCPCD_IF_UP | DHCPCD_LINK | DHCPCD_INITIAL_DELAY;
2279 	ifo->timeout = DEFAULT_TIMEOUT;
2280 	ifo->reboot = DEFAULT_REBOOT;
2281 	ifo->script = UNCONST(default_script);
2282 	ifo->metric = -1;
2283 	ifo->auth.options |= DHCPCD_AUTH_REQUIRE;
2284 	rb_tree_init(&ifo->routes, &rt_compare_list_ops);
2285 #ifdef AUTH
2286 	TAILQ_INIT(&ifo->auth.tokens);
2287 #endif
2288 
2289 	/* Inherit some global defaults */
2290 	if (ctx->options & DHCPCD_PERSISTENT)
2291 		ifo->options |= DHCPCD_PERSISTENT;
2292 	if (ctx->options & DHCPCD_SLAACPRIVATE)
2293 		ifo->options |= DHCPCD_SLAACPRIVATE;
2294 
2295 	return ifo;
2296 }
2297 
2298 struct if_options *
2299 read_config(struct dhcpcd_ctx *ctx,
2300     const char *ifname, const char *ssid, const char *profile)
2301 {
2302 	struct if_options *ifo;
2303 	FILE *fp;
2304 	struct stat sb;
2305 	char *line, *buf, *option, *p;
2306 	size_t buflen;
2307 	ssize_t vlen;
2308 	int skip, have_profile, new_block, had_block;
2309 #ifndef EMBEDDED_CONFIG
2310 	const char * const *e;
2311 	size_t ol;
2312 #endif
2313 #if !defined(INET) || !defined(INET6)
2314 	size_t i;
2315 	struct dhcp_opt *opt;
2316 #endif
2317 	struct dhcp_opt *ldop, *edop;
2318 
2319 	/* Seed our default options */
2320 	if ((ifo = default_config(ctx)) == NULL)
2321 		return NULL;
2322 	ifo->options |= DHCPCD_DAEMONISE | DHCPCD_GATEWAY;
2323 #ifdef PLUGIN_DEV
2324 	ifo->options |= DHCPCD_DEV;
2325 #endif
2326 #ifdef INET
2327 	ifo->options |= DHCPCD_IPV4 | DHCPCD_ARP | DHCPCD_DHCP | DHCPCD_IPV4LL;
2328 #endif
2329 #ifdef INET6
2330 	ifo->options |= DHCPCD_IPV6 | DHCPCD_IPV6RS;
2331 	ifo->options |= DHCPCD_IPV6RA_AUTOCONF | DHCPCD_IPV6RA_REQRDNSS;
2332 	ifo->options |= DHCPCD_DHCP6;
2333 #endif
2334 
2335 	vlen = dhcp_vendor((char *)ifo->vendorclassid + 1,
2336 	            sizeof(ifo->vendorclassid) - 1);
2337 	ifo->vendorclassid[0] = (uint8_t)(vlen == -1 ? 0 : vlen);
2338 
2339 	buf = NULL;
2340 	buflen = 0;
2341 
2342 	/* Reset route order */
2343 	ctx->rt_order = 0;
2344 
2345 	/* Parse our embedded options file */
2346 	if (ifname == NULL && !(ctx->options & DHCPCD_PRINT_PIDFILE)) {
2347 		/* Space for initial estimates */
2348 #if defined(INET) && defined(INITDEFINES)
2349 		ifo->dhcp_override =
2350 		    calloc(INITDEFINES, sizeof(*ifo->dhcp_override));
2351 		if (ifo->dhcp_override == NULL)
2352 			logerr(__func__);
2353 		else
2354 			ifo->dhcp_override_len = INITDEFINES;
2355 #endif
2356 
2357 #if defined(INET6) && defined(INITDEFINENDS)
2358 		ifo->nd_override =
2359 		    calloc(INITDEFINENDS, sizeof(*ifo->nd_override));
2360 		if (ifo->nd_override == NULL)
2361 			logerr(__func__);
2362 		else
2363 			ifo->nd_override_len = INITDEFINENDS;
2364 #endif
2365 #if defined(INET6) && defined(INITDEFINE6S)
2366 		ifo->dhcp6_override =
2367 		    calloc(INITDEFINE6S, sizeof(*ifo->dhcp6_override));
2368 		if (ifo->dhcp6_override == NULL)
2369 			logerr(__func__);
2370 		else
2371 			ifo->dhcp6_override_len = INITDEFINE6S;
2372 #endif
2373 
2374 		/* Now load our embedded config */
2375 #ifdef EMBEDDED_CONFIG
2376 		fp = fopen(EMBEDDED_CONFIG, "r");
2377 		if (fp == NULL)
2378 			logerr("%s: fopen `%s'", __func__, EMBEDDED_CONFIG);
2379 
2380 		while (fp && (line = get_line(&buf, &buflen, fp))) {
2381 #else
2382 		buflen = 80;
2383 		buf = malloc(buflen);
2384 		if (buf == NULL) {
2385 			logerr(__func__);
2386 			free_options(ctx, ifo);
2387 			return NULL;
2388 		}
2389 		ldop = edop = NULL;
2390 		for (e = dhcpcd_embedded_conf; *e; e++) {
2391 			ol = strlen(*e) + 1;
2392 			if (ol > buflen) {
2393 				char *nbuf;
2394 
2395 				buflen = ol;
2396 				nbuf = realloc(buf, buflen);
2397 				if (nbuf == NULL) {
2398 					logerr(__func__);
2399 					free(buf);
2400 					free_options(ctx, ifo);
2401 					return NULL;
2402 				}
2403 				buf = nbuf;
2404 			}
2405 			memcpy(buf, *e, ol);
2406 			line = buf;
2407 #endif
2408 			option = strsep(&line, " \t");
2409 			if (line)
2410 				line = strskipwhite(line);
2411 			/* Trim trailing whitespace */
2412 			if (line) {
2413 				p = line + strlen(line) - 1;
2414 				while (p != line &&
2415 				    (*p == ' ' || *p == '\t') &&
2416 				    *(p - 1) != '\\')
2417 					*p-- = '\0';
2418 			}
2419 			parse_config_line(ctx, NULL, ifo, option, line,
2420 			    &ldop, &edop);
2421 
2422 		}
2423 
2424 #ifdef EMBEDDED_CONFIG
2425 		if (fp)
2426 			fclose(fp);
2427 #endif
2428 #ifdef INET
2429 		ctx->dhcp_opts = ifo->dhcp_override;
2430 		ctx->dhcp_opts_len = ifo->dhcp_override_len;
2431 #else
2432 		for (i = 0, opt = ifo->dhcp_override;
2433 		    i < ifo->dhcp_override_len;
2434 		    i++, opt++)
2435 			free_dhcp_opt_embenc(opt);
2436 		free(ifo->dhcp_override);
2437 #endif
2438 		ifo->dhcp_override = NULL;
2439 		ifo->dhcp_override_len = 0;
2440 
2441 #ifdef INET6
2442 		ctx->nd_opts = ifo->nd_override;
2443 		ctx->nd_opts_len = ifo->nd_override_len;
2444 #ifdef DHCP6
2445 		ctx->dhcp6_opts = ifo->dhcp6_override;
2446 		ctx->dhcp6_opts_len = ifo->dhcp6_override_len;
2447 #endif
2448 #else
2449 		for (i = 0, opt = ifo->nd_override;
2450 		    i < ifo->nd_override_len;
2451 		    i++, opt++)
2452 			free_dhcp_opt_embenc(opt);
2453 		free(ifo->nd_override);
2454 		for (i = 0, opt = ifo->dhcp6_override;
2455 		    i < ifo->dhcp6_override_len;
2456 		    i++, opt++)
2457 			free_dhcp_opt_embenc(opt);
2458 		free(ifo->dhcp6_override);
2459 #endif
2460 		ifo->nd_override = NULL;
2461 		ifo->nd_override_len = 0;
2462 		ifo->dhcp6_override = NULL;
2463 		ifo->dhcp6_override_len = 0;
2464 
2465 		ctx->vivso = ifo->vivso_override;
2466 		ctx->vivso_len = ifo->vivso_override_len;
2467 		ifo->vivso_override = NULL;
2468 		ifo->vivso_override_len = 0;
2469 	}
2470 
2471 	/* Parse our options file */
2472 	fp = fopen(ctx->cffile, "r");
2473 	if (fp == NULL) {
2474 		/* dhcpcd can continue without it, but no DNS options
2475 		 * would be requested ... */
2476 		logwarn("%s: fopen `%s'", __func__, ctx->cffile);
2477 		free(buf);
2478 		return ifo;
2479 	}
2480 	if (stat(ctx->cffile, &sb) == 0)
2481 		ifo->mtime = sb.st_mtime;
2482 
2483 	ldop = edop = NULL;
2484 	skip = have_profile = new_block = 0;
2485 	had_block = ifname == NULL ? 1 : 0;
2486 	while ((line = get_line(&buf, &buflen, fp))) {
2487 		option = strsep(&line, " \t");
2488 		if (line)
2489 			line = strskipwhite(line);
2490 		/* Trim trailing whitespace */
2491 		if (line) {
2492 			p = line + strlen(line) - 1;
2493 			while (p != line &&
2494 			    (*p == ' ' || *p == '\t') &&
2495 			    *(p - 1) != '\\')
2496 				*p-- = '\0';
2497 		}
2498 		if (skip == 0 && new_block) {
2499 			had_block = 1;
2500 			new_block = 0;
2501 			ifo->options &= ~DHCPCD_WAITOPTS;
2502 		}
2503 		/* Start of an interface block, skip if not ours */
2504 		if (strcmp(option, "interface") == 0) {
2505 			char **n;
2506 
2507 			new_block = 1;
2508 			if (line == NULL) {
2509 				/* No interface given */
2510 				skip = 1;
2511 				continue;
2512 			}
2513 			if (ifname && strcmp(line, ifname) == 0)
2514 				skip = 0;
2515 			else
2516 				skip = 1;
2517 			if (ifname)
2518 				continue;
2519 
2520 			n = reallocarray(ctx->ifcv,
2521 			    (size_t)ctx->ifcc + 1, sizeof(char *));
2522 			if (n == NULL) {
2523 				logerr(__func__);
2524 				continue;
2525 			}
2526 			ctx->ifcv = n;
2527 			ctx->ifcv[ctx->ifcc] = strdup(line);
2528 			if (ctx->ifcv[ctx->ifcc] == NULL) {
2529 				logerr(__func__);
2530 				continue;
2531 			}
2532 			ctx->ifcc++;
2533 			continue;
2534 		}
2535 		/* Start of an ssid block, skip if not ours */
2536 		if (strcmp(option, "ssid") == 0) {
2537 			new_block = 1;
2538 			if (ssid && line && strcmp(line, ssid) == 0)
2539 				skip = 0;
2540 			else
2541 				skip = 1;
2542 			continue;
2543 		}
2544 		/* Start of a profile block, skip if not ours */
2545 		if (strcmp(option, "profile") == 0) {
2546 			new_block = 1;
2547 			if (profile && line && strcmp(line, profile) == 0) {
2548 				skip = 0;
2549 				have_profile = 1;
2550 			} else
2551 				skip = 1;
2552 			continue;
2553 		}
2554 		/* Skip arping if we have selected a profile but not parsing
2555 		 * one. */
2556 		if (profile && !have_profile && strcmp(option, "arping") == 0)
2557 			continue;
2558 		if (skip)
2559 			continue;
2560 		parse_config_line(ctx, ifname, ifo, option, line, &ldop, &edop);
2561 	}
2562 	fclose(fp);
2563 	free(buf);
2564 
2565 	if (profile && !have_profile) {
2566 		free_options(ctx, ifo);
2567 		errno = ENOENT;
2568 		return NULL;
2569 	}
2570 
2571 	if (!had_block)
2572 		ifo->options &= ~DHCPCD_WAITOPTS;
2573 	finish_config(ifo);
2574 	return ifo;
2575 }
2576 
2577 int
2578 add_options(struct dhcpcd_ctx *ctx, const char *ifname,
2579     struct if_options *ifo, int argc, char **argv)
2580 {
2581 	int oi, opt, r;
2582 	unsigned long long wait_opts;
2583 
2584 	if (argc == 0)
2585 		return 1;
2586 
2587 	optind = 0;
2588 	r = 1;
2589 	/* Don't apply the command line wait options to each interface,
2590 	 * only use the dhcpcd.conf entry for that. */
2591 	if (ifname != NULL)
2592 		wait_opts = ifo->options & DHCPCD_WAITOPTS;
2593 	while ((opt = getopt_long(argc, argv,
2594 	    ctx->options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS,
2595 	    cf_options, &oi)) != -1)
2596 	{
2597 		r = parse_option(ctx, ifname, ifo, opt, optarg, NULL, NULL);
2598 		if (r != 1)
2599 			break;
2600 	}
2601 	if (ifname != NULL) {
2602 		ifo->options &= ~DHCPCD_WAITOPTS;
2603 		ifo->options |= wait_opts;
2604 	}
2605 
2606 	finish_config(ifo);
2607 	return r;
2608 }
2609 
2610 void
2611 free_options(struct dhcpcd_ctx *ctx, struct if_options *ifo)
2612 {
2613 	size_t i;
2614 #ifdef RT_FREE_ROUTE_TABLE
2615 	struct interface *ifp;
2616 	struct rt *rt;
2617 #endif
2618 	struct dhcp_opt *opt;
2619 	struct vivco *vo;
2620 #ifdef AUTH
2621 	struct token *token;
2622 #endif
2623 
2624 	if (ifo == NULL)
2625 		return;
2626 
2627 	if (ifo->environ) {
2628 		i = 0;
2629 		while (ifo->environ[i])
2630 			free(ifo->environ[i++]);
2631 		free(ifo->environ);
2632 	}
2633 	if (ifo->config) {
2634 		i = 0;
2635 		while (ifo->config[i])
2636 			free(ifo->config[i++]);
2637 		free(ifo->config);
2638 	}
2639 
2640 #ifdef RT_FREE_ROUTE_TABLE
2641 	/* Stupidly, we don't know the interface when creating the options.
2642 	 * As such, make sure each route has one so they can goto the
2643 	 * free list. */
2644 	ifp = ctx->ifaces != NULL ? TAILQ_FIRST(ctx->ifaces) : NULL;
2645 	if (ifp != NULL) {
2646 		RB_TREE_FOREACH(rt, &ifo->routes) {
2647 			if (rt->rt_ifp == NULL)
2648 				rt->rt_ifp = ifp;
2649 		}
2650 	}
2651 #endif
2652 	rt_headclear0(ctx, &ifo->routes, AF_UNSPEC);
2653 
2654 	if (ifo->script != default_script)
2655 		free(ifo->script);
2656 	free(ifo->arping);
2657 	free(ifo->blacklist);
2658 	free(ifo->fallback);
2659 
2660 	for (opt = ifo->dhcp_override;
2661 	    ifo->dhcp_override_len > 0;
2662 	    opt++, ifo->dhcp_override_len--)
2663 		free_dhcp_opt_embenc(opt);
2664 	free(ifo->dhcp_override);
2665 	for (opt = ifo->nd_override;
2666 	    ifo->nd_override_len > 0;
2667 	    opt++, ifo->nd_override_len--)
2668 		free_dhcp_opt_embenc(opt);
2669 	free(ifo->nd_override);
2670 	for (opt = ifo->dhcp6_override;
2671 	    ifo->dhcp6_override_len > 0;
2672 	    opt++, ifo->dhcp6_override_len--)
2673 		free_dhcp_opt_embenc(opt);
2674 	free(ifo->dhcp6_override);
2675 	for (vo = ifo->vivco;
2676 	    ifo->vivco_len > 0;
2677 	    vo++, ifo->vivco_len--)
2678 		free(vo->data);
2679 	free(ifo->vivco);
2680 	for (opt = ifo->vivso_override;
2681 	    ifo->vivso_override_len > 0;
2682 	    opt++, ifo->vivso_override_len--)
2683 		free_dhcp_opt_embenc(opt);
2684 	free(ifo->vivso_override);
2685 
2686 #if defined(INET6) && !defined(SMALL)
2687 	for (; ifo->ia_len > 0; ifo->ia_len--)
2688 		free(ifo->ia[ifo->ia_len - 1].sla);
2689 #endif
2690 	free(ifo->ia);
2691 
2692 #ifdef AUTH
2693 	while ((token = TAILQ_FIRST(&ifo->auth.tokens))) {
2694 		TAILQ_REMOVE(&ifo->auth.tokens, token, next);
2695 		if (token->realm_len)
2696 			free(token->realm);
2697 		free(token->key);
2698 		free(token);
2699 	}
2700 #endif
2701 	free(ifo);
2702 }
2703