beacon.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /* Bluetooth Mesh */
  2. /*
  3. * SPDX-FileCopyrightText: 2017 Intel Corporation
  4. * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. */
  8. #include <string.h>
  9. #include <errno.h>
  10. #include "adv.h"
  11. #include "scan.h"
  12. #include "mesh.h"
  13. #include "crypto.h"
  14. #include "beacon.h"
  15. #include "access.h"
  16. #include "foundation.h"
  17. #include "proxy_client.h"
  18. #include "mesh/main.h"
  19. #include "prov_common.h"
  20. #include "prov_node.h"
  21. #include "prov_pvnr.h"
  22. #include "pvnr_mgmt.h"
  23. #include "mesh/common.h"
  24. #include "mesh_v1.1/utils.h"
  25. #if defined(CONFIG_BLE_MESH_UNPROVISIONED_BEACON_INTERVAL)
  26. #define UNPROV_BEACON_INTERVAL K_SECONDS(CONFIG_BLE_MESH_UNPROVISIONED_BEACON_INTERVAL)
  27. #else
  28. #define UNPROV_BEACON_INTERVAL K_SECONDS(5)
  29. #endif
  30. #define SECURE_BEACON_INTERVAL K_SECONDS(10)
  31. /* 3 transmissions, 20ms interval */
  32. #define UNPROV_XMIT BLE_MESH_TRANSMIT(2, 20)
  33. /* 1 transmission, 20ms interval */
  34. #define SNB_XMIT BLE_MESH_TRANSMIT(0, 20)
  35. /* For a device, using the snb_timer when sending Unprovisioned Device Beacon;
  36. * For a node, using the snb_timer when sending Secure Network Beacon.
  37. */
  38. static struct k_delayed_work snb_timer;
  39. struct bt_mesh_subnet *cache_check(uint8_t data[21], bool private_beacon)
  40. {
  41. size_t subnet_size = 0U;
  42. uint8_t *cache = NULL;
  43. int i = 0;
  44. subnet_size = bt_mesh_rx_netkey_size();
  45. for (i = 0; i < subnet_size; i++) {
  46. struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i);
  47. if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) {
  48. continue;
  49. }
  50. #if CONFIG_BLE_MESH_PRIVATE_BEACON
  51. cache = private_beacon ? sub->mpb_cache : sub->snb_cache;
  52. #else
  53. cache = sub->snb_cache;
  54. #endif
  55. if (!memcmp(cache, data, 21)) {
  56. return sub;
  57. }
  58. }
  59. return NULL;
  60. }
  61. void cache_add(uint8_t data[21], struct bt_mesh_subnet *sub, bool private_beacon)
  62. {
  63. #if CONFIG_BLE_MESH_PRIVATE_BEACON
  64. if (private_beacon) {
  65. memcpy(sub->mpb_cache, data, 21);
  66. } else
  67. #endif
  68. {
  69. memcpy(sub->snb_cache, data, 21);
  70. }
  71. }
  72. static void secure_beacon_complete(int err, void *user_data)
  73. {
  74. struct bt_mesh_subnet *sub = NULL;
  75. uint16_t net_idx = BLE_MESH_KEY_UNUSED;
  76. BT_DBG("err %d", err);
  77. net_idx = (uint16_t)NET_IDX_GET(user_data);
  78. /* For node, directly updating the "beacon_sent" timestamp is fine,
  79. * since the subnet is pre-allocated.
  80. * For Provisioner, before updating the "beacon_sent" timestamp, we
  81. * need to make sure that the subnet still exists, because there is
  82. * a chance that the subnet is removed just before the completion of
  83. * sending the Secure Network Beacon.
  84. */
  85. sub = bt_mesh_subnet_get(net_idx);
  86. if (sub) {
  87. sub->snb_sent = k_uptime_get_32();
  88. }
  89. }
  90. void bt_mesh_secure_beacon_create(struct bt_mesh_subnet *sub,
  91. struct net_buf_simple *buf)
  92. {
  93. uint8_t flags = bt_mesh_net_flags(sub);
  94. struct bt_mesh_subnet_keys *keys = NULL;
  95. net_buf_simple_add_u8(buf, BEACON_TYPE_SECURE);
  96. if (sub->kr_flag) {
  97. keys = &sub->keys[1];
  98. } else {
  99. keys = &sub->keys[0];
  100. }
  101. net_buf_simple_add_u8(buf, flags);
  102. /* Network ID */
  103. net_buf_simple_add_mem(buf, keys->net_id, 8);
  104. /* IV Index */
  105. net_buf_simple_add_be32(buf, bt_mesh.iv_index);
  106. net_buf_simple_add_mem(buf, sub->auth, 8);
  107. BT_DBG("SNB: net_idx 0x%03x iv_index 0x%08x flags 0x%02x",
  108. sub->net_idx, bt_mesh.iv_index, flags);
  109. BT_DBG("SNB: NetID %s Auth %s", bt_hex(keys->net_id, 8),
  110. bt_hex(sub->auth, 8));
  111. }
  112. static int secure_beacon_send(void)
  113. {
  114. static const struct bt_mesh_send_cb send_cb = {
  115. .end = secure_beacon_complete,
  116. };
  117. uint32_t now = k_uptime_get_32();
  118. size_t subnet_size = 0U;
  119. int i = 0;
  120. subnet_size = bt_mesh_rx_netkey_size();
  121. for (i = 0; i < subnet_size; i++) {
  122. struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i);
  123. struct net_buf *buf;
  124. uint32_t time_diff;
  125. if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) {
  126. continue;
  127. }
  128. time_diff = now - sub->snb_sent;
  129. if (time_diff < K_SECONDS(600) &&
  130. time_diff < BEACON_THRESHOLD(sub->snb_last)) {
  131. continue;
  132. }
  133. /* If a node enables the Proxy Client functionality, and it
  134. * succeeds to send Secure Network Beacon with GATT bearer,
  135. * here we will continue to send Secure Network Beacon of
  136. * other subnets.
  137. */
  138. #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT
  139. if (bt_mesh_proxy_client_beacon_send(sub, false)) {
  140. continue;
  141. }
  142. #endif
  143. buf = bt_mesh_adv_create(BLE_MESH_ADV_BEACON, K_NO_WAIT);
  144. if (!buf) {
  145. BT_ERR("Out of secure beacon buffer");
  146. return -ENOBUFS;
  147. }
  148. bt_mesh_secure_beacon_create(sub, &buf->b);
  149. /* Care should be taken here. Previously the user_data is the
  150. * pointer of a subnet. When the device is a Provisioner, its
  151. * subnet is created dynamically. If the corresponding subnet
  152. * is removed right after the Secure Network Beacon is sent,
  153. * update its "snb_sent" timestamp in secure_beacon_complete()
  154. * will cause exception.
  155. * Here we use the "net_idx" of the subnet instead. And in the
  156. * secure_beacon_complete(), we will try to get the subnet before
  157. * updating its "snb_sent" timestamp.
  158. */
  159. bt_mesh_adv_send(buf, SNB_XMIT, &send_cb, NET_IDX_SET(sub->net_idx));
  160. net_buf_unref(buf);
  161. }
  162. return 0;
  163. }
  164. #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_ADV)
  165. static int unprovisioned_beacon_send(void)
  166. {
  167. uint8_t uri_hash[16] = {0};
  168. struct net_buf *buf = NULL;
  169. uint16_t oob_info = 0U;
  170. if (bt_mesh_prov_get() == NULL) {
  171. BT_ERR("No provisioning context provided");
  172. return -EINVAL;
  173. }
  174. buf = bt_mesh_adv_create(BLE_MESH_ADV_BEACON, K_NO_WAIT);
  175. if (!buf) {
  176. BT_ERR("Out of unprov beacon buffer");
  177. return -ENOBUFS;
  178. }
  179. net_buf_add_u8(buf, BEACON_TYPE_UNPROVISIONED);
  180. net_buf_add_mem(buf, bt_mesh_prov_get()->uuid, 16);
  181. if (bt_mesh_prov_get()->uri &&
  182. bt_mesh_s1(bt_mesh_prov_get()->uri, uri_hash) == 0) {
  183. oob_info = bt_mesh_prov_get()->oob_info | BLE_MESH_PROV_OOB_URI;
  184. } else {
  185. oob_info = bt_mesh_prov_get()->oob_info;
  186. }
  187. net_buf_add_be16(buf, oob_info);
  188. net_buf_add_mem(buf, uri_hash, 4);
  189. bt_mesh_adv_send(buf, UNPROV_XMIT, NULL, NULL);
  190. net_buf_unref(buf);
  191. if (bt_mesh_prov_get()->uri) {
  192. size_t len = 0;
  193. buf = bt_mesh_adv_create(BLE_MESH_ADV_URI, K_NO_WAIT);
  194. if (!buf) {
  195. BT_ERR("Unable to allocate URI buffer");
  196. return -ENOBUFS;
  197. }
  198. len = strlen(bt_mesh_prov_get()->uri);
  199. if (net_buf_tailroom(buf) < len) {
  200. BT_WARN("Too long URI to fit advertising data");
  201. } else {
  202. net_buf_add_mem(buf, bt_mesh_prov_get()->uri, len);
  203. bt_mesh_adv_send(buf, UNPROV_XMIT, NULL, NULL);
  204. }
  205. net_buf_unref(buf);
  206. }
  207. return 0;
  208. }
  209. #else /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_ADV) */
  210. static int unprovisioned_beacon_send(void)
  211. {
  212. return 0;
  213. }
  214. #endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_ADV) */
  215. void update_beacon_observation(bool private_beacon)
  216. {
  217. static bool snb_first_half;
  218. size_t subnet_size = 0U;
  219. int i = 0;
  220. /* Observation period is 20 seconds, whereas the beacon timer
  221. * runs every 10 seconds. We process what's happened during
  222. * the window only after the second half.
  223. */
  224. #if CONFIG_BLE_MESH_PRB_SRV
  225. static bool mpb_first_half;
  226. if (private_beacon) {
  227. mpb_first_half = !mpb_first_half;
  228. if (mpb_first_half) {
  229. return;
  230. }
  231. } else
  232. #endif
  233. {
  234. snb_first_half = !snb_first_half;
  235. if (snb_first_half) {
  236. return;
  237. }
  238. }
  239. subnet_size = bt_mesh_rx_netkey_size();
  240. for (i = 0; i < subnet_size; i++) {
  241. struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i);
  242. if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) {
  243. continue;
  244. }
  245. #if CONFIG_BLE_MESH_PRB_SRV
  246. if (private_beacon) {
  247. sub->mpb_last = sub->mpb_cur;
  248. sub->mpb_cur = 0U;
  249. } else
  250. #endif
  251. {
  252. sub->snb_last = sub->snb_cur;
  253. sub->snb_cur = 0U;
  254. }
  255. }
  256. }
  257. static bool ready_to_send(void)
  258. {
  259. if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) {
  260. return true;
  261. }
  262. return false;
  263. }
  264. static void secure_beacon_send_timeout(struct k_work *work)
  265. {
  266. /* Don't send anything if we have an active provisioning link */
  267. if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() &&
  268. IS_ENABLED(CONFIG_BLE_MESH_PROV) && bt_mesh_prov_active()) {
  269. k_delayed_work_submit(&snb_timer, UNPROV_BEACON_INTERVAL);
  270. return;
  271. }
  272. if (ready_to_send()) {
  273. update_beacon_observation(false);
  274. secure_beacon_send();
  275. /* Only resubmit if beaconing is still enabled */
  276. if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED ||
  277. bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR)) {
  278. k_delayed_work_submit(&snb_timer, SECURE_BEACON_INTERVAL);
  279. }
  280. } else {
  281. if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) {
  282. unprovisioned_beacon_send();
  283. k_delayed_work_submit(&snb_timer, UNPROV_BEACON_INTERVAL);
  284. }
  285. }
  286. }
  287. static void secure_beacon_recv(struct net_buf_simple *buf)
  288. {
  289. uint8_t *data = NULL, *net_id = NULL, *auth = NULL;
  290. struct bt_mesh_subnet *sub = NULL;
  291. uint32_t iv_index = 0U;
  292. bool kr_change = false;
  293. bool iv_change = false;
  294. bool new_key = false;
  295. uint8_t flags = 0U;
  296. if (buf->len != 21) {
  297. BT_ERR("Malformed secure beacon (len %u)", buf->len);
  298. return;
  299. }
  300. sub = cache_check(buf->data, false);
  301. if (sub) {
  302. /* We've seen this beacon before - just update the stats */
  303. goto update_stats;
  304. }
  305. /* So we can add to the cache if auth matches */
  306. data = buf->data;
  307. flags = net_buf_simple_pull_u8(buf);
  308. net_id = net_buf_simple_pull_mem(buf, 8);
  309. iv_index = net_buf_simple_pull_be32(buf);
  310. auth = buf->data;
  311. BT_DBG("flags 0x%02x id %s iv_index 0x%08x",
  312. flags, bt_hex(net_id, 8), iv_index);
  313. sub = bt_mesh_subnet_find_with_snb(net_id, flags, iv_index, auth, &new_key);
  314. if (!sub) {
  315. BT_DBG("No subnet that matched secure beacon");
  316. return;
  317. }
  318. if (sub->kr_phase == BLE_MESH_KR_PHASE_2 && !new_key) {
  319. BT_WARN("Ignoring Phase 2 KR Update secured using old key");
  320. return;
  321. }
  322. cache_add(data, sub, false);
  323. /* If we have NetKey0 accept initiation only from it.
  324. *
  325. * Spec v1.1, Section 3.8.4:
  326. * If a device on a primary subnet receives an update on
  327. * the primary subnet, it shall propagate the IV update
  328. * to all other subnets. If a device on a primary subnet
  329. * receives an IV update on any other subnet, the update
  330. * shall be ignored.
  331. */
  332. if (bt_mesh_primary_subnet_exist() &&
  333. sub->net_idx != BLE_MESH_KEY_PRIMARY &&
  334. BLE_MESH_IV_UPDATE(flags) &&
  335. !BLE_MESH_KEY_REFRESH(flags)) {
  336. BT_WARN("Ignoring secure beacon on non-primary subnet");
  337. goto update_stats;
  338. }
  339. BT_DBG("SNB: net_idx 0x%03x iv_index 0x%08x current iv_index 0x%08x",
  340. sub->net_idx, iv_index, bt_mesh.iv_index);
  341. if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR) &&
  342. (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) ==
  343. BLE_MESH_IV_UPDATE(flags))) {
  344. bt_mesh_beacon_ivu_initiator(false);
  345. }
  346. /* If a node on a primary subnet receives an IV update on any other subnet,
  347. * the IV update shall be ignored. And if a node on a non-primary subnet
  348. * receives an IV update on primary subnet, the IV update shall be ignored,
  349. * because it doesn't have a primary network key.
  350. */
  351. if ((bt_mesh_primary_subnet_exist() && sub->net_idx == BLE_MESH_KEY_PRIMARY) ||
  352. (!bt_mesh_primary_subnet_exist() && sub->net_idx != BLE_MESH_KEY_PRIMARY)) {
  353. iv_change = bt_mesh_net_iv_update(iv_index, BLE_MESH_IV_UPDATE(flags));
  354. }
  355. kr_change = bt_mesh_kr_update(sub, BLE_MESH_KEY_REFRESH(flags), new_key);
  356. if (kr_change) {
  357. bt_mesh_net_secure_beacon_update(sub);
  358. }
  359. if (iv_change) {
  360. /* Update all subnets */
  361. bt_mesh_net_sec_update(NULL);
  362. } else if (kr_change) {
  363. /* Key Refresh without IV Update only impacts one subnet */
  364. bt_mesh_net_sec_update(sub);
  365. }
  366. update_stats:
  367. if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED &&
  368. sub->snb_cur < 0xff) {
  369. sub->snb_cur++;
  370. }
  371. }
  372. void bt_mesh_beacon_recv(struct net_buf_simple *buf, int8_t rssi)
  373. {
  374. uint8_t type = 0U;
  375. BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len));
  376. if (buf->len < 1) {
  377. BT_ERR("Too short beacon");
  378. return;
  379. }
  380. type = net_buf_simple_pull_u8(buf);
  381. switch (type) {
  382. case BEACON_TYPE_UNPROVISIONED:
  383. BT_DBG("Unprovisioned device beacon received");
  384. if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) &&
  385. IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
  386. bt_mesh_is_provisioner_en()) {
  387. bt_mesh_provisioner_unprov_beacon_recv(buf, rssi);
  388. }
  389. if (IS_ENABLED(CONFIG_BLE_MESH_RPR_SRV) &&
  390. bt_mesh_is_provisioned()) {
  391. const bt_mesh_addr_t *addr = bt_mesh_get_unprov_dev_addr();
  392. bt_mesh_unprov_dev_fifo_enqueue(buf->data, addr->val, bt_mesh_get_adv_type());
  393. bt_mesh_rpr_srv_unprov_beacon_recv(buf, bt_mesh_get_adv_type(), addr, rssi);
  394. }
  395. break;
  396. case BEACON_TYPE_SECURE:
  397. secure_beacon_recv(buf);
  398. break;
  399. #if CONFIG_BLE_MESH_PRIVATE_BEACON
  400. case BEACON_TYPE_PRIVATE:
  401. bt_mesh_private_beacon_recv(buf);
  402. break;
  403. #endif
  404. default:
  405. BT_DBG("Unknown beacon type 0x%02x", type);
  406. break;
  407. }
  408. }
  409. void bt_mesh_beacon_init(void)
  410. {
  411. /* secure beacon init */
  412. if (k_delayed_work_init(&snb_timer, secure_beacon_send_timeout)) {
  413. BT_ERR("Failed to create a snb_timer");
  414. return;
  415. }
  416. #if CONFIG_BLE_MESH_PRB_SRV
  417. /* private beacon init */
  418. if (bt_mesh_private_beacon_timer_init()) {
  419. BT_ERR("Failed to create a mpb_timer");
  420. return;
  421. }
  422. #endif
  423. }
  424. #if CONFIG_BLE_MESH_DEINIT
  425. void bt_mesh_beacon_deinit(void)
  426. {
  427. /* secure beacon deinit */
  428. k_delayed_work_free(&snb_timer);
  429. #if CONFIG_BLE_MESH_PRB_SRV
  430. /* private beacon deinit */
  431. bt_mesh_private_beacon_timer_free();
  432. #endif
  433. }
  434. #endif /* CONFIG_BLE_MESH_DEINIT */
  435. void bt_mesh_beacon_ivu_initiator(bool enable)
  436. {
  437. bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_INITIATOR, enable);
  438. if (enable) {
  439. k_delayed_work_submit(&snb_timer, K_NO_WAIT);
  440. #if CONFIG_BLE_MESH_PRB_SRV
  441. if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
  442. bt_mesh_private_beacon_timer_submit(K_NO_WAIT);
  443. }
  444. #endif
  445. } else {
  446. if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_DISABLED) {
  447. k_delayed_work_cancel(&snb_timer);
  448. }
  449. #if CONFIG_BLE_MESH_PRB_SRV
  450. if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_DISABLED) {
  451. bt_mesh_private_beacon_timer_cancel();
  452. }
  453. #endif
  454. }
  455. }
  456. void bt_mesh_secure_beacon_enable(void)
  457. {
  458. size_t subnet_size = 0U;
  459. int i = 0;
  460. if (IS_ENABLED(CONFIG_BLE_MESH_NODE) &&
  461. bt_mesh_is_node() && !bt_mesh_is_provisioned()) {
  462. k_delayed_work_submit(&snb_timer, K_NO_WAIT);
  463. return;
  464. }
  465. subnet_size = bt_mesh_rx_netkey_size();
  466. for (i = 0; i < subnet_size; i++) {
  467. struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i);
  468. if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) {
  469. continue;
  470. }
  471. sub->snb_last = 0U;
  472. sub->snb_cur = 0U;
  473. bt_mesh_net_secure_beacon_update(sub);
  474. }
  475. k_delayed_work_submit(&snb_timer, K_NO_WAIT);
  476. }
  477. void bt_mesh_secure_beacon_disable(void)
  478. {
  479. if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR)) {
  480. k_delayed_work_cancel(&snb_timer);
  481. }
  482. }