• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 13935887515

18 Mar 2025 07:10PM UTC coverage: 71.913% (-0.03%) from 71.946%
13935887515

push

github

web-flow
Several fixes and cleanups around sd_listen_fds() (#36788)

15 of 24 new or added lines in 5 files covered. (62.5%)

993 existing lines in 54 files now uncovered.

296157 of 411825 relevant lines covered (71.91%)

710024.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.18
/src/network/networkd-link.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
/* Make sure the net/if.h header is included before any linux/ one */
4
#include <net/if.h>
5
#include <netinet/in.h>
6
#include <linux/if.h>
7
#include <linux/if_arp.h>
8
#include <linux/if_link.h>
9
#include <linux/netdevice.h>
10
#include <sys/socket.h>
11
#include <unistd.h>
12

13
#include "alloc-util.h"
14
#include "arphrd-util.h"
15
#include "batadv.h"
16
#include "bitfield.h"
17
#include "bond.h"
18
#include "bridge.h"
19
#include "bus-util.h"
20
#include "device-private.h"
21
#include "device-util.h"
22
#include "dhcp-lease-internal.h"
23
#include "env-file.h"
24
#include "ethtool-util.h"
25
#include "event-util.h"
26
#include "fd-util.h"
27
#include "fileio.h"
28
#include "format-ifname.h"
29
#include "fs-util.h"
30
#include "glyph-util.h"
31
#include "logarithm.h"
32
#include "missing_network.h"
33
#include "netlink-util.h"
34
#include "network-internal.h"
35
#include "networkd-address-label.h"
36
#include "networkd-address.h"
37
#include "networkd-bridge-fdb.h"
38
#include "networkd-bridge-mdb.h"
39
#include "networkd-bridge-vlan.h"
40
#include "networkd-can.h"
41
#include "networkd-dhcp-prefix-delegation.h"
42
#include "networkd-dhcp-server.h"
43
#include "networkd-dhcp4.h"
44
#include "networkd-dhcp6.h"
45
#include "networkd-ipv4acd.h"
46
#include "networkd-ipv4ll.h"
47
#include "networkd-ipv6-proxy-ndp.h"
48
#include "networkd-link-bus.h"
49
#include "networkd-link.h"
50
#include "networkd-lldp-tx.h"
51
#include "networkd-manager.h"
52
#include "networkd-ndisc.h"
53
#include "networkd-neighbor.h"
54
#include "networkd-nexthop.h"
55
#include "networkd-queue.h"
56
#include "networkd-radv.h"
57
#include "networkd-route-util.h"
58
#include "networkd-route.h"
59
#include "networkd-routing-policy-rule.h"
60
#include "networkd-setlink.h"
61
#include "networkd-sriov.h"
62
#include "networkd-state-file.h"
63
#include "networkd-sysctl.h"
64
#include "networkd-wifi.h"
65
#include "set.h"
66
#include "socket-util.h"
67
#include "stdio-util.h"
68
#include "string-table.h"
69
#include "strv.h"
70
#include "tc.h"
71
#include "tmpfile-util.h"
72
#include "tuntap.h"
73
#include "udev-util.h"
74
#include "vrf.h"
75

76
void link_required_operstate_for_online(Link *link, LinkOperationalStateRange *ret) {
16,984✔
77
        assert(link);
16,984✔
78
        assert(ret);
16,984✔
79

80
        if (link->network && operational_state_range_is_valid(&link->network->required_operstate_for_online))
16,984✔
81
                /* If explicitly specified, use it as is. */
82
                *ret = link->network->required_operstate_for_online;
267✔
83
        else if (link->iftype == ARPHRD_CAN)
16,717✔
84
                /* CAN devices do not support addressing, hence defaults to 'carrier'. */
85
                *ret = (const LinkOperationalStateRange) {
44✔
86
                        .min = LINK_OPERSTATE_CARRIER,
87
                        .max = LINK_OPERSTATE_CARRIER,
88
                };
89
        else if (link->network && link->network->bond)
16,673✔
90
                /* Bonding slaves do not support addressing. */
91
                *ret = (const LinkOperationalStateRange) {
112✔
92
                        .min = LINK_OPERSTATE_ENSLAVED,
93
                        .max = LINK_OPERSTATE_ENSLAVED,
94
                };
95
        else if (STRPTR_IN_SET(link->kind, "batadv", "bond", "bridge", "vrf"))
16,561✔
96
                /* Some of slave interfaces may be offline. */
97
                *ret = (const LinkOperationalStateRange) {
648✔
98
                        .min = LINK_OPERSTATE_DEGRADED_CARRIER,
99
                        .max = LINK_OPERSTATE_ROUTABLE,
100
                };
101
        else
102
                *ret = LINK_OPERSTATE_RANGE_DEFAULT;
15,913✔
103
}
16,984✔
104

105
AddressFamily link_required_family_for_online(Link *link) {
8,887✔
106
        assert(link);
8,887✔
107

108
        if (link->network && link->network->required_family_for_online >= 0)
8,887✔
109
                return link->network->required_family_for_online;
110

111
        if (link->network && operational_state_range_is_valid(&link->network->required_operstate_for_online))
×
112
                /* If RequiredForOnline= is explicitly specified, defaults to no. */
113
                return ADDRESS_FAMILY_NO;
114

115
        if (STRPTR_IN_SET(link->kind, "batadv", "bond", "bridge", "vrf"))
×
116
                /* As the minimum required operstate for master interfaces is 'degraded-carrier',
117
                 * we should request an address assigned to the link for backward compatibility. */
118
                return ADDRESS_FAMILY_YES;
×
119

120
        return ADDRESS_FAMILY_NO;
121
}
122

123
bool link_ipv6_enabled(Link *link) {
802✔
124
        assert(link);
802✔
125

126
        if (!socket_ipv6_is_supported())
802✔
127
                return false;
128

129
        if (link->iftype == ARPHRD_CAN)
802✔
130
                return false;
131

132
        if (!link->network)
800✔
133
                return false;
134

135
        if (link->network->bond)
800✔
136
                return false;
137

138
        if (link_may_have_ipv6ll(link, /* check_multicast = */ false))
791✔
139
                return true;
140

141
        if (network_has_static_ipv6_configurations(link->network))
83✔
142
                return true;
22✔
143

144
        return false;
145
}
146

147
bool link_has_ipv6_connectivity(Link *link) {
267✔
148
        LinkAddressState ipv6_address_state;
267✔
149

150
        assert(link);
267✔
151

152
        link_get_address_states(link, NULL, &ipv6_address_state, NULL);
267✔
153

154
        switch (ipv6_address_state) {
267✔
155
        case LINK_ADDRESS_STATE_ROUTABLE:
156
                /* If the interface has a routable IPv6 address, then we assume yes. */
157
                return true;
267✔
158

159
        case LINK_ADDRESS_STATE_DEGRADED:
160
                /* If the interface has only degraded IPv6 address (mostly, link-local address), then let's check
161
                 * there is an IPv6 default gateway. */
162
                return link_has_default_gateway(link, AF_INET6);
117✔
163

164
        case LINK_ADDRESS_STATE_OFF:
102✔
165
                /* No IPv6 address. */
166
                return false;
102✔
167

168
        default:
×
169
                assert_not_reached();
×
170
        }
171
}
172

173
static bool link_is_ready_to_configure_one(Link *link, bool allow_unmanaged) {
56,560✔
174
        assert(link);
56,560✔
175

176
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
56,560✔
177
                return false;
178

179
        if (!link->network)
56,523✔
180
                return allow_unmanaged;
181

182
        if (!link->network->configure_without_carrier) {
56,522✔
183
                if (link->set_flags_messages > 0)
56,334✔
184
                        return false;
185

186
                if (!link_has_carrier(link))
14,770✔
187
                        return false;
188
        }
189

190
        if (link->set_link_messages > 0)
11,462✔
191
                return false;
192

193
        if (!link->activated)
11,333✔
194
                return false;
66✔
195

196
        return true;
197
}
198

199
bool link_is_ready_to_configure(Link *link, bool allow_unmanaged) {
56,560✔
200
        return check_ready_for_all_sr_iov_ports(link, allow_unmanaged, link_is_ready_to_configure_one);
56,560✔
201
}
202

203
bool link_is_ready_to_configure_by_name(Manager *manager, const char *name, bool allow_unmanaged) {
1,254✔
204
        assert(manager);
1,254✔
205
        assert(name);
1,254✔
206

207
        Link *link;
1,254✔
208
        if (link_get_by_name(manager, name, &link) < 0)
1,254✔
209
                return false;
1,254✔
210

211
        return link_is_ready_to_configure(link, allow_unmanaged);
1,254✔
212
}
213

214
void link_ntp_settings_clear(Link *link) {
2,580✔
215
        link->ntp = strv_free(link->ntp);
2,580✔
216
}
2,580✔
217

218
void link_dns_settings_clear(Link *link) {
2,593✔
219
        if (link->n_dns != UINT_MAX)
2,593✔
220
                for (unsigned i = 0; i < link->n_dns; i++)
9✔
221
                        in_addr_full_free(link->dns[i]);
6✔
222
        link->dns = mfree(link->dns);
2,593✔
223
        link->n_dns = UINT_MAX;
2,593✔
224

225
        link->search_domains = ordered_set_free(link->search_domains);
2,593✔
226
        link->route_domains = ordered_set_free(link->route_domains);
2,593✔
227

228
        link->dns_default_route = -1;
2,593✔
229
        link->llmnr = _RESOLVE_SUPPORT_INVALID;
2,593✔
230
        link->mdns = _RESOLVE_SUPPORT_INVALID;
2,593✔
231
        link->dnssec_mode = _DNSSEC_MODE_INVALID;
2,593✔
232
        link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID;
2,593✔
233

234
        link->dnssec_negative_trust_anchors = set_free_free(link->dnssec_negative_trust_anchors);
2,593✔
235
}
2,593✔
236

237
static void link_free_engines(Link *link) {
4,442✔
238
        if (!link)
4,442✔
239
                return;
240

241
        link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
4,442✔
242

243
        link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
4,442✔
244
        link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
4,442✔
245
        link->dhcp4_6rd_tunnel_name = mfree(link->dhcp4_6rd_tunnel_name);
4,442✔
246

247
        link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx);
4,442✔
248
        link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
4,442✔
249

250
        link->ipv4acd_by_address = hashmap_free(link->ipv4acd_by_address);
4,442✔
251

252
        link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
4,442✔
253

254
        link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
4,442✔
255
        link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
4,442✔
256

257
        link->ndisc = sd_ndisc_unref(link->ndisc);
4,442✔
258
        link->ndisc_expire = sd_event_source_disable_unref(link->ndisc_expire);
4,442✔
259
        ndisc_flush(link);
4,442✔
260

261
        link->radv = sd_radv_unref(link->radv);
4,442✔
262
}
263

264
static Link *link_free(Link *link) {
2,579✔
265
        assert(link);
2,579✔
266

267
        (void) link_clear_sysctl_shadows(link);
2,579✔
268

269
        link_ntp_settings_clear(link);
2,579✔
270
        link_dns_settings_clear(link);
2,579✔
271

272
        link->neighbors = set_free(link->neighbors);
2,579✔
273
        link->addresses = set_free(link->addresses);
2,579✔
274
        link->qdiscs = set_free(link->qdiscs);
2,579✔
275
        link->tclasses = set_free(link->tclasses);
2,579✔
276

277
        link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
2,579✔
278

279
        link_free_engines(link);
2,579✔
280

281
        set_free(link->sr_iov_virt_port_ifindices);
2,579✔
282
        free(link->ifname);
2,579✔
283
        strv_free(link->alternative_names);
2,579✔
284
        free(link->kind);
2,579✔
285
        free(link->ssid);
2,579✔
286
        free(link->previous_ssid);
2,579✔
287
        free(link->driver);
2,579✔
288

289
        unlink_and_free(link->lease_file);
2,579✔
290
        unlink_and_free(link->state_file);
2,579✔
291

292
        sd_device_unref(link->dev);
2,579✔
293
        netdev_unref(link->netdev);
2,579✔
294

295
        hashmap_free(link->bound_to_links);
2,579✔
296
        hashmap_free(link->bound_by_links);
2,579✔
297

298
        set_free_with_destructor(link->slaves, link_unref);
2,586✔
299

300
        network_unref(link->network);
2,579✔
301

302
        sd_event_source_disable_unref(link->carrier_lost_timer);
2,579✔
303
        sd_event_source_disable_unref(link->ipv6_mtu_wait_synced_event_source);
2,579✔
304

305
        return mfree(link);
2,579✔
306
}
307

308
DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
162,417✔
309

310
int link_get_by_index(Manager *m, int ifindex, Link **ret) {
67,685✔
311
        Link *link;
67,685✔
312

313
        assert(m);
67,685✔
314

315
        if (ifindex <= 0)
67,685✔
316
                return -EINVAL;
317

318
        link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
66,992✔
319
        if (!link)
66,992✔
320
                return -ENODEV;
321

322
        if (ret)
63,572✔
323
                *ret = link;
63,572✔
324
        return 0;
325
}
326

327
int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
1,585✔
328
        Link *link;
1,585✔
329

330
        assert(m);
1,585✔
331
        assert(ifname);
1,585✔
332

333
        link = hashmap_get(m->links_by_name, ifname);
1,585✔
334
        if (!link)
1,585✔
335
                return -ENODEV;
336

337
        if (ret)
1,548✔
338
                *ret = link;
1,548✔
339
        return 0;
340
}
341

342
int link_get_by_hw_addr(Manager *m, const struct hw_addr_data *hw_addr, Link **ret) {
×
343
        Link *link;
×
344

345
        assert(m);
×
346
        assert(hw_addr);
×
347

348
        link = hashmap_get(m->links_by_hw_addr, hw_addr);
×
349
        if (!link)
×
350
                return -ENODEV;
351

352
        if (ret)
×
353
                *ret = link;
×
354
        return 0;
355
}
356

357
int link_get_master(Link *link, Link **ret) {
13,326✔
358
        assert(link);
13,326✔
359
        assert(link->manager);
13,326✔
360
        assert(ret);
13,326✔
361

362
        if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex)
13,326✔
363
                return -ENODEV;
364

365
        return link_get_by_index(link->manager, link->master_ifindex, ret);
625✔
366
}
367

368
void link_set_state(Link *link, LinkState state) {
8,405✔
369
        assert(link);
8,405✔
370

371
        if (link->state == state)
8,405✔
372
                return;
373

374
        log_link_debug(link, "State changed: %s -> %s",
6,800✔
375
                       link_state_to_string(link->state),
376
                       link_state_to_string(state));
377

378
        link->state = state;
6,800✔
379

380
        link_send_changed(link, "AdministrativeState", NULL);
6,800✔
381
        link_dirty(link);
6,800✔
382
}
383

