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

systemd / systemd / 23172097450

16 Mar 2026 07:45PM UTC coverage: 72.568% (+0.06%) from 72.513%
23172097450

push

github

keszybz
add-ug-bo-translation

316055 of 435531 relevant lines covered (72.57%)

1182407.5 hits per line

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

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

3
#include <linux/if.h>
4
#include <linux/if_arp.h>
5
#include <linux/if_link.h>
6
#include <linux/netdevice.h>
7
#include <net/if.h>
8
#include <sys/socket.h>
9
#include <unistd.h>
10

11
#include "sd-bus.h"
12
#include "sd-dhcp-client.h"
13
#include "sd-dhcp-server.h"
14
#include "sd-dhcp6-client.h"
15
#include "sd-dhcp6-lease.h"
16
#include "sd-ipv4ll.h"
17
#include "sd-lldp-rx.h"
18
#include "sd-ndisc.h"
19
#include "sd-netlink.h"
20
#include "sd-radv.h"
21

22
#include "alloc-util.h"
23
#include "arphrd-util.h"
24
#include "bitfield.h"
25
#include "device-util.h"
26
#include "dns-domain.h"
27
#include "errno-util.h"
28
#include "ethtool-util.h"
29
#include "event-util.h"
30
#include "format-ifname.h"
31
#include "fs-util.h"
32
#include "glyph-util.h"
33
#include "logarithm.h"
34
#include "netif-util.h"
35
#include "netlink-util.h"
36
#include "networkd-address.h"
37
#include "networkd-address-label.h"
38
#include "networkd-bridge-fdb.h"
39
#include "networkd-bridge-mdb.h"
40
#include "networkd-bridge-vlan.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.h"
49
#include "networkd-link-bus.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-resolve-hook.h"
58
#include "networkd-route.h"
59
#include "networkd-route-util.h"
60
#include "networkd-routing-policy-rule.h"
61
#include "networkd-setlink.h"
62
#include "networkd-sriov.h"
63
#include "networkd-state-file.h"
64
#include "networkd-sysctl.h"
65
#include "networkd-wifi.h"
66
#include "networkd-wwan-bus.h"
67
#include "ordered-set.h"
68
#include "parse-util.h"
69
#include "set.h"
70
#include "socket-util.h"
71
#include "string-table.h"
72
#include "string-util.h"
73
#include "strv.h"
74
#include "tc.h"
75
#include "udev-util.h"
76

77
void link_required_operstate_for_online(Link *link, LinkOperationalStateRange *ret) {
18,870✔
78
        assert(link);
18,870✔
79
        assert(ret);
18,870✔
80

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

106
AddressFamily link_required_family_for_online(Link *link) {
9,835✔
107
        assert(link);
9,835✔
108

109
        if (link->network && link->network->required_family_for_online >= 0)
9,835✔
110
                return link->network->required_family_for_online;
111

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

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

121
        return ADDRESS_FAMILY_NO;
122
}
123

124
bool link_ipv6_enabled(Link *link) {
925✔
125
        assert(link);
925✔
126

127
        if (!socket_ipv6_is_supported())
925✔
128
                return false;
129

130
        if (link->iftype == ARPHRD_CAN)
925✔
131
                return false;
132

133
        if (!link->network)
923✔
134
                return false;
135

136
        if (link->network->bond)
923✔
137
                return false;
138

139
        if (link_ipv6ll_enabled(link))
912✔
140
                return true;
141

142
        if (network_has_static_ipv6_configurations(link->network))
123✔
143
                return true;
44✔
144

145
        return false;
146
}
147

148
bool link_has_ipv6_connectivity(Link *link) {
308✔
149
        LinkAddressState ipv6_address_state;
308✔
150

151
        assert(link);
308✔
152

153
        link_get_address_states(link, NULL, &ipv6_address_state, NULL);
308✔
154

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

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

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

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

174
static bool link_is_ready_to_configure_one(Link *link, bool allow_unmanaged) {
68,946✔
175
        assert(link);
68,946✔
176

177
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
68,946✔
178
                return false;
179

180
        if (!link->network)
68,903✔
181
                return allow_unmanaged;
182

183
        if (!link->network->configure_without_carrier) {
68,902✔
184
                if (link->set_flags_messages > 0)
68,608✔
185
                        return false;
186

187
                if (!link_has_carrier(link))
18,962✔
188
                        return false;
189
        }
190

191
        if (link->set_link_messages > 0)
13,514✔
192
                return false;
193

194
        if (!link->activated)
13,381✔
195
                return false;
60✔
196

197
        return true;
198
}
199

200
bool link_is_ready_to_configure(Link *link, bool allow_unmanaged) {
68,946✔
201
        return check_ready_for_all_sr_iov_ports(link, allow_unmanaged, link_is_ready_to_configure_one);
68,946✔
202
}
203

204
bool link_is_ready_to_configure_by_name(Manager *manager, const char *name, bool allow_unmanaged) {
2,023✔
205
        assert(manager);
2,023✔
206
        assert(name);
2,023✔
207

208
        Link *link;
2,023✔
209
        if (link_get_by_name(manager, name, &link) < 0)
2,023✔
210
                return false;
2,023✔
211

212
        return link_is_ready_to_configure(link, allow_unmanaged);
2,023✔
213
}
214

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

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

226
        link->search_domains = ordered_set_free(link->search_domains);
2,855✔
227
        link->route_domains = ordered_set_free(link->route_domains);
2,855✔
228

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

235
        link->dnssec_negative_trust_anchors = set_free(link->dnssec_negative_trust_anchors);
2,855✔
236
}
2,855✔
237

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

242
        link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
4,873✔
243

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

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

251
        link->ipv4acd_by_address = hashmap_free(link->ipv4acd_by_address);
4,873✔
252

253
        link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
4,873✔
254

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

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

262
        link->radv = sd_radv_unref(link->radv);
4,873✔
263
}
264

265
static Link* link_free(Link *link) {
2,838✔
266
        assert(link);
2,838✔
267

268
        (void) link_clear_sysctl_shadows(link);
2,838✔
269

270
        link_ntp_settings_clear(link);
2,838✔
271
        link_dns_settings_clear(link);
2,838✔
272

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

278
        link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
2,838✔
279

280
        link_free_engines(link);
2,838✔
281

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

290
        unlink_and_free(link->lease_file);
2,838✔
291
        unlink_and_free(link->state_file);
2,838✔
292

293
        sd_device_unref(link->dev);
2,838✔
294
        netdev_unref(link->netdev);
2,838✔
295

296
        hashmap_free(link->bound_to_links);
2,838✔
297
        hashmap_free(link->bound_by_links);
2,838✔
298

299
        set_free(link->slaves);
2,838✔
300

301
        network_unref(link->network);
2,838✔
302

303
        sd_event_source_disable_unref(link->carrier_lost_timer);
2,838✔
304
        sd_event_source_disable_unref(link->ipv6_mtu_wait_synced_event_source);
2,838✔
305

306
        return mfree(link);
2,838✔
307
}
308

309
DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
196,435✔
310

311
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
1,969✔
312
                link_hash_ops,
313
                void, trivial_hash_func, trivial_compare_func,
314
                Link, link_unref);
315

316
int link_get_by_index(Manager *m, int ifindex, Link **ret) {
71,065✔
317
        Link *link;
71,065✔
318

319
        assert(m);
71,065✔
320

321
        if (ifindex <= 0)
71,065✔
322
                return -EINVAL;
323

324
        link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
70,335✔
325
        if (!link)
70,335✔
326
                return -ENODEV;
327

328
        if (ret)
66,523✔
329
                *ret = link;
66,523✔
330
        return 0;
331
}
332

333
int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
2,470✔
334
        Link *link;
2,470✔
335

336
        assert(m);
2,470✔
337
        assert(ifname);
2,470✔
338

339
        link = hashmap_get(m->links_by_name, ifname);
2,470✔
340
        if (!link)
2,470✔
341
                return -ENODEV;
342

343
        if (ret)
2,392✔
344
                *ret = link;
2,392✔
345
        return 0;
346
}
347

348
int link_get_by_hw_addr(Manager *m, const struct hw_addr_data *hw_addr, Link **ret) {
×
349
        Link *link;
×
350

351
        assert(m);
×
352
        assert(hw_addr);
×
353

354
        link = hashmap_get(m->links_by_hw_addr, hw_addr);
×
355
        if (!link)
×
356
                return -ENODEV;
357

358
        if (ret)
×
359
                *ret = link;
×
360
        return 0;
361
}
362

363
int link_get_master(Link *link, Link **ret) {
24,885✔
364
        assert(link);
24,885✔
365
        assert(link->manager);
24,885✔
366
        assert(ret);
24,885✔
367

368
        if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex)
24,885✔
369
                return -ENODEV;
370

371
        return link_get_by_index(link->manager, link->master_ifindex, ret);
705✔
372
}
373

374
void link_set_state(Link *link, LinkState state) {
9,425✔
375
        assert(link);
9,425✔
376

377
        if (link->state == state)
9,425✔
378
                return;
1,830✔
379

380
        log_link_debug(link, "State changed: %s -> %s",
7,595✔
381
                       link_state_to_string(link->state),
382
                       link_state_to_string(state));
383

384
        link->state = state;
7,595✔
385

386
        link_send_changed(link, "AdministrativeState");
7,595✔
387
        link_dirty(link);
7,595✔
388
}
389

390
int link_stop_engines(Link *link, bool may_keep_dynamic) {
4,679✔
391
        int r, ret = 0;
4,679✔
392

393
        assert(link);
4,679✔
394
        assert(link->manager);
4,679✔
395
        assert(link->manager->event);
4,679✔
396

397
        bool keep_dynamic =
13,992✔
398
                may_keep_dynamic &&
1,962✔
399
                link->network &&
4,679✔
400
                (link->manager->state == MANAGER_RESTARTING ||
58✔
401
                 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC_ON_STOP));
15✔
402

403
        if (!keep_dynamic) {
4,634✔
404
                r = sd_dhcp_client_stop(link->dhcp_client);
4,634✔
405
                if (r < 0)
4,634✔
406
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 client: %m"));
×
407

408
                r = sd_ipv4ll_stop(link->ipv4ll);
4,634✔
409
                if (r < 0)
4,634✔
410
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 link-local: %m"));
×
411

412
                r = sd_dhcp6_client_stop(link->dhcp6_client);
4,634✔
413
                if (r < 0)
4,634✔
414
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"));
×
415

416
                r = dhcp_pd_remove(link, /* only_marked= */ false);
4,634✔
417
                if (r < 0)
4,634✔
418
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not remove DHCPv6 PD addresses and routes: %m"));
×
419

420
                r = ndisc_stop(link);
4,634✔
421
                if (r < 0)
4,634✔
422
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Discovery: %m"));
×
423

424
                ndisc_flush(link);
4,634✔
425
        }
426

427
        r = sd_dhcp_server_stop(link->dhcp_server);
4,679✔
428
        if (r < 0)
4,679✔
429
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 server: %m"));
×
430

431
        r = sd_lldp_rx_stop(link->lldp_rx);
4,679✔
432
        if (r < 0)
4,679✔
433
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Rx: %m"));
×
434

435
        r = sd_lldp_tx_stop(link->lldp_tx);
4,679✔
436
        if (r < 0)
4,679✔
437
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Tx: %m"));
×
438

439
        r = ipv4acd_stop(link);
4,679✔
440
        if (r < 0)
4,679✔
441
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 ACD client: %m"));
×
442

443
        r = sd_radv_stop(link->radv);
4,679✔
444
        if (r < 0)
4,679✔
445
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Advertisement: %m"));
×
446

447
        return ret;
4,679✔
448
}
449

450
void link_enter_failed(Link *link) {
5✔
451
        assert(link);
5✔
452

453
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
5✔
454
                return;
455

456
        log_link_warning(link, "Failed");
5✔
457

458
        link_set_state(link, LINK_STATE_FAILED);
5✔
459

460
        if (!ratelimit_below(&link->automatic_reconfigure_ratelimit)) {
5✔
461
                log_link_warning(link, "The interface entered the failed state frequently, refusing to reconfigure it automatically.");
×
462
                goto stop;
×
463
        }
464

465
        log_link_info(link, "Trying to reconfigure the interface.");
5✔
466
        if (link_reconfigure(link, LINK_RECONFIGURE_UNCONDITIONALLY) > 0)
5✔
467
                return;
468

469
stop:
×
470
        (void) link_stop_engines(link, /* may_keep_dynamic= */ false);
×
471
}
472

473
void link_check_ready(Link *link) {
27,409✔
474
        Address *a;
27,409✔
475

476
        assert(link);
27,409✔
477

478
        if (link->state == LINK_STATE_CONFIGURED)
27,409✔
479
                return;
26,514✔
480

481
        if (link->state != LINK_STATE_CONFIGURING)
23,357✔
482
                return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
1,352✔
483

484
        if (!link->network)
22,005✔
485
                return (void) log_link_debug(link, "%s(): link is unmanaged.", __func__);
×
486

487
        if (!link->tc_configured)
22,005✔
488
                return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
341✔
489

490
        if (link->set_link_messages > 0)
21,664✔
491
                return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
2,684✔
492

493
        if (!link->activated)
18,980✔
494
                return (void) log_link_debug(link, "%s(): link is not activated.", __func__);
11,712✔
495

496
        if (link->iftype == ARPHRD_CAN)
7,268✔
497
                /* let's shortcut things for CAN which doesn't need most of checks below. */
498
                goto ready;
6✔
499

500
        if (!link->stacked_netdevs_created)
7,262✔
501
                return (void) log_link_debug(link, "%s(): stacked netdevs are not created.", __func__);
242✔
502

503
        if (!link->static_addresses_configured)
7,020✔
504
                return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
3,459✔
505

506
        if (!link->static_address_labels_configured)
3,561✔
507
                return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__);
