Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2323067pxa; Mon, 24 Aug 2020 10:54:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzMqCzNlhwRQ0OCbvpWWorBkV5yl+lRkIK5eLPSjy0vsKJKJoPVMk62dXpYAzSsKAxbLt5X X-Received: by 2002:a05:6402:229b:: with SMTP id cw27mr3620690edb.272.1598291660824; Mon, 24 Aug 2020 10:54:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598291660; cv=none; d=google.com; s=arc-20160816; b=jvkEVO9BrduX8x2y9wyr+kOW93M28rN71nHRSd3j65OcV3sOku7yPJ0Mgcd7VDSxAb GjLzlBzS027CaVRujm+YepktHirTJ/dVD3bpn1vbv0hXlzEBKI8YCyQkFVq1dP0ffVYV tWoIEJpjbwOs3AI+SCC4LrCYbNt+QivufMCNXrLwZjwD4DpAJkKL7RT5kx4YyYIGy+3s XZRYfeJkvLo7aarTp63LDdVjSdhHtQhT5nVlIXETzTWGhCA0JGkpL98okM+Rrnkss3HA z4gM2iUocxGsCZI3GMZ80yxyyi8ALtwDCEn5tngbhqKw2pgDn5GhD95pEJFrmUsMv1ou kAWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=bJlp5UfFGJnQxMwDhhKCKIM7qw6Mi3B035gdSE7VbrI=; b=ST0GEhV+CnPmEfH8lgaFp2UQVP1i/94K2PL0tLsLcQ6pGt6JwmQ6PAjGvO2erpqEk+ 2IxD44rPXFk1L2cE3NSVUXGKNZFH38mt/eXVd8om5ZX5oBOGQKFbgxSPEVkl/3noG9lP wUK2oomE9164rsJeGviFe1yoQmMifIih+KcFeSfVeZWbeEX7ni8dna2KoxF21cEAgd+G K2B/BgQFen2gaLFRsNJQpgKQK/WjniZt9nCtCdIXfhqZBP+AFcgJXGRqsd6I83HFVMRS Xr0bNjEL1eplSOG04agzGqCO6lVPEOhk4ZJJGYBLM5yNGoShROsssZT6amXxwkQI72do U0Eg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e26si2266572edc.216.2020.08.24.10.53.56; Mon, 24 Aug 2020 10:54:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726529AbgHXRvZ (ORCPT + 99 others); Mon, 24 Aug 2020 13:51:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726697AbgHXRvX (ORCPT ); Mon, 24 Aug 2020 13:51:23 -0400 Received: from sipsolutions.net (s3.sipsolutions.net [IPv6:2a01:4f8:191:4433::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 066D4C061575; Mon, 24 Aug 2020 10:51:22 -0700 (PDT) Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.94) (envelope-from ) id 1kAGco-00A1ue-Sm; Mon, 24 Aug 2020 19:51:18 +0200 From: Johannes Berg To: linux-wireless@vger.kernel.org, Stephen Hemminger , netdev@vger.kernel.org Cc: David Ahern Subject: [PATCH 2/3] libnetlink: add nl_print_policy() helper Date: Mon, 24 Aug 2020 19:51:07 +0200 Message-Id: <20200824175108.53101-2-johannes@sipsolutions.net> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200824175108.53101-1-johannes@sipsolutions.net> References: <20200824175108.53101-1-johannes@sipsolutions.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org This prints out the data from the given nested attribute to the given FILE pointer, interpreting the firmware that the kernel has for showing netlink policies. Signed-off-by: Johannes Berg --- include/libnetlink.h | 2 ++ lib/libnetlink.c | 73 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/include/libnetlink.h b/include/libnetlink.h index 0d4a9f29afbd..b9073a6a13ad 100644 --- a/include/libnetlink.h +++ b/include/libnetlink.h @@ -289,4 +289,6 @@ int rtnl_from_file(FILE *, rtnl_listen_filter_t handler, RTA_OK(attr, RTA_PAYLOAD(nest) - ((char *)(attr) - (char *)RTA_DATA((nest)))); \ (attr) = RTA_TAIL((attr))) +void nl_print_policy(const struct rtattr *attr, FILE *fp); + #endif /* __LIBNETLINK_H__ */ diff --git a/lib/libnetlink.c b/lib/libnetlink.c index e02d6294b02e..a7b60d873afb 100644 --- a/lib/libnetlink.c +++ b/lib/libnetlink.c @@ -28,6 +28,7 @@ #include #include "libnetlink.h" +#include "utils.h" #define __aligned(x) __attribute__((aligned(x))) @@ -1440,3 +1441,75 @@ int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); return 0; } + +static const char *get_nla_type_str(unsigned int attr) +{ + switch (attr) { +#define C(x) case NL_ATTR_TYPE_ ## x: return #x + C(U8); + C(U16); + C(U32); + C(U64); + C(STRING); + C(FLAG); + C(NESTED); + C(NESTED_ARRAY); + C(NUL_STRING); + C(BINARY); + C(S8); + C(S16); + C(S32); + C(S64); + C(BITFIELD32); + default: + return "unknown"; + } +} + +void nl_print_policy(const struct rtattr *attr, FILE *fp) +{ + const struct rtattr *pos; + + rtattr_for_each_nested(pos, attr) { + const struct rtattr *attr; + + fprintf(fp, " policy[%u]:", pos->rta_type & ~NLA_F_NESTED); + + rtattr_for_each_nested(attr, pos) { + struct rtattr *tp[NL_POLICY_TYPE_ATTR_MAX + 1]; + + parse_rtattr_nested(tp, ARRAY_SIZE(tp) - 1, attr); + + if (tp[NL_POLICY_TYPE_ATTR_TYPE]) + fprintf(fp, "attr[%u]: type=%s", + attr->rta_type & ~NLA_F_NESTED, + get_nla_type_str(rta_getattr_u32(tp[NL_POLICY_TYPE_ATTR_TYPE]))); + + if (tp[NL_POLICY_TYPE_ATTR_POLICY_IDX]) + fprintf(fp, " policy:%u", + rta_getattr_u32(tp[NL_POLICY_TYPE_ATTR_POLICY_IDX])); + + if (tp[NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE]) + fprintf(fp, " maxattr:%u", + rta_getattr_u32(tp[NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE])); + + if (tp[NL_POLICY_TYPE_ATTR_MIN_VALUE_S] && tp[NL_POLICY_TYPE_ATTR_MAX_VALUE_S]) + fprintf(fp, " range:[%lld,%lld]", + (signed long long)rta_getattr_u64(tp[NL_POLICY_TYPE_ATTR_MIN_VALUE_S]), + (signed long long)rta_getattr_u64(tp[NL_POLICY_TYPE_ATTR_MAX_VALUE_S])); + + if (tp[NL_POLICY_TYPE_ATTR_MIN_VALUE_U] && tp[NL_POLICY_TYPE_ATTR_MAX_VALUE_U]) + fprintf(fp, " range:[%llu,%llu]", + (unsigned long long)rta_getattr_u64(tp[NL_POLICY_TYPE_ATTR_MIN_VALUE_U]), + (unsigned long long)rta_getattr_u64(tp[NL_POLICY_TYPE_ATTR_MAX_VALUE_U])); + + if (tp[NL_POLICY_TYPE_ATTR_MIN_LENGTH]) + fprintf(fp, " min len:%u", + rta_getattr_u32(tp[NL_POLICY_TYPE_ATTR_MIN_LENGTH])); + + if (tp[NL_POLICY_TYPE_ATTR_MAX_LENGTH]) + fprintf(fp, " max len:%u", + rta_getattr_u32(tp[NL_POLICY_TYPE_ATTR_MAX_LENGTH])); + } + } +} -- 2.26.2