blob: ab15a6c095d3d30c1cf2840f56259ee488e9a724 [file] [log] [blame]
Thomas Grafbfa83a92005-11-10 02:25:51 +01001/*
2 * NETLINK Netlink attributes
3 *
4 * Authors: Thomas Graf <tgraf@suug.ch>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 */
7
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05008#include <linux/export.h>
Thomas Grafbfa83a92005-11-10 02:25:51 +01009#include <linux/kernel.h>
10#include <linux/errno.h>
11#include <linux/jiffies.h>
Thomas Grafbfa83a92005-11-10 02:25:51 +010012#include <linux/skbuff.h>
13#include <linux/string.h>
14#include <linux/types.h>
15#include <net/netlink.h>
16
Alexey Dobriyan32d84cd2016-11-19 03:59:07 +030017static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
Thomas Grafbfa83a92005-11-10 02:25:51 +010018 [NLA_U8] = sizeof(u8),
19 [NLA_U16] = sizeof(u16),
20 [NLA_U32] = sizeof(u32),
21 [NLA_U64] = sizeof(u64),
Johannes Bergc30bc942011-11-03 00:07:32 +000022 [NLA_MSECS] = sizeof(u64),
Thomas Grafbfa83a92005-11-10 02:25:51 +010023 [NLA_NESTED] = NLA_HDRLEN,
Julian Anastasov9eca2eb2012-08-25 22:47:57 +000024 [NLA_S8] = sizeof(s8),
25 [NLA_S16] = sizeof(s16),
26 [NLA_S32] = sizeof(s32),
27 [NLA_S64] = sizeof(s64),
Thomas Grafbfa83a92005-11-10 02:25:51 +010028};
29
Jan Engelhardt36546542010-11-16 09:52:32 -080030static int validate_nla(const struct nlattr *nla, int maxtype,
Patrick McHardyef7c79e2007-06-05 12:38:30 -070031 const struct nla_policy *policy)
Thomas Grafbfa83a92005-11-10 02:25:51 +010032{
Patrick McHardyef7c79e2007-06-05 12:38:30 -070033 const struct nla_policy *pt;
Thomas Graf8f4c1f92007-09-12 14:44:36 +020034 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +010035
Thomas Graf8f4c1f92007-09-12 14:44:36 +020036 if (type <= 0 || type > maxtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +010037 return 0;
38
Thomas Graf8f4c1f92007-09-12 14:44:36 +020039 pt = &policy[type];
Thomas Grafbfa83a92005-11-10 02:25:51 +010040
41 BUG_ON(pt->type > NLA_TYPE_MAX);
42
Thomas Grafa5531a52006-08-26 20:11:47 -070043 switch (pt->type) {
44 case NLA_FLAG:
45 if (attrlen > 0)
46 return -ERANGE;
47 break;
Thomas Grafbfa83a92005-11-10 02:25:51 +010048
Thomas Grafa5531a52006-08-26 20:11:47 -070049 case NLA_NUL_STRING:
50 if (pt->len)
51 minlen = min_t(int, attrlen, pt->len + 1);
52 else
53 minlen = attrlen;
Thomas Grafbfa83a92005-11-10 02:25:51 +010054
Thomas Grafa5531a52006-08-26 20:11:47 -070055 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
56 return -EINVAL;
57 /* fall through */
58
59 case NLA_STRING:
60 if (attrlen < 1)
61 return -ERANGE;
62
63 if (pt->len) {
64 char *buf = nla_data(nla);
65
66 if (buf[attrlen - 1] == '\0')
67 attrlen--;
68
69 if (attrlen > pt->len)
70 return -ERANGE;
71 }
72 break;
73
Johannes Bergd30045a2007-03-23 11:37:48 -070074 case NLA_BINARY:
75 if (pt->len && attrlen > pt->len)
76 return -ERANGE;
77 break;
78
Patrick McHardy1092cb22007-06-25 13:49:35 -070079 case NLA_NESTED_COMPAT:
80 if (attrlen < pt->len)
81 return -ERANGE;
82 if (attrlen < NLA_ALIGN(pt->len))
83 break;
84 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
85 return -ERANGE;
86 nla = nla_data(nla) + NLA_ALIGN(pt->len);
87 if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
88 return -ERANGE;
89 break;
Patrick McHardyea5693c2008-11-28 03:05:19 -080090 case NLA_NESTED:
91 /* a nested attributes is allowed to be empty; if its not,
92 * it must have a size of at least NLA_HDRLEN.
93 */
94 if (attrlen == 0)
95 break;
Thomas Grafa5531a52006-08-26 20:11:47 -070096 default:
97 if (pt->len)
98 minlen = pt->len;
99 else if (pt->type != NLA_UNSPEC)
100 minlen = nla_attr_minlen[pt->type];
101
102 if (attrlen < minlen)
103 return -ERANGE;
104 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100105
106 return 0;
107}
108
109/**
110 * nla_validate - Validate a stream of attributes
111 * @head: head of attribute stream
112 * @len: length of attribute stream
113 * @maxtype: maximum attribute type to be expected
114 * @policy: validation policy
Johannes Bergfceb6432017-04-12 14:34:07 +0200115 * @extack: extended ACK report struct
Thomas Grafbfa83a92005-11-10 02:25:51 +0100116 *
117 * Validates all attributes in the specified attribute stream against the
118 * specified policy. Attributes with a type exceeding maxtype will be
119 * ignored. See documenation of struct nla_policy for more details.
120 *
121 * Returns 0 on success or a negative error code.
122 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800123int nla_validate(const struct nlattr *head, int len, int maxtype,
Johannes Bergfceb6432017-04-12 14:34:07 +0200124 const struct nla_policy *policy,
125 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100126{
Jan Engelhardt36546542010-11-16 09:52:32 -0800127 const struct nlattr *nla;
Johannes Bergfceb6432017-04-12 14:34:07 +0200128 int rem;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100129
130 nla_for_each_attr(nla, head, len, rem) {
Johannes Bergfceb6432017-04-12 14:34:07 +0200131 int err = validate_nla(nla, maxtype, policy);
132
133 if (err < 0) {
134 if (extack)
135 extack->bad_attr = nla;
136 return err;
137 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100138 }
139
Johannes Bergfceb6432017-04-12 14:34:07 +0200140 return 0;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100141}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700142EXPORT_SYMBOL(nla_validate);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100143
144/**
Holger Eitzenbergere487eb92009-03-25 18:26:30 +0100145 * nla_policy_len - Determin the max. length of a policy
146 * @policy: policy to use
147 * @n: number of policies
148 *
149 * Determines the max. length of the policy. It is currently used
150 * to allocated Netlink buffers roughly the size of the actual
151 * message.
152 *
153 * Returns 0 on success or a negative error code.
154 */
155int
156nla_policy_len(const struct nla_policy *p, int n)
157{
158 int i, len = 0;
159
Lars Ellenberge3fa3af2011-02-28 12:38:25 -0800160 for (i = 0; i < n; i++, p++) {
Holger Eitzenbergere487eb92009-03-25 18:26:30 +0100161 if (p->len)
162 len += nla_total_size(p->len);
163 else if (nla_attr_minlen[p->type])
164 len += nla_total_size(nla_attr_minlen[p->type]);
165 }
166
167 return len;
168}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700169EXPORT_SYMBOL(nla_policy_len);
Holger Eitzenbergere487eb92009-03-25 18:26:30 +0100170
171/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100172 * nla_parse - Parse a stream of attributes into a tb buffer
173 * @tb: destination array with maxtype+1 elements
174 * @maxtype: maximum attribute type to be expected
175 * @head: head of attribute stream
176 * @len: length of attribute stream
Julius Volz10b595a2008-06-27 20:02:14 -0700177 * @policy: validation policy
Thomas Grafbfa83a92005-11-10 02:25:51 +0100178 *
179 * Parses a stream of attributes and stores a pointer to each attribute in
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400180 * the tb array accessible via the attribute type. Attributes with a type
Thomas Grafbfa83a92005-11-10 02:25:51 +0100181 * exceeding maxtype will be silently ignored for backwards compatibility
182 * reasons. policy may be set to NULL if no validation is required.
183 *
184 * Returns 0 on success or a negative error code.
185 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800186int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
Johannes Bergfceb6432017-04-12 14:34:07 +0200187 int len, const struct nla_policy *policy,
188 struct netlink_ext_ack *extack)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100189{
Jan Engelhardt36546542010-11-16 09:52:32 -0800190 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100191 int rem, err;
192
193 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
194
195 nla_for_each_attr(nla, head, len, rem) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200196 u16 type = nla_type(nla);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100197
198 if (type > 0 && type <= maxtype) {
199 if (policy) {
200 err = validate_nla(nla, maxtype, policy);
Johannes Bergfceb6432017-04-12 14:34:07 +0200201 if (err < 0) {
202 if (extack)
203 extack->bad_attr = nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100204 goto errout;
Johannes Bergfceb6432017-04-12 14:34:07 +0200205 }
Thomas Grafbfa83a92005-11-10 02:25:51 +0100206 }
207
Jan Engelhardt36546542010-11-16 09:52:32 -0800208 tb[type] = (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100209 }
210 }
211
212 if (unlikely(rem > 0))
Michal Schmidtbfc51842014-06-02 18:25:02 +0200213 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
214 rem, current->comm);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100215
216 err = 0;
217errout:
218 return err;
219}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700220EXPORT_SYMBOL(nla_parse);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100221
222/**
223 * nla_find - Find a specific attribute in a stream of attributes
224 * @head: head of attribute stream
225 * @len: length of attribute stream
226 * @attrtype: type of attribute to look for
227 *
228 * Returns the first attribute in the stream matching the specified type.
229 */
Jan Engelhardt36546542010-11-16 09:52:32 -0800230struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100231{
Jan Engelhardt36546542010-11-16 09:52:32 -0800232 const struct nlattr *nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100233 int rem;
234
235 nla_for_each_attr(nla, head, len, rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200236 if (nla_type(nla) == attrtype)
Jan Engelhardt36546542010-11-16 09:52:32 -0800237 return (struct nlattr *)nla;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100238
239 return NULL;
240}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700241EXPORT_SYMBOL(nla_find);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100242
243/**
244 * nla_strlcpy - Copy string attribute payload into a sized buffer
245 * @dst: where to copy the string to
Julius Volz10b595a2008-06-27 20:02:14 -0700246 * @nla: attribute to copy the string from
Thomas Grafbfa83a92005-11-10 02:25:51 +0100247 * @dstsize: size of destination buffer
248 *
249 * Copies at most dstsize - 1 bytes into the destination buffer.
250 * The result is always a valid NUL-terminated string. Unlike
251 * strlcpy the destination buffer is always padded out.
252 *
253 * Returns the length of the source buffer.
254 */
255size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
256{
257 size_t srclen = nla_len(nla);
258 char *src = nla_data(nla);
259
260 if (srclen > 0 && src[srclen - 1] == '\0')
261 srclen--;
262
263 if (dstsize > 0) {
264 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
265
266 memset(dst, 0, dstsize);
267 memcpy(dst, src, len);
268 }
269
270 return srclen;
271}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700272EXPORT_SYMBOL(nla_strlcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100273
274/**
275 * nla_memcpy - Copy a netlink attribute into another memory area
276 * @dest: where to copy to memcpy
277 * @src: netlink attribute to copy from
278 * @count: size of the destination area
279 *
280 * Note: The number of bytes copied is limited by the length of
281 * attribute's payload. memcpy
282 *
283 * Returns the number of bytes copied.
284 */
Patrick McHardyb057efd2008-10-28 11:59:11 -0700285int nla_memcpy(void *dest, const struct nlattr *src, int count)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100286{
287 int minlen = min_t(int, count, nla_len(src));
288
289 memcpy(dest, nla_data(src), minlen);
Jiri Benc5899f042015-03-29 16:05:28 +0200290 if (count > minlen)
291 memset(dest + minlen, 0, count - minlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100292
293 return minlen;
294}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700295EXPORT_SYMBOL(nla_memcpy);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100296
297/**
298 * nla_memcmp - Compare an attribute with sized memory area
299 * @nla: netlink attribute
300 * @data: memory area
301 * @size: size of memory area
302 */
303int nla_memcmp(const struct nlattr *nla, const void *data,
304 size_t size)
305{
306 int d = nla_len(nla) - size;
307
308 if (d == 0)
309 d = memcmp(nla_data(nla), data, size);
310
311 return d;
312}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700313EXPORT_SYMBOL(nla_memcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100314
315/**
316 * nla_strcmp - Compare a string attribute against a string
317 * @nla: netlink string attribute
318 * @str: another string
319 */
320int nla_strcmp(const struct nlattr *nla, const char *str)
321{
Pablo Neira8b7b9322014-04-01 19:38:44 +0200322 int len = strlen(str);
323 char *buf = nla_data(nla);
324 int attrlen = nla_len(nla);
325 int d;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100326
Pablo Neira8b7b9322014-04-01 19:38:44 +0200327 if (attrlen > 0 && buf[attrlen - 1] == '\0')
328 attrlen--;
329
330 d = attrlen - len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100331 if (d == 0)
332 d = memcmp(nla_data(nla), str, len);
333
334 return d;
335}
Fabian Frederick6d6a1382014-06-04 16:11:57 -0700336EXPORT_SYMBOL(nla_strcmp);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100337
Herbert Xu90800212009-03-11 23:18:32 +0800338#ifdef CONFIG_NET
Thomas Grafbfa83a92005-11-10 02:25:51 +0100339/**
340 * __nla_reserve - reserve room for attribute on the skb
341 * @skb: socket buffer to reserve room on
342 * @attrtype: attribute type
343 * @attrlen: length of attribute payload
344 *
345 * Adds a netlink attribute header to a socket buffer and reserves
346 * room for the payload but does not copy it.
347 *
348 * The caller is responsible to ensure that the skb provides enough
349 * tailroom for the attribute header and payload.
350 */
351struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
352{
353 struct nlattr *nla;
354
355 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
356 nla->nla_type = attrtype;
357 nla->nla_len = nla_attr_size(attrlen);
358
359 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
360
361 return nla;
362}
Herbert Xu90800212009-03-11 23:18:32 +0800363EXPORT_SYMBOL(__nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100364
365/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200366 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
367 * @skb: socket buffer to reserve room on
368 * @attrtype: attribute type
369 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200370 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200371 *
372 * Adds a netlink attribute header to a socket buffer and reserves
373 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200374 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200375 *
376 * The caller is responsible to ensure that the skb provides enough
377 * tailroom for the attribute header and payload.
378 */
379struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
380 int attrlen, int padattr)
381{
382 if (nla_need_padding_for_64bit(skb))
383 nla_align_64bit(skb, padattr);
384
385 return __nla_reserve(skb, attrtype, attrlen);
386}
387EXPORT_SYMBOL(__nla_reserve_64bit);
388
389/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700390 * __nla_reserve_nohdr - reserve room for attribute without header
391 * @skb: socket buffer to reserve room on
392 * @attrlen: length of attribute payload
393 *
394 * Reserves room for attribute payload without a header.
395 *
396 * The caller is responsible to ensure that the skb provides enough
397 * tailroom for the payload.
398 */
399void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
400{
401 void *start;
402
Johannes Bergaa9f9792017-06-13 14:28:18 +0200403 start = skb_put_zero(skb, NLA_ALIGN(attrlen));
Thomas Graffe4944e2006-08-04 23:03:05 -0700404
405 return start;
406}
Herbert Xu90800212009-03-11 23:18:32 +0800407EXPORT_SYMBOL(__nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700408
409/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100410 * nla_reserve - reserve room for attribute on the skb
411 * @skb: socket buffer to reserve room on
412 * @attrtype: attribute type
413 * @attrlen: length of attribute payload
414 *
415 * Adds a netlink attribute header to a socket buffer and reserves
416 * room for the payload but does not copy it.
417 *
418 * Returns NULL if the tailroom of the skb is insufficient to store
419 * the attribute header and payload.
420 */
421struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
422{
423 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
424 return NULL;
425
426 return __nla_reserve(skb, attrtype, attrlen);
427}
Herbert Xu90800212009-03-11 23:18:32 +0800428EXPORT_SYMBOL(nla_reserve);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100429
430/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200431 * nla_reserve_64bit - reserve room for attribute on the skb and align it
432 * @skb: socket buffer to reserve room on
433 * @attrtype: attribute type
434 * @attrlen: length of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200435 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200436 *
437 * Adds a netlink attribute header to a socket buffer and reserves
438 * room for the payload but does not copy it. It also ensure that this
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200439 * attribute will have a 64-bit aligned nla_data() area.
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200440 *
441 * Returns NULL if the tailroom of the skb is insufficient to store
442 * the attribute header and payload.
443 */
444struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
445 int padattr)
446{
447 size_t len;
448
449 if (nla_need_padding_for_64bit(skb))
450 len = nla_total_size_64bit(attrlen);
451 else
452 len = nla_total_size(attrlen);
453 if (unlikely(skb_tailroom(skb) < len))
454 return NULL;
455
456 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
457}
458EXPORT_SYMBOL(nla_reserve_64bit);
459
460/**
Julius Volz10b595a2008-06-27 20:02:14 -0700461 * nla_reserve_nohdr - reserve room for attribute without header
Thomas Graffe4944e2006-08-04 23:03:05 -0700462 * @skb: socket buffer to reserve room on
Julius Volz10b595a2008-06-27 20:02:14 -0700463 * @attrlen: length of attribute payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700464 *
465 * Reserves room for attribute payload without a header.
466 *
467 * Returns NULL if the tailroom of the skb is insufficient to store
468 * the attribute payload.
469 */
470void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
471{
472 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
473 return NULL;
474
475 return __nla_reserve_nohdr(skb, attrlen);
476}
Herbert Xu90800212009-03-11 23:18:32 +0800477EXPORT_SYMBOL(nla_reserve_nohdr);
Thomas Graffe4944e2006-08-04 23:03:05 -0700478
479/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100480 * __nla_put - Add a netlink attribute to a socket buffer
481 * @skb: socket buffer to add attribute to
482 * @attrtype: attribute type
483 * @attrlen: length of attribute payload
484 * @data: head of attribute payload
485 *
486 * The caller is responsible to ensure that the skb provides enough
487 * tailroom for the attribute header and payload.
488 */
489void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
490 const void *data)
491{
492 struct nlattr *nla;
493
494 nla = __nla_reserve(skb, attrtype, attrlen);
495 memcpy(nla_data(nla), data, attrlen);
496}
Herbert Xu90800212009-03-11 23:18:32 +0800497EXPORT_SYMBOL(__nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100498
Thomas Graffe4944e2006-08-04 23:03:05 -0700499/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200500 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
501 * @skb: socket buffer to add attribute to
502 * @attrtype: attribute type
503 * @attrlen: length of attribute payload
504 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200505 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200506 *
507 * The caller is responsible to ensure that the skb provides enough
508 * tailroom for the attribute header and payload.
509 */
510void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
511 const void *data, int padattr)
512{
513 struct nlattr *nla;
514
515 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
516 memcpy(nla_data(nla), data, attrlen);
517}
518EXPORT_SYMBOL(__nla_put_64bit);
519
520/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700521 * __nla_put_nohdr - Add a netlink attribute without header
522 * @skb: socket buffer to add attribute to
523 * @attrlen: length of attribute payload
524 * @data: head of attribute payload
525 *
526 * The caller is responsible to ensure that the skb provides enough
527 * tailroom for the attribute payload.
528 */
529void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
530{
531 void *start;
532
533 start = __nla_reserve_nohdr(skb, attrlen);
534 memcpy(start, data, attrlen);
535}
Herbert Xu90800212009-03-11 23:18:32 +0800536EXPORT_SYMBOL(__nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100537
538/**
539 * nla_put - Add a netlink attribute to a socket buffer
540 * @skb: socket buffer to add attribute to
541 * @attrtype: attribute type
542 * @attrlen: length of attribute payload
543 * @data: head of attribute payload
544 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700545 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Grafbfa83a92005-11-10 02:25:51 +0100546 * the attribute header and payload.
547 */
548int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
549{
550 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700551 return -EMSGSIZE;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100552
553 __nla_put(skb, attrtype, attrlen, data);
554 return 0;
555}
Herbert Xu90800212009-03-11 23:18:32 +0800556EXPORT_SYMBOL(nla_put);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100557
Thomas Graffe4944e2006-08-04 23:03:05 -0700558/**
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200559 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
560 * @skb: socket buffer to add attribute to
561 * @attrtype: attribute type
562 * @attrlen: length of attribute payload
563 * @data: head of attribute payload
Nicolas Dichtel11a99572016-04-22 17:31:16 +0200564 * @padattr: attribute type for the padding
Nicolas Dichtel089bf1a2016-04-21 18:58:24 +0200565 *
566 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
567 * the attribute header and payload.
568 */
569int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
570 const void *data, int padattr)
571{
572 size_t len;
573
574 if (nla_need_padding_for_64bit(skb))
575 len = nla_total_size_64bit(attrlen);
576 else
577 len = nla_total_size(attrlen);
578 if (unlikely(skb_tailroom(skb) < len))
579 return -EMSGSIZE;
580
581 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
582 return 0;
583}
584EXPORT_SYMBOL(nla_put_64bit);
585
586/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700587 * nla_put_nohdr - Add a netlink attribute without header
588 * @skb: socket buffer to add attribute to
589 * @attrlen: length of attribute payload
590 * @data: head of attribute payload
591 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700592 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Thomas Graffe4944e2006-08-04 23:03:05 -0700593 * the attribute payload.
594 */
595int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
596{
597 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700598 return -EMSGSIZE;
Thomas Graffe4944e2006-08-04 23:03:05 -0700599
600 __nla_put_nohdr(skb, attrlen, data);
601 return 0;
602}
Herbert Xu90800212009-03-11 23:18:32 +0800603EXPORT_SYMBOL(nla_put_nohdr);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100604
Patrick McHardy01480e12008-01-22 22:10:59 -0800605/**
606 * nla_append - Add a netlink attribute without header or padding
607 * @skb: socket buffer to add attribute to
608 * @attrlen: length of attribute payload
609 * @data: head of attribute payload
610 *
Thomas Grafbc3ed282008-06-03 16:36:54 -0700611 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
Patrick McHardy01480e12008-01-22 22:10:59 -0800612 * the attribute payload.
613 */
614int nla_append(struct sk_buff *skb, int attrlen, const void *data)
615{
616 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
Thomas Grafbc3ed282008-06-03 16:36:54 -0700617 return -EMSGSIZE;
Patrick McHardy01480e12008-01-22 22:10:59 -0800618
Johannes Berg59ae1d12017-06-16 14:29:20 +0200619 skb_put_data(skb, data, attrlen);
Patrick McHardy01480e12008-01-22 22:10:59 -0800620 return 0;
621}
Herbert Xu90800212009-03-11 23:18:32 +0800622EXPORT_SYMBOL(nla_append);
623#endif