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

systemd / systemd / 23927985597

02 Apr 2026 07:45PM UTC coverage: 72.362% (+0.02%) from 72.343%
23927985597

push

github

daandemeyer
ci: Drop base64 encoding in claude review workflow

Doesn't seem to work nearly as good as the previous solution which
just told claude not to escape stuff.

319121 of 441004 relevant lines covered (72.36%)

1167673.48 hits per line

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

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

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

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

23
#include "alloc-util.h"
24
#include "arphrd-util.h"
25
#include "bitfield.h"
26
#include "device-util.h"
27
#include "dns-domain.h"
28
#include "errno-util.h"
29
#include "ethtool-util.h"
30
#include "event-util.h"
31
#include "format-ifname.h"
32
#include "fs-util.h"
33
#include "glyph-util.h"
34
#include "logarithm.h"
35
#include "netif-util.h"
36
#include "netlink-util.h"
37
#include "networkd-address.h"
38
#include "networkd-address-label.h"
39
#include "networkd-bridge-fdb.h"
40
#include "networkd-bridge-mdb.h"
41
#include "networkd-bridge-vlan.h"
42
#include "networkd-dhcp-prefix-delegation.h"
43
#include "networkd-dhcp-server.h"
44
#include "networkd-dhcp4.h"
45
#include "networkd-dhcp6.h"
46
#include "networkd-ipv4acd.h"
47
#include "networkd-ipv4ll.h"
48
#include "networkd-ipv6-proxy-ndp.h"
49
#include "networkd-link.h"
50
#include "networkd-link-bus.h"
51
#include "networkd-lldp-tx.h"
52
#include "networkd-manager.h"
53
#include "networkd-ndisc.h"
54
#include "networkd-neighbor.h"
55
#include "networkd-nexthop.h"
56
#include "networkd-queue.h"
57
#include "networkd-radv.h"
58
#include "networkd-resolve-hook.h"
59
#include "networkd-route.h"
60
#include "networkd-route-util.h"
61
#include "networkd-routing-policy-rule.h"
62
#include "networkd-setlink.h"
63
#include "networkd-sriov.h"
64
#include "networkd-state-file.h"
65
#include "networkd-sysctl.h"
66
#include "networkd-wifi.h"
67
#include "networkd-wwan-bus.h"
68
#include "ordered-set.h"
69
#include "parse-util.h"
70
#include "set.h"
71
#include "socket-util.h"
72
#include "string-table.h"
73
#include "string-util.h"
74
#include "strv.h"
75
#include "tc.h"
76
#include "udev-util.h"
77

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

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

107
AddressFamily link_required_family_for_online(Link *link) {
9,882✔
108
        assert(link);
9,882✔
109

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

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

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

122
        return ADDRESS_FAMILY_NO;
123
}
124

125
bool link_ipv6_enabled(Link *link) {
913✔
126
        assert(link);
913✔
127

128
        if (!socket_ipv6_is_supported())
913✔
129
                return false;
130

131
        if (link->iftype == ARPHRD_CAN)
913✔
132
                return false;
133

134
        if (!link->network)
911✔
135
                return false;
136

137
        if (link->network->bond)
911✔
138
                return false;
139

140
        if (link_ipv6ll_enabled(link))
900✔
141
                return true;
142

143
        if (network_has_static_ipv6_configurations(link->network))
111✔
144
                return true;
44✔
145

146
        return false;
147
}
148

149
bool link_has_ipv6_connectivity(Link *link) {
307✔
150
        LinkAddressState ipv6_address_state;
307✔
151

152
        assert(link);
307✔
153

154
        link_get_address_states(link, NULL, &ipv6_address_state, NULL);
307✔
155

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

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

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

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

175
static bool link_is_ready_to_configure_one(Link *link, bool allow_unmanaged) {
64,981✔
176
        assert(link);
64,981✔
177

178
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
64,981✔
179
                return false;
180

181
        if (!link->network)
64,949✔
182
                return allow_unmanaged;
183

184
        if (!link->network->configure_without_carrier) {
64,948✔
185
                if (link->set_flags_messages > 0)
64,663✔
186
                        return false;
187

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

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

195
        if (!link->activated)
12,896✔
196
                return false;
76✔
197

198
        return true;
199
}
200

201
bool link_is_ready_to_configure(Link *link, bool allow_unmanaged) {
64,981✔
202
        return check_ready_for_all_sr_iov_ports(link, allow_unmanaged, link_is_ready_to_configure_one);
64,981✔
203
}
204

205
bool link_is_ready_to_configure_by_name(Manager *manager, const char *name, bool allow_unmanaged) {
1,743✔
206
        assert(manager);
1,743✔
207
        assert(name);
1,743✔
208

209
        Link *link;
1,743✔
210
        if (link_get_by_name(manager, name, &link) < 0)
1,743✔
211
                return false;
1,743✔
212

213
        return link_is_ready_to_configure(link, allow_unmanaged);
1,743✔
214
}
215

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

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

227
        link->search_domains = ordered_set_free(link->search_domains);
2,866✔
228
        link->route_domains = ordered_set_free(link->route_domains);
2,866✔
229

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

236
        link->dnssec_negative_trust_anchors = set_free(link->dnssec_negative_trust_anchors);
2,866✔
237
}
2,866✔
238

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

243
        link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
4,890✔
244

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

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

252
        link->ipv4acd_by_address = hashmap_free(link->ipv4acd_by_address);
4,890✔
253

254
        link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
4,890✔
255

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

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

263
        link->radv = sd_radv_unref(link->radv);
4,890✔
264
}
265

266
static Link* link_free(Link *link) {
2,849✔
267
        assert(link);
2,849✔
268

269
        (void) link_clear_sysctl_shadows(link);
2,849✔
270

271
        link_ntp_settings_clear(link);
2,849✔
272
        link_dns_settings_clear(link);
2,849✔
273

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

279
        link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
2,849✔
280

281
        link_free_engines(link);
2,849✔
282

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

291
        unlink_and_free(link->lease_file);
2,849✔
292
        unlink_and_free(link->state_file);
2,849✔
293

294
        sd_device_unref(link->dev);
2,849✔
295
        netdev_unref(link->netdev);
2,849✔
296

297
        hashmap_free(link->bound_to_links);
2,849✔
298
        hashmap_free(link->bound_by_links);
2,849✔
299

300
        set_free(link->slaves);
2,849✔
301

302
        network_unref(link->network);
2,849✔
303

304
        sd_event_source_disable_unref(link->carrier_lost_timer);
2,849✔
305
        sd_event_source_disable_unref(link->ipv6_mtu_wait_synced_event_source);
2,849✔
306

307
        return mfree(link);
2,849✔
308
}
309

310
DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
187,904✔
311

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

317
int link_get_by_index(Manager *m, int ifindex, Link **ret) {
68,505✔
318
        Link *link;
68,505✔
319

320
        assert(m);
68,505✔
321

322
        if (ifindex <= 0)
68,505✔
323
                return -EINVAL;
324

325
        link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
67,767✔
326
        if (!link)
67,767✔
327
                return -ENODEV;
328

329
        if (ret)
63,850✔
330
                *ret = link;
63,850✔
331
        return 0;
332
}
333

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

337
        assert(m);
2,168✔
338
        assert(ifname);
2,168✔
339

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

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

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

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

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

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

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

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

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

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

378
        if (link->state == state)
9,412✔
379
                return;
1,822✔
380

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

385
        link->state = state;
7,590✔
386

387
        link_send_changed(link, "AdministrativeState");
7,590✔
388
        link_dirty(link);
7,590✔
389
}
390

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

394
        assert(link);
4,694✔
395
        assert(link->manager);
4,694✔
396
        assert(link->manager->event);
4,694✔
397

398
        bool keep_dynamic =
14,037✔
399
                may_keep_dynamic &&
1,971✔
400
                link->network &&
4,694✔
401
                (link->manager->state == MANAGER_RESTARTING ||
58✔
402
                 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC_ON_STOP));
15✔
403

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

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

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

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

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

425
                ndisc_flush(link);
4,649✔
426
        }
427

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

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

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

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

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

448
        return ret;
4,694✔
449
}
450

451
void link_enter_failed(Link *link) {
11✔
452
        assert(link);
11✔
453

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

457
        log_link_warning(link, "Failed");
11✔
458

459
        link_set_state(link, LINK_STATE_FAILED);
11✔
460

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

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

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

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

477
        assert(link);
27,276✔
478

479
        if (link->state == LINK_STATE_CONFIGURED)
27,276✔
480
                return;
26,388✔
481

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

565
        bool ipv4ll_ready =
2,508✔
566
                link_ipv4ll_enabled(link) && link->ipv4ll_address_configured &&
1,308✔
567
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_IPV4LL);
54✔
568
        bool dhcp4_ready =
1,254✔
569
                link_dhcp4_enabled(link) && link->dhcp4_configured &&
1,348✔
570
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP4);
94✔
571
        bool dhcp6_ready =
1,254✔
572
                link_dhcp6_enabled(link) && link->dhcp6_configured &&
1,254✔
573
                (!link->network->dhcp6_use_address ||
50✔
574
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP6));
24✔
575
        bool dhcp_pd_ready =
2,508✔
576
                link_dhcp_pd_is_enabled(link) && link->dhcp_pd_configured &&
1,254✔
577
                (!link->network->dhcp_pd_assign ||
161✔
578
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP_PD));
77✔
579
        bool ndisc_ready =
2,508✔
580
                link_ndisc_enabled(link) && link->ndisc_configured &&
1,254✔
581
                (!link->network->ndisc_use_autonomous_prefix ||
448✔
582
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_NDISC));
224✔
583

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

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

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

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

615
ready:
888✔
616
        link_set_state(link, LINK_STATE_CONFIGURED);
888✔
617
}
618

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

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

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

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

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

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

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

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

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

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

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

662
        return 0;
663
}
664

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

669
        assert(link);
11,979✔
670

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

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

676
        link->stacked_netdevs_created = false;
2,663✔
677

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

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

687
        if (link->create_stacked_netdev_messages == 0) {
2,663✔
688
                link->stacked_netdevs_created = true;
2,487✔
689
                link_check_ready(link);
2,487✔
690
        }
691

692
        return 0;
693
}
694

695
static int link_acquire_dynamic_ipv6_conf(Link *link) {
799✔
696
        int r;
799✔
697

698
        assert(link);
799✔
699

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

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

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

712
        return 0;
713
}
714

