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