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 struct nla_policy *policy)
29 struct nla_policy *pt;
30 int minlen = 0, attrlen = nla_len(nla);
32 if (nla->nla_type <= 0 || nla->nla_type > maxtype)
35 pt = &policy[nla->nla_type];
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)
73 else if (pt->type != NLA_UNSPEC)
74 minlen = nla_attr_minlen[pt->type];
84 * nla_validate - Validate a stream of attributes
85 * @head: head of attribute stream
86 * @len: length of attribute stream
87 * @maxtype: maximum attribute type to be expected
88 * @policy: validation policy
90 * Validates all attributes in the specified attribute stream against the
91 * specified policy. Attributes with a type exceeding maxtype will be
92 * ignored. See documenation of struct nla_policy for more details.
94 * Returns 0 on success or a negative error code.
96 int nla_validate(struct nlattr *head, int len, int maxtype,
97 struct nla_policy *policy)
102 nla_for_each_attr(nla, head, len, rem) {
103 err = validate_nla(nla, maxtype, policy);
114 * nla_parse - Parse a stream of attributes into a tb buffer
115 * @tb: destination array with maxtype+1 elements
116 * @maxtype: maximum attribute type to be expected
117 * @head: head of attribute stream
118 * @len: length of attribute stream
120 * Parses a stream of attributes and stores a pointer to each attribute in
121 * the tb array accessable via the attribute type. Attributes with a type
122 * exceeding maxtype will be silently ignored for backwards compatibility
123 * reasons. policy may be set to NULL if no validation is required.
125 * Returns 0 on success or a negative error code.
127 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
128 struct nla_policy *policy)
133 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
135 nla_for_each_attr(nla, head, len, rem) {
136 u16 type = nla->nla_type;
138 if (type > 0 && type <= maxtype) {
140 err = validate_nla(nla, maxtype, policy);
149 if (unlikely(rem > 0))
150 printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
151 "attributes.\n", rem);
159 * nla_find - Find a specific attribute in a stream of attributes
160 * @head: head of attribute stream
161 * @len: length of attribute stream
162 * @attrtype: type of attribute to look for
164 * Returns the first attribute in the stream matching the specified type.
166 struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
171 nla_for_each_attr(nla, head, len, rem)
172 if (nla->nla_type == attrtype)
179 * nla_strlcpy - Copy string attribute payload into a sized buffer
180 * @dst: where to copy the string to
181 * @src: attribute to copy the string from
182 * @dstsize: size of destination buffer
184 * Copies at most dstsize - 1 bytes into the destination buffer.
185 * The result is always a valid NUL-terminated string. Unlike
186 * strlcpy the destination buffer is always padded out.
188 * Returns the length of the source buffer.
190 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
192 size_t srclen = nla_len(nla);
193 char *src = nla_data(nla);
195 if (srclen > 0 && src[srclen - 1] == '\0')
199 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
201 memset(dst, 0, dstsize);
202 memcpy(dst, src, len);
209 * nla_memcpy - Copy a netlink attribute into another memory area
210 * @dest: where to copy to memcpy
211 * @src: netlink attribute to copy from
212 * @count: size of the destination area
214 * Note: The number of bytes copied is limited by the length of
215 * attribute's payload. memcpy
217 * Returns the number of bytes copied.
219 int nla_memcpy(void *dest, struct nlattr *src, int count)
221 int minlen = min_t(int, count, nla_len(src));
223 memcpy(dest, nla_data(src), minlen);
229 * nla_memcmp - Compare an attribute with sized memory area
230 * @nla: netlink attribute
232 * @size: size of memory area
234 int nla_memcmp(const struct nlattr *nla, const void *data,
237 int d = nla_len(nla) - size;
240 d = memcmp(nla_data(nla), data, size);
246 * nla_strcmp - Compare a string attribute against a string
247 * @nla: netlink string attribute
248 * @str: another string
250 int nla_strcmp(const struct nlattr *nla, const char *str)
252 int len = strlen(str) + 1;
253 int d = nla_len(nla) - len;
256 d = memcmp(nla_data(nla), str, len);
262 * __nla_reserve - reserve room for attribute on the skb
263 * @skb: socket buffer to reserve room on
264 * @attrtype: attribute type
265 * @attrlen: length of attribute payload
267 * Adds a netlink attribute header to a socket buffer and reserves
268 * room for the payload but does not copy it.
270 * The caller is responsible to ensure that the skb provides enough
271 * tailroom for the attribute header and payload.
273 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
277 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
278 nla->nla_type = attrtype;
279 nla->nla_len = nla_attr_size(attrlen);
281 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
287 * __nla_reserve_nohdr - reserve room for attribute without header
288 * @skb: socket buffer to reserve room on
289 * @attrlen: length of attribute payload
291 * Reserves room for attribute payload without a header.
293 * The caller is responsible to ensure that the skb provides enough
294 * tailroom for the payload.
296 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
300 start = skb_put(skb, NLA_ALIGN(attrlen));
301 memset(start, 0, NLA_ALIGN(attrlen));
307 * nla_reserve - reserve room for attribute on the skb
308 * @skb: socket buffer to reserve room on
309 * @attrtype: attribute type
310 * @attrlen: length of attribute payload
312 * Adds a netlink attribute header to a socket buffer and reserves
313 * room for the payload but does not copy it.
315 * Returns NULL if the tailroom of the skb is insufficient to store
316 * the attribute header and payload.
318 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
320 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
323 return __nla_reserve(skb, attrtype, attrlen);
327 * nla_reserve - reserve room for attribute without header
328 * @skb: socket buffer to reserve room on
329 * @len: length of attribute payload
331 * Reserves room for attribute payload without a header.
333 * Returns NULL if the tailroom of the skb is insufficient to store
334 * the attribute payload.
336 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
338 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
341 return __nla_reserve_nohdr(skb, attrlen);
345 * __nla_put - Add a netlink attribute to a socket buffer
346 * @skb: socket buffer to add attribute to
347 * @attrtype: attribute type
348 * @attrlen: length of attribute payload
349 * @data: head of attribute payload
351 * The caller is responsible to ensure that the skb provides enough
352 * tailroom for the attribute header and payload.
354 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
359 nla = __nla_reserve(skb, attrtype, attrlen);
360 memcpy(nla_data(nla), data, attrlen);
364 * __nla_put_nohdr - Add a netlink attribute without header
365 * @skb: socket buffer to add attribute to
366 * @attrlen: length of attribute payload
367 * @data: head of attribute payload
369 * The caller is responsible to ensure that the skb provides enough
370 * tailroom for the attribute payload.
372 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
376 start = __nla_reserve_nohdr(skb, attrlen);
377 memcpy(start, data, attrlen);
381 * nla_put - Add a netlink attribute to a socket buffer
382 * @skb: socket buffer to add attribute to
383 * @attrtype: attribute type
384 * @attrlen: length of attribute payload
385 * @data: head of attribute payload
387 * Returns -1 if the tailroom of the skb is insufficient to store
388 * the attribute header and payload.
390 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
392 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
395 __nla_put(skb, attrtype, attrlen, data);
400 * nla_put_nohdr - Add a netlink attribute without header
401 * @skb: socket buffer to add attribute to
402 * @attrlen: length of attribute payload
403 * @data: head of attribute payload
405 * Returns -1 if the tailroom of the skb is insufficient to store
406 * the attribute payload.
408 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
410 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
413 __nla_put_nohdr(skb, attrlen, data);
417 EXPORT_SYMBOL(nla_validate);
418 EXPORT_SYMBOL(nla_parse);
419 EXPORT_SYMBOL(nla_find);
420 EXPORT_SYMBOL(nla_strlcpy);
421 EXPORT_SYMBOL(__nla_reserve);
422 EXPORT_SYMBOL(__nla_reserve_nohdr);
423 EXPORT_SYMBOL(nla_reserve);
424 EXPORT_SYMBOL(nla_reserve_nohdr);
425 EXPORT_SYMBOL(__nla_put);
426 EXPORT_SYMBOL(__nla_put_nohdr);
427 EXPORT_SYMBOL(nla_put);
428 EXPORT_SYMBOL(nla_put_nohdr);
429 EXPORT_SYMBOL(nla_memcpy);
430 EXPORT_SYMBOL(nla_memcmp);
431 EXPORT_SYMBOL(nla_strcmp);