blob: 136e529e5780cf4bec9a8523f07ce758028a83c2 [file] [log] [blame]
Thomas Grafbfa83a92005-11-10 02:25:51 +01001/*
2 * NETLINK Netlink attributes
3 *
4 * Authors: Thomas Graf <tgraf@suug.ch>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 */
7
Thomas Grafbfa83a92005-11-10 02:25:51 +01008#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>
17
18static 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,
25};
26
27static int validate_nla(struct nlattr *nla, int maxtype,
28 struct nla_policy *policy)
29{
30 struct nla_policy *pt;
31 int minlen = 0;
32
33 if (nla->nla_type <= 0 || nla->nla_type > maxtype)
34 return 0;
35
36 pt = &policy[nla->nla_type];
37
38 BUG_ON(pt->type > NLA_TYPE_MAX);
39
40 if (pt->minlen)
41 minlen = pt->minlen;
42 else if (pt->type != NLA_UNSPEC)
43 minlen = nla_attr_minlen[pt->type];
44
45 if (pt->type == NLA_FLAG && nla_len(nla) > 0)
46 return -ERANGE;
47
48 if (nla_len(nla) < minlen)
49 return -ERANGE;
50
51 return 0;
52}
53
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
60 *
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.
64 *
65 * Returns 0 on success or a negative error code.
66 */
67int nla_validate(struct nlattr *head, int len, int maxtype,
68 struct nla_policy *policy)
69{
70 struct nlattr *nla;
71 int rem, err;
72
73 nla_for_each_attr(nla, head, len, rem) {
74 err = validate_nla(nla, maxtype, policy);
75 if (err < 0)
76 goto errout;
77 }
78
79 err = 0;
80errout:
81 return err;
82}
83
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
90 *
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.
95 *
96 * Returns 0 on success or a negative error code.
97 */
98int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
99 struct nla_policy *policy)
100{
101 struct nlattr *nla;
102 int rem, err;
103
104 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
105
106 nla_for_each_attr(nla, head, len, rem) {
107 u16 type = nla->nla_type;
108
109 if (type > 0 && type <= maxtype) {
110 if (policy) {
111 err = validate_nla(nla, maxtype, policy);
112 if (err < 0)
113 goto errout;
114 }
115
116 tb[type] = nla;
117 }
118 }
119
120 if (unlikely(rem > 0))
121 printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
122 "attributes.\n", rem);
123
124 err = 0;
125errout:
126 return err;
127}
128
129/**
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
134 *
135 * Returns the first attribute in the stream matching the specified type.
136 */
137struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
138{
139 struct nlattr *nla;
140 int rem;
141
142 nla_for_each_attr(nla, head, len, rem)
143 if (nla->nla_type == attrtype)
144 return nla;
145
146 return NULL;
147}
148
149/**
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
154 *
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.
158 *
159 * Returns the length of the source buffer.
160 */
161size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
162{
163 size_t srclen = nla_len(nla);
164 char *src = nla_data(nla);
165
166 if (srclen > 0 && src[srclen - 1] == '\0')
167 srclen--;
168
169 if (dstsize > 0) {
170 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
171
172 memset(dst, 0, dstsize);
173 memcpy(dst, src, len);
174 }
175
176 return srclen;
177}
178
179/**
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
184 *
185 * Note: The number of bytes copied is limited by the length of
186 * attribute's payload. memcpy
187 *
188 * Returns the number of bytes copied.
189 */
190int nla_memcpy(void *dest, struct nlattr *src, int count)
191{
192 int minlen = min_t(int, count, nla_len(src));
193
194 memcpy(dest, nla_data(src), minlen);
195
196 return minlen;
197}
198
199/**
200 * nla_memcmp - Compare an attribute with sized memory area
201 * @nla: netlink attribute
202 * @data: memory area
203 * @size: size of memory area
204 */
205int nla_memcmp(const struct nlattr *nla, const void *data,
206 size_t size)
207{
208 int d = nla_len(nla) - size;
209
210 if (d == 0)
211 d = memcmp(nla_data(nla), data, size);
212
213 return d;
214}
215
216/**
217 * nla_strcmp - Compare a string attribute against a string
218 * @nla: netlink string attribute
219 * @str: another string
220 */
221int nla_strcmp(const struct nlattr *nla, const char *str)
222{
223 int len = strlen(str) + 1;
224 int d = nla_len(nla) - len;
225
226 if (d == 0)
227 d = memcmp(nla_data(nla), str, len);
228
229 return d;
230}
231
232/**
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
237 *
238 * Adds a netlink attribute header to a socket buffer and reserves
239 * room for the payload but does not copy it.
240 *
241 * The caller is responsible to ensure that the skb provides enough
242 * tailroom for the attribute header and payload.
243 */
244struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
245{
246 struct nlattr *nla;
247
248 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
249 nla->nla_type = attrtype;
250 nla->nla_len = nla_attr_size(attrlen);
251
252 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
253
254 return nla;
255}
256
257/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700258 * __nla_reserve_nohdr - reserve room for attribute without header
259 * @skb: socket buffer to reserve room on
260 * @attrlen: length of attribute payload
261 *
262 * Reserves room for attribute payload without a header.
263 *
264 * The caller is responsible to ensure that the skb provides enough
265 * tailroom for the payload.
266 */
267void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
268{
269 void *start;
270
271 start = skb_put(skb, NLA_ALIGN(attrlen));
272 memset(start, 0, NLA_ALIGN(attrlen));
273
274 return start;
275}
276
277/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100278 * 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
282 *
283 * Adds a netlink attribute header to a socket buffer and reserves
284 * room for the payload but does not copy it.
285 *
286 * Returns NULL if the tailroom of the skb is insufficient to store
287 * the attribute header and payload.
288 */
289struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
290{
291 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
292 return NULL;
293
294 return __nla_reserve(skb, attrtype, attrlen);
295}
296
297/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700298 * nla_reserve - reserve room for attribute without header
299 * @skb: socket buffer to reserve room on
300 * @len: length of attribute payload
301 *
302 * Reserves room for attribute payload without a header.
303 *
304 * Returns NULL if the tailroom of the skb is insufficient to store
305 * the attribute payload.
306 */
307void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
308{
309 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
310 return NULL;
311
312 return __nla_reserve_nohdr(skb, attrlen);
313}
314
315/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100316 * __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
321 *
322 * The caller is responsible to ensure that the skb provides enough
323 * tailroom for the attribute header and payload.
324 */
325void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
326 const void *data)
327{
328 struct nlattr *nla;
329
330 nla = __nla_reserve(skb, attrtype, attrlen);
331 memcpy(nla_data(nla), data, attrlen);
332}
333
Thomas Graffe4944e2006-08-04 23:03:05 -0700334/**
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
339 *
340 * The caller is responsible to ensure that the skb provides enough
341 * tailroom for the attribute payload.
342 */
343void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
344{
345 void *start;
346
347 start = __nla_reserve_nohdr(skb, attrlen);
348 memcpy(start, data, attrlen);
349}
Thomas Grafbfa83a92005-11-10 02:25:51 +0100350
351/**
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
357 *
358 * Returns -1 if the tailroom of the skb is insufficient to store
359 * the attribute header and payload.
360 */
361int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
362{
363 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
364 return -1;
365
366 __nla_put(skb, attrtype, attrlen, data);
367 return 0;
368}
369
Thomas Graffe4944e2006-08-04 23:03:05 -0700370/**
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
375 *
376 * Returns -1 if the tailroom of the skb is insufficient to store
377 * the attribute payload.
378 */
379int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
380{
381 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
382 return -1;
383
384 __nla_put_nohdr(skb, attrlen, data);
385 return 0;
386}
Thomas Grafbfa83a92005-11-10 02:25:51 +0100387
388EXPORT_SYMBOL(nla_validate);
389EXPORT_SYMBOL(nla_parse);
390EXPORT_SYMBOL(nla_find);
391EXPORT_SYMBOL(nla_strlcpy);
392EXPORT_SYMBOL(__nla_reserve);
Thomas Graffe4944e2006-08-04 23:03:05 -0700393EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100394EXPORT_SYMBOL(nla_reserve);
Thomas Graffe4944e2006-08-04 23:03:05 -0700395EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100396EXPORT_SYMBOL(__nla_put);
Thomas Graffe4944e2006-08-04 23:03:05 -0700397EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100398EXPORT_SYMBOL(nla_put);
Thomas Graffe4944e2006-08-04 23:03:05 -0700399EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100400EXPORT_SYMBOL(nla_memcpy);
401EXPORT_SYMBOL(nla_memcmp);
402EXPORT_SYMBOL(nla_strcmp);