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