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

systemd / systemd / 21192089104

20 Jan 2026 11:35PM UTC coverage: 72.524% (-0.3%) from 72.818%
21192089104

push

github

yuwata
mkdir: reset mtime *after* fchown()

Follow-up for 34c3d5747

Also, drop pointless shortcut.

1 of 2 new or added lines in 1 file covered. (50.0%)

2960 existing lines in 48 files now uncovered.

309808 of 427181 relevant lines covered (72.52%)

1236537.64 hits per line

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

85.27
/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 "ordered-set.h"
67
#include "parse-util.h"
68
#include "set.h"
69
#include "socket-util.h"
70
#include "string-table.h"
71
#include "string-util.h"
72
#include "strv.h"
73
#include "tc.h"
74
#include "udev-util.h"
75

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

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

105
AddressFamily link_required_family_for_online(Link *link) {
9,809✔
106
        assert(link);
9,809✔
107

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

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

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

120
        return ADDRESS_FAMILY_NO;
121
}
122

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

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

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

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

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

138
        if (link_ipv6ll_enabled(link))
895✔
139
                return true;
140

141
        if (network_has_static_ipv6_configurations(link->network))
110✔
142
                return true;
44✔
143

144
        return false;
145
}
146

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

150
        assert(link);
304✔
151

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

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

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

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

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

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

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

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

182
        if (!link->network->configure_without_carrier) {
63,918✔
183
                if (link->set_flags_messages > 0)
63,741✔
184
                        return false;
185

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

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

193
        if (!link->activated)
12,995✔
194
                return false;
64✔
195

196
        return true;
197
}
198

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

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

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

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

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

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

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

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

234
        link->dnssec_negative_trust_anchors = set_free(link->dnssec_negative_trust_anchors);
2,826✔
235
}
2,826✔
236

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

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

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

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

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

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

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

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

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

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

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

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

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

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

279
        link_free_engines(link);
2,809✔
280

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

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

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

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

298
        set_free(link->slaves);
2,809✔
299

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

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

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

308
DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
185,439✔
309

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

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

318
        assert(m);
71,458✔
319

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

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

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

332
int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
1,921✔
333
        Link *link;
1,921✔
334

335
        assert(m);
1,921✔
336
        assert(ifname);
1,921✔
337

338
        link = hashmap_get(m->links_by_name, ifname);
1,921✔
339
        if (!link)
1,921✔
340
                return -ENODEV;
341

342
        if (ret)
1,847✔
343
                *ret = link;
1,847✔
344
        return 0;
345
}
346

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

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

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

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

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

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

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

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

376
        if (link->state == state)
9,332✔
377
                return;
1,814✔
378

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

383
        link->state = state;
7,518✔
384

385
        link_send_changed(link, "AdministrativeState");
7,518✔
386
        link_dirty(link);
7,518✔
387
}
388

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

392
        assert(link);
4,640✔
393
        assert(link->manager);
4,640✔
394
        assert(link->manager->event);
4,640✔
395

396
        bool keep_dynamic =
13,875✔
397
                may_keep_dynamic &&
1,945✔
398
                link->network &&
4,640✔
399
                (link->manager->state == MANAGER_RESTARTING ||
57✔
400
                 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC_ON_STOP));
14✔
401

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

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

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

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

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

423
                ndisc_flush(link);
4,595✔
424
        }
425

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

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

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

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

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

446
        return ret;
4,640✔
447
}
448

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

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

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

457
        link_set_state(link, LINK_STATE_FAILED);
5✔
458

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

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

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

472
void link_check_ready(Link *link) {
26,283✔
473
        Address *a;
26,283✔
474

475
        assert(link);
26,283✔
476

477
        if (link->state == LINK_STATE_CONFIGURED)
26,283✔
478
                return;
25,394✔
479

480
        if (link->state != LINK_STATE_CONFIGURING)
22,297✔
481
                return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
1,345✔
482

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

486
        if (!link->tc_configured)
20,952✔
487
                return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
316✔
488

489
        if (link->set_link_messages > 0)
20,636✔
490
                return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
2,335✔
491

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

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

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

502
        if (!link->static_addresses_configured)
6,946✔
503
                return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
3,405✔
504

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

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

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

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

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

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

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

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

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

532
        /* IPv6LL is assigned after the link gains its carrier. */
533
        if (!link->network->configure_without_carrier &&
6,610✔
534
            link_ipv6ll_enabled(link) &&
3,299✔
535
            !in6_addr_is_set(&link->ipv6ll_address))
3,176✔
536
                return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__);
1,236✔
537

538
        /* All static addresses must be ready. */
539
        bool has_static_address = false;
2,075✔
540
        SET_FOREACH(a, link->addresses) {
7,270✔
541
                if (a->source != NETWORK_CONFIG_SOURCE_STATIC)
5,371✔
542
                        continue;
3,274✔
543
                if (!address_is_ready(a))
2,097✔
544
                        return (void) log_link_debug(link, "%s(): static address %s is not ready.", __func__,
176✔
545
                                                     IN_ADDR_PREFIX_TO_STRING(a->family, &a->in_addr, a->prefixlen));
546
                has_static_address = true;
547
        }
548

549
        /* If at least one static address is requested, do not request that dynamic addressing protocols are finished. */
550
        if (has_static_address)
1,899✔
551
                goto ready;
397✔
552

553
        /* If no dynamic addressing protocol enabled, assume the interface is ready.
554
         * Note, ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
555
        if (!link_ipv4ll_enabled(link) && !link_dhcp4_enabled(link) &&
2,881✔
556
            !link_dhcp6_enabled(link) && !link_dhcp_pd_is_enabled(link) &&
809✔
557
            (link->network->configure_without_carrier || !link_ndisc_enabled(link)))
599✔
558
                goto ready;
258✔
559

560
        bool ipv4ll_ready =
2,488✔
561
                link_ipv4ll_enabled(link) && link->ipv4ll_address_configured &&
1,298✔
562
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_IPV4LL);
54✔
563
        bool dhcp4_ready =
1,244✔
564
                link_dhcp4_enabled(link) && link->dhcp4_configured &&
1,338✔
565
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP4);
94✔
566
        bool dhcp6_ready =
1,244✔
567
                link_dhcp6_enabled(link) && link->dhcp6_configured &&
1,244✔
568
                (!link->network->dhcp6_use_address ||
60✔
569
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP6));
29✔
570
        bool dhcp_pd_ready =
2,488✔
571
                link_dhcp_pd_is_enabled(link) && link->dhcp_pd_configured &&
1,244✔
572
                (!link->network->dhcp_pd_assign ||
127✔
573
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP_PD));
60✔
574
        bool ndisc_ready =
2,488✔
575
                link_ndisc_enabled(link) && link->ndisc_configured &&
1,244✔
576
                (!link->network->ndisc_use_autonomous_prefix ||
428✔
577
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_NDISC));
214✔
578

579
        /* If the uplink for PD is self, then request the corresponding DHCP protocol is also ready. */
580
        if (dhcp_pd_is_uplink(link, link, /* accept_auto= */ false)) {
1,244✔
581
                if (link_dhcp4_enabled(link) && link->network->dhcp_use_6rd &&
81✔
582
                    sd_dhcp_lease_has_6rd(link->dhcp_lease)) {
26✔
583
                        if (!link->dhcp4_configured)
23✔
584
                                return (void) log_link_debug(link, "%s(): DHCPv4 6rd prefix is assigned, but DHCPv4 protocol is not finished yet.", __func__);
10✔
585
                        if (!dhcp_pd_ready)
13✔
586
                                return (void) log_link_debug(link, "%s(): DHCPv4 is finished, but prefix acquired by DHCPv4-6rd is not assigned yet.", __func__);
11✔
587
                }
588

589
                if (link_dhcp6_enabled(link) && link->network->dhcp6_use_pd_prefix &&
63✔
590
                    sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease)) {
29✔
591
                        if (!link->dhcp6_configured)
20✔
592
                                return (void) log_link_debug(link, "%s(): DHCPv6 IA_PD prefix is assigned, but DHCPv6 protocol is not finished yet.", __func__);
8✔
593
                        if (!dhcp_pd_ready)
12✔
594
                                return (void) log_link_debug(link, "%s(): DHCPv6 is finished, but prefix acquired by DHCPv6 IA_PD is not assigned yet.", __func__);
9✔
595
                }
596
        }
597

598
        /* At least one dynamic addressing protocol is finished. */
599
        if (!ipv4ll_ready && !dhcp4_ready && !dhcp6_ready && !dhcp_pd_ready && !ndisc_ready)
1,206✔
600
                return (void) log_link_debug(link, "%s(): dynamic addressing protocols are enabled but none of them finished yet.", __func__);
978✔
601

602
        log_link_debug(link, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
1,181✔
603
                       __func__,
604
                       yes_no(ipv4ll_ready),
605
                       yes_no(dhcp4_ready),
606
                       yes_no(dhcp6_ready),
607
                       yes_no(dhcp_pd_ready),
608
                       yes_no(ndisc_ready));
609

610
ready:
889✔
611
        link_set_state(link, LINK_STATE_CONFIGURED);
889✔
612
}
613

614
static int link_request_static_configs(Link *link) {
1,546✔
615
        int r;
1,546✔
616

617
        assert(link);
1,546✔
618
        assert(link->network);
1,546✔
619
        assert(link->state != _LINK_STATE_INVALID);
1,546✔
620

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

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

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

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

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

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

645
        r = link_request_static_nexthops(link, false);
1,546✔
646
        if (r < 0)
1,546✔
647
                return r;
648

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

653
        r = link_request_static_routing_policy_rules(link);
1,546✔
654
        if (r < 0)
1,546✔
655
                return r;
×
656

657
        return 0;
658
}
659