715
static int link_acquire_dynamic_ipv4_conf(Link *link) {
951✔
716
        int r;
951✔
717

718
        assert(link);
951✔
719
        assert(link->manager);
951✔
720
        assert(link->manager->event);
951✔
721

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

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

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

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

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

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

751
        return 0;
752
}
753

754
static int link_acquire_dynamic_conf(Link *link) {
951✔
755
        int r;
951✔
756

757
        assert(link);
951✔
758
        assert(link->network);
951✔
759

760
        r = link_acquire_dynamic_ipv4_conf(link);
951✔
761
        if (r < 0)
951✔
762
                return r;
763

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

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

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

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

792
        return 0;
793
}
794

795
int link_ipv6ll_gained(Link *link) {
646✔
796
        int r;
646✔
797

798
        assert(link);
646✔
799

800
        log_link_info(link, "Gained IPv6LL");
646✔
801

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

805
        r = link_acquire_dynamic_ipv6_conf(link);
521✔
806
        if (r < 0)
521✔
807
                return r;
808

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

813
        link_check_ready(link);
521✔
814
        return 0;
521✔
815
}
816

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

821
        assert(link);
34✔
822

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

826
        if (hashmap_isempty(link->bound_to_links))
34✔
827
                return 0;
34✔
828

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

835
        if (!required_up && link_is_up(link))
29✔
836
                return link_request_to_bring_up_or_down(link, /* up= */ false);
4✔
837
        if (required_up && !link_is_up(link))
21✔
838
                return link_request_to_bring_up_or_down(link, /* up= */ true);
11✔
839

840
        return 0;
841
}
842

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

847
        assert(link);
4,679✔
848

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

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

857
        return 0;
4,679✔
858
}
859

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

863
        assert(link);
28✔
864
        assert(carrier);
28✔
865
        assert(h);
28✔
866

867
        if (link == carrier)
28✔
868
                return 0;
869

870
        if (hashmap_contains(*h, INT_TO_PTR(carrier->ifindex)))
28✔
871
                return 0;
872

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

877
        link_dirty(link);
26✔
878

879
        return 0;
26✔
880
}
881

882
static int link_new_bound_by_list(Link *link) {
2,660✔
883
        Manager *m;
2,660✔
884
        Link *carrier;
2,660✔
885
        int r;
2,660✔
886

887
        assert(link);
2,660✔
888
        assert(link->manager);
2,660✔
889

890
        m = link->manager;
2,660✔
891

892
        HASHMAP_FOREACH(carrier, m->links_by_index) {
18,654✔
893
                if (!carrier->network)
15,994✔
894
                        continue;
15,146✔
895

896
                if (strv_isempty(carrier->network->bind_carrier))
848✔
897
                        continue;
841✔
898

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

906
        HASHMAP_FOREACH(carrier, link->bound_by_links) {
2,666✔
907
                r = link_put_carrier(carrier, link, &carrier->bound_to_links);
6✔
908
                if (r < 0)
6✔
909
                        return r;
×
910
        }
911

912
        return 0;
2,660✔
913
}
914

915
static int link_new_bound_to_list(Link *link) {
867✔
916
        Manager *m;
867✔
917
        Link *carrier;
867✔
918
        int r;
867✔
919

920
        assert(link);
867✔
921
        assert(link->manager);
867✔
922

923
        if (!link->network)
867✔
924
                return 0;
867✔
925

926
        if (strv_isempty(link->network->bind_carrier))
867✔
927
                return 0;
928

929
        m = link->manager;
8✔
930

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

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

945
        return 0;
8✔
946
}
947

948
static void link_free_bound_to_list(Link *link) {
3,747✔
949
        bool updated = false;
3,747✔
950
        Link *bound_to;
3,747✔
951

952
        assert(link);
3,747✔
953

954
        while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
3,752✔
955
                updated = true;
5✔
956

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

961
        if (updated)
3,747✔
962
                link_dirty(link);
5✔
963
}
3,747✔
964

965
static void link_free_bound_by_list(Link *link) {
877✔
966
        bool updated = false;
877✔
967
        Link *bound_by;
877✔
968

969
        assert(link);
877✔
970

971
        while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
883✔
972
                updated = true;
6✔
973

974
                if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
6✔
975
                        link_dirty(bound_by);
6✔
976
                        link_handle_bound_to_list(bound_by);
6✔
977
                }
978
        }
979

980
        if (updated)
877✔
981
                link_dirty(link);
6✔
982
}
877✔
983

984
static int link_append_to_master(Link *link) {
10,763✔
985
        Link *master;
10,763✔
986
        int r;
10,763✔
987

988
        assert(link);
10,763✔
989

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

995
        r = set_ensure_put(&master->slaves, &link_hash_ops, link);
503✔
996
        if (r <= 0)
503✔
997
                return r;
998

999
        link_ref(link);
46✔
1000
        return 0;
1001
}
1002

1003
static void link_drop_from_master(Link *link) {
933✔
1004
        Link *master;
933✔
1005

1006
        assert(link);
933✔
1007

1008
        if (!link->manager)
933✔
1009
                return;
890✔
1010

1011
        if (link_get_master(link, &master) < 0)
933✔
1012
                return;
1013

1014
        link_unref(set_remove(master->slaves, link));
43✔
1015
}
1016

1017
static int link_drop_requests(Link *link) {
3,747✔
1018
        Request *req;
3,747✔
1019
        int ret = 0;
3,747✔
1020

1021
        assert(link);
3,747✔
1022
        assert(link->manager);
3,747✔
1023

1024
        ORDERED_SET_FOREACH(req, link->manager->request_queue) {
5,763✔
1025
                if (req->link != link)
2,016✔
1026
                        continue;
1,933✔
1027

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

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

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

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

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

1065
                request_detach(req);
83✔
1066
        }
1067

1068
        return ret;
3,747✔
1069
}
1070

1071
static Link *link_drop(Link *link) {
900✔
1072
        if (!link)
900✔
1073
                return NULL;
1074

1075
        assert(link->manager);
877✔
1076

1077
        bool notify = link_has_local_lease_domain(link);
877✔
1078

1079
        link_set_state(link, LINK_STATE_LINGER);
877✔
1080

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

1084
        (void) link_drop_requests(link);
877✔
1085

1086
        link_free_bound_to_list(link);
877✔
1087
        link_free_bound_by_list(link);
877✔
1088

1089
        link_clear_sr_iov_ifindices(link);
877✔
1090

1091
        link_drop_from_master(link);
877✔
1092

1093
        if (link->state_file)
877✔
1094
                (void) unlink(link->state_file);
877✔
1095

1096
        link_clean(link);
877✔
1097

1098
        STRV_FOREACH(n, link->alternative_names)
911✔
1099
                hashmap_remove(link->manager->links_by_name, *n);
34✔
1100
        hashmap_remove(link->manager->links_by_name, link->ifname);
877✔
1101

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

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

1108
        if (notify)
877✔
1109
                manager_notify_hook_filters(link->manager);
3✔
1110

1111
        return link_unref(link);
877✔
1112
}
1113

1114
static int link_drop_unmanaged_config(Link *link) {
867✔
1115
        int r;
867✔
1116

1117
        assert(link);
867✔
1118
        assert(link->state == LINK_STATE_CONFIGURING);
867✔
1119
        assert(link->manager);
867✔
1120

1121
        r = link_drop_unmanaged_routes(link);
867✔
1122
        RET_GATHER(r, link_drop_unmanaged_nexthops(link));
867✔
1123
        RET_GATHER(r, link_drop_unmanaged_addresses(link));
867✔
1124
        RET_GATHER(r, link_drop_unmanaged_neighbors(link));
867✔
1125
        RET_GATHER(r, link_drop_unmanaged_routing_policy_rules(link));
867✔
1126

1127
        return r;
867✔
1128
}
1129

1130
static int link_drop_static_config(Link *link) {
2,682✔
1131
        int r;
2,682✔
1132

1133
        assert(link);
2,682✔
1134
        assert(link->manager);
2,682✔
1135

1136
        r = link_drop_static_routes(link);
2,682✔
1137
        RET_GATHER(r, link_drop_static_nexthops(link));
2,682✔
1138
        RET_GATHER(r, link_drop_static_addresses(link));
2,682✔
1139
        RET_GATHER(r, link_drop_static_neighbors(link));
2,682✔
1140
        RET_GATHER(r, link_drop_static_routing_policy_rules(link));
2,682✔
1141

1142
        return r;
2,682✔
1143
}
1144

1145
static int link_drop_dynamic_config(Link *link, Network *network) {
829✔
1146
        int r;
829✔
1147

1148
        assert(link);
829✔
1149
        assert(link->network);
829✔
1150

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

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

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

1173
        return r;
829✔
1174
}
1175

1176
static int link_configure(Link *link) {
867✔
1177
        int r;
867✔
1178

1179
        assert(link);
867✔
1180
        assert(link->network);
867✔
1181
        assert(link->state == LINK_STATE_INITIALIZED);
867✔
1182

1183
        link_set_state(link, LINK_STATE_CONFIGURING);
867✔
1184

1185
        r = link_drop_unmanaged_config(link);
867✔
1186
        if (r < 0)
867✔
1187
                return r;
1188

1189
        r = link_new_bound_to_list(link);
867✔
1190
        if (r < 0)
867✔
1191
                return r;
1192

1193
        r = link_request_traffic_control(link);
867✔
1194
        if (r < 0)
867✔
1195
                return r;
1196

1197
        r = link_configure_mtu(link);
867✔
1198
        if (r < 0)
867✔
1199
                return r;
1200

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

1207
                return link_request_to_activate(link);
6✔
1208
        }
1209

1210
        r = link_request_sr_iov_vfs(link);
861✔
1211
        if (r < 0)
861✔
1212
                return r;
1213

1214
        r = link_set_sysctl(link);
861✔
1215
        if (r < 0)
861✔
1216
                return r;
1217

1218
        r = link_request_to_set_mac(link, /* allow_retry= */ true);
861✔
1219
        if (r < 0)
861✔
1220
                return r;
1221

1222
        r = link_request_to_set_ipoib(link);
861✔
1223
        if (r < 0)
861✔
1224
                return r;
1225

1226
        r = link_request_to_set_flags(link);
861✔
1227
        if (r < 0)
861✔
1228
                return r;
1229

1230
        r = link_request_to_set_group(link);
861✔
1231
        if (r < 0)
861✔
1232
                return r;
1233

1234
        r = link_request_to_set_addrgen_mode(link);
861✔
1235
        if (r < 0)
861✔
1236
                return r;
