]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a6cc569e | 2 | /*** |
a6cc569e TG |
3 | Copyright (C) 2013 Intel Corporation. All rights reserved. |
4 | Copyright (C) 2014 Tom Gundersen | |
a6cc569e TG |
5 | ***/ |
6 | ||
07630cea | 7 | #include <arpa/inet.h> |
a6cc569e | 8 | #include <errno.h> |
a6cc569e | 9 | #include <stdio.h> |
0d536673 | 10 | #include <stdio_ext.h> |
07630cea LP |
11 | #include <stdlib.h> |
12 | #include <string.h> | |
13 | ||
14 | #include "sd-dhcp-lease.h" | |
a6cc569e | 15 | |
b5efdb8a | 16 | #include "alloc-util.h" |
07630cea LP |
17 | #include "dhcp-lease-internal.h" |
18 | #include "dhcp-protocol.h" | |
19 | #include "dns-domain.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" |
07630cea | 29 | #include "unaligned.h" |
a6cc569e TG |
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 | ||
0339cd77 LP |
35 | if (lease->address == 0) |
36 | return -ENODATA; | |
37 | ||
a6cc569e | 38 | addr->s_addr = lease->address; |
0339cd77 LP |
39 | return 0; |
40 | } | |
a6cc569e | 41 | |
0339cd77 LP |
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; | |
a6cc569e TG |
50 | return 0; |
51 | } | |
52 | ||
68ceb9df PF |
53 | int sd_dhcp_lease_get_lifetime(sd_dhcp_lease *lease, uint32_t *lifetime) { |
54 | assert_return(lease, -EINVAL); | |
1c6eb4e3 | 55 | assert_return(lifetime, -EINVAL); |
68ceb9df | 56 | |
0339cd77 LP |
57 | if (lease->lifetime <= 0) |
58 | return -ENODATA; | |
59 | ||
68ceb9df | 60 | *lifetime = lease->lifetime; |
0339cd77 LP |
61 | return 0; |
62 | } | |
68ceb9df | 63 | |
0339cd77 LP |
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; | |
68ceb9df PF |
83 | return 0; |
84 | } | |
85 | ||
a6cc569e TG |
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 | ||
0339cd77 LP |
90 | if (lease->mtu <= 0) |
91 | return -ENODATA; | |
a6cc569e | 92 | |
0339cd77 | 93 | *mtu = lease->mtu; |
a6cc569e TG |
94 | return 0; |
95 | } | |
96 | ||
a2ba62c7 | 97 | int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr) { |
a6cc569e TG |
98 | assert_return(lease, -EINVAL); |
99 | assert_return(addr, -EINVAL); | |
a6cc569e | 100 | |
0339cd77 LP |
101 | if (lease->dns_size <= 0) |
102 | return -ENODATA; | |
a6cc569e | 103 | |
0339cd77 LP |
104 | *addr = lease->dns; |
105 | return (int) lease->dns_size; | |
a6cc569e TG |
106 | } |
107 | ||
a2ba62c7 | 108 | int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr) { |
46844696 TG |
109 | assert_return(lease, -EINVAL); |
110 | assert_return(addr, -EINVAL); | |
46844696 | 111 | |
0339cd77 LP |
112 | if (lease->ntp_size <= 0) |
113 | return -ENODATA; | |
46844696 | 114 | |
0339cd77 LP |
115 | *addr = lease->ntp; |
116 | return (int) lease->ntp_size; | |
46844696 TG |
117 | } |
118 | ||
a6cc569e TG |
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 | ||
0339cd77 LP |
123 | if (!lease->domainname) |
124 | return -ENODATA; | |
a6cc569e | 125 | |
0339cd77 | 126 | *domainname = lease->domainname; |
a6cc569e TG |
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 | ||
0339cd77 LP |
134 | if (!lease->hostname) |
135 | return -ENODATA; | |
a6cc569e | 136 | |
0339cd77 | 137 | *hostname = lease->hostname; |
a6cc569e TG |
138 | return 0; |
139 | } | |
140 | ||
ce78df79 TG |
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 | ||
0339cd77 LP |
145 | if (!lease->root_path) |
146 | return -ENODATA; | |
ce78df79 | 147 | |
0339cd77 | 148 | *root_path = lease->root_path; |
ce78df79 TG |
149 | return 0; |
150 | } | |
151 | ||
a6cc569e TG |
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 | ||
0339cd77 LP |
156 | if (lease->router == 0) |
157 | return -ENODATA; | |
a6cc569e | 158 | |
0339cd77 | 159 | addr->s_addr = lease->router; |
a6cc569e TG |
160 | return 0; |
161 | } | |
162 | ||
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 | ||
0339cd77 LP |
167 | if (!lease->have_subnet_mask) |
168 | return -ENODATA; | |
a6cc569e | 169 | |
0339cd77 | 170 | addr->s_addr = lease->subnet_mask; |
a6cc569e TG |
171 | return 0; |
172 | } | |
173 | ||
0ad853bc TG |
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 | ||
0339cd77 LP |
178 | if (lease->server_address == 0) |
179 | return -ENODATA; | |
0ad853bc | 180 | |
0339cd77 | 181 | addr->s_addr = lease->server_address; |
0ad853bc TG |
182 | return 0; |
183 | } | |
184 | ||
8e34a618 TG |
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 | ||
0339cd77 LP |
189 | if (lease->next_server == 0) |
190 | return -ENODATA; | |
8e34a618 | 191 | |
0339cd77 | 192 | addr->s_addr = lease->next_server; |
8e34a618 TG |
193 | return 0; |
194 | } | |
195 | ||
f8693fc7 BG |
196 | /* |
197 | * The returned routes array must be freed by the caller. | |
198 | * Route objects have the same lifetime of the lease and must not be freed. | |
199 | */ | |
200 | int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, sd_dhcp_route ***routes) { | |
201 | sd_dhcp_route **ret; | |
202 | unsigned i; | |
203 | ||
e1ea665e EY |
204 | assert_return(lease, -EINVAL); |
205 | assert_return(routes, -EINVAL); | |
e1ea665e | 206 | |
0339cd77 LP |
207 | if (lease->static_route_size <= 0) |
208 | return -ENODATA; | |
e1ea665e | 209 | |
f8693fc7 BG |
210 | ret = new(sd_dhcp_route *, lease->static_route_size); |
211 | if (!ret) | |
212 | return -ENOMEM; | |
213 | ||
214 | for (i = 0; i < lease->static_route_size; i++) | |
215 | ret[i] = &lease->static_route[i]; | |
216 | ||
217 | *routes = ret; | |
0339cd77 | 218 | return (int) lease->static_route_size; |
e1ea665e EY |
219 | } |
220 | ||
b85bc551 | 221 | int sd_dhcp_lease_get_search_domains(sd_dhcp_lease *lease, char ***domains) { |
da6053d0 | 222 | size_t r; |
b85bc551 DW |
223 | |
224 | assert_return(lease, -EINVAL); | |
225 | assert_return(domains, -EINVAL); | |
226 | ||
227 | r = strv_length(lease->search_domains); | |
228 | if (r > 0) { | |
229 | *domains = lease->search_domains; | |
230 | return (int) r; | |
231 | } | |
232 | ||
233 | return -ENODATA; | |
234 | } | |
235 | ||
0339cd77 | 236 | int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const void **data, size_t *data_len) { |
e43a8393 BG |
237 | assert_return(lease, -EINVAL); |
238 | assert_return(data, -EINVAL); | |
239 | assert_return(data_len, -EINVAL); | |
240 | ||
0339cd77 LP |
241 | if (lease->vendor_specific_len <= 0) |
242 | return -ENODATA; | |
e43a8393 BG |
243 | |
244 | *data = lease->vendor_specific; | |
245 | *data_len = lease->vendor_specific_len; | |
e43a8393 BG |
246 | return 0; |
247 | } | |
248 | ||
a6cc569e | 249 | sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) { |
3733eec3 LP |
250 | |
251 | if (!lease) | |
252 | return NULL; | |
253 | ||
254 | assert(lease->n_ref >= 1); | |
255 | lease->n_ref++; | |
a6cc569e TG |
256 | |
257 | return lease; | |
258 | } | |
259 | ||
260 | sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) { | |
7e753d9d | 261 | |
3733eec3 LP |
262 | if (!lease) |
263 | return NULL; | |
7e753d9d | 264 | |
3733eec3 LP |
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); | |
a6cc569e TG |
278 | } |
279 | ||
3733eec3 LP |
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); | |
b85bc551 | 287 | strv_free(lease->search_domains); |
6b430fdb | 288 | return mfree(lease); |
a6cc569e TG |
289 | } |
290 | ||
0339cd77 | 291 | static int lease_parse_u32(const uint8_t *option, size_t len, uint32_t *ret, uint32_t min) { |
e140ae58 TG |
292 | assert(option); |
293 | assert(ret); | |
294 | ||
0339cd77 LP |
295 | if (len != 4) |
296 | return -EINVAL; | |
e140ae58 | 297 | |
0339cd77 LP |
298 | *ret = unaligned_read_be32((be32_t*) option); |
299 | if (*ret < min) | |
300 | *ret = min; | |
e140ae58 | 301 | |
0339cd77 | 302 | return 0; |
e140ae58 TG |
303 | } |
304 | ||
0339cd77 | 305 | static int lease_parse_u16(const uint8_t *option, size_t len, uint16_t *ret, uint16_t min) { |
e140ae58 TG |
306 | assert(option); |
307 | assert(ret); | |
308 | ||
0339cd77 LP |
309 | if (len != 2) |
310 | return -EINVAL; | |
e140ae58 | 311 | |
0339cd77 LP |
312 | *ret = unaligned_read_be16((be16_t*) option); |
313 | if (*ret < min) | |
314 | *ret = min; | |
f5c0c00f | 315 | |
0339cd77 | 316 | return 0; |
f5c0c00f TG |
317 | } |
318 | ||
0339cd77 | 319 | static int lease_parse_be32(const uint8_t *option, size_t len, be32_t *ret) { |
f5c0c00f TG |
320 | assert(option); |
321 | assert(ret); | |
322 | ||
0339cd77 LP |
323 | if (len != 4) |
324 | return -EINVAL; | |
f5c0c00f | 325 | |
0339cd77 LP |
326 | memcpy(ret, option, 4); |
327 | return 0; | |
f5c0c00f TG |
328 | } |
329 | ||
e140ae58 TG |
330 | static int lease_parse_string(const uint8_t *option, size_t len, char **ret) { |
331 | assert(option); | |
332 | assert(ret); | |
333 | ||
0339cd77 LP |
334 | if (len <= 0) |
335 | *ret = mfree(*ret); | |
336 | else { | |
e140ae58 TG |
337 | char *string; |
338 | ||
e989fd9b LP |
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)) | |
43f447b1 LP |
344 | return -EINVAL; |
345 | ||
e989fd9b | 346 | string = strndup((const char *) option, len); |
e140ae58 | 347 | if (!string) |
43f447b1 | 348 | return -ENOMEM; |
e140ae58 TG |
349 | |
350 | free(*ret); | |
351 | *ret = string; | |
0339cd77 | 352 | } |
e140ae58 TG |
353 | |
354 | return 0; | |
355 | } | |
356 | ||
978c6477 LP |
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 | ||
f9ecfd3b | 382 | free_and_replace(*ret, normalized); |
978c6477 LP |
383 | |
384 | return 0; | |
385 | } | |
386 | ||
d9ec2e63 LP |
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 | ||
0339cd77 | 404 | static int lease_parse_in_addrs(const uint8_t *option, size_t len, struct in_addr **ret, size_t *n_ret) { |
e140ae58 TG |
405 | assert(option); |
406 | assert(ret); | |
0339cd77 | 407 | assert(n_ret); |
e140ae58 | 408 | |
0339cd77 LP |
409 | if (len <= 0) { |
410 | *ret = mfree(*ret); | |
411 | *n_ret = 0; | |
412 | } else { | |
413 | size_t n_addresses; | |
e140ae58 TG |
414 | struct in_addr *addresses; |
415 | ||
0339cd77 LP |
416 | if (len % 4 != 0) |
417 | return -EINVAL; | |
e140ae58 | 418 | |
0339cd77 LP |
419 | n_addresses = len / 4; |
420 | ||
421 | addresses = newdup(struct in_addr, option, n_addresses); | |
e140ae58 TG |
422 | if (!addresses) |
423 | return -ENOMEM; | |
424 | ||
d9ec2e63 LP |
425 | filter_bogus_addresses(addresses, &n_addresses); |
426 | ||
e140ae58 TG |
427 | free(*ret); |
428 | *ret = addresses; | |
0339cd77 | 429 | *n_ret = n_addresses; |
e140ae58 TG |
430 | } |
431 | ||
432 | return 0; | |
433 | } | |
434 | ||
0339cd77 LP |
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) { | |
e1ea665e EY |
438 | |
439 | struct in_addr addr; | |
440 | ||
0339cd77 | 441 | assert(option || len <= 0); |
e1ea665e EY |
442 | assert(routes); |
443 | assert(routes_size); | |
444 | assert(routes_allocated); | |
445 | ||
0339cd77 | 446 | if (len <= 0) |
e1ea665e EY |
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; | |
1caa12d0 | 457 | int r; |
e1ea665e | 458 | |
8cdc46e7 | 459 | route->option = SD_DHCP_OPTION_STATIC_ROUTE; |
5a941f5f | 460 | r = in4_addr_default_prefixlen((struct in_addr*) option, &route->dst_prefixlen); |
1caa12d0 | 461 | if (r < 0) { |
0339cd77 | 462 | log_debug("Failed to determine destination prefix length from class based IP, ignoring"); |
e1ea665e EY |
463 | continue; |
464 | } | |
465 | ||
0339cd77 | 466 | assert_se(lease_parse_be32(option, 4, &addr.s_addr) >= 0); |
e1ea665e EY |
467 | route->dst_addr = inet_makeaddr(inet_netof(addr), 0); |
468 | option += 4; | |
469 | ||
0339cd77 | 470 | assert_se(lease_parse_be32(option, 4, &route->gw_addr.s_addr) >= 0); |
e1ea665e EY |
471 | option += 4; |
472 | ||
473 | len -= 8; | |
474 | (*routes_size)++; | |
475 | } | |
476 | ||
477 | return 0; | |
478 | } | |
479 | ||
480 | /* parses RFC3442 Classless Static Route Option */ | |
0339cd77 LP |
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) { | |
e1ea665e | 484 | |
0339cd77 | 485 | assert(option || len <= 0); |
e1ea665e EY |
486 | assert(routes); |
487 | assert(routes_size); | |
488 | assert(routes_allocated); | |
489 | ||
0339cd77 LP |
490 | if (len <= 0) |
491 | return 0; | |
492 | ||
e1ea665e EY |
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)) | |
0339cd77 | 500 | return -ENOMEM; |
e1ea665e EY |
501 | |
502 | route = *routes + *routes_size; | |
8cdc46e7 | 503 | route->option = SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE; |
e1ea665e EY |
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 | ||
b14fff6e | 522 | assert_se(lease_parse_be32(option, 4, &route->gw_addr.s_addr) >= 0); |
e1ea665e EY |
523 | option += 4; |
524 | len -= 4; | |
525 | ||
526 | (*routes_size)++; | |
527 | } | |
528 | ||
529 | return 0; | |
530 | } | |
531 | ||
e4735228 | 532 | int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void *userdata) { |
2d03c0b8 | 533 | sd_dhcp_lease *lease = userdata; |
e140ae58 TG |
534 | int r; |
535 | ||
536 | assert(lease); | |
a6cc569e TG |
537 | |
538 | switch(code) { | |
539 | ||
22805d92 | 540 | case SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME: |
0339cd77 LP |
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 | ||
a6cc569e TG |
545 | break; |
546 | ||
22805d92 | 547 | case SD_DHCP_OPTION_SERVER_IDENTIFIER: |
0339cd77 LP |
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 | ||
a6cc569e TG |
552 | break; |
553 | ||
22805d92 | 554 | case SD_DHCP_OPTION_SUBNET_MASK: |
0339cd77 LP |
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; | |
a6cc569e TG |
560 | break; |
561 | ||
22805d92 | 562 | case SD_DHCP_OPTION_BROADCAST: |
0339cd77 LP |
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; | |
f5c0c00f TG |
568 | break; |
569 | ||
22805d92 | 570 | case SD_DHCP_OPTION_ROUTER: |
0339cd77 LP |
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 | } | |
a6cc569e TG |
576 | break; |
577 | ||
22805d92 | 578 | case SD_DHCP_OPTION_DOMAIN_NAME_SERVER: |
0339cd77 LP |
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"); | |
a6cc569e TG |
582 | break; |
583 | ||
22805d92 | 584 | case SD_DHCP_OPTION_NTP_SERVER: |
0339cd77 LP |
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"); | |
f5c0c00f TG |
588 | break; |
589 | ||
22805d92 | 590 | case SD_DHCP_OPTION_STATIC_ROUTE: |
0339cd77 LP |
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"); | |
f5c0c00f TG |
594 | break; |
595 | ||
22805d92 | 596 | case SD_DHCP_OPTION_INTERFACE_MTU: |
0339cd77 LP |
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"); | |
955d99ed | 600 | if (lease->mtu < DHCP_DEFAULT_MIN_SIZE) { |
4dd53da9 | 601 | log_debug("MTU value of %" PRIu16 " too small. Using default MTU value of %d instead.", lease->mtu, DHCP_DEFAULT_MIN_SIZE); |
955d99ed MG |
602 | lease->mtu = DHCP_DEFAULT_MIN_SIZE; |
603 | } | |
604 | ||
f5c0c00f TG |
605 | break; |
606 | ||
978c6477 LP |
607 | case SD_DHCP_OPTION_DOMAIN_NAME: |
608 | r = lease_parse_domain(option, len, &lease->domainname); | |
0339cd77 LP |
609 | if (r < 0) { |
610 | log_debug_errno(r, "Failed to parse domain name, ignoring: %m"); | |
611 | return 0; | |
612 | } | |
a6cc569e | 613 | |
784d9b9c | 614 | break; |
784d9b9c | 615 | |
b85bc551 DW |
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 | ||
978c6477 LP |
622 | case SD_DHCP_OPTION_HOST_NAME: |
623 | r = lease_parse_domain(option, len, &lease->hostname); | |
0339cd77 LP |
624 | if (r < 0) { |
625 | log_debug_errno(r, "Failed to parse host name, ignoring: %m"); | |
626 | return 0; | |
627 | } | |
a6cc569e | 628 | |
784d9b9c | 629 | break; |
ce78df79 | 630 | |
22805d92 | 631 | case SD_DHCP_OPTION_ROOT_PATH: |
0339cd77 LP |
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; | |
ce78df79 | 636 | |
22805d92 | 637 | case SD_DHCP_OPTION_RENEWAL_T1_TIME: |
0339cd77 LP |
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"); | |
a6cc569e TG |
641 | break; |
642 | ||
22805d92 | 643 | case SD_DHCP_OPTION_REBINDING_T2_TIME: |
0339cd77 LP |
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"); | |
a6cc569e | 647 | break; |
e1ea665e | 648 | |
22805d92 | 649 | case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE: |
0339cd77 | 650 | r = lease_parse_classless_routes( |
2d03c0b8 LP |
651 | option, len, |
652 | &lease->static_route, | |
653 | &lease->static_route_size, | |
654 | &lease->static_route_allocated); | |
0339cd77 LP |
655 | if (r < 0) |
656 | log_debug_errno(r, "Failed to parse classless routes, ignoring: %m"); | |
657 | break; | |
e43a8393 | 658 | |
22805d92 | 659 | case SD_DHCP_OPTION_NEW_TZDB_TIMEZONE: { |
8eb9058d LP |
660 | _cleanup_free_ char *tz = NULL; |
661 | ||
662 | r = lease_parse_string(option, len, &tz); | |
0339cd77 LP |
663 | if (r < 0) { |
664 | log_debug_errno(r, "Failed to parse timezone option, ignoring: %m"); | |
665 | return 0; | |
666 | } | |
8eb9058d | 667 | |
089fb865 | 668 | if (!timezone_is_valid(tz, LOG_DEBUG)) { |
0339cd77 LP |
669 | log_debug_errno(r, "Timezone is not valid, ignoring: %m"); |
670 | return 0; | |
671 | } | |
8eb9058d | 672 | |
f9ecfd3b | 673 | free_and_replace(lease->timezone, tz); |
0339cd77 | 674 | |
8eb9058d LP |
675 | break; |
676 | } | |
677 | ||
22805d92 | 678 | case SD_DHCP_OPTION_VENDOR_SPECIFIC: |
2d03c0b8 | 679 | |
0339cd77 LP |
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) | |
e43a8393 | 687 | return -ENOMEM; |
e43a8393 | 688 | |
0339cd77 LP |
689 | free(lease->vendor_specific); |
690 | lease->vendor_specific = p; | |
691 | } | |
7e753d9d | 692 | |
0339cd77 LP |
693 | lease->vendor_specific_len = len; |
694 | break; | |
7e753d9d | 695 | |
22805d92 | 696 | case SD_DHCP_OPTION_PRIVATE_BASE ... SD_DHCP_OPTION_PRIVATE_LAST: |
7e753d9d AC |
697 | r = dhcp_lease_insert_private_option(lease, code, option, len); |
698 | if (r < 0) | |
699 | return r; | |
0339cd77 LP |
700 | |
701 | break; | |
702 | ||
703 | default: | |
f693e9b3 | 704 | log_debug("Ignoring option DHCP option %"PRIu8" while parsing.", code); |
0339cd77 | 705 | break; |
a6cc569e TG |
706 | } |
707 | ||
708 | return 0; | |
709 | } | |
710 | ||
b85bc551 DW |
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 | ||
ae2a15bc | 795 | *domains = TAKE_PTR(names); |
b85bc551 DW |
796 | |
797 | return cnt; | |
798 | } | |
799 | ||
0339cd77 | 800 | int dhcp_lease_insert_private_option(sd_dhcp_lease *lease, uint8_t tag, const void *data, uint8_t len) { |
7e753d9d AC |
801 | struct sd_dhcp_raw_option *cur, *option; |
802 | ||
0339cd77 LP |
803 | assert(lease); |
804 | ||
7e753d9d AC |
805 | LIST_FOREACH(options, cur, lease->private_options) { |
806 | if (tag < cur->tag) | |
807 | break; | |
0339cd77 LP |
808 | if (tag == cur->tag) { |
809 | log_debug("Ignoring duplicate option, tagged %i.", tag); | |
7e753d9d AC |
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); | |
7e753d9d AC |
827 | return 0; |
828 | } | |
829 | ||
a6cc569e | 830 | int dhcp_lease_new(sd_dhcp_lease **ret) { |
6e00a806 | 831 | sd_dhcp_lease *lease; |
a6cc569e TG |
832 | |
833 | lease = new0(sd_dhcp_lease, 1); | |
834 | if (!lease) | |
835 | return -ENOMEM; | |
836 | ||
8ddbeaa2 | 837 | lease->router = INADDR_ANY; |
3733eec3 | 838 | lease->n_ref = 1; |
a6cc569e TG |
839 | |
840 | *ret = lease; | |
a6cc569e TG |
841 | return 0; |
842 | } | |
fe8db0c5 | 843 | |
bd91b83e | 844 | int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { |
fe8db0c5 TG |
845 | _cleanup_free_ char *temp_path = NULL; |
846 | _cleanup_fclose_ FILE *f = NULL; | |
a073309f | 847 | struct sd_dhcp_raw_option *option; |
fe8db0c5 | 848 | struct in_addr address; |
a2ba62c7 | 849 | const struct in_addr *addresses; |
e4735228 | 850 | const void *client_id, *data; |
e43a8393 | 851 | size_t client_id_len, data_len; |
fe8db0c5 TG |
852 | const char *string; |
853 | uint16_t mtu; | |
f8693fc7 | 854 | _cleanup_free_ sd_dhcp_route **routes = NULL; |
b85bc551 | 855 | char **search_domains = NULL; |
0339cd77 | 856 | uint32_t t1, t2, lifetime; |
fe8db0c5 TG |
857 | int r; |
858 | ||
859 | assert(lease); | |
860 | assert(lease_file); | |
861 | ||
fe8db0c5 TG |
862 | r = fopen_temporary(lease_file, &f, &temp_path); |
863 | if (r < 0) | |
dacd6cee | 864 | goto fail; |
fe8db0c5 | 865 | |
0d536673 LP |
866 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
867 | (void) fchmod(fileno(f), 0644); | |
fe8db0c5 | 868 | |
fe8db0c5 | 869 | fprintf(f, |
0339cd77 | 870 | "# This is private data. Do not parse.\n"); |
fe8db0c5 | 871 | |
0339cd77 LP |
872 | r = sd_dhcp_lease_get_address(lease, &address); |
873 | if (r >= 0) | |
874 | fprintf(f, "ADDRESS=%s\n", inet_ntoa(address)); | |
fe8db0c5 | 875 | |
0339cd77 LP |
876 | r = sd_dhcp_lease_get_netmask(lease, &address); |
877 | if (r >= 0) | |
878 | fprintf(f, "NETMASK=%s\n", inet_ntoa(address)); | |
fe8db0c5 | 879 | |
8ddbeaa2 UTL |
880 | r = sd_dhcp_lease_get_router(lease, &address); |
881 | if (r >= 0) | |
882 | fprintf(f, "ROUTER=%s\n", inet_ntoa(address)); | |
883 | ||
0ad853bc | 884 | r = sd_dhcp_lease_get_server_identifier(lease, &address); |
109731eb | 885 | if (r >= 0) |
0339cd77 | 886 | fprintf(f, "SERVER_ADDRESS=%s\n", inet_ntoa(address)); |
0ad853bc | 887 | |
8e34a618 | 888 | r = sd_dhcp_lease_get_next_server(lease, &address); |
109731eb TG |
889 | if (r >= 0) |
890 | fprintf(f, "NEXT_SERVER=%s\n", inet_ntoa(address)); | |
8e34a618 | 891 | |
0339cd77 LP |
892 | r = sd_dhcp_lease_get_broadcast(lease, &address); |
893 | if (r >= 0) | |
894 | fprintf(f, "BROADCAST=%s\n", inet_ntoa(address)); | |
895 | ||
fe8db0c5 TG |
896 | r = sd_dhcp_lease_get_mtu(lease, &mtu); |
897 | if (r >= 0) | |
898 | fprintf(f, "MTU=%" PRIu16 "\n", mtu); | |
899 | ||
0339cd77 LP |
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); | |
109731eb | 905 | if (r >= 0) |
0339cd77 LP |
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) { | |
0d536673 | 914 | fputs("DNS=", f); |
b0e39c82 | 915 | serialize_in_addrs(f, addresses, r); |
0d536673 | 916 | fputs("\n", f); |
0339cd77 | 917 | } |
109731eb | 918 | |
a2ba62c7 | 919 | r = sd_dhcp_lease_get_ntp(lease, &addresses); |
0339cd77 | 920 | if (r > 0) { |
0d536673 | 921 | fputs("NTP=", f); |
b0e39c82 | 922 | serialize_in_addrs(f, addresses, r); |
0d536673 | 923 | fputs("\n", f); |
0339cd77 | 924 | } |
fe8db0c5 TG |
925 | |
926 | r = sd_dhcp_lease_get_domainname(lease, &string); | |
927 | if (r >= 0) | |
928 | fprintf(f, "DOMAINNAME=%s\n", string); | |
929 | ||
b85bc551 DW |
930 | r = sd_dhcp_lease_get_search_domains(lease, &search_domains); |
931 | if (r > 0) { | |
0d536673 | 932 | fputs("DOMAIN_SEARCH_LIST=", f); |
b85bc551 | 933 | fputstrv(f, search_domains, NULL, NULL); |
0d536673 | 934 | fputs("\n", f); |
b85bc551 DW |
935 | } |
936 | ||
fe8db0c5 TG |
937 | r = sd_dhcp_lease_get_hostname(lease, &string); |
938 | if (r >= 0) | |
939 | fprintf(f, "HOSTNAME=%s\n", string); | |
940 | ||
ce78df79 TG |
941 | r = sd_dhcp_lease_get_root_path(lease, &string); |
942 | if (r >= 0) | |
943 | fprintf(f, "ROOT_PATH=%s\n", string); | |
944 | ||
a2ba62c7 | 945 | r = sd_dhcp_lease_get_routes(lease, &routes); |
0339cd77 | 946 | if (r > 0) |
a2ba62c7 | 947 | serialize_dhcp_routes(f, "ROUTES", routes, r); |
e1ea665e | 948 | |
8eb9058d LP |
949 | r = sd_dhcp_lease_get_timezone(lease, &string); |
950 | if (r >= 0) | |
951 | fprintf(f, "TIMEZONE=%s\n", string); | |
952 | ||
e37f74a6 DW |
953 | r = sd_dhcp_lease_get_client_id(lease, &client_id, &client_id_len); |
954 | if (r >= 0) { | |
3587161a | 955 | _cleanup_free_ char *client_id_hex = NULL; |
e37f74a6 | 956 | |
dde8bb32 | 957 | client_id_hex = hexmem(client_id, client_id_len); |
e37f74a6 DW |
958 | if (!client_id_hex) { |
959 | r = -ENOMEM; | |
dacd6cee | 960 | goto fail; |
e37f74a6 DW |
961 | } |
962 | fprintf(f, "CLIENTID=%s\n", client_id_hex); | |
963 | } | |
964 | ||
e43a8393 BG |
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; | |
dacd6cee | 972 | goto fail; |
e43a8393 BG |
973 | } |
974 | fprintf(f, "VENDOR_SPECIFIC=%s\n", option_hex); | |
975 | } | |
976 | ||
a073309f | 977 | LIST_FOREACH(options, option, lease->private_options) { |
dbcb4a90 | 978 | char key[STRLEN("OPTION_000")+1]; |
0339cd77 | 979 | |
d054f0a4 | 980 | xsprintf(key, "OPTION_%" PRIu8, option->tag); |
a073309f AC |
981 | r = serialize_dhcp_option(f, key, option->data, option->length); |
982 | if (r < 0) | |
983 | goto fail; | |
984 | } | |
985 | ||
dacd6cee LP |
986 | r = fflush_and_check(f); |
987 | if (r < 0) | |
988 | goto fail; | |
fe8db0c5 | 989 | |
dacd6cee | 990 | if (rename(temp_path, lease_file) < 0) { |
fe8db0c5 | 991 | r = -errno; |
dacd6cee | 992 | goto fail; |
fe8db0c5 TG |
993 | } |
994 | ||
dacd6cee LP |
995 | return 0; |
996 | ||
997 | fail: | |
998 | if (temp_path) | |
999 | (void) unlink(temp_path); | |
fe8db0c5 | 1000 | |
dacd6cee | 1001 | return log_error_errno(r, "Failed to save lease data %s: %m", lease_file); |
fe8db0c5 TG |
1002 | } |
1003 | ||
bd91b83e | 1004 | int dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) { |
0339cd77 | 1005 | |
4afd3348 | 1006 | _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL; |
0339cd77 LP |
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, | |
b85bc551 | 1018 | *domains = NULL, |
0339cd77 LP |
1019 | *client_id_hex = NULL, |
1020 | *vendor_specific_hex = NULL, | |
1021 | *lifetime = NULL, | |
1022 | *t1 = NULL, | |
1023 | *t2 = NULL, | |
22805d92 | 1024 | *options[SD_DHCP_OPTION_PRIVATE_LAST - SD_DHCP_OPTION_PRIVATE_BASE + 1] = {}; |
0339cd77 | 1025 | |
a073309f | 1026 | int r, i; |
fe8db0c5 TG |
1027 | |
1028 | assert(lease_file); | |
1029 | assert(ret); | |
1030 | ||
1031 | r = dhcp_lease_new(&lease); | |
1032 | if (r < 0) | |
1033 | return r; | |
1034 | ||
1a5a177e | 1035 | r = parse_env_file(NULL, lease_file, NEWLINE, |
fe8db0c5 TG |
1036 | "ADDRESS", &address, |
1037 | "ROUTER", &router, | |
1038 | "NETMASK", &netmask, | |
0ad853bc | 1039 | "SERVER_IDENTIFIER", &server_address, |
8e34a618 | 1040 | "NEXT_SERVER", &next_server, |
0339cd77 | 1041 | "BROADCAST", &broadcast, |
109731eb TG |
1042 | "DNS", &dns, |
1043 | "NTP", &ntp, | |
fe8db0c5 TG |
1044 | "MTU", &mtu, |
1045 | "DOMAINNAME", &lease->domainname, | |
1046 | "HOSTNAME", &lease->hostname, | |
b85bc551 | 1047 | "DOMAIN_SEARCH_LIST", &domains, |
ce78df79 | 1048 | "ROOT_PATH", &lease->root_path, |
e1ea665e | 1049 | "ROUTES", &routes, |
e37f74a6 | 1050 | "CLIENTID", &client_id_hex, |
8eb9058d | 1051 | "TIMEZONE", &lease->timezone, |
e43a8393 | 1052 | "VENDOR_SPECIFIC", &vendor_specific_hex, |
0339cd77 LP |
1053 | "LIFETIME", &lifetime, |
1054 | "T1", &t1, | |
1055 | "T2", &t2, | |
a073309f AC |
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], | |
fe8db0c5 | 1087 | NULL); |
fe8db0c5 TG |
1088 | if (r < 0) |
1089 | return r; | |
1090 | ||
0339cd77 LP |
1091 | if (address) { |
1092 | r = inet_pton(AF_INET, address, &lease->address); | |
1093 | if (r <= 0) | |
e26ea7fc | 1094 | log_debug("Failed to parse address %s, ignoring.", address); |
0339cd77 | 1095 | } |
fe8db0c5 | 1096 | |
8ddbeaa2 | 1097 | if (router) { |
0339cd77 LP |
1098 | r = inet_pton(AF_INET, router, &lease->router); |
1099 | if (r <= 0) | |
e26ea7fc | 1100 | log_debug("Failed to parse router %s, ignoring.", router); |
8ddbeaa2 | 1101 | } |
fe8db0c5 | 1102 | |
0339cd77 LP |
1103 | if (netmask) { |
1104 | r = inet_pton(AF_INET, netmask, &lease->subnet_mask); | |
1105 | if (r <= 0) | |
e26ea7fc | 1106 | log_debug("Failed to parse netmask %s, ignoring.", netmask); |
0339cd77 LP |
1107 | else |
1108 | lease->have_subnet_mask = true; | |
1109 | } | |
fe8db0c5 | 1110 | |
0ad853bc | 1111 | if (server_address) { |
0339cd77 LP |
1112 | r = inet_pton(AF_INET, server_address, &lease->server_address); |
1113 | if (r <= 0) | |
e26ea7fc | 1114 | log_debug("Failed to parse server address %s, ignoring.", server_address); |
0ad853bc TG |
1115 | } |
1116 | ||
8e34a618 | 1117 | if (next_server) { |
0339cd77 LP |
1118 | r = inet_pton(AF_INET, next_server, &lease->next_server); |
1119 | if (r <= 0) | |
e26ea7fc | 1120 | log_debug("Failed to parse next server %s, ignoring.", next_server); |
0339cd77 | 1121 | } |
8e34a618 | 1122 | |
0339cd77 LP |
1123 | if (broadcast) { |
1124 | r = inet_pton(AF_INET, broadcast, &lease->broadcast); | |
1125 | if (r <= 0) | |
e26ea7fc | 1126 | log_debug("Failed to parse broadcast address %s, ignoring.", broadcast); |
0339cd77 LP |
1127 | else |
1128 | lease->have_broadcast = true; | |
8e34a618 TG |
1129 | } |
1130 | ||
109731eb | 1131 | if (dns) { |
a2ba62c7 | 1132 | r = deserialize_in_addrs(&lease->dns, dns); |
109731eb | 1133 | if (r < 0) |
0339cd77 LP |
1134 | log_debug_errno(r, "Failed to deserialize DNS servers %s, ignoring: %m", dns); |
1135 | else | |
1136 | lease->dns_size = r; | |
109731eb TG |
1137 | } |
1138 | ||
1139 | if (ntp) { | |
a2ba62c7 | 1140 | r = deserialize_in_addrs(&lease->ntp, ntp); |
109731eb | 1141 | if (r < 0) |
0339cd77 LP |
1142 | log_debug_errno(r, "Failed to deserialize NTP servers %s, ignoring: %m", ntp); |
1143 | else | |
1144 | lease->ntp_size = r; | |
109731eb TG |
1145 | } |
1146 | ||
fe8db0c5 | 1147 | if (mtu) { |
0339cd77 LP |
1148 | r = safe_atou16(mtu, &lease->mtu); |
1149 | if (r < 0) | |
1150 | log_debug_errno(r, "Failed to parse MTU %s, ignoring: %m", mtu); | |
fe8db0c5 TG |
1151 | } |
1152 | ||
b85bc551 DW |
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 | ||
e1ea665e | 1165 | if (routes) { |
0339cd77 LP |
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); | |
e1ea665e | 1189 | if (r < 0) |
0339cd77 | 1190 | log_debug_errno(r, "Failed to parse T2 %s, ignoring: %m", t2); |
e1ea665e EY |
1191 | } |
1192 | ||
e37f74a6 | 1193 | if (client_id_hex) { |
ce088d6f | 1194 | r = unhexmem(client_id_hex, (size_t) -1, &lease->client_id, &lease->client_id_len); |
30494563 | 1195 | if (r < 0) |
0339cd77 | 1196 | log_debug_errno(r, "Failed to parse client ID %s, ignoring: %m", client_id_hex); |
e37f74a6 DW |
1197 | } |
1198 | ||
e43a8393 | 1199 | if (vendor_specific_hex) { |
ce088d6f | 1200 | r = unhexmem(vendor_specific_hex, (size_t) -1, &lease->vendor_specific, &lease->vendor_specific_len); |
e43a8393 | 1201 | if (r < 0) |
0339cd77 | 1202 | log_debug_errno(r, "Failed to parse vendor specific data %s, ignoring: %m", vendor_specific_hex); |
e43a8393 BG |
1203 | } |
1204 | ||
22805d92 | 1205 | for (i = 0; i <= SD_DHCP_OPTION_PRIVATE_LAST - SD_DHCP_OPTION_PRIVATE_BASE; i++) { |
e4735228 | 1206 | _cleanup_free_ void *data = NULL; |
a073309f AC |
1207 | size_t len; |
1208 | ||
1209 | if (!options[i]) | |
1210 | continue; | |
1211 | ||
ce088d6f | 1212 | r = unhexmem(options[i], (size_t) -1, &data, &len); |
0339cd77 LP |
1213 | if (r < 0) { |
1214 | log_debug_errno(r, "Failed to parse private DHCP option %s, ignoring: %m", options[i]); | |
1215 | continue; | |
1216 | } | |
a073309f | 1217 | |
22805d92 | 1218 | r = dhcp_lease_insert_private_option(lease, SD_DHCP_OPTION_PRIVATE_BASE + i, data, len); |
626be147 | 1219 | if (r < 0) |
a073309f AC |
1220 | return r; |
1221 | } | |
1222 | ||
1cc6c93a | 1223 | *ret = TAKE_PTR(lease); |
fe8db0c5 TG |
1224 | |
1225 | return 0; | |
1226 | } | |
9e64dd72 TG |
1227 | |
1228 | int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease *lease) { | |
0339cd77 | 1229 | struct in_addr address, mask; |
df40eee8 | 1230 | int r; |
9e64dd72 TG |
1231 | |
1232 | assert(lease); | |
9e64dd72 | 1233 | |
0339cd77 LP |
1234 | if (lease->address == 0) |
1235 | return -ENODATA; | |
1236 | ||
df40eee8 | 1237 | address.s_addr = lease->address; |
9e64dd72 TG |
1238 | |
1239 | /* fall back to the default subnet masks based on address class */ | |
5a941f5f | 1240 | r = in4_addr_default_subnet_mask(&address, &mask); |
df40eee8 TG |
1241 | if (r < 0) |
1242 | return r; | |
9e64dd72 | 1243 | |
df40eee8 | 1244 | lease->subnet_mask = mask.s_addr; |
0339cd77 | 1245 | lease->have_subnet_mask = true; |
9e64dd72 TG |
1246 | |
1247 | return 0; | |
1248 | } | |
e37f74a6 | 1249 | |
0339cd77 | 1250 | int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const void **client_id, size_t *client_id_len) { |
e37f74a6 DW |
1251 | assert_return(lease, -EINVAL); |
1252 | assert_return(client_id, -EINVAL); | |
1253 | assert_return(client_id_len, -EINVAL); | |
1254 | ||
0339cd77 LP |
1255 | if (!lease->client_id) |
1256 | return -ENODATA; | |
1257 | ||
e37f74a6 DW |
1258 | *client_id = lease->client_id; |
1259 | *client_id_len = lease->client_id_len; | |
0339cd77 | 1260 | |
e37f74a6 DW |
1261 | return 0; |
1262 | } | |
1263 | ||
0339cd77 | 1264 | int dhcp_lease_set_client_id(sd_dhcp_lease *lease, const void *client_id, size_t client_id_len) { |
e37f74a6 | 1265 | assert_return(lease, -EINVAL); |
0339cd77 LP |
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; | |
e37f74a6 | 1272 | |
0339cd77 LP |
1273 | p = memdup(client_id, client_id_len); |
1274 | if (!p) | |
1275 | return -ENOMEM; | |
e37f74a6 | 1276 | |
0339cd77 LP |
1277 | free(lease->client_id); |
1278 | lease->client_id = p; | |
e37f74a6 DW |
1279 | lease->client_id_len = client_id_len; |
1280 | } | |
1281 | ||
1282 | return 0; | |
1283 | } | |
8eb9058d | 1284 | |
64d6c229 | 1285 | int sd_dhcp_lease_get_timezone(sd_dhcp_lease *lease, const char **tz) { |
8eb9058d | 1286 | assert_return(lease, -EINVAL); |
64d6c229 | 1287 | assert_return(tz, -EINVAL); |
8eb9058d LP |
1288 | |
1289 | if (!lease->timezone) | |
0339cd77 | 1290 | return -ENODATA; |
8eb9058d | 1291 | |
64d6c229 | 1292 | *tz = lease->timezone; |
8eb9058d LP |
1293 | return 0; |
1294 | } | |
f8693fc7 BG |
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 | } |