2 * NETLINK Netlink attributes
4 * Authors: Thomas Graf <tgraf@suug.ch>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 #include <linux/module.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 u16 nla_attr_minlen[NLA_TYPE_MAX+1] __read_mostly = {
19 [NLA_U8] = sizeof(u8),
20 [NLA_U16] = sizeof(u16),
21 [NLA_U32] = sizeof(u32),
22 [NLA_U64] = sizeof(u64),
23 [NLA_NESTED] = NLA_HDRLEN,
26 static int validate_nla(struct nlattr *nla, int maxtype,
27 const struct nla_policy *policy)
29 const struct nla_policy *pt;
30 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
32 if (type <= 0 || type > maxtype)
37 BUG_ON(pt->type > NLA_TYPE_MAX);
47 minlen = min_t(int, attrlen, pt->len + 1);
51 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
60 char *buf = nla_data(nla);
62 if (buf[attrlen - 1] == '\0')
65 if (attrlen > pt->len)
71 if (pt->len && attrlen > pt->len)
75 case NLA_NESTED_COMPAT:
76 if (attrlen < pt->len)
78 if (attrlen < NLA_ALIGN(pt->len))
80 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
82 nla = nla_data(nla) + NLA_ALIGN(pt->len);
83 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
89 else if (pt->type != NLA_UNSPEC)
90 minlen = nla_attr_minlen[pt->type];
100 * nla_validate - Validate a stream of attributes
101 * @head: head of attribute stream
102 * @len: length of attribute stream
103 * @maxtype: maximum attribute type to be expected
104 * @policy: validation policy
106 * Validates all attributes in the specified attribute stream against the
107 * specified policy. Attributes with a type exceeding maxtype will be
108 * ignored. See documenation of struct nla_policy for more details.
110 * Returns 0 on success or a negative error code.
112 int nla_validate(struct nlattr *head, int len, int maxtype,
113 const struct nla_policy *policy)
118 nla_for_each_attr(nla, head, len, rem) {
119 err = validate_nla(nla, maxtype, policy);
130 * nla_parse - Parse a stream of attributes into a tb buffer
131 * @tb: destination array with maxtype+1 elements
132 * @maxtype: maximum attribute type to be expected
133 * @head: head of attribute stream
134 * @len: length of attribute stream
136 * Parses a stream of attributes and stores a pointer to each attribute in
137 * the tb array accessable via the attribute type. Attributes with a type
138 * exceeding maxtype will be silently ignored for backwards compatibility
139 * reasons. policy may be set to NULL if no validation is required.
141 * Returns 0 on success or a negative error code.
143 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
144 const struct nla_policy *policy)
149 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
151 nla_for_each_attr(nla, head, len, rem) {
152 u16 type = nla_type(nla);
154 if (type > 0 && type <= maxtype) {
156 err = validate_nla(nla, maxtype, policy);
165 if (unlikely(rem > 0))
166 printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
167 "attributes.\n", rem);
175 * nla_find - Find a specific attribute in a stream of attributes
176 * @head: head of attribute stream
177 * @len: length of attribute stream
178 * @attrtype: type of attribute to look for
180 * Returns the first attribute in the stream matching the specified type.
182 struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
187 nla_for_each_attr(nla, head, len, rem)
188 if (nla_type(nla) == attrtype)
195 * nla_strlcpy - Copy string attribute payload into a sized buffer
196 * @dst: where to copy the string to
197 * @src: attribute to copy the string from
198 * @dstsize: size of destination buffer
200 * Copies at most dstsize - 1 bytes into the destination buffer.
201 * The result is always a valid NUL-terminated string. Unlike
202 * strlcpy the destination buffer is always padded out.
204 * Returns the length of the source buffer.
206 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
208 size_t srclen = nla_len(nla);
209 char *src = nla_data(nla);
211 if (srclen > 0 && src[srclen - 1] == '\0')
215 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
217 memset(dst, 0, dstsize);
218 memcpy(dst, src, len);
225 * nla_memcpy - Copy a netlink attribute into another memory area
226 * @dest: where to copy to memcpy
227 * @src: netlink attribute to copy from
228 * @count: size of the destination area
230 * Note: The number of bytes copied is limited by the length of
231 * attribute's payload. memcpy
233 * Returns the number of bytes copied.
235 int nla_memcpy(void *dest, struct nlattr *src, int count)
237 int minlen = min_t(int, count, nla_len(src));
239 memcpy(dest, nla_data(src), minlen);
245 * nla_memcmp - Compare an attribute with sized memory area
246 * @nla: netlink attribute
248 * @size: size of memory area
250 int nla_memcmp(const struct nlattr *nla, const void *data,
253 int d = nla_len(nla) - size;
256 d = memcmp(nla_data(nla), data, size);
262 * nla_strcmp - Compare a string attribute against a string
263 * @nla: netlink string attribute
264 * @str: another string
266 int nla_strcmp(const struct nlattr *nla, const char *str)
268 int len = strlen(str) + 1;
269 int d = nla_len(nla) - len;
272 d = memcmp(nla_data(nla), str, len);
278 * __nla_reserve - reserve room for attribute on the skb
279 * @skb: socket buffer to reserve room on
280 * @attrtype: attribute type
281 * @attrlen: length of attribute payload
283 * Adds a netlink attribute header to a socket buffer and reserves
284 * room for the payload but does not copy it.
286 * The caller is responsible to ensure that the skb provides enough
287 * tailroom for the attribute header and payload.
289 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
293 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
294 nla->nla_type = attrtype;
295 nla->nla_len = nla_attr_size(attrlen);
297 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
303 * __nla_reserve_nohdr - reserve room for attribute without header
304 * @skb: socket buffer to reserve room on
305 * @attrlen: length of attribute payload
307 * Reserves room for attribute payload without a header.
309 * The caller is responsible to ensure that the skb provides enough
310 * tailroom for the payload.
312 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
316 start = skb_put(skb, NLA_ALIGN(attrlen));
317 memset(start, 0, NLA_ALIGN(attrlen));
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 * Returns NULL if the tailroom of the skb is insufficient to store
332 * the attribute header and payload.
334 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
336 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
339 return __nla_reserve(skb, attrtype, attrlen);
343 * nla_reserve - reserve room for attribute without header
344 * @skb: socket buffer to reserve room on
345 * @len: length of attribute payload
347 * Reserves room for attribute payload without a header.
349 * Returns NULL if the tailroom of the skb is insufficient to store
350 * the attribute payload.
352 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
354 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
357 return __nla_reserve_nohdr(skb, attrlen);
361 * __nla_put - Add a netlink attribute to a socket buffer
362 * @skb: socket buffer to add attribute to
363 * @attrtype: attribute type
364 * @attrlen: length of attribute payload
365 * @data: head of attribute payload
367 * The caller is responsible to ensure that the skb provides enough
368 * tailroom for the attribute header and payload.
370 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
375 nla = __nla_reserve(skb, attrtype, attrlen);
376 memcpy(nla_data(nla), data, attrlen);
380 * __nla_put_nohdr - Add a netlink attribute without header
381 * @skb: socket buffer to add attribute to
382 * @attrlen: length of attribute payload
383 * @data: head of attribute payload
385 * The caller is responsible to ensure that the skb provides enough
386 * tailroom for the attribute payload.
388 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
392 start = __nla_reserve_nohdr(skb, attrlen);
393 memcpy(start, data, attrlen);
397 * nla_put - Add a netlink attribute to a socket buffer
398 * @skb: socket buffer to add attribute to
399 * @attrtype: attribute type
400 * @attrlen: length of attribute payload
401 * @data: head of attribute payload
403 * Returns -1 if the tailroom of the skb is insufficient to store
404 * the attribute header and payload.
406 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
408 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
411 __nla_put(skb, attrtype, attrlen, data);
416 * nla_put_nohdr - Add a netlink attribute without header
417 * @skb: socket buffer to add attribute to
418 * @attrlen: length of attribute payload
419 * @data: head of attribute payload
421 * Returns -1 if the tailroom of the skb is insufficient to store
422 * the attribute payload.
424 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
426 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
429 __nla_put_nohdr(skb, attrlen, data);
434 * nla_append - Add a netlink attribute without header or padding
435 * @skb: socket buffer to add attribute to
436 * @attrlen: length of attribute payload
437 * @data: head of attribute payload
439 * Returns -1 if the tailroom of the skb is insufficient to store
440 * the attribute payload.
442 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
444 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
447 memcpy(skb_put(skb, attrlen), data, attrlen);
451 EXPORT_SYMBOL(nla_validate);
452 EXPORT_SYMBOL(nla_parse);
453 EXPORT_SYMBOL(nla_find);
454 EXPORT_SYMBOL(nla_strlcpy);
455 EXPORT_SYMBOL(__nla_reserve);
456 EXPORT_SYMBOL(__nla_reserve_nohdr);
457 EXPORT_SYMBOL(nla_reserve);
458 EXPORT_SYMBOL(nla_reserve_nohdr);
459 EXPORT_SYMBOL(__nla_put);
460 EXPORT_SYMBOL(__nla_put_nohdr);
461 EXPORT_SYMBOL(nla_put);
462 EXPORT_SYMBOL(nla_put_nohdr);
463 EXPORT_SYMBOL(nla_memcpy);
464 EXPORT_SYMBOL(nla_memcmp);
465 EXPORT_SYMBOL(nla_strcmp);
466 EXPORT_SYMBOL(nla_append);