]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/dhcp6-option.c
mkosi: Add explicit --bootable=no openSUSE dependencies
[thirdparty/systemd.git] / src / libsystemd-network / dhcp6-option.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2014-2015 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <errno.h>
7 #include <netinet/in.h>
8
9 #include "sd-dhcp6-client.h"
10
11 #include "alloc-util.h"
12 #include "dhcp6-internal.h"
13 #include "dhcp6-lease-internal.h"
14 #include "dhcp6-protocol.h"
15 #include "dns-domain.h"
16 #include "memory-util.h"
17 #include "sparse-endian.h"
18 #include "strv.h"
19 #include "unaligned.h"
20
21 typedef struct DHCP6StatusOption {
22 struct DHCP6Option option;
23 be16_t status;
24 char msg[];
25 } _packed_ DHCP6StatusOption;
26
27 typedef struct DHCP6AddressOption {
28 struct DHCP6Option option;
29 struct iaaddr iaaddr;
30 uint8_t options[];
31 } _packed_ DHCP6AddressOption;
32
33 typedef struct DHCP6PDPrefixOption {
34 struct DHCP6Option option;
35 struct iapdprefix iapdprefix;
36 uint8_t options[];
37 } _packed_ DHCP6PDPrefixOption;
38
39 #define DHCP6_OPTION_IA_NA_LEN (sizeof(struct ia_na))
40 #define DHCP6_OPTION_IA_PD_LEN (sizeof(struct ia_pd))
41 #define DHCP6_OPTION_IA_TA_LEN (sizeof(struct ia_ta))
42
43 static int option_append_hdr(uint8_t **buf, size_t *buflen, uint16_t optcode,
44 size_t optlen) {
45 DHCP6Option *option = (DHCP6Option*) *buf;
46
47 assert_return(buf, -EINVAL);
48 assert_return(*buf, -EINVAL);
49 assert_return(buflen, -EINVAL);
50
51 if (optlen > 0xffff || *buflen < optlen + offsetof(DHCP6Option, data))
52 return -ENOBUFS;
53
54 option->code = htobe16(optcode);
55 option->len = htobe16(optlen);
56
57 *buf += offsetof(DHCP6Option, data);
58 *buflen -= offsetof(DHCP6Option, data);
59
60 return 0;
61 }
62
63 int dhcp6_option_append(uint8_t **buf, size_t *buflen, uint16_t code,
64 size_t optlen, const void *optval) {
65 int r;
66
67 assert_return(optval || optlen == 0, -EINVAL);
68
69 r = option_append_hdr(buf, buflen, code, optlen);
70 if (r < 0)
71 return r;
72
73 memcpy_safe(*buf, optval, optlen);
74
75 *buf += optlen;
76 *buflen -= optlen;
77
78 return 0;
79 }
80
81 int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, const DHCP6IA *ia) {
82 uint16_t len;
83 uint8_t *ia_hdr;
84 size_t iaid_offset, ia_buflen, ia_addrlen = 0;
85 DHCP6Address *addr;
86 int r;
87
88 assert_return(buf, -EINVAL);
89 assert_return(*buf, -EINVAL);
90 assert_return(buflen, -EINVAL);
91 assert_return(ia, -EINVAL);
92
93 switch (ia->type) {
94 case SD_DHCP6_OPTION_IA_NA:
95 len = DHCP6_OPTION_IA_NA_LEN;
96 iaid_offset = offsetof(DHCP6IA, ia_na);
97 break;
98
99 case SD_DHCP6_OPTION_IA_TA:
100 len = DHCP6_OPTION_IA_TA_LEN;
101 iaid_offset = offsetof(DHCP6IA, ia_ta);
102 break;
103
104 default:
105 return -EINVAL;
106 }
107
108 if (*buflen < offsetof(DHCP6Option, data) + len)
109 return -ENOBUFS;
110
111 ia_hdr = *buf;
112 ia_buflen = *buflen;
113
114 *buf += offsetof(DHCP6Option, data);
115 *buflen -= offsetof(DHCP6Option, data);
116
117 memcpy(*buf, (char*) ia + iaid_offset, len);
118
119 *buf += len;
120 *buflen -= len;
121
122 LIST_FOREACH(addresses, addr, ia->addresses) {
123 r = option_append_hdr(buf, buflen, SD_DHCP6_OPTION_IAADDR,
124 sizeof(addr->iaaddr));
125 if (r < 0)
126 return r;
127
128 memcpy(*buf, &addr->iaaddr, sizeof(addr->iaaddr));
129
130 *buf += sizeof(addr->iaaddr);
131 *buflen -= sizeof(addr->iaaddr);
132
133 ia_addrlen += offsetof(DHCP6Option, data) + sizeof(addr->iaaddr);
134 }
135
136 r = option_append_hdr(&ia_hdr, &ia_buflen, ia->type, len + ia_addrlen);
137 if (r < 0)
138 return r;
139
140 return 0;
141 }
142
143 int dhcp6_option_append_fqdn(uint8_t **buf, size_t *buflen, const char *fqdn) {
144 uint8_t buffer[1 + DNS_WIRE_FORMAT_HOSTNAME_MAX];
145 int r;
146
147 assert_return(buf && *buf && buflen && fqdn, -EINVAL);
148
149 buffer[0] = DHCP6_FQDN_FLAG_S; /* Request server to perform AAAA RR DNS updates */
150
151 /* Store domain name after flags field */
152 r = dns_name_to_wire_format(fqdn, buffer + 1, sizeof(buffer) - 1, false);
153 if (r <= 0)
154 return r;
155
156 /*
157 * According to RFC 4704, chapter 4.2 only add terminating zero-length
158 * label in case a FQDN is provided. Since dns_name_to_wire_format
159 * always adds terminating zero-length label remove if only a hostname
160 * is provided.
161 */
162 if (dns_name_is_single_label(fqdn))
163 r--;
164
165 r = dhcp6_option_append(buf, buflen, SD_DHCP6_OPTION_FQDN, 1 + r, buffer);
166
167 return r;
168 }
169
170 int dhcp6_option_append_pd(uint8_t *buf, size_t len, const DHCP6IA *pd, DHCP6Address *hint_pd_prefix) {
171 DHCP6Option *option = (DHCP6Option *)buf;
172 size_t i = sizeof(*option) + sizeof(pd->ia_pd);
173 DHCP6PDPrefixOption *prefix_opt;
174 DHCP6Address *prefix;
175
176 assert_return(buf, -EINVAL);
177 assert_return(pd, -EINVAL);
178 assert_return(pd->type == SD_DHCP6_OPTION_IA_PD, -EINVAL);
179
180 if (len < i)
181 return -ENOBUFS;
182
183 option->code = htobe16(SD_DHCP6_OPTION_IA_PD);
184
185 memcpy(&option->data, &pd->ia_pd, sizeof(pd->ia_pd));
186 LIST_FOREACH(addresses, prefix, pd->addresses) {
187 if (len < i + sizeof(*prefix_opt))
188 return -ENOBUFS;
189
190 prefix_opt = (DHCP6PDPrefixOption *)&buf[i];
191 prefix_opt->option.code = htobe16(SD_DHCP6_OPTION_IA_PD_PREFIX);
192 prefix_opt->option.len = htobe16(sizeof(prefix_opt->iapdprefix));
193
194 memcpy(&prefix_opt->iapdprefix, &prefix->iapdprefix, sizeof(struct iapdprefix));
195 i += sizeof(*prefix_opt);
196 }
197
198 if (hint_pd_prefix && hint_pd_prefix->iapdprefix.prefixlen > 0) {
199 if (len < i + sizeof(*prefix_opt))
200 return -ENOBUFS;
201
202 prefix_opt = (DHCP6PDPrefixOption *)&buf[i];
203 prefix_opt->option.code = htobe16(SD_DHCP6_OPTION_IA_PD_PREFIX);
204 prefix_opt->option.len = htobe16(sizeof(prefix_opt->iapdprefix));
205
206 memcpy(&prefix_opt->iapdprefix, &hint_pd_prefix->iapdprefix, sizeof(struct iapdprefix));
207 i += sizeof(*prefix_opt);
208 }
209
210 option->len = htobe16(i - sizeof(*option));
211
212 return i;
213 }
214
215 static int option_parse_hdr(uint8_t **buf, size_t *buflen, uint16_t *optcode, size_t *optlen) {
216 DHCP6Option *option = (DHCP6Option*) *buf;
217 uint16_t len;
218
219 assert_return(buf, -EINVAL);
220 assert_return(optcode, -EINVAL);
221 assert_return(optlen, -EINVAL);
222
223 if (*buflen < offsetof(DHCP6Option, data))
224 return -ENOMSG;
225
226 len = be16toh(option->len);
227
228 if (len > *buflen)
229 return -ENOMSG;
230
231 *optcode = be16toh(option->code);
232 *optlen = len;
233
234 *buf += 4;
235 *buflen -= 4;
236
237 return 0;
238 }
239
240 int dhcp6_option_parse(uint8_t **buf, size_t *buflen, uint16_t *optcode,
241 size_t *optlen, uint8_t **optvalue) {
242 int r;
243
244 assert_return(buf && buflen && optcode && optlen && optvalue, -EINVAL);
245
246 r = option_parse_hdr(buf, buflen, optcode, optlen);
247 if (r < 0)
248 return r;
249
250 if (*optlen > *buflen)
251 return -ENOBUFS;
252
253 *optvalue = *buf;
254 *buflen -= *optlen;
255 *buf += *optlen;
256
257 return 0;
258 }
259
260 int dhcp6_option_parse_status(DHCP6Option *option, size_t len) {
261 DHCP6StatusOption *statusopt = (DHCP6StatusOption *)option;
262
263 if (len < sizeof(DHCP6StatusOption) ||
264 be16toh(option->len) + offsetof(DHCP6Option, data) < sizeof(DHCP6StatusOption))
265 return -ENOBUFS;
266
267 return be16toh(statusopt->status);
268 }
269
270 static int dhcp6_option_parse_address(DHCP6Option *option, DHCP6IA *ia,
271 uint32_t *lifetime_valid) {
272 DHCP6AddressOption *addr_option = (DHCP6AddressOption *)option;
273 DHCP6Address *addr;
274 uint32_t lt_valid, lt_pref;
275 int r;
276
277 if (be16toh(option->len) + offsetof(DHCP6Option, data) < sizeof(*addr_option))
278 return -ENOBUFS;
279
280 lt_valid = be32toh(addr_option->iaaddr.lifetime_valid);
281 lt_pref = be32toh(addr_option->iaaddr.lifetime_preferred);
282
283 if (lt_valid == 0 || lt_pref > lt_valid) {
284 log_dhcp6_client(client, "Valid lifetime of an IA address is zero or preferred lifetime %d > valid lifetime %d",
285 lt_pref, lt_valid);
286
287 return 0;
288 }
289
290 if (be16toh(option->len) + offsetof(DHCP6Option, data) > sizeof(*addr_option)) {
291 r = dhcp6_option_parse_status((DHCP6Option *)addr_option->options, be16toh(option->len) + offsetof(DHCP6Option, data) - sizeof(*addr_option));
292 if (r != 0)
293 return r < 0 ? r: 0;
294 }
295
296 addr = new0(DHCP6Address, 1);
297 if (!addr)
298 return -ENOMEM;
299
300 LIST_INIT(addresses, addr);
301 memcpy(&addr->iaaddr, option->data, sizeof(addr->iaaddr));
302
303 LIST_PREPEND(addresses, ia->addresses, addr);
304
305 *lifetime_valid = be32toh(addr->iaaddr.lifetime_valid);
306
307 return 0;
308 }
309
310 static int dhcp6_option_parse_pdprefix(DHCP6Option *option, DHCP6IA *ia,
311 uint32_t *lifetime_valid) {
312 DHCP6PDPrefixOption *pdprefix_option = (DHCP6PDPrefixOption *)option;
313 DHCP6Address *prefix;
314 uint32_t lt_valid, lt_pref;
315 int r;
316
317 if (be16toh(option->len) + offsetof(DHCP6Option, data) < sizeof(*pdprefix_option))
318 return -ENOBUFS;
319
320 lt_valid = be32toh(pdprefix_option->iapdprefix.lifetime_valid);
321 lt_pref = be32toh(pdprefix_option->iapdprefix.lifetime_preferred);
322
323 if (lt_valid == 0 || lt_pref > lt_valid) {
324 log_dhcp6_client(client, "Valid lifetieme of a PD prefix is zero or preferred lifetime %d > valid lifetime %d",
325 lt_pref, lt_valid);
326
327 return 0;
328 }
329
330 if (be16toh(option->len) + offsetof(DHCP6Option, data) > sizeof(*pdprefix_option)) {
331 r = dhcp6_option_parse_status((DHCP6Option *)pdprefix_option->options, be16toh(option->len) + offsetof(DHCP6Option, data) - sizeof(*pdprefix_option));
332 if (r != 0)
333 return r < 0 ? r: 0;
334 }
335
336 prefix = new0(DHCP6Address, 1);
337 if (!prefix)
338 return -ENOMEM;
339
340 LIST_INIT(addresses, prefix);
341 memcpy(&prefix->iapdprefix, option->data, sizeof(prefix->iapdprefix));
342
343 LIST_PREPEND(addresses, ia->addresses, prefix);
344
345 *lifetime_valid = be32toh(prefix->iapdprefix.lifetime_valid);
346
347 return 0;
348 }
349
350 int dhcp6_option_parse_ia(DHCP6Option *iaoption, DHCP6IA *ia) {
351 uint16_t iatype, optlen;
352 size_t i, len;
353 int r = 0, status;
354 uint16_t opt;
355 size_t iaaddr_offset;
356 uint32_t lt_t1, lt_t2, lt_valid = 0, lt_min = UINT32_MAX;
357
358 assert_return(ia, -EINVAL);
359 assert_return(!ia->addresses, -EINVAL);
360
361 iatype = be16toh(iaoption->code);
362 len = be16toh(iaoption->len);
363
364 switch (iatype) {
365 case SD_DHCP6_OPTION_IA_NA:
366
367 if (len < DHCP6_OPTION_IA_NA_LEN)
368 return -ENOBUFS;
369
370 iaaddr_offset = DHCP6_OPTION_IA_NA_LEN;
371 memcpy(&ia->ia_na, iaoption->data, sizeof(ia->ia_na));
372
373 lt_t1 = be32toh(ia->ia_na.lifetime_t1);
374 lt_t2 = be32toh(ia->ia_na.lifetime_t2);
375
376 if (lt_t1 && lt_t2 && lt_t1 > lt_t2) {
377 log_dhcp6_client(client, "IA NA T1 %ds > T2 %ds",
378 lt_t1, lt_t2);
379 return -EINVAL;
380 }
381
382 break;
383
384 case SD_DHCP6_OPTION_IA_PD:
385
386 if (len < sizeof(ia->ia_pd))
387 return -ENOBUFS;
388
389 iaaddr_offset = sizeof(ia->ia_pd);
390 memcpy(&ia->ia_pd, iaoption->data, sizeof(ia->ia_pd));
391
392 lt_t1 = be32toh(ia->ia_pd.lifetime_t1);
393 lt_t2 = be32toh(ia->ia_pd.lifetime_t2);
394
395 if (lt_t1 && lt_t2 && lt_t1 > lt_t2) {
396 log_dhcp6_client(client, "IA PD T1 %ds > T2 %ds",
397 lt_t1, lt_t2);
398 return -EINVAL;
399 }
400
401 break;
402
403 case SD_DHCP6_OPTION_IA_TA:
404 if (len < DHCP6_OPTION_IA_TA_LEN)
405 return -ENOBUFS;
406
407 iaaddr_offset = DHCP6_OPTION_IA_TA_LEN;
408 memcpy(&ia->ia_ta.id, iaoption->data, sizeof(ia->ia_ta));
409
410 break;
411
412 default:
413 return -ENOMSG;
414 }
415
416 ia->type = iatype;
417 i = iaaddr_offset;
418
419 while (i < len) {
420 DHCP6Option *option = (DHCP6Option *)&iaoption->data[i];
421
422 if (len < i + sizeof(*option) || len < i + sizeof(*option) + be16toh(option->len))
423 return -ENOBUFS;
424
425 opt = be16toh(option->code);
426 optlen = be16toh(option->len);
427
428 switch (opt) {
429 case SD_DHCP6_OPTION_IAADDR:
430
431 if (!IN_SET(ia->type, SD_DHCP6_OPTION_IA_NA, SD_DHCP6_OPTION_IA_TA)) {
432 log_dhcp6_client(client, "IA Address option not in IA NA or TA option");
433 return -EINVAL;
434 }
435
436 r = dhcp6_option_parse_address(option, ia, &lt_valid);
437 if (r < 0)
438 return r;
439
440 if (lt_valid < lt_min)
441 lt_min = lt_valid;
442
443 break;
444
445 case SD_DHCP6_OPTION_IA_PD_PREFIX:
446
447 if (!IN_SET(ia->type, SD_DHCP6_OPTION_IA_PD)) {
448 log_dhcp6_client(client, "IA PD Prefix option not in IA PD option");
449 return -EINVAL;
450 }
451
452 r = dhcp6_option_parse_pdprefix(option, ia, &lt_valid);
453 if (r < 0)
454 return r;
455
456 if (lt_valid < lt_min)
457 lt_min = lt_valid;
458
459 break;
460
461 case SD_DHCP6_OPTION_STATUS_CODE:
462
463 status = dhcp6_option_parse_status(option, optlen + offsetof(DHCP6Option, data));
464 if (status < 0)
465 return status;
466 if (status > 0) {
467 log_dhcp6_client(client, "IA status %s",
468 dhcp6_message_status_to_string(status));
469
470 return -EINVAL;
471 }
472
473 break;
474
475 default:
476 log_dhcp6_client(client, "Unknown IA option %d", opt);
477 break;
478 }
479
480 i += sizeof(*option) + optlen;
481 }
482
483 switch(iatype) {
484 case SD_DHCP6_OPTION_IA_NA:
485 if (!ia->ia_na.lifetime_t1 && !ia->ia_na.lifetime_t2) {
486 lt_t1 = lt_min / 2;
487 lt_t2 = lt_min / 10 * 8;
488 ia->ia_na.lifetime_t1 = htobe32(lt_t1);
489 ia->ia_na.lifetime_t2 = htobe32(lt_t2);
490
491 log_dhcp6_client(client, "Computed IA NA T1 %ds and T2 %ds as both were zero",
492 lt_t1, lt_t2);
493 }
494
495 break;
496
497 case SD_DHCP6_OPTION_IA_PD:
498 if (!ia->ia_pd.lifetime_t1 && !ia->ia_pd.lifetime_t2) {
499 lt_t1 = lt_min / 2;
500 lt_t2 = lt_min / 10 * 8;
501 ia->ia_pd.lifetime_t1 = htobe32(lt_t1);
502 ia->ia_pd.lifetime_t2 = htobe32(lt_t2);
503
504 log_dhcp6_client(client, "Computed IA PD T1 %ds and T2 %ds as both were zero",
505 lt_t1, lt_t2);
506 }
507
508 break;
509
510 default:
511 break;
512 }
513
514 return 0;
515 }
516
517 int dhcp6_option_parse_ip6addrs(uint8_t *optval, uint16_t optlen,
518 struct in6_addr **addrs, size_t count,
519 size_t *allocated) {
520
521 if (optlen == 0 || optlen % sizeof(struct in6_addr) != 0)
522 return -EINVAL;
523
524 if (!GREEDY_REALLOC(*addrs, *allocated,
525 count * sizeof(struct in6_addr) + optlen))
526 return -ENOMEM;
527
528 memcpy(*addrs + count, optval, optlen);
529
530 count += optlen / sizeof(struct in6_addr);
531
532 return count;
533 }
534
535 int dhcp6_option_parse_domainname(const uint8_t *optval, uint16_t optlen, char ***str_arr) {
536 size_t pos = 0, idx = 0;
537 _cleanup_strv_free_ char **names = NULL;
538 int r;
539
540 assert_return(optlen > 1, -ENODATA);
541 assert_return(optval[optlen - 1] == '\0', -EINVAL);
542
543 while (pos < optlen) {
544 _cleanup_free_ char *ret = NULL;
545 size_t n = 0, allocated = 0;
546 bool first = true;
547
548 for (;;) {
549 const char *label;
550 uint8_t c;
551
552 c = optval[pos++];
553
554 if (c == 0)
555 /* End of name */
556 break;
557 if (c > 63)
558 return -EBADMSG;
559
560 /* Literal label */
561 label = (const char *)&optval[pos];
562 pos += c;
563 if (pos >= optlen)
564 return -EMSGSIZE;
565
566 if (!GREEDY_REALLOC(ret, allocated, n + !first + DNS_LABEL_ESCAPED_MAX))
567 return -ENOMEM;
568
569 if (first)
570 first = false;
571 else
572 ret[n++] = '.';
573
574 r = dns_label_escape(label, c, ret + n, DNS_LABEL_ESCAPED_MAX);
575 if (r < 0)
576 return r;
577
578 n += r;
579 }
580
581 if (n == 0)
582 continue;
583
584 if (!GREEDY_REALLOC(ret, allocated, n + 1))
585 return -ENOMEM;
586
587 ret[n] = 0;
588
589 r = strv_extend(&names, ret);
590 if (r < 0)
591 return r;
592
593 idx++;
594 }
595
596 *str_arr = TAKE_PTR(names);
597
598 return idx;
599 }
600
601 static sd_dhcp6_option* dhcp6_option_free(sd_dhcp6_option *i) {
602 if (!i)
603 return NULL;
604
605 free(i->data);
606 return mfree(i);
607 }
608
609 int sd_dhcp6_option_new(uint16_t option, const void *data, size_t length, sd_dhcp6_option **ret) {
610 assert_return(ret, -EINVAL);
611 assert_return(length == 0 || data, -EINVAL);
612
613 _cleanup_free_ void *q = memdup(data, length);
614 if (!q)
615 return -ENOMEM;
616
617 sd_dhcp6_option *p = new(sd_dhcp6_option, 1);
618 if (!p)
619 return -ENOMEM;
620
621 *p = (sd_dhcp6_option) {
622 .n_ref = 1,
623 .option = option,
624 .length = length,
625 .data = TAKE_PTR(q),
626 };
627
628 *ret = p;
629 return 0;
630 }
631
632 DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp6_option, sd_dhcp6_option, dhcp6_option_free);
633 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
634 dhcp6_option_hash_ops,
635 void,
636 trivial_hash_func,
637 trivial_compare_func,
638 sd_dhcp6_option,
639 sd_dhcp6_option_unref);