blob: 3d8295c8550532cea0a6a50409deed23c8a2026b [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
Alexey Dobriyan32d84cd2016-11-19 03:59:07 +030018static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
Thomas Grafbfa83a92005-11-10 02:25:51 +010019 [NLA_U8] = sizeof(u8),
20 [NLA_U16] = sizeof(u16),
21 [NLA_U32] = sizeof(u32),
22 [NLA_U64] = sizeof(u64),
Johannes Bergc30bc942011-11-03 00:07:32 +000023 [NLA_MSECS] = sizeof(u64),
Thomas Grafbfa83a92005-11-10 02:25:51 +010024 [NLA_NESTED] = NLA_HDRLEN,
Julian Anastasov9eca2eb2012-08-25 22:47:57 +000025 [NLA_S8] = sizeof(s8),
26 [NLA_S16] = sizeof(s16),
27 [NLA_S32] = sizeof(s32),
28 [NLA_S64] = sizeof(s64),
Thomas Grafbfa83a92005-11-10 02:25:51 +010029};
30
Jamal Hadi Salim64c83d832017-07-30 13:24:49 -040031static int validate_nla_bitfield32(const struct nlattr *nla,
32 u32 *valid_flags_allowed)
33{
34 const struct nla_bitfield32 *bf = nla_data(nla);
35 u32 *valid_flags_mask = valid_flags_allowed;
36
37 if (!valid_flags_allowed)
38 return -EINVAL;
39
40 /*disallow invalid bit selector */
41 if (bf->selector & ~*valid_flags_mask)
42 return -EINVAL;
43
44 /*disallow invalid bit values */
45 if (bf->value & ~*valid_flags_mask)
46 return -EINVAL;
47
48 /*disallow valid bit values that are not selected*/
49 if (bf->value & ~bf->selector)
50 return -EINVAL;
51
52 return 0;
53}
54
Jan Engelhardt36546542010-11-16 09:52:32 -080055static int validate_nla(const struct nlattr *nla, int maxtype,
Patrick McHardyef7c79e2007-06-05 12:38:30 -070056 const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +010057{
Patrick McHardyef7c79e2007-06-05 12:38:30 -070058 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +020059 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +010060
Thomas Graf8f4c1f92007-09-12 14:44:36 +020061 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +010062 return 0;
63
Thomas Graf8f4c1f92007-09-12 14:44:36 +020064 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +010065
66 BUG_ON(pt->type > NLA_TYPE_MAX);
67
Thomas Grafa5531a52006-08-26 20:11:47 -070068 switch (pt->type) {
69 case NLA_FLAG:
70 if (attrlen > 0)
71 return -ERANGE;
72 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +010073
Jamal Hadi Salim64c83d832017-07-30 13:24:49 -040074 case NLA_BITFIELD32:
75 if (attrlen != sizeof(struct nla_bitfield32))
76 return -ERANGE;
77
78 return validate_nla_bitfield32(nla, pt->validation_data);
79
Thomas Grafa5531a52006-08-26 20:11:47 -070080 case NLA_NUL_STRING:
81 if (pt->len)
82 minlen = min_t(int, attrlen, pt->len + 1);
83 else
84 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +010085
Thomas Grafa5531a52006-08-26 20:11:47 -070086 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
87 return -EINVAL;
88 /* fall through */
89
90 case NLA_STRING:
91 if (attrlen < 1)
92 return -ERANGE;
93
94 if (pt->len) {
95 char *buf = nla_data(nla);
96
97 if (buf[attrlen - 1] == '\0')
98 attrlen--;
99
100 if (attrlen > pt->len)
101 return -ERANGE;
102 }
103 break;
104
Johannes Bergd30045a2007-03-23 11:37:48 -0700105 case NLA_BINARY:
106 if (pt->len && attrlen > pt->len)
107 return -ERANGE;
108 break;
109
Patrick McHardy1092cb22007-06-25 13:49:35 -0700110 case NLA_NESTED_COMPAT:
111 if (attrlen < pt->len)
112 return -ERANGE;
113 if (attrlen < NLA_ALIGN(pt->len))
114 break;
115 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
116 return -ERANGE;
117 nla = nla_data(nla) + NLA_ALIGN(pt->len);
118 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
119 return -ERANGE;
120 break;
Patrick McHardyea5693c2008-11-28 03:05:19 -0800121 case NLA_NESTED:
122 /* a nested attributes is allowed to be empty; if its not,
123 * it must have a size of at least NLA_HDRLEN.
124 */
125 if (attrlen == 0)
126 break;
Thomas Grafa5531a52006-08-26 20:11:47 -0700127 default:
128 if (pt->len)
129 minlen = pt->len;
130 else if (pt->type != NLA_UNSPEC)
131 minlen = nla_attr_minlen[pt->type];
132
133 if (attrlen < minlen)
134 return -ERANGE;
135 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100136
137 return 0;
138}
139
140/**
141 * nla_validate - Validate a stream of attributes
142 * @head: head of attribute stream
143 * @len: length of attribute stream
144 * @maxtype: maximum attribute type to be expected
145 * @policy: validation policy
Johannes Bergfceb6432017-04-12 14:34:07 +0200146 * @extack: extended ACK report struct
Thomas Grafbfa83a92005-11-10 02:25:51 +0100147 *
148 * Validates all attributes in the specified attribute stream against the
149 * specified policy. Attributes with a type exceeding maxtype will be
150 * ignored. See documenation of struct nla_policy for more details.
151 *
152 * Returns 0 on success or a negative error code.
153 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800154int nla_validate(const struct nlattr *head, int len, int maxtype,
Johannes Bergfceb6432017-04-12 14:34:07 +0200155 const struct nla_policy *policy,
156 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100157{
Jan Engelhardt36546542010-11-16 09:52:32 -0800158 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200159 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100160
161 nla_for_each_attr(nla, head, len, rem) {
Johannes Bergfceb6432017-04-12 14:34:07 +0200162 int err = validate_nla(nla, maxtype, policy);
163
164 if (err < 0) {
165 if (extack)
166 extack->bad_attr = nla;
167 return err;
168 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100169 }
170
Johannes Bergfceb6432017-04-12 14:34:07 +0200171 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100172}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700173EXPORT_SYMBOL(nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100174
175/**
Holger Eitzenbergere487eb92009-03-25 18:26:30 +0100176 * nla_policy_len - Determin the max. length of a policy
177 * @policy: policy to use
178 * @n: number of policies
179 *
180 * Determines the max. length of the policy. It is currently used
181 * to allocated Netlink buffers roughly the size of the actual
182 * message.
183 *
184 * Returns 0 on success or a negative error code.
185 */
186int
187nla_policy_len(const struct nla_policy *p, int n)
188{
189 int i, len = 0;
190
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800191 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb92009-03-25 18:26:30 +0100192 if (p->len)
193 len += nla_total_size(p->len);
194 else if (nla_attr_minlen[p->type])
195 len += nla_total_size(nla_attr_minlen[p->type]);
196 }
197
198 return len;
199}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700200EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb92009-03-25 18:26:30 +0100201
202/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100203 * nla_parse - Parse a stream of attributes into a tb buffer
204 * @tb: destination array with maxtype+1 elements
205 * @maxtype: maximum attribute type to be expected
206 * @head: head of attribute stream
207 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700208 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100209 *
210 * Parses a stream of attributes and stores a pointer to each attribute in
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400211 * the tb array accessible via the attribute type. Attributes with a type
Thomas Grafbfa83a92005-11-10 02:25:51 +0100212 * exceeding maxtype will be silently ignored for backwards compatibility
213 * reasons. policy may be set to NULL if no validation is required.
214 *
215 * Returns 0 on success or a negative error code.
216 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800217int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
Johannes Bergfceb6432017-04-12 14:34:07 +0200218 int len, const struct nla_policy *policy,
219 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100220{
Jan Engelhardt36546542010-11-16 09:52:32 -0800221 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100222 int rem, err;
223
224 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
225
226 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200227 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100228
229 if (type > 0 && type <= maxtype) {
230 if (policy) {
231 err = validate_nla(nla, maxtype, policy);
Johannes Bergfceb6432017-04-12 14:34:07 +0200232 if (err < 0) {
233 if (extack)
234 extack->bad_attr = nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100235 goto errout;
Johannes Bergfceb6432017-04-12 14:34:07 +0200236 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100237 }
238
Jan Engelhardt36546542010-11-16 09:52:32 -0800239 tb[type] = (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100240 }
241 }
242
243 if (unlikely(rem > 0))
Michal Schmidtbfc51842014-06-02 18:25:02 +0200244 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
245 rem, current->comm);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100246
247 err = 0;
248errout:
249 return err;
250}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700251EXPORT_SYMBOL(nla_parse);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100252
253/**
254 * nla_find - Find a specific attribute in a stream of attributes
255 * @head: head of attribute stream
256 * @len: length of attribute stream
257 * @attrtype: type of attribute to look for
258 *
259 * Returns the first attribute in the stream matching the specified type.
260 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800261struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100262{
Jan Engelhardt36546542010-11-16 09:52:32 -0800263 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100264 int rem;
265
266 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200267 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800268 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100269
270 return NULL;
271}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700272EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100273
274/**
275 * nla_strlcpy - Copy string attribute payload into a sized buffer
276 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700277 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100278 * @dstsize: size of destination buffer
279 *
280 * Copies at most dstsize - 1 bytes into the destination buffer.
281 * The result is always a valid NUL-terminated string. Unlike
282 * strlcpy the destination buffer is always padded out.
283 *
284 * Returns the length of the source buffer.
285 */
286size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
287{
288 size_t srclen = nla_len(nla);
289 char *src = nla_data(nla);
290
291 if (srclen > 0 && src[srclen - 1] == '\0')
292 srclen--;
293
294 if (dstsize > 0) {
295 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
296
297 memset(dst, 0, dstsize);
298 memcpy(dst, src, len);
299 }
300
301 return srclen;
302}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700303EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100304
305/**
Phil Sutter2cf0c8b2017-07-27 16:56:40 +0200306 * nla_strdup - Copy string attribute payload into a newly allocated buffer
307 * @nla: attribute to copy the string from
308 * @flags: the type of memory to allocate (see kmalloc).
309 *
310 * Returns a pointer to the allocated buffer or NULL on error.
311 */
312char *nla_strdup(const struct nlattr *nla, gfp_t flags)
313{
314 size_t srclen = nla_len(nla);
315 char *src = nla_data(nla), *dst;
316
317 if (srclen > 0 && src[srclen - 1] == '\0')
318 srclen--;
319
320 dst = kmalloc(srclen + 1, flags);
321 if (dst != NULL) {
322 memcpy(dst, src, srclen);
323 dst[srclen] = '\0';
324 }
325 return dst;
326}
327EXPORT_SYMBOL(nla_strdup);
328
329/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100330 * nla_memcpy - Copy a netlink attribute into another memory area
331 * @dest: where to copy to memcpy
332 * @src: netlink attribute to copy from
333 * @count: size of the destination area
334 *
335 * Note: The number of bytes copied is limited by the length of
336 * attribute's payload. memcpy
337 *
338 * Returns the number of bytes copied.
339 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700340int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100341{
342 int minlen = min_t(int, count, nla_len(src));
343
344 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200345 if (count > minlen)
346 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100347
348 return minlen;
349}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700350EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100351
352/**
353 * nla_memcmp - Compare an attribute with sized memory area
354 * @nla: netlink attribute
355 * @data: memory area
356 * @size: size of memory area
357 */
358int nla_memcmp(const struct nlattr *nla, const void *data,
359 size_t size)
360{
361 int d = nla_len(nla) - size;
362
363 if (d == 0)
364 d = memcmp(nla_data(nla), data, size);
365
366 return d;
367}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700368EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100369
370/**
371 * nla_strcmp - Compare a string attribute against a string
372 * @nla: netlink string attribute
373 * @str: another string
374 */
375int nla_strcmp(const struct nlattr *nla, const char *str)
376{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200377 int len = strlen(str);
378 char *buf = nla_data(nla);
379 int attrlen = nla_len(nla);
380 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100381
Pablo Neira8b7b9322014-04-01 19:38:44 +0200382 if (attrlen > 0 && buf[attrlen - 1] == '\0')
383 attrlen--;
384
385 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100386 if (d == 0)
387 d = memcmp(nla_data(nla), str, len);
388
389 return d;
390}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700391EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100392
Herbert Xu90800212009-03-11 23:18:32 +0800393#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100394/**
395 * __nla_reserve - reserve room for attribute on the skb
396 * @skb: socket buffer to reserve room on
397 * @attrtype: attribute type
398 * @attrlen: length of attribute payload
399 *
400 * Adds a netlink attribute header to a socket buffer and reserves
401 * room for the payload but does not copy it.
402 *
403 * The caller is responsible to ensure that the skb provides enough
404 * tailroom for the attribute header and payload.
405 */
406struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
407{
408 struct nlattr *nla;
409
Johannes Berg4df864c2017-06-16 14:29:21 +0200410 nla = skb_put(skb, nla_total_size(attrlen));
Thomas Grafbfa83a92005-11-10 02:25:51 +0100411 nla->nla_type = attrtype;
412 nla->nla_len = nla_attr_size(attrlen);
413
414 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
415
416 return nla;
417}
Herbert Xu90800212009-03-11 23:18:32 +0800418EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100419
420/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200421 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
422 * @skb: socket buffer to reserve room on
423 * @attrtype: attribute type
424 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200425 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200426 *
427 * Adds a netlink attribute header to a socket buffer and reserves
428 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200429 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200430 *
431 * The caller is responsible to ensure that the skb provides enough
432 * tailroom for the attribute header and payload.
433 */
434struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
435 int attrlen, int padattr)
436{
437 if (nla_need_padding_for_64bit(skb))
438 nla_align_64bit(skb, padattr);
439
440 return __nla_reserve(skb, attrtype, attrlen);
441}
442EXPORT_SYMBOL(__nla_reserve_64bit);
443
444/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700445 * __nla_reserve_nohdr - reserve room for attribute without header
446 * @skb: socket buffer to reserve room on
447 * @attrlen: length of attribute payload
448 *
449 * Reserves room for attribute payload without a header.
450 *
451 * The caller is responsible to ensure that the skb provides enough
452 * tailroom for the payload.
453 */
454void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
455{
yuan linyub952f4d2017-06-18 22:52:04 +0800456 return skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700457}
Herbert Xu90800212009-03-11 23:18:32 +0800458EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700459
460/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100461 * nla_reserve - reserve room for attribute on the skb
462 * @skb: socket buffer to reserve room on
463 * @attrtype: attribute type
464 * @attrlen: length of attribute payload
465 *
466 * Adds a netlink attribute header to a socket buffer and reserves
467 * room for the payload but does not copy it.
468 *
469 * Returns NULL if the tailroom of the skb is insufficient to store
470 * the attribute header and payload.
471 */
472struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
473{
474 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
475 return NULL;
476
477 return __nla_reserve(skb, attrtype, attrlen);
478}
Herbert Xu90800212009-03-11 23:18:32 +0800479EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100480
481/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200482 * nla_reserve_64bit - reserve room for attribute on the skb and align it
483 * @skb: socket buffer to reserve room on
484 * @attrtype: attribute type
485 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200486 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200487 *
488 * Adds a netlink attribute header to a socket buffer and reserves
489 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200490 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200491 *
492 * Returns NULL if the tailroom of the skb is insufficient to store
493 * the attribute header and payload.
494 */
495struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
496 int padattr)
497{
498 size_t len;
499
500 if (nla_need_padding_for_64bit(skb))
501 len = nla_total_size_64bit(attrlen);
502 else
503 len = nla_total_size(attrlen);
504 if (unlikely(skb_tailroom(skb) < len))
505 return NULL;
506
507 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
508}
509EXPORT_SYMBOL(nla_reserve_64bit);
510
511/**
Julius Volz10b595a2008-06-27 20:02:14 -0700512 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700513 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700514 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700515 *
516 * Reserves room for attribute payload without a header.
517 *
518 * Returns NULL if the tailroom of the skb is insufficient to store
519 * the attribute payload.
520 */
521void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
522{
523 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
524 return NULL;
525
526 return __nla_reserve_nohdr(skb, attrlen);
527}
Herbert Xu90800212009-03-11 23:18:32 +0800528EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700529
530/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100531 * __nla_put - Add a netlink attribute to a socket buffer
532 * @skb: socket buffer to add attribute to
533 * @attrtype: attribute type
534 * @attrlen: length of attribute payload
535 * @data: head of attribute payload
536 *
537 * The caller is responsible to ensure that the skb provides enough
538 * tailroom for the attribute header and payload.
539 */
540void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
541 const void *data)
542{
543 struct nlattr *nla;
544
545 nla = __nla_reserve(skb, attrtype, attrlen);
546 memcpy(nla_data(nla), data, attrlen);
547}
Herbert Xu90800212009-03-11 23:18:32 +0800548EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100549
Thomas Graffe4944e2006-08-04 23:03:05 -0700550/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200551 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
552 * @skb: socket buffer to add attribute to
553 * @attrtype: attribute type
554 * @attrlen: length of attribute payload
555 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200556 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200557 *
558 * The caller is responsible to ensure that the skb provides enough
559 * tailroom for the attribute header and payload.
560 */
561void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
562 const void *data, int padattr)
563{
564 struct nlattr *nla;
565
566 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
567 memcpy(nla_data(nla), data, attrlen);
568}
569EXPORT_SYMBOL(__nla_put_64bit);
570
571/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700572 * __nla_put_nohdr - Add a netlink attribute without header
573 * @skb: socket buffer to add attribute to
574 * @attrlen: length of attribute payload
575 * @data: head of attribute payload
576 *
577 * The caller is responsible to ensure that the skb provides enough
578 * tailroom for the attribute payload.
579 */
580void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
581{
582 void *start;
583
584 start = __nla_reserve_nohdr(skb, attrlen);
585 memcpy(start, data, attrlen);
586}
Herbert Xu90800212009-03-11 23:18:32 +0800587EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100588
589/**
590 * nla_put - Add a netlink attribute to a socket buffer
591 * @skb: socket buffer to add attribute to
592 * @attrtype: attribute type
593 * @attrlen: length of attribute payload
594 * @data: head of attribute payload
595 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700596 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100597 * the attribute header and payload.
598 */
599int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
600{
601 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700602 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100603
604 __nla_put(skb, attrtype, attrlen, data);
605 return 0;
606}
Herbert Xu90800212009-03-11 23:18:32 +0800607EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100608
Thomas Graffe4944e2006-08-04 23:03:05 -0700609/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200610 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
611 * @skb: socket buffer to add attribute to
612 * @attrtype: attribute type
613 * @attrlen: length of attribute payload
614 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200615 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200616 *
617 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
618 * the attribute header and payload.
619 */
620int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
621 const void *data, int padattr)
622{
623 size_t len;
624
625 if (nla_need_padding_for_64bit(skb))
626 len = nla_total_size_64bit(attrlen);
627 else
628 len = nla_total_size(attrlen);
629 if (unlikely(skb_tailroom(skb) < len))
630 return -EMSGSIZE;
631
632 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
633 return 0;
634}
635EXPORT_SYMBOL(nla_put_64bit);
636
637/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700638 * nla_put_nohdr - Add a netlink attribute without header
639 * @skb: socket buffer to add attribute to
640 * @attrlen: length of attribute payload
641 * @data: head of attribute payload
642 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700643 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700644 * the attribute payload.
645 */
646int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
647{
648 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700649 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700650
651 __nla_put_nohdr(skb, attrlen, data);
652 return 0;
653}
Herbert Xu90800212009-03-11 23:18:32 +0800654EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100655
Patrick McHardy01480e12008-01-22 22:10:59 -0800656/**
657 * nla_append - Add a netlink attribute without header or padding
658 * @skb: socket buffer to add attribute to
659 * @attrlen: length of attribute payload
660 * @data: head of attribute payload
661 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700662 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800663 * the attribute payload.
664 */
665int nla_append(struct sk_buff *skb, int attrlen, const void *data)
666{
667 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700668 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800669
Johannes Berg59ae1d12017-06-16 14:29:20 +0200670 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800671 return 0;
672}
Herbert Xu90800212009-03-11 23:18:32 +0800673EXPORT_SYMBOL(nla_append);
674#endif