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