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 | ||
7690aa1c JB |
47 | /* |
48 | * Nested policies might refer back to the original | |
49 | * policy in some cases, and userspace could try to | |
50 | * abuse that and recurse by nesting in the right | |
51 | * ways. Limit recursion to avoid this problem. | |
52 | */ | |
53 | #define MAX_POLICY_RECURSION_DEPTH 10 | |
54 | ||
55 | static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype, | |
56 | const struct nla_policy *policy, | |
57 | unsigned int validate, | |
58 | struct netlink_ext_ack *extack, | |
59 | struct nlattr **tb, unsigned int depth); | |
60 | ||
64c83d83 | 61 | static int validate_nla_bitfield32(const struct nlattr *nla, |
47a1494b | 62 | const u32 valid_flags_mask) |
64c83d83 JHS |
63 | { |
64 | const struct nla_bitfield32 *bf = nla_data(nla); | |
64c83d83 | 65 | |
48fde90a | 66 | if (!valid_flags_mask) |
64c83d83 JHS |
67 | return -EINVAL; |
68 | ||
69 | /*disallow invalid bit selector */ | |
47a1494b | 70 | if (bf->selector & ~valid_flags_mask) |
64c83d83 JHS |
71 | return -EINVAL; |
72 | ||
73 | /*disallow invalid bit values */ | |
47a1494b | 74 | if (bf->value & ~valid_flags_mask) |
64c83d83 JHS |
75 | return -EINVAL; |
76 | ||
77 | /*disallow valid bit values that are not selected*/ | |
78 | if (bf->value & ~bf->selector) | |
79 | return -EINVAL; | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
1501d135 JB |
84 | static int nla_validate_array(const struct nlattr *head, int len, int maxtype, |
85 | const struct nla_policy *policy, | |
8cb08174 | 86 | struct netlink_ext_ack *extack, |
7690aa1c | 87 | unsigned int validate, unsigned int depth) |
1501d135 JB |
88 | { |
89 | const struct nlattr *entry; | |
90 | int rem; | |
91 | ||
92 | nla_for_each_attr(entry, head, len, rem) { | |
93 | int ret; | |
94 | ||
95 | if (nla_len(entry) == 0) | |
96 | continue; | |
97 | ||
98 | if (nla_len(entry) < NLA_HDRLEN) { | |
44f3625b JB |
99 | NL_SET_ERR_MSG_ATTR_POL(extack, entry, policy, |
100 | "Array element too short"); | |
1501d135 JB |
101 | return -ERANGE; |
102 | } | |
103 | ||
7690aa1c JB |
104 | ret = __nla_validate_parse(nla_data(entry), nla_len(entry), |
105 | maxtype, policy, validate, extack, | |
106 | NULL, depth + 1); | |
1501d135 JB |
107 | if (ret < 0) |
108 | return ret; | |
109 | } | |
110 | ||
111 | return 0; | |
112 | } | |
113 | ||
2c28ae48 JB |
114 | void nla_get_range_unsigned(const struct nla_policy *pt, |
115 | struct netlink_range_validation *range) | |
3e48be05 | 116 | { |
d06a09b9 JB |
117 | WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR && |
118 | (pt->min < 0 || pt->max < 0)); | |
119 | ||
2c28ae48 JB |
120 | range->min = 0; |
121 | ||
122 | switch (pt->type) { | |
123 | case NLA_U8: | |
124 | range->max = U8_MAX; | |
125 | break; | |
126 | case NLA_U16: | |
8aa26c57 | 127 | case NLA_BINARY: |
2c28ae48 JB |
128 | range->max = U16_MAX; |
129 | break; | |
130 | case NLA_U32: | |
131 | range->max = U32_MAX; | |
132 | break; | |
133 | case NLA_U64: | |
134 | case NLA_MSECS: | |
135 | range->max = U64_MAX; | |
136 | break; | |
137 | default: | |
138 | WARN_ON_ONCE(1); | |
139 | return; | |
140 | } | |
141 | ||
d06a09b9 JB |
142 | switch (pt->validation_type) { |
143 | case NLA_VALIDATE_RANGE: | |
8aa26c57 | 144 | case NLA_VALIDATE_RANGE_WARN_TOO_LONG: |
d06a09b9 JB |
145 | range->min = pt->min; |
146 | range->max = pt->max; | |
147 | break; | |
148 | case NLA_VALIDATE_RANGE_PTR: | |
2c28ae48 | 149 | *range = *pt->range; |
d06a09b9 JB |
150 | break; |
151 | case NLA_VALIDATE_MIN: | |
152 | range->min = pt->min; | |
153 | break; | |
154 | case NLA_VALIDATE_MAX: | |
155 | range->max = pt->max; | |
156 | break; | |
2c28ae48 JB |
157 | default: |
158 | break; | |
d06a09b9 | 159 | } |
2c28ae48 JB |
160 | } |
161 | ||
08724ef6 FW |
162 | static u64 nla_get_attr_bo(const struct nla_policy *pt, |
163 | const struct nlattr *nla) | |
164 | { | |
165 | switch (pt->type) { | |
166 | case NLA_U16: | |
167 | if (pt->network_byte_order) | |
168 | return ntohs(nla_get_be16(nla)); | |
169 | ||
170 | return nla_get_u16(nla); | |
171 | case NLA_U32: | |
172 | if (pt->network_byte_order) | |
173 | return ntohl(nla_get_be32(nla)); | |
174 | ||
175 | return nla_get_u32(nla); | |
176 | case NLA_U64: | |
177 | if (pt->network_byte_order) | |
178 | return be64_to_cpu(nla_get_be64(nla)); | |
179 | ||
180 | return nla_get_u64(nla); | |
181 | } | |
182 | ||
183 | WARN_ON_ONCE(1); | |
184 | return 0; | |
185 | } | |
186 | ||
8aa26c57 JB |
187 | static int nla_validate_range_unsigned(const struct nla_policy *pt, |
188 | const struct nlattr *nla, | |
189 | struct netlink_ext_ack *extack, | |
190 | unsigned int validate) | |
2c28ae48 JB |
191 | { |
192 | struct netlink_range_validation range; | |
193 | u64 value; | |
3e48be05 JB |
194 | |
195 | switch (pt->type) { | |
196 | case NLA_U8: | |
197 | value = nla_get_u8(nla); | |
198 | break; | |
199 | case NLA_U16: | |
3e48be05 | 200 | case NLA_U32: |
d06a09b9 | 201 | case NLA_U64: |
08724ef6 FW |
202 | value = nla_get_attr_bo(pt, nla); |
203 | break; | |
da4063bd | 204 | case NLA_MSECS: |
d06a09b9 JB |
205 | value = nla_get_u64(nla); |
206 | break; | |
8aa26c57 JB |
207 | case NLA_BINARY: |
208 | value = nla_len(nla); | |
209 | break; | |
d06a09b9 JB |
210 | default: |
211 | return -EINVAL; | |
212 | } | |
213 | ||
2c28ae48 JB |
214 | nla_get_range_unsigned(pt, &range); |
215 | ||
8aa26c57 JB |
216 | if (pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG && |
217 | pt->type == NLA_BINARY && value > range.max) { | |
218 | pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n", | |
219 | current->comm, pt->type); | |
220 | if (validate & NL_VALIDATE_STRICT_ATTRS) { | |
44f3625b JB |
221 | NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
222 | "invalid attribute length"); | |
8aa26c57 JB |
223 | return -EINVAL; |
224 | } | |
225 | ||
226 | /* this assumes min <= max (don't validate against min) */ | |
227 | return 0; | |
228 | } | |
229 | ||
2c28ae48 | 230 | if (value < range.min || value > range.max) { |
8aa26c57 JB |
231 | bool binary = pt->type == NLA_BINARY; |
232 | ||
233 | if (binary) | |
44f3625b JB |
234 | NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
235 | "binary attribute size out of range"); | |
8aa26c57 | 236 | else |
44f3625b JB |
237 | NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
238 | "integer out of range"); | |
8aa26c57 | 239 | |
d06a09b9 JB |
240 | return -ERANGE; |
241 | } | |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
2c28ae48 JB |
246 | void nla_get_range_signed(const struct nla_policy *pt, |
247 | struct netlink_range_validation_signed *range) | |
d06a09b9 | 248 | { |
2c28ae48 JB |
249 | switch (pt->type) { |
250 | case NLA_S8: | |
251 | range->min = S8_MIN; | |
252 | range->max = S8_MAX; | |
253 | break; | |
254 | case NLA_S16: | |
255 | range->min = S16_MIN; | |
256 | range->max = S16_MAX; | |
257 | break; | |
258 | case NLA_S32: | |
259 | range->min = S32_MIN; | |
260 | range->max = S32_MAX; | |
261 | break; | |
262 | case NLA_S64: | |
263 | range->min = S64_MIN; | |
264 | range->max = S64_MAX; | |
265 | break; | |
266 | default: | |
267 | WARN_ON_ONCE(1); | |
268 | return; | |
269 | } | |
d06a09b9 JB |
270 | |
271 | switch (pt->validation_type) { | |
272 | case NLA_VALIDATE_RANGE: | |
273 | range->min = pt->min; | |
274 | range->max = pt->max; | |
275 | break; | |
276 | case NLA_VALIDATE_RANGE_PTR: | |
2c28ae48 | 277 | *range = *pt->range_signed; |
d06a09b9 JB |
278 | break; |
279 | case NLA_VALIDATE_MIN: | |
280 | range->min = pt->min; | |
281 | break; | |
282 | case NLA_VALIDATE_MAX: | |
283 | range->max = pt->max; | |
284 | break; | |
2c28ae48 JB |
285 | default: |
286 | break; | |
d06a09b9 | 287 | } |
2c28ae48 JB |
288 | } |
289 | ||
290 | static int nla_validate_int_range_signed(const struct nla_policy *pt, | |
291 | const struct nlattr *nla, | |
292 | struct netlink_ext_ack *extack) | |
293 | { | |
294 | struct netlink_range_validation_signed range; | |
295 | s64 value; | |
d06a09b9 JB |
296 | |
297 | switch (pt->type) { | |
3e48be05 JB |
298 | case NLA_S8: |
299 | value = nla_get_s8(nla); | |
300 | break; | |
301 | case NLA_S16: | |
302 | value = nla_get_s16(nla); | |
303 | break; | |
304 | case NLA_S32: | |
305 | value = nla_get_s32(nla); | |
306 | break; | |
307 | case NLA_S64: | |
308 | value = nla_get_s64(nla); | |
309 | break; | |
3e48be05 | 310 | default: |
3e48be05 JB |
311 | return -EINVAL; |
312 | } | |
313 | ||
2c28ae48 JB |
314 | nla_get_range_signed(pt, &range); |
315 | ||
316 | if (value < range.min || value > range.max) { | |
44f3625b JB |
317 | NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
318 | "integer out of range"); | |
3e48be05 JB |
319 | return -ERANGE; |
320 | } | |
321 | ||
322 | return 0; | |
323 | } | |
324 | ||
d06a09b9 JB |
325 | static int nla_validate_int_range(const struct nla_policy *pt, |
326 | const struct nlattr *nla, | |
8aa26c57 JB |
327 | struct netlink_ext_ack *extack, |
328 | unsigned int validate) | |
d06a09b9 JB |
329 | { |
330 | switch (pt->type) { | |
331 | case NLA_U8: | |
332 | case NLA_U16: | |
333 | case NLA_U32: | |
334 | case NLA_U64: | |
da4063bd | 335 | case NLA_MSECS: |
8aa26c57 JB |
336 | case NLA_BINARY: |
337 | return nla_validate_range_unsigned(pt, nla, extack, validate); | |
d06a09b9 JB |
338 | case NLA_S8: |
339 | case NLA_S16: | |
340 | case NLA_S32: | |
341 | case NLA_S64: | |
342 | return nla_validate_int_range_signed(pt, nla, extack); | |
343 | default: | |
344 | WARN_ON(1); | |
345 | return -EINVAL; | |
346 | } | |
347 | } | |
348 | ||
bdbb4e29 JK |
349 | static int nla_validate_mask(const struct nla_policy *pt, |
350 | const struct nlattr *nla, | |
351 | struct netlink_ext_ack *extack) | |
352 | { | |
353 | u64 value; | |
354 | ||
355 | switch (pt->type) { | |
356 | case NLA_U8: | |
357 | value = nla_get_u8(nla); | |
358 | break; | |
359 | case NLA_U16: | |
360 | value = nla_get_u16(nla); | |
361 | break; | |
362 | case NLA_U32: | |
363 | value = nla_get_u32(nla); | |
364 | break; | |
365 | case NLA_U64: | |
366 | value = nla_get_u64(nla); | |
367 | break; | |
368 | default: | |
369 | return -EINVAL; | |
370 | } | |
371 | ||
372 | if (value & ~(u64)pt->mask) { | |
373 | NL_SET_ERR_MSG_ATTR(extack, nla, "reserved bit set"); | |
374 | return -EINVAL; | |
375 | } | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
3654654f | 380 | static int validate_nla(const struct nlattr *nla, int maxtype, |
8cb08174 | 381 | const struct nla_policy *policy, unsigned int validate, |
7690aa1c | 382 | struct netlink_ext_ack *extack, unsigned int depth) |
bfa83a9e | 383 | { |
56738f46 | 384 | u16 strict_start_type = policy[0].strict_start_type; |
ef7c79ed | 385 | const struct nla_policy *pt; |
8f4c1f9b | 386 | int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla); |
c29f1845 | 387 | int err = -ERANGE; |
bfa83a9e | 388 | |
56738f46 JB |
389 | if (strict_start_type && type >= strict_start_type) |
390 | validate |= NL_VALIDATE_STRICT; | |
391 | ||
8f4c1f9b | 392 | if (type <= 0 || type > maxtype) |
bfa83a9e TG |
393 | return 0; |
394 | ||
8f4c1f9b | 395 | pt = &policy[type]; |
bfa83a9e TG |
396 | |
397 | BUG_ON(pt->type > NLA_TYPE_MAX); | |
398 | ||
8aa26c57 | 399 | if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) { |
6e237d09 DA |
400 | pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n", |
401 | current->comm, type); | |
8cb08174 | 402 | if (validate & NL_VALIDATE_STRICT_ATTRS) { |
44f3625b JB |
403 | NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
404 | "invalid attribute length"); | |
8cb08174 JB |
405 | return -EINVAL; |
406 | } | |
28033ae4 DA |
407 | } |
408 | ||
b424e432 MK |
409 | if (validate & NL_VALIDATE_NESTED) { |
410 | if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) && | |
411 | !(nla->nla_type & NLA_F_NESTED)) { | |
44f3625b JB |
412 | NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
413 | "NLA_F_NESTED is missing"); | |
b424e432 MK |
414 | return -EINVAL; |
415 | } | |
416 | if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY && | |
417 | pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) { | |
44f3625b JB |
418 | NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
419 | "NLA_F_NESTED not expected"); | |
b424e432 MK |
420 | return -EINVAL; |
421 | } | |
422 | } | |
423 | ||
a5531a5d | 424 | switch (pt->type) { |
568b742a | 425 | case NLA_REJECT: |
47a1494b | 426 | if (extack && pt->reject_message) { |
c29f1845 | 427 | NL_SET_BAD_ATTR(extack, nla); |
47a1494b | 428 | extack->_msg = pt->reject_message; |
c29f1845 JB |
429 | return -EINVAL; |
430 | } | |
431 | err = -EINVAL; | |
432 | goto out_err; | |
568b742a | 433 | |
a5531a5d TG |
434 | case NLA_FLAG: |
435 | if (attrlen > 0) | |
c29f1845 | 436 | goto out_err; |
a5531a5d | 437 | break; |
bfa83a9e | 438 | |
64c83d83 JHS |
439 | case NLA_BITFIELD32: |
440 | if (attrlen != sizeof(struct nla_bitfield32)) | |
c29f1845 | 441 | goto out_err; |
64c83d83 | 442 | |
47a1494b | 443 | err = validate_nla_bitfield32(nla, pt->bitfield32_valid); |
c29f1845 JB |
444 | if (err) |
445 | goto out_err; | |
446 | break; | |
64c83d83 | 447 | |
a5531a5d TG |
448 | case NLA_NUL_STRING: |
449 | if (pt->len) | |
450 | minlen = min_t(int, attrlen, pt->len + 1); | |
451 | else | |
452 | minlen = attrlen; | |
bfa83a9e | 453 | |
c29f1845 JB |
454 | if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) { |
455 | err = -EINVAL; | |
456 | goto out_err; | |
457 | } | |
36f9ff9e | 458 | fallthrough; |
a5531a5d TG |
459 | |
460 | case NLA_STRING: | |
461 | if (attrlen < 1) | |
c29f1845 | 462 | goto out_err; |
a5531a5d TG |
463 | |
464 | if (pt->len) { | |
465 | char *buf = nla_data(nla); | |
466 | ||
467 | if (buf[attrlen - 1] == '\0') | |
468 | attrlen--; | |
469 | ||
470 | if (attrlen > pt->len) | |
c29f1845 | 471 | goto out_err; |
a5531a5d TG |
472 | } |
473 | break; | |
474 | ||
d30045a0 JB |
475 | case NLA_BINARY: |
476 | if (pt->len && attrlen > pt->len) | |
c29f1845 | 477 | goto out_err; |
d30045a0 JB |
478 | break; |
479 | ||
ea5693cc PM |
480 | case NLA_NESTED: |
481 | /* a nested attributes is allowed to be empty; if its not, | |
482 | * it must have a size of at least NLA_HDRLEN. | |
483 | */ | |
484 | if (attrlen == 0) | |
485 | break; | |
9a659a35 JB |
486 | if (attrlen < NLA_HDRLEN) |
487 | goto out_err; | |
47a1494b | 488 | if (pt->nested_policy) { |
7690aa1c JB |
489 | err = __nla_validate_parse(nla_data(nla), nla_len(nla), |
490 | pt->len, pt->nested_policy, | |
491 | validate, extack, NULL, | |
492 | depth + 1); | |
9a659a35 JB |
493 | if (err < 0) { |
494 | /* | |
495 | * return directly to preserve the inner | |
496 | * error message/attribute pointer | |
497 | */ | |
498 | return err; | |
499 | } | |
500 | } | |
501 | break; | |
1501d135 JB |
502 | case NLA_NESTED_ARRAY: |
503 | /* a nested array attribute is allowed to be empty; if its not, | |
504 | * it must have a size of at least NLA_HDRLEN. | |
505 | */ | |
506 | if (attrlen == 0) | |
507 | break; | |
508 | if (attrlen < NLA_HDRLEN) | |
509 | goto out_err; | |
47a1494b | 510 | if (pt->nested_policy) { |
1501d135 JB |
511 | int err; |
512 | ||
513 | err = nla_validate_array(nla_data(nla), nla_len(nla), | |
47a1494b | 514 | pt->len, pt->nested_policy, |
7690aa1c | 515 | extack, validate, depth); |
1501d135 JB |
516 | if (err < 0) { |
517 | /* | |
518 | * return directly to preserve the inner | |
519 | * error message/attribute pointer | |
520 | */ | |
521 | return err; | |
522 | } | |
523 | } | |
524 | break; | |
6f455f5f JB |
525 | |
526 | case NLA_UNSPEC: | |
8cb08174 JB |
527 | if (validate & NL_VALIDATE_UNSPEC) { |
528 | NL_SET_ERR_MSG_ATTR(extack, nla, | |
529 | "Unsupported attribute"); | |
530 | return -EINVAL; | |
531 | } | |
6f455f5f JB |
532 | if (attrlen < pt->len) |
533 | goto out_err; | |
534 | break; | |
535 | ||
a5531a5d TG |
536 | default: |
537 | if (pt->len) | |
538 | minlen = pt->len; | |
6f455f5f | 539 | else |
a5531a5d TG |
540 | minlen = nla_attr_minlen[pt->type]; |
541 | ||
542 | if (attrlen < minlen) | |
c29f1845 | 543 | goto out_err; |
a5531a5d | 544 | } |
bfa83a9e | 545 | |
3e48be05 JB |
546 | /* further validation */ |
547 | switch (pt->validation_type) { | |
548 | case NLA_VALIDATE_NONE: | |
549 | /* nothing to do */ | |
550 | break; | |
d06a09b9 | 551 | case NLA_VALIDATE_RANGE_PTR: |
3e48be05 | 552 | case NLA_VALIDATE_RANGE: |
8aa26c57 | 553 | case NLA_VALIDATE_RANGE_WARN_TOO_LONG: |
3e48be05 JB |
554 | case NLA_VALIDATE_MIN: |
555 | case NLA_VALIDATE_MAX: | |
8aa26c57 | 556 | err = nla_validate_int_range(pt, nla, extack, validate); |
3e48be05 JB |
557 | if (err) |
558 | return err; | |
559 | break; | |
bdbb4e29 JK |
560 | case NLA_VALIDATE_MASK: |
561 | err = nla_validate_mask(pt, nla, extack); | |
562 | if (err) | |
563 | return err; | |
564 | break; | |
33188bd6 JB |
565 | case NLA_VALIDATE_FUNCTION: |
566 | if (pt->validate) { | |
567 | err = pt->validate(nla, extack); | |
568 | if (err) | |
569 | return err; | |
570 | } | |
571 | break; | |
3e48be05 JB |
572 | } |
573 | ||
bfa83a9e | 574 | return 0; |
c29f1845 | 575 | out_err: |
44f3625b JB |
576 | NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
577 | "Attribute failed policy validation"); | |
c29f1845 | 578 | return err; |
bfa83a9e TG |
579 | } |
580 | ||
8cb08174 JB |
581 | static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype, |
582 | const struct nla_policy *policy, | |
583 | unsigned int validate, | |
584 | struct netlink_ext_ack *extack, | |
7690aa1c | 585 | struct nlattr **tb, unsigned int depth) |
8cb08174 JB |
586 | { |
587 | const struct nlattr *nla; | |
588 | int rem; | |
589 | ||
7690aa1c JB |
590 | if (depth >= MAX_POLICY_RECURSION_DEPTH) { |
591 | NL_SET_ERR_MSG(extack, | |
592 | "allowed policy recursion depth exceeded"); | |
593 | return -EINVAL; | |
594 | } | |
595 | ||
8cb08174 JB |
596 | if (tb) |
597 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); | |
598 | ||
599 | nla_for_each_attr(nla, head, len, rem) { | |
600 | u16 type = nla_type(nla); | |
601 | ||
602 | if (type == 0 || type > maxtype) { | |
603 | if (validate & NL_VALIDATE_MAXTYPE) { | |
d54a16b2 MK |
604 | NL_SET_ERR_MSG_ATTR(extack, nla, |
605 | "Unknown attribute type"); | |
8cb08174 JB |
606 | return -EINVAL; |
607 | } | |
608 | continue; | |
609 | } | |
610 | if (policy) { | |
611 | int err = validate_nla(nla, maxtype, policy, | |
7690aa1c | 612 | validate, extack, depth); |
8cb08174 JB |
613 | |
614 | if (err < 0) | |
615 | return err; | |
616 | } | |
617 | ||
618 | if (tb) | |
619 | tb[type] = (struct nlattr *)nla; | |
620 | } | |
621 | ||
622 | if (unlikely(rem > 0)) { | |
623 | pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", | |
624 | rem, current->comm); | |
625 | NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes"); | |
626 | if (validate & NL_VALIDATE_TRAILING) | |
627 | return -EINVAL; | |
628 | } | |
629 | ||
630 | return 0; | |
631 | } | |
632 | ||
bfa83a9e | 633 | /** |
8cb08174 | 634 | * __nla_validate - Validate a stream of attributes |
bfa83a9e TG |
635 | * @head: head of attribute stream |
636 | * @len: length of attribute stream | |
637 | * @maxtype: maximum attribute type to be expected | |
638 | * @policy: validation policy | |
8cb08174 | 639 | * @validate: validation strictness |
fceb6435 | 640 | * @extack: extended ACK report struct |
bfa83a9e TG |
641 | * |
642 | * Validates all attributes in the specified attribute stream against the | |
8cb08174 JB |
643 | * specified policy. Validation depends on the validate flags passed, see |
644 | * &enum netlink_validation for more details on that. | |
9dbbc3b9 | 645 | * See documentation of struct nla_policy for more details. |
bfa83a9e TG |
646 | * |
647 | * Returns 0 on success or a negative error code. | |
648 | */ | |
8cb08174 JB |
649 | int __nla_validate(const struct nlattr *head, int len, int maxtype, |
650 | const struct nla_policy *policy, unsigned int validate, | |
651 | struct netlink_ext_ack *extack) | |
bfa83a9e | 652 | { |
8cb08174 | 653 | return __nla_validate_parse(head, len, maxtype, policy, validate, |
7690aa1c | 654 | extack, NULL, 0); |
bfa83a9e | 655 | } |
8cb08174 | 656 | EXPORT_SYMBOL(__nla_validate); |
bfa83a9e | 657 | |
e487eb99 | 658 | /** |
9dbbc3b9 | 659 | * nla_policy_len - Determine the max. length of a policy |
e487eb99 HE |
660 | * @policy: policy to use |
661 | * @n: number of policies | |
662 | * | |
663 | * Determines the max. length of the policy. It is currently used | |
664 | * to allocated Netlink buffers roughly the size of the actual | |
665 | * message. | |
666 | * | |
667 | * Returns 0 on success or a negative error code. | |
668 | */ | |
669 | int | |
670 | nla_policy_len(const struct nla_policy *p, int n) | |
671 | { | |
672 | int i, len = 0; | |
673 | ||
e3fa3aff | 674 | for (i = 0; i < n; i++, p++) { |
e487eb99 HE |
675 | if (p->len) |
676 | len += nla_total_size(p->len); | |
28033ae4 DA |
677 | else if (nla_attr_len[p->type]) |
678 | len += nla_total_size(nla_attr_len[p->type]); | |
e487eb99 HE |
679 | else if (nla_attr_minlen[p->type]) |
680 | len += nla_total_size(nla_attr_minlen[p->type]); | |
681 | } | |
682 | ||
683 | return len; | |
684 | } | |
6d6a138f | 685 | EXPORT_SYMBOL(nla_policy_len); |
e487eb99 | 686 | |
bfa83a9e | 687 | /** |
8cb08174 | 688 | * __nla_parse - Parse a stream of attributes into a tb buffer |
bfa83a9e TG |
689 | * @tb: destination array with maxtype+1 elements |
690 | * @maxtype: maximum attribute type to be expected | |
691 | * @head: head of attribute stream | |
692 | * @len: length of attribute stream | |
10b595af | 693 | * @policy: validation policy |
8cb08174 JB |
694 | * @validate: validation strictness |
695 | * @extack: extended ACK pointer | |
bfa83a9e TG |
696 | * |
697 | * Parses a stream of attributes and stores a pointer to each attribute in | |
8cb08174 JB |
698 | * the tb array accessible via the attribute type. |
699 | * Validation is controlled by the @validate parameter. | |
bfa83a9e TG |
700 | * |
701 | * Returns 0 on success or a negative error code. | |
702 | */ | |
8cb08174 JB |
703 | int __nla_parse(struct nlattr **tb, int maxtype, |
704 | const struct nlattr *head, int len, | |
705 | const struct nla_policy *policy, unsigned int validate, | |
706 | struct netlink_ext_ack *extack) | |
a5f6cba2 | 707 | { |
8cb08174 | 708 | return __nla_validate_parse(head, len, maxtype, policy, validate, |
7690aa1c | 709 | extack, tb, 0); |
a5f6cba2 | 710 | } |
8cb08174 | 711 | EXPORT_SYMBOL(__nla_parse); |
a5f6cba2 | 712 | |
bfa83a9e TG |
713 | /** |
714 | * nla_find - Find a specific attribute in a stream of attributes | |
715 | * @head: head of attribute stream | |
716 | * @len: length of attribute stream | |
717 | * @attrtype: type of attribute to look for | |
718 | * | |
719 | * Returns the first attribute in the stream matching the specified type. | |
720 | */ | |
3654654f | 721 | struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype) |
bfa83a9e | 722 | { |
3654654f | 723 | const struct nlattr *nla; |
bfa83a9e TG |
724 | int rem; |
725 | ||
726 | nla_for_each_attr(nla, head, len, rem) | |
8f4c1f9b | 727 | if (nla_type(nla) == attrtype) |
3654654f | 728 | return (struct nlattr *)nla; |
bfa83a9e TG |
729 | |
730 | return NULL; | |
731 | } | |
6d6a138f | 732 | EXPORT_SYMBOL(nla_find); |
bfa83a9e TG |
733 | |
734 | /** | |
872f6903 | 735 | * nla_strscpy - Copy string attribute payload into a sized buffer |
9ca71874 FL |
736 | * @dst: Where to copy the string to. |
737 | * @nla: Attribute to copy the string from. | |
738 | * @dstsize: Size of destination buffer. | |
bfa83a9e TG |
739 | * |
740 | * Copies at most dstsize - 1 bytes into the destination buffer. | |
9ca71874 | 741 | * Unlike strlcpy the destination buffer is always padded out. |
bfa83a9e | 742 | * |
9ca71874 FL |
743 | * Return: |
744 | * * srclen - Returns @nla length (not including the trailing %NUL). | |
745 | * * -E2BIG - If @dstsize is 0 or greater than U16_MAX or @nla length greater | |
746 | * than @dstsize. | |
bfa83a9e | 747 | */ |
872f6903 | 748 | ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize) |
bfa83a9e TG |
749 | { |
750 | size_t srclen = nla_len(nla); | |
751 | char *src = nla_data(nla); | |
9ca71874 FL |
752 | ssize_t ret; |
753 | size_t len; | |
754 | ||
755 | if (dstsize == 0 || WARN_ON_ONCE(dstsize > U16_MAX)) | |
756 | return -E2BIG; | |
bfa83a9e TG |
757 | |
758 | if (srclen > 0 && src[srclen - 1] == '\0') | |
759 | srclen--; | |
760 | ||
9ca71874 FL |
761 | if (srclen >= dstsize) { |
762 | len = dstsize - 1; | |
763 | ret = -E2BIG; | |
764 | } else { | |
765 | len = srclen; | |
766 | ret = len; | |
bfa83a9e TG |
767 | } |
768 | ||
9ca71874 FL |
769 | memcpy(dst, src, len); |
770 | /* Zero pad end of dst. */ | |
771 | memset(dst + len, 0, dstsize - len); | |
772 | ||
773 | return ret; | |
bfa83a9e | 774 | } |
872f6903 | 775 | EXPORT_SYMBOL(nla_strscpy); |
bfa83a9e | 776 | |
2cf0c8b3 PS |
777 | /** |
778 | * nla_strdup - Copy string attribute payload into a newly allocated buffer | |
779 | * @nla: attribute to copy the string from | |
780 | * @flags: the type of memory to allocate (see kmalloc). | |
781 | * | |
782 | * Returns a pointer to the allocated buffer or NULL on error. | |
783 | */ | |
784 | char *nla_strdup(const struct nlattr *nla, gfp_t flags) | |
785 | { | |
786 | size_t srclen = nla_len(nla); | |
787 | char *src = nla_data(nla), *dst; | |
788 | ||
789 | if (srclen > 0 && src[srclen - 1] == '\0') | |
790 | srclen--; | |
791 | ||
792 | dst = kmalloc(srclen + 1, flags); | |
793 | if (dst != NULL) { | |
794 | memcpy(dst, src, srclen); | |
795 | dst[srclen] = '\0'; | |
796 | } | |
797 | return dst; | |
798 | } | |
799 | EXPORT_SYMBOL(nla_strdup); | |
800 | ||
bfa83a9e TG |
801 | /** |
802 | * nla_memcpy - Copy a netlink attribute into another memory area | |
803 | * @dest: where to copy to memcpy | |
804 | * @src: netlink attribute to copy from | |
805 | * @count: size of the destination area | |
806 | * | |
807 | * Note: The number of bytes copied is limited by the length of | |
808 | * attribute's payload. memcpy | |
809 | * | |
810 | * Returns the number of bytes copied. | |
811 | */ | |
b057efd4 | 812 | int nla_memcpy(void *dest, const struct nlattr *src, int count) |
bfa83a9e TG |
813 | { |
814 | int minlen = min_t(int, count, nla_len(src)); | |
815 | ||
816 | memcpy(dest, nla_data(src), minlen); | |
5899f047 JB |
817 | if (count > minlen) |
818 | memset(dest + minlen, 0, count - minlen); | |
bfa83a9e TG |
819 | |
820 | return minlen; | |
821 | } | |
6d6a138f | 822 | EXPORT_SYMBOL(nla_memcpy); |
bfa83a9e TG |
823 | |
824 | /** | |
825 | * nla_memcmp - Compare an attribute with sized memory area | |
826 | * @nla: netlink attribute | |
827 | * @data: memory area | |
828 | * @size: size of memory area | |
829 | */ | |
830 | int nla_memcmp(const struct nlattr *nla, const void *data, | |
831 | size_t size) | |
832 | { | |
833 | int d = nla_len(nla) - size; | |
834 | ||
835 | if (d == 0) | |
836 | d = memcmp(nla_data(nla), data, size); | |
837 | ||
838 | return d; | |
839 | } | |
6d6a138f | 840 | EXPORT_SYMBOL(nla_memcmp); |
bfa83a9e TG |
841 | |
842 | /** | |
843 | * nla_strcmp - Compare a string attribute against a string | |
844 | * @nla: netlink string attribute | |
845 | * @str: another string | |
846 | */ | |
847 | int nla_strcmp(const struct nlattr *nla, const char *str) | |
848 | { | |
8b7b9324 PN |
849 | int len = strlen(str); |
850 | char *buf = nla_data(nla); | |
851 | int attrlen = nla_len(nla); | |
852 | int d; | |
bfa83a9e | 853 | |
2c16db6c | 854 | while (attrlen > 0 && buf[attrlen - 1] == '\0') |
8b7b9324 PN |
855 | attrlen--; |
856 | ||
857 | d = attrlen - len; | |
bfa83a9e TG |
858 | if (d == 0) |
859 | d = memcmp(nla_data(nla), str, len); | |
860 | ||
861 | return d; | |
862 | } | |
6d6a138f | 863 | EXPORT_SYMBOL(nla_strcmp); |
bfa83a9e | 864 | |
90800216 | 865 | #ifdef CONFIG_NET |
bfa83a9e TG |
866 | /** |
867 | * __nla_reserve - reserve room for attribute on the skb | |
868 | * @skb: socket buffer to reserve room on | |
869 | * @attrtype: attribute type | |
870 | * @attrlen: length of attribute payload | |
871 | * | |
872 | * Adds a netlink attribute header to a socket buffer and reserves | |
873 | * room for the payload but does not copy it. | |
874 | * | |
875 | * The caller is responsible to ensure that the skb provides enough | |
876 | * tailroom for the attribute header and payload. | |
877 | */ | |
878 | struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | |
879 | { | |
880 | struct nlattr *nla; | |
881 | ||
4df864c1 | 882 | nla = skb_put(skb, nla_total_size(attrlen)); |
bfa83a9e TG |
883 | nla->nla_type = attrtype; |
884 | nla->nla_len = nla_attr_size(attrlen); | |
885 | ||
886 | memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen)); | |
887 | ||
888 | return nla; | |
889 | } | |
90800216 | 890 | EXPORT_SYMBOL(__nla_reserve); |
bfa83a9e | 891 | |
089bf1a6 ND |
892 | /** |
893 | * __nla_reserve_64bit - reserve room for attribute on the skb and align it | |
894 | * @skb: socket buffer to reserve room on | |
895 | * @attrtype: attribute type | |
896 | * @attrlen: length of attribute payload | |
11a99573 | 897 | * @padattr: attribute type for the padding |
089bf1a6 ND |
898 | * |
899 | * Adds a netlink attribute header to a socket buffer and reserves | |
900 | * room for the payload but does not copy it. It also ensure that this | |
11a99573 | 901 | * attribute will have a 64-bit aligned nla_data() area. |
089bf1a6 ND |
902 | * |
903 | * The caller is responsible to ensure that the skb provides enough | |
904 | * tailroom for the attribute header and payload. | |
905 | */ | |
906 | struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, | |
907 | int attrlen, int padattr) | |
908 | { | |
4718a471 | 909 | nla_align_64bit(skb, padattr); |
089bf1a6 ND |
910 | |
911 | return __nla_reserve(skb, attrtype, attrlen); | |
912 | } | |
913 | EXPORT_SYMBOL(__nla_reserve_64bit); | |
914 | ||
fe4944e5 TG |
915 | /** |
916 | * __nla_reserve_nohdr - reserve room for attribute without header | |
917 | * @skb: socket buffer to reserve room on | |
918 | * @attrlen: length of attribute payload | |
919 | * | |
920 | * Reserves room for attribute payload without a header. | |
921 | * | |
922 | * The caller is responsible to ensure that the skb provides enough | |
923 | * tailroom for the payload. | |
924 | */ | |
925 | void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen) | |
926 | { | |
b952f4df | 927 | return skb_put_zero(skb, NLA_ALIGN(attrlen)); |
fe4944e5 | 928 | } |
90800216 | 929 | EXPORT_SYMBOL(__nla_reserve_nohdr); |
fe4944e5 | 930 | |
bfa83a9e TG |
931 | /** |
932 | * nla_reserve - reserve room for attribute on the skb | |
933 | * @skb: socket buffer to reserve room on | |
934 | * @attrtype: attribute type | |
935 | * @attrlen: length of attribute payload | |
936 | * | |
937 | * Adds a netlink attribute header to a socket buffer and reserves | |
938 | * room for the payload but does not copy it. | |
939 | * | |
940 | * Returns NULL if the tailroom of the skb is insufficient to store | |
941 | * the attribute header and payload. | |
942 | */ | |
943 | struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | |
944 | { | |
945 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | |
946 | return NULL; | |
947 | ||
948 | return __nla_reserve(skb, attrtype, attrlen); | |
949 | } | |
90800216 | 950 | EXPORT_SYMBOL(nla_reserve); |
bfa83a9e | 951 | |
089bf1a6 ND |
952 | /** |
953 | * nla_reserve_64bit - reserve room for attribute on the skb and align it | |
954 | * @skb: socket buffer to reserve room on | |
955 | * @attrtype: attribute type | |
956 | * @attrlen: length of attribute payload | |
11a99573 | 957 | * @padattr: attribute type for the padding |
089bf1a6 ND |
958 | * |
959 | * Adds a netlink attribute header to a socket buffer and reserves | |
960 | * room for the payload but does not copy it. It also ensure that this | |
11a99573 | 961 | * attribute will have a 64-bit aligned nla_data() area. |
089bf1a6 ND |
962 | * |
963 | * Returns NULL if the tailroom of the skb is insufficient to store | |
964 | * the attribute header and payload. | |
965 | */ | |
966 | struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
967 | int padattr) | |
968 | { | |
969 | size_t len; | |
970 | ||
971 | if (nla_need_padding_for_64bit(skb)) | |
972 | len = nla_total_size_64bit(attrlen); | |
973 | else | |
974 | len = nla_total_size(attrlen); | |
975 | if (unlikely(skb_tailroom(skb) < len)) | |
976 | return NULL; | |
977 | ||
978 | return __nla_reserve_64bit(skb, attrtype, attrlen, padattr); | |
979 | } | |
980 | EXPORT_SYMBOL(nla_reserve_64bit); | |
981 | ||
fe4944e5 | 982 | /** |
10b595af | 983 | * nla_reserve_nohdr - reserve room for attribute without header |
fe4944e5 | 984 | * @skb: socket buffer to reserve room on |
10b595af | 985 | * @attrlen: length of attribute payload |
fe4944e5 TG |
986 | * |
987 | * Reserves room for attribute payload without a header. | |
988 | * | |
989 | * Returns NULL if the tailroom of the skb is insufficient to store | |
990 | * the attribute payload. | |
991 | */ | |
992 | void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen) | |
993 | { | |
994 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
995 | return NULL; | |
996 | ||
997 | return __nla_reserve_nohdr(skb, attrlen); | |
998 | } | |
90800216 | 999 | EXPORT_SYMBOL(nla_reserve_nohdr); |
fe4944e5 | 1000 | |
bfa83a9e TG |
1001 | /** |
1002 | * __nla_put - Add a netlink attribute to a socket buffer | |
1003 | * @skb: socket buffer to add attribute to | |
1004 | * @attrtype: attribute type | |
1005 | * @attrlen: length of attribute payload | |
1006 | * @data: head of attribute payload | |
1007 | * | |
1008 | * The caller is responsible to ensure that the skb provides enough | |
1009 | * tailroom for the attribute header and payload. | |
1010 | */ | |
1011 | void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, | |
1012 | const void *data) | |
1013 | { | |
1014 | struct nlattr *nla; | |
1015 | ||
1016 | nla = __nla_reserve(skb, attrtype, attrlen); | |
1017 | memcpy(nla_data(nla), data, attrlen); | |
1018 | } | |
90800216 | 1019 | EXPORT_SYMBOL(__nla_put); |
bfa83a9e | 1020 | |
089bf1a6 ND |
1021 | /** |
1022 | * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it | |
1023 | * @skb: socket buffer to add attribute to | |
1024 | * @attrtype: attribute type | |
1025 | * @attrlen: length of attribute payload | |
1026 | * @data: head of attribute payload | |
11a99573 | 1027 | * @padattr: attribute type for the padding |
089bf1a6 ND |
1028 | * |
1029 | * The caller is responsible to ensure that the skb provides enough | |
1030 | * tailroom for the attribute header and payload. | |
1031 | */ | |
1032 | void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
1033 | const void *data, int padattr) | |
1034 | { | |
1035 | struct nlattr *nla; | |
1036 | ||
1037 | nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr); | |
1038 | memcpy(nla_data(nla), data, attrlen); | |
1039 | } | |
1040 | EXPORT_SYMBOL(__nla_put_64bit); | |
1041 | ||
fe4944e5 TG |
1042 | /** |
1043 | * __nla_put_nohdr - Add a netlink attribute without header | |
1044 | * @skb: socket buffer to add attribute to | |
1045 | * @attrlen: length of attribute payload | |
1046 | * @data: head of attribute payload | |
1047 | * | |
1048 | * The caller is responsible to ensure that the skb provides enough | |
1049 | * tailroom for the attribute payload. | |
1050 | */ | |
1051 | void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) | |
1052 | { | |
1053 | void *start; | |
1054 | ||
1055 | start = __nla_reserve_nohdr(skb, attrlen); | |
1056 | memcpy(start, data, attrlen); | |
1057 | } | |
90800216 | 1058 | EXPORT_SYMBOL(__nla_put_nohdr); |
bfa83a9e TG |
1059 | |
1060 | /** | |
1061 | * nla_put - Add a netlink attribute to a socket buffer | |
1062 | * @skb: socket buffer to add attribute to | |
1063 | * @attrtype: attribute type | |
1064 | * @attrlen: length of attribute payload | |
1065 | * @data: head of attribute payload | |
1066 | * | |
bc3ed28c | 1067 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
bfa83a9e TG |
1068 | * the attribute header and payload. |
1069 | */ | |
1070 | int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data) | |
1071 | { | |
1072 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | |
bc3ed28c | 1073 | return -EMSGSIZE; |
bfa83a9e TG |
1074 | |
1075 | __nla_put(skb, attrtype, attrlen, data); | |
1076 | return 0; | |
1077 | } | |
90800216 | 1078 | EXPORT_SYMBOL(nla_put); |
bfa83a9e | 1079 | |
089bf1a6 ND |
1080 | /** |
1081 | * nla_put_64bit - Add a netlink attribute to a socket buffer and align it | |
1082 | * @skb: socket buffer to add attribute to | |
1083 | * @attrtype: attribute type | |
1084 | * @attrlen: length of attribute payload | |
1085 | * @data: head of attribute payload | |
11a99573 | 1086 | * @padattr: attribute type for the padding |
089bf1a6 ND |
1087 | * |
1088 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store | |
1089 | * the attribute header and payload. | |
1090 | */ | |
1091 | int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
1092 | const void *data, int padattr) | |
1093 | { | |
1094 | size_t len; | |
1095 | ||
1096 | if (nla_need_padding_for_64bit(skb)) | |
1097 | len = nla_total_size_64bit(attrlen); | |
1098 | else | |
1099 | len = nla_total_size(attrlen); | |
1100 | if (unlikely(skb_tailroom(skb) < len)) | |
1101 | return -EMSGSIZE; | |
1102 | ||
1103 | __nla_put_64bit(skb, attrtype, attrlen, data, padattr); | |
1104 | return 0; | |
1105 | } | |
1106 | EXPORT_SYMBOL(nla_put_64bit); | |
1107 | ||
fe4944e5 TG |
1108 | /** |
1109 | * nla_put_nohdr - Add a netlink attribute without header | |
1110 | * @skb: socket buffer to add attribute to | |
1111 | * @attrlen: length of attribute payload | |
1112 | * @data: head of attribute payload | |
1113 | * | |
bc3ed28c | 1114 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
fe4944e5 TG |
1115 | * the attribute payload. |
1116 | */ | |
1117 | int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) | |
1118 | { | |
1119 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
bc3ed28c | 1120 | return -EMSGSIZE; |
fe4944e5 TG |
1121 | |
1122 | __nla_put_nohdr(skb, attrlen, data); | |
1123 | return 0; | |
1124 | } | |
90800216 | 1125 | EXPORT_SYMBOL(nla_put_nohdr); |
bfa83a9e | 1126 | |
01480e1c PM |
1127 | /** |
1128 | * nla_append - Add a netlink attribute without header or padding | |
1129 | * @skb: socket buffer to add attribute to | |
1130 | * @attrlen: length of attribute payload | |
1131 | * @data: head of attribute payload | |
1132 | * | |
bc3ed28c | 1133 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
01480e1c PM |
1134 | * the attribute payload. |
1135 | */ | |
1136 | int nla_append(struct sk_buff *skb, int attrlen, const void *data) | |
1137 | { | |
1138 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
bc3ed28c | 1139 | return -EMSGSIZE; |
01480e1c | 1140 | |
59ae1d12 | 1141 | skb_put_data(skb, data, attrlen); |
01480e1c PM |
1142 | return 0; |
1143 | } | |
90800216 HX |
1144 | EXPORT_SYMBOL(nla_append); |
1145 | #endif |