Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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