384
int link_stop_engines(Link *link, bool may_keep_dynamic) {
4,247✔
385
        int r, ret = 0;
4,247✔
386

387
        assert(link);
4,247✔
388
        assert(link->manager);
4,247✔
389
        assert(link->manager->event);
4,247✔
390

391
        bool keep_dynamic =
12,702✔
392
                may_keep_dynamic &&
1,788✔
393
                link->network &&
4,247✔
394
                (link->manager->state == MANAGER_RESTARTING ||
51✔
395
                 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC_ON_STOP));
14✔
396

397
        if (!keep_dynamic) {
4,208✔
398
                r = sd_dhcp_client_stop(link->dhcp_client);
4,208✔
399
                if (r < 0)
4,208✔
400
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 client: %m"));
×
401

402
                r = sd_ipv4ll_stop(link->ipv4ll);
4,208✔
403
                if (r < 0)
4,208✔
404
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 link-local: %m"));
×
405

406
                r = sd_dhcp6_client_stop(link->dhcp6_client);
4,208✔
407
                if (r < 0)
4,208✔
408
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"));
×
409

410
                r = dhcp_pd_remove(link, /* only_marked = */ false);
4,208✔
411
                if (r < 0)
4,208✔
412
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not remove DHCPv6 PD addresses and routes: %m"));
×
413

414
                r = ndisc_stop(link);
4,208✔
415
                if (r < 0)
4,208✔
416
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Discovery: %m"));
×
417

418
                ndisc_flush(link);
4,208✔
419
        }
420

421
        r = sd_dhcp_server_stop(link->dhcp_server);
4,247✔
422
        if (r < 0)
4,247✔
423
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 server: %m"));
×
424

425
        r = sd_lldp_rx_stop(link->lldp_rx);
4,247✔
426
        if (r < 0)
4,247✔
427
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Rx: %m"));
×
428

429
        r = sd_lldp_tx_stop(link->lldp_tx);
4,247✔
430
        if (r < 0)
4,247✔
431
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Tx: %m"));
×
432

433
        r = ipv4acd_stop(link);
4,247✔
434
        if (r < 0)
4,247✔
435
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 ACD client: %m"));
×
436

437
        r = sd_radv_stop(link->radv);
4,247✔
438
        if (r < 0)
4,247✔
439
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Advertisement: %m"));
×
440

441
        return ret;
4,247✔
442
}
443

UNCOV
444
void link_enter_failed(Link *link) {
×
UNCOV
445
        assert(link);
×
446

UNCOV
447
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
×
448
                return;
449

UNCOV
450
        log_link_warning(link, "Failed");
×
451

UNCOV
452
        link_set_state(link, LINK_STATE_FAILED);
×
453

UNCOV
454
        if (!ratelimit_below(&link->automatic_reconfigure_ratelimit)) {
×
455
                log_link_warning(link, "The interface entered the failed state frequently, refusing to reconfigure it automatically.");
×
456
                goto stop;
×
457
        }
458

UNCOV
459
        log_link_info(link, "Trying to reconfigure the interface.");
×
UNCOV
460
        if (link_reconfigure(link, LINK_RECONFIGURE_UNCONDITIONALLY) > 0)
×
461
                return;
462

463
stop:
×
464
        (void) link_stop_engines(link, /* may_keep_dynamic = */ false);
×
465
}
466

467
void link_check_ready(Link *link) {
23,325✔
468
        Address *a;
23,325✔
469

470
        assert(link);
23,325✔
471

472
        if (link->state == LINK_STATE_CONFIGURED)
23,325✔
473
                return;
22,547✔
474

475
        if (link->state != LINK_STATE_CONFIGURING)
19,797✔
476
                return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
1,292✔
477

478
        if (!link->network)
18,505✔
479
                return (void) log_link_debug(link, "%s(): link is unmanaged.", __func__);
×
480

481
        if (!link->tc_configured)
18,505✔
482
                return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
313✔
483

484
        if (link->set_link_messages > 0)
18,192✔
485
                return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
1,251✔
486

487
        if (!link->activated)
16,941✔
488
                return (void) log_link_debug(link, "%s(): link is not activated.", __func__);
10,550✔
489

490
        if (link->iftype == ARPHRD_CAN)
6,391✔
491
                /* let's shortcut things for CAN which doesn't need most of checks below. */
492
                goto ready;
6✔
493

494
        if (!link->stacked_netdevs_created)
6,385✔
495
                return (void) log_link_debug(link, "%s(): stacked netdevs are not created.", __func__);
157✔
496

497
        if (!link->static_addresses_configured)
6,228✔
498
                return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
3,143✔
499

500
        if (!link->static_address_labels_configured)
3,085✔
501
                return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__);
3✔
502

503
        if (!link->static_bridge_fdb_configured)
3,082✔
504
                return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
4✔
505

506
        if (!link->static_bridge_mdb_configured)
3,078✔
507
                return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
12✔
508

509
        if (!link->static_ipv6_proxy_ndp_configured)
3,066✔
510
                return (void) log_link_debug(link, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__);
4✔
511

512
        if (!link->static_neighbors_configured)
3,062✔
513
                return (void) log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
8✔
514

515
        if (!link->static_nexthops_configured)
3,054✔
516
                return (void) log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
20✔
517

518
        if (!link->static_routes_configured)
3,034✔
519
                return (void) log_link_debug(link, "%s(): static routes are not configured.", __func__);
128✔
520

521
        if (!link->static_routing_policy_rules_configured)
2,906✔
522
                return (void) log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
39✔
523

524
        if (!link->sr_iov_configured)
2,867✔
525
                return (void) log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
×
526

527
        /* IPv6LL is assigned after the link gains its carrier. */
528
        if (!link->network->configure_without_carrier &&
5,722✔
529
            link_ipv6ll_enabled(link) &&
2,855✔
530
            !in6_addr_is_set(&link->ipv6ll_address))
2,759✔
531
                return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__);
888✔
532

533
        /* All static addresses must be ready. */
534
        bool has_static_address = false;
1,979✔
535
        SET_FOREACH(a, link->addresses) {
6,816✔
536
                if (a->source != NETWORK_CONFIG_SOURCE_STATIC)
4,946✔
537
                        continue;
3,157✔
538
                if (!address_is_ready(a))
1,789✔
539
                        return (void) log_link_debug(link, "%s(): static address %s is not ready.", __func__,
109✔
540
                                                     IN_ADDR_PREFIX_TO_STRING(a->family, &a->in_addr, a->prefixlen));
541
                has_static_address = true;
542
        }
543

544
        /* If at least one static address is requested, do not request that dynamic addressing protocols are finished. */
545
        if (has_static_address)
1,870✔
546
                goto ready;
349✔
547

548
        /* If no dynamic addressing protocol enabled, assume the interface is ready.
549
         * Note, ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
550
        if (!link_ipv4ll_enabled(link) && !link_dhcp4_enabled(link) &&
2,761✔
551
            !link_dhcp6_enabled(link) && !link_dhcp_pd_is_enabled(link) &&
767✔
552
            (link->network->configure_without_carrier || !link_ndisc_enabled(link)))
551✔
553
                goto ready;
221✔
554

555
        bool ipv4ll_ready =
2,600✔
556
                link_ipv4ll_enabled(link) && link->ipv4ll_address_configured &&
1,354✔
557
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_IPV4LL);
54✔
558
        bool dhcp4_ready =
1,300✔
559
                link_dhcp4_enabled(link) && link->dhcp4_configured &&
1,364✔
560
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP4);
64✔
561
        bool dhcp6_ready =
1,300✔
562
                link_dhcp6_enabled(link) && link->dhcp6_configured &&
1,300✔
563
                (!link->network->dhcp6_use_address ||
56✔
564
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP6));
27✔
565
        bool dhcp_pd_ready =
2,600✔
566
                link_dhcp_pd_is_enabled(link) && link->dhcp_pd_configured &&
1,300✔
567
                (!link->network->dhcp_pd_assign ||
135✔
568
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP_PD));
64✔
569
        bool ndisc_ready =
2,600✔
570
                link_ndisc_enabled(link) && link->ndisc_configured &&
1,300✔
571
                (!link->network->ndisc_use_autonomous_prefix ||
420✔
572
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_NDISC));
210✔
573

574
        /* If the uplink for PD is self, then request the corresponding DHCP protocol is also ready. */
575
        if (dhcp_pd_is_uplink(link, link, /* accept_auto = */ false)) {
1,300✔
576
                if (link_dhcp4_enabled(link) && link->network->dhcp_use_6rd &&
80✔
577
                    sd_dhcp_lease_has_6rd(link->dhcp_lease)) {
25✔
578
                        if (!link->dhcp4_configured)
22✔
579
                                return (void) log_link_debug(link, "%s(): DHCPv4 6rd prefix is assigned, but DHCPv4 protocol is not finished yet.", __func__);
20✔
580
                        if (!dhcp_pd_ready)
2✔
581
                                return (void) log_link_debug(link, "%s(): DHCPv4 is finished, but prefix acquired by DHCPv4-6rd is not assigned yet.", __func__);
×
582
                }
583

584
                if (link_dhcp6_enabled(link) && link->network->dhcp6_use_pd_prefix &&
65✔
585
                    sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease)) {
30✔
586
                        if (!link->dhcp6_configured)
21✔
587
                                return (void) log_link_debug(link, "%s(): DHCPv6 IA_PD prefix is assigned, but DHCPv6 protocol is not finished yet.", __func__);
9✔
588
                        if (!dhcp_pd_ready)
12✔
589
                                return (void) log_link_debug(link, "%s(): DHCPv6 is finished, but prefix acquired by DHCPv6 IA_PD is not assigned yet.", __func__);
9✔
590
                }
591
        }
592

593
        /* At least one dynamic addressing protocol is finished. */
594
        if (!ipv4ll_ready && !dhcp4_ready && !dhcp6_ready && !dhcp_pd_ready && !ndisc_ready)
1,262✔
595
                return (void) log_link_debug(link, "%s(): dynamic addressing protocols are enabled but none of them finished yet.", __func__);
1,060✔
596

597
        log_link_debug(link, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
1,046✔
598
                       __func__,
599
                       yes_no(ipv4ll_ready),
600
                       yes_no(dhcp4_ready),
601
                       yes_no(dhcp6_ready),
602
                       yes_no(dhcp_pd_ready),
603
                       yes_no(ndisc_ready));
604

605
ready:
778✔
606
        link_set_state(link, LINK_STATE_CONFIGURED);
778✔
607
}
608

609
static int link_request_static_configs(Link *link) {
1,361✔
610
        int r;
1,361✔
611

612
        assert(link);
1,361✔
613
        assert(link->network);
1,361✔
614
        assert(link->state != _LINK_STATE_INVALID);
1,361✔
615

616
        r = link_request_static_addresses(link);
1,361✔
617
        if (r < 0)
1,361✔
618
                return r;
619

620
        r = link_request_static_address_labels(link);
1,361✔
621
        if (r < 0)
1,361✔
622
                return r;
623

624
        r = link_request_static_bridge_fdb(link);
1,361✔
625
        if (r < 0)
1,361✔
626
                return r;
627

628
        r = link_request_static_bridge_mdb(link);
1,361✔
629
        if (r < 0)
1,361✔
630
                return r;
631

632
        r = link_request_static_ipv6_proxy_ndp_addresses(link);
1,361✔
633
        if (r < 0)
1,361✔
634
                return r;
635

636
        r = link_request_static_neighbors(link);
1,361✔
637
        if (r < 0)
1,361✔
638
                return r;
639

640
        r = link_request_static_nexthops(link, false);
1,361✔
641
        if (r < 0)
1,361✔
642
                return r;
643

644
        r = link_request_static_routes(link, false);
1,361✔
645
        if (r < 0)
1,361✔
646
                return r;
647

648
        r = link_request_static_routing_policy_rules(link);
1,361✔
649
        if (r < 0)
1,361✔
650
                return r;
×
651

652
        return 0;
653
}
654

655
int link_request_stacked_netdevs(Link *link, NetDevLocalAddressType type) {
10,782✔
656
        NetDev *netdev;
10,782✔
657
        int r;
10,782✔
658

659
        assert(link);
10,782✔
660

661
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
10,782✔
662
                return 0;
10,782✔
663

664
        assert(link->network);
2,338✔
665

666
        link->stacked_netdevs_created = false;
2,338✔
667

668
        HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
2,686✔
669
                if (!netdev_needs_reconfigure(netdev, type))
348✔
670
                        continue;
206✔
671

672
                r = link_request_stacked_netdev(link, netdev);
142✔
673
                if (r < 0)
142✔
674
                        return r;
×
675
        }
676

677
        if (link->create_stacked_netdev_messages == 0) {
2,338✔
678
                link->stacked_netdevs_created = true;
2,207✔
679
                link_check_ready(link);
2,207✔
680
        }
681

682
        return 0;
683
}
684

685
static int link_acquire_dynamic_ipv6_conf(Link *link) {
718✔
686
        int r;
718✔
687

688
        assert(link);
718✔
689

690
        r = radv_start(link);
718✔
691
        if (r < 0)
718✔
692
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
×
693

694
        r = ndisc_start(link);
718✔
695
        if (r < 0)
718✔
696
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
×
697

698
        r = dhcp6_start(link);
718✔
699
        if (r < 0)
718✔
700
                return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m");
×
701

702
        return 0;
703
}
704

705
static int link_acquire_dynamic_ipv4_conf(Link *link) {
835✔
706
        int r;
835✔
707

708
        assert(link);
835✔
709
        assert(link->manager);
835✔
710
        assert(link->manager->event);
835✔
711

712
        if (link->dhcp_client) {
835✔
713
                r = dhcp4_start(link);
1✔
714
                if (r < 0)
1✔
715
                        return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
×
716

717
                log_link_debug(link, "Acquiring DHCPv4 lease.");
1✔
718

719
        } else if (link->ipv4ll) {
834✔
720
                if (in4_addr_is_set(&link->network->ipv4ll_start_address)) {
71✔
721
                        r = sd_ipv4ll_set_address(link->ipv4ll, &link->network->ipv4ll_start_address);
1✔
722
                        if (r < 0)
1✔
723
                                return log_link_warning_errno(link, r, "Could not set IPv4 link-local start address: %m");
×
724
                }
725

726
                r = sd_ipv4ll_start(link->ipv4ll);
71✔
727
                if (r < 0)
71✔
728
                        return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
×
729

730
                log_link_debug(link, "Acquiring IPv4 link-local address.");
71✔
731
        }
732

733
        r = link_start_dhcp4_server(link);
835✔
734
        if (r < 0)
835✔
735
                return log_link_warning_errno(link, r, "Could not start DHCP server: %m");
×
736

737
        r = ipv4acd_start(link);
835✔
738
        if (r < 0)
835✔
739
                return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m");
×
740

741
        return 0;
742
}
743