1237

1238
        r = link_request_to_set_master(link);
861✔
1239
        if (r < 0)
861✔
1240
                return r;
1241

1242
        r = link_request_stacked_netdevs(link, _NETDEV_LOCAL_ADDRESS_TYPE_INVALID);
861✔
1243
        if (r < 0)
861✔
1244
                return r;
1245

1246
        r = link_request_to_set_bond(link);
861✔
1247
        if (r < 0)
861✔
1248
                return r;
1249

1250
        r = link_request_to_set_bridge(link);
861✔
1251
        if (r < 0)
861✔
1252
                return r;
1253

1254
        r = link_request_to_set_bridge_vlan(link);
861✔
1255
        if (r < 0)
861✔
1256
                return r;
1257

1258
        r = link_request_to_activate(link);
861✔
1259
        if (r < 0)
861✔
1260
                return r;
1261

1262
        r = ipv4ll_configure(link);
861✔
1263
        if (r < 0)
861✔
1264
                return r;
1265

1266
        r = link_request_dhcp4_client(link);
861✔
1267
        if (r < 0)
861✔
1268
                return r;
1269

1270
        r = link_request_dhcp6_client(link);
861✔
1271
        if (r < 0)
861✔
1272
                return r;
1273

1274
        r = link_request_ndisc(link);
861✔
1275
        if (r < 0)
861✔
1276
                return r;
1277

1278
        r = link_request_dhcp_server(link);
861✔
1279
        if (r < 0)
861✔
1280
                return r;
1281

1282
        r = link_request_radv(link);
861✔
1283
        if (r < 0)
861✔
1284
                return r;
1285

1286
        r = link_lldp_rx_configure(link);
861✔
1287
        if (r < 0)
861✔
1288
                return r;
1289

1290
        r = link_lldp_tx_configure(link);
861✔
1291
        if (r < 0)
861✔
1292
                return r;
1293

1294
        r = link_request_static_configs(link);
861✔
1295
        if (r < 0)
861✔
1296
                return r;
1297

1298
        r = link_modem_reconfigure(link);
861✔
1299
        if (r < 0)
861✔
1300
                return r;
1301

1302
        if (!link_has_carrier(link))
861✔
1303
                return 0;
1304

1305
        return link_acquire_dynamic_conf(link);
255✔
1306
}
1307

1308
static int link_get_network(Link *link, Network **ret) {
4,779✔
1309
        Network *network;
4,779✔
1310
        int r;
4,779✔
1311

1312
        assert(link);
4,779✔
1313
        assert(link->manager);
4,779✔
1314
        assert(ret);
4,779✔
1315

1316
        ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
19,684✔
1317
                bool warn = false;
18,610✔
1318

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

1337
                if (network->match.ifname && link->dev) {
3,705✔
1338
                        uint8_t name_assign_type = NET_NAME_UNKNOWN;
3,679✔
1339
                        const char *attr;
3,679✔
1340

1341
                        if (sd_device_get_sysattr_value(link->dev, "name_assign_type", &attr) >= 0)
3,679✔
1342
                                (void) safe_atou8(attr, &name_assign_type);
2,146✔
1343

1344
                        warn = name_assign_type == NET_NAME_ENUM;
3,679✔
1345
                }
1346

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

1352
                if (network->unmanaged)
3,705✔
1353
                        return -ENOENT;
1354

1355
                *ret = network;
1,821✔
1356
                return 0;
1,821✔
1357
        }
1358

1359
        return log_link_debug_errno(link, SYNTHETIC_ERRNO(ENOENT), "No matching .network found.");
1,074✔
1360
}
1361

1362
static void link_enter_unmanaged(Link *link) {
2,958✔
1363
        assert(link);
2,958✔
1364

1365
        if (link->state == LINK_STATE_UNMANAGED)
2,958✔
1366
                return;
1367

1368
        bool notify = link_has_local_lease_domain(link);
2,003✔
1369

1370
        log_link_full(link, link->state == LINK_STATE_INITIALIZED ? LOG_DEBUG : LOG_INFO,
2,009✔
1371
                      "Unmanaging interface.");
1372

1373
        (void) link_stop_engines(link, /* may_keep_dynamic= */ false);
2,003✔
1374
        (void) link_drop_requests(link);
2,003✔
1375
        (void) link_drop_static_config(link);
2,003✔
1376

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

1384
        link->network = network_unref(link->network);
2,003✔
1385
        link_set_state(link, LINK_STATE_UNMANAGED);
2,003✔
1386

1387
        if (notify)
2,003✔
1388
                manager_notify_hook_filters(link->manager);
×
1389
}
1390

1391
static int link_managed_by_us(Link *link) {
7,465✔
1392
        int r;
7,465✔
1393

1394
        assert(link);
7,465✔
1395

1396
        if (!link->dev)
7,465✔
1397
                return true;
7,465✔
1398

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

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

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

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

1417
int link_reconfigure_impl(Link *link, LinkReconfigurationFlag flags) {
5,562✔
1418
        Network *network = NULL;
5,562✔
1419
        int r;
5,562✔
1420

1421
        assert(link);
5,562✔
1422
        assert(link->manager);
5,562✔
1423

1424
        link_assign_netdev(link);
5,562✔
1425

1426
        if (link->manager->state != MANAGER_RUNNING)
5,562✔
1427
                return 0;
5,562✔
1428

1429
        if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
5,562✔
1430
                return 0;
1431

1432
        r = link_managed_by_us(link);
4,781✔
1433
        if (r <= 0)
4,781✔
1434
                return r;
1435

1436
        r = link_get_network(link, &network);
4,779✔
1437
        if (r == -ENOENT) {
4,779✔
1438
                link_enter_unmanaged(link);
2,958✔
1439
                return 0;
2,958✔
1440
        }
1441
        if (r < 0)
1,821✔
1442
                return r;
1443

1444
        if (link->network == network && !FLAGS_SET(flags, LINK_RECONFIGURE_UNCONDITIONALLY))
1,821✔
1445
                return 0;
1446

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

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

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

1472
        _cleanup_(network_unrefp) Network *old_network = TAKE_PTR(link->network);
867✔
1473

1474
        /* Then, apply new .network file */
1475
        link->network = network_ref(network);
867✔
1476

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

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

1496
        link_update_operstate(link, true);
867✔
1497
        link_dirty(link);
867✔
1498

1499
        link_set_state(link, LINK_STATE_INITIALIZED);
867✔
1500
        link->activated = false;
867✔
1501

1502
        r = link_configure(link);
867✔
1503
        if (r < 0)
867✔
1504
                return r;
1✔
1505

1506
        return 1;
1507
}
1508

1509
typedef struct LinkReconfigurationData {
1510
        Manager *manager;
1511
        Link *link;
1512
        LinkReconfigurationFlag flags;
1513
        sd_bus_message *message;
1514
        sd_varlink *varlink;
1515
        unsigned *counter;
1516
} LinkReconfigurationData;
1517

1518
static LinkReconfigurationData* link_reconfiguration_data_free(LinkReconfigurationData *data) {
1,366✔
1519
        if (!data)
1,366✔
1520
                return NULL;
1521

1522
        link_unref(data->link);
1,366✔
1523
        sd_bus_message_unref(data->message);
1,366✔
1524
        sd_varlink_unref(data->varlink);
1,366✔
1525

1526
        return mfree(data);
1,366✔
1527
}
1528

1529
DEFINE_TRIVIAL_CLEANUP_FUNC(LinkReconfigurationData*, link_reconfiguration_data_free);
1,369✔
1530

1531
static void link_reconfiguration_data_destroy_callback(LinkReconfigurationData *data) {
1,366✔
1532
        int r;
1,366✔
1533

1534
        assert(data);
1,366✔
1535
        assert(!data->message || !data->varlink); /* D-Bus and Varlink callers are mutually exclusive */
1,366✔
1536

1537
        if (data->counter) {
1,366✔
1538
                assert(*data->counter > 0);
1,218✔
1539
                (*data->counter)--;
1,218✔
1540
        }
1541

1542
        if (!data->counter || *data->counter == 0) {
1,366✔
1543
                /* Update the state files before replying. Otherwise, systemd-networkd-wait-online following
1544
                 * networkctl reload/reconfigure may read an outdated state file and wrongly consider an
1545
                 * interface already in the configured state. */
1546
                (void) manager_clean_all(data->manager);
301✔
1547

1548
                if (data->message) {
301✔
1549
                        r = sd_bus_reply_method_return(data->message, NULL);
×
1550
                        if (r < 0)
×
1551
                                log_warning_errno(r, "Failed to reply for DBus method, ignoring: %m");
×
1552
                }
1553

1554
                if (data->varlink) {
301✔
1555
                        r = sd_varlink_reply(data->varlink, NULL);
191✔
1556
                        if (r < 0)
191✔
1557
                                log_warning_errno(r, "Failed to reply to Varlink request, ignoring: %m");
×
1558
                }
1559
        }
1560

1561
        link_reconfiguration_data_free(data);
1,366✔
1562
}
1,366✔
1563

1564
static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, LinkReconfigurationData *data) {
1,366✔
1565
        Link *link = ASSERT_PTR(ASSERT_PTR(data)->link);
1,366✔
1566
        int r;
1,366✔
1567

1568
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1,366✔
1569
        if (r <= 0)
1,366✔
1570
                return r;
1571

1572
        r = link_reconfigure_impl(link, data->flags);
1,355✔
1573
        if (r < 0) {
1,355✔
1574
                link_enter_failed(link);
×
1575
                return 0;
×
1576
        }
1577

1578
        return r;
1579
}
1580

