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