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