Commit | Line | Data |
---|---|---|
10b518d4 MK |
1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | ||
3 | #include <linux/ethtool_netlink.h> | |
4 | #include <linux/bitmap.h> | |
5 | #include "netlink.h" | |
6 | #include "bitset.h" | |
7 | ||
8 | /* Some bitmaps are internally represented as an array of unsigned long, some | |
9 | * as an array of u32 (some even as single u32 for now). To avoid the need of | |
10 | * wrappers on caller side, we provide two set of functions: those with "32" | |
11 | * suffix in their names expect u32 based bitmaps, those without it expect | |
12 | * unsigned long bitmaps. | |
13 | */ | |
14 | ||
15 | static u32 ethnl_lower_bits(unsigned int n) | |
16 | { | |
17 | return ~(u32)0 >> (32 - n % 32); | |
18 | } | |
19 | ||
20 | static u32 ethnl_upper_bits(unsigned int n) | |
21 | { | |
22 | return ~(u32)0 << (n % 32); | |
23 | } | |
24 | ||
25 | /** | |
26 | * ethnl_bitmap32_clear() - Clear u32 based bitmap | |
27 | * @dst: bitmap to clear | |
28 | * @start: beginning of the interval | |
29 | * @end: end of the interval | |
30 | * @mod: set if bitmap was modified | |
31 | * | |
32 | * Clear @nbits bits of a bitmap with indices @start <= i < @end | |
33 | */ | |
34 | static void ethnl_bitmap32_clear(u32 *dst, unsigned int start, unsigned int end, | |
35 | bool *mod) | |
36 | { | |
37 | unsigned int start_word = start / 32; | |
38 | unsigned int end_word = end / 32; | |
39 | unsigned int i; | |
40 | u32 mask; | |
41 | ||
42 | if (end <= start) | |
43 | return; | |
44 | ||
45 | if (start % 32) { | |
46 | mask = ethnl_upper_bits(start); | |
47 | if (end_word == start_word) { | |
48 | mask &= ethnl_lower_bits(end); | |
49 | if (dst[start_word] & mask) { | |
50 | dst[start_word] &= ~mask; | |
51 | *mod = true; | |
52 | } | |
53 | return; | |
54 | } | |
55 | if (dst[start_word] & mask) { | |
56 | dst[start_word] &= ~mask; | |
57 | *mod = true; | |
58 | } | |
59 | start_word++; | |
60 | } | |
61 | ||
62 | for (i = start_word; i < end_word; i++) { | |
63 | if (dst[i]) { | |
64 | dst[i] = 0; | |
65 | *mod = true; | |
66 | } | |
67 | } | |
68 | if (end % 32) { | |
69 | mask = ethnl_lower_bits(end); | |
70 | if (dst[end_word] & mask) { | |
71 | dst[end_word] &= ~mask; | |
72 | *mod = true; | |
73 | } | |
74 | } | |
75 | } | |
76 | ||
77 | /** | |
78 | * ethnl_bitmap32_not_zero() - Check if any bit is set in an interval | |
79 | * @map: bitmap to test | |
80 | * @start: beginning of the interval | |
81 | * @end: end of the interval | |
82 | * | |
83 | * Return: true if there is non-zero bit with index @start <= i < @end, | |
84 | * false if the whole interval is zero | |
85 | */ | |
86 | static bool ethnl_bitmap32_not_zero(const u32 *map, unsigned int start, | |
87 | unsigned int end) | |
88 | { | |
89 | unsigned int start_word = start / 32; | |
90 | unsigned int end_word = end / 32; | |
91 | u32 mask; | |
92 | ||
93 | if (end <= start) | |
94 | return true; | |
95 | ||
96 | if (start % 32) { | |
97 | mask = ethnl_upper_bits(start); | |
98 | if (end_word == start_word) { | |
99 | mask &= ethnl_lower_bits(end); | |
100 | return map[start_word] & mask; | |
101 | } | |
102 | if (map[start_word] & mask) | |
103 | return true; | |
104 | start_word++; | |
105 | } | |
106 | ||
107 | if (!memchr_inv(map + start_word, '\0', | |
108 | (end_word - start_word) * sizeof(u32))) | |
109 | return true; | |
110 | if (end % 32 == 0) | |
111 | return true; | |
112 | return map[end_word] & ethnl_lower_bits(end); | |
113 | } | |
114 | ||
115 | /** | |
116 | * ethnl_bitmap32_update() - Modify u32 based bitmap according to value/mask | |
117 | * pair | |
118 | * @dst: bitmap to update | |
119 | * @nbits: bit size of the bitmap | |
120 | * @value: values to set | |
121 | * @mask: mask of bits to set | |
122 | * @mod: set to true if bitmap is modified, preserve if not | |
123 | * | |
124 | * Set bits in @dst bitmap which are set in @mask to values from @value, leave | |
125 | * the rest untouched. If destination bitmap was modified, set @mod to true, | |
126 | * leave as it is if not. | |
127 | */ | |
128 | static void ethnl_bitmap32_update(u32 *dst, unsigned int nbits, | |
129 | const u32 *value, const u32 *mask, bool *mod) | |
130 | { | |
131 | while (nbits > 0) { | |
132 | u32 real_mask = mask ? *mask : ~(u32)0; | |
133 | u32 new_value; | |
134 | ||
135 | if (nbits < 32) | |
136 | real_mask &= ethnl_lower_bits(nbits); | |
137 | new_value = (*dst & ~real_mask) | (*value & real_mask); | |
138 | if (new_value != *dst) { | |
139 | *dst = new_value; | |
140 | *mod = true; | |
141 | } | |
142 | ||
143 | if (nbits <= 32) | |
144 | break; | |
145 | dst++; | |
146 | nbits -= 32; | |
147 | value++; | |
148 | if (mask) | |
149 | mask++; | |
150 | } | |
151 | } | |
152 | ||
153 | static bool ethnl_bitmap32_test_bit(const u32 *map, unsigned int index) | |
154 | { | |
155 | return map[index / 32] & (1U << (index % 32)); | |
156 | } | |
157 | ||
158 | /** | |
159 | * ethnl_bitset32_size() - Calculate size of bitset nested attribute | |
160 | * @val: value bitmap (u32 based) | |
161 | * @mask: mask bitmap (u32 based, optional) | |
162 | * @nbits: bit length of the bitset | |
163 | * @names: array of bit names (optional) | |
164 | * @compact: assume compact format for output | |
165 | * | |
166 | * Estimate length of netlink attribute composed by a later call to | |
167 | * ethnl_put_bitset32() call with the same arguments. | |
168 | * | |
169 | * Return: negative error code or attribute length estimate | |
170 | */ | |
171 | int ethnl_bitset32_size(const u32 *val, const u32 *mask, unsigned int nbits, | |
172 | ethnl_string_array_t names, bool compact) | |
173 | { | |
174 | unsigned int len = 0; | |
175 | ||
176 | /* list flag */ | |
177 | if (!mask) | |
178 | len += nla_total_size(sizeof(u32)); | |
179 | /* size */ | |
180 | len += nla_total_size(sizeof(u32)); | |
181 | ||
182 | if (compact) { | |
183 | unsigned int nwords = DIV_ROUND_UP(nbits, 32); | |
184 | ||
185 | /* value, mask */ | |
186 | len += (mask ? 2 : 1) * nla_total_size(nwords * sizeof(u32)); | |
187 | } else { | |
188 | unsigned int bits_len = 0; | |
189 | unsigned int bit_len, i; | |
190 | ||
191 | for (i = 0; i < nbits; i++) { | |
192 | const char *name = names ? names[i] : NULL; | |
193 | ||
194 | if (!ethnl_bitmap32_test_bit(mask ?: val, i)) | |
195 | continue; | |
196 | /* index */ | |
197 | bit_len = nla_total_size(sizeof(u32)); | |
198 | /* name */ | |
199 | if (name) | |
200 | bit_len += ethnl_strz_size(name); | |
201 | /* value */ | |
202 | if (mask && ethnl_bitmap32_test_bit(val, i)) | |
203 | bit_len += nla_total_size(0); | |
204 | ||
205 | /* bit nest */ | |
206 | bits_len += nla_total_size(bit_len); | |
207 | } | |
208 | /* bits nest */ | |
209 | len += nla_total_size(bits_len); | |
210 | } | |
211 | ||
212 | /* outermost nest */ | |
213 | return nla_total_size(len); | |
214 | } | |
215 | ||
216 | /** | |
217 | * ethnl_put_bitset32() - Put a bitset nest into a message | |
218 | * @skb: skb with the message | |
219 | * @attrtype: attribute type for the bitset nest | |
220 | * @val: value bitmap (u32 based) | |
221 | * @mask: mask bitmap (u32 based, optional) | |
222 | * @nbits: bit length of the bitset | |
223 | * @names: array of bit names (optional) | |
224 | * @compact: use compact format for the output | |
225 | * | |
226 | * Compose a nested attribute representing a bitset. If @mask is null, simple | |
227 | * bitmap (bit list) is created, if @mask is provided, represent a value/mask | |
228 | * pair. Bit names are only used in verbose mode and when provided by calller. | |
229 | * | |
230 | * Return: 0 on success, negative error value on error | |
231 | */ | |
232 | int ethnl_put_bitset32(struct sk_buff *skb, int attrtype, const u32 *val, | |
233 | const u32 *mask, unsigned int nbits, | |
234 | ethnl_string_array_t names, bool compact) | |
235 | { | |
236 | struct nlattr *nest; | |
237 | struct nlattr *attr; | |
238 | ||
239 | nest = nla_nest_start(skb, attrtype); | |
240 | if (!nest) | |
241 | return -EMSGSIZE; | |
242 | ||
243 | if (!mask && nla_put_flag(skb, ETHTOOL_A_BITSET_NOMASK)) | |
244 | goto nla_put_failure; | |
245 | if (nla_put_u32(skb, ETHTOOL_A_BITSET_SIZE, nbits)) | |
246 | goto nla_put_failure; | |
247 | if (compact) { | |
248 | unsigned int nwords = DIV_ROUND_UP(nbits, 32); | |
249 | unsigned int nbytes = nwords * sizeof(u32); | |
250 | u32 *dst; | |
251 | ||
252 | attr = nla_reserve(skb, ETHTOOL_A_BITSET_VALUE, nbytes); | |
253 | if (!attr) | |
254 | goto nla_put_failure; | |
255 | dst = nla_data(attr); | |
256 | memcpy(dst, val, nbytes); | |
257 | if (nbits % 32) | |
258 | dst[nwords - 1] &= ethnl_lower_bits(nbits); | |
259 | ||
260 | if (mask) { | |
261 | attr = nla_reserve(skb, ETHTOOL_A_BITSET_MASK, nbytes); | |
262 | if (!attr) | |
263 | goto nla_put_failure; | |
264 | dst = nla_data(attr); | |
265 | memcpy(dst, mask, nbytes); | |
266 | if (nbits % 32) | |
267 | dst[nwords - 1] &= ethnl_lower_bits(nbits); | |
268 | } | |
269 | } else { | |
270 | struct nlattr *bits; | |
271 | unsigned int i; | |
272 | ||
273 | bits = nla_nest_start(skb, ETHTOOL_A_BITSET_BITS); | |
274 | if (!bits) | |
275 | goto nla_put_failure; | |
276 | for (i = 0; i < nbits; i++) { | |
277 | const char *name = names ? names[i] : NULL; | |
278 | ||
279 | if (!ethnl_bitmap32_test_bit(mask ?: val, i)) | |
280 | continue; | |
281 | attr = nla_nest_start(skb, ETHTOOL_A_BITSET_BITS_BIT); | |
282 | if (!attr) | |
283 | goto nla_put_failure; | |
284 | if (nla_put_u32(skb, ETHTOOL_A_BITSET_BIT_INDEX, i)) | |
285 | goto nla_put_failure; | |
286 | if (name && | |
287 | ethnl_put_strz(skb, ETHTOOL_A_BITSET_BIT_NAME, name)) | |
288 | goto nla_put_failure; | |
289 | if (mask && ethnl_bitmap32_test_bit(val, i) && | |
290 | nla_put_flag(skb, ETHTOOL_A_BITSET_BIT_VALUE)) | |
291 | goto nla_put_failure; | |
292 | nla_nest_end(skb, attr); | |
293 | } | |
294 | nla_nest_end(skb, bits); | |
295 | } | |
296 | ||
297 | nla_nest_end(skb, nest); | |
298 | return 0; | |
299 | ||
300 | nla_put_failure: | |
301 | nla_nest_cancel(skb, nest); | |
302 | return -EMSGSIZE; | |
303 | } | |
304 | ||
305 | static const struct nla_policy bitset_policy[ETHTOOL_A_BITSET_MAX + 1] = { | |
306 | [ETHTOOL_A_BITSET_UNSPEC] = { .type = NLA_REJECT }, | |
307 | [ETHTOOL_A_BITSET_NOMASK] = { .type = NLA_FLAG }, | |
308 | [ETHTOOL_A_BITSET_SIZE] = { .type = NLA_U32 }, | |
309 | [ETHTOOL_A_BITSET_BITS] = { .type = NLA_NESTED }, | |
310 | [ETHTOOL_A_BITSET_VALUE] = { .type = NLA_BINARY }, | |
311 | [ETHTOOL_A_BITSET_MASK] = { .type = NLA_BINARY }, | |
312 | }; | |
313 | ||
314 | static const struct nla_policy bit_policy[ETHTOOL_A_BITSET_BIT_MAX + 1] = { | |
315 | [ETHTOOL_A_BITSET_BIT_UNSPEC] = { .type = NLA_REJECT }, | |
316 | [ETHTOOL_A_BITSET_BIT_INDEX] = { .type = NLA_U32 }, | |
317 | [ETHTOOL_A_BITSET_BIT_NAME] = { .type = NLA_NUL_STRING }, | |
318 | [ETHTOOL_A_BITSET_BIT_VALUE] = { .type = NLA_FLAG }, | |
319 | }; | |
320 | ||
321 | /** | |
322 | * ethnl_bitset_is_compact() - check if bitset attribute represents a compact | |
323 | * bitset | |
324 | * @bitset: nested attribute representing a bitset | |
325 | * @compact: pointer for return value | |
326 | * | |
327 | * Return: 0 on success, negative error code on failure | |
328 | */ | |
329 | int ethnl_bitset_is_compact(const struct nlattr *bitset, bool *compact) | |
330 | { | |
331 | struct nlattr *tb[ETHTOOL_A_BITSET_MAX + 1]; | |
332 | int ret; | |
333 | ||
334 | ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_MAX, bitset, | |
335 | bitset_policy, NULL); | |
336 | if (ret < 0) | |
337 | return ret; | |
338 | ||
339 | if (tb[ETHTOOL_A_BITSET_BITS]) { | |
340 | if (tb[ETHTOOL_A_BITSET_VALUE] || tb[ETHTOOL_A_BITSET_MASK]) | |
341 | return -EINVAL; | |
342 | *compact = false; | |
343 | return 0; | |
344 | } | |
345 | if (!tb[ETHTOOL_A_BITSET_SIZE] || !tb[ETHTOOL_A_BITSET_VALUE]) | |
346 | return -EINVAL; | |
347 | ||
348 | *compact = true; | |
349 | return 0; | |
350 | } | |
351 | ||
352 | /** | |
353 | * ethnl_name_to_idx() - look up string index for a name | |
354 | * @names: array of ETH_GSTRING_LEN sized strings | |
355 | * @n_names: number of strings in the array | |
356 | * @name: name to look up | |
357 | * | |
358 | * Return: index of the string if found, -ENOENT if not found | |
359 | */ | |
360 | static int ethnl_name_to_idx(ethnl_string_array_t names, unsigned int n_names, | |
361 | const char *name) | |
362 | { | |
363 | unsigned int i; | |
364 | ||
365 | if (!names) | |
366 | return -ENOENT; | |
367 | ||
368 | for (i = 0; i < n_names; i++) { | |
369 | /* names[i] may not be null terminated */ | |
370 | if (!strncmp(names[i], name, ETH_GSTRING_LEN) && | |
371 | strlen(name) <= ETH_GSTRING_LEN) | |
372 | return i; | |
373 | } | |
374 | ||
375 | return -ENOENT; | |
376 | } | |
377 | ||
378 | static int ethnl_parse_bit(unsigned int *index, bool *val, unsigned int nbits, | |
379 | const struct nlattr *bit_attr, bool no_mask, | |
380 | ethnl_string_array_t names, | |
381 | struct netlink_ext_ack *extack) | |
382 | { | |
383 | struct nlattr *tb[ETHTOOL_A_BITSET_BIT_MAX + 1]; | |
384 | int ret, idx; | |
385 | ||
386 | ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_BIT_MAX, bit_attr, | |
387 | bit_policy, extack); | |
388 | if (ret < 0) | |
389 | return ret; | |
390 | ||
391 | if (tb[ETHTOOL_A_BITSET_BIT_INDEX]) { | |
392 | const char *name; | |
393 | ||
394 | idx = nla_get_u32(tb[ETHTOOL_A_BITSET_BIT_INDEX]); | |
395 | if (idx >= nbits) { | |
396 | NL_SET_ERR_MSG_ATTR(extack, | |
397 | tb[ETHTOOL_A_BITSET_BIT_INDEX], | |
398 | "bit index too high"); | |
399 | return -EOPNOTSUPP; | |
400 | } | |
401 | name = names ? names[idx] : NULL; | |
402 | if (tb[ETHTOOL_A_BITSET_BIT_NAME] && name && | |
403 | strncmp(nla_data(tb[ETHTOOL_A_BITSET_BIT_NAME]), name, | |
404 | nla_len(tb[ETHTOOL_A_BITSET_BIT_NAME]))) { | |
405 | NL_SET_ERR_MSG_ATTR(extack, bit_attr, | |
406 | "bit index and name mismatch"); | |
407 | return -EINVAL; | |
408 | } | |
409 | } else if (tb[ETHTOOL_A_BITSET_BIT_NAME]) { | |
410 | idx = ethnl_name_to_idx(names, nbits, | |
411 | nla_data(tb[ETHTOOL_A_BITSET_BIT_NAME])); | |
412 | if (idx < 0) { | |
413 | NL_SET_ERR_MSG_ATTR(extack, | |
414 | tb[ETHTOOL_A_BITSET_BIT_NAME], | |
415 | "bit name not found"); | |
416 | return -EOPNOTSUPP; | |
417 | } | |
418 | } else { | |
419 | NL_SET_ERR_MSG_ATTR(extack, bit_attr, | |
420 | "neither bit index nor name specified"); | |
421 | return -EINVAL; | |
422 | } | |
423 | ||
424 | *index = idx; | |
425 | *val = no_mask || tb[ETHTOOL_A_BITSET_BIT_VALUE]; | |
426 | return 0; | |
427 | } | |
428 | ||
429 | static int | |
430 | ethnl_update_bitset32_verbose(u32 *bitmap, unsigned int nbits, | |
431 | const struct nlattr *attr, struct nlattr **tb, | |
432 | ethnl_string_array_t names, | |
433 | struct netlink_ext_ack *extack, bool *mod) | |
434 | { | |
435 | struct nlattr *bit_attr; | |
436 | bool no_mask; | |
437 | int rem; | |
438 | int ret; | |
439 | ||
440 | if (tb[ETHTOOL_A_BITSET_VALUE]) { | |
441 | NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE], | |
442 | "value only allowed in compact bitset"); | |
443 | return -EINVAL; | |
444 | } | |
445 | if (tb[ETHTOOL_A_BITSET_MASK]) { | |
446 | NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK], | |
447 | "mask only allowed in compact bitset"); | |
448 | return -EINVAL; | |
449 | } | |
450 | no_mask = tb[ETHTOOL_A_BITSET_NOMASK]; | |
451 | ||
452 | nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) { | |
453 | bool old_val, new_val; | |
454 | unsigned int idx; | |
455 | ||
456 | if (nla_type(bit_attr) != ETHTOOL_A_BITSET_BITS_BIT) { | |
457 | NL_SET_ERR_MSG_ATTR(extack, bit_attr, | |
458 | "only ETHTOOL_A_BITSET_BITS_BIT allowed in ETHTOOL_A_BITSET_BITS"); | |
459 | return -EINVAL; | |
460 | } | |
461 | ret = ethnl_parse_bit(&idx, &new_val, nbits, bit_attr, no_mask, | |
462 | names, extack); | |
463 | if (ret < 0) | |
464 | return ret; | |
465 | old_val = bitmap[idx / 32] & ((u32)1 << (idx % 32)); | |
466 | if (new_val != old_val) { | |
467 | if (new_val) | |
468 | bitmap[idx / 32] |= ((u32)1 << (idx % 32)); | |
469 | else | |
470 | bitmap[idx / 32] &= ~((u32)1 << (idx % 32)); | |
471 | *mod = true; | |
472 | } | |
473 | } | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
478 | static int ethnl_compact_sanity_checks(unsigned int nbits, | |
479 | const struct nlattr *nest, | |
480 | struct nlattr **tb, | |
481 | struct netlink_ext_ack *extack) | |
482 | { | |
483 | bool no_mask = tb[ETHTOOL_A_BITSET_NOMASK]; | |
484 | unsigned int attr_nbits, attr_nwords; | |
485 | const struct nlattr *test_attr; | |
486 | ||
487 | if (no_mask && tb[ETHTOOL_A_BITSET_MASK]) { | |
488 | NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK], | |
489 | "mask not allowed in list bitset"); | |
490 | return -EINVAL; | |
491 | } | |
492 | if (!tb[ETHTOOL_A_BITSET_SIZE]) { | |
493 | NL_SET_ERR_MSG_ATTR(extack, nest, | |
494 | "missing size in compact bitset"); | |
495 | return -EINVAL; | |
496 | } | |
497 | if (!tb[ETHTOOL_A_BITSET_VALUE]) { | |
498 | NL_SET_ERR_MSG_ATTR(extack, nest, | |
499 | "missing value in compact bitset"); | |
500 | return -EINVAL; | |
501 | } | |
502 | if (!no_mask && !tb[ETHTOOL_A_BITSET_MASK]) { | |
503 | NL_SET_ERR_MSG_ATTR(extack, nest, | |
504 | "missing mask in compact nonlist bitset"); | |
505 | return -EINVAL; | |
506 | } | |
507 | ||
508 | attr_nbits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]); | |
509 | attr_nwords = DIV_ROUND_UP(attr_nbits, 32); | |
510 | if (nla_len(tb[ETHTOOL_A_BITSET_VALUE]) != attr_nwords * sizeof(u32)) { | |
511 | NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE], | |
512 | "bitset value length does not match size"); | |
513 | return -EINVAL; | |
514 | } | |
515 | if (tb[ETHTOOL_A_BITSET_MASK] && | |
516 | nla_len(tb[ETHTOOL_A_BITSET_MASK]) != attr_nwords * sizeof(u32)) { | |
517 | NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK], | |
518 | "bitset mask length does not match size"); | |
519 | return -EINVAL; | |
520 | } | |
521 | if (attr_nbits <= nbits) | |
522 | return 0; | |
523 | ||
524 | test_attr = no_mask ? tb[ETHTOOL_A_BITSET_VALUE] : | |
525 | tb[ETHTOOL_A_BITSET_MASK]; | |
526 | if (ethnl_bitmap32_not_zero(nla_data(test_attr), nbits, attr_nbits)) { | |
527 | NL_SET_ERR_MSG_ATTR(extack, test_attr, | |
528 | "cannot modify bits past kernel bitset size"); | |
529 | return -EINVAL; | |
530 | } | |
531 | return 0; | |
532 | } | |
533 | ||
534 | /** | |
535 | * ethnl_update_bitset32() - Apply a bitset nest to a u32 based bitmap | |
536 | * @bitmap: bitmap to update | |
537 | * @nbits: size of the updated bitmap in bits | |
538 | * @attr: nest attribute to parse and apply | |
539 | * @names: array of bit names; may be null for compact format | |
540 | * @extack: extack for error reporting | |
541 | * @mod: set this to true if bitmap is modified, leave as it is if not | |
542 | * | |
543 | * Apply bitset netsted attribute to a bitmap. If the attribute represents | |
544 | * a bit list, @bitmap is set to its contents; otherwise, bits in mask are | |
545 | * set to values from value. Bitmaps in the attribute may be longer than | |
546 | * @nbits but the message must not request modifying any bits past @nbits. | |
547 | * | |
548 | * Return: negative error code on failure, 0 on success | |
549 | */ | |
550 | int ethnl_update_bitset32(u32 *bitmap, unsigned int nbits, | |
551 | const struct nlattr *attr, ethnl_string_array_t names, | |
552 | struct netlink_ext_ack *extack, bool *mod) | |
553 | { | |
554 | struct nlattr *tb[ETHTOOL_A_BITSET_MAX + 1]; | |
555 | unsigned int change_bits; | |
556 | bool no_mask; | |
557 | int ret; | |
558 | ||
559 | if (!attr) | |
560 | return 0; | |
561 | ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_MAX, attr, bitset_policy, | |
562 | extack); | |
563 | if (ret < 0) | |
564 | return ret; | |
565 | ||
566 | if (tb[ETHTOOL_A_BITSET_BITS]) | |
567 | return ethnl_update_bitset32_verbose(bitmap, nbits, attr, tb, | |
568 | names, extack, mod); | |
569 | ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack); | |
570 | if (ret < 0) | |
571 | return ret; | |
572 | ||
573 | no_mask = tb[ETHTOOL_A_BITSET_NOMASK]; | |
574 | change_bits = min_t(unsigned int, | |
575 | nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]), nbits); | |
576 | ethnl_bitmap32_update(bitmap, change_bits, | |
577 | nla_data(tb[ETHTOOL_A_BITSET_VALUE]), | |
578 | no_mask ? NULL : | |
579 | nla_data(tb[ETHTOOL_A_BITSET_MASK]), | |
580 | mod); | |
581 | if (no_mask && change_bits < nbits) | |
582 | ethnl_bitmap32_clear(bitmap, change_bits, nbits, mod); | |
583 | ||
584 | return 0; | |
585 | } | |
586 | ||
587 | #if BITS_PER_LONG == 64 && defined(__BIG_ENDIAN) | |
588 | ||
589 | /* 64-bit big endian architectures are the only case when u32 based bitmaps | |
590 | * and unsigned long based bitmaps have different memory layout so that we | |
591 | * cannot simply cast the latter to the former and need actual wrappers | |
592 | * converting the latter to the former. | |
593 | * | |
594 | * To reduce the number of slab allocations, the wrappers use fixed size local | |
595 | * variables for bitmaps up to ETHNL_SMALL_BITMAP_BITS bits which is the | |
596 | * majority of bitmaps used by ethtool. | |
597 | */ | |
598 | #define ETHNL_SMALL_BITMAP_BITS 128 | |
599 | #define ETHNL_SMALL_BITMAP_WORDS DIV_ROUND_UP(ETHNL_SMALL_BITMAP_BITS, 32) | |
600 | ||
601 | int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask, | |
602 | unsigned int nbits, ethnl_string_array_t names, | |
603 | bool compact) | |
604 | { | |
605 | u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS]; | |
606 | u32 small_val32[ETHNL_SMALL_BITMAP_WORDS]; | |
607 | u32 *mask32; | |
608 | u32 *val32; | |
609 | int ret; | |
610 | ||
611 | if (nbits > ETHNL_SMALL_BITMAP_BITS) { | |
612 | unsigned int nwords = DIV_ROUND_UP(nbits, 32); | |
613 | ||
614 | val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL); | |
615 | if (!val32) | |
616 | return -ENOMEM; | |
617 | mask32 = val32 + nwords; | |
618 | } else { | |
619 | val32 = small_val32; | |
620 | mask32 = small_mask32; | |
621 | } | |
622 | ||
623 | bitmap_to_arr32(val32, val, nbits); | |
624 | if (mask) | |
625 | bitmap_to_arr32(mask32, mask, nbits); | |
626 | else | |
627 | mask32 = NULL; | |
628 | ret = ethnl_bitset32_size(val32, mask32, nbits, names, compact); | |
629 | ||
630 | if (nbits > ETHNL_SMALL_BITMAP_BITS) | |
631 | kfree(val32); | |
632 | ||
633 | return ret; | |
634 | } | |
635 | ||
636 | int ethnl_put_bitset(struct sk_buff *skb, int attrtype, | |
637 | const unsigned long *val, const unsigned long *mask, | |
638 | unsigned int nbits, ethnl_string_array_t names, | |
639 | bool compact) | |
640 | { | |
641 | u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS]; | |
642 | u32 small_val32[ETHNL_SMALL_BITMAP_WORDS]; | |
643 | u32 *mask32; | |
644 | u32 *val32; | |
645 | int ret; | |
646 | ||
647 | if (nbits > ETHNL_SMALL_BITMAP_BITS) { | |
648 | unsigned int nwords = DIV_ROUND_UP(nbits, 32); | |
649 | ||
650 | val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL); | |
651 | if (!val32) | |
652 | return -ENOMEM; | |
653 | mask32 = val32 + nwords; | |
654 | } else { | |
655 | val32 = small_val32; | |
656 | mask32 = small_mask32; | |
657 | } | |
658 | ||
659 | bitmap_to_arr32(val32, val, nbits); | |
660 | if (mask) | |
661 | bitmap_to_arr32(mask32, mask, nbits); | |
662 | else | |
663 | mask32 = NULL; | |
664 | ret = ethnl_put_bitset32(skb, attrtype, val32, mask32, nbits, names, | |
665 | compact); | |
666 | ||
667 | if (nbits > ETHNL_SMALL_BITMAP_BITS) | |
668 | kfree(val32); | |
669 | ||
670 | return ret; | |
671 | } | |
672 | ||
673 | int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits, | |
674 | const struct nlattr *attr, ethnl_string_array_t names, | |
675 | struct netlink_ext_ack *extack, bool *mod) | |
676 | { | |
677 | u32 small_bitmap32[ETHNL_SMALL_BITMAP_WORDS]; | |
678 | u32 *bitmap32 = small_bitmap32; | |
679 | bool u32_mod = false; | |
680 | int ret; | |
681 | ||
682 | if (nbits > ETHNL_SMALL_BITMAP_BITS) { | |
683 | unsigned int dst_words = DIV_ROUND_UP(nbits, 32); | |
684 | ||
685 | bitmap32 = kmalloc_array(dst_words, sizeof(u32), GFP_KERNEL); | |
686 | if (!bitmap32) | |
687 | return -ENOMEM; | |
688 | } | |
689 | ||
690 | bitmap_to_arr32(bitmap32, bitmap, nbits); | |
691 | ret = ethnl_update_bitset32(bitmap32, nbits, attr, names, extack, | |
692 | &u32_mod); | |
693 | if (u32_mod) { | |
694 | bitmap_from_arr32(bitmap, bitmap32, nbits); | |
695 | *mod = true; | |
696 | } | |
697 | ||
698 | if (nbits > ETHNL_SMALL_BITMAP_BITS) | |
699 | kfree(bitmap32); | |
700 | ||
701 | return ret; | |
702 | } | |
703 | ||
704 | #else | |
705 | ||
706 | /* On little endian 64-bit and all 32-bit architectures, an unsigned long | |
707 | * based bitmap can be interpreted as u32 based one using a simple cast. | |
708 | */ | |
709 | ||
710 | int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask, | |
711 | unsigned int nbits, ethnl_string_array_t names, | |
712 | bool compact) | |
713 | { | |
714 | return ethnl_bitset32_size((const u32 *)val, (const u32 *)mask, nbits, | |
715 | names, compact); | |
716 | } | |
717 | ||
718 | int ethnl_put_bitset(struct sk_buff *skb, int attrtype, | |
719 | const unsigned long *val, const unsigned long *mask, | |
720 | unsigned int nbits, ethnl_string_array_t names, | |
721 | bool compact) | |
722 | { | |
723 | return ethnl_put_bitset32(skb, attrtype, (const u32 *)val, | |
724 | (const u32 *)mask, nbits, names, compact); | |
725 | } | |
726 | ||
727 | int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits, | |
728 | const struct nlattr *attr, ethnl_string_array_t names, | |
729 | struct netlink_ext_ack *extack, bool *mod) | |
730 | { | |
731 | return ethnl_update_bitset32((u32 *)bitmap, nbits, attr, names, extack, | |
732 | mod); | |
733 | } | |
734 | ||
735 | #endif /* BITS_PER_LONG == 64 && defined(__BIG_ENDIAN) */ |