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