From nobody Mon Feb 20 14:34:07 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PL4g41FsQz3st5g; Mon, 20 Feb 2023 14:34:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PL4g40XmGz3vX2; Mon, 20 Feb 2023 14:34:08 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1676903648; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HoGQ6YLHLJU5kc4Bns/J7Lq9FxMMkVFcK+h1r285nlE=; b=klQ8XrJ4MiUA2votauypW0DxGoeRTdpX8q5wRb9Aume7u/hHf6F7ws4oLs6oloMZslRv4U yjpySXGSYTceZlyr8bFhvAbMaFn5m0ftTow7MUX9yQ9LSfcLxO4DQXAJSxsbLKt0KKChNT fdQ1rGaN9jPv3kXaaK+MuQRhHhKLztL7eSnrilx7OcdmwzAIgUjzr0k8MNZYd31oLeAtKJ ynooBfMZKzdt5T3KlJGM0lmFFx/SdFXaFB6vgv1kjbNHfgrsSMM9ZHx6SHCWyMXFmPEnKQ vTl0H7YSGuTumdYFtnRV5aqsItkMfuhWGSXdFksiOAH/2B5BsDzgyxhL7Dpa1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1676903648; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HoGQ6YLHLJU5kc4Bns/J7Lq9FxMMkVFcK+h1r285nlE=; b=EjpmEZIPwO3HCKNqUgoHFHhRsPJuPF9Z4N3fE89Fn4M51vFHIDhCU6nl/JYQaXbg9PPpVV TzoYVazekMDI6yYnkdmN0HLCMjwiHQmiJ8on+wVWWu2VUALz2uyQuS3ZMtIvPY8eAL1dRm bPy+U/g8BAaBxlH1/G/Fq7EZVMNzjrTX1PEDrVt9FFXtIUsEkurQZ20wGemS5wCGkqa8S2 e5WbNnQ3XSL8ip5ggqu2JDnaUN1Yh3UUUQc9iA2PCY2Y/EDVuAuIL6CKcVGGFE1ldehoAC 6VN4HXEDrcNHBKjTjD/lJxbH4y8GuT+ncDckMaoEbiiOf0epyGj/LGxZYCD+eQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1676903648; a=rsa-sha256; cv=none; b=mU++KmyL7Es2M8ORv2bN3qA8ILezpCgoDSPeirpRUGqQxTkncYQVMlOL805YPndKa9MKoa WTWxdsir+4L4rEceyOHwpWiVrRO7HMj6vRVE+lS00PSViTC6lfTPuBlJRFjtObX7Pf6zNr qVnVlDuivEIhCe+uE8WRtqfvn2a/71JeDkl6HMIUPg4BDfADaJp/AMJYgWOPorQtgqTQip obm1PbDi62ZcxpkPFWjBJzNszPhUnAFGRQmt6yZsNPfgYM6ddfb2Db5YCBMwMgThudf0zz /KTts0HvyWD6BFwBP1rXE+/JQsVaOrcdcbjrFuM7E8Bf9MXzVQ+PArDhvXueaA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4PL4g36fWWzl8f; Mon, 20 Feb 2023 14:34:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 31KEY7iF005773; Mon, 20 Feb 2023 14:34:07 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 31KEY7Cc005772; Mon, 20 Feb 2023 14:34:07 GMT (envelope-from git) Date: Mon, 20 Feb 2023 14:34:07 GMT Message-Id: <202302201434.31KEY7Cc005772@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: a800622765b1 - stable/13 - netlink: fix interface dump. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: a800622765b1e7cab79fa857e9a671e6c0c1ccec Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=a800622765b1e7cab79fa857e9a671e6c0c1ccec commit a800622765b1e7cab79fa857e9a671e6c0c1ccec Author: Alexander V. Chernikov AuthorDate: 2023-02-16 13:17:58 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-02-20 14:33:28 +0000 netlink: fix interface dump. The current code missed interface addition when reallocating temporary buffer. Tweak the code to perform the reallocation first and add interface afterwards unconditionally. Reported by: Marek Zarychta MFC after: 3 days (cherry picked from commit 86fd0bdba540132ae298457e160b651f61d1db6b) --- sys/netlink/route/iface.c | 33 ++++++++++--------- tests/sys/netlink/test_rtnl_iface.py | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/sys/netlink/route/iface.c b/sys/netlink/route/iface.c index 9cd7e6e80f3c..0eafacff4775 100644 --- a/sys/netlink/route/iface.c +++ b/sys/netlink/route/iface.c @@ -449,24 +449,23 @@ rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *n CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { wa.count++; if (match_iface(&attrs, ifp)) { - if (offset < base_count) { - if (!if_try_ref(ifp)) - continue; - match_array[offset++] = ifp; - continue; - } - /* Too many matches, need to reallocate */ - struct ifnet **new_array; - int sz = base_count * sizeof(void *); - base_count *= 2; - new_array = malloc(sz * 2, M_TEMP, M_NOWAIT); - if (new_array == NULL) { - error = ENOMEM; - break; + if (offset >= base_count) { + /* Too many matches, need to reallocate */ + struct ifnet **new_array; + int sz = base_count * sizeof(void *); + base_count *= 2; + new_array = malloc(sz * 2, M_TEMP, M_NOWAIT); + if (new_array == NULL) { + error = ENOMEM; + break; + } + memcpy(new_array, match_array, sz); + free(match_array, M_TEMP); + match_array = new_array; } - memcpy(new_array, match_array, sz); - free(match_array, M_TEMP); - match_array = new_array; + + if (if_try_ref(ifp)) + match_array[offset++] = ifp; } } NET_EPOCH_EXIT(et); diff --git a/tests/sys/netlink/test_rtnl_iface.py b/tests/sys/netlink/test_rtnl_iface.py index ecc932c6213e..cbe5aed8b8ae 100644 --- a/tests/sys/netlink/test_rtnl_iface.py +++ b/tests/sys/netlink/test_rtnl_iface.py @@ -242,6 +242,67 @@ class TestRtNlIface(NetlinkTestTemplate, SingleVnetTestTemplate): assert rx_msg.is_type(NlMsgType.NLMSG_ERROR) assert rx_msg.error_code == errno.ENODEV + @pytest.mark.require_user("root") + def test_dump_ifaces_many(self): + """Tests if interface dummp is not missing interfaces""" + + ifmap = {} + for ifname in (self.vnet.iface_alias_map["if1"].name, "lo0"): + ifindex = socket.if_nametoindex(ifname) + ifmap[ifindex] = ifname + + for i in range(40): + ifname = "lo{}".format(i + 1) + flags = NlmNewFlags.NLM_F_EXCL.value | NlmNewFlags.NLM_F_CREATE.value + msg = NetlinkIflaMessage(self.helper, NlRtMsgType.RTM_NEWLINK.value) + msg.nl_hdr.nlmsg_flags = ( + flags | NlmBaseFlags.NLM_F_ACK.value | NlmBaseFlags.NLM_F_REQUEST.value + ) + msg.add_nla(NlAttrStr(IflattrType.IFLA_IFNAME, ifname)) + msg.add_nla( + NlAttrNested( + IflattrType.IFLA_LINKINFO, + [ + NlAttrStrn(IflinkInfo.IFLA_INFO_KIND, "lo"), + ], + ) + ) + + rx_msg = self.get_reply(msg) + assert rx_msg.is_type(NlMsgType.NLMSG_ERROR) + nla_list, _ = rx_msg.parse_attrs(bytes(rx_msg.cookie)[4:], rtnl_ifla_attrs) + nla_map = {n.nla_type: n for n in nla_list} + assert nla_map[IflattrType.IFLA_IFNAME.value].text == ifname + ifindex = nla_map[IflattrType.IFLA_NEW_IFINDEX.value].u32 + assert ifindex > 0 + assert ifindex not in ifmap + ifmap[ifindex] = ifname + + # Dump all interfaces and check if the output matches ifmap + kernel_ifmap = {} + msg = NetlinkIflaMessage(self.helper, NlRtMsgType.RTM_GETLINK.value) + msg.nl_hdr.nlmsg_flags = ( + NlmBaseFlags.NLM_F_ACK.value | NlmBaseFlags.NLM_F_REQUEST.value + ) + self.write_message(msg) + while True: + rx_msg = self.read_message() + if msg.nl_hdr.nlmsg_seq != rx_msg.nl_hdr.nlmsg_seq: + raise ValueError( + "unexpected seq {}".format(rx_msg.nl_hdr.nlmsg_seq) + ) + if rx_msg.is_type(NlMsgType.NLMSG_ERROR): + raise ValueError("unexpected message {}".format(rx_msg)) + if rx_msg.is_type(NlMsgType.NLMSG_DONE): + break + if not rx_msg.is_type(NlRtMsgType.RTM_NEWLINK): + raise ValueError("unexpected message {}".format(rx_msg)) + + ifindex = rx_msg.base_hdr.ifi_index + assert ifindex == rx_msg.base_hdr.ifi_index + kernel_ifmap[ifindex] = rx_msg.get_nla(IflattrType.IFLA_IFNAME).text + assert kernel_ifmap == ifmap + # # * # * {len=76, type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1662892737, pid=0},