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