1581
int link_reconfigure_full(Link *link, LinkReconfigurationFlag flags, sd_bus_message *message, sd_varlink *varlink, unsigned *counter) {
1,369✔
1582
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1,369✔
1583
        _cleanup_(link_reconfiguration_data_freep) LinkReconfigurationData *data = NULL;
1,369✔
1584
        int r;
1,369✔
1585

1586
        assert(link);
1,369✔
1587
        assert(link->manager);
1,369✔
1588
        assert(link->manager->rtnl);
1,369✔
1589
        assert(!message || !varlink); /* D-Bus and Varlink callers are mutually exclusive */
1,369✔
1590

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

1597
        data = new(LinkReconfigurationData, 1);
1,366✔
1598
        if (!data) {
1,366✔
1599
                r = -ENOMEM;
×
1600
                goto failed;
×
1601
        }
1602

1603
        *data = (LinkReconfigurationData) {
2,732✔
1604
                .manager = link->manager,
1,366✔
1605
                .link = link_ref(link),
1,366✔
1606
                .flags = flags,
1607
                .message = sd_bus_message_ref(message), /* message may be NULL, but _ref() works fine. */
1,366✔
1608
                .varlink = sd_varlink_ref(varlink),     /* varlink may be NULL, but _ref() works fine. */
1,366✔
1609
                .counter = counter,
1610
        };
1611

1612
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
1,366✔
1613
        if (r < 0)
1,366✔
1614
                goto failed;
×
1615

1616
        r = netlink_call_async(link->manager->rtnl, NULL, req,
1,366✔
1617
                               link_reconfigure_handler,
1618
                               link_reconfiguration_data_destroy_callback, data);
1619
        if (r < 0)
1,366✔
1620
                goto failed;
×
1621

1622
        TAKE_PTR(data);
1,366✔
1623
        if (counter)
1,366✔
1624
                (*counter)++;
1,218✔
1625

1626
        if (link->state == LINK_STATE_FAILED)
1,366✔
1627
                link_set_state(link, LINK_STATE_INITIALIZED);
11✔
1628

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

1631
failed:
1632
        log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
1633
        link_enter_failed(link);
×
1634
        return r;
1635
}
1636

1637
static int link_initialized_and_synced(Link *link) {
2,663✔
1638
        int r;
2,663✔
1639

1640
        assert(link);
2,663✔
1641
        assert(link->manager);
2,663✔
1642

1643
        if (link->state == LINK_STATE_PENDING) {
2,663✔
1644
                log_link_debug(link, "Link state is up-to-date");
2,660✔
1645
                link_set_state(link, LINK_STATE_INITIALIZED);
2,660✔
1646

1647
                r = link_new_bound_by_list(link);
2,660✔
1648
                if (r < 0)
2,660✔
1649
                        return r;
1650

1651
                r = link_handle_bound_by_list(link);
2,660✔
1652
                if (r < 0)
2,660✔
1653
                        return r;
1654
        }
1655

1656
        return link_reconfigure_impl(link, /* flags= */ 0);
2,663✔
1657
}
1658

1659
static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2,585✔
1660
        int r;
2,585✔
1661

1662
        r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
2,585✔
1663
        if (r <= 0)
2,585✔
1664
                return r;
1665

1666
        r = link_initialized_and_synced(link);
2,584✔
1667
        if (r < 0)
2,584✔
1668
                link_enter_failed(link);
1✔
1669

1670
        return 0;
1671
}
1672

1673
static int link_initialized(Link *link, sd_device *device) {
2,684✔
1674
        int r;
2,684✔
1675

1676
        assert(link);
2,684✔
1677
        assert(device);
2,684✔
1678

1679
        /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1680
         * or sysattrs) may be outdated. */
1681
        device_unref_and_replace(link->dev, device);
2,684✔
1682

1683
        r = link_managed_by_us(link);
2,684✔
1684
        if (r <= 0)
2,684✔
1685
                return r;
1686

1687
        if (link->dhcp_client) {
2,683✔
1688
                r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev);
×
1689
                if (r < 0)
×
1690
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv4 client, ignoring: %m");
×
1691
        }
1692

1693
        if (link->dhcp6_client) {
2,683✔
1694
                r = sd_dhcp6_client_attach_device(link->dhcp6_client, link->dev);
×
1695
                if (r < 0)
×
1696
                        log_link_warning_errno(link, r, "Failed to attach device to DHCPv6 client, ignoring: %m");
×
1697
        }
1698

1699
        r = link_set_sr_iov_ifindices(link);
2,683✔
1700
        if (r < 0)
2,683✔
1701
                log_link_warning_errno(link, r, "Failed to manage SR-IOV PF and VF ports, ignoring: %m");
×
1702

1703
        if (link->state != LINK_STATE_PENDING)
2,683✔
1704
                return link_reconfigure(link, /* flags= */ 0);
98✔
1705

1706
        log_link_debug(link, "udev initialized link");
2,585✔
1707

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

1713
        return link_call_getlink(link, link_initialized_handler);
2,585✔
1714
}
1715

1716
int link_check_initialized(Link *link) {
2,848✔
1717
        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
2,848✔
1718
        int r;
2,848✔
1719

1720
        assert(link);
2,848✔
1721

1722
        if (!udev_available())
2,848✔
1723
                return link_initialized_and_synced(link);
79✔
1724

1725
        /* udev should be around */
1726
        r = sd_device_new_from_ifindex(&device, link->ifindex);
2,769✔
1727
        if (r < 0) {
2,769✔
1728
                log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
1✔
1729
                return 0;
1✔
1730
        }
1731

1732
        r = device_is_processed(device);
2,768✔
1733
        if (r < 0)
2,768✔
1734
                return log_link_warning_errno(link, r, "Could not determine whether the device is processed by udevd: %m");
×
1735
        if (r == 0) {
2,768✔
1736
                /* not yet ready */
1737
                log_link_debug(link, "link pending udev initialization...");
977✔
1738
                return 0;
977✔
1739
        }
1740

1741
        r = device_is_renaming(device);
1,791✔
1742
        if (r < 0)
1,791✔
1743
                return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
×
1744
        if (r > 0) {
1,791✔
1745
                log_link_debug(link, "Interface is being renamed, pending initialization.");
×
1746
                return 0;
×
1747
        }
1748

1749
        return link_initialized(link, device);
1,791✔
1750
}
1751

1752
int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t action) {
1,985✔
1753
        int r, ifindex;
1,985✔
1754
        Link *link;
1,985✔
1755

1756
        assert(m);
1,985✔
1757
        assert(device);
1,985✔
1758

1759
        r = sd_device_get_ifindex(device, &ifindex);
1,985✔
1760
        if (r < 0)
1,985✔
1761
                return log_device_debug_errno(device, r, "Failed to get ifindex: %m");
×
1762

1763
        r = link_get_by_index(m, ifindex, &link);
1,985✔
1764
        if (r < 0) {
1,985✔
1765
                /* This error is not critical, as the corresponding rtnl message may be received later. */
1766
                log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
824✔
1767
                return 0;
824✔
1768
        }
1769

1770
        /* Let's unref the sd-device object assigned to the corresponding Link object, but keep the Link
1771
         * object here. It will be removed only when rtnetlink says so. */
1772
        if (action == SD_DEVICE_REMOVE) {
1,161✔
1773
                link->dev = sd_device_unref(link->dev);
259✔
1774
                return 0;
259✔
1775
        }
1776

1777
        r = device_is_renaming(device);
902✔
1778
        if (r < 0)
902✔
1779
                return log_device_debug_errno(device, r, "Failed to determine if the device is renaming or not: %m");
×
1780
        if (r > 0) {
902✔
1781
                log_device_debug(device, "Device is renaming, waiting for the interface to be renamed.");
9✔
1782
                /* TODO:
1783
                 * What happens when a device is initialized, then soon renamed after that? When we detect
1784
                 * such, maybe we should cancel or postpone all queued requests for the interface. */
1785
                return 0;
9✔
1786
        }
1787

1788
        r = link_initialized(link, device);
893✔
1789
        if (r < 0)
893✔
1790
                link_enter_failed(link);
×
1791

1792
        return 0;
1793
}
1794

1795
static int link_carrier_gained(Link *link) {
1,278✔
1796
        LinkReconfigurationFlag flags = 0;
1,278✔
1797
        int r;
1,278✔
1798

1799
        assert(link);
1,278✔
1800

1801
        log_link_info(link, "Gained carrier");
1,278✔
1802

1803
        r = event_source_disable(link->carrier_lost_timer);
1,278✔
1804
        if (r < 0)
1,278✔
1805
                log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
×
1806

1807
        /* Process BindCarrier= setting specified by other interfaces. This is independent of the .network
1808
         * file assigned to this interface, but depends on .network files assigned to other interfaces.
1809
         * Hence, this can and should be called earlier. */
1810
        r = link_handle_bound_by_list(link);
1,278✔
1811
        if (r < 0)
1,278✔
1812
                return r;
1813

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

1832
        /* AP and P2P-GO interfaces may have a new SSID - update the link properties in case a new .network
1833
         * profile wants to match on it with SSID= in its [Match] section.
1834
         */
1835
        if (IN_SET(link->wlan_iftype, NL80211_IFTYPE_AP, NL80211_IFTYPE_P2P_GO)) {
1,278✔
1836
                r = link_get_wlan_interface(link);
×
1837
                if (r < 0)
×
1838
                        return r;
1839
        }
1840

1841
        /* At this stage, both wlan and link information should be up-to-date. Hence, it is not necessary to
1842
         * call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or NL80211_CMD_GET_STATION commands, and simply call
1843
         * link_reconfigure_impl(). Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1844
        r = link_reconfigure_impl(link, flags);
1,278✔
1845
        if (r != 0)
1,278✔
1846
                return r;
1847

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

1852
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
1,274✔
1853
                r = link_acquire_dynamic_conf(link);
696✔
1854
                if (r < 0)
696✔
1855
                        return r;
1856

1857
                r = link_request_static_configs(link);
694✔
1858
                if (r < 0)
694✔
1859
                        return r;
×
1860
        }
1861

1862
        return 0;
1863
}
1864

1865
static int link_carrier_lost_impl(Link *link) {
741✔
1866
        int ret = 0;
741✔
1867

1868
        assert(link);
741✔
1869

1870
        link->previous_ssid = mfree(link->previous_ssid);
741✔
1871

1872
        ret = link_handle_bound_by_list(link);
741✔
1873

1874
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
741✔
1875
                return ret;
741✔
1876

1877
        if (!link->network)
741✔
1878
                return ret;
1879

1880
        RET_GATHER(ret, link_stop_engines(link, /* may_keep_dynamic= */ false));
679✔
1881
        RET_GATHER(ret, link_drop_static_config(link));
679✔
1882

1883
        return ret;
1884
}
1885

1886
static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
×
1887
        Link *link = ASSERT_PTR(userdata);
×
1888
        int r;
×
1889

1890
        r = link_carrier_lost_impl(link);
×
1891
        if (r < 0) {
×
1892
                log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
×
1893
                link_enter_failed(link);
×
1894
        }
1895

1896
        return 0;
×
1897
}
1898

