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