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