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