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