3✔
508

509
        if (!link->static_bridge_fdb_configured)
3,558✔
510
                return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
4✔
511

512
        if (!link->static_bridge_mdb_configured)
3,554✔
513
                return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
12✔
514

515
        if (!link->static_ipv6_proxy_ndp_configured)
3,542✔
516
                return (void) log_link_debug(link, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__);
3✔
517

518
        if (!link->static_neighbors_configured)
3,539✔
519
                return (void) log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
7✔
520

521
        if (!link->static_nexthops_configured)
3,532✔
522
                return (void) log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
20✔
523

524
        if (!link->static_routes_configured)
3,512✔
525
                return (void) log_link_debug(link, "%s(): static routes are not configured.", __func__);
157✔
526

527
        if (!link->static_routing_policy_rules_configured)
3,355✔
528
                return (void) log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
48✔
529

530
        if (!link->sr_iov_configured)
3,307✔
531
                return (void) log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
×
532

533
        if (!link->bearer_configured)
3,307✔
534
                return (void) log_link_debug(link, "%s(): Bearer has not been applied.", __func__);
×
535

536
        /* IPv6LL is assigned after the link gains its carrier. */
537
        if (!link->network->configure_without_carrier &&
6,600✔
538
            link_ipv6ll_enabled(link) &&
3,293✔
539
            !in6_addr_is_set(&link->ipv6ll_address))
3,162✔
540
                return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__);
1,238✔
541

542
        /* All static addresses must be ready. */
543
        bool has_static_address = false;
2,069✔
544
        SET_FOREACH(a, link->addresses) {
7,079✔
545
                if (!IN_SET(a->source, NETWORK_CONFIG_SOURCE_STATIC, NETWORK_CONFIG_SOURCE_MODEM_MANAGER))
5,162✔
546
                        continue;
3,261✔
547
                if (!address_is_ready(a))
1,901✔
548
                        return (void) log_link_debug(link, "%s(): static address %s is not ready.", __func__,
152✔
549
                                                     IN_ADDR_PREFIX_TO_STRING(a->family, &a->in_addr, a->prefixlen));
550
                has_static_address = true;
551
        }
552

553
        /* If at least one static address is requested, do not request that dynamic addressing protocols are finished. */
554
        if (has_static_address)
1,917✔
555
                goto ready;
398✔
556

557
        /* If no dynamic addressing protocol enabled, assume the interface is ready.
558
         * Note, ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
559
        if (!link_ipv4ll_enabled(link) && !link_dhcp4_enabled(link) &&
2,908✔
560
            !link_dhcp6_enabled(link) && !link_dhcp_pd_is_enabled(link) &&
806✔
561
            (link->network->configure_without_carrier || !link_ndisc_enabled(link)))
597✔
562
                goto ready;
260✔
563

564
        bool ipv4ll_ready =
2,518✔
565
                link_ipv4ll_enabled(link) && link->ipv4ll_address_configured &&
1,313✔
566
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_IPV4LL);
54✔
567
        bool dhcp4_ready =
1,259✔
568
                link_dhcp4_enabled(link) && link->dhcp4_configured &&
1,357✔
569
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP4);
98✔
570
        bool dhcp6_ready =
1,259✔
571
                link_dhcp6_enabled(link) && link->dhcp6_configured &&
1,259✔
572
                (!link->network->dhcp6_use_address ||
50✔
573
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP6));
24✔
574
        bool dhcp_pd_ready =
2,518✔
575
                link_dhcp_pd_is_enabled(link) && link->dhcp_pd_configured &&
1,259✔
576
                (!link->network->dhcp_pd_assign ||
139✔
577
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP_PD));
66✔
578
        bool ndisc_ready =
2,518✔
579
                link_ndisc_enabled(link) && link->ndisc_configured &&
1,259✔
580
                (!link->network->ndisc_use_autonomous_prefix ||
438✔
581
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_NDISC));
219✔
582

583
        /* If the uplink for PD is self, then request the corresponding DHCP protocol is also ready. */
584
        if (dhcp_pd_is_uplink(link, link, /* accept_auto= */ false)) {
1,259✔
585
                if (link_dhcp4_enabled(link) && link->network->dhcp_use_6rd &&
78✔
586
                    sd_dhcp_lease_has_6rd(link->dhcp_lease)) {
25✔
587
                        if (!link->dhcp4_configured)
22✔
588
                                return (void) log_link_debug(link, "%s(): DHCPv4 6rd prefix is assigned, but DHCPv4 protocol is not finished yet.", __func__);
10✔
589
                        if (!dhcp_pd_ready)
12✔
590
                                return (void) log_link_debug(link, "%s(): DHCPv4 is finished, but prefix acquired by DHCPv4-6rd is not assigned yet.", __func__);
10✔
591
                }
592

593
                if (link_dhcp6_enabled(link) && link->network->dhcp6_use_pd_prefix &&
61✔
594
                    sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease)) {
28✔
595
                        if (!link->dhcp6_configured)
19✔
596
                                return (void) log_link_debug(link, "%s(): DHCPv6 IA_PD prefix is assigned, but DHCPv6 protocol is not finished yet.", __func__);
10✔
597
                        if (!dhcp_pd_ready)
9✔
598
                                return (void) log_link_debug(link, "%s(): DHCPv6 is finished, but prefix acquired by DHCPv6 IA_PD is not assigned yet.", __func__);
6✔
599
                }
600
        }
601

602
        /* At least one dynamic addressing protocol is finished. */
603
        if (!ipv4ll_ready && !dhcp4_ready && !dhcp6_ready && !dhcp_pd_ready && !ndisc_ready)
1,223✔
604
                return (void) log_link_debug(link, "%s(): dynamic addressing protocols are enabled but none of them finished yet.", __func__);
992✔
605

606
        log_link_debug(link, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
1,196✔
607
                       __func__,
608
                       yes_no(ipv4ll_ready),
609
                       yes_no(dhcp4_ready),
610
                       yes_no(dhcp6_ready),
611
                       yes_no(dhcp_pd_ready),
612
                       yes_no(ndisc_ready));
613

614
ready:
895✔
615
        link_set_state(link, LINK_STATE_CONFIGURED);
895✔
616
}
617

618
static int link_request_static_configs(Link *link) {
1,565✔
619
        int r;
1,565✔
620

621
        assert(link);
1,565✔
622
        assert(link->network);
1,565✔
623
        assert(link->state != _LINK_STATE_INVALID);
1,565✔
624

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

629
        r = link_request_static_address_labels(link);
1,565✔
630
        if (r < 0)
1,565✔
631
                return r;
632

633
        r = link_request_static_bridge_fdb(link);
1,565✔
634
        if (r < 0)
1,565✔
635
                return r;
636

637
        r = link_request_static_bridge_mdb(link);
1,565✔
638
        if (r < 0)
1,565✔
639
                return r;
640

641
        r = link_request_static_ipv6_proxy_ndp_addresses(link);
1,565✔
642
        if (r < 0)
1,565✔
643
                return r;
644

645
        r = link_request_static_neighbors(link);
1,565✔
646
        if (r < 0)
1,565✔
647
                return r;
648

649
        r = link_request_static_nexthops(link, false);
1,565✔
650
        if (r < 0)
1,565✔
651
                return r;
652

653
        r = link_request_static_routes(link, false);
1,565✔
654
        if (r < 0)
1,565✔
655
                return r;
656

657
        r = link_request_static_routing_policy_rules(link);
1,565✔
658
        if (r < 0)
1,565✔
659
                return r;
×
660

661
        return 0;
662
}
663

664
int link_request_stacked_netdevs(Link *link, NetDevLocalAddressType type) {
11,965✔
665
        NetDev *netdev;
11,965✔
666
        int r;
11,965✔
667

668
        assert(link);
11,965✔
669

670
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
11,965✔
671
                return 0;
11,965✔
672

673
        assert(link->network);
2,675✔
674

675
        link->stacked_netdevs_created = false;
2,675✔
676

677
        HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
3,187✔
678
                if (!netdev_needs_reconfigure(netdev, type))
512✔
679
                        continue;
296✔
680

681
                r = link_request_stacked_netdev(link, netdev);
216✔
682
                if (r < 0)
216✔
683
                        return r;
×
684
        }
685

686
        if (link->create_stacked_netdev_messages == 0) {
2,675✔
687
                link->stacked_netdevs_created = true;
2,499✔
688
                link_check_ready(link);
2,499✔
689
        }
690

691
        return 0;
692
}
693

694
static int link_acquire_dynamic_ipv6_conf(Link *link) {
801✔
695
        int r;
801✔
696

697
        assert(link);
801✔
698

699
        r = radv_start(link);
801✔
700
        if (r < 0)
801✔
701
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
×
702

703
        r = ndisc_start(link);
801✔
704
        if (r < 0)
801✔
705
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
×
706

707
        r = dhcp6_start(link);
801✔
708
        if (r < 0)
801✔
709
                return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m");
×
710

711
        return 0;
712
}
713

714
static int link_acquire_dynamic_ipv4_conf(Link *link) {
959✔
715
        int r;
959✔
716

717
        assert(link);
959✔
718
        assert(link->manager);
959✔
719
        assert(link->manager->event);
959✔
720

721
        if (link->dhcp_client) {
959✔
722
                r = dhcp4_start(link);
×
723
                if (r < 0)
×
724
                        return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
×
725

726
                log_link_debug(link, "Acquiring DHCPv4 lease.");
×
727

728
        } else if (link->ipv4ll) {
959✔
729
                if (in4_addr_is_set(&link->network->ipv4ll_start_address)) {
72✔
730
                        r = sd_ipv4ll_set_address(link->ipv4ll, &link->network->ipv4ll_start_address);
1✔
731
                        if (r < 0)
1✔
732
                                return log_link_warning_errno(link, r, "Could not set IPv4 link-local start address: %m");
×
733
                }
734

735
                r = ipv4ll_start(link);
72✔
736
                if (r < 0)
72✔
737
                        return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
×
738
                if (r > 0)
72✔
739
                        log_link_debug(link, "Acquiring IPv4 link-local address.");
71✔
740
        }
741

742
        r = link_start_dhcp4_server(link);
959✔
743
        if (r < 0)
959✔
744
                return log_link_warning_errno(link, r, "Could not start DHCP server: %m");
×
745

746
        r = ipv4acd_start(link);
959✔
747
        if (r < 0)
959✔
748
                return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m");
×
749

750
        return 0;
751
}
752

753
static int link_acquire_dynamic_conf(Link *link) {
959✔
754
        int r;
959✔
755

756
        assert(link);
959✔
757
        assert(link->network);
959✔
758

759
        r = link_acquire_dynamic_ipv4_conf(link);
959✔
760
        if (r < 0)
959✔
761
                return r;
762

763
        if (in6_addr_is_set(&link->ipv6ll_address)) {
959✔
764
                r = link_acquire_dynamic_ipv6_conf(link);
279✔
765
                if (r < 0)
279✔
766
                        return r;
767
        }
768

769
        if (!link_radv_enabled(link) || !link->network->dhcp_pd_announce) {
959✔
770
                /* DHCPv6PD downstream does not require IPv6LL address. But may require RADV to be
771
                 * configured, and RADV may not be configured yet here. Only acquire subnet prefix when
772
                 * RADV is disabled, or the announcement of the prefix is disabled. Otherwise, the
773
                 * below will be called in radv_start(). */
774
                r = dhcp_request_prefix_delegation(link);
884✔
775
                if (r < 0)
884✔
776
                        return log_link_warning_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
×
777
        }
778

779
        if (link->lldp_tx) {
959✔
780
                r = sd_lldp_tx_start(link->lldp_tx);
7✔
781
                if (r < 0)
7✔
782
                        return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
×
783
        }
784

785
        if (link->lldp_rx) {
959✔
786
                r = sd_lldp_rx_start(link->lldp_rx);
842✔
787
                if (r < 0)
842✔
788
                        return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
×
789
        }
790

791
        return 0;
792
}
793

794
int link_ipv6ll_gained(Link *link) {
642✔
795
        int r;
642✔
796

797
        assert(link);
642✔
798

799
        log_link_info(link, "Gained IPv6LL");
642✔
800

801
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
642✔
802
                return 0;
803

804
        r = link_acquire_dynamic_ipv6_conf(link);
522✔
805
        if (r < 0)
522✔
806
                return r;
807

808
        r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_IPV6LL);
522✔
809
        if (r < 0)
522✔
810
                return r;
811

812
        link_check_ready(link);
522✔
813
        return 0;
522✔
814
}
815

816
int link_handle_bound_to_list(Link *link) {
42✔
817
        bool required_up = false;
42✔
818
        Link *l;
42✔
819

820
        assert(link);
42✔
821

822
        /* If at least one interface in bound_to_links has carrier, then make this interface up.
823
         * If all interfaces in bound_to_links do not, then make this interface down. */
824

825
        if (hashmap_isempty(link->bound_to_links))
42✔
826
                return 0;
42✔
827

828
        HASHMAP_FOREACH(l, link->bound_to_links)
51✔
829
                if (link_has_carrier(l)) {
37✔
830
                        required_up = true;
831
                        break;
832
                }
833

834
        if (!required_up && link_is_up(link))
35✔
835
                return link_request_to_bring_up_or_down(link, /* up= */ false);
7✔
836
        if (required_up && !link_is_up(link))
21✔
837
                return link_request_to_bring_up_or_down(link, /* up= */ true);
10✔
838

839
        return 0;
840
}
841

842
static int link_handle_bound_by_list(Link *link) {
4,680✔
843
        Link *l;
4,680✔
844
        int r;
4,680✔
845

846
        assert(link);
4,680✔
847

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

850
        HASHMAP_FOREACH(l, link->bound_by_links) {
4,707✔
851
                r = link_handle_bound_to_list(l);
27✔
852
                if (r < 0)
27✔
853
                        return r;
×
854
        }
855

856
        return 0;
4,680✔
857
}
858

