Skip to content

Commit 2cf0c8b

Browse files
Phil Sutterummakynes
authored andcommitted
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>
1 parent 6e69267 commit 2cf0c8b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

include/net/netlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
247247
int nla_policy_len(const struct nla_policy *, int);
248248
struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
249249
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
250+
char *nla_strdup(const struct nlattr *nla, gfp_t flags);
250251
int nla_memcpy(void *dest, const struct nlattr *src, int count);
251252
int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
252253
int nla_strcmp(const struct nlattr *nla, const char *str);

lib/nlattr.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,30 @@ size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
271271
}
272272
EXPORT_SYMBOL(nla_strlcpy);
273273

274+
/**
275+
* nla_strdup - Copy string attribute payload into a newly allocated buffer
276+
* @nla: attribute to copy the string from
277+
* @flags: the type of memory to allocate (see kmalloc).
278+
*
279+
* Returns a pointer to the allocated buffer or NULL on error.
280+
*/
281+
char *nla_strdup(const struct nlattr *nla, gfp_t flags)
282+
{
283+
size_t srclen = nla_len(nla);
284+
char *src = nla_data(nla), *dst;
285+
286+
if (srclen > 0 && src[srclen - 1] == '\0')
287+
srclen--;
288+
289+
dst = kmalloc(srclen + 1, flags);
290+
if (dst != NULL) {
291+
memcpy(dst, src, srclen);
292+
dst[srclen] = '\0';
293+
}
294+
return dst;
295+
}
296+
EXPORT_SYMBOL(nla_strdup);
297+
274298
/**
275299
* nla_memcpy - Copy a netlink attribute into another memory area
276300
* @dest: where to copy to memcpy

0 commit comments

Comments
 (0)