1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013 Intel Corporation. All rights reserved.
13 #include "sd-dhcp-lease.h"
15 #include "alloc-util.h"
16 #include "dhcp-lease-internal.h"
17 #include "dhcp-protocol.h"
18 #include "dns-domain.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"
30 #include "tmpfile-util.h"
31 #include "unaligned.h"
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
);
37 if (lease
->address
== 0)
40 addr
->s_addr
= lease
->address
;
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
);
48 if (!lease
->have_broadcast
)
51 addr
->s_addr
= lease
->broadcast
;
55 int sd_dhcp_lease_get_lifetime(sd_dhcp_lease
*lease
, uint32_t *lifetime
) {
56 assert_return(lease
, -EINVAL
);
57 assert_return(lifetime
, -EINVAL
);
59 if (lease
->lifetime
<= 0)
62 *lifetime
= lease
->lifetime
;
66 int sd_dhcp_lease_get_t1(sd_dhcp_lease
*lease
, uint32_t *t1
) {
67 assert_return(lease
, -EINVAL
);
68 assert_return(t1
, -EINVAL
);
77 int sd_dhcp_lease_get_t2(sd_dhcp_lease
*lease
, uint32_t *t2
) {
78 assert_return(lease
, -EINVAL
);
79 assert_return(t2
, -EINVAL
);
88 int sd_dhcp_lease_get_mtu(sd_dhcp_lease
*lease
, uint16_t *mtu
) {
89 assert_return(lease
, -EINVAL
);
90 assert_return(mtu
, -EINVAL
);
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
);
103 if (lease
->dns_size
<= 0)
107 return (int) lease
->dns_size
;
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
);
114 if (lease
->ntp_size
<= 0)
118 return (int) lease
->ntp_size
;
121 int sd_dhcp_lease_get_domainname(sd_dhcp_lease
*lease
, const char **domainname
) {
122 assert_return(lease
, -EINVAL
);
123 assert_return(domainname
, -EINVAL
);
125 if (!lease
->domainname
)
128 *domainname
= lease
->domainname
;
132 int sd_dhcp_lease_get_hostname(sd_dhcp_lease
*lease
, const char **hostname
) {
133 assert_return(lease
, -EINVAL
);
134 assert_return(hostname
, -EINVAL
);
136 if (!lease
->hostname
)
139 *hostname
= lease
->hostname
;
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
);
147 if (!lease
->root_path
)
150 *root_path
= lease
->root_path
;
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
);
158 if (lease
->router_size
<= 0)
161 *addr
= lease
->router
;
162 return (int) lease
->router_size
;
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
);
169 if (!lease
->have_subnet_mask
)
172 addr
->s_addr
= lease
->subnet_mask
;
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
);
180 if (lease
->server_address
== 0)
183 addr
->s_addr
= lease
->server_address
;
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
);
191 if (lease
->next_server
== 0)
194 addr
->s_addr
= lease
->next_server
;
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.
202 int sd_dhcp_lease_get_routes(sd_dhcp_lease
*lease
, sd_dhcp_route
***routes
) {
206 assert_return(lease
, -EINVAL
);
207 assert_return(routes
, -EINVAL
);
209 if (lease
->static_route_size
<= 0)
212 ret
= new(sd_dhcp_route
*, lease
->static_route_size
);
216 for (i
= 0; i
< lease
->static_route_size
; i
++)
217 ret
[i
] = &lease
->static_route
[i
];
220 return (int) lease
->static_route_size
;
223 int sd_dhcp_lease_get_search_domains(sd_dhcp_lease
*lease
, char ***domains
) {
226 assert_return(lease
, -EINVAL
);
227 assert_return(domains
, -EINVAL
);
229 r
= strv_length(lease
->search_domains
);
231 *domains
= lease
->search_domains
;
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
);
243 if (lease
->vendor_specific_len
<= 0)
246 *data
= lease
->vendor_specific
;
247 *data_len
= lease
->vendor_specific_len
;
251 static sd_dhcp_lease
*dhcp_lease_free(sd_dhcp_lease
*lease
) {
254 while (lease
->private_options
) {
255 struct sd_dhcp_raw_option
*option
= lease
->private_options
;
257 LIST_REMOVE(options
, lease
->private_options
, option
);
263 free(lease
->root_path
);
265 free(lease
->timezone
);
266 free(lease
->hostname
);
267 free(lease
->domainname
);
270 free(lease
->static_route
);
271 free(lease
->client_id
);
272 free(lease
->vendor_specific
);
273 strv_free(lease
->search_domains
);
277 DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_lease
, sd_dhcp_lease
, dhcp_lease_free
);
279 static int lease_parse_u32(const uint8_t *option
, size_t len
, uint32_t *ret
, uint32_t min
) {
286 *ret
= unaligned_read_be32((be32_t
*) option
);
293 static int lease_parse_u16(const uint8_t *option
, size_t len
, uint16_t *ret
, uint16_t min
) {
300 *ret
= unaligned_read_be16((be16_t
*) option
);
307 static int lease_parse_be32(const uint8_t *option
, size_t len
, be32_t
*ret
) {
314 memcpy(ret
, option
, 4);
318 static int lease_parse_string(const uint8_t *option
, size_t len
, char **ret
) {
328 * One trailing NUL byte is OK, we don't mind. See:
329 * https://github.com/systemd/systemd/issues/1337
331 if (memchr(option
, 0, len
- 1))
334 string
= strndup((const char *) option
, len
);
338 free_and_replace(*ret
, string
);
344 static int lease_parse_domain(const uint8_t *option
, size_t len
, char **ret
) {
345 _cleanup_free_
char *name
= NULL
, *normalized
= NULL
;
351 r
= lease_parse_string(option
, len
, &name
);
359 r
= dns_name_normalize(name
, 0, &normalized
);
363 if (is_localhost(normalized
))
366 if (dns_name_is_root(normalized
))
369 free_and_replace(*ret
, normalized
);
374 static void filter_bogus_addresses(struct in_addr
*addresses
, size_t *n
) {
377 /* Silently filter DNS/NTP servers supplied to us that do not make outside of the local scope. */
379 for (i
= 0, j
= 0; i
< *n
; i
++) {
381 if (in4_addr_is_null(addresses
+i
) ||
382 in4_addr_is_localhost(addresses
+i
))
385 addresses
[j
++] = addresses
[i
];
391 static int lease_parse_in_addrs(const uint8_t *option
, size_t len
, bool filter_bogus
, struct in_addr
**ret
, size_t *n_ret
) {
401 struct in_addr
*addresses
;
406 n_addresses
= len
/ 4;
408 addresses
= newdup(struct in_addr
, option
, n_addresses
);
413 filter_bogus_addresses(addresses
, &n_addresses
);
417 *n_ret
= n_addresses
;
423 static int lease_parse_routes(
424 const uint8_t *option
, size_t len
,
425 struct sd_dhcp_route
**routes
, size_t *routes_size
, size_t *routes_allocated
) {
429 assert(option
|| len
<= 0);
432 assert(routes_allocated
);
440 if (!GREEDY_REALLOC(*routes
, *routes_allocated
, *routes_size
+ (len
/ 8)))
444 struct sd_dhcp_route
*route
= *routes
+ *routes_size
;
447 route
->option
= SD_DHCP_OPTION_STATIC_ROUTE
;
448 r
= in4_addr_default_prefixlen((struct in_addr
*) option
, &route
->dst_prefixlen
);
450 log_debug("Failed to determine destination prefix length from class based IP, ignoring");
454 assert_se(lease_parse_be32(option
, 4, &addr
.s_addr
) >= 0);
455 route
->dst_addr
= inet_makeaddr(inet_netof(addr
), 0);
458 assert_se(lease_parse_be32(option
, 4, &route
->gw_addr
.s_addr
) >= 0);
468 /* parses RFC3442 Classless Static Route Option */
469 static int lease_parse_classless_routes(
470 const uint8_t *option
, size_t len
,
471 struct sd_dhcp_route
**routes
, size_t *routes_size
, size_t *routes_allocated
) {
473 assert(option
|| len
<= 0);
476 assert(routes_allocated
);
481 /* option format: (subnet-mask-width significant-subnet-octets gateway-ip)* */
485 struct sd_dhcp_route
*route
;
487 if (!GREEDY_REALLOC(*routes
, *routes_allocated
, *routes_size
+ 1))
490 route
= *routes
+ *routes_size
;
491 route
->option
= SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
;
493 dst_octets
= (*option
== 0 ? 0 : ((*option
- 1) / 8) + 1);
494 route
->dst_prefixlen
= *option
;
498 /* can't have more than 4 octets in IPv4 */
499 if (dst_octets
> 4 || len
< dst_octets
)
502 route
->dst_addr
.s_addr
= 0;
503 memcpy(&route
->dst_addr
.s_addr
, option
, dst_octets
);
504 option
+= dst_octets
;
510 assert_se(lease_parse_be32(option
, 4, &route
->gw_addr
.s_addr
) >= 0);
520 int dhcp_lease_parse_options(uint8_t code
, uint8_t len
, const void *option
, void *userdata
) {
521 sd_dhcp_lease
*lease
= userdata
;
528 case SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME
:
529 r
= lease_parse_u32(option
, len
, &lease
->lifetime
, 1);
531 log_debug_errno(r
, "Failed to parse lease time, ignoring: %m");
535 case SD_DHCP_OPTION_SERVER_IDENTIFIER
:
536 r
= lease_parse_be32(option
, len
, &lease
->server_address
);
538 log_debug_errno(r
, "Failed to parse server identifier, ignoring: %m");
542 case SD_DHCP_OPTION_SUBNET_MASK
:
543 r
= lease_parse_be32(option
, len
, &lease
->subnet_mask
);
545 log_debug_errno(r
, "Failed to parse subnet mask, ignoring: %m");
547 lease
->have_subnet_mask
= true;
550 case SD_DHCP_OPTION_BROADCAST
:
551 r
= lease_parse_be32(option
, len
, &lease
->broadcast
);
553 log_debug_errno(r
, "Failed to parse broadcast address, ignoring: %m");
555 lease
->have_broadcast
= true;
558 case SD_DHCP_OPTION_ROUTER
:
559 r
= lease_parse_in_addrs(option
, len
, false, &lease
->router
, &lease
->router_size
);
561 log_debug_errno(r
, "Failed to parse router addresses, ignoring: %m");
564 case SD_DHCP_OPTION_DOMAIN_NAME_SERVER
:
565 r
= lease_parse_in_addrs(option
, len
, true, &lease
->dns
, &lease
->dns_size
);
567 log_debug_errno(r
, "Failed to parse DNS server, ignoring: %m");
570 case SD_DHCP_OPTION_NTP_SERVER
:
571 r
= lease_parse_in_addrs(option
, len
, true, &lease
->ntp
, &lease
->ntp_size
);
573 log_debug_errno(r
, "Failed to parse NTP server, ignoring: %m");
576 case SD_DHCP_OPTION_STATIC_ROUTE
:
577 r
= lease_parse_routes(option
, len
, &lease
->static_route
, &lease
->static_route_size
, &lease
->static_route_allocated
);
579 log_debug_errno(r
, "Failed to parse static routes, ignoring: %m");
582 case SD_DHCP_OPTION_INTERFACE_MTU
:
583 r
= lease_parse_u16(option
, len
, &lease
->mtu
, 68);
585 log_debug_errno(r
, "Failed to parse MTU, ignoring: %m");
586 if (lease
->mtu
< DHCP_DEFAULT_MIN_SIZE
) {
587 log_debug("MTU value of %" PRIu16
" too small. Using default MTU value of %d instead.", lease
->mtu
, DHCP_DEFAULT_MIN_SIZE
);
588 lease
->mtu
= DHCP_DEFAULT_MIN_SIZE
;
593 case SD_DHCP_OPTION_DOMAIN_NAME
:
594 r
= lease_parse_domain(option
, len
, &lease
->domainname
);
596 log_debug_errno(r
, "Failed to parse domain name, ignoring: %m");
602 case SD_DHCP_OPTION_DOMAIN_SEARCH_LIST
:
603 r
= dhcp_lease_parse_search_domains(option
, len
, &lease
->search_domains
);
605 log_debug_errno(r
, "Failed to parse Domain Search List, ignoring: %m");
608 case SD_DHCP_OPTION_HOST_NAME
:
609 r
= lease_parse_domain(option
, len
, &lease
->hostname
);
611 log_debug_errno(r
, "Failed to parse host name, ignoring: %m");
617 case SD_DHCP_OPTION_ROOT_PATH
:
618 r
= lease_parse_string(option
, len
, &lease
->root_path
);
620 log_debug_errno(r
, "Failed to parse root path, ignoring: %m");
623 case SD_DHCP_OPTION_RENEWAL_T1_TIME
:
624 r
= lease_parse_u32(option
, len
, &lease
->t1
, 1);
626 log_debug_errno(r
, "Failed to parse T1 time, ignoring: %m");
629 case SD_DHCP_OPTION_REBINDING_T2_TIME
:
630 r
= lease_parse_u32(option
, len
, &lease
->t2
, 1);
632 log_debug_errno(r
, "Failed to parse T2 time, ignoring: %m");
635 case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE
:
636 r
= lease_parse_classless_routes(
638 &lease
->static_route
,
639 &lease
->static_route_size
,
640 &lease
->static_route_allocated
);
642 log_debug_errno(r
, "Failed to parse classless routes, ignoring: %m");
645 case SD_DHCP_OPTION_NEW_TZDB_TIMEZONE
: {
646 _cleanup_free_
char *tz
= NULL
;
648 r
= lease_parse_string(option
, len
, &tz
);
650 log_debug_errno(r
, "Failed to parse timezone option, ignoring: %m");
654 if (!timezone_is_valid(tz
, LOG_DEBUG
)) {
655 log_debug_errno(r
, "Timezone is not valid, ignoring: %m");
659 free_and_replace(lease
->timezone
, tz
);
664 case SD_DHCP_OPTION_VENDOR_SPECIFIC
:
667 lease
->vendor_specific
= mfree(lease
->vendor_specific
);
671 p
= memdup(option
, len
);
675 free(lease
->vendor_specific
);
676 lease
->vendor_specific
= p
;
679 lease
->vendor_specific_len
= len
;
682 case SD_DHCP_OPTION_PRIVATE_BASE
... SD_DHCP_OPTION_PRIVATE_LAST
:
683 r
= dhcp_lease_insert_private_option(lease
, code
, option
, len
);
690 log_debug("Ignoring option DHCP option %"PRIu8
" while parsing.", code
);
697 /* Parses compressed domain names. */
698 int dhcp_lease_parse_search_domains(const uint8_t *option
, size_t len
, char ***domains
) {
699 _cleanup_strv_free_
char **names
= NULL
;
700 size_t pos
= 0, cnt
= 0;
704 assert_return(option
&& len
> 0, -ENODATA
);
707 _cleanup_free_
char *name
= NULL
;
708 size_t n
= 0, allocated
= 0;
709 size_t jump_barrier
= pos
, next_chunk
= 0;
719 } else if (c
<= 63) {
723 label
= (const char*) (option
+ pos
);
728 if (!GREEDY_REALLOC(name
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
736 r
= dns_label_escape(label
, c
, name
+ n
, DNS_LABEL_ESCAPED_MAX
);
741 } else if ((c
& 0xc0) == 0xc0) {
751 ptr
= (uint16_t) (c
& ~0xc0) << 8 | (uint16_t) d
;
753 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
754 if (ptr
>= jump_barrier
)
758 /* Save current location so we don't end up re-parsing what's parsed so far. */
767 if (!GREEDY_REALLOC(name
, allocated
, n
+ 1))
771 r
= strv_extend(&names
, name
);
781 *domains
= TAKE_PTR(names
);
786 int dhcp_lease_insert_private_option(sd_dhcp_lease
*lease
, uint8_t tag
, const void *data
, uint8_t len
) {
787 struct sd_dhcp_raw_option
*cur
, *option
;
791 LIST_FOREACH(options
, cur
, lease
->private_options
) {
794 if (tag
== cur
->tag
) {
795 log_debug("Ignoring duplicate option, tagged %i.", tag
);
800 option
= new(struct sd_dhcp_raw_option
, 1);
805 option
->length
= len
;
806 option
->data
= memdup(data
, len
);
812 LIST_INSERT_BEFORE(options
, lease
->private_options
, cur
, option
);
816 int dhcp_lease_new(sd_dhcp_lease
**ret
) {
817 sd_dhcp_lease
*lease
;
819 lease
= new0(sd_dhcp_lease
, 1);
829 int dhcp_lease_save(sd_dhcp_lease
*lease
, const char *lease_file
) {
830 _cleanup_free_
char *temp_path
= NULL
;
831 _cleanup_fclose_
FILE *f
= NULL
;
832 struct sd_dhcp_raw_option
*option
;
833 struct in_addr address
;
834 const struct in_addr
*addresses
;
835 const void *client_id
, *data
;
836 size_t client_id_len
, data_len
;
839 _cleanup_free_ sd_dhcp_route
**routes
= NULL
;
840 char **search_domains
= NULL
;
841 uint32_t t1
, t2
, lifetime
;
847 r
= fopen_temporary(lease_file
, &f
, &temp_path
);
851 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
852 (void) fchmod(fileno(f
), 0644);
855 "# This is private data. Do not parse.\n");
857 r
= sd_dhcp_lease_get_address(lease
, &address
);
859 fprintf(f
, "ADDRESS=%s\n", inet_ntoa(address
));
861 r
= sd_dhcp_lease_get_netmask(lease
, &address
);
863 fprintf(f
, "NETMASK=%s\n", inet_ntoa(address
));
865 r
= sd_dhcp_lease_get_router(lease
, &addresses
);
868 serialize_in_addrs(f
, addresses
, r
);
872 r
= sd_dhcp_lease_get_server_identifier(lease
, &address
);
874 fprintf(f
, "SERVER_ADDRESS=%s\n", inet_ntoa(address
));
876 r
= sd_dhcp_lease_get_next_server(lease
, &address
);
878 fprintf(f
, "NEXT_SERVER=%s\n", inet_ntoa(address
));
880 r
= sd_dhcp_lease_get_broadcast(lease
, &address
);
882 fprintf(f
, "BROADCAST=%s\n", inet_ntoa(address
));
884 r
= sd_dhcp_lease_get_mtu(lease
, &mtu
);
886 fprintf(f
, "MTU=%" PRIu16
"\n", mtu
);
888 r
= sd_dhcp_lease_get_t1(lease
, &t1
);
890 fprintf(f
, "T1=%" PRIu32
"\n", t1
);
892 r
= sd_dhcp_lease_get_t2(lease
, &t2
);
894 fprintf(f
, "T2=%" PRIu32
"\n", t2
);
896 r
= sd_dhcp_lease_get_lifetime(lease
, &lifetime
);
898 fprintf(f
, "LIFETIME=%" PRIu32
"\n", lifetime
);
900 r
= sd_dhcp_lease_get_dns(lease
, &addresses
);
903 serialize_in_addrs(f
, addresses
, r
);
907 r
= sd_dhcp_lease_get_ntp(lease
, &addresses
);
910 serialize_in_addrs(f
, addresses
, r
);
914 r
= sd_dhcp_lease_get_domainname(lease
, &string
);
916 fprintf(f
, "DOMAINNAME=%s\n", string
);
918 r
= sd_dhcp_lease_get_search_domains(lease
, &search_domains
);
920 fputs("DOMAIN_SEARCH_LIST=", f
);
921 fputstrv(f
, search_domains
, NULL
, NULL
);
925 r
= sd_dhcp_lease_get_hostname(lease
, &string
);
927 fprintf(f
, "HOSTNAME=%s\n", string
);
929 r
= sd_dhcp_lease_get_root_path(lease
, &string
);
931 fprintf(f
, "ROOT_PATH=%s\n", string
);
933 r
= sd_dhcp_lease_get_routes(lease
, &routes
);
935 serialize_dhcp_routes(f
, "ROUTES", routes
, r
);
937 r
= sd_dhcp_lease_get_timezone(lease
, &string
);
939 fprintf(f
, "TIMEZONE=%s\n", string
);
941 r
= sd_dhcp_lease_get_client_id(lease
, &client_id
, &client_id_len
);
943 _cleanup_free_
char *client_id_hex
= NULL
;
945 client_id_hex
= hexmem(client_id
, client_id_len
);
946 if (!client_id_hex
) {
950 fprintf(f
, "CLIENTID=%s\n", client_id_hex
);
953 r
= sd_dhcp_lease_get_vendor_specific(lease
, &data
, &data_len
);
955 _cleanup_free_
char *option_hex
= NULL
;
957 option_hex
= hexmem(data
, data_len
);
962 fprintf(f
, "VENDOR_SPECIFIC=%s\n", option_hex
);
965 LIST_FOREACH(options
, option
, lease
->private_options
) {
966 char key
[STRLEN("OPTION_000")+1];
968 xsprintf(key
, "OPTION_%" PRIu8
, option
->tag
);
969 r
= serialize_dhcp_option(f
, key
, option
->data
, option
->length
);
974 r
= fflush_and_check(f
);
978 if (rename(temp_path
, lease_file
) < 0) {
987 (void) unlink(temp_path
);
989 return log_error_errno(r
, "Failed to save lease data %s: %m", lease_file
);
992 int dhcp_lease_load(sd_dhcp_lease
**ret
, const char *lease_file
) {
994 _cleanup_(sd_dhcp_lease_unrefp
) sd_dhcp_lease
*lease
= NULL
;
999 *server_address
= NULL
,
1000 *next_server
= NULL
,
1007 *client_id_hex
= NULL
,
1008 *vendor_specific_hex
= NULL
,
1012 *options
[SD_DHCP_OPTION_PRIVATE_LAST
- SD_DHCP_OPTION_PRIVATE_BASE
+ 1] = {};
1019 r
= dhcp_lease_new(&lease
);
1023 r
= parse_env_file(NULL
, lease_file
,
1024 "ADDRESS", &address
,
1026 "NETMASK", &netmask
,
1027 "SERVER_IDENTIFIER", &server_address
,
1028 "NEXT_SERVER", &next_server
,
1029 "BROADCAST", &broadcast
,
1033 "DOMAINNAME", &lease
->domainname
,
1034 "HOSTNAME", &lease
->hostname
,
1035 "DOMAIN_SEARCH_LIST", &domains
,
1036 "ROOT_PATH", &lease
->root_path
,
1038 "CLIENTID", &client_id_hex
,
1039 "TIMEZONE", &lease
->timezone
,
1040 "VENDOR_SPECIFIC", &vendor_specific_hex
,
1041 "LIFETIME", &lifetime
,
1044 "OPTION_224", &options
[0],
1045 "OPTION_225", &options
[1],
1046 "OPTION_226", &options
[2],
1047 "OPTION_227", &options
[3],
1048 "OPTION_228", &options
[4],
1049 "OPTION_229", &options
[5],
1050 "OPTION_230", &options
[6],
1051 "OPTION_231", &options
[7],
1052 "OPTION_232", &options
[8],
1053 "OPTION_233", &options
[9],
1054 "OPTION_234", &options
[10],
1055 "OPTION_235", &options
[11],
1056 "OPTION_236", &options
[12],
1057 "OPTION_237", &options
[13],
1058 "OPTION_238", &options
[14],
1059 "OPTION_239", &options
[15],
1060 "OPTION_240", &options
[16],
1061 "OPTION_241", &options
[17],
1062 "OPTION_242", &options
[18],
1063 "OPTION_243", &options
[19],
1064 "OPTION_244", &options
[20],
1065 "OPTION_245", &options
[21],
1066 "OPTION_246", &options
[22],
1067 "OPTION_247", &options
[23],
1068 "OPTION_248", &options
[24],
1069 "OPTION_249", &options
[25],
1070 "OPTION_250", &options
[26],
1071 "OPTION_251", &options
[27],
1072 "OPTION_252", &options
[28],
1073 "OPTION_253", &options
[29],
1074 "OPTION_254", &options
[30]);
1079 r
= inet_pton(AF_INET
, address
, &lease
->address
);
1081 log_debug("Failed to parse address %s, ignoring.", address
);
1085 r
= deserialize_in_addrs(&lease
->router
, router
);
1087 log_debug_errno(r
, "Failed to deserialize router addresses %s, ignoring: %m", router
);
1089 lease
->router_size
= r
;
1093 r
= inet_pton(AF_INET
, netmask
, &lease
->subnet_mask
);
1095 log_debug("Failed to parse netmask %s, ignoring.", netmask
);
1097 lease
->have_subnet_mask
= true;
1100 if (server_address
) {
1101 r
= inet_pton(AF_INET
, server_address
, &lease
->server_address
);
1103 log_debug("Failed to parse server address %s, ignoring.", server_address
);
1107 r
= inet_pton(AF_INET
, next_server
, &lease
->next_server
);
1109 log_debug("Failed to parse next server %s, ignoring.", next_server
);
1113 r
= inet_pton(AF_INET
, broadcast
, &lease
->broadcast
);
1115 log_debug("Failed to parse broadcast address %s, ignoring.", broadcast
);
1117 lease
->have_broadcast
= true;
1121 r
= deserialize_in_addrs(&lease
->dns
, dns
);
1123 log_debug_errno(r
, "Failed to deserialize DNS servers %s, ignoring: %m", dns
);
1125 lease
->dns_size
= r
;
1129 r
= deserialize_in_addrs(&lease
->ntp
, ntp
);
1131 log_debug_errno(r
, "Failed to deserialize NTP servers %s, ignoring: %m", ntp
);
1133 lease
->ntp_size
= r
;
1137 r
= safe_atou16(mtu
, &lease
->mtu
);
1139 log_debug_errno(r
, "Failed to parse MTU %s, ignoring: %m", mtu
);
1143 _cleanup_strv_free_
char **a
= NULL
;
1144 a
= strv_split(domains
, " ");
1148 if (!strv_isempty(a
)) {
1149 lease
->search_domains
= a
;
1155 r
= deserialize_dhcp_routes(
1156 &lease
->static_route
,
1157 &lease
->static_route_size
,
1158 &lease
->static_route_allocated
,
1161 log_debug_errno(r
, "Failed to parse DHCP routes %s, ignoring: %m", routes
);
1165 r
= safe_atou32(lifetime
, &lease
->lifetime
);
1167 log_debug_errno(r
, "Failed to parse lifetime %s, ignoring: %m", lifetime
);
1171 r
= safe_atou32(t1
, &lease
->t1
);
1173 log_debug_errno(r
, "Failed to parse T1 %s, ignoring: %m", t1
);
1177 r
= safe_atou32(t2
, &lease
->t2
);
1179 log_debug_errno(r
, "Failed to parse T2 %s, ignoring: %m", t2
);
1182 if (client_id_hex
) {
1183 r
= unhexmem(client_id_hex
, (size_t) -1, &lease
->client_id
, &lease
->client_id_len
);
1185 log_debug_errno(r
, "Failed to parse client ID %s, ignoring: %m", client_id_hex
);
1188 if (vendor_specific_hex
) {
1189 r
= unhexmem(vendor_specific_hex
, (size_t) -1, &lease
->vendor_specific
, &lease
->vendor_specific_len
);
1191 log_debug_errno(r
, "Failed to parse vendor specific data %s, ignoring: %m", vendor_specific_hex
);
1194 for (i
= 0; i
<= SD_DHCP_OPTION_PRIVATE_LAST
- SD_DHCP_OPTION_PRIVATE_BASE
; i
++) {
1195 _cleanup_free_
void *data
= NULL
;
1201 r
= unhexmem(options
[i
], (size_t) -1, &data
, &len
);
1203 log_debug_errno(r
, "Failed to parse private DHCP option %s, ignoring: %m", options
[i
]);
1207 r
= dhcp_lease_insert_private_option(lease
, SD_DHCP_OPTION_PRIVATE_BASE
+ i
, data
, len
);
1212 *ret
= TAKE_PTR(lease
);
1217 int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease
*lease
) {
1218 struct in_addr address
, mask
;
1223 if (lease
->address
== 0)
1226 address
.s_addr
= lease
->address
;
1228 /* fall back to the default subnet masks based on address class */
1229 r
= in4_addr_default_subnet_mask(&address
, &mask
);
1233 lease
->subnet_mask
= mask
.s_addr
;
1234 lease
->have_subnet_mask
= true;
1239 int sd_dhcp_lease_get_client_id(sd_dhcp_lease
*lease
, const void **client_id
, size_t *client_id_len
) {
1240 assert_return(lease
, -EINVAL
);
1241 assert_return(client_id
, -EINVAL
);
1242 assert_return(client_id_len
, -EINVAL
);
1244 if (!lease
->client_id
)
1247 *client_id
= lease
->client_id
;
1248 *client_id_len
= lease
->client_id_len
;
1253 int dhcp_lease_set_client_id(sd_dhcp_lease
*lease
, const void *client_id
, size_t client_id_len
) {
1254 assert_return(lease
, -EINVAL
);
1255 assert_return(client_id
|| client_id_len
<= 0, -EINVAL
);
1257 if (client_id_len
<= 0)
1258 lease
->client_id
= mfree(lease
->client_id
);
1262 p
= memdup(client_id
, client_id_len
);
1266 free(lease
->client_id
);
1267 lease
->client_id
= p
;
1268 lease
->client_id_len
= client_id_len
;
1274 int sd_dhcp_lease_get_timezone(sd_dhcp_lease
*lease
, const char **tz
) {
1275 assert_return(lease
, -EINVAL
);
1276 assert_return(tz
, -EINVAL
);
1278 if (!lease
->timezone
)
1281 *tz
= lease
->timezone
;
1285 int sd_dhcp_route_get_destination(sd_dhcp_route
*route
, struct in_addr
*destination
) {
1286 assert_return(route
, -EINVAL
);
1287 assert_return(destination
, -EINVAL
);
1289 *destination
= route
->dst_addr
;
1293 int sd_dhcp_route_get_destination_prefix_length(sd_dhcp_route
*route
, uint8_t *length
) {
1294 assert_return(route
, -EINVAL
);
1295 assert_return(length
, -EINVAL
);
1297 *length
= route
->dst_prefixlen
;
1301 int sd_dhcp_route_get_gateway(sd_dhcp_route
*route
, struct in_addr
*gateway
) {
1302 assert_return(route
, -EINVAL
);
1303 assert_return(gateway
, -EINVAL
);
1305 *gateway
= route
->gw_addr
;
1309 int sd_dhcp_route_get_option(sd_dhcp_route
*route
) {
1310 assert_return(route
, -EINVAL
);
1312 return route
->option
;