From 4fa83282b56e98ca7021f31c62928fc51acf18a2 Mon Sep 17 00:00:00 2001 From: Alexander Mikhalitsyn Date: Fri, 6 Jan 2023 16:20:02 +0100 Subject: [PATCH] lxc_user_nic: fix get_mtu() error handling get_mtu() returns int, but "mtu" variable has unsigned int type. It leads to logical error in error handling, which can end up with strange -EINVAL error in lxc_veth_create(), cause (mtu > 0) condition is met, but negative "mtu" value is too large when set as mtu for network device. Issue #4232 Signed-off-by: Alexander Mikhalitsyn --- src/lxc/cmd/lxc_user_nic.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/lxc/cmd/lxc_user_nic.c b/src/lxc/cmd/lxc_user_nic.c index a91e2259d..006896750 100644 --- a/src/lxc/cmd/lxc_user_nic.c +++ b/src/lxc/cmd/lxc_user_nic.c @@ -489,20 +489,30 @@ static int instantiate_veth(char *veth1, char *veth2, pid_t pid, unsigned int mt return netdev_set_flag(veth1, IFF_UP); } -static int get_mtu(char *name) +#define NETDEV_MTU_DEFAULT 1500 + +static unsigned int get_mtu(char *name) { - int idx; + int idx, val; idx = if_nametoindex(name); - if (idx < 0) - return -1; + if (idx < 0) { + usernic_error("Could not find netdev %s\n", name); + return NETDEV_MTU_DEFAULT; + } + + val = netdev_get_mtu(idx); + if (val < 0) { + usernic_error("Could not get MTU for netdev %s ifindex %d\n", name, idx); + return NETDEV_MTU_DEFAULT; + } - return netdev_get_mtu(idx); + return val; } static int create_nic(char *nic, char *br, int pid, char **cnic) { - unsigned int mtu = 1500; + unsigned int mtu = NETDEV_MTU_DEFAULT; int ret; char veth1buf[IFNAMSIZ], veth2buf[IFNAMSIZ]; @@ -520,8 +530,6 @@ static int create_nic(char *nic, char *br, int pid, char **cnic) if (strcmp(br, "none")) mtu = get_mtu(br); - if (!mtu) - mtu = 1500; /* create the nics */ ret = instantiate_veth(veth1buf, veth2buf, pid, mtu); -- 2.47.2