From patchwork Sat Jan 23 04:53:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Edwin Peer X-Patchwork-Id: 12041097 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA10DC433DB for ; Sat, 23 Jan 2021 04:54:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7EEEF23AA1 for ; Sat, 23 Jan 2021 04:54:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726694AbhAWEyT (ORCPT ); Fri, 22 Jan 2021 23:54:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726426AbhAWEyK (ORCPT ); Fri, 22 Jan 2021 23:54:10 -0500 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95EABC0613D6 for ; Fri, 22 Jan 2021 20:53:30 -0800 (PST) Received: by mail-pf1-x429.google.com with SMTP id y205so5172694pfc.5 for ; Fri, 22 Jan 2021 20:53:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version; bh=h7VPruB668G7g5/SHWgIuv8gpm2uLDqUmzfEvcvvW90=; b=iG69MaHjvgRumTR94d6RYcAhIJ6aQV+bMIO2wNQAw9J7AOdXwvyKEVbtrrLT87O7zm GNSXlguoaF4IsPVCpGcn/2JWN5Yf7UZbednoHt7ego4nwLXR05jIw2DG3intLMTpk50P ZPF5J+cCDkdIDMT6pnn6zhqng02CAf6+o+Nao= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version; bh=h7VPruB668G7g5/SHWgIuv8gpm2uLDqUmzfEvcvvW90=; b=YjR/ZxgWxtDrft1Q1rECkDdgH8I7KdaS3GL5RWmuu6oNO5agbsxx6RQ/ACTFaPdddM i1MAd/U2FBEO5lrOvn71st/AoejbyRUFZlCmLnoLz4yg0uYG9jvpViIC+RqDQSGnGsu1 Gu6SozHTmFVc9CnLF29uEXpsH9eI239EPHAGi4JVfjUB/wzx0+5M3GlyRrglkK/sAsEi aBUkddF1g/p1bF/zwsKnT1/Rr1hvgdQBTK789vNWD4sqHyvKzXSNy/afulss9nbFBYIP mSx2T3Yzjmk/ieFSK0oAJb58WwnaYSjBz+2ukwW/vlJQW++goY46Pu6mWkAnPz66o662 RJ+w== X-Gm-Message-State: AOAM530iey/fW/aZZ4xxF8ugKys10sSrJ1T6taDfJkqVi8jFhH66wfDN 7OjMkyXEj2v9vi6UvARlUlLCxtsbHN2/IvuUWFY4FcQnz4Bl4ACL0e+IORp99/6H4r3FJnvu7US FXegcwYrsXvJbEa59L3NC7RWSGMSwqmjY00c45gXxPjpQ5izckFwxL1gfZvbmeOSY0rDn1vbx X-Google-Smtp-Source: ABdhPJw+4Yh1sHiUlRpUnv16S72bwaklF4sSmvr7Q90atuyIeCdGhyMj/4gL50mk//zzuJuE6leAjw== X-Received: by 2002:a62:e30e:0:b029:1b9:3823:4b3a with SMTP id g14-20020a62e30e0000b02901b938234b3amr1994914pfh.15.1611377609441; Fri, 22 Jan 2021 20:53:29 -0800 (PST) Received: from hex.swdvt.lab.broadcom.net ([2600:8802:d04:de02::77c]) by smtp.gmail.com with ESMTPSA id d2sm10725832pjd.29.2021.01.22.20.53.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jan 2021 20:53:28 -0800 (PST) From: Edwin Peer To: netdev@vger.kernel.org Cc: Edwin Peer , Jakub Kicinski , Andrew Gospodarek , Michael Chan , Stephen Hemminger , Michal Kubecek , David Ahern Subject: [PATCH net-next 1/4] netlink: truncate overlength attribute list in nla_nest_end() Date: Fri, 22 Jan 2021 20:53:18 -0800 Message-Id: <20210123045321.2797360-2-edwin.peer@broadcom.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210123045321.2797360-1-edwin.peer@broadcom.com> References: <20210123045321.2797360-1-edwin.peer@broadcom.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org If a nested list of attributes is too long, then the length will exceed the 16-bit nla_len of the parent nlattr. In such cases, determine how many whole attributes can fit and truncate the message to this length. This properly maintains the nesting hierarchy, keeping the entire message valid, while fitting more subelements inside the nest range than may result if the length is wrapped modulo 64KB. Marking truncated attributes, such that user space can determine the precise attribute truncated, by means of an additional bit in the nla_type was considered and rejected. The NLA_F_NESTED and NLA_F_NET_BYTEORDER flags are supposed to be mutually exclusive. So, in theory, the latter bit could have been redefined for nested attributes in order to indicate truncation, but user space tools (most notably iproute2) cannot be relied on to honor NLA_TYPE_MASK, resulting in alteration of the perceived nla_type and subsequent catastrophic failure. Failing the entire message with a hard error must also be rejected, as this would break existing user space functionality. The trigger issue is evident for IFLA_VFINFO_LIST and a hard error here would cause iproute2 to fail to render an entire interface list even if only a single interface warranted a truncated VF list. Instead, set NLM_F_NEST_TRUNCATED in the netlink header to inform user space about the incomplete data. In this particular case, however, user space can better ascertain which instance is truncated by consulting the associated IFLA_NUM_VF to determine how many VFs were expected. Fixes: bfa83a9e03cf ("[NETLINK]: Type-safe netlink messages/attributes interface") Signed-off-by: Edwin Peer --- include/net/netlink.h | 11 +++++++++-- include/uapi/linux/netlink.h | 1 + lib/nlattr.c | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/net/netlink.h b/include/net/netlink.h index 1ceec518ab49..fc8c57dafb05 100644 --- a/include/net/netlink.h +++ b/include/net/netlink.h @@ -1785,19 +1785,26 @@ static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED); } +int __nla_nest_trunc_msg(struct sk_buff *skb, const struct nlattr *start); + /** * nla_nest_end - Finalize nesting of attributes * @skb: socket buffer the attributes are stored in * @start: container attribute * * Corrects the container attribute header to include the all - * appeneded attributes. + * appeneded attributes. The list of attributes will be truncated + * if too long to fit within the parent attribute's maximum reach. * * Returns the total data length of the skb. */ static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) { - start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start; + int len = skb_tail_pointer(skb) - (unsigned char *)start; + + if (len > 0xffff) + len = __nla_nest_trunc_msg(skb, start); + start->nla_len = len; return skb->len; } diff --git a/include/uapi/linux/netlink.h b/include/uapi/linux/netlink.h index 3d94269bbfa8..44a250825c30 100644 --- a/include/uapi/linux/netlink.h +++ b/include/uapi/linux/netlink.h @@ -57,6 +57,7 @@ struct nlmsghdr { #define NLM_F_ECHO 0x08 /* Echo this request */ #define NLM_F_DUMP_INTR 0x10 /* Dump was inconsistent due to sequence change */ #define NLM_F_DUMP_FILTERED 0x20 /* Dump was filtered as requested */ +#define NLM_F_NEST_TRUNCATED 0x40 /* Message contains truncated nested attribute */ /* Modifiers to GET request */ #define NLM_F_ROOT 0x100 /* specify tree root */ diff --git a/lib/nlattr.c b/lib/nlattr.c index 5b6116e81f9f..2a267c0d3e16 100644 --- a/lib/nlattr.c +++ b/lib/nlattr.c @@ -1119,4 +1119,31 @@ int nla_append(struct sk_buff *skb, int attrlen, const void *data) return 0; } EXPORT_SYMBOL(nla_append); + +/** + * __nla_nest_trunc_msg - Truncate list of nested netlink attributes to max len + * @skb: socket buffer with tail pointer positioned after end of nested list + * @start: container attribute designating the beginning of the list + * + * Trims the skb to fit only the attributes which are within the range of the + * containing nest attribute. This is a helper for nla_nest_end, to prevent + * adding unduly to the length of what is an inline function. It is not + * intended to be called from anywhere else. + * + * Returns the truncated length of the enclosing nest attribute in accordance + * with the number of whole attributes that can fit. + */ +int __nla_nest_trunc_msg(struct sk_buff *skb, const struct nlattr *start) +{ + struct nlattr *attr = nla_data(start); + int rem = 0xffff - NLA_HDRLEN; + + while (nla_ok(attr, rem)) + attr = nla_next(attr, &rem); + nlmsg_trim(skb, attr); + nlmsg_hdr(skb)->nlmsg_flags |= NLM_F_NEST_TRUNCATED; + return (unsigned char *)attr - (unsigned char *)start; +} +EXPORT_SYMBOL(__nla_nest_trunc_msg); + #endif From patchwork Sat Jan 23 04:53:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Edwin Peer X-Patchwork-Id: 12041101 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B60BFC433E0 for ; Sat, 23 Jan 2021 04:54:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7A2B323AA1 for ; Sat, 23 Jan 2021 04:54:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726708AbhAWEyW (ORCPT ); Fri, 22 Jan 2021 23:54:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726597AbhAWEyM (ORCPT ); Fri, 22 Jan 2021 23:54:12 -0500 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86FAEC061786 for ; Fri, 22 Jan 2021 20:53:32 -0800 (PST) Received: by mail-pg1-x52e.google.com with SMTP id c22so5202152pgg.13 for ; Fri, 22 Jan 2021 20:53:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version; bh=7zXTc6CMeL7KIeVi/3C+r0OPMcrMBNOUicc8iFisY5w=; b=GdKrTFCo4+KGfmDsGYmi/Oc27yXNB/5JnXtfGWh8r2jRJ7FfINXYvL3ewxHp9dSMTQ 99N9L2AnDMHyfO6gLutZPUTG09kduFf6N3Dtrh6ADUFsqyPT5buk+QUmdsHw23BKlilO azbEUbeXN8J4H3Kx5vwazV3A7MPcyro1OTUYg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version; bh=7zXTc6CMeL7KIeVi/3C+r0OPMcrMBNOUicc8iFisY5w=; b=PYFgx1+yAeU5YlJBsSrvxHW7PFHm3LpwlWsLWdjvOOyyxHrH/K9gR1bv/qBqrY+rEK TE8XfyTPCZ0B+YDlH7mQfsbhAsHWNN/hnHF6z3SAar6jv4jzH/qo1Z3PDBBexdo2RSr2 yZQRazQAzTm9ybSy+SbUdiTgQ7W3amsoUCy1BmYDINayq0nfoc5IiNzWnfHlh5pKciPs SJ9TL9qHgXth3b73DiQOHDFWcPTWBEQi61QuwxDIilPUQFQzZiJvs+xga4x9aphQjtdQ dg4BiPheUvJuJBOp/hTh4ohOODR8uyIpLrl05kRJ01jMMb/aC6zFfuv+y816BgatClU0 McIw== X-Gm-Message-State: AOAM531unKAEP7O27sTTXNJ/K5BrRV9wX0hjIfcafGHmOEzFEojf7EVN sz0p9tSK9wdkHiSwKsxdRsxqF0PMbkbCtcVZZ8m6Y68kkMmxp/H7Ca3hCbdap8kkI3RJAlSwDGY kY3ZbnKlnxGGvUbPxWik2da9jvJQ4xqJlsxPOPE3DxWEdNcdSge43Nnta1U2oZ6t0nqGGWG3u X-Google-Smtp-Source: ABdhPJylECeeQBAJlxJdJt2uBRWFTe2EmrO+hE1iLScg/A264G/gZM03ZDfZne6bIbrU2nCEza6OAg== X-Received: by 2002:a62:32c5:0:b029:1b6:7586:f718 with SMTP id y188-20020a6232c50000b02901b67586f718mr8243373pfy.74.1611377611327; Fri, 22 Jan 2021 20:53:31 -0800 (PST) Received: from hex.swdvt.lab.broadcom.net ([2600:8802:d04:de02::77c]) by smtp.gmail.com with ESMTPSA id d2sm10725832pjd.29.2021.01.22.20.53.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jan 2021 20:53:30 -0800 (PST) From: Edwin Peer To: netdev@vger.kernel.org Cc: Edwin Peer , Jakub Kicinski , Andrew Gospodarek , Michael Chan , Stephen Hemminger , Michal Kubecek , David Ahern Subject: [PATCH net-next 2/4] rtnetlink: extend RTEXT_FILTER_SKIP_STATS to IFLA_VF_INFO Date: Fri, 22 Jan 2021 20:53:19 -0800 Message-Id: <20210123045321.2797360-3-edwin.peer@broadcom.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210123045321.2797360-1-edwin.peer@broadcom.com> References: <20210123045321.2797360-1-edwin.peer@broadcom.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org This filter already exists for excluding IPv6 SNMP stats. Extend its definition to also exclude IFLA_VF_INFO stats in RTM_GETLINK. This patch constitutes a partial fix for a netlink attribute nesting overflow bug in IFLA_VFINFO_LIST. By excluding the stats when the requester doesn't need them, the truncation of the VF list is avoided. While it was technically only the stats added in commit c5a9f6f0ab40 ("net/core: Add drop counters to VF statistics") breaking the camel's back, the appreciable size of the stats data should never have been included without due consideration for the maximum number of VFs supported by PCI. Fixes: 3b766cd83232 ("net/core: Add reading VF statistics through the PF netdevice") Fixes: c5a9f6f0ab40 ("net/core: Add drop counters to VF statistics") Signed-off-by: Edwin Peer --- net/core/rtnetlink.c | 96 +++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 3d6ab194d0f5..466f920ac974 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -931,24 +931,27 @@ static inline int rtnl_vfinfo_size(const struct net_device *dev, nla_total_size(sizeof(struct ifla_vf_rate)) + nla_total_size(sizeof(struct ifla_vf_link_state)) + nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + - nla_total_size(0) + /* nest IFLA_VF_STATS */ - /* IFLA_VF_STATS_RX_PACKETS */ - nla_total_size_64bit(sizeof(__u64)) + - /* IFLA_VF_STATS_TX_PACKETS */ - nla_total_size_64bit(sizeof(__u64)) + - /* IFLA_VF_STATS_RX_BYTES */ - nla_total_size_64bit(sizeof(__u64)) + - /* IFLA_VF_STATS_TX_BYTES */ - nla_total_size_64bit(sizeof(__u64)) + - /* IFLA_VF_STATS_BROADCAST */ - nla_total_size_64bit(sizeof(__u64)) + - /* IFLA_VF_STATS_MULTICAST */ - nla_total_size_64bit(sizeof(__u64)) + - /* IFLA_VF_STATS_RX_DROPPED */ - nla_total_size_64bit(sizeof(__u64)) + - /* IFLA_VF_STATS_TX_DROPPED */ - nla_total_size_64bit(sizeof(__u64)) + nla_total_size(sizeof(struct ifla_vf_trust))); + if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) { + size += num_vfs * + (nla_total_size(0) + /* nest IFLA_VF_STATS */ + /* IFLA_VF_STATS_RX_PACKETS */ + nla_total_size_64bit(sizeof(__u64)) + + /* IFLA_VF_STATS_TX_PACKETS */ + nla_total_size_64bit(sizeof(__u64)) + + /* IFLA_VF_STATS_RX_BYTES */ + nla_total_size_64bit(sizeof(__u64)) + + /* IFLA_VF_STATS_TX_BYTES */ + nla_total_size_64bit(sizeof(__u64)) + + /* IFLA_VF_STATS_BROADCAST */ + nla_total_size_64bit(sizeof(__u64)) + + /* IFLA_VF_STATS_MULTICAST */ + nla_total_size_64bit(sizeof(__u64)) + + /* IFLA_VF_STATS_RX_DROPPED */ + nla_total_size_64bit(sizeof(__u64)) + + /* IFLA_VF_STATS_TX_DROPPED */ + nla_total_size_64bit(sizeof(__u64))); + } return size; } else return 0; @@ -1223,7 +1226,8 @@ static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, struct net_device *dev, int vfs_num, - struct nlattr *vfinfo) + struct nlattr *vfinfo, + u32 ext_filter_mask) { struct ifla_vf_rss_query_en vf_rss_query_en; struct nlattr *vf, *vfstats, *vfvlanlist; @@ -1329,33 +1333,35 @@ static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, goto nla_put_vf_failure; } nla_nest_end(skb, vfvlanlist); - memset(&vf_stats, 0, sizeof(vf_stats)); - if (dev->netdev_ops->ndo_get_vf_stats) - dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, - &vf_stats); - vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); - if (!vfstats) - goto nla_put_vf_failure; - if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, - vf_stats.rx_packets, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, - vf_stats.tx_packets, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, - vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, - vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, - vf_stats.broadcast, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, - vf_stats.multicast, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, - vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, - vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { - nla_nest_cancel(skb, vfstats); - goto nla_put_vf_failure; + if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) { + memset(&vf_stats, 0, sizeof(vf_stats)); + if (dev->netdev_ops->ndo_get_vf_stats) + dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, + &vf_stats); + vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); + if (!vfstats) + goto nla_put_vf_failure; + if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, + vf_stats.rx_packets, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, + vf_stats.tx_packets, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, + vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, + vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, + vf_stats.broadcast, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, + vf_stats.multicast, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, + vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, + vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { + nla_nest_cancel(skb, vfstats); + goto nla_put_vf_failure; + } + nla_nest_end(skb, vfstats); } - nla_nest_end(skb, vfstats); nla_nest_end(skb, vf); return 0; @@ -1388,7 +1394,7 @@ static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, return -EMSGSIZE; for (i = 0; i < num_vfs; i++) { - if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) + if (rtnl_fill_vfinfo(skb, dev, i, vfinfo, ext_filter_mask)) return -EMSGSIZE; } From patchwork Sat Jan 23 04:53:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Edwin Peer X-Patchwork-Id: 12041099 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1934C433E6 for ; Sat, 23 Jan 2021 04:54:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9D04223AF8 for ; Sat, 23 Jan 2021 04:54:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726720AbhAWEyk (ORCPT ); Fri, 22 Jan 2021 23:54:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726654AbhAWEyO (ORCPT ); Fri, 22 Jan 2021 23:54:14 -0500 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82284C061788 for ; Fri, 22 Jan 2021 20:53:34 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id u11so4414871plg.13 for ; Fri, 22 Jan 2021 20:53:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version; bh=K83YF7GcNY82TVK2vJYspKj0xmLPWlJ36U3nO8TnBUE=; b=AEj2GmuSq9bAwmytXSXrvfU1Tvb3GkGgDFWbpAjFqi2td1IS73PkWrfpN/UyL5+CD4 70kXImoeWMlbmJWuN5/GL0+blgrmnUviMGLcD29o9UOp28bU7Y4jaG10Fu3IKWHWyWBy fYluih1Yy9c8An4nEP1/kCQJuTmQWfAeIpwnc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version; bh=K83YF7GcNY82TVK2vJYspKj0xmLPWlJ36U3nO8TnBUE=; b=hU/lUSNPMuFR44W4YIaE77k4NFUMJ38AnKsE3qJ2LPAHDkgCIuxYW7IwkG1cFvV3Ed tx1qupfCdZQ7ZsruMOwCETqlV+X3cdM/3WXYZkpZduJaokgmvQC0z8gmgsRda7GtNRys X2N1LCx1zKkGOxxr9uf98LQUGohzlphEGWWPxKJioc06OyRp2Ao6bnSrdDojpepQbozy 5nrNART27wyPwrzC26jEuBJoT5jrzPfdZFpSHQ5A7s0IBgCPgJVdUsVr9DnnpL27W/SO QaNLVTXd13UvHpJ7NH/Cdv54HIxLOaoNobcVQBu/yMPB9Au8yv6gaNnsXDEoT+speXbm TbKQ== X-Gm-Message-State: AOAM533CTp1GkC1ggBoAYVO8TjicFzv+HvT2WxwfM+QY2Kf8GPgO4O1U jx4QQxRJnfBo2Sx3sktQEydSoXk1K0tcXQb8wzs/y0pgrx9uPlFWK8MIgiTVuAWksIxD6PayLo0 S/CJnZs+dxFQ6lycbLISH+LJGQ67XC6jlnP+fuyLWEyoA0HFmf9m7y9LUtdtJZxYZghi9OnDd X-Google-Smtp-Source: ABdhPJxDPNUZI9t3LGRfbfRNqdv9D1LgnQFipdU7hhrCtRRe5CVwmQcG76I8yH1uqw+i1UIiRSvoIA== X-Received: by 2002:a17:90a:bd8c:: with SMTP id z12mr9270511pjr.204.1611377613505; Fri, 22 Jan 2021 20:53:33 -0800 (PST) Received: from hex.swdvt.lab.broadcom.net ([2600:8802:d04:de02::77c]) by smtp.gmail.com with ESMTPSA id d2sm10725832pjd.29.2021.01.22.20.53.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jan 2021 20:53:32 -0800 (PST) From: Edwin Peer To: netdev@vger.kernel.org Cc: Edwin Peer , Jakub Kicinski , Andrew Gospodarek , Michael Chan , Stephen Hemminger , Michal Kubecek , David Ahern Subject: [PATCH net-next 3/4] rtnetlink: refactor IFLA_VF_INFO stats into rtnl_fill_vfstats() Date: Fri, 22 Jan 2021 20:53:20 -0800 Message-Id: <20210123045321.2797360-4-edwin.peer@broadcom.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210123045321.2797360-1-edwin.peer@broadcom.com> References: <20210123045321.2797360-1-edwin.peer@broadcom.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Moving VF stats into their own function will facilitate separating them out later. No functional change. Signed-off-by: Edwin Peer --- net/core/rtnetlink.c | 66 +++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 466f920ac974..95564fd12f24 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -1223,6 +1223,42 @@ static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, return 0; } +static noinline_for_stack int rtnl_fill_vfstats(struct sk_buff *skb, + struct net_device *dev, + int vf_num) +{ + struct ifla_vf_stats vf_stats; + struct nlattr *vfstats; + + memset(&vf_stats, 0, sizeof(vf_stats)); + if (dev->netdev_ops->ndo_get_vf_stats) + dev->netdev_ops->ndo_get_vf_stats(dev, vf_num, &vf_stats); + vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); + if (!vfstats) + return -EMSGSIZE; + if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, + vf_stats.rx_packets, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, + vf_stats.tx_packets, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, + vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, + vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, + vf_stats.broadcast, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, + vf_stats.multicast, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, + vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || + nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, + vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { + nla_nest_cancel(skb, vfstats); + return -EMSGSIZE; + } + nla_nest_end(skb, vfstats); + return 0; +} + static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, struct net_device *dev, int vfs_num, @@ -1230,12 +1266,11 @@ static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, u32 ext_filter_mask) { struct ifla_vf_rss_query_en vf_rss_query_en; - struct nlattr *vf, *vfstats, *vfvlanlist; + struct nlattr *vf, *vfvlanlist; struct ifla_vf_link_state vf_linkstate; struct ifla_vf_vlan_info vf_vlan_info; struct ifla_vf_spoofchk vf_spoofchk; struct ifla_vf_tx_rate vf_tx_rate; - struct ifla_vf_stats vf_stats; struct ifla_vf_trust vf_trust; struct ifla_vf_vlan vf_vlan; struct ifla_vf_rate vf_rate; @@ -1334,33 +1369,8 @@ static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, } nla_nest_end(skb, vfvlanlist); if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) { - memset(&vf_stats, 0, sizeof(vf_stats)); - if (dev->netdev_ops->ndo_get_vf_stats) - dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, - &vf_stats); - vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); - if (!vfstats) - goto nla_put_vf_failure; - if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, - vf_stats.rx_packets, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, - vf_stats.tx_packets, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, - vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, - vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, - vf_stats.broadcast, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, - vf_stats.multicast, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, - vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || - nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, - vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { - nla_nest_cancel(skb, vfstats); + if (rtnl_fill_vfstats(skb, dev, vfs_num)) goto nla_put_vf_failure; - } - nla_nest_end(skb, vfstats); } nla_nest_end(skb, vf); return 0; From patchwork Sat Jan 23 04:53:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Edwin Peer X-Patchwork-Id: 12041103 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E372DC433E9 for ; Sat, 23 Jan 2021 04:54:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B505223B16 for ; Sat, 23 Jan 2021 04:54:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726733AbhAWEym (ORCPT ); Fri, 22 Jan 2021 23:54:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726335AbhAWEyQ (ORCPT ); Fri, 22 Jan 2021 23:54:16 -0500 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96C55C06178B for ; Fri, 22 Jan 2021 20:53:36 -0800 (PST) Received: by mail-pg1-x52f.google.com with SMTP id 30so5228837pgr.6 for ; Fri, 22 Jan 2021 20:53:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version; bh=ILT1zsp1gp4scssLPHXpg9nOq/pEWZdcrMBoO8vJPso=; b=FDHG9xt/uUv4UCDAT7XXiyEpUfYUbGqCzgr57SUu6eINvIsFKq9BvEJ0yPdg8PS69Q eYO5Cd/IqLEFKFUs0R5Q1cIUa1eYtwyOwemQhekp05pnQP1i9vW7pFzI+pZ7W0GyWOpD iRelljGFJ9W/a4ABlqWYz0SD0LBhTLTtpLM4Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version; bh=ILT1zsp1gp4scssLPHXpg9nOq/pEWZdcrMBoO8vJPso=; b=dcrbjXwUXsuwpm7hKaTw/HzD70NZHZBFct1mwNpjhtUzMLVz4pguYUYZ06cDSxbe2e H12IYdEFGbsyryyQKqs0f+SCC2e6xVZeFum5RLhPZjfzYBuVzTap1XziCIRFII8te1IX swCw3v/g656kypYBxvQ5niW9KHPNRq5pe2fXgxYXcuhh+UaXJXrXQLpr7b10LZmBdCYl pJEw2i0jDLrnMQPLp8zdis96cOWMdm231ekUAgJV0H3jHtUEEjDntX5/zNZlOeeFlA8X RCNFNz1SL2vR5jGHhDeFRaLAXvlrhx9Mw/kIFAhN1CdlUzSe1jIEoX998QgtFii8Bq60 Y2Eg== X-Gm-Message-State: AOAM531/NmTlx4ASbZHPTldehR+dS2Retw6vILBLeftgdcN29YWy1EYw 8Azh7MFDaTbrCmQIJc1nHyN/wO3+nemP3NHjCiQ/qbHGf0+3+xe2YdvmewRZxJKHHJPkYACDrwf Ob1OW4tmAJNly4P/gjLFZee8sy5BD9wnBOmYXRWu88JNrsMFoJ2rakDgFj4L/alNn/M0736qh X-Google-Smtp-Source: ABdhPJySCY33xJR4VeDi1DgVlWuCpV9QTkW315eyZKeZZEYSuPRBO3z+J86cBy7oIX+KFjtL3pqmwA== X-Received: by 2002:a65:6405:: with SMTP id a5mr7953724pgv.389.1611377615593; Fri, 22 Jan 2021 20:53:35 -0800 (PST) Received: from hex.swdvt.lab.broadcom.net ([2600:8802:d04:de02::77c]) by smtp.gmail.com with ESMTPSA id d2sm10725832pjd.29.2021.01.22.20.53.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jan 2021 20:53:34 -0800 (PST) From: Edwin Peer To: netdev@vger.kernel.org Cc: Edwin Peer , Jakub Kicinski , Andrew Gospodarek , Michael Chan , Stephen Hemminger , Michal Kubecek , David Ahern Subject: [PATCH net-next 4/4] rtnetlink: promote IFLA_VF_STATS to same level as IFLA_VF_INFO Date: Fri, 22 Jan 2021 20:53:21 -0800 Message-Id: <20210123045321.2797360-5-edwin.peer@broadcom.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210123045321.2797360-1-edwin.peer@broadcom.com> References: <20210123045321.2797360-1-edwin.peer@broadcom.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Separating the VF stats out of IFLA_VF_INFO appears to be the least impact way of resolving the nlattr overflow bug in IFLA_VFINFO_LIST. Since changing the hierarchy does constitute an ABI change, it must be explicitly requested via RTEXT_FILTER_VF_SEPARATE_STATS. Otherwise, the old location is maintained for compatibility. A new container type, namely IFLA_VFSTATS_LIST, is introduced to group the stats objects into an ordered list that corresponds with the order of VFs in IFLA_VFINFO_LIST. Fixes: 3b766cd83232 ("net/core: Add reading VF statistics through the PF netdevice") Fixes: c5a9f6f0ab40 ("net/core: Add drop counters to VF statistics") Signed-off-by: Edwin Peer --- include/uapi/linux/if_link.h | 1 + include/uapi/linux/rtnetlink.h | 1 + net/core/rtnetlink.c | 24 +++++++++++++++++++++--- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/uapi/linux/if_link.h b/include/uapi/linux/if_link.h index 2bd0d8bbcdb2..db12ffd2bffd 100644 --- a/include/uapi/linux/if_link.h +++ b/include/uapi/linux/if_link.h @@ -341,6 +341,7 @@ enum { IFLA_ALT_IFNAME, /* Alternative ifname */ IFLA_PERM_ADDRESS, IFLA_PROTO_DOWN_REASON, + IFLA_VFSTATS_LIST, __IFLA_MAX }; diff --git a/include/uapi/linux/rtnetlink.h b/include/uapi/linux/rtnetlink.h index b841caa4657e..f2f4f9b4d595 100644 --- a/include/uapi/linux/rtnetlink.h +++ b/include/uapi/linux/rtnetlink.h @@ -789,6 +789,7 @@ enum { #define RTEXT_FILTER_MRP (1 << 4) #define RTEXT_FILTER_CFM_CONFIG (1 << 5) #define RTEXT_FILTER_CFM_STATUS (1 << 6) +#define RTEXT_FILTER_VF_SEPARATE_STATS (1 << 7) /* End of information exported to user level */ diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 95564fd12f24..cddd3945bc11 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -933,6 +933,8 @@ static inline int rtnl_vfinfo_size(const struct net_device *dev, nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + nla_total_size(sizeof(struct ifla_vf_trust))); if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) { + if (ext_filter_mask & RTEXT_FILTER_VF_SEPARATE_STATS) + size += nla_total_size(0); /* IFLA_VFSTATS_LIST */ size += num_vfs * (nla_total_size(0) + /* nest IFLA_VF_STATS */ /* IFLA_VF_STATS_RX_PACKETS */ @@ -1368,7 +1370,8 @@ static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, goto nla_put_vf_failure; } nla_nest_end(skb, vfvlanlist); - if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) { + if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS && + ~ext_filter_mask & RTEXT_FILTER_VF_SEPARATE_STATS) { if (rtnl_fill_vfstats(skb, dev, vfs_num)) goto nla_put_vf_failure; } @@ -1386,7 +1389,7 @@ static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, struct net_device *dev, u32 ext_filter_mask) { - struct nlattr *vfinfo; + struct nlattr *vfinfo, *vfstats; int i, num_vfs; if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) @@ -1407,8 +1410,23 @@ static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, if (rtnl_fill_vfinfo(skb, dev, i, vfinfo, ext_filter_mask)) return -EMSGSIZE; } - nla_nest_end(skb, vfinfo); + + if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS && + ext_filter_mask & RTEXT_FILTER_VF_SEPARATE_STATS) { + vfstats = nla_nest_start_noflag(skb, IFLA_VFSTATS_LIST); + if (!vfstats) + return -EMSGSIZE; + + for (i = 0; i < num_vfs; i++) { + if (rtnl_fill_vfstats(skb, dev, i)) { + nla_nest_cancel(skb, vfstats); + return -EMSGSIZE; + } + } + nla_nest_end(skb, vfstats); + } + return 0; }