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

systemd / systemd / 14766779411

30 Apr 2025 04:55PM UTC coverage: 72.225% (-0.06%) from 72.282%
14766779411

push

github

web-flow
wait-online: handle varlink connection errors while waiting for DNS (#37283)

Currently, if systemd-networkd-wait-online is started with --dns, and
systemd-resolved is not running, it will exit with an error right away.
Similarly, if systemd-resolved is restarted while waiting for DNS
configuration, systemd-networkd-wait-online will not attempt to
re-connect, and will potentially never see subsequent DNS
configurations.

Improve this by adding socket units for the systemd-resolved varlink
servers, and re-establish the connection in systemd-networkd-wait-online
when we receive `SD_VARLINK_ERROR_DISCONNECTED`.

8 of 16 new or added lines in 2 files covered. (50.0%)

5825 existing lines in 217 files now uncovered.

297168 of 411450 relevant lines covered (72.22%)

695892.62 hits per line

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

88.51
/src/network/netdev/netdev.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <linux/if_arp.h>
4
#include <net/if.h>
5
#include <netinet/in.h>
6
#include <unistd.h>
7

8
#include "alloc-util.h"
9
#include "arphrd-util.h"
10
#include "bareudp.h"
11
#include "batadv.h"
12
#include "bond.h"
13
#include "bridge.h"
14
#include "conf-files.h"
15
#include "conf-parser.h"
16
#include "dummy.h"
17
#include "fd-util.h"
18
#include "fou-tunnel.h"
19
#include "geneve.h"
20
#include "hsr.h"
21
#include "ifb.h"
22
#include "ipoib.h"
23
#include "ipvlan.h"
24
#include "l2tp-tunnel.h"
25
#include "list.h"
26
#include "macsec.h"
27
#include "macvlan.h"
28
#include "netdev.h"
29
#include "netif-util.h"
30
#include "netlink-util.h"
31
#include "network-util.h"
32
#include "networkd-manager.h"
33
#include "networkd-queue.h"
34
#include "networkd-setlink.h"
35
#include "networkd-sriov.h"
36
#include "networkd-state-file.h"
37
#include "nlmon.h"
38
#include "path-lookup.h"
39
#include "siphash24.h"
40
#include "stat-util.h"
41
#include "string-table.h"
42
#include "string-util.h"
43
#include "strv.h"
44
#include "tunnel.h"
45
#include "tuntap.h"
46
#include "vcan.h"
47
#include "veth.h"
48
#include "vlan.h"
49
#include "vrf.h"
50
#include "vxcan.h"
51
#include "vxlan.h"
52
#include "wireguard.h"
53
#include "wlan.h"
54
#include "xfrm.h"
55

56
const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX] = {
57
        [NETDEV_KIND_BAREUDP]   = &bare_udp_vtable,
58
        [NETDEV_KIND_BATADV]    = &batadv_vtable,
59
        [NETDEV_KIND_BOND]      = &bond_vtable,
60
        [NETDEV_KIND_BRIDGE]    = &bridge_vtable,
61
        [NETDEV_KIND_DUMMY]     = &dummy_vtable,
62
        [NETDEV_KIND_ERSPAN]    = &erspan_vtable,
63
        [NETDEV_KIND_FOU]       = &foutnl_vtable,
64
        [NETDEV_KIND_GENEVE]    = &geneve_vtable,
65
        [NETDEV_KIND_GRE]       = &gre_vtable,
66
        [NETDEV_KIND_GRETAP]    = &gretap_vtable,
67
        [NETDEV_KIND_HSR]       = &hsr_vtable,
68
        [NETDEV_KIND_IFB]       = &ifb_vtable,
69
        [NETDEV_KIND_IP6GRE]    = &ip6gre_vtable,
70
        [NETDEV_KIND_IP6GRETAP] = &ip6gretap_vtable,
71
        [NETDEV_KIND_IP6TNL]    = &ip6tnl_vtable,
72
        [NETDEV_KIND_IPIP]      = &ipip_vtable,
73
        [NETDEV_KIND_IPOIB]     = &ipoib_vtable,
74
        [NETDEV_KIND_IPVLAN]    = &ipvlan_vtable,
75
        [NETDEV_KIND_IPVTAP]    = &ipvtap_vtable,
76
        [NETDEV_KIND_L2TP]      = &l2tptnl_vtable,
77
        [NETDEV_KIND_MACSEC]    = &macsec_vtable,
78
        [NETDEV_KIND_MACVLAN]   = &macvlan_vtable,
79
        [NETDEV_KIND_MACVTAP]   = &macvtap_vtable,
80
        [NETDEV_KIND_NLMON]     = &nlmon_vtable,
81
        [NETDEV_KIND_SIT]       = &sit_vtable,
82
        [NETDEV_KIND_TAP]       = &tap_vtable,
83
        [NETDEV_KIND_TUN]       = &tun_vtable,
84
        [NETDEV_KIND_VCAN]      = &vcan_vtable,
85
        [NETDEV_KIND_VETH]      = &veth_vtable,
86
        [NETDEV_KIND_VLAN]      = &vlan_vtable,
87
        [NETDEV_KIND_VRF]       = &vrf_vtable,
88
        [NETDEV_KIND_VTI6]      = &vti6_vtable,
89
        [NETDEV_KIND_VTI]       = &vti_vtable,
90
        [NETDEV_KIND_VXCAN]     = &vxcan_vtable,
91
        [NETDEV_KIND_VXLAN]     = &vxlan_vtable,
92
        [NETDEV_KIND_WIREGUARD] = &wireguard_vtable,
93
        [NETDEV_KIND_WLAN]      = &wlan_vtable,
94
        [NETDEV_KIND_XFRM]      = &xfrm_vtable,
95
};
96

