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

systemd / systemd / 14345988482

08 Apr 2025 09:45PM UTC coverage: 71.913% (+0.006%) from 71.907%
14345988482

push

github

yuwata
test: Improve coverage in test-memfd-util and use ASSERT_OK() macro and friends

14 of 16 new or added lines in 1 file covered. (87.5%)

4696 existing lines in 76 files now uncovered.

296867 of 412812 relevant lines covered (71.91%)

666289.39 hits per line

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

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

3
/* Make sure the net/if.h header is included before any linux/ one */
4
#include <net/if.h>
5
#include <netinet/in.h>
6
#include <linux/if_arp.h>
7
#include <unistd.h>
8

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

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

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

142
DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
6,554✔
143

144
bool netdev_is_managed(NetDev *netdev) {
3,023✔
145
        if (!netdev || !netdev->manager || !netdev->ifname)
3,023✔
146
                return false;
147

148
        return hashmap_get(netdev->manager->netdevs, netdev->ifname) == netdev;
3,023✔
149
}
150

151
static bool netdev_is_stacked_and_independent(NetDev *netdev) {
375✔
152
        assert(netdev);
375✔
153

154
        if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
375✔
155
                return false;
156

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

187
static bool netdev_is_stacked(NetDev *netdev) {
1,293✔
188
        assert(netdev);
1,293✔
189

190
        if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
1,293✔
191
                return false;
192

193
        if (netdev_is_stacked_and_independent(netdev))
375✔
194
                return false;
26✔
195

196
        return true;
197
}
198

199
NetDev* netdev_detach_name(NetDev *netdev, const char *name) {
2,488✔
200
        assert(netdev);
2,488✔
201

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

205
        return hashmap_remove_value(netdev->manager->netdevs, name, netdev);
1,097✔
206
}
207

208
static NetDev* netdev_detach_impl(NetDev *netdev) {
2,168✔
209
        assert(netdev);
2,168✔
210

211
        if (netdev->state != _NETDEV_STATE_INVALID &&
2,168✔
212
            NETDEV_VTABLE(netdev) &&
1,445✔
213
            NETDEV_VTABLE(netdev)->detach)
1,445✔
214
                NETDEV_VTABLE(netdev)->detach(netdev);
312✔
215

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

218
        netdev->manager = NULL;
2,168✔
219
        return n; /* Return NULL when it is not attached yet, or already detached. */
2,168✔
220
}
221

222
void netdev_detach(NetDev *netdev) {
722✔
223
        assert(netdev);
722✔
224

225
        netdev_unref(netdev_detach_impl(netdev));
722✔
226
}
722✔
227

228
static NetDev* netdev_free(NetDev *netdev) {
1,446✔
229
        assert(netdev);
1,446✔
230

231
        netdev_detach_impl(netdev);
1,446✔
232

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

245
        condition_free_list(netdev->conditions);
1,446✔
246
        free(netdev->filename);
1,446✔
247
        strv_free(netdev->dropins);
1,446✔
248
        hashmap_free(netdev->stats_by_path);
1,446✔
249
        free(netdev->description);
1,446✔
250
        free(netdev->ifname);
1,446✔
251

252
        return mfree(netdev);
1,446✔
253
}
254

255
DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev, netdev_free);
29,237✔
256

257
void netdev_drop(NetDev *netdev) {
822✔
258
        if (!netdev)
822✔
259
                return;
260

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

267
                log_netdev_debug(netdev, "netdev removed");
74✔
268
                return;
74✔
269
        }
270

271
        if (NETDEV_VTABLE(netdev) && NETDEV_VTABLE(netdev)->drop)
354✔
272
                NETDEV_VTABLE(netdev)->drop(netdev);
4✔
273

274
        netdev->state = NETDEV_STATE_LINGER;
354✔
275

276
        log_netdev_debug(netdev, "netdev removed");
354✔
277

278
        netdev_detach(netdev);
354✔
279
}
280

