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