744
static int link_acquire_dynamic_conf(Link *link) {
835✔
745
        int r;
835✔
746

747
        assert(link);
835✔
748
        assert(link->network);
835✔
749

750
        r = link_acquire_dynamic_ipv4_conf(link);
835✔
751
        if (r < 0)
835✔
752
                return r;
753

754
        if (in6_addr_is_set(&link->ipv6ll_address)) {
835✔
755
                r = link_acquire_dynamic_ipv6_conf(link);
254✔
756
                if (r < 0)
254✔
757
                        return r;
758
        }
759

760
        if (!link_radv_enabled(link) || !link->network->dhcp_pd_announce) {
835✔
761
                /* DHCPv6PD downstream does not require IPv6LL address. But may require RADV to be
762
                 * configured, and RADV may not be configured yet here. Only acquire subnet prefix when
763
                 * RADV is disabled, or the announcement of the prefix is disabled. Otherwise, the
764
                 * below will be called in radv_start(). */
765
                r = dhcp_request_prefix_delegation(link);
764✔
766
                if (r < 0)
764✔
767
                        return log_link_warning_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
×
768
        }
769

770
        if (link->lldp_tx) {
835✔
771
                r = sd_lldp_tx_start(link->lldp_tx);
4✔
772
                if (r < 0)
4✔
773
                        return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
×
774
        }
775

776
        if (link->lldp_rx) {
835✔
777
                r = sd_lldp_rx_start(link->lldp_rx);
755✔
778
                if (r < 0)
755✔
UNCOV
779
                        return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
×
780
        }
781

782
        return 0;
783
}
784

785
int link_ipv6ll_gained(Link *link) {
592✔
786
        int r;
592✔
787

788
        assert(link);
592✔
789

790
        log_link_info(link, "Gained IPv6LL");
592✔
791

792
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
592✔
793
                return 0;
794

795
        r = link_acquire_dynamic_ipv6_conf(link);
464✔
796
        if (r < 0)
464✔
797
                return r;
798

799
        r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_IPV6LL);
464✔
800
        if (r < 0)
464✔
801
                return r;
802

803
        link_check_ready(link);
464✔
804
        return 0;
464✔
805
}
806

807
int link_handle_bound_to_list(Link *link) {
34✔
808
        bool required_up = false;
34✔
809
        bool link_is_up = false;
34✔
810
        Link *l;
34✔
811

812
        assert(link);
34✔
813

814
        /* If at least one interface in bound_to_links has carrier, then make this interface up.
815
         * If all interfaces in bound_to_links do not, then make this interface down. */
816

817
        if (hashmap_isempty(link->bound_to_links))
34✔
818
                return 0;
34✔
819

820
        if (link->flags & IFF_UP)
26✔
821
                link_is_up = true;
14✔
822

823
        HASHMAP_FOREACH(l, link->bound_to_links)
40✔
824
                if (link_has_carrier(l)) {
28✔
825
                        required_up = true;
826
                        break;
827
                }
828

829
        if (!required_up && link_is_up)
26✔
830
                return link_request_to_bring_up_or_down(link, /* up = */ false);
7✔
831
        if (required_up && !link_is_up)
19✔
832
                return link_request_to_bring_up_or_down(link, /* up = */ true);
7✔
833

834
        return 0;
835
}
836

837
static int link_handle_bound_by_list(Link *link) {
4,225✔
838
        Link *l;
4,225✔
839
        int r;
4,225✔
840

841
        assert(link);
4,225✔
842

843
        /* Update up or down state of interfaces which depend on this interface's carrier state. */
844

845
        HASHMAP_FOREACH(l, link->bound_by_links) {
4,246✔
846
                r = link_handle_bound_to_list(l);
21✔
847
                if (r < 0)
21✔
848
                        return r;
×
849
        }
850

851
        return 0;
4,225✔
852
}
853

854
static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
20✔
855
        int r;
20✔
856

857
        assert(link);
20✔
858
        assert(carrier);
20✔
859

860
        if (link == carrier)
20✔
861
                return 0;
862

863
        if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
20✔
864
                return 0;
865

866
        r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
16✔
867
        if (r < 0)
16✔
868
                return r;
869

870
        link_dirty(link);
16✔
871

872
        return 0;
16✔
873
}
874

875
static int link_new_bound_by_list(Link *link) {
2,397✔
876
        Manager *m;
2,397✔
877
        Link *carrier;
2,397✔
878
        int r;
2,397✔
879

880
        assert(link);
2,397✔
881
        assert(link->manager);
2,397✔
882

883
        m = link->manager;
2,397✔
884

885
        HASHMAP_FOREACH(carrier, m->links_by_index) {
16,449✔
886
                if (!carrier->network)
14,052✔
887
                        continue;
13,387✔
888

889
                if (strv_isempty(carrier->network->bind_carrier))
665✔
890
                        continue;
657✔
891

892
                if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
8✔
893
                        r = link_put_carrier(link, carrier, &link->bound_by_links);
7✔
894
                        if (r < 0)
7✔
895
                                return r;
×
896
                }
897
        }
898

899
        HASHMAP_FOREACH(carrier, link->bound_by_links) {
2,404✔
900
                r = link_put_carrier(carrier, link, &carrier->bound_to_links);
7✔
901
                if (r < 0)
7✔
902
                        return r;
×
903
        }
904

905
        return 0;
2,397✔
906
}
907

908
static int link_new_bound_to_list(Link *link) {
758✔
909
        Manager *m;
758✔
910
        Link *carrier;
758✔
911
        int r;
758✔
912

913
        assert(link);
758✔
914
        assert(link->manager);
758✔
915

916
        if (!link->network)
758✔
917
                return 0;
758✔
918

919
        if (strv_isempty(link->network->bind_carrier))
758✔
920
                return 0;
921

922
        m = link->manager;
5✔
923

924
        HASHMAP_FOREACH(carrier, m->links_by_index) {
42✔
925
                if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
32✔
926
                        r = link_put_carrier(link, carrier, &link->bound_to_links);
3✔
927
                        if (r < 0)
3✔
928
                                return r;
×
929
                }
930
        }
931

932
        HASHMAP_FOREACH(carrier, link->bound_to_links) {
8✔
933
                r = link_put_carrier(carrier, link, &carrier->bound_by_links);
3✔
934
                if (r < 0)
3✔
935
                        return r;
×
936
        }
937

938
        return 0;
5✔
939
}
940

941
static void link_free_bound_to_list(Link *link) {
3,374✔
942
        bool updated = false;
3,374✔
943
        Link *bound_to;
3,374✔
944

945
        assert(link);
3,374✔
946

947
        while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
3,374✔
UNCOV
948
                updated = true;
×
949

UNCOV
950
                if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
×
UNCOV
951
                        link_dirty(bound_to);
×
952
        }
953

954
        if (updated)
3,374✔
UNCOV
955
                link_dirty(link);
×
956
}
3,374✔
957

958
static void link_free_bound_by_list(Link *link) {
789✔
959
        bool updated = false;
789✔
960
        Link *bound_by;
789✔
961

962
        assert(link);
789✔
963

964
        while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
797✔
965
                updated = true;
8✔
966

967
                if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
8✔
968
                        link_dirty(bound_by);
8✔
969
                        link_handle_bound_to_list(bound_by);
8✔
970
                }
971
        }
972

973
        if (updated)
789✔
974
                link_dirty(link);
8✔
975
}
789✔
976

977
static int link_append_to_master(Link *link) {
429✔
978
        Link *master;
429✔
979
        int r;
429✔
980

981
        assert(link);
429✔
982

983
        /* - The link may have no master.
984
         * - RTM_NEWLINK message about master interface may not be received yet. */
985
        if (link_get_master(link, &master) < 0)
429✔
986
                return 0;
429✔
987

988
        r = set_ensure_put(&master->slaves, NULL, link);
421✔
989
        if (r <= 0)
421✔
990
                return r;
991

992
        link_ref(link);
34✔
993
        return 0;
994
}
995

996
static void link_drop_from_master(Link *link) {
823✔
997
        Link *master;
823✔
998

999
        assert(link);
823✔
1000

1001
        if (!link->manager)
823✔
1002
                return;
796✔
1003

1004
        if (link_get_master(link, &master) < 0)
823✔
1005
                return;
1006

1007
        link_unref(set_remove(master->slaves, link));
27✔
1008
}
1009

1010
static int link_drop_requests(Link *link) {
3,374✔
1011
        Request *req;
3,374✔
1012
        int ret = 0;
3,374✔
1013

1014
        assert(link);
3,374✔
1015
        assert(link->manager);
3,374✔
1016

1017
        ORDERED_SET_FOREACH(req, link->manager->request_queue) {
5,135✔
1018
                if (req->link != link)
1,761✔
1019
                        continue;
1,675✔
1020

1021
                /* If the request is already called, but its reply is not received, then we need to
1022
                 * drop the configuration (e.g. address) here. Note, if the configuration is known,
1023
                 * it will be handled later by link_drop_unmanaged_addresses() or so. */
1024
                if (req->waiting_reply && link->state != LINK_STATE_LINGER)
86✔
UNCOV
1025
                        switch (req->type) {
×
1026
                        case REQUEST_TYPE_ADDRESS: {
×
1027
                                Address *address = ASSERT_PTR(req->userdata);
×
1028

1029
                                if (address_get(link, address, NULL) < 0)
×
1030
                                        RET_GATHER(ret, address_remove(address, link));
×
1031
                                break;
1032
                        }
1033
                        case REQUEST_TYPE_NEIGHBOR: {
×
1034
                                Neighbor *neighbor = ASSERT_PTR(req->userdata);
×
1035

1036
                                if (neighbor_get(link, neighbor, NULL) < 0)
×
1037
                                        RET_GATHER(ret, neighbor_remove(neighbor, link));
×
1038
                                break;
1039
                        }
1040
                        case REQUEST_TYPE_NEXTHOP: {
×
1041
                                NextHop *nexthop = ASSERT_PTR(req->userdata);
×
1042

1043
                                if (nexthop_get_by_id(link->manager, nexthop->id, NULL) < 0)
×
1044
                                        RET_GATHER(ret, nexthop_remove(nexthop, link->manager));
×
1045
                                break;
1046
                        }
UNCOV
1047
                        case REQUEST_TYPE_ROUTE: {
×
UNCOV
1048
                                Route *route = ASSERT_PTR(req->userdata);
×
1049

UNCOV
1050
                                if (route_get(link->manager, route, NULL) < 0)
×
UNCOV
1051
                                        RET_GATHER(ret, route_remove(route, link->manager));
×
1052
                                break;
1053
                        }
1054
                        default:
86✔
1055
                                ;
86✔
1056
                        }
1057

1058
                request_detach(req);
86✔
1059
        }
1060

1061
        return ret;
3,374✔
1062
}
1063

1064
static Link *link_drop(Link *link) {
813✔
1065
        if (!link)
813✔
1066
                return NULL;
1067

1068
        assert(link->manager);
789✔
1069

1070
        link_set_state(link, LINK_STATE_LINGER);
789✔
1071

1072
        /* Drop all references from other links and manager. Note that async netlink calls may have
1073
         * references to the link, and they will be dropped when we receive replies. */
1074

1075
        (void) link_drop_requests(link);
789✔
1076

1077
        link_free_bound_to_list(link);
789✔
1078
        link_free_bound_by_list(link);
789✔
1079

1080
        link_clear_sr_iov_ifindices(link);
789✔
1081

1082
        link_drop_from_master(link);
789✔
1083

1084
        if (link->state_file)
789✔
1085
                (void) unlink(link->state_file);
789✔
1086

1087
        link_clean(link);
789✔
1088

1089
        STRV_FOREACH(n, link->alternative_names)
826✔
1090
                hashmap_remove(link->manager->links_by_name, *n);
37✔
1091
        hashmap_remove(link->manager->links_by_name, link->ifname);
789✔
1092

1093
        /* bonding master and its slaves have the same hardware address. */
1094
        hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
789✔
1095

1096
        /* The following must be called at last. */
1097
        assert_se(hashmap_remove(link->manager->links_by_index, INT_TO_PTR(link->ifindex)) == link);
789✔
1098
        return link_unref(link);
789✔
1099
}
1100

1101
static int link_drop_unmanaged_config(Link *link) {
758✔
1102
        int r;
758✔
1103

1104
        assert(link);
758✔
1105
        assert(link->state == LINK_STATE_CONFIGURING);
758✔
1106
        assert(link->manager);
758✔
1107

1108
        r = link_drop_unmanaged_routes(link);
758✔
1109
        RET_GATHER(r, link_drop_unmanaged_nexthops(link));
758✔
1110
        RET_GATHER(r, link_drop_unmanaged_addresses(link));
758✔
1111
        RET_GATHER(r, link_drop_unmanaged_neighbors(link));
758✔
1112
        RET_GATHER(r, link_drop_unmanaged_routing_policy_rules(link));
758✔
1113

1114
        return r;
758✔
1115
}
1116

1117
static int link_drop_static_config(Link *link) {
2,423✔
1118
        int r;
2,423✔
1119

1120
        assert(link);
2,423✔
1121
        assert(link->manager);
2,423✔
1122

1123
        r = link_drop_static_routes(link);
2,423✔
1124
        RET_GATHER(r, link_drop_static_nexthops(link));
2,423✔
1125
        RET_GATHER(r, link_drop_static_addresses(link));
2,423✔
1126
        RET_GATHER(r, link_drop_static_neighbors(link));
2,423✔
1127
        RET_GATHER(r, link_drop_static_routing_policy_rules(link));
2,423✔
1128

1129
        return r;
2,423✔
1130
}
1131

1132
static int link_drop_dynamic_config(Link *link, Network *network) {
722✔
1133
        int r;
722✔
1134

1135
        assert(link);
722✔
1136
        assert(link->network);
722✔
1137

1138
        /* Drop unnecessary dynamic configurations gracefully, e.g. drop DHCP lease in the case that
1139
         * previously DHCP=yes and now DHCP=no, but keep DHCP lease when DHCP setting is unchanged. */
1140

1141
        r = link_drop_ndisc_config(link, network);
722✔
1142
        RET_GATHER(r, link_drop_radv_config(link, network)); /* Stop before dropping DHCP-PD prefixes. */
722✔
1143
        RET_GATHER(r, link_drop_ipv4ll_config(link, network)); /* Stop before DHCPv4 client. */
722✔
1144
        RET_GATHER(r, link_drop_dhcp4_config(link, network));
722✔
1145
        RET_GATHER(r, link_drop_dhcp6_config(link, network));
722✔
1146
        RET_GATHER(r, link_drop_dhcp_pd_config(link, network));
722✔
1147
        link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
722✔
1148
        link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx); /* TODO: keep the received neighbors. */
