]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/in-addr-util.c
in-addr-util: introduce in{4,6}_addr_is_multicast()
[thirdparty/systemd.git] / src / basic / in-addr-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <arpa/inet.h>
4 #include <endian.h>
5 #include <errno.h>
6 #include <net/if.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include "alloc-util.h"
12 #include "errno-util.h"
13 #include "in-addr-util.h"
14 #include "logarithm.h"
15 #include "macro.h"
16 #include "parse-util.h"
17 #include "random-util.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
20 #include "strxcpyx.h"
21
22 bool in4_addr_is_null(const struct in_addr *a) {
23 assert(a);
24
25 return a->s_addr == 0;
26 }
27
28 bool in6_addr_is_null(const struct in6_addr *a) {
29 assert(a);
30
31 return IN6_IS_ADDR_UNSPECIFIED(a);
32 }
33
34 int in_addr_is_null(int family, const union in_addr_union *u) {
35 assert(u);
36
37 if (family == AF_INET)
38 return in4_addr_is_null(&u->in);
39
40 if (family == AF_INET6)
41 return in6_addr_is_null(&u->in6);
42
43 return -EAFNOSUPPORT;
44 }
45
46 bool in4_addr_is_link_local(const struct in_addr *a) {
47 assert(a);
48
49 return (be32toh(a->s_addr) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
50 }
51
52 bool in4_addr_is_link_local_dynamic(const struct in_addr *a) {
53 assert(a);
54
55 if (!in4_addr_is_link_local(a))
56 return false;
57
58 /* 169.254.0.0/24 and 169.254.255.0/24 must not be used for the dynamic IPv4LL assignment.
59 * See RFC 3927 Section 2.1:
60 * The IPv4 prefix 169.254/16 is registered with the IANA for this purpose. The first 256 and last
61 * 256 addresses in the 169.254/16 prefix are reserved for future use and MUST NOT be selected by a
62 * host using this dynamic configuration mechanism. */
63 return !IN_SET(be32toh(a->s_addr) & 0x0000FF00U, 0x0000U, 0xFF00U);
64 }
65
66 bool in6_addr_is_link_local(const struct in6_addr *a) {
67 assert(a);
68
69 return IN6_IS_ADDR_LINKLOCAL(a);
70 }
71
72 int in_addr_is_link_local(int family, const union in_addr_union *u) {
73 assert(u);
74
75 if (family == AF_INET)
76 return in4_addr_is_link_local(&u->in);
77
78 if (family == AF_INET6)
79 return in6_addr_is_link_local(&u->in6);
80
81 return -EAFNOSUPPORT;
82 }
83
84 bool in6_addr_is_link_local_all_nodes(const struct in6_addr *a) {
85 assert(a);
86
87 /* ff02::1 */
88 return be32toh(a->s6_addr32[0]) == UINT32_C(0xff020000) &&
89 a->s6_addr32[1] == 0 &&
90 a->s6_addr32[2] == 0 &&
91 be32toh(a->s6_addr32[3]) == UINT32_C(0x00000001);
92 }
93
94 bool in4_addr_is_multicast(const struct in_addr *a) {
95 assert(a);
96
97 return IN_MULTICAST(be32toh(a->s_addr));
98 }
99
100 bool in6_addr_is_multicast(const struct in6_addr *a) {
101 assert(a);
102
103 return IN6_IS_ADDR_MULTICAST(a);
104 }
105
106 int in_addr_is_multicast(int family, const union in_addr_union *u) {
107 assert(u);
108
109 if (family == AF_INET)
110 return in4_addr_is_multicast(&u->in);
111
112 if (family == AF_INET6)
113 return in6_addr_is_multicast(&u->in6);
114
115 return -EAFNOSUPPORT;
116 }
117
118 bool in4_addr_is_local_multicast(const struct in_addr *a) {
119 assert(a);
120
121 return (be32toh(a->s_addr) & UINT32_C(0xffffff00)) == UINT32_C(0xe0000000);
122 }
123
124 bool in4_addr_is_localhost(const struct in_addr *a) {
125 assert(a);
126
127 /* All of 127.x.x.x is localhost. */
128 return (be32toh(a->s_addr) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
129 }
130
131 bool in4_addr_is_non_local(const struct in_addr *a) {
132 /* Whether the address is not null and not localhost.
133 *
134 * As such, it is suitable to configure as DNS/NTP server from DHCP. */
135 return !in4_addr_is_null(a) &&
136 !in4_addr_is_localhost(a);
137 }
138
139 int in_addr_is_localhost(int family, const union in_addr_union *u) {
140 assert(u);
141
142 if (family == AF_INET)
143 return in4_addr_is_localhost(&u->in);
144
145 if (family == AF_INET6)
146 return IN6_IS_ADDR_LOOPBACK(&u->in6);
147
148 return -EAFNOSUPPORT;
149 }
150
151 int in_addr_is_localhost_one(int family, const union in_addr_union *u) {
152 assert(u);
153
154 if (family == AF_INET)
155 /* 127.0.0.1 */
156 return be32toh(u->in.s_addr) == UINT32_C(0x7F000001);
157
158 if (family == AF_INET6)
159 return IN6_IS_ADDR_LOOPBACK(&u->in6);
160
161 return -EAFNOSUPPORT;
162 }
163
164 bool in6_addr_is_ipv4_mapped_address(const struct in6_addr *a) {
165 return a->s6_addr32[0] == 0 &&
166 a->s6_addr32[1] == 0 &&
167 a->s6_addr32[2] == htobe32(UINT32_C(0x0000ffff));
168 }
169
170 bool in4_addr_equal(const struct in_addr *a, const struct in_addr *b) {
171 assert(a);
172 assert(b);
173
174 return a->s_addr == b->s_addr;
175 }
176
177 bool in6_addr_equal(const struct in6_addr *a, const struct in6_addr *b) {
178 assert(a);
179 assert(b);
180
181 return IN6_ARE_ADDR_EQUAL(a, b);
182 }
183
184 int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {
185 assert(a);
186 assert(b);
187
188 if (family == AF_INET)
189 return in4_addr_equal(&a->in, &b->in);
190
191 if (family == AF_INET6)
192 return in6_addr_equal(&a->in6, &b->in6);
193
194 return -EAFNOSUPPORT;
195 }
196
197 int in_addr_prefix_intersect(
198 int family,
199 const union in_addr_union *a,
200 unsigned aprefixlen,
201 const union in_addr_union *b,
202 unsigned bprefixlen) {
203
204 unsigned m;
205
206 assert(a);
207 assert(b);
208
209 /* Checks whether there are any addresses that are in both networks */
210
211 m = MIN(aprefixlen, bprefixlen);
212
213 if (family == AF_INET) {
214 uint32_t x, nm;
215
216 x = be32toh(a->in.s_addr ^ b->in.s_addr);
217 nm = m == 0 ? 0 : 0xFFFFFFFFUL << (32 - m);
218
219 return (x & nm) == 0;
220 }
221
222 if (family == AF_INET6) {
223 unsigned i;
224
225 if (m > 128)
226 m = 128;
227
228 for (i = 0; i < 16; i++) {
229 uint8_t x, nm;
230
231 x = a->in6.s6_addr[i] ^ b->in6.s6_addr[i];
232
233 if (m < 8)
234 nm = 0xFF << (8 - m);
235 else
236 nm = 0xFF;
237
238 if ((x & nm) != 0)
239 return 0;
240
241 if (m > 8)
242 m -= 8;
243 else
244 m = 0;
245 }
246
247 return 1;
248 }
249
250 return -EAFNOSUPPORT;
251 }
252
253 int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen) {
254 assert(u);
255
256 /* Increases the network part of an address by one. Returns 0 if that succeeds, or -ERANGE if
257 * this overflows. */
258
259 return in_addr_prefix_nth(family, u, prefixlen, 1);
260 }
261
262 /*
263 * Calculates the nth prefix of size prefixlen starting from the address denoted by u.
264 *
265 * On success 0 will be returned and the calculated prefix will be available in
266 * u. In case the calculation cannot be performed (invalid prefix length,
267 * overflows would occur) -ERANGE is returned. If the address family given isn't
268 * supported -EAFNOSUPPORT will be returned.
269 *
270 * Examples:
271 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 2), returns 0, writes 192.168.2.0 to u
272 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 0), returns 0, no data written
273 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 24, 1), returns -ERANGE, no data written
274 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 0, 1), returns -ERANGE, no data written
275 * - in_addr_prefix_nth(AF_INET6, 2001:db8, 64, 0xff00) returns 0, writes 2001:0db8:0000:ff00:: to u
276 */
277 int in_addr_prefix_nth(int family, union in_addr_union *u, unsigned prefixlen, uint64_t nth) {
278 assert(u);
279
280 if (prefixlen <= 0)
281 return -ERANGE;
282
283 if (family == AF_INET) {
284 uint32_t c, n, t;
285
286 if (prefixlen > 32)
287 return -ERANGE;
288
289 c = be32toh(u->in.s_addr);
290
291 t = nth << (32 - prefixlen);
292
293 /* Check for wrap */
294 if (c > UINT32_MAX - t)
295 return -ERANGE;
296
297 n = c + t;
298
299 n &= UINT32_C(0xFFFFFFFF) << (32 - prefixlen);
300 u->in.s_addr = htobe32(n);
301 return 0;
302 }
303
304 if (family == AF_INET6) {
305 bool overflow = false;
306
307 if (prefixlen > 128)
308 return -ERANGE;
309
310 for (unsigned i = 16; i > 0; i--) {
311 unsigned t, j = i - 1, p = j * 8;
312
313 if (p >= prefixlen) {
314 u->in6.s6_addr[j] = 0;
315 continue;
316 }
317
318 if (prefixlen - p < 8) {
319 u->in6.s6_addr[j] &= 0xff << (8 - (prefixlen - p));
320 t = u->in6.s6_addr[j] + ((nth & 0xff) << (8 - (prefixlen - p)));
321 nth >>= prefixlen - p;
322 } else {
323 t = u->in6.s6_addr[j] + (nth & 0xff) + overflow;
324 nth >>= 8;
325 }
326
327 overflow = t > UINT8_MAX;
328 u->in6.s6_addr[j] = (uint8_t) (t & 0xff);
329 }
330
331 if (overflow || nth != 0)
332 return -ERANGE;
333
334 return 0;
335 }
336
337 return -EAFNOSUPPORT;
338 }
339
340 int in_addr_random_prefix(
341 int family,
342 union in_addr_union *u,
343 unsigned prefixlen_fixed_part,
344 unsigned prefixlen) {
345
346 assert(u);
347
348 /* Random network part of an address by one. */
349
350 if (prefixlen <= 0)
351 return 0;
352
353 if (family == AF_INET) {
354 uint32_t c, n;
355
356 if (prefixlen_fixed_part > 32)
357 prefixlen_fixed_part = 32;
358 if (prefixlen > 32)
359 prefixlen = 32;
360 if (prefixlen_fixed_part >= prefixlen)
361 return -EINVAL;
362
363 c = be32toh(u->in.s_addr);
364 c &= ((UINT32_C(1) << prefixlen_fixed_part) - 1) << (32 - prefixlen_fixed_part);
365
366 random_bytes(&n, sizeof(n));
367 n &= ((UINT32_C(1) << (prefixlen - prefixlen_fixed_part)) - 1) << (32 - prefixlen);
368
369 u->in.s_addr = htobe32(n | c);
370 return 1;
371 }
372
373 if (family == AF_INET6) {
374 struct in6_addr n;
375 unsigned i, j;
376
377 if (prefixlen_fixed_part > 128)
378 prefixlen_fixed_part = 128;
379 if (prefixlen > 128)
380 prefixlen = 128;
381 if (prefixlen_fixed_part >= prefixlen)
382 return -EINVAL;
383
384 random_bytes(&n, sizeof(n));
385
386 for (i = 0; i < 16; i++) {
387 uint8_t mask_fixed_part = 0, mask = 0;
388
389 if (i < (prefixlen_fixed_part + 7) / 8) {
390 if (i < prefixlen_fixed_part / 8)
391 mask_fixed_part = 0xffu;
392 else {
393 j = prefixlen_fixed_part % 8;
394 mask_fixed_part = ((UINT8_C(1) << (j + 1)) - 1) << (8 - j);
395 }
396 }
397
398 if (i < (prefixlen + 7) / 8) {
399 if (i < prefixlen / 8)
400 mask = 0xffu ^ mask_fixed_part;
401 else {
402 j = prefixlen % 8;
403 mask = (((UINT8_C(1) << (j + 1)) - 1) << (8 - j)) ^ mask_fixed_part;
404 }
405 }
406
407 u->in6.s6_addr[i] &= mask_fixed_part;
408 u->in6.s6_addr[i] |= n.s6_addr[i] & mask;
409 }
410
411 return 1;
412 }
413
414 return -EAFNOSUPPORT;
415 }
416
417 int in_addr_prefix_range(
418 int family,
419 const union in_addr_union *in,
420 unsigned prefixlen,
421 union in_addr_union *ret_start,
422 union in_addr_union *ret_end) {
423
424 union in_addr_union start, end;
425 int r;
426
427 assert(in);
428
429 if (!IN_SET(family, AF_INET, AF_INET6))
430 return -EAFNOSUPPORT;
431
432 if (ret_start) {
433 start = *in;
434 r = in_addr_prefix_nth(family, &start, prefixlen, 0);
435 if (r < 0)
436 return r;
437 }
438
439 if (ret_end) {
440 end = *in;
441 r = in_addr_prefix_nth(family, &end, prefixlen, 1);
442 if (r < 0)
443 return r;
444 }
445
446 if (ret_start)
447 *ret_start = start;
448 if (ret_end)
449 *ret_end = end;
450
451 return 0;
452 }
453
454 int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
455 _cleanup_free_ char *x = NULL;
456 size_t l;
457
458 assert(u);
459 assert(ret);
460
461 if (family == AF_INET)
462 l = INET_ADDRSTRLEN;
463 else if (family == AF_INET6)
464 l = INET6_ADDRSTRLEN;
465 else
466 return -EAFNOSUPPORT;
467
468 x = new(char, l);
469 if (!x)
470 return -ENOMEM;
471
472 errno = 0;
473 if (!typesafe_inet_ntop(family, u, x, l))
474 return errno_or_else(EINVAL);
475
476 *ret = TAKE_PTR(x);
477 return 0;
478 }
479
480 int in_addr_prefix_to_string(
481 int family,
482 const union in_addr_union *u,
483 unsigned prefixlen,
484 char *buf,
485 size_t buf_len) {
486
487 assert(u);
488 assert(buf);
489
490 if (!IN_SET(family, AF_INET, AF_INET6))
491 return -EAFNOSUPPORT;
492
493 errno = 0;
494 if (!typesafe_inet_ntop(family, u, buf, buf_len))
495 return errno_or_else(ENOSPC);
496
497 size_t l = strlen(buf);
498 if (!snprintf_ok(buf + l, buf_len - l, "/%u", prefixlen))
499 return -ENOSPC;
500 return 0;
501 }
502
503 int in_addr_port_ifindex_name_to_string(int family, const union in_addr_union *u, uint16_t port, int ifindex, const char *server_name, char **ret) {
504 _cleanup_free_ char *ip_str = NULL, *x = NULL;
505 int r;
506
507 assert(IN_SET(family, AF_INET, AF_INET6));
508 assert(u);
509 assert(ret);
510
511 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
512 * handle IPv6 link-local addresses. */
513
514 r = in_addr_to_string(family, u, &ip_str);
515 if (r < 0)
516 return r;
517
518 if (family == AF_INET6) {
519 r = in_addr_is_link_local(family, u);
520 if (r < 0)
521 return r;
522 if (r == 0)
523 ifindex = 0;
524 } else
525 ifindex = 0; /* For IPv4 address, ifindex is always ignored. */
526
527 if (port == 0 && ifindex == 0 && isempty(server_name)) {
528 *ret = TAKE_PTR(ip_str);
529 return 0;
530 }
531
532 const char *separator = isempty(server_name) ? "" : "#";
533 server_name = strempty(server_name);
534
535 if (port > 0) {
536 if (family == AF_INET6) {
537 if (ifindex > 0)
538 r = asprintf(&x, "[%s]:%"PRIu16"%%%i%s%s", ip_str, port, ifindex, separator, server_name);
539 else
540 r = asprintf(&x, "[%s]:%"PRIu16"%s%s", ip_str, port, separator, server_name);
541 } else
542 r = asprintf(&x, "%s:%"PRIu16"%s%s", ip_str, port, separator, server_name);
543 } else {
544 if (ifindex > 0)
545 r = asprintf(&x, "%s%%%i%s%s", ip_str, ifindex, separator, server_name);
546 else {
547 x = strjoin(ip_str, separator, server_name);
548 r = x ? 0 : -ENOMEM;
549 }
550 }
551 if (r < 0)
552 return -ENOMEM;
553
554 *ret = TAKE_PTR(x);
555 return 0;
556 }
557
558 int in_addr_from_string(int family, const char *s, union in_addr_union *ret) {
559 union in_addr_union buffer;
560 assert(s);
561
562 if (!IN_SET(family, AF_INET, AF_INET6))
563 return -EAFNOSUPPORT;
564
565 errno = 0;
566 if (inet_pton(family, s, ret ?: &buffer) <= 0)
567 return errno_or_else(EINVAL);
568
569 return 0;
570 }
571
572 int in_addr_from_string_auto(const char *s, int *ret_family, union in_addr_union *ret) {
573 int r;
574
575 assert(s);
576
577 r = in_addr_from_string(AF_INET, s, ret);
578 if (r >= 0) {
579 if (ret_family)
580 *ret_family = AF_INET;
581 return 0;
582 }
583
584 r = in_addr_from_string(AF_INET6, s, ret);
585 if (r >= 0) {
586 if (ret_family)
587 *ret_family = AF_INET6;
588 return 0;
589 }
590
591 return -EINVAL;
592 }
593
594 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr *addr) {
595 assert(addr);
596
597 return 32U - u32ctz(be32toh(addr->s_addr));
598 }
599
600 /* Calculate an IPv4 netmask from prefix length, for example /8 -> 255.0.0.0. */
601 struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen) {
602 assert(addr);
603 assert(prefixlen <= 32);
604
605 /* Shifting beyond 32 is not defined, handle this specially. */
606 if (prefixlen == 0)
607 addr->s_addr = 0;
608 else
609 addr->s_addr = htobe32((0xffffffff << (32 - prefixlen)) & 0xffffffff);
610
611 return addr;
612 }
613
614 /* Calculate an IPv6 netmask from prefix length, for example /16 -> ffff::. */
615 struct in6_addr* in6_addr_prefixlen_to_netmask(struct in6_addr *addr, unsigned char prefixlen) {
616 assert(addr);
617 assert(prefixlen <= 128);
618
619 for (unsigned i = 0; i < 16; i++) {
620 uint8_t mask;
621
622 if (prefixlen >= 8) {
623 mask = 0xFF;
624 prefixlen -= 8;
625 } else if (prefixlen > 0) {
626 mask = 0xFF << (8 - prefixlen);
627 prefixlen = 0;
628 } else {
629 assert(prefixlen == 0);
630 mask = 0;
631 }
632
633 addr->s6_addr[i] = mask;
634 }
635
636 return addr;
637 }
638
639 /* Calculate an IPv4 or IPv6 netmask from prefix length, for example /8 -> 255.0.0.0 or /16 -> ffff::. */
640 int in_addr_prefixlen_to_netmask(int family, union in_addr_union *addr, unsigned char prefixlen) {
641 assert(addr);
642
643 switch (family) {
644 case AF_INET:
645 in4_addr_prefixlen_to_netmask(&addr->in, prefixlen);
646 return 0;
647 case AF_INET6:
648 in6_addr_prefixlen_to_netmask(&addr->in6, prefixlen);
649 return 0;
650 default:
651 return -EAFNOSUPPORT;
652 }
653 }
654
655 int in4_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
656 uint8_t msb_octet = *(uint8_t*) addr;
657
658 /* addr may not be aligned, so make sure we only access it byte-wise */
659
660 assert(addr);
661 assert(prefixlen);
662
663 if (msb_octet < 128)
664 /* class A, leading bits: 0 */
665 *prefixlen = 8;
666 else if (msb_octet < 192)
667 /* class B, leading bits 10 */
668 *prefixlen = 16;
669 else if (msb_octet < 224)
670 /* class C, leading bits 110 */
671 *prefixlen = 24;
672 else
673 /* class D or E, no default prefixlen */
674 return -ERANGE;
675
676 return 0;
677 }
678
679 int in4_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
680 unsigned char prefixlen;
681 int r;
682
683 assert(addr);
684 assert(mask);
685
686 r = in4_addr_default_prefixlen(addr, &prefixlen);
687 if (r < 0)
688 return r;
689
690 in4_addr_prefixlen_to_netmask(mask, prefixlen);
691 return 0;
692 }
693
694 int in4_addr_mask(struct in_addr *addr, unsigned char prefixlen) {
695 struct in_addr mask;
696
697 assert(addr);
698
699 if (!in4_addr_prefixlen_to_netmask(&mask, prefixlen))
700 return -EINVAL;
701
702 addr->s_addr &= mask.s_addr;
703 return 0;
704 }
705
706 int in6_addr_mask(struct in6_addr *addr, unsigned char prefixlen) {
707 unsigned i;
708
709 for (i = 0; i < 16; i++) {
710 uint8_t mask;
711
712 if (prefixlen >= 8) {
713 mask = 0xFF;
714 prefixlen -= 8;
715 } else if (prefixlen > 0) {
716 mask = 0xFF << (8 - prefixlen);
717 prefixlen = 0;
718 } else {
719 assert(prefixlen == 0);
720 mask = 0;
721 }
722
723 addr->s6_addr[i] &= mask;
724 }
725
726 return 0;
727 }
728
729 int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen) {
730 assert(addr);
731
732 switch (family) {
733 case AF_INET:
734 return in4_addr_mask(&addr->in, prefixlen);
735 case AF_INET6:
736 return in6_addr_mask(&addr->in6, prefixlen);
737 default:
738 return -EAFNOSUPPORT;
739 }
740 }
741
742 int in4_addr_prefix_covers_full(
743 const struct in_addr *prefix,
744 unsigned char prefixlen,
745 const struct in_addr *address,
746 unsigned char address_prefixlen) {
747
748 struct in_addr masked_prefix, masked_address;
749 int r;
750
751 assert(prefix);
752 assert(address);
753
754 if (prefixlen > address_prefixlen)
755 return false;
756
757 masked_prefix = *prefix;
758 r = in4_addr_mask(&masked_prefix, prefixlen);
759 if (r < 0)
760 return r;
761
762 masked_address = *address;
763 r = in4_addr_mask(&masked_address, prefixlen);
764 if (r < 0)
765 return r;
766
767 return in4_addr_equal(&masked_prefix, &masked_address);
768 }
769
770 int in6_addr_prefix_covers_full(
771 const struct in6_addr *prefix,
772 unsigned char prefixlen,
773 const struct in6_addr *address,
774 unsigned char address_prefixlen) {
775
776 struct in6_addr masked_prefix, masked_address;
777 int r;
778
779 assert(prefix);
780 assert(address);
781
782 if (prefixlen > address_prefixlen)
783 return false;
784
785 masked_prefix = *prefix;
786 r = in6_addr_mask(&masked_prefix, prefixlen);
787 if (r < 0)
788 return r;
789
790 masked_address = *address;
791 r = in6_addr_mask(&masked_address, prefixlen);
792 if (r < 0)
793 return r;
794
795 return in6_addr_equal(&masked_prefix, &masked_address);
796 }
797
798 int in_addr_prefix_covers_full(
799 int family,
800 const union in_addr_union *prefix,
801 unsigned char prefixlen,
802 const union in_addr_union *address,
803 unsigned char address_prefixlen) {
804
805 assert(prefix);
806 assert(address);
807
808 switch (family) {
809 case AF_INET:
810 return in4_addr_prefix_covers_full(&prefix->in, prefixlen, &address->in, address_prefixlen);
811 case AF_INET6:
812 return in6_addr_prefix_covers_full(&prefix->in6, prefixlen, &address->in6, address_prefixlen);
813 default:
814 return -EAFNOSUPPORT;
815 }
816 }
817
818 int in_addr_parse_prefixlen(int family, const char *p, unsigned char *ret) {
819 uint8_t u;
820 int r;
821
822 if (!IN_SET(family, AF_INET, AF_INET6))
823 return -EAFNOSUPPORT;
824
825 r = safe_atou8(p, &u);
826 if (r < 0)
827 return r;
828
829 if (u > FAMILY_ADDRESS_SIZE(family) * 8)
830 return -ERANGE;
831
832 *ret = u;
833 return 0;
834 }
835
836 int in_addr_prefix_from_string(
837 const char *p,
838 int family,
839 union in_addr_union *ret_prefix,
840 unsigned char *ret_prefixlen) {
841
842 _cleanup_free_ char *str = NULL;
843 union in_addr_union buffer;
844 const char *e, *l;
845 unsigned char k;
846 int r;
847
848 assert(p);
849
850 if (!IN_SET(family, AF_INET, AF_INET6))
851 return -EAFNOSUPPORT;
852
853 e = strchr(p, '/');
854 if (e) {
855 str = strndup(p, e - p);
856 if (!str)
857 return -ENOMEM;
858
859 l = str;
860 } else
861 l = p;
862
863 r = in_addr_from_string(family, l, &buffer);
864 if (r < 0)
865 return r;
866
867 if (e) {
868 r = in_addr_parse_prefixlen(family, e+1, &k);
869 if (r < 0)
870 return r;
871 } else
872 k = FAMILY_ADDRESS_SIZE(family) * 8;
873
874 if (ret_prefix)
875 *ret_prefix = buffer;
876 if (ret_prefixlen)
877 *ret_prefixlen = k;
878
879 return 0;
880 }
881
882 int in_addr_prefix_from_string_auto_internal(
883 const char *p,
884 InAddrPrefixLenMode mode,
885 int *ret_family,
886 union in_addr_union *ret_prefix,
887 unsigned char *ret_prefixlen) {
888
889 _cleanup_free_ char *str = NULL;
890 union in_addr_union buffer;
891 const char *e, *l;
892 unsigned char k;
893 int family, r;
894
895 assert(p);
896
897 e = strchr(p, '/');
898 if (e) {
899 str = strndup(p, e - p);
900 if (!str)
901 return -ENOMEM;
902
903 l = str;
904 } else
905 l = p;
906
907 r = in_addr_from_string_auto(l, &family, &buffer);
908 if (r < 0)
909 return r;
910
911 if (e) {
912 r = in_addr_parse_prefixlen(family, e+1, &k);
913 if (r < 0)
914 return r;
915 } else
916 switch (mode) {
917 case PREFIXLEN_FULL:
918 k = FAMILY_ADDRESS_SIZE(family) * 8;
919 break;
920 case PREFIXLEN_REFUSE:
921 return -ENOANO; /* To distinguish this error from others. */
922 default:
923 assert_not_reached();
924 }
925
926 if (ret_family)
927 *ret_family = family;
928 if (ret_prefix)
929 *ret_prefix = buffer;
930 if (ret_prefixlen)
931 *ret_prefixlen = k;
932
933 return 0;
934
935 }
936
937 void in_addr_hash_func(const union in_addr_union *u, int family, struct siphash *state) {
938 assert(u);
939 assert(state);
940
941 siphash24_compress(u->bytes, FAMILY_ADDRESS_SIZE(family), state);
942 }
943
944 void in_addr_data_hash_func(const struct in_addr_data *a, struct siphash *state) {
945 assert(a);
946 assert(state);
947
948 siphash24_compress_typesafe(a->family, state);
949 in_addr_hash_func(&a->address, a->family, state);
950 }
951
952 int in_addr_data_compare_func(const struct in_addr_data *x, const struct in_addr_data *y) {
953 int r;
954
955 assert(x);
956 assert(y);
957
958 r = CMP(x->family, y->family);
959 if (r != 0)
960 return r;
961
962 return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family));
963 }
964
965 DEFINE_HASH_OPS(
966 in_addr_data_hash_ops,
967 struct in_addr_data,
968 in_addr_data_hash_func,
969 in_addr_data_compare_func);
970
971 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
972 in_addr_data_hash_ops_free,
973 struct in_addr_data,
974 in_addr_data_hash_func,
975 in_addr_data_compare_func,
976 free);
977
978 void in6_addr_hash_func(const struct in6_addr *addr, struct siphash *state) {
979 assert(addr);
980 assert(state);
981
982 siphash24_compress_typesafe(*addr, state);
983 }
984
985 int in6_addr_compare_func(const struct in6_addr *a, const struct in6_addr *b) {
986 assert(a);
987 assert(b);
988
989 return memcmp(a, b, sizeof(*a));
990 }
991
992 DEFINE_HASH_OPS(
993 in6_addr_hash_ops,
994 struct in6_addr,
995 in6_addr_hash_func,
996 in6_addr_compare_func);
997
998 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
999 in6_addr_hash_ops_free,
1000 struct in6_addr,
1001 in6_addr_hash_func,
1002 in6_addr_compare_func,
1003 free);