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