660
int link_request_stacked_netdevs(Link *link, NetDevLocalAddressType type) {
11,852✔
661
        NetDev *netdev;
11,852✔
662
        int r;
11,852✔
663

664
        assert(link);
11,852✔
665

666
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
11,852✔
667
                return 0;
11,852✔
668

669
        assert(link->network);
2,649✔
670

671
        link->stacked_netdevs_created = false;
2,649✔
672

673
        HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
3,160✔
674
                if (!netdev_needs_reconfigure(netdev, type))
511✔
675
                        continue;
296✔
676

677
                r = link_request_stacked_netdev(link, netdev);
215✔
678
                if (r < 0)
215✔
679
                        return r;
×
680
        }
681

682
        if (link->create_stacked_netdev_messages == 0) {
2,649✔
683
                link->stacked_netdevs_created = true;
2,475✔
684
                link_check_ready(link);
2,475✔
685
        }
686

687
        return 0;
688
}
689

690
static int link_acquire_dynamic_ipv6_conf(Link *link) {
793✔
691
        int r;
793✔
692

693
        assert(link);
793✔
694

695
        r = radv_start(link);
793✔
696
        if (r < 0)
793✔
697
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
×
698

699
        r = ndisc_start(link);
793✔
700
        if (r < 0)
793✔
701
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
×
702

703
        r = dhcp6_start(link);
793✔
704
        if (r < 0)
793✔
705
                return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m");
×
706

707
        return 0;
708
}
709

710
static int link_acquire_dynamic_ipv4_conf(Link *link) {
943✔
711
        int r;
943✔
712

713
        assert(link);
943✔
714
        assert(link->manager);
943✔
715
        assert(link->manager->event);
943✔
716

717
        if (link->dhcp_client) {
943✔
UNCOV
718
                r = dhcp4_start(link);
×
UNCOV
719
                if (r < 0)
×
UNCOV
720
                        return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
×
721

722
                log_link_debug(link, "Acquiring DHCPv4 lease.");
×
723

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

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

738
        r = link_start_dhcp4_server(link);
943✔
739
        if (r < 0)
943✔
740
                return log_link_warning_errno(link, r, "Could not start DHCP server: %m");
×
741

742
        r = ipv4acd_start(link);
943✔
743
        if (r < 0)
943✔
744
                return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m");
×
745

746
        return 0;
747
}
748

749
static int link_acquire_dynamic_conf(Link *link) {
943✔
750
        int r;
943✔
751

752
        assert(link);
943✔
753
        assert(link->network);
943✔
754

755
        r = link_acquire_dynamic_ipv4_conf(link);
943✔
756
        if (r < 0)
943✔
757
                return r;
758

759
        if (in6_addr_is_set(&link->ipv6ll_address)) {
943✔
760
                r = link_acquire_dynamic_ipv6_conf(link);
273✔
761
                if (r < 0)
273✔
762
                        return r;
763
        }
764

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

775
        if (link->lldp_tx) {
943✔
776
                r = sd_lldp_tx_start(link->lldp_tx);
7✔
777
                if (r < 0)
7✔
778
                        return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
×
779
        }
780

781
        if (link->lldp_rx) {
943✔
782
                r = sd_lldp_rx_start(link->lldp_rx);
825✔
783
                if (r < 0)
825✔
784
                        return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
1✔
785
        }
786

787
        return 0;
788
}
789

790
int link_ipv6ll_gained(Link *link) {
637✔
791
        int r;
637✔
792

793
        assert(link);
637✔
794

795
        log_link_info(link, "Gained IPv6LL");
637✔
796

797
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
637✔
798
                return 0;
799

800
        r = link_acquire_dynamic_ipv6_conf(link);
520✔
801
        if (r < 0)
520✔
802
                return r;
803

804
        r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_IPV6LL);
520✔
805
        if (r < 0)
520✔
806
                return r;
807

808
        link_check_ready(link);
520✔
809
        return 0;
520✔
810
}
811

812
int link_handle_bound_to_list(Link *link) {
34✔
813
        bool required_up = false;
34✔
814
        bool link_is_up = false;
34✔
815
        Link *l;
34✔
816

817
        assert(link);
34✔
818

819
        /* If at least one interface in bound_to_links has carrier, then make this interface up.
820
         * If all interfaces in bound_to_links do not, then make this interface down. */
821

822
        if (hashmap_isempty(link->bound_to_links))
34✔
823
                return 0;
34✔
824

825
        if (link->flags & IFF_UP)
30✔
826
                link_is_up = true;
16✔
827

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

834
        if (!required_up && link_is_up)
30✔
835
                return link_request_to_bring_up_or_down(link, /* up= */ false);
3✔
836
        if (required_up && !link_is_up)
27✔
837
                return link_request_to_bring_up_or_down(link, /* up= */ true);
9✔
838

839
        return 0;
840
}
841

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

846
        assert(link);
4,629✔
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,651✔
851
                r = link_handle_bound_to_list(l);
22✔
852
                if (r < 0)
22✔
853
                        return r;
×
854
        }
855

856
        return 0;
4,629✔
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_get(*h, INT_TO_PTR(carrier->ifindex)))
30✔
869
                return 0;
870

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

875
        link_dirty(link);
24✔
876

877
        return 0;
24✔
878
}
879

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

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

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

890
        HASHMAP_FOREACH(carrier, m->links_by_index) {
18,469✔
891
                if (!carrier->network)
15,835✔
892
                        continue;
14,998✔
893

894
                if (strv_isempty(carrier->network->bind_carrier))
837✔
895
                        continue;
828✔
896

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

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

910
        return 0;
2,634✔
911
}
912

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

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

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

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

927
        m = link->manager;
7✔
928

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

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

943
        return 0;
7✔
944
}
945

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

950
        assert(link);
3,707✔
951

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

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

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

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

967
        assert(link);
863✔
968

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

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

978
        if (updated)
863✔
979
                link_dirty(link);
5✔
980
}
863✔
981

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

986
        assert(link);
10,651✔
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,651✔
991
                return 0;
10,651✔
992

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

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

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

1004
        assert(link);
918✔
1005

1006
        if (!link->manager)
918✔
1007
                return;
875✔
1008

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

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

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

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

1022
        ORDERED_SET_FOREACH(req, link->manager->request_queue) {
6,011✔
1023
                if (req->link != link)
2,304✔
1024
                        continue;
2,223✔
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)
81✔
1030
                        switch (req->type) {
2✔
1031
                        case REQUEST_TYPE_ADDRESS: {
1✔
1032
                                Address *address = ASSERT_PTR(req->userdata);
1✔
1033

1034
                                if (address_get(link, address, NULL) < 0)
1✔
1035
                                        RET_GATHER(ret, address_remove(address, link));
1✔
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: {
1✔
1053
                                Route *route = ASSERT_PTR(req->userdata);
1✔
1054

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

1063
                request_detach(req);
81✔
1064
        }
1065

1066
        return ret;
3,707✔
1067
}
1068

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

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

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

1077
        link_set_state(link, LINK_STATE_LINGER);
863✔
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);
863✔
1083

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

1087
        link_clear_sr_iov_ifindices(link);
863✔
1088

1089
        link_drop_from_master(link);
863✔
1090

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

1094
        link_clean(link);
863✔
1095

1096
        STRV_FOREACH(n, link->alternative_names)
900✔
1097
                hashmap_remove(link->manager->links_by_name, *n);
37✔
1098
        hashmap_remove(link->manager->links_by_name, link->ifname);
863✔
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);
863✔
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);
863✔
1105

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

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

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

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

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

1125
        return r;
862✔
1126
}
1127

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

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

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

1140
        return r;
2,657✔
1141
}
1142

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

1146
        assert(link);
824✔
1147
        assert(link->network);
824✔
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);
824✔
1153
        RET_GATHER(r, link_drop_radv_config(link, network)); /* Stop before dropping DHCP-PD prefixes. */
824✔
1154
        RET_GATHER(r, link_drop_ipv4ll_config(link, network)); /* Stop before DHCPv4 client. */
824✔
1155
        RET_GATHER(r, link_drop_dhcp4_config(link, network));
824✔
1156
        RET_GATHER(r, link_drop_dhcp6_config(link, network));
824✔
1157
        RET_GATHER(r, link_drop_dhcp_pd_config(link, network));
824✔
1158
        link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
824✔
1159
        link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx); /* TODO: keep the received neighbors. */
824✔
1160
        link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
824✔
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;
824✔
1166
        link->dhcp4_configured = false;
824✔
1167
        link->dhcp6_configured = false;
824✔
1168
        link->dhcp_pd_configured = false;
824✔
1169
        link->ndisc_configured = false;
824✔
1170

1171
        return r;
824✔
1172
}
1173

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

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

1181
        link_set_state(link, LINK_STATE_CONFIGURING);
862✔
1182

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

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

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

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

1199
        if (link->iftype == ARPHRD_CAN) {
862✔
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);
856✔
1209
        if (r < 0)
856✔
1210
                return r;
1211

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1296
        if (!link_has_carrier(link))
856✔
1297
                return 0;
1298

1299
        return link_acquire_dynamic_conf(link);
253✔
1300
}
1301

