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

systemd / systemd / 13168948113

05 Feb 2025 10:37PM UTC coverage: 71.813% (-0.004%) from 71.817%
13168948113

push

github

poettering
update TODO

293032 of 408051 relevant lines covered (71.81%)

710489.93 hits per line

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

84.3
/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,869✔
77
        assert(link);
16,869✔
78
        assert(ret);
16,869✔
79

80
        if (link->network && operational_state_range_is_valid(&link->network->required_operstate_for_online))
16,869✔
81
                /* If explicitly specified, use it as is. */
82
                *ret = link->network->required_operstate_for_online;
175✔
83
        else if (link->iftype == ARPHRD_CAN)
16,694✔
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,650✔
90
                /* Bonding slaves do not support addressing. */
91
                *ret = (const LinkOperationalStateRange) {
111✔
92
                        .min = LINK_OPERSTATE_ENSLAVED,
93
                        .max = LINK_OPERSTATE_ENSLAVED,
94
                };
95
        else if (STRPTR_IN_SET(link->kind, "batadv", "bond", "bridge", "vrf"))
16,539✔
96
                /* Some of slave interfaces may be offline. */
97
                *ret = (const LinkOperationalStateRange) {
615✔
98
                        .min = LINK_OPERSTATE_DEGRADED_CARRIER,
99
                        .max = LINK_OPERSTATE_ROUTABLE,
100
                };
101
        else
102
                *ret = LINK_OPERSTATE_RANGE_DEFAULT;
15,924✔
103
}
16,869✔
104

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

108
        if (link->network && link->network->required_family_for_online >= 0)
8,855✔
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) {
801✔
124
        assert(link);
801✔
125

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

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

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

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

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

141
        if (network_has_static_ipv6_configurations(link->network))
90✔
142
                return true;
23✔
143

144
        return false;
145
}
146

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

150
        assert(link);
266✔
151

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

154
        switch (ipv6_address_state) {
266✔
155
        case LINK_ADDRESS_STATE_ROUTABLE:
156
                /* If the interface has a routable IPv6 address, then we assume yes. */
157
                return true;
266✔
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:
99✔
165
                /* No IPv6 address. */
166
                return false;
99✔
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,977✔
174
        assert(link);
56,977✔
175

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

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

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

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

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

193
        if (!link->activated)
10,974✔
194
                return false;
54✔
195

196
        return true;
197
}
198

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

203
void link_ntp_settings_clear(Link *link) {
2,557✔
204
        link->ntp = strv_free(link->ntp);
2,557✔
205
}
2,557✔
206

207
void link_dns_settings_clear(Link *link) {
2,568✔
208
        if (link->n_dns != UINT_MAX)
2,568✔
209
                for (unsigned i = 0; i < link->n_dns; i++)
7✔
210
                        in_addr_full_free(link->dns[i]);
5✔
211
        link->dns = mfree(link->dns);
2,568✔
212
        link->n_dns = UINT_MAX;
2,568✔
213

214
        link->search_domains = ordered_set_free(link->search_domains);
2,568✔
215
        link->route_domains = ordered_set_free(link->route_domains);
2,568✔
216

217
        link->dns_default_route = -1;
2,568✔
218
        link->llmnr = _RESOLVE_SUPPORT_INVALID;
2,568✔
219
        link->mdns = _RESOLVE_SUPPORT_INVALID;
2,568✔
220
        link->dnssec_mode = _DNSSEC_MODE_INVALID;
2,568✔
221
        link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID;
2,568✔
222

223
        link->dnssec_negative_trust_anchors = set_free_free(link->dnssec_negative_trust_anchors);
2,568✔
224
}
2,568✔
225

226
static void link_free_engines(Link *link) {
4,389✔
227
        if (!link)
4,389✔
228
                return;
229

230
        link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
4,389✔
231

232
        link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
4,389✔
233
        link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
4,389✔
234
        link->dhcp4_6rd_tunnel_name = mfree(link->dhcp4_6rd_tunnel_name);
4,389✔
235

236
        link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx);
4,389✔
237
        link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
4,389✔
238

239
        link->ipv4acd_by_address = hashmap_free(link->ipv4acd_by_address);
4,389✔
240

241
        link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
4,389✔
242

243
        link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
4,389✔
244
        link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
4,389✔
245

246
        link->ndisc = sd_ndisc_unref(link->ndisc);
4,389✔
247
        link->ndisc_expire = sd_event_source_disable_unref(link->ndisc_expire);
4,389✔
248
        ndisc_flush(link);
4,389✔
249

250
        link->radv = sd_radv_unref(link->radv);
4,389✔
251
}
252

253
static Link *link_free(Link *link) {
2,556✔
254
        assert(link);
2,556✔
255

256
        (void) link_clear_sysctl_shadows(link);
2,556✔
257

258
        link_ntp_settings_clear(link);
2,556✔
259
        link_dns_settings_clear(link);
2,556✔
260

261
        link->neighbors = set_free(link->neighbors);
2,556✔
262
        link->addresses = set_free(link->addresses);
2,556✔
263
        link->qdiscs = set_free(link->qdiscs);
2,556✔
264
        link->tclasses = set_free(link->tclasses);
2,556✔
265

266
        link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
2,556✔
267

268
        link_free_engines(link);
2,556✔
269

270
        set_free(link->sr_iov_virt_port_ifindices);
2,556✔
271
        free(link->ifname);
2,556✔
272
        strv_free(link->alternative_names);
2,556✔
273
        free(link->kind);
2,556✔
274
        free(link->ssid);
2,556✔
275
        free(link->previous_ssid);
2,556✔
276
        free(link->driver);
2,556✔
277

278
        unlink_and_free(link->lease_file);
2,556✔
279
        unlink_and_free(link->state_file);
2,556✔
280

281
        sd_device_unref(link->dev);
2,556✔
282
        netdev_unref(link->netdev);
2,556✔
283

284
        hashmap_free(link->bound_to_links);
2,556✔
285
        hashmap_free(link->bound_by_links);
2,556✔
286

287
        set_free_with_destructor(link->slaves, link_unref);
2,564✔
288

289
        network_unref(link->network);
2,556✔
290

291
        sd_event_source_disable_unref(link->carrier_lost_timer);
2,556✔
292
        sd_event_source_disable_unref(link->ipv6_mtu_wait_synced_event_source);
2,556✔
293

294
        return mfree(link);
2,556✔
295
}
296

297
DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
163,307✔
298

299
int link_get_by_index(Manager *m, int ifindex, Link **ret) {
67,437✔
300
        Link *link;
67,437✔
301

302
        assert(m);
67,437✔
303

304
        if (ifindex <= 0)
67,437✔
305
                return -EINVAL;
306

307
        link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
66,747✔
308
        if (!link)
66,747✔
309
                return -ENODEV;
310

311
        if (ret)
63,330✔
312
                *ret = link;
63,330✔
313
        return 0;
314
}
315

316
int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
289✔
317
        Link *link;
289✔
318

319
        assert(m);
289✔
320
        assert(ifname);
289✔
321

322
        link = hashmap_get(m->links_by_name, ifname);
289✔
323
        if (!link)
289✔
324
                return -ENODEV;
325

326
        if (ret)
251✔
327
                *ret = link;
251✔
328
        return 0;
329
}
330

331
int link_get_by_hw_addr(Manager *m, const struct hw_addr_data *hw_addr, Link **ret) {
×
332
        Link *link;
×
333

334
        assert(m);
×
335
        assert(hw_addr);
×
336

337
        link = hashmap_get(m->links_by_hw_addr, hw_addr);
×
338
        if (!link)
×
339
                return -ENODEV;
340

341
        if (ret)
×
342
                *ret = link;
×
343
        return 0;
344
}
345

346
int link_get_master(Link *link, Link **ret) {
13,251✔
347
        assert(link);
13,251✔
348
        assert(link->manager);
13,251✔
349
        assert(ret);
13,251✔
350

351
        if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex)
13,251✔
352
                return -ENODEV;
353

354
        return link_get_by_index(link->manager, link->master_ifindex, ret);
637✔
355
}
356

357
void link_set_state(Link *link, LinkState state) {
8,326✔
358
        assert(link);
8,326✔
359

360
        if (link->state == state)
8,326✔
361
                return;
362

363
        log_link_debug(link, "State changed: %s -> %s",
6,735✔
364
                       link_state_to_string(link->state),
365
                       link_state_to_string(state));
366

367
        link->state = state;
6,735✔
368

369
        link_send_changed(link, "AdministrativeState", NULL);
6,735✔
370
        link_dirty(link);
6,735✔
371
}
372

373
int link_stop_engines(Link *link, bool may_keep_dynamic) {
4,193✔
374
        int r, ret = 0;
4,193✔
375

376
        assert(link);
4,193✔
377
        assert(link->manager);
4,193✔
378
        assert(link->manager->event);
4,193✔
379

380
        bool keep_dynamic =
12,541✔
381
                may_keep_dynamic &&
1,768✔
382
                link->network &&
4,193✔
383
                (link->manager->state == MANAGER_RESTARTING ||
50✔
384
                 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC_ON_STOP));
14✔
385

386
        if (!keep_dynamic) {
4,155✔
387
                r = sd_dhcp_client_stop(link->dhcp_client);
4,155✔
388
                if (r < 0)
4,155✔
389
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 client: %m"));
×
390

391
                r = sd_ipv4ll_stop(link->ipv4ll);
4,155✔
392
                if (r < 0)
4,155✔
393
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 link-local: %m"));
×
394

395
                r = sd_dhcp6_client_stop(link->dhcp6_client);
4,155✔
396
                if (r < 0)
4,155✔
397
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"));
×
398

399
                r = dhcp_pd_remove(link, /* only_marked = */ false);
4,155✔
400
                if (r < 0)
4,155✔
401
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not remove DHCPv6 PD addresses and routes: %m"));
×
402

403
                r = ndisc_stop(link);
4,155✔
404
                if (r < 0)
4,155✔
405
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Discovery: %m"));
×
406

407
                ndisc_flush(link);
4,155✔
408
        }
409

410
        r = sd_dhcp_server_stop(link->dhcp_server);
4,193✔
411
        if (r < 0)
4,193✔
412
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 server: %m"));
×
413

414
        r = sd_lldp_rx_stop(link->lldp_rx);
4,193✔
415
        if (r < 0)
4,193✔
416
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Rx: %m"));
×
417

418
        r = sd_lldp_tx_stop(link->lldp_tx);
4,193✔
419
        if (r < 0)
4,193✔
420
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Tx: %m"));
×
421

422
        r = ipv4acd_stop(link);
4,193✔
423
        if (r < 0)
4,193✔
424
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 ACD client: %m"));
×
425

426
        r = sd_radv_stop(link->radv);
4,193✔
427
        if (r < 0)
4,193✔
428
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Advertisement: %m"));
×
429

430
        return ret;
4,193✔
431
}
432

433
void link_enter_failed(Link *link) {
1✔
434
        assert(link);
1✔
435

436
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1✔
437
                return;
438

439
        log_link_warning(link, "Failed");
1✔
440

441
        link_set_state(link, LINK_STATE_FAILED);
1✔
442

443
        if (!ratelimit_below(&link->automatic_reconfigure_ratelimit)) {
1✔
444
                log_link_warning(link, "The interface entered the failed state frequently, refusing to reconfigure it automatically.");
×
445
                goto stop;
×
446
        }
447

448
        log_link_info(link, "Trying to reconfigure the interface.");
1✔
449
        if (link_reconfigure(link, LINK_RECONFIGURE_UNCONDITIONALLY) > 0)
1✔
450
                return;
451

452
stop:
×
453
        (void) link_stop_engines(link, /* may_keep_dynamic = */ false);
×
454
}
455

456
void link_check_ready(Link *link) {
23,262✔
457
        Address *a;
23,262✔
458

459
        assert(link);
23,262✔
460

461
        if (link->state == LINK_STATE_CONFIGURED)
23,262✔
462
                return;
22,487✔
463

464
        if (link->state != LINK_STATE_CONFIGURING)
19,770✔
465
                return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
1,269✔
466

467
        if (!link->network)
18,501✔
468
                return (void) log_link_debug(link, "%s(): link is unmanaged.", __func__);
×
469

470
        if (!link->tc_configured)
18,501✔
471
                return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
313✔
472

473
        if (link->set_link_messages > 0)
18,188✔
474
                return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
1,312✔
475

476
        if (!link->activated)
16,876✔
477
                return (void) log_link_debug(link, "%s(): link is not activated.", __func__);
10,451✔
478

479
        if (link->iftype == ARPHRD_CAN)
6,425✔
480
                /* let's shortcut things for CAN which doesn't need most of checks below. */
481
                goto ready;
6✔
482

483
        if (!link->stacked_netdevs_created)
6,419✔
484
                return (void) log_link_debug(link, "%s(): stacked netdevs are not created.", __func__);
147✔
485

486
        if (!link->static_addresses_configured)
6,272✔
487
                return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
3,141✔
488

489
        if (!link->static_address_labels_configured)
3,131✔
490
                return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__);
4✔
491

492
        if (!link->static_bridge_fdb_configured)
3,127✔
493
                return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
2✔
494

