Linux Audio

Check our new training course

Open-source upstreaming

Need help get the support for your hardware in upstream Linux?
Loading...
v3.5.6
  1/*
  2 * NETLINK      Netlink attributes
  3 *
  4 * 		Authors:	Thomas Graf <tgraf@suug.ch>
  5 * 				Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  6 */
  7
  8#include <linux/export.h>
  9#include <linux/kernel.h>
 10#include <linux/errno.h>
 11#include <linux/jiffies.h>
 12#include <linux/netdevice.h>
 13#include <linux/skbuff.h>
 14#include <linux/string.h>
 15#include <linux/types.h>
 16#include <net/netlink.h>
 17
 18static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = {
 19	[NLA_U8]	= sizeof(u8),
 20	[NLA_U16]	= sizeof(u16),
 21	[NLA_U32]	= sizeof(u32),
 22	[NLA_U64]	= sizeof(u64),
 23	[NLA_MSECS]	= sizeof(u64),
 24	[NLA_NESTED]	= NLA_HDRLEN,
 
 
 
 
 25};
 26
 27static int validate_nla(const struct nlattr *nla, int maxtype,
 28			const struct nla_policy *policy)
 29{
 30	const struct nla_policy *pt;
 31	int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
 32
 33	if (type <= 0 || type > maxtype)
 34		return 0;
 35
 36	pt = &policy[type];
 37
 38	BUG_ON(pt->type > NLA_TYPE_MAX);
 39
 40	switch (pt->type) {
 41	case NLA_FLAG:
 42		if (attrlen > 0)
 43			return -ERANGE;
 44		break;
 45
 46	case NLA_NUL_STRING:
 47		if (pt->len)
 48			minlen = min_t(int, attrlen, pt->len + 1);
 49		else
 50			minlen = attrlen;
 51
 52		if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
 53			return -EINVAL;
 54		/* fall through */
 55
 56	case NLA_STRING:
 57		if (attrlen < 1)
 58			return -ERANGE;
 59
 60		if (pt->len) {
 61			char *buf = nla_data(nla);
 62
 63			if (buf[attrlen - 1] == '\0')
 64				attrlen--;
 65
 66			if (attrlen > pt->len)
 67				return -ERANGE;
 68		}
 69		break;
 70
 71	case NLA_BINARY:
 72		if (pt->len && attrlen > pt->len)
 73			return -ERANGE;
 74		break;
 75
 76	case NLA_NESTED_COMPAT:
 77		if (attrlen < pt->len)
 78			return -ERANGE;
 79		if (attrlen < NLA_ALIGN(pt->len))
 80			break;
 81		if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
 82			return -ERANGE;
 83		nla = nla_data(nla) + NLA_ALIGN(pt->len);
 84		if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
 85			return -ERANGE;
 86		break;
 87	case NLA_NESTED:
 88		/* a nested attributes is allowed to be empty; if its not,
 89		 * it must have a size of at least NLA_HDRLEN.
 90		 */
 91		if (attrlen == 0)
 92			break;
 93	default:
 94		if (pt->len)
 95			minlen = pt->len;
 96		else if (pt->type != NLA_UNSPEC)
 97			minlen = nla_attr_minlen[pt->type];
 98
 99		if (attrlen < minlen)
100			return -ERANGE;
101	}
102
103	return 0;
104}
105
106/**
107 * nla_validate - Validate a stream of attributes
108 * @head: head of attribute stream
109 * @len: length of attribute stream
110 * @maxtype: maximum attribute type to be expected
111 * @policy: validation policy
112 *
113 * Validates all attributes in the specified attribute stream against the
114 * specified policy. Attributes with a type exceeding maxtype will be
115 * ignored. See documenation of struct nla_policy for more details.
116 *
117 * Returns 0 on success or a negative error code.
118 */
119int nla_validate(const struct nlattr *head, int len, int maxtype,
120		 const struct nla_policy *policy)
121{
122	const struct nlattr *nla;
123	int rem, err;
124
125	nla_for_each_attr(nla, head, len, rem) {
126		err = validate_nla(nla, maxtype, policy);
127		if (err < 0)
128			goto errout;
129	}
130
131	err = 0;
132errout:
133	return err;
134}
135
136/**
137 * nla_policy_len - Determin the max. length of a policy
138 * @policy: policy to use
139 * @n: number of policies
140 *
141 * Determines the max. length of the policy.  It is currently used
142 * to allocated Netlink buffers roughly the size of the actual
143 * message.
144 *
145 * Returns 0 on success or a negative error code.
146 */
147int
148nla_policy_len(const struct nla_policy *p, int n)
149{
150	int i, len = 0;
151
152	for (i = 0; i < n; i++, p++) {
153		if (p->len)
154			len += nla_total_size(p->len);
155		else if (nla_attr_minlen[p->type])
156			len += nla_total_size(nla_attr_minlen[p->type]);
157	}
158
159	return len;
160}
161
162/**
163 * nla_parse - Parse a stream of attributes into a tb buffer
164 * @tb: destination array with maxtype+1 elements
165 * @maxtype: maximum attribute type to be expected
166 * @head: head of attribute stream
167 * @len: length of attribute stream
168 * @policy: validation policy
169 *
170 * Parses a stream of attributes and stores a pointer to each attribute in
171 * the tb array accessible via the attribute type. Attributes with a type
172 * exceeding maxtype will be silently ignored for backwards compatibility
173 * reasons. policy may be set to NULL if no validation is required.
174 *
175 * Returns 0 on success or a negative error code.
176 */
177int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
178	      int len, const struct nla_policy *policy)
179{
180	const struct nlattr *nla;
181	int rem, err;
182
183	memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
184
185	nla_for_each_attr(nla, head, len, rem) {
186		u16 type = nla_type(nla);
187
188		if (type > 0 && type <= maxtype) {
189			if (policy) {
190				err = validate_nla(nla, maxtype, policy);
191				if (err < 0)
192					goto errout;
193			}
194
195			tb[type] = (struct nlattr *)nla;
196		}
197	}
198
199	if (unlikely(rem > 0))
200		printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
201		       "attributes.\n", rem);
202
203	err = 0;
204errout:
205	return err;
206}
207
208/**
209 * nla_find - Find a specific attribute in a stream of attributes
210 * @head: head of attribute stream
211 * @len: length of attribute stream
212 * @attrtype: type of attribute to look for
213 *
214 * Returns the first attribute in the stream matching the specified type.
215 */
216struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
217{
218	const struct nlattr *nla;
219	int rem;
220
221	nla_for_each_attr(nla, head, len, rem)
222		if (nla_type(nla) == attrtype)
223			return (struct nlattr *)nla;
224
225	return NULL;
226}
227
228/**
229 * nla_strlcpy - Copy string attribute payload into a sized buffer
230 * @dst: where to copy the string to
231 * @nla: attribute to copy the string from
232 * @dstsize: size of destination buffer
233 *
234 * Copies at most dstsize - 1 bytes into the destination buffer.
235 * The result is always a valid NUL-terminated string. Unlike
236 * strlcpy the destination buffer is always padded out.
237 *
238 * Returns the length of the source buffer.
239 */
240size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
241{
242	size_t srclen = nla_len(nla);
243	char *src = nla_data(nla);
244
245	if (srclen > 0 && src[srclen - 1] == '\0')
246		srclen--;
247
248	if (dstsize > 0) {
249		size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
250
251		memset(dst, 0, dstsize);
252		memcpy(dst, src, len);
253	}
254
255	return srclen;
256}
257
258/**
259 * nla_memcpy - Copy a netlink attribute into another memory area
260 * @dest: where to copy to memcpy
261 * @src: netlink attribute to copy from
262 * @count: size of the destination area
263 *
264 * Note: The number of bytes copied is limited by the length of
265 *       attribute's payload. memcpy
266 *
267 * Returns the number of bytes copied.
268 */
269int nla_memcpy(void *dest, const struct nlattr *src, int count)
270{
271	int minlen = min_t(int, count, nla_len(src));
272
273	memcpy(dest, nla_data(src), minlen);
274
275	return minlen;
276}
277
278/**
279 * nla_memcmp - Compare an attribute with sized memory area
280 * @nla: netlink attribute
281 * @data: memory area
282 * @size: size of memory area
283 */
284int nla_memcmp(const struct nlattr *nla, const void *data,
285			     size_t size)
286{
287	int d = nla_len(nla) - size;
288
289	if (d == 0)
290		d = memcmp(nla_data(nla), data, size);
291
292	return d;
293}
294
295/**
296 * nla_strcmp - Compare a string attribute against a string
297 * @nla: netlink string attribute
298 * @str: another string
299 */
300int nla_strcmp(const struct nlattr *nla, const char *str)
301{
302	int len = strlen(str) + 1;
303	int d = nla_len(nla) - len;
 
 
304
 
 
 
 
305	if (d == 0)
306		d = memcmp(nla_data(nla), str, len);
307
308	return d;
309}
310
311#ifdef CONFIG_NET
312/**
313 * __nla_reserve - reserve room for attribute on the skb
314 * @skb: socket buffer to reserve room on
315 * @attrtype: attribute type
316 * @attrlen: length of attribute payload
317 *
318 * Adds a netlink attribute header to a socket buffer and reserves
319 * room for the payload but does not copy it.
320 *
321 * The caller is responsible to ensure that the skb provides enough
322 * tailroom for the attribute header and payload.
323 */
324struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
325{
326	struct nlattr *nla;
327
328	nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
329	nla->nla_type = attrtype;
330	nla->nla_len = nla_attr_size(attrlen);
331
332	memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
333
334	return nla;
335}
336EXPORT_SYMBOL(__nla_reserve);
337
338/**
339 * __nla_reserve_nohdr - reserve room for attribute without header
340 * @skb: socket buffer to reserve room on
341 * @attrlen: length of attribute payload
342 *
343 * Reserves room for attribute payload without a header.
344 *
345 * The caller is responsible to ensure that the skb provides enough
346 * tailroom for the payload.
347 */
348void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
349{
350	void *start;
351
352	start = skb_put(skb, NLA_ALIGN(attrlen));
353	memset(start, 0, NLA_ALIGN(attrlen));
354
355	return start;
356}
357EXPORT_SYMBOL(__nla_reserve_nohdr);
358
359/**
360 * nla_reserve - reserve room for attribute on the skb
361 * @skb: socket buffer to reserve room on
362 * @attrtype: attribute type
363 * @attrlen: length of attribute payload
364 *
365 * Adds a netlink attribute header to a socket buffer and reserves
366 * room for the payload but does not copy it.
367 *
368 * Returns NULL if the tailroom of the skb is insufficient to store
369 * the attribute header and payload.
370 */
371struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
372{
373	if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
374		return NULL;
375
376	return __nla_reserve(skb, attrtype, attrlen);
377}
378EXPORT_SYMBOL(nla_reserve);
379
380/**
381 * nla_reserve_nohdr - reserve room for attribute without header
382 * @skb: socket buffer to reserve room on
383 * @attrlen: length of attribute payload
384 *
385 * Reserves room for attribute payload without a header.
386 *
387 * Returns NULL if the tailroom of the skb is insufficient to store
388 * the attribute payload.
389 */
390void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
391{
392	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
393		return NULL;
394
395	return __nla_reserve_nohdr(skb, attrlen);
396}
397EXPORT_SYMBOL(nla_reserve_nohdr);
398
399/**
400 * __nla_put - Add a netlink attribute to a socket buffer
401 * @skb: socket buffer to add attribute to
402 * @attrtype: attribute type
403 * @attrlen: length of attribute payload
404 * @data: head of attribute payload
405 *
406 * The caller is responsible to ensure that the skb provides enough
407 * tailroom for the attribute header and payload.
408 */
409void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
410			     const void *data)
411{
412	struct nlattr *nla;
413
414	nla = __nla_reserve(skb, attrtype, attrlen);
415	memcpy(nla_data(nla), data, attrlen);
416}
417EXPORT_SYMBOL(__nla_put);
418
419/**
420 * __nla_put_nohdr - Add a netlink attribute without header
421 * @skb: socket buffer to add attribute to
422 * @attrlen: length of attribute payload
423 * @data: head of attribute payload
424 *
425 * The caller is responsible to ensure that the skb provides enough
426 * tailroom for the attribute payload.
427 */
428void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
429{
430	void *start;
431
432	start = __nla_reserve_nohdr(skb, attrlen);
433	memcpy(start, data, attrlen);
434}
435EXPORT_SYMBOL(__nla_put_nohdr);
436
437/**
438 * nla_put - Add a netlink attribute to a socket buffer
439 * @skb: socket buffer to add attribute to
440 * @attrtype: attribute type
441 * @attrlen: length of attribute payload
442 * @data: head of attribute payload
443 *
444 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
445 * the attribute header and payload.
446 */
447int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
448{
449	if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
450		return -EMSGSIZE;
451
452	__nla_put(skb, attrtype, attrlen, data);
453	return 0;
454}
455EXPORT_SYMBOL(nla_put);
456
457/**
458 * nla_put_nohdr - Add a netlink attribute without header
459 * @skb: socket buffer to add attribute to
460 * @attrlen: length of attribute payload
461 * @data: head of attribute payload
462 *
463 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
464 * the attribute payload.
465 */
466int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
467{
468	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
469		return -EMSGSIZE;
470
471	__nla_put_nohdr(skb, attrlen, data);
472	return 0;
473}
474EXPORT_SYMBOL(nla_put_nohdr);
475
476/**
477 * nla_append - Add a netlink attribute without header or padding
478 * @skb: socket buffer to add attribute to
479 * @attrlen: length of attribute payload
480 * @data: head of attribute payload
481 *
482 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
483 * the attribute payload.
484 */
485int nla_append(struct sk_buff *skb, int attrlen, const void *data)
486{
487	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
488		return -EMSGSIZE;
489
490	memcpy(skb_put(skb, attrlen), data, attrlen);
491	return 0;
492}
493EXPORT_SYMBOL(nla_append);
494#endif
495
496EXPORT_SYMBOL(nla_validate);
497EXPORT_SYMBOL(nla_policy_len);
498EXPORT_SYMBOL(nla_parse);
499EXPORT_SYMBOL(nla_find);
500EXPORT_SYMBOL(nla_strlcpy);
501EXPORT_SYMBOL(nla_memcpy);
502EXPORT_SYMBOL(nla_memcmp);
503EXPORT_SYMBOL(nla_strcmp);
v3.15
  1/*
  2 * NETLINK      Netlink attributes
  3 *
  4 * 		Authors:	Thomas Graf <tgraf@suug.ch>
  5 * 				Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  6 */
  7
  8#include <linux/export.h>
  9#include <linux/kernel.h>
 10#include <linux/errno.h>
 11#include <linux/jiffies.h>
 12#include <linux/netdevice.h>
 13#include <linux/skbuff.h>
 14#include <linux/string.h>
 15#include <linux/types.h>
 16#include <net/netlink.h>
 17
 18static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = {
 19	[NLA_U8]	= sizeof(u8),
 20	[NLA_U16]	= sizeof(u16),
 21	[NLA_U32]	= sizeof(u32),
 22	[NLA_U64]	= sizeof(u64),
 23	[NLA_MSECS]	= sizeof(u64),
 24	[NLA_NESTED]	= NLA_HDRLEN,
 25	[NLA_S8]	= sizeof(s8),
 26	[NLA_S16]	= sizeof(s16),
 27	[NLA_S32]	= sizeof(s32),
 28	[NLA_S64]	= sizeof(s64),
 29};
 30
 31static int validate_nla(const struct nlattr *nla, int maxtype,
 32			const struct nla_policy *policy)
 33{
 34	const struct nla_policy *pt;
 35	int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
 36
 37	if (type <= 0 || type > maxtype)
 38		return 0;
 39
 40	pt = &policy[type];
 41
 42	BUG_ON(pt->type > NLA_TYPE_MAX);
 43
 44	switch (pt->type) {
 45	case NLA_FLAG:
 46		if (attrlen > 0)
 47			return -ERANGE;
 48		break;
 49
 50	case NLA_NUL_STRING:
 51		if (pt->len)
 52			minlen = min_t(int, attrlen, pt->len + 1);
 53		else
 54			minlen = attrlen;
 55
 56		if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
 57			return -EINVAL;
 58		/* fall through */
 59
 60	case NLA_STRING:
 61		if (attrlen < 1)
 62			return -ERANGE;
 63
 64		if (pt->len) {
 65			char *buf = nla_data(nla);
 66
 67			if (buf[attrlen - 1] == '\0')
 68				attrlen--;
 69
 70			if (attrlen > pt->len)
 71				return -ERANGE;
 72		}
 73		break;
 74
 75	case NLA_BINARY:
 76		if (pt->len && attrlen > pt->len)
 77			return -ERANGE;
 78		break;
 79
 80	case NLA_NESTED_COMPAT:
 81		if (attrlen < pt->len)
 82			return -ERANGE;
 83		if (attrlen < NLA_ALIGN(pt->len))
 84			break;
 85		if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
 86			return -ERANGE;
 87		nla = nla_data(nla) + NLA_ALIGN(pt->len);
 88		if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
 89			return -ERANGE;
 90		break;
 91	case NLA_NESTED:
 92		/* a nested attributes is allowed to be empty; if its not,
 93		 * it must have a size of at least NLA_HDRLEN.
 94		 */
 95		if (attrlen == 0)
 96			break;
 97	default:
 98		if (pt->len)
 99			minlen = pt->len;
100		else if (pt->type != NLA_UNSPEC)
101			minlen = nla_attr_minlen[pt->type];
102
103		if (attrlen < minlen)
104			return -ERANGE;
105	}
106
107	return 0;
108}
109
110/**
111 * nla_validate - Validate a stream of attributes
112 * @head: head of attribute stream
113 * @len: length of attribute stream
114 * @maxtype: maximum attribute type to be expected
115 * @policy: validation policy
116 *
117 * Validates all attributes in the specified attribute stream against the
118 * specified policy. Attributes with a type exceeding maxtype will be
119 * ignored. See documenation of struct nla_policy for more details.
120 *
121 * Returns 0 on success or a negative error code.
122 */
123int nla_validate(const struct nlattr *head, int len, int maxtype,
124		 const struct nla_policy *policy)
125{
126	const struct nlattr *nla;
127	int rem, err;
128
129	nla_for_each_attr(nla, head, len, rem) {
130		err = validate_nla(nla, maxtype, policy);
131		if (err < 0)
132			goto errout;
133	}
134
135	err = 0;
136errout:
137	return err;
138}
139
140/**
141 * nla_policy_len - Determin the max. length of a policy
142 * @policy: policy to use
143 * @n: number of policies
144 *
145 * Determines the max. length of the policy.  It is currently used
146 * to allocated Netlink buffers roughly the size of the actual
147 * message.
148 *
149 * Returns 0 on success or a negative error code.
150 */
151int
152nla_policy_len(const struct nla_policy *p, int n)
153{
154	int i, len = 0;
155
156	for (i = 0; i < n; i++, p++) {
157		if (p->len)
158			len += nla_total_size(p->len);
159		else if (nla_attr_minlen[p->type])
160			len += nla_total_size(nla_attr_minlen[p->type]);
161	}
162
163	return len;
164}
165
166/**
167 * nla_parse - Parse a stream of attributes into a tb buffer
168 * @tb: destination array with maxtype+1 elements
169 * @maxtype: maximum attribute type to be expected
170 * @head: head of attribute stream
171 * @len: length of attribute stream
172 * @policy: validation policy
173 *
174 * Parses a stream of attributes and stores a pointer to each attribute in
175 * the tb array accessible via the attribute type. Attributes with a type
176 * exceeding maxtype will be silently ignored for backwards compatibility
177 * reasons. policy may be set to NULL if no validation is required.
178 *
179 * Returns 0 on success or a negative error code.
180 */
181int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
182	      int len, const struct nla_policy *policy)
183{
184	const struct nlattr *nla;
185	int rem, err;
186
187	memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
188
189	nla_for_each_attr(nla, head, len, rem) {
190		u16 type = nla_type(nla);
191
192		if (type > 0 && type <= maxtype) {
193			if (policy) {
194				err = validate_nla(nla, maxtype, policy);
195				if (err < 0)
196					goto errout;
197			}
198
199			tb[type] = (struct nlattr *)nla;
200		}
201	}
202
203	if (unlikely(rem > 0))
204		pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
205				    rem, current->comm);
206
207	err = 0;
208errout:
209	return err;
210}
211
212/**
213 * nla_find - Find a specific attribute in a stream of attributes
214 * @head: head of attribute stream
215 * @len: length of attribute stream
216 * @attrtype: type of attribute to look for
217 *
218 * Returns the first attribute in the stream matching the specified type.
219 */
220struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
221{
222	const struct nlattr *nla;
223	int rem;
224
225	nla_for_each_attr(nla, head, len, rem)
226		if (nla_type(nla) == attrtype)
227			return (struct nlattr *)nla;
228
229	return NULL;
230}
231
232/**
233 * nla_strlcpy - Copy string attribute payload into a sized buffer
234 * @dst: where to copy the string to
235 * @nla: attribute to copy the string from
236 * @dstsize: size of destination buffer
237 *
238 * Copies at most dstsize - 1 bytes into the destination buffer.
239 * The result is always a valid NUL-terminated string. Unlike
240 * strlcpy the destination buffer is always padded out.
241 *
242 * Returns the length of the source buffer.
243 */
244size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
245{
246	size_t srclen = nla_len(nla);
247	char *src = nla_data(nla);
248
249	if (srclen > 0 && src[srclen - 1] == '\0')
250		srclen--;
251
252	if (dstsize > 0) {
253		size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
254
255		memset(dst, 0, dstsize);
256		memcpy(dst, src, len);
257	}
258
259	return srclen;
260}
261
262/**
263 * nla_memcpy - Copy a netlink attribute into another memory area
264 * @dest: where to copy to memcpy
265 * @src: netlink attribute to copy from
266 * @count: size of the destination area
267 *
268 * Note: The number of bytes copied is limited by the length of
269 *       attribute's payload. memcpy
270 *
271 * Returns the number of bytes copied.
272 */
273int nla_memcpy(void *dest, const struct nlattr *src, int count)
274{
275	int minlen = min_t(int, count, nla_len(src));
276
277	memcpy(dest, nla_data(src), minlen);
278
279	return minlen;
280}
281
282/**
283 * nla_memcmp - Compare an attribute with sized memory area
284 * @nla: netlink attribute
285 * @data: memory area
286 * @size: size of memory area
287 */
288int nla_memcmp(const struct nlattr *nla, const void *data,
289			     size_t size)
290{
291	int d = nla_len(nla) - size;
292
293	if (d == 0)
294		d = memcmp(nla_data(nla), data, size);
295
296	return d;
297}
298
299/**
300 * nla_strcmp - Compare a string attribute against a string
301 * @nla: netlink string attribute
302 * @str: another string
303 */
304int nla_strcmp(const struct nlattr *nla, const char *str)
305{
306	int len = strlen(str);
307	char *buf = nla_data(nla);
308	int attrlen = nla_len(nla);
309	int d;
310
311	if (attrlen > 0 && buf[attrlen - 1] == '\0')
312		attrlen--;
313
314	d = attrlen - len;
315	if (d == 0)
316		d = memcmp(nla_data(nla), str, len);
317
318	return d;
319}
320
321#ifdef CONFIG_NET
322/**
323 * __nla_reserve - reserve room for attribute on the skb
324 * @skb: socket buffer to reserve room on
325 * @attrtype: attribute type
326 * @attrlen: length of attribute payload
327 *
328 * Adds a netlink attribute header to a socket buffer and reserves
329 * room for the payload but does not copy it.
330 *
331 * The caller is responsible to ensure that the skb provides enough
332 * tailroom for the attribute header and payload.
333 */
334struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
335{
336	struct nlattr *nla;
337
338	nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
339	nla->nla_type = attrtype;
340	nla->nla_len = nla_attr_size(attrlen);
341
342	memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
343
344	return nla;
345}
346EXPORT_SYMBOL(__nla_reserve);
347
348/**
349 * __nla_reserve_nohdr - reserve room for attribute without header
350 * @skb: socket buffer to reserve room on
351 * @attrlen: length of attribute payload
352 *
353 * Reserves room for attribute payload without a header.
354 *
355 * The caller is responsible to ensure that the skb provides enough
356 * tailroom for the payload.
357 */
358void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
359{
360	void *start;
361
362	start = skb_put(skb, NLA_ALIGN(attrlen));
363	memset(start, 0, NLA_ALIGN(attrlen));
364
365	return start;
366}
367EXPORT_SYMBOL(__nla_reserve_nohdr);
368
369/**
370 * nla_reserve - reserve room for attribute on the skb
371 * @skb: socket buffer to reserve room on
372 * @attrtype: attribute type
373 * @attrlen: length of attribute payload
374 *
375 * Adds a netlink attribute header to a socket buffer and reserves
376 * room for the payload but does not copy it.
377 *
378 * Returns NULL if the tailroom of the skb is insufficient to store
379 * the attribute header and payload.
380 */
381struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
382{
383	if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
384		return NULL;
385
386	return __nla_reserve(skb, attrtype, attrlen);
387}
388EXPORT_SYMBOL(nla_reserve);
389
390/**
391 * nla_reserve_nohdr - reserve room for attribute without header
392 * @skb: socket buffer to reserve room on
393 * @attrlen: length of attribute payload
394 *
395 * Reserves room for attribute payload without a header.
396 *
397 * Returns NULL if the tailroom of the skb is insufficient to store
398 * the attribute payload.
399 */
400void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
401{
402	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
403		return NULL;
404
405	return __nla_reserve_nohdr(skb, attrlen);
406}
407EXPORT_SYMBOL(nla_reserve_nohdr);
408
409/**
410 * __nla_put - Add a netlink attribute to a socket buffer
411 * @skb: socket buffer to add attribute to
412 * @attrtype: attribute type
413 * @attrlen: length of attribute payload
414 * @data: head of attribute payload
415 *
416 * The caller is responsible to ensure that the skb provides enough
417 * tailroom for the attribute header and payload.
418 */
419void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
420			     const void *data)
421{
422	struct nlattr *nla;
423
424	nla = __nla_reserve(skb, attrtype, attrlen);
425	memcpy(nla_data(nla), data, attrlen);
426}
427EXPORT_SYMBOL(__nla_put);
428
429/**
430 * __nla_put_nohdr - Add a netlink attribute without header
431 * @skb: socket buffer to add attribute to
432 * @attrlen: length of attribute payload
433 * @data: head of attribute payload
434 *
435 * The caller is responsible to ensure that the skb provides enough
436 * tailroom for the attribute payload.
437 */
438void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
439{
440	void *start;
441
442	start = __nla_reserve_nohdr(skb, attrlen);
443	memcpy(start, data, attrlen);
444}
445EXPORT_SYMBOL(__nla_put_nohdr);
446
447/**
448 * nla_put - Add a netlink attribute to a socket buffer
449 * @skb: socket buffer to add attribute to
450 * @attrtype: attribute type
451 * @attrlen: length of attribute payload
452 * @data: head of attribute payload
453 *
454 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
455 * the attribute header and payload.
456 */
457int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
458{
459	if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
460		return -EMSGSIZE;
461
462	__nla_put(skb, attrtype, attrlen, data);
463	return 0;
464}
465EXPORT_SYMBOL(nla_put);
466
467/**
468 * nla_put_nohdr - Add a netlink attribute without header
469 * @skb: socket buffer to add attribute to
470 * @attrlen: length of attribute payload
471 * @data: head of attribute payload
472 *
473 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
474 * the attribute payload.
475 */
476int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
477{
478	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
479		return -EMSGSIZE;
480
481	__nla_put_nohdr(skb, attrlen, data);
482	return 0;
483}
484EXPORT_SYMBOL(nla_put_nohdr);
485
486/**
487 * nla_append - Add a netlink attribute without header or padding
488 * @skb: socket buffer to add attribute to
489 * @attrlen: length of attribute payload
490 * @data: head of attribute payload
491 *
492 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
493 * the attribute payload.
494 */
495int nla_append(struct sk_buff *skb, int attrlen, const void *data)
496{
497	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
498		return -EMSGSIZE;
499
500	memcpy(skb_put(skb, attrlen), data, attrlen);
501	return 0;
502}
503EXPORT_SYMBOL(nla_append);
504#endif
505
506EXPORT_SYMBOL(nla_validate);
507EXPORT_SYMBOL(nla_policy_len);
508EXPORT_SYMBOL(nla_parse);
509EXPORT_SYMBOL(nla_find);
510EXPORT_SYMBOL(nla_strlcpy);
511EXPORT_SYMBOL(nla_memcpy);
512EXPORT_SYMBOL(nla_memcmp);
513EXPORT_SYMBOL(nla_strcmp);