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

systemd / systemd / 21419361949

27 Jan 2026 02:53PM UTC coverage: 72.793% (-0.03%) from 72.821%
21419361949

push

github

keszybz
kernel-install: handle removal unsuccessful UKIs and loader entries separately

When a tries file exists, 90-uki-copy.install removes a previous UKI of the
same kernel version and all it's unbooted variants. This removal is guarded
behind a check for the existence of the already booted UKI, i.e. if uki.efi
already exists, uki.efi and uki+*.efi will be removed.

This leaves the edge case that if uki.efi does not exist, but only an unbooted,
e.g. uki+3.efi, it will not be removed. This is not a problem, if the number of
tries is constant between both builds, since a new uki+3.efi would overwrite
the existing one, but if the number of tries is changed to, e.g. uki+5.efi, we
are left with both uki+3.efi and uki+5.efi.

The same is done for loader entries.

311334 of 427698 relevant lines covered (72.79%)

1157141.18 hits per line

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

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

3
#include <linux/if_arp.h>
4
#include <unistd.h>
5

6
#include "sd-netlink.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 "condition.h"
15
#include "conf-files.h"
16
#include "conf-parser.h"
17
#include "dummy.h"
18
#include "fou-tunnel.h"
19
#include "geneve.h"
20
#include "hashmap.h"
21
#include "hsr.h"
22
#include "ifb.h"
23
#include "ipoib.h"
24
#include "ipvlan.h"
25
#include "l2tp-tunnel.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-sriov.h"
35
#include "networkd-state-file.h"
36
#include "nlmon.h"
37
#include "path-util.h"
38
#include "stat-util.h"
39
#include "string-table.h"
40
#include "string-util.h"
41
#include "strv.h"
42
#include "tunnel.h"
43
#include "tuntap.h"
44
#include "vcan.h"
45
#include "veth.h"
46
#include "vlan.h"
47
#include "vrf.h"
48
#include "vxcan.h"
49
#include "vxlan.h"
50
#include "wireguard.h"
51
#include "wlan.h"
52
#include "xfrm.h"
53

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

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

136
DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
7,362✔
137

138
bool netdev_is_managed(NetDev *netdev) {
4,382✔
139
        if (!netdev || !netdev->manager || !netdev->ifname)
4,382✔
140
                return false;
141

142
        return hashmap_get(netdev->manager->netdevs, netdev->ifname) == netdev;
4,382✔
143
}
144

145
static bool netdev_is_stacked_and_independent(NetDev *netdev) {
552✔
146
        assert(netdev);
552✔
147

148
        if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
552✔
149
                return false;
150

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

181
static bool netdev_is_stacked(NetDev *netdev) {
1,544✔
182
        assert(netdev);
1,544✔
183

184
        if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
1,544✔
185
                return false;
186

187
        if (netdev_is_stacked_and_independent(netdev))
552✔
188
                return false;
35✔
189

190
        return true;
191
}
192

193
NetDev* netdev_detach_name(NetDev *netdev, const char *name) {
2,894✔
194
        assert(netdev);
2,894✔
195

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

199
        return hashmap_remove_value(netdev->manager->netdevs, name, netdev);
1,271✔
200
}
201

202
static NetDev* netdev_detach_impl(NetDev *netdev) {
2,534✔
203
        assert(netdev);
2,534✔
204

205
        if (netdev->state != _NETDEV_STATE_INVALID &&
2,534✔
206
            NETDEV_VTABLE(netdev) &&
1,689✔
207
            NETDEV_VTABLE(netdev)->detach)
1,689✔
208
                NETDEV_VTABLE(netdev)->detach(netdev);
352✔
209

210
        NetDev *n = netdev_detach_name(netdev, netdev->ifname);
2,534✔
211

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

216
void netdev_detach(NetDev *netdev) {
844✔
217
        assert(netdev);
844✔
218

219
        netdev_unref(netdev_detach_impl(netdev));
844✔
220
}
844✔
221

222
static NetDev* netdev_free(NetDev *netdev) {
1,690✔
223
        assert(netdev);
1,690✔
224

225
        netdev_detach_impl(netdev);
1,690✔
226

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

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

246
        return mfree(netdev);
1,690✔
247
}
248

249
DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev, netdev_free);
34,094✔
250

251
void netdev_drop(NetDev *netdev) {
888✔
252
        if (!netdev)
888✔
253
                return;
254

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

261
                log_netdev_debug(netdev, "netdev removed");
106✔
262
                return;
106✔
263
        }
264

265
        if (NETDEV_VTABLE(netdev) && NETDEV_VTABLE(netdev)->drop)
379✔
266
                NETDEV_VTABLE(netdev)->drop(netdev);
4✔
267

268
        netdev->state = NETDEV_STATE_LINGER;
379✔
269

270
        log_netdev_debug(netdev, "netdev removed");
379✔
271

272
        netdev_detach(netdev);
379✔
273
}
274

275
static int netdev_attach_name_full(NetDev *netdev, const char *name, Hashmap **netdevs) {
1,343✔
276
        int r;
1,343✔
277

278
        assert(netdev);
1,343✔
279
        assert(name);
1,343✔
280

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

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

293
                return -EEXIST;
1✔
294
        }
295
        assert(r > 0);
1,342✔
296

297
        return 0;
298
}
299

300
int netdev_attach_name(NetDev *netdev, const char *name) {
1,025✔
301
        assert(netdev);
1,025✔
302
        assert(netdev->manager);
1,025✔
303

304
        return netdev_attach_name_full(netdev, name, &netdev->manager->netdevs);
1,025✔
305
}
306

307
static int netdev_attach(NetDev *netdev) {
845✔
308
        int r;
845✔
309

310
        assert(netdev);
845✔
311
        assert(netdev->ifname);
845✔
312

313
        r = netdev_attach_name(netdev, netdev->ifname);
845✔
314
        if (r < 0)
845✔
315
                return r;
316

317
        if (NETDEV_VTABLE(netdev)->attach) {
844✔
318
                r = NETDEV_VTABLE(netdev)->attach(netdev);
176✔
319
                if (r < 0)
176✔
320
                        return r;
×
321
        }
322

323
        return 0;
324
}
325

326
int netdev_get(Manager *manager, const char *name, NetDev **ret) {
13,859✔
327
        NetDev *netdev;
13,859✔
328

329
        assert(manager);
13,859✔
330
        assert(name);
13,859✔
331
        assert(ret);
13,859✔
332

333
        netdev = hashmap_get(manager->netdevs, name);
13,859✔
334
        if (!netdev)
13,859✔
335
                return -ENOENT;
336

337
        *ret = netdev;
6,077✔
338

339
        return 0;
6,077✔
340
}
341

342
void link_assign_netdev(Link *link) {
6,741✔
343
        _unused_ _cleanup_(netdev_unrefp) NetDev *old = NULL;
6,741✔
344
        NetDev *netdev;
6,741✔
345

346
        assert(link);
6,741✔
347
        assert(link->manager);
6,741✔
348
        assert(link->ifname);
6,741✔
349

350
        old = TAKE_PTR(link->netdev);
6,741✔
351

352
        if (netdev_get(link->manager, link->ifname, &netdev) < 0)
6,741✔
353
                goto not_found;
4,537✔
354

355
        int ifindex = NETDEV_VTABLE(netdev)->get_ifindex ?
3,607✔
356
                NETDEV_VTABLE(netdev)->get_ifindex(netdev, link->ifname) :
2,204✔
357
                netdev->ifindex;
358
        if (ifindex != link->ifindex)
2,204✔
359
                goto not_found;
22✔
360

361
        if (NETDEV_VTABLE(netdev)->iftype != link->iftype)
2,182✔
362
                goto not_found;
×
363

364
        if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
2,182✔
365
                const char *kind;
2,170✔
366

367
                if (netdev->kind == NETDEV_KIND_TAP)
2,170✔
368
                        kind = "tun"; /* the kernel does not distinguish between tun and tap */
369
                else
370
                        kind = netdev_kind_to_string(netdev->kind);
2,157✔
371

372
                if (!streq_ptr(kind, link->kind))
2,170✔
373
                        goto not_found;
×
374
        }
375

376
        link->netdev = netdev_ref(netdev);
2,182✔
377

378
        if (netdev == old)
2,182✔
379
                return; /* The same NetDev found. */
380

381
        log_link_debug(link, "Found matching .netdev file: %s", netdev->filename);
759✔
382
        link_dirty(link);
759✔
383
        return;
384

385
not_found:
4,559✔
386

387
        if (old)
4,559✔
388
                /* Previously assigned NetDev is detached from Manager? Update the state file. */
389
                link_dirty(link);
9✔
390
}
391

392
void netdev_enter_failed(NetDev *netdev) {
22✔
393
        netdev->state = NETDEV_STATE_FAILED;
22✔
394
}
22✔
395

396
int netdev_enter_ready(NetDev *netdev) {
765✔
397
        assert(netdev);
765✔
398
        assert(netdev->ifname);
765✔
399

400
        if (!IN_SET(netdev->state, NETDEV_STATE_LOADING, NETDEV_STATE_CREATING))
765✔
401
                return 0;
402

403
        netdev->state = NETDEV_STATE_READY;
658✔
404

405
        log_netdev_info(netdev, "netdev ready");
658✔
406

407
        if (NETDEV_VTABLE(netdev)->post_create)
658✔
408
                NETDEV_VTABLE(netdev)->post_create(netdev, NULL);
25✔
409

410
        return 0;
411
}
412

413
bool netdev_needs_reconfigure(NetDev *netdev, NetDevLocalAddressType type) {
511✔
414
        assert(netdev);
511✔
415
        assert(type < _NETDEV_LOCAL_ADDRESS_TYPE_MAX);
511✔
416

417
        if (type < 0)
511✔
418
                return true;
419

420
        return NETDEV_VTABLE(netdev)->needs_reconfigure &&
774✔
421
                NETDEV_VTABLE(netdev)->needs_reconfigure(netdev, type);
239✔
422
}
423

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

428
        assert(netdev);
437✔
429
        assert(netdev->state != _NETDEV_STATE_INVALID);
437✔
430

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

442
        return netdev_enter_ready(netdev);
424✔
443
}
444

445
int netdev_set_ifindex_internal(NetDev *netdev, int ifindex) {
2,192✔
446
        assert(netdev);
2,192✔
447
        assert(ifindex > 0);
2,192✔
448

449
        if (netdev->ifindex == ifindex)
2,192✔
450
                return 0; /* Already set. */
451

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

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

462
static int netdev_set_ifindex_impl(NetDev *netdev, const char *name, int ifindex) {
2,692✔
463
        assert(netdev);
2,692✔
464
        assert(name);
2,692✔
465
        assert(ifindex > 0);
2,692✔
466

467
        if (NETDEV_VTABLE(netdev)->set_ifindex)
2,692✔
468
                return NETDEV_VTABLE(netdev)->set_ifindex(netdev, name, ifindex);
1,009✔
469

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

475
        return netdev_set_ifindex_internal(netdev, ifindex);
1,683✔
476
}
477

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

485
        assert(netdev);
2,842✔
486
        assert(message);
2,842✔
487

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

492
        if (type != RTM_NEWLINK)
2,842✔
493
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Cannot set ifindex from unexpected rtnl message type.");
×
494

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

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

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

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

512
        if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
2,692✔
513

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

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

522
                r = sd_netlink_message_exit_container(message);
2,678✔
523
                if (r < 0)
2,678✔
524
                        return log_netdev_warning_errno(netdev, r, "Could not exit container: %m");
×
525

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

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

540
        return netdev_set_ifindex_impl(netdev, received_name, ifindex);
2,692✔
541
}
542

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

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

552
        struct hw_addr_data a = HW_ADDR_NULL;
722✔
553
        bool is_static = false;
722✔
554
        int r;
722✔
555

556
        assert(netdev);
722✔
557
        assert(name);
722✔
558
        assert(hw_addr);
722✔
559
        assert(ret);
722✔
560

561
        if (hw_addr_equal(hw_addr, &HW_ADDR_NONE)) {
722✔
562
                *ret = HW_ADDR_NULL;
1✔
563
                return 0;
1✔
564
        }
565

566
        if (hw_addr->length == 0) {
721✔
567
                uint64_t result;
530✔
568

569
                /* HardwareAddress= is not specified. */
570

571
                if (!NETDEV_VTABLE(netdev)->generate_mac)
530✔
572
                        goto finalize;
176✔
573

574
                if (!IN_SET(NETDEV_VTABLE(netdev)->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND))
354✔
575
                        goto finalize;
×
576

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

584
                a.length = arphrd_to_hw_addr_len(NETDEV_VTABLE(netdev)->iftype);
354✔
585

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

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

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

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

612
        } else {
613
                a = *hw_addr;
191✔
614
                is_static = true;
191✔
615
        }
616

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

622
finalize:
545✔
623
        *ret = a;
721✔
624
        return 0;
721✔
625
}
626

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

632
        if (hw_addr->length <= 0)
628✔
633
                return false;
628✔
634

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

639
        if (hw_addr_equal(&link->hw_addr, hw_addr))
59✔
640
                return false; /* Unchanged, not necessary to set. */
641

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

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

657
static bool netdev_can_set_mtu(NetDev *netdev, uint32_t mtu) {
628✔
658
        assert(netdev);
628✔
659

660
        if (mtu <= 0)
628✔
661
                return false;
628✔
662

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

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

670
        if (link->mtu == mtu)
17✔
671
                return false; /* Unchanged, not necessary to set. */
672

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

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

681
static int netdev_create_message(NetDev *netdev, Link *link, sd_netlink_message *m) {
628✔
682
        int r;
628✔
683

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

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

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

705
        if (netdev_can_set_mtu(netdev, netdev->mtu)) {
628✔
706
                r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu);
37✔
707
                if (r < 0)
37✔
708
                        return r;
709
        }
710

711
        if (link) {
628✔
712
                r = sd_netlink_message_append_u32(m, IFLA_LINK, link->ifindex);
191✔
713
                if (r < 0)
191✔
714
                        return r;
715
        }
716

717
        r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
628✔
718
        if (r < 0)
628✔
719
                return r;
720

721
        if (NETDEV_VTABLE(netdev)->fill_message_create) {
628✔
722
                r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind));
330✔
723
                if (r < 0)
330✔
724
                        return r;
725

726
                r = NETDEV_VTABLE(netdev)->fill_message_create(netdev, link, m);
330✔
727
                if (r < 0)
330✔
728
                        return r;
729

730
                r = sd_netlink_message_close_container(m);
330✔
731
                if (r < 0)
330✔
732
                        return r;
733
        } else {
734
                r = sd_netlink_message_append_string(m, IFLA_INFO_KIND, netdev_kind_to_string(netdev->kind));
298✔
735
                if (r < 0)
298✔
736
                        return r;
737
        }
738

739
        r = sd_netlink_message_close_container(m);
628✔
740
        if (r < 0)
628✔
741
                return r;
×
742

743
        return 0;
744
}
745

746
static int independent_netdev_create(NetDev *netdev) {
455✔
747
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
455✔
748
        int r;
455✔
749

750
        assert(netdev);
455✔
751
        assert(netdev->manager);
455✔
752

753
        /* create netdev */
754
        if (NETDEV_VTABLE(netdev)->create) {
455✔
755
                r = NETDEV_VTABLE(netdev)->create(netdev);
18✔
756
                if (r < 0)
18✔
757
                        return r;
758

759
                log_netdev_debug(netdev, "Created");
18✔
760
                return 0;
18✔
761
        }
762

763
        r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, netdev->ifindex);
437✔
764
        if (r < 0)
437✔
765
                return r;
766

767
        r = netdev_create_message(netdev, NULL, m);
437✔
768
        if (r < 0)
437✔
769
                return r;
770

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

776
        netdev_ref(netdev);
437✔
777

778
        netdev->state = NETDEV_STATE_CREATING;
437✔
779
        log_netdev_debug(netdev, "Creating");
437✔
780
        return 0;
781
}
782

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

787
        assert(netdev);
191✔
788
        assert(netdev->manager);
191✔
789
        assert(link);
191✔
790
        assert(req);
191✔
791

792
        r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, netdev->ifindex);
191✔
793
        if (r < 0)
191✔
794
                return r;
795

796
        r = netdev_create_message(netdev, link, m);
191✔
797
        if (r < 0)
191✔
798
                return r;
799

800
        r = request_call_netlink_async(netdev->manager->rtnl, m, req);
191✔
801
        if (r < 0)
191✔
802
                return r;
803

804
        netdev->state = NETDEV_STATE_CREATING;
191✔
805
        log_netdev_debug(netdev, "Creating");
191✔
806
        return 0;
807
}
808

809
static bool link_is_ready_to_create_stacked_netdev_one(Link *link, bool allow_unmanaged) {
2,819✔
810
        assert(link);
2,819✔
811

812
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
2,819✔
813
                return false;
814

815
        if (!link->network)
2,819✔
816
                return allow_unmanaged;
817

818
        if (link->set_link_messages > 0)
2,819✔
819
                return false;
820

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

826
        return true;
827
}
828

829
static bool link_is_ready_to_create_stacked_netdev(Link *link) {
2,819✔
830
        return check_ready_for_all_sr_iov_ports(link, /* allow_unmanaged= */ false,
2,819✔
831
                                                link_is_ready_to_create_stacked_netdev_one);
832
}
833

834
static int netdev_is_ready_to_create(NetDev *netdev, Link *link) {
3,377✔
835
        assert(netdev);
3,377✔
836

837
        if (link && !link_is_ready_to_create_stacked_netdev(link))
3,377✔
838
                return false;
839

840
        if (NETDEV_VTABLE(netdev)->is_ready_to_create)
1,538✔
841
                return NETDEV_VTABLE(netdev)->is_ready_to_create(netdev, link);
1,078✔
842

843
        return true;
844
}
845

846
static int stacked_netdev_process_request(Request *req, Link *link, void *userdata) {
2,833✔
847
        NetDev *netdev = ASSERT_PTR(userdata);
2,833✔
848
        int r;
2,833✔
849

850
        assert(req);
2,833✔
851
        assert(link);
2,833✔
852

853
        if (!netdev_is_managed(netdev))
2,833✔
854
                goto cancelled; /* Already detached, due to e.g. reloading .netdev files, cancelling the request. */
×
855

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

862
                goto cancelled;
14✔
863
        }
864

865
        r = netdev_is_ready_to_create(netdev, link);
2,819✔
866
        if (r <= 0)
2,819✔
867
                return r;
868

869
        r = stacked_netdev_create(netdev, link, req);
191✔
870
        if (r < 0)
191✔
871
                return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
×
872

873
        return 1;
874

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

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

885
        return 1;
886
}
887

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

892
        assert(m);
191✔
893
        assert(link);
191✔
894

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

907
        (void) netdev_enter_ready(netdev);
191✔
908

909
        if (link->create_stacked_netdev_messages == 0) {
191✔
910
                link->stacked_netdevs_created = true;
97✔
911
                log_link_debug(link, "Stacked netdevs created.");
97✔
912
                link_check_ready(link);
97✔
913
        }
914

915
        return 0;
916
}
917

918
int link_request_stacked_netdev(Link *link, NetDev *netdev) {
215✔
919
        int r;
215✔
920

921
        assert(link);
215✔
922
        assert(netdev);
215✔
923

924
        if (!netdev_is_stacked(netdev))
215✔
925
                return -EINVAL;
926

927
        if (!netdev_is_managed(netdev))
215✔
928
                return 0; /* Already detached, due to e.g. reloading .netdev files. */
929

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

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

948
static int independent_netdev_process_request(Request *req, Link *link, void *userdata) {
575✔
949
        NetDev *netdev = ASSERT_PTR(userdata);
575✔
950
        int r;
575✔
951

952
        assert(!link);
575✔
953

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

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

963
                return 1; /* Skip this request. */
17✔
964
        }
965

966
        r = netdev_is_ready_to_create(netdev, NULL);
558✔
967
        if (r <= 0)
558✔
968
                return r;
969

970
        r = independent_netdev_create(netdev);
455✔
971
        if (r < 0)
455✔
972
                return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
×
973

974
        return 1;
975
}
976

977
static int netdev_request_to_create(NetDev *netdev) {
844✔
978
        int r;
844✔
979

980
        assert(netdev);
844✔
981
        assert(netdev->manager);
844✔
982

983
        if (netdev->manager->test_mode)
844✔
984
                return 0;
985

986
        if (netdev_is_stacked(netdev))
844✔
987
                return 0;
988

989
        if (!netdev_is_managed(netdev))
648✔
990
                return 0; /* Already detached, due to e.g. reloading .netdev files. */
991

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

995
        r = netdev_queue_request(netdev, independent_netdev_process_request, NULL);
472✔
996
        if (r < 0)
472✔
997
                return log_netdev_warning_errno(netdev, r, "Failed to request to create netdev: %m");
×
998

999
        return 0;
1000
}
1001

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

1008
        assert(manager);
845✔
1009
        assert(filename);
845✔
1010
        assert(ret);
845✔
1011

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

1018
        netdev_raw = new(NetDev, 1);
845✔
1019
        if (!netdev_raw)
845✔
1020
                return log_oom();
×
1021

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

1028
        r = path_extract_filename(filename, &file_basename);
845✔
1029
        if (r < 0)
845✔
1030
                return log_warning_errno(r, "Failed to extract file name of '%s': %m", filename);
×
1031

1032
        dropin_dirname = strjoina(file_basename, ".d");
4,225✔
1033
        r = config_parse_many(
1,690✔
1034
                        STRV_MAKE_CONST(filename),
845✔
1035
                        NETWORK_DIRS,
845✔
1036
                        dropin_dirname,
1037
                        NETDEV_COMMON_SECTIONS NETDEV_OTHER_SECTIONS,
1038
                        config_item_perf_lookup,
1039
                        network_netdev_gperf_lookup,
1040
                        CONFIG_PARSE_WARN,
1041
                        netdev_raw);
1042
        if (r < 0)
845✔
1043
                return r; /* config_parse_many() logs internally. */
1044

1045
        /* skip out early if configuration does not match the environment */
1046
        if (!condition_test_list(netdev_raw->conditions, environ, NULL, NULL, NULL))
845✔
1047
                return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "%s: Conditions in the file do not match the system environment, skipping.", filename);
×
1048

1049
        if (netdev_raw->kind == _NETDEV_KIND_INVALID)
845✔
1050
                return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "NetDev has no Kind= configured in \"%s\", ignoring.", filename);
×
1051

1052
        if (!netdev_raw->ifname)
845✔
1053
                return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "NetDev without Name= configured in \"%s\", ignoring.", filename);
×
1054

1055
        netdev = malloc0(NETDEV_VTABLE(netdev_raw)->object_size);
845✔
1056
        if (!netdev)
845✔
1057
                return log_oom();
×
1058

1059
        netdev->n_ref = 1;
845✔
1060
        netdev->manager = manager;
845✔
1061
        netdev->kind = netdev_raw->kind;
845✔
1062
        netdev->state = NETDEV_STATE_LOADING; /* we initialize the state here for the first time,
845✔
1063
                                                 so that done() will be called on destruction */
1064

1065
        if (NETDEV_VTABLE(netdev)->init)
845✔
1066
                NETDEV_VTABLE(netdev)->init(netdev);
288✔
1067

1068
        r = config_parse_many_full(
2,535✔
1069
                        STRV_MAKE_CONST(filename),
845✔
1070
                        NETWORK_DIRS,
845✔
1071
                        dropin_dirname,
1072
                        /* root= */ NULL,
1073
                        /* root_fd= */ -EBADF,
1074
                        NETDEV_VTABLE(netdev)->sections,
845✔
1075
                        config_item_perf_lookup,
1076
                        network_netdev_gperf_lookup,
1077
                        CONFIG_PARSE_WARN,
1078
                        netdev,
1079
                        &netdev->stats_by_path,
1080
                        &netdev->dropins);
845✔
1081
        if (r < 0)
845✔
1082
                return r; /* config_parse_many() logs internally. */
1083

1084
        /* verify configuration */
1085
        if (NETDEV_VTABLE(netdev)->config_verify) {
845✔
1086
                r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename);
396✔
1087
                if (r < 0)
396✔
1088
                        return r; /* config_verify() logs internally. */
1089
        }
1090

1091
        netdev->filename = strdup(filename);
845✔
1092
        if (!netdev->filename)
845✔
1093
                return log_oom();
×
1094

1095
        log_syntax(/* unit= */ NULL, LOG_DEBUG, filename, /* config_line= */ 0, /* error= */ 0, "Successfully loaded.");
845✔
1096

1097
        *ret = TAKE_PTR(netdev);
845✔
1098
        return 0;
845✔
1099
}
1100

1101
int netdev_load(Manager *manager) {
447✔
1102
        _cleanup_strv_free_ char **files = NULL;
447✔
1103
        int r;
447✔
1104

1105
        assert(manager);
447✔
1106

1107
        r = conf_files_list_strv(&files, ".netdev", /* root= */ NULL, CONF_FILES_WARN, NETWORK_DIRS);
447✔
1108
        if (r < 0)
447✔
1109
                return log_error_errno(r, "Failed to enumerate netdev files: %m");
×
1110

1111
        STRV_FOREACH(f, files) {
974✔
1112
                _cleanup_(netdev_unrefp) NetDev *netdev = NULL;
1✔
1113

1114
                if (netdev_load_one(manager, *f, &netdev) < 0)
527✔
1115
                        continue;
×
1116

1117
                if (netdev_attach(netdev) < 0)
527✔
1118
                        continue;
1✔
1119

1120
                if (netdev_request_to_create(netdev) < 0)
526✔
1121
                        continue;
×
1122

1123
                TAKE_PTR(netdev);
526✔
1124
        }
1125

1126
        return 0;
1127
}
1128

1129
int netdev_reload(Manager *manager) {
154✔
1130
        _cleanup_hashmap_free_ Hashmap *new_netdevs = NULL;
154✔
1131
        _cleanup_strv_free_ char **files = NULL;
154✔
1132
        int r;
154✔
1133

1134
        assert(manager);
154✔
1135

1136
        r = conf_files_list_strv(&files, ".netdev", /* root= */ NULL, CONF_FILES_WARN, NETWORK_DIRS);
154✔
1137
        if (r < 0)
154✔
1138
                return log_error_errno(r, "Failed to enumerate netdev files: %m");
×
1139

1140
        STRV_FOREACH(f, files) {
472✔
1141
                _cleanup_(netdev_unrefp) NetDev *netdev = NULL;
318✔
1142
                NetDev *old;
318✔
1143

1144
                if (netdev_load_one(manager, *f, &netdev) < 0)
318✔
1145
                        continue;
×
1146

1147
                if (netdev_get(manager, netdev->ifname, &old) < 0) {
318✔
1148
                        log_netdev_debug(netdev, "Found new .netdev file: %s", netdev->filename);
12✔
1149

1150
                        if (netdev_attach_name_full(netdev, netdev->ifname, &new_netdevs) >= 0)
12✔
1151
                                TAKE_PTR(netdev);
12✔
1152

1153
                        continue;
12✔
1154
                }
1155

1156
                if (!stats_by_path_equal(netdev->stats_by_path, old->stats_by_path)) {
306✔
1157
                        log_netdev_debug(netdev, "Found updated .netdev file: %s", netdev->filename);
125✔
1158

1159
                        /* Copy ifindex. */
1160
                        netdev->ifindex = old->ifindex;
125✔
1161

1162
                        if (netdev_attach_name_full(netdev, netdev->ifname, &new_netdevs) >= 0)
125✔
1163
                                TAKE_PTR(netdev);
125✔
1164

1165
                        continue;
125✔
1166
                }
1167

1168
                /* Keep the original object, and drop the new one. */
1169
                if (netdev_attach_name_full(old, old->ifname, &new_netdevs) >= 0)
181✔
1170
                        netdev_ref(old);
181✔
1171
        }
1172

1173
        /* Detach old NetDev objects from Manager.
1174
         * The same object may be registered with multiple names, and netdev_detach() may drop multiple entries. */
1175
        for (NetDev *n; (n = hashmap_first(manager->netdevs)); )
462✔
1176
                netdev_detach(n);
308✔
1177

1178
        /* Attach new NetDev objects to Manager. */
1179
        for (;;) {
472✔
1180
                _cleanup_(netdev_unrefp) NetDev *netdev = hashmap_steal_first(new_netdevs);
626✔
1181
                if (!netdev)
472✔
1182
                        break;
1183

1184
                netdev->manager = manager;
318✔
1185
                if (netdev_attach(netdev) < 0)
318✔
1186
                        continue;
×
1187

1188
                /* Create a new netdev or update existing netdev, */
1189
                if (netdev_request_to_create(netdev) < 0)
318✔
1190
                        continue;
×
1191

1192
                TAKE_PTR(netdev);
318✔
1193
        }
1194

1195
        /* Reassign NetDev objects to Link object. */
1196
        Link *link;
154✔
1197
        HASHMAP_FOREACH(link, manager->links_by_index)
1,376✔
1198
                link_assign_netdev(link);
1,222✔
1199

1200
        return 0;
154✔
1201
}
1202

1203
int config_parse_netdev_kind(
1,690✔
1204
                const char *unit,
1205
                const char *filename,
1206
                unsigned line,
1207
                const char *section,
1208
                unsigned section_line,
1209
                const char *lvalue,
1210
                int ltype,
1211
                const char *rvalue,
1212
                void *data,
1213
                void *userdata) {
1214

1215
        NetDevKind k, *kind = ASSERT_PTR(data);
1,690✔
1216

1217
        assert(filename);
1,690✔
1218
        assert(rvalue);
1,690✔
1219

1220
        k = netdev_kind_from_string(rvalue);
1,690✔
1221
        if (k < 0) {
1,690✔
1222
                log_syntax(unit, LOG_WARNING, filename, line, k, "Failed to parse netdev kind, ignoring assignment: %s", rvalue);
×
1223
                return 0;
×
1224
        }
1225

1226
        if (*kind != _NETDEV_KIND_INVALID && *kind != k) {
1,690✔
1227
                log_syntax(unit, LOG_WARNING, filename, line, 0,
×
1228
                           "Specified netdev kind is different from the previous value '%s', ignoring assignment: %s",
1229
                           netdev_kind_to_string(*kind), rvalue);
1230
                return 0;
×
1231
        }
1232

1233
        *kind = k;
1,690✔
1234

1235
        return 0;
1,690✔
1236
}
1237

1238
int config_parse_netdev_hw_addr(
525✔
1239
                const char *unit,
1240
                const char *filename,
1241
                unsigned line,
1242
                const char *section,
1243
                unsigned section_line,
1244
                const char *lvalue,
1245
                int ltype,
1246
                const char *rvalue,
1247
                void *data,
1248
                void *userdata) {
1249

1250
        struct hw_addr_data *hw_addr = ASSERT_PTR(data);
525✔
1251

1252
        assert(rvalue);
525✔
1253

1254
        if (streq(rvalue, "none")) {
525✔
1255
                *hw_addr = HW_ADDR_NONE;
2✔
1256
                return 0;
2✔
1257
        }
1258

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