Linux 2.6.18.8
[linux-2.6/suspend2-2.6.18.git] / net / netlink / attr.c
blobdddbd15135a8712421c4148f32801861d0b7f7c4
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 - reserve room for attribute on the skb
259 * @skb: socket buffer to reserve room on
260 * @attrtype: attribute type
261 * @attrlen: length of attribute payload
263 * Adds a netlink attribute header to a socket buffer and reserves
264 * room for the payload but does not copy it.
266 * Returns NULL if the tailroom of the skb is insufficient to store
267 * the attribute header and payload.
269 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
271 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
272 return NULL;
274 return __nla_reserve(skb, attrtype, attrlen);
278 * __nla_put - Add a netlink attribute to a socket buffer
279 * @skb: socket buffer to add attribute to
280 * @attrtype: attribute type
281 * @attrlen: length of attribute payload
282 * @data: head of attribute payload
284 * The caller is responsible to ensure that the skb provides enough
285 * tailroom for the attribute header and payload.
287 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
288 const void *data)
290 struct nlattr *nla;
292 nla = __nla_reserve(skb, attrtype, attrlen);
293 memcpy(nla_data(nla), data, attrlen);
298 * nla_put - Add a netlink attribute to a socket buffer
299 * @skb: socket buffer to add attribute to
300 * @attrtype: attribute type
301 * @attrlen: length of attribute payload
302 * @data: head of attribute payload
304 * Returns -1 if the tailroom of the skb is insufficient to store
305 * the attribute header and payload.
307 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
309 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
310 return -1;
312 __nla_put(skb, attrtype, attrlen, data);
313 return 0;
317 EXPORT_SYMBOL(nla_validate);
318 EXPORT_SYMBOL(nla_parse);
319 EXPORT_SYMBOL(nla_find);
320 EXPORT_SYMBOL(nla_strlcpy);
321 EXPORT_SYMBOL(__nla_reserve);
322 EXPORT_SYMBOL(nla_reserve);
323 EXPORT_SYMBOL(__nla_put);
324 EXPORT_SYMBOL(nla_put);
325 EXPORT_SYMBOL(nla_memcpy);
326 EXPORT_SYMBOL(nla_memcmp);
327 EXPORT_SYMBOL(nla_strcmp);