281
static int netdev_attach_name_full(NetDev *netdev, const char *name, Hashmap **netdevs) {
1,143✔
282
        int r;
1,143✔
283

284
        assert(netdev);
1,143✔
285
        assert(name);
1,143✔
286

287
        r = hashmap_ensure_put(netdevs, &string_hash_ops, name, netdev);
1,143✔
288
        if (r == -ENOMEM)
1,143✔
UNCOV
289
                return log_oom();
×
290
        if (r == -EEXIST) {
1,143✔
291
                NetDev *n = hashmap_get(*netdevs, name);
1✔
292

293
                assert(n);
1✔
294
                if (!streq(netdev->filename, n->filename))
1✔
295
                        log_netdev_warning_errno(netdev, r,
1✔
296
                                                 "Device \"%s\" was already configured by \"%s\", ignoring %s.",
297
                                                 name, n->filename, netdev->filename);
298

299
                return -EEXIST;
1✔
300
        }
301
        assert(r > 0);
1,142✔
302

303
        return 0;
304
}
305

306
int netdev_attach_name(NetDev *netdev, const char *name) {
883✔
307
        assert(netdev);
883✔
308
        assert(netdev->manager);
883✔
309

310
        return netdev_attach_name_full(netdev, name, &netdev->manager->netdevs);
883✔
311
}
312

313
static int netdev_attach(NetDev *netdev) {
723✔
314
        int r;
723✔
315

316
        assert(netdev);
723✔
317
        assert(netdev->ifname);
723✔
318

319
        r = netdev_attach_name(netdev, netdev->ifname);
723✔
320
        if (r < 0)
723✔
321
                return r;
322

323
        if (NETDEV_VTABLE(netdev)->attach) {
722✔
324
                r = NETDEV_VTABLE(netdev)->attach(netdev);
156✔
325
                if (r < 0)
156✔
UNCOV
326
                        return r;
×
327
        }
328

329
        return 0;
330
}
331

332
int netdev_get(Manager *manager, const char *name, NetDev **ret) {
12,685✔
333
        NetDev *netdev;
12,685✔
334

335
        assert(manager);
12,685✔
336
        assert(name);
12,685✔
337
        assert(ret);
12,685✔
338

339
        netdev = hashmap_get(manager->netdevs, name);
12,685✔
340
        if (!netdev)
12,685✔
341
                return -ENOENT;
342

343
        *ret = netdev;
5,436✔
344

345
        return 0;
5,436✔
346
}
347

348
void link_assign_netdev(Link *link) {
6,295✔
349
        _unused_ _cleanup_(netdev_unrefp) NetDev *old = NULL;
6,295✔
350
        NetDev *netdev;
6,295✔
351

352
        assert(link);
6,295✔
353
        assert(link->manager);
6,295✔
354
        assert(link->ifname);
6,295✔
355

356
        old = TAKE_PTR(link->netdev);
6,295✔
357

358
        if (netdev_get(link->manager, link->ifname, &netdev) < 0)
6,295✔
359
                goto not_found;
4,302✔
360

361
        int ifindex = NETDEV_VTABLE(netdev)->get_ifindex ?
1,993✔
362
                NETDEV_VTABLE(netdev)->get_ifindex(netdev, link->ifname) :
1,993✔
363
                netdev->ifindex;
364
        if (ifindex != link->ifindex)
1,993✔
365
                goto not_found;
22✔
366

367
        if (NETDEV_VTABLE(netdev)->iftype != link->iftype)
1,971✔
UNCOV
368
                goto not_found;
×
369

370
        if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
1,971✔
371
                const char *kind;
1,959✔
372

373
                if (netdev->kind == NETDEV_KIND_TAP)
1,959✔
374
                        kind = "tun"; /* the kernel does not distinguish between tun and tap */
375
                else
376
                        kind = netdev_kind_to_string(netdev->kind);
1,945✔
377

378
                if (!streq_ptr(kind, link->kind))
1,959✔
UNCOV
379
                        goto not_found;
×
380
        }
381

382
        link->netdev = netdev_ref(netdev);
1,971✔
383

384
        if (netdev == old)
1,971✔
385
                return; /* The same NetDev found. */
386

387
        log_link_debug(link, "Found matching .netdev file: %s", netdev->filename);
652✔
388
        link_dirty(link);
652✔
389
        return;
390

391
not_found:
4,324✔
392

393
        if (old)