1302
static int link_get_network(Link *link, Network **ret) {
4,762✔
1303
        Network *network;
4,762✔
1304
        int r;
4,762✔
1305

1306
        assert(link);
4,762✔
1307
        assert(link->manager);
4,762✔
1308
        assert(ret);
4,762✔
1309

1310
        ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
19,750✔
1311
                bool warn = false;
18,670✔
1312

1313
                r = net_match_config(
37,340✔
1314
                                &network->match,
18,670✔
1315
                                link->dev,
1316
                                &link->hw_addr,
18,670✔
1317
                                &link->permanent_hw_addr,
18,670✔
1318
                                link->driver,
18,670✔
1319
                                link->iftype,
18,670✔
1320
                                link->kind,
18,670✔
1321
                                link->ifname,
18,670✔
1322
                                link->alternative_names,
18,670✔
1323
                                link->wlan_iftype,
1324
                                link->ssid,
18,670✔
1325
                                &link->bssid);
18,670✔
1326
                if (r < 0)
18,670✔
1327
                        return r;
3,682✔
1328
                if (r == 0)
18,670✔
1329
                        continue;
14,988✔
1330

1331
                if (network->match.ifname && link->dev) {
3,682✔
1332
                        uint8_t name_assign_type = NET_NAME_UNKNOWN;
3,655✔
1333
                        const char *attr;
3,655✔
1334

1335
                        if (sd_device_get_sysattr_value(link->dev, "name_assign_type", &attr) >= 0)
3,655✔
1336
                                (void) safe_atou8(attr, &name_assign_type);
2,134✔
1337

1338
                        warn = name_assign_type == NET_NAME_ENUM;
3,655✔
1339
                }
1340

1341
                log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
7,364✔
1342
                              "Found matching .network file%s: %s",
1343
                              warn ? ", based on potentially unpredictable interface name" : "",
1344
                              network->filename);
1345

1346
                if (network->unmanaged)
3,682✔
1347
                        return -ENOENT;
1348

1349
                *ret = network;
1,813✔
1350
                return 0;
1,813✔
1351
        }
1352

1353
        return log_link_debug_errno(link, SYNTHETIC_ERRNO(ENOENT), "No matching .network found.");
1,080✔
1354
}
1355

1356
static void link_enter_unmanaged(Link *link) {
2,949✔
1357
        assert(link);
2,949✔
1358

1359
        if (link->state == LINK_STATE_UNMANAGED)
2,949✔
1360
                return;
1361

1362
        bool notify = link_has_local_lease_domain(link);
1,982✔
1363

1364
        log_link_full(link, link->state == LINK_STATE_INITIALIZED ? LOG_DEBUG : LOG_INFO,
1,988✔
1365
                      "Unmanaging interface.");
1366

1367
        (void) link_stop_engines(link, /* may_keep_dynamic= */ false);
1,982✔
1368
        (void) link_drop_requests(link);
1,982✔
1369
        (void) link_drop_static_config(link);
1,982✔
1370

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

1378
        link->network = network_unref(link->network);
1,982✔
1379
        link_set_state(link, LINK_STATE_UNMANAGED);
1,982✔
1380

1381
        if (notify)
1,982✔
1382
                manager_notify_hook_filters(link->manager);
×
1383
}
1384

1385
static int link_managed_by_us(Link *link) {
7,424✔
1386
        int r;
7,424✔
1387

1388
        assert(link);
7,424✔
1389

1390
        if (!link->dev)
7,424✔
1391
                return true;
7,424✔
1392

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

1400
        if (streq(s, "io.systemd.Network"))
42✔
1401
                return true;
1402

1403
        if (link->state == LINK_STATE_UNMANAGED)
3✔
1404
                return false; /* Already in unmanaged state */
1405

1406
        log_link_debug(link, "Interface is requested to be managed by '%s', unmanaging the interface.", s);
1✔
1407
        link_set_state(link, LINK_STATE_UNMANAGED);
1✔
1408
        return false;
1409
}
1410

1411
int link_reconfigure_impl(Link *link, LinkReconfigurationFlag flags) {
5,513✔
1412
        Network *network = NULL;
5,513✔
1413
        int r;
5,513✔
1414

1415
        assert(link);
5,513✔
1416
        assert(link->manager);
5,513✔
1417

1418
        link_assign_netdev(link);
5,513✔
1419

1420
        if (link->manager->state != MANAGER_RUNNING)
5,513✔
1421
                return 0;
5,513✔
1422

1423
        if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
5,513✔
1424
                return 0;
1425

1426
        r = link_managed_by_us(link);
4,764✔
1427
        if (r <= 0)
4,764✔
1428
                return r;
1429

1430
        r = link_get_network(link, &network);
4,762✔
1431
        if (r == -ENOENT) {
4,762✔
1432
                link_enter_unmanaged(link);
2,949✔
1433
                return 0;
2,949✔
1434
        }
1435
        if (r < 0)
1,813✔
1436
                return r;
1437

1438
        if (link->network == network && !FLAGS_SET(flags, LINK_RECONFIGURE_UNCONDITIONALLY))
1,813✔
1439
                return 0;
1440

1441
        _cleanup_free_ char *joined = strv_join(network->dropins, ", ");
1,724✔
1442
        if (link->network)
862✔
1443
                log_link_info(link, "Reconfiguring with %s%s%s%s.",
594✔
1444
                              network->filename,
1445
                              isempty(joined) ? "" : " (dropins: ",
1446
                              joined,
1447
                              isempty(joined) ? "" : ")");
1448
        else
1449
                log_link_info(link, "Configuring with %s%s%s%s.",
1,992✔
1450
                              network->filename,
1451
                              isempty(joined) ? "" : " (dropins: ",
1452
                              joined,
1453
                              isempty(joined) ? "" : ")");
1454

1455
        /* Dropping configurations based on the old .network file. */
1456
        r = link_drop_requests(link);
862✔
1457
        if (r < 0)
862✔
1458
                return r;
1459

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

1466
        _cleanup_(network_unrefp) Network *old_network = TAKE_PTR(link->network);
862✔
1467

1468
        /* Then, apply new .network file */
1469
        link->network = network_ref(network);
862✔
1470

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

1486
                /* Free DHCP client and friends. */
1487
                link_free_engines(link);
38✔
1488
        }
1489

1490
        link_update_operstate(link, true);
862✔
1491
        link_dirty(link);
862✔
1492

1493
        link_set_state(link, LINK_STATE_INITIALIZED);
862✔
1494
        link->activated = false;
862✔
1495

1496
        r = link_configure(link);
862✔
1497
        if (r < 0)
862✔
1498
                return r;
1✔
1499

1500
        return 1;
1501
}
1502

1503
typedef struct LinkReconfigurationData {
1504
        Manager *manager;
1505
        Link *link;
1506
        LinkReconfigurationFlag flags;
1507
        sd_bus_message *message;
1508
        unsigned *counter;
1509
} LinkReconfigurationData;
1510

1511
static LinkReconfigurationData* link_reconfiguration_data_free(LinkReconfigurationData *data) {
1,365✔
1512
        if (!data)
1,365✔
1513
                return NULL;
1514

1515
        link_unref(data->link);
1,365✔
1516
        sd_bus_message_unref(data->message);
1,365✔
1517

1518
        return mfree(data);
1,365✔
1519
}
1520

1521
DEFINE_TRIVIAL_CLEANUP_FUNC(LinkReconfigurationData*, link_reconfiguration_data_free);
1,365✔
1522

1523
static void link_reconfiguration_data_destroy_callback(LinkReconfigurationData *data) {
1,365✔
1524
        int r;
1,365✔
1525

1526
        assert(data);
1,365✔
1527

1528
        if (data->message) {
1,365✔
1529
                if (data->counter) {
1,259✔
1530
                        assert(*data->counter > 0);
1,221✔
1531
                        (*data->counter)--;
1,221✔
1532
                }
1533

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

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

1547
        link_reconfiguration_data_free(data);
1,365✔
1548
}
1,365✔
1549

1550
static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, LinkReconfigurationData *data) {
1,365✔
1551
        Link *link = ASSERT_PTR(ASSERT_PTR(data)->link);
1,365✔
1552
        int r;
1,365✔
1553

1554
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1,365✔
1555
        if (r <= 0)
1,365✔
1556
                return r;
1557

1558
        r = link_reconfigure_impl(link, data->flags);
1,360✔
1559
        if (r < 0) {
1,360✔
1560
                link_enter_failed(link);
×
1561
                return 0;
×
1562
        }
1563

1564
        return r;
1565
}
1566

1567
int link_reconfigure_full(Link *link, LinkReconfigurationFlag flags, sd_bus_message *message, unsigned *counter) {
1,365✔
1568
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1,365✔
1569
        _cleanup_(link_reconfiguration_data_freep) LinkReconfigurationData *data = NULL;
1,365✔
1570
        int r;
1,365✔
1571

1572
        assert(link);
1,365✔
1573
        assert(link->manager);
1,365✔
1574
        assert(link->manager->rtnl);
1,365✔
1575

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

1582
        data = new(LinkReconfigurationData, 1);
1,365✔
1583
        if (!data) {
1,365✔
1584
                r = -ENOMEM;
×
1585
                goto failed;
×
1586
        }
1587

1588
        *data = (LinkReconfigurationData) {
2,730✔
1589
                .manager = link->manager,
1,365✔
1590
                .link = link_ref(link),
1,365✔
1591
                .flags = flags,
1592
                .message = sd_bus_message_ref(message), /* message may be NULL, but _ref() works fine. */
1,365✔
1593
                .counter = counter,
1594
        };
1595

1596
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
1,365✔
1597
        if (r < 0)
1,365✔
1598
                goto failed;
×
1599

1600
        r = netlink_call_async(link->manager->rtnl, NULL, req,
1,365✔
1601
                               link_reconfigure_handler,
1602
                               link_reconfiguration_data_destroy_callback, data);
1603
        if (r < 0)
1,365✔
1604
                goto failed;
×
1605

1606
        TAKE_PTR(data);
1,365✔
1607
        if (counter)
1,365✔
1608
                (*counter)++;
1,221✔
1609

1610
        if (link->state == LINK_STATE_FAILED)
1,365✔
1611
                link_set_state(link, LINK_STATE_INITIALIZED);
5✔
1612

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

1615
failed:
1616
        log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
1617
        link_enter_failed(link);
×
1618
        return r;
1619
}
1620