1899
static int link_carrier_lost(Link *link) {
759✔
1900
        uint16_t dhcp_mtu;
759✔
1901
        usec_t usec;
759✔
1902

1903
        assert(link);
759✔
1904

1905
        log_link_info(link, "Lost carrier");
759✔
1906

1907
        if (link->iftype == ARPHRD_CAN)
759✔
1908
                /* let's shortcut things for CAN which doesn't need most of what's done below. */
1909
                usec = 0;
1910

1911
        else if (!link->network)
755✔
1912
                usec = 0;
1913

1914
        else if (link->network->ignore_carrier_loss_set)
693✔
1915
                /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1916
                usec = link->network->ignore_carrier_loss_usec;
12✔
1917

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

1923
        else if (link->network->dhcp_use_mtu &&
681✔
1924
                 link->dhcp_lease &&
4✔
1925
                 sd_dhcp_lease_get_mtu(link->dhcp_lease, &dhcp_mtu) >= 0 &&
2✔
1926
                 dhcp_mtu != link->original_mtu)
1✔
1927
                /* Some drivers reset interfaces when changing MTU. Resetting interfaces by the static
1928
                 * MTU should not cause any issues, as MTU is changed only once. However, setting MTU
1929
                 * through DHCP lease causes an infinite loop of resetting the interface. See #18738. */
1930
                usec = 5 * USEC_PER_SEC;
1931

1932
        else
1933
                /* Otherwise, use the implied default value. */
1934
                usec = link->network->ignore_carrier_loss_usec;
680✔
1935

1936
        if (usec == USEC_INFINITY)
692✔
1937
                return 0;
759✔
1938

1939
        if (usec == 0)
742✔
1940
                return link_carrier_lost_impl(link);
741✔
1941

1942
        return event_reset_time_relative(link->manager->event,
1✔
1943
                                         &link->carrier_lost_timer,
1944
                                         CLOCK_BOOTTIME,
1945
                                         usec,
1946
                                         0,
1947
                                         link_carrier_lost_handler,
1948
                                         link,
1949
                                         0,
1950
                                         "link-carrier-loss",
1951
                                         true);
1952
}
1953

1954
static int link_admin_state_up(Link *link) {
1,250✔
1955
        assert(link);
1,250✔
1956

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

1960
        log_link_info(link, "Link UP");
1,250✔
1961

1962
        if (!link->network)
1,250✔
1963
                return 0;
1964

1965
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
668✔
1966
                log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
4✔
1967
                return link_request_to_bring_up_or_down(link, /* up= */ false);
4✔
1968
        }
1969

1970
        /* We set the ipv6 mtu after the device mtu, but the kernel resets
1971
         * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1972
        (void) link_set_ipv6_mtu(link, LOG_INFO);
664✔
1973

1974
        return 0;
664✔
1975
}
1976

1977
static int link_admin_state_down(Link *link) {
729✔
1978
        assert(link);
729✔
1979

1980
        log_link_info(link, "Link DOWN");
729✔
1981

1982
        link_forget_nexthops(link);
729✔
1983
        link_forget_routes(link);
729✔
1984

1985
        if (!link->network)
729✔
1986
                return 0;
1987

1988
        if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
668✔
1989
                log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
9✔
1990
                return link_request_to_bring_up_or_down(link, /* up= */ true);
9✔
1991
        }
1992

1993
        return 0;
1994
}
1995

1996
static bool link_is_enslaved(Link *link) {
7,071✔
1997
        if (link->flags & IFF_SLAVE)
7,071✔
1998
                return true;
1999

2000
        if (link->master_ifindex > 0)
7,016✔
2001
                return true;
244✔
2002

2003
        return false;
2004
}
2005

2006
void link_update_operstate(Link *link, bool also_update_master) {
13,507✔
2007
        LinkOperationalState operstate;
13,507✔
2008
        LinkCarrierState carrier_state;
13,507✔
2009
        LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
13,507✔
2010
        LinkOnlineState online_state;
13,507✔
2011
        _cleanup_strv_free_ char **p = NULL;
13,507✔
2012
        bool changed = false;
13,507✔
2013

2014
        assert(link);
13,507✔
2015

2016
        if (link->kernel_operstate == IF_OPER_DORMANT)
13,507✔
2017
                carrier_state = LINK_CARRIER_STATE_DORMANT;
2018
        else if (link_has_carrier(link)) {
13,507✔
2019
                if (link_is_enslaved(link))
7,071✔
2020
                        carrier_state = LINK_CARRIER_STATE_ENSLAVED;
2021
                else
2022
                        carrier_state = LINK_CARRIER_STATE_CARRIER;
2023
        } else if (link_is_up(link))
6,436✔
2024
                carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
2025
        else
2026
                carrier_state = LINK_CARRIER_STATE_OFF;
2027

2028
        if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
2029
                Link *slave;
7,071✔
2030

2031
                SET_FOREACH(slave, link->slaves) {
7,332✔
2032
                        link_update_operstate(slave, false);
261✔
2033

2034
                        if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
261✔
2035
                                carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
64✔
2036
                }
2037
        }
2038

2039
        link_get_address_states(link, &ipv4_address_state, &ipv6_address_state, &address_state);
13,507✔
2040

2041
        /* Mapping of address and carrier state vs operational state
2042
         *                                                     carrier state
2043
         *                          | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
2044
         *                 ------------------------------------------------------------------------------
2045
         *                 off      | off | no-carrier | dormant | degraded-carrier | carrier  | enslaved
2046
         * address_state   degraded | off | no-carrier | dormant | degraded         | degraded | enslaved
2047
         *                 routable | off | no-carrier | dormant | routable         | routable | routable
2048
         */
2049

2050
        if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_ROUTABLE)
13,507✔
2051
                operstate = LINK_OPERSTATE_ROUTABLE;
2052
        else if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_DEGRADED)
32✔
2053
                operstate = LINK_OPERSTATE_DEGRADED;
2054
        else if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
13,470✔
2055
                operstate = (LinkOperationalState) carrier_state;
2056
        else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
4,553✔
2057
                operstate = LINK_OPERSTATE_ROUTABLE;
2058
        else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1,393✔
2059
                operstate = LINK_OPERSTATE_DEGRADED;
2060
        else
2061
                operstate = LINK_OPERSTATE_ENSLAVED;
33✔
2062

2063
        LinkOperationalStateRange req;
13,507✔
2064
        link_required_operstate_for_online(link, &req);
13,507✔
2065

2066
        /* Only determine online state for managed links with RequiredForOnline=yes */
2067
        if (!link->network || !link->network->required_for_online)
13,507✔
2068
                online_state = _LINK_ONLINE_STATE_INVALID;
2069

2070
        else if (!operational_state_is_in_range(operstate, &req))
8,871✔
2071
                online_state = LINK_ONLINE_STATE_OFFLINE;
2072

2073
        else {
2074
                AddressFamily required_family = link_required_family_for_online(link);
4,475✔
2075
                bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
4,475✔
2076
                bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
4,475✔
2077

2078
                /* The operational state is within the range required for online.
2079
                 * If a particular address family is also required, we might revert
2080
                 * to offline in the blocks below. */
2081
                online_state = LINK_ONLINE_STATE_ONLINE;
4,475✔
2082

2083
                if (req.min >= LINK_OPERSTATE_DEGRADED) {
4,475✔
2084
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
4,294✔
2085
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2086
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
4,294✔
2087
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2088
                }
2089

2090
                if (req.min >= LINK_OPERSTATE_ROUTABLE) {
4,475✔
2091
                        if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2092
                                online_state = LINK_ONLINE_STATE_OFFLINE;
×
2093
                        if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
4✔
2094
                                online_state = LINK_ONLINE_STATE_OFFLINE;
3✔
2095
                }
2096
        }
2097

2098
        if (link->carrier_state != carrier_state) {
13,507✔
2099
                link->carrier_state = carrier_state;
4,747✔
2100
                changed = true;
4,747✔
2101
                if (strv_extend(&p, "CarrierState") < 0)
4,747✔
2102
                        log_oom();
×
2103
        }
2104

2105
        if (link->address_state != address_state) {
13,507✔
2106
                link->address_state = address_state;
2,006✔
2107
                changed = true;
2,006✔
2108
                if (strv_extend(&p, "AddressState") < 0)
2,006✔
2109
                        log_oom();
×
2110
        }
2111

2112
        if (link->ipv4_address_state != ipv4_address_state) {
13,507✔
2113
                link->ipv4_address_state = ipv4_address_state;
967✔
2114
                changed = true;
967✔
2115
                if (strv_extend(&p, "IPv4AddressState") < 0)
967✔
2116
                        log_oom();
×
2117
        }
2118

2119
        if (link->ipv6_address_state != ipv6_address_state) {
13,507✔
2120
                link->ipv6_address_state = ipv6_address_state;
1,716✔
2121
                changed = true;
1,716✔
2122
                if (strv_extend(&p, "IPv6AddressState") < 0)
1,716✔
2123
                        log_oom();
×
2124
        }
2125

2126
        if (link->operstate != operstate) {
13,507✔
2127
                link->operstate = operstate;
5,747✔
2128
                changed = true;
5,747✔
2129
                if (strv_extend(&p, "OperationalState") < 0)
5,747✔
2130
                        log_oom();
×
2131
        }
2132

2133
        if (link->online_state != online_state) {
13,507✔
2134
                link->online_state = online_state;
1,889✔
2135
                changed = true;
1,889✔
2136
                if (strv_extend(&p, "OnlineState") < 0)
1,889✔
2137
                        log_oom();
×
2138
        }
2139

2140
        if (p)
13,507✔
2141
                link_send_changed_strv(link, p);
8,063✔
2142
        if (changed)
13,507✔
2143
                link_dirty(link);
8,063✔
2144

2145
        if (also_update_master) {
13,507✔
2146
                Link *master;
13,246✔
2147

2148
                if (link_get_master(link, &master) >= 0)
13,246✔
2149
                        link_update_operstate(master, true);
157✔
2150
        }
2151
}
13,507✔
2152

2153
bool link_has_carrier(Link *link) {
43,172✔
2154
        assert(link);
43,172✔
2155
        return netif_has_carrier(link->kernel_operstate, link->flags);
43,172✔
2156
}
2157

2158
bool link_is_up(Link *link) {
20,449✔
2159
        assert(link);
20,449✔
2160
        return FLAGS_SET(link->flags, IFF_UP);
20,449✔
2161
}
2162

2163
bool link_multicast_enabled(Link *link) {
9,792✔
2164
        assert(link);
9,792✔
2165

2166
        /* If Multicast= is specified, use the value. */
2167
        if (link->network && link->network->multicast >= 0)
9,792✔
2168
                return link->network->multicast;
×
2169

2170
        /* Otherwise, return the current state. */
2171
        return FLAGS_SET(link->flags, IFF_MULTICAST);
9,792✔
2172
}
2173