4,324✔
394
                /* Previously assigned NetDev is detached from Manager? Update the state file. */
395
                link_dirty(link);
8✔
396
}
397

398
void netdev_enter_failed(NetDev *netdev) {
19✔
399
        netdev->state = NETDEV_STATE_FAILED;
19✔
400
}
19✔
401

402
int netdev_enter_ready(NetDev *netdev) {
656✔
403
        assert(netdev);
656✔
404
        assert(netdev->ifname);
656✔
405

406
        if (!IN_SET(netdev->state, NETDEV_STATE_LOADING, NETDEV_STATE_CREATING))
656✔
407
                return 0;
408

409
        netdev->state = NETDEV_STATE_READY;
559✔
410

411
        log_netdev_info(netdev, "netdev ready");
559✔
412

413
        if (NETDEV_VTABLE(netdev)->post_create)
559✔
414
                NETDEV_VTABLE(netdev)->post_create(netdev, NULL);
25✔
415

416
        return 0;
417
}
418

419
bool netdev_needs_reconfigure(NetDev *netdev, NetDevLocalAddressType type) {
349✔
420
        assert(netdev);
349✔
421
        assert(type < _NETDEV_LOCAL_ADDRESS_TYPE_MAX);
349✔
422

423
        if (type < 0)
349✔
424
                return true;
425

426
        return NETDEV_VTABLE(netdev)->needs_reconfigure &&
488✔
427
                NETDEV_VTABLE(netdev)->needs_reconfigure(netdev, type);
141✔
428
}
429

430
/* callback for netdev's created without a backing Link */
431
static int netdev_create_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) {
402✔
432
        int r;
402✔
433

434
        assert(netdev);
402✔
435
        assert(netdev->state != _NETDEV_STATE_INVALID);
402✔
436

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

448
        return netdev_enter_ready(netdev);
392✔
449
}
450

451
int netdev_set_ifindex_internal(NetDev *netdev, int ifindex) {
1,978✔
452
        assert(netdev);
1,978✔
453
        assert(ifindex > 0);
1,978✔
454

455
        if (netdev->ifindex == ifindex)
1,978✔
456
                return 0; /* Already set. */
457

458
        if (netdev->ifindex > 0 && netdev->ifindex != ifindex)
474✔
UNCOV
459
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EEXIST),
×
460
                                                "Could not set ifindex to %i, already set to %i.",
461
                                                ifindex, netdev->ifindex);
462

463
        netdev->ifindex = ifindex;
474✔
464
        log_netdev_debug(netdev, "Gained index %i.", ifindex);
474✔
465
        return 1; /* set new ifindex. */
466
}
467

468
static int netdev_set_ifindex_impl(NetDev *netdev, const char *name, int ifindex) {
2,440✔
469
        assert(netdev);
2,440✔
470
        assert(name);
2,440✔
471
        assert(ifindex > 0);
2,440✔
472

473
        if (NETDEV_VTABLE(netdev)->set_ifindex)
2,440✔
474
                return NETDEV_VTABLE(netdev)->set_ifindex(netdev, name, ifindex);
942✔
475

476
        if (!streq(netdev->ifname, name))
1,498✔
UNCOV
477
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
×
478
                                                "Received netlink message with unexpected interface name %s (ifindex=%i).",
479
                                                name, ifindex);
480

481
        return netdev_set_ifindex_internal(netdev, ifindex);
1,498✔
482
}
483