1621
static int link_initialized_and_synced(Link *link) {
2,637✔
1622
        int r;
2,637✔
1623

1624
        assert(link);
2,637✔
1625
        assert(link->manager);
2,637✔
1626

1627
        if (link->state == LINK_STATE_PENDING) {
2,637✔
1628
                log_link_debug(link, "Link state is up-to-date");
2,634✔
1629
                link_set_state(link, LINK_STATE_INITIALIZED);
2,634✔
1630

1631
                r = link_new_bound_by_list(link);
2,634✔
1632
                if (r < 0)
2,634✔
1633
                        return r;
1634

1635
                r = link_handle_bound_by_list(link);
2,634✔
1636
                if (r < 0)
2,634✔
1637
                        return r;
1638
        }
1639

1640
        return link_reconfigure_impl(link, /* flags= */ 0);
2,637✔
1641
}
1642

1643
static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2,559✔
1644
        int r;
2,559✔
1645

1646
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
2,559✔
1647
        if (r <= 0)
2,559✔
1648
                return r;
1649

1650
        r = link_initialized_and_synced(link);
2,559✔
1651
        if (r < 0)
2,559✔
1652
                link_enter_failed(link);
1✔
1653

1654
        return 0;
1655
}
1656

1657
static int link_initialized(Link *link, sd_device *device) {
2,660✔
1658
        int r;
2,660✔
1659

1660
        assert(link);
2,660✔
1661
        assert(device);
2,660✔
1662

1663
        /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1664
         * or sysattrs) may be outdated. */
1665
        device_unref_and_replace(link->dev, device);
2,660✔
1666

1667
        r = link_managed_by_us(link);
2,660✔
1668
        if (r <= 0)
2,660✔
1669
                return r;
1670

1671
        if (link->dhcp_client) {
2,659✔
1672
                r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev);
×
1673
                if (r < 0)
×
1674
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv4 client, ignoring: %m");
×
1675
        }
1676

1677
        if (link->dhcp6_client) {
2,659✔
1678
                r = sd_dhcp6_client_attach_device(link->dhcp6_client, link->dev);
×
1679
                if (r < 0)
×
1680
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv6 client, ignoring: %m");
×
1681
        }
1682

1683
        r = link_set_sr_iov_ifindices(link);
2,659✔
1684
        if (r < 0)
2,659✔
1685
                log_link_warning_errno(link, r, "Failed to manage SR-IOV PF and VF ports, ignoring: %m");
×
1686

1687
        if (link->state != LINK_STATE_PENDING)
2,659✔
1688
                return link_reconfigure(link, /* flags= */ 0);
100✔
1689

1690
        log_link_debug(link, "udev initialized link");
2,559✔
1691

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

1697
        return link_call_getlink(link, link_initialized_handler);
2,559✔
1698
}
1699

1700
int link_check_initialized(Link *link) {
2,808✔
1701
        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
2,808✔
1702
        int r;
2,808✔
1703

1704
        assert(link);
2,808✔
1705

1706
        if (!udev_available())
2,808✔
1707
                return link_initialized_and_synced(link);
78✔
1708

1709
        /* udev should be around */
1710
        r = sd_device_new_from_ifindex(&device, link->ifindex);
2,730✔
1711
        if (r < 0) {
2,730✔
UNCOV
1712
                log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
×
UNCOV
1713
                return 0;
×
1714
        }
1715

1716
        r = device_is_processed(device);
2,730✔
1717
        if (r < 0)
2,730✔
1718
                return log_link_warning_errno(link, r, "Could not determine whether the device is processed by udevd: %m");
×
1719
        if (r == 0) {
2,730✔
1720
                /* not yet ready */
1721
                log_link_debug(link, "link pending udev initialization...");
961✔
1722
                return 0;
961✔
1723
        }
1724

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

1733
        return link_initialized(link, device);
1,769✔
1734
}
1735

1736
int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t action) {
1,949✔
1737
        int r, ifindex;
1,949✔
1738
        Link *link;
1,949✔
1739

1740
        assert(m);
1,949✔
1741
        assert(device);
1,949✔
1742

1743
        r = sd_device_get_ifindex(device, &ifindex);
1,949✔
1744
        if (r < 0)
1,949✔
1745
                return log_device_debug_errno(device, r, "Failed to get ifindex: %m");
×
1746

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

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

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

1772
        r = link_initialized(link, device);
891✔
1773
        if (r < 0)
891✔
1774
                link_enter_failed(link);
×
1775

1776
        return 0;
1777
}
1778

1779
static int link_carrier_gained(Link *link) {
1,262✔
1780
        LinkReconfigurationFlag flags = 0;
1,262✔
1781
        int r;
1,262✔
1782

1783
        assert(link);
1,262✔
1784

1785
        log_link_info(link, "Gained carrier");
1,262✔
1786

1787
        r = event_source_disable(link->carrier_lost_timer);
1,262✔
1788
        if (r < 0)
1,262✔
1789
                log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
×
1790

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

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

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

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

1832
        if (link->iftype == ARPHRD_CAN)
1,262✔
1833
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1834
                return 0;
1835

1836
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
1,258✔
1837
                r = link_acquire_dynamic_conf(link);
690✔
1838
                if (r < 0)
690✔
1839
                        return r;
1840

1841
                r = link_request_static_configs(link);
690✔
1842
                if (r < 0)
690✔
1843
                        return r;
×
1844
        }
1845

1846
        return 0;
1847
}
1848

1849
static int link_carrier_lost_impl(Link *link) {
733✔
1850
        int ret = 0;
733✔
1851

1852
        assert(link);
733✔
1853

1854
        link->previous_ssid = mfree(link->previous_ssid);
733✔
1855

1856
        ret = link_handle_bound_by_list(link);
733✔
1857

1858
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
733✔
1859
                return ret;
733✔
1860

1861
        if (!link->network)
733✔
1862
                return ret;
1863

1864
        RET_GATHER(ret, link_stop_engines(link, /* may_keep_dynamic= */ false));
675✔
1865
        RET_GATHER(ret, link_drop_static_config(link));
675✔
1866

1867
        return ret;
1868
}
1869

1870
static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
×
1871
        Link *link = ASSERT_PTR(userdata);
×
1872
        int r;
×
1873

1874
        r = link_carrier_lost_impl(link);
×
1875
        if (r < 0) {
×
1876
                log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
×
1877
                link_enter_failed(link);
×
1878
        }
1879

1880
        return 0;
×
1881
}
1882

1883
static int link_carrier_lost(Link *link) {
749✔
1884
        uint16_t dhcp_mtu;
749✔
1885
        usec_t usec;
749✔
1886

1887
        assert(link);
749✔
1888

1889
        log_link_info(link, "Lost carrier");
749✔
1890

1891
        if (link->iftype == ARPHRD_CAN)
749✔
1892
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1893
                usec = 0;
1894

1895
        else if (!link->network)
745✔
1896
                usec = 0;
1897

1898
        else if (link->network->ignore_carrier_loss_set)
687✔
1899
                /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1900
                usec = link->network->ignore_carrier_loss_usec;
12✔
1901

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

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

1916
        else
1917
                /* Otherwise, use the implied default value. */
1918
                usec = link->network->ignore_carrier_loss_usec;
674✔
1919

1920
        if (usec == USEC_INFINITY)
686✔
1921
                return 0;
749✔
1922

1923
        if (usec == 0)
734✔
1924
                return link_carrier_lost_impl(link);
733✔
1925

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

1938
static int link_admin_state_up(Link *link) {
1,237✔
1939
        assert(link);
1,237✔
1940

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

1944
        log_link_info(link, "Link UP");
1,237✔
1945

1946
        if (!link->network)
1,237✔
1947
                return 0;
1948

1949
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
666✔
1950
                log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
4✔
1951
                return link_request_to_bring_up_or_down(link, /* up= */ false);
4✔
1952
        }
1953

1954
        /* We set the ipv6 mtu after the device mtu, but the kernel resets
1955
         * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1956
        (void) link_set_ipv6_mtu(link, LOG_INFO);
662✔
1957

1958
        return 0;
662✔
1959
}
1960

1961
static int link_admin_state_down(Link *link) {
723✔
1962
        assert(link);
723✔
1963

1964
        log_link_info(link, "Link DOWN");
723✔
1965

1966
        link_forget_nexthops(link);
723✔
1967
        link_forget_routes(link);
723✔
1968

1969
        if (!link->network)
723✔
1970
                return 0;
1971

1972
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
665✔
1973
                log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
9✔
1974
                return link_request_to_bring_up_or_down(link, /* up= */ true);
9✔
1975
        }
1976

1977
        return 0;
1978
}
1979

1980
static bool link_is_enslaved(Link *link) {
7,012✔
1981
        if (link->flags & IFF_SLAVE)
7,012✔
1982
                return true;
1983

1984
        if (link->master_ifindex > 0)
6,957✔
1985
                return true;
238✔
1986

1987
        return false;
1988
}
1989

