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