484
int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) {
2,591✔
485
        uint16_t type;
2,591✔
486
        const char *kind;
2,591✔
487
        const char *received_kind;
2,591✔
488
        const char *received_name;
2,591✔
489
        int r, ifindex, family;
2,591✔
490

491
        assert(netdev);
2,591✔
492
        assert(message);
2,591✔
493

494
        r = sd_netlink_message_get_type(message, &type);
2,591✔
495
        if (r < 0)
2,591✔
496
                return log_netdev_warning_errno(netdev, r, "Could not get rtnl message type: %m");
×
497

498
        if (type != RTM_NEWLINK)
2,591✔
UNCOV
499
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Cannot set ifindex from unexpected rtnl message type.");
×
500

501
        r = sd_rtnl_message_get_family(message, &family);
2,591✔
502
        if (r < 0)
2,591✔
UNCOV
503
                return log_netdev_warning_errno(netdev, r, "Failed to get family from received rtnl message: %m");
×
504

505
        if (family != AF_UNSPEC)
2,591✔
506
                return 0; /* IFLA_LINKINFO is only contained in the message with AF_UNSPEC. */
507

508
        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2,440✔
509
        if (r < 0)
2,440✔
UNCOV
510
                return log_netdev_warning_errno(netdev, r, "Could not get ifindex: %m");
×
511
        if (ifindex <= 0)
2,440✔
UNCOV
512
                return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Got invalid ifindex: %d", ifindex);
×
513

514
        r = sd_netlink_message_read_string(message, IFLA_IFNAME, &received_name);
2,440✔
515
        if (r < 0)
2,440✔
UNCOV
516
                return log_netdev_warning_errno(netdev, r, "Could not get IFNAME: %m");
×
517

518
        if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
2,440✔
519

520
                r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2,426✔
521
                if (r < 0)
2,426✔
UNCOV
522
                        return log_netdev_warning_errno(netdev, r, "Could not get LINKINFO: %m");
×
523

524
                r = sd_netlink_message_read_string(message, IFLA_INFO_KIND, &received_kind);
2,426✔
525
                if (r < 0)
2,426✔
UNCOV
526
                        return log_netdev_warning_errno(netdev, r, "Could not get KIND: %m");
×
527

528
                r = sd_netlink_message_exit_container(message);
2,426✔
529
                if (r < 0)
2,426✔
UNCOV
530
                        return log_netdev_warning_errno(netdev, r, "Could not exit container: %m");
×
531

532
                if (netdev->kind == NETDEV_KIND_TAP)
2,426✔
533
                        /* the kernel does not distinguish between tun and tap */
534
                        kind = "tun";
535
                else
536
                        kind = netdev_kind_to_string(netdev->kind);
2,408✔
537
                if (!kind)
2,408✔
538
                        return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Could not get netdev kind.");
×
539

540
                if (!streq(kind, received_kind))
2,426✔
UNCOV
541
                        return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
×
542
                                                        "Received newlink with wrong KIND %s, expected %s",
543
                                                        received_kind, kind);
544
        }
545

546
        return netdev_set_ifindex_impl(netdev, received_name, ifindex);
2,440✔
547
}
548

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

551
int netdev_generate_hw_addr(
612✔
552
                NetDev *netdev,
553
                Link *parent,
554
                const char *name,
555
                const struct hw_addr_data *hw_addr,
556
                struct hw_addr_data *ret) {
557

558
        struct hw_addr_data a = HW_ADDR_NULL;
612✔
559
        bool is_static = false;
612✔
560
        int r;
612✔
561

562
        assert(netdev);
612✔
563
        assert(name);
612✔
564
        assert(hw_addr);
612✔
565
        assert(ret);
612✔
566

567
        if (hw_addr_equal(hw_addr, &HW_ADDR_NONE)) {
612✔
568
                *ret = HW_ADDR_NULL;
1✔
569
                return 0;
1✔
570
        }
571

572
        if (hw_addr->length == 0) {
611✔
573
                uint64_t result;
438✔
574

575
                /* HardwareAddress= is not specified. */
576

577
                if (!NETDEV_VTABLE(netdev)->generate_mac)
438✔
578
                        goto finalize;
103✔
579

580
                if (!IN_SET(NETDEV_VTABLE(netdev)->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND))
335✔
UNCOV
581
                        goto finalize;
×
582

583
                r = net_get_unique_predictable_data_from_name(name, &HASH_KEY, &result);
335✔
584
                if (r < 0) {
335✔
UNCOV
585
                        log_netdev_warning_errno(netdev, r,
×
586
                                                 "Failed to generate persistent MAC address, ignoring: %m");
UNCOV
587
                        goto finalize;
×
588
                }
589

590
                a.length = arphrd_to_hw_addr_len(NETDEV_VTABLE(netdev)->iftype);
335✔
591

592
                switch (NETDEV_VTABLE(netdev)->iftype) {
335✔
593
                case ARPHRD_ETHER:
335✔
594
                        assert(a.length <= sizeof(result));
335✔
595
                        memcpy(a.bytes, &result, a.length);
335✔
596

597
                        if (ether_addr_is_null(&a.ether) || ether_addr_is_broadcast(&a.ether)) {
670✔
UNCOV
598
                                log_netdev_warning(netdev, "Failed to generate persistent MAC address, ignoring.");
×
UNCOV
599
                                a = HW_ADDR_NULL;
×
UNCOV
600
                                goto finalize;
×
601
                        }
602

603
                        break;
604
                case ARPHRD_INFINIBAND:
×
UNCOV
605
                        if (result == 0) {
×
UNCOV
606
                                log_netdev_warning(netdev, "Failed to generate persistent MAC address.");
×
607
                                goto finalize;
×
608
                        }
609

610
                        assert(a.length >= sizeof(result));
×
611
                        memzero(a.bytes, a.length - sizeof(result));
×
612
                        memcpy(a.bytes + a.length - sizeof(result), &result, sizeof(result));
×
UNCOV
613
                        break;
×
UNCOV
614
                default:
×
UNCOV
615
                        assert_not_reached();
×
616
                }
617

618
        } else {
619
                a = *hw_addr;
173✔
620
                is_static = true;
173✔
621
        }
622

623
        r = net_verify_hardware_address(name, is_static, NETDEV_VTABLE(netdev)->iftype,
508✔
624
                                        parent ? &parent->hw_addr : NULL, &a);
625
        if (r < 0)
508✔
626
                return r;
627

628
finalize:
508✔
629
        *ret = a;
611✔
630
        return 0;
611✔
631
}
632