495
        if (!link->static_bridge_mdb_configured)
3,125✔
496
                return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
10✔
497

498
        if (!link->static_ipv6_proxy_ndp_configured)
3,115✔
499
                return (void) log_link_debug(link, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__);
2✔
500

501
        if (!link->static_neighbors_configured)
3,113✔
502
                return (void) log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
5✔
503

504
        if (!link->static_nexthops_configured)
3,108✔
505
                return (void) log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
18✔
506

507
        if (!link->static_routes_configured)
3,090✔
508
                return (void) log_link_debug(link, "%s(): static routes are not configured.", __func__);
127✔
509

510
        if (!link->static_routing_policy_rules_configured)
2,963✔
511
                return (void) log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
32✔
512

513
        if (!link->sr_iov_configured)
2,931✔
514
                return (void) log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
×
515

516
        /* IPv6LL is assigned after the link gains its carrier. */
517
        if (!link->network->configure_without_carrier &&
5,850✔
518
            link_ipv6ll_enabled(link) &&
2,919✔
519
            !in6_addr_is_set(&link->ipv6ll_address))
2,821✔
520
                return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__);
921✔
521

522
        /* All static addresses must be ready. */
523
        bool has_static_address = false;
2,010✔
524
        SET_FOREACH(a, link->addresses) {
6,918✔
525
                if (a->source != NETWORK_CONFIG_SOURCE_STATIC)
5,056✔
526
                        continue;
3,156✔
527
                if (!address_is_ready(a))
1,900✔
528
                        return (void) log_link_debug(link, "%s(): static address %s is not ready.", __func__,
148✔
529
                                                     IN_ADDR_PREFIX_TO_STRING(a->family, &a->in_addr, a->prefixlen));
530
                has_static_address = true;
531
        }
532

533
        /* If at least one static address is requested, do not request that dynamic addressing protocols are finished. */
534
        if (has_static_address)
1,862✔
535
                goto ready;
346✔
536

537
        /* If no dynamic addressing protocol enabled, assume the interface is ready.
538
         * Note, ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
539
        if (!link_ipv4ll_enabled(link) && !link_dhcp4_enabled(link) &&
2,749✔
540
            !link_dhcp6_enabled(link) && !link_dhcp_pd_is_enabled(link) &&
757✔
541
            (link->network->configure_without_carrier || !link_ndisc_enabled(link)))
547✔
542
                goto ready;
220✔
543

544
        bool ipv4ll_ready =
2,592✔
545
                link_ipv4ll_enabled(link) && link->ipv4ll_address_configured &&
1,350✔
546
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_IPV4LL);
54✔
547
        bool dhcp4_ready =
1,296✔
548
                link_dhcp4_enabled(link) && link->dhcp4_configured &&
1,360✔
549
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP4);
64✔
550
        bool dhcp6_ready =
1,296✔
551
                link_dhcp6_enabled(link) && link->dhcp6_configured &&
1,296✔
552
                (!link->network->dhcp6_use_address ||
62✔
553
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP6));
30✔
554
        bool dhcp_pd_ready =
2,592✔
555
                link_dhcp_pd_is_enabled(link) && link->dhcp_pd_configured &&
1,296✔
556
                (!link->network->dhcp_pd_assign ||
143✔
557
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP_PD));
68✔
558
        bool ndisc_ready =
2,592✔
559
                link_ndisc_enabled(link) && link->ndisc_configured &&
1,296✔
560
                (!link->network->ndisc_use_autonomous_prefix ||
418✔
561
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_NDISC));
209✔
562

563
        /* If the uplink for PD is self, then request the corresponding DHCP protocol is also ready. */
564
        if (dhcp_pd_is_uplink(link, link, /* accept_auto = */ false)) {
1,296✔
565
                if (link_dhcp4_enabled(link) && link->network->dhcp_use_6rd &&
80✔
566
                    sd_dhcp_lease_has_6rd(link->dhcp_lease)) {
25✔
567
                        if (!link->dhcp4_configured)
22✔
568
                                return (void) log_link_debug(link, "%s(): DHCPv4 6rd prefix is assigned, but DHCPv4 protocol is not finished yet.", __func__);
20✔
569
                        if (!dhcp_pd_ready)
2✔
570
                                return (void) log_link_debug(link, "%s(): DHCPv4 is finished, but prefix acquired by DHCPv4-6rd is not assigned yet.", __func__);
×
571
                }
572

573
                if (link_dhcp6_enabled(link) && link->network->dhcp6_use_pd_prefix &&
65✔
574
                    sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease)) {
30✔
575
                        if (!link->dhcp6_configured)
21✔
576
                                return (void) log_link_debug(link, "%s(): DHCPv6 IA_PD prefix is assigned, but DHCPv6 protocol is not finished yet.", __func__);
8✔
577
                        if (!dhcp_pd_ready)
13✔
578
                                return (void) log_link_debug(link, "%s(): DHCPv6 is finished, but prefix acquired by DHCPv6 IA_PD is not assigned yet.", __func__);
10✔
579
                }
580
        }
581

582
        /* At least one dynamic addressing protocol is finished. */
583
        if (!ipv4ll_ready && !dhcp4_ready && !dhcp6_ready && !dhcp_pd_ready && !ndisc_ready)
1,258✔
584
                return (void) log_link_debug(link, "%s(): dynamic addressing protocols are enabled but none of them finished yet.", __func__);
1,055✔
585

586
        log_link_debug(link, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
1,050✔
587
                       __func__,
588
                       yes_no(ipv4ll_ready),
589
                       yes_no(dhcp4_ready),
590
                       yes_no(dhcp6_ready),
591
                       yes_no(dhcp_pd_ready),
592
                       yes_no(ndisc_ready));
593

594
ready:
775✔
595
        link_set_state(link, LINK_STATE_CONFIGURED);
775✔
596
}
597

598
static int link_request_static_configs(Link *link) {
1,358✔
599
        int r;
1,358✔
600

601
        assert(link);
1,358✔
602
        assert(link->network);
1,358✔
603
        assert(link->state != _LINK_STATE_INVALID);
1,358✔
604

605
        r = link_request_static_addresses(link);
1,358✔
606
        if (r < 0)
1,358✔
607
                return r;
608

609
        r = link_request_static_address_labels(link);
1,358✔
610
        if (r < 0)
1,358✔
611
                return r;
612

613
        r = link_request_static_bridge_fdb(link);
1,358✔
614
        if (r < 0)
1,358✔
615
                return r;
616

617
        r = link_request_static_bridge_mdb(link);
1,358✔
618
        if (r < 0)
1,358✔
619
                return r;
620

621
        r = link_request_static_ipv6_proxy_ndp_addresses(link);
1,358✔
622
        if (r < 0)
1,358✔
623
                return r;
624

625
        r = link_request_static_neighbors(link);
1,358✔
626
        if (r < 0)
1,358✔
627
                return r;
628

629
        r = link_request_static_nexthops(link, false);
1,358✔
630
        if (r < 0)
1,358✔
631
                return r;
632

633
        r = link_request_static_routes(link, false);
1,358✔
634
        if (r < 0)
1,358✔
635
                return r;
636

637
        r = link_request_static_routing_policy_rules(link);
1,358✔
638
        if (r < 0)
1,358✔
639
                return r;
×
640

641
        return 0;
642
}
643

644
int link_request_stacked_netdevs(Link *link, NetDevLocalAddressType type) {
10,673✔
645
        NetDev *netdev;
10,673✔
646
        int r;
10,673✔
647

648
        assert(link);
10,673✔
649

650
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
10,673✔
651
                return 0;
10,673✔
652

653
        assert(link->network);
2,327✔
654

655
        link->stacked_netdevs_created = false;
2,327✔
656

657
        HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
2,676✔
658
                if (!netdev_needs_reconfigure(netdev, type))
349✔
659
                        continue;
206✔
660

661
                r = link_request_stacked_netdev(link, netdev);
143✔
662
                if (r < 0)
143✔
663
                        return r;
×
664
        }
665

666
        if (link->create_stacked_netdev_messages == 0) {
2,327✔
667
                link->stacked_netdevs_created = true;
2,193✔
668
                link_check_ready(link);
2,193✔
669
        }
670

671
        return 0;
672
}
673

674
static int link_acquire_dynamic_ipv6_conf(Link *link) {
710✔
675
        int r;
710✔
676

677
        assert(link);
710✔
678

679
        r = radv_start(link);
710✔
680
        if (r < 0)
710✔
681
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
×
682

683
        r = ndisc_start(link);
710✔
684
        if (r < 0)
710✔
685
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
×
686

687
        r = dhcp6_start(link);
710✔
688
        if (r < 0)
710✔
689
                return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m");
×
690

691
        return 0;
692
}
693

694
static int link_acquire_dynamic_ipv4_conf(Link *link) {
835✔
695
        int r;
835✔
696

697
        assert(link);
835✔
698
        assert(link->manager);
835✔
699
        assert(link->manager->event);
835✔
700

701
        if (link->dhcp_client) {
835✔
702
                r = dhcp4_start(link);
×
703
                if (r < 0)
×
704
                        return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
×
705

706
                log_link_debug(link, "Acquiring DHCPv4 lease.");
×
707

708
        } else if (link->ipv4ll) {
835✔
709
                if (in4_addr_is_set(&link->network->ipv4ll_start_address)) {
71✔
710
                        r = sd_ipv4ll_set_address(link->ipv4ll, &link->network->ipv4ll_start_address);
1✔
711
                        if (r < 0)
1✔
712
                                return log_link_warning_errno(link, r, "Could not set IPv4 link-local start address: %m");
×
713
                }
714

715
                r = sd_ipv4ll_start(link->ipv4ll);
71✔
716
                if (r < 0)
71✔
717
                        return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
×
718

719
                log_link_debug(link, "Acquiring IPv4 link-local address.");
71✔
720
        }
721

722
        r = link_start_dhcp4_server(link);
835✔
723
        if (r < 0)
835✔
724
                return log_link_warning_errno(link, r, "Could not start DHCP server: %m");
×
725

726
        r = ipv4acd_start(link);
835✔
727
        if (r < 0)
835✔
728
                return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m");
×
729

730
        return 0;
731
}
732

733
static int link_acquire_dynamic_conf(Link *link) {
835✔
734
        int r;
835✔
735

736
        assert(link);
835✔
737
        assert(link->network);
835✔
738

739
        r = link_acquire_dynamic_ipv4_conf(link);
835✔
740
        if (r < 0)
835✔
741
                return r;
742

743
        if (in6_addr_is_set(&link->ipv6ll_address)) {
835✔
744
                r = link_acquire_dynamic_ipv6_conf(link);
247✔
745
                if (r < 0)
247✔
746
                        return r;
747
        }
748

749
        if (!link_radv_enabled(link) || !link->network->dhcp_pd_announce) {
835✔
750
                /* DHCPv6PD downstream does not require IPv6LL address. But may require RADV to be
751
                 * configured, and RADV may not be configured yet here. Only acquire subnet prefix when
752
                 * RADV is disabled, or the announcement of the prefix is disabled. Otherwise, the
753
                 * below will be called in radv_start(). */
754
                r = dhcp_request_prefix_delegation(link);
764✔
755
                if (r < 0)
764✔
756
                        return log_link_warning_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
×
757
        }
758

759
        if (link->lldp_tx) {
835✔
760
                r = sd_lldp_tx_start(link->lldp_tx);
5✔
761
                if (r < 0)
5✔
762
                        return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
×
763
        }
764

765
        if (link->lldp_rx) {
835✔
766
                r = sd_lldp_rx_start(link->lldp_rx);
754✔
767
                if (r < 0)
754✔
768
                        return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
1✔
769
        }
770

771
        return 0;
772
}
773

774
int link_ipv6ll_gained(Link *link) {
585✔
775
        int r;
585✔
776

777
        assert(link);
585✔
778

779
        log_link_info(link, "Gained IPv6LL");
585✔
780

781
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
585✔
782
                return 0;
783

784
        r = link_acquire_dynamic_ipv6_conf(link);
463✔
785
        if (r < 0)
463✔
786
                return r;
787

788
        r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_IPV6LL);
463✔
789
        if (r < 0)
463✔
790
                return r;
791

792
        link_check_ready(link);
463✔
793
        return 0;
463✔
794
}
795

796
int link_handle_bound_to_list(Link *link) {
24✔
797
        bool required_up = false;
24✔
798
        bool link_is_up = false;
24✔
799
        Link *l;
24✔
800

801
        assert(link);
24✔
802

803
        /* If at least one interface in bound_to_links has carrier, then make this interface up.
804
         * If all interfaces in bound_to_links do not, then make this interface down. */
805

806
        if (hashmap_isempty(link->bound_to_links))
24✔
807
                return 0;
24✔
808

809
        if (link->flags & IFF_UP)
20✔
810
                link_is_up = true;
10✔
811

812
        HASHMAP_FOREACH(l, link->bound_to_links)
28✔
813
                if (link_has_carrier(l)) {
22✔
814
                        required_up = true;
815
                        break;
816
                }
817

818
        if (!required_up && link_is_up)
20✔
819
                return link_request_to_bring_up_or_down(link, /* up = */ false);
3✔
820
        if (required_up && !link_is_up)
17✔
821
                return link_request_to_bring_up_or_down(link, /* up = */ true);
7✔
822

823
        return 0;
824
}
825

