]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a6cc569e | 2 | /*** |
810adae9 | 3 | Copyright © 2013 Intel Corporation. All rights reserved. |
a6cc569e TG |
4 | ***/ |
5 | ||
07630cea | 6 | #include <arpa/inet.h> |
a6cc569e | 7 | #include <errno.h> |
a6cc569e | 8 | #include <stdio.h> |
0d536673 | 9 | #include <stdio_ext.h> |
07630cea LP |
10 | #include <stdlib.h> |
11 | #include <string.h> | |
12 | ||
13 | #include "sd-dhcp-lease.h" | |
a6cc569e | 14 | |
b5efdb8a | 15 | #include "alloc-util.h" |
07630cea LP |
16 | #include "dhcp-lease-internal.h" |
17 | #include "dhcp-protocol.h" | |
18 | #include "dns-domain.h" | |
686d13b9 | 19 | #include "env-file.h" |
3ffd4af2 | 20 | #include "fd-util.h" |
fe8db0c5 | 21 | #include "fileio.h" |
b11d6a7b | 22 | #include "hexdecoct.h" |
958b66ea | 23 | #include "hostname-util.h" |
07630cea | 24 | #include "in-addr-util.h" |
0339cd77 | 25 | #include "network-internal.h" |
6bedfcbb | 26 | #include "parse-util.h" |
d054f0a4 | 27 | #include "stdio-util.h" |
b11d6a7b | 28 | #include "string-util.h" |
51517f9e | 29 | #include "strv.h" |
e4de7287 | 30 | #include "tmpfile-util.h" |
07630cea | 31 | #include "unaligned.h" |
a6cc569e TG |
32 | |
33 | int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) { | |
34 | assert_return(lease, -EINVAL); | |
35 | assert_return(addr, -EINVAL); | |
36 | ||
0339cd77 LP |
37 | if (lease->address == 0) |
38 | return -ENODATA; | |
39 | ||
a6cc569e | 40 | addr->s_addr = lease->address; |
0339cd77 LP |
41 | return 0; |
42 | } | |
a6cc569e | 43 | |
0339cd77 LP |
44 | int sd_dhcp_lease_get_broadcast(sd_dhcp_lease *lease, struct in_addr *addr) { |
45 | assert_return(lease, -EINVAL); | |
46 | assert_return(addr, -EINVAL); | |
47 | ||
48 | if (!lease->have_broadcast) | |
49 | return -ENODATA; | |
50 | ||
51 | addr->s_addr = lease->broadcast; | |
a6cc569e TG |
52 | return 0; |
53 | } | |
54 | ||
68ceb9df PF |
55 | int sd_dhcp_lease_get_lifetime(sd_dhcp_lease *lease, uint32_t *lifetime) { |
56 | assert_return(lease, -EINVAL); | |
1c6eb4e3 | 57 | assert_return(lifetime, -EINVAL); |
68ceb9df | 58 | |
0339cd77 LP |
59 | if (lease->lifetime <= 0) |
60 | return -ENODATA; | |
61 | ||
68ceb9df | 62 | *lifetime = lease->lifetime; |
0339cd77 LP |
63 | return 0; |
64 | } | |
68ceb9df | 65 | |
0339cd77 LP |
66 | int sd_dhcp_lease_get_t1(sd_dhcp_lease *lease, uint32_t *t1) { |
67 | assert_return(lease, -EINVAL); | |
68 | assert_return(t1, -EINVAL); | |
69 | ||
70 | if (lease->t1 <= 0) | |
71 | return -ENODATA; | |
72 | ||
73 | *t1 = lease->t1; | |
74 | return 0; | |
75 | } | |
76 | ||
77 | int sd_dhcp_lease_get_t2(sd_dhcp_lease *lease, uint32_t *t2) { | |
78 | assert_return(lease, -EINVAL); | |
79 | assert_return(t2, -EINVAL); | |
80 | ||
81 | if (lease->t2 <= 0) | |
82 | return -ENODATA; | |
83 | ||
84 | *t2 = lease->t2; | |
68ceb9df PF |
85 | return 0; |
86 | } | |
87 | ||
a6cc569e TG |
88 | int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu) { |
89 | assert_return(lease, -EINVAL); | |
90 | assert_return(mtu, -EINVAL); | |
91 | ||
0339cd77 LP |
92 | if (lease->mtu <= 0) |
93 | return -ENODATA; | |
a6cc569e | 94 | |
0339cd77 | 95 | *mtu = lease->mtu; |
a6cc569e TG |
96 | return 0; |
97 | } | |
98 | ||
a2ba62c7 | 99 | int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr) { |
a6cc569e TG |
100 | assert_return(lease, -EINVAL); |
101 | assert_return(addr, -EINVAL); | |
a6cc569e | 102 | |
0339cd77 LP |
103 | if (lease->dns_size <= 0) |
104 | return -ENODATA; | |
a6cc569e | 105 | |
0339cd77 LP |
106 | *addr = lease->dns; |
107 | return (int) lease->dns_size; | |
a6cc569e TG |
108 | } |
109 | ||
a2ba62c7 | 110 | int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr) { |
46844696 TG |
111 | assert_return(lease, -EINVAL); |
112 | assert_return(addr, -EINVAL); | |
46844696 | 113 | |
0339cd77 LP |
114 | if (lease->ntp_size <= 0) |
115 | return -ENODATA; | |
46844696 | 116 | |
0339cd77 LP |
117 | *addr = lease->ntp; |
118 | return (int) lease->ntp_size; | |
46844696 TG |
119 | } |
120 | ||
a6cc569e TG |
121 | int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname) { |
122 | assert_return(lease, -EINVAL); | |
123 | assert_return(domainname, -EINVAL); | |
124 | ||
0339cd77 LP |
125 | if (!lease->domainname) |
126 | return -ENODATA; | |
a6cc569e | 127 | |
0339cd77 | 128 | *domainname = lease->domainname; |
a6cc569e TG |
129 | return 0; |
130 | } | |
131 | ||
132 | int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname) { | |
133 | assert_return(lease, -EINVAL); | |
134 | assert_return(hostname, -EINVAL); | |
135 | ||
0339cd77 LP |
136 | if (!lease->hostname) |
137 | return -ENODATA; | |
a6cc569e | 138 | |
0339cd77 | 139 | *hostname = lease->hostname; |
a6cc569e TG |
140 | return 0; |
141 | } | |
142 | ||
ce78df79 TG |
143 | int sd_dhcp_lease_get_root_path(sd_dhcp_lease *lease, const char **root_path) { |
144 | assert_return(lease, -EINVAL); | |
145 | assert_return(root_path, -EINVAL); | |
146 | ||
0339cd77 LP |
147 | if (!lease->root_path) |
148 | return -ENODATA; | |
ce78df79 | 149 | |
0339cd77 | 150 | *root_path = lease->root_path; |
ce78df79 TG |
151 | return 0; |
152 | } | |
153 | ||
a6cc569e TG |
154 | int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, struct in_addr *addr) { |
155 | assert_return(lease, -EINVAL); | |
156 | assert_return(addr, -EINVAL); | |
157 | ||
0339cd77 LP |
158 | if (lease->router == 0) |
159 | return -ENODATA; | |
a6cc569e | 160 | |
0339cd77 | 161 | addr->s_addr = lease->router; |
a6cc569e TG |
162 | return 0; |
163 | } | |
164 | ||
165 | int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr) { | |
166 | assert_return(lease, -EINVAL); | |
167 | assert_return(addr, -EINVAL); | |
168 | ||
0339cd77 LP |
169 | if (!lease->have_subnet_mask) |
170 | return -ENODATA; | |
a6cc569e | 171 | |
0339cd77 | 172 | addr->s_addr = lease->subnet_mask; |
a6cc569e TG |
173 | return 0; |
174 | } | |
175 | ||
0ad853bc TG |
176 | int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr) { |
177 | assert_return(lease, -EINVAL); | |
178 | assert_return(addr, -EINVAL); | |
179 | ||
0339cd77 LP |
180 | if (lease->server_address == 0) |
181 | return -ENODATA; | |
0ad853bc | 182 | |
0339cd77 | 183 | addr->s_addr = lease->server_address; |
0ad853bc TG |
184 | return 0; |
185 | } | |
186 | ||
8e34a618 TG |
187 | int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr) { |
188 | assert_return(lease, -EINVAL); | |
189 | assert_return(addr, -EINVAL); | |
190 | ||
0339cd77 LP |
191 | if (lease->next_server == 0) |
192 | return -ENODATA; | |
8e34a618 | 193 | |
0339cd77 | 194 | addr->s_addr = lease->next_server; |
8e34a618 TG |
195 | return 0; |
196 | } | |
197 | ||
f8693fc7 BG |
198 | /* |
199 | * The returned routes array must be freed by the caller. | |
200 | * Route objects have the same lifetime of the lease and must not be freed. | |
201 | */ | |
202 | int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, sd_dhcp_route ***routes) { | |
203 | sd_dhcp_route **ret; | |
204 | unsigned i; | |
205 | ||
e1ea665e EY |
206 | assert_return(lease, -EINVAL); |
207 | assert_return(routes, -EINVAL); | |
e1ea665e | 208 | |
0339cd77 LP |
209 | if (lease->static_route_size <= 0) |
210 | return -ENODATA; | |
e1ea665e | 211 | |
f8693fc7 BG |
212 | ret = new(sd_dhcp_route *, lease->static_route_size); |
213 | if (!ret) | |
214 | return -ENOMEM; | |
215 | ||
216 | for (i = 0; i < lease->static_route_size; i++) | |
217 | ret[i] = &lease->static_route[i]; | |
218 | ||
219 | *routes = ret; | |
0339cd77 | 220 | return (int) lease->static_route_size; |
e1ea665e EY |
221 | } |
222 | ||
b85bc551 | 223 | int sd_dhcp_lease_get_search_domains(sd_dhcp_lease *lease, char ***domains) { |
da6053d0 | 224 | size_t r; |
b85bc551 DW |
225 | |
226 | assert_return(lease, -EINVAL); | |
227 | assert_return(domains, -EINVAL); | |
228 | ||
229 | r = strv_length(lease->search_domains); | |
230 | if (r > 0) { | |
231 | *domains = lease->search_domains; | |
232 | return (int) r; | |
233 | } | |
234 | ||
235 | return -ENODATA; | |
236 | } | |
237 | ||
0339cd77 | 238 | int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const void **data, size_t *data_len) { |
e43a8393 BG |
239 | assert_return(lease, -EINVAL); |
240 | assert_return(data, -EINVAL); | |
241 | assert_return(data_len, -EINVAL); | |
242 | ||
0339cd77 LP |
243 | if (lease->vendor_specific_len <= 0) |
244 | return -ENODATA; | |
e43a8393 BG |
245 | |
246 | *data = lease->vendor_specific; | |
247 | *data_len = lease->vendor_specific_len; | |
e43a8393 BG |
248 | return 0; |
249 | } | |
250 | ||
8301aa0b YW |
251 | static sd_dhcp_lease *dhcp_lease_free(sd_dhcp_lease *lease) { |
252 | assert(lease); | |
3733eec3 LP |
253 | |
254 | while (lease->private_options) { | |
255 | struct sd_dhcp_raw_option *option = lease->private_options; | |
256 | ||
257 | LIST_REMOVE(options, lease->private_options, option); | |
258 | ||
259 | free(option->data); | |
260 | free(option); | |
a6cc569e TG |
261 | } |
262 | ||
e2975f85 YW |
263 | free(lease->root_path); |
264 | free(lease->timezone); | |
3733eec3 LP |
265 | free(lease->hostname); |
266 | free(lease->domainname); | |
267 | free(lease->dns); | |
268 | free(lease->ntp); | |
269 | free(lease->static_route); | |
270 | free(lease->client_id); | |
271 | free(lease->vendor_specific); | |
b85bc551 | 272 | strv_free(lease->search_domains); |
6b430fdb | 273 | return mfree(lease); |
a6cc569e TG |
274 | } |
275 | ||
8301aa0b YW |
276 | DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_lease, sd_dhcp_lease, dhcp_lease_free); |
277 | ||
0339cd77 | 278 | static int lease_parse_u32(const uint8_t *option, size_t len, uint32_t *ret, uint32_t min) { |
e140ae58 TG |
279 | assert(option); |
280 | assert(ret); | |
281 | ||
0339cd77 LP |
282 | if (len != 4) |
283 | return -EINVAL; | |
e140ae58 | 284 | |
0339cd77 LP |
285 | *ret = unaligned_read_be32((be32_t*) option); |
286 | if (*ret < min) | |
287 | *ret = min; | |
e140ae58 | 288 | |
0339cd77 | 289 | return 0; |
e140ae58 TG |
290 | } |
291 | ||
0339cd77 | 292 | static int lease_parse_u16(const uint8_t *option, size_t len, uint16_t *ret, uint16_t min) { |
e140ae58 TG |
293 | assert(option); |
294 | assert(ret); | |
295 | ||
0339cd77 LP |
296 | if (len != 2) |
297 | return -EINVAL; | |
e140ae58 | 298 | |
0339cd77 LP |
299 | *ret = unaligned_read_be16((be16_t*) option); |
300 | if (*ret < min) | |
301 | *ret = min; | |
f5c0c00f | 302 | |
0339cd77 | 303 | return 0; |
f5c0c00f TG |
304 | } |
305 | ||
0339cd77 | 306 | static int lease_parse_be32(const uint8_t *option, size_t len, be32_t *ret) { |
f5c0c00f TG |
307 | assert(option); |
308 | assert(ret); | |
309 | ||
0339cd77 LP |
310 | if (len != 4) |
311 | return -EINVAL; | |
f5c0c00f | 312 | |
0339cd77 LP |
313 | memcpy(ret, option, 4); |
314 | return 0; | |
f5c0c00f TG |
315 | } |
316 | ||
e140ae58 TG |
317 | static int lease_parse_string(const uint8_t *option, size_t len, char **ret) { |
318 | assert(option); | |
319 | assert(ret); | |
320 | ||
0339cd77 LP |
321 | if (len <= 0) |
322 | *ret = mfree(*ret); | |
323 | else { | |
e140ae58 TG |
324 | char *string; |
325 | ||
e989fd9b LP |
326 | /* |
327 | * One trailing NUL byte is OK, we don't mind. See: | |
328 | * https://github.com/systemd/systemd/issues/1337 | |
329 | */ | |
330 | if (memchr(option, 0, len - 1)) | |
43f447b1 LP |
331 | return -EINVAL; |
332 | ||
e989fd9b | 333 | string = strndup((const char *) option, len); |
e140ae58 | 334 | if (!string) |
43f447b1 | 335 | return -ENOMEM; |
e140ae58 | 336 | |
09348d40 | 337 | free_and_replace(*ret, string); |
0339cd77 | 338 | } |
e140ae58 TG |
339 | |
340 | return 0; | |
341 | } | |
342 | ||
978c6477 LP |
343 | static int lease_parse_domain(const uint8_t *option, size_t len, char **ret) { |
344 | _cleanup_free_ char *name = NULL, *normalized = NULL; | |
345 | int r; | |
346 | ||
347 | assert(option); | |
348 | assert(ret); | |
349 | ||
350 | r = lease_parse_string(option, len, &name); | |
351 | if (r < 0) | |
352 | return r; | |
353 | if (!name) { | |
354 | *ret = mfree(*ret); | |
355 | return 0; | |
356 | } | |
357 | ||
7470cc4c | 358 | r = dns_name_normalize(name, 0, &normalized); |
978c6477 LP |
359 | if (r < 0) |
360 | return r; | |
361 | ||
362 | if (is_localhost(normalized)) | |
363 | return -EINVAL; | |
364 | ||
365 | if (dns_name_is_root(normalized)) | |
366 | return -EINVAL; | |
367 | ||
f9ecfd3b | 368 | free_and_replace(*ret, normalized); |
978c6477 LP |
369 | |
370 | return 0; | |
371 | } | |
372 | ||
d9ec2e63 LP |
373 | static void filter_bogus_addresses(struct in_addr *addresses, size_t *n) { |
374 | size_t i, j; | |
375 | ||
376 | /* Silently filter DNS/NTP servers supplied to us that do not make outside of the local scope. */ | |
377 | ||
378 | for (i = 0, j = 0; i < *n; i ++) { | |
379 | ||
380 | if (in4_addr_is_null(addresses+i) || | |
381 | in4_addr_is_localhost(addresses+i)) | |
382 | continue; | |
383 | ||
384 | addresses[j++] = addresses[i]; | |
385 | } | |
386 | ||
387 | *n = j; | |
388 | } | |
389 | ||
0339cd77 | 390 | static int lease_parse_in_addrs(const uint8_t *option, size_t len, struct in_addr **ret, size_t *n_ret) { |
e140ae58 TG |
391 | assert(option); |
392 | assert(ret); | |
0339cd77 | 393 | assert(n_ret); |
e140ae58 | 394 | |
0339cd77 LP |
395 | if (len <= 0) { |
396 | *ret = mfree(*ret); | |
397 | *n_ret = 0; | |
398 | } else { | |
399 | size_t n_addresses; | |
e140ae58 TG |
400 | struct in_addr *addresses; |
401 | ||
0339cd77 LP |
402 | if (len % 4 != 0) |
403 | return -EINVAL; | |
e140ae58 | 404 | |
0339cd77 LP |
405 | n_addresses = len / 4; |
406 | ||
407 | addresses = newdup(struct in_addr, option, n_addresses); | |
e140ae58 TG |
408 | if (!addresses) |
409 | return -ENOMEM; | |
410 | ||
d9ec2e63 LP |
411 | filter_bogus_addresses(addresses, &n_addresses); |
412 | ||
e140ae58 TG |
413 | free(*ret); |
414 | *ret = addresses; | |
0339cd77 | 415 | *n_ret = n_addresses; |
e140ae58 TG |
416 | } |
417 | ||
418 | return 0; | |
419 | } | |
420 | ||
0339cd77 LP |
421 | static int lease_parse_routes( |
422 | const uint8_t *option, size_t len, | |
423 | struct sd_dhcp_route **routes, size_t *routes_size, size_t *routes_allocated) { | |
e1ea665e EY |
424 | |
425 | struct in_addr addr; | |
426 | ||
0339cd77 | 427 | assert(option || len <= 0); |
e1ea665e EY |
428 | assert(routes); |
429 | assert(routes_size); | |
430 | assert(routes_allocated); | |
431 | ||
0339cd77 | 432 | if (len <= 0) |
e1ea665e EY |
433 | return 0; |
434 | ||
435 | if (len % 8 != 0) | |
436 | return -EINVAL; | |
437 | ||
438 | if (!GREEDY_REALLOC(*routes, *routes_allocated, *routes_size + (len / 8))) | |
439 | return -ENOMEM; | |
440 | ||
441 | while (len >= 8) { | |
442 | struct sd_dhcp_route *route = *routes + *routes_size; | |
1caa12d0 | 443 | int r; |
e1ea665e | 444 | |
8cdc46e7 | 445 | route->option = SD_DHCP_OPTION_STATIC_ROUTE; |
5a941f5f | 446 | r = in4_addr_default_prefixlen((struct in_addr*) option, &route->dst_prefixlen); |
1caa12d0 | 447 | if (r < 0) { |
0339cd77 | 448 | log_debug("Failed to determine destination prefix length from class based IP, ignoring"); |
e1ea665e EY |
449 | continue; |
450 | } | |
451 | ||
0339cd77 | 452 | assert_se(lease_parse_be32(option, 4, &addr.s_addr) >= 0); |
e1ea665e EY |
453 | route->dst_addr = inet_makeaddr(inet_netof(addr), 0); |
454 | option += 4; | |
455 | ||
0339cd77 | 456 | assert_se(lease_parse_be32(option, 4, &route->gw_addr.s_addr) >= 0); |
e1ea665e EY |
457 | option += 4; |
458 | ||
459 | len -= 8; | |
460 | (*routes_size)++; | |
461 | } | |
462 | ||
463 | return 0; | |
464 | } | |
465 | ||
466 | /* parses RFC3442 Classless Static Route Option */ | |
0339cd77 LP |
467 | static int lease_parse_classless_routes( |
468 | const uint8_t *option, size_t len, | |
469 | struct sd_dhcp_route **routes, size_t *routes_size, size_t *routes_allocated) { | |
e1ea665e | 470 | |
0339cd77 | 471 | assert(option || len <= 0); |
e1ea665e EY |
472 | assert(routes); |
473 | assert(routes_size); | |
474 | assert(routes_allocated); | |
475 | ||
0339cd77 LP |
476 | if (len <= 0) |
477 | return 0; | |
478 | ||
e1ea665e EY |
479 | /* option format: (subnet-mask-width significant-subnet-octets gateway-ip)* */ |
480 | ||
481 | while (len > 0) { | |
482 | uint8_t dst_octets; | |
483 | struct sd_dhcp_route *route; | |
484 | ||
485 | if (!GREEDY_REALLOC(*routes, *routes_allocated, *routes_size + 1)) | |
0339cd77 | 486 | return -ENOMEM; |
e1ea665e EY |
487 | |
488 | route = *routes + *routes_size; | |
8cdc46e7 | 489 | route->option = SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE; |
e1ea665e EY |
490 | |
491 | dst_octets = (*option == 0 ? 0 : ((*option - 1) / 8) + 1); | |
492 | route->dst_prefixlen = *option; | |
493 | option++; | |
494 | len--; | |
495 | ||
496 | /* can't have more than 4 octets in IPv4 */ | |
497 | if (dst_octets > 4 || len < dst_octets) | |
498 | return -EINVAL; | |
499 | ||
500 | route->dst_addr.s_addr = 0; | |
501 | memcpy(&route->dst_addr.s_addr, option, dst_octets); | |
502 | option += dst_octets; | |
503 | len -= dst_octets; | |
504 | ||
505 | if (len < 4) | |
506 | return -EINVAL; | |
507 | ||
b14fff6e | 508 | assert_se(lease_parse_be32(option, 4, &route->gw_addr.s_addr) >= 0); |
e1ea665e EY |
509 | option += 4; |
510 | len -= 4; | |
511 | ||
512 | (*routes_size)++; | |
513 | } | |
514 | ||
515 | return 0; | |
516 | } | |
517 | ||
e4735228 | 518 | int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void *userdata) { |
2d03c0b8 | 519 | sd_dhcp_lease *lease = userdata; |
e140ae58 TG |
520 | int r; |
521 | ||
522 | assert(lease); | |
a6cc569e TG |
523 | |
524 | switch(code) { | |
525 | ||
22805d92 | 526 | case SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME: |
0339cd77 LP |
527 | r = lease_parse_u32(option, len, &lease->lifetime, 1); |
528 | if (r < 0) | |
529 | log_debug_errno(r, "Failed to parse lease time, ignoring: %m"); | |
530 | ||
a6cc569e TG |
531 | break; |
532 | ||
22805d92 | 533 | case SD_DHCP_OPTION_SERVER_IDENTIFIER: |
0339cd77 LP |
534 | r = lease_parse_be32(option, len, &lease->server_address); |
535 | if (r < 0) | |
536 | log_debug_errno(r, "Failed to parse server identifier, ignoring: %m"); | |
537 | ||
a6cc569e TG |
538 | break; |
539 | ||
22805d92 | 540 | case SD_DHCP_OPTION_SUBNET_MASK: |
0339cd77 LP |
541 | r = lease_parse_be32(option, len, &lease->subnet_mask); |
542 | if (r < 0) | |
543 | log_debug_errno(r, "Failed to parse subnet mask, ignoring: %m"); | |
544 | else | |
545 | lease->have_subnet_mask = true; | |
a6cc569e TG |
546 | break; |
547 | ||
22805d92 | 548 | case SD_DHCP_OPTION_BROADCAST: |
0339cd77 LP |
549 | r = lease_parse_be32(option, len, &lease->broadcast); |
550 | if (r < 0) | |
551 | log_debug_errno(r, "Failed to parse broadcast address, ignoring: %m"); | |
552 | else | |
553 | lease->have_broadcast = true; | |
f5c0c00f TG |
554 | break; |
555 | ||
22805d92 | 556 | case SD_DHCP_OPTION_ROUTER: |
0339cd77 LP |
557 | if (len >= 4) { |
558 | r = lease_parse_be32(option, 4, &lease->router); | |
559 | if (r < 0) | |
560 | log_debug_errno(r, "Failed to parse router address, ignoring: %m"); | |
561 | } | |
a6cc569e TG |
562 | break; |
563 | ||
22805d92 | 564 | case SD_DHCP_OPTION_DOMAIN_NAME_SERVER: |
0339cd77 LP |
565 | r = lease_parse_in_addrs(option, len, &lease->dns, &lease->dns_size); |
566 | if (r < 0) | |
567 | log_debug_errno(r, "Failed to parse DNS server, ignoring: %m"); | |
a6cc569e TG |
568 | break; |
569 | ||
22805d92 | 570 | case SD_DHCP_OPTION_NTP_SERVER: |
0339cd77 LP |
571 | r = lease_parse_in_addrs(option, len, &lease->ntp, &lease->ntp_size); |
572 | if (r < 0) | |
573 | log_debug_errno(r, "Failed to parse NTP server, ignoring: %m"); | |
f5c0c00f TG |
574 | break; |
575 | ||
22805d92 | 576 | case SD_DHCP_OPTION_STATIC_ROUTE: |
0339cd77 LP |
577 | r = lease_parse_routes(option, len, &lease->static_route, &lease->static_route_size, &lease->static_route_allocated); |
578 | if (r < 0) | |
579 | log_debug_errno(r, "Failed to parse static routes, ignoring: %m"); | |
f5c0c00f TG |
580 | break; |
581 | ||
22805d92 | 582 | case SD_DHCP_OPTION_INTERFACE_MTU: |
0339cd77 LP |
583 | r = lease_parse_u16(option, len, &lease->mtu, 68); |
584 | if (r < 0) | |
585 | log_debug_errno(r, "Failed to parse MTU, ignoring: %m"); | |
955d99ed | 586 | if (lease->mtu < DHCP_DEFAULT_MIN_SIZE) { |
4dd53da9 | 587 | log_debug("MTU value of %" PRIu16 " too small. Using default MTU value of %d instead.", lease->mtu, DHCP_DEFAULT_MIN_SIZE); |
955d99ed MG |
588 | lease->mtu = DHCP_DEFAULT_MIN_SIZE; |
589 | } | |
590 | ||
f5c0c00f TG |
591 | break; |
592 | ||
978c6477 LP |
593 | case SD_DHCP_OPTION_DOMAIN_NAME: |
594 | r = lease_parse_domain(option, len, &lease->domainname); | |
0339cd77 LP |
595 | if (r < 0) { |
596 | log_debug_errno(r, "Failed to parse domain name, ignoring: %m"); | |
597 | return 0; | |
598 | } | |
a6cc569e | 599 | |
784d9b9c | 600 | break; |
784d9b9c | 601 | |
b85bc551 DW |
602 | case SD_DHCP_OPTION_DOMAIN_SEARCH_LIST: |
603 | r = dhcp_lease_parse_search_domains(option, len, &lease->search_domains); | |
604 | if (r < 0) | |
605 | log_debug_errno(r, "Failed to parse Domain Search List, ignoring: %m"); | |
606 | break; | |
607 | ||
978c6477 LP |
608 | case SD_DHCP_OPTION_HOST_NAME: |
609 | r = lease_parse_domain(option, len, &lease->hostname); | |
0339cd77 LP |
610 | if (r < 0) { |
611 | log_debug_errno(r, "Failed to parse host name, ignoring: %m"); | |
612 | return 0; | |
613 | } | |
a6cc569e | 614 | |
784d9b9c | 615 | break; |
ce78df79 | 616 | |
22805d92 | 617 | case SD_DHCP_OPTION_ROOT_PATH: |
0339cd77 LP |
618 | r = lease_parse_string(option, len, &lease->root_path); |
619 | if (r < 0) | |
620 | log_debug_errno(r, "Failed to parse root path, ignoring: %m"); | |
621 | break; | |
ce78df79 | 622 | |
22805d92 | 623 | case SD_DHCP_OPTION_RENEWAL_T1_TIME: |
0339cd77 LP |
624 | r = lease_parse_u32(option, len, &lease->t1, 1); |
625 | if (r < 0) | |
626 | log_debug_errno(r, "Failed to parse T1 time, ignoring: %m"); | |
a6cc569e TG |
627 | break; |
628 | ||
22805d92 | 629 | case SD_DHCP_OPTION_REBINDING_T2_TIME: |
0339cd77 LP |
630 | r = lease_parse_u32(option, len, &lease->t2, 1); |
631 | if (r < 0) | |
632 | log_debug_errno(r, "Failed to parse T2 time, ignoring: %m"); | |
a6cc569e | 633 | break; |
e1ea665e | 634 | |
22805d92 | 635 | case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE: |
0339cd77 | 636 | r = lease_parse_classless_routes( |
2d03c0b8 LP |
637 | option, len, |
638 | &lease->static_route, | |
639 | &lease->static_route_size, | |
640 | &lease->static_route_allocated); | |
0339cd77 LP |
641 | if (r < 0) |
642 | log_debug_errno(r, "Failed to parse classless routes, ignoring: %m"); | |
643 | break; | |
e43a8393 | 644 | |
22805d92 | 645 | case SD_DHCP_OPTION_NEW_TZDB_TIMEZONE: { |
8eb9058d LP |
646 | _cleanup_free_ char *tz = NULL; |
647 | ||
648 | r = lease_parse_string(option, len, &tz); | |
0339cd77 LP |
649 | if (r < 0) { |
650 | log_debug_errno(r, "Failed to parse timezone option, ignoring: %m"); | |
651 | return 0; | |
652 | } | |
8eb9058d | 653 | |
089fb865 | 654 | if (!timezone_is_valid(tz, LOG_DEBUG)) { |
0339cd77 LP |
655 | log_debug_errno(r, "Timezone is not valid, ignoring: %m"); |
656 | return 0; | |
657 | } | |
8eb9058d | 658 | |
f9ecfd3b | 659 | free_and_replace(lease->timezone, tz); |
0339cd77 | 660 | |
8eb9058d LP |
661 | break; |
662 | } | |
663 | ||
22805d92 | 664 | case SD_DHCP_OPTION_VENDOR_SPECIFIC: |
2d03c0b8 | 665 | |
0339cd77 LP |
666 | if (len <= 0) |
667 | lease->vendor_specific = mfree(lease->vendor_specific); | |
668 | else { | |
669 | void *p; | |
670 | ||
671 | p = memdup(option, len); | |
672 | if (!p) | |
e43a8393 | 673 | return -ENOMEM; |
e43a8393 | 674 | |
0339cd77 LP |
675 | free(lease->vendor_specific); |
676 | lease->vendor_specific = p; | |
677 | } | |
7e753d9d | 678 | |
0339cd77 LP |
679 | lease->vendor_specific_len = len; |
680 | break; | |
7e753d9d | 681 | |
22805d92 | 682 | case SD_DHCP_OPTION_PRIVATE_BASE ... SD_DHCP_OPTION_PRIVATE_LAST: |
7e753d9d AC |
683 | r = dhcp_lease_insert_private_option(lease, code, option, len); |
684 | if (r < 0) | |
685 | return r; | |
0339cd77 LP |
686 | |
687 | break; | |
688 | ||
689 | default: | |
f693e9b3 | 690 | log_debug("Ignoring option DHCP option %"PRIu8" while parsing.", code); |
0339cd77 | 691 | break; |
a6cc569e TG |
692 | } |
693 | ||
694 | return 0; | |
695 | } | |
696 | ||
b85bc551 DW |
697 | /* Parses compressed domain names. */ |
698 | int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***domains) { | |
699 | _cleanup_strv_free_ char **names = NULL; | |
700 | size_t pos = 0, cnt = 0; | |
701 | int r; | |
702 | ||
703 | assert(domains); | |
704 | assert_return(option && len > 0, -ENODATA); | |
705 | ||
706 | while (pos < len) { | |
707 | _cleanup_free_ char *name = NULL; | |
708 | size_t n = 0, allocated = 0; | |
709 | size_t jump_barrier = pos, next_chunk = 0; | |
710 | bool first = true; | |
711 | ||
712 | for (;;) { | |
713 | uint8_t c; | |
714 | c = option[pos++]; | |
715 | ||
716 | if (c == 0) { | |
717 | /* End of name */ | |
718 | break; | |
719 | } else if (c <= 63) { | |
720 | const char *label; | |
721 | ||
722 | /* Literal label */ | |
723 | label = (const char*) (option + pos); | |
724 | pos += c; | |
725 | if (pos >= len) | |
726 | return -EBADMSG; | |
727 | ||
728 | if (!GREEDY_REALLOC(name, allocated, n + !first + DNS_LABEL_ESCAPED_MAX)) | |
729 | return -ENOMEM; | |
730 | ||
731 | if (first) | |
732 | first = false; | |
733 | else | |
734 | name[n++] = '.'; | |
735 | ||
736 | r = dns_label_escape(label, c, name + n, DNS_LABEL_ESCAPED_MAX); | |
737 | if (r < 0) | |
738 | return r; | |
739 | ||
740 | n += r; | |
741 | } else if ((c & 0xc0) == 0xc0) { | |
742 | /* Pointer */ | |
743 | ||
744 | uint8_t d; | |
745 | uint16_t ptr; | |
746 | ||
747 | if (pos >= len) | |
748 | return -EBADMSG; | |
749 | ||
750 | d = option[pos++]; | |
751 | ptr = (uint16_t) (c & ~0xc0) << 8 | (uint16_t) d; | |
752 | ||
753 | /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */ | |
754 | if (ptr >= jump_barrier) | |
755 | return -EBADMSG; | |
756 | jump_barrier = ptr; | |
757 | ||
758 | /* Save current location so we don't end up re-parsing what's parsed so far. */ | |
759 | if (next_chunk == 0) | |
760 | next_chunk = pos; | |
761 | ||
762 | pos = ptr; | |
763 | } else | |
764 | return -EBADMSG; | |
765 | } | |
766 | ||
767 | if (!GREEDY_REALLOC(name, allocated, n + 1)) | |
768 | return -ENOMEM; | |
769 | name[n] = 0; | |
770 | ||
771 | r = strv_extend(&names, name); | |
772 | if (r < 0) | |
773 | return r; | |
774 | ||
775 | cnt++; | |
776 | ||
777 | if (next_chunk != 0) | |
778 | pos = next_chunk; | |
779 | } | |
780 | ||
ae2a15bc | 781 | *domains = TAKE_PTR(names); |
b85bc551 DW |
782 | |
783 | return cnt; | |
784 | } | |
785 | ||
0339cd77 | 786 | int dhcp_lease_insert_private_option(sd_dhcp_lease *lease, uint8_t tag, const void *data, uint8_t len) { |
7e753d9d AC |
787 | struct sd_dhcp_raw_option *cur, *option; |
788 | ||
0339cd77 LP |
789 | assert(lease); |
790 | ||
7e753d9d AC |
791 | LIST_FOREACH(options, cur, lease->private_options) { |
792 | if (tag < cur->tag) | |
793 | break; | |
0339cd77 LP |
794 | if (tag == cur->tag) { |
795 | log_debug("Ignoring duplicate option, tagged %i.", tag); | |
7e753d9d AC |
796 | return 0; |
797 | } | |
798 | } | |
799 | ||
800 | option = new(struct sd_dhcp_raw_option, 1); | |
801 | if (!option) | |
802 | return -ENOMEM; | |
803 | ||
804 | option->tag = tag; | |
805 | option->length = len; | |
806 | option->data = memdup(data, len); | |
807 | if (!option->data) { | |
808 | free(option); | |
809 | return -ENOMEM; | |
810 | } | |
811 | ||
812 | LIST_INSERT_BEFORE(options, lease->private_options, cur, option); | |
7e753d9d AC |
813 | return 0; |
814 | } | |
815 | ||
a6cc569e | 816 | int dhcp_lease_new(sd_dhcp_lease **ret) { |
6e00a806 | 817 | sd_dhcp_lease *lease; |
a6cc569e TG |
818 | |
819 | lease = new0(sd_dhcp_lease, 1); | |
820 | if (!lease) | |
821 | return -ENOMEM; | |
822 | ||
8ddbeaa2 | 823 | lease->router = INADDR_ANY; |
3733eec3 | 824 | lease->n_ref = 1; |
a6cc569e TG |
825 | |
826 | *ret = lease; | |
a6cc569e TG |
827 | return 0; |
828 | } | |
fe8db0c5 | 829 | |
bd91b83e | 830 | int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { |
fe8db0c5 TG |
831 | _cleanup_free_ char *temp_path = NULL; |
832 | _cleanup_fclose_ FILE *f = NULL; | |
a073309f | 833 | struct sd_dhcp_raw_option *option; |
fe8db0c5 | 834 | struct in_addr address; |
a2ba62c7 | 835 | const struct in_addr *addresses; |
e4735228 | 836 | const void *client_id, *data; |
e43a8393 | 837 | size_t client_id_len, data_len; |
fe8db0c5 TG |
838 | const char *string; |
839 | uint16_t mtu; | |
f8693fc7 | 840 | _cleanup_free_ sd_dhcp_route **routes = NULL; |
b85bc551 | 841 | char **search_domains = NULL; |
0339cd77 | 842 | uint32_t t1, t2, lifetime; |
fe8db0c5 TG |
843 | int r; |
844 | ||
845 | assert(lease); | |
846 | assert(lease_file); | |
847 | ||
fe8db0c5 TG |
848 | r = fopen_temporary(lease_file, &f, &temp_path); |
849 | if (r < 0) | |
dacd6cee | 850 | goto fail; |
fe8db0c5 | 851 | |
0d536673 LP |
852 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
853 | (void) fchmod(fileno(f), 0644); | |
fe8db0c5 | 854 | |
fe8db0c5 | 855 | fprintf(f, |
0339cd77 | 856 | "# This is private data. Do not parse.\n"); |
fe8db0c5 | 857 | |
0339cd77 LP |
858 | r = sd_dhcp_lease_get_address(lease, &address); |
859 | if (r >= 0) | |
860 | fprintf(f, "ADDRESS=%s\n", inet_ntoa(address)); | |
fe8db0c5 | 861 | |
0339cd77 LP |
862 | r = sd_dhcp_lease_get_netmask(lease, &address); |
863 | if (r >= 0) | |
864 | fprintf(f, "NETMASK=%s\n", inet_ntoa(address)); | |
fe8db0c5 | 865 | |
8ddbeaa2 UTL |
866 | r = sd_dhcp_lease_get_router(lease, &address); |
867 | if (r >= 0) | |
868 | fprintf(f, "ROUTER=%s\n", inet_ntoa(address)); | |
869 | ||
0ad853bc | 870 | r = sd_dhcp_lease_get_server_identifier(lease, &address); |
109731eb | 871 | if (r >= 0) |
0339cd77 | 872 | fprintf(f, "SERVER_ADDRESS=%s\n", inet_ntoa(address)); |
0ad853bc | 873 | |
8e34a618 | 874 | r = sd_dhcp_lease_get_next_server(lease, &address); |
109731eb TG |
875 | if (r >= 0) |
876 | fprintf(f, "NEXT_SERVER=%s\n", inet_ntoa(address)); | |
8e34a618 | 877 | |
0339cd77 LP |
878 | r = sd_dhcp_lease_get_broadcast(lease, &address); |
879 | if (r >= 0) | |
880 | fprintf(f, "BROADCAST=%s\n", inet_ntoa(address)); | |
881 | ||
fe8db0c5 TG |
882 | r = sd_dhcp_lease_get_mtu(lease, &mtu); |
883 | if (r >= 0) | |
884 | fprintf(f, "MTU=%" PRIu16 "\n", mtu); | |
885 | ||
0339cd77 LP |
886 | r = sd_dhcp_lease_get_t1(lease, &t1); |
887 | if (r >= 0) | |
888 | fprintf(f, "T1=%" PRIu32 "\n", t1); | |
889 | ||
890 | r = sd_dhcp_lease_get_t2(lease, &t2); | |
109731eb | 891 | if (r >= 0) |
0339cd77 LP |
892 | fprintf(f, "T2=%" PRIu32 "\n", t2); |
893 | ||
894 | r = sd_dhcp_lease_get_lifetime(lease, &lifetime); | |
895 | if (r >= 0) | |
896 | fprintf(f, "LIFETIME=%" PRIu32 "\n", lifetime); | |
897 | ||
898 | r = sd_dhcp_lease_get_dns(lease, &addresses); | |
899 | if (r > 0) { | |
0d536673 | 900 | fputs("DNS=", f); |
b0e39c82 | 901 | serialize_in_addrs(f, addresses, r); |
0d536673 | 902 | fputs("\n", f); |
0339cd77 | 903 | } |
109731eb | 904 | |
a2ba62c7 | 905 | r = sd_dhcp_lease_get_ntp(lease, &addresses); |
0339cd77 | 906 | if (r > 0) { |
0d536673 | 907 | fputs("NTP=", f); |
b0e39c82 | 908 | serialize_in_addrs(f, addresses, r); |
0d536673 | 909 | fputs("\n", f); |
0339cd77 | 910 | } |
fe8db0c5 TG |
911 | |
912 | r = sd_dhcp_lease_get_domainname(lease, &string); | |
913 | if (r >= 0) | |
914 | fprintf(f, "DOMAINNAME=%s\n", string); | |
915 | ||
b85bc551 DW |
916 | r = sd_dhcp_lease_get_search_domains(lease, &search_domains); |
917 | if (r > 0) { | |
0d536673 | 918 | fputs("DOMAIN_SEARCH_LIST=", f); |
b85bc551 | 919 | fputstrv(f, search_domains, NULL, NULL); |
0d536673 | 920 | fputs("\n", f); |
b85bc551 DW |
921 | } |
922 | ||
fe8db0c5 TG |
923 | r = sd_dhcp_lease_get_hostname(lease, &string); |
924 | if (r >= 0) | |
925 | fprintf(f, "HOSTNAME=%s\n", string); | |
926 | ||
ce78df79 TG |
927 | r = sd_dhcp_lease_get_root_path(lease, &string); |
928 | if (r >= 0) | |
929 | fprintf(f, "ROOT_PATH=%s\n", string); | |
930 | ||
a2ba62c7 | 931 | r = sd_dhcp_lease_get_routes(lease, &routes); |
0339cd77 | 932 | if (r > 0) |
a2ba62c7 | 933 | serialize_dhcp_routes(f, "ROUTES", routes, r); |
e1ea665e | 934 | |
8eb9058d LP |
935 | r = sd_dhcp_lease_get_timezone(lease, &string); |
936 | if (r >= 0) | |
937 | fprintf(f, "TIMEZONE=%s\n", string); | |
938 | ||
e37f74a6 DW |
939 | r = sd_dhcp_lease_get_client_id(lease, &client_id, &client_id_len); |
940 | if (r >= 0) { | |
3587161a | 941 | _cleanup_free_ char *client_id_hex = NULL; |
e37f74a6 | 942 | |
dde8bb32 | 943 | client_id_hex = hexmem(client_id, client_id_len); |
e37f74a6 DW |
944 | if (!client_id_hex) { |
945 | r = -ENOMEM; | |
dacd6cee | 946 | goto fail; |
e37f74a6 DW |
947 | } |
948 | fprintf(f, "CLIENTID=%s\n", client_id_hex); | |
949 | } | |
950 | ||
e43a8393 BG |
951 | r = sd_dhcp_lease_get_vendor_specific(lease, &data, &data_len); |
952 | if (r >= 0) { | |
953 | _cleanup_free_ char *option_hex = NULL; | |
954 | ||
955 | option_hex = hexmem(data, data_len); | |
956 | if (!option_hex) { | |
957 | r = -ENOMEM; | |
dacd6cee | 958 | goto fail; |
e43a8393 BG |
959 | } |
960 | fprintf(f, "VENDOR_SPECIFIC=%s\n", option_hex); | |
961 | } | |
962 | ||
a073309f | 963 | LIST_FOREACH(options, option, lease->private_options) { |
dbcb4a90 | 964 | char key[STRLEN("OPTION_000")+1]; |
0339cd77 | 965 | |
d054f0a4 | 966 | xsprintf(key, "OPTION_%" PRIu8, option->tag); |
a073309f AC |
967 | r = serialize_dhcp_option(f, key, option->data, option->length); |
968 | if (r < 0) | |
969 | goto fail; | |
970 | } | |
971 | ||
dacd6cee LP |
972 | r = fflush_and_check(f); |
973 | if (r < 0) | |
974 | goto fail; | |
fe8db0c5 | 975 | |
dacd6cee | 976 | if (rename(temp_path, lease_file) < 0) { |
fe8db0c5 | 977 | r = -errno; |
dacd6cee | 978 | goto fail; |
fe8db0c5 TG |
979 | } |
980 | ||
dacd6cee LP |
981 | return 0; |
982 | ||
983 | fail: | |
984 | if (temp_path) | |
985 | (void) unlink(temp_path); | |
fe8db0c5 | 986 | |
dacd6cee | 987 | return log_error_errno(r, "Failed to save lease data %s: %m", lease_file); |
fe8db0c5 TG |
988 | } |
989 | ||
bd91b83e | 990 | int dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) { |
0339cd77 | 991 | |
4afd3348 | 992 | _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL; |
0339cd77 LP |
993 | _cleanup_free_ char |
994 | *address = NULL, | |
995 | *router = NULL, | |
996 | *netmask = NULL, | |
997 | *server_address = NULL, | |
998 | *next_server = NULL, | |
999 | *broadcast = NULL, | |
1000 | *dns = NULL, | |
1001 | *ntp = NULL, | |
1002 | *mtu = NULL, | |
1003 | *routes = NULL, | |
b85bc551 | 1004 | *domains = NULL, |
0339cd77 LP |
1005 | *client_id_hex = NULL, |
1006 | *vendor_specific_hex = NULL, | |
1007 | *lifetime = NULL, | |
1008 | *t1 = NULL, | |
1009 | *t2 = NULL, | |
22805d92 | 1010 | *options[SD_DHCP_OPTION_PRIVATE_LAST - SD_DHCP_OPTION_PRIVATE_BASE + 1] = {}; |
0339cd77 | 1011 | |
a073309f | 1012 | int r, i; |
fe8db0c5 TG |
1013 | |
1014 | assert(lease_file); | |
1015 | assert(ret); | |
1016 | ||
1017 | r = dhcp_lease_new(&lease); | |
1018 | if (r < 0) | |
1019 | return r; | |
1020 | ||
aa8fbc74 | 1021 | r = parse_env_file(NULL, lease_file, |
fe8db0c5 TG |
1022 | "ADDRESS", &address, |
1023 | "ROUTER", &router, | |
1024 | "NETMASK", &netmask, | |
0ad853bc | 1025 | "SERVER_IDENTIFIER", &server_address, |
8e34a618 | 1026 | "NEXT_SERVER", &next_server, |
0339cd77 | 1027 | "BROADCAST", &broadcast, |
109731eb TG |
1028 | "DNS", &dns, |
1029 | "NTP", &ntp, | |
fe8db0c5 TG |
1030 | "MTU", &mtu, |
1031 | "DOMAINNAME", &lease->domainname, | |
1032 | "HOSTNAME", &lease->hostname, | |
b85bc551 | 1033 | "DOMAIN_SEARCH_LIST", &domains, |
ce78df79 | 1034 | "ROOT_PATH", &lease->root_path, |
e1ea665e | 1035 | "ROUTES", &routes, |
e37f74a6 | 1036 | "CLIENTID", &client_id_hex, |
8eb9058d | 1037 | "TIMEZONE", &lease->timezone, |
e43a8393 | 1038 | "VENDOR_SPECIFIC", &vendor_specific_hex, |
0339cd77 LP |
1039 | "LIFETIME", &lifetime, |
1040 | "T1", &t1, | |
1041 | "T2", &t2, | |
a073309f AC |
1042 | "OPTION_224", &options[0], |
1043 | "OPTION_225", &options[1], | |
1044 | "OPTION_226", &options[2], | |
1045 | "OPTION_227", &options[3], | |
1046 | "OPTION_228", &options[4], | |
1047 | "OPTION_229", &options[5], | |
1048 | "OPTION_230", &options[6], | |
1049 | "OPTION_231", &options[7], | |
1050 | "OPTION_232", &options[8], | |
1051 | "OPTION_233", &options[9], | |
1052 | "OPTION_234", &options[10], | |
1053 | "OPTION_235", &options[11], | |
1054 | "OPTION_236", &options[12], | |
1055 | "OPTION_237", &options[13], | |
1056 | "OPTION_238", &options[14], | |
1057 | "OPTION_239", &options[15], | |
1058 | "OPTION_240", &options[16], | |
1059 | "OPTION_241", &options[17], | |
1060 | "OPTION_242", &options[18], | |
1061 | "OPTION_243", &options[19], | |
1062 | "OPTION_244", &options[20], | |
1063 | "OPTION_245", &options[21], | |
1064 | "OPTION_246", &options[22], | |
1065 | "OPTION_247", &options[23], | |
1066 | "OPTION_248", &options[24], | |
1067 | "OPTION_249", &options[25], | |
1068 | "OPTION_250", &options[26], | |
1069 | "OPTION_251", &options[27], | |
1070 | "OPTION_252", &options[28], | |
1071 | "OPTION_253", &options[29], | |
13df9c39 | 1072 | "OPTION_254", &options[30]); |
fe8db0c5 TG |
1073 | if (r < 0) |
1074 | return r; | |
1075 | ||
0339cd77 LP |
1076 | if (address) { |
1077 | r = inet_pton(AF_INET, address, &lease->address); | |
1078 | if (r <= 0) | |
e26ea7fc | 1079 | log_debug("Failed to parse address %s, ignoring.", address); |
0339cd77 | 1080 | } |
fe8db0c5 | 1081 | |
8ddbeaa2 | 1082 | if (router) { |
0339cd77 LP |
1083 | r = inet_pton(AF_INET, router, &lease->router); |
1084 | if (r <= 0) | |
e26ea7fc | 1085 | log_debug("Failed to parse router %s, ignoring.", router); |
8ddbeaa2 | 1086 | } |
fe8db0c5 | 1087 | |
0339cd77 LP |
1088 | if (netmask) { |
1089 | r = inet_pton(AF_INET, netmask, &lease->subnet_mask); | |
1090 | if (r <= 0) | |
e26ea7fc | 1091 | log_debug("Failed to parse netmask %s, ignoring.", netmask); |
0339cd77 LP |
1092 | else |
1093 | lease->have_subnet_mask = true; | |
1094 | } | |
fe8db0c5 | 1095 | |
0ad853bc | 1096 | if (server_address) { |
0339cd77 LP |
1097 | r = inet_pton(AF_INET, server_address, &lease->server_address); |
1098 | if (r <= 0) | |
e26ea7fc | 1099 | log_debug("Failed to parse server address %s, ignoring.", server_address); |
0ad853bc TG |
1100 | } |
1101 | ||
8e34a618 | 1102 | if (next_server) { |
0339cd77 LP |
1103 | r = inet_pton(AF_INET, next_server, &lease->next_server); |
1104 | if (r <= 0) | |
e26ea7fc | 1105 | log_debug("Failed to parse next server %s, ignoring.", next_server); |
0339cd77 | 1106 | } |
8e34a618 | 1107 | |
0339cd77 LP |
1108 | if (broadcast) { |
1109 | r = inet_pton(AF_INET, broadcast, &lease->broadcast); | |
1110 | if (r <= 0) | |
e26ea7fc | 1111 | log_debug("Failed to parse broadcast address %s, ignoring.", broadcast); |
0339cd77 LP |
1112 | else |
1113 | lease->have_broadcast = true; | |
8e34a618 TG |
1114 | } |
1115 | ||
109731eb | 1116 | if (dns) { |
a2ba62c7 | 1117 | r = deserialize_in_addrs(&lease->dns, dns); |
109731eb | 1118 | if (r < 0) |
0339cd77 LP |
1119 | log_debug_errno(r, "Failed to deserialize DNS servers %s, ignoring: %m", dns); |
1120 | else | |
1121 | lease->dns_size = r; | |
109731eb TG |
1122 | } |
1123 | ||
1124 | if (ntp) { | |
a2ba62c7 | 1125 | r = deserialize_in_addrs(&lease->ntp, ntp); |
109731eb | 1126 | if (r < 0) |
0339cd77 LP |
1127 | log_debug_errno(r, "Failed to deserialize NTP servers %s, ignoring: %m", ntp); |
1128 | else | |
1129 | lease->ntp_size = r; | |
109731eb TG |
1130 | } |
1131 | ||
fe8db0c5 | 1132 | if (mtu) { |
0339cd77 LP |
1133 | r = safe_atou16(mtu, &lease->mtu); |
1134 | if (r < 0) | |
1135 | log_debug_errno(r, "Failed to parse MTU %s, ignoring: %m", mtu); | |
fe8db0c5 TG |
1136 | } |
1137 | ||
b85bc551 DW |
1138 | if (domains) { |
1139 | _cleanup_strv_free_ char **a = NULL; | |
1140 | a = strv_split(domains, " "); | |
1141 | if (!a) | |
1142 | return -ENOMEM; | |
1143 | ||
1144 | if (!strv_isempty(a)) { | |
1145 | lease->search_domains = a; | |
1146 | a = NULL; | |
1147 | } | |
1148 | } | |
1149 | ||
e1ea665e | 1150 | if (routes) { |
0339cd77 LP |
1151 | r = deserialize_dhcp_routes( |
1152 | &lease->static_route, | |
1153 | &lease->static_route_size, | |
1154 | &lease->static_route_allocated, | |
1155 | routes); | |
1156 | if (r < 0) | |
1157 | log_debug_errno(r, "Failed to parse DHCP routes %s, ignoring: %m", routes); | |
1158 | } | |
1159 | ||
1160 | if (lifetime) { | |
1161 | r = safe_atou32(lifetime, &lease->lifetime); | |
1162 | if (r < 0) | |
1163 | log_debug_errno(r, "Failed to parse lifetime %s, ignoring: %m", lifetime); | |
1164 | } | |
1165 | ||
1166 | if (t1) { | |
1167 | r = safe_atou32(t1, &lease->t1); | |
1168 | if (r < 0) | |
1169 | log_debug_errno(r, "Failed to parse T1 %s, ignoring: %m", t1); | |
1170 | } | |
1171 | ||
1172 | if (t2) { | |
1173 | r = safe_atou32(t2, &lease->t2); | |
e1ea665e | 1174 | if (r < 0) |
0339cd77 | 1175 | log_debug_errno(r, "Failed to parse T2 %s, ignoring: %m", t2); |
e1ea665e EY |
1176 | } |
1177 | ||
e37f74a6 | 1178 | if (client_id_hex) { |
ce088d6f | 1179 | r = unhexmem(client_id_hex, (size_t) -1, &lease->client_id, &lease->client_id_len); |
30494563 | 1180 | if (r < 0) |
0339cd77 | 1181 | log_debug_errno(r, "Failed to parse client ID %s, ignoring: %m", client_id_hex); |
e37f74a6 DW |
1182 | } |
1183 | ||
e43a8393 | 1184 | if (vendor_specific_hex) { |
ce088d6f | 1185 | r = unhexmem(vendor_specific_hex, (size_t) -1, &lease->vendor_specific, &lease->vendor_specific_len); |
e43a8393 | 1186 | if (r < 0) |
0339cd77 | 1187 | log_debug_errno(r, "Failed to parse vendor specific data %s, ignoring: %m", vendor_specific_hex); |
e43a8393 BG |
1188 | } |
1189 | ||
22805d92 | 1190 | for (i = 0; i <= SD_DHCP_OPTION_PRIVATE_LAST - SD_DHCP_OPTION_PRIVATE_BASE; i++) { |
e4735228 | 1191 | _cleanup_free_ void *data = NULL; |
a073309f AC |
1192 | size_t len; |
1193 | ||
1194 | if (!options[i]) | |
1195 | continue; | |
1196 | ||
ce088d6f | 1197 | r = unhexmem(options[i], (size_t) -1, &data, &len); |
0339cd77 LP |
1198 | if (r < 0) { |
1199 | log_debug_errno(r, "Failed to parse private DHCP option %s, ignoring: %m", options[i]); | |
1200 | continue; | |
1201 | } | |
a073309f | 1202 | |
22805d92 | 1203 | r = dhcp_lease_insert_private_option(lease, SD_DHCP_OPTION_PRIVATE_BASE + i, data, len); |
626be147 | 1204 | if (r < 0) |
a073309f AC |
1205 | return r; |
1206 | } | |
1207 | ||
1cc6c93a | 1208 | *ret = TAKE_PTR(lease); |
fe8db0c5 TG |
1209 | |
1210 | return 0; | |
1211 | } | |
9e64dd72 TG |
1212 | |
1213 | int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease *lease) { | |
0339cd77 | 1214 | struct in_addr address, mask; |
df40eee8 | 1215 | int r; |
9e64dd72 TG |
1216 | |
1217 | assert(lease); | |
9e64dd72 | 1218 | |
0339cd77 LP |
1219 | if (lease->address == 0) |
1220 | return -ENODATA; | |
1221 | ||
df40eee8 | 1222 | address.s_addr = lease->address; |
9e64dd72 TG |
1223 | |
1224 | /* fall back to the default subnet masks based on address class */ | |
5a941f5f | 1225 | r = in4_addr_default_subnet_mask(&address, &mask); |
df40eee8 TG |
1226 | if (r < 0) |
1227 | return r; | |
9e64dd72 | 1228 | |
df40eee8 | 1229 | lease->subnet_mask = mask.s_addr; |
0339cd77 | 1230 | lease->have_subnet_mask = true; |
9e64dd72 TG |
1231 | |
1232 | return 0; | |
1233 | } | |
e37f74a6 | 1234 | |
0339cd77 | 1235 | int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const void **client_id, size_t *client_id_len) { |
e37f74a6 DW |
1236 | assert_return(lease, -EINVAL); |
1237 | assert_return(client_id, -EINVAL); | |
1238 | assert_return(client_id_len, -EINVAL); | |
1239 | ||
0339cd77 LP |
1240 | if (!lease->client_id) |
1241 | return -ENODATA; | |
1242 | ||
e37f74a6 DW |
1243 | *client_id = lease->client_id; |
1244 | *client_id_len = lease->client_id_len; | |
0339cd77 | 1245 | |
e37f74a6 DW |
1246 | return 0; |
1247 | } | |
1248 | ||
0339cd77 | 1249 | int dhcp_lease_set_client_id(sd_dhcp_lease *lease, const void *client_id, size_t client_id_len) { |
e37f74a6 | 1250 | assert_return(lease, -EINVAL); |
0339cd77 LP |
1251 | assert_return(client_id || client_id_len <= 0, -EINVAL); |
1252 | ||
1253 | if (client_id_len <= 0) | |
1254 | lease->client_id = mfree(lease->client_id); | |
1255 | else { | |
1256 | void *p; | |
e37f74a6 | 1257 | |
0339cd77 LP |
1258 | p = memdup(client_id, client_id_len); |
1259 | if (!p) | |
1260 | return -ENOMEM; | |
e37f74a6 | 1261 | |
0339cd77 LP |
1262 | free(lease->client_id); |
1263 | lease->client_id = p; | |
e37f74a6 DW |
1264 | lease->client_id_len = client_id_len; |
1265 | } | |
1266 | ||
1267 | return 0; | |
1268 | } | |
8eb9058d | 1269 | |
64d6c229 | 1270 | int sd_dhcp_lease_get_timezone(sd_dhcp_lease *lease, const char **tz) { |
8eb9058d | 1271 | assert_return(lease, -EINVAL); |
64d6c229 | 1272 | assert_return(tz, -EINVAL); |
8eb9058d LP |
1273 | |
1274 | if (!lease->timezone) | |
0339cd77 | 1275 | return -ENODATA; |
8eb9058d | 1276 | |
64d6c229 | 1277 | *tz = lease->timezone; |
8eb9058d LP |
1278 | return 0; |
1279 | } | |
f8693fc7 BG |
1280 | |
1281 | int sd_dhcp_route_get_destination(sd_dhcp_route *route, struct in_addr *destination) { | |
1282 | assert_return(route, -EINVAL); | |
1283 | assert_return(destination, -EINVAL); | |
1284 | ||
1285 | *destination = route->dst_addr; | |
1286 | return 0; | |
1287 | } | |
1288 | ||
1289 | int sd_dhcp_route_get_destination_prefix_length(sd_dhcp_route *route, uint8_t *length) { | |
1290 | assert_return(route, -EINVAL); | |
1291 | assert_return(length, -EINVAL); | |
1292 | ||
1293 | *length = route->dst_prefixlen; | |
1294 | return 0; | |
1295 | } | |
1296 | ||
1297 | int sd_dhcp_route_get_gateway(sd_dhcp_route *route, struct in_addr *gateway) { | |
1298 | assert_return(route, -EINVAL); | |
1299 | assert_return(gateway, -EINVAL); | |
1300 | ||
1301 | *gateway = route->gw_addr; | |
1302 | return 0; | |
1303 | } | |
cf6f5bb5 TH |
1304 | |
1305 | int sd_dhcp_route_get_option(sd_dhcp_route *route) { | |
1306 | assert_return(route, -EINVAL); | |
1307 | ||
1308 | return route->option; | |
1309 | } |