remote.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2019-2020 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <ell/ell.h>
  14. #include "src/shared/shell.h"
  15. #include "src/shared/util.h"
  16. #include "mesh/mesh-defs.h"
  17. #include "tools/mesh/keys.h"
  18. #include "tools/mesh/mesh-db.h"
  19. #include "tools/mesh/remote.h"
  20. #include "tools/mesh/util.h"
  21. #define abs_diff(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
  22. struct remote_key {
  23. uint16_t idx;
  24. bool updated;
  25. };
  26. struct remote_node {
  27. uint16_t unicast;
  28. struct l_queue *net_keys;
  29. struct l_queue *app_keys;
  30. struct l_queue **els;
  31. bool comp;
  32. uint8_t uuid[16];
  33. uint8_t num_ele;
  34. };
  35. struct rejected_addr {
  36. uint32_t iv_index;
  37. uint16_t unicast;
  38. };
  39. static struct l_queue *nodes;
  40. static struct l_queue *reject_list;
  41. static int compare_mod_id(const void *a, const void *b, void *user_data)
  42. {
  43. uint32_t id1 = L_PTR_TO_UINT(a);
  44. uint32_t id2 = L_PTR_TO_UINT(b);
  45. if (id1 >= VENDOR_ID_MASK)
  46. id1 &= ~VENDOR_ID_MASK;
  47. if (id2 >= VENDOR_ID_MASK)
  48. id2 &= ~VENDOR_ID_MASK;
  49. if (id1 < id2)
  50. return -1;
  51. if (id1 > id2)
  52. return 1;
  53. return 0;
  54. }
  55. static int compare_unicast(const void *a, const void *b, void *user_data)
  56. {
  57. const struct remote_node *a_rmt = a;
  58. const struct remote_node *b_rmt = b;
  59. if (a_rmt->unicast < b_rmt->unicast)
  60. return -1;
  61. if (a_rmt->unicast > b_rmt->unicast)
  62. return 1;
  63. return 0;
  64. }
  65. static bool match_node_addr(const void *a, const void *b)
  66. {
  67. const struct remote_node *rmt = a;
  68. uint16_t addr = L_PTR_TO_UINT(b);
  69. if (addr >= rmt->unicast &&
  70. addr <= (rmt->unicast + rmt->num_ele - 1))
  71. return true;
  72. return false;
  73. }
  74. static bool match_key(const void *a, const void *b)
  75. {
  76. const struct remote_key *key = a;
  77. uint16_t idx = L_PTR_TO_UINT(b);
  78. return (key->idx == idx);
  79. }
  80. static bool match_bound_key(const void *a, const void *b)
  81. {
  82. const struct remote_key *app_key = a;
  83. uint16_t net_idx = L_PTR_TO_UINT(b);
  84. return (net_idx == keys_get_bound_key(app_key->idx));
  85. }
  86. uint8_t remote_del_node(uint16_t unicast)
  87. {
  88. struct remote_node *rmt;
  89. uint8_t num_ele, i;
  90. uint32_t iv_index = mesh_db_get_iv_index();
  91. rmt = l_queue_remove_if(nodes, match_node_addr, L_UINT_TO_PTR(unicast));
  92. if (!rmt)
  93. return 0;
  94. num_ele = rmt->num_ele;
  95. for (i = 0; i < num_ele; ++i) {
  96. l_queue_destroy(rmt->els[i], NULL);
  97. remote_add_rejected_address(unicast + i, iv_index, true);
  98. }
  99. l_free(rmt->els);
  100. l_queue_destroy(rmt->net_keys, NULL);
  101. l_queue_destroy(rmt->app_keys, NULL);
  102. l_free(rmt);
  103. mesh_db_del_node(unicast);
  104. return num_ele;
  105. }
  106. bool remote_add_node(const uint8_t uuid[16], uint16_t unicast,
  107. uint8_t ele_cnt, uint16_t net_idx)
  108. {
  109. struct remote_node *rmt;
  110. struct remote_key *key;
  111. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(unicast));
  112. if (rmt)
  113. return false;
  114. rmt = l_new(struct remote_node, 1);
  115. memcpy(rmt->uuid, uuid, 16);
  116. rmt->unicast = unicast;
  117. rmt->num_ele = ele_cnt;
  118. rmt->net_keys = l_queue_new();
  119. key = l_new(struct remote_key, 1);
  120. key->idx = net_idx;
  121. l_queue_push_tail(rmt->net_keys, key);
  122. rmt->els = l_new(struct l_queue *, ele_cnt);
  123. if (!nodes)
  124. nodes = l_queue_new();
  125. l_queue_insert(nodes, rmt, compare_unicast, NULL);
  126. return true;
  127. }
  128. bool remote_set_model(uint16_t unicast, uint8_t ele_idx, uint32_t mod_id,
  129. bool vendor)
  130. {
  131. struct remote_node *rmt;
  132. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(unicast));
  133. if (!rmt)
  134. return false;
  135. if (ele_idx >= rmt->num_ele)
  136. return false;
  137. if (!rmt->els[ele_idx])
  138. rmt->els[ele_idx] = l_queue_new();
  139. if (!vendor)
  140. mod_id = VENDOR_ID_MASK | mod_id;
  141. l_queue_insert(rmt->els[ele_idx], L_UINT_TO_PTR(mod_id),
  142. compare_mod_id, NULL);
  143. return true;
  144. }
  145. void remote_set_composition(uint16_t addr, bool comp)
  146. {
  147. struct remote_node *rmt;
  148. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  149. if (!rmt)
  150. return;
  151. rmt->comp = comp;
  152. }
  153. bool remote_has_composition(uint16_t addr)
  154. {
  155. struct remote_node *rmt;
  156. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  157. if (!rmt)
  158. return false;
  159. return rmt->comp;
  160. }
  161. bool remote_add_net_key(uint16_t addr, uint16_t net_idx, bool save)
  162. {
  163. struct remote_node *rmt;
  164. struct remote_key *key;
  165. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  166. if (!rmt)
  167. return false;
  168. if (l_queue_find(rmt->net_keys, match_key, L_UINT_TO_PTR(net_idx)))
  169. return true;
  170. key = l_new(struct remote_key, 1);
  171. key->idx = net_idx;
  172. l_queue_push_tail(rmt->net_keys, key);
  173. if (save)
  174. return mesh_db_node_add_net_key(addr, net_idx);
  175. else
  176. return true;
  177. }
  178. bool remote_del_net_key(uint16_t addr, uint16_t net_idx)
  179. {
  180. struct remote_node *rmt;
  181. struct remote_key *key;
  182. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  183. if (!rmt)
  184. return false;
  185. key = l_queue_remove_if(rmt->net_keys, match_key,
  186. L_UINT_TO_PTR(net_idx));
  187. if (!key)
  188. return false;
  189. mesh_db_node_del_net_key(addr, net_idx);
  190. l_free(key);
  191. key = l_queue_remove_if(rmt->app_keys, match_bound_key,
  192. L_UINT_TO_PTR(net_idx));
  193. while (key) {
  194. mesh_db_node_del_app_key(rmt->unicast, key->idx);
  195. l_free(key);
  196. key = l_queue_remove_if(rmt->app_keys, match_bound_key,
  197. L_UINT_TO_PTR(net_idx));
  198. }
  199. return true;
  200. }
  201. bool remote_update_net_key(uint16_t addr, uint16_t net_idx, bool update,
  202. bool save)
  203. {
  204. struct remote_node *rmt;
  205. struct remote_key *key;
  206. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  207. if (!rmt)
  208. return false;
  209. key = l_queue_find(rmt->net_keys, match_key,
  210. L_UINT_TO_PTR(net_idx));
  211. if (!key)
  212. return false;
  213. key->updated = update;
  214. if (save)
  215. return mesh_db_node_update_net_key(addr, net_idx, update);
  216. else
  217. return true;
  218. }
  219. bool remote_add_app_key(uint16_t addr, uint16_t app_idx, bool save)
  220. {
  221. struct remote_node *rmt;
  222. struct remote_key *key;
  223. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  224. if (!rmt)
  225. return false;
  226. if (!rmt->app_keys)
  227. rmt->app_keys = l_queue_new();
  228. if (l_queue_find(rmt->app_keys, match_key, L_UINT_TO_PTR(app_idx)))
  229. return true;
  230. key = l_new(struct remote_key, 1);
  231. key->idx = app_idx;
  232. l_queue_push_tail(rmt->app_keys, key);
  233. if (save)
  234. return mesh_db_node_add_app_key(addr, app_idx);
  235. else
  236. return true;
  237. }
  238. bool remote_del_app_key(uint16_t addr, uint16_t app_idx)
  239. {
  240. struct remote_node *rmt;
  241. struct remote_key *key;
  242. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  243. if (!rmt)
  244. return false;
  245. key = l_queue_remove_if(rmt->app_keys, match_key,
  246. L_UINT_TO_PTR(app_idx));
  247. l_free(key);
  248. return mesh_db_node_del_app_key(addr, app_idx);
  249. }
  250. bool remote_update_app_key(uint16_t addr, uint16_t app_idx, bool update,
  251. bool save)
  252. {
  253. struct remote_node *rmt;
  254. struct remote_key *key;
  255. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  256. if (!rmt)
  257. return false;
  258. key = l_queue_find(rmt->app_keys, match_key,
  259. L_UINT_TO_PTR(app_idx));
  260. if (!key)
  261. return false;
  262. key->updated = update;
  263. if (save)
  264. return mesh_db_node_update_app_key(addr, app_idx, update);
  265. else
  266. return true;
  267. }
  268. bool remote_finish_key_refresh(uint16_t addr, uint16_t net_idx)
  269. {
  270. struct remote_node *rmt;
  271. struct remote_key *key;
  272. const struct l_queue_entry *l;
  273. bool res = true;
  274. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  275. if (!rmt)
  276. return false;
  277. if (!remote_update_net_key(addr, net_idx, false, true))
  278. return false;
  279. l = l_queue_get_entries(rmt->app_keys);
  280. for (; l; l = l->next) {
  281. key = l->data;
  282. if (net_idx != keys_get_bound_key(key->idx))
  283. continue;
  284. key->updated = false;
  285. res &= mesh_db_node_update_app_key(addr, key->idx, false);
  286. }
  287. return res;
  288. }
  289. uint16_t remote_get_subnet_idx(uint16_t addr)
  290. {
  291. struct remote_node *rmt;
  292. struct remote_key *key;
  293. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  294. if (!rmt || l_queue_isempty(rmt->net_keys))
  295. return NET_IDX_INVALID;
  296. key = l_queue_peek_head(rmt->net_keys);
  297. return key->idx;
  298. }
  299. static void print_key(void *data, void *user_data)
  300. {
  301. struct remote_key *key = data;
  302. bt_shell_printf("%u (0x%3.3x) %s, ", key->idx, key->idx,
  303. key->updated ? ", updated":"");
  304. }
  305. static void print_model(void *model, void *user_data)
  306. {
  307. uint32_t mod_id = L_PTR_TO_UINT(model);
  308. if (mod_id >= VENDOR_ID_MASK) {
  309. mod_id &= ~VENDOR_ID_MASK;
  310. bt_shell_printf("\t\t\t" COLOR_GREEN "SIG model: %4.4x \"%s\"\n"
  311. COLOR_OFF, mod_id, sig_model_string(mod_id));
  312. return;
  313. }
  314. bt_shell_printf("\t\t\t" COLOR_GREEN "Vendor model: %8.8x\n"
  315. COLOR_OFF, mod_id);
  316. }
  317. static void print_element(struct l_queue *mods, int idx)
  318. {
  319. if (!mods)
  320. return;
  321. bt_shell_printf("\t\t" COLOR_GREEN "element %u:\n" COLOR_OFF, idx);
  322. l_queue_foreach(mods, print_model, NULL);
  323. }
  324. static void print_node(void *rmt, void *user_data)
  325. {
  326. struct remote_node *node = rmt;
  327. int i;
  328. char *str;
  329. bt_shell_printf(COLOR_YELLOW "Mesh node:\n" COLOR_OFF);
  330. str = l_util_hexstring_upper(node->uuid, 16);
  331. bt_shell_printf("\t" COLOR_GREEN "UUID = %s\n" COLOR_OFF, str);
  332. l_free(str);
  333. bt_shell_printf("\t" COLOR_GREEN "primary = %4.4x\n" COLOR_OFF,
  334. node->unicast);
  335. bt_shell_printf("\t" COLOR_GREEN "net_keys = ");
  336. l_queue_foreach(node->net_keys, print_key, NULL);
  337. bt_shell_printf("\n" COLOR_OFF);
  338. if (node->app_keys && !l_queue_isempty(node->app_keys)) {
  339. bt_shell_printf("\t" COLOR_GREEN "app_keys = ");
  340. l_queue_foreach(node->app_keys, print_key, NULL);
  341. bt_shell_printf("\n" COLOR_OFF);
  342. }
  343. bt_shell_printf("\t" COLOR_GREEN "elements (%u):\n" COLOR_OFF,
  344. node->num_ele);
  345. for (i = 0; i < node->num_ele; ++i)
  346. print_element(node->els[i], i);
  347. }
  348. static bool match_rejected_addr(const void *a, const void *b)
  349. {
  350. const struct rejected_addr *addr = a;
  351. uint16_t unicast = L_PTR_TO_UINT(b);
  352. return addr->unicast == unicast;
  353. }
  354. static uint16_t get_next_addr(uint16_t high, uint16_t addr,
  355. uint8_t ele_cnt)
  356. {
  357. while ((addr + ele_cnt - 1) <= high) {
  358. int i = 0;
  359. for (i = 0; i < ele_cnt; i++) {
  360. struct rejected_addr *reject;
  361. reject = l_queue_find(reject_list, match_rejected_addr,
  362. L_UINT_TO_PTR(addr + i));
  363. if (!reject)
  364. break;
  365. }
  366. addr += i;
  367. if ((i != ele_cnt) && (addr + ele_cnt - 1) <= high)
  368. return addr;
  369. }
  370. return 0;
  371. }
  372. static bool check_iv_index(const void *a, const void *b)
  373. {
  374. const struct rejected_addr *reject = a;
  375. uint32_t iv_index = L_PTR_TO_UINT(b);
  376. return (abs_diff(iv_index, reject->iv_index) > 2);
  377. }
  378. void remote_print_node(uint16_t addr)
  379. {
  380. struct remote_node *rmt;
  381. if (!nodes)
  382. return;
  383. rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
  384. if (!rmt)
  385. return;
  386. print_node(rmt, NULL);
  387. }
  388. void remote_print_all(void)
  389. {
  390. if (!nodes)
  391. return;
  392. l_queue_foreach(nodes, print_node, NULL);
  393. }
  394. uint16_t remote_get_next_unicast(uint16_t low, uint16_t high, uint8_t ele_cnt)
  395. {
  396. struct remote_node *rmt;
  397. const struct l_queue_entry *l;
  398. uint16_t addr;
  399. /* Note: the address space includes both low and high terminal values */
  400. if (ele_cnt > (high - low + 1))
  401. return 0;
  402. if (!nodes || l_queue_isempty(nodes))
  403. return low;
  404. addr = low;
  405. l = l_queue_get_entries(nodes);
  406. /* Cycle through the sorted (by unicast) node list */
  407. for (; l; l = l->next) {
  408. rmt = l->data;
  409. if (rmt->unicast < low)
  410. continue;
  411. if (rmt->unicast >= (addr + ele_cnt)) {
  412. uint16_t unicast;
  413. unicast = get_next_addr(rmt->unicast - 1, addr,
  414. ele_cnt);
  415. if (unicast)
  416. return unicast;
  417. }
  418. addr = rmt->unicast + rmt->num_ele;
  419. }
  420. addr = get_next_addr(high, addr, ele_cnt);
  421. return addr;
  422. }
  423. void remote_add_rejected_address(uint16_t addr, uint32_t iv_index, bool save)
  424. {
  425. struct rejected_addr *reject;
  426. if (!reject_list)
  427. reject_list = l_queue_new();
  428. reject = l_new(struct rejected_addr, 1);
  429. reject->unicast = addr;
  430. reject->iv_index = iv_index;
  431. l_queue_push_tail(reject_list, reject);
  432. if (save)
  433. mesh_db_add_rejected_addr(addr, iv_index);
  434. }
  435. void remote_clear_rejected_addresses(uint32_t iv_index)
  436. {
  437. struct rejected_addr *reject;
  438. reject = l_queue_remove_if(reject_list, check_iv_index,
  439. L_UINT_TO_PTR(iv_index));
  440. while (reject) {
  441. l_free(reject);
  442. reject = l_queue_remove_if(reject_list, check_iv_index,
  443. L_UINT_TO_PTR(iv_index));
  444. }
  445. mesh_db_clear_rejected(iv_index);
  446. }