]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
bfa83a9e TG |
2 | /* |
3 | * NETLINK Netlink attributes | |
4 | * | |
5 | * Authors: Thomas Graf <tgraf@suug.ch> | |
6 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> | |
7 | */ | |
8 | ||
8bc3bcc9 | 9 | #include <linux/export.h> |
bfa83a9e TG |
10 | #include <linux/kernel.h> |
11 | #include <linux/errno.h> | |
12 | #include <linux/jiffies.h> | |
bfa83a9e TG |
13 | #include <linux/skbuff.h> |
14 | #include <linux/string.h> | |
15 | #include <linux/types.h> | |
16 | #include <net/netlink.h> | |
17 | ||
6e237d09 DA |
18 | /* For these data types, attribute length should be exactly the given |
19 | * size. However, to maintain compatibility with broken commands, if the | |
20 | * attribute length does not match the expected size a warning is emitted | |
21 | * to the user that the command is sending invalid data and needs to be fixed. | |
22 | */ | |
28033ae4 | 23 | static const u8 nla_attr_len[NLA_TYPE_MAX+1] = { |
bfa83a9e TG |
24 | [NLA_U8] = sizeof(u8), |
25 | [NLA_U16] = sizeof(u16), | |
26 | [NLA_U32] = sizeof(u32), | |
27 | [NLA_U64] = sizeof(u64), | |
9eca2eb9 JA |
28 | [NLA_S8] = sizeof(s8), |
29 | [NLA_S16] = sizeof(s16), | |
30 | [NLA_S32] = sizeof(s32), | |
31 | [NLA_S64] = sizeof(s64), | |
bfa83a9e TG |
32 | }; |
33 | ||
28033ae4 | 34 | static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = { |
6e237d09 DA |
35 | [NLA_U8] = sizeof(u8), |
36 | [NLA_U16] = sizeof(u16), | |
37 | [NLA_U32] = sizeof(u32), | |
38 | [NLA_U64] = sizeof(u64), | |
28033ae4 DA |
39 | [NLA_MSECS] = sizeof(u64), |
40 | [NLA_NESTED] = NLA_HDRLEN, | |
6e237d09 DA |
41 | [NLA_S8] = sizeof(s8), |
42 | [NLA_S16] = sizeof(s16), | |
43 | [NLA_S32] = sizeof(s32), | |
44 | [NLA_S64] = sizeof(s64), | |
28033ae4 DA |
45 | }; |
46 | ||
64c83d83 JHS |
47 | static int validate_nla_bitfield32(const struct nlattr *nla, |
48 | u32 *valid_flags_allowed) | |
49 | { | |
50 | const struct nla_bitfield32 *bf = nla_data(nla); | |
51 | u32 *valid_flags_mask = valid_flags_allowed; | |
52 | ||
53 | if (!valid_flags_allowed) | |
54 | return -EINVAL; | |
55 | ||
56 | /*disallow invalid bit selector */ | |
57 | if (bf->selector & ~*valid_flags_mask) | |
58 | return -EINVAL; | |
59 | ||
60 | /*disallow invalid bit values */ | |
61 | if (bf->value & ~*valid_flags_mask) | |
62 | return -EINVAL; | |
63 | ||
64 | /*disallow valid bit values that are not selected*/ | |
65 | if (bf->value & ~bf->selector) | |
66 | return -EINVAL; | |
67 | ||
68 | return 0; | |
69 | } | |
70 | ||
3654654f | 71 | static int validate_nla(const struct nlattr *nla, int maxtype, |
568b742a JB |
72 | const struct nla_policy *policy, |
73 | const char **error_msg) | |
bfa83a9e | 74 | { |
ef7c79ed | 75 | const struct nla_policy *pt; |
8f4c1f9b | 76 | int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla); |
bfa83a9e | 77 | |
8f4c1f9b | 78 | if (type <= 0 || type > maxtype) |
bfa83a9e TG |
79 | return 0; |
80 | ||
8f4c1f9b | 81 | pt = &policy[type]; |
bfa83a9e TG |
82 | |
83 | BUG_ON(pt->type > NLA_TYPE_MAX); | |
84 | ||
6e237d09 DA |
85 | if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) { |
86 | pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n", | |
87 | current->comm, type); | |
28033ae4 DA |
88 | } |
89 | ||
a5531a5d | 90 | switch (pt->type) { |
568b742a JB |
91 | case NLA_REJECT: |
92 | if (pt->validation_data && error_msg) | |
93 | *error_msg = pt->validation_data; | |
94 | return -EINVAL; | |
95 | ||
a5531a5d TG |
96 | case NLA_FLAG: |
97 | if (attrlen > 0) | |
98 | return -ERANGE; | |
99 | break; | |
bfa83a9e | 100 | |
64c83d83 JHS |
101 | case NLA_BITFIELD32: |
102 | if (attrlen != sizeof(struct nla_bitfield32)) | |
103 | return -ERANGE; | |
104 | ||
105 | return validate_nla_bitfield32(nla, pt->validation_data); | |
106 | ||
a5531a5d TG |
107 | case NLA_NUL_STRING: |
108 | if (pt->len) | |
109 | minlen = min_t(int, attrlen, pt->len + 1); | |
110 | else | |
111 | minlen = attrlen; | |
bfa83a9e | 112 | |
a5531a5d TG |
113 | if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) |
114 | return -EINVAL; | |
115 | /* fall through */ | |
116 | ||
117 | case NLA_STRING: | |
118 | if (attrlen < 1) | |
119 | return -ERANGE; | |
120 | ||
121 | if (pt->len) { | |
122 | char *buf = nla_data(nla); | |
123 | ||
124 | if (buf[attrlen - 1] == '\0') | |
125 | attrlen--; | |
126 | ||
127 | if (attrlen > pt->len) | |
128 | return -ERANGE; | |
129 | } | |
130 | break; | |
131 | ||
d30045a0 JB |
132 | case NLA_BINARY: |
133 | if (pt->len && attrlen > pt->len) | |
134 | return -ERANGE; | |
135 | break; | |
136 | ||
1092cb21 PM |
137 | case NLA_NESTED_COMPAT: |
138 | if (attrlen < pt->len) | |
139 | return -ERANGE; | |
140 | if (attrlen < NLA_ALIGN(pt->len)) | |
141 | break; | |
142 | if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN) | |
143 | return -ERANGE; | |
144 | nla = nla_data(nla) + NLA_ALIGN(pt->len); | |
145 | if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla)) | |
146 | return -ERANGE; | |
147 | break; | |
ea5693cc PM |
148 | case NLA_NESTED: |
149 | /* a nested attributes is allowed to be empty; if its not, | |
150 | * it must have a size of at least NLA_HDRLEN. | |
151 | */ | |
152 | if (attrlen == 0) | |
153 | break; | |
a5531a5d TG |
154 | default: |
155 | if (pt->len) | |
156 | minlen = pt->len; | |
157 | else if (pt->type != NLA_UNSPEC) | |
158 | minlen = nla_attr_minlen[pt->type]; | |
159 | ||
160 | if (attrlen < minlen) | |
161 | return -ERANGE; | |
162 | } | |
bfa83a9e TG |
163 | |
164 | return 0; | |
165 | } | |
166 | ||
167 | /** | |
168 | * nla_validate - Validate a stream of attributes | |
169 | * @head: head of attribute stream | |
170 | * @len: length of attribute stream | |
171 | * @maxtype: maximum attribute type to be expected | |
172 | * @policy: validation policy | |
fceb6435 | 173 | * @extack: extended ACK report struct |
bfa83a9e TG |
174 | * |
175 | * Validates all attributes in the specified attribute stream against the | |
176 | * specified policy. Attributes with a type exceeding maxtype will be | |
177 | * ignored. See documenation of struct nla_policy for more details. | |
178 | * | |
179 | * Returns 0 on success or a negative error code. | |
180 | */ | |
3654654f | 181 | int nla_validate(const struct nlattr *head, int len, int maxtype, |
fceb6435 JB |
182 | const struct nla_policy *policy, |
183 | struct netlink_ext_ack *extack) | |
bfa83a9e | 184 | { |
3654654f | 185 | const struct nlattr *nla; |
fceb6435 | 186 | int rem; |
bfa83a9e TG |
187 | |
188 | nla_for_each_attr(nla, head, len, rem) { | |
568b742a | 189 | int err = validate_nla(nla, maxtype, policy, NULL); |
fceb6435 JB |
190 | |
191 | if (err < 0) { | |
568b742a | 192 | NL_SET_BAD_ATTR(extack, nla); |
fceb6435 JB |
193 | return err; |
194 | } | |
bfa83a9e TG |
195 | } |
196 | ||
fceb6435 | 197 | return 0; |
bfa83a9e | 198 | } |
6d6a138f | 199 | EXPORT_SYMBOL(nla_validate); |
bfa83a9e | 200 | |
e487eb99 HE |
201 | /** |
202 | * nla_policy_len - Determin the max. length of a policy | |
203 | * @policy: policy to use | |
204 | * @n: number of policies | |
205 | * | |
206 | * Determines the max. length of the policy. It is currently used | |
207 | * to allocated Netlink buffers roughly the size of the actual | |
208 | * message. | |
209 | * | |
210 | * Returns 0 on success or a negative error code. | |
211 | */ | |
212 | int | |
213 | nla_policy_len(const struct nla_policy *p, int n) | |
214 | { | |
215 | int i, len = 0; | |
216 | ||
e3fa3aff | 217 | for (i = 0; i < n; i++, p++) { |
e487eb99 HE |
218 | if (p->len) |
219 | len += nla_total_size(p->len); | |
28033ae4 DA |
220 | else if (nla_attr_len[p->type]) |
221 | len += nla_total_size(nla_attr_len[p->type]); | |
e487eb99 HE |
222 | else if (nla_attr_minlen[p->type]) |
223 | len += nla_total_size(nla_attr_minlen[p->type]); | |
224 | } | |
225 | ||
226 | return len; | |
227 | } | |
6d6a138f | 228 | EXPORT_SYMBOL(nla_policy_len); |
e487eb99 | 229 | |
bfa83a9e TG |
230 | /** |
231 | * nla_parse - Parse a stream of attributes into a tb buffer | |
232 | * @tb: destination array with maxtype+1 elements | |
233 | * @maxtype: maximum attribute type to be expected | |
234 | * @head: head of attribute stream | |
235 | * @len: length of attribute stream | |
10b595af | 236 | * @policy: validation policy |
bfa83a9e TG |
237 | * |
238 | * Parses a stream of attributes and stores a pointer to each attribute in | |
b595076a | 239 | * the tb array accessible via the attribute type. Attributes with a type |
bfa83a9e TG |
240 | * exceeding maxtype will be silently ignored for backwards compatibility |
241 | * reasons. policy may be set to NULL if no validation is required. | |
242 | * | |
243 | * Returns 0 on success or a negative error code. | |
244 | */ | |
3654654f | 245 | int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, |
fceb6435 JB |
246 | int len, const struct nla_policy *policy, |
247 | struct netlink_ext_ack *extack) | |
bfa83a9e | 248 | { |
3654654f | 249 | const struct nlattr *nla; |
bfa83a9e TG |
250 | int rem, err; |
251 | ||
252 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); | |
253 | ||
254 | nla_for_each_attr(nla, head, len, rem) { | |
8f4c1f9b | 255 | u16 type = nla_type(nla); |
bfa83a9e TG |
256 | |
257 | if (type > 0 && type <= maxtype) { | |
568b742a JB |
258 | static const char _msg[] = "Attribute failed policy validation"; |
259 | const char *msg = _msg; | |
260 | ||
bfa83a9e | 261 | if (policy) { |
568b742a | 262 | err = validate_nla(nla, maxtype, policy, &msg); |
fceb6435 | 263 | if (err < 0) { |
568b742a JB |
264 | NL_SET_BAD_ATTR(extack, nla); |
265 | if (extack) | |
266 | extack->_msg = msg; | |
bfa83a9e | 267 | goto errout; |
fceb6435 | 268 | } |
bfa83a9e TG |
269 | } |
270 | ||
3654654f | 271 | tb[type] = (struct nlattr *)nla; |
bfa83a9e TG |
272 | } |
273 | } | |
274 | ||
275 | if (unlikely(rem > 0)) | |
bfc5184b MS |
276 | pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", |
277 | rem, current->comm); | |
bfa83a9e TG |
278 | |
279 | err = 0; | |
280 | errout: | |
281 | return err; | |
282 | } | |
6d6a138f | 283 | EXPORT_SYMBOL(nla_parse); |
bfa83a9e TG |
284 | |
285 | /** | |
286 | * nla_find - Find a specific attribute in a stream of attributes | |
287 | * @head: head of attribute stream | |
288 | * @len: length of attribute stream | |
289 | * @attrtype: type of attribute to look for | |
290 | * | |
291 | * Returns the first attribute in the stream matching the specified type. | |
292 | */ | |
3654654f | 293 | struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype) |
bfa83a9e | 294 | { |
3654654f | 295 | const struct nlattr *nla; |
bfa83a9e TG |
296 | int rem; |
297 | ||
298 | nla_for_each_attr(nla, head, len, rem) | |
8f4c1f9b | 299 | if (nla_type(nla) == attrtype) |
3654654f | 300 | return (struct nlattr *)nla; |
bfa83a9e TG |
301 | |
302 | return NULL; | |
303 | } | |
6d6a138f | 304 | EXPORT_SYMBOL(nla_find); |
bfa83a9e TG |
305 | |
306 | /** | |
307 | * nla_strlcpy - Copy string attribute payload into a sized buffer | |
308 | * @dst: where to copy the string to | |
10b595af | 309 | * @nla: attribute to copy the string from |
bfa83a9e TG |
310 | * @dstsize: size of destination buffer |
311 | * | |
312 | * Copies at most dstsize - 1 bytes into the destination buffer. | |
313 | * The result is always a valid NUL-terminated string. Unlike | |
314 | * strlcpy the destination buffer is always padded out. | |
315 | * | |
316 | * Returns the length of the source buffer. | |
317 | */ | |
318 | size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize) | |
319 | { | |
320 | size_t srclen = nla_len(nla); | |
321 | char *src = nla_data(nla); | |
322 | ||
323 | if (srclen > 0 && src[srclen - 1] == '\0') | |
324 | srclen--; | |
325 | ||
326 | if (dstsize > 0) { | |
327 | size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen; | |
328 | ||
329 | memset(dst, 0, dstsize); | |
330 | memcpy(dst, src, len); | |
331 | } | |
332 | ||
333 | return srclen; | |
334 | } | |
6d6a138f | 335 | EXPORT_SYMBOL(nla_strlcpy); |
bfa83a9e | 336 | |
2cf0c8b3 PS |
337 | /** |
338 | * nla_strdup - Copy string attribute payload into a newly allocated buffer | |
339 | * @nla: attribute to copy the string from | |
340 | * @flags: the type of memory to allocate (see kmalloc). | |
341 | * | |
342 | * Returns a pointer to the allocated buffer or NULL on error. | |
343 | */ | |
344 | char *nla_strdup(const struct nlattr *nla, gfp_t flags) | |
345 | { | |
346 | size_t srclen = nla_len(nla); | |
347 | char *src = nla_data(nla), *dst; | |
348 | ||
349 | if (srclen > 0 && src[srclen - 1] == '\0') | |
350 | srclen--; | |
351 | ||
352 | dst = kmalloc(srclen + 1, flags); | |
353 | if (dst != NULL) { | |
354 | memcpy(dst, src, srclen); | |
355 | dst[srclen] = '\0'; | |
356 | } | |
357 | return dst; | |
358 | } | |
359 | EXPORT_SYMBOL(nla_strdup); | |
360 | ||
bfa83a9e TG |
361 | /** |
362 | * nla_memcpy - Copy a netlink attribute into another memory area | |
363 | * @dest: where to copy to memcpy | |
364 | * @src: netlink attribute to copy from | |
365 | * @count: size of the destination area | |
366 | * | |
367 | * Note: The number of bytes copied is limited by the length of | |
368 | * attribute's payload. memcpy | |
369 | * | |
370 | * Returns the number of bytes copied. | |
371 | */ | |
b057efd4 | 372 | int nla_memcpy(void *dest, const struct nlattr *src, int count) |
bfa83a9e TG |
373 | { |
374 | int minlen = min_t(int, count, nla_len(src)); | |
375 | ||
376 | memcpy(dest, nla_data(src), minlen); | |
5899f047 JB |
377 | if (count > minlen) |
378 | memset(dest + minlen, 0, count - minlen); | |
bfa83a9e TG |
379 | |
380 | return minlen; | |
381 | } | |
6d6a138f | 382 | EXPORT_SYMBOL(nla_memcpy); |
bfa83a9e TG |
383 | |
384 | /** | |
385 | * nla_memcmp - Compare an attribute with sized memory area | |
386 | * @nla: netlink attribute | |
387 | * @data: memory area | |
388 | * @size: size of memory area | |
389 | */ | |
390 | int nla_memcmp(const struct nlattr *nla, const void *data, | |
391 | size_t size) | |
392 | { | |
393 | int d = nla_len(nla) - size; | |
394 | ||
395 | if (d == 0) | |
396 | d = memcmp(nla_data(nla), data, size); | |
397 | ||
398 | return d; | |
399 | } | |
6d6a138f | 400 | EXPORT_SYMBOL(nla_memcmp); |
bfa83a9e TG |
401 | |
402 | /** | |
403 | * nla_strcmp - Compare a string attribute against a string | |
404 | * @nla: netlink string attribute | |
405 | * @str: another string | |
406 | */ | |
407 | int nla_strcmp(const struct nlattr *nla, const char *str) | |
408 | { | |
8b7b9324 PN |
409 | int len = strlen(str); |
410 | char *buf = nla_data(nla); | |
411 | int attrlen = nla_len(nla); | |
412 | int d; | |
bfa83a9e | 413 | |
8b7b9324 PN |
414 | if (attrlen > 0 && buf[attrlen - 1] == '\0') |
415 | attrlen--; | |
416 | ||
417 | d = attrlen - len; | |
bfa83a9e TG |
418 | if (d == 0) |
419 | d = memcmp(nla_data(nla), str, len); | |
420 | ||
421 | return d; | |
422 | } | |
6d6a138f | 423 | EXPORT_SYMBOL(nla_strcmp); |
bfa83a9e | 424 | |
90800216 | 425 | #ifdef CONFIG_NET |
bfa83a9e TG |
426 | /** |
427 | * __nla_reserve - reserve room for attribute on the skb | |
428 | * @skb: socket buffer to reserve room on | |
429 | * @attrtype: attribute type | |
430 | * @attrlen: length of attribute payload | |
431 | * | |
432 | * Adds a netlink attribute header to a socket buffer and reserves | |
433 | * room for the payload but does not copy it. | |
434 | * | |
435 | * The caller is responsible to ensure that the skb provides enough | |
436 | * tailroom for the attribute header and payload. | |
437 | */ | |
438 | struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | |
439 | { | |
440 | struct nlattr *nla; | |
441 | ||
4df864c1 | 442 | nla = skb_put(skb, nla_total_size(attrlen)); |
bfa83a9e TG |
443 | nla->nla_type = attrtype; |
444 | nla->nla_len = nla_attr_size(attrlen); | |
445 | ||
446 | memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen)); | |
447 | ||
448 | return nla; | |
449 | } | |
90800216 | 450 | EXPORT_SYMBOL(__nla_reserve); |
bfa83a9e | 451 | |
089bf1a6 ND |
452 | /** |
453 | * __nla_reserve_64bit - reserve room for attribute on the skb and align it | |
454 | * @skb: socket buffer to reserve room on | |
455 | * @attrtype: attribute type | |
456 | * @attrlen: length of attribute payload | |
11a99573 | 457 | * @padattr: attribute type for the padding |
089bf1a6 ND |
458 | * |
459 | * Adds a netlink attribute header to a socket buffer and reserves | |
460 | * room for the payload but does not copy it. It also ensure that this | |
11a99573 | 461 | * attribute will have a 64-bit aligned nla_data() area. |
089bf1a6 ND |
462 | * |
463 | * The caller is responsible to ensure that the skb provides enough | |
464 | * tailroom for the attribute header and payload. | |
465 | */ | |
466 | struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, | |
467 | int attrlen, int padattr) | |
468 | { | |
469 | if (nla_need_padding_for_64bit(skb)) | |
470 | nla_align_64bit(skb, padattr); | |
471 | ||
472 | return __nla_reserve(skb, attrtype, attrlen); | |
473 | } | |
474 | EXPORT_SYMBOL(__nla_reserve_64bit); | |
475 | ||
fe4944e5 TG |
476 | /** |
477 | * __nla_reserve_nohdr - reserve room for attribute without header | |
478 | * @skb: socket buffer to reserve room on | |
479 | * @attrlen: length of attribute payload | |
480 | * | |
481 | * Reserves room for attribute payload without a header. | |
482 | * | |
483 | * The caller is responsible to ensure that the skb provides enough | |
484 | * tailroom for the payload. | |
485 | */ | |
486 | void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen) | |
487 | { | |
b952f4df | 488 | return skb_put_zero(skb, NLA_ALIGN(attrlen)); |
fe4944e5 | 489 | } |
90800216 | 490 | EXPORT_SYMBOL(__nla_reserve_nohdr); |
fe4944e5 | 491 | |
bfa83a9e TG |
492 | /** |
493 | * nla_reserve - reserve room for attribute on the skb | |
494 | * @skb: socket buffer to reserve room on | |
495 | * @attrtype: attribute type | |
496 | * @attrlen: length of attribute payload | |
497 | * | |
498 | * Adds a netlink attribute header to a socket buffer and reserves | |
499 | * room for the payload but does not copy it. | |
500 | * | |
501 | * Returns NULL if the tailroom of the skb is insufficient to store | |
502 | * the attribute header and payload. | |
503 | */ | |
504 | struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | |
505 | { | |
506 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | |
507 | return NULL; | |
508 | ||
509 | return __nla_reserve(skb, attrtype, attrlen); | |
510 | } | |
90800216 | 511 | EXPORT_SYMBOL(nla_reserve); |
bfa83a9e | 512 | |
089bf1a6 ND |
513 | /** |
514 | * nla_reserve_64bit - reserve room for attribute on the skb and align it | |
515 | * @skb: socket buffer to reserve room on | |
516 | * @attrtype: attribute type | |
517 | * @attrlen: length of attribute payload | |
11a99573 | 518 | * @padattr: attribute type for the padding |
089bf1a6 ND |
519 | * |
520 | * Adds a netlink attribute header to a socket buffer and reserves | |
521 | * room for the payload but does not copy it. It also ensure that this | |
11a99573 | 522 | * attribute will have a 64-bit aligned nla_data() area. |
089bf1a6 ND |
523 | * |
524 | * Returns NULL if the tailroom of the skb is insufficient to store | |
525 | * the attribute header and payload. | |
526 | */ | |
527 | struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
528 | int padattr) | |
529 | { | |
530 | size_t len; | |
531 | ||
532 | if (nla_need_padding_for_64bit(skb)) | |
533 | len = nla_total_size_64bit(attrlen); | |
534 | else | |
535 | len = nla_total_size(attrlen); | |
536 | if (unlikely(skb_tailroom(skb) < len)) | |
537 | return NULL; | |
538 | ||
539 | return __nla_reserve_64bit(skb, attrtype, attrlen, padattr); | |
540 | } | |
541 | EXPORT_SYMBOL(nla_reserve_64bit); | |
542 | ||
fe4944e5 | 543 | /** |
10b595af | 544 | * nla_reserve_nohdr - reserve room for attribute without header |
fe4944e5 | 545 | * @skb: socket buffer to reserve room on |
10b595af | 546 | * @attrlen: length of attribute payload |
fe4944e5 TG |
547 | * |
548 | * Reserves room for attribute payload without a header. | |
549 | * | |
550 | * Returns NULL if the tailroom of the skb is insufficient to store | |
551 | * the attribute payload. | |
552 | */ | |
553 | void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen) | |
554 | { | |
555 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
556 | return NULL; | |
557 | ||
558 | return __nla_reserve_nohdr(skb, attrlen); | |
559 | } | |
90800216 | 560 | EXPORT_SYMBOL(nla_reserve_nohdr); |
fe4944e5 | 561 | |
bfa83a9e TG |
562 | /** |
563 | * __nla_put - Add a netlink attribute to a socket buffer | |
564 | * @skb: socket buffer to add attribute to | |
565 | * @attrtype: attribute type | |
566 | * @attrlen: length of attribute payload | |
567 | * @data: head of attribute payload | |
568 | * | |
569 | * The caller is responsible to ensure that the skb provides enough | |
570 | * tailroom for the attribute header and payload. | |
571 | */ | |
572 | void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, | |
573 | const void *data) | |
574 | { | |
575 | struct nlattr *nla; | |
576 | ||
577 | nla = __nla_reserve(skb, attrtype, attrlen); | |
578 | memcpy(nla_data(nla), data, attrlen); | |
579 | } | |
90800216 | 580 | EXPORT_SYMBOL(__nla_put); |
bfa83a9e | 581 | |
089bf1a6 ND |
582 | /** |
583 | * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it | |
584 | * @skb: socket buffer to add attribute to | |
585 | * @attrtype: attribute type | |
586 | * @attrlen: length of attribute payload | |
587 | * @data: head of attribute payload | |
11a99573 | 588 | * @padattr: attribute type for the padding |
089bf1a6 ND |
589 | * |
590 | * The caller is responsible to ensure that the skb provides enough | |
591 | * tailroom for the attribute header and payload. | |
592 | */ | |
593 | void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
594 | const void *data, int padattr) | |
595 | { | |
596 | struct nlattr *nla; | |
597 | ||
598 | nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr); | |
599 | memcpy(nla_data(nla), data, attrlen); | |
600 | } | |
601 | EXPORT_SYMBOL(__nla_put_64bit); | |
602 | ||
fe4944e5 TG |
603 | /** |
604 | * __nla_put_nohdr - Add a netlink attribute without header | |
605 | * @skb: socket buffer to add attribute to | |
606 | * @attrlen: length of attribute payload | |
607 | * @data: head of attribute payload | |
608 | * | |
609 | * The caller is responsible to ensure that the skb provides enough | |
610 | * tailroom for the attribute payload. | |
611 | */ | |
612 | void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) | |
613 | { | |
614 | void *start; | |
615 | ||
616 | start = __nla_reserve_nohdr(skb, attrlen); | |
617 | memcpy(start, data, attrlen); | |
618 | } | |
90800216 | 619 | EXPORT_SYMBOL(__nla_put_nohdr); |
bfa83a9e TG |
620 | |
621 | /** | |
622 | * nla_put - Add a netlink attribute to a socket buffer | |
623 | * @skb: socket buffer to add attribute to | |
624 | * @attrtype: attribute type | |
625 | * @attrlen: length of attribute payload | |
626 | * @data: head of attribute payload | |
627 | * | |
bc3ed28c | 628 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
bfa83a9e TG |
629 | * the attribute header and payload. |
630 | */ | |
631 | int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data) | |
632 | { | |
633 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | |
bc3ed28c | 634 | return -EMSGSIZE; |
bfa83a9e TG |
635 | |
636 | __nla_put(skb, attrtype, attrlen, data); | |
637 | return 0; | |
638 | } | |
90800216 | 639 | EXPORT_SYMBOL(nla_put); |
bfa83a9e | 640 | |
089bf1a6 ND |
641 | /** |
642 | * nla_put_64bit - Add a netlink attribute to a socket buffer and align it | |
643 | * @skb: socket buffer to add attribute to | |
644 | * @attrtype: attribute type | |
645 | * @attrlen: length of attribute payload | |
646 | * @data: head of attribute payload | |
11a99573 | 647 | * @padattr: attribute type for the padding |
089bf1a6 ND |
648 | * |
649 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store | |
650 | * the attribute header and payload. | |
651 | */ | |
652 | int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
653 | const void *data, int padattr) | |
654 | { | |
655 | size_t len; | |
656 | ||
657 | if (nla_need_padding_for_64bit(skb)) | |
658 | len = nla_total_size_64bit(attrlen); | |
659 | else | |
660 | len = nla_total_size(attrlen); | |
661 | if (unlikely(skb_tailroom(skb) < len)) | |
662 | return -EMSGSIZE; | |
663 | ||
664 | __nla_put_64bit(skb, attrtype, attrlen, data, padattr); | |
665 | return 0; | |
666 | } | |
667 | EXPORT_SYMBOL(nla_put_64bit); | |
668 | ||
fe4944e5 TG |
669 | /** |
670 | * nla_put_nohdr - Add a netlink attribute without header | |
671 | * @skb: socket buffer to add attribute to | |
672 | * @attrlen: length of attribute payload | |
673 | * @data: head of attribute payload | |
674 | * | |
bc3ed28c | 675 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
fe4944e5 TG |
676 | * the attribute payload. |
677 | */ | |
678 | int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) | |
679 | { | |
680 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
bc3ed28c | 681 | return -EMSGSIZE; |
fe4944e5 TG |
682 | |
683 | __nla_put_nohdr(skb, attrlen, data); | |
684 | return 0; | |
685 | } | |
90800216 | 686 | EXPORT_SYMBOL(nla_put_nohdr); |
bfa83a9e | 687 | |
01480e1c PM |
688 | /** |
689 | * nla_append - Add a netlink attribute without header or padding | |
690 | * @skb: socket buffer to add attribute to | |
691 | * @attrlen: length of attribute payload | |
692 | * @data: head of attribute payload | |
693 | * | |
bc3ed28c | 694 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
01480e1c PM |
695 | * the attribute payload. |
696 | */ | |
697 | int nla_append(struct sk_buff *skb, int attrlen, const void *data) | |
698 | { | |
699 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
bc3ed28c | 700 | return -EMSGSIZE; |
01480e1c | 701 | |
59ae1d12 | 702 | skb_put_data(skb, data, attrlen); |
01480e1c PM |
703 | return 0; |
704 | } | |
90800216 HX |
705 | EXPORT_SYMBOL(nla_append); |
706 | #endif |