633
static bool netdev_can_set_mac(NetDev *netdev, const struct hw_addr_data *hw_addr) {
527✔
634
        assert(netdev);
527✔
635
        assert(netdev->manager);
527✔
636
        assert(hw_addr);
527✔
637

638
        if (hw_addr->length <= 0)
527✔
639
                return false;
527✔
640

641
        Link *link;
423✔
642
        if (link_get_by_index(netdev->manager, netdev->ifindex, &link) < 0)
423✔
643
                return true; /* The netdev does not exist yet. We can set MAC address. */
644

645
        if (hw_addr_equal(&link->hw_addr, hw_addr))
54✔
646
                return false; /* Unchanged, not necessary to set. */
647

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

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

663
static bool netdev_can_set_mtu(NetDev *netdev, uint32_t mtu) {
527✔
664
        assert(netdev);
527✔
665

666
        if (mtu <= 0)
527✔
667
                return false;
527✔
668

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

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

676
        if (link->mtu == mtu)
9✔
677
                return false; /* Unchanged, not necessary to set. */
678

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

683
        /* By default, allow to update the MTU. */
684
        return true;
685
}
686

687
static int netdev_create_message(NetDev *netdev, Link *link, sd_netlink_message *m) {
527✔
688
        int r;
527✔
689

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

699
        struct hw_addr_data hw_addr;
527✔
700
        r = netdev_generate_hw_addr(netdev, link, netdev->ifname, &netdev->hw_addr, &hw_addr);
527✔
701
        if (r < 0)
527✔
702
                return r;
703

704
        if (netdev_can_set_mac(netdev, &hw_addr)) {
527✔
705
                log_netdev_debug(netdev, "Using MAC address: %s", HW_ADDR_TO_STR(&hw_addr));
372✔
706
                r = netlink_message_append_hw_addr(m, IFLA_ADDRESS, &hw_addr);
372✔
707
                if (r < 0)
372✔
708
                        return r;
709
        }
710

711
        if (netdev_can_set_mtu(netdev, netdev->mtu)) {
527✔
712
                r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu);
29✔
713
                if (r < 0)
29✔
714
                        return r;
715
        }
716

717
        if (link) {
527✔
718
                r = sd_netlink_message_append_u32(m, IFLA_LINK, link->ifindex);
125✔
719
                if (r < 0)
125✔
720
                        return r;
721
        }
722

723
        r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
527✔
724
        if (r < 0)
527✔
725
                return r;
726

727
        if (NETDEV_VTABLE(netdev)->fill_message_create) {
527✔
728
                r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind));