722✔
1149
        link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
722✔
1150

1151
        /* Even if we do not release DHCP lease or so, reset 'configured' flags. Otherwise, e.g. if
1152
         * previously UseDNS= was disabled but is now enabled, link will enter configured state before
1153
         * expected DNS servers being acquired. */
1154
        link->ipv4ll_address_configured = false;
722✔
1155
        link->dhcp4_configured = false;
722✔
1156
        link->dhcp6_configured = false;
722✔
1157
        link->dhcp_pd_configured = false;
722✔
1158
        link->ndisc_configured = false;
722✔
1159

1160
        return r;
722✔
1161
}
1162

1163
static int link_configure(Link *link) {
758✔
1164
        int r;
758✔
1165

1166
        assert(link);
758✔
1167
        assert(link->network);
758✔
1168
        assert(link->state == LINK_STATE_INITIALIZED);
758✔
1169

1170
        link_set_state(link, LINK_STATE_CONFIGURING);
758✔
1171

1172
        r = link_drop_unmanaged_config(link);
758✔
1173
        if (r < 0)
758✔
1174
                return r;
1175

1176
        r = link_new_bound_to_list(link);
758✔
1177
        if (r < 0)
758✔
1178
                return r;
1179

1180
        r = link_request_traffic_control(link);
758✔
1181
        if (r < 0)
758✔
1182
                return r;
1183

1184
        r = link_configure_mtu(link);
758✔
1185
        if (r < 0)
758✔
1186
                return r;
1187

1188
        if (link->iftype == ARPHRD_CAN) {
758✔
1189
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1190
                r = link_request_to_set_can(link);
6✔
1191
                if (r < 0)
6✔
1192
                        return r;
1193

1194
                return link_request_to_activate(link);
6✔
1195
        }
1196

1197
        r = link_request_sr_iov_vfs(link);
752✔
1198
        if (r < 0)
752✔
1199
                return r;
1200

1201
        r = link_set_sysctl(link);
752✔
1202
        if (r < 0)
752✔
1203
                return r;
1204

1205
        r = link_request_to_set_mac(link, /* allow_retry = */ true);
752✔
1206
        if (r < 0)
752✔
1207
                return r;
1208

1209
        r = link_request_to_set_ipoib(link);
752✔
1210
        if (r < 0)
752✔
1211
                return r;
1212

1213
        r = link_request_to_set_flags(link);
752✔
1214
        if (r < 0)
752✔
1215
                return r;
1216

1217
        r = link_request_to_set_group(link);
752✔
1218
        if (r < 0)
752✔
1219
                return r;
1220

1221
        r = link_request_to_set_addrgen_mode(link);
752✔
1222
        if (r < 0)
752✔
1223
                return r;
1224

1225
        r = link_request_to_set_master(link);
752✔
1226
        if (r < 0)
752✔
1227
                return r;
1228

1229
        r = link_request_stacked_netdevs(link, _NETDEV_LOCAL_ADDRESS_TYPE_INVALID);
752✔
1230
        if (r < 0)
752✔
1231
                return r;
1232

1233
        r = link_request_to_set_bond(link);
752✔
1234
        if (r < 0)
752✔
1235
                return r;
1236

1237
        r = link_request_to_set_bridge(link);
752✔
1238
        if (r < 0)
752✔
1239
                return r;
1240

1241
        r = link_request_to_set_bridge_vlan(link);
752✔
1242
        if (r < 0)
752✔
1243
                return r;
1244

1245
        r = link_request_to_activate(link);
752✔
1246
        if (r < 0)
752✔
1247
                return r;
1248

1249
        r = ipv4ll_configure(link);
752✔
1250
        if (r < 0)
752✔
1251
                return r;
1252

1253
        r = link_request_dhcp4_client(link);
752✔
1254
        if (r < 0)
752✔
1255
                return r;
1256

1257
        r = link_request_dhcp6_client(link);
752✔
1258
        if (r < 0)
752✔
1259
                return r;
1260

1261
        r = link_request_ndisc(link);
752✔
1262
        if (r < 0)
752✔
1263
                return r;
1264

1265
        r = link_request_dhcp_server(link);
752✔
1266
        if (r < 0)
752✔
1267
                return r;
1268

1269
        r = link_request_radv(link);
752✔
1270
        if (r < 0)
752✔
1271
                return r;
1272

1273
        r = link_lldp_rx_configure(link);
752✔
1274
        if (r < 0)
752✔
1275
                return r;
1276

1277
        r = link_lldp_tx_configure(link);
752✔
1278
        if (r < 0)
752✔
1279
                return r;
1280

1281
        r = link_request_static_configs(link);
752✔
1282
        if (r < 0)
752✔
1283
                return r;
1284

1285
        if (!link_has_carrier(link))
752✔
1286
                return 0;
1287

1288
        return link_acquire_dynamic_conf(link);
226✔
1289
}
1290

1291
static int link_get_network(Link *link, Network **ret) {
4,274✔
1292
        Network *network;
4,274✔
1293
        int r;
4,274✔
1294

1295
        assert(link);
4,274✔
1296
        assert(link->manager);
4,274✔
1297
        assert(ret);
4,274✔
1298

1299
        ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
15,551✔
1300
                bool warn = false;
14,636✔
1301

1302
                r = net_match_config(
29,272✔
1303
                                &network->match,
14,636✔
1304
                                link->dev,
1305
                                &link->hw_addr,
14,636✔
1306
                                &link->permanent_hw_addr,
14,636✔
1307
                                link->driver,
14,636✔
1308
                                link->iftype,
14,636✔
1309
                                link->kind,
14,636✔
1310
                                link->ifname,
14,636✔
1311
                                link->alternative_names,
14,636✔
1312
                                link->wlan_iftype,
1313
                                link->ssid,
14,636✔
1314
                                &link->bssid);
14,636✔
1315
                if (r < 0)
14,636✔
1316
                        return r;
3,359✔
1317
                if (r == 0)
14,636✔
1318
                        continue;
11,277✔
1319

1320
                if (network->match.ifname && link->dev) {
3,359✔
1321
                        uint8_t name_assign_type = NET_NAME_UNKNOWN;
3,349✔
1322
                        const char *attr;
3,349✔
1323

1324
                        if (sd_device_get_sysattr_value(link->dev, "name_assign_type", &attr) >= 0)
3,349✔
1325
                                (void) safe_atou8(attr, &name_assign_type);
1,904✔
1326

1327
                        warn = name_assign_type == NET_NAME_ENUM;
3,349✔
1328
                }
1329

1330
                log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
6,718✔
1331
                              "Found matching .network file%s: %s",
1332
                              warn ? ", based on potentially unpredictable interface name" : "",
1333
                              network->filename);
1334

1335
                if (network->unmanaged)
3,359✔
1336
                        return -ENOENT;
1337

1338
                *ret = network;
1,595✔
1339
                return 0;
1,595✔
1340
        }
1341

1342
        return log_link_debug_errno(link, SYNTHETIC_ERRNO(ENOENT), "No matching .network found.");
915✔
1343
}
1344

1345
static void link_enter_unmanaged(Link *link) {
2,679✔
1346
        assert(link);
2,679✔
1347

1348
        if (link->state == LINK_STATE_UNMANAGED)
2,679✔
1349
                return;
1350

1351
        log_link_full(link, link->state == LINK_STATE_INITIALIZED ? LOG_DEBUG : LOG_INFO,
1,832✔
1352
                      "Unmanaging interface.");
1353

1354
        (void) link_stop_engines(link, /* may_keep_dynamic = */ false);
1,827✔
1355
        (void) link_drop_requests(link);
1,827✔
1356
        (void) link_drop_static_config(link);
1,827✔
1357

1358
        /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1359
         * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1360
         * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1361
         * map here, as it depends on .network files assigned to other links. */
1362
        link_free_bound_to_list(link);
1,827✔
1363
        link_free_engines(link);
1,827✔
1364

1365
        link->network = network_unref(link->network);
1,827✔
1366
        link_set_state(link, LINK_STATE_UNMANAGED);
1,827✔
1367
}
1368

1369
int link_reconfigure_impl(Link *link, LinkReconfigurationFlag flags) {
5,158✔
1370
        Network *network = NULL;
5,158✔
1371
        int r;
5,158✔
1372

1373
        assert(link);
5,158✔
1374
        assert(link->manager);
5,158✔
1375

1376
        link_assign_netdev(link);
5,158✔
1377

1378
        if (link->manager->state != MANAGER_RUNNING)
5,158✔
1379
                return 0;
5,158✔
1380

1381
        if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
9,432✔
1382
                return 0;
1383

1384
        r = link_get_network(link, &network);
4,274✔
1385
        if (r == -ENOENT) {
4,274✔
1386
                link_enter_unmanaged(link);
2,679✔
1387
                return 0;
2,679✔
1388
        }
1389
        if (r < 0)
1,595✔
1390
                return r;
1391

1392
        if (link->network == network && !FLAGS_SET(flags, LINK_RECONFIGURE_UNCONDITIONALLY))
1,595✔
1393
                return 0;
1394

1395
        _cleanup_free_ char *joined = strv_join(network->dropins, ", ");
1,516✔
1396
        if (link->network)
758✔
1397
                log_link_info(link, "Reconfiguring with %s%s%s%s.",
531✔
1398
                              network->filename,
1399
                              isempty(joined) ? "" : " (dropins: ",
1400
                              joined,
1401
                              isempty(joined) ? "" : ")");
1402
        else
1403
                log_link_info(link, "Configuring with %s%s%s%s.",
1,743✔
1404
                              network->filename,
1405
                              isempty(joined) ? "" : " (dropins: ",
1406
                              joined,
1407
                              isempty(joined) ? "" : ")");
1408

1409
        /* Dropping configurations based on the old .network file. */
1410
        r = link_drop_requests(link);
758✔
1411
        if (r < 0)
758✔
1412
                return r;
1413

1414
        /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1415
         * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1416
         * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1417
         * map here, as it depends on .network files assigned to other links. */
1418
        link_free_bound_to_list(link);
758✔
1419

1420
        _cleanup_(network_unrefp) Network *old_network = TAKE_PTR(link->network);
758✔
1421

1422
        /* Then, apply new .network file */
1423
        link->network = network_ref(network);
758✔
1424

1425
        if (FLAGS_SET(network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC) ||
758✔
1426
            !FLAGS_SET(flags, LINK_RECONFIGURE_CLEANLY)) {
753✔
1427
                /* To make 'networkctl reconfigure INTERFACE' work safely for an interface whose new .network
1428
                 * file has KeepConfiguration=dynamic or yes, even if a clean reconfiguration is requested,
1429
                 * drop only unnecessary or possibly being changed dynamic configurations here. */
1430
                r = link_drop_dynamic_config(link, old_network);
722✔
1431
                if (r < 0)
722✔
1432
                        return r;
1433
        } else {
1434
                /* Otherwise, stop DHCP client and friends unconditionally, and drop all dynamic
1435
                 * configurations like DHCP address and routes. */
1436
                r = link_stop_engines(link, /* may_keep_dhcp = */ false);
36✔
1437
                if (r < 0)
36✔
1438
                        return r;
1439

1440
                /* Free DHCP client and friends. */
1441
                link_free_engines(link);
36✔
1442
        }
1443

1444
        link_update_operstate(link, true);
758✔
1445
        link_dirty(link);
758✔
1446

1447
        link_set_state(link, LINK_STATE_INITIALIZED);
758✔
1448
        link->activated = false;
758✔
1449

1450
        r = link_configure(link);
758✔
1451
        if (r < 0)
758✔
UNCOV
1452
                return r;
×
1453

1454
        return 1;
1455
}
1456

1457
typedef struct LinkReconfigurationData {
1458
        Manager *manager;
1459
        Link *link;
1460
        LinkReconfigurationFlag flags;
1461
        sd_bus_message *message;
1462
        unsigned *counter;
1463
} LinkReconfigurationData;
1464

1465
static LinkReconfigurationData* link_reconfiguration_data_free(LinkReconfigurationData *data) {
1,163✔
1466
        if (!data)
1,163✔
1467
                return NULL;
1468

1469
        link_unref(data->link);
1,163✔
1470
        sd_bus_message_unref(data->message);
1,163✔
1471

1472
        return mfree(data);
1,163✔
1473
}
1474

1475
DEFINE_TRIVIAL_CLEANUP_FUNC(LinkReconfigurationData*, link_reconfiguration_data_free);
1,165✔
1476

1477
static void link_reconfiguration_data_destroy_callback(LinkReconfigurationData *data) {
1,163✔
1478
        int r;
1,163✔
1479

1480
        assert(data);
1,163✔
1481

1482
        if (data->message) {
1,163✔
1483
                if (data->counter) {
1,069✔
1484
                        assert(*data->counter > 0);
1,033✔
1485
                        (*data->counter)--;
1,033✔
1486
                }
1487

1488
                if (!data->counter || *data->counter <= 0) {
1,069✔
1489
                        /* Update the state files before replying the bus method. Otherwise,
1490
                         * systemd-networkd-wait-online following networkctl reload/reconfigure may read an
1491
                         * outdated state file and wrongly handle an interface is already in the configured
1492
                         * state. */
1493
                        (void) manager_clean_all(data->manager);
170✔
1494

1495
                        r = sd_bus_reply_method_return(data->message, NULL);
170✔
1496
                        if (r < 0)
170✔
1497
                                log_warning_errno(r, "Failed to reply for DBus method, ignoring: %m");
×
1498
                }
1499
        }
1500

1501
        link_reconfiguration_data_free(data);
1,163✔
1502
}
1,163✔
1503

1504
static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, LinkReconfigurationData *data) {
1,163✔
1505
        Link *link = ASSERT_PTR(ASSERT_PTR(data)->link);
1,163✔
1506
        int r;
1,163✔
1507

1508
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1,163✔
1509
        if (r <= 0)
1,163✔
1510
                return r;
1511

1512
        r = link_reconfigure_impl(link, data->flags);
1,163✔
1513
        if (r < 0) {
1,163✔
1514
                link_enter_failed(link);
×
1515
                return 0;
×
1516
        }
1517

1518
        return r;
1519
}
1520

