]> git.ipfire.org Git - thirdparty/dhcpcd.git/blame - if-options.c
Fix compile on QNX. Thanks to Will Miles.
[thirdparty/dhcpcd.git] / if-options.c
CommitLineData
8cc47ba2 1/*
fd05b7dc 2 * dhcpcd - DHCP client daemon
fad320b9 3 * Copyright (c) 2006-2014 Roy Marples <roy@marples.name>
fd05b7dc
RM
4 * All rights reserved
5
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
0d033d17
RM
28#define _WITH_GETLINE /* Stop FreeBSD bitching */
29
0d0c5f66 30#include <sys/param.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>
0d0c5f66 39#include <limits.h>
fd05b7dc
RM
40#include <paths.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
765fbf7d 44#include <syslog.h>
fd05b7dc
RM
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"
fd05b7dc 53#include "if-options.h"
e88c525f 54#include "ipv4.h"
fd05b7dc 55
4ca7460f
RM
56/* These options only make sense in the config file, so don't use any
57 valid short options for them */
eebe9a18
RM
58#define O_BASE MAX('z', 'Z') + 1
59#define O_ARPING O_BASE + 1
60#define O_FALLBACK O_BASE + 2
61#define O_DESTINATION O_BASE + 3
62#define O_IPV6RS O_BASE + 4
00ababe4
RM
63#define O_NOIPV6RS O_BASE + 5
64#define O_IPV6RA_FORK O_BASE + 6
eebe9a18
RM
65#define O_IPV6RA_OWN O_BASE + 7
66#define O_IPV6RA_OWN_D O_BASE + 8
7dab081f 67#define O_NOALIAS O_BASE + 9
00ababe4
RM
68#define O_IA_NA O_BASE + 10
69#define O_IA_TA O_BASE + 11
70#define O_IA_PD O_BASE + 12
d6a18654 71#define O_HOSTNAME_SHORT O_BASE + 13
413652c1
RM
72#define O_DEV O_BASE + 14
73#define O_NODEV O_BASE + 15
bb8051bf
RM
74#define O_NOIPV4 O_BASE + 16
75#define O_NOIPV6 O_BASE + 17
ebc9d360 76#define O_IAID O_BASE + 18
8e7d8c37
RM
77#define O_DEFINE O_BASE + 19
78#define O_DEFINE6 O_BASE + 20
79#define O_EMBED O_BASE + 21
80#define O_ENCAP O_BASE + 22
7a911e57
RM
81#define O_VENDOPT O_BASE + 23
82#define O_VENDCLASS O_BASE + 24
c73ed171
RM
83#define O_AUTHPROTOCOL O_BASE + 25
84#define O_AUTHTOKEN O_BASE + 26
85#define O_AUTHNOTREQUIRED O_BASE + 27
d4154ba7
RM
86#define O_NODHCP O_BASE + 28
87#define O_NODHCP6 O_BASE + 29
94bec972
RM
88#define O_DHCP O_BASE + 30
89#define O_DHCP6 O_BASE + 31
90#define O_IPV4 O_BASE + 32
91#define O_IPV6 O_BASE + 33
a93e79c6 92#define O_CONTROLGRP O_BASE + 34
1aeaf0e7 93#define O_SLAAC O_BASE + 35
ee56a47d 94#define O_GATEWAY O_BASE + 36
9d5cb9f9 95#define O_PFXDLGMIX O_BASE + 37
413652c1 96
fd05b7dc 97const struct option cf_options[] = {
ba97e494
RM
98 {"background", no_argument, NULL, 'b'},
99 {"script", required_argument, NULL, 'c'},
100 {"debug", no_argument, NULL, 'd'},
6bfd88f1 101 {"env", required_argument, NULL, 'e'},
ba97e494 102 {"config", required_argument, NULL, 'f'},
6bfd88f1 103 {"reconfigure", no_argument, NULL, 'g'},
ba97e494
RM
104 {"hostname", optional_argument, NULL, 'h'},
105 {"vendorclassid", optional_argument, NULL, 'i'},
106 {"release", no_argument, NULL, 'k'},
107 {"leasetime", required_argument, NULL, 'l'},
108 {"metric", required_argument, NULL, 'm'},
109 {"rebind", no_argument, NULL, 'n'},
110 {"option", required_argument, NULL, 'o'},
111 {"persistent", no_argument, NULL, 'p'},
112 {"quiet", no_argument, NULL, 'q'},
113 {"request", optional_argument, NULL, 'r'},
114 {"inform", optional_argument, NULL, 's'},
115 {"timeout", required_argument, NULL, 't'},
116 {"userclass", required_argument, NULL, 'u'},
117 {"vendor", required_argument, NULL, 'v'},
7013b073 118 {"waitip", optional_argument, NULL, 'w'},
ba97e494 119 {"exit", no_argument, NULL, 'x'},
d3088c74 120 {"allowinterfaces", required_argument, NULL, 'z'},
a2a9a498 121 {"reboot", required_argument, NULL, 'y'},
ba97e494
RM
122 {"noarp", no_argument, NULL, 'A'},
123 {"nobackground", no_argument, NULL, 'B'},
124 {"nohook", required_argument, NULL, 'C'},
125 {"duid", no_argument, NULL, 'D'},
126 {"lastlease", no_argument, NULL, 'E'},
127 {"fqdn", optional_argument, NULL, 'F'},
128 {"nogateway", no_argument, NULL, 'G'},
00ababe4 129 {"xidhwaddr", no_argument, NULL, 'H'},
ba97e494 130 {"clientid", optional_argument, NULL, 'I'},
900b3da4 131 {"broadcast", no_argument, NULL, 'J'},
ba97e494
RM
132 {"nolink", no_argument, NULL, 'K'},
133 {"noipv4ll", no_argument, NULL, 'L'},
b3174181 134 {"master", no_argument, NULL, 'M'},
ba97e494
RM
135 {"nooption", optional_argument, NULL, 'O'},
136 {"require", required_argument, NULL, 'Q'},
91a44b91 137 {"static", required_argument, NULL, 'S'},
ba97e494 138 {"test", no_argument, NULL, 'T'},
dc60cba4 139 {"dumplease", no_argument, NULL, 'U'},
ba97e494 140 {"variables", no_argument, NULL, 'V'},
bf80d526 141 {"whitelist", required_argument, NULL, 'W'},
ba97e494 142 {"blacklist", required_argument, NULL, 'X'},
d3088c74 143 {"denyinterfaces", required_argument, NULL, 'Z'},
4ca7460f 144 {"arping", required_argument, NULL, O_ARPING},
41c60e02 145 {"destination", required_argument, NULL, O_DESTINATION},
ff021b0b 146 {"fallback", required_argument, NULL, O_FALLBACK},
eebe9a18 147 {"ipv6rs", no_argument, NULL, O_IPV6RS},
91cd7324 148 {"noipv6rs", no_argument, NULL, O_NOIPV6RS},
eebe9a18
RM
149 {"ipv6ra_fork", no_argument, NULL, O_IPV6RA_FORK},
150 {"ipv6ra_own", no_argument, NULL, O_IPV6RA_OWN},
151 {"ipv6ra_own_default", no_argument, NULL, O_IPV6RA_OWN_D},
d7555c12
RM
152 {"ipv4only", no_argument, NULL, '4'},
153 {"ipv6only", no_argument, NULL, '6'},
94bec972 154 {"ipv4", no_argument, NULL, O_IPV4},
bb8051bf 155 {"noipv4", no_argument, NULL, O_NOIPV4},
94bec972 156 {"ipv6", no_argument, NULL, O_IPV6},
bb8051bf 157 {"noipv6", no_argument, NULL, O_NOIPV6},
7dab081f 158 {"noalias", no_argument, NULL, O_NOALIAS},
7a911e57 159 {"iaid", required_argument, NULL, O_IAID},
00ababe4
RM
160 {"ia_na", no_argument, NULL, O_IA_NA},
161 {"ia_ta", no_argument, NULL, O_IA_TA},
162 {"ia_pd", no_argument, NULL, O_IA_PD},
d6a18654 163 {"hostname_short", no_argument, NULL, O_HOSTNAME_SHORT},
413652c1
RM
164 {"dev", required_argument, NULL, O_DEV},
165 {"nodev", no_argument, NULL, O_NODEV},
7a911e57
RM
166 {"define", required_argument, NULL, O_DEFINE},
167 {"define6", required_argument, NULL, O_DEFINE6},
168 {"embed", required_argument, NULL, O_EMBED},
169 {"encap", required_argument, NULL, O_ENCAP},
170 {"vendopt", required_argument, NULL, O_VENDOPT},
171 {"vendclass", required_argument, NULL, O_VENDCLASS},
c73ed171
RM
172 {"authprotocol", required_argument, NULL, O_AUTHPROTOCOL},
173 {"authtoken", required_argument, NULL, O_AUTHTOKEN},
174 {"noauthrequired", no_argument, NULL, O_AUTHNOTREQUIRED},
94bec972 175 {"dhcp", no_argument, NULL, O_DHCP},
d4154ba7 176 {"nodhcp", no_argument, NULL, O_NODHCP},
94bec972 177 {"dhcp6", no_argument, NULL, O_DHCP6},
d4154ba7 178 {"nodhcp6", no_argument, NULL, O_NODHCP6},
a93e79c6 179 {"controlgroup", required_argument, NULL, O_CONTROLGRP},
1aeaf0e7 180 {"slaac", required_argument, NULL, O_SLAAC},
3d3e410f 181 {"gateway", no_argument, NULL, O_GATEWAY},
9d5cb9f9 182 {"ia_pd_mix", no_argument, NULL, O_PFXDLGMIX},
ba97e494 183 {NULL, 0, NULL, '\0'}
fd05b7dc
RM
184};
185
186static int
187atoint(const char *s)
188{
189 char *t;
190 long n;
191
192 errno = 0;
193 n = strtol(s, &t, 0);
194 if ((errno != 0 && n == 0) || s == t ||
195 (errno == ERANGE && (n == LONG_MAX || n == LONG_MIN)))
196 {
367f7b11
RM
197 if (errno == 0)
198 errno = EINVAL;
765fbf7d 199 syslog(LOG_ERR, "`%s' out of range", s);
fd05b7dc
RM
200 return -1;
201 }
202
203 return (int)n;
204}
205
00ababe4 206static char *
fd05b7dc
RM
207add_environ(struct if_options *ifo, const char *value, int uniq)
208{
209 char **newlist;
210 char **lst = ifo->environ;
211 size_t i = 0, l, lv;
fa245a4d 212 char *match = NULL, *p, *n;
fd05b7dc 213
78369646
RM
214 match = strdup(value);
215 if (match == NULL) {
216 syslog(LOG_ERR, "%s: %m", __func__);
217 return NULL;
218 }
fd05b7dc 219 p = strchr(match, '=');
d7cfde68
RM
220 if (p == NULL) {
221 syslog(LOG_ERR, "%s: no assignment: %s", __func__, value);
222 free(match);
223 return NULL;
224 }
225 *p++ = '\0';
fd05b7dc
RM
226 l = strlen(match);
227
228 while (lst && lst[i]) {
229 if (match && strncmp(lst[i], match, l) == 0) {
230 if (uniq) {
78369646
RM
231 n = strdup(value);
232 if (n == NULL) {
233 syslog(LOG_ERR, "%s: %m", __func__);
8fc52ced 234 free(match);
78369646
RM
235 return NULL;
236 }
fd05b7dc 237 free(lst[i]);
78369646 238 lst[i] = n;
fd05b7dc
RM
239 } else {
240 /* Append a space and the value to it */
241 l = strlen(lst[i]);
d7cfde68 242 lv = strlen(p);
fa245a4d
RM
243 n = realloc(lst[i], l + lv + 2);
244 if (n == NULL) {
245 syslog(LOG_ERR, "%s: %m", __func__);
8fc52ced 246 free(match);
fa245a4d
RM
247 return NULL;
248 }
249 lst[i] = n;
fd05b7dc
RM
250 lst[i][l] = ' ';
251 memcpy(lst[i] + l + 1, p, lv);
252 lst[i][l + lv + 1] = '\0';
253 }
254 free(match);
255 return lst[i];
256 }
257 i++;
258 }
259
8fc52ced 260 free(match);
78369646
RM
261 n = strdup(value);
262 if (n == NULL) {
263 syslog(LOG_ERR, "%s: %m", __func__);
264 return NULL;
265 }
fa245a4d
RM
266 newlist = realloc(lst, sizeof(char *) * (i + 2));
267 if (newlist == NULL) {
268 syslog(LOG_ERR, "%s: %m", __func__);
8fc52ced 269 free(n);
fa245a4d
RM
270 return NULL;
271 }
78369646 272 newlist[i] = n;
fd05b7dc
RM
273 newlist[i + 1] = NULL;
274 ifo->environ = newlist;
fd05b7dc
RM
275 return newlist[i];
276}
277
278#define parse_string(buf, len, arg) parse_string_hwaddr(buf, len, arg, 0)
279static ssize_t
34457fe6 280parse_string_hwaddr(char *sbuf, size_t slen, const char *str, int clid)
fd05b7dc 281{
34457fe6 282 size_t l;
fd05b7dc
RM
283 const char *p;
284 int i, punt_last = 0;
285 char c[4];
286
287 /* If surrounded by quotes then it's a string */
288 if (*str == '"') {
289 str++;
290 l = strlen(str);
291 p = str + l - 1;
292 if (*p == '"')
293 punt_last = 1;
294 } else {
34457fe6
RM
295 l = (size_t)hwaddr_aton(NULL, str);
296 if ((ssize_t) l != -1 && l > 1) {
fd05b7dc
RM
297 if (l > slen) {
298 errno = ENOBUFS;
299 return -1;
300 }
301 hwaddr_aton((uint8_t *)sbuf, str);
34457fe6 302 return (ssize_t)l;
fd05b7dc
RM
303 }
304 }
305
306 /* Process escapes */
307 l = 0;
308 /* If processing a string on the clientid, first byte should be
309 * 0 to indicate a non hardware type */
9c9ad2f1 310 if (clid && *str) {
7a911e57
RM
311 if (sbuf)
312 *sbuf++ = 0;
fd05b7dc
RM
313 l++;
314 }
315 c[3] = '\0';
316 while (*str) {
7a911e57 317 if (++l > slen && sbuf) {
fd05b7dc
RM
318 errno = ENOBUFS;
319 return -1;
320 }
321 if (*str == '\\') {
322 str++;
857576b4 323 switch(*str) {
fd05b7dc
RM
324 case '\0':
325 break;
326 case 'b':
7a911e57
RM
327 if (sbuf)
328 *sbuf++ = '\b';
857576b4 329 str++;
fd05b7dc
RM
330 break;
331 case 'n':
7a911e57
RM
332 if (sbuf)
333 *sbuf++ = '\n';
857576b4 334 str++;
fd05b7dc
RM
335 break;
336 case 'r':
7a911e57
RM
337 if (sbuf)
338 *sbuf++ = '\r';
857576b4 339 str++;
fd05b7dc
RM
340 break;
341 case 't':
7a911e57
RM
342 if (sbuf)
343 *sbuf++ = '\t';
857576b4 344 str++;
fd05b7dc
RM
345 break;
346 case 'x':
347 /* Grab a hex code */
348 c[1] = '\0';
349 for (i = 0; i < 2; i++) {
350 if (isxdigit((unsigned char)*str) == 0)
351 break;
352 c[i] = *str++;
353 }
7a911e57 354 if (c[1] != '\0' && sbuf) {
fd05b7dc 355 c[2] = '\0';
34457fe6 356 *sbuf++ = (char)strtol(c, NULL, 16);
fd05b7dc
RM
357 } else
358 l--;
359 break;
360 case '0':
361 /* Grab an octal code */
362 c[2] = '\0';
363 for (i = 0; i < 3; i++) {
364 if (*str < '0' || *str > '7')
365 break;
366 c[i] = *str++;
367 }
7a911e57 368 if (c[2] != '\0' && sbuf) {
34457fe6 369 i = (int)strtol(c, NULL, 8);
fd05b7dc
RM
370 if (i > 255)
371 i = 255;
34457fe6 372 *sbuf ++= (char)i;
fd05b7dc
RM
373 } else
374 l--;
375 break;
376 default:
7a911e57
RM
377 if (sbuf)
378 *sbuf++ = *str;
379 str++;
380 break;
fd05b7dc 381 }
7a911e57
RM
382 } else {
383 if (sbuf)
384 *sbuf++ = *str;
385 str++;
386 }
fd05b7dc 387 }
c35f63c8 388 if (punt_last) {
7a911e57
RM
389 if (sbuf)
390 *--sbuf = '\0';
c35f63c8
RM
391 l--;
392 }
34457fe6 393 return (ssize_t)l;
fd05b7dc
RM
394}
395
ebc9d360 396static int
c73ed171 397parse_iaid1(uint8_t *iaid, const char *arg, size_t len, int n)
ebc9d360
RM
398{
399 unsigned long l;
34457fe6 400 ssize_t s;
ebc9d360
RM
401 uint32_t u32;
402 char *np;
403
404 errno = 0;
405 l = strtoul(arg, &np, 0);
406 if (l <= (unsigned long)UINT32_MAX && errno == 0 && *np == '\0') {
c73ed171 407 if (n)
76bb4d03 408 u32 = htonl((uint32_t)l);
c73ed171 409 else
76bb4d03 410 u32 = (uint32_t)l;
ebc9d360
RM
411 memcpy(iaid, &u32, sizeof(u32));
412 return 0;
413 }
414
415 if ((s = parse_string((char *)iaid, len, arg)) < 1) {
416 syslog(LOG_ERR, "%s: invalid IAID", arg);
417 return -1;
418 }
419 if (s < 4)
420 iaid[3] = '\0';
421 if (s < 3)
422 iaid[2] = '\0';
423 if (s < 2)
424 iaid[1] = '\0';
425 return 0;
426}
427
c73ed171
RM
428static int
429parse_iaid(uint8_t *iaid, const char *arg, size_t len)
430{
431
432 return parse_iaid1(iaid, arg, len, 1);
433}
434
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}
441
ba97e494
RM
442static char **
443splitv(int *argc, char **argv, const char *arg)
444{
fa245a4d 445 char **n, **v = argv;
78369646 446 char *o = strdup(arg), *p, *t, *nt;
ba97e494 447
78369646
RM
448 if (o == NULL) {
449 syslog(LOG_ERR, "%s: %m", __func__);
450 return v;
451 }
ba97e494
RM
452 p = o;
453 while ((t = strsep(&p, ", "))) {
fa245a4d
RM
454 nt = strdup(t);
455 if (nt == NULL) {
456 syslog(LOG_ERR, "%s: %m", __func__);
8fc52ced
RM
457 free(o);
458 return v;
fa245a4d 459 }
34457fe6 460 n = realloc(v, sizeof(char *) * ((size_t)(*argc) + 1));
fa245a4d
RM
461 if (n == NULL) {
462 syslog(LOG_ERR, "%s: %m", __func__);
8fc52ced
RM
463 free(o);
464 free(nt);
465 return v;
fa245a4d
RM
466 }
467 v = n;
8fc52ced 468 v[(*argc)++] = nt;
ba97e494
RM
469 }
470 free(o);
00ababe4 471 return v;
ba97e494
RM
472}
473
1f4c1525 474#ifdef INET
91a44b91
RM
475static int
476parse_addr(struct in_addr *addr, struct in_addr *net, const char *arg)
477{
478 char *p;
479 int i;
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
RM
488 if ((p = strchr(arg, '/')) != NULL) {
489 *p++ = '\0';
490 if (net != NULL &&
491 (sscanf(p, "%d", &i) != 1 ||
eab2229c 492 inet_cidrtoaddr(i, net) != 0))
91a44b91
RM
493 {
494 syslog(LOG_ERR, "`%s' is not a valid CIDR", p);
495 return -1;
496 }
00ababe4 497 }
2f7cb97c 498
91a44b91
RM
499 if (addr != NULL && inet_aton(arg, addr) == 0) {
500 syslog(LOG_ERR, "`%s' is not a valid IP address", arg);
501 return -1;
502 }
2f7cb97c 503 if (p != NULL)
e095a6eb 504 *--p = '/';
8fc52ced 505 else if (net != NULL && addr != NULL)
e88c525f 506 net->s_addr = ipv4_getnetmask(addr->s_addr);
91a44b91 507 return 0;
1f4c1525 508}
aae24feb 509#else
1f4c1525
RM
510static int
511parse_addr(__unused struct in_addr *addr, __unused struct in_addr *net,
512 __unused const char *arg)
513{
514
aae24feb
RM
515 syslog(LOG_ERR, "No IPv4 support");
516 return -1;
91a44b91 517}
1f4c1525 518#endif
91a44b91 519
00ababe4 520static const char *
4eb7b489 521set_option_space(struct dhcpcd_ctx *ctx,
49b749f0
RM
522 const char *arg,
523 const struct dhcp_opt **d, size_t *dl,
524 const struct dhcp_opt **od, size_t *odl,
d7555c12
RM
525 struct if_options *ifo,
526 uint8_t *request[], uint8_t *require[], uint8_t *no[])
527{
528
aae24feb 529#ifdef INET6
d7555c12 530 if (strncmp(arg, "dhcp6_", strlen("dhcp6_")) == 0) {
4eb7b489
RM
531 *d = ctx->dhcp6_opts;
532 *dl = ctx->dhcp6_opts_len;
49b749f0
RM
533 *od = ifo->dhcp6_override;
534 *odl = ifo->dhcp6_override_len;
d7555c12
RM
535 *request = ifo->requestmask6;
536 *require = ifo->requiremask6;
537 *no = ifo->nomask6;
538 return arg + strlen("dhcp6_");
539 }
aae24feb
RM
540#endif
541
542#ifdef INET
4eb7b489
RM
543 *d = ctx->dhcp_opts;
544 *dl = ctx->dhcp_opts_len;
49b749f0
RM
545 *od = ifo->dhcp_override;
546 *odl = ifo->dhcp_override_len;
aae24feb
RM
547#else
548 *d = NULL;
ae4e592f 549 *dl = 0;
49b749f0
RM
550 *od = NULL;
551 *odl = 0;
aae24feb 552#endif
d7555c12
RM
553 *request = ifo->requestmask;
554 *require = ifo->requiremask;
555 *no = ifo->nomask;
556 return arg;
557}
558
1cd05a96 559void
8e7d8c37
RM
560free_dhcp_opt_embenc(struct dhcp_opt *opt)
561{
562 size_t i;
bc22d277 563 struct dhcp_opt *o;
8e7d8c37 564
bc22d277 565 free(opt->var);
8e7d8c37 566
bc22d277 567 for (i = 0, o = opt->embopts; i < opt->embopts_len; i++, o++)
e877fba7 568 free_dhcp_opt_embenc(o);
8e7d8c37
RM
569 free(opt->embopts);
570 opt->embopts_len = 0;
571 opt->embopts = NULL;
572
bc22d277 573 for (i = 0, o = opt->encopts; i < opt->encopts_len; i++, o++)
e877fba7 574 free_dhcp_opt_embenc(o);
8e7d8c37
RM
575 free(opt->encopts);
576 opt->encopts_len = 0;
577 opt->encopts = NULL;
578}
579
1cd05a96
RM
580static char *
581strwhite(const char *s)
582{
583
4dd50184
RM
584 if (s == NULL)
585 return NULL;
1cd05a96
RM
586 while (*s != ' ' && *s != '\t') {
587 if (*s == '\0')
588 return NULL;
589 s++;
590 }
591 return UNCONST(s);
592}
593
594static char *
595strskipwhite(const char *s)
596{
597
4dd50184
RM
598 if (s == NULL)
599 return NULL;
1cd05a96
RM
600 while (*s == ' ' || *s == '\t') {
601 if (*s == '\0')
602 return NULL;
603 s++;
604 }
605 return UNCONST(s);
606}
607
c73ed171
RM
608/* Find the end pointer of a string. */
609static char *
610strend(const char *s)
611{
612
613 s = strskipwhite(s);
614 if (s == NULL)
615 return NULL;
616 if (*s != '"')
617 return strchr(s, ' ');
618 s++;
619 for (; *s != '"' ; s++) {
620 if (*s == '\0')
621 return NULL;
622 if (*s == '\\') {
623 if (*(++s) == '\0')
624 return NULL;
625 }
626 }
627 return UNCONST(++s);
628}
629
fd05b7dc 630static int
4eb7b489
RM
631parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
632 int opt, const char *arg, struct dhcp_opt **ldop, struct dhcp_opt **edop)
fd05b7dc 633{
a93e79c6
RM
634 int i, t;
635 long l;
76bb4d03 636 unsigned long u;
fa245a4d 637 char *p = NULL, *fp, *np, **nconf;
fd05b7dc 638 ssize_t s;
e095a6eb 639 struct in_addr addr, addr2;
fa245a4d 640 in_addr_t *naddr;
91a44b91 641 struct rt *rt;
49b749f0 642 const struct dhcp_opt *d, *od;
d7555c12 643 uint8_t *request, *require, *no;
8e7d8c37 644 struct dhcp_opt **dop, *ndop;
49b749f0 645 size_t *dop_len, dl, odl;
7a911e57 646 struct vivco *vivco;
c73ed171 647 struct token *token;
a93e79c6
RM
648 struct group *grp;
649#ifdef _REENTRANT
a93e79c6
RM
650 struct group grpbuf;
651#endif
1f4c1525 652#ifdef INET6
1f4c1525 653 size_t sl;
ebc9d360
RM
654 struct if_ia *ia;
655 uint8_t iaid[4];
5985c4e2 656 struct if_sla *sla, *slap;
1f4c1525 657#endif
fd05b7dc 658
8e7d8c37
RM
659 dop = NULL;
660 dop_len = NULL;
7a02120f
RM
661#ifdef INET6
662 i = 0;
663#endif
fd05b7dc 664 switch(opt) {
5e2062a4 665 case 'f': /* FALLTHROUGH */
6bfd88f1 666 case 'g': /* FALLTHROUGH */
da166178
RM
667 case 'n': /* FALLTHROUGH */
668 case 'x': /* FALLTHROUGH */
dc60cba4 669 case 'T': /* FALLTHROUGH */
b7f5d1db
RM
670 case 'U': /* FALLTHROUGH */
671 case 'V': /* We need to handle non interface options */
fd05b7dc 672 break;
03c2c879
RM
673 case 'b':
674 ifo->options |= DHCPCD_BACKGROUND;
675 break;
fd05b7dc 676 case 'c':
1681b126
RM
677 free(ifo->script);
678 ifo->script = strdup(arg);
679 if (ifo->script == NULL)
680 syslog(LOG_ERR, "%s: %m", __func__);
fd05b7dc 681 break;
acb1cf88
RM
682 case 'd':
683 ifo->options |= DHCPCD_DEBUG;
684 break;
6bfd88f1
RM
685 case 'e':
686 add_environ(ifo, arg, 1);
687 break;
fd05b7dc 688 case 'h':
cc3c3560
RM
689 if (!arg) {
690 ifo->options |= DHCPCD_HOSTNAME;
691 break;
692 }
693 s = parse_string(ifo->hostname, HOSTNAME_MAX_LEN, arg);
694 if (s == -1) {
695 syslog(LOG_ERR, "hostname: %m");
696 return -1;
697 }
698 if (s != 0 && ifo->hostname[0] == '.') {
699 syslog(LOG_ERR, "hostname cannot begin with .");
700 return -1;
fd05b7dc 701 }
cc3c3560 702 ifo->hostname[s] = '\0';
ed913a59
RM
703 if (ifo->hostname[0] == '\0')
704 ifo->options &= ~DHCPCD_HOSTNAME;
705 else
706 ifo->options |= DHCPCD_HOSTNAME;
fd05b7dc
RM
707 break;
708 case 'i':
709 if (arg)
710 s = parse_string((char *)ifo->vendorclassid + 1,
eab2229c 711 VENDORCLASSID_MAX_LEN, arg);
fd05b7dc
RM
712 else
713 s = 0;
714 if (s == -1) {
765fbf7d 715 syslog(LOG_ERR, "vendorclassid: %m");
fd05b7dc
RM
716 return -1;
717 }
718 *ifo->vendorclassid = (uint8_t)s;
719 break;
2662d519
RM
720 case 'k':
721 ifo->options |= DHCPCD_RELEASE;
722 break;
fd05b7dc
RM
723 case 'l':
724 if (*arg == '-') {
765fbf7d 725 syslog(LOG_ERR,
eab2229c 726 "leasetime must be a positive value");
fd05b7dc
RM
727 return -1;
728 }
729 errno = 0;
ebc9d360 730 ifo->leasetime = (uint32_t)strtoul(arg, NULL, 0);
fd05b7dc 731 if (errno == EINVAL || errno == ERANGE) {
765fbf7d 732 syslog(LOG_ERR, "`%s' out of range", arg);
fd05b7dc
RM
733 return -1;
734 }
735 break;
736 case 'm':
737 ifo->metric = atoint(arg);
738 if (ifo->metric < 0) {
765fbf7d 739 syslog(LOG_ERR, "metric must be a positive value");
fd05b7dc
RM
740 return -1;
741 }
742 break;
743 case 'o':
49b749f0 744 arg = set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
ae4e592f 745 &request, &require, &no);
49b749f0
RM
746 if (make_option_mask(d, dl, od, odl, request, arg, 1) != 0 ||
747 make_option_mask(d, dl, od, odl, no, arg, -1) != 0)
ee56a47d 748 {
765fbf7d 749 syslog(LOG_ERR, "unknown option `%s'", arg);
fd05b7dc
RM
750 return -1;
751 }
752 break;
753 case 'p':
754 ifo->options |= DHCPCD_PERSISTENT;
755 break;
03c2c879
RM
756 case 'q':
757 ifo->options |= DHCPCD_QUIET;
758 break;
2f7cb97c 759 case 'r':
2f7cb97c
RM
760 if (parse_addr(&ifo->req_addr, NULL, arg) != 0)
761 return -1;
5b39d8f5 762 ifo->options |= DHCPCD_REQUEST;
2f7cb97c
RM
763 ifo->req_mask.s_addr = 0;
764 break;
fd05b7dc 765 case 's':
d7555c12
RM
766 if (ifo->options & DHCPCD_IPV6 &&
767 !(ifo->options & DHCPCD_IPV4))
768 {
769 ifo->options |= DHCPCD_INFORM;
770 break;
771 }
2f7cb97c
RM
772 if (arg && *arg != '\0') {
773 if (parse_addr(&ifo->req_addr, &ifo->req_mask,
eab2229c 774 arg) != 0)
91a44b91 775 return -1;
2f7cb97c
RM
776 } else {
777 ifo->req_addr.s_addr = 0;
778 ifo->req_mask.s_addr = 0;
fd05b7dc 779 }
5b39d8f5
RM
780 ifo->options |= DHCPCD_INFORM | DHCPCD_PERSISTENT;
781 ifo->options &= ~(DHCPCD_ARP | DHCPCD_STATIC);
91a44b91 782 break;
fd05b7dc
RM
783 case 't':
784 ifo->timeout = atoint(arg);
785 if (ifo->timeout < 0) {
a2a9a498 786 syslog(LOG_ERR, "timeout must be a positive value");
fd05b7dc
RM
787 return -1;
788 }
789 break;
790 case 'u':
791 s = USERCLASS_MAX_LEN - ifo->userclass[0] - 1;
eab2229c 792 s = parse_string((char *)ifo->userclass +
34457fe6 793 ifo->userclass[0] + 2, (size_t)s, arg);
fd05b7dc 794 if (s == -1) {
765fbf7d 795 syslog(LOG_ERR, "userclass: %m");
fd05b7dc
RM
796 return -1;
797 }
798 if (s != 0) {
34457fe6
RM
799 ifo->userclass[ifo->userclass[0] + 1] = (uint8_t)s;
800 ifo->userclass[0] += (uint8_t)s + 1;
fd05b7dc
RM
801 }
802 break;
803 case 'v':
804 p = strchr(arg, ',');
805 if (!p || !p[1]) {
b357d09f 806 syslog(LOG_ERR, "invalid vendor format: %s", arg);
fd05b7dc
RM
807 return -1;
808 }
95d6dcfa
RM
809
810 /* If vendor starts with , then it is not encapsulated */
811 if (p == arg) {
812 arg++;
813 s = parse_string((char *)ifo->vendor + 1,
814 VENDOR_MAX_LEN, arg);
815 if (s == -1) {
816 syslog(LOG_ERR, "vendor: %m");
817 return -1;
818 }
819 ifo->vendor[0] = (uint8_t)s;
820 ifo->options |= DHCPCD_VENDORRAW;
821 break;
822 }
823
824 /* Encapsulated vendor options */
825 if (ifo->options & DHCPCD_VENDORRAW) {
826 ifo->options &= ~DHCPCD_VENDORRAW;
827 ifo->vendor[0] = 0;
828 }
829
b357d09f 830 /* No need to strip the comma */
fd05b7dc 831 i = atoint(arg);
fd05b7dc 832 if (i < 1 || i > 254) {
765fbf7d 833 syslog(LOG_ERR, "vendor option should be between"
eab2229c 834 " 1 and 254 inclusive");
fd05b7dc
RM
835 return -1;
836 }
b357d09f
RM
837
838 arg = p + 1;
fd05b7dc
RM
839 s = VENDOR_MAX_LEN - ifo->vendor[0] - 2;
840 if (inet_aton(arg, &addr) == 1) {
841 if (s < 6) {
842 s = -1;
843 errno = ENOBUFS;
aeddc61a 844 } else {
fd05b7dc 845 memcpy(ifo->vendor + ifo->vendor[0] + 3,
eab2229c 846 &addr.s_addr, sizeof(addr.s_addr));
aeddc61a
RM
847 s = sizeof(addr.s_addr);
848 }
fd05b7dc 849 } else {
eab2229c 850 s = parse_string((char *)ifo->vendor +
34457fe6 851 ifo->vendor[0] + 3, (size_t)s, arg);
fd05b7dc
RM
852 }
853 if (s == -1) {
765fbf7d 854 syslog(LOG_ERR, "vendor: %m");
fd05b7dc
RM
855 return -1;
856 }
857 if (s != 0) {
34457fe6
RM
858 ifo->vendor[ifo->vendor[0] + 1] = (uint8_t)i;
859 ifo->vendor[ifo->vendor[0] + 2] = (uint8_t)s;
860 ifo->vendor[0] += (uint8_t)s + 2;
fd05b7dc
RM
861 }
862 break;
2a07a2af
RM
863 case 'w':
864 ifo->options |= DHCPCD_WAITIP;
7013b073
RM
865 if (arg != NULL && arg[0] != '\0') {
866 if (arg[0] == '4' || arg[1] == '4')
867 ifo->options |= DHCPCD_WAITIP4;
868 if (arg[0] == '6' || arg[1] == '6')
869 ifo->options |= DHCPCD_WAITIP6;
870 }
2a07a2af 871 break;
a2a9a498
RM
872 case 'y':
873 ifo->reboot = atoint(arg);
874 if (ifo->reboot < 0) {
875 syslog(LOG_ERR, "reboot must be a positive value");
876 return -1;
877 }
878 break;
d3088c74 879 case 'z':
4eb7b489
RM
880 if (ifname == NULL)
881 ctx->ifav = splitv(&ctx->ifac, ctx->ifav, arg);
d3088c74 882 break;
fd05b7dc
RM
883 case 'A':
884 ifo->options &= ~DHCPCD_ARP;
885 /* IPv4LL requires ARP */
886 ifo->options &= ~DHCPCD_IPV4LL;
887 break;
03c2c879
RM
888 case 'B':
889 ifo->options &= ~DHCPCD_DAEMONISE;
890 break;
fd05b7dc
RM
891 case 'C':
892 /* Commas to spaces for shell */
893 while ((p = strchr(arg, ',')))
894 *p = ' ';
34457fe6
RM
895 dl = strlen("skip_hooks=") + strlen(arg) + 1;
896 p = malloc(sizeof(char) * dl);
28382337
RM
897 if (p == NULL) {
898 syslog(LOG_ERR, "%s: %m", __func__);
899 return -1;
900 }
34457fe6 901 snprintf(p, dl, "skip_hooks=%s", arg);
fd05b7dc
RM
902 add_environ(ifo, p, 0);
903 free(p);
904 break;
905 case 'D':
c989b023 906 ifo->options |= DHCPCD_CLIENTID | DHCPCD_DUID;
fd05b7dc
RM
907 break;
908 case 'E':
909 ifo->options |= DHCPCD_LASTLEASE;
910 break;
911 case 'F':
912 if (!arg) {
913 ifo->fqdn = FQDN_BOTH;
914 break;
915 }
916 if (strcmp(arg, "none") == 0)
917 ifo->fqdn = FQDN_NONE;
918 else if (strcmp(arg, "ptr") == 0)
919 ifo->fqdn = FQDN_PTR;
920 else if (strcmp(arg, "both") == 0)
921 ifo->fqdn = FQDN_BOTH;
922 else if (strcmp(arg, "disable") == 0)
923 ifo->fqdn = FQDN_DISABLE;
924 else {
765fbf7d 925 syslog(LOG_ERR, "invalid value `%s' for FQDN", arg);
fd05b7dc
RM
926 return -1;
927 }
928 break;
929 case 'G':
930 ifo->options &= ~DHCPCD_GATEWAY;
931 break;
4242c9b3
RM
932 case 'H':
933 ifo->options |= DHCPCD_XID_HWADDR;
934 break;
fd05b7dc
RM
935 case 'I':
936 /* Strings have a type of 0 */;
937 ifo->clientid[1] = 0;
938 if (arg)
939 s = parse_string_hwaddr((char *)ifo->clientid + 1,
eab2229c 940 CLIENTID_MAX_LEN, arg, 1);
fd05b7dc
RM
941 else
942 s = 0;
943 if (s == -1) {
765fbf7d 944 syslog(LOG_ERR, "clientid: %m");
fd05b7dc
RM
945 return -1;
946 }
c989b023 947 ifo->options |= DHCPCD_CLIENTID;
fd05b7dc 948 ifo->clientid[0] = (uint8_t)s;
fd05b7dc 949 break;
900b3da4
RM
950 case 'J':
951 ifo->options |= DHCPCD_BROADCAST;
952 break;
fd05b7dc
RM
953 case 'K':
954 ifo->options &= ~DHCPCD_LINK;
955 break;
956 case 'L':
957 ifo->options &= ~DHCPCD_IPV4LL;
958 break;
b3174181
RM
959 case 'M':
960 ifo->options |= DHCPCD_MASTER;
961 break;
fd05b7dc 962 case 'O':
49b749f0 963 arg = set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
ae4e592f 964 &request, &require, &no);
49b749f0
RM
965 if (make_option_mask(d, dl, od, odl, request, arg, -1) != 0 ||
966 make_option_mask(d, dl, od, odl, require, arg, -1) != 0 ||
967 make_option_mask(d, dl, od, odl, no, arg, 1) != 0)
fd05b7dc 968 {
765fbf7d 969 syslog(LOG_ERR, "unknown option `%s'", arg);
fd05b7dc
RM
970 return -1;
971 }
972 break;
973 case 'Q':
49b749f0 974 arg = set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
ae4e592f 975 &request, &require, &no);
49b749f0
RM
976 if (make_option_mask(d, dl, od, odl, require, arg, 1) != 0 ||
977 make_option_mask(d, dl, od, odl, request, arg, 1) != 0 ||
978 make_option_mask(d, dl, od, odl, no, arg, -1) != 0)
fd05b7dc 979 {
765fbf7d 980 syslog(LOG_ERR, "unknown option `%s'", arg);
fd05b7dc
RM
981 return -1;
982 }
983 break;
91a44b91
RM
984 case 'S':
985 p = strchr(arg, '=');
986 if (p == NULL) {
987 syslog(LOG_ERR, "static assignment required");
988 return -1;
989 }
990 p++;
991 if (strncmp(arg, "ip_address=", strlen("ip_address=")) == 0) {
776961cf
RM
992 if (parse_addr(&ifo->req_addr,
993 ifo->req_mask.s_addr == 0 ? &ifo->req_mask : NULL,
994 p) != 0)
91a44b91
RM
995 return -1;
996
997 ifo->options |= DHCPCD_STATIC;
fa8b2a7a 998 ifo->options &= ~DHCPCD_INFORM;
776961cf
RM
999 } else if (strncmp(arg, "subnet_mask=", strlen("subnet_mask=")) == 0) {
1000 if (parse_addr(&ifo->req_mask, NULL, p) != 0)
1001 return -1;
91a44b91 1002 } else if (strncmp(arg, "routes=", strlen("routes=")) == 0 ||
eab2229c
RM
1003 strncmp(arg, "static_routes=", strlen("static_routes=")) == 0 ||
1004 strncmp(arg, "classless_static_routes=", strlen("classless_static_routes=")) == 0 ||
1005 strncmp(arg, "ms_classless_static_routes=", strlen("ms_classless_static_routes=")) == 0)
91a44b91 1006 {
1cd05a96 1007 fp = np = strwhite(p);
91a44b91
RM
1008 if (np == NULL) {
1009 syslog(LOG_ERR, "all routes need a gateway");
1010 return -1;
1011 }
1012 *np++ = '\0';
1cd05a96 1013 np = strskipwhite(np);
91a44b91 1014 if (ifo->routes == NULL) {
4c9c4b3e
RM
1015 ifo->routes = malloc(sizeof(*ifo->routes));
1016 if (ifo->routes == NULL) {
10e17e3f 1017 syslog(LOG_ERR, "%s: %m", __func__);
10e17e3f
RM
1018 return -1;
1019 }
673e81e5 1020 TAILQ_INIT(ifo->routes);
4c9c4b3e
RM
1021 }
1022 rt = malloc(sizeof(*rt));
1023 if (rt == NULL) {
1024 syslog(LOG_ERR, "%s: %m", __func__);
1025 *fp = ' ';
1026 return -1;
91a44b91 1027 }
91a44b91
RM
1028 if (parse_addr(&rt->dest, &rt->net, p) == -1 ||
1029 parse_addr(&rt->gate, NULL, np) == -1)
332a5fe6 1030 {
4c9c4b3e 1031 free(rt);
332a5fe6 1032 *fp = ' ';
91a44b91 1033 return -1;
332a5fe6 1034 }
4c9c4b3e 1035 TAILQ_INSERT_TAIL(ifo->routes, rt, next);
332a5fe6 1036 *fp = ' ';
91a44b91
RM
1037 } else if (strncmp(arg, "routers=", strlen("routers=")) == 0) {
1038 if (ifo->routes == NULL) {
4c9c4b3e
RM
1039 ifo->routes = malloc(sizeof(*ifo->routes));
1040 if (ifo->routes == NULL) {
10e17e3f
RM
1041 syslog(LOG_ERR, "%s: %m", __func__);
1042 return -1;
1043 }
00ababe4 1044 TAILQ_INIT(ifo->routes);
4c9c4b3e
RM
1045 }
1046 rt = malloc(sizeof(*rt));
1047 if (rt == NULL) {
1048 syslog(LOG_ERR, "%s: %m", __func__);
1049 return -1;
91a44b91 1050 }
1abffd5b
RM
1051 rt->dest.s_addr = INADDR_ANY;
1052 rt->net.s_addr = INADDR_ANY;
4c9c4b3e
RM
1053 if (parse_addr(&rt->gate, NULL, p) == -1) {
1054 free(rt);
91a44b91 1055 return -1;
4c9c4b3e
RM
1056 }
1057 TAILQ_INSERT_TAIL(ifo->routes, rt, next);
91a44b91 1058 } else {
34457fe6 1059 dl = 0;
91a44b91 1060 if (ifo->config != NULL) {
34457fe6
RM
1061 while (ifo->config[dl] != NULL) {
1062 if (strncmp(ifo->config[dl], arg,
1063 (size_t)(p - arg)) == 0)
eab2229c 1064 {
78369646
RM
1065 p = strdup(arg);
1066 if (p == NULL) {
1067 syslog(LOG_ERR,
1068 "%s: %m", __func__);
1069 return -1;
1070 }
34457fe6
RM
1071 free(ifo->config[dl]);
1072 ifo->config[dl] = p;
91a44b91
RM
1073 return 1;
1074 }
34457fe6 1075 dl++;
91a44b91
RM
1076 }
1077 }
78369646 1078 p = strdup(arg);
fa245a4d
RM
1079 if (p == NULL) {
1080 syslog(LOG_ERR, "%s: %m", __func__);
1081 return -1;
1082 }
34457fe6 1083 nconf = realloc(ifo->config, sizeof(char *) * (dl + 2));
78369646
RM
1084 if (nconf == NULL) {
1085 syslog(LOG_ERR, "%s: %m", __func__);
1086 return -1;
1087 }
fa245a4d 1088 ifo->config = nconf;
34457fe6
RM
1089 ifo->config[dl] = p;
1090 ifo->config[dl + 1] = NULL;
91a44b91
RM
1091 }
1092 break;
bf80d526
RM
1093 case 'W':
1094 if (parse_addr(&addr, &addr2, arg) != 0)
1095 return -1;
1096 if (strchr(arg, '/') == NULL)
1097 addr2.s_addr = INADDR_BROADCAST;
fa245a4d 1098 naddr = realloc(ifo->whitelist,
bf80d526 1099 sizeof(in_addr_t) * (ifo->whitelist_len + 2));
fa245a4d
RM
1100 if (naddr == NULL) {
1101 syslog(LOG_ERR, "%s: %m", __func__);
1102 return -1;
1103 }
1104 ifo->whitelist = naddr;
bf80d526
RM
1105 ifo->whitelist[ifo->whitelist_len++] = addr.s_addr;
1106 ifo->whitelist[ifo->whitelist_len++] = addr2.s_addr;
1107 break;
fd05b7dc 1108 case 'X':
e095a6eb 1109 if (parse_addr(&addr, &addr2, arg) != 0)
fd05b7dc 1110 return -1;
ce6b39df
RM
1111 if (strchr(arg, '/') == NULL)
1112 addr2.s_addr = INADDR_BROADCAST;
fa245a4d 1113 naddr = realloc(ifo->blacklist,
e095a6eb 1114 sizeof(in_addr_t) * (ifo->blacklist_len + 2));
fa245a4d
RM
1115 if (naddr == NULL) {
1116 syslog(LOG_ERR, "%s: %m", __func__);
1117 return -1;
1118 }
1119 ifo->blacklist = naddr;
e095a6eb
RM
1120 ifo->blacklist[ifo->blacklist_len++] = addr.s_addr;
1121 ifo->blacklist[ifo->blacklist_len++] = addr2.s_addr;
fd05b7dc 1122 break;
d3088c74 1123 case 'Z':
4eb7b489
RM
1124 if (ifname == NULL)
1125 ctx->ifdv = splitv(&ctx->ifdc, ctx->ifdv, arg);
d3088c74 1126 break;
d7555c12 1127 case '4':
2f0addfd 1128 ifo->options &= ~DHCPCD_IPV6;
d7555c12
RM
1129 ifo->options |= DHCPCD_IPV4;
1130 break;
1131 case '6':
1132 ifo->options &= ~DHCPCD_IPV4;
2f0addfd 1133 ifo->options |= DHCPCD_IPV6;
d7555c12 1134 break;
94bec972
RM
1135 case O_IPV4:
1136 ifo->options |= DHCPCD_IPV4;
1137 break;
bb8051bf
RM
1138 case O_NOIPV4:
1139 ifo->options &= ~DHCPCD_IPV4;
1140 break;
94bec972
RM
1141 case O_IPV6:
1142 ifo->options |= DHCPCD_IPV6;
1143 break;
bb8051bf
RM
1144 case O_NOIPV6:
1145 ifo->options &= ~DHCPCD_IPV6;
1146 break;
aae24feb 1147#ifdef INET
4ca7460f 1148 case O_ARPING:
48ac077b
RM
1149 while (arg && *arg != '\0') {
1150 fp = strwhite(arg);
1151 if (fp)
1152 *fp++ = '\0';
1153 if (parse_addr(&addr, NULL, arg) != 0)
1154 return -1;
1155 naddr = realloc(ifo->arping,
1156 sizeof(in_addr_t) * (ifo->arping_len + 1));
1157 if (naddr == NULL) {
1158 syslog(LOG_ERR, "%s: %m", __func__);
1159 return -1;
1160 }
1161 ifo->arping = naddr;
1162 ifo->arping[ifo->arping_len++] = addr.s_addr;
4dd50184 1163 arg = strskipwhite(fp);
fa245a4d 1164 }
4ca7460f 1165 break;
41c60e02 1166 case O_DESTINATION:
49b749f0
RM
1167 arg = set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
1168 &request, &require, &no);
1169 if (make_option_mask(d, dl, od, odl,
1170 ifo->dstmask, arg, 2) != 0)
1171 {
41c60e02
RM
1172 if (errno == EINVAL)
1173 syslog(LOG_ERR, "option `%s' does not take"
1174 " an IPv4 address", arg);
1175 else
1176 syslog(LOG_ERR, "unknown option `%s'", arg);
1177 return -1;
1178 }
1179 break;
ff021b0b
RM
1180 case O_FALLBACK:
1181 free(ifo->fallback);
78369646
RM
1182 ifo->fallback = strdup(arg);
1183 if (ifo->fallback == NULL) {
1184 syslog(LOG_ERR, "%s: %m", __func__);
00ababe4 1185 return -1;
78369646 1186 }
ff021b0b 1187 break;
aae24feb 1188#endif
ebc9d360 1189 case O_IAID:
9ff636a5
RM
1190 if (ifname == NULL) {
1191 syslog(LOG_ERR,
1192 "IAID must belong in an interface block");
1193 return -1;
1194 }
ebc9d360
RM
1195 if (parse_iaid(ifo->iaid, arg, sizeof(ifo->iaid)) == -1)
1196 return -1;
1197 ifo->options |= DHCPCD_IAID;
563a421c 1198 break;
eebe9a18
RM
1199 case O_IPV6RS:
1200 ifo->options |= DHCPCD_IPV6RS;
1201 break;
91cd7324 1202 case O_NOIPV6RS:
61dd6cf9
RM
1203 ifo->options &= ~DHCPCD_IPV6RS;
1204 break;
eebe9a18 1205 case O_IPV6RA_FORK:
61dd6cf9 1206 ifo->options &= ~DHCPCD_IPV6RA_REQRDNSS;
91cd7324 1207 break;
eebe9a18
RM
1208 case O_IPV6RA_OWN:
1209 ifo->options |= DHCPCD_IPV6RA_OWN;
1210 break;
1211 case O_IPV6RA_OWN_D:
1212 ifo->options |= DHCPCD_IPV6RA_OWN_DEFAULT;
1213 break;
7dab081f
RM
1214 case O_NOALIAS:
1215 ifo->options |= DHCPCD_NOALIAS;
1216 break;
00ababe4
RM
1217#ifdef INET6
1218 case O_IA_NA:
1219 i = D6_OPTION_IA_NA;
1220 /* FALLTHROUGH */
1221 case O_IA_TA:
1222 if (i == 0)
1223 i = D6_OPTION_IA_TA;
1224 /* FALLTHROUGH */
1225 case O_IA_PD:
9ff636a5
RM
1226 if (i == 0) {
1227 if (ifname == NULL) {
1228 syslog(LOG_ERR,
1229 "IA PD must belong in an interface block");
1230 return -1;
1231 }
00ababe4 1232 i = D6_OPTION_IA_PD;
9ff636a5 1233 }
b0ca9ae9 1234 if (ifname == NULL && arg) {
9ff636a5
RM
1235 syslog(LOG_ERR,
1236 "IA with IAID must belong in an interface block");
1237 return -1;
1238 }
00ababe4 1239 ifo->options |= DHCPCD_IA_FORCED;
52bcdc34
RM
1240 fp = strwhite(arg);
1241 if (fp) {
1242 *fp++ = '\0';
1243 fp = strskipwhite(fp);
1244 }
b0ca9ae9 1245 if (arg) {
b0ca9ae9
RM
1246 p = strchr(arg, '/');
1247 if (p)
1248 *p++ = '\0';
1249 if (parse_iaid(iaid, arg, sizeof(iaid)) == -1)
1250 return -1;
52bcdc34 1251 }
ebc9d360
RM
1252 ia = NULL;
1253 for (sl = 0; sl < ifo->ia_len; sl++) {
52bcdc34 1254 if ((arg == NULL && !ifo->ia[sl].iaid_set) ||
8371b540
RM
1255 (ifo->ia[sl].iaid_set &&
1256 ifo->ia[sl].iaid[0] == iaid[0] &&
ebc9d360
RM
1257 ifo->ia[sl].iaid[1] == iaid[1] &&
1258 ifo->ia[sl].iaid[2] == iaid[2] &&
b0ca9ae9 1259 ifo->ia[sl].iaid[3] == iaid[3]))
00ababe4 1260 {
ebc9d360 1261 ia = &ifo->ia[sl];
00ababe4
RM
1262 break;
1263 }
1264 }
b0ca9ae9
RM
1265 if (ia && ia->ia_type != (uint16_t)i) {
1266 syslog(LOG_ERR, "Cannot mix IA for the same IAID");
1267 break;
1268 }
ebc9d360
RM
1269 if (ia == NULL) {
1270 ia = realloc(ifo->ia,
1271 sizeof(*ifo->ia) * (ifo->ia_len + 1));
1272 if (ia == NULL) {
00ababe4
RM
1273 syslog(LOG_ERR, "%s: %m", __func__);
1274 return -1;
1275 }
ebc9d360
RM
1276 ifo->ia = ia;
1277 ia = &ifo->ia[ifo->ia_len++];
b0ca9ae9 1278 ia->ia_type = (uint16_t)i;
52bcdc34 1279 if (arg) {
b0ca9ae9
RM
1280 ia->iaid[0] = iaid[0];
1281 ia->iaid[1] = iaid[1];
1282 ia->iaid[2] = iaid[2];
1283 ia->iaid[3] = iaid[3];
1284 ia->iaid_set = 1;
1285 } else
1286 ia->iaid_set = 0;
1287 if (!ia->iaid_set ||
1288 p == NULL ||
1289 ia->ia_type == D6_OPTION_IA_TA)
1290 {
4f94ed51
RM
1291 memset(&ia->addr, 0, sizeof(ia->addr));
1292 ia->prefix_len = 0;
1293 } else {
1294 arg = p;
1295 p = strchr(arg, '/');
1296 if (p)
1297 *p++ = '\0';
1298 if (inet_pton(AF_INET6, arg, &ia->addr) == -1) {
1299 syslog(LOG_ERR, "%s: %m", arg);
1300 memset(&ia->addr, 0, sizeof(ia->addr));
1301 }
b0ca9ae9 1302 if (p && ia->ia_type == D6_OPTION_IA_PD) {
4f94ed51
RM
1303 i = atoint(p);
1304 if (i != -1 && (i < 8 || i > 120)) {
1305 errno = EINVAL;
1306 i = -1;
1307 }
1308 if (i == -1) {
1309 syslog(LOG_ERR, "%s: %m", p);
1310 ia->prefix_len = 0;
1311 } else
1312 ia->prefix_len = (uint8_t)i;
1313 }
1314 }
e69793ff 1315 ia->sla_max = 0;
ebc9d360 1316 ia->sla_len = 0;
4f94ed51 1317 ia->sla = NULL;
00ababe4 1318 }
b0ca9ae9 1319 if (ia->ia_type != D6_OPTION_IA_PD)
7cece083 1320 break;
00ababe4 1321 for (p = fp; p; p = fp) {
1cd05a96
RM
1322 fp = strwhite(p);
1323 if (fp) {
00ababe4 1324 *fp++ = '\0';
1cd05a96
RM
1325 fp = strskipwhite(fp);
1326 }
ebc9d360
RM
1327 sla = realloc(ia->sla,
1328 sizeof(*ia->sla) * (ia->sla_len + 1));
00ababe4
RM
1329 if (sla == NULL) {
1330 syslog(LOG_ERR, "%s: %m", __func__);
1331 return -1;
1332 }
ebc9d360
RM
1333 ia->sla = sla;
1334 sla = &ia->sla[ia->sla_len++];
00ababe4
RM
1335 np = strchr(p, '/');
1336 if (np)
1337 *np++ = '\0';
00ababe4
RM
1338 if (strlcpy(sla->ifname, p,
1339 sizeof(sla->ifname)) >= sizeof(sla->ifname))
1340 {
1341 syslog(LOG_ERR, "%s: interface name too long",
1342 arg);
94a79cea 1343 goto err_sla;
00ababe4
RM
1344 }
1345 p = np;
367f7b11
RM
1346 if (p) {
1347 np = strchr(p, '/');
1348 if (np)
1349 *np++ = '\0';
83919266
RM
1350 if (*p == '\0')
1351 sla->sla_set = 0;
1352 else {
1353 errno = 0;
34457fe6
RM
1354 i = atoint(p);
1355 if (i == -1)
1356 goto err_sla;
1357 sla->sla = (uint32_t)i;
94a79cea
RM
1358 if (sla->sla == 0 && ia->sla_len > 1) {
1359 syslog(LOG_ERR, "%s: cannot"
1360 " assign multiple prefixes"
1361 " with a SLA of 0",
1362 ifname);
1363 goto err_sla;
1364 }
83919266
RM
1365 sla->sla_set = 1;
1366 if (errno)
94a79cea 1367 goto err_sla;
83919266 1368 }
367f7b11 1369 if (np) {
34457fe6
RM
1370 i = atoint(np);
1371 if (i < 0 || i > 128)
94a79cea 1372 goto err_sla;
34457fe6 1373 sla->prefix_len = (uint8_t)i;
367f7b11 1374 } else
e69793ff 1375 sla->prefix_len = 0;
5985c4e2
RM
1376 } else {
1377 sla->sla_set = 0;
e69793ff
RM
1378 sla->prefix_len = 0;
1379 }
1380 /* Sanity check */
1381 for (sl = 0; sl < ia->sla_len - 1; sl++) {
1382 slap = &ia->sla[sl];
1383 if (slap->sla_set && sla->sla_set == 0) {
1384 syslog(LOG_WARNING,
1385 "%s: cannot mix automatic "
1386 "and fixed SLA",
1387 sla->ifname);
1388 goto err_sla;
1389 }
1390 if (sla->sla_set == 0 &&
1391 strcmp(slap->ifname, sla->ifname) == 0)
1392 {
1393 syslog(LOG_WARNING,
1394 "%s: cannot specify the "
1395 "same interface twice with "
1396 "an automatic SLA",
1397 sla->ifname);
1398 goto err_sla;
5985c4e2 1399 }
367f7b11 1400 }
e69793ff
RM
1401 if (sla->sla_set && sla->sla > ia->sla_max)
1402 ia->sla_max = sla->sla;
00ababe4 1403 }
ebc9d360 1404 break;
94a79cea
RM
1405err_sla:
1406 ia->sla_len--;
1407 return -1;
1408#endif
d6a18654 1409 case O_HOSTNAME_SHORT:
9c54b11b 1410 ifo->options |= DHCPCD_HOSTNAME | DHCPCD_HOSTNAME_SHORT;
d6a18654 1411 break;
413652c1 1412 case O_DEV:
4eb7b489
RM
1413#ifdef PLUGIN_DEV
1414 if (ctx->dev_load)
1415 free(ctx->dev_load);
1416 ctx->dev_load = strdup(arg);
1417#endif
413652c1
RM
1418 break;
1419 case O_NODEV:
1420 ifo->options &= ~DHCPCD_DEV;
1421 break;
8e7d8c37
RM
1422 case O_DEFINE:
1423 dop = &ifo->dhcp_override;
1424 dop_len = &ifo->dhcp_override_len;
1425 /* FALLTHROUGH */
1426 case O_DEFINE6:
1427 if (dop == NULL) {
1428 dop = &ifo->dhcp6_override;
1429 dop_len = &ifo->dhcp6_override_len;
1430 }
7a911e57
RM
1431 /* FALLTHROUGH */
1432 case O_VENDOPT:
1433 if (dop == NULL) {
1434 dop = &ifo->vivso_override;
1435 dop_len = &ifo->vivso_override_len;
1436 }
4eb7b489 1437 *edop = *ldop = NULL;
8e7d8c37
RM
1438 /* FALLTHROUGH */
1439 case O_EMBED:
1440 if (dop == NULL) {
4eb7b489
RM
1441 if (*edop) {
1442 dop = &(*edop)->embopts;
1443 dop_len = &(*edop)->embopts_len;
03476881 1444 } else if (ldop) {
4eb7b489
RM
1445 dop = &(*ldop)->embopts;
1446 dop_len = &(*ldop)->embopts_len;
03476881
RM
1447 } else {
1448 syslog(LOG_ERR,
1449 "embed must be after a define or encap");
8e7d8c37
RM
1450 return -1;
1451 }
8e7d8c37 1452 }
03476881 1453 /* FALLTHROUGH */
8e7d8c37
RM
1454 case O_ENCAP:
1455 if (dop == NULL) {
4eb7b489 1456 if (*ldop == NULL) {
8e7d8c37
RM
1457 syslog(LOG_ERR, "encap must be after a define");
1458 return -1;
1459 }
4eb7b489
RM
1460 dop = &(*ldop)->encopts;
1461 dop_len = &(*ldop)->encopts_len;
8e7d8c37
RM
1462 }
1463
1464 /* Shared code for define, define6, embed and encap */
1465
1466 /* code */
1467 if (opt == O_EMBED) /* Embedded options don't have codes */
7a911e57 1468 u = 0;
8e7d8c37 1469 else {
1cd05a96 1470 fp = strwhite(arg);
4dd50184 1471 if (fp == NULL) {
8e7d8c37
RM
1472 syslog(LOG_ERR, "invalid syntax: %s", arg);
1473 return -1;
1474 }
1475 *fp++ = '\0';
7a911e57
RM
1476 errno = 0;
1477 u = strtoul(arg, &np, 0);
1478 if (u > UINT32_MAX || errno != 0 || *np != '\0') {
1479 syslog(LOG_ERR, "invalid code: %s", arg);
8e7d8c37 1480 return -1;
7a911e57 1481 }
1cd05a96 1482 arg = strskipwhite(fp);
4dd50184
RM
1483 if (arg == NULL) {
1484 syslog(LOG_ERR, "invalid syntax");
1485 return -1;
1486 }
8e7d8c37
RM
1487 }
1488 /* type */
1cd05a96 1489 fp = strwhite(arg);
ab371aa6 1490 if (fp)
1cd05a96 1491 *fp++ = '\0';
b21cd906
RM
1492 np = strchr(arg, ':');
1493 /* length */
1494 if (np) {
1495 *np++ = '\0';
1496 if ((l = atoint(np)) == -1)
1497 return -1;
1498 } else
1499 l = 0;
1cd05a96
RM
1500 t = 0;
1501 if (strcasecmp(arg, "request") == 0) {
1502 t |= REQUEST;
1503 arg = strskipwhite(fp);
1504 fp = strwhite(arg);
1505 if (fp == NULL) {
1506 syslog(LOG_ERR, "incomplete request type");
1507 return -1;
1508 }
1509 *fp++ = '\0';
1510 } else if (strcasecmp(arg, "norequest") == 0) {
1511 t |= NOREQ;
1512 arg = strskipwhite(fp);
1513 fp = strwhite(arg);
1514 if (fp == NULL) {
1515 syslog(LOG_ERR, "incomplete request type");
1516 return -1;
1517 }
8e7d8c37 1518 *fp++ = '\0';
1cd05a96 1519 }
03476881
RM
1520 if (strcasecmp(arg, "index") == 0) {
1521 t |= INDEX;
1522 arg = strskipwhite(fp);
1523 fp = strwhite(arg);
1524 if (fp == NULL) {
1525 syslog(LOG_ERR, "incomplete index type");
1526 return -1;
1527 }
1528 *fp++ = '\0';
1529 }
1cd05a96
RM
1530 if (strcasecmp(arg, "array") == 0) {
1531 t |= ARRAY;
1532 arg = strskipwhite(fp);
1533 fp = strwhite(arg);
1534 if (fp == NULL) {
1535 syslog(LOG_ERR, "incomplete array type");
1536 return -1;
1537 }
1538 *fp++ = '\0';
1539 }
8e7d8c37 1540 if (strcasecmp(arg, "ipaddress") == 0)
1cd05a96 1541 t |= ADDRIPV4;
8e7d8c37 1542 else if (strcasecmp(arg, "ip6address") == 0)
1cd05a96 1543 t |= ADDRIPV6;
8e7d8c37 1544 else if (strcasecmp(arg, "string") == 0)
1cd05a96 1545 t |= STRING;
8e7d8c37 1546 else if (strcasecmp(arg, "byte") == 0)
1cd05a96 1547 t |= UINT8;
8e7d8c37 1548 else if (strcasecmp(arg, "uint16") == 0)
1cd05a96 1549 t |= UINT16;
8e7d8c37 1550 else if (strcasecmp(arg, "int16") == 0)
1cd05a96 1551 t |= SINT16;
8e7d8c37 1552 else if (strcasecmp(arg, "uint32") == 0)
1cd05a96 1553 t |= UINT32;
8e7d8c37 1554 else if (strcasecmp(arg, "int32") == 0)
1cd05a96
RM
1555 t |= SINT32;
1556 else if (strcasecmp(arg, "flag") == 0)
1557 t |= FLAG;
8f008ca7
RM
1558 else if (strcasecmp(arg, "raw") == 0)
1559 t |= STRING | RAW;
1560 else if (strcasecmp(arg, "ascii") == 0)
1561 t |= STRING | ASCII;
8e7d8c37 1562 else if (strcasecmp(arg, "domain") == 0)
8f008ca7
RM
1563 t |= STRING | DOMAIN | RFC3397;
1564 else if (strcasecmp(arg, "dname") == 0)
1565 t |= STRING | DOMAIN;
8e7d8c37 1566 else if (strcasecmp(arg, "binhex") == 0)
8f008ca7 1567 t |= STRING | BINHEX;
8e7d8c37 1568 else if (strcasecmp(arg, "embed") == 0)
03476881 1569 t |= EMBED;
8e7d8c37 1570 else if (strcasecmp(arg, "encap") == 0)
03476881 1571 t |= ENCAP;
1cd05a96 1572 else if (strcasecmp(arg, "rfc3361") ==0)
03476881 1573 t |= STRING | RFC3361;
dfee59d2 1574 else if (strcasecmp(arg, "rfc3442") ==0)
03476881 1575 t |= STRING | RFC3442;
1cd05a96 1576 else if (strcasecmp(arg, "rfc5969") == 0)
03476881
RM
1577 t |= STRING | RFC5969;
1578 else if (strcasecmp(arg, "option") == 0)
1579 t |= OPTION;
8e7d8c37
RM
1580 else {
1581 syslog(LOG_ERR, "unknown type: %s", arg);
1582 return -1;
1583 }
b21cd906
RM
1584 if (l && !(t & (STRING | BINHEX))) {
1585 syslog(LOG_WARNING,
1586 "ignoring length for type `%s'", arg);
1587 l = 0;
1588 }
8f008ca7
RM
1589 if (t & ARRAY && t & (STRING | BINHEX) &&
1590 !(t & (RFC3397 | DOMAIN)))
1591 {
63bdd2c2
RM
1592 syslog(LOG_WARNING, "ignoring array for strings");
1593 t &= ~ARRAY;
1594 }
8e7d8c37 1595 /* variable */
d9fbb118 1596 if (!fp) {
03476881
RM
1597 if (!(t & OPTION)) {
1598 syslog(LOG_ERR,
1599 "type %s requires a variable name", arg);
1600 return -1;
1601 }
1602 np = NULL;
1603 } else {
1604 arg = strskipwhite(fp);
1605 fp = strwhite(arg);
1606 if (fp)
1607 *fp++ = '\0';
1608 np = strdup(arg);
1609 if (np == NULL) {
1610 syslog(LOG_ERR, "%s: %m", __func__);
1611 return -1;
1612 }
8e7d8c37 1613 }
6714b786 1614 if (opt != O_EMBED) {
f132acc3
RM
1615 for (dl = 0, ndop = *dop; dl < *dop_len; dl++, ndop++)
1616 {
1cd05a96
RM
1617 /* type 0 seems freshly malloced struct
1618 * for us to use */
7a911e57 1619 if (ndop->option == u || ndop->type == 0)
8e7d8c37
RM
1620 break;
1621 }
f132acc3
RM
1622 if (dl == *dop_len)
1623 ndop = NULL;
1624 } else
1625 ndop = NULL;
6714b786 1626 if (ndop == NULL) {
8e7d8c37 1627 if ((ndop = realloc(*dop,
f132acc3
RM
1628 sizeof(**dop) * ((*dop_len) + 1))) == NULL)
1629 {
8e7d8c37 1630 syslog(LOG_ERR, "%s: %m", __func__);
8fc52ced 1631 free(np);
8e7d8c37
RM
1632 return -1;
1633 }
1634 *dop = ndop;
1635 ndop = &(*dop)[(*dop_len)++];
8e7d8c37
RM
1636 ndop->embopts = NULL;
1637 ndop->embopts_len = 0;
1638 ndop->encopts = NULL;
1639 ndop->encopts_len = 0;
1640 } else
1641 free_dhcp_opt_embenc(ndop);
76bb4d03 1642 ndop->option = (uint32_t)u; /* could have been 0 */
8e7d8c37 1643 ndop->type = t;
34457fe6 1644 ndop->len = (size_t)l;
bc22d277 1645 ndop->var = np;
8e7d8c37 1646 /* Save the define for embed and encap options */
7a911e57 1647 if (opt == O_DEFINE || opt == O_DEFINE6 || opt == O_VENDOPT)
4eb7b489 1648 *ldop = ndop;
03476881 1649 else if (opt == O_ENCAP)
4eb7b489 1650 *edop = ndop;
8e7d8c37 1651 break;
7a911e57
RM
1652 case O_VENDCLASS:
1653 fp = strwhite(arg);
1654 if (fp)
1655 *fp++ = '\0';
1656 errno = 0;
1657 u = strtoul(arg, &np, 0);
1658 if (u > UINT32_MAX || errno != 0 || *np != '\0') {
1659 syslog(LOG_ERR, "invalid code: %s", arg);
1660 return -1;
1661 }
1662 if (fp) {
1663 s = parse_string(NULL, 0, fp);
1664 if (s == -1) {
1665 syslog(LOG_ERR, "%s: %m", __func__);
1666 return -1;
1667 }
34457fe6
RM
1668 dl = (size_t)s;
1669 if (dl + (sizeof(uint16_t) * 2) > UINT16_MAX) {
7a911e57
RM
1670 syslog(LOG_ERR, "vendor class is too big");
1671 return -1;
1672 }
34457fe6 1673 np = malloc(dl);
7a911e57
RM
1674 if (np == NULL) {
1675 syslog(LOG_ERR, "%s: %m", __func__);
1676 return -1;
1677 }
34457fe6 1678 parse_string(np, dl, fp);
7a911e57 1679 } else {
34457fe6 1680 dl = 0;
7a911e57
RM
1681 np = NULL;
1682 }
900b6f94
RM
1683 vivco = realloc(ifo->vivco, sizeof(*ifo->vivco) *
1684 (ifo->vivco_len + 1));
7a911e57
RM
1685 if (vivco == NULL) {
1686 syslog(LOG_ERR, "%s: %m", __func__);
1687 return -1;
1688 }
1689 ifo->vivco = vivco;
76bb4d03 1690 ifo->vivco_en = (uint32_t)u;
7a911e57 1691 vivco = &ifo->vivco[ifo->vivco_len++];
34457fe6 1692 vivco->len = dl;
7a911e57
RM
1693 vivco->data = (uint8_t *)np;
1694 break;
c73ed171
RM
1695 case O_AUTHPROTOCOL:
1696 fp = strwhite(arg);
1697 if (fp)
1698 *fp++ = '\0';
1699 if (strcasecmp(arg, "token") == 0)
1700 ifo->auth.protocol = AUTH_PROTO_TOKEN;
1701 else if (strcasecmp(arg, "delayed") == 0)
1702 ifo->auth.protocol = AUTH_PROTO_DELAYED;
1703 else if (strcasecmp(arg, "delayedrealm") == 0)
1704 ifo->auth.protocol = AUTH_PROTO_DELAYEDREALM;
1705 else {
1706 syslog(LOG_ERR, "%s: unsupported protocol", arg);
1707 return -1;
1708 }
1709 arg = strskipwhite(fp);
1710 fp = strwhite(arg);
1711 if (arg == NULL) {
1712 ifo->auth.options |= DHCPCD_AUTH_SEND;
1713 ifo->auth.algorithm = AUTH_ALG_HMAC_MD5;
1714 ifo->auth.rdm = AUTH_RDM_MONOTONIC;
1715 break;
1716 }
1717 if (fp)
1718 *fp++ = '\0';
1719 if (strcasecmp(arg, "hmacmd5") == 0 ||
1720 strcasecmp(arg, "hmac-md5") == 0)
1721 ifo->auth.algorithm = AUTH_ALG_HMAC_MD5;
1722 else {
1723 syslog(LOG_ERR, "%s: unsupported algorithm", arg);
1724 return 1;
1725 }
1726 arg = fp;
1727 if (arg == NULL) {
1728 ifo->auth.options |= DHCPCD_AUTH_SEND;
1729 ifo->auth.rdm = AUTH_RDM_MONOTONIC;
1730 break;
1731 }
cf0840ef
RM
1732 if (strcasecmp(arg, "monocounter") == 0) {
1733 ifo->auth.rdm = AUTH_RDM_MONOTONIC;
1734 ifo->auth.options |= DHCPCD_AUTH_RDM_COUNTER;
1735 } else if (strcasecmp(arg, "monotonic") ==0 ||
1736 strcasecmp(arg, "monotime") == 0)
c73ed171
RM
1737 ifo->auth.rdm = AUTH_RDM_MONOTONIC;
1738 else {
1739 syslog(LOG_ERR, "%s: unsupported RDM", arg);
1740 return -1;
1741 }
35fef8ed 1742 ifo->auth.options |= DHCPCD_AUTH_SEND;
c73ed171
RM
1743 break;
1744 case O_AUTHTOKEN:
1745 fp = strwhite(arg);
1746 if (fp == NULL) {
1747 syslog(LOG_ERR, "authtoken requires a realm");
1748 return -1;
1749 }
1750 *fp++ = '\0';
1751 token = malloc(sizeof(*token));
1752 if (token == NULL) {
1753 syslog(LOG_ERR, "%s: %m", __func__);
a8c8e686 1754 free(token);
c73ed171
RM
1755 return -1;
1756 }
1757 if (parse_uint32(&token->secretid, arg) == -1) {
1758 syslog(LOG_ERR, "%s: not a number", arg);
1759 free(token);
1760 return -1;
1761 }
1762 arg = fp;
1763 fp = strend(arg);
1764 if (fp == NULL) {
1765 syslog(LOG_ERR, "authtoken requies an a key");
1766 free(token);
1767 return -1;
1768 }
1769 *fp++ = '\0';
34457fe6
RM
1770 s = parse_string(NULL, 0, arg);
1771 if (s == -1) {
1772 syslog(LOG_ERR, "realm_len: %m");
a8c8e686 1773 free(token);
34457fe6
RM
1774 return -1;
1775 }
1776 if (s) {
1777 token->realm_len = (size_t)s;
c73ed171
RM
1778 token->realm = malloc(token->realm_len);
1779 if (token->realm == NULL) {
1780 free(token);
1781 syslog(LOG_ERR, "%s: %m", __func__);
1782 return -1;
1783 }
1784 parse_string((char *)token->realm, token->realm_len,
1785 arg);
34457fe6
RM
1786 } else {
1787 token->realm_len = 0;
1788 token->realm = NULL;
c73ed171
RM
1789 }
1790 arg = fp;
1791 fp = strend(arg);
1792 if (fp == NULL) {
1793 syslog(LOG_ERR, "authtoken requies an an expiry date");
1794 free(token->realm);
1795 free(token);
1796 return -1;
1797 }
1798 *fp++ = '\0';
1799 if (*arg == '"') {
1800 arg++;
1801 np = strchr(arg, '"');
1802 if (np)
1803 *np = '\0';
1804 }
1805 if (strcmp(arg, "0") == 0 || strcasecmp(arg, "forever") == 0)
1806 token->expire =0;
1807 else {
1808 struct tm tm;
1809
1810 memset(&tm, 0, sizeof(tm));
1811 if (strptime(arg, "%Y-%m-%d %H:%M", &tm) == NULL) {
1812 syslog(LOG_ERR, "%s: invalid date time", arg);
1813 free(token->realm);
1814 free(token);
1815 return -1;
1816 }
1817 if ((token->expire = mktime(&tm)) == (time_t)-1) {
1818 syslog(LOG_ERR, "%s: mktime: %m", __func__);
1819 free(token->realm);
1820 free(token);
1821 return -1;
1822 }
1823 }
1824 arg = fp;
34457fe6
RM
1825 s = parse_string(NULL, 0, arg);
1826 if (s == -1 || s == 0) {
1827 syslog(LOG_ERR, s == -1 ? "token_len: %m" :
1828 "authtoken needs a key");
c73ed171
RM
1829 free(token->realm);
1830 free(token);
1831 return -1;
1832 }
34457fe6 1833 token->key_len = (size_t)s;
c73ed171
RM
1834 token->key = malloc(token->key_len);
1835 parse_string((char *)token->key, token->key_len, arg);
1836 TAILQ_INSERT_TAIL(&ifo->auth.tokens, token, next);
1837 break;
1838 case O_AUTHNOTREQUIRED:
1839 ifo->auth.options &= ~DHCPCD_AUTH_REQUIRE;
1840 break;
94bec972
RM
1841 case O_DHCP:
1842 ifo->options |= DHCPCD_DHCP | DHCPCD_IPV4;
1843 break;
d4154ba7
RM
1844 case O_NODHCP:
1845 ifo->options &= ~DHCPCD_DHCP;
1846 break;
94bec972
RM
1847 case O_DHCP6:
1848 ifo->options |= DHCPCD_DHCP6 | DHCPCD_IPV6;
1849 break;
d4154ba7
RM
1850 case O_NODHCP6:
1851 ifo->options &= ~DHCPCD_DHCP6;
1852 break;
a93e79c6
RM
1853 case O_CONTROLGRP:
1854#ifdef _REENTRANT
1855 l = sysconf(_SC_GETGR_R_SIZE_MAX);
1856 if (l == -1)
1857 dl = 1024;
1858 else
1859 dl = (size_t)l;
1860 p = malloc(dl);
1861 if (p == NULL) {
1862 syslog(LOG_ERR, "%s: malloc: %m", __func__);
1863 return -1;
1864 }
1865 while ((i = getgrnam_r(arg, &grpbuf, p, (size_t)l, &grp)) ==
1866 ERANGE)
1867 {
1868 size_t nl = dl * 2;
1869 if (nl < dl) {
1870 syslog(LOG_ERR, "control_group: out of buffer");
1871 free(p);
1872 return -1;
1873 }
1874 dl = nl;
1875 np = realloc(p, dl);
1876 if (np == NULL) {
1877 syslog(LOG_ERR, "control_group: realloc: %m");
1878 free(p);
1879 return -1;
1880 }
1881 p = np;
1882 }
1883 if (i != 0) {
1884 errno = i;
1885 syslog(LOG_ERR, "getgrnam_r: %m");
1886 free(p);
1887 return -1;
1888 }
1889 if (grp == NULL) {
1890 syslog(LOG_ERR, "controlgroup: %s: not found", arg);
1891 free(p);
1892 return -1;
1893 }
1894 ctx->control_group = grp->gr_gid;
1895 free(p);
1896#else
1897 grp = getgrnam(arg);
1898 if (grp == NULL) {
1899 syslog(LOG_ERR, "controlgroup: %s: not found", arg);
1900 return -1;
1901 }
1902 ctx->control_group = grp->gr_gid;
1903#endif
1904 break;
ee56a47d
RM
1905 case O_GATEWAY:
1906 ifo->options |= DHCPCD_GATEWAY;
1907 break;
1aeaf0e7
RM
1908 case O_SLAAC:
1909 if (strcmp(arg, "private") == 0 ||
1910 strcmp(arg, "stableprivate") == 0 ||
1911 strcmp(arg, "stable") == 0)
1912 ifo->options |= DHCPCD_SLAACPRIVATE;
1913 else
1914 ifo->options &= ~DHCPCD_SLAACPRIVATE;
3ed12ab8 1915 break;
9d5cb9f9
RM
1916 case O_PFXDLGMIX:
1917 ifo->options |= DHCPCD_PFXDLGMIX;
fb8a0db7 1918 break;
fd05b7dc
RM
1919 default:
1920 return 0;
1921 }
1922
1923 return 1;
1924}
1925
1926static int
4eb7b489
RM
1927parse_config_line(struct dhcpcd_ctx *ctx, const char *ifname,
1928 struct if_options *ifo, const char *opt, char *line,
1929 struct dhcp_opt **ldop, struct dhcp_opt **edop)
fd05b7dc
RM
1930{
1931 unsigned int i;
1932
1933 for (i = 0; i < sizeof(cf_options) / sizeof(cf_options[0]); i++) {
1934 if (!cf_options[i].name ||
1935 strcmp(cf_options[i].name, opt) != 0)
1936 continue;
1937
1938 if (cf_options[i].has_arg == required_argument && !line) {
1939 fprintf(stderr,
eab2229c
RM
1940 PACKAGE ": option requires an argument -- %s\n",
1941 opt);
fd05b7dc
RM
1942 return -1;
1943 }
1944
4eb7b489
RM
1945 return parse_option(ctx, ifname, ifo, cf_options[i].val, line,
1946 ldop, edop);
fd05b7dc
RM
1947 }
1948
4eb7b489 1949 syslog(LOG_ERR, "unknown option: %s", opt);
fd05b7dc
RM
1950 return -1;
1951}
1952
741f46c6 1953static void
b0272a9d 1954finish_config(struct if_options *ifo)
741f46c6
RM
1955{
1956
1957 /* Terminate the encapsulated options */
1958 if (ifo->vendor[0] && !(ifo->options & DHCPCD_VENDORRAW)) {
1959 ifo->vendor[0]++;
1960 ifo->vendor[ifo->vendor[0]] = DHO_END;
aeddc61a
RM
1961 /* We are called twice.
1962 * This should be fixed, but in the meantime, this
1963 * guard should suffice */
1964 ifo->options |= DHCPCD_VENDORRAW;
741f46c6 1965 }
b0272a9d 1966}
741f46c6 1967
c1b54b57
RM
1968/* Handy routine to read very long lines in text files.
1969 * This means we read the whole line and avoid any nasty buffer overflows.
1970 * We strip leading space and avoid comment lines, making the code that calls
1971 * us smaller. */
1972static char *
4eb7b489
RM
1973get_line(char ** __restrict buf, size_t * __restrict buflen,
1974 FILE * __restrict fp)
c1b54b57
RM
1975{
1976 char *p;
1977 ssize_t bytes;
1978
1979 do {
1980 bytes = getline(buf, buflen, fp);
1981 if (bytes == -1)
1982 return NULL;
1983 for (p = *buf; *p == ' ' || *p == '\t'; p++)
1984 ;
1985 } while (*p == '\0' || *p == '\n' || *p == '#' || *p == ';');
1986 if ((*buf)[--bytes] == '\n')
1987 (*buf)[bytes] = '\0';
1988 return p;
1989}
1990
fd05b7dc 1991struct if_options *
4eb7b489 1992read_config(struct dhcpcd_ctx *ctx,
6f767217 1993 const char *ifname, const char *ssid, const char *profile)
fd05b7dc
RM
1994{
1995 struct if_options *ifo;
c1b54b57
RM
1996 FILE *fp;
1997 char *line, *buf, *option, *p;
1998 size_t buflen;
fec195b5 1999 ssize_t vlen;
6f767217 2000 int skip = 0, have_profile = 0;
1cd05a96 2001#ifndef EMBEDDED_CONFIG
e2e644e9 2002 const char * const *e;
c1b54b57 2003 size_t ol;
1cd05a96 2004#endif
d9fbb118 2005#if !defined(INET) || !defined(INET6)
10cac699 2006 size_t i;
d9fbb118
RM
2007 struct dhcp_opt *opt;
2008#endif
4eb7b489 2009 struct dhcp_opt *ldop, *edop;
fd05b7dc
RM
2010
2011 /* Seed our default options */
10e17e3f
RM
2012 ifo = calloc(1, sizeof(*ifo));
2013 if (ifo == NULL) {
2014 syslog(LOG_ERR, "%s: %m", __func__);
2015 return NULL;
2016 }
aae24feb 2017 ifo->options |= DHCPCD_DAEMONISE | DHCPCD_LINK;
413652c1
RM
2018#ifdef PLUGIN_DEV
2019 ifo->options |= DHCPCD_DEV;
2020#endif
aae24feb 2021#ifdef INET
d4154ba7 2022 ifo->options |= DHCPCD_IPV4 | DHCPCD_DHCP | DHCPCD_IPV4LL;
00ababe4 2023 ifo->options |= DHCPCD_GATEWAY | DHCPCD_ARP;
aae24feb
RM
2024#endif
2025#ifdef INET6
d7555c12 2026 ifo->options |= DHCPCD_IPV6 | DHCPCD_IPV6RS | DHCPCD_IPV6RA_REQRDNSS;
d4154ba7 2027 ifo->options |= DHCPCD_DHCP6;
aae24feb 2028#endif
fd05b7dc 2029 ifo->timeout = DEFAULT_TIMEOUT;
a2a9a498 2030 ifo->reboot = DEFAULT_REBOOT;
f43e5853 2031 ifo->metric = -1;
c73ed171
RM
2032 ifo->auth.options |= DHCPCD_AUTH_REQUIRE;
2033 TAILQ_INIT(&ifo->auth.tokens);
793c4286 2034
fec195b5
RM
2035 vlen = dhcp_vendor((char *)ifo->vendorclassid + 1,
2036 sizeof(ifo->vendorclassid) - 1);
2037 ifo->vendorclassid[0] = vlen == -1 ? 0 : (uint8_t)vlen;
fd05b7dc 2038
c1b54b57
RM
2039 buf = NULL;
2040 buflen = 0;
2041
8e7d8c37 2042 /* Parse our embedded options file */
1cd05a96
RM
2043 if (ifname == NULL) {
2044 /* Space for initial estimates */
2045#if defined(INET) && defined(INITDEFINES)
2046 ifo->dhcp_override =
2047 calloc(INITDEFINES, sizeof(*ifo->dhcp_override));
2048 if (ifo->dhcp_override == NULL)
2049 syslog(LOG_ERR, "%s: %m", __func__);
2050 else
2051 ifo->dhcp_override_len = INITDEFINES;
2052#endif
2053
2054#if defined(INET6) && defined(INITDEFINE6S)
2055 ifo->dhcp6_override =
2056 calloc(INITDEFINE6S, sizeof(*ifo->dhcp6_override));
2057 if (ifo->dhcp6_override == NULL)
2058 syslog(LOG_ERR, "%s: %m", __func__);
2059 else
d9fbb118 2060 ifo->dhcp6_override_len = INITDEFINE6S;
1cd05a96
RM
2061#endif
2062
2063 /* Now load our embedded config */
2064#ifdef EMBEDDED_CONFIG
c1b54b57
RM
2065 fp = fopen(EMBEDDED_CONFIG, "r");
2066 if (fp == NULL)
1cd05a96
RM
2067 syslog(LOG_ERR, "fopen `%s': %m", EMBEDDED_CONFIG);
2068
c1b54b57 2069 while (fp && (line = get_line(&buf, &buflen, fp))) {
1cd05a96 2070#else
a319fa53
RM
2071 buflen = 80;
2072 buf = malloc(buflen);
2073 if (buf == NULL) {
1cd05a96
RM
2074 syslog(LOG_ERR, "%s: %m", __func__);
2075 return NULL;
2076 }
4eb7b489 2077 ldop = edop = NULL;
1cd05a96
RM
2078 for (e = dhcpcd_embedded_conf; *e; e++) {
2079 ol = strlen(*e) + 1;
a319fa53 2080 if (ol > buflen) {
a319fa53 2081 buflen = ol;
967db595 2082 buf = realloc(buf, buflen);
a319fa53 2083 if (buf == NULL) {
1cd05a96 2084 syslog(LOG_ERR, "%s: %m", __func__);
967db595 2085 free(buf);
1cd05a96
RM
2086 return NULL;
2087 }
2088 }
a319fa53
RM
2089 memcpy(buf, *e, ol);
2090 line = buf;
1cd05a96 2091#endif
8e7d8c37 2092 option = strsep(&line, " \t");
ab371aa6
RM
2093 if (line)
2094 line = strskipwhite(line);
8e7d8c37
RM
2095 /* Trim trailing whitespace */
2096 if (line && *line) {
2097 p = line + strlen(line) - 1;
2098 while (p != line &&
2099 (*p == ' ' || *p == '\t') &&
2100 *(p - 1) != '\\')
2101 *p-- = '\0';
2102 }
4eb7b489
RM
2103 parse_config_line(ctx, NULL, ifo, option, line,
2104 &ldop, &edop);
1cd05a96 2105
8e7d8c37 2106 }
1cd05a96
RM
2107
2108#ifdef EMBEDDED_CONFIG
c1b54b57
RM
2109 if (fp)
2110 fclose(fp);
1cd05a96
RM
2111#endif
2112#ifdef INET
4eb7b489
RM
2113 ctx->dhcp_opts = ifo->dhcp_override;
2114 ctx->dhcp_opts_len = ifo->dhcp_override_len;
1cd05a96 2115#else
d9fbb118
RM
2116 for (i = 0, opt = ifo->dhcp_override;
2117 i < ifo->dhcp_override_len;
2118 i++, opt++)
2119 free_dhcp_opt_embenc(opt);
1cd05a96
RM
2120 free(ifo->dhcp_override);
2121#endif
2122 ifo->dhcp_override = NULL;
2123 ifo->dhcp_override_len = 0;
2124
2125#ifdef INET6
4eb7b489
RM
2126 ctx->dhcp6_opts = ifo->dhcp6_override;
2127 ctx->dhcp6_opts_len = ifo->dhcp6_override_len;
1cd05a96 2128#else
d9fbb118 2129 for (i = 0, opt = ifo->dhcp6_override;
10cac699 2130 i < ifo->dhcp6_override_len;
d9fbb118
RM
2131 i++, opt++)
2132 free_dhcp_opt_embenc(opt);
1cd05a96
RM
2133 free(ifo->dhcp6_override);
2134#endif
2135 ifo->dhcp6_override = NULL;
2136 ifo->dhcp6_override_len = 0;
7a911e57 2137
4eb7b489
RM
2138 ctx->vivso = ifo->vivso_override;
2139 ctx->vivso_len = ifo->vivso_override_len;
7a911e57
RM
2140 ifo->vivso_override = NULL;
2141 ifo->vivso_override_len = 0;
8e7d8c37
RM
2142 }
2143
fd05b7dc 2144 /* Parse our options file */
4eb7b489 2145 fp = fopen(ctx->cffile, "r");
c1b54b57 2146 if (fp == NULL) {
4eb7b489
RM
2147 if (strcmp(ctx->cffile, CONFIG))
2148 syslog(LOG_ERR, "fopen `%s': %m", ctx->cffile);
c1b54b57 2149 free(buf);
fd05b7dc 2150 return ifo;
5e2062a4 2151 }
fd05b7dc 2152
4eb7b489 2153 ldop = edop = NULL;
c1b54b57 2154 while ((line = get_line(&buf, &buflen, fp))) {
e1caa8db 2155 option = strsep(&line, " \t");
ab371aa6
RM
2156 if (line)
2157 line = strskipwhite(line);
fd05b7dc
RM
2158 /* Trim trailing whitespace */
2159 if (line && *line) {
2160 p = line + strlen(line) - 1;
2161 while (p != line &&
eab2229c
RM
2162 (*p == ' ' || *p == '\t') &&
2163 *(p - 1) != '\\')
fd05b7dc
RM
2164 *p-- = '\0';
2165 }
2166 /* Start of an interface block, skip if not ours */
2167 if (strcmp(option, "interface") == 0) {
4d91c2e7
RM
2168 char **n;
2169
fd05b7dc
RM
2170 if (ifname && line && strcmp(line, ifname) == 0)
2171 skip = 0;
2172 else
2173 skip = 1;
4d91c2e7
RM
2174 if (ifname)
2175 continue;
2176
2177 n = realloc(ctx->ifcv,
2178 sizeof(char *) * ((size_t)ctx->ifcc + 1));
2179 if (n == NULL) {
2180 syslog(LOG_ERR, "%s: %m", __func__);
2181 continue;
2182 }
2183 ctx->ifcv = n;
2184 ctx->ifcv[ctx->ifcc] = strdup(line);
2185 if (ctx->ifcv[ctx->ifcc] == NULL) {
2186 syslog(LOG_ERR, "%s: %m", __func__);
2187 continue;
2188 }
2189 ctx->ifcc++;
2190 syslog(LOG_DEBUG, "allowing interface %s",
2191 ctx->ifcv[ctx->ifcc - 1]);
fd05b7dc 2192 continue;
c53cf4ef
RM
2193 }
2194 /* Start of an ssid block, skip if not ours */
2195 if (strcmp(option, "ssid") == 0) {
2196 if (ssid && line && strcmp(line, ssid) == 0)
2197 skip = 0;
2198 else
2199 skip = 1;
2200 continue;
fd05b7dc 2201 }
6f767217
RM
2202 /* Start of a profile block, skip if not ours */
2203 if (strcmp(option, "profile") == 0) {
2204 if (profile && line && strcmp(line, profile) == 0) {
2205 skip = 0;
2206 have_profile = 1;
2207 } else
2208 skip = 1;
2209 continue;
2210 }
ba7a34fb
RM
2211 /* Skip arping if we have selected a profile but not parsing
2212 * one. */
2213 if (profile && !have_profile && strcmp(option, "arping") == 0)
2214 continue;
fd05b7dc
RM
2215 if (skip)
2216 continue;
4eb7b489 2217 parse_config_line(ctx, ifname, ifo, option, line, &ldop, &edop);
fd05b7dc 2218 }
c1b54b57
RM
2219 fclose(fp);
2220 free(buf);
fd05b7dc 2221
6f767217
RM
2222 if (profile && !have_profile) {
2223 free_options(ifo);
2224 errno = ENOENT;
658bfd5e 2225 return NULL;
6f767217
RM
2226 }
2227
b0272a9d 2228 finish_config(ifo);
fd05b7dc
RM
2229 return ifo;
2230}
2231
2232int
4eb7b489
RM
2233add_options(struct dhcpcd_ctx *ctx, const char *ifname,
2234 struct if_options *ifo, int argc, char **argv)
fd05b7dc 2235{
29c0fd6f
RM
2236 int oi, opt, r;
2237
2238 if (argc == 0)
2239 return 1;
fd05b7dc
RM
2240
2241 optind = 0;
29c0fd6f 2242 r = 1;
fd05b7dc
RM
2243 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
2244 {
4eb7b489 2245 r = parse_option(ctx, ifname, ifo, opt, optarg, NULL, NULL);
fd05b7dc
RM
2246 if (r != 1)
2247 break;
2248 }
741f46c6 2249
b0272a9d 2250 finish_config(ifo);
fd05b7dc
RM
2251 return r;
2252}
2253
2254void
2255free_options(struct if_options *ifo)
2256{
2257 size_t i;
d9fbb118 2258 struct dhcp_opt *opt;
900b6f94 2259 struct vivco *vo;
c73ed171 2260 struct token *token;
fd05b7dc 2261
f43e5853
RM
2262 if (ifo) {
2263 if (ifo->environ) {
2264 i = 0;
2265 while (ifo->environ[i])
2266 free(ifo->environ[i++]);
2267 free(ifo->environ);
2268 }
91a44b91
RM
2269 if (ifo->config) {
2270 i = 0;
2271 while (ifo->config[i])
2272 free(ifo->config[i++]);
2273 free(ifo->config);
2274 }
e88c525f 2275 ipv4_freeroutes(ifo->routes);
1681b126 2276 free(ifo->script);
ff021b0b 2277 free(ifo->arping);
f43e5853 2278 free(ifo->blacklist);
ff021b0b 2279 free(ifo->fallback);
8e7d8c37 2280
59a555cd
RM
2281 for (opt = ifo->dhcp_override;
2282 ifo->dhcp_override_len > 0;
2283 opt++, ifo->dhcp_override_len--)
d9fbb118 2284 free_dhcp_opt_embenc(opt);
8e7d8c37 2285 free(ifo->dhcp_override);
59a555cd
RM
2286 for (opt = ifo->dhcp6_override;
2287 ifo->dhcp6_override_len > 0;
2288 opt++, ifo->dhcp6_override_len--)
7a911e57
RM
2289 free_dhcp_opt_embenc(opt);
2290 free(ifo->dhcp6_override);
59a555cd
RM
2291 for (vo = ifo->vivco;
2292 ifo->vivco_len > 0;
2293 vo++, ifo->vivco_len--)
900b6f94
RM
2294 free(vo->data);
2295 free(ifo->vivco);
59a555cd
RM
2296 for (opt = ifo->vivso_override;
2297 ifo->vivso_override_len > 0;
2298 opt++, ifo->vivso_override_len--)
7a911e57 2299 free_dhcp_opt_embenc(opt);
900b6f94 2300 free(ifo->vivso_override);
8e7d8c37 2301
00ababe4 2302#ifdef INET6
59a555cd
RM
2303 for (; ifo->ia_len > 0; ifo->ia_len--)
2304 free(ifo->ia[ifo->ia_len - 1].sla);
00ababe4 2305#endif
ebc9d360
RM
2306 free(ifo->ia);
2307
c73ed171
RM
2308 while ((token = TAILQ_FIRST(&ifo->auth.tokens))) {
2309 TAILQ_REMOVE(&ifo->auth.tokens, token, next);
2310 if (token->realm_len)
2311 free(token->realm);
2312 free(token->key);
2313 free(token);
2314 }
f43e5853 2315 free(ifo);
fd05b7dc 2316 }
fd05b7dc 2317}