]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-address.c
network: rename netdev kind virtual-wlan -> wlan
[thirdparty/systemd.git] / src / network / networkd-address.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
f579559b
TG
2
3#include <net/if.h>
b8162cd2 4#include <net/if_arp.h>
f579559b 5
b5efdb8a 6#include "alloc-util.h"
12c2884c 7#include "firewall-util.h"
0a970718 8#include "memory-util.h"
fc2f9534 9#include "netlink-util.h"
bfbf150e 10#include "networkd-address-pool.h"
6bedfcbb 11#include "networkd-address.h"
3b6a3bde 12#include "networkd-dhcp-server.h"
76a86ffd 13#include "networkd-ipv4acd.h"
23f53b99 14#include "networkd-manager.h"
093e3533 15#include "networkd-network.h"
76c5a0f2 16#include "networkd-queue.h"
d8c472f2 17#include "networkd-route-util.h"
3b6a3bde 18#include "networkd-route.h"
6bedfcbb 19#include "parse-util.h"
07630cea 20#include "string-util.h"
51517f9e 21#include "strv.h"
0fd97a25 22#include "strxcpyx.h"
f579559b 23
1b566071 24#define ADDRESSES_PER_LINK_MAX 2048U
8c34b963
LP
25#define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
26
42ceb474 27int address_flags_to_string_alloc(uint32_t flags, int family, char **ret) {
a2362484 28 _cleanup_free_ char *str = NULL;
7396e014
ZJS
29 static const char* map[] = {
30 [LOG2U(IFA_F_SECONDARY)] = "secondary", /* This is also called "temporary" for ipv6. */
31 [LOG2U(IFA_F_NODAD)] = "nodad",
32 [LOG2U(IFA_F_OPTIMISTIC)] = "optimistic",
33 [LOG2U(IFA_F_DADFAILED)] = "dadfailed",
34 [LOG2U(IFA_F_HOMEADDRESS)] = "home-address",
35 [LOG2U(IFA_F_DEPRECATED)] = "deprecated",
36 [LOG2U(IFA_F_TENTATIVE)] = "tentative",
37 [LOG2U(IFA_F_PERMANENT)] = "permanent",
38 [LOG2U(IFA_F_MANAGETEMPADDR)] = "manage-temporary-address",
39 [LOG2U(IFA_F_NOPREFIXROUTE)] = "no-prefixroute",
40 [LOG2U(IFA_F_MCAUTOJOIN)] = "auto-join",
41 [LOG2U(IFA_F_STABLE_PRIVACY)] = "stable-privacy",
a2362484
YW
42 };
43
44 assert(IN_SET(family, AF_INET, AF_INET6));
45 assert(ret);
46
0828a386 47 for (size_t i = 0; i < ELEMENTSOF(map); i++)
7396e014
ZJS
48 if (FLAGS_SET(flags, 1 << i) && map[i])
49 if (!strextend_with_separator(
50 &str, ",",
51 family == AF_INET6 && (1 << i) == IFA_F_SECONDARY ? "temporary" : map[i]))
52 return -ENOMEM;
a2362484
YW
53
54 *ret = TAKE_PTR(str);
55 return 0;
56}
57
f0213e37 58int address_new(Address **ret) {
8e766630 59 _cleanup_(address_freep) Address *address = NULL;
f0213e37 60
17f9c355 61 address = new(Address, 1);
f0213e37
TG
62 if (!address)
63 return -ENOMEM;
aba496a5 64
17f9c355
YW
65 *address = (Address) {
66 .family = AF_UNSPEC,
67 .scope = RT_SCOPE_UNIVERSE,
16bc8635
YW
68 .lifetime_valid_usec = USEC_INFINITY,
69 .lifetime_preferred_usec = USEC_INFINITY,
832583ad 70 .set_broadcast = -1,
17f9c355 71 };
f0213e37 72
1cc6c93a 73 *ret = TAKE_PTR(address);
f0213e37
TG
74
75 return 0;
aba496a5
UTL
76}
77
9560e5b3 78static int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
307fe3cd 79 _cleanup_(config_section_freep) ConfigSection *n = NULL;
8e766630 80 _cleanup_(address_freep) Address *address = NULL;
f0213e37 81 int r;
f579559b 82
8c34b963
LP
83 assert(network);
84 assert(ret);
9cd9fc8f
YW
85 assert(filename);
86 assert(section_line > 0);
f4859fc7 87
307fe3cd 88 r = config_section_new(filename, section_line, &n);
9cd9fc8f
YW
89 if (r < 0)
90 return r;
6ae115c1 91
9cd9fc8f
YW
92 address = ordered_hashmap_get(network->addresses_by_section, n);
93 if (address) {
94 *ret = TAKE_PTR(address);
95 return 0;
6ae115c1
TG
96 }
97
9cd9fc8f 98 if (ordered_hashmap_size(network->addresses_by_section) >= STATIC_ADDRESSES_PER_NETWORK_MAX)
8c34b963
LP
99 return -E2BIG;
100
f0213e37
TG
101 r = address_new(&address);
102 if (r < 0)
103 return r;
801bd9e8 104
0f7f2769 105 address->network = network;
9cd9fc8f 106 address->section = TAKE_PTR(n);
3b6a3bde 107 address->source = NETWORK_CONFIG_SOURCE_STATIC;
2859932b
YW
108 /* This will be adjusted in address_section_verify(). */
109 address->duplicate_address_detection = _ADDRESS_FAMILY_INVALID;
0f7f2769 110
307fe3cd 111 r = ordered_hashmap_ensure_put(&network->addresses_by_section, &config_section_hash_ops, address->section, address);
9cd9fc8f
YW
112 if (r < 0)
113 return r;
6ae115c1 114
1cc6c93a 115 *ret = TAKE_PTR(address);
f579559b
TG
116 return 0;
117}
118
7818f858 119Address *address_free(Address *address) {
f579559b 120 if (!address)
7818f858 121 return NULL;
f579559b 122
f048a16b 123 if (address->network) {
9cd9fc8f
YW
124 assert(address->section);
125 ordered_hashmap_remove(address->network->addresses_by_section, address->section);
f048a16b 126 }
6ae115c1 127
490ccbd5 128 if (address->link) {
cf1d700d 129 set_remove(address->link->addresses, address);
f150100a 130
94876904
YW
131 if (address->family == AF_INET6 &&
132 in6_addr_equal(&address->in_addr.in6, &address->link->ipv6ll_address))
f150100a 133 memzero(&address->link->ipv6ll_address, sizeof(struct in6_addr));
adda1ed9 134 }
cf1d700d 135
051e77ca
SS
136 sd_ipv4acd_unref(address->acd);
137
307fe3cd 138 config_section_free(address->section);
de4224aa 139 free(address->label);
7818f858 140 return mfree(address);
f579559b
TG
141}
142
3b6a3bde
YW
143bool address_is_ready(const Address *a) {
144 assert(a);
145
146 if (FLAGS_SET(a->flags, IFA_F_TENTATIVE))
147 return false;
148
149 if (FLAGS_SET(a->state, NETWORK_CONFIG_STATE_REMOVING))
150 return false;
151
152 if (FLAGS_SET(a->state, NETWORK_CONFIG_STATE_PROBING))
153 return false;
154
155 if (!FLAGS_SET(a->state, NETWORK_CONFIG_STATE_CONFIGURED))
156 return false;
157
158 return true;
159}
160
161void link_mark_addresses(Link *link, NetworkConfigSource source, const struct in6_addr *router) {
162 Address *a;
163
164 assert(link);
165
166 SET_FOREACH(a, link->addresses) {
167 if (a->source != source)
168 continue;
169
170 if (source == NETWORK_CONFIG_SOURCE_NDISC &&
171 router && !in6_addr_equal(router, &a->provider.in6))
172 continue;
173
174 address_mark(a);
175 }
176}
177
e680486d 178static bool address_needs_to_set_broadcast(const Address *a, Link *link) {
2a236f9f 179 assert(a);
e680486d 180 assert(link);
2a236f9f 181
473680be
YW
182 if (a->family != AF_INET)
183 return false;
184
185 if (in4_addr_is_set(&a->in_addr_peer.in))
186 return false;
187
2a236f9f
YW
188 /* A /31 or /32 IPv4 address does not have a broadcast address.
189 * See https://tools.ietf.org/html/rfc3021 */
473680be
YW
190 if (a->prefixlen > 30)
191 return false;
192
473680be
YW
193 /* If explicitly configured, do not update the address. */
194 if (in4_addr_is_set(&a->broadcast))
e680486d 195 return false;
473680be 196
e680486d
YW
197 if (a->set_broadcast >= 0)
198 return a->set_broadcast;
473680be 199
e680486d
YW
200 /* Defaults to true, except for wireguard, as typical configuration for wireguard does not set
201 * broadcast. */
202 return !streq_ptr(link->kind, "wireguard");
2a236f9f
YW
203}
204
e680486d 205void address_set_broadcast(Address *a, Link *link) {
832583ad
YW
206 assert(a);
207 assert(link);
208
e680486d
YW
209 if (!address_needs_to_set_broadcast(a, link))
210 return;
832583ad 211
e680486d 212 a->broadcast.s_addr = a->in_addr.in.s_addr | htobe32(UINT32_C(0xffffffff) >> a->prefixlen);
832583ad
YW
213}
214
16bc8635
YW
215static struct ifa_cacheinfo *address_set_cinfo(const Address *a, struct ifa_cacheinfo *cinfo) {
216 usec_t now_usec;
217
218 assert(a);
219 assert(cinfo);
220
221 now_usec = now(clock_boottime_or_monotonic());
222
223 *cinfo = (struct ifa_cacheinfo) {
224 .ifa_valid = MIN(usec_sub_unsigned(a->lifetime_valid_usec, now_usec) / USEC_PER_SEC, UINT32_MAX),
225 .ifa_prefered = MIN(usec_sub_unsigned(a->lifetime_preferred_usec, now_usec) / USEC_PER_SEC, UINT32_MAX),
226 };
227
228 return cinfo;
229}
230
231static void address_set_lifetime(Address *a, const struct ifa_cacheinfo *cinfo) {
232 usec_t now_usec;
233
234 assert(a);
235 assert(cinfo);
236
237 now_usec = now(clock_boottime_or_monotonic());
238
239 if (cinfo->ifa_valid == UINT32_MAX)
240 a->lifetime_valid_usec = USEC_INFINITY;
241 else
242 a->lifetime_valid_usec = usec_add(cinfo->ifa_valid * USEC_PER_SEC, now_usec);
243
244 if (cinfo->ifa_prefered == UINT32_MAX)
245 a->lifetime_preferred_usec = USEC_INFINITY;
246 else
247 a->lifetime_preferred_usec = usec_add(cinfo->ifa_prefered * USEC_PER_SEC, now_usec);
248}
249
1d30fc5c
YW
250static uint32_t address_prefix(const Address *a) {
251 assert(a);
252
253 /* make sure we don't try to shift by 32.
254 * See ISO/IEC 9899:TC3 § 6.5.7.3. */
255 if (a->prefixlen == 0)
256 return 0;
257
258 if (a->in_addr_peer.in.s_addr != 0)
259 return be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen);
260 else
261 return be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen);
262}
263
5d003031 264static void address_kernel_hash_func(const Address *a, struct siphash *state) {
3ac8e543
TG
265 assert(a);
266
267 siphash24_compress(&a->family, sizeof(a->family), state);
268
1d30fc5c
YW
269 switch (a->family) {
270 case AF_INET:
271 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
3ac8e543 272
1d30fc5c
YW
273 uint32_t prefix = address_prefix(a);
274 siphash24_compress(&prefix, sizeof(prefix), state);
3ac8e543 275
1d30fc5c
YW
276 _fallthrough_;
277 case AF_INET6:
278 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
279 break;
280 default:
281 /* treat any other address family as AF_UNSPEC */
282 break;
283 }
3ac8e543
TG
284}
285
5d003031 286static int address_kernel_compare_func(const Address *a1, const Address *a2) {
a0edd02e 287 int r;
3ac8e543 288
a0edd02e
FB
289 r = CMP(a1->family, a2->family);
290 if (r != 0)
291 return r;
3ac8e543 292
1d30fc5c
YW
293 switch (a1->family) {
294 case AF_INET:
295 /* See kernel's find_matching_ifa() in net/ipv4/devinet.c */
296 r = CMP(a1->prefixlen, a2->prefixlen);
24be9181
ZJS
297 if (r != 0)
298 return r;
3ac8e543 299
1d30fc5c
YW
300 r = CMP(address_prefix(a1), address_prefix(a2));
301 if (r != 0)
302 return r;
2a236f9f 303
1d30fc5c
YW
304 _fallthrough_;
305 case AF_INET6:
306 /* See kernel's ipv6_get_ifaddr() in net/ipv6/addrconf.c */
307 return memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family));
308 default:
309 /* treat any other address family as AF_UNSPEC */
310 return 0;
311 }
3ac8e543
TG
312}
313
50783f91 314DEFINE_PRIVATE_HASH_OPS(
5d003031 315 address_kernel_hash_ops,
50783f91 316 Address,
5d003031
YW
317 address_kernel_hash_func,
318 address_kernel_compare_func);
319
09d09207 320static void address_hash_func(const Address *a, struct siphash *state) {
5d003031
YW
321 assert(a);
322
323 siphash24_compress(&a->family, sizeof(a->family), state);
324
325 /* treat any other address family as AF_UNSPEC */
326 if (!IN_SET(a->family, AF_INET, AF_INET6))
327 return;
328
329 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
330 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
331 siphash24_compress(&a->in_addr_peer, FAMILY_ADDRESS_SIZE(a->family), state);
390247d2
YW
332
333 if (a->family == AF_INET) {
334 /* On update, the kernel ignores the address label and broadcast address, hence we need
335 * to distinguish addresses with different labels or broadcast addresses. Otherwise,
336 * the label or broadcast address change will not be applied when we reconfigure the
337 * interface. */
338 siphash24_compress_string(a->label, state);
339 siphash24_compress(&a->broadcast, sizeof(a->broadcast), state);
340 }
5d003031
YW
341}
342
343int address_compare_func(const Address *a1, const Address *a2) {
344 int r;
345
346 r = CMP(a1->family, a2->family);
347 if (r != 0)
348 return r;
349
350 if (!IN_SET(a1->family, AF_INET, AF_INET6))
351 return 0;
352
353 r = CMP(a1->prefixlen, a2->prefixlen);
354 if (r != 0)
355 return r;
356
357 r = memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family));
358 if (r != 0)
359 return r;
360
361 r = memcmp(&a1->in_addr_peer, &a2->in_addr_peer, FAMILY_ADDRESS_SIZE(a1->family));
362 if (r != 0)
363 return r;
364
390247d2
YW
365 if (a1->family == AF_INET) {
366 r = strcmp_ptr(a1->label, a2->label);
367 if (r != 0)
368 return r;
369
370 r = CMP(a1->broadcast.s_addr, a2->broadcast.s_addr);
371 if (r != 0)
372 return r;
373 }
374
5d003031
YW
375 return 0;
376}
50783f91 377
6bf03f6f 378DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
5b43c2c8 379 address_hash_ops_free,
6bf03f6f
YW
380 Address,
381 address_hash_func,
382 address_compare_func,
383 address_free);
3ac8e543 384
99b46669
YW
385int address_dup(const Address *src, Address **ret) {
386 _cleanup_(address_freep) Address *dest = NULL;
cde1f0e8
YW
387 int r;
388
cde1f0e8 389 assert(src);
99b46669
YW
390 assert(ret);
391
392 dest = newdup(Address, src, 1);
393 if (!dest)
394 return -ENOMEM;
395
396 /* clear all pointers */
397 dest->network = NULL;
398 dest->section = NULL;
399 dest->link = NULL;
400 dest->label = NULL;
401 dest->acd = NULL;
cde1f0e8 402
2a236f9f
YW
403 if (src->family == AF_INET) {
404 r = free_and_strdup(&dest->label, src->label);
405 if (r < 0)
406 return r;
407 }
cde1f0e8 408
99b46669 409 *ret = TAKE_PTR(dest);
cde1f0e8
YW
410 return 0;
411}
412
494b6b43
YW
413static int address_set_masquerade(Address *address, bool add) {
414 union in_addr_union masked;
91b5f997
TG
415 int r;
416
417 assert(address);
494b6b43 418 assert(address->link);
91b5f997 419
494b6b43
YW
420 if (!address->link->network)
421 return 0;
91b5f997 422
48ed2766
FW
423 if (address->family == AF_INET &&
424 !FLAGS_SET(address->link->network->ip_masquerade, ADDRESS_FAMILY_IPV4))
494b6b43 425 return 0;
91b5f997 426
48ed2766
FW
427 if (address->family == AF_INET6 &&
428 !FLAGS_SET(address->link->network->ip_masquerade, ADDRESS_FAMILY_IPV6))
494b6b43 429 return 0;
91b5f997 430
494b6b43
YW
431 if (address->scope >= RT_SCOPE_LINK)
432 return 0;
433
da0e2bae 434 if (address->ip_masquerade_done == add)
494b6b43
YW
435 return 0;
436
437 masked = address->in_addr;
438 r = in_addr_mask(address->family, &masked, address->prefixlen);
439 if (r < 0)
440 return r;
441
48ed2766 442 r = fw_add_masquerade(&address->link->manager->fw_ctx, add, address->family, &masked, address->prefixlen);
494b6b43
YW
443 if (r < 0)
444 return r;
445
da0e2bae 446 address->ip_masquerade_done = add;
91b5f997
TG
447
448 return 0;
449}
450
3b6a3bde 451static int address_add(Link *link, Address *address) {
cf1d700d
TG
452 int r;
453
454 assert(link);
3b6a3bde 455 assert(address);
cf1d700d 456
5b43c2c8 457 r = set_ensure_put(&link->addresses, &address_hash_ops_free, address);
cf1d700d
TG
458 if (r < 0)
459 return r;
75a302b5
YW
460 if (r == 0)
461 return -EEXIST;
cf1d700d
TG
462
463 address->link = link;
7b3a7581 464 return 0;
adda1ed9
TG
465}
466
f9831557 467static int address_update(Address *address) {
2c40a889 468 Link *link;
e7ab854c 469 int r;
36c32f61
TG
470
471 assert(address);
ea121d8f 472 assert(address->link);
36c32f61 473
2c40a889 474 link = address->link;
36c32f61 475
7657ec3e
YW
476 if (address_is_ready(address) &&
477 address->family == AF_INET6 &&
478 in6_addr_is_link_local(&address->in_addr.in6) &&
479 in6_addr_is_null(&link->ipv6ll_address)) {
480
481 link->ipv6ll_address = address->in_addr.in6;
482
483 r = link_ipv6ll_gained(link);
484 if (r < 0)
485 return r;
486 }
487
2c40a889 488 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
ea121d8f
YW
489 return 0;
490
7a8685ff
YW
491 r = address_set_masquerade(address, true);
492 if (r < 0)
493 return log_link_warning_errno(link, r, "Could not enable IP masquerading: %m");
494
981fab5d
YW
495 if (address_is_ready(address) && address->callback) {
496 r = address->callback(address);
497 if (r < 0)
498 return r;
a3a019e1 499 }
36c32f61 500
981fab5d
YW
501 link_update_operstate(link, true);
502 link_check_ready(link);
36c32f61
TG
503 return 0;
504}
505
67a58eb3 506static int address_drop(Address *address) {
8012cd39
TG
507 Link *link;
508 bool ready;
7750b796 509 int r;
8012cd39 510
3b6a3bde
YW
511 assert(address);
512 assert(address->link);
91b5f997 513
8012cd39
TG
514 ready = address_is_ready(address);
515 link = address->link;
516
494b6b43 517 r = address_set_masquerade(address, false);
7750b796
YW
518 if (r < 0)
519 log_link_warning_errno(link, r, "Failed to disable IP masquerading, ignoring: %m");
520
3b6a3bde
YW
521 if (address->state == 0)
522 address_free(address);
91b5f997 523
959f65d3 524 link_update_operstate(link, true);
84de38c5 525
8012cd39
TG
526 if (link && !ready)
527 link_check_ready(link);
528
91b5f997
TG
529 return 0;
530}
531
fe841414
YW
532int address_get(Link *link, const Address *in, Address **ret) {
533 Address *existing;
91b5f997 534
5a8bcb67 535 assert(link);
fe841414 536 assert(in);
5a8bcb67 537
fe841414 538 existing = set_get(link->addresses, in);
3b6a3bde
YW
539 if (!existing)
540 return -ENOENT;
5a8bcb67 541
3b6a3bde
YW
542 if (ret)
543 *ret = existing;
544 return 0;
5a8bcb67
LP
545}
546
5d003031 547int link_get_address(Link *link, int family, const union in_addr_union *address, unsigned char prefixlen, Address **ret) {
390247d2 548 Address *a;
c5a0aeb3 549 int r;
5eec0a08 550
c5a0aeb3 551 assert(link);
5d003031 552 assert(IN_SET(family, AF_INET, AF_INET6));
c5a0aeb3 553 assert(address);
5eec0a08 554
5d003031
YW
555 /* This find an Address object on the link which matches the given address and prefix length
556 * and does not have peer address. When the prefixlen is zero, then an Address object with an
557 * arbitrary prefixlen will be returned. */
998545a7
YW
558
559 if (prefixlen != 0) {
390247d2 560 _cleanup_(address_freep) Address *tmp = NULL;
998545a7
YW
561
562 /* If prefixlen is set, then we can use address_get(). */
563
390247d2 564 r = address_new(&tmp);
998545a7
YW
565 if (r < 0)
566 return r;
567
390247d2
YW
568 tmp->family = family;
569 tmp->in_addr = *address;
570 tmp->prefixlen = prefixlen;
571 address_set_broadcast(tmp, link);
3b6a3bde 572
390247d2 573 if (address_get(link, tmp, &a) >= 0) {
3b6a3bde
YW
574 if (ret)
575 *ret = a;
576
577 return 0;
578 }
579
390247d2
YW
580 if (family == AF_INET6)
581 return -ENOENT;
582
583 /* IPv4 addresses may have label and/or non-default broadcast address.
584 * Hence, we need to always fallback below. */
585 }
586
587 SET_FOREACH(a, link->addresses) {
588 if (a->family != family)
589 continue;
590
591 if (!in_addr_equal(family, &a->in_addr, address))
592 continue;
593
594 if (in_addr_is_set(family, &a->in_addr_peer))
595 continue;
596
597 if (ret)
598 *ret = a;
599
600 return 0;
3b6a3bde 601 }
390247d2
YW
602
603 return -ENOENT;
998545a7
YW
604}
605
77e9be1e 606int manager_get_address(Manager *manager, int family, const union in_addr_union *address, unsigned char prefixlen, Address **ret) {
591bd5f3 607 Link *link;
591bd5f3
YW
608
609 assert(manager);
610 assert(IN_SET(family, AF_INET, AF_INET6));
611 assert(address);
612
77e9be1e
YW
613 HASHMAP_FOREACH(link, manager->links_by_index) {
614 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
615 continue;
616
617 if (link_get_address(link, family, address, prefixlen, ret) >= 0)
618 return 0;
619 }
620
621 return -ENOENT;
622}
623
624bool manager_has_address(Manager *manager, int family, const union in_addr_union *address, bool check_ready) {
625 Address *a;
626
627 assert(manager);
628 assert(IN_SET(family, AF_INET, AF_INET6));
629 assert(address);
630
631 if (manager_get_address(manager, family, address, 0, &a) < 0)
632 return false;
591bd5f3 633
77e9be1e 634 return check_ready ? address_is_ready(a) : address_exists(a);
591bd5f3
YW
635}
636
16bc8635 637const char* format_lifetime(char *buf, size_t l, usec_t lifetime_usec) {
0fd97a25 638 assert(buf);
a35f3128 639 assert(l > 4);
0fd97a25 640
16bc8635 641 if (lifetime_usec == USEC_INFINITY)
a35f3128 642 return "forever";
0fd97a25 643
a35f3128
ZJS
644 sprintf(buf, "for ");
645 /* format_timespan() never fails */
16bc8635 646 assert_se(format_timespan(buf + 4, l - 4, usec_sub_unsigned(lifetime_usec, now(clock_boottime_or_monotonic())), USEC_PER_SEC));
a35f3128 647 return buf;
0fd97a25
YW
648}
649
24f50382 650static void log_address_debug(const Address *address, const char *str, const Link *link) {
78ef25a5 651 _cleanup_free_ char *state = NULL, *addr = NULL, *peer = NULL, *flags_str = NULL, *scope_str = NULL;
7653a9dc 652
24f50382
YW
653 assert(address);
654 assert(str);
655 assert(link);
656
7653a9dc
YW
657 if (!DEBUG_LOGGING)
658 return;
659
3b6a3bde 660 (void) network_config_state_to_string_alloc(address->state, &state);
7653a9dc 661 (void) in_addr_to_string(address->family, &address->in_addr, &addr);
fa3f917a 662 if (in_addr_is_set(address->family, &address->in_addr_peer))
7653a9dc
YW
663 (void) in_addr_to_string(address->family, &address->in_addr_peer, &peer);
664
a2362484 665 (void) address_flags_to_string_alloc(address->flags, address->family, &flags_str);
78ef25a5 666 (void) route_scope_to_string_alloc(address->scope, &scope_str);
a2362484 667
78ef25a5 668 log_link_debug(link, "%s %s address (%s): %s%s%s/%u (valid %s, preferred %s), flags: %s, scope: %s",
3b6a3bde
YW
669 str, strna(network_config_source_to_string(address->source)), strna(state),
670 strnull(addr), peer ? " peer " : "", strempty(peer), address->prefixlen,
16bc8635
YW
671 FORMAT_LIFETIME(address->lifetime_valid_usec),
672 FORMAT_LIFETIME(address->lifetime_preferred_usec),
78ef25a5 673 strna(flags_str), strna(scope_str));
24f50382
YW
674}
675
a79a8d16 676static int address_set_netlink_message(const Address *address, sd_netlink_message *m, Link *link) {
d5e3b3eb 677 uint32_t flags;
a8481354
YW
678 int r;
679
680 assert(address);
a79a8d16 681 assert(m);
a8481354
YW
682 assert(link);
683
a79a8d16 684 r = sd_rtnl_message_addr_set_prefixlen(m, address->prefixlen);
a8481354 685 if (r < 0)
a79a8d16 686 return r;
a8481354
YW
687
688 /* On remove, only IFA_F_MANAGETEMPADDR flag for IPv6 addresses are used. But anyway, set all
d5e3b3eb
YW
689 * flags except tentative flag here unconditionally. Without setting the flag, the template
690 * addresses generated by kernel will not be removed automatically when the main address is
691 * removed. */
692 flags = address->flags & ~IFA_F_TENTATIVE;
a79a8d16 693 r = sd_rtnl_message_addr_set_flags(m, flags & 0xff);
a8481354 694 if (r < 0)
a79a8d16 695 return r;
a8481354 696
d5e3b3eb 697 if ((flags & ~0xff) != 0) {
a79a8d16 698 r = sd_netlink_message_append_u32(m, IFA_FLAGS, flags);
a8481354 699 if (r < 0)
a79a8d16 700 return r;
a8481354
YW
701 }
702
a79a8d16 703 r = netlink_message_append_in_addr_union(m, IFA_LOCAL, address->family, &address->in_addr);
a8481354 704 if (r < 0)
a79a8d16 705 return r;
a8481354
YW
706
707 return 0;
708}
709
f4cc1364 710static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
5a07fa9d
YW
711 int r;
712
5a07fa9d
YW
713 assert(m);
714 assert(link);
76c5a0f2 715
5a07fa9d
YW
716 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
717 return 0;
718
719 r = sd_netlink_message_get_errno(m);
720 if (r < 0 && r != -EADDRNOTAVAIL)
f4cc1364 721 log_link_message_warning_errno(link, m, r, "Could not drop address");
5a07fa9d
YW
722
723 return 1;
724}
725
3b6a3bde 726int address_remove(Address *address) {
a79a8d16 727 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
3b6a3bde 728 Link *link;
407fe036
TG
729 int r;
730
731 assert(address);
4c701096 732 assert(IN_SET(address->family, AF_INET, AF_INET6));
3b6a3bde
YW
733 assert(address->link);
734 assert(address->link->ifindex > 0);
735 assert(address->link->manager);
736 assert(address->link->manager->rtnl);
737
738 link = address->link;
407fe036 739
24f50382 740 log_address_debug(address, "Removing", link);
30226d27 741
a79a8d16 742 r = sd_rtnl_message_new_addr(link->manager->rtnl, &m, RTM_DELADDR,
151b9b96 743 link->ifindex, address->family);
eb56eb9b 744 if (r < 0)
a79a8d16 745 return log_link_warning_errno(link, r, "Could not allocate RTM_DELADDR message: %m");
407fe036 746
a79a8d16 747 r = address_set_netlink_message(address, m, link);
eb56eb9b 748 if (r < 0)
a79a8d16 749 return log_link_warning_errno(link, r, "Could not set netlink attributes: %m");
407fe036 750
a79a8d16 751 r = netlink_call_async(link->manager->rtnl, NULL, m,
f4cc1364 752 address_remove_handler,
302a796f 753 link_netlink_destroy_callback, link);
eb56eb9b 754 if (r < 0)
a79a8d16 755 return log_link_warning_errno(link, r, "Could not send rtnetlink message: %m");
407fe036 756
563c69c6
TG
757 link_ref(link);
758
3b6a3bde 759 address_enter_removing(address);
613d65d8
YW
760
761 /* The operational state is determined by address state and carrier state. Hence, if we remove
762 * an address, the operational state may be changed. */
763 link_update_operstate(link, true);
407fe036
TG
764 return 0;
765}
766
3def8850 767bool link_address_is_dynamic(const Link *link, const Address *address) {
f8f2f880
YW
768 Route *route;
769
770 assert(link);
771 assert(address);
772
16bc8635 773 if (address->lifetime_preferred_usec != USEC_INFINITY)
f8f2f880
YW
774 return true;
775
776 /* Even when the address is leased from a DHCP server, networkd assign the address
777 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
778 * corresponding routes with RTPROT_DHCP. */
3b6a3bde
YW
779 SET_FOREACH(route, link->routes) {
780 if (route->source != NETWORK_CONFIG_SOURCE_FOREIGN)
781 continue;
782
f974f17a
YW
783 /* The route is not assigned yet, or already removed. Ignoring. */
784 if (!route_exists(route))
785 continue;
786
f8f2f880
YW
787 if (route->protocol != RTPROT_DHCP)
788 continue;
789
790 if (address->family != route->family)
791 continue;
792
793 if (in_addr_equal(address->family, &address->in_addr, &route->prefsrc))
794 return true;
795 }
796
797 return false;
798}
799
b9bf3f22 800int link_drop_ipv6ll_addresses(Link *link) {
51f5dfd8 801 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
51f5dfd8
YW
802 int r;
803
804 assert(link);
805 assert(link->manager);
806 assert(link->manager->rtnl);
807
b9bf3f22
YW
808 /* IPv6LL address may be in the tentative state, and in that case networkd has not received it.
809 * So, we need to dump all IPv6 addresses. */
810
e096cab2 811 if (link_may_have_ipv6ll(link))
b9bf3f22
YW
812 return 0;
813
c31298be 814 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_GETADDR, link->ifindex, AF_INET6);
51f5dfd8
YW
815 if (r < 0)
816 return r;
817
f318f643
YW
818 r = sd_netlink_message_request_dump(req, true);
819 if (r < 0)
820 return r;
821
51f5dfd8
YW
822 r = sd_netlink_call(link->manager->rtnl, req, 0, &reply);
823 if (r < 0)
824 return r;
825
cda7fc8d 826 for (sd_netlink_message *addr = reply; addr; addr = sd_netlink_message_next(addr)) {
b9bf3f22
YW
827 _cleanup_(address_freep) Address *a = NULL;
828 unsigned char flags, prefixlen;
829 struct in6_addr address;
3b6a3bde 830 Address *existing;
51f5dfd8
YW
831 int ifindex;
832
c31298be
YW
833 /* NETLINK_GET_STRICT_CHK socket option is supported since kernel 4.20. To support
834 * older kernels, we need to check ifindex here. */
51f5dfd8
YW
835 r = sd_rtnl_message_addr_get_ifindex(addr, &ifindex);
836 if (r < 0) {
b9bf3f22 837 log_link_debug_errno(link, r, "rtnl: received address message without valid ifindex, ignoring: %m");
51f5dfd8
YW
838 continue;
839 } else if (link->ifindex != ifindex)
840 continue;
841
842 r = sd_rtnl_message_addr_get_flags(addr, &flags);
843 if (r < 0) {
b9bf3f22
YW
844 log_link_debug_errno(link, r, "rtnl: received address message without valid flags, ignoring: %m");
845 continue;
846 }
847
848 r = sd_rtnl_message_addr_get_prefixlen(addr, &prefixlen);
849 if (r < 0) {
850 log_link_debug_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m");
51f5dfd8 851 continue;
b9bf3f22
YW
852 }
853
854 if (sd_netlink_message_read_in6_addr(addr, IFA_LOCAL, NULL) >= 0)
855 /* address with peer, ignoring. */
51f5dfd8
YW
856 continue;
857
b9bf3f22
YW
858 r = sd_netlink_message_read_in6_addr(addr, IFA_ADDRESS, &address);
859 if (r < 0) {
860 log_link_debug_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
861 continue;
862 }
863
864 if (!in6_addr_is_link_local(&address))
865 continue;
866
867 r = address_new(&a);
868 if (r < 0)
869 return -ENOMEM;
870
871 a->family = AF_INET6;
872 a->in_addr.in6 = address;
873 a->prefixlen = prefixlen;
874 a->flags = flags;
875
3b6a3bde
YW
876 if (address_get(link, a, &existing) < 0) {
877 r = address_add(link, a);
878 if (r < 0)
879 return r;
880
881 existing = TAKE_PTR(a);
882 }
883
884 r = address_remove(existing);
b9bf3f22
YW
885 if (r < 0)
886 return r;
51f5dfd8
YW
887 }
888
889 return 0;
890}
891
f8f2f880
YW
892int link_drop_foreign_addresses(Link *link) {
893 Address *address;
894 int k, r = 0;
895
896 assert(link);
3b6a3bde 897 assert(link->network);
f8f2f880 898
3b6a3bde
YW
899 /* First, mark all addresses. */
900 SET_FOREACH(address, link->addresses) {
b9bf3f22 901 /* We consider IPv6LL addresses to be managed by the kernel, or dropped in link_drop_ipv6ll_addresses() */
ecedc48b 902 if (address->family == AF_INET6 && in6_addr_is_link_local(&address->in_addr.in6))
f8f2f880
YW
903 continue;
904
b69bfa43
YW
905 /* Do not remove localhost address (127.0.0.1 and ::1) */
906 if (link->flags & IFF_LOOPBACK && in_addr_is_localhost_one(address->family, &address->in_addr) > 0)
907 continue;
908
3b6a3bde
YW
909 /* Ignore addresses we configured. */
910 if (address->source != NETWORK_CONFIG_SOURCE_FOREIGN)
911 continue;
912
913 /* Ignore addresses not assigned yet or already removing. */
914 if (!address_exists(address))
915 continue;
916
a71c5c44
YW
917 /* link_address_is_dynamic() is slightly heavy. Let's call the function only when KeepConfiguration= is set. */
918 if (IN_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP, KEEP_CONFIGURATION_STATIC) &&
919 link_address_is_dynamic(link, address) == (link->network->keep_configuration == KEEP_CONFIGURATION_DHCP))
f8f2f880
YW
920 continue;
921
3b6a3bde
YW
922 address_mark(address);
923 }
924
925 /* Then, unmark requested addresses. */
926 ORDERED_HASHMAP_FOREACH(address, link->network->addresses_by_section) {
927 Address *existing;
928
929 if (address_get(link, address, &existing) >= 0)
930 address_unmark(existing);
931 }
932
933 /* Finally, remove all marked addresses. */
934 SET_FOREACH(address, link->addresses) {
935 if (!address_is_marked(address))
936 continue;
937
938 k = address_remove(address);
939 if (k < 0 && r >= 0)
940 r = k;
f8f2f880
YW
941 }
942
943 return r;
944}
945
a0e99a37 946int link_drop_managed_addresses(Link *link) {
3b6a3bde 947 Address *address;
d7fbb9f5
YW
948 int k, r = 0;
949
950 assert(link);
951
952 SET_FOREACH(address, link->addresses) {
a0e99a37
YW
953 /* Do not touch addresses managed by kernel or other tools. */
954 if (address->source == NETWORK_CONFIG_SOURCE_FOREIGN)
3b6a3bde
YW
955 continue;
956
a0e99a37
YW
957 /* Ignore addresses not assigned yet or already removing. */
958 if (!address_exists(address))
d7fbb9f5
YW
959 continue;
960
3b6a3bde 961 k = address_remove(address);
d7fbb9f5
YW
962 if (k < 0 && r >= 0) {
963 r = k;
964 continue;
965 }
d7fbb9f5
YW
966 }
967
968 return r;
969}
970
b4564f4e
YW
971void link_foreignize_addresses(Link *link) {
972 Address *address;
973
974 assert(link);
975
976 SET_FOREACH(address, link->addresses)
977 address->source = NETWORK_CONFIG_SOURCE_FOREIGN;
978}
979
693ec5ca 980static int address_acquire(Link *link, const Address *original, Address **ret) {
8e766630 981 _cleanup_(address_freep) Address *na = NULL;
3b6a3bde 982 union in_addr_union in_addr;
11bf3cce
LP
983 int r;
984
985 assert(link);
986 assert(original);
987 assert(ret);
988
989 /* Something useful was configured? just use it */
94876904 990 if (in_addr_is_set(original->family, &original->in_addr)) {
aa651e88
YW
991 *ret = NULL;
992 return 0;
993 }
11bf3cce
LP
994
995 /* The address is configured to be 0.0.0.0 or [::] by the user?
996 * Then let's acquire something more useful from the pool. */
ed76f585 997 r = address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
6a7a4e4d 998 if (r < 0)
7750b796
YW
999 return r;
1000 if (r == 0)
11bf3cce 1001 return -EBUSY;
11bf3cce 1002
473680be
YW
1003 /* Pick first address in range for ourselves. */
1004 if (original->family == AF_INET)
11bf3cce 1005 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
473680be 1006 else if (original->family == AF_INET6)
11bf3cce
LP
1007 in_addr.in6.s6_addr[15] |= 1;
1008
99b46669 1009 r = address_dup(original, &na);
cde1f0e8
YW
1010 if (r < 0)
1011 return r;
11bf3cce 1012
11bf3cce
LP
1013 na->in_addr = in_addr;
1014
1cc6c93a 1015 *ret = TAKE_PTR(na);
aa651e88 1016 return 1;
11bf3cce
LP
1017}
1018
5a07fa9d
YW
1019int address_configure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
1020 int r;
1021
1022 assert(rtnl);
1023 assert(m);
1024 assert(link);
1025 assert(error_msg);
1026
5a07fa9d
YW
1027 r = sd_netlink_message_get_errno(m);
1028 if (r < 0 && r != -EEXIST) {
1029 log_link_message_warning_errno(link, m, r, error_msg);
1030 link_enter_failed(link);
1031 return 0;
3a1dfdb4 1032 }
5a07fa9d
YW
1033
1034 return 1;
1035}
1036
54ff39f7 1037static int address_configure(const Address *address, Link *link, Request *req) {
a79a8d16 1038 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
7b3a7581 1039 int r;
f579559b 1040
c166a070 1041 assert(address);
4c701096 1042 assert(IN_SET(address->family, AF_INET, AF_INET6));
c166a070
TG
1043 assert(link);
1044 assert(link->ifindex > 0);
f882c247 1045 assert(link->manager);
c166a070 1046 assert(link->manager->rtnl);
54ff39f7 1047 assert(req);
f882c247 1048
c0c04a4b 1049 log_address_debug(address, "Configuring", link);
1b566071 1050
a79a8d16 1051 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &m, link->ifindex, address->family);
eb56eb9b 1052 if (r < 0)
a79a8d16 1053 return r;
f579559b 1054
a79a8d16 1055 r = address_set_netlink_message(address, m, link);
eb56eb9b 1056 if (r < 0)
a8481354 1057 return r;
851c9f82 1058
a79a8d16 1059 r = sd_rtnl_message_addr_set_scope(m, address->scope);
eb56eb9b 1060 if (r < 0)
a79a8d16 1061 return r;
5a723174 1062
94876904 1063 if (in_addr_is_set(address->family, &address->in_addr_peer)) {
a79a8d16 1064 r = netlink_message_append_in_addr_union(m, IFA_ADDRESS, address->family, &address->in_addr_peer);
eb56eb9b 1065 if (r < 0)
a79a8d16 1066 return r;
e680486d 1067 } else if (in4_addr_is_set(&address->broadcast)) {
a79a8d16 1068 r = sd_netlink_message_append_in_addr(m, IFA_BROADCAST, &address->broadcast);
66e0bb33 1069 if (r < 0)
a79a8d16 1070 return r;
f579559b
TG
1071 }
1072
2a236f9f 1073 if (address->family == AF_INET && address->label) {
a79a8d16 1074 r = sd_netlink_message_append_string(m, IFA_LABEL, address->label);
eb56eb9b 1075 if (r < 0)
a79a8d16 1076 return r;
f579559b
TG
1077 }
1078
a79a8d16 1079 r = sd_netlink_message_append_cache_info(m, IFA_CACHEINFO,
16bc8635 1080 address_set_cinfo(address, &(struct ifa_cacheinfo) {}));
eb56eb9b 1081 if (r < 0)
a79a8d16 1082 return r;
68ceb9df 1083
a79a8d16 1084 r = sd_netlink_message_append_u32(m, IFA_RT_PRIORITY, address->route_metric);
c4ff0629 1085 if (r < 0)
a79a8d16 1086 return r;
c4ff0629 1087
80d62d4f 1088 return request_call_netlink_async(link->manager->rtnl, m, req);
f579559b
TG
1089}
1090
8bed7c55
YW
1091static bool address_is_ready_to_configure(Link *link, const Address *address) {
1092 assert(link);
1093 assert(address);
1094
1095 if (!link_is_ready_to_configure(link, false))
1096 return false;
1097
1098 if (FLAGS_SET(address->state, NETWORK_CONFIG_STATE_PROBING))
1099 return false;
1100
1101 /* Refuse adding more than the limit */
1102 if (set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX)
1103 return false;
1104
1105 return true;
1106}
1107
09d09207 1108static int address_process_request(Request *req, Link *link, Address *address) {
8bed7c55
YW
1109 int r;
1110
1111 assert(req);
ff51134c
YW
1112 assert(link);
1113 assert(address);
8bed7c55
YW
1114
1115 if (!address_is_ready_to_configure(link, address))
1116 return 0;
1117
54ff39f7 1118 r = address_configure(address, link, req);
8bed7c55
YW
1119 if (r < 0)
1120 return log_link_warning_errno(link, r, "Failed to configure address: %m");
1121
1122 address_enter_configuring(address);
1123 return 1;
1124}
1125
76c5a0f2
YW
1126int link_request_address(
1127 Link *link,
1128 Address *address,
1129 bool consume_object,
1130 unsigned *message_counter,
80d62d4f 1131 address_netlink_handler_t netlink_handler,
76c5a0f2
YW
1132 Request **ret) {
1133
3b6a3bde 1134 Address *acquired, *existing;
c0c04a4b
YW
1135 int r;
1136
76c5a0f2
YW
1137 assert(link);
1138 assert(address);
3b6a3bde 1139 assert(address->source != NETWORK_CONFIG_SOURCE_FOREIGN);
76c5a0f2 1140
c0c04a4b
YW
1141 r = address_acquire(link, address, &acquired);
1142 if (r < 0)
1143 return log_link_warning_errno(link, r, "Failed to acquire an address from pool: %m");
1144 if (r > 0) {
3b6a3bde 1145 if (consume_object)
c0c04a4b 1146 address_free(address);
3b6a3bde 1147
c0c04a4b 1148 address = acquired;
3b6a3bde
YW
1149 consume_object = true;
1150 }
1151
e680486d
YW
1152 if (address_needs_to_set_broadcast(address, link)) {
1153 if (!consume_object) {
1154 Address *a;
1155
1156 r = address_dup(address, &a);
1157 if (r < 0)
1158 return r;
1159
1160 address = a;
1161 consume_object = true;
1162 }
1163
1164 address_set_broadcast(address, link);
1165 }
1166
3b6a3bde
YW
1167 if (address_get(link, address, &existing) < 0) {
1168 _cleanup_(address_freep) Address *tmp = NULL;
1169
435910b3
YW
1170 if (consume_object)
1171 tmp = address;
1172 else {
1173 r = address_dup(address, &tmp);
1174 if (r < 0)
1175 return r;
1176 }
3b6a3bde
YW
1177
1178 /* Consider address tentative until we get the real flags from the kernel */
1179 tmp->flags |= IFA_F_TENTATIVE;
1180
1181 r = address_add(link, tmp);
1182 if (r < 0)
1183 return r;
1184
1185 existing = TAKE_PTR(tmp);
1186 } else {
1187 existing->source = address->source;
1188 existing->provider = address->provider;
435910b3
YW
1189 existing->lifetime_valid_usec = address->lifetime_valid_usec;
1190 existing->lifetime_preferred_usec = address->lifetime_preferred_usec;
1191 if (consume_object)
1192 address_free(address);
c0c04a4b
YW
1193 }
1194
3b6a3bde
YW
1195 r = ipv4acd_configure(existing);
1196 if (r < 0)
1197 return r;
1198
435910b3 1199 log_address_debug(existing, "Requesting", link);
09d09207
YW
1200 r = link_queue_request_safe(link, REQUEST_TYPE_ADDRESS,
1201 existing, NULL,
1202 address_hash_func,
1203 address_compare_func,
1204 address_process_request,
1205 message_counter, netlink_handler, ret);
c0c04a4b
YW
1206 if (r < 0)
1207 return log_link_warning_errno(link, r, "Failed to request address: %m");
3b6a3bde
YW
1208 if (r == 0)
1209 return 0;
1210
1211 address_enter_requesting(existing);
1212
1213 return 1;
76c5a0f2
YW
1214}
1215
80d62d4f 1216static int static_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) {
e1e4cd1e
YW
1217 int r;
1218
1219 assert(link);
e1e4cd1e
YW
1220
1221 r = address_configure_handler_internal(rtnl, m, link, "Failed to set static address");
1222 if (r <= 0)
1223 return r;
1224
1225 if (link->static_address_messages == 0) {
1226 log_link_debug(link, "Addresses set");
1227 link->static_addresses_configured = true;
1228 link_check_ready(link);
1229 }
1230
1231 return 1;
1232}
1233
8c29f484 1234int link_request_static_address(Link *link, Address *address, bool consume) {
8c29f484
YW
1235 assert(link);
1236 assert(address);
3b6a3bde 1237 assert(address->source == NETWORK_CONFIG_SOURCE_STATIC);
8c29f484 1238
3b6a3bde
YW
1239 return link_request_address(link, address, consume, &link->static_address_messages,
1240 static_address_handler, NULL);
8c29f484
YW
1241}
1242
76c5a0f2
YW
1243int link_request_static_addresses(Link *link) {
1244 Address *a;
682c65b0
YW
1245 int r;
1246
1247 assert(link);
1248 assert(link->network);
1249
76c5a0f2 1250 link->static_addresses_configured = false;
682c65b0 1251
76c5a0f2 1252 ORDERED_HASHMAP_FOREACH(a, link->network->addresses_by_section) {
8c29f484 1253 r = link_request_static_address(link, a, false);
682c65b0
YW
1254 if (r < 0)
1255 return r;
1256 }
1257
3b6a3bde
YW
1258 r = link_request_radv_addresses(link);
1259 if (r < 0)
1260 return r;
0017ba31 1261
3b6a3bde
YW
1262 r = link_request_dhcp_server_address(link);
1263 if (r < 0)
1264 return r;
0017ba31 1265
76c5a0f2
YW
1266 if (link->static_address_messages == 0) {
1267 link->static_addresses_configured = true;
1268 link_check_ready(link);
682c65b0
YW
1269 } else {
1270 log_link_debug(link, "Setting addresses");
1271 link_set_state(link, LINK_STATE_CONFIGURING);
1272 }
1273
1274 return 0;
1275}
1276
e1e4cd1e
YW
1277void address_cancel_request(Address *address) {
1278 Request req;
1279
1280 assert(address);
1281 assert(address->link);
1282
1283 if (!address_is_requesting(address))
1284 return;
1285
1286 req = (Request) {
1287 .link = address->link,
1288 .type = REQUEST_TYPE_ADDRESS,
09d09207
YW
1289 .userdata = address,
1290 .hash_func = (hash_func_t) address_hash_func,
1291 .compare_func = (compare_func_t) address_compare_func,
e1e4cd1e
YW
1292 };
1293
e26d3d40 1294 request_detach(address->link->manager, &req);
e1e4cd1e
YW
1295 address_cancel_requesting(address);
1296}
1297
e1fc2c43 1298int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
fe841414 1299 _cleanup_(address_freep) Address *tmp = NULL;
16bc8635 1300 struct ifa_cacheinfo cinfo;
e1fc2c43
YW
1301 Link *link = NULL;
1302 uint16_t type;
e1fc2c43 1303 Address *address = NULL;
fe841414 1304 int ifindex, r;
e1fc2c43
YW
1305
1306 assert(rtnl);
1307 assert(message);
1308 assert(m);
1309
1310 if (sd_netlink_message_is_error(message)) {
1311 r = sd_netlink_message_get_errno(message);
1312 if (r < 0)
1313 log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring");
1314
1315 return 0;
1316 }
1317
1318 r = sd_netlink_message_get_type(message, &type);
1319 if (r < 0) {
1320 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1321 return 0;
1322 } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) {
1323 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type);
1324 return 0;
1325 }
1326
1327 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
1328 if (r < 0) {
1329 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
1330 return 0;
1331 } else if (ifindex <= 0) {
1332 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex);
1333 return 0;
1334 }
1335
6eab614d 1336 r = link_get_by_index(m, ifindex, &link);
e1fc2c43
YW
1337 if (r < 0 || !link) {
1338 /* when enumerating we might be out of sync, but we will get the address again, so just
1339 * ignore it */
1340 if (!m->enumerating)
1341 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex);
1342 return 0;
1343 }
1344
fe841414
YW
1345 r = address_new(&tmp);
1346 if (r < 0)
1347 return log_oom();
1348
1349 r = sd_rtnl_message_addr_get_family(message, &tmp->family);
e1fc2c43
YW
1350 if (r < 0) {
1351 log_link_warning(link, "rtnl: received address message without family, ignoring.");
1352 return 0;
fe841414
YW
1353 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1354 log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", tmp->family);
e1fc2c43
YW
1355 return 0;
1356 }
1357
fe841414 1358 r = sd_rtnl_message_addr_get_prefixlen(message, &tmp->prefixlen);
e1fc2c43 1359 if (r < 0) {
9a0ad16b 1360 log_link_warning_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m");
e1fc2c43
YW
1361 return 0;
1362 }
1363
fe841414 1364 r = sd_rtnl_message_addr_get_scope(message, &tmp->scope);
e1fc2c43 1365 if (r < 0) {
9a0ad16b 1366 log_link_warning_errno(link, r, "rtnl: received address message without scope, ignoring: %m");
e1fc2c43
YW
1367 return 0;
1368 }
1369
0828a386 1370 r = sd_netlink_message_read_u32(message, IFA_FLAGS, &tmp->flags);
8ed68422
YW
1371 if (r == -ENODATA) {
1372 unsigned char flags;
1373
1374 /* For old kernels. */
1375 r = sd_rtnl_message_addr_get_flags(message, &flags);
1376 if (r >= 0)
1377 tmp->flags = flags;
1378 }
e1fc2c43 1379 if (r < 0) {
9a0ad16b 1380 log_link_warning_errno(link, r, "rtnl: received address message without flags, ignoring: %m");
e1fc2c43
YW
1381 return 0;
1382 }
1383
fe841414 1384 switch (tmp->family) {
e1fc2c43 1385 case AF_INET:
fe841414 1386 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &tmp->in_addr.in);
e1fc2c43
YW
1387 if (r < 0) {
1388 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1389 return 0;
1390 }
1391
fe841414
YW
1392 r = sd_netlink_message_read_in_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in);
1393 if (r < 0 && r != -ENODATA) {
1394 log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m");
1395 return 0;
1396 } else if (r >= 0) {
1397 if (in4_addr_equal(&tmp->in_addr.in, &tmp->in_addr_peer.in))
1398 tmp->in_addr_peer = IN_ADDR_NULL;
fe841414
YW
1399 }
1400
1401 r = sd_netlink_message_read_in_addr(message, IFA_BROADCAST, &tmp->broadcast);
1402 if (r < 0 && r != -ENODATA) {
1403 log_link_warning_errno(link, r, "rtnl: could not get broadcast from address message, ignoring: %m");
1404 return 0;
1405 }
1406
1407 r = sd_netlink_message_read_string_strdup(message, IFA_LABEL, &tmp->label);
1408 if (r < 0 && r != -ENODATA) {
1409 log_link_warning_errno(link, r, "rtnl: could not get label from address message, ignoring: %m");
1410 return 0;
1411 } else if (r >= 0 && streq_ptr(tmp->label, link->ifname))
1412 tmp->label = mfree(tmp->label);
1413
e1fc2c43
YW
1414 break;
1415
1416 case AF_INET6:
fe841414
YW
1417 r = sd_netlink_message_read_in6_addr(message, IFA_LOCAL, &tmp->in_addr.in6);
1418 if (r >= 0) {
1419 /* Have peer address. */
1420 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in6);
1421 if (r < 0) {
1422 log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m");
1423 return 0;
1424 }
fe841414
YW
1425 } else if (r == -ENODATA) {
1426 /* Does not have peer address. */
1427 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr.in6);
1428 if (r < 0) {
1429 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1430 return 0;
1431 }
1432 } else {
1433 log_link_warning_errno(link, r, "rtnl: could not get local address from address message, ignoring: %m");
e1fc2c43
YW
1434 return 0;
1435 }
1436
1437 break;
1438
1439 default:
04499a70 1440 assert_not_reached();
e1fc2c43
YW
1441 }
1442
16bc8635 1443 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
e1fc2c43
YW
1444 if (r < 0 && r != -ENODATA) {
1445 log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
1446 return 0;
24f50382 1447 }
e1fc2c43 1448
fe841414 1449 (void) address_get(link, tmp, &address);
e1fc2c43
YW
1450
1451 switch (type) {
1452 case RTM_NEWADDR:
3b6a3bde 1453 if (address) {
f9831557
YW
1454 /* update flags and etc. */
1455 address->flags = tmp->flags;
1456 address->scope = tmp->scope;
16bc8635 1457 address_set_lifetime(address, &cinfo);
3b6a3bde 1458 address_enter_configured(address);
f9831557 1459 log_address_debug(address, "Received updated", link);
3b6a3bde 1460 } else {
16bc8635 1461 address_set_lifetime(tmp, &cinfo);
3b6a3bde
YW
1462 address_enter_configured(tmp);
1463 log_address_debug(tmp, "Received new", link);
1464
1465 r = address_add(link, tmp);
e1fc2c43 1466 if (r < 0) {
24f50382
YW
1467 _cleanup_free_ char *buf = NULL;
1468
5380707a
YW
1469 (void) in_addr_prefix_to_string(tmp->family, &tmp->in_addr, tmp->prefixlen, &buf);
1470 log_link_warning_errno(link, r, "Failed to remember foreign address %s, ignoring: %m",
1471 strnull(buf));
e1fc2c43 1472 return 0;
24f50382 1473 }
3b6a3bde
YW
1474
1475 address = TAKE_PTR(tmp);
e1fc2c43
YW
1476 }
1477
1478 /* address_update() logs internally, so we don't need to here. */
f9831557 1479 r = address_update(address);
e1fc2c43
YW
1480 if (r < 0)
1481 link_enter_failed(link);
1482
1483 break;
1484
1485 case RTM_DELADDR:
3b6a3bde
YW
1486 if (address) {
1487 address_enter_removed(address);
1488 log_address_debug(address, address->state == 0 ? "Forgetting" : "Removed", link);
1489 (void) address_drop(address);
1490 } else
1491 log_address_debug(tmp, "Kernel removed unknown", link);
e1fc2c43
YW
1492
1493 break;
1494
1495 default:
04499a70 1496 assert_not_reached();
e1fc2c43
YW
1497 }
1498
1499 return 1;
1500}
1501
44e7b949
LP
1502int config_parse_broadcast(
1503 const char *unit,
eb0ea358
TG
1504 const char *filename,
1505 unsigned line,
1506 const char *section,
1507 unsigned section_line,
1508 const char *lvalue,
1509 int ltype,
1510 const char *rvalue,
1511 void *data,
1512 void *userdata) {
44e7b949 1513
eb0ea358 1514 Network *network = userdata;
fcbf4cb7 1515 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
94af46fc 1516 union in_addr_union u;
eb0ea358
TG
1517 int r;
1518
1519 assert(filename);
1520 assert(section);
1521 assert(lvalue);
1522 assert(rvalue);
1523 assert(data);
1524
f4859fc7 1525 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1526 if (r == -ENOMEM)
1527 return log_oom();
1528 if (r < 0) {
1529 log_syntax(unit, LOG_WARNING, filename, line, r,
1530 "Failed to allocate new address, ignoring assignment: %m");
1531 return 0;
1532 }
eb0ea358 1533
832583ad
YW
1534 if (isempty(rvalue)) {
1535 /* The broadcast address will be calculated based on Address=, and set if the link is
1536 * not a wireguard interface. Here, we do not check or set n->family. */
1537 n->broadcast = (struct in_addr) {};
1538 n->set_broadcast = -1;
1539 TAKE_PTR(n);
1540 return 0;
1541 }
1542
1543 r = parse_boolean(rvalue);
1544 if (r >= 0) {
1545 /* The broadcast address will be calculated based on Address=. Here, we do not check or
1546 * set n->family. */
1547 n->broadcast = (struct in_addr) {};
1548 n->set_broadcast = r;
1549 TAKE_PTR(n);
1550 return 0;
1551 }
1552
482e2ac1 1553 if (n->family == AF_INET6) {
d96edb2c 1554 log_syntax(unit, LOG_WARNING, filename, line, 0,
2850cd40 1555 "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
482e2ac1
TG
1556 return 0;
1557 }
1558
94af46fc 1559 r = in_addr_from_string(AF_INET, rvalue, &u);
eb0ea358 1560 if (r < 0) {
d96edb2c 1561 log_syntax(unit, LOG_WARNING, filename, line, r,
2850cd40 1562 "Broadcast is invalid, ignoring assignment: %s", rvalue);
eb0ea358
TG
1563 return 0;
1564 }
832583ad
YW
1565 if (in4_addr_is_null(&u.in)) {
1566 log_syntax(unit, LOG_WARNING, filename, line, 0,
1567 "Broadcast cannot be ANY address, ignoring assignment: %s", rvalue);
1568 return 0;
1569 }
eb0ea358 1570
94af46fc 1571 n->broadcast = u.in;
832583ad 1572 n->set_broadcast = true;
44e7b949 1573 n->family = AF_INET;
dea161d9 1574 TAKE_PTR(n);
eb0ea358
TG
1575
1576 return 0;
1577}
1578
f5ee7d74
YW
1579int config_parse_address(
1580 const char *unit,
f579559b
TG
1581 const char *filename,
1582 unsigned line,
1583 const char *section,
71a61510 1584 unsigned section_line,
f579559b
TG
1585 const char *lvalue,
1586 int ltype,
1587 const char *rvalue,
1588 void *data,
1589 void *userdata) {
44e7b949 1590
6ae115c1 1591 Network *network = userdata;
fcbf4cb7 1592 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
44e7b949 1593 union in_addr_union buffer;
b7cb4452 1594 unsigned char prefixlen;
44e7b949 1595 int r, f;
f579559b
TG
1596
1597 assert(filename);
6ae115c1 1598 assert(section);
f579559b
TG
1599 assert(lvalue);
1600 assert(rvalue);
1601 assert(data);
1602
9cd9fc8f
YW
1603 if (streq(section, "Network"))
1604 /* we are not in an Address section, so use line number instead. */
1605 r = address_new_static(network, filename, line, &n);
1606 else
f4859fc7 1607 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1608 if (r == -ENOMEM)
1609 return log_oom();
1610 if (r < 0) {
1611 log_syntax(unit, LOG_WARNING, filename, line, r,
1612 "Failed to allocate new address, ignoring assignment: %m");
1613 return 0;
1614 }
f579559b
TG
1615
1616 /* Address=address/prefixlen */
0f707207
YW
1617 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_REFUSE, &f, &buffer, &prefixlen);
1618 if (r == -ENOANO) {
d96edb2c 1619 log_syntax(unit, LOG_WARNING, filename, line, r,
0f707207
YW
1620 "An address '%s' is specified without prefix length. "
1621 "The behavior of parsing addresses without prefix length will be changed in the future release. "
1622 "Please specify prefix length explicitly.", rvalue);
1623
1624 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_LEGACY, &f, &buffer, &prefixlen);
1625 }
f579559b 1626 if (r < 0) {
d96edb2c 1627 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue);
f579559b
TG
1628 return 0;
1629 }
1630
44e7b949 1631 if (n->family != AF_UNSPEC && f != n->family) {
d96edb2c 1632 log_syntax(unit, LOG_WARNING, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue);
44e7b949
LP
1633 return 0;
1634 }
1635
c9207ff3
YW
1636 if (in_addr_is_null(f, &buffer)) {
1637 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
1638 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
1639 * let's limit the prefix length to 64 or larger. See RFC4193. */
1640 if ((f == AF_INET && prefixlen < 8) ||
1641 (f == AF_INET6 && prefixlen < 64)) {
d96edb2c 1642 log_syntax(unit, LOG_WARNING, filename, line, 0,
c9207ff3
YW
1643 "Null address with invalid prefixlen='%u', ignoring assignment: %s",
1644 prefixlen, rvalue);
1645 return 0;
1646 }
1647 }
1648
44e7b949 1649 n->family = f;
b7cb4452 1650 n->prefixlen = prefixlen;
44e7b949
LP
1651
1652 if (streq(lvalue, "Address"))
1653 n->in_addr = buffer;
1654 else
1655 n->in_addr_peer = buffer;
1656
dea161d9 1657 TAKE_PTR(n);
f579559b
TG
1658 return 0;
1659}
6ae115c1 1660
d31645ad
LP
1661int config_parse_label(
1662 const char *unit,
6ae115c1
TG
1663 const char *filename,
1664 unsigned line,
1665 const char *section,
1666 unsigned section_line,
1667 const char *lvalue,
1668 int ltype,
1669 const char *rvalue,
1670 void *data,
1671 void *userdata) {
d31645ad 1672
fcbf4cb7 1673 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
d31645ad 1674 Network *network = userdata;
6ae115c1
TG
1675 int r;
1676
1677 assert(filename);
1678 assert(section);
1679 assert(lvalue);
1680 assert(rvalue);
1681 assert(data);
1682
f4859fc7 1683 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1684 if (r == -ENOMEM)
1685 return log_oom();
1686 if (r < 0) {
1687 log_syntax(unit, LOG_WARNING, filename, line, r,
1688 "Failed to allocate new address, ignoring assignment: %m");
1689 return 0;
1690 }
6ae115c1 1691
a87d19fe 1692 if (!address_label_valid(rvalue)) {
d96edb2c 1693 log_syntax(unit, LOG_WARNING, filename, line, 0,
2850cd40 1694 "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
6ae115c1
TG
1695 return 0;
1696 }
1697
d31645ad
LP
1698 r = free_and_strdup(&n->label, rvalue);
1699 if (r < 0)
1700 return log_oom();
6ae115c1 1701
dea161d9 1702 TAKE_PTR(n);
6ae115c1
TG
1703 return 0;
1704}
ce6c77eb 1705
f5ee7d74
YW
1706int config_parse_lifetime(
1707 const char *unit,
1708 const char *filename,
1709 unsigned line,
1710 const char *section,
1711 unsigned section_line,
1712 const char *lvalue,
1713 int ltype,
1714 const char *rvalue,
1715 void *data,
1716 void *userdata) {
1717
b5834a0b 1718 Network *network = userdata;
fcbf4cb7 1719 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
16bc8635 1720 usec_t k;
b5834a0b
SS
1721 int r;
1722
1723 assert(filename);
1724 assert(section);
1725 assert(lvalue);
1726 assert(rvalue);
1727 assert(data);
1728
f4859fc7 1729 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1730 if (r == -ENOMEM)
1731 return log_oom();
1732 if (r < 0) {
1733 log_syntax(unit, LOG_WARNING, filename, line, r,
1734 "Failed to allocate new address, ignoring assignment: %m");
1735 return 0;
1736 }
b5834a0b 1737
10b20e5a
ZJS
1738 /* We accept only "forever", "infinity", empty, or "0". */
1739 if (STR_IN_SET(rvalue, "forever", "infinity", ""))
16bc8635 1740 k = USEC_INFINITY;
33680b0a
YW
1741 else if (streq(rvalue, "0"))
1742 k = 0;
1743 else {
d96edb2c 1744 log_syntax(unit, LOG_WARNING, filename, line, 0,
33680b0a 1745 "Invalid PreferredLifetime= value, ignoring: %s", rvalue);
b5834a0b
SS
1746 return 0;
1747 }
1748
16bc8635 1749 n->lifetime_preferred_usec = k;
d2735796 1750 TAKE_PTR(n);
b5834a0b
SS
1751
1752 return 0;
1753}
1754
f5ee7d74
YW
1755int config_parse_address_flags(
1756 const char *unit,
1757 const char *filename,
1758 unsigned line,
1759 const char *section,
1760 unsigned section_line,
1761 const char *lvalue,
1762 int ltype,
1763 const char *rvalue,
1764 void *data,
1765 void *userdata) {
1766
e63be084 1767 Network *network = userdata;
fcbf4cb7 1768 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
e63be084
SS
1769 int r;
1770
1771 assert(filename);
1772 assert(section);
1773 assert(lvalue);
1774 assert(rvalue);
1775 assert(data);
1776
f4859fc7 1777 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1778 if (r == -ENOMEM)
1779 return log_oom();
1780 if (r < 0) {
1781 log_syntax(unit, LOG_WARNING, filename, line, r,
1782 "Failed to allocate new address, ignoring assignment: %m");
1783 return 0;
1784 }
e63be084
SS
1785
1786 r = parse_boolean(rvalue);
1787 if (r < 0) {
d96edb2c 1788 log_syntax(unit, LOG_WARNING, filename, line, r,
051e77ca 1789 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
e63be084
SS
1790 return 0;
1791 }
1792
eaff204f
YW
1793 if (streq(lvalue, "AddPrefixRoute"))
1794 r = !r;
1795
1796 SET_FLAG(n->flags, ltype, r);
e63be084 1797
dea161d9 1798 TAKE_PTR(n);
e63be084
SS
1799 return 0;
1800}
1801
f5ee7d74
YW
1802int config_parse_address_scope(
1803 const char *unit,
1804 const char *filename,
1805 unsigned line,
1806 const char *section,
1807 unsigned section_line,
1808 const char *lvalue,
1809 int ltype,
1810 const char *rvalue,
1811 void *data,
1812 void *userdata) {
1813
2959fb07 1814 Network *network = userdata;
fcbf4cb7 1815 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
2959fb07
SS
1816 int r;
1817
1818 assert(filename);
1819 assert(section);
1820 assert(lvalue);
1821 assert(rvalue);
1822 assert(data);
1823
1824 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1825 if (r == -ENOMEM)
1826 return log_oom();
1827 if (r < 0) {
1828 log_syntax(unit, LOG_WARNING, filename, line, r,
1829 "Failed to allocate new address, ignoring assignment: %m");
1830 return 0;
1831 }
2959fb07 1832
d8c472f2
YW
1833 r = route_scope_from_string(rvalue);
1834 if (r < 0) {
1835 log_syntax(unit, LOG_WARNING, filename, line, r,
1836 "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue);
1837 return 0;
2959fb07
SS
1838 }
1839
d8c472f2 1840 n->scope = r;
07336a06 1841 n->scope_set = true;
dea161d9 1842 TAKE_PTR(n);
2959fb07
SS
1843 return 0;
1844}
1845
c4ff0629
YW
1846int config_parse_address_route_metric(
1847 const char *unit,
1848 const char *filename,
1849 unsigned line,
1850 const char *section,
1851 unsigned section_line,
1852 const char *lvalue,
1853 int ltype,
1854 const char *rvalue,
1855 void *data,
1856 void *userdata) {
1857
1858 Network *network = userdata;
1859 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1860 int r;
1861
1862 assert(filename);
1863 assert(section);
1864 assert(lvalue);
1865 assert(rvalue);
1866 assert(data);
1867
1868 r = address_new_static(network, filename, section_line, &n);
1869 if (r == -ENOMEM)
1870 return log_oom();
1871 if (r < 0) {
1872 log_syntax(unit, LOG_WARNING, filename, line, r,
1873 "Failed to allocate new address, ignoring assignment: %m");
1874 return 0;
1875 }
1876
1877 r = safe_atou32(rvalue, &n->route_metric);
1878 if (r < 0) {
1879 log_syntax(unit, LOG_WARNING, filename, line, r,
1880 "Could not parse %s=, ignoring assignment: %s", lvalue, rvalue);
1881 return 0;
1882 }
1883
1884 TAKE_PTR(n);
1885 return 0;
1886}
1887
051e77ca
SS
1888int config_parse_duplicate_address_detection(
1889 const char *unit,
1890 const char *filename,
1891 unsigned line,
1892 const char *section,
1893 unsigned section_line,
1894 const char *lvalue,
1895 int ltype,
1896 const char *rvalue,
1897 void *data,
1898 void *userdata) {
f5ee7d74 1899
051e77ca
SS
1900 Network *network = userdata;
1901 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
051e77ca
SS
1902 int r;
1903
1904 assert(filename);
1905 assert(section);
1906 assert(lvalue);
1907 assert(rvalue);
1908 assert(data);
1909
1910 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1911 if (r == -ENOMEM)
1912 return log_oom();
1913 if (r < 0) {
1914 log_syntax(unit, LOG_WARNING, filename, line, r,
1915 "Failed to allocate new address, ignoring assignment: %m");
1916 return 0;
1917 }
051e77ca
SS
1918
1919 r = parse_boolean(rvalue);
1920 if (r >= 0) {
1921 log_syntax(unit, LOG_WARNING, filename, line, 0,
1922 "For historical reasons, %s=%s means %s=%s. "
1923 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
1924 lvalue, rvalue, lvalue, r ? "none" : "both");
1925 n->duplicate_address_detection = r ? ADDRESS_FAMILY_NO : ADDRESS_FAMILY_YES;
1926 n = NULL;
1927 return 0;
1928 }
1929
7211c853 1930 AddressFamily a = duplicate_address_detection_address_family_from_string(rvalue);
051e77ca 1931 if (a < 0) {
7211c853 1932 log_syntax(unit, LOG_WARNING, filename, line, a,
051e77ca
SS
1933 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
1934 return 0;
1935 }
051e77ca 1936 n->duplicate_address_detection = a;
7211c853 1937
dea161d9 1938 TAKE_PTR(n);
051e77ca
SS
1939 return 0;
1940}
1941
32400c2f 1942static int address_section_verify(Address *address) {
fcbf4cb7
YW
1943 if (section_is_invalid(address->section))
1944 return -EINVAL;
1945
1946 if (address->family == AF_UNSPEC) {
1947 assert(address->section);
1948
1949 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1950 "%s: Address section without Address= field configured. "
1951 "Ignoring [Address] section from line %u.",
1952 address->section->filename, address->section->line);
1953 }
1954
2859932b
YW
1955 assert(IN_SET(address->family, AF_INET, AF_INET6));
1956
e680486d
YW
1957 if (in4_addr_is_set(&address->broadcast) &&
1958 (address->family == AF_INET6 || address->prefixlen > 30 ||
1959 in_addr_is_set(address->family, &address->in_addr_peer))) {
1960 log_warning("%s: broadcast address is set for an IPv6 address, "
1961 "an IPv4 address with peer address, or with prefix length larger than 30. "
fe841414
YW
1962 "Ignoring Broadcast= setting in the [Address] section from line %u.",
1963 address->section->filename, address->section->line);
1964
1965 address->broadcast.s_addr = 0;
1966 }
1967
1968 if (address->family == AF_INET6 && address->label) {
1969 log_warning("%s: address label is set for IPv6 address in the [Address] section from line %u. "
1970 "Ignoring Label= setting.",
1971 address->section->filename, address->section->line);
1972
1973 address->label = mfree(address->label);
1974 }
1975
20228b6d
YW
1976 if (!address->scope_set) {
1977 if (in_addr_is_localhost(address->family, &address->in_addr) > 0)
1978 address->scope = RT_SCOPE_HOST;
1979 else if (in_addr_is_link_local(address->family, &address->in_addr) > 0)
1980 address->scope = RT_SCOPE_LINK;
1981 }
07336a06 1982
2859932b
YW
1983 if (address->duplicate_address_detection < 0) {
1984 if (address->family == AF_INET6)
1985 address->duplicate_address_detection = ADDRESS_FAMILY_IPV6;
1986 else if (in4_addr_is_link_local(&address->in_addr.in))
1987 address->duplicate_address_detection = ADDRESS_FAMILY_IPV4;
1988 else
1989 address->duplicate_address_detection = ADDRESS_FAMILY_NO;
1990 } else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV6 && address->family == AF_INET)
1991 log_warning("%s: DuplicateAddressDetection=ipv6 is specified for IPv4 address, ignoring.",
1992 address->section->filename);
1993 else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV4 && address->family == AF_INET6)
1994 log_warning("%s: DuplicateAddressDetection=ipv4 is specified for IPv6 address, ignoring.",
1995 address->section->filename);
1996
bd5146c6
YW
1997 if (address->family == AF_INET6 &&
1998 !FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV6))
eaff204f
YW
1999 address->flags |= IFA_F_NODAD;
2000
fcbf4cb7
YW
2001 return 0;
2002}
32400c2f 2003
50783f91
YW
2004int network_drop_invalid_addresses(Network *network) {
2005 _cleanup_set_free_ Set *addresses = NULL;
9cd9fc8f 2006 Address *address;
50783f91 2007 int r;
32400c2f
YW
2008
2009 assert(network);
2010
50783f91
YW
2011 ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section) {
2012 Address *dup;
2013
2014 if (address_section_verify(address) < 0) {
2015 /* Drop invalid [Address] sections or Address= settings in [Network].
2016 * Note that address_free() will drop the address from addresses_by_section. */
32400c2f 2017 address_free(address);
50783f91
YW
2018 continue;
2019 }
2020
2021 /* Always use the setting specified later. So, remove the previously assigned setting. */
2022 dup = set_remove(addresses, address);
2023 if (dup) {
2024 _cleanup_free_ char *buf = NULL;
2025
2026 (void) in_addr_prefix_to_string(address->family, &address->in_addr, address->prefixlen, &buf);
2027 log_warning("%s: Duplicated address %s is specified at line %u and %u, "
2028 "dropping the address setting specified at line %u.",
2029 dup->section->filename, strna(buf), address->section->line,
2030 dup->section->line, dup->section->line);
2031 /* address_free() will drop the address from addresses_by_section. */
2032 address_free(dup);
2033 }
2034
5d003031
YW
2035 /* Use address_kernel_hash_ops here. The function address_kernel_compare_func() matches
2036 * how kernel compares addresses, and is more lenient than address_compare_func().
2037 * Hence, the logic of dedup here is stricter than when address_hash_ops is used. */
2038 r = set_ensure_put(&addresses, &address_kernel_hash_ops, address);
50783f91
YW
2039 if (r < 0)
2040 return log_oom();
2041 assert(r > 0);
2042 }
2043
2044 return 0;
32400c2f 2045}