859
static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
30✔
860
        int r;
30✔
861

862
        assert(link);
30✔
863
        assert(carrier);
30✔
864

865
        if (link == carrier)
30✔
866
                return 0;
867

868
        if (hashmap_contains(*h, INT_TO_PTR(carrier->ifindex)))
30✔
869
                return 0;
870

871
        r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
26✔
872
        if (r < 0)
26✔
873
                return r;
874

875
        link_dirty(link);
26✔
876

877
        return 0;
26✔
878
}
879

880
static int link_new_bound_by_list(Link *link) {
2,663✔
881
        Manager *m;
2,663✔
882
        Link *carrier;
2,663✔
883
        int r;
2,663✔
884

885
        assert(link);
2,663✔
886
        assert(link->manager);
2,663✔
887

888
        m = link->manager;
2,663✔
889

890
        HASHMAP_FOREACH(carrier, m->links_by_index) {
18,679✔
891
                if (!carrier->network)
16,016✔
892
                        continue;
15,148✔
893

894
                if (strv_isempty(carrier->network->bind_carrier))
868✔
895
                        continue;
858✔
896

897
                if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
10✔
898
                        r = link_put_carrier(link, carrier, &link->bound_by_links);
7✔
899
                        if (r < 0)
7✔
900
                                return r;
×
901
                }
902
        }
903

904
        HASHMAP_FOREACH(carrier, link->bound_by_links) {
2,670✔
905
                r = link_put_carrier(carrier, link, &carrier->bound_to_links);
7✔
906
                if (r < 0)
7✔
907
                        return r;
×
908
        }
909

910
        return 0;
2,663✔
911
}
912

913
static int link_new_bound_to_list(Link *link) {
876✔
914
        Manager *m;
876✔
915
        Link *carrier;
876✔
916
        int r;
876✔
917

918
        assert(link);
876✔
919
        assert(link->manager);
876✔
920

921
        if (!link->network)
876✔
922
                return 0;
876✔
923

924
        if (strv_isempty(link->network->bind_carrier))
876✔
925
                return 0;
926

927
        m = link->manager;
8✔
928

929
        HASHMAP_FOREACH(carrier, m->links_by_index) {
70✔
930
                if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
54✔
931
                        r = link_put_carrier(link, carrier, &link->bound_to_links);
8✔
932
                        if (r < 0)
8✔
933
                                return r;
×
934
                }
935
        }
936

937
        HASHMAP_FOREACH(carrier, link->bound_to_links) {
16✔
938
                r = link_put_carrier(carrier, link, &carrier->bound_by_links);
8✔
939
                if (r < 0)
8✔
940
                        return r;
×
941
        }
942

943
        return 0;
8✔
944
}
945

946
static void link_free_bound_to_list(Link *link) {
3,748✔
947
        bool updated = false;
3,748✔
948
        Link *bound_to;
3,748✔
949

950
        assert(link);
3,748✔
951

952
        while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
3,752✔
953
                updated = true;
4✔
954

955
                if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
4✔
956
                        link_dirty(bound_to);
4✔
957
        }
958

959
        if (updated)
3,748✔
960
                link_dirty(link);
3✔
961
}
3,748✔
962

963
static void link_free_bound_by_list(Link *link) {
875✔
964
        bool updated = false;
875✔
965
        Link *bound_by;
875✔
966

967
        assert(link);
875✔
968

969
        while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
882✔
970
                updated = true;
7✔
971

972
                if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
7✔
973
                        link_dirty(bound_by);
7✔
974
                        link_handle_bound_to_list(bound_by);
7✔
975
                }
976
        }
977

978
        if (updated)
875✔
979
                link_dirty(link);
7✔
980
}
875✔
981

982
static int link_append_to_master(Link *link) {
10,725✔
983
        Link *master;
10,725✔
984
        int r;
10,725✔
985

986
        assert(link);
10,725✔
987

988
        /* - The link may have no master.
989
         * - RTM_NEWLINK message about master interface may not be received yet. */
990
        if (link_get_master(link, &master) < 0)
10,725✔
991
                return 0;
10,725✔
992

993
        r = set_ensure_put(&master->slaves, &link_hash_ops, link);
500✔
994
        if (r <= 0)
500✔
995
                return r;
996

997
        link_ref(link);
46✔
998
        return 0;
999
}
1000

1001
static void link_drop_from_master(Link *link) {
935✔
1002
        Link *master;
935✔
1003

1004
        assert(link);
935✔
1005

1006
        if (!link->manager)
935✔
1007
                return;
892✔
1008

1009
        if (link_get_master(link, &master) < 0)
935✔
1010
                return;
1011

1012
        link_unref(set_remove(master->slaves, link));
43✔
1013
}
1014

1015
static int link_drop_requests(Link *link) {
3,748✔
1016
        Request *req;
3,748✔
1017
        int ret = 0;
3,748✔
1018

1019
        assert(link);
3,748✔
1020
        assert(link->manager);
3,748✔
1021

1022
        ORDERED_SET_FOREACH(req, link->manager->request_queue) {
6,136✔
1023
                if (req->link != link)
2,388✔
1024
                        continue;
2,280✔
1025

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

1034
                                if (address_get(link, address, NULL) < 0)
×
1035
                                        RET_GATHER(ret, address_remove(address, link));
×
1036
                                break;
1037
                        }
1038
                        case REQUEST_TYPE_NEIGHBOR: {
×
1039
                                Neighbor *neighbor = ASSERT_PTR(req->userdata);
×
1040

1041
                                if (neighbor_get(link, neighbor, NULL) < 0)
×
1042
                                        RET_GATHER(ret, neighbor_remove(neighbor, link));
×
1043
                                break;
1044
                        }
1045
                        case REQUEST_TYPE_NEXTHOP: {
×
1046
                                NextHop *nexthop = ASSERT_PTR(req->userdata);
×
1047

1048
                                if (nexthop_get_by_id(link->manager, nexthop->id, NULL) < 0)
×
1049
                                        RET_GATHER(ret, nexthop_remove(nexthop, link->manager));
×
1050
                                break;
1051
                        }
1052
                        case REQUEST_TYPE_ROUTE: {
×
1053
                                Route *route = ASSERT_PTR(req->userdata);
×
1054

1055
                                if (route_get(link->manager, route, NULL) < 0)
×
1056
                                        RET_GATHER(ret, route_remove(route, link->manager));
×
1057
                                break;
1058
                        }
1059
                        default:
108✔
1060
                                ;
108✔
1061
                        }
1062

1063
                request_detach(req);
108✔
1064
        }
1065

1066
        return ret;
3,748✔
1067
}
1068

1069
static Link *link_drop(Link *link) {
890✔
1070
        if (!link)
890✔
1071
                return NULL;
1072

1073
        assert(link->manager);
875✔
1074

1075
        bool notify = link_has_local_lease_domain(link);
875✔
1076

1077
        link_set_state(link, LINK_STATE_LINGER);
875✔
1078

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

1082
        (void) link_drop_requests(link);
875✔
1083

1084
        link_free_bound_to_list(link);
875✔
1085
        link_free_bound_by_list(link);
875✔
1086

1087
        link_clear_sr_iov_ifindices(link);
875✔
1088

1089
        link_drop_from_master(link);
875✔
1090

1091
        if (link->state_file)
875✔
1092
                (void) unlink(link->state_file);
875✔
1093

1094
        link_clean(link);
875✔
1095

1096
        STRV_FOREACH(n, link->alternative_names)
910✔
1097
                hashmap_remove(link->manager->links_by_name, *n);
35✔
1098
        hashmap_remove(link->manager->links_by_name, link->ifname);
875✔
1099

1100
        /* bonding master and its slaves have the same hardware address. */
1101
        hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
875✔
1102

1103
        /* The following must be called at last. */
1104
        assert_se(hashmap_remove(link->manager->links_by_index, INT_TO_PTR(link->ifindex)) == link);
875✔
1105

1106
        if (notify)
875✔
1107
                manager_notify_hook_filters(link->manager);
3✔
1108

1109
        return link_unref(link);
875✔
1110
}
1111

1112
static int link_drop_unmanaged_config(Link *link) {
876✔
1113
        int r;
876✔
1114

1115
        assert(link);
876✔
1116
        assert(link->state == LINK_STATE_CONFIGURING);
876✔
1117
        assert(link->manager);
876✔
1118

1119
        r = link_drop_unmanaged_routes(link);
876✔
1120
        RET_GATHER(r, link_drop_unmanaged_nexthops(link));
876✔
1121
        RET_GATHER(r, link_drop_unmanaged_addresses(link));
876✔
1122
        RET_GATHER(r, link_drop_unmanaged_neighbors(link));
876✔
1123
        RET_GATHER(r, link_drop_unmanaged_routing_policy_rules(link));
876✔
1124

1125
        return r;
876✔
1126
}
1127

1128
static int link_drop_static_config(Link *link) {
2,676✔
1129
        int r;
2,676✔
1130

1131
        assert(link);
2,676✔
1132
        assert(link->manager);
2,676✔
1133

1134
        r = link_drop_static_routes(link);
2,676✔
1135
        RET_GATHER(r, link_drop_static_nexthops(link));
2,676✔
1136
        RET_GATHER(r, link_drop_static_addresses(link));
2,676✔
1137
        RET_GATHER(r, link_drop_static_neighbors(link));
2,676✔
1138
        RET_GATHER(r, link_drop_static_routing_policy_rules(link));
2,676✔
1139

1140
        return r;
2,676✔
1141
}
1142

1143
static int link_drop_dynamic_config(Link *link, Network *network) {
838✔
1144
        int r;
838✔
1145

1146
        assert(link);
838✔
1147
        assert(link->network);
838✔
1148

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

1152
        r = link_drop_ndisc_config(link, network);
838✔
1153
        RET_GATHER(r, link_drop_radv_config(link, network)); /* Stop before dropping DHCP-PD prefixes. */
838✔
1154
        RET_GATHER(r, link_drop_ipv4ll_config(link, network)); /* Stop before DHCPv4 client. */
838✔
1155
        RET_GATHER(r, link_drop_dhcp4_config(link, network));
838✔
1156
        RET_GATHER(r, link_drop_dhcp6_config(link, network));
838✔
1157
        RET_GATHER(r, link_drop_dhcp_pd_config(link, network));
838✔
1158
        link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
838✔
1159
        link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx); /* TODO: keep the received neighbors. */
838✔
1160
        link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
838✔
1161

1162
        /* Even if we do not release DHCP lease or so, reset 'configured' flags. Otherwise, e.g. if
1163
         * previously UseDNS= was disabled but is now enabled, link will enter configured state before
1164
         * expected DNS servers being acquired. */
1165
        link->ipv4ll_address_configured = false;
838✔
1166
        link->dhcp4_configured = false;
838✔
1167
        link->dhcp6_configured = false;
838✔
1168
        link->dhcp_pd_configured = false;
838✔
1169
        link->ndisc_configured = false;
838✔
1170

1171
        return r;
838✔
1172
}
1173

1174
static int link_configure(Link *link) {
876✔
1175
        int r;
876✔
1176

1177
        assert(link);
876✔
1178
        assert(link->network);
876✔
1179
        assert(link->state == LINK_STATE_INITIALIZED);
876✔
1180

1181
        link_set_state(link, LINK_STATE_CONFIGURING);
876✔
1182

1183
        r = link_drop_unmanaged_config(link);
876✔
1184
        if (r < 0)
876✔
1185
                return r;
1186

1187
        r = link_new_bound_to_list(link);
876✔
1188
        if (r < 0)
876✔
1189
                return r;
1190

1191
        r = link_request_traffic_control(link);
876✔
1192
        if (r < 0)
876✔
1193
                return r;
1194

1195
        r = link_configure_mtu(link);
876✔
1196
        if (r < 0)
876✔
1197
                return r;
1198

1199
        if (link->iftype == ARPHRD_CAN) {
876✔
1200
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1201
                r = link_request_to_set_can(link);
6✔
1202
                if (r < 0)
6✔
1203
                        return r;
1204

1205
                return link_request_to_activate(link);
6✔
1206
        }
1207

1208
        r = link_request_sr_iov_vfs(link);
870✔
1209
        if (r < 0)
870✔
1210
                return r;
1211

1212
        r = link_set_sysctl(link);
870✔
1213
        if (r < 0)
870✔
1214
                return r;
1215

1216
        r = link_request_to_set_mac(link, /* allow_retry= */ true);
870✔
1217
        if (r < 0)
870✔
1218
                return r;
1219

1220
        r = link_request_to_set_ipoib(link);
870✔
1221
        if (r < 0)
870✔
1222
                return r;
1223

1224
        r = link_request_to_set_flags(link);
870✔
1225
        if (r < 0)
870✔
1226
                return r;
1227

1228
        r = link_request_to_set_group(link);
870✔
1229
        if (r < 0)
870✔
1230
                return r;
1231

1232
        r = link_request_to_set_addrgen_mode(link);
870✔
1233
        if (r < 0)
870✔
1234
                return r;
1235

1236
        r = link_request_to_set_master(link);
870✔
1237
        if (r < 0)
870✔
1238
                return r;
1239

1240
        r = link_request_stacked_netdevs(link, _NETDEV_LOCAL_ADDRESS_TYPE_INVALID);
870✔
1241
        if (r < 0)
870✔
1242
                return r;
1243

1244
        r = link_request_to_set_bond(link);
870✔
1245
        if (r < 0)
870✔
1246
                return r;
1247

1248
        r = link_request_to_set_bridge(link);
870✔
1249
        if (r < 0)
870✔
1250
                return r;
1251

1252
        r = link_request_to_set_bridge_vlan(link);
870✔
1253
        if (r < 0)
870✔
1254
                return r;
1255

1256
        r = link_request_to_activate(link);
870✔
1257
        if (r < 0)
870✔
1258
                return r;
1259

1260
        r = ipv4ll_configure(link);
870✔
1261
        if (r < 0)
870✔
1262
                return r;
1263

1264
        r = link_request_dhcp4_client(link);
870✔
1265
        if (r < 0)
870✔
1266
                return r;
1267

1268
        r = link_request_dhcp6_client(link);
870✔
1269
        if (r < 0)
870✔
1270
                return r;
1271

1272
        r = link_request_ndisc(link);
870✔
1273
        if (r < 0)
870✔
1274
                return r;
1275

1276
        r = link_request_dhcp_server(link);
870✔
1277
        if (r < 0)
870✔
1278
                return r;
1279

1280
        r = link_request_radv(link);
870✔
1281
        if (r < 0)
870✔
1282
                return r;
1283

1284
        r = link_lldp_rx_configure(link);
870✔
1285
        if (r < 0)
870✔
1286
                return r;
1287

1288
        r = link_lldp_tx_configure(link);
870✔
1289
        if (r < 0)
870✔
1290
                return r;
1291

1292
        r = link_request_static_configs(link);
870✔
1293
        if (r < 0)
870✔
1294
                return r;
1295

1296
        r = link_modem_reconfigure(link);
870✔
1297
        if (r < 0)
870✔
1298
                return r;
1299

1300
        if (!link_has_carrier(link))
870✔
1301
                return 0;
1302

1303
        return link_acquire_dynamic_conf(link);
264✔
1304
}
1305

