]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/in-addr-util.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / basic / in-addr-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 <stdlib.h>
9
10 #include "alloc-util.h"
11 #include "in-addr-util.h"
12 #include "macro.h"
13 #include "parse-util.h"
14 #include "util.h"
15
16 bool in4_addr_is_null(const struct in_addr *a) {
17 assert(a);
18
19 return a->s_addr == 0;
20 }
21
22 int in_addr_is_null(int family, const union in_addr_union *u) {
23 assert(u);
24
25 if (family == AF_INET)
26 return in4_addr_is_null(&u->in);
27
28 if (family == AF_INET6)
29 return IN6_IS_ADDR_UNSPECIFIED(&u->in6);
30
31 return -EAFNOSUPPORT;
32 }
33
34 bool in4_addr_is_link_local(const struct in_addr *a) {
35 assert(a);
36
37 return (be32toh(a->s_addr) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
38 }
39
40 int in_addr_is_link_local(int family, const union in_addr_union *u) {
41 assert(u);
42
43 if (family == AF_INET)
44 return in4_addr_is_link_local(&u->in);
45
46 if (family == AF_INET6)
47 return IN6_IS_ADDR_LINKLOCAL(&u->in6);
48
49 return -EAFNOSUPPORT;
50 }
51
52 int in_addr_is_multicast(int family, const union in_addr_union *u) {
53 assert(u);
54
55 if (family == AF_INET)
56 return IN_MULTICAST(be32toh(u->in.s_addr));
57
58 if (family == AF_INET6)
59 return IN6_IS_ADDR_MULTICAST(&u->in6);
60
61 return -EAFNOSUPPORT;
62 }
63
64 bool in4_addr_is_localhost(const struct in_addr *a) {
65 assert(a);
66
67 /* All of 127.x.x.x is localhost. */
68 return (be32toh(a->s_addr) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
69 }
70
71 bool in4_addr_is_non_local(const struct in_addr *a) {
72 /* Whether the address is not null and not localhost.
73 *
74 * As such, it is suitable to configure as DNS/NTP server from DHCP. */
75 return !in4_addr_is_null(a) &&
76 !in4_addr_is_localhost(a);
77 }
78
79 int in_addr_is_localhost(int family, const union in_addr_union *u) {
80 assert(u);
81
82 if (family == AF_INET)
83 return in4_addr_is_localhost(&u->in);
84
85 if (family == AF_INET6)
86 return IN6_IS_ADDR_LOOPBACK(&u->in6);
87
88 return -EAFNOSUPPORT;
89 }
90
91 int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {
92 assert(a);
93 assert(b);
94
95 if (family == AF_INET)
96 return a->in.s_addr == b->in.s_addr;
97
98 if (family == AF_INET6)
99 return
100 a->in6.s6_addr32[0] == b->in6.s6_addr32[0] &&
101 a->in6.s6_addr32[1] == b->in6.s6_addr32[1] &&
102 a->in6.s6_addr32[2] == b->in6.s6_addr32[2] &&
103 a->in6.s6_addr32[3] == b->in6.s6_addr32[3];
104
105 return -EAFNOSUPPORT;
106 }
107
108 int in_addr_prefix_intersect(
109 int family,
110 const union in_addr_union *a,
111 unsigned aprefixlen,
112 const union in_addr_union *b,
113 unsigned bprefixlen) {
114
115 unsigned m;
116
117 assert(a);
118 assert(b);
119
120 /* Checks whether there are any addresses that are in both
121 * networks */
122
123 m = MIN(aprefixlen, bprefixlen);
124
125 if (family == AF_INET) {
126 uint32_t x, nm;
127
128 x = be32toh(a->in.s_addr ^ b->in.s_addr);
129 nm = (m == 0) ? 0 : 0xFFFFFFFFUL << (32 - m);
130
131 return (x & nm) == 0;
132 }
133
134 if (family == AF_INET6) {
135 unsigned i;
136
137 if (m > 128)
138 m = 128;
139
140 for (i = 0; i < 16; i++) {
141 uint8_t x, nm;
142
143 x = a->in6.s6_addr[i] ^ b->in6.s6_addr[i];
144
145 if (m < 8)
146 nm = 0xFF << (8 - m);
147 else
148 nm = 0xFF;
149
150 if ((x & nm) != 0)
151 return 0;
152
153 if (m > 8)
154 m -= 8;
155 else
156 m = 0;
157 }
158
159 return 1;
160 }
161
162 return -EAFNOSUPPORT;
163 }
164
165 int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen) {
166 assert(u);
167
168 /* Increases the network part of an address by one. Returns
169 * positive it that succeeds, or 0 if this overflows. */
170
171 if (prefixlen <= 0)
172 return 0;
173
174 if (family == AF_INET) {
175 uint32_t c, n;
176
177 if (prefixlen > 32)
178 prefixlen = 32;
179
180 c = be32toh(u->in.s_addr);
181 n = c + (1UL << (32 - prefixlen));
182 if (n < c)
183 return 0;
184 n &= 0xFFFFFFFFUL << (32 - prefixlen);
185
186 u->in.s_addr = htobe32(n);
187 return 1;
188 }
189
190 if (family == AF_INET6) {
191 struct in6_addr add = {}, result;
192 uint8_t overflow = 0;
193 unsigned i;
194
195 if (prefixlen > 128)
196 prefixlen = 128;
197
198 /* First calculate what we have to add */
199 add.s6_addr[(prefixlen-1) / 8] = 1 << (7 - (prefixlen-1) % 8);
200
201 for (i = 16; i > 0; i--) {
202 unsigned j = i - 1;
203
204 result.s6_addr[j] = u->in6.s6_addr[j] + add.s6_addr[j] + overflow;
205 overflow = (result.s6_addr[j] < u->in6.s6_addr[j]);
206 }
207
208 if (overflow)
209 return 0;
210
211 u->in6 = result;
212 return 1;
213 }
214
215 return -EAFNOSUPPORT;
216 }
217
218 int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
219 char *x;
220 size_t l;
221
222 assert(u);
223 assert(ret);
224
225 if (family == AF_INET)
226 l = INET_ADDRSTRLEN;
227 else if (family == AF_INET6)
228 l = INET6_ADDRSTRLEN;
229 else
230 return -EAFNOSUPPORT;
231
232 x = new(char, l);
233 if (!x)
234 return -ENOMEM;
235
236 errno = 0;
237 if (!inet_ntop(family, u, x, l)) {
238 free(x);
239 return errno > 0 ? -errno : -EINVAL;
240 }
241
242 *ret = x;
243 return 0;
244 }
245
246 int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifindex, char **ret) {
247 size_t l;
248 char *x;
249 int r;
250
251 assert(u);
252 assert(ret);
253
254 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
255 * handle IPv6 link-local addresses. */
256
257 if (family != AF_INET6)
258 goto fallback;
259 if (ifindex <= 0)
260 goto fallback;
261
262 r = in_addr_is_link_local(family, u);
263 if (r < 0)
264 return r;
265 if (r == 0)
266 goto fallback;
267
268 l = INET6_ADDRSTRLEN + 1 + DECIMAL_STR_MAX(ifindex) + 1;
269 x = new(char, l);
270 if (!x)
271 return -ENOMEM;
272
273 errno = 0;
274 if (!inet_ntop(family, u, x, l)) {
275 free(x);
276 return errno > 0 ? -errno : -EINVAL;
277 }
278
279 sprintf(strchr(x, 0), "%%%i", ifindex);
280 *ret = x;
281
282 return 0;
283
284 fallback:
285 return in_addr_to_string(family, u, ret);
286 }
287
288 int in_addr_from_string(int family, const char *s, union in_addr_union *ret) {
289 union in_addr_union buffer;
290 assert(s);
291
292 if (!IN_SET(family, AF_INET, AF_INET6))
293 return -EAFNOSUPPORT;
294
295 errno = 0;
296 if (inet_pton(family, s, ret ?: &buffer) <= 0)
297 return errno > 0 ? -errno : -EINVAL;
298
299 return 0;
300 }
301
302 int in_addr_from_string_auto(const char *s, int *ret_family, union in_addr_union *ret) {
303 int r;
304
305 assert(s);
306
307 r = in_addr_from_string(AF_INET, s, ret);
308 if (r >= 0) {
309 if (ret_family)
310 *ret_family = AF_INET;
311 return 0;
312 }
313
314 r = in_addr_from_string(AF_INET6, s, ret);
315 if (r >= 0) {
316 if (ret_family)
317 *ret_family = AF_INET6;
318 return 0;
319 }
320
321 return -EINVAL;
322 }
323
324 int in_addr_ifindex_from_string_auto(const char *s, int *family, union in_addr_union *ret, int *ifindex) {
325 _cleanup_free_ char *buf = NULL;
326 const char *suffix;
327 int r, ifi = 0;
328
329 assert(s);
330 assert(family);
331 assert(ret);
332
333 /* Similar to in_addr_from_string_auto() but also parses an optionally appended IPv6 zone suffix ("scope id")
334 * if one is found. */
335
336 suffix = strchr(s, '%');
337 if (suffix) {
338
339 if (ifindex) {
340 /* If we shall return the interface index, try to parse it */
341 r = parse_ifindex(suffix + 1, &ifi);
342 if (r < 0) {
343 unsigned u;
344
345 u = if_nametoindex(suffix + 1);
346 if (u <= 0)
347 return -errno;
348
349 ifi = (int) u;
350 }
351 }
352
353 buf = strndup(s, suffix - s);
354 if (!buf)
355 return -ENOMEM;
356
357 s = buf;
358 }
359
360 r = in_addr_from_string_auto(s, family, ret);
361 if (r < 0)
362 return r;
363
364 if (ifindex)
365 *ifindex = ifi;
366
367 return r;
368 }
369
370 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr *addr) {
371 assert(addr);
372
373 return 32U - u32ctz(be32toh(addr->s_addr));
374 }
375
376 struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen) {
377 assert(addr);
378 assert(prefixlen <= 32);
379
380 /* Shifting beyond 32 is not defined, handle this specially. */
381 if (prefixlen == 0)
382 addr->s_addr = 0;
383 else
384 addr->s_addr = htobe32((0xffffffff << (32 - prefixlen)) & 0xffffffff);
385
386 return addr;
387 }
388
389 int in4_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
390 uint8_t msb_octet = *(uint8_t*) addr;
391
392 /* addr may not be aligned, so make sure we only access it byte-wise */
393
394 assert(addr);
395 assert(prefixlen);
396
397 if (msb_octet < 128)
398 /* class A, leading bits: 0 */
399 *prefixlen = 8;
400 else if (msb_octet < 192)
401 /* class B, leading bits 10 */
402 *prefixlen = 16;
403 else if (msb_octet < 224)
404 /* class C, leading bits 110 */
405 *prefixlen = 24;
406 else
407 /* class D or E, no default prefixlen */
408 return -ERANGE;
409
410 return 0;
411 }
412
413 int in4_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
414 unsigned char prefixlen;
415 int r;
416
417 assert(addr);
418 assert(mask);
419
420 r = in4_addr_default_prefixlen(addr, &prefixlen);
421 if (r < 0)
422 return r;
423
424 in4_addr_prefixlen_to_netmask(mask, prefixlen);
425 return 0;
426 }
427
428 int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen) {
429 assert(addr);
430
431 if (family == AF_INET) {
432 struct in_addr mask;
433
434 if (!in4_addr_prefixlen_to_netmask(&mask, prefixlen))
435 return -EINVAL;
436
437 addr->in.s_addr &= mask.s_addr;
438 return 0;
439 }
440
441 if (family == AF_INET6) {
442 unsigned i;
443
444 for (i = 0; i < 16; i++) {
445 uint8_t mask;
446
447 if (prefixlen >= 8) {
448 mask = 0xFF;
449 prefixlen -= 8;
450 } else {
451 mask = 0xFF << (8 - prefixlen);
452 prefixlen = 0;
453 }
454
455 addr->in6.s6_addr[i] &= mask;
456 }
457
458 return 0;
459 }
460
461 return -EAFNOSUPPORT;
462 }
463
464 int in_addr_prefix_covers(int family,
465 const union in_addr_union *prefix,
466 unsigned char prefixlen,
467 const union in_addr_union *address) {
468
469 union in_addr_union masked_prefix, masked_address;
470 int r;
471
472 assert(prefix);
473 assert(address);
474
475 masked_prefix = *prefix;
476 r = in_addr_mask(family, &masked_prefix, prefixlen);
477 if (r < 0)
478 return r;
479
480 masked_address = *address;
481 r = in_addr_mask(family, &masked_address, prefixlen);
482 if (r < 0)
483 return r;
484
485 return in_addr_equal(family, &masked_prefix, &masked_address);
486 }
487
488 int in_addr_parse_prefixlen(int family, const char *p, unsigned char *ret) {
489 uint8_t u;
490 int r;
491
492 if (!IN_SET(family, AF_INET, AF_INET6))
493 return -EAFNOSUPPORT;
494
495 r = safe_atou8(p, &u);
496 if (r < 0)
497 return r;
498
499 if (u > FAMILY_ADDRESS_SIZE(family) * 8)
500 return -ERANGE;
501
502 *ret = u;
503 return 0;
504 }
505
506 int in_addr_prefix_from_string(
507 const char *p,
508 int family,
509 union in_addr_union *ret_prefix,
510 unsigned char *ret_prefixlen) {
511
512 _cleanup_free_ char *str = NULL;
513 union in_addr_union buffer;
514 const char *e, *l;
515 unsigned char k;
516 int r;
517
518 assert(p);
519
520 if (!IN_SET(family, AF_INET, AF_INET6))
521 return -EAFNOSUPPORT;
522
523 e = strchr(p, '/');
524 if (e) {
525 str = strndup(p, e - p);
526 if (!str)
527 return -ENOMEM;
528
529 l = str;
530 } else
531 l = p;
532
533 r = in_addr_from_string(family, l, &buffer);
534 if (r < 0)
535 return r;
536
537 if (e) {
538 r = in_addr_parse_prefixlen(family, e+1, &k);
539 if (r < 0)
540 return r;
541 } else
542 k = FAMILY_ADDRESS_SIZE(family) * 8;
543
544 if (ret_prefix)
545 *ret_prefix = buffer;
546 if (ret_prefixlen)
547 *ret_prefixlen = k;
548
549 return 0;
550 }
551
552 int in_addr_prefix_from_string_auto_internal(
553 const char *p,
554 InAddrPrefixLenMode mode,
555 int *ret_family,
556 union in_addr_union *ret_prefix,
557 unsigned char *ret_prefixlen) {
558
559 _cleanup_free_ char *str = NULL;
560 union in_addr_union buffer;
561 const char *e, *l;
562 unsigned char k;
563 int family, r;
564
565 assert(p);
566
567 e = strchr(p, '/');
568 if (e) {
569 str = strndup(p, e - p);
570 if (!str)
571 return -ENOMEM;
572
573 l = str;
574 } else
575 l = p;
576
577 r = in_addr_from_string_auto(l, &family, &buffer);
578 if (r < 0)
579 return r;
580
581 if (e) {
582 r = in_addr_parse_prefixlen(family, e+1, &k);
583 if (r < 0)
584 return r;
585 } else
586 switch (mode) {
587 case PREFIXLEN_FULL:
588 k = FAMILY_ADDRESS_SIZE(family) * 8;
589 break;
590 case PREFIXLEN_REFUSE:
591 return -ENOANO; /* To distinguish this error from others. */
592 case PREFIXLEN_LEGACY:
593 if (family == AF_INET) {
594 r = in4_addr_default_prefixlen(&buffer.in, &k);
595 if (r < 0)
596 return r;
597 } else
598 k = 0;
599 break;
600 default:
601 assert_not_reached("Invalid prefixlen mode");
602 }
603
604 if (ret_family)
605 *ret_family = family;
606 if (ret_prefix)
607 *ret_prefix = buffer;
608 if (ret_prefixlen)
609 *ret_prefixlen = k;
610
611 return 0;
612
613 }
614
615 static void in_addr_data_hash_func(const struct in_addr_data *a, struct siphash *state) {
616 siphash24_compress(&a->family, sizeof(a->family), state);
617 siphash24_compress(&a->address, FAMILY_ADDRESS_SIZE(a->family), state);
618 }
619
620 static int in_addr_data_compare_func(const struct in_addr_data *x, const struct in_addr_data *y) {
621 int r;
622
623 r = CMP(x->family, y->family);
624 if (r != 0)
625 return r;
626
627 return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family));
628 }
629
630 DEFINE_HASH_OPS(in_addr_data_hash_ops, struct in_addr_data, in_addr_data_hash_func, in_addr_data_compare_func);