]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - lib/nlattr.c
2 * NETLINK Netlink attributes
4 * Authors: Thomas Graf <tgraf@suug.ch>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
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>
18 static 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
),
31 static int validate_nla(const struct nlattr
*nla
, int maxtype
,
32 const struct nla_policy
*policy
)
34 const struct nla_policy
*pt
;
35 int minlen
= 0, attrlen
= nla_len(nla
), type
= nla_type(nla
);
37 if (type
<= 0 || type
> maxtype
)
42 BUG_ON(pt
->type
> NLA_TYPE_MAX
);
52 minlen
= min_t(int, attrlen
, pt
->len
+ 1);
56 if (!minlen
|| memchr(nla_data(nla
), '\0', minlen
) == NULL
)
65 char *buf
= nla_data(nla
);
67 if (buf
[attrlen
- 1] == '\0')
70 if (attrlen
> pt
->len
)
76 if (pt
->len
&& attrlen
> pt
->len
)
80 case NLA_NESTED_COMPAT
:
81 if (attrlen
< pt
->len
)
83 if (attrlen
< NLA_ALIGN(pt
->len
))
85 if (attrlen
< NLA_ALIGN(pt
->len
) + NLA_HDRLEN
)
87 nla
= nla_data(nla
) + NLA_ALIGN(pt
->len
);
88 if (attrlen
< NLA_ALIGN(pt
->len
) + NLA_HDRLEN
+ nla_len(nla
))
92 /* a nested attributes is allowed to be empty; if its not,
93 * it must have a size of at least NLA_HDRLEN.
100 else if (pt
->type
!= NLA_UNSPEC
)
101 minlen
= nla_attr_minlen
[pt
->type
];
103 if (attrlen
< minlen
)
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
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.
121 * Returns 0 on success or a negative error code.
123 int nla_validate(const struct nlattr
*head
, int len
, int maxtype
,
124 const struct nla_policy
*policy
)
126 const struct nlattr
*nla
;
129 nla_for_each_attr(nla
, head
, len
, rem
) {
130 err
= validate_nla(nla
, maxtype
, policy
);
139 EXPORT_SYMBOL(nla_validate
);
142 * nla_policy_len - Determin the max. length of a policy
143 * @policy: policy to use
144 * @n: number of policies
146 * Determines the max. length of the policy. It is currently used
147 * to allocated Netlink buffers roughly the size of the actual
150 * Returns 0 on success or a negative error code.
153 nla_policy_len(const struct nla_policy
*p
, int n
)
157 for (i
= 0; i
< n
; i
++, p
++) {
159 len
+= nla_total_size(p
->len
);
160 else if (nla_attr_minlen
[p
->type
])
161 len
+= nla_total_size(nla_attr_minlen
[p
->type
]);
166 EXPORT_SYMBOL(nla_policy_len
);
169 * nla_parse - Parse a stream of attributes into a tb buffer
170 * @tb: destination array with maxtype+1 elements
171 * @maxtype: maximum attribute type to be expected
172 * @head: head of attribute stream
173 * @len: length of attribute stream
174 * @policy: validation policy
176 * Parses a stream of attributes and stores a pointer to each attribute in
177 * the tb array accessible via the attribute type. Attributes with a type
178 * exceeding maxtype will be silently ignored for backwards compatibility
179 * reasons. policy may be set to NULL if no validation is required.
181 * Returns 0 on success or a negative error code.
183 int nla_parse(struct nlattr
**tb
, int maxtype
, const struct nlattr
*head
,
184 int len
, const struct nla_policy
*policy
)
186 const struct nlattr
*nla
;
189 memset(tb
, 0, sizeof(struct nlattr
*) * (maxtype
+ 1));
191 nla_for_each_attr(nla
, head
, len
, rem
) {
192 u16 type
= nla_type(nla
);
194 if (type
> 0 && type
<= maxtype
) {
196 err
= validate_nla(nla
, maxtype
, policy
);
201 tb
[type
] = (struct nlattr
*)nla
;
205 if (unlikely(rem
> 0))
206 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
213 EXPORT_SYMBOL(nla_parse
);
216 * nla_find - Find a specific attribute in a stream of attributes
217 * @head: head of attribute stream
218 * @len: length of attribute stream
219 * @attrtype: type of attribute to look for
221 * Returns the first attribute in the stream matching the specified type.
223 struct nlattr
*nla_find(const struct nlattr
*head
, int len
, int attrtype
)
225 const struct nlattr
*nla
;
228 nla_for_each_attr(nla
, head
, len
, rem
)
229 if (nla_type(nla
) == attrtype
)
230 return (struct nlattr
*)nla
;
234 EXPORT_SYMBOL(nla_find
);
237 * nla_strlcpy - Copy string attribute payload into a sized buffer
238 * @dst: where to copy the string to
239 * @nla: attribute to copy the string from
240 * @dstsize: size of destination buffer
242 * Copies at most dstsize - 1 bytes into the destination buffer.
243 * The result is always a valid NUL-terminated string. Unlike
244 * strlcpy the destination buffer is always padded out.
246 * Returns the length of the source buffer.
248 size_t nla_strlcpy(char *dst
, const struct nlattr
*nla
, size_t dstsize
)
250 size_t srclen
= nla_len(nla
);
251 char *src
= nla_data(nla
);
253 if (srclen
> 0 && src
[srclen
- 1] == '\0')
257 size_t len
= (srclen
>= dstsize
) ? dstsize
- 1 : srclen
;
259 memset(dst
, 0, dstsize
);
260 memcpy(dst
, src
, len
);
265 EXPORT_SYMBOL(nla_strlcpy
);
268 * nla_memcpy - Copy a netlink attribute into another memory area
269 * @dest: where to copy to memcpy
270 * @src: netlink attribute to copy from
271 * @count: size of the destination area
273 * Note: The number of bytes copied is limited by the length of
274 * attribute's payload. memcpy
276 * Returns the number of bytes copied.
278 int nla_memcpy(void *dest
, const struct nlattr
*src
, int count
)
280 int minlen
= min_t(int, count
, nla_len(src
));
282 memcpy(dest
, nla_data(src
), minlen
);
286 EXPORT_SYMBOL(nla_memcpy
);
289 * nla_memcmp - Compare an attribute with sized memory area
290 * @nla: netlink attribute
292 * @size: size of memory area
294 int nla_memcmp(const struct nlattr
*nla
, const void *data
,
297 int d
= nla_len(nla
) - size
;
300 d
= memcmp(nla_data(nla
), data
, size
);
304 EXPORT_SYMBOL(nla_memcmp
);
307 * nla_strcmp - Compare a string attribute against a string
308 * @nla: netlink string attribute
309 * @str: another string
311 int nla_strcmp(const struct nlattr
*nla
, const char *str
)
313 int len
= strlen(str
);
314 char *buf
= nla_data(nla
);
315 int attrlen
= nla_len(nla
);
318 if (attrlen
> 0 && buf
[attrlen
- 1] == '\0')
323 d
= memcmp(nla_data(nla
), str
, len
);
327 EXPORT_SYMBOL(nla_strcmp
);
331 * __nla_reserve - reserve room for attribute on the skb
332 * @skb: socket buffer to reserve room on
333 * @attrtype: attribute type
334 * @attrlen: length of attribute payload
336 * Adds a netlink attribute header to a socket buffer and reserves
337 * room for the payload but does not copy it.
339 * The caller is responsible to ensure that the skb provides enough
340 * tailroom for the attribute header and payload.
342 struct nlattr
*__nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
346 nla
= (struct nlattr
*) skb_put(skb
, nla_total_size(attrlen
));
347 nla
->nla_type
= attrtype
;
348 nla
->nla_len
= nla_attr_size(attrlen
);
350 memset((unsigned char *) nla
+ nla
->nla_len
, 0, nla_padlen(attrlen
));
354 EXPORT_SYMBOL(__nla_reserve
);
357 * __nla_reserve_nohdr - reserve room for attribute without header
358 * @skb: socket buffer to reserve room on
359 * @attrlen: length of attribute payload
361 * Reserves room for attribute payload without a header.
363 * The caller is responsible to ensure that the skb provides enough
364 * tailroom for the payload.
366 void *__nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
370 start
= skb_put(skb
, NLA_ALIGN(attrlen
));
371 memset(start
, 0, NLA_ALIGN(attrlen
));
375 EXPORT_SYMBOL(__nla_reserve_nohdr
);
378 * nla_reserve - reserve room for attribute on the skb
379 * @skb: socket buffer to reserve room on
380 * @attrtype: attribute type
381 * @attrlen: length of attribute payload
383 * Adds a netlink attribute header to a socket buffer and reserves
384 * room for the payload but does not copy it.
386 * Returns NULL if the tailroom of the skb is insufficient to store
387 * the attribute header and payload.
389 struct nlattr
*nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
391 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
394 return __nla_reserve(skb
, attrtype
, attrlen
);
396 EXPORT_SYMBOL(nla_reserve
);
399 * nla_reserve_nohdr - reserve room for attribute without header
400 * @skb: socket buffer to reserve room on
401 * @attrlen: length of attribute payload
403 * Reserves room for attribute payload without a header.
405 * Returns NULL if the tailroom of the skb is insufficient to store
406 * the attribute payload.
408 void *nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
410 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
413 return __nla_reserve_nohdr(skb
, attrlen
);
415 EXPORT_SYMBOL(nla_reserve_nohdr
);
418 * __nla_put - Add a netlink attribute to a socket buffer
419 * @skb: socket buffer to add attribute to
420 * @attrtype: attribute type
421 * @attrlen: length of attribute payload
422 * @data: head of attribute payload
424 * The caller is responsible to ensure that the skb provides enough
425 * tailroom for the attribute header and payload.
427 void __nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
,
432 nla
= __nla_reserve(skb
, attrtype
, attrlen
);
433 memcpy(nla_data(nla
), data
, attrlen
);
435 EXPORT_SYMBOL(__nla_put
);
438 * __nla_put_nohdr - Add a netlink attribute without header
439 * @skb: socket buffer to add attribute to
440 * @attrlen: length of attribute payload
441 * @data: head of attribute payload
443 * The caller is responsible to ensure that the skb provides enough
444 * tailroom for the attribute payload.
446 void __nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
450 start
= __nla_reserve_nohdr(skb
, attrlen
);
451 memcpy(start
, data
, attrlen
);
453 EXPORT_SYMBOL(__nla_put_nohdr
);
456 * nla_put - Add a netlink attribute to a socket buffer
457 * @skb: socket buffer to add attribute to
458 * @attrtype: attribute type
459 * @attrlen: length of attribute payload
460 * @data: head of attribute payload
462 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
463 * the attribute header and payload.
465 int nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
467 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
470 __nla_put(skb
, attrtype
, attrlen
, data
);
473 EXPORT_SYMBOL(nla_put
);
476 * nla_put_nohdr - Add a netlink attribute without header
477 * @skb: socket buffer to add attribute to
478 * @attrlen: length of attribute payload
479 * @data: head of attribute payload
481 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
482 * the attribute payload.
484 int nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
486 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
489 __nla_put_nohdr(skb
, attrlen
, data
);
492 EXPORT_SYMBOL(nla_put_nohdr
);
495 * nla_append - Add a netlink attribute without header or padding
496 * @skb: socket buffer to add attribute to
497 * @attrlen: length of attribute payload
498 * @data: head of attribute payload
500 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
501 * the attribute payload.
503 int nla_append(struct sk_buff
*skb
, int attrlen
, const void *data
)
505 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
508 memcpy(skb_put(skb
, attrlen
), data
, attrlen
);
511 EXPORT_SYMBOL(nla_append
);