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