blob: 2f8feff669a7b41be4f6747c978a050ce7ade1f5 [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
Jan Engelhardt36546542010-11-16 09:52:32 -080098static int validate_nla(const struct nlattr *nla, int maxtype,
Johannes Berg568b7422018-09-17 11:57:28 +020099 const struct nla_policy *policy,
Johannes Bergc29f1842018-09-26 11:15:32 +0200100 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100101{
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700102 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200103 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Johannes Bergc29f1842018-09-26 11:15:32 +0200104 int err = -ERANGE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100105
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200106 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100107 return 0;
108
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200109 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +0100110
111 BUG_ON(pt->type > NLA_TYPE_MAX);
112
Johannes Bergb60b87f2018-09-17 11:57:29 +0200113 if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) ||
114 (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) {
David Ahern6e237d02017-12-06 20:09:12 -0800115 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
116 current->comm, type);
David Ahern28033ae2017-11-07 21:59:40 -0800117 }
118
Thomas Grafa5531a52006-08-26 20:11:47 -0700119 switch (pt->type) {
Johannes Bergb60b87f2018-09-17 11:57:29 +0200120 case NLA_EXACT_LEN:
121 if (attrlen != pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200122 goto out_err;
Johannes Bergb60b87f2018-09-17 11:57:29 +0200123 break;
124
Johannes Berg568b7422018-09-17 11:57:28 +0200125 case NLA_REJECT:
Johannes Bergc29f1842018-09-26 11:15:32 +0200126 if (extack && pt->validation_data) {
127 NL_SET_BAD_ATTR(extack, nla);
128 extack->_msg = pt->validation_data;
129 return -EINVAL;
130 }
131 err = -EINVAL;
132 goto out_err;
Johannes Berg568b7422018-09-17 11:57:28 +0200133
Thomas Grafa5531a52006-08-26 20:11:47 -0700134 case NLA_FLAG:
135 if (attrlen > 0)
Johannes Bergc29f1842018-09-26 11:15:32 +0200136 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700137 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100138
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400139 case NLA_BITFIELD32:
140 if (attrlen != sizeof(struct nla_bitfield32))
Johannes Bergc29f1842018-09-26 11:15:32 +0200141 goto out_err;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400142
Johannes Bergc29f1842018-09-26 11:15:32 +0200143 err = validate_nla_bitfield32(nla, pt->validation_data);
144 if (err)
145 goto out_err;
146 break;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400147
Thomas Grafa5531a52006-08-26 20:11:47 -0700148 case NLA_NUL_STRING:
149 if (pt->len)
150 minlen = min_t(int, attrlen, pt->len + 1);
151 else
152 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100153
Johannes Bergc29f1842018-09-26 11:15:32 +0200154 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
155 err = -EINVAL;
156 goto out_err;
157 }
Thomas Grafa5531a52006-08-26 20:11:47 -0700158 /* fall through */
159
160 case NLA_STRING:
161 if (attrlen < 1)
Johannes Bergc29f1842018-09-26 11:15:32 +0200162 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700163
164 if (pt->len) {
165 char *buf = nla_data(nla);
166
167 if (buf[attrlen - 1] == '\0')
168 attrlen--;
169
170 if (attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200171 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700172 }
173 break;
174
Johannes Bergd30045a2007-03-23 11:37:48 -0700175 case NLA_BINARY:
176 if (pt->len && attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200177 goto out_err;
Johannes Bergd30045a2007-03-23 11:37:48 -0700178 break;
179
Patrick McHardyea5693c2008-11-28 03:05:19 -0800180 case NLA_NESTED:
181 /* a nested attributes is allowed to be empty; if its not,
182 * it must have a size of at least NLA_HDRLEN.
183 */
184 if (attrlen == 0)
185 break;
Johannes Berg9a659a32018-09-26 11:15:33 +0200186 if (attrlen < NLA_HDRLEN)
187 goto out_err;
188 if (pt->validation_data) {
189 err = nla_validate(nla_data(nla), nla_len(nla), pt->len,
190 pt->validation_data, extack);
191 if (err < 0) {
192 /*
193 * return directly to preserve the inner
194 * error message/attribute pointer
195 */
196 return err;
197 }
198 }
199 break;
Johannes Berg1501d132018-09-26 11:15:34 +0200200 case NLA_NESTED_ARRAY:
201 /* a nested array attribute is allowed to be empty; if its not,
202 * it must have a size of at least NLA_HDRLEN.
203 */
204 if (attrlen == 0)
205 break;
206 if (attrlen < NLA_HDRLEN)
207 goto out_err;
208 if (pt->validation_data) {
209 int err;
210
211 err = nla_validate_array(nla_data(nla), nla_len(nla),
212 pt->len, pt->validation_data,
213 extack);
214 if (err < 0) {
215 /*
216 * return directly to preserve the inner
217 * error message/attribute pointer
218 */
219 return err;
220 }
221 }
222 break;
Thomas Grafa5531a52006-08-26 20:11:47 -0700223 default:
224 if (pt->len)
225 minlen = pt->len;
226 else if (pt->type != NLA_UNSPEC)
227 minlen = nla_attr_minlen[pt->type];
228
229 if (attrlen < minlen)
Johannes Bergc29f1842018-09-26 11:15:32 +0200230 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700231 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100232
233 return 0;
Johannes Bergc29f1842018-09-26 11:15:32 +0200234out_err:
235 NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation");
236 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100237}
238
239/**
240 * nla_validate - Validate a stream of attributes
241 * @head: head of attribute stream
242 * @len: length of attribute stream
243 * @maxtype: maximum attribute type to be expected
244 * @policy: validation policy
Johannes Bergfceb6432017-04-12 14:34:07 +0200245 * @extack: extended ACK report struct
Thomas Grafbfa83a92005-11-10 02:25:51 +0100246 *
247 * Validates all attributes in the specified attribute stream against the
248 * specified policy. Attributes with a type exceeding maxtype will be
249 * ignored. See documenation of struct nla_policy for more details.
250 *
251 * Returns 0 on success or a negative error code.
252 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800253int nla_validate(const struct nlattr *head, int len, int maxtype,
Johannes Bergfceb6432017-04-12 14:34:07 +0200254 const struct nla_policy *policy,
255 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100256{
Jan Engelhardt36546542010-11-16 09:52:32 -0800257 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200258 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100259
260 nla_for_each_attr(nla, head, len, rem) {
Johannes Bergc29f1842018-09-26 11:15:32 +0200261 int err = validate_nla(nla, maxtype, policy, extack);
Johannes Bergfceb6432017-04-12 14:34:07 +0200262
Johannes Bergc29f1842018-09-26 11:15:32 +0200263 if (err < 0)
Johannes Bergfceb6432017-04-12 14:34:07 +0200264 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100265 }
266
Johannes Bergfceb6432017-04-12 14:34:07 +0200267 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100268}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700269EXPORT_SYMBOL(nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100270
271/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100272 * nla_policy_len - Determin the max. length of a policy
273 * @policy: policy to use
274 * @n: number of policies
275 *
276 * Determines the max. length of the policy. It is currently used
277 * to allocated Netlink buffers roughly the size of the actual
278 * message.
279 *
280 * Returns 0 on success or a negative error code.
281 */
282int
283nla_policy_len(const struct nla_policy *p, int n)
284{
285 int i, len = 0;
286
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800287 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100288 if (p->len)
289 len += nla_total_size(p->len);
David Ahern28033ae2017-11-07 21:59:40 -0800290 else if (nla_attr_len[p->type])
291 len += nla_total_size(nla_attr_len[p->type]);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100292 else if (nla_attr_minlen[p->type])
293 len += nla_total_size(nla_attr_minlen[p->type]);
294 }
295
296 return len;
297}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700298EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100299
300/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100301 * nla_parse - Parse a stream of attributes into a tb buffer
302 * @tb: destination array with maxtype+1 elements
303 * @maxtype: maximum attribute type to be expected
304 * @head: head of attribute stream
305 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700306 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100307 *
308 * Parses a stream of attributes and stores a pointer to each attribute in
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400309 * the tb array accessible via the attribute type. Attributes with a type
Thomas Grafbfa83a92005-11-10 02:25:51 +0100310 * exceeding maxtype will be silently ignored for backwards compatibility
311 * reasons. policy may be set to NULL if no validation is required.
312 *
313 * Returns 0 on success or a negative error code.
314 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800315int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
Johannes Bergfceb6432017-04-12 14:34:07 +0200316 int len, const struct nla_policy *policy,
317 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100318{
Jan Engelhardt36546542010-11-16 09:52:32 -0800319 const struct nlattr *nla;
Johannes Bergc29f1842018-09-26 11:15:32 +0200320 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100321
322 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
323
324 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200325 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100326
327 if (type > 0 && type <= maxtype) {
328 if (policy) {
Johannes Bergc29f1842018-09-26 11:15:32 +0200329 int err = validate_nla(nla, maxtype, policy,
330 extack);
331
332 if (err < 0)
333 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100334 }
335
Jan Engelhardt36546542010-11-16 09:52:32 -0800336 tb[type] = (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100337 }
338 }
339
340 if (unlikely(rem > 0))
Michal Schmidtbfc51842014-06-02 18:25:02 +0200341 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
342 rem, current->comm);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100343
Johannes Bergc29f1842018-09-26 11:15:32 +0200344 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100345}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700346EXPORT_SYMBOL(nla_parse);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100347
348/**
349 * nla_find - Find a specific attribute in a stream of attributes
350 * @head: head of attribute stream
351 * @len: length of attribute stream
352 * @attrtype: type of attribute to look for
353 *
354 * Returns the first attribute in the stream matching the specified type.
355 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800356struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100357{
Jan Engelhardt36546542010-11-16 09:52:32 -0800358 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100359 int rem;
360
361 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200362 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800363 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100364
365 return NULL;
366}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700367EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100368
369/**
370 * nla_strlcpy - Copy string attribute payload into a sized buffer
371 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700372 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100373 * @dstsize: size of destination buffer
374 *
375 * Copies at most dstsize - 1 bytes into the destination buffer.
376 * The result is always a valid NUL-terminated string. Unlike
377 * strlcpy the destination buffer is always padded out.
378 *
379 * Returns the length of the source buffer.
380 */
381size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
382{
383 size_t srclen = nla_len(nla);
384 char *src = nla_data(nla);
385
386 if (srclen > 0 && src[srclen - 1] == '\0')
387 srclen--;
388
389 if (dstsize > 0) {
390 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
391
392 memset(dst, 0, dstsize);
393 memcpy(dst, src, len);
394 }
395
396 return srclen;
397}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700398EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100399
400/**
Phil Sutter2cf0c8b2017-07-27 16:56:40 +0200401 * nla_strdup - Copy string attribute payload into a newly allocated buffer
402 * @nla: attribute to copy the string from
403 * @flags: the type of memory to allocate (see kmalloc).
404 *
405 * Returns a pointer to the allocated buffer or NULL on error.
406 */
407char *nla_strdup(const struct nlattr *nla, gfp_t flags)
408{
409 size_t srclen = nla_len(nla);
410 char *src = nla_data(nla), *dst;
411
412 if (srclen > 0 && src[srclen - 1] == '\0')
413 srclen--;
414
415 dst = kmalloc(srclen + 1, flags);
416 if (dst != NULL) {
417 memcpy(dst, src, srclen);
418 dst[srclen] = '\0';
419 }
420 return dst;
421}
422EXPORT_SYMBOL(nla_strdup);
423
424/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100425 * nla_memcpy - Copy a netlink attribute into another memory area
426 * @dest: where to copy to memcpy
427 * @src: netlink attribute to copy from
428 * @count: size of the destination area
429 *
430 * Note: The number of bytes copied is limited by the length of
431 * attribute's payload. memcpy
432 *
433 * Returns the number of bytes copied.
434 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700435int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100436{
437 int minlen = min_t(int, count, nla_len(src));
438
439 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200440 if (count > minlen)
441 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100442
443 return minlen;
444}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700445EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100446
447/**
448 * nla_memcmp - Compare an attribute with sized memory area
449 * @nla: netlink attribute
450 * @data: memory area
451 * @size: size of memory area
452 */
453int nla_memcmp(const struct nlattr *nla, const void *data,
454 size_t size)
455{
456 int d = nla_len(nla) - size;
457
458 if (d == 0)
459 d = memcmp(nla_data(nla), data, size);
460
461 return d;
462}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700463EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100464
465/**
466 * nla_strcmp - Compare a string attribute against a string
467 * @nla: netlink string attribute
468 * @str: another string
469 */
470int nla_strcmp(const struct nlattr *nla, const char *str)
471{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200472 int len = strlen(str);
473 char *buf = nla_data(nla);
474 int attrlen = nla_len(nla);
475 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100476
Pablo Neira8b7b9322014-04-01 19:38:44 +0200477 if (attrlen > 0 && buf[attrlen - 1] == '\0')
478 attrlen--;
479
480 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100481 if (d == 0)
482 d = memcmp(nla_data(nla), str, len);
483
484 return d;
485}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700486EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100487
Herbert Xu90800212009-03-11 23:18:32 +0800488#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100489/**
490 * __nla_reserve - reserve room for attribute on the skb
491 * @skb: socket buffer to reserve room on
492 * @attrtype: attribute type
493 * @attrlen: length of attribute payload
494 *
495 * Adds a netlink attribute header to a socket buffer and reserves
496 * room for the payload but does not copy it.
497 *
498 * The caller is responsible to ensure that the skb provides enough
499 * tailroom for the attribute header and payload.
500 */
501struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
502{
503 struct nlattr *nla;
504
Johannes Berg4df864c2017-06-16 14:29:21 +0200505 nla = skb_put(skb, nla_total_size(attrlen));
Thomas Grafbfa83a92005-11-10 02:25:51 +0100506 nla->nla_type = attrtype;
507 nla->nla_len = nla_attr_size(attrlen);
508
509 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
510
511 return nla;
512}
Herbert Xu90800212009-03-11 23:18:32 +0800513EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100514
515/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200516 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
517 * @skb: socket buffer to reserve room on
518 * @attrtype: attribute type
519 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200520 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200521 *
522 * Adds a netlink attribute header to a socket buffer and reserves
523 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200524 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200525 *
526 * The caller is responsible to ensure that the skb provides enough
527 * tailroom for the attribute header and payload.
528 */
529struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
530 int attrlen, int padattr)
531{
532 if (nla_need_padding_for_64bit(skb))
533 nla_align_64bit(skb, padattr);
534
535 return __nla_reserve(skb, attrtype, attrlen);
536}
537EXPORT_SYMBOL(__nla_reserve_64bit);
538
539/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700540 * __nla_reserve_nohdr - reserve room for attribute without header
541 * @skb: socket buffer to reserve room on
542 * @attrlen: length of attribute payload
543 *
544 * Reserves room for attribute payload without a header.
545 *
546 * The caller is responsible to ensure that the skb provides enough
547 * tailroom for the payload.
548 */
549void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
550{
yuan linyub952f4d2017-06-18 22:52:04 +0800551 return skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700552}
Herbert Xu90800212009-03-11 23:18:32 +0800553EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700554
555/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100556 * nla_reserve - reserve room for attribute on the skb
557 * @skb: socket buffer to reserve room on
558 * @attrtype: attribute type
559 * @attrlen: length of attribute payload
560 *
561 * Adds a netlink attribute header to a socket buffer and reserves
562 * room for the payload but does not copy it.
563 *
564 * Returns NULL if the tailroom of the skb is insufficient to store
565 * the attribute header and payload.
566 */
567struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
568{
569 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
570 return NULL;
571
572 return __nla_reserve(skb, attrtype, attrlen);
573}
Herbert Xu90800212009-03-11 23:18:32 +0800574EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100575
576/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200577 * nla_reserve_64bit - reserve room for attribute on the skb and align it
578 * @skb: socket buffer to reserve room on
579 * @attrtype: attribute type
580 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200581 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200582 *
583 * Adds a netlink attribute header to a socket buffer and reserves
584 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200585 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200586 *
587 * Returns NULL if the tailroom of the skb is insufficient to store
588 * the attribute header and payload.
589 */
590struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
591 int padattr)
592{
593 size_t len;
594
595 if (nla_need_padding_for_64bit(skb))
596 len = nla_total_size_64bit(attrlen);
597 else
598 len = nla_total_size(attrlen);
599 if (unlikely(skb_tailroom(skb) < len))
600 return NULL;
601
602 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
603}
604EXPORT_SYMBOL(nla_reserve_64bit);
605
606/**
Julius Volz10b595a2008-06-27 20:02:14 -0700607 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700608 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700609 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700610 *
611 * Reserves room for attribute payload without a header.
612 *
613 * Returns NULL if the tailroom of the skb is insufficient to store
614 * the attribute payload.
615 */
616void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
617{
618 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
619 return NULL;
620
621 return __nla_reserve_nohdr(skb, attrlen);
622}
Herbert Xu90800212009-03-11 23:18:32 +0800623EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700624
625/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100626 * __nla_put - Add a netlink attribute to a socket buffer
627 * @skb: socket buffer to add attribute to
628 * @attrtype: attribute type
629 * @attrlen: length of attribute payload
630 * @data: head of attribute payload
631 *
632 * The caller is responsible to ensure that the skb provides enough
633 * tailroom for the attribute header and payload.
634 */
635void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
636 const void *data)
637{
638 struct nlattr *nla;
639
640 nla = __nla_reserve(skb, attrtype, attrlen);
641 memcpy(nla_data(nla), data, attrlen);
642}
Herbert Xu90800212009-03-11 23:18:32 +0800643EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100644
Thomas Graffe4944e2006-08-04 23:03:05 -0700645/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200646 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
647 * @skb: socket buffer to add attribute to
648 * @attrtype: attribute type
649 * @attrlen: length of attribute payload
650 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200651 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200652 *
653 * The caller is responsible to ensure that the skb provides enough
654 * tailroom for the attribute header and payload.
655 */
656void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
657 const void *data, int padattr)
658{
659 struct nlattr *nla;
660
661 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
662 memcpy(nla_data(nla), data, attrlen);
663}
664EXPORT_SYMBOL(__nla_put_64bit);
665
666/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700667 * __nla_put_nohdr - Add a netlink attribute without header
668 * @skb: socket buffer to add attribute to
669 * @attrlen: length of attribute payload
670 * @data: head of attribute payload
671 *
672 * The caller is responsible to ensure that the skb provides enough
673 * tailroom for the attribute payload.
674 */
675void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
676{
677 void *start;
678
679 start = __nla_reserve_nohdr(skb, attrlen);
680 memcpy(start, data, attrlen);
681}
Herbert Xu90800212009-03-11 23:18:32 +0800682EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100683
684/**
685 * nla_put - Add a netlink attribute to a socket buffer
686 * @skb: socket buffer to add attribute to
687 * @attrtype: attribute type
688 * @attrlen: length of attribute payload
689 * @data: head of attribute payload
690 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700691 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100692 * the attribute header and payload.
693 */
694int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
695{
696 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700697 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100698
699 __nla_put(skb, attrtype, attrlen, data);
700 return 0;
701}
Herbert Xu90800212009-03-11 23:18:32 +0800702EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100703
Thomas Graffe4944e2006-08-04 23:03:05 -0700704/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200705 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
706 * @skb: socket buffer to add attribute to
707 * @attrtype: attribute type
708 * @attrlen: length of attribute payload
709 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200710 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200711 *
712 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
713 * the attribute header and payload.
714 */
715int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
716 const void *data, int padattr)
717{
718 size_t len;
719
720 if (nla_need_padding_for_64bit(skb))
721 len = nla_total_size_64bit(attrlen);
722 else
723 len = nla_total_size(attrlen);
724 if (unlikely(skb_tailroom(skb) < len))
725 return -EMSGSIZE;
726
727 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
728 return 0;
729}
730EXPORT_SYMBOL(nla_put_64bit);
731
732/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700733 * nla_put_nohdr - Add a netlink attribute without header
734 * @skb: socket buffer to add attribute to
735 * @attrlen: length of attribute payload
736 * @data: head of attribute payload
737 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700738 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700739 * the attribute payload.
740 */
741int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
742{
743 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700744 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700745
746 __nla_put_nohdr(skb, attrlen, data);
747 return 0;
748}
Herbert Xu90800212009-03-11 23:18:32 +0800749EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100750
Patrick McHardy01480e12008-01-22 22:10:59 -0800751/**
752 * nla_append - Add a netlink attribute without header or padding
753 * @skb: socket buffer to add attribute to
754 * @attrlen: length of attribute payload
755 * @data: head of attribute payload
756 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700757 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800758 * the attribute payload.
759 */
760int nla_append(struct sk_buff *skb, int attrlen, const void *data)
761{
762 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700763 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800764
Johannes Berg59ae1d12017-06-16 14:29:20 +0200765 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800766 return 0;
767}
Herbert Xu90800212009-03-11 23:18:32 +0800768EXPORT_SYMBOL(nla_append);
769#endif