826
static int link_handle_bound_by_list(Link *link) {
4,194✔
827
        Link *l;
4,194✔
828
        int r;
4,194✔
829

830
        assert(link);
4,194✔
831

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

834
        HASHMAP_FOREACH(l, link->bound_by_links) {
4,209✔
835
                r = link_handle_bound_to_list(l);
15✔
836
                if (r < 0)
15✔
837
                        return r;
×
838
        }
839

840
        return 0;
4,194✔
841
}
842

843
static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
18✔
844
        int r;
18✔
845

846
        assert(link);
18✔
847
        assert(carrier);
18✔
848

849
        if (link == carrier)
18✔
850
                return 0;
851

852
        if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
18✔
853
                return 0;
854

855
        r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
16✔
856
        if (r < 0)
16✔
857
                return r;
858

859
        link_dirty(link);
16✔
860

861
        return 0;
16✔
862
}
863

864
static int link_new_bound_by_list(Link *link) {
2,369✔
865
        Manager *m;
2,369✔
866
        Link *carrier;
2,369✔
867
        int r;
2,369✔
868

869
        assert(link);
2,369✔
870
        assert(link->manager);
2,369✔
871

872
        m = link->manager;
2,369✔
873

874
        HASHMAP_FOREACH(carrier, m->links_by_index) {
16,271✔
875
                if (!carrier->network)
13,902✔
876
                        continue;
13,244✔
877

878
                if (strv_isempty(carrier->network->bind_carrier))
658✔
879
                        continue;
651✔
880

881
                if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
7✔
882
                        r = link_put_carrier(link, carrier, &link->bound_by_links);
6✔
883
                        if (r < 0)
6✔
884
                                return r;
×
885
                }
886
        }
887

888
        HASHMAP_FOREACH(carrier, link->bound_by_links) {
2,375✔
889
                r = link_put_carrier(carrier, link, &carrier->bound_to_links);
6✔
890
                if (r < 0)
6✔
891
                        return r;
×
892
        }
893

894
        return 0;
2,369✔
895
}
896

897
static int link_new_bound_to_list(Link *link) {
756✔
898
        Manager *m;
756✔
899
        Link *carrier;
756✔
900
        int r;
756✔
901

902
        assert(link);
756✔
903
        assert(link->manager);
756✔
904

905
        if (!link->network)
756✔
906
                return 0;
756✔
907

908
        if (strv_isempty(link->network->bind_carrier))
756✔
909
                return 0;
910

911
        m = link->manager;
5✔
912

913
        HASHMAP_FOREACH(carrier, m->links_by_index) {
42✔
914
                if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
32✔
915
                        r = link_put_carrier(link, carrier, &link->bound_to_links);
3✔
916
                        if (r < 0)
3✔
917
                                return r;
×
918
                }
919
        }
920

921
        HASHMAP_FOREACH(carrier, link->bound_to_links) {
8✔
922
                r = link_put_carrier(carrier, link, &carrier->bound_by_links);
3✔
923
                if (r < 0)
3✔
924
                        return r;
×
925
        }
926

927
        return 0;
5✔
928
}
929

930
static void link_free_bound_to_list(Link *link) {
3,340✔
931
        bool updated = false;
3,340✔
932
        Link *bound_to;
3,340✔
933

934
        assert(link);
3,340✔
935

936
        while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
3,344✔
937
                updated = true;
4✔
938

939
                if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
4✔
940
                        link_dirty(bound_to);
4✔
941
        }
942

943
        if (updated)
3,340✔
944
                link_dirty(link);
4✔
945
}
3,340✔
946

947
static void link_free_bound_by_list(Link *link) {
787✔
948
        bool updated = false;
787✔
949
        Link *bound_by;
787✔
950

951
        assert(link);
787✔
952

953
        while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
791✔
954
                updated = true;
4✔
955

956
                if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
4✔
957
                        link_dirty(bound_by);
4✔
958
                        link_handle_bound_to_list(bound_by);
4✔
959
                }
960
        }
961

962
        if (updated)
787✔
963
                link_dirty(link);
4✔
964
}
787✔
965

966
static int link_append_to_master(Link *link) {
423✔
967
        Link *master;
423✔
968
        int r;
423✔
969

970
        assert(link);
423✔
971

972
        /* - The link may have no master.
973
         * - RTM_NEWLINK message about master interface may not be received yet. */
974
        if (link_get_master(link, &master) < 0)
423✔
975
                return 0;
423✔
976

977
        r = set_ensure_put(&master->slaves, NULL, link);
415✔
978
        if (r <= 0)
415✔
979
                return r;
980

981
        link_ref(link);
34✔
982
        return 0;
983
}
984

985
static void link_drop_from_master(Link *link) {
821✔
986
        Link *master;
821✔
987

988
        assert(link);
821✔
989

990
        if (!link->manager)
821✔
991
                return;
795✔
992

993
        if (link_get_master(link, &master) < 0)
821✔
994
                return;
995

996
        link_unref(set_remove(master->slaves, link));
26✔
997
}
998

999
static int link_drop_requests(Link *link) {
3,340✔
1000
        Request *req;
3,340✔
1001
        int ret = 0;
3,340✔
1002

1003
        assert(link);
3,340✔
1004
        assert(link->manager);
3,340✔
1005

1006
        ORDERED_SET_FOREACH(req, link->manager->request_queue) {
4,928✔
1007
                if (req->link != link)
1,588✔
1008
                        continue;
1,490✔
1009

1010
                /* If the request is already called, but its reply is not received, then we need to
1011
                 * drop the configuration (e.g. address) here. Note, if the configuration is known,
1012
                 * it will be handled later by link_drop_unmanaged_addresses() or so. */
1013
                if (req->waiting_reply && link->state != LINK_STATE_LINGER)
98✔
1014
                        switch (req->type) {
2✔
1015
                        case REQUEST_TYPE_ADDRESS: {
×
1016
                                Address *address = ASSERT_PTR(req->userdata);
×
1017

1018
                                if (address_get(link, address, NULL) < 0)
×
1019
                                        RET_GATHER(ret, address_remove(address, link));
×
1020
                                break;
1021
                        }
1022
                        case REQUEST_TYPE_NEIGHBOR: {
×
1023
                                Neighbor *neighbor = ASSERT_PTR(req->userdata);
×
1024

1025
                                if (neighbor_get(link, neighbor, NULL) < 0)
×
1026
                                        RET_GATHER(ret, neighbor_remove(neighbor, link));
×
1027
                                break;
1028
                        }
1029
                        case REQUEST_TYPE_NEXTHOP: {
×
1030
                                NextHop *nexthop = ASSERT_PTR(req->userdata);
×
1031

1032
                                if (nexthop_get_by_id(link->manager, nexthop->id, NULL) < 0)
×
1033
                                        RET_GATHER(ret, nexthop_remove(nexthop, link->manager));
×
1034
                                break;
1035
                        }
1036
                        case REQUEST_TYPE_ROUTE: {
2✔
1037
                                Route *route = ASSERT_PTR(req->userdata);
2✔
1038

1039
                                if (route_get(link->manager, route, NULL) < 0)
2✔
1040
                                        RET_GATHER(ret, route_remove(route, link->manager));
×
1041
                                break;
1042
                        }
1043
                        default:
96✔
1044
                                ;
98✔
1045
                        }
1046

1047
                request_detach(req);
98✔
1048
        }
1049

1050
        return ret;
3,340✔
1051
}
1052

1053
static Link *link_drop(Link *link) {
803✔
1054
        if (!link)
803✔
1055
                return NULL;
1056

1057
        assert(link->manager);
787✔
1058

1059
        link_set_state(link, LINK_STATE_LINGER);
787✔
1060

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

1064
        (void) link_drop_requests(link);
787✔
1065

1066
        link_free_bound_to_list(link);
787✔
1067
        link_free_bound_by_list(link);
787✔
1068

1069
        link_clear_sr_iov_ifindices(link);
787✔
1070

1071
        link_drop_from_master(link);
787✔
1072

1073
        if (link->state_file)
787✔
1074
                (void) unlink(link->state_file);
787✔
1075

1076
        link_clean(link);
787✔
1077

1078
        STRV_FOREACH(n, link->alternative_names)
821✔
1079
                hashmap_remove(link->manager->links_by_name, *n);
34✔
1080
        hashmap_remove(link->manager->links_by_name, link->ifname);
787✔
1081

1082
        /* bonding master and its slaves have the same hardware address. */
1083
        hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
787✔
1084

1085
        /* The following must be called at last. */
1086
        assert_se(hashmap_remove(link->manager->links_by_index, INT_TO_PTR(link->ifindex)) == link);
787✔
1087
        return link_unref(link);
787✔
1088
}
1089

1090
static int link_drop_unmanaged_config(Link *link) {
756✔
1091
        int r;
756✔
1092

1093
        assert(link);
756✔
1094
        assert(link->state == LINK_STATE_CONFIGURING);
756✔
1095
        assert(link->manager);
756✔
1096

1097
        r = link_drop_unmanaged_routes(link);
756✔
1098
        RET_GATHER(r, link_drop_unmanaged_nexthops(link));
756✔
1099
        RET_GATHER(r, link_drop_unmanaged_addresses(link));
756✔
1100
        RET_GATHER(r, link_drop_unmanaged_neighbors(link));
756✔
1101
        RET_GATHER(r, link_drop_unmanaged_routing_policy_rules(link));
756✔
1102

1103
        return r;
756✔
1104
}
1105

1106
static int link_drop_static_config(Link *link) {
2,389✔
1107
        int r;
2,389✔
1108

1109
        assert(link);
2,389✔
1110
        assert(link->manager);
2,389✔
1111

1112
        r = link_drop_static_routes(link);
2,389✔
1113
        RET_GATHER(r, link_drop_static_nexthops(link));
2,389✔
1114
        RET_GATHER(r, link_drop_static_addresses(link));
2,389✔
1115
        RET_GATHER(r, link_drop_static_neighbors(link));
2,389✔
1116
        RET_GATHER(r, link_drop_static_routing_policy_rules(link));
2,389✔
1117

1118
        return r;
2,389✔
1119
}
1120

1121
static int link_drop_dynamic_config(Link *link, Network *network) {
720✔
1122
        int r;
720✔
1123

1124
        assert(link);
720✔
1125
        assert(link->network);
720✔
1126

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

1130
        r = link_drop_ndisc_config(link, network);
720✔
1131
        RET_GATHER(r, link_drop_radv_config(link, network)); /* Stop before dropping DHCP-PD prefixes. */
720✔
1132
        RET_GATHER(r, link_drop_ipv4ll_config(link, network)); /* Stop before DHCPv4 client. */
720✔
1133
        RET_GATHER(r, link_drop_dhcp4_config(link, network));
720✔
1134
        RET_GATHER(r, link_drop_dhcp6_config(link, network));
720✔
1135
        RET_GATHER(r, link_drop_dhcp_pd_config(link, network));
720✔
1136
        link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
720✔
1137
        link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx); /* TODO: keep the received neighbors. */
720✔
1138
        link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
720✔
1139

1140
        /* Even if we do not release DHCP lease or so, reset 'configured' flags. Otherwise, e.g. if
1141
         * previously UseDNS= was disabled but is now enabled, link will enter configured state before
1142
         * expected DNS servers being acquired. */
1143
        link->ipv4ll_address_configured = false;
720✔
1144
        link->dhcp4_configured = false;
720✔
1145
        link->dhcp6_configured = false;
720✔
1146
        link->dhcp_pd_configured = false;
720✔
1147
        link->ndisc_configured = false;
720✔
1148

1149
        return r;
720✔
1150
}
1151