248✔
729
                if (r < 0)
248✔
730
                        return r;
731

732
                r = NETDEV_VTABLE(netdev)->fill_message_create(netdev, link, m);
248✔
733
                if (r < 0)
248✔
734
                        return r;
735

736
                r = sd_netlink_message_close_container(m);
248✔
737
                if (r < 0)
248✔
738
                        return r;
739
        } else {
740
                r = sd_netlink_message_append_string(m, IFLA_INFO_KIND, netdev_kind_to_string(netdev->kind));
279✔
741
                if (r < 0)
279✔
742
                        return r;
743
        }
744

745
        r = sd_netlink_message_close_container(m);
527✔
746
        if (r < 0)
527✔
UNCOV
747
                return r;
×
748

749
        return 0;
750
}
751

752
static int independent_netdev_create(NetDev *netdev) {
420✔
753
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
420✔
754
        int r;
420✔
755

756
        assert(netdev);
420✔
757
        assert(netdev->manager);
420✔
758

759
        /* create netdev */
760
        if (NETDEV_VTABLE(netdev)->create) {
420✔
761
                r = NETDEV_VTABLE(netdev)->create(netdev);
18✔
762
                if (r < 0)
18✔
763
                        return r;
764

765
                log_netdev_debug(netdev, "Created");
18✔
766
                return 0;
18✔
767
        }
768

769
        r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, netdev->ifindex);
402✔
770
        if (r < 0)
402✔
771
                return r;
772

773
        r = netdev_create_message(netdev, NULL, m);
402✔
774
        if (r < 0)
402✔
775
                return r;
776

777
        r = netlink_call_async(netdev->manager->rtnl, NULL, m, netdev_create_handler,
402✔
778
                               netdev_destroy_callback, netdev);
779
        if (r < 0)
402✔
780
                return r;
781

782
        netdev_ref(netdev);
402✔
783

784
        netdev->state = NETDEV_STATE_CREATING;
402✔
785
        log_netdev_debug(netdev, "Creating");
402✔
786
        return 0;
787
}
788

789
static int stacked_netdev_create(NetDev *netdev, Link *link, Request *req) {
125✔
790
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
125✔
791
        int r;
125✔
792

793
        assert(netdev);
125✔
794
        assert(netdev->manager);
125✔
795
        assert(link);
125✔
796
        assert(req);
125✔
797

798
        r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, netdev->ifindex);
125✔
799
        if (r < 0)
125✔
800
                return r;
801

802
        r = netdev_create_message(netdev, link, m);
125✔
803
        if (r < 0)
125✔
804
                return r;
805

806
        r = request_call_netlink_async(netdev->manager->rtnl, m, req);
125✔
807
        if (r < 0)
125✔
808
                return r;
809

810
        netdev->state = NETDEV_STATE_CREATING;
125✔
811
        log_netdev_debug(netdev, "Creating");
125✔
812
        return 0;
813
}
814

815
static bool link_is_ready_to_create_stacked_netdev_one(Link *link, bool allow_unmanaged) {
1,632✔
816
        assert(link);
1,632✔
817

818
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
1,632✔
819
                return false;
820

821
        if (!link->network)
1,632✔
822
                return allow_unmanaged;
823

824
        if (link->set_link_messages > 0)
1,632✔
825
                return false;
826

827
        /* If stacked netdevs are created before the underlying interface being activated, then
828
         * the activation policy for the netdevs are ignored. See issue #22593. */
829
        if (!link->activated)
1,564✔
830
                return false;
1,170✔
831

832
        return true;
833
}
834

835
static bool link_is_ready_to_create_stacked_netdev(Link *link) {
1,632✔
836
        return check_ready_for_all_sr_iov_ports(link, /* allow_unmanaged = */ false,
1,632✔
837
                                                link_is_ready_to_create_stacked_netdev_one);
838
}
839

840
static int netdev_is_ready_to_create(NetDev *netdev, Link *link) {
2,149✔
841
        assert(netdev);
2,149✔
842

843
        if (link && !link_is_ready_to_create_stacked_netdev(link))
2,149✔
844
                return false;
845

846
        if (NETDEV_VTABLE(netdev)->is_ready_to_create)
911✔
847
                return NETDEV_VTABLE(netdev)->is_ready_to_create(netdev, link);
480✔
848

849
        return true;
850
}
851