97
static const char* const netdev_kind_table[_NETDEV_KIND_MAX] = {
98
        [NETDEV_KIND_BAREUDP]   = "bareudp",
99
        [NETDEV_KIND_BATADV]    = "batadv",
100
        [NETDEV_KIND_BOND]      = "bond",
101
        [NETDEV_KIND_BRIDGE]    = "bridge",
102
        [NETDEV_KIND_DUMMY]     = "dummy",
103
        [NETDEV_KIND_ERSPAN]    = "erspan",
104
        [NETDEV_KIND_FOU]       = "fou",
105
        [NETDEV_KIND_GENEVE]    = "geneve",
106
        [NETDEV_KIND_GRE]       = "gre",
107
        [NETDEV_KIND_GRETAP]    = "gretap",
108
        [NETDEV_KIND_HSR]       = "hsr",
109
        [NETDEV_KIND_IFB]       = "ifb",
110
        [NETDEV_KIND_IP6GRE]    = "ip6gre",
111
        [NETDEV_KIND_IP6GRETAP] = "ip6gretap",
112
        [NETDEV_KIND_IP6TNL]    = "ip6tnl",
113
        [NETDEV_KIND_IPIP]      = "ipip",
114
        [NETDEV_KIND_IPOIB]     = "ipoib",
115
        [NETDEV_KIND_IPVLAN]    = "ipvlan",
116
        [NETDEV_KIND_IPVTAP]    = "ipvtap",
117
        [NETDEV_KIND_L2TP]      = "l2tp",
118
        [NETDEV_KIND_MACSEC]    = "macsec",
119
        [NETDEV_KIND_MACVLAN]   = "macvlan",
120
        [NETDEV_KIND_MACVTAP]   = "macvtap",
121
        [NETDEV_KIND_NLMON]     = "nlmon",
122
        [NETDEV_KIND_SIT]       = "sit",
123
        [NETDEV_KIND_TAP]       = "tap",
124
        [NETDEV_KIND_TUN]       = "tun",
125
        [NETDEV_KIND_VCAN]      = "vcan",
126
        [NETDEV_KIND_VETH]      = "veth",
127
        [NETDEV_KIND_VLAN]      = "vlan",
128
        [NETDEV_KIND_VRF]       = "vrf",
129
        [NETDEV_KIND_VTI6]      = "vti6",
130
        [NETDEV_KIND_VTI]       = "vti",
131
        [NETDEV_KIND_VXCAN]     = "vxcan",
132
        [NETDEV_KIND_VXLAN]     = "vxlan",
133
        [NETDEV_KIND_WIREGUARD] = "wireguard",
134
        [NETDEV_KIND_WLAN]      = "wlan",
135
        [NETDEV_KIND_XFRM]      = "xfrm",
136
};
137

138
DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
6,504✔
139

140
bool netdev_is_managed(NetDev *netdev) {
3,099✔
141
        if (!netdev || !netdev->manager || !netdev->ifname)
3,099✔
142
                return false;
143

144
        return hashmap_get(netdev->manager->netdevs, netdev->ifname) == netdev;
3,099✔
145
}
146

147
static bool netdev_is_stacked_and_independent(NetDev *netdev) {
374✔
148
        assert(netdev);
374✔
149

150
        if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
374✔
151
                return false;
152

153
        switch (netdev->kind) {
374✔
154
        case NETDEV_KIND_ERSPAN:
30✔
155
                return ERSPAN(netdev)->independent;
30✔
156
        case NETDEV_KIND_GRE:
30✔
157
                return GRE(netdev)->independent;
30✔
158
        case NETDEV_KIND_GRETAP:
13✔
159
                return GRETAP(netdev)->independent;
13✔
160
        case NETDEV_KIND_IP6GRE:
27✔
161
                return IP6GRE(netdev)->independent;
27✔
162
        case NETDEV_KIND_IP6GRETAP:
10✔
163
                return IP6GRETAP(netdev)->independent;
10✔
164
        case NETDEV_KIND_IP6TNL:
26✔
165
                return IP6TNL(netdev)->independent;
26✔
166
        case NETDEV_KIND_IPIP:
27✔
167
                return IPIP(netdev)->independent;
27✔
168
        case NETDEV_KIND_SIT:
37✔
169
                return SIT(netdev)->independent;
37✔
170
        case NETDEV_KIND_VTI:
20✔
171
                return VTI(netdev)->independent;
20✔
172
        case NETDEV_KIND_VTI6:
15✔
173
                return VTI6(netdev)->independent;
15✔
174
        case NETDEV_KIND_VXLAN:
15✔
175
                return VXLAN(netdev)->independent;
15✔
176
        case NETDEV_KIND_XFRM:
8✔
177
                return XFRM(netdev)->independent;
8✔
178
        default:
179
                return false;
180
        }
181
}
182

183
static bool netdev_is_stacked(NetDev *netdev) {
1,292✔
184
        assert(netdev);
1,292✔
185

186
        if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
1,292✔
187
                return false;
188

189
        if (netdev_is_stacked_and_independent(netdev))
374✔
190
                return false;
26✔
191

192
        return true;
193
}
194

195
NetDev* netdev_detach_name(NetDev *netdev, const char *name) {
2,488✔
196
        assert(netdev);
2,488✔
197

198
        if (!netdev->manager || !name)
2,488✔
199
                return NULL; /* Already detached or not attached yet. */
200

201
        return hashmap_remove_value(netdev->manager->netdevs, name, netdev);
1,097✔
202
}
203

204
static NetDev* netdev_detach_impl(NetDev *netdev) {
2,168✔
205
        assert(netdev);
2,168✔
206

207
        if (netdev->state != _NETDEV_STATE_INVALID &&
2,168✔
208
            NETDEV_VTABLE(netdev) &&
1,445✔
209
            NETDEV_VTABLE(netdev)->detach)
1,445✔
210
                NETDEV_VTABLE(netdev)->detach(netdev);
312✔
211

212
        NetDev *n = netdev_detach_name(netdev, netdev->ifname);
2,168✔
213

214
        netdev->manager = NULL;
2,168✔
215
        return n; /* Return NULL when it is not attached yet, or already detached. */
2,168✔
216
}
217

218
void netdev_detach(NetDev *netdev) {
722✔
219
        assert(netdev);
722✔
220

221
        netdev_unref(netdev_detach_impl(netdev));
722✔
222
}
722✔
223

224
static NetDev* netdev_free(NetDev *netdev) {
1,446✔
225
        assert(netdev);
1,446✔
226

227
        netdev_detach_impl(netdev);
1,446✔
228

229
        /* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
230
         * because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure
231
         * allocation, with no room for per-kind fields), and once to read the kind's properties (with a full,
232
         * comprehensive NetDev structure allocation with enough space for whatever the specific kind needs). Now, in
233
         * the first case we shouldn't try to destruct the per-kind NetDev fields on destruction, in the second case we
234
         * should. We use the state field to discern the two cases: it's _NETDEV_STATE_INVALID on the first "raw"
235
         * call. */
236
        if (netdev->state != _NETDEV_STATE_INVALID &&
1,446✔
237
            NETDEV_VTABLE(netdev) &&
723✔
238
            NETDEV_VTABLE(netdev)->done)
723✔
239
                NETDEV_VTABLE(netdev)->done(netdev);
218✔
240

241
        condition_free_list(netdev->conditions);
1,446✔
242
        free(netdev->filename);
1,446✔
243
        strv_free(netdev->dropins);
1,446✔
244
        hashmap_free(netdev->stats_by_path);
1,446✔
245
        free(netdev->description);
1,446✔
246
        free(netdev->ifname);
1,446✔
247

248
        return mfree(netdev);
1,446✔
249
}
250

251
DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev, netdev_free);
29,234✔
252

