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

systemd / systemd / 14554080340

19 Apr 2025 11:46AM UTC coverage: 72.101% (-0.03%) from 72.13%
14554080340

push

github

web-flow
Add two new paragraphs to coding style about header files (#37188)

296880 of 411754 relevant lines covered (72.1%)

687547.52 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 "netif-util.h"
31
#include "netlink-util.h"
32
#include "network-util.h"
33
#include "networkd-manager.h"
34
#include "networkd-queue.h"
35
#include "networkd-setlink.h"
36
#include "networkd-sriov.h"
37
#include "networkd-state-file.h"
38
#include "nlmon.h"
39
#include "path-lookup.h"
40
#include "siphash24.h"
41
#include "stat-util.h"
42
#include "string-table.h"
43
#include "string-util.h"
44
#include "strv.h"
45
#include "tunnel.h"
46
#include "tuntap.h"
47
#include "vcan.h"
48
#include "veth.h"
49
#include "vlan.h"
50
#include "vrf.h"
51
#include "vxcan.h"
52
#include "vxlan.h"
53
#include "wireguard.h"
54
#include "wlan.h"
55
#include "xfrm.h"
56

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

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

139
DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
6,554✔
140

141
bool netdev_is_managed(NetDev *netdev) {
2,931✔
142
        if (!netdev || !netdev->manager || !netdev->ifname)
2,931✔
143
                return false;
144

145
        return hashmap_get(netdev->manager->netdevs, netdev->ifname) == netdev;
2,931✔
146
}
147

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

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

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

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

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

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

193
        return true;
194
}
195

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

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

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

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

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

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

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

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

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

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

228
        netdev_detach_impl(netdev);
1,446✔
229

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

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

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

252
DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev, netdev_free);
29,244✔
253

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

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

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

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

271
        netdev->state = NETDEV_STATE_LINGER;
354✔
272

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

275
        netdev_detach(netdev);
354✔
276
}
277

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

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

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

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

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

300
        return 0;
301
}
302

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

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

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

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

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

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

326
        return 0;
327
}
328

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

332
        assert(manager);
12,719✔
333
        assert(name);
12,719✔
334
        assert(ret);
12,719✔
335

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

340
        *ret = netdev;
5,446✔
341

342
        return 0;
5,446✔
343
}
344

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

349
        assert(link);
6,310✔
350
        assert(link->manager);
6,310✔
351
        assert(link->ifname);
6,310✔
352

353
        old = TAKE_PTR(link->netdev);
6,310✔
354

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

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

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

367
        if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
1,973✔
368
                const char *kind;
1,961✔
369

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

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

379
        link->netdev = netdev_ref(netdev);
1,973✔
380

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

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

388
not_found:
4,337✔
389

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

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

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

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

406
        netdev->state = NETDEV_STATE_READY;
558✔
407

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

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

413
        return 0;
414
}
415

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

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

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

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

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

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

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

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

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

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

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

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

470
        if (NETDEV_VTABLE(netdev)->set_ifindex)
2,440✔
471
                return NETDEV_VTABLE(netdev)->set_ifindex(netdev, name, ifindex);
944✔
472

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

478
        return netdev_set_ifindex_internal(netdev, ifindex);
1,496✔
479
}
480

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

488
        assert(netdev);
2,599✔
489
        assert(message);
2,599✔
490

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

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

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

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

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

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

515
        if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
2,440✔
516

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

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

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

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

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

543
        return netdev_set_ifindex_impl(netdev, received_name, ifindex);
2,440✔
544
}
545

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

746
        return 0;
747
}
748

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

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

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

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

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

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

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

779
        netdev_ref(netdev);
402✔
780

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

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

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

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

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

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

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

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

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

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

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

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

829
        return true;
830
}
831

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

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

840
        if (link && !link_is_ready_to_create_stacked_netdev(link))
2,058✔
841
                return false;
842

843
        if (NETDEV_VTABLE(netdev)->is_ready_to_create)
931✔
844
                return NETDEV_VTABLE(netdev)->is_ready_to_create(netdev, link);
500✔
845

846
        return true;
847
}
848

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

853
        assert(req);
1,555✔
854
        assert(link);
1,555✔
855

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

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

865
                goto cancelled;
14✔
866
        }
867

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

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

876
        return 1;
877

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

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

888
        return 1;
889
}
890

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

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

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

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

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

918
        return 0;
919
}
920

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

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

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

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

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

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

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

955
        assert(!link);
533✔
956

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

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

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

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

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

977
        return 1;
978
}
979

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

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

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

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

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

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

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

1002
        return 0;
1003
}
1004

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1097
        assert(manager);
418✔
1098

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

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

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

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

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

1115
                TAKE_PTR(netdev);
462✔
1116
        }
1117

1118
        return 0;
1119
}
1120

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

1126
        assert(manager);
136✔
1127

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

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

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

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

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

1145
                        continue;
11✔
1146
                }
1147

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

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

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

1157
                        continue;
90✔
1158
                }
1159

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

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

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

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

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

1184
                TAKE_PTR(netdev);
260✔
1185
        }
1186

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

1192
        return 0;
136✔
1193
}
1194

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

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

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

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

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

1225
        *kind = k;
1,446✔
1226

1227
        return 0;
1,446✔
1228
}
1229

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

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

1244
        assert(rvalue);
465✔
1245

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

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