1306
static int link_get_network(Link *link, Network **ret) {
4,782✔
1307
        Network *network;
4,782✔
1308
        int r;
4,782✔
1309

1310
        assert(link);
4,782✔
1311
        assert(link->manager);
4,782✔
1312
        assert(ret);
4,782✔
1313

1314
        ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
19,674✔
1315
                bool warn = false;
18,604✔
1316

1317
                r = net_match_config(
37,208✔
1318
                                &network->match,
18,604✔
1319
                                link->dev,
1320
                                &link->hw_addr,
18,604✔
1321
                                &link->permanent_hw_addr,
18,604✔
1322
                                link->driver,
18,604✔
1323
                                link->iftype,
18,604✔
1324
                                link->kind,
18,604✔
1325
                                link->ifname,
18,604✔
1326
                                link->alternative_names,
18,604✔
1327
                                link->wlan_iftype,
1328
                                link->ssid,
18,604✔
1329
                                &link->bssid);
18,604✔
1330
                if (r < 0)
18,604✔
1331
                        return r;
3,712✔
1332
                if (r == 0)
18,604✔
1333
                        continue;
14,892✔
1334

1335
                if (network->match.ifname && link->dev) {
3,712✔
1336
                        uint8_t name_assign_type = NET_NAME_UNKNOWN;
3,685✔
1337
                        const char *attr;
3,685✔
1338

1339
                        if (sd_device_get_sysattr_value(link->dev, "name_assign_type", &attr) >= 0)
3,685✔
1340
                                (void) safe_atou8(attr, &name_assign_type);
2,152✔
1341

1342
                        warn = name_assign_type == NET_NAME_ENUM;
3,685✔
1343
                }
1344

1345
                log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
7,424✔
1346
                              "Found matching .network file%s: %s",
1347
                              warn ? ", based on potentially unpredictable interface name" : "",
1348
                              network->filename);
1349

1350
                if (network->unmanaged)
3,712✔
1351
                        return -ENOENT;
1352

1353
                *ret = network;
1,827✔
1354
                return 0;
1,827✔
1355
        }
1356

1357
        return log_link_debug_errno(link, SYNTHETIC_ERRNO(ENOENT), "No matching .network found.");
1,070✔
1358
}
1359

1360
static void link_enter_unmanaged(Link *link) {
2,955✔
1361
        assert(link);
2,955✔
1362

1363
        if (link->state == LINK_STATE_UNMANAGED)
2,955✔
1364
                return;
1365

1366
        bool notify = link_has_local_lease_domain(link);
1,997✔
1367

1368
        log_link_full(link, link->state == LINK_STATE_INITIALIZED ? LOG_DEBUG : LOG_INFO,
2,003✔
1369
                      "Unmanaging interface.");
1370

1371
        (void) link_stop_engines(link, /* may_keep_dynamic= */ false);
1,997✔
1372
        (void) link_drop_requests(link);
1,997✔
1373
        (void) link_drop_static_config(link);
1,997✔
1374

1375
        /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1376
         * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1377
         * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1378
         * map here, as it depends on .network files assigned to other links. */
1379
        link_free_bound_to_list(link);
1,997✔
1380
        link_free_engines(link);
1,997✔
1381

1382
        link->network = network_unref(link->network);
1,997✔
1383
        link_set_state(link, LINK_STATE_UNMANAGED);
1,997✔
1384

1385
        if (notify)
1,997✔
1386
                manager_notify_hook_filters(link->manager);
×
1387
}
1388

1389
static int link_managed_by_us(Link *link) {
7,472✔
1390
        int r;
7,472✔
1391

1392
        assert(link);
7,472✔
1393

1394
        if (!link->dev)
7,472✔
1395
                return true;
7,472✔
1396

1397
        const char *s;
7,351✔
1398
        r = sd_device_get_property_value(link->dev, "ID_NET_MANAGED_BY", &s);
7,351✔
1399
        if (r == -ENOENT)
7,351✔
1400
                return true;
1401
        if (r < 0)
42✔
1402
                return log_link_warning_errno(link, r, "Failed to get ID_NET_MANAGED_BY udev property: %m");
×
1403

1404
        if (streq(s, "io.systemd.Network"))
42✔
1405
                return true;
1406

1407
        if (link->state == LINK_STATE_UNMANAGED)
3✔
1408
                return false; /* Already in unmanaged state */
1409

1410
        log_link_debug(link, "Interface is requested to be managed by '%s', unmanaging the interface.", s);
1✔
1411
        link_set_state(link, LINK_STATE_UNMANAGED);
1✔
1412
        return false;
1413
}
1414

1415
int link_reconfigure_impl(Link *link, LinkReconfigurationFlag flags) {
5,539✔
1416
        Network *network = NULL;
5,539✔
1417
        int r;
5,539✔
1418

1419
        assert(link);
5,539✔
1420
        assert(link->manager);
5,539✔
1421

1422
        link_assign_netdev(link);
5,539✔
1423

1424
        if (link->manager->state != MANAGER_RUNNING)
5,539✔
1425
                return 0;
5,539✔
1426

1427
        if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
5,539✔
1428
                return 0;
1429

1430
        r = link_managed_by_us(link);
4,784✔
1431
        if (r <= 0)
4,784✔
1432
                return r;
1433

1434
        r = link_get_network(link, &network);
4,782✔
1435
        if (r == -ENOENT) {
4,782✔
1436
                link_enter_unmanaged(link);
2,955✔
1437
                return 0;
2,955✔
1438
        }
1439
        if (r < 0)
1,827✔
1440
                return r;
1441

1442
        if (link->network == network && !FLAGS_SET(flags, LINK_RECONFIGURE_UNCONDITIONALLY))
1,827✔
1443
                return 0;
1444

1445
        _cleanup_free_ char *joined = strv_join(network->dropins, ", ");
1,752✔
1446
        if (link->network)
876✔
1447
                log_link_info(link, "Reconfiguring with %s%s%s%s.",
594✔
1448
                              network->filename,
1449
                              isempty(joined) ? "" : " (dropins: ",
1450
                              joined,
1451
                              isempty(joined) ? "" : ")");
1452
        else
1453
                log_link_info(link, "Configuring with %s%s%s%s.",
2,034✔
1454
                              network->filename,
1455
                              isempty(joined) ? "" : " (dropins: ",
1456
                              joined,
1457
                              isempty(joined) ? "" : ")");
1458

1459
        /* Dropping configurations based on the old .network file. */
1460
        r = link_drop_requests(link);
876✔
1461
        if (r < 0)
876✔
1462
                return r;
1463

1464
        /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1465
         * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1466
         * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1467
         * map here, as it depends on .network files assigned to other links. */
1468
        link_free_bound_to_list(link);
876✔
1469

1470
        _cleanup_(network_unrefp) Network *old_network = TAKE_PTR(link->network);
876✔
1471

1472
        /* Then, apply new .network file */
1473
        link->network = network_ref(network);
876✔
1474

1475
        if (FLAGS_SET(network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC) ||
876✔
1476
            !FLAGS_SET(flags, LINK_RECONFIGURE_CLEANLY)) {
869✔
1477
                /* To make 'networkctl reconfigure INTERFACE' work safely for an interface whose new .network
1478
                 * file has KeepConfiguration=dynamic or yes, even if a clean reconfiguration is requested,
1479
                 * drop only unnecessary or possibly being changed dynamic configurations here. */
1480
                r = link_drop_dynamic_config(link, old_network);
838✔
1481
                if (r < 0)
838✔
1482
                        return r;
1483
        } else {
1484
                /* Otherwise, stop DHCP client and friends unconditionally, and drop all dynamic
1485
                 * configurations like DHCP address and routes. */
1486
                r = link_stop_engines(link, /* may_keep_dynamic= */ false);
38✔
1487
                if (r < 0)
38✔
1488
                        return r;
1489

1490
                /* Free DHCP client and friends. */
1491
                link_free_engines(link);
38✔
1492
        }
1493

1494
        link_update_operstate(link, true);
876✔
1495
        link_dirty(link);
876✔
1496

1497
        link_set_state(link, LINK_STATE_INITIALIZED);
876✔
1498
        link->activated = false;
876✔
1499

1500
        r = link_configure(link);
876✔
1501
        if (r < 0)
876✔
1502
                return r;
×
1503

1504
        return 1;
1505
}
1506

1507
typedef struct LinkReconfigurationData {
1508
        Manager *manager;
1509
        Link *link;
1510
        LinkReconfigurationFlag flags;
1511
        sd_bus_message *message;
1512
        unsigned *counter;
1513
} LinkReconfigurationData;
1514

1515
static LinkReconfigurationData* link_reconfiguration_data_free(LinkReconfigurationData *data) {
1,364✔
1516
        if (!data)
1,364✔
1517
                return NULL;
1518

1519
        link_unref(data->link);
1,364✔
1520
        sd_bus_message_unref(data->message);
1,364✔
1521

1522
        return mfree(data);
1,364✔
1523
}
1524

1525
DEFINE_TRIVIAL_CLEANUP_FUNC(LinkReconfigurationData*, link_reconfiguration_data_free);
1,364✔
1526

1527
static void link_reconfiguration_data_destroy_callback(LinkReconfigurationData *data) {
1,364✔
1528
        int r;
1,364✔
1529

1530
        assert(data);
1,364✔
1531

1532
        if (data->message) {
1,364✔
1533
                if (data->counter) {
1,259✔
1534
                        assert(*data->counter > 0);
1,221✔
1535
                        (*data->counter)--;
1,221✔
1536
                }
1537

1538
                if (!data->counter || *data->counter <= 0) {
1,259✔
1539
                        /* Update the state files before replying the bus method. Otherwise,
1540
                         * systemd-networkd-wait-online following networkctl reload/reconfigure may read an
1541
                         * outdated state file and wrongly handle an interface is already in the configured
1542
                         * state. */
1543
                        (void) manager_clean_all(data->manager);
191✔
1544

1545
                        r = sd_bus_reply_method_return(data->message, NULL);
191✔
1546
                        if (r < 0)
191✔
1547
                                log_warning_errno(r, "Failed to reply for DBus method, ignoring: %m");
×
1548
                }
1549
        }
1550

1551
        link_reconfiguration_data_free(data);
1,364✔
1552
}
1,364✔
1553

1554
static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, LinkReconfigurationData *data) {
1,364✔
1555
        Link *link = ASSERT_PTR(ASSERT_PTR(data)->link);
1,364✔
1556
        int r;
1,364✔
1557

1558
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1,364✔
1559
        if (r <= 0)
1,364✔
1560
                return r;
1561

1562
        r = link_reconfigure_impl(link, data->flags);
1,359✔
1563
        if (r < 0) {
1,359✔
1564
                link_enter_failed(link);
×
1565
                return 0;
×
1566
        }
1567

1568
        return r;
1569
}
1570

1571
int link_reconfigure_full(Link *link, LinkReconfigurationFlag flags, sd_bus_message *message, unsigned *counter) {
1,364✔
1572
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1,364✔
1573
        _cleanup_(link_reconfiguration_data_freep) LinkReconfigurationData *data = NULL;
1,364✔
1574
        int r;
1,364✔
1575

1576
        assert(link);
1,364✔
1577
        assert(link->manager);
1,364✔
1578
        assert(link->manager->rtnl);
1,364✔
1579

1580
        /* When the link is in the pending or initialized state, link_reconfigure_impl() will be called later
1581
         * by link_initialized() or link_initialized_and_synced(). To prevent the function from being called
1582
         * multiple times simultaneously, let's refuse to reconfigure the interface here in such cases. */
1583
        if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED, LINK_STATE_LINGER))
1,364✔
1584
                return 0; /* 0 means no-op. */
1585

1586
        data = new(LinkReconfigurationData, 1);
1,364✔
1587
        if (!data) {
1,364✔
1588
                r = -ENOMEM;
×
1589
                goto failed;
×
1590
        }
1591

1592
        *data = (LinkReconfigurationData) {
2,728✔
1593
                .manager = link->manager,
1,364✔
1594
                .link = link_ref(link),
1,364✔
1595
                .flags = flags,
1596
                .message = sd_bus_message_ref(message), /* message may be NULL, but _ref() works fine. */
1,364✔
1597
                .counter = counter,
1598
        };
1599

1600
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
1,364✔
1601
        if (r < 0)
1,364✔
1602
                goto failed;
×
1603

