]>
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 | ||
1501d135 JB |
70 | static int nla_validate_array(const struct nlattr *head, int len, int maxtype, |
71 | const struct nla_policy *policy, | |
8cb08174 JB |
72 | struct netlink_ext_ack *extack, |
73 | unsigned int validate) | |
1501d135 JB |
74 | { |
75 | const struct nlattr *entry; | |
76 | int rem; | |
77 | ||
78 | nla_for_each_attr(entry, head, len, rem) { | |
79 | int ret; | |
80 | ||
81 | if (nla_len(entry) == 0) | |
82 | continue; | |
83 | ||
84 | if (nla_len(entry) < NLA_HDRLEN) { | |
85 | NL_SET_ERR_MSG_ATTR(extack, entry, | |
86 | "Array element too short"); | |
87 | return -ERANGE; | |
88 | } | |
89 | ||
8cb08174 JB |
90 | ret = __nla_validate(nla_data(entry), nla_len(entry), |
91 | maxtype, policy, validate, extack); | |
1501d135 JB |
92 | if (ret < 0) |
93 | return ret; | |
94 | } | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
3e48be05 JB |
99 | static int nla_validate_int_range(const struct nla_policy *pt, |
100 | const struct nlattr *nla, | |
101 | struct netlink_ext_ack *extack) | |
102 | { | |
103 | bool validate_min, validate_max; | |
104 | s64 value; | |
105 | ||
106 | validate_min = pt->validation_type == NLA_VALIDATE_RANGE || | |
107 | pt->validation_type == NLA_VALIDATE_MIN; | |
108 | validate_max = pt->validation_type == NLA_VALIDATE_RANGE || | |
109 | pt->validation_type == NLA_VALIDATE_MAX; | |
110 | ||
111 | switch (pt->type) { | |
112 | case NLA_U8: | |
113 | value = nla_get_u8(nla); | |
114 | break; | |
115 | case NLA_U16: | |
116 | value = nla_get_u16(nla); | |
117 | break; | |
118 | case NLA_U32: | |
119 | value = nla_get_u32(nla); | |
120 | break; | |
121 | case NLA_S8: | |
122 | value = nla_get_s8(nla); | |
123 | break; | |
124 | case NLA_S16: | |
125 | value = nla_get_s16(nla); | |
126 | break; | |
127 | case NLA_S32: | |
128 | value = nla_get_s32(nla); | |
129 | break; | |
130 | case NLA_S64: | |
131 | value = nla_get_s64(nla); | |
132 | break; | |
133 | case NLA_U64: | |
134 | /* treat this one specially, since it may not fit into s64 */ | |
135 | if ((validate_min && nla_get_u64(nla) < pt->min) || | |
136 | (validate_max && nla_get_u64(nla) > pt->max)) { | |
137 | NL_SET_ERR_MSG_ATTR(extack, nla, | |
138 | "integer out of range"); | |
139 | return -ERANGE; | |
140 | } | |
141 | return 0; | |
142 | default: | |
143 | WARN_ON(1); | |
144 | return -EINVAL; | |
145 | } | |
146 | ||
147 | if ((validate_min && value < pt->min) || | |
148 | (validate_max && value > pt->max)) { | |
149 | NL_SET_ERR_MSG_ATTR(extack, nla, | |
150 | "integer out of range"); | |
151 | return -ERANGE; | |
152 | } | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
3654654f | 157 | static int validate_nla(const struct nlattr *nla, int maxtype, |
8cb08174 | 158 | const struct nla_policy *policy, unsigned int validate, |
c29f1845 | 159 | struct netlink_ext_ack *extack) |
bfa83a9e | 160 | { |
ef7c79ed | 161 | const struct nla_policy *pt; |
8f4c1f9b | 162 | int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla); |
c29f1845 | 163 | int err = -ERANGE; |
bfa83a9e | 164 | |
8f4c1f9b | 165 | if (type <= 0 || type > maxtype) |
bfa83a9e TG |
166 | return 0; |
167 | ||
8f4c1f9b | 168 | pt = &policy[type]; |
bfa83a9e TG |
169 | |
170 | BUG_ON(pt->type > NLA_TYPE_MAX); | |
171 | ||
b60b87fc JB |
172 | if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) || |
173 | (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) { | |
6e237d09 DA |
174 | pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n", |
175 | current->comm, type); | |
8cb08174 JB |
176 | if (validate & NL_VALIDATE_STRICT_ATTRS) { |
177 | NL_SET_ERR_MSG_ATTR(extack, nla, | |
178 | "invalid attribute length"); | |
179 | return -EINVAL; | |
180 | } | |
28033ae4 DA |
181 | } |
182 | ||
a5531a5d | 183 | switch (pt->type) { |
b60b87fc JB |
184 | case NLA_EXACT_LEN: |
185 | if (attrlen != pt->len) | |
c29f1845 | 186 | goto out_err; |
b60b87fc JB |
187 | break; |
188 | ||
568b742a | 189 | case NLA_REJECT: |
c29f1845 JB |
190 | if (extack && pt->validation_data) { |
191 | NL_SET_BAD_ATTR(extack, nla); | |
192 | extack->_msg = pt->validation_data; | |
193 | return -EINVAL; | |
194 | } | |
195 | err = -EINVAL; | |
196 | goto out_err; | |
568b742a | 197 | |
a5531a5d TG |
198 | case NLA_FLAG: |
199 | if (attrlen > 0) | |
c29f1845 | 200 | goto out_err; |
a5531a5d | 201 | break; |
bfa83a9e | 202 | |
64c83d83 JHS |
203 | case NLA_BITFIELD32: |
204 | if (attrlen != sizeof(struct nla_bitfield32)) | |
c29f1845 | 205 | goto out_err; |
64c83d83 | 206 | |
c29f1845 JB |
207 | err = validate_nla_bitfield32(nla, pt->validation_data); |
208 | if (err) | |
209 | goto out_err; | |
210 | break; | |
64c83d83 | 211 | |
a5531a5d TG |
212 | case NLA_NUL_STRING: |
213 | if (pt->len) | |
214 | minlen = min_t(int, attrlen, pt->len + 1); | |
215 | else | |
216 | minlen = attrlen; | |
bfa83a9e | 217 | |
c29f1845 JB |
218 | if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) { |
219 | err = -EINVAL; | |
220 | goto out_err; | |
221 | } | |
a5531a5d TG |
222 | /* fall through */ |
223 | ||
224 | case NLA_STRING: | |
225 | if (attrlen < 1) | |
c29f1845 | 226 | goto out_err; |
a5531a5d TG |
227 | |
228 | if (pt->len) { | |
229 | char *buf = nla_data(nla); | |
230 | ||
231 | if (buf[attrlen - 1] == '\0') | |
232 | attrlen--; | |
233 | ||
234 | if (attrlen > pt->len) | |
c29f1845 | 235 | goto out_err; |
a5531a5d TG |
236 | } |
237 | break; | |
238 | ||
d30045a0 JB |
239 | case NLA_BINARY: |
240 | if (pt->len && attrlen > pt->len) | |
c29f1845 | 241 | goto out_err; |
d30045a0 JB |
242 | break; |
243 | ||
ea5693cc PM |
244 | case NLA_NESTED: |
245 | /* a nested attributes is allowed to be empty; if its not, | |
246 | * it must have a size of at least NLA_HDRLEN. | |
247 | */ | |
248 | if (attrlen == 0) | |
249 | break; | |
9a659a35 JB |
250 | if (attrlen < NLA_HDRLEN) |
251 | goto out_err; | |
252 | if (pt->validation_data) { | |
8cb08174 JB |
253 | err = __nla_validate(nla_data(nla), nla_len(nla), pt->len, |
254 | pt->validation_data, validate, | |
255 | extack); | |
9a659a35 JB |
256 | if (err < 0) { |
257 | /* | |
258 | * return directly to preserve the inner | |
259 | * error message/attribute pointer | |
260 | */ | |
261 | return err; | |
262 | } | |
263 | } | |
264 | break; | |
1501d135 JB |
265 | case NLA_NESTED_ARRAY: |
266 | /* a nested array attribute is allowed to be empty; if its not, | |
267 | * it must have a size of at least NLA_HDRLEN. | |
268 | */ | |
269 | if (attrlen == 0) | |
270 | break; | |
271 | if (attrlen < NLA_HDRLEN) | |
272 | goto out_err; | |
273 | if (pt->validation_data) { | |
274 | int err; | |
275 | ||
276 | err = nla_validate_array(nla_data(nla), nla_len(nla), | |
277 | pt->len, pt->validation_data, | |
8cb08174 | 278 | extack, validate); |
1501d135 JB |
279 | if (err < 0) { |
280 | /* | |
281 | * return directly to preserve the inner | |
282 | * error message/attribute pointer | |
283 | */ | |
284 | return err; | |
285 | } | |
286 | } | |
287 | break; | |
6f455f5f JB |
288 | |
289 | case NLA_UNSPEC: | |
8cb08174 JB |
290 | if (validate & NL_VALIDATE_UNSPEC) { |
291 | NL_SET_ERR_MSG_ATTR(extack, nla, | |
292 | "Unsupported attribute"); | |
293 | return -EINVAL; | |
294 | } | |
295 | /* fall through */ | |
6f455f5f JB |
296 | case NLA_MIN_LEN: |
297 | if (attrlen < pt->len) | |
298 | goto out_err; | |
299 | break; | |
300 | ||
a5531a5d TG |
301 | default: |
302 | if (pt->len) | |
303 | minlen = pt->len; | |
6f455f5f | 304 | else |
a5531a5d TG |
305 | minlen = nla_attr_minlen[pt->type]; |
306 | ||
307 | if (attrlen < minlen) | |
c29f1845 | 308 | goto out_err; |
a5531a5d | 309 | } |
bfa83a9e | 310 | |
3e48be05 JB |
311 | /* further validation */ |
312 | switch (pt->validation_type) { | |
313 | case NLA_VALIDATE_NONE: | |
314 | /* nothing to do */ | |
315 | break; | |
316 | case NLA_VALIDATE_RANGE: | |
317 | case NLA_VALIDATE_MIN: | |
318 | case NLA_VALIDATE_MAX: | |
319 | err = nla_validate_int_range(pt, nla, extack); | |
320 | if (err) | |
321 | return err; | |
322 | break; | |
33188bd6 JB |
323 | case NLA_VALIDATE_FUNCTION: |
324 | if (pt->validate) { | |
325 | err = pt->validate(nla, extack); | |
326 | if (err) | |
327 | return err; | |
328 | } | |
329 | break; | |
3e48be05 JB |
330 | } |
331 | ||
bfa83a9e | 332 | return 0; |
c29f1845 JB |
333 | out_err: |
334 | NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation"); | |
335 | return err; | |
bfa83a9e TG |
336 | } |
337 | ||
8cb08174 JB |
338 | static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype, |
339 | const struct nla_policy *policy, | |
340 | unsigned int validate, | |
341 | struct netlink_ext_ack *extack, | |
342 | struct nlattr **tb) | |
343 | { | |
344 | const struct nlattr *nla; | |
345 | int rem; | |
346 | ||
347 | if (tb) | |
348 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); | |
349 | ||
350 | nla_for_each_attr(nla, head, len, rem) { | |
351 | u16 type = nla_type(nla); | |
352 | ||
353 | if (type == 0 || type > maxtype) { | |
354 | if (validate & NL_VALIDATE_MAXTYPE) { | |
355 | NL_SET_ERR_MSG(extack, "Unknown attribute type"); | |
356 | return -EINVAL; | |
357 | } | |
358 | continue; | |
359 | } | |
360 | if (policy) { | |
361 | int err = validate_nla(nla, maxtype, policy, | |
362 | validate, extack); | |
363 | ||
364 | if (err < 0) | |
365 | return err; | |
366 | } | |
367 | ||
368 | if (tb) | |
369 | tb[type] = (struct nlattr *)nla; | |
370 | } | |
371 | ||
372 | if (unlikely(rem > 0)) { | |
373 | pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", | |
374 | rem, current->comm); | |
375 | NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes"); | |
376 | if (validate & NL_VALIDATE_TRAILING) | |
377 | return -EINVAL; | |
378 | } | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
bfa83a9e | 383 | /** |
8cb08174 | 384 | * __nla_validate - Validate a stream of attributes |
bfa83a9e TG |
385 | * @head: head of attribute stream |
386 | * @len: length of attribute stream | |
387 | * @maxtype: maximum attribute type to be expected | |
388 | * @policy: validation policy | |
8cb08174 | 389 | * @validate: validation strictness |
fceb6435 | 390 | * @extack: extended ACK report struct |
bfa83a9e TG |
391 | * |
392 | * Validates all attributes in the specified attribute stream against the | |
8cb08174 JB |
393 | * specified policy. Validation depends on the validate flags passed, see |
394 | * &enum netlink_validation for more details on that. | |
395 | * See documenation of struct nla_policy for more details. | |
bfa83a9e TG |
396 | * |
397 | * Returns 0 on success or a negative error code. | |
398 | */ | |
8cb08174 JB |
399 | int __nla_validate(const struct nlattr *head, int len, int maxtype, |
400 | const struct nla_policy *policy, unsigned int validate, | |
401 | struct netlink_ext_ack *extack) | |
bfa83a9e | 402 | { |
8cb08174 JB |
403 | return __nla_validate_parse(head, len, maxtype, policy, validate, |
404 | extack, NULL); | |
bfa83a9e | 405 | } |
8cb08174 | 406 | EXPORT_SYMBOL(__nla_validate); |
bfa83a9e | 407 | |
e487eb99 HE |
408 | /** |
409 | * nla_policy_len - Determin the max. length of a policy | |
410 | * @policy: policy to use | |
411 | * @n: number of policies | |
412 | * | |
413 | * Determines the max. length of the policy. It is currently used | |
414 | * to allocated Netlink buffers roughly the size of the actual | |
415 | * message. | |
416 | * | |
417 | * Returns 0 on success or a negative error code. | |
418 | */ | |
419 | int | |
420 | nla_policy_len(const struct nla_policy *p, int n) | |
421 | { | |
422 | int i, len = 0; | |
423 | ||
e3fa3aff | 424 | for (i = 0; i < n; i++, p++) { |
e487eb99 HE |
425 | if (p->len) |
426 | len += nla_total_size(p->len); | |
28033ae4 DA |
427 | else if (nla_attr_len[p->type]) |
428 | len += nla_total_size(nla_attr_len[p->type]); | |
e487eb99 HE |
429 | else if (nla_attr_minlen[p->type]) |
430 | len += nla_total_size(nla_attr_minlen[p->type]); | |
431 | } | |
432 | ||
433 | return len; | |
434 | } | |
6d6a138f | 435 | EXPORT_SYMBOL(nla_policy_len); |
e487eb99 | 436 | |
bfa83a9e | 437 | /** |
8cb08174 | 438 | * __nla_parse - Parse a stream of attributes into a tb buffer |
bfa83a9e TG |
439 | * @tb: destination array with maxtype+1 elements |
440 | * @maxtype: maximum attribute type to be expected | |
441 | * @head: head of attribute stream | |
442 | * @len: length of attribute stream | |
10b595af | 443 | * @policy: validation policy |
8cb08174 JB |
444 | * @validate: validation strictness |
445 | * @extack: extended ACK pointer | |
bfa83a9e TG |
446 | * |
447 | * Parses a stream of attributes and stores a pointer to each attribute in | |
8cb08174 JB |
448 | * the tb array accessible via the attribute type. |
449 | * Validation is controlled by the @validate parameter. | |
bfa83a9e TG |
450 | * |
451 | * Returns 0 on success or a negative error code. | |
452 | */ | |
8cb08174 JB |
453 | int __nla_parse(struct nlattr **tb, int maxtype, |
454 | const struct nlattr *head, int len, | |
455 | const struct nla_policy *policy, unsigned int validate, | |
456 | struct netlink_ext_ack *extack) | |
a5f6cba2 | 457 | { |
8cb08174 JB |
458 | return __nla_validate_parse(head, len, maxtype, policy, validate, |
459 | extack, tb); | |
a5f6cba2 | 460 | } |
8cb08174 | 461 | EXPORT_SYMBOL(__nla_parse); |
a5f6cba2 | 462 | |
bfa83a9e TG |
463 | /** |
464 | * nla_find - Find a specific attribute in a stream of attributes | |
465 | * @head: head of attribute stream | |
466 | * @len: length of attribute stream | |
467 | * @attrtype: type of attribute to look for | |
468 | * | |
469 | * Returns the first attribute in the stream matching the specified type. | |
470 | */ | |
3654654f | 471 | struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype) |
bfa83a9e | 472 | { |
3654654f | 473 | const struct nlattr *nla; |
bfa83a9e TG |
474 | int rem; |
475 | ||
476 | nla_for_each_attr(nla, head, len, rem) | |
8f4c1f9b | 477 | if (nla_type(nla) == attrtype) |
3654654f | 478 | return (struct nlattr *)nla; |
bfa83a9e TG |
479 | |
480 | return NULL; | |
481 | } | |
6d6a138f | 482 | EXPORT_SYMBOL(nla_find); |
bfa83a9e TG |
483 | |
484 | /** | |
485 | * nla_strlcpy - Copy string attribute payload into a sized buffer | |
486 | * @dst: where to copy the string to | |
10b595af | 487 | * @nla: attribute to copy the string from |
bfa83a9e TG |
488 | * @dstsize: size of destination buffer |
489 | * | |
490 | * Copies at most dstsize - 1 bytes into the destination buffer. | |
491 | * The result is always a valid NUL-terminated string. Unlike | |
492 | * strlcpy the destination buffer is always padded out. | |
493 | * | |
494 | * Returns the length of the source buffer. | |
495 | */ | |
496 | size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize) | |
497 | { | |
498 | size_t srclen = nla_len(nla); | |
499 | char *src = nla_data(nla); | |
500 | ||
501 | if (srclen > 0 && src[srclen - 1] == '\0') | |
502 | srclen--; | |
503 | ||
504 | if (dstsize > 0) { | |
505 | size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen; | |
506 | ||
507 | memset(dst, 0, dstsize); | |
508 | memcpy(dst, src, len); | |
509 | } | |
510 | ||
511 | return srclen; | |
512 | } | |
6d6a138f | 513 | EXPORT_SYMBOL(nla_strlcpy); |
bfa83a9e | 514 | |
2cf0c8b3 PS |
515 | /** |
516 | * nla_strdup - Copy string attribute payload into a newly allocated buffer | |
517 | * @nla: attribute to copy the string from | |
518 | * @flags: the type of memory to allocate (see kmalloc). | |
519 | * | |
520 | * Returns a pointer to the allocated buffer or NULL on error. | |
521 | */ | |
522 | char *nla_strdup(const struct nlattr *nla, gfp_t flags) | |
523 | { | |
524 | size_t srclen = nla_len(nla); | |
525 | char *src = nla_data(nla), *dst; | |
526 | ||
527 | if (srclen > 0 && src[srclen - 1] == '\0') | |
528 | srclen--; | |
529 | ||
530 | dst = kmalloc(srclen + 1, flags); | |
531 | if (dst != NULL) { | |
532 | memcpy(dst, src, srclen); | |
533 | dst[srclen] = '\0'; | |
534 | } | |
535 | return dst; | |
536 | } | |
537 | EXPORT_SYMBOL(nla_strdup); | |
538 | ||
bfa83a9e TG |
539 | /** |
540 | * nla_memcpy - Copy a netlink attribute into another memory area | |
541 | * @dest: where to copy to memcpy | |
542 | * @src: netlink attribute to copy from | |
543 | * @count: size of the destination area | |
544 | * | |
545 | * Note: The number of bytes copied is limited by the length of | |
546 | * attribute's payload. memcpy | |
547 | * | |
548 | * Returns the number of bytes copied. | |
549 | */ | |
b057efd4 | 550 | int nla_memcpy(void *dest, const struct nlattr *src, int count) |
bfa83a9e TG |
551 | { |
552 | int minlen = min_t(int, count, nla_len(src)); | |
553 | ||
554 | memcpy(dest, nla_data(src), minlen); | |
5899f047 JB |
555 | if (count > minlen) |
556 | memset(dest + minlen, 0, count - minlen); | |
bfa83a9e TG |
557 | |
558 | return minlen; | |
559 | } | |
6d6a138f | 560 | EXPORT_SYMBOL(nla_memcpy); |
bfa83a9e TG |
561 | |
562 | /** | |
563 | * nla_memcmp - Compare an attribute with sized memory area | |
564 | * @nla: netlink attribute | |
565 | * @data: memory area | |
566 | * @size: size of memory area | |
567 | */ | |
568 | int nla_memcmp(const struct nlattr *nla, const void *data, | |
569 | size_t size) | |
570 | { | |
571 | int d = nla_len(nla) - size; | |
572 | ||
573 | if (d == 0) | |
574 | d = memcmp(nla_data(nla), data, size); | |
575 | ||
576 | return d; | |
577 | } | |
6d6a138f | 578 | EXPORT_SYMBOL(nla_memcmp); |
bfa83a9e TG |
579 | |
580 | /** | |
581 | * nla_strcmp - Compare a string attribute against a string | |
582 | * @nla: netlink string attribute | |
583 | * @str: another string | |
584 | */ | |
585 | int nla_strcmp(const struct nlattr *nla, const char *str) | |
586 | { | |
8b7b9324 PN |
587 | int len = strlen(str); |
588 | char *buf = nla_data(nla); | |
589 | int attrlen = nla_len(nla); | |
590 | int d; | |
bfa83a9e | 591 | |
8b7b9324 PN |
592 | if (attrlen > 0 && buf[attrlen - 1] == '\0') |
593 | attrlen--; | |
594 | ||
595 | d = attrlen - len; | |
bfa83a9e TG |
596 | if (d == 0) |
597 | d = memcmp(nla_data(nla), str, len); | |
598 | ||
599 | return d; | |
600 | } | |
6d6a138f | 601 | EXPORT_SYMBOL(nla_strcmp); |
bfa83a9e | 602 | |
90800216 | 603 | #ifdef CONFIG_NET |
bfa83a9e TG |
604 | /** |
605 | * __nla_reserve - reserve room for attribute on the skb | |
606 | * @skb: socket buffer to reserve room on | |
607 | * @attrtype: attribute type | |
608 | * @attrlen: length of attribute payload | |
609 | * | |
610 | * Adds a netlink attribute header to a socket buffer and reserves | |
611 | * room for the payload but does not copy it. | |
612 | * | |
613 | * The caller is responsible to ensure that the skb provides enough | |
614 | * tailroom for the attribute header and payload. | |
615 | */ | |
616 | struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | |
617 | { | |
618 | struct nlattr *nla; | |
619 | ||
4df864c1 | 620 | nla = skb_put(skb, nla_total_size(attrlen)); |
bfa83a9e TG |
621 | nla->nla_type = attrtype; |
622 | nla->nla_len = nla_attr_size(attrlen); | |
623 | ||
624 | memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen)); | |
625 | ||
626 | return nla; | |
627 | } | |
90800216 | 628 | EXPORT_SYMBOL(__nla_reserve); |
bfa83a9e | 629 | |
089bf1a6 ND |
630 | /** |
631 | * __nla_reserve_64bit - reserve room for attribute on the skb and align it | |
632 | * @skb: socket buffer to reserve room on | |
633 | * @attrtype: attribute type | |
634 | * @attrlen: length of attribute payload | |
11a99573 | 635 | * @padattr: attribute type for the padding |
089bf1a6 ND |
636 | * |
637 | * Adds a netlink attribute header to a socket buffer and reserves | |
638 | * room for the payload but does not copy it. It also ensure that this | |
11a99573 | 639 | * attribute will have a 64-bit aligned nla_data() area. |
089bf1a6 ND |
640 | * |
641 | * The caller is responsible to ensure that the skb provides enough | |
642 | * tailroom for the attribute header and payload. | |
643 | */ | |
644 | struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, | |
645 | int attrlen, int padattr) | |
646 | { | |
647 | if (nla_need_padding_for_64bit(skb)) | |
648 | nla_align_64bit(skb, padattr); | |
649 | ||
650 | return __nla_reserve(skb, attrtype, attrlen); | |
651 | } | |
652 | EXPORT_SYMBOL(__nla_reserve_64bit); | |
653 | ||
fe4944e5 TG |
654 | /** |
655 | * __nla_reserve_nohdr - reserve room for attribute without header | |
656 | * @skb: socket buffer to reserve room on | |
657 | * @attrlen: length of attribute payload | |
658 | * | |
659 | * Reserves room for attribute payload without a header. | |
660 | * | |
661 | * The caller is responsible to ensure that the skb provides enough | |
662 | * tailroom for the payload. | |
663 | */ | |
664 | void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen) | |
665 | { | |
b952f4df | 666 | return skb_put_zero(skb, NLA_ALIGN(attrlen)); |
fe4944e5 | 667 | } |
90800216 | 668 | EXPORT_SYMBOL(__nla_reserve_nohdr); |
fe4944e5 | 669 | |
bfa83a9e TG |
670 | /** |
671 | * nla_reserve - reserve room for attribute on the skb | |
672 | * @skb: socket buffer to reserve room on | |
673 | * @attrtype: attribute type | |
674 | * @attrlen: length of attribute payload | |
675 | * | |
676 | * Adds a netlink attribute header to a socket buffer and reserves | |
677 | * room for the payload but does not copy it. | |
678 | * | |
679 | * Returns NULL if the tailroom of the skb is insufficient to store | |
680 | * the attribute header and payload. | |
681 | */ | |
682 | struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) | |
683 | { | |
684 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | |
685 | return NULL; | |
686 | ||
687 | return __nla_reserve(skb, attrtype, attrlen); | |
688 | } | |
90800216 | 689 | EXPORT_SYMBOL(nla_reserve); |
bfa83a9e | 690 | |
089bf1a6 ND |
691 | /** |
692 | * nla_reserve_64bit - reserve room for attribute on the skb and align it | |
693 | * @skb: socket buffer to reserve room on | |
694 | * @attrtype: attribute type | |
695 | * @attrlen: length of attribute payload | |
11a99573 | 696 | * @padattr: attribute type for the padding |
089bf1a6 ND |
697 | * |
698 | * Adds a netlink attribute header to a socket buffer and reserves | |
699 | * room for the payload but does not copy it. It also ensure that this | |
11a99573 | 700 | * attribute will have a 64-bit aligned nla_data() area. |
089bf1a6 ND |
701 | * |
702 | * Returns NULL if the tailroom of the skb is insufficient to store | |
703 | * the attribute header and payload. | |
704 | */ | |
705 | struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
706 | int padattr) | |
707 | { | |
708 | size_t len; | |
709 | ||
710 | if (nla_need_padding_for_64bit(skb)) | |
711 | len = nla_total_size_64bit(attrlen); | |
712 | else | |
713 | len = nla_total_size(attrlen); | |
714 | if (unlikely(skb_tailroom(skb) < len)) | |
715 | return NULL; | |
716 | ||
717 | return __nla_reserve_64bit(skb, attrtype, attrlen, padattr); | |
718 | } | |
719 | EXPORT_SYMBOL(nla_reserve_64bit); | |
720 | ||
fe4944e5 | 721 | /** |
10b595af | 722 | * nla_reserve_nohdr - reserve room for attribute without header |
fe4944e5 | 723 | * @skb: socket buffer to reserve room on |
10b595af | 724 | * @attrlen: length of attribute payload |
fe4944e5 TG |
725 | * |
726 | * Reserves room for attribute payload without a header. | |
727 | * | |
728 | * Returns NULL if the tailroom of the skb is insufficient to store | |
729 | * the attribute payload. | |
730 | */ | |
731 | void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen) | |
732 | { | |
733 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
734 | return NULL; | |
735 | ||
736 | return __nla_reserve_nohdr(skb, attrlen); | |
737 | } | |
90800216 | 738 | EXPORT_SYMBOL(nla_reserve_nohdr); |
fe4944e5 | 739 | |
bfa83a9e TG |
740 | /** |
741 | * __nla_put - Add a netlink attribute to a socket buffer | |
742 | * @skb: socket buffer to add attribute to | |
743 | * @attrtype: attribute type | |
744 | * @attrlen: length of attribute payload | |
745 | * @data: head of attribute payload | |
746 | * | |
747 | * The caller is responsible to ensure that the skb provides enough | |
748 | * tailroom for the attribute header and payload. | |
749 | */ | |
750 | void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, | |
751 | const void *data) | |
752 | { | |
753 | struct nlattr *nla; | |
754 | ||
755 | nla = __nla_reserve(skb, attrtype, attrlen); | |
756 | memcpy(nla_data(nla), data, attrlen); | |
757 | } | |
90800216 | 758 | EXPORT_SYMBOL(__nla_put); |
bfa83a9e | 759 | |
089bf1a6 ND |
760 | /** |
761 | * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it | |
762 | * @skb: socket buffer to add attribute to | |
763 | * @attrtype: attribute type | |
764 | * @attrlen: length of attribute payload | |
765 | * @data: head of attribute payload | |
11a99573 | 766 | * @padattr: attribute type for the padding |
089bf1a6 ND |
767 | * |
768 | * The caller is responsible to ensure that the skb provides enough | |
769 | * tailroom for the attribute header and payload. | |
770 | */ | |
771 | void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
772 | const void *data, int padattr) | |
773 | { | |
774 | struct nlattr *nla; | |
775 | ||
776 | nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr); | |
777 | memcpy(nla_data(nla), data, attrlen); | |
778 | } | |
779 | EXPORT_SYMBOL(__nla_put_64bit); | |
780 | ||
fe4944e5 TG |
781 | /** |
782 | * __nla_put_nohdr - Add a netlink attribute without header | |
783 | * @skb: socket buffer to add attribute to | |
784 | * @attrlen: length of attribute payload | |
785 | * @data: head of attribute payload | |
786 | * | |
787 | * The caller is responsible to ensure that the skb provides enough | |
788 | * tailroom for the attribute payload. | |
789 | */ | |
790 | void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) | |
791 | { | |
792 | void *start; | |
793 | ||
794 | start = __nla_reserve_nohdr(skb, attrlen); | |
795 | memcpy(start, data, attrlen); | |
796 | } | |
90800216 | 797 | EXPORT_SYMBOL(__nla_put_nohdr); |
bfa83a9e TG |
798 | |
799 | /** | |
800 | * nla_put - Add a netlink attribute to a socket buffer | |
801 | * @skb: socket buffer to add attribute to | |
802 | * @attrtype: attribute type | |
803 | * @attrlen: length of attribute payload | |
804 | * @data: head of attribute payload | |
805 | * | |
bc3ed28c | 806 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
bfa83a9e TG |
807 | * the attribute header and payload. |
808 | */ | |
809 | int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data) | |
810 | { | |
811 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) | |
bc3ed28c | 812 | return -EMSGSIZE; |
bfa83a9e TG |
813 | |
814 | __nla_put(skb, attrtype, attrlen, data); | |
815 | return 0; | |
816 | } | |
90800216 | 817 | EXPORT_SYMBOL(nla_put); |
bfa83a9e | 818 | |
089bf1a6 ND |
819 | /** |
820 | * nla_put_64bit - Add a netlink attribute to a socket buffer and align it | |
821 | * @skb: socket buffer to add attribute to | |
822 | * @attrtype: attribute type | |
823 | * @attrlen: length of attribute payload | |
824 | * @data: head of attribute payload | |
11a99573 | 825 | * @padattr: attribute type for the padding |
089bf1a6 ND |
826 | * |
827 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store | |
828 | * the attribute header and payload. | |
829 | */ | |
830 | int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, | |
831 | const void *data, int padattr) | |
832 | { | |
833 | size_t len; | |
834 | ||
835 | if (nla_need_padding_for_64bit(skb)) | |
836 | len = nla_total_size_64bit(attrlen); | |
837 | else | |
838 | len = nla_total_size(attrlen); | |
839 | if (unlikely(skb_tailroom(skb) < len)) | |
840 | return -EMSGSIZE; | |
841 | ||
842 | __nla_put_64bit(skb, attrtype, attrlen, data, padattr); | |
843 | return 0; | |
844 | } | |
845 | EXPORT_SYMBOL(nla_put_64bit); | |
846 | ||
fe4944e5 TG |
847 | /** |
848 | * nla_put_nohdr - Add a netlink attribute without header | |
849 | * @skb: socket buffer to add attribute to | |
850 | * @attrlen: length of attribute payload | |
851 | * @data: head of attribute payload | |
852 | * | |
bc3ed28c | 853 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
fe4944e5 TG |
854 | * the attribute payload. |
855 | */ | |
856 | int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) | |
857 | { | |
858 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
bc3ed28c | 859 | return -EMSGSIZE; |
fe4944e5 TG |
860 | |
861 | __nla_put_nohdr(skb, attrlen, data); | |
862 | return 0; | |
863 | } | |
90800216 | 864 | EXPORT_SYMBOL(nla_put_nohdr); |
bfa83a9e | 865 | |
01480e1c PM |
866 | /** |
867 | * nla_append - Add a netlink attribute without header or padding | |
868 | * @skb: socket buffer to add attribute to | |
869 | * @attrlen: length of attribute payload | |
870 | * @data: head of attribute payload | |
871 | * | |
bc3ed28c | 872 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
01480e1c PM |
873 | * the attribute payload. |
874 | */ | |
875 | int nla_append(struct sk_buff *skb, int attrlen, const void *data) | |
876 | { | |
877 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) | |
bc3ed28c | 878 | return -EMSGSIZE; |
01480e1c | 879 | |
59ae1d12 | 880 | skb_put_data(skb, data, attrlen); |
01480e1c PM |
881 | return 0; |
882 | } | |
90800216 HX |
883 | EXPORT_SYMBOL(nla_append); |
884 | #endif |