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