[NETLINK]: Extend netlink messaging interface
[hh.org.git] / net / netlink / attr.c
blob136e529e5780cf4bec9a8523f07ce758028a83c2
1 /*
2 * NETLINK Netlink attributes
4 * Authors: Thomas Graf <tgraf@suug.ch>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 */
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_STRING] = 1,
24 [NLA_NESTED] = NLA_HDRLEN,
27 static int validate_nla(struct nlattr *nla, int maxtype,
28 struct nla_policy *policy)
30 struct nla_policy *pt;
31 int minlen = 0;
33 if (nla->nla_type <= 0 || nla->nla_type > maxtype)
34 return 0;
36 pt = &policy[nla->nla_type];
38 BUG_ON(pt->type > NLA_TYPE_MAX);
40 if (pt->minlen)
41 minlen = pt->minlen;
42 else if (pt->type != NLA_UNSPEC)
43 minlen = nla_attr_minlen[pt->type];
45 if (pt->type == NLA_FLAG && nla_len(nla) > 0)
46 return -ERANGE;
48 if (nla_len(nla) < minlen)
49 return -ERANGE;
51 return 0;
54 /**
55 * nla_validate - Validate a stream of attributes
56 * @head: head of attribute stream
57 * @len: length of attribute stream
58 * @maxtype: maximum attribute type to be expected
59 * @policy: validation policy
61 * Validates all attributes in the specified attribute stream against the
62 * specified policy. Attributes with a type exceeding maxtype will be
63 * ignored. See documenation of struct nla_policy for more details.
65 * Returns 0 on success or a negative error code.
67 int nla_validate(struct nlattr *head, int len, int maxtype,
68 struct nla_policy *policy)
70 struct nlattr *nla;
71 int rem, err;
73 nla_for_each_attr(nla, head, len, rem) {
74 err = validate_nla(nla, maxtype, policy);
75 if (err < 0)
76 goto errout;
79 err = 0;
80 errout:
81 return err;
84 /**
85 * nla_parse - Parse a stream of attributes into a tb buffer
86 * @tb: destination array with maxtype+1 elements
87 * @maxtype: maximum attribute type to be expected
88 * @head: head of attribute stream
89 * @len: length of attribute stream
91 * Parses a stream of attributes and stores a pointer to each attribute in
92 * the tb array accessable via the attribute type. Attributes with a type
93 * exceeding maxtype will be silently ignored for backwards compatibility
94 * reasons. policy may be set to NULL if no validation is required.
96 * Returns 0 on success or a negative error code.
98 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
99 struct nla_policy *policy)
101 struct nlattr *nla;
102 int rem, err;
104 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
106 nla_for_each_attr(nla, head, len, rem) {
107 u16 type = nla->nla_type;
109 if (type > 0 && type <= maxtype) {
110 if (policy) {
111 err = validate_nla(nla, maxtype, policy);
112 if (err < 0)
113 goto errout;
116 tb[type] = nla;
120 if (unlikely(rem > 0))
121 printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
122 "attributes.\n", rem);
124 err = 0;
125 errout:
126 return err;
130 * nla_find - Find a specific attribute in a stream of attributes
131 * @head: head of attribute stream
132 * @len: length of attribute stream
133 * @attrtype: type of attribute to look for
135 * Returns the first attribute in the stream matching the specified type.
137 struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
139 struct nlattr *nla;
140 int rem;
142 nla_for_each_attr(nla, head, len, rem)
143 if (nla->nla_type == attrtype)
144 return nla;
146 return NULL;
150 * nla_strlcpy - Copy string attribute payload into a sized buffer
151 * @dst: where to copy the string to
152 * @src: attribute to copy the string from
153 * @dstsize: size of destination buffer
155 * Copies at most dstsize - 1 bytes into the destination buffer.
156 * The result is always a valid NUL-terminated string. Unlike
157 * strlcpy the destination buffer is always padded out.
159 * Returns the length of the source buffer.
161 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
163 size_t srclen = nla_len(nla);
164 char *src = nla_data(nla);
166 if (srclen > 0 && src[srclen - 1] == '\0')
167 srclen--;
169 if (dstsize > 0) {
170 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
172 memset(dst, 0, dstsize);
173 memcpy(dst, src, len);
176 return srclen;
180 * nla_memcpy - Copy a netlink attribute into another memory area
181 * @dest: where to copy to memcpy
182 * @src: netlink attribute to copy from
183 * @count: size of the destination area
185 * Note: The number of bytes copied is limited by the length of
186 * attribute's payload. memcpy
188 * Returns the number of bytes copied.
190 int nla_memcpy(void *dest, struct nlattr *src, int count)
192 int minlen = min_t(int, count, nla_len(src));
194 memcpy(dest, nla_data(src), minlen);
196 return minlen;
200 * nla_memcmp - Compare an attribute with sized memory area
201 * @nla: netlink attribute
202 * @data: memory area
203 * @size: size of memory area
205 int nla_memcmp(const struct nlattr *nla, const void *data,
206 size_t size)
208 int d = nla_len(nla) - size;
210 if (d == 0)
211 d = memcmp(nla_data(nla), data, size);
213 return d;
217 * nla_strcmp - Compare a string attribute against a string
218 * @nla: netlink string attribute
219 * @str: another string
221 int nla_strcmp(const struct nlattr *nla, const char *str)
223 int len = strlen(str) + 1;
224 int d = nla_len(nla) - len;
226 if (d == 0)
227 d = memcmp(nla_data(nla), str, len);
229 return d;
233 * __nla_reserve - reserve room for attribute on the skb
234 * @skb: socket buffer to reserve room on
235 * @attrtype: attribute type
236 * @attrlen: length of attribute payload
238 * Adds a netlink attribute header to a socket buffer and reserves
239 * room for the payload but does not copy it.
241 * The caller is responsible to ensure that the skb provides enough
242 * tailroom for the attribute header and payload.
244 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
246 struct nlattr *nla;
248 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
249 nla->nla_type = attrtype;
250 nla->nla_len = nla_attr_size(attrlen);
252 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
254 return nla;
258 * __nla_reserve_nohdr - reserve room for attribute without header
259 * @skb: socket buffer to reserve room on
260 * @attrlen: length of attribute payload
262 * Reserves room for attribute payload without a header.
264 * The caller is responsible to ensure that the skb provides enough
265 * tailroom for the payload.
267 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
269 void *start;
271 start = skb_put(skb, NLA_ALIGN(attrlen));
272 memset(start, 0, NLA_ALIGN(attrlen));
274 return start;
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 * Returns NULL if the tailroom of the skb is insufficient to store
287 * the attribute header and payload.
289 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
291 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
292 return NULL;
294 return __nla_reserve(skb, attrtype, attrlen);
298 * nla_reserve - reserve room for attribute without header
299 * @skb: socket buffer to reserve room on
300 * @len: length of attribute payload
302 * Reserves room for attribute payload without a header.
304 * Returns NULL if the tailroom of the skb is insufficient to store
305 * the attribute payload.
307 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
309 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
310 return NULL;
312 return __nla_reserve_nohdr(skb, attrlen);
316 * __nla_put - Add a netlink attribute to a socket buffer
317 * @skb: socket buffer to add attribute to
318 * @attrtype: attribute type
319 * @attrlen: length of attribute payload
320 * @data: head of attribute payload
322 * The caller is responsible to ensure that the skb provides enough
323 * tailroom for the attribute header and payload.
325 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
326 const void *data)
328 struct nlattr *nla;
330 nla = __nla_reserve(skb, attrtype, attrlen);
331 memcpy(nla_data(nla), data, attrlen);
335 * __nla_put_nohdr - Add a netlink attribute without header
336 * @skb: socket buffer to add attribute to
337 * @attrlen: length of attribute payload
338 * @data: head of attribute payload
340 * The caller is responsible to ensure that the skb provides enough
341 * tailroom for the attribute payload.
343 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
345 void *start;
347 start = __nla_reserve_nohdr(skb, attrlen);
348 memcpy(start, data, attrlen);
352 * nla_put - Add a netlink attribute to a socket buffer
353 * @skb: socket buffer to add attribute to
354 * @attrtype: attribute type
355 * @attrlen: length of attribute payload
356 * @data: head of attribute payload
358 * Returns -1 if the tailroom of the skb is insufficient to store
359 * the attribute header and payload.
361 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
363 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
364 return -1;
366 __nla_put(skb, attrtype, attrlen, data);
367 return 0;
371 * nla_put_nohdr - Add a netlink attribute without header
372 * @skb: socket buffer to add attribute to
373 * @attrlen: length of attribute payload
374 * @data: head of attribute payload
376 * Returns -1 if the tailroom of the skb is insufficient to store
377 * the attribute payload.
379 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
381 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
382 return -1;
384 __nla_put_nohdr(skb, attrlen, data);
385 return 0;
388 EXPORT_SYMBOL(nla_validate);
389 EXPORT_SYMBOL(nla_parse);
390 EXPORT_SYMBOL(nla_find);
391 EXPORT_SYMBOL(nla_strlcpy);
392 EXPORT_SYMBOL(__nla_reserve);
393 EXPORT_SYMBOL(__nla_reserve_nohdr);
394 EXPORT_SYMBOL(nla_reserve);
395 EXPORT_SYMBOL(nla_reserve_nohdr);
396 EXPORT_SYMBOL(__nla_put);
397 EXPORT_SYMBOL(__nla_put_nohdr);
398 EXPORT_SYMBOL(nla_put);
399 EXPORT_SYMBOL(nla_put_nohdr);
400 EXPORT_SYMBOL(nla_memcpy);
401 EXPORT_SYMBOL(nla_memcmp);
402 EXPORT_SYMBOL(nla_strcmp);