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