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