852
static int stacked_netdev_process_request(Request *req, Link *link, void *userdata) {
1,646✔
853
        NetDev *netdev = ASSERT_PTR(userdata);
1,646✔
854
        int r;
1,646✔
855

856
        assert(req);
1,646✔
857
        assert(link);
1,646✔
858

859
        if (!netdev_is_managed(netdev))
1,646✔
UNCOV
860
                goto cancelled; /* Already detached, due to e.g. reloading .netdev files, cancelling the request. */
×
861

862
        if (NETDEV_VTABLE(netdev)->keep_existing && netdev->ifindex > 0) {
1,646✔
863
                /* Already exists, and the netdev does not support updating, entering the ready state. */
864
                r = netdev_enter_ready(netdev);
14✔
865
                if (r < 0)
14✔
866
                        return r;
867

868
                goto cancelled;
14✔
869
        }
870

871
        r = netdev_is_ready_to_create(netdev, link);
1,632✔
872
        if (r <= 0)
1,632✔
873
                return r;
874

875
        r = stacked_netdev_create(netdev, link, req);
125✔
876
        if (r < 0)
125✔
UNCOV
877
                return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
×
878

879
        return 1;
880

881
cancelled:
14✔
882
        assert_se(TAKE_PTR(req->counter) == &link->create_stacked_netdev_messages);
14✔
883
        link->create_stacked_netdev_messages--;
14✔
884

885
        if (link->create_stacked_netdev_messages == 0) {
14✔
886
                link->stacked_netdevs_created = true;
14✔
887
                log_link_debug(link, "Stacked netdevs created.");
14✔
888
                link_check_ready(link);
14✔
889
        }
890

891
        return 1;
892
}
893

894
static int create_stacked_netdev_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, void *userdata) {
125✔
895
        NetDev *netdev = ASSERT_PTR(userdata);
125✔
896
        int r;
125✔
897

898
        assert(m);
125✔
899
        assert(link);
125✔
900

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

913
        (void) netdev_enter_ready(netdev);
125✔
914

915
        if (link->create_stacked_netdev_messages == 0) {
125✔
916
                link->stacked_netdevs_created = true;
73✔
917
                log_link_debug(link, "Stacked netdevs created.");
73✔
918
                link_check_ready(link);
73✔
919
        }
920

921
        return 0;
922
}
923

924
int link_request_stacked_netdev(Link *link, NetDev *netdev) {
143✔
925
        int r;
143✔
926

927
        assert(link);
143✔
928
        assert(netdev);
143✔
929

930
        if (!netdev_is_stacked(netdev))
143✔
931
                return -EINVAL;
932

933
        if (!netdev_is_managed(netdev))
143✔
934
                return 0; /* Already detached, due to e.g. reloading .netdev files. */
935

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

949
        netdev_ref(netdev);
141✔
950
        log_link_debug(link, "Requested stacked netdev '%s'", netdev->ifname);
141✔
951
        return 1;
952
}
953

954
static int independent_netdev_process_request(Request *req, Link *link, void *userdata) {
533✔
955
        NetDev *netdev = ASSERT_PTR(userdata);
533✔
956
        int r;
533✔
957

958
        assert(!link);
533✔
959

960
        if (!netdev_is_managed(netdev))
533✔
961
                return 1; /* Already detached, due to e.g. reloading .netdev files, cancelling the request. */
962

963
        if (NETDEV_VTABLE(netdev)->keep_existing && netdev->ifindex > 0) {
533✔
964
                /* Already exists, and the netdev does not support updating, entering the ready state. */
965
                r = netdev_enter_ready(netdev);
16✔
966
                if (r < 0)
16✔
967
                        return r;
968

969
                return 1; /* Skip this request. */
16✔
970
        }
971

972
        r = netdev_is_ready_to_create(netdev, NULL);
517✔
973
        if (r <= 0)
517✔
974
                return r;
975

976
        r = independent_netdev_create(netdev);
420✔
977
        if (r < 0)
420✔
UNCOV
978
                return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
×
979

980
        return 1;
981
}
982

