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

systemd / systemd / 15057632786

15 May 2025 09:01PM UTC coverage: 72.267% (+0.02%) from 72.244%
15057632786

push

github

bluca
man: document how to hook stuff into system wakeup

Fixes: #6364

298523 of 413084 relevant lines covered (72.27%)

738132.88 hits per line

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

84.45
/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 <netinet/in.h>
9
#include <sys/socket.h>
10
#include <unistd.h>
11

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

75
void link_required_operstate_for_online(Link *link, LinkOperationalStateRange *ret) {
17,132✔
76
        assert(link);
17,132✔
77
        assert(ret);
17,132✔
78

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

104
AddressFamily link_required_family_for_online(Link *link) {
8,922✔
105
        assert(link);
8,922✔
106

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

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

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

119
        return ADDRESS_FAMILY_NO;
120
}
121

122
bool link_ipv6_enabled(Link *link) {
824✔
123
        assert(link);
824✔
124

125
        if (!socket_ipv6_is_supported())
824✔
126
                return false;
127

128
        if (link->iftype == ARPHRD_CAN)
824✔
129
                return false;
130

131
        if (!link->network)
822✔
132
                return false;
133

134
        if (link->network->bond)
822✔
135
                return false;
136

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

140
        if (network_has_static_ipv6_configurations(link->network))
98✔
141
                return true;
23✔
142

143
        return false;
144
}
145

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

149
        assert(link);
267✔
150

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

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

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

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

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

172
static bool link_is_ready_to_configure_one(Link *link, bool allow_unmanaged) {
55,017✔
173
        assert(link);
55,017✔
174

175
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
55,017✔
176
                return false;
177

178
        if (!link->network)
54,988✔
179
                return allow_unmanaged;
180

181
        if (!link->network->configure_without_carrier) {
54,987✔
182
                if (link->set_flags_messages > 0)
54,807✔
183
                        return false;
184

185
                if (!link_has_carrier(link))
13,435✔
186
                        return false;
187
        }
188

189
        if (link->set_link_messages > 0)
10,395✔
190
                return false;
191

192
        if (!link->activated)
10,262✔
193
                return false;
52✔
194

195
        return true;
196
}
197

198
bool link_is_ready_to_configure(Link *link, bool allow_unmanaged) {
55,017✔
199
        return check_ready_for_all_sr_iov_ports(link, allow_unmanaged, link_is_ready_to_configure_one);
55,017✔
200
}
201

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

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

210
        return link_is_ready_to_configure(link, allow_unmanaged);
1,639✔
211
}
212

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

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

224
        link->search_domains = ordered_set_free(link->search_domains);
2,624✔
225
        link->route_domains = ordered_set_free(link->route_domains);
2,624✔
226

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

233
        link->dnssec_negative_trust_anchors = set_free(link->dnssec_negative_trust_anchors);
2,624✔
234
}
2,624✔
235

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

240
        link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
4,482✔
241

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

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

249
        link->ipv4acd_by_address = hashmap_free(link->ipv4acd_by_address);
4,482✔
250

251
        link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
4,482✔
252

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

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

260
        link->radv = sd_radv_unref(link->radv);
4,482✔
261
}
262

263
static Link* link_free(Link *link) {
2,609✔
264
        assert(link);
2,609✔
265

266
        (void) link_clear_sysctl_shadows(link);
2,609✔
267

268
        link_ntp_settings_clear(link);
2,609✔
269
        link_dns_settings_clear(link);
2,609✔
270

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

276
        link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
2,609✔
277

278
        link_free_engines(link);
2,609✔
279

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

288
        unlink_and_free(link->lease_file);
2,609✔
289
        unlink_and_free(link->state_file);
2,609✔
290

291
        sd_device_unref(link->dev);
2,609✔
292
        netdev_unref(link->netdev);
2,609✔
293

294
        hashmap_free(link->bound_to_links);
2,609✔
295
        hashmap_free(link->bound_by_links);
2,609✔
296

297
        set_free(link->slaves);
2,609✔
298

299
        network_unref(link->network);
2,609✔
300

301
        sd_event_source_disable_unref(link->carrier_lost_timer);
2,609✔
302
        sd_event_source_disable_unref(link->ipv6_mtu_wait_synced_event_source);
2,609✔
303

304
        return mfree(link);
2,609✔
305
}
306

307
DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
163,209✔
308

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

314
int link_get_by_index(Manager *m, int ifindex, Link **ret) {
67,103✔
315
        Link *link;
67,103✔
316

317
        assert(m);
67,103✔
318

319
        if (ifindex <= 0)
67,103✔
320
                return -EINVAL;
321

322
        link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
66,397✔
323
        if (!link)
66,397✔
324
                return -ENODEV;
325

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

331
int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
2,013✔
332
        Link *link;
2,013✔
333

334
        assert(m);
2,013✔
335
        assert(ifname);
2,013✔
336

337
        link = hashmap_get(m->links_by_name, ifname);
2,013✔
338
        if (!link)
2,013✔
339
                return -ENODEV;
340

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

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

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

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

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

361
int link_get_master(Link *link, Link **ret) {
13,491✔
362
        assert(link);
13,491✔
363
        assert(link->manager);
13,491✔
364
        assert(ret);
13,491✔
365

366
        if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex)
13,491✔
367
                return -ENODEV;
368

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

372
void link_set_state(Link *link, LinkState state) {
8,528✔
373
        assert(link);
8,528✔
374

375
        if (link->state == state)
8,528✔
376
                return;
1,623✔
377

378
        log_link_debug(link, "State changed: %s -> %s",
6,905✔
379
                       link_state_to_string(link->state),
380
                       link_state_to_string(state));
381

382
        link->state = state;
6,905✔
383

384
        link_send_changed(link, "AdministrativeState");
6,905✔
385
        link_dirty(link);
6,905✔
386
}
387

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

391
        assert(link);
4,274✔
392
        assert(link->manager);
4,274✔
393
        assert(link->manager->event);
4,274✔
394

395
        bool keep_dynamic =
12,783✔
396
                may_keep_dynamic &&
1,799✔
397
                link->network &&
4,274✔
398
                (link->manager->state == MANAGER_RESTARTING ||
51✔
399
                 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC_ON_STOP));
14✔
400

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

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

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

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

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

422
                ndisc_flush(link);
4,235✔
423
        }
424

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

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

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

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

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

445
        return ret;
4,274✔
446
}
447

