sky2: synchronize irq on remove
[hh.org.git] / net / netlink / attr.c
blobfffef4ab276f08a27e6890bda232d83cb5e6ac68
1 /*
2 * NETLINK Netlink attributes
4 * Authors: Thomas Graf <tgraf@suug.ch>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 */
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/jiffies.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <net/netlink.h>
19 static u16 nla_attr_minlen[NLA_TYPE_MAX+1] __read_mostly = {
20 [NLA_U8] = sizeof(u8),
21 [NLA_U16] = sizeof(u16),
22 [NLA_U32] = sizeof(u32),
23 [NLA_U64] = sizeof(u64),
24 [NLA_STRING] = 1,
25 [NLA_NESTED] = NLA_HDRLEN,
28 static int validate_nla(struct nlattr *nla, int maxtype,
29 struct nla_policy *policy)
31 struct nla_policy *pt;
32 int minlen = 0;
34 if (nla->nla_type <= 0 || nla->nla_type > maxtype)
35 return 0;
37 pt = &policy[nla->nla_type];
39 BUG_ON(pt->type > NLA_TYPE_MAX);
41 if (pt->minlen)
42 minlen = pt->minlen;
43 else if (pt->type != NLA_UNSPEC)
44 minlen = nla_attr_minlen[pt->type];
46 if (pt->type == NLA_FLAG && nla_len(nla) > 0)
47 return -ERANGE;
49 if (nla_len(nla) < minlen)
50 return -ERANGE;
52 return 0;
55 /**
56 * nla_validate - Validate a stream of attributes
57 * @head: head of attribute stream
58 * @len: length of attribute stream
59 * @maxtype: maximum attribute type to be expected
60 * @policy: validation policy
62 * Validates all attributes in the specified attribute stream against the
63 * specified policy. Attributes with a type exceeding maxtype will be
64 * ignored. See documenation of struct nla_policy for more details.
66 * Returns 0 on success or a negative error code.
68 int nla_validate(struct nlattr *head, int len, int maxtype,
69 struct nla_policy *policy)
71 struct nlattr *nla;
72 int rem, err;
74 nla_for_each_attr(nla, head, len, rem) {
75 err = validate_nla(nla, maxtype, policy);
76 if (err < 0)
77 goto errout;
80 err = 0;
81 errout:
82 return err;
85 /**
86 * nla_parse - Parse a stream of attributes into a tb buffer
87 * @tb: destination array with maxtype+1 elements
88 * @maxtype: maximum attribute type to be expected
89 * @head: head of attribute stream
90 * @len: length of attribute stream
92 * Parses a stream of attributes and stores a pointer to each attribute in
93 * the tb array accessable via the attribute type. Attributes with a type
94 * exceeding maxtype will be silently ignored for backwards compatibility
95 * reasons. policy may be set to NULL if no validation is required.
97 * Returns 0 on success or a negative error code.
99 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
100 struct nla_policy *policy)
102 struct nlattr *nla;
103 int rem, err;
105 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
107 nla_for_each_attr(nla, head, len, rem) {
108 u16 type = nla->nla_type;
110 if (type > 0 && type <= maxtype) {
111 if (policy) {
112 err = validate_nla(nla, maxtype, policy);
113 if (err < 0)
114 goto errout;
117 tb[type] = nla;
121 if (unlikely(rem > 0))
122 printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
123 "attributes.\n", rem);
125 err = 0;
126 errout:
127 return err;
131 * nla_find - Find a specific attribute in a stream of attributes
132 * @head: head of attribute stream
133 * @len: length of attribute stream
134 * @attrtype: type of attribute to look for
136 * Returns the first attribute in the stream matching the specified type.
138 struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
140 struct nlattr *nla;
141 int rem;
143 nla_for_each_attr(nla, head, len, rem)
144 if (nla->nla_type == attrtype)
145 return nla;
147 return NULL;
151 * nla_strlcpy - Copy string attribute payload into a sized buffer
152 * @dst: where to copy the string to
153 * @src: attribute to copy the string from
154 * @dstsize: size of destination buffer
156 * Copies at most dstsize - 1 bytes into the destination buffer.
157 * The result is always a valid NUL-terminated string. Unlike
158 * strlcpy the destination buffer is always padded out.
160 * Returns the length of the source buffer.
162 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
164 size_t srclen = nla_len(nla);
165 char *src = nla_data(nla);
167 if (srclen > 0 && src[srclen - 1] == '\0')
168 srclen--;
170 if (dstsize > 0) {
171 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
173 memset(dst, 0, dstsize);
174 memcpy(dst, src, len);
177 return srclen;
181 * nla_memcpy - Copy a netlink attribute into another memory area
182 * @dest: where to copy to memcpy
183 * @src: netlink attribute to copy from
184 * @count: size of the destination area
186 * Note: The number of bytes copied is limited by the length of
187 * attribute's payload. memcpy
189 * Returns the number of bytes copied.
191 int nla_memcpy(void *dest, struct nlattr *src, int count)
193 int minlen = min_t(int, count, nla_len(src));
195 memcpy(dest, nla_data(src), minlen);
197 return minlen;
201 * nla_memcmp - Compare an attribute with sized memory area
202 * @nla: netlink attribute
203 * @data: memory area
204 * @size: size of memory area
206 int nla_memcmp(const struct nlattr *nla, const void *data,
207 size_t size)
209 int d = nla_len(nla) - size;
211 if (d == 0)
212 d = memcmp(nla_data(nla), data, size);
214 return d;
218 * nla_strcmp - Compare a string attribute against a string
219 * @nla: netlink string attribute
220 * @str: another string
222 int nla_strcmp(const struct nlattr *nla, const char *str)
224 int len = strlen(str) + 1;
225 int d = nla_len(nla) - len;
227 if (d == 0)
228 d = memcmp(nla_data(nla), str, len);
230 return d;
234 * __nla_reserve - reserve room for attribute on the skb
235 * @skb: socket buffer to reserve room on
236 * @attrtype: attribute type
237 * @attrlen: length of attribute payload
239 * Adds a netlink attribute header to a socket buffer and reserves
240 * room for the payload but does not copy it.
242 * The caller is responsible to ensure that the skb provides enough
243 * tailroom for the attribute header and payload.
245 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
247 struct nlattr *nla;
249 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
250 nla->nla_type = attrtype;
251 nla->nla_len = nla_attr_size(attrlen);
253 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
255 return nla;
259 * nla_reserve - reserve room for attribute on the skb
260 * @skb: socket buffer to reserve room on
261 * @attrtype: attribute type
262 * @attrlen: length of attribute payload
264 * Adds a netlink attribute header to a socket buffer and reserves
265 * room for the payload but does not copy it.
267 * Returns NULL if the tailroom of the skb is insufficient to store
268 * the attribute header and payload.
270 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
272 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
273 return NULL;
275 return __nla_reserve(skb, attrtype, attrlen);
279 * __nla_put - Add a netlink attribute to a socket buffer
280 * @skb: socket buffer to add attribute to
281 * @attrtype: attribute type
282 * @attrlen: length of attribute payload
283 * @data: head of attribute payload
285 * The caller is responsible to ensure that the skb provides enough
286 * tailroom for the attribute header and payload.
288 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
289 const void *data)
291 struct nlattr *nla;
293 nla = __nla_reserve(skb, attrtype, attrlen);
294 memcpy(nla_data(nla), data, attrlen);
299 * nla_put - Add a netlink attribute to a socket buffer
300 * @skb: socket buffer to add attribute to
301 * @attrtype: attribute type
302 * @attrlen: length of attribute payload
303 * @data: head of attribute payload
305 * Returns -1 if the tailroom of the skb is insufficient to store
306 * the attribute header and payload.
308 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
310 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
311 return -1;
313 __nla_put(skb, attrtype, attrlen, data);
314 return 0;
318 EXPORT_SYMBOL(nla_validate);
319 EXPORT_SYMBOL(nla_parse);
320 EXPORT_SYMBOL(nla_find);
321 EXPORT_SYMBOL(nla_strlcpy);
322 EXPORT_SYMBOL(__nla_reserve);
323 EXPORT_SYMBOL(nla_reserve);
324 EXPORT_SYMBOL(__nla_put);
325 EXPORT_SYMBOL(nla_put);
326 EXPORT_SYMBOL(nla_memcpy);
327 EXPORT_SYMBOL(nla_memcmp);
328 EXPORT_SYMBOL(nla_strcmp);