110b518d4SMichal Kubecek // SPDX-License-Identifier: GPL-2.0-only
210b518d4SMichal Kubecek
310b518d4SMichal Kubecek #include <linux/ethtool_netlink.h>
410b518d4SMichal Kubecek #include <linux/bitmap.h>
510b518d4SMichal Kubecek #include "netlink.h"
610b518d4SMichal Kubecek #include "bitset.h"
710b518d4SMichal Kubecek
810b518d4SMichal Kubecek /* Some bitmaps are internally represented as an array of unsigned long, some
910b518d4SMichal Kubecek * as an array of u32 (some even as single u32 for now). To avoid the need of
1010b518d4SMichal Kubecek * wrappers on caller side, we provide two set of functions: those with "32"
1110b518d4SMichal Kubecek * suffix in their names expect u32 based bitmaps, those without it expect
1210b518d4SMichal Kubecek * unsigned long bitmaps.
1310b518d4SMichal Kubecek */
1410b518d4SMichal Kubecek
ethnl_lower_bits(unsigned int n)1510b518d4SMichal Kubecek static u32 ethnl_lower_bits(unsigned int n)
1610b518d4SMichal Kubecek {
1710b518d4SMichal Kubecek return ~(u32)0 >> (32 - n % 32);
1810b518d4SMichal Kubecek }
1910b518d4SMichal Kubecek
ethnl_upper_bits(unsigned int n)2010b518d4SMichal Kubecek static u32 ethnl_upper_bits(unsigned int n)
2110b518d4SMichal Kubecek {
2210b518d4SMichal Kubecek return ~(u32)0 << (n % 32);
2310b518d4SMichal Kubecek }
2410b518d4SMichal Kubecek
2510b518d4SMichal Kubecek /**
2610b518d4SMichal Kubecek * ethnl_bitmap32_clear() - Clear u32 based bitmap
2710b518d4SMichal Kubecek * @dst: bitmap to clear
2810b518d4SMichal Kubecek * @start: beginning of the interval
2910b518d4SMichal Kubecek * @end: end of the interval
3010b518d4SMichal Kubecek * @mod: set if bitmap was modified
3110b518d4SMichal Kubecek *
3210b518d4SMichal Kubecek * Clear @nbits bits of a bitmap with indices @start <= i < @end
3310b518d4SMichal Kubecek */
ethnl_bitmap32_clear(u32 * dst,unsigned int start,unsigned int end,bool * mod)3410b518d4SMichal Kubecek static void ethnl_bitmap32_clear(u32 *dst, unsigned int start, unsigned int end,
3510b518d4SMichal Kubecek bool *mod)
3610b518d4SMichal Kubecek {
3710b518d4SMichal Kubecek unsigned int start_word = start / 32;
3810b518d4SMichal Kubecek unsigned int end_word = end / 32;
3910b518d4SMichal Kubecek unsigned int i;
4010b518d4SMichal Kubecek u32 mask;
4110b518d4SMichal Kubecek
4210b518d4SMichal Kubecek if (end <= start)
4310b518d4SMichal Kubecek return;
4410b518d4SMichal Kubecek
4510b518d4SMichal Kubecek if (start % 32) {
4610b518d4SMichal Kubecek mask = ethnl_upper_bits(start);
4710b518d4SMichal Kubecek if (end_word == start_word) {
4810b518d4SMichal Kubecek mask &= ethnl_lower_bits(end);
4910b518d4SMichal Kubecek if (dst[start_word] & mask) {
5010b518d4SMichal Kubecek dst[start_word] &= ~mask;
5110b518d4SMichal Kubecek *mod = true;
5210b518d4SMichal Kubecek }
5310b518d4SMichal Kubecek return;
5410b518d4SMichal Kubecek }
5510b518d4SMichal Kubecek if (dst[start_word] & mask) {
5610b518d4SMichal Kubecek dst[start_word] &= ~mask;
5710b518d4SMichal Kubecek *mod = true;
5810b518d4SMichal Kubecek }
5910b518d4SMichal Kubecek start_word++;
6010b518d4SMichal Kubecek }
6110b518d4SMichal Kubecek
6210b518d4SMichal Kubecek for (i = start_word; i < end_word; i++) {
6310b518d4SMichal Kubecek if (dst[i]) {
6410b518d4SMichal Kubecek dst[i] = 0;
6510b518d4SMichal Kubecek *mod = true;
6610b518d4SMichal Kubecek }
6710b518d4SMichal Kubecek }
6810b518d4SMichal Kubecek if (end % 32) {
6910b518d4SMichal Kubecek mask = ethnl_lower_bits(end);
7010b518d4SMichal Kubecek if (dst[end_word] & mask) {
7110b518d4SMichal Kubecek dst[end_word] &= ~mask;
7210b518d4SMichal Kubecek *mod = true;
7310b518d4SMichal Kubecek }
7410b518d4SMichal Kubecek }
7510b518d4SMichal Kubecek }
7610b518d4SMichal Kubecek
7710b518d4SMichal Kubecek /**
7810b518d4SMichal Kubecek * ethnl_bitmap32_not_zero() - Check if any bit is set in an interval
7910b518d4SMichal Kubecek * @map: bitmap to test
8010b518d4SMichal Kubecek * @start: beginning of the interval
8110b518d4SMichal Kubecek * @end: end of the interval
8210b518d4SMichal Kubecek *
8310b518d4SMichal Kubecek * Return: true if there is non-zero bit with index @start <= i < @end,
8410b518d4SMichal Kubecek * false if the whole interval is zero
8510b518d4SMichal Kubecek */
ethnl_bitmap32_not_zero(const u32 * map,unsigned int start,unsigned int end)8610b518d4SMichal Kubecek static bool ethnl_bitmap32_not_zero(const u32 *map, unsigned int start,
8710b518d4SMichal Kubecek unsigned int end)
8810b518d4SMichal Kubecek {
8910b518d4SMichal Kubecek unsigned int start_word = start / 32;
9010b518d4SMichal Kubecek unsigned int end_word = end / 32;
9110b518d4SMichal Kubecek u32 mask;
9210b518d4SMichal Kubecek
9310b518d4SMichal Kubecek if (end <= start)
9410b518d4SMichal Kubecek return true;
9510b518d4SMichal Kubecek
9610b518d4SMichal Kubecek if (start % 32) {
9710b518d4SMichal Kubecek mask = ethnl_upper_bits(start);
9810b518d4SMichal Kubecek if (end_word == start_word) {
9910b518d4SMichal Kubecek mask &= ethnl_lower_bits(end);
10010b518d4SMichal Kubecek return map[start_word] & mask;
10110b518d4SMichal Kubecek }
10210b518d4SMichal Kubecek if (map[start_word] & mask)
10310b518d4SMichal Kubecek return true;
10410b518d4SMichal Kubecek start_word++;
10510b518d4SMichal Kubecek }
10610b518d4SMichal Kubecek
10710b518d4SMichal Kubecek if (!memchr_inv(map + start_word, '\0',
10810b518d4SMichal Kubecek (end_word - start_word) * sizeof(u32)))
10910b518d4SMichal Kubecek return true;
11010b518d4SMichal Kubecek if (end % 32 == 0)
11110b518d4SMichal Kubecek return true;
11210b518d4SMichal Kubecek return map[end_word] & ethnl_lower_bits(end);
11310b518d4SMichal Kubecek }
11410b518d4SMichal Kubecek
11510b518d4SMichal Kubecek /**
11610b518d4SMichal Kubecek * ethnl_bitmap32_update() - Modify u32 based bitmap according to value/mask
11710b518d4SMichal Kubecek * pair
11810b518d4SMichal Kubecek * @dst: bitmap to update
11910b518d4SMichal Kubecek * @nbits: bit size of the bitmap
12010b518d4SMichal Kubecek * @value: values to set
12110b518d4SMichal Kubecek * @mask: mask of bits to set
12210b518d4SMichal Kubecek * @mod: set to true if bitmap is modified, preserve if not
12310b518d4SMichal Kubecek *
12410b518d4SMichal Kubecek * Set bits in @dst bitmap which are set in @mask to values from @value, leave
12510b518d4SMichal Kubecek * the rest untouched. If destination bitmap was modified, set @mod to true,
12610b518d4SMichal Kubecek * leave as it is if not.
12710b518d4SMichal Kubecek */
ethnl_bitmap32_update(u32 * dst,unsigned int nbits,const u32 * value,const u32 * mask,bool * mod)12810b518d4SMichal Kubecek static void ethnl_bitmap32_update(u32 *dst, unsigned int nbits,
12910b518d4SMichal Kubecek const u32 *value, const u32 *mask, bool *mod)
13010b518d4SMichal Kubecek {
13110b518d4SMichal Kubecek while (nbits > 0) {
13210b518d4SMichal Kubecek u32 real_mask = mask ? *mask : ~(u32)0;
13310b518d4SMichal Kubecek u32 new_value;
13410b518d4SMichal Kubecek
13510b518d4SMichal Kubecek if (nbits < 32)
13610b518d4SMichal Kubecek real_mask &= ethnl_lower_bits(nbits);
13710b518d4SMichal Kubecek new_value = (*dst & ~real_mask) | (*value & real_mask);
13810b518d4SMichal Kubecek if (new_value != *dst) {
13910b518d4SMichal Kubecek *dst = new_value;
14010b518d4SMichal Kubecek *mod = true;
14110b518d4SMichal Kubecek }
14210b518d4SMichal Kubecek
14310b518d4SMichal Kubecek if (nbits <= 32)
14410b518d4SMichal Kubecek break;
14510b518d4SMichal Kubecek dst++;
14610b518d4SMichal Kubecek nbits -= 32;
14710b518d4SMichal Kubecek value++;
14810b518d4SMichal Kubecek if (mask)
14910b518d4SMichal Kubecek mask++;
15010b518d4SMichal Kubecek }
15110b518d4SMichal Kubecek }
15210b518d4SMichal Kubecek
ethnl_bitmap32_test_bit(const u32 * map,unsigned int index)15310b518d4SMichal Kubecek static bool ethnl_bitmap32_test_bit(const u32 *map, unsigned int index)
15410b518d4SMichal Kubecek {
15510b518d4SMichal Kubecek return map[index / 32] & (1U << (index % 32));
15610b518d4SMichal Kubecek }
15710b518d4SMichal Kubecek
15810b518d4SMichal Kubecek /**
15910b518d4SMichal Kubecek * ethnl_bitset32_size() - Calculate size of bitset nested attribute
16010b518d4SMichal Kubecek * @val: value bitmap (u32 based)
16110b518d4SMichal Kubecek * @mask: mask bitmap (u32 based, optional)
16210b518d4SMichal Kubecek * @nbits: bit length of the bitset
16310b518d4SMichal Kubecek * @names: array of bit names (optional)
16410b518d4SMichal Kubecek * @compact: assume compact format for output
16510b518d4SMichal Kubecek *
16610b518d4SMichal Kubecek * Estimate length of netlink attribute composed by a later call to
16710b518d4SMichal Kubecek * ethnl_put_bitset32() call with the same arguments.
16810b518d4SMichal Kubecek *
16910b518d4SMichal Kubecek * Return: negative error code or attribute length estimate
17010b518d4SMichal Kubecek */
ethnl_bitset32_size(const u32 * val,const u32 * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)17110b518d4SMichal Kubecek int ethnl_bitset32_size(const u32 *val, const u32 *mask, unsigned int nbits,
17210b518d4SMichal Kubecek ethnl_string_array_t names, bool compact)
17310b518d4SMichal Kubecek {
17410b518d4SMichal Kubecek unsigned int len = 0;
17510b518d4SMichal Kubecek
17610b518d4SMichal Kubecek /* list flag */
17710b518d4SMichal Kubecek if (!mask)
17810b518d4SMichal Kubecek len += nla_total_size(sizeof(u32));
17910b518d4SMichal Kubecek /* size */
18010b518d4SMichal Kubecek len += nla_total_size(sizeof(u32));
18110b518d4SMichal Kubecek
18210b518d4SMichal Kubecek if (compact) {
18310b518d4SMichal Kubecek unsigned int nwords = DIV_ROUND_UP(nbits, 32);
18410b518d4SMichal Kubecek
18510b518d4SMichal Kubecek /* value, mask */
18610b518d4SMichal Kubecek len += (mask ? 2 : 1) * nla_total_size(nwords * sizeof(u32));
18710b518d4SMichal Kubecek } else {
18810b518d4SMichal Kubecek unsigned int bits_len = 0;
18910b518d4SMichal Kubecek unsigned int bit_len, i;
19010b518d4SMichal Kubecek
19110b518d4SMichal Kubecek for (i = 0; i < nbits; i++) {
19210b518d4SMichal Kubecek const char *name = names ? names[i] : NULL;
19310b518d4SMichal Kubecek
19410b518d4SMichal Kubecek if (!ethnl_bitmap32_test_bit(mask ?: val, i))
19510b518d4SMichal Kubecek continue;
19610b518d4SMichal Kubecek /* index */
19710b518d4SMichal Kubecek bit_len = nla_total_size(sizeof(u32));
19810b518d4SMichal Kubecek /* name */
19910b518d4SMichal Kubecek if (name)
20010b518d4SMichal Kubecek bit_len += ethnl_strz_size(name);
20110b518d4SMichal Kubecek /* value */
20210b518d4SMichal Kubecek if (mask && ethnl_bitmap32_test_bit(val, i))
20310b518d4SMichal Kubecek bit_len += nla_total_size(0);
20410b518d4SMichal Kubecek
20510b518d4SMichal Kubecek /* bit nest */
20610b518d4SMichal Kubecek bits_len += nla_total_size(bit_len);
20710b518d4SMichal Kubecek }
20810b518d4SMichal Kubecek /* bits nest */
20910b518d4SMichal Kubecek len += nla_total_size(bits_len);
21010b518d4SMichal Kubecek }
21110b518d4SMichal Kubecek
21210b518d4SMichal Kubecek /* outermost nest */
21310b518d4SMichal Kubecek return nla_total_size(len);
21410b518d4SMichal Kubecek }
21510b518d4SMichal Kubecek
21610b518d4SMichal Kubecek /**
21710b518d4SMichal Kubecek * ethnl_put_bitset32() - Put a bitset nest into a message
21810b518d4SMichal Kubecek * @skb: skb with the message
21910b518d4SMichal Kubecek * @attrtype: attribute type for the bitset nest
22010b518d4SMichal Kubecek * @val: value bitmap (u32 based)
22110b518d4SMichal Kubecek * @mask: mask bitmap (u32 based, optional)
22210b518d4SMichal Kubecek * @nbits: bit length of the bitset
22310b518d4SMichal Kubecek * @names: array of bit names (optional)
22410b518d4SMichal Kubecek * @compact: use compact format for the output
22510b518d4SMichal Kubecek *
22610b518d4SMichal Kubecek * Compose a nested attribute representing a bitset. If @mask is null, simple
22710b518d4SMichal Kubecek * bitmap (bit list) is created, if @mask is provided, represent a value/mask
22810b518d4SMichal Kubecek * pair. Bit names are only used in verbose mode and when provided by calller.
22910b518d4SMichal Kubecek *
23010b518d4SMichal Kubecek * Return: 0 on success, negative error value on error
23110b518d4SMichal Kubecek */
ethnl_put_bitset32(struct sk_buff * skb,int attrtype,const u32 * val,const u32 * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)23210b518d4SMichal Kubecek int ethnl_put_bitset32(struct sk_buff *skb, int attrtype, const u32 *val,
23310b518d4SMichal Kubecek const u32 *mask, unsigned int nbits,
23410b518d4SMichal Kubecek ethnl_string_array_t names, bool compact)
23510b518d4SMichal Kubecek {
23610b518d4SMichal Kubecek struct nlattr *nest;
23710b518d4SMichal Kubecek struct nlattr *attr;
23810b518d4SMichal Kubecek
23910b518d4SMichal Kubecek nest = nla_nest_start(skb, attrtype);
24010b518d4SMichal Kubecek if (!nest)
24110b518d4SMichal Kubecek return -EMSGSIZE;
24210b518d4SMichal Kubecek
24310b518d4SMichal Kubecek if (!mask && nla_put_flag(skb, ETHTOOL_A_BITSET_NOMASK))
24410b518d4SMichal Kubecek goto nla_put_failure;
24510b518d4SMichal Kubecek if (nla_put_u32(skb, ETHTOOL_A_BITSET_SIZE, nbits))
24610b518d4SMichal Kubecek goto nla_put_failure;
24710b518d4SMichal Kubecek if (compact) {
24810b518d4SMichal Kubecek unsigned int nwords = DIV_ROUND_UP(nbits, 32);
24910b518d4SMichal Kubecek unsigned int nbytes = nwords * sizeof(u32);
25010b518d4SMichal Kubecek u32 *dst;
25110b518d4SMichal Kubecek
25210b518d4SMichal Kubecek attr = nla_reserve(skb, ETHTOOL_A_BITSET_VALUE, nbytes);
25310b518d4SMichal Kubecek if (!attr)
25410b518d4SMichal Kubecek goto nla_put_failure;
25510b518d4SMichal Kubecek dst = nla_data(attr);
25610b518d4SMichal Kubecek memcpy(dst, val, nbytes);
25710b518d4SMichal Kubecek if (nbits % 32)
25810b518d4SMichal Kubecek dst[nwords - 1] &= ethnl_lower_bits(nbits);
25910b518d4SMichal Kubecek
26010b518d4SMichal Kubecek if (mask) {
26110b518d4SMichal Kubecek attr = nla_reserve(skb, ETHTOOL_A_BITSET_MASK, nbytes);
26210b518d4SMichal Kubecek if (!attr)
26310b518d4SMichal Kubecek goto nla_put_failure;
26410b518d4SMichal Kubecek dst = nla_data(attr);
26510b518d4SMichal Kubecek memcpy(dst, mask, nbytes);
26610b518d4SMichal Kubecek if (nbits % 32)
26710b518d4SMichal Kubecek dst[nwords - 1] &= ethnl_lower_bits(nbits);
26810b518d4SMichal Kubecek }
26910b518d4SMichal Kubecek } else {
27010b518d4SMichal Kubecek struct nlattr *bits;
27110b518d4SMichal Kubecek unsigned int i;
27210b518d4SMichal Kubecek
27310b518d4SMichal Kubecek bits = nla_nest_start(skb, ETHTOOL_A_BITSET_BITS);
27410b518d4SMichal Kubecek if (!bits)
27510b518d4SMichal Kubecek goto nla_put_failure;
27610b518d4SMichal Kubecek for (i = 0; i < nbits; i++) {
27710b518d4SMichal Kubecek const char *name = names ? names[i] : NULL;
27810b518d4SMichal Kubecek
27910b518d4SMichal Kubecek if (!ethnl_bitmap32_test_bit(mask ?: val, i))
28010b518d4SMichal Kubecek continue;
28110b518d4SMichal Kubecek attr = nla_nest_start(skb, ETHTOOL_A_BITSET_BITS_BIT);
28210b518d4SMichal Kubecek if (!attr)
28310b518d4SMichal Kubecek goto nla_put_failure;
28410b518d4SMichal Kubecek if (nla_put_u32(skb, ETHTOOL_A_BITSET_BIT_INDEX, i))
28510b518d4SMichal Kubecek goto nla_put_failure;
28610b518d4SMichal Kubecek if (name &&
28710b518d4SMichal Kubecek ethnl_put_strz(skb, ETHTOOL_A_BITSET_BIT_NAME, name))
28810b518d4SMichal Kubecek goto nla_put_failure;
28910b518d4SMichal Kubecek if (mask && ethnl_bitmap32_test_bit(val, i) &&
29010b518d4SMichal Kubecek nla_put_flag(skb, ETHTOOL_A_BITSET_BIT_VALUE))
29110b518d4SMichal Kubecek goto nla_put_failure;
29210b518d4SMichal Kubecek nla_nest_end(skb, attr);
29310b518d4SMichal Kubecek }
29410b518d4SMichal Kubecek nla_nest_end(skb, bits);
29510b518d4SMichal Kubecek }
29610b518d4SMichal Kubecek
29710b518d4SMichal Kubecek nla_nest_end(skb, nest);
29810b518d4SMichal Kubecek return 0;
29910b518d4SMichal Kubecek
30010b518d4SMichal Kubecek nla_put_failure:
30110b518d4SMichal Kubecek nla_nest_cancel(skb, nest);
30210b518d4SMichal Kubecek return -EMSGSIZE;
30310b518d4SMichal Kubecek }
30410b518d4SMichal Kubecek
305ff419afaSJakub Kicinski static const struct nla_policy bitset_policy[] = {
30610b518d4SMichal Kubecek [ETHTOOL_A_BITSET_NOMASK] = { .type = NLA_FLAG },
307e34f1753SMichal Kubecek [ETHTOOL_A_BITSET_SIZE] = NLA_POLICY_MAX(NLA_U32,
308e34f1753SMichal Kubecek ETHNL_MAX_BITSET_SIZE),
30910b518d4SMichal Kubecek [ETHTOOL_A_BITSET_BITS] = { .type = NLA_NESTED },
31010b518d4SMichal Kubecek [ETHTOOL_A_BITSET_VALUE] = { .type = NLA_BINARY },
31110b518d4SMichal Kubecek [ETHTOOL_A_BITSET_MASK] = { .type = NLA_BINARY },
31210b518d4SMichal Kubecek };
31310b518d4SMichal Kubecek
314ff419afaSJakub Kicinski static const struct nla_policy bit_policy[] = {
31510b518d4SMichal Kubecek [ETHTOOL_A_BITSET_BIT_INDEX] = { .type = NLA_U32 },
31610b518d4SMichal Kubecek [ETHTOOL_A_BITSET_BIT_NAME] = { .type = NLA_NUL_STRING },
31710b518d4SMichal Kubecek [ETHTOOL_A_BITSET_BIT_VALUE] = { .type = NLA_FLAG },
31810b518d4SMichal Kubecek };
31910b518d4SMichal Kubecek
32010b518d4SMichal Kubecek /**
32110b518d4SMichal Kubecek * ethnl_bitset_is_compact() - check if bitset attribute represents a compact
32210b518d4SMichal Kubecek * bitset
32310b518d4SMichal Kubecek * @bitset: nested attribute representing a bitset
32410b518d4SMichal Kubecek * @compact: pointer for return value
32510b518d4SMichal Kubecek *
32610b518d4SMichal Kubecek * Return: 0 on success, negative error code on failure
32710b518d4SMichal Kubecek */
ethnl_bitset_is_compact(const struct nlattr * bitset,bool * compact)32810b518d4SMichal Kubecek int ethnl_bitset_is_compact(const struct nlattr *bitset, bool *compact)
32910b518d4SMichal Kubecek {
330ff419afaSJakub Kicinski struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
33110b518d4SMichal Kubecek int ret;
33210b518d4SMichal Kubecek
333ff419afaSJakub Kicinski ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, bitset,
33410b518d4SMichal Kubecek bitset_policy, NULL);
33510b518d4SMichal Kubecek if (ret < 0)
33610b518d4SMichal Kubecek return ret;
33710b518d4SMichal Kubecek
33810b518d4SMichal Kubecek if (tb[ETHTOOL_A_BITSET_BITS]) {
33910b518d4SMichal Kubecek if (tb[ETHTOOL_A_BITSET_VALUE] || tb[ETHTOOL_A_BITSET_MASK])
34010b518d4SMichal Kubecek return -EINVAL;
34110b518d4SMichal Kubecek *compact = false;
34210b518d4SMichal Kubecek return 0;
34310b518d4SMichal Kubecek }
34410b518d4SMichal Kubecek if (!tb[ETHTOOL_A_BITSET_SIZE] || !tb[ETHTOOL_A_BITSET_VALUE])
34510b518d4SMichal Kubecek return -EINVAL;
34610b518d4SMichal Kubecek
34710b518d4SMichal Kubecek *compact = true;
34810b518d4SMichal Kubecek return 0;
34910b518d4SMichal Kubecek }
35010b518d4SMichal Kubecek
35110b518d4SMichal Kubecek /**
35210b518d4SMichal Kubecek * ethnl_name_to_idx() - look up string index for a name
35310b518d4SMichal Kubecek * @names: array of ETH_GSTRING_LEN sized strings
35410b518d4SMichal Kubecek * @n_names: number of strings in the array
35510b518d4SMichal Kubecek * @name: name to look up
35610b518d4SMichal Kubecek *
35710b518d4SMichal Kubecek * Return: index of the string if found, -ENOENT if not found
35810b518d4SMichal Kubecek */
ethnl_name_to_idx(ethnl_string_array_t names,unsigned int n_names,const char * name)35910b518d4SMichal Kubecek static int ethnl_name_to_idx(ethnl_string_array_t names, unsigned int n_names,
36010b518d4SMichal Kubecek const char *name)
36110b518d4SMichal Kubecek {
36210b518d4SMichal Kubecek unsigned int i;
36310b518d4SMichal Kubecek
36410b518d4SMichal Kubecek if (!names)
36510b518d4SMichal Kubecek return -ENOENT;
36610b518d4SMichal Kubecek
36710b518d4SMichal Kubecek for (i = 0; i < n_names; i++) {
36810b518d4SMichal Kubecek /* names[i] may not be null terminated */
36910b518d4SMichal Kubecek if (!strncmp(names[i], name, ETH_GSTRING_LEN) &&
37010b518d4SMichal Kubecek strlen(name) <= ETH_GSTRING_LEN)
37110b518d4SMichal Kubecek return i;
37210b518d4SMichal Kubecek }
37310b518d4SMichal Kubecek
37410b518d4SMichal Kubecek return -ENOENT;
37510b518d4SMichal Kubecek }
37610b518d4SMichal Kubecek
ethnl_parse_bit(unsigned int * index,bool * val,unsigned int nbits,const struct nlattr * bit_attr,bool no_mask,ethnl_string_array_t names,struct netlink_ext_ack * extack)37710b518d4SMichal Kubecek static int ethnl_parse_bit(unsigned int *index, bool *val, unsigned int nbits,
37810b518d4SMichal Kubecek const struct nlattr *bit_attr, bool no_mask,
37910b518d4SMichal Kubecek ethnl_string_array_t names,
38010b518d4SMichal Kubecek struct netlink_ext_ack *extack)
38110b518d4SMichal Kubecek {
382ff419afaSJakub Kicinski struct nlattr *tb[ARRAY_SIZE(bit_policy)];
38310b518d4SMichal Kubecek int ret, idx;
38410b518d4SMichal Kubecek
385ff419afaSJakub Kicinski ret = nla_parse_nested(tb, ARRAY_SIZE(bit_policy) - 1, bit_attr,
38610b518d4SMichal Kubecek bit_policy, extack);
38710b518d4SMichal Kubecek if (ret < 0)
38810b518d4SMichal Kubecek return ret;
38910b518d4SMichal Kubecek
39010b518d4SMichal Kubecek if (tb[ETHTOOL_A_BITSET_BIT_INDEX]) {
39110b518d4SMichal Kubecek const char *name;
39210b518d4SMichal Kubecek
39310b518d4SMichal Kubecek idx = nla_get_u32(tb[ETHTOOL_A_BITSET_BIT_INDEX]);
39410b518d4SMichal Kubecek if (idx >= nbits) {
39510b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack,
39610b518d4SMichal Kubecek tb[ETHTOOL_A_BITSET_BIT_INDEX],
39710b518d4SMichal Kubecek "bit index too high");
39810b518d4SMichal Kubecek return -EOPNOTSUPP;
39910b518d4SMichal Kubecek }
40010b518d4SMichal Kubecek name = names ? names[idx] : NULL;
40110b518d4SMichal Kubecek if (tb[ETHTOOL_A_BITSET_BIT_NAME] && name &&
40210b518d4SMichal Kubecek strncmp(nla_data(tb[ETHTOOL_A_BITSET_BIT_NAME]), name,
40310b518d4SMichal Kubecek nla_len(tb[ETHTOOL_A_BITSET_BIT_NAME]))) {
40410b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, bit_attr,
40510b518d4SMichal Kubecek "bit index and name mismatch");
40610b518d4SMichal Kubecek return -EINVAL;
40710b518d4SMichal Kubecek }
40810b518d4SMichal Kubecek } else if (tb[ETHTOOL_A_BITSET_BIT_NAME]) {
40910b518d4SMichal Kubecek idx = ethnl_name_to_idx(names, nbits,
41010b518d4SMichal Kubecek nla_data(tb[ETHTOOL_A_BITSET_BIT_NAME]));
41110b518d4SMichal Kubecek if (idx < 0) {
41210b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack,
41310b518d4SMichal Kubecek tb[ETHTOOL_A_BITSET_BIT_NAME],
41410b518d4SMichal Kubecek "bit name not found");
41510b518d4SMichal Kubecek return -EOPNOTSUPP;
41610b518d4SMichal Kubecek }
41710b518d4SMichal Kubecek } else {
41810b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, bit_attr,
41910b518d4SMichal Kubecek "neither bit index nor name specified");
42010b518d4SMichal Kubecek return -EINVAL;
42110b518d4SMichal Kubecek }
42210b518d4SMichal Kubecek
42310b518d4SMichal Kubecek *index = idx;
42410b518d4SMichal Kubecek *val = no_mask || tb[ETHTOOL_A_BITSET_BIT_VALUE];
42510b518d4SMichal Kubecek return 0;
42610b518d4SMichal Kubecek }
42710b518d4SMichal Kubecek
428*e5242c5fSKory Maincent /**
429*e5242c5fSKory Maincent * ethnl_bitmap32_equal() - Compare two bitmaps
430*e5242c5fSKory Maincent * @map1: first bitmap
431*e5242c5fSKory Maincent * @map2: second bitmap
432*e5242c5fSKory Maincent * @nbits: bit size to compare
433*e5242c5fSKory Maincent *
434*e5242c5fSKory Maincent * Return: true if first @nbits are equal, false if not
435*e5242c5fSKory Maincent */
ethnl_bitmap32_equal(const u32 * map1,const u32 * map2,unsigned int nbits)436*e5242c5fSKory Maincent static bool ethnl_bitmap32_equal(const u32 *map1, const u32 *map2,
437*e5242c5fSKory Maincent unsigned int nbits)
438*e5242c5fSKory Maincent {
439*e5242c5fSKory Maincent if (memcmp(map1, map2, nbits / 32 * sizeof(u32)))
440*e5242c5fSKory Maincent return false;
441*e5242c5fSKory Maincent if (nbits % 32 == 0)
442*e5242c5fSKory Maincent return true;
443*e5242c5fSKory Maincent return !((map1[nbits / 32] ^ map2[nbits / 32]) &
444*e5242c5fSKory Maincent ethnl_lower_bits(nbits % 32));
445*e5242c5fSKory Maincent }
446*e5242c5fSKory Maincent
44710b518d4SMichal Kubecek static int
ethnl_update_bitset32_verbose(u32 * bitmap,unsigned int nbits,const struct nlattr * attr,struct nlattr ** tb,ethnl_string_array_t names,struct netlink_ext_ack * extack,bool * mod)44810b518d4SMichal Kubecek ethnl_update_bitset32_verbose(u32 *bitmap, unsigned int nbits,
44910b518d4SMichal Kubecek const struct nlattr *attr, struct nlattr **tb,
45010b518d4SMichal Kubecek ethnl_string_array_t names,
45110b518d4SMichal Kubecek struct netlink_ext_ack *extack, bool *mod)
45210b518d4SMichal Kubecek {
453*e5242c5fSKory Maincent u32 *saved_bitmap = NULL;
45410b518d4SMichal Kubecek struct nlattr *bit_attr;
45510b518d4SMichal Kubecek bool no_mask;
45610b518d4SMichal Kubecek int rem;
45710b518d4SMichal Kubecek int ret;
45810b518d4SMichal Kubecek
45910b518d4SMichal Kubecek if (tb[ETHTOOL_A_BITSET_VALUE]) {
46010b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
46110b518d4SMichal Kubecek "value only allowed in compact bitset");
46210b518d4SMichal Kubecek return -EINVAL;
46310b518d4SMichal Kubecek }
46410b518d4SMichal Kubecek if (tb[ETHTOOL_A_BITSET_MASK]) {
46510b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
46610b518d4SMichal Kubecek "mask only allowed in compact bitset");
46710b518d4SMichal Kubecek return -EINVAL;
46810b518d4SMichal Kubecek }
46966991703SMichal Kubecek
47010b518d4SMichal Kubecek no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
471*e5242c5fSKory Maincent if (no_mask) {
472*e5242c5fSKory Maincent unsigned int nwords = DIV_ROUND_UP(nbits, 32);
473*e5242c5fSKory Maincent unsigned int nbytes = nwords * sizeof(u32);
474*e5242c5fSKory Maincent bool dummy;
475*e5242c5fSKory Maincent
476*e5242c5fSKory Maincent /* The bitmap size is only the size of the map part without
477*e5242c5fSKory Maincent * its mask part.
478*e5242c5fSKory Maincent */
479*e5242c5fSKory Maincent saved_bitmap = kcalloc(nwords, sizeof(u32), GFP_KERNEL);
480*e5242c5fSKory Maincent if (!saved_bitmap)
481*e5242c5fSKory Maincent return -ENOMEM;
482*e5242c5fSKory Maincent memcpy(saved_bitmap, bitmap, nbytes);
483*e5242c5fSKory Maincent ethnl_bitmap32_clear(bitmap, 0, nbits, &dummy);
484*e5242c5fSKory Maincent }
48510b518d4SMichal Kubecek
48610b518d4SMichal Kubecek nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
48710b518d4SMichal Kubecek bool old_val, new_val;
48810b518d4SMichal Kubecek unsigned int idx;
48910b518d4SMichal Kubecek
49010b518d4SMichal Kubecek if (nla_type(bit_attr) != ETHTOOL_A_BITSET_BITS_BIT) {
49110b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, bit_attr,
49210b518d4SMichal Kubecek "only ETHTOOL_A_BITSET_BITS_BIT allowed in ETHTOOL_A_BITSET_BITS");
493*e5242c5fSKory Maincent kfree(saved_bitmap);
49452451502SKory Maincent return -EINVAL;
49510b518d4SMichal Kubecek }
49610b518d4SMichal Kubecek ret = ethnl_parse_bit(&idx, &new_val, nbits, bit_attr, no_mask,
49710b518d4SMichal Kubecek names, extack);
498*e5242c5fSKory Maincent if (ret < 0) {
499*e5242c5fSKory Maincent kfree(saved_bitmap);
50052451502SKory Maincent return ret;
501*e5242c5fSKory Maincent }
50252451502SKory Maincent old_val = bitmap[idx / 32] & ((u32)1 << (idx % 32));
50310b518d4SMichal Kubecek if (new_val != old_val) {
50410b518d4SMichal Kubecek if (new_val)
50510b518d4SMichal Kubecek bitmap[idx / 32] |= ((u32)1 << (idx % 32));
50610b518d4SMichal Kubecek else
50710b518d4SMichal Kubecek bitmap[idx / 32] &= ~((u32)1 << (idx % 32));
508*e5242c5fSKory Maincent if (!no_mask)
50910b518d4SMichal Kubecek *mod = true;
51010b518d4SMichal Kubecek }
51110b518d4SMichal Kubecek }
51210b518d4SMichal Kubecek
513*e5242c5fSKory Maincent if (no_mask && !ethnl_bitmap32_equal(saved_bitmap, bitmap, nbits))
514*e5242c5fSKory Maincent *mod = true;
515*e5242c5fSKory Maincent
516*e5242c5fSKory Maincent kfree(saved_bitmap);
51752451502SKory Maincent return 0;
51810b518d4SMichal Kubecek }
51910b518d4SMichal Kubecek
ethnl_compact_sanity_checks(unsigned int nbits,const struct nlattr * nest,struct nlattr ** tb,struct netlink_ext_ack * extack)52010b518d4SMichal Kubecek static int ethnl_compact_sanity_checks(unsigned int nbits,
52110b518d4SMichal Kubecek const struct nlattr *nest,
52210b518d4SMichal Kubecek struct nlattr **tb,
52310b518d4SMichal Kubecek struct netlink_ext_ack *extack)
52410b518d4SMichal Kubecek {
52510b518d4SMichal Kubecek bool no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
52610b518d4SMichal Kubecek unsigned int attr_nbits, attr_nwords;
52710b518d4SMichal Kubecek const struct nlattr *test_attr;
52810b518d4SMichal Kubecek
52910b518d4SMichal Kubecek if (no_mask && tb[ETHTOOL_A_BITSET_MASK]) {
53010b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
53110b518d4SMichal Kubecek "mask not allowed in list bitset");
53210b518d4SMichal Kubecek return -EINVAL;
53310b518d4SMichal Kubecek }
53410b518d4SMichal Kubecek if (!tb[ETHTOOL_A_BITSET_SIZE]) {
53510b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, nest,
53610b518d4SMichal Kubecek "missing size in compact bitset");
53710b518d4SMichal Kubecek return -EINVAL;
53810b518d4SMichal Kubecek }
53910b518d4SMichal Kubecek if (!tb[ETHTOOL_A_BITSET_VALUE]) {
54010b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, nest,
54110b518d4SMichal Kubecek "missing value in compact bitset");
54210b518d4SMichal Kubecek return -EINVAL;
54310b518d4SMichal Kubecek }
54410b518d4SMichal Kubecek if (!no_mask && !tb[ETHTOOL_A_BITSET_MASK]) {
54510b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, nest,
54610b518d4SMichal Kubecek "missing mask in compact nonlist bitset");
54710b518d4SMichal Kubecek return -EINVAL;
54810b518d4SMichal Kubecek }
54910b518d4SMichal Kubecek
55010b518d4SMichal Kubecek attr_nbits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
55110b518d4SMichal Kubecek attr_nwords = DIV_ROUND_UP(attr_nbits, 32);
55210b518d4SMichal Kubecek if (nla_len(tb[ETHTOOL_A_BITSET_VALUE]) != attr_nwords * sizeof(u32)) {
55310b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
55410b518d4SMichal Kubecek "bitset value length does not match size");
55510b518d4SMichal Kubecek return -EINVAL;
55610b518d4SMichal Kubecek }
55710b518d4SMichal Kubecek if (tb[ETHTOOL_A_BITSET_MASK] &&
55810b518d4SMichal Kubecek nla_len(tb[ETHTOOL_A_BITSET_MASK]) != attr_nwords * sizeof(u32)) {
55910b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
56010b518d4SMichal Kubecek "bitset mask length does not match size");
56110b518d4SMichal Kubecek return -EINVAL;
56210b518d4SMichal Kubecek }
56310b518d4SMichal Kubecek if (attr_nbits <= nbits)
56410b518d4SMichal Kubecek return 0;
56510b518d4SMichal Kubecek
56610b518d4SMichal Kubecek test_attr = no_mask ? tb[ETHTOOL_A_BITSET_VALUE] :
56710b518d4SMichal Kubecek tb[ETHTOOL_A_BITSET_MASK];
56810b518d4SMichal Kubecek if (ethnl_bitmap32_not_zero(nla_data(test_attr), nbits, attr_nbits)) {
56910b518d4SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, test_attr,
57010b518d4SMichal Kubecek "cannot modify bits past kernel bitset size");
57110b518d4SMichal Kubecek return -EINVAL;
57210b518d4SMichal Kubecek }
57310b518d4SMichal Kubecek return 0;
57410b518d4SMichal Kubecek }
57510b518d4SMichal Kubecek
57610b518d4SMichal Kubecek /**
57710b518d4SMichal Kubecek * ethnl_update_bitset32() - Apply a bitset nest to a u32 based bitmap
57810b518d4SMichal Kubecek * @bitmap: bitmap to update
57910b518d4SMichal Kubecek * @nbits: size of the updated bitmap in bits
58010b518d4SMichal Kubecek * @attr: nest attribute to parse and apply
58110b518d4SMichal Kubecek * @names: array of bit names; may be null for compact format
58210b518d4SMichal Kubecek * @extack: extack for error reporting
58310b518d4SMichal Kubecek * @mod: set this to true if bitmap is modified, leave as it is if not
58410b518d4SMichal Kubecek *
58510b518d4SMichal Kubecek * Apply bitset netsted attribute to a bitmap. If the attribute represents
58610b518d4SMichal Kubecek * a bit list, @bitmap is set to its contents; otherwise, bits in mask are
58710b518d4SMichal Kubecek * set to values from value. Bitmaps in the attribute may be longer than
58810b518d4SMichal Kubecek * @nbits but the message must not request modifying any bits past @nbits.
58910b518d4SMichal Kubecek *
59010b518d4SMichal Kubecek * Return: negative error code on failure, 0 on success
59110b518d4SMichal Kubecek */
ethnl_update_bitset32(u32 * bitmap,unsigned int nbits,const struct nlattr * attr,ethnl_string_array_t names,struct netlink_ext_ack * extack,bool * mod)59210b518d4SMichal Kubecek int ethnl_update_bitset32(u32 *bitmap, unsigned int nbits,
59310b518d4SMichal Kubecek const struct nlattr *attr, ethnl_string_array_t names,
59410b518d4SMichal Kubecek struct netlink_ext_ack *extack, bool *mod)
59510b518d4SMichal Kubecek {
596ff419afaSJakub Kicinski struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
59710b518d4SMichal Kubecek unsigned int change_bits;
59810b518d4SMichal Kubecek bool no_mask;
59910b518d4SMichal Kubecek int ret;
60010b518d4SMichal Kubecek
60110b518d4SMichal Kubecek if (!attr)
60210b518d4SMichal Kubecek return 0;
603ff419afaSJakub Kicinski ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, attr,
604ff419afaSJakub Kicinski bitset_policy, extack);
60510b518d4SMichal Kubecek if (ret < 0)
60610b518d4SMichal Kubecek return ret;
60710b518d4SMichal Kubecek
60810b518d4SMichal Kubecek if (tb[ETHTOOL_A_BITSET_BITS])
60910b518d4SMichal Kubecek return ethnl_update_bitset32_verbose(bitmap, nbits, attr, tb,
61010b518d4SMichal Kubecek names, extack, mod);
61110b518d4SMichal Kubecek ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack);
61210b518d4SMichal Kubecek if (ret < 0)
61310b518d4SMichal Kubecek return ret;
61410b518d4SMichal Kubecek
61510b518d4SMichal Kubecek no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
61610b518d4SMichal Kubecek change_bits = min_t(unsigned int,
61710b518d4SMichal Kubecek nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]), nbits);
61810b518d4SMichal Kubecek ethnl_bitmap32_update(bitmap, change_bits,
61910b518d4SMichal Kubecek nla_data(tb[ETHTOOL_A_BITSET_VALUE]),
62010b518d4SMichal Kubecek no_mask ? NULL :
62110b518d4SMichal Kubecek nla_data(tb[ETHTOOL_A_BITSET_MASK]),
62210b518d4SMichal Kubecek mod);
62310b518d4SMichal Kubecek if (no_mask && change_bits < nbits)
62410b518d4SMichal Kubecek ethnl_bitmap32_clear(bitmap, change_bits, nbits, mod);
62510b518d4SMichal Kubecek
62610b518d4SMichal Kubecek return 0;
62710b518d4SMichal Kubecek }
62810b518d4SMichal Kubecek
62988db6d1eSMichal Kubecek /**
63088db6d1eSMichal Kubecek * ethnl_parse_bitset() - Compute effective value and mask from bitset nest
63188db6d1eSMichal Kubecek * @val: unsigned long based bitmap to put value into
63288db6d1eSMichal Kubecek * @mask: unsigned long based bitmap to put mask into
63388db6d1eSMichal Kubecek * @nbits: size of @val and @mask bitmaps
63488db6d1eSMichal Kubecek * @attr: nest attribute to parse and apply
63588db6d1eSMichal Kubecek * @names: array of bit names; may be null for compact format
63688db6d1eSMichal Kubecek * @extack: extack for error reporting
63788db6d1eSMichal Kubecek *
63888db6d1eSMichal Kubecek * Provide @nbits size long bitmaps for value and mask so that
63988db6d1eSMichal Kubecek * x = (val & mask) | (x & ~mask) would modify any @nbits sized bitmap x
64088db6d1eSMichal Kubecek * the same way ethnl_update_bitset() with the same bitset attribute would.
64188db6d1eSMichal Kubecek *
64288db6d1eSMichal Kubecek * Return: negative error code on failure, 0 on success
64388db6d1eSMichal Kubecek */
ethnl_parse_bitset(unsigned long * val,unsigned long * mask,unsigned int nbits,const struct nlattr * attr,ethnl_string_array_t names,struct netlink_ext_ack * extack)64488db6d1eSMichal Kubecek int ethnl_parse_bitset(unsigned long *val, unsigned long *mask,
64588db6d1eSMichal Kubecek unsigned int nbits, const struct nlattr *attr,
64688db6d1eSMichal Kubecek ethnl_string_array_t names,
64788db6d1eSMichal Kubecek struct netlink_ext_ack *extack)
64888db6d1eSMichal Kubecek {
649ff419afaSJakub Kicinski struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
65088db6d1eSMichal Kubecek const struct nlattr *bit_attr;
65188db6d1eSMichal Kubecek bool no_mask;
65288db6d1eSMichal Kubecek int rem;
65388db6d1eSMichal Kubecek int ret;
65488db6d1eSMichal Kubecek
65588db6d1eSMichal Kubecek if (!attr)
65688db6d1eSMichal Kubecek return 0;
657ff419afaSJakub Kicinski ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, attr,
658ff419afaSJakub Kicinski bitset_policy, extack);
65988db6d1eSMichal Kubecek if (ret < 0)
66088db6d1eSMichal Kubecek return ret;
66188db6d1eSMichal Kubecek no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
66288db6d1eSMichal Kubecek
66388db6d1eSMichal Kubecek if (!tb[ETHTOOL_A_BITSET_BITS]) {
66488db6d1eSMichal Kubecek unsigned int change_bits;
66588db6d1eSMichal Kubecek
66688db6d1eSMichal Kubecek ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack);
66788db6d1eSMichal Kubecek if (ret < 0)
66888db6d1eSMichal Kubecek return ret;
66988db6d1eSMichal Kubecek
67088db6d1eSMichal Kubecek change_bits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
671a770bf51SMichal Kubecek if (change_bits > nbits)
672a770bf51SMichal Kubecek change_bits = nbits;
67388db6d1eSMichal Kubecek bitmap_from_arr32(val, nla_data(tb[ETHTOOL_A_BITSET_VALUE]),
67488db6d1eSMichal Kubecek change_bits);
67588db6d1eSMichal Kubecek if (change_bits < nbits)
67688db6d1eSMichal Kubecek bitmap_clear(val, change_bits, nbits - change_bits);
67788db6d1eSMichal Kubecek if (no_mask) {
67888db6d1eSMichal Kubecek bitmap_fill(mask, nbits);
67988db6d1eSMichal Kubecek } else {
68088db6d1eSMichal Kubecek bitmap_from_arr32(mask,
68188db6d1eSMichal Kubecek nla_data(tb[ETHTOOL_A_BITSET_MASK]),
68288db6d1eSMichal Kubecek change_bits);
68388db6d1eSMichal Kubecek if (change_bits < nbits)
68488db6d1eSMichal Kubecek bitmap_clear(mask, change_bits,
68588db6d1eSMichal Kubecek nbits - change_bits);
68688db6d1eSMichal Kubecek }
68788db6d1eSMichal Kubecek
68888db6d1eSMichal Kubecek return 0;
68988db6d1eSMichal Kubecek }
69088db6d1eSMichal Kubecek
69188db6d1eSMichal Kubecek if (tb[ETHTOOL_A_BITSET_VALUE]) {
69288db6d1eSMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
69388db6d1eSMichal Kubecek "value only allowed in compact bitset");
69488db6d1eSMichal Kubecek return -EINVAL;
69588db6d1eSMichal Kubecek }
69688db6d1eSMichal Kubecek if (tb[ETHTOOL_A_BITSET_MASK]) {
69788db6d1eSMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
69888db6d1eSMichal Kubecek "mask only allowed in compact bitset");
69988db6d1eSMichal Kubecek return -EINVAL;
70088db6d1eSMichal Kubecek }
70188db6d1eSMichal Kubecek
70288db6d1eSMichal Kubecek bitmap_zero(val, nbits);
70388db6d1eSMichal Kubecek if (no_mask)
70488db6d1eSMichal Kubecek bitmap_fill(mask, nbits);
70588db6d1eSMichal Kubecek else
70688db6d1eSMichal Kubecek bitmap_zero(mask, nbits);
70788db6d1eSMichal Kubecek
70888db6d1eSMichal Kubecek nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
70988db6d1eSMichal Kubecek unsigned int idx;
71088db6d1eSMichal Kubecek bool bit_val;
71188db6d1eSMichal Kubecek
71288db6d1eSMichal Kubecek ret = ethnl_parse_bit(&idx, &bit_val, nbits, bit_attr, no_mask,
71388db6d1eSMichal Kubecek names, extack);
71488db6d1eSMichal Kubecek if (ret < 0)
71588db6d1eSMichal Kubecek return ret;
71688db6d1eSMichal Kubecek if (bit_val)
71788db6d1eSMichal Kubecek __set_bit(idx, val);
71888db6d1eSMichal Kubecek if (!no_mask)
71988db6d1eSMichal Kubecek __set_bit(idx, mask);
72088db6d1eSMichal Kubecek }
72188db6d1eSMichal Kubecek
72288db6d1eSMichal Kubecek return 0;
72388db6d1eSMichal Kubecek }
72488db6d1eSMichal Kubecek
72510b518d4SMichal Kubecek #if BITS_PER_LONG == 64 && defined(__BIG_ENDIAN)
72610b518d4SMichal Kubecek
72710b518d4SMichal Kubecek /* 64-bit big endian architectures are the only case when u32 based bitmaps
72810b518d4SMichal Kubecek * and unsigned long based bitmaps have different memory layout so that we
72910b518d4SMichal Kubecek * cannot simply cast the latter to the former and need actual wrappers
73010b518d4SMichal Kubecek * converting the latter to the former.
73110b518d4SMichal Kubecek *
73210b518d4SMichal Kubecek * To reduce the number of slab allocations, the wrappers use fixed size local
73310b518d4SMichal Kubecek * variables for bitmaps up to ETHNL_SMALL_BITMAP_BITS bits which is the
73410b518d4SMichal Kubecek * majority of bitmaps used by ethtool.
73510b518d4SMichal Kubecek */
73610b518d4SMichal Kubecek #define ETHNL_SMALL_BITMAP_BITS 128
73710b518d4SMichal Kubecek #define ETHNL_SMALL_BITMAP_WORDS DIV_ROUND_UP(ETHNL_SMALL_BITMAP_BITS, 32)
73810b518d4SMichal Kubecek
ethnl_bitset_size(const unsigned long * val,const unsigned long * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)73910b518d4SMichal Kubecek int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
74010b518d4SMichal Kubecek unsigned int nbits, ethnl_string_array_t names,
74110b518d4SMichal Kubecek bool compact)
74210b518d4SMichal Kubecek {
74310b518d4SMichal Kubecek u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS];
74410b518d4SMichal Kubecek u32 small_val32[ETHNL_SMALL_BITMAP_WORDS];
74510b518d4SMichal Kubecek u32 *mask32;
74610b518d4SMichal Kubecek u32 *val32;
74710b518d4SMichal Kubecek int ret;
74810b518d4SMichal Kubecek
74910b518d4SMichal Kubecek if (nbits > ETHNL_SMALL_BITMAP_BITS) {
75010b518d4SMichal Kubecek unsigned int nwords = DIV_ROUND_UP(nbits, 32);
75110b518d4SMichal Kubecek
75210b518d4SMichal Kubecek val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL);
75310b518d4SMichal Kubecek if (!val32)
75410b518d4SMichal Kubecek return -ENOMEM;
75510b518d4SMichal Kubecek mask32 = val32 + nwords;
75610b518d4SMichal Kubecek } else {
75710b518d4SMichal Kubecek val32 = small_val32;
75810b518d4SMichal Kubecek mask32 = small_mask32;
75910b518d4SMichal Kubecek }
76010b518d4SMichal Kubecek
76110b518d4SMichal Kubecek bitmap_to_arr32(val32, val, nbits);
76210b518d4SMichal Kubecek if (mask)
76310b518d4SMichal Kubecek bitmap_to_arr32(mask32, mask, nbits);
76410b518d4SMichal Kubecek else
76510b518d4SMichal Kubecek mask32 = NULL;
76610b518d4SMichal Kubecek ret = ethnl_bitset32_size(val32, mask32, nbits, names, compact);
76710b518d4SMichal Kubecek
76810b518d4SMichal Kubecek if (nbits > ETHNL_SMALL_BITMAP_BITS)
76910b518d4SMichal Kubecek kfree(val32);
77010b518d4SMichal Kubecek
77110b518d4SMichal Kubecek return ret;
77210b518d4SMichal Kubecek }
77310b518d4SMichal Kubecek
ethnl_put_bitset(struct sk_buff * skb,int attrtype,const unsigned long * val,const unsigned long * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)77410b518d4SMichal Kubecek int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
77510b518d4SMichal Kubecek const unsigned long *val, const unsigned long *mask,
77610b518d4SMichal Kubecek unsigned int nbits, ethnl_string_array_t names,
77710b518d4SMichal Kubecek bool compact)
77810b518d4SMichal Kubecek {
77910b518d4SMichal Kubecek u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS];
78010b518d4SMichal Kubecek u32 small_val32[ETHNL_SMALL_BITMAP_WORDS];
78110b518d4SMichal Kubecek u32 *mask32;
78210b518d4SMichal Kubecek u32 *val32;
78310b518d4SMichal Kubecek int ret;
78410b518d4SMichal Kubecek
78510b518d4SMichal Kubecek if (nbits > ETHNL_SMALL_BITMAP_BITS) {
78610b518d4SMichal Kubecek unsigned int nwords = DIV_ROUND_UP(nbits, 32);
78710b518d4SMichal Kubecek
78810b518d4SMichal Kubecek val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL);
78910b518d4SMichal Kubecek if (!val32)
79010b518d4SMichal Kubecek return -ENOMEM;
79110b518d4SMichal Kubecek mask32 = val32 + nwords;
79210b518d4SMichal Kubecek } else {
79310b518d4SMichal Kubecek val32 = small_val32;
79410b518d4SMichal Kubecek mask32 = small_mask32;
79510b518d4SMichal Kubecek }
79610b518d4SMichal Kubecek
79710b518d4SMichal Kubecek bitmap_to_arr32(val32, val, nbits);
79810b518d4SMichal Kubecek if (mask)
79910b518d4SMichal Kubecek bitmap_to_arr32(mask32, mask, nbits);
80010b518d4SMichal Kubecek else
80110b518d4SMichal Kubecek mask32 = NULL;
80210b518d4SMichal Kubecek ret = ethnl_put_bitset32(skb, attrtype, val32, mask32, nbits, names,
80310b518d4SMichal Kubecek compact);
80410b518d4SMichal Kubecek
80510b518d4SMichal Kubecek if (nbits > ETHNL_SMALL_BITMAP_BITS)
80610b518d4SMichal Kubecek kfree(val32);
80710b518d4SMichal Kubecek
80810b518d4SMichal Kubecek return ret;
80910b518d4SMichal Kubecek }
81010b518d4SMichal Kubecek
ethnl_update_bitset(unsigned long * bitmap,unsigned int nbits,const struct nlattr * attr,ethnl_string_array_t names,struct netlink_ext_ack * extack,bool * mod)81110b518d4SMichal Kubecek int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
81210b518d4SMichal Kubecek const struct nlattr *attr, ethnl_string_array_t names,
81310b518d4SMichal Kubecek struct netlink_ext_ack *extack, bool *mod)
81410b518d4SMichal Kubecek {
81510b518d4SMichal Kubecek u32 small_bitmap32[ETHNL_SMALL_BITMAP_WORDS];
81610b518d4SMichal Kubecek u32 *bitmap32 = small_bitmap32;
81710b518d4SMichal Kubecek bool u32_mod = false;
81810b518d4SMichal Kubecek int ret;
81910b518d4SMichal Kubecek
82010b518d4SMichal Kubecek if (nbits > ETHNL_SMALL_BITMAP_BITS) {
82110b518d4SMichal Kubecek unsigned int dst_words = DIV_ROUND_UP(nbits, 32);
82210b518d4SMichal Kubecek
82310b518d4SMichal Kubecek bitmap32 = kmalloc_array(dst_words, sizeof(u32), GFP_KERNEL);
82410b518d4SMichal Kubecek if (!bitmap32)
82510b518d4SMichal Kubecek return -ENOMEM;
82610b518d4SMichal Kubecek }
82710b518d4SMichal Kubecek
82810b518d4SMichal Kubecek bitmap_to_arr32(bitmap32, bitmap, nbits);
82910b518d4SMichal Kubecek ret = ethnl_update_bitset32(bitmap32, nbits, attr, names, extack,
83010b518d4SMichal Kubecek &u32_mod);
83110b518d4SMichal Kubecek if (u32_mod) {
83210b518d4SMichal Kubecek bitmap_from_arr32(bitmap, bitmap32, nbits);
83310b518d4SMichal Kubecek *mod = true;
83410b518d4SMichal Kubecek }
83510b518d4SMichal Kubecek
83610b518d4SMichal Kubecek if (nbits > ETHNL_SMALL_BITMAP_BITS)
83710b518d4SMichal Kubecek kfree(bitmap32);
83810b518d4SMichal Kubecek
83910b518d4SMichal Kubecek return ret;
84010b518d4SMichal Kubecek }
84110b518d4SMichal Kubecek
84210b518d4SMichal Kubecek #else
84310b518d4SMichal Kubecek
84410b518d4SMichal Kubecek /* On little endian 64-bit and all 32-bit architectures, an unsigned long
84510b518d4SMichal Kubecek * based bitmap can be interpreted as u32 based one using a simple cast.
84610b518d4SMichal Kubecek */
84710b518d4SMichal Kubecek
ethnl_bitset_size(const unsigned long * val,const unsigned long * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)84810b518d4SMichal Kubecek int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
84910b518d4SMichal Kubecek unsigned int nbits, ethnl_string_array_t names,
85010b518d4SMichal Kubecek bool compact)
85110b518d4SMichal Kubecek {
85210b518d4SMichal Kubecek return ethnl_bitset32_size((const u32 *)val, (const u32 *)mask, nbits,
85310b518d4SMichal Kubecek names, compact);
85410b518d4SMichal Kubecek }
85510b518d4SMichal Kubecek
ethnl_put_bitset(struct sk_buff * skb,int attrtype,const unsigned long * val,const unsigned long * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)85610b518d4SMichal Kubecek int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
85710b518d4SMichal Kubecek const unsigned long *val, const unsigned long *mask,
85810b518d4SMichal Kubecek unsigned int nbits, ethnl_string_array_t names,
85910b518d4SMichal Kubecek bool compact)
86010b518d4SMichal Kubecek {
86110b518d4SMichal Kubecek return ethnl_put_bitset32(skb, attrtype, (const u32 *)val,
86210b518d4SMichal Kubecek (const u32 *)mask, nbits, names, compact);
86310b518d4SMichal Kubecek }
86410b518d4SMichal Kubecek
ethnl_update_bitset(unsigned long * bitmap,unsigned int nbits,const struct nlattr * attr,ethnl_string_array_t names,struct netlink_ext_ack * extack,bool * mod)86510b518d4SMichal Kubecek int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
86610b518d4SMichal Kubecek const struct nlattr *attr, ethnl_string_array_t names,
86710b518d4SMichal Kubecek struct netlink_ext_ack *extack, bool *mod)
86810b518d4SMichal Kubecek {
86910b518d4SMichal Kubecek return ethnl_update_bitset32((u32 *)bitmap, nbits, attr, names, extack,
87010b518d4SMichal Kubecek mod);
87110b518d4SMichal Kubecek }
87210b518d4SMichal Kubecek
87310b518d4SMichal Kubecek #endif /* BITS_PER_LONG == 64 && defined(__BIG_ENDIAN) */
874