448
void link_enter_failed(Link *link) {
6✔
449
        assert(link);
6✔
450

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

454
        log_link_warning(link, "Failed");
6✔
455

456
        link_set_state(link, LINK_STATE_FAILED);
6✔
457

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

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

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

471
void link_check_ready(Link *link) {
23,810✔
472
        Address *a;
23,810✔
473

474
        assert(link);
23,810✔
475

476
        if (link->state == LINK_STATE_CONFIGURED)
23,810✔
477
                return;
23,018✔
478

479
        if (link->state != LINK_STATE_CONFIGURING)
20,201✔
480
                return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
1,296✔
481

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

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

488
        if (link->set_link_messages > 0)
18,589✔
489
                return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
2,146✔
490

491
        if (!link->activated)
16,443✔
492
                return (void) log_link_debug(link, "%s(): link is not activated.", __func__);
9,911✔
493

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

498
        if (!link->stacked_netdevs_created)
6,526✔
499
                return (void) log_link_debug(link, "%s(): stacked netdevs are not created.", __func__);
153✔
500

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

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

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

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

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

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

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

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

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

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

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

537
        /* All static addresses must be ready. */
538
        bool has_static_address = false;
1,879✔
539
        SET_FOREACH(a, link->addresses) {
6,703✔
540
                if (a->source != NETWORK_CONFIG_SOURCE_STATIC)
4,985✔
541
                        continue;
2,961✔
542
                if (!address_is_ready(a))
2,024✔
543
                        return (void) log_link_debug(link, "%s(): static address %s is not ready.", __func__,
161✔
544
                                                     IN_ADDR_PREFIX_TO_STRING(a->family, &a->in_addr, a->prefixlen));
545
                has_static_address = true;
546
        }
547

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

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

559
        bool ipv4ll_ready =
2,274✔
560
                link_ipv4ll_enabled(link) && link->ipv4ll_address_configured &&
1,191✔
561
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_IPV4LL);
54✔
562
        bool dhcp4_ready =
1,137✔
563
                link_dhcp4_enabled(link) && link->dhcp4_configured &&
1,215✔
564
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP4);
78✔
565
        bool dhcp6_ready =
1,137✔
566
                link_dhcp6_enabled(link) && link->dhcp6_configured &&
1,137✔
567
                (!link->network->dhcp6_use_address ||
58✔
568
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP6));
28✔
569
        bool dhcp_pd_ready =
2,274✔
570
                link_dhcp_pd_is_enabled(link) && link->dhcp_pd_configured &&
1,137✔
571
                (!link->network->dhcp_pd_assign ||
129✔
572
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP_PD));
61✔
573
        bool ndisc_ready =
2,274✔
574
                link_ndisc_enabled(link) && link->ndisc_configured &&
1,137✔
575
                (!link->network->ndisc_use_autonomous_prefix ||
398✔
576
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_NDISC));
199✔
577

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

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

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

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

609
ready:
792✔
610
        link_set_state(link, LINK_STATE_CONFIGURED);
792✔
611
}
612

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

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

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

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

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

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

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

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

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

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

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

656
        return 0;
657
}
658

659
int link_request_stacked_netdevs(Link *link, NetDevLocalAddressType type) {
10,907✔
660
        NetDev *netdev;
10,907✔
661
        int r;
10,907✔
662

663
        assert(link);
10,907✔
664

665
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
10,907✔
666
                return 0;
10,907✔
667

668
        assert(link->network);
2,383✔
669

670
        link->stacked_netdevs_created = false;
2,383✔
671

672
        HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
2,731✔
673
                if (!netdev_needs_reconfigure(netdev, type))
348✔
674
                        continue;
206✔
675

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

681
        if (link->create_stacked_netdev_messages == 0) {
2,383✔
682
                link->stacked_netdevs_created = true;
2,250✔
683
                link_check_ready(link);
2,250✔
684
        }
685

686
        return 0;
687
}
688

689
static int link_acquire_dynamic_ipv6_conf(Link *link) {
724✔
690
        int r;
724✔
691

692
        assert(link);
724✔
693

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

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

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

706
        return 0;
707
}
708

709
static int link_acquire_dynamic_ipv4_conf(Link *link) {
852✔
710
        int r;
852✔
711

712
        assert(link);
852✔
713
        assert(link->manager);
852✔
714
        assert(link->manager->event);
852✔
715

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

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

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

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

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

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

745
        return 0;
746
}
747

748
static int link_acquire_dynamic_conf(Link *link) {
852✔
749
        int r;
852✔
750

751
        assert(link);
852✔
752
        assert(link->network);
852✔
753

754
        r = link_acquire_dynamic_ipv4_conf(link);
852✔
755
        if (r < 0)
852✔
756
                return r;
757

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

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

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

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

786
        return 0;
787
}
788

789
int link_ipv6ll_gained(Link *link) {
599✔
790
        int r;
599✔
791

792
        assert(link);
599✔
793

794
        log_link_info(link, "Gained IPv6LL");
599✔
795

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

799
        r = link_acquire_dynamic_ipv6_conf(link);
471✔
800
        if (r < 0)
471✔
801
                return r;
802

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

807
        link_check_ready(link);
471✔
808
        return 0;
471✔
809
}
810

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

816
        assert(link);
20✔
817

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

821
        if (hashmap_isempty(link->bound_to_links))
20✔
822
                return 0;
20✔
823

824
        if (link->flags & IFF_UP)
17✔
825
                link_is_up = true;
7✔
826

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

833
        if (!required_up && link_is_up)
17✔
834
                return link_request_to_bring_up_or_down(link, /* up = */ false);
2✔
835
        if (required_up && !link_is_up)
15✔
836
                return link_request_to_bring_up_or_down(link, /* up = */ true);
8✔
837

838
        return 0;
839
}
840

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

845
        assert(link);
4,279✔
846

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

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

855
        return 0;
4,279✔
856
}
857

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

861
        assert(link);
16✔
862
        assert(carrier);
16✔
863

864
        if (link == carrier)
16✔
865
                return 0;
866

867
        if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
16✔
868
                return 0;
869

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

874
        link_dirty(link);
16✔
875

876
        return 0;
16✔
877
}
878

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

884
        assert(link);
2,425✔
885
        assert(link->manager);
2,425✔
886

887
        m = link->manager;
2,425✔
888

889
        HASHMAP_FOREACH(carrier, m->links_by_index) {
16,670✔
890
                if (!carrier->network)
14,245✔
891
                        continue;
13,585✔
892

893
                if (strv_isempty(carrier->network->bind_carrier))
660✔
894
                        continue;
654✔
895

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

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

909
        return 0;
2,425✔
910
}
911

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

917
        assert(link);
777✔
918
        assert(link->manager);
777✔
919

920
        if (!link->network)
777✔
921
                return 0;
777✔
922

923
        if (strv_isempty(link->network->bind_carrier))
777✔
924
                return 0;
925

926
        m = link->manager;
5✔
927

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

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

942
        return 0;
5✔
943
}
944

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

949
        assert(link);