983
static int netdev_request_to_create(NetDev *netdev) {
722✔
984
        int r;
722✔
985

986
        assert(netdev);
722✔
987
        assert(netdev->manager);
722✔
988

989
        if (netdev->manager->test_mode)
722✔
990
                return 0;
991

992
        if (netdev_is_stacked(netdev))
722✔
993
                return 0;
994

995
        if (!netdev_is_managed(netdev))
590✔
996
                return 0; /* Already detached, due to e.g. reloading .netdev files. */
997

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

1001
        r = netdev_queue_request(netdev, independent_netdev_process_request, NULL);
436✔
1002
        if (r < 0)
436✔
UNCOV
1003
                return log_netdev_warning_errno(netdev, r, "Failed to request to create netdev: %m");
×
1004

1005
        return 0;
1006
}
1007

1008
int netdev_load_one(Manager *manager, const char *filename, NetDev **ret) {
723✔
1009
        _cleanup_(netdev_unrefp) NetDev *netdev_raw = NULL, *netdev = NULL;
723✔
1010
        const char *dropin_dirname;
723✔
1011
        int r;
723✔
1012

1013
        assert(manager);
723✔
1014
        assert(filename);
723✔
1015
        assert(ret);
723✔
1016

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

1023
        netdev_raw = new(NetDev, 1);
723✔
1024
        if (!netdev_raw)
723✔
UNCOV
1025
                return log_oom();
×
1026

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

1033
        dropin_dirname = strjoina(basename(filename), ".d");
3,615✔
1034
        r = config_parse_many(
1,446✔
1035
                        STRV_MAKE_CONST(filename), NETWORK_DIRS, dropin_dirname, /* root = */ NULL,
723✔
1036
                        NETDEV_COMMON_SECTIONS NETDEV_OTHER_SECTIONS,
1037
                        config_item_perf_lookup, network_netdev_gperf_lookup,
1038
                        CONFIG_PARSE_WARN,
1039
                        netdev_raw,
1040
                        NULL,
1041
                        NULL);
1042
        if (r < 0)
723✔
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))
723✔
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)
723✔
1050
                return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "NetDev has no Kind= configured in \"%s\", ignoring.", filename);
×
1051

1052
        if (!netdev_raw->ifname)
723✔
UNCOV
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);
723✔
1056
        if (!netdev)
723✔
UNCOV
1057
                return log_oom();
×
1058

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

1065
        if (NETDEV_VTABLE(netdev)->init)
723✔
1066
                NETDEV_VTABLE(netdev)->init(netdev);
207✔
1067

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

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

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

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

1092
        *ret = TAKE_PTR(netdev);
723✔
1093
        return 0;
723✔
1094
}
1095

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

1100
        assert(manager);
418✔
1101

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

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

1109
                if (netdev_load_one(manager, *f, &netdev) < 0)
463✔
UNCOV
1110
                        continue;
×
1111

1112
                if (netdev_attach(netdev) < 0)
463✔
1113
                        continue;
1✔
1114

1115
                if (netdev_request_to_create(netdev) < 0)
462✔
UNCOV
1116
                        continue;
×
1117

1118
                TAKE_PTR(netdev);
462✔
1119
        }
1120

1121
        return 0;
1122
}
1123

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

1129
        assert(manager);
136✔
1130

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

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

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

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

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

1148
                        continue;
11✔
1149
                }
1150

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

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

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

1160
                        continue;
90✔
1161
                }
1162

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

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

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

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

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

1188
                TAKE_PTR(netdev);
260✔
1189
        }
1190

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

1196
        return 0;
136✔
1197
}
1198

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

1211
        NetDevKind k, *kind = ASSERT_PTR(data);
1,446✔
1212

1213
        assert(filename);
1,446✔
1214
        assert(rvalue);
1,446✔
1215

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

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

1229
        *kind = k;
1,446✔
1230

1231
        return 0;
1,446✔
1232
}
1233

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

1246
        struct hw_addr_data *hw_addr = ASSERT_PTR(data);
465✔
1247

1248
        assert(rvalue);
465✔
1249

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

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