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
),
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
;
33 if (nla
->nla_type
<= 0 || nla
->nla_type
> maxtype
)
36 pt
= &policy
[nla
->nla_type
];
38 BUG_ON(pt
->type
> NLA_TYPE_MAX
);
42 else if (pt
->type
!= NLA_UNSPEC
)
43 minlen
= nla_attr_minlen
[pt
->type
];
45 if (pt
->type
== NLA_FLAG
&& nla_len(nla
) > 0)
48 if (nla_len(nla
) < minlen
)
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
)
73 nla_for_each_attr(nla
, head
, len
, rem
) {
74 err
= validate_nla(nla
, maxtype
, policy
);
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
)
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
) {
111 err
= validate_nla(nla
, maxtype
, policy
);
120 if (unlikely(rem
> 0))
121 printk(KERN_WARNING
"netlink: %d bytes leftover after parsing "
122 "attributes.\n", rem
);
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
)
142 nla_for_each_attr(nla
, head
, len
, rem
)
143 if (nla
->nla_type
== attrtype
)
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')
170 size_t len
= (srclen
>= dstsize
) ? dstsize
- 1 : srclen
;
172 memset(dst
, 0, dstsize
);
173 memcpy(dst
, src
, len
);
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
);
200 * nla_memcmp - Compare an attribute with sized memory area
201 * @nla: netlink attribute
203 * @size: size of memory area
205 int nla_memcmp(const struct nlattr
*nla
, const void *data
,
208 int d
= nla_len(nla
) - size
;
211 d
= memcmp(nla_data(nla
), data
, size
);
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
;
227 d
= memcmp(nla_data(nla
), str
, len
);
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
)
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
));
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
)
271 start
= skb_put(skb
, NLA_ALIGN(attrlen
));
272 memset(start
, 0, NLA_ALIGN(attrlen
));
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
)))
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
)))
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
,
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
)
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
)))
366 __nla_put(skb
, attrtype
, attrlen
, data
);
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
)))
384 __nla_put_nohdr(skb
, attrlen
, data
);
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
);