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