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