bitset.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. /**
  380. * ethnl_bitmap32_equal() - Compare two bitmaps
  381. * @map1: first bitmap
  382. * @map2: second bitmap
  383. * @nbits: bit size to compare
  384. *
  385. * Return: true if first @nbits are equal, false if not
  386. */
  387. static bool ethnl_bitmap32_equal(const u32 *map1, const u32 *map2,
  388. unsigned int nbits)
  389. {
  390. if (memcmp(map1, map2, nbits / 32 * sizeof(u32)))
  391. return false;
  392. if (nbits % 32 == 0)
  393. return true;
  394. return !((map1[nbits / 32] ^ map2[nbits / 32]) &
  395. ethnl_lower_bits(nbits % 32));
  396. }
  397. static int
  398. ethnl_update_bitset32_verbose(u32 *bitmap, unsigned int nbits,
  399. const struct nlattr *attr, struct nlattr **tb,
  400. ethnl_string_array_t names,
  401. struct netlink_ext_ack *extack, bool *mod)
  402. {
  403. u32 *saved_bitmap = NULL;
  404. struct nlattr *bit_attr;
  405. bool no_mask;
  406. int rem;
  407. int ret;
  408. if (tb[ETHTOOL_A_BITSET_VALUE]) {
  409. NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
  410. "value only allowed in compact bitset");
  411. return -EINVAL;
  412. }
  413. if (tb[ETHTOOL_A_BITSET_MASK]) {
  414. NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
  415. "mask only allowed in compact bitset");
  416. return -EINVAL;
  417. }
  418. no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
  419. if (no_mask) {
  420. unsigned int nwords = DIV_ROUND_UP(nbits, 32);
  421. unsigned int nbytes = nwords * sizeof(u32);
  422. bool dummy;
  423. /* The bitmap size is only the size of the map part without
  424. * its mask part.
  425. */
  426. saved_bitmap = kcalloc(nwords, sizeof(u32), GFP_KERNEL);
  427. if (!saved_bitmap)
  428. return -ENOMEM;
  429. memcpy(saved_bitmap, bitmap, nbytes);
  430. ethnl_bitmap32_clear(bitmap, 0, nbits, &dummy);
  431. }
  432. nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
  433. bool old_val, new_val;
  434. unsigned int idx;
  435. if (nla_type(bit_attr) != ETHTOOL_A_BITSET_BITS_BIT) {
  436. NL_SET_ERR_MSG_ATTR(extack, bit_attr,
  437. "only ETHTOOL_A_BITSET_BITS_BIT allowed in ETHTOOL_A_BITSET_BITS");
  438. kfree(saved_bitmap);
  439. return -EINVAL;
  440. }
  441. ret = ethnl_parse_bit(&idx, &new_val, nbits, bit_attr, no_mask,
  442. names, extack);
  443. if (ret < 0) {
  444. kfree(saved_bitmap);
  445. return ret;
  446. }
  447. old_val = bitmap[idx / 32] & ((u32)1 << (idx % 32));
  448. if (new_val != old_val) {
  449. if (new_val)
  450. bitmap[idx / 32] |= ((u32)1 << (idx % 32));
  451. else
  452. bitmap[idx / 32] &= ~((u32)1 << (idx % 32));
  453. if (!no_mask)
  454. *mod = true;
  455. }
  456. }
  457. if (no_mask && !ethnl_bitmap32_equal(saved_bitmap, bitmap, nbits))
  458. *mod = true;
  459. kfree(saved_bitmap);
  460. return 0;
  461. }
  462. static int ethnl_compact_sanity_checks(unsigned int nbits,
  463. const struct nlattr *nest,
  464. struct nlattr **tb,
  465. struct netlink_ext_ack *extack)
  466. {
  467. bool no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
  468. unsigned int attr_nbits, attr_nwords;
  469. const struct nlattr *test_attr;
  470. if (no_mask && tb[ETHTOOL_A_BITSET_MASK]) {
  471. NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
  472. "mask not allowed in list bitset");
  473. return -EINVAL;
  474. }
  475. if (!tb[ETHTOOL_A_BITSET_SIZE]) {
  476. NL_SET_ERR_MSG_ATTR(extack, nest,
  477. "missing size in compact bitset");
  478. return -EINVAL;
  479. }
  480. if (!tb[ETHTOOL_A_BITSET_VALUE]) {
  481. NL_SET_ERR_MSG_ATTR(extack, nest,
  482. "missing value in compact bitset");
  483. return -EINVAL;
  484. }
  485. if (!no_mask && !tb[ETHTOOL_A_BITSET_MASK]) {
  486. NL_SET_ERR_MSG_ATTR(extack, nest,
  487. "missing mask in compact nonlist bitset");
  488. return -EINVAL;
  489. }
  490. attr_nbits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
  491. attr_nwords = DIV_ROUND_UP(attr_nbits, 32);
  492. if (nla_len(tb[ETHTOOL_A_BITSET_VALUE]) != attr_nwords * sizeof(u32)) {
  493. NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
  494. "bitset value length does not match size");
  495. return -EINVAL;
  496. }
  497. if (tb[ETHTOOL_A_BITSET_MASK] &&
  498. nla_len(tb[ETHTOOL_A_BITSET_MASK]) != attr_nwords * sizeof(u32)) {
  499. NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
  500. "bitset mask length does not match size");
  501. return -EINVAL;
  502. }
  503. if (attr_nbits <= nbits)
  504. return 0;
  505. test_attr = no_mask ? tb[ETHTOOL_A_BITSET_VALUE] :
  506. tb[ETHTOOL_A_BITSET_MASK];
  507. if (ethnl_bitmap32_not_zero(nla_data(test_attr), nbits, attr_nbits)) {
  508. NL_SET_ERR_MSG_ATTR(extack, test_attr,
  509. "cannot modify bits past kernel bitset size");
  510. return -EINVAL;
  511. }
  512. return 0;
  513. }
  514. /**
  515. * ethnl_update_bitset32() - Apply a bitset nest to a u32 based bitmap
  516. * @bitmap: bitmap to update
  517. * @nbits: size of the updated bitmap in bits
  518. * @attr: nest attribute to parse and apply
  519. * @names: array of bit names; may be null for compact format
  520. * @extack: extack for error reporting
  521. * @mod: set this to true if bitmap is modified, leave as it is if not
  522. *
  523. * Apply bitset netsted attribute to a bitmap. If the attribute represents
  524. * a bit list, @bitmap is set to its contents; otherwise, bits in mask are
  525. * set to values from value. Bitmaps in the attribute may be longer than
  526. * @nbits but the message must not request modifying any bits past @nbits.
  527. *
  528. * Return: negative error code on failure, 0 on success
  529. */
  530. int ethnl_update_bitset32(u32 *bitmap, unsigned int nbits,
  531. const struct nlattr *attr, ethnl_string_array_t names,
  532. struct netlink_ext_ack *extack, bool *mod)
  533. {
  534. struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
  535. unsigned int change_bits;
  536. bool no_mask;
  537. int ret;
  538. if (!attr)
  539. return 0;
  540. ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, attr,
  541. bitset_policy, extack);
  542. if (ret < 0)
  543. return ret;
  544. if (tb[ETHTOOL_A_BITSET_BITS])
  545. return ethnl_update_bitset32_verbose(bitmap, nbits, attr, tb,
  546. names, extack, mod);
  547. ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack);
  548. if (ret < 0)
  549. return ret;
  550. no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
  551. change_bits = min_t(unsigned int,
  552. nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]), nbits);
  553. ethnl_bitmap32_update(bitmap, change_bits,
  554. nla_data(tb[ETHTOOL_A_BITSET_VALUE]),
  555. no_mask ? NULL :
  556. nla_data(tb[ETHTOOL_A_BITSET_MASK]),
  557. mod);
  558. if (no_mask && change_bits < nbits)
  559. ethnl_bitmap32_clear(bitmap, change_bits, nbits, mod);
  560. return 0;
  561. }
  562. /**
  563. * ethnl_parse_bitset() - Compute effective value and mask from bitset nest
  564. * @val: unsigned long based bitmap to put value into
  565. * @mask: unsigned long based bitmap to put mask into
  566. * @nbits: size of @val and @mask bitmaps
  567. * @attr: nest attribute to parse and apply
  568. * @names: array of bit names; may be null for compact format
  569. * @extack: extack for error reporting
  570. *
  571. * Provide @nbits size long bitmaps for value and mask so that
  572. * x = (val & mask) | (x & ~mask) would modify any @nbits sized bitmap x
  573. * the same way ethnl_update_bitset() with the same bitset attribute would.
  574. *
  575. * Return: negative error code on failure, 0 on success
  576. */
  577. int ethnl_parse_bitset(unsigned long *val, unsigned long *mask,
  578. unsigned int nbits, const struct nlattr *attr,
  579. ethnl_string_array_t names,
  580. struct netlink_ext_ack *extack)
  581. {
  582. struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
  583. const struct nlattr *bit_attr;
  584. bool no_mask;
  585. int rem;
  586. int ret;
  587. if (!attr)
  588. return 0;
  589. ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, attr,
  590. bitset_policy, extack);
  591. if (ret < 0)
  592. return ret;
  593. no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
  594. if (!tb[ETHTOOL_A_BITSET_BITS]) {
  595. unsigned int change_bits;
  596. ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack);
  597. if (ret < 0)
  598. return ret;
  599. change_bits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
  600. if (change_bits > nbits)
  601. change_bits = nbits;
  602. bitmap_from_arr32(val, nla_data(tb[ETHTOOL_A_BITSET_VALUE]),
  603. change_bits);
  604. if (change_bits < nbits)
  605. bitmap_clear(val, change_bits, nbits - change_bits);
  606. if (no_mask) {
  607. bitmap_fill(mask, nbits);
  608. } else {
  609. bitmap_from_arr32(mask,
  610. nla_data(tb[ETHTOOL_A_BITSET_MASK]),
  611. change_bits);
  612. if (change_bits < nbits)
  613. bitmap_clear(mask, change_bits,
  614. nbits - change_bits);
  615. }
  616. return 0;
  617. }
  618. if (tb[ETHTOOL_A_BITSET_VALUE]) {
  619. NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
  620. "value only allowed in compact bitset");
  621. return -EINVAL;
  622. }
  623. if (tb[ETHTOOL_A_BITSET_MASK]) {
  624. NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
  625. "mask only allowed in compact bitset");
  626. return -EINVAL;
  627. }
  628. bitmap_zero(val, nbits);
  629. if (no_mask)
  630. bitmap_fill(mask, nbits);
  631. else
  632. bitmap_zero(mask, nbits);
  633. nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
  634. unsigned int idx;
  635. bool bit_val;
  636. ret = ethnl_parse_bit(&idx, &bit_val, nbits, bit_attr, no_mask,
  637. names, extack);
  638. if (ret < 0)
  639. return ret;
  640. if (bit_val)
  641. __set_bit(idx, val);
  642. if (!no_mask)
  643. __set_bit(idx, mask);
  644. }
  645. return 0;
  646. }
  647. #if BITS_PER_LONG == 64 && defined(__BIG_ENDIAN)
  648. /* 64-bit big endian architectures are the only case when u32 based bitmaps
  649. * and unsigned long based bitmaps have different memory layout so that we
  650. * cannot simply cast the latter to the former and need actual wrappers
  651. * converting the latter to the former.
  652. *
  653. * To reduce the number of slab allocations, the wrappers use fixed size local
  654. * variables for bitmaps up to ETHNL_SMALL_BITMAP_BITS bits which is the
  655. * majority of bitmaps used by ethtool.
  656. */
  657. #define ETHNL_SMALL_BITMAP_BITS 128
  658. #define ETHNL_SMALL_BITMAP_WORDS DIV_ROUND_UP(ETHNL_SMALL_BITMAP_BITS, 32)
  659. int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
  660. unsigned int nbits, ethnl_string_array_t names,
  661. bool compact)
  662. {
  663. u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS];
  664. u32 small_val32[ETHNL_SMALL_BITMAP_WORDS];
  665. u32 *mask32;
  666. u32 *val32;
  667. int ret;
  668. if (nbits > ETHNL_SMALL_BITMAP_BITS) {
  669. unsigned int nwords = DIV_ROUND_UP(nbits, 32);
  670. val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL);
  671. if (!val32)
  672. return -ENOMEM;
  673. mask32 = val32 + nwords;
  674. } else {
  675. val32 = small_val32;
  676. mask32 = small_mask32;
  677. }
  678. bitmap_to_arr32(val32, val, nbits);
  679. if (mask)
  680. bitmap_to_arr32(mask32, mask, nbits);
  681. else
  682. mask32 = NULL;
  683. ret = ethnl_bitset32_size(val32, mask32, nbits, names, compact);
  684. if (nbits > ETHNL_SMALL_BITMAP_BITS)
  685. kfree(val32);
  686. return ret;
  687. }
  688. int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
  689. const unsigned long *val, const unsigned long *mask,
  690. unsigned int nbits, ethnl_string_array_t names,
  691. bool compact)
  692. {
  693. u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS];
  694. u32 small_val32[ETHNL_SMALL_BITMAP_WORDS];
  695. u32 *mask32;
  696. u32 *val32;
  697. int ret;
  698. if (nbits > ETHNL_SMALL_BITMAP_BITS) {
  699. unsigned int nwords = DIV_ROUND_UP(nbits, 32);
  700. val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL);
  701. if (!val32)
  702. return -ENOMEM;
  703. mask32 = val32 + nwords;
  704. } else {
  705. val32 = small_val32;
  706. mask32 = small_mask32;
  707. }
  708. bitmap_to_arr32(val32, val, nbits);
  709. if (mask)
  710. bitmap_to_arr32(mask32, mask, nbits);
  711. else
  712. mask32 = NULL;
  713. ret = ethnl_put_bitset32(skb, attrtype, val32, mask32, nbits, names,
  714. compact);
  715. if (nbits > ETHNL_SMALL_BITMAP_BITS)
  716. kfree(val32);
  717. return ret;
  718. }
  719. int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
  720. const struct nlattr *attr, ethnl_string_array_t names,
  721. struct netlink_ext_ack *extack, bool *mod)
  722. {
  723. u32 small_bitmap32[ETHNL_SMALL_BITMAP_WORDS];
  724. u32 *bitmap32 = small_bitmap32;
  725. bool u32_mod = false;
  726. int ret;
  727. if (nbits > ETHNL_SMALL_BITMAP_BITS) {
  728. unsigned int dst_words = DIV_ROUND_UP(nbits, 32);
  729. bitmap32 = kmalloc_array(dst_words, sizeof(u32), GFP_KERNEL);
  730. if (!bitmap32)
  731. return -ENOMEM;
  732. }
  733. bitmap_to_arr32(bitmap32, bitmap, nbits);
  734. ret = ethnl_update_bitset32(bitmap32, nbits, attr, names, extack,
  735. &u32_mod);
  736. if (u32_mod) {
  737. bitmap_from_arr32(bitmap, bitmap32, nbits);
  738. *mod = true;
  739. }
  740. if (nbits > ETHNL_SMALL_BITMAP_BITS)
  741. kfree(bitmap32);
  742. return ret;
  743. }
  744. #else
  745. /* On little endian 64-bit and all 32-bit architectures, an unsigned long
  746. * based bitmap can be interpreted as u32 based one using a simple cast.
  747. */
  748. int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
  749. unsigned int nbits, ethnl_string_array_t names,
  750. bool compact)
  751. {
  752. return ethnl_bitset32_size((const u32 *)val, (const u32 *)mask, nbits,
  753. names, compact);
  754. }
  755. int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
  756. const unsigned long *val, const unsigned long *mask,
  757. unsigned int nbits, ethnl_string_array_t names,
  758. bool compact)
  759. {
  760. return ethnl_put_bitset32(skb, attrtype, (const u32 *)val,
  761. (const u32 *)mask, nbits, names, compact);
  762. }
  763. int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
  764. const struct nlattr *attr, ethnl_string_array_t names,
  765. struct netlink_ext_ack *extack, bool *mod)
  766. {
  767. return ethnl_update_bitset32((u32 *)bitmap, nbits, attr, names, extack,
  768. mod);
  769. }
  770. #endif /* BITS_PER_LONG == 64 && defined(__BIG_ENDIAN) */