1521
int link_reconfigure_full(Link *link, LinkReconfigurationFlag flags, sd_bus_message *message, unsigned *counter) {
1,165✔
1522
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1,165✔
1523
        _cleanup_(link_reconfiguration_data_freep) LinkReconfigurationData *data = NULL;
×
1524
        int r;
1,165✔
1525

1526
        assert(link);
1,165✔
1527
        assert(link->manager);
1,165✔
1528
        assert(link->manager->rtnl);
1,165✔
1529

1530
        /* When the link is in the pending or initialized state, link_reconfigure_impl() will be called later
1531
         * by link_initialized() or link_initialized_and_synced(). To prevent the function from being called
1532
         * multiple times simultaneously, let's refuse to reconfigure the interface here in such cases. */
1533
        if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED, LINK_STATE_LINGER))
2,328✔
1534
                return 0; /* 0 means no-op. */
1535

1536
        data = new(LinkReconfigurationData, 1);
1,163✔
1537
        if (!data) {
1,163✔
1538
                r = -ENOMEM;
×
1539
                goto failed;
×
1540
        }
1541

1542
        *data = (LinkReconfigurationData) {
2,326✔
1543
                .manager = link->manager,
1,163✔
1544
                .link = link_ref(link),
1,163✔
1545
                .flags = flags,
1546
                .message = sd_bus_message_ref(message), /* message may be NULL, but _ref() works fine. */
1,163✔
1547
                .counter = counter,
1548
        };
1549

1550
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
1,163✔
1551
        if (r < 0)
1,163✔
1552
                goto failed;
×
1553

1554
        r = netlink_call_async(link->manager->rtnl, NULL, req,
1,163✔
1555
                               link_reconfigure_handler,
1556
                               link_reconfiguration_data_destroy_callback, data);
1557
        if (r < 0)
1,163✔
1558
                goto failed;
×
1559

1560
        TAKE_PTR(data);
1,163✔
1561
        if (counter)
1,163✔
1562
                (*counter)++;
1,033✔
1563

1564
        if (link->state == LINK_STATE_FAILED)
1,163✔
UNCOV
1565
                link_set_state(link, LINK_STATE_INITIALIZED);
×
1566

1567
        return 1; /* 1 means the interface will be reconfigured, and bus method will be replied later. */
1568

1569
failed:
1570
        log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
1571
        link_enter_failed(link);
×
1572
        return r;
1573
}
1574

1575
static int link_initialized_and_synced(Link *link) {
2,402✔
1576
        int r;
2,402✔
1577

1578
        assert(link);
2,402✔
1579
        assert(link->manager);
2,402✔
1580

1581
        if (link->state == LINK_STATE_PENDING) {
2,402✔
1582
                log_link_debug(link, "Link state is up-to-date");
2,397✔
1583
                link_set_state(link, LINK_STATE_INITIALIZED);
2,397✔
1584

1585
                r = link_new_bound_by_list(link);
2,397✔
1586
                if (r < 0)
2,397✔
1587
                        return r;
1588

1589
                r = link_handle_bound_by_list(link);
2,397✔
1590
                if (r < 0)
2,397✔
1591
                        return r;
1592
        }
1593

1594
        return link_reconfigure_impl(link, /* flags = */ 0);
2,402✔
1595
}
1596

1597
static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2,339✔
1598
        int r;
2,339✔
1599

1600
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
2,339✔
1601
        if (r <= 0)
2,339✔
1602
                return r;
1603

1604
        r = link_initialized_and_synced(link);
2,336✔
1605
        if (r < 0)
2,336✔
UNCOV
1606
                link_enter_failed(link);
×
1607

1608
        return 0;
1609
}
1610

1611
static int link_initialized(Link *link, sd_device *device) {
2,433✔
1612
        int r;
2,433✔
1613

1614
        assert(link);
2,433✔
1615
        assert(device);
2,433✔
1616

1617
        /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1618
         * or sysattrs) may be outdated. */
1619
        device_unref_and_replace(link->dev, device);
2,433✔
1620

1621
        if (link->dhcp_client) {
2,433✔
1622
                r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev);
×
1623
                if (r < 0)
×
1624
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv4 client, ignoring: %m");
×
1625
        }
1626

1627
        if (link->dhcp6_client) {
2,433✔
1628
                r = sd_dhcp6_client_attach_device(link->dhcp6_client, link->dev);
×
1629
                if (r < 0)
×
1630
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv6 client, ignoring: %m");
×
1631
        }
1632

1633
        r = link_set_sr_iov_ifindices(link);
2,433✔
1634
        if (r < 0)
2,433✔
1635
                log_link_warning_errno(link, r, "Failed to manage SR-IOV PF and VF ports, ignoring: %m");
×
1636

1637
        if (link->state != LINK_STATE_PENDING)
2,433✔
1638
                return link_reconfigure(link, /* flags = */ 0);
94✔
1639

1640
        log_link_debug(link, "udev initialized link");
2,339✔
1641

1642
        /* udev has initialized the link, but we don't know if we have yet
1643
         * processed the NEWLINK messages with the latest state. Do a GETLINK,
1644
         * when it returns we know that the pending NEWLINKs have already been
1645
         * processed and that we are up-to-date */
1646

1647
        return link_call_getlink(link, link_initialized_handler);
2,339✔
1648
}
1649

1650
int link_check_initialized(Link *link) {
2,577✔
1651
        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
2,577✔
1652
        int r;
2,577✔
1653

1654
        assert(link);
2,577✔
1655

1656
        if (!udev_available())
2,577✔
1657
                return link_initialized_and_synced(link);
66✔
1658

1659
        /* udev should be around */
1660
        r = sd_device_new_from_ifindex(&device, link->ifindex);
2,511✔
1661
        if (r < 0) {
2,511✔
1662
                log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
3✔
1663
                return 0;
3✔
1664
        }
1665

1666
        r = device_is_processed(device);
2,508✔
1667
        if (r < 0)
2,508✔
1668
                return log_link_warning_errno(link, r, "Could not determine whether the device is processed by udevd: %m");
×
1669
        if (r == 0) {
2,508✔
1670
                /* not yet ready */
1671
                log_link_debug(link, "link pending udev initialization...");
842✔
1672
                return 0;
842✔
1673
        }
1674

1675
        r = device_is_renaming(device);
1,666✔
1676
        if (r < 0)
1,666✔
1677
                return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
×
1678
        if (r > 0) {
1,666✔
1679
                log_link_debug(link, "Interface is being renamed, pending initialization.");
×
1680
                return 0;
×
1681
        }
1682

1683
        return link_initialized(link, device);
1,666✔
1684
}
1685

1686
int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t action) {
1,742✔
1687
        int r, ifindex;
1,742✔
1688
        const char *s;
1,742✔
1689
        Link *link;
1,742✔
1690

1691
        assert(m);
1,742✔
1692
        assert(device);
1,742✔
1693

1694
        r = sd_device_get_ifindex(device, &ifindex);
1,742✔
1695
        if (r < 0)
1,742✔
1696
                return log_device_debug_errno(device, r, "Failed to get ifindex: %m");
×
1697

1698
        r = link_get_by_index(m, ifindex, &link);
1,742✔
1699
        if (r < 0) {
1,742✔
1700
                /* This error is not critical, as the corresponding rtnl message may be received later. */
1701
                log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
618✔
1702
                return 0;
618✔
1703
        }
1704

1705
        /* Let's unref the sd-device object assigned to the corresponding Link object, but keep the Link
1706
         * object here. It will be removed only when rtnetlink says so. */
1707
        if (action == SD_DEVICE_REMOVE) {
1,124✔
1708
                link->dev = sd_device_unref(link->dev);
348✔
1709
                return 0;
348✔
1710
        }
1711

1712
        r = device_is_renaming(device);
776✔
1713
        if (r < 0)
776✔
1714
                return log_device_debug_errno(device, r, "Failed to determine if the device is renaming or not: %m");
×
1715
        if (r > 0) {
776✔
1716
                log_device_debug(device, "Device is renaming, waiting for the interface to be renamed.");
9✔
1717
                /* TODO:
1718
                 * What happens when a device is initialized, then soon renamed after that? When we detect
1719
                 * such, maybe we should cancel or postpone all queued requests for the interface. */
1720
                return 0;
9✔
1721
        }
1722

1723
        r = sd_device_get_property_value(device, "ID_NET_MANAGED_BY", &s);
767✔
1724
        if (r < 0 && r != -ENOENT)
767✔
1725
                log_device_debug_errno(device, r, "Failed to get ID_NET_MANAGED_BY udev property, ignoring: %m");
×
1726
        if (r >= 0 && !streq(s, "io.systemd.Network")) {
767✔
1727
                log_device_debug(device, "Interface is requested to be managed by '%s', not managing the interface.", s);
×
1728
                link_set_state(link, LINK_STATE_UNMANAGED);
×
1729
                return 0;
×
1730
        }
1731

1732
        r = link_initialized(link, device);
767✔
1733
        if (r < 0)
767✔
1734
                link_enter_failed(link);
×
1735

1736
        return 0;
1737
}
1738

1739
static int link_carrier_gained(Link *link) {
1,158✔
1740
        LinkReconfigurationFlag flags = 0;
1,158✔
1741
        int r;
1,158✔
1742

1743
        assert(link);
1,158✔
1744

1745
        r = event_source_disable(link->carrier_lost_timer);
1,158✔
1746
        if (r < 0)
1,158✔
1747
                log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
×
1748

1749
        /* Process BindCarrier= setting specified by other interfaces. This is independent of the .network
1750
         * file assigned to this interface, but depends on .network files assigned to other interfaces.
1751
         * Hence, this can and should be called earlier. */
1752
        r = link_handle_bound_by_list(link);
1,158✔
1753
        if (r < 0)
1,158✔
1754
                return r;
1755

1756
        /* If a wireless interface was connected to an access point, and the SSID is changed (that is,
1757
         * both previous_ssid and ssid are non-NULL), then the connected wireless network could be
1758
         * changed. So, always reconfigure the link. Which means e.g. the DHCP client will be
1759
         * restarted, and the correct network information will be gained.
1760
         *
1761
         * However, do not reconfigure the wireless interface forcibly if it was not connected to any
1762
         * access points previously (previous_ssid is NULL in this case). As, a .network file may be
1763
         * already assigned to the interface (in that case, the .network file does not have the SSID=
1764
         * setting in the [Match] section), and the interface is already being configured. Of course,
1765
         * there may exist another .network file with higher priority and a matching SSID= setting. But
1766
         * in that case, link_reconfigure_impl() can handle that without any flags.
1767
         *
1768
         * For non-wireless interfaces, we have no way to detect the connected network change. So,
1769
         * we do not set any flags here. Note, both ssid and previous_ssid are NULL in that case. */
1770
        if (link->previous_ssid && !streq_ptr(link->previous_ssid, link->ssid))
1,158✔
1771
                flags |= LINK_RECONFIGURE_UNCONDITIONALLY | LINK_RECONFIGURE_CLEANLY;
×
1772
        link->previous_ssid = mfree(link->previous_ssid);
1,158✔
1773

1774
        /* AP and P2P-GO interfaces may have a new SSID - update the link properties in case a new .network
1775
         * profile wants to match on it with SSID= in its [Match] section.
1776
         */
1777
        if (IN_SET(link->wlan_iftype, NL80211_IFTYPE_AP, NL80211_IFTYPE_P2P_GO)) {
1,158✔
1778
                r = link_get_wlan_interface(link);
×
1779
                if (r < 0)
×
1780
                        return r;
1781
        }
1782

1783
        /* At this stage, both wlan and link information should be up-to-date. Hence, it is not necessary to
1784
         * call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or NL80211_CMD_GET_STATION commands, and simply call
1785
         * link_reconfigure_impl(). Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1786
        r = link_reconfigure_impl(link, flags);
1,158✔
1787
        if (r != 0)
1,158✔
1788
                return r;
1789

1790
        if (link->iftype == ARPHRD_CAN)
1,158✔
1791
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1792
                return 0;
1793

1794
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
1,154✔
1795
                r = link_acquire_dynamic_conf(link);
609✔
1796
                if (r < 0)
609✔
1797
                        return r;
1798

1799
                r = link_request_static_configs(link);
609✔
1800
                if (r < 0)
609✔
1801
                        return r;
×
1802
        }
1803

1804
        return 0;
1805
}
1806

1807
static int link_carrier_lost_impl(Link *link) {
670✔
1808
        int ret = 0;
670✔
1809

1810
        assert(link);
670✔
1811

1812
        link->previous_ssid = mfree(link->previous_ssid);
670✔
1813

1814
        ret = link_handle_bound_by_list(link);
670✔
1815

1816
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
670✔
1817
                return ret;
670✔
1818

1819
        if (!link->network)
670✔
1820
                return ret;
1821

1822
        RET_GATHER(ret, link_stop_engines(link, /* may_keep_dynamic = */ false));
596✔
1823
        RET_GATHER(ret, link_drop_static_config(link));
596✔
1824

1825
        return ret;
1826
}
1827

1828
static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
×
1829
        Link *link = ASSERT_PTR(userdata);
×
1830
        int r;
×
1831

1832
        r = link_carrier_lost_impl(link);
×
1833
        if (r < 0) {
×
1834
                log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
×
1835
                link_enter_failed(link);
×
1836
        }
1837

1838
        return 0;
×
1839
}
1840

1841
static int link_carrier_lost(Link *link) {
686✔
1842
        uint16_t dhcp_mtu;
686✔
1843
        usec_t usec;
686✔
1844

1845
        assert(link);
686✔
1846

1847
        if (link->iftype == ARPHRD_CAN)
686✔
1848
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1849
                usec = 0;
1850

1851
        else if (!link->network)
682✔
1852
                usec = 0;
1853

1854
        else if (link->network->ignore_carrier_loss_set)
608✔
1855
                /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1856
                usec = link->network->ignore_carrier_loss_usec;
12✔
1857

1858
        else if (link->network->bond && link->wlan_iftype > 0)
596✔
1859
                /* Enslaving wlan interface to a bond disconnects from the connected AP, and causes its
1860
                 * carrier to be lost. See #19832. */
1861
                usec = 3 * USEC_PER_SEC;
1862

1863
        else if (link->network->dhcp_use_mtu &&
596✔
1864
                 link->dhcp_lease &&
4✔
1865
                 sd_dhcp_lease_get_mtu(link->dhcp_lease, &dhcp_mtu) >= 0 &&
2✔
1866
                 dhcp_mtu != link->original_mtu)
1✔
1867
                /* Some drivers reset interfaces when changing MTU. Resetting interfaces by the static
1868
                 * MTU should not cause any issues, as MTU is changed only once. However, setting MTU
1869
                 * through DHCP lease causes an infinite loop of resetting the interface. See #18738. */
1870
                usec = 5 * USEC_PER_SEC;
1871

1872
        else
1873
                /* Otherwise, use the implied default value. */
1874
                usec = link->network->ignore_carrier_loss_usec;
595✔
1875

1876
        if (usec == USEC_INFINITY)
607✔
1877
                return 0;
686✔
1878

1879
        if (usec == 0)
671✔
1880
                return link_carrier_lost_impl(link);
670✔
1881

1882
        return event_reset_time_relative(link->manager->event,
1✔
1883
                                         &link->carrier_lost_timer,
1884
                                         CLOCK_BOOTTIME,
1885
                                         usec,
1886
                                         0,
1887
                                         link_carrier_lost_handler,
1888
                                         link,
1889
                                         0,
1890
                                         "link-carrier-loss",
1891
                                         true);
1892
}
1893

