Coverage Report

Created: 2026-10-28 06:53

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