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
);
141 * nla_policy_len - Determin the max. length of a policy
142 * @policy: policy to use
143 * @n: number of policies
145 * Determines the max. length of the policy. It is currently used
146 * to allocated Netlink buffers roughly the size of the actual
149 * Returns 0 on success or a negative error code.
152 nla_policy_len(const struct nla_policy
*p
, int n
)
156 for (i
= 0; i
< n
; i
++, p
++) {
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
]);
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
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.
179 * Returns 0 on success or a negative error code.
181 int nla_parse(struct nlattr
**tb
, int maxtype
, const struct nlattr
*head
,
182 int len
, const struct nla_policy
*policy
)
184 const struct nlattr
*nla
;
187 memset(tb
, 0, sizeof(struct nlattr
*) * (maxtype
+ 1));
189 nla_for_each_attr(nla
, head
, len
, rem
) {
190 u16 type
= nla_type(nla
);
192 if (type
> 0 && type
<= maxtype
) {
194 err
= validate_nla(nla
, maxtype
, policy
);
199 tb
[type
] = (struct nlattr
*)nla
;
203 if (unlikely(rem
> 0))
204 printk(KERN_WARNING
"netlink: %d bytes leftover after parsing "
205 "attributes.\n", rem
);
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
218 * Returns the first attribute in the stream matching the specified type.
220 struct nlattr
*nla_find(const struct nlattr
*head
, int len
, int attrtype
)
222 const struct nlattr
*nla
;
225 nla_for_each_attr(nla
, head
, len
, rem
)
226 if (nla_type(nla
) == attrtype
)
227 return (struct nlattr
*)nla
;
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
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.
242 * Returns the length of the source buffer.
244 size_t nla_strlcpy(char *dst
, const struct nlattr
*nla
, size_t dstsize
)
246 size_t srclen
= nla_len(nla
);
247 char *src
= nla_data(nla
);
249 if (srclen
> 0 && src
[srclen
- 1] == '\0')
253 size_t len
= (srclen
>= dstsize
) ? dstsize
- 1 : srclen
;
255 memset(dst
, 0, dstsize
);
256 memcpy(dst
, src
, len
);
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
268 * Note: The number of bytes copied is limited by the length of
269 * attribute's payload. memcpy
271 * Returns the number of bytes copied.
273 int nla_memcpy(void *dest
, const struct nlattr
*src
, int count
)
275 int minlen
= min_t(int, count
, nla_len(src
));
277 memcpy(dest
, nla_data(src
), minlen
);
283 * nla_memcmp - Compare an attribute with sized memory area
284 * @nla: netlink attribute
286 * @size: size of memory area
288 int nla_memcmp(const struct nlattr
*nla
, const void *data
,
291 int d
= nla_len(nla
) - size
;
294 d
= memcmp(nla_data(nla
), data
, size
);
300 * nla_strcmp - Compare a string attribute against a string
301 * @nla: netlink string attribute
302 * @str: another string
304 int nla_strcmp(const struct nlattr
*nla
, const char *str
)
306 int len
= strlen(str
);
307 char *buf
= nla_data(nla
);
308 int attrlen
= nla_len(nla
);
311 if (attrlen
> 0 && buf
[attrlen
- 1] == '\0')
316 d
= memcmp(nla_data(nla
), str
, len
);
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
328 * Adds a netlink attribute header to a socket buffer and reserves
329 * room for the payload but does not copy it.
331 * The caller is responsible to ensure that the skb provides enough
332 * tailroom for the attribute header and payload.
334 struct nlattr
*__nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
338 nla
= (struct nlattr
*) skb_put(skb
, nla_total_size(attrlen
));
339 nla
->nla_type
= attrtype
;
340 nla
->nla_len
= nla_attr_size(attrlen
);
342 memset((unsigned char *) nla
+ nla
->nla_len
, 0, nla_padlen(attrlen
));
346 EXPORT_SYMBOL(__nla_reserve
);
349 * __nla_reserve_nohdr - reserve room for attribute without header
350 * @skb: socket buffer to reserve room on
351 * @attrlen: length of attribute payload
353 * Reserves room for attribute payload without a header.
355 * The caller is responsible to ensure that the skb provides enough
356 * tailroom for the payload.
358 void *__nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
362 start
= skb_put(skb
, NLA_ALIGN(attrlen
));
363 memset(start
, 0, NLA_ALIGN(attrlen
));
367 EXPORT_SYMBOL(__nla_reserve_nohdr
);
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
375 * Adds a netlink attribute header to a socket buffer and reserves
376 * room for the payload but does not copy it.
378 * Returns NULL if the tailroom of the skb is insufficient to store
379 * the attribute header and payload.
381 struct nlattr
*nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
383 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
386 return __nla_reserve(skb
, attrtype
, attrlen
);
388 EXPORT_SYMBOL(nla_reserve
);
391 * nla_reserve_nohdr - reserve room for attribute without header
392 * @skb: socket buffer to reserve room on
393 * @attrlen: length of attribute payload
395 * Reserves room for attribute payload without a header.
397 * Returns NULL if the tailroom of the skb is insufficient to store
398 * the attribute payload.
400 void *nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
402 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
405 return __nla_reserve_nohdr(skb
, attrlen
);
407 EXPORT_SYMBOL(nla_reserve_nohdr
);
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
416 * The caller is responsible to ensure that the skb provides enough
417 * tailroom for the attribute header and payload.
419 void __nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
,
424 nla
= __nla_reserve(skb
, attrtype
, attrlen
);
425 memcpy(nla_data(nla
), data
, attrlen
);
427 EXPORT_SYMBOL(__nla_put
);
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
435 * The caller is responsible to ensure that the skb provides enough
436 * tailroom for the attribute payload.
438 void __nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
442 start
= __nla_reserve_nohdr(skb
, attrlen
);
443 memcpy(start
, data
, attrlen
);
445 EXPORT_SYMBOL(__nla_put_nohdr
);
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
454 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
455 * the attribute header and payload.
457 int nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
459 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
462 __nla_put(skb
, attrtype
, attrlen
, data
);
465 EXPORT_SYMBOL(nla_put
);
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
473 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
474 * the attribute payload.
476 int nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
478 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
481 __nla_put_nohdr(skb
, attrlen
, data
);
484 EXPORT_SYMBOL(nla_put_nohdr
);
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
492 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
493 * the attribute payload.
495 int nla_append(struct sk_buff
*skb
, int attrlen
, const void *data
)
497 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
500 memcpy(skb_put(skb
, attrlen
), data
, attrlen
);
503 EXPORT_SYMBOL(nla_append
);
506 EXPORT_SYMBOL(nla_validate
);
507 EXPORT_SYMBOL(nla_policy_len
);
508 EXPORT_SYMBOL(nla_parse
);
509 EXPORT_SYMBOL(nla_find
);
510 EXPORT_SYMBOL(nla_strlcpy
);
511 EXPORT_SYMBOL(nla_memcpy
);
512 EXPORT_SYMBOL(nla_memcmp
);
513 EXPORT_SYMBOL(nla_strcmp
);