blob: 6e03d650bec486938a5f6fb95d034fd1b6ba6d7c [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
Jan Engelhardt36546542010-11-16 09:52:32 -080070static int validate_nla(const struct nlattr *nla, int maxtype,
Johannes Berg568b7422018-09-17 11:57:28 +020071 const struct nla_policy *policy,
Johannes Bergc29f1842018-09-26 11:15:32 +020072 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +010073{
Patrick McHardyef7c79e2007-06-05 12:38:30 -070074 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +020075 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Johannes Bergc29f1842018-09-26 11:15:32 +020076 int err = -ERANGE;
Thomas Grafbfa83a92005-11-10 02:25:51 +010077
Thomas Graf8f4c1f92007-09-12 14:44:36 +020078 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +010079 return 0;
80
Thomas Graf8f4c1f92007-09-12 14:44:36 +020081 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +010082
83 BUG_ON(pt->type > NLA_TYPE_MAX);
84
Johannes Bergb60b87f2018-09-17 11:57:29 +020085 if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) ||
86 (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) {
David Ahern6e237d02017-12-06 20:09:12 -080087 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
88 current->comm, type);
David Ahern28033ae2017-11-07 21:59:40 -080089 }
90
Thomas Grafa5531a52006-08-26 20:11:47 -070091 switch (pt->type) {
Johannes Bergb60b87f2018-09-17 11:57:29 +020092 case NLA_EXACT_LEN:
93 if (attrlen != pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +020094 goto out_err;
Johannes Bergb60b87f2018-09-17 11:57:29 +020095 break;
96
Johannes Berg568b7422018-09-17 11:57:28 +020097 case NLA_REJECT:
Johannes Bergc29f1842018-09-26 11:15:32 +020098 if (extack && pt->validation_data) {
99 NL_SET_BAD_ATTR(extack, nla);
100 extack->_msg = pt->validation_data;
101 return -EINVAL;
102 }
103 err = -EINVAL;
104 goto out_err;
Johannes Berg568b7422018-09-17 11:57:28 +0200105
Thomas Grafa5531a52006-08-26 20:11:47 -0700106 case NLA_FLAG:
107 if (attrlen > 0)
Johannes Bergc29f1842018-09-26 11:15:32 +0200108 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700109 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100110
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400111 case NLA_BITFIELD32:
112 if (attrlen != sizeof(struct nla_bitfield32))
Johannes Bergc29f1842018-09-26 11:15:32 +0200113 goto out_err;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400114
Johannes Bergc29f1842018-09-26 11:15:32 +0200115 err = validate_nla_bitfield32(nla, pt->validation_data);
116 if (err)
117 goto out_err;
118 break;
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400119
Thomas Grafa5531a52006-08-26 20:11:47 -0700120 case NLA_NUL_STRING:
121 if (pt->len)
122 minlen = min_t(int, attrlen, pt->len + 1);
123 else
124 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100125
Johannes Bergc29f1842018-09-26 11:15:32 +0200126 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
127 err = -EINVAL;
128 goto out_err;
129 }
Thomas Grafa5531a52006-08-26 20:11:47 -0700130 /* fall through */
131
132 case NLA_STRING:
133 if (attrlen < 1)
Johannes Bergc29f1842018-09-26 11:15:32 +0200134 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700135
136 if (pt->len) {
137 char *buf = nla_data(nla);
138
139 if (buf[attrlen - 1] == '\0')
140 attrlen--;
141
142 if (attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200143 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700144 }
145 break;
146
Johannes Bergd30045a2007-03-23 11:37:48 -0700147 case NLA_BINARY:
148 if (pt->len && attrlen > pt->len)
Johannes Bergc29f1842018-09-26 11:15:32 +0200149 goto out_err;
Johannes Bergd30045a2007-03-23 11:37:48 -0700150 break;
151
Patrick McHardyea5693c2008-11-28 03:05:19 -0800152 case NLA_NESTED:
153 /* a nested attributes is allowed to be empty; if its not,
154 * it must have a size of at least NLA_HDRLEN.
155 */
156 if (attrlen == 0)
157 break;
Thomas Grafa5531a52006-08-26 20:11:47 -0700158 default:
159 if (pt->len)
160 minlen = pt->len;
161 else if (pt->type != NLA_UNSPEC)
162 minlen = nla_attr_minlen[pt->type];
163
164 if (attrlen < minlen)
Johannes Bergc29f1842018-09-26 11:15:32 +0200165 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700166 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100167
168 return 0;
Johannes Bergc29f1842018-09-26 11:15:32 +0200169out_err:
170 NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation");
171 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100172}
173
174/**
175 * nla_validate - Validate a stream of attributes
176 * @head: head of attribute stream
177 * @len: length of attribute stream
178 * @maxtype: maximum attribute type to be expected
179 * @policy: validation policy
Johannes Bergfceb6432017-04-12 14:34:07 +0200180 * @extack: extended ACK report struct
Thomas Grafbfa83a92005-11-10 02:25:51 +0100181 *
182 * Validates all attributes in the specified attribute stream against the
183 * specified policy. Attributes with a type exceeding maxtype will be
184 * ignored. See documenation of struct nla_policy for more details.
185 *
186 * Returns 0 on success or a negative error code.
187 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800188int nla_validate(const struct nlattr *head, int len, int maxtype,
Johannes Bergfceb6432017-04-12 14:34:07 +0200189 const struct nla_policy *policy,
190 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100191{
Jan Engelhardt36546542010-11-16 09:52:32 -0800192 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200193 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100194
195 nla_for_each_attr(nla, head, len, rem) {
Johannes Bergc29f1842018-09-26 11:15:32 +0200196 int err = validate_nla(nla, maxtype, policy, extack);
Johannes Bergfceb6432017-04-12 14:34:07 +0200197
Johannes Bergc29f1842018-09-26 11:15:32 +0200198 if (err < 0)
Johannes Bergfceb6432017-04-12 14:34:07 +0200199 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100200 }
201
Johannes Bergfceb6432017-04-12 14:34:07 +0200202 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100203}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700204EXPORT_SYMBOL(nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100205
206/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100207 * nla_policy_len - Determin the max. length of a policy
208 * @policy: policy to use
209 * @n: number of policies
210 *
211 * Determines the max. length of the policy. It is currently used
212 * to allocated Netlink buffers roughly the size of the actual
213 * message.
214 *
215 * Returns 0 on success or a negative error code.
216 */
217int
218nla_policy_len(const struct nla_policy *p, int n)
219{
220 int i, len = 0;
221
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800222 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100223 if (p->len)
224 len += nla_total_size(p->len);
David Ahern28033ae2017-11-07 21:59:40 -0800225 else if (nla_attr_len[p->type])
226 len += nla_total_size(nla_attr_len[p->type]);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100227 else if (nla_attr_minlen[p->type])
228 len += nla_total_size(nla_attr_minlen[p->type]);
229 }
230
231 return len;
232}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700233EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100234
235/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100236 * nla_parse - Parse a stream of attributes into a tb buffer
237 * @tb: destination array with maxtype+1 elements
238 * @maxtype: maximum attribute type to be expected
239 * @head: head of attribute stream
240 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700241 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100242 *
243 * Parses a stream of attributes and stores a pointer to each attribute in
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400244 * the tb array accessible via the attribute type. Attributes with a type
Thomas Grafbfa83a92005-11-10 02:25:51 +0100245 * exceeding maxtype will be silently ignored for backwards compatibility
246 * reasons. policy may be set to NULL if no validation is required.
247 *
248 * Returns 0 on success or a negative error code.
249 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800250int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
Johannes Bergfceb6432017-04-12 14:34:07 +0200251 int len, const struct nla_policy *policy,
252 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100253{
Jan Engelhardt36546542010-11-16 09:52:32 -0800254 const struct nlattr *nla;
Johannes Bergc29f1842018-09-26 11:15:32 +0200255 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100256
257 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
258
259 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200260 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100261
262 if (type > 0 && type <= maxtype) {
263 if (policy) {
Johannes Bergc29f1842018-09-26 11:15:32 +0200264 int err = validate_nla(nla, maxtype, policy,
265 extack);
266
267 if (err < 0)
268 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100269 }
270
Jan Engelhardt36546542010-11-16 09:52:32 -0800271 tb[type] = (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100272 }
273 }
274
275 if (unlikely(rem > 0))
Michal Schmidtbfc51842014-06-02 18:25:02 +0200276 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
277 rem, current->comm);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100278
Johannes Bergc29f1842018-09-26 11:15:32 +0200279 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100280}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700281EXPORT_SYMBOL(nla_parse);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100282
283/**
284 * nla_find - Find a specific attribute in a stream of attributes
285 * @head: head of attribute stream
286 * @len: length of attribute stream
287 * @attrtype: type of attribute to look for
288 *
289 * Returns the first attribute in the stream matching the specified type.
290 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800291struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100292{
Jan Engelhardt36546542010-11-16 09:52:32 -0800293 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100294 int rem;
295
296 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200297 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800298 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100299
300 return NULL;
301}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700302EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100303
304/**
305 * nla_strlcpy - Copy string attribute payload into a sized buffer
306 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700307 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100308 * @dstsize: size of destination buffer
309 *
310 * Copies at most dstsize - 1 bytes into the destination buffer.
311 * The result is always a valid NUL-terminated string. Unlike
312 * strlcpy the destination buffer is always padded out.
313 *
314 * Returns the length of the source buffer.
315 */
316size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
317{
318 size_t srclen = nla_len(nla);
319 char *src = nla_data(nla);
320
321 if (srclen > 0 && src[srclen - 1] == '\0')
322 srclen--;
323
324 if (dstsize > 0) {
325 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
326
327 memset(dst, 0, dstsize);
328 memcpy(dst, src, len);
329 }
330
331 return srclen;
332}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700333EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100334
335/**
Phil Sutter2cf0c8b2017-07-27 16:56:40 +0200336 * nla_strdup - Copy string attribute payload into a newly allocated buffer
337 * @nla: attribute to copy the string from
338 * @flags: the type of memory to allocate (see kmalloc).
339 *
340 * Returns a pointer to the allocated buffer or NULL on error.
341 */
342char *nla_strdup(const struct nlattr *nla, gfp_t flags)
343{
344 size_t srclen = nla_len(nla);
345 char *src = nla_data(nla), *dst;
346
347 if (srclen > 0 && src[srclen - 1] == '\0')
348 srclen--;
349
350 dst = kmalloc(srclen + 1, flags);
351 if (dst != NULL) {
352 memcpy(dst, src, srclen);
353 dst[srclen] = '\0';
354 }
355 return dst;
356}
357EXPORT_SYMBOL(nla_strdup);
358
359/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100360 * nla_memcpy - Copy a netlink attribute into another memory area
361 * @dest: where to copy to memcpy
362 * @src: netlink attribute to copy from
363 * @count: size of the destination area
364 *
365 * Note: The number of bytes copied is limited by the length of
366 * attribute's payload. memcpy
367 *
368 * Returns the number of bytes copied.
369 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700370int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100371{
372 int minlen = min_t(int, count, nla_len(src));
373
374 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200375 if (count > minlen)
376 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100377
378 return minlen;
379}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700380EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100381
382/**
383 * nla_memcmp - Compare an attribute with sized memory area
384 * @nla: netlink attribute
385 * @data: memory area
386 * @size: size of memory area
387 */
388int nla_memcmp(const struct nlattr *nla, const void *data,
389 size_t size)
390{
391 int d = nla_len(nla) - size;
392
393 if (d == 0)
394 d = memcmp(nla_data(nla), data, size);
395
396 return d;
397}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700398EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100399
400/**
401 * nla_strcmp - Compare a string attribute against a string
402 * @nla: netlink string attribute
403 * @str: another string
404 */
405int nla_strcmp(const struct nlattr *nla, const char *str)
406{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200407 int len = strlen(str);
408 char *buf = nla_data(nla);
409 int attrlen = nla_len(nla);
410 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100411
Pablo Neira8b7b9322014-04-01 19:38:44 +0200412 if (attrlen > 0 && buf[attrlen - 1] == '\0')
413 attrlen--;
414
415 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100416 if (d == 0)
417 d = memcmp(nla_data(nla), str, len);
418
419 return d;
420}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700421EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100422
Herbert Xu90800212009-03-11 23:18:32 +0800423#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100424/**
425 * __nla_reserve - reserve room for attribute on the skb
426 * @skb: socket buffer to reserve room on
427 * @attrtype: attribute type
428 * @attrlen: length of attribute payload
429 *
430 * Adds a netlink attribute header to a socket buffer and reserves
431 * room for the payload but does not copy it.
432 *
433 * The caller is responsible to ensure that the skb provides enough
434 * tailroom for the attribute header and payload.
435 */
436struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
437{
438 struct nlattr *nla;
439
Johannes Berg4df864c2017-06-16 14:29:21 +0200440 nla = skb_put(skb, nla_total_size(attrlen));
Thomas Grafbfa83a92005-11-10 02:25:51 +0100441 nla->nla_type = attrtype;
442 nla->nla_len = nla_attr_size(attrlen);
443
444 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
445
446 return nla;
447}
Herbert Xu90800212009-03-11 23:18:32 +0800448EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100449
450/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200451 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
452 * @skb: socket buffer to reserve room on
453 * @attrtype: attribute type
454 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200455 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200456 *
457 * Adds a netlink attribute header to a socket buffer and reserves
458 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200459 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200460 *
461 * The caller is responsible to ensure that the skb provides enough
462 * tailroom for the attribute header and payload.
463 */
464struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
465 int attrlen, int padattr)
466{
467 if (nla_need_padding_for_64bit(skb))
468 nla_align_64bit(skb, padattr);
469
470 return __nla_reserve(skb, attrtype, attrlen);
471}
472EXPORT_SYMBOL(__nla_reserve_64bit);
473
474/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700475 * __nla_reserve_nohdr - reserve room for attribute without header
476 * @skb: socket buffer to reserve room on
477 * @attrlen: length of attribute payload
478 *
479 * Reserves room for attribute payload without a header.
480 *
481 * The caller is responsible to ensure that the skb provides enough
482 * tailroom for the payload.
483 */
484void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
485{
yuan linyub952f4d2017-06-18 22:52:04 +0800486 return skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700487}
Herbert Xu90800212009-03-11 23:18:32 +0800488EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700489
490/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100491 * nla_reserve - reserve room for attribute on the skb
492 * @skb: socket buffer to reserve room on
493 * @attrtype: attribute type
494 * @attrlen: length of attribute payload
495 *
496 * Adds a netlink attribute header to a socket buffer and reserves
497 * room for the payload but does not copy it.
498 *
499 * Returns NULL if the tailroom of the skb is insufficient to store
500 * the attribute header and payload.
501 */
502struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
503{
504 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
505 return NULL;
506
507 return __nla_reserve(skb, attrtype, attrlen);
508}
Herbert Xu90800212009-03-11 23:18:32 +0800509EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100510
511/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200512 * nla_reserve_64bit - reserve room for attribute on the skb and align it
513 * @skb: socket buffer to reserve room on
514 * @attrtype: attribute type
515 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200516 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200517 *
518 * Adds a netlink attribute header to a socket buffer and reserves
519 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200520 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200521 *
522 * Returns NULL if the tailroom of the skb is insufficient to store
523 * the attribute header and payload.
524 */
525struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
526 int padattr)
527{
528 size_t len;
529
530 if (nla_need_padding_for_64bit(skb))
531 len = nla_total_size_64bit(attrlen);
532 else
533 len = nla_total_size(attrlen);
534 if (unlikely(skb_tailroom(skb) < len))
535 return NULL;
536
537 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
538}
539EXPORT_SYMBOL(nla_reserve_64bit);
540
541/**
Julius Volz10b595a2008-06-27 20:02:14 -0700542 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700543 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700544 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700545 *
546 * Reserves room for attribute payload without a header.
547 *
548 * Returns NULL if the tailroom of the skb is insufficient to store
549 * the attribute payload.
550 */
551void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
552{
553 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
554 return NULL;
555
556 return __nla_reserve_nohdr(skb, attrlen);
557}
Herbert Xu90800212009-03-11 23:18:32 +0800558EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700559
560/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100561 * __nla_put - Add a netlink attribute to a socket buffer
562 * @skb: socket buffer to add attribute to
563 * @attrtype: attribute type
564 * @attrlen: length of attribute payload
565 * @data: head of attribute payload
566 *
567 * The caller is responsible to ensure that the skb provides enough
568 * tailroom for the attribute header and payload.
569 */
570void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
571 const void *data)
572{
573 struct nlattr *nla;
574
575 nla = __nla_reserve(skb, attrtype, attrlen);
576 memcpy(nla_data(nla), data, attrlen);
577}
Herbert Xu90800212009-03-11 23:18:32 +0800578EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100579
Thomas Graffe4944e2006-08-04 23:03:05 -0700580/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200581 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
582 * @skb: socket buffer to add attribute to
583 * @attrtype: attribute type
584 * @attrlen: length of attribute payload
585 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200586 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200587 *
588 * The caller is responsible to ensure that the skb provides enough
589 * tailroom for the attribute header and payload.
590 */
591void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
592 const void *data, int padattr)
593{
594 struct nlattr *nla;
595
596 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
597 memcpy(nla_data(nla), data, attrlen);
598}
599EXPORT_SYMBOL(__nla_put_64bit);
600
601/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700602 * __nla_put_nohdr - Add a netlink attribute without header
603 * @skb: socket buffer to add attribute to
604 * @attrlen: length of attribute payload
605 * @data: head of attribute payload
606 *
607 * The caller is responsible to ensure that the skb provides enough
608 * tailroom for the attribute payload.
609 */
610void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
611{
612 void *start;
613
614 start = __nla_reserve_nohdr(skb, attrlen);
615 memcpy(start, data, attrlen);
616}
Herbert Xu90800212009-03-11 23:18:32 +0800617EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100618
619/**
620 * nla_put - Add a netlink attribute to a socket buffer
621 * @skb: socket buffer to add attribute to
622 * @attrtype: attribute type
623 * @attrlen: length of attribute payload
624 * @data: head of attribute payload
625 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700626 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100627 * the attribute header and payload.
628 */
629int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
630{
631 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700632 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100633
634 __nla_put(skb, attrtype, attrlen, data);
635 return 0;
636}
Herbert Xu90800212009-03-11 23:18:32 +0800637EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100638
Thomas Graffe4944e2006-08-04 23:03:05 -0700639/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200640 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
641 * @skb: socket buffer to add attribute to
642 * @attrtype: attribute type
643 * @attrlen: length of attribute payload
644 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200645 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200646 *
647 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
648 * the attribute header and payload.
649 */
650int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
651 const void *data, int padattr)
652{
653 size_t len;
654
655 if (nla_need_padding_for_64bit(skb))
656 len = nla_total_size_64bit(attrlen);
657 else
658 len = nla_total_size(attrlen);
659 if (unlikely(skb_tailroom(skb) < len))
660 return -EMSGSIZE;
661
662 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
663 return 0;
664}
665EXPORT_SYMBOL(nla_put_64bit);
666
667/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700668 * nla_put_nohdr - Add a netlink attribute without header
669 * @skb: socket buffer to add attribute to
670 * @attrlen: length of attribute payload
671 * @data: head of attribute payload
672 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700673 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700674 * the attribute payload.
675 */
676int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
677{
678 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700679 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700680
681 __nla_put_nohdr(skb, attrlen, data);
682 return 0;
683}
Herbert Xu90800212009-03-11 23:18:32 +0800684EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100685
Patrick McHardy01480e12008-01-22 22:10:59 -0800686/**
687 * nla_append - Add a netlink attribute without header or padding
688 * @skb: socket buffer to add attribute to
689 * @attrlen: length of attribute payload
690 * @data: head of attribute payload
691 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700692 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800693 * the attribute payload.
694 */
695int nla_append(struct sk_buff *skb, int attrlen, const void *data)
696{
697 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700698 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800699
Johannes Berg59ae1d12017-06-16 14:29:20 +0200700 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800701 return 0;
702}
Herbert Xu90800212009-03-11 23:18:32 +0800703EXPORT_SYMBOL(nla_append);
704#endif