Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
bfa83a9e TG |
2 | /* |
3 | * NETLINK Netlink attributes | |
4 | * | |
5 | * Authors: Thomas Graf <tgraf@suug.ch> | |
6 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> | |
7 | */ | |
8 | ||
8bc3bcc9 | 9 | #include <linux/export.h> |
bfa83a9e TG |
10 | #include <linux/kernel.h> |
11 | #include <linux/errno.h> | |
12 | #include <linux/jiffies.h> | |
bfa83a9e TG |
13 | #include <linux/skbuff.h> |
14 | #include <linux/string.h> | |
15 | #include <linux/types.h> | |
16 | #include <net/netlink.h> | |
17 | ||
6e237d09 DA |
18 | /* 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 | */ | |
28033ae4 | 23 | static const u8 nla_attr_len[NLA_TYPE_MAX+1] = { |
bfa83a9e TG |
24 | [NLA_U8] = sizeof(u8), |
25 | [NLA_U16] = sizeof(u16), | |
26 | [NLA_U32] = sizeof(u32), | |
27 | [NLA_U64] = sizeof(u64), | |
9eca2eb9 JA |
28 | [NLA_S8] = sizeof(s8), |
29 | [NLA_S16] = sizeof(s16), | |
30 | [NLA_S32] = sizeof(s32), | |
31 | [NLA_S64] = sizeof(s64), | |
bfa83a9e TG |
32 | }; |
33 | ||
28033ae4 | 34 | static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = { |
6e237d09 DA |
35 | [NLA_U8] = sizeof(u8), |
36 | [NLA_U16] = sizeof(u16), | |
37 | [NLA_U32] = sizeof(u32), | |
38 | [NLA_U64] = sizeof(u64), | |
28033ae4 DA |
39 | [NLA_MSECS] = sizeof(u64), |
40 | [NLA_NESTED] = NLA_HDRLEN, | |
6e237d09 DA |
41 | [NLA_S8] = sizeof(s8), |
42 | [NLA_S16] = sizeof(s16), | |
43 | [NLA_S32] = sizeof(s32), | |
44 | [NLA_S64] = sizeof(s64), | |
28033ae4 DA |
45 | }; |
46 | ||
64c83d83 | 47 | static int validate_nla_bitfield32(const struct nlattr *nla, |
48fde90a | 48 | const u32 *valid_flags_mask) |
64c83d83 JHS |
49 | { |
50 | const struct nla_bitfield32 *bf = nla_data(nla); | |
64c83d83 | 51 | |
48fde90a | 52 | if (!valid_flags_mask) |
64c83d83 JHS |
53 | 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 | ||
3654654f | 70 | static int validate_nla(const struct nlattr *nla, int maxtype, |
568b742a | 71 | const struct nla_policy *policy, |
c29f1845 | 72 | struct netlink_ext_ack *extack) |
bfa83a9e | 73 | { |
ef7c79ed | 74 | const struct nla_policy *pt; |
8f4c1f9b | 75 | int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla); |
c29f1845 | 76 | int err = -ERANGE; |
bfa83a9e | 77 | |
8f4c1f9b | 78 | if (type <= 0 || type > maxtype) |
bfa83a9e TG |
79 | return 0; |
80 | ||
8f4c1f9b | 81 | pt = &policy[type]; |
bfa83a9e TG |
82 | |
83 | BUG_ON(pt->type > NLA_TYPE_MAX); | |
84 | ||
b60b87fc JB |
85 | if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) || |
86 | (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) { | |
6e237d09 DA |
87 | pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n", |
88 | current->comm, type); | |
28033ae4 DA |
89 | } |
90 | ||
a5531a5d | 91 | switch (pt->type) { |
b60b87fc JB |
92 | case NLA_EXACT_LEN: |
93 | if (attrlen != pt->len) | |
c29f1845 | 94 | goto out_err; |
b60b87fc JB |
95 | break; |
96 | ||
568b742a | 97 | case NLA_REJECT: |
c29f1845 JB |
98 | if (extack && pt->validation_data) { |
99 | NL_SET_BAD_ATTR(extack, nla); | |
100 | extack->_msg = pt->validation_data; | |
101 | return -EINVAL; | |
102 | } | |
103 | err = -EINVAL; | |
104 | goto out_err; | |
568b742a | 105 | |
a5531a5d TG |
106 | case NLA_FLAG: |
107 | if (attrlen > 0) | |
c29f1845 | 108 | goto out_err; |
a5531a5d | 109 | break; |
bfa83a9e | 110 | |
64c83d83 JHS |
111 | case NLA_BITFIELD32: |
112 | if (attrlen != sizeof(struct nla_bitfield32)) | |
c29f1845 | 113 | goto out_err; |
64c83d83 | 114 | |
c29f1845 JB |
115 | err = validate_nla_bitfield32(nla, pt->validation_data); |
116 | if (err) | |
117 | goto out_err; | |
118 | break; | |
64c83d83 | 119 | |
a5531a5d TG |
120 | case NLA_NUL_STRING: |
121 | if (pt->len) | |
122 | minlen = min_t(int, attrlen, pt->len + 1); | |
123 | else | |
124 | minlen = attrlen; | |
bfa83a9e | 125 | |
c29f1845 JB |
126 | if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) { |
127 | err = -EINVAL; | |
128 | goto out_err; | |
129 | } | |
a5531a5d TG |
130 | /* fall through */ |
131 | ||
132 | case NLA_STRING: | |
133 | if (attrlen < 1) | |
c29f1845 | 134 | goto out_err; |
a5531a5d TG |
135 | |
136 | if (pt->len) { | |
137 | char *buf = nla_data(nla); | |
138 | ||
139 | if (buf[attrlen - 1] == '\0') | |
140 | attrlen--; | |
141 | ||
142 | if (attrlen > pt->len) | |
c29f1845 | 143 | goto out_err; |
a5531a5d TG |
144 | } |
145 | break; | |
146 | ||
d30045a0 JB |
147 | case NLA_BINARY: |
148 | if (pt->len && attrlen > pt->len) | |
c29f1845 | 149 | goto out_err; |
d30045a0 JB |
150 | break; |
151 | ||
ea5693cc PM |
152 | case NLA_NESTED: |
153 | /* a nested attributes is allowed to be empty; if its not, | |
154 | * it must have a size of at least NLA_HDRLEN. | |
155 | */ | |
156 | if (attrlen == 0) | |
157 | break; | |
9a659a35 JB |
158 | if (attrlen < NLA_HDRLEN) |
159 | goto out_err; | |
160 | if (pt->validation_data) { | |
161 | err = nla_validate(nla_data(nla), nla_len(nla), pt->len, | |
162 | pt->validation_data, extack); | |
163 | if (err < 0) { | |
164 | /* | |
165 | * return directly to preserve the inner | |
166 | * error message/attribute pointer | |
167 | */ | |
168 | return err; | |
169 | } | |
170 | } | |
171 | break; | |
a5531a5d TG |
172 | default: |
173 | if (pt->len) | |
174 | minlen = pt->len; | |
175 | else if (pt->type != NLA_UNSPEC) | |
176 | minlen = nla_attr_minlen[pt->type]; | |
177 | ||
178 | if (attrlen < minlen) | |
c29f1845 | 179 | goto out_err; |
a5531a5d | 180 | } |
bfa83a9e TG |
181 | |
182 | return 0; | |
c29f1845 JB |
183 | out_err: |
184 | NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation"); | |
185 | return err; | |
bfa83a9e TG |
186 | } |
187 | ||
188 | /** | |
189 | * nla_validate - Validate a stream of attributes | |
190 | * @head: head of attribute stream | |
191 | * @len: length of attribute stream | |
192 | * @maxtype: maximum attribute type to be expected | |
193 | * @policy: validation policy | |
fceb6435 | 194 | * @extack: extended ACK report struct |
bfa83a9e TG |
195 | * |
196 | * Validates all attributes in the specified attribute stream against the | |
197 | * specified policy. Attributes with a type exceeding maxtype will be | |
198 | * ignored. See documenation of struct nla_policy for more details. | |
199 | * | |
200 | * Returns 0 on success or a negative error code. | |
201 | */ | |
3654654f | 202 | int nla_validate(const struct nlattr *head, int len, int maxtype, |
fceb6435 JB |
203 | const struct nla_policy *policy, |
204 | struct netlink_ext_ack *extack) | |
bfa83a9e | 205 | { |
3654654f | 206 | const struct nlattr *nla; |
fceb6435 | 207 | int rem; |
bfa83a9e TG |
208 | |
209 | nla_for_each_attr(nla, head, len, rem) { | |
c29f1845 | 210 | int err = validate_nla(nla, maxtype, policy, extack); |
fceb6435 | 211 | |
c29f1845 | 212 | if (err < 0) |
fceb6435 | 213 | return err; |
bfa83a9e TG |
214 | } |
215 | ||
fceb6435 | 216 | return 0; |
bfa83a9e | 217 | } |
6d6a138f | 218 | EXPORT_SYMBOL(nla_validate); |
bfa83a9e | 219 | |
e487eb99 HE |
220 | /** |
221 | * nla_policy_len - Determin the max. length of a policy | |
222 | * @policy: policy to use | |
223 | * @n: number of policies | |
224 | * | |
225 | * Determines the max. length of the policy. It is currently used | |
226 | * to allocated Netlink buffers roughly the size of the actual | |
227 | * message. | |
228 | * | |
229 | * Returns 0 on success or a negative error code. | |
230 | */ | |
231 | int | |
232 | nla_policy_len(const struct nla_policy *p, int n) | |
233 | { | |
234 | int i, len = 0; | |
235 | ||
e3fa3aff | 236 | for (i = 0; i < n; i++, p++) { |
e487eb99 HE |
237 | if (p->len) |
238 | len += nla_total_size(p->len); | |
28033ae4 DA |
239 | else if (nla_attr_len[p->type]) |
240 | len += nla_total_size(nla_attr_len[p->type]); | |
e487eb99 HE |
241 | else if (nla_attr_minlen[p->type]) |
242 | len += nla_total_size(nla_attr_minlen[p->type]); | |
243 | } | |
244 | ||
245 | return len; | |
246 | } | |
6d6a138f | 247 | EXPORT_SYMBOL(nla_policy_len); |
e487eb99 | 248 | |
bfa83a9e TG |
249 | /** |
250 | * nla_parse - Parse a stream of attributes into a tb buffer | |
251 | * @tb: destination array with maxtype+1 elements | |
252 | * @maxtype: maximum attribute type to be expected | |
253 | * @head: head of attribute stream | |
254 | * @len: length of attribute stream | |
10b595af | 255 | * @policy: validation policy |
bfa83a9e TG |
256 | * |
257 | * Parses a stream of attributes and stores a pointer to each attribute in | |
b595076a | 258 | * the tb array accessible via the attribute type. Attributes with a type |
bfa83a9e TG |
259 | * exceeding maxtype will be silently ignored for backwards compatibility |
260 | * reasons. policy may be set to NULL if no validation is required. | |
261 | * | |
262 | * Returns 0 on success or a negative error code. | |
263 | */ | |
3654654f | 264 | int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, |
fceb6435 JB |
265 | int len, const struct nla_policy *policy, |
266 | struct netlink_ext_ack *extack) | |
bfa83a9e | 267 | { |
3654654f | 268 | const struct nlattr *nla; |
c29f1845 | 269 | int rem; |
bfa83a9e TG |
270 | |
271 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); | |
272 | ||
273 | nla_for_each_attr(nla, head, len, rem) { | |
8f4c1f9b | 274 | u16 type = nla_type(nla); |
bfa83a9e TG |
275 | |
276 | if (type > 0 && type <= maxtype) { | |
277 | if (policy) { | |
c29f1845 JB |
278 | int err = validate_nla(nla, maxtype, policy, |
279 | extack); | |
280 | ||
281 | if (err < 0) | |
282 | return err; | |
bfa83a9e TG |
283 | } |
284 | ||
3654654f | 285 | tb[type] = (struct nlattr *)nla; |
bfa83a9e TG |
286 | } |
287 | } | |
288 | ||
289 | if (unlikely(rem > 0)) | |
bfc5184b MS |
290 | pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", |
291 | rem, current->comm); | |
bfa83a9e | 292 | |
c29f1845 | 293 | return 0; |
bfa83a9e | 294 | } |
6d6a138f | 295 | EXPORT_SYMBOL(nla_parse); |
bfa83a9e TG |
296 | |
297 | /** | |
298 | * nla_find - Find a specific attribute in a stream of attributes | |
299 | * @head: head of attribute stream | |
300 | * @len: length of attribute stream | |
301 | * @attrtype: type of attribute to look for | |
302 | * | |
303 | * Returns the first attribute in the stream matching the specified type. | |
304 | */ | |
3654654f | 305 | struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype) |
bfa83a9e | 306 | { |
3654654f | 307 | const struct nlattr *nla; |
bfa83a9e TG |
308 | int rem; |
309 | ||
310 | nla_for_each_attr(nla, head, len, rem) | |
8f4c1f9b | 311 | if (nla_type(nla) == attrtype) |
3654654f | 312 | return (struct nlattr *)nla; |
bfa83a9e TG |
313 | |
314 | return NULL; | |
315 | } | |
6d6a138f | 316 | EXPORT_SYMBOL(nla_find); |
bfa83a9e TG |
317 | |
318 | /** | |
319 | * nla_strlcpy - Copy string attribute payload into a sized buffer | |
320 | * @dst: where to copy the string to | |
10b595af | 321 | * @nla: attribute to copy the string from |
bfa83a9e TG |
322 | * @dstsize: size of destination buffer |
323 | * | |
324 | * Copies at most dstsize - 1 bytes into the destination buffer. | |
325 | * The result is always a valid NUL-terminated string. Unlike | |
326 | * strlcpy the destination buffer is always padded out. | |
327 | * | |
328 | * Returns the length of the source buffer. | |
329 | */ | |
330 | size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize) | |
331 | { | |
332 | size_t srclen = nla_len(nla); | |
333 | char *src = nla_data(nla); | |
334 | ||
335 | if (srclen > 0 && src[srclen - 1] == '\0') | |
336 | srclen--; | |
337 | ||
338 | if (dstsize > 0) { | |
339 | size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen; | |
340 | ||
341 | memset(dst, 0, dstsize); | |
342 | memcpy(dst, src, len); | |
343 | } | |
344 | ||
345 | return srclen; | |
346 | } | |
6d6a138f | 347 | EXPORT_SYMBOL(nla_strlcpy); |
bfa83a9e | 348 | |
2cf0c8b3 PS |
349 | /** |
350 | * nla_strdup - Copy string attribute payload into a newly allocated buffer | |
351 | * @nla: attribute to copy the string from | |
352 | * @flags: the type of memory to allocate (see kmalloc). | |
353 | * | |
354 | * Returns a pointer to the allocated buffer or NULL on error. | |
355 | */ | |
356 | char *nla_strdup(const struct nlattr *nla, gfp_t flags) | |
357 | { | |
358 | size_t srclen = nla_len(nla); | |
359 | char *src = nla_data(nla), *dst; | |
360 | ||
361 | if (srclen > 0 && src[srclen - 1] == '\0') | |
362 | srclen--; | |
363 | ||
364 | dst = kmalloc(srclen + 1, flags); | |
365 | if (dst != NULL) { | |
366 | memcpy(dst, src, srclen); | |
367 | dst[srclen] = '\0'; | |
368 | } | |
369 | return dst; | |
370 | } | |
371 | EXPORT_SYMBOL(nla_strdup); | |
372 | ||
bfa83a9e TG |
373 | /** |
374 | * nla_memcpy - Copy a netlink attribute into another memory area | |
375 | * @dest: where to copy to memcpy | |
376 | * @src: netlink attribute to copy from | |
377 | * @count: size of the destination area | |
378 | * | |
379 | * Note: The number of bytes copied is limited by the length of | |
380 | * attribute's payload. memcpy | |
381 | * | |
382 | * Returns the number of bytes copied. | |
383 | */ | |
b057efd4 | 384 | int nla_memcpy(void *dest, const struct nlattr *src, int count) |
bfa83a9e TG |
385 | { |
386 | int minlen = min_t(int, count, nla_len(src)); | |
387 | ||
388 | memcpy(dest, nla_data(src), minlen); | |
5899f047 JB |
389 | if (count > minlen) |
390 | memset(dest + minlen, 0, count - minlen); | |
bfa83a9e TG |
391 | |
392 | return minlen; | |
393 | } | |
6d6a138f | 394 | EXPORT_SYMBOL(nla_memcpy); |
bfa83a9e TG |
395 | |
396 | /** | |
397 | * nla_memcmp - Compare an attribute with sized memory area | |
398 | * @nla: netlink attribute | |
399 | * @data: memory area | |
400 | * @size: size of memory area | |
401 | */ | |
402 | int nla_memcmp(const struct nlattr *nla, const void *data, | |
403 | size_t size) | |
404 | { | |
405 | int d = nla_len(nla) - size; | |
406 | ||
407 | if (d == 0) | |
408 | d = memcmp(nla_data(nla), data, size); | |
409 | ||
410 | return d; | |
411 | } | |
6d6a138f | 412 | EXPORT_SYMBOL(nla_memcmp); |
bfa83a9e TG |
413 | |
414 | /** | |
415 | * nla_strcmp - Compare a string attribute against a string | |
416 | * @nla: netlink string attribute | |
417 | * @str: another string | |
418 | */ | |
419 | int nla_strcmp(const struct nlattr *nla, const char *str) | |
420 | { | |
8b7b9324 PN |
421 | int len = strlen(str); |
422 | char *buf = nla_data(nla); | |
423 | int attrlen = nla_len(nla); | |
424 | int d; | |
bfa83a9e | 425 | |
8b7b9324 PN |
426 | if (attrlen > 0 && buf[attrlen - 1] == '\0') |
427 | attrlen--; | |
428 | ||
429 | d = attrlen - len; | |
bfa83a9e TG |
430 | if (d == 0) |
431 | d = memcmp(nla_data(nla), str, len); | |
432 | ||
433 | return d; | |
434 | } | |
6d6a138f | 435 | EXPORT_SYMBOL(nla_strcmp); |
bfa83a9e | 436 | |
90800216 | 437 | #ifdef CONFIG_NET |
bfa83a9e TG |
438 | /** |
439 | * __nla_reserve - reserve room for attribute on the skb | |
440 | * @skb: socket buffer to reserve room on | |
441 | * @attrtype: attribute type | |
442 | * @attrlen: length of attribute payload | |
443 | * | |
444 | * Adds a netlink attribute header to a socket buffer and reserves | |
445 | * room for the payload but does not copy it. | |
446 | * | |
447 | * The caller is responsible to ensure that the skb provides enough | |
448 | * tailroom for the attribute header and payload. | |
449 | */ | |
450 | struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | |
451 | { | |
452 | struct nlattr *nla; | |
453 | ||
4df864c1 | 454 | nla = skb_put(skb, nla_total_size(attrlen)); |
bfa83a9e TG |
455 | nla->nla_type = attrtype; |
456 | nla->nla_len = nla_attr_size(attrlen); | |
457 | ||
458 | memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen)); | |
459 | ||
460 | return nla; | |
461 | } | |
90800216 | 462 | EXPORT_SYMBOL(__nla_reserve); |
bfa83a9e | 463 | |
089bf1a6 ND |
464 | /** |
465 | * __nla_reserve_64bit - reserve room for attribute on the skb and align it | |
466 | * @skb: socket buffer to reserve room on | |
467 | * @attrtype: attribute type | |
468 | * @attrlen: length of attribute payload | |
11a99573 | 469 | * @padattr: attribute type for the padding |
089bf1a6 ND |
470 | * |
471 | * Adds a netlink attribute header to a socket buffer and reserves | |
472 | * room for the payload but does not copy it. It also ensure that this | |
11a99573 | 473 | * attribute will have a 64-bit aligned nla_data() area. |
089bf1a6 ND |
474 | * |
475 | * The caller is responsible to ensure that the skb provides enough | |
476 | * tailroom for the attribute header and payload. | |
477 | */ | |
478 | struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, | |
479 | int attrlen, int padattr) | |
480 | { | |
481 | if (nla_need_padding_for_64bit(skb)) | |
482 | nla_align_64bit(skb, padattr); | |
483 | ||
484 | return __nla_reserve(skb, attrtype, attrlen); | |
485 | } | |
486 | EXPORT_SYMBOL(__nla_reserve_64bit); | |
487 | ||
fe4944e5 TG |
488 | /** |
489 | * __nla_reserve_nohdr - reserve room for attribute without header | |
490 | * @skb: socket buffer to reserve room on | |
491 | * @attrlen: length of attribute payload | |
492 | * | |
493 | * Reserves room for attribute payload without a header. | |
494 | * | |
495 | * The caller is responsible to ensure that the skb provides enough | |
496 | * tailroom for the payload. | |
497 | */ | |
498 | void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen) | |
499 | { | |
b952f4df | 500 | return skb_put_zero(skb, NLA_ALIGN(attrlen)); |
fe4944e5 | 501 | } |
90800216 | 502 | EXPORT_SYMBOL(__nla_reserve_nohdr); |
fe4944e5 | 503 | |
bfa83a9e TG |
504 | /** |
505 | * nla_reserve - reserve room for attribute on the skb | |
506 | * @skb: socket buffer to reserve room on | |
507 | * @attrtype: attribute type | |
508 | * @attrlen: length of attribute payload | |
509 | * | |
510 | * Adds a netlink attribute header to a socket buffer and reserves | |
511 | * room for the payload but does not copy it. | |
512 | * | |
513 | * Returns NULL if the tailroom of the skb is insufficient to store | |
514 | * the attribute header and payload. | |
515 | */ | |
516 | struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | |
517 | { | |
518 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | |
519 | return NULL; | |
520 | ||
521 | return __nla_reserve(skb, attrtype, attrlen); | |
522 | } | |
90800216 | 523 | EXPORT_SYMBOL(nla_reserve); |
bfa83a9e | 524 | |
089bf1a6 ND |
525 | /** |
526 | * nla_reserve_64bit - reserve room for attribute on the skb and align it | |
527 | * @skb: socket buffer to reserve room on | |
528 | * @attrtype: attribute type | |
529 | * @attrlen: length of attribute payload | |
11a99573 | 530 | * @padattr: attribute type for the padding |
089bf1a6 ND |
531 | * |
532 | * Adds a netlink attribute header to a socket buffer and reserves | |
533 | * room for the payload but does not copy it. It also ensure that this | |
11a99573 | 534 | * attribute will have a 64-bit aligned nla_data() area. |
089bf1a6 ND |
535 | * |
536 | * Returns NULL if the tailroom of the skb is insufficient to store | |
537 | * the attribute header and payload. | |
538 | */ | |
539 | struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
540 | int padattr) | |
541 | { | |
542 | size_t len; | |
543 | ||
544 | if (nla_need_padding_for_64bit(skb)) | |
545 | len = nla_total_size_64bit(attrlen); | |
546 | else | |
547 | len = nla_total_size(attrlen); | |
548 | if (unlikely(skb_tailroom(skb) < len)) | |
549 | return NULL; | |
550 | ||
551 | return __nla_reserve_64bit(skb, attrtype, attrlen, padattr); | |
552 | } | |
553 | EXPORT_SYMBOL(nla_reserve_64bit); | |
554 | ||
fe4944e5 | 555 | /** |
10b595af | 556 | * nla_reserve_nohdr - reserve room for attribute without header |
fe4944e5 | 557 | * @skb: socket buffer to reserve room on |
10b595af | 558 | * @attrlen: length of attribute payload |
fe4944e5 TG |
559 | * |
560 | * Reserves room for attribute payload without a header. | |
561 | * | |
562 | * Returns NULL if the tailroom of the skb is insufficient to store | |
563 | * the attribute payload. | |
564 | */ | |
565 | void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen) | |
566 | { | |
567 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
568 | return NULL; | |
569 | ||
570 | return __nla_reserve_nohdr(skb, attrlen); | |
571 | } | |
90800216 | 572 | EXPORT_SYMBOL(nla_reserve_nohdr); |
fe4944e5 | 573 | |
bfa83a9e TG |
574 | /** |
575 | * __nla_put - Add a netlink attribute to a socket buffer | |
576 | * @skb: socket buffer to add attribute to | |
577 | * @attrtype: attribute type | |
578 | * @attrlen: length of attribute payload | |
579 | * @data: head of attribute payload | |
580 | * | |
581 | * The caller is responsible to ensure that the skb provides enough | |
582 | * tailroom for the attribute header and payload. | |
583 | */ | |
584 | void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, | |
585 | const void *data) | |
586 | { | |
587 | struct nlattr *nla; | |
588 | ||
589 | nla = __nla_reserve(skb, attrtype, attrlen); | |
590 | memcpy(nla_data(nla), data, attrlen); | |
591 | } | |
90800216 | 592 | EXPORT_SYMBOL(__nla_put); |
bfa83a9e | 593 | |
089bf1a6 ND |
594 | /** |
595 | * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it | |
596 | * @skb: socket buffer to add attribute to | |
597 | * @attrtype: attribute type | |
598 | * @attrlen: length of attribute payload | |
599 | * @data: head of attribute payload | |
11a99573 | 600 | * @padattr: attribute type for the padding |
089bf1a6 ND |
601 | * |
602 | * The caller is responsible to ensure that the skb provides enough | |
603 | * tailroom for the attribute header and payload. | |
604 | */ | |
605 | void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
606 | const void *data, int padattr) | |
607 | { | |
608 | struct nlattr *nla; | |
609 | ||
610 | nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr); | |
611 | memcpy(nla_data(nla), data, attrlen); | |
612 | } | |
613 | EXPORT_SYMBOL(__nla_put_64bit); | |
614 | ||
fe4944e5 TG |
615 | /** |
616 | * __nla_put_nohdr - Add a netlink attribute without header | |
617 | * @skb: socket buffer to add attribute to | |
618 | * @attrlen: length of attribute payload | |
619 | * @data: head of attribute payload | |
620 | * | |
621 | * The caller is responsible to ensure that the skb provides enough | |
622 | * tailroom for the attribute payload. | |
623 | */ | |
624 | void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) | |
625 | { | |
626 | void *start; | |
627 | ||
628 | start = __nla_reserve_nohdr(skb, attrlen); | |
629 | memcpy(start, data, attrlen); | |
630 | } | |
90800216 | 631 | EXPORT_SYMBOL(__nla_put_nohdr); |
bfa83a9e TG |
632 | |
633 | /** | |
634 | * nla_put - Add a netlink attribute to a socket buffer | |
635 | * @skb: socket buffer to add attribute to | |
636 | * @attrtype: attribute type | |
637 | * @attrlen: length of attribute payload | |
638 | * @data: head of attribute payload | |
639 | * | |
bc3ed28c | 640 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
bfa83a9e TG |
641 | * the attribute header and payload. |
642 | */ | |
643 | int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data) | |
644 | { | |
645 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | |
bc3ed28c | 646 | return -EMSGSIZE; |
bfa83a9e TG |
647 | |
648 | __nla_put(skb, attrtype, attrlen, data); | |
649 | return 0; | |
650 | } | |
90800216 | 651 | EXPORT_SYMBOL(nla_put); |
bfa83a9e | 652 | |
089bf1a6 ND |
653 | /** |
654 | * nla_put_64bit - Add a netlink attribute to a socket buffer and align it | |
655 | * @skb: socket buffer to add attribute to | |
656 | * @attrtype: attribute type | |
657 | * @attrlen: length of attribute payload | |
658 | * @data: head of attribute payload | |
11a99573 | 659 | * @padattr: attribute type for the padding |
089bf1a6 ND |
660 | * |
661 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store | |
662 | * the attribute header and payload. | |
663 | */ | |
664 | int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
665 | const void *data, int padattr) | |
666 | { | |
667 | size_t len; | |
668 | ||
669 | if (nla_need_padding_for_64bit(skb)) | |
670 | len = nla_total_size_64bit(attrlen); | |
671 | else | |
672 | len = nla_total_size(attrlen); | |
673 | if (unlikely(skb_tailroom(skb) < len)) | |
674 | return -EMSGSIZE; | |
675 | ||
676 | __nla_put_64bit(skb, attrtype, attrlen, data, padattr); | |
677 | return 0; | |
678 | } | |
679 | EXPORT_SYMBOL(nla_put_64bit); | |
680 | ||
fe4944e5 TG |
681 | /** |
682 | * nla_put_nohdr - Add a netlink attribute without header | |
683 | * @skb: socket buffer to add attribute to | |
684 | * @attrlen: length of attribute payload | |
685 | * @data: head of attribute payload | |
686 | * | |
bc3ed28c | 687 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
fe4944e5 TG |
688 | * the attribute payload. |
689 | */ | |
690 | int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) | |
691 | { | |
692 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
bc3ed28c | 693 | return -EMSGSIZE; |
fe4944e5 TG |
694 | |
695 | __nla_put_nohdr(skb, attrlen, data); | |
696 | return 0; | |
697 | } | |
90800216 | 698 | EXPORT_SYMBOL(nla_put_nohdr); |
bfa83a9e | 699 | |
01480e1c PM |
700 | /** |
701 | * nla_append - Add a netlink attribute without header or padding | |
702 | * @skb: socket buffer to add attribute to | |
703 | * @attrlen: length of attribute payload | |
704 | * @data: head of attribute payload | |
705 | * | |
bc3ed28c | 706 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
01480e1c PM |
707 | * the attribute payload. |
708 | */ | |
709 | int nla_append(struct sk_buff *skb, int attrlen, const void *data) | |
710 | { | |
711 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
bc3ed28c | 712 | return -EMSGSIZE; |
01480e1c | 713 | |
59ae1d12 | 714 | skb_put_data(skb, data, attrlen); |
01480e1c PM |
715 | return 0; |
716 | } | |
90800216 HX |
717 | EXPORT_SYMBOL(nla_append); |
718 | #endif |