blob: 36d74b07915159d9ed153bcd0aeb65e4a8f07778 [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,
48 u32 *valid_flags_allowed)
49{
50 const struct nla_bitfield32 *bf = nla_data(nla);
51 u32 *valid_flags_mask = valid_flags_allowed;
52
53 if (!valid_flags_allowed)
54 return -EINVAL;
55
56 /*disallow invalid bit selector */
57 if (bf->selector & ~*valid_flags_mask)
58 return -EINVAL;
59
60 /*disallow invalid bit values */
61 if (bf->value & ~*valid_flags_mask)
62 return -EINVAL;
63
64 /*disallow valid bit values that are not selected*/
65 if (bf->value & ~bf->selector)
66 return -EINVAL;
67
68 return 0;
69}
70
Jan Engelhardt36546542010-11-16 09:52:32 -080071static int validate_nla(const struct nlattr *nla, int maxtype,
Johannes Berg568b7422018-09-17 11:57:28 +020072 const struct nla_policy *policy,
73 const char **error_msg)
Thomas Grafbfa83a92005-11-10 02:25:51 +010074{
Patrick McHardyef7c79e2007-06-05 12:38:30 -070075 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +020076 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
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
David Ahern6e237d02017-12-06 20:09:12 -080085 if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) {
86 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 Berg568b7422018-09-17 11:57:28 +020091 case NLA_REJECT:
92 if (pt->validation_data && error_msg)
93 *error_msg = pt->validation_data;
94 return -EINVAL;
95
Thomas Grafa5531a52006-08-26 20:11:47 -070096 case NLA_FLAG:
97 if (attrlen > 0)
98 return -ERANGE;
99 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100100
Jamal Hadi Salim64c83d82017-07-30 13:24:49 -0400101 case NLA_BITFIELD32:
102 if (attrlen != sizeof(struct nla_bitfield32))
103 return -ERANGE;
104
105 return validate_nla_bitfield32(nla, pt->validation_data);
106
Thomas Grafa5531a52006-08-26 20:11:47 -0700107 case NLA_NUL_STRING:
108 if (pt->len)
109 minlen = min_t(int, attrlen, pt->len + 1);
110 else
111 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100112
Thomas Grafa5531a52006-08-26 20:11:47 -0700113 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
114 return -EINVAL;
115 /* fall through */
116
117 case NLA_STRING:
118 if (attrlen < 1)
119 return -ERANGE;
120
121 if (pt->len) {
122 char *buf = nla_data(nla);
123
124 if (buf[attrlen - 1] == '\0')
125 attrlen--;
126
127 if (attrlen > pt->len)
128 return -ERANGE;
129 }
130 break;
131
Johannes Bergd30045a2007-03-23 11:37:48 -0700132 case NLA_BINARY:
133 if (pt->len && attrlen > pt->len)
134 return -ERANGE;
135 break;
136
Patrick McHardy1092cb22007-06-25 13:49:35 -0700137 case NLA_NESTED_COMPAT:
138 if (attrlen < pt->len)
139 return -ERANGE;
140 if (attrlen < NLA_ALIGN(pt->len))
141 break;
142 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
143 return -ERANGE;
144 nla = nla_data(nla) + NLA_ALIGN(pt->len);
145 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
146 return -ERANGE;
147 break;
Patrick McHardyea5693c2008-11-28 03:05:19 -0800148 case NLA_NESTED:
149 /* a nested attributes is allowed to be empty; if its not,
150 * it must have a size of at least NLA_HDRLEN.
151 */
152 if (attrlen == 0)
153 break;
Thomas Grafa5531a52006-08-26 20:11:47 -0700154 default:
155 if (pt->len)
156 minlen = pt->len;
157 else if (pt->type != NLA_UNSPEC)
158 minlen = nla_attr_minlen[pt->type];
159
160 if (attrlen < minlen)
161 return -ERANGE;
162 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100163
164 return 0;
165}
166
167/**
168 * nla_validate - Validate a stream of attributes
169 * @head: head of attribute stream
170 * @len: length of attribute stream
171 * @maxtype: maximum attribute type to be expected
172 * @policy: validation policy
Johannes Bergfceb6432017-04-12 14:34:07 +0200173 * @extack: extended ACK report struct
Thomas Grafbfa83a92005-11-10 02:25:51 +0100174 *
175 * Validates all attributes in the specified attribute stream against the
176 * specified policy. Attributes with a type exceeding maxtype will be
177 * ignored. See documenation of struct nla_policy for more details.
178 *
179 * Returns 0 on success or a negative error code.
180 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800181int nla_validate(const struct nlattr *head, int len, int maxtype,
Johannes Bergfceb6432017-04-12 14:34:07 +0200182 const struct nla_policy *policy,
183 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100184{
Jan Engelhardt36546542010-11-16 09:52:32 -0800185 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200186 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100187
188 nla_for_each_attr(nla, head, len, rem) {
Johannes Berg568b7422018-09-17 11:57:28 +0200189 int err = validate_nla(nla, maxtype, policy, NULL);
Johannes Bergfceb6432017-04-12 14:34:07 +0200190
191 if (err < 0) {
Johannes Berg568b7422018-09-17 11:57:28 +0200192 NL_SET_BAD_ATTR(extack, nla);
Johannes Bergfceb6432017-04-12 14:34:07 +0200193 return err;
194 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100195 }
196
Johannes Bergfceb6432017-04-12 14:34:07 +0200197 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100198}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700199EXPORT_SYMBOL(nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100200
201/**
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100202 * nla_policy_len - Determin the max. length of a policy
203 * @policy: policy to use
204 * @n: number of policies
205 *
206 * Determines the max. length of the policy. It is currently used
207 * to allocated Netlink buffers roughly the size of the actual
208 * message.
209 *
210 * Returns 0 on success or a negative error code.
211 */
212int
213nla_policy_len(const struct nla_policy *p, int n)
214{
215 int i, len = 0;
216
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800217 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100218 if (p->len)
219 len += nla_total_size(p->len);
David Ahern28033ae2017-11-07 21:59:40 -0800220 else if (nla_attr_len[p->type])
221 len += nla_total_size(nla_attr_len[p->type]);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100222 else if (nla_attr_minlen[p->type])
223 len += nla_total_size(nla_attr_minlen[p->type]);
224 }
225
226 return len;
227}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700228EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb992009-03-25 18:26:30 +0100229
230/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100231 * nla_parse - Parse a stream of attributes into a tb buffer
232 * @tb: destination array with maxtype+1 elements
233 * @maxtype: maximum attribute type to be expected
234 * @head: head of attribute stream
235 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700236 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100237 *
238 * Parses a stream of attributes and stores a pointer to each attribute in
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400239 * the tb array accessible via the attribute type. Attributes with a type
Thomas Grafbfa83a92005-11-10 02:25:51 +0100240 * exceeding maxtype will be silently ignored for backwards compatibility
241 * reasons. policy may be set to NULL if no validation is required.
242 *
243 * Returns 0 on success or a negative error code.
244 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800245int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
Johannes Bergfceb6432017-04-12 14:34:07 +0200246 int len, const struct nla_policy *policy,
247 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100248{
Jan Engelhardt36546542010-11-16 09:52:32 -0800249 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100250 int rem, err;
251
252 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
253
254 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200255 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100256
257 if (type > 0 && type <= maxtype) {
Johannes Berg568b7422018-09-17 11:57:28 +0200258 static const char _msg[] = "Attribute failed policy validation";
259 const char *msg = _msg;
260
Thomas Grafbfa83a92005-11-10 02:25:51 +0100261 if (policy) {
Johannes Berg568b7422018-09-17 11:57:28 +0200262 err = validate_nla(nla, maxtype, policy, &msg);
Johannes Bergfceb6432017-04-12 14:34:07 +0200263 if (err < 0) {
Johannes Berg568b7422018-09-17 11:57:28 +0200264 NL_SET_BAD_ATTR(extack, nla);
265 if (extack)
266 extack->_msg = msg;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100267 goto errout;
Johannes Bergfceb6432017-04-12 14:34:07 +0200268 }
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
279 err = 0;
280errout:
281 return err;
282}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700283EXPORT_SYMBOL(nla_parse);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100284
285/**
286 * nla_find - Find a specific attribute in a stream of attributes
287 * @head: head of attribute stream
288 * @len: length of attribute stream
289 * @attrtype: type of attribute to look for
290 *
291 * Returns the first attribute in the stream matching the specified type.
292 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800293struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100294{
Jan Engelhardt36546542010-11-16 09:52:32 -0800295 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100296 int rem;
297
298 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200299 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800300 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100301
302 return NULL;
303}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700304EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100305
306/**
307 * nla_strlcpy - Copy string attribute payload into a sized buffer
308 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700309 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100310 * @dstsize: size of destination buffer
311 *
312 * Copies at most dstsize - 1 bytes into the destination buffer.
313 * The result is always a valid NUL-terminated string. Unlike
314 * strlcpy the destination buffer is always padded out.
315 *
316 * Returns the length of the source buffer.
317 */
318size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
319{
320 size_t srclen = nla_len(nla);
321 char *src = nla_data(nla);
322
323 if (srclen > 0 && src[srclen - 1] == '\0')
324 srclen--;
325
326 if (dstsize > 0) {
327 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
328
329 memset(dst, 0, dstsize);
330 memcpy(dst, src, len);
331 }
332
333 return srclen;
334}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700335EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100336
337/**
Phil Sutter2cf0c8b2017-07-27 16:56:40 +0200338 * nla_strdup - Copy string attribute payload into a newly allocated buffer
339 * @nla: attribute to copy the string from
340 * @flags: the type of memory to allocate (see kmalloc).
341 *
342 * Returns a pointer to the allocated buffer or NULL on error.
343 */
344char *nla_strdup(const struct nlattr *nla, gfp_t flags)
345{
346 size_t srclen = nla_len(nla);
347 char *src = nla_data(nla), *dst;
348
349 if (srclen > 0 && src[srclen - 1] == '\0')
350 srclen--;
351
352 dst = kmalloc(srclen + 1, flags);
353 if (dst != NULL) {
354 memcpy(dst, src, srclen);
355 dst[srclen] = '\0';
356 }
357 return dst;
358}
359EXPORT_SYMBOL(nla_strdup);
360
361/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100362 * nla_memcpy - Copy a netlink attribute into another memory area
363 * @dest: where to copy to memcpy
364 * @src: netlink attribute to copy from
365 * @count: size of the destination area
366 *
367 * Note: The number of bytes copied is limited by the length of
368 * attribute's payload. memcpy
369 *
370 * Returns the number of bytes copied.
371 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700372int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100373{
374 int minlen = min_t(int, count, nla_len(src));
375
376 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200377 if (count > minlen)
378 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100379
380 return minlen;
381}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700382EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100383
384/**
385 * nla_memcmp - Compare an attribute with sized memory area
386 * @nla: netlink attribute
387 * @data: memory area
388 * @size: size of memory area
389 */
390int nla_memcmp(const struct nlattr *nla, const void *data,
391 size_t size)
392{
393 int d = nla_len(nla) - size;
394
395 if (d == 0)
396 d = memcmp(nla_data(nla), data, size);
397
398 return d;
399}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700400EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100401
402/**
403 * nla_strcmp - Compare a string attribute against a string
404 * @nla: netlink string attribute
405 * @str: another string
406 */
407int nla_strcmp(const struct nlattr *nla, const char *str)
408{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200409 int len = strlen(str);
410 char *buf = nla_data(nla);
411 int attrlen = nla_len(nla);
412 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100413
Pablo Neira8b7b9322014-04-01 19:38:44 +0200414 if (attrlen > 0 && buf[attrlen - 1] == '\0')
415 attrlen--;
416
417 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100418 if (d == 0)
419 d = memcmp(nla_data(nla), str, len);
420
421 return d;
422}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700423EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100424
Herbert Xu90800212009-03-11 23:18:32 +0800425#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100426/**
427 * __nla_reserve - reserve room for attribute on the skb
428 * @skb: socket buffer to reserve room on
429 * @attrtype: attribute type
430 * @attrlen: length of attribute payload
431 *
432 * Adds a netlink attribute header to a socket buffer and reserves
433 * room for the payload but does not copy it.
434 *
435 * The caller is responsible to ensure that the skb provides enough
436 * tailroom for the attribute header and payload.
437 */
438struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
439{
440 struct nlattr *nla;
441
Johannes Berg4df864c2017-06-16 14:29:21 +0200442 nla = skb_put(skb, nla_total_size(attrlen));
Thomas Grafbfa83a92005-11-10 02:25:51 +0100443 nla->nla_type = attrtype;
444 nla->nla_len = nla_attr_size(attrlen);
445
446 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
447
448 return nla;
449}
Herbert Xu90800212009-03-11 23:18:32 +0800450EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100451
452/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200453 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
454 * @skb: socket buffer to reserve room on
455 * @attrtype: attribute type
456 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200457 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200458 *
459 * Adds a netlink attribute header to a socket buffer and reserves
460 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200461 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200462 *
463 * The caller is responsible to ensure that the skb provides enough
464 * tailroom for the attribute header and payload.
465 */
466struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
467 int attrlen, int padattr)
468{
469 if (nla_need_padding_for_64bit(skb))
470 nla_align_64bit(skb, padattr);
471
472 return __nla_reserve(skb, attrtype, attrlen);
473}
474EXPORT_SYMBOL(__nla_reserve_64bit);
475
476/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700477 * __nla_reserve_nohdr - reserve room for attribute without header
478 * @skb: socket buffer to reserve room on
479 * @attrlen: length of attribute payload
480 *
481 * Reserves room for attribute payload without a header.
482 *
483 * The caller is responsible to ensure that the skb provides enough
484 * tailroom for the payload.
485 */
486void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
487{
yuan linyub952f4d2017-06-18 22:52:04 +0800488 return skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700489}
Herbert Xu90800212009-03-11 23:18:32 +0800490EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700491
492/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100493 * nla_reserve - reserve room for attribute on the skb
494 * @skb: socket buffer to reserve room on
495 * @attrtype: attribute type
496 * @attrlen: length of attribute payload
497 *
498 * Adds a netlink attribute header to a socket buffer and reserves
499 * room for the payload but does not copy it.
500 *
501 * Returns NULL if the tailroom of the skb is insufficient to store
502 * the attribute header and payload.
503 */
504struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
505{
506 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
507 return NULL;
508
509 return __nla_reserve(skb, attrtype, attrlen);
510}
Herbert Xu90800212009-03-11 23:18:32 +0800511EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100512
513/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200514 * nla_reserve_64bit - reserve room for attribute on the skb and align it
515 * @skb: socket buffer to reserve room on
516 * @attrtype: attribute type
517 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200518 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200519 *
520 * Adds a netlink attribute header to a socket buffer and reserves
521 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200522 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200523 *
524 * Returns NULL if the tailroom of the skb is insufficient to store
525 * the attribute header and payload.
526 */
527struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
528 int padattr)
529{
530 size_t len;
531
532 if (nla_need_padding_for_64bit(skb))
533 len = nla_total_size_64bit(attrlen);
534 else
535 len = nla_total_size(attrlen);
536 if (unlikely(skb_tailroom(skb) < len))
537 return NULL;
538
539 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
540}
541EXPORT_SYMBOL(nla_reserve_64bit);
542
543/**
Julius Volz10b595a2008-06-27 20:02:14 -0700544 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700545 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700546 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700547 *
548 * Reserves room for attribute payload without a header.
549 *
550 * Returns NULL if the tailroom of the skb is insufficient to store
551 * the attribute payload.
552 */
553void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
554{
555 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
556 return NULL;
557
558 return __nla_reserve_nohdr(skb, attrlen);
559}
Herbert Xu90800212009-03-11 23:18:32 +0800560EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700561
562/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100563 * __nla_put - Add a netlink attribute to a socket buffer
564 * @skb: socket buffer to add attribute to
565 * @attrtype: attribute type
566 * @attrlen: length of attribute payload
567 * @data: head of attribute payload
568 *
569 * The caller is responsible to ensure that the skb provides enough
570 * tailroom for the attribute header and payload.
571 */
572void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
573 const void *data)
574{
575 struct nlattr *nla;
576
577 nla = __nla_reserve(skb, attrtype, attrlen);
578 memcpy(nla_data(nla), data, attrlen);
579}
Herbert Xu90800212009-03-11 23:18:32 +0800580EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100581
Thomas Graffe4944e2006-08-04 23:03:05 -0700582/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200583 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
584 * @skb: socket buffer to add attribute to
585 * @attrtype: attribute type
586 * @attrlen: length of attribute payload
587 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200588 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200589 *
590 * The caller is responsible to ensure that the skb provides enough
591 * tailroom for the attribute header and payload.
592 */
593void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
594 const void *data, int padattr)
595{
596 struct nlattr *nla;
597
598 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
599 memcpy(nla_data(nla), data, attrlen);
600}
601EXPORT_SYMBOL(__nla_put_64bit);
602
603/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700604 * __nla_put_nohdr - Add a netlink attribute without header
605 * @skb: socket buffer to add attribute to
606 * @attrlen: length of attribute payload
607 * @data: head of attribute payload
608 *
609 * The caller is responsible to ensure that the skb provides enough
610 * tailroom for the attribute payload.
611 */
612void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
613{
614 void *start;
615
616 start = __nla_reserve_nohdr(skb, attrlen);
617 memcpy(start, data, attrlen);
618}
Herbert Xu90800212009-03-11 23:18:32 +0800619EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100620
621/**
622 * nla_put - Add a netlink attribute to a socket buffer
623 * @skb: socket buffer to add attribute to
624 * @attrtype: attribute type
625 * @attrlen: length of attribute payload
626 * @data: head of attribute payload
627 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700628 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100629 * the attribute header and payload.
630 */
631int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
632{
633 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700634 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100635
636 __nla_put(skb, attrtype, attrlen, data);
637 return 0;
638}
Herbert Xu90800212009-03-11 23:18:32 +0800639EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100640
Thomas Graffe4944e2006-08-04 23:03:05 -0700641/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200642 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
643 * @skb: socket buffer to add attribute to
644 * @attrtype: attribute type
645 * @attrlen: length of attribute payload
646 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200647 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200648 *
649 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
650 * the attribute header and payload.
651 */
652int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
653 const void *data, int padattr)
654{
655 size_t len;
656
657 if (nla_need_padding_for_64bit(skb))
658 len = nla_total_size_64bit(attrlen);
659 else
660 len = nla_total_size(attrlen);
661 if (unlikely(skb_tailroom(skb) < len))
662 return -EMSGSIZE;
663
664 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
665 return 0;
666}
667EXPORT_SYMBOL(nla_put_64bit);
668
669/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700670 * nla_put_nohdr - Add a netlink attribute without header
671 * @skb: socket buffer to add attribute to
672 * @attrlen: length of attribute payload
673 * @data: head of attribute payload
674 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700675 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700676 * the attribute payload.
677 */
678int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
679{
680 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700681 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700682
683 __nla_put_nohdr(skb, attrlen, data);
684 return 0;
685}
Herbert Xu90800212009-03-11 23:18:32 +0800686EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100687
Patrick McHardy01480e12008-01-22 22:10:59 -0800688/**
689 * nla_append - Add a netlink attribute without header or padding
690 * @skb: socket buffer to add attribute to
691 * @attrlen: length of attribute payload
692 * @data: head of attribute payload
693 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700694 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800695 * the attribute payload.
696 */
697int nla_append(struct sk_buff *skb, int attrlen, const void *data)
698{
699 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700700 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800701
Johannes Berg59ae1d12017-06-16 14:29:20 +0200702 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800703 return 0;
704}
Herbert Xu90800212009-03-11 23:18:32 +0800705EXPORT_SYMBOL(nla_append);
706#endif