Debian Codegraph @

src/libsystemd-network/ndisc-option.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/ipv6.h>
4 #include <netinet/icmp6.h>
5
6 #include "sd-ndisc-protocol.h"
7
8 #include "alloc-util.h"
9 #include "dns-def.h"
10 #include "dns-domain.h"
11 #include "dns-packet.h"
12 #include "dns-resolver-internal.h"
13 #include "ether-addr-util.h"
14 #include "hash-funcs.h"
15 #include "hostname-util.h"
16 #include "icmp6-packet.h"
17 #include "icmp6-util.h"
18 #include "in-addr-util.h"
19 #include "iovec-util.h"
20 #include "ndisc-option.h"
21 #include "network-common.h"
22 #include "set.h"
23 #include "siphash24.h"
24 #include "string-util.h"
25 #include "strv.h"
26 #include "unaligned.h"
27
28 /* RFC does not say anything about the maximum number of options, but let's limit the number of options for
29 * safety. Typically, the number of options in an ICMPv6 message should be only a few. */
30 #define MAX_OPTIONS 128
31
32 int ndisc_option_parse(
33 ICMP6Packet *p,
34 size_t offset,
35 uint8_t *ret_type,
36 size_t *ret_len,
37 const uint8_t **ret_opt) {
38
39 assert(p);
40
41 if (offset == p->raw_size)
42 return -ESPIPE; /* end of the packet */
43
44 if (offset > p->raw_size)
45 return -EBADMSG;
46
47 if (p->raw_size - offset < sizeof(struct nd_opt_hdr))
48 return -EBADMSG;
49
50 assert_cc(alignof(struct nd_opt_hdr) == 1);
51 const struct nd_opt_hdr *hdr = (const struct nd_opt_hdr*) (p->raw_packet + offset);
52 if (hdr->nd_opt_len == 0)
53 return -EBADMSG;
54
55 size_t len = hdr->nd_opt_len * 8;
56 if (p->raw_size - offset < len)
57 return -EBADMSG;
58
59 if (ret_type)
60 *ret_type = hdr->nd_opt_type;
61 if (ret_len)
62 *ret_len = len;
63 if (ret_opt)
64 *ret_opt = p->raw_packet + offset;
65
66 return 0;
67 }
68
69 static sd_ndisc_option* ndisc_option_new(uint8_t type, size_t offset) {
70 sd_ndisc_option *p = new0(sd_ndisc_option, 1); /* use new0() here to make the fuzzers silent. */
71 if (!p)
72 return NULL;
73
74 /* As the same reason in the above, do not use the structured initializer here. */
75 p->type = type;
76 p->offset = offset;
77
78 return p;
79 }
80
81 static void ndisc_raw_done(sd_ndisc_raw *raw) {
82 if (!raw)
83 return;
84
85 free(raw->bytes);
86 }
87
88 static void ndisc_rdnss_done(sd_ndisc_rdnss *rdnss) {
89 if (!rdnss)
90 return;
91
92 free(rdnss->addresses);
93 }
94
95 static void ndisc_dnssl_done(sd_ndisc_dnssl *dnssl) {
96 if (!dnssl)
97 return;
98
99 strv_free(dnssl->domains);
100 }
101
102 static void ndisc_dnr_done(sd_ndisc_dnr *dnr) {
103 if (!dnr)
104 return;
105
106 sd_dns_resolver_unref(dnr->resolver);
107 }
108
109 sd_ndisc_option* ndisc_option_free(sd_ndisc_option *option) {
110 if (!option)
111 return NULL;
112
113 switch (option->type) {
114 case 0:
115 ndisc_raw_done(&option->raw);
116 break;
117
118 case SD_NDISC_OPTION_RDNSS:
119 ndisc_rdnss_done(&option->rdnss);
120 break;
121
122 case SD_NDISC_OPTION_DNSSL:
123 ndisc_dnssl_done(&option->dnssl);
124 break;
125
126 case SD_NDISC_OPTION_CAPTIVE_PORTAL:
127 free(option->captive_portal);
128 break;
129
130 case SD_NDISC_OPTION_ENCRYPTED_DNS:
131 ndisc_dnr_done(&option->encrypted_dns);
132 break;
133 }
134
135 return mfree(option);
136 }
137
138 static int ndisc_option_compare_func(const sd_ndisc_option *x, const sd_ndisc_option *y) {
139 int r;
140
141 assert(x);
142 assert(y);
143
144 r = CMP(x->type, y->type);
145 if (r != 0)
146 return r;
147
148 switch (x->type) {
149 case 0:
150 return memcmp_nn(x->raw.bytes, x->raw.length, y->raw.bytes, y->raw.length);
151
152 case SD_NDISC_OPTION_SOURCE_LL_ADDRESS:
153 case SD_NDISC_OPTION_TARGET_LL_ADDRESS:
154 case SD_NDISC_OPTION_REDIRECTED_HEADER:
155 case SD_NDISC_OPTION_MTU:
156 case SD_NDISC_OPTION_HOME_AGENT:
157 case SD_NDISC_OPTION_FLAGS_EXTENSION:
158 case SD_NDISC_OPTION_CAPTIVE_PORTAL:
159 /* These options cannot be specified multiple times. */
160 return 0;
161
162 case SD_NDISC_OPTION_PREFIX_INFORMATION:
163 /* Should not specify the same prefix multiple times. */
164 r = CMP(x->prefix.prefixlen, y->prefix.prefixlen);
165 if (r != 0)
166 return r;
167
168 return memcmp(&x->prefix.address, &y->prefix.address, sizeof(struct in6_addr));
169
170 case SD_NDISC_OPTION_ROUTE_INFORMATION:
171 r = CMP(x->route.prefixlen, y->route.prefixlen);
172 if (r != 0)
173 return r;
174
175 return memcmp(&x->route.address, &y->route.address, sizeof(struct in6_addr));
176
177 case SD_NDISC_OPTION_PREF64:
178 r = CMP(x->prefix64.prefixlen, y->prefix64.prefixlen);
179 if (r != 0)
180 return r;
181
182 return memcmp(&x->prefix64.prefix, &y->prefix64.prefix, sizeof(struct in6_addr));
183
184 default:
185 /* DNSSL, RDNSS, and other unsupported options can be specified multiple times. */
186 return trivial_compare_func(x, y);
187 }
188 }
189
190 static void ndisc_option_hash_func(const sd_ndisc_option *option, struct siphash *state) {
191 assert(option);
192 assert(state);
193
194 siphash24_compress_typesafe(option->type, state);
195
196 switch (option->type) {
197 case 0:
198 siphash24_compress(option->raw.bytes, option->raw.length, state);
199 break;
200
201 case SD_NDISC_OPTION_SOURCE_LL_ADDRESS:
202 case SD_NDISC_OPTION_TARGET_LL_ADDRESS:
203 case SD_NDISC_OPTION_REDIRECTED_HEADER:
204 case SD_NDISC_OPTION_MTU:
205 case SD_NDISC_OPTION_HOME_AGENT:
206 case SD_NDISC_OPTION_FLAGS_EXTENSION:
207 case SD_NDISC_OPTION_CAPTIVE_PORTAL:
208 break;
209
210 case SD_NDISC_OPTION_PREFIX_INFORMATION:
211 siphash24_compress_typesafe(option->prefix.prefixlen, state);
212 siphash24_compress_typesafe(option->prefix.address, state);
213 break;
214
215 case SD_NDISC_OPTION_ROUTE_INFORMATION:
216 siphash24_compress_typesafe(option->route.prefixlen, state);
217 siphash24_compress_typesafe(option->route.address, state);
218 break;
219
220 case SD_NDISC_OPTION_PREF64:
221 siphash24_compress_typesafe(option->prefix64.prefixlen, state);
222 siphash24_compress_typesafe(option->prefix64.prefix, state);
223 break;
224
225 default:
226 trivial_hash_func(option, state);
227 }
228 }
229
230 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
231 ndisc_option_hash_ops,
232 sd_ndisc_option,
233 ndisc_option_hash_func,
234 ndisc_option_compare_func,
235 ndisc_option_free);
236
237 static int ndisc_option_consume(Set **options, sd_ndisc_option *p) {
238 assert(options);
239 assert(p);
240
241 if (set_size(*options) >= MAX_OPTIONS) {
242 ndisc_option_free(p);
243 return -ETOOMANYREFS; /* recognizable error code */
244 }
245
246 return set_ensure_consume(options, &ndisc_option_hash_ops, p);
247 }
248
249 static int ndisc_option_build_raw(const sd_ndisc_option *option, uint8_t **ret) {
250 assert(option);
251 assert(option->type == 0);
252 assert(ret);
253
254 _cleanup_free_ uint8_t *buf = newdup(uint8_t, option->raw.bytes, option->raw.length);
255 if (!buf)
256 return -ENOMEM;
257
258 *ret = TAKE_PTR(buf);
259 return 0;
260 }
261
262 int ndisc_option_add_link_layer_address(Set **options, uint8_t type, size_t offset, const struct ether_addr *mac) {
263 assert(options);
264 assert(IN_SET(type, SD_NDISC_OPTION_SOURCE_LL_ADDRESS, SD_NDISC_OPTION_TARGET_LL_ADDRESS));
265
266 if (!mac || ether_addr_is_null(mac)) {
267 ndisc_option_remove_by_type(*options, type);
268 return 0;
269 }
270
271 sd_ndisc_option *p = ndisc_option_get_by_type(*options, type);
272 if (p) {
273 /* offset == 0 means that we are now building a packet to be sent, and in that case we allow
274 * to override the option we previously set.
275 * offset != 0 means that we are now parsing a received packet, and we refuse to override
276 * conflicting options. */
277 if (offset != 0)
278 return -EEXIST;
279
280 p->mac = *mac;
281 return 0;
282 }
283
284 p = ndisc_option_new(type, offset);
285 if (!p)
286 return -ENOMEM;
287
288 p->mac = *mac;
289
290 return set_ensure_consume(options, &ndisc_option_hash_ops, p);
291 }
292
293 static int ndisc_option_parse_link_layer_address(Set **options, size_t offset, size_t len, const uint8_t *opt) {
294 assert(options);
295 assert(opt);
296
297 if (len != sizeof(struct ether_addr) + 2)
298 return -EBADMSG;
299
300 if (!IN_SET(opt[0], SD_NDISC_OPTION_SOURCE_LL_ADDRESS, SD_NDISC_OPTION_TARGET_LL_ADDRESS))
301 return -EBADMSG;
302
303 struct ether_addr mac;
304 memcpy(&mac, opt + 2, sizeof(struct ether_addr));
305
306 if (ether_addr_is_null(&mac))
307 return -EBADMSG;
308
309 return ndisc_option_add_link_layer_address(options, opt[0], offset, &mac);
310 }
311
312 static int ndisc_option_build_link_layer_address(const sd_ndisc_option *option, uint8_t **ret) {
313 assert(option);
314 assert(IN_SET(option->type, SD_NDISC_OPTION_SOURCE_LL_ADDRESS, SD_NDISC_OPTION_TARGET_LL_ADDRESS));
315 assert(ret);
316
317 assert_cc(2 + sizeof(struct ether_addr) == 8);
318
319 _cleanup_free_ uint8_t *buf = new(uint8_t, 2 + sizeof(struct ether_addr));
320 if (!buf)
321 return -ENOMEM;
322
323 buf[0] = option->type;
324 buf[1] = 1;
325 memcpy(buf + 2, &option->mac, sizeof(struct ether_addr));
326
327 *ret = TAKE_PTR(buf);
328 return 0;
329 }
330
331 int ndisc_option_add_prefix_internal(
332 Set **options,
333 size_t offset,
334 uint8_t flags,
335 uint8_t prefixlen,
336 const struct in6_addr *address,
337 usec_t valid_lifetime,
338 usec_t preferred_lifetime,
339 usec_t valid_until,
340 usec_t preferred_until) {
341
342 assert(options);
343 assert(address);
344
345 if (prefixlen > 128)
346 return -EINVAL;
347
348 struct in6_addr addr = *address;
349 in6_addr_mask(&addr, prefixlen);
350
351 /* RFC 4861 and 4862 only state that link-local prefix should be ignored.
352 * But here we also ignore null and multicast addresses. */
353 if (in6_addr_is_link_local(&addr) || in6_addr_is_null(&addr) || in6_addr_is_multicast(&addr))
354 return -EINVAL;
355
356 if (preferred_lifetime > valid_lifetime)
357 return -EINVAL;
358
359 if (preferred_until > valid_until)
360 return -EINVAL;
361
362 sd_ndisc_option *p = ndisc_option_get(
363 *options,
364 &(const sd_ndisc_option) {
365 .type = SD_NDISC_OPTION_PREFIX_INFORMATION,
366 .prefix.prefixlen = prefixlen,
367 .prefix.address = addr,
368 });
369 if (p) {
370 if (offset != 0)
371 return -EEXIST;
372
373 p->prefix.flags = flags;
374 p->prefix.valid_lifetime = valid_lifetime;
375 p->prefix.preferred_lifetime = preferred_lifetime;
376 p->prefix.valid_until = valid_until;
377 p->prefix.preferred_until = preferred_until;
378 return 0;
379 }
380
381 p = ndisc_option_new(SD_NDISC_OPTION_PREFIX_INFORMATION, offset);
382 if (!p)
383 return -ENOMEM;
384
385 p->prefix = (sd_ndisc_prefix) {
386 .flags = flags,
387 .prefixlen = prefixlen,
388 .address = addr,
389 .valid_lifetime = valid_lifetime,
390 .preferred_lifetime = preferred_lifetime,
391 .valid_until = valid_until,
392 .preferred_until = preferred_until,
393 };
394
395 return ndisc_option_consume(options, p);
396 }
397
398 static int ndisc_option_parse_prefix(Set **options, size_t offset, size_t len, const uint8_t *opt) {
399 const struct nd_opt_prefix_info *pi = (const struct nd_opt_prefix_info*) ASSERT_PTR(opt);
400
401 assert(options);
402
403 if (len != sizeof(struct nd_opt_prefix_info))
404 return -EBADMSG;
405
406 if (pi->nd_opt_pi_type != SD_NDISC_OPTION_PREFIX_INFORMATION)
407 return -EBADMSG;
408
409 usec_t valid = be32_sec_to_usec(pi->nd_opt_pi_valid_time, /* max_as_infinity= */ true);
410 usec_t pref = be32_sec_to_usec(pi->nd_opt_pi_preferred_time, /* max_as_infinity= */ true);
411
412 /* We only support 64 bits interface identifier for addrconf. */
413 uint8_t flags = pi->nd_opt_pi_flags_reserved;
414 if (FLAGS_SET(flags, ND_OPT_PI_FLAG_AUTO) && pi->nd_opt_pi_prefix_len != 64)
415 flags &= ~ND_OPT_PI_FLAG_AUTO;
416
417 return ndisc_option_add_prefix(options, offset, flags,
418 pi->nd_opt_pi_prefix_len, &pi->nd_opt_pi_prefix,
419 valid, pref);
420 }
421
422 static int ndisc_option_build_prefix(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
423 assert(option);
424 assert(option->type == SD_NDISC_OPTION_PREFIX_INFORMATION);
425 assert(ret);
426
427 assert_cc(sizeof(struct nd_opt_prefix_info) % 8 == 0);
428
429 _cleanup_free_ struct nd_opt_prefix_info *buf = new(struct nd_opt_prefix_info, 1);
430 if (!buf)
431 return -ENOMEM;
432
433 usec_t valid = MIN(option->prefix.valid_lifetime,
434 usec_sub_unsigned(option->prefix.valid_until, timestamp));
435 usec_t pref = MIN3(valid,
436 option->prefix.preferred_lifetime,
437 usec_sub_unsigned(option->prefix.preferred_until, timestamp));
438
439 *buf = (struct nd_opt_prefix_info) {
440 .nd_opt_pi_type = SD_NDISC_OPTION_PREFIX_INFORMATION,
441 .nd_opt_pi_len = sizeof(struct nd_opt_prefix_info) / 8,
442 .nd_opt_pi_prefix_len = option->prefix.prefixlen,
443 .nd_opt_pi_flags_reserved = option->prefix.flags,
444 .nd_opt_pi_valid_time = usec_to_be32_sec(valid),
445 .nd_opt_pi_preferred_time = usec_to_be32_sec(pref),
446 .nd_opt_pi_prefix = option->prefix.address,
447 };
448
449 *ret = (uint8_t*) TAKE_PTR(buf);
450 return 0;
451 }
452
453 int ndisc_option_add_redirected_header(Set **options, size_t offset, const struct ip6_hdr *hdr) {
454 assert(options);
455
456 if (!hdr) {
457 ndisc_option_remove_by_type(*options, SD_NDISC_OPTION_REDIRECTED_HEADER);
458 return 0;
459 }
460
461 sd_ndisc_option *p = ndisc_option_get_by_type(*options, SD_NDISC_OPTION_REDIRECTED_HEADER);
462 if (p) {
463 if (offset != 0)
464 return -EEXIST;
465
466 memcpy(&p->hdr, hdr, sizeof(struct ip6_hdr));
467 return 0;
468 }
469
470 p = ndisc_option_new(SD_NDISC_OPTION_REDIRECTED_HEADER, offset);
471 if (!p)
472 return -ENOMEM;
473
474 /* For safety, here we copy only IPv6 header. */
475 memcpy(&p->hdr, hdr, sizeof(struct ip6_hdr));
476
477 return ndisc_option_consume(options, p);
478 }
479
480 static int ndisc_option_parse_redirected_header(Set **options, size_t offset, size_t len, const uint8_t *opt) {
481 assert(options);
482 assert(opt);
483
484 if (len < sizeof(struct nd_opt_rd_hdr) + sizeof(struct ip6_hdr))
485 return -EBADMSG;
486
487 if (opt[0] != SD_NDISC_OPTION_REDIRECTED_HEADER)
488 return -EBADMSG;
489
490 return ndisc_option_add_redirected_header(options, offset, (const struct ip6_hdr*) (opt + sizeof(struct nd_opt_rd_hdr)));
491 }
492
493 static int ndisc_option_build_redirected_header(const sd_ndisc_option *option, uint8_t **ret) {
494 assert(option);
495 assert(option->type == SD_NDISC_OPTION_REDIRECTED_HEADER);
496 assert(ret);
497
498 assert_cc((sizeof(struct nd_opt_rd_hdr) + sizeof(struct ip6_hdr)) % 8 == 0);
499
500 size_t len = DIV_ROUND_UP(sizeof(struct nd_opt_rd_hdr) + sizeof(struct ip6_hdr), 8);
501
502 _cleanup_free_ uint8_t *buf = new(uint8_t, len * 8);
503 if (!buf)
504 return -ENOMEM;
505
506 uint8_t *p;
507 p = mempcpy(buf,
508 &(const struct nd_opt_rd_hdr) {
509 .nd_opt_rh_type = SD_NDISC_OPTION_REDIRECTED_HEADER,
510 .nd_opt_rh_len = len,
511 },
512 sizeof(struct nd_opt_rd_hdr));
513 memcpy(p, &option->hdr, sizeof(struct ip6_hdr));
514
515 *ret = TAKE_PTR(buf);
516 return 0;
517 }
518
519 int ndisc_option_add_mtu(Set **options, size_t offset, uint32_t mtu) {
520 assert(options);
521
522 if (mtu < IPV6_MIN_MTU)
523 return -EINVAL;
524
525 sd_ndisc_option *p = ndisc_option_get_by_type(*options, SD_NDISC_OPTION_MTU);
526 if (p) {
527 if (offset != 0)
528 return -EEXIST;
529
530 p->mtu = mtu;
531 return 0;
532 }
533
534 p = ndisc_option_new(SD_NDISC_OPTION_MTU, offset);
535 if (!p)
536 return -ENOMEM;
537
538 p->mtu = mtu;
539
540 return ndisc_option_consume(options, p);
541 }
542
543 static int ndisc_option_parse_mtu(Set **options, size_t offset, size_t len, const uint8_t *opt) {
544 const struct nd_opt_mtu *pm = (const struct nd_opt_mtu*) ASSERT_PTR(opt);
545
546 assert(options);
547
548 if (len != sizeof(struct nd_opt_mtu))
549 return -EBADMSG;
550
551 if (pm->nd_opt_mtu_type != SD_NDISC_OPTION_MTU)
552 return -EBADMSG;
553
554 return ndisc_option_add_mtu(options, offset, be32toh(pm->nd_opt_mtu_mtu));
555 }
556
557 static int ndisc_option_build_mtu(const sd_ndisc_option *option, uint8_t **ret) {
558 assert(option);
559 assert(option->type == SD_NDISC_OPTION_MTU);
560 assert(ret);
561
562 assert_cc(sizeof(struct nd_opt_mtu) % 8 == 0);
563
564 _cleanup_free_ struct nd_opt_mtu *buf = new(struct nd_opt_mtu, 1);
565 if (!buf)
566 return -ENOMEM;
567
568 *buf = (struct nd_opt_mtu) {
569 .nd_opt_mtu_type = SD_NDISC_OPTION_MTU,
570 .nd_opt_mtu_len = sizeof(struct nd_opt_mtu) / 8,
571 .nd_opt_mtu_mtu = htobe32(option->mtu),
572 };
573
574 *ret = (uint8_t*) TAKE_PTR(buf);
575 return 0;
576 }
577
578 int ndisc_option_add_home_agent_internal(
579 Set **options,
580 size_t offset,
581 uint16_t preference,
582 usec_t lifetime,
583 usec_t valid_until) {
584
585 assert(options);
586
587 if (lifetime > UINT16_MAX * USEC_PER_SEC)
588 return -EINVAL;
589
590 sd_ndisc_option *p = ndisc_option_get_by_type(*options, SD_NDISC_OPTION_HOME_AGENT);
591 if (p) {
592 if (offset != 0)
593 return -EEXIST;
594
595 p->home_agent = (sd_ndisc_home_agent) {
596 .preference = preference,
597 .lifetime = lifetime,
598 .valid_until = valid_until,
599 };
600 return 0;
601 }
602
603 p = ndisc_option_new(SD_NDISC_OPTION_HOME_AGENT, offset);
604 if (!p)
605 return -ENOMEM;
606
607 p->home_agent = (sd_ndisc_home_agent) {
608 .preference = preference,
609 .lifetime = lifetime,
610 .valid_until = valid_until,
611 };
612
613 return ndisc_option_consume(options, p);
614 }
615
616 static int ndisc_option_parse_home_agent(Set **options, size_t offset, size_t len, const uint8_t *opt) {
617 const struct nd_opt_home_agent_info *p = (const struct nd_opt_home_agent_info*) ASSERT_PTR(opt);
618
619 assert(options);
620
621 if (len != sizeof(struct nd_opt_home_agent_info))
622 return -EBADMSG;
623
624 if (p->nd_opt_home_agent_info_type != SD_NDISC_OPTION_HOME_AGENT)
625 return -EBADMSG;
626
627 return ndisc_option_add_home_agent(
628 options, offset,
629 be16toh(p->nd_opt_home_agent_info_preference),
630 be16_sec_to_usec(p->nd_opt_home_agent_info_lifetime, /* max_as_infinity= */ false));
631 }
632
633 static int ndisc_option_build_home_agent(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
634 assert(option);
635 assert(option->type == SD_NDISC_OPTION_HOME_AGENT);
636 assert(ret);
637
638 assert_cc(sizeof(struct nd_opt_home_agent_info) % 8 == 0);
639
640 usec_t lifetime = MIN(option->home_agent.lifetime,
641 usec_sub_unsigned(option->home_agent.valid_until, timestamp));
642
643 _cleanup_free_ struct nd_opt_home_agent_info *buf = new(struct nd_opt_home_agent_info, 1);
644 if (!buf)
645 return -ENOMEM;
646
647 *buf = (struct nd_opt_home_agent_info) {
648 .nd_opt_home_agent_info_type = SD_NDISC_OPTION_HOME_AGENT,
649 .nd_opt_home_agent_info_len = sizeof(struct nd_opt_home_agent_info) / 8,
650 .nd_opt_home_agent_info_preference = htobe16(option->home_agent.preference),
651 .nd_opt_home_agent_info_lifetime = usec_to_be16_sec(lifetime),
652 };
653
654 *ret = (uint8_t*) TAKE_PTR(buf);
655 return 0;
656 }
657
658 int ndisc_option_add_route_internal(
659 Set **options,
660 size_t offset,
661 uint8_t preference,
662 uint8_t prefixlen,
663 const struct in6_addr *prefix,
664 usec_t lifetime,
665 usec_t valid_until) {
666
667 assert(options);
668 assert(prefix);
669
670 if (prefixlen > 128)
671 return -EINVAL;
672
673 /* RFC 4191 section 2.3
674 * Prf (Route Preference)
675 * 2-bit signed integer. The Route Preference indicates whether to prefer the router associated with
676 * this prefix over others, when multiple identical prefixes (for different routers) have been
677 * received. If the Reserved (10) value is received, the Route Information Option MUST be ignored. */
678 if (!IN_SET(preference, SD_NDISC_PREFERENCE_LOW, SD_NDISC_PREFERENCE_MEDIUM, SD_NDISC_PREFERENCE_HIGH))
679 return -EINVAL;
680
681 struct in6_addr addr = *prefix;
682 in6_addr_mask(&addr, prefixlen);
683
684 sd_ndisc_option *p = ndisc_option_get(
685 *options,
686 &(const sd_ndisc_option) {
687 .type = SD_NDISC_OPTION_ROUTE_INFORMATION,
688 .route.prefixlen = prefixlen,
689 .route.address = addr,
690 });
691 if (p) {
692 if (offset != 0)
693 return -EEXIST;
694
695 p->route.preference = preference;
696 p->route.lifetime = lifetime;
697 p->route.valid_until = valid_until;
698 return 0;
699 }
700
701 p = ndisc_option_new(SD_NDISC_OPTION_ROUTE_INFORMATION, offset);
702 if (!p)
703 return -ENOMEM;
704
705 p->route = (sd_ndisc_route) {
706 .preference = preference,
707 .prefixlen = prefixlen,
708 .address = addr,
709 .lifetime = lifetime,
710 .valid_until = valid_until,
711 };
712
713 return ndisc_option_consume(options, p);
714 }
715
716 static int ndisc_option_parse_route(Set **options, size_t offset, size_t len, const uint8_t *opt) {
717 assert(options);
718 assert(opt);
719
720 if (!IN_SET(len, 1*8, 2*8, 3*8))
721 return -EBADMSG;
722
723 if (opt[0] != SD_NDISC_OPTION_ROUTE_INFORMATION)
724 return -EBADMSG;
725
726 uint8_t prefixlen = opt[2];
727 if (prefixlen > 128)
728 return -EBADMSG;
729
730 if (len < (size_t) (DIV_ROUND_UP(prefixlen, 64) + 1) * 8)
731 return -EBADMSG;
732
733 uint8_t preference = (opt[3] >> 3) & 0x03;
734 usec_t lifetime = unaligned_be32_sec_to_usec(opt + 4, /* max_as_infinity= */ true);
735
736 struct in6_addr prefix;
737 memcpy_safe(&prefix, opt + 8, len - 8);
738 in6_addr_mask(&prefix, prefixlen);
739
740 return ndisc_option_add_route(options, offset, preference, prefixlen, &prefix, lifetime);
741 }
742
743 static int ndisc_option_build_route(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
744 assert(option);
745 assert(option->type == SD_NDISC_OPTION_ROUTE_INFORMATION);
746 assert(option->route.prefixlen <= 128);
747 assert(ret);
748
749 size_t len = 1 + DIV_ROUND_UP(option->route.prefixlen, 64);
750 be32_t lifetime = usec_to_be32_sec(MIN(option->route.lifetime,
751 usec_sub_unsigned(option->route.valid_until, timestamp)));
752
753 _cleanup_free_ uint8_t *buf = new(uint8_t, len * 8);
754 if (!buf)
755 return -ENOMEM;
756
757 buf[0] = SD_NDISC_OPTION_ROUTE_INFORMATION;
758 buf[1] = len;
759 buf[2] = option->route.prefixlen;
760 buf[3] = option->route.preference << 3;
761 memcpy(buf + 4, &lifetime, sizeof(be32_t));
762 memcpy_safe(buf + 8, &option->route.address, (len - 1) * 8);
763
764 *ret = TAKE_PTR(buf);
765 return 0;
766 }
767
768 int ndisc_option_add_rdnss_internal(
769 Set **options,
770 size_t offset,
771 size_t n_addresses,
772 const struct in6_addr *addresses,
773 usec_t lifetime,
774 usec_t valid_until) {
775
776 assert(options);
777 assert(addresses);
778
779 if (n_addresses == 0)
780 return -EINVAL;
781
782 _cleanup_free_ struct in6_addr *addrs = newdup(struct in6_addr, addresses, n_addresses);
783 if (!addrs)
784 return -ENOMEM;
785
786 sd_ndisc_option *p = ndisc_option_new(SD_NDISC_OPTION_RDNSS, offset);
787 if (!p)
788 return -ENOMEM;
789
790 p->rdnss = (sd_ndisc_rdnss) {
791 .n_addresses = n_addresses,
792 .addresses = TAKE_PTR(addrs),
793 .lifetime = lifetime,
794 .valid_until = valid_until,
795 };
796
797 return ndisc_option_consume(options, p);
798 }
799
800 static int ndisc_option_parse_rdnss(Set **options, size_t offset, size_t len, const uint8_t *opt) {
801 assert(options);
802 assert(opt);
803
804 if (len < 8 + sizeof(struct in6_addr) || (len % sizeof(struct in6_addr)) != 8)
805 return -EBADMSG;
806
807 if (opt[0] != SD_NDISC_OPTION_RDNSS)
808 return -EBADMSG;
809
810 usec_t lifetime = unaligned_be32_sec_to_usec(opt + 4, /* max_as_infinity= */ true);
811 size_t n_addrs = len / sizeof(struct in6_addr);
812
813 return ndisc_option_add_rdnss(options, offset, n_addrs, (const struct in6_addr*) (opt + 8), lifetime);
814 }
815
816 static int ndisc_option_build_rdnss(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
817 assert(option);
818 assert(option->type == SD_NDISC_OPTION_RDNSS);
819 assert(ret);
820
821 size_t len = option->rdnss.n_addresses * 2 + 1;
822 be32_t lifetime = usec_to_be32_sec(MIN(option->rdnss.lifetime,
823 usec_sub_unsigned(option->rdnss.valid_until, timestamp)));
824
825 _cleanup_free_ uint8_t *buf = new(uint8_t, len * 8);
826 if (!buf)
827 return -ENOMEM;
828
829 buf[0] = SD_NDISC_OPTION_RDNSS;
830 buf[1] = len;
831 buf[2] = 0;
832 buf[3] = 0;
833 memcpy(buf + 4, &lifetime, sizeof(be32_t));
834 memcpy(buf + 8, option->rdnss.addresses, sizeof(struct in6_addr) * option->rdnss.n_addresses);
835
836 *ret = TAKE_PTR(buf);
837 return 0;
838 }
839
840 int ndisc_option_add_flags_extension(Set **options, size_t offset, uint64_t flags) {
841 assert(options);
842
843 if ((flags & UINT64_C(0x00ffffffffffff00)) != flags)
844 return -EINVAL;
845
846 sd_ndisc_option *p = ndisc_option_get_by_type(*options, SD_NDISC_OPTION_FLAGS_EXTENSION);
847 if (p) {
848 if (offset != 0)
849 return -EEXIST;
850
851 p->extended_flags = flags;
852 return 0;
853 }
854
855 p = ndisc_option_new(SD_NDISC_OPTION_FLAGS_EXTENSION, offset);
856 if (!p)
857 return -ENOMEM;
858
859 p->extended_flags = flags;
860
861 return ndisc_option_consume(options, p);
862 }
863
864 static int ndisc_option_parse_flags_extension(Set **options, size_t offset, size_t len, const uint8_t *opt) {
865 assert(options);
866 assert(opt);
867
868 if (len != 8)
869 return -EBADMSG;
870
871 if (opt[0] != SD_NDISC_OPTION_FLAGS_EXTENSION)
872 return -EBADMSG;
873
874 uint64_t flags = (unaligned_read_be64(opt) & UINT64_C(0xffffffffffff0000)) >> 8;
875 return ndisc_option_add_flags_extension(options, offset, flags);
876 }
877
878 static int ndisc_option_build_flags_extension(const sd_ndisc_option *option, uint8_t **ret) {
879 assert(option);
880 assert(option->type == SD_NDISC_OPTION_FLAGS_EXTENSION);
881 assert(ret);
882
883 _cleanup_free_ uint8_t *buf = new(uint8_t, 8);
884 if (!buf)
885 return 0;
886
887 unaligned_write_be64(buf, (option->extended_flags & UINT64_C(0x00ffffffffffff00)) << 8);
888 buf[0] = SD_NDISC_OPTION_FLAGS_EXTENSION;
889 buf[1] = 1;
890
891 *ret = TAKE_PTR(buf);
892 return 0;
893 }
894
895 int ndisc_option_add_dnssl_internal(
896 Set **options,
897 size_t offset, char *
898 const *domains,
899 usec_t lifetime,
900 usec_t valid_until) {
901
902 int r;
903
904 assert(options);
905
906 if (strv_isempty(domains))
907 return -EINVAL;
908
909 STRV_FOREACH(s, domains) {
910 r = dns_name_is_valid(*s);
911 if (r < 0)
912 return r;
913
914 if (is_localhost(*s) || dns_name_is_root(*s))
915 return -EINVAL;
916 }
917
918 _cleanup_strv_free_ char **copy = strv_copy(domains);
919 if (!copy)
920 return -ENOMEM;
921
922 sd_ndisc_option *p = ndisc_option_new(SD_NDISC_OPTION_DNSSL, offset);
923 if (!p)
924 return -ENOMEM;
925
926 p->dnssl = (sd_ndisc_dnssl) {
927 .domains = TAKE_PTR(copy),
928 .lifetime = lifetime,
929 .valid_until = valid_until,
930 };
931
932 return ndisc_option_consume(options, p);
933 }
934
935 static int ndisc_option_parse_dnssl(Set **options, size_t offset, size_t len, const uint8_t *opt) {
936 int r;
937
938 assert(options);
939 assert(opt);
940
941 if (len < 2*8)
942 return -EBADMSG;
943
944 if (opt[0] != SD_NDISC_OPTION_DNSSL)
945 return -EBADMSG;
946
947 usec_t lifetime = unaligned_be32_sec_to_usec(opt + 4, /* max_as_infinity= */ true);
948
949 _cleanup_strv_free_ char **l = NULL;
950 _cleanup_free_ char *e = NULL;
951 size_t n = 0;
952 for (size_t c, pos = 8; pos < len; pos += c) {
953
954 c = opt[pos];
955 pos++;
956
957 if (c == 0) {
958 /* Found NUL termination */
959
960 if (n > 0) {
961 _cleanup_free_ char *normalized = NULL;
962
963 e[n] = 0;
964 r = dns_name_normalize(e, 0, &normalized);
965 if (r < 0)
966 return r;
967
968 /* Ignore the root domain name or "localhost" and friends */
969 if (!is_localhost(normalized) && !dns_name_is_root(normalized)) {
970 r = strv_consume(&l, TAKE_PTR(normalized));
971 if (r < 0)
972 return r;
973 }
974 }
975
976 n = 0;
977 continue;
978 }
979
980 /* Check for compression (which is not allowed) */
981 if (c > 63)
982 return -EBADMSG;
983
984 if (pos + c >= len)
985 return -EBADMSG;
986
987 if (!GREEDY_REALLOC(e, n + (n != 0) + DNS_LABEL_ESCAPED_MAX + 1U))
988 return -ENOMEM;
989
990 if (n != 0)
991 e[n++] = '.';
992
993 r = dns_label_escape((const char*) (opt + pos), c, e + n, DNS_LABEL_ESCAPED_MAX);
994 if (r < 0)
995 return r;
996
997 n += r;
998 }
999
1000 if (n > 0) /* Not properly NUL terminated */
1001 return -EBADMSG;
1002
1003 return ndisc_option_add_dnssl(options, offset, l, lifetime);
1004 }
1005
1006 static int ndisc_option_build_dnssl(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
1007 int r;
1008
1009 assert(option);
1010 assert(option->type == SD_NDISC_OPTION_DNSSL);
1011 assert(ret);
1012
1013 size_t len = 8;
1014 STRV_FOREACH(s, option->dnssl.domains)
1015 len += strlen(*s) + 2;
1016 len = DIV_ROUND_UP(len, 8);
1017
1018 be32_t lifetime = usec_to_be32_sec(MIN(option->dnssl.lifetime,
1019 usec_sub_unsigned(option->dnssl.valid_until, timestamp)));
1020
1021 _cleanup_free_ uint8_t *buf = new(uint8_t, len * 8);
1022 if (!buf)
1023 return -ENOMEM;
1024
1025 buf[0] = SD_NDISC_OPTION_DNSSL;
1026 buf[1] = len;
1027 buf[2] = 0;
1028 buf[3] = 0;
1029 memcpy(buf + 4, &lifetime, sizeof(be32_t));
1030
1031 size_t remaining = len * 8 - 8;
1032 uint8_t *p = buf + 8;
1033
1034 STRV_FOREACH(s, option->dnssl.domains) {
1035 r = dns_name_to_wire_format(*s, p, remaining, /* canonical= */ false);
1036 if (r < 0)
1037 return r;
1038
1039 assert(remaining >= (size_t) r);
1040 p += r;
1041 remaining -= r;
1042 }
1043
1044 memzero(p, remaining);
1045
1046 *ret = TAKE_PTR(buf);
1047 return 0;
1048 }
1049
1050 int ndisc_option_add_captive_portal(Set **options, size_t offset, const char *portal) {
1051 assert(options);
1052
1053 if (isempty(portal))
1054 return -EINVAL;
1055
1056 if (!in_charset(portal, URI_VALID))
1057 return -EINVAL;
1058
1059 sd_ndisc_option *p = ndisc_option_get_by_type(*options, SD_NDISC_OPTION_CAPTIVE_PORTAL);
1060 if (p) {
1061 if (offset != 0)
1062 return -EEXIST;
1063
1064 return free_and_strdup(&p->captive_portal, portal);
1065 }
1066
1067 _cleanup_free_ char *copy = strdup(portal);
1068 if (!copy)
1069 return -ENOMEM;
1070
1071 p = ndisc_option_new(SD_NDISC_OPTION_CAPTIVE_PORTAL, offset);
1072 if (!p)
1073 return -ENOMEM;
1074
1075 p->captive_portal = TAKE_PTR(copy);
1076
1077 return ndisc_option_consume(options, p);
1078 }
1079
1080 static int ndisc_option_parse_captive_portal(Set **options, size_t offset, size_t len, const uint8_t *opt) {
1081 assert(options);
1082 assert(opt);
1083
1084 if (len < 8)
1085 return -EBADMSG;
1086
1087 if (opt[0] != SD_NDISC_OPTION_CAPTIVE_PORTAL)
1088 return -EBADMSG;
1089
1090 _cleanup_free_ char *portal = memdup_suffix0(opt + 2, len - 2);
1091 if (!portal)
1092 return -ENOMEM;
1093
1094 size_t size = strlen(portal);
1095 if (size == 0)
1096 return -EBADMSG;
1097
1098 /* Check that the message is not truncated by an embedded NUL.
1099 * NUL padding to a multiple of 8 is expected. */
1100 if (DIV_ROUND_UP(size + 2, 8) * 8 != len && DIV_ROUND_UP(size + 3, 8) * 8 != len)
1101 return -EBADMSG;
1102
1103 return ndisc_option_add_captive_portal(options, offset, portal);
1104 }
1105
1106 static int ndisc_option_build_captive_portal(const sd_ndisc_option *option, uint8_t **ret) {
1107 assert(option);
1108 assert(option->type == SD_NDISC_OPTION_CAPTIVE_PORTAL);
1109 assert(ret);
1110
1111 size_t len_portal = strlen(option->captive_portal);
1112 size_t len = DIV_ROUND_UP(len_portal + 1 + 2, 8);
1113
1114 _cleanup_free_ uint8_t *buf = new(uint8_t, len * 8);
1115 if (!buf)
1116 return -ENOMEM;
1117
1118 buf[0] = SD_NDISC_OPTION_CAPTIVE_PORTAL;
1119 buf[1] = len;
1120
1121 uint8_t *p = mempcpy(buf + 2, option->captive_portal, len_portal);
1122 size_t remaining = len * 8 - 2 - len_portal;
1123
1124 memzero(p, remaining);
1125
1126 *ret = TAKE_PTR(buf);
1127 return 0;
1128 }
1129
1130 static const uint8_t prefix_length_code_to_prefix_length[_PREFIX_LENGTH_CODE_MAX] = {
1131 [PREFIX_LENGTH_CODE_96] = 96,
1132 [PREFIX_LENGTH_CODE_64] = 64,
1133 [PREFIX_LENGTH_CODE_56] = 56,
1134 [PREFIX_LENGTH_CODE_48] = 48,
1135 [PREFIX_LENGTH_CODE_40] = 40,
1136 [PREFIX_LENGTH_CODE_32] = 32,
1137 };
1138
1139 int pref64_prefix_length_to_plc(uint8_t prefixlen, uint8_t *ret) {
1140 for (size_t i = 0; i < ELEMENTSOF(prefix_length_code_to_prefix_length); i++)
1141 if (prefix_length_code_to_prefix_length[i] == prefixlen) {
1142 if (ret)
1143 *ret = i;
1144 return 0;
1145 }
1146
1147 return -EINVAL;
1148 }
1149
1150 static int pref64_lifetime_and_plc_parse(uint16_t lifetime_and_plc, uint8_t *ret_prefixlen, usec_t *ret_lifetime) {
1151 uint16_t plc = lifetime_and_plc & PREF64_PLC_MASK;
1152 if (plc >= _PREFIX_LENGTH_CODE_MAX)
1153 return -EINVAL;
1154
1155 if (ret_prefixlen)
1156 *ret_prefixlen = prefix_length_code_to_prefix_length[plc];
1157 if (ret_lifetime)
1158 *ret_lifetime = (lifetime_and_plc & PREF64_SCALED_LIFETIME_MASK) * USEC_PER_SEC;
1159 return 0;
1160 }
1161
1162 int ndisc_option_add_prefix64_internal(
1163 Set **options,
1164 size_t offset,
1165 uint8_t prefixlen,
1166 const struct in6_addr *prefix,
1167 usec_t lifetime,
1168 usec_t valid_until) {
1169
1170 int r;
1171
1172 assert(options);
1173 assert(prefix);
1174
1175 r = pref64_prefix_length_to_plc(prefixlen, NULL);
1176 if (r < 0)
1177 return r;
1178
1179 if (lifetime > PREF64_MAX_LIFETIME_USEC)
1180 return -EINVAL;
1181
1182 struct in6_addr addr = *prefix;
1183 in6_addr_mask(&addr, prefixlen);
1184
1185 sd_ndisc_option *p = ndisc_option_get(
1186 *options,
1187 &(const sd_ndisc_option) {
1188 .type = SD_NDISC_OPTION_PREF64,
1189 .prefix64.prefixlen = prefixlen,
1190 .prefix64.prefix = addr,
1191 });
1192 if (p) {
1193 if (offset != 0)
1194 return -EEXIST;
1195
1196 p->prefix64.lifetime = lifetime;
1197 p->prefix64.valid_until = valid_until;
1198 return 0;
1199 }
1200
1201 p = ndisc_option_new(SD_NDISC_OPTION_PREF64, offset);
1202 if (!p)
1203 return -ENOMEM;
1204
1205 p->prefix64 = (sd_ndisc_prefix64) {
1206 .prefixlen = prefixlen,
1207 .prefix = addr,
1208 .lifetime = lifetime,
1209 .valid_until = valid_until,
1210 };
1211
1212 return ndisc_option_consume(options, p);
1213 }
1214
1215 static int ndisc_option_parse_prefix64(Set **options, size_t offset, size_t len, const uint8_t *opt) {
1216 int r;
1217
1218 assert(options);
1219 assert(opt);
1220
1221 if (len != 2*8)
1222 return -EBADMSG;
1223
1224 if (opt[0] != SD_NDISC_OPTION_PREF64)
1225 return -EBADMSG;
1226
1227 uint8_t prefixlen;
1228 usec_t lifetime;
1229 r = pref64_lifetime_and_plc_parse(unaligned_read_be16(opt + 2), &prefixlen, &lifetime);
1230 if (r < 0)
1231 return r;
1232
1233 struct in6_addr prefix;
1234 memcpy(&prefix, opt + 4, len - 4);
1235 in6_addr_mask(&prefix, prefixlen);
1236
1237 return ndisc_option_add_prefix64(options, offset, prefixlen, &prefix, lifetime);
1238 }
1239
1240 static int ndisc_option_build_prefix64(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
1241 int r;
1242
1243 assert(option);
1244 assert(option->type == SD_NDISC_OPTION_PREF64);
1245 assert(ret);
1246
1247 uint8_t code;
1248 r = pref64_prefix_length_to_plc(option->prefix64.prefixlen, &code);
1249 if (r < 0)
1250 return r;
1251
1252 uint16_t lifetime = (uint16_t) DIV_ROUND_UP(MIN(option->prefix64.lifetime,
1253 usec_sub_unsigned(option->prefix64.valid_until, timestamp)),
1254 USEC_PER_SEC) & PREF64_SCALED_LIFETIME_MASK;
1255
1256 _cleanup_free_ uint8_t *buf = new(uint8_t, 2 * 8);
1257 if (!buf)
1258 return -ENOMEM;
1259
1260 buf[0] = SD_NDISC_OPTION_PREF64;
1261 buf[1] = 2;
1262 unaligned_write_be16(buf + 2, lifetime | code);
1263 memcpy(buf + 4, &option->prefix64.prefix, 12);
1264
1265 *ret = TAKE_PTR(buf);
1266 return 0;
1267 }
1268
1269 int ndisc_option_add_encrypted_dns_internal(
1270 Set **options,
1271 size_t offset,
1272 sd_dns_resolver *res,
1273 usec_t lifetime,
1274 usec_t valid_until) {
1275 assert(options);
1276
1277 sd_ndisc_option *p = ndisc_option_new(SD_NDISC_OPTION_ENCRYPTED_DNS, offset);
1278 if (!p)
1279 return -ENOMEM;
1280
1281 p->encrypted_dns = (sd_ndisc_dnr) {
1282 .resolver = res,
1283 .lifetime = lifetime,
1284 .valid_until = valid_until,
1285 };
1286
1287 return ndisc_option_consume(options, p);
1288 }
1289
1290 static int ndisc_get_dns_name(const uint8_t *optval, size_t optlen, char **ret) {
1291 _cleanup_free_ char *name = NULL;
1292 int r;
1293
1294 assert(optval || optlen == 0);
1295 assert(ret);
1296
1297 r = dns_name_from_wire_format(&optval, &optlen, &name);
1298 if (r < 0)
1299 return r;
1300 if (r == 0 || optlen != 0)
1301 return -EBADMSG;
1302
1303 *ret = TAKE_PTR(name);
1304 return r;
1305 }
1306
1307 static int ndisc_option_parse_encrypted_dns(Set **options, size_t offset, size_t len, const uint8_t *opt) {
1308 int r;
1309
1310 assert(options);
1311 assert(opt);
1312 _cleanup_(sd_dns_resolver_done) sd_dns_resolver res = {};
1313 usec_t lifetime;
1314 size_t ilen;
1315
1316 /* Every field up to and including adn must be present */
1317 if (len < 2*8)
1318 return -EBADMSG;
1319
1320 if (opt[0] != SD_NDISC_OPTION_ENCRYPTED_DNS)
1321 return -EBADMSG;
1322
1323 size_t off = 2;
1324
1325 /* Priority */
1326 res.priority = unaligned_read_be16(opt + off);
1327 /* Alias mode is not allowed */
1328 if (res.priority == 0)
1329 return -EBADMSG;
1330 off += sizeof(uint16_t);
1331
1332 /* Lifetime */
1333 lifetime = unaligned_be32_sec_to_usec(opt + off, /* max_as_infinity= */ true);
1334 off += sizeof(uint32_t);
1335
1336 /* adn field (length + dns-name) */
1337 ilen = unaligned_read_be16(opt + off);
1338 off += sizeof(uint16_t);
1339 if (off + ilen > len)
1340 return -EBADMSG;
1341
1342 r = ndisc_get_dns_name(opt + off, ilen, &res.auth_name);
1343 if (r < 0)
1344 return r;
1345 r = dns_name_is_valid_ldh(res.auth_name);
1346 if (r < 0)
1347 return r;
1348 if (!r)
1349 return -EBADMSG;
1350 if (dns_name_is_root(res.auth_name))
1351 return -EBADMSG;
1352 off += ilen;
1353
1354 /* This is the last field in adn-only mode, sans padding */
1355 if (8 * DIV_ROUND_UP(off, 8) == len && memeqzero(opt + off, len - off))
1356 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Received ADN-only encrypted DNS option, ignoring.");
1357
1358 /* Fields following the variable (octets) length adn field are no longer certain to be aligned. */
1359
1360 /* addrs (length + packed struct in6_addr) */
1361 if (off + sizeof(uint16_t) > len)
1362 return -EBADMSG;
1363 ilen = unaligned_read_be16(opt + off);
1364 off += sizeof(uint16_t);
1365 if (off + ilen > len || ilen % (sizeof(struct in6_addr)) != 0)
1366 return -EBADMSG;
1367
1368 size_t n_addrs = ilen / (sizeof(struct in6_addr));
1369 if (n_addrs == 0)
1370 return -EBADMSG;
1371 res.addrs = new(union in_addr_union, n_addrs);
1372 if (!res.addrs)
1373 return -ENOMEM;
1374
1375 for (size_t i = 0; i < n_addrs; i++) {
1376 union in_addr_union addr;
1377 memcpy(&addr.in6, opt + off, sizeof(struct in6_addr));
1378 if (in_addr_is_multicast(AF_INET6, &addr) ||
1379 in_addr_is_localhost(AF_INET6, &addr))
1380 return -EBADMSG;
1381 res.addrs[i] = addr;
1382 off += sizeof(struct in6_addr);
1383 }
1384 res.n_addrs = n_addrs;
1385 res.family = AF_INET6;
1386
1387 /* SvcParam field. (length + SvcParams) */
1388 if (off + sizeof(uint16_t) > len)
1389 return -EBADMSG;
1390 ilen = unaligned_read_be16(opt + off);
1391 off += sizeof(uint16_t);
1392 if (off + ilen > len)
1393 return -EBADMSG;
1394
1395 r = dnr_parse_svc_params(opt + off, ilen, &res);
1396 if (r < 0)
1397 return r;
1398 if (r == 0) /* This indicates a valid message we don't support */
1399 return -EOPNOTSUPP;
1400 off += ilen;
1401
1402 /* the remaining padding bytes must be zeroed */
1403 if (len - off >= 8 || !memeqzero(opt + off, len - off))
1404 return -EBADMSG;
1405
1406 sd_dns_resolver *new_res = new(sd_dns_resolver, 1);
1407 if (!new_res)
1408 return -ENOMEM;
1409
1410 *new_res = TAKE_STRUCT(res);
1411
1412 return ndisc_option_add_encrypted_dns(options, offset, new_res, lifetime);
1413 }
1414
1415 static int ndisc_option_build_encrypted_dns(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
1416 int r;
1417
1418 assert(option);
1419 assert(option->type == SD_NDISC_OPTION_ENCRYPTED_DNS);
1420 assert(ret);
1421
1422 size_t off, len, ilen, plen, poff;
1423
1424 /* Everything up to adn field is required, so we need at least 2*8 bytes */
1425 _cleanup_free_ uint8_t *buf = new(uint8_t, 2 * 8);
1426 if (!buf)
1427 return -ENOMEM;
1428
1429 _cleanup_strv_free_ char **alpns = NULL;
1430 const sd_dns_resolver *res = option->encrypted_dns.resolver;
1431 be32_t lifetime = usec_to_be32_sec(MIN(option->encrypted_dns.lifetime,
1432 usec_sub_unsigned(option->encrypted_dns.valid_until, timestamp)));
1433
1434 /* Type (Length field filled in last) */
1435 buf[0] = option->type;
1436
1437 /* Priority */
1438 off = 2;
1439 unaligned_write_be16(buf + off, res->priority);
1440 off += sizeof(be16_t);
1441
1442 /* Lifetime */
1443 memcpy(buf + off, &lifetime, sizeof(be32_t));
1444 off += sizeof(be32_t);
1445
1446 /* ADN */
1447 //FIXME can the wire format be longer than this?
1448 ilen = strlen(res->auth_name) + 2;
1449
1450 /* From now on, there isn't guaranteed to be enough space to put each field */
1451 if (!GREEDY_REALLOC(buf, off + sizeof(uint16_t) + ilen))
1452 return -ENOMEM;
1453
1454 r = dns_name_to_wire_format(res->auth_name, buf + off + sizeof(uint16_t), ilen, /* canonical= */ false);
1455 if (r < 0)
1456 return r;
1457 unaligned_write_be16(buf + off, (uint16_t) r);
1458 off += sizeof(uint16_t) + r;
1459
1460 /* ADN-only mode */
1461 if (res->n_addrs == 0)
1462 goto padding;
1463
1464 /* addrs */
1465 if (size_multiply_overflow(sizeof(struct in6_addr), res->n_addrs))
1466 return -ENOMEM;
1467
1468 ilen = res->n_addrs * sizeof(struct in6_addr);
1469 if (!GREEDY_REALLOC(buf, off + sizeof(uint16_t) + ilen))
1470 return -ENOMEM;
1471
1472 unaligned_write_be16(buf + off, ilen);
1473 off += sizeof(uint16_t);
1474
1475 FOREACH_ARRAY(addr, res->addrs, res->n_addrs) {
1476 memcpy(buf + off, &addr->in6, sizeof(struct in6_addr));
1477 off += sizeof(struct in6_addr);
1478 }
1479
1480 /* SvcParam, MUST appear in order */
1481 poff = off + sizeof(uint16_t);
1482
1483 /* ALPN */
1484 dns_resolver_transports_to_strv(res->transports, &alpns);
1485
1486 /* res needs to have at least one valid transport */
1487 if (strv_isempty(alpns))
1488 return -EINVAL;
1489
1490 plen = 0;
1491 STRV_FOREACH(alpn, alpns)
1492 plen += sizeof(uint8_t) + strlen(*alpn);
1493
1494 if (!GREEDY_REALLOC(buf, poff + 2 * sizeof(uint16_t) + plen))
1495 return -ENOMEM;
1496
1497 unaligned_write_be16(buf + poff, (uint16_t) DNS_SVC_PARAM_KEY_ALPN);
1498 poff += sizeof(uint16_t);
1499 unaligned_write_be16(buf + poff, plen);
1500 poff += sizeof(uint16_t);
1501
1502 STRV_FOREACH(alpn, alpns) {
1503 size_t alen = strlen(*alpn);
1504 buf[poff++] = alen;
1505 memcpy(buf + poff, *alpn, alen);
1506 poff += alen;
1507 }
1508
1509 /* port */
1510 if (res->port > 0) {
1511 plen = 2;
1512 if (!GREEDY_REALLOC(buf, poff + 2 * sizeof(uint16_t) + plen))
1513 return -ENOMEM;
1514
1515 unaligned_write_be16(buf + poff, (uint16_t) DNS_SVC_PARAM_KEY_PORT);
1516 poff += sizeof(uint16_t);
1517 unaligned_write_be16(buf + poff, plen);
1518 poff += sizeof(uint16_t);
1519 unaligned_write_be16(buf + poff, res->port);
1520 poff += sizeof(uint16_t);
1521 }
1522
1523 /* dohpath */
1524 if (res->dohpath) {
1525 plen = strlen(res->dohpath);
1526 if (!GREEDY_REALLOC(buf, poff + 2 * sizeof(uint16_t) + plen))
1527 return -ENOMEM;
1528
1529 unaligned_write_be16(buf + poff, (uint16_t) DNS_SVC_PARAM_KEY_DOHPATH);
1530 poff += sizeof(uint16_t);
1531 unaligned_write_be16(buf + poff, plen);
1532 poff += sizeof(uint16_t);
1533 memcpy(buf + poff, res->dohpath, plen);
1534 poff += plen;
1535 }
1536
1537 unaligned_write_be16(buf + off, LESS_BY(poff, off));
1538 off = poff;
1539
1540 padding:
1541 len = DIV_ROUND_UP(off, 8);
1542 if (!GREEDY_REALLOC(buf, 8*len))
1543 return -ENOMEM;
1544 memzero(buf + off, 8*len - off);
1545
1546 buf[1] = len;
1547 *ret = TAKE_PTR(buf);
1548 return 0;
1549 }
1550
1551 static int ndisc_option_parse_default(Set **options, size_t offset, size_t len, const uint8_t *opt) {
1552 assert(options);
1553 assert(opt);
1554 assert(len > 0);
1555
1556 sd_ndisc_option *p = ndisc_option_new(opt[0], offset);
1557 if (!p)
1558 return -ENOMEM;
1559
1560 return ndisc_option_consume(options, p);
1561 }
1562
1563 static int ndisc_header_size(uint8_t icmp6_type) {
1564 switch (icmp6_type) {
1565 case ND_ROUTER_SOLICIT:
1566 return sizeof(struct nd_router_solicit);
1567 case ND_ROUTER_ADVERT:
1568 return sizeof(struct nd_router_advert);
1569 case ND_NEIGHBOR_SOLICIT:
1570 return sizeof(struct nd_neighbor_solicit);
1571 case ND_NEIGHBOR_ADVERT:
1572 return sizeof(struct nd_neighbor_advert);
1573 case ND_REDIRECT:
1574 return sizeof(struct nd_redirect);
1575 default:
1576 return -EINVAL;
1577 }
1578 }
1579
1580 int ndisc_parse_options(ICMP6Packet *packet, Set **ret_options) {
1581 _cleanup_set_free_ Set *options = NULL;
1582 int r;
1583
1584 assert(packet);
1585 assert(ret_options);
1586
1587 r = icmp6_packet_get_type(packet);
1588 if (r < 0)
1589 return r;
1590
1591 r = ndisc_header_size(r);
1592 if (r < 0)
1593 return -EBADMSG;
1594 size_t header_size = r;
1595
1596 if (packet->raw_size < header_size)
1597 return -EBADMSG;
1598
1599 for (size_t length, offset = header_size; offset < packet->raw_size; offset += length) {
1600 uint8_t type;
1601 const uint8_t *opt;
1602
1603 r = ndisc_option_parse(packet, offset, &type, &length, &opt);
1604 if (r < 0)
1605 return log_debug_errno(r, "Failed to parse NDisc option header: %m");
1606
1607 switch (type) {
1608 case 0:
1609 r = -EBADMSG;
1610 break;
1611
1612 case SD_NDISC_OPTION_SOURCE_LL_ADDRESS:
1613 case SD_NDISC_OPTION_TARGET_LL_ADDRESS:
1614 r = ndisc_option_parse_link_layer_address(&options, offset, length, opt);
1615 break;
1616
1617 case SD_NDISC_OPTION_PREFIX_INFORMATION:
1618 r = ndisc_option_parse_prefix(&options, offset, length, opt);
1619 break;
1620
1621 case SD_NDISC_OPTION_REDIRECTED_HEADER:
1622 r = ndisc_option_parse_redirected_header(&options, offset, length, opt);
1623 break;
1624
1625 case SD_NDISC_OPTION_MTU:
1626 r = ndisc_option_parse_mtu(&options, offset, length, opt);
1627 break;
1628
1629 case SD_NDISC_OPTION_HOME_AGENT:
1630 r = ndisc_option_parse_home_agent(&options, offset, length, opt);
1631 break;
1632
1633 case SD_NDISC_OPTION_ROUTE_INFORMATION:
1634 r = ndisc_option_parse_route(&options, offset, length, opt);
1635 break;
1636
1637 case SD_NDISC_OPTION_RDNSS:
1638 r = ndisc_option_parse_rdnss(&options, offset, length, opt);
1639 break;
1640
1641 case SD_NDISC_OPTION_FLAGS_EXTENSION:
1642 r = ndisc_option_parse_flags_extension(&options, offset, length, opt);
1643 break;
1644
1645 case SD_NDISC_OPTION_DNSSL:
1646 r = ndisc_option_parse_dnssl(&options, offset, length, opt);
1647 break;
1648
1649 case SD_NDISC_OPTION_CAPTIVE_PORTAL:
1650 r = ndisc_option_parse_captive_portal(&options, offset, length, opt);
1651 break;
1652
1653 case SD_NDISC_OPTION_PREF64:
1654 r = ndisc_option_parse_prefix64(&options, offset, length, opt);
1655 break;
1656
1657 case SD_NDISC_OPTION_ENCRYPTED_DNS:
1658 r = ndisc_option_parse_encrypted_dns(&options, offset, length, opt);
1659 break;
1660
1661 default:
1662 r = ndisc_option_parse_default(&options, offset, length, opt);
1663 }
1664 if (r == -ENOMEM)
1665 return log_oom_debug();
1666 if (r < 0)
1667 log_debug_errno(r, "Failed to parse NDisc option %u, ignoring: %m", type);
1668 }
1669
1670 *ret_options = TAKE_PTR(options);
1671 return 0;
1672 }
1673
1674 sd_ndisc_option* ndisc_option_get(Set *options, const sd_ndisc_option *p) {
1675 return set_get(options, ASSERT_PTR(p));
1676 }
1677
1678 sd_ndisc_option* ndisc_option_get_by_type(Set *options, uint8_t type) {
1679 return ndisc_option_get(options, &(const sd_ndisc_option) { .type = type });
1680 }
1681
1682 int ndisc_option_get_mac(Set *options, uint8_t type, struct ether_addr *ret) {
1683 assert(IN_SET(type, SD_NDISC_OPTION_SOURCE_LL_ADDRESS, SD_NDISC_OPTION_TARGET_LL_ADDRESS));
1684
1685 sd_ndisc_option *p = ndisc_option_get_by_type(options, type);
1686 if (!p)
1687 return -ENODATA;
1688
1689 if (ret)
1690 *ret = p->mac;
1691 return 0;
1692 }
1693
1694 void ndisc_option_remove(Set *options, const sd_ndisc_option *p) {
1695 ndisc_option_free(set_remove(options, ASSERT_PTR(p)));
1696 }
1697
1698 int ndisc_send(int fd, const struct in6_addr *dst, const struct icmp6_hdr *hdr, Set *options, usec_t timestamp) {
1699 int r;
1700
1701 assert(fd >= 0);
1702 assert(dst);
1703 assert(hdr);
1704
1705 size_t n;
1706 _cleanup_free_ sd_ndisc_option **list = NULL;
1707 r = set_dump_sorted(options, (void***) &list, &n);
1708 if (r < 0)
1709 return r;
1710
1711 struct iovec *iov = NULL;
1712 size_t n_iov = 0;
1713 CLEANUP_ARRAY(iov, n_iov, iovec_array_free);
1714
1715 iov = new(struct iovec, 1 + n);
1716 if (!iov)
1717 return -ENOMEM;
1718
1719 r = ndisc_header_size(hdr->icmp6_type);
1720 if (r < 0)
1721 return r;
1722 size_t hdr_size = r;
1723
1724 _cleanup_free_ uint8_t *copy = newdup(uint8_t, hdr, hdr_size);
1725 if (!copy)
1726 return -ENOMEM;
1727
1728 iov[n_iov++] = IOVEC_MAKE(TAKE_PTR(copy), hdr_size);
1729
1730 FOREACH_ARRAY(p, list, n) {
1731 _cleanup_free_ uint8_t *buf = NULL;
1732 sd_ndisc_option *option = *p;
1733
1734 switch (option->type) {
1735 case 0:
1736 r = ndisc_option_build_raw(option, &buf);
1737 break;
1738
1739 case SD_NDISC_OPTION_SOURCE_LL_ADDRESS:
1740 case SD_NDISC_OPTION_TARGET_LL_ADDRESS:
1741 r = ndisc_option_build_link_layer_address(option, &buf);
1742 break;
1743
1744 case SD_NDISC_OPTION_PREFIX_INFORMATION:
1745 r = ndisc_option_build_prefix(option, timestamp, &buf);
1746 break;
1747
1748 case SD_NDISC_OPTION_REDIRECTED_HEADER:
1749 r = ndisc_option_build_redirected_header(option, &buf);
1750 break;
1751
1752 case SD_NDISC_OPTION_MTU:
1753 r = ndisc_option_build_mtu(option, &buf);
1754 break;
1755
1756 case SD_NDISC_OPTION_HOME_AGENT:
1757 r = ndisc_option_build_home_agent(option, timestamp, &buf);
1758 break;
1759
1760 case SD_NDISC_OPTION_ROUTE_INFORMATION:
1761 r = ndisc_option_build_route(option, timestamp, &buf);
1762 break;
1763
1764 case SD_NDISC_OPTION_RDNSS:
1765 r = ndisc_option_build_rdnss(option, timestamp, &buf);
1766 break;
1767
1768 case SD_NDISC_OPTION_FLAGS_EXTENSION:
1769 r = ndisc_option_build_flags_extension(option, &buf);
1770 break;
1771
1772 case SD_NDISC_OPTION_DNSSL:
1773 r = ndisc_option_build_dnssl(option, timestamp, &buf);
1774 break;
1775
1776 case SD_NDISC_OPTION_CAPTIVE_PORTAL:
1777 r = ndisc_option_build_captive_portal(option, &buf);
1778 break;
1779
1780 case SD_NDISC_OPTION_PREF64:
1781 r = ndisc_option_build_prefix64(option, timestamp, &buf);
1782 break;
1783
1784 case SD_NDISC_OPTION_ENCRYPTED_DNS:
1785 r = ndisc_option_build_encrypted_dns(option, timestamp, &buf);
1786 break;
1787
1788 default:
1789 continue;
1790 }
1791 if (r == -ENOMEM)
1792 return log_oom_debug();
1793 if (r < 0)
1794 log_debug_errno(r, "Failed to build NDisc option %u, ignoring: %m", option->type);
1795
1796 iov[n_iov++] = IOVEC_MAKE(buf, buf[1] * 8);
1797 TAKE_PTR(buf);
1798 }
1799
1800 return icmp6_send(fd, dst, iov, n_iov);
1801 }