1894
static int link_admin_state_up(Link *link) {
1,132✔
1895
        assert(link);
1,132✔
1896

1897
        /* This is called every time an interface admin state changes to up;
1898
         * specifically, when IFF_UP flag changes from unset to set. */
1899

1900
        if (!link->network)
1,132✔
1901
                return 0;
1902

1903
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
584✔
1904
                log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
4✔
1905
                return link_request_to_bring_up_or_down(link, /* up = */ false);
4✔
1906
        }
1907

1908
        /* We set the ipv6 mtu after the device mtu, but the kernel resets
1909
         * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1910
        (void) link_set_ipv6_mtu(link, LOG_INFO);
580✔
1911

1912
        return 0;
580✔
1913
}
1914

1915
static int link_admin_state_down(Link *link) {
658✔
1916
        assert(link);
658✔
1917

1918
        link_forget_nexthops(link);
658✔
1919
        link_forget_routes(link);
658✔
1920

1921
        if (!link->network)
658✔
1922
                return 0;
1923

1924
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
585✔
1925
                log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
9✔
1926
                return link_request_to_bring_up_or_down(link, /* up = */ true);
9✔
1927
        }
1928

1929
        return 0;
1930
}
1931

1932
static bool link_is_enslaved(Link *link) {
6,542✔
1933
        if (link->flags & IFF_SLAVE)
6,542✔
1934
                return true;
1935

1936
        if (link->master_ifindex > 0)
6,501✔
1937
                return true;
224✔
1938

1939
        return false;
1940
}
1941

1942
void link_update_operstate(Link *link, bool also_update_master) {
12,283✔
1943
        LinkOperationalState operstate;
12,283✔
1944
        LinkCarrierState carrier_state;
12,283✔
1945
        LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
12,283✔
1946
        LinkOnlineState online_state;
12,283✔
1947
        _cleanup_strv_free_ char **p = NULL;
12,283✔
1948
        bool changed = false;
12,283✔
1949

1950
        assert(link);
12,283✔
1951

1952
        if (link->kernel_operstate == IF_OPER_DORMANT)
12,283✔
1953
                carrier_state = LINK_CARRIER_STATE_DORMANT;
1954
        else if (link_has_carrier(link)) {
12,283✔
1955
                if (link_is_enslaved(link))
6,542✔
1956
                        carrier_state = LINK_CARRIER_STATE_ENSLAVED;
1957
                else
1958
                        carrier_state = LINK_CARRIER_STATE_CARRIER;
1959
        } else if (link->flags & IFF_UP)
5,741✔
1960
                carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
1961
        else
1962
                carrier_state = LINK_CARRIER_STATE_OFF;
1963

1964
        if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
1965
                Link *slave;
6,542✔
1966

1967
                SET_FOREACH(slave, link->slaves) {
6,757✔
1968
                        link_update_operstate(slave, false);
215✔
1969

1970
                        if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
215✔
1971
                                carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
52✔
1972
                }
1973
        }
1974

1975
        link_get_address_states(link, &ipv4_address_state, &ipv6_address_state, &address_state);
12,283✔
1976

1977
        /* Mapping of address and carrier state vs operational state
1978
         *                                                     carrier state
1979
         *                          | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
1980
         *                 ------------------------------------------------------------------------------
1981
         *                 off      | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
1982
         * address_state   degraded | off | no-carrier | dormant | degraded         | degraded | enslaved
1983
         *                 routable | off | no-carrier | dormant | routable         | routable | routable
1984
         */
1985

1986
        if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_ROUTABLE)
12,283✔
1987
                operstate = LINK_OPERSTATE_ROUTABLE;
1988
        else if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_DEGRADED)
23✔
1989
                operstate = LINK_OPERSTATE_DEGRADED;
1990
        else if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
12,251✔
1991
                operstate = (LinkOperationalState) carrier_state;
1992
        else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
4,266✔
1993
                operstate = LINK_OPERSTATE_ROUTABLE;
1994
        else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1,284✔
1995
                operstate = LINK_OPERSTATE_DEGRADED;
1996
        else
1997
                operstate = LINK_OPERSTATE_ENSLAVED;
18✔
1998

1999
        LinkOperationalStateRange req;
12,283✔
2000
        link_required_operstate_for_online(link, &req);
12,283✔
2001

2002
        /* Only determine online state for managed links with RequiredForOnline=yes */
2003
        if (!link->network || !link->network->required_for_online)
12,283✔
2004
                online_state = _LINK_ONLINE_STATE_INVALID;
2005

2006
        else if (!operational_state_is_in_range(operstate, &req))
8,035✔
2007
                online_state = LINK_ONLINE_STATE_OFFLINE;
2008

2009
        else {
2010
                AddressFamily required_family = link_required_family_for_online(link);
4,186✔
2011
                bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
4,186✔
2012
                bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
4,186✔
2013

2014
                /* The operational state is within the range required for online.
2015
                 * If a particular address family is also required, we might revert
2016
                 * to offline in the blocks below. */
2017
                online_state = LINK_ONLINE_STATE_ONLINE;
4,186✔
2018

2019
                if (req.min >= LINK_OPERSTATE_DEGRADED) {
4,186✔
2020
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
3,999✔
2021
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2022
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
3,999✔
2023
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2024
                }
2025

2026
                if (req.min >= LINK_OPERSTATE_ROUTABLE) {
4,186✔
2027
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2028
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2029
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2030
                                online_state = LINK_ONLINE_STATE_OFFLINE;
3✔
2031
                }
2032
        }
2033

2034
        if (link->carrier_state != carrier_state) {
12,283✔
2035
                link->carrier_state = carrier_state;
4,299✔
2036
                changed = true;
4,299✔
2037
                if (strv_extend(&p, "CarrierState") < 0)
4,299✔
2038
                        log_oom();
×
2039
        }
2040

2041
        if (link->address_state != address_state) {
12,283✔
2042
                link->address_state = address_state;
1,802✔
2043
                changed = true;
1,802✔
2044
                if (strv_extend(&p, "AddressState") < 0)
1,802✔
2045
                        log_oom();
×
2046
        }
2047

2048
        if (link->ipv4_address_state != ipv4_address_state) {
12,283✔
2049
                link->ipv4_address_state = ipv4_address_state;
852✔
2050
                changed = true;
852✔
2051
                if (strv_extend(&p, "IPv4AddressState") < 0)
852✔
2052
                        log_oom();
×
2053
        }
2054

2055
        if (link->ipv6_address_state != ipv6_address_state) {
12,283✔
2056
                link->ipv6_address_state = ipv6_address_state;
1,527✔
2057
                changed = true;
1,527✔
2058
                if (strv_extend(&p, "IPv6AddressState") < 0)
1,527✔
2059
                        log_oom();
×
2060
        }
2061

2062
        if (link->operstate != operstate) {
12,283✔
2063
                link->operstate = operstate;
5,204✔
2064
                changed = true;
5,204✔
2065
                if (strv_extend(&p, "OperationalState") < 0)
5,204✔
2066
                        log_oom();
×
2067
        }
2068

2069
        if (link->online_state != online_state) {
12,283✔
2070
                link->online_state = online_state;
1,648✔
2071
                changed = true;
1,648✔
2072
                if (strv_extend(&p, "OnlineState") < 0)
1,648✔
2073
                        log_oom();
×
2074
        }
2075

2076
        if (p)
12,283✔
2077
                link_send_changed_strv(link, p);
7,225✔
2078
        if (changed)
12,283✔
2079
                link_dirty(link);
7,225✔
2080

2081
        if (also_update_master) {
12,283✔
2082
                Link *master;
12,068✔
2083

2084
                if (link_get_master(link, &master) >= 0)
12,068✔
2085
                        link_update_operstate(master, true);
147✔
2086
        }
2087
}
12,283✔
2088

2089
#define FLAG_STRING(string, flag, old, new)                      \
2090
        (((old ^ new) & flag)                                    \
2091
         ? ((old & flag) ? (" -" string) : (" +" string))        \
2092
         : "")
2093

2094
static int link_update_flags(Link *link, sd_netlink_message *message) {
9,383✔
2095
        bool link_was_admin_up, had_carrier;
9,383✔
2096
        uint8_t operstate;
9,383✔
2097
        unsigned flags;
9,383✔
2098
        int r;
9,383✔
2099

2100
        assert(link);
9,383✔
2101
        assert(message);
9,383✔
2102

2103
        r = sd_rtnl_message_link_get_flags(message, &flags);
9,383✔
2104
        if (r < 0)
9,383✔
2105
                return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
×
2106

2107
        r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
9,383✔
2108
        if (r == -ENODATA)
9,383✔
2109
                /* If we got a message without operstate, assume the state was unchanged. */
2110
                operstate = link->kernel_operstate;
×
2111
        else if (r < 0)
9,383✔
2112
                return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
×
2113

2114
        if (link->flags == flags && link->kernel_operstate == operstate)
9,383✔
2115
                return 0;
2116

2117
        if (link->flags != flags) {
4,274✔
2118
                unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
4,274✔
2119

2120
                log_link_debug(link, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
11,752✔
2121
                               FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
2122
                               FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
2123
                               FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
2124
                               FLAG_STRING("UP", IFF_UP, link->flags, flags),
2125
                               FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
2126
                               FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
2127
                               FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
2128
                               FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
2129
                               FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
2130
                               FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
2131
                               FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
2132
                               FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
2133
                               FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
2134
                               FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
2135
                               FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
2136
                               FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
2137
                               FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
2138
                               FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
2139
                               FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
2140

2141
                unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
4,274✔
2142
                                  IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
2143
                                  IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
2144
                                  IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
2145
                                  IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
2146
                                  IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
2147
                unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
4,274✔
2148
                unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
4,274✔
2149

2150
                if (unknown_flags_added)
4,274✔
2151
                        log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
×
2152

2153
                if (unknown_flags_removed)
4,274✔
2154
                        log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
×
2155
        }
2156

2157
        link_was_admin_up = link->flags & IFF_UP;
4,274✔
2158
        had_carrier = link_has_carrier(link);
4,274✔
2159

2160
        link->flags = flags;
4,274✔
2161
        link->kernel_operstate = operstate;
4,274✔
2162

2163
        link_update_operstate(link, true);
4,274✔
2164

2165
        if (!link_was_admin_up && (link->flags & IFF_UP)) {
4,274✔
2166
                log_link_info(link, "Link UP");
1,132✔
2167

2168
                r = link_admin_state_up(link);
1,132✔
2169
                if (r < 0)
1,132✔
2170
                        return r;
2171
        } else if (link_was_admin_up && !(link->flags & IFF_UP)) {
1,037✔
2172
                log_link_info(link, "Link DOWN");
658✔
2173

2174
                r = link_admin_state_down(link);
658✔
2175
                if (r < 0)
658✔
2176
                        return r;
2177
        }
2178

2179
        if (!had_carrier && link_has_carrier(link)) {
4,274✔
2180
                log_link_info(link, "Gained carrier");
1,158✔
2181

2182
                r = link_carrier_gained(link);
1,158✔
2183
                if (r < 0)
1,158✔
2184
                        return r;
×
2185
        } else if (had_carrier && !link_has_carrier(link)) {
709✔
2186
                log_link_info(link, "Lost carrier");
686✔
2187

2188
                r = link_carrier_lost(link);
686✔
2189
                if (r < 0)
686✔
2190
                        return r;
×
2191
        }
2192

2193
        return 0;
2194
}
2195

2196
static int link_update_master(Link *link, sd_netlink_message *message) {
9,383✔
2197
        int master_ifindex, r;
9,383✔
2198

2199
        assert(link);
9,383✔
2200
        assert(message);
9,383✔
2201

2202
        r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
9,383✔
2203
        if (r == -ENODATA)
9,383✔
2204
                return 0;
9,383✔
2205
        if (r < 0)
429✔
2206
                return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
×
2207

2208
        if (master_ifindex == link->ifindex)
429✔
2209
                master_ifindex = 0;
7✔
2210

2211
        if (master_ifindex != link->master_ifindex) {
429✔
2212
                if (link->master_ifindex == 0)
34✔
2213
                        log_link_debug(link, "Attached to master interface: %i", master_ifindex);
34✔
2214
                else if (master_ifindex == 0)
×
2215
                        log_link_debug(link, "Detached from master interface: %i", link->master_ifindex);
×
2216
                else
2217
                        log_link_debug(link, "Master interface changed: %i %s %i", link->master_ifindex,
×
2218
                                       glyph(GLYPH_ARROW_RIGHT), master_ifindex);
2219

2220
                link_drop_from_master(link);
34✔
2221
                link->master_ifindex = master_ifindex;
34✔
2222
        }
2223

2224
        r = link_append_to_master(link);
429✔
2225
        if (r < 0)
429✔
2226
                return log_link_debug_errno(link, r, "Failed to append link to master: %m");
×
2227

2228
        return 0;
2229
}
2230

2231
static int link_update_driver(Link *link, sd_netlink_message *message) {
9,383✔
2232
        int r;
9,383✔
2233

2234
        assert(link);
9,383✔
2235
        assert(link->manager);
9,383✔
2236
        assert(message);
9,383✔
2237

2238
        /* Driver is already read. Assuming the driver is never changed. */
2239
        if (link->ethtool_driver_read)
9,383✔
2240
                return 0;
2241

2242
        /* When udevd is running, read the driver after the interface is initialized by udevd.
2243
         * Otherwise, ethtool may not work correctly. See issue #22538.
2244
         * When udevd is not running, read the value when the interface is detected. */
2245
        if (udev_available() && !link->dev)
5,261✔
2246
                return 0;
2247

2248
        link->ethtool_driver_read = true;
2,399✔
2249

2250
        r = ethtool_get_driver(&link->manager->ethtool_fd, link->ifname, &link->driver);
2,399✔
2251
        if (r < 0) {
2,399✔
2252
                log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
467✔
2253
                return 0;
467✔
2254
        }
2255

2256
        log_link_debug(link, "Found driver: %s", strna(link->driver));
1,932✔
2257

2258
        if (streq_ptr(link->driver, "dsa")) {
1,932✔
2259
                uint32_t dsa_master_ifindex = 0;
×
2260

2261
                r = sd_netlink_message_read_u32(message, IFLA_LINK, &dsa_master_ifindex);
×
2262
                if (r < 0 && r != -ENODATA)
×
2263
                        return log_link_debug_errno(link, r, "rtnl: failed to read ifindex of the DSA master interface: %m");
×
2264

2265
                if (dsa_master_ifindex > INT_MAX) {
×
2266
                        log_link_debug(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
×
2267
                                       dsa_master_ifindex);
2268
                        dsa_master_ifindex = 0;
×
2269
                }
2270

2271
                link->dsa_master_ifindex = (int) dsa_master_ifindex;
×
2272
        }
2273

2274
        return 1; /* needs reconfigure */
2275
}
2276

2277
static int link_update_permanent_hardware_address_from_ethtool(Link *link, sd_netlink_message *message) {
4,981✔
2278
        int r;
4,981✔
2279

2280
        assert(link);
4,981✔
2281
        assert(link->manager);
4,981✔
2282
        assert(message);
4,981✔
2283

2284
        if (link->ethtool_permanent_hw_addr_read)
4,981✔
2285
                return 0;
2286

2287
        /* When udevd is running, read the permanent hardware address after the interface is
2288
         * initialized by udevd. Otherwise, ethtool may not work correctly. See issue #22538.
2289
         * When udevd is not running, read the value when the interface is detected. */
2290
        if (udev_available() && !link->dev)
4,978✔
2291
                return 0;
2292

2293
        /* If the interface does not have a hardware address, then it will not have a permanent address either. */
2294
        r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, NULL);
2,301✔
2295
        if (r == -ENODATA)
2,301✔
2296
                return 0;
2297
        if (r < 0)
2,159✔
2298
                return log_link_debug_errno(link, r, "Failed to read IFLA_ADDRESS attribute: %m");
×
2299

2300
        link->ethtool_permanent_hw_addr_read = true;
2,159✔
2301

2302
        r = ethtool_get_permanent_hw_addr(&link->manager->ethtool_fd, link->ifname, &link->permanent_hw_addr);
2,159✔
2303
        if (r < 0)
2,159✔
2304
                log_link_debug_errno(link, r, "Permanent hardware address not found, continuing without: %m");
2✔
2305

2306
        return 0;
2307
}
2308

