blob: a8beb173f558aff41f83a8cd8c15ee5f4204fa8e [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 Bergd06a09b2020-04-30 22:13:08 +0200114static int nla_validate_int_range_unsigned(const struct nla_policy *pt,
115 const struct nlattr *nla,
116 struct netlink_ext_ack *extack)
Johannes Berg3e48be02018-09-27 11:28:35 +0200117{
Johannes Bergd06a09b2020-04-30 22:13:08 +0200118 struct netlink_range_validation _range = {
119 .min = 0,
120 .max = U64_MAX,
121 }, *range = &_range;
122 u64 value;
Johannes Berg3e48be02018-09-27 11:28:35 +0200123
Johannes Bergd06a09b2020-04-30 22:13:08 +0200124 WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR &&
125 (pt->min < 0 || pt->max < 0));
126
127 switch (pt->validation_type) {
128 case NLA_VALIDATE_RANGE:
129 range->min = pt->min;
130 range->max = pt->max;
131 break;
132 case NLA_VALIDATE_RANGE_PTR:
133 range = pt->range;
134 break;
135 case NLA_VALIDATE_MIN:
136 range->min = pt->min;
137 break;
138 case NLA_VALIDATE_MAX:
139 range->max = pt->max;
140 break;
141 }
Johannes Berg3e48be02018-09-27 11:28:35 +0200142
143 switch (pt->type) {
144 case NLA_U8:
145 value = nla_get_u8(nla);
146 break;
147 case NLA_U16:
148 value = nla_get_u16(nla);
149 break;
150 case NLA_U32:
151 value = nla_get_u32(nla);
152 break;
Johannes Bergd06a09b2020-04-30 22:13:08 +0200153 case NLA_U64:
154 value = nla_get_u64(nla);
155 break;
156 default:
157 return -EINVAL;
158 }
159
160 if (value < range->min || value > range->max) {
161 NL_SET_ERR_MSG_ATTR(extack, nla,
162 "integer out of range");
163 return -ERANGE;
164 }
165
166 return 0;
167}
168
169static int nla_validate_int_range_signed(const struct nla_policy *pt,
170 const struct nlattr *nla,
171 struct netlink_ext_ack *extack)
172{
173 struct netlink_range_validation_signed _range = {
174 .min = S64_MIN,
175 .max = S64_MAX,
176 }, *range = &_range;
177 s64 value;
178
179 switch (pt->validation_type) {
180 case NLA_VALIDATE_RANGE:
181 range->min = pt->min;
182 range->max = pt->max;
183 break;
184 case NLA_VALIDATE_RANGE_PTR:
185 range = pt->range_signed;
186 break;
187 case NLA_VALIDATE_MIN:
188 range->min = pt->min;
189 break;
190 case NLA_VALIDATE_MAX:
191 range->max = pt->max;
192 break;
193 }
194
195 switch (pt->type) {
Johannes Berg3e48be02018-09-27 11:28:35 +0200196 case NLA_S8:
197 value = nla_get_s8(nla);
198 break;
199 case NLA_S16:
200 value = nla_get_s16(nla);
201 break;
202 case NLA_S32:
203 value = nla_get_s32(nla);
204 break;
205 case NLA_S64:
206 value = nla_get_s64(nla);
207 break;
Johannes Berg3e48be02018-09-27 11:28:35 +0200208 default:
Johannes Berg3e48be02018-09-27 11:28:35 +0200209 return -EINVAL;
210 }
211
Johannes Bergd06a09b2020-04-30 22:13:08 +0200212 if (value < range->min || value > range->max) {
Johannes Berg3e48be02018-09-27 11:28:35 +0200213 NL_SET_ERR_MSG_ATTR(extack, nla,
214 "integer out of range");
215 return -ERANGE;
216 }
217
218 return 0;
219}
220
Johannes Bergd06a09b2020-04-30 22:13:08 +0200221static int nla_validate_int_range(const struct nla_policy *pt,
222 const struct nlattr *nla,
223 struct netlink_ext_ack *extack)
224{
225 switch (pt->type) {
226 case NLA_U8:
227 case NLA_U16:
228 case NLA_U32:
229 case NLA_U64:
230 return nla_validate_int_range_unsigned(pt, nla, extack);
231 case NLA_S8:
232 case NLA_S16:
233 case NLA_S32:
234 case NLA_S64:
235 return nla_validate_int_range_signed(pt, nla, extack);
236 default:
237 WARN_ON(1);
238 return -EINVAL;
239 }
240}
241
Jan Engelhardt36546542010-11-16 09:52:32 -0800242static int validate_nla(const struct nlattr *nla, int maxtype,
Johannes Berg8cb08172019-04-26 14:07:28 +0200243 const struct nla_policy *policy, unsigned int validate,
Johannes Berg7690aa12020-04-30 22:13:06 +0200244 struct netlink_ext_ack *extack, unsigned int depth)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100245{
Johannes Berg56738f42019-04-26 14:07:30 +0200246 u16 strict_start_type = policy[0].strict_start_type;
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700247 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200248 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Johannes Bergc29f1842018-09-26 11:15:32 +0200249 int err = -ERANGE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100250
Johannes Berg56738f42019-04-26 14:07:30 +0200251 if (strict_start_type && type >= strict_start_type)
252 validate |= NL_VALIDATE_STRICT;
253
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200254 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100255 return 0;
256
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200257 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +0100258
259 BUG_ON(pt->type > NLA_TYPE_MAX);
260
Johannes Bergb60b87f2018-09-17 11:57:29 +0200261 if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) ||
262 (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) {
David Ahern6e237d02017-12-06 20:09:12 -0800263 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
264 current->comm, type);
Johannes Berg8cb08172019-04-26 14:07:28 +0200265 if (validate & NL_VALIDATE_STRICT_ATTRS) {
266 NL_SET_ERR_MSG_ATTR(extack, nla,
267 "invalid attribute length");
268 return -EINVAL;
269 }
David Ahern28033ae2017-11-07 21:59:40 -0800270 }
271
Michal Kubecekb424e432019-05-02 16:15:10 +0200272 if (validate & NL_VALIDATE_NESTED) {
273 if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
274 !(nla->nla_type & NLA_F_NESTED)) {
275 NL_SET_ERR_MSG_ATTR(extack, nla,
276 "NLA_F_NESTED is missing");
277 return -EINVAL;
278 }
279 if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
280 pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
281 NL_SET_ERR_MSG_ATTR(extack, nla,
282 "NLA_F_NESTED not expected");
283 return -EINVAL;
284 }
285 }
286
Thomas Grafa5531a52006-08-26 20:11:47 -0700287 switch (pt->type) {
Johannes Bergb60b87f2018-09-17 11:57:29 +0200288 case NLA_EXACT_LEN:
289 if (attrlen != pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200290 goto out_err;
Johannes Bergb60b87f2018-09-17 11:57:29 +0200291 break;
292
Johannes Berg568b7422018-09-17 11:57:28 +0200293 case NLA_REJECT:
Johannes Berg47a14942020-04-30 22:13:05 +0200294 if (extack && pt->reject_message) {
Johannes Bergc29f1842018-09-26 11:15:32 +0200295 NL_SET_BAD_ATTR(extack, nla);
Johannes Berg47a14942020-04-30 22:13:05 +0200296 extack->_msg = pt->reject_message;
Johannes Bergc29f1842018-09-26 11:15:32 +0200297 return -EINVAL;
298 }
299 err = -EINVAL;
300 goto out_err;
Johannes Berg568b7422018-09-17 11:57:28 +0200301
Thomas Grafa5531a52006-08-26 20:11:47 -0700302 case NLA_FLAG:
303 if (attrlen > 0)
Johannes Bergc29f1842018-09-26 11:15:32 +0200304 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700305 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100306
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400307 case NLA_BITFIELD32:
308 if (attrlen != sizeof(struct nla_bitfield32))
Johannes Bergc29f1842018-09-26 11:15:32 +0200309 goto out_err;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400310
Johannes Berg47a14942020-04-30 22:13:05 +0200311 err = validate_nla_bitfield32(nla, pt->bitfield32_valid);
Johannes Bergc29f1842018-09-26 11:15:32 +0200312 if (err)
313 goto out_err;
314 break;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400315
Thomas Grafa5531a52006-08-26 20:11:47 -0700316 case NLA_NUL_STRING:
317 if (pt->len)
318 minlen = min_t(int, attrlen, pt->len + 1);
319 else
320 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100321
Johannes Bergc29f1842018-09-26 11:15:32 +0200322 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
323 err = -EINVAL;
324 goto out_err;
325 }
Thomas Grafa5531a52006-08-26 20:11:47 -0700326 /* fall through */
327
328 case NLA_STRING:
329 if (attrlen < 1)
Johannes Bergc29f1842018-09-26 11:15:32 +0200330 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700331
332 if (pt->len) {
333 char *buf = nla_data(nla);
334
335 if (buf[attrlen - 1] == '\0')
336 attrlen--;
337
338 if (attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200339 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700340 }
341 break;
342
Johannes Bergd30045a2007-03-23 11:37:48 -0700343 case NLA_BINARY:
344 if (pt->len && attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200345 goto out_err;
Johannes Bergd30045a2007-03-23 11:37:48 -0700346 break;
347
Patrick McHardyea5693c2008-11-28 03:05:19 -0800348 case NLA_NESTED:
349 /* a nested attributes is allowed to be empty; if its not,
350 * it must have a size of at least NLA_HDRLEN.
351 */
352 if (attrlen == 0)
353 break;
Johannes Berg9a659a352018-09-26 11:15:33 +0200354 if (attrlen < NLA_HDRLEN)
355 goto out_err;
Johannes Berg47a14942020-04-30 22:13:05 +0200356 if (pt->nested_policy) {
Johannes Berg7690aa12020-04-30 22:13:06 +0200357 err = __nla_validate_parse(nla_data(nla), nla_len(nla),
358 pt->len, pt->nested_policy,
359 validate, extack, NULL,
360 depth + 1);
Johannes Berg9a659a352018-09-26 11:15:33 +0200361 if (err < 0) {
362 /*
363 * return directly to preserve the inner
364 * error message/attribute pointer
365 */
366 return err;
367 }
368 }
369 break;
Johannes Berg1501d132018-09-26 11:15:34 +0200370 case NLA_NESTED_ARRAY:
371 /* a nested array attribute is allowed to be empty; if its not,
372 * it must have a size of at least NLA_HDRLEN.
373 */
374 if (attrlen == 0)
375 break;
376 if (attrlen < NLA_HDRLEN)
377 goto out_err;
Johannes Berg47a14942020-04-30 22:13:05 +0200378 if (pt->nested_policy) {
Johannes Berg1501d132018-09-26 11:15:34 +0200379 int err;
380
381 err = nla_validate_array(nla_data(nla), nla_len(nla),
Johannes Berg47a14942020-04-30 22:13:05 +0200382 pt->len, pt->nested_policy,
Johannes Berg7690aa12020-04-30 22:13:06 +0200383 extack, validate, depth);
Johannes Berg1501d132018-09-26 11:15:34 +0200384 if (err < 0) {
385 /*
386 * return directly to preserve the inner
387 * error message/attribute pointer
388 */
389 return err;
390 }
391 }
392 break;
Johannes Berg6f455f52019-04-26 14:07:27 +0200393
394 case NLA_UNSPEC:
Johannes Berg8cb08172019-04-26 14:07:28 +0200395 if (validate & NL_VALIDATE_UNSPEC) {
396 NL_SET_ERR_MSG_ATTR(extack, nla,
397 "Unsupported attribute");
398 return -EINVAL;
399 }
400 /* fall through */
Johannes Berg6f455f52019-04-26 14:07:27 +0200401 case NLA_MIN_LEN:
402 if (attrlen < pt->len)
403 goto out_err;
404 break;
405
Thomas Grafa5531a52006-08-26 20:11:47 -0700406 default:
407 if (pt->len)
408 minlen = pt->len;
Johannes Berg6f455f52019-04-26 14:07:27 +0200409 else
Thomas Grafa5531a52006-08-26 20:11:47 -0700410 minlen = nla_attr_minlen[pt->type];
411
412 if (attrlen < minlen)
Johannes Bergc29f1842018-09-26 11:15:32 +0200413 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700414 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100415
Johannes Berg3e48be02018-09-27 11:28:35 +0200416 /* further validation */
417 switch (pt->validation_type) {
418 case NLA_VALIDATE_NONE:
419 /* nothing to do */
420 break;
Johannes Bergd06a09b2020-04-30 22:13:08 +0200421 case NLA_VALIDATE_RANGE_PTR:
Johannes Berg3e48be02018-09-27 11:28:35 +0200422 case NLA_VALIDATE_RANGE:
423 case NLA_VALIDATE_MIN:
424 case NLA_VALIDATE_MAX:
425 err = nla_validate_int_range(pt, nla, extack);
426 if (err)
427 return err;
428 break;
Johannes Berg33188bd2018-09-27 11:28:36 +0200429 case NLA_VALIDATE_FUNCTION:
430 if (pt->validate) {
431 err = pt->validate(nla, extack);
432 if (err)
433 return err;
434 }
435 break;
Johannes Berg3e48be02018-09-27 11:28:35 +0200436 }
437
Thomas Grafbfa83a92005-11-10 02:25:51 +0100438 return 0;
Johannes Bergc29f1842018-09-26 11:15:32 +0200439out_err:
440 NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation");
441 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100442}
443
Johannes Berg8cb08172019-04-26 14:07:28 +0200444static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
445 const struct nla_policy *policy,
446 unsigned int validate,
447 struct netlink_ext_ack *extack,
Johannes Berg7690aa12020-04-30 22:13:06 +0200448 struct nlattr **tb, unsigned int depth)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100449{
Jan Engelhardt36546542010-11-16 09:52:32 -0800450 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200451 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100452
Johannes Berg7690aa12020-04-30 22:13:06 +0200453 if (depth >= MAX_POLICY_RECURSION_DEPTH) {
454 NL_SET_ERR_MSG(extack,
455 "allowed policy recursion depth exceeded");
456 return -EINVAL;
457 }
458
Johannes Berg8cb08172019-04-26 14:07:28 +0200459 if (tb)
460 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
Johannes Bergfceb6432017-04-12 14:34:07 +0200461
Johannes Berg8cb08172019-04-26 14:07:28 +0200462 nla_for_each_attr(nla, head, len, rem) {
463 u16 type = nla_type(nla);
464
465 if (type == 0 || type > maxtype) {
466 if (validate & NL_VALIDATE_MAXTYPE) {
Michal Kubecekd54a16b2019-05-02 16:15:10 +0200467 NL_SET_ERR_MSG_ATTR(extack, nla,
468 "Unknown attribute type");
Johannes Berg8cb08172019-04-26 14:07:28 +0200469 return -EINVAL;
470 }
471 continue;
472 }
473 if (policy) {
474 int err = validate_nla(nla, maxtype, policy,
Johannes Berg7690aa12020-04-30 22:13:06 +0200475 validate, extack, depth);
Johannes Berg8cb08172019-04-26 14:07:28 +0200476
477 if (err < 0)
478 return err;
479 }
480
481 if (tb)
482 tb[type] = (struct nlattr *)nla;
483 }
484
485 if (unlikely(rem > 0)) {
486 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
487 rem, current->comm);
488 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
489 if (validate & NL_VALIDATE_TRAILING)
490 return -EINVAL;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100491 }
492
Johannes Bergfceb6432017-04-12 14:34:07 +0200493 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100494}
Johannes Berg8cb08172019-04-26 14:07:28 +0200495
496/**
497 * __nla_validate - Validate a stream of attributes
498 * @head: head of attribute stream
499 * @len: length of attribute stream
500 * @maxtype: maximum attribute type to be expected
501 * @policy: validation policy
502 * @validate: validation strictness
503 * @extack: extended ACK report struct
504 *
505 * Validates all attributes in the specified attribute stream against the
506 * specified policy. Validation depends on the validate flags passed, see
507 * &enum netlink_validation for more details on that.
508 * See documenation of struct nla_policy for more details.
509 *
510 * Returns 0 on success or a negative error code.
511 */
512int __nla_validate(const struct nlattr *head, int len, int maxtype,
513 const struct nla_policy *policy, unsigned int validate,
514 struct netlink_ext_ack *extack)
515{
516 return __nla_validate_parse(head, len, maxtype, policy, validate,
Johannes Berg7690aa12020-04-30 22:13:06 +0200517 extack, NULL, 0);
Johannes Berg8cb08172019-04-26 14:07:28 +0200518}
519EXPORT_SYMBOL(__nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100520
521/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100522 * nla_policy_len - Determin the max. length of a policy
523 * @policy: policy to use
524 * @n: number of policies
525 *
526 * Determines the max. length of the policy. It is currently used
527 * to allocated Netlink buffers roughly the size of the actual
528 * message.
529 *
530 * Returns 0 on success or a negative error code.
531 */
532int
533nla_policy_len(const struct nla_policy *p, int n)
534{
535 int i, len = 0;
536
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800537 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100538 if (p->len)
539 len += nla_total_size(p->len);
David Ahern28033ae2017-11-07 21:59:40 -0800540 else if (nla_attr_len[p->type])
541 len += nla_total_size(nla_attr_len[p->type]);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100542 else if (nla_attr_minlen[p->type])
543 len += nla_total_size(nla_attr_minlen[p->type]);
544 }
545
546 return len;
547}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700548EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100549
550/**
Johannes Berg8cb08172019-04-26 14:07:28 +0200551 * __nla_parse - Parse a stream of attributes into a tb buffer
Thomas Grafbfa83a92005-11-10 02:25:51 +0100552 * @tb: destination array with maxtype+1 elements
553 * @maxtype: maximum attribute type to be expected
554 * @head: head of attribute stream
555 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700556 * @policy: validation policy
Johannes Berg8cb08172019-04-26 14:07:28 +0200557 * @validate: validation strictness
558 * @extack: extended ACK pointer
Thomas Grafbfa83a92005-11-10 02:25:51 +0100559 *
560 * Parses a stream of attributes and stores a pointer to each attribute in
Johannes Berg8cb08172019-04-26 14:07:28 +0200561 * the tb array accessible via the attribute type.
562 * Validation is controlled by the @validate parameter.
Thomas Grafbfa83a92005-11-10 02:25:51 +0100563 *
564 * Returns 0 on success or a negative error code.
565 */
Johannes Berg8cb08172019-04-26 14:07:28 +0200566int __nla_parse(struct nlattr **tb, int maxtype,
567 const struct nlattr *head, int len,
568 const struct nla_policy *policy, unsigned int validate,
569 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100570{
Johannes Berg8cb08172019-04-26 14:07:28 +0200571 return __nla_validate_parse(head, len, maxtype, policy, validate,
Johannes Berg7690aa12020-04-30 22:13:06 +0200572 extack, tb, 0);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100573}
Johannes Berg8cb08172019-04-26 14:07:28 +0200574EXPORT_SYMBOL(__nla_parse);
David Aherna5f6cba2018-10-07 20:16:25 -0700575
Thomas Grafbfa83a92005-11-10 02:25:51 +0100576/**
577 * nla_find - Find a specific attribute in a stream of attributes
578 * @head: head of attribute stream
579 * @len: length of attribute stream
580 * @attrtype: type of attribute to look for
581 *
582 * Returns the first attribute in the stream matching the specified type.
583 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800584struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100585{
Jan Engelhardt36546542010-11-16 09:52:32 -0800586 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100587 int rem;
588
589 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200590 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800591 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100592
593 return NULL;
594}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700595EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100596
597/**
598 * nla_strlcpy - Copy string attribute payload into a sized buffer
599 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700600 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100601 * @dstsize: size of destination buffer
602 *
603 * Copies at most dstsize - 1 bytes into the destination buffer.
604 * The result is always a valid NUL-terminated string. Unlike
605 * strlcpy the destination buffer is always padded out.
606 *
607 * Returns the length of the source buffer.
608 */
609size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
610{
611 size_t srclen = nla_len(nla);
612 char *src = nla_data(nla);
613
614 if (srclen > 0 && src[srclen - 1] == '\0')
615 srclen--;
616
617 if (dstsize > 0) {
618 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
619
620 memset(dst, 0, dstsize);
621 memcpy(dst, src, len);
622 }
623
624 return srclen;
625}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700626EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100627
628/**
Phil Sutter2cf0c8b2017-07-27 16:56:40 +0200629 * nla_strdup - Copy string attribute payload into a newly allocated buffer
630 * @nla: attribute to copy the string from
631 * @flags: the type of memory to allocate (see kmalloc).
632 *
633 * Returns a pointer to the allocated buffer or NULL on error.
634 */
635char *nla_strdup(const struct nlattr *nla, gfp_t flags)
636{
637 size_t srclen = nla_len(nla);
638 char *src = nla_data(nla), *dst;
639
640 if (srclen > 0 && src[srclen - 1] == '\0')
641 srclen--;
642
643 dst = kmalloc(srclen + 1, flags);
644 if (dst != NULL) {
645 memcpy(dst, src, srclen);
646 dst[srclen] = '\0';
647 }
648 return dst;
649}
650EXPORT_SYMBOL(nla_strdup);
651
652/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100653 * nla_memcpy - Copy a netlink attribute into another memory area
654 * @dest: where to copy to memcpy
655 * @src: netlink attribute to copy from
656 * @count: size of the destination area
657 *
658 * Note: The number of bytes copied is limited by the length of
659 * attribute's payload. memcpy
660 *
661 * Returns the number of bytes copied.
662 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700663int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100664{
665 int minlen = min_t(int, count, nla_len(src));
666
667 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200668 if (count > minlen)
669 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100670
671 return minlen;
672}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700673EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100674
675/**
676 * nla_memcmp - Compare an attribute with sized memory area
677 * @nla: netlink attribute
678 * @data: memory area
679 * @size: size of memory area
680 */
681int nla_memcmp(const struct nlattr *nla, const void *data,
682 size_t size)
683{
684 int d = nla_len(nla) - size;
685
686 if (d == 0)
687 d = memcmp(nla_data(nla), data, size);
688
689 return d;
690}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700691EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100692
693/**
694 * nla_strcmp - Compare a string attribute against a string
695 * @nla: netlink string attribute
696 * @str: another string
697 */
698int nla_strcmp(const struct nlattr *nla, const char *str)
699{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200700 int len = strlen(str);
701 char *buf = nla_data(nla);
702 int attrlen = nla_len(nla);
703 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100704
Pablo Neira8b7b9322014-04-01 19:38:44 +0200705 if (attrlen > 0 && buf[attrlen - 1] == '\0')
706 attrlen--;
707
708 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100709 if (d == 0)
710 d = memcmp(nla_data(nla), str, len);
711
712 return d;
713}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700714EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100715
Herbert Xu90800212009-03-11 23:18:32 +0800716#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100717/**
718 * __nla_reserve - reserve room for attribute on the skb
719 * @skb: socket buffer to reserve room on
720 * @attrtype: attribute type
721 * @attrlen: length of attribute payload
722 *
723 * Adds a netlink attribute header to a socket buffer and reserves
724 * room for the payload but does not copy it.
725 *
726 * The caller is responsible to ensure that the skb provides enough
727 * tailroom for the attribute header and payload.
728 */
729struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
730{
731 struct nlattr *nla;
732
Johannes Berg4df864c2017-06-16 14:29:21 +0200733 nla = skb_put(skb, nla_total_size(attrlen));
Thomas Grafbfa83a92005-11-10 02:25:51 +0100734 nla->nla_type = attrtype;
735 nla->nla_len = nla_attr_size(attrlen);
736
737 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
738
739 return nla;
740}
Herbert Xu90800212009-03-11 23:18:32 +0800741EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100742
743/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200744 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
745 * @skb: socket buffer to reserve room on
746 * @attrtype: attribute type
747 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200748 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200749 *
750 * Adds a netlink attribute header to a socket buffer and reserves
751 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200752 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200753 *
754 * The caller is responsible to ensure that the skb provides enough
755 * tailroom for the attribute header and payload.
756 */
757struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
758 int attrlen, int padattr)
759{
760 if (nla_need_padding_for_64bit(skb))
761 nla_align_64bit(skb, padattr);
762
763 return __nla_reserve(skb, attrtype, attrlen);
764}
765EXPORT_SYMBOL(__nla_reserve_64bit);
766
767/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700768 * __nla_reserve_nohdr - reserve room for attribute without header
769 * @skb: socket buffer to reserve room on
770 * @attrlen: length of attribute payload
771 *
772 * Reserves room for attribute payload without a header.
773 *
774 * The caller is responsible to ensure that the skb provides enough
775 * tailroom for the payload.
776 */
777void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
778{
yuan linyub952f4d2017-06-18 22:52:04 +0800779 return skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700780}
Herbert Xu90800212009-03-11 23:18:32 +0800781EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700782
783/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100784 * nla_reserve - reserve room for attribute on the skb
785 * @skb: socket buffer to reserve room on
786 * @attrtype: attribute type
787 * @attrlen: length of attribute payload
788 *
789 * Adds a netlink attribute header to a socket buffer and reserves
790 * room for the payload but does not copy it.
791 *
792 * Returns NULL if the tailroom of the skb is insufficient to store
793 * the attribute header and payload.
794 */
795struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
796{
797 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
798 return NULL;
799
800 return __nla_reserve(skb, attrtype, attrlen);
801}
Herbert Xu90800212009-03-11 23:18:32 +0800802EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100803
804/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200805 * nla_reserve_64bit - reserve room for attribute on the skb and align it
806 * @skb: socket buffer to reserve room on
807 * @attrtype: attribute type
808 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200809 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200810 *
811 * Adds a netlink attribute header to a socket buffer and reserves
812 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200813 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200814 *
815 * Returns NULL if the tailroom of the skb is insufficient to store
816 * the attribute header and payload.
817 */
818struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
819 int padattr)
820{
821 size_t len;
822
823 if (nla_need_padding_for_64bit(skb))
824 len = nla_total_size_64bit(attrlen);
825 else
826 len = nla_total_size(attrlen);
827 if (unlikely(skb_tailroom(skb) < len))
828 return NULL;
829
830 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
831}
832EXPORT_SYMBOL(nla_reserve_64bit);
833
834/**
Julius Volz10b595a2008-06-27 20:02:14 -0700835 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700836 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700837 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700838 *
839 * Reserves room for attribute payload without a header.
840 *
841 * Returns NULL if the tailroom of the skb is insufficient to store
842 * the attribute payload.
843 */
844void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
845{
846 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
847 return NULL;
848
849 return __nla_reserve_nohdr(skb, attrlen);
850}
Herbert Xu90800212009-03-11 23:18:32 +0800851EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700852
853/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100854 * __nla_put - Add a netlink attribute to a socket buffer
855 * @skb: socket buffer to add attribute to
856 * @attrtype: attribute type
857 * @attrlen: length of attribute payload
858 * @data: head of attribute payload
859 *
860 * The caller is responsible to ensure that the skb provides enough
861 * tailroom for the attribute header and payload.
862 */
863void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
864 const void *data)
865{
866 struct nlattr *nla;
867
868 nla = __nla_reserve(skb, attrtype, attrlen);
869 memcpy(nla_data(nla), data, attrlen);
870}
Herbert Xu90800212009-03-11 23:18:32 +0800871EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100872
Thomas Graffe4944e2006-08-04 23:03:05 -0700873/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200874 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
875 * @skb: socket buffer to add attribute to
876 * @attrtype: attribute type
877 * @attrlen: length of attribute payload
878 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200879 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200880 *
881 * The caller is responsible to ensure that the skb provides enough
882 * tailroom for the attribute header and payload.
883 */
884void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
885 const void *data, int padattr)
886{
887 struct nlattr *nla;
888
889 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
890 memcpy(nla_data(nla), data, attrlen);
891}
892EXPORT_SYMBOL(__nla_put_64bit);
893
894/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700895 * __nla_put_nohdr - Add a netlink attribute without header
896 * @skb: socket buffer to add attribute to
897 * @attrlen: length of attribute payload
898 * @data: head of attribute payload
899 *
900 * The caller is responsible to ensure that the skb provides enough
901 * tailroom for the attribute payload.
902 */
903void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
904{
905 void *start;
906
907 start = __nla_reserve_nohdr(skb, attrlen);
908 memcpy(start, data, attrlen);
909}
Herbert Xu90800212009-03-11 23:18:32 +0800910EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100911
912/**
913 * nla_put - Add a netlink attribute to a socket buffer
914 * @skb: socket buffer to add attribute to
915 * @attrtype: attribute type
916 * @attrlen: length of attribute payload
917 * @data: head of attribute payload
918 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700919 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100920 * the attribute header and payload.
921 */
922int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
923{
924 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700925 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100926
927 __nla_put(skb, attrtype, attrlen, data);
928 return 0;
929}
Herbert Xu90800212009-03-11 23:18:32 +0800930EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100931
Thomas Graffe4944e2006-08-04 23:03:05 -0700932/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200933 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
934 * @skb: socket buffer to add attribute to
935 * @attrtype: attribute type
936 * @attrlen: length of attribute payload
937 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200938 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200939 *
940 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
941 * the attribute header and payload.
942 */
943int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
944 const void *data, int padattr)
945{
946 size_t len;
947
948 if (nla_need_padding_for_64bit(skb))
949 len = nla_total_size_64bit(attrlen);
950 else
951 len = nla_total_size(attrlen);
952 if (unlikely(skb_tailroom(skb) < len))
953 return -EMSGSIZE;
954
955 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
956 return 0;
957}
958EXPORT_SYMBOL(nla_put_64bit);
959
960/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700961 * nla_put_nohdr - Add a netlink attribute without header
962 * @skb: socket buffer to add attribute to
963 * @attrlen: length of attribute payload
964 * @data: head of attribute payload
965 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700966 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700967 * the attribute payload.
968 */
969int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
970{
971 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700972 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700973
974 __nla_put_nohdr(skb, attrlen, data);
975 return 0;
976}
Herbert Xu90800212009-03-11 23:18:32 +0800977EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100978
Patrick McHardy01480e12008-01-22 22:10:59 -0800979/**
980 * nla_append - Add a netlink attribute without header or padding
981 * @skb: socket buffer to add attribute to
982 * @attrlen: length of attribute payload
983 * @data: head of attribute payload
984 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700985 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800986 * the attribute payload.
987 */
988int nla_append(struct sk_buff *skb, int attrlen, const void *data)
989{
990 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700991 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800992
Johannes Berg59ae1d12017-06-16 14:29:20 +0200993 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800994 return 0;
995}
Herbert Xu90800212009-03-11 23:18:32 +0800996EXPORT_SYMBOL(nla_append);
997#endif