3,423✔
950

951
        while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
3,429✔
952
                updated = true;
6✔
953

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

958
        if (updated)
3,423✔
959
                link_dirty(link);
5✔
960
}
3,423✔
961

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

966
        assert(link);
809✔
967

968
        while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
811✔
969
                updated = true;
2✔
970

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

977
        if (updated)
809✔
978
                link_dirty(link);
2✔
979
}
809✔
980

981
static int link_append_to_master(Link *link) {
434✔
982
        Link *master;
434✔
983
        int r;
434✔
984

985
        assert(link);
434✔
986

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

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

996
        link_ref(link);
35✔
997
        return 0;
998
}
999

1000
static void link_drop_from_master(Link *link) {
844✔
1001
        Link *master;
844✔
1002

1003
        assert(link);
844✔
1004

1005
        if (!link->manager)
844✔
1006
                return;
818✔
1007

1008
        if (link_get_master(link, &master) < 0)
844✔
1009
                return;
1010

1011
        link_unref(set_remove(master->slaves, link));
26✔
1012
}
1013

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

1018
        assert(link);
3,423✔
1019
        assert(link->manager);
3,423✔
1020

1021
        ORDERED_SET_FOREACH(req, link->manager->request_queue) {
5,168✔
1022
                if (req->link != link)
1,745✔
1023
                        continue;
1,632✔
1024

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

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

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

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

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

1062
                request_detach(req);
113✔
1063
        }
1064

1065
        return ret;
3,423✔
1066
}
1067

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

1072
        assert(link->manager);
809✔
1073

1074
        link_set_state(link, LINK_STATE_LINGER);
809✔
1075

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

1079
        (void) link_drop_requests(link);
809✔
1080

1081
        link_free_bound_to_list(link);
809✔
1082
        link_free_bound_by_list(link);
809✔
1083

1084
        link_clear_sr_iov_ifindices(link);
809✔
1085

1086
        link_drop_from_master(link);
809✔
1087

1088
        if (link->state_file)
809✔
1089
                (void) unlink(link->state_file);
809✔
1090

1091
        link_clean(link);
809✔
1092

1093
        STRV_FOREACH(n, link->alternative_names)
842✔
1094
                hashmap_remove(link->manager->links_by_name, *n);
33✔
1095
        hashmap_remove(link->manager->links_by_name, link->ifname);
809✔
1096

1097
        /* bonding master and its slaves have the same hardware address. */
1098
        hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
809✔
1099

1100
        /* The following must be called at last. */
1101
        assert_se(hashmap_remove(link->manager->links_by_index, INT_TO_PTR(link->ifindex)) == link);
809✔
1102
        return link_unref(link);
809✔
1103
}
1104

1105
static int link_drop_unmanaged_config(Link *link) {
777✔
1106
        int r;
777✔
1107

1108
        assert(link);
777✔
1109
        assert(link->state == LINK_STATE_CONFIGURING);
777✔
1110
        assert(link->manager);
777✔
1111

1112
        r = link_drop_unmanaged_routes(link);
777✔
1113
        RET_GATHER(r, link_drop_unmanaged_nexthops(link));
777✔
1114
        RET_GATHER(r, link_drop_unmanaged_addresses(link));
777✔
1115
        RET_GATHER(r, link_drop_unmanaged_neighbors(link));
777✔
1116
        RET_GATHER(r, link_drop_unmanaged_routing_policy_rules(link));
777✔
1117

1118
        return r;
777✔
1119
}
1120

1121
static int link_drop_static_config(Link *link) {
2,439✔
1122
        int r;
2,439✔
1123

1124
        assert(link);
2,439✔
1125
        assert(link->manager);
2,439✔
1126

1127
        r = link_drop_static_routes(link);
2,439✔
1128
        RET_GATHER(r, link_drop_static_nexthops(link));
2,439✔
1129
        RET_GATHER(r, link_drop_static_addresses(link));
2,439✔
1130
        RET_GATHER(r, link_drop_static_neighbors(link));
2,439✔
1131
        RET_GATHER(r, link_drop_static_routing_policy_rules(link));
2,439✔
1132

1133
        return r;
2,439✔
1134
}
1135

1136
static int link_drop_dynamic_config(Link *link, Network *network) {
741✔
1137
        int r;
741✔
1138

1139
        assert(link);
741✔
1140
        assert(link->network);
741✔
1141

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

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

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

1164
        return r;
741✔
1165
}
1166

1167
static int link_configure(Link *link) {
777✔
1168
        int r;
777✔
1169

1170
        assert(link);
777✔
1171
        assert(link->network);
777✔
1172
        assert(link->state == LINK_STATE_INITIALIZED);
777✔
1173

1174
        link_set_state(link, LINK_STATE_CONFIGURING);
777✔
1175

1176
        r = link_drop_unmanaged_config(link);
777✔
1177
        if (r < 0)
777✔
1178
                return r;
1179

1180
        r = link_new_bound_to_list(link);
777✔
1181
        if (r < 0)
777✔
1182
                return r;
1183

1184
        r = link_request_traffic_control(link);
777✔
1185
        if (r < 0)
777✔
1186
                return r;
1187

1188
        r = link_configure_mtu(link);
777✔
1189
        if (r < 0)
777✔
1190
                return r;
1191

1192
        if (link->iftype == ARPHRD_CAN) {
777✔
1193
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1194
                r = link_request_to_set_can(link);
6✔
1195
                if (r < 0)
6✔
1196
                        return r;
1197

1198
                return link_request_to_activate(link);
6✔
1199
        }
1200

1201
        r = link_request_sr_iov_vfs(link);
771✔
1202
        if (r < 0)
771✔
1203
                return r;
1204

1205
        r = link_set_sysctl(link);
771✔
1206
        if (r < 0)
771✔
1207
                return r;
1208

1209
        r = link_request_to_set_mac(link, /* allow_retry = */ true);
771✔
1210
        if (r < 0)
771✔
1211
                return r;
1212

1213
        r = link_request_to_set_ipoib(link);
771✔
1214
        if (r < 0)
771✔
1215
                return r;
1216

1217
        r = link_request_to_set_flags(link);
771✔
1218
        if (r < 0)
771✔
1219
                return r;
1220

1221
        r = link_request_to_set_group(link);
771✔
1222
        if (r < 0)
771✔
1223
                return r;
1224

1225
        r = link_request_to_set_addrgen_mode(link);
771✔
1226
        if (r < 0)
771✔
1227
                return r;
1228

1229
        r = link_request_to_set_master(link);
771✔
1230
        if (r < 0)
771✔
1231
                return r;
1232

1233
        r = link_request_stacked_netdevs(link, _NETDEV_LOCAL_ADDRESS_TYPE_INVALID);
771✔
1234
        if (r < 0)
771✔
1235
                return r;
1236

1237
        r = link_request_to_set_bond(link);
771✔
1238
        if (r < 0)
771✔
1239
                return r;
1240

1241
        r = link_request_to_set_bridge(link);
771✔
1242
        if (r < 0)
771✔
1243
                return r;
1244

1245
        r = link_request_to_set_bridge_vlan(link);
771✔
1246
        if (r < 0)
771✔
1247
                return r;
1248

1249
        r = link_request_to_activate(link);
771✔
1250
        if (r < 0)
771✔
1251
                return r;
1252

1253
        r = ipv4ll_configure(link);
771✔
1254
        if (r < 0)
771✔
1255
                return r;
1256

1257
        r = link_request_dhcp4_client(link);
771✔
1258
        if (r < 0)
771✔
1259
                return r;
1260

1261
        r = link_request_dhcp6_client(link);
771✔
1262
        if (r < 0)
771✔
1263
                return r;
1264

1265
        r = link_request_ndisc(link);
771✔
1266
        if (r < 0)
771✔
1267
                return r;
1268

1269
        r = link_request_dhcp_server(link);
771✔
1270
        if (r < 0)
771✔
1271
                return r;
1272

1273
        r = link_request_radv(link);
771✔
1274
        if (r < 0)
771✔
1275
                return r;
1276

1277
        r = link_lldp_rx_configure(link);
771✔
1278
        if (r < 0)
771✔
1279
                return r;
1280

1281
        r = link_lldp_tx_configure(link);
771✔
1282
        if (r < 0)
771✔
1283
                return r;
1284

1285
        r = link_request_static_configs(link);
771✔
1286
        if (r < 0)
771✔
1287
                return r;
1288

1289
        if (!link_has_carrier(link))
771✔
1290
                return 0;
1291

1292
        return link_acquire_dynamic_conf(link);
237✔
1293
}
1294