1604
        r = netlink_call_async(link->manager->rtnl, NULL, req,
1,364✔
1605
                               link_reconfigure_handler,
1606
                               link_reconfiguration_data_destroy_callback, data);
1607
        if (r < 0)
1,364✔
1608
                goto failed;
×
1609

1610
        TAKE_PTR(data);
1,364✔
1611
        if (counter)
1,364✔
1612
                (*counter)++;
1,221✔
1613

1614
        if (link->state == LINK_STATE_FAILED)
1,364✔
1615
                link_set_state(link, LINK_STATE_INITIALIZED);
5✔
1616

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

1619
failed:
1620
        log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
1621
        link_enter_failed(link);
×
1622
        return r;
1623
}
1624

1625
static int link_initialized_and_synced(Link *link) {
2,665✔
1626
        int r;
2,665✔
1627

1628
        assert(link);
2,665✔
1629
        assert(link->manager);
2,665✔
1630

1631
        if (link->state == LINK_STATE_PENDING) {
2,665✔
1632
                log_link_debug(link, "Link state is up-to-date");
2,663✔
1633
                link_set_state(link, LINK_STATE_INITIALIZED);
2,663✔
1634

1635
                r = link_new_bound_by_list(link);
2,663✔
1636
                if (r < 0)
2,663✔
1637
                        return r;
1638

1639
                r = link_handle_bound_by_list(link);
2,663✔
1640
                if (r < 0)
2,663✔
1641
                        return r;
1642
        }
1643

1644
        return link_reconfigure_impl(link, /* flags= */ 0);
2,665✔
1645
}
1646

1647
static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2,588✔
1648
        int r;
2,588✔
1649

1650
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
2,588✔
1651
        if (r <= 0)
2,588✔
1652
                return r;
1653

1654
        r = link_initialized_and_synced(link);
2,587✔
1655
        if (r < 0)
2,587✔
1656
                link_enter_failed(link);
×
1657

1658
        return 0;
1659
}
1660

1661
static int link_initialized(Link *link, sd_device *device) {
2,688✔
1662
        int r;
2,688✔
1663

1664
        assert(link);
2,688✔
1665
        assert(device);
2,688✔
1666

1667
        /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1668
         * or sysattrs) may be outdated. */
1669
        device_unref_and_replace(link->dev, device);
2,688✔
1670

1671
        r = link_managed_by_us(link);
2,688✔
1672
        if (r <= 0)
2,688✔
1673
                return r;
1674

1675
        if (link->dhcp_client) {
2,687✔
1676
                r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev);
×
1677
                if (r < 0)
×
1678
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv4 client, ignoring: %m");
×
1679
        }
1680

1681
        if (link->dhcp6_client) {
2,687✔
1682
                r = sd_dhcp6_client_attach_device(link->dhcp6_client, link->dev);
×
1683
                if (r < 0)
×
1684
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv6 client, ignoring: %m");
×
1685
        }
1686

1687
        r = link_set_sr_iov_ifindices(link);
2,687✔
1688
        if (r < 0)
2,687✔
1689
                log_link_warning_errno(link, r, "Failed to manage SR-IOV PF and VF ports, ignoring: %m");
×
1690

1691
        if (link->state != LINK_STATE_PENDING)
2,687✔
1692
                return link_reconfigure(link, /* flags= */ 0);
99✔
1693

1694
        log_link_debug(link, "udev initialized link");
2,588✔
1695

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

1701
        return link_call_getlink(link, link_initialized_handler);
2,588✔
1702
}
1703

1704
int link_check_initialized(Link *link) {
2,837✔
1705
        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
2,837✔
1706
        int r;
2,837✔
1707

1708
        assert(link);
2,837✔
1709

1710
        if (!udev_available())
2,837✔
1711
                return link_initialized_and_synced(link);
78✔
1712

1713
        /* udev should be around */
1714
        r = sd_device_new_from_ifindex(&device, link->ifindex);
2,759✔
1715
        if (r < 0) {
2,759✔
1716
                log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
2✔
1717
                return 0;
2✔
1718
        }
1719

1720
        r = device_is_processed(device);
2,757✔
1721
        if (r < 0)
2,757✔
1722
                return log_link_warning_errno(link, r, "Could not determine whether the device is processed by udevd: %m");
×
1723
        if (r == 0) {
2,757✔
1724
                /* not yet ready */
1725
                log_link_debug(link, "link pending udev initialization...");
962✔
1726
                return 0;
962✔
1727
        }
1728

1729
        r = device_is_renaming(device);
1,795✔
1730
        if (r < 0)
1,795✔
1731
                return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
×
1732
        if (r > 0) {
1,795✔
1733
                log_link_debug(link, "Interface is being renamed, pending initialization.");
×
1734
                return 0;
×
1735
        }
1736

1737
        return link_initialized(link, device);
1,795✔
1738
}
1739

1740
int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t action) {
1,948✔
1741
        int r, ifindex;
1,948✔
1742
        Link *link;
1,948✔
1743

1744
        assert(m);
1,948✔
1745
        assert(device);
1,948✔
1746

1747
        r = sd_device_get_ifindex(device, &ifindex);
1,948✔
1748
        if (r < 0)
1,948✔
1749
                return log_device_debug_errno(device, r, "Failed to get ifindex: %m");
×
1750

1751
        r = link_get_by_index(m, ifindex, &link);
1,948✔
1752
        if (r < 0) {
1,948✔
1753
                /* This error is not critical, as the corresponding rtnl message may be received later. */
1754
                log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
739✔
1755
                return 0;
739✔
1756
        }
1757

1758
        /* Let's unref the sd-device object assigned to the corresponding Link object, but keep the Link
1759
         * object here. It will be removed only when rtnetlink says so. */
1760
        if (action == SD_DEVICE_REMOVE) {
1,209✔
1761
                link->dev = sd_device_unref(link->dev);
307✔
1762
                return 0;
307✔
1763
        }
1764

1765
        r = device_is_renaming(device);
902✔
1766
        if (r < 0)
902✔
1767
                return log_device_debug_errno(device, r, "Failed to determine if the device is renaming or not: %m");
×
1768
        if (r > 0) {
902✔
1769
                log_device_debug(device, "Device is renaming, waiting for the interface to be renamed.");
9✔
1770
                /* TODO:
1771
                 * What happens when a device is initialized, then soon renamed after that? When we detect
1772
                 * such, maybe we should cancel or postpone all queued requests for the interface. */
1773
                return 0;
9✔
1774
        }
1775

1776
        r = link_initialized(link, device);
893✔
1777
        if (r < 0)
893✔
1778
                link_enter_failed(link);
×
1779

1780
        return 0;
1781
}
1782

1783
static int link_carrier_gained(Link *link) {
1,280✔
1784
        LinkReconfigurationFlag flags = 0;
1,280✔
1785
        int r;
1,280✔
1786

1787
        assert(link);
1,280✔
1788

1789
        log_link_info(link, "Gained carrier");
1,280✔
1790

1791
        r = event_source_disable(link->carrier_lost_timer);
1,280✔
1792
        if (r < 0)
1,280✔
1793
                log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
×
1794

1795
        /* Process BindCarrier= setting specified by other interfaces. This is independent of the .network
1796
         * file assigned to this interface, but depends on .network files assigned to other interfaces.
1797
         * Hence, this can and should be called earlier. */
1798
        r = link_handle_bound_by_list(link);
1,280✔
1799
        if (r < 0)
1,280✔
1800
                return r;
1801

1802
        /* If a wireless interface was connected to an access point, and the SSID is changed (that is,
1803
         * both previous_ssid and ssid are non-NULL), then the connected wireless network could be
1804
         * changed. So, always reconfigure the link. Which means e.g. the DHCP client will be
1805
         * restarted, and the correct network information will be gained.
1806
         *
1807
         * However, do not reconfigure the wireless interface forcibly if it was not connected to any
1808
         * access points previously (previous_ssid is NULL in this case). As, a .network file may be
1809
         * already assigned to the interface (in that case, the .network file does not have the SSID=
1810
         * setting in the [Match] section), and the interface is already being configured. Of course,
1811
         * there may exist another .network file with higher priority and a matching SSID= setting. But
1812
         * in that case, link_reconfigure_impl() can handle that without any flags.
1813
         *
1814
         * For non-wireless interfaces, we have no way to detect the connected network change. So,
1815
         * we do not set any flags here. Note, both ssid and previous_ssid are NULL in that case. */
1816
        if (link->previous_ssid && !streq_ptr(link->previous_ssid, link->ssid))
1,280✔
1817
                flags |= LINK_RECONFIGURE_UNCONDITIONALLY | LINK_RECONFIGURE_CLEANLY;
×
1818
        link->previous_ssid = mfree(link->previous_ssid);
1,280✔
1819

1820
        /* AP and P2P-GO interfaces may have a new SSID - update the link properties in case a new .network
1821
         * profile wants to match on it with SSID= in its [Match] section.
1822
         */
1823
        if (IN_SET(link->wlan_iftype, NL80211_IFTYPE_AP, NL80211_IFTYPE_P2P_GO)) {
1,280✔
1824
                r = link_get_wlan_interface(link);
×
1825
                if (r < 0)
×
1826
                        return r;
1827
        }
1828

1829
        /* At this stage, both wlan and link information should be up-to-date. Hence, it is not necessary to
1830
         * call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or NL80211_CMD_GET_STATION commands, and simply call
1831
         * link_reconfigure_impl(). Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1832
        r = link_reconfigure_impl(link, flags);
1,280✔
1833
        if (r != 0)
1,280✔
1834
                return r;
1835

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

1840
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
1,276✔
1841
                r = link_acquire_dynamic_conf(link);
695✔
1842
                if (r < 0)
695✔
1843
                        return r;
1844

1845
                r = link_request_static_configs(link);
695✔
1846
                if (r < 0)
695✔
1847
                        return r;
×
1848
        }
1849

1850
        return 0;
1851
}
1852

1853
static int link_carrier_lost_impl(Link *link) {
737✔
1854
        int ret = 0;
737✔
1855

1856
        assert(link);
737✔
1857

1858
        link->previous_ssid = mfree(link->previous_ssid);
737✔
1859

1860
        ret = link_handle_bound_by_list(link);
737✔
1861

1862
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
737✔
1863
                return ret;
737✔
1864

1865
        if (!link->network)
737✔
1866
                return ret;
1867

1868
        RET_GATHER(ret, link_stop_engines(link, /* may_keep_dynamic= */ false));
679✔
1869
        RET_GATHER(ret, link_drop_static_config(link));
679✔
1870

1871
        return ret;
1872
}
1873

1874
static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
×
1875
        Link *link = ASSERT_PTR(userdata);
×
1876
        int r;
×
1877

1878
        r = link_carrier_lost_impl(link);
×
1879
        if (r < 0) {
×
1880
                log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
×
1881
                link_enter_failed(link);
×
1882
        }
1883

1884
        return 0;
×
1885
}
1886

1887
static int link_carrier_lost(Link *link) {
755✔
1888
        uint16_t dhcp_mtu;
755✔
1889
        usec_t usec;
755✔
1890

1891
        assert(link);
755✔
1892

1893
        log_link_info(link, "Lost carrier");
755✔
1894

1895
        if (link->iftype == ARPHRD_CAN)
755✔
1896
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1897
                usec = 0;
1898

1899
        else if (!link->network)
751✔
1900
                usec = 0;
1901

1902
        else if (link->network->ignore_carrier_loss_set)
693✔
1903
                /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1904
                usec = link->network->ignore_carrier_loss_usec;
12✔
1905

1906
        else if (link->network->bond && link->wlan_iftype > 0)
681✔
1907
                /* Enslaving wlan interface to a bond disconnects from the connected AP, and causes its
1908
                 * carrier to be lost. See #19832. */
1909
                usec = 3 * USEC_PER_SEC;
1910

1911
        else if (link->network->dhcp_use_mtu &&
681✔
1912
                 link->dhcp_lease &&
4✔
1913
                 sd_dhcp_lease_get_mtu(link->dhcp_lease, &dhcp_mtu) >= 0 &&
2✔
1914
                 dhcp_mtu != link->original_mtu)
1✔
1915
                /* Some drivers reset interfaces when changing MTU. Resetting interfaces by the static
1916
                 * MTU should not cause any issues, as MTU is changed only once. However, setting MTU
1917
                 * through DHCP lease causes an infinite loop of resetting the interface. See #18738. */
1918
                usec = 5 * USEC_PER_SEC;
1919

1920
        else
1921
                /* Otherwise, use the implied default value. */
1922
                usec = link->network->ignore_carrier_loss_usec;
680✔
1923

1924
        if (usec == USEC_INFINITY)
692✔
1925
                return 0;
755✔
1926

1927
        if (usec == 0)
738✔
1928
                return link_carrier_lost_impl(link);
737✔
1929

1930
        return event_reset_time_relative(link->manager->event,
1✔
1931
                                         &link->carrier_lost_timer,
1932
                                         CLOCK_BOOTTIME,
1933
                                         usec,
1934
                                         0,
1935
                                         link_carrier_lost_handler,
1936
                                         link,
1937
                                         0,
1938
                                         "link-carrier-loss",
1939
                                         true);
1940
}
1941

1942
static int link_admin_state_up(Link *link) {
1,253✔
1943
        assert(link);
1,253✔
1944

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

1948
        log_link_info(link, "Link UP");
1,253✔
1949

1950
        if (!link->network)
1,253✔
1951
                return 0;
1952

1953
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
668✔
1954
                log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
4✔
1955
                return link_request_to_bring_up_or_down(link, /* up= */ false);
4✔
1956
        }
1957

