[Devel] [PATCH RHEL7 COMMIT] ms/netlink: Introduce nla_strdup()

Vasily Averin vvs at virtuozzo.com
Wed Nov 24 14:50:23 MSK 2021


The commit is pushed to "branch-rh7-3.10.0-1160.42.2.vz7.184.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-1160.42.2.vz7.184.4
------>
commit adc075192fc4b93842c36af1f69604d630552d84
Author: Phil Sutter <phil at nwl.cc>
Date:   Wed Nov 24 14:50:23 2021 +0300

    ms/netlink: Introduce nla_strdup()
    
    This is similar to strdup() for netlink string attributes.
    
    Signed-off-by: Phil Sutter <phil at nwl.cc>
    Signed-off-by: Pablo Neira Ayuso <pablo at netfilter.org>
    
    Upstream commit 2cf0c8b3e6942ecafe6ebb1a6d0328a81641bf39.
    
    https://jira.sw.ru/browse/PSBM-136188
    Signed-off-by: Nikita Yushchenko <nikita.yushchenko at virtuozzo.com>
---
 include/net/netlink.h |  1 +
 lib/nlattr.c          | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/include/net/netlink.h b/include/net/netlink.h
index 4b8499a..225b86a 100644
--- a/include/net/netlink.h
+++ b/include/net/netlink.h
@@ -246,6 +246,7 @@ int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
 int nla_policy_len(const struct nla_policy *, int);
 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
+char *nla_strdup(const struct nlattr *nla, gfp_t flags);
 int nla_memcpy(void *dest, const struct nlattr *src, int count);
 int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
 int nla_strcmp(const struct nlattr *nla, const char *str);
diff --git a/lib/nlattr.c b/lib/nlattr.c
index b9aa683..f96c0a5 100644
--- a/lib/nlattr.c
+++ b/lib/nlattr.c
@@ -291,6 +291,30 @@ size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
 }
 
 /**
+ * nla_strdup - Copy string attribute payload into a newly allocated buffer
+ * @nla: attribute to copy the string from
+ * @flags: the type of memory to allocate (see kmalloc).
+ *
+ * Returns a pointer to the allocated buffer or NULL on error.
+ */
+char *nla_strdup(const struct nlattr *nla, gfp_t flags)
+{
+	size_t srclen = nla_len(nla);
+	char *src = nla_data(nla), *dst;
+
+	if (srclen > 0 && src[srclen - 1] == '\0')
+		srclen--;
+
+	dst = kmalloc(srclen + 1, flags);
+	if (dst != NULL) {
+		memcpy(dst, src, srclen);
+		dst[srclen] = '\0';
+	}
+	return dst;
+}
+EXPORT_SYMBOL(nla_strdup);
+
+/**
  * nla_memcpy - Copy a netlink attribute into another memory area
  * @dest: where to copy to memcpy
  * @src: netlink attribute to copy from


More information about the Devel mailing list