summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2017-07-27 16:56:40 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2017-07-31 20:41:50 +0200
commit2cf0c8b3e6942ecafe6ebb1a6d0328a81641bf39 (patch)
tree73cde1e5fb89887682cdd7f221b34d424d0407f8 /lib
parent6e692678d74289d6129bbd4bb20bb9fe01278faa (diff)
downloadop-kernel-dev-2cf0c8b3e6942ecafe6ebb1a6d0328a81641bf39.zip
op-kernel-dev-2cf0c8b3e6942ecafe6ebb1a6d0328a81641bf39.tar.gz
netlink: Introduce nla_strdup()
This is similar to strdup() for netlink string attributes. Signed-off-by: Phil Sutter <phil@nwl.cc> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/nlattr.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/nlattr.c b/lib/nlattr.c
index fb52435..f13013f 100644
--- a/lib/nlattr.c
+++ b/lib/nlattr.c
@@ -272,6 +272,30 @@ size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
EXPORT_SYMBOL(nla_strlcpy);
/**
+ * 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
OpenPOWER on IntegriCloud