1295
static int link_get_network(Link *link, Network **ret) {
4,350✔
1296
        Network *network;
4,350✔
1297
        int r;
4,350✔
1298

1299
        assert(link);
4,350✔
1300
        assert(link->manager);
4,350✔
1301
        assert(ret);
4,350✔
1302

1303
        ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
15,998✔
1304
                bool warn = false;
15,056✔
1305

1306
                r = net_match_config(
30,112✔
1307
                                &network->match,
15,056✔
1308
                                link->dev,
1309
                                &link->hw_addr,
15,056✔
1310
                                &link->permanent_hw_addr,
15,056✔
1311
                                link->driver,
15,056✔
1312
                                link->iftype,
15,056✔
1313
                                link->kind,
15,056✔
1314
                                link->ifname,
15,056✔
1315
                                link->alternative_names,
15,056✔
1316
                                link->wlan_iftype,
1317
                                link->ssid,
15,056✔
1318
                                &link->bssid);
15,056✔
1319
                if (r < 0)
15,056✔
1320
                        return r;
3,408✔
1321
                if (r == 0)
15,056✔
1322
                        continue;
11,648✔
1323

1324
                if (network->match.ifname && link->dev) {
3,408✔
1325
                        uint8_t name_assign_type = NET_NAME_UNKNOWN;
3,395✔
1326
                        const char *attr;
3,395✔
1327

1328
                        if (sd_device_get_sysattr_value(link->dev, "name_assign_type", &attr) >= 0)
3,395✔
1329
                                (void) safe_atou8(attr, &name_assign_type);
1,938✔
1330

1331
                        warn = name_assign_type == NET_NAME_ENUM;
3,395✔
1332
                }
1333

1334
                log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
6,816✔
1335
                              "Found matching .network file%s: %s",
1336
                              warn ? ", based on potentially unpredictable interface name" : "",
1337
                              network->filename);
1338

1339
                if (network->unmanaged)
3,408✔
1340
                        return -ENOENT;
1341

1342
                *ret = network;
1,632✔
1343
                return 0;
1,632✔
1344
        }
1345

1346
        return log_link_debug_errno(link, SYNTHETIC_ERRNO(ENOENT), "No matching .network found.");
942✔
1347
}
1348

1349
static void link_enter_unmanaged(Link *link) {
2,718✔
1350
        assert(link);
2,718✔
1351

1352
        if (link->state == LINK_STATE_UNMANAGED)
2,718✔
1353
                return;
1354

1355
        log_link_full(link, link->state == LINK_STATE_INITIALIZED ? LOG_DEBUG : LOG_INFO,
1,842✔
1356
                      "Unmanaging interface.");
1357

1358
        (void) link_stop_engines(link, /* may_keep_dynamic = */ false);
1,837✔
1359
        (void) link_drop_requests(link);
1,837✔
1360
        (void) link_drop_static_config(link);
1,837✔
1361

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

1369
        link->network = network_unref(link->network);
1,837✔
1370
        link_set_state(link, LINK_STATE_UNMANAGED);
1,837✔
1371
}
1372

1373
int link_reconfigure_impl(Link *link, LinkReconfigurationFlag flags) {
5,305✔
1374
        Network *network = NULL;
5,305✔
1375
        int r;
5,305✔
1376

1377
        assert(link);
5,305✔
1378
        assert(link->manager);
5,305✔
1379

1380
        link_assign_netdev(link);
5,305✔
1381

1382
        if (link->manager->state != MANAGER_RUNNING)
5,305✔
1383
                return 0;
5,305✔
1384

1385
        if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
5,305✔
1386
                return 0;
1387

1388
        r = link_get_network(link, &network);
4,350✔
1389
        if (r == -ENOENT) {
4,350✔
1390
                link_enter_unmanaged(link);
2,718✔
1391
                return 0;
2,718✔
1392
        }
1393
        if (r < 0)
1,632✔
1394
                return r;
1395

1396
        if (link->network == network && !FLAGS_SET(flags, LINK_RECONFIGURE_UNCONDITIONALLY))
1,632✔
1397
                return 0;
1398

1399
        _cleanup_free_ char *joined = strv_join(network->dropins, ", ");
1,554✔
1400
        if (link->network)
777✔
1401
                log_link_info(link, "Reconfiguring with %s%s%s%s.",
534✔
1402
                              network->filename,
1403
                              isempty(joined) ? "" : " (dropins: ",
1404
                              joined,
1405
                              isempty(joined) ? "" : ")");
1406
        else
1407
                log_link_info(link, "Configuring with %s%s%s%s.",
1,797✔
1408
                              network->filename,
1409
                              isempty(joined) ? "" : " (dropins: ",
1410
                              joined,
1411
                              isempty(joined) ? "" : ")");
1412

1413
        /* Dropping configurations based on the old .network file. */
1414
        r = link_drop_requests(link);
777✔
1415
        if (r < 0)
777✔
1416
                return r;
1417

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

1424
        _cleanup_(network_unrefp) Network *old_network = TAKE_PTR(link->network);
777✔
1425

1426
        /* Then, apply new .network file */
1427
        link->network = network_ref(network);
777✔
1428

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

1444
                /* Free DHCP client and friends. */
1445
                link_free_engines(link);
36✔
1446
        }