1958
        /* We set the ipv6 mtu after the device mtu, but the kernel resets
1959
         * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1960
        (void) link_set_ipv6_mtu(link, LOG_INFO);
664✔
1961

1962
        return 0;
664✔
1963
}
1964

1965
static int link_admin_state_down(Link *link) {
726✔
1966
        assert(link);
726✔
1967

1968
        log_link_info(link, "Link DOWN");
726✔
1969

1970
        link_forget_nexthops(link);
726✔
1971
        link_forget_routes(link);
726✔
1972

1973
        if (!link->network)
726✔
1974
                return 0;
1975

1976
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
669✔
1977
                log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
9✔
1978
                return link_request_to_bring_up_or_down(link, /* up= */ true);
9✔
1979
        }
1980

1981
        return 0;
1982
}
1983

1984
static bool link_is_enslaved(Link *link) {
7,025✔
1985
        if (link->flags & IFF_SLAVE)
7,025✔
1986
                return true;
1987

1988
        if (link->master_ifindex > 0)
6,970✔
1989
                return true;
245✔
1990

1991
        return false;
1992
}
1993

1994
void link_update_operstate(Link *link, bool also_update_master) {
13,460✔
1995
        LinkOperationalState operstate;
13,460✔
1996
        LinkCarrierState carrier_state;
13,460✔
1997
        LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
13,460✔
1998
        LinkOnlineState online_state;
13,460✔
1999
        _cleanup_strv_free_ char **p = NULL;
13,460✔
2000
        bool changed = false;
13,460✔
2001

2002
        assert(link);
13,460✔
2003

2004
        if (link->kernel_operstate == IF_OPER_DORMANT)
13,460✔
2005
                carrier_state = LINK_CARRIER_STATE_DORMANT;
2006
        else if (link_has_carrier(link)) {
13,460✔
2007
                if (link_is_enslaved(link))
7,025✔
2008
                        carrier_state = LINK_CARRIER_STATE_ENSLAVED;
2009
                else
2010
                        carrier_state = LINK_CARRIER_STATE_CARRIER;
2011
        } else if (link_is_up(link))
6,435✔
2012
                carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
2013
        else
2014
                carrier_state = LINK_CARRIER_STATE_OFF;
2015

2016
        if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
2017
                Link *slave;
7,025✔
2018

2019
                SET_FOREACH(slave, link->slaves) {
7,272✔
2020
                        link_update_operstate(slave, false);
247✔
2021

2022
                        if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
247✔
2023
                                carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
49✔
2024
                }
2025
        }
2026

2027
        link_get_address_states(link, &ipv4_address_state, &ipv6_address_state, &address_state);
13,460✔
2028

2029
        /* Mapping of address and carrier state vs operational state
2030
         *                                                     carrier state
2031
         *                          | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
2032
         *                 ------------------------------------------------------------------------------
2033
         *                 off      | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
2034
         * address_state   degraded | off | no-carrier | dormant | degraded         | degraded | enslaved
2035
         *                 routable | off | no-carrier | dormant | routable         | routable | routable
2036
         */
2037

2038
        if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_ROUTABLE)
13,460✔
2039
                operstate = LINK_OPERSTATE_ROUTABLE;
2040
        else if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_DEGRADED)
24✔
2041
                operstate = LINK_OPERSTATE_DEGRADED;
2042
        else if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
13,431✔
2043
                operstate = (LinkOperationalState) carrier_state;
2044
        else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
4,513✔
2045
                operstate = LINK_OPERSTATE_ROUTABLE;
2046
        else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1,366✔
2047
                operstate = LINK_OPERSTATE_DEGRADED;
2048
        else
2049
                operstate = LINK_OPERSTATE_ENSLAVED;
34✔
2050

2051
        LinkOperationalStateRange req;
13,460✔
2052
        link_required_operstate_for_online(link, &req);
13,460✔
2053

2054
        /* Only determine online state for managed links with RequiredForOnline=yes */
2055
        if (!link->network || !link->network->required_for_online)
13,460✔
2056
                online_state = _LINK_ONLINE_STATE_INVALID;
2057

2058
        else if (!operational_state_is_in_range(operstate, &req))
8,855✔
2059
                online_state = LINK_ONLINE_STATE_OFFLINE;
2060

2061
        else {
2062
                AddressFamily required_family = link_required_family_for_online(link);
4,425✔
2063
                bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
4,425✔
2064
                bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
4,425✔
2065

2066
                /* The operational state is within the range required for online.
2067
                 * If a particular address family is also required, we might revert
2068
                 * to offline in the blocks below. */
2069
                online_state = LINK_ONLINE_STATE_ONLINE;
4,425✔
2070

2071
                if (req.min >= LINK_OPERSTATE_DEGRADED) {
4,425✔
2072
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
4,265✔
2073
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2074
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
4,265✔
2075
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2076
                }
2077

2078
                if (req.min >= LINK_OPERSTATE_ROUTABLE) {
4,425✔
2079
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2080
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2081
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2082
                                online_state = LINK_ONLINE_STATE_OFFLINE;
3✔
2083
                }
2084
        }
2085

2086
        if (link->carrier_state != carrier_state) {
13,460✔
2087
                link->carrier_state = carrier_state;
4,705✔
2088
                changed = true;
4,705✔
2089
                if (strv_extend(&p, "CarrierState") < 0)
4,705✔
2090
                        log_oom();
×
2091
        }
2092

2093
        if (link->address_state != address_state) {
13,460✔
2094
                link->address_state = address_state;
2,003✔
2095
                changed = true;
2,003✔
2096
                if (strv_extend(&p, "AddressState") < 0)
2,003✔
2097
                        log_oom();
×
2098
        }
2099

2100
        if (link->ipv4_address_state != ipv4_address_state) {
13,460✔
2101
                link->ipv4_address_state = ipv4_address_state;
969✔
2102
                changed = true;
969✔
2103
                if (strv_extend(&p, "IPv4AddressState") < 0)
969✔
2104
                        log_oom();
×
2105
        }
2106

2107
        if (link->ipv6_address_state != ipv6_address_state) {
13,460✔
2108
                link->ipv6_address_state = ipv6_address_state;
1,711✔
2109
                changed = true;
1,711✔
2110
                if (strv_extend(&p, "IPv6AddressState") < 0)
1,711✔
2111
                        log_oom();
×
2112
        }
2113

2114
        if (link->operstate != operstate) {
13,460✔
2115
                link->operstate = operstate;
5,708✔
2116
                changed = true;
5,708✔
2117
                if (strv_extend(&p, "OperationalState") < 0)
5,708✔
2118
                        log_oom();
×
2119
        }
2120

2121
        if (link->online_state != online_state) {
13,460✔
2122
                link->online_state = online_state;
1,889✔
2123
                changed = true;
1,889✔
2124
                if (strv_extend(&p, "OnlineState") < 0)
1,889✔
2125
                        log_oom();
×
2126
        }
2127

2128
        if (p)
13,460✔
2129
                link_send_changed_strv(link, p);
8,025✔
2130
        if (changed)
13,460✔
2131
                link_dirty(link);
8,025✔
2132

2133
        if (also_update_master) {
13,460✔
2134
                Link *master;
13,213✔
2135

2136
                if (link_get_master(link, &master) >= 0)
13,213✔
2137
                        link_update_operstate(master, true);
148✔
2138
        }
2139
}
13,460✔
2140

2141
bool link_has_carrier(Link *link) {
43,587✔
2142
        assert(link);
43,587✔
2143
        return netif_has_carrier(link->kernel_operstate, link->flags);
43,587✔
2144
}
2145

2146
bool link_is_up(Link *link) {
20,670✔
2147
        assert(link);
20,670✔
2148
        return FLAGS_SET(link->flags, IFF_UP);
20,670✔
2149
}
2150

2151
bool link_multicast_enabled(Link *link) {
9,860✔
2152
        assert(link);
9,860✔
2153

2154
        /* If Multicast= is specified, use the value. */
2155
        if (link->network && link->network->multicast >= 0)
9,860✔
2156
                return link->network->multicast;
×
2157

2158
        /* Otherwise, return the current state. */
2159
        return FLAGS_SET(link->flags, IFF_MULTICAST);
9,860✔
2160
}
2161

2162
#define FLAG_STRING(string, flag, old, new)                      \
2163
        (((old ^ new) & flag)                                    \
2164
         ? ((old & flag) ? (" -" string) : (" +" string))        \
2165
         : "")
2166

2167
static int link_update_flags(Link *link, sd_netlink_message *message) {
10,725✔
2168
        bool link_was_admin_up, had_carrier;
10,725✔
2169
        uint8_t operstate;
10,725✔
2170
        unsigned flags;
10,725✔
2171
        int r;
10,725✔
2172

2173
        assert(link);
10,725✔
2174
        assert(message);
10,725✔
2175

2176
        r = sd_rtnl_message_link_get_flags(message, &flags);
10,725✔
2177
        if (r < 0)
10,725✔
2178
                return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
×
2179

2180
        r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
10,725✔
2181
        if (r == -ENODATA)
10,725✔
2182
                /* If we got a message without operstate, assume the state was unchanged. */
2183
                operstate = link->kernel_operstate;
×
2184
        else if (r < 0)
10,725✔
2185
                return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
×
2186

2187
        if (link->flags == flags && link->kernel_operstate == operstate)
10,725✔
2188
                return 0;
2189

2190
        if (link->flags != flags) {
4,690✔
2191
                unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
4,689✔
2192

2193
                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",
12,928✔
2194
                               FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
2195
                               FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
2196
                               FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
2197
                               FLAG_STRING("UP", IFF_UP, link->flags, flags),
2198
                               FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
2199
                               FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
2200
                               FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
2201
                               FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
2202
                               FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
2203
                               FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
2204
                               FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
2205
                               FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
2206
                               FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
2207
                               FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
2208
                               FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
2209
                               FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
2210
                               FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
2211
                               FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
2212
                               FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
2213

2214
                unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
4,689✔
2215
                                  IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
2216
                                  IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
2217
                                  IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
2218
                                  IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
2219
                                  IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
2220
                unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
4,689✔
2221
                unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
4,689✔
2222

2223
                if (unknown_flags_added)
4,689✔
2224
                        log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
×
2225

2226
                if (unknown_flags_removed)
4,689✔
2227
                        log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
×
2228
        }
2229

2230
        link_was_admin_up = link_is_up(link);
4,690✔
2231
        had_carrier = link_has_carrier(link);
4,690✔
2232

2233
        link->flags = flags;
4,690✔
2234
        link->kernel_operstate = operstate;
4,690✔
2235

2236
        link_update_operstate(link, true);
4,690✔
2237

2238
        r = 0;
4,690✔
2239

2240
        if (!link_was_admin_up && link_is_up(link))
4,690✔
2241
                r = link_admin_state_up(link);
1,253✔
2242
        else if (link_was_admin_up && !link_is_up(link))
1,116✔
2243
                r = link_admin_state_down(link);
726✔
2244
        if (r < 0)
1,979✔
2245
                return r;
2246

2247
        if (!had_carrier && link_has_carrier(link))
4,690✔
2248
                r = link_carrier_gained(link);
1,280✔
2249
        else if (had_carrier && !link_has_carrier(link))
771✔
2250
                r = link_carrier_lost(link);
755✔
2251
        if (r < 0)
4,690✔
2252
                return r;
×
2253

2254
        return 0;
2255
}
2256

2257
static int link_update_master(Link *link, sd_netlink_message *message) {
10,725✔
2258
        int master_ifindex, r;
10,725✔
2259

2260
        assert(link);
10,725✔
2261
        assert(message);
10,725✔
2262

2263
        r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
10,725✔
2264
        if (r == -ENODATA)
10,725✔
2265
                master_ifindex = 0; /* no master interface */
10,213✔
2266
        else if (r < 0)
512✔
2267
                return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
×
2268

2269
        if (master_ifindex == link->ifindex)
10,725✔
2270
                master_ifindex = 0;
7✔
2271

2272
        if (master_ifindex != link->master_ifindex) {
10,725✔
2273
                if (link->master_ifindex == 0)
60✔
2274
                        log_link_debug(link, "Attached to master interface: %i", master_ifindex);
46✔
2275
                else if (master_ifindex == 0)
14✔
2276
                        log_link_debug(link, "Detached from master interface: %i", link->master_ifindex);
14✔
2277
                else
2278
                        log_link_debug(link, "Master interface changed: %i %s %i", link->master_ifindex,
×
2279
                                       glyph(GLYPH_ARROW_RIGHT), master_ifindex);
2280

2281
                link_drop_from_master(link);
60✔
2282
                link->master_ifindex = master_ifindex;
60✔
2283

2284
                /* Updating master ifindex may cause operational state change, e.g. carrier <-> enslaved */
2285
                link_dirty(link);
60✔
2286
        }
2287

2288
        r = link_append_to_master(link);
10,725✔
2289
        if (r < 0)
10,725✔
2290
                return log_link_debug_errno(link, r, "Failed to append link to master: %m");
×
2291

2292
        return 0;
2293
}
2294

2295
static int link_update_driver(Link *link, sd_netlink_message *message) {
10,725✔
2296
        int r;
10,725✔
2297

2298
        assert(link);
10,725✔
2299
        assert(link->manager);
10,725✔
2300
        assert(message);
10,725✔
2301

2302
        /* Driver is already read. Assuming the driver is never changed. */
2303
        if (link->ethtool_driver_read)
10,725✔
2304
                return 0;
2305

2306
        /* When udevd is running, read the driver after the interface is initialized by udevd.
2307
         * Otherwise, ethtool may not work correctly. See issue #22538.
2308
         * When udevd is not running, read the value when the interface is detected. */
2309
        if (udev_available() && !link->dev)
5,785✔
2310
                return 0;
2311

2312
        link->ethtool_driver_read = true;
2,665✔
2313

2314
        r = ethtool_get_driver(&link->manager->ethtool_fd, link->ifname, &link->driver);
2,665✔
2315
        if (r < 0) {
2,665✔
2316
                log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
501✔
2317
                return 0;
501✔
2318
        }
2319

2320
        log_link_debug(link, "Found driver: %s", strna(link->driver));
2,164✔
2321

2322
        if (streq_ptr(link->driver, "dsa")) {
2,164✔
2323
                uint32_t dsa_master_ifindex = 0;
×
2324

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

2329
                if (dsa_master_ifindex > INT_MAX) {
×
2330
                        log_link_debug(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
×
2331
                                       dsa_master_ifindex);
2332
                        dsa_master_ifindex = 0;
×
2333
                }
2334

2335
                link->dsa_master_ifindex = (int) dsa_master_ifindex;
×
2336
        }
2337

2338
        return 1; /* needs reconfigure */
2339
}
2340

