blob: 04750f88477ca3d28eab644fa3d27659a286c941 [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;
Johannes Berg9a659a352018-09-26 11:15:33 +0200158 if (attrlen < NLA_HDRLEN)
159 goto out_err;
160 if (pt->validation_data) {
161 err = nla_validate(nla_data(nla), nla_len(nla), pt->len,
162 pt->validation_data, extack);
163 if (err < 0) {
164 /*
165 * return directly to preserve the inner
166 * error message/attribute pointer
167 */
168 return err;
169 }
170 }
171 break;
Thomas Grafa5531a52006-08-26 20:11:47 -0700172 default:
173 if (pt->len)
174 minlen = pt->len;
175 else if (pt->type != NLA_UNSPEC)
176 minlen = nla_attr_minlen[pt->type];
177
178 if (attrlen < minlen)
Johannes Bergc29f1842018-09-26 11:15:32 +0200179 goto out_err;
Thomas Grafa5531a52006-08-26 20:11:47 -0700180 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100181
182 return 0;
Johannes Bergc29f1842018-09-26 11:15:32 +0200183out_err:
184 NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation");
185 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100186}
187
188/**
189 * nla_validate - Validate a stream of attributes
190 * @head: head of attribute stream
191 * @len: length of attribute stream
192 * @maxtype: maximum attribute type to be expected
193 * @policy: validation policy
Johannes Bergfceb6432017-04-12 14:34:07 +0200194 * @extack: extended ACK report struct
Thomas Grafbfa83a92005-11-10 02:25:51 +0100195 *
196 * Validates all attributes in the specified attribute stream against the
197 * specified policy. Attributes with a type exceeding maxtype will be
198 * ignored. See documenation of struct nla_policy for more details.
199 *
200 * Returns 0 on success or a negative error code.
201 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800202int nla_validate(const struct nlattr *head, int len, int maxtype,
Johannes Bergfceb6432017-04-12 14:34:07 +0200203 const struct nla_policy *policy,
204 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100205{
Jan Engelhardt36546542010-11-16 09:52:32 -0800206 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200207 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100208
209 nla_for_each_attr(nla, head, len, rem) {
Johannes Bergc29f1842018-09-26 11:15:32 +0200210 int err = validate_nla(nla, maxtype, policy, extack);
Johannes Bergfceb6432017-04-12 14:34:07 +0200211
Johannes Bergc29f1842018-09-26 11:15:32 +0200212 if (err < 0)
Johannes Bergfceb6432017-04-12 14:34:07 +0200213 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100214 }
215
Johannes Bergfceb6432017-04-12 14:34:07 +0200216 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100217}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700218EXPORT_SYMBOL(nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100219
220/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100221 * nla_policy_len - Determin the max. length of a policy
222 * @policy: policy to use
223 * @n: number of policies
224 *
225 * Determines the max. length of the policy. It is currently used
226 * to allocated Netlink buffers roughly the size of the actual
227 * message.
228 *
229 * Returns 0 on success or a negative error code.
230 */
231int
232nla_policy_len(const struct nla_policy *p, int n)
233{
234 int i, len = 0;
235
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800236 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100237 if (p->len)
238 len += nla_total_size(p->len);
David Ahern28033ae2017-11-07 21:59:40 -0800239 else if (nla_attr_len[p->type])
240 len += nla_total_size(nla_attr_len[p->type]);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100241 else if (nla_attr_minlen[p->type])
242 len += nla_total_size(nla_attr_minlen[p->type]);
243 }
244
245 return len;
246}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700247EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100248
249/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100250 * nla_parse - Parse a stream of attributes into a tb buffer
251 * @tb: destination array with maxtype+1 elements
252 * @maxtype: maximum attribute type to be expected
253 * @head: head of attribute stream
254 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700255 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100256 *
257 * Parses a stream of attributes and stores a pointer to each attribute in
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400258 * the tb array accessible via the attribute type. Attributes with a type
Thomas Grafbfa83a92005-11-10 02:25:51 +0100259 * exceeding maxtype will be silently ignored for backwards compatibility
260 * reasons. policy may be set to NULL if no validation is required.
261 *
262 * Returns 0 on success or a negative error code.
263 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800264int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
Johannes Bergfceb6432017-04-12 14:34:07 +0200265 int len, const struct nla_policy *policy,
266 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100267{
Jan Engelhardt36546542010-11-16 09:52:32 -0800268 const struct nlattr *nla;
Johannes Bergc29f1842018-09-26 11:15:32 +0200269 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100270
271 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
272
273 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200274 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100275
276 if (type > 0 && type <= maxtype) {
277 if (policy) {
Johannes Bergc29f1842018-09-26 11:15:32 +0200278 int err = validate_nla(nla, maxtype, policy,
279 extack);
280
281 if (err < 0)
282 return err;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100283 }
284
Jan Engelhardt36546542010-11-16 09:52:32 -0800285 tb[type] = (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100286 }
287 }
288
289 if (unlikely(rem > 0))
Michal Schmidtbfc51842014-06-02 18:25:02 +0200290 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
291 rem, current->comm);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100292
Johannes Bergc29f1842018-09-26 11:15:32 +0200293 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100294}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700295EXPORT_SYMBOL(nla_parse);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100296
297/**
298 * nla_find - Find a specific attribute in a stream of attributes
299 * @head: head of attribute stream
300 * @len: length of attribute stream
301 * @attrtype: type of attribute to look for
302 *
303 * Returns the first attribute in the stream matching the specified type.
304 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800305struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100306{
Jan Engelhardt36546542010-11-16 09:52:32 -0800307 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100308 int rem;
309
310 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200311 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800312 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100313
314 return NULL;
315}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700316EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100317
318/**
319 * nla_strlcpy - Copy string attribute payload into a sized buffer
320 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700321 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100322 * @dstsize: size of destination buffer
323 *
324 * Copies at most dstsize - 1 bytes into the destination buffer.
325 * The result is always a valid NUL-terminated string. Unlike
326 * strlcpy the destination buffer is always padded out.
327 *
328 * Returns the length of the source buffer.
329 */
330size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
331{
332 size_t srclen = nla_len(nla);
333 char *src = nla_data(nla);
334
335 if (srclen > 0 && src[srclen - 1] == '\0')
336 srclen--;
337
338 if (dstsize > 0) {
339 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
340
341 memset(dst, 0, dstsize);
342 memcpy(dst, src, len);
343 }
344
345 return srclen;
346}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700347EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100348
349/**
Phil Sutter2cf0c8b2017-07-27 16:56:40 +0200350 * nla_strdup - Copy string attribute payload into a newly allocated buffer
351 * @nla: attribute to copy the string from
352 * @flags: the type of memory to allocate (see kmalloc).
353 *
354 * Returns a pointer to the allocated buffer or NULL on error.
355 */
356char *nla_strdup(const struct nlattr *nla, gfp_t flags)
357{
358 size_t srclen = nla_len(nla);
359 char *src = nla_data(nla), *dst;
360
361 if (srclen > 0 && src[srclen - 1] == '\0')
362 srclen--;
363
364 dst = kmalloc(srclen + 1, flags);
365 if (dst != NULL) {
366 memcpy(dst, src, srclen);
367 dst[srclen] = '\0';
368 }
369 return dst;
370}
371EXPORT_SYMBOL(nla_strdup);
372
373/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100374 * nla_memcpy - Copy a netlink attribute into another memory area
375 * @dest: where to copy to memcpy
376 * @src: netlink attribute to copy from
377 * @count: size of the destination area
378 *
379 * Note: The number of bytes copied is limited by the length of
380 * attribute's payload. memcpy
381 *
382 * Returns the number of bytes copied.
383 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700384int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100385{
386 int minlen = min_t(int, count, nla_len(src));
387
388 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200389 if (count > minlen)
390 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100391
392 return minlen;
393}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700394EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100395
396/**
397 * nla_memcmp - Compare an attribute with sized memory area
398 * @nla: netlink attribute
399 * @data: memory area
400 * @size: size of memory area
401 */
402int nla_memcmp(const struct nlattr *nla, const void *data,
403 size_t size)
404{
405 int d = nla_len(nla) - size;
406
407 if (d == 0)
408 d = memcmp(nla_data(nla), data, size);
409
410 return d;
411}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700412EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100413
414/**
415 * nla_strcmp - Compare a string attribute against a string
416 * @nla: netlink string attribute
417 * @str: another string
418 */
419int nla_strcmp(const struct nlattr *nla, const char *str)
420{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200421 int len = strlen(str);
422 char *buf = nla_data(nla);
423 int attrlen = nla_len(nla);
424 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100425
Pablo Neira8b7b9322014-04-01 19:38:44 +0200426 if (attrlen > 0 && buf[attrlen - 1] == '\0')
427 attrlen--;
428
429 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100430 if (d == 0)
431 d = memcmp(nla_data(nla), str, len);
432
433 return d;
434}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700435EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100436
Herbert Xu90800212009-03-11 23:18:32 +0800437#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100438/**
439 * __nla_reserve - reserve room for attribute on the skb
440 * @skb: socket buffer to reserve room on
441 * @attrtype: attribute type
442 * @attrlen: length of attribute payload
443 *
444 * Adds a netlink attribute header to a socket buffer and reserves
445 * room for the payload but does not copy it.
446 *
447 * The caller is responsible to ensure that the skb provides enough
448 * tailroom for the attribute header and payload.
449 */
450struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
451{
452 struct nlattr *nla;
453
Johannes Berg4df864c2017-06-16 14:29:21 +0200454 nla = skb_put(skb, nla_total_size(attrlen));
Thomas Grafbfa83a92005-11-10 02:25:51 +0100455 nla->nla_type = attrtype;
456 nla->nla_len = nla_attr_size(attrlen);
457
458 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
459
460 return nla;
461}
Herbert Xu90800212009-03-11 23:18:32 +0800462EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100463
464/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200465 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
466 * @skb: socket buffer to reserve room on
467 * @attrtype: attribute type
468 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200469 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200470 *
471 * Adds a netlink attribute header to a socket buffer and reserves
472 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200473 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200474 *
475 * The caller is responsible to ensure that the skb provides enough
476 * tailroom for the attribute header and payload.
477 */
478struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
479 int attrlen, int padattr)
480{
481 if (nla_need_padding_for_64bit(skb))
482 nla_align_64bit(skb, padattr);
483
484 return __nla_reserve(skb, attrtype, attrlen);
485}
486EXPORT_SYMBOL(__nla_reserve_64bit);
487
488/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700489 * __nla_reserve_nohdr - reserve room for attribute without header
490 * @skb: socket buffer to reserve room on
491 * @attrlen: length of attribute payload
492 *
493 * Reserves room for attribute payload without a header.
494 *
495 * The caller is responsible to ensure that the skb provides enough
496 * tailroom for the payload.
497 */
498void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
499{
yuan linyub952f4d2017-06-18 22:52:04 +0800500 return skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700501}
Herbert Xu90800212009-03-11 23:18:32 +0800502EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700503
504/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100505 * nla_reserve - reserve room for attribute on the skb
506 * @skb: socket buffer to reserve room on
507 * @attrtype: attribute type
508 * @attrlen: length of attribute payload
509 *
510 * Adds a netlink attribute header to a socket buffer and reserves
511 * room for the payload but does not copy it.
512 *
513 * Returns NULL if the tailroom of the skb is insufficient to store
514 * the attribute header and payload.
515 */
516struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
517{
518 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
519 return NULL;
520
521 return __nla_reserve(skb, attrtype, attrlen);
522}
Herbert Xu90800212009-03-11 23:18:32 +0800523EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100524
525/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200526 * nla_reserve_64bit - reserve room for attribute on the skb and align it
527 * @skb: socket buffer to reserve room on
528 * @attrtype: attribute type
529 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200530 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200531 *
532 * Adds a netlink attribute header to a socket buffer and reserves
533 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200534 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200535 *
536 * Returns NULL if the tailroom of the skb is insufficient to store
537 * the attribute header and payload.
538 */
539struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
540 int padattr)
541{
542 size_t len;
543
544 if (nla_need_padding_for_64bit(skb))
545 len = nla_total_size_64bit(attrlen);
546 else
547 len = nla_total_size(attrlen);
548 if (unlikely(skb_tailroom(skb) < len))
549 return NULL;
550
551 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
552}
553EXPORT_SYMBOL(nla_reserve_64bit);
554
555/**
Julius Volz10b595a2008-06-27 20:02:14 -0700556 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700557 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700558 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700559 *
560 * Reserves room for attribute payload without a header.
561 *
562 * Returns NULL if the tailroom of the skb is insufficient to store
563 * the attribute payload.
564 */
565void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
566{
567 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
568 return NULL;
569
570 return __nla_reserve_nohdr(skb, attrlen);
571}
Herbert Xu90800212009-03-11 23:18:32 +0800572EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700573
574/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100575 * __nla_put - Add a netlink attribute to a socket buffer
576 * @skb: socket buffer to add attribute to
577 * @attrtype: attribute type
578 * @attrlen: length of attribute payload
579 * @data: head of attribute payload
580 *
581 * The caller is responsible to ensure that the skb provides enough
582 * tailroom for the attribute header and payload.
583 */
584void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
585 const void *data)
586{
587 struct nlattr *nla;
588
589 nla = __nla_reserve(skb, attrtype, attrlen);
590 memcpy(nla_data(nla), data, attrlen);
591}
Herbert Xu90800212009-03-11 23:18:32 +0800592EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100593
Thomas Graffe4944e2006-08-04 23:03:05 -0700594/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200595 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
596 * @skb: socket buffer to add attribute to
597 * @attrtype: attribute type
598 * @attrlen: length of attribute payload
599 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200600 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200601 *
602 * The caller is responsible to ensure that the skb provides enough
603 * tailroom for the attribute header and payload.
604 */
605void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
606 const void *data, int padattr)
607{
608 struct nlattr *nla;
609
610 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
611 memcpy(nla_data(nla), data, attrlen);
612}
613EXPORT_SYMBOL(__nla_put_64bit);
614
615/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700616 * __nla_put_nohdr - Add a netlink attribute without header
617 * @skb: socket buffer to add attribute to
618 * @attrlen: length of attribute payload
619 * @data: head of attribute payload
620 *
621 * The caller is responsible to ensure that the skb provides enough
622 * tailroom for the attribute payload.
623 */
624void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
625{
626 void *start;
627
628 start = __nla_reserve_nohdr(skb, attrlen);
629 memcpy(start, data, attrlen);
630}
Herbert Xu90800212009-03-11 23:18:32 +0800631EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100632
633/**
634 * nla_put - Add a netlink attribute to a socket buffer
635 * @skb: socket buffer to add attribute to
636 * @attrtype: attribute type
637 * @attrlen: length of attribute payload
638 * @data: head of attribute payload
639 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700640 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100641 * the attribute header and payload.
642 */
643int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
644{
645 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700646 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100647
648 __nla_put(skb, attrtype, attrlen, data);
649 return 0;
650}
Herbert Xu90800212009-03-11 23:18:32 +0800651EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100652
Thomas Graffe4944e2006-08-04 23:03:05 -0700653/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200654 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
655 * @skb: socket buffer to add attribute to
656 * @attrtype: attribute type
657 * @attrlen: length of attribute payload
658 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200659 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200660 *
661 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
662 * the attribute header and payload.
663 */
664int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
665 const void *data, int padattr)
666{
667 size_t len;
668
669 if (nla_need_padding_for_64bit(skb))
670 len = nla_total_size_64bit(attrlen);
671 else
672 len = nla_total_size(attrlen);
673 if (unlikely(skb_tailroom(skb) < len))
674 return -EMSGSIZE;
675
676 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
677 return 0;
678}
679EXPORT_SYMBOL(nla_put_64bit);
680
681/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700682 * nla_put_nohdr - Add a netlink attribute without header
683 * @skb: socket buffer to add attribute to
684 * @attrlen: length of attribute payload
685 * @data: head of attribute payload
686 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700687 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700688 * the attribute payload.
689 */
690int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
691{
692 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700693 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700694
695 __nla_put_nohdr(skb, attrlen, data);
696 return 0;
697}
Herbert Xu90800212009-03-11 23:18:32 +0800698EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100699
Patrick McHardy01480e12008-01-22 22:10:59 -0800700/**
701 * nla_append - Add a netlink attribute without header or padding
702 * @skb: socket buffer to add attribute to
703 * @attrlen: length of attribute payload
704 * @data: head of attribute payload
705 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700706 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800707 * the attribute payload.
708 */
709int nla_append(struct sk_buff *skb, int attrlen, const void *data)
710{
711 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700712 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800713
Johannes Berg59ae1d12017-06-16 14:29:20 +0200714 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800715 return 0;
716}
Herbert Xu90800212009-03-11 23:18:32 +0800717EXPORT_SYMBOL(nla_append);
718#endif