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