]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | /*** | |
3 | Copyright © 2013 Intel Corporation. All rights reserved. | |
4 | ***/ | |
5 | ||
6 | #include <arpa/inet.h> | |
7 | #include <sys/stat.h> | |
8 | ||
9 | #include "sd-dhcp-lease.h" | |
10 | ||
11 | #include "alloc-util.h" | |
12 | #include "dhcp-lease-internal.h" | |
13 | #include "dhcp-option.h" | |
14 | #include "dns-def.h" | |
15 | #include "dns-domain.h" | |
16 | #include "dns-resolver-internal.h" | |
17 | #include "env-file.h" | |
18 | #include "fd-util.h" | |
19 | #include "fileio.h" | |
20 | #include "fs-util.h" | |
21 | #include "hexdecoct.h" | |
22 | #include "hostname-util.h" | |
23 | #include "in-addr-util.h" | |
24 | #include "network-common.h" | |
25 | #include "network-internal.h" | |
26 | #include "parse-util.h" | |
27 | #include "sort-util.h" | |
28 | #include "stdio-util.h" | |
29 | #include "string-util.h" | |
30 | #include "strv.h" | |
31 | #include "time-util.h" | |
32 | #include "tmpfile-util.h" | |
33 | #include "unaligned.h" | |
34 | ||
35 | void dhcp_lease_set_timestamp(sd_dhcp_lease *lease, const triple_timestamp *timestamp) { | |
36 | assert(lease); | |
37 | ||
38 | if (timestamp && triple_timestamp_is_set(timestamp)) | |
39 | lease->timestamp = *timestamp; | |
40 | else | |
41 | triple_timestamp_now(&lease->timestamp); | |
42 | } | |
43 | ||
44 | int sd_dhcp_lease_get_timestamp(sd_dhcp_lease *lease, clockid_t clock, uint64_t *ret) { | |
45 | assert_return(lease, -EINVAL); | |
46 | assert_return(TRIPLE_TIMESTAMP_HAS_CLOCK(clock), -EOPNOTSUPP); | |
47 | assert_return(clock_supported(clock), -EOPNOTSUPP); | |
48 | assert_return(ret, -EINVAL); | |
49 | ||
50 | if (!triple_timestamp_is_set(&lease->timestamp)) | |
51 | return -ENODATA; | |
52 | ||
53 | *ret = triple_timestamp_by_clock(&lease->timestamp, clock); | |
54 | return 0; | |
55 | } | |
56 | ||
57 | int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) { | |
58 | assert_return(lease, -EINVAL); | |
59 | assert_return(addr, -EINVAL); | |
60 | ||
61 | if (lease->address == 0) | |
62 | return -ENODATA; | |
63 | ||
64 | addr->s_addr = lease->address; | |
65 | return 0; | |
66 | } | |
67 | ||
68 | int sd_dhcp_lease_get_broadcast(sd_dhcp_lease *lease, struct in_addr *addr) { | |
69 | assert_return(lease, -EINVAL); | |
70 | assert_return(addr, -EINVAL); | |
71 | ||
72 | if (!lease->have_broadcast) | |
73 | return -ENODATA; | |
74 | ||
75 | addr->s_addr = lease->broadcast; | |
76 | return 0; | |
77 | } | |
78 | ||
79 | int sd_dhcp_lease_get_lifetime(sd_dhcp_lease *lease, uint64_t *ret) { | |
80 | assert_return(lease, -EINVAL); | |
81 | assert_return(ret, -EINVAL); | |
82 | ||
83 | if (lease->lifetime <= 0) | |
84 | return -ENODATA; | |
85 | ||
86 | *ret = lease->lifetime; | |
87 | return 0; | |
88 | } | |
89 | ||
90 | int sd_dhcp_lease_get_t1(sd_dhcp_lease *lease, uint64_t *ret) { | |
91 | assert_return(lease, -EINVAL); | |
92 | assert_return(ret, -EINVAL); | |
93 | ||
94 | if (lease->t1 <= 0) | |
95 | return -ENODATA; | |
96 | ||
97 | *ret = lease->t1; | |
98 | return 0; | |
99 | } | |
100 | ||
101 | int sd_dhcp_lease_get_t2(sd_dhcp_lease *lease, uint64_t *ret) { | |
102 | assert_return(lease, -EINVAL); | |
103 | assert_return(ret, -EINVAL); | |
104 | ||
105 | if (lease->t2 <= 0) | |
106 | return -ENODATA; | |
107 | ||
108 | *ret = lease->t2; | |
109 | return 0; | |
110 | } | |
111 | ||
112 | #define DEFINE_GET_TIMESTAMP(name) \ | |
113 | int sd_dhcp_lease_get_##name##_timestamp( \ | |
114 | sd_dhcp_lease *lease, \ | |
115 | clockid_t clock, \ | |
116 | uint64_t *ret) { \ | |
117 | \ | |
118 | usec_t t, timestamp; \ | |
119 | int r; \ | |
120 | \ | |
121 | assert_return(ret, -EINVAL); \ | |
122 | \ | |
123 | r = sd_dhcp_lease_get_##name(lease, &t); \ | |
124 | if (r < 0) \ | |
125 | return r; \ | |
126 | \ | |
127 | r = sd_dhcp_lease_get_timestamp(lease, clock, ×tamp); \ | |
128 | if (r < 0) \ | |
129 | return r; \ | |
130 | \ | |
131 | *ret = usec_add(t, timestamp); \ | |
132 | return 0; \ | |
133 | } | |
134 | ||
135 | DEFINE_GET_TIMESTAMP(lifetime); | |
136 | DEFINE_GET_TIMESTAMP(t1); | |
137 | DEFINE_GET_TIMESTAMP(t2); | |
138 | ||
139 | int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu) { | |
140 | assert_return(lease, -EINVAL); | |
141 | assert_return(mtu, -EINVAL); | |
142 | ||
143 | if (lease->mtu <= 0) | |
144 | return -ENODATA; | |
145 | ||
146 | *mtu = lease->mtu; | |
147 | return 0; | |
148 | } | |
149 | ||
150 | int sd_dhcp_lease_get_servers( | |
151 | sd_dhcp_lease *lease, | |
152 | sd_dhcp_lease_server_type_t what, | |
153 | const struct in_addr **addr) { | |
154 | ||
155 | assert_return(lease, -EINVAL); | |
156 | assert_return(what >= 0, -EINVAL); | |
157 | assert_return(what < _SD_DHCP_LEASE_SERVER_TYPE_MAX, -EINVAL); | |
158 | ||
159 | if (lease->servers[what].size <= 0) | |
160 | return -ENODATA; | |
161 | ||
162 | if (addr) | |
163 | *addr = lease->servers[what].addr; | |
164 | ||
165 | return (int) lease->servers[what].size; | |
166 | } | |
167 | ||
168 | int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr) { | |
169 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_DNS, addr); | |
170 | } | |
171 | int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr) { | |
172 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_NTP, addr); | |
173 | } | |
174 | int sd_dhcp_lease_get_sip(sd_dhcp_lease *lease, const struct in_addr **addr) { | |
175 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_SIP, addr); | |
176 | } | |
177 | int sd_dhcp_lease_get_pop3(sd_dhcp_lease *lease, const struct in_addr **addr) { | |
178 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_POP3, addr); | |
179 | } | |
180 | int sd_dhcp_lease_get_smtp(sd_dhcp_lease *lease, const struct in_addr **addr) { | |
181 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_SMTP, addr); | |
182 | } | |
183 | int sd_dhcp_lease_get_lpr(sd_dhcp_lease *lease, const struct in_addr **addr) { | |
184 | return sd_dhcp_lease_get_servers(lease, SD_DHCP_LEASE_LPR, addr); | |
185 | } | |
186 | ||
187 | int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname) { | |
188 | assert_return(lease, -EINVAL); | |
189 | assert_return(domainname, -EINVAL); | |
190 | ||
191 | if (!lease->domainname) | |
192 | return -ENODATA; | |
193 | ||
194 | *domainname = lease->domainname; | |
195 | return 0; | |
196 | } | |
197 | ||
198 | int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname) { | |
199 | assert_return(lease, -EINVAL); | |
200 | assert_return(hostname, -EINVAL); | |
201 | ||
202 | if (!lease->hostname) | |
203 | return -ENODATA; | |
204 | ||
205 | *hostname = lease->hostname; | |
206 | return 0; | |
207 | } | |
208 | ||
209 | int sd_dhcp_lease_get_root_path(sd_dhcp_lease *lease, const char **root_path) { | |
210 | assert_return(lease, -EINVAL); | |
211 | assert_return(root_path, -EINVAL); | |
212 | ||
213 | if (!lease->root_path) | |
214 | return -ENODATA; | |
215 | ||
216 | *root_path = lease->root_path; | |
217 | return 0; | |
218 | } | |
219 | ||
220 | int sd_dhcp_lease_get_captive_portal(sd_dhcp_lease *lease, const char **ret) { | |
221 | assert_return(lease, -EINVAL); | |
222 | assert_return(ret, -EINVAL); | |
223 | ||
224 | if (!lease->captive_portal) | |
225 | return -ENODATA; | |
226 | ||
227 | *ret = lease->captive_portal; | |
228 | return 0; | |
229 | } | |
230 | ||
231 | int sd_dhcp_lease_get_dnr(sd_dhcp_lease *lease, sd_dns_resolver **ret_resolvers) { | |
232 | assert_return(lease, -EINVAL); | |
233 | assert_return(ret_resolvers, -EINVAL); | |
234 | ||
235 | if (!lease->dnr) | |
236 | return -ENODATA; | |
237 | ||
238 | *ret_resolvers = lease->dnr; | |
239 | return lease->n_dnr; | |
240 | } | |
241 | ||
242 | int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, const struct in_addr **addr) { | |
243 | assert_return(lease, -EINVAL); | |
244 | assert_return(addr, -EINVAL); | |
245 | ||
246 | if (lease->router_size <= 0) | |
247 | return -ENODATA; | |
248 | ||
249 | *addr = lease->router; | |
250 | return (int) lease->router_size; | |
251 | } | |
252 | ||
253 | int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr) { | |
254 | assert_return(lease, -EINVAL); | |
255 | assert_return(addr, -EINVAL); | |
256 | ||
257 | if (!lease->have_subnet_mask) | |
258 | return -ENODATA; | |
259 | ||
260 | addr->s_addr = lease->subnet_mask; | |
261 | return 0; | |
262 | } | |
263 | ||
264 | int sd_dhcp_lease_get_prefix(sd_dhcp_lease *lease, struct in_addr *ret_prefix, uint8_t *ret_prefixlen) { | |
265 | struct in_addr address, netmask; | |
266 | uint8_t prefixlen; | |
267 | int r; | |
268 | ||
269 | assert_return(lease, -EINVAL); | |
270 | ||
271 | r = sd_dhcp_lease_get_address(lease, &address); | |
272 | if (r < 0) | |
273 | return r; | |
274 | ||
275 | r = sd_dhcp_lease_get_netmask(lease, &netmask); | |
276 | if (r < 0) | |
277 | return r; | |
278 | ||
279 | prefixlen = in4_addr_netmask_to_prefixlen(&netmask); | |
280 | ||
281 | r = in4_addr_mask(&address, prefixlen); | |
282 | if (r < 0) | |
283 | return r; | |
284 | ||
285 | if (ret_prefix) | |
286 | *ret_prefix = address; | |
287 | if (ret_prefixlen) | |
288 | *ret_prefixlen = prefixlen; | |
289 | return 0; | |
290 | } | |
291 | ||
292 | int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr) { | |
293 | assert_return(lease, -EINVAL); | |
294 | assert_return(addr, -EINVAL); | |
295 | ||
296 | if (lease->server_address == 0) | |
297 | return -ENODATA; | |
298 | ||
299 | addr->s_addr = lease->server_address; | |
300 | return 0; | |
301 | } | |
302 | ||
303 | int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr) { | |
304 | assert_return(lease, -EINVAL); | |
305 | assert_return(addr, -EINVAL); | |
306 | ||
307 | if (lease->next_server == 0) | |
308 | return -ENODATA; | |
309 | ||
310 | addr->s_addr = lease->next_server; | |
311 | return 0; | |
312 | } | |
313 | ||
314 | /* | |
315 | * The returned routes array must be freed by the caller. | |
316 | * Route objects have the same lifetime of the lease and must not be freed. | |
317 | */ | |
318 | static int dhcp_lease_get_routes(sd_dhcp_route *routes, size_t n_routes, sd_dhcp_route ***ret) { | |
319 | assert(routes || n_routes == 0); | |
320 | ||
321 | if (n_routes <= 0) | |
322 | return -ENODATA; | |
323 | ||
324 | if (ret) { | |
325 | sd_dhcp_route **buf; | |
326 | ||
327 | buf = new(sd_dhcp_route*, n_routes); | |
328 | if (!buf) | |
329 | return -ENOMEM; | |
330 | ||
331 | for (size_t i = 0; i < n_routes; i++) | |
332 | buf[i] = &routes[i]; | |
333 | ||
334 | *ret = buf; | |
335 | } | |
336 | ||
337 | return (int) n_routes; | |
338 | } | |
339 | ||
340 | int sd_dhcp_lease_get_static_routes(sd_dhcp_lease *lease, sd_dhcp_route ***ret) { | |
341 | assert_return(lease, -EINVAL); | |
342 | ||
343 | return dhcp_lease_get_routes(lease->static_routes, lease->n_static_routes, ret); | |
344 | } | |
345 | ||
346 | int sd_dhcp_lease_get_classless_routes(sd_dhcp_lease *lease, sd_dhcp_route ***ret) { | |
347 | assert_return(lease, -EINVAL); | |
348 | ||
349 | return dhcp_lease_get_routes(lease->classless_routes, lease->n_classless_routes, ret); | |
350 | } | |
351 | ||
352 | int sd_dhcp_lease_get_search_domains(sd_dhcp_lease *lease, char ***domains) { | |
353 | size_t r; | |
354 | ||
355 | assert_return(lease, -EINVAL); | |
356 | assert_return(domains, -EINVAL); | |
357 | ||
358 | r = strv_length(lease->search_domains); | |
359 | if (r > 0) { | |
360 | *domains = lease->search_domains; | |
361 | return (int) r; | |
362 | } | |
363 | ||
364 | return -ENODATA; | |
365 | } | |
366 | ||
367 | int sd_dhcp_lease_get_6rd( | |
368 | sd_dhcp_lease *lease, | |
369 | uint8_t *ret_ipv4masklen, | |
370 | uint8_t *ret_prefixlen, | |
371 | struct in6_addr *ret_prefix, | |
372 | const struct in_addr **ret_br_addresses, | |
373 | size_t *ret_n_br_addresses) { | |
374 | ||
375 | assert_return(lease, -EINVAL); | |
376 | ||
377 | if (lease->sixrd_n_br_addresses <= 0) | |
378 | return -ENODATA; | |
379 | ||
380 | if (ret_ipv4masklen) | |
381 | *ret_ipv4masklen = lease->sixrd_ipv4masklen; | |
382 | if (ret_prefixlen) | |
383 | *ret_prefixlen = lease->sixrd_prefixlen; | |
384 | if (ret_prefix) | |
385 | *ret_prefix = lease->sixrd_prefix; | |
386 | if (ret_br_addresses) | |
387 | *ret_br_addresses = lease->sixrd_br_addresses; | |
388 | if (ret_n_br_addresses) | |
389 | *ret_n_br_addresses = lease->sixrd_n_br_addresses; | |
390 | ||
391 | return 0; | |
392 | } | |
393 | ||
394 | int sd_dhcp_lease_has_6rd(sd_dhcp_lease *lease) { | |
395 | return lease && lease->sixrd_n_br_addresses > 0; | |
396 | } | |
397 | ||
398 | int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const void **data, size_t *data_len) { | |
399 | assert_return(lease, -EINVAL); | |
400 | assert_return(data, -EINVAL); | |
401 | assert_return(data_len, -EINVAL); | |
402 | ||
403 | if (lease->vendor_specific_len <= 0) | |
404 | return -ENODATA; | |
405 | ||
406 | *data = lease->vendor_specific; | |
407 | *data_len = lease->vendor_specific_len; | |
408 | return 0; | |
409 | } | |
410 | ||
411 | static sd_dhcp_lease *dhcp_lease_free(sd_dhcp_lease *lease) { | |
412 | struct sd_dhcp_raw_option *option; | |
413 | ||
414 | assert(lease); | |
415 | ||
416 | while ((option = LIST_POP(options, lease->private_options))) { | |
417 | free(option->data); | |
418 | free(option); | |
419 | } | |
420 | ||
421 | free(lease->root_path); | |
422 | free(lease->router); | |
423 | free(lease->timezone); | |
424 | free(lease->hostname); | |
425 | free(lease->domainname); | |
426 | free(lease->captive_portal); | |
427 | ||
428 | for (sd_dhcp_lease_server_type_t i = 0; i < _SD_DHCP_LEASE_SERVER_TYPE_MAX; i++) | |
429 | free(lease->servers[i].addr); | |
430 | ||
431 | dns_resolver_done_many(lease->dnr, lease->n_dnr); | |
432 | free(lease->static_routes); | |
433 | free(lease->classless_routes); | |
434 | free(lease->vendor_specific); | |
435 | strv_free(lease->search_domains); | |
436 | free(lease->sixrd_br_addresses); | |
437 | return mfree(lease); | |
438 | } | |
439 | ||
440 | DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_lease, sd_dhcp_lease, dhcp_lease_free); | |
441 | ||
442 | static int lease_parse_be32_seconds(const uint8_t *option, size_t len, bool max_as_infinity, usec_t *ret) { | |
443 | assert(option); | |
444 | assert(ret); | |
445 | ||
446 | if (len != 4) | |
447 | return -EINVAL; | |
448 | ||
449 | *ret = unaligned_be32_sec_to_usec(option, max_as_infinity); | |
450 | return 0; | |
451 | } | |
452 | ||
453 | static int lease_parse_u16(const uint8_t *option, size_t len, uint16_t *ret, uint16_t min) { | |
454 | assert(option); | |
455 | assert(ret); | |
456 | ||
457 | if (len != 2) | |
458 | return -EINVAL; | |
459 | ||
460 | *ret = unaligned_read_be16((be16_t*) option); | |
461 | if (*ret < min) | |
462 | *ret = min; | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
467 | static int lease_parse_be32(const uint8_t *option, size_t len, be32_t *ret) { | |
468 | assert(option); | |
469 | assert(ret); | |
470 | ||
471 | if (len != 4) | |
472 | return -EINVAL; | |
473 | ||
474 | memcpy(ret, option, 4); | |
475 | return 0; | |
476 | } | |
477 | ||
478 | static int lease_parse_domain(const uint8_t *option, size_t len, char **ret) { | |
479 | _cleanup_free_ char *name = NULL, *normalized = NULL; | |
480 | int r; | |
481 | ||
482 | assert(option); | |
483 | assert(ret); | |
484 | ||
485 | r = dhcp_option_parse_string(option, len, &name); | |
486 | if (r < 0) | |
487 | return r; | |
488 | if (!name) { | |
489 | *ret = mfree(*ret); | |
490 | return 0; | |
491 | } | |
492 | ||
493 | r = dns_name_normalize(name, 0, &normalized); | |
494 | if (r < 0) | |
495 | return r; | |
496 | ||
497 | if (is_localhost(normalized)) | |
498 | return -EINVAL; | |
499 | ||
500 | if (dns_name_is_root(normalized)) | |
501 | return -EINVAL; | |
502 | ||
503 | free_and_replace(*ret, normalized); | |
504 | ||
505 | return 0; | |
506 | } | |
507 | ||
508 | static int lease_parse_captive_portal(const uint8_t *option, size_t len, char **ret) { | |
509 | _cleanup_free_ char *uri = NULL; | |
510 | int r; | |
511 | ||
512 | assert(option); | |
513 | assert(ret); | |
514 | ||
515 | r = dhcp_option_parse_string(option, len, &uri); | |
516 | if (r < 0) | |
517 | return r; | |
518 | if (uri && !in_charset(uri, URI_VALID)) | |
519 | return -EINVAL; | |
520 | ||
521 | return free_and_replace(*ret, uri); | |
522 | } | |
523 | ||
524 | static int lease_parse_in_addrs(const uint8_t *option, size_t len, struct in_addr **ret, size_t *n_ret) { | |
525 | assert(option || len == 0); | |
526 | assert(ret); | |
527 | assert(n_ret); | |
528 | ||
529 | if (len <= 0) { | |
530 | *ret = mfree(*ret); | |
531 | *n_ret = 0; | |
532 | } else { | |
533 | size_t n_addresses; | |
534 | struct in_addr *addresses; | |
535 | ||
536 | if (len % 4 != 0) | |
537 | return -EINVAL; | |
538 | ||
539 | n_addresses = len / 4; | |
540 | ||
541 | addresses = newdup(struct in_addr, option, n_addresses); | |
542 | if (!addresses) | |
543 | return -ENOMEM; | |
544 | ||
545 | free_and_replace(*ret, addresses); | |
546 | *n_ret = n_addresses; | |
547 | } | |
548 | ||
549 | return 0; | |
550 | } | |
551 | ||
552 | static int lease_parse_sip_server(const uint8_t *option, size_t len, struct in_addr **ret, size_t *n_ret) { | |
553 | assert(option || len == 0); | |
554 | assert(ret); | |
555 | assert(n_ret); | |
556 | ||
557 | if (len <= 0) | |
558 | return -EINVAL; | |
559 | ||
560 | /* The SIP record is like the other, regular server records, but prefixed with a single "encoding" | |
561 | * byte that is either 0 or 1. We only support it to be 1 for now. Let's drop it and parse it like | |
562 | * the other fields */ | |
563 | ||
564 | if (option[0] != 1) { /* We only support IP address encoding for now */ | |
565 | *ret = mfree(*ret); | |
566 | *n_ret = 0; | |
567 | return 0; | |
568 | } | |
569 | ||
570 | return lease_parse_in_addrs(option + 1, len - 1, ret, n_ret); | |
571 | } | |
572 | ||
573 | static int lease_parse_dns_name(const uint8_t *optval, size_t optlen, char **ret) { | |
574 | _cleanup_free_ char *name = NULL; | |
575 | int r; | |
576 | ||
577 | assert(optval); | |
578 | assert(ret); | |
579 | ||
580 | r = dns_name_from_wire_format(&optval, &optlen, &name); | |
581 | if (r < 0) | |
582 | return r; | |
583 | if (r == 0 || optlen != 0) | |
584 | return -EBADMSG; | |
585 | ||
586 | *ret = TAKE_PTR(name); | |
587 | return r; | |
588 | } | |
589 | ||
590 | static int lease_parse_dnr(const uint8_t *option, size_t len, sd_dns_resolver **ret_dnr, size_t *ret_n_dnr) { | |
591 | int r; | |
592 | sd_dns_resolver *res_list = NULL; | |
593 | size_t n_resolvers = 0; | |
594 | CLEANUP_ARRAY(res_list, n_resolvers, dns_resolver_done_many); | |
595 | ||
596 | assert(option || len == 0); | |
597 | assert(ret_dnr); | |
598 | ||
599 | _cleanup_(sd_dns_resolver_done) sd_dns_resolver res = {}; | |
600 | ||
601 | size_t offset = 0; | |
602 | while (offset < len) { | |
603 | /* Instance Data length */ | |
604 | if (offset + 2 > len) | |
605 | return -EBADMSG; | |
606 | size_t ilen = unaligned_read_be16(option + offset); | |
607 | if (offset + ilen + 2 > len) | |
608 | return -EBADMSG; | |
609 | offset += 2; | |
610 | size_t iend = offset + ilen; | |
611 | ||
612 | /* priority */ | |
613 | if (offset + 2 > len) | |
614 | return -EBADMSG; | |
615 | res.priority = unaligned_read_be16(option + offset); | |
616 | offset += 2; | |
617 | ||
618 | /* Authenticated Domain Name */ | |
619 | if (offset + 1 > len) | |
620 | return -EBADMSG; | |
621 | ilen = option[offset++]; | |
622 | if (offset + ilen > iend) | |
623 | return -EBADMSG; | |
624 | ||
625 | r = lease_parse_dns_name(option + offset, ilen, &res.auth_name); | |
626 | if (r < 0) | |
627 | return r; | |
628 | r = dns_name_is_valid_ldh(res.auth_name); | |
629 | if (r < 0) | |
630 | return r; | |
631 | if (!r) | |
632 | return -EBADMSG; | |
633 | if (dns_name_is_root(res.auth_name)) | |
634 | return -EBADMSG; | |
635 | offset += ilen; | |
636 | ||
637 | /* RFC9463 § 3.1.6: In ADN-only mode, server omits everything after the ADN. | |
638 | * We don't support these, but they are not invalid. */ | |
639 | if (offset == iend) { | |
640 | log_debug("Received ADN-only DNRv4 option, ignoring."); | |
641 | sd_dns_resolver_done(&res); | |
642 | continue; | |
643 | } | |
644 | ||
645 | /* IPv4 addrs */ | |
646 | if (offset + 1 > len) | |
647 | return -EBADMSG; | |
648 | ilen = option[offset++]; | |
649 | if (offset + ilen > iend) | |
650 | return -EBADMSG; | |
651 | ||
652 | size_t n_addrs; | |
653 | _cleanup_free_ struct in_addr *addrs = NULL; | |
654 | r = lease_parse_in_addrs(option + offset, ilen, &addrs, &n_addrs); | |
655 | if (r < 0) | |
656 | return r; | |
657 | offset += ilen; | |
658 | ||
659 | /* RFC9463 § 3.1.8: option MUST include at least one valid IP addr */ | |
660 | if (!n_addrs) | |
661 | return -EBADMSG; | |
662 | ||
663 | res.addrs = new(union in_addr_union, n_addrs); | |
664 | if (!res.addrs) | |
665 | return -ENOMEM; | |
666 | for (size_t i = 0; i < n_addrs; i++) { | |
667 | union in_addr_union addr = {.in = addrs[i]}; | |
668 | /* RFC9463 § 5.2 client MUST discard multicast and host loopback addresses */ | |
669 | if (in_addr_is_multicast(AF_INET, &addr) || | |
670 | in_addr_is_localhost(AF_INET, &addr)) | |
671 | return -EBADMSG; | |
672 | res.addrs[i] = addr; | |
673 | } | |
674 | res.n_addrs = n_addrs; | |
675 | res.family = AF_INET; | |
676 | ||
677 | /* service params */ | |
678 | r = dnr_parse_svc_params(option + offset, iend-offset, &res); | |
679 | if (r < 0) | |
680 | return r; | |
681 | if (r == 0) { | |
682 | /* We can't use this record, but it was not invalid. */ | |
683 | log_debug("Received DNRv4 option with unsupported SvcParams, ignoring."); | |
684 | sd_dns_resolver_done(&res); | |
685 | continue; | |
686 | } | |
687 | offset = iend; | |
688 | ||
689 | /* Append the latest resolver */ | |
690 | if (!GREEDY_REALLOC0(res_list, n_resolvers+1)) | |
691 | return -ENOMEM; | |
692 | ||
693 | res_list[n_resolvers++] = TAKE_STRUCT(res); | |
694 | } | |
695 | ||
696 | typesafe_qsort(*ret_dnr, *ret_n_dnr, dns_resolver_prio_compare); | |
697 | ||
698 | dns_resolver_done_many(*ret_dnr, *ret_n_dnr); | |
699 | *ret_dnr = TAKE_PTR(res_list); | |
700 | *ret_n_dnr = n_resolvers; | |
701 | ||
702 | return n_resolvers; | |
703 | } | |
704 | ||
705 | static int lease_parse_static_routes(sd_dhcp_lease *lease, const uint8_t *option, size_t len) { | |
706 | int r; | |
707 | ||
708 | assert(lease); | |
709 | assert(option || len <= 0); | |
710 | ||
711 | if (len % 8 != 0) | |
712 | return -EINVAL; | |
713 | ||
714 | while (len >= 8) { | |
715 | struct in_addr dst, gw; | |
716 | uint8_t prefixlen; | |
717 | ||
718 | assert_se(lease_parse_be32(option, 4, &dst.s_addr) >= 0); | |
719 | option += 4; | |
720 | ||
721 | assert_se(lease_parse_be32(option, 4, &gw.s_addr) >= 0); | |
722 | option += 4; | |
723 | ||
724 | len -= 8; | |
725 | ||
726 | r = in4_addr_default_prefixlen(&dst, &prefixlen); | |
727 | if (r < 0) { | |
728 | log_debug("sd-dhcp-lease: cannot determine class of received static route, ignoring."); | |
729 | continue; | |
730 | } | |
731 | ||
732 | (void) in4_addr_mask(&dst, prefixlen); | |
733 | ||
734 | if (!GREEDY_REALLOC(lease->static_routes, lease->n_static_routes + 1)) | |
735 | return -ENOMEM; | |
736 | ||
737 | lease->static_routes[lease->n_static_routes++] = (struct sd_dhcp_route) { | |
738 | .dst_addr = dst, | |
739 | .gw_addr = gw, | |
740 | .dst_prefixlen = prefixlen, | |
741 | }; | |
742 | } | |
743 | ||
744 | return 0; | |
745 | } | |
746 | ||
747 | /* parses RFC3442 Classless Static Route Option */ | |
748 | static int lease_parse_classless_routes(sd_dhcp_lease *lease, const uint8_t *option, size_t len) { | |
749 | assert(lease); | |
750 | assert(option || len <= 0); | |
751 | ||
752 | /* option format: (subnet-mask-width significant-subnet-octets gateway-ip) */ | |
753 | ||
754 | while (len > 0) { | |
755 | uint8_t prefixlen, dst_octets; | |
756 | struct in_addr dst = {}, gw; | |
757 | ||
758 | prefixlen = *option; | |
759 | option++; | |
760 | len--; | |
761 | ||
762 | dst_octets = DIV_ROUND_UP(prefixlen, 8); | |
763 | ||
764 | /* can't have more than 4 octets in IPv4 */ | |
765 | if (dst_octets > 4 || len < dst_octets) | |
766 | return -EINVAL; | |
767 | ||
768 | memcpy(&dst, option, dst_octets); | |
769 | option += dst_octets; | |
770 | len -= dst_octets; | |
771 | ||
772 | if (len < 4) | |
773 | return -EINVAL; | |
774 | ||
775 | assert_se(lease_parse_be32(option, 4, &gw.s_addr) >= 0); | |
776 | option += 4; | |
777 | len -= 4; | |
778 | ||
779 | if (!GREEDY_REALLOC(lease->classless_routes, lease->n_classless_routes + 1)) | |
780 | return -ENOMEM; | |
781 | ||
782 | lease->classless_routes[lease->n_classless_routes++] = (struct sd_dhcp_route) { | |
783 | .dst_addr = dst, | |
784 | .gw_addr = gw, | |
785 | .dst_prefixlen = prefixlen, | |
786 | }; | |
787 | } | |
788 | ||
789 | return 0; | |
790 | } | |
791 | ||
792 | static int lease_parse_6rd(sd_dhcp_lease *lease, const uint8_t *option, size_t len) { | |
793 | uint8_t ipv4masklen, prefixlen; | |
794 | struct in6_addr prefix; | |
795 | _cleanup_free_ struct in_addr *br_addresses = NULL; | |
796 | size_t n_br_addresses; | |
797 | ||
798 | assert(lease); | |
799 | assert(option); | |
800 | ||
801 | /* See RFC 5969 Section 7.1.1 */ | |
802 | ||
803 | if (lease->sixrd_n_br_addresses > 0) | |
804 | /* Multiple 6rd option?? */ | |
805 | return -EINVAL; | |
806 | ||
807 | /* option-length: The length of the DHCP option in octets (22 octets with one BR IPv4 address). */ | |
808 | if (len < 2 + sizeof(struct in6_addr) + sizeof(struct in_addr) || | |
809 | (len - 2 - sizeof(struct in6_addr)) % sizeof(struct in_addr) != 0) | |
810 | return -EINVAL; | |
811 | ||
812 | /* IPv4MaskLen: The number of high-order bits that are identical across all CE IPv4 addresses | |
813 | * within a given 6rd domain. This may be any value between 0 and 32. Any value | |
814 | * greater than 32 is invalid. */ | |
815 | ipv4masklen = option[0]; | |
816 | if (ipv4masklen > 32) | |
817 | return -EINVAL; | |
818 | ||
819 | /* 6rdPrefixLen: The IPv6 prefix length of the SP's 6rd IPv6 prefix in number of bits. For the | |
820 | * purpose of bounds checking by DHCP option processing, the sum of | |
821 | * (32 - IPv4MaskLen) + 6rdPrefixLen MUST be less than or equal to 128. */ | |
822 | prefixlen = option[1]; | |
823 | if (32 - ipv4masklen + prefixlen > 128) | |
824 | return -EINVAL; | |
825 | ||
826 | /* 6rdPrefix: The service provider's 6rd IPv6 prefix represented as a 16-octet IPv6 address. | |
827 | * The bits in the prefix after the 6rdPrefixlen number of bits are reserved and | |
828 | * MUST be initialized to zero by the sender and ignored by the receiver. */ | |
829 | memcpy(&prefix, option + 2, sizeof(struct in6_addr)); | |
830 | (void) in6_addr_mask(&prefix, prefixlen); | |
831 | ||
832 | /* 6rdBRIPv4Address: One or more IPv4 addresses of the 6rd Border Relays for a given 6rd domain. */ | |
833 | n_br_addresses = (len - 2 - sizeof(struct in6_addr)) / sizeof(struct in_addr); | |
834 | br_addresses = newdup(struct in_addr, option + 2 + sizeof(struct in6_addr), n_br_addresses); | |
835 | if (!br_addresses) | |
836 | return -ENOMEM; | |
837 | ||
838 | lease->sixrd_ipv4masklen = ipv4masklen; | |
839 | lease->sixrd_prefixlen = prefixlen; | |
840 | lease->sixrd_prefix = prefix; | |
841 | lease->sixrd_br_addresses = TAKE_PTR(br_addresses); | |
842 | lease->sixrd_n_br_addresses = n_br_addresses; | |
843 | ||
844 | return 0; | |
845 | } | |
846 | ||
847 | int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void *userdata) { | |
848 | sd_dhcp_lease *lease = ASSERT_PTR(userdata); | |
849 | int r; | |
850 | ||
851 | switch (code) { | |
852 | ||
853 | case SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME: | |
854 | r = lease_parse_be32_seconds(option, len, /* max_as_infinity = */ true, &lease->lifetime); | |
855 | if (r < 0) | |
856 | log_debug_errno(r, "Failed to parse lease time, ignoring: %m"); | |
857 | ||
858 | break; | |
859 | ||
860 | case SD_DHCP_OPTION_SERVER_IDENTIFIER: | |
861 | r = lease_parse_be32(option, len, &lease->server_address); | |
862 | if (r < 0) | |
863 | log_debug_errno(r, "Failed to parse server identifier, ignoring: %m"); | |
864 | ||
865 | break; | |
866 | ||
867 | case SD_DHCP_OPTION_SUBNET_MASK: | |
868 | r = lease_parse_be32(option, len, &lease->subnet_mask); | |
869 | if (r < 0) | |
870 | log_debug_errno(r, "Failed to parse subnet mask, ignoring: %m"); | |
871 | else | |
872 | lease->have_subnet_mask = true; | |
873 | break; | |
874 | ||
875 | case SD_DHCP_OPTION_BROADCAST: | |
876 | r = lease_parse_be32(option, len, &lease->broadcast); | |
877 | if (r < 0) | |
878 | log_debug_errno(r, "Failed to parse broadcast address, ignoring: %m"); | |
879 | else | |
880 | lease->have_broadcast = true; | |
881 | break; | |
882 | ||
883 | case SD_DHCP_OPTION_ROUTER: | |
884 | r = lease_parse_in_addrs(option, len, &lease->router, &lease->router_size); | |
885 | if (r < 0) | |
886 | log_debug_errno(r, "Failed to parse router addresses, ignoring: %m"); | |
887 | break; | |
888 | ||
889 | case SD_DHCP_OPTION_RAPID_COMMIT: | |
890 | if (len > 0) | |
891 | log_debug("Invalid DHCP Rapid Commit option, ignoring."); | |
892 | lease->rapid_commit = true; | |
893 | break; | |
894 | ||
895 | case SD_DHCP_OPTION_DOMAIN_NAME_SERVER: | |
896 | r = lease_parse_in_addrs(option, len, &lease->servers[SD_DHCP_LEASE_DNS].addr, &lease->servers[SD_DHCP_LEASE_DNS].size); | |
897 | if (r < 0) | |
898 | log_debug_errno(r, "Failed to parse DNS server, ignoring: %m"); | |
899 | break; | |
900 | ||
901 | case SD_DHCP_OPTION_NTP_SERVER: | |
902 | r = lease_parse_in_addrs(option, len, &lease->servers[SD_DHCP_LEASE_NTP].addr, &lease->servers[SD_DHCP_LEASE_NTP].size); | |
903 | if (r < 0) | |
904 | log_debug_errno(r, "Failed to parse NTP server, ignoring: %m"); | |
905 | break; | |
906 | ||
907 | case SD_DHCP_OPTION_SIP_SERVER: | |
908 | r = lease_parse_sip_server(option, len, &lease->servers[SD_DHCP_LEASE_SIP].addr, &lease->servers[SD_DHCP_LEASE_SIP].size); | |
909 | if (r < 0) | |
910 | log_debug_errno(r, "Failed to parse SIP server, ignoring: %m"); | |
911 | break; | |
912 | ||
913 | case SD_DHCP_OPTION_POP3_SERVER: | |
914 | r = lease_parse_in_addrs(option, len, &lease->servers[SD_DHCP_LEASE_POP3].addr, &lease->servers[SD_DHCP_LEASE_POP3].size); | |
915 | if (r < 0) | |
916 | log_debug_errno(r, "Failed to parse POP3 server, ignoring: %m"); | |
917 | break; | |
918 | ||
919 | case SD_DHCP_OPTION_SMTP_SERVER: | |
920 | r = lease_parse_in_addrs(option, len, &lease->servers[SD_DHCP_LEASE_SMTP].addr, &lease->servers[SD_DHCP_LEASE_SMTP].size); | |
921 | if (r < 0) | |
922 | log_debug_errno(r, "Failed to parse SMTP server, ignoring: %m"); | |
923 | break; | |
924 | ||
925 | case SD_DHCP_OPTION_LPR_SERVER: | |
926 | r = lease_parse_in_addrs(option, len, &lease->servers[SD_DHCP_LEASE_LPR].addr, &lease->servers[SD_DHCP_LEASE_LPR].size); | |
927 | if (r < 0) | |
928 | log_debug_errno(r, "Failed to parse LPR server, ignoring: %m"); | |
929 | break; | |
930 | ||
931 | case SD_DHCP_OPTION_DHCP_CAPTIVE_PORTAL: | |
932 | r = lease_parse_captive_portal(option, len, &lease->captive_portal); | |
933 | if (r < 0) | |
934 | log_debug_errno(r, "Failed to parse captive portal, ignoring: %m"); | |
935 | break; | |
936 | ||
937 | case SD_DHCP_OPTION_STATIC_ROUTE: | |
938 | r = lease_parse_static_routes(lease, option, len); | |
939 | if (r < 0) | |
940 | log_debug_errno(r, "Failed to parse static routes, ignoring: %m"); | |
941 | break; | |
942 | ||
943 | case SD_DHCP_OPTION_MTU_INTERFACE: | |
944 | r = lease_parse_u16(option, len, &lease->mtu, 68); | |
945 | if (r < 0) | |
946 | log_debug_errno(r, "Failed to parse MTU, ignoring: %m"); | |
947 | if (lease->mtu < DHCP_MIN_PACKET_SIZE) { | |
948 | log_debug("MTU value of %" PRIu16 " too small. Using default MTU value of %d instead.", lease->mtu, DHCP_MIN_PACKET_SIZE); | |
949 | lease->mtu = DHCP_MIN_PACKET_SIZE; | |
950 | } | |
951 | ||
952 | break; | |
953 | ||
954 | case SD_DHCP_OPTION_DOMAIN_NAME: | |
955 | r = lease_parse_domain(option, len, &lease->domainname); | |
956 | if (r < 0) { | |
957 | log_debug_errno(r, "Failed to parse domain name, ignoring: %m"); | |
958 | return 0; | |
959 | } | |
960 | ||
961 | break; | |
962 | ||
963 | case SD_DHCP_OPTION_DOMAIN_SEARCH: | |
964 | r = dhcp_lease_parse_search_domains(option, len, &lease->search_domains); | |
965 | if (r < 0) | |
966 | log_debug_errno(r, "Failed to parse Domain Search List, ignoring: %m"); | |
967 | break; | |
968 | ||
969 | case SD_DHCP_OPTION_HOST_NAME: | |
970 | r = lease_parse_domain(option, len, &lease->hostname); | |
971 | if (r < 0) { | |
972 | log_debug_errno(r, "Failed to parse hostname, ignoring: %m"); | |
973 | return 0; | |
974 | } | |
975 | ||
976 | break; | |
977 | ||
978 | case SD_DHCP_OPTION_ROOT_PATH: { | |
979 | _cleanup_free_ char *p = NULL; | |
980 | ||
981 | r = dhcp_option_parse_string(option, len, &p); | |
982 | if (r < 0) | |
983 | log_debug_errno(r, "Failed to parse root path, ignoring: %m"); | |
984 | ||
985 | free_and_replace(lease->root_path, p); | |
986 | break; | |
987 | } | |
988 | case SD_DHCP_OPTION_RENEWAL_TIME: | |
989 | r = lease_parse_be32_seconds(option, len, /* max_as_infinity = */ true, &lease->t1); | |
990 | if (r < 0) | |
991 | log_debug_errno(r, "Failed to parse T1 time, ignoring: %m"); | |
992 | break; | |
993 | ||
994 | case SD_DHCP_OPTION_REBINDING_TIME: | |
995 | r = lease_parse_be32_seconds(option, len, /* max_as_infinity = */ true, &lease->t2); | |
996 | if (r < 0) | |
997 | log_debug_errno(r, "Failed to parse T2 time, ignoring: %m"); | |
998 | break; | |
999 | ||
1000 | case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE: | |
1001 | r = lease_parse_classless_routes(lease, option, len); | |
1002 | if (r < 0) | |
1003 | log_debug_errno(r, "Failed to parse classless routes, ignoring: %m"); | |
1004 | break; | |
1005 | ||
1006 | case SD_DHCP_OPTION_TZDB_TIMEZONE: { | |
1007 | _cleanup_free_ char *tz = NULL; | |
1008 | ||
1009 | r = dhcp_option_parse_string(option, len, &tz); | |
1010 | if (r < 0) { | |
1011 | log_debug_errno(r, "Failed to parse timezone option, ignoring: %m"); | |
1012 | return 0; | |
1013 | } | |
1014 | ||
1015 | if (!timezone_is_valid(tz, LOG_DEBUG)) { | |
1016 | log_debug("Timezone is not valid, ignoring."); | |
1017 | return 0; | |
1018 | } | |
1019 | ||
1020 | free_and_replace(lease->timezone, tz); | |
1021 | ||
1022 | break; | |
1023 | } | |
1024 | ||
1025 | case SD_DHCP_OPTION_V4_DNR: | |
1026 | r = lease_parse_dnr(option, len, &lease->dnr, &lease->n_dnr); | |
1027 | if (r < 0) { | |
1028 | log_debug_errno(r, "Failed to parse network-designated resolvers, ignoring: %m"); | |
1029 | return 0; | |
1030 | } | |
1031 | ||
1032 | break; | |
1033 | ||
1034 | case SD_DHCP_OPTION_VENDOR_SPECIFIC: | |
1035 | ||
1036 | if (len <= 0) | |
1037 | lease->vendor_specific = mfree(lease->vendor_specific); | |
1038 | else { | |
1039 | void *p; | |
1040 | ||
1041 | p = memdup(option, len); | |
1042 | if (!p) | |
1043 | return -ENOMEM; | |
1044 | ||
1045 | free_and_replace(lease->vendor_specific, p); | |
1046 | } | |
1047 | ||
1048 | lease->vendor_specific_len = len; | |
1049 | break; | |
1050 | ||
1051 | case SD_DHCP_OPTION_6RD: | |
1052 | r = lease_parse_6rd(lease, option, len); | |
1053 | if (r < 0) | |
1054 | log_debug_errno(r, "Failed to parse 6rd option, ignoring: %m"); | |
1055 | break; | |
1056 | ||
1057 | case SD_DHCP_OPTION_IPV6_ONLY_PREFERRED: | |
1058 | r = lease_parse_be32_seconds(option, len, /* max_as_infinity = */ false, &lease->ipv6_only_preferred_usec); | |
1059 | if (r < 0) | |
1060 | log_debug_errno(r, "Failed to parse IPv6 only preferred option, ignoring: %m"); | |
1061 | ||
1062 | else if (lease->ipv6_only_preferred_usec < MIN_V6ONLY_WAIT_USEC && | |
1063 | !network_test_mode_enabled()) | |
1064 | lease->ipv6_only_preferred_usec = MIN_V6ONLY_WAIT_USEC; | |
1065 | break; | |
1066 | ||
1067 | case SD_DHCP_OPTION_PRIVATE_BASE ... SD_DHCP_OPTION_PRIVATE_LAST: | |
1068 | r = dhcp_lease_insert_private_option(lease, code, option, len); | |
1069 | if (r < 0) | |
1070 | return r; | |
1071 | ||
1072 | break; | |
1073 | ||
1074 | default: | |
1075 | log_debug("Ignoring DHCP option %"PRIu8" while parsing.", code); | |
1076 | } | |
1077 | ||
1078 | return 0; | |
1079 | } | |
1080 | ||
1081 | /* Parses compressed domain names. */ | |
1082 | int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***domains) { | |
1083 | _cleanup_strv_free_ char **names = NULL; | |
1084 | size_t pos = 0, cnt = 0; | |
1085 | int r; | |
1086 | ||
1087 | assert(domains); | |
1088 | assert(option || len == 0); | |
1089 | ||
1090 | if (len == 0) | |
1091 | return -EBADMSG; | |
1092 | ||
1093 | while (pos < len) { | |
1094 | _cleanup_free_ char *name = NULL; | |
1095 | size_t n = 0; | |
1096 | size_t jump_barrier = pos, next_chunk = 0; | |
1097 | bool first = true; | |
1098 | ||
1099 | for (;;) { | |
1100 | uint8_t c; | |
1101 | c = option[pos++]; | |
1102 | ||
1103 | if (c == 0) { | |
1104 | /* End of name */ | |
1105 | break; | |
1106 | } else if (c <= 63) { | |
1107 | const char *label; | |
1108 | ||
1109 | /* Literal label */ | |
1110 | label = (const char*) (option + pos); | |
1111 | pos += c; | |
1112 | if (pos >= len) | |
1113 | return -EBADMSG; | |
1114 | ||
1115 | if (!GREEDY_REALLOC(name, n + !first + DNS_LABEL_ESCAPED_MAX)) | |
1116 | return -ENOMEM; | |
1117 | ||
1118 | if (first) | |
1119 | first = false; | |
1120 | else | |
1121 | name[n++] = '.'; | |
1122 | ||
1123 | r = dns_label_escape(label, c, name + n, DNS_LABEL_ESCAPED_MAX); | |
1124 | if (r < 0) | |
1125 | return r; | |
1126 | ||
1127 | n += r; | |
1128 | } else if (FLAGS_SET(c, 0xc0)) { | |
1129 | /* Pointer */ | |
1130 | ||
1131 | uint8_t d; | |
1132 | uint16_t ptr; | |
1133 | ||
1134 | if (pos >= len) | |
1135 | return -EBADMSG; | |
1136 | ||
1137 | d = option[pos++]; | |
1138 | ptr = (uint16_t) (c & ~0xc0) << 8 | (uint16_t) d; | |
1139 | ||
1140 | /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */ | |
1141 | if (ptr >= jump_barrier) | |
1142 | return -EBADMSG; | |
1143 | jump_barrier = ptr; | |
1144 | ||
1145 | /* Save current location so we don't end up re-parsing what's parsed so far. */ | |
1146 | if (next_chunk == 0) | |
1147 | next_chunk = pos; | |
1148 | ||
1149 | pos = ptr; | |
1150 | } else | |
1151 | return -EBADMSG; | |
1152 | } | |
1153 | ||
1154 | if (!GREEDY_REALLOC(name, n + 1)) | |
1155 | return -ENOMEM; | |
1156 | name[n] = 0; | |
1157 | ||
1158 | r = strv_extend(&names, name); | |
1159 | if (r < 0) | |
1160 | return r; | |
1161 | ||
1162 | cnt++; | |
1163 | ||
1164 | if (next_chunk != 0) | |
1165 | pos = next_chunk; | |
1166 | } | |
1167 | ||
1168 | strv_free_and_replace(*domains, names); | |
1169 | ||
1170 | return cnt; | |
1171 | } | |
1172 | ||
1173 | int dhcp_lease_insert_private_option(sd_dhcp_lease *lease, uint8_t tag, const void *data, uint8_t len) { | |
1174 | struct sd_dhcp_raw_option *option, *before = NULL; | |
1175 | ||
1176 | assert(lease); | |
1177 | ||
1178 | LIST_FOREACH(options, cur, lease->private_options) { | |
1179 | if (tag < cur->tag) { | |
1180 | before = cur; | |
1181 | break; | |
1182 | } | |
1183 | if (tag == cur->tag) { | |
1184 | log_debug("Ignoring duplicate option, tagged %i.", tag); | |
1185 | return 0; | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | option = new(struct sd_dhcp_raw_option, 1); | |
1190 | if (!option) | |
1191 | return -ENOMEM; | |
1192 | ||
1193 | option->tag = tag; | |
1194 | option->length = len; | |
1195 | option->data = memdup(data, len); | |
1196 | if (!option->data) { | |
1197 | free(option); | |
1198 | return -ENOMEM; | |
1199 | } | |
1200 | ||
1201 | LIST_INSERT_BEFORE(options, lease->private_options, before, option); | |
1202 | return 0; | |
1203 | } | |
1204 | ||
1205 | int dhcp_lease_new(sd_dhcp_lease **ret) { | |
1206 | sd_dhcp_lease *lease; | |
1207 | ||
1208 | lease = new0(sd_dhcp_lease, 1); | |
1209 | if (!lease) | |
1210 | return -ENOMEM; | |
1211 | ||
1212 | lease->n_ref = 1; | |
1213 | ||
1214 | *ret = lease; | |
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { | |
1219 | _cleanup_(unlink_and_freep) char *temp_path = NULL; | |
1220 | _cleanup_fclose_ FILE *f = NULL; | |
1221 | struct in_addr address; | |
1222 | const struct in_addr *addresses; | |
1223 | const void *data; | |
1224 | size_t data_len; | |
1225 | const char *string; | |
1226 | uint16_t mtu; | |
1227 | _cleanup_free_ sd_dhcp_route **routes = NULL; | |
1228 | char **search_domains; | |
1229 | usec_t t; | |
1230 | int r; | |
1231 | ||
1232 | assert(lease); | |
1233 | assert(lease_file); | |
1234 | ||
1235 | r = fopen_temporary(lease_file, &f, &temp_path); | |
1236 | if (r < 0) | |
1237 | return r; | |
1238 | ||
1239 | (void) fchmod(fileno(f), 0644); | |
1240 | ||
1241 | fprintf(f, | |
1242 | "# This is private data. Do not parse.\n"); | |
1243 | ||
1244 | r = sd_dhcp_lease_get_address(lease, &address); | |
1245 | if (r >= 0) | |
1246 | fprintf(f, "ADDRESS=%s\n", IN4_ADDR_TO_STRING(&address)); | |
1247 | ||
1248 | r = sd_dhcp_lease_get_netmask(lease, &address); | |
1249 | if (r >= 0) | |
1250 | fprintf(f, "NETMASK=%s\n", IN4_ADDR_TO_STRING(&address)); | |
1251 | ||
1252 | r = sd_dhcp_lease_get_router(lease, &addresses); | |
1253 | if (r > 0) { | |
1254 | fputs("ROUTER=", f); | |
1255 | serialize_in_addrs(f, addresses, r, NULL, NULL); | |
1256 | fputc('\n', f); | |
1257 | } | |
1258 | ||
1259 | r = sd_dhcp_lease_get_server_identifier(lease, &address); | |
1260 | if (r >= 0) | |
1261 | fprintf(f, "SERVER_ADDRESS=%s\n", IN4_ADDR_TO_STRING(&address)); | |
1262 | ||
1263 | r = sd_dhcp_lease_get_next_server(lease, &address); | |
1264 | if (r >= 0) | |
1265 | fprintf(f, "NEXT_SERVER=%s\n", IN4_ADDR_TO_STRING(&address)); | |
1266 | ||
1267 | r = sd_dhcp_lease_get_broadcast(lease, &address); | |
1268 | if (r >= 0) | |
1269 | fprintf(f, "BROADCAST=%s\n", IN4_ADDR_TO_STRING(&address)); | |
1270 | ||
1271 | r = sd_dhcp_lease_get_mtu(lease, &mtu); | |
1272 | if (r >= 0) | |
1273 | fprintf(f, "MTU=%" PRIu16 "\n", mtu); | |
1274 | ||
1275 | r = sd_dhcp_lease_get_t1(lease, &t); | |
1276 | if (r >= 0) | |
1277 | fprintf(f, "T1=%s\n", FORMAT_TIMESPAN(t, USEC_PER_SEC)); | |
1278 | ||
1279 | r = sd_dhcp_lease_get_t2(lease, &t); | |
1280 | if (r >= 0) | |
1281 | fprintf(f, "T2=%s\n", FORMAT_TIMESPAN(t, USEC_PER_SEC)); | |
1282 | ||
1283 | r = sd_dhcp_lease_get_lifetime(lease, &t); | |
1284 | if (r >= 0) | |
1285 | fprintf(f, "LIFETIME=%s\n", FORMAT_TIMESPAN(t, USEC_PER_SEC)); | |
1286 | ||
1287 | r = sd_dhcp_lease_get_dns(lease, &addresses); | |
1288 | if (r > 0) { | |
1289 | fputs("DNS=", f); | |
1290 | serialize_in_addrs(f, addresses, r, NULL, NULL); | |
1291 | fputc('\n', f); | |
1292 | } | |
1293 | ||
1294 | sd_dns_resolver *resolvers; | |
1295 | r = sd_dhcp_lease_get_dnr(lease, &resolvers); | |
1296 | if (r > 0) { | |
1297 | fputs("DNR=", f); | |
1298 | serialize_dnr(f, resolvers, r, NULL); | |
1299 | fputc('\n', f); | |
1300 | } | |
1301 | ||
1302 | r = sd_dhcp_lease_get_ntp(lease, &addresses); | |
1303 | if (r > 0) { | |
1304 | fputs("NTP=", f); | |
1305 | serialize_in_addrs(f, addresses, r, NULL, NULL); | |
1306 | fputc('\n', f); | |
1307 | } | |
1308 | ||
1309 | r = sd_dhcp_lease_get_sip(lease, &addresses); | |
1310 | if (r > 0) { | |
1311 | fputs("SIP=", f); | |
1312 | serialize_in_addrs(f, addresses, r, NULL, NULL); | |
1313 | fputc('\n', f); | |
1314 | } | |
1315 | ||
1316 | r = sd_dhcp_lease_get_domainname(lease, &string); | |
1317 | if (r >= 0) | |
1318 | fprintf(f, "DOMAINNAME=%s\n", string); | |
1319 | ||
1320 | r = sd_dhcp_lease_get_search_domains(lease, &search_domains); | |
1321 | if (r > 0) { | |
1322 | fputs("DOMAIN_SEARCH_LIST=", f); | |
1323 | fputstrv(f, search_domains, NULL, NULL); | |
1324 | fputc('\n', f); | |
1325 | } | |
1326 | ||
1327 | r = sd_dhcp_lease_get_hostname(lease, &string); | |
1328 | if (r >= 0) | |
1329 | fprintf(f, "HOSTNAME=%s\n", string); | |
1330 | ||
1331 | r = sd_dhcp_lease_get_root_path(lease, &string); | |
1332 | if (r >= 0) | |
1333 | fprintf(f, "ROOT_PATH=%s\n", string); | |
1334 | ||
1335 | r = sd_dhcp_lease_get_static_routes(lease, &routes); | |
1336 | if (r > 0) | |
1337 | serialize_dhcp_routes(f, "STATIC_ROUTES", routes, r); | |
1338 | ||
1339 | routes = mfree(routes); | |
1340 | r = sd_dhcp_lease_get_classless_routes(lease, &routes); | |
1341 | if (r > 0) | |
1342 | serialize_dhcp_routes(f, "CLASSLESS_ROUTES", routes, r); | |
1343 | ||
1344 | r = sd_dhcp_lease_get_timezone(lease, &string); | |
1345 | if (r >= 0) | |
1346 | fprintf(f, "TIMEZONE=%s\n", string); | |
1347 | ||
1348 | if (sd_dhcp_client_id_is_set(&lease->client_id)) { | |
1349 | _cleanup_free_ char *client_id_hex = NULL; | |
1350 | ||
1351 | client_id_hex = hexmem(lease->client_id.raw, lease->client_id.size); | |
1352 | if (!client_id_hex) | |
1353 | return -ENOMEM; | |
1354 | fprintf(f, "CLIENTID=%s\n", client_id_hex); | |
1355 | } | |
1356 | ||
1357 | r = sd_dhcp_lease_get_vendor_specific(lease, &data, &data_len); | |
1358 | if (r >= 0) { | |
1359 | _cleanup_free_ char *option_hex = NULL; | |
1360 | ||
1361 | option_hex = hexmem(data, data_len); | |
1362 | if (!option_hex) | |
1363 | return -ENOMEM; | |
1364 | fprintf(f, "VENDOR_SPECIFIC=%s\n", option_hex); | |
1365 | } | |
1366 | ||
1367 | LIST_FOREACH(options, option, lease->private_options) { | |
1368 | char key[STRLEN("OPTION_000")+1]; | |
1369 | ||
1370 | xsprintf(key, "OPTION_%" PRIu8, option->tag); | |
1371 | r = serialize_dhcp_option(f, key, option->data, option->length); | |
1372 | if (r < 0) | |
1373 | return r; | |
1374 | } | |
1375 | ||
1376 | r = fflush_and_check(f); | |
1377 | if (r < 0) | |
1378 | return r; | |
1379 | ||
1380 | r = conservative_rename(temp_path, lease_file); | |
1381 | if (r < 0) | |
1382 | return r; | |
1383 | ||
1384 | temp_path = mfree(temp_path); | |
1385 | ||
1386 | return 0; | |
1387 | } | |
1388 | ||
1389 | static char **private_options_free(char **options) { | |
1390 | if (!options) | |
1391 | return NULL; | |
1392 | ||
1393 | free_many_charp(options, SD_DHCP_OPTION_PRIVATE_LAST - SD_DHCP_OPTION_PRIVATE_BASE + 1); | |
1394 | ||
1395 | return mfree(options); | |
1396 | } | |
1397 | ||
1398 | DEFINE_TRIVIAL_CLEANUP_FUNC(char**, private_options_free); | |
1399 | ||
1400 | int dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) { | |
1401 | _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL; | |
1402 | _cleanup_free_ char | |
1403 | *address = NULL, | |
1404 | *router = NULL, | |
1405 | *netmask = NULL, | |
1406 | *server_address = NULL, | |
1407 | *next_server = NULL, | |
1408 | *broadcast = NULL, | |
1409 | *dns = NULL, | |
1410 | *dnr = NULL, | |
1411 | *ntp = NULL, | |
1412 | *sip = NULL, | |
1413 | *pop3 = NULL, | |
1414 | *smtp = NULL, | |
1415 | *lpr = NULL, | |
1416 | *mtu = NULL, | |
1417 | *static_routes = NULL, | |
1418 | *classless_routes = NULL, | |
1419 | *domains = NULL, | |
1420 | *client_id_hex = NULL, | |
1421 | *vendor_specific_hex = NULL, | |
1422 | *lifetime = NULL, | |
1423 | *t1 = NULL, | |
1424 | *t2 = NULL; | |
1425 | _cleanup_(private_options_freep) char **options = NULL; | |
1426 | ||
1427 | int r, i; | |
1428 | ||
1429 | assert(lease_file); | |
1430 | assert(ret); | |
1431 | ||
1432 | r = dhcp_lease_new(&lease); | |
1433 | if (r < 0) | |
1434 | return r; | |
1435 | ||
1436 | options = new0(char*, SD_DHCP_OPTION_PRIVATE_LAST - SD_DHCP_OPTION_PRIVATE_BASE + 1); | |
1437 | if (!options) | |
1438 | return -ENOMEM; | |
1439 | ||
1440 | r = parse_env_file(NULL, lease_file, | |
1441 | "ADDRESS", &address, | |
1442 | "ROUTER", &router, | |
1443 | "NETMASK", &netmask, | |
1444 | "SERVER_ADDRESS", &server_address, | |
1445 | "NEXT_SERVER", &next_server, | |
1446 | "BROADCAST", &broadcast, | |
1447 | "DNS", &dns, | |
1448 | "DNR", &dnr, | |
1449 | "NTP", &ntp, | |
1450 | "SIP", &sip, | |
1451 | "POP3", &pop3, | |
1452 | "SMTP", &smtp, | |
1453 | "LPR", &lpr, | |
1454 | "MTU", &mtu, | |
1455 | "DOMAINNAME", &lease->domainname, | |
1456 | "HOSTNAME", &lease->hostname, | |
1457 | "DOMAIN_SEARCH_LIST", &domains, | |
1458 | "ROOT_PATH", &lease->root_path, | |
1459 | "STATIC_ROUTES", &static_routes, | |
1460 | "CLASSLESS_ROUTES", &classless_routes, | |
1461 | "CLIENTID", &client_id_hex, | |
1462 | "TIMEZONE", &lease->timezone, | |
1463 | "VENDOR_SPECIFIC", &vendor_specific_hex, | |
1464 | "LIFETIME", &lifetime, | |
1465 | "T1", &t1, | |
1466 | "T2", &t2, | |
1467 | "OPTION_224", &options[0], | |
1468 | "OPTION_225", &options[1], | |
1469 | "OPTION_226", &options[2], | |
1470 | "OPTION_227", &options[3], | |
1471 | "OPTION_228", &options[4], | |
1472 | "OPTION_229", &options[5], | |
1473 | "OPTION_230", &options[6], | |
1474 | "OPTION_231", &options[7], | |
1475 | "OPTION_232", &options[8], | |
1476 | "OPTION_233", &options[9], | |
1477 | "OPTION_234", &options[10], | |
1478 | "OPTION_235", &options[11], | |
1479 | "OPTION_236", &options[12], | |
1480 | "OPTION_237", &options[13], | |
1481 | "OPTION_238", &options[14], | |
1482 | "OPTION_239", &options[15], | |
1483 | "OPTION_240", &options[16], | |
1484 | "OPTION_241", &options[17], | |
1485 | "OPTION_242", &options[18], | |
1486 | "OPTION_243", &options[19], | |
1487 | "OPTION_244", &options[20], | |
1488 | "OPTION_245", &options[21], | |
1489 | "OPTION_246", &options[22], | |
1490 | "OPTION_247", &options[23], | |
1491 | "OPTION_248", &options[24], | |
1492 | "OPTION_249", &options[25], | |
1493 | "OPTION_250", &options[26], | |
1494 | "OPTION_251", &options[27], | |
1495 | "OPTION_252", &options[28], | |
1496 | "OPTION_253", &options[29], | |
1497 | "OPTION_254", &options[30]); | |
1498 | if (r < 0) | |
1499 | return r; | |
1500 | ||
1501 | if (address) { | |
1502 | r = inet_pton(AF_INET, address, &lease->address); | |
1503 | if (r <= 0) | |
1504 | log_debug("Failed to parse address %s, ignoring.", address); | |
1505 | } | |
1506 | ||
1507 | if (router) { | |
1508 | r = deserialize_in_addrs(&lease->router, router); | |
1509 | if (r < 0) | |
1510 | log_debug_errno(r, "Failed to deserialize router addresses %s, ignoring: %m", router); | |
1511 | else | |
1512 | lease->router_size = r; | |
1513 | } | |
1514 | ||
1515 | if (netmask) { | |
1516 | r = inet_pton(AF_INET, netmask, &lease->subnet_mask); | |
1517 | if (r <= 0) | |
1518 | log_debug("Failed to parse netmask %s, ignoring.", netmask); | |
1519 | else | |
1520 | lease->have_subnet_mask = true; | |
1521 | } | |
1522 | ||
1523 | if (server_address) { | |
1524 | r = inet_pton(AF_INET, server_address, &lease->server_address); | |
1525 | if (r <= 0) | |
1526 | log_debug("Failed to parse server address %s, ignoring.", server_address); | |
1527 | } | |
1528 | ||
1529 | if (next_server) { | |
1530 | r = inet_pton(AF_INET, next_server, &lease->next_server); | |
1531 | if (r <= 0) | |
1532 | log_debug("Failed to parse next server %s, ignoring.", next_server); | |
1533 | } | |
1534 | ||
1535 | if (broadcast) { | |
1536 | r = inet_pton(AF_INET, broadcast, &lease->broadcast); | |
1537 | if (r <= 0) | |
1538 | log_debug("Failed to parse broadcast address %s, ignoring.", broadcast); | |
1539 | else | |
1540 | lease->have_broadcast = true; | |
1541 | } | |
1542 | ||
1543 | if (dns) { | |
1544 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_DNS].addr, dns); | |
1545 | if (r < 0) | |
1546 | log_debug_errno(r, "Failed to deserialize DNS servers %s, ignoring: %m", dns); | |
1547 | else | |
1548 | lease->servers[SD_DHCP_LEASE_DNS].size = r; | |
1549 | } | |
1550 | ||
1551 | if (dnr) { | |
1552 | r = deserialize_dnr(&lease->dnr, dnr); | |
1553 | if (r < 0) | |
1554 | log_debug_errno(r, "Failed to deserialize DNR servers %s, ignoring: %m", dnr); | |
1555 | else | |
1556 | lease->n_dnr = r; | |
1557 | } | |
1558 | ||
1559 | if (ntp) { | |
1560 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_NTP].addr, ntp); | |
1561 | if (r < 0) | |
1562 | log_debug_errno(r, "Failed to deserialize NTP servers %s, ignoring: %m", ntp); | |
1563 | else | |
1564 | lease->servers[SD_DHCP_LEASE_NTP].size = r; | |
1565 | } | |
1566 | ||
1567 | if (sip) { | |
1568 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_SIP].addr, sip); | |
1569 | if (r < 0) | |
1570 | log_debug_errno(r, "Failed to deserialize SIP servers %s, ignoring: %m", sip); | |
1571 | else | |
1572 | lease->servers[SD_DHCP_LEASE_SIP].size = r; | |
1573 | } | |
1574 | ||
1575 | if (pop3) { | |
1576 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_POP3].addr, pop3); | |
1577 | if (r < 0) | |
1578 | log_debug_errno(r, "Failed to deserialize POP3 server %s, ignoring: %m", pop3); | |
1579 | else | |
1580 | lease->servers[SD_DHCP_LEASE_POP3].size = r; | |
1581 | } | |
1582 | ||
1583 | if (smtp) { | |
1584 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_SMTP].addr, smtp); | |
1585 | if (r < 0) | |
1586 | log_debug_errno(r, "Failed to deserialize SMTP server %s, ignoring: %m", smtp); | |
1587 | else | |
1588 | lease->servers[SD_DHCP_LEASE_SMTP].size = r; | |
1589 | } | |
1590 | ||
1591 | if (lpr) { | |
1592 | r = deserialize_in_addrs(&lease->servers[SD_DHCP_LEASE_LPR].addr, lpr); | |
1593 | if (r < 0) | |
1594 | log_debug_errno(r, "Failed to deserialize LPR server %s, ignoring: %m", lpr); | |
1595 | else | |
1596 | lease->servers[SD_DHCP_LEASE_LPR].size = r; | |
1597 | } | |
1598 | ||
1599 | if (mtu) { | |
1600 | r = safe_atou16(mtu, &lease->mtu); | |
1601 | if (r < 0) | |
1602 | log_debug_errno(r, "Failed to parse MTU %s, ignoring: %m", mtu); | |
1603 | } | |
1604 | ||
1605 | if (domains) { | |
1606 | _cleanup_strv_free_ char **a = NULL; | |
1607 | a = strv_split(domains, " "); | |
1608 | if (!a) | |
1609 | return -ENOMEM; | |
1610 | ||
1611 | if (!strv_isempty(a)) | |
1612 | lease->search_domains = TAKE_PTR(a); | |
1613 | } | |
1614 | ||
1615 | if (static_routes) { | |
1616 | r = deserialize_dhcp_routes( | |
1617 | &lease->static_routes, | |
1618 | &lease->n_static_routes, | |
1619 | static_routes); | |
1620 | if (r < 0) | |
1621 | log_debug_errno(r, "Failed to parse DHCP static routes %s, ignoring: %m", static_routes); | |
1622 | } | |
1623 | ||
1624 | if (classless_routes) { | |
1625 | r = deserialize_dhcp_routes( | |
1626 | &lease->classless_routes, | |
1627 | &lease->n_classless_routes, | |
1628 | classless_routes); | |
1629 | if (r < 0) | |
1630 | log_debug_errno(r, "Failed to parse DHCP classless routes %s, ignoring: %m", classless_routes); | |
1631 | } | |
1632 | ||
1633 | if (lifetime) { | |
1634 | r = parse_sec(lifetime, &lease->lifetime); | |
1635 | if (r < 0) | |
1636 | log_debug_errno(r, "Failed to parse lifetime %s, ignoring: %m", lifetime); | |
1637 | } | |
1638 | ||
1639 | if (t1) { | |
1640 | r = parse_sec(t1, &lease->t1); | |
1641 | if (r < 0) | |
1642 | log_debug_errno(r, "Failed to parse T1 %s, ignoring: %m", t1); | |
1643 | } | |
1644 | ||
1645 | if (t2) { | |
1646 | r = parse_sec(t2, &lease->t2); | |
1647 | if (r < 0) | |
1648 | log_debug_errno(r, "Failed to parse T2 %s, ignoring: %m", t2); | |
1649 | } | |
1650 | ||
1651 | if (client_id_hex) { | |
1652 | _cleanup_free_ void *data = NULL; | |
1653 | size_t data_size; | |
1654 | ||
1655 | r = unhexmem(client_id_hex, &data, &data_size); | |
1656 | if (r < 0) | |
1657 | log_debug_errno(r, "Failed to parse client ID %s, ignoring: %m", client_id_hex); | |
1658 | ||
1659 | r = sd_dhcp_client_id_set_raw(&lease->client_id, data, data_size); | |
1660 | if (r < 0) | |
1661 | log_debug_errno(r, "Failed to assign client ID, ignoring: %m"); | |
1662 | } | |
1663 | ||
1664 | if (vendor_specific_hex) { | |
1665 | r = unhexmem(vendor_specific_hex, &lease->vendor_specific, &lease->vendor_specific_len); | |
1666 | if (r < 0) | |
1667 | log_debug_errno(r, "Failed to parse vendor specific data %s, ignoring: %m", vendor_specific_hex); | |
1668 | } | |
1669 | ||
1670 | for (i = 0; i <= SD_DHCP_OPTION_PRIVATE_LAST - SD_DHCP_OPTION_PRIVATE_BASE; i++) { | |
1671 | _cleanup_free_ void *data = NULL; | |
1672 | size_t len; | |
1673 | ||
1674 | if (!options[i]) | |
1675 | continue; | |
1676 | ||
1677 | r = unhexmem(options[i], &data, &len); | |
1678 | if (r < 0) { | |
1679 | log_debug_errno(r, "Failed to parse private DHCP option %s, ignoring: %m", options[i]); | |
1680 | continue; | |
1681 | } | |
1682 | ||
1683 | r = dhcp_lease_insert_private_option(lease, SD_DHCP_OPTION_PRIVATE_BASE + i, data, len); | |
1684 | if (r < 0) | |
1685 | return r; | |
1686 | } | |
1687 | ||
1688 | *ret = TAKE_PTR(lease); | |
1689 | ||
1690 | return 0; | |
1691 | } | |
1692 | ||
1693 | int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease *lease) { | |
1694 | struct in_addr address, mask; | |
1695 | int r; | |
1696 | ||
1697 | assert(lease); | |
1698 | ||
1699 | if (lease->have_subnet_mask) | |
1700 | return 0; | |
1701 | ||
1702 | if (lease->address == 0) | |
1703 | return -ENODATA; | |
1704 | ||
1705 | address.s_addr = lease->address; | |
1706 | ||
1707 | /* fall back to the default subnet masks based on address class */ | |
1708 | r = in4_addr_default_subnet_mask(&address, &mask); | |
1709 | if (r < 0) | |
1710 | return r; | |
1711 | ||
1712 | lease->subnet_mask = mask.s_addr; | |
1713 | lease->have_subnet_mask = true; | |
1714 | ||
1715 | return 0; | |
1716 | } | |
1717 | ||
1718 | int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const sd_dhcp_client_id **ret) { | |
1719 | assert_return(lease, -EINVAL); | |
1720 | assert_return(ret, -EINVAL); | |
1721 | ||
1722 | if (!sd_dhcp_client_id_is_set(&lease->client_id)) | |
1723 | return -ENODATA; | |
1724 | ||
1725 | *ret = &lease->client_id; | |
1726 | ||
1727 | return 0; | |
1728 | } | |
1729 | ||
1730 | int dhcp_lease_set_client_id(sd_dhcp_lease *lease, const sd_dhcp_client_id *client_id) { | |
1731 | assert_return(lease, -EINVAL); | |
1732 | ||
1733 | if (!sd_dhcp_client_id_is_set(client_id)) | |
1734 | return sd_dhcp_client_id_clear(&lease->client_id); | |
1735 | ||
1736 | lease->client_id = *client_id; | |
1737 | ||
1738 | return 0; | |
1739 | } | |
1740 | ||
1741 | int sd_dhcp_lease_get_timezone(sd_dhcp_lease *lease, const char **tz) { | |
1742 | assert_return(lease, -EINVAL); | |
1743 | assert_return(tz, -EINVAL); | |
1744 | ||
1745 | if (!lease->timezone) | |
1746 | return -ENODATA; | |
1747 | ||
1748 | *tz = lease->timezone; | |
1749 | return 0; | |
1750 | } | |
1751 | ||
1752 | int sd_dhcp_route_get_destination(sd_dhcp_route *route, struct in_addr *destination) { | |
1753 | assert_return(route, -EINVAL); | |
1754 | assert_return(destination, -EINVAL); | |
1755 | ||
1756 | *destination = route->dst_addr; | |
1757 | return 0; | |
1758 | } | |
1759 | ||
1760 | int sd_dhcp_route_get_destination_prefix_length(sd_dhcp_route *route, uint8_t *length) { | |
1761 | assert_return(route, -EINVAL); | |
1762 | assert_return(length, -EINVAL); | |
1763 | ||
1764 | *length = route->dst_prefixlen; | |
1765 | return 0; | |
1766 | } | |
1767 | ||
1768 | int sd_dhcp_route_get_gateway(sd_dhcp_route *route, struct in_addr *gateway) { | |
1769 | assert_return(route, -EINVAL); | |
1770 | assert_return(gateway, -EINVAL); | |
1771 | ||
1772 | *gateway = route->gw_addr; | |
1773 | return 0; | |
1774 | } |