From 29b559a3005c35643bc5bb2101e07f4bd0a570a6 Mon Sep 17 00:00:00 2001 From: Fernando Fernandez Mancera Date: Mon, 6 Mar 2023 17:57:16 +0100 Subject: [PATCH] MACVLAN: better error handling for unrealized parent interface When creating a MACVLAN device it can have an unrealized parent. In that case, the parent won't have an ifindex and it will cause a crash due to an assert on parent being missing. In order to avoid this, we are handling the situation similar to what we do for VLAN devices. NetworkManager will raise different error and block the autoconnection instead of crashing. This solves the crash for the following commands: ``` nmcli connection add type macvlan ifname mv1 con-name mv1+ macvlan.parent dummy0 mode vepa nmcli connection add type dummy ifname dummy0 con-name dummy0+ autoconnect no ``` --- src/core/devices/nm-device-macvlan.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/core/devices/nm-device-macvlan.c b/src/core/devices/nm-device-macvlan.c index 3f57bfb1fc..b0ee62d0ea 100644 --- a/src/core/devices/nm-device-macvlan.c +++ b/src/core/devices/nm-device-macvlan.c @@ -208,14 +208,23 @@ create_and_realize(NMDevice *device, s_macvlan = nm_connection_get_setting_macvlan(connection); g_return_val_if_fail(s_macvlan, FALSE); - parent_ifindex = parent ? nm_device_get_ifindex(parent) : 0; + if (!parent) { + g_set_error(error, + NM_DEVICE_ERROR, + NM_DEVICE_ERROR_MISSING_DEPENDENCIES, + "MACVLAN device can not be created without a parent interface"); + return FALSE; + } + + parent_ifindex = nm_device_get_ifindex(parent); if (parent_ifindex <= 0) { g_set_error(error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_MISSING_DEPENDENCIES, - "MACVLAN devices can not be created without a parent interface"); - g_return_val_if_fail(!parent, FALSE); + "cannot retrieve ifindex of interface %s (%s)", + nm_device_get_iface(parent), + nm_device_get_type_desc(parent)); return FALSE; }