bitset.c 22 KB

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