blob: af0f8b0309c663dec8cbc32cf9c72873b40c7e12 [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
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -040047static int validate_nla_bitfield32(const struct nlattr *nla,
Johannes Berg48fde902018-09-26 11:15:31 +020048 const u32 *valid_flags_mask)
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -040049{
50 const struct nla_bitfield32 *bf = nla_data(nla);
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -040051
Johannes Berg48fde902018-09-26 11:15:31 +020052 if (!valid_flags_mask)
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -040053 return -EINVAL;
54
55 /*disallow invalid bit selector */
56 if (bf->selector & ~*valid_flags_mask)
57 return -EINVAL;
58
59 /*disallow invalid bit values */
60 if (bf->value & ~*valid_flags_mask)
61 return -EINVAL;
62
63 /*disallow valid bit values that are not selected*/
64 if (bf->value & ~bf->selector)
65 return -EINVAL;
66
67 return 0;
68}
69
Johannes Berg1501d132018-09-26 11:15:34 +020070static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
71 const struct nla_policy *policy,
Johannes Berg8cb08172019-04-26 14:07:28 +020072 struct netlink_ext_ack *extack,
73 unsigned int validate)
Johannes Berg1501d132018-09-26 11:15:34 +020074{
75 const struct nlattr *entry;
76 int rem;
77
78 nla_for_each_attr(entry, head, len, rem) {
79 int ret;
80
81 if (nla_len(entry) == 0)
82 continue;
83
84 if (nla_len(entry) < NLA_HDRLEN) {
85 NL_SET_ERR_MSG_ATTR(extack, entry,
86 "Array element too short");
87 return -ERANGE;
88 }
89
Johannes Berg8cb08172019-04-26 14:07:28 +020090 ret = __nla_validate(nla_data(entry), nla_len(entry),
91 maxtype, policy, validate, extack);
Johannes Berg1501d132018-09-26 11:15:34 +020092 if (ret < 0)
93 return ret;
94 }
95
96 return 0;
97}
98
Johannes Berg3e48be02018-09-27 11:28:35 +020099static int nla_validate_int_range(const struct nla_policy *pt,
100 const struct nlattr *nla,
101 struct netlink_ext_ack *extack)
102{
103 bool validate_min, validate_max;
104 s64 value;
105
106 validate_min = pt->validation_type == NLA_VALIDATE_RANGE ||
107 pt->validation_type == NLA_VALIDATE_MIN;
108 validate_max = pt->validation_type == NLA_VALIDATE_RANGE ||
109 pt->validation_type == NLA_VALIDATE_MAX;
110
111 switch (pt->type) {
112 case NLA_U8:
113 value = nla_get_u8(nla);
114 break;
115 case NLA_U16:
116 value = nla_get_u16(nla);
117 break;
118 case NLA_U32:
119 value = nla_get_u32(nla);
120 break;
121 case NLA_S8:
122 value = nla_get_s8(nla);
123 break;
124 case NLA_S16:
125 value = nla_get_s16(nla);
126 break;
127 case NLA_S32:
128 value = nla_get_s32(nla);
129 break;
130 case NLA_S64:
131 value = nla_get_s64(nla);
132 break;
133 case NLA_U64:
134 /* treat this one specially, since it may not fit into s64 */
135 if ((validate_min && nla_get_u64(nla) < pt->min) ||
136 (validate_max && nla_get_u64(nla) > pt->max)) {
137 NL_SET_ERR_MSG_ATTR(extack, nla,
138 "integer out of range");
139 return -ERANGE;
140 }
141 return 0;
142 default:
143 WARN_ON(1);
144 return -EINVAL;
145 }
146
147 if ((validate_min && value < pt->min) ||
148 (validate_max && value > pt->max)) {
149 NL_SET_ERR_MSG_ATTR(extack, nla,
150 "integer out of range");
151 return -ERANGE;
152 }
153
154 return 0;
155}
156
Jan Engelhardt36546542010-11-16 09:52:32 -0800157static int validate_nla(const struct nlattr *nla, int maxtype,
Johannes Berg8cb08172019-04-26 14:07:28 +0200158 const struct nla_policy *policy, unsigned int validate,
Johannes Bergc29f1842018-09-26 11:15:32 +0200159 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100160{
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700161 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200162 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Johannes Bergc29f1842018-09-26 11:15:32 +0200163 int err = -ERANGE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100164
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200165 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100166 return 0;
167
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200168 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +0100169
170 BUG_ON(pt->type > NLA_TYPE_MAX);
171
Johannes Bergb60b87f2018-09-17 11:57:29 +0200172 if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) ||
173 (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) {
David Ahern6e237d02017-12-06 20:09:12 -0800174 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
175 current->comm, type);
Johannes Berg8cb08172019-04-26 14:07:28 +0200176 if (validate & NL_VALIDATE_STRICT_ATTRS) {
177 NL_SET_ERR_MSG_ATTR(extack, nla,
178 "invalid attribute length");
179 return -EINVAL;
180 }
David Ahern28033ae2017-11-07 21:59:40 -0800181 }
182
Thomas Grafa5531a52006-08-26 20:11:47 -0700183 switch (pt->type) {
Johannes Bergb60b87f2018-09-17 11:57:29 +0200184 case NLA_EXACT_LEN:
185 if (attrlen != pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200186 goto out_err;
Johannes Bergb60b87f2018-09-17 11:57:29 +0200187 break;
188
Johannes Berg568b7422018-09-17 11:57:28 +0200189 case NLA_REJECT:
Johannes Bergc29f1842018-09-26 11:15:32 +0200190 if (extack && pt->validation_data) {
191 NL_SET_BAD_ATTR(extack, nla);
192 extack->_msg = pt->validation_data;
193 return -EINVAL;
194 }
195 err = -EINVAL;
196 goto out_err;
Johannes Berg568b7422018-09-17 11:57:28 +0200197
Thomas Grafa5531a52006-08-26 20:11:47 -0700198 case NLA_FLAG:
199 if (attrlen > 0)
Johannes Bergc29f1842018-09-26 11:15:32 +0200200 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700201 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100202
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400203 case NLA_BITFIELD32:
204 if (attrlen != sizeof(struct nla_bitfield32))
Johannes Bergc29f1842018-09-26 11:15:32 +0200205 goto out_err;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400206
Johannes Bergc29f1842018-09-26 11:15:32 +0200207 err = validate_nla_bitfield32(nla, pt->validation_data);
208 if (err)
209 goto out_err;
210 break;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400211
Thomas Grafa5531a52006-08-26 20:11:47 -0700212 case NLA_NUL_STRING:
213 if (pt->len)
214 minlen = min_t(int, attrlen, pt->len + 1);
215 else
216 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100217
Johannes Bergc29f1842018-09-26 11:15:32 +0200218 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
219 err = -EINVAL;
220 goto out_err;
221 }
Thomas Grafa5531a52006-08-26 20:11:47 -0700222 /* fall through */
223
224 case NLA_STRING:
225 if (attrlen < 1)
Johannes Bergc29f1842018-09-26 11:15:32 +0200226 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700227
228 if (pt->len) {
229 char *buf = nla_data(nla);
230
231 if (buf[attrlen - 1] == '\0')
232 attrlen--;
233
234 if (attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200235 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700236 }
237 break;
238
Johannes Bergd30045a2007-03-23 11:37:48 -0700239 case NLA_BINARY:
240 if (pt->len && attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200241 goto out_err;
Johannes Bergd30045a2007-03-23 11:37:48 -0700242 break;
243
Patrick McHardyea5693c2008-11-28 03:05:19 -0800244 case NLA_NESTED:
245 /* a nested attributes is allowed to be empty; if its not,
246 * it must have a size of at least NLA_HDRLEN.
247 */
248 if (attrlen == 0)
249 break;
Johannes Berg9a659a352018-09-26 11:15:33 +0200250 if (attrlen < NLA_HDRLEN)
251 goto out_err;
252 if (pt->validation_data) {
Johannes Berg8cb08172019-04-26 14:07:28 +0200253 err = __nla_validate(nla_data(nla), nla_len(nla), pt->len,
254 pt->validation_data, validate,
255 extack);
Johannes Berg9a659a352018-09-26 11:15:33 +0200256 if (err < 0) {
257 /*
258 * return directly to preserve the inner
259 * error message/attribute pointer
260 */
261 return err;
262 }
263 }
264 break;
Johannes Berg1501d132018-09-26 11:15:34 +0200265 case NLA_NESTED_ARRAY:
266 /* a nested array attribute is allowed to be empty; if its not,
267 * it must have a size of at least NLA_HDRLEN.
268 */
269 if (attrlen == 0)
270 break;
271 if (attrlen < NLA_HDRLEN)
272 goto out_err;
273 if (pt->validation_data) {
274 int err;
275
276 err = nla_validate_array(nla_data(nla), nla_len(nla),
277 pt->len, pt->validation_data,
Johannes Berg8cb08172019-04-26 14:07:28 +0200278 extack, validate);
Johannes Berg1501d132018-09-26 11:15:34 +0200279 if (err < 0) {
280 /*
281 * return directly to preserve the inner
282 * error message/attribute pointer
283 */
284 return err;
285 }
286 }
287 break;
Johannes Berg6f455f52019-04-26 14:07:27 +0200288
289 case NLA_UNSPEC:
Johannes Berg8cb08172019-04-26 14:07:28 +0200290 if (validate & NL_VALIDATE_UNSPEC) {
291 NL_SET_ERR_MSG_ATTR(extack, nla,
292 "Unsupported attribute");
293 return -EINVAL;
294 }
295 /* fall through */
Johannes Berg6f455f52019-04-26 14:07:27 +0200296 case NLA_MIN_LEN:
297 if (attrlen < pt->len)
298 goto out_err;
299 break;
300
Thomas Grafa5531a52006-08-26 20:11:47 -0700301 default:
302 if (pt->len)
303 minlen = pt->len;
Johannes Berg6f455f52019-04-26 14:07:27 +0200304 else
Thomas Grafa5531a52006-08-26 20:11:47 -0700305 minlen = nla_attr_minlen[pt->type];
306
307 if (attrlen < minlen)
Johannes Bergc29f1842018-09-26 11:15:32 +0200308 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700309 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100310
Johannes Berg3e48be02018-09-27 11:28:35 +0200311 /* further validation */
312 switch (pt->validation_type) {
313 case NLA_VALIDATE_NONE:
314 /* nothing to do */
315 break;
316 case NLA_VALIDATE_RANGE:
317 case NLA_VALIDATE_MIN:
318 case NLA_VALIDATE_MAX:
319 err = nla_validate_int_range(pt, nla, extack);
320 if (err)
321 return err;
322 break;
Johannes Berg33188bd2018-09-27 11:28:36 +0200323 case NLA_VALIDATE_FUNCTION:
324 if (pt->validate) {
325 err = pt->validate(nla, extack);
326 if (err)
327 return err;
328 }
329 break;
Johannes Berg3e48be02018-09-27 11:28:35 +0200330 }
331
Thomas Grafbfa83a92005-11-10 02:25:51 +0100332 return 0;
Johannes Bergc29f1842018-09-26 11:15:32 +0200333out_err:
334 NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation");
335 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100336}
337
Johannes Berg8cb08172019-04-26 14:07:28 +0200338static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
339 const struct nla_policy *policy,
340 unsigned int validate,
341 struct netlink_ext_ack *extack,
342 struct nlattr **tb)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100343{
Jan Engelhardt36546542010-11-16 09:52:32 -0800344 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200345 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100346
Johannes Berg8cb08172019-04-26 14:07:28 +0200347 if (tb)
348 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
Johannes Bergfceb6432017-04-12 14:34:07 +0200349
Johannes Berg8cb08172019-04-26 14:07:28 +0200350 nla_for_each_attr(nla, head, len, rem) {
351 u16 type = nla_type(nla);
352
353 if (type == 0 || type > maxtype) {
354 if (validate & NL_VALIDATE_MAXTYPE) {
355 NL_SET_ERR_MSG(extack, "Unknown attribute type");
356 return -EINVAL;
357 }
358 continue;
359 }
360 if (policy) {
361 int err = validate_nla(nla, maxtype, policy,
362 validate, extack);
363
364 if (err < 0)
365 return err;
366 }
367
368 if (tb)
369 tb[type] = (struct nlattr *)nla;
370 }
371
372 if (unlikely(rem > 0)) {
373 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
374 rem, current->comm);
375 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
376 if (validate & NL_VALIDATE_TRAILING)
377 return -EINVAL;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100378 }
379
Johannes Bergfceb6432017-04-12 14:34:07 +0200380 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100381}
Johannes Berg8cb08172019-04-26 14:07:28 +0200382
383/**
384 * __nla_validate - Validate a stream of attributes
385 * @head: head of attribute stream
386 * @len: length of attribute stream
387 * @maxtype: maximum attribute type to be expected
388 * @policy: validation policy
389 * @validate: validation strictness
390 * @extack: extended ACK report struct
391 *
392 * Validates all attributes in the specified attribute stream against the
393 * specified policy. Validation depends on the validate flags passed, see
394 * &enum netlink_validation for more details on that.
395 * See documenation of struct nla_policy for more details.
396 *
397 * Returns 0 on success or a negative error code.
398 */
399int __nla_validate(const struct nlattr *head, int len, int maxtype,
400 const struct nla_policy *policy, unsigned int validate,
401 struct netlink_ext_ack *extack)
402{
403 return __nla_validate_parse(head, len, maxtype, policy, validate,
404 extack, NULL);
405}
406EXPORT_SYMBOL(__nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100407
408/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100409 * nla_policy_len - Determin the max. length of a policy
410 * @policy: policy to use
411 * @n: number of policies
412 *
413 * Determines the max. length of the policy. It is currently used
414 * to allocated Netlink buffers roughly the size of the actual
415 * message.
416 *
417 * Returns 0 on success or a negative error code.
418 */
419int
420nla_policy_len(const struct nla_policy *p, int n)
421{
422 int i, len = 0;
423
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800424 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100425 if (p->len)
426 len += nla_total_size(p->len);
David Ahern28033ae2017-11-07 21:59:40 -0800427 else if (nla_attr_len[p->type])
428 len += nla_total_size(nla_attr_len[p->type]);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100429 else if (nla_attr_minlen[p->type])
430 len += nla_total_size(nla_attr_minlen[p->type]);
431 }
432
433 return len;
434}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700435EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100436
437/**
Johannes Berg8cb08172019-04-26 14:07:28 +0200438 * __nla_parse - Parse a stream of attributes into a tb buffer
Thomas Grafbfa83a92005-11-10 02:25:51 +0100439 * @tb: destination array with maxtype+1 elements
440 * @maxtype: maximum attribute type to be expected
441 * @head: head of attribute stream
442 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700443 * @policy: validation policy
Johannes Berg8cb08172019-04-26 14:07:28 +0200444 * @validate: validation strictness
445 * @extack: extended ACK pointer
Thomas Grafbfa83a92005-11-10 02:25:51 +0100446 *
447 * Parses a stream of attributes and stores a pointer to each attribute in
Johannes Berg8cb08172019-04-26 14:07:28 +0200448 * the tb array accessible via the attribute type.
449 * Validation is controlled by the @validate parameter.
Thomas Grafbfa83a92005-11-10 02:25:51 +0100450 *
451 * Returns 0 on success or a negative error code.
452 */
Johannes Berg8cb08172019-04-26 14:07:28 +0200453int __nla_parse(struct nlattr **tb, int maxtype,
454 const struct nlattr *head, int len,
455 const struct nla_policy *policy, unsigned int validate,
456 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100457{
Johannes Berg8cb08172019-04-26 14:07:28 +0200458 return __nla_validate_parse(head, len, maxtype, policy, validate,
459 extack, tb);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100460}
Johannes Berg8cb08172019-04-26 14:07:28 +0200461EXPORT_SYMBOL(__nla_parse);
David Aherna5f6cba2018-10-07 20:16:25 -0700462
Thomas Grafbfa83a92005-11-10 02:25:51 +0100463/**
464 * nla_find - Find a specific attribute in a stream of attributes
465 * @head: head of attribute stream
466 * @len: length of attribute stream
467 * @attrtype: type of attribute to look for
468 *
469 * Returns the first attribute in the stream matching the specified type.
470 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800471struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100472{
Jan Engelhardt36546542010-11-16 09:52:32 -0800473 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100474 int rem;
475
476 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200477 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800478 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100479
480 return NULL;
481}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700482EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100483
484/**
485 * nla_strlcpy - Copy string attribute payload into a sized buffer
486 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700487 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100488 * @dstsize: size of destination buffer
489 *
490 * Copies at most dstsize - 1 bytes into the destination buffer.
491 * The result is always a valid NUL-terminated string. Unlike
492 * strlcpy the destination buffer is always padded out.
493 *
494 * Returns the length of the source buffer.
495 */
496size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
497{
498 size_t srclen = nla_len(nla);
499 char *src = nla_data(nla);
500
501 if (srclen > 0 && src[srclen - 1] == '\0')
502 srclen--;
503
504 if (dstsize > 0) {
505 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
506
507 memset(dst, 0, dstsize);
508 memcpy(dst, src, len);
509 }
510
511 return srclen;
512}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700513EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100514
515/**
Phil Sutter2cf0c8b2017-07-27 16:56:40 +0200516 * nla_strdup - Copy string attribute payload into a newly allocated buffer
517 * @nla: attribute to copy the string from
518 * @flags: the type of memory to allocate (see kmalloc).
519 *
520 * Returns a pointer to the allocated buffer or NULL on error.
521 */
522char *nla_strdup(const struct nlattr *nla, gfp_t flags)
523{
524 size_t srclen = nla_len(nla);
525 char *src = nla_data(nla), *dst;
526
527 if (srclen > 0 && src[srclen - 1] == '\0')
528 srclen--;
529
530 dst = kmalloc(srclen + 1, flags);
531 if (dst != NULL) {
532 memcpy(dst, src, srclen);
533 dst[srclen] = '\0';
534 }
535 return dst;
536}
537EXPORT_SYMBOL(nla_strdup);
538
539/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100540 * nla_memcpy - Copy a netlink attribute into another memory area
541 * @dest: where to copy to memcpy
542 * @src: netlink attribute to copy from
543 * @count: size of the destination area
544 *
545 * Note: The number of bytes copied is limited by the length of
546 * attribute's payload. memcpy
547 *
548 * Returns the number of bytes copied.
549 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700550int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100551{
552 int minlen = min_t(int, count, nla_len(src));
553
554 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200555 if (count > minlen)
556 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100557
558 return minlen;
559}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700560EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100561
562/**
563 * nla_memcmp - Compare an attribute with sized memory area
564 * @nla: netlink attribute
565 * @data: memory area
566 * @size: size of memory area
567 */
568int nla_memcmp(const struct nlattr *nla, const void *data,
569 size_t size)
570{
571 int d = nla_len(nla) - size;
572
573 if (d == 0)
574 d = memcmp(nla_data(nla), data, size);
575
576 return d;
577}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700578EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100579
580/**
581 * nla_strcmp - Compare a string attribute against a string
582 * @nla: netlink string attribute
583 * @str: another string
584 */
585int nla_strcmp(const struct nlattr *nla, const char *str)
586{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200587 int len = strlen(str);
588 char *buf = nla_data(nla);
589 int attrlen = nla_len(nla);
590 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100591
Pablo Neira8b7b9322014-04-01 19:38:44 +0200592 if (attrlen > 0 && buf[attrlen - 1] == '\0')
593 attrlen--;
594
595 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100596 if (d == 0)
597 d = memcmp(nla_data(nla), str, len);
598
599 return d;
600}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700601EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100602
Herbert Xu90800212009-03-11 23:18:32 +0800603#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100604/**
605 * __nla_reserve - reserve room for attribute on the skb
606 * @skb: socket buffer to reserve room on
607 * @attrtype: attribute type
608 * @attrlen: length of attribute payload
609 *
610 * Adds a netlink attribute header to a socket buffer and reserves
611 * room for the payload but does not copy it.
612 *
613 * The caller is responsible to ensure that the skb provides enough
614 * tailroom for the attribute header and payload.
615 */
616struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
617{
618 struct nlattr *nla;
619
Johannes Berg4df864c2017-06-16 14:29:21 +0200620 nla = skb_put(skb, nla_total_size(attrlen));
Thomas Grafbfa83a92005-11-10 02:25:51 +0100621 nla->nla_type = attrtype;
622 nla->nla_len = nla_attr_size(attrlen);
623
624 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
625
626 return nla;
627}
Herbert Xu90800212009-03-11 23:18:32 +0800628EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100629
630/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200631 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
632 * @skb: socket buffer to reserve room on
633 * @attrtype: attribute type
634 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200635 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200636 *
637 * Adds a netlink attribute header to a socket buffer and reserves
638 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200639 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200640 *
641 * The caller is responsible to ensure that the skb provides enough
642 * tailroom for the attribute header and payload.
643 */
644struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
645 int attrlen, int padattr)
646{
647 if (nla_need_padding_for_64bit(skb))
648 nla_align_64bit(skb, padattr);
649
650 return __nla_reserve(skb, attrtype, attrlen);
651}
652EXPORT_SYMBOL(__nla_reserve_64bit);
653
654/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700655 * __nla_reserve_nohdr - reserve room for attribute without header
656 * @skb: socket buffer to reserve room on
657 * @attrlen: length of attribute payload
658 *
659 * Reserves room for attribute payload without a header.
660 *
661 * The caller is responsible to ensure that the skb provides enough
662 * tailroom for the payload.
663 */
664void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
665{
yuan linyub952f4d2017-06-18 22:52:04 +0800666 return skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700667}
Herbert Xu90800212009-03-11 23:18:32 +0800668EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700669
670/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100671 * nla_reserve - reserve room for attribute on the skb
672 * @skb: socket buffer to reserve room on
673 * @attrtype: attribute type
674 * @attrlen: length of attribute payload
675 *
676 * Adds a netlink attribute header to a socket buffer and reserves
677 * room for the payload but does not copy it.
678 *
679 * Returns NULL if the tailroom of the skb is insufficient to store
680 * the attribute header and payload.
681 */
682struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
683{
684 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
685 return NULL;
686
687 return __nla_reserve(skb, attrtype, attrlen);
688}
Herbert Xu90800212009-03-11 23:18:32 +0800689EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100690
691/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200692 * nla_reserve_64bit - reserve room for attribute on the skb and align it
693 * @skb: socket buffer to reserve room on
694 * @attrtype: attribute type
695 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200696 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200697 *
698 * Adds a netlink attribute header to a socket buffer and reserves
699 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200700 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200701 *
702 * Returns NULL if the tailroom of the skb is insufficient to store
703 * the attribute header and payload.
704 */
705struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
706 int padattr)
707{
708 size_t len;
709
710 if (nla_need_padding_for_64bit(skb))
711 len = nla_total_size_64bit(attrlen);
712 else
713 len = nla_total_size(attrlen);
714 if (unlikely(skb_tailroom(skb) < len))
715 return NULL;
716
717 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
718}
719EXPORT_SYMBOL(nla_reserve_64bit);
720
721/**
Julius Volz10b595a2008-06-27 20:02:14 -0700722 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700723 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700724 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700725 *
726 * Reserves room for attribute payload without a header.
727 *
728 * Returns NULL if the tailroom of the skb is insufficient to store
729 * the attribute payload.
730 */
731void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
732{
733 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
734 return NULL;
735
736 return __nla_reserve_nohdr(skb, attrlen);
737}
Herbert Xu90800212009-03-11 23:18:32 +0800738EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700739
740/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100741 * __nla_put - Add a netlink attribute to a socket buffer
742 * @skb: socket buffer to add attribute to
743 * @attrtype: attribute type
744 * @attrlen: length of attribute payload
745 * @data: head of attribute payload
746 *
747 * The caller is responsible to ensure that the skb provides enough
748 * tailroom for the attribute header and payload.
749 */
750void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
751 const void *data)
752{
753 struct nlattr *nla;
754
755 nla = __nla_reserve(skb, attrtype, attrlen);
756 memcpy(nla_data(nla), data, attrlen);
757}
Herbert Xu90800212009-03-11 23:18:32 +0800758EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100759
Thomas Graffe4944e2006-08-04 23:03:05 -0700760/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200761 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
762 * @skb: socket buffer to add attribute to
763 * @attrtype: attribute type
764 * @attrlen: length of attribute payload
765 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200766 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200767 *
768 * The caller is responsible to ensure that the skb provides enough
769 * tailroom for the attribute header and payload.
770 */
771void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
772 const void *data, int padattr)
773{
774 struct nlattr *nla;
775
776 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
777 memcpy(nla_data(nla), data, attrlen);
778}
779EXPORT_SYMBOL(__nla_put_64bit);
780
781/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700782 * __nla_put_nohdr - Add a netlink attribute without header
783 * @skb: socket buffer to add attribute to
784 * @attrlen: length of attribute payload
785 * @data: head of attribute payload
786 *
787 * The caller is responsible to ensure that the skb provides enough
788 * tailroom for the attribute payload.
789 */
790void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
791{
792 void *start;
793
794 start = __nla_reserve_nohdr(skb, attrlen);
795 memcpy(start, data, attrlen);
796}
Herbert Xu90800212009-03-11 23:18:32 +0800797EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100798
799/**
800 * nla_put - Add a netlink attribute to a socket buffer
801 * @skb: socket buffer to add attribute to
802 * @attrtype: attribute type
803 * @attrlen: length of attribute payload
804 * @data: head of attribute payload
805 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700806 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100807 * the attribute header and payload.
808 */
809int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
810{
811 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700812 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100813
814 __nla_put(skb, attrtype, attrlen, data);
815 return 0;
816}
Herbert Xu90800212009-03-11 23:18:32 +0800817EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100818
Thomas Graffe4944e2006-08-04 23:03:05 -0700819/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200820 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
821 * @skb: socket buffer to add attribute to
822 * @attrtype: attribute type
823 * @attrlen: length of attribute payload
824 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200825 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200826 *
827 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
828 * the attribute header and payload.
829 */
830int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
831 const void *data, int padattr)
832{
833 size_t len;
834
835 if (nla_need_padding_for_64bit(skb))
836 len = nla_total_size_64bit(attrlen);
837 else
838 len = nla_total_size(attrlen);
839 if (unlikely(skb_tailroom(skb) < len))
840 return -EMSGSIZE;
841
842 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
843 return 0;
844}
845EXPORT_SYMBOL(nla_put_64bit);
846
847/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700848 * nla_put_nohdr - Add a netlink attribute without header
849 * @skb: socket buffer to add attribute to
850 * @attrlen: length of attribute payload
851 * @data: head of attribute payload
852 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700853 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700854 * the attribute payload.
855 */
856int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
857{
858 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700859 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700860
861 __nla_put_nohdr(skb, attrlen, data);
862 return 0;
863}
Herbert Xu90800212009-03-11 23:18:32 +0800864EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100865
Patrick McHardy01480e12008-01-22 22:10:59 -0800866/**
867 * nla_append - Add a netlink attribute without header or padding
868 * @skb: socket buffer to add attribute to
869 * @attrlen: length of attribute payload
870 * @data: head of attribute payload
871 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700872 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800873 * the attribute payload.
874 */
875int nla_append(struct sk_buff *skb, int attrlen, const void *data)
876{
877 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700878 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800879
Johannes Berg59ae1d12017-06-16 14:29:20 +0200880 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800881 return 0;
882}
Herbert Xu90800212009-03-11 23:18:32 +0800883EXPORT_SYMBOL(nla_append);
884#endif