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