From nobody Tue Oct 17 07:37:58 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 4S8m6b0l6xz4xC5d; Tue, 17 Oct 2023 07:37:59 +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 4S8m6Z5JB7z4QSj; Tue, 17 Oct 2023 07:37:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1697528278; 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=ghPHCBGzCRrvzvORf5tbivK49sAs9TlttaaMrG+cD5w=; b=G5Y4DfSt9l0tmLs0XbTWeQsOK9DNqZJ7xby0mqalGsbQzUKpNfGb2z44RboMGteDINjQly 8u7h8SP6XNNWoVkIJSTJklmDQ84rNFPYI0Vij6piVT7RgFGSEmhgtuUrMNFaYFgX2Cv76U 4QkdA8Y9VOfew4OaooYDrYNK55SHUlASefTxJdiqioqj8ZbpgHMCMeopTnQQR5zz55uZfi /Am++nOV8+Tov8R8XFtep4Y0nGVcCNJg/kfm6RuqxCz9bk6bznEdy1Hzxqw9Ki1MYjhgsg 1zUQX8KRG5pdufWtWT2wG4maIKBo2AOuSDPXJEi+LqVnjLrjChh9Z+ZgTfkZeg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1697528278; a=rsa-sha256; cv=none; b=MnjJYk8xF0dwwZrUBvr54l9xTTmopBa9pTMaeTCQnM42Mz/JHbpCAfxMdJzw9bBBvlDG5j uHDtbUW2sk3Ml3Up7IHGz/v+uu0Lxe69OIUXRi15tu9wpdg3vdz8OCNRk6Ft/lCrGTUGWj ZMrkpOTyvXCEqMth/2jH87NxVOsWmt7R9U2qbrgrb7rF40VIJ1ykSi8S+dgRQ11CYCHn5E G9nM+NNxSjz710nz1BgMXHrRlmzc0FTaxxwe+dDn0ueDBrZ/PRmmjNhtGWGXrxu5lvUp++ 1FxoyZajyfZYWKeutTtdJ0LSMn/8ujDXzvPQz1rWUzHW76XExeMFgRJP1CROow== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1697528278; 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=ghPHCBGzCRrvzvORf5tbivK49sAs9TlttaaMrG+cD5w=; b=Y11jvEt/7gZyxpLkhnfkJEwFKsApKCv/ZjgsjdBF/XrC3b3FcNO4f9SoZ4ZpRkLO1Q+zW5 5+AxA37mz2kC9vXeCgSQ+silYBbk61BngiZW+fN+tyGBtcS4cFuDwjYsaFtJgQNaRx8I6l MLcmC9ft4wryxkJj9KL9K7lzNCC7bEitDjyq82MD/JFb66VvP2cbltr9sOyaxfYDNZDyp6 c67gK4wdFS8hKEr/74yLK2n+ckTD5LWZty5Td918KqOyG+DdANcODOuWso44ZZmLF+MUax q4rfynMfSifshCthbMT7LKE9H3WglLBA375fiXjN/4xBHPZKRRiWHounkKdDDg== 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 4S8m6Z467LzS1; Tue, 17 Oct 2023 07:37:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 39H7bw0I095216; Tue, 17 Oct 2023 07:37:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 39H7bwUm095213; Tue, 17 Oct 2023 07:37:58 GMT (envelope-from git) Date: Tue, 17 Oct 2023 07:37:58 GMT Message-Id: <202310170737.39H7bwUm095213@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kristof Provost Subject: git: 1c5c7e61c85f - main - netlink: add attr parser utility functions 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: kp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1c5c7e61c85fffa274119a69d69b3405848b9c82 Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=1c5c7e61c85fffa274119a69d69b3405848b9c82 commit 1c5c7e61c85fffa274119a69d69b3405848b9c82 Author: Kristof Provost AuthorDate: 2023-10-14 10:13:30 +0000 Commit: Kristof Provost CommitDate: 2023-10-17 06:47:46 +0000 netlink: add attr parser utility functions - nlattr_get_chara() to read a string into a char array, rather than to a char * - nlattr_get_bytes() to read an arbitrary (fixed length) byte sequence - nlattr_get_nested_ptr() to read a nested type to a struct foo *, rather than struct foo Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D42221 --- sys/netlink/netlink_message_parser.c | 42 ++++++++++++++++++++++++++++++++++++ sys/netlink/netlink_message_parser.h | 6 ++++++ 2 files changed, 48 insertions(+) diff --git a/sys/netlink/netlink_message_parser.c b/sys/netlink/netlink_message_parser.c index 9ff5cdee40b4..48d712211a98 100644 --- a/sys/netlink/netlink_message_parser.c +++ b/sys/netlink/netlink_message_parser.c @@ -428,6 +428,23 @@ nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void return (nlattr_get_ifp_internal(nla, npt, target, true)); } +int +nlattr_get_chara(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target) +{ + int maxlen = NLA_DATA_LEN(nla); + int target_size = (size_t)arg; + int len = strnlen((char *)NLA_DATA(nla), maxlen); + + if (__predict_false(len >= maxlen) || __predict_false(len >= target_size)) { + NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not NULL-terminated or longer than %u", + nla->nla_type, maxlen, target_size); + return (EINVAL); + } + + strncpy((char *)target, (char *)NLA_DATA(nla), target_size); + return (0); +} + int nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target) { @@ -457,6 +474,20 @@ nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt, const void *arg, v *((char **)target) = buf; return (0); } + +int +nlattr_get_bytes(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target) +{ + size_t size = (size_t)arg; + + if (NLA_DATA_LEN(nla) != size) + return (EINVAL); + + memcpy(target, NLA_DATA(nla), size); + + return (0); +} + int nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target) { @@ -476,6 +507,17 @@ nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt, const void *arg, vo return (error); } +int +nlattr_get_nested_ptr(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target) +{ + const struct nlhdr_parser *p = (const struct nlhdr_parser *)arg; + int error; + + /* Assumes target points to the beginning of the structure */ + error = nl_parse_header(NLA_DATA(nla), NLA_DATA_LEN(nla), p, npt, *(void **)target); + return (error); +} + int nlf_get_ifp(void *src, struct nl_pstate *npt, void *target) { diff --git a/sys/netlink/netlink_message_parser.h b/sys/netlink/netlink_message_parser.h index 0242177fdd26..517f3ebd49f2 100644 --- a/sys/netlink/netlink_message_parser.h +++ b/sys/netlink/netlink_message_parser.h @@ -187,14 +187,20 @@ int nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target); int nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target); +int nlattr_get_chara(struct nlattr *nla, struct nl_pstate *npt, + const void *arg, void *target); int nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target); int nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target); +int nlattr_get_bytes(struct nlattr *nla, struct nl_pstate *npt, + const void *arg, void *target); int nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target); int nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target); +int nlattr_get_nested_ptr(struct nlattr *nla, struct nl_pstate *npt, + const void *arg, void *target); bool nlmsg_report_err_msg(struct nl_pstate *npt, const char *fmt, ...);