1152
static int link_configure(Link *link) {
756✔
1153
        int r;
756✔
1154

1155
        assert(link);
756✔
1156
        assert(link->network);
756✔
1157
        assert(link->state == LINK_STATE_INITIALIZED);
756✔
1158

1159
        link_set_state(link, LINK_STATE_CONFIGURING);
756✔
1160

1161
        r = link_drop_unmanaged_config(link);
756✔
1162
        if (r < 0)
756✔
1163
                return r;
1164

1165
        r = link_new_bound_to_list(link);
756✔
1166
        if (r < 0)
756✔
1167
                return r;
1168

1169
        r = link_request_traffic_control(link);
756✔
1170
        if (r < 0)
756✔
1171
                return r;
1172

1173
        r = link_configure_mtu(link);
756✔
1174
        if (r < 0)
756✔
1175
                return r;
1176

1177
        if (link->iftype == ARPHRD_CAN) {
756✔
1178
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1179
                r = link_request_to_set_can(link);
6✔
1180
                if (r < 0)
6✔
1181
                        return r;
1182

1183
                return link_request_to_activate(link);
6✔
1184
        }
1185

1186
        r = link_request_sr_iov_vfs(link);
750✔
1187
        if (r < 0)
750✔
1188
                return r;
1189

1190
        r = link_set_sysctl(link);
750✔
1191
        if (r < 0)
750✔
1192
                return r;
1193

1194
        r = link_request_to_set_mac(link, /* allow_retry = */ true);
750✔
1195
        if (r < 0)
750✔
1196
                return r;
1197

1198
        r = link_request_to_set_ipoib(link);
750✔
1199
        if (r < 0)
750✔
1200
                return r;
1201

1202
        r = link_request_to_set_flags(link);
750✔
1203
        if (r < 0)
750✔
1204
                return r;
1205

1206
        r = link_request_to_set_group(link);
750✔
1207
        if (r < 0)
750✔
1208
                return r;
1209

1210
        r = link_request_to_set_addrgen_mode(link);
750✔
1211
        if (r < 0)
750✔
1212
                return r;
1213

1214
        r = link_request_to_set_master(link);
750✔
1215
        if (r < 0)
750✔
1216
                return r;
1217

1218
        r = link_request_stacked_netdevs(link, _NETDEV_LOCAL_ADDRESS_TYPE_INVALID);
750✔
1219
        if (r < 0)
750✔
1220
                return r;
1221

1222
        r = link_request_to_set_bond(link);
750✔
1223
        if (r < 0)
750✔
1224
                return r;
1225

1226
        r = link_request_to_set_bridge(link);
750✔
1227
        if (r < 0)
750✔
1228
                return r;
1229

1230
        r = link_request_to_set_bridge_vlan(link);
750✔
1231
        if (r < 0)
750✔
1232
                return r;
1233

1234
        r = link_request_to_activate(link);
750✔
1235
        if (r < 0)
750✔
1236
                return r;
1237

1238
        r = ipv4ll_configure(link);
750✔
1239
        if (r < 0)
750✔
1240
                return r;
1241

1242
        r = link_request_dhcp4_client(link);
750✔
1243
        if (r < 0)
750✔
1244
                return r;
1245

1246
        r = link_request_dhcp6_client(link);
750✔
1247
        if (r < 0)
750✔
1248
                return r;
1249

1250
        r = link_request_ndisc(link);
750✔
1251
        if (r < 0)
750✔
1252
                return r;
1253

1254
        r = link_request_dhcp_server(link);
750✔
1255
        if (r < 0)
750✔
1256
                return r;
1257

1258
        r = link_request_radv(link);
750✔
1259
        if (r < 0)
750✔
1260
                return r;
1261

1262
        r = link_lldp_rx_configure(link);
750✔
1263
        if (r < 0)
750✔
1264
                return r;
1265

1266
        r = link_lldp_tx_configure(link);
750✔
1267
        if (r < 0)
750✔
1268
                return r;
1269

1270
        r = link_request_static_configs(link);
750✔
1271
        if (r < 0)
750✔
1272
                return r;
1273

1274
        if (!link_has_carrier(link))
750✔
1275
                return 0;
1276

1277
        return link_acquire_dynamic_conf(link);
227✔
1278
}
1279

1280
static int link_get_network(Link *link, Network **ret) {
4,208✔
1281
        Network *network;
4,208✔
1282
        int r;
4,208✔
1283

1284
        assert(link);
4,208✔
1285
        assert(link->manager);
4,208✔
1286
        assert(ret);
4,208✔
1287

1288
        ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
15,143✔
1289
                bool warn = false;
14,262✔
1290

1291
                r = net_match_config(
28,524✔
1292
                                &network->match,
14,262✔
1293
                                link->dev,
1294
                                &link->hw_addr,
14,262✔
1295
                                &link->permanent_hw_addr,
14,262✔
1296
                                link->driver,
14,262✔
1297
                                link->iftype,
14,262✔
1298
                                link->kind,
14,262✔
1299
                                link->ifname,
14,262✔
1300
                                link->alternative_names,
14,262✔
1301
                                link->wlan_iftype,
1302
                                link->ssid,
14,262✔
1303
                                &link->bssid);
14,262✔
1304
                if (r < 0)
14,262✔
1305
                        return r;
3,327✔
1306
                if (r == 0)
14,262✔
1307
                        continue;
10,935✔
1308

1309
                if (network->match.ifname && link->dev) {
3,327✔
1310
                        uint8_t name_assign_type = NET_NAME_UNKNOWN;
3,314✔
1311
                        const char *attr;
3,314✔
1312

1313
                        if (sd_device_get_sysattr_value(link->dev, "name_assign_type", &attr) >= 0)
3,314✔
1314
                                (void) safe_atou8(attr, &name_assign_type);
1,893✔
1315

1316
                        warn = name_assign_type == NET_NAME_ENUM;
3,314✔
1317
                }
1318

1319
                log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
6,654✔
1320
                              "Found matching .network file%s: %s",
1321
                              warn ? ", based on potentially unpredictable interface name" : "",
1322
                              network->filename);
1323

1324
                if (network->unmanaged)
3,327✔
1325
                        return -ENOENT;
1326

1327
                *ret = network;
1,593✔
1328
                return 0;
1,593✔
1329
        }
1330

1331
        return log_link_debug_errno(link, SYNTHETIC_ERRNO(ENOENT), "No matching .network found.");
881✔
1332
}
1333

1334
static void link_enter_unmanaged(Link *link) {
2,615✔
1335
        assert(link);
2,615✔
1336

1337
        if (link->state == LINK_STATE_UNMANAGED)
2,615✔
1338
                return;
1339

1340
        log_link_full(link, link->state == LINK_STATE_INITIALIZED ? LOG_DEBUG : LOG_INFO,
1,800✔
1341
                      "Unmanaging interface.");
1342

1343
        (void) link_stop_engines(link, /* may_keep_dynamic = */ false);
1,797✔
1344
        (void) link_drop_requests(link);
1,797✔
1345
        (void) link_drop_static_config(link);
1,797✔
1346

1347
        /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1348
         * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1349
         * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1350
         * map here, as it depends on .network files assigned to other links. */
1351
        link_free_bound_to_list(link);
1,797✔
1352
        link_free_engines(link);
1,797✔
1353

1354
        link->network = network_unref(link->network);
1,797✔
1355
        link_set_state(link, LINK_STATE_UNMANAGED);
1,797✔
1356
}
1357

1358
int link_reconfigure_impl(Link *link, LinkReconfigurationFlag flags) {
5,095✔
1359
        Network *network = NULL;
5,095✔
1360
        int r;
5,095✔
1361

1362
        assert(link);
5,095✔
1363
        assert(link->manager);
5,095✔
1364

1365
        link_assign_netdev(link);
5,095✔
1366

1367
        if (link->manager->state != MANAGER_RUNNING)
5,095✔
1368
                return 0;
5,095✔
1369

1370
        if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
9,303✔
1371
                return 0;
1372

1373
        r = link_get_network(link, &network);
4,208✔
1374
        if (r == -ENOENT) {
4,208✔
1375
                link_enter_unmanaged(link);
2,615✔
1376
                return 0;
2,615✔
1377
        }
1378
        if (r < 0)
1,593✔
1379
                return r;
1380

1381
        if (link->network == network && !FLAGS_SET(flags, LINK_RECONFIGURE_UNCONDITIONALLY))
1,593✔
1382
                return 0;
1383

1384
        _cleanup_free_ char *joined = strv_join(network->dropins, ", ");
1,512✔
1385
        if (link->network)
756✔
1386
                log_link_info(link, "Reconfiguring with %s%s%s%s.",
531✔
1387
                              network->filename,
1388
                              isempty(joined) ? "" : " (dropins: ",
1389
                              joined,
1390
                              isempty(joined) ? "" : ")");
1391
        else
1392
                log_link_info(link, "Configuring with %s%s%s%s.",
1,737✔
1393
                              network->filename,
1394
                              isempty(joined) ? "" : " (dropins: ",
1395
                              joined,
1396
                              isempty(joined) ? "" : ")");
1397

1398
        /* Dropping configurations based on the old .network file. */
1399
        r = link_drop_requests(link);
756✔
1400
        if (r < 0)
756✔
1401
                return r;
1402

1403
        /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1404
         * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1405
         * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1406
         * map here, as it depends on .network files assigned to other links. */
1407
        link_free_bound_to_list(link);
756✔
1408

1409
        _cleanup_(network_unrefp) Network *old_network = TAKE_PTR(link->network);
756✔
1410

1411
        /* Then, apply new .network file */
1412
        link->network = network_ref(network);
756✔
1413

1414
        if (FLAGS_SET(network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC) ||
756✔
1415
            !FLAGS_SET(flags, LINK_RECONFIGURE_CLEANLY)) {
754✔
1416
                /* To make 'networkctl reconfigure INTERFACE' work safely for an interface whose new .network
1417
                 * file has KeepConfiguration=dynamic or yes, even if a clean reconfiguration is requested,
1418
                 * drop only unnecessary or possibly being changed dynamic configurations here. */
1419
                r = link_drop_dynamic_config(link, old_network);
720✔
1420
                if (r < 0)
720✔
1421
                        return r;
1422
        } else {
1423
                /* Otherwise, stop DHCP client and friends unconditionally, and drop all dynamic
1424
                 * configurations like DHCP address and routes. */
1425
                r = link_stop_engines(link, /* may_keep_dhcp = */ false);
36✔
1426
                if (r < 0)
36✔
1427
                        return r;
1428

1429
                /* Free DHCP client and friends. */
1430
                link_free_engines(link);
36✔
1431
        }
1432

1433
        link_update_operstate(link, true);
756✔
1434
        link_dirty(link);
756✔
1435

1436
        link_set_state(link, LINK_STATE_INITIALIZED);
756✔
1437
        link->activated = false;
756✔
1438

1439
        r = link_configure(link);
756✔
1440
        if (r < 0)
756✔
1441
                return r;
1✔
1442

1443
        return 1;
1444
}
1445

1446
typedef struct LinkReconfigurationData {
1447
        Manager *manager;
1448
        Link *link;
1449
        LinkReconfigurationFlag flags;
1450
        sd_bus_message *message;
1451
        unsigned *counter;
1452
} LinkReconfigurationData;
1453

1454
static LinkReconfigurationData* link_reconfiguration_data_free(LinkReconfigurationData *data) {
1,132✔
1455
        if (!data)
1,132✔
1456
                return NULL;
1457

1458
        link_unref(data->link);
1,132✔
1459
        sd_bus_message_unref(data->message);
1,132✔
1460

1461
        return mfree(data);
1,132✔
1462
}
1463

1464
DEFINE_TRIVIAL_CLEANUP_FUNC(LinkReconfigurationData*, link_reconfiguration_data_free);
1,132✔
1465

1466
static void link_reconfiguration_data_destroy_callback(LinkReconfigurationData *data) {
1,132✔
1467
        int r;
1,132✔
1468

1469
        assert(data);
1,132✔
1470

1471
        if (data->message) {
1,132✔
1472
                if (data->counter) {
1,035✔
1473
                        assert(*data->counter > 0);
999✔
1474
                        (*data->counter)--;
999✔
1475
                }
1476

1477
                if (!data->counter || *data->counter <= 0) {
1,035✔
1478
                        /* Update the state files before replying the bus method. Otherwise,
1479
                         * systemd-networkd-wait-online following networkctl reload/reconfigure may read an
1480
                         * outdated state file and wrongly handle an interface is already in the configured
1481
                         * state. */
1482
                        (void) manager_clean_all(data->manager);
166✔
1483

1484
                        r = sd_bus_reply_method_return(data->message, NULL);
166✔
1485
                        if (r < 0)
166✔
1486
                                log_warning_errno(r, "Failed to reply for DBus method, ignoring: %m");
×
1487
                }
1488
        }
1489

1490
        link_reconfiguration_data_free(data);
1,132✔
1491
}
1,132✔
1492

1493
static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, LinkReconfigurationData *data) {
1,132✔
1494
        Link *link = ASSERT_PTR(ASSERT_PTR(data)->link);
1,132✔
1495
        int r;
1,132✔
1496

1497
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1,132✔
1498
        if (r <= 0)
1,132✔
1499
                return r;
1500

1501
        r = link_reconfigure_impl(link, data->flags);
1,131✔
1502
        if (r < 0) {
1,131✔
1503
                link_enter_failed(link);
×
1504
                return 0;
×
1505
        }
1506

1507
        return r;
1508
}
1509

1510
int link_reconfigure_full(Link *link, LinkReconfigurationFlag flags, sd_bus_message *message, unsigned *counter) {
1,132✔
1511
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1,132✔
1512
        _cleanup_(link_reconfiguration_data_freep) LinkReconfigurationData *data = NULL;
×
1513
        int r;
1,132✔
1514

1515
        assert(link);
1,132✔
1516
        assert(link->manager);
1,132✔
1517
        assert(link->manager->rtnl);
1,132✔
1518

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

1525
        data = new(LinkReconfigurationData, 1);
1,132✔
1526
        if (!data) {
1,132✔
1527
                r = -ENOMEM;
×
1528
                goto failed;
×
1529
        }
1530

1531
        *data = (LinkReconfigurationData) {
2,264✔
1532
                .manager = link->manager,
1,132✔
1533
                .link = link_ref(link),
1,132✔
1534
                .flags = flags,
1535
                .message = sd_bus_message_ref(message), /* message may be NULL, but _ref() works fine. */
1,132✔
1536
                .counter = counter,
1537
        };
1538

1539
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
1,132✔
1540
        if (r < 0)
1,132✔
1541
                goto failed;
×
1542

1543
        r = netlink_call_async(link->manager->rtnl, NULL, req,
1,132✔
1544
                               link_reconfigure_handler,
1545
                               link_reconfiguration_data_destroy_callback, data);
1546
        if (r < 0)
1,132✔
1547
                goto failed;
×
1548

1549
        TAKE_PTR(data);
1,132✔
1550
        if (counter)
1,132✔
1551
                (*counter)++;
999✔
1552

1553
        if (link->state == LINK_STATE_FAILED)
1,132✔
1554
                link_set_state(link, LINK_STATE_INITIALIZED);
1✔
1555

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

1558
failed:
1559
        log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
1560
        link_enter_failed(link);
×
1561
        return r;
1562
}
1563

