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