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