1447

1448
        link_update_operstate(link, true);
777✔
1449
        link_dirty(link);
777✔
1450

1451
        link_set_state(link, LINK_STATE_INITIALIZED);
777✔
1452
        link->activated = false;
777✔
1453

1454
        r = link_configure(link);
777✔
1455
        if (r < 0)
777✔
1456
                return r;
2✔
1457

1458
        return 1;
1459
}
1460

1461
typedef struct LinkReconfigurationData {
1462
        Manager *manager;
1463
        Link *link;
1464
        LinkReconfigurationFlag flags;
1465
        sd_bus_message *message;
1466
        unsigned *counter;
1467
} LinkReconfigurationData;
1468

1469
static LinkReconfigurationData* link_reconfiguration_data_free(LinkReconfigurationData *data) {
1,202✔
1470
        if (!data)
1,202✔
1471
                return NULL;
1472

1473
        link_unref(data->link);
1,202✔
1474
        sd_bus_message_unref(data->message);
1,202✔
1475

1476
        return mfree(data);
1,202✔
1477
}
1478

1479
DEFINE_TRIVIAL_CLEANUP_FUNC(LinkReconfigurationData*, link_reconfiguration_data_free);
1,202✔
1480

1481
static void link_reconfiguration_data_destroy_callback(LinkReconfigurationData *data) {
1,202✔
1482
        int r;
1,202✔
1483

1484
        assert(data);
1,202✔
1485

1486
        if (data->message) {
1,202✔
1487
                if (data->counter) {
1,090✔
1488
                        assert(*data->counter > 0);
1,054✔
1489
                        (*data->counter)--;
1,054✔
1490
                }
1491

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

1499
                        r = sd_bus_reply_method_return(data->message, NULL);
172✔
1500
                        if (r < 0)
172✔
1501
                                log_warning_errno(r, "Failed to reply for DBus method, ignoring: %m");
×
1502
                }
1503
        }
1504

1505
        link_reconfiguration_data_free(data);
1,202✔
1506
}
1,202✔
1507

1508
static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, LinkReconfigurationData *data) {
1,202✔
1509
        Link *link = ASSERT_PTR(ASSERT_PTR(data)->link);
1,202✔
1510
        int r;
1,202✔
1511

1512
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1,202✔
1513
        if (r <= 0)
1,202✔
1514
                return r;
1515

1516
        r = link_reconfigure_impl(link, data->flags);
1,196✔
1517
        if (r < 0) {
1,196✔
1518
                link_enter_failed(link);
×
1519
                return 0;
×
1520
        }
1521

1522
        return r;
1523
}
1524

1525
int link_reconfigure_full(Link *link, LinkReconfigurationFlag flags, sd_bus_message *message, unsigned *counter) {
1,202✔
1526
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1,202✔
1527
        _cleanup_(link_reconfiguration_data_freep) LinkReconfigurationData *data = NULL;
1,202✔
1528
        int r;
1,202✔
1529

1530
        assert(link);
1,202✔
1531
        assert(link->manager);
1,202✔
1532
        assert(link->manager->rtnl);
1,202✔
1533

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

1540
        data = new(LinkReconfigurationData, 1);
1,202✔
1541
        if (!data) {
1,202✔
1542
                r = -ENOMEM;
×
1543
                goto failed;
×
1544
        }
1545

1546
        *data = (LinkReconfigurationData) {
2,404✔
1547
                .manager = link->manager,
1,202✔
1548
                .link = link_ref(link),
1,202✔
1549
                .flags = flags,
1550
                .message = sd_bus_message_ref(message), /* message may be NULL, but _ref() works fine. */
1,202✔
1551
                .counter = counter,
1552
        };
1553

1554
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
1,202✔
1555
        if (r < 0)
1,202✔
1556
                goto failed;
×
1557

1558
        r = netlink_call_async(link->manager->rtnl, NULL, req,
1,202✔
1559
                               link_reconfigure_handler,
1560
                               link_reconfiguration_data_destroy_callback, data);
1561
        if (r < 0)
1,202✔
1562
                goto failed;
×
1563

1564
        TAKE_PTR(data);
1,202✔
1565
        if (counter)
1,202✔
1566
                (*counter)++;
1,054✔
1567

1568
        if (link->state == LINK_STATE_FAILED)
1,202✔
1569
                link_set_state(link, LINK_STATE_INITIALIZED);
6✔
1570

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

1573
failed:
1574
        log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
1575
        link_enter_failed(link);
×
1576
        return r;
1577
}
1578

1579
static int link_initialized_and_synced(Link *link) {
2,439✔
1580
        int r;
2,439✔
1581

1582
        assert(link);
2,439✔
1583
        assert(link->manager);
2,439✔
1584

1585
        if (link->state == LINK_STATE_PENDING) {
2,439✔
1586
                log_link_debug(link, "Link state is up-to-date");
2,425✔
1587
                link_set_state(link, LINK_STATE_INITIALIZED);
2,425✔
1588

1589
                r = link_new_bound_by_list(link);
2,425✔
1590
                if (r < 0)
2,425✔
1591
                        return r;
1592

1593
                r = link_handle_bound_by_list(link);
2,425✔
1594
                if (r < 0)
2,425✔
1595
                        return r;
1596
        }
1597

1598
        return link_reconfigure_impl(link, /* flags = */ 0);
2,439✔
1599
}
1600

1601
static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2,375✔
1602
        int r;
2,375✔
1603

1604
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
2,375✔
1605
        if (r <= 0)
2,375✔
1606
                return r;
1607

1608
        r = link_initialized_and_synced(link);
2,373✔
1609
        if (r < 0)
2,373✔
1610
                link_enter_failed(link);
2✔
1611

1612
        return 0;
1613
}
1614

1615
static int link_initialized(Link *link, sd_device *device) {
2,481✔
1616
        int r;
2,481✔
1617

1618
        assert(link);
2,481✔
1619
        assert(device);
2,481✔
1620

1621
        /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1622
         * or sysattrs) may be outdated. */
1623
        device_unref_and_replace(link->dev, device);
2,481✔
1624

1625
        if (link->dhcp_client) {
2,481✔
1626
                r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev);
×
1627
                if (r < 0)
×
1628
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv4 client, ignoring: %m");
×
1629
        }
1630

