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
),
23 [NLA_NESTED
] = NLA_HDRLEN
,
26 static int validate_nla(struct nlattr
*nla
, int maxtype
,
27 struct nla_policy
*policy
)
29 struct nla_policy
*pt
;
30 int minlen
= 0, attrlen
= nla_len(nla
);
32 if (nla
->nla_type
<= 0 || nla
->nla_type
> maxtype
)
35 pt
= &policy
[nla
->nla_type
];
37 BUG_ON(pt
->type
> NLA_TYPE_MAX
);
47 minlen
= min_t(int, attrlen
, pt
->len
+ 1);
51 if (!minlen
|| memchr(nla_data(nla
), '\0', minlen
) == NULL
)
60 char *buf
= nla_data(nla
);
62 if (buf
[attrlen
- 1] == '\0')
65 if (attrlen
> pt
->len
)
71 if (pt
->len
&& attrlen
> pt
->len
)
78 else if (pt
->type
!= NLA_UNSPEC
)
79 minlen
= nla_attr_minlen
[pt
->type
];
89 * nla_validate - Validate a stream of attributes
90 * @head: head of attribute stream
91 * @len: length of attribute stream
92 * @maxtype: maximum attribute type to be expected
93 * @policy: validation policy
95 * Validates all attributes in the specified attribute stream against the
96 * specified policy. Attributes with a type exceeding maxtype will be
97 * ignored. See documenation of struct nla_policy for more details.
99 * Returns 0 on success or a negative error code.
101 int nla_validate(struct nlattr
*head
, int len
, int maxtype
,
102 struct nla_policy
*policy
)
107 nla_for_each_attr(nla
, head
, len
, rem
) {
108 err
= validate_nla(nla
, maxtype
, policy
);
119 * nla_parse - Parse a stream of attributes into a tb buffer
120 * @tb: destination array with maxtype+1 elements
121 * @maxtype: maximum attribute type to be expected
122 * @head: head of attribute stream
123 * @len: length of attribute stream
125 * Parses a stream of attributes and stores a pointer to each attribute in
126 * the tb array accessable via the attribute type. Attributes with a type
127 * exceeding maxtype will be silently ignored for backwards compatibility
128 * reasons. policy may be set to NULL if no validation is required.
130 * Returns 0 on success or a negative error code.
132 int nla_parse(struct nlattr
*tb
[], int maxtype
, struct nlattr
*head
, int len
,
133 struct nla_policy
*policy
)
138 memset(tb
, 0, sizeof(struct nlattr
*) * (maxtype
+ 1));
140 nla_for_each_attr(nla
, head
, len
, rem
) {
141 u16 type
= nla
->nla_type
;
143 if (type
> 0 && type
<= maxtype
) {
145 err
= validate_nla(nla
, maxtype
, policy
);
154 if (unlikely(rem
> 0))
155 printk(KERN_WARNING
"netlink: %d bytes leftover after parsing "
156 "attributes.\n", rem
);
164 * nla_find - Find a specific attribute in a stream of attributes
165 * @head: head of attribute stream
166 * @len: length of attribute stream
167 * @attrtype: type of attribute to look for
169 * Returns the first attribute in the stream matching the specified type.
171 struct nlattr
*nla_find(struct nlattr
*head
, int len
, int attrtype
)
176 nla_for_each_attr(nla
, head
, len
, rem
)
177 if (nla
->nla_type
== attrtype
)
184 * nla_strlcpy - Copy string attribute payload into a sized buffer
185 * @dst: where to copy the string to
186 * @src: attribute to copy the string from
187 * @dstsize: size of destination buffer
189 * Copies at most dstsize - 1 bytes into the destination buffer.
190 * The result is always a valid NUL-terminated string. Unlike
191 * strlcpy the destination buffer is always padded out.
193 * Returns the length of the source buffer.
195 size_t nla_strlcpy(char *dst
, const struct nlattr
*nla
, size_t dstsize
)
197 size_t srclen
= nla_len(nla
);
198 char *src
= nla_data(nla
);
200 if (srclen
> 0 && src
[srclen
- 1] == '\0')
204 size_t len
= (srclen
>= dstsize
) ? dstsize
- 1 : srclen
;
206 memset(dst
, 0, dstsize
);
207 memcpy(dst
, src
, len
);
214 * nla_memcpy - Copy a netlink attribute into another memory area
215 * @dest: where to copy to memcpy
216 * @src: netlink attribute to copy from
217 * @count: size of the destination area
219 * Note: The number of bytes copied is limited by the length of
220 * attribute's payload. memcpy
222 * Returns the number of bytes copied.
224 int nla_memcpy(void *dest
, struct nlattr
*src
, int count
)
226 int minlen
= min_t(int, count
, nla_len(src
));
228 memcpy(dest
, nla_data(src
), minlen
);
234 * nla_memcmp - Compare an attribute with sized memory area
235 * @nla: netlink attribute
237 * @size: size of memory area
239 int nla_memcmp(const struct nlattr
*nla
, const void *data
,
242 int d
= nla_len(nla
) - size
;
245 d
= memcmp(nla_data(nla
), data
, size
);
251 * nla_strcmp - Compare a string attribute against a string
252 * @nla: netlink string attribute
253 * @str: another string
255 int nla_strcmp(const struct nlattr
*nla
, const char *str
)
257 int len
= strlen(str
) + 1;
258 int d
= nla_len(nla
) - len
;
261 d
= memcmp(nla_data(nla
), str
, len
);
267 * __nla_reserve - reserve room for attribute on the skb
268 * @skb: socket buffer to reserve room on
269 * @attrtype: attribute type
270 * @attrlen: length of attribute payload
272 * Adds a netlink attribute header to a socket buffer and reserves
273 * room for the payload but does not copy it.
275 * The caller is responsible to ensure that the skb provides enough
276 * tailroom for the attribute header and payload.
278 struct nlattr
*__nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
282 nla
= (struct nlattr
*) skb_put(skb
, nla_total_size(attrlen
));
283 nla
->nla_type
= attrtype
;
284 nla
->nla_len
= nla_attr_size(attrlen
);
286 memset((unsigned char *) nla
+ nla
->nla_len
, 0, nla_padlen(attrlen
));
292 * __nla_reserve_nohdr - reserve room for attribute without header
293 * @skb: socket buffer to reserve room on
294 * @attrlen: length of attribute payload
296 * Reserves room for attribute payload without a header.
298 * The caller is responsible to ensure that the skb provides enough
299 * tailroom for the payload.
301 void *__nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
305 start
= skb_put(skb
, NLA_ALIGN(attrlen
));
306 memset(start
, 0, NLA_ALIGN(attrlen
));
312 * nla_reserve - reserve room for attribute on the skb
313 * @skb: socket buffer to reserve room on
314 * @attrtype: attribute type
315 * @attrlen: length of attribute payload
317 * Adds a netlink attribute header to a socket buffer and reserves
318 * room for the payload but does not copy it.
320 * Returns NULL if the tailroom of the skb is insufficient to store
321 * the attribute header and payload.
323 struct nlattr
*nla_reserve(struct sk_buff
*skb
, int attrtype
, int attrlen
)
325 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
328 return __nla_reserve(skb
, attrtype
, attrlen
);
332 * nla_reserve - reserve room for attribute without header
333 * @skb: socket buffer to reserve room on
334 * @len: length of attribute payload
336 * Reserves room for attribute payload without a header.
338 * Returns NULL if the tailroom of the skb is insufficient to store
339 * the attribute payload.
341 void *nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
)
343 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
346 return __nla_reserve_nohdr(skb
, attrlen
);
350 * __nla_put - Add a netlink attribute to a socket buffer
351 * @skb: socket buffer to add attribute to
352 * @attrtype: attribute type
353 * @attrlen: length of attribute payload
354 * @data: head of attribute payload
356 * The caller is responsible to ensure that the skb provides enough
357 * tailroom for the attribute header and payload.
359 void __nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
,
364 nla
= __nla_reserve(skb
, attrtype
, attrlen
);
365 memcpy(nla_data(nla
), data
, attrlen
);
369 * __nla_put_nohdr - Add a netlink attribute without header
370 * @skb: socket buffer to add attribute to
371 * @attrlen: length of attribute payload
372 * @data: head of attribute payload
374 * The caller is responsible to ensure that the skb provides enough
375 * tailroom for the attribute payload.
377 void __nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
381 start
= __nla_reserve_nohdr(skb
, attrlen
);
382 memcpy(start
, data
, attrlen
);
386 * nla_put - Add a netlink attribute to a socket buffer
387 * @skb: socket buffer to add attribute to
388 * @attrtype: attribute type
389 * @attrlen: length of attribute payload
390 * @data: head of attribute payload
392 * Returns -1 if the tailroom of the skb is insufficient to store
393 * the attribute header and payload.
395 int nla_put(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
397 if (unlikely(skb_tailroom(skb
) < nla_total_size(attrlen
)))
400 __nla_put(skb
, attrtype
, attrlen
, data
);
405 * nla_put_nohdr - Add a netlink attribute without header
406 * @skb: socket buffer to add attribute to
407 * @attrlen: length of attribute payload
408 * @data: head of attribute payload
410 * Returns -1 if the tailroom of the skb is insufficient to store
411 * the attribute payload.
413 int nla_put_nohdr(struct sk_buff
*skb
, int attrlen
, const void *data
)
415 if (unlikely(skb_tailroom(skb
) < NLA_ALIGN(attrlen
)))
418 __nla_put_nohdr(skb
, attrlen
, data
);
422 EXPORT_SYMBOL(nla_validate
);
423 EXPORT_SYMBOL(nla_parse
);
424 EXPORT_SYMBOL(nla_find
);
425 EXPORT_SYMBOL(nla_strlcpy
);
426 EXPORT_SYMBOL(__nla_reserve
);
427 EXPORT_SYMBOL(__nla_reserve_nohdr
);
428 EXPORT_SYMBOL(nla_reserve
);
429 EXPORT_SYMBOL(nla_reserve_nohdr
);
430 EXPORT_SYMBOL(__nla_put
);
431 EXPORT_SYMBOL(__nla_put_nohdr
);
432 EXPORT_SYMBOL(nla_put
);
433 EXPORT_SYMBOL(nla_put_nohdr
);
434 EXPORT_SYMBOL(nla_memcpy
);
435 EXPORT_SYMBOL(nla_memcmp
);
436 EXPORT_SYMBOL(nla_strcmp
);