2341
static int link_update_permanent_hardware_address(Link *link, sd_netlink_message *message) {
10,725✔
2342
        int r;
10,725✔
2343

2344
        assert(link);
10,725✔
2345
        assert(link->manager);
10,725✔
2346
        assert(message);
10,725✔
2347

2348
        if (link->permanent_hw_addr.length > 0)
10,725✔
2349
                return 0;
2350

2351
        r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
10,053✔
2352
        if (r == -ENODATA)
10,053✔
2353
                return 0;
2354
        if (r < 0)
224✔
2355
                return log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute: %m");
×
2356

2357
        if (link->permanent_hw_addr.length > 0)
224✔
2358
                log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
224✔
2359

2360
        return 1; /* needs reconfigure */
2361
}
2362

2363
static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
10,725✔
2364
        struct hw_addr_data addr;
10,725✔
2365
        int r;
10,725✔
2366

2367
        assert(link);
10,725✔
2368
        assert(message);
10,725✔
2369

2370
        r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
10,725✔
2371
        if (r < 0 && r != -ENODATA)
10,725✔
2372
                return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
×
2373

2374
        r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
10,725✔
2375
        if (r == -ENODATA)
10,725✔
2376
                return 0;
2377
        if (r < 0)
10,517✔
2378
                return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
×
2379

2380
        if (hw_addr_equal(&link->hw_addr, &addr))
10,517✔
2381
                return 0;
2382

2383
        if (link->hw_addr.length == 0)
2,969✔
2384
                log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
2,775✔
2385
        else {
2386
                log_link_debug(link, "Hardware address is changed: %s %s %s",
194✔
2387
                               HW_ADDR_TO_STR(&link->hw_addr),
2388
                               glyph(GLYPH_ARROW_RIGHT),
2389
                               HW_ADDR_TO_STR(&addr));
2390

2391
                hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
194✔
2392
        }
2393

2394
        link->hw_addr = addr;
2,969✔
2395

2396
        if (!hw_addr_is_null(&link->hw_addr)) {
2,969✔
2397
                r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
1,072✔
2398
                if (r == -EEXIST && streq_ptr(link->kind, "bond"))
1,072✔
2399
                        /* bonding master and its slaves have the same hardware address. */
2400
                        r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
3✔
2401
                if (r < 0)
1,072✔
2402
                        log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
51✔
2403
        }
2404

2405
        r = ipv4acd_update_mac(link);
2,969✔
2406
        if (r < 0)
2,969✔
2407
                return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
×
2408

2409
        r = ipv4ll_update_mac(link);
2,969✔
2410
        if (r < 0)
2,969✔
2411
                return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
×
2412

2413
        r = dhcp4_update_mac(link);
2,969✔
2414
        if (r < 0)
2,969✔
2415
                return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
×
2416

2417
        r = dhcp6_update_mac(link);
2,969✔
2418
        if (r < 0)
2,969✔
2419
                return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
×
2420

2421
        r = radv_update_mac(link);
2,969✔
2422
        if (r < 0)
2,969✔
2423
                return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
×
2424

2425
        if (link->ndisc && link->hw_addr.length == ETH_ALEN) {
2,969✔
2426
                r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
×
2427
                if (r < 0)
×
2428
                        return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
×
2429
        }
2430

2431
        if (link->lldp_rx) {
2,969✔
2432
                r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
23✔
2433
                if (r < 0)
23✔
2434
                        return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
×
2435
        }
2436

2437
        if (link->lldp_tx) {
2,969✔
2438
                r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
×
2439
                if (r < 0)
×
2440
                        return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
×
2441
        }
2442

2443
        return 1; /* needs reconfigure */
2444
}
2445

2446
static int link_update_mtu(Link *link, sd_netlink_message *message) {
10,725✔
2447
        uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
10,725✔
2448
        int r;
10,725✔
2449

2450
        assert(link);
10,725✔
2451
        assert(message);
10,725✔
2452

2453
        r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
10,725✔
2454
        if (r == -ENODATA)
10,725✔
2455
                return 0;
10,725✔
2456
        if (r < 0)
10,725✔
2457
                return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
×
2458
        if (mtu == 0)
10,725✔
2459
                return 0;
2460

2461
        r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
10,703✔
2462
        if (r < 0 && r != -ENODATA)
10,703✔
2463
                return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
×
2464

2465
        r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
10,703✔
2466
        if (r < 0 && r != -ENODATA)
10,703✔
2467
                return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
×
2468

2469
        if (max_mtu == 0)
10,703✔
2470
                max_mtu = UINT32_MAX;
6,882✔
2471

2472
        link->min_mtu = min_mtu;
10,703✔
2473
        link->max_mtu = max_mtu;
10,703✔
2474

2475
        if (link->original_mtu == 0) {
10,703✔
2476
                link->original_mtu = mtu;
2,838✔
2477
                log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2,838✔
2478
                               link->original_mtu, link->min_mtu, link->max_mtu);
2479
        }
2480

2481
        if (link->mtu == mtu)
10,703✔
2482
                return 0;
2483

2484
        if (link->mtu != 0)
2,927✔
2485
                log_link_debug(link, "MTU is changed: %"PRIu32" %s %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
89✔
2486
                               link->mtu, glyph(GLYPH_ARROW_RIGHT), mtu,
2487
                               link->min_mtu, link->max_mtu);
2488

2489
        link->mtu = mtu;
2,927✔
2490

2491
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
2,927✔
2492
                /* The kernel resets IPv6 MTU after changing device MTU. So, we need to re-set IPv6 MTU again. */
2493
                (void) link_set_ipv6_mtu_async(link);
45✔
2494

2495
        if (link->dhcp_client) {
2,927✔
2496
                r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
4✔
2497
                if (r < 0)
4✔
2498
                        return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
×
2499
        }
2500

2501
        if (link->radv) {
2,927✔
2502
                r = sd_radv_set_mtu(link->radv, link->mtu);
×
2503
                if (r < 0)
×
2504
                        return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
×
2505
        }
2506

2507
        return 0;
2508
}
2509

2510
static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
10,725✔
2511
        _cleanup_strv_free_ char **altnames = NULL;
10,725✔
2512
        int r;
10,725✔
2513

2514
        assert(link);
10,725✔
2515
        assert(message);
10,725✔
2516

2517
        r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
10,725✔
2518
        if (r == -ENODATA)
10,725✔
2519
                /* The message does not have IFLA_PROP_LIST container attribute. It does not mean the
2520
                 * interface has no alternative name. */
2521
                return 0;
2522
        if (r < 0)
166✔
2523
                return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
×
2524

2525
        if (strv_equal(altnames, link->alternative_names))
166✔
2526
                return 0;
2527

2528
        STRV_FOREACH(n, link->alternative_names)
47✔
2529
                hashmap_remove(link->manager->links_by_name, *n);
10✔
2530

2531
        strv_free_and_replace(link->alternative_names, altnames);
37✔
2532

2533
        STRV_FOREACH(n, link->alternative_names) {
82✔
2534
                r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
45✔
2535
                if (r < 0)
45✔
2536
                        return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
×
2537
        }
2538

2539
        return 1; /* needs reconfigure */
2540
}
2541

2542
static int link_update_name(Link *link, sd_netlink_message *message) {
10,730✔
2543
        char ifname_from_index[IF_NAMESIZE];
10,730✔
2544
        const char *ifname;
10,730✔
2545
        int r;
10,730✔
2546

2547
        assert(link);
10,730✔
2548
        assert(message);
10,730✔
2549

2550
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
10,730✔
2551
        if (r == -ENODATA)
10,730✔
2552
                /* Hmm?? But ok. */
2553
                return 0;
10,730✔
2554
        if (r < 0)
10,730✔
2555
                return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
×
2556

2557
        if (streq(ifname, link->ifname))
10,730✔
2558
                return 0;
2559

2560
        r = format_ifname(link->ifindex, ifname_from_index);
36✔
2561
        if (r < 0)
36✔
2562
                return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
5✔
2563

2564
        if (!streq(ifname, ifname_from_index)) {
31✔
2565
                log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "
1✔
2566
                               "with the name currently configured on the actual interface '%s'. Ignoring.",
2567
                               ifname, ifname_from_index);
2568
                return 0;
1✔
2569
        }
2570

2571
        log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
30✔
2572

2573
        hashmap_remove(link->manager->links_by_name, link->ifname);
30✔
2574

2575
        r = free_and_strdup(&link->ifname, ifname);
30✔
2576
        if (r < 0)
30✔
2577
                return log_oom_debug();
×
2578

2579
        r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
30✔
2580
        if (r < 0)
30✔
2581
                return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
×
2582

2583
        if (link->dhcp_client) {
30✔
2584
                r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname);
×
2585
                if (r < 0)
×
2586
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m");
×
2587
        }
2588

2589
        if (link->dhcp6_client) {
30✔
2590
                r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
×
2591
                if (r < 0)
×
2592
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCPv6 client: %m");
×
2593
        }
2594

2595
        if (link->ndisc) {
30✔
2596
                r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
×
2597
                if (r < 0)
×
2598
                        return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
×
2599
        }
2600

2601
        if (link->dhcp_server) {
30✔
2602
                r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
×
2603
                if (r < 0)
×
2604
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
×
2605
        }
2606

2607
        if (link->radv) {
30✔
2608
                r = sd_radv_set_ifname(link->radv, link->ifname);
×
2609
                if (r < 0)
×
2610
                        return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
×
2611
        }
2612

2613
        if (link->lldp_rx) {
30✔
2614
                r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
2✔
2615
                if (r < 0)
2✔
2616
                        return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
×
2617
        }
2618

2619
        if (link->lldp_tx) {
30✔
2620
                r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
×
2621
                if (r < 0)
×
2622
                        return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
×
2623
        }
2624

2625
        if (link->ipv4ll) {
30✔
2626
                r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
×
2627
                if (r < 0)
×
2628
                        return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
×
2629
        }
2630

2631
        r = ipv4acd_set_ifname(link);
30✔
2632
        if (r < 0)
30✔
2633
                return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
×
2634

2635
        return 1; /* needs reconfigure */
2636
}
2637

2638
static int link_update(Link *link, sd_netlink_message *message) {
10,730✔
2639
        bool needs_reconfigure = false;
10,730✔
2640
        int r;
10,730✔
2641

2642
        assert(link);
10,730✔
2643
        assert(message);
10,730✔
2644

2645
        r = link_update_name(link, message);
10,730✔
2646
        if (r < 0)
10,730✔
2647
                return r;
2648
        needs_reconfigure = needs_reconfigure || r > 0;
10,725✔
2649

2650
        r = link_update_alternative_names(link, message);
10,725✔
2651
        if (r < 0)
10,725✔
2652
                return r;
2653
        needs_reconfigure = needs_reconfigure || r > 0;
10,725✔
2654

2655
        r = link_update_mtu(link, message);
10,725✔
2656
        if (r < 0)
10,725✔
2657
                return r;
2658

2659
        r = link_update_driver(link, message);
10,725✔
2660
        if (r < 0)
10,725✔
2661
                return r;
2662
        needs_reconfigure = needs_reconfigure || r > 0;
10,725✔
2663

2664
        r = link_update_permanent_hardware_address(link, message);
10,725✔
2665
        if (r < 0)
10,725✔
2666
                return r;
2667
        needs_reconfigure = needs_reconfigure || r > 0;
10,725✔
2668

2669
        r = link_update_hardware_address(link, message);
10,725✔
2670
        if (r < 0)
10,725✔
2671
                return r;
2672
        needs_reconfigure = needs_reconfigure || r > 0;
10,725✔
2673

2674
        r = link_update_master(link, message);
10,725✔
2675
        if (r < 0)
10,725✔
2676
                return r;
2677

2678
        r = link_update_ipv6ll_addrgen_mode(link, message);
10,725✔
2679
        if (r < 0)
10,725✔
2680
                return r;
2681

2682
        r = link_update_flags(link, message);
10,725✔
2683
        if (r < 0)
10,725✔
2684
                return r;
2685

2686
        r = link_update_bridge_vlan(link, message);
10,725✔
2687
        if (r < 0)
10,725✔
2688
                return r;
2689

2690
        return needs_reconfigure;
10,725✔
2691
}
2692

2693
static Link *link_drop_or_unref(Link *link) {
×
2694
        if (!link)
×
2695
                return NULL;
2696
        if (!link->manager)
×
2697
                return link_unref(link);
×
2698
        return link_drop(link);
×
2699
}
2700

2701
DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2,838✔
2702

2703
static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
2,838✔
2704
        _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL;
2,838✔
2705
        _cleanup_(link_drop_or_unrefp) Link *link = NULL;
2,838✔
2706
        unsigned short iftype;
2,838✔
2707
        int r, ifindex;
2,838✔
2708

2709
        assert(manager);
2,838✔
2710
        assert(message);
2,838✔
2711
        assert(ret);
2,838✔
2712

2713
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2,838✔
2714
        if (r < 0)
2,838✔
2715
                return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
×
2716
        else if (ifindex <= 0)
2,838✔
2717
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
×
2718

2719
        r = sd_rtnl_message_link_get_type(message, &iftype);
2,838✔
2720
        if (r < 0)
2,838✔
2721
                return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
×
2722

