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