1631
        if (link->dhcp6_client) {
2,481✔
1632
                r = sd_dhcp6_client_attach_device(link->dhcp6_client, link->dev);
×
1633
                if (r < 0)
×
1634
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv6 client, ignoring: %m");
×
1635
        }
1636

1637
        r = link_set_sr_iov_ifindices(link);
2,481✔
1638
        if (r < 0)
2,481✔
1639
                log_link_warning_errno(link, r, "Failed to manage SR-IOV PF and VF ports, ignoring: %m");
×
1640

1641
        if (link->state != LINK_STATE_PENDING)
2,481✔
1642
                return link_reconfigure(link, /* flags = */ 0);
106✔
1643

1644
        log_link_debug(link, "udev initialized link");
2,375✔
1645

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

1651
        return link_call_getlink(link, link_initialized_handler);
2,375✔
1652
}
1653

1654
int link_check_initialized(Link *link) {
2,608✔
1655
        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
2,608✔
1656
        int r;
2,608✔
1657

1658
        assert(link);
2,608✔
1659

1660
        if (!udev_available())
2,608✔
1661
                return link_initialized_and_synced(link);
66✔
1662

1663
        /* udev should be around */
1664
        r = sd_device_new_from_ifindex(&device, link->ifindex);
2,542✔
1665
        if (r < 0) {
2,542✔
1666
                log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
1✔
1667
                return 0;
1✔
1668
        }
1669

1670
        r = device_is_processed(device);
2,541✔
1671
        if (r < 0)
2,541✔
1672
                return log_link_warning_errno(link, r, "Could not determine whether the device is processed by udevd: %m");
×
1673
        if (r == 0) {
2,541✔
1674
                /* not yet ready */
1675
                log_link_debug(link, "link pending udev initialization...");
877✔
1676
                return 0;
877✔
1677
        }
1678

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

1687
        return link_initialized(link, device);
1,664✔
1688
}
1689

1690
int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t action) {
1,828✔
1691
        int r, ifindex;
1,828✔
1692
        const char *s;
1,828✔
1693
        Link *link;
1,828✔
1694

1695
        assert(m);
1,828✔
1696
        assert(device);
1,828✔
1697

1698
        r = sd_device_get_ifindex(device, &ifindex);
1,828✔
1699
        if (r < 0)
1,828✔
1700
                return log_device_debug_errno(device, r, "Failed to get ifindex: %m");
×
1701

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

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

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

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

1736
        r = link_initialized(link, device);
817✔
1737
        if (r < 0)
817✔
1738
                link_enter_failed(link);
×
1739

1740
        return 0;
1741
}
1742

1743
static int link_carrier_gained(Link *link) {
1,177✔
1744
        LinkReconfigurationFlag flags = 0;
1,177✔
1745
        int r;
1,177✔
1746

1747
        assert(link);
1,177✔
1748

1749
        log_link_info(link, "Gained carrier");
1,177✔
1750

1751
        r = event_source_disable(link->carrier_lost_timer);
1,177✔
1752
        if (r < 0)
1,177✔
1753
                log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
×
1754

1755
        /* Process BindCarrier= setting specified by other interfaces. This is independent of the .network
1756
         * file assigned to this interface, but depends on .network files assigned to other interfaces.
1757
         * Hence, this can and should be called earlier. */
1758
        r = link_handle_bound_by_list(link);
1,177✔
1759
        if (r < 0)
1,177✔
1760
                return r;
1761

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

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

1789
        /* At this stage, both wlan and link information should be up-to-date. Hence, it is not necessary to
1790
         * call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or NL80211_CMD_GET_STATION commands, and simply call
1791
         * link_reconfigure_impl(). Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1792
        r = link_reconfigure_impl(link, flags);
1,177✔
1793
        if (r != 0)
1,177✔
1794
                return r;
1795

1796
        if (link->iftype == ARPHRD_CAN)
1,177✔
1797
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1798
                return 0;
1799

1800
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
1,173✔
1801
                r = link_acquire_dynamic_conf(link);
615✔
1802
                if (r < 0)
615✔
1803
                        return r;
1804

1805
                r = link_request_static_configs(link);
615✔
1806
                if (r < 0)
615✔
1807
                        return r;
×
1808
        }
1809

1810
        return 0;
1811
}
1812

1813
static int link_carrier_lost_impl(Link *link) {
677✔
1814
        int ret = 0;
677✔
1815

1816
        assert(link);
677✔
1817

1818
        link->previous_ssid = mfree(link->previous_ssid);
677✔
1819

1820
        ret = link_handle_bound_by_list(link);
677✔
1821

1822
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
677✔
1823
                return ret;
677✔
1824

1825
        if (!link->network)
677✔
1826
                return ret;
1827

1828
        RET_GATHER(ret, link_stop_engines(link, /* may_keep_dynamic = */ false));
602✔
1829
        RET_GATHER(ret, link_drop_static_config(link));
602✔
1830

1831
        return ret;
1832
}
1833

1834
static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
×
1835
        Link *link = ASSERT_PTR(userdata);
×
1836
        int r;
×
1837

1838
        r = link_carrier_lost_impl(link);
×
1839
        if (r < 0) {
×
1840
                log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
×
1841
                link_enter_failed(link);
×
1842
        }
1843

1844
        return 0;
×
1845
}
1846

1847
static int link_carrier_lost(Link *link) {
693✔
1848
        uint16_t dhcp_mtu;
693✔
1849
        usec_t usec;
693✔
1850

1851
        assert(link);
693✔
1852

1853
        log_link_info(link, "Lost carrier");
693✔
1854

1855
        if (link->iftype == ARPHRD_CAN)
693✔
1856
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1857
                usec = 0;
1858

1859
        else if (!link->network)
689✔
1860
                usec = 0;
1861

1862
        else if (link->network->ignore_carrier_loss_set)
614✔
1863
                /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1864
                usec = link->network->ignore_carrier_loss_usec;
12✔
1865

1866
        else if (link->network->bond && link->wlan_iftype > 0)
602✔
1867
                /* Enslaving wlan interface to a bond disconnects from the connected AP, and causes its
1868
                 * carrier to be lost. See #19832. */
1869
                usec = 3 * USEC_PER_SEC;
1870

1871
        else if (link->network->dhcp_use_mtu &&
602✔
1872
                 link->dhcp_lease &&
4✔
1873
                 sd_dhcp_lease_get_mtu(link->dhcp_lease, &dhcp_mtu) >= 0 &&
2✔
1874
                 dhcp_mtu != link->original_mtu)
1✔
1875
                /* Some drivers reset interfaces when changing MTU. Resetting interfaces by the static
1876
                 * MTU should not cause any issues, as MTU is changed only once. However, setting MTU
1877
                 * through DHCP lease causes an infinite loop of resetting the interface. See #18738. */
1878
                usec = 5 * USEC_PER_SEC;
1879

1880
        else
1881
                /* Otherwise, use the implied default value. */
1882
                usec = link->network->ignore_carrier_loss_usec;
601✔
1883

1884
        if (usec == USEC_INFINITY)
613✔
1885
                return 0;
693✔
1886

1887
        if (usec == 0)
678✔
1888
                return link_carrier_lost_impl(link);
677✔
1889

1890
        return event_reset_time_relative(link->manager->event,
1✔
1891
                                         &link->carrier_lost_timer,
1892
                                         CLOCK_BOOTTIME,
1893
                                         usec,
1894
                                         0,
1895
                                         link_carrier_lost_handler,
1896
                                         link,
1897
                                         0,
1898
                                         "link-carrier-loss",
1899
                                         true);
1900
}
1901

