]>
Commit | Line | Data |
---|---|---|
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 | 16 | bool 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 | 22 | int 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 |
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 | ||
af93291c LP |
40 | int 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 |
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 | ||
fdedbe26 LP |
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 | ||
d830ebbd LP |
71 | int 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 | 83 | int 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 | ||
100 | int 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 | 157 | int 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 | 210 | int 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 |
238 | int 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 | ||
276 | fallback: | |
277 | return in_addr_to_string(family, u, ret); | |
278 | } | |
279 | ||
0dd25fb9 | 280 | int 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 | 294 | int 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 LP |
316 | int in_addr_ifindex_from_string_auto(const char *s, int *family, union in_addr_union *ret, int *ifindex) { |
317 | const char *suffix; | |
318 | int r, ifi = 0; | |
319 | ||
320 | assert(s); | |
321 | assert(family); | |
322 | assert(ret); | |
323 | ||
324 | /* Similar to in_addr_from_string_auto() but also parses an optionally appended IPv6 zone suffix ("scope id") | |
325 | * if one is found. */ | |
326 | ||
327 | suffix = strchr(s, '%'); | |
328 | if (suffix) { | |
329 | ||
330 | if (ifindex) { | |
331 | /* If we shall return the interface index, try to parse it */ | |
332 | r = parse_ifindex(suffix + 1, &ifi); | |
333 | if (r < 0) { | |
334 | unsigned u; | |
335 | ||
336 | u = if_nametoindex(suffix + 1); | |
337 | if (u <= 0) | |
338 | return -errno; | |
339 | ||
340 | ifi = (int) u; | |
341 | } | |
342 | } | |
343 | ||
344 | s = strndupa(s, suffix - s); | |
345 | } | |
346 | ||
347 | r = in_addr_from_string_auto(s, family, ret); | |
348 | if (r < 0) | |
349 | return r; | |
350 | ||
351 | if (ifindex) | |
352 | *ifindex = ifi; | |
353 | ||
354 | return r; | |
355 | } | |
356 | ||
5a941f5f | 357 | unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr *addr) { |
44e7b949 LP |
358 | assert(addr); |
359 | ||
360 | return 32 - u32ctz(be32toh(addr->s_addr)); | |
361 | } | |
df40eee8 | 362 | |
5a941f5f | 363 | struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen) { |
76917807 LP |
364 | assert(addr); |
365 | assert(prefixlen <= 32); | |
366 | ||
367 | /* Shifting beyond 32 is not defined, handle this specially. */ | |
368 | if (prefixlen == 0) | |
369 | addr->s_addr = 0; | |
370 | else | |
371 | addr->s_addr = htobe32((0xffffffff << (32 - prefixlen)) & 0xffffffff); | |
372 | ||
373 | return addr; | |
374 | } | |
375 | ||
5a941f5f | 376 | int in4_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) { |
1caa12d0 TG |
377 | uint8_t msb_octet = *(uint8_t*) addr; |
378 | ||
379 | /* addr may not be aligned, so make sure we only access it byte-wise */ | |
df40eee8 TG |
380 | |
381 | assert(addr); | |
df40eee8 TG |
382 | assert(prefixlen); |
383 | ||
1caa12d0 | 384 | if (msb_octet < 128) |
df40eee8 TG |
385 | /* class A, leading bits: 0 */ |
386 | *prefixlen = 8; | |
1caa12d0 | 387 | else if (msb_octet < 192) |
df40eee8 TG |
388 | /* class B, leading bits 10 */ |
389 | *prefixlen = 16; | |
1caa12d0 | 390 | else if (msb_octet < 224) |
df40eee8 TG |
391 | /* class C, leading bits 110 */ |
392 | *prefixlen = 24; | |
393 | else | |
394 | /* class D or E, no default prefixlen */ | |
395 | return -ERANGE; | |
396 | ||
397 | return 0; | |
398 | } | |
399 | ||
5a941f5f | 400 | int in4_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) { |
df40eee8 TG |
401 | unsigned char prefixlen; |
402 | int r; | |
403 | ||
404 | assert(addr); | |
405 | assert(mask); | |
406 | ||
5a941f5f | 407 | r = in4_addr_default_prefixlen(addr, &prefixlen); |
df40eee8 TG |
408 | if (r < 0) |
409 | return r; | |
410 | ||
5a941f5f | 411 | in4_addr_prefixlen_to_netmask(mask, prefixlen); |
df40eee8 TG |
412 | return 0; |
413 | } | |
5a8bcb67 LP |
414 | |
415 | int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen) { | |
416 | assert(addr); | |
417 | ||
418 | if (family == AF_INET) { | |
419 | struct in_addr mask; | |
420 | ||
5a941f5f | 421 | if (!in4_addr_prefixlen_to_netmask(&mask, prefixlen)) |
5a8bcb67 LP |
422 | return -EINVAL; |
423 | ||
424 | addr->in.s_addr &= mask.s_addr; | |
425 | return 0; | |
426 | } | |
427 | ||
428 | if (family == AF_INET6) { | |
429 | unsigned i; | |
430 | ||
431 | for (i = 0; i < 16; i++) { | |
432 | uint8_t mask; | |
433 | ||
434 | if (prefixlen >= 8) { | |
435 | mask = 0xFF; | |
436 | prefixlen -= 8; | |
437 | } else { | |
438 | mask = 0xFF << (8 - prefixlen); | |
439 | prefixlen = 0; | |
440 | } | |
441 | ||
442 | addr->in6.s6_addr[i] &= mask; | |
443 | } | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
448 | return -EAFNOSUPPORT; | |
449 | } | |
f7bf1abe | 450 | |
1274b6c6 LP |
451 | int in_addr_prefix_covers(int family, |
452 | const union in_addr_union *prefix, | |
453 | unsigned char prefixlen, | |
454 | const union in_addr_union *address) { | |
455 | ||
456 | union in_addr_union masked_prefix, masked_address; | |
457 | int r; | |
458 | ||
459 | assert(prefix); | |
460 | assert(address); | |
461 | ||
462 | masked_prefix = *prefix; | |
463 | r = in_addr_mask(family, &masked_prefix, prefixlen); | |
464 | if (r < 0) | |
465 | return r; | |
466 | ||
467 | masked_address = *address; | |
468 | r = in_addr_mask(family, &masked_address, prefixlen); | |
469 | if (r < 0) | |
470 | return r; | |
471 | ||
472 | return in_addr_equal(family, &masked_prefix, &masked_address); | |
473 | } | |
474 | ||
f4912f3a LP |
475 | int in_addr_parse_prefixlen(int family, const char *p, unsigned char *ret) { |
476 | uint8_t u; | |
477 | int r; | |
478 | ||
479 | if (!IN_SET(family, AF_INET, AF_INET6)) | |
480 | return -EAFNOSUPPORT; | |
481 | ||
482 | r = safe_atou8(p, &u); | |
483 | if (r < 0) | |
484 | return r; | |
485 | ||
486 | if (u > FAMILY_ADDRESS_SIZE(family) * 8) | |
487 | return -ERANGE; | |
488 | ||
489 | *ret = u; | |
490 | return 0; | |
491 | } | |
492 | ||
493 | int in_addr_prefix_from_string( | |
494 | const char *p, | |
495 | int family, | |
496 | union in_addr_union *ret_prefix, | |
497 | unsigned char *ret_prefixlen) { | |
498 | ||
f7bf1abe SS |
499 | union in_addr_union buffer; |
500 | const char *e, *l; | |
f4912f3a | 501 | unsigned char k; |
f7bf1abe SS |
502 | int r; |
503 | ||
504 | assert(p); | |
505 | ||
506 | if (!IN_SET(family, AF_INET, AF_INET6)) | |
507 | return -EAFNOSUPPORT; | |
508 | ||
509 | e = strchr(p, '/'); | |
510 | if (e) | |
511 | l = strndupa(p, e - p); | |
512 | else | |
513 | l = p; | |
514 | ||
515 | r = in_addr_from_string(family, l, &buffer); | |
516 | if (r < 0) | |
517 | return r; | |
518 | ||
f7bf1abe | 519 | if (e) { |
f4912f3a | 520 | r = in_addr_parse_prefixlen(family, e+1, &k); |
f7bf1abe SS |
521 | if (r < 0) |
522 | return r; | |
f4912f3a LP |
523 | } else |
524 | k = FAMILY_ADDRESS_SIZE(family) * 8; | |
f7bf1abe | 525 | |
f4912f3a LP |
526 | if (ret_prefix) |
527 | *ret_prefix = buffer; | |
528 | if (ret_prefixlen) | |
529 | *ret_prefixlen = k; | |
f7bf1abe | 530 | |
f4912f3a LP |
531 | return 0; |
532 | } | |
f7bf1abe | 533 | |
f4912f3a LP |
534 | int in_addr_prefix_from_string_auto( |
535 | const char *p, | |
536 | int *ret_family, | |
537 | union in_addr_union *ret_prefix, | |
538 | unsigned char *ret_prefixlen) { | |
539 | ||
540 | union in_addr_union buffer; | |
541 | const char *e, *l; | |
542 | unsigned char k; | |
543 | int family, r; | |
544 | ||
545 | assert(p); | |
546 | ||
547 | e = strchr(p, '/'); | |
548 | if (e) | |
549 | l = strndupa(p, e - p); | |
550 | else | |
551 | l = p; | |
552 | ||
553 | r = in_addr_from_string_auto(l, &family, &buffer); | |
554 | if (r < 0) | |
555 | return r; | |
556 | ||
557 | if (e) { | |
558 | r = in_addr_parse_prefixlen(family, e+1, &k); | |
559 | if (r < 0) | |
560 | return r; | |
561 | } else | |
562 | k = FAMILY_ADDRESS_SIZE(family) * 8; | |
563 | ||
564 | if (ret_family) | |
565 | *ret_family = family; | |
566 | if (ret_prefix) | |
567 | *ret_prefix = buffer; | |
568 | if (ret_prefixlen) | |
569 | *ret_prefixlen = k; | |
f7bf1abe SS |
570 | |
571 | return 0; | |
f4912f3a | 572 | |
f7bf1abe | 573 | } |
6c39e026 YW |
574 | |
575 | void in_addr_data_hash_func(const void *p, struct siphash *state) { | |
576 | const struct in_addr_data *a = p; | |
577 | ||
578 | siphash24_compress(&a->family, sizeof(a->family), state); | |
23577246 | 579 | siphash24_compress(&a->address, FAMILY_ADDRESS_SIZE(a->family), state); |
6c39e026 YW |
580 | } |
581 | ||
582 | int in_addr_data_compare_func(const void *a, const void *b) { | |
583 | const struct in_addr_data *x = a, *y = b; | |
a0edd02e | 584 | int r; |
6c39e026 | 585 | |
a0edd02e FB |
586 | r = CMP(x->family, y->family); |
587 | if (r != 0) | |
588 | return r; | |
6c39e026 | 589 | |
23577246 | 590 | return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family)); |
6c39e026 YW |
591 | } |
592 | ||
593 | const struct hash_ops in_addr_data_hash_ops = { | |
594 | .hash = in_addr_data_hash_func, | |
595 | .compare = in_addr_data_compare_func, | |
596 | }; |