xref: /openbmc/linux/lib/nlattr.c (revision 816ffd28002651a469e86d1118a225862e392ecd)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2e9cc8bddSGeert Uytterhoeven /*
3e9cc8bddSGeert Uytterhoeven  * NETLINK      Netlink attributes
4e9cc8bddSGeert Uytterhoeven  *
5e9cc8bddSGeert Uytterhoeven  * 		Authors:	Thomas Graf <tgraf@suug.ch>
6e9cc8bddSGeert Uytterhoeven  * 				Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7e9cc8bddSGeert Uytterhoeven  */
8e9cc8bddSGeert Uytterhoeven 
98bc3bcc9SPaul Gortmaker #include <linux/export.h>
10e9cc8bddSGeert Uytterhoeven #include <linux/kernel.h>
11e9cc8bddSGeert Uytterhoeven #include <linux/errno.h>
12e9cc8bddSGeert Uytterhoeven #include <linux/jiffies.h>
13f0950402SEric Dumazet #include <linux/nospec.h>
14e9cc8bddSGeert Uytterhoeven #include <linux/skbuff.h>
15e9cc8bddSGeert Uytterhoeven #include <linux/string.h>
16e9cc8bddSGeert Uytterhoeven #include <linux/types.h>
17e9cc8bddSGeert Uytterhoeven #include <net/netlink.h>
18e9cc8bddSGeert Uytterhoeven 
196e237d09SDavid Ahern /* For these data types, attribute length should be exactly the given
206e237d09SDavid Ahern  * size. However, to maintain compatibility with broken commands, if the
216e237d09SDavid Ahern  * attribute length does not match the expected size a warning is emitted
226e237d09SDavid Ahern  * to the user that the command is sending invalid data and needs to be fixed.
236e237d09SDavid Ahern  */
2428033ae4SDavid Ahern static const u8 nla_attr_len[NLA_TYPE_MAX+1] = {
25e9cc8bddSGeert Uytterhoeven 	[NLA_U8]	= sizeof(u8),
26e9cc8bddSGeert Uytterhoeven 	[NLA_U16]	= sizeof(u16),
27e9cc8bddSGeert Uytterhoeven 	[NLA_U32]	= sizeof(u32),
28e9cc8bddSGeert Uytterhoeven 	[NLA_U64]	= sizeof(u64),
299eca2eb9SJulian Anastasov 	[NLA_S8]	= sizeof(s8),
309eca2eb9SJulian Anastasov 	[NLA_S16]	= sizeof(s16),
319eca2eb9SJulian Anastasov 	[NLA_S32]	= sizeof(s32),
329eca2eb9SJulian Anastasov 	[NLA_S64]	= sizeof(s64),
33*a2ab0281SFlorian Westphal 	[NLA_BE16]	= sizeof(__be16),
34*a2ab0281SFlorian Westphal 	[NLA_BE32]	= sizeof(__be32),
35e9cc8bddSGeert Uytterhoeven };
36e9cc8bddSGeert Uytterhoeven 
3728033ae4SDavid Ahern static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
386e237d09SDavid Ahern 	[NLA_U8]	= sizeof(u8),
396e237d09SDavid Ahern 	[NLA_U16]	= sizeof(u16),
406e237d09SDavid Ahern 	[NLA_U32]	= sizeof(u32),
416e237d09SDavid Ahern 	[NLA_U64]	= sizeof(u64),
4228033ae4SDavid Ahern 	[NLA_MSECS]	= sizeof(u64),
4328033ae4SDavid Ahern 	[NLA_NESTED]	= NLA_HDRLEN,
446e237d09SDavid Ahern 	[NLA_S8]	= sizeof(s8),
456e237d09SDavid Ahern 	[NLA_S16]	= sizeof(s16),
466e237d09SDavid Ahern 	[NLA_S32]	= sizeof(s32),
476e237d09SDavid Ahern 	[NLA_S64]	= sizeof(s64),
48*a2ab0281SFlorian Westphal 	[NLA_BE16]	= sizeof(__be16),
49*a2ab0281SFlorian Westphal 	[NLA_BE32]	= sizeof(__be32),
5028033ae4SDavid Ahern };
5128033ae4SDavid Ahern 
527690aa1cSJohannes Berg /*
537690aa1cSJohannes Berg  * Nested policies might refer back to the original
547690aa1cSJohannes Berg  * policy in some cases, and userspace could try to
557690aa1cSJohannes Berg  * abuse that and recurse by nesting in the right
567690aa1cSJohannes Berg  * ways. Limit recursion to avoid this problem.
577690aa1cSJohannes Berg  */
587690aa1cSJohannes Berg #define MAX_POLICY_RECURSION_DEPTH	10
597690aa1cSJohannes Berg 
607690aa1cSJohannes Berg static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
617690aa1cSJohannes Berg 				const struct nla_policy *policy,
627690aa1cSJohannes Berg 				unsigned int validate,
637690aa1cSJohannes Berg 				struct netlink_ext_ack *extack,
647690aa1cSJohannes Berg 				struct nlattr **tb, unsigned int depth);
657690aa1cSJohannes Berg 
validate_nla_bitfield32(const struct nlattr * nla,const u32 valid_flags_mask)6664c83d83SJamal Hadi Salim static int validate_nla_bitfield32(const struct nlattr *nla,
6747a1494bSJohannes Berg 				   const u32 valid_flags_mask)
6864c83d83SJamal Hadi Salim {
6964c83d83SJamal Hadi Salim 	const struct nla_bitfield32 *bf = nla_data(nla);
7064c83d83SJamal Hadi Salim 
7148fde90aSJohannes Berg 	if (!valid_flags_mask)
7264c83d83SJamal Hadi Salim 		return -EINVAL;
7364c83d83SJamal Hadi Salim 
7464c83d83SJamal Hadi Salim 	/*disallow invalid bit selector */
7547a1494bSJohannes Berg 	if (bf->selector & ~valid_flags_mask)
7664c83d83SJamal Hadi Salim 		return -EINVAL;
7764c83d83SJamal Hadi Salim 
7864c83d83SJamal Hadi Salim 	/*disallow invalid bit values */
7947a1494bSJohannes Berg 	if (bf->value & ~valid_flags_mask)
8064c83d83SJamal Hadi Salim 		return -EINVAL;
8164c83d83SJamal Hadi Salim 
8264c83d83SJamal Hadi Salim 	/*disallow valid bit values that are not selected*/
8364c83d83SJamal Hadi Salim 	if (bf->value & ~bf->selector)
8464c83d83SJamal Hadi Salim 		return -EINVAL;
8564c83d83SJamal Hadi Salim 
8664c83d83SJamal Hadi Salim 	return 0;
8764c83d83SJamal Hadi Salim }
8864c83d83SJamal Hadi Salim 
nla_validate_array(const struct nlattr * head,int len,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack,unsigned int validate,unsigned int depth)891501d135SJohannes Berg static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
901501d135SJohannes Berg 			      const struct nla_policy *policy,
918cb08174SJohannes Berg 			      struct netlink_ext_ack *extack,
927690aa1cSJohannes Berg 			      unsigned int validate, unsigned int depth)
931501d135SJohannes Berg {
941501d135SJohannes Berg 	const struct nlattr *entry;
951501d135SJohannes Berg 	int rem;
961501d135SJohannes Berg 
971501d135SJohannes Berg 	nla_for_each_attr(entry, head, len, rem) {
981501d135SJohannes Berg 		int ret;
991501d135SJohannes Berg 
1001501d135SJohannes Berg 		if (nla_len(entry) == 0)
1011501d135SJohannes Berg 			continue;
1021501d135SJohannes Berg 
1031501d135SJohannes Berg 		if (nla_len(entry) < NLA_HDRLEN) {
10444f3625bSJohannes Berg 			NL_SET_ERR_MSG_ATTR_POL(extack, entry, policy,
1051501d135SJohannes Berg 						"Array element too short");
1061501d135SJohannes Berg 			return -ERANGE;
1071501d135SJohannes Berg 		}
1081501d135SJohannes Berg 
1097690aa1cSJohannes Berg 		ret = __nla_validate_parse(nla_data(entry), nla_len(entry),
1107690aa1cSJohannes Berg 					   maxtype, policy, validate, extack,
1117690aa1cSJohannes Berg 					   NULL, depth + 1);
1121501d135SJohannes Berg 		if (ret < 0)
1131501d135SJohannes Berg 			return ret;
1141501d135SJohannes Berg 	}
1151501d135SJohannes Berg 
1161501d135SJohannes Berg 	return 0;
1171501d135SJohannes Berg }
1181501d135SJohannes Berg 
nla_get_range_unsigned(const struct nla_policy * pt,struct netlink_range_validation * range)1192c28ae48SJohannes Berg void nla_get_range_unsigned(const struct nla_policy *pt,
1202c28ae48SJohannes Berg 			    struct netlink_range_validation *range)
1213e48be05SJohannes Berg {
122d06a09b9SJohannes Berg 	WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR &&
123d06a09b9SJohannes Berg 		     (pt->min < 0 || pt->max < 0));
124d06a09b9SJohannes Berg 
1252c28ae48SJohannes Berg 	range->min = 0;
1262c28ae48SJohannes Berg 
1272c28ae48SJohannes Berg 	switch (pt->type) {
1282c28ae48SJohannes Berg 	case NLA_U8:
1292c28ae48SJohannes Berg 		range->max = U8_MAX;
1302c28ae48SJohannes Berg 		break;
1312c28ae48SJohannes Berg 	case NLA_U16:
132ecaf75ffSFlorian Westphal 	case NLA_BE16:
1338aa26c57SJohannes Berg 	case NLA_BINARY:
1342c28ae48SJohannes Berg 		range->max = U16_MAX;
1352c28ae48SJohannes Berg 		break;
1362c28ae48SJohannes Berg 	case NLA_U32:
137ecaf75ffSFlorian Westphal 	case NLA_BE32:
1382c28ae48SJohannes Berg 		range->max = U32_MAX;
1392c28ae48SJohannes Berg 		break;
1402c28ae48SJohannes Berg 	case NLA_U64:
1412c28ae48SJohannes Berg 	case NLA_MSECS:
1422c28ae48SJohannes Berg 		range->max = U64_MAX;
1432c28ae48SJohannes Berg 		break;
1442c28ae48SJohannes Berg 	default:
1452c28ae48SJohannes Berg 		WARN_ON_ONCE(1);
1462c28ae48SJohannes Berg 		return;
1472c28ae48SJohannes Berg 	}
1482c28ae48SJohannes Berg 
149d06a09b9SJohannes Berg 	switch (pt->validation_type) {
150d06a09b9SJohannes Berg 	case NLA_VALIDATE_RANGE:
1518aa26c57SJohannes Berg 	case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
152d06a09b9SJohannes Berg 		range->min = pt->min;
153d06a09b9SJohannes Berg 		range->max = pt->max;
154d06a09b9SJohannes Berg 		break;
155d06a09b9SJohannes Berg 	case NLA_VALIDATE_RANGE_PTR:
1562c28ae48SJohannes Berg 		*range = *pt->range;
157d06a09b9SJohannes Berg 		break;
158d06a09b9SJohannes Berg 	case NLA_VALIDATE_MIN:
159d06a09b9SJohannes Berg 		range->min = pt->min;
160d06a09b9SJohannes Berg 		break;
161d06a09b9SJohannes Berg 	case NLA_VALIDATE_MAX:
162d06a09b9SJohannes Berg 		range->max = pt->max;
163d06a09b9SJohannes Berg 		break;
1642c28ae48SJohannes Berg 	default:
1652c28ae48SJohannes Berg 		break;
166d06a09b9SJohannes Berg 	}
1672c28ae48SJohannes Berg }
1682c28ae48SJohannes Berg 
nla_validate_range_unsigned(const struct nla_policy * pt,const struct nlattr * nla,struct netlink_ext_ack * extack,unsigned int validate)1698aa26c57SJohannes Berg static int nla_validate_range_unsigned(const struct nla_policy *pt,
1702c28ae48SJohannes Berg 				       const struct nlattr *nla,
1718aa26c57SJohannes Berg 				       struct netlink_ext_ack *extack,
1728aa26c57SJohannes Berg 				       unsigned int validate)
1732c28ae48SJohannes Berg {
1742c28ae48SJohannes Berg 	struct netlink_range_validation range;
1752c28ae48SJohannes Berg 	u64 value;
1763e48be05SJohannes Berg 
1773e48be05SJohannes Berg 	switch (pt->type) {
1783e48be05SJohannes Berg 	case NLA_U8:
1793e48be05SJohannes Berg 		value = nla_get_u8(nla);
1803e48be05SJohannes Berg 		break;
1813e48be05SJohannes Berg 	case NLA_U16:
182ecaf75ffSFlorian Westphal 		value = nla_get_u16(nla);
183ecaf75ffSFlorian Westphal 		break;
1843e48be05SJohannes Berg 	case NLA_U32:
185ecaf75ffSFlorian Westphal 		value = nla_get_u32(nla);
186ecaf75ffSFlorian Westphal 		break;
187d06a09b9SJohannes Berg 	case NLA_U64:
188ecaf75ffSFlorian Westphal 		value = nla_get_u64(nla);
18908724ef6SFlorian Westphal 		break;
190da4063bdSJohannes Berg 	case NLA_MSECS:
191d06a09b9SJohannes Berg 		value = nla_get_u64(nla);
192d06a09b9SJohannes Berg 		break;
1938aa26c57SJohannes Berg 	case NLA_BINARY:
1948aa26c57SJohannes Berg 		value = nla_len(nla);
1958aa26c57SJohannes Berg 		break;
196ecaf75ffSFlorian Westphal 	case NLA_BE16:
197ecaf75ffSFlorian Westphal 		value = ntohs(nla_get_be16(nla));
198ecaf75ffSFlorian Westphal 		break;
199ecaf75ffSFlorian Westphal 	case NLA_BE32:
200ecaf75ffSFlorian Westphal 		value = ntohl(nla_get_be32(nla));
201ecaf75ffSFlorian Westphal 		break;
202d06a09b9SJohannes Berg 	default:
203d06a09b9SJohannes Berg 		return -EINVAL;
204d06a09b9SJohannes Berg 	}
205d06a09b9SJohannes Berg 
2062c28ae48SJohannes Berg 	nla_get_range_unsigned(pt, &range);
2072c28ae48SJohannes Berg 
2088aa26c57SJohannes Berg 	if (pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG &&
2098aa26c57SJohannes Berg 	    pt->type == NLA_BINARY && value > range.max) {
2108aa26c57SJohannes Berg 		pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
2118aa26c57SJohannes Berg 				    current->comm, pt->type);
2128aa26c57SJohannes Berg 		if (validate & NL_VALIDATE_STRICT_ATTRS) {
21344f3625bSJohannes Berg 			NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
2148aa26c57SJohannes Berg 						"invalid attribute length");
2158aa26c57SJohannes Berg 			return -EINVAL;
2168aa26c57SJohannes Berg 		}
2178aa26c57SJohannes Berg 
2188aa26c57SJohannes Berg 		/* this assumes min <= max (don't validate against min) */
2198aa26c57SJohannes Berg 		return 0;
2208aa26c57SJohannes Berg 	}
2218aa26c57SJohannes Berg 
2222c28ae48SJohannes Berg 	if (value < range.min || value > range.max) {
2238aa26c57SJohannes Berg 		bool binary = pt->type == NLA_BINARY;
2248aa26c57SJohannes Berg 
2258aa26c57SJohannes Berg 		if (binary)
22644f3625bSJohannes Berg 			NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
2278aa26c57SJohannes Berg 						"binary attribute size out of range");
2288aa26c57SJohannes Berg 		else
22944f3625bSJohannes Berg 			NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
230d06a09b9SJohannes Berg 						"integer out of range");
2318aa26c57SJohannes Berg 
232d06a09b9SJohannes Berg 		return -ERANGE;
233d06a09b9SJohannes Berg 	}
234d06a09b9SJohannes Berg 
235d06a09b9SJohannes Berg 	return 0;
236d06a09b9SJohannes Berg }
237d06a09b9SJohannes Berg 
nla_get_range_signed(const struct nla_policy * pt,struct netlink_range_validation_signed * range)2382c28ae48SJohannes Berg void nla_get_range_signed(const struct nla_policy *pt,
2392c28ae48SJohannes Berg 			  struct netlink_range_validation_signed *range)
240d06a09b9SJohannes Berg {
2412c28ae48SJohannes Berg 	switch (pt->type) {
2422c28ae48SJohannes Berg 	case NLA_S8:
2432c28ae48SJohannes Berg 		range->min = S8_MIN;
2442c28ae48SJohannes Berg 		range->max = S8_MAX;
2452c28ae48SJohannes Berg 		break;
2462c28ae48SJohannes Berg 	case NLA_S16:
2472c28ae48SJohannes Berg 		range->min = S16_MIN;
2482c28ae48SJohannes Berg 		range->max = S16_MAX;
2492c28ae48SJohannes Berg 		break;
2502c28ae48SJohannes Berg 	case NLA_S32:
2512c28ae48SJohannes Berg 		range->min = S32_MIN;
2522c28ae48SJohannes Berg 		range->max = S32_MAX;
2532c28ae48SJohannes Berg 		break;
2542c28ae48SJohannes Berg 	case NLA_S64:
2552c28ae48SJohannes Berg 		range->min = S64_MIN;
2562c28ae48SJohannes Berg 		range->max = S64_MAX;
2572c28ae48SJohannes Berg 		break;
2582c28ae48SJohannes Berg 	default:
2592c28ae48SJohannes Berg 		WARN_ON_ONCE(1);
2602c28ae48SJohannes Berg 		return;
2612c28ae48SJohannes Berg 	}
262d06a09b9SJohannes Berg 
263d06a09b9SJohannes Berg 	switch (pt->validation_type) {
264d06a09b9SJohannes Berg 	case NLA_VALIDATE_RANGE:
265d06a09b9SJohannes Berg 		range->min = pt->min;
266d06a09b9SJohannes Berg 		range->max = pt->max;
267d06a09b9SJohannes Berg 		break;
268d06a09b9SJohannes Berg 	case NLA_VALIDATE_RANGE_PTR:
2692c28ae48SJohannes Berg 		*range = *pt->range_signed;
270d06a09b9SJohannes Berg 		break;
271d06a09b9SJohannes Berg 	case NLA_VALIDATE_MIN:
272d06a09b9SJohannes Berg 		range->min = pt->min;
273d06a09b9SJohannes Berg 		break;
274d06a09b9SJohannes Berg 	case NLA_VALIDATE_MAX:
275d06a09b9SJohannes Berg 		range->max = pt->max;
276d06a09b9SJohannes Berg 		break;
2772c28ae48SJohannes Berg 	default:
2782c28ae48SJohannes Berg 		break;
279d06a09b9SJohannes Berg 	}
2802c28ae48SJohannes Berg }
2812c28ae48SJohannes Berg 
nla_validate_int_range_signed(const struct nla_policy * pt,const struct nlattr * nla,struct netlink_ext_ack * extack)2822c28ae48SJohannes Berg static int nla_validate_int_range_signed(const struct nla_policy *pt,
2832c28ae48SJohannes Berg 					 const struct nlattr *nla,
2842c28ae48SJohannes Berg 					 struct netlink_ext_ack *extack)
2852c28ae48SJohannes Berg {
2862c28ae48SJohannes Berg 	struct netlink_range_validation_signed range;
2872c28ae48SJohannes Berg 	s64 value;
288d06a09b9SJohannes Berg 
289d06a09b9SJohannes Berg 	switch (pt->type) {
2903e48be05SJohannes Berg 	case NLA_S8:
2913e48be05SJohannes Berg 		value = nla_get_s8(nla);
2923e48be05SJohannes Berg 		break;
2933e48be05SJohannes Berg 	case NLA_S16:
2943e48be05SJohannes Berg 		value = nla_get_s16(nla);
2953e48be05SJohannes Berg 		break;
2963e48be05SJohannes Berg 	case NLA_S32:
2973e48be05SJohannes Berg 		value = nla_get_s32(nla);
2983e48be05SJohannes Berg 		break;
2993e48be05SJohannes Berg 	case NLA_S64:
3003e48be05SJohannes Berg 		value = nla_get_s64(nla);
3013e48be05SJohannes Berg 		break;
302d06a09b9SJohannes Berg 	default:
303d06a09b9SJohannes Berg 		return -EINVAL;
304d06a09b9SJohannes Berg 	}
305d06a09b9SJohannes Berg 
3062c28ae48SJohannes Berg 	nla_get_range_signed(pt, &range);
3072c28ae48SJohannes Berg 
3082c28ae48SJohannes Berg 	if (value < range.min || value > range.max) {
30944f3625bSJohannes Berg 		NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
3103e48be05SJohannes Berg 					"integer out of range");
3113e48be05SJohannes Berg 		return -ERANGE;
3123e48be05SJohannes Berg 	}
313d06a09b9SJohannes Berg 
3143e48be05SJohannes Berg 	return 0;
315d06a09b9SJohannes Berg }
316d06a09b9SJohannes Berg 
nla_validate_int_range(const struct nla_policy * pt,const struct nlattr * nla,struct netlink_ext_ack * extack,unsigned int validate)317d06a09b9SJohannes Berg static int nla_validate_int_range(const struct nla_policy *pt,
318d06a09b9SJohannes Berg 				  const struct nlattr *nla,
3198aa26c57SJohannes Berg 				  struct netlink_ext_ack *extack,
3208aa26c57SJohannes Berg 				  unsigned int validate)
321d06a09b9SJohannes Berg {
322d06a09b9SJohannes Berg 	switch (pt->type) {
323d06a09b9SJohannes Berg 	case NLA_U8:
324d06a09b9SJohannes Berg 	case NLA_U16:
325d06a09b9SJohannes Berg 	case NLA_U32:
326d06a09b9SJohannes Berg 	case NLA_U64:
327da4063bdSJohannes Berg 	case NLA_MSECS:
3288aa26c57SJohannes Berg 	case NLA_BINARY:
329ecaf75ffSFlorian Westphal 	case NLA_BE16:
330ecaf75ffSFlorian Westphal 	case NLA_BE32:
3318aa26c57SJohannes Berg 		return nla_validate_range_unsigned(pt, nla, extack, validate);
332d06a09b9SJohannes Berg 	case NLA_S8:
333d06a09b9SJohannes Berg 	case NLA_S16:
334d06a09b9SJohannes Berg 	case NLA_S32:
335d06a09b9SJohannes Berg 	case NLA_S64:
336d06a09b9SJohannes Berg 		return nla_validate_int_range_signed(pt, nla, extack);
3373e48be05SJohannes Berg 	default:
3383e48be05SJohannes Berg 		WARN_ON(1);
3393e48be05SJohannes Berg 		return -EINVAL;
3403e48be05SJohannes Berg 	}
3413e48be05SJohannes Berg }
3423e48be05SJohannes Berg 
nla_validate_mask(const struct nla_policy * pt,const struct nlattr * nla,struct netlink_ext_ack * extack)343bdbb4e29SJakub Kicinski static int nla_validate_mask(const struct nla_policy *pt,
344bdbb4e29SJakub Kicinski 			     const struct nlattr *nla,
345bdbb4e29SJakub Kicinski 			     struct netlink_ext_ack *extack)
346bdbb4e29SJakub Kicinski {
347bdbb4e29SJakub Kicinski 	u64 value;
348bdbb4e29SJakub Kicinski 
349bdbb4e29SJakub Kicinski 	switch (pt->type) {
350bdbb4e29SJakub Kicinski 	case NLA_U8:
351bdbb4e29SJakub Kicinski 		value = nla_get_u8(nla);
352bdbb4e29SJakub Kicinski 		break;
353bdbb4e29SJakub Kicinski 	case NLA_U16:
354bdbb4e29SJakub Kicinski 		value = nla_get_u16(nla);
355bdbb4e29SJakub Kicinski 		break;
356bdbb4e29SJakub Kicinski 	case NLA_U32:
357bdbb4e29SJakub Kicinski 		value = nla_get_u32(nla);
358bdbb4e29SJakub Kicinski 		break;
359bdbb4e29SJakub Kicinski 	case NLA_U64:
360bdbb4e29SJakub Kicinski 		value = nla_get_u64(nla);
361bdbb4e29SJakub Kicinski 		break;
3625fac9b7cSFlorian Westphal 	case NLA_BE16:
3635fac9b7cSFlorian Westphal 		value = ntohs(nla_get_be16(nla));
3645fac9b7cSFlorian Westphal 		break;
3655fac9b7cSFlorian Westphal 	case NLA_BE32:
3665fac9b7cSFlorian Westphal 		value = ntohl(nla_get_be32(nla));
3675fac9b7cSFlorian Westphal 		break;
368bdbb4e29SJakub Kicinski 	default:
369bdbb4e29SJakub Kicinski 		return -EINVAL;
370bdbb4e29SJakub Kicinski 	}
371bdbb4e29SJakub Kicinski 
372bdbb4e29SJakub Kicinski 	if (value & ~(u64)pt->mask) {
373bdbb4e29SJakub Kicinski 		NL_SET_ERR_MSG_ATTR(extack, nla, "reserved bit set");
374bdbb4e29SJakub Kicinski 		return -EINVAL;
375bdbb4e29SJakub Kicinski 	}
376bdbb4e29SJakub Kicinski 
377bdbb4e29SJakub Kicinski 	return 0;
378bdbb4e29SJakub Kicinski }
379bdbb4e29SJakub Kicinski 
validate_nla(const struct nlattr * nla,int maxtype,const struct nla_policy * policy,unsigned int validate,struct netlink_ext_ack * extack,unsigned int depth)3803654654fSJan Engelhardt static int validate_nla(const struct nlattr *nla, int maxtype,
3818cb08174SJohannes Berg 			const struct nla_policy *policy, unsigned int validate,
3827690aa1cSJohannes Berg 			struct netlink_ext_ack *extack, unsigned int depth)
383e9cc8bddSGeert Uytterhoeven {
38456738f46SJohannes Berg 	u16 strict_start_type = policy[0].strict_start_type;
385e9cc8bddSGeert Uytterhoeven 	const struct nla_policy *pt;
386e9cc8bddSGeert Uytterhoeven 	int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
387c29f1845SJohannes Berg 	int err = -ERANGE;
388e9cc8bddSGeert Uytterhoeven 
38956738f46SJohannes Berg 	if (strict_start_type && type >= strict_start_type)
39056738f46SJohannes Berg 		validate |= NL_VALIDATE_STRICT;
39156738f46SJohannes Berg 
392e9cc8bddSGeert Uytterhoeven 	if (type <= 0 || type > maxtype)
393e9cc8bddSGeert Uytterhoeven 		return 0;
394e9cc8bddSGeert Uytterhoeven 
395f0950402SEric Dumazet 	type = array_index_nospec(type, maxtype + 1);
396e9cc8bddSGeert Uytterhoeven 	pt = &policy[type];
397e9cc8bddSGeert Uytterhoeven 
398e9cc8bddSGeert Uytterhoeven 	BUG_ON(pt->type > NLA_TYPE_MAX);
399e9cc8bddSGeert Uytterhoeven 
4008aa26c57SJohannes Berg 	if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) {
4016e237d09SDavid Ahern 		pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
4026e237d09SDavid Ahern 				    current->comm, type);
4038cb08174SJohannes Berg 		if (validate & NL_VALIDATE_STRICT_ATTRS) {
40444f3625bSJohannes Berg 			NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
4058cb08174SJohannes Berg 						"invalid attribute length");
4068cb08174SJohannes Berg 			return -EINVAL;
4078cb08174SJohannes Berg 		}
40828033ae4SDavid Ahern 	}
40928033ae4SDavid Ahern 
410b424e432SMichal Kubecek 	if (validate & NL_VALIDATE_NESTED) {
411b424e432SMichal Kubecek 		if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
412b424e432SMichal Kubecek 		    !(nla->nla_type & NLA_F_NESTED)) {
41344f3625bSJohannes Berg 			NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
414b424e432SMichal Kubecek 						"NLA_F_NESTED is missing");
415b424e432SMichal Kubecek 			return -EINVAL;
416b424e432SMichal Kubecek 		}
417b424e432SMichal Kubecek 		if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
418b424e432SMichal Kubecek 		    pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
41944f3625bSJohannes Berg 			NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
420b424e432SMichal Kubecek 						"NLA_F_NESTED not expected");
421b424e432SMichal Kubecek 			return -EINVAL;
422b424e432SMichal Kubecek 		}
423b424e432SMichal Kubecek 	}
424b424e432SMichal Kubecek 
425e9cc8bddSGeert Uytterhoeven 	switch (pt->type) {
426568b742aSJohannes Berg 	case NLA_REJECT:
42747a1494bSJohannes Berg 		if (extack && pt->reject_message) {
428c29f1845SJohannes Berg 			NL_SET_BAD_ATTR(extack, nla);
42947a1494bSJohannes Berg 			extack->_msg = pt->reject_message;
430568b742aSJohannes Berg 			return -EINVAL;
431c29f1845SJohannes Berg 		}
432c29f1845SJohannes Berg 		err = -EINVAL;
433c29f1845SJohannes Berg 		goto out_err;
434568b742aSJohannes Berg 
435e9cc8bddSGeert Uytterhoeven 	case NLA_FLAG:
436e9cc8bddSGeert Uytterhoeven 		if (attrlen > 0)
437c29f1845SJohannes Berg 			goto out_err;
438e9cc8bddSGeert Uytterhoeven 		break;
439e9cc8bddSGeert Uytterhoeven 
44064c83d83SJamal Hadi Salim 	case NLA_BITFIELD32:
44164c83d83SJamal Hadi Salim 		if (attrlen != sizeof(struct nla_bitfield32))
442c29f1845SJohannes Berg 			goto out_err;
44364c83d83SJamal Hadi Salim 
44447a1494bSJohannes Berg 		err = validate_nla_bitfield32(nla, pt->bitfield32_valid);
445c29f1845SJohannes Berg 		if (err)
446c29f1845SJohannes Berg 			goto out_err;
447c29f1845SJohannes Berg 		break;
44864c83d83SJamal Hadi Salim 
449e9cc8bddSGeert Uytterhoeven 	case NLA_NUL_STRING:
450e9cc8bddSGeert Uytterhoeven 		if (pt->len)
451e9cc8bddSGeert Uytterhoeven 			minlen = min_t(int, attrlen, pt->len + 1);
452e9cc8bddSGeert Uytterhoeven 		else
453e9cc8bddSGeert Uytterhoeven 			minlen = attrlen;
454e9cc8bddSGeert Uytterhoeven 
455c29f1845SJohannes Berg 		if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
456c29f1845SJohannes Berg 			err = -EINVAL;
457c29f1845SJohannes Berg 			goto out_err;
458c29f1845SJohannes Berg 		}
45936f9ff9eSGustavo A. R. Silva 		fallthrough;
460e9cc8bddSGeert Uytterhoeven 
461e9cc8bddSGeert Uytterhoeven 	case NLA_STRING:
462e9cc8bddSGeert Uytterhoeven 		if (attrlen < 1)
463c29f1845SJohannes Berg 			goto out_err;
464e9cc8bddSGeert Uytterhoeven 
465e9cc8bddSGeert Uytterhoeven 		if (pt->len) {
466e9cc8bddSGeert Uytterhoeven 			char *buf = nla_data(nla);
467e9cc8bddSGeert Uytterhoeven 
468e9cc8bddSGeert Uytterhoeven 			if (buf[attrlen - 1] == '\0')
469e9cc8bddSGeert Uytterhoeven 				attrlen--;
470e9cc8bddSGeert Uytterhoeven 
471e9cc8bddSGeert Uytterhoeven 			if (attrlen > pt->len)
472c29f1845SJohannes Berg 				goto out_err;
473e9cc8bddSGeert Uytterhoeven 		}
474e9cc8bddSGeert Uytterhoeven 		break;
475e9cc8bddSGeert Uytterhoeven 
476e9cc8bddSGeert Uytterhoeven 	case NLA_BINARY:
477e9cc8bddSGeert Uytterhoeven 		if (pt->len && attrlen > pt->len)
478c29f1845SJohannes Berg 			goto out_err;
479e9cc8bddSGeert Uytterhoeven 		break;
480e9cc8bddSGeert Uytterhoeven 
481e9cc8bddSGeert Uytterhoeven 	case NLA_NESTED:
482e9cc8bddSGeert Uytterhoeven 		/* a nested attributes is allowed to be empty; if its not,
483e9cc8bddSGeert Uytterhoeven 		 * it must have a size of at least NLA_HDRLEN.
484e9cc8bddSGeert Uytterhoeven 		 */
485e9cc8bddSGeert Uytterhoeven 		if (attrlen == 0)
486e9cc8bddSGeert Uytterhoeven 			break;
4879a659a35SJohannes Berg 		if (attrlen < NLA_HDRLEN)
4889a659a35SJohannes Berg 			goto out_err;
48947a1494bSJohannes Berg 		if (pt->nested_policy) {
4907690aa1cSJohannes Berg 			err = __nla_validate_parse(nla_data(nla), nla_len(nla),
4917690aa1cSJohannes Berg 						   pt->len, pt->nested_policy,
4927690aa1cSJohannes Berg 						   validate, extack, NULL,
4937690aa1cSJohannes Berg 						   depth + 1);
4949a659a35SJohannes Berg 			if (err < 0) {
4959a659a35SJohannes Berg 				/*
4969a659a35SJohannes Berg 				 * return directly to preserve the inner
4979a659a35SJohannes Berg 				 * error message/attribute pointer
4989a659a35SJohannes Berg 				 */
4999a659a35SJohannes Berg 				return err;
5009a659a35SJohannes Berg 			}
5019a659a35SJohannes Berg 		}
5029a659a35SJohannes Berg 		break;
5031501d135SJohannes Berg 	case NLA_NESTED_ARRAY:
5041501d135SJohannes Berg 		/* a nested array attribute is allowed to be empty; if its not,
5051501d135SJohannes Berg 		 * it must have a size of at least NLA_HDRLEN.
5061501d135SJohannes Berg 		 */
5071501d135SJohannes Berg 		if (attrlen == 0)
5081501d135SJohannes Berg 			break;
5091501d135SJohannes Berg 		if (attrlen < NLA_HDRLEN)
5101501d135SJohannes Berg 			goto out_err;
51147a1494bSJohannes Berg 		if (pt->nested_policy) {
5121501d135SJohannes Berg 			int err;
5131501d135SJohannes Berg 
5141501d135SJohannes Berg 			err = nla_validate_array(nla_data(nla), nla_len(nla),
51547a1494bSJohannes Berg 						 pt->len, pt->nested_policy,
5167690aa1cSJohannes Berg 						 extack, validate, depth);
5171501d135SJohannes Berg 			if (err < 0) {
5181501d135SJohannes Berg 				/*
5191501d135SJohannes Berg 				 * return directly to preserve the inner
5201501d135SJohannes Berg 				 * error message/attribute pointer
5211501d135SJohannes Berg 				 */
5221501d135SJohannes Berg 				return err;
5231501d135SJohannes Berg 			}
5241501d135SJohannes Berg 		}
5251501d135SJohannes Berg 		break;
5266f455f5fSJohannes Berg 
5276f455f5fSJohannes Berg 	case NLA_UNSPEC:
5288cb08174SJohannes Berg 		if (validate & NL_VALIDATE_UNSPEC) {
5298cb08174SJohannes Berg 			NL_SET_ERR_MSG_ATTR(extack, nla,
5308cb08174SJohannes Berg 					    "Unsupported attribute");
5318cb08174SJohannes Berg 			return -EINVAL;
5328cb08174SJohannes Berg 		}
5336f455f5fSJohannes Berg 		if (attrlen < pt->len)
5346f455f5fSJohannes Berg 			goto out_err;
5356f455f5fSJohannes Berg 		break;
5366f455f5fSJohannes Berg 
537e9cc8bddSGeert Uytterhoeven 	default:
538e9cc8bddSGeert Uytterhoeven 		if (pt->len)
539e9cc8bddSGeert Uytterhoeven 			minlen = pt->len;
5406f455f5fSJohannes Berg 		else
541e9cc8bddSGeert Uytterhoeven 			minlen = nla_attr_minlen[pt->type];
542e9cc8bddSGeert Uytterhoeven 
543e9cc8bddSGeert Uytterhoeven 		if (attrlen < minlen)
544c29f1845SJohannes Berg 			goto out_err;
545e9cc8bddSGeert Uytterhoeven 	}
546e9cc8bddSGeert Uytterhoeven 
5473e48be05SJohannes Berg 	/* further validation */
5483e48be05SJohannes Berg 	switch (pt->validation_type) {
5493e48be05SJohannes Berg 	case NLA_VALIDATE_NONE:
5503e48be05SJohannes Berg 		/* nothing to do */
5513e48be05SJohannes Berg 		break;
552d06a09b9SJohannes Berg 	case NLA_VALIDATE_RANGE_PTR:
5533e48be05SJohannes Berg 	case NLA_VALIDATE_RANGE:
5548aa26c57SJohannes Berg 	case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
5553e48be05SJohannes Berg 	case NLA_VALIDATE_MIN:
5563e48be05SJohannes Berg 	case NLA_VALIDATE_MAX:
5578aa26c57SJohannes Berg 		err = nla_validate_int_range(pt, nla, extack, validate);
5583e48be05SJohannes Berg 		if (err)
5593e48be05SJohannes Berg 			return err;
5603e48be05SJohannes Berg 		break;
561bdbb4e29SJakub Kicinski 	case NLA_VALIDATE_MASK:
562bdbb4e29SJakub Kicinski 		err = nla_validate_mask(pt, nla, extack);
563bdbb4e29SJakub Kicinski 		if (err)
564bdbb4e29SJakub Kicinski 			return err;
565bdbb4e29SJakub Kicinski 		break;
56633188bd6SJohannes Berg 	case NLA_VALIDATE_FUNCTION:
56733188bd6SJohannes Berg 		if (pt->validate) {
56833188bd6SJohannes Berg 			err = pt->validate(nla, extack);
56933188bd6SJohannes Berg 			if (err)
57033188bd6SJohannes Berg 				return err;
57133188bd6SJohannes Berg 		}
57233188bd6SJohannes Berg 		break;
5733e48be05SJohannes Berg 	}
5743e48be05SJohannes Berg 
575e9cc8bddSGeert Uytterhoeven 	return 0;
576c29f1845SJohannes Berg out_err:
57744f3625bSJohannes Berg 	NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
57844f3625bSJohannes Berg 				"Attribute failed policy validation");
579c29f1845SJohannes Berg 	return err;
580e9cc8bddSGeert Uytterhoeven }
581e9cc8bddSGeert Uytterhoeven 
__nla_validate_parse(const struct nlattr * head,int len,int maxtype,const struct nla_policy * policy,unsigned int validate,struct netlink_ext_ack * extack,struct nlattr ** tb,unsigned int depth)5828cb08174SJohannes Berg static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
583fceb6435SJohannes Berg 				const struct nla_policy *policy,
5848cb08174SJohannes Berg 				unsigned int validate,
5858cb08174SJohannes Berg 				struct netlink_ext_ack *extack,
5867690aa1cSJohannes Berg 				struct nlattr **tb, unsigned int depth)
587e9cc8bddSGeert Uytterhoeven {
5883654654fSJan Engelhardt 	const struct nlattr *nla;
589fceb6435SJohannes Berg 	int rem;
590e9cc8bddSGeert Uytterhoeven 
5917690aa1cSJohannes Berg 	if (depth >= MAX_POLICY_RECURSION_DEPTH) {
5927690aa1cSJohannes Berg 		NL_SET_ERR_MSG(extack,
5937690aa1cSJohannes Berg 			       "allowed policy recursion depth exceeded");
5947690aa1cSJohannes Berg 		return -EINVAL;
5957690aa1cSJohannes Berg 	}
5967690aa1cSJohannes Berg 
5978cb08174SJohannes Berg 	if (tb)
5988cb08174SJohannes Berg 		memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
5998cb08174SJohannes Berg 
600e9cc8bddSGeert Uytterhoeven 	nla_for_each_attr(nla, head, len, rem) {
6018cb08174SJohannes Berg 		u16 type = nla_type(nla);
6028cb08174SJohannes Berg 
6038cb08174SJohannes Berg 		if (type == 0 || type > maxtype) {
6048cb08174SJohannes Berg 			if (validate & NL_VALIDATE_MAXTYPE) {
605d54a16b2SMichal Kubecek 				NL_SET_ERR_MSG_ATTR(extack, nla,
606d54a16b2SMichal Kubecek 						    "Unknown attribute type");
6078cb08174SJohannes Berg 				return -EINVAL;
6088cb08174SJohannes Berg 			}
6098cb08174SJohannes Berg 			continue;
6108cb08174SJohannes Berg 		}
611f0950402SEric Dumazet 		type = array_index_nospec(type, maxtype + 1);
6128cb08174SJohannes Berg 		if (policy) {
6138cb08174SJohannes Berg 			int err = validate_nla(nla, maxtype, policy,
6147690aa1cSJohannes Berg 					       validate, extack, depth);
615fceb6435SJohannes Berg 
616c29f1845SJohannes Berg 			if (err < 0)
617fceb6435SJohannes Berg 				return err;
618fceb6435SJohannes Berg 		}
619e9cc8bddSGeert Uytterhoeven 
6208cb08174SJohannes Berg 		if (tb)
6218cb08174SJohannes Berg 			tb[type] = (struct nlattr *)nla;
6228cb08174SJohannes Berg 	}
6238cb08174SJohannes Berg 
6248cb08174SJohannes Berg 	if (unlikely(rem > 0)) {
6258cb08174SJohannes Berg 		pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
6268cb08174SJohannes Berg 				    rem, current->comm);
6278cb08174SJohannes Berg 		NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
6288cb08174SJohannes Berg 		if (validate & NL_VALIDATE_TRAILING)
6298cb08174SJohannes Berg 			return -EINVAL;
6308cb08174SJohannes Berg 	}
6318cb08174SJohannes Berg 
632fceb6435SJohannes Berg 	return 0;
633e9cc8bddSGeert Uytterhoeven }
6348cb08174SJohannes Berg 
6358cb08174SJohannes Berg /**
6368cb08174SJohannes Berg  * __nla_validate - Validate a stream of attributes
6378cb08174SJohannes Berg  * @head: head of attribute stream
6388cb08174SJohannes Berg  * @len: length of attribute stream
6398cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
6408cb08174SJohannes Berg  * @policy: validation policy
6418cb08174SJohannes Berg  * @validate: validation strictness
6428cb08174SJohannes Berg  * @extack: extended ACK report struct
6438cb08174SJohannes Berg  *
6448cb08174SJohannes Berg  * Validates all attributes in the specified attribute stream against the
6458cb08174SJohannes Berg  * specified policy. Validation depends on the validate flags passed, see
6468cb08174SJohannes Berg  * &enum netlink_validation for more details on that.
6479dbbc3b9SZhen Lei  * See documentation of struct nla_policy for more details.
6488cb08174SJohannes Berg  *
6498cb08174SJohannes Berg  * Returns 0 on success or a negative error code.
6508cb08174SJohannes Berg  */
__nla_validate(const struct nlattr * head,int len,int maxtype,const struct nla_policy * policy,unsigned int validate,struct netlink_ext_ack * extack)6518cb08174SJohannes Berg int __nla_validate(const struct nlattr *head, int len, int maxtype,
6528cb08174SJohannes Berg 		   const struct nla_policy *policy, unsigned int validate,
6538cb08174SJohannes Berg 		   struct netlink_ext_ack *extack)
6548cb08174SJohannes Berg {
6558cb08174SJohannes Berg 	return __nla_validate_parse(head, len, maxtype, policy, validate,
6567690aa1cSJohannes Berg 				    extack, NULL, 0);
6578cb08174SJohannes Berg }
6588cb08174SJohannes Berg EXPORT_SYMBOL(__nla_validate);
659e9cc8bddSGeert Uytterhoeven 
660e9cc8bddSGeert Uytterhoeven /**
6619dbbc3b9SZhen Lei  * nla_policy_len - Determine the max. length of a policy
6628e18be76SYang Li  * @p: policy to use
66301e6de64SDavid S. Miller  * @n: number of policies
66401e6de64SDavid S. Miller  *
66501e6de64SDavid S. Miller  * Determines the max. length of the policy.  It is currently used
66601e6de64SDavid S. Miller  * to allocated Netlink buffers roughly the size of the actual
66701e6de64SDavid S. Miller  * message.
66801e6de64SDavid S. Miller  *
66901e6de64SDavid S. Miller  * Returns 0 on success or a negative error code.
67001e6de64SDavid S. Miller  */
67101e6de64SDavid S. Miller int
nla_policy_len(const struct nla_policy * p,int n)67201e6de64SDavid S. Miller nla_policy_len(const struct nla_policy *p, int n)
67301e6de64SDavid S. Miller {
67401e6de64SDavid S. Miller 	int i, len = 0;
67501e6de64SDavid S. Miller 
676e3fa3affSLars Ellenberg 	for (i = 0; i < n; i++, p++) {
67701e6de64SDavid S. Miller 		if (p->len)
67801e6de64SDavid S. Miller 			len += nla_total_size(p->len);
67928033ae4SDavid Ahern 		else if (nla_attr_len[p->type])
68028033ae4SDavid Ahern 			len += nla_total_size(nla_attr_len[p->type]);
68101e6de64SDavid S. Miller 		else if (nla_attr_minlen[p->type])
68201e6de64SDavid S. Miller 			len += nla_total_size(nla_attr_minlen[p->type]);
68301e6de64SDavid S. Miller 	}
68401e6de64SDavid S. Miller 
68501e6de64SDavid S. Miller 	return len;
68601e6de64SDavid S. Miller }
6876d6a138fSFabian Frederick EXPORT_SYMBOL(nla_policy_len);
68801e6de64SDavid S. Miller 
68901e6de64SDavid S. Miller /**
6908cb08174SJohannes Berg  * __nla_parse - Parse a stream of attributes into a tb buffer
691e9cc8bddSGeert Uytterhoeven  * @tb: destination array with maxtype+1 elements
692e9cc8bddSGeert Uytterhoeven  * @maxtype: maximum attribute type to be expected
693e9cc8bddSGeert Uytterhoeven  * @head: head of attribute stream
694e9cc8bddSGeert Uytterhoeven  * @len: length of attribute stream
695e9cc8bddSGeert Uytterhoeven  * @policy: validation policy
6968cb08174SJohannes Berg  * @validate: validation strictness
6978cb08174SJohannes Berg  * @extack: extended ACK pointer
698e9cc8bddSGeert Uytterhoeven  *
699e9cc8bddSGeert Uytterhoeven  * Parses a stream of attributes and stores a pointer to each attribute in
7008cb08174SJohannes Berg  * the tb array accessible via the attribute type.
7018cb08174SJohannes Berg  * Validation is controlled by the @validate parameter.
702e9cc8bddSGeert Uytterhoeven  *
703e9cc8bddSGeert Uytterhoeven  * Returns 0 on success or a negative error code.
704e9cc8bddSGeert Uytterhoeven  */
__nla_parse(struct nlattr ** tb,int maxtype,const struct nlattr * head,int len,const struct nla_policy * policy,unsigned int validate,struct netlink_ext_ack * extack)7058cb08174SJohannes Berg int __nla_parse(struct nlattr **tb, int maxtype,
706a5f6cba2SDavid Ahern 		const struct nlattr *head, int len,
7078cb08174SJohannes Berg 		const struct nla_policy *policy, unsigned int validate,
708fceb6435SJohannes Berg 		struct netlink_ext_ack *extack)
709e9cc8bddSGeert Uytterhoeven {
7108cb08174SJohannes Berg 	return __nla_validate_parse(head, len, maxtype, policy, validate,
7117690aa1cSJohannes Berg 				    extack, tb, 0);
712a5f6cba2SDavid Ahern }
7138cb08174SJohannes Berg EXPORT_SYMBOL(__nla_parse);
714a5f6cba2SDavid Ahern 
715e9cc8bddSGeert Uytterhoeven /**
716e9cc8bddSGeert Uytterhoeven  * nla_find - Find a specific attribute in a stream of attributes
717e9cc8bddSGeert Uytterhoeven  * @head: head of attribute stream
718e9cc8bddSGeert Uytterhoeven  * @len: length of attribute stream
719e9cc8bddSGeert Uytterhoeven  * @attrtype: type of attribute to look for
720e9cc8bddSGeert Uytterhoeven  *
721e9cc8bddSGeert Uytterhoeven  * Returns the first attribute in the stream matching the specified type.
722e9cc8bddSGeert Uytterhoeven  */
nla_find(const struct nlattr * head,int len,int attrtype)7233654654fSJan Engelhardt struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
724e9cc8bddSGeert Uytterhoeven {
7253654654fSJan Engelhardt 	const struct nlattr *nla;
726e9cc8bddSGeert Uytterhoeven 	int rem;
727e9cc8bddSGeert Uytterhoeven 
728e9cc8bddSGeert Uytterhoeven 	nla_for_each_attr(nla, head, len, rem)
729e9cc8bddSGeert Uytterhoeven 		if (nla_type(nla) == attrtype)
7303654654fSJan Engelhardt 			return (struct nlattr *)nla;
731e9cc8bddSGeert Uytterhoeven 
732e9cc8bddSGeert Uytterhoeven 	return NULL;
733e9cc8bddSGeert Uytterhoeven }
7346d6a138fSFabian Frederick EXPORT_SYMBOL(nla_find);
735e9cc8bddSGeert Uytterhoeven 
736e9cc8bddSGeert Uytterhoeven /**
737872f6903SFrancis Laniel  * nla_strscpy - Copy string attribute payload into a sized buffer
7389ca71874SFrancis Laniel  * @dst: Where to copy the string to.
7399ca71874SFrancis Laniel  * @nla: Attribute to copy the string from.
7409ca71874SFrancis Laniel  * @dstsize: Size of destination buffer.
741e9cc8bddSGeert Uytterhoeven  *
742e9cc8bddSGeert Uytterhoeven  * Copies at most dstsize - 1 bytes into the destination buffer.
7439ca71874SFrancis Laniel  * Unlike strlcpy the destination buffer is always padded out.
744e9cc8bddSGeert Uytterhoeven  *
7459ca71874SFrancis Laniel  * Return:
7469ca71874SFrancis Laniel  * * srclen - Returns @nla length (not including the trailing %NUL).
7479ca71874SFrancis Laniel  * * -E2BIG - If @dstsize is 0 or greater than U16_MAX or @nla length greater
7489ca71874SFrancis Laniel  *            than @dstsize.
749e9cc8bddSGeert Uytterhoeven  */
nla_strscpy(char * dst,const struct nlattr * nla,size_t dstsize)750872f6903SFrancis Laniel ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize)
751e9cc8bddSGeert Uytterhoeven {
752e9cc8bddSGeert Uytterhoeven 	size_t srclen = nla_len(nla);
753e9cc8bddSGeert Uytterhoeven 	char *src = nla_data(nla);
7549ca71874SFrancis Laniel 	ssize_t ret;
7559ca71874SFrancis Laniel 	size_t len;
7569ca71874SFrancis Laniel 
7579ca71874SFrancis Laniel 	if (dstsize == 0 || WARN_ON_ONCE(dstsize > U16_MAX))
7589ca71874SFrancis Laniel 		return -E2BIG;
759e9cc8bddSGeert Uytterhoeven 
760e9cc8bddSGeert Uytterhoeven 	if (srclen > 0 && src[srclen - 1] == '\0')
761e9cc8bddSGeert Uytterhoeven 		srclen--;
762e9cc8bddSGeert Uytterhoeven 
7639ca71874SFrancis Laniel 	if (srclen >= dstsize) {
7649ca71874SFrancis Laniel 		len = dstsize - 1;
7659ca71874SFrancis Laniel 		ret = -E2BIG;
7669ca71874SFrancis Laniel 	} else {
7679ca71874SFrancis Laniel 		len = srclen;
7689ca71874SFrancis Laniel 		ret = len;
7699ca71874SFrancis Laniel 	}
770e9cc8bddSGeert Uytterhoeven 
771e9cc8bddSGeert Uytterhoeven 	memcpy(dst, src, len);
7728eeb99bcSFrancis Laniel 	/* Zero pad end of dst. */
7738eeb99bcSFrancis Laniel 	memset(dst + len, 0, dstsize - len);
774e9cc8bddSGeert Uytterhoeven 
7759ca71874SFrancis Laniel 	return ret;
776e9cc8bddSGeert Uytterhoeven }
777872f6903SFrancis Laniel EXPORT_SYMBOL(nla_strscpy);
778e9cc8bddSGeert Uytterhoeven 
779e9cc8bddSGeert Uytterhoeven /**
7802cf0c8b3SPhil Sutter  * nla_strdup - Copy string attribute payload into a newly allocated buffer
7812cf0c8b3SPhil Sutter  * @nla: attribute to copy the string from
7822cf0c8b3SPhil Sutter  * @flags: the type of memory to allocate (see kmalloc).
7832cf0c8b3SPhil Sutter  *
7842cf0c8b3SPhil Sutter  * Returns a pointer to the allocated buffer or NULL on error.
7852cf0c8b3SPhil Sutter  */
nla_strdup(const struct nlattr * nla,gfp_t flags)7862cf0c8b3SPhil Sutter char *nla_strdup(const struct nlattr *nla, gfp_t flags)
7872cf0c8b3SPhil Sutter {
7882cf0c8b3SPhil Sutter 	size_t srclen = nla_len(nla);
7892cf0c8b3SPhil Sutter 	char *src = nla_data(nla), *dst;
7902cf0c8b3SPhil Sutter 
7912cf0c8b3SPhil Sutter 	if (srclen > 0 && src[srclen - 1] == '\0')
7922cf0c8b3SPhil Sutter 		srclen--;
7932cf0c8b3SPhil Sutter 
7942cf0c8b3SPhil Sutter 	dst = kmalloc(srclen + 1, flags);
7952cf0c8b3SPhil Sutter 	if (dst != NULL) {
7962cf0c8b3SPhil Sutter 		memcpy(dst, src, srclen);
7972cf0c8b3SPhil Sutter 		dst[srclen] = '\0';
7982cf0c8b3SPhil Sutter 	}
7992cf0c8b3SPhil Sutter 	return dst;
8002cf0c8b3SPhil Sutter }
8012cf0c8b3SPhil Sutter EXPORT_SYMBOL(nla_strdup);
8022cf0c8b3SPhil Sutter 
8032cf0c8b3SPhil Sutter /**
804e9cc8bddSGeert Uytterhoeven  * nla_memcpy - Copy a netlink attribute into another memory area
805e9cc8bddSGeert Uytterhoeven  * @dest: where to copy to memcpy
806e9cc8bddSGeert Uytterhoeven  * @src: netlink attribute to copy from
807e9cc8bddSGeert Uytterhoeven  * @count: size of the destination area
808e9cc8bddSGeert Uytterhoeven  *
809e9cc8bddSGeert Uytterhoeven  * Note: The number of bytes copied is limited by the length of
810e9cc8bddSGeert Uytterhoeven  *       attribute's payload. memcpy
811e9cc8bddSGeert Uytterhoeven  *
812e9cc8bddSGeert Uytterhoeven  * Returns the number of bytes copied.
813e9cc8bddSGeert Uytterhoeven  */
nla_memcpy(void * dest,const struct nlattr * src,int count)814e9cc8bddSGeert Uytterhoeven int nla_memcpy(void *dest, const struct nlattr *src, int count)
815e9cc8bddSGeert Uytterhoeven {
816e9cc8bddSGeert Uytterhoeven 	int minlen = min_t(int, count, nla_len(src));
817e9cc8bddSGeert Uytterhoeven 
818e9cc8bddSGeert Uytterhoeven 	memcpy(dest, nla_data(src), minlen);
8195899f047SJiri Benc 	if (count > minlen)
8205899f047SJiri Benc 		memset(dest + minlen, 0, count - minlen);
821e9cc8bddSGeert Uytterhoeven 
822e9cc8bddSGeert Uytterhoeven 	return minlen;
823e9cc8bddSGeert Uytterhoeven }
8246d6a138fSFabian Frederick EXPORT_SYMBOL(nla_memcpy);
825e9cc8bddSGeert Uytterhoeven 
826e9cc8bddSGeert Uytterhoeven /**
827e9cc8bddSGeert Uytterhoeven  * nla_memcmp - Compare an attribute with sized memory area
828e9cc8bddSGeert Uytterhoeven  * @nla: netlink attribute
829e9cc8bddSGeert Uytterhoeven  * @data: memory area
830e9cc8bddSGeert Uytterhoeven  * @size: size of memory area
831e9cc8bddSGeert Uytterhoeven  */
nla_memcmp(const struct nlattr * nla,const void * data,size_t size)832e9cc8bddSGeert Uytterhoeven int nla_memcmp(const struct nlattr *nla, const void *data,
833e9cc8bddSGeert Uytterhoeven 			     size_t size)
834e9cc8bddSGeert Uytterhoeven {
835e9cc8bddSGeert Uytterhoeven 	int d = nla_len(nla) - size;
836e9cc8bddSGeert Uytterhoeven 
837e9cc8bddSGeert Uytterhoeven 	if (d == 0)
838e9cc8bddSGeert Uytterhoeven 		d = memcmp(nla_data(nla), data, size);
839e9cc8bddSGeert Uytterhoeven 
840e9cc8bddSGeert Uytterhoeven 	return d;
841e9cc8bddSGeert Uytterhoeven }
8426d6a138fSFabian Frederick EXPORT_SYMBOL(nla_memcmp);
843e9cc8bddSGeert Uytterhoeven 
844e9cc8bddSGeert Uytterhoeven /**
845e9cc8bddSGeert Uytterhoeven  * nla_strcmp - Compare a string attribute against a string
846e9cc8bddSGeert Uytterhoeven  * @nla: netlink string attribute
847e9cc8bddSGeert Uytterhoeven  * @str: another string
848e9cc8bddSGeert Uytterhoeven  */
nla_strcmp(const struct nlattr * nla,const char * str)849e9cc8bddSGeert Uytterhoeven int nla_strcmp(const struct nlattr *nla, const char *str)
850e9cc8bddSGeert Uytterhoeven {
8518b7b9324SPablo Neira 	int len = strlen(str);
8528b7b9324SPablo Neira 	char *buf = nla_data(nla);
8538b7b9324SPablo Neira 	int attrlen = nla_len(nla);
8548b7b9324SPablo Neira 	int d;
855e9cc8bddSGeert Uytterhoeven 
8562c16db6cSMaciej Żenczykowski 	while (attrlen > 0 && buf[attrlen - 1] == '\0')
8578b7b9324SPablo Neira 		attrlen--;
8588b7b9324SPablo Neira 
8598b7b9324SPablo Neira 	d = attrlen - len;
860e9cc8bddSGeert Uytterhoeven 	if (d == 0)
861e9cc8bddSGeert Uytterhoeven 		d = memcmp(nla_data(nla), str, len);
862e9cc8bddSGeert Uytterhoeven 
863e9cc8bddSGeert Uytterhoeven 	return d;
864e9cc8bddSGeert Uytterhoeven }
8656d6a138fSFabian Frederick EXPORT_SYMBOL(nla_strcmp);
866e9cc8bddSGeert Uytterhoeven 
86790800216SHerbert Xu #ifdef CONFIG_NET
868e9cc8bddSGeert Uytterhoeven /**
869e9cc8bddSGeert Uytterhoeven  * __nla_reserve - reserve room for attribute on the skb
870e9cc8bddSGeert Uytterhoeven  * @skb: socket buffer to reserve room on
871e9cc8bddSGeert Uytterhoeven  * @attrtype: attribute type
872e9cc8bddSGeert Uytterhoeven  * @attrlen: length of attribute payload
873e9cc8bddSGeert Uytterhoeven  *
874e9cc8bddSGeert Uytterhoeven  * Adds a netlink attribute header to a socket buffer and reserves
875e9cc8bddSGeert Uytterhoeven  * room for the payload but does not copy it.
876e9cc8bddSGeert Uytterhoeven  *
877e9cc8bddSGeert Uytterhoeven  * The caller is responsible to ensure that the skb provides enough
878e9cc8bddSGeert Uytterhoeven  * tailroom for the attribute header and payload.
879e9cc8bddSGeert Uytterhoeven  */
__nla_reserve(struct sk_buff * skb,int attrtype,int attrlen)880e9cc8bddSGeert Uytterhoeven struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
881e9cc8bddSGeert Uytterhoeven {
882e9cc8bddSGeert Uytterhoeven 	struct nlattr *nla;
883e9cc8bddSGeert Uytterhoeven 
8844df864c1SJohannes Berg 	nla = skb_put(skb, nla_total_size(attrlen));
885e9cc8bddSGeert Uytterhoeven 	nla->nla_type = attrtype;
886e9cc8bddSGeert Uytterhoeven 	nla->nla_len = nla_attr_size(attrlen);
887e9cc8bddSGeert Uytterhoeven 
888e9cc8bddSGeert Uytterhoeven 	memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
889e9cc8bddSGeert Uytterhoeven 
890e9cc8bddSGeert Uytterhoeven 	return nla;
891e9cc8bddSGeert Uytterhoeven }
89290800216SHerbert Xu EXPORT_SYMBOL(__nla_reserve);
893e9cc8bddSGeert Uytterhoeven 
894e9cc8bddSGeert Uytterhoeven /**
895089bf1a6SNicolas Dichtel  * __nla_reserve_64bit - reserve room for attribute on the skb and align it
896089bf1a6SNicolas Dichtel  * @skb: socket buffer to reserve room on
897089bf1a6SNicolas Dichtel  * @attrtype: attribute type
898089bf1a6SNicolas Dichtel  * @attrlen: length of attribute payload
89911a99573SNicolas Dichtel  * @padattr: attribute type for the padding
900089bf1a6SNicolas Dichtel  *
901089bf1a6SNicolas Dichtel  * Adds a netlink attribute header to a socket buffer and reserves
902089bf1a6SNicolas Dichtel  * room for the payload but does not copy it. It also ensure that this
90311a99573SNicolas Dichtel  * attribute will have a 64-bit aligned nla_data() area.
904089bf1a6SNicolas Dichtel  *
905089bf1a6SNicolas Dichtel  * The caller is responsible to ensure that the skb provides enough
906089bf1a6SNicolas Dichtel  * tailroom for the attribute header and payload.
907089bf1a6SNicolas Dichtel  */
__nla_reserve_64bit(struct sk_buff * skb,int attrtype,int attrlen,int padattr)908089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
909089bf1a6SNicolas Dichtel 				   int attrlen, int padattr)
910089bf1a6SNicolas Dichtel {
911089bf1a6SNicolas Dichtel 	nla_align_64bit(skb, padattr);
912089bf1a6SNicolas Dichtel 
913089bf1a6SNicolas Dichtel 	return __nla_reserve(skb, attrtype, attrlen);
914089bf1a6SNicolas Dichtel }
915089bf1a6SNicolas Dichtel EXPORT_SYMBOL(__nla_reserve_64bit);
916089bf1a6SNicolas Dichtel 
917089bf1a6SNicolas Dichtel /**
918e9cc8bddSGeert Uytterhoeven  * __nla_reserve_nohdr - reserve room for attribute without header
919e9cc8bddSGeert Uytterhoeven  * @skb: socket buffer to reserve room on
920e9cc8bddSGeert Uytterhoeven  * @attrlen: length of attribute payload
921e9cc8bddSGeert Uytterhoeven  *
922e9cc8bddSGeert Uytterhoeven  * Reserves room for attribute payload without a header.
923e9cc8bddSGeert Uytterhoeven  *
924e9cc8bddSGeert Uytterhoeven  * The caller is responsible to ensure that the skb provides enough
925e9cc8bddSGeert Uytterhoeven  * tailroom for the payload.
926e9cc8bddSGeert Uytterhoeven  */
__nla_reserve_nohdr(struct sk_buff * skb,int attrlen)927e9cc8bddSGeert Uytterhoeven void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
928e9cc8bddSGeert Uytterhoeven {
929b952f4dfSyuan linyu 	return skb_put_zero(skb, NLA_ALIGN(attrlen));
930e9cc8bddSGeert Uytterhoeven }
93190800216SHerbert Xu EXPORT_SYMBOL(__nla_reserve_nohdr);
932e9cc8bddSGeert Uytterhoeven 
933e9cc8bddSGeert Uytterhoeven /**
934e9cc8bddSGeert Uytterhoeven  * nla_reserve - reserve room for attribute on the skb
935e9cc8bddSGeert Uytterhoeven  * @skb: socket buffer to reserve room on
936e9cc8bddSGeert Uytterhoeven  * @attrtype: attribute type
937e9cc8bddSGeert Uytterhoeven  * @attrlen: length of attribute payload
938e9cc8bddSGeert Uytterhoeven  *
939e9cc8bddSGeert Uytterhoeven  * Adds a netlink attribute header to a socket buffer and reserves
940e9cc8bddSGeert Uytterhoeven  * room for the payload but does not copy it.
941e9cc8bddSGeert Uytterhoeven  *
942e9cc8bddSGeert Uytterhoeven  * Returns NULL if the tailroom of the skb is insufficient to store
943e9cc8bddSGeert Uytterhoeven  * the attribute header and payload.
944e9cc8bddSGeert Uytterhoeven  */
nla_reserve(struct sk_buff * skb,int attrtype,int attrlen)945e9cc8bddSGeert Uytterhoeven struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
946e9cc8bddSGeert Uytterhoeven {
947e9cc8bddSGeert Uytterhoeven 	if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
948e9cc8bddSGeert Uytterhoeven 		return NULL;
949e9cc8bddSGeert Uytterhoeven 
950e9cc8bddSGeert Uytterhoeven 	return __nla_reserve(skb, attrtype, attrlen);
951e9cc8bddSGeert Uytterhoeven }
95290800216SHerbert Xu EXPORT_SYMBOL(nla_reserve);
953e9cc8bddSGeert Uytterhoeven 
954e9cc8bddSGeert Uytterhoeven /**
955089bf1a6SNicolas Dichtel  * nla_reserve_64bit - reserve room for attribute on the skb and align it
956089bf1a6SNicolas Dichtel  * @skb: socket buffer to reserve room on
957089bf1a6SNicolas Dichtel  * @attrtype: attribute type
958089bf1a6SNicolas Dichtel  * @attrlen: length of attribute payload
95911a99573SNicolas Dichtel  * @padattr: attribute type for the padding
960089bf1a6SNicolas Dichtel  *
961089bf1a6SNicolas Dichtel  * Adds a netlink attribute header to a socket buffer and reserves
962089bf1a6SNicolas Dichtel  * room for the payload but does not copy it. It also ensure that this
96311a99573SNicolas Dichtel  * attribute will have a 64-bit aligned nla_data() area.
964089bf1a6SNicolas Dichtel  *
965089bf1a6SNicolas Dichtel  * Returns NULL if the tailroom of the skb is insufficient to store
966089bf1a6SNicolas Dichtel  * the attribute header and payload.
967089bf1a6SNicolas Dichtel  */
nla_reserve_64bit(struct sk_buff * skb,int attrtype,int attrlen,int padattr)968089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
969089bf1a6SNicolas Dichtel 				 int padattr)
970089bf1a6SNicolas Dichtel {
971089bf1a6SNicolas Dichtel 	size_t len;
972089bf1a6SNicolas Dichtel 
973089bf1a6SNicolas Dichtel 	if (nla_need_padding_for_64bit(skb))
974089bf1a6SNicolas Dichtel 		len = nla_total_size_64bit(attrlen);
975089bf1a6SNicolas Dichtel 	else
976089bf1a6SNicolas Dichtel 		len = nla_total_size(attrlen);
977089bf1a6SNicolas Dichtel 	if (unlikely(skb_tailroom(skb) < len))
978089bf1a6SNicolas Dichtel 		return NULL;
979089bf1a6SNicolas Dichtel 
980089bf1a6SNicolas Dichtel 	return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
981089bf1a6SNicolas Dichtel }
982089bf1a6SNicolas Dichtel EXPORT_SYMBOL(nla_reserve_64bit);
983089bf1a6SNicolas Dichtel 
984089bf1a6SNicolas Dichtel /**
985e9cc8bddSGeert Uytterhoeven  * nla_reserve_nohdr - reserve room for attribute without header
986e9cc8bddSGeert Uytterhoeven  * @skb: socket buffer to reserve room on
987e9cc8bddSGeert Uytterhoeven  * @attrlen: length of attribute payload
988e9cc8bddSGeert Uytterhoeven  *
989e9cc8bddSGeert Uytterhoeven  * Reserves room for attribute payload without a header.
990e9cc8bddSGeert Uytterhoeven  *
991e9cc8bddSGeert Uytterhoeven  * Returns NULL if the tailroom of the skb is insufficient to store
992e9cc8bddSGeert Uytterhoeven  * the attribute payload.
993e9cc8bddSGeert Uytterhoeven  */
nla_reserve_nohdr(struct sk_buff * skb,int attrlen)994e9cc8bddSGeert Uytterhoeven void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
995e9cc8bddSGeert Uytterhoeven {
996e9cc8bddSGeert Uytterhoeven 	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
997e9cc8bddSGeert Uytterhoeven 		return NULL;
998e9cc8bddSGeert Uytterhoeven 
999e9cc8bddSGeert Uytterhoeven 	return __nla_reserve_nohdr(skb, attrlen);
1000e9cc8bddSGeert Uytterhoeven }
100190800216SHerbert Xu EXPORT_SYMBOL(nla_reserve_nohdr);
1002e9cc8bddSGeert Uytterhoeven 
1003e9cc8bddSGeert Uytterhoeven /**
1004e9cc8bddSGeert Uytterhoeven  * __nla_put - Add a netlink attribute to a socket buffer
1005e9cc8bddSGeert Uytterhoeven  * @skb: socket buffer to add attribute to
1006e9cc8bddSGeert Uytterhoeven  * @attrtype: attribute type
1007e9cc8bddSGeert Uytterhoeven  * @attrlen: length of attribute payload
1008e9cc8bddSGeert Uytterhoeven  * @data: head of attribute payload
1009e9cc8bddSGeert Uytterhoeven  *
1010e9cc8bddSGeert Uytterhoeven  * The caller is responsible to ensure that the skb provides enough
1011e9cc8bddSGeert Uytterhoeven  * tailroom for the attribute header and payload.
1012e9cc8bddSGeert Uytterhoeven  */
__nla_put(struct sk_buff * skb,int attrtype,int attrlen,const void * data)1013e9cc8bddSGeert Uytterhoeven void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
1014e9cc8bddSGeert Uytterhoeven 			     const void *data)
1015e9cc8bddSGeert Uytterhoeven {
1016e9cc8bddSGeert Uytterhoeven 	struct nlattr *nla;
1017e9cc8bddSGeert Uytterhoeven 
1018e9cc8bddSGeert Uytterhoeven 	nla = __nla_reserve(skb, attrtype, attrlen);
1019e9cc8bddSGeert Uytterhoeven 	memcpy(nla_data(nla), data, attrlen);
1020e9cc8bddSGeert Uytterhoeven }
102190800216SHerbert Xu EXPORT_SYMBOL(__nla_put);
1022e9cc8bddSGeert Uytterhoeven 
1023e9cc8bddSGeert Uytterhoeven /**
1024089bf1a6SNicolas Dichtel  * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1025089bf1a6SNicolas Dichtel  * @skb: socket buffer to add attribute to
1026089bf1a6SNicolas Dichtel  * @attrtype: attribute type
1027089bf1a6SNicolas Dichtel  * @attrlen: length of attribute payload
1028089bf1a6SNicolas Dichtel  * @data: head of attribute payload
102911a99573SNicolas Dichtel  * @padattr: attribute type for the padding
1030089bf1a6SNicolas Dichtel  *
1031089bf1a6SNicolas Dichtel  * The caller is responsible to ensure that the skb provides enough
1032089bf1a6SNicolas Dichtel  * tailroom for the attribute header and payload.
1033089bf1a6SNicolas Dichtel  */
__nla_put_64bit(struct sk_buff * skb,int attrtype,int attrlen,const void * data,int padattr)1034089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
1035089bf1a6SNicolas Dichtel 		     const void *data, int padattr)
1036089bf1a6SNicolas Dichtel {
1037089bf1a6SNicolas Dichtel 	struct nlattr *nla;
1038089bf1a6SNicolas Dichtel 
1039089bf1a6SNicolas Dichtel 	nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
1040089bf1a6SNicolas Dichtel 	memcpy(nla_data(nla), data, attrlen);
1041089bf1a6SNicolas Dichtel }
1042089bf1a6SNicolas Dichtel EXPORT_SYMBOL(__nla_put_64bit);
1043089bf1a6SNicolas Dichtel 
1044089bf1a6SNicolas Dichtel /**
1045e9cc8bddSGeert Uytterhoeven  * __nla_put_nohdr - Add a netlink attribute without header
1046e9cc8bddSGeert Uytterhoeven  * @skb: socket buffer to add attribute to
1047e9cc8bddSGeert Uytterhoeven  * @attrlen: length of attribute payload
1048e9cc8bddSGeert Uytterhoeven  * @data: head of attribute payload
1049e9cc8bddSGeert Uytterhoeven  *
1050e9cc8bddSGeert Uytterhoeven  * The caller is responsible to ensure that the skb provides enough
1051e9cc8bddSGeert Uytterhoeven  * tailroom for the attribute payload.
1052e9cc8bddSGeert Uytterhoeven  */
__nla_put_nohdr(struct sk_buff * skb,int attrlen,const void * data)1053e9cc8bddSGeert Uytterhoeven void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
1054e9cc8bddSGeert Uytterhoeven {
1055e9cc8bddSGeert Uytterhoeven 	void *start;
1056e9cc8bddSGeert Uytterhoeven 
1057e9cc8bddSGeert Uytterhoeven 	start = __nla_reserve_nohdr(skb, attrlen);
1058e9cc8bddSGeert Uytterhoeven 	memcpy(start, data, attrlen);
1059e9cc8bddSGeert Uytterhoeven }
106090800216SHerbert Xu EXPORT_SYMBOL(__nla_put_nohdr);
1061e9cc8bddSGeert Uytterhoeven 
1062e9cc8bddSGeert Uytterhoeven /**
1063e9cc8bddSGeert Uytterhoeven  * nla_put - Add a netlink attribute to a socket buffer
1064e9cc8bddSGeert Uytterhoeven  * @skb: socket buffer to add attribute to
1065e9cc8bddSGeert Uytterhoeven  * @attrtype: attribute type
1066e9cc8bddSGeert Uytterhoeven  * @attrlen: length of attribute payload
1067e9cc8bddSGeert Uytterhoeven  * @data: head of attribute payload
1068e9cc8bddSGeert Uytterhoeven  *
1069e9cc8bddSGeert Uytterhoeven  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1070e9cc8bddSGeert Uytterhoeven  * the attribute header and payload.
1071e9cc8bddSGeert Uytterhoeven  */
nla_put(struct sk_buff * skb,int attrtype,int attrlen,const void * data)1072e9cc8bddSGeert Uytterhoeven int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
1073e9cc8bddSGeert Uytterhoeven {
1074e9cc8bddSGeert Uytterhoeven 	if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
1075e9cc8bddSGeert Uytterhoeven 		return -EMSGSIZE;
1076e9cc8bddSGeert Uytterhoeven 
1077e9cc8bddSGeert Uytterhoeven 	__nla_put(skb, attrtype, attrlen, data);
1078e9cc8bddSGeert Uytterhoeven 	return 0;
1079e9cc8bddSGeert Uytterhoeven }
108090800216SHerbert Xu EXPORT_SYMBOL(nla_put);
1081e9cc8bddSGeert Uytterhoeven 
1082e9cc8bddSGeert Uytterhoeven /**
1083089bf1a6SNicolas Dichtel  * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1084089bf1a6SNicolas Dichtel  * @skb: socket buffer to add attribute to
1085089bf1a6SNicolas Dichtel  * @attrtype: attribute type
1086089bf1a6SNicolas Dichtel  * @attrlen: length of attribute payload
1087089bf1a6SNicolas Dichtel  * @data: head of attribute payload
108811a99573SNicolas Dichtel  * @padattr: attribute type for the padding
1089089bf1a6SNicolas Dichtel  *
1090089bf1a6SNicolas Dichtel  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1091089bf1a6SNicolas Dichtel  * the attribute header and payload.
1092089bf1a6SNicolas Dichtel  */
nla_put_64bit(struct sk_buff * skb,int attrtype,int attrlen,const void * data,int padattr)1093089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
1094089bf1a6SNicolas Dichtel 		  const void *data, int padattr)
1095089bf1a6SNicolas Dichtel {
1096089bf1a6SNicolas Dichtel 	size_t len;
1097089bf1a6SNicolas Dichtel 
1098089bf1a6SNicolas Dichtel 	if (nla_need_padding_for_64bit(skb))
1099089bf1a6SNicolas Dichtel 		len = nla_total_size_64bit(attrlen);
1100089bf1a6SNicolas Dichtel 	else
1101089bf1a6SNicolas Dichtel 		len = nla_total_size(attrlen);
1102089bf1a6SNicolas Dichtel 	if (unlikely(skb_tailroom(skb) < len))
1103089bf1a6SNicolas Dichtel 		return -EMSGSIZE;
1104089bf1a6SNicolas Dichtel 
1105089bf1a6SNicolas Dichtel 	__nla_put_64bit(skb, attrtype, attrlen, data, padattr);
1106089bf1a6SNicolas Dichtel 	return 0;
1107089bf1a6SNicolas Dichtel }
1108089bf1a6SNicolas Dichtel EXPORT_SYMBOL(nla_put_64bit);
1109089bf1a6SNicolas Dichtel 
1110089bf1a6SNicolas Dichtel /**
1111e9cc8bddSGeert Uytterhoeven  * nla_put_nohdr - Add a netlink attribute without header
1112e9cc8bddSGeert Uytterhoeven  * @skb: socket buffer to add attribute to
1113e9cc8bddSGeert Uytterhoeven  * @attrlen: length of attribute payload
1114e9cc8bddSGeert Uytterhoeven  * @data: head of attribute payload
1115e9cc8bddSGeert Uytterhoeven  *
1116e9cc8bddSGeert Uytterhoeven  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1117e9cc8bddSGeert Uytterhoeven  * the attribute payload.
1118e9cc8bddSGeert Uytterhoeven  */
nla_put_nohdr(struct sk_buff * skb,int attrlen,const void * data)1119e9cc8bddSGeert Uytterhoeven int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
1120e9cc8bddSGeert Uytterhoeven {
1121e9cc8bddSGeert Uytterhoeven 	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
1122e9cc8bddSGeert Uytterhoeven 		return -EMSGSIZE;
1123e9cc8bddSGeert Uytterhoeven 
1124e9cc8bddSGeert Uytterhoeven 	__nla_put_nohdr(skb, attrlen, data);
1125e9cc8bddSGeert Uytterhoeven 	return 0;
1126e9cc8bddSGeert Uytterhoeven }
112790800216SHerbert Xu EXPORT_SYMBOL(nla_put_nohdr);
1128e9cc8bddSGeert Uytterhoeven 
1129e9cc8bddSGeert Uytterhoeven /**
1130e9cc8bddSGeert Uytterhoeven  * nla_append - Add a netlink attribute without header or padding
1131e9cc8bddSGeert Uytterhoeven  * @skb: socket buffer to add attribute to
1132e9cc8bddSGeert Uytterhoeven  * @attrlen: length of attribute payload
1133e9cc8bddSGeert Uytterhoeven  * @data: head of attribute payload
1134e9cc8bddSGeert Uytterhoeven  *
1135e9cc8bddSGeert Uytterhoeven  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1136e9cc8bddSGeert Uytterhoeven  * the attribute payload.
1137e9cc8bddSGeert Uytterhoeven  */
nla_append(struct sk_buff * skb,int attrlen,const void * data)1138e9cc8bddSGeert Uytterhoeven int nla_append(struct sk_buff *skb, int attrlen, const void *data)
1139e9cc8bddSGeert Uytterhoeven {
1140e9cc8bddSGeert Uytterhoeven 	if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
1141e9cc8bddSGeert Uytterhoeven 		return -EMSGSIZE;
1142e9cc8bddSGeert Uytterhoeven 
114359ae1d12SJohannes Berg 	skb_put_data(skb, data, attrlen);
1144e9cc8bddSGeert Uytterhoeven 	return 0;
1145e9cc8bddSGeert Uytterhoeven }
114690800216SHerbert Xu EXPORT_SYMBOL(nla_append);
114790800216SHerbert Xu #endif
1148