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