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