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