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