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