2174
#define FLAG_STRING(string, flag, old, new)                      \
2175
        (((old ^ new) & flag)                                    \
2176
         ? ((old & flag) ? (" -" string) : (" +" string))        \
2177
         : "")
2178

2179
static int link_update_flags(Link *link, sd_netlink_message *message) {
10,763✔
2180
        bool link_was_admin_up, had_carrier;
10,763✔
2181
        uint8_t operstate;
10,763✔
2182
        unsigned flags;
10,763✔
2183
        int r;
10,763✔
2184

2185
        assert(link);
10,763✔
2186
        assert(message);
10,763✔
2187

2188
        r = sd_rtnl_message_link_get_flags(message, &flags);
10,763✔
2189
        if (r < 0)
10,763✔
2190
                return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
×
2191

2192
        r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
10,763✔
2193
        if (r == -ENODATA)
10,763✔
2194
                /* If we got a message without operstate, assume the state was unchanged. */
2195
                operstate = link->kernel_operstate;
×
2196
        else if (r < 0)
10,763✔
2197
                return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
×
2198

2199
        if (link->flags == flags && link->kernel_operstate == operstate)
10,763✔
2200
                return 0;
2201

2202
        if (link->flags != flags) {
4,727✔
2203
                unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
4,726✔
2204

2205
                log_link_debug(link, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
12,973✔
2206
                               FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
2207
                               FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
2208
                               FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
2209
                               FLAG_STRING("UP", IFF_UP, link->flags, flags),
2210
                               FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
2211
                               FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
2212
                               FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
2213
                               FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
2214
                               FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
2215
                               FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
2216
                               FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
2217
                               FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
2218
                               FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
2219
                               FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
2220
                               FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
2221
                               FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
2222
                               FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
2223
                               FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
2224
                               FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
2225

2226
                unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
4,726✔
2227
                                  IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
2228
                                  IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
2229
                                  IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
2230
                                  IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
2231
                                  IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
2232
                unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
4,726✔
2233
                unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
4,726✔
2234

2235
                if (unknown_flags_added)
4,726✔
2236
                        log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
×
2237

2238
                if (unknown_flags_removed)
4,726✔
2239
                        log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
×
2240
        }
2241

2242
        link_was_admin_up = link_is_up(link);
4,727✔
2243
        had_carrier = link_has_carrier(link);
4,727✔
2244

2245
        link->flags = flags;
4,727✔
2246
        link->kernel_operstate = operstate;
4,727✔
2247

2248
        link_update_operstate(link, true);
4,727✔
2249

2250
        r = 0;
4,727✔
2251

2252
        if (!link_was_admin_up && link_is_up(link))
4,727✔
2253
                r = link_admin_state_up(link);
1,250✔
2254
        else if (link_was_admin_up && !link_is_up(link))
1,141✔
2255
                r = link_admin_state_down(link);
729✔
2256
        if (r < 0)
1,979✔
2257
                return r;
2258

2259
        if (!had_carrier && link_has_carrier(link))
4,727✔
2260
                r = link_carrier_gained(link);
1,278✔
2261
        else if (had_carrier && !link_has_carrier(link))
786✔
2262
                r = link_carrier_lost(link);
759✔
2263
        if (r < 0)
4,727✔
2264
                return r;
2✔
2265

2266
        return 0;
2267
}
2268

2269
static int link_update_master(Link *link, sd_netlink_message *message) {
10,763✔
2270
        int master_ifindex, r;
10,763✔
2271

2272
        assert(link);
10,763✔
2273
        assert(message);
10,763✔
2274

2275
        r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
10,763✔
2276
        if (r == -ENODATA)
10,763✔
2277
                master_ifindex = 0; /* no master interface */
10,248✔
2278
        else if (r < 0)
515✔
2279
                return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
×
2280

2281
        if (master_ifindex == link->ifindex)
10,763✔
2282
                master_ifindex = 0;
7✔
2283

2284
        if (master_ifindex != link->master_ifindex) {
10,763✔
2285
                if (link->master_ifindex == 0)
56✔
2286
                        log_link_debug(link, "Attached to master interface: %i", master_ifindex);
46✔
2287
                else if (master_ifindex == 0)
10✔
2288
                        log_link_debug(link, "Detached from master interface: %i", link->master_ifindex);
10✔
2289
                else
2290
                        log_link_debug(link, "Master interface changed: %i %s %i", link->master_ifindex,
×
2291
                                       glyph(GLYPH_ARROW_RIGHT), master_ifindex);
2292

2293
                link_drop_from_master(link);
56✔
2294
                link->master_ifindex = master_ifindex;
56✔
2295

2296
                /* Updating master ifindex may cause operational state change, e.g. carrier <-> enslaved */
2297
                link_dirty(link);
56✔
2298
        }
2299

2300
        r = link_append_to_master(link);
10,763✔
2301
        if (r < 0)
10,763✔
2302
                return log_link_debug_errno(link, r, "Failed to append link to master: %m");
×
2303

2304
        return 0;
2305
}
2306

2307
static int link_update_driver(Link *link, sd_netlink_message *message) {
10,763✔
2308
        int r;
10,763✔
2309

2310
        assert(link);
10,763✔
2311
        assert(link->manager);
10,763✔
2312
        assert(message);
10,763✔
2313

2314
        /* Driver is already read. Assuming the driver is never changed. */
2315
        if (link->ethtool_driver_read)
10,763✔
2316
                return 0;
2317

2318
        /* When udevd is running, read the driver after the interface is initialized by udevd.
2319
         * Otherwise, ethtool may not work correctly. See issue #22538.
2320
         * When udevd is not running, read the value when the interface is detected. */
2321
        if (udev_available() && !link->dev)
5,836✔
2322
                return 0;
2323

2324
        link->ethtool_driver_read = true;
2,662✔
2325

2326
        r = ethtool_get_driver(&link->manager->ethtool_fd, link->ifname, &link->driver);
2,662✔
2327
        if (r < 0) {
2,662✔
2328
                log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
506✔
2329
                return 0;
506✔
2330
        }
2331

2332
        log_link_debug(link, "Found driver: %s", strna(link->driver));
2,156✔
2333

2334
        if (streq_ptr(link->driver, "dsa")) {
2,156✔
2335
                uint32_t dsa_master_ifindex = 0;
×
2336

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

2341
                if (dsa_master_ifindex > INT_MAX) {
×
2342
                        log_link_debug(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
×
2343
                                       dsa_master_ifindex);
2344
                        dsa_master_ifindex = 0;
×
2345
                }
2346

2347
                link->dsa_master_ifindex = (int) dsa_master_ifindex;
×
2348
        }
2349

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

2353
static int link_update_permanent_hardware_address(Link *link, sd_netlink_message *message) {
10,763✔
2354
        int r;
10,763✔
2355

2356
        assert(link);
10,763✔
2357
        assert(link->manager);
10,763✔
2358
        assert(message);
10,763✔
2359

2360
        if (link->permanent_hw_addr.length > 0)
10,763✔
2361
                return 0;
2362

2363
        r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
10,095✔
2364
        if (r == -ENODATA)
10,095✔
2365
                return 0;
2366
        if (r < 0)
224✔
2367
                return log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute: %m");
×
2368

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

2372
        return 1; /* needs reconfigure */
2373
}
2374

2375
static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
10,763✔
2376
        struct hw_addr_data addr;
10,763✔
2377
        int r;
10,763✔
2378

2379
        assert(link);
10,763✔
2380
        assert(message);
10,763✔
2381

2382
        r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
10,763✔
2383
        if (r < 0 && r != -ENODATA)
10,763✔
2384
                return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
×
2385

2386
        r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
10,763✔
2387
        if (r == -ENODATA)
10,763✔
2388
                return 0;
2389
        if (r < 0)
10,553✔
2390
                return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
×
2391

2392
        if (hw_addr_equal(&link->hw_addr, &addr))
10,553✔
2393
                return 0;
2394

2395
        if (link->hw_addr.length == 0)
3,001✔
2396
                log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
2,785✔
2397
        else {
2398
                log_link_debug(link, "Hardware address is changed: %s %s %s",
216✔
2399
                               HW_ADDR_TO_STR(&link->hw_addr),
2400
                               glyph(GLYPH_ARROW_RIGHT),
2401
                               HW_ADDR_TO_STR(&addr));
2402

2403
                hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
216✔
2404
        }
2405

2406
        link->hw_addr = addr;
3,001✔
2407

2408
        if (!hw_addr_is_null(&link->hw_addr)) {
3,001✔
2409
                r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
1,095✔
2410
                if (r == -EEXIST && streq_ptr(link->kind, "bond"))
1,095✔
2411
                        /* bonding master and its slaves have the same hardware address. */
2412
                        r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
3✔
2413
                if (r < 0)
1,095✔
2414
                        log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
51✔
2415
        }
2416

2417
        r = ipv4acd_update_mac(link);
3,001✔
2418
        if (r < 0)
3,001✔
2419
                return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
×
2420

2421
        r = ipv4ll_update_mac(link);
3,001✔
2422
        if (r < 0)
3,001✔
2423
                return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
×
2424

2425
        r = dhcp4_update_mac(link);
3,001✔
2426
        if (r < 0)
3,001✔
2427
                return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
×
2428

2429
        r = dhcp6_update_mac(link);
3,001✔
2430
        if (r < 0)
3,001✔
2431
                return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
×
2432

2433
        r = radv_update_mac(link);
3,001✔
2434
        if (r < 0)
3,001✔
2435
                return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
×
2436

2437
        if (link->ndisc && link->hw_addr.length == ETH_ALEN) {
3,001✔
2438
                r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
×
2439
                if (r < 0)
×
2440
                        return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
×
2441
        }
2442

2443
        if (link->lldp_rx) {
3,001✔
2444
                r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
21✔
2445
                if (r < 0)
21✔
2446
                        return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
×
2447
        }
2448

2449
        if (link->lldp_tx) {
3,001✔
2450
                r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
×
2451
                if (r < 0)
×
2452
                        return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
×
2453
        }
2454

2455
        return 1; /* needs reconfigure */
2456
}
2457

2458
static int link_update_mtu(Link *link, sd_netlink_message *message) {
10,763✔
2459
        uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
10,763✔
2460
        int r;
10,763✔
2461

2462
        assert(link);
10,763✔
2463
        assert(message);
10,763✔
2464

2465
        r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
10,763✔
2466
        if (r == -ENODATA)
10,763✔
2467
                return 0;
10,763✔
2468
        if (r < 0)
10,763✔
2469
                return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
×
2470
        if (mtu == 0)
10,763✔
2471
                return 0;
2472

2473
        r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
10,740✔
2474
        if (r < 0 && r != -ENODATA)
10,740✔
2475
                return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
×
2476

2477
        r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
10,740✔
2478
        if (r < 0 && r != -ENODATA)
10,740✔
2479
                return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
×
2480

2481
        if (max_mtu == 0)
10,740✔
2482
                max_mtu = UINT32_MAX;
6,870✔
2483

2484
        link->min_mtu = min_mtu;
10,740✔
2485
        link->max_mtu = max_mtu;
10,740✔
2486

2487
        if (link->original_mtu == 0) {
10,740✔
2488
                link->original_mtu = mtu;
2,849✔
2489
                log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2,849✔
2490
                               link->original_mtu, link->min_mtu, link->max_mtu);
2491
        }
2492

2493
        if (link->mtu == mtu)
10,740✔
2494
                return 0;
2495

2496
        if (link->mtu != 0)
2,942✔
2497
                log_link_debug(link, "MTU is changed: %"PRIu32" %s %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
93✔
2498
                               link->mtu, glyph(GLYPH_ARROW_RIGHT), mtu,
2499
                               link->min_mtu, link->max_mtu);
2500

2501
        link->mtu = mtu;
2,942✔
2502

2503
        if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
2,942✔
2504
                /* The kernel resets IPv6 MTU after changing device MTU. So, we need to re-set IPv6 MTU again. */
2505
                (void) link_set_ipv6_mtu_async(link);
47✔
2506

2507
        if (link->dhcp_client) {
2,942✔
2508
                r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
4✔
2509
                if (r < 0)
4✔
2510
                        return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
×
2511
        }
2512

2513
        if (link->radv) {
2,942✔
2514
                r = sd_radv_set_mtu(link->radv, link->mtu);
×
2515
                if (r < 0)
×
2516
                        return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
×
2517
        }
2518

2519
        return 0;
2520
}
2521