1564
static int link_initialized_and_synced(Link *link) {
2,371✔
1565
        int r;
2,371✔
1566

1567
        assert(link);
2,371✔
1568
        assert(link->manager);
2,371✔
1569

1570
        if (link->state == LINK_STATE_PENDING) {
2,371✔
1571
                log_link_debug(link, "Link state is up-to-date");
2,369✔
1572
                link_set_state(link, LINK_STATE_INITIALIZED);
2,369✔
1573

1574
                r = link_new_bound_by_list(link);
2,369✔
1575
                if (r < 0)
2,369✔
1576
                        return r;
1577

1578
                r = link_handle_bound_by_list(link);
2,369✔
1579
                if (r < 0)
2,369✔
1580
                        return r;
1581
        }
1582

1583
        return link_reconfigure_impl(link, /* flags = */ 0);
2,371✔
1584
}
1585

1586
static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2,306✔
1587
        int r;
2,306✔
1588

1589
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
2,306✔
1590
        if (r <= 0)
2,306✔
1591
                return r;
1592

1593
        r = link_initialized_and_synced(link);
2,305✔
1594
        if (r < 0)
2,305✔
1595
                link_enter_failed(link);
1✔
1596

1597
        return 0;
1598
}
1599

1600
static int link_initialized(Link *link, sd_device *device) {
2,402✔
1601
        int r;
2,402✔
1602

1603
        assert(link);
2,402✔
1604
        assert(device);
2,402✔
1605

1606
        /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1607
         * or sysattrs) may be outdated. */
1608
        device_unref_and_replace(link->dev, device);
2,402✔
1609

1610
        if (link->dhcp_client) {
2,402✔
1611
                r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev);
×
1612
                if (r < 0)
×
1613
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv4 client, ignoring: %m");
×
1614
        }
1615

1616
        if (link->dhcp6_client) {
2,402✔
1617
                r = sd_dhcp6_client_attach_device(link->dhcp6_client, link->dev);
×
1618
                if (r < 0)
×
1619
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv6 client, ignoring: %m");
×
1620
        }
1621

1622
        r = link_set_sr_iov_ifindices(link);
2,402✔
1623
        if (r < 0)
2,402✔
1624
                log_link_warning_errno(link, r, "Failed to manage SR-IOV PF and VF ports, ignoring: %m");
×
1625

1626
        if (link->state != LINK_STATE_PENDING)
2,402✔
1627
                return link_reconfigure(link, /* flags = */ 0);
96✔
1628

1629
        log_link_debug(link, "udev initialized link");
2,306✔
1630

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

1636
        return link_call_getlink(link, link_initialized_handler);
2,306✔
1637
}
1638

1639
int link_check_initialized(Link *link) {
2,555✔
1640
        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
2,555✔
1641
        int r;
2,555✔
1642

1643
        assert(link);
2,555✔
1644

1645
        if (!udev_available())
2,555✔
1646
                return link_initialized_and_synced(link);
66✔
1647

1648
        /* udev should be around */
1649
        r = sd_device_new_from_ifindex(&device, link->ifindex);
2,489✔
1650
        if (r < 0) {
2,489✔
1651
                log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
3✔
1652
                return 0;
3✔
1653
        }
1654

1655
        r = device_is_processed(device);
2,486✔
1656
        if (r < 0)
2,486✔
1657
                return log_link_warning_errno(link, r, "Could not determine whether the device is processed by udevd: %m");
×
1658
        if (r == 0) {
2,486✔
1659
                /* not yet ready */
1660
                log_link_debug(link, "link pending udev initialization...");
834✔
1661
                return 0;
834✔
1662
        }
1663

1664
        r = device_is_renaming(device);
1,652✔
1665
        if (r < 0)
1,652✔
1666
                return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
×
1667
        if (r > 0) {
1,652✔
1668
                log_link_debug(link, "Interface is being renamed, pending initialization.");
×
1669
                return 0;
×
1670
        }
1671

1672
        return link_initialized(link, device);
1,652✔
1673
}
1674

1675
int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t action) {
1,723✔
1676
        int r, ifindex;
1,723✔
1677
        const char *s;
1,723✔
1678
        Link *link;
1,723✔
1679

1680
        assert(m);
1,723✔
1681
        assert(device);
1,723✔
1682

1683
        r = sd_device_get_ifindex(device, &ifindex);
1,723✔
1684
        if (r < 0)
1,723✔
1685
                return log_device_debug_errno(device, r, "Failed to get ifindex: %m");
×
1686

1687
        r = link_get_by_index(m, ifindex, &link);
1,723✔
1688
        if (r < 0) {
1,723✔
1689
                /* This error is not critical, as the corresponding rtnl message may be received later. */
1690
                log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
627✔
1691
                return 0;
627✔
1692
        }
1693

1694
        /* Let's unref the sd-device object assigned to the corresponding Link object, but keep the Link
1695
         * object here. It will be removed only when rtnetlink says so. */
1696
        if (action == SD_DEVICE_REMOVE) {
1,096✔
1697
                link->dev = sd_device_unref(link->dev);
338✔
1698
                return 0;
338✔
1699
        }
1700

1701
        r = device_is_renaming(device);
758✔
1702
        if (r < 0)
758✔
1703
                return log_device_debug_errno(device, r, "Failed to determine if the device is renaming or not: %m");
×
1704
        if (r > 0) {
758✔
1705
                log_device_debug(device, "Device is renaming, waiting for the interface to be renamed.");
8✔
1706
                /* TODO:
1707
                 * What happens when a device is initialized, then soon renamed after that? When we detect
1708
                 * such, maybe we should cancel or postpone all queued requests for the interface. */
1709
                return 0;
8✔
1710
        }
1711

1712
        r = sd_device_get_property_value(device, "ID_NET_MANAGED_BY", &s);
750✔
1713
        if (r < 0 && r != -ENOENT)
750✔
1714
                log_device_debug_errno(device, r, "Failed to get ID_NET_MANAGED_BY udev property, ignoring: %m");
×
1715
        if (r >= 0 && !streq(s, "io.systemd.Network")) {
750✔
1716
                log_device_debug(device, "Interface is requested to be managed by '%s', not managing the interface.", s);
×
1717
                link_set_state(link, LINK_STATE_UNMANAGED);
×
1718
                return 0;
×
1719
        }
1720

1721
        r = link_initialized(link, device);
750✔
1722
        if (r < 0)
750✔
1723
                link_enter_failed(link);
×
1724

1725
        return 0;
1726
}
1727

1728
static int link_carrier_gained(Link *link) {
1,157✔
1729
        LinkReconfigurationFlag flags = 0;
1,157✔
1730
        int r;
1,157✔
1731

1732
        assert(link);
1,157✔
1733

1734
        r = event_source_disable(link->carrier_lost_timer);
1,157✔
1735
        if (r < 0)
1,157✔
1736
                log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
×
1737

1738
        /* Process BindCarrier= setting specified by other interfaces. This is independent of the .network
1739
         * file assigned to this interface, but depends on .network files assigned to other interfaces.
1740
         * Hence, this can and should be called earlier. */
1741
        r = link_handle_bound_by_list(link);
1,157✔
1742
        if (r < 0)
1,157✔
1743
                return r;
1744

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

1763
        /* AP and P2P-GO interfaces may have a new SSID - update the link properties in case a new .network
1764
         * profile wants to match on it with SSID= in its [Match] section.
1765
         */
1766
        if (IN_SET(link->wlan_iftype, NL80211_IFTYPE_AP, NL80211_IFTYPE_P2P_GO)) {
1,157✔
1767
                r = link_get_wlan_interface(link);
×
1768
                if (r < 0)
×
1769
                        return r;
1770
        }
1771

1772
        /* At this stage, both wlan and link information should be up-to-date. Hence, it is not necessary to
1773
         * call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or NL80211_CMD_GET_STATION commands, and simply call
1774
         * link_reconfigure_impl(). Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1775
        r = link_reconfigure_impl(link, flags);
1,157✔
1776
        if (r != 0)
1,157✔
1777
                return r;
1778

1779
        if (link->iftype == ARPHRD_CAN)
1,157✔
1780
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1781
                return 0;
1782

1783
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
1,153✔
1784
                r = link_acquire_dynamic_conf(link);
608✔
1785
                if (r < 0)
608✔
1786
                        return r;
1787

1788
                r = link_request_static_configs(link);
608✔
1789
                if (r < 0)
608✔
1790
                        return r;
×
1791
        }
1792

1793
        return 0;
1794
}
1795

1796
static int link_carrier_lost_impl(Link *link) {
668✔
1797
        int ret = 0;
668✔
1798

1799
        assert(link);
668✔
1800

1801
        link->previous_ssid = mfree(link->previous_ssid);
668✔
1802

1803
        ret = link_handle_bound_by_list(link);
668✔
1804

1805
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
668✔
1806
                return ret;
668✔
1807

1808
        if (!link->network)
668✔
1809
                return ret;
1810

1811
        RET_GATHER(ret, link_stop_engines(link, /* may_keep_dynamic = */ false));
592✔
1812
        RET_GATHER(ret, link_drop_static_config(link));
592✔
1813

1814
        return ret;
1815
}
1816

1817
static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
×
1818
        Link *link = ASSERT_PTR(userdata);
×
1819
        int r;
×
1820

1821
        r = link_carrier_lost_impl(link);
×
1822
        if (r < 0) {
×
1823
                log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
×
1824
                link_enter_failed(link);
×
1825
        }
1826

1827
        return 0;
×
1828
}
1829

1830
static int link_carrier_lost(Link *link) {
684✔
1831
        uint16_t dhcp_mtu;
684✔
1832
        usec_t usec;
684✔
1833

1834
        assert(link);
684✔
1835

1836
        if (link->iftype == ARPHRD_CAN)
684✔
1837
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1838
                usec = 0;
1839

1840
        else if (!link->network)
680✔
1841
                usec = 0;
1842

1843
        else if (link->network->ignore_carrier_loss_set)
604✔
1844
                /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1845
                usec = link->network->ignore_carrier_loss_usec;
12✔
1846

1847
        else if (link->network->bond && link->wlan_iftype > 0)
592✔
1848
                /* Enslaving wlan interface to a bond disconnects from the connected AP, and causes its
1849
                 * carrier to be lost. See #19832. */
1850
                usec = 3 * USEC_PER_SEC;
1851

1852
        else if (link->network->dhcp_use_mtu &&
592✔
1853
                 link->dhcp_lease &&
4✔
1854
                 sd_dhcp_lease_get_mtu(link->dhcp_lease, &dhcp_mtu) >= 0 &&
2✔
1855
                 dhcp_mtu != link->original_mtu)
1✔
1856
                /* Some drivers reset interfaces when changing MTU. Resetting interfaces by the static
1857
                 * MTU should not cause any issues, as MTU is changed only once. However, setting MTU
1858
                 * through DHCP lease causes an infinite loop of resetting the interface. See #18738. */
1859
                usec = 5 * USEC_PER_SEC;
1860

1861
        else
1862
                /* Otherwise, use the implied default value. */
1863
                usec = link->network->ignore_carrier_loss_usec;
591✔
1864

1865
        if (usec == USEC_INFINITY)
603✔
1866
                return 0;
684✔
1867

1868
        if (usec == 0)
669✔
1869
                return link_carrier_lost_impl(link);
668✔
1870

1871
        return event_reset_time_relative(link->manager->event,
1✔
1872
                                         &link->carrier_lost_timer,
1873
                                         CLOCK_BOOTTIME,
1874
                                         usec,
1875
                                         0,
1876
                                         link_carrier_lost_handler,
1877
                                         link,
1878
                                         0,
1879
                                         "link-carrier-loss",
1880
                                         true);
1881
}
1882

1883
static int link_admin_state_up(Link *link) {
1,127✔
1884
        assert(link);
1,127✔
1885

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

1889
        if (!link->network)
1,127✔
1890
                return 0;
1891

1892
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
581✔
1893
                log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
4✔
1894
                return link_request_to_bring_up_or_down(link, /* up = */ false);
4✔
1895
        }
1896

1897
        /* We set the ipv6 mtu after the device mtu, but the kernel resets
1898
         * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1899
        (void) link_set_ipv6_mtu(link, LOG_INFO);
577✔
1900

1901
        return 0;
577✔
1902
}
1903

1904
static int link_admin_state_down(Link *link) {
652✔
1905
        assert(link);
652✔
1906

1907
        link_forget_nexthops(link);
652✔
1908
        link_forget_routes(link);
652✔
1909

1910
        if (!link->network)
652✔
1911
                return 0;
1912

1913
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
579✔
1914
                log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
9✔
1915
                return link_request_to_bring_up_or_down(link, /* up = */ true);
