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