2522
static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
10,763✔
2523
        _cleanup_strv_free_ char **altnames = NULL;
10,763✔
2524
        int r;
10,763✔
2525

2526
        assert(link);
10,763✔
2527
        assert(message);
10,763✔
2528

2529
        r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
10,763✔
2530
        if (r == -ENODATA)
10,763✔
2531
                /* The message does not have IFLA_PROP_LIST container attribute. It does not mean the
2532
                 * interface has no alternative name. */
2533
                return 0;
2534
        if (r < 0)
161✔
2535
                return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
×
2536

2537
        if (strv_equal(altnames, link->alternative_names))
161✔
2538
                return 0;
2539

2540
        STRV_FOREACH(n, link->alternative_names)
46✔
2541
                hashmap_remove(link->manager->links_by_name, *n);
10✔
2542

2543
        strv_free_and_replace(link->alternative_names, altnames);
36✔
2544

2545
        STRV_FOREACH(n, link->alternative_names) {
80✔
2546
                r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
44✔
2547
                if (r < 0)
44✔
2548
                        return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
×
2549
        }
2550

2551
        return 1; /* needs reconfigure */
2552
}
2553

2554
static int link_update_name(Link *link, sd_netlink_message *message) {
10,771✔
2555
        char ifname_from_index[IF_NAMESIZE];
10,771✔
2556
        const char *ifname;
10,771✔
2557
        int r;
10,771✔
2558

2559
        assert(link);
10,771✔
2560
        assert(message);
10,771✔
2561

2562
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
10,771✔
2563
        if (r == -ENODATA)
10,771✔
2564
                /* Hmm?? But ok. */
2565
                return 0;
10,771✔
2566
        if (r < 0)
10,771✔
2567
                return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
×
2568

2569
        if (streq(ifname, link->ifname))
10,771✔
2570
                return 0;
2571

2572
        r = format_ifname(link->ifindex, ifname_from_index);
50✔
2573
        if (r < 0)
50✔
2574
                return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
8✔
2575

2576
        if (!streq(ifname, ifname_from_index)) {
42✔
2577
                log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "
×
2578
                               "with the name currently configured on the actual interface '%s'. Ignoring.",
2579
                               ifname, ifname_from_index);
2580
                return 0;
×
2581
        }
2582

2583
        log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
42✔
2584

2585
        hashmap_remove(link->manager->links_by_name, link->ifname);
42✔
2586

2587
        r = free_and_strdup(&link->ifname, ifname);
42✔
2588
        if (r < 0)
42✔
2589
                return log_oom_debug();
×
2590

2591
        r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
42✔
2592
        if (r < 0)
42✔
2593
                return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
×
2594

2595
        if (link->dhcp_client) {
42✔
2596
                r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname);
×
2597
                if (r < 0)
×
2598
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m");
×
2599
        }
2600

2601
        if (link->dhcp6_client) {
42✔
2602
                r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
×
2603
                if (r < 0)
×
2604
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCPv6 client: %m");
×
2605
        }
2606

2607
        if (link->ndisc) {
42✔
2608
                r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
×
2609
                if (r < 0)
×
2610
                        return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
×
2611
        }
2612

2613
        if (link->dhcp_server) {
42✔
2614
                r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
×
2615
                if (r < 0)
×
2616
                        return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
×
2617
        }
2618

2619
        if (link->radv) {
42✔
2620
                r = sd_radv_set_ifname(link->radv, link->ifname);
×
2621
                if (r < 0)
×
2622
                        return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
×
2623
        }
2624

2625
        if (link->lldp_rx) {
42✔
2626
                r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
2✔
2627
                if (r < 0)
2✔
2628
                        return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
×
2629
        }
2630

2631
        if (link->lldp_tx) {
42✔
2632
                r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
×
2633
                if (r < 0)
×
2634
                        return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
×
2635
        }
2636

2637
        if (link->ipv4ll) {
42✔
2638
                r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
×
2639
                if (r < 0)
×
2640
                        return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
×
2641
        }
2642

2643
        r = ipv4acd_set_ifname(link);
42✔
2644
        if (r < 0)
42✔
2645
                return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
×
2646

2647
        return 1; /* needs reconfigure */
2648
}
2649

2650
static int link_update(Link *link, sd_netlink_message *message) {
10,771✔
2651
        bool needs_reconfigure = false;
10,771✔
2652
        int r;
10,771✔
2653

2654
        assert(link);
10,771✔
2655
        assert(message);
10,771✔
2656

2657
        r = link_update_name(link, message);
10,771✔
2658
        if (r < 0)
10,771✔
2659
                return r;
2660
        needs_reconfigure = needs_reconfigure || r > 0;
10,763✔
2661

2662
        r = link_update_alternative_names(link, message);
10,763✔
2663
        if (r < 0)
10,763✔
2664
                return r;
2665
        needs_reconfigure = needs_reconfigure || r > 0;
10,763✔
2666

2667
        r = link_update_mtu(link, message);
10,763✔
2668
        if (r < 0)
10,763✔
2669
                return r;
2670

2671
        r = link_update_driver(link, message);
10,763✔
2672
        if (r < 0)
10,763✔
2673
                return r;
2674
        needs_reconfigure = needs_reconfigure || r > 0;
10,763✔
2675

2676
        r = link_update_permanent_hardware_address(link, message);
10,763✔
2677
        if (r < 0)
10,763✔
2678
                return r;
2679
        needs_reconfigure = needs_reconfigure || r > 0;
10,763✔
2680

2681
        r = link_update_hardware_address(link, message);
10,763✔
2682
        if (r < 0)
10,763✔
2683
                return r;
2684
        needs_reconfigure = needs_reconfigure || r > 0;
10,763✔
2685

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

2690
        r = link_update_ipv6ll_addrgen_mode(link, message);
10,763✔
2691
        if (r < 0)
10,763✔
2692
                return r;
2693

2694
        r = link_update_flags(link, message);
10,763✔
2695
        if (r < 0)
10,763✔
2696
                return r;
2697

2698
        r = link_update_bridge_vlan(link, message);
10,761✔
2699
        if (r < 0)
10,761✔
2700
                return r;
2701

2702
        return needs_reconfigure;
10,761✔
2703
}
2704

2705
static Link *link_drop_or_unref(Link *link) {
×
2706
        if (!link)
×
2707
                return NULL;
2708
        if (!link->manager)
×
2709
                return link_unref(link);
×
2710
        return link_drop(link);
×
2711
}
2712

2713
DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2,849✔
2714

2715
static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
2,849✔
2716
        _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL;
2,849✔
2717
        _cleanup_(link_drop_or_unrefp) Link *link = NULL;
2,849✔
2718
        unsigned short iftype;
2,849✔
2719
        int r, ifindex;
2,849✔
2720

2721
        assert(manager);
2,849✔
2722
        assert(message);
2,849✔
2723
        assert(ret);
2,849✔
2724

2725
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2,849✔
2726
        if (r < 0)
2,849✔
2727
                return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
×
2728
        else if (ifindex <= 0)
2,849✔
2729
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
×
2730

2731
        r = sd_rtnl_message_link_get_type(message, &iftype);
2,849✔
2732
        if (r < 0)
2,849✔
2733
                return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
×
2734

2735
        r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2,849✔
2736
        if (r < 0)
2,849✔
2737
                return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
×
2738

2739
        /* check for link kind */
2740
        r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2,849✔
2741
        if (r >= 0) {
2,849✔
2742
                r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2,301✔
2743
                if (r < 0 && r != -ENODATA)
2,301✔
2744
                        return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
×
2745
                r = sd_netlink_message_exit_container(message);
2,301✔
2746
                if (r < 0)
2,301✔
2747
                        return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
×
2748
        }
2749

2750
        if (!manager->test_mode) {
2,849✔
2751
                /* Do not update state files when running in test mode. */
2752
                if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2,848✔
2753
                        return log_oom_debug();
×
2754

2755
                if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2,848✔
2756
                        return log_oom_debug();
×
2757
        }
2758

2759
        link = new(Link, 1);
2,849✔
2760
        if (!link)
2,849✔
2761
                return -ENOMEM;
2762

2763
        *link = (Link) {
2,849✔
2764
                .n_ref = 1,
2765
                .state = LINK_STATE_PENDING,
2766
                .online_state = _LINK_ONLINE_STATE_INVALID,
2767
                .automatic_reconfigure_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_SEC, .burst = 5 },
