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