2309
static int link_update_permanent_hardware_address(Link *link, sd_netlink_message *message) {
9,383✔
2310
        int r;
9,383✔
2311

2312
        assert(link);
9,383✔
2313
        assert(link->manager);
9,383✔
2314
        assert(message);
9,383✔
2315

2316
        if (link->permanent_hw_addr.length > 0)
9,383✔
2317
                return 0;
2318

2319
        r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
5,160✔
2320
        if (r < 0) {
5,160✔
2321
                if (r != -ENODATA)
4,981✔
2322
                        return log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute: %m");
×
2323

2324
                /* Fallback to ethtool for kernels older than v5.6 (f74877a5457d34d604dba6dbbb13c4c05bac8b93). */
2325
                r = link_update_permanent_hardware_address_from_ethtool(link, message);
4,981✔
2326
                if (r < 0)
4,981✔
2327
                        return r;
2328
        }
2329

2330
        if (link->permanent_hw_addr.length > 0)
5,160✔
2331
                log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
2,336✔
2332

2333
        return 1; /* needs reconfigure */
2334
}
2335

2336
static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
9,383✔
2337
        struct hw_addr_data addr;
9,383✔
2338
        int r;
9,383✔
2339

2340
        assert(link);
9,383✔
2341
        assert(message);
9,383✔
2342

2343
        r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
9,383✔
2344
        if (r < 0 && r != -ENODATA)
9,383✔
2345
                return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
×
2346

2347
        r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
9,383✔
2348
        if (r == -ENODATA)
9,383✔
2349
                return 0;
2350
        if (r < 0)
9,180✔
2351
                return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
×
2352

2353
        if (hw_addr_equal(&link->hw_addr, &addr))
9,180✔
2354
                return 0;
2355

2356
        if (link->hw_addr.length == 0)
2,692✔
2357
                log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
2,518✔
2358
        else {
2359
                log_link_debug(link, "Hardware address is changed: %s %s %s",
174✔
2360
                               HW_ADDR_TO_STR(&link->hw_addr),
2361
                               glyph(GLYPH_ARROW_RIGHT),
2362
                               HW_ADDR_TO_STR(&addr));
2363

2364
                hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
174✔
2365
        }
2366

2367
        link->hw_addr = addr;
2,692✔
2368

2369
        if (!hw_addr_is_null(&link->hw_addr)) {
2,692✔
2370
                r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
974✔
2371
                if (r == -EEXIST && streq_ptr(link->kind, "bond"))
974✔
2372
                        /* bonding master and its slaves have the same hardware address. */
2373
                        r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
2✔
2374
                if (r < 0)
974✔
2375
                        log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
36✔
2376
        }
2377

2378
        r = ipv4acd_update_mac(link);
2,692✔
2379
        if (r < 0)
2,692✔
2380
                return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
×
2381

2382
        r = ipv4ll_update_mac(link);
2,692✔
2383
        if (r < 0)
2,692✔
2384
                return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
×
2385

2386
        r = dhcp4_update_mac(link);
2,692✔
2387
        if (r < 0)
2,692✔
2388
                return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
×
2389

2390
        r = dhcp6_update_mac(link);
2,692✔
2391
        if (r < 0)
2,692✔
2392
                return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
×
2393

2394
        r = radv_update_mac(link);
2,692✔
2395
        if (r < 0)
2,692✔
2396
                return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
×
2397

2398
        if (link->ndisc && link->hw_addr.length == ETH_ALEN) {
2,692✔
2399
                r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
×
2400
                if (r < 0)
×
2401
                        return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
×
2402
        }
2403

2404
        if (link->lldp_rx) {
2,692✔
2405
                r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
19✔
2406
                if (r < 0)
19✔
2407
                        return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
×
2408
        }
2409

2410
        if (link->lldp_tx) {
2,692✔
2411
                r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
×
2412
                if (r < 0)
×
2413
                        return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
×
2414
        }
2415

2416
        return 1; /* needs reconfigure */
2417
}
2418

2419
static int link_update_mtu(Link *link, sd_netlink_message *message) {
9,383✔
2420
        uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
9,383✔
2421
        int r;
9,383✔
2422

2423
        assert(link);
9,383✔
2424
        assert(message);
9,383✔
2425

2426
        r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
9,383✔
2427
        if (r == -ENODATA)
9,383✔
2428
                return 0;
9,383✔
2429
        if (r < 0)
9,383✔
2430
                return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
×
2431
        if (mtu == 0)
9,383✔
2432
                return 0;
2433

2434
        r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
9,362✔
2435
        if (r < 0 && r != -ENODATA)
9,362✔
2436
                return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
×
2437

2438
        r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
9,362✔
2439
        if (r < 0 && r != -ENODATA)
9,362✔
2440
                return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
×
2441

2442
        if (max_mtu == 0)
9,362✔
2443
                max_mtu = UINT32_MAX;
6,225✔
2444

2445
        link->min_mtu = min_mtu;
9,362✔
2446
        link->max_mtu = max_mtu;
9,362✔
2447

2448
        if (link->original_mtu == 0) {
9,362✔
2449
                link->original_mtu = mtu;
2,579✔
2450
                log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2,579✔
2451
                               link->original_mtu, link->min_mtu, link->max_mtu);
2452
        }
2453

2454
        if (link->mtu == mtu)
9,362✔
2455
                return 0;
2456

2457
        if (link->mtu != 0)
2,666✔
2458
                log_link_debug(link, "MTU is changed: %"PRIu32" %s %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
87✔
2459
                               link->mtu, glyph(GLYPH_ARROW_RIGHT), mtu,
2460
                               link->min_mtu, link->max_mtu);
2461

2462
        link->mtu = mtu;
2,666✔
2463

2464
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
2,666✔
2465
                /* The kernel resets IPv6 MTU after changing device MTU. So, we need to re-set IPv6 MTU again. */
2466
                (void) link_set_ipv6_mtu_async(link);
45✔
2467

2468
        if (link->dhcp_client) {
2,666✔
2469
                r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
4✔
2470
                if (r < 0)
4✔
2471
                        return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
×
2472
        }
2473

2474
        if (link->radv) {
2,666✔
2475
                r = sd_radv_set_mtu(link->radv, link->mtu);
×
2476
                if (r < 0)
×
2477
                        return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
×
2478
        }
2479

2480
        return 0;
2481
}
2482

2483
static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
9,383✔
2484
        _cleanup_strv_free_ char **altnames = NULL;
9,383✔
2485
        int r;
9,383✔
2486

2487
        assert(link);
9,383✔
2488
        assert(message);
9,383✔
2489

2490
        r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
9,383✔
2491
        if (r == -ENODATA)
9,383✔
2492
                /* The message does not have IFLA_PROP_LIST container attribute. It does not mean the
2493
                 * interface has no alternative name. */
2494
                return 0;
2495
        if (r < 0)
100✔
2496
                return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
×
2497

2498
        if (strv_equal(altnames, link->alternative_names))
100✔
2499
                return 0;
2500

2501
        STRV_FOREACH(n, link->alternative_names)
52✔
2502
                hashmap_remove(link->manager->links_by_name, *n);
11✔
2503

2504
        strv_free_and_replace(link->alternative_names, altnames);
41✔
2505

2506
        STRV_FOREACH(n, link->alternative_names) {
90✔
2507
                r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
49✔
2508
                if (r < 0)
49✔
2509
                        return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
×
2510
        }
2511

2512
        return 1; /* needs reconfigure */
2513
}
2514

2515
static int link_update_name(Link *link, sd_netlink_message *message) {
9,383✔
2516
        char ifname_from_index[IF_NAMESIZE];
9,383✔
2517
        const char *ifname;
9,383✔
2518
        int r;
9,383✔
2519

2520
        assert(link);
9,383✔
2521
        assert(message);
9,383✔
2522

2523
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
9,383✔
2524
        if (r == -ENODATA)
9,383✔
2525
                /* Hmm?? But ok. */
2526
                return 0;
9,383✔
2527
        if (r < 0)
9,383✔
2528
                return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
×
2529

2530
        if (streq(ifname, link->ifname))
9,383✔
2531
                return 0;
2532

2533
        r = format_ifname(link->ifindex, ifname_from_index);
21✔
2534
        if (r < 0)
21✔
UNCOV
2535
                return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
×
2536

2537
        if (!streq(ifname, ifname_from_index)) {
21✔
UNCOV
2538
                log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "
×
2539
                               "with the name currently configured on the actual interface '%s'. Ignoring.",
2540
                               ifname, ifname_from_index);
UNCOV
2541
                return 0;
×
2542
        }
2543

2544
        log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
21✔
2545

2546
        hashmap_remove(link->manager->links_by_name, link->ifname);
21✔
2547

2548
        r = free_and_strdup(&link->ifname, ifname);
21✔
2549
        if (r < 0)
21✔
2550
                return log_oom_debug();
×
2551

2552
        r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
21✔
2553
        if (r < 0)
21✔
2554
                return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
×
2555

2556
        if (link->dhcp_client) {
21✔
2557
                r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname);
×
2558
                if (r < 0)
×
2559
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m");
×
2560
        }
2561

2562
        if (link->dhcp6_client) {
21✔
2563
                r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
×
2564
                if (r < 0)
×
2565
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCPv6 client: %m");
×
2566
        }
2567

2568
        if (link->ndisc) {
21✔
2569
                r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
×
2570
                if (r < 0)
×
2571
                        return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
×
2572
        }
2573

2574
        if (link->dhcp_server) {
21✔
2575
                r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
×
2576
                if (r < 0)
×
2577
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
×
2578
        }
2579

2580
        if (link->radv) {
21✔
2581
                r = sd_radv_set_ifname(link->radv, link->ifname);
×
2582
                if (r < 0)
×
2583
                        return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
×
2584
        }
2585

2586
        if (link->lldp_rx) {
21✔
2587
                r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
2✔
2588
                if (r < 0)
2✔
2589
                        return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
×
2590
        }
2591

2592
        if (link->lldp_tx) {
21✔
2593
                r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
×
2594
                if (r < 0)
×
2595
                        return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
×
2596
        }
2597

2598
        if (link->ipv4ll) {
21✔
2599
                r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
×
2600
                if (r < 0)
×
2601
                        return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
×
2602
        }
2603

2604
        r = ipv4acd_set_ifname(link);
21✔
2605
        if (r < 0)
21✔
2606
                return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
×
2607

2608
        return 1; /* needs reconfigure */
2609
}
2610

2611
static int link_update(Link *link, sd_netlink_message *message) {
9,383✔
2612
        bool needs_reconfigure = false;
9,383✔
2613
        int r;
9,383✔
2614

2615
        assert(link);
9,383✔
2616
        assert(message);
9,383✔
2617

2618
        r = link_update_name(link, message);
9,383✔
2619
        if (r < 0)
9,383✔
2620
                return r;
2621
        needs_reconfigure = needs_reconfigure || r > 0;
9,383✔
2622

2623
        r = link_update_alternative_names(link, message);
9,383✔
2624
        if (r < 0)
9,383✔
2625
                return r;
2626
        needs_reconfigure = needs_reconfigure || r > 0;
9,383✔
2627

2628
        r = link_update_mtu(link, message);
9,383✔
2629
        if (r < 0)
9,383✔
2630
                return r;
2631

2632
        r = link_update_driver(link, message);
9,383✔
2633
        if (r < 0)
9,383✔
2634
                return r;
2635
        needs_reconfigure = needs_reconfigure || r > 0;
9,383✔
2636

2637
        r = link_update_permanent_hardware_address(link, message);
9,383✔
2638
        if (r < 0)
9,383✔
2639
                return r;
2640
        needs_reconfigure = needs_reconfigure || r > 0;
9,383✔
2641

2642
        r = link_update_hardware_address(link, message);
9,383✔
2643
        if (r < 0)
9,383✔
2644
                return r;
2645
        needs_reconfigure = needs_reconfigure || r > 0;
9,383✔
2646

2647
        r = link_update_master(link, message);
9,383✔
2648
        if (r < 0)
9,383✔
2649
                return r;
2650

2651
        r = link_update_ipv6ll_addrgen_mode(link, message);
9,383✔
2652
        if (r < 0)
9,383✔
2653
                return r;
2654

2655
        r = link_update_flags(link, message);
9,383✔
2656
        if (r < 0)
9,383✔
2657
                return r;
2658

2659
        r = link_update_bridge_vlan(link, message);
9,383✔
2660
        if (r < 0)
9,383✔
2661
                return r;
2662

2663
        return needs_reconfigure;
9,383✔
2664
}
2665

