blob: e2e5b38394d51c4cd10a2ce288f91c438774d1a8 [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,
72 const char **error_msg)
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);
Thomas Grafbfa83a92005-11-10 02:25:51 +010076
Thomas Graf8f4c1f92007-09-12 14:44:36 +020077 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +010078 return 0;
79
Thomas Graf8f4c1f92007-09-12 14:44:36 +020080 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +010081
82 BUG_ON(pt->type > NLA_TYPE_MAX);
83
Johannes Bergb60b87f2018-09-17 11:57:29 +020084 if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) ||
85 (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) {
David Ahern6e237d02017-12-06 20:09:12 -080086 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
87 current->comm, type);
David Ahern28033ae2017-11-07 21:59:40 -080088 }
89
Thomas Grafa5531a52006-08-26 20:11:47 -070090 switch (pt->type) {
Johannes Bergb60b87f2018-09-17 11:57:29 +020091 case NLA_EXACT_LEN:
92 if (attrlen != pt->len)
93 return -ERANGE;
94 break;
95
Johannes Berg568b7422018-09-17 11:57:28 +020096 case NLA_REJECT:
97 if (pt->validation_data && error_msg)
98 *error_msg = pt->validation_data;
99 return -EINVAL;
100
Thomas Grafa5531a52006-08-26 20:11:47 -0700101 case NLA_FLAG:
102 if (attrlen > 0)
103 return -ERANGE;
104 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100105
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400106 case NLA_BITFIELD32:
107 if (attrlen != sizeof(struct nla_bitfield32))
108 return -ERANGE;
109
110 return validate_nla_bitfield32(nla, pt->validation_data);
111
Thomas Grafa5531a52006-08-26 20:11:47 -0700112 case NLA_NUL_STRING:
113 if (pt->len)
114 minlen = min_t(int, attrlen, pt->len + 1);
115 else
116 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100117
Thomas Grafa5531a52006-08-26 20:11:47 -0700118 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
119 return -EINVAL;
120 /* fall through */
121
122 case NLA_STRING:
123 if (attrlen < 1)
124 return -ERANGE;
125
126 if (pt->len) {
127 char *buf = nla_data(nla);
128
129 if (buf[attrlen - 1] == '\0')
130 attrlen--;
131
132 if (attrlen > pt->len)
133 return -ERANGE;
134 }
135 break;
136
Johannes Bergd30045a2007-03-23 11:37:48 -0700137 case NLA_BINARY:
138 if (pt->len && attrlen > pt->len)
139 return -ERANGE;
140 break;
141
Patrick McHardyea5693c2008-11-28 03:05:19 -0800142 case NLA_NESTED:
143 /* a nested attributes is allowed to be empty; if its not,
144 * it must have a size of at least NLA_HDRLEN.
145 */
146 if (attrlen == 0)
147 break;
Thomas Grafa5531a52006-08-26 20:11:47 -0700148 default:
149 if (pt->len)
150 minlen = pt->len;
151 else if (pt->type != NLA_UNSPEC)
152 minlen = nla_attr_minlen[pt->type];
153
154 if (attrlen < minlen)
155 return -ERANGE;
156 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100157
158 return 0;
159}
160
161/**
162 * nla_validate - Validate a stream of attributes
163 * @head: head of attribute stream
164 * @len: length of attribute stream
165 * @maxtype: maximum attribute type to be expected
166 * @policy: validation policy
Johannes Bergfceb6432017-04-12 14:34:07 +0200167 * @extack: extended ACK report struct
Thomas Grafbfa83a92005-11-10 02:25:51 +0100168 *
169 * Validates all attributes in the specified attribute stream against the
170 * specified policy. Attributes with a type exceeding maxtype will be
171 * ignored. See documenation of struct nla_policy for more details.
172 *
173 * Returns 0 on success or a negative error code.
174 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800175int nla_validate(const struct nlattr *head, int len, int maxtype,
Johannes Bergfceb6432017-04-12 14:34:07 +0200176 const struct nla_policy *policy,
177 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100178{
Jan Engelhardt36546542010-11-16 09:52:32 -0800179 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200180 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100181
182 nla_for_each_attr(nla, head, len, rem) {
Johannes Berg568b7422018-09-17 11:57:28 +0200183 int err = validate_nla(nla, maxtype, policy, NULL);
Johannes Bergfceb6432017-04-12 14:34:07 +0200184
185 if (err < 0) {
Johannes Berg568b7422018-09-17 11:57:28 +0200186 NL_SET_BAD_ATTR(extack, nla);
Johannes Bergfceb6432017-04-12 14:34:07 +0200187 return err;
188 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100189 }
190
Johannes Bergfceb6432017-04-12 14:34:07 +0200191 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100192}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700193EXPORT_SYMBOL(nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100194
195/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100196 * nla_policy_len - Determin the max. length of a policy
197 * @policy: policy to use
198 * @n: number of policies
199 *
200 * Determines the max. length of the policy. It is currently used
201 * to allocated Netlink buffers roughly the size of the actual
202 * message.
203 *
204 * Returns 0 on success or a negative error code.
205 */
206int
207nla_policy_len(const struct nla_policy *p, int n)
208{
209 int i, len = 0;
210
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800211 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100212 if (p->len)
213 len += nla_total_size(p->len);
David Ahern28033ae2017-11-07 21:59:40 -0800214 else if (nla_attr_len[p->type])
215 len += nla_total_size(nla_attr_len[p->type]);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100216 else if (nla_attr_minlen[p->type])
217 len += nla_total_size(nla_attr_minlen[p->type]);
218 }
219
220 return len;
221}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700222EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100223
224/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100225 * nla_parse - Parse a stream of attributes into a tb buffer
226 * @tb: destination array with maxtype+1 elements
227 * @maxtype: maximum attribute type to be expected
228 * @head: head of attribute stream
229 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700230 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100231 *
232 * Parses a stream of attributes and stores a pointer to each attribute in
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400233 * the tb array accessible via the attribute type. Attributes with a type
Thomas Grafbfa83a92005-11-10 02:25:51 +0100234 * exceeding maxtype will be silently ignored for backwards compatibility
235 * reasons. policy may be set to NULL if no validation is required.
236 *
237 * Returns 0 on success or a negative error code.
238 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800239int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
Johannes Bergfceb6432017-04-12 14:34:07 +0200240 int len, const struct nla_policy *policy,
241 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100242{
Jan Engelhardt36546542010-11-16 09:52:32 -0800243 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100244 int rem, err;
245
246 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
247
248 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200249 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100250
251 if (type > 0 && type <= maxtype) {
Johannes Berg568b7422018-09-17 11:57:28 +0200252 static const char _msg[] = "Attribute failed policy validation";
253 const char *msg = _msg;
254
Thomas Grafbfa83a92005-11-10 02:25:51 +0100255 if (policy) {
Johannes Berg568b7422018-09-17 11:57:28 +0200256 err = validate_nla(nla, maxtype, policy, &msg);
Johannes Bergfceb6432017-04-12 14:34:07 +0200257 if (err < 0) {
Johannes Berg568b7422018-09-17 11:57:28 +0200258 NL_SET_BAD_ATTR(extack, nla);
259 if (extack)
260 extack->_msg = msg;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100261 goto errout;
Johannes Bergfceb6432017-04-12 14:34:07 +0200262 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100263 }
264
Jan Engelhardt36546542010-11-16 09:52:32 -0800265 tb[type] = (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100266 }
267 }
268
269 if (unlikely(rem > 0))
Michal Schmidtbfc51842014-06-02 18:25:02 +0200270 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
271 rem, current->comm);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100272
273 err = 0;
274errout:
275 return err;
276}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700277EXPORT_SYMBOL(nla_parse);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100278
279/**
280 * nla_find - Find a specific attribute in a stream of attributes
281 * @head: head of attribute stream
282 * @len: length of attribute stream
283 * @attrtype: type of attribute to look for
284 *
285 * Returns the first attribute in the stream matching the specified type.
286 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800287struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100288{
Jan Engelhardt36546542010-11-16 09:52:32 -0800289 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100290 int rem;
291
292 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200293 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800294 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100295
296 return NULL;
297}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700298EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100299
300/**
301 * nla_strlcpy - Copy string attribute payload into a sized buffer
302 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700303 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100304 * @dstsize: size of destination buffer
305 *
306 * Copies at most dstsize - 1 bytes into the destination buffer.
307 * The result is always a valid NUL-terminated string. Unlike
308 * strlcpy the destination buffer is always padded out.
309 *
310 * Returns the length of the source buffer.
311 */
312size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
313{
314 size_t srclen = nla_len(nla);
315 char *src = nla_data(nla);
316
317 if (srclen > 0 && src[srclen - 1] == '\0')
318 srclen--;
319
320 if (dstsize > 0) {
321 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
322
323 memset(dst, 0, dstsize);
324 memcpy(dst, src, len);
325 }
326
327 return srclen;
328}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700329EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100330
331/**
Phil Sutter2cf0c8b2017-07-27 16:56:40 +0200332 * nla_strdup - Copy string attribute payload into a newly allocated buffer
333 * @nla: attribute to copy the string from
334 * @flags: the type of memory to allocate (see kmalloc).
335 *
336 * Returns a pointer to the allocated buffer or NULL on error.
337 */
338char *nla_strdup(const struct nlattr *nla, gfp_t flags)
339{
340 size_t srclen = nla_len(nla);
341 char *src = nla_data(nla), *dst;
342
343 if (srclen > 0 && src[srclen - 1] == '\0')
344 srclen--;
345
346 dst = kmalloc(srclen + 1, flags);
347 if (dst != NULL) {
348 memcpy(dst, src, srclen);
349 dst[srclen] = '\0';
350 }
351 return dst;
352}
353EXPORT_SYMBOL(nla_strdup);
354
355/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100356 * nla_memcpy - Copy a netlink attribute into another memory area
357 * @dest: where to copy to memcpy
358 * @src: netlink attribute to copy from
359 * @count: size of the destination area
360 *
361 * Note: The number of bytes copied is limited by the length of
362 * attribute's payload. memcpy
363 *
364 * Returns the number of bytes copied.
365 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700366int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100367{
368 int minlen = min_t(int, count, nla_len(src));
369
370 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200371 if (count > minlen)
372 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100373
374 return minlen;
375}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700376EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100377
378/**
379 * nla_memcmp - Compare an attribute with sized memory area
380 * @nla: netlink attribute
381 * @data: memory area
382 * @size: size of memory area
383 */
384int nla_memcmp(const struct nlattr *nla, const void *data,
385 size_t size)
386{
387 int d = nla_len(nla) - size;
388
389 if (d == 0)
390 d = memcmp(nla_data(nla), data, size);
391
392 return d;
393}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700394EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100395
396/**
397 * nla_strcmp - Compare a string attribute against a string
398 * @nla: netlink string attribute
399 * @str: another string
400 */
401int nla_strcmp(const struct nlattr *nla, const char *str)
402{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200403 int len = strlen(str);
404 char *buf = nla_data(nla);
405 int attrlen = nla_len(nla);
406 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100407
Pablo Neira8b7b9322014-04-01 19:38:44 +0200408 if (attrlen > 0 && buf[attrlen - 1] == '\0')
409 attrlen--;
410
411 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100412 if (d == 0)
413 d = memcmp(nla_data(nla), str, len);
414
415 return d;
416}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700417EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100418
Herbert Xu90800212009-03-11 23:18:32 +0800419#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100420/**
421 * __nla_reserve - reserve room for attribute on the skb
422 * @skb: socket buffer to reserve room on
423 * @attrtype: attribute type
424 * @attrlen: length of attribute payload
425 *
426 * Adds a netlink attribute header to a socket buffer and reserves
427 * room for the payload but does not copy it.
428 *
429 * The caller is responsible to ensure that the skb provides enough
430 * tailroom for the attribute header and payload.
431 */
432struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
433{
434 struct nlattr *nla;
435
Johannes Berg4df864c2017-06-16 14:29:21 +0200436 nla = skb_put(skb, nla_total_size(attrlen));
Thomas Grafbfa83a92005-11-10 02:25:51 +0100437 nla->nla_type = attrtype;
438 nla->nla_len = nla_attr_size(attrlen);
439
440 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
441
442 return nla;
443}
Herbert Xu90800212009-03-11 23:18:32 +0800444EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100445
446/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200447 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
448 * @skb: socket buffer to reserve room on
449 * @attrtype: attribute type
450 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200451 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200452 *
453 * Adds a netlink attribute header to a socket buffer and reserves
454 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200455 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200456 *
457 * The caller is responsible to ensure that the skb provides enough
458 * tailroom for the attribute header and payload.
459 */
460struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
461 int attrlen, int padattr)
462{
463 if (nla_need_padding_for_64bit(skb))
464 nla_align_64bit(skb, padattr);
465
466 return __nla_reserve(skb, attrtype, attrlen);
467}
468EXPORT_SYMBOL(__nla_reserve_64bit);
469
470/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700471 * __nla_reserve_nohdr - reserve room for attribute without header
472 * @skb: socket buffer to reserve room on
473 * @attrlen: length of attribute payload
474 *
475 * Reserves room for attribute payload without a header.
476 *
477 * The caller is responsible to ensure that the skb provides enough
478 * tailroom for the payload.
479 */
480void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
481{
yuan linyub952f4d2017-06-18 22:52:04 +0800482 return skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700483}
Herbert Xu90800212009-03-11 23:18:32 +0800484EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700485
486/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100487 * nla_reserve - reserve room for attribute on the skb
488 * @skb: socket buffer to reserve room on
489 * @attrtype: attribute type
490 * @attrlen: length of attribute payload
491 *
492 * Adds a netlink attribute header to a socket buffer and reserves
493 * room for the payload but does not copy it.
494 *
495 * Returns NULL if the tailroom of the skb is insufficient to store
496 * the attribute header and payload.
497 */
498struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
499{
500 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
501 return NULL;
502
503 return __nla_reserve(skb, attrtype, attrlen);
504}
Herbert Xu90800212009-03-11 23:18:32 +0800505EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100506
507/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200508 * nla_reserve_64bit - reserve room for attribute on the skb and align it
509 * @skb: socket buffer to reserve room on
510 * @attrtype: attribute type
511 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200512 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200513 *
514 * Adds a netlink attribute header to a socket buffer and reserves
515 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200516 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200517 *
518 * Returns NULL if the tailroom of the skb is insufficient to store
519 * the attribute header and payload.
520 */
521struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
522 int padattr)
523{
524 size_t len;
525
526 if (nla_need_padding_for_64bit(skb))
527 len = nla_total_size_64bit(attrlen);
528 else
529 len = nla_total_size(attrlen);
530 if (unlikely(skb_tailroom(skb) < len))
531 return NULL;
532
533 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
534}
535EXPORT_SYMBOL(nla_reserve_64bit);
536
537/**
Julius Volz10b595a2008-06-27 20:02:14 -0700538 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700539 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700540 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700541 *
542 * Reserves room for attribute payload without a header.
543 *
544 * Returns NULL if the tailroom of the skb is insufficient to store
545 * the attribute payload.
546 */
547void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
548{
549 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
550 return NULL;
551
552 return __nla_reserve_nohdr(skb, attrlen);
553}
Herbert Xu90800212009-03-11 23:18:32 +0800554EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700555
556/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100557 * __nla_put - Add a netlink attribute to a socket buffer
558 * @skb: socket buffer to add attribute to
559 * @attrtype: attribute type
560 * @attrlen: length of attribute payload
561 * @data: head of attribute payload
562 *
563 * The caller is responsible to ensure that the skb provides enough
564 * tailroom for the attribute header and payload.
565 */
566void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
567 const void *data)
568{
569 struct nlattr *nla;
570
571 nla = __nla_reserve(skb, attrtype, attrlen);
572 memcpy(nla_data(nla), data, attrlen);
573}
Herbert Xu90800212009-03-11 23:18:32 +0800574EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100575
Thomas Graffe4944e2006-08-04 23:03:05 -0700576/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200577 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
578 * @skb: socket buffer to add attribute to
579 * @attrtype: attribute type
580 * @attrlen: length of attribute payload
581 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200582 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200583 *
584 * The caller is responsible to ensure that the skb provides enough
585 * tailroom for the attribute header and payload.
586 */
587void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
588 const void *data, int padattr)
589{
590 struct nlattr *nla;
591
592 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
593 memcpy(nla_data(nla), data, attrlen);
594}
595EXPORT_SYMBOL(__nla_put_64bit);
596
597/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700598 * __nla_put_nohdr - Add a netlink attribute without header
599 * @skb: socket buffer to add attribute to
600 * @attrlen: length of attribute payload
601 * @data: head of attribute payload
602 *
603 * The caller is responsible to ensure that the skb provides enough
604 * tailroom for the attribute payload.
605 */
606void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
607{
608 void *start;
609
610 start = __nla_reserve_nohdr(skb, attrlen);
611 memcpy(start, data, attrlen);
612}
Herbert Xu90800212009-03-11 23:18:32 +0800613EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100614
615/**
616 * nla_put - Add a netlink attribute to a socket buffer
617 * @skb: socket buffer to add attribute to
618 * @attrtype: attribute type
619 * @attrlen: length of attribute payload
620 * @data: head of attribute payload
621 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700622 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100623 * the attribute header and payload.
624 */
625int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
626{
627 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700628 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100629
630 __nla_put(skb, attrtype, attrlen, data);
631 return 0;
632}
Herbert Xu90800212009-03-11 23:18:32 +0800633EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100634
Thomas Graffe4944e2006-08-04 23:03:05 -0700635/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200636 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
637 * @skb: socket buffer to add attribute to
638 * @attrtype: attribute type
639 * @attrlen: length of attribute payload
640 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200641 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200642 *
643 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
644 * the attribute header and payload.
645 */
646int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
647 const void *data, int padattr)
648{
649 size_t len;
650
651 if (nla_need_padding_for_64bit(skb))
652 len = nla_total_size_64bit(attrlen);
653 else
654 len = nla_total_size(attrlen);
655 if (unlikely(skb_tailroom(skb) < len))
656 return -EMSGSIZE;
657
658 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
659 return 0;
660}
661EXPORT_SYMBOL(nla_put_64bit);
662
663/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700664 * nla_put_nohdr - Add a netlink attribute without header
665 * @skb: socket buffer to add attribute to
666 * @attrlen: length of attribute payload
667 * @data: head of attribute payload
668 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700669 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700670 * the attribute payload.
671 */
672int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
673{
674 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700675 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700676
677 __nla_put_nohdr(skb, attrlen, data);
678 return 0;
679}
Herbert Xu90800212009-03-11 23:18:32 +0800680EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100681
Patrick McHardy01480e12008-01-22 22:10:59 -0800682/**
683 * nla_append - Add a netlink attribute without header or padding
684 * @skb: socket buffer to add attribute to
685 * @attrlen: length of attribute payload
686 * @data: head of attribute payload
687 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700688 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800689 * the attribute payload.
690 */
691int nla_append(struct sk_buff *skb, int attrlen, const void *data)
692{
693 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700694 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800695
Johannes Berg59ae1d12017-06-16 14:29:20 +0200696 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800697 return 0;
698}
Herbert Xu90800212009-03-11 23:18:32 +0800699EXPORT_SYMBOL(nla_append);
700#endif