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