253
void netdev_drop(NetDev *netdev) {
822✔
254
        if (!netdev)
822✔
255
                return;
256

257
        if (netdev_is_stacked(netdev)) {
428✔
258
                /* The netdev may be removed due to the underlying device removal, and the device may
259
                 * be re-added later. */
260
                netdev->state = NETDEV_STATE_LOADING;
74✔
261
                netdev->ifindex = 0;
74✔
262

263
                log_netdev_debug(netdev, "netdev removed");
74✔
264
                return;
74✔
265
        }
266

267
        if (NETDEV_VTABLE(netdev) && NETDEV_VTABLE(netdev)->drop)
354✔
268
                NETDEV_VTABLE(netdev)->drop(netdev);
4✔
269

270
        netdev->state = NETDEV_STATE_LINGER;
354✔
271

272
        log_netdev_debug(netdev, "netdev removed");
354✔
273

274
        netdev_detach(netdev);
354✔
275
}
276

277
static int netdev_attach_name_full(NetDev *netdev, const char *name, Hashmap **netdevs) {
1,143✔
278
        int r;
1,143✔
279

280
        assert(netdev);
1,143✔
281
        assert(name);
1,143✔
282

283
        r = hashmap_ensure_put(netdevs, &string_hash_ops, name, netdev);
1,143✔
284
        if (r == -ENOMEM)
1,143✔
UNCOV
285
                return log_oom();
×
286
        if (r == -EEXIST) {
1,143✔
287
                NetDev *n = hashmap_get(*netdevs, name);
1✔
288

289
                assert(n);
1✔
290
                if (!streq(netdev->filename, n->filename))
1✔
291
                        log_netdev_warning_errno(netdev, r,
1✔
292
                                                 "Device \"%s\" was already configured by \"%s\", ignoring %s.",
293
                                                 name, n->filename, netdev->filename);
294

295
                return -EEXIST;
1✔
296
        }
297
        assert(r > 0);
1,142✔
298

299
        return 0;
300
}
301

302
int netdev_attach_name(NetDev *netdev, const char *name) {
883✔
303
        assert(netdev);
883✔
304
        assert(netdev->manager);
883✔
305

306
        return netdev_attach_name_full(netdev, name, &netdev->manager->netdevs);
883✔
307
}
308

309
static int netdev_attach(NetDev *netdev) {
723✔
310
        int r;
723✔
311

312
        assert(netdev);
723✔
313
        assert(netdev->ifname);
723✔
314

315
        r = netdev_attach_name(netdev, netdev->ifname);
723✔
316
        if (r < 0)
723✔
317
                return r;
318

319
        if (NETDEV_VTABLE(netdev)->attach) {
722✔
320
                r = NETDEV_VTABLE(netdev)->attach(netdev);
156✔
321
                if (r < 0)
156✔
UNCOV
322
                        return r;
×
323
        }
324

325
        return 0;
326
}
327

328
int netdev_get(Manager *manager, const char *name, NetDev **ret) {
12,713✔
329
        NetDev *netdev;
12,713✔
330

331
        assert(manager);
12,713✔
332
        assert(name);
12,713✔
333
        assert(ret);
12,713✔
334

335
        netdev = hashmap_get(manager->netdevs, name);
12,713✔
336
        if (!netdev)
12,713✔
337
                return -ENOENT;
338

339
        *ret = netdev;
5,383✔
340

341
        return 0;
5,383✔
342
}
343

344
void link_assign_netdev(Link *link) {
6,315✔
345
        _unused_ _cleanup_(netdev_unrefp) NetDev *old = NULL;
6,315✔
346
        NetDev *netdev;
6,315✔
347

348
        assert(link);
6,315✔
349
        assert(link->manager);
6,315✔
350
        assert(link->ifname);
6,315✔
351

352
        old = TAKE_PTR(link->netdev);
6,315✔
353

354
        if (netdev_get(link->manager, link->ifname, &netdev) < 0)
6,315✔
355
                goto not_found;
4,327✔
356

357
        int ifindex = NETDEV_VTABLE(netdev)->get_ifindex ?
3,251✔
358
                NETDEV_VTABLE(netdev)->get_ifindex(netdev, link->ifname) :
1,988✔
359
                netdev->ifindex;
360
        if (ifindex != link->ifindex)
1,988✔
361
                goto not_found;
22✔
362

363
        if (NETDEV_VTABLE(netdev)->iftype != link->iftype)
1,966✔
UNCOV
364
                goto not_found;
×
365

366
        if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
1,966✔
367
                const char *kind;
1,954✔
368

369
                if (netdev->kind == NETDEV_KIND_TAP)
1,954✔
370
                        kind = "tun"; /* the kernel does not distinguish between tun and tap */
371
                else
372
                        kind = netdev_kind_to_string(netdev->kind);
1,941✔
373

374
                if (!streq_ptr(kind, link->kind))
1,954✔
UNCOV
375
                        goto not_found;
×
376
        }
377

378
        link->netdev = netdev_ref(netdev);
1,966✔
379

380
        if (netdev == old)
1,966✔
381
                return; /* The same NetDev found. */
382

383
        log_link_debug(link, "Found matching .netdev file: %s", netdev->filename);
648✔
384
        link_dirty(link);
648✔
385
        return;
386

387
not_found:
4,349✔
388

389
        if (old)
4,349✔
390
                /* Previously assigned NetDev is detached from Manager? Update the state file. */
391
                link_dirty(link);
8✔
392
}
393

394
void netdev_enter_failed(NetDev *netdev) {
19✔
395
        netdev->state = NETDEV_STATE_FAILED;
19✔
396
}
19✔
397

398
int netdev_enter_ready(NetDev *netdev) {
655✔
399
        assert(netdev);
655✔
400
        assert(netdev->ifname);
655✔
401

402
        if (!IN_SET(netdev->state, NETDEV_STATE_LOADING, NETDEV_STATE_CREATING))
655✔
403
                return 0;
404

405
        netdev->state = NETDEV_STATE_READY;
558✔
406

407
        log_netdev_info(netdev, "netdev ready");
558✔
408

409
        if (NETDEV_VTABLE(netdev)->post_create)
558✔
410
                NETDEV_VTABLE(netdev)->post_create(netdev, NULL);
25✔
411

412
        return 0;
413
}
414

415
bool netdev_needs_reconfigure(NetDev *netdev, NetDevLocalAddressType type) {
348✔
416
        assert(netdev);
348✔
417
        assert(type < _NETDEV_LOCAL_ADDRESS_TYPE_MAX);
348✔
418

419
        if (type < 0)
348✔
420
                return true;
421

422
        return NETDEV_VTABLE(netdev)->needs_reconfigure &&
486✔
423
                NETDEV_VTABLE(netdev)->needs_reconfigure(netdev, type);
140✔
424
}
425

426
/* callback for netdev's created without a backing Link */
427
static int netdev_create_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) {
402✔
428
        int r;
402✔
429

430
        assert(netdev);
402✔
431
        assert(netdev->state != _NETDEV_STATE_INVALID);
402✔
432

433
        r = sd_netlink_message_get_errno(m);
402✔
434
        if (r >= 0)
402✔
435
                log_netdev_debug(netdev, "Created.");
392✔
436
        else if (r == -EEXIST && netdev->ifindex > 0)
10✔
UNCOV
437
                log_netdev_debug(netdev, "Already exists.");
×
438
        else {
439
                log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
10✔
440
                netdev_enter_failed(netdev);
10✔
441
                return 0;
10✔
442
        }
443

444
        return netdev_enter_ready(netdev);
392✔
445
}
446

447
int netdev_set_ifindex_internal(NetDev *netdev, int ifindex) {
1,934✔
448
        assert(netdev);
1,934✔
449
        assert(ifindex > 0);
1,934✔
450

451
        if (netdev->ifindex == ifindex)
1,934✔
452
                return 0; /* Already set. */
453

454
        if (netdev->ifindex > 0 && netdev->ifindex != ifindex)
474✔
UNCOV
455
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EEXIST),
×
456
                                                "Could not set ifindex to %i, already set to %i.",
457
                                                ifindex, netdev->ifindex);
458

459
        netdev->ifindex = ifindex;
474✔
460
        log_netdev_debug(netdev, "Gained index %i.", ifindex);
474✔
461
        return 1; /* set new ifindex. */
462
}
463

464
static int netdev_set_ifindex_impl(NetDev *netdev, const char *name, int ifindex) {
2,397✔
465
        assert(netdev);
2,397✔
466
        assert(name);
2,397✔
467
        assert(ifindex > 0);
2,397✔
468

469
        if (NETDEV_VTABLE(netdev)->set_ifindex)
2,397✔
470
                return NETDEV_VTABLE(netdev)->set_ifindex(netdev, name, ifindex);
939✔
471

472
        if (!streq(netdev->ifname, name))
1,458✔
UNCOV
473
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
×
474
                                                "Received netlink message with unexpected interface name %s (ifindex=%i).",
475
                                                name, ifindex);
476

477
        return netdev_set_ifindex_internal(netdev, ifindex);
1,458✔
478
}
479

480
int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) {
2,543✔
481
        uint16_t type;
2,543✔
482
        const char *kind;
2,543✔
483
        const char *received_kind;
2,543✔
484
        const char *received_name;
2,543✔
485
        int r, ifindex, family;
2,543✔
486

487
        assert(netdev);
2,543✔
488
        assert(message);
2,543✔
489

490
        r = sd_netlink_message_get_type(message, &type);
2,543✔
491
        if (r < 0)
2,543✔
UNCOV
492
                return log_netdev_warning_errno(netdev, r, "Could not get rtnl message type: %m");
×
493

494
        if (type != RTM_NEWLINK)
2,543✔
UNCOV
495
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Cannot set ifindex from unexpected rtnl message type.");
×
496

497
        r = sd_rtnl_message_get_family(message, &family);
2,543✔
498
        if (r < 0)
2,543✔
UNCOV
499
                return log_netdev_warning_errno(netdev, r, "Failed to get family from received rtnl message: %m");
×
500

501
        if (family != AF_UNSPEC)
2,543✔
502
                return 0; /* IFLA_LINKINFO is only contained in the message with AF_UNSPEC. */
503

504
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2,397✔
505
        if (r < 0)
2,397✔
UNCOV
506
                return log_netdev_warning_errno(netdev, r, "Could not get ifindex: %m");
×
507
        if (ifindex <= 0)
2,397✔
UNCOV
508
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Got invalid ifindex: %d", ifindex);
×
509

510
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &received_name);
2,397✔
511
        if (r < 0)
2,397✔
UNCOV
512
                return log_netdev_warning_errno(netdev, r, "Could not get IFNAME: %m");
×
513

514
        if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
2,397✔
515

516
                r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2,383✔
517
                if (r < 0)
2,383✔
UNCOV
518
                        return log_netdev_warning_errno(netdev, r, "Could not get LINKINFO: %m");
×
519

520
                r = sd_netlink_message_read_string(message, IFLA_INFO_KIND, &received_kind);
2,383✔
521
                if (r < 0)
2,383✔
UNCOV
522
                        return log_netdev_warning_errno(netdev, r, "Could not get KIND: %m");
×
523

524
                r = sd_netlink_message_exit_container(message);
2,383✔
525
                if (r < 0)
2,383✔
UNCOV
526
                        return log_netdev_warning_errno(netdev, r, "Could not exit container: %m");
×
527

528
                if (netdev->kind == NETDEV_KIND_TAP)
2,383✔
529
                        /* the kernel does not distinguish between tun and tap */
530
                        kind = "tun";
531
                else
532
                        kind = netdev_kind_to_string(netdev->kind);
2,365✔
533
                if (!kind)
2,365✔
UNCOV
534
                        return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Could not get netdev kind.");
×
535

536
                if (!streq(kind, received_kind))
2,383✔
UNCOV
537
                        return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
×
538
                                                        "Received newlink with wrong KIND %s, expected %s",
539
                                                        received_kind, kind);
540
        }
541

542
        return netdev_set_ifindex_impl(netdev, received_name, ifindex);
2,397✔
543
}
544

545
#define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
546

547
int netdev_generate_hw_addr(
611✔
548
                NetDev *netdev,
549
                Link *parent,
550
                const char *name,
551
                const struct hw_addr_data *hw_addr,
552
                struct hw_addr_data *ret) {
553

554
        struct hw_addr_data a = HW_ADDR_NULL;
611✔
555
        bool is_static = false;
611✔
556
        int r;
611✔
557

558
        assert(netdev);
611✔
559
        assert(name);
611✔
560
        assert(hw_addr);
611✔
561
        assert(ret);
611✔
562

563
        if (hw_addr_equal(hw_addr, &HW_ADDR_NONE)) {
611✔
564
                *ret = HW_ADDR_NULL;
1✔
565
                return 0;
1✔
566
        }
567

568
        if (hw_addr->length == 0) {
610✔
569
                uint64_t result;
437✔
570

571
                /* HardwareAddress= is not specified. */
572

573
                if (!NETDEV_VTABLE(netdev)->generate_mac)
437✔
574
                        goto finalize;
103✔
575

576
                if (!IN_SET(NETDEV_VTABLE(netdev)->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND))
334✔
UNCOV
577
                        goto finalize;
×
578

579
                r = net_get_unique_predictable_data_from_name(name, &HASH_KEY, &result);
334✔
580
                if (r < 0) {
334✔
UNCOV
581
                        log_netdev_warning_errno(netdev, r,
×
582
                                                 "Failed to generate persistent MAC address, ignoring: %m");
UNCOV
583
                        goto finalize;
×
584
                }
585

586
                a.length = arphrd_to_hw_addr_len(NETDEV_VTABLE(netdev)->iftype);
334✔
587

588
                switch (NETDEV_VTABLE(netdev)->iftype) {
334✔
589
                case ARPHRD_ETHER:
334✔
590
                        assert(a.length <= sizeof(result));
334✔
591
                        memcpy(a.bytes, &result, a.length);
334✔
592

593
                        if (ether_addr_is_null(&a.ether) || ether_addr_is_broadcast(&a.ether)) {
668✔
UNCOV
594
                                log_netdev_warning(netdev, "Failed to generate persistent MAC address, ignoring.");
×
595
                                a = HW_ADDR_NULL;
×
596
                                goto finalize;
×
597
                        }
598

599
                        break;
UNCOV
600
                case ARPHRD_INFINIBAND:
×
601
                        if (result == 0) {
×
602
                                log_netdev_warning(netdev, "Failed to generate persistent MAC address.");
×
603
                                goto finalize;
×
604
                        }
605

UNCOV
606
                        assert(a.length >= sizeof(result));
×
607
                        memzero(a.bytes, a.length - sizeof(result));
×
608
                        memcpy(a.bytes + a.length - sizeof(result), &result, sizeof(result));
×
609
                        break;
×
610
                default:
×
611
                        assert_not_reached();
×
612
                }
613

614
        } else {
615
                a = *hw_addr;
173✔
616
                is_static = true;
173✔
617
        }
618

619
        r = net_verify_hardware_address(name, is_static, NETDEV_VTABLE(netdev)->iftype,
507✔
620
                                        parent ? &parent->hw_addr : NULL, &a);
621
        if (r < 0)
507✔
622
                return r;
623

624
finalize:
507✔
625
        *ret = a;
610✔
626
        return 0;
610✔
627
}
628

629
static bool netdev_can_set_mac(NetDev *netdev, const struct hw_addr_data *hw_addr) {
526✔
630
        assert(netdev);
526✔
631
        assert(netdev->manager);
526✔
632
        assert(hw_addr);
526✔
633

634
        if (hw_addr->length <= 0)
526✔
635
                return false;
526✔
636

637
        Link *link;
422✔
638
        if (link_get_by_index(netdev->manager, netdev->ifindex, &link) < 0)
422✔
639
                return true; /* The netdev does not exist yet. We can set MAC address. */
640

641
        if (hw_addr_equal(&link->hw_addr, hw_addr))
53✔
642
                return false; /* Unchanged, not necessary to set. */
643

644
        /* Some netdevs refuse to update MAC address even if the interface is not running, e.g. ipvlan.
645
         * Some other netdevs have the IFF_LIVE_ADDR_CHANGE flag and can update update MAC address even if
646
         * the interface is running, e.g. dummy. For those cases, use custom checkers. */
647
        if (NETDEV_VTABLE(netdev)->can_set_mac)
5✔
648
                return NETDEV_VTABLE(netdev)->can_set_mac(netdev, hw_addr);
3✔
649

650
        /* Before ad72c4a06acc6762e84994ac2f722da7a07df34e and 0ec92a8f56ff07237dbe8af7c7a72aba7f957baf
651
         * (both in v6.5), the kernel refuse to set MAC address for existing netdevs even if it is unchanged.
652
         * So, by default, do not update MAC address if the it is running. See eth_prepare_mac_addr_change(),
653
         * which is called by eth_mac_addr(). Note, the result of netif_running() is mapped to operstate
654
         * and flags. See rtnl_fill_ifinfo() and dev_get_flags(). */
655
        return link->kernel_operstate == IF_OPER_DOWN &&
4✔
UNCOV
656
                (link->flags & (IFF_RUNNING | IFF_LOWER_UP | IFF_DORMANT)) == 0;
×
657
}
658

659
static bool netdev_can_set_mtu(NetDev *netdev, uint32_t mtu) {
526✔
660
        assert(netdev);
526✔
661

662
        if (mtu <= 0)
526✔
663
                return false;
526✔
664

665
        Link *link;
38✔
666
        if (link_get_by_index(netdev->manager, netdev->ifindex, &link) < 0)
38✔
667
                return true; /* The netdev does not exist yet. We can set MTU. */
668

669
        if (mtu < link->min_mtu || link->max_mtu < mtu)
9✔
670
                return false; /* The MTU is out of range. */
671

672
        if (link->mtu == mtu)
9✔
673
                return false; /* Unchanged, not necessary to set. */
674

675
        /* Some netdevs cannot change MTU, e.g. vxlan. Let's use the custom checkers in such cases. */
UNCOV
676
        if (NETDEV_VTABLE(netdev)->can_set_mtu)
×
677
                return NETDEV_VTABLE(netdev)->can_set_mtu(netdev, mtu);
×
678

679
        /* By default, allow to update the MTU. */
680
        return true;
681
}
682

683
static int netdev_create_message(NetDev *netdev, Link *link, sd_netlink_message *m) {
526✔
684
        int r;
526✔
685

686
        if (netdev->ifindex <= 0) {
526✔
687
                /* Set interface name when it is newly created. Otherwise, the kernel older than
688
                 * bd039b5ea2a91ea707ee8539df26456bd5be80af (v6.2) will refuse the netlink message even if
689
                 * the name is unchanged. */
690
                r = sd_netlink_message_append_string(m, IFLA_IFNAME, netdev->ifname);
428✔
691
                if (r < 0)
428✔
692
                        return r;
526✔
693
        }
694

695
        struct hw_addr_data hw_addr;
526✔
696
        r = netdev_generate_hw_addr(netdev, link, netdev->ifname, &netdev->hw_addr, &hw_addr);
526✔
697
        if (r < 0)
526✔
698
                return r;
699

700
        if (netdev_can_set_mac(netdev, &hw_addr)) {
526✔
701
                log_netdev_debug(netdev, "Using MAC address: %s", HW_ADDR_TO_STR(&hw_addr));
372✔
702
                r = netlink_message_append_hw_addr(m, IFLA_ADDRESS, &hw_addr);
372✔
703
                if (r < 0)
372✔
704
                        return r;
705
        }
706

707
        if (netdev_can_set_mtu(netdev, netdev->mtu)) {
526✔
708
                r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu);
29✔
709
                if (r < 0)
29✔
710
                        return r;
711
        }
712

713
        if (link) {
526✔
714
                r = sd_netlink_message_append_u32(m, IFLA_LINK, link->ifindex);
124✔
715
                if (r < 0)
124✔
716
                        return r;
717
        }
718

719
        r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
526✔
720
        if (r < 0)
526✔
721
                return r;
722

723
        if (NETDEV_VTABLE(netdev)->fill_message_create) {
526✔
724
                r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind));
247✔
725
                if (r < 0)
247✔
726
                        return r;
727

728
                r = NETDEV_VTABLE(netdev)->fill_message_create(netdev, link, m);
247✔
729
                if (r < 0)
247✔
730
                        return r;
731

732
                r = sd_netlink_message_close_container(m);
247✔
733
                if (r < 0)
247✔
734
                        return r;
735
        } else {
736
                r = sd_netlink_message_append_string(m, IFLA_INFO_KIND, netdev_kind_to_string(netdev->kind));
279✔
737
                if (r < 0)
279✔
738
                        return r;
739
        }
740

741
        r = sd_netlink_message_close_container(m);
526✔
742
        if (r < 0)
526✔
UNCOV
743
                return r;
×
744

745
        return 0;
746
}
747

748
static int independent_netdev_create(NetDev *netdev) {
420✔
749
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
420✔
750
        int r;
420✔
751

752
        assert(netdev);
420✔
753
        assert(netdev->manager);
420✔
754

755
        /* create netdev */
756
        if (NETDEV_VTABLE(netdev)->create) {
420✔
757
                r = NETDEV_VTABLE(netdev)->create(netdev);
18✔
758
                if (r < 0)
18✔
759
                        return r;
760

761
                log_netdev_debug(netdev, "Created");
18✔
762
                return 0;
18✔
763
        }
764

765
        r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, netdev->ifindex);
402✔
766
        if (r < 0)
402✔
767
                return r;
768

769
        r = netdev_create_message(netdev, NULL, m);
402✔
770
        if (r < 0)
402✔
771
                return r;
772

773
        r = netlink_call_async(netdev->manager->rtnl, NULL, m, netdev_create_handler,
402✔
774
                               netdev_destroy_callback, netdev);
775
        if (r < 0)
402✔
776
                return r;
777

778
        netdev_ref(netdev);
402✔
779

780
        netdev->state = NETDEV_STATE_CREATING;
402✔
781
        log_netdev_debug(netdev, "Creating");
402✔
782
        return 0;
783
}
784

785
static int stacked_netdev_create(NetDev *netdev, Link *link, Request *req) {
124✔
786
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
124✔
787
        int r;
124✔
788

789
        assert(netdev);
124✔
790
        assert(netdev->manager);
124✔
791
        assert(link);
124✔
792
        assert(req);
124✔
793

794
        r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, netdev->ifindex);
124✔
795
        if (r < 0)
124✔
796
                return r;
797

798
        r = netdev_create_message(netdev, link, m);
124✔
799
        if (r < 0)
124✔
800
                return r;
801

802
        r = request_call_netlink_async(netdev->manager->rtnl, m, req);
124✔
803
        if (r < 0)
124✔
804
                return r;
805

806
        netdev->state = NETDEV_STATE_CREATING;
124✔
807
        log_netdev_debug(netdev, "Creating");
124✔
808
        return 0;
809
}
810

811
static bool link_is_ready_to_create_stacked_netdev_one(Link *link, bool allow_unmanaged) {
1,714✔
812
        assert(link);
1,714✔
813

814
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
1,714✔
815
                return false;
816

817
        if (!link->network)
1,714✔
818
                return allow_unmanaged;
819

820
        if (link->set_link_messages > 0)
1,714✔
821
                return false;
822

823
        /* If stacked netdevs are created before the underlying interface being activated, then
824
         * the activation policy for the netdevs are ignored. See issue #22593. */
825
        if (!link->activated)
1,624✔
826
                return false;
1,172✔
827

828
        return true;
829
}
830

831
static bool link_is_ready_to_create_stacked_netdev(Link *link) {
1,714✔
832
        return check_ready_for_all_sr_iov_ports(link, /* allow_unmanaged = */ false,
1,714✔
833
                                                link_is_ready_to_create_stacked_netdev_one);
834
}
835

836
static int netdev_is_ready_to_create(NetDev *netdev, Link *link) {
2,226✔
837
        assert(netdev);
2,226✔
838

839
        if (link && !link_is_ready_to_create_stacked_netdev(link))
2,226✔
840
                return false;
841

842
        if (NETDEV_VTABLE(netdev)->is_ready_to_create)
964✔
843
                return NETDEV_VTABLE(netdev)->is_ready_to_create(netdev, link);
533✔
844

845
        return true;
846
}
847

848
static int stacked_netdev_process_request(Request *req, Link *link, void *userdata) {
1,728✔
849
        NetDev *netdev = ASSERT_PTR(userdata);
1,728✔
850
        int r;
1,728✔
851

852
        assert(req);
1,728✔
853
        assert(link);
1,728✔
854

855
        if (!netdev_is_managed(netdev))
1,728✔
UNCOV
856
                goto cancelled; /* Already detached, due to e.g. reloading .netdev files, cancelling the request. */
×
857

858
        if (NETDEV_VTABLE(netdev)->keep_existing && netdev->ifindex > 0) {
1,728✔
859
                /* Already exists, and the netdev does not support updating, entering the ready state. */
860
                r = netdev_enter_ready(netdev);
14✔
861
                if (r < 0)
14✔
862
                        return r;
863

864
                goto cancelled;
14✔
865
        }
866

867
        r = netdev_is_ready_to_create(netdev, link);
1,714✔
868
        if (r <= 0)
1,714✔
869
                return r;
870

871
        r = stacked_netdev_create(netdev, link, req);
124✔
872
        if (r < 0)
124✔
UNCOV
873
                return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
×
874

875
        return 1;
876

877
cancelled:
14✔
878
        assert_se(TAKE_PTR(req->counter) == &link->create_stacked_netdev_messages);
14✔
879
        link->create_stacked_netdev_messages--;
14✔
880

881
        if (link->create_stacked_netdev_messages == 0) {
14✔
882
                link->stacked_netdevs_created = true;
14✔
883
                log_link_debug(link, "Stacked netdevs created.");
14✔
884
                link_check_ready(link);
14✔
885
        }
886

887
        return 1;
888
}
889

890
static int create_stacked_netdev_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, void *userdata) {
124✔
891
        NetDev *netdev = ASSERT_PTR(userdata);
124✔
892
        int r;
124✔
893

894
        assert(m);
124✔
895
        assert(link);
124✔
896

897
        r = sd_netlink_message_get_errno(m);
124✔
898
        if (r >= 0)
124✔
899
                log_netdev_debug(netdev, "Created.");
124✔
UNCOV
900
        else if (r == -EEXIST && netdev->ifindex > 0)
×
901
                log_netdev_debug(netdev, "Already exists.");
×
902
        else {
UNCOV
903
                log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
×
904
                netdev_enter_failed(netdev);
×
905
                link_enter_failed(link);
×
906
                return 0;
×
907
        }
908

909
        (void) netdev_enter_ready(netdev);
124✔
910

911
        if (link->create_stacked_netdev_messages == 0) {
124✔
912
                link->stacked_netdevs_created = true;
72✔
913
                log_link_debug(link, "Stacked netdevs created.");
72✔
914
                link_check_ready(link);
72✔
915
        }
916

917
        return 0;
918
}
919

920
int link_request_stacked_netdev(Link *link, NetDev *netdev) {
142✔
921
        int r;
142✔
922

923
        assert(link);
142✔
924
        assert(netdev);
142✔
925

926
        if (!netdev_is_stacked(netdev))
142✔
927
                return -EINVAL;
928

929
        if (!netdev_is_managed(netdev))
142✔
930
                return 0; /* Already detached, due to e.g. reloading .netdev files. */
931

932
        link->stacked_netdevs_created = false;
142✔
933
        r = link_queue_request_full(link, REQUEST_TYPE_NETDEV_STACKED,
142✔
934
                                    netdev, (mfree_func_t) netdev_unref,
935
                                    trivial_hash_func, trivial_compare_func,
936
                                    stacked_netdev_process_request,
937
                                    &link->create_stacked_netdev_messages,
938
                                    create_stacked_netdev_handler, NULL);
939
        if (r < 0)
142✔
UNCOV
940
                return log_link_error_errno(link, r, "Failed to request stacked netdev '%s': %m",
×
941
                                            netdev->ifname);
942
        if (r == 0)
142✔
943
                return 0;
944

945
        netdev_ref(netdev);
140✔
946
        log_link_debug(link, "Requested stacked netdev '%s'", netdev->ifname);
140✔
947
        return 1;
948
}
949

950
static int independent_netdev_process_request(Request *req, Link *link, void *userdata) {
528✔
951
        NetDev *netdev = ASSERT_PTR(userdata);
528✔
952
        int r;
528✔
953

954
        assert(!link);
528✔
955

956
        if (!netdev_is_managed(netdev))
528✔
957
                return 1; /* Already detached, due to e.g. reloading .netdev files, cancelling the request. */
958

959
        if (NETDEV_VTABLE(netdev)->keep_existing && netdev->ifindex > 0) {
528✔
960
                /* Already exists, and the netdev does not support updating, entering the ready state. */
961
                r = netdev_enter_ready(netdev);
16✔
962
                if (r < 0)
16✔
963
                        return r;
964

965
                return 1; /* Skip this request. */
16✔
966
        }
967

968
        r = netdev_is_ready_to_create(netdev, NULL);
512✔
969
        if (r <= 0)
512✔
970
                return r;
971

972
        r = independent_netdev_create(netdev);
420✔
973
        if (r < 0)
420✔
UNCOV
974
                return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
×
975

976
        return 1;
977
}
978

979
static int netdev_request_to_create(NetDev *netdev) {
722✔
980
        int r;
722✔
981

982
        assert(netdev);
722✔
983
        assert(netdev->manager);
722✔
984

985
        if (netdev->manager->test_mode)
722✔
986
                return 0;
987

988
        if (netdev_is_stacked(netdev))
722✔
989
                return 0;
990

991
        if (!netdev_is_managed(netdev))
590✔
992
                return 0; /* Already detached, due to e.g. reloading .netdev files. */
993

994
        if (netdev->state != NETDEV_STATE_LOADING)
590✔
995
                return 0; /* Already configured (at least tried previously). Not necessary to reconfigure. */
996

997
        r = netdev_queue_request(netdev, independent_netdev_process_request, NULL);
436✔
998
        if (r < 0)
436✔
UNCOV
999
                return log_netdev_warning_errno(netdev, r, "Failed to request to create netdev: %m");
×
1000

1001
        return 0;
1002
}
1003

1004
int netdev_load_one(Manager *manager, const char *filename, NetDev **ret) {
723✔
1005
        _cleanup_(netdev_unrefp) NetDev *netdev_raw = NULL, *netdev = NULL;
723✔
1006
        const char *dropin_dirname;
723✔
1007
        int r;
723✔
1008

1009
        assert(manager);
723✔
1010
        assert(filename);
723✔
1011
        assert(ret);
723✔
1012

1013
        r = null_or_empty_path(filename);
723✔
1014
        if (r < 0)
723✔
UNCOV
1015
                return log_warning_errno(r, "Failed to check if \"%s\" is empty: %m", filename);
×
1016
        if (r > 0)
723✔
UNCOV
1017
                return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Skipping empty file: %s", filename);
×
1018

1019
        netdev_raw = new(NetDev, 1);
723✔
1020
        if (!netdev_raw)
723✔
UNCOV
1021
                return log_oom();
×
1022

1023
        *netdev_raw = (NetDev) {
723✔
1024
                .n_ref = 1,
1025
                .kind = _NETDEV_KIND_INVALID,
1026
                .state = _NETDEV_STATE_INVALID, /* an invalid state means done() of the implementation won't be called on destruction */
1027
        };
1028

1029
        dropin_dirname = strjoina(basename(filename), ".d");
3,615✔
1030
        r = config_parse_many(
1,446✔
1031
                        STRV_MAKE_CONST(filename), NETWORK_DIRS, dropin_dirname, /* root = */ NULL,
723✔
1032
                        NETDEV_COMMON_SECTIONS NETDEV_OTHER_SECTIONS,
1033
                        config_item_perf_lookup, network_netdev_gperf_lookup,
1034
                        CONFIG_PARSE_WARN,
1035
                        netdev_raw,
1036
                        NULL,
1037
                        NULL);
1038
        if (r < 0)
723✔
1039
                return r; /* config_parse_many() logs internally. */
1040

1041
        /* skip out early if configuration does not match the environment */
1042
        if (!condition_test_list(netdev_raw->conditions, environ, NULL, NULL, NULL))
723✔
UNCOV
1043
                return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "%s: Conditions in the file do not match the system environment, skipping.", filename);
×
1044

1045
        if (netdev_raw->kind == _NETDEV_KIND_INVALID)
723✔
UNCOV
1046
                return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "NetDev has no Kind= configured in \"%s\", ignoring.", filename);
×
1047

1048
        if (!netdev_raw->ifname)
723✔
UNCOV
1049
                return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "NetDev without Name= configured in \"%s\", ignoring.", filename);
×
1050

1051
        netdev = malloc0(NETDEV_VTABLE(netdev_raw)->object_size);
723✔
1052
        if (!netdev)
723✔
UNCOV
1053
                return log_oom();
×
1054

1055
        netdev->n_ref = 1;
723✔
1056
        netdev->manager = manager;
723✔
1057
        netdev->kind = netdev_raw->kind;
723✔
1058
        netdev->state = NETDEV_STATE_LOADING; /* we initialize the state here for the first time,
723✔
1059
                                                 so that done() will be called on destruction */
1060

1061
        if (NETDEV_VTABLE(netdev)->init)
723✔
1062
                NETDEV_VTABLE(netdev)->init(netdev);
207✔
1063

1064
        r = config_parse_many(
2,169✔
1065
                        STRV_MAKE_CONST(filename), NETWORK_DIRS, dropin_dirname, /* root = */ NULL,
723✔
1066
                        NETDEV_VTABLE(netdev)->sections,
723✔
1067
                        config_item_perf_lookup, network_netdev_gperf_lookup,
1068
                        CONFIG_PARSE_WARN,
1069
                        netdev,
1070
                        &netdev->stats_by_path,
1071
                        &netdev->dropins);
723✔
1072
        if (r < 0)
723✔
1073
                return r; /* config_parse_many() logs internally. */
1074

1075
        /* verify configuration */
1076
        if (NETDEV_VTABLE(netdev)->config_verify) {
723✔
1077
                r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename);
306✔
1078
                if (r < 0)
306✔
1079
                        return r; /* config_verify() logs internally. */
1080
        }
1081

1082
        netdev->filename = strdup(filename);
723✔
1083
        if (!netdev->filename)
723✔
UNCOV
1084
                return log_oom();
×
1085

1086
        log_syntax(/* unit = */ NULL, LOG_DEBUG, filename, /* config_line = */ 0, /* error = */ 0, "Successfully loaded.");
723✔
1087

1088
        *ret = TAKE_PTR(netdev);
723✔
1089
        return 0;
723✔
1090
}
1091

1092
int netdev_load(Manager *manager) {
418✔
1093
        _cleanup_strv_free_ char **files = NULL;
418✔
1094
        int r;
418✔
1095

1096
        assert(manager);
418✔
1097

1098
        r = conf_files_list_strv(&files, ".netdev", NULL, 0, NETWORK_DIRS);
418✔
1099
        if (r < 0)
418✔
UNCOV
1100
                return log_error_errno(r, "Failed to enumerate netdev files: %m");
×
1101

1102
        STRV_FOREACH(f, files) {
881✔
1103
                _cleanup_(netdev_unrefp) NetDev *netdev = NULL;
1✔
1104

1105
                if (netdev_load_one(manager, *f, &netdev) < 0)
463✔
UNCOV
1106
                        continue;
×
1107

1108
                if (netdev_attach(netdev) < 0)
463✔
1109
                        continue;
1✔
1110

1111
                if (netdev_request_to_create(netdev) < 0)
462✔
UNCOV
1112
                        continue;
×
1113

1114
                TAKE_PTR(netdev);
462✔
1115
        }
1116

1117
        return 0;
1118
}
1119

1120
int netdev_reload(Manager *manager) {
136✔
1121
        _cleanup_hashmap_free_ Hashmap *new_netdevs = NULL;
136✔
1122
        _cleanup_strv_free_ char **files = NULL;
136✔
1123
        int r;
136✔
1124

1125
        assert(manager);
136✔
1126

1127
        r = conf_files_list_strv(&files, ".netdev", NULL, 0, NETWORK_DIRS);
136✔
1128
        if (r < 0)
136✔
UNCOV
1129
                return log_error_errno(r, "Failed to enumerate netdev files: %m");
×
1130

1131
        STRV_FOREACH(f, files) {
396✔
1132
                _cleanup_(netdev_unrefp) NetDev *netdev = NULL;
260✔
1133
                NetDev *old;
260✔
1134

1135
                if (netdev_load_one(manager, *f, &netdev) < 0)
260✔
UNCOV
1136
                        continue;
×
1137

1138
                if (netdev_get(manager, netdev->ifname, &old) < 0) {
260✔
1139
                        log_netdev_debug(netdev, "Found new .netdev file: %s", netdev->filename);
11✔
1140

1141
                        if (netdev_attach_name_full(netdev, netdev->ifname, &new_netdevs) >= 0)
11✔
1142
                                TAKE_PTR(netdev);
11✔
1143

1144
                        continue;
11✔
1145
                }
1146

1147
                if (!stats_by_path_equal(netdev->stats_by_path, old->stats_by_path)) {
249✔
1148
                        log_netdev_debug(netdev, "Found updated .netdev file: %s", netdev->filename);
90✔
1149

1150
                        /* Copy ifindex. */
1151
                        netdev->ifindex = old->ifindex;
90✔
1152

1153
                        if (netdev_attach_name_full(netdev, netdev->ifname, &new_netdevs) >= 0)
90✔
1154
                                TAKE_PTR(netdev);
90✔
1155

1156
                        continue;
90✔
1157
                }
1158

1159
                /* Keep the original object, and drop the new one. */
1160
                if (netdev_attach_name_full(old, old->ifname, &new_netdevs) >= 0)
159✔
1161
                        netdev_ref(old);
159✔
1162
        }
1163

1164
        /* Detach old NetDev objects from Manager.
1165
         * The same object may be registered with multiple names, and netdev_detach() may drop multiple entries. */
1166
        for (NetDev *n; (n = hashmap_first(manager->netdevs)); )
386✔
1167
                netdev_detach(n);
250✔
1168

1169
        /* Attach new NetDev objects to Manager. */
1170
        for (;;) {
396✔
1171
                _cleanup_(netdev_unrefp) NetDev *netdev = hashmap_steal_first(new_netdevs);
532✔
1172
                if (!netdev)
396✔
1173
                        break;
1174

1175
                netdev->manager = manager;
260✔
1176
                if (netdev_attach(netdev) < 0)
260✔
UNCOV
1177
                        continue;
×
1178

1179
                /* Create a new netdev or update existing netdev, */
1180
                if (netdev_request_to_create(netdev) < 0)
260✔
UNCOV
1181
                        continue;
×
1182

1183
                TAKE_PTR(netdev);
260✔
1184
        }
1185

1186
        /* Reassign NetDev objects to Link object. */
1187
        Link *link;
136✔
1188
        HASHMAP_FOREACH(link, manager->links_by_index)
1,190✔
1189
                link_assign_netdev(link);
1,054✔
1190

1191
        return 0;
136✔
1192
}
1193

1194
int config_parse_netdev_kind(
1,446✔
1195
                const char *unit,
1196
                const char *filename,
1197
                unsigned line,
1198
                const char *section,
1199
                unsigned section_line,
1200
                const char *lvalue,
1201
                int ltype,
1202
                const char *rvalue,
1203
                void *data,
1204
                void *userdata) {
1205

1206
        NetDevKind k, *kind = ASSERT_PTR(data);
1,446✔
1207

1208
        assert(filename);
1,446✔
1209
        assert(rvalue);
1,446✔
1210

1211
        k = netdev_kind_from_string(rvalue);
1,446✔
1212
        if (k < 0) {
1,446✔
UNCOV
1213
                log_syntax(unit, LOG_WARNING, filename, line, k, "Failed to parse netdev kind, ignoring assignment: %s", rvalue);
×
1214
                return 0;
×
1215
        }
1216

1217
        if (*kind != _NETDEV_KIND_INVALID && *kind != k) {
1,446✔
UNCOV
1218
                log_syntax(unit, LOG_WARNING, filename, line, 0,
×
1219
                           "Specified netdev kind is different from the previous value '%s', ignoring assignment: %s",
1220
                           netdev_kind_to_string(*kind), rvalue);
UNCOV
1221
                return 0;
×
1222
        }
1223

1224
        *kind = k;
1,446✔
1225

1226
        return 0;
1,446✔
1227
}
1228

1229
int config_parse_netdev_hw_addr(
465✔
1230
                const char *unit,
1231
                const char *filename,
1232
                unsigned line,
1233
                const char *section,
1234
                unsigned section_line,
1235
                const char *lvalue,
1236
                int ltype,
1237
                const char *rvalue,
1238
                void *data,
1239
                void *userdata) {
1240

1241
        struct hw_addr_data *hw_addr = ASSERT_PTR(data);
465✔
1242

1243
        assert(rvalue);
465✔
1244

1245
        if (streq(rvalue, "none")) {
465✔
1246
                *hw_addr = HW_ADDR_NONE;
2✔
1247
                return 0;
2✔
1248
        }
1249

1250
        return config_parse_hw_addr(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
463✔
1251
}
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