2723
        r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2,838✔
2724
        if (r < 0)
2,838✔
2725
                return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
×
2726

2727
        /* check for link kind */
2728
        r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2,838✔
2729
        if (r >= 0) {
2,838✔
2730
                r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2,295✔
2731
                if (r < 0 && r != -ENODATA)
2,295✔
2732
                        return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
×
2733
                r = sd_netlink_message_exit_container(message);
2,295✔
2734
                if (r < 0)
2,295✔
2735
                        return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
×
2736
        }
2737

2738
        if (!manager->test_mode) {
2,838✔
2739
                /* Do not update state files when running in test mode. */
2740
                if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2,837✔
2741
                        return log_oom_debug();
×
2742

2743
                if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2,837✔
2744
                        return log_oom_debug();
×
2745
        }
2746

2747
        link = new(Link, 1);
2,838✔
2748
        if (!link)
2,838✔
2749
                return -ENOMEM;
2750

2751
        *link = (Link) {
2,838✔
2752
                .n_ref = 1,
2753
                .state = LINK_STATE_PENDING,
2754
                .online_state = _LINK_ONLINE_STATE_INVALID,
2755
                .automatic_reconfigure_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_SEC, .burst = 5 },
2756
                .ifindex = ifindex,
2757
                .iftype = iftype,
2758
                .ifname = TAKE_PTR(ifname),
2,838✔
2759
                .kind = TAKE_PTR(kind),
2,838✔
2760

2761
                .bridge_vlan_pvid = UINT16_MAX,
2762

2763
                .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2764

2765
                .state_file = TAKE_PTR(state_file),
2,838✔
2766
                .lease_file = TAKE_PTR(lease_file),
2,838✔
2767

2768
                .n_dns = UINT_MAX,
2769
                .dns_default_route = -1,
2770
                .llmnr = _RESOLVE_SUPPORT_INVALID,
2771
                .mdns = _RESOLVE_SUPPORT_INVALID,
2772
                .dnssec_mode = _DNSSEC_MODE_INVALID,
2773
                .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2774
        };
2775

2776
        r = hashmap_ensure_put(&manager->links_by_index, &link_hash_ops, INT_TO_PTR(link->ifindex), link);
2,838✔
2777
        if (r < 0)
2,838✔
2778
                return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
×
2779

2780
        link->manager = manager;
2,838✔
2781

2782
        r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
2,838✔
2783
        if (r < 0)
2,838✔
2784
                return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
×
2785

2786
        log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
3,381✔
2787
                       link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2788

2789
        /* If contained in this set, the link is wireless and the corresponding NL80211_CMD_NEW_INTERFACE
2790
         * message arrived too early. Request the wireless link information again.
2791
         */
2792
        if (set_remove(manager->new_wlan_ifindices, INT_TO_PTR(link->ifindex))) {
2,838✔
2793
                r = link_get_wlan_interface(link);
×
2794
                if (r < 0)
×
2795
                        log_link_warning_errno(link, r, "Failed to get wireless interface, ignoring: %m");
×
2796
        }
2797

2798
        *ret = TAKE_PTR(link);
2,838✔
2799
        return 0;
2,838✔
2800
}
2801

2802
int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
6,630✔
2803
        Link *link = NULL;
6,630✔
2804
        NetDev *netdev = NULL;
6,630✔
2805
        uint16_t type;
6,630✔
2806
        const char *name;
6,630✔
2807
        int r, ifindex;
6,630✔
2808

2809
        assert(rtnl);
6,630✔
2810
        assert(message);
6,630✔
2811
        assert(manager);
6,630✔
2812

2813
        if (sd_netlink_message_is_error(message)) {
6,630✔
2814
                r = sd_netlink_message_get_errno(message);
×
2815
                if (r < 0)
×
2816
                        log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
×
2817

2818
                return 0;
×
2819
        }
2820

2821
        r = sd_netlink_message_get_type(message, &type);
6,630✔
2822
        if (r < 0) {
6,630✔
2823
                log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
×
2824
                return 0;
×
2825
        } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
6,630✔
2826
                log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
×
2827
                return 0;
×
2828
        }
2829

2830
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
6,630✔
2831
        if (r < 0) {
6,630✔
2832
                log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
×
2833
                return 0;
×
2834
        } else if (ifindex <= 0) {
6,630✔
2835
                log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
×
2836
                return 0;
×
2837
        }
2838

2839
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
6,630✔
2840
        if (r < 0) {
6,630✔
2841
                log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
×
2842
                return 0;
×
2843
        }
2844

2845
        (void) link_get_by_index(manager, ifindex, &link);
6,630✔
2846
        (void) netdev_get(manager, name, &netdev);
6,630✔
2847

2848
        switch (type) {
6,630✔
2849
        case RTM_NEWLINK:
5,740✔
2850
                if (netdev) {
5,740✔
2851
                        /* netdev exists, so make sure the ifindex matches */
2852
                        r = netdev_set_ifindex(netdev, message);
2,911✔
2853
                        if (r < 0) {
2,911✔
2854
                                log_netdev_warning_errno(netdev, r, "Could not process new link message for netdev, ignoring: %m");
×
2855
                                netdev_enter_failed(netdev);
×
2856
                                return 0;
×
2857
                        }
2858
                }
2859

2860
                if (!link) {
5,740✔
2861
                        /* link is new, so add it */
2862
                        r = link_new(manager, message, &link);
2,838✔
2863
                        if (r < 0) {
2,838✔
2864
                                log_warning_errno(r, "Could not process new link message: %m");
×
2865
                                return 0;
×
2866
                        }
2867

2868
                        r = link_update(link, message);
2,838✔
2869
                        if (r < 0) {
2,838✔
2870
                                log_link_warning_errno(link, r, "Could not process link message: %m");
×
2871
                                link_enter_failed(link);
×
2872
                                return 0;
×
2873
                        }
2874

2875
                        if (link->manager->test_mode) {
2,838✔
2876
                                log_link_debug(link, "Running in test mode, refusing to enter initialized state.");
1✔
2877
                                link_set_state(link, LINK_STATE_UNMANAGED);
1✔
2878
                                return 0;
1✔
2879
                        }
2880

2881
                        /* Do not enter initialized state if we are enumerating. */
2882
                        if (manager->enumerating)
2,837✔
2883
                                return 0;
2884

2885
                        r = link_check_initialized(link);
941✔
2886
                        if (r < 0) {
941✔
2887
                                log_link_warning_errno(link, r, "Failed to check link is initialized: %m");
×
2888
                                link_enter_failed(link);
×
2889
                                return 0;
×
2890
                        }
2891
                } else {
2892
                        r = link_update(link, message);
2,902✔
2893
                        if (r < 0) {
2,902✔
2894
                                log_link_warning_errno(link, r, "Could not process link message: %m");
5✔
2895
                                link_enter_failed(link);
5✔
2896
                                return 0;
5✔
2897
                        }
2898
                        if (r == 0)
2,897✔
2899
                                return 0;
2900

2901
                        if (link->manager->test_mode) {
235✔
2902
                                log_link_debug(link, "Running in test mode, refusing to configure interface.");
×
2903
                                link_set_state(link, LINK_STATE_UNMANAGED);
×
2904
                                return 0;
×
2905
                        }
2906

2907
                        /* Do not configure interface if we are enumerating. */
2908
                        if (manager->enumerating)
235✔
2909
                                return 0;
2910

2911
                        r = link_reconfigure_impl(link, /* flags= */ 0);
235✔
2912
                        if (r < 0) {
235✔
2913
                                log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
2914
                                link_enter_failed(link);
×
2915
                                return 0;
×
2916
                        }
2917
                }
2918
                break;
2919

2920
        case RTM_DELLINK:
890✔
2921
                link_drop(link);
890✔
2922
                netdev_drop(netdev);
890✔
2923
                break;
2924

2925
        default:
×
2926
                assert_not_reached();
×
2927
        }
2928

2929
        return 1;
2930
}
2931

2932
int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
4,997✔
2933
        uint16_t message_type;
4,997✔
2934
        int r;
4,997✔
2935

2936
        assert(m);
4,997✔
2937
        assert(link);
4,997✔
2938
        assert(error_msg);
4,997✔
2939

2940
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
4,997✔
2941
                return 0;
4,997✔
2942

2943
        r = sd_netlink_message_get_errno(m);
4,990✔
2944
        if (r < 0) {
4,990✔
2945
                log_link_message_warning_errno(link, m, r, "%s", error_msg);
×
2946
                link_enter_failed(link);
×
2947
                return 0;
×
2948
        }
2949

2950
        r = sd_netlink_message_get_type(m, &message_type);
4,990✔
2951
        if (r < 0) {
4,990✔
2952
                log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
×
2953
                return 0;
×
2954
        }
2955
        if (message_type != RTM_NEWLINK) {
4,990✔
2956
                log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
×
2957
                return 0;
×
2958
        }
2959

2960
        r = link_update(link, m);
4,990✔
2961
        if (r < 0) {
4,990✔
2962
                link_enter_failed(link);
×
2963
                return 0;
×
2964
        }
2965

2966
        return 1;
2967
}
2968

2969
int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
3,633✔
2970
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
3,633✔
2971
        int r;
3,633✔
2972

2973
        assert(link);
3,633✔
2974
        assert(link->manager);
3,633✔
2975
        assert(link->manager->rtnl);
3,633✔
2976
        assert(callback);
3,633✔
2977

2978
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
3,633✔
2979
        if (r < 0)
3,633✔
2980
                return r;
2981

2982
        r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
3,633✔
2983
                               link_netlink_destroy_callback, link);
2984
        if (r < 0)
3,633✔
2985
                return r;
2986

2987
        link_ref(link);
3,633✔
2988
        return 0;
2989
}
2990

2991
static const char* const link_state_table[_LINK_STATE_MAX] = {
2992
        [LINK_STATE_PENDING]     = "pending",
2993
        [LINK_STATE_INITIALIZED] = "initialized",
2994
        [LINK_STATE_CONFIGURING] = "configuring",
2995
        [LINK_STATE_CONFIGURED]  = "configured",
2996
        [LINK_STATE_UNMANAGED]   = "unmanaged",
2997
        [LINK_STATE_FAILED]      = "failed",
2998
        [LINK_STATE_LINGER]      = "linger",
2999
};
3000

3001
DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
33,378✔
3002

3003
int link_flags_to_string_alloc(uint32_t flags, char **ret) {
594✔
3004
        _cleanup_free_ char *str = NULL;
594✔
3005
        static const char* map[] = {
594✔
3006
                [LOG2U(IFF_UP)]          = "up",             /* interface is up. */
3007
                [LOG2U(IFF_BROADCAST)]   = "broadcast",      /* broadcast address valid. */
3008
                [LOG2U(IFF_DEBUG)]       = "debug",          /* turn on debugging. */
3009
                [LOG2U(IFF_LOOPBACK)]    = "loopback",       /* interface is a loopback net. */
3010
                [LOG2U(IFF_POINTOPOINT)] = "point-to-point", /* interface has p-p link. */
3011
                [LOG2U(IFF_NOTRAILERS)]  = "no-trailers",    /* avoid use of trailers. */
3012
                [LOG2U(IFF_RUNNING)]     = "running",        /* interface RFC2863 OPER_UP. */
3013
                [LOG2U(IFF_NOARP)]       = "no-arp",         /* no ARP protocol. */
3014
                [LOG2U(IFF_PROMISC)]     = "promiscuous",    /* receive all packets. */
3015
                [LOG2U(IFF_ALLMULTI)]    = "all-multicast",  /* receive all multicast packets. */
3016
                [LOG2U(IFF_MASTER)]      = "master",         /* master of a load balancer. */
3017
                [LOG2U(IFF_SLAVE)]       = "slave",          /* slave of a load balancer. */
3018
                [LOG2U(IFF_MULTICAST)]   = "multicast",      /* supports multicast. */
3019
                [LOG2U(IFF_PORTSEL)]     = "portsel",        /* can set media type. */
3020
                [LOG2U(IFF_AUTOMEDIA)]   = "auto-media",     /* auto media select active. */
3021
                [LOG2U(IFF_DYNAMIC)]     = "dynamic",        /* dialup device with changing addresses. */
3022
                [LOG2U(IFF_LOWER_UP)]    = "lower-up",       /* driver signals L1 up. */
3023
                [LOG2U(IFF_DORMANT)]     = "dormant",        /* driver signals dormant. */
3024
                [LOG2U(IFF_ECHO)]        = "echo",           /* echo sent packets. */
3025
        };
3026

3027
        assert(ret);
594✔
3028

3029
        for (size_t i = 0; i < ELEMENTSOF(map); i++)
11,880✔
3030
                if (BIT_SET(flags, i) && map[i])
11,286✔
3031
                        if (!strextend_with_separator(&str, ",", map[i]))
1,679✔
3032
                                return -ENOMEM;
3033

3034
        *ret = TAKE_PTR(str);
594✔
3035
        return 0;
594✔
3036
}
3037

3038
static const char * const kernel_operstate_table[] = {
3039
        [IF_OPER_UNKNOWN]        = "unknown",
3040
        [IF_OPER_NOTPRESENT]     = "not-present",
3041
        [IF_OPER_DOWN]           = "down",
3042
        [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
3043
        [IF_OPER_TESTING]        = "testing",
3044
        [IF_OPER_DORMANT]        = "dormant",
3045
        [IF_OPER_UP]             = "up",
3046
};
3047

3048
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);
594✔
3049

3050
bool link_has_local_lease_domain(Link *link) {
14,061✔
3051
        assert(link);
14,061✔
3052

3053
        return link->dhcp_server &&
14,235✔
3054
                link->network &&
174✔
3055
                link->network->dhcp_server_local_lease_domain &&
14,252✔
3056
                !dns_name_is_root(link->network->dhcp_server_local_lease_domain);
17✔
3057
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc