]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-address.c
network: use strdup_or_null() where appropriate
[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"
3ae6b3bf 8#include "logarithm.h"
0a970718 9#include "memory-util.h"
fc2f9534 10#include "netlink-util.h"
bfbf150e 11#include "networkd-address-pool.h"
6bedfcbb 12#include "networkd-address.h"
3b6a3bde 13#include "networkd-dhcp-server.h"
76a86ffd 14#include "networkd-ipv4acd.h"
23f53b99 15#include "networkd-manager.h"
4b3590c3 16#include "networkd-netlabel.h"
093e3533 17#include "networkd-network.h"
76c5a0f2 18#include "networkd-queue.h"
d8c472f2 19#include "networkd-route-util.h"
3b6a3bde 20#include "networkd-route.h"
6bedfcbb 21#include "parse-util.h"
07630cea 22#include "string-util.h"
51517f9e 23#include "strv.h"
0fd97a25 24#include "strxcpyx.h"
f579559b 25
1b566071 26#define ADDRESSES_PER_LINK_MAX 2048U
8c34b963
LP
27#define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
28
b1179a5d
YW
29#define KNOWN_FLAGS \
30 (IFA_F_SECONDARY | \
31 IFA_F_NODAD | \
32 IFA_F_OPTIMISTIC | \
33 IFA_F_DADFAILED | \
34 IFA_F_HOMEADDRESS | \
35 IFA_F_DEPRECATED | \
36 IFA_F_TENTATIVE | \
37 IFA_F_PERMANENT | \
38 IFA_F_MANAGETEMPADDR | \
39 IFA_F_NOPREFIXROUTE | \
40 IFA_F_MCAUTOJOIN | \
41 IFA_F_STABLE_PRIVACY)
42
43/* From net/ipv4/devinet.c */
44#define IPV6ONLY_FLAGS \
45 (IFA_F_NODAD | \
46 IFA_F_OPTIMISTIC | \
47 IFA_F_DADFAILED | \
48 IFA_F_HOMEADDRESS | \
49 IFA_F_TENTATIVE | \
50 IFA_F_MANAGETEMPADDR | \
51 IFA_F_STABLE_PRIVACY)
52
53/* We do not control the following flags. */
54#define UNMANAGED_FLAGS \
55 (IFA_F_SECONDARY | \
56 IFA_F_DADFAILED | \
57 IFA_F_DEPRECATED | \
58 IFA_F_TENTATIVE | \
59 IFA_F_PERMANENT | \
60 IFA_F_STABLE_PRIVACY)
61
42ceb474 62int address_flags_to_string_alloc(uint32_t flags, int family, char **ret) {
a2362484 63 _cleanup_free_ char *str = NULL;
7396e014
ZJS
64 static const char* map[] = {
65 [LOG2U(IFA_F_SECONDARY)] = "secondary", /* This is also called "temporary" for ipv6. */
66 [LOG2U(IFA_F_NODAD)] = "nodad",
67 [LOG2U(IFA_F_OPTIMISTIC)] = "optimistic",
68 [LOG2U(IFA_F_DADFAILED)] = "dadfailed",
69 [LOG2U(IFA_F_HOMEADDRESS)] = "home-address",
70 [LOG2U(IFA_F_DEPRECATED)] = "deprecated",
71 [LOG2U(IFA_F_TENTATIVE)] = "tentative",
72 [LOG2U(IFA_F_PERMANENT)] = "permanent",
73 [LOG2U(IFA_F_MANAGETEMPADDR)] = "manage-temporary-address",
74 [LOG2U(IFA_F_NOPREFIXROUTE)] = "no-prefixroute",
75 [LOG2U(IFA_F_MCAUTOJOIN)] = "auto-join",
76 [LOG2U(IFA_F_STABLE_PRIVACY)] = "stable-privacy",
a2362484
YW
77 };
78
79 assert(IN_SET(family, AF_INET, AF_INET6));
80 assert(ret);
81
0828a386 82 for (size_t i = 0; i < ELEMENTSOF(map); i++)
7396e014
ZJS
83 if (FLAGS_SET(flags, 1 << i) && map[i])
84 if (!strextend_with_separator(
85 &str, ",",
86 family == AF_INET6 && (1 << i) == IFA_F_SECONDARY ? "temporary" : map[i]))
87 return -ENOMEM;
a2362484
YW
88
89 *ret = TAKE_PTR(str);
90 return 0;
91}
92
60415c13
YW
93static LinkAddressState address_state_from_scope(uint8_t scope) {
94 if (scope < RT_SCOPE_SITE)
95 /* universally accessible addresses found */
96 return LINK_ADDRESS_STATE_ROUTABLE;
97
98 if (scope < RT_SCOPE_HOST)
99 /* only link or site local addresses found */
100 return LINK_ADDRESS_STATE_DEGRADED;
101
102 /* no useful addresses found */
103 return LINK_ADDRESS_STATE_OFF;
104}
105
106void link_get_address_states(
107 Link *link,
108 LinkAddressState *ret_ipv4,
109 LinkAddressState *ret_ipv6,
110 LinkAddressState *ret_all) {
111
112 uint8_t ipv4_scope = RT_SCOPE_NOWHERE, ipv6_scope = RT_SCOPE_NOWHERE;
113 Address *address;
114
115 assert(link);
116
117 SET_FOREACH(address, link->addresses) {
118 if (!address_is_ready(address))
119 continue;
120
121 if (address->family == AF_INET)
122 ipv4_scope = MIN(ipv4_scope, address->scope);
123
124 if (address->family == AF_INET6)
125 ipv6_scope = MIN(ipv6_scope, address->scope);
126 }
127
128 if (ret_ipv4)
129 *ret_ipv4 = address_state_from_scope(ipv4_scope);
130 if (ret_ipv6)
131 *ret_ipv6 = address_state_from_scope(ipv6_scope);
132 if (ret_all)
133 *ret_all = address_state_from_scope(MIN(ipv4_scope, ipv6_scope));
134}
135
ebd96906
YW
136static void address_hash_func(const Address *a, struct siphash *state);
137static int address_compare_func(const Address *a1, const Address *a2);
138static void address_detach(Address *address);
139
140DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
141 address_hash_ops_detach,
142 Address,
143 address_hash_func,
144 address_compare_func,
145 address_detach);
146
147DEFINE_HASH_OPS(
148 address_hash_ops,
149 Address,
150 address_hash_func,
151 address_compare_func);
152
153DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
154 address_section_hash_ops,
155 ConfigSection,
156 config_section_hash_func,
157 config_section_compare_func,
158 Address,
159 address_detach);
160
f0213e37 161int address_new(Address **ret) {
ebd96906 162 _cleanup_(address_unrefp) Address *address = NULL;
f0213e37 163
17f9c355 164 address = new(Address, 1);
f0213e37
TG
165 if (!address)
166 return -ENOMEM;
aba496a5 167
17f9c355 168 *address = (Address) {
ebd96906 169 .n_ref = 1,
17f9c355
YW
170 .family = AF_UNSPEC,
171 .scope = RT_SCOPE_UNIVERSE,
16bc8635
YW
172 .lifetime_valid_usec = USEC_INFINITY,
173 .lifetime_preferred_usec = USEC_INFINITY,
832583ad 174 .set_broadcast = -1,
17f9c355 175 };
f0213e37 176
1cc6c93a 177 *ret = TAKE_PTR(address);
f0213e37
TG
178
179 return 0;
aba496a5
UTL
180}
181
5459e11d 182int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
307fe3cd 183 _cleanup_(config_section_freep) ConfigSection *n = NULL;
ebd96906 184 _cleanup_(address_unrefp) Address *address = NULL;
f0213e37 185 int r;
f579559b 186
8c34b963
LP
187 assert(network);
188 assert(ret);
9cd9fc8f
YW
189 assert(filename);
190 assert(section_line > 0);
f4859fc7 191
307fe3cd 192 r = config_section_new(filename, section_line, &n);
9cd9fc8f
YW
193 if (r < 0)
194 return r;
6ae115c1 195
9cd9fc8f
YW
196 address = ordered_hashmap_get(network->addresses_by_section, n);
197 if (address) {
198 *ret = TAKE_PTR(address);
199 return 0;
6ae115c1
TG
200 }
201
9cd9fc8f 202 if (ordered_hashmap_size(network->addresses_by_section) >= STATIC_ADDRESSES_PER_NETWORK_MAX)
8c34b963
LP
203 return -E2BIG;
204
f0213e37
TG
205 r = address_new(&address);
206 if (r < 0)
207 return r;
801bd9e8 208
0f7f2769 209 address->network = network;
9cd9fc8f 210 address->section = TAKE_PTR(n);
3b6a3bde 211 address->source = NETWORK_CONFIG_SOURCE_STATIC;
2859932b
YW
212 /* This will be adjusted in address_section_verify(). */
213 address->duplicate_address_detection = _ADDRESS_FAMILY_INVALID;
0f7f2769 214
ebd96906 215 r = ordered_hashmap_ensure_put(&network->addresses_by_section, &address_section_hash_ops, address->section, address);
9cd9fc8f
YW
216 if (r < 0)
217 return r;
6ae115c1 218
1cc6c93a 219 *ret = TAKE_PTR(address);
f579559b
TG
220 return 0;
221}
222
ebd96906
YW
223static Address* address_detach_impl(Address *address) {
224 assert(address);
225 assert(!address->link || !address->network);
f579559b 226
f048a16b 227 if (address->network) {
9cd9fc8f
YW
228 assert(address->section);
229 ordered_hashmap_remove(address->network->addresses_by_section, address->section);
8ae3a4f5
YW
230
231 if (address->network->dhcp_server_address == address)
232 address->network->dhcp_server_address = NULL;
ebd96906
YW
233
234 address->network = NULL;
235 return address;
f048a16b 236 }
6ae115c1 237
ebd96906 238 if (address->link) {
cf1d700d 239 set_remove(address->link->addresses, address);
f150100a 240
ebd96906
YW
241 address->link = NULL;
242 return address;
243 }
244
245 return NULL;
246}
247
248static void address_detach(Address *address) {
249 assert(address);
250
251 address_unref(address_detach_impl(address));
252}
253
254static Address* address_free(Address *address) {
255 if (!address)
256 return NULL;
257
258 address_detach_impl(address);
259
307fe3cd 260 config_section_free(address->section);
de4224aa 261 free(address->label);
4b3590c3 262 free(address->netlabel);
fc289dd0 263 nft_set_context_clear(&address->nft_set_context);
7818f858 264 return mfree(address);
f579559b
TG
265}
266
ebd96906
YW
267DEFINE_TRIVIAL_REF_UNREF_FUNC(Address, address, address_free);
268
dc32de39
YW
269static bool address_lifetime_is_valid(const Address *a) {
270 assert(a);
271
272 return
273 a->lifetime_valid_usec == USEC_INFINITY ||
274 a->lifetime_valid_usec > now(CLOCK_BOOTTIME);
275}
276
3b6a3bde
YW
277bool address_is_ready(const Address *a) {
278 assert(a);
5385e5f9 279 assert(a->link);
3b6a3bde 280
5385e5f9 281 if (!ipv4acd_bound(a->link, a))
3b6a3bde
YW
282 return false;
283
e402e99e 284 if (FLAGS_SET(a->flags, IFA_F_TENTATIVE))
3b6a3bde
YW
285 return false;
286
e402e99e 287 if (FLAGS_SET(a->state, NETWORK_CONFIG_STATE_REMOVING))
3b6a3bde
YW
288 return false;
289
290 if (!FLAGS_SET(a->state, NETWORK_CONFIG_STATE_CONFIGURED))
291 return false;
292
dc32de39 293 return address_lifetime_is_valid(a);
3b6a3bde
YW
294}
295
5f950e56
YW
296bool link_check_addresses_ready(Link *link, NetworkConfigSource source) {
297 Address *a;
298 bool has = false;
299
300 assert(link);
301
302 /* Check if all addresses on the interface are ready. If there is no address, this will return false. */
303
304 SET_FOREACH(a, link->addresses) {
305 if (source >= 0 && a->source != source)
306 continue;
307 if (address_is_marked(a))
308 continue;
309 if (!address_exists(a))
310 continue;
311 if (!address_is_ready(a))
312 return false;
313 has = true;
314 }
315
316 return has;
317}
318
2ccada8d 319void link_mark_addresses(Link *link, NetworkConfigSource source) {
3b6a3bde
YW
320 Address *a;
321
322 assert(link);
323
324 SET_FOREACH(a, link->addresses) {
325 if (a->source != source)
326 continue;
327
3b6a3bde
YW
328 address_mark(a);
329 }
330}
331
0ae8ff50 332static int address_get_broadcast(const Address *a, Link *link, struct in_addr *ret) {
1a1f4a80
YW
333 struct in_addr b_addr = {};
334
2a236f9f 335 assert(a);
e680486d 336 assert(link);
2a236f9f 337
1a1f4a80
YW
338 /* Returns 0 when broadcast address is null, 1 when non-null broadcast address, -EAGAIN when the main
339 * address is null. */
340
341 /* broadcast is only for IPv4. */
473680be 342 if (a->family != AF_INET)
1a1f4a80 343 goto finalize;
473680be 344
1a1f4a80 345 /* broadcast address cannot be used when peer address is specified. */
473680be 346 if (in4_addr_is_set(&a->in_addr_peer.in))
1a1f4a80 347 goto finalize;
473680be 348
2a236f9f
YW
349 /* A /31 or /32 IPv4 address does not have a broadcast address.
350 * See https://tools.ietf.org/html/rfc3021 */
473680be 351 if (a->prefixlen > 30)
1a1f4a80 352 goto finalize;
473680be 353
1a1f4a80
YW
354 /* If explicitly configured, use the address as is. */
355 if (in4_addr_is_set(&a->broadcast)) {
356 b_addr = a->broadcast;
357 goto finalize;
358 }
473680be 359
1a1f4a80
YW
360 /* If explicitly disabled, then return null address. */
361 if (a->set_broadcast == 0)
362 goto finalize;
473680be 363
1a1f4a80
YW
364 /* For wireguard interfaces, broadcast is disabled by default. */
365 if (a->set_broadcast < 0 && streq_ptr(link->kind, "wireguard"))
366 goto finalize;
2a236f9f 367
1a1f4a80
YW
368 /* If the main address is null, e.g. Address=0.0.0.0/24, the broadcast address will be automatically
369 * determined after an address is acquired. */
370 if (!in4_addr_is_set(&a->in_addr.in))
371 return -EAGAIN;
372
373 /* Otherwise, generate a broadcast address from the main address and prefix length. */
374 b_addr.s_addr = a->in_addr.in.s_addr | htobe32(UINT32_C(0xffffffff) >> a->prefixlen);
832583ad 375
1a1f4a80
YW
376finalize:
377 if (ret)
378 *ret = b_addr;
832583ad 379
1a1f4a80 380 return in4_addr_is_set(&b_addr);
832583ad
YW
381}
382
0ae8ff50
YW
383static void address_set_broadcast(Address *a, Link *link) {
384 assert(a);
385 assert_se(address_get_broadcast(a, link, &a->broadcast) >= 0);
386}
387
b9e4ec50 388static void address_set_cinfo(Manager *m, const Address *a, struct ifa_cacheinfo *cinfo) {
16bc8635
YW
389 usec_t now_usec;
390
def4741b 391 assert(m);
16bc8635
YW
392 assert(a);
393 assert(cinfo);
394
def4741b 395 assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &now_usec) >= 0);
16bc8635
YW
396
397 *cinfo = (struct ifa_cacheinfo) {
5235d739
YW
398 .ifa_valid = usec_to_sec(a->lifetime_valid_usec, now_usec),
399 .ifa_prefered = usec_to_sec(a->lifetime_preferred_usec, now_usec),
16bc8635 400 };
16bc8635
YW
401}
402
def4741b 403static void address_set_lifetime(Manager *m, Address *a, const struct ifa_cacheinfo *cinfo) {
16bc8635
YW
404 usec_t now_usec;
405
def4741b 406 assert(m);
16bc8635
YW
407 assert(a);
408 assert(cinfo);
409
def4741b 410 assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &now_usec) >= 0);
16bc8635 411
5235d739
YW
412 a->lifetime_valid_usec = sec_to_usec(cinfo->ifa_valid, now_usec);
413 a->lifetime_preferred_usec = sec_to_usec(cinfo->ifa_prefered, now_usec);
16bc8635
YW
414}
415
d0009d92
YW
416static bool address_is_static_null(const Address *address) {
417 assert(address);
418
419 if (!address->network)
420 return false;
421
422 if (!address->requested_as_null)
423 return false;
424
425 assert(!in_addr_is_set(address->family, &address->in_addr));
426 return true;
427}
428
381e3cc6
YW
429static int address_ipv4_prefix(const Address *a, struct in_addr *ret) {
430 struct in_addr p;
431 int r;
432
1d30fc5c 433 assert(a);
381e3cc6
YW
434 assert(a->family == AF_INET);
435 assert(ret);
1d30fc5c 436
381e3cc6
YW
437 p = in4_addr_is_set(&a->in_addr_peer.in) ? a->in_addr_peer.in : a->in_addr.in;
438 r = in4_addr_mask(&p, a->prefixlen);
439 if (r < 0)
440 return r;
1d30fc5c 441
381e3cc6
YW
442 *ret = p;
443 return 0;
1d30fc5c
YW
444}
445
4da252c5 446static void address_hash_func(const Address *a, struct siphash *state) {
3ac8e543
TG
447 assert(a);
448
c01a5c05 449 siphash24_compress_typesafe(a->family, state);
3ac8e543 450
1d30fc5c 451 switch (a->family) {
381e3cc6
YW
452 case AF_INET: {
453 struct in_addr prefix;
454
c01a5c05 455 siphash24_compress_typesafe(a->prefixlen, state);
3ac8e543 456
381e3cc6 457 assert_se(address_ipv4_prefix(a, &prefix) >= 0);
c01a5c05 458 siphash24_compress_typesafe(prefix, state);
3ac8e543 459
c01a5c05 460 siphash24_compress_typesafe(a->in_addr.in, state);
52af8e5e 461 break;
381e3cc6 462 }
1d30fc5c 463 case AF_INET6:
c01a5c05 464 siphash24_compress_typesafe(a->in_addr.in6, state);
6174dc7d
YW
465
466 if (in6_addr_is_null(&a->in_addr.in6))
c01a5c05 467 siphash24_compress_typesafe(a->prefixlen, state);
1d30fc5c 468 break;
52af8e5e 469
1d30fc5c
YW
470 default:
471 /* treat any other address family as AF_UNSPEC */
472 break;
473 }
3ac8e543
TG
474}
475
4da252c5 476static int address_compare_func(const Address *a1, const Address *a2) {
a0edd02e 477 int r;
3ac8e543 478
a0edd02e
FB
479 r = CMP(a1->family, a2->family);
480 if (r != 0)
481 return r;
3ac8e543 482
1d30fc5c 483 switch (a1->family) {
381e3cc6
YW
484 case AF_INET: {
485 struct in_addr p1, p2;
486
1d30fc5c
YW
487 /* See kernel's find_matching_ifa() in net/ipv4/devinet.c */
488 r = CMP(a1->prefixlen, a2->prefixlen);
24be9181
ZJS
489 if (r != 0)
490 return r;
3ac8e543 491
381e3cc6
YW
492 assert_se(address_ipv4_prefix(a1, &p1) >= 0);
493 assert_se(address_ipv4_prefix(a2, &p2) >= 0);
494 r = memcmp(&p1, &p2, sizeof(p1));
1d30fc5c
YW
495 if (r != 0)
496 return r;
2a236f9f 497
52af8e5e 498 return memcmp(&a1->in_addr.in, &a2->in_addr.in, sizeof(a1->in_addr.in));
381e3cc6 499 }
1d30fc5c
YW
500 case AF_INET6:
501 /* See kernel's ipv6_get_ifaddr() in net/ipv6/addrconf.c */
6174dc7d
YW
502 r = memcmp(&a1->in_addr.in6, &a2->in_addr.in6, sizeof(a1->in_addr.in6));
503 if (r != 0)
504 return r;
505
506 /* To distinguish IPv6 null addresses with different prefixlen, e.g. ::48 vs ::64, let's
507 * compare the prefix length. */
508 if (in6_addr_is_null(&a1->in_addr.in6))
509 r = CMP(a1->prefixlen, a2->prefixlen);
510
511 return r;
52af8e5e 512
1d30fc5c
YW
513 default:
514 /* treat any other address family as AF_UNSPEC */
515 return 0;
516 }
3ac8e543
TG
517}
518
b5aefc90
YW
519static bool address_can_update(const Address *la, const Address *na) {
520 assert(la);
521 assert(la->link);
522 assert(na);
523 assert(na->network);
524
525 /*
526 * property | IPv4 | IPv6
527 * -----------------------------------------
528 * family | ✗ | ✗
529 * prefixlen | ✗ | ✗
530 * address | ✗ | ✗
531 * scope | ✗ | -
532 * label | ✗ | -
533 * broadcast | ✗ | -
534 * peer | ✗ | ✓
535 * flags | ✗ | ✓
536 * lifetime | ✓ | ✓
537 * route metric | ✓ | ✓
538 * protocol | ✓ | ✓
539 *
540 * ✗ : cannot be changed
541 * ✓ : can be changed
542 * - : unused
543 *
544 * IPv4 : See inet_rtm_newaddr() in net/ipv4/devinet.c.
545 * IPv6 : See inet6_addr_modify() in net/ipv6/addrconf.c.
546 */
547
548 if (la->family != na->family)
549 return false;
550
551 if (la->prefixlen != na->prefixlen)
552 return false;
553
554 /* When a null address is requested, the address to be assigned/updated will be determined later. */
555 if (!address_is_static_null(na) &&
556 in_addr_equal(la->family, &la->in_addr, &na->in_addr) <= 0)
557 return false;
558
559 switch (la->family) {
560 case AF_INET: {
561 struct in_addr bcast;
562
563 if (la->scope != na->scope)
564 return false;
565 if (((la->flags ^ na->flags) & KNOWN_FLAGS & ~IPV6ONLY_FLAGS & ~UNMANAGED_FLAGS) != 0)
566 return false;
567 if (!streq_ptr(la->label, na->label))
568 return false;
569 if (!in4_addr_equal(&la->in_addr_peer.in, &na->in_addr_peer.in))
570 return false;
571 if (address_get_broadcast(na, la->link, &bcast) >= 0) {
572 /* If the broadcast address can be determined now, check if they match. */
573 if (!in4_addr_equal(&la->broadcast, &bcast))
574 return false;
575 } else {
576 /* When a null address is requested, then the broadcast address will be
577 * automatically calculated from the acquired address, e.g.
578 * 192.168.0.10/24 -> 192.168.0.255
579 * So, here let's only check if the broadcast is the last address in the range, e.g.
580 * 0.0.0.0/24 -> 0.0.0.255 */
581 if (!FLAGS_SET(la->broadcast.s_addr, htobe32(UINT32_C(0xffffffff) >> la->prefixlen)))
582 return false;
583 }
584 break;
585 }
586 case AF_INET6:
587 break;
588
589 default:
590 assert_not_reached();
591 }
592
593 return true;
594}
595
99b46669 596int address_dup(const Address *src, Address **ret) {
ebd96906 597 _cleanup_(address_unrefp) Address *dest = NULL;
cde1f0e8
YW
598 int r;
599
cde1f0e8 600 assert(src);
99b46669
YW
601 assert(ret);
602
603 dest = newdup(Address, src, 1);
604 if (!dest)
605 return -ENOMEM;
606
ebd96906
YW
607 /* clear the reference counter and all pointers */
608 dest->n_ref = 1;
99b46669
YW
609 dest->network = NULL;
610 dest->section = NULL;
611 dest->link = NULL;
612 dest->label = NULL;
4b3590c3 613 dest->netlabel = NULL;
fc289dd0
TM
614 dest->nft_set_context.sets = NULL;
615 dest->nft_set_context.n_sets = 0;
cde1f0e8 616
2a236f9f 617 if (src->family == AF_INET) {
6504d41c 618 r = strdup_or_null(src->label, &dest->label);
2a236f9f
YW
619 if (r < 0)
620 return r;
621 }
cde1f0e8 622
6504d41c 623 r = strdup_or_null(src->netlabel, &dest->netlabel);
4b3590c3
TM
624 if (r < 0)
625 return r;
626
fc289dd0
TM
627 r = nft_set_context_dup(&src->nft_set_context, &dest->nft_set_context);
628 if (r < 0)
629 return r;
630
99b46669 631 *ret = TAKE_PTR(dest);
cde1f0e8
YW
632 return 0;
633}
634
494b6b43
YW
635static int address_set_masquerade(Address *address, bool add) {
636 union in_addr_union masked;
91b5f997
TG
637 int r;
638
639 assert(address);
494b6b43 640 assert(address->link);
91b5f997 641
494b6b43
YW
642 if (!address->link->network)
643 return 0;
91b5f997 644
48ed2766
FW
645 if (address->family == AF_INET &&
646 !FLAGS_SET(address->link->network->ip_masquerade, ADDRESS_FAMILY_IPV4))
494b6b43 647 return 0;
91b5f997 648
48ed2766
FW
649 if (address->family == AF_INET6 &&
650 !FLAGS_SET(address->link->network->ip_masquerade, ADDRESS_FAMILY_IPV6))
494b6b43 651 return 0;
91b5f997 652
494b6b43
YW
653 if (address->scope >= RT_SCOPE_LINK)
654 return 0;
655
da0e2bae 656 if (address->ip_masquerade_done == add)
494b6b43
YW
657 return 0;
658
659 masked = address->in_addr;
660 r = in_addr_mask(address->family, &masked, address->prefixlen);
661 if (r < 0)
662 return r;
663
48ed2766 664 r = fw_add_masquerade(&address->link->manager->fw_ctx, add, address->family, &masked, address->prefixlen);
494b6b43
YW
665 if (r < 0)
666 return r;
667
da0e2bae 668 address->ip_masquerade_done = add;
91b5f997
TG
669
670 return 0;
671}
672
fc289dd0
TM
673static void address_modify_nft_set_context(Address *address, bool add, NFTSetContext *nft_set_context) {
674 int r;
675
676 assert(address);
677 assert(address->link);
678 assert(address->link->manager);
679 assert(nft_set_context);
680
681 if (!address->link->manager->fw_ctx) {
58c6e75f 682 r = fw_ctx_new_full(&address->link->manager->fw_ctx, /* init_tables= */ false);
fc289dd0
TM
683 if (r < 0)
684 return;
685 }
686
687 FOREACH_ARRAY(nft_set, nft_set_context->sets, nft_set_context->n_sets) {
688 uint32_t ifindex;
689
690 assert(nft_set);
691
692 switch (nft_set->source) {
693 case NFT_SET_SOURCE_ADDRESS:
694 r = nft_set_element_modify_ip(address->link->manager->fw_ctx, add, nft_set->nfproto, address->family, nft_set->table, nft_set->set,
695 &address->in_addr);
696 break;
697 case NFT_SET_SOURCE_PREFIX:
698 r = nft_set_element_modify_iprange(address->link->manager->fw_ctx, add, nft_set->nfproto, address->family, nft_set->table, nft_set->set,
699 &address->in_addr, address->prefixlen);
700 break;
701 case NFT_SET_SOURCE_IFINDEX:
702 ifindex = address->link->ifindex;
703 r = nft_set_element_modify_any(address->link->manager->fw_ctx, add, nft_set->nfproto, nft_set->table, nft_set->set,
704 &ifindex, sizeof(ifindex));
705 break;
706 default:
707 assert_not_reached();
708 }
709
710 if (r < 0)
13b8fb98 711 log_warning_errno(r, "Failed to %s NFT set: family %s, table %s, set %s, IP address %s, ignoring: %m",
4d9a30f1 712 add ? "add" : "delete",
fc289dd0
TM
713 nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set,
714 IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen));
715 else
716 log_debug("%s NFT set: family %s, table %s, set %s, IP address %s",
717 add ? "Added" : "Deleted",
718 nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set,
719 IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen));
720 }
721}
722
723static void address_modify_nft_set(Address *address, bool add) {
724 assert(address);
725 assert(address->link);
726
727 if (!IN_SET(address->family, AF_INET, AF_INET6))
728 return;
729
730 if (!address->link->network)
731 return;
732
733 switch (address->source) {
734 case NETWORK_CONFIG_SOURCE_DHCP4:
735 return address_modify_nft_set_context(address, add, &address->link->network->dhcp_nft_set_context);
736 case NETWORK_CONFIG_SOURCE_DHCP6:
737 return address_modify_nft_set_context(address, add, &address->link->network->dhcp6_nft_set_context);
738 case NETWORK_CONFIG_SOURCE_DHCP_PD:
739 return address_modify_nft_set_context(address, add, &address->link->network->dhcp_pd_nft_set_context);
740 case NETWORK_CONFIG_SOURCE_NDISC:
741 return address_modify_nft_set_context(address, add, &address->link->network->ndisc_nft_set_context);
742 case NETWORK_CONFIG_SOURCE_STATIC:
743 return address_modify_nft_set_context(address, add, &address->nft_set_context);
744 default:
745 return;
746 }
747}
748
ebd96906 749static int address_attach(Link *link, Address *address) {
cf1d700d
TG
750 int r;
751
752 assert(link);
3b6a3bde 753 assert(address);
ebd96906 754 assert(!address->link);
cf1d700d 755
ebd96906 756 r = set_ensure_put(&link->addresses, &address_hash_ops_detach, address);
cf1d700d
TG
757 if (r < 0)
758 return r;
75a302b5
YW
759 if (r == 0)
760 return -EEXIST;
cf1d700d
TG
761
762 address->link = link;
ebd96906 763 address_ref(address);
7b3a7581 764 return 0;
adda1ed9
TG
765}
766
f9831557 767static int address_update(Address *address) {
4bbe559b 768 Link *link = ASSERT_PTR(ASSERT_PTR(address)->link);
e7ab854c 769 int r;
36c32f61 770
7657ec3e
YW
771 if (address_is_ready(address) &&
772 address->family == AF_INET6 &&
773 in6_addr_is_link_local(&address->in_addr.in6) &&
774 in6_addr_is_null(&link->ipv6ll_address)) {
775
776 link->ipv6ll_address = address->in_addr.in6;
777
778 r = link_ipv6ll_gained(link);
779 if (r < 0)
780 return r;
781 }
782
2c40a889 783 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
ea121d8f
YW
784 return 0;
785
4bbe559b 786 r = address_set_masquerade(address, /* add = */ true);
7a8685ff
YW
787 if (r < 0)
788 return log_link_warning_errno(link, r, "Could not enable IP masquerading: %m");
789
4b3590c3
TM
790 address_add_netlabel(address);
791
fc289dd0
TM
792 address_modify_nft_set(address, /* add = */ true);
793
981fab5d
YW
794 if (address_is_ready(address) && address->callback) {
795 r = address->callback(address);
796 if (r < 0)
797 return r;
a3a019e1 798 }
36c32f61 799
4bbe559b 800 link_update_operstate(link, /* also_update_master = */ true);
981fab5d 801 link_check_ready(link);
36c32f61
TG
802 return 0;
803}
804
67a58eb3 805static int address_drop(Address *address) {
4bbe559b 806 Link *link = ASSERT_PTR(ASSERT_PTR(address)->link);
7750b796 807 int r;
8012cd39 808
4bbe559b 809 r = address_set_masquerade(address, /* add = */ false);
7750b796
YW
810 if (r < 0)
811 log_link_warning_errno(link, r, "Failed to disable IP masquerading, ignoring: %m");
812
fc289dd0
TM
813 address_modify_nft_set(address, /* add = */ false);
814
4b3590c3
TM
815 address_del_netlabel(address);
816
f089d3eb
YW
817 /* FIXME: if the IPv6LL address is dropped, stop DHCPv6, NDISC, RADV. */
818 if (address->family == AF_INET6 &&
819 in6_addr_equal(&address->in_addr.in6, &link->ipv6ll_address))
820 link->ipv6ll_address = (const struct in6_addr) {};
821
822 ipv4acd_detach(link, address);
823
ebd96906 824 address_detach(address);
91b5f997 825
aaad20e0
YW
826 link_update_operstate(link, /* also_update_master = */ true);
827 link_check_ready(link);
91b5f997
TG
828 return 0;
829}
830
a2532c9d
YW
831static bool address_match_null(const Address *a, const Address *null_address) {
832 assert(a);
833 assert(null_address);
834
835 if (!a->requested_as_null)
836 return false;
837
838 /* Currently, null address is supported only by static addresses. Note that static
839 * address may be set as foreign during reconfiguring the interface. */
840 if (!IN_SET(a->source, NETWORK_CONFIG_SOURCE_FOREIGN, NETWORK_CONFIG_SOURCE_STATIC))
841 return false;
842
843 if (a->family != null_address->family)
844 return false;
845
846 if (a->prefixlen != null_address->prefixlen)
847 return false;
848
849 return true;
850}
851
0a0c2672
YW
852static int address_get_request(Link *link, const Address *address, Request **ret) {
853 Request *req;
854
855 assert(link);
856 assert(link->manager);
857 assert(address);
858
859 req = ordered_set_get(
860 link->manager->request_queue,
861 &(Request) {
862 .link = link,
863 .type = REQUEST_TYPE_ADDRESS,
864 .userdata = (void*) address,
4da252c5
YW
865 .hash_func = (hash_func_t) address_hash_func,
866 .compare_func = (compare_func_t) address_compare_func,
0a0c2672
YW
867 });
868 if (req) {
869 if (ret)
870 *ret = req;
871 return 0;
872 }
873
874 if (address_is_static_null(address))
875 ORDERED_SET_FOREACH(req, link->manager->request_queue) {
876 if (req->link != link)
877 continue;
878 if (req->type != REQUEST_TYPE_ADDRESS)
879 continue;
880
881 if (!address_match_null(req->userdata, address))
882 continue;
883
884 if (ret)
885 *ret = req;
886
887 return 0;
888 }
889
890 return -ENOENT;
891}
892
fe841414 893int address_get(Link *link, const Address *in, Address **ret) {
d0009d92 894 Address *a;
91b5f997 895
5a8bcb67 896 assert(link);
fe841414 897 assert(in);
5a8bcb67 898
d0009d92
YW
899 a = set_get(link->addresses, in);
900 if (a) {
901 if (ret)
902 *ret = a;
903 return 0;
904 }
905
906 /* Find matching address that originally requested as null address. */
907 if (address_is_static_null(in))
908 SET_FOREACH(a, link->addresses) {
a2532c9d 909 if (!address_match_null(a, in))
d0009d92
YW
910 continue;
911
912 if (ret)
913 *ret = a;
914 return 0;
915 }
916
917 return -ENOENT;
5a8bcb67
LP
918}
919
b33dd04e
YW
920int address_get_harder(Link *link, const Address *in, Address **ret) {
921 Request *req;
922 int r;
923
924 assert(link);
925 assert(in);
926
927 if (address_get(link, in, ret) >= 0)
928 return 0;
929
930 r = address_get_request(link, in, &req);
931 if (r < 0)
932 return r;
933
934 if (ret)
935 *ret = ASSERT_PTR(req->userdata);
936
937 return 0;
938}
939
5d003031 940int link_get_address(Link *link, int family, const union in_addr_union *address, unsigned char prefixlen, Address **ret) {
390247d2 941 Address *a;
c5a0aeb3 942 int r;
5eec0a08 943
c5a0aeb3 944 assert(link);
5d003031 945 assert(IN_SET(family, AF_INET, AF_INET6));
c5a0aeb3 946 assert(address);
5eec0a08 947
5d003031
YW
948 /* This find an Address object on the link which matches the given address and prefix length
949 * and does not have peer address. When the prefixlen is zero, then an Address object with an
950 * arbitrary prefixlen will be returned. */
998545a7 951
42f8b6a8 952 if (family == AF_INET6 || prefixlen != 0) {
ebd96906 953 _cleanup_(address_unrefp) Address *tmp = NULL;
998545a7 954
42f8b6a8 955 /* In this case, we can use address_get(). */
998545a7 956
390247d2 957 r = address_new(&tmp);
998545a7
YW
958 if (r < 0)
959 return r;
960
390247d2
YW
961 tmp->family = family;
962 tmp->in_addr = *address;
963 tmp->prefixlen = prefixlen;
3b6a3bde 964
42f8b6a8
YW
965 r = address_get(link, tmp, &a);
966 if (r < 0)
967 return r;
3b6a3bde 968
42f8b6a8
YW
969 if (family == AF_INET6) {
970 /* IPv6 addresses are managed without peer address and prefix length. Hence, we need
971 * to check them explicitly. */
972 if (in_addr_is_set(family, &a->in_addr_peer))
973 return -ENOENT;
974 if (prefixlen != 0 && a->prefixlen != prefixlen)
975 return -ENOENT;
3b6a3bde
YW
976 }
977
42f8b6a8
YW
978 if (ret)
979 *ret = a;
390247d2 980
42f8b6a8 981 return 0;
390247d2
YW
982 }
983
984 SET_FOREACH(a, link->addresses) {
985 if (a->family != family)
986 continue;
987
988 if (!in_addr_equal(family, &a->in_addr, address))
989 continue;
990
991 if (in_addr_is_set(family, &a->in_addr_peer))
992 continue;
993
994 if (ret)
995 *ret = a;
996
997 return 0;
3b6a3bde 998 }
390247d2
YW
999
1000 return -ENOENT;
998545a7
YW
1001}
1002
77e9be1e 1003int manager_get_address(Manager *manager, int family, const union in_addr_union *address, unsigned char prefixlen, Address **ret) {
591bd5f3 1004 Link *link;
591bd5f3
YW
1005
1006 assert(manager);
1007 assert(IN_SET(family, AF_INET, AF_INET6));
1008 assert(address);
1009
77e9be1e
YW
1010 HASHMAP_FOREACH(link, manager->links_by_index) {
1011 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
1012 continue;
1013
1014 if (link_get_address(link, family, address, prefixlen, ret) >= 0)
1015 return 0;
1016 }
1017
1018 return -ENOENT;
1019}
1020
1a4b0459 1021bool manager_has_address(Manager *manager, int family, const union in_addr_union *address) {
77e9be1e
YW
1022 Address *a;
1023
1024 assert(manager);
1025 assert(IN_SET(family, AF_INET, AF_INET6));
1026 assert(address);
1027
1028 if (manager_get_address(manager, family, address, 0, &a) < 0)
1029 return false;
591bd5f3 1030
1a4b0459 1031 return address_is_ready(a);
591bd5f3
YW
1032}
1033
16bc8635 1034const char* format_lifetime(char *buf, size_t l, usec_t lifetime_usec) {
0fd97a25 1035 assert(buf);
a35f3128 1036 assert(l > 4);
0fd97a25 1037
16bc8635 1038 if (lifetime_usec == USEC_INFINITY)
a35f3128 1039 return "forever";
0fd97a25 1040
a35f3128
ZJS
1041 sprintf(buf, "for ");
1042 /* format_timespan() never fails */
ba4e0427 1043 assert_se(format_timespan(buf + 4, l - 4, usec_sub_unsigned(lifetime_usec, now(CLOCK_BOOTTIME)), USEC_PER_SEC));
a35f3128 1044 return buf;
0fd97a25
YW
1045}
1046
24f50382 1047static void log_address_debug(const Address *address, const char *str, const Link *link) {
84dbb3fd 1048 _cleanup_free_ char *state = NULL, *flags_str = NULL, *scope_str = NULL;
7653a9dc 1049
24f50382
YW
1050 assert(address);
1051 assert(str);
1052 assert(link);
1053
7653a9dc
YW
1054 if (!DEBUG_LOGGING)
1055 return;
1056
3b6a3bde 1057 (void) network_config_state_to_string_alloc(address->state, &state);
84dbb3fd
ZJS
1058
1059 const char *peer = in_addr_is_set(address->family, &address->in_addr_peer) ?
1060 IN_ADDR_TO_STRING(address->family, &address->in_addr_peer) : NULL;
7653a9dc 1061
14044a41
YW
1062 const char *broadcast = (address->family == AF_INET && in4_addr_is_set(&address->broadcast)) ?
1063 IN4_ADDR_TO_STRING(&address->broadcast) : NULL;
1064
a2362484 1065 (void) address_flags_to_string_alloc(address->flags, address->family, &flags_str);
78ef25a5 1066 (void) route_scope_to_string_alloc(address->scope, &scope_str);
a2362484 1067
14044a41 1068 log_link_debug(link, "%s %s address (%s): %s%s%s/%u%s%s (valid %s, preferred %s), flags: %s, scope: %s%s%s",
3b6a3bde 1069 str, strna(network_config_source_to_string(address->source)), strna(state),
84dbb3fd
ZJS
1070 IN_ADDR_TO_STRING(address->family, &address->in_addr),
1071 peer ? " peer " : "", strempty(peer), address->prefixlen,
14044a41 1072 broadcast ? " broadcast " : "", strempty(broadcast),
16bc8635
YW
1073 FORMAT_LIFETIME(address->lifetime_valid_usec),
1074 FORMAT_LIFETIME(address->lifetime_preferred_usec),
14044a41
YW
1075 strna(flags_str), strna(scope_str),
1076 address->family == AF_INET ? ", label: " : "",
1077 address->family == AF_INET ? strna(address->label) : "");
24f50382
YW
1078}
1079
a79a8d16 1080static int address_set_netlink_message(const Address *address, sd_netlink_message *m, Link *link) {
d5e3b3eb 1081 uint32_t flags;
a8481354
YW
1082 int r;
1083
1084 assert(address);
a79a8d16 1085 assert(m);
a8481354
YW
1086 assert(link);
1087
a79a8d16 1088 r = sd_rtnl_message_addr_set_prefixlen(m, address->prefixlen);
a8481354 1089 if (r < 0)
a79a8d16 1090 return r;
a8481354
YW
1091
1092 /* On remove, only IFA_F_MANAGETEMPADDR flag for IPv6 addresses are used. But anyway, set all
d5e3b3eb
YW
1093 * flags except tentative flag here unconditionally. Without setting the flag, the template
1094 * addresses generated by kernel will not be removed automatically when the main address is
1095 * removed. */
1096 flags = address->flags & ~IFA_F_TENTATIVE;
a79a8d16 1097 r = sd_rtnl_message_addr_set_flags(m, flags & 0xff);
a8481354 1098 if (r < 0)
a79a8d16 1099 return r;
a8481354 1100
d5e3b3eb 1101 if ((flags & ~0xff) != 0) {
a79a8d16 1102 r = sd_netlink_message_append_u32(m, IFA_FLAGS, flags);
a8481354 1103 if (r < 0)
a79a8d16 1104 return r;
a8481354
YW
1105 }
1106
a79a8d16 1107 r = netlink_message_append_in_addr_union(m, IFA_LOCAL, address->family, &address->in_addr);
a8481354 1108 if (r < 0)
a79a8d16 1109 return r;
a8481354
YW
1110
1111 return 0;
1112}
1113
56a995fe 1114static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, RemoveRequest *rreq) {
5a07fa9d
YW
1115 int r;
1116
5a07fa9d 1117 assert(m);
56a995fe 1118 assert(rreq);
76c5a0f2 1119
56a995fe
YW
1120 Link *link = ASSERT_PTR(rreq->link);
1121 Address *address = ASSERT_PTR(rreq->userdata);
1122
1123 if (link->state == LINK_STATE_LINGER)
5a07fa9d
YW
1124 return 0;
1125
1126 r = sd_netlink_message_get_errno(m);
56a995fe
YW
1127 if (r < 0) {
1128 log_link_message_full_errno(link, m,
1129 (r == -EADDRNOTAVAIL || !address->link) ? LOG_DEBUG : LOG_WARNING,
1130 r, "Could not drop address");
1131
1132 if (address->link) {
1133 /* If the address cannot be removed, then assume the address is already removed. */
1134 log_address_debug(address, "Forgetting", link);
1135
1136 Request *req;
1137 if (address_get_request(link, address, &req) >= 0)
1138 address_enter_removed(req->userdata);
1139
1140 (void) address_drop(address);
1141 }
1142 }
5a07fa9d
YW
1143
1144 return 1;
1145}
1146
7f74b00a 1147int address_remove(Address *address, Link *link) {
a79a8d16 1148 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
407fe036
TG
1149 int r;
1150
1151 assert(address);
4c701096 1152 assert(IN_SET(address->family, AF_INET, AF_INET6));
7f74b00a
YW
1153 assert(link);
1154 assert(link->ifindex > 0);
1155 assert(link->manager);
1156 assert(link->manager->rtnl);
407fe036 1157
24f50382 1158 log_address_debug(address, "Removing", link);
30226d27 1159
a79a8d16 1160 r = sd_rtnl_message_new_addr(link->manager->rtnl, &m, RTM_DELADDR,
151b9b96 1161 link->ifindex, address->family);
eb56eb9b 1162 if (r < 0)
a79a8d16 1163 return log_link_warning_errno(link, r, "Could not allocate RTM_DELADDR message: %m");
407fe036 1164
a79a8d16 1165 r = address_set_netlink_message(address, m, link);
eb56eb9b 1166 if (r < 0)
a79a8d16 1167 return log_link_warning_errno(link, r, "Could not set netlink attributes: %m");
407fe036 1168
56a995fe 1169 r = link_remove_request_add(link, address, address, link->manager->rtnl, m, address_remove_handler);
eb56eb9b 1170 if (r < 0)
56a995fe 1171 return log_link_warning_errno(link, r, "Could not queue rtnetlink message: %m");
563c69c6 1172
3b6a3bde 1173 address_enter_removing(address);
613d65d8
YW
1174
1175 /* The operational state is determined by address state and carrier state. Hence, if we remove
1176 * an address, the operational state may be changed. */
1177 link_update_operstate(link, true);
407fe036
TG
1178 return 0;
1179}
1180
f22b586a
YW
1181int address_remove_and_cancel(Address *address, Link *link) {
1182 bool waiting = false;
1183 Request *req;
d4b76314 1184
f22b586a
YW
1185 assert(address);
1186 assert(link);
1187 assert(link->manager);
d4b76314 1188
f22b586a
YW
1189 /* If the address is remembered by the link, then use the remembered object. */
1190 (void) address_get(link, address, &address);
d4b76314 1191
f22b586a
YW
1192 /* Cancel the request for the address. If the request is already called but we have not received the
1193 * notification about the request, then explicitly remove the address. */
1194 if (address_get_request(link, address, &req) >= 0) {
1195 waiting = req->waiting_reply;
b9b5096e 1196 request_detach(req);
f22b586a
YW
1197 address_cancel_requesting(address);
1198 }
1199
1200 /* If we know the address will come or already exists, remove it. */
1201 if (waiting || (address->link && address_exists(address)))
1202 return address_remove(address, link);
1203
1204 return 0;
d4b76314
YW
1205}
1206
3def8850 1207bool link_address_is_dynamic(const Link *link, const Address *address) {
f8f2f880
YW
1208 Route *route;
1209
1210 assert(link);
1211 assert(address);
1212
16bc8635 1213 if (address->lifetime_preferred_usec != USEC_INFINITY)
f8f2f880
YW
1214 return true;
1215
1216 /* Even when the address is leased from a DHCP server, networkd assign the address
1217 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
1218 * corresponding routes with RTPROT_DHCP. */
3b6a3bde
YW
1219 SET_FOREACH(route, link->routes) {
1220 if (route->source != NETWORK_CONFIG_SOURCE_FOREIGN)
1221 continue;
1222
f974f17a
YW
1223 /* The route is not assigned yet, or already removed. Ignoring. */
1224 if (!route_exists(route))
1225 continue;
1226
f8f2f880
YW
1227 if (route->protocol != RTPROT_DHCP)
1228 continue;
1229
1230 if (address->family != route->family)
1231 continue;
1232
1233 if (in_addr_equal(address->family, &address->in_addr, &route->prefsrc))
1234 return true;
1235 }
1236
1237 return false;
1238}
1239
b9bf3f22 1240int link_drop_ipv6ll_addresses(Link *link) {
51f5dfd8 1241 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
51f5dfd8
YW
1242 int r;
1243
1244 assert(link);
1245 assert(link->manager);
1246 assert(link->manager->rtnl);
1247
b9bf3f22
YW
1248 /* IPv6LL address may be in the tentative state, and in that case networkd has not received it.
1249 * So, we need to dump all IPv6 addresses. */
1250
bd7e0a3f 1251 if (link_may_have_ipv6ll(link, /* check_multicast = */ false))
b9bf3f22
YW
1252 return 0;
1253
c31298be 1254 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_GETADDR, link->ifindex, AF_INET6);
51f5dfd8
YW
1255 if (r < 0)
1256 return r;
1257
24c0f385 1258 r = sd_netlink_message_set_request_dump(req, true);
f318f643
YW
1259 if (r < 0)
1260 return r;
1261
51f5dfd8
YW
1262 r = sd_netlink_call(link->manager->rtnl, req, 0, &reply);
1263 if (r < 0)
1264 return r;
1265
cda7fc8d 1266 for (sd_netlink_message *addr = reply; addr; addr = sd_netlink_message_next(addr)) {
ebd96906 1267 _cleanup_(address_unrefp) Address *a = NULL;
b9bf3f22
YW
1268 unsigned char flags, prefixlen;
1269 struct in6_addr address;
51f5dfd8
YW
1270 int ifindex;
1271
c31298be
YW
1272 /* NETLINK_GET_STRICT_CHK socket option is supported since kernel 4.20. To support
1273 * older kernels, we need to check ifindex here. */
51f5dfd8
YW
1274 r = sd_rtnl_message_addr_get_ifindex(addr, &ifindex);
1275 if (r < 0) {
b9bf3f22 1276 log_link_debug_errno(link, r, "rtnl: received address message without valid ifindex, ignoring: %m");
51f5dfd8
YW
1277 continue;
1278 } else if (link->ifindex != ifindex)
1279 continue;
1280
1281 r = sd_rtnl_message_addr_get_flags(addr, &flags);
1282 if (r < 0) {
b9bf3f22
YW
1283 log_link_debug_errno(link, r, "rtnl: received address message without valid flags, ignoring: %m");
1284 continue;
1285 }
1286
1287 r = sd_rtnl_message_addr_get_prefixlen(addr, &prefixlen);
1288 if (r < 0) {
1289 log_link_debug_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m");
51f5dfd8 1290 continue;
b9bf3f22
YW
1291 }
1292
1293 if (sd_netlink_message_read_in6_addr(addr, IFA_LOCAL, NULL) >= 0)
1294 /* address with peer, ignoring. */
51f5dfd8
YW
1295 continue;
1296
b9bf3f22
YW
1297 r = sd_netlink_message_read_in6_addr(addr, IFA_ADDRESS, &address);
1298 if (r < 0) {
1299 log_link_debug_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1300 continue;
1301 }
1302
1303 if (!in6_addr_is_link_local(&address))
1304 continue;
1305
1306 r = address_new(&a);
1307 if (r < 0)
1308 return -ENOMEM;
1309
1310 a->family = AF_INET6;
1311 a->in_addr.in6 = address;
1312 a->prefixlen = prefixlen;
1313 a->flags = flags;
1314
f1b73dbb 1315 r = address_remove(a, link);
b9bf3f22
YW
1316 if (r < 0)
1317 return r;
51f5dfd8
YW
1318 }
1319
1320 return 0;
1321}
1322
f8f2f880
YW
1323int link_drop_foreign_addresses(Link *link) {
1324 Address *address;
1339b950 1325 int r = 0;
f8f2f880
YW
1326
1327 assert(link);
3b6a3bde 1328 assert(link->network);
f8f2f880 1329
3b6a3bde
YW
1330 /* First, mark all addresses. */
1331 SET_FOREACH(address, link->addresses) {
b9bf3f22 1332 /* We consider IPv6LL addresses to be managed by the kernel, or dropped in link_drop_ipv6ll_addresses() */
ecedc48b 1333 if (address->family == AF_INET6 && in6_addr_is_link_local(&address->in_addr.in6))
f8f2f880
YW
1334 continue;
1335
b69bfa43
YW
1336 /* Do not remove localhost address (127.0.0.1 and ::1) */
1337 if (link->flags & IFF_LOOPBACK && in_addr_is_localhost_one(address->family, &address->in_addr) > 0)
1338 continue;
1339
3b6a3bde
YW
1340 /* Ignore addresses we configured. */
1341 if (address->source != NETWORK_CONFIG_SOURCE_FOREIGN)
1342 continue;
1343
1344 /* Ignore addresses not assigned yet or already removing. */
1345 if (!address_exists(address))
1346 continue;
1347
a71c5c44
YW
1348 /* link_address_is_dynamic() is slightly heavy. Let's call the function only when KeepConfiguration= is set. */
1349 if (IN_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP, KEEP_CONFIGURATION_STATIC) &&
1350 link_address_is_dynamic(link, address) == (link->network->keep_configuration == KEEP_CONFIGURATION_DHCP))
f8f2f880
YW
1351 continue;
1352
3b6a3bde
YW
1353 address_mark(address);
1354 }
1355
1356 /* Then, unmark requested addresses. */
1357 ORDERED_HASHMAP_FOREACH(address, link->network->addresses_by_section) {
1358 Address *existing;
1359
d0009d92
YW
1360 if (address_get(link, address, &existing) < 0)
1361 continue;
1362
b5aefc90 1363 if (!address_can_update(existing, address))
d0009d92
YW
1364 continue;
1365
1366 /* Found matching static configuration. Keep the existing address. */
1367 address_unmark(existing);
3b6a3bde
YW
1368 }
1369
1370 /* Finally, remove all marked addresses. */
1371 SET_FOREACH(address, link->addresses) {
1372 if (!address_is_marked(address))
1373 continue;
1374
7f74b00a 1375 RET_GATHER(r, address_remove(address, link));
f8f2f880
YW
1376 }
1377
1378 return r;
1379}
1380
a0e99a37 1381int link_drop_managed_addresses(Link *link) {
3b6a3bde 1382 Address *address;
1339b950 1383 int r = 0;
d7fbb9f5
YW
1384
1385 assert(link);
1386
1387 SET_FOREACH(address, link->addresses) {
a0e99a37
YW
1388 /* Do not touch addresses managed by kernel or other tools. */
1389 if (address->source == NETWORK_CONFIG_SOURCE_FOREIGN)
3b6a3bde
YW
1390 continue;
1391
a0e99a37
YW
1392 /* Ignore addresses not assigned yet or already removing. */
1393 if (!address_exists(address))
d7fbb9f5
YW
1394 continue;
1395
7f74b00a 1396 RET_GATHER(r, address_remove(address, link));
d7fbb9f5
YW
1397 }
1398
1399 return r;
1400}
1401
b4564f4e
YW
1402void link_foreignize_addresses(Link *link) {
1403 Address *address;
1404
1405 assert(link);
1406
1407 SET_FOREACH(address, link->addresses)
1408 address->source = NETWORK_CONFIG_SOURCE_FOREIGN;
1409}
1410
693ec5ca 1411static int address_acquire(Link *link, const Address *original, Address **ret) {
ebd96906 1412 _cleanup_(address_unrefp) Address *na = NULL;
3b6a3bde 1413 union in_addr_union in_addr;
11bf3cce
LP
1414 int r;
1415
1416 assert(link);
1417 assert(original);
1418 assert(ret);
1419
1420 /* Something useful was configured? just use it */
9684a8de
YW
1421 if (in_addr_is_set(original->family, &original->in_addr))
1422 return address_dup(original, ret);
11bf3cce
LP
1423
1424 /* The address is configured to be 0.0.0.0 or [::] by the user?
1425 * Then let's acquire something more useful from the pool. */
ed76f585 1426 r = address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
6a7a4e4d 1427 if (r < 0)
7750b796
YW
1428 return r;
1429 if (r == 0)
11bf3cce 1430 return -EBUSY;
11bf3cce 1431
473680be
YW
1432 /* Pick first address in range for ourselves. */
1433 if (original->family == AF_INET)
11bf3cce 1434 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
473680be 1435 else if (original->family == AF_INET6)
11bf3cce
LP
1436 in_addr.in6.s6_addr[15] |= 1;
1437
99b46669 1438 r = address_dup(original, &na);
cde1f0e8
YW
1439 if (r < 0)
1440 return r;
11bf3cce 1441
11bf3cce
LP
1442 na->in_addr = in_addr;
1443
1cc6c93a 1444 *ret = TAKE_PTR(na);
9684a8de 1445 return 0;
11bf3cce
LP
1446}
1447
5a07fa9d
YW
1448int address_configure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
1449 int r;
1450
1451 assert(rtnl);
1452 assert(m);
1453 assert(link);
1454 assert(error_msg);
1455
5a07fa9d
YW
1456 r = sd_netlink_message_get_errno(m);
1457 if (r < 0 && r != -EEXIST) {
1458 log_link_message_warning_errno(link, m, r, error_msg);
1459 link_enter_failed(link);
1460 return 0;
3a1dfdb4 1461 }
5a07fa9d
YW
1462
1463 return 1;
1464}
1465
b9e4ec50 1466static int address_configure(const Address *address, const struct ifa_cacheinfo *c, Link *link, Request *req) {
a79a8d16 1467 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
7b3a7581 1468 int r;
f579559b 1469
c166a070 1470 assert(address);
4c701096 1471 assert(IN_SET(address->family, AF_INET, AF_INET6));
b9e4ec50 1472 assert(c);
c166a070
TG
1473 assert(link);
1474 assert(link->ifindex > 0);
f882c247 1475 assert(link->manager);
c166a070 1476 assert(link->manager->rtnl);
54ff39f7 1477 assert(req);
f882c247 1478
c0c04a4b 1479 log_address_debug(address, "Configuring", link);
1b566071 1480
a79a8d16 1481 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &m, link->ifindex, address->family);
eb56eb9b 1482 if (r < 0)
a79a8d16 1483 return r;
f579559b 1484
a79a8d16 1485 r = address_set_netlink_message(address, m, link);
eb56eb9b 1486 if (r < 0)
a8481354 1487 return r;
851c9f82 1488
a79a8d16 1489 r = sd_rtnl_message_addr_set_scope(m, address->scope);
eb56eb9b 1490 if (r < 0)
a79a8d16 1491 return r;
5a723174 1492
f35aecc4 1493 if (address->family == AF_INET6 || in_addr_is_set(address->family, &address->in_addr_peer)) {
a79a8d16 1494 r = netlink_message_append_in_addr_union(m, IFA_ADDRESS, address->family, &address->in_addr_peer);
eb56eb9b 1495 if (r < 0)
a79a8d16 1496 return r;
e680486d 1497 } else if (in4_addr_is_set(&address->broadcast)) {
a79a8d16 1498 r = sd_netlink_message_append_in_addr(m, IFA_BROADCAST, &address->broadcast);
66e0bb33 1499 if (r < 0)
a79a8d16 1500 return r;
f579559b
TG
1501 }
1502
2a236f9f 1503 if (address->family == AF_INET && address->label) {
a79a8d16 1504 r = sd_netlink_message_append_string(m, IFA_LABEL, address->label);
eb56eb9b 1505 if (r < 0)
a79a8d16 1506 return r;
f579559b
TG
1507 }
1508
b9e4ec50 1509 r = sd_netlink_message_append_cache_info(m, IFA_CACHEINFO, c);
eb56eb9b 1510 if (r < 0)
a79a8d16 1511 return r;
68ceb9df 1512
a79a8d16 1513 r = sd_netlink_message_append_u32(m, IFA_RT_PRIORITY, address->route_metric);
c4ff0629 1514 if (r < 0)
a79a8d16 1515 return r;
c4ff0629 1516
80d62d4f 1517 return request_call_netlink_async(link->manager->rtnl, m, req);
f579559b
TG
1518}
1519
8bed7c55
YW
1520static bool address_is_ready_to_configure(Link *link, const Address *address) {
1521 assert(link);
1522 assert(address);
1523
1524 if (!link_is_ready_to_configure(link, false))
1525 return false;
1526
5385e5f9 1527 if (!ipv4acd_bound(link, address))
8bed7c55
YW
1528 return false;
1529
1530 /* Refuse adding more than the limit */
1531 if (set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX)
1532 return false;
1533
1534 return true;
1535}
1536
09d09207 1537static int address_process_request(Request *req, Link *link, Address *address) {
60f4cfe4 1538 Address *existing;
b9e4ec50 1539 struct ifa_cacheinfo c;
8bed7c55
YW
1540 int r;
1541
1542 assert(req);
ff51134c
YW
1543 assert(link);
1544 assert(address);
8bed7c55
YW
1545
1546 if (!address_is_ready_to_configure(link, address))
1547 return 0;
1548
b9e4ec50
YW
1549 address_set_cinfo(link->manager, address, &c);
1550 if (c.ifa_valid == 0) {
1551 log_link_debug(link, "Refuse to configure %s address %s, as its valid lifetime is zero.",
1552 network_config_source_to_string(address->source),
1553 IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen));
0a0c2672 1554
b9e4ec50 1555 address_cancel_requesting(address);
0a0c2672 1556 if (address_get(link, address, &existing) >= 0)
d6b64f7a 1557 address_cancel_requesting(existing);
b9e4ec50
YW
1558 return 1;
1559 }
1560
1561 r = address_configure(address, &c, link, req);
8bed7c55
YW
1562 if (r < 0)
1563 return log_link_warning_errno(link, r, "Failed to configure address: %m");
1564
1565 address_enter_configuring(address);
0a0c2672
YW
1566 if (address_get(link, address, &existing) >= 0)
1567 address_enter_configuring(existing);
1568
8bed7c55
YW
1569 return 1;
1570}
1571
76c5a0f2
YW
1572int link_request_address(
1573 Link *link,
f60e6558 1574 const Address *address,
76c5a0f2 1575 unsigned *message_counter,
80d62d4f 1576 address_netlink_handler_t netlink_handler,
76c5a0f2
YW
1577 Request **ret) {
1578
ebd96906 1579 _cleanup_(address_unrefp) Address *tmp = NULL;
0a0c2672 1580 Address *existing = NULL;
c0c04a4b
YW
1581 int r;
1582
76c5a0f2
YW
1583 assert(link);
1584 assert(address);
3b6a3bde 1585 assert(address->source != NETWORK_CONFIG_SOURCE_FOREIGN);
76c5a0f2 1586
0a0c2672
YW
1587 if (address->lifetime_valid_usec == 0)
1588 /* The requested address is outdated. Let's ignore the request. */
1589 return 0;
af2aea8b 1590
0a0c2672
YW
1591 if (address_get(link, address, &existing) < 0) {
1592 if (address_get_request(link, address, NULL) >= 0)
1593 return 0; /* already requested, skipping. */
303dfa73 1594
9684a8de
YW
1595 r = address_acquire(link, address, &tmp);
1596 if (r < 0)
1597 return log_link_warning_errno(link, r, "Failed to acquire an address from pool: %m");
af2aea8b 1598
3b6a3bde
YW
1599 /* Consider address tentative until we get the real flags from the kernel */
1600 tmp->flags |= IFA_F_TENTATIVE;
1601
3b6a3bde 1602 } else {
0a0c2672 1603 r = address_dup(address, &tmp);
42f8b6a8 1604 if (r < 0)
0a0c2672 1605 return log_oom();
9684a8de 1606
0a0c2672
YW
1607 /* Copy already assigned address when it is requested as a null address. */
1608 if (address_is_static_null(address))
1609 tmp->in_addr = existing->in_addr;
9684a8de 1610
0a0c2672
YW
1611 /* Copy state for logging below. */
1612 tmp->state = existing->state;
c0c04a4b
YW
1613 }
1614
0a0c2672
YW
1615 address_set_broadcast(tmp, link);
1616
1617 r = ipv4acd_configure(link, tmp);
3b6a3bde
YW
1618 if (r < 0)
1619 return r;
1620
0a0c2672 1621 log_address_debug(tmp, "Requesting", link);
09d09207 1622 r = link_queue_request_safe(link, REQUEST_TYPE_ADDRESS,
0a0c2672 1623 tmp,
ebd96906 1624 address_unref,
4da252c5
YW
1625 address_hash_func,
1626 address_compare_func,
09d09207
YW
1627 address_process_request,
1628 message_counter, netlink_handler, ret);
c0c04a4b
YW
1629 if (r < 0)
1630 return log_link_warning_errno(link, r, "Failed to request address: %m");
3b6a3bde
YW
1631 if (r == 0)
1632 return 0;
1633
0a0c2672
YW
1634 address_enter_requesting(tmp);
1635 if (existing)
1636 address_enter_requesting(existing);
3b6a3bde 1637
0a0c2672 1638 TAKE_PTR(tmp);
3b6a3bde 1639 return 1;
76c5a0f2
YW
1640}
1641
80d62d4f 1642static int static_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) {
e1e4cd1e
YW
1643 int r;
1644
1645 assert(link);
e1e4cd1e
YW
1646
1647 r = address_configure_handler_internal(rtnl, m, link, "Failed to set static address");
1648 if (r <= 0)
1649 return r;
1650
1651 if (link->static_address_messages == 0) {
1652 log_link_debug(link, "Addresses set");
1653 link->static_addresses_configured = true;
1654 link_check_ready(link);
1655 }
1656
1657 return 1;
1658}
1659
f60e6558 1660int link_request_static_address(Link *link, const Address *address) {
8c29f484
YW
1661 assert(link);
1662 assert(address);
3b6a3bde 1663 assert(address->source == NETWORK_CONFIG_SOURCE_STATIC);
8c29f484 1664
f60e6558 1665 return link_request_address(link, address, &link->static_address_messages,
3b6a3bde 1666 static_address_handler, NULL);
8c29f484
YW
1667}
1668
76c5a0f2
YW
1669int link_request_static_addresses(Link *link) {
1670 Address *a;
682c65b0
YW
1671 int r;
1672
1673 assert(link);
1674 assert(link->network);
1675
76c5a0f2 1676 link->static_addresses_configured = false;
682c65b0 1677
76c5a0f2 1678 ORDERED_HASHMAP_FOREACH(a, link->network->addresses_by_section) {
f60e6558 1679 r = link_request_static_address(link, a);
682c65b0
YW
1680 if (r < 0)
1681 return r;
1682 }
1683
3b6a3bde
YW
1684 r = link_request_radv_addresses(link);
1685 if (r < 0)
1686 return r;
0017ba31 1687
76c5a0f2
YW
1688 if (link->static_address_messages == 0) {
1689 link->static_addresses_configured = true;
1690 link_check_ready(link);
682c65b0
YW
1691 } else {
1692 log_link_debug(link, "Setting addresses");
1693 link_set_state(link, LINK_STATE_CONFIGURING);
1694 }
1695
1696 return 0;
1697}
1698
e1fc2c43 1699int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
ebd96906 1700 _cleanup_(address_unrefp) Address *tmp = NULL;
16bc8635 1701 struct ifa_cacheinfo cinfo;
a3a25d01 1702 Link *link;
e1fc2c43 1703 uint16_t type;
e1fc2c43 1704 Address *address = NULL;
0a0c2672 1705 Request *req = NULL;
fc35a9f8 1706 bool is_new = false, update_dhcp4;
fe841414 1707 int ifindex, r;
e1fc2c43
YW
1708
1709 assert(rtnl);
1710 assert(message);
1711 assert(m);
1712
1713 if (sd_netlink_message_is_error(message)) {
1714 r = sd_netlink_message_get_errno(message);
1715 if (r < 0)
1716 log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring");
1717
1718 return 0;
1719 }
1720
1721 r = sd_netlink_message_get_type(message, &type);
1722 if (r < 0) {
1723 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1724 return 0;
1725 } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) {
1726 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type);
1727 return 0;
1728 }
1729
1730 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
1731 if (r < 0) {
1732 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
1733 return 0;
1734 } else if (ifindex <= 0) {
1735 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex);
1736 return 0;
1737 }
1738
6eab614d 1739 r = link_get_by_index(m, ifindex, &link);
a3a25d01 1740 if (r < 0) {
e1fc2c43
YW
1741 /* when enumerating we might be out of sync, but we will get the address again, so just
1742 * ignore it */
1743 if (!m->enumerating)
1744 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex);
1745 return 0;
1746 }
1747
fe841414
YW
1748 r = address_new(&tmp);
1749 if (r < 0)
1750 return log_oom();
1751
3c283289
YW
1752 /* First, read minimal information to make address_get() work below. */
1753
fe841414 1754 r = sd_rtnl_message_addr_get_family(message, &tmp->family);
e1fc2c43
YW
1755 if (r < 0) {
1756 log_link_warning(link, "rtnl: received address message without family, ignoring.");
1757 return 0;
fe841414
YW
1758 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1759 log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", tmp->family);
e1fc2c43
YW
1760 return 0;
1761 }
1762
fe841414 1763 r = sd_rtnl_message_addr_get_prefixlen(message, &tmp->prefixlen);
e1fc2c43 1764 if (r < 0) {
9a0ad16b 1765 log_link_warning_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m");
e1fc2c43
YW
1766 return 0;
1767 }
1768
fe841414 1769 switch (tmp->family) {
e1fc2c43 1770 case AF_INET:
fe841414 1771 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &tmp->in_addr.in);
e1fc2c43
YW
1772 if (r < 0) {
1773 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1774 return 0;
1775 }
1776
fe841414
YW
1777 r = sd_netlink_message_read_in_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in);
1778 if (r < 0 && r != -ENODATA) {
1779 log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m");
1780 return 0;
1781 } else if (r >= 0) {
1782 if (in4_addr_equal(&tmp->in_addr.in, &tmp->in_addr_peer.in))
1783 tmp->in_addr_peer = IN_ADDR_NULL;
fe841414
YW
1784 }
1785
e1fc2c43
YW
1786 break;
1787
1788 case AF_INET6:
fe841414
YW
1789 r = sd_netlink_message_read_in6_addr(message, IFA_LOCAL, &tmp->in_addr.in6);
1790 if (r >= 0) {
1791 /* Have peer address. */
1792 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in6);
1793 if (r < 0) {
1794 log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m");
1795 return 0;
1796 }
fe841414
YW
1797 } else if (r == -ENODATA) {
1798 /* Does not have peer address. */
1799 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr.in6);
1800 if (r < 0) {
1801 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1802 return 0;
1803 }
1804 } else {
1805 log_link_warning_errno(link, r, "rtnl: could not get local address from address message, ignoring: %m");
e1fc2c43
YW
1806 return 0;
1807 }
1808
1809 break;
1810
1811 default:
04499a70 1812 assert_not_reached();
e1fc2c43
YW
1813 }
1814
fc35a9f8
YW
1815 update_dhcp4 = tmp->family == AF_INET6;
1816
0a0c2672 1817 /* Then, find the managed Address and Request objects corresponding to the received address. */
fe841414 1818 (void) address_get(link, tmp, &address);
0a0c2672 1819 (void) address_get_request(link, tmp, &req);
e1fc2c43 1820
3c283289 1821 if (type == RTM_DELADDR) {
3b6a3bde 1822 if (address) {
3c283289 1823 address_enter_removed(address);
0a0c2672 1824 log_address_debug(address, "Forgetting removed", link);
3c283289
YW
1825 (void) address_drop(address);
1826 } else
1827 log_address_debug(tmp, "Kernel removed unknown", link);
3b6a3bde 1828
0a0c2672
YW
1829 if (req)
1830 address_enter_removed(req->userdata);
1831
fc35a9f8 1832 goto finalize;
3c283289 1833 }
3b6a3bde 1834
3c283289
YW
1835 if (!address) {
1836 /* If we did not know the address, then save it. */
ebd96906 1837 r = address_attach(link, tmp);
3c283289 1838 if (r < 0) {
e924dc59 1839 log_link_warning_errno(link, r, "Failed to save received address %s, ignoring: %m",
3c283289
YW
1840 IN_ADDR_PREFIX_TO_STRING(tmp->family, &tmp->in_addr, tmp->prefixlen));
1841 return 0;
e1fc2c43 1842 }
ebd96906 1843 address = tmp;
e1fc2c43 1844
3c283289 1845 is_new = true;
e1fc2c43 1846
3c283289
YW
1847 } else {
1848 /* Otherwise, update the managed Address object with the netlink notification. */
1849 address->prefixlen = tmp->prefixlen;
1850 address->in_addr_peer = tmp->in_addr_peer;
1851 }
e1fc2c43 1852
0a0c2672
YW
1853 /* Also update information that cannot be obtained through netlink notification. */
1854 if (req && req->waiting_reply) {
1855 Address *a = ASSERT_PTR(req->userdata);
1856
1857 address->source = a->source;
1858 address->provider = a->provider;
1859 (void) free_and_strdup_warn(&address->netlabel, a->netlabel);
fc289dd0
TM
1860 nft_set_context_clear(&address->nft_set_context);
1861 (void) nft_set_context_dup(&a->nft_set_context, &address->nft_set_context);
0a0c2672
YW
1862 address->requested_as_null = a->requested_as_null;
1863 address->callback = a->callback;
1864 }
1865
3c283289
YW
1866 /* Then, update miscellaneous info. */
1867 r = sd_rtnl_message_addr_get_scope(message, &address->scope);
1868 if (r < 0)
1869 log_link_debug_errno(link, r, "rtnl: received address message without scope, ignoring: %m");
e1fc2c43 1870
3c283289
YW
1871 if (address->family == AF_INET) {
1872 _cleanup_free_ char *label = NULL;
e1fc2c43 1873
3c283289
YW
1874 r = sd_netlink_message_read_string_strdup(message, IFA_LABEL, &label);
1875 if (r >= 0) {
1876 if (!streq_ptr(label, link->ifname))
1877 free_and_replace(address->label, label);
1878 } else if (r != -ENODATA)
1879 log_link_debug_errno(link, r, "rtnl: could not get label from address message, ignoring: %m");
1880
1881 r = sd_netlink_message_read_in_addr(message, IFA_BROADCAST, &address->broadcast);
1882 if (r < 0 && r != -ENODATA)
1883 log_link_debug_errno(link, r, "rtnl: could not get broadcast from address message, ignoring: %m");
e1fc2c43
YW
1884 }
1885
3c283289
YW
1886 r = sd_netlink_message_read_u32(message, IFA_FLAGS, &address->flags);
1887 if (r == -ENODATA) {
1888 unsigned char flags;
1889
1890 /* For old kernels. */
1891 r = sd_rtnl_message_addr_get_flags(message, &flags);
1892 if (r >= 0)
1893 address->flags = flags;
1894 } else if (r < 0)
1895 log_link_debug_errno(link, r, "rtnl: failed to read IFA_FLAGS attribute, ignoring: %m");
1896
1897 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
1898 if (r >= 0)
1899 address_set_lifetime(m, address, &cinfo);
1900 else if (r != -ENODATA)
1901 log_link_debug_errno(link, r, "rtnl: failed to read IFA_CACHEINFO attribute, ignoring: %m");
1902
7e18f9b4
YW
1903 r = sd_netlink_message_read_u32(message, IFA_RT_PRIORITY, &address->route_metric);
1904 if (r < 0 && r != -ENODATA)
1905 log_link_debug_errno(link, r, "rtnl: failed to read IFA_RT_PRIORITY attribute, ignoring: %m");
1906
3c283289 1907 address_enter_configured(address);
0a0c2672
YW
1908 if (req)
1909 address_enter_configured(req->userdata);
1910
3c283289
YW
1911 log_address_debug(address, is_new ? "Received new": "Received updated", link);
1912
1913 /* address_update() logs internally, so we don't need to here. */
1914 r = address_update(address);
1915 if (r < 0)
1916 link_enter_failed(link);
1917
fc35a9f8
YW
1918finalize:
1919 if (update_dhcp4) {
1920 r = dhcp4_update_ipv6_connectivity(link);
1921 if (r < 0) {
1922 log_link_warning_errno(link, r, "Failed to notify IPv6 connectivity to DHCPv4 client: %m");
1923 link_enter_failed(link);
1924 }
1925 }
1926
e1fc2c43
YW
1927 return 1;
1928}
1929
44e7b949
LP
1930int config_parse_broadcast(
1931 const char *unit,
eb0ea358
TG
1932 const char *filename,
1933 unsigned line,
1934 const char *section,
1935 unsigned section_line,
1936 const char *lvalue,
1937 int ltype,
1938 const char *rvalue,
1939 void *data,
1940 void *userdata) {
44e7b949 1941
eb0ea358 1942 Network *network = userdata;
ebd96906 1943 _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
94af46fc 1944 union in_addr_union u;
eb0ea358
TG
1945 int r;
1946
1947 assert(filename);
1948 assert(section);
1949 assert(lvalue);
1950 assert(rvalue);
1951 assert(data);
1952
f4859fc7 1953 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
1954 if (r == -ENOMEM)
1955 return log_oom();
1956 if (r < 0) {
1957 log_syntax(unit, LOG_WARNING, filename, line, r,
1958 "Failed to allocate new address, ignoring assignment: %m");
1959 return 0;
1960 }
eb0ea358 1961
832583ad
YW
1962 if (isempty(rvalue)) {
1963 /* The broadcast address will be calculated based on Address=, and set if the link is
1964 * not a wireguard interface. Here, we do not check or set n->family. */
1965 n->broadcast = (struct in_addr) {};
1966 n->set_broadcast = -1;
1967 TAKE_PTR(n);
1968 return 0;
1969 }
1970
1971 r = parse_boolean(rvalue);
1972 if (r >= 0) {
1973 /* The broadcast address will be calculated based on Address=. Here, we do not check or
1974 * set n->family. */
1975 n->broadcast = (struct in_addr) {};
1976 n->set_broadcast = r;
1977 TAKE_PTR(n);
1978 return 0;
1979 }
1980
482e2ac1 1981 if (n->family == AF_INET6) {
d96edb2c 1982 log_syntax(unit, LOG_WARNING, filename, line, 0,
2850cd40 1983 "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
482e2ac1
TG
1984 return 0;
1985 }
1986
94af46fc 1987 r = in_addr_from_string(AF_INET, rvalue, &u);
eb0ea358 1988 if (r < 0) {
d96edb2c 1989 log_syntax(unit, LOG_WARNING, filename, line, r,
2850cd40 1990 "Broadcast is invalid, ignoring assignment: %s", rvalue);
eb0ea358
TG
1991 return 0;
1992 }
832583ad
YW
1993 if (in4_addr_is_null(&u.in)) {
1994 log_syntax(unit, LOG_WARNING, filename, line, 0,
1995 "Broadcast cannot be ANY address, ignoring assignment: %s", rvalue);
1996 return 0;
1997 }
eb0ea358 1998
94af46fc 1999 n->broadcast = u.in;
832583ad 2000 n->set_broadcast = true;
44e7b949 2001 n->family = AF_INET;
dea161d9 2002 TAKE_PTR(n);
eb0ea358
TG
2003
2004 return 0;
2005}
2006
f5ee7d74
YW
2007int config_parse_address(
2008 const char *unit,
f579559b
TG
2009 const char *filename,
2010 unsigned line,
2011 const char *section,
71a61510 2012 unsigned section_line,
f579559b
TG
2013 const char *lvalue,
2014 int ltype,
2015 const char *rvalue,
2016 void *data,
2017 void *userdata) {
44e7b949 2018
6ae115c1 2019 Network *network = userdata;
ebd96906 2020 _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
44e7b949 2021 union in_addr_union buffer;
b7cb4452 2022 unsigned char prefixlen;
44e7b949 2023 int r, f;
f579559b
TG
2024
2025 assert(filename);
6ae115c1 2026 assert(section);
f579559b
TG
2027 assert(lvalue);
2028 assert(rvalue);
2029 assert(data);
2030
a61738b3
YW
2031 if (streq(section, "Network")) {
2032 if (isempty(rvalue)) {
2033 /* If an empty string specified in [Network] section, clear previously assigned addresses. */
ebd96906 2034 network->addresses_by_section = ordered_hashmap_free(network->addresses_by_section);
a61738b3
YW
2035 return 0;
2036 }
2037
9cd9fc8f
YW
2038 /* we are not in an Address section, so use line number instead. */
2039 r = address_new_static(network, filename, line, &n);
a61738b3 2040 } else
f4859fc7 2041 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
2042 if (r == -ENOMEM)
2043 return log_oom();
2044 if (r < 0) {
2045 log_syntax(unit, LOG_WARNING, filename, line, r,
2046 "Failed to allocate new address, ignoring assignment: %m");
2047 return 0;
2048 }
f579559b
TG
2049
2050 /* Address=address/prefixlen */
0f707207
YW
2051 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_REFUSE, &f, &buffer, &prefixlen);
2052 if (r == -ENOANO) {
e5e07431
YW
2053 r = in_addr_prefix_from_string_auto(rvalue, &f, &buffer, &prefixlen);
2054 if (r >= 0)
2055 log_syntax(unit, LOG_WARNING, filename, line, r,
c5874748 2056 "Address '%s' is specified without prefix length. Assuming the prefix length is %u. "
e5e07431 2057 "Please specify the prefix length explicitly.", rvalue, prefixlen);
0f707207 2058 }
f579559b 2059 if (r < 0) {
d96edb2c 2060 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue);
f579559b
TG
2061 return 0;
2062 }
2063
44e7b949 2064 if (n->family != AF_UNSPEC && f != n->family) {
d96edb2c 2065 log_syntax(unit, LOG_WARNING, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue);
44e7b949
LP
2066 return 0;
2067 }
2068
c9207ff3
YW
2069 if (in_addr_is_null(f, &buffer)) {
2070 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
2071 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
2072 * let's limit the prefix length to 64 or larger. See RFC4193. */
2073 if ((f == AF_INET && prefixlen < 8) ||
2074 (f == AF_INET6 && prefixlen < 64)) {
d96edb2c 2075 log_syntax(unit, LOG_WARNING, filename, line, 0,
c9207ff3
YW
2076 "Null address with invalid prefixlen='%u', ignoring assignment: %s",
2077 prefixlen, rvalue);
2078 return 0;
2079 }
2080 }
2081
44e7b949 2082 n->family = f;
b7cb4452 2083 n->prefixlen = prefixlen;
44e7b949 2084
d0009d92 2085 if (streq(lvalue, "Address")) {
44e7b949 2086 n->in_addr = buffer;
d0009d92
YW
2087 n->requested_as_null = !in_addr_is_set(n->family, &n->in_addr);
2088 } else
44e7b949
LP
2089 n->in_addr_peer = buffer;
2090
dea161d9 2091 TAKE_PTR(n);
f579559b
TG
2092 return 0;
2093}
6ae115c1 2094
d31645ad
LP
2095int config_parse_label(
2096 const char *unit,
6ae115c1
TG
2097 const char *filename,
2098 unsigned line,
2099 const char *section,
2100 unsigned section_line,
2101 const char *lvalue,
2102 int ltype,
2103 const char *rvalue,
2104 void *data,
2105 void *userdata) {
d31645ad 2106
ebd96906 2107 _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
d31645ad 2108 Network *network = userdata;
6ae115c1
TG
2109 int r;
2110
2111 assert(filename);
2112 assert(section);
2113 assert(lvalue);
2114 assert(rvalue);
2115 assert(data);
2116
f4859fc7 2117 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
2118 if (r == -ENOMEM)
2119 return log_oom();
2120 if (r < 0) {
2121 log_syntax(unit, LOG_WARNING, filename, line, r,
2122 "Failed to allocate new address, ignoring assignment: %m");
2123 return 0;
2124 }
6ae115c1 2125
b8e898a6
YW
2126 if (isempty(rvalue)) {
2127 n->label = mfree(n->label);
2128 TAKE_PTR(n);
2129 return 0;
2130 }
2131
a87d19fe 2132 if (!address_label_valid(rvalue)) {
d96edb2c 2133 log_syntax(unit, LOG_WARNING, filename, line, 0,
2850cd40 2134 "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
6ae115c1
TG
2135 return 0;
2136 }
2137
d31645ad
LP
2138 r = free_and_strdup(&n->label, rvalue);
2139 if (r < 0)
2140 return log_oom();
6ae115c1 2141
dea161d9 2142 TAKE_PTR(n);
6ae115c1
TG
2143 return 0;
2144}
ce6c77eb 2145
f5ee7d74
YW
2146int config_parse_lifetime(
2147 const char *unit,
2148 const char *filename,
2149 unsigned line,
2150 const char *section,
2151 unsigned section_line,
2152 const char *lvalue,
2153 int ltype,
2154 const char *rvalue,
2155 void *data,
2156 void *userdata) {
2157
b5834a0b 2158 Network *network = userdata;
ebd96906 2159 _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
16bc8635 2160 usec_t k;
b5834a0b
SS
2161 int r;
2162
2163 assert(filename);
2164 assert(section);
2165 assert(lvalue);
2166 assert(rvalue);
2167 assert(data);
2168
f4859fc7 2169 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
2170 if (r == -ENOMEM)
2171 return log_oom();
2172 if (r < 0) {
2173 log_syntax(unit, LOG_WARNING, filename, line, r,
2174 "Failed to allocate new address, ignoring assignment: %m");
2175 return 0;
2176 }
b5834a0b 2177
10b20e5a
ZJS
2178 /* We accept only "forever", "infinity", empty, or "0". */
2179 if (STR_IN_SET(rvalue, "forever", "infinity", ""))
16bc8635 2180 k = USEC_INFINITY;
33680b0a
YW
2181 else if (streq(rvalue, "0"))
2182 k = 0;
2183 else {
d96edb2c 2184 log_syntax(unit, LOG_WARNING, filename, line, 0,
33680b0a 2185 "Invalid PreferredLifetime= value, ignoring: %s", rvalue);
b5834a0b
SS
2186 return 0;
2187 }
2188
16bc8635 2189 n->lifetime_preferred_usec = k;
d2735796 2190 TAKE_PTR(n);
b5834a0b
SS
2191
2192 return 0;
2193}
2194
f5ee7d74
YW
2195int config_parse_address_flags(
2196 const char *unit,
2197 const char *filename,
2198 unsigned line,
2199 const char *section,
2200 unsigned section_line,
2201 const char *lvalue,
2202 int ltype,
2203 const char *rvalue,
2204 void *data,
2205 void *userdata) {
2206
e63be084 2207 Network *network = userdata;
ebd96906 2208 _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
e63be084
SS
2209 int r;
2210
2211 assert(filename);
2212 assert(section);
2213 assert(lvalue);
2214 assert(rvalue);
2215 assert(data);
2216
f4859fc7 2217 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
2218 if (r == -ENOMEM)
2219 return log_oom();
2220 if (r < 0) {
2221 log_syntax(unit, LOG_WARNING, filename, line, r,
2222 "Failed to allocate new address, ignoring assignment: %m");
2223 return 0;
2224 }
e63be084
SS
2225
2226 r = parse_boolean(rvalue);
2227 if (r < 0) {
d96edb2c 2228 log_syntax(unit, LOG_WARNING, filename, line, r,
051e77ca 2229 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
e63be084
SS
2230 return 0;
2231 }
2232
eaff204f
YW
2233 if (streq(lvalue, "AddPrefixRoute"))
2234 r = !r;
2235
2236 SET_FLAG(n->flags, ltype, r);
e63be084 2237
dea161d9 2238 TAKE_PTR(n);
e63be084
SS
2239 return 0;
2240}
2241
f5ee7d74
YW
2242int config_parse_address_scope(
2243 const char *unit,
2244 const char *filename,
2245 unsigned line,
2246 const char *section,
2247 unsigned section_line,
2248 const char *lvalue,
2249 int ltype,
2250 const char *rvalue,
2251 void *data,
2252 void *userdata) {
2253
2959fb07 2254 Network *network = userdata;
ebd96906 2255 _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
2959fb07
SS
2256 int r;
2257
2258 assert(filename);
2259 assert(section);
2260 assert(lvalue);
2261 assert(rvalue);
2262 assert(data);
2263
2264 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
2265 if (r == -ENOMEM)
2266 return log_oom();
2267 if (r < 0) {
2268 log_syntax(unit, LOG_WARNING, filename, line, r,
2269 "Failed to allocate new address, ignoring assignment: %m");
2270 return 0;
2271 }
2959fb07 2272
d8c472f2
YW
2273 r = route_scope_from_string(rvalue);
2274 if (r < 0) {
2275 log_syntax(unit, LOG_WARNING, filename, line, r,
2276 "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue);
2277 return 0;
2959fb07
SS
2278 }
2279
d8c472f2 2280 n->scope = r;
07336a06 2281 n->scope_set = true;
dea161d9 2282 TAKE_PTR(n);
2959fb07
SS
2283 return 0;
2284}
2285
c4ff0629
YW
2286int config_parse_address_route_metric(
2287 const char *unit,
2288 const char *filename,
2289 unsigned line,
2290 const char *section,
2291 unsigned section_line,
2292 const char *lvalue,
2293 int ltype,
2294 const char *rvalue,
2295 void *data,
2296 void *userdata) {
2297
2298 Network *network = userdata;
ebd96906 2299 _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
c4ff0629
YW
2300 int r;
2301
2302 assert(filename);
2303 assert(section);
2304 assert(lvalue);
2305 assert(rvalue);
2306 assert(data);
2307
2308 r = address_new_static(network, filename, section_line, &n);
2309 if (r == -ENOMEM)
2310 return log_oom();
2311 if (r < 0) {
2312 log_syntax(unit, LOG_WARNING, filename, line, r,
2313 "Failed to allocate new address, ignoring assignment: %m");
2314 return 0;
2315 }
2316
2317 r = safe_atou32(rvalue, &n->route_metric);
2318 if (r < 0) {
2319 log_syntax(unit, LOG_WARNING, filename, line, r,
2320 "Could not parse %s=, ignoring assignment: %s", lvalue, rvalue);
2321 return 0;
2322 }
2323
2324 TAKE_PTR(n);
2325 return 0;
2326}
2327
051e77ca
SS
2328int config_parse_duplicate_address_detection(
2329 const char *unit,
2330 const char *filename,
2331 unsigned line,
2332 const char *section,
2333 unsigned section_line,
2334 const char *lvalue,
2335 int ltype,
2336 const char *rvalue,
2337 void *data,
2338 void *userdata) {
f5ee7d74 2339
051e77ca 2340 Network *network = userdata;
ebd96906 2341 _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
051e77ca
SS
2342 int r;
2343
2344 assert(filename);
2345 assert(section);
2346 assert(lvalue);
2347 assert(rvalue);
2348 assert(data);
2349
2350 r = address_new_static(network, filename, section_line, &n);
d96edb2c
YW
2351 if (r == -ENOMEM)
2352 return log_oom();
2353 if (r < 0) {
2354 log_syntax(unit, LOG_WARNING, filename, line, r,
2355 "Failed to allocate new address, ignoring assignment: %m");
2356 return 0;
2357 }
051e77ca
SS
2358
2359 r = parse_boolean(rvalue);
2360 if (r >= 0) {
2361 log_syntax(unit, LOG_WARNING, filename, line, 0,
2362 "For historical reasons, %s=%s means %s=%s. "
2363 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
2364 lvalue, rvalue, lvalue, r ? "none" : "both");
2365 n->duplicate_address_detection = r ? ADDRESS_FAMILY_NO : ADDRESS_FAMILY_YES;
2366 n = NULL;
2367 return 0;
2368 }
2369
7211c853 2370 AddressFamily a = duplicate_address_detection_address_family_from_string(rvalue);
051e77ca 2371 if (a < 0) {
7211c853 2372 log_syntax(unit, LOG_WARNING, filename, line, a,
051e77ca
SS
2373 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
2374 return 0;
2375 }
051e77ca 2376 n->duplicate_address_detection = a;
7211c853 2377
dea161d9 2378 TAKE_PTR(n);
051e77ca
SS
2379 return 0;
2380}
2381
4b3590c3
TM
2382int config_parse_address_netlabel(
2383 const char *unit,
2384 const char *filename,
2385 unsigned line,
2386 const char *section,
2387 unsigned section_line,
2388 const char *lvalue,
2389 int ltype,
2390 const char *rvalue,
2391 void *data,
2392 void *userdata) {
2393
2394 Network *network = userdata;
ebd96906 2395 _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
4b3590c3
TM
2396 int r;
2397
2398 assert(filename);
2399 assert(section);
2400 assert(lvalue);
2401 assert(rvalue);
2402 assert(data);
2403 assert(network);
2404
2405 r = address_new_static(network, filename, section_line, &n);
2406 if (r == -ENOMEM)
2407 return log_oom();
2408 if (r < 0) {
2409 log_syntax(unit, LOG_WARNING, filename, line, r,
2410 "Failed to allocate new address, ignoring assignment: %m");
2411 return 0;
2412 }
2413
2414 r = config_parse_string(unit, filename, line, section, section_line,
2415 lvalue, CONFIG_PARSE_STRING_SAFE, rvalue, &n->netlabel, network);
2416 if (r < 0)
2417 return r;
2418
2419 TAKE_PTR(n);
2420 return 0;
2421}
2422
9a45125a
YW
2423static void address_section_adjust_broadcast(Address *address) {
2424 assert(address);
2425 assert(address->section);
2426
2427 if (!in4_addr_is_set(&address->broadcast))
2428 return;
2429
2430 if (address->family == AF_INET6)
2431 log_warning("%s: broadcast address is set for an IPv6 address. "
2432 "Ignoring Broadcast= setting in the [Address] section from line %u.",
2433 address->section->filename, address->section->line);
2434 else if (address->prefixlen > 30)
2435 log_warning("%s: broadcast address is set for an IPv4 address with prefix length larger than 30. "
2436 "Ignoring Broadcast= setting in the [Address] section from line %u.",
2437 address->section->filename, address->section->line);
2438 else if (in4_addr_is_set(&address->in_addr_peer.in))
2439 log_warning("%s: broadcast address is set for an IPv4 address with peer address. "
2440 "Ignoring Broadcast= setting in the [Address] section from line %u.",
2441 address->section->filename, address->section->line);
0acc6470
YW
2442 else if (!in4_addr_is_set(&address->in_addr.in))
2443 log_warning("%s: broadcast address is set for an IPv4 address with null address. "
2444 "Ignoring Broadcast= setting in the [Address] section from line %u.",
2445 address->section->filename, address->section->line);
9a45125a
YW
2446 else
2447 /* Otherwise, keep the specified broadcast address. */
2448 return;
2449
2450 address->broadcast.s_addr = 0;
2451}
2452
5459e11d 2453int address_section_verify(Address *address) {
fcbf4cb7
YW
2454 if (section_is_invalid(address->section))
2455 return -EINVAL;
2456
2457 if (address->family == AF_UNSPEC) {
2458 assert(address->section);
2459
2460 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
d9e2afc0 2461 "%s: Address section without Address= field was configured. "
fcbf4cb7
YW
2462 "Ignoring [Address] section from line %u.",
2463 address->section->filename, address->section->line);
2464 }
2465
d9e2afc0
YW
2466 if (address->family == AF_INET6 && !socket_ipv6_is_supported())
2467 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
2468 "%s: an IPv6 address was configured, but the kernel does not support IPv6. "
2469 "Ignoring [Address] section from line %u.",
2470 address->section->filename, address->section->line);
2471
2859932b
YW
2472 assert(IN_SET(address->family, AF_INET, AF_INET6));
2473
9a45125a 2474 address_section_adjust_broadcast(address);
fe841414
YW
2475
2476 if (address->family == AF_INET6 && address->label) {
2477 log_warning("%s: address label is set for IPv6 address in the [Address] section from line %u. "
2478 "Ignoring Label= setting.",
2479 address->section->filename, address->section->line);
2480
2481 address->label = mfree(address->label);
2482 }
2483
20228b6d
YW
2484 if (!address->scope_set) {
2485 if (in_addr_is_localhost(address->family, &address->in_addr) > 0)
2486 address->scope = RT_SCOPE_HOST;
2487 else if (in_addr_is_link_local(address->family, &address->in_addr) > 0)
2488 address->scope = RT_SCOPE_LINK;
2489 }
07336a06 2490
2859932b
YW
2491 if (address->duplicate_address_detection < 0) {
2492 if (address->family == AF_INET6)
2493 address->duplicate_address_detection = ADDRESS_FAMILY_IPV6;
2494 else if (in4_addr_is_link_local(&address->in_addr.in))
2495 address->duplicate_address_detection = ADDRESS_FAMILY_IPV4;
2496 else
2497 address->duplicate_address_detection = ADDRESS_FAMILY_NO;
2498 } else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV6 && address->family == AF_INET)
2499 log_warning("%s: DuplicateAddressDetection=ipv6 is specified for IPv4 address, ignoring.",
2500 address->section->filename);
2501 else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV4 && address->family == AF_INET6)
2502 log_warning("%s: DuplicateAddressDetection=ipv4 is specified for IPv6 address, ignoring.",
2503 address->section->filename);
2504
bd5146c6
YW
2505 if (address->family == AF_INET6 &&
2506 !FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV6))
eaff204f
YW
2507 address->flags |= IFA_F_NODAD;
2508
b1179a5d
YW
2509 uint32_t filtered_flags = address->family == AF_INET ?
2510 address->flags & KNOWN_FLAGS & ~UNMANAGED_FLAGS & ~IPV6ONLY_FLAGS :
2511 address->flags & KNOWN_FLAGS & ~UNMANAGED_FLAGS;
2512 if (address->flags != filtered_flags) {
2513 _cleanup_free_ char *str = NULL;
2514
b7d43592 2515 (void) address_flags_to_string_alloc(address->flags ^ filtered_flags, address->family, &str);
b1179a5d
YW
2516 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
2517 "%s: unexpected address flags \"%s\" were configured. "
2518 "Ignoring [Address] section from line %u.",
2519 address->section->filename, strna(str), address->section->line);
2520 }
2521
fcbf4cb7
YW
2522 return 0;
2523}
32400c2f 2524
50783f91
YW
2525int network_drop_invalid_addresses(Network *network) {
2526 _cleanup_set_free_ Set *addresses = NULL;
9cd9fc8f 2527 Address *address;
50783f91 2528 int r;
32400c2f
YW
2529
2530 assert(network);
2531
50783f91
YW
2532 ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section) {
2533 Address *dup;
2534
2535 if (address_section_verify(address) < 0) {
2536 /* Drop invalid [Address] sections or Address= settings in [Network].
ebd96906
YW
2537 * Note that address_detach() will drop the address from addresses_by_section. */
2538 address_detach(address);
50783f91
YW
2539 continue;
2540 }
2541
2542 /* Always use the setting specified later. So, remove the previously assigned setting. */
2543 dup = set_remove(addresses, address);
2544 if (dup) {
50783f91
YW
2545 log_warning("%s: Duplicated address %s is specified at line %u and %u, "
2546 "dropping the address setting specified at line %u.",
c71384a9
ZJS
2547 dup->section->filename,
2548 IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen),
2549 address->section->line,
50783f91 2550 dup->section->line, dup->section->line);
ebd96906
YW
2551
2552 /* address_detach() will drop the address from addresses_by_section. */
2553 address_detach(dup);
50783f91
YW
2554 }
2555
ebd96906
YW
2556 /* Use address_hash_ops, instead of address_hash_ops_detach. Otherwise, the Address objects
2557 * will be detached. */
4da252c5 2558 r = set_ensure_put(&addresses, &address_hash_ops, address);
50783f91
YW
2559 if (r < 0)
2560 return log_oom();
2561 assert(r > 0);
2562 }
2563
5459e11d
YW
2564 r = network_adjust_dhcp_server(network, &addresses);
2565 if (r < 0)
2566 return r;
26f88471 2567
50783f91 2568 return 0;
32400c2f 2569}
fc289dd0
TM
2570
2571int config_parse_address_ip_nft_set(
2572 const char *unit,
2573 const char *filename,
2574 unsigned line,
2575 const char *section,
2576 unsigned section_line,
2577 const char *lvalue,
2578 int ltype,
2579 const char *rvalue,
2580 void *data,
2581 void *userdata) {
2582
2583 Network *network = userdata;
ebd96906 2584 _cleanup_(address_unref_or_set_invalidp) Address *n = NULL;
fc289dd0
TM
2585 int r;
2586
2587 assert(filename);
2588 assert(lvalue);
2589 assert(rvalue);
2590 assert(network);
2591
2592 r = address_new_static(network, filename, section_line, &n);
2593 if (r == -ENOMEM)
2594 return log_oom();
2595 if (r < 0) {
2596 log_syntax(unit, LOG_WARNING, filename, line, r,
2597 "Failed to allocate a new address, ignoring assignment: %m");
2598 return 0;
2599 }
2600
2601 r = config_parse_nft_set(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &n->nft_set_context, network);
2602 if (r < 0)
2603 return r;
2604
2605 TAKE_PTR(n);
2606 return 0;
2607}