net.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. /* Bluetooth Mesh */
  2. /*
  3. * SPDX-FileCopyrightText: 2017 Intel Corporation
  4. * SPDX-FileContributor: 2018-2022 Espressif Systems (Shanghai) CO LTD
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. */
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <stdbool.h>
  11. #include "crypto.h"
  12. #include "adv.h"
  13. #include "scan.h"
  14. #include "mesh.h"
  15. #include "lpn.h"
  16. #include "rpl.h"
  17. #include "friend.h"
  18. #include "transport.h"
  19. #include "access.h"
  20. #include "foundation.h"
  21. #include "beacon.h"
  22. #include "settings.h"
  23. #include "fast_prov.h"
  24. #include "prov_node.h"
  25. #include "test.h"
  26. #include "fast_prov.h"
  27. #include "proxy_client.h"
  28. #include "proxy_server.h"
  29. #include "pvnr_mgmt.h"
  30. #include "mesh_v1.1/utils.h"
  31. /* Minimum valid Mesh Network PDU length. The Network headers
  32. * themselves take up 9 bytes. After that there is a minumum of 1 byte
  33. * payload for both CTL=1 and CTL=0 PDUs (smallest OpCode is 1 byte). CTL=1
  34. * PDUs must use a 64-bit (8 byte) NetMIC, whereas CTL=0 PDUs have at least
  35. * a 32-bit (4 byte) NetMIC and AppMIC giving again a total of 8 bytes.
  36. */
  37. #define BLE_MESH_NET_MIN_PDU_LEN (BLE_MESH_NET_HDR_LEN + 1 + 8)
  38. /* Seq limit after IV Update is triggered */
  39. #define IV_UPDATE_SEQ_LIMIT 8000000
  40. /* Determine how many friendship credentials we need */
  41. #if CONFIG_BLE_MESH_FRIEND
  42. #define FRIEND_CRED_COUNT CONFIG_BLE_MESH_FRIEND_LPN_COUNT
  43. #elif CONFIG_BLE_MESH_LOW_POWER
  44. #define FRIEND_CRED_COUNT CONFIG_BLE_MESH_SUBNET_COUNT
  45. #else
  46. #define FRIEND_CRED_COUNT 0
  47. #endif
  48. #if FRIEND_CRED_COUNT > 0
  49. static struct friend_cred friend_cred[FRIEND_CRED_COUNT];
  50. #endif
  51. static struct {
  52. uint32_t src:15, /* MSB of source address is always 0 */
  53. seq:17;
  54. } msg_cache[CONFIG_BLE_MESH_MSG_CACHE_SIZE];
  55. static uint16_t msg_cache_next;
  56. /* Singleton network context (the implementation only supports one) */
  57. struct bt_mesh_net bt_mesh = {
  58. .local_queue = SYS_SLIST_STATIC_INIT(&bt_mesh.local_queue),
  59. .sub = {
  60. [0 ... (CONFIG_BLE_MESH_SUBNET_COUNT - 1)] = {
  61. .net_idx = BLE_MESH_KEY_UNUSED,
  62. }
  63. },
  64. .app_keys = {
  65. [0 ... (CONFIG_BLE_MESH_APP_KEY_COUNT - 1)] = {
  66. .net_idx = BLE_MESH_KEY_UNUSED,
  67. }
  68. },
  69. };
  70. static uint32_t dup_cache[4];
  71. static int dup_cache_next;
  72. #if CONFIG_BLE_MESH_RELAY_ADV_BUF
  73. #define BLE_MESH_MAX_STORED_RELAY_COUNT (CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT / 2)
  74. #endif
  75. static bool check_dup(struct net_buf_simple *data)
  76. {
  77. const uint8_t *tail = net_buf_simple_tail(data);
  78. uint32_t val = 0U;
  79. int i;
  80. val = sys_get_be32(tail - 4) ^ sys_get_be32(tail - 8);
  81. for (i = 0; i < ARRAY_SIZE(dup_cache); i++) {
  82. if (dup_cache[i] == val) {
  83. return true;
  84. }
  85. }
  86. dup_cache[dup_cache_next++] = val;
  87. dup_cache_next %= ARRAY_SIZE(dup_cache);
  88. return false;
  89. }
  90. static bool msg_cache_match(struct bt_mesh_net_rx *rx,
  91. struct net_buf_simple *pdu)
  92. {
  93. int i;
  94. for (i = 0; i < ARRAY_SIZE(msg_cache); i++) {
  95. if (msg_cache[i].src == BLE_MESH_NET_HDR_SRC(pdu->data) &&
  96. msg_cache[i].seq == (BLE_MESH_NET_HDR_SEQ(pdu->data) & BIT_MASK(17))) {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. static void msg_cache_add(struct bt_mesh_net_rx *rx)
  103. {
  104. rx->msg_cache_idx = msg_cache_next++;
  105. msg_cache[rx->msg_cache_idx].src = rx->ctx.addr;
  106. msg_cache[rx->msg_cache_idx].seq = rx->seq;
  107. msg_cache_next %= ARRAY_SIZE(msg_cache);
  108. }
  109. #if CONFIG_BLE_MESH_PROVISIONER
  110. void bt_mesh_msg_cache_clear(uint16_t unicast_addr, uint8_t elem_num)
  111. {
  112. int i;
  113. for (i = 0; i < ARRAY_SIZE(msg_cache); i++) {
  114. if (msg_cache[i].src >= unicast_addr &&
  115. msg_cache[i].src < unicast_addr + elem_num) {
  116. memset(&msg_cache[i], 0, sizeof(msg_cache[i]));
  117. }
  118. }
  119. }
  120. #endif /* CONFIG_BLE_MESH_PROVISIONER */
  121. struct bt_mesh_subnet *bt_mesh_subnet_get(uint16_t net_idx)
  122. {
  123. if (bt_mesh_is_provisioned()) {
  124. #if CONFIG_BLE_MESH_NODE
  125. if (!IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) {
  126. if (net_idx == BLE_MESH_KEY_ANY) {
  127. return &bt_mesh.sub[0];
  128. }
  129. for (int i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
  130. if (bt_mesh.sub[i].net_idx == net_idx) {
  131. return &bt_mesh.sub[i];
  132. }
  133. }
  134. } else {
  135. return bt_mesh_fast_prov_subnet_get(net_idx);
  136. }
  137. #endif
  138. } else if (bt_mesh_is_provisioner_en()) {
  139. #if CONFIG_BLE_MESH_PROVISIONER
  140. if (net_idx == BLE_MESH_KEY_ANY) {
  141. return bt_mesh.p_sub[0];
  142. }
  143. for (int i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) {
  144. if (bt_mesh.p_sub[i] &&
  145. bt_mesh.p_sub[i]->net_idx == net_idx) {
  146. return bt_mesh.p_sub[i];
  147. }
  148. }
  149. #endif
  150. }
  151. return NULL;
  152. }
  153. int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys,
  154. const uint8_t key[16])
  155. {
  156. uint8_t p[] = { 0 };
  157. uint8_t nid = 0U;
  158. int err = 0;
  159. err = bt_mesh_k2(key, p, sizeof(p), &nid, keys->enc, keys->privacy);
  160. if (err) {
  161. BT_ERR("Unable to generate NID, EncKey & PrivacyKey");
  162. return err;
  163. }
  164. memcpy(keys->net, key, 16);
  165. keys->nid = nid;
  166. BT_DBG("NID 0x%02x EncKey %s", keys->nid, bt_hex(keys->enc, 16));
  167. BT_DBG("PrivacyKey %s", bt_hex(keys->privacy, 16));
  168. err = bt_mesh_k3(key, keys->net_id);
  169. if (err) {
  170. BT_ERR("Unable to generate Net ID");
  171. return err;
  172. }
  173. BT_DBG("NetID %s", bt_hex(keys->net_id, 8));
  174. #if CONFIG_BLE_MESH_GATT_PROXY_SERVER
  175. err = bt_mesh_identity_key(key, keys->identity);
  176. if (err) {
  177. BT_ERR("Unable to generate IdentityKey");
  178. return err;
  179. }
  180. BT_DBG("IdentityKey %s", bt_hex(keys->identity, 16));
  181. #endif /* GATT_PROXY */
  182. err = bt_mesh_secure_beacon_key(key, keys->beacon);
  183. if (err) {
  184. BT_ERR("Unable to generate beacon key");
  185. return err;
  186. }
  187. BT_DBG("BeaconKey %s", bt_hex(keys->beacon, 16));
  188. #if CONFIG_BLE_MESH_PRIVATE_BEACON
  189. err = bt_mesh_private_beacon_key(key, keys->private_beacon);
  190. if (err) {
  191. BT_ERR("Unable to generate private beacon key");
  192. return err;
  193. }
  194. BT_DBG("PrivateBeaconKey %s", bt_hex(keys->private_beacon, 16));
  195. #endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */
  196. #if CONFIG_BLE_MESH_DF_SRV
  197. p[0] = 0x02;
  198. err = bt_mesh_k2(key, p, sizeof(p), &nid, keys->direct_enc, keys->direct_privacy);
  199. if (err) {
  200. BT_ERR("Unable to generate directed NID, EncKey & PrivacyKey");
  201. return err;
  202. }
  203. keys->direct_nid = nid;
  204. #endif /* CONFIG_BLE_MESH_DF_SRV */
  205. return 0;
  206. }
  207. #if (CONFIG_BLE_MESH_LOW_POWER || CONFIG_BLE_MESH_FRIEND)
  208. int friend_cred_set(struct friend_cred *cred, uint8_t idx, const uint8_t net_key[16])
  209. {
  210. uint16_t lpn_addr = 0U, frnd_addr = 0U;
  211. uint8_t p[9] = {0};
  212. int err = 0;
  213. #if CONFIG_BLE_MESH_LOW_POWER
  214. if (cred->addr == bt_mesh.lpn.frnd) {
  215. lpn_addr = bt_mesh_primary_addr();
  216. frnd_addr = cred->addr;
  217. } else {
  218. lpn_addr = cred->addr;
  219. frnd_addr = bt_mesh_primary_addr();
  220. }
  221. #else
  222. lpn_addr = cred->addr;
  223. frnd_addr = bt_mesh_primary_addr();
  224. #endif
  225. BT_DBG("LPNAddress 0x%04x FriendAddress 0x%04x", lpn_addr, frnd_addr);
  226. BT_DBG("LPNCounter 0x%04x FriendCounter 0x%04x", cred->lpn_counter,
  227. cred->frnd_counter);
  228. p[0] = 0x01;
  229. sys_put_be16(lpn_addr, p + 1);
  230. sys_put_be16(frnd_addr, p + 3);
  231. sys_put_be16(cred->lpn_counter, p + 5);
  232. sys_put_be16(cred->frnd_counter, p + 7);
  233. err = bt_mesh_k2(net_key, p, sizeof(p), &cred->cred[idx].nid,
  234. cred->cred[idx].enc, cred->cred[idx].privacy);
  235. if (err) {
  236. BT_ERR("Unable to generate NID, EncKey & PrivacyKey");
  237. return err;
  238. }
  239. BT_DBG("Friend NID 0x%02x EncKey %s", cred->cred[idx].nid,
  240. bt_hex(cred->cred[idx].enc, 16));
  241. BT_DBG("Friend PrivacyKey %s", bt_hex(cred->cred[idx].privacy, 16));
  242. return 0;
  243. }
  244. void friend_cred_refresh(uint16_t net_idx)
  245. {
  246. int i;
  247. for (i = 0; i < ARRAY_SIZE(friend_cred); i++) {
  248. struct friend_cred *cred = &friend_cred[i];
  249. if (cred->addr != BLE_MESH_ADDR_UNASSIGNED &&
  250. cred->net_idx == net_idx) {
  251. memcpy(&cred->cred[0], &cred->cred[1],
  252. sizeof(cred->cred[0]));
  253. }
  254. }
  255. }
  256. int friend_cred_update(struct bt_mesh_subnet *sub)
  257. {
  258. int err = 0, i;
  259. BT_DBG("net_idx 0x%04x", sub->net_idx);
  260. for (i = 0; i < ARRAY_SIZE(friend_cred); i++) {
  261. struct friend_cred *cred = &friend_cred[i];
  262. if (cred->addr == BLE_MESH_ADDR_UNASSIGNED ||
  263. cred->net_idx != sub->net_idx) {
  264. continue;
  265. }
  266. err = friend_cred_set(cred, 1, sub->keys[1].net);
  267. if (err) {
  268. return err;
  269. }
  270. }
  271. return 0;
  272. }
  273. struct friend_cred *friend_cred_create(struct bt_mesh_subnet *sub, uint16_t addr,
  274. uint16_t lpn_counter, uint16_t frnd_counter)
  275. {
  276. struct friend_cred *cred = NULL;
  277. int i, err = 0;
  278. BT_DBG("net_idx 0x%04x addr 0x%04x", sub->net_idx, addr);
  279. for (cred = NULL, i = 0; i < ARRAY_SIZE(friend_cred); i++) {
  280. if ((friend_cred[i].addr == BLE_MESH_ADDR_UNASSIGNED) ||
  281. (friend_cred[i].addr == addr &&
  282. friend_cred[i].net_idx == sub->net_idx)) {
  283. cred = &friend_cred[i];
  284. break;
  285. }
  286. }
  287. if (!cred) {
  288. BT_WARN("No free friend credential slots");
  289. return NULL;
  290. }
  291. cred->net_idx = sub->net_idx;
  292. cred->addr = addr;
  293. cred->lpn_counter = lpn_counter;
  294. cred->frnd_counter = frnd_counter;
  295. err = friend_cred_set(cred, 0, sub->keys[0].net);
  296. if (err) {
  297. friend_cred_clear(cred);
  298. return NULL;
  299. }
  300. if (sub->kr_flag) {
  301. err = friend_cred_set(cred, 1, sub->keys[1].net);
  302. if (err) {
  303. friend_cred_clear(cred);
  304. return NULL;
  305. }
  306. }
  307. return cred;
  308. }
  309. void friend_cred_clear(struct friend_cred *cred)
  310. {
  311. cred->net_idx = BLE_MESH_KEY_UNUSED;
  312. cred->addr = BLE_MESH_ADDR_UNASSIGNED;
  313. cred->lpn_counter = 0U;
  314. cred->frnd_counter = 0U;
  315. (void)memset(cred->cred, 0, sizeof(cred->cred));
  316. }
  317. int friend_cred_del(uint16_t net_idx, uint16_t addr)
  318. {
  319. int i;
  320. for (i = 0; i < ARRAY_SIZE(friend_cred); i++) {
  321. struct friend_cred *cred = &friend_cred[i];
  322. if (cred->addr == addr && cred->net_idx == net_idx) {
  323. friend_cred_clear(cred);
  324. return 0;
  325. }
  326. }
  327. return -ENOENT;
  328. }
  329. int friend_cred_get(struct bt_mesh_subnet *sub, uint16_t addr, uint8_t *nid,
  330. const uint8_t **enc, const uint8_t **priv)
  331. {
  332. int i;
  333. BT_DBG("net_idx 0x%04x addr 0x%04x", sub->net_idx, addr);
  334. for (i = 0; i < ARRAY_SIZE(friend_cred); i++) {
  335. struct friend_cred *cred = &friend_cred[i];
  336. if (cred->net_idx != sub->net_idx) {
  337. continue;
  338. }
  339. if (addr != BLE_MESH_ADDR_UNASSIGNED && cred->addr != addr) {
  340. continue;
  341. }
  342. if (nid) {
  343. *nid = cred->cred[sub->kr_flag].nid;
  344. }
  345. if (enc) {
  346. *enc = cred->cred[sub->kr_flag].enc;
  347. }
  348. if (priv) {
  349. *priv = cred->cred[sub->kr_flag].privacy;
  350. }
  351. return 0;
  352. }
  353. return -ENOENT;
  354. }
  355. #else
  356. int friend_cred_get(struct bt_mesh_subnet *sub, uint16_t addr, uint8_t *nid,
  357. const uint8_t **enc, const uint8_t **priv)
  358. {
  359. return -ENOENT;
  360. }
  361. #endif /* FRIEND || LOW_POWER */
  362. uint8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub)
  363. {
  364. uint8_t flags = 0x00;
  365. if (sub && sub->kr_flag) {
  366. flags |= BLE_MESH_NET_FLAG_KR;
  367. }
  368. if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
  369. flags |= BLE_MESH_NET_FLAG_IVU;
  370. }
  371. return flags;
  372. }
  373. int bt_mesh_net_secure_beacon_update(struct bt_mesh_subnet *sub)
  374. {
  375. uint8_t flags = bt_mesh_net_flags(sub);
  376. struct bt_mesh_subnet_keys *keys = NULL;
  377. if (sub->kr_flag) {
  378. BT_DBG("NetIndex %u Using new key", sub->net_idx);
  379. keys = &sub->keys[1];
  380. } else {
  381. BT_DBG("NetIndex %u Using current key", sub->net_idx);
  382. keys = &sub->keys[0];
  383. }
  384. BT_DBG("flags 0x%02x, IVI 0x%08x", flags, bt_mesh.iv_index);
  385. return bt_mesh_secure_beacon_auth(keys->beacon, flags, keys->net_id,
  386. bt_mesh.iv_index, sub->auth);
  387. }
  388. int bt_mesh_net_create(uint16_t idx, uint8_t flags, const uint8_t key[16],
  389. uint32_t iv_index)
  390. {
  391. struct bt_mesh_subnet *sub = NULL;
  392. int err = 0;
  393. BT_DBG("idx %u flags 0x%02x iv_index %u", idx, flags, iv_index);
  394. BT_DBG("NetKey %s", bt_hex(key, 16));
  395. (void)memset(msg_cache, 0, sizeof(msg_cache));
  396. msg_cache_next = 0U;
  397. sub = &bt_mesh.sub[0];
  398. sub->kr_flag = BLE_MESH_KEY_REFRESH(flags);
  399. if (sub->kr_flag) {
  400. err = bt_mesh_net_keys_create(&sub->keys[1], key);
  401. if (err) {
  402. return -EIO;
  403. }
  404. sub->kr_phase = BLE_MESH_KR_PHASE_2;
  405. } else {
  406. err = bt_mesh_net_keys_create(&sub->keys[0], key);
  407. if (err) {
  408. return -EIO;
  409. }
  410. }
  411. sub->net_idx = idx;
  412. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
  413. sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED;
  414. } else {
  415. sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED;
  416. #if CONFIG_BLE_MESH_PRB_SRV
  417. sub->private_node_id = BLE_MESH_PRIVATE_NODE_IDENTITY_NOT_SUPPORTED;
  418. #endif
  419. }
  420. bt_mesh.iv_index = iv_index;
  421. bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS,
  422. BLE_MESH_IV_UPDATE(flags));
  423. /* Set minimum required hours, since the 96-hour minimum requirement
  424. * doesn't apply straight after provisioning (since we can't know how
  425. * long has actually passed since the network changed its state).
  426. */
  427. bt_mesh.ivu_duration = BLE_MESH_IVU_MIN_HOURS;
  428. /* Make sure we have valid beacon data to be sent */
  429. bt_mesh_net_secure_beacon_update(sub);
  430. #if CONFIG_BLE_MESH_DF_SRV
  431. return bt_mesh_directed_forwarding_sub_init(sub);
  432. #endif
  433. return 0;
  434. }
  435. void bt_mesh_net_revoke_keys(struct bt_mesh_subnet *sub)
  436. {
  437. int i;
  438. BT_DBG("idx 0x%04x", sub->net_idx);
  439. memcpy(&sub->keys[0], &sub->keys[1], sizeof(sub->keys[0]));
  440. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  441. BT_DBG("Store updated NetKey persistently");
  442. bt_mesh_store_subnet(sub);
  443. }
  444. for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) {
  445. struct bt_mesh_app_key *key = &bt_mesh.app_keys[i];
  446. if (key->net_idx != sub->net_idx || !key->updated) {
  447. continue;
  448. }
  449. memcpy(&key->keys[0], &key->keys[1], sizeof(key->keys[0]));
  450. key->updated = false;
  451. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  452. BT_DBG("Store updated AppKey persistently");
  453. bt_mesh_store_app_key(key);
  454. }
  455. }
  456. }
  457. bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, uint8_t new_kr, bool new_key)
  458. {
  459. if (new_kr != sub->kr_flag && sub->kr_phase == BLE_MESH_KR_NORMAL) {
  460. BT_WARN("KR change in normal operation. Are we blacklisted?");
  461. return false;
  462. }
  463. sub->kr_flag = new_kr;
  464. if (sub->kr_flag) {
  465. if (sub->kr_phase == BLE_MESH_KR_PHASE_1) {
  466. BT_INFO("Phase 1 -> Phase 2");
  467. sub->kr_phase = BLE_MESH_KR_PHASE_2;
  468. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  469. BT_DBG("Storing kr phase persistently");
  470. bt_mesh_store_subnet(sub);
  471. }
  472. return true;
  473. }
  474. } else {
  475. switch (sub->kr_phase) {
  476. case BLE_MESH_KR_PHASE_1:
  477. if (!new_key) {
  478. /* Ignore */
  479. break;
  480. }
  481. /* Upon receiving a Secure Network beacon with the KR flag set
  482. * to 0 using the new NetKey in Phase 1, the node shall
  483. * immediately transition to Phase 3, which effectively skips
  484. * Phase 2.
  485. *
  486. * Intentional fall-through.
  487. */
  488. case BLE_MESH_KR_PHASE_2:
  489. BT_INFO("KR Phase 0x%02x -> Normal", sub->kr_phase);
  490. #if CONFIG_BLE_MESH_PRB_SRV
  491. /* In this case, consider that kr_flag has changed, so
  492. * need to modify the content of the random field.
  493. */
  494. bt_mesh_private_beacon_update_random(sub);
  495. #endif
  496. sub->kr_phase = BLE_MESH_KR_NORMAL;
  497. bt_mesh_net_revoke_keys(sub);
  498. if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) ||
  499. IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
  500. friend_cred_refresh(sub->net_idx);
  501. }
  502. return true;
  503. }
  504. }
  505. return false;
  506. }
  507. #if CONFIG_BLE_MESH_IV_UPDATE_TEST
  508. void bt_mesh_iv_update_test(bool enable)
  509. {
  510. bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_TEST, enable);
  511. /* Reset the duration variable - needed for some PTS tests */
  512. bt_mesh.ivu_duration = 0U;
  513. }
  514. bool bt_mesh_iv_update(void)
  515. {
  516. if (!bt_mesh_is_provisioned()) {
  517. BT_ERR("Not yet provisioned");
  518. return false;
  519. }
  520. if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
  521. bt_mesh_net_iv_update(bt_mesh.iv_index, false);
  522. } else {
  523. bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true);
  524. }
  525. bt_mesh_net_sec_update(NULL);
  526. return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS);
  527. }
  528. #endif /* CONFIG_BLE_MESH_IV_UPDATE_TEST */
  529. /* Used for sending immediate beacons to Friend queues and GATT clients */
  530. void bt_mesh_net_sec_update(struct bt_mesh_subnet *sub)
  531. {
  532. if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
  533. bt_mesh_friend_sec_update(sub ? sub->net_idx : BLE_MESH_KEY_ANY);
  534. }
  535. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
  536. (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED
  537. #if CONFIG_BLE_MESH_PRB_SRV
  538. || bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED
  539. #endif
  540. )) {
  541. bt_mesh_proxy_server_beacon_send(sub);
  542. }
  543. }
  544. bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update)
  545. {
  546. int i;
  547. /* If a node in Normal Operation receives a Secure Network beacon or
  548. * a Mesh Private beacon with an IV index less than the last known
  549. * IV Index or greater than the last known IV Index + 42, the Secure
  550. * Network beacon or the Mesh Private beacon shall be ignored.
  551. */
  552. if (iv_index < bt_mesh.iv_index ||
  553. iv_index > bt_mesh.iv_index + 42) {
  554. BT_ERR("IV Index out of sync: 0x%08x != 0x%08x",
  555. iv_index, bt_mesh.iv_index);
  556. return false;
  557. }
  558. if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
  559. /* We're currently in IV Update mode */
  560. if (iv_index >= bt_mesh.iv_index + 1) {
  561. BT_WARN("Performing IV Index Recovery");
  562. (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl));
  563. bt_mesh.iv_index = iv_index;
  564. bt_mesh.seq = 0U;
  565. goto do_update;
  566. }
  567. if (iv_update) {
  568. /* Nothing to do */
  569. BT_DBG("Already in IV Update in Progress state");
  570. return false;
  571. }
  572. } else {
  573. /* We're currently in Normal mode */
  574. if (iv_index == bt_mesh.iv_index) {
  575. BT_DBG("Same IV Index in normal mode");
  576. return false;
  577. }
  578. /* If a node in Normal Operation receives a Secure Network beacon
  579. * or a Mesh Private beacon with an IV index greater than the
  580. * last known IV Index + 1, it may initiate an IV Index Recovery
  581. * procedure see Section 3.10.6
  582. */
  583. if ((iv_index > bt_mesh.iv_index + 1)
  584. #if CONFIG_BLE_MESH_IVU_RECOVERY_IVI
  585. || (iv_index == bt_mesh.iv_index + 1 && !iv_update)
  586. #endif
  587. ) {
  588. BT_WARN("Performing IV Index Recovery");
  589. (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl));
  590. bt_mesh.iv_index = iv_index;
  591. bt_mesh.seq = 0U;
  592. goto do_update;
  593. }
  594. /* If a node in Normal Operation receives a Secure Network beacon
  595. * or a Mesh Private beacon with an IV index equal to the last known
  596. * IV index+1 and the IV Update Flag set to 0, the node may update
  597. * its IV without going to the IV Update in Progress state, or it may
  598. * initiate an IV Index Recovery procedure(Section 3.10.6), or it may
  599. * ignore the Secure Network beacon or the Mesh Private beacon.
  600. */
  601. #if !CONFIG_BLE_MESH_IVU_RECOVERY_IVI
  602. if (iv_index == bt_mesh.iv_index + 1 && !iv_update) {
  603. BT_WARN("Ignoring new index in normal mode");
  604. return false;
  605. }
  606. #endif
  607. if (!iv_update) {
  608. /* Nothing to do */
  609. BT_DBG("Already in Normal state");
  610. return false;
  611. }
  612. }
  613. if (!(IS_ENABLED(CONFIG_BLE_MESH_IV_UPDATE_TEST) &&
  614. bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_TEST))) {
  615. if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) {
  616. BT_WARN("IV Update before minimum duration");
  617. return false;
  618. }
  619. }
  620. /* Defer change to Normal Operation if there are pending acks */
  621. if (!iv_update && bt_mesh_tx_in_progress()) {
  622. BT_WARN("IV Update deferred because of pending transfer");
  623. bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING);
  624. return false;
  625. }
  626. do_update:
  627. bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, iv_update);
  628. bt_mesh.ivu_duration = 0U;
  629. if (iv_update) {
  630. bt_mesh.iv_index = iv_index;
  631. BT_INFO("IV Update state entered. New index 0x%08x",
  632. bt_mesh.iv_index);
  633. bt_mesh_rpl_update();
  634. } else {
  635. BT_INFO("Normal mode entered");
  636. bt_mesh.seq = 0U;
  637. }
  638. k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT);
  639. size_t subnet_size = bt_mesh_rx_netkey_size();
  640. for (i = 0; i < subnet_size; i++) {
  641. struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i);
  642. if (sub && sub->net_idx != BLE_MESH_KEY_UNUSED) {
  643. bt_mesh_net_secure_beacon_update(sub);
  644. }
  645. }
  646. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  647. bt_mesh_store_iv(false);
  648. }
  649. return true;
  650. }
  651. bool bt_mesh_primary_subnet_exist(void)
  652. {
  653. if (bt_mesh_subnet_get(BLE_MESH_KEY_PRIMARY)) {
  654. return true;
  655. }
  656. return false;
  657. }
  658. uint32_t bt_mesh_next_seq(void)
  659. {
  660. uint32_t seq = bt_mesh.seq++;
  661. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  662. bt_mesh_store_seq();
  663. }
  664. if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) &&
  665. bt_mesh.seq > IV_UPDATE_SEQ_LIMIT &&
  666. bt_mesh_primary_subnet_exist()) {
  667. bt_mesh_beacon_ivu_initiator(true);
  668. bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true);
  669. bt_mesh_net_sec_update(NULL);
  670. }
  671. return seq;
  672. }
  673. int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf,
  674. bool new_key, uint8_t *tx_cred, uint8_t tx_tag,
  675. const struct bt_mesh_send_cb *cb, void *cb_data)
  676. {
  677. const uint8_t *enc = NULL, *priv = NULL;
  678. uint32_t seq = 0U;
  679. uint16_t dst = 0U;
  680. int err = 0;
  681. BT_DBG("net_idx 0x%04x new_key %u len %u", sub->net_idx, new_key,
  682. buf->len);
  683. /* Previously when resending the segments, only managed flooding
  684. * security credentials will be used.
  685. * Now for the segments retransmission, try to get the security
  686. * credentials based on the parameters "tx_cred" and "tag".
  687. *
  688. * Note:
  689. * Use "tag" here in case the friendship is terminated, which will
  690. * cause the friendship security credentials are deleted.
  691. *
  692. * TODO:
  693. * Potential issue here, if the subnet is deleted, exception (on
  694. * Provisioner) will happen.
  695. */
  696. if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) &&
  697. *tx_cred == BLE_MESH_FRIENDSHIP_CRED) {
  698. err = friend_cred_get(sub, BLE_MESH_ADDR_UNASSIGNED, NULL, &enc, &priv);
  699. if (err) {
  700. /* If tagged with immutable-credentials, then no
  701. * falling back should happen.
  702. */
  703. if (bt_mesh_tag_immutable_cred(tx_tag)) {
  704. BT_ERR("Resend, no friendship credentials found");
  705. return err;
  706. }
  707. BT_WARN("Falling back to managed flooding credentials");
  708. *tx_cred = BLE_MESH_FLOODING_CRED;
  709. enc = sub->keys[new_key].enc;
  710. priv = sub->keys[new_key].privacy;
  711. }
  712. }
  713. #if CONFIG_BLE_MESH_DF_SRV
  714. else if (*tx_cred == BLE_MESH_DIRECTED_CRED) {
  715. enc = sub->keys[new_key].direct_enc;
  716. priv = sub->keys[new_key].direct_privacy;
  717. }
  718. #endif /* CONFIG_BLE_MESH_DF_SRV */
  719. else {
  720. enc = sub->keys[new_key].enc;
  721. priv = sub->keys[new_key].privacy;
  722. }
  723. err = bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv);
  724. if (err) {
  725. BT_ERR("De-obfuscate failed (err %d)", err);
  726. return err;
  727. }
  728. err = bt_mesh_net_decrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false, false);
  729. if (err) {
  730. BT_ERR("Decrypt failed (err %d)", err);
  731. return err;
  732. }
  733. /* Update with a new sequence number */
  734. seq = bt_mesh_next_seq();
  735. sys_put_be24(seq, &buf->data[2]);
  736. /* Get destination, in case it's a proxy client */
  737. dst = BLE_MESH_NET_HDR_DST(buf->data);
  738. err = bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false, false);
  739. if (err) {
  740. BT_ERR("Encrypt failed (err %d)", err);
  741. return err;
  742. }
  743. err = bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv);
  744. if (err) {
  745. BT_ERR("Obfuscate failed (err %d)", err);
  746. return err;
  747. }
  748. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
  749. bt_mesh_proxy_server_relay(&buf->b, dst) &&
  750. BLE_MESH_ADDR_IS_UNICAST(dst)) {
  751. send_cb_finalize(cb, cb_data);
  752. return 0;
  753. }
  754. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) &&
  755. bt_mesh_proxy_client_relay(&buf->b, dst)) {
  756. send_cb_finalize(cb, cb_data);
  757. return 0;
  758. }
  759. bt_mesh_adv_send(buf, BLE_MESH_ADV(buf)->xmit, cb, cb_data);
  760. return 0;
  761. }
  762. static void bt_mesh_net_local(void)
  763. {
  764. struct net_buf *buf = NULL;
  765. while ((buf = net_buf_slist_get(&bt_mesh.local_queue))) {
  766. BT_DBG("len %u: %s", buf->len, bt_hex(buf->data, buf->len));
  767. bt_mesh_net_recv(&buf->b, 0, BLE_MESH_NET_IF_LOCAL);
  768. net_buf_unref(buf);
  769. }
  770. }
  771. int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf,
  772. bool proxy)
  773. {
  774. const bool ctl = (tx->ctx->app_idx == BLE_MESH_KEY_UNUSED);
  775. const uint8_t *enc = NULL, *priv = NULL;
  776. uint8_t nid = 0U;
  777. int err = 0;
  778. if (ctl && net_buf_simple_tailroom(buf) < BLE_MESH_MIC_LONG) {
  779. BT_ERR("Insufficient MIC space for CTL PDU");
  780. return -EINVAL;
  781. }
  782. if (net_buf_simple_tailroom(buf) < BLE_MESH_MIC_SHORT) {
  783. BT_ERR("Insufficient MIC space for PDU");
  784. return -EINVAL;
  785. }
  786. BT_DBG("src 0x%04x dst 0x%04x ctl %u seq 0x%06x",
  787. tx->src, tx->ctx->addr, ctl, bt_mesh.seq);
  788. net_buf_simple_push_be16(buf, tx->ctx->addr);
  789. net_buf_simple_push_be16(buf, tx->src);
  790. net_buf_simple_push_be24(buf, bt_mesh_next_seq());
  791. if (ctl) {
  792. net_buf_simple_push_u8(buf, tx->ctx->send_ttl | 0x80);
  793. } else {
  794. net_buf_simple_push_u8(buf, tx->ctx->send_ttl);
  795. }
  796. BT_INFO("Use security credentials 0x%02x, proxy %d", tx->ctx->send_cred, proxy);
  797. if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) &&
  798. tx->ctx->send_cred == BLE_MESH_FRIENDSHIP_CRED) {
  799. err = friend_cred_get(tx->sub, BLE_MESH_ADDR_UNASSIGNED,
  800. &nid, &enc, &priv);
  801. if (err) {
  802. /* If tagged with immutable-credentials, then no
  803. * falling back should happen.
  804. */
  805. if (bt_mesh_tag_immutable_cred(tx->ctx->send_tag)) {
  806. BT_ERR("No friendship credentials found");
  807. return err;
  808. }
  809. BT_WARN("Falling back to managed flooding credentials");
  810. tx->ctx->send_cred = BLE_MESH_FLOODING_CRED;
  811. nid = tx->sub->keys[tx->sub->kr_flag].nid;
  812. enc = tx->sub->keys[tx->sub->kr_flag].enc;
  813. priv = tx->sub->keys[tx->sub->kr_flag].privacy;
  814. }
  815. }
  816. #if CONFIG_BLE_MESH_DF_SRV
  817. else if (tx->ctx->send_cred == BLE_MESH_DIRECTED_CRED) {
  818. nid = tx->sub->keys[tx->sub->kr_flag].direct_nid;
  819. enc = tx->sub->keys[tx->sub->kr_flag].direct_enc;
  820. priv = tx->sub->keys[tx->sub->kr_flag].direct_privacy;
  821. }
  822. #endif /* CONFIG_BLE_MESH_DF_SRV */
  823. else {
  824. tx->ctx->send_cred = BLE_MESH_FLOODING_CRED;
  825. nid = tx->sub->keys[tx->sub->kr_flag].nid;
  826. enc = tx->sub->keys[tx->sub->kr_flag].enc;
  827. priv = tx->sub->keys[tx->sub->kr_flag].privacy;
  828. }
  829. net_buf_simple_push_u8(buf, (nid | (BLE_MESH_NET_IVI_TX & 1) << 7));
  830. err = bt_mesh_net_encrypt(enc, buf, BLE_MESH_NET_IVI_TX, proxy, false);
  831. if (err) {
  832. return err;
  833. }
  834. return bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv);
  835. }
  836. int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf,
  837. const struct bt_mesh_send_cb *cb, void *cb_data)
  838. {
  839. uint8_t bearer = BLE_MESH_ALL_BEARERS;
  840. int err = 0;
  841. BT_DBG("src 0x%04x dst 0x%04x len %u headroom %u tailroom %u",
  842. tx->src, tx->ctx->addr, buf->len, net_buf_headroom(buf),
  843. net_buf_tailroom(buf));
  844. BT_DBG("Payload len %u: %s", buf->len, bt_hex(buf->data, buf->len));
  845. BT_DBG("Seq 0x%06x", bt_mesh.seq);
  846. if (tx->ctx->send_ttl == BLE_MESH_TTL_DEFAULT) {
  847. tx->ctx->send_ttl = bt_mesh_default_ttl_get();
  848. }
  849. #if 0
  850. /* The output filter of the interface connected to advertising
  851. * or GATT bearers shall drop all messages with the TTL value
  852. * set to 1 unless they contain a network PDU that is tagged
  853. * as relay.
  854. */
  855. if (tx->ctx->send_ttl == 1U &&
  856. !bt_mesh_tag_relay(tx->ctx->send_tag)) {
  857. BT_WARN("Ignore PDU with TTL=1 but not tagged as relay");
  858. err = -EIO;
  859. goto done;
  860. }
  861. #endif
  862. /* Spec:
  863. * If the message security material is not set by the network
  864. * layer or any higher layer, the message shall be secured
  865. * using the managed flooding security credentials.
  866. *
  867. * Note:
  868. * Currently no need to add any code for this here, because if
  869. * the security material is not set, the "tx->ctx->send_cred"
  870. * will be initialized to 0x00, which will be used as managed
  871. * flooding security credentials.
  872. */
  873. #if CONFIG_BLE_MESH_DF_SRV
  874. bt_mesh_update_net_send_cred(tx, &bearer);
  875. #endif /* CONFIG_BLE_MESH_DF_SRV */
  876. err = bt_mesh_net_encode(tx, &buf->b, false);
  877. if (err) {
  878. goto done;
  879. }
  880. #if CONFIG_BLE_MESH_SELF_TEST
  881. if (net_pdu_test_cb) {
  882. net_pdu_test_cb(buf->data, buf->len);
  883. goto done;
  884. }
  885. #endif /* CONFIG_BLE_MESH_SELF_TEST */
  886. /* Deliver to GATT Proxy Clients if necessary. Mesh spec 3.4.5.2:
  887. * "The output filter of the interface connected to advertising or
  888. * GATT bearers shall drop all messages with the TTL value set to
  889. * 1 unless they contain a network PDU that is tagged as relay."
  890. *
  891. * "The output filter of the interface connected to the GATT bearer
  892. * shall drop all messages secured using the friendship security
  893. * credentials."
  894. */
  895. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
  896. (bearer & BLE_MESH_GATT_BEARER) &&
  897. (tx->ctx->send_ttl != 1U ||
  898. bt_mesh_tag_relay(tx->ctx->send_tag)) &&
  899. tx->ctx->send_cred != BLE_MESH_FRIENDSHIP_CRED) {
  900. if (bt_mesh_proxy_server_relay(&buf->b, tx->ctx->addr) &&
  901. BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) {
  902. /* Notify completion if this only went
  903. * through the Mesh Proxy.
  904. */
  905. if ((bearer & (~BLE_MESH_GATT_BEARER)) == 0) {
  906. send_cb_finalize(cb, cb_data);
  907. err = 0;
  908. goto done;
  909. }
  910. }
  911. }
  912. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) &&
  913. (bearer & BLE_MESH_GATT_BEARER) &&
  914. (tx->ctx->send_ttl != 1U ||
  915. bt_mesh_tag_relay(tx->ctx->send_tag)) &&
  916. tx->ctx->send_cred != BLE_MESH_FRIENDSHIP_CRED) {
  917. if (bt_mesh_proxy_client_relay(&buf->b, tx->ctx->addr)) {
  918. /* If Proxy Client succeeds to send messages with GATT bearer,
  919. * we can directly finish here. And if not, which means no
  920. * connection has been created with Proxy Client, here we will
  921. * use advertising bearer for the messages.
  922. */
  923. send_cb_finalize(cb, cb_data);
  924. err = 0;
  925. goto done;
  926. }
  927. }
  928. /* Deliver to local network interface if necessary */
  929. if (((IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) ||
  930. (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en())) &&
  931. (bt_mesh_fixed_group_match(tx->ctx->addr) || bt_mesh_elem_find(tx->ctx->addr))) {
  932. if (cb && cb->start) {
  933. cb->start(0, 0, cb_data);
  934. }
  935. net_buf_slist_put(&bt_mesh.local_queue, net_buf_ref(buf));
  936. if (cb && cb->end) {
  937. cb->end(0, cb_data);
  938. }
  939. bt_mesh_net_local();
  940. err = 0;
  941. /* If it is a group address, it still needs to be relayed */
  942. if (BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) {
  943. goto done;
  944. }
  945. }
  946. if ((bearer & BLE_MESH_ADV_BEARER) &&
  947. (tx->ctx->send_ttl != 1U || bt_mesh_tag_relay(tx->ctx->send_tag))) {
  948. /* Deliver to the advertising network interface. Mesh spec
  949. * 3.4.5.2: "The output filter of the interface connected to
  950. * advertising or GATT bearers shall drop all messages with
  951. * TTL value set to 1."
  952. * Spec v1.1 adds additional condition:
  953. * "unless they contain a network PDU that is tagged as relay"
  954. *
  955. * "tx->xmit" has already been initialized in the access.c
  956. * (i.e. bt_mesh_model_send() & bt_mesh_model_publish()).
  957. * Here xmit may needs to be updated according Mesh spec 3.4.6.
  958. */
  959. bt_mesh_net_adv_xmit_update(tx);
  960. BT_INFO("Network PDU, count %d, interval %d",
  961. BLE_MESH_TRANSMIT_COUNT(tx->xmit), BLE_MESH_TRANSMIT_INT(tx->xmit));
  962. bt_mesh_adv_send(buf, tx->xmit, cb, cb_data);
  963. err = 0;
  964. goto done;
  965. }
  966. BT_WARN("Not sent, src 0x%04x, dst 0x%04x, ttl %d, cred 0x%02x, tag 0x%02x",
  967. tx->src, tx->ctx->addr, tx->ctx->send_ttl, tx->ctx->send_cred,
  968. tx->ctx->send_tag);
  969. err = -EIO;
  970. done:
  971. net_buf_unref(buf);
  972. return err;
  973. }
  974. static bool auth_match(struct bt_mesh_subnet_keys *keys,
  975. const uint8_t net_id[8], uint8_t flags,
  976. uint32_t iv_index, const uint8_t auth[8])
  977. {
  978. uint8_t net_auth[8] = {0};
  979. if (memcmp(net_id, keys->net_id, 8)) {
  980. return false;
  981. }
  982. bt_mesh_secure_beacon_auth(keys->beacon, flags, keys->net_id, iv_index,
  983. net_auth);
  984. if (memcmp(auth, net_auth, 8)) {
  985. BT_WARN("Authentication Value %s != %s",
  986. bt_hex(auth, 8), bt_hex(net_auth, 8));
  987. return false;
  988. }
  989. return true;
  990. }
  991. struct bt_mesh_subnet *bt_mesh_subnet_find_with_snb(const uint8_t net_id[8], uint8_t flags,
  992. uint32_t iv_index, const uint8_t auth[8],
  993. bool *new_key)
  994. {
  995. size_t subnet_size = 0U;
  996. int i;
  997. subnet_size = bt_mesh_rx_netkey_size();
  998. for (i = 0; i < subnet_size; i++) {
  999. struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i);
  1000. if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) {
  1001. continue;
  1002. }
  1003. if (auth_match(&sub->keys[0], net_id, flags, iv_index, auth)) {
  1004. *new_key = false;
  1005. return sub;
  1006. }
  1007. if (sub->kr_phase == BLE_MESH_KR_NORMAL) {
  1008. continue;
  1009. }
  1010. if (auth_match(&sub->keys[1], net_id, flags, iv_index, auth)) {
  1011. *new_key = true;
  1012. return sub;
  1013. }
  1014. }
  1015. return NULL;
  1016. }
  1017. int net_decrypt(struct bt_mesh_subnet *sub, const uint8_t *enc,
  1018. const uint8_t *priv, const uint8_t *data,
  1019. size_t data_len, struct bt_mesh_net_rx *rx,
  1020. struct net_buf_simple *buf)
  1021. {
  1022. BT_DBG("NID 0x%02x net_idx 0x%04x", BLE_MESH_NET_HDR_NID(data), sub->net_idx);
  1023. BT_DBG("IVI %u net->iv_index 0x%08x", BLE_MESH_NET_HDR_IVI(data), bt_mesh.iv_index);
  1024. rx->old_iv = (BLE_MESH_NET_HDR_IVI(data) != (bt_mesh.iv_index & 0x01));
  1025. net_buf_simple_reset(buf);
  1026. memcpy(net_buf_simple_add(buf, data_len), data, data_len);
  1027. if (bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_RX(rx), priv)) {
  1028. return -ENOENT;
  1029. }
  1030. rx->ctx.addr = BLE_MESH_NET_HDR_SRC(buf->data);
  1031. if (!BLE_MESH_ADDR_IS_UNICAST(rx->ctx.addr)) {
  1032. BT_DBG("Ignoring non-unicast src addr 0x%04x", rx->ctx.addr);
  1033. return -EINVAL;
  1034. }
  1035. if (rx->net_if == BLE_MESH_NET_IF_ADV && msg_cache_match(rx, buf)) {
  1036. BT_DBG("Duplicate found in Network Message Cache");
  1037. return -EALREADY;
  1038. }
  1039. BT_DBG("src 0x%04x", rx->ctx.addr);
  1040. if (IS_ENABLED(CONFIG_BLE_MESH_PROXY) &&
  1041. rx->net_if == BLE_MESH_NET_IF_PROXY_CFG) {
  1042. return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx),
  1043. true, false);
  1044. }
  1045. return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), false, false);
  1046. }
  1047. #if (CONFIG_BLE_MESH_LOW_POWER || CONFIG_BLE_MESH_FRIEND)
  1048. static int friend_decrypt(struct bt_mesh_subnet *sub, const uint8_t *data,
  1049. size_t data_len, struct bt_mesh_net_rx *rx,
  1050. struct net_buf_simple *buf)
  1051. {
  1052. int i;
  1053. BT_DBG("NID 0x%02x net_idx 0x%04x", BLE_MESH_NET_HDR_NID(data), sub->net_idx);
  1054. for (i = 0; i < ARRAY_SIZE(friend_cred); i++) {
  1055. struct friend_cred *cred = &friend_cred[i];
  1056. if (cred->net_idx != sub->net_idx) {
  1057. continue;
  1058. }
  1059. if (BLE_MESH_NET_HDR_NID(data) == cred->cred[0].nid &&
  1060. !net_decrypt(sub, cred->cred[0].enc, cred->cred[0].privacy,
  1061. data, data_len, rx, buf)) {
  1062. return 0;
  1063. }
  1064. if (sub->kr_phase == BLE_MESH_KR_NORMAL) {
  1065. continue;
  1066. }
  1067. if (BLE_MESH_NET_HDR_NID(data) == cred->cred[1].nid &&
  1068. !net_decrypt(sub, cred->cred[1].enc, cred->cred[1].privacy,
  1069. data, data_len, rx, buf)) {
  1070. rx->new_key = 1U;
  1071. return 0;
  1072. }
  1073. }
  1074. return -ENOENT;
  1075. }
  1076. #endif /* (CONFIG_BLE_MESH_LOW_POWER || CONFIG_BLE_MESH_FRIEND) */
  1077. static int flooding_decrypt(struct bt_mesh_subnet *sub, const uint8_t *data,
  1078. size_t data_len, struct bt_mesh_net_rx *rx,
  1079. struct net_buf_simple *buf)
  1080. {
  1081. BT_DBG("NID 0x%02x net_idx 0x%04x", BLE_MESH_NET_HDR_NID(data), sub->net_idx);
  1082. if (BLE_MESH_NET_HDR_NID(data) == sub->keys[0].nid &&
  1083. !net_decrypt(sub, sub->keys[0].enc, sub->keys[0].privacy,
  1084. data, data_len, rx, buf)) {
  1085. return 0;
  1086. }
  1087. if (sub->kr_phase == BLE_MESH_KR_NORMAL) {
  1088. return -ENOENT;
  1089. }
  1090. if (BLE_MESH_NET_HDR_NID(data) == sub->keys[1].nid &&
  1091. !net_decrypt(sub, sub->keys[1].enc, sub->keys[1].privacy,
  1092. data, data_len, rx, buf)) {
  1093. rx->new_key = 1U;
  1094. return 0;
  1095. }
  1096. return -ENOENT;
  1097. }
  1098. static bool net_find_and_decrypt(const uint8_t *data, size_t data_len,
  1099. struct bt_mesh_net_rx *rx,
  1100. struct net_buf_simple *buf)
  1101. {
  1102. struct bt_mesh_subnet *sub = NULL;
  1103. size_t array_size = 0U;
  1104. int i;
  1105. array_size = bt_mesh_rx_netkey_size();
  1106. for (i = 0; i < array_size; i++) {
  1107. sub = bt_mesh_rx_netkey_get(i);
  1108. if (!sub) {
  1109. BT_DBG("Subnet not found");
  1110. continue;
  1111. }
  1112. if (sub->net_idx == BLE_MESH_KEY_UNUSED) {
  1113. continue;
  1114. }
  1115. #if CONFIG_BLE_MESH_BRC_SRV
  1116. sub->sbr_net_idx = BLE_MESH_KEY_UNUSED;
  1117. #endif
  1118. #if (CONFIG_BLE_MESH_LOW_POWER || CONFIG_BLE_MESH_FRIEND)
  1119. if (!friend_decrypt(sub, data, data_len, rx, buf)) {
  1120. rx->ctx.recv_cred = BLE_MESH_FRIENDSHIP_CRED;
  1121. rx->ctx.net_idx = sub->net_idx;
  1122. rx->sub = sub;
  1123. return true;
  1124. }
  1125. #endif
  1126. #if CONFIG_BLE_MESH_DF_SRV
  1127. if (!bt_mesh_directed_decrypt(sub, data, data_len, rx, buf)) {
  1128. rx->ctx.recv_cred = BLE_MESH_DIRECTED_CRED;
  1129. rx->ctx.net_idx = sub->net_idx;
  1130. rx->sub = sub;
  1131. return true;
  1132. }
  1133. #endif /* CONFIG_BLE_MESH_DF_SRV */
  1134. if (!flooding_decrypt(sub, data, data_len, rx, buf)) {
  1135. rx->ctx.recv_cred = BLE_MESH_FLOODING_CRED;
  1136. rx->ctx.net_idx = sub->net_idx;
  1137. rx->sub = sub;
  1138. return true;
  1139. }
  1140. }
  1141. return false;
  1142. }
  1143. /* Relaying from advertising to the advertising bearer should only happen
  1144. * if the Relay state is set to enabled. Locally originated packets always
  1145. * get sent to the advertising bearer. If the packet came in through GATT,
  1146. * then we should only relay it if the GATT Proxy state is enabled.
  1147. */
  1148. static bool relay_to_adv(enum bt_mesh_net_if net_if)
  1149. {
  1150. switch (net_if) {
  1151. case BLE_MESH_NET_IF_LOCAL:
  1152. return true;
  1153. case BLE_MESH_NET_IF_ADV:
  1154. return (bt_mesh_relay_get() == BLE_MESH_RELAY_ENABLED);
  1155. case BLE_MESH_NET_IF_PROXY:
  1156. return (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED ||
  1157. bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED);
  1158. default:
  1159. return false;
  1160. }
  1161. }
  1162. static void bt_mesh_net_relay(struct net_buf_simple *sbuf,
  1163. struct bt_mesh_net_rx *rx)
  1164. {
  1165. const uint8_t *enc = NULL, *priv = NULL;
  1166. struct net_buf *buf = NULL;
  1167. uint8_t bearer = 0;
  1168. uint8_t xmit = 0U;
  1169. uint8_t cred = 0;
  1170. uint8_t nid = 0U;
  1171. bool netkey_changed = false;
  1172. uint8_t tag = 0;
  1173. if (rx->net_if == BLE_MESH_NET_IF_LOCAL) {
  1174. /* Locally originated PDUs with TTL=1 will only be delivered
  1175. * to local elements as per Mesh Profile 1.0 section 3.4.5.2:
  1176. * "The output filter of the interface connected to
  1177. * advertising or GATT bearers shall drop all messages with
  1178. * TTL value set to 1."
  1179. */
  1180. /* For local network interface, no outbound bearer will be found.
  1181. * So currently if a PDU is received from local network interface
  1182. * with TTL=1, it will be ignored here.
  1183. */
  1184. if (rx->ctx.recv_ttl == 1U) {
  1185. return;
  1186. }
  1187. } else {
  1188. /* Only PDU with TTL=0 will be ignored here. PDU with TTL=1 will
  1189. * be checked later to see if it could be tagged with RELAY.
  1190. */
  1191. if (rx->ctx.recv_ttl < 1U) {
  1192. return;
  1193. }
  1194. }
  1195. #if 0
  1196. /* This could be moved, since in mesh v1.1, two more conditions could
  1197. * cause a message to be relayed, i.e. directed forwarding and subnet
  1198. * bridge.
  1199. * The two conditions will be checked with retransmission requirements.
  1200. */
  1201. if (rx->net_if == BLE_MESH_NET_IF_ADV &&
  1202. bt_mesh_relay_get() != BLE_MESH_RELAY_ENABLED &&
  1203. bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_ENABLED &&
  1204. rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED) {
  1205. return;
  1206. }
  1207. #endif
  1208. /* Mesh spec 3.4.6.3:
  1209. * "The Network PDU shall be evaluated against retransmission conditions
  1210. * for incoming Network PDUs as defined in Table 3.10. For a Network PDU,
  1211. * there may be more than one matching row in Table 3.10. If there is no
  1212. * row that matches the retransmission conditions, then the Network PDU
  1213. * shall not be retransmitted."
  1214. */
  1215. bearer = bt_mesh_net_retrans_match(rx, &cred, &tag);
  1216. if (bearer == BLE_MESH_NONE_BEARER) {
  1217. BT_WARN("No outbound bearer found, inbound bearer %d", rx->net_if);
  1218. return;
  1219. }
  1220. #if CONFIG_BLE_MESH_BRC_SRV
  1221. if (rx->sbr_rpl) {
  1222. BT_ERR("Bridge RPL attack");
  1223. goto done;
  1224. }
  1225. #endif
  1226. if (cred != BLE_MESH_FLOODING_CRED
  1227. #if CONFIG_BLE_MESH_DF_SRV
  1228. && cred != BLE_MESH_DIRECTED_CRED
  1229. #endif
  1230. ) {
  1231. BT_WARN("No outbound security cred found, inbound cred %d", rx->ctx.recv_cred);
  1232. return;
  1233. }
  1234. if (rx->ctx.recv_ttl == 0x01) {
  1235. BT_DBG("Ignore PDU with TTL = 1");
  1236. return;
  1237. }
  1238. if (rx->ctx.recv_ttl == 0x02 && bt_mesh_tag_relay(tag) == false) {
  1239. BT_DBG("Ignore PDU with TTL = 2 but not tagged as relay");
  1240. return;
  1241. }
  1242. BT_DBG("TTL %u CTL %u dst 0x%04x", rx->ctx.recv_ttl, rx->ctl, rx->ctx.recv_dst);
  1243. /* The Relay Retransmit state is only applied to adv-adv relaying.
  1244. * Anything else (like GATT to adv, or locally originated packets)
  1245. * use the Network Transmit state.
  1246. */
  1247. if (rx->net_if == BLE_MESH_NET_IF_ADV &&
  1248. rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED) {
  1249. xmit = bt_mesh_relay_retransmit_get();
  1250. } else {
  1251. xmit = bt_mesh_net_transmit_get();
  1252. }
  1253. #if 0
  1254. /* TODO: Needs further thinking. */
  1255. xmit = bt_mesh_net_adv_xmit_update(cred, tag, rx->ctl);
  1256. if (xmit == 0) {
  1257. return;
  1258. }
  1259. #endif
  1260. /* TODO:
  1261. * When the node tries to relay a Segment ACK message, in this case
  1262. * the corresponding segment packets (if exist) can be removed from
  1263. * the relay queue.
  1264. */
  1265. #if !CONFIG_BLE_MESH_RELAY_ADV_BUF
  1266. buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, K_NO_WAIT);
  1267. #else
  1268. /* Check if the number of relay packets in queue is too large, if so
  1269. * use minimum relay retransmit value for later relay packets.
  1270. */
  1271. if (bt_mesh_get_stored_relay_count() >= BLE_MESH_MAX_STORED_RELAY_COUNT) {
  1272. xmit = BLE_MESH_TRANSMIT(0, 20);
  1273. }
  1274. buf = bt_mesh_relay_adv_create(BLE_MESH_ADV_DATA, K_NO_WAIT);
  1275. #endif
  1276. if (!buf) {
  1277. BT_ERR("Out of relay buffers");
  1278. return;
  1279. }
  1280. /* Only decrement TTL for non-locally originated packets */
  1281. if (rx->net_if != BLE_MESH_NET_IF_LOCAL) {
  1282. /* Leave CTL bit intact */
  1283. sbuf->data[1] &= 0x80;
  1284. sbuf->data[1] |= rx->ctx.recv_ttl - 1U;
  1285. }
  1286. net_buf_add_mem(buf, sbuf->data, sbuf->len);
  1287. if (cred == BLE_MESH_FLOODING_CRED) {
  1288. enc = rx->sub->keys[rx->sub->kr_flag].enc;
  1289. priv = rx->sub->keys[rx->sub->kr_flag].privacy;
  1290. nid = rx->sub->keys[rx->sub->kr_flag].nid;
  1291. #if CONFIG_BLE_MESH_DF_SRV
  1292. } else {
  1293. enc = rx->sub->keys[rx->sub->kr_flag].direct_enc;
  1294. priv = rx->sub->keys[rx->sub->kr_flag].direct_privacy;
  1295. nid = rx->sub->keys[rx->sub->kr_flag].direct_nid;
  1296. #endif /* CONFIG_BLE_MESH_DF_SRV */
  1297. }
  1298. #if CONFIG_BLE_MESH_BRC_SRV
  1299. /* Check if subnet bridge is supported & decide which NetKey is used */
  1300. if (bt_mesh_subnet_bridge_state_get() == BLE_MESH_SUBNET_BRIDGE_ENABLED) {
  1301. netkey_changed = bt_mesh_bridge_change_net_key(rx, &enc, &priv, &nid, cred);
  1302. }
  1303. #endif
  1304. BT_DBG("Relaying packet. TTL is now %u", BLE_MESH_NET_HDR_TTL(buf->data));
  1305. /* 1. Update NID if RX or RX was with friend credentials(included by case 3).
  1306. * 2. Update NID if the net_key has changed.
  1307. * 3. Update NID if credential has changed.
  1308. */
  1309. if (netkey_changed || cred != rx->ctx.recv_cred) {
  1310. buf->data[0] &= 0x80; /* Clear everything except IVI */
  1311. buf->data[0] |= nid;
  1312. }
  1313. /* We re-encrypt and obfuscate using the received IVI rather than
  1314. * the normal TX IVI (which may be different) since the transport
  1315. * layer nonce includes the IVI.
  1316. */
  1317. if (bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_RX(rx), false, false)) {
  1318. BT_ERR("Re-encrypting failed");
  1319. goto done;
  1320. }
  1321. if (bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_RX(rx), priv)) {
  1322. BT_ERR("Re-obfuscating failed");
  1323. goto done;
  1324. }
  1325. /* Sending to the GATT bearer should only happen if GATT Proxy
  1326. * is enabled or the message originates from the local node.
  1327. *
  1328. * The output filter of the interface connected to the GATT bearer
  1329. * shall drop all messages secured using the friendship security
  1330. * credentials.
  1331. */
  1332. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
  1333. (bearer & BLE_MESH_GATT_BEARER) &&
  1334. ((bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED &&
  1335. cred != BLE_MESH_FRIENDSHIP_CRED) ||
  1336. bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED ||
  1337. rx->net_if == BLE_MESH_NET_IF_LOCAL ||
  1338. rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED)) {
  1339. if (bt_mesh_proxy_server_relay(&buf->b, rx->ctx.recv_dst) &&
  1340. BLE_MESH_ADDR_IS_UNICAST(rx->ctx.recv_dst)) {
  1341. if ((bearer & (~BLE_MESH_GATT_BEARER)) == 0) {
  1342. goto done;
  1343. }
  1344. }
  1345. }
  1346. if (((bearer & BLE_MESH_ADV_BEARER) && relay_to_adv(rx->net_if)) ||
  1347. netkey_changed ||
  1348. rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED) {
  1349. #if !CONFIG_BLE_MESH_RELAY_ADV_BUF
  1350. bt_mesh_adv_send(buf, xmit, NULL, NULL);
  1351. #else
  1352. bt_mesh_relay_adv_send(buf, xmit, rx->ctx.addr, rx->ctx.recv_dst, NULL, NULL);
  1353. #endif
  1354. }
  1355. done:
  1356. net_buf_unref(buf);
  1357. }
  1358. void bt_mesh_net_header_parse(struct net_buf_simple *buf,
  1359. struct bt_mesh_net_rx *rx)
  1360. {
  1361. rx->old_iv = (BLE_MESH_NET_HDR_IVI(buf->data) != (bt_mesh.iv_index & 0x01));
  1362. rx->ctl = BLE_MESH_NET_HDR_CTL(buf->data);
  1363. rx->ctx.recv_ttl = BLE_MESH_NET_HDR_TTL(buf->data);
  1364. rx->seq = BLE_MESH_NET_HDR_SEQ(buf->data);
  1365. rx->ctx.addr = BLE_MESH_NET_HDR_SRC(buf->data);
  1366. rx->ctx.recv_dst = BLE_MESH_NET_HDR_DST(buf->data);
  1367. }
  1368. int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if,
  1369. struct bt_mesh_net_rx *rx, struct net_buf_simple *buf)
  1370. {
  1371. if (data->len < BLE_MESH_NET_MIN_PDU_LEN) {
  1372. BT_WARN("Dropping too short mesh packet (len %u)", data->len);
  1373. BT_WARN("%s", bt_hex(data->data, data->len));
  1374. return -EINVAL;
  1375. }
  1376. if (net_if == BLE_MESH_NET_IF_ADV && check_dup(data)) {
  1377. return -EINVAL;
  1378. }
  1379. BT_DBG("%u bytes: %s", data->len, bt_hex(data->data, data->len));
  1380. rx->net_if = net_if;
  1381. if (!net_find_and_decrypt(data->data, data->len, rx, buf)) {
  1382. BT_DBG("Unable to find matching net for packet");
  1383. return -ENOENT;
  1384. }
  1385. /* Spec v1.1, Section 3.4.5.1:
  1386. * The input filter of the interface connected to the GATT
  1387. * bearer shall drop all messages that have been secured
  1388. * using the friendship security credentials.
  1389. */
  1390. if (net_if == BLE_MESH_NET_IF_PROXY &&
  1391. rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED) {
  1392. BT_WARN("Dropping packet from GATT bearer using friendship creds");
  1393. return -EBADMSG;
  1394. }
  1395. /* Initialize AppIdx to a sane value */
  1396. rx->ctx.app_idx = BLE_MESH_KEY_UNUSED;
  1397. rx->ctx.recv_ttl = BLE_MESH_NET_HDR_TTL(buf->data);
  1398. /* Default to responding with TTL 0 for non-routed messages */
  1399. if (rx->ctx.recv_ttl == 0U) {
  1400. rx->ctx.send_ttl = 0U;
  1401. } else {
  1402. rx->ctx.send_ttl = BLE_MESH_TTL_DEFAULT;
  1403. }
  1404. rx->ctl = BLE_MESH_NET_HDR_CTL(buf->data);
  1405. rx->seq = BLE_MESH_NET_HDR_SEQ(buf->data);
  1406. rx->ctx.recv_dst = BLE_MESH_NET_HDR_DST(buf->data);
  1407. BT_DBG("Decryption successful. Payload len %u", buf->len);
  1408. if (net_if != BLE_MESH_NET_IF_PROXY_CFG &&
  1409. rx->ctx.recv_dst == BLE_MESH_ADDR_UNASSIGNED) {
  1410. BT_ERR("Destination address is unassigned; dropping packet");
  1411. return -EBADMSG;
  1412. }
  1413. /* For case MESH/NODE/RLY/BV-01-C, even the DST is RFU, it needs to be forwarded. */
  1414. #if !CONFIG_BLE_MESH_BQB_TEST
  1415. if (BLE_MESH_ADDR_IS_RFU(rx->ctx.recv_dst)) {
  1416. BT_ERR("Destination address is RFU; dropping packet 0x%02x", rx->ctx.recv_dst);
  1417. return -EBADMSG;
  1418. }
  1419. #endif
  1420. if (net_if != BLE_MESH_NET_IF_LOCAL && bt_mesh_elem_find(rx->ctx.addr)) {
  1421. BT_DBG("Dropping locally originated packet");
  1422. return -EBADMSG;
  1423. }
  1424. BT_DBG("src 0x%04x dst 0x%04x ttl %u", rx->ctx.addr, rx->ctx.recv_dst,
  1425. rx->ctx.recv_ttl);
  1426. BT_DBG("PDU: %s", bt_hex(buf->data, buf->len));
  1427. msg_cache_add(rx);
  1428. return 0;
  1429. }
  1430. static bool ready_to_recv(void)
  1431. {
  1432. if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) {
  1433. return true;
  1434. }
  1435. if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) {
  1436. if (bt_mesh_provisioner_get_node_count()) {
  1437. return true;
  1438. }
  1439. }
  1440. return false;
  1441. }
  1442. static bool ignore_net_msg(uint16_t src, uint16_t dst)
  1443. {
  1444. if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) {
  1445. /* When fast provisioning is enabled, the node addr
  1446. * message will be sent to the Primary Provisioner,
  1447. * which shall not be ignored here.
  1448. */
  1449. return false;
  1450. }
  1451. if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) &&
  1452. bt_mesh_is_provisioner_en() &&
  1453. BLE_MESH_ADDR_IS_UNICAST(dst) &&
  1454. bt_mesh_elem_find(dst)) {
  1455. /* If the destination address of the message is the element
  1456. * address of Provisioner, but Provisioner fails to find the
  1457. * node in its provisioning database, then this message will
  1458. * be ignored.
  1459. * Note: extra check src here to support Provisioner sending
  1460. * messages to itself.
  1461. */
  1462. if (!bt_mesh_provisioner_get_node_with_addr(src) &&
  1463. !bt_mesh_elem_find(src)) {
  1464. BT_INFO("Not found node address 0x%04x", src);
  1465. return true;
  1466. }
  1467. }
  1468. return false;
  1469. }
  1470. void bt_mesh_net_recv(struct net_buf_simple *data, int8_t rssi,
  1471. enum bt_mesh_net_if net_if)
  1472. {
  1473. NET_BUF_SIMPLE_DEFINE(buf, 29);
  1474. struct bt_mesh_net_rx rx = { .ctx.recv_rssi = rssi };
  1475. struct net_buf_simple_state state = {0};
  1476. BT_DBG("rssi %d net_if %u", rssi, net_if);
  1477. if (!ready_to_recv()) {
  1478. return;
  1479. }
  1480. if (bt_mesh_net_decode(data, net_if, &rx, &buf)) {
  1481. return;
  1482. }
  1483. if (ignore_net_msg(rx.ctx.addr, rx.ctx.recv_dst)) {
  1484. return;
  1485. }
  1486. /* Save the state so the buffer can later be relayed */
  1487. net_buf_simple_save(&buf, &state);
  1488. BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \
  1489. BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_NET,
  1490. "\nNetRecv: ctl: %d, src: %d, dst: %d, ttl: %d, data: 0x%s",
  1491. rx.ctl, rx.ctx.addr, rx.ctx.recv_dst, rx.ctx.recv_ttl,
  1492. bt_hex(buf.data + BLE_MESH_NET_HDR_LEN, buf.len - BLE_MESH_NET_HDR_LEN));
  1493. /* If trying to handle a message with DST set to all-directed-forwarding-nodes,
  1494. * we need to make sure the directed forwarding functionality is enabled in the
  1495. * corresponding subnet.
  1496. */
  1497. rx.local_match = (bt_mesh_fixed_group_match(rx.ctx.recv_dst) ||
  1498. bt_mesh_fixed_direct_match(rx.sub, rx.ctx.recv_dst) ||
  1499. bt_mesh_elem_find(rx.ctx.recv_dst));
  1500. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
  1501. bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_DISABLED &&
  1502. net_if == BLE_MESH_NET_IF_PROXY) {
  1503. bt_mesh_proxy_server_addr_add(data, rx.ctx.addr);
  1504. if (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_DISABLED &&
  1505. !rx.local_match) {
  1506. BT_INFO("Proxy is disabled; ignoring message");
  1507. return;
  1508. }
  1509. /* If the Directed Proxy Server receives a valid Network PDU from the Directed
  1510. * Proxy Client, and the value of the Use_Directed parameter of the connection
  1511. * for the subnet identified by the Network PDU is Enabled, and the source
  1512. * address of the Network PDU is outside the unicast address range defined by
  1513. * the Proxy_Client_Address_Range parameter, then the Directed Proxy Server shall
  1514. * tag the Network PDU with the immutable-credentials tag.
  1515. */
  1516. #if CONFIG_BLE_MESH_DF_SRV
  1517. if (rx.sub->directed_proxy == BLE_MESH_DIRECTED_PROXY_ENABLED &&
  1518. rx.sub->use_directed == BLE_MESH_PROXY_USE_DIRECTED_ENABLED &&
  1519. !bt_mesh_addr_in_uar(&rx.sub->proxy_client_uar, rx.ctx.addr) &&
  1520. !bt_mesh_proxy_server_find_client_by_addr(rx.ctx.addr)) {
  1521. rx.ctx.recv_tag |= BLE_MESH_TAG_IMMUTABLE_CRED;
  1522. }
  1523. #endif /* CONFIG_BLE_MESH_DF_SRV */
  1524. }
  1525. /* The transport layer has indicated that it has rejected the message,
  1526. * but would like to see it again if it is received in the future.
  1527. * This can happen if a message is received when the device is in
  1528. * Low Power mode, but the message was not encrypted with the friend
  1529. * credentials. Remove it from the message cache so that we accept
  1530. * it again in the future.
  1531. */
  1532. if (bt_mesh_trans_recv(&buf, &rx) == -EAGAIN) {
  1533. BT_WARN("Removing rejected message from Network Message Cache");
  1534. msg_cache[rx.msg_cache_idx].src = BLE_MESH_ADDR_UNASSIGNED;
  1535. /* Rewind the next index now that we're not using this entry */
  1536. msg_cache_next = rx.msg_cache_idx;
  1537. }
  1538. /* Relay if this was a group/virtual address, or if the destination
  1539. * was neither a local element nor an LPN we're Friends for.
  1540. */
  1541. if (!BLE_MESH_ADDR_IS_UNICAST(rx.ctx.recv_dst) ||
  1542. (!rx.local_match && !rx.friend_match)) {
  1543. net_buf_simple_restore(&buf, &state);
  1544. bt_mesh_net_relay(&buf, &rx);
  1545. }
  1546. }
  1547. static void ivu_refresh(struct k_work *work)
  1548. {
  1549. bt_mesh.ivu_duration += BLE_MESH_IVU_HOURS;
  1550. BT_INFO("%s for %u hour%s",
  1551. bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) ?
  1552. "IVU in Progress" : "IVU Normal mode",
  1553. bt_mesh.ivu_duration, bt_mesh.ivu_duration == 1U ? "" : "s");
  1554. if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) {
  1555. if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  1556. bt_mesh_store_iv(true);
  1557. }
  1558. k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT);
  1559. return;
  1560. }
  1561. if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
  1562. bt_mesh_beacon_ivu_initiator(true);
  1563. bt_mesh_net_iv_update(bt_mesh.iv_index, false);
  1564. } else if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
  1565. bt_mesh_store_iv(true);
  1566. }
  1567. }
  1568. void bt_mesh_net_start(void)
  1569. {
  1570. if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) {
  1571. bt_mesh_secure_beacon_enable();
  1572. } else {
  1573. bt_mesh_secure_beacon_disable();
  1574. }
  1575. #if CONFIG_BLE_MESH_PRB_SRV
  1576. if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) {
  1577. bt_mesh_private_beacon_enable();
  1578. } else {
  1579. bt_mesh_private_beacon_disable();
  1580. }
  1581. #endif
  1582. if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&
  1583. bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_NOT_SUPPORTED) {
  1584. bt_mesh_proxy_server_gatt_enable();
  1585. bt_mesh_adv_update();
  1586. }
  1587. #if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
  1588. /* Add Mesh beacon type (Secure Network Beacon) to the exceptional list */
  1589. bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD,
  1590. BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON,
  1591. NULL);
  1592. #endif
  1593. if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
  1594. /* TODO: Enable duplicate scan in Low Power Mode */
  1595. bt_mesh_lpn_init();
  1596. } else {
  1597. bt_mesh_scan_enable();
  1598. }
  1599. if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
  1600. bt_mesh_friend_init();
  1601. }
  1602. if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) {
  1603. uint16_t net_idx = bt_mesh.sub[0].net_idx;
  1604. uint16_t addr = bt_mesh_primary_addr();
  1605. uint32_t iv_index = bt_mesh.iv_index;
  1606. uint8_t flags = (uint8_t)bt_mesh.sub[0].kr_flag;
  1607. const uint8_t *net_key = bt_mesh.sub[0].keys[flags].net;
  1608. if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) {
  1609. flags |= BLE_MESH_NET_FLAG_IVU;
  1610. }
  1611. bt_mesh_prov_complete(net_idx, net_key, addr, flags, iv_index);
  1612. }
  1613. }
  1614. void bt_mesh_net_init(void)
  1615. {
  1616. k_delayed_work_init(&bt_mesh.ivu_timer, ivu_refresh);
  1617. }
  1618. void bt_mesh_net_reset(void)
  1619. {
  1620. k_delayed_work_cancel(&bt_mesh.ivu_timer);
  1621. #if FRIEND_CRED_COUNT > 0
  1622. memset(friend_cred, 0, sizeof(friend_cred));
  1623. #endif
  1624. memset(msg_cache, 0, sizeof(msg_cache));
  1625. msg_cache_next = 0U;
  1626. memset(dup_cache, 0, sizeof(dup_cache));
  1627. dup_cache_next = 0U;
  1628. bt_mesh.iv_index = 0U;
  1629. bt_mesh.seq = 0U;
  1630. }
  1631. #if CONFIG_BLE_MESH_DEINIT
  1632. void bt_mesh_net_deinit(void)
  1633. {
  1634. bt_mesh_net_reset();
  1635. k_delayed_work_free(&bt_mesh.ivu_timer);
  1636. /* Local queue uses a while loop, currently no need
  1637. * to handle this.
  1638. */
  1639. }
  1640. #endif /* CONFIG_BLE_MESH_DEINIT */