blob: d26de6156b97db73803cb617bcd81fd7415ac7b1 [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,
72 struct netlink_ext_ack *extack)
73{
74 const struct nlattr *entry;
75 int rem;
76
77 nla_for_each_attr(entry, head, len, rem) {
78 int ret;
79
80 if (nla_len(entry) == 0)
81 continue;
82
83 if (nla_len(entry) < NLA_HDRLEN) {
84 NL_SET_ERR_MSG_ATTR(extack, entry,
85 "Array element too short");
86 return -ERANGE;
87 }
88
89 ret = nla_validate(nla_data(entry), nla_len(entry),
90 maxtype, policy, extack);
91 if (ret < 0)
92 return ret;
93 }
94
95 return 0;
96}
97
Johannes Berg3e48be02018-09-27 11:28:35 +020098static int nla_validate_int_range(const struct nla_policy *pt,
99 const struct nlattr *nla,
100 struct netlink_ext_ack *extack)
101{
102 bool validate_min, validate_max;
103 s64 value;
104
105 validate_min = pt->validation_type == NLA_VALIDATE_RANGE ||
106 pt->validation_type == NLA_VALIDATE_MIN;
107 validate_max = pt->validation_type == NLA_VALIDATE_RANGE ||
108 pt->validation_type == NLA_VALIDATE_MAX;
109
110 switch (pt->type) {
111 case NLA_U8:
112 value = nla_get_u8(nla);
113 break;
114 case NLA_U16:
115 value = nla_get_u16(nla);
116 break;
117 case NLA_U32:
118 value = nla_get_u32(nla);
119 break;
120 case NLA_S8:
121 value = nla_get_s8(nla);
122 break;
123 case NLA_S16:
124 value = nla_get_s16(nla);
125 break;
126 case NLA_S32:
127 value = nla_get_s32(nla);
128 break;
129 case NLA_S64:
130 value = nla_get_s64(nla);
131 break;
132 case NLA_U64:
133 /* treat this one specially, since it may not fit into s64 */
134 if ((validate_min && nla_get_u64(nla) < pt->min) ||
135 (validate_max && nla_get_u64(nla) > pt->max)) {
136 NL_SET_ERR_MSG_ATTR(extack, nla,
137 "integer out of range");
138 return -ERANGE;
139 }
140 return 0;
141 default:
142 WARN_ON(1);
143 return -EINVAL;
144 }
145
146 if ((validate_min && value < pt->min) ||
147 (validate_max && value > pt->max)) {
148 NL_SET_ERR_MSG_ATTR(extack, nla,
149 "integer out of range");
150 return -ERANGE;
151 }
152
153 return 0;
154}
155
Jan Engelhardt36546542010-11-16 09:52:32 -0800156static int validate_nla(const struct nlattr *nla, int maxtype,
Johannes Berg568b7422018-09-17 11:57:28 +0200157 const struct nla_policy *policy,
Johannes Bergc29f1842018-09-26 11:15:32 +0200158 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100159{
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700160 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200161 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Johannes Bergc29f1842018-09-26 11:15:32 +0200162 int err = -ERANGE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100163
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200164 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100165 return 0;
166
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200167 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +0100168
169 BUG_ON(pt->type > NLA_TYPE_MAX);
170
Johannes Bergb60b87f2018-09-17 11:57:29 +0200171 if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) ||
172 (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) {
David Ahern6e237d02017-12-06 20:09:12 -0800173 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
174 current->comm, type);
David Ahern28033ae2017-11-07 21:59:40 -0800175 }
176
Thomas Grafa5531a52006-08-26 20:11:47 -0700177 switch (pt->type) {
Johannes Bergb60b87f2018-09-17 11:57:29 +0200178 case NLA_EXACT_LEN:
179 if (attrlen != pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200180 goto out_err;
Johannes Bergb60b87f2018-09-17 11:57:29 +0200181 break;
182
Johannes Berg568b7422018-09-17 11:57:28 +0200183 case NLA_REJECT:
Johannes Bergc29f1842018-09-26 11:15:32 +0200184 if (extack && pt->validation_data) {
185 NL_SET_BAD_ATTR(extack, nla);
186 extack->_msg = pt->validation_data;
187 return -EINVAL;
188 }
189 err = -EINVAL;
190 goto out_err;
Johannes Berg568b7422018-09-17 11:57:28 +0200191
Thomas Grafa5531a52006-08-26 20:11:47 -0700192 case NLA_FLAG:
193 if (attrlen > 0)
Johannes Bergc29f1842018-09-26 11:15:32 +0200194 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700195 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100196
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400197 case NLA_BITFIELD32:
198 if (attrlen != sizeof(struct nla_bitfield32))
Johannes Bergc29f1842018-09-26 11:15:32 +0200199 goto out_err;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400200
Johannes Bergc29f1842018-09-26 11:15:32 +0200201 err = validate_nla_bitfield32(nla, pt->validation_data);
202 if (err)
203 goto out_err;
204 break;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400205
Thomas Grafa5531a52006-08-26 20:11:47 -0700206 case NLA_NUL_STRING:
207 if (pt->len)
208 minlen = min_t(int, attrlen, pt->len + 1);
209 else
210 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100211
Johannes Bergc29f1842018-09-26 11:15:32 +0200212 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
213 err = -EINVAL;
214 goto out_err;
215 }
Thomas Grafa5531a52006-08-26 20:11:47 -0700216 /* fall through */
217
218 case NLA_STRING:
219 if (attrlen < 1)
Johannes Bergc29f1842018-09-26 11:15:32 +0200220 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700221
222 if (pt->len) {
223 char *buf = nla_data(nla);
224
225 if (buf[attrlen - 1] == '\0')
226 attrlen--;
227
228 if (attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200229 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700230 }
231 break;
232
Johannes Bergd30045a2007-03-23 11:37:48 -0700233 case NLA_BINARY:
234 if (pt->len && attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200235 goto out_err;
Johannes Bergd30045a2007-03-23 11:37:48 -0700236 break;
237
Patrick McHardyea5693c2008-11-28 03:05:19 -0800238 case NLA_NESTED:
239 /* a nested attributes is allowed to be empty; if its not,
240 * it must have a size of at least NLA_HDRLEN.
241 */
242 if (attrlen == 0)
243 break;
Johannes Berg9a659a352018-09-26 11:15:33 +0200244 if (attrlen < NLA_HDRLEN)
245 goto out_err;
246 if (pt->validation_data) {
247 err = nla_validate(nla_data(nla), nla_len(nla), pt->len,
248 pt->validation_data, extack);
249 if (err < 0) {
250 /*
251 * return directly to preserve the inner
252 * error message/attribute pointer
253 */
254 return err;
255 }
256 }
257 break;
Johannes Berg1501d132018-09-26 11:15:34 +0200258 case NLA_NESTED_ARRAY:
259 /* a nested array attribute is allowed to be empty; if its not,
260 * it must have a size of at least NLA_HDRLEN.
261 */
262 if (attrlen == 0)
263 break;
264 if (attrlen < NLA_HDRLEN)
265 goto out_err;
266 if (pt->validation_data) {
267 int err;
268
269 err = nla_validate_array(nla_data(nla), nla_len(nla),
270 pt->len, pt->validation_data,
271 extack);
272 if (err < 0) {
273 /*
274 * return directly to preserve the inner
275 * error message/attribute pointer
276 */
277 return err;
278 }
279 }
280 break;
Thomas Grafa5531a52006-08-26 20:11:47 -0700281 default:
282 if (pt->len)
283 minlen = pt->len;
284 else if (pt->type != NLA_UNSPEC)
285 minlen = nla_attr_minlen[pt->type];
286
287 if (attrlen < minlen)
Johannes Bergc29f1842018-09-26 11:15:32 +0200288 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700289 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100290
Johannes Berg3e48be02018-09-27 11:28:35 +0200291 /* further validation */
292 switch (pt->validation_type) {
293 case NLA_VALIDATE_NONE:
294 /* nothing to do */
295 break;
296 case NLA_VALIDATE_RANGE:
297 case NLA_VALIDATE_MIN:
298 case NLA_VALIDATE_MAX:
299 err = nla_validate_int_range(pt, nla, extack);
300 if (err)
301 return err;
302 break;
Johannes Berg33188bd2018-09-27 11:28:36 +0200303 case NLA_VALIDATE_FUNCTION:
304 if (pt->validate) {
305 err = pt->validate(nla, extack);
306 if (err)
307 return err;
308 }
309 break;
Johannes Berg3e48be02018-09-27 11:28:35 +0200310 }
311
Thomas Grafbfa83a92005-11-10 02:25:51 +0100312 return 0;
Johannes Bergc29f1842018-09-26 11:15:32 +0200313out_err:
314 NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation");
315 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100316}
317
318/**
319 * nla_validate - Validate a stream of attributes
320 * @head: head of attribute stream
321 * @len: length of attribute stream
322 * @maxtype: maximum attribute type to be expected
323 * @policy: validation policy
Johannes Bergfceb6432017-04-12 14:34:07 +0200324 * @extack: extended ACK report struct
Thomas Grafbfa83a92005-11-10 02:25:51 +0100325 *
326 * Validates all attributes in the specified attribute stream against the
327 * specified policy. Attributes with a type exceeding maxtype will be
328 * ignored. See documenation of struct nla_policy for more details.
329 *
330 * Returns 0 on success or a negative error code.
331 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800332int nla_validate(const struct nlattr *head, int len, int maxtype,
Johannes Bergfceb6432017-04-12 14:34:07 +0200333 const struct nla_policy *policy,
334 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100335{
Jan Engelhardt36546542010-11-16 09:52:32 -0800336 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200337 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100338
339 nla_for_each_attr(nla, head, len, rem) {
Johannes Bergc29f1842018-09-26 11:15:32 +0200340 int err = validate_nla(nla, maxtype, policy, extack);
Johannes Bergfceb6432017-04-12 14:34:07 +0200341
Johannes Bergc29f1842018-09-26 11:15:32 +0200342 if (err < 0)
Johannes Bergfceb6432017-04-12 14:34:07 +0200343 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100344 }
345
Johannes Bergfceb6432017-04-12 14:34:07 +0200346 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100347}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700348EXPORT_SYMBOL(nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100349
350/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100351 * nla_policy_len - Determin the max. length of a policy
352 * @policy: policy to use
353 * @n: number of policies
354 *
355 * Determines the max. length of the policy. It is currently used
356 * to allocated Netlink buffers roughly the size of the actual
357 * message.
358 *
359 * Returns 0 on success or a negative error code.
360 */
361int
362nla_policy_len(const struct nla_policy *p, int n)
363{
364 int i, len = 0;
365
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800366 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100367 if (p->len)
368 len += nla_total_size(p->len);
David Ahern28033ae2017-11-07 21:59:40 -0800369 else if (nla_attr_len[p->type])
370 len += nla_total_size(nla_attr_len[p->type]);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100371 else if (nla_attr_minlen[p->type])
372 len += nla_total_size(nla_attr_minlen[p->type]);
373 }
374
375 return len;
376}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700377EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100378
379/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100380 * nla_parse - Parse a stream of attributes into a tb buffer
381 * @tb: destination array with maxtype+1 elements
382 * @maxtype: maximum attribute type to be expected
383 * @head: head of attribute stream
384 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700385 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100386 *
387 * Parses a stream of attributes and stores a pointer to each attribute in
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400388 * the tb array accessible via the attribute type. Attributes with a type
Thomas Grafbfa83a92005-11-10 02:25:51 +0100389 * exceeding maxtype will be silently ignored for backwards compatibility
390 * reasons. policy may be set to NULL if no validation is required.
391 *
392 * Returns 0 on success or a negative error code.
393 */
David Aherna5f6cba2018-10-07 20:16:25 -0700394static int __nla_parse(struct nlattr **tb, int maxtype,
395 const struct nlattr *head, int len,
396 bool strict, const struct nla_policy *policy,
397 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100398{
Jan Engelhardt36546542010-11-16 09:52:32 -0800399 const struct nlattr *nla;
Johannes Bergc29f1842018-09-26 11:15:32 +0200400 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100401
402 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
403
404 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200405 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100406
David Aherna5f6cba2018-10-07 20:16:25 -0700407 if (type == 0 || type > maxtype) {
408 if (strict) {
409 NL_SET_ERR_MSG(extack, "Unknown attribute type");
410 return -EINVAL;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100411 }
David Aherna5f6cba2018-10-07 20:16:25 -0700412 continue;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100413 }
David Aherna5f6cba2018-10-07 20:16:25 -0700414 if (policy) {
415 int err = validate_nla(nla, maxtype, policy, extack);
416
417 if (err < 0)
418 return err;
419 }
420
421 tb[type] = (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100422 }
423
David Aherna5f6cba2018-10-07 20:16:25 -0700424 if (unlikely(rem > 0)) {
Michal Schmidtbfc51842014-06-02 18:25:02 +0200425 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
426 rem, current->comm);
David Aherna5f6cba2018-10-07 20:16:25 -0700427 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
428 if (strict)
429 return -EINVAL;
430 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100431
Johannes Bergc29f1842018-09-26 11:15:32 +0200432 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100433}
David Aherna5f6cba2018-10-07 20:16:25 -0700434
435int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
436 int len, const struct nla_policy *policy,
437 struct netlink_ext_ack *extack)
438{
439 return __nla_parse(tb, maxtype, head, len, false, policy, extack);
440}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700441EXPORT_SYMBOL(nla_parse);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100442
David Aherna5f6cba2018-10-07 20:16:25 -0700443int nla_parse_strict(struct nlattr **tb, int maxtype, const struct nlattr *head,
444 int len, const struct nla_policy *policy,
445 struct netlink_ext_ack *extack)
446{
447 return __nla_parse(tb, maxtype, head, len, true, policy, extack);
448}
449EXPORT_SYMBOL(nla_parse_strict);
450
Thomas Grafbfa83a92005-11-10 02:25:51 +0100451/**
452 * nla_find - Find a specific attribute in a stream of attributes
453 * @head: head of attribute stream
454 * @len: length of attribute stream
455 * @attrtype: type of attribute to look for
456 *
457 * Returns the first attribute in the stream matching the specified type.
458 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800459struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100460{
Jan Engelhardt36546542010-11-16 09:52:32 -0800461 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100462 int rem;
463
464 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200465 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800466 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100467
468 return NULL;
469}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700470EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100471
472/**
473 * nla_strlcpy - Copy string attribute payload into a sized buffer
474 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700475 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100476 * @dstsize: size of destination buffer
477 *
478 * Copies at most dstsize - 1 bytes into the destination buffer.
479 * The result is always a valid NUL-terminated string. Unlike
480 * strlcpy the destination buffer is always padded out.
481 *
482 * Returns the length of the source buffer.
483 */
484size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
485{
486 size_t srclen = nla_len(nla);
487 char *src = nla_data(nla);
488
489 if (srclen > 0 && src[srclen - 1] == '\0')
490 srclen--;
491
492 if (dstsize > 0) {
493 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
494
495 memset(dst, 0, dstsize);
496 memcpy(dst, src, len);
497 }
498
499 return srclen;
500}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700501EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100502
503/**
Phil Sutter2cf0c8b2017-07-27 16:56:40 +0200504 * nla_strdup - Copy string attribute payload into a newly allocated buffer
505 * @nla: attribute to copy the string from
506 * @flags: the type of memory to allocate (see kmalloc).
507 *
508 * Returns a pointer to the allocated buffer or NULL on error.
509 */
510char *nla_strdup(const struct nlattr *nla, gfp_t flags)
511{
512 size_t srclen = nla_len(nla);
513 char *src = nla_data(nla), *dst;
514
515 if (srclen > 0 && src[srclen - 1] == '\0')
516 srclen--;
517
518 dst = kmalloc(srclen + 1, flags);
519 if (dst != NULL) {
520 memcpy(dst, src, srclen);
521 dst[srclen] = '\0';
522 }
523 return dst;
524}
525EXPORT_SYMBOL(nla_strdup);
526
527/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100528 * nla_memcpy - Copy a netlink attribute into another memory area
529 * @dest: where to copy to memcpy
530 * @src: netlink attribute to copy from
531 * @count: size of the destination area
532 *
533 * Note: The number of bytes copied is limited by the length of
534 * attribute's payload. memcpy
535 *
536 * Returns the number of bytes copied.
537 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700538int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100539{
540 int minlen = min_t(int, count, nla_len(src));
541
542 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200543 if (count > minlen)
544 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100545
546 return minlen;
547}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700548EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100549
550/**
551 * nla_memcmp - Compare an attribute with sized memory area
552 * @nla: netlink attribute
553 * @data: memory area
554 * @size: size of memory area
555 */
556int nla_memcmp(const struct nlattr *nla, const void *data,
557 size_t size)
558{
559 int d = nla_len(nla) - size;
560
561 if (d == 0)
562 d = memcmp(nla_data(nla), data, size);
563
564 return d;
565}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700566EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100567
568/**
569 * nla_strcmp - Compare a string attribute against a string
570 * @nla: netlink string attribute
571 * @str: another string
572 */
573int nla_strcmp(const struct nlattr *nla, const char *str)
574{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200575 int len = strlen(str);
576 char *buf = nla_data(nla);
577 int attrlen = nla_len(nla);
578 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100579
Pablo Neira8b7b9322014-04-01 19:38:44 +0200580 if (attrlen > 0 && buf[attrlen - 1] == '\0')
581 attrlen--;
582
583 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100584 if (d == 0)
585 d = memcmp(nla_data(nla), str, len);
586
587 return d;
588}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700589EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100590
Herbert Xu90800212009-03-11 23:18:32 +0800591#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100592/**
593 * __nla_reserve - reserve room for attribute on the skb
594 * @skb: socket buffer to reserve room on
595 * @attrtype: attribute type
596 * @attrlen: length of attribute payload
597 *
598 * Adds a netlink attribute header to a socket buffer and reserves
599 * room for the payload but does not copy it.
600 *
601 * The caller is responsible to ensure that the skb provides enough
602 * tailroom for the attribute header and payload.
603 */
604struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
605{
606 struct nlattr *nla;
607
Johannes Berg4df864c2017-06-16 14:29:21 +0200608 nla = skb_put(skb, nla_total_size(attrlen));
Thomas Grafbfa83a92005-11-10 02:25:51 +0100609 nla->nla_type = attrtype;
610 nla->nla_len = nla_attr_size(attrlen);
611
612 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
613
614 return nla;
615}
Herbert Xu90800212009-03-11 23:18:32 +0800616EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100617
618/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200619 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
620 * @skb: socket buffer to reserve room on
621 * @attrtype: attribute type
622 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200623 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200624 *
625 * Adds a netlink attribute header to a socket buffer and reserves
626 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200627 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200628 *
629 * The caller is responsible to ensure that the skb provides enough
630 * tailroom for the attribute header and payload.
631 */
632struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
633 int attrlen, int padattr)
634{
635 if (nla_need_padding_for_64bit(skb))
636 nla_align_64bit(skb, padattr);
637
638 return __nla_reserve(skb, attrtype, attrlen);
639}
640EXPORT_SYMBOL(__nla_reserve_64bit);
641
642/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700643 * __nla_reserve_nohdr - reserve room for attribute without header
644 * @skb: socket buffer to reserve room on
645 * @attrlen: length of attribute payload
646 *
647 * Reserves room for attribute payload without a header.
648 *
649 * The caller is responsible to ensure that the skb provides enough
650 * tailroom for the payload.
651 */
652void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
653{
yuan linyub952f4d2017-06-18 22:52:04 +0800654 return skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700655}
Herbert Xu90800212009-03-11 23:18:32 +0800656EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700657
658/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100659 * nla_reserve - reserve room for attribute on the skb
660 * @skb: socket buffer to reserve room on
661 * @attrtype: attribute type
662 * @attrlen: length of attribute payload
663 *
664 * Adds a netlink attribute header to a socket buffer and reserves
665 * room for the payload but does not copy it.
666 *
667 * Returns NULL if the tailroom of the skb is insufficient to store
668 * the attribute header and payload.
669 */
670struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
671{
672 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
673 return NULL;
674
675 return __nla_reserve(skb, attrtype, attrlen);
676}
Herbert Xu90800212009-03-11 23:18:32 +0800677EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100678
679/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200680 * nla_reserve_64bit - reserve room for attribute on the skb and align it
681 * @skb: socket buffer to reserve room on
682 * @attrtype: attribute type
683 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200684 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200685 *
686 * Adds a netlink attribute header to a socket buffer and reserves
687 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200688 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200689 *
690 * Returns NULL if the tailroom of the skb is insufficient to store
691 * the attribute header and payload.
692 */
693struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
694 int padattr)
695{
696 size_t len;
697
698 if (nla_need_padding_for_64bit(skb))
699 len = nla_total_size_64bit(attrlen);
700 else
701 len = nla_total_size(attrlen);
702 if (unlikely(skb_tailroom(skb) < len))
703 return NULL;
704
705 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
706}
707EXPORT_SYMBOL(nla_reserve_64bit);
708
709/**
Julius Volz10b595a2008-06-27 20:02:14 -0700710 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700711 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700712 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700713 *
714 * Reserves room for attribute payload without a header.
715 *
716 * Returns NULL if the tailroom of the skb is insufficient to store
717 * the attribute payload.
718 */
719void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
720{
721 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
722 return NULL;
723
724 return __nla_reserve_nohdr(skb, attrlen);
725}
Herbert Xu90800212009-03-11 23:18:32 +0800726EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700727
728/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100729 * __nla_put - Add a netlink attribute to a socket buffer
730 * @skb: socket buffer to add attribute to
731 * @attrtype: attribute type
732 * @attrlen: length of attribute payload
733 * @data: head of attribute payload
734 *
735 * The caller is responsible to ensure that the skb provides enough
736 * tailroom for the attribute header and payload.
737 */
738void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
739 const void *data)
740{
741 struct nlattr *nla;
742
743 nla = __nla_reserve(skb, attrtype, attrlen);
744 memcpy(nla_data(nla), data, attrlen);
745}
Herbert Xu90800212009-03-11 23:18:32 +0800746EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100747
Thomas Graffe4944e2006-08-04 23:03:05 -0700748/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200749 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
750 * @skb: socket buffer to add attribute to
751 * @attrtype: attribute type
752 * @attrlen: length of attribute payload
753 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200754 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200755 *
756 * The caller is responsible to ensure that the skb provides enough
757 * tailroom for the attribute header and payload.
758 */
759void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
760 const void *data, int padattr)
761{
762 struct nlattr *nla;
763
764 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
765 memcpy(nla_data(nla), data, attrlen);
766}
767EXPORT_SYMBOL(__nla_put_64bit);
768
769/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700770 * __nla_put_nohdr - Add a netlink attribute without header
771 * @skb: socket buffer to add attribute to
772 * @attrlen: length of attribute payload
773 * @data: head of attribute payload
774 *
775 * The caller is responsible to ensure that the skb provides enough
776 * tailroom for the attribute payload.
777 */
778void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
779{
780 void *start;
781
782 start = __nla_reserve_nohdr(skb, attrlen);
783 memcpy(start, data, attrlen);
784}
Herbert Xu90800212009-03-11 23:18:32 +0800785EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100786
787/**
788 * nla_put - Add a netlink attribute to a socket buffer
789 * @skb: socket buffer to add attribute to
790 * @attrtype: attribute type
791 * @attrlen: length of attribute payload
792 * @data: head of attribute payload
793 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700794 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100795 * the attribute header and payload.
796 */
797int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
798{
799 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700800 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100801
802 __nla_put(skb, attrtype, attrlen, data);
803 return 0;
804}
Herbert Xu90800212009-03-11 23:18:32 +0800805EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100806
Thomas Graffe4944e2006-08-04 23:03:05 -0700807/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200808 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
809 * @skb: socket buffer to add attribute to
810 * @attrtype: attribute type
811 * @attrlen: length of attribute payload
812 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200813 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200814 *
815 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
816 * the attribute header and payload.
817 */
818int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
819 const void *data, 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 -EMSGSIZE;
829
830 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
831 return 0;
832}
833EXPORT_SYMBOL(nla_put_64bit);
834
835/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700836 * nla_put_nohdr - Add a netlink attribute without header
837 * @skb: socket buffer to add attribute to
838 * @attrlen: length of attribute payload
839 * @data: head of attribute payload
840 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700841 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700842 * the attribute payload.
843 */
844int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
845{
846 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700847 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700848
849 __nla_put_nohdr(skb, attrlen, data);
850 return 0;
851}
Herbert Xu90800212009-03-11 23:18:32 +0800852EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100853
Patrick McHardy01480e12008-01-22 22:10:59 -0800854/**
855 * nla_append - Add a netlink attribute without header or padding
856 * @skb: socket buffer to add attribute to
857 * @attrlen: length of attribute payload
858 * @data: head of attribute payload
859 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700860 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800861 * the attribute payload.
862 */
863int nla_append(struct sk_buff *skb, int attrlen, const void *data)
864{
865 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700866 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800867
Johannes Berg59ae1d12017-06-16 14:29:20 +0200868 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800869 return 0;
870}
Herbert Xu90800212009-03-11 23:18:32 +0800871EXPORT_SYMBOL(nla_append);
872#endif