2666
static Link *link_drop_or_unref(Link *link) {
×
2667
        if (!link)
×
2668
                return NULL;
2669
        if (!link->manager)
×
2670
                return link_unref(link);
×
2671
        return link_drop(link);
×
2672
}
2673

2674
DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2,579✔
2675

2676
static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
2,579✔
2677
        _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL;
2,579✔
2678
        _cleanup_(link_drop_or_unrefp) Link *link = NULL;
2,579✔
2679
        unsigned short iftype;
2,579✔
2680
        int r, ifindex;
2,579✔
2681

2682
        assert(manager);
2,579✔
2683
        assert(message);
2,579✔
2684
        assert(ret);
2,579✔
2685

2686
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2,579✔
2687
        if (r < 0)
2,579✔
2688
                return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
×
2689
        else if (ifindex <= 0)
2,579✔
2690
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
×
2691

2692
        r = sd_rtnl_message_link_get_type(message, &iftype);
2,579✔
2693
        if (r < 0)
2,579✔
2694
                return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
×
2695

2696
        r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2,579✔
2697
        if (r < 0)
2,579✔
2698
                return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
×
2699

2700
        /* check for link kind */
2701
        r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2,579✔
2702
        if (r >= 0) {
2,579✔
2703
                r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2,073✔
2704
                if (r < 0 && r != -ENODATA)
2,073✔
2705
                        return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
×
2706
                r = sd_netlink_message_exit_container(message);
2,073✔
2707
                if (r < 0)
2,073✔
2708
                        return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
×
2709
        }
2710

2711
        if (!manager->test_mode) {
2,579✔
2712
                /* Do not update state files when running in test mode. */
2713
                if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2,577✔
2714
                        return log_oom_debug();
×
2715

2716
                if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2,577✔
2717
                        return log_oom_debug();
×
2718
        }
2719

2720
        link = new(Link, 1);
2,579✔
2721
        if (!link)
2,579✔
2722
                return -ENOMEM;
2723

2724
        *link = (Link) {
2,579✔
2725
                .n_ref = 1,
2726
                .state = LINK_STATE_PENDING,
2727
                .online_state = _LINK_ONLINE_STATE_INVALID,
2728
                .automatic_reconfigure_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_SEC, .burst = 5 },
2729
                .ifindex = ifindex,
2730
                .iftype = iftype,
2731
                .ifname = TAKE_PTR(ifname),
2,579✔
2732
                .kind = TAKE_PTR(kind),
2,579✔
2733

2734
                .bridge_vlan_pvid = UINT16_MAX,
2735

2736
                .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2737

2738
                .state_file = TAKE_PTR(state_file),
2,579✔
2739
                .lease_file = TAKE_PTR(lease_file),
2,579✔
2740

2741
                .n_dns = UINT_MAX,
2742
                .dns_default_route = -1,
2743
                .llmnr = _RESOLVE_SUPPORT_INVALID,
2744
                .mdns = _RESOLVE_SUPPORT_INVALID,
2745
                .dnssec_mode = _DNSSEC_MODE_INVALID,
2746
                .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2747
        };
2748

2749
        r = hashmap_ensure_put(&manager->links_by_index, NULL, INT_TO_PTR(link->ifindex), link);
2,579✔
2750
        if (r < 0)
2,579✔
2751
                return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
×
2752

2753
        link->manager = manager;
2,579✔
2754

2755
        r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
2,579✔
2756
        if (r < 0)
2,579✔
2757
                return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
×
2758

2759
        log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
3,085✔
2760
                       link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2761

2762
        /* If contained in this set, the link is wireless and the corresponding NL80211_CMD_NEW_INTERFACE
2763
         * message arrived too early. Request the wireless link information again.
2764
         */
2765
        if (set_remove(manager->new_wlan_ifindices, INT_TO_PTR(link->ifindex))) {
2,579✔
2766
                r = link_get_wlan_interface(link);
×
2767
                if (r < 0)
×
2768
                        log_link_warning_errno(link, r, "Failed to get wireless interface, ignoring: %m");
×
2769
        }
2770

2771
        *ret = TAKE_PTR(link);
2,579✔
2772
        return 0;
2,579✔
2773
}
2774

2775
int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
5,859✔
2776
        Link *link = NULL;
5,859✔
2777
        NetDev *netdev = NULL;
5,859✔
2778
        uint16_t type;
5,859✔
2779
        const char *name;
5,859✔
2780
        int r, ifindex;
5,859✔
2781

2782
        assert(rtnl);
5,859✔
2783
        assert(message);
5,859✔
2784
        assert(manager);
5,859✔
2785

2786
        if (sd_netlink_message_is_error(message)) {
5,859✔
2787
                r = sd_netlink_message_get_errno(message);
×
2788
                if (r < 0)
×
2789
                        log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
×
2790

2791
                return 0;
×
2792
        }
2793

2794
        r = sd_netlink_message_get_type(message, &type);
5,859✔
2795
        if (r < 0) {
5,859✔
2796
                log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
×
2797
                return 0;
×
2798
        } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
5,859✔
2799
                log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
×
2800
                return 0;
×
2801
        }
2802

2803
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
5,859✔
2804
        if (r < 0) {
5,859✔
2805
                log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
×
2806
                return 0;
×
2807
        } else if (ifindex <= 0) {
5,859✔
2808
                log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
×
2809
                return 0;
×
2810
        }
2811

2812
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
5,859✔
2813
        if (r < 0) {
5,859✔
2814
                log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
×
2815
                return 0;
×
2816
        }
2817

2818
        (void) link_get_by_index(manager, ifindex, &link);
5,859✔
2819
        (void) netdev_get(manager, name, &netdev);
5,859✔
2820

2821
        switch (type) {
5,859✔
2822
        case RTM_NEWLINK:
5,046✔
2823
                if (netdev) {
5,046✔
2824
                        /* netdev exists, so make sure the ifindex matches */
2825
                        r = netdev_set_ifindex(netdev, message);
2,547✔
2826
                        if (r < 0) {
2,547✔
2827
                                log_netdev_warning_errno(netdev, r, "Could not process new link message for netdev, ignoring: %m");
×
2828
                                netdev_enter_failed(netdev);
×
2829
                                return 0;
×
2830
                        }
2831
                }
2832

2833
                if (!link) {
5,046✔
2834
                        /* link is new, so add it */
2835
                        r = link_new(manager, message, &link);
2,579✔
2836
                        if (r < 0) {
2,579✔
2837
                                log_warning_errno(r, "Could not process new link message: %m");
×
2838
                                return 0;
×
2839
                        }
2840

2841
                        r = link_update(link, message);
2,579✔
2842
                        if (r < 0) {
2,579✔
2843
                                log_link_warning_errno(link, r, "Could not process link message: %m");
×
2844
                                link_enter_failed(link);
×
2845
                                return 0;
×
2846
                        }
2847

2848
                        if (link->manager->test_mode) {
2,579✔
2849
                                log_link_debug(link, "Running in test mode, refusing to enter initialized state.");
2✔
2850
                                link_set_state(link, LINK_STATE_UNMANAGED);
2✔
2851
                                return 0;
2✔
2852
                        }
2853

2854
                        /* Do not enter initialized state if we are enumerating. */
2855
                        if (manager->enumerating)
2,577✔
2856
                                return 0;
2857

2858
                        r = link_check_initialized(link);
855✔
2859
                        if (r < 0) {
855✔
2860
                                log_link_warning_errno(link, r, "Failed to check link is initialized: %m");
×
2861
                                link_enter_failed(link);
×
2862
                                return 0;
×
2863
                        }
2864
                } else {
2865
                        r = link_update(link, message);
2,467✔
2866
                        if (r < 0) {
2,467✔
UNCOV
2867
                                log_link_warning_errno(link, r, "Could not process link message: %m");
×
UNCOV
2868
                                link_enter_failed(link);
×
UNCOV
2869
                                return 0;
×
2870
                        }
2871
                        if (r == 0)
2,467✔
2872
                                return 0;
2873

2874
                        if (link->manager->test_mode) {
436✔
2875
                                log_link_debug(link, "Running in test mode, refusing to configure interface.");
×
2876
                                link_set_state(link, LINK_STATE_UNMANAGED);
×
2877
                                return 0;
×
2878
                        }
2879

2880
                        /* Do not configure interface if we are enumerating. */
2881
                        if (manager->enumerating)
436✔
2882
                                return 0;
2883

2884
                        r = link_reconfigure_impl(link, /* flags = */ 0);
435✔
2885
                        if (r < 0) {
435✔
2886
                                log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
2887
                                link_enter_failed(link);
×
2888
                                return 0;
×
2889
                        }
2890
                }
2891
                break;
2892

2893
        case RTM_DELLINK:
813✔
2894
                link_drop(link);
813✔
2895
                netdev_drop(netdev);
813✔
2896
                break;
2897

2898
        default:
×
2899
                assert_not_reached();
×
2900
        }
2901

2902
        return 1;
2903
}
2904

2905
int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
4,343✔
2906
        uint16_t message_type;
4,343✔
2907
        int r;
4,343✔
2908

2909
        assert(m);
4,343✔
2910
        assert(link);
4,343✔
2911
        assert(error_msg);
4,343✔
2912

2913
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
4,343✔
2914
                return 0;
4,343✔
2915

2916
        r = sd_netlink_message_get_errno(m);
4,337✔
2917
        if (r < 0) {
4,337✔
2918
                log_link_message_warning_errno(link, m, r, error_msg);
×
2919
                link_enter_failed(link);
×
2920
                return 0;
×
2921
        }
2922

2923
        r = sd_netlink_message_get_type(m, &message_type);
4,337✔
2924
        if (r < 0) {
4,337✔
2925
                log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
×
2926
                return 0;
×
2927
        }
2928
        if (message_type != RTM_NEWLINK) {
4,337✔
2929
                log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
×
2930
                return 0;
×
2931
        }
2932

2933
        r = link_update(link, m);
4,337✔
2934
        if (r < 0) {
4,337✔
2935
                link_enter_failed(link);
×
2936
                return 0;
×
2937
        }
2938

2939
        return 1;
2940
}
2941

2942
int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
3,180✔
2943
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
3,180✔
2944
        int r;
3,180✔
2945

2946
        assert(link);
3,180✔
2947
        assert(link->manager);
3,180✔
2948
        assert(link->manager->rtnl);
3,180✔
2949
        assert(callback);
3,180✔
2950

2951
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
3,180✔
2952
        if (r < 0)
3,180✔
2953
                return r;
2954

2955
        r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
3,180✔
2956
                               link_netlink_destroy_callback, link);
2957
        if (r < 0)
3,180✔
2958
                return r;
2959

2960
        link_ref(link);
3,180✔
2961
        return 0;
2962
}
2963

2964
static const char* const link_state_table[_LINK_STATE_MAX] = {
2965
        [LINK_STATE_PENDING]     = "pending",
2966
        [LINK_STATE_INITIALIZED] = "initialized",
2967
        [LINK_STATE_CONFIGURING] = "configuring",
2968
        [LINK_STATE_CONFIGURED]  = "configured",
2969
        [LINK_STATE_UNMANAGED]   = "unmanaged",
2970
        [LINK_STATE_FAILED]      = "failed",
2971
        [LINK_STATE_LINGER]      = "linger",
2972
};
2973

2974
DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
29,868✔
2975

2976
int link_flags_to_string_alloc(uint32_t flags, char **ret) {
551✔
2977
        _cleanup_free_ char *str = NULL;
551✔
2978
        static const char* map[] = {
551✔
2979
                [LOG2U(IFF_UP)]          = "up",             /* interface is up. */
2980
                [LOG2U(IFF_BROADCAST)]   = "broadcast",      /* broadcast address valid. */
2981
                [LOG2U(IFF_DEBUG)]       = "debug",          /* turn on debugging. */
2982
                [LOG2U(IFF_LOOPBACK)]    = "loopback",       /* interface is a loopback net. */
2983
                [LOG2U(IFF_POINTOPOINT)] = "point-to-point", /* interface has p-p link. */
2984
                [LOG2U(IFF_NOTRAILERS)]  = "no-trailers",    /* avoid use of trailers. */
2985
                [LOG2U(IFF_RUNNING)]     = "running",        /* interface RFC2863 OPER_UP. */
2986
                [LOG2U(IFF_NOARP)]       = "no-arp",         /* no ARP protocol. */
2987
                [LOG2U(IFF_PROMISC)]     = "promiscuous",    /* receive all packets. */
2988
                [LOG2U(IFF_ALLMULTI)]    = "all-multicast",  /* receive all multicast packets. */
2989
                [LOG2U(IFF_MASTER)]      = "master",         /* master of a load balancer. */
2990
                [LOG2U(IFF_SLAVE)]       = "slave",          /* slave of a load balancer. */
2991
                [LOG2U(IFF_MULTICAST)]   = "multicast",      /* supports multicast. */
2992
                [LOG2U(IFF_PORTSEL)]     = "portsel",        /* can set media type. */
2993
                [LOG2U(IFF_AUTOMEDIA)]   = "auto-media",     /* auto media select active. */
2994
                [LOG2U(IFF_DYNAMIC)]     = "dynamic",        /* dialup device with changing addresses. */
2995
                [LOG2U(IFF_LOWER_UP)]    = "lower-up",       /* driver signals L1 up. */
2996
                [LOG2U(IFF_DORMANT)]     = "dormant",        /* driver signals dormant. */
2997
                [LOG2U(IFF_ECHO)]        = "echo",           /* echo sent packets. */
2998
        };
2999

3000
        assert(ret);
551✔
3001

3002
        for (size_t i = 0; i < ELEMENTSOF(map); i++)
11,020✔
3003
                if (BIT_SET(flags, i) && map[i])
10,469✔
3004
                        if (!strextend_with_separator(&str, ",", map[i]))
1,547✔
3005
                                return -ENOMEM;
3006

3007
        *ret = TAKE_PTR(str);
551✔
3008
        return 0;
551✔
3009
}
3010

3011
static const char * const kernel_operstate_table[] = {
3012
        [IF_OPER_UNKNOWN]        = "unknown",
3013
        [IF_OPER_NOTPRESENT]     = "not-present",
3014
        [IF_OPER_DOWN]           = "down",
3015
        [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
3016
        [IF_OPER_TESTING]        = "testing",
3017
        [IF_OPER_DORMANT]        = "dormant",
3018
        [IF_OPER_UP]             = "up",
3019
};
3020

3021
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);
551✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc