]>
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 | 47 | static int validate_nla_bitfield32(const struct nlattr *nla, |
48fde90a | 48 | const u32 *valid_flags_mask) |
64c83d83 JHS |
49 | { |
50 | const struct nla_bitfield32 *bf = nla_data(nla); | |
64c83d83 | 51 | |
48fde90a | 52 | if (!valid_flags_mask) |
64c83d83 JHS |
53 | return -EINVAL; |
54 | ||
55 | /*disallow invalid bit selector */ | |
56 | if (bf->selector & ~*valid_flags_mask) | |
57 | return -EINVAL; | |
58 | ||
59 | /*disallow invalid bit values */ | |
60 | if (bf->value & ~*valid_flags_mask) | |
61 | return -EINVAL; | |
62 | ||
63 | /*disallow valid bit values that are not selected*/ | |
64 | if (bf->value & ~bf->selector) | |
65 | return -EINVAL; | |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
1501d135 JB |
70 | static int nla_validate_array(const struct nlattr *head, int len, int maxtype, |
71 | const struct nla_policy *policy, | |
72 | struct netlink_ext_ack *extack) | |
73 | { | |
74 | const struct nlattr *entry; | |
75 | int rem; | |
76 | ||
77 | nla_for_each_attr(entry, head, len, rem) { | |
78 | int ret; | |
79 | ||
80 | if (nla_len(entry) == 0) | |
81 | continue; | |
82 | ||
83 | if (nla_len(entry) < NLA_HDRLEN) { | |
84 | NL_SET_ERR_MSG_ATTR(extack, entry, | |
85 | "Array element too short"); | |
86 | return -ERANGE; | |
87 | } | |
88 | ||
89 | ret = nla_validate(nla_data(entry), nla_len(entry), | |
90 | maxtype, policy, extack); | |
91 | if (ret < 0) | |
92 | return ret; | |
93 | } | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
3e48be05 JB |
98 | static int nla_validate_int_range(const struct nla_policy *pt, |
99 | const struct nlattr *nla, | |
100 | struct netlink_ext_ack *extack) | |
101 | { | |
102 | bool validate_min, validate_max; | |
103 | s64 value; | |
104 | ||
105 | validate_min = pt->validation_type == NLA_VALIDATE_RANGE || | |
106 | pt->validation_type == NLA_VALIDATE_MIN; | |
107 | validate_max = pt->validation_type == NLA_VALIDATE_RANGE || | |
108 | pt->validation_type == NLA_VALIDATE_MAX; | |
109 | ||
110 | switch (pt->type) { | |
111 | case NLA_U8: | |
112 | value = nla_get_u8(nla); | |
113 | break; | |
114 | case NLA_U16: | |
115 | value = nla_get_u16(nla); | |
116 | break; | |
117 | case NLA_U32: | |
118 | value = nla_get_u32(nla); | |
119 | break; | |
120 | case NLA_S8: | |
121 | value = nla_get_s8(nla); | |
122 | break; | |
123 | case NLA_S16: | |
124 | value = nla_get_s16(nla); | |
125 | break; | |
126 | case NLA_S32: | |
127 | value = nla_get_s32(nla); | |
128 | break; | |
129 | case NLA_S64: | |
130 | value = nla_get_s64(nla); | |
131 | break; | |
132 | case NLA_U64: | |
133 | /* treat this one specially, since it may not fit into s64 */ | |
134 | if ((validate_min && nla_get_u64(nla) < pt->min) || | |
135 | (validate_max && nla_get_u64(nla) > pt->max)) { | |
136 | NL_SET_ERR_MSG_ATTR(extack, nla, | |
137 | "integer out of range"); | |
138 | return -ERANGE; | |
139 | } | |
140 | return 0; | |
141 | default: | |
142 | WARN_ON(1); | |
143 | return -EINVAL; | |
144 | } | |
145 | ||
146 | if ((validate_min && value < pt->min) || | |
147 | (validate_max && value > pt->max)) { | |
148 | NL_SET_ERR_MSG_ATTR(extack, nla, | |
149 | "integer out of range"); | |
150 | return -ERANGE; | |
151 | } | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
3654654f | 156 | static int validate_nla(const struct nlattr *nla, int maxtype, |
568b742a | 157 | const struct nla_policy *policy, |
c29f1845 | 158 | struct netlink_ext_ack *extack) |
bfa83a9e | 159 | { |
ef7c79ed | 160 | const struct nla_policy *pt; |
8f4c1f9b | 161 | int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla); |
c29f1845 | 162 | int err = -ERANGE; |
bfa83a9e | 163 | |
8f4c1f9b | 164 | if (type <= 0 || type > maxtype) |
bfa83a9e TG |
165 | return 0; |
166 | ||
8f4c1f9b | 167 | pt = &policy[type]; |
bfa83a9e TG |
168 | |
169 | BUG_ON(pt->type > NLA_TYPE_MAX); | |
170 | ||
b60b87fc JB |
171 | if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) || |
172 | (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) { | |
6e237d09 DA |
173 | pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n", |
174 | current->comm, type); | |
28033ae4 DA |
175 | } |
176 | ||
a5531a5d | 177 | switch (pt->type) { |
b60b87fc JB |
178 | case NLA_EXACT_LEN: |
179 | if (attrlen != pt->len) | |
c29f1845 | 180 | goto out_err; |
b60b87fc JB |
181 | break; |
182 | ||
568b742a | 183 | case NLA_REJECT: |
c29f1845 JB |
184 | if (extack && pt->validation_data) { |
185 | NL_SET_BAD_ATTR(extack, nla); | |
186 | extack->_msg = pt->validation_data; | |
187 | return -EINVAL; | |
188 | } | |
189 | err = -EINVAL; | |
190 | goto out_err; | |
568b742a | 191 | |
a5531a5d TG |
192 | case NLA_FLAG: |
193 | if (attrlen > 0) | |
c29f1845 | 194 | goto out_err; |
a5531a5d | 195 | break; |
bfa83a9e | 196 | |
64c83d83 JHS |
197 | case NLA_BITFIELD32: |
198 | if (attrlen != sizeof(struct nla_bitfield32)) | |
c29f1845 | 199 | goto out_err; |
64c83d83 | 200 | |
c29f1845 JB |
201 | err = validate_nla_bitfield32(nla, pt->validation_data); |
202 | if (err) | |
203 | goto out_err; | |
204 | break; | |
64c83d83 | 205 | |
a5531a5d TG |
206 | case NLA_NUL_STRING: |
207 | if (pt->len) | |
208 | minlen = min_t(int, attrlen, pt->len + 1); | |
209 | else | |
210 | minlen = attrlen; | |
bfa83a9e | 211 | |
c29f1845 JB |
212 | if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) { |
213 | err = -EINVAL; | |
214 | goto out_err; | |
215 | } | |
a5531a5d TG |
216 | /* fall through */ |
217 | ||
218 | case NLA_STRING: | |
219 | if (attrlen < 1) | |
c29f1845 | 220 | goto out_err; |
a5531a5d TG |
221 | |
222 | if (pt->len) { | |
223 | char *buf = nla_data(nla); | |
224 | ||
225 | if (buf[attrlen - 1] == '\0') | |
226 | attrlen--; | |
227 | ||
228 | if (attrlen > pt->len) | |
c29f1845 | 229 | goto out_err; |
a5531a5d TG |
230 | } |
231 | break; | |
232 | ||
d30045a0 JB |
233 | case NLA_BINARY: |
234 | if (pt->len && attrlen > pt->len) | |
c29f1845 | 235 | goto out_err; |
d30045a0 JB |
236 | break; |
237 | ||
ea5693cc PM |
238 | case NLA_NESTED: |
239 | /* a nested attributes is allowed to be empty; if its not, | |
240 | * it must have a size of at least NLA_HDRLEN. | |
241 | */ | |
242 | if (attrlen == 0) | |
243 | break; | |
9a659a35 JB |
244 | if (attrlen < NLA_HDRLEN) |
245 | goto out_err; | |
246 | if (pt->validation_data) { | |
247 | err = nla_validate(nla_data(nla), nla_len(nla), pt->len, | |
248 | pt->validation_data, extack); | |
249 | if (err < 0) { | |
250 | /* | |
251 | * return directly to preserve the inner | |
252 | * error message/attribute pointer | |
253 | */ | |
254 | return err; | |
255 | } | |
256 | } | |
257 | break; | |
1501d135 JB |
258 | case NLA_NESTED_ARRAY: |
259 | /* a nested array attribute is allowed to be empty; if its not, | |
260 | * it must have a size of at least NLA_HDRLEN. | |
261 | */ | |
262 | if (attrlen == 0) | |
263 | break; | |
264 | if (attrlen < NLA_HDRLEN) | |
265 | goto out_err; | |
266 | if (pt->validation_data) { | |
267 | int err; | |
268 | ||
269 | err = nla_validate_array(nla_data(nla), nla_len(nla), | |
270 | pt->len, pt->validation_data, | |
271 | extack); | |
272 | if (err < 0) { | |
273 | /* | |
274 | * return directly to preserve the inner | |
275 | * error message/attribute pointer | |
276 | */ | |
277 | return err; | |
278 | } | |
279 | } | |
280 | break; | |
a5531a5d TG |
281 | default: |
282 | if (pt->len) | |
283 | minlen = pt->len; | |
284 | else if (pt->type != NLA_UNSPEC) | |
285 | minlen = nla_attr_minlen[pt->type]; | |
286 | ||
287 | if (attrlen < minlen) | |
c29f1845 | 288 | goto out_err; |
a5531a5d | 289 | } |
bfa83a9e | 290 | |
3e48be05 JB |
291 | /* further validation */ |
292 | switch (pt->validation_type) { | |
293 | case NLA_VALIDATE_NONE: | |
294 | /* nothing to do */ | |
295 | break; | |
296 | case NLA_VALIDATE_RANGE: | |
297 | case NLA_VALIDATE_MIN: | |
298 | case NLA_VALIDATE_MAX: | |
299 | err = nla_validate_int_range(pt, nla, extack); | |
300 | if (err) | |
301 | return err; | |
302 | break; | |
303 | } | |
304 | ||
bfa83a9e | 305 | return 0; |
c29f1845 JB |
306 | out_err: |
307 | NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation"); | |
308 | return err; | |
bfa83a9e TG |
309 | } |
310 | ||
311 | /** | |
312 | * nla_validate - Validate a stream of attributes | |
313 | * @head: head of attribute stream | |
314 | * @len: length of attribute stream | |
315 | * @maxtype: maximum attribute type to be expected | |
316 | * @policy: validation policy | |
fceb6435 | 317 | * @extack: extended ACK report struct |
bfa83a9e TG |
318 | * |
319 | * Validates all attributes in the specified attribute stream against the | |
320 | * specified policy. Attributes with a type exceeding maxtype will be | |
321 | * ignored. See documenation of struct nla_policy for more details. | |
322 | * | |
323 | * Returns 0 on success or a negative error code. | |
324 | */ | |
3654654f | 325 | int nla_validate(const struct nlattr *head, int len, int maxtype, |
fceb6435 JB |
326 | const struct nla_policy *policy, |
327 | struct netlink_ext_ack *extack) | |
bfa83a9e | 328 | { |
3654654f | 329 | const struct nlattr *nla; |
fceb6435 | 330 | int rem; |
bfa83a9e TG |
331 | |
332 | nla_for_each_attr(nla, head, len, rem) { | |
c29f1845 | 333 | int err = validate_nla(nla, maxtype, policy, extack); |
fceb6435 | 334 | |
c29f1845 | 335 | if (err < 0) |
fceb6435 | 336 | return err; |
bfa83a9e TG |
337 | } |
338 | ||
fceb6435 | 339 | return 0; |
bfa83a9e | 340 | } |
6d6a138f | 341 | EXPORT_SYMBOL(nla_validate); |
bfa83a9e | 342 | |
e487eb99 HE |
343 | /** |
344 | * nla_policy_len - Determin the max. length of a policy | |
345 | * @policy: policy to use | |
346 | * @n: number of policies | |
347 | * | |
348 | * Determines the max. length of the policy. It is currently used | |
349 | * to allocated Netlink buffers roughly the size of the actual | |
350 | * message. | |
351 | * | |
352 | * Returns 0 on success or a negative error code. | |
353 | */ | |
354 | int | |
355 | nla_policy_len(const struct nla_policy *p, int n) | |
356 | { | |
357 | int i, len = 0; | |
358 | ||
e3fa3aff | 359 | for (i = 0; i < n; i++, p++) { |
e487eb99 HE |
360 | if (p->len) |
361 | len += nla_total_size(p->len); | |
28033ae4 DA |
362 | else if (nla_attr_len[p->type]) |
363 | len += nla_total_size(nla_attr_len[p->type]); | |
e487eb99 HE |
364 | else if (nla_attr_minlen[p->type]) |
365 | len += nla_total_size(nla_attr_minlen[p->type]); | |
366 | } | |
367 | ||
368 | return len; | |
369 | } | |
6d6a138f | 370 | EXPORT_SYMBOL(nla_policy_len); |
e487eb99 | 371 | |
bfa83a9e TG |
372 | /** |
373 | * nla_parse - Parse a stream of attributes into a tb buffer | |
374 | * @tb: destination array with maxtype+1 elements | |
375 | * @maxtype: maximum attribute type to be expected | |
376 | * @head: head of attribute stream | |
377 | * @len: length of attribute stream | |
10b595af | 378 | * @policy: validation policy |
bfa83a9e TG |
379 | * |
380 | * Parses a stream of attributes and stores a pointer to each attribute in | |
b595076a | 381 | * the tb array accessible via the attribute type. Attributes with a type |
bfa83a9e TG |
382 | * exceeding maxtype will be silently ignored for backwards compatibility |
383 | * reasons. policy may be set to NULL if no validation is required. | |
384 | * | |
385 | * Returns 0 on success or a negative error code. | |
386 | */ | |
3654654f | 387 | int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, |
fceb6435 JB |
388 | int len, const struct nla_policy *policy, |
389 | struct netlink_ext_ack *extack) | |
bfa83a9e | 390 | { |
3654654f | 391 | const struct nlattr *nla; |
c29f1845 | 392 | int rem; |
bfa83a9e TG |
393 | |
394 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); | |
395 | ||
396 | nla_for_each_attr(nla, head, len, rem) { | |
8f4c1f9b | 397 | u16 type = nla_type(nla); |
bfa83a9e TG |
398 | |
399 | if (type > 0 && type <= maxtype) { | |
400 | if (policy) { | |
c29f1845 JB |
401 | int err = validate_nla(nla, maxtype, policy, |
402 | extack); | |
403 | ||
404 | if (err < 0) | |
405 | return err; | |
bfa83a9e TG |
406 | } |
407 | ||
3654654f | 408 | tb[type] = (struct nlattr *)nla; |
bfa83a9e TG |
409 | } |
410 | } | |
411 | ||
412 | if (unlikely(rem > 0)) | |
bfc5184b MS |
413 | pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", |
414 | rem, current->comm); | |
bfa83a9e | 415 | |
c29f1845 | 416 | return 0; |
bfa83a9e | 417 | } |
6d6a138f | 418 | EXPORT_SYMBOL(nla_parse); |
bfa83a9e TG |
419 | |
420 | /** | |
421 | * nla_find - Find a specific attribute in a stream of attributes | |
422 | * @head: head of attribute stream | |
423 | * @len: length of attribute stream | |
424 | * @attrtype: type of attribute to look for | |
425 | * | |
426 | * Returns the first attribute in the stream matching the specified type. | |
427 | */ | |
3654654f | 428 | struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype) |
bfa83a9e | 429 | { |
3654654f | 430 | const struct nlattr *nla; |
bfa83a9e TG |
431 | int rem; |
432 | ||
433 | nla_for_each_attr(nla, head, len, rem) | |
8f4c1f9b | 434 | if (nla_type(nla) == attrtype) |
3654654f | 435 | return (struct nlattr *)nla; |
bfa83a9e TG |
436 | |
437 | return NULL; | |
438 | } | |
6d6a138f | 439 | EXPORT_SYMBOL(nla_find); |
bfa83a9e TG |
440 | |
441 | /** | |
442 | * nla_strlcpy - Copy string attribute payload into a sized buffer | |
443 | * @dst: where to copy the string to | |
10b595af | 444 | * @nla: attribute to copy the string from |
bfa83a9e TG |
445 | * @dstsize: size of destination buffer |
446 | * | |
447 | * Copies at most dstsize - 1 bytes into the destination buffer. | |
448 | * The result is always a valid NUL-terminated string. Unlike | |
449 | * strlcpy the destination buffer is always padded out. | |
450 | * | |
451 | * Returns the length of the source buffer. | |
452 | */ | |
453 | size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize) | |
454 | { | |
455 | size_t srclen = nla_len(nla); | |
456 | char *src = nla_data(nla); | |
457 | ||
458 | if (srclen > 0 && src[srclen - 1] == '\0') | |
459 | srclen--; | |
460 | ||
461 | if (dstsize > 0) { | |
462 | size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen; | |
463 | ||
464 | memset(dst, 0, dstsize); | |
465 | memcpy(dst, src, len); | |
466 | } | |
467 | ||
468 | return srclen; | |
469 | } | |
6d6a138f | 470 | EXPORT_SYMBOL(nla_strlcpy); |
bfa83a9e | 471 | |
2cf0c8b3 PS |
472 | /** |
473 | * nla_strdup - Copy string attribute payload into a newly allocated buffer | |
474 | * @nla: attribute to copy the string from | |
475 | * @flags: the type of memory to allocate (see kmalloc). | |
476 | * | |
477 | * Returns a pointer to the allocated buffer or NULL on error. | |
478 | */ | |
479 | char *nla_strdup(const struct nlattr *nla, gfp_t flags) | |
480 | { | |
481 | size_t srclen = nla_len(nla); | |
482 | char *src = nla_data(nla), *dst; | |
483 | ||
484 | if (srclen > 0 && src[srclen - 1] == '\0') | |
485 | srclen--; | |
486 | ||
487 | dst = kmalloc(srclen + 1, flags); | |
488 | if (dst != NULL) { | |
489 | memcpy(dst, src, srclen); | |
490 | dst[srclen] = '\0'; | |
491 | } | |
492 | return dst; | |
493 | } | |
494 | EXPORT_SYMBOL(nla_strdup); | |
495 | ||
bfa83a9e TG |
496 | /** |
497 | * nla_memcpy - Copy a netlink attribute into another memory area | |
498 | * @dest: where to copy to memcpy | |
499 | * @src: netlink attribute to copy from | |
500 | * @count: size of the destination area | |
501 | * | |
502 | * Note: The number of bytes copied is limited by the length of | |
503 | * attribute's payload. memcpy | |
504 | * | |
505 | * Returns the number of bytes copied. | |
506 | */ | |
b057efd4 | 507 | int nla_memcpy(void *dest, const struct nlattr *src, int count) |
bfa83a9e TG |
508 | { |
509 | int minlen = min_t(int, count, nla_len(src)); | |
510 | ||
511 | memcpy(dest, nla_data(src), minlen); | |
5899f047 JB |
512 | if (count > minlen) |
513 | memset(dest + minlen, 0, count - minlen); | |
bfa83a9e TG |
514 | |
515 | return minlen; | |
516 | } | |
6d6a138f | 517 | EXPORT_SYMBOL(nla_memcpy); |
bfa83a9e TG |
518 | |
519 | /** | |
520 | * nla_memcmp - Compare an attribute with sized memory area | |
521 | * @nla: netlink attribute | |
522 | * @data: memory area | |
523 | * @size: size of memory area | |
524 | */ | |
525 | int nla_memcmp(const struct nlattr *nla, const void *data, | |
526 | size_t size) | |
527 | { | |
528 | int d = nla_len(nla) - size; | |
529 | ||
530 | if (d == 0) | |
531 | d = memcmp(nla_data(nla), data, size); | |
532 | ||
533 | return d; | |
534 | } | |
6d6a138f | 535 | EXPORT_SYMBOL(nla_memcmp); |
bfa83a9e TG |
536 | |
537 | /** | |
538 | * nla_strcmp - Compare a string attribute against a string | |
539 | * @nla: netlink string attribute | |
540 | * @str: another string | |
541 | */ | |
542 | int nla_strcmp(const struct nlattr *nla, const char *str) | |
543 | { | |
8b7b9324 PN |
544 | int len = strlen(str); |
545 | char *buf = nla_data(nla); | |
546 | int attrlen = nla_len(nla); | |
547 | int d; | |
bfa83a9e | 548 | |
8b7b9324 PN |
549 | if (attrlen > 0 && buf[attrlen - 1] == '\0') |
550 | attrlen--; | |
551 | ||
552 | d = attrlen - len; | |
bfa83a9e TG |
553 | if (d == 0) |
554 | d = memcmp(nla_data(nla), str, len); | |
555 | ||
556 | return d; | |
557 | } | |
6d6a138f | 558 | EXPORT_SYMBOL(nla_strcmp); |
bfa83a9e | 559 | |
90800216 | 560 | #ifdef CONFIG_NET |
bfa83a9e TG |
561 | /** |
562 | * __nla_reserve - reserve room for attribute on the skb | |
563 | * @skb: socket buffer to reserve room on | |
564 | * @attrtype: attribute type | |
565 | * @attrlen: length of attribute payload | |
566 | * | |
567 | * Adds a netlink attribute header to a socket buffer and reserves | |
568 | * room for the payload but does not copy it. | |
569 | * | |
570 | * The caller is responsible to ensure that the skb provides enough | |
571 | * tailroom for the attribute header and payload. | |
572 | */ | |
573 | struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | |
574 | { | |
575 | struct nlattr *nla; | |
576 | ||
4df864c1 | 577 | nla = skb_put(skb, nla_total_size(attrlen)); |
bfa83a9e TG |
578 | nla->nla_type = attrtype; |
579 | nla->nla_len = nla_attr_size(attrlen); | |
580 | ||
581 | memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen)); | |
582 | ||
583 | return nla; | |
584 | } | |
90800216 | 585 | EXPORT_SYMBOL(__nla_reserve); |
bfa83a9e | 586 | |
089bf1a6 ND |
587 | /** |
588 | * __nla_reserve_64bit - reserve room for attribute on the skb and align it | |
589 | * @skb: socket buffer to reserve room on | |
590 | * @attrtype: attribute type | |
591 | * @attrlen: length of attribute payload | |
11a99573 | 592 | * @padattr: attribute type for the padding |
089bf1a6 ND |
593 | * |
594 | * Adds a netlink attribute header to a socket buffer and reserves | |
595 | * room for the payload but does not copy it. It also ensure that this | |
11a99573 | 596 | * attribute will have a 64-bit aligned nla_data() area. |
089bf1a6 ND |
597 | * |
598 | * The caller is responsible to ensure that the skb provides enough | |
599 | * tailroom for the attribute header and payload. | |
600 | */ | |
601 | struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, | |
602 | int attrlen, int padattr) | |
603 | { | |
604 | if (nla_need_padding_for_64bit(skb)) | |
605 | nla_align_64bit(skb, padattr); | |
606 | ||
607 | return __nla_reserve(skb, attrtype, attrlen); | |
608 | } | |
609 | EXPORT_SYMBOL(__nla_reserve_64bit); | |
610 | ||
fe4944e5 TG |
611 | /** |
612 | * __nla_reserve_nohdr - reserve room for attribute without header | |
613 | * @skb: socket buffer to reserve room on | |
614 | * @attrlen: length of attribute payload | |
615 | * | |
616 | * Reserves room for attribute payload without a header. | |
617 | * | |
618 | * The caller is responsible to ensure that the skb provides enough | |
619 | * tailroom for the payload. | |
620 | */ | |
621 | void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen) | |
622 | { | |
b952f4df | 623 | return skb_put_zero(skb, NLA_ALIGN(attrlen)); |
fe4944e5 | 624 | } |
90800216 | 625 | EXPORT_SYMBOL(__nla_reserve_nohdr); |
fe4944e5 | 626 | |
bfa83a9e TG |
627 | /** |
628 | * nla_reserve - reserve room for attribute on the skb | |
629 | * @skb: socket buffer to reserve room on | |
630 | * @attrtype: attribute type | |
631 | * @attrlen: length of attribute payload | |
632 | * | |
633 | * Adds a netlink attribute header to a socket buffer and reserves | |
634 | * room for the payload but does not copy it. | |
635 | * | |
636 | * Returns NULL if the tailroom of the skb is insufficient to store | |
637 | * the attribute header and payload. | |
638 | */ | |
639 | struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | |
640 | { | |
641 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | |
642 | return NULL; | |
643 | ||
644 | return __nla_reserve(skb, attrtype, attrlen); | |
645 | } | |
90800216 | 646 | EXPORT_SYMBOL(nla_reserve); |
bfa83a9e | 647 | |
089bf1a6 ND |
648 | /** |
649 | * nla_reserve_64bit - reserve room for attribute on the skb and align it | |
650 | * @skb: socket buffer to reserve room on | |
651 | * @attrtype: attribute type | |
652 | * @attrlen: length of attribute payload | |
11a99573 | 653 | * @padattr: attribute type for the padding |
089bf1a6 ND |
654 | * |
655 | * Adds a netlink attribute header to a socket buffer and reserves | |
656 | * room for the payload but does not copy it. It also ensure that this | |
11a99573 | 657 | * attribute will have a 64-bit aligned nla_data() area. |
089bf1a6 ND |
658 | * |
659 | * Returns NULL if the tailroom of the skb is insufficient to store | |
660 | * the attribute header and payload. | |
661 | */ | |
662 | struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
663 | int padattr) | |
664 | { | |
665 | size_t len; | |
666 | ||
667 | if (nla_need_padding_for_64bit(skb)) | |
668 | len = nla_total_size_64bit(attrlen); | |
669 | else | |
670 | len = nla_total_size(attrlen); | |
671 | if (unlikely(skb_tailroom(skb) < len)) | |
672 | return NULL; | |
673 | ||
674 | return __nla_reserve_64bit(skb, attrtype, attrlen, padattr); | |
675 | } | |
676 | EXPORT_SYMBOL(nla_reserve_64bit); | |
677 | ||
fe4944e5 | 678 | /** |
10b595af | 679 | * nla_reserve_nohdr - reserve room for attribute without header |
fe4944e5 | 680 | * @skb: socket buffer to reserve room on |
10b595af | 681 | * @attrlen: length of attribute payload |
fe4944e5 TG |
682 | * |
683 | * Reserves room for attribute payload without a header. | |
684 | * | |
685 | * Returns NULL if the tailroom of the skb is insufficient to store | |
686 | * the attribute payload. | |
687 | */ | |
688 | void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen) | |
689 | { | |
690 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
691 | return NULL; | |
692 | ||
693 | return __nla_reserve_nohdr(skb, attrlen); | |
694 | } | |
90800216 | 695 | EXPORT_SYMBOL(nla_reserve_nohdr); |
fe4944e5 | 696 | |
bfa83a9e TG |
697 | /** |
698 | * __nla_put - Add a netlink attribute to a socket buffer | |
699 | * @skb: socket buffer to add attribute to | |
700 | * @attrtype: attribute type | |
701 | * @attrlen: length of attribute payload | |
702 | * @data: head of attribute payload | |
703 | * | |
704 | * The caller is responsible to ensure that the skb provides enough | |
705 | * tailroom for the attribute header and payload. | |
706 | */ | |
707 | void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, | |
708 | const void *data) | |
709 | { | |
710 | struct nlattr *nla; | |
711 | ||
712 | nla = __nla_reserve(skb, attrtype, attrlen); | |
713 | memcpy(nla_data(nla), data, attrlen); | |
714 | } | |
90800216 | 715 | EXPORT_SYMBOL(__nla_put); |
bfa83a9e | 716 | |
089bf1a6 ND |
717 | /** |
718 | * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it | |
719 | * @skb: socket buffer to add attribute to | |
720 | * @attrtype: attribute type | |
721 | * @attrlen: length of attribute payload | |
722 | * @data: head of attribute payload | |
11a99573 | 723 | * @padattr: attribute type for the padding |
089bf1a6 ND |
724 | * |
725 | * The caller is responsible to ensure that the skb provides enough | |
726 | * tailroom for the attribute header and payload. | |
727 | */ | |
728 | void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
729 | const void *data, int padattr) | |
730 | { | |
731 | struct nlattr *nla; | |
732 | ||
733 | nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr); | |
734 | memcpy(nla_data(nla), data, attrlen); | |
735 | } | |
736 | EXPORT_SYMBOL(__nla_put_64bit); | |
737 | ||
fe4944e5 TG |
738 | /** |
739 | * __nla_put_nohdr - Add a netlink attribute without header | |
740 | * @skb: socket buffer to add attribute to | |
741 | * @attrlen: length of attribute payload | |
742 | * @data: head of attribute payload | |
743 | * | |
744 | * The caller is responsible to ensure that the skb provides enough | |
745 | * tailroom for the attribute payload. | |
746 | */ | |
747 | void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) | |
748 | { | |
749 | void *start; | |
750 | ||
751 | start = __nla_reserve_nohdr(skb, attrlen); | |
752 | memcpy(start, data, attrlen); | |
753 | } | |
90800216 | 754 | EXPORT_SYMBOL(__nla_put_nohdr); |
bfa83a9e TG |
755 | |
756 | /** | |
757 | * nla_put - Add a netlink attribute to a socket buffer | |
758 | * @skb: socket buffer to add attribute to | |
759 | * @attrtype: attribute type | |
760 | * @attrlen: length of attribute payload | |
761 | * @data: head of attribute payload | |
762 | * | |
bc3ed28c | 763 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
bfa83a9e TG |
764 | * the attribute header and payload. |
765 | */ | |
766 | int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data) | |
767 | { | |
768 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | |
bc3ed28c | 769 | return -EMSGSIZE; |
bfa83a9e TG |
770 | |
771 | __nla_put(skb, attrtype, attrlen, data); | |
772 | return 0; | |
773 | } | |
90800216 | 774 | EXPORT_SYMBOL(nla_put); |
bfa83a9e | 775 | |
089bf1a6 ND |
776 | /** |
777 | * nla_put_64bit - Add a netlink attribute to a socket buffer and align it | |
778 | * @skb: socket buffer to add attribute to | |
779 | * @attrtype: attribute type | |
780 | * @attrlen: length of attribute payload | |
781 | * @data: head of attribute payload | |
11a99573 | 782 | * @padattr: attribute type for the padding |
089bf1a6 ND |
783 | * |
784 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store | |
785 | * the attribute header and payload. | |
786 | */ | |
787 | int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
788 | const void *data, int padattr) | |
789 | { | |
790 | size_t len; | |
791 | ||
792 | if (nla_need_padding_for_64bit(skb)) | |
793 | len = nla_total_size_64bit(attrlen); | |
794 | else | |
795 | len = nla_total_size(attrlen); | |
796 | if (unlikely(skb_tailroom(skb) < len)) | |
797 | return -EMSGSIZE; | |
798 | ||
799 | __nla_put_64bit(skb, attrtype, attrlen, data, padattr); | |
800 | return 0; | |
801 | } | |
802 | EXPORT_SYMBOL(nla_put_64bit); | |
803 | ||
fe4944e5 TG |
804 | /** |
805 | * nla_put_nohdr - Add a netlink attribute without header | |
806 | * @skb: socket buffer to add attribute to | |
807 | * @attrlen: length of attribute payload | |
808 | * @data: head of attribute payload | |
809 | * | |
bc3ed28c | 810 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
fe4944e5 TG |
811 | * the attribute payload. |
812 | */ | |
813 | int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) | |
814 | { | |
815 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
bc3ed28c | 816 | return -EMSGSIZE; |
fe4944e5 TG |
817 | |
818 | __nla_put_nohdr(skb, attrlen, data); | |
819 | return 0; | |
820 | } | |
90800216 | 821 | EXPORT_SYMBOL(nla_put_nohdr); |
bfa83a9e | 822 | |
01480e1c PM |
823 | /** |
824 | * nla_append - Add a netlink attribute without header or padding | |
825 | * @skb: socket buffer to add attribute to | |
826 | * @attrlen: length of attribute payload | |
827 | * @data: head of attribute payload | |
828 | * | |
bc3ed28c | 829 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
01480e1c PM |
830 | * the attribute payload. |
831 | */ | |
832 | int nla_append(struct sk_buff *skb, int attrlen, const void *data) | |
833 | { | |
834 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
bc3ed28c | 835 | return -EMSGSIZE; |
01480e1c | 836 | |
59ae1d12 | 837 | skb_put_data(skb, data, attrlen); |
01480e1c PM |
838 | return 0; |
839 | } | |
90800216 HX |
840 | EXPORT_SYMBOL(nla_append); |
841 | #endif |