1990
void link_update_operstate(Link *link, bool also_update_master) {
13,432✔
1991
        LinkOperationalState operstate;
13,432✔
1992
        LinkCarrierState carrier_state;
13,432✔
1993
        LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
13,432✔
1994
        LinkOnlineState online_state;
13,432✔
1995
        _cleanup_strv_free_ char **p = NULL;
13,432✔
1996
        bool changed = false;
13,432✔
1997

1998
        assert(link);
13,432✔
1999

2000
        if (link->kernel_operstate == IF_OPER_DORMANT)
13,432✔
2001
                carrier_state = LINK_CARRIER_STATE_DORMANT;
2002
        else if (link_has_carrier(link)) {
13,432✔
2003
                if (link_is_enslaved(link))
7,012✔
2004
                        carrier_state = LINK_CARRIER_STATE_ENSLAVED;
2005
                else
2006
                        carrier_state = LINK_CARRIER_STATE_CARRIER;
2007
        } else if (link->flags & IFF_UP)
6,420✔
2008
                carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
2009
        else
2010
                carrier_state = LINK_CARRIER_STATE_OFF;
2011

2012
        if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
2013
                Link *slave;
7,012✔
2014

2015
                SET_FOREACH(slave, link->slaves) {
7,271✔
2016
                        link_update_operstate(slave, false);
259✔
2017

2018
                        if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
259✔
2019
                                carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
68✔
2020
                }
2021
        }
2022

2023
        link_get_address_states(link, &ipv4_address_state, &ipv6_address_state, &address_state);
13,432✔
2024

2025
        /* Mapping of address and carrier state vs operational state
2026
         *                                                     carrier state
2027
         *                          | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
2028
         *                 ------------------------------------------------------------------------------
2029
         *                 off      | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
2030
         * address_state   degraded | off | no-carrier | dormant | degraded         | degraded | enslaved
2031
         *                 routable | off | no-carrier | dormant | routable         | routable | routable
2032
         */
2033

2034
        if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_ROUTABLE)
13,432✔
2035
                operstate = LINK_OPERSTATE_ROUTABLE;
2036
        else if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_DEGRADED)
30✔
2037
                operstate = LINK_OPERSTATE_DEGRADED;
2038
        else if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
13,386✔
2039
                operstate = (LinkOperationalState) carrier_state;
2040
        else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
4,513✔
2041
                operstate = LINK_OPERSTATE_ROUTABLE;
2042
        else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1,360✔
2043
                operstate = LINK_OPERSTATE_DEGRADED;
2044
        else
2045
                operstate = LINK_OPERSTATE_ENSLAVED;
36✔
2046

2047
        LinkOperationalStateRange req;
13,432✔
2048
        link_required_operstate_for_online(link, &req);
13,432✔
2049

2050
        /* Only determine online state for managed links with RequiredForOnline=yes */
2051
        if (!link->network || !link->network->required_for_online)
13,432✔
2052
                online_state = _LINK_ONLINE_STATE_INVALID;
2053

2054
        else if (!operational_state_is_in_range(operstate, &req))
8,850✔
2055
                online_state = LINK_ONLINE_STATE_OFFLINE;
2056

2057
        else {
2058
                AddressFamily required_family = link_required_family_for_online(link);
4,432✔
2059
                bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
4,432✔
2060
                bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
4,432✔
2061

2062
                /* The operational state is within the range required for online.
2063
                 * If a particular address family is also required, we might revert
2064
                 * to offline in the blocks below. */
2065
                online_state = LINK_ONLINE_STATE_ONLINE;
4,432✔
2066

2067
                if (req.min >= LINK_OPERSTATE_DEGRADED) {
4,432✔
2068
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
4,263✔
2069
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2070
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
4,263✔
2071
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2072
                }
2073

2074
                if (req.min >= LINK_OPERSTATE_ROUTABLE) {
4,432✔
2075
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2076
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2077
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2078
                                online_state = LINK_ONLINE_STATE_OFFLINE;
3✔
2079
                }
2080
        }
2081

2082
        if (link->carrier_state != carrier_state) {
13,432✔
2083
                link->carrier_state = carrier_state;
4,688✔
2084
                changed = true;
4,688✔
2085
                if (strv_extend(&p, "CarrierState") < 0)
4,688✔
2086
                        log_oom();
×
2087
        }
2088

2089
        if (link->address_state != address_state) {
13,432✔
2090
                link->address_state = address_state;
1,980✔
2091
                changed = true;
1,980✔
2092
                if (strv_extend(&p, "AddressState") < 0)
1,980✔
2093
                        log_oom();
×
2094
        }
2095

2096
        if (link->ipv4_address_state != ipv4_address_state) {
13,432✔
2097
                link->ipv4_address_state = ipv4_address_state;
961✔
2098
                changed = true;
961✔
2099
                if (strv_extend(&p, "IPv4AddressState") < 0)
961✔
2100
                        log_oom();
×
2101
        }
2102

2103
        if (link->ipv6_address_state != ipv6_address_state) {
13,432✔
2104
                link->ipv6_address_state = ipv6_address_state;
1,697✔
2105
                changed = true;
1,697✔
2106
                if (strv_extend(&p, "IPv6AddressState") < 0)
1,697✔
2107
                        log_oom();
×
2108
        }
2109

2110
        if (link->operstate != operstate) {
13,432✔
2111
                link->operstate = operstate;
5,664✔
2112
                changed = true;
5,664✔
2113
                if (strv_extend(&p, "OperationalState") < 0)
5,664✔
2114
                        log_oom();
×
2115
        }
2116

2117
        if (link->online_state != online_state) {
13,432✔
2118
                link->online_state = online_state;
1,873✔
2119
                changed = true;
1,873✔
2120
                if (strv_extend(&p, "OnlineState") < 0)
1,873✔
2121
                        log_oom();
×
2122
        }
2123

2124
        if (p)
13,432✔
2125
                link_send_changed_strv(link, p);
7,972✔
2126
        if (changed)
13,432✔
2127
                link_dirty(link);
7,972✔
2128

2129
        if (also_update_master) {
13,432✔
2130
                Link *master;
13,173✔
2131

2132
                if (link_get_master(link, &master) >= 0)
13,173✔
2133
                        link_update_operstate(master, true);
161✔
2134
        }
2135
}
13,432✔
2136

2137
bool link_has_carrier(Link *link) {
42,483✔
2138
        assert(link);
42,483✔
2139
        return netif_has_carrier(link->kernel_operstate, link->flags);
42,483✔
2140
}
2141

2142
bool link_multicast_enabled(Link *link) {
9,741✔
2143
        assert(link);
9,741✔
2144

2145
        /* If Multicast= is specified, use the value. */
2146
        if (link->network && link->network->multicast >= 0)
9,741✔
2147
                return link->network->multicast;
×
2148

2149
        /* Otherwise, return the current state. */
2150
        return FLAGS_SET(link->flags, IFF_MULTICAST);
9,741✔
2151
}
2152

2153
#define FLAG_STRING(string, flag, old, new)                      \
2154
        (((old ^ new) & flag)                                    \
2155
         ? ((old & flag) ? (" -" string) : (" +" string))        \
2156
         : "")
2157

2158
static int link_update_flags(Link *link, sd_netlink_message *message) {
10,651✔
2159
        bool link_was_admin_up, had_carrier;
10,651✔
2160
        uint8_t operstate;
10,651✔
2161
        unsigned flags;
10,651✔
2162
        int r;
10,651✔
2163

2164
        assert(link);
10,651✔
2165
        assert(message);
10,651✔
2166

2167
        r = sd_rtnl_message_link_get_flags(message, &flags);
10,651✔
2168
        if (r < 0)
10,651✔
2169
                return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
×
2170

2171
        r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
10,651✔
2172
        if (r == -ENODATA)
10,651✔
2173
                /* If we got a message without operstate, assume the state was unchanged. */
2174
                operstate = link->kernel_operstate;
×
2175
        else if (r < 0)
10,651✔
2176
                return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
×
2177

2178
        if (link->flags == flags && link->kernel_operstate == operstate)
10,651✔
2179
                return 0;
2180

2181
        if (link->flags != flags) {
4,667✔
2182
                unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
4,665✔
2183

2184
                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,805✔
2185
                               FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
2186
                               FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
2187
                               FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
2188
                               FLAG_STRING("UP", IFF_UP, link->flags, flags),
2189
                               FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
2190
                               FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
2191
                               FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
2192
                               FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
2193
                               FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
2194
                               FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
2195
                               FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
2196
                               FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
2197
                               FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
2198
                               FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
2199
                               FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
2200
                               FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
2201
                               FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
2202
                               FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
2203
                               FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
2204

2205
                unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
4,665✔
2206
                                  IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
2207
                                  IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
2208
                                  IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
2209
                                  IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
2210
                                  IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
2211
                unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
4,665✔
2212
                unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
4,665✔
2213

2214
                if (unknown_flags_added)
4,665✔
2215
                        log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
×
2216

2217
                if (unknown_flags_removed)
4,665✔
2218
                        log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
×
2219
        }
2220

2221
        link_was_admin_up = link->flags & IFF_UP;
4,667✔
2222
        had_carrier = link_has_carrier(link);
4,667✔
2223

2224
        link->flags = flags;
4,667✔
2225
        link->kernel_operstate = operstate;
4,667✔
2226

2227
        link_update_operstate(link, true);
4,667✔
2228

2229
        r = 0;
4,667✔
2230

2231
        if (!link_was_admin_up && (link->flags & IFF_UP))
4,667✔
2232
                r = link_admin_state_up(link);
1,237✔
2233
        else if (link_was_admin_up && !(link->flags & IFF_UP))
1,126✔
2234
                r = link_admin_state_down(link);
723✔
2235
        if (r < 0)
1,960✔
2236
                return r;
2237

2238
        if (!had_carrier && link_has_carrier(link))
4,667✔
2239
                r = link_carrier_gained(link);
1,262✔
2240
        else if (had_carrier && !link_has_carrier(link))
775✔
2241
                r = link_carrier_lost(link);
749✔
2242
        if (r < 0)
4,667✔
UNCOV
2243
                return r;
×
2244

2245
        return 0;
2246
}
2247

