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