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