]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/netdev.c
Merge pull request #12222 from yuwata/macsec
[thirdparty/systemd.git] / src / network / netdev / netdev.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <net/if.h>
4
5 #include "alloc-util.h"
6 #include "conf-files.h"
7 #include "conf-parser.h"
8 #include "fd-util.h"
9 #include "list.h"
10 #include "netdev/bond.h"
11 #include "netdev/bridge.h"
12 #include "netdev/dummy.h"
13 #include "netdev/fou-tunnel.h"
14 #include "netdev/geneve.h"
15 #include "netdev/ipvlan.h"
16 #include "netdev/l2tp-tunnel.h"
17 #include "netdev/macsec.h"
18 #include "netdev/macvlan.h"
19 #include "netdev/netdev.h"
20 #include "netdev/netdevsim.h"
21 #include "netdev/tunnel.h"
22 #include "netdev/tuntap.h"
23 #include "netdev/vcan.h"
24 #include "netdev/veth.h"
25 #include "netdev/vlan.h"
26 #include "netdev/vrf.h"
27 #include "netdev/vxcan.h"
28 #include "netdev/vxlan.h"
29 #include "netdev/wireguard.h"
30 #include "netlink-util.h"
31 #include "network-internal.h"
32 #include "networkd-link.h"
33 #include "networkd-manager.h"
34 #include "siphash24.h"
35 #include "stat-util.h"
36 #include "string-table.h"
37 #include "string-util.h"
38 #include "strv.h"
39
40 const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX] = {
41 [NETDEV_KIND_BRIDGE] = &bridge_vtable,
42 [NETDEV_KIND_BOND] = &bond_vtable,
43 [NETDEV_KIND_VLAN] = &vlan_vtable,
44 [NETDEV_KIND_MACVLAN] = &macvlan_vtable,
45 [NETDEV_KIND_MACVTAP] = &macvtap_vtable,
46 [NETDEV_KIND_IPVLAN] = &ipvlan_vtable,
47 [NETDEV_KIND_VXLAN] = &vxlan_vtable,
48 [NETDEV_KIND_IPIP] = &ipip_vtable,
49 [NETDEV_KIND_GRE] = &gre_vtable,
50 [NETDEV_KIND_GRETAP] = &gretap_vtable,
51 [NETDEV_KIND_IP6GRE] = &ip6gre_vtable,
52 [NETDEV_KIND_IP6GRETAP] = &ip6gretap_vtable,
53 [NETDEV_KIND_SIT] = &sit_vtable,
54 [NETDEV_KIND_VTI] = &vti_vtable,
55 [NETDEV_KIND_VTI6] = &vti6_vtable,
56 [NETDEV_KIND_VETH] = &veth_vtable,
57 [NETDEV_KIND_DUMMY] = &dummy_vtable,
58 [NETDEV_KIND_TUN] = &tun_vtable,
59 [NETDEV_KIND_TAP] = &tap_vtable,
60 [NETDEV_KIND_IP6TNL] = &ip6tnl_vtable,
61 [NETDEV_KIND_VRF] = &vrf_vtable,
62 [NETDEV_KIND_VCAN] = &vcan_vtable,
63 [NETDEV_KIND_GENEVE] = &geneve_vtable,
64 [NETDEV_KIND_VXCAN] = &vxcan_vtable,
65 [NETDEV_KIND_WIREGUARD] = &wireguard_vtable,
66 [NETDEV_KIND_NETDEVSIM] = &netdevsim_vtable,
67 [NETDEV_KIND_FOU] = &foutnl_vtable,
68 [NETDEV_KIND_ERSPAN] = &erspan_vtable,
69 [NETDEV_KIND_L2TP] = &l2tptnl_vtable,
70 [NETDEV_KIND_MACSEC] = &macsec_vtable,
71 };
72
73 static const char* const netdev_kind_table[_NETDEV_KIND_MAX] = {
74 [NETDEV_KIND_BRIDGE] = "bridge",
75 [NETDEV_KIND_BOND] = "bond",
76 [NETDEV_KIND_VLAN] = "vlan",
77 [NETDEV_KIND_MACVLAN] = "macvlan",
78 [NETDEV_KIND_MACVTAP] = "macvtap",
79 [NETDEV_KIND_IPVLAN] = "ipvlan",
80 [NETDEV_KIND_VXLAN] = "vxlan",
81 [NETDEV_KIND_IPIP] = "ipip",
82 [NETDEV_KIND_GRE] = "gre",
83 [NETDEV_KIND_GRETAP] = "gretap",
84 [NETDEV_KIND_IP6GRE] = "ip6gre",
85 [NETDEV_KIND_IP6GRETAP] = "ip6gretap",
86 [NETDEV_KIND_SIT] = "sit",
87 [NETDEV_KIND_VETH] = "veth",
88 [NETDEV_KIND_VTI] = "vti",
89 [NETDEV_KIND_VTI6] = "vti6",
90 [NETDEV_KIND_DUMMY] = "dummy",
91 [NETDEV_KIND_TUN] = "tun",
92 [NETDEV_KIND_TAP] = "tap",
93 [NETDEV_KIND_IP6TNL] = "ip6tnl",
94 [NETDEV_KIND_VRF] = "vrf",
95 [NETDEV_KIND_VCAN] = "vcan",
96 [NETDEV_KIND_GENEVE] = "geneve",
97 [NETDEV_KIND_VXCAN] = "vxcan",
98 [NETDEV_KIND_WIREGUARD] = "wireguard",
99 [NETDEV_KIND_NETDEVSIM] = "netdevsim",
100 [NETDEV_KIND_FOU] = "fou",
101 [NETDEV_KIND_ERSPAN] = "erspan",
102 [NETDEV_KIND_L2TP] = "l2tp",
103 [NETDEV_KIND_MACSEC] = "macsec",
104 };
105
106 DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
107
108 int config_parse_netdev_kind(
109 const char *unit,
110 const char *filename,
111 unsigned line,
112 const char *section,
113 unsigned section_line,
114 const char *lvalue,
115 int ltype,
116 const char *rvalue,
117 void *data,
118 void *userdata) {
119
120 NetDevKind k, *kind = data;
121
122 assert(rvalue);
123 assert(data);
124
125 k = netdev_kind_from_string(rvalue);
126 if (k < 0) {
127 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse netdev kind, ignoring assignment: %s", rvalue);
128 return 0;
129 }
130
131 if (*kind != _NETDEV_KIND_INVALID && *kind != k) {
132 log_syntax(unit, LOG_ERR, filename, line, 0,
133 "Specified netdev kind is different from the previous value '%s', ignoring assignment: %s",
134 netdev_kind_to_string(*kind), rvalue);
135 return 0;
136 }
137
138 *kind = k;
139
140 return 0;
141 }
142
143 static void netdev_callbacks_clear(NetDev *netdev) {
144 netdev_join_callback *callback;
145
146 if (!netdev)
147 return;
148
149 while ((callback = netdev->callbacks)) {
150 LIST_REMOVE(callbacks, netdev->callbacks, callback);
151 link_unref(callback->link);
152 free(callback);
153 }
154 }
155
156 bool netdev_is_managed(NetDev *netdev) {
157 if (!netdev || !netdev->manager || !netdev->ifname)
158 return false;
159
160 return hashmap_get(netdev->manager->netdevs, netdev->ifname) == netdev;
161 }
162
163 static void netdev_detach_from_manager(NetDev *netdev) {
164 if (netdev->ifname && netdev->manager)
165 hashmap_remove(netdev->manager->netdevs, netdev->ifname);
166 }
167
168 static NetDev *netdev_free(NetDev *netdev) {
169 assert(netdev);
170
171 netdev_callbacks_clear(netdev);
172
173 netdev_detach_from_manager(netdev);
174
175 free(netdev->filename);
176
177 free(netdev->description);
178 free(netdev->ifname);
179 free(netdev->mac);
180 condition_free_list(netdev->conditions);
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 static int netdev_create_after_configured(NetDev *netdev, Link *link) {
602 assert(netdev);
603 assert(link);
604 assert(NETDEV_VTABLE(netdev)->create_after_configured);
605
606 return NETDEV_VTABLE(netdev)->create_after_configured(netdev, link);
607 }
608
609 /* the callback must be called, possibly after a timeout, as otherwise the Link will hang */
610 int netdev_join(NetDev *netdev, Link *link, link_netlink_message_handler_t callback) {
611 int r;
612
613 assert(netdev);
614 assert(netdev->manager);
615 assert(netdev->manager->rtnl);
616
617 switch (netdev_get_create_type(netdev)) {
618 case NETDEV_CREATE_MASTER:
619 r = netdev_enslave(netdev, link, callback);
620 if (r < 0)
621 return r;
622
623 break;
624 case NETDEV_CREATE_STACKED:
625 r = netdev_create(netdev, link, callback);
626 if (r < 0)
627 return r;
628
629 break;
630 case NETDEV_CREATE_AFTER_CONFIGURED:
631 r = netdev_create_after_configured(netdev, link);
632 if (r < 0)
633 return r;
634 break;
635 default:
636 assert_not_reached("Can not join independent netdev");
637 }
638
639 return 0;
640 }
641
642 int netdev_load_one(Manager *manager, const char *filename) {
643 _cleanup_(netdev_unrefp) NetDev *netdev_raw = NULL, *netdev = NULL;
644 _cleanup_fclose_ FILE *file = NULL;
645 const char *dropin_dirname;
646 bool independent = false;
647 int r;
648
649 assert(manager);
650 assert(filename);
651
652 file = fopen(filename, "re");
653 if (!file) {
654 if (errno == ENOENT)
655 return 0;
656
657 return -errno;
658 }
659
660 if (null_or_empty_fd(fileno(file))) {
661 log_debug("Skipping empty file: %s", filename);
662 return 0;
663 }
664
665 netdev_raw = new(NetDev, 1);
666 if (!netdev_raw)
667 return log_oom();
668
669 *netdev_raw = (NetDev) {
670 .n_ref = 1,
671 .kind = _NETDEV_KIND_INVALID,
672 .state = _NETDEV_STATE_INVALID, /* an invalid state means done() of the implementation won't be called on destruction */
673 };
674
675 dropin_dirname = strjoina(basename(filename), ".d");
676 r = config_parse_many(filename, NETWORK_DIRS, dropin_dirname,
677 "Match\0NetDev\0",
678 config_item_perf_lookup, network_netdev_gperf_lookup,
679 CONFIG_PARSE_WARN|CONFIG_PARSE_RELAXED, netdev_raw);
680 if (r < 0)
681 return r;
682
683 /* skip out early if configuration does not match the environment */
684 if (!condition_test_list(netdev_raw->conditions, NULL, NULL, NULL)) {
685 log_debug("%s: Conditions in the file do not match the system environment, skipping.", filename);
686 return 0;
687 }
688
689 if (netdev_raw->kind == _NETDEV_KIND_INVALID) {
690 log_warning("NetDev has no Kind= configured in %s. Ignoring", filename);
691 return 0;
692 }
693
694 if (!netdev_raw->ifname) {
695 log_warning("NetDev without Name= configured in %s. Ignoring", filename);
696 return 0;
697 }
698
699 r = fseek(file, 0, SEEK_SET);
700 if (r < 0)
701 return -errno;
702
703 netdev = malloc0(NETDEV_VTABLE(netdev_raw)->object_size);
704 if (!netdev)
705 return log_oom();
706
707 netdev->n_ref = 1;
708 netdev->manager = manager;
709 netdev->kind = netdev_raw->kind;
710 netdev->state = NETDEV_STATE_LOADING; /* we initialize the state here for the first time,
711 so that done() will be called on destruction */
712
713 if (NETDEV_VTABLE(netdev)->init)
714 NETDEV_VTABLE(netdev)->init(netdev);
715
716 r = config_parse_many(filename, NETWORK_DIRS, dropin_dirname,
717 NETDEV_VTABLE(netdev)->sections,
718 config_item_perf_lookup, network_netdev_gperf_lookup,
719 CONFIG_PARSE_WARN, netdev);
720 if (r < 0)
721 return r;
722
723 /* verify configuration */
724 if (NETDEV_VTABLE(netdev)->config_verify) {
725 r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename);
726 if (r < 0)
727 return 0;
728 }
729
730 netdev->filename = strdup(filename);
731 if (!netdev->filename)
732 return log_oom();
733
734 if (!netdev->mac && netdev->kind != NETDEV_KIND_VLAN) {
735 r = netdev_get_mac(netdev->ifname, &netdev->mac);
736 if (r < 0)
737 return log_netdev_error_errno(netdev, r,
738 "Failed to generate predictable MAC address for %s: %m",
739 netdev->ifname);
740 }
741
742 r = hashmap_ensure_allocated(&netdev->manager->netdevs, &string_hash_ops);
743 if (r < 0)
744 return r;
745
746 r = hashmap_put(netdev->manager->netdevs, netdev->ifname, netdev);
747 if (r == -EEXIST) {
748 NetDev *n = hashmap_get(netdev->manager->netdevs, netdev->ifname);
749
750 assert(n);
751 log_netdev_warning_errno(netdev, r,
752 "The setting Name=%s in %s conflicts with the one in %s, ignoring",
753 netdev->ifname, netdev->filename, n->filename);
754
755 /* Clear ifname before netdev_free() is called. Otherwise, the NetDev object 'n' is
756 * removed from the hashmap 'manager->netdevs'. */
757 netdev->ifname = mfree(netdev->ifname);
758 return 0;
759 }
760 if (r < 0)
761 return r;
762
763 LIST_HEAD_INIT(netdev->callbacks);
764
765 log_netdev_debug(netdev, "loaded %s", netdev_kind_to_string(netdev->kind));
766
767 if (IN_SET(netdev_get_create_type(netdev), NETDEV_CREATE_MASTER, NETDEV_CREATE_INDEPENDENT)) {
768 r = netdev_create(netdev, NULL, NULL);
769 if (r < 0)
770 return r;
771 }
772
773 switch (netdev->kind) {
774 case NETDEV_KIND_IPIP:
775 independent = IPIP(netdev)->independent;
776 break;
777 case NETDEV_KIND_GRE:
778 independent = GRE(netdev)->independent;
779 break;
780 case NETDEV_KIND_GRETAP:
781 independent = GRETAP(netdev)->independent;
782 break;
783 case NETDEV_KIND_IP6GRE:
784 independent = IP6GRE(netdev)->independent;
785 break;
786 case NETDEV_KIND_IP6GRETAP:
787 independent = IP6GRETAP(netdev)->independent;
788 break;
789 case NETDEV_KIND_SIT:
790 independent = SIT(netdev)->independent;
791 break;
792 case NETDEV_KIND_VTI:
793 independent = VTI(netdev)->independent;
794 break;
795 case NETDEV_KIND_VTI6:
796 independent = VTI6(netdev)->independent;
797 break;
798 case NETDEV_KIND_IP6TNL:
799 independent = IP6TNL(netdev)->independent;
800 break;
801 case NETDEV_KIND_ERSPAN:
802 independent = ERSPAN(netdev)->independent;
803 break;
804 default:
805 break;
806 }
807
808 if (independent) {
809 r = netdev_create(netdev, NULL, NULL);
810 if (r < 0)
811 return r;
812 }
813
814 netdev = NULL;
815
816 return 0;
817 }
818
819 int netdev_load(Manager *manager) {
820 _cleanup_strv_free_ char **files = NULL;
821 char **f;
822 int r;
823
824 assert(manager);
825
826 hashmap_clear_with_destructor(manager->netdevs, netdev_unref);
827
828 r = conf_files_list_strv(&files, ".netdev", NULL, 0, NETWORK_DIRS);
829 if (r < 0)
830 return log_error_errno(r, "Failed to enumerate netdev files: %m");
831
832 STRV_FOREACH(f, files) {
833 r = netdev_load_one(manager, *f);
834 if (r < 0)
835 return r;
836 }
837
838 return 0;
839 }