1902
static int link_admin_state_up(Link *link) {
1,148✔
1903
        assert(link);
1,148✔
1904

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

1908
        log_link_info(link, "Link UP");
1,148✔
1909

1910
        if (!link->network)
1,148✔
1911
                return 0;
1912

1913
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
589✔
1914
                log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
4✔
1915
                return link_request_to_bring_up_or_down(link, /* up = */ false);
4✔
1916
        }
1917

1918
        /* We set the ipv6 mtu after the device mtu, but the kernel resets
1919
         * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1920
        (void) link_set_ipv6_mtu(link, LOG_INFO);
585✔
1921

1922
        return 0;
585✔
1923
}
1924

1925
static int link_admin_state_down(Link *link) {
663✔
1926
        assert(link);
663✔
1927

1928
        log_link_info(link, "Link DOWN");
663✔
1929

1930
        link_forget_nexthops(link);
663✔
1931
        link_forget_routes(link);
663✔
1932

1933
        if (!link->network)
663✔
1934
                return 0;
1935

1936
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
590✔
1937
                log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
9✔
1938
                return link_request_to_bring_up_or_down(link, /* up = */ true);
9✔
1939
        }
1940

1941
        return 0;
1942
}
1943

1944
static bool link_is_enslaved(Link *link) {
6,613✔
1945
        if (link->flags & IFF_SLAVE)
6,613✔
1946
                return true;
1947

1948
        if (link->master_ifindex > 0)
6,572✔
1949
                return true;
206✔
1950

1951
        return false;
1952
}
1953

1954
void link_update_operstate(Link *link, bool also_update_master) {
12,432✔
1955
        LinkOperationalState operstate;
12,432✔
1956
        LinkCarrierState carrier_state;
12,432✔
1957
        LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
12,432✔
1958
        LinkOnlineState online_state;
12,432✔
1959
        _cleanup_strv_free_ char **p = NULL;
12,432✔
1960
        bool changed = false;
12,432✔
1961

1962
        assert(link);
12,432✔
1963

1964
        if (link->kernel_operstate == IF_OPER_DORMANT)
12,432✔
1965
                carrier_state = LINK_CARRIER_STATE_DORMANT;
1966
        else if (link_has_carrier(link)) {
12,432✔
1967
                if (link_is_enslaved(link))
6,613✔
1968
                        carrier_state = LINK_CARRIER_STATE_ENSLAVED;
1969
                else
1970
                        carrier_state = LINK_CARRIER_STATE_CARRIER;
1971
        } else if (link->flags & IFF_UP)
5,819✔
1972
                carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
1973
        else
1974
                carrier_state = LINK_CARRIER_STATE_OFF;
1975

1976
        if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
1977
                Link *slave;
6,613✔
1978

1979
                SET_FOREACH(slave, link->slaves) {
6,840✔
1980
                        link_update_operstate(slave, false);
227✔
1981

1982
                        if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
227✔
1983
                                carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
64✔
1984
                }
1985
        }
1986

1987
        link_get_address_states(link, &ipv4_address_state, &ipv6_address_state, &address_state);
12,432✔
1988

1989
        /* Mapping of address and carrier state vs operational state
1990
         *                                                     carrier state
1991
         *                          | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
1992
         *                 ------------------------------------------------------------------------------
1993
         *                 off      | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
1994
         * address_state   degraded | off | no-carrier | dormant | degraded         | degraded | enslaved
1995
         *                 routable | off | no-carrier | dormant | routable         | routable | routable
1996
         */
1997

1998
        if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_ROUTABLE)
12,432✔
1999
                operstate = LINK_OPERSTATE_ROUTABLE;
2000
        else if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_DEGRADED)
33✔
2001
                operstate = LINK_OPERSTATE_DEGRADED;
2002
        else if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
12,403✔
2003
                operstate = (LinkOperationalState) carrier_state;
2004
        else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
4,299✔
2005
                operstate = LINK_OPERSTATE_ROUTABLE;
2006
        else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1,294✔
2007
                operstate = LINK_OPERSTATE_DEGRADED;
2008
        else
2009
                operstate = LINK_OPERSTATE_ENSLAVED;
14✔
2010

2011
        LinkOperationalStateRange req;
12,432✔
2012
        link_required_operstate_for_online(link, &req);
12,432✔
2013

2014
        /* Only determine online state for managed links with RequiredForOnline=yes */
2015
        if (!link->network || !link->network->required_for_online)
12,432✔
2016
                online_state = _LINK_ONLINE_STATE_INVALID;
2017

2018
        else if (!operational_state_is_in_range(operstate, &req))
8,147✔
2019
                online_state = LINK_ONLINE_STATE_OFFLINE;
2020

2021
        else {
2022
                AddressFamily required_family = link_required_family_for_online(link);
4,222✔
2023
                bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
4,222✔
2024
                bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
4,222✔
2025

2026
                /* The operational state is within the range required for online.
2027
                 * If a particular address family is also required, we might revert
2028
                 * to offline in the blocks below. */
2029
                online_state = LINK_ONLINE_STATE_ONLINE;
4,222✔
2030

2031
                if (req.min >= LINK_OPERSTATE_DEGRADED) {
4,222✔
2032
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
4,043✔
2033
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2034
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
4,043✔
2035
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2036
                }
2037

2038
                if (req.min >= LINK_OPERSTATE_ROUTABLE) {
4,222✔
2039
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2040
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2041
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2042
                                online_state = LINK_ONLINE_STATE_OFFLINE;
3✔
2043
                }
2044
        }
2045

2046
        if (link->carrier_state != carrier_state) {
12,432✔
2047
                link->carrier_state = carrier_state;
4,309✔
2048
                changed = true;
4,309✔
2049
                if (strv_extend(&p, "CarrierState") < 0)
4,309✔
2050
                        log_oom();
×
2051
        }
2052

