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