1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/types.h>
32 #include <arpa/inet.h>
51 #include "dhcpcd-embedded.h"
54 #include "if-options.h"
59 #define IN_CONFIG_BLOCK(ifo) ((ifo)->options & DHCPCD_FORKED)
60 #define SET_CONFIG_BLOCK(ifo) ((ifo)->options |= DHCPCD_FORKED)
61 #define CLEAR_CONFIG_BLOCK(ifo) ((ifo)->options &= ~DHCPCD_FORKED)
63 static unsigned long long default_options;
65 const struct option cf_options[] = {
66 {"background", no_argument, NULL, 'b'},
67 {"script", required_argument, NULL, 'c'},
68 {"debug", no_argument, NULL, 'd'},
69 {"env", required_argument, NULL, 'e'},
70 {"config", required_argument, NULL, 'f'},
71 {"reconfigure", no_argument, NULL, 'g'},
72 {"hostname", optional_argument, NULL, 'h'},
73 {"vendorclassid", optional_argument, NULL, 'i'},
74 {"logfile", required_argument, NULL, 'j'},
75 {"release", no_argument, NULL, 'k'},
76 {"leasetime", required_argument, NULL, 'l'},
77 {"metric", required_argument, NULL, 'm'},
78 {"rebind", no_argument, NULL, 'n'},
79 {"option", required_argument, NULL, 'o'},
80 {"persistent", no_argument, NULL, 'p'},
81 {"quiet", no_argument, NULL, 'q'},
82 {"request", optional_argument, NULL, 'r'},
83 {"inform", optional_argument, NULL, 's'},
84 {"inform6", optional_argument, NULL, O_INFORM6},
85 {"timeout", required_argument, NULL, 't'},
86 {"userclass", required_argument, NULL, 'u'},
88 {"msuserclass", required_argument, NULL, O_MSUSERCLASS},
90 {"vendor", required_argument, NULL, 'v'},
91 {"waitip", optional_argument, NULL, 'w'},
92 {"exit", no_argument, NULL, 'x'},
93 {"allowinterfaces", required_argument, NULL, 'z'},
94 {"reboot", required_argument, NULL, 'y'},
95 {"noarp", no_argument, NULL, 'A'},
96 {"nobackground", no_argument, NULL, 'B'},
97 {"nohook", required_argument, NULL, 'C'},
98 {"duid", optional_argument, NULL, 'D'},
99 {"lastlease", no_argument, NULL, 'E'},
100 {"fqdn", optional_argument, NULL, 'F'},
101 {"nogateway", no_argument, NULL, 'G'},
102 {"xidhwaddr", no_argument, NULL, 'H'},
103 {"clientid", optional_argument, NULL, 'I'},
104 {"broadcast", no_argument, NULL, 'J'},
105 {"nolink", no_argument, NULL, 'K'},
106 {"noipv4ll", no_argument, NULL, 'L'},
107 {"master", no_argument, NULL, 'M'},
108 {"renew", no_argument, NULL, 'N'},
109 {"nooption", required_argument, NULL, 'O'},
110 {"printpidfile", no_argument, NULL, 'P'},
111 {"require", required_argument, NULL, 'Q'},
112 {"static", required_argument, NULL, 'S'},
113 {"test", no_argument, NULL, 'T'},
114 {"dumplease", no_argument, NULL, 'U'},
115 {"variables", no_argument, NULL, 'V'},
116 {"whitelist", required_argument, NULL, 'W'},
117 {"blacklist", required_argument, NULL, 'X'},
118 {"denyinterfaces", required_argument, NULL, 'Z'},
119 {"oneshot", no_argument, NULL, '1'},
120 {"ipv4only", no_argument, NULL, '4'},
121 {"ipv6only", no_argument, NULL, '6'},
122 {"anonymous", no_argument, NULL, O_ANONYMOUS},
123 {"arping", required_argument, NULL, O_ARPING},
124 {"destination", required_argument, NULL, O_DESTINATION},
125 {"fallback", required_argument, NULL, O_FALLBACK},
126 {"ipv6rs", no_argument, NULL, O_IPV6RS},
127 {"noipv6rs", no_argument, NULL, O_NOIPV6RS},
128 {"ipv6ra_autoconf", no_argument, NULL, O_IPV6RA_AUTOCONF},
129 {"ipv6ra_noautoconf", no_argument, NULL, O_IPV6RA_NOAUTOCONF},
130 {"ipv6ra_fork", no_argument, NULL, O_IPV6RA_FORK},
131 {"ipv4", no_argument, NULL, O_IPV4},
132 {"noipv4", no_argument, NULL, O_NOIPV4},
133 {"ipv6", no_argument, NULL, O_IPV6},
134 {"noipv6", no_argument, NULL, O_NOIPV6},
135 {"noalias", no_argument, NULL, O_NOALIAS},
136 {"iaid", required_argument, NULL, O_IAID},
137 {"ia_na", optional_argument, NULL, O_IA_NA},
138 {"ia_ta", optional_argument, NULL, O_IA_TA},
139 {"ia_pd", optional_argument, NULL, O_IA_PD},
140 {"hostname_short", no_argument, NULL, O_HOSTNAME_SHORT},
141 {"dev", required_argument, NULL, O_DEV},
142 {"nodev", no_argument, NULL, O_NODEV},
143 {"define", required_argument, NULL, O_DEFINE},
144 {"definend", required_argument, NULL, O_DEFINEND},
145 {"define6", required_argument, NULL, O_DEFINE6},
146 {"embed", required_argument, NULL, O_EMBED},
147 {"encap", required_argument, NULL, O_ENCAP},
148 {"vendopt", required_argument, NULL, O_VENDOPT},
149 {"vendclass", required_argument, NULL, O_VENDCLASS},
150 {"authprotocol", required_argument, NULL, O_AUTHPROTOCOL},
151 {"authtoken", required_argument, NULL, O_AUTHTOKEN},
152 {"noauthrequired", no_argument, NULL, O_AUTHNOTREQUIRED},
153 {"dhcp", no_argument, NULL, O_DHCP},
154 {"nodhcp", no_argument, NULL, O_NODHCP},
155 {"dhcp6", no_argument, NULL, O_DHCP6},
156 {"nodhcp6", no_argument, NULL, O_NODHCP6},
157 {"controlgroup", required_argument, NULL, O_CONTROLGRP},
158 {"slaac", required_argument, NULL, O_SLAAC},
159 {"gateway", no_argument, NULL, O_GATEWAY},
160 {"reject", required_argument, NULL, O_REJECT},
161 {"bootp", no_argument, NULL, O_BOOTP},
162 {"nodelay", no_argument, NULL, O_NODELAY},
163 {"noup", no_argument, NULL, O_NOUP},
164 {"lastleaseextend", no_argument, NULL, O_LASTLEASE_EXTEND},
165 {"inactive", no_argument, NULL, O_INACTIVE},
166 {"mudurl", required_argument, NULL, O_MUDURL},
167 {"link_rcvbuf", required_argument, NULL, O_LINK_RCVBUF},
168 {"configure", no_argument, NULL, O_CONFIGURE},
169 {"noconfigure", no_argument, NULL, O_NOCONFIGURE},
170 {NULL, 0, NULL, '\0'}
174 add_environ(char ***array, const char *value, int uniq)
176 char **newlist, **list = *array;
178 char *match = NULL, *p, *n;
180 match = strdup(value);
185 p = strchr(match, '=');
187 logerrx("%s: no assignment: %s", __func__, value);
194 while (list && list[i]) {
195 if (match && strncmp(list[i], match, l) == 0) {
206 /* Append a space and the value to it */
209 n = realloc(list[i], l + lv + 2);
217 memcpy(list[i] + l + 1, p, lv);
218 list[i][l + lv + 1] = '\0';
232 newlist = reallocarray(list, i + 2, sizeof(char *));
233 if (newlist == NULL) {
239 newlist[i + 1] = NULL;
244 #define PARSE_STRING 0
245 #define PARSE_STRING_NULL 1
246 #define PARSE_HWADDR 2
247 #define parse_string(a, b, c) parse_str((a), (b), (c), PARSE_STRING)
248 #define parse_nstring(a, b, c) parse_str((a), (b), (c), PARSE_STRING_NULL)
249 #define parse_hwaddr(a, b, c) parse_str((a), (b), (c), PARSE_HWADDR)
251 parse_str(char *sbuf, size_t slen, const char *str, int flags)
258 end = str + strlen(str);
259 /* If surrounded by quotes then it's a string */
267 l = (size_t)hwaddr_aton(NULL, str);
268 if ((ssize_t) l != -1 && l > 1) {
273 hwaddr_aton((uint8_t *)sbuf, str);
278 /* Process escapes */
280 /* If processing a string on the clientid, first byte should be
281 * 0 to indicate a non hardware type */
282 if (flags == PARSE_HWADDR && *str) {
289 if (++l > slen && sbuf) {
295 switch((cmd = *str++)) {
316 /* Grab a hex code */
318 for (i = 0; i < 2; i++) {
319 if (isxdigit((unsigned char)*str) == 0)
323 if (c[1] != '\0' && sbuf) {
325 *sbuf++ = (char)strtol(c, NULL, 16);
330 /* Grab an octal code */
332 for (i = 0; i < 3; i++) {
333 if (*str < '0' || *str > '7')
337 if (c[2] != '\0' && sbuf) {
338 i = (int)strtol(c, NULL, 8);
356 if (flags == PARSE_STRING_NULL) {
370 parse_iaid1(uint8_t *iaid, const char *arg, size_t len, int n)
376 narg = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e);
380 memcpy(iaid, &narg, sizeof(narg));
384 if ((s = parse_string((char *)iaid, len, arg)) < 1)
396 parse_iaid(uint8_t *iaid, const char *arg, size_t len)
399 return parse_iaid1(iaid, arg, len, 1);
404 parse_uint32(uint32_t *i, const char *arg)
407 return parse_iaid1((uint8_t *)i, arg, sizeof(uint32_t), 0);
412 splitv(int *argc, char **argv, const char *arg)
414 char **n, **v = argv;
415 char *o = strdup(arg), *p, *t, *nt;
422 while ((t = strsep(&p, ", "))) {
429 n = reallocarray(v, (size_t)(*argc) + 1, sizeof(char *));
445 parse_addr(struct in_addr *addr, struct in_addr *net, const char *arg)
449 if (arg == NULL || *arg == '\0') {
456 if ((p = strchr(arg, '/')) != NULL) {
461 i = strtoi(p, NULL, 10, 0, 32, &e);
463 (net != NULL && inet_cidrtoaddr((int)i, net) != 0))
465 logerrx("invalid CIDR: %s", p);
470 if (addr != NULL && inet_aton(arg, addr) == 0) {
471 logerrx("invalid IP address: %s", arg);
476 else if (net != NULL && addr != NULL)
477 net->s_addr = ipv4_getnetmask(addr->s_addr);
482 parse_addr(__unused struct in_addr *addr, __unused struct in_addr *net,
483 __unused const char *arg)
486 logerrx("No IPv4 support");
492 set_option_space(struct dhcpcd_ctx *ctx,
494 const struct dhcp_opt **d, size_t *dl,
495 const struct dhcp_opt **od, size_t *odl,
496 struct if_options *ifo,
497 uint8_t *request[], uint8_t *require[], uint8_t *no[], uint8_t *reject[])
500 #if !defined(INET) && !defined(INET6)
505 if (strncmp(arg, "nd_", strlen("nd_")) == 0) {
507 *dl = ctx->nd_opts_len;
508 *od = ifo->nd_override;
509 *odl = ifo->nd_override_len;
510 *request = ifo->requestmasknd;
511 *require = ifo->requiremasknd;
513 *reject = ifo->rejectmasknd;
518 if (strncmp(arg, "dhcp6_", strlen("dhcp6_")) == 0) {
519 *d = ctx->dhcp6_opts;
520 *dl = ctx->dhcp6_opts_len;
521 *od = ifo->dhcp6_override;
522 *odl = ifo->dhcp6_override_len;
523 *request = ifo->requestmask6;
524 *require = ifo->requiremask6;
526 *reject = ifo->rejectmask6;
536 *dl = ctx->dhcp_opts_len;
537 *od = ifo->dhcp_override;
538 *odl = ifo->dhcp_override_len;
545 *request = ifo->requestmask;
546 *require = ifo->requiremask;
548 *reject = ifo->rejectmask;
552 free_dhcp_opt_embenc(struct dhcp_opt *opt)
559 for (i = 0, o = opt->embopts; i < opt->embopts_len; i++, o++)
560 free_dhcp_opt_embenc(o);
562 opt->embopts_len = 0;
565 for (i = 0, o = opt->encopts; i < opt->encopts_len; i++, o++)
566 free_dhcp_opt_embenc(o);
568 opt->encopts_len = 0;
573 strwhite(const char *s)
578 while (*s != ' ' && *s != '\t') {
587 strskipwhite(const char *s)
590 if (s == NULL || *s == '\0')
592 while (*s == ' ' || *s == '\t') {
601 /* Find the end pointer of a string. */
603 strend(const char *s)
610 return strchr(s, ' ');
612 for (; *s != '"' ; s++) {
625 parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
626 int opt, const char *arg, struct dhcp_opt **ldop, struct dhcp_opt **edop)
631 char *p = NULL, *bp, *fp, *np;
633 struct in_addr addr, addr2;
636 const struct dhcp_opt *d, *od;
637 uint8_t *request, *require, *no, *reject;
638 struct dhcp_opt **dop, *ndop;
639 size_t *dop_len, dl, odl;
653 struct if_sla *sla, *slap;
663 /* Add a guard for static analysers.
664 * This should not be needed really because of the argument_required option
665 * in the options declaration above. */
666 #define ARG_REQUIRED if (arg == NULL) goto arg_required
669 case 'f': /* FALLTHROUGH */
670 case 'g': /* FALLTHROUGH */
671 case 'n': /* FALLTHROUGH */
672 case 'q': /* FALLTHROUGH */
673 case 'x': /* FALLTHROUGH */
674 case 'N': /* FALLTHROUGH */
675 case 'P': /* FALLTHROUGH */
676 case 'T': /* FALLTHROUGH */
677 case 'U': /* FALLTHROUGH */
678 case 'V': /* We need to handle non interface options */
681 ifo->options |= DHCPCD_BACKGROUND;
685 if (IN_CONFIG_BLOCK(ifo)) {
686 logerrx("%s: per interface scripts"
687 " are no longer supported",
691 if (ctx->script != dhcpcd_default_script)
693 s = parse_nstring(NULL, 0, arg);
699 if (s == -1 || (ctx->script = malloc(dl)) == NULL) {
704 s = parse_nstring(ctx->script, dl, arg);
706 ctx->script[0] == '\0' ||
707 strcmp(ctx->script, "/dev/null") == 0)
714 ifo->options |= DHCPCD_DEBUG;
718 add_environ(&ifo->environ, arg, 1);
722 ifo->options |= DHCPCD_HOSTNAME;
725 s = parse_nstring(ifo->hostname, sizeof(ifo->hostname), arg);
727 logerr("%s: hostname", __func__);
730 if (s != 0 && ifo->hostname[0] == '.') {
731 logerrx("hostname cannot begin with .");
734 if (ifo->hostname[0] == '\0')
735 ifo->options &= ~DHCPCD_HOSTNAME;
737 ifo->options |= DHCPCD_HOSTNAME;
741 s = parse_string((char *)ifo->vendorclassid + 1,
742 VENDORCLASSID_MAX_LEN, arg);
746 logerr("vendorclassid");
749 *ifo->vendorclassid = (uint8_t)s;
753 /* per interface logging is not supported
754 * don't want to overide the commandline */
755 if (!IN_CONFIG_BLOCK(ifo) && ctx->logfile == NULL) {
757 ctx->logfile = strdup(arg);
758 logopen(ctx->logfile);
762 ifo->options |= DHCPCD_RELEASE;
766 if (strcmp(arg, "-1") == 0) {
767 ifo->leasetime = DHCP_INFINITE_LIFETIME;
770 ifo->leasetime = (uint32_t)strtou(arg, NULL,
771 0, 0, UINT32_MAX, &e);
773 logerrx("failed to convert leasetime %s", arg);
779 ifo->metric = (int)strtoi(arg, NULL, 0, 0, INT32_MAX, &e);
781 logerrx("failed to convert metric %s", arg);
787 if (ctx->options & DHCPCD_PRINT_PIDFILE)
789 set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
790 &request, &require, &no, &reject);
791 if (make_option_mask(d, dl, od, odl, request, arg, 1) != 0 ||
792 make_option_mask(d, dl, od, odl, no, arg, -1) != 0 ||
793 make_option_mask(d, dl, od, odl, reject, arg, -1) != 0)
795 logerrx("unknown option: %s", arg);
801 if (ctx->options & DHCPCD_PRINT_PIDFILE)
803 set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
804 &request, &require, &no, &reject);
805 if (make_option_mask(d, dl, od, odl, reject, arg, 1) != 0 ||
806 make_option_mask(d, dl, od, odl, request, arg, -1) != 0 ||
807 make_option_mask(d, dl, od, odl, require, arg, -1) != 0)
809 logerrx("unknown option: %s", arg);
814 ifo->options |= DHCPCD_PERSISTENT;
817 if (parse_addr(&ifo->req_addr, NULL, arg) != 0)
819 ifo->options |= DHCPCD_REQUEST;
820 ifo->req_mask.s_addr = 0;
823 if (arg && *arg != '\0') {
824 /* Strip out a broadcast address */
825 p = strchr(arg, '/');
827 p = strchr(p + 1, '/');
831 i = parse_addr(&ifo->req_addr, &ifo->req_mask, arg);
833 /* Ensure the original string is preserved */
836 i = parse_addr(&ifo->req_brd, NULL, p);
841 ifo->req_addr.s_addr = 0;
842 ifo->req_mask.s_addr = 0;
844 ifo->options |= DHCPCD_INFORM | DHCPCD_PERSISTENT;
845 ifo->options &= ~DHCPCD_STATIC;
848 ifo->options |= DHCPCD_INFORM6;
852 ifo->timeout = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e);
854 logerrx("failed to convert timeout %s", arg);
859 dl = sizeof(ifo->userclass) - ifo->userclass[0] - 1;
860 s = parse_string((char *)ifo->userclass +
861 ifo->userclass[0] + 2, dl, arg);
867 ifo->userclass[ifo->userclass[0] + 1] = (uint8_t)s;
868 ifo->userclass[0] = (uint8_t)(ifo->userclass[0] + s +1);
873 /* Some Microsoft DHCP servers expect userclass to be an
874 * opaque blob. This is not RFC 3004 compliant. */
875 s = parse_string((char *)ifo->userclass + 1,
876 sizeof(ifo->userclass) - 1, arg);
878 logerr("msuserclass");
881 ifo->userclass[0] = (uint8_t)s;
886 p = strchr(arg, ',');
888 logerrx("invalid vendor format: %s", arg);
892 /* If vendor starts with , then it is not encapsulated */
895 s = parse_string((char *)ifo->vendor + 1,
896 VENDOR_MAX_LEN, arg);
901 ifo->vendor[0] = (uint8_t)s;
902 ifo->options |= DHCPCD_VENDORRAW;
906 /* Encapsulated vendor options */
907 if (ifo->options & DHCPCD_VENDORRAW) {
908 ifo->options &= ~DHCPCD_VENDORRAW;
912 /* Strip and preserve the comma */
914 i = (int)strtoi(arg, NULL, 0, 1, 254, &e);
917 logerrx("vendor option should be between"
918 " 1 and 254 inclusive");
923 s = VENDOR_MAX_LEN - ifo->vendor[0] - 2;
924 if (inet_aton(arg, &addr) == 1) {
929 memcpy(ifo->vendor + ifo->vendor[0] + 3,
930 &addr.s_addr, sizeof(addr.s_addr));
931 s = sizeof(addr.s_addr);
934 s = parse_string((char *)ifo->vendor +
935 ifo->vendor[0] + 3, (size_t)s, arg);
942 ifo->vendor[ifo->vendor[0] + 1] = (uint8_t)i;
943 ifo->vendor[ifo->vendor[0] + 2] = (uint8_t)s;
944 ifo->vendor[0] = (uint8_t)(ifo->vendor[0] + s + 2);
948 ifo->options |= DHCPCD_WAITIP;
949 if (arg != NULL && arg[0] != '\0') {
950 if (arg[0] == '4' || arg[1] == '4')
951 ifo->options |= DHCPCD_WAITIP4;
952 if (arg[0] == '6' || arg[1] == '6')
953 ifo->options |= DHCPCD_WAITIP6;
958 ifo->reboot = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e);
960 logerr("failed to convert reboot %s", arg);
966 if (!IN_CONFIG_BLOCK(ifo))
967 ctx->ifav = splitv(&ctx->ifac, ctx->ifav, arg);
970 ifo->options &= ~DHCPCD_ARP;
971 /* IPv4LL requires ARP */
972 ifo->options &= ~DHCPCD_IPV4LL;
975 ifo->options &= ~DHCPCD_DAEMONISE;
979 /* Commas to spaces for shell */
980 while ((p = strchr(arg, ',')))
982 dl = strlen("skip_hooks=") + strlen(arg) + 1;
983 p = malloc(sizeof(char) * dl);
988 snprintf(p, dl, "skip_hooks=%s", arg);
989 add_environ(&ifo->environ, p, 0);
993 ifo->options |= DHCPCD_CLIENTID | DHCPCD_DUID;
994 if (ifname != NULL) /* duid type only a global option */
997 ctx->duid_type = DUID_DEFAULT;
998 else if (strcmp(arg, "ll") == 0)
999 ctx->duid_type = DUID_LL;
1000 else if (strcmp(arg, "llt") == 0)
1001 ctx->duid_type = DUID_LLT;
1002 else if (strcmp(arg, "uuid") == 0)
1003 ctx->duid_type = DUID_UUID;
1005 dl = hwaddr_aton(NULL, arg);
1007 no = realloc(ctx->duid, dl);
1012 ctx->duid_len = hwaddr_aton(no, arg);
1018 ifo->options |= DHCPCD_LASTLEASE;
1022 ifo->fqdn = FQDN_BOTH;
1025 if (strcmp(arg, "none") == 0)
1026 ifo->fqdn = FQDN_NONE;
1027 else if (strcmp(arg, "ptr") == 0)
1028 ifo->fqdn = FQDN_PTR;
1029 else if (strcmp(arg, "both") == 0)
1030 ifo->fqdn = FQDN_BOTH;
1031 else if (strcmp(arg, "disable") == 0)
1032 ifo->fqdn = FQDN_DISABLE;
1034 logerrx("invalid FQDN value: %s", arg);
1039 ifo->options &= ~DHCPCD_GATEWAY;
1042 ifo->options |= DHCPCD_XID_HWADDR;
1045 /* Strings have a type of 0 */;
1046 ifo->clientid[1] = 0;
1048 s = parse_hwaddr((char *)ifo->clientid + 1,
1049 CLIENTID_MAX_LEN, arg);
1056 ifo->options |= DHCPCD_CLIENTID;
1057 ifo->clientid[0] = (uint8_t)s;
1058 ifo->options &= ~DHCPCD_DUID;
1061 ifo->options |= DHCPCD_BROADCAST;
1064 ifo->options &= ~DHCPCD_LINK;
1067 ifo->options &= ~DHCPCD_IPV4LL;
1070 ifo->options |= DHCPCD_MASTER;
1074 if (ctx->options & DHCPCD_PRINT_PIDFILE)
1076 set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
1077 &request, &require, &no, &reject);
1078 if (make_option_mask(d, dl, od, odl, request, arg, -1) != 0 ||
1079 make_option_mask(d, dl, od, odl, require, arg, -1) != 0 ||
1080 make_option_mask(d, dl, od, odl, no, arg, 1) != 0)
1082 logerrx("unknown option: %s", arg);
1088 if (ctx->options & DHCPCD_PRINT_PIDFILE)
1090 set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
1091 &request, &require, &no, &reject);
1092 if (make_option_mask(d, dl, od, odl, require, arg, 1) != 0 ||
1093 make_option_mask(d, dl, od, odl, request, arg, 1) != 0 ||
1094 make_option_mask(d, dl, od, odl, no, arg, -1) != 0 ||
1095 make_option_mask(d, dl, od, odl, reject, arg, -1) != 0)
1097 logerrx("unknown option: %s", arg);
1103 p = strchr(arg, '=');
1105 logerrx("static assignment required");
1109 if (strncmp(arg, "ip_address=", strlen("ip_address=")) == 0) {
1110 if (parse_addr(&ifo->req_addr,
1111 ifo->req_mask.s_addr == 0 ? &ifo->req_mask : NULL,
1115 ifo->options |= DHCPCD_STATIC;
1116 ifo->options &= ~DHCPCD_INFORM;
1117 } else if (strncmp(arg, "subnet_mask=",
1118 strlen("subnet_mask=")) == 0)
1120 if (parse_addr(&ifo->req_mask, NULL, p) != 0)
1122 } else if (strncmp(arg, "broadcast_address=",
1123 strlen("broadcast_address=")) == 0)
1125 if (parse_addr(&ifo->req_brd, NULL, p) != 0)
1127 } else if (strncmp(arg, "routes=", strlen("routes=")) == 0 ||
1128 strncmp(arg, "static_routes=",
1129 strlen("static_routes=")) == 0 ||
1130 strncmp(arg, "classless_static_routes=",
1131 strlen("classless_static_routes=")) == 0 ||
1132 strncmp(arg, "ms_classless_static_routes=",
1133 strlen("ms_classless_static_routes=")) == 0)
1135 struct in_addr addr3;
1137 fp = np = strwhite(p);
1139 logerrx("all routes need a gateway");
1143 np = strskipwhite(np);
1144 if (parse_addr(&addr, &addr2, p) == -1 ||
1145 parse_addr(&addr3, NULL, np) == -1)
1151 if ((rt = rt_new0(ctx)) == NULL)
1153 sa_in_init(&rt->rt_dest, &addr);
1154 sa_in_init(&rt->rt_netmask, &addr2);
1155 sa_in_init(&rt->rt_gateway, &addr3);
1156 if (rt_proto_add_ctx(&ifo->routes, rt, ctx))
1157 add_environ(&ifo->config, arg, 0);
1158 } else if (strncmp(arg, "routers=", strlen("routers=")) == 0) {
1159 if (parse_addr(&addr, NULL, p) == -1)
1161 if ((rt = rt_new0(ctx)) == NULL)
1163 addr2.s_addr = INADDR_ANY;
1164 sa_in_init(&rt->rt_dest, &addr2);
1165 sa_in_init(&rt->rt_netmask, &addr2);
1166 sa_in_init(&rt->rt_gateway, &addr);
1167 if (rt_proto_add_ctx(&ifo->routes, rt, ctx))
1168 add_environ(&ifo->config, arg, 0);
1169 } else if (strncmp(arg, "interface_mtu=",
1170 strlen("interface_mtu=")) == 0 ||
1171 strncmp(arg, "mtu=", strlen("mtu=")) == 0)
1173 ifo->mtu = (unsigned int)strtou(p, NULL, 0,
1174 MTU_MIN, MTU_MAX, &e);
1176 logerrx("invalid MTU %s", p);
1179 } else if (strncmp(arg, "ip6_address=", strlen("ip6_address=")) == 0) {
1180 np = strchr(p, '/');
1183 if ((i = inet_pton(AF_INET6, p, &ifo->req_addr6)) == 1) {
1185 ifo->req_prefix_len = (uint8_t)strtou(np,
1186 NULL, 0, 0, 128, &e);
1188 logerrx("%s: failed to "
1189 "convert prefix len",
1194 ifo->req_prefix_len = 128;
1199 logerrx("invalid AF_INET6: %s", p);
1200 memset(&ifo->req_addr6, 0,
1201 sizeof(ifo->req_addr6));
1205 add_environ(&ifo->config, arg, 1);
1208 if (parse_addr(&addr, &addr2, arg) != 0)
1210 if (strchr(arg, '/') == NULL)
1211 addr2.s_addr = INADDR_BROADCAST;
1212 naddr = reallocarray(ifo->whitelist,
1213 ifo->whitelist_len + 2, sizeof(in_addr_t));
1214 if (naddr == NULL) {
1218 ifo->whitelist = naddr;
1219 ifo->whitelist[ifo->whitelist_len++] = addr.s_addr;
1220 ifo->whitelist[ifo->whitelist_len++] = addr2.s_addr;
1223 if (parse_addr(&addr, &addr2, arg) != 0)
1225 if (strchr(arg, '/') == NULL)
1226 addr2.s_addr = INADDR_BROADCAST;
1227 naddr = reallocarray(ifo->blacklist,
1228 ifo->blacklist_len + 2, sizeof(in_addr_t));
1229 if (naddr == NULL) {
1233 ifo->blacklist = naddr;
1234 ifo->blacklist[ifo->blacklist_len++] = addr.s_addr;
1235 ifo->blacklist[ifo->blacklist_len++] = addr2.s_addr;
1239 if (!IN_CONFIG_BLOCK(ifo))
1240 ctx->ifdv = splitv(&ctx->ifdc, ctx->ifdv, arg);
1243 ifo->options |= DHCPCD_ONESHOT;
1247 ifo->options &= ~DHCPCD_IPV6;
1248 ifo->options |= DHCPCD_IPV4;
1251 logerrx("INET has been compiled out");
1256 ifo->options &= ~DHCPCD_IPV4;
1257 ifo->options |= DHCPCD_IPV6;
1260 logerrx("INET6 has been compiled out");
1264 ifo->options |= DHCPCD_IPV4;
1267 ifo->options &= ~DHCPCD_IPV4;
1270 ifo->options |= DHCPCD_IPV6;
1273 ifo->options &= ~DHCPCD_IPV6;
1276 ifo->options |= DHCPCD_ANONYMOUS;
1277 ifo->options &= ~DHCPCD_HOSTNAME;
1278 ifo->fqdn = FQDN_DISABLE;
1280 /* Block everything */
1281 memset(ifo->nomask, 0xff, sizeof(ifo->nomask));
1282 memset(ifo->nomask6, 0xff, sizeof(ifo->nomask6));
1284 /* Allow the bare minimum through */
1286 del_option_mask(ifo->nomask, DHO_SUBNETMASK);
1287 del_option_mask(ifo->nomask, DHO_CSR);
1288 del_option_mask(ifo->nomask, DHO_ROUTER);
1289 del_option_mask(ifo->nomask, DHO_DNSSERVER);
1290 del_option_mask(ifo->nomask, DHO_DNSDOMAIN);
1291 del_option_mask(ifo->nomask, DHO_BROADCAST);
1292 del_option_mask(ifo->nomask, DHO_STATICROUTE);
1293 del_option_mask(ifo->nomask, DHO_SERVERID);
1294 del_option_mask(ifo->nomask, DHO_RENEWALTIME);
1295 del_option_mask(ifo->nomask, DHO_REBINDTIME);
1296 del_option_mask(ifo->nomask, DHO_DNSSEARCH);
1300 del_option_mask(ifo->nomask6, D6_OPTION_DNS_SERVERS);
1301 del_option_mask(ifo->nomask6, D6_OPTION_DOMAIN_LIST);
1302 del_option_mask(ifo->nomask6, D6_OPTION_SOL_MAX_RT);
1303 del_option_mask(ifo->nomask6, D6_OPTION_INF_MAX_RT);
1309 while (arg != NULL) {
1313 if (parse_addr(&addr, NULL, arg) != 0)
1315 naddr = reallocarray(ifo->arping,
1316 (size_t)ifo->arping_len + 1, sizeof(in_addr_t));
1317 if (naddr == NULL) {
1321 ifo->arping = naddr;
1322 ifo->arping[ifo->arping_len++] = addr.s_addr;
1323 arg = strskipwhite(fp);
1328 if (ctx->options & DHCPCD_PRINT_PIDFILE)
1330 set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo,
1331 &request, &require, &no, &reject);
1332 if (make_option_mask(d, dl, od, odl,
1333 ifo->dstmask, arg, 2) != 0)
1335 if (errno == EINVAL)
1336 logerrx("option does not take"
1337 " an IPv4 address: %s", arg);
1339 logerrx("unknown option: %s", arg);
1345 free(ifo->fallback);
1346 ifo->fallback = strdup(arg);
1347 if (ifo->fallback == NULL) {
1355 if (ctx->options & DHCPCD_MASTER && !IN_CONFIG_BLOCK(ifo)) {
1356 logerrx("IAID must belong in an interface block");
1359 if (parse_iaid(ifo->iaid, arg, sizeof(ifo->iaid)) == -1) {
1360 logerrx("invalid IAID %s", arg);
1363 ifo->options |= DHCPCD_IAID;
1366 ifo->options |= DHCPCD_IPV6RS;
1369 ifo->options &= ~DHCPCD_IPV6RS;
1372 ifo->options &= ~DHCPCD_IPV6RA_REQRDNSS;
1374 case O_IPV6RA_AUTOCONF:
1375 ifo->options |= DHCPCD_IPV6RA_AUTOCONF;
1377 case O_IPV6RA_NOAUTOCONF:
1378 ifo->options &= ~DHCPCD_IPV6RA_AUTOCONF;
1381 ifo->options |= DHCPCD_NOALIAS;
1385 i = D6_OPTION_IA_NA;
1389 i = D6_OPTION_IA_TA;
1394 logwarnx("%s: IA_PD not compiled in", ifname);
1397 if (ctx->options & DHCPCD_MASTER &&
1398 !IN_CONFIG_BLOCK(ifo))
1400 logerrx("IA PD must belong in an "
1404 i = D6_OPTION_IA_PD;
1407 if (ctx->options & DHCPCD_MASTER &&
1408 !IN_CONFIG_BLOCK(ifo) && arg)
1410 logerrx("IA with IAID must belong in an "
1414 ifo->options |= DHCPCD_IA_FORCED;
1418 fp = strskipwhite(fp);
1421 p = strchr(arg, '/');
1424 if (parse_iaid(iaid, arg, sizeof(iaid)) == -1) {
1425 logerr("invalid IAID: %s", arg);
1430 for (sl = 0; sl < ifo->ia_len; sl++) {
1431 if ((arg == NULL && !ifo->ia[sl].iaid_set) ||
1432 (arg != NULL && ifo->ia[sl].iaid_set &&
1433 ifo->ia[sl].ia_type == (uint16_t)i &&
1434 ifo->ia[sl].iaid[0] == iaid[0] &&
1435 ifo->ia[sl].iaid[1] == iaid[1] &&
1436 ifo->ia[sl].iaid[2] == iaid[2] &&
1437 ifo->ia[sl].iaid[3] == iaid[3]))
1444 ia = reallocarray(ifo->ia,
1445 ifo->ia_len + 1, sizeof(*ifo->ia));
1451 ia = &ifo->ia[ifo->ia_len++];
1452 ia->ia_type = (uint16_t)i;
1454 ia->iaid[0] = iaid[0];
1455 ia->iaid[1] = iaid[1];
1456 ia->iaid[2] = iaid[2];
1457 ia->iaid[3] = iaid[3];
1461 if (!ia->iaid_set ||
1463 ia->ia_type == D6_OPTION_IA_TA)
1465 memset(&ia->addr, 0, sizeof(ia->addr));
1469 p = strchr(arg, '/');
1472 if (inet_pton(AF_INET6, arg, &ia->addr) != 1) {
1473 logerrx("invalid AF_INET6: %s", arg);
1474 memset(&ia->addr, 0, sizeof(ia->addr));
1476 if (p && ia->ia_type == D6_OPTION_IA_PD) {
1477 ia->prefix_len = (uint8_t)strtou(p,
1478 NULL, 0, 8, 120, &e);
1480 logerrx("%s: failed to convert"
1497 if (ia->ia_type != D6_OPTION_IA_PD)
1500 for (p = fp; p; p = fp) {
1504 fp = strskipwhite(fp);
1506 sla = reallocarray(ia->sla,
1507 ia->sla_len + 1, sizeof(*ia->sla));
1513 sla = &ia->sla[ia->sla_len++];
1514 np = strchr(p, '/');
1517 if (strlcpy(sla->ifname, p,
1518 sizeof(sla->ifname)) >= sizeof(sla->ifname))
1520 logerrx("%s: interface name too long", arg);
1523 sla->sla_set = false;
1524 sla->prefix_len = 0;
1528 np = strchr(p, '/');
1532 sla->sla = (uint32_t)strtou(p, NULL,
1533 0, 0, UINT32_MAX, &e);
1534 sla->sla_set = true;
1536 logerrx("%s: failed to convert "
1545 np = strchr(p, '/');
1549 sla->prefix_len = (uint8_t)strtou(p,
1550 NULL, 0, 0, 120, &e);
1552 logerrx("%s: failed to "
1553 "convert prefix len",
1561 np = strchr(p, '/');
1565 sla->suffix = (uint64_t)strtou(p, NULL,
1566 0, 0, UINT64_MAX, &e);
1568 logerrx("%s: failed to "
1576 for (sl = 0; sl < ia->sla_len - 1; sl++) {
1577 slap = &ia->sla[sl];
1578 if (slap->sla_set != sla->sla_set) {
1579 logerrx("%s: cannot mix automatic "
1584 if (ia->prefix_len &&
1585 (sla->prefix_len == ia->prefix_len ||
1586 slap->prefix_len == ia->prefix_len))
1588 logerrx("%s: cannot delegte the same"
1589 "prefix length more than once",
1593 if (!sla->sla_set &&
1594 strcmp(slap->ifname, sla->ifname) == 0)
1596 logwarnx("%s: cannot specify the "
1597 "same interface twice with "
1602 if (slap->sla_set && sla->sla_set &&
1603 slap->sla == sla->sla)
1605 logerrx("%s: cannot"
1606 " assign the same SLA %u"
1608 sla->ifname, sla->sla);
1612 if (sla->sla_set && sla->sla > ia->sla_max)
1613 ia->sla_max = sla->sla;
1621 case O_HOSTNAME_SHORT:
1622 ifo->options |= DHCPCD_HOSTNAME | DHCPCD_HOSTNAME_SHORT;
1628 free(ctx->dev_load);
1629 ctx->dev_load = strdup(arg);
1633 ifo->options &= ~DHCPCD_DEV;
1636 dop = &ifo->dhcp_override;
1637 dop_len = &ifo->dhcp_override_len;
1641 dop = &ifo->nd_override;
1642 dop_len = &ifo->nd_override_len;
1647 dop = &ifo->dhcp6_override;
1648 dop_len = &ifo->dhcp6_override_len;
1653 dop = &ifo->vivso_override;
1654 dop_len = &ifo->vivso_override_len;
1656 *edop = *ldop = NULL;
1661 dop = &(*edop)->embopts;
1662 dop_len = &(*edop)->embopts_len;
1664 dop = &(*ldop)->embopts;
1665 dop_len = &(*ldop)->embopts_len;
1667 logerrx("embed must be after a define "
1676 if (*ldop == NULL) {
1677 logerrx("encap must be after a define");
1680 dop = &(*ldop)->encopts;
1681 dop_len = &(*ldop)->encopts_len;
1684 /* Shared code for define, define6, embed and encap */
1687 if (opt == O_EMBED) /* Embedded options don't have codes */
1692 logerrx("invalid syntax: %s", arg);
1696 u = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e);
1698 logerrx("invalid code: %s", arg);
1701 arg = strskipwhite(fp);
1703 logerrx("invalid syntax");
1711 np = strchr(arg, ':');
1715 bp = NULL; /* No bitflag */
1716 l = (long)strtou(np, NULL, 0, 0, LONG_MAX, &e);
1718 logerrx("failed to convert length");
1723 bp = strchr(arg, '='); /* bitflag assignment */
1728 if (strcasecmp(arg, "request") == 0) {
1730 arg = strskipwhite(fp);
1733 logerrx("incomplete request type");
1737 } else if (strcasecmp(arg, "norequest") == 0) {
1739 arg = strskipwhite(fp);
1742 logerrx("incomplete request type");
1747 if (strcasecmp(arg, "optional") == 0) {
1749 arg = strskipwhite(fp);
1752 logerrx("incomplete optional type");
1757 if (strcasecmp(arg, "index") == 0) {
1759 arg = strskipwhite(fp);
1762 logerrx("incomplete index type");
1767 if (strcasecmp(arg, "array") == 0) {
1769 arg = strskipwhite(fp);
1772 logerrx("incomplete array type");
1777 if (strcasecmp(arg, "ipaddress") == 0)
1779 else if (strcasecmp(arg, "ip6address") == 0)
1781 else if (strcasecmp(arg, "string") == 0)
1783 else if (strcasecmp(arg, "byte") == 0)
1785 else if (strcasecmp(arg, "bitflags") == 0)
1787 else if (strcasecmp(arg, "uint8") == 0)
1789 else if (strcasecmp(arg, "int8") == 0)
1791 else if (strcasecmp(arg, "uint16") == 0)
1793 else if (strcasecmp(arg, "int16") == 0)
1795 else if (strcasecmp(arg, "uint32") == 0)
1797 else if (strcasecmp(arg, "int32") == 0)
1799 else if (strcasecmp(arg, "flag") == 0)
1801 else if (strcasecmp(arg, "raw") == 0)
1802 t |= OT_STRING | OT_RAW;
1803 else if (strcasecmp(arg, "ascii") == 0)
1804 t |= OT_STRING | OT_ASCII;
1805 else if (strcasecmp(arg, "domain") == 0)
1806 t |= OT_STRING | OT_DOMAIN | OT_RFC1035;
1807 else if (strcasecmp(arg, "dname") == 0)
1808 t |= OT_STRING | OT_DOMAIN;
1809 else if (strcasecmp(arg, "binhex") == 0)
1810 t |= OT_STRING | OT_BINHEX;
1811 else if (strcasecmp(arg, "embed") == 0)
1813 else if (strcasecmp(arg, "encap") == 0)
1815 else if (strcasecmp(arg, "rfc3361") ==0)
1816 t |= OT_STRING | OT_RFC3361;
1817 else if (strcasecmp(arg, "rfc3442") ==0)
1818 t |= OT_STRING | OT_RFC3442;
1819 else if (strcasecmp(arg, "option") == 0)
1822 logerrx("unknown type: %s", arg);
1825 if (l && !(t & (OT_STRING | OT_BINHEX))) {
1826 logwarnx("ignoring length for type: %s", arg);
1829 if (t & OT_ARRAY && t & (OT_STRING | OT_BINHEX) &&
1830 !(t & (OT_RFC1035 | OT_DOMAIN)))
1832 logwarnx("ignoring array for strings");
1835 if (t & OT_BITFLAG) {
1837 logwarnx("missing bitflag assignment");
1841 if (!(t & OT_OPTION)) {
1842 logerrx("type %s requires a variable name",
1848 arg = strskipwhite(fp);
1852 if (strcasecmp(arg, "reserved")) {
1863 if (opt != O_EMBED) {
1864 for (dl = 0, ndop = *dop; dl < *dop_len; dl++, ndop++)
1866 /* type 0 seems freshly malloced struct
1868 if (ndop->option == u || ndop->type == 0)
1876 ndop = reallocarray(*dop, *dop_len + 1, sizeof(**dop));
1883 ndop = &(*dop)[(*dop_len)++];
1884 ndop->embopts = NULL;
1885 ndop->embopts_len = 0;
1886 ndop->encopts = NULL;
1887 ndop->encopts_len = 0;
1889 free_dhcp_opt_embenc(ndop);
1890 ndop->option = (uint32_t)u; /* could have been 0 */
1892 ndop->len = (size_t)l;
1896 memcpy(ndop->bitflags, bp, dl);
1897 memset(ndop->bitflags + dl, 0,
1898 sizeof(ndop->bitflags) - dl);
1900 memset(ndop->bitflags, 0, sizeof(ndop->bitflags));
1901 /* Save the define for embed and encap options */
1919 u = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e);
1921 logerrx("invalid code: %s", arg);
1924 fp = strskipwhite(fp);
1926 s = parse_string(NULL, 0, fp);
1932 if (dl + (sizeof(uint16_t) * 2) > UINT16_MAX) {
1933 logerrx("vendor class is too big");
1941 parse_string(np, dl, fp);
1946 vivco = reallocarray(ifo->vivco,
1947 ifo->vivco_len + 1, sizeof(*ifo->vivco));
1948 if (vivco == NULL) {
1954 ifo->vivco_en = (uint32_t)u;
1955 vivco = &ifo->vivco[ifo->vivco_len++];
1957 vivco->data = (uint8_t *)np;
1959 case O_AUTHPROTOCOL:
1965 if (strcasecmp(arg, "token") == 0)
1966 ifo->auth.protocol = AUTH_PROTO_TOKEN;
1967 else if (strcasecmp(arg, "delayed") == 0)
1968 ifo->auth.protocol = AUTH_PROTO_DELAYED;
1969 else if (strcasecmp(arg, "delayedrealm") == 0)
1970 ifo->auth.protocol = AUTH_PROTO_DELAYEDREALM;
1972 logerrx("%s: unsupported protocol", arg);
1975 arg = strskipwhite(fp);
1978 ifo->auth.options |= DHCPCD_AUTH_SEND;
1979 if (ifo->auth.protocol == AUTH_PROTO_TOKEN)
1980 ifo->auth.protocol = AUTH_ALG_NONE;
1982 ifo->auth.algorithm = AUTH_ALG_HMAC_MD5;
1983 ifo->auth.rdm = AUTH_RDM_MONOTONIC;
1988 if (ifo->auth.protocol == AUTH_PROTO_TOKEN) {
1989 np = strchr(arg, '/');
1991 if (fp == NULL || np < fp)
1996 if (parse_uint32(&ifo->auth.token_snd_secretid,
1998 logerrx("%s: not a number", arg);
2000 ifo->auth.token_rcv_secretid =
2001 ifo->auth.token_snd_secretid;
2003 parse_uint32(&ifo->auth.token_rcv_secretid,
2005 logerrx("%s: not a number", arg);
2007 if (strcasecmp(arg, "hmacmd5") == 0 ||
2008 strcasecmp(arg, "hmac-md5") == 0)
2009 ifo->auth.algorithm = AUTH_ALG_HMAC_MD5;
2011 logerrx("%s: unsupported algorithm", arg);
2017 ifo->auth.options |= DHCPCD_AUTH_SEND;
2018 ifo->auth.rdm = AUTH_RDM_MONOTONIC;
2021 if (strcasecmp(arg, "monocounter") == 0) {
2022 ifo->auth.rdm = AUTH_RDM_MONOTONIC;
2023 ifo->auth.options |= DHCPCD_AUTH_RDM_COUNTER;
2024 } else if (strcasecmp(arg, "monotonic") ==0 ||
2025 strcasecmp(arg, "monotime") == 0)
2026 ifo->auth.rdm = AUTH_RDM_MONOTONIC;
2028 logerrx("%s: unsupported RDM", arg);
2031 ifo->auth.options |= DHCPCD_AUTH_SEND;
2034 logerrx("no authentication support");
2042 logerrx("authtoken requires a realm");
2046 token = calloc(1, sizeof(*token));
2047 if (token == NULL) {
2051 if (parse_uint32(&token->secretid, arg) == -1) {
2052 logerrx("%s: not a number", arg);
2058 logerrx("authtoken requies an a key");
2062 s = parse_string(NULL, 0, arg);
2064 logerr("realm_len");
2068 token->realm_len = (size_t)s;
2069 token->realm = malloc(token->realm_len);
2070 if (token->realm == NULL) {
2074 parse_string((char *)token->realm, token->realm_len,
2080 logerrx("authtoken requies an expiry date");
2086 np = strchr(arg, '"');
2090 if (strcmp(arg, "0") == 0 || strcasecmp(arg, "forever") == 0)
2095 memset(&tm, 0, sizeof(tm));
2096 if (strptime(arg, "%Y-%m-%d %H:%M", &tm) == NULL) {
2097 logerrx("%s: invalid date time", arg);
2100 if ((token->expire = mktime(&tm)) == (time_t)-1) {
2101 logerr("%s: mktime", __func__);
2106 s = parse_string(NULL, 0, arg);
2107 if (s == -1 || s == 0) {
2109 logerr("token_len");
2111 logerrx("authtoken needs a key");
2114 token->key_len = (size_t)s;
2115 token->key = malloc(token->key_len);
2116 if (token->key == NULL) {
2120 parse_string((char *)token->key, token->key_len, arg);
2121 TAILQ_INSERT_TAIL(&ifo->auth.tokens, token, next);
2128 logerrx("no authentication support");
2131 case O_AUTHNOTREQUIRED:
2132 ifo->auth.options &= ~DHCPCD_AUTH_REQUIRE;
2135 ifo->options |= DHCPCD_DHCP | DHCPCD_WANTDHCP | DHCPCD_IPV4;
2138 ifo->options &= ~DHCPCD_DHCP;
2141 ifo->options |= DHCPCD_DHCP6 | DHCPCD_IPV6;
2144 ifo->options &= ~DHCPCD_DHCP6;
2149 /* Control group is already set by this point.
2150 * We don't need to pledge getpw either with this. */
2151 if (IN_PRIVSEP(ctx))
2155 l = sysconf(_SC_GETGR_R_SIZE_MAX);
2165 while ((i = getgrnam_r(arg, &grpbuf, p, dl, &grp)) ==
2170 logerrx("control_group: out of buffer");
2175 np = realloc(p, dl);
2185 logerr("getgrnam_r");
2190 if (!ctx->control_group)
2191 logerrx("controlgroup: %s: not found", arg);
2195 ctx->control_group = grp->gr_gid;
2198 grp = getgrnam(arg);
2200 if (!ctx->control_group)
2201 logerrx("controlgroup: %s: not found", arg);
2204 ctx->control_group = grp->gr_gid;
2208 ifo->options |= DHCPCD_GATEWAY;
2211 ifo->options &= ~DHCPCD_IF_UP;
2218 np = strskipwhite(np);
2220 if (strcmp(arg, "private") == 0 ||
2221 strcmp(arg, "stableprivate") == 0 ||
2222 strcmp(arg, "stable") == 0)
2223 ifo->options |= DHCPCD_SLAACPRIVATE;
2225 ifo->options &= ~DHCPCD_SLAACPRIVATE;
2227 (strcmp(np, "temp") == 0 || strcmp(np, "temporary") == 0))
2228 ifo->options |= DHCPCD_SLAACTEMP;
2231 ifo->options |= DHCPCD_BOOTP;
2234 ifo->options &= ~DHCPCD_INITIAL_DELAY;
2236 case O_LASTLEASE_EXTEND:
2237 ifo->options |= DHCPCD_LASTLEASE | DHCPCD_LASTLEASE_EXTEND;
2240 ifo->options |= DHCPCD_INACTIVE;
2244 s = parse_string((char *)ifo->mudurl + 1, MUDURL_MAX_LEN, arg);
2249 *ifo->mudurl = (uint8_t)s;
2254 ctx->link_rcvbuf = (int)strtoi(arg, NULL, 0, 0, INT32_MAX, &e);
2256 logerrx("failed to convert link_rcvbuf %s", arg);
2262 ifo->options |= DHCPCD_CONFIGURE;
2265 ifo->options &= ~DHCPCD_CONFIGURE;
2275 logerrx("option %d requires an argument", opt);
2282 parse_config_line(struct dhcpcd_ctx *ctx, const char *ifname,
2283 struct if_options *ifo, const char *opt, char *line,
2284 struct dhcp_opt **ldop, struct dhcp_opt **edop)
2288 for (i = 0; i < sizeof(cf_options) / sizeof(cf_options[0]); i++) {
2289 if (!cf_options[i].name ||
2290 strcmp(cf_options[i].name, opt) != 0)
2293 if (cf_options[i].has_arg == required_argument && !line) {
2294 logerrx("option requires an argument -- %s", opt);
2298 return parse_option(ctx, ifname, ifo, cf_options[i].val, line,
2302 if (!(ctx->options & DHCPCD_PRINT_PIDFILE))
2303 logerrx("unknown option: %s", opt);
2308 finish_config(struct if_options *ifo)
2311 /* Terminate the encapsulated options */
2312 if (ifo->vendor[0] && !(ifo->options & DHCPCD_VENDORRAW)) {
2314 ifo->vendor[ifo->vendor[0]] = DHO_END;
2315 /* We are called twice.
2316 * This should be fixed, but in the meantime, this
2317 * guard should suffice */
2318 ifo->options |= DHCPCD_VENDORRAW;
2321 if (!(ifo->options & DHCPCD_ARP) ||
2322 ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))
2323 ifo->options &= ~DHCPCD_IPV4LL;
2325 if (!(ifo->options & DHCPCD_IPV4))
2326 ifo->options &= ~(DHCPCD_DHCP | DHCPCD_IPV4LL | DHCPCD_WAITIP4);
2328 if (!(ifo->options & DHCPCD_IPV6))
2330 ~(DHCPCD_IPV6RS | DHCPCD_DHCP6 | DHCPCD_WAITIP6);
2332 if (!(ifo->options & DHCPCD_IPV6RS))
2334 ~(DHCPCD_IPV6RA_AUTOCONF | DHCPCD_IPV6RA_REQRDNSS);
2338 default_config(struct dhcpcd_ctx *ctx)
2340 struct if_options *ifo;
2342 /* Seed our default options */
2343 if ((ifo = calloc(1, sizeof(*ifo))) == NULL) {
2347 ifo->options |= DHCPCD_IF_UP | DHCPCD_LINK | DHCPCD_INITIAL_DELAY;
2348 ifo->timeout = DEFAULT_TIMEOUT;
2349 ifo->reboot = DEFAULT_REBOOT;
2351 ifo->auth.options |= DHCPCD_AUTH_REQUIRE;
2352 rb_tree_init(&ifo->routes, &rt_compare_list_ops);
2354 TAILQ_INIT(&ifo->auth.tokens);
2357 /* Inherit some global defaults */
2358 if (ctx->options & DHCPCD_CONFIGURE)
2359 ifo->options |= DHCPCD_CONFIGURE;
2360 if (ctx->options & DHCPCD_PERSISTENT)
2361 ifo->options |= DHCPCD_PERSISTENT;
2362 if (ctx->options & DHCPCD_SLAACPRIVATE)
2363 ifo->options |= DHCPCD_SLAACPRIVATE;
2369 read_config(struct dhcpcd_ctx *ctx,
2370 const char *ifname, const char *ssid, const char *profile)
2372 struct if_options *ifo;
2373 char buf[UDPLEN_MAX], *bp; /* 64k max config file size */
2374 char *line, *option, *p;
2377 int skip, have_profile, new_block, had_block;
2378 #if !defined(INET) || !defined(INET6)
2380 struct dhcp_opt *opt;
2382 struct dhcp_opt *ldop, *edop;
2384 /* Seed our default options */
2385 if ((ifo = default_config(ctx)) == NULL)
2387 if (default_options == 0) {
2388 default_options |= DHCPCD_CONFIGURE | DHCPCD_DAEMONISE |
2391 skip = socket(PF_INET, SOCK_DGRAM, 0);
2394 default_options |= DHCPCD_IPV4 | DHCPCD_ARP |
2395 DHCPCD_DHCP | DHCPCD_IPV4LL;
2399 skip = socket(PF_INET6, SOCK_DGRAM, 0);
2402 default_options |= DHCPCD_IPV6 | DHCPCD_IPV6RS |
2403 DHCPCD_IPV6RA_AUTOCONF | DHCPCD_IPV6RA_REQRDNSS |
2408 default_options |= DHCPCD_DEV;
2411 ifo->options |= default_options;
2413 CLEAR_CONFIG_BLOCK(ifo);
2415 vlen = strlcpy((char *)ifo->vendorclassid + 1, ctx->vendor,
2416 sizeof(ifo->vendorclassid) - 1);
2417 ifo->vendorclassid[0] = (uint8_t)(vlen > 255 ? 0 : vlen);
2419 /* Reset route order */
2422 /* Parse our embedded options file */
2423 if (ifname == NULL && !(ctx->options & DHCPCD_PRINT_PIDFILE)) {
2424 /* Space for initial estimates */
2425 #if defined(INET) && defined(INITDEFINES)
2426 ifo->dhcp_override =
2427 calloc(INITDEFINES, sizeof(*ifo->dhcp_override));
2428 if (ifo->dhcp_override == NULL)
2431 ifo->dhcp_override_len = INITDEFINES;
2434 #if defined(INET6) && defined(INITDEFINENDS)
2436 calloc(INITDEFINENDS, sizeof(*ifo->nd_override));
2437 if (ifo->nd_override == NULL)
2440 ifo->nd_override_len = INITDEFINENDS;
2442 #if defined(INET6) && defined(INITDEFINE6S)
2443 ifo->dhcp6_override =
2444 calloc(INITDEFINE6S, sizeof(*ifo->dhcp6_override));
2445 if (ifo->dhcp6_override == NULL)
2448 ifo->dhcp6_override_len = INITDEFINE6S;
2451 /* Now load our embedded config */
2452 #ifdef EMBEDDED_CONFIG
2453 buflen = dhcp_readfile(ctx, EMBEDDED_CONFIG, buf, sizeof(buf));
2455 logerr("%s: %s", __func__, EMBEDDED_CONFIG);
2458 if (buf[buflen - 1] != '\0') {
2459 if ((size_t)buflen < sizeof(buf) - 1)
2461 buf[buflen - 1] = '\0';
2464 buflen = (ssize_t)strlcpy(buf, dhcpcd_embedded_conf,
2466 if ((size_t)buflen >= sizeof(buf)) {
2467 logerrx("%s: embedded config too big", __func__);
2470 /* Our embedded config is NULL terminated */
2473 while ((line = get_line(&bp, &buflen)) != NULL) {
2474 option = strsep(&line, " \t");
2476 line = strskipwhite(line);
2477 /* Trim trailing whitespace */
2479 p = line + strlen(line) - 1;
2481 (*p == ' ' || *p == '\t') &&
2485 parse_config_line(ctx, NULL, ifo, option, line,
2490 ctx->dhcp_opts = ifo->dhcp_override;
2491 ctx->dhcp_opts_len = ifo->dhcp_override_len;
2493 for (i = 0, opt = ifo->dhcp_override;
2494 i < ifo->dhcp_override_len;
2496 free_dhcp_opt_embenc(opt);
2497 free(ifo->dhcp_override);
2499 ifo->dhcp_override = NULL;
2500 ifo->dhcp_override_len = 0;
2503 ctx->nd_opts = ifo->nd_override;
2504 ctx->nd_opts_len = ifo->nd_override_len;
2506 ctx->dhcp6_opts = ifo->dhcp6_override;
2507 ctx->dhcp6_opts_len = ifo->dhcp6_override_len;
2510 for (i = 0, opt = ifo->nd_override;
2511 i < ifo->nd_override_len;
2513 free_dhcp_opt_embenc(opt);
2514 free(ifo->nd_override);
2515 for (i = 0, opt = ifo->dhcp6_override;
2516 i < ifo->dhcp6_override_len;
2518 free_dhcp_opt_embenc(opt);
2519 free(ifo->dhcp6_override);
2521 ifo->nd_override = NULL;
2522 ifo->nd_override_len = 0;
2523 ifo->dhcp6_override = NULL;
2524 ifo->dhcp6_override_len = 0;
2526 ctx->vivso = ifo->vivso_override;
2527 ctx->vivso_len = ifo->vivso_override_len;
2528 ifo->vivso_override = NULL;
2529 ifo->vivso_override_len = 0;
2532 /* Parse our options file */
2533 buflen = dhcp_readfile(ctx, ctx->cffile, buf, sizeof(buf));
2535 /* dhcpcd can continue without it, but no DNS options
2536 * would be requested ... */
2537 logerr("%s: %s", __func__, ctx->cffile);
2540 if (buf[buflen - 1] != '\0') {
2541 if ((size_t)buflen < sizeof(buf) - 1)
2543 buf[buflen - 1] = '\0';
2545 dhcp_filemtime(ctx, ctx->cffile, &ifo->mtime);
2548 skip = have_profile = new_block = 0;
2549 had_block = ifname == NULL ? 1 : 0;
2551 while ((line = get_line(&bp, &buflen)) != NULL) {
2552 option = strsep(&line, " \t");
2554 line = strskipwhite(line);
2555 /* Trim trailing whitespace */
2557 p = line + strlen(line) - 1;
2559 (*p == ' ' || *p == '\t') &&
2563 if (skip == 0 && new_block) {
2566 ifo->options &= ~DHCPCD_WAITOPTS;
2567 SET_CONFIG_BLOCK(ifo);
2570 /* Start of an interface block, skip if not ours */
2571 if (strcmp(option, "interface") == 0) {
2576 /* No interface given */
2580 if (ifname && strcmp(line, ifname) == 0)
2587 n = reallocarray(ctx->ifcv,
2588 (size_t)ctx->ifcc + 1, sizeof(char *));
2594 ctx->ifcv[ctx->ifcc] = strdup(line);
2595 if (ctx->ifcv[ctx->ifcc] == NULL) {
2602 /* Start of an ssid block, skip if not ours */
2603 if (strcmp(option, "ssid") == 0) {
2605 if (ssid && line && strcmp(line, ssid) == 0)
2611 /* Start of a profile block, skip if not ours */
2612 if (strcmp(option, "profile") == 0) {
2614 if (profile && line && strcmp(line, profile) == 0) {
2621 /* Skip arping if we have selected a profile but not parsing
2623 if (profile && !have_profile && strcmp(option, "arping") == 0)
2628 parse_config_line(ctx, ifname, ifo, option, line, &ldop, &edop);
2631 if (profile && !have_profile) {
2632 free_options(ctx, ifo);
2638 ifo->options &= ~DHCPCD_WAITOPTS;
2639 CLEAR_CONFIG_BLOCK(ifo);
2645 add_options(struct dhcpcd_ctx *ctx, const char *ifname,
2646 struct if_options *ifo, int argc, char **argv)
2649 unsigned long long wait_opts;
2656 /* Don't apply the command line wait options to each interface,
2657 * only use the dhcpcd.conf entry for that. */
2659 wait_opts = ifo->options & DHCPCD_WAITOPTS;
2660 while ((opt = getopt_long(argc, argv,
2661 ctx->options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS,
2662 cf_options, &oi)) != -1)
2664 r = parse_option(ctx, ifname, ifo, opt, optarg, NULL, NULL);
2668 if (ifname != NULL) {
2669 ifo->options &= ~DHCPCD_WAITOPTS;
2670 ifo->options |= wait_opts;
2678 free_options(struct dhcpcd_ctx *ctx, struct if_options *ifo)
2681 #ifdef RT_FREE_ROUTE_TABLE
2682 struct interface *ifp;
2685 struct dhcp_opt *opt;
2688 struct token *token;
2696 while (ifo->environ[i])
2697 free(ifo->environ[i++]);
2702 while (ifo->config[i])
2703 free(ifo->config[i++]);
2707 #ifdef RT_FREE_ROUTE_TABLE
2708 /* Stupidly, we don't know the interface when creating the options.
2709 * As such, make sure each route has one so they can goto the
2711 ifp = ctx->ifaces != NULL ? TAILQ_FIRST(ctx->ifaces) : NULL;
2713 RB_TREE_FOREACH(rt, &ifo->routes) {
2714 if (rt->rt_ifp == NULL)
2719 rt_headclear0(ctx, &ifo->routes, AF_UNSPEC);
2722 free(ifo->blacklist);
2723 free(ifo->fallback);
2725 for (opt = ifo->dhcp_override;
2726 ifo->dhcp_override_len > 0;
2727 opt++, ifo->dhcp_override_len--)
2728 free_dhcp_opt_embenc(opt);
2729 free(ifo->dhcp_override);
2730 for (opt = ifo->nd_override;
2731 ifo->nd_override_len > 0;
2732 opt++, ifo->nd_override_len--)
2733 free_dhcp_opt_embenc(opt);
2734 free(ifo->nd_override);
2735 for (opt = ifo->dhcp6_override;
2736 ifo->dhcp6_override_len > 0;
2737 opt++, ifo->dhcp6_override_len--)
2738 free_dhcp_opt_embenc(opt);
2739 free(ifo->dhcp6_override);
2740 for (vo = ifo->vivco;
2742 vo++, ifo->vivco_len--)
2745 for (opt = ifo->vivso_override;
2746 ifo->vivso_override_len > 0;
2747 opt++, ifo->vivso_override_len--)
2748 free_dhcp_opt_embenc(opt);
2749 free(ifo->vivso_override);
2751 #if defined(INET6) && !defined(SMALL)
2752 for (; ifo->ia_len > 0; ifo->ia_len--)
2753 free(ifo->ia[ifo->ia_len - 1].sla);
2758 while ((token = TAILQ_FIRST(&ifo->auth.tokens))) {
2759 TAILQ_REMOVE(&ifo->auth.tokens, token, next);
2760 if (token->realm_len)