2053
        if (link->address_state != address_state) {
12,432✔
2054
                link->address_state = address_state;
1,817✔
2055
                changed = true;
1,817✔
2056
                if (strv_extend(&p, "AddressState") < 0)
1,817✔
2057
                        log_oom();
×
2058
        }
2059

2060
        if (link->ipv4_address_state != ipv4_address_state) {
12,432✔
2061
                link->ipv4_address_state = ipv4_address_state;
845✔
2062
                changed = true;
845✔
2063
                if (strv_extend(&p, "IPv4AddressState") < 0)
845✔
2064
                        log_oom();
×
2065
        }
2066

2067
        if (link->ipv6_address_state != ipv6_address_state) {
12,432✔
2068
                link->ipv6_address_state = ipv6_address_state;
1,549✔
2069
                changed = true;
1,549✔
2070
                if (strv_extend(&p, "IPv6AddressState") < 0)
1,549✔
2071
                        log_oom();
×
2072
        }
2073

2074
        if (link->operstate != operstate) {
12,432✔
2075
                link->operstate = operstate;
5,238✔
2076
                changed = true;
5,238✔
2077
                if (strv_extend(&p, "OperationalState") < 0)
5,238✔
2078
                        log_oom();
×
2079
        }
2080

2081
        if (link->online_state != online_state) {
12,432✔
2082
                link->online_state = online_state;
1,670✔
2083
                changed = true;
1,670✔
2084
                if (strv_extend(&p, "OnlineState") < 0)
1,670✔
2085
                        log_oom();
×
2086
        }
2087

2088
        if (p)
12,432✔
2089
                link_send_changed_strv(link, p);
7,269✔
2090
        if (changed)
12,432✔
2091
                link_dirty(link);
7,269✔
2092

2093
        if (also_update_master) {
12,432✔
2094
                Link *master;
12,205✔
2095

2096
                if (link_get_master(link, &master) >= 0)
12,205✔
2097
                        link_update_operstate(master, true);
150✔
2098
        }
2099
}
12,432✔
2100

2101
#define FLAG_STRING(string, flag, old, new)                      \
2102
        (((old ^ new) & flag)                                    \
2103
         ? ((old & flag) ? (" -" string) : (" +" string))        \
2104
         : "")
2105

2106
static int link_update_flags(Link *link, sd_netlink_message *message) {
9,685✔
2107
        bool link_was_admin_up, had_carrier;
9,685✔
2108
        uint8_t operstate;
9,685✔
2109
        unsigned flags;
9,685✔
2110
        int r;
9,685✔
2111

2112
        assert(link);
9,685✔
2113
        assert(message);
9,685✔
2114

2115
        r = sd_rtnl_message_link_get_flags(message, &flags);
9,685✔
2116
        if (r < 0)
9,685✔
2117
                return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
×
2118

2119
        r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
9,685✔
2120
        if (r == -ENODATA)
9,685✔
2121
                /* If we got a message without operstate, assume the state was unchanged. */
2122
                operstate = link->kernel_operstate;
×
2123
        else if (r < 0)
9,685✔
2124
                return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
×
2125

2126
        if (link->flags == flags && link->kernel_operstate == operstate)
9,685✔
2127
                return 0;
2128

2129
        if (link->flags != flags) {
4,304✔
2130
                unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
4,303✔
2131

2132
                log_link_debug(link, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
11,886✔
2133
                               FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
2134
                               FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
2135
                               FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
2136
                               FLAG_STRING("UP", IFF_UP, link->flags, flags),
2137
                               FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
2138
                               FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
2139
                               FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
2140
                               FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
2141
                               FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
2142
                               FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
2143
                               FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
2144
                               FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
2145
                               FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
2146
                               FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
2147
                               FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
2148
                               FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
2149
                               FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
2150
                               FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
2151
                               FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
2152

2153
                unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
4,303✔
2154
                                  IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
2155
                                  IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
2156
                                  IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
2157
                                  IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
2158
                                  IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
2159
                unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
4,303✔
2160
                unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
4,303✔
2161

2162
                if (unknown_flags_added)
4,303✔
2163
                        log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
×
2164

2165
                if (unknown_flags_removed)
4,303✔
2166
                        log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
×
2167
        }
2168

2169
        link_was_admin_up = link->flags & IFF_UP;
4,304✔
2170
        had_carrier = link_has_carrier(link);
4,304✔
2171

2172
        link->flags = flags;
4,304✔
2173
        link->kernel_operstate = operstate;
4,304✔
2174

2175
        link_update_operstate(link, true);
4,304✔
2176

2177
        r = 0;
4,304✔
2178

2179
        if (!link_was_admin_up && (link->flags & IFF_UP))
4,304✔
2180
                r = link_admin_state_up(link);
1,148✔
2181
        else if (link_was_admin_up && !(link->flags & IFF_UP))
1,022✔
2182
                r = link_admin_state_down(link);
663✔
2183
        if (r < 0)
1,811✔
2184
                return r;
2185

2186
        if (!had_carrier && link_has_carrier(link))
4,304✔
2187
                r = link_carrier_gained(link);
1,177✔
2188
        else if (had_carrier && !link_has_carrier(link))
704✔
2189
                link_carrier_lost(link);
693✔
2190
        if (r < 0)
4,304✔
2191
                return r;
×
2192

2193
        return 0;
2194
}
2195

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

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

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

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

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

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

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

2228
        return 0;
2229
}
2230

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

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

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

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

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

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

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

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

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

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

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

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

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

2280
        assert(link);
5,097✔
2281
        assert(link->manager);
5,097✔
2282
        assert(message);
5,097✔
2283

2284
        if (link->ethtool_permanent_hw_addr_read)
5,097✔
2285
                return 0;
2286

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

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

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

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

2306
        return 0;
2307
}
2308

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2442
        if (max_mtu == 0)
9,664✔
2443
                max_mtu = UINT32_MAX;
6,389✔
2444

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

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

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

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

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

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

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

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

2480
        return 0;
2481
}
2482

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2663
        return needs_reconfigure;
9,685✔
2664
}
2665

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

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

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

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

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

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

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

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

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

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

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

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

2734
                .bridge_vlan_pvid = UINT16_MAX,
2735

2736
                .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2737

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

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

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

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

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

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

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

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

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

2782
        assert(rtnl);
6,011✔
2783
        assert(message);
6,011✔
2784
        assert(manager);
6,011✔
2785

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

2791
                return 0;
×
2792
        }
2793

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2902
        return 1;
2903
}
2904

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

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

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

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

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

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

2939
        return 1;
2940
}
2941

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

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

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

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

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

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

2974
DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
30,130✔
2975

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

3000
        assert(ret);
551✔
3001

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

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

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

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

© 2026 Coveralls, Inc