2248
static int link_update_master(Link *link, sd_netlink_message *message) {
10,651✔
2249
        int master_ifindex, r;
10,651✔
2250

2251
        assert(link);
10,651✔
2252
        assert(message);
10,651✔
2253

2254
        r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
10,651✔
2255
        if (r == -ENODATA)
10,651✔
2256
                master_ifindex = 0; /* no master interface */
10,133✔
2257
        else if (r < 0)
518✔
2258
                return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
×
2259

2260
        if (master_ifindex == link->ifindex)
10,651✔
2261
                master_ifindex = 0;
7✔
2262

2263
        if (master_ifindex != link->master_ifindex) {
10,651✔
2264
                if (link->master_ifindex == 0)
55✔
2265
                        log_link_debug(link, "Attached to master interface: %i", master_ifindex);
46✔
2266
                else if (master_ifindex == 0)
9✔
2267
                        log_link_debug(link, "Detached from master interface: %i", link->master_ifindex);
9✔
2268
                else
2269
                        log_link_debug(link, "Master interface changed: %i %s %i", link->master_ifindex,
×
2270
                                       glyph(GLYPH_ARROW_RIGHT), master_ifindex);
2271

2272
                link_drop_from_master(link);
55✔
2273
                link->master_ifindex = master_ifindex;
55✔
2274

2275
                /* Updating master ifindex may cause operational state change, e.g. carrier <-> enslaved */
2276
                link_dirty(link);
55✔
2277
        }
2278

2279
        r = link_append_to_master(link);
10,651✔
2280
        if (r < 0)
10,651✔
2281
                return log_link_debug_errno(link, r, "Failed to append link to master: %m");
×
2282

2283
        return 0;
2284
}
2285

2286
static int link_update_driver(Link *link, sd_netlink_message *message) {
10,651✔
2287
        int r;
10,651✔
2288

2289
        assert(link);
10,651✔
2290
        assert(link->manager);
10,651✔
2291
        assert(message);
10,651✔
2292

2293
        /* Driver is already read. Assuming the driver is never changed. */
2294
        if (link->ethtool_driver_read)
10,651✔
2295
                return 0;
2296

2297
        /* When udevd is running, read the driver after the interface is initialized by udevd.
2298
         * Otherwise, ethtool may not work correctly. See issue #22538.
2299
         * When udevd is not running, read the value when the interface is detected. */
2300
        if (udev_available() && !link->dev)
5,737✔
2301
                return 0;
2302

2303
        link->ethtool_driver_read = true;
2,636✔
2304

2305
        r = ethtool_get_driver(&link->manager->ethtool_fd, link->ifname, &link->driver);
2,636✔
2306
        if (r < 0) {
2,636✔
2307
                log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
499✔
2308
                return 0;
499✔
2309
        }
2310

2311
        log_link_debug(link, "Found driver: %s", strna(link->driver));
2,137✔
2312

2313
        if (streq_ptr(link->driver, "dsa")) {
2,137✔
2314
                uint32_t dsa_master_ifindex = 0;
×
2315

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

2320
                if (dsa_master_ifindex > INT_MAX) {
×
2321
                        log_link_debug(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
×
2322
                                       dsa_master_ifindex);
2323
                        dsa_master_ifindex = 0;
×
2324
                }
2325

2326
                link->dsa_master_ifindex = (int) dsa_master_ifindex;
×
2327
        }
2328

2329
        return 1; /* needs reconfigure */
2330
}
2331

2332
static int link_update_permanent_hardware_address(Link *link, sd_netlink_message *message) {
10,651✔
2333
        int r;
10,651✔
2334

2335
        assert(link);
10,651✔
2336
        assert(link->manager);
10,651✔
2337
        assert(message);
10,651✔
2338

2339
        if (link->permanent_hw_addr.length > 0)
10,651✔
2340
                return 0;
2341

2342
        r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
9,983✔
2343
        if (r == -ENODATA)
9,983✔
2344
                return 0;
2345
        if (r < 0)
222✔
2346
                return log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute: %m");
×
2347

2348
        if (link->permanent_hw_addr.length > 0)
222✔
2349
                log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
222✔
2350

2351
        return 1; /* needs reconfigure */
2352
}
2353

2354
static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
10,651✔
2355
        struct hw_addr_data addr;
10,651✔
2356
        int r;
10,651✔
2357

2358
        assert(link);
10,651✔
2359
        assert(message);
10,651✔
2360

2361
        r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
10,651✔
2362
        if (r < 0 && r != -ENODATA)
10,651✔
2363
                return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
×
2364

2365
        r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
10,651✔
2366
        if (r == -ENODATA)
10,651✔
2367
                return 0;
2368
        if (r < 0)
10,443✔
2369
                return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
×
2370

2371
        if (hw_addr_equal(&link->hw_addr, &addr))
10,443✔
2372
                return 0;
2373

2374
        if (link->hw_addr.length == 0)
2,953✔
2375
                log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
2,746✔
2376
        else {
2377
                log_link_debug(link, "Hardware address is changed: %s %s %s",
207✔
2378
                               HW_ADDR_TO_STR(&link->hw_addr),
2379
                               glyph(GLYPH_ARROW_RIGHT),
2380
                               HW_ADDR_TO_STR(&addr));
2381

2382
                hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
207✔
2383
        }
2384

2385
        link->hw_addr = addr;
2,953✔
2386

2387
        if (!hw_addr_is_null(&link->hw_addr)) {
2,953✔
2388
                r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
1,071✔
2389
                if (r == -EEXIST && streq_ptr(link->kind, "bond"))
1,071✔
2390
                        /* bonding master and its slaves have the same hardware address. */
2391
                        r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
4✔
2392
                if (r < 0)
1,071✔
2393
                        log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
51✔
2394
        }
2395

2396
        r = ipv4acd_update_mac(link);
2,953✔
2397
        if (r < 0)
2,953✔
2398
                return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
×
2399

2400
        r = ipv4ll_update_mac(link);
2,953✔
2401
        if (r < 0)
2,953✔
2402
                return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
×
2403

2404
        r = dhcp4_update_mac(link);
2,953✔
2405
        if (r < 0)
2,953✔
2406
                return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
×
2407

2408
        r = dhcp6_update_mac(link);
2,953✔
2409
        if (r < 0)
2,953✔
2410
                return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
×
2411

2412
        r = radv_update_mac(link);
2,953✔
2413
        if (r < 0)
2,953✔
2414
                return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
×
2415

2416
        if (link->ndisc && link->hw_addr.length == ETH_ALEN) {
2,953✔
2417
                r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
×
2418
                if (r < 0)
×
2419
                        return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
×
2420
        }
2421

2422
        if (link->lldp_rx) {
2,953✔
2423
                r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
22✔
2424
                if (r < 0)
22✔
2425
                        return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
×
2426
        }
2427

2428
        if (link->lldp_tx) {
2,953✔
2429
                r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
×
2430
                if (r < 0)
×
2431
                        return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
×
2432
        }
2433

2434
        return 1; /* needs reconfigure */
2435
}
2436

2437
static int link_update_mtu(Link *link, sd_netlink_message *message) {
10,651✔
2438
        uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
10,651✔
2439
        int r;
10,651✔
2440

2441
        assert(link);
10,651✔
2442
        assert(message);
10,651✔
2443

2444
        r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
10,651✔
2445
        if (r == -ENODATA)
10,651✔
2446
                return 0;
10,651✔
2447
        if (r < 0)
10,651✔
2448
                return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
×
2449
        if (mtu == 0)
10,651✔
2450
                return 0;
2451

2452
        r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
10,629✔
2453
        if (r < 0 && r != -ENODATA)
10,629✔
2454
                return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
×
2455

2456
        r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
10,629✔
2457
        if (r < 0 && r != -ENODATA)
10,629✔
2458
                return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
×
2459

2460
        if (max_mtu == 0)
10,629✔
2461
                max_mtu = UINT32_MAX;
6,815✔
2462

2463
        link->min_mtu = min_mtu;
10,629✔
2464
        link->max_mtu = max_mtu;
10,629✔
2465

2466
        if (link->original_mtu == 0) {
10,629✔
2467
                link->original_mtu = mtu;
2,809✔
2468
                log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2,809✔
2469
                               link->original_mtu, link->min_mtu, link->max_mtu);
2470
        }
2471

2472
        if (link->mtu == mtu)
10,629✔
2473
                return 0;
2474

2475
        if (link->mtu != 0)
2,900✔
2476
                log_link_debug(link, "MTU is changed: %"PRIu32" %s %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
91✔
2477
                               link->mtu, glyph(GLYPH_ARROW_RIGHT), mtu,
2478
                               link->min_mtu, link->max_mtu);
2479

2480
        link->mtu = mtu;
2,900✔
2481

2482
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
2,900✔
2483
                /* The kernel resets IPv6 MTU after changing device MTU. So, we need to re-set IPv6 MTU again. */