2768
                .ifindex = ifindex,
2769
                .iftype = iftype,
2770
                .ifname = TAKE_PTR(ifname),
2,849✔
2771
                .kind = TAKE_PTR(kind),
2,849✔
2772

2773
                .bridge_vlan_pvid = UINT16_MAX,
2774

2775
                .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2776

2777
                .state_file = TAKE_PTR(state_file),
2,849✔
2778
                .lease_file = TAKE_PTR(lease_file),
2,849✔
2779

2780
                .n_dns = UINT_MAX,
2781
                .dns_default_route = -1,
2782
                .llmnr = _RESOLVE_SUPPORT_INVALID,
2783
                .mdns = _RESOLVE_SUPPORT_INVALID,
2784
                .dnssec_mode = _DNSSEC_MODE_INVALID,
2785
                .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2786
        };
2787

2788
        r = hashmap_ensure_put(&manager->links_by_index, &link_hash_ops, INT_TO_PTR(link->ifindex), link);
2,849✔
2789
        if (r < 0)
2,849✔
2790
                return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
×
2791

2792
        link->manager = manager;
2,849✔
2793

2794
        r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
2,849✔
2795
        if (r < 0)
2,849✔
2796
                return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
×
2797

2798
        log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
3,397✔
2799
                       link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2800

2801
        /* If contained in this set, the link is wireless and the corresponding NL80211_CMD_NEW_INTERFACE
2802
         * message arrived too early. Request the wireless link information again.
2803
         */
2804
        if (set_remove(manager->new_wlan_ifindices, INT_TO_PTR(link->ifindex))) {
2,849✔
2805
                r = link_get_wlan_interface(link);
×
2806
                if (r < 0)
×
2807
                        log_link_warning_errno(link, r, "Failed to get wireless interface, ignoring: %m");
×
2808
        }
2809

2810
        *ret = TAKE_PTR(link);
2,849✔
2811
        return 0;
2,849✔
2812
}
2813

2814
int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
6,692✔
2815
        Link *link = NULL;
6,692✔
2816
        NetDev *netdev = NULL;
6,692✔
2817
        uint16_t type;
6,692✔
2818
        const char *name;
6,692✔
2819
        int r, ifindex;
6,692✔
2820

2821
        assert(rtnl);
6,692✔
2822
        assert(message);
6,692✔
2823
        assert(manager);
6,692✔
2824

2825
        if (sd_netlink_message_is_error(message)) {
6,692✔
2826
                r = sd_netlink_message_get_errno(message);
×
2827
                if (r < 0)
×
2828
                        log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
×
2829

2830
                return 0;
×
2831
        }
2832

2833
        r = sd_netlink_message_get_type(message, &type);
6,692✔
2834
        if (r < 0) {
6,692✔
2835
                log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
×
2836
                return 0;
×
2837
        } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
6,692✔
2838
                log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
×
2839
                return 0;
×
2840
        }
2841

2842
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
6,692✔
2843
        if (r < 0) {
6,692✔
2844
                log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
×
2845
                return 0;
×
2846
        } else if (ifindex <= 0) {
6,692✔
2847
                log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
×
2848
                return 0;
×
2849
        }
2850

2851
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
6,692✔
2852
        if (r < 0) {
6,692✔
2853
                log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
×
2854
                return 0;
×
2855
        }
2856

2857
        (void) link_get_by_index(manager, ifindex, &link);
6,692✔
2858
        (void) netdev_get(manager, name, &netdev);
6,692✔
2859

2860
        switch (type) {
6,692✔
2861
        case RTM_NEWLINK:
5,792✔
2862
                if (netdev) {
5,792✔
2863
                        /* netdev exists, so make sure the ifindex matches */
2864
                        r = netdev_set_ifindex(netdev, message);
2,919✔
2865
                        if (r < 0) {
2,919✔
2866
                                log_netdev_warning_errno(netdev, r, "Could not process new link message for netdev, ignoring: %m");
×
2867
                                netdev_enter_failed(netdev);
×
2868
                                return 0;
×
2869
                        }
2870
                }
2871

2872
                if (!link) {
5,792✔
2873
                        /* link is new, so add it */
2874
                        r = link_new(manager, message, &link);
2,849✔
2875
                        if (r < 0) {
2,849✔
2876
                                log_warning_errno(r, "Could not process new link message: %m");
×
2877
                                return 0;
×
2878
                        }
2879

2880
                        r = link_update(link, message);
2,849✔
2881
                        if (r < 0) {
2,849✔
2882
                                log_link_warning_errno(link, r, "Could not process link message: %m");
×
2883
                                link_enter_failed(link);
×
2884
                                return 0;
×
2885
                        }
2886

2887
                        if (link->manager->test_mode) {
2,849✔
2888
                                log_link_debug(link, "Running in test mode, refusing to enter initialized state.");
1✔
2889
                                link_set_state(link, LINK_STATE_UNMANAGED);
1✔
2890
                                return 0;
1✔
2891
                        }
2892

2893
                        /* Do not enter initialized state if we are enumerating. */
2894
                        if (manager->enumerating)
2,848✔
2895
                                return 0;
2896

2897
                        r = link_check_initialized(link);
947✔
2898
                        if (r < 0) {
947✔
2899
                                log_link_warning_errno(link, r, "Failed to check link is initialized: %m");
×
2900
                                link_enter_failed(link);
×
2901
                                return 0;
×
2902
                        }
2903
                } else {
2904
                        r = link_update(link, message);
2,943✔
2905
                        if (r < 0) {
2,943✔
2906
                                log_link_warning_errno(link, r, "Could not process link message: %m");
10✔
2907
                                link_enter_failed(link);
10✔
2908
                                return 0;
10✔
2909
                        }
2910
                        if (r == 0)
2,933✔
2911
                                return 0;
2912

2913
                        if (link->manager->test_mode) {
266✔
2914
                                log_link_debug(link, "Running in test mode, refusing to configure interface.");
×
2915
                                link_set_state(link, LINK_STATE_UNMANAGED);
×
2916
                                return 0;
×
2917
                        }
2918

2919
                        /* Do not configure interface if we are enumerating. */
2920
                        if (manager->enumerating)
266✔
2921
                                return 0;
2922

2923
                        r = link_reconfigure_impl(link, /* flags= */ 0);
266✔
2924
                        if (r < 0) {
266✔
2925
                                log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
×
2926
                                link_enter_failed(link);
×
2927
                                return 0;
×
2928
                        }
2929
                }
2930
                break;
2931

2932
        case RTM_DELLINK:
900✔
2933
                link_drop(link);
900✔
2934
                netdev_drop(netdev);
900✔
2935
                break;
2936

2937
        default:
×
2938
                assert_not_reached();
×
2939
        }
2940

2941
        return 1;
2942
}
2943

2944
int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
4,991✔
2945
        uint16_t message_type;
4,991✔
2946
        int r;
4,991✔
2947

2948
        assert(m);
4,991✔
2949
        assert(link);
4,991✔
2950
        assert(error_msg);
4,991✔
2951

2952
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
4,991✔
2953
                return 0;
4,991✔
2954

2955
        r = sd_netlink_message_get_errno(m);
4,979✔
2956
        if (r < 0) {
4,979✔
2957
                log_link_message_warning_errno(link, m, r, "%s", error_msg);
×
2958
                link_enter_failed(link);
×
2959
                return 0;
×
2960
        }
2961

2962
        r = sd_netlink_message_get_type(m, &message_type);
4,979✔
2963
        if (r < 0) {
4,979✔
2964
                log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
×
2965
                return 0;
×
2966
        }
2967
        if (message_type != RTM_NEWLINK) {
4,979✔
2968
                log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
×
2969
                return 0;
×
2970
        }
2971

2972
        r = link_update(link, m);
4,979✔
2973
        if (r < 0) {
4,979✔
2974
                link_enter_failed(link);
×
2975
                return 0;
×
2976
        }
2977

2978
        return 1;
2979
}
2980

2981
int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
3,625✔
2982
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
3,625✔
2983
        int r;
3,625✔
2984

2985
        assert(link);
3,625✔
2986
        assert(link->manager);
3,625✔
2987
        assert(link->manager->rtnl);
3,625✔
2988
        assert(callback);
3,625✔
2989

2990
        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
3,625✔
2991
        if (r < 0)
3,625✔
2992
                return r;
2993

2994
        r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
3,625✔
2995
                               link_netlink_destroy_callback, link);
2996
        if (r < 0)
3,625✔
2997
                return r;
2998

2999
        link_ref(link);
3,625✔
3000
        return 0;
3001
}
3002

3003
static const char* const link_state_table[_LINK_STATE_MAX] = {
3004
        [LINK_STATE_PENDING]     = "pending",
3005
        [LINK_STATE_INITIALIZED] = "initialized",
3006
        [LINK_STATE_CONFIGURING] = "configuring",
3007
        [LINK_STATE_CONFIGURED]  = "configured",
3008
        [LINK_STATE_UNMANAGED]   = "unmanaged",
3009
        [LINK_STATE_FAILED]      = "failed",
3010
        [LINK_STATE_LINGER]      = "linger",
3011
};
3012

3013
DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
34,832✔
3014

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

3039
        assert(ret);
2,034✔
3040

3041
        for (size_t i = 0; i < ELEMENTSOF(map); i++)
40,680✔
3042
                if (BIT_SET(flags, i) && map[i])
38,646✔
3043
                        if (!strextend_with_separator(&str, ",", map[i]))
8,353✔
3044
                                return -ENOMEM;
3045

3046
        *ret = TAKE_PTR(str);
2,034✔
3047
        return 0;
2,034✔
3048
}
3049

3050
static const char * const kernel_operstate_table[] = {
3051
        [IF_OPER_UNKNOWN]        = "unknown",
3052
        [IF_OPER_NOTPRESENT]     = "not-present",
3053
        [IF_OPER_DOWN]           = "down",
3054
        [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
3055
        [IF_OPER_TESTING]        = "testing",
3056
        [IF_OPER_DORMANT]        = "dormant",
3057
        [IF_OPER_UP]             = "up",
3058
};
3059

3060
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);
2,034✔
3061

3062
bool link_has_local_lease_domain(Link *link) {
15,985✔
3063
        assert(link);
15,985✔
3064

3065
        return link->dhcp_server &&
16,154✔
3066
                link->network &&
169✔
3067
                link->network->dhcp_server_local_lease_domain &&
16,169✔
3068
                !dns_name_is_root(link->network->dhcp_server_local_lease_domain);
15✔
3069
}
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