net.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2017 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <inttypes.h>
  14. #include <ctype.h>
  15. #include <stdbool.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <glib.h>
  19. #include "src/shared/util.h"
  20. #include "src/shared/shell.h"
  21. #include "tools/mesh-gatt/crypto.h"
  22. #include "tools/mesh-gatt/gatt.h"
  23. #include "tools/mesh-gatt/mesh-net.h"
  24. #include "tools/mesh-gatt/util.h"
  25. #include "tools/mesh-gatt/keys.h"
  26. #include "tools/mesh-gatt/node.h"
  27. #include "tools/mesh-gatt/prov-db.h"
  28. #include "tools/mesh-gatt/net.h"
  29. struct address_range
  30. {
  31. uint16_t min;
  32. uint16_t max;
  33. };
  34. struct mesh_net {
  35. uint32_t iv_index;
  36. uint32_t seq_num;
  37. uint32_t seq_num_reserved;
  38. uint16_t primary_addr;
  39. uint8_t iv_upd_state;
  40. uint8_t num_elements;
  41. uint8_t default_ttl;
  42. bool iv_update;
  43. bool provisioner;
  44. bool reject_list;
  45. guint iv_update_timeout;
  46. GDBusProxy *proxy_in;
  47. GList *address_pool;
  48. GList *dest; /* List of valid local destinations for Accept List */
  49. GList *sar_in; /* Incoming segmented messages in progress */
  50. GList *msg_out; /* Pre-Network encoded, might be multi-segment */
  51. GList *pkt_out; /* Fully encoded packets awaiting Tx in order */
  52. net_mesh_session_open_callback open_cb;
  53. };
  54. struct generic_key {
  55. uint16_t idx;
  56. };
  57. struct net_key_parts {
  58. uint8_t nid;
  59. uint8_t enc_key[16];
  60. uint8_t privacy_key[16];
  61. uint8_t net_key[16];
  62. uint8_t beacon_key[16];
  63. uint8_t net_id[8];
  64. };
  65. struct mesh_net_key {
  66. struct generic_key generic;
  67. uint8_t phase;
  68. struct net_key_parts current;
  69. struct net_key_parts new;
  70. };
  71. struct app_key_parts {
  72. uint8_t key[16];
  73. uint8_t akf_aid;
  74. };
  75. struct mesh_app_key {
  76. struct generic_key generic;
  77. uint16_t net_idx;
  78. struct app_key_parts current;
  79. struct app_key_parts new;
  80. };
  81. struct mesh_virt_addr {
  82. uint16_t va16;
  83. uint32_t va32;
  84. uint8_t va128[16];
  85. };
  86. struct mesh_pkt {
  87. uint8_t data[30];
  88. uint8_t len;
  89. };
  90. struct mesh_sar_msg {
  91. guint ack_to;
  92. guint msg_to;
  93. uint32_t iv_index;
  94. uint32_t seqAuth;
  95. uint32_t ack;
  96. uint32_t dst;
  97. uint16_t src;
  98. uint16_t net_idx;
  99. uint16_t len;
  100. uint8_t akf_aid;
  101. uint8_t ttl;
  102. uint8_t segN;
  103. uint8_t activity_cnt;
  104. bool ctl;
  105. bool segmented;
  106. bool szmic;
  107. bool proxy;
  108. uint8_t data[20]; /* Open ended, min 20 */
  109. };
  110. struct mesh_destination {
  111. uint16_t cnt;
  112. uint16_t dst;
  113. };
  114. /* Network Packet Layer based Offsets */
  115. #define AKF_BIT 0x40
  116. #define PKT_IVI(p) !!((p)[0] & 0x80)
  117. #define SET_PKT_IVI(p,v) do {(p)[0] &= 0x7f; \
  118. (p)[0] |= ((v) ? 0x80 : 0);} while(0)
  119. #define PKT_NID(p) ((p)[0] & 0x7f)
  120. #define SET_PKT_NID(p,v) do {(p)[0] &= 0x80; (p)[0] |= (v);} while(0)
  121. #define PKT_CTL(p) (!!((p)[1] & 0x80))
  122. #define SET_PKT_CTL(p,v) do {(p)[1] &= 0x7f; \
  123. (p)[1] |= ((v) ? 0x80 : 0);} while(0)
  124. #define PKT_TTL(p) ((p)[1] & 0x7f)
  125. #define SET_PKT_TTL(p,v) do {(p)[1] &= 0x80; (p)[1] |= (v);} while(0)
  126. #define PKT_SEQ(p) (get_be32((p) + 1) & 0xffffff)
  127. #define SET_PKT_SEQ(p,v) put_be32(((p)[1] << 24) + ((v) & 0xffffff), \
  128. (p) + 1)
  129. #define PKT_SRC(p) get_be16((p) + 5)
  130. #define SET_PKT_SRC(p,v) put_be16(v, (p) + 5)
  131. #define PKT_DST(p) get_be16((p) + 7)
  132. #define SET_PKT_DST(p,v) put_be16(v, (p) + 7)
  133. #define PKT_TRANS(p) ((p) + 9)
  134. #define PKT_TRANS_LEN(l) ((l) - 9)
  135. #define PKT_SEGMENTED(p) (!!((p)[9] & 0x80))
  136. #define SET_PKT_SEGMENTED(p,v) do {(p)[9] &= 0x7f; \
  137. (p)[9] |= ((v) ? 0x80 : 0);} while(0)
  138. #define PKT_AKF_AID(p) ((p)[9] & 0x7f)
  139. #define SET_PKT_AKF_AID(p,v) do {(p)[9] &= 0x80; (p)[9] |= (v);} while(0)
  140. #define PKT_OPCODE(p) ((p)[9] & 0x7f)
  141. #define SET_PKT_OPCODE(p,v) do {(p)[9] &= 0x80; (p)[9] |= (v);} while(0)
  142. #define PKT_OBO(p) (!!((p)[10] & 0x80))
  143. #define PKT_SZMIC(p) (!!(PKT_SEGMENTED(p) ? ((p)[10] & 0x40) : 0))
  144. #define SET_PKT_SZMIC(p,v) do {(p)[10] &= 0x7f; \
  145. (p)[10] |= ((v) ? 0x80 : 0);} while(0)
  146. #define PKT_SEQ0(p) ((get_be16((p) + 10) >> 2) & 0x1fff)
  147. #define SET_PKT_SEQ0(p,v) do {put_be16((get_be16((p) + 10) & 0x8003) \
  148. | (((v) & 0x1fff) << 2), \
  149. (p) + 10);} while(0)
  150. #define SET_PKT_SEGO(p,v) do {put_be16((get_be16( \
  151. (p) + 11) & 0xfc1f) | ((v) << 5), \
  152. (p) + 11);} while(0)
  153. #define SET_PKT_SEGN(p,v) do {(p)[12] = ((p)[12] & 0xe0) | (v);} while(0)
  154. #define PKT_ACK(p) (get_be32((p) + 12))
  155. #define SET_PKT_ACK(p,v) (put_be32((v)(p) + 12))
  156. /* Transport Layer based offsets */
  157. #define TRANS_SEGMENTED(t) (!!((t)[0] & 0x80))
  158. #define SET_TRANS_SEGMENTD(t,v) do {(t)[0] &= 0x7f; \
  159. (t)[0] |= ((v) ? 0x80 : 0);} while(0)
  160. #define TRANS_OPCODE(t) ((t)[0] & 0x7f)
  161. #define SET_TRANS_OPCODE(t,v) do {(t)[0] &= 0x80; (t)[0] |= (v);} while(0)
  162. #define TRANS_AKF_AID(t) ((t)[0] & 0x7f)
  163. #define SET_TRANS_AKF_AID(t,v) do {(t)[0] &= 0xc0; (t)[0] |= (v);} while(0)
  164. #define TRANS_AKF(t) (!!((t)[0] & AKF_BIT))
  165. #define TRANS_SZMIC(t) (!!(TRANS_SEGMENTED(t) ? ((t)[1] & 0x80) : 0))
  166. #define TRANS_SEQ0(t) ((get_be16((t) + 1) >> 2) & 0x1fff)
  167. #define SET_TRANS_SEQ0(t,v) do {put_be16((get_be16((t) + 1) & 0x8003) \
  168. | (((v) & 0x1fff) << 2), \
  169. (t) + 1);} while(0)
  170. #define SET_TRANS_ACK(t,v) put_be32((v), (t) + 3)
  171. #define TRANS_SEGO(t) ((get_be16((t) + 2) >> 5) & 0x1f)
  172. #define TRANS_SEGN(t) ((t)[3] & 0x1f)
  173. #define TRANS_PAYLOAD(t) ((t) + (TRANS_SEGMENTED(t) ? 4 : 1))
  174. #define TRANS_LEN(t,l) ((l) -(TRANS_SEGMENTED(t) ? 4 : 1))
  175. /* Proxy Config Opcodes */
  176. #define FILTER_SETUP 0x00
  177. #define FILTER_ADD 0x01
  178. #define FILTER_DEL 0x02
  179. #define FILTER_STATUS 0x03
  180. /* Proxy Filter Types */
  181. #define ACCEPT_LIST_FILTER 0x00
  182. #define REJECT_LIST_FILTER 0x01
  183. /* IV Updating states for timing enforcement */
  184. #define IV_UPD_INIT 0
  185. #define IV_UPD_NORMAL 1
  186. #define IV_UPD_UPDATING 2
  187. #define IV_UPD_NORMAL_HOLD 3
  188. #define IV_IDX_DIFF_RANGE 42
  189. static struct mesh_net net;
  190. static GList *virt_addrs = NULL;
  191. static GList *net_keys = NULL;
  192. static GList *app_keys = NULL;
  193. /* Forward static declarations */
  194. static void resend_segs(struct mesh_sar_msg *sar);
  195. static int match_net_id(const void *a, const void *net_id)
  196. {
  197. const struct mesh_net_key *net_key = a;
  198. if (net_key->current.nid != 0xff &&
  199. !memcmp(net_key->current.net_id, net_id, 8))
  200. return 0;
  201. if (net_key->new.nid != 0xff &&
  202. !memcmp(net_key->new.net_id, net_id, 8))
  203. return 0;
  204. return -1;
  205. }
  206. static struct mesh_net_key *find_net_key_by_id(const uint8_t *net_id)
  207. {
  208. GList *l;
  209. l = g_list_find_custom(net_keys, net_id, match_net_id);
  210. if (!l)
  211. return NULL;
  212. return l->data;
  213. }
  214. uint16_t net_validate_proxy_beacon(const uint8_t *proxy_beacon)
  215. {
  216. struct mesh_net_key *net_key = find_net_key_by_id(proxy_beacon);
  217. if (net_key == NULL)
  218. return NET_IDX_INVALID;
  219. return net_key->generic.idx;
  220. }
  221. static int match_sar_dst(const void *a, const void *b)
  222. {
  223. const struct mesh_sar_msg *sar = a;
  224. uint16_t dst = GPOINTER_TO_UINT(b);
  225. return (sar->dst == dst) ? 0 : -1;
  226. }
  227. static struct mesh_sar_msg *find_sar_out_by_dst(uint16_t dst)
  228. {
  229. GList *l;
  230. l = g_list_find_custom(net.msg_out, GUINT_TO_POINTER(dst),
  231. match_sar_dst);
  232. if (!l)
  233. return NULL;
  234. return l->data;
  235. }
  236. static int match_sar_src(const void *a, const void *b)
  237. {
  238. const struct mesh_sar_msg *sar = a;
  239. uint16_t src = GPOINTER_TO_UINT(b);
  240. return (sar->src == src) ? 0 : -1;
  241. }
  242. static struct mesh_sar_msg *find_sar_in_by_src(uint16_t src)
  243. {
  244. GList *l;
  245. l = g_list_find_custom(net.sar_in, GUINT_TO_POINTER(src),
  246. match_sar_src);
  247. if (!l)
  248. return NULL;
  249. return l->data;
  250. }
  251. static int match_key_index(const void *a, const void *b)
  252. {
  253. const struct generic_key *generic = a;
  254. uint16_t index = GPOINTER_TO_UINT(b);
  255. return (generic->idx == index) ? 0 : -1;
  256. }
  257. static bool delete_key(GList **list, uint16_t index)
  258. {
  259. GList *l;
  260. l = g_list_find_custom(*list, GUINT_TO_POINTER(index),
  261. match_key_index);
  262. if (!l)
  263. return false;
  264. *list = g_list_delete_link(*list, l);
  265. return true;
  266. }
  267. static uint8_t *get_key(GList *list, uint16_t index)
  268. {
  269. GList *l;
  270. struct mesh_app_key *app_key;
  271. struct mesh_net_key *net_key;
  272. l = g_list_find_custom(list, GUINT_TO_POINTER(index),
  273. match_key_index);
  274. if (!l) return NULL;
  275. if (list == app_keys) {
  276. app_key = l->data;
  277. /* All App Keys must belong to a valid Net Key */
  278. l = g_list_find_custom(net_keys,
  279. GUINT_TO_POINTER(app_key->net_idx),
  280. match_key_index);
  281. if (!l) return NULL;
  282. net_key = l->data;
  283. if (net_key->phase == 2 && app_key->new.akf_aid != 0xff)
  284. return app_key->new.key;
  285. if (app_key->current.akf_aid != 0xff)
  286. return app_key->current.key;
  287. return NULL;
  288. }
  289. net_key = l->data;
  290. if (net_key->phase == 2 && net_key->new.nid != 0xff)
  291. return net_key->new.net_key;
  292. if (net_key->current.nid != 0xff)
  293. return net_key->current.net_key;
  294. return NULL;
  295. }
  296. bool keys_app_key_add(uint16_t net_idx, uint16_t app_idx, uint8_t *key,
  297. bool update)
  298. {
  299. struct mesh_app_key *app_key = NULL;
  300. uint8_t akf_aid;
  301. GList *l = g_list_find_custom(app_keys, GUINT_TO_POINTER(app_idx),
  302. match_key_index);
  303. if (!mesh_crypto_k4(key, &akf_aid))
  304. return false;
  305. akf_aid |= AKF_BIT;
  306. if (l && update) {
  307. app_key = l->data;
  308. if (app_key->net_idx != net_idx)
  309. return false;
  310. memcpy(app_key->new.key, key, 16);
  311. app_key->new.akf_aid = akf_aid;
  312. } else if (l) {
  313. app_key = l->data;
  314. if (memcmp(app_key->current.key, key, 16) ||
  315. app_key->net_idx != net_idx)
  316. return false;
  317. } else {
  318. app_key = g_new(struct mesh_app_key, 1);
  319. memcpy(app_key->current.key, key, 16);
  320. app_key->net_idx = net_idx;
  321. app_key->generic.idx = app_idx;
  322. app_key->current.akf_aid = akf_aid;
  323. /* Invalidate "New" version */
  324. app_key->new.akf_aid = 0xff;
  325. app_keys = g_list_append(app_keys, app_key);
  326. }
  327. return true;
  328. }
  329. bool keys_net_key_add(uint16_t net_idx, uint8_t *key, bool update)
  330. {
  331. struct mesh_net_key *net_key = NULL;
  332. uint8_t p = 0;
  333. GList *l = g_list_find_custom(net_keys, GUINT_TO_POINTER(net_idx),
  334. match_key_index);
  335. if (l && update) {
  336. bool result;
  337. net_key = l->data;
  338. memcpy(net_key->new.net_key, key, 16);
  339. /* Calculate the many component parts */
  340. result = mesh_crypto_nkbk(key, net_key->new.beacon_key);
  341. if (!result)
  342. return false;
  343. result = mesh_crypto_k3(key, net_key->new.net_id);
  344. if (!result)
  345. return false;
  346. result = mesh_crypto_k2(key, &p, 1,
  347. &net_key->new.nid,
  348. net_key->new.enc_key,
  349. net_key->new.privacy_key);
  350. if (!result)
  351. net_key->new.nid = 0xff;
  352. return result;
  353. } else if (l) {
  354. net_key = l->data;
  355. if (memcmp(net_key->current.net_key, key, 16))
  356. return false;
  357. } else {
  358. bool result;
  359. net_key = g_new(struct mesh_net_key, 1);
  360. memcpy(net_key->current.net_key, key, 16);
  361. net_key->generic.idx = net_idx;
  362. /* Invalidate "New" version */
  363. net_key->new.nid = 0xff;
  364. /* Calculate the many component parts */
  365. result = mesh_crypto_nkbk(key, net_key->current.beacon_key);
  366. if (!result) {
  367. g_free(net_key);
  368. return false;
  369. }
  370. result = mesh_crypto_k3(key, net_key->current.net_id);
  371. if (!result) {
  372. g_free(net_key);
  373. return false;
  374. }
  375. result = mesh_crypto_k2(key, &p, 1,
  376. &net_key->current.nid,
  377. net_key->current.enc_key,
  378. net_key->current.privacy_key);
  379. if (!result) {
  380. g_free(net_key);
  381. return false;
  382. }
  383. net_keys = g_list_append(net_keys, net_key);
  384. }
  385. return true;
  386. }
  387. static struct mesh_app_key *find_app_key_by_idx(uint16_t app_idx)
  388. {
  389. GList *l;
  390. l = g_list_find_custom(app_keys, GUINT_TO_POINTER(app_idx),
  391. match_key_index);
  392. if (!l) return NULL;
  393. return l->data;
  394. }
  395. static struct mesh_net_key *find_net_key_by_idx(uint16_t net_idx)
  396. {
  397. GList *l;
  398. l = g_list_find_custom(net_keys, GUINT_TO_POINTER(net_idx),
  399. match_key_index);
  400. if (!l) return NULL;
  401. return l->data;
  402. }
  403. static int match_virt_dst(const void *a, const void *b)
  404. {
  405. const struct mesh_virt_addr *virt = a;
  406. uint32_t dst = GPOINTER_TO_UINT(b);
  407. if (dst < 0x10000 && dst == virt->va16)
  408. return 0;
  409. if (dst == virt->va32)
  410. return 0;
  411. return -1;
  412. }
  413. static struct mesh_virt_addr *find_virt_by_dst(uint32_t dst)
  414. {
  415. GList *l;
  416. l = g_list_find_custom(virt_addrs, GUINT_TO_POINTER(dst),
  417. match_virt_dst);
  418. if (!l) return NULL;
  419. return l->data;
  420. }
  421. uint8_t *keys_net_key_get(uint16_t net_idx, bool current)
  422. {
  423. GList *l;
  424. l = g_list_find_custom(net_keys, GUINT_TO_POINTER(net_idx),
  425. match_key_index);
  426. if (!l) {
  427. return NULL;
  428. } else {
  429. struct mesh_net_key *key = l->data;
  430. if (current)
  431. return key->current.net_key;
  432. else
  433. return key->new.net_key;
  434. }
  435. }
  436. bool keys_app_key_delete(uint16_t app_idx)
  437. {
  438. /* TODO: remove all associated bindings */
  439. return delete_key(&app_keys, app_idx);
  440. }
  441. bool keys_net_key_delete(uint16_t net_idx)
  442. {
  443. /* TODO: remove all associated app keys and bindings */
  444. return delete_key(&net_keys, net_idx);
  445. }
  446. uint8_t keys_get_kr_phase(uint16_t net_idx)
  447. {
  448. GList *l;
  449. struct mesh_net_key *key;
  450. l = g_list_find_custom(net_keys, GUINT_TO_POINTER(net_idx),
  451. match_key_index);
  452. if (!l)
  453. return KR_PHASE_INVALID;
  454. key = l->data;
  455. return key->phase;
  456. }
  457. bool keys_set_kr_phase(uint16_t index, uint8_t phase)
  458. {
  459. GList *l;
  460. struct mesh_net_key *net_key;
  461. l = g_list_find_custom(net_keys, GUINT_TO_POINTER(index),
  462. match_key_index);
  463. if (!l)
  464. return false;
  465. net_key = l->data;
  466. net_key->phase = phase;
  467. return true;
  468. }
  469. uint16_t keys_app_key_get_bound(uint16_t app_idx)
  470. {
  471. GList *l;
  472. l = g_list_find_custom(app_keys, GUINT_TO_POINTER(app_idx),
  473. match_key_index);
  474. if (!l)
  475. return NET_IDX_INVALID;
  476. else {
  477. struct mesh_app_key *key = l->data;
  478. return key->net_idx;
  479. }
  480. }
  481. uint8_t *keys_app_key_get(uint16_t app_idx, bool current)
  482. {
  483. GList *l;
  484. l = g_list_find_custom(app_keys, GUINT_TO_POINTER(app_idx),
  485. match_key_index);
  486. if (!l) {
  487. return NULL;
  488. } else {
  489. struct mesh_app_key *key = l->data;
  490. if (current)
  491. return key->current.key;
  492. else
  493. return key->new.key;
  494. }
  495. }
  496. void keys_cleanup_all(void)
  497. {
  498. g_list_free_full(app_keys, g_free);
  499. g_list_free_full(net_keys, g_free);
  500. app_keys = net_keys = NULL;
  501. }
  502. bool net_get_key(uint16_t net_idx, uint8_t *key)
  503. {
  504. uint8_t *buf;
  505. buf = get_key(net_keys, net_idx);
  506. if (!buf)
  507. return false;
  508. memcpy(key, buf, 16);
  509. return true;
  510. }
  511. bool net_get_flags(uint16_t net_idx, uint8_t *out_flags)
  512. {
  513. uint8_t phase;
  514. phase = keys_get_kr_phase(net_idx);
  515. if (phase == KR_PHASE_INVALID || !out_flags)
  516. return false;
  517. if (phase != KR_PHASE_NONE)
  518. *out_flags = 0x01;
  519. else
  520. *out_flags = 0x00;
  521. if (net.iv_update)
  522. *out_flags |= 0x02;
  523. return true;
  524. }
  525. uint32_t net_get_iv_index(bool *update)
  526. {
  527. if (update)
  528. *update = net.iv_update;
  529. return net.iv_index;
  530. }
  531. void net_set_iv_index(uint32_t iv_index, bool update)
  532. {
  533. net.iv_index = iv_index;
  534. net.iv_update = update;
  535. }
  536. void set_sequence_number(uint32_t seq_num)
  537. {
  538. net.seq_num = seq_num;
  539. }
  540. uint32_t get_sequence_number(void)
  541. {
  542. return net.seq_num;
  543. }
  544. bool net_add_address_pool(uint16_t min, uint16_t max)
  545. {
  546. uint32_t range;
  547. if (max < min)
  548. return false;
  549. range = min + (max << 16);
  550. net.address_pool = g_list_append(net.address_pool,
  551. GUINT_TO_POINTER(range));
  552. return true;
  553. }
  554. static int match_address_range(const void *a, const void *b)
  555. {
  556. uint32_t range = GPOINTER_TO_UINT(a);
  557. uint8_t num_elements = (uint8_t) (GPOINTER_TO_UINT(b));
  558. uint16_t max = range >> 16;
  559. uint16_t min = range & 0xffff;
  560. return ((max - min + 1) >= num_elements) ? 0 : -1;
  561. }
  562. uint16_t net_obtain_address(uint8_t num_eles)
  563. {
  564. uint16_t addr;
  565. GList *l;
  566. l = g_list_find_custom(net.address_pool, GUINT_TO_POINTER(num_eles),
  567. match_address_range);
  568. if (l) {
  569. uint32_t range = GPOINTER_TO_UINT(l->data);
  570. uint16_t max = range >> 16;
  571. uint16_t min = range & 0xffff;
  572. addr = min;
  573. min += num_eles;
  574. if (min > max) {
  575. net.address_pool = g_list_delete_link(net.address_pool,
  576. l);
  577. } else {
  578. range = min + (max << 16);
  579. l->data = GUINT_TO_POINTER(range);
  580. }
  581. return addr;
  582. }
  583. return UNASSIGNED_ADDRESS;
  584. }
  585. static int range_cmp(const void *a, const void *b)
  586. {
  587. uint32_t range1 = GPOINTER_TO_UINT(a);
  588. uint32_t range2 = GPOINTER_TO_UINT(b);
  589. return range2 - range1;
  590. }
  591. void net_release_address(uint16_t addr, uint8_t num_elements)
  592. {
  593. GList *l;
  594. uint32_t range;
  595. if (addr == UNASSIGNED_ADDRESS)
  596. return;
  597. for (l = net.address_pool; l != NULL; l = l->next)
  598. {
  599. uint16_t max;
  600. uint16_t min;
  601. GList *l1 = l->next;
  602. range = GPOINTER_TO_UINT(l->data);
  603. max = range >> 16;
  604. min = range & 0xffff;
  605. if (min == (addr + num_elements))
  606. min = addr;
  607. else if (max == (addr - 1))
  608. max = addr + num_elements;
  609. else
  610. continue;
  611. /* Check if range pools need to be merged */
  612. if (l1) {
  613. uint16_t min1;
  614. range = GPOINTER_TO_UINT(l1->data);
  615. min1 = range & 0xffff;
  616. if (min1 == (max + 1)) {
  617. max = range >> 16;
  618. l->next = l1->next;
  619. net.address_pool = g_list_delete_link(
  620. net.address_pool, l1);
  621. }
  622. }
  623. range = min + (max << 16);
  624. l->data = GUINT_TO_POINTER(range);
  625. return;
  626. }
  627. range = addr + ((addr + num_elements - 1) << 16);
  628. net.address_pool = g_list_insert_sorted(net.address_pool,
  629. GUINT_TO_POINTER(range),
  630. range_cmp);
  631. }
  632. bool net_reserve_address_range(uint16_t base, uint8_t num_elements)
  633. {
  634. GList *l;
  635. uint32_t range;
  636. uint16_t max;
  637. uint16_t min;
  638. bool shrink;
  639. for (l = net.address_pool; l != NULL; l = l->next) {
  640. range = GPOINTER_TO_UINT(l->data);
  641. max = range >> 16;
  642. min = range & 0xffff;
  643. if (base >= min && (base + num_elements - 1) <= max)
  644. break;
  645. }
  646. if (!l)
  647. return false;
  648. net.address_pool = g_list_delete_link(net.address_pool, l);
  649. shrink = false;
  650. if (base == min) {
  651. shrink = true;
  652. min = base + num_elements;
  653. }
  654. if (max == base + num_elements - 1) {
  655. shrink = true;
  656. max -= num_elements;
  657. }
  658. if (min > max)
  659. return true;
  660. if (shrink)
  661. range = min + (max << 16);
  662. else
  663. range = min + ((base - 1) << 16);
  664. net.address_pool = g_list_insert_sorted(net.address_pool,
  665. GUINT_TO_POINTER(range),
  666. range_cmp);
  667. if (shrink)
  668. return true;
  669. range = (base + num_elements) + (max << 16);
  670. net.address_pool = g_list_insert_sorted(net.address_pool,
  671. GUINT_TO_POINTER(range),
  672. range_cmp);
  673. return true;
  674. }
  675. static int match_destination(const void *a, const void *b)
  676. {
  677. const struct mesh_destination *dest = a;
  678. uint16_t dst = GPOINTER_TO_UINT(b);
  679. return (dest->dst == dst) ? 0 : -1;
  680. }
  681. void net_dest_ref(uint16_t dst)
  682. {
  683. struct mesh_destination *dest;
  684. GList *l;
  685. if (!dst) return;
  686. l = g_list_find_custom(net.dest, GUINT_TO_POINTER(dst),
  687. match_destination);
  688. if (l) {
  689. dest = l->data;
  690. dest->cnt++;
  691. return;
  692. }
  693. dest = g_new0(struct mesh_destination, 1);
  694. dest->dst = dst;
  695. dest->cnt++;
  696. net.dest = g_list_append(net.dest, dest);
  697. }
  698. void net_dest_unref(uint16_t dst)
  699. {
  700. struct mesh_destination *dest;
  701. GList *l;
  702. l = g_list_find_custom(net.dest, GUINT_TO_POINTER(dst),
  703. match_destination);
  704. if (!l)
  705. return;
  706. dest = l->data;
  707. dest->cnt--;
  708. if (dest->cnt == 0) {
  709. net.dest = g_list_remove(net.dest, dest);
  710. g_free(dest);
  711. }
  712. }
  713. struct build_accept_list {
  714. uint8_t len;
  715. uint8_t data[12];
  716. };
  717. static void accept_filter_add(gpointer data, gpointer user_data)
  718. {
  719. struct mesh_destination *dest = data;
  720. struct build_accept_list *accept = user_data;
  721. if (accept->len == 0)
  722. accept->data[accept->len++] = FILTER_ADD;
  723. put_be16(dest->dst, accept->data + accept->len);
  724. accept->len += 2;
  725. if (accept->len > (sizeof(accept->data) - sizeof(uint16_t))) {
  726. net_ctl_msg_send(0, 0, 0, accept->data, accept->len);
  727. accept->len = 0;
  728. }
  729. }
  730. static void setup_accept_list(void)
  731. {
  732. struct build_accept_list accept;
  733. accept.len = 0;
  734. /* Enable (and Clear) Proxy Accept List */
  735. accept.data[accept.len++] = FILTER_SETUP;
  736. accept.data[accept.len++] = ACCEPT_LIST_FILTER;
  737. net_ctl_msg_send(0, 0, 0, accept.data, accept.len);
  738. accept.len = 0;
  739. g_list_foreach(net.dest, accept_filter_add, &accept);
  740. if (accept.len)
  741. net_ctl_msg_send(0, 0, 0, accept.data, accept.len);
  742. }
  743. static void beacon_update(bool first, bool iv_update, uint32_t iv_index)
  744. {
  745. /* Enforcement of 96 hour and 192 hour IVU time windows */
  746. if (iv_update && !net.iv_update) {
  747. bt_shell_printf("iv_upd_state = IV_UPD_UPDATING\n");
  748. net.iv_upd_state = IV_UPD_UPDATING;
  749. /* TODO: Start timer to enforce IV Update parameters */
  750. } else if (first) {
  751. if (iv_update)
  752. net.iv_upd_state = IV_UPD_UPDATING;
  753. else
  754. net.iv_upd_state = IV_UPD_NORMAL;
  755. bt_shell_printf("iv_upd_state = IV_UPD_%s\n",
  756. iv_update ? "UPDATING" : "NORMAL");
  757. } else if (iv_update && iv_index != net.iv_index) {
  758. bt_shell_printf("IV Update too soon -- Rejecting\n");
  759. return;
  760. }
  761. if (iv_index > net.iv_index ||
  762. iv_update != net.iv_update) {
  763. /* Don't reset our seq_num unless
  764. * we start using new iv_index */
  765. if (!(iv_update && (net.iv_index + 1 == iv_index))) {
  766. net.seq_num = 0;
  767. net.seq_num_reserved = 100;
  768. }
  769. }
  770. if (!net.seq_num || net.iv_index != iv_index ||
  771. net.iv_update != iv_update) {
  772. if (net.seq_num_reserved <= net.seq_num)
  773. net.seq_num_reserved = net.seq_num + 100;
  774. prov_db_local_set_iv_index(iv_index, iv_update,
  775. net.provisioner);
  776. prov_db_local_set_seq_num(net.seq_num_reserved);
  777. }
  778. net.iv_index = iv_index;
  779. net.iv_update = iv_update;
  780. if (first) {
  781. /* Must be done once per Proxy Connection after Beacon RXed */
  782. setup_accept_list();
  783. if (net.open_cb)
  784. net.open_cb(0);
  785. }
  786. }
  787. static bool process_beacon(uint8_t *data, uint8_t size)
  788. {
  789. struct mesh_net_key *net_key;
  790. struct net_key_parts *key_part;
  791. bool rxed_iv_update, rxed_key_refresh, iv_update;
  792. bool my_krf;
  793. uint32_t rxed_iv_index, iv_index;
  794. uint64_t cmac;
  795. if (size != 22)
  796. return false;
  797. rxed_key_refresh = (data[1] & 0x01) == 0x01;
  798. iv_update = rxed_iv_update = (data[1] & 0x02) == 0x02;
  799. iv_index = rxed_iv_index = get_be32(data + 10);
  800. /* Inhibit recognizing iv_update true-->false
  801. * if we have outbound SAR messages in flight */
  802. if (net.msg_out != NULL) {
  803. if (net.iv_update && !rxed_iv_update)
  804. iv_update = true;
  805. }
  806. /* Don't bother going further if nothing has changed */
  807. if (iv_index == net.iv_index && iv_update == net.iv_update &&
  808. net.iv_upd_state != IV_UPD_INIT)
  809. return true;
  810. /* Find key we are using for SNBs */
  811. net_key = find_net_key_by_id(data + 2);
  812. if (net_key == NULL)
  813. return false;
  814. /* We are Provisioner, and control the key_refresh flag */
  815. if (rxed_key_refresh != !!(net_key->phase == 2))
  816. return false;
  817. if (net_key->phase != 2) {
  818. my_krf = false;
  819. key_part = &net_key->current;
  820. } else {
  821. my_krf = true;
  822. key_part = &net_key->new;
  823. }
  824. /* Ignore for incorrect KR state */
  825. if (memcmp(key_part->net_id, data + 2, 8))
  826. return false;
  827. if ((net.iv_index + IV_IDX_DIFF_RANGE < iv_index) ||
  828. (iv_index < net.iv_index)) {
  829. bt_shell_printf("iv index outside range\n");
  830. return false;
  831. }
  832. /* Any behavioral changes must pass CMAC test */
  833. if (!mesh_crypto_beacon_cmac(key_part->beacon_key, key_part->net_id,
  834. rxed_iv_index, my_krf,
  835. rxed_iv_update, &cmac)) {
  836. return false;
  837. }
  838. if (cmac != get_be64(data + 14))
  839. return false;
  840. if (iv_update && (net.iv_upd_state > IV_UPD_UPDATING)) {
  841. if (iv_index != net.iv_index) {
  842. bt_shell_printf("Update too soon -- Rejecting\n");
  843. }
  844. /* Silently ignore old beacons */
  845. return true;
  846. }
  847. beacon_update(net.iv_upd_state == IV_UPD_INIT, iv_update, iv_index);
  848. return true;
  849. }
  850. struct decode_params {
  851. struct mesh_net_key *net_key;
  852. uint8_t *packet;
  853. uint32_t iv_index;
  854. uint8_t size;
  855. bool proxy;
  856. };
  857. static void try_decode(gpointer data, gpointer user_data)
  858. {
  859. struct mesh_net_key *net_key = data;
  860. struct decode_params *decode = user_data;
  861. uint8_t nid = decode->packet[0] & 0x7f;
  862. uint8_t tmp[29];
  863. bool status = false;
  864. if (decode->net_key)
  865. return;
  866. if (net_key->current.nid == nid)
  867. status = mesh_crypto_packet_decode(decode->packet,
  868. decode->size, decode->proxy, tmp,
  869. decode->iv_index,
  870. net_key->current.enc_key,
  871. net_key->current.privacy_key);
  872. if (!status && net_key->new.nid == nid)
  873. status = mesh_crypto_packet_decode(decode->packet,
  874. decode->size, decode->proxy, tmp,
  875. decode->iv_index,
  876. net_key->new.enc_key,
  877. net_key->new.privacy_key);
  878. if (status) {
  879. decode->net_key = net_key;
  880. memcpy(decode->packet, tmp, decode->size);
  881. return;
  882. }
  883. }
  884. static struct mesh_net_key *net_packet_decode(bool proxy, uint32_t iv_index,
  885. uint8_t *packet, uint8_t size)
  886. {
  887. struct decode_params decode = {
  888. .proxy = proxy,
  889. .iv_index = iv_index,
  890. .packet = packet,
  891. .size = size,
  892. .net_key = NULL,
  893. };
  894. g_list_foreach(net_keys, try_decode, &decode);
  895. return decode.net_key;
  896. }
  897. static void flush_sar(GList **list, struct mesh_sar_msg *sar)
  898. {
  899. *list = g_list_remove(*list, sar);
  900. if (sar->msg_to)
  901. g_source_remove(sar->msg_to);
  902. if (sar->ack_to)
  903. g_source_remove(sar->ack_to);
  904. g_free(sar);
  905. }
  906. static void flush_sar_list(GList **list)
  907. {
  908. struct mesh_sar_msg *sar;
  909. GList *l = g_list_first(*list);
  910. while (l) {
  911. sar = l->data;
  912. flush_sar(list, sar);
  913. l = g_list_first(*list);
  914. }
  915. }
  916. static void flush_pkt_list(GList **list)
  917. {
  918. struct mesh_pkt *pkt;
  919. GList *l = g_list_first(*list);
  920. while (l) {
  921. pkt = l->data;
  922. *list = g_list_remove(*list, pkt);
  923. g_free(pkt);
  924. l = g_list_first(*list);
  925. }
  926. }
  927. static void resend_unacked_segs(gpointer data, gpointer user_data)
  928. {
  929. struct mesh_sar_msg *sar = data;
  930. if (sar->activity_cnt)
  931. resend_segs(sar);
  932. }
  933. static void send_pkt_cmplt(DBusMessage *message, void *user_data)
  934. {
  935. struct mesh_pkt *pkt = user_data;
  936. GList *l = g_list_first(net.pkt_out);
  937. if (l && user_data == l->data) {
  938. net.pkt_out = g_list_delete_link(net.pkt_out, l);
  939. g_free(pkt);
  940. } else {
  941. /* This is a serious error, and probable memory leak */
  942. bt_shell_printf("ERR: send_pkt_cmplt %p not head of queue\n", pkt);
  943. }
  944. l = g_list_first(net.pkt_out);
  945. if (l == NULL) {
  946. /* If queue is newly empty, resend all SAR outbound packets */
  947. g_list_foreach(net.msg_out, resend_unacked_segs, NULL);
  948. return;
  949. }
  950. pkt = l->data;
  951. mesh_gatt_write(net.proxy_in, pkt->data, pkt->len,
  952. send_pkt_cmplt, pkt);
  953. }
  954. static void send_mesh_pkt(struct mesh_pkt *pkt)
  955. {
  956. bool queued = !!(net.pkt_out);
  957. net.pkt_out = g_list_append(net.pkt_out, pkt);
  958. if (queued)
  959. return;
  960. mesh_gatt_write(net.proxy_in, pkt->data, pkt->len,
  961. send_pkt_cmplt, pkt);
  962. }
  963. static uint32_t get_next_seq()
  964. {
  965. uint32_t this_seq = net.seq_num++;
  966. if (net.seq_num + 32 >= net.seq_num_reserved) {
  967. net.seq_num_reserved = net.seq_num + 100;
  968. prov_db_local_set_seq_num(net.seq_num_reserved);
  969. }
  970. return this_seq;
  971. }
  972. static void send_seg(struct mesh_sar_msg *sar, uint8_t seg)
  973. {
  974. struct mesh_net_key *net_key;
  975. struct net_key_parts *part;
  976. struct mesh_pkt *pkt;
  977. uint8_t *data;
  978. net_key = find_net_key_by_idx(sar->net_idx);
  979. if (net_key == NULL)
  980. return;
  981. /* Choose which components to use to secure pkt */
  982. if (net_key->phase == 2 && net_key->new.nid != 0xff)
  983. part = &net_key->new;
  984. else
  985. part = &net_key->current;
  986. pkt = g_new0(struct mesh_pkt, 1);
  987. if (pkt == NULL)
  988. return;
  989. /* leave extra byte at start for GATT Proxy type */
  990. data = pkt->data + 1;
  991. SET_PKT_NID(data, part->nid);
  992. SET_PKT_IVI(data, sar->iv_index & 1);
  993. SET_PKT_CTL(data, sar->ctl);
  994. SET_PKT_TTL(data, sar->ttl);
  995. SET_PKT_SEQ(data, get_next_seq());
  996. SET_PKT_SRC(data, sar->src);
  997. SET_PKT_DST(data, sar->dst);
  998. SET_PKT_SEGMENTED(data, sar->segmented);
  999. if (sar->ctl)
  1000. SET_PKT_OPCODE(data, sar->data[0]);
  1001. else
  1002. SET_PKT_AKF_AID(data, sar->akf_aid);
  1003. if (sar->segmented) {
  1004. if (!sar->ctl)
  1005. SET_PKT_SZMIC(data, sar->szmic);
  1006. SET_PKT_SEQ0(data, sar->seqAuth);
  1007. SET_PKT_SEGO(data, seg);
  1008. SET_PKT_SEGN(data, sar->segN);
  1009. memcpy(PKT_TRANS(data) + 4,
  1010. sar->data + sar->ctl + (seg * 12), 12);
  1011. pkt->len = 9 + 4;
  1012. if (sar->segN == seg)
  1013. pkt->len += (sar->len - sar->ctl) % 12;
  1014. if (pkt->len == (9 + 4))
  1015. pkt->len += 12;
  1016. } else {
  1017. memcpy(PKT_TRANS(data) + 1,
  1018. sar->data + sar->ctl, 15);
  1019. pkt->len = 9 + 1 + sar->len - sar->ctl;
  1020. }
  1021. pkt->len += (sar->ctl ? 8 : 4);
  1022. mesh_crypto_packet_encode(data, pkt->len,
  1023. part->enc_key,
  1024. sar->iv_index,
  1025. part->privacy_key);
  1026. /* Prepend GATT_Proxy packet type */
  1027. if (sar->proxy)
  1028. pkt->data[0] = PROXY_CONFIG_PDU;
  1029. else
  1030. pkt->data[0] = PROXY_NETWORK_PDU;
  1031. pkt->len++;
  1032. send_mesh_pkt(pkt);
  1033. }
  1034. static void resend_segs(struct mesh_sar_msg *sar)
  1035. {
  1036. uint32_t ack = 1;
  1037. uint8_t i;
  1038. sar->activity_cnt = 0;
  1039. for (i = 0; i <= sar->segN; i++, ack <<= 1) {
  1040. if (!(ack & sar->ack))
  1041. send_seg(sar, i);
  1042. }
  1043. }
  1044. static bool ack_rxed(bool to, uint16_t src, uint16_t dst, bool obo,
  1045. uint16_t seq0, uint32_t ack_flags)
  1046. {
  1047. struct mesh_sar_msg *sar = find_sar_out_by_dst(src);
  1048. uint32_t full_ack;
  1049. /* Silently ignore unknown (stale?) ACKs */
  1050. if (sar == NULL)
  1051. return true;
  1052. full_ack = 0xffffffff >> (31 - sar->segN);
  1053. sar->ack |= (ack_flags & full_ack);
  1054. if (sar->ack == full_ack) {
  1055. /* Outbound message 100% received by remote node */
  1056. flush_sar(&net.msg_out, sar);
  1057. return true;
  1058. }
  1059. /* Because we are GATT, and slow, only resend PKTs if it is
  1060. * time *and* our outbound PKT queue is empty. */
  1061. sar->activity_cnt++;
  1062. if (net.pkt_out == NULL)
  1063. resend_segs(sar);
  1064. return true;
  1065. }
  1066. static bool proxy_ctl_rxed(uint16_t net_idx, uint32_t iv_index,
  1067. uint8_t ttl, uint32_t seq_num, uint16_t src, uint16_t dst,
  1068. uint8_t *trans, uint16_t len)
  1069. {
  1070. if (len < 1)
  1071. return false;
  1072. switch(trans[0]) {
  1073. case FILTER_STATUS:
  1074. if (len != 4)
  1075. return false;
  1076. net.reject_list = !!(trans[1] == REJECT_LIST_FILTER);
  1077. bt_shell_printf("Proxy %s list filter length: %d\n",
  1078. net.reject_list ? "Reject" : "Accept",
  1079. get_be16(trans + 2));
  1080. return true;
  1081. default:
  1082. return false;
  1083. }
  1084. return false;
  1085. }
  1086. static bool ctl_rxed(uint16_t net_idx, uint32_t iv_index,
  1087. uint8_t ttl, uint32_t seq_num, uint16_t src, uint16_t dst,
  1088. uint8_t *trans, uint16_t len)
  1089. {
  1090. /* TODO: Handle control messages */
  1091. /* Per Mesh Profile 3.6.5.10 */
  1092. if (trans[0] == NET_OP_HEARTBEAT) {
  1093. uint16_t feat = get_be16(trans + 2);
  1094. bt_shell_printf("HEARTBEAT src: %4.4x dst: %4.4x \
  1095. TTL: %2.2x feat: %s%s%s%s\n",
  1096. src, dst, trans[1],
  1097. (feat & MESH_FEATURE_RELAY) ? "relay " : "",
  1098. (feat & MESH_FEATURE_PROXY) ? "proxy " : "",
  1099. (feat & MESH_FEATURE_FRIEND) ? "friend " : "",
  1100. (feat & MESH_FEATURE_LPN) ? "lpn" : "");
  1101. return true;
  1102. }
  1103. bt_shell_printf("unrecognized control message src:%4.4x dst:%4.4x len:%d\n",
  1104. src, dst, len);
  1105. print_byte_array("msg: ", trans, len);
  1106. return false;
  1107. }
  1108. struct decrypt_params {
  1109. uint8_t *nonce;
  1110. uint8_t *aad;
  1111. uint8_t *out_msg;
  1112. uint8_t *trans;
  1113. uint32_t iv_index;
  1114. uint32_t seq_num;
  1115. uint16_t src;
  1116. uint16_t dst;
  1117. uint16_t len;
  1118. uint16_t net_idx;
  1119. uint16_t app_idx;
  1120. uint8_t akf_aid;
  1121. bool szmic;
  1122. };
  1123. static void try_decrypt(gpointer data, gpointer user_data)
  1124. {
  1125. struct mesh_app_key *app_key = data;
  1126. struct decrypt_params *decrypt = user_data;
  1127. size_t mic_size = decrypt->szmic ? sizeof(uint64_t) : sizeof(uint32_t);
  1128. bool status = false;
  1129. /* Already done... Nothing to do */
  1130. if (decrypt->app_idx != APP_IDX_INVALID)
  1131. return;
  1132. /* Don't decrypt on Appkeys not owned by this NetKey */
  1133. if (app_key->net_idx != decrypt->net_idx)
  1134. return;
  1135. /* Test and decrypt against current key copy */
  1136. if (app_key->current.akf_aid == decrypt->akf_aid)
  1137. status = mesh_crypto_aes_ccm_decrypt(decrypt->nonce,
  1138. app_key->current.key,
  1139. decrypt->aad, decrypt->aad ? 16 : 0,
  1140. decrypt->trans, decrypt->len,
  1141. decrypt->out_msg, NULL, mic_size);
  1142. /* Test and decrypt against new key copy */
  1143. if (!status && app_key->new.akf_aid == decrypt->akf_aid)
  1144. status = mesh_crypto_aes_ccm_decrypt(decrypt->nonce,
  1145. app_key->new.key,
  1146. decrypt->aad, decrypt->aad ? 16 : 0,
  1147. decrypt->trans, decrypt->len,
  1148. decrypt->out_msg, NULL, mic_size);
  1149. /* If successful, terminate with successful App IDX */
  1150. if (status)
  1151. decrypt->app_idx = app_key->generic.idx;
  1152. }
  1153. static uint16_t access_pkt_decrypt(uint8_t *nonce, uint8_t *aad,
  1154. uint16_t net_idx, uint8_t akf_aid, bool szmic,
  1155. uint8_t *trans, uint16_t len)
  1156. {
  1157. uint8_t *out_msg;
  1158. struct decrypt_params decrypt = {
  1159. .nonce = nonce,
  1160. .aad = aad,
  1161. .net_idx = net_idx,
  1162. .akf_aid = akf_aid,
  1163. .szmic = szmic,
  1164. .trans = trans,
  1165. .len = len,
  1166. .app_idx = APP_IDX_INVALID,
  1167. };
  1168. out_msg = g_malloc(len);
  1169. if (out_msg == NULL)
  1170. return false;
  1171. decrypt.out_msg = out_msg;
  1172. g_list_foreach(app_keys, try_decrypt, &decrypt);
  1173. if (decrypt.app_idx != APP_IDX_INVALID)
  1174. memcpy(trans, out_msg, len);
  1175. g_free(out_msg);
  1176. return decrypt.app_idx;
  1177. }
  1178. static bool access_rxed(uint8_t *nonce, uint16_t net_idx,
  1179. uint32_t iv_index, uint32_t seq_num,
  1180. uint16_t src, uint16_t dst,
  1181. uint8_t akf_aid, bool szmic, uint8_t *trans, uint16_t len)
  1182. {
  1183. uint16_t app_idx = access_pkt_decrypt(nonce, NULL,
  1184. net_idx, akf_aid, szmic, trans, len);
  1185. if (app_idx != APP_IDX_INVALID) {
  1186. len -= szmic ? sizeof(uint64_t) : sizeof(uint32_t);
  1187. node_local_data_handler(src, dst, iv_index, seq_num,
  1188. app_idx, trans, len);
  1189. return true;
  1190. }
  1191. return false;
  1192. }
  1193. static void try_virt_decrypt(gpointer data, gpointer user_data)
  1194. {
  1195. struct mesh_virt_addr *virt = data;
  1196. struct decrypt_params *decrypt = user_data;
  1197. if (decrypt->app_idx != APP_IDX_INVALID || decrypt->dst != virt->va16)
  1198. return;
  1199. decrypt->app_idx = access_pkt_decrypt(decrypt->nonce,
  1200. virt->va128,
  1201. decrypt->net_idx, decrypt->akf_aid,
  1202. decrypt->szmic, decrypt->trans, decrypt->len);
  1203. if (decrypt->app_idx != APP_IDX_INVALID) {
  1204. uint16_t len = decrypt->len;
  1205. len -= decrypt->szmic ? sizeof(uint64_t) : sizeof(uint32_t);
  1206. node_local_data_handler(decrypt->src, virt->va32,
  1207. decrypt->iv_index, decrypt->seq_num,
  1208. decrypt->app_idx, decrypt->trans, len);
  1209. }
  1210. }
  1211. static bool virtual_rxed(uint8_t *nonce, uint16_t net_idx,
  1212. uint32_t iv_index, uint32_t seq_num,
  1213. uint16_t src, uint16_t dst,
  1214. uint8_t akf_aid, bool szmic, uint8_t *trans, uint16_t len)
  1215. {
  1216. struct decrypt_params decrypt = {
  1217. .nonce = nonce,
  1218. .net_idx = net_idx,
  1219. .iv_index = iv_index,
  1220. .seq_num = seq_num,
  1221. .src = dst,
  1222. .dst = dst,
  1223. .akf_aid = akf_aid,
  1224. .szmic = szmic,
  1225. .trans = trans,
  1226. .len = len,
  1227. .app_idx = APP_IDX_INVALID,
  1228. };
  1229. /* Cycle through known virtual addresses */
  1230. g_list_foreach(virt_addrs, try_virt_decrypt, &decrypt);
  1231. if (decrypt.app_idx != APP_IDX_INVALID)
  1232. return true;
  1233. return false;
  1234. }
  1235. static bool msg_rxed(uint16_t net_idx, uint32_t iv_index, bool szmic,
  1236. uint8_t ttl, uint32_t seq_num, uint32_t seq_auth,
  1237. uint16_t src, uint16_t dst,
  1238. uint8_t *trans, uint16_t len)
  1239. {
  1240. uint8_t akf_aid = TRANS_AKF_AID(trans);
  1241. bool result;
  1242. size_t mic_size = szmic ? sizeof(uint64_t) : sizeof(uint32_t);
  1243. uint8_t nonce[13];
  1244. uint8_t *dev_key;
  1245. uint8_t *out = NULL;
  1246. if (!TRANS_AKF(trans)) {
  1247. /* Compose Nonce */
  1248. result = mesh_crypto_device_nonce(seq_auth, src, dst,
  1249. iv_index, szmic, nonce);
  1250. if (!result) return false;
  1251. out = g_malloc0(TRANS_LEN(trans, len));
  1252. if (out == NULL) return false;
  1253. /* If we are provisioner, we probably RXed on remote Dev Key */
  1254. if (net.provisioner) {
  1255. dev_key = node_get_device_key(node_find_by_addr(src));
  1256. if (dev_key == NULL)
  1257. goto local_dev_key;
  1258. } else
  1259. goto local_dev_key;
  1260. result = mesh_crypto_aes_ccm_decrypt(nonce, dev_key,
  1261. NULL, 0,
  1262. TRANS_PAYLOAD(trans), TRANS_LEN(trans, len),
  1263. out, NULL, mic_size);
  1264. if (result) {
  1265. node_local_data_handler(src, dst,
  1266. iv_index, seq_num, APP_IDX_DEV,
  1267. out, TRANS_LEN(trans, len) - mic_size);
  1268. goto done;
  1269. }
  1270. local_dev_key:
  1271. /* Always fallback to the local Dev Key */
  1272. dev_key = node_get_device_key(node_get_local_node());
  1273. if (dev_key == NULL)
  1274. goto done;
  1275. result = mesh_crypto_aes_ccm_decrypt(nonce, dev_key,
  1276. NULL, 0,
  1277. TRANS_PAYLOAD(trans), TRANS_LEN(trans, len),
  1278. out, NULL, mic_size);
  1279. if (result) {
  1280. node_local_data_handler(src, dst,
  1281. iv_index, seq_num, APP_IDX_DEV,
  1282. out, TRANS_LEN(trans, len) - mic_size);
  1283. goto done;
  1284. }
  1285. goto done;
  1286. }
  1287. result = mesh_crypto_application_nonce(seq_auth, src, dst,
  1288. iv_index, szmic, nonce);
  1289. if (!result) goto done;
  1290. /* If Virtual destination wrap the Access decoder with Virtual */
  1291. if (IS_VIRTUAL(dst)) {
  1292. result = virtual_rxed(nonce, net_idx, iv_index, seq_num,
  1293. src, dst, akf_aid, szmic,
  1294. TRANS_PAYLOAD(trans), TRANS_LEN(trans, len));
  1295. goto done;
  1296. }
  1297. /* Try all matching App Keys until success or exhaustion */
  1298. result = access_rxed(nonce, net_idx, iv_index, seq_num,
  1299. src, dst, akf_aid, szmic,
  1300. TRANS_PAYLOAD(trans), TRANS_LEN(trans, len));
  1301. done:
  1302. if (out != NULL)
  1303. g_free(out);
  1304. return result;
  1305. }
  1306. static void send_sar_ack(struct mesh_sar_msg *sar)
  1307. {
  1308. uint8_t ack[7];
  1309. sar->activity_cnt = 0;
  1310. memset(ack, 0, sizeof(ack));
  1311. SET_TRANS_OPCODE(ack, NET_OP_SEG_ACKNOWLEDGE);
  1312. SET_TRANS_SEQ0(ack, sar->seqAuth);
  1313. SET_TRANS_ACK(ack, sar->ack);
  1314. net_ctl_msg_send(0xff, sar->dst, sar->src, ack, sizeof(ack));
  1315. }
  1316. static gboolean sar_out_ack_timeout(void *user_data)
  1317. {
  1318. struct mesh_sar_msg *sar = user_data;
  1319. sar->activity_cnt++;
  1320. /* Because we are GATT, and slow, only resend PKTs if it is
  1321. * time *and* our outbound PKT queue is empty. */
  1322. if (net.pkt_out == NULL)
  1323. resend_segs(sar);
  1324. /* Only add resent SAR pkts to empty queue */
  1325. return true;
  1326. }
  1327. static gboolean sar_out_msg_timeout(void *user_data)
  1328. {
  1329. struct mesh_sar_msg *sar = user_data;
  1330. /* msg_to will expire when we return false */
  1331. sar->msg_to = 0;
  1332. flush_sar(&net.msg_out, sar);
  1333. return false;
  1334. }
  1335. static gboolean sar_in_ack_timeout(void *user_data)
  1336. {
  1337. struct mesh_sar_msg *sar = user_data;
  1338. uint32_t full_ack = 0xffffffff >> (31 - sar->segN);
  1339. if (sar->activity_cnt || sar->ack != full_ack)
  1340. send_sar_ack(sar);
  1341. return true;
  1342. }
  1343. static gboolean sar_in_msg_timeout(void *user_data)
  1344. {
  1345. struct mesh_sar_msg *sar = user_data;
  1346. /* msg_to will expire when we return false */
  1347. sar->msg_to = 0;
  1348. flush_sar(&net.sar_in, sar);
  1349. return false;
  1350. }
  1351. static uint32_t calc_seqAuth(uint32_t seq_num, uint8_t *trans)
  1352. {
  1353. uint32_t seqAuth = seq_num & ~0x1fff;
  1354. seqAuth |= TRANS_SEQ0(trans);
  1355. return seqAuth;
  1356. }
  1357. static bool seg_rxed(uint16_t net_idx, uint32_t iv_index, bool ctl,
  1358. uint8_t ttl, uint32_t seq_num, uint16_t src, uint16_t dst,
  1359. uint8_t *trans, uint16_t len)
  1360. {
  1361. struct mesh_sar_msg *sar;
  1362. uint32_t seqAuth = calc_seqAuth(seq_num, trans);
  1363. uint8_t segN, segO;
  1364. uint32_t old_ack, full_ack, last_ack_mask;
  1365. bool send_ack, result = false;
  1366. segN = TRANS_SEGN(trans);
  1367. segO = TRANS_SEGO(trans);
  1368. /* Only support single incoming SAR'd message per SRC */
  1369. sar = find_sar_in_by_src(src);
  1370. /* Reuse existing SAR structure if appropriate */
  1371. if (sar) {
  1372. uint64_t iv_seqAuth = (uint64_t)iv_index << 32 | seqAuth;
  1373. uint64_t old_iv_seqAuth = (uint64_t)sar->iv_index << 32 |
  1374. sar->seqAuth;
  1375. if (old_iv_seqAuth < iv_seqAuth) {
  1376. flush_sar(&net.sar_in, sar);
  1377. sar = NULL;
  1378. } else if (old_iv_seqAuth > iv_seqAuth) {
  1379. /* New segment is Stale. Silently ignore */
  1380. return false;
  1381. } else if (segN != sar->segN) {
  1382. /* Remote side sent conflicting data: abandon */
  1383. flush_sar(&net.sar_in, sar);
  1384. sar = NULL;
  1385. }
  1386. }
  1387. if (sar == NULL) {
  1388. sar = g_malloc0(sizeof(*sar) + (12 * segN));
  1389. if (sar == NULL)
  1390. return false;
  1391. sar->net_idx = net_idx;
  1392. sar->iv_index = iv_index;
  1393. sar->ctl = ctl;
  1394. sar->ttl = ttl;
  1395. sar->seqAuth = seqAuth;
  1396. sar->src = src;
  1397. sar->dst = dst;
  1398. sar->segmented = true;
  1399. sar->szmic = TRANS_SZMIC(trans);
  1400. sar->segN = segN;
  1401. /* In all cases, the reassembled packet should begin with the
  1402. * same first octet of all segments, minus the SEGMENTED flag */
  1403. sar->data[0] = trans[0] & 0x7f;
  1404. net.sar_in = g_list_append(net.sar_in, sar);
  1405. /* Setup expiration timers */
  1406. if (IS_UNICAST(dst))
  1407. sar->ack_to = g_timeout_add(5000,
  1408. sar_in_ack_timeout, sar);
  1409. sar->msg_to = g_timeout_add(60000, sar_in_msg_timeout, sar);
  1410. }
  1411. /* If last segment, calculate full msg size */
  1412. if (segN == segO)
  1413. sar->len = (segN * 12) + len - 3;
  1414. /* Copy to correct offset */
  1415. memcpy(sar->data + 1 + (12 * segO), trans + 4, 12);
  1416. full_ack = 0xffffffff >> (31 - segN);
  1417. last_ack_mask = 0xffffffff << segO;
  1418. old_ack = sar->ack;
  1419. sar->ack |= 1 << segO;
  1420. send_ack = false;
  1421. /* Determine if we should forward message */
  1422. if (sar->ack == full_ack && old_ack != full_ack) {
  1423. /* First time we have seen this complete message */
  1424. send_ack = true;
  1425. if (ctl)
  1426. result = ctl_rxed(sar->net_idx, sar->iv_index,
  1427. sar->ttl, sar->seqAuth, sar->src,
  1428. sar->dst, sar->data, sar->len);
  1429. else
  1430. result = msg_rxed(sar->net_idx, sar->iv_index,
  1431. sar->szmic, sar->ttl,
  1432. seq_num, sar->seqAuth, sar->src,
  1433. sar->dst, sar->data, sar->len);
  1434. }
  1435. /* Never Ack Group addressed SAR messages */
  1436. if (!IS_UNICAST(dst))
  1437. return result;
  1438. /* Tickle the ACK system so it knows we are still RXing segments */
  1439. sar->activity_cnt++;
  1440. /* Determine if we should ACK */
  1441. if (old_ack == sar->ack)
  1442. /* Let the timer generate repeat ACKs as needed */
  1443. send_ack = false;
  1444. else if ((last_ack_mask & sar->ack) == (last_ack_mask & full_ack))
  1445. /* If this was largest segO outstanding segment, we ACK */
  1446. send_ack = true;
  1447. if (send_ack)
  1448. send_sar_ack(sar);
  1449. return result;
  1450. }
  1451. bool net_data_ready(uint8_t *msg, uint8_t len)
  1452. {
  1453. uint8_t type = *msg++;
  1454. uint32_t iv_index = net.iv_index;
  1455. struct mesh_net_key *net_key;
  1456. if (len-- < 10) return false;
  1457. if (type == PROXY_MESH_BEACON)
  1458. return process_beacon(msg, len);
  1459. else if (type > PROXY_CONFIG_PDU)
  1460. return false;
  1461. /* RXed iv_index must be equal or 1 less than local iv_index */
  1462. /* With the clue being high-order bit of first octet */
  1463. if (!!(iv_index & 0x01) != !!(msg[0] & 0x80)) {
  1464. if (iv_index)
  1465. iv_index--;
  1466. else
  1467. return false;
  1468. }
  1469. net_key = net_packet_decode(type == PROXY_CONFIG_PDU,
  1470. iv_index, msg, len);
  1471. if (net_key == NULL)
  1472. return false;
  1473. /* CTL packets have 64 bit network MIC, otherwise 32 bit MIC */
  1474. len -= PKT_CTL(msg) ? sizeof(uint64_t) : sizeof(uint32_t);
  1475. if (type == PROXY_CONFIG_PDU) {
  1476. /* Proxy Configuration DST messages must be 0x0000 */
  1477. if (PKT_DST(msg))
  1478. return false;
  1479. return proxy_ctl_rxed(net_key->generic.idx,
  1480. iv_index, PKT_TTL(msg), PKT_SEQ(msg),
  1481. PKT_SRC(msg), PKT_DST(msg),
  1482. PKT_TRANS(msg), PKT_TRANS_LEN(len));
  1483. } if (PKT_CTL(msg) && PKT_OPCODE(msg) == NET_OP_SEG_ACKNOWLEDGE) {
  1484. return ack_rxed(false, PKT_SRC(msg), PKT_DST(msg),
  1485. PKT_OBO(msg), PKT_SEQ0(msg), PKT_ACK(msg));
  1486. } else if (PKT_SEGMENTED(msg)) {
  1487. return seg_rxed(net_key->generic.idx, iv_index, PKT_CTL(msg),
  1488. PKT_TTL(msg), PKT_SEQ(msg),
  1489. PKT_SRC(msg), PKT_DST(msg),
  1490. PKT_TRANS(msg), PKT_TRANS_LEN(len));
  1491. } else if (!PKT_CTL(msg)){
  1492. return msg_rxed(net_key->generic.idx,
  1493. iv_index, false, PKT_TTL(msg), PKT_SEQ(msg),
  1494. PKT_SEQ(msg), PKT_SRC(msg), PKT_DST(msg),
  1495. PKT_TRANS(msg), PKT_TRANS_LEN(len));
  1496. } else {
  1497. return ctl_rxed(net_key->generic.idx,
  1498. iv_index, PKT_TTL(msg), PKT_SEQ(msg),
  1499. PKT_SRC(msg), PKT_DST(msg),
  1500. PKT_TRANS(msg), PKT_TRANS_LEN(len));
  1501. }
  1502. return false;
  1503. }
  1504. bool net_session_open(GDBusProxy *data_in, bool provisioner,
  1505. net_mesh_session_open_callback cb)
  1506. {
  1507. if (net.proxy_in)
  1508. return false;
  1509. net.proxy_in = data_in;
  1510. net.iv_upd_state = IV_UPD_INIT;
  1511. net.reject_list = false;
  1512. net.provisioner = provisioner;
  1513. net.open_cb = cb;
  1514. flush_pkt_list(&net.pkt_out);
  1515. return true;
  1516. }
  1517. void net_session_close(GDBusProxy *data_in)
  1518. {
  1519. if (net.proxy_in == data_in)
  1520. net.proxy_in = NULL;
  1521. flush_sar_list(&net.sar_in);
  1522. flush_sar_list(&net.msg_out);
  1523. flush_pkt_list(&net.pkt_out);
  1524. }
  1525. bool net_register_unicast(uint16_t unicast, uint8_t count)
  1526. {
  1527. /* TODO */
  1528. return true;
  1529. }
  1530. bool net_register_group(uint16_t group_addr)
  1531. {
  1532. /* TODO */
  1533. return true;
  1534. }
  1535. uint32_t net_register_virtual(uint8_t buf[16])
  1536. {
  1537. /* TODO */
  1538. return 0;
  1539. }
  1540. static bool get_enc_keys(uint16_t app_idx, uint16_t dst,
  1541. uint8_t *akf_aid, uint8_t **app_enc_key,
  1542. uint16_t *net_idx)
  1543. {
  1544. if (app_idx == APP_IDX_DEV) {
  1545. struct mesh_node *node;
  1546. uint8_t *enc_key = NULL;
  1547. if (net.provisioner) {
  1548. /* Default to Remote Device Key when Provisioner */
  1549. node = node_find_by_addr(dst);
  1550. enc_key = node_get_device_key(node);
  1551. }
  1552. if (enc_key == NULL) {
  1553. /* Use Local node Device Key */
  1554. node = node_get_local_node();
  1555. enc_key = node_get_device_key(node);
  1556. }
  1557. if (enc_key == NULL || node == NULL)
  1558. return false;
  1559. if (akf_aid) *akf_aid = 0;
  1560. if (app_enc_key) *app_enc_key = enc_key;
  1561. if (net_idx) *net_idx = node_get_primary_net_idx(node);
  1562. } else {
  1563. struct mesh_app_key *app_key = find_app_key_by_idx(app_idx);
  1564. struct mesh_net_key *net_key;
  1565. bool phase_two;
  1566. if (app_key == NULL)
  1567. return false;
  1568. net_key = find_net_key_by_idx(app_key->net_idx);
  1569. if (net_key == NULL)
  1570. return false;
  1571. if (net_idx) *net_idx = app_key->net_idx;
  1572. phase_two = !!(net_key->phase == 2);
  1573. if (phase_two && app_key->new.akf_aid != 0xff) {
  1574. if (app_enc_key) *app_enc_key = app_key->new.key;
  1575. if (akf_aid) *akf_aid = app_key->new.akf_aid;
  1576. } else {
  1577. if (app_enc_key) *app_enc_key = app_key->current.key;
  1578. if (akf_aid) *akf_aid = app_key->current.akf_aid;
  1579. }
  1580. }
  1581. return true;
  1582. }
  1583. bool net_ctl_msg_send(uint8_t ttl, uint16_t src, uint16_t dst,
  1584. uint8_t *buf, uint16_t len)
  1585. {
  1586. struct mesh_node *node = node_get_local_node();
  1587. struct mesh_sar_msg sar_ctl;
  1588. /* For simplicity, we will reject segmented OB CTL messages */
  1589. if (len > 12 || node == NULL || buf == NULL || buf[0] & 0x80)
  1590. return false;
  1591. if (!src) {
  1592. src = node_get_primary(node);
  1593. if (!src)
  1594. return false;
  1595. }
  1596. if (ttl == 0xff)
  1597. ttl = net.default_ttl;
  1598. memset(&sar_ctl, 0, sizeof(sar_ctl));
  1599. if (!dst)
  1600. sar_ctl.proxy = true;
  1601. /* Get the default net_idx for remote device (or local) */
  1602. get_enc_keys(APP_IDX_DEV, dst, NULL, NULL, &sar_ctl.net_idx);
  1603. sar_ctl.ctl = true;
  1604. sar_ctl.iv_index = net.iv_index - net.iv_update;
  1605. sar_ctl.ttl = ttl;
  1606. sar_ctl.src = src;
  1607. sar_ctl.dst = dst;
  1608. sar_ctl.len = len;
  1609. memcpy(sar_ctl.data, buf, len);
  1610. send_seg(&sar_ctl, 0);
  1611. return true;
  1612. }
  1613. bool net_access_layer_send(uint8_t ttl, uint16_t src, uint32_t dst,
  1614. uint16_t app_idx, uint8_t *buf, uint16_t len)
  1615. {
  1616. struct mesh_node *node = node_get_local_node();
  1617. struct mesh_sar_msg *sar;
  1618. uint8_t *app_enc_key = NULL;
  1619. uint8_t *aad = NULL;
  1620. uint32_t mic32;
  1621. uint8_t aad_len = 0;
  1622. uint8_t i, j, ackless_retries = 0;
  1623. uint8_t segN, akf_aid;
  1624. uint16_t net_idx;
  1625. bool result;
  1626. if (len > 384 || node == NULL)
  1627. return false;
  1628. if (!src)
  1629. src = node_get_primary(node);
  1630. if (!src || !dst)
  1631. return false;
  1632. if (ttl == 0xff)
  1633. ttl = net.default_ttl;
  1634. if (IS_VIRTUAL(dst)) {
  1635. struct mesh_virt_addr *virt = find_virt_by_dst(dst);
  1636. if (virt == NULL)
  1637. return false;
  1638. dst = virt->va16;
  1639. aad = virt->va128;
  1640. aad_len = sizeof(virt->va128);
  1641. }
  1642. result = get_enc_keys(app_idx, dst,
  1643. &akf_aid, &app_enc_key, &net_idx);
  1644. if (!result)
  1645. return false;
  1646. segN = SEG_MAX(len + sizeof(mic32));
  1647. /* Only one ACK required SAR message per destination at a time */
  1648. if (segN && IS_UNICAST(dst)) {
  1649. sar = find_sar_out_by_dst(dst);
  1650. if (sar)
  1651. flush_sar(&net.msg_out, sar);
  1652. }
  1653. sar = g_malloc0(sizeof(struct mesh_sar_msg) + (segN * 12));
  1654. if (sar == NULL)
  1655. return false;
  1656. if (segN)
  1657. sar->segmented = true;
  1658. sar->ttl = ttl;
  1659. sar->segN = segN;
  1660. sar->seqAuth = net.seq_num;
  1661. sar->iv_index = net.iv_index - net.iv_update;
  1662. sar->net_idx = net_idx;
  1663. sar->src = src;
  1664. sar->dst = dst;
  1665. sar->akf_aid = akf_aid;
  1666. sar->len = len + sizeof(uint32_t);
  1667. mesh_crypto_application_encrypt(akf_aid,
  1668. sar->seqAuth, src,
  1669. dst, sar->iv_index,
  1670. app_enc_key,
  1671. aad, aad_len,
  1672. buf, len,
  1673. sar->data, &mic32,
  1674. sizeof(uint32_t));
  1675. /* If sending as a segmented message to a non-Unicast (thus non-ACKing)
  1676. * destination, send each segments multiple times. */
  1677. if (!IS_UNICAST(dst) && segN)
  1678. ackless_retries = 4;
  1679. for (j = 0; j <= ackless_retries; j++) {
  1680. for (i = 0; i <= segN; i++)
  1681. send_seg(sar, i);
  1682. }
  1683. if (IS_UNICAST(dst) && segN) {
  1684. net.msg_out = g_list_append(net.msg_out, sar);
  1685. sar->ack_to = g_timeout_add(2000, sar_out_ack_timeout, sar);
  1686. sar->msg_to = g_timeout_add(60000, sar_out_msg_timeout, sar);
  1687. } else
  1688. g_free(sar);
  1689. return true;
  1690. }
  1691. bool net_set_default_ttl(uint8_t ttl)
  1692. {
  1693. if (ttl > 0x7f)
  1694. return false;
  1695. net.default_ttl = ttl;
  1696. return true;
  1697. }
  1698. uint8_t net_get_default_ttl()
  1699. {
  1700. return net.default_ttl;
  1701. }
  1702. bool net_set_seq_num(uint32_t seq_num)
  1703. {
  1704. if (seq_num > 0xffffff)
  1705. return false;
  1706. net.seq_num = seq_num;
  1707. return true;
  1708. }
  1709. uint32_t net_get_seq_num()
  1710. {
  1711. return net.seq_num;
  1712. }