2484
                (void) link_set_ipv6_mtu_async(link);
47✔
2485

2486
        if (link->dhcp_client) {
2,900✔
2487
                r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
4✔
2488
                if (r < 0)
4✔
2489
                        return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
×
2490
        }
2491

2492
        if (link->radv) {
2,900✔
2493
                r = sd_radv_set_mtu(link->radv, link->mtu);
×
2494
                if (r < 0)
×
2495
                        return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
×
2496
        }
2497

2498
        return 0;
2499
}
2500

2501
static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
10,651✔
2502
        _cleanup_strv_free_ char **altnames = NULL;
10,651✔
2503
        int r;
10,651✔
2504

2505
        assert(link);
10,651✔
2506
        assert(message);
10,651✔
2507

2508
        r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
10,651✔
2509
        if (r == -ENODATA)
10,651✔
2510
                /* The message does not have IFLA_PROP_LIST container attribute. It does not mean the
2511
                 * interface has no alternative name. */
2512
                return 0;
2513
        if (r < 0)
172✔
2514
                return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
×
2515

2516
        if (strv_equal(altnames, link->alternative_names))
172✔
2517
                return 0;
2518

2519
        STRV_FOREACH(n, link->alternative_names)
51✔
2520
                hashmap_remove(link->manager->links_by_name, *n);
11✔
2521

2522
        strv_free_and_replace(link->alternative_names, altnames);
40✔
2523

2524
        STRV_FOREACH(n, link->alternative_names) {
88✔
2525
                r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
48✔
2526
                if (r < 0)
48✔
2527
                        return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
×
2528
        }
2529

2530
        return 1; /* needs reconfigure */
2531
}
2532

2533
static int link_update_name(Link *link, sd_netlink_message *message) {
10,655✔
2534
        char ifname_from_index[IF_NAMESIZE];
10,655✔
2535
        const char *ifname;
10,655✔
2536
        int r;
10,655✔
2537

2538
        assert(link);
10,655✔
2539
        assert(message);
10,655✔
2540

2541
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
10,655✔
2542
        if (r == -ENODATA)
10,655✔
2543
                /* Hmm?? But ok. */
2544
                return 0;
10,655✔
2545
        if (r < 0)
10,655✔
2546
                return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
×
2547

2548
        if (streq(ifname, link->ifname))
10,655✔
2549
                return 0;
2550

2551
        r = format_ifname(link->ifindex, ifname_from_index);
41✔
2552
        if (r < 0)
41✔
2553
                return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
4✔
2554

2555
        if (!streq(ifname, ifname_from_index)) {
37✔
2556
                log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "
×
2557
                               "with the name currently configured on the actual interface '%s'. Ignoring.",
2558
                               ifname, ifname_from_index);
2559
                return 0;
×
2560
        }
2561

2562
        log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
37✔
2563

2564
        hashmap_remove(link->manager->links_by_name, link->ifname);
37✔
2565

2566
        r = free_and_strdup(&link->ifname, ifname);
37✔
2567
        if (r < 0)
37✔
2568
                return log_oom_debug();
×
2569

2570
        r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
37✔
2571
        if (r < 0)
37✔
2572
                return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
×
2573

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

2580
        if (link->dhcp6_client) {
37✔
2581
                r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
×
2582
                if (r < 0)
×
2583
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCPv6 client: %m");
×
2584
        }
2585

2586
        if (link->ndisc) {
37✔
2587
                r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
×
2588
                if (r < 0)
×
2589
                        return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
×
2590
        }
2591

2592
        if (link->dhcp_server) {
37✔
2593
                r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
×
2594
                if (r < 0)
×
2595
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
×
2596
        }
2597

2598
        if (link->radv) {
37✔
2599
                r = sd_radv_set_ifname(link->radv, link->ifname);
×
2600
                if (r < 0)
×
2601
                        return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
×
2602
        }
2603

2604
        if (link->lldp_rx) {
37✔
2605
                r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
2✔
2606
                if (r < 0)
2✔
2607
                        return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
×
2608
        }
2609

2610
        if (link->lldp_tx) {
37✔
2611
                r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
×
2612
                if (r < 0)
×
2613
                        return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
×
2614
        }
2615

2616
        if (link->ipv4ll) {
37✔
2617
                r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
×
2618
                if (r < 0)
×
2619
                        return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
×
2620
        }
2621

2622
        r = ipv4acd_set_ifname(link);
37✔
2623
        if (r < 0)
37✔
2624
                return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
×
2625

2626
        return 1; /* needs reconfigure */
2627
}
2628

2629
static int link_update(Link *link, sd_netlink_message *message) {
10,655✔
2630
        bool needs_reconfigure = false;
10,655✔
2631
        int r;
10,655✔
2632

2633
        assert(link);
10,655✔
2634
        assert(message);
10,655✔
2635

2636
        r = link_update_name(link, message);
10,655✔
2637
        if (r < 0)
10,655✔
2638
                return r;
2639
        needs_reconfigure = needs_reconfigure || r > 0;
10,651✔
2640

2641
        r = link_update_alternative_names(link, message);
10,651✔
2642
        if (r < 0)
10,651✔
2643
                return r;
2644
        needs_reconfigure = needs_reconfigure || r > 0;
10,651✔
2645

2646
        r = link_update_mtu(link, message);
10,651✔
2647
        if (r < 0)
10,651✔
2648
                return r;
2649

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

2655
        r = link_update_permanent_hardware_address(link, message);
10,651✔
2656
        if (r < 0)
10,651✔
2657
                return r;
2658
        needs_reconfigure = needs_reconfigure || r > 0;
10,651✔
2659

2660
        r = link_update_hardware_address(link, message);
10,651✔
2661
        if (r < 0)
10,651✔
2662
                return r;
2663
        needs_reconfigure = needs_reconfigure || r > 0;
10,651✔
2664

2665
        r = link_update_master(link, message);
10,651✔
2666
        if (r < 0)
10,651✔
2667
                return r;
2668

2669
        r = link_update_ipv6ll_addrgen_mode(link, message);
10,651✔
2670
        if (r < 0)
10,651✔
2671
                return r;
2672

2673
        r = link_update_flags(link, message);
10,651✔
2674
        if (r < 0)
10,651✔
2675
                return r;
2676

2677
        r = link_update_bridge_vlan(link, message);
10,651✔
2678
        if (r < 0)
10,651✔
2679
                return r;
2680

2681
        return needs_reconfigure;
10,651✔
2682
}
2683

2684
static Link *link_drop_or_unref(Link *link) {
×
2685
        if (!link)
×
2686
                return NULL;
2687
        if (!link->manager)
×
2688
                return link_unref(link);
×
2689
        return link_drop(link);
×
2690
}
2691

2692
DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2,809✔
2693

2694
static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
2,809✔
2695
        _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL;
2,809✔
2696
        _cleanup_(link_drop_or_unrefp) Link *link = NULL;
2,809✔
2697
        unsigned short iftype;
2,809✔
2698
        int r, ifindex;
2,809✔
2699

2700
        assert(manager);
2,809✔
2701
        assert(message);
2,809✔
2702
        assert(ret);
2,809✔
2703

2704
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2,809✔
2705
        if (r < 0)
2,809✔
2706
                return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
×
2707
        else if (ifindex <= 0)
2,809✔
2708
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
×
2709

2710
        r = sd_rtnl_message_link_get_type(message, &iftype);
2,809✔
2711
        if (r < 0)
2,809✔
2712
                return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
×
2713

2714
        r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2,809✔
2715
        if (r < 0)
2,809✔
2716
                return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
×
2717

2718
        /* check for link kind */
2719
        r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2,809✔
2720
        if (r >= 0) {
2,809✔
2721
                r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2,269✔
2722
                if (r < 0 && r != -ENODATA)
2,269✔
2723
                        return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
×
2724
                r = sd_netlink_message_exit_container(message);
2,269✔
2725
                if (r < 0)
2,269✔
2726
                        return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
×
2727
        }
2728

2729
        if (!manager->test_mode) {
2,809✔
2730
                /* Do not update state files when running in test mode. */
2731
                if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2,808✔
2732
                        return log_oom_debug();
×
2733

2734
                if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2,808✔
2735
                        return log_oom_debug();
×
2736
        }
2737

2738
        link = new(Link, 1);
2,809✔
2739
        if (!link)
2,809✔
2740
                return -ENOMEM;
2741

2742
        *link = (Link) {
2,809✔
2743
                .n_ref = 1,
2744
                .state = LINK_STATE_PENDING,
2745
                .online_state = _LINK_ONLINE_STATE_INVALID,
2746
                .automatic_reconfigure_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_SEC, .burst = 5 },
2747
                .ifindex = ifindex,
2748
                .iftype = iftype,
2749
                .ifname = TAKE_PTR(ifname),
2,809✔
2750
                .kind = TAKE_PTR(kind),
2,809✔
2751

2752
                .bridge_vlan_pvid = UINT16_MAX,
2753

2754
                .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2755

2756
                .state_file = TAKE_PTR(state_file),
2,809✔
2757
                .lease_file = TAKE_PTR(lease_file),
2,809✔
2758

2759
                .n_dns = UINT_MAX,
2760
                .dns_default_route = -1,
2761
                .llmnr = _RESOLVE_SUPPORT_INVALID,
2762
                .mdns = _RESOLVE_SUPPORT_INVALID,
2763
                .dnssec_mode = _DNSSEC_MODE_INVALID,
2764
                .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2765
        };
2766

2767
        r = hashmap_ensure_put(&manager->links_by_index, &link_hash_ops, INT_TO_PTR(link->ifindex), link);
