blob: 7f7ebd89caa4ac683dbe969b9eda5d6ea8e66c21 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Grafbfa83a92005-11-10 02:25:51 +01002/*
3 * NETLINK Netlink attributes
4 *
5 * Authors: Thomas Graf <tgraf@suug.ch>
6 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7 */
8
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05009#include <linux/export.h>
Thomas Grafbfa83a92005-11-10 02:25:51 +010010#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/jiffies.h>
Thomas Grafbfa83a92005-11-10 02:25:51 +010013#include <linux/skbuff.h>
14#include <linux/string.h>
15#include <linux/types.h>
16#include <net/netlink.h>
17
David Ahern6e237d02017-12-06 20:09:12 -080018/* 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 */
David Ahern28033ae2017-11-07 21:59:40 -080023static const u8 nla_attr_len[NLA_TYPE_MAX+1] = {
Thomas Grafbfa83a92005-11-10 02:25:51 +010024 [NLA_U8] = sizeof(u8),
25 [NLA_U16] = sizeof(u16),
26 [NLA_U32] = sizeof(u32),
27 [NLA_U64] = sizeof(u64),
Julian Anastasov9eca2eb2012-08-25 22:47:57 +000028 [NLA_S8] = sizeof(s8),
29 [NLA_S16] = sizeof(s16),
30 [NLA_S32] = sizeof(s32),
31 [NLA_S64] = sizeof(s64),
Thomas Grafbfa83a92005-11-10 02:25:51 +010032};
33
David Ahern28033ae2017-11-07 21:59:40 -080034static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
David Ahern6e237d02017-12-06 20:09:12 -080035 [NLA_U8] = sizeof(u8),
36 [NLA_U16] = sizeof(u16),
37 [NLA_U32] = sizeof(u32),
38 [NLA_U64] = sizeof(u64),
David Ahern28033ae2017-11-07 21:59:40 -080039 [NLA_MSECS] = sizeof(u64),
40 [NLA_NESTED] = NLA_HDRLEN,
David Ahern6e237d02017-12-06 20:09:12 -080041 [NLA_S8] = sizeof(s8),
42 [NLA_S16] = sizeof(s16),
43 [NLA_S32] = sizeof(s32),
44 [NLA_S64] = sizeof(s64),
David Ahern28033ae2017-11-07 21:59:40 -080045};
46
Johannes Berg7690aa12020-04-30 22:13:06 +020047/*
48 * Nested policies might refer back to the original
49 * policy in some cases, and userspace could try to
50 * abuse that and recurse by nesting in the right
51 * ways. Limit recursion to avoid this problem.
52 */
53#define MAX_POLICY_RECURSION_DEPTH 10
54
55static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
56 const struct nla_policy *policy,
57 unsigned int validate,
58 struct netlink_ext_ack *extack,
59 struct nlattr **tb, unsigned int depth);
60
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -040061static int validate_nla_bitfield32(const struct nlattr *nla,
Johannes Berg47a14942020-04-30 22:13:05 +020062 const u32 valid_flags_mask)
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -040063{
64 const struct nla_bitfield32 *bf = nla_data(nla);
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -040065
Johannes Berg48fde902018-09-26 11:15:31 +020066 if (!valid_flags_mask)
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -040067 return -EINVAL;
68
69 /*disallow invalid bit selector */
Johannes Berg47a14942020-04-30 22:13:05 +020070 if (bf->selector & ~valid_flags_mask)
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -040071 return -EINVAL;
72
73 /*disallow invalid bit values */
Johannes Berg47a14942020-04-30 22:13:05 +020074 if (bf->value & ~valid_flags_mask)
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -040075 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
Johannes Berg1501d132018-09-26 11:15:34 +020084static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
85 const struct nla_policy *policy,
Johannes Berg8cb08172019-04-26 14:07:28 +020086 struct netlink_ext_ack *extack,
Johannes Berg7690aa12020-04-30 22:13:06 +020087 unsigned int validate, unsigned int depth)
Johannes Berg1501d132018-09-26 11:15:34 +020088{
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(extack, entry,
100 "Array element too short");
101 return -ERANGE;
102 }
103
Johannes Berg7690aa12020-04-30 22:13:06 +0200104 ret = __nla_validate_parse(nla_data(entry), nla_len(entry),
105 maxtype, policy, validate, extack,
106 NULL, depth + 1);
Johannes Berg1501d132018-09-26 11:15:34 +0200107 if (ret < 0)
108 return ret;
109 }
110
111 return 0;
112}
113
Johannes Berg3e48be02018-09-27 11:28:35 +0200114static int nla_validate_int_range(const struct nla_policy *pt,
115 const struct nlattr *nla,
116 struct netlink_ext_ack *extack)
117{
118 bool validate_min, validate_max;
119 s64 value;
120
121 validate_min = pt->validation_type == NLA_VALIDATE_RANGE ||
122 pt->validation_type == NLA_VALIDATE_MIN;
123 validate_max = pt->validation_type == NLA_VALIDATE_RANGE ||
124 pt->validation_type == NLA_VALIDATE_MAX;
125
126 switch (pt->type) {
127 case NLA_U8:
128 value = nla_get_u8(nla);
129 break;
130 case NLA_U16:
131 value = nla_get_u16(nla);
132 break;
133 case NLA_U32:
134 value = nla_get_u32(nla);
135 break;
136 case NLA_S8:
137 value = nla_get_s8(nla);
138 break;
139 case NLA_S16:
140 value = nla_get_s16(nla);
141 break;
142 case NLA_S32:
143 value = nla_get_s32(nla);
144 break;
145 case NLA_S64:
146 value = nla_get_s64(nla);
147 break;
148 case NLA_U64:
149 /* treat this one specially, since it may not fit into s64 */
150 if ((validate_min && nla_get_u64(nla) < pt->min) ||
151 (validate_max && nla_get_u64(nla) > pt->max)) {
152 NL_SET_ERR_MSG_ATTR(extack, nla,
153 "integer out of range");
154 return -ERANGE;
155 }
156 return 0;
157 default:
158 WARN_ON(1);
159 return -EINVAL;
160 }
161
162 if ((validate_min && value < pt->min) ||
163 (validate_max && value > pt->max)) {
164 NL_SET_ERR_MSG_ATTR(extack, nla,
165 "integer out of range");
166 return -ERANGE;
167 }
168
169 return 0;
170}
171
Jan Engelhardt36546542010-11-16 09:52:32 -0800172static int validate_nla(const struct nlattr *nla, int maxtype,
Johannes Berg8cb08172019-04-26 14:07:28 +0200173 const struct nla_policy *policy, unsigned int validate,
Johannes Berg7690aa12020-04-30 22:13:06 +0200174 struct netlink_ext_ack *extack, unsigned int depth)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100175{
Johannes Berg56738f42019-04-26 14:07:30 +0200176 u16 strict_start_type = policy[0].strict_start_type;
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700177 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200178 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Johannes Bergc29f1842018-09-26 11:15:32 +0200179 int err = -ERANGE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100180
Johannes Berg56738f42019-04-26 14:07:30 +0200181 if (strict_start_type && type >= strict_start_type)
182 validate |= NL_VALIDATE_STRICT;
183
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200184 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100185 return 0;
186
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200187 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +0100188
189 BUG_ON(pt->type > NLA_TYPE_MAX);
190
Johannes Bergb60b87f2018-09-17 11:57:29 +0200191 if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) ||
192 (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) {
David Ahern6e237d02017-12-06 20:09:12 -0800193 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
194 current->comm, type);
Johannes Berg8cb08172019-04-26 14:07:28 +0200195 if (validate & NL_VALIDATE_STRICT_ATTRS) {
196 NL_SET_ERR_MSG_ATTR(extack, nla,
197 "invalid attribute length");
198 return -EINVAL;
199 }
David Ahern28033ae2017-11-07 21:59:40 -0800200 }
201
Michal Kubecekb424e432019-05-02 16:15:10 +0200202 if (validate & NL_VALIDATE_NESTED) {
203 if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
204 !(nla->nla_type & NLA_F_NESTED)) {
205 NL_SET_ERR_MSG_ATTR(extack, nla,
206 "NLA_F_NESTED is missing");
207 return -EINVAL;
208 }
209 if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
210 pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
211 NL_SET_ERR_MSG_ATTR(extack, nla,
212 "NLA_F_NESTED not expected");
213 return -EINVAL;
214 }
215 }
216
Thomas Grafa5531a52006-08-26 20:11:47 -0700217 switch (pt->type) {
Johannes Bergb60b87f2018-09-17 11:57:29 +0200218 case NLA_EXACT_LEN:
219 if (attrlen != pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200220 goto out_err;
Johannes Bergb60b87f2018-09-17 11:57:29 +0200221 break;
222
Johannes Berg568b7422018-09-17 11:57:28 +0200223 case NLA_REJECT:
Johannes Berg47a14942020-04-30 22:13:05 +0200224 if (extack && pt->reject_message) {
Johannes Bergc29f1842018-09-26 11:15:32 +0200225 NL_SET_BAD_ATTR(extack, nla);
Johannes Berg47a14942020-04-30 22:13:05 +0200226 extack->_msg = pt->reject_message;
Johannes Bergc29f1842018-09-26 11:15:32 +0200227 return -EINVAL;
228 }
229 err = -EINVAL;
230 goto out_err;
Johannes Berg568b7422018-09-17 11:57:28 +0200231
Thomas Grafa5531a52006-08-26 20:11:47 -0700232 case NLA_FLAG:
233 if (attrlen > 0)
Johannes Bergc29f1842018-09-26 11:15:32 +0200234 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700235 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100236
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400237 case NLA_BITFIELD32:
238 if (attrlen != sizeof(struct nla_bitfield32))
Johannes Bergc29f1842018-09-26 11:15:32 +0200239 goto out_err;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400240
Johannes Berg47a14942020-04-30 22:13:05 +0200241 err = validate_nla_bitfield32(nla, pt->bitfield32_valid);
Johannes Bergc29f1842018-09-26 11:15:32 +0200242 if (err)
243 goto out_err;
244 break;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400245
Thomas Grafa5531a52006-08-26 20:11:47 -0700246 case NLA_NUL_STRING:
247 if (pt->len)
248 minlen = min_t(int, attrlen, pt->len + 1);
249 else
250 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100251
Johannes Bergc29f1842018-09-26 11:15:32 +0200252 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
253 err = -EINVAL;
254 goto out_err;
255 }
Thomas Grafa5531a52006-08-26 20:11:47 -0700256 /* fall through */
257
258 case NLA_STRING:
259 if (attrlen < 1)
Johannes Bergc29f1842018-09-26 11:15:32 +0200260 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700261
262 if (pt->len) {
263 char *buf = nla_data(nla);
264
265 if (buf[attrlen - 1] == '\0')
266 attrlen--;
267
268 if (attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200269 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700270 }
271 break;
272
Johannes Bergd30045a2007-03-23 11:37:48 -0700273 case NLA_BINARY:
274 if (pt->len && attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200275 goto out_err;
Johannes Bergd30045a2007-03-23 11:37:48 -0700276 break;
277
Patrick McHardyea5693c2008-11-28 03:05:19 -0800278 case NLA_NESTED:
279 /* a nested attributes is allowed to be empty; if its not,
280 * it must have a size of at least NLA_HDRLEN.
281 */
282 if (attrlen == 0)
283 break;
Johannes Berg9a659a32018-09-26 11:15:33 +0200284 if (attrlen < NLA_HDRLEN)
285 goto out_err;
Johannes Berg47a14942020-04-30 22:13:05 +0200286 if (pt->nested_policy) {
Johannes Berg7690aa12020-04-30 22:13:06 +0200287 err = __nla_validate_parse(nla_data(nla), nla_len(nla),
288 pt->len, pt->nested_policy,
289 validate, extack, NULL,
290 depth + 1);
Johannes Berg9a659a32018-09-26 11:15:33 +0200291 if (err < 0) {
292 /*
293 * return directly to preserve the inner
294 * error message/attribute pointer
295 */
296 return err;
297 }
298 }
299 break;
Johannes Berg1501d132018-09-26 11:15:34 +0200300 case NLA_NESTED_ARRAY:
301 /* a nested array attribute is allowed to be empty; if its not,
302 * it must have a size of at least NLA_HDRLEN.
303 */
304 if (attrlen == 0)
305 break;
306 if (attrlen < NLA_HDRLEN)
307 goto out_err;
Johannes Berg47a14942020-04-30 22:13:05 +0200308 if (pt->nested_policy) {
Johannes Berg1501d132018-09-26 11:15:34 +0200309 int err;
310
311 err = nla_validate_array(nla_data(nla), nla_len(nla),
Johannes Berg47a14942020-04-30 22:13:05 +0200312 pt->len, pt->nested_policy,
Johannes Berg7690aa12020-04-30 22:13:06 +0200313 extack, validate, depth);
Johannes Berg1501d132018-09-26 11:15:34 +0200314 if (err < 0) {
315 /*
316 * return directly to preserve the inner
317 * error message/attribute pointer
318 */
319 return err;
320 }
321 }
322 break;
Johannes Berg6f455f52019-04-26 14:07:27 +0200323
324 case NLA_UNSPEC:
Johannes Berg8cb08172019-04-26 14:07:28 +0200325 if (validate & NL_VALIDATE_UNSPEC) {
326 NL_SET_ERR_MSG_ATTR(extack, nla,
327 "Unsupported attribute");
328 return -EINVAL;
329 }
330 /* fall through */
Johannes Berg6f455f52019-04-26 14:07:27 +0200331 case NLA_MIN_LEN:
332 if (attrlen < pt->len)
333 goto out_err;
334 break;
335
Thomas Grafa5531a52006-08-26 20:11:47 -0700336 default:
337 if (pt->len)
338 minlen = pt->len;
Johannes Berg6f455f52019-04-26 14:07:27 +0200339 else
Thomas Grafa5531a52006-08-26 20:11:47 -0700340 minlen = nla_attr_minlen[pt->type];
341
342 if (attrlen < minlen)
Johannes Bergc29f1842018-09-26 11:15:32 +0200343 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700344 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100345
Johannes Berg3e48be02018-09-27 11:28:35 +0200346 /* further validation */
347 switch (pt->validation_type) {
348 case NLA_VALIDATE_NONE:
349 /* nothing to do */
350 break;
351 case NLA_VALIDATE_RANGE:
352 case NLA_VALIDATE_MIN:
353 case NLA_VALIDATE_MAX:
354 err = nla_validate_int_range(pt, nla, extack);
355 if (err)
356 return err;
357 break;
Johannes Berg33188bd2018-09-27 11:28:36 +0200358 case NLA_VALIDATE_FUNCTION:
359 if (pt->validate) {
360 err = pt->validate(nla, extack);
361 if (err)
362 return err;
363 }
364 break;
Johannes Berg3e48be02018-09-27 11:28:35 +0200365 }
366
Thomas Grafbfa83a92005-11-10 02:25:51 +0100367 return 0;
Johannes Bergc29f1842018-09-26 11:15:32 +0200368out_err:
369 NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation");
370 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100371}
372
Johannes Berg8cb08172019-04-26 14:07:28 +0200373static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
374 const struct nla_policy *policy,
375 unsigned int validate,
376 struct netlink_ext_ack *extack,
Johannes Berg7690aa12020-04-30 22:13:06 +0200377 struct nlattr **tb, unsigned int depth)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100378{
Jan Engelhardt36546542010-11-16 09:52:32 -0800379 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200380 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100381
Johannes Berg7690aa12020-04-30 22:13:06 +0200382 if (depth >= MAX_POLICY_RECURSION_DEPTH) {
383 NL_SET_ERR_MSG(extack,
384 "allowed policy recursion depth exceeded");
385 return -EINVAL;
386 }
387
Johannes Berg8cb08172019-04-26 14:07:28 +0200388 if (tb)
389 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
Johannes Bergfceb6432017-04-12 14:34:07 +0200390
Johannes Berg8cb08172019-04-26 14:07:28 +0200391 nla_for_each_attr(nla, head, len, rem) {
392 u16 type = nla_type(nla);
393
394 if (type == 0 || type > maxtype) {
395 if (validate & NL_VALIDATE_MAXTYPE) {
Michal Kubecekd54a16b2019-05-02 16:15:10 +0200396 NL_SET_ERR_MSG_ATTR(extack, nla,
397 "Unknown attribute type");
Johannes Berg8cb08172019-04-26 14:07:28 +0200398 return -EINVAL;
399 }
400 continue;
401 }
402 if (policy) {
403 int err = validate_nla(nla, maxtype, policy,
Johannes Berg7690aa12020-04-30 22:13:06 +0200404 validate, extack, depth);
Johannes Berg8cb08172019-04-26 14:07:28 +0200405
406 if (err < 0)
407 return err;
408 }
409
410 if (tb)
411 tb[type] = (struct nlattr *)nla;
412 }
413
414 if (unlikely(rem > 0)) {
415 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
416 rem, current->comm);
417 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
418 if (validate & NL_VALIDATE_TRAILING)
419 return -EINVAL;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100420 }
421
Johannes Bergfceb6432017-04-12 14:34:07 +0200422 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100423}
Johannes Berg8cb08172019-04-26 14:07:28 +0200424
425/**
426 * __nla_validate - Validate a stream of attributes
427 * @head: head of attribute stream
428 * @len: length of attribute stream
429 * @maxtype: maximum attribute type to be expected
430 * @policy: validation policy
431 * @validate: validation strictness
432 * @extack: extended ACK report struct
433 *
434 * Validates all attributes in the specified attribute stream against the
435 * specified policy. Validation depends on the validate flags passed, see
436 * &enum netlink_validation for more details on that.
437 * See documenation of struct nla_policy for more details.
438 *
439 * Returns 0 on success or a negative error code.
440 */
441int __nla_validate(const struct nlattr *head, int len, int maxtype,
442 const struct nla_policy *policy, unsigned int validate,
443 struct netlink_ext_ack *extack)
444{
445 return __nla_validate_parse(head, len, maxtype, policy, validate,
Johannes Berg7690aa12020-04-30 22:13:06 +0200446 extack, NULL, 0);
Johannes Berg8cb08172019-04-26 14:07:28 +0200447}
448EXPORT_SYMBOL(__nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100449
450/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100451 * nla_policy_len - Determin the max. length of a policy
452 * @policy: policy to use
453 * @n: number of policies
454 *
455 * Determines the max. length of the policy. It is currently used
456 * to allocated Netlink buffers roughly the size of the actual
457 * message.
458 *
459 * Returns 0 on success or a negative error code.
460 */
461int
462nla_policy_len(const struct nla_policy *p, int n)
463{
464 int i, len = 0;
465
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800466 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100467 if (p->len)
468 len += nla_total_size(p->len);
David Ahern28033ae2017-11-07 21:59:40 -0800469 else if (nla_attr_len[p->type])
470 len += nla_total_size(nla_attr_len[p->type]);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100471 else if (nla_attr_minlen[p->type])
472 len += nla_total_size(nla_attr_minlen[p->type]);
473 }
474
475 return len;
476}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700477EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100478
479/**
Johannes Berg8cb08172019-04-26 14:07:28 +0200480 * __nla_parse - Parse a stream of attributes into a tb buffer
Thomas Grafbfa83a92005-11-10 02:25:51 +0100481 * @tb: destination array with maxtype+1 elements
482 * @maxtype: maximum attribute type to be expected
483 * @head: head of attribute stream
484 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700485 * @policy: validation policy
Johannes Berg8cb08172019-04-26 14:07:28 +0200486 * @validate: validation strictness
487 * @extack: extended ACK pointer
Thomas Grafbfa83a92005-11-10 02:25:51 +0100488 *
489 * Parses a stream of attributes and stores a pointer to each attribute in
Johannes Berg8cb08172019-04-26 14:07:28 +0200490 * the tb array accessible via the attribute type.
491 * Validation is controlled by the @validate parameter.
Thomas Grafbfa83a92005-11-10 02:25:51 +0100492 *
493 * Returns 0 on success or a negative error code.
494 */
Johannes Berg8cb08172019-04-26 14:07:28 +0200495int __nla_parse(struct nlattr **tb, int maxtype,
496 const struct nlattr *head, int len,
497 const struct nla_policy *policy, unsigned int validate,
498 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100499{
Johannes Berg8cb08172019-04-26 14:07:28 +0200500 return __nla_validate_parse(head, len, maxtype, policy, validate,
Johannes Berg7690aa12020-04-30 22:13:06 +0200501 extack, tb, 0);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100502}
Johannes Berg8cb08172019-04-26 14:07:28 +0200503EXPORT_SYMBOL(__nla_parse);
David Aherna5f6cba2018-10-07 20:16:25 -0700504
Thomas Grafbfa83a92005-11-10 02:25:51 +0100505/**
506 * nla_find - Find a specific attribute in a stream of attributes
507 * @head: head of attribute stream
508 * @len: length of attribute stream
509 * @attrtype: type of attribute to look for
510 *
511 * Returns the first attribute in the stream matching the specified type.
512 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800513struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100514{
Jan Engelhardt36546542010-11-16 09:52:32 -0800515 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100516 int rem;
517
518 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200519 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800520 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100521
522 return NULL;
523}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700524EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100525
526/**
527 * nla_strlcpy - Copy string attribute payload into a sized buffer
528 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700529 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100530 * @dstsize: size of destination buffer
531 *
532 * Copies at most dstsize - 1 bytes into the destination buffer.
533 * The result is always a valid NUL-terminated string. Unlike
534 * strlcpy the destination buffer is always padded out.
535 *
536 * Returns the length of the source buffer.
537 */
538size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
539{
540 size_t srclen = nla_len(nla);
541 char *src = nla_data(nla);
542
543 if (srclen > 0 && src[srclen - 1] == '\0')
544 srclen--;
545
546 if (dstsize > 0) {
547 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
548
549 memset(dst, 0, dstsize);
550 memcpy(dst, src, len);
551 }
552
553 return srclen;
554}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700555EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100556
557/**
Phil Sutter2cf0c8b2017-07-27 16:56:40 +0200558 * nla_strdup - Copy string attribute payload into a newly allocated buffer
559 * @nla: attribute to copy the string from
560 * @flags: the type of memory to allocate (see kmalloc).
561 *
562 * Returns a pointer to the allocated buffer or NULL on error.
563 */
564char *nla_strdup(const struct nlattr *nla, gfp_t flags)
565{
566 size_t srclen = nla_len(nla);
567 char *src = nla_data(nla), *dst;
568
569 if (srclen > 0 && src[srclen - 1] == '\0')
570 srclen--;
571
572 dst = kmalloc(srclen + 1, flags);
573 if (dst != NULL) {
574 memcpy(dst, src, srclen);
575 dst[srclen] = '\0';
576 }
577 return dst;
578}
579EXPORT_SYMBOL(nla_strdup);
580
581/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100582 * nla_memcpy - Copy a netlink attribute into another memory area
583 * @dest: where to copy to memcpy
584 * @src: netlink attribute to copy from
585 * @count: size of the destination area
586 *
587 * Note: The number of bytes copied is limited by the length of
588 * attribute's payload. memcpy
589 *
590 * Returns the number of bytes copied.
591 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700592int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100593{
594 int minlen = min_t(int, count, nla_len(src));
595
596 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200597 if (count > minlen)
598 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100599
600 return minlen;
601}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700602EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100603
604/**
605 * nla_memcmp - Compare an attribute with sized memory area
606 * @nla: netlink attribute
607 * @data: memory area
608 * @size: size of memory area
609 */
610int nla_memcmp(const struct nlattr *nla, const void *data,
611 size_t size)
612{
613 int d = nla_len(nla) - size;
614
615 if (d == 0)
616 d = memcmp(nla_data(nla), data, size);
617
618 return d;
619}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700620EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100621
622/**
623 * nla_strcmp - Compare a string attribute against a string
624 * @nla: netlink string attribute
625 * @str: another string
626 */
627int nla_strcmp(const struct nlattr *nla, const char *str)
628{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200629 int len = strlen(str);
630 char *buf = nla_data(nla);
631 int attrlen = nla_len(nla);
632 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100633
Pablo Neira8b7b9322014-04-01 19:38:44 +0200634 if (attrlen > 0 && buf[attrlen - 1] == '\0')
635 attrlen--;
636
637 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100638 if (d == 0)
639 d = memcmp(nla_data(nla), str, len);
640
641 return d;
642}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700643EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100644
Herbert Xu90800212009-03-11 23:18:32 +0800645#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100646/**
647 * __nla_reserve - reserve room for attribute on the skb
648 * @skb: socket buffer to reserve room on
649 * @attrtype: attribute type
650 * @attrlen: length of attribute payload
651 *
652 * Adds a netlink attribute header to a socket buffer and reserves
653 * room for the payload but does not copy it.
654 *
655 * The caller is responsible to ensure that the skb provides enough
656 * tailroom for the attribute header and payload.
657 */
658struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
659{
660 struct nlattr *nla;
661
Johannes Berg4df864c2017-06-16 14:29:21 +0200662 nla = skb_put(skb, nla_total_size(attrlen));
Thomas Grafbfa83a92005-11-10 02:25:51 +0100663 nla->nla_type = attrtype;
664 nla->nla_len = nla_attr_size(attrlen);
665
666 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
667
668 return nla;
669}
Herbert Xu90800212009-03-11 23:18:32 +0800670EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100671
672/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200673 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
674 * @skb: socket buffer to reserve room on
675 * @attrtype: attribute type
676 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200677 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200678 *
679 * Adds a netlink attribute header to a socket buffer and reserves
680 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200681 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200682 *
683 * The caller is responsible to ensure that the skb provides enough
684 * tailroom for the attribute header and payload.
685 */
686struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
687 int attrlen, int padattr)
688{
689 if (nla_need_padding_for_64bit(skb))
690 nla_align_64bit(skb, padattr);
691
692 return __nla_reserve(skb, attrtype, attrlen);
693}
694EXPORT_SYMBOL(__nla_reserve_64bit);
695
696/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700697 * __nla_reserve_nohdr - reserve room for attribute without header
698 * @skb: socket buffer to reserve room on
699 * @attrlen: length of attribute payload
700 *
701 * Reserves room for attribute payload without a header.
702 *
703 * The caller is responsible to ensure that the skb provides enough
704 * tailroom for the payload.
705 */
706void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
707{
yuan linyub952f4d2017-06-18 22:52:04 +0800708 return skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700709}
Herbert Xu90800212009-03-11 23:18:32 +0800710EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700711
712/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100713 * nla_reserve - reserve room for attribute on the skb
714 * @skb: socket buffer to reserve room on
715 * @attrtype: attribute type
716 * @attrlen: length of attribute payload
717 *
718 * Adds a netlink attribute header to a socket buffer and reserves
719 * room for the payload but does not copy it.
720 *
721 * Returns NULL if the tailroom of the skb is insufficient to store
722 * the attribute header and payload.
723 */
724struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
725{
726 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
727 return NULL;
728
729 return __nla_reserve(skb, attrtype, attrlen);
730}
Herbert Xu90800212009-03-11 23:18:32 +0800731EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100732
733/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200734 * nla_reserve_64bit - reserve room for attribute on the skb and align it
735 * @skb: socket buffer to reserve room on
736 * @attrtype: attribute type
737 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200738 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200739 *
740 * Adds a netlink attribute header to a socket buffer and reserves
741 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200742 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200743 *
744 * Returns NULL if the tailroom of the skb is insufficient to store
745 * the attribute header and payload.
746 */
747struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
748 int padattr)
749{
750 size_t len;
751
752 if (nla_need_padding_for_64bit(skb))
753 len = nla_total_size_64bit(attrlen);
754 else
755 len = nla_total_size(attrlen);
756 if (unlikely(skb_tailroom(skb) < len))
757 return NULL;
758
759 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
760}
761EXPORT_SYMBOL(nla_reserve_64bit);
762
763/**
Julius Volz10b595a2008-06-27 20:02:14 -0700764 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700765 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700766 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700767 *
768 * Reserves room for attribute payload without a header.
769 *
770 * Returns NULL if the tailroom of the skb is insufficient to store
771 * the attribute payload.
772 */
773void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
774{
775 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
776 return NULL;
777
778 return __nla_reserve_nohdr(skb, attrlen);
779}
Herbert Xu90800212009-03-11 23:18:32 +0800780EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700781
782/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100783 * __nla_put - Add a netlink attribute to a socket buffer
784 * @skb: socket buffer to add attribute to
785 * @attrtype: attribute type
786 * @attrlen: length of attribute payload
787 * @data: head of attribute payload
788 *
789 * The caller is responsible to ensure that the skb provides enough
790 * tailroom for the attribute header and payload.
791 */
792void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
793 const void *data)
794{
795 struct nlattr *nla;
796
797 nla = __nla_reserve(skb, attrtype, attrlen);
798 memcpy(nla_data(nla), data, attrlen);
799}
Herbert Xu90800212009-03-11 23:18:32 +0800800EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100801
Thomas Graffe4944e2006-08-04 23:03:05 -0700802/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200803 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
804 * @skb: socket buffer to add attribute to
805 * @attrtype: attribute type
806 * @attrlen: length of attribute payload
807 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200808 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200809 *
810 * The caller is responsible to ensure that the skb provides enough
811 * tailroom for the attribute header and payload.
812 */
813void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
814 const void *data, int padattr)
815{
816 struct nlattr *nla;
817
818 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
819 memcpy(nla_data(nla), data, attrlen);
820}
821EXPORT_SYMBOL(__nla_put_64bit);
822
823/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700824 * __nla_put_nohdr - Add a netlink attribute without header
825 * @skb: socket buffer to add attribute to
826 * @attrlen: length of attribute payload
827 * @data: head of attribute payload
828 *
829 * The caller is responsible to ensure that the skb provides enough
830 * tailroom for the attribute payload.
831 */
832void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
833{
834 void *start;
835
836 start = __nla_reserve_nohdr(skb, attrlen);
837 memcpy(start, data, attrlen);
838}
Herbert Xu90800212009-03-11 23:18:32 +0800839EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100840
841/**
842 * nla_put - Add a netlink attribute to a socket buffer
843 * @skb: socket buffer to add attribute to
844 * @attrtype: attribute type
845 * @attrlen: length of attribute payload
846 * @data: head of attribute payload
847 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700848 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100849 * the attribute header and payload.
850 */
851int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
852{
853 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700854 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100855
856 __nla_put(skb, attrtype, attrlen, data);
857 return 0;
858}
Herbert Xu90800212009-03-11 23:18:32 +0800859EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100860
Thomas Graffe4944e2006-08-04 23:03:05 -0700861/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200862 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
863 * @skb: socket buffer to add attribute to
864 * @attrtype: attribute type
865 * @attrlen: length of attribute payload
866 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200867 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200868 *
869 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
870 * the attribute header and payload.
871 */
872int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
873 const void *data, int padattr)
874{
875 size_t len;
876
877 if (nla_need_padding_for_64bit(skb))
878 len = nla_total_size_64bit(attrlen);
879 else
880 len = nla_total_size(attrlen);
881 if (unlikely(skb_tailroom(skb) < len))
882 return -EMSGSIZE;
883
884 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
885 return 0;
886}
887EXPORT_SYMBOL(nla_put_64bit);
888
889/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700890 * nla_put_nohdr - Add a netlink attribute without header
891 * @skb: socket buffer to add attribute to
892 * @attrlen: length of attribute payload
893 * @data: head of attribute payload
894 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700895 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700896 * the attribute payload.
897 */
898int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
899{
900 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700901 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700902
903 __nla_put_nohdr(skb, attrlen, data);
904 return 0;
905}
Herbert Xu90800212009-03-11 23:18:32 +0800906EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100907
Patrick McHardy01480e12008-01-22 22:10:59 -0800908/**
909 * nla_append - Add a netlink attribute without header or padding
910 * @skb: socket buffer to add attribute to
911 * @attrlen: length of attribute payload
912 * @data: head of attribute payload
913 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700914 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800915 * the attribute payload.
916 */
917int nla_append(struct sk_buff *skb, int attrlen, const void *data)
918{
919 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700920 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800921
Johannes Berg59ae1d12017-06-16 14:29:20 +0200922 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800923 return 0;
924}
Herbert Xu90800212009-03-11 23:18:32 +0800925EXPORT_SYMBOL(nla_append);
926#endif