]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/netdev.c
Merge pull request #12030 from poettering/condition-memory
[thirdparty/systemd.git] / src / network / netdev / netdev.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <net/if.h>
4
5 #include "alloc-util.h"
6 #include "conf-files.h"
7 #include "conf-parser.h"
8 #include "fd-util.h"
9 #include "list.h"
10 #include "netdev/bond.h"
11 #include "netdev/bridge.h"
12 #include "netdev/dummy.h"
13 #include "netdev/fou-tunnel.h"
14 #include "netdev/geneve.h"
15 #include "netdev/ipvlan.h"
16 #include "netdev/l2tp-tunnel.h"
17 #include "netdev/macvlan.h"
18 #include "netdev/netdev.h"
19 #include "netdev/netdevsim.h"
20 #include "netdev/tunnel.h"
21 #include "netdev/tuntap.h"
22 #include "netdev/vcan.h"
23 #include "netdev/veth.h"
24 #include "netdev/vlan.h"
25 #include "netdev/vrf.h"
26 #include "netdev/vxcan.h"
27 #include "netdev/vxlan.h"
28 #include "netdev/wireguard.h"
29 #include "netlink-util.h"
30 #include "network-internal.h"
31 #include "networkd-link.h"
32 #include "networkd-manager.h"
33 #include "siphash24.h"
34 #include "stat-util.h"
35 #include "string-table.h"
36 #include "string-util.h"
37 #include "strv.h"
38
39 const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX] = {
40 [NETDEV_KIND_BRIDGE] = &bridge_vtable,
41 [NETDEV_KIND_BOND] = &bond_vtable,
42 [NETDEV_KIND_VLAN] = &vlan_vtable,
43 [NETDEV_KIND_MACVLAN] = &macvlan_vtable,
44 [NETDEV_KIND_MACVTAP] = &macvtap_vtable,
45 [NETDEV_KIND_IPVLAN] = &ipvlan_vtable,
46 [NETDEV_KIND_VXLAN] = &vxlan_vtable,
47 [NETDEV_KIND_IPIP] = &ipip_vtable,
48 [NETDEV_KIND_GRE] = &gre_vtable,
49 [NETDEV_KIND_GRETAP] = &gretap_vtable,
50 [NETDEV_KIND_IP6GRE] = &ip6gre_vtable,
51 [NETDEV_KIND_IP6GRETAP] = &ip6gretap_vtable,
52 [NETDEV_KIND_SIT] = &sit_vtable,
53 [NETDEV_KIND_VTI] = &vti_vtable,
54 [NETDEV_KIND_VTI6] = &vti6_vtable,
55 [NETDEV_KIND_VETH] = &veth_vtable,
56 [NETDEV_KIND_DUMMY] = &dummy_vtable,
57 [NETDEV_KIND_TUN] = &tun_vtable,
58 [NETDEV_KIND_TAP] = &tap_vtable,
59 [NETDEV_KIND_IP6TNL] = &ip6tnl_vtable,
60 [NETDEV_KIND_VRF] = &vrf_vtable,
61 [NETDEV_KIND_VCAN] = &vcan_vtable,
62 [NETDEV_KIND_GENEVE] = &geneve_vtable,
63 [NETDEV_KIND_VXCAN] = &vxcan_vtable,
64 [NETDEV_KIND_WIREGUARD] = &wireguard_vtable,
65 [NETDEV_KIND_NETDEVSIM] = &netdevsim_vtable,
66 [NETDEV_KIND_FOU] = &foutnl_vtable,
67 [NETDEV_KIND_ERSPAN] = &erspan_vtable,
68 [NETDEV_KIND_L2TP] = &l2tptnl_vtable,
69 };
70
71 static const char* const netdev_kind_table[_NETDEV_KIND_MAX] = {
72 [NETDEV_KIND_BRIDGE] = "bridge",
73 [NETDEV_KIND_BOND] = "bond",
74 [NETDEV_KIND_VLAN] = "vlan",
75 [NETDEV_KIND_MACVLAN] = "macvlan",
76 [NETDEV_KIND_MACVTAP] = "macvtap",
77 [NETDEV_KIND_IPVLAN] = "ipvlan",
78 [NETDEV_KIND_VXLAN] = "vxlan",
79 [NETDEV_KIND_IPIP] = "ipip",
80 [NETDEV_KIND_GRE] = "gre",
81 [NETDEV_KIND_GRETAP] = "gretap",
82 [NETDEV_KIND_IP6GRE] = "ip6gre",
83 [NETDEV_KIND_IP6GRETAP] = "ip6gretap",
84 [NETDEV_KIND_SIT] = "sit",
85 [NETDEV_KIND_VETH] = "veth",
86 [NETDEV_KIND_VTI] = "vti",
87 [NETDEV_KIND_VTI6] = "vti6",
88 [NETDEV_KIND_DUMMY] = "dummy",
89 [NETDEV_KIND_TUN] = "tun",
90 [NETDEV_KIND_TAP] = "tap",
91 [NETDEV_KIND_IP6TNL] = "ip6tnl",
92 [NETDEV_KIND_VRF] = "vrf",
93 [NETDEV_KIND_VCAN] = "vcan",
94 [NETDEV_KIND_GENEVE] = "geneve",
95 [NETDEV_KIND_VXCAN] = "vxcan",
96 [NETDEV_KIND_WIREGUARD] = "wireguard",
97 [NETDEV_KIND_NETDEVSIM] = "netdevsim",
98 [NETDEV_KIND_FOU] = "fou",
99 [NETDEV_KIND_ERSPAN] = "erspan",
100 [NETDEV_KIND_L2TP] = "l2tp",
101 };
102
103 DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
104
105 int config_parse_netdev_kind(
106 const char *unit,
107 const char *filename,
108 unsigned line,
109 const char *section,
110 unsigned section_line,
111 const char *lvalue,
112 int ltype,
113 const char *rvalue,
114 void *data,
115 void *userdata) {
116
117 NetDevKind k, *kind = data;
118
119 assert(rvalue);
120 assert(data);
121
122 k = netdev_kind_from_string(rvalue);
123 if (k < 0) {
124 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse netdev kind, ignoring assignment: %s", rvalue);
125 return 0;
126 }
127
128 if (*kind != _NETDEV_KIND_INVALID && *kind != k) {
129 log_syntax(unit, LOG_ERR, filename, line, 0,
130 "Specified netdev kind is different from the previous value '%s', ignoring assignment: %s",
131 netdev_kind_to_string(*kind), rvalue);
132 return 0;
133 }
134
135 *kind = k;
136
137 return 0;
138 }
139
140 static void netdev_callbacks_clear(NetDev *netdev) {
141 netdev_join_callback *callback;
142
143 if (!netdev)
144 return;
145
146 while ((callback = netdev->callbacks)) {
147 LIST_REMOVE(callbacks, netdev->callbacks, callback);
148 link_unref(callback->link);
149 free(callback);
150 }
151 }
152
153 bool netdev_is_managed(NetDev *netdev) {
154 if (!netdev || !netdev->manager || !netdev->ifname)
155 return false;
156
157 return hashmap_get(netdev->manager->netdevs, netdev->ifname) == netdev;
158 }
159
160 static void netdev_detach_from_manager(NetDev *netdev) {
161 if (netdev->ifname && netdev->manager)
162 hashmap_remove(netdev->manager->netdevs, netdev->ifname);
163 }
164
165 static NetDev *netdev_free(NetDev *netdev) {
166 assert(netdev);
167
168 netdev_callbacks_clear(netdev);
169
170 netdev_detach_from_manager(netdev);
171
172 free(netdev->filename);
173
174 free(netdev->description);
175 free(netdev->ifname);
176 free(netdev->mac);
177 condition_free_list(netdev->conditions);
178
179 /* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
180 * because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure
181 * allocation, with no room for per-kind fields), and once to read the kind's properties (with a full,
182 * comprehensive NetDev structure allocation with enough space for whatever the specific kind needs). Now, in
183 * the first case we shouldn't try to destruct the per-kind NetDev fields on destruction, in the second case we
184 * should. We use the state field to discern the two cases: it's _NETDEV_STATE_INVALID on the first "raw"
185 * call. */
186 if (netdev->state != _NETDEV_STATE_INVALID &&
187 NETDEV_VTABLE(netdev) &&
188 NETDEV_VTABLE(netdev)->done)
189 NETDEV_VTABLE(netdev)->done(netdev);
190
191 return mfree(netdev);
192 }
193
194 DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev, netdev_free);
195
196 void netdev_drop(NetDev *netdev) {
197 if (!netdev || netdev->state == NETDEV_STATE_LINGER)
198 return;
199
200 netdev->state = NETDEV_STATE_LINGER;
201
202 log_netdev_debug(netdev, "netdev removed");
203
204 netdev_callbacks_clear(netdev);
205
206 netdev_detach_from_manager(netdev);
207
208 netdev_unref(netdev);
209
210 return;
211 }
212
213 int netdev_get(Manager *manager, const char *name, NetDev **ret) {
214 NetDev *netdev;
215
216 assert(manager);
217 assert(name);
218 assert(ret);
219
220 netdev = hashmap_get(manager->netdevs, name);
221 if (!netdev) {
222 *ret = NULL;
223 return -ENOENT;
224 }
225
226 *ret = netdev;
227
228 return 0;
229 }
230
231 static int netdev_enter_failed(NetDev *netdev) {
232 netdev->state = NETDEV_STATE_FAILED;
233
234 netdev_callbacks_clear(netdev);
235
236 return 0;
237 }
238
239 static int netdev_enslave_ready(NetDev *netdev, Link* link, link_netlink_message_handler_t callback) {
240 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
241 int r;
242
243 assert(netdev);
244 assert(netdev->state == NETDEV_STATE_READY);
245 assert(netdev->manager);
246 assert(netdev->manager->rtnl);
247 assert(IN_SET(netdev->kind, NETDEV_KIND_BRIDGE, NETDEV_KIND_BOND, NETDEV_KIND_VRF));
248 assert(link);
249 assert(callback);
250
251 if (link->flags & IFF_UP && netdev->kind == NETDEV_KIND_BOND) {
252 log_netdev_debug(netdev, "Link '%s' was up when attempting to enslave it. Bringing link down.", link->ifname);
253 r = link_down(link);
254 if (r < 0)
255 return log_netdev_error_errno(netdev, r, "Could not bring link down: %m");
256 }
257
258 r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
259 if (r < 0)
260 return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m");
261
262 r = sd_netlink_message_append_u32(req, IFLA_MASTER, netdev->ifindex);
263 if (r < 0)
264 return log_netdev_error_errno(netdev, r, "Could not append IFLA_MASTER attribute: %m");
265
266 r = netlink_call_async(netdev->manager->rtnl, NULL, req, callback,
267 link_netlink_destroy_callback, link);
268 if (r < 0)
269 return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");
270
271 link_ref(link);
272
273 log_netdev_debug(netdev, "Enslaving link '%s'", link->ifname);
274
275 return 0;
276 }
277
278 static int netdev_enter_ready(NetDev *netdev) {
279 netdev_join_callback *callback, *callback_next;
280 int r;
281
282 assert(netdev);
283 assert(netdev->ifname);
284
285 if (netdev->state != NETDEV_STATE_CREATING)
286 return 0;
287
288 netdev->state = NETDEV_STATE_READY;
289
290 log_netdev_info(netdev, "netdev ready");
291
292 LIST_FOREACH_SAFE(callbacks, callback, callback_next, netdev->callbacks) {
293 /* enslave the links that were attempted to be enslaved before the
294 * link was ready */
295 r = netdev_enslave_ready(netdev, callback->link, callback->callback);
296 if (r < 0)
297 return r;
298
299 LIST_REMOVE(callbacks, netdev->callbacks, callback);
300 link_unref(callback->link);
301 free(callback);
302 }
303
304 if (NETDEV_VTABLE(netdev)->post_create)
305 NETDEV_VTABLE(netdev)->post_create(netdev, NULL, NULL);
306
307 return 0;
308 }
309
310 /* callback for netdev's created without a backing Link */
311 static int netdev_create_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) {
312 int r;
313
314 assert(netdev);
315 assert(netdev->state != _NETDEV_STATE_INVALID);
316
317 r = sd_netlink_message_get_errno(m);
318 if (r == -EEXIST)
319 log_netdev_info(netdev, "netdev exists, using existing without changing its parameters");
320 else if (r < 0) {
321 log_netdev_warning_errno(netdev, r, "netdev could not be created: %m");
322 netdev_drop(netdev);
323
324 return 1;
325 }
326
327 log_netdev_debug(netdev, "Created");
328
329 return 1;
330 }
331
332 static int netdev_enslave(NetDev *netdev, Link *link, link_netlink_message_handler_t callback) {
333 int r;
334
335 assert(netdev);
336 assert(netdev->manager);
337 assert(netdev->manager->rtnl);
338 assert(IN_SET(netdev->kind, NETDEV_KIND_BRIDGE, NETDEV_KIND_BOND, NETDEV_KIND_VRF));
339
340 if (netdev->state == NETDEV_STATE_READY) {
341 r = netdev_enslave_ready(netdev, link, callback);
342 if (r < 0)
343 return r;
344 } else if (IN_SET(netdev->state, NETDEV_STATE_LINGER, NETDEV_STATE_FAILED)) {
345 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
346
347 r = rtnl_message_new_synthetic_error(netdev->manager->rtnl, -ENODEV, 0, &m);
348 if (r >= 0)
349 callback(netdev->manager->rtnl, m, link);
350 } else {
351 /* the netdev is not yet read, save this request for when it is */
352 netdev_join_callback *cb;
353
354 cb = new(netdev_join_callback, 1);
355 if (!cb)
356 return log_oom();
357
358 *cb = (netdev_join_callback) {
359 .callback = callback,
360 .link = link_ref(link),
361 };
362
363 LIST_PREPEND(callbacks, netdev->callbacks, cb);
364
365 log_netdev_debug(netdev, "Will enslave '%s', when ready", link->ifname);
366 }
367
368 return 0;
369 }
370
371 int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) {
372 uint16_t type;
373 const char *kind;
374 const char *received_kind;
375 const char *received_name;
376 int r, ifindex;
377
378 assert(netdev);
379 assert(message);
380
381 r = sd_netlink_message_get_type(message, &type);
382 if (r < 0)
383 return log_netdev_error_errno(netdev, r, "Could not get rtnl message type: %m");
384
385 if (type != RTM_NEWLINK) {
386 log_netdev_error(netdev, "Cannot set ifindex from unexpected rtnl message type.");
387 return -EINVAL;
388 }
389
390 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
391 if (r < 0) {
392 log_netdev_error_errno(netdev, r, "Could not get ifindex: %m");
393 netdev_enter_failed(netdev);
394 return r;
395 } else if (ifindex <= 0) {
396 log_netdev_error(netdev, "Got invalid ifindex: %d", ifindex);
397 netdev_enter_failed(netdev);
398 return -EINVAL;
399 }
400
401 if (netdev->ifindex > 0) {
402 if (netdev->ifindex != ifindex) {
403 log_netdev_error(netdev, "Could not set ifindex to %d, already set to %d",
404 ifindex, netdev->ifindex);
405 netdev_enter_failed(netdev);
406 return -EEXIST;
407 } else
408 /* ifindex already set to the same for this netdev */
409 return 0;
410 }
411
412 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &received_name);
413 if (r < 0)
414 return log_netdev_error_errno(netdev, r, "Could not get IFNAME: %m");
415
416 if (!streq(netdev->ifname, received_name)) {
417 log_netdev_error(netdev, "Received newlink with wrong IFNAME %s", received_name);
418 netdev_enter_failed(netdev);
419 return r;
420 }
421
422 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
423 if (r < 0)
424 return log_netdev_error_errno(netdev, r, "Could not get LINKINFO: %m");
425
426 r = sd_netlink_message_read_string(message, IFLA_INFO_KIND, &received_kind);
427 if (r < 0)
428 return log_netdev_error_errno(netdev, r, "Could not get KIND: %m");
429
430 r = sd_netlink_message_exit_container(message);
431 if (r < 0)
432 return log_netdev_error_errno(netdev, r, "Could not exit container: %m");
433
434 if (netdev->kind == NETDEV_KIND_TAP)
435 /* the kernel does not distinguish between tun and tap */
436 kind = "tun";
437 else {
438 kind = netdev_kind_to_string(netdev->kind);
439 if (!kind) {
440 log_netdev_error(netdev, "Could not get kind");
441 netdev_enter_failed(netdev);
442 return -EINVAL;
443 }
444 }
445
446 if (!streq(kind, received_kind)) {
447 log_netdev_error(netdev,
448 "Received newlink with wrong KIND %s, "
449 "expected %s", received_kind, kind);
450 netdev_enter_failed(netdev);
451 return r;
452 }
453
454 netdev->ifindex = ifindex;
455
456 log_netdev_debug(netdev, "netdev has index %d", netdev->ifindex);
457
458 netdev_enter_ready(netdev);
459
460 return 0;
461 }
462
463 #define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
464
465 int netdev_get_mac(const char *ifname, struct ether_addr **ret) {
466 _cleanup_free_ struct ether_addr *mac = NULL;
467 uint64_t result;
468 size_t l, sz;
469 uint8_t *v;
470 int r;
471
472 assert(ifname);
473 assert(ret);
474
475 mac = new0(struct ether_addr, 1);
476 if (!mac)
477 return -ENOMEM;
478
479 l = strlen(ifname);
480 sz = sizeof(sd_id128_t) + l;
481 v = newa(uint8_t, sz);
482
483 /* fetch some persistent data unique to the machine */
484 r = sd_id128_get_machine((sd_id128_t*) v);
485 if (r < 0)
486 return r;
487
488 /* combine with some data unique (on this machine) to this
489 * netdev */
490 memcpy(v + sizeof(sd_id128_t), ifname, l);
491
492 /* Let's hash the host machine ID plus the container name. We
493 * use a fixed, but originally randomly created hash key here. */
494 result = siphash24(v, sz, HASH_KEY.bytes);
495
496 assert_cc(ETH_ALEN <= sizeof(result));
497 memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
498
499 /* see eth_random_addr in the kernel */
500 mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
501 mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
502
503 *ret = TAKE_PTR(mac);
504
505 return 0;
506 }
507
508 static int netdev_create(NetDev *netdev, Link *link, link_netlink_message_handler_t callback) {
509 int r;
510
511 assert(netdev);
512 assert(!link || callback);
513
514 /* create netdev */
515 if (NETDEV_VTABLE(netdev)->create) {
516 assert(!link);
517
518 r = NETDEV_VTABLE(netdev)->create(netdev);
519 if (r < 0)
520 return r;
521
522 log_netdev_debug(netdev, "Created");
523 } else {
524 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
525
526 r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0);
527 if (r < 0)
528 return log_netdev_error_errno(netdev, r, "Could not allocate RTM_NEWLINK message: %m");
529
530 r = sd_netlink_message_append_string(m, IFLA_IFNAME, netdev->ifname);
531 if (r < 0)
532 return log_netdev_error_errno(netdev, r, "Could not append IFLA_IFNAME, attribute: %m");
533
534 if (netdev->mac) {
535 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
536 if (r < 0)
537 return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");
538 }
539
540 if (netdev->mtu) {
541 r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu);
542 if (r < 0)
543 return log_netdev_error_errno(netdev, r, "Could not append IFLA_MTU attribute: %m");
544 }
545
546 if (link) {
547 r = sd_netlink_message_append_u32(m, IFLA_LINK, link->ifindex);
548 if (r < 0)
549 return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINK attribute: %m");
550 }
551
552 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
553 if (r < 0)
554 return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m");
555
556 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind));
557 if (r < 0)
558 return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");
559
560 if (NETDEV_VTABLE(netdev)->fill_message_create) {
561 r = NETDEV_VTABLE(netdev)->fill_message_create(netdev, link, m);
562 if (r < 0)
563 return r;
564 }
565
566 r = sd_netlink_message_close_container(m);
567 if (r < 0)
568 return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");
569
570 r = sd_netlink_message_close_container(m);
571 if (r < 0)
572 return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m");
573
574 if (link) {
575 r = netlink_call_async(netdev->manager->rtnl, NULL, m, callback,
576 link_netlink_destroy_callback, link);
577 if (r < 0)
578 return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");
579
580 link_ref(link);
581 } else {
582 r = netlink_call_async(netdev->manager->rtnl, NULL, m, netdev_create_handler,
583 netdev_destroy_callback, netdev);
584 if (r < 0)
585 return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");
586
587 netdev_ref(netdev);
588 }
589
590 netdev->state = NETDEV_STATE_CREATING;
591
592 log_netdev_debug(netdev, "Creating");
593 }
594
595 return 0;
596 }
597
598 static int netdev_create_after_configured(NetDev *netdev, Link *link) {
599 assert(netdev);
600 assert(link);
601 assert(NETDEV_VTABLE(netdev)->create_after_configured);
602
603 return NETDEV_VTABLE(netdev)->create_after_configured(netdev, link);
604 }
605
606 /* the callback must be called, possibly after a timeout, as otherwise the Link will hang */
607 int netdev_join(NetDev *netdev, Link *link, link_netlink_message_handler_t callback) {
608 int r;
609
610 assert(netdev);
611 assert(netdev->manager);
612 assert(netdev->manager->rtnl);
613
614 switch (netdev_get_create_type(netdev)) {
615 case NETDEV_CREATE_MASTER:
616 r = netdev_enslave(netdev, link, callback);
617 if (r < 0)
618 return r;
619
620 break;
621 case NETDEV_CREATE_STACKED:
622 r = netdev_create(netdev, link, callback);
623 if (r < 0)
624 return r;
625
626 break;
627 case NETDEV_CREATE_AFTER_CONFIGURED:
628 r = netdev_create_after_configured(netdev, link);
629 if (r < 0)
630 return r;
631 break;
632 default:
633 assert_not_reached("Can not join independent netdev");
634 }
635
636 return 0;
637 }
638
639 int netdev_load_one(Manager *manager, const char *filename) {
640 _cleanup_(netdev_unrefp) NetDev *netdev_raw = NULL, *netdev = NULL;
641 _cleanup_fclose_ FILE *file = NULL;
642 const char *dropin_dirname;
643 bool independent = false;
644 int r;
645
646 assert(manager);
647 assert(filename);
648
649 file = fopen(filename, "re");
650 if (!file) {
651 if (errno == ENOENT)
652 return 0;
653
654 return -errno;
655 }
656
657 if (null_or_empty_fd(fileno(file))) {
658 log_debug("Skipping empty file: %s", filename);
659 return 0;
660 }
661
662 netdev_raw = new(NetDev, 1);
663 if (!netdev_raw)
664 return log_oom();
665
666 *netdev_raw = (NetDev) {
667 .n_ref = 1,
668 .kind = _NETDEV_KIND_INVALID,
669 .state = _NETDEV_STATE_INVALID, /* an invalid state means done() of the implementation won't be called on destruction */
670 };
671
672 dropin_dirname = strjoina(basename(filename), ".d");
673 r = config_parse_many(filename, NETWORK_DIRS, dropin_dirname,
674 "Match\0NetDev\0",
675 config_item_perf_lookup, network_netdev_gperf_lookup,
676 CONFIG_PARSE_WARN|CONFIG_PARSE_RELAXED, netdev_raw);
677 if (r < 0)
678 return r;
679
680 /* skip out early if configuration does not match the environment */
681 if (!condition_test_list(netdev_raw->conditions, NULL, NULL, NULL)) {
682 log_debug("%s: Conditions in the file do not match the system environment, skipping.", filename);
683 return 0;
684 }
685
686 if (netdev_raw->kind == _NETDEV_KIND_INVALID) {
687 log_warning("NetDev has no Kind= configured in %s. Ignoring", filename);
688 return 0;
689 }
690
691 if (!netdev_raw->ifname) {
692 log_warning("NetDev without Name= configured in %s. Ignoring", filename);
693 return 0;
694 }
695
696 r = fseek(file, 0, SEEK_SET);
697 if (r < 0)
698 return -errno;
699
700 netdev = malloc0(NETDEV_VTABLE(netdev_raw)->object_size);
701 if (!netdev)
702 return log_oom();
703
704 netdev->n_ref = 1;
705 netdev->manager = manager;
706 netdev->kind = netdev_raw->kind;
707 netdev->state = NETDEV_STATE_LOADING; /* we initialize the state here for the first time,
708 so that done() will be called on destruction */
709
710 if (NETDEV_VTABLE(netdev)->init)
711 NETDEV_VTABLE(netdev)->init(netdev);
712
713 r = config_parse_many(filename, NETWORK_DIRS, dropin_dirname,
714 NETDEV_VTABLE(netdev)->sections,
715 config_item_perf_lookup, network_netdev_gperf_lookup,
716 CONFIG_PARSE_WARN, netdev);
717 if (r < 0)
718 return r;
719
720 /* verify configuration */
721 if (NETDEV_VTABLE(netdev)->config_verify) {
722 r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename);
723 if (r < 0)
724 return 0;
725 }
726
727 netdev->filename = strdup(filename);
728 if (!netdev->filename)
729 return log_oom();
730
731 if (!netdev->mac && netdev->kind != NETDEV_KIND_VLAN) {
732 r = netdev_get_mac(netdev->ifname, &netdev->mac);
733 if (r < 0)
734 return log_netdev_error_errno(netdev, r,
735 "Failed to generate predictable MAC address for %s: %m",
736 netdev->ifname);
737 }
738
739 r = hashmap_ensure_allocated(&netdev->manager->netdevs, &string_hash_ops);
740 if (r < 0)
741 return r;
742
743 r = hashmap_put(netdev->manager->netdevs, netdev->ifname, netdev);
744 if (r == -EEXIST) {
745 NetDev *n = hashmap_get(netdev->manager->netdevs, netdev->ifname);
746
747 assert(n);
748 log_netdev_warning_errno(netdev, r,
749 "The setting Name=%s in %s conflicts with the one in %s, ignoring",
750 netdev->ifname, netdev->filename, n->filename);
751
752 /* Clear ifname before netdev_free() is called. Otherwise, the NetDev object 'n' is
753 * removed from the hashmap 'manager->netdevs'. */
754 netdev->ifname = mfree(netdev->ifname);
755 return 0;
756 }
757 if (r < 0)
758 return r;
759
760 LIST_HEAD_INIT(netdev->callbacks);
761
762 log_netdev_debug(netdev, "loaded %s", netdev_kind_to_string(netdev->kind));
763
764 if (IN_SET(netdev_get_create_type(netdev), NETDEV_CREATE_MASTER, NETDEV_CREATE_INDEPENDENT)) {
765 r = netdev_create(netdev, NULL, NULL);
766 if (r < 0)
767 return r;
768 }
769
770 switch (netdev->kind) {
771 case NETDEV_KIND_IPIP:
772 independent = IPIP(netdev)->independent;
773 break;
774 case NETDEV_KIND_GRE:
775 independent = GRE(netdev)->independent;
776 break;
777 case NETDEV_KIND_GRETAP:
778 independent = GRETAP(netdev)->independent;
779 break;
780 case NETDEV_KIND_IP6GRE:
781 independent = IP6GRE(netdev)->independent;
782 break;
783 case NETDEV_KIND_IP6GRETAP:
784 independent = IP6GRETAP(netdev)->independent;
785 break;
786 case NETDEV_KIND_SIT:
787 independent = SIT(netdev)->independent;
788 break;
789 case NETDEV_KIND_VTI:
790 independent = VTI(netdev)->independent;
791 break;
792 case NETDEV_KIND_VTI6:
793 independent = VTI6(netdev)->independent;
794 break;
795 case NETDEV_KIND_IP6TNL:
796 independent = IP6TNL(netdev)->independent;
797 break;
798 case NETDEV_KIND_ERSPAN:
799 independent = ERSPAN(netdev)->independent;
800 break;
801 default:
802 break;
803 }
804
805 if (independent) {
806 r = netdev_create(netdev, NULL, NULL);
807 if (r < 0)
808 return r;
809 }
810
811 netdev = NULL;
812
813 return 0;
814 }
815
816 int netdev_load(Manager *manager) {
817 _cleanup_strv_free_ char **files = NULL;
818 char **f;
819 int r;
820
821 assert(manager);
822
823 hashmap_clear_with_destructor(manager->netdevs, netdev_unref);
824
825 r = conf_files_list_strv(&files, ".netdev", NULL, 0, NETWORK_DIRS);
826 if (r < 0)
827 return log_error_errno(r, "Failed to enumerate netdev files: %m");
828
829 STRV_FOREACH(f, files) {
830 r = netdev_load_one(manager, *f);
831 if (r < 0)
832 return r;
833 }
834
835 return 0;
836 }