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