9✔
1916
        }
1917

1918
        return 0;
1919
}
1920

1921
static bool link_is_enslaved(Link *link) {
6,525✔
1922
        if (link->flags & IFF_SLAVE)
6,525✔
1923
                return true;
1924

1925
        if (link->master_ifindex > 0)
6,484✔
1926
                return true;
224✔
1927

1928
        return false;
1929
}
1930

1931
void link_update_operstate(Link *link, bool also_update_master) {
12,205✔
1932
        LinkOperationalState operstate;
12,205✔
1933
        LinkCarrierState carrier_state;
12,205✔
1934
        LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
12,205✔
1935
        LinkOnlineState online_state;
12,205✔
1936
        _cleanup_strv_free_ char **p = NULL;
12,205✔
1937
        bool changed = false;
12,205✔
1938

1939
        assert(link);
12,205✔
1940

1941
        if (link->kernel_operstate == IF_OPER_DORMANT)
12,205✔
1942
                carrier_state = LINK_CARRIER_STATE_DORMANT;
1943
        else if (link_has_carrier(link)) {
12,205✔
1944
                if (link_is_enslaved(link))
6,525✔
1945
                        carrier_state = LINK_CARRIER_STATE_ENSLAVED;
1946
                else
1947
                        carrier_state = LINK_CARRIER_STATE_CARRIER;
1948
        } else if (link->flags & IFF_UP)
5,680✔
1949
                carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
1950
        else
1951
                carrier_state = LINK_CARRIER_STATE_OFF;
1952

1953
        if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
1954
                Link *slave;
6,525✔
1955

1956
                SET_FOREACH(slave, link->slaves) {
6,731✔
1957
                        link_update_operstate(slave, false);
206✔
1958

1959
                        if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
206✔
1960
                                carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
50✔
1961
                }
1962
        }
1963

1964
        link_get_address_states(link, &ipv4_address_state, &ipv6_address_state, &address_state);
12,205✔
1965

1966
        /* Mapping of address and carrier state vs operational state
1967
         *                                                     carrier state
1968
         *                          | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
1969
         *                 ------------------------------------------------------------------------------
1970
         *                 off      | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
1971
         * address_state   degraded | off | no-carrier | dormant | degraded         | degraded | enslaved
1972
         *                 routable | off | no-carrier | dormant | routable         | routable | routable
1973
         */
1974

1975
        if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_ROUTABLE)
12,205✔
1976
                operstate = LINK_OPERSTATE_ROUTABLE;
1977
        else if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_DEGRADED)
20✔
1978
                operstate = LINK_OPERSTATE_DEGRADED;
1979
        else if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
12,176✔
1980
                operstate = (LinkOperationalState) carrier_state;
1981
        else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
4,266✔
1982
                operstate = LINK_OPERSTATE_ROUTABLE;
1983
        else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1,280✔
1984
                operstate = LINK_OPERSTATE_DEGRADED;
1985
        else
1986
                operstate = LINK_OPERSTATE_ENSLAVED;
22✔
1987

1988
        LinkOperationalStateRange req;
12,205✔
1989
        link_required_operstate_for_online(link, &req);
12,205✔
1990

1991
        /* Only determine online state for managed links with RequiredForOnline=yes */
1992
        if (!link->network || !link->network->required_for_online)
12,205✔
1993
                online_state = _LINK_ONLINE_STATE_INVALID;
1994

1995
        else if (!operational_state_is_in_range(operstate, &req))
8,063✔
1996
                online_state = LINK_ONLINE_STATE_OFFLINE;
1997

1998
        else {
1999
                AddressFamily required_family = link_required_family_for_online(link);
4,191✔
2000
                bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
4,191✔
2001
                bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
4,191✔
2002

2003
                /* The operational state is within the range required for online.
2004
                 * If a particular address family is also required, we might revert
2005
                 * to offline in the blocks below. */
2006
                online_state = LINK_ONLINE_STATE_ONLINE;
4,191✔
2007

2008
                if (req.min >= LINK_OPERSTATE_DEGRADED) {
4,191✔
2009
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
4,014✔
2010
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2011
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
4,014✔
2012
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2013
                }
2014

2015
                if (req.min >= LINK_OPERSTATE_ROUTABLE) {
4,191✔
2016
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2017
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2018
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2019
                                online_state = LINK_ONLINE_STATE_OFFLINE;
3✔
2020
                }
2021
        }
2022

2023
        if (link->carrier_state != carrier_state) {
12,205✔
2024
                link->carrier_state = carrier_state;
4,249✔
2025
                changed = true;
4,249✔
2026
                if (strv_extend(&p, "CarrierState") < 0)
4,249✔
2027
                        log_oom();
×
2028
        }
2029

2030
        if (link->address_state != address_state) {
12,205✔
2031
                link->address_state = address_state;
1,788✔
2032
                changed = true;
1,788✔
2033
                if (strv_extend(&p, "AddressState") < 0)
1,788✔
2034
                        log_oom();
×
2035
        }
2036

2037
        if (link->ipv4_address_state != ipv4_address_state) {
12,205✔
2038
                link->ipv4_address_state = ipv4_address_state;
845✔
2039
                changed = true;
845✔
2040
                if (strv_extend(&p, "IPv4AddressState") < 0)
845✔
2041
                        log_oom();
×
2042
        }
2043

2044
        if (link->ipv6_address_state != ipv6_address_state) {
12,205✔
2045
                link->ipv6_address_state = ipv6_address_state;
1,520✔
2046
                changed = true;
1,520✔
2047
                if (strv_extend(&p, "IPv6AddressState") < 0)
1,520✔
2048
                        log_oom();
×
2049
        }
2050

2051
        if (link->operstate != operstate) {
12,205✔
2052
                link->operstate = operstate;
5,158✔
2053
                changed = true;
5,158✔
2054
                if (strv_extend(&p, "OperationalState") < 0)
5,158✔
2055
                        log_oom();
×
2056
        }
2057

2058
        if (link->online_state != online_state) {
12,205✔
2059
                link->online_state = online_state;
1,650✔
2060
                changed = true;
1,650✔
2061
                if (strv_extend(&p, "OnlineState") < 0)
1,650✔
2062
                        log_oom();
×
2063
        }
2064

2065
        if (p)
12,205✔
2066
                link_send_changed_strv(link, p);
7,172✔
2067
        if (changed)
12,205✔
2068
                link_dirty(link);
7,172✔
2069

2070
        if (also_update_master) {
12,205✔
2071
                Link *master;
11,999✔
2072

2073
                if (link_get_master(link, &master) >= 0)
11,999✔
2074
                        link_update_operstate(master, true);
142✔
2075
        }
2076
}
12,205✔
2077

2078
#define FLAG_STRING(string, flag, old, new)                      \
2079
        (((old ^ new) & flag)                                    \
2080
         ? ((old & flag) ? (" -" string) : (" +" string))        \
2081
         : "")
2082

2083
static int link_update_flags(Link *link, sd_netlink_message *message) {
9,245✔
2084
        bool link_was_admin_up, had_carrier;
9,245✔
2085
        uint8_t operstate;
9,245✔
2086
        unsigned flags;
9,245✔
2087
        int r;
9,245✔
2088

2089
        assert(link);
9,245✔
2090
        assert(message);
9,245✔
2091

2092
        r = sd_rtnl_message_link_get_flags(message, &flags);
9,245✔
2093
        if (r < 0)
9,245✔
2094
                return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
×
2095

2096
        r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
9,245✔
2097
        if (r == -ENODATA)
9,245✔
2098
                /* If we got a message without operstate, assume the state was unchanged. */
2099
                operstate = link->kernel_operstate;
×
2100
        else if (r < 0)
9,245✔
2101
                return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
×
2102

2103
        if (link->flags == flags && link->kernel_operstate == operstate)
9,245✔
2104
                return 0;
2105

2106
        if (link->flags != flags) {
4,228✔
2107
                unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
4,228✔
2108

2109
                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,661✔
2110
                               FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
2111
                               FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
2112
                               FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
2113
                               FLAG_STRING("UP", IFF_UP, link->flags, flags),
2114
                               FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
2115
                               FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
2116
                               FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
2117
                               FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
2118
                               FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
2119
                               FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
2120
                               FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
2121
                               FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
2122
                               FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
2123
                               FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
2124
                               FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
2125
                               FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
2126
                               FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
2127
                               FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
2128
                               FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
2129

2130
                unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
4,228✔
2131
                                  IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
2132
                                  IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
2133
                                  IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
2134
                                  IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
2135
                                  IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
2136
                unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
4,228✔
2137
                unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
4,228✔
2138

2139
                if (unknown_flags_added)
4,228✔
2140
                        log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
×
2141

2142
                if (unknown_flags_removed)
4,228✔
2143
                        log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
×
2144
        }
2145

2146
        link_was_admin_up = link->flags & IFF_UP;
4,228✔
2147
        had_carrier = link_has_carrier(link);
4,228✔
2148

2149
        link->flags = flags;
4,228✔
2150
        link->kernel_operstate = operstate;
4,228✔
2151

2152
        link_update_operstate(link, true);
4,228✔
2153

2154
        if (!link_was_admin_up && (link->flags & IFF_UP)) {
4,228✔
2155
                log_link_info(link, "Link UP");
1,127✔
2156

2157
                r = link_admin_state_up(link);
1,127✔
2158
                if (r < 0)
1,127✔
2159
                        return r;
2160
        } else if (link_was_admin_up && !(link->flags & IFF_UP)) {
1,017✔
2161
                log_link_info(link, "Link DOWN");
652✔
2162

2163
                r = link_admin_state_down(link);
652✔
2164
                if (r < 0)
652✔
2165
                        return r;
2166
        }
2167

2168
        if (!had_carrier && link_has_carrier(link)) {
4,228✔
2169
                log_link_info(link, "Gained carrier");
1,157✔
2170

2171
                r = link_carrier_gained(link);
1,157✔
2172
                if (r < 0)
1,157✔
2173
                        return r;
×
2174
        } else if (had_carrier && !link_has_carrier(link)) {
699✔
2175
                log_link_info(link, "Lost carrier");
684✔
2176

2177
                r = link_carrier_lost(link);
684✔
2178
                if (r < 0)
684✔
2179
                        return r;
×
2180
        }
2181

2182
        return 0;
2183
}
2184

2185
static int link_update_master(Link *link, sd_netlink_message *message) {
9,245✔
2186
        int master_ifindex, r;
9,245✔
2187

2188
        assert(link);
9,245✔
2189
        assert(message);
9,245✔
2190

2191
        r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
9,245✔
2192
        if (r == -ENODATA)
9,245✔
2193
                return 0;
9,245✔
2194
        if (r < 0)
423✔
2195
                return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
×
2196

2197
        if (master_ifindex == link->ifindex)
423✔
2198
                master_ifindex = 0;
7✔
2199

2200
        if (master_ifindex != link->master_ifindex) {
423✔
2201
                if (link->master_ifindex == 0)
34✔
2202
                        log_link_debug(link, "Attached to master interface: %i", master_ifindex);
34✔
2203
                else if (master_ifindex == 0)
×
2204
                        log_link_debug(link, "Detached from master interface: %i", link->master_ifindex);
×
2205
                else
2206
                        log_link_debug(link, "Master interface changed: %i %s %i", link->master_ifindex,
×
2207
                                       special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), master_ifindex);
2208

2209
                link_drop_from_master(link);
34✔
2210
                link->master_ifindex = master_ifindex;
34✔
2211
        }
2212

2213
        r = link_append_to_master(link);
423✔
2214
        if (r < 0)
423✔
2215
                return log_link_debug_errno(link, r, "Failed to append link to master: %m");
×
2216

2217
        return 0;
2218
}
2219

2220
static int link_update_driver(Link *link, sd_netlink_message *message) {
9,245✔
2221
        int r;
9,245✔
2222

2223
        assert(link);
9,245✔
2224
        assert(link->manager);
9,245✔
2225
        assert(message);
9,245✔
2226

2227
        /* Driver is already read. Assuming the driver is never changed. */
2228
        if (link->ethtool_driver_read)
9,245✔
2229
                return 0;
2230

2231
        /* When udevd is running, read the driver after the interface is initialized by udevd.
2232
         * Otherwise, ethtool may not work correctly. See issue #22538.
2233
         * When udevd is not running, read the value when the interface is detected. */
2234
        if (udev_available() && !link->dev)
5,210✔
2235
                return 0;
2236

2237
        link->ethtool_driver_read = true;
2,368✔
2238

2239
        r = ethtool_get_driver(&link->manager->ethtool_fd, link->ifname, &link->driver);
2,368✔
2240
        if (r < 0) {
2,368✔
2241
                log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
463✔
2242
                return 0;
463✔
2243
        }
2244

2245
        log_link_debug(link, "Found driver: %s", strna(link->driver));
1,905✔
2246

2247
        if (streq_ptr(link->driver, "dsa")) {
1,905✔
2248
                uint32_t dsa_master_ifindex = 0;
×
2249

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

2254
                if (dsa_master_ifindex > INT_MAX) {
×
2255
                        log_link_debug(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
×
2256
                                       dsa_master_ifindex);
2257
                        dsa_master_ifindex = 0;
×
2258
                }
2259

2260
                link->dsa_master_ifindex = (int) dsa_master_ifindex;
×
2261
        }
2262

2263
        return 1; /* needs reconfigure */
2264
}
2265

