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