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