2,809✔
2768
        if (r < 0)
2,809✔
2769
                return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
×
2770

2771
        link->manager = manager;
2,809✔
2772

2773
        r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
2,809✔
2774
        if (r < 0)
2,809✔
2775
                return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
×
2776

2777
        log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
3,349✔
2778
                       link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2779

2780
        /* If contained in this set, the link is wireless and the corresponding NL80211_CMD_NEW_INTERFACE
2781
         * message arrived too early. Request the wireless link information again.
2782
         */
2783
        if (set_remove(manager->new_wlan_ifindices, INT_TO_PTR(link->ifindex))) {
2,809✔
2784
                r = link_get_wlan_interface(link);
×
2785
                if (r < 0)
×
2786
                        log_link_warning_errno(link, r, "Failed to get wireless interface, ignoring: %m");
×
2787
        }
2788

2789
        *ret = TAKE_PTR(link);
2,809✔
2790
        return 0;
2,809✔
2791
}
2792

2793
int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
6,593✔
2794
        Link *link = NULL;
6,593✔
2795
        NetDev *netdev = NULL;
6,593✔
2796
        uint16_t type;
6,593✔
2797
        const char *name;
6,593✔
2798
        int r, ifindex;
6,593✔
2799

2800
        assert(rtnl);
6,593✔
2801
        assert(message);
6,593✔
2802
        assert(manager);
6,593✔
2803

2804
        if (sd_netlink_message_is_error(message)) {
6,593✔
2805
                r = sd_netlink_message_get_errno(message);
×
2806
                if (r < 0)
×
2807
                        log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
×
2808

2809
                return 0;
×
2810
        }
2811

2812
        r = sd_netlink_message_get_type(message, &type);
6,593✔
2813
        if (r < 0) {
6,593✔
2814
                log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
×
2815
                return 0;
×
2816
        } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
6,593✔
2817
                log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
×
2818
                return 0;
×
2819
        }
2820

2821
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
6,593✔
2822
        if (r < 0) {
6,593✔
2823
                log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
×
2824
                return 0;
×
2825
        } else if (ifindex <= 0) {
6,593✔
2826
                log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
×
2827
                return 0;
×
2828
        }
2829

2830
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
6,593✔
2831
        if (r < 0) {
6,593✔
2832
                log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
×
2833
                return 0;
×
2834
        }
2835

2836
        (void) link_get_by_index(manager, ifindex, &link);
6,593✔
2837
        (void) netdev_get(manager, name, &netdev);
6,593✔
2838

2839
        switch (type) {
6,593✔
2840
        case RTM_NEWLINK:
5,706✔
2841
                if (netdev) {
5,706✔
2842
                        /* netdev exists, so make sure the ifindex matches */
2843
                        r = netdev_set_ifindex(netdev, message);
2,903✔
2844
                        if (r < 0) {
2,903✔
2845
                                log_netdev_warning_errno(netdev, r, "Could not process new link message for netdev, ignoring: %m");
×
2846
                                netdev_enter_failed(netdev);
×
2847
                                return 0;
×
2848
                        }
2849
                }
2850

2851
                if (!link) {
5,706✔
2852
                        /* link is new, so add it */
2853
                        r = link_new(manager, message, &link);
2,809✔
2854
                        if (r < 0) {
2,809✔
2855
                                log_warning_errno(r, "Could not process new link message: %m");
×
2856
                                return 0;
×
2857
                        }
2858

2859
                        r = link_update(link, message);
2,809✔
2860
                        if (r < 0) {
2,809✔
2861
                                log_link_warning_errno(link, r, "Could not process link message: %m");
×
2862
                                link_enter_failed(link);
×
2863
                                return 0;
×
2864
                        }
2865

2866
                        if (link->manager->test_mode) {
2,809✔
2867
                                log_link_debug(link, "Running in test mode, refusing to enter initialized state.");
1✔
2868
                                link_set_state(link, LINK_STATE_UNMANAGED);
1✔
2869
                                return 0;
1✔
2870
                        }
2871

2872
                        /* Do not enter initialized state if we are enumerating. */
2873
                        if (manager->enumerating)
2,808✔
2874
                                return 0;
2875

2876
                        r = link_check_initialized(link);
931✔
2877
                        if (r < 0) {
931✔
2878
                                log_link_warning_errno(link, r, "Failed to check link is initialized: %m");
×
2879
                                link_enter_failed(link);
×
2880
                                return 0;
×
2881
                        }
2882
                } else {
2883
                        r = link_update(link, message);
2,897✔
2884
                        if (r < 0) {
2,897✔
2885
                                log_link_warning_errno(link, r, "Could not process link message: %m");
4✔
2886
                                link_enter_failed(link);
4✔
2887
                                return 0;
4✔
2888
                        }
2889
                        if (r == 0)
2,893✔
2890
                                return 0;
2891

2892
                        if (link->manager->test_mode) {
254✔
2893
                                log_link_debug(link, "Running in test mode, refusing to configure interface.");
×
2894
                                link_set_state(link, LINK_STATE_UNMANAGED);
×
2895
                                return 0;
×
2896
                        }
2897

2898
                        /* Do not configure interface if we are enumerating. */
2899
                        if (manager->enumerating)
254✔
2900
                                return 0;
2901

2902
                        r = link_reconfigure_impl(link, /* flags= */ 0);
254✔
2903
                        if (r < 0) {
254✔
2904
                                log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
2905
                                link_enter_failed(link);
×
2906
                                return 0;
×
2907
                        }
2908
                }
2909
                break;
2910

2911
        case RTM_DELLINK:
887✔
2912
                link_drop(link);
887✔
2913
                netdev_drop(netdev);
887✔
2914
                break;
2915

2916
        default:
×
2917
                assert_not_reached();
×
2918
        }
2919

2920
        return 1;
2921
}
2922

2923
int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
4,954✔
2924
        uint16_t message_type;
4,954✔
2925
        int r;
4,954✔
2926

2927
        assert(m);
4,954✔
2928
        assert(link);
4,954✔
2929
        assert(error_msg);
4,954✔
2930

2931
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
4,954✔
2932
                return 0;
4,954✔
2933

2934
        r = sd_netlink_message_get_errno(m);
4,949✔
2935
        if (r < 0) {
4,949✔
2936
                log_link_message_warning_errno(link, m, r, "%s", error_msg);
×
2937
                link_enter_failed(link);
×
2938
                return 0;
×
2939
        }
2940

2941
        r = sd_netlink_message_get_type(m, &message_type);
4,949✔
2942
        if (r < 0) {
4,949✔
2943
                log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
×
2944
                return 0;
×
2945
        }
2946
        if (message_type != RTM_NEWLINK) {
4,949✔
2947
                log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
×
2948
                return 0;
×
2949
        }
2950

2951
        r = link_update(link, m);
4,949✔
2952
        if (r < 0) {
4,949✔
2953
                link_enter_failed(link);
×
2954
                return 0;
×
2955
        }
2956

2957
        return 1;
2958
}
2959

2960
int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
3,589✔
2961
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
3,589✔
2962
        int r;
3,589✔
2963

2964
        assert(link);
3,589✔
2965
        assert(link->manager);
3,589✔
2966
        assert(link->manager->rtnl);
3,589✔
2967
        assert(callback);
3,589✔
2968

2969
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
3,589✔
2970
        if (r < 0)
3,589✔
2971
                return r;
2972

2973
        r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
3,589✔
2974
                               link_netlink_destroy_callback, link);
2975
        if (r < 0)
3,589✔
2976
                return r;
2977

2978
        link_ref(link);
3,589✔
2979
        return 0;
2980
}
2981

2982
static const char* const link_state_table[_LINK_STATE_MAX] = {
2983
        [LINK_STATE_PENDING]     = "pending",
2984
        [LINK_STATE_INITIALIZED] = "initialized",
2985
        [LINK_STATE_CONFIGURING] = "configuring",
2986
        [LINK_STATE_CONFIGURED]  = "configured",
2987
        [LINK_STATE_UNMANAGED]   = "unmanaged",
2988
        [LINK_STATE_FAILED]      = "failed",
2989
        [LINK_STATE_LINGER]      = "linger",
2990
};
2991

2992
DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
32,947✔
2993

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

3018
        assert(ret);
594✔
3019

3020
        for (size_t i = 0; i < ELEMENTSOF(map); i++)
11,880✔
3021
                if (BIT_SET(flags, i) && map[i])
11,286✔
3022
                        if (!strextend_with_separator(&str, ",", map[i]))
1,679✔
3023
                                return -ENOMEM;
3024

3025
        *ret = TAKE_PTR(str);
594✔
3026
        return 0;
594✔
3027
}
3028

3029
static const char * const kernel_operstate_table[] = {
3030
        [IF_OPER_UNKNOWN]        = "unknown",
3031
        [IF_OPER_NOTPRESENT]     = "not-present",
3032
        [IF_OPER_DOWN]           = "down",
3033
        [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
3034
        [IF_OPER_TESTING]        = "testing",
3035
        [IF_OPER_DORMANT]        = "dormant",
3036
        [IF_OPER_UP]             = "up",
3037
};
3038

3039
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);
594✔
3040

3041
bool link_has_local_lease_domain(Link *link) {
12,241✔
3042
        assert(link);
12,241✔
3043

3044
        return link->dhcp_server &&
12,398✔
3045
                link->network &&
157✔
3046
                link->network->dhcp_server_local_lease_domain &&
12,417✔
3047
                !dns_name_is_root(link->network->dhcp_server_local_lease_domain);
19✔
3048
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc