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

systemd / systemd / 15512929258

06 Jun 2025 10:28PM UTC coverage: 72.058% (-0.03%) from 72.092%
15512929258

push

github

poettering
nspawn: do basic port to PidRef

THis is sometimes a bit superficial, but in many cases allows us to use
pidfd for various of our operations.

75 of 86 new or added lines in 4 files covered. (87.21%)

3361 existing lines in 68 files now uncovered.

300145 of 416533 relevant lines covered (72.06%)

703207.48 hits per line

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

88.42
/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);
6,551✔
137

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

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

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

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

151
        switch (netdev->kind) {
374✔
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:
26✔
163
                return IP6TNL(netdev)->independent;
26✔
164
        case NETDEV_KIND_IPIP:
27✔
165
                return IPIP(netdev)->independent;
27✔
166
        case NETDEV_KIND_SIT:
37✔
167
                return SIT(netdev)->independent;
37✔
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,295✔
182
        assert(netdev);
1,295✔
183

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

187
        if (netdev_is_stacked_and_independent(netdev))
374✔
188
                return false;
26✔
189

190
        return true;
191
}
192

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

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

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

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

205
        if (netdev->state != _NETDEV_STATE_INVALID &&
2,174✔
206
            NETDEV_VTABLE(netdev) &&
1,449✔
207
            NETDEV_VTABLE(netdev)->detach)
1,449✔
208
                NETDEV_VTABLE(netdev)->detach(netdev);
312✔
209

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

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

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

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

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

225
        netdev_detach_impl(netdev);
1,450✔
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,450✔
235
            NETDEV_VTABLE(netdev) &&
725✔
236
            NETDEV_VTABLE(netdev)->done)
725✔
237
                NETDEV_VTABLE(netdev)->done(netdev);
218✔
238

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

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

249
DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev, netdev_free);
29,334✔
250

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

255
        if (netdev_is_stacked(netdev)) {
429✔
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;
74✔
259
                netdev->ifindex = 0;
74✔
260

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

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

268
        netdev->state = NETDEV_STATE_LINGER;
355✔
269

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

272
        netdev_detach(netdev);
355✔
273
}
274

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

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

281
        r = hashmap_ensure_put(netdevs, &string_hash_ops, name, netdev);
1,145✔
282
        if (r == -ENOMEM)
1,145✔
UNCOV
283
                return log_oom();
×
284
        if (r == -EEXIST) {
1,145✔
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,144✔
296

297
        return 0;
298
}
299

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

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

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

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

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

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

323
        return 0;
324
}
325

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

329
        assert(manager);
12,915✔
330
        assert(name);
12,915✔
331
        assert(ret);
12,915✔
332

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

337
        *ret = netdev;
5,433✔
338

339
        return 0;
5,433✔
340
}
341

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

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

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

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

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

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

364
        if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
1,973✔
365
                const char *kind;
1,961✔
366

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

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

376
        link->netdev = netdev_ref(netdev);
1,973✔
377

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

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

385
not_found:
4,400✔
386

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

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

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

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

403
        netdev->state = NETDEV_STATE_READY;
560✔
404

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

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

410
        return 0;
411
}
412

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

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

420
        return NETDEV_VTABLE(netdev)->needs_reconfigure &&
486✔
421
                NETDEV_VTABLE(netdev)->needs_reconfigure(netdev, type);
140✔
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) {
404✔
426
        int r;
404✔
427

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

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

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

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

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

452
        if (netdev->ifindex > 0 && netdev->ifindex != ifindex)
476✔
UNCOV
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;
476✔
458
        log_netdev_debug(netdev, "Gained index %i.", ifindex);
476✔
459
        return 1; /* set new ifindex. */
460
}
461

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

467
        if (NETDEV_VTABLE(netdev)->set_ifindex)
2,431✔
468
                return NETDEV_VTABLE(netdev)->set_ifindex(netdev, name, ifindex);
936✔
469

470
        if (!streq(netdev->ifname, name))
1,495✔
UNCOV
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,495✔
476
}
477

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

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

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

492
        if (type != RTM_NEWLINK)
2,585✔
UNCOV
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,585✔
496
        if (r < 0)
2,585✔
UNCOV
497
                return log_netdev_warning_errno(netdev, r, "Failed to get family from received rtnl message: %m");
×
498

499
        if (family != AF_UNSPEC)
2,585✔
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,431✔
503
        if (r < 0)
2,431✔
UNCOV
504
                return log_netdev_warning_errno(netdev, r, "Could not get ifindex: %m");
×
505
        if (ifindex <= 0)
2,431✔
UNCOV
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,431✔
509
        if (r < 0)
2,431✔
UNCOV
510
                return log_netdev_warning_errno(netdev, r, "Could not get IFNAME: %m");
×
511

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

514
                r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2,417✔
515
                if (r < 0)
2,417✔
UNCOV
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,417✔
519
                if (r < 0)
2,417✔
UNCOV
520
                        return log_netdev_warning_errno(netdev, r, "Could not get KIND: %m");
×
521

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

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

534
                if (!streq(kind, received_kind))
2,417✔
UNCOV
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,431✔
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(
613✔
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;
613✔
553
        bool is_static = false;
613✔
554
        int r;
613✔
555

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

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

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

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

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

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

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

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

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

591
                        if (ether_addr_is_null(&a.ether) || ether_addr_is_broadcast(&a.ether)) {
336✔
592
                                log_netdev_warning(netdev, "Failed to generate persistent MAC address, ignoring.");
×
593
                                a = HW_ADDR_NULL;
×
UNCOV
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.");
×
UNCOV
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:
×
UNCOV
609
                        assert_not_reached();
×
610
                }
611

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

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

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

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

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

635
        Link *link;
424✔
636
        if (link_get_by_index(netdev->manager, netdev->ifindex, &link) < 0)
424✔
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))
54✔
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)
5✔
646
                return NETDEV_VTABLE(netdev)->can_set_mac(netdev, hw_addr);
3✔
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✔
UNCOV
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) {
528✔
658
        assert(netdev);
528✔
659

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

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

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

670
        if (link->mtu == mtu)
9✔
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)
×
UNCOV
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) {
528✔
682
        int r;
528✔
683

684
        if (netdev->ifindex <= 0) {
528✔
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);
429✔
689
                if (r < 0)
429✔
690
                        return r;
528✔
691
        }
692

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

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

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

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

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

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

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

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

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

743
        return 0;
744
}
745

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

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

753
        /* create netdev */
754
        if (NETDEV_VTABLE(netdev)->create) {
422✔
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);
404✔
764
        if (r < 0)
404✔
765
                return r;
766

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

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

776
        netdev_ref(netdev);
404✔
777

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

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

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

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

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

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

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

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

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

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

818
        if (link->set_link_messages > 0)
1,517✔
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)
1,433✔
824
                return false;
1,018✔
825

826
        return true;
827
}
828

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

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

837
        if (link && !link_is_ready_to_create_stacked_netdev(link))
2,032✔
838
                return false;
839

840
        if (NETDEV_VTABLE(netdev)->is_ready_to_create)
930✔
841
                return NETDEV_VTABLE(netdev)->is_ready_to_create(netdev, link);
497✔
842

843
        return true;
844
}
845

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

850
        assert(req);
1,531✔
851
        assert(link);
1,531✔
852

853
        if (!netdev_is_managed(netdev))
1,531✔
UNCOV
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) {
1,531✔
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);
1,517✔
866
        if (r <= 0)
1,517✔
867
                return r;
868

869
        r = stacked_netdev_create(netdev, link, req);
124✔
870
        if (r < 0)
124✔
UNCOV
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) {
124✔
889
        NetDev *netdev = ASSERT_PTR(userdata);
124✔
890
        int r;
124✔
891

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

895
        r = sd_netlink_message_get_errno(m);
124✔
896
        if (r >= 0)
124✔
897
                log_netdev_debug(netdev, "Created.");
124✔
898
        else if (r == -EEXIST && netdev->ifindex > 0)
×
UNCOV
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);
×
UNCOV
904
                return 0;
×
905
        }
906

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

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

915
        return 0;
916
}
917

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

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

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

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

930
        link->stacked_netdevs_created = false;
142✔
931
        r = link_queue_request_full(link, REQUEST_TYPE_NETDEV_STACKED,
142✔
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)
142✔
UNCOV
938
                return log_link_error_errno(link, r, "Failed to request stacked netdev '%s': %m",
×
939
                                            netdev->ifname);
940
        if (r == 0)
142✔
941
                return 0;
942

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

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

952
        assert(!link);
531✔
953

954
        if (!netdev_is_managed(netdev))
531✔
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) {
531✔
958
                /* Already exists, and the netdev does not support updating, entering the ready state. */
959
                r = netdev_enter_ready(netdev);
16✔
960
                if (r < 0)
16✔
961
                        return r;
962

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

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

970
        r = independent_netdev_create(netdev);
422✔
971
        if (r < 0)
422✔
UNCOV
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) {
724✔
978
        int r;
724✔
979

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

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

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

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

992
        if (netdev->state != NETDEV_STATE_LOADING)
592✔
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);
438✔
996
        if (r < 0)
438✔
UNCOV
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) {
725✔
1003
        _cleanup_(netdev_unrefp) NetDev *netdev_raw = NULL, *netdev = NULL;
725✔
1004
        _cleanup_free_ char *file_basename = NULL;
725✔
1005
        const char *dropin_dirname;
725✔
1006
        int r;
725✔
1007

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

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

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

1022
        *netdev_raw = (NetDev) {
725✔
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);
725✔
1029
        if (r < 0)
725✔
UNCOV
1030
                return log_warning_errno(r, "Failed to extract file name of '%s': %m", filename);
×
1031

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

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

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

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

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

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

1064
        if (NETDEV_VTABLE(netdev)->init)
725✔
1065
                NETDEV_VTABLE(netdev)->init(netdev);
217✔
1066

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

1078
        /* verify configuration */
1079
        if (NETDEV_VTABLE(netdev)->config_verify) {
725✔
1080
                r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename);
306✔
1081
                if (r < 0)
306✔
1082
                        return r; /* config_verify() logs internally. */
1083
        }
1084

1085
        netdev->filename = strdup(filename);
725✔
1086
        if (!netdev->filename)
725✔
UNCOV
1087
                return log_oom();
×
1088

1089
        log_syntax(/* unit = */ NULL, LOG_DEBUG, filename, /* config_line = */ 0, /* error = */ 0, "Successfully loaded.");
725✔
1090

1091
        *ret = TAKE_PTR(netdev);
725✔
1092
        return 0;
725✔
1093
}
1094

1095
int netdev_load(Manager *manager) {
420✔
1096
        _cleanup_strv_free_ char **files = NULL;
420✔
1097
        int r;
420✔
1098

1099
        assert(manager);
420✔
1100

1101
        r = conf_files_list_strv(&files, ".netdev", NULL, 0, NETWORK_DIRS);
420✔
1102
        if (r < 0)
420✔
1103
                return log_error_errno(r, "Failed to enumerate netdev files: %m");
×
1104

1105
        STRV_FOREACH(f, files) {
885✔
1106
                _cleanup_(netdev_unrefp) NetDev *netdev = NULL;
1✔
1107

1108
                if (netdev_load_one(manager, *f, &netdev) < 0)
465✔
1109
                        continue;
×
1110

1111
                if (netdev_attach(netdev) < 0)
465✔
1112
                        continue;
1✔
1113

1114
                if (netdev_request_to_create(netdev) < 0)
464✔
UNCOV
1115
                        continue;
×
1116

1117
                TAKE_PTR(netdev);
464✔
1118
        }
1119

1120
        return 0;
1121
}
1122

1123
int netdev_reload(Manager *manager) {
136✔
1124
        _cleanup_hashmap_free_ Hashmap *new_netdevs = NULL;
136✔
1125
        _cleanup_strv_free_ char **files = NULL;
136✔
1126
        int r;
136✔
1127

1128
        assert(manager);
136✔
1129

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

1134
        STRV_FOREACH(f, files) {
396✔
1135
                _cleanup_(netdev_unrefp) NetDev *netdev = NULL;
260✔
1136
                NetDev *old;
260✔
1137

1138
                if (netdev_load_one(manager, *f, &netdev) < 0)
260✔
UNCOV
1139
                        continue;
×
1140

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

1144
                        if (netdev_attach_name_full(netdev, netdev->ifname, &new_netdevs) >= 0)
11✔
1145
                                TAKE_PTR(netdev);
11✔
1146

1147
                        continue;
11✔
1148
                }
1149

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

1153
                        /* Copy ifindex. */
1154
                        netdev->ifindex = old->ifindex;
90✔
1155

1156
                        if (netdev_attach_name_full(netdev, netdev->ifname, &new_netdevs) >= 0)
90✔
1157
                                TAKE_PTR(netdev);
90✔
1158

1159
                        continue;
90✔
1160
                }
1161

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

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

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

1178
                netdev->manager = manager;
260✔
1179
                if (netdev_attach(netdev) < 0)
260✔
UNCOV
1180
                        continue;
×
1181

1182
                /* Create a new netdev or update existing netdev, */
1183
                if (netdev_request_to_create(netdev) < 0)
260✔
UNCOV
1184
                        continue;
×
1185

1186
                TAKE_PTR(netdev);
260✔
1187
        }
1188

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

1194
        return 0;
136✔
1195
}
1196

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

1209
        NetDevKind k, *kind = ASSERT_PTR(data);
1,450✔
1210

1211
        assert(filename);
1,450✔
1212
        assert(rvalue);
1,450✔
1213

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

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

1227
        *kind = k;
1,450✔
1228

1229
        return 0;
1,450✔
1230
}
1231

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

1244
        struct hw_addr_data *hw_addr = ASSERT_PTR(data);
465✔
1245

1246
        assert(rvalue);
465✔
1247

1248
        if (streq(rvalue, "none")) {
465✔
1249
                *hw_addr = HW_ADDR_NONE;
2✔
1250
                return 0;
2✔
1251
        }
1252

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