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