ublk: add UBLK_CMD_DEL_DEV_ASYNC
[linux-block.git] / lib / nlattr.c
CommitLineData
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>
f0950402 13#include <linux/nospec.h>
bfa83a9e
TG
14#include <linux/skbuff.h>
15#include <linux/string.h>
16#include <linux/types.h>
17#include <net/netlink.h>
18
6e237d09
DA
19/* For these data types, attribute length should be exactly the given
20 * size. However, to maintain compatibility with broken commands, if the
21 * attribute length does not match the expected size a warning is emitted
22 * to the user that the command is sending invalid data and needs to be fixed.
23 */
28033ae4 24static const u8 nla_attr_len[NLA_TYPE_MAX+1] = {
bfa83a9e
TG
25 [NLA_U8] = sizeof(u8),
26 [NLA_U16] = sizeof(u16),
27 [NLA_U32] = sizeof(u32),
28 [NLA_U64] = sizeof(u64),
9eca2eb9
JA
29 [NLA_S8] = sizeof(s8),
30 [NLA_S16] = sizeof(s16),
31 [NLA_S32] = sizeof(s32),
32 [NLA_S64] = sizeof(s64),
bfa83a9e
TG
33};
34
28033ae4 35static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
6e237d09
DA
36 [NLA_U8] = sizeof(u8),
37 [NLA_U16] = sizeof(u16),
38 [NLA_U32] = sizeof(u32),
39 [NLA_U64] = sizeof(u64),
28033ae4
DA
40 [NLA_MSECS] = sizeof(u64),
41 [NLA_NESTED] = NLA_HDRLEN,
6e237d09
DA
42 [NLA_S8] = sizeof(s8),
43 [NLA_S16] = sizeof(s16),
44 [NLA_S32] = sizeof(s32),
45 [NLA_S64] = sizeof(s64),
28033ae4
DA
46};
47
7690aa1c
JB
48/*
49 * Nested policies might refer back to the original
50 * policy in some cases, and userspace could try to
51 * abuse that and recurse by nesting in the right
52 * ways. Limit recursion to avoid this problem.
53 */
54#define MAX_POLICY_RECURSION_DEPTH 10
55
56static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
57 const struct nla_policy *policy,
58 unsigned int validate,
59 struct netlink_ext_ack *extack,
60 struct nlattr **tb, unsigned int depth);
61
64c83d83 62static int validate_nla_bitfield32(const struct nlattr *nla,
47a1494b 63 const u32 valid_flags_mask)
64c83d83
JHS
64{
65 const struct nla_bitfield32 *bf = nla_data(nla);
64c83d83 66
48fde90a 67 if (!valid_flags_mask)
64c83d83
JHS
68 return -EINVAL;
69
70 /*disallow invalid bit selector */
47a1494b 71 if (bf->selector & ~valid_flags_mask)
64c83d83
JHS
72 return -EINVAL;
73
74 /*disallow invalid bit values */
47a1494b 75 if (bf->value & ~valid_flags_mask)
64c83d83
JHS
76 return -EINVAL;
77
78 /*disallow valid bit values that are not selected*/
79 if (bf->value & ~bf->selector)
80 return -EINVAL;
81
82 return 0;
83}
84
1501d135
JB
85static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
86 const struct nla_policy *policy,
8cb08174 87 struct netlink_ext_ack *extack,
7690aa1c 88 unsigned int validate, unsigned int depth)
1501d135
JB
89{
90 const struct nlattr *entry;
91 int rem;
92
93 nla_for_each_attr(entry, head, len, rem) {
94 int ret;
95
96 if (nla_len(entry) == 0)
97 continue;
98
99 if (nla_len(entry) < NLA_HDRLEN) {
44f3625b
JB
100 NL_SET_ERR_MSG_ATTR_POL(extack, entry, policy,
101 "Array element too short");
1501d135
JB
102 return -ERANGE;
103 }
104
7690aa1c
JB
105 ret = __nla_validate_parse(nla_data(entry), nla_len(entry),
106 maxtype, policy, validate, extack,
107 NULL, depth + 1);
1501d135
JB
108 if (ret < 0)
109 return ret;
110 }
111
112 return 0;
113}
114
2c28ae48
JB
115void nla_get_range_unsigned(const struct nla_policy *pt,
116 struct netlink_range_validation *range)
3e48be05 117{
d06a09b9
JB
118 WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR &&
119 (pt->min < 0 || pt->max < 0));
120
2c28ae48
JB
121 range->min = 0;
122
123 switch (pt->type) {
124 case NLA_U8:
125 range->max = U8_MAX;
126 break;
127 case NLA_U16:
ecaf75ff 128 case NLA_BE16:
8aa26c57 129 case NLA_BINARY:
2c28ae48
JB
130 range->max = U16_MAX;
131 break;
132 case NLA_U32:
ecaf75ff 133 case NLA_BE32:
2c28ae48
JB
134 range->max = U32_MAX;
135 break;
136 case NLA_U64:
374d345d 137 case NLA_UINT:
2c28ae48
JB
138 case NLA_MSECS:
139 range->max = U64_MAX;
140 break;
141 default:
142 WARN_ON_ONCE(1);
143 return;
144 }
145
d06a09b9
JB
146 switch (pt->validation_type) {
147 case NLA_VALIDATE_RANGE:
8aa26c57 148 case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
d06a09b9
JB
149 range->min = pt->min;
150 range->max = pt->max;
151 break;
152 case NLA_VALIDATE_RANGE_PTR:
2c28ae48 153 *range = *pt->range;
d06a09b9
JB
154 break;
155 case NLA_VALIDATE_MIN:
156 range->min = pt->min;
157 break;
158 case NLA_VALIDATE_MAX:
159 range->max = pt->max;
160 break;
2c28ae48
JB
161 default:
162 break;
d06a09b9 163 }
2c28ae48
JB
164}
165
8aa26c57
JB
166static int nla_validate_range_unsigned(const struct nla_policy *pt,
167 const struct nlattr *nla,
168 struct netlink_ext_ack *extack,
169 unsigned int validate)
2c28ae48
JB
170{
171 struct netlink_range_validation range;
172 u64 value;
3e48be05
JB
173
174 switch (pt->type) {
175 case NLA_U8:
176 value = nla_get_u8(nla);
177 break;
178 case NLA_U16:
ecaf75ff
FW
179 value = nla_get_u16(nla);
180 break;
3e48be05 181 case NLA_U32:
ecaf75ff
FW
182 value = nla_get_u32(nla);
183 break;
d06a09b9 184 case NLA_U64:
ecaf75ff 185 value = nla_get_u64(nla);
08724ef6 186 break;
374d345d
JK
187 case NLA_UINT:
188 value = nla_get_uint(nla);
189 break;
da4063bd 190 case NLA_MSECS:
d06a09b9
JB
191 value = nla_get_u64(nla);
192 break;
8aa26c57
JB
193 case NLA_BINARY:
194 value = nla_len(nla);
195 break;
ecaf75ff
FW
196 case NLA_BE16:
197 value = ntohs(nla_get_be16(nla));
198 break;
199 case NLA_BE32:
200 value = ntohl(nla_get_be32(nla));
201 break;
d06a09b9
JB
202 default:
203 return -EINVAL;
204 }
205
2c28ae48
JB
206 nla_get_range_unsigned(pt, &range);
207
8aa26c57
JB
208 if (pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG &&
209 pt->type == NLA_BINARY && value > range.max) {
210 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
211 current->comm, pt->type);
212 if (validate & NL_VALIDATE_STRICT_ATTRS) {
44f3625b
JB
213 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
214 "invalid attribute length");
8aa26c57
JB
215 return -EINVAL;
216 }
217
218 /* this assumes min <= max (don't validate against min) */
219 return 0;
220 }
221
2c28ae48 222 if (value < range.min || value > range.max) {
8aa26c57
JB
223 bool binary = pt->type == NLA_BINARY;
224
225 if (binary)
44f3625b
JB
226 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
227 "binary attribute size out of range");
8aa26c57 228 else
44f3625b
JB
229 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
230 "integer out of range");
8aa26c57 231
d06a09b9
JB
232 return -ERANGE;
233 }
234
235 return 0;
236}
237
2c28ae48
JB
238void nla_get_range_signed(const struct nla_policy *pt,
239 struct netlink_range_validation_signed *range)
d06a09b9 240{
2c28ae48
JB
241 switch (pt->type) {
242 case NLA_S8:
243 range->min = S8_MIN;
244 range->max = S8_MAX;
245 break;
246 case NLA_S16:
247 range->min = S16_MIN;
248 range->max = S16_MAX;
249 break;
250 case NLA_S32:
251 range->min = S32_MIN;
252 range->max = S32_MAX;
253 break;
254 case NLA_S64:
374d345d 255 case NLA_SINT:
2c28ae48
JB
256 range->min = S64_MIN;
257 range->max = S64_MAX;
258 break;
259 default:
260 WARN_ON_ONCE(1);
261 return;
262 }
d06a09b9
JB
263
264 switch (pt->validation_type) {
265 case NLA_VALIDATE_RANGE:
266 range->min = pt->min;
267 range->max = pt->max;
268 break;
269 case NLA_VALIDATE_RANGE_PTR:
2c28ae48 270 *range = *pt->range_signed;
d06a09b9
JB
271 break;
272 case NLA_VALIDATE_MIN:
273 range->min = pt->min;
274 break;
275 case NLA_VALIDATE_MAX:
276 range->max = pt->max;
277 break;
2c28ae48
JB
278 default:
279 break;
d06a09b9 280 }
2c28ae48
JB
281}
282
283static int nla_validate_int_range_signed(const struct nla_policy *pt,
284 const struct nlattr *nla,
285 struct netlink_ext_ack *extack)
286{
287 struct netlink_range_validation_signed range;
288 s64 value;
d06a09b9
JB
289
290 switch (pt->type) {
3e48be05
JB
291 case NLA_S8:
292 value = nla_get_s8(nla);
293 break;
294 case NLA_S16:
295 value = nla_get_s16(nla);
296 break;
297 case NLA_S32:
298 value = nla_get_s32(nla);
299 break;
300 case NLA_S64:
301 value = nla_get_s64(nla);
302 break;
374d345d
JK
303 case NLA_SINT:
304 value = nla_get_sint(nla);
305 break;
3e48be05 306 default:
3e48be05
JB
307 return -EINVAL;
308 }
309
2c28ae48
JB
310 nla_get_range_signed(pt, &range);
311
312 if (value < range.min || value > range.max) {
44f3625b
JB
313 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
314 "integer out of range");
3e48be05
JB
315 return -ERANGE;
316 }
317
318 return 0;
319}
320
d06a09b9
JB
321static int nla_validate_int_range(const struct nla_policy *pt,
322 const struct nlattr *nla,
8aa26c57
JB
323 struct netlink_ext_ack *extack,
324 unsigned int validate)
d06a09b9
JB
325{
326 switch (pt->type) {
327 case NLA_U8:
328 case NLA_U16:
329 case NLA_U32:
330 case NLA_U64:
374d345d 331 case NLA_UINT:
da4063bd 332 case NLA_MSECS:
8aa26c57 333 case NLA_BINARY:
ecaf75ff
FW
334 case NLA_BE16:
335 case NLA_BE32:
8aa26c57 336 return nla_validate_range_unsigned(pt, nla, extack, validate);
d06a09b9
JB
337 case NLA_S8:
338 case NLA_S16:
339 case NLA_S32:
340 case NLA_S64:
374d345d 341 case NLA_SINT:
d06a09b9
JB
342 return nla_validate_int_range_signed(pt, nla, extack);
343 default:
344 WARN_ON(1);
345 return -EINVAL;
346 }
347}
348
bdbb4e29
JK
349static int nla_validate_mask(const struct nla_policy *pt,
350 const struct nlattr *nla,
351 struct netlink_ext_ack *extack)
352{
353 u64 value;
354
355 switch (pt->type) {
356 case NLA_U8:
357 value = nla_get_u8(nla);
358 break;
359 case NLA_U16:
360 value = nla_get_u16(nla);
361 break;
362 case NLA_U32:
363 value = nla_get_u32(nla);
364 break;
365 case NLA_U64:
366 value = nla_get_u64(nla);
367 break;
374d345d
JK
368 case NLA_UINT:
369 value = nla_get_uint(nla);
370 break;
5fac9b7c
FW
371 case NLA_BE16:
372 value = ntohs(nla_get_be16(nla));
373 break;
374 case NLA_BE32:
375 value = ntohl(nla_get_be32(nla));
376 break;
bdbb4e29
JK
377 default:
378 return -EINVAL;
379 }
380
381 if (value & ~(u64)pt->mask) {
382 NL_SET_ERR_MSG_ATTR(extack, nla, "reserved bit set");
383 return -EINVAL;
384 }
385
386 return 0;
387}
388
3654654f 389static int validate_nla(const struct nlattr *nla, int maxtype,
8cb08174 390 const struct nla_policy *policy, unsigned int validate,
7690aa1c 391 struct netlink_ext_ack *extack, unsigned int depth)
bfa83a9e 392{
56738f46 393 u16 strict_start_type = policy[0].strict_start_type;
ef7c79ed 394 const struct nla_policy *pt;
8f4c1f9b 395 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
c29f1845 396 int err = -ERANGE;
bfa83a9e 397
56738f46
JB
398 if (strict_start_type && type >= strict_start_type)
399 validate |= NL_VALIDATE_STRICT;
400
8f4c1f9b 401 if (type <= 0 || type > maxtype)
bfa83a9e
TG
402 return 0;
403
f0950402 404 type = array_index_nospec(type, maxtype + 1);
8f4c1f9b 405 pt = &policy[type];
bfa83a9e
TG
406
407 BUG_ON(pt->type > NLA_TYPE_MAX);
408
8aa26c57 409 if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) {
6e237d09
DA
410 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
411 current->comm, type);
8cb08174 412 if (validate & NL_VALIDATE_STRICT_ATTRS) {
44f3625b
JB
413 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
414 "invalid attribute length");
8cb08174
JB
415 return -EINVAL;
416 }
28033ae4
DA
417 }
418
b424e432
MK
419 if (validate & NL_VALIDATE_NESTED) {
420 if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
421 !(nla->nla_type & NLA_F_NESTED)) {
44f3625b
JB
422 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
423 "NLA_F_NESTED is missing");
b424e432
MK
424 return -EINVAL;
425 }
426 if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
427 pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
44f3625b
JB
428 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
429 "NLA_F_NESTED not expected");
b424e432
MK
430 return -EINVAL;
431 }
432 }
433
a5531a5d 434 switch (pt->type) {
568b742a 435 case NLA_REJECT:
47a1494b 436 if (extack && pt->reject_message) {
c29f1845 437 NL_SET_BAD_ATTR(extack, nla);
47a1494b 438 extack->_msg = pt->reject_message;
c29f1845
JB
439 return -EINVAL;
440 }
441 err = -EINVAL;
442 goto out_err;
568b742a 443
a5531a5d
TG
444 case NLA_FLAG:
445 if (attrlen > 0)
c29f1845 446 goto out_err;
a5531a5d 447 break;
bfa83a9e 448
374d345d
JK
449 case NLA_SINT:
450 case NLA_UINT:
451 if (attrlen != sizeof(u32) && attrlen != sizeof(u64)) {
452 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
453 "invalid attribute length");
454 return -EINVAL;
455 }
456 break;
457
64c83d83
JHS
458 case NLA_BITFIELD32:
459 if (attrlen != sizeof(struct nla_bitfield32))
c29f1845 460 goto out_err;
64c83d83 461
47a1494b 462 err = validate_nla_bitfield32(nla, pt->bitfield32_valid);
c29f1845
JB
463 if (err)
464 goto out_err;
465 break;
64c83d83 466
a5531a5d
TG
467 case NLA_NUL_STRING:
468 if (pt->len)
469 minlen = min_t(int, attrlen, pt->len + 1);
470 else
471 minlen = attrlen;
bfa83a9e 472
c29f1845
JB
473 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
474 err = -EINVAL;
475 goto out_err;
476 }
36f9ff9e 477 fallthrough;
a5531a5d
TG
478
479 case NLA_STRING:
480 if (attrlen < 1)
c29f1845 481 goto out_err;
a5531a5d
TG
482
483 if (pt->len) {
484 char *buf = nla_data(nla);
485
486 if (buf[attrlen - 1] == '\0')
487 attrlen--;
488
489 if (attrlen > pt->len)
c29f1845 490 goto out_err;
a5531a5d
TG
491 }
492 break;
493
d30045a0
JB
494 case NLA_BINARY:
495 if (pt->len && attrlen > pt->len)
c29f1845 496 goto out_err;
d30045a0
JB
497 break;
498
ea5693cc
PM
499 case NLA_NESTED:
500 /* a nested attributes is allowed to be empty; if its not,
501 * it must have a size of at least NLA_HDRLEN.
502 */
503 if (attrlen == 0)
504 break;
9a659a35
JB
505 if (attrlen < NLA_HDRLEN)
506 goto out_err;
47a1494b 507 if (pt->nested_policy) {
7690aa1c
JB
508 err = __nla_validate_parse(nla_data(nla), nla_len(nla),
509 pt->len, pt->nested_policy,
510 validate, extack, NULL,
511 depth + 1);
9a659a35
JB
512 if (err < 0) {
513 /*
514 * return directly to preserve the inner
515 * error message/attribute pointer
516 */
517 return err;
518 }
519 }
520 break;
1501d135
JB
521 case NLA_NESTED_ARRAY:
522 /* a nested array attribute is allowed to be empty; if its not,
523 * it must have a size of at least NLA_HDRLEN.
524 */
525 if (attrlen == 0)
526 break;
527 if (attrlen < NLA_HDRLEN)
528 goto out_err;
47a1494b 529 if (pt->nested_policy) {
1501d135
JB
530 int err;
531
532 err = nla_validate_array(nla_data(nla), nla_len(nla),
47a1494b 533 pt->len, pt->nested_policy,
7690aa1c 534 extack, validate, depth);
1501d135
JB
535 if (err < 0) {
536 /*
537 * return directly to preserve the inner
538 * error message/attribute pointer
539 */
540 return err;
541 }
542 }
543 break;
6f455f5f
JB
544
545 case NLA_UNSPEC:
8cb08174
JB
546 if (validate & NL_VALIDATE_UNSPEC) {
547 NL_SET_ERR_MSG_ATTR(extack, nla,
548 "Unsupported attribute");
549 return -EINVAL;
550 }
6f455f5f
JB
551 if (attrlen < pt->len)
552 goto out_err;
553 break;
554
a5531a5d
TG
555 default:
556 if (pt->len)
557 minlen = pt->len;
6f455f5f 558 else
a5531a5d
TG
559 minlen = nla_attr_minlen[pt->type];
560
561 if (attrlen < minlen)
c29f1845 562 goto out_err;
a5531a5d 563 }
bfa83a9e 564
3e48be05
JB
565 /* further validation */
566 switch (pt->validation_type) {
567 case NLA_VALIDATE_NONE:
568 /* nothing to do */
569 break;
d06a09b9 570 case NLA_VALIDATE_RANGE_PTR:
3e48be05 571 case NLA_VALIDATE_RANGE:
8aa26c57 572 case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
3e48be05
JB
573 case NLA_VALIDATE_MIN:
574 case NLA_VALIDATE_MAX:
8aa26c57 575 err = nla_validate_int_range(pt, nla, extack, validate);
3e48be05
JB
576 if (err)
577 return err;
578 break;
bdbb4e29
JK
579 case NLA_VALIDATE_MASK:
580 err = nla_validate_mask(pt, nla, extack);
581 if (err)
582 return err;
583 break;
33188bd6
JB
584 case NLA_VALIDATE_FUNCTION:
585 if (pt->validate) {
586 err = pt->validate(nla, extack);
587 if (err)
588 return err;
589 }
590 break;
3e48be05
JB
591 }
592
bfa83a9e 593 return 0;
c29f1845 594out_err:
44f3625b
JB
595 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
596 "Attribute failed policy validation");
c29f1845 597 return err;
bfa83a9e
TG
598}
599
8cb08174
JB
600static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
601 const struct nla_policy *policy,
602 unsigned int validate,
603 struct netlink_ext_ack *extack,
7690aa1c 604 struct nlattr **tb, unsigned int depth)
8cb08174
JB
605{
606 const struct nlattr *nla;
607 int rem;
608
7690aa1c
JB
609 if (depth >= MAX_POLICY_RECURSION_DEPTH) {
610 NL_SET_ERR_MSG(extack,
611 "allowed policy recursion depth exceeded");
612 return -EINVAL;
613 }
614
8cb08174
JB
615 if (tb)
616 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
617
618 nla_for_each_attr(nla, head, len, rem) {
619 u16 type = nla_type(nla);
620
621 if (type == 0 || type > maxtype) {
622 if (validate & NL_VALIDATE_MAXTYPE) {
d54a16b2
MK
623 NL_SET_ERR_MSG_ATTR(extack, nla,
624 "Unknown attribute type");
8cb08174
JB
625 return -EINVAL;
626 }
627 continue;
628 }
f0950402 629 type = array_index_nospec(type, maxtype + 1);
8cb08174
JB
630 if (policy) {
631 int err = validate_nla(nla, maxtype, policy,
7690aa1c 632 validate, extack, depth);
8cb08174
JB
633
634 if (err < 0)
635 return err;
636 }
637
638 if (tb)
639 tb[type] = (struct nlattr *)nla;
640 }
641
642 if (unlikely(rem > 0)) {
643 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
644 rem, current->comm);
645 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
646 if (validate & NL_VALIDATE_TRAILING)
647 return -EINVAL;
648 }
649
650 return 0;
651}
652
bfa83a9e 653/**
8cb08174 654 * __nla_validate - Validate a stream of attributes
bfa83a9e
TG
655 * @head: head of attribute stream
656 * @len: length of attribute stream
657 * @maxtype: maximum attribute type to be expected
658 * @policy: validation policy
8cb08174 659 * @validate: validation strictness
fceb6435 660 * @extack: extended ACK report struct
bfa83a9e
TG
661 *
662 * Validates all attributes in the specified attribute stream against the
8cb08174
JB
663 * specified policy. Validation depends on the validate flags passed, see
664 * &enum netlink_validation for more details on that.
9dbbc3b9 665 * See documentation of struct nla_policy for more details.
bfa83a9e
TG
666 *
667 * Returns 0 on success or a negative error code.
668 */
8cb08174
JB
669int __nla_validate(const struct nlattr *head, int len, int maxtype,
670 const struct nla_policy *policy, unsigned int validate,
671 struct netlink_ext_ack *extack)
bfa83a9e 672{
8cb08174 673 return __nla_validate_parse(head, len, maxtype, policy, validate,
7690aa1c 674 extack, NULL, 0);
bfa83a9e 675}
8cb08174 676EXPORT_SYMBOL(__nla_validate);
bfa83a9e 677
e487eb99 678/**
9dbbc3b9 679 * nla_policy_len - Determine the max. length of a policy
8e18be76 680 * @p: policy to use
e487eb99
HE
681 * @n: number of policies
682 *
683 * Determines the max. length of the policy. It is currently used
684 * to allocated Netlink buffers roughly the size of the actual
685 * message.
686 *
687 * Returns 0 on success or a negative error code.
688 */
689int
690nla_policy_len(const struct nla_policy *p, int n)
691{
692 int i, len = 0;
693
e3fa3aff 694 for (i = 0; i < n; i++, p++) {
e487eb99
HE
695 if (p->len)
696 len += nla_total_size(p->len);
28033ae4
DA
697 else if (nla_attr_len[p->type])
698 len += nla_total_size(nla_attr_len[p->type]);
e487eb99
HE
699 else if (nla_attr_minlen[p->type])
700 len += nla_total_size(nla_attr_minlen[p->type]);
701 }
702
703 return len;
704}
6d6a138f 705EXPORT_SYMBOL(nla_policy_len);
e487eb99 706
bfa83a9e 707/**
8cb08174 708 * __nla_parse - Parse a stream of attributes into a tb buffer
bfa83a9e
TG
709 * @tb: destination array with maxtype+1 elements
710 * @maxtype: maximum attribute type to be expected
711 * @head: head of attribute stream
712 * @len: length of attribute stream
10b595af 713 * @policy: validation policy
8cb08174
JB
714 * @validate: validation strictness
715 * @extack: extended ACK pointer
bfa83a9e
TG
716 *
717 * Parses a stream of attributes and stores a pointer to each attribute in
8cb08174
JB
718 * the tb array accessible via the attribute type.
719 * Validation is controlled by the @validate parameter.
bfa83a9e
TG
720 *
721 * Returns 0 on success or a negative error code.
722 */
8cb08174
JB
723int __nla_parse(struct nlattr **tb, int maxtype,
724 const struct nlattr *head, int len,
725 const struct nla_policy *policy, unsigned int validate,
726 struct netlink_ext_ack *extack)
a5f6cba2 727{
8cb08174 728 return __nla_validate_parse(head, len, maxtype, policy, validate,
7690aa1c 729 extack, tb, 0);
a5f6cba2 730}
8cb08174 731EXPORT_SYMBOL(__nla_parse);
a5f6cba2 732
bfa83a9e
TG
733/**
734 * nla_find - Find a specific attribute in a stream of attributes
735 * @head: head of attribute stream
736 * @len: length of attribute stream
737 * @attrtype: type of attribute to look for
738 *
739 * Returns the first attribute in the stream matching the specified type.
740 */
3654654f 741struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
bfa83a9e 742{
3654654f 743 const struct nlattr *nla;
bfa83a9e
TG
744 int rem;
745
746 nla_for_each_attr(nla, head, len, rem)
8f4c1f9b 747 if (nla_type(nla) == attrtype)
3654654f 748 return (struct nlattr *)nla;
bfa83a9e
TG
749
750 return NULL;
751}
6d6a138f 752EXPORT_SYMBOL(nla_find);
bfa83a9e
TG
753
754/**
872f6903 755 * nla_strscpy - Copy string attribute payload into a sized buffer
9ca71874
FL
756 * @dst: Where to copy the string to.
757 * @nla: Attribute to copy the string from.
758 * @dstsize: Size of destination buffer.
bfa83a9e
TG
759 *
760 * Copies at most dstsize - 1 bytes into the destination buffer.
d2627006 761 * Unlike strscpy() the destination buffer is always padded out.
bfa83a9e 762 *
9ca71874
FL
763 * Return:
764 * * srclen - Returns @nla length (not including the trailing %NUL).
765 * * -E2BIG - If @dstsize is 0 or greater than U16_MAX or @nla length greater
766 * than @dstsize.
bfa83a9e 767 */
872f6903 768ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize)
bfa83a9e
TG
769{
770 size_t srclen = nla_len(nla);
771 char *src = nla_data(nla);
9ca71874
FL
772 ssize_t ret;
773 size_t len;
774
775 if (dstsize == 0 || WARN_ON_ONCE(dstsize > U16_MAX))
776 return -E2BIG;
bfa83a9e
TG
777
778 if (srclen > 0 && src[srclen - 1] == '\0')
779 srclen--;
780
9ca71874
FL
781 if (srclen >= dstsize) {
782 len = dstsize - 1;
783 ret = -E2BIG;
784 } else {
785 len = srclen;
786 ret = len;
bfa83a9e
TG
787 }
788
9ca71874
FL
789 memcpy(dst, src, len);
790 /* Zero pad end of dst. */
791 memset(dst + len, 0, dstsize - len);
792
793 return ret;
bfa83a9e 794}
872f6903 795EXPORT_SYMBOL(nla_strscpy);
bfa83a9e 796
2cf0c8b3
PS
797/**
798 * nla_strdup - Copy string attribute payload into a newly allocated buffer
799 * @nla: attribute to copy the string from
800 * @flags: the type of memory to allocate (see kmalloc).
801 *
802 * Returns a pointer to the allocated buffer or NULL on error.
803 */
804char *nla_strdup(const struct nlattr *nla, gfp_t flags)
805{
806 size_t srclen = nla_len(nla);
807 char *src = nla_data(nla), *dst;
808
809 if (srclen > 0 && src[srclen - 1] == '\0')
810 srclen--;
811
812 dst = kmalloc(srclen + 1, flags);
813 if (dst != NULL) {
814 memcpy(dst, src, srclen);
815 dst[srclen] = '\0';
816 }
817 return dst;
818}
819EXPORT_SYMBOL(nla_strdup);
820
bfa83a9e
TG
821/**
822 * nla_memcpy - Copy a netlink attribute into another memory area
823 * @dest: where to copy to memcpy
824 * @src: netlink attribute to copy from
825 * @count: size of the destination area
826 *
827 * Note: The number of bytes copied is limited by the length of
828 * attribute's payload. memcpy
829 *
830 * Returns the number of bytes copied.
831 */
b057efd4 832int nla_memcpy(void *dest, const struct nlattr *src, int count)
bfa83a9e
TG
833{
834 int minlen = min_t(int, count, nla_len(src));
835
836 memcpy(dest, nla_data(src), minlen);
5899f047
JB
837 if (count > minlen)
838 memset(dest + minlen, 0, count - minlen);
bfa83a9e
TG
839
840 return minlen;
841}
6d6a138f 842EXPORT_SYMBOL(nla_memcpy);
bfa83a9e
TG
843
844/**
845 * nla_memcmp - Compare an attribute with sized memory area
846 * @nla: netlink attribute
847 * @data: memory area
848 * @size: size of memory area
849 */
850int nla_memcmp(const struct nlattr *nla, const void *data,
851 size_t size)
852{
853 int d = nla_len(nla) - size;
854
855 if (d == 0)
856 d = memcmp(nla_data(nla), data, size);
857
858 return d;
859}
6d6a138f 860EXPORT_SYMBOL(nla_memcmp);
bfa83a9e
TG
861
862/**
863 * nla_strcmp - Compare a string attribute against a string
864 * @nla: netlink string attribute
865 * @str: another string
866 */
867int nla_strcmp(const struct nlattr *nla, const char *str)
868{
8b7b9324
PN
869 int len = strlen(str);
870 char *buf = nla_data(nla);
871 int attrlen = nla_len(nla);
872 int d;
bfa83a9e 873
2c16db6c 874 while (attrlen > 0 && buf[attrlen - 1] == '\0')
8b7b9324
PN
875 attrlen--;
876
877 d = attrlen - len;
bfa83a9e
TG
878 if (d == 0)
879 d = memcmp(nla_data(nla), str, len);
880
881 return d;
882}
6d6a138f 883EXPORT_SYMBOL(nla_strcmp);
bfa83a9e 884
90800216 885#ifdef CONFIG_NET
bfa83a9e
TG
886/**
887 * __nla_reserve - reserve room for attribute on the skb
888 * @skb: socket buffer to reserve room on
889 * @attrtype: attribute type
890 * @attrlen: length of attribute payload
891 *
892 * Adds a netlink attribute header to a socket buffer and reserves
893 * room for the payload but does not copy it.
894 *
895 * The caller is responsible to ensure that the skb provides enough
896 * tailroom for the attribute header and payload.
897 */
898struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
899{
900 struct nlattr *nla;
901
4df864c1 902 nla = skb_put(skb, nla_total_size(attrlen));
bfa83a9e
TG
903 nla->nla_type = attrtype;
904 nla->nla_len = nla_attr_size(attrlen);
905
906 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
907
908 return nla;
909}
90800216 910EXPORT_SYMBOL(__nla_reserve);
bfa83a9e 911
089bf1a6
ND
912/**
913 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
914 * @skb: socket buffer to reserve room on
915 * @attrtype: attribute type
916 * @attrlen: length of attribute payload
11a99573 917 * @padattr: attribute type for the padding
089bf1a6
ND
918 *
919 * Adds a netlink attribute header to a socket buffer and reserves
920 * room for the payload but does not copy it. It also ensure that this
11a99573 921 * attribute will have a 64-bit aligned nla_data() area.
089bf1a6
ND
922 *
923 * The caller is responsible to ensure that the skb provides enough
924 * tailroom for the attribute header and payload.
925 */
926struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
927 int attrlen, int padattr)
928{
4718a471 929 nla_align_64bit(skb, padattr);
089bf1a6
ND
930
931 return __nla_reserve(skb, attrtype, attrlen);
932}
933EXPORT_SYMBOL(__nla_reserve_64bit);
934
fe4944e5
TG
935/**
936 * __nla_reserve_nohdr - reserve room for attribute without header
937 * @skb: socket buffer to reserve room on
938 * @attrlen: length of attribute payload
939 *
940 * Reserves room for attribute payload without a header.
941 *
942 * The caller is responsible to ensure that the skb provides enough
943 * tailroom for the payload.
944 */
945void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
946{
b952f4df 947 return skb_put_zero(skb, NLA_ALIGN(attrlen));
fe4944e5 948}
90800216 949EXPORT_SYMBOL(__nla_reserve_nohdr);
fe4944e5 950
bfa83a9e
TG
951/**
952 * nla_reserve - reserve room for attribute on the skb
953 * @skb: socket buffer to reserve room on
954 * @attrtype: attribute type
955 * @attrlen: length of attribute payload
956 *
957 * Adds a netlink attribute header to a socket buffer and reserves
958 * room for the payload but does not copy it.
959 *
960 * Returns NULL if the tailroom of the skb is insufficient to store
961 * the attribute header and payload.
962 */
963struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
964{
965 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
966 return NULL;
967
968 return __nla_reserve(skb, attrtype, attrlen);
969}
90800216 970EXPORT_SYMBOL(nla_reserve);
bfa83a9e 971
089bf1a6
ND
972/**
973 * nla_reserve_64bit - reserve room for attribute on the skb and align it
974 * @skb: socket buffer to reserve room on
975 * @attrtype: attribute type
976 * @attrlen: length of attribute payload
11a99573 977 * @padattr: attribute type for the padding
089bf1a6
ND
978 *
979 * Adds a netlink attribute header to a socket buffer and reserves
980 * room for the payload but does not copy it. It also ensure that this
11a99573 981 * attribute will have a 64-bit aligned nla_data() area.
089bf1a6
ND
982 *
983 * Returns NULL if the tailroom of the skb is insufficient to store
984 * the attribute header and payload.
985 */
986struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
987 int padattr)
988{
989 size_t len;
990
991 if (nla_need_padding_for_64bit(skb))
992 len = nla_total_size_64bit(attrlen);
993 else
994 len = nla_total_size(attrlen);
995 if (unlikely(skb_tailroom(skb) < len))
996 return NULL;
997
998 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
999}
1000EXPORT_SYMBOL(nla_reserve_64bit);
1001
fe4944e5 1002/**
10b595af 1003 * nla_reserve_nohdr - reserve room for attribute without header
fe4944e5 1004 * @skb: socket buffer to reserve room on
10b595af 1005 * @attrlen: length of attribute payload
fe4944e5
TG
1006 *
1007 * Reserves room for attribute payload without a header.
1008 *
1009 * Returns NULL if the tailroom of the skb is insufficient to store
1010 * the attribute payload.
1011 */
1012void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
1013{
1014 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
1015 return NULL;
1016
1017 return __nla_reserve_nohdr(skb, attrlen);
1018}
90800216 1019EXPORT_SYMBOL(nla_reserve_nohdr);
fe4944e5 1020
bfa83a9e
TG
1021/**
1022 * __nla_put - Add a netlink attribute to a socket buffer
1023 * @skb: socket buffer to add attribute to
1024 * @attrtype: attribute type
1025 * @attrlen: length of attribute payload
1026 * @data: head of attribute payload
1027 *
1028 * The caller is responsible to ensure that the skb provides enough
1029 * tailroom for the attribute header and payload.
1030 */
1031void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
1032 const void *data)
1033{
1034 struct nlattr *nla;
1035
1036 nla = __nla_reserve(skb, attrtype, attrlen);
1037 memcpy(nla_data(nla), data, attrlen);
1038}
90800216 1039EXPORT_SYMBOL(__nla_put);
bfa83a9e 1040
089bf1a6
ND
1041/**
1042 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1043 * @skb: socket buffer to add attribute to
1044 * @attrtype: attribute type
1045 * @attrlen: length of attribute payload
1046 * @data: head of attribute payload
11a99573 1047 * @padattr: attribute type for the padding
089bf1a6
ND
1048 *
1049 * The caller is responsible to ensure that the skb provides enough
1050 * tailroom for the attribute header and payload.
1051 */
1052void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
1053 const void *data, int padattr)
1054{
1055 struct nlattr *nla;
1056
1057 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
1058 memcpy(nla_data(nla), data, attrlen);
1059}
1060EXPORT_SYMBOL(__nla_put_64bit);
1061
fe4944e5
TG
1062/**
1063 * __nla_put_nohdr - Add a netlink attribute without header
1064 * @skb: socket buffer to add attribute to
1065 * @attrlen: length of attribute payload
1066 * @data: head of attribute payload
1067 *
1068 * The caller is responsible to ensure that the skb provides enough
1069 * tailroom for the attribute payload.
1070 */
1071void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
1072{
1073 void *start;
1074
1075 start = __nla_reserve_nohdr(skb, attrlen);
1076 memcpy(start, data, attrlen);
1077}
90800216 1078EXPORT_SYMBOL(__nla_put_nohdr);
bfa83a9e
TG
1079
1080/**
1081 * nla_put - Add a netlink attribute to a socket buffer
1082 * @skb: socket buffer to add attribute to
1083 * @attrtype: attribute type
1084 * @attrlen: length of attribute payload
1085 * @data: head of attribute payload
1086 *
bc3ed28c 1087 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
bfa83a9e
TG
1088 * the attribute header and payload.
1089 */
1090int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
1091{
1092 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
bc3ed28c 1093 return -EMSGSIZE;
bfa83a9e
TG
1094
1095 __nla_put(skb, attrtype, attrlen, data);
1096 return 0;
1097}
90800216 1098EXPORT_SYMBOL(nla_put);
bfa83a9e 1099
089bf1a6
ND
1100/**
1101 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1102 * @skb: socket buffer to add attribute to
1103 * @attrtype: attribute type
1104 * @attrlen: length of attribute payload
1105 * @data: head of attribute payload
11a99573 1106 * @padattr: attribute type for the padding
089bf1a6
ND
1107 *
1108 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1109 * the attribute header and payload.
1110 */
1111int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
1112 const void *data, int padattr)
1113{
1114 size_t len;
1115
1116 if (nla_need_padding_for_64bit(skb))
1117 len = nla_total_size_64bit(attrlen);
1118 else
1119 len = nla_total_size(attrlen);
1120 if (unlikely(skb_tailroom(skb) < len))
1121 return -EMSGSIZE;
1122
1123 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
1124 return 0;
1125}
1126EXPORT_SYMBOL(nla_put_64bit);
1127
fe4944e5
TG
1128/**
1129 * nla_put_nohdr - Add a netlink attribute without header
1130 * @skb: socket buffer to add attribute to
1131 * @attrlen: length of attribute payload
1132 * @data: head of attribute payload
1133 *
bc3ed28c 1134 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
fe4944e5
TG
1135 * the attribute payload.
1136 */
1137int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
1138{
1139 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
bc3ed28c 1140 return -EMSGSIZE;
fe4944e5
TG
1141
1142 __nla_put_nohdr(skb, attrlen, data);
1143 return 0;
1144}
90800216 1145EXPORT_SYMBOL(nla_put_nohdr);
bfa83a9e 1146
01480e1c
PM
1147/**
1148 * nla_append - Add a netlink attribute without header or padding
1149 * @skb: socket buffer to add attribute to
1150 * @attrlen: length of attribute payload
1151 * @data: head of attribute payload
1152 *
bc3ed28c 1153 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
01480e1c
PM
1154 * the attribute payload.
1155 */
1156int nla_append(struct sk_buff *skb, int attrlen, const void *data)
1157{
1158 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
bc3ed28c 1159 return -EMSGSIZE;
01480e1c 1160
59ae1d12 1161 skb_put_data(skb, data, attrlen);
01480e1c
PM
1162 return 0;
1163}
90800216
HX
1164EXPORT_SYMBOL(nla_append);
1165#endif