2266
static int link_update_permanent_hardware_address_from_ethtool(Link *link, sd_netlink_message *message) {
4,934✔
2267
        int r;
4,934✔
2268

2269
        assert(link);
4,934✔
2270
        assert(link->manager);
4,934✔
2271
        assert(message);
4,934✔
2272

2273
        if (link->ethtool_permanent_hw_addr_read)
4,934✔
2274
                return 0;
2275

2276
        /* When udevd is running, read the permanent hardware address after the interface is
2277
         * initialized by udevd. Otherwise, ethtool may not work correctly. See issue #22538.
2278
         * When udevd is not running, read the value when the interface is detected. */
2279
        if (udev_available() && !link->dev)
4,931✔
2280
                return 0;
2281

2282
        /* If the interface does not have a hardware address, then it will not have a permanent address either. */
2283
        r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, NULL);
2,272✔
2284
        if (r == -ENODATA)
2,272✔
2285
                return 0;
2286
        if (r < 0)
2,130✔
2287
                return log_link_debug_errno(link, r, "Failed to read IFLA_ADDRESS attribute: %m");
×
2288

2289
        link->ethtool_permanent_hw_addr_read = true;
2,130✔
2290

2291
        r = ethtool_get_permanent_hw_addr(&link->manager->ethtool_fd, link->ifname, &link->permanent_hw_addr);
2,130✔
2292
        if (r < 0)
2,130✔
2293
                log_link_debug_errno(link, r, "Permanent hardware address not found, continuing without: %m");
1✔
2294

2295
        return 0;
2296
}
2297

2298
static int link_update_permanent_hardware_address(Link *link, sd_netlink_message *message) {
9,245✔
2299
        int r;
9,245✔
2300

2301
        assert(link);
9,245✔
2302
        assert(link->manager);
9,245✔
2303
        assert(message);
9,245✔
2304

2305
        if (link->permanent_hw_addr.length > 0)
9,245✔
2306
                return 0;
2307

2308
        r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
5,111✔
2309
        if (r < 0) {
5,111✔
2310
                if (r != -ENODATA)
4,934✔
2311
                        return log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute: %m");
×
2312

2313
                /* Fallback to ethtool for older kernels. */
2314
                r = link_update_permanent_hardware_address_from_ethtool(link, message);
4,934✔
2315
                if (r < 0)
4,934✔
2316
                        return r;
2317
        }
2318

2319
        if (link->permanent_hw_addr.length > 0)
5,111✔
2320
                log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
2,306✔
2321

2322
        return 1; /* needs reconfigure */
2323
}
2324

2325
static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
9,245✔
2326
        struct hw_addr_data addr;
9,245✔
2327
        int r;
9,245✔
2328

2329
        assert(link);
9,245✔
2330
        assert(message);
9,245✔
2331

2332
        r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
9,245✔
2333
        if (r < 0 && r != -ENODATA)
9,245✔
2334
                return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
×
2335

2336
        r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
9,245✔
2337
        if (r == -ENODATA)
9,245✔
2338
                return 0;
2339
        if (r < 0)
9,042✔
2340
                return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
×
2341

2342
        if (hw_addr_equal(&link->hw_addr, &addr))
9,042✔
2343
                return 0;
2344

2345
        if (link->hw_addr.length == 0)
2,668✔
2346
                log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
2,495✔
2347
        else {
2348
                log_link_debug(link, "Hardware address is changed: %s %s %s",
173✔
2349
                               HW_ADDR_TO_STR(&link->hw_addr),
2350
                               special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
2351
                               HW_ADDR_TO_STR(&addr));
2352

2353
                hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
173✔
2354
        }
2355

2356
        link->hw_addr = addr;
2,668✔
2357

2358
        if (!hw_addr_is_null(&link->hw_addr)) {
2,668✔
2359
                r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
969✔
2360
                if (r == -EEXIST && streq_ptr(link->kind, "bond"))
969✔
2361
                        /* bonding master and its slaves have the same hardware address. */
2362
                        r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
2✔
2363
                if (r < 0)
969✔
2364
                        log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
37✔
2365
        }
2366

2367
        r = ipv4acd_update_mac(link);
2,668✔
2368
        if (r < 0)
2,668✔
2369
                return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
×
2370

2371
        r = ipv4ll_update_mac(link);
2,668✔
2372
        if (r < 0)
2,668✔
2373
                return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
×
2374

2375
        r = dhcp4_update_mac(link);
2,668✔
2376
        if (r < 0)
2,668✔
2377
                return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
×
2378

2379
        r = dhcp6_update_mac(link);
2,668✔
2380
        if (r < 0)
2,668✔
2381
                return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
×
2382

2383
        r = radv_update_mac(link);
2,668✔
2384
        if (r < 0)
2,668✔
2385
                return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
×
2386

2387
        if (link->ndisc && link->hw_addr.length == ETH_ALEN) {
2,668✔
2388
                r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
×
2389
                if (r < 0)
×
2390
                        return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
×
2391
        }
2392

2393
        if (link->lldp_rx) {
2,668✔
2394
                r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
22✔
2395
                if (r < 0)
22✔
2396
                        return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
×
2397
        }
2398

2399
        if (link->lldp_tx) {
2,668✔
2400
                r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
×
2401
                if (r < 0)
×
2402
                        return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
×
2403
        }
2404

2405
        return 1; /* needs reconfigure */
2406
}
2407

2408
static int link_update_mtu(Link *link, sd_netlink_message *message) {
9,245✔
2409
        uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
9,245✔
2410
        int r;
9,245✔
2411

2412
        assert(link);
9,245✔
2413
        assert(message);
9,245✔
2414

2415
        r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
9,245✔
2416
        if (r == -ENODATA)
9,245✔
2417
                return 0;
9,245✔
2418
        if (r < 0)
9,245✔
2419
                return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
×
2420
        if (mtu == 0)
9,245✔
2421
                return 0;
2422

2423
        r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
9,224✔
2424
        if (r < 0 && r != -ENODATA)
9,224✔
2425
                return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
×
2426

2427
        r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
9,224✔
2428
        if (r < 0 && r != -ENODATA)
9,224✔
2429
                return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
×
2430

2431
        if (max_mtu == 0)
9,224✔
2432
                max_mtu = UINT32_MAX;
6,135✔
2433

2434
        link->min_mtu = min_mtu;
9,224✔
2435
        link->max_mtu = max_mtu;
9,224✔
2436

2437
        if (link->original_mtu == 0) {
9,224✔
2438
                link->original_mtu = mtu;
2,556✔
2439
                log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2,556✔
2440
                               link->original_mtu, link->min_mtu, link->max_mtu);
2441
        }
2442

2443
        if (link->mtu == mtu)
9,224✔
2444
                return 0;
2445

2446
        if (link->mtu != 0)
2,639✔
2447
                log_link_debug(link, "MTU is changed: %"PRIu32" %s %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
83✔
2448
                               link->mtu, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), mtu,
2449
                               link->min_mtu, link->max_mtu);
2450

2451
        link->mtu = mtu;
2,639✔
2452

2453
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
2,639✔
2454
                /* The kernel resets IPv6 MTU after changing device MTU. So, we need to re-set IPv6 MTU again. */
2455
                (void) link_set_ipv6_mtu_async(link);
42✔
2456

2457
        if (link->dhcp_client) {
2,639✔
2458
                r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
4✔
2459
                if (r < 0)
4✔
2460
                        return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
×
2461
        }
2462

2463
        if (link->radv) {
2,639✔
2464
                r = sd_radv_set_mtu(link->radv, link->mtu);
×
2465
                if (r < 0)
×
2466
                        return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
×
2467
        }
2468

2469
        return 0;
2470
}
2471

2472
static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
9,245✔
2473
        _cleanup_strv_free_ char **altnames = NULL;
9,245✔
2474
        int r;
9,245✔
2475

2476
        assert(link);
9,245✔
2477
        assert(message);
9,245✔
2478

2479
        r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
9,245✔
2480
        if (r == -ENODATA)
9,245✔
2481
                /* The message does not have IFLA_PROP_LIST container attribute. It does not mean the
2482
                 * interface has no alternative name. */
2483
                return 0;
2484
        if (r < 0)
94✔
2485
                return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
×
2486

2487
        if (strv_equal(altnames, link->alternative_names))
94✔
2488
                return 0;
2489

2490
        STRV_FOREACH(n, link->alternative_names)
48✔
2491
                hashmap_remove(link->manager->links_by_name, *n);
11✔
2492

2493
        strv_free_and_replace(link->alternative_names, altnames);
37✔
2494

2495
        STRV_FOREACH(n, link->alternative_names) {
82✔
2496
                r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
45✔
2497
                if (r < 0)
45✔
2498
                        return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
×
2499
        }
2500

2501
        return 1; /* needs reconfigure */
2502
}
2503

2504
static int link_update_name(Link *link, sd_netlink_message *message) {
9,245✔
2505
        char ifname_from_index[IF_NAMESIZE];
9,245✔
2506
        const char *ifname;
9,245✔
2507
        int r;
9,245✔
2508

2509
        assert(link);
9,245✔
2510
        assert(message);
9,245✔
2511

2512
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
9,245✔
2513
        if (r == -ENODATA)
9,245✔
2514
                /* Hmm?? But ok. */
2515
                return 0;
9,245✔
2516
        if (r < 0)
9,245✔
2517
                return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
×
2518

2519
        if (streq(ifname, link->ifname))
9,245✔
2520
                return 0;
2521

2522
        r = format_ifname(link->ifindex, ifname_from_index);
22✔
2523
        if (r < 0)
22✔
2524
                return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
×
2525

2526
        if (!streq(ifname, ifname_from_index)) {
22✔
2527
                log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "
1✔
2528
                               "with the name currently configured on the actual interface '%s'. Ignoring.",
2529
                               ifname, ifname_from_index);
2530
                return 0;
1✔
2531
        }
2532

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

2535
        hashmap_remove(link->manager->links_by_name, link->ifname);
21✔
2536

2537
        r = free_and_strdup(&link->ifname, ifname);
21✔
2538
        if (r < 0)
21✔
2539
                return log_oom_debug();
×
2540

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

2545
        if (link->dhcp_client) {
21✔
2546
                r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname);
×
2547
                if (r < 0)
×
2548
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m");
×
2549
        }
2550

2551
        if (link->dhcp6_client) {
21✔
2552
                r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
×
2553
                if (r < 0)
×
2554
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCPv6 client: %m");
×
2555
        }
2556

2557
        if (link->ndisc) {
21✔
2558
                r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
×
2559
                if (r < 0)
×
2560
                        return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
×
2561
        }
2562

2563
        if (link->dhcp_server) {
21✔
2564
                r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
×
2565
                if (r < 0)
×
2566
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
×
2567
        }
2568

2569
        if (link->radv) {
21✔
2570
                r = sd_radv_set_ifname(link->radv, link->ifname);
×
2571
                if (r < 0)
×
2572
                        return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
×
2573
        }
2574

2575
        if (link->lldp_rx) {
21✔
2576
                r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
2✔
2577
                if (r < 0)
2✔
2578
                        return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
×
2579
        }
2580

2581
        if (link->lldp_tx) {
21✔
2582
                r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
×
2583
                if (r < 0)
×
2584
                        return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
×
2585
        }
2586

2587
        if (link->ipv4ll) {
21✔
2588
                r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
×
2589
                if (r < 0)
×
2590
                        return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
×
2591
        }
2592

2593
        r = ipv4acd_set_ifname(link);
21✔
2594
        if (r < 0)
21✔
2595
                return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
×
2596

2597
        return 1; /* needs reconfigure */
2598
}
2599

2600
static int link_update(Link *link, sd_netlink_message *message) {
9,245✔
2601
        bool needs_reconfigure = false;
9,245✔
2602
        int r;
9,245✔
2603

2604
        assert(link);
9,245✔
2605
        assert(message);
9,245✔
2606

2607
        r = link_update_name(link, message);
9,245✔
2608
        if (r < 0)
9,245✔
2609
                return r;
2610
        needs_reconfigure = needs_reconfigure || r > 0;
9,245✔
2611

2612
        r = link_update_alternative_names(link, message);
9,245✔
2613
        if (r < 0)
9,245✔
2614
                return r;
2615
        needs_reconfigure = needs_reconfigure || r > 0;
9,245✔
2616

2617
        r = link_update_mtu(link, message);
9,245✔
2618
        if (r < 0)
9,245✔
2619
                return r;
2620

2621
        r = link_update_driver(link, message);
9,245✔
2622
        if (r < 0)
9,245✔
2623
                return r;
2624
        needs_reconfigure = needs_reconfigure || r > 0;
9,245✔
2625

2626
        r = link_update_permanent_hardware_address(link, message);
9,245✔
2627
        if (r < 0)
9,245✔
2628
                return r;
2629
        needs_reconfigure = needs_reconfigure || r > 0;
9,245✔
2630

2631
        r = link_update_hardware_address(link, message);
9,245✔
2632
        if (r < 0)
9,245✔
2633
                return r;
2634
        needs_reconfigure = needs_reconfigure || r > 0;
9,245✔
2635

2636
        r = link_update_master(link, message);
9,245✔
2637
        if (r < 0)
9,245✔
2638
                return r;
2639

2640
        r = link_update_ipv6ll_addrgen_mode(link, message);
9,245✔
2641
        if (r < 0)
9,245✔
2642
                return r;
2643

2644
        r = link_update_flags(link, message);
9,245✔
2645
        if (r < 0)
9,245✔
2646
                return r;
2647

2648
        r = link_update_bridge_vlan(link, message);
9,245✔
2649
        if (r < 0)
9,245✔
2650
                return r;
2651

2652
        return needs_reconfigure;
9,245✔
2653
}
2654

2655
static Link *link_drop_or_unref(Link *link) {
×
2656
        if (!link)
×
2657
                return NULL;
2658
        if (!link->manager)
×
2659
                return link_unref(link);
×
2660
        return link_drop(link);
×
2661
}
2662

2663
DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2,556✔
2664

2665
static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
2,556✔
2666
        _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL;
2,556✔
2667
        _cleanup_(link_drop_or_unrefp) Link *link = NULL;
2,556✔
2668
        unsigned short iftype;
2,556✔
2669
        int r, ifindex;
2,556✔
2670

2671
        assert(manager);
2,556✔
2672
        assert(message);
2,556✔
2673
        assert(ret);
2,556✔
2674

2675
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2,556✔
2676
        if (r < 0)
2,556✔
2677
                return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
×
2678
        else if (ifindex <= 0)
2,556✔
2679
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
×
2680

2681
        r = sd_rtnl_message_link_get_type(message, &iftype);
2,556✔
2682
        if (r < 0)
2,556✔
2683
                return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
×
2684

2685
        r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2,556✔
2686
        if (r < 0)
2,556✔
2687
                return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
×
2688

2689
        /* check for link kind */
2690
        r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2,556✔
2691
        if (r >= 0) {
2,556✔
2692
                r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2,054✔
2693
                if (r < 0 && r != -ENODATA)
2,054✔
2694
                        return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
×
2695
                r = sd_netlink_message_exit_container(message);
2,054✔
2696
                if (r < 0)
2,054✔
2697
                        return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
×
2698
        }
2699

2700
        if (!manager->test_mode) {
2,556✔
2701
                /* Do not update state files when running in test mode. */
2702
                if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2,555✔
2703
                        return log_oom_debug();
×
2704

2705
                if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2,555✔
2706
                        return log_oom_debug();
×
2707
        }
2708

2709
        link = new(Link, 1);
2,556✔
2710
        if (!link)
2,556✔
2711
                return -ENOMEM;
2712

2713
        *link = (Link) {
2,556✔
2714
                .n_ref = 1,
2715
                .state = LINK_STATE_PENDING,
2716
                .online_state = _LINK_ONLINE_STATE_INVALID,
2717
                .automatic_reconfigure_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_SEC, .burst = 5 },
2718
                .ifindex = ifindex,
2719
                .iftype = iftype,
2720
                .ifname = TAKE_PTR(ifname),
2,556✔
2721
                .kind = TAKE_PTR(kind),
2,556✔
2722

2723
                .bridge_vlan_pvid = UINT16_MAX,
2724

2725
                .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2726

2727
                .state_file = TAKE_PTR(state_file),
2,556✔
2728
                .lease_file = TAKE_PTR(lease_file),
2,556✔
2729

2730
                .n_dns = UINT_MAX,
2731
                .dns_default_route = -1,
2732
                .llmnr = _RESOLVE_SUPPORT_INVALID,
2733
                .mdns = _RESOLVE_SUPPORT_INVALID,
2734
                .dnssec_mode = _DNSSEC_MODE_INVALID,
2735
                .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2736
        };
2737

2738
        r = hashmap_ensure_put(&manager->links_by_index, NULL, INT_TO_PTR(link->ifindex), link);
2,556✔
2739
        if (r < 0)
2,556✔
2740
                return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
×
2741

2742
        link->manager = manager;
2,556✔
2743

2744
        r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
2,556✔
2745
        if (r < 0)
2,556✔
2746
                return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
×
2747

2748
        log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
3,058✔
2749
                       link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2750

2751
        /* If contained in this set, the link is wireless and the corresponding NL80211_CMD_NEW_INTERFACE
2752
         * message arrived too early. Request the wireless link information again.
2753
         */
2754
        if (set_remove(manager->new_wlan_ifindices, INT_TO_PTR(link->ifindex))) {
2,556✔
2755
                r = link_get_wlan_interface(link);
×
2756
                if (r < 0)
×
2757
                        log_link_warning_errno(link, r, "Failed to get wireless interface, ignoring: %m");
×
2758
        }
2759

2760
        *ret = TAKE_PTR(link);
2,556✔
2761
        return 0;
2,556✔
2762
}
2763

2764
int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
5,779✔
2765
        Link *link = NULL;
5,779✔
2766
        NetDev *netdev = NULL;
5,779✔
2767
        uint16_t type;
5,779✔
2768
        const char *name;
5,779✔
2769
        int r, ifindex;
5,779✔
2770

2771
        assert(rtnl);
5,779✔
2772
        assert(message);
5,779✔
2773
        assert(manager);
5,779✔
2774

2775
        if (sd_netlink_message_is_error(message)) {
5,779✔
2776
                r = sd_netlink_message_get_errno(message);
×
2777
                if (r < 0)
×
2778
                        log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
×
2779

2780
                return 0;
×
2781
        }
2782

2783
        r = sd_netlink_message_get_type(message, &type);
5,779✔
2784
        if (r < 0) {
5,779✔
2785
                log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
×
2786
                return 0;
×
2787
        } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
5,779✔
2788
                log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
×
2789
                return 0;
×
2790
        }
2791

2792
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
5,779✔
2793
        if (r < 0) {
5,779✔
2794
                log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
×
2795
                return 0;
×
2796
        } else if (ifindex <= 0) {
5,779✔
2797
                log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
×
2798
                return 0;
×
2799
        }
2800

2801
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
5,779✔
2802
        if (r < 0) {
5,779✔
2803
                log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
×
2804
                return 0;
×
2805
        }
2806

2807
        (void) link_get_by_index(manager, ifindex, &link);
5,779✔
2808
        (void) netdev_get(manager, name, &netdev);
5,779✔
2809

2810
        switch (type) {
5,779✔
2811
        case RTM_NEWLINK:
4,976✔
2812
                if (netdev) {
4,976✔
2813
                        /* netdev exists, so make sure the ifindex matches */
2814
                        r = netdev_set_ifindex(netdev, message);
2,501✔
2815
                        if (r < 0) {
2,501✔
2816
                                log_netdev_warning_errno(netdev, r, "Could not process new link message for netdev, ignoring: %m");
×
2817
                                netdev_enter_failed(netdev);
×
2818
                                return 0;
×
2819
                        }
2820
                }
2821

2822
                if (!link) {
4,976✔
2823
                        /* link is new, so add it */
2824
                        r = link_new(manager, message, &link);
2,556✔
2825
                        if (r < 0) {
2,556✔
2826
                                log_warning_errno(r, "Could not process new link message: %m");
×
2827
                                return 0;
×
2828
                        }
2829

2830
                        r = link_update(link, message);
2,556✔
2831
                        if (r < 0) {
2,556✔
2832
                                log_link_warning_errno(link, r, "Could not process link message: %m");
×
2833
                                link_enter_failed(link);
×
2834
                                return 0;
×
2835
                        }
2836

2837
                        if (link->manager->test_mode) {
2,556✔
2838
                                log_link_debug(link, "Running in test mode, refusing to enter initialized state.");
1✔
2839
                                link_set_state(link, LINK_STATE_UNMANAGED);
1✔
2840
                                return 0;
1✔
2841
                        }
2842

2843
                        /* Do not enter initialized state if we are enumerating. */
2844
                        if (manager->enumerating)
2,555✔
2845
                                return 0;
2846

2847
                        r = link_check_initialized(link);
855✔
2848
                        if (r < 0) {
855✔
2849
                                log_link_warning_errno(link, r, "Failed to check link is initialized: %m");
×
2850
                                link_enter_failed(link);
×
2851
                                return 0;
×
2852
                        }
2853
                } else {
2854
                        r = link_update(link, message);
2,420✔
2855
                        if (r < 0) {
2,420✔
2856
                                log_link_warning_errno(link, r, "Could not process link message: %m");
×
2857
                                link_enter_failed(link);
×
2858
                                return 0;
×
2859
                        }
2860
                        if (r == 0)
2,420✔
2861
                                return 0;
2862

2863
                        if (link->manager->test_mode) {
437✔
2864
                                log_link_debug(link, "Running in test mode, refusing to configure interface.");
×
2865
                                link_set_state(link, LINK_STATE_UNMANAGED);
×
2866
                                return 0;
×
2867
                        }
2868

2869
                        /* Do not configure interface if we are enumerating. */
2870
                        if (manager->enumerating)
437✔
2871
                                return 0;
2872

2873
                        r = link_reconfigure_impl(link, /* flags = */ 0);
436✔
2874
                        if (r < 0) {
436✔
2875
                                log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
2876
                                link_enter_failed(link);
×
2877
                                return 0;
×
2878
                        }
2879
                }
2880
                break;
2881

2882
        case RTM_DELLINK:
803✔
2883
                link_drop(link);
803✔
2884
                netdev_drop(netdev);
803✔
2885
                break;
2886

2887
        default:
×
2888
                assert_not_reached();
×
2889
        }
2890

2891
        return 1;
2892
}
2893

2894
int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
4,271✔
2895
        uint16_t message_type;
4,271✔
2896
        int r;
4,271✔
2897

2898
        assert(m);
4,271✔
2899
        assert(link);
4,271✔
2900
        assert(error_msg);
4,271✔
2901

2902
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
4,271✔
2903
                return 0;
4,271✔
2904

2905
        r = sd_netlink_message_get_errno(m);
4,269✔
2906
        if (r < 0) {
4,269✔
2907
                log_link_message_warning_errno(link, m, r, error_msg);
×
2908
                link_enter_failed(link);
×
2909
                return 0;
×
2910
        }
2911

2912
        r = sd_netlink_message_get_type(m, &message_type);
4,269✔
2913
        if (r < 0) {
4,269✔
2914
                log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
×
2915
                return 0;
×
2916
        }
2917
        if (message_type != RTM_NEWLINK) {
4,269✔
2918
                log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
×
2919
                return 0;
×
2920
        }
2921

2922
        r = link_update(link, m);
4,269✔
2923
        if (r < 0) {
4,269✔
2924
                link_enter_failed(link);
×
2925
                return 0;
×
2926
        }
2927

2928
        return 1;
2929
}
2930

2931
int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
3,139✔
2932
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
3,139✔
2933
        int r;
3,139✔
2934

2935
        assert(link);
3,139✔
2936
        assert(link->manager);
3,139✔
2937
        assert(link->manager->rtnl);
3,139✔
2938
        assert(callback);
3,139✔
2939

2940
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
3,139✔
2941
        if (r < 0)
3,139✔
2942
                return r;
2943

2944
        r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
3,139✔
2945
                               link_netlink_destroy_callback, link);
2946
        if (r < 0)
3,139✔
2947
                return r;
2948

2949
        link_ref(link);
3,139✔
2950
        return 0;
2951
}
2952

2953
static const char* const link_state_table[_LINK_STATE_MAX] = {
2954
        [LINK_STATE_PENDING]     = "pending",
2955
        [LINK_STATE_INITIALIZED] = "initialized",
2956
        [LINK_STATE_CONFIGURING] = "configuring",
2957
        [LINK_STATE_CONFIGURED]  = "configured",
2958
        [LINK_STATE_UNMANAGED]   = "unmanaged",
2959
        [LINK_STATE_FAILED]      = "failed",
2960
        [LINK_STATE_LINGER]      = "linger",
2961
};
2962

2963
DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
29,395✔
2964

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

2989
        assert(ret);
449✔
2990

2991
        for (size_t i = 0; i < ELEMENTSOF(map); i++)
8,980✔
2992
                if (BIT_SET(flags, i) && map[i])
8,531✔
2993
                        if (!strextend_with_separator(&str, ",", map[i]))
1,289✔
2994
                                return -ENOMEM;
2995

2996
        *ret = TAKE_PTR(str);
449✔
2997
        return 0;
449✔
2998
}
2999

3000
static const char * const kernel_operstate_table[] = {
3001
        [IF_OPER_UNKNOWN]        = "unknown",
3002
        [IF_OPER_NOTPRESENT]     = "not-present",
3003
        [IF_OPER_DOWN]           = "down",
3004
        [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
3005
        [IF_OPER_TESTING]        = "testing",
3006
        [IF_OPER_DORMANT]        = "dormant",
3007
        [IF_OPER_UP]             = "up",
3008
};
3009

3010
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);
449✔
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