]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-address.c
network: show route metric in debug log
[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
d9eee312
YW
921 k = address_add(link, address, &a);
922 if (k < 0)
923 return log_link_error_errno(link, k, "Could not add address: %m");
494b6b43 924
494b6b43 925 r = address_set_masquerade(a, true);
eb56eb9b 926 if (r < 0)
7750b796 927 log_link_warning_errno(link, r, "Could not enable IP masquerading, ignoring: %m");
fcf50cff 928
dfef713f 929 r = netlink_call_async(link->manager->rtnl, NULL, req, callback, link_netlink_destroy_callback, link);
fcf50cff 930 if (r < 0) {
494b6b43 931 (void) address_set_masquerade(a, false);
7750b796 932 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
fcf50cff 933 }
f579559b 934
563c69c6
TG
935 link_ref(link);
936
490ccbd5
YW
937 if (FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV4)) {
938 r = ipv4_dad_configure(a);
051e77ca
SS
939 if (r < 0)
940 log_link_warning_errno(link, r, "Failed to start IPv4ACD client, ignoring: %m");
941 }
942
80b0e860
YW
943 if (ret)
944 *ret = a;
945
d9eee312 946 return k;
f579559b
TG
947}
948
682c65b0
YW
949static int static_address_ready_callback(Address *address) {
950 Address *a;
951 Link *link;
12f7469b 952 int r;
682c65b0
YW
953
954 assert(address);
955 assert(address->link);
956
957 link = address->link;
958
959 if (!link->addresses_configured)
960 return 0;
961
962 SET_FOREACH(a, link->static_addresses)
963 if (!address_is_ready(a)) {
964 _cleanup_free_ char *str = NULL;
965
5380707a
YW
966 (void) in_addr_prefix_to_string(a->family, &a->in_addr, a->prefixlen, &str);
967 log_link_debug(link, "an address %s is not ready", strnull(str));
682c65b0
YW
968 return 0;
969 }
970
971 /* This should not be called again */
972 SET_FOREACH(a, link->static_addresses)
973 a->callback = NULL;
974
975 link->addresses_ready = true;
976
12f7469b
KF
977 r = link_set_ipv6_proxy_ndp_addresses(link);
978 if (r < 0)
979 return r;
980
682c65b0
YW
981 return link_set_routes(link);
982}
983
984static int address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
985 int r;
986
987 assert(rtnl);
988 assert(m);
989 assert(link);
990 assert(link->ifname);
991 assert(link->address_messages > 0);
682c65b0
YW
992
993 link->address_messages--;
994
995 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
996 return 1;
997
998 r = sd_netlink_message_get_errno(m);
999 if (r < 0 && r != -EEXIST) {
1000 log_link_message_warning_errno(link, m, r, "Could not set address");
1001 link_enter_failed(link);
1002 return 1;
1003 } else if (r >= 0)
1004 (void) manager_rtnl_process_address(rtnl, m, link->manager);
1005
1006 if (link->address_messages == 0) {
1007 Address *a;
1008
1009 log_link_debug(link, "Addresses set");
1010 link->addresses_configured = true;
1011
1012 /* When all static addresses are already ready, then static_address_ready_callback()
1013 * will not be called automatically. So, call it here. */
1014 a = set_first(link->static_addresses);
1015 if (!a) {
b788a429 1016 log_link_debug(link, "No static address is stored. Already removed?");
682c65b0
YW
1017 return 1;
1018 }
b788a429
YW
1019
1020 r = static_address_ready_callback(a);
682c65b0
YW
1021 if (r < 0)
1022 link_enter_failed(link);
1023 }
1024
1025 return 1;
1026}
1027
37c0b601 1028static int static_address_configure(const Address *address, Link *link) {
682c65b0
YW
1029 Address *ret;
1030 int r;
1031
1032 assert(address);
1033 assert(link);
1034
37c0b601 1035 r = address_configure(address, link, address_handler, &ret);
682c65b0
YW
1036 if (r < 0)
1037 return log_link_warning_errno(link, r, "Could not configure static address: %m");
1038
1039 link->address_messages++;
1040
1041 r = set_ensure_put(&link->static_addresses, &address_hash_ops, ret);
1042 if (r < 0)
1043 return log_link_warning_errno(link, r, "Failed to store static address: %m");
1044
1045 ret->callback = static_address_ready_callback;
1046
1047 return 0;
1048}
1049
1050int link_set_addresses(Link *link) {
1051 Address *ad;
8a08bbfc 1052 Prefix *p;
682c65b0
YW
1053 int r;
1054
1055 assert(link);
1056 assert(link->network);
1057
1058 if (link->address_remove_messages != 0) {
1059 log_link_debug(link, "Removing old addresses, new addresses will be configured later.");
1060 link->request_static_addresses = true;
1061 return 0;
1062 }
1063
bd4733da
YW
1064 if (link->address_messages != 0) {
1065 log_link_debug(link, "Static addresses are configuring.");
1066 return 0;
1067 }
1068
9cd9fc8f 1069 ORDERED_HASHMAP_FOREACH(ad, link->network->addresses_by_section) {
37c0b601 1070 r = static_address_configure(ad, link);
682c65b0
YW
1071 if (r < 0)
1072 return r;
1073 }
1074
8a08bbfc
YW
1075 HASHMAP_FOREACH(p, link->network->prefixes_by_section) {
1076 _cleanup_(address_freep) Address *address = NULL;
682c65b0 1077
8a08bbfc
YW
1078 if (!p->assign)
1079 continue;
682c65b0 1080
8a08bbfc
YW
1081 r = address_new(&address);
1082 if (r < 0)
1083 return log_oom();
682c65b0 1084
8a08bbfc
YW
1085 r = sd_radv_prefix_get_prefix(p->radv_prefix, &address->in_addr.in6, &address->prefixlen);
1086 if (r < 0)
1087 return log_link_warning_errno(link, r, "Could not get RA prefix: %m");
682c65b0 1088
8a08bbfc
YW
1089 r = generate_ipv6_eui_64_address(link, &address->in_addr.in6);
1090 if (r < 0)
1091 return log_link_warning_errno(link, r, "Could not generate EUI64 address: %m");
682c65b0 1092
8a08bbfc 1093 address->family = AF_INET6;
37c0b601 1094 r = static_address_configure(address, link);
8a08bbfc
YW
1095 if (r < 0)
1096 return r;
682c65b0
YW
1097 }
1098
1099 if (link->address_messages == 0) {
1100 link->addresses_configured = true;
1101 link->addresses_ready = true;
12f7469b
KF
1102
1103 r = link_set_ipv6_proxy_ndp_addresses(link);
1104 if (r < 0)
1105 return r;
1106
682c65b0
YW
1107 r = link_set_routes(link);
1108 if (r < 0)
1109 return r;
1110 } else {
1111 log_link_debug(link, "Setting addresses");
1112 link_set_state(link, LINK_STATE_CONFIGURING);
1113 }
1114
1115 return 0;
1116}
1117
e1fc2c43 1118int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
fe841414 1119 _cleanup_(address_freep) Address *tmp = NULL;
e1fc2c43
YW
1120 Link *link = NULL;
1121 uint16_t type;
fe841414 1122 unsigned char flags;
e1fc2c43 1123 Address *address = NULL;
fe841414 1124 int ifindex, r;
e1fc2c43
YW
1125
1126 assert(rtnl);
1127 assert(message);
1128 assert(m);
1129
1130 if (sd_netlink_message_is_error(message)) {
1131 r = sd_netlink_message_get_errno(message);
1132 if (r < 0)
1133 log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring");
1134
1135 return 0;
1136 }
1137
1138 r = sd_netlink_message_get_type(message, &type);
1139 if (r < 0) {
1140 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1141 return 0;
1142 } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) {
1143 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type);
1144 return 0;
1145 }
1146
1147 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
1148 if (r < 0) {
1149 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
1150 return 0;
1151 } else if (ifindex <= 0) {
1152 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex);
1153 return 0;
1154 }
1155
1156 r = link_get(m, ifindex, &link);
1157 if (r < 0 || !link) {
1158 /* when enumerating we might be out of sync, but we will get the address again, so just
1159 * ignore it */
1160 if (!m->enumerating)
1161 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex);
1162 return 0;
1163 }
1164
fe841414
YW
1165 r = address_new(&tmp);
1166 if (r < 0)
1167 return log_oom();
1168
1169 r = sd_rtnl_message_addr_get_family(message, &tmp->family);
e1fc2c43
YW
1170 if (r < 0) {
1171 log_link_warning(link, "rtnl: received address message without family, ignoring.");
1172 return 0;
fe841414
YW
1173 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1174 log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", tmp->family);
e1fc2c43
YW
1175 return 0;
1176 }
1177
fe841414 1178 r = sd_rtnl_message_addr_get_prefixlen(message, &tmp->prefixlen);
e1fc2c43 1179 if (r < 0) {
9a0ad16b 1180 log_link_warning_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m");
e1fc2c43
YW
1181 return 0;
1182 }
1183
fe841414 1184 r = sd_rtnl_message_addr_get_scope(message, &tmp->scope);
e1fc2c43 1185 if (r < 0) {
9a0ad16b 1186 log_link_warning_errno(link, r, "rtnl: received address message without scope, ignoring: %m");
e1fc2c43
YW
1187 return 0;
1188 }
1189
1190 r = sd_rtnl_message_addr_get_flags(message, &flags);
1191 if (r < 0) {
9a0ad16b 1192 log_link_warning_errno(link, r, "rtnl: received address message without flags, ignoring: %m");
e1fc2c43
YW
1193 return 0;
1194 }
fe841414 1195 tmp->flags = flags;
e1fc2c43 1196
fe841414 1197 switch (tmp->family) {
e1fc2c43 1198 case AF_INET:
fe841414 1199 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &tmp->in_addr.in);
e1fc2c43
YW
1200 if (r < 0) {
1201 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1202 return 0;
1203 }
1204
fe841414
YW
1205 r = sd_netlink_message_read_in_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in);
1206 if (r < 0 && r != -ENODATA) {
1207 log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m");
1208 return 0;
1209 } else if (r >= 0) {
1210 if (in4_addr_equal(&tmp->in_addr.in, &tmp->in_addr_peer.in))
1211 tmp->in_addr_peer = IN_ADDR_NULL;
fe841414
YW
1212 }
1213
1214 r = sd_netlink_message_read_in_addr(message, IFA_BROADCAST, &tmp->broadcast);
1215 if (r < 0 && r != -ENODATA) {
1216 log_link_warning_errno(link, r, "rtnl: could not get broadcast from address message, ignoring: %m");
1217 return 0;
1218 }
1219
1220 r = sd_netlink_message_read_string_strdup(message, IFA_LABEL, &tmp->label);
1221 if (r < 0 && r != -ENODATA) {
1222 log_link_warning_errno(link, r, "rtnl: could not get label from address message, ignoring: %m");
1223 return 0;
1224 } else if (r >= 0 && streq_ptr(tmp->label, link->ifname))
1225 tmp->label = mfree(tmp->label);
1226
e1fc2c43
YW
1227 break;
1228
1229 case AF_INET6:
fe841414
YW
1230 r = sd_netlink_message_read_in6_addr(message, IFA_LOCAL, &tmp->in_addr.in6);
1231 if (r >= 0) {
1232 /* Have peer address. */
1233 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in6);
1234 if (r < 0) {
1235 log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m");
1236 return 0;
1237 }
fe841414
YW
1238 } else if (r == -ENODATA) {
1239 /* Does not have peer address. */
1240 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr.in6);
1241 if (r < 0) {
1242 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1243 return 0;
1244 }
1245 } else {
1246 log_link_warning_errno(link, r, "rtnl: could not get local address from address message, ignoring: %m");
e1fc2c43
YW
1247 return 0;
1248 }
1249
1250 break;
1251
1252 default:
1253 assert_not_reached("Received unsupported address family");
1254 }
1255
fe841414 1256 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &tmp->cinfo);
e1fc2c43
YW
1257 if (r < 0 && r != -ENODATA) {
1258 log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
1259 return 0;
24f50382 1260 }
e1fc2c43 1261
fe841414 1262 (void) address_get(link, tmp, &address);
e1fc2c43
YW
1263
1264 switch (type) {
1265 case RTM_NEWADDR:
24f50382
YW
1266 log_address_debug(tmp, address ? "Remembering updated" : "Remembering foreign", link);
1267 if (!address) {
e1fc2c43 1268 /* An address appeared that we did not request */
fe841414 1269 r = address_add_foreign(link, tmp, &address);
e1fc2c43 1270 if (r < 0) {
24f50382
YW
1271 _cleanup_free_ char *buf = NULL;
1272
5380707a
YW
1273 (void) in_addr_prefix_to_string(tmp->family, &tmp->in_addr, tmp->prefixlen, &buf);
1274 log_link_warning_errno(link, r, "Failed to remember foreign address %s, ignoring: %m",
1275 strnull(buf));
e1fc2c43 1276 return 0;
24f50382 1277 }
e1fc2c43
YW
1278 }
1279
1280 /* address_update() logs internally, so we don't need to here. */
fe841414 1281 r = address_update(address, tmp);
e1fc2c43
YW
1282 if (r < 0)
1283 link_enter_failed(link);
1284
1285 break;
1286
1287 case RTM_DELADDR:
24f50382 1288 log_address_debug(tmp, address ? "Forgetting" : "Kernel removed unknown", link);
1db01a33 1289 (void) address_drop(address);
e1fc2c43
YW
1290
1291 break;
1292
1293 default:
1294 assert_not_reached("Received invalid RTNL message type");
1295 }
1296
1297 return 1;
1298}
1299
051e77ca
SS
1300static void static_address_on_acd(sd_ipv4acd *acd, int event, void *userdata) {
1301 _cleanup_free_ char *pretty = NULL;
1302 Address *address;
1303 Link *link;
1304 int r;
1305
1306 assert(acd);
1307 assert(userdata);
1308
1309 address = (Address *) userdata;
1310 link = address->link;
1311
1312 (void) in_addr_to_string(address->family, &address->in_addr, &pretty);
1313 switch (event) {
1314 case SD_IPV4ACD_EVENT_STOP:
1315 log_link_debug(link, "Stopping ACD client...");
1316 return;
1317
1318 case SD_IPV4ACD_EVENT_BIND:
1319 log_link_debug(link, "Successfully claimed address %s", strna(pretty));
1320 link_check_ready(link);
1321 break;
1322
1323 case SD_IPV4ACD_EVENT_CONFLICT:
1324 log_link_warning(link, "DAD conflict. Dropping address %s", strna(pretty));
1325 r = address_remove(address, link, NULL);
1326 if (r < 0)
1327 log_link_error_errno(link, r, "Failed to drop DAD conflicted address %s", strna(pretty));;
1328
1329 link_check_ready(link);
1330 break;
1331
1332 default:
1333 assert_not_reached("Invalid IPv4ACD event.");
1334 }
1335
c6a7531e 1336 (void) sd_ipv4acd_stop(acd);
051e77ca
SS
1337
1338 return;
1339}
1340
490ccbd5 1341static int ipv4_dad_configure(Address *address) {
051e77ca
SS
1342 int r;
1343
051e77ca 1344 assert(address);
490ccbd5 1345 assert(address->link);
051e77ca 1346
490ccbd5
YW
1347 if (address->family != AF_INET)
1348 return 0;
051e77ca 1349
490ccbd5
YW
1350 if (DEBUG_LOGGING) {
1351 _cleanup_free_ char *pretty = NULL;
051e77ca 1352
490ccbd5
YW
1353 (void) in_addr_to_string(address->family, &address->in_addr, &pretty);
1354 log_link_debug(address->link, "Starting IPv4ACD client. Probing address %s", strna(pretty));
1355 }
1356
1357 if (!address->acd) {
1358 r = sd_ipv4acd_new(&address->acd);
1359 if (r < 0)
1360 return r;
1361
1362 r = sd_ipv4acd_attach_event(address->acd, address->link->manager->event, 0);
1363 if (r < 0)
1364 return r;
1365 }
051e77ca 1366
490ccbd5 1367 r = sd_ipv4acd_set_ifindex(address->acd, address->link->ifindex);
051e77ca
SS
1368 if (r < 0)
1369 return r;
1370
b8162cd2 1371 r = sd_ipv4acd_set_mac(address->acd, &address->link->hw_addr.addr.ether);
051e77ca
SS
1372 if (r < 0)
1373 return r;
1374
1375 r = sd_ipv4acd_set_address(address->acd, &address->in_addr.in);
1376 if (r < 0)
1377 return r;
1378
1379 r = sd_ipv4acd_set_callback(address->acd, static_address_on_acd, address);
1380 if (r < 0)
1381 return r;
1382
490ccbd5 1383 return sd_ipv4acd_start(address->acd, true);
051e77ca
SS
1384}
1385
d93d655c
YW
1386static int ipv4_dad_update_mac_one(Address *address) {
1387 bool running;
1388 int r;
1389
1390 assert(address);
1391
1392 if (!address->acd)
1393 return 0;
1394
1395 running = sd_ipv4acd_is_running(address->acd);
1396
a391901e
YW
1397 r = sd_ipv4acd_stop(address->acd);
1398 if (r < 0)
1399 return r;
d93d655c 1400
b8162cd2 1401 r = sd_ipv4acd_set_mac(address->acd, &address->link->hw_addr.addr.ether);
d93d655c
YW
1402 if (r < 0)
1403 return r;
1404
1405 if (running) {
1406 r = sd_ipv4acd_start(address->acd, true);
1407 if (r < 0)
1408 return r;
1409 }
1410
1411 return 0;
1412}
1413
1414int ipv4_dad_update_mac(Link *link) {
1415 Address *address;
1416 int k, r = 0;
1417
1418 assert(link);
1419
1420 SET_FOREACH(address, link->addresses) {
1421 k = ipv4_dad_update_mac_one(address);
1422 if (k < 0 && r >= 0)
1423 r = k;
1424 }
1425
1426 return r;
1427}
1428
490ccbd5 1429int ipv4_dad_stop(Link *link) {
2488e4d9
YW
1430 Address *address;
1431 int k, r = 0;
1432
1433 assert(link);
1434
490ccbd5 1435 SET_FOREACH(address, link->addresses) {
2488e4d9
YW
1436 k = sd_ipv4acd_stop(address->acd);
1437 if (k < 0 && r >= 0)
1438 r = k;
1439 }
1440
1441 return r;
1442}
1443
7abe175c
YW
1444void ipv4_dad_unref(Link *link) {
1445 Address *address;
1446
1447 assert(link);
1448
1449 SET_FOREACH(address, link->addresses)
1450 address->acd = sd_ipv4acd_unref(address->acd);
1451}
1452
44e7b949
LP
1453int config_parse_broadcast(
1454 const char *unit,
eb0ea358
TG
1455 const char *filename,
1456 unsigned line,
1457 const char *section,
1458 unsigned section_line,
1459 const char *lvalue,
1460 int ltype,
1461 const char *rvalue,
1462 void *data,
1463 void *userdata) {
44e7b949 1464
eb0ea358 1465 Network *network = userdata;
fcbf4cb7 1466 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
94af46fc 1467 union in_addr_union u;
eb0ea358
TG
1468 int r;
1469
1470 assert(filename);
1471 assert(section);
1472 assert(lvalue);
1473 assert(rvalue);
1474 assert(data);
1475
f4859fc7 1476 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1477 if (r == -ENOMEM)
1478 return log_oom();
1479 if (r < 0) {
1480 log_syntax(unit, LOG_WARNING, filename, line, r,
1481 "Failed to allocate new address, ignoring assignment: %m");
1482 return 0;
1483 }
eb0ea358 1484
832583ad
YW
1485 if (isempty(rvalue)) {
1486 /* The broadcast address will be calculated based on Address=, and set if the link is
1487 * not a wireguard interface. Here, we do not check or set n->family. */
1488 n->broadcast = (struct in_addr) {};
1489 n->set_broadcast = -1;
1490 TAKE_PTR(n);
1491 return 0;
1492 }
1493
1494 r = parse_boolean(rvalue);
1495 if (r >= 0) {
1496 /* The broadcast address will be calculated based on Address=. Here, we do not check or
1497 * set n->family. */
1498 n->broadcast = (struct in_addr) {};
1499 n->set_broadcast = r;
1500 TAKE_PTR(n);
1501 return 0;
1502 }
1503
482e2ac1 1504 if (n->family == AF_INET6) {
d96edb2c 1505 log_syntax(unit, LOG_WARNING, filename, line, 0,
2850cd40 1506 "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
482e2ac1
TG
1507 return 0;
1508 }
1509
94af46fc 1510 r = in_addr_from_string(AF_INET, rvalue, &u);
eb0ea358 1511 if (r < 0) {
d96edb2c 1512 log_syntax(unit, LOG_WARNING, filename, line, r,
2850cd40 1513 "Broadcast is invalid, ignoring assignment: %s", rvalue);
eb0ea358
TG
1514 return 0;
1515 }
832583ad
YW
1516 if (in4_addr_is_null(&u.in)) {
1517 log_syntax(unit, LOG_WARNING, filename, line, 0,
1518 "Broadcast cannot be ANY address, ignoring assignment: %s", rvalue);
1519 return 0;
1520 }
eb0ea358 1521
94af46fc 1522 n->broadcast = u.in;
832583ad 1523 n->set_broadcast = true;
44e7b949 1524 n->family = AF_INET;
dea161d9 1525 TAKE_PTR(n);
eb0ea358
TG
1526
1527 return 0;
1528}
1529
f5ee7d74
YW
1530int config_parse_address(
1531 const char *unit,
f579559b
TG
1532 const char *filename,
1533 unsigned line,
1534 const char *section,
71a61510 1535 unsigned section_line,
f579559b
TG
1536 const char *lvalue,
1537 int ltype,
1538 const char *rvalue,
1539 void *data,
1540 void *userdata) {
44e7b949 1541
6ae115c1 1542 Network *network = userdata;
fcbf4cb7 1543 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
44e7b949 1544 union in_addr_union buffer;
b7cb4452 1545 unsigned char prefixlen;
44e7b949 1546 int r, f;
f579559b
TG
1547
1548 assert(filename);
6ae115c1 1549 assert(section);
f579559b
TG
1550 assert(lvalue);
1551 assert(rvalue);
1552 assert(data);
1553
9cd9fc8f
YW
1554 if (streq(section, "Network"))
1555 /* we are not in an Address section, so use line number instead. */
1556 r = address_new_static(network, filename, line, &n);
1557 else
f4859fc7 1558 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1559 if (r == -ENOMEM)
1560 return log_oom();
1561 if (r < 0) {
1562 log_syntax(unit, LOG_WARNING, filename, line, r,
1563 "Failed to allocate new address, ignoring assignment: %m");
1564 return 0;
1565 }
f579559b
TG
1566
1567 /* Address=address/prefixlen */
0f707207
YW
1568 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_REFUSE, &f, &buffer, &prefixlen);
1569 if (r == -ENOANO) {
d96edb2c 1570 log_syntax(unit, LOG_WARNING, filename, line, r,
0f707207
YW
1571 "An address '%s' is specified without prefix length. "
1572 "The behavior of parsing addresses without prefix length will be changed in the future release. "
1573 "Please specify prefix length explicitly.", rvalue);
1574
1575 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_LEGACY, &f, &buffer, &prefixlen);
1576 }
f579559b 1577 if (r < 0) {
d96edb2c 1578 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue);
f579559b
TG
1579 return 0;
1580 }
1581
44e7b949 1582 if (n->family != AF_UNSPEC && f != n->family) {
d96edb2c 1583 log_syntax(unit, LOG_WARNING, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue);
44e7b949
LP
1584 return 0;
1585 }
1586
c9207ff3
YW
1587 if (in_addr_is_null(f, &buffer)) {
1588 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
1589 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
1590 * let's limit the prefix length to 64 or larger. See RFC4193. */
1591 if ((f == AF_INET && prefixlen < 8) ||
1592 (f == AF_INET6 && prefixlen < 64)) {
d96edb2c 1593 log_syntax(unit, LOG_WARNING, filename, line, 0,
c9207ff3
YW
1594 "Null address with invalid prefixlen='%u', ignoring assignment: %s",
1595 prefixlen, rvalue);
1596 return 0;
1597 }
1598 }
1599
44e7b949 1600 n->family = f;
b7cb4452 1601 n->prefixlen = prefixlen;
44e7b949
LP
1602
1603 if (streq(lvalue, "Address"))
1604 n->in_addr = buffer;
1605 else
1606 n->in_addr_peer = buffer;
1607
dea161d9 1608 TAKE_PTR(n);
f579559b
TG
1609 return 0;
1610}
6ae115c1 1611
d31645ad
LP
1612int config_parse_label(
1613 const char *unit,
6ae115c1
TG
1614 const char *filename,
1615 unsigned line,
1616 const char *section,
1617 unsigned section_line,
1618 const char *lvalue,
1619 int ltype,
1620 const char *rvalue,
1621 void *data,
1622 void *userdata) {
d31645ad 1623
fcbf4cb7 1624 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
d31645ad 1625 Network *network = userdata;
6ae115c1
TG
1626 int r;
1627
1628 assert(filename);
1629 assert(section);
1630 assert(lvalue);
1631 assert(rvalue);
1632 assert(data);
1633
f4859fc7 1634 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1635 if (r == -ENOMEM)
1636 return log_oom();
1637 if (r < 0) {
1638 log_syntax(unit, LOG_WARNING, filename, line, r,
1639 "Failed to allocate new address, ignoring assignment: %m");
1640 return 0;
1641 }
6ae115c1 1642
a87d19fe 1643 if (!address_label_valid(rvalue)) {
d96edb2c 1644 log_syntax(unit, LOG_WARNING, filename, line, 0,
2850cd40 1645 "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
6ae115c1
TG
1646 return 0;
1647 }
1648
d31645ad
LP
1649 r = free_and_strdup(&n->label, rvalue);
1650 if (r < 0)
1651 return log_oom();
6ae115c1 1652
dea161d9 1653 TAKE_PTR(n);
6ae115c1
TG
1654 return 0;
1655}
ce6c77eb 1656
f5ee7d74
YW
1657int config_parse_lifetime(
1658 const char *unit,
1659 const char *filename,
1660 unsigned line,
1661 const char *section,
1662 unsigned section_line,
1663 const char *lvalue,
1664 int ltype,
1665 const char *rvalue,
1666 void *data,
1667 void *userdata) {
1668
b5834a0b 1669 Network *network = userdata;
fcbf4cb7 1670 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
d2735796 1671 uint32_t k;
b5834a0b
SS
1672 int r;
1673
1674 assert(filename);
1675 assert(section);
1676 assert(lvalue);
1677 assert(rvalue);
1678 assert(data);
1679
f4859fc7 1680 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1681 if (r == -ENOMEM)
1682 return log_oom();
1683 if (r < 0) {
1684 log_syntax(unit, LOG_WARNING, filename, line, r,
1685 "Failed to allocate new address, ignoring assignment: %m");
1686 return 0;
1687 }
b5834a0b 1688
10b20e5a
ZJS
1689 /* We accept only "forever", "infinity", empty, or "0". */
1690 if (STR_IN_SET(rvalue, "forever", "infinity", ""))
33680b0a
YW
1691 k = CACHE_INFO_INFINITY_LIFE_TIME;
1692 else if (streq(rvalue, "0"))
1693 k = 0;
1694 else {
d96edb2c 1695 log_syntax(unit, LOG_WARNING, filename, line, 0,
33680b0a 1696 "Invalid PreferredLifetime= value, ignoring: %s", rvalue);
b5834a0b
SS
1697 return 0;
1698 }
1699
33680b0a 1700 n->cinfo.ifa_prefered = k;
d2735796 1701 TAKE_PTR(n);
b5834a0b
SS
1702
1703 return 0;
1704}
1705
f5ee7d74
YW
1706int config_parse_address_flags(
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
e63be084 1718 Network *network = userdata;
fcbf4cb7 1719 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
e63be084
SS
1720 int r;
1721
1722 assert(filename);
1723 assert(section);
1724 assert(lvalue);
1725 assert(rvalue);
1726 assert(data);
1727
f4859fc7 1728 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1729 if (r == -ENOMEM)
1730 return log_oom();
1731 if (r < 0) {
1732 log_syntax(unit, LOG_WARNING, filename, line, r,
1733 "Failed to allocate new address, ignoring assignment: %m");
1734 return 0;
1735 }
e63be084
SS
1736
1737 r = parse_boolean(rvalue);
1738 if (r < 0) {
d96edb2c 1739 log_syntax(unit, LOG_WARNING, filename, line, r,
051e77ca 1740 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
e63be084
SS
1741 return 0;
1742 }
1743
eaff204f
YW
1744 if (streq(lvalue, "AddPrefixRoute"))
1745 r = !r;
1746
1747 SET_FLAG(n->flags, ltype, r);
e63be084 1748
dea161d9 1749 TAKE_PTR(n);
e63be084
SS
1750 return 0;
1751}
1752
f5ee7d74
YW
1753int config_parse_address_scope(
1754 const char *unit,
1755 const char *filename,
1756 unsigned line,
1757 const char *section,
1758 unsigned section_line,
1759 const char *lvalue,
1760 int ltype,
1761 const char *rvalue,
1762 void *data,
1763 void *userdata) {
1764
2959fb07 1765 Network *network = userdata;
fcbf4cb7 1766 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
2959fb07
SS
1767 int r;
1768
1769 assert(filename);
1770 assert(section);
1771 assert(lvalue);
1772 assert(rvalue);
1773 assert(data);
1774
1775 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1776 if (r == -ENOMEM)
1777 return log_oom();
1778 if (r < 0) {
1779 log_syntax(unit, LOG_WARNING, filename, line, r,
1780 "Failed to allocate new address, ignoring assignment: %m");
1781 return 0;
1782 }
2959fb07
SS
1783
1784 if (streq(rvalue, "host"))
1785 n->scope = RT_SCOPE_HOST;
1786 else if (streq(rvalue, "link"))
1787 n->scope = RT_SCOPE_LINK;
1788 else if (streq(rvalue, "global"))
1789 n->scope = RT_SCOPE_UNIVERSE;
1790 else {
1791 r = safe_atou8(rvalue , &n->scope);
1792 if (r < 0) {
d96edb2c 1793 log_syntax(unit, LOG_WARNING, filename, line, r,
2850cd40 1794 "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue);
2959fb07
SS
1795 return 0;
1796 }
1797 }
1798
07336a06 1799 n->scope_set = true;
dea161d9 1800 TAKE_PTR(n);
2959fb07
SS
1801 return 0;
1802}
1803
051e77ca
SS
1804int config_parse_duplicate_address_detection(
1805 const char *unit,
1806 const char *filename,
1807 unsigned line,
1808 const char *section,
1809 unsigned section_line,
1810 const char *lvalue,
1811 int ltype,
1812 const char *rvalue,
1813 void *data,
1814 void *userdata) {
f5ee7d74 1815
051e77ca
SS
1816 Network *network = userdata;
1817 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
051e77ca
SS
1818 int r;
1819
1820 assert(filename);
1821 assert(section);
1822 assert(lvalue);
1823 assert(rvalue);
1824 assert(data);
1825
1826 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1827 if (r == -ENOMEM)
1828 return log_oom();
1829 if (r < 0) {
1830 log_syntax(unit, LOG_WARNING, filename, line, r,
1831 "Failed to allocate new address, ignoring assignment: %m");
1832 return 0;
1833 }
051e77ca
SS
1834
1835 r = parse_boolean(rvalue);
1836 if (r >= 0) {
1837 log_syntax(unit, LOG_WARNING, filename, line, 0,
1838 "For historical reasons, %s=%s means %s=%s. "
1839 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
1840 lvalue, rvalue, lvalue, r ? "none" : "both");
1841 n->duplicate_address_detection = r ? ADDRESS_FAMILY_NO : ADDRESS_FAMILY_YES;
1842 n = NULL;
1843 return 0;
1844 }
1845
7211c853 1846 AddressFamily a = duplicate_address_detection_address_family_from_string(rvalue);
051e77ca 1847 if (a < 0) {
7211c853 1848 log_syntax(unit, LOG_WARNING, filename, line, a,
051e77ca
SS
1849 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
1850 return 0;
1851 }
051e77ca 1852 n->duplicate_address_detection = a;
7211c853 1853
dea161d9 1854 TAKE_PTR(n);
051e77ca
SS
1855 return 0;
1856}
1857
ce6c77eb
TG
1858bool address_is_ready(const Address *a) {
1859 assert(a);
1860
ce158189 1861 return !(a->flags & IFA_F_TENTATIVE);
ce6c77eb 1862}
fcbf4cb7 1863
32400c2f 1864static int address_section_verify(Address *address) {
fcbf4cb7
YW
1865 if (section_is_invalid(address->section))
1866 return -EINVAL;
1867
1868 if (address->family == AF_UNSPEC) {
1869 assert(address->section);
1870
1871 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1872 "%s: Address section without Address= field configured. "
1873 "Ignoring [Address] section from line %u.",
1874 address->section->filename, address->section->line);
1875 }
1876
2a236f9f 1877 if (address_may_have_broadcast(address)) {
832583ad 1878 if (address->broadcast.s_addr == 0 && address->set_broadcast != 0)
05a7023d
YW
1879 address->broadcast.s_addr = address->in_addr.in.s_addr | htobe32(0xfffffffflu >> address->prefixlen);
1880 } else if (address->broadcast.s_addr != 0) {
fe841414
YW
1881 log_warning("%s: broadcast address is set for IPv6 address or IPv4 address with prefixlength larger than 30. "
1882 "Ignoring Broadcast= setting in the [Address] section from line %u.",
1883 address->section->filename, address->section->line);
1884
1885 address->broadcast.s_addr = 0;
1886 }
1887
1888 if (address->family == AF_INET6 && address->label) {
1889 log_warning("%s: address label is set for IPv6 address in the [Address] section from line %u. "
1890 "Ignoring Label= setting.",
1891 address->section->filename, address->section->line);
1892
1893 address->label = mfree(address->label);
1894 }
1895
cd1caf30
YW
1896 if (in_addr_is_localhost(address->family, &address->in_addr) > 0 &&
1897 (address->family == AF_INET || !address->scope_set)) {
1898 /* For IPv4, scope must be always RT_SCOPE_HOST.
1899 * For IPv6, use RT_SCOPE_HOST only when it is not explicitly specified. */
1900
1901 if (address->scope_set && address->scope != RT_SCOPE_HOST)
1902 log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1903 "%s: non-host scope is set in the [Address] section from line %u. "
1904 "Ignoring Scope= setting.",
1905 address->section->filename, address->section->line);
1906
07336a06 1907 address->scope = RT_SCOPE_HOST;
cd1caf30 1908 }
07336a06 1909
eaff204f
YW
1910 if (!FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV6))
1911 address->flags |= IFA_F_NODAD;
1912
fcbf4cb7
YW
1913 return 0;
1914}
32400c2f 1915
13ffa39f 1916void network_drop_invalid_addresses(Network *network) {
9cd9fc8f 1917 Address *address;
32400c2f
YW
1918
1919 assert(network);
1920
9cd9fc8f 1921 ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section)
32400c2f
YW
1922 if (address_section_verify(address) < 0)
1923 address_free(address);
1924}