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