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))
87 /* a nested attributes is allowed to be empty; if its not,
88 * it must have a size of at least NLA_HDRLEN.
95 else if (pt->type != NLA_UNSPEC)
96 minlen = nla_attr_minlen[pt->type];
106 * nla_validate - Validate a stream of attributes
107 * @head: head of attribute stream
108 * @len: length of attribute stream
109 * @maxtype: maximum attribute type to be expected
110 * @policy: validation policy
112 * Validates all attributes in the specified attribute stream against the
113 * specified policy. Attributes with a type exceeding maxtype will be
114 * ignored. See documenation of struct nla_policy for more details.
116 * Returns 0 on success or a negative error code.
118 int nla_validate(struct nlattr *head, int len, int maxtype,
119 const struct nla_policy *policy)
124 nla_for_each_attr(nla, head, len, rem) {
125 err = validate_nla(nla, maxtype, policy);
136 * nla_policy_len - Determin the max. length of a policy
137 * @policy: policy to use
138 * @n: number of policies
140 * Determines the max. length of the policy. It is currently used
141 * to allocated Netlink buffers roughly the size of the actual
144 * Returns 0 on success or a negative error code.
147 nla_policy_len(const struct nla_policy *p, int n)
151 for (i = 0; i < n; i++) {
153 len += nla_total_size(p->len);
154 else if (nla_attr_minlen[p->type])
155 len += nla_total_size(nla_attr_minlen[p->type]);
162 * nla_parse - Parse a stream of attributes into a tb buffer
163 * @tb: destination array with maxtype+1 elements
164 * @maxtype: maximum attribute type to be expected
165 * @head: head of attribute stream
166 * @len: length of attribute stream
167 * @policy: validation policy
169 * Parses a stream of attributes and stores a pointer to each attribute in
170 * the tb array accessable via the attribute type. Attributes with a type
171 * exceeding maxtype will be silently ignored for backwards compatibility
172 * reasons. policy may be set to NULL if no validation is required.
174 * Returns 0 on success or a negative error code.
176 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
177 const struct nla_policy *policy)
182 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
184 nla_for_each_attr(nla, head, len, rem) {
185 u16 type = nla_type(nla);
187 if (type > 0 && type <= maxtype) {
189 err = validate_nla(nla, maxtype, policy);
198 if (unlikely(rem > 0))
199 printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
200 "attributes.\n", rem);
208 * nla_find - Find a specific attribute in a stream of attributes
209 * @head: head of attribute stream
210 * @len: length of attribute stream
211 * @attrtype: type of attribute to look for
213 * Returns the first attribute in the stream matching the specified type.
215 struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
220 nla_for_each_attr(nla, head, len, rem)
221 if (nla_type(nla) == attrtype)
228 * nla_strlcpy - Copy string attribute payload into a sized buffer
229 * @dst: where to copy the string to
230 * @nla: attribute to copy the string from
231 * @dstsize: size of destination buffer
233 * Copies at most dstsize - 1 bytes into the destination buffer.
234 * The result is always a valid NUL-terminated string. Unlike
235 * strlcpy the destination buffer is always padded out.
237 * Returns the length of the source buffer.
239 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
241 size_t srclen = nla_len(nla);
242 char *src = nla_data(nla);
244 if (srclen > 0 && src[srclen - 1] == '\0')
248 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
250 memset(dst, 0, dstsize);
251 memcpy(dst, src, len);
258 * nla_memcpy - Copy a netlink attribute into another memory area
259 * @dest: where to copy to memcpy
260 * @src: netlink attribute to copy from
261 * @count: size of the destination area
263 * Note: The number of bytes copied is limited by the length of
264 * attribute's payload. memcpy
266 * Returns the number of bytes copied.
268 int nla_memcpy(void *dest, const struct nlattr *src, int count)
270 int minlen = min_t(int, count, nla_len(src));
272 memcpy(dest, nla_data(src), minlen);
278 * nla_memcmp - Compare an attribute with sized memory area
279 * @nla: netlink attribute
281 * @size: size of memory area
283 int nla_memcmp(const struct nlattr *nla, const void *data,
286 int d = nla_len(nla) - size;
289 d = memcmp(nla_data(nla), data, size);
295 * nla_strcmp - Compare a string attribute against a string
296 * @nla: netlink string attribute
297 * @str: another string
299 int nla_strcmp(const struct nlattr *nla, const char *str)
301 int len = strlen(str) + 1;
302 int d = nla_len(nla) - len;
305 d = memcmp(nla_data(nla), str, len);
311 * __nla_reserve - reserve room for attribute on the skb
312 * @skb: socket buffer to reserve room on
313 * @attrtype: attribute type
314 * @attrlen: length of attribute payload
316 * Adds a netlink attribute header to a socket buffer and reserves
317 * room for the payload but does not copy it.
319 * The caller is responsible to ensure that the skb provides enough
320 * tailroom for the attribute header and payload.
322 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
326 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
327 nla->nla_type = attrtype;
328 nla->nla_len = nla_attr_size(attrlen);
330 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
336 * __nla_reserve_nohdr - reserve room for attribute without header
337 * @skb: socket buffer to reserve room on
338 * @attrlen: length of attribute payload
340 * Reserves room for attribute payload without a header.
342 * The caller is responsible to ensure that the skb provides enough
343 * tailroom for the payload.
345 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
349 start = skb_put(skb, NLA_ALIGN(attrlen));
350 memset(start, 0, NLA_ALIGN(attrlen));
356 * nla_reserve - reserve room for attribute on the skb
357 * @skb: socket buffer to reserve room on
358 * @attrtype: attribute type
359 * @attrlen: length of attribute payload
361 * Adds a netlink attribute header to a socket buffer and reserves
362 * room for the payload but does not copy it.
364 * Returns NULL if the tailroom of the skb is insufficient to store
365 * the attribute header and payload.
367 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
369 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
372 return __nla_reserve(skb, attrtype, attrlen);
376 * nla_reserve_nohdr - reserve room for attribute without header
377 * @skb: socket buffer to reserve room on
378 * @attrlen: length of attribute payload
380 * Reserves room for attribute payload without a header.
382 * Returns NULL if the tailroom of the skb is insufficient to store
383 * the attribute payload.
385 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
387 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
390 return __nla_reserve_nohdr(skb, attrlen);
394 * __nla_put - Add a netlink attribute to a socket buffer
395 * @skb: socket buffer to add attribute to
396 * @attrtype: attribute type
397 * @attrlen: length of attribute payload
398 * @data: head of attribute payload
400 * The caller is responsible to ensure that the skb provides enough
401 * tailroom for the attribute header and payload.
403 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
408 nla = __nla_reserve(skb, attrtype, attrlen);
409 memcpy(nla_data(nla), data, attrlen);
413 * __nla_put_nohdr - Add a netlink attribute without header
414 * @skb: socket buffer to add attribute to
415 * @attrlen: length of attribute payload
416 * @data: head of attribute payload
418 * The caller is responsible to ensure that the skb provides enough
419 * tailroom for the attribute payload.
421 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
425 start = __nla_reserve_nohdr(skb, attrlen);
426 memcpy(start, data, attrlen);
430 * nla_put - Add a netlink attribute to a socket buffer
431 * @skb: socket buffer to add attribute to
432 * @attrtype: attribute type
433 * @attrlen: length of attribute payload
434 * @data: head of attribute payload
436 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
437 * the attribute header and payload.
439 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
441 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
444 __nla_put(skb, attrtype, attrlen, data);
449 * nla_put_nohdr - Add a netlink attribute without header
450 * @skb: socket buffer to add attribute to
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 payload.
457 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
459 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
462 __nla_put_nohdr(skb, attrlen, data);
467 * nla_append - Add a netlink attribute without header or padding
468 * @skb: socket buffer to add attribute to
469 * @attrlen: length of attribute payload
470 * @data: head of attribute payload
472 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
473 * the attribute payload.
475 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
477 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
480 memcpy(skb_put(skb, attrlen), data, attrlen);
484 EXPORT_SYMBOL(nla_validate);
485 EXPORT_SYMBOL(nla_policy_len);
486 EXPORT_SYMBOL(nla_parse);
487 EXPORT_SYMBOL(nla_find);
488 EXPORT_SYMBOL(nla_strlcpy);
489 EXPORT_SYMBOL(__nla_reserve);
490 EXPORT_SYMBOL(__nla_reserve_nohdr);
491 EXPORT_SYMBOL(nla_reserve);
492 EXPORT_SYMBOL(nla_reserve_nohdr);
493 EXPORT_SYMBOL(__nla_put);
494 EXPORT_SYMBOL(__nla_put_nohdr);
495 EXPORT_SYMBOL(nla_put);
496 EXPORT_SYMBOL(nla_put_nohdr);
497 EXPORT_SYMBOL(nla_memcpy);
498 EXPORT_SYMBOL(nla_memcmp);
499 EXPORT_SYMBOL(nla_strcmp);
500 EXPORT_SYMBOL(nla_append);