model.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2018-2020 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <sys/time.h>
  14. #include <ell/ell.h>
  15. #include "mesh/mesh-defs.h"
  16. #include "mesh/mesh.h"
  17. #include "mesh/crypto.h"
  18. #include "mesh/node.h"
  19. #include "mesh/mesh-config.h"
  20. #include "mesh/net.h"
  21. #include "mesh/appkey.h"
  22. #include "mesh/cfgmod.h"
  23. #include "mesh/error.h"
  24. #include "mesh/dbus.h"
  25. #include "mesh/util.h"
  26. #include "mesh/model.h"
  27. #include "mesh/keyring.h"
  28. /* Divide and round to ceiling (up) to calculate segment count */
  29. #define CEILDIV(val, div) (((val) + (div) - 1) / (div))
  30. #define VIRTUAL_BASE 0x10000
  31. struct mesh_model {
  32. const struct mesh_model_ops *cbs;
  33. void *user_data;
  34. struct l_queue *bindings;
  35. struct l_queue *subs;
  36. struct l_queue *virtuals;
  37. struct mesh_model_pub *pub;
  38. bool sub_enabled;
  39. bool pub_enabled;
  40. uint32_t id;
  41. };
  42. struct mesh_virtual {
  43. uint16_t ref_cnt;
  44. uint16_t addr; /* 16-bit virtual address, used in messages */
  45. uint8_t label[16]; /* 128 bit label UUID */
  46. };
  47. /* These struct is used to pass lots of params to l_queue_foreach */
  48. struct mod_forward {
  49. struct mesh_virtual *virt;
  50. const uint8_t *data;
  51. uint16_t src;
  52. uint16_t dst;
  53. uint16_t unicast;
  54. uint16_t app_idx;
  55. uint16_t net_idx;
  56. uint16_t size;
  57. int8_t rssi;
  58. bool szmict;
  59. bool has_dst;
  60. bool done;
  61. };
  62. static struct l_queue *mesh_virtuals;
  63. static bool is_internal(uint32_t id)
  64. {
  65. if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
  66. return true;
  67. return false;
  68. }
  69. static void unref_virt(void *data)
  70. {
  71. struct mesh_virtual *virt = data;
  72. if (virt->ref_cnt > 0)
  73. virt->ref_cnt--;
  74. if (virt->ref_cnt)
  75. return;
  76. l_queue_remove(mesh_virtuals, virt);
  77. l_free(virt);
  78. }
  79. static bool simple_match(const void *a, const void *b)
  80. {
  81. return a == b;
  82. }
  83. static bool has_binding(struct l_queue *bindings, uint16_t idx)
  84. {
  85. const struct l_queue_entry *entry;
  86. for (entry = l_queue_get_entries(bindings); entry; entry = entry->next)
  87. if (L_PTR_TO_INT(entry->data) == idx)
  88. return true;
  89. return false;
  90. }
  91. static bool find_virt_by_label(const void *a, const void *b)
  92. {
  93. const struct mesh_virtual *virt = a;
  94. const uint8_t *label = b;
  95. return memcmp(virt->label, label, 16) == 0;
  96. }
  97. static bool match_model_id(const void *a, const void *b)
  98. {
  99. const struct mesh_model *mod = a;
  100. uint32_t id = L_PTR_TO_UINT(b);
  101. return (mod->id == id);
  102. }
  103. static int compare_model_id(const void *a, const void *b, void *user_data)
  104. {
  105. const struct mesh_model *mod_a = a;
  106. const struct mesh_model *mod_b = b;
  107. if (mod_a->id < mod_b->id)
  108. return -1;
  109. if (mod_a->id > mod_b->id)
  110. return 1;
  111. return 0;
  112. }
  113. static struct mesh_model *get_model(struct mesh_node *node, uint8_t ele_idx,
  114. uint32_t id)
  115. {
  116. struct l_queue *mods;
  117. struct mesh_model *mod;
  118. mods = node_get_element_models(node, ele_idx);
  119. if (!mods)
  120. return NULL;
  121. mod = l_queue_find(mods, match_model_id, L_UINT_TO_PTR(id));
  122. if (!mod)
  123. l_debug("Model not found");
  124. return mod;
  125. }
  126. static uint32_t pub_period_to_ms(uint8_t pub_period)
  127. {
  128. int step_res, num_steps;
  129. step_res = pub_period >> 6;
  130. num_steps = pub_period & 0x3f;
  131. switch (step_res) {
  132. default:
  133. return num_steps * 100;
  134. case 2:
  135. num_steps *= 10;
  136. /* Fall Through */
  137. case 1:
  138. return num_steps * 1000;
  139. case 3:
  140. return num_steps * 10 * 60 * 1000;
  141. }
  142. }
  143. static struct l_dbus_message *create_config_update_msg(struct mesh_node *node,
  144. uint8_t ele_idx, uint32_t id,
  145. struct l_dbus_message_builder **builder)
  146. {
  147. struct l_dbus *dbus = dbus_get_bus();
  148. struct l_dbus_message *msg;
  149. const char *owner;
  150. const char *path;
  151. uint16_t model_id;
  152. owner = node_get_owner(node);
  153. path = node_get_element_path(node, ele_idx);
  154. if (!path || !owner)
  155. return NULL;
  156. l_debug("Send \"UpdateModelConfiguration\"");
  157. msg = l_dbus_message_new_method_call(dbus, owner, path,
  158. MESH_ELEMENT_INTERFACE,
  159. "UpdateModelConfiguration");
  160. *builder = l_dbus_message_builder_new(msg);
  161. model_id = (uint16_t) MODEL_ID(id);
  162. l_dbus_message_builder_append_basic(*builder, 'q', &model_id);
  163. l_dbus_message_builder_enter_array(*builder, "{sv}");
  164. if (IS_VENDOR(id)) {
  165. uint16_t vendor_id = (uint16_t) VENDOR_ID(id);
  166. dbus_append_dict_entry_basic(*builder, "Vendor", "q",
  167. &vendor_id);
  168. }
  169. return msg;
  170. }
  171. static void config_update_model_pub_period(struct mesh_node *node,
  172. uint8_t ele_idx, uint32_t model_id,
  173. uint32_t period)
  174. {
  175. struct l_dbus *dbus = dbus_get_bus();
  176. struct l_dbus_message *msg;
  177. struct l_dbus_message_builder *builder;
  178. msg = create_config_update_msg(node, ele_idx, model_id, &builder);
  179. if (!msg)
  180. return;
  181. dbus_append_dict_entry_basic(builder, "PublicationPeriod", "u",
  182. &period);
  183. l_dbus_message_builder_leave_array(builder);
  184. l_dbus_message_builder_finalize(builder);
  185. l_dbus_message_builder_destroy(builder);
  186. l_dbus_send(dbus, msg);
  187. }
  188. static void append_dict_uint16_array(struct l_dbus_message_builder *builder,
  189. struct l_queue *q, const char *key)
  190. {
  191. const struct l_queue_entry *entry;
  192. l_dbus_message_builder_enter_dict(builder, "sv");
  193. l_dbus_message_builder_append_basic(builder, 's', key);
  194. l_dbus_message_builder_enter_variant(builder, "aq");
  195. l_dbus_message_builder_enter_array(builder, "q");
  196. for (entry = l_queue_get_entries(q); entry; entry = entry->next) {
  197. uint16_t value = (uint16_t) L_PTR_TO_UINT(entry->data);
  198. l_dbus_message_builder_append_basic(builder,'q', &value);
  199. }
  200. l_dbus_message_builder_leave_array(builder);
  201. l_dbus_message_builder_leave_variant(builder);
  202. l_dbus_message_builder_leave_dict(builder);
  203. }
  204. static void cfg_update_mod_bindings(struct mesh_node *node, uint16_t ele_idx,
  205. struct mesh_model *mod)
  206. {
  207. struct l_dbus *dbus = dbus_get_bus();
  208. struct l_dbus_message *msg;
  209. struct l_dbus_message_builder *builder;
  210. msg = create_config_update_msg(node, ele_idx, mod->id, &builder);
  211. if (!msg)
  212. return;
  213. append_dict_uint16_array(builder, mod->bindings, "Bindings");
  214. l_dbus_message_builder_leave_array(builder);
  215. l_dbus_message_builder_finalize(builder);
  216. l_dbus_message_builder_destroy(builder);
  217. l_dbus_send(dbus, msg);
  218. }
  219. static void append_dict_subs_array(struct l_dbus_message_builder *builder,
  220. struct l_queue *subs,
  221. struct l_queue *virts,
  222. const char *key)
  223. {
  224. const struct l_queue_entry *entry;
  225. l_dbus_message_builder_enter_dict(builder, "sv");
  226. l_dbus_message_builder_append_basic(builder, 's', key);
  227. l_dbus_message_builder_enter_variant(builder, "av");
  228. l_dbus_message_builder_enter_array(builder, "v");
  229. if (l_queue_isempty(subs))
  230. goto virts;
  231. for (entry = l_queue_get_entries(subs); entry; entry = entry->next) {
  232. uint16_t grp = L_PTR_TO_UINT(entry->data);
  233. l_dbus_message_builder_enter_variant(builder, "q");
  234. l_dbus_message_builder_append_basic(builder, 'q', &grp);
  235. l_dbus_message_builder_leave_variant(builder);
  236. }
  237. virts:
  238. if (l_queue_isempty(virts))
  239. goto done;
  240. for (entry = l_queue_get_entries(virts); entry; entry = entry->next) {
  241. const struct mesh_virtual *virt = entry->data;
  242. l_dbus_message_builder_enter_variant(builder, "ay");
  243. dbus_append_byte_array(builder, virt->label,
  244. sizeof(virt->label));
  245. l_dbus_message_builder_leave_variant(builder);
  246. }
  247. done:
  248. l_dbus_message_builder_leave_array(builder);
  249. l_dbus_message_builder_leave_variant(builder);
  250. l_dbus_message_builder_leave_dict(builder);
  251. }
  252. static void cfg_update_model_subs(struct mesh_node *node, uint16_t ele_idx,
  253. struct mesh_model *mod)
  254. {
  255. struct l_dbus *dbus = dbus_get_bus();
  256. struct l_dbus_message *msg;
  257. struct l_dbus_message_builder *builder;
  258. msg = create_config_update_msg(node, ele_idx, mod->id, &builder);
  259. if (!msg)
  260. return;
  261. append_dict_subs_array(builder, mod->subs, mod->virtuals,
  262. "Subscriptions");
  263. l_dbus_message_builder_leave_array(builder);
  264. l_dbus_message_builder_finalize(builder);
  265. l_dbus_message_builder_destroy(builder);
  266. l_dbus_send(dbus, msg);
  267. }
  268. static void forward_model(void *a, void *b)
  269. {
  270. struct mesh_model *mod = a;
  271. struct mod_forward *fwd = b;
  272. struct mesh_virtual *virt;
  273. uint16_t dst;
  274. bool result;
  275. if (fwd->app_idx != APP_IDX_DEV_LOCAL &&
  276. fwd->app_idx != APP_IDX_DEV_REMOTE &&
  277. !has_binding(mod->bindings, fwd->app_idx))
  278. return;
  279. dst = fwd->dst;
  280. if (dst == fwd->unicast || IS_FIXED_GROUP_ADDRESS(dst)) {
  281. fwd->has_dst = true;
  282. } else if (fwd->virt) {
  283. virt = l_queue_find(mod->virtuals, simple_match, fwd->virt);
  284. if (virt) {
  285. fwd->has_dst = true;
  286. dst = virt->addr;
  287. }
  288. } else {
  289. if (l_queue_find(mod->subs, simple_match, L_UINT_TO_PTR(dst)))
  290. fwd->has_dst = true;
  291. }
  292. if (!fwd->has_dst)
  293. return;
  294. /* Return, if this is not a internal model */
  295. if (!mod->cbs)
  296. return;
  297. result = false;
  298. if (mod->cbs->recv)
  299. result = mod->cbs->recv(fwd->src, dst, fwd->app_idx,
  300. fwd->net_idx,
  301. fwd->data, fwd->size, mod->user_data);
  302. if (dst == fwd->unicast && result)
  303. fwd->done = true;
  304. }
  305. static int app_packet_decrypt(struct mesh_net *net, const uint8_t *data,
  306. uint16_t size, bool szmict, uint16_t src,
  307. uint16_t dst, uint8_t *virt, uint16_t virt_size,
  308. uint8_t key_aid, uint32_t seq,
  309. uint32_t iv_idx, uint8_t *out)
  310. {
  311. struct l_queue *app_keys = mesh_net_get_app_keys(net);
  312. const struct l_queue_entry *entry;
  313. if (!app_keys)
  314. return -1;
  315. for (entry = l_queue_get_entries(app_keys); entry;
  316. entry = entry->next) {
  317. const uint8_t *old_key = NULL, *new_key = NULL;
  318. uint8_t old_key_aid, new_key_aid;
  319. int app_idx;
  320. bool decrypted;
  321. app_idx = appkey_get_key_idx(entry->data,
  322. &old_key, &old_key_aid,
  323. &new_key, &new_key_aid);
  324. if (app_idx < 0)
  325. continue;
  326. if (old_key && old_key_aid == key_aid) {
  327. decrypted = mesh_crypto_payload_decrypt(virt, virt_size,
  328. data, size, szmict, src, dst, key_aid,
  329. seq, iv_idx, out, old_key);
  330. if (decrypted) {
  331. print_packet("Used App Key", old_key, 16);
  332. return app_idx;
  333. }
  334. print_packet("Failed App Key", old_key, 16);
  335. }
  336. if (new_key && new_key_aid == key_aid) {
  337. decrypted = mesh_crypto_payload_decrypt(virt, virt_size,
  338. data, size, szmict, src, dst, key_aid,
  339. seq, iv_idx, out, new_key);
  340. if (decrypted) {
  341. print_packet("Used App Key", new_key, 16);
  342. return app_idx;
  343. }
  344. print_packet("Failed App Key", new_key, 16);
  345. }
  346. }
  347. return -1;
  348. }
  349. static int dev_packet_decrypt(struct mesh_node *node, const uint8_t *data,
  350. uint16_t size, bool szmict, uint16_t src,
  351. uint16_t dst, uint8_t key_aid, uint32_t seq,
  352. uint32_t iv_idx, uint8_t *out)
  353. {
  354. uint8_t dev_key[16];
  355. const uint8_t *key;
  356. key = node_get_device_key(node);
  357. if (!key)
  358. return -1;
  359. if (mesh_crypto_payload_decrypt(NULL, 0, data, size, szmict, src,
  360. dst, key_aid, seq, iv_idx, out, key))
  361. return APP_IDX_DEV_LOCAL;
  362. if (!keyring_get_remote_dev_key(node, src, dev_key))
  363. return -1;
  364. key = dev_key;
  365. if (mesh_crypto_payload_decrypt(NULL, 0, data, size, szmict, src,
  366. dst, key_aid, seq, iv_idx, out, key))
  367. return APP_IDX_DEV_REMOTE;
  368. return -1;
  369. }
  370. static int virt_packet_decrypt(struct mesh_net *net, const uint8_t *data,
  371. uint16_t size, bool szmict, uint16_t src,
  372. uint16_t dst, uint8_t key_aid, uint32_t seq,
  373. uint32_t iv_idx, uint8_t *out,
  374. struct mesh_virtual **decrypt_virt)
  375. {
  376. const struct l_queue_entry *v;
  377. for (v = l_queue_get_entries(mesh_virtuals); v; v = v->next) {
  378. struct mesh_virtual *virt = v->data;
  379. int decrypt_idx;
  380. if (virt->addr != dst)
  381. continue;
  382. decrypt_idx = app_packet_decrypt(net, data, size, szmict, src,
  383. dst, virt->label, 16,
  384. key_aid, seq, iv_idx,
  385. out);
  386. if (decrypt_idx >= 0) {
  387. *decrypt_virt = virt;
  388. return decrypt_idx;
  389. }
  390. }
  391. return -1;
  392. }
  393. static bool msg_send(struct mesh_node *node, bool cred, uint16_t src,
  394. uint16_t dst, uint16_t app_idx, uint16_t net_idx,
  395. uint8_t *label, uint8_t ttl, uint8_t cnt,
  396. uint16_t interval, bool segmented, const void *msg,
  397. uint16_t msg_len)
  398. {
  399. uint8_t dev_key[16];
  400. uint32_t iv_index, seq_num;
  401. const uint8_t *key;
  402. uint8_t *out;
  403. uint8_t key_aid = APP_AID_DEV;
  404. bool szmic = false;
  405. bool ret = false;
  406. uint16_t out_len = msg_len + sizeof(uint32_t);
  407. struct mesh_net *net = node_get_net(node);
  408. /* Use large MIC if it doesn't affect segmentation */
  409. if (msg_len > 11 && msg_len <= 376) {
  410. if (CEILDIV(out_len, 12) == CEILDIV(out_len + 4, 12)) {
  411. szmic = true;
  412. out_len = msg_len + sizeof(uint64_t);
  413. }
  414. }
  415. if (app_idx == APP_IDX_DEV_LOCAL) {
  416. key = node_get_device_key(node);
  417. if (!key)
  418. return false;
  419. } else if (app_idx == APP_IDX_DEV_REMOTE) {
  420. if (!keyring_get_remote_dev_key(node, dst, dev_key))
  421. return false;
  422. key = dev_key;
  423. } else {
  424. key = appkey_get_key(node_get_net(node), app_idx, &key_aid);
  425. if (!key) {
  426. l_debug("no app key for (%x)", app_idx);
  427. return false;
  428. }
  429. }
  430. out = l_malloc(out_len);
  431. iv_index = mesh_net_get_iv_index(net);
  432. seq_num = mesh_net_next_seq_num(net);
  433. if (!mesh_crypto_payload_encrypt(label, msg, out, msg_len, src, dst,
  434. key_aid, seq_num, iv_index, szmic, key)) {
  435. l_error("Failed to Encrypt Payload");
  436. goto done;
  437. }
  438. ret = mesh_net_app_send(net, cred, src, dst, key_aid, net_idx, ttl,
  439. cnt, interval, seq_num, iv_index,
  440. segmented, szmic, out, out_len);
  441. done:
  442. l_free(out);
  443. return ret;
  444. }
  445. static void remove_pub(struct mesh_node *node, uint16_t ele_idx,
  446. struct mesh_model *mod)
  447. {
  448. if (mod->pub) {
  449. if (mod->pub->virt)
  450. unref_virt(mod->pub->virt);
  451. l_free(mod->pub);
  452. mod->pub = NULL;
  453. }
  454. if (!mod->cbs)
  455. /* External models */
  456. config_update_model_pub_period(node, ele_idx, mod->id, 0);
  457. else if (mod->cbs && mod->cbs->pub)
  458. /* Internal models */
  459. mod->cbs->pub(NULL);
  460. }
  461. static void model_unbind_idx(struct mesh_node *node, uint16_t ele_idx,
  462. struct mesh_model *mod, uint16_t idx)
  463. {
  464. l_queue_remove(mod->bindings, L_UINT_TO_PTR(idx));
  465. if (!mod->cbs)
  466. /* External model */
  467. cfg_update_mod_bindings(node, ele_idx, mod);
  468. else if (mod->cbs->bind)
  469. /* Internal model */
  470. mod->cbs->bind(idx, ACTION_DELETE);
  471. /* Remove model publication if the publication key is unbound */
  472. if (mod->pub && idx == mod->pub->idx)
  473. remove_pub(node, ele_idx, mod);
  474. }
  475. static void model_bind_idx(struct mesh_node *node, uint16_t ele_idx,
  476. struct mesh_model *mod, uint16_t idx)
  477. {
  478. if (!mod->bindings)
  479. mod->bindings = l_queue_new();
  480. l_queue_push_tail(mod->bindings, L_UINT_TO_PTR(idx));
  481. l_debug("Bind key %4.4x to model %8.8x", idx, mod->id);
  482. if (!mod->cbs)
  483. /* External model */
  484. cfg_update_mod_bindings(node, ele_idx, mod);
  485. else if (mod->cbs->bind)
  486. /* Internal model */
  487. mod->cbs->bind(idx, ACTION_ADD);
  488. }
  489. static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,
  490. uint16_t app_idx, bool unbind)
  491. {
  492. struct mesh_model *mod;
  493. bool vendor;
  494. int ele_idx = node_get_element_idx(node, addr);
  495. if (ele_idx < 0)
  496. return MESH_STATUS_INVALID_ADDRESS;
  497. mod = get_model(node, (uint8_t) ele_idx, id);
  498. if (!mod) {
  499. return MESH_STATUS_INVALID_MODEL;
  500. }
  501. if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
  502. return MESH_STATUS_INVALID_MODEL;
  503. if (!appkey_have_key(node_get_net(node), app_idx))
  504. return MESH_STATUS_INVALID_APPKEY;
  505. /*
  506. * If deleting a binding that is not present, return success.
  507. * If adding a binding that already exists, return success.
  508. */
  509. if (unbind ^ has_binding(mod->bindings, app_idx))
  510. return MESH_STATUS_SUCCESS;
  511. vendor = IS_VENDOR(id);
  512. id = vendor ? id : MODEL_ID(id);
  513. if (unbind) {
  514. model_unbind_idx(node, ele_idx, mod, app_idx);
  515. if (!mesh_config_model_binding_del(node_config_get(node),
  516. addr, id, vendor,
  517. app_idx))
  518. return MESH_STATUS_STORAGE_FAIL;
  519. l_debug("Unbind key %4.4x to model %8.8x", app_idx, mod->id);
  520. return MESH_STATUS_SUCCESS;
  521. }
  522. if (l_queue_length(mod->bindings) >= MAX_MODEL_BINDINGS)
  523. return MESH_STATUS_INSUFF_RESOURCES;
  524. if (!mesh_config_model_binding_add(node_config_get(node), addr,
  525. id, vendor, app_idx))
  526. return MESH_STATUS_STORAGE_FAIL;
  527. model_bind_idx(node, ele_idx, mod, app_idx);
  528. return MESH_STATUS_SUCCESS;
  529. }
  530. static struct mesh_virtual *add_virtual(const uint8_t *v)
  531. {
  532. struct mesh_virtual *virt = l_queue_find(mesh_virtuals,
  533. find_virt_by_label, v);
  534. if (virt) {
  535. virt->ref_cnt++;
  536. return virt;
  537. }
  538. virt = l_new(struct mesh_virtual, 1);
  539. if (!mesh_crypto_virtual_addr(v, &virt->addr)) {
  540. l_free(virt);
  541. return NULL;
  542. }
  543. memcpy(virt->label, v, 16);
  544. virt->ref_cnt = 1;
  545. l_queue_push_head(mesh_virtuals, virt);
  546. return virt;
  547. }
  548. static int set_pub(struct mesh_model *mod, uint16_t pub_addr,
  549. uint16_t idx, bool cred_flag, uint8_t ttl,
  550. uint8_t period, uint8_t cnt, uint16_t interval)
  551. {
  552. if (!mod->pub)
  553. mod->pub = l_new(struct mesh_model_pub, 1);
  554. mod->pub->addr = pub_addr;
  555. mod->pub->credential = cred_flag;
  556. mod->pub->idx = idx;
  557. mod->pub->ttl = ttl;
  558. mod->pub->period = period;
  559. mod->pub->rtx.cnt = cnt;
  560. mod->pub->rtx.interval = interval;
  561. return MESH_STATUS_SUCCESS;
  562. }
  563. static int set_virt_pub(struct mesh_model *mod, const uint8_t *label,
  564. uint16_t idx, bool cred_flag, uint8_t ttl,
  565. uint8_t period, uint8_t cnt, uint16_t interval)
  566. {
  567. struct mesh_virtual *virt = NULL;
  568. virt = add_virtual(label);
  569. if (!virt)
  570. return MESH_STATUS_STORAGE_FAIL;
  571. if (!mod->pub)
  572. mod->pub = l_new(struct mesh_model_pub, 1);
  573. mod->pub->virt = virt;
  574. return set_pub(mod, virt->addr, idx, cred_flag, ttl, period, cnt,
  575. interval);
  576. }
  577. static int add_virt_sub(struct mesh_net *net, struct mesh_model *mod,
  578. const uint8_t *label, uint16_t *addr)
  579. {
  580. struct mesh_virtual *virt = l_queue_find(mod->virtuals,
  581. find_virt_by_label, label);
  582. if (!virt) {
  583. virt = add_virtual(label);
  584. if (!virt)
  585. return MESH_STATUS_INSUFF_RESOURCES;
  586. l_queue_push_head(mod->virtuals, virt);
  587. mesh_net_dst_reg(net, virt->addr);
  588. l_debug("Added virtual sub addr %4.4x", virt->addr);
  589. }
  590. if (addr)
  591. *addr = virt->addr;
  592. return MESH_STATUS_SUCCESS;
  593. }
  594. static int add_sub(struct mesh_net *net, struct mesh_model *mod,
  595. uint16_t addr)
  596. {
  597. if (!mod->subs)
  598. mod->subs = l_queue_new();
  599. if (l_queue_find(mod->subs, simple_match, L_UINT_TO_PTR(addr)))
  600. return MESH_STATUS_SUCCESS;
  601. if ((l_queue_length(mod->subs) + l_queue_length(mod->virtuals)) >=
  602. MAX_MODEL_SUBS)
  603. return MESH_STATUS_INSUFF_RESOURCES;
  604. l_queue_push_tail(mod->subs, L_UINT_TO_PTR(addr));
  605. mesh_net_dst_reg(net, addr);
  606. l_debug("Added group subscription %4.4x", addr);
  607. return MESH_STATUS_SUCCESS;
  608. }
  609. static void send_dev_key_msg_rcvd(struct mesh_node *node, uint8_t ele_idx,
  610. uint16_t src, uint16_t app_idx,
  611. uint16_t net_idx, uint16_t size,
  612. const uint8_t *data)
  613. {
  614. struct l_dbus *dbus = dbus_get_bus();
  615. struct l_dbus_message *msg;
  616. struct l_dbus_message_builder *builder;
  617. const char *owner;
  618. const char *path;
  619. bool remote = (app_idx != APP_IDX_DEV_LOCAL);
  620. owner = node_get_owner(node);
  621. path = node_get_element_path(node, ele_idx);
  622. if (!path || !owner)
  623. return;
  624. l_debug("Send \"DevKeyMessageReceived\"");
  625. msg = l_dbus_message_new_method_call(dbus, owner, path,
  626. MESH_ELEMENT_INTERFACE,
  627. "DevKeyMessageReceived");
  628. builder = l_dbus_message_builder_new(msg);
  629. l_dbus_message_builder_append_basic(builder, 'q', &src);
  630. l_dbus_message_builder_append_basic(builder, 'b', &remote);
  631. l_dbus_message_builder_append_basic(builder, 'q', &net_idx);
  632. dbus_append_byte_array(builder, data, size);
  633. l_dbus_message_builder_finalize(builder);
  634. l_dbus_message_builder_destroy(builder);
  635. l_dbus_send(dbus, msg);
  636. }
  637. static void send_msg_rcvd(struct mesh_node *node, uint8_t ele_idx,
  638. uint16_t src, uint16_t dst,
  639. const struct mesh_virtual *virt,
  640. uint16_t app_idx,
  641. uint16_t size, const uint8_t *data)
  642. {
  643. struct l_dbus *dbus = dbus_get_bus();
  644. struct l_dbus_message *msg;
  645. struct l_dbus_message_builder *builder;
  646. const char *owner;
  647. const char *path;
  648. owner = node_get_owner(node);
  649. path = node_get_element_path(node, ele_idx);
  650. if (!path || !owner)
  651. return;
  652. l_debug("Send \"MessageReceived\"");
  653. msg = l_dbus_message_new_method_call(dbus, owner, path,
  654. MESH_ELEMENT_INTERFACE, "MessageReceived");
  655. builder = l_dbus_message_builder_new(msg);
  656. l_dbus_message_builder_append_basic(builder, 'q', &src);
  657. l_dbus_message_builder_append_basic(builder, 'q', &app_idx);
  658. if (virt) {
  659. l_dbus_message_builder_enter_variant(builder, "ay");
  660. dbus_append_byte_array(builder, virt->label,
  661. sizeof(virt->label));
  662. l_dbus_message_builder_leave_variant(builder);
  663. } else {
  664. l_dbus_message_builder_enter_variant(builder, "q");
  665. l_dbus_message_builder_append_basic(builder, 'q', &dst);
  666. l_dbus_message_builder_leave_variant(builder);
  667. }
  668. dbus_append_byte_array(builder, data, size);
  669. l_dbus_message_builder_finalize(builder);
  670. l_dbus_message_builder_destroy(builder);
  671. l_dbus_send(dbus, msg);
  672. }
  673. bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,
  674. uint32_t iv_index, uint16_t net_idx, uint16_t src,
  675. uint16_t dst, uint8_t key_aid, const uint8_t *data,
  676. uint16_t size)
  677. {
  678. uint8_t *clear_text;
  679. struct mod_forward forward = {
  680. .src = src,
  681. .dst = dst,
  682. .data = NULL,
  683. .size = size - (szmict ? 8 : 4),
  684. .virt = NULL,
  685. };
  686. struct mesh_net *net = node_get_net(node);
  687. uint8_t num_ele;
  688. int decrypt_idx, i, ele_idx;
  689. uint16_t addr;
  690. struct mesh_virtual *decrypt_virt = NULL;
  691. bool result = false;
  692. bool is_subscription;
  693. l_debug("iv_index %8.8x key_aid = %2.2x", iv_index, key_aid);
  694. if (!dst)
  695. return false;
  696. ele_idx = node_get_element_idx(node, dst);
  697. if (dst < 0x8000 && ele_idx < 0)
  698. /* Unicast and not addressed to us */
  699. return false;
  700. clear_text = l_malloc(size);
  701. forward.data = clear_text;
  702. /*
  703. * The packet needs to be decoded by the correct key which
  704. * is hinted by key_aid, but is not necessarily definitive
  705. */
  706. if (key_aid == APP_AID_DEV)
  707. decrypt_idx = dev_packet_decrypt(node, data, size, szmict, src,
  708. dst, key_aid, seq0, iv_index,
  709. clear_text);
  710. else if ((dst & 0xc000) == 0x8000)
  711. decrypt_idx = virt_packet_decrypt(net, data, size, szmict, src,
  712. dst, key_aid, seq0,
  713. iv_index, clear_text,
  714. &decrypt_virt);
  715. else
  716. decrypt_idx = app_packet_decrypt(net, data, size, szmict, src,
  717. dst, NULL, 0,
  718. key_aid, seq0, iv_index,
  719. clear_text);
  720. if (decrypt_idx < 0) {
  721. l_error("model.c - Failed to decrypt application payload");
  722. result = false;
  723. goto done;
  724. }
  725. print_packet("Clr Rx", clear_text, size - (szmict ? 8 : 4));
  726. forward.virt = decrypt_virt;
  727. forward.app_idx = decrypt_idx;
  728. forward.net_idx = net_idx;
  729. num_ele = node_get_num_elements(node);
  730. addr = node_get_primary(node);
  731. if (!num_ele || IS_UNASSIGNED(addr))
  732. goto done;
  733. is_subscription = !(IS_UNICAST(dst));
  734. for (i = 0; i < num_ele; i++) {
  735. struct l_queue *models;
  736. if (!is_subscription && ele_idx != i)
  737. continue;
  738. forward.unicast = addr + i;
  739. forward.has_dst = false;
  740. models = node_get_element_models(node, i);
  741. /* Internal models */
  742. l_queue_foreach(models, forward_model, &forward);
  743. /*
  744. * Cycle through external models if the message has not been
  745. * handled by internal models
  746. */
  747. if (forward.has_dst && !forward.done) {
  748. if ((decrypt_idx & APP_IDX_MASK) == decrypt_idx)
  749. send_msg_rcvd(node, i, src, dst, decrypt_virt,
  750. forward.app_idx, forward.size,
  751. forward.data);
  752. else if (decrypt_idx == APP_IDX_DEV_REMOTE ||
  753. decrypt_idx == APP_IDX_DEV_LOCAL)
  754. send_dev_key_msg_rcvd(node, i, src, decrypt_idx,
  755. net_idx, forward.size,
  756. forward.data);
  757. }
  758. /*
  759. * Either the message has been processed internally or
  760. * has been passed on to an external model.
  761. */
  762. result |= forward.has_dst | forward.done;
  763. /* If the message was to unicast address, we are done */
  764. if (!is_subscription && ele_idx == i)
  765. break;
  766. /*
  767. * For the fixed group addresses, i.e., all-proxies,
  768. * all-friends, all-relays, all-nodes, the message is delivered
  769. * to a primary element only.
  770. */
  771. if (IS_FIXED_GROUP_ADDRESS(dst))
  772. break;
  773. }
  774. done:
  775. l_free(clear_text);
  776. return result;
  777. }
  778. int mesh_model_publish(struct mesh_node *node, uint32_t id, uint16_t src,
  779. bool segmented, uint16_t msg_len, const void *msg)
  780. {
  781. struct mesh_net *net = node_get_net(node);
  782. struct mesh_model *mod;
  783. uint8_t *label = NULL;
  784. uint16_t net_idx;
  785. bool result;
  786. int ele_idx;
  787. if (!net || msg_len > 380)
  788. return MESH_ERROR_INVALID_ARGS;
  789. /* If SRC is 0, use the Primary Element */
  790. if (src == 0)
  791. src = mesh_net_get_address(net);
  792. ele_idx = node_get_element_idx(node, src);
  793. if (ele_idx < 0)
  794. return MESH_ERROR_NOT_FOUND;
  795. mod = get_model(node, (uint8_t) ele_idx, id);
  796. if (!mod)
  797. return MESH_ERROR_NOT_FOUND;
  798. if (!mod->pub) {
  799. l_debug("publication doesn't exist (model %x)", id);
  800. return MESH_ERROR_DOES_NOT_EXIST;
  801. }
  802. if (IS_UNASSIGNED(mod->pub->addr))
  803. return MESH_ERROR_DOES_NOT_EXIST;
  804. if (mod->pub->virt)
  805. label = mod->pub->virt->label;
  806. net_idx = appkey_net_idx(net, mod->pub->idx);
  807. result = msg_send(node, mod->pub->credential != 0, src, mod->pub->addr,
  808. mod->pub->idx, net_idx, label, mod->pub->ttl,
  809. mod->pub->rtx.cnt, mod->pub->rtx.interval,
  810. segmented, msg, msg_len);
  811. return result ? MESH_ERROR_NONE : MESH_ERROR_FAILED;
  812. }
  813. bool mesh_model_send(struct mesh_node *node, uint16_t src, uint16_t dst,
  814. uint16_t app_idx, uint16_t net_idx,
  815. uint8_t ttl, bool segmented,
  816. uint16_t msg_len, const void *msg)
  817. {
  818. struct mesh_net *net = node_get_net(node);
  819. uint8_t cnt;
  820. uint16_t interval;
  821. /* If SRC is 0, use the Primary Element */
  822. if (src == 0)
  823. src = node_get_primary(node);
  824. if (IS_UNASSIGNED(dst))
  825. return false;
  826. mesh_net_transmit_params_get(net, &cnt, &interval);
  827. return msg_send(node, false, src, dst, app_idx, net_idx, NULL, ttl, cnt,
  828. interval, segmented, msg, msg_len);
  829. }
  830. int mesh_model_pub_set(struct mesh_node *node, uint16_t addr, uint32_t id,
  831. const uint8_t *pub_addr, uint16_t idx, bool cred_flag,
  832. uint8_t ttl, uint8_t period, uint8_t cnt,
  833. uint16_t interval, bool is_virt, uint16_t *pub_dst)
  834. {
  835. struct mesh_model *mod;
  836. int status, ele_idx = node_get_element_idx(node, addr);
  837. if (ele_idx < 0)
  838. return MESH_STATUS_INVALID_ADDRESS;
  839. mod = get_model(node, (uint8_t) ele_idx, id);
  840. if (!mod)
  841. return MESH_STATUS_INVALID_MODEL;
  842. if (!mod->pub_enabled || (mod->cbs && !(mod->cbs->pub)))
  843. return MESH_STATUS_INVALID_PUB_PARAM;
  844. if (!appkey_have_key(node_get_net(node), idx))
  845. return MESH_STATUS_INVALID_APPKEY;
  846. /*
  847. * If the publication address is set to unassigned address value,
  848. * remove the publication
  849. */
  850. if (!is_virt && IS_UNASSIGNED(l_get_le16(pub_addr))) {
  851. remove_pub(node, ele_idx, mod);
  852. return MESH_STATUS_SUCCESS;
  853. }
  854. if (cred_flag && node_lpn_mode_get(node) != MESH_MODE_ENABLED)
  855. return MESH_STATUS_FEATURE_NO_SUPPORT;
  856. /* Check if the old publication destination is a virtual label */
  857. if (mod->pub && mod->pub->virt) {
  858. unref_virt(mod->pub->virt);
  859. mod->pub->virt = NULL;
  860. }
  861. if (!is_virt) {
  862. status = set_pub(mod, l_get_le16(pub_addr), idx, cred_flag,
  863. ttl, period, cnt, interval);
  864. } else
  865. status = set_virt_pub(mod, pub_addr, idx, cred_flag, ttl,
  866. period, cnt, interval);
  867. if (status != MESH_STATUS_SUCCESS)
  868. return status;
  869. *pub_dst = mod->pub->addr;
  870. if (!mod->cbs)
  871. /* External model */
  872. config_update_model_pub_period(node, ele_idx, id,
  873. pub_period_to_ms(period));
  874. else {
  875. /* Internal model, call registered callbacks */
  876. if (mod->cbs->pub)
  877. mod->cbs->pub(mod->pub);
  878. }
  879. return MESH_STATUS_SUCCESS;
  880. }
  881. struct mesh_model_pub *mesh_model_pub_get(struct mesh_node *node, uint16_t addr,
  882. uint32_t mod_id, int *status)
  883. {
  884. struct mesh_model *mod;
  885. int ele_idx = node_get_element_idx(node, addr);
  886. if (ele_idx < 0) {
  887. *status = MESH_STATUS_INVALID_ADDRESS;
  888. return NULL;
  889. }
  890. mod = get_model(node, (uint8_t) ele_idx, mod_id);
  891. if (!mod) {
  892. *status = MESH_STATUS_INVALID_MODEL;
  893. return NULL;
  894. }
  895. if (!mod->pub_enabled || (mod->cbs && !(mod->cbs->pub)))
  896. *status = MESH_STATUS_INVALID_PUB_PARAM;
  897. else
  898. *status = MESH_STATUS_SUCCESS;
  899. return mod->pub;
  900. }
  901. void mesh_model_free(void *data)
  902. {
  903. struct mesh_model *mod = data;
  904. l_queue_destroy(mod->bindings, NULL);
  905. l_queue_destroy(mod->subs, NULL);
  906. l_queue_destroy(mod->virtuals, unref_virt);
  907. l_free(mod->pub);
  908. l_free(mod);
  909. }
  910. static void remove_subs(struct mesh_node *node, struct mesh_model *mod)
  911. {
  912. const struct l_queue_entry *entry;
  913. struct mesh_net *net = node_get_net(node);
  914. entry = l_queue_get_entries(mod->subs);
  915. for (; entry; entry = entry->next)
  916. mesh_net_dst_unreg(net, (uint16_t) L_PTR_TO_UINT(entry->data));
  917. l_queue_clear(mod->subs, NULL);
  918. l_queue_clear(mod->virtuals, unref_virt);
  919. }
  920. static struct mesh_model *model_new(uint32_t id)
  921. {
  922. struct mesh_model *mod = l_new(struct mesh_model, 1);
  923. mod->id = id;
  924. mod->virtuals = l_queue_new();
  925. /*
  926. * Unless specifically indicated by an app, subscriptions and
  927. * publications are enabled by default
  928. */
  929. mod->sub_enabled = true;
  930. mod->pub_enabled = true;
  931. return mod;
  932. }
  933. static void model_enable_pub(struct mesh_model *mod, bool enable)
  934. {
  935. mod->pub_enabled = enable;
  936. if (!mod->pub_enabled && mod->pub) {
  937. if (mod->pub->virt)
  938. unref_virt(mod->pub->virt);
  939. l_free(mod->pub);
  940. mod->pub = NULL;
  941. }
  942. }
  943. static void model_enable_sub(struct mesh_node *node, struct mesh_model *mod,
  944. bool enable)
  945. {
  946. mod->sub_enabled = enable;
  947. if (!mod->sub_enabled)
  948. remove_subs(node, mod);
  949. }
  950. static bool get_model_options(struct mesh_model *mod,
  951. struct l_dbus_message_iter *opts)
  952. {
  953. const char *key;
  954. struct l_dbus_message_iter var;
  955. bool opt;
  956. while (l_dbus_message_iter_next_entry(opts, &key, &var)) {
  957. if (!strcmp(key, "Publish")) {
  958. if (!l_dbus_message_iter_get_variant(&var, "b", &opt))
  959. return false;
  960. mod->pub_enabled = opt;
  961. } else if (!strcmp(key, "Subscribe")) {
  962. if (!l_dbus_message_iter_get_variant(&var, "b", &opt))
  963. return false;
  964. mod->sub_enabled = opt;
  965. } else
  966. return false;
  967. }
  968. return true;
  969. }
  970. bool mesh_model_add(struct mesh_node *node, struct l_queue *mods,
  971. uint32_t id, struct l_dbus_message_iter *opts)
  972. {
  973. struct mesh_model *mod;
  974. /* Disallow duplicates */
  975. mod = l_queue_find(mods, match_model_id, L_UINT_TO_PTR(id));
  976. if (mod)
  977. return false;
  978. mod = model_new(id);
  979. if (opts && !get_model_options(mod, opts)) {
  980. mesh_model_free(mod);
  981. return false;
  982. }
  983. l_queue_insert(mods, mod, compare_model_id, NULL);
  984. return true;
  985. }
  986. /* Internal models only */
  987. static void restore_model_state(struct mesh_model *mod)
  988. {
  989. const struct mesh_model_ops *cbs;
  990. const struct l_queue_entry *b;
  991. cbs = mod->cbs;
  992. if (!cbs)
  993. return;
  994. if (!l_queue_isempty(mod->bindings) && cbs->bind) {
  995. for (b = l_queue_get_entries(mod->bindings); b; b = b->next) {
  996. if (cbs->bind(L_PTR_TO_UINT(b->data), ACTION_ADD) !=
  997. MESH_STATUS_SUCCESS)
  998. break;
  999. }
  1000. }
  1001. if (mod->pub && cbs->pub)
  1002. cbs->pub(mod->pub);
  1003. }
  1004. /* This registers an internal model, i.e. implemented within meshd */
  1005. bool mesh_model_register(struct mesh_node *node, uint8_t ele_idx, uint32_t id,
  1006. const struct mesh_model_ops *cbs,
  1007. void *user_data)
  1008. {
  1009. struct mesh_model *mod;
  1010. mod = get_model(node, ele_idx, id);
  1011. if (!mod)
  1012. return MESH_STATUS_INVALID_MODEL;
  1013. mod->cbs = cbs;
  1014. mod->user_data = user_data;
  1015. restore_model_state(mod);
  1016. return true;
  1017. }
  1018. void mesh_model_app_key_delete(struct mesh_node *node, uint16_t ele_idx,
  1019. struct l_queue *models, uint16_t app_idx)
  1020. {
  1021. const struct l_queue_entry *entry = l_queue_get_entries(models);
  1022. for (; entry; entry = entry->next) {
  1023. struct mesh_model *mod = entry->data;
  1024. model_unbind_idx(node, ele_idx, mod, app_idx);
  1025. }
  1026. }
  1027. int mesh_model_binding_del(struct mesh_node *node, uint16_t addr, uint32_t id,
  1028. uint16_t app_idx)
  1029. {
  1030. return update_binding(node, addr, id, app_idx, true);
  1031. }
  1032. int mesh_model_binding_add(struct mesh_node *node, uint16_t addr, uint32_t id,
  1033. uint16_t app_idx)
  1034. {
  1035. return update_binding(node, addr, id, app_idx, false);
  1036. }
  1037. int mesh_model_get_bindings(struct mesh_node *node, uint16_t addr, uint32_t id,
  1038. uint8_t *buf, uint16_t buf_size, uint16_t *size)
  1039. {
  1040. struct mesh_model *mod;
  1041. const struct l_queue_entry *entry;
  1042. uint16_t n;
  1043. uint32_t idx_pair;
  1044. int i, ele_idx = node_get_element_idx(node, addr);
  1045. if (ele_idx < 0)
  1046. return MESH_STATUS_INVALID_ADDRESS;
  1047. mod = get_model(node, (uint8_t) ele_idx, id);
  1048. if (!mod) {
  1049. *size = 0;
  1050. return MESH_STATUS_INVALID_MODEL;
  1051. }
  1052. entry = l_queue_get_entries(mod->bindings);
  1053. n = 0;
  1054. i = 0;
  1055. idx_pair = 0;
  1056. for (; entry; entry = entry->next) {
  1057. uint16_t app_idx = (uint16_t) (L_PTR_TO_UINT(entry->data));
  1058. if (!(i & 0x1)) {
  1059. idx_pair = app_idx;
  1060. } else {
  1061. idx_pair <<= 12;
  1062. idx_pair += app_idx;
  1063. /* Unlikely, but check for overflow*/
  1064. if ((n + 3) > buf_size) {
  1065. l_warn("Binding list too large");
  1066. goto done;
  1067. }
  1068. l_put_le32(idx_pair, buf);
  1069. buf += 3;
  1070. n += 3;
  1071. }
  1072. i++;
  1073. }
  1074. /* Process the last app key if present */
  1075. if (i & 0x1 && ((n + 2) <= buf_size)) {
  1076. l_put_le16(idx_pair, buf);
  1077. n += 2;
  1078. }
  1079. done:
  1080. *size = n;
  1081. return MESH_STATUS_SUCCESS;
  1082. }
  1083. int mesh_model_sub_get(struct mesh_node *node, uint16_t ele_addr, uint32_t id,
  1084. uint8_t *buf, uint16_t buf_size, uint16_t *size)
  1085. {
  1086. int16_t n;
  1087. struct mesh_model *mod;
  1088. const struct l_queue_entry *entry;
  1089. int ele_idx = node_get_element_idx(node, ele_addr);
  1090. if (ele_idx < 0)
  1091. return MESH_STATUS_INVALID_ADDRESS;
  1092. mod = get_model(node, (uint8_t) ele_idx, id);
  1093. if (!mod)
  1094. return MESH_STATUS_INVALID_MODEL;
  1095. if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
  1096. return MESH_STATUS_NOT_SUB_MOD;
  1097. entry = l_queue_get_entries(mod->subs);
  1098. *size = 0;
  1099. n = 0;
  1100. for (; entry; entry = entry->next) {
  1101. if ((n + 2) > buf_size)
  1102. return MESH_STATUS_UNSPECIFIED_ERROR;
  1103. l_put_le16((uint16_t) L_PTR_TO_UINT(entry->data), buf);
  1104. buf += 2;
  1105. n += 2;
  1106. }
  1107. entry = l_queue_get_entries(mod->virtuals);
  1108. for (; entry; entry = entry->next) {
  1109. struct mesh_virtual *virt = entry->data;
  1110. if ((n + 2) > buf_size)
  1111. return MESH_STATUS_UNSPECIFIED_ERROR;
  1112. l_put_le16((uint16_t) L_PTR_TO_UINT(virt->addr), buf);
  1113. buf += 2;
  1114. n += 2;
  1115. }
  1116. *size = n;
  1117. return MESH_STATUS_SUCCESS;
  1118. }
  1119. int mesh_model_sub_add(struct mesh_node *node, uint16_t ele_addr, uint32_t id,
  1120. uint16_t addr)
  1121. {
  1122. struct mesh_model *mod;
  1123. int status, ele_idx = node_get_element_idx(node, ele_addr);
  1124. if (ele_idx < 0)
  1125. return MESH_STATUS_INVALID_ADDRESS;
  1126. mod = get_model(node, (uint8_t) ele_idx, id);
  1127. if (!mod)
  1128. return MESH_STATUS_INVALID_MODEL;
  1129. if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
  1130. return MESH_STATUS_NOT_SUB_MOD;
  1131. status = add_sub(node_get_net(node), mod, addr);
  1132. if (status != MESH_STATUS_SUCCESS)
  1133. return status;
  1134. if (!mod->cbs)
  1135. /* External models */
  1136. cfg_update_model_subs(node, ele_idx, mod);
  1137. return MESH_STATUS_SUCCESS;
  1138. }
  1139. int mesh_model_virt_sub_add(struct mesh_node *node, uint16_t ele_addr,
  1140. uint32_t id, const uint8_t *label,
  1141. uint16_t *pub_addr)
  1142. {
  1143. struct mesh_model *mod;
  1144. int status, ele_idx = node_get_element_idx(node, ele_addr);
  1145. if (ele_idx < 0)
  1146. return MESH_STATUS_INVALID_ADDRESS;
  1147. mod = get_model(node, (uint8_t) ele_idx, id);
  1148. if (!mod)
  1149. return MESH_STATUS_INVALID_MODEL;
  1150. if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
  1151. return MESH_STATUS_NOT_SUB_MOD;
  1152. status = add_virt_sub(node_get_net(node), mod, label, pub_addr);
  1153. if (status != MESH_STATUS_SUCCESS)
  1154. return status;
  1155. if (!mod->cbs)
  1156. /* External models */
  1157. cfg_update_model_subs(node, ele_idx, mod);
  1158. return MESH_STATUS_SUCCESS;
  1159. }
  1160. int mesh_model_sub_ovrt(struct mesh_node *node, uint16_t ele_addr, uint32_t id,
  1161. uint16_t addr)
  1162. {
  1163. struct mesh_model *mod;
  1164. int ele_idx = node_get_element_idx(node, ele_addr);
  1165. if (ele_idx < 0)
  1166. return MESH_STATUS_INVALID_ADDRESS;
  1167. mod = get_model(node, (uint8_t) ele_idx, id);
  1168. if (!mod)
  1169. return MESH_STATUS_INVALID_MODEL;
  1170. if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
  1171. return MESH_STATUS_NOT_SUB_MOD;
  1172. l_queue_clear(mod->subs, NULL);
  1173. l_queue_clear(mod->virtuals, unref_virt);
  1174. add_sub(node_get_net(node), mod, addr);
  1175. if (!mod->cbs)
  1176. /* External models */
  1177. cfg_update_model_subs(node, ele_idx, mod);
  1178. return MESH_STATUS_SUCCESS;
  1179. }
  1180. int mesh_model_virt_sub_ovrt(struct mesh_node *node, uint16_t ele_addr,
  1181. uint32_t id, const uint8_t *label,
  1182. uint16_t *addr)
  1183. {
  1184. struct mesh_model *mod;
  1185. int status, ele_idx = node_get_element_idx(node, ele_addr);
  1186. if (ele_idx < 0)
  1187. return MESH_STATUS_INVALID_ADDRESS;
  1188. mod = get_model(node, (uint8_t) ele_idx, id);
  1189. if (!mod)
  1190. return MESH_STATUS_INVALID_MODEL;
  1191. if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
  1192. return MESH_STATUS_NOT_SUB_MOD;
  1193. l_queue_clear(mod->subs, NULL);
  1194. l_queue_clear(mod->virtuals, unref_virt);
  1195. status = add_virt_sub(node_get_net(node), mod, label, addr);
  1196. if (!mod->cbs)
  1197. /* External models */
  1198. cfg_update_model_subs(node, ele_idx, mod);
  1199. return status;
  1200. }
  1201. int mesh_model_sub_del(struct mesh_node *node, uint16_t ele_addr, uint32_t id,
  1202. uint16_t addr)
  1203. {
  1204. struct mesh_model *mod;
  1205. int ele_idx = node_get_element_idx(node, ele_addr);
  1206. if (ele_idx < 0)
  1207. return MESH_STATUS_INVALID_ADDRESS;
  1208. mod = get_model(node, (uint8_t) ele_idx, id);
  1209. if (!mod)
  1210. return MESH_STATUS_INVALID_MODEL;
  1211. if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
  1212. return MESH_STATUS_NOT_SUB_MOD;
  1213. if (l_queue_remove(mod->subs, L_UINT_TO_PTR(addr))) {
  1214. mesh_net_dst_unreg(node_get_net(node), addr);
  1215. if (!mod->cbs)
  1216. /* External models */
  1217. cfg_update_model_subs(node, ele_idx, mod);
  1218. }
  1219. return MESH_STATUS_SUCCESS;
  1220. }
  1221. int mesh_model_virt_sub_del(struct mesh_node *node, uint16_t ele_addr,
  1222. uint32_t id, const uint8_t *label,
  1223. uint16_t *addr)
  1224. {
  1225. struct mesh_model *mod;
  1226. struct mesh_virtual *virt;
  1227. int ele_idx = node_get_element_idx(node, ele_addr);
  1228. if (ele_idx < 0)
  1229. return MESH_STATUS_INVALID_ADDRESS;
  1230. mod = get_model(node, (uint8_t) ele_idx, id);
  1231. if (!mod)
  1232. return MESH_STATUS_INVALID_MODEL;
  1233. if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
  1234. return MESH_STATUS_NOT_SUB_MOD;
  1235. virt = l_queue_remove_if(mod->virtuals, find_virt_by_label, label);
  1236. if (virt) {
  1237. *addr = virt->addr;
  1238. unref_virt(virt);
  1239. } else {
  1240. *addr = UNASSIGNED_ADDRESS;
  1241. return MESH_STATUS_SUCCESS;
  1242. }
  1243. if (l_queue_remove(mod->subs, L_UINT_TO_PTR(*addr))) {
  1244. mesh_net_dst_unreg(node_get_net(node), *addr);
  1245. if (!mod->cbs)
  1246. /* External models */
  1247. cfg_update_model_subs(node, ele_idx, mod);
  1248. }
  1249. return MESH_STATUS_SUCCESS;
  1250. }
  1251. int mesh_model_sub_del_all(struct mesh_node *node, uint16_t ele_addr,
  1252. uint32_t id)
  1253. {
  1254. struct mesh_model *mod;
  1255. int ele_idx = node_get_element_idx(node, ele_addr);
  1256. if (ele_idx < 0)
  1257. return MESH_STATUS_INVALID_ADDRESS;
  1258. mod = get_model(node, (uint8_t) ele_idx, id);
  1259. if (!mod)
  1260. return MESH_STATUS_INVALID_MODEL;
  1261. if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
  1262. return MESH_STATUS_NOT_SUB_MOD;
  1263. remove_subs(node, mod);
  1264. if (!mod->cbs)
  1265. /* External models */
  1266. cfg_update_model_subs(node, ele_idx, mod);
  1267. return MESH_STATUS_SUCCESS;
  1268. }
  1269. static struct mesh_model *model_setup(struct mesh_net *net, uint8_t ele_idx,
  1270. struct mesh_config_model *db_mod)
  1271. {
  1272. struct mesh_model *mod;
  1273. struct mesh_config_pub *pub = db_mod->pub;
  1274. uint32_t i;
  1275. if (db_mod->num_bindings > MAX_MODEL_BINDINGS) {
  1276. l_warn("Binding list too long %u (max %u)",
  1277. db_mod->num_bindings, MAX_MODEL_BINDINGS);
  1278. return NULL;
  1279. }
  1280. mod = model_new(db_mod->vendor ? db_mod->id :
  1281. SET_ID(SIG_VENDOR, db_mod->id));
  1282. /* Implicitly bind config server model to device key */
  1283. if (db_mod->id == CONFIG_SRV_MODEL) {
  1284. if (ele_idx != PRIMARY_ELE_IDX) {
  1285. l_free(mod);
  1286. return NULL;
  1287. }
  1288. l_queue_push_head(mod->bindings,
  1289. L_UINT_TO_PTR(APP_IDX_DEV_LOCAL));
  1290. return mod;
  1291. }
  1292. if (db_mod->id == CONFIG_CLI_MODEL) {
  1293. l_queue_push_head(mod->bindings,
  1294. L_UINT_TO_PTR(APP_IDX_DEV_LOCAL));
  1295. return mod;
  1296. }
  1297. /* Add application key bindings if present */
  1298. if (db_mod->bindings) {
  1299. mod->bindings = l_queue_new();
  1300. for (i = 0; i < db_mod->num_bindings; i++)
  1301. l_queue_push_tail(mod->bindings,
  1302. L_UINT_TO_PTR(db_mod->bindings[i]));
  1303. }
  1304. mod->pub_enabled = db_mod->pub_enabled;
  1305. /* Add publication if enabled and present */
  1306. if (mod->pub_enabled && pub) {
  1307. if (pub->virt)
  1308. set_virt_pub(mod, pub->virt_addr, pub->idx,
  1309. pub->credential, pub->ttl, pub->period,
  1310. pub->cnt, pub->interval);
  1311. else if (!IS_UNASSIGNED(pub->addr))
  1312. set_pub(mod, pub->addr, pub->idx, pub->credential,
  1313. pub->ttl, pub->period, pub->cnt, pub->interval);
  1314. }
  1315. mod->sub_enabled = db_mod->sub_enabled;
  1316. /* Add subscriptions if enabled and present */
  1317. if (!db_mod->subs || !mod->sub_enabled)
  1318. return mod;
  1319. for (i = 0; i < db_mod->num_subs; i++) {
  1320. struct mesh_config_sub *sub = &db_mod->subs[i];
  1321. if (!sub->virt)
  1322. add_sub(net, mod, sub->addr.grp);
  1323. else
  1324. add_virt_sub(net, mod, sub->addr.label, NULL);
  1325. }
  1326. return mod;
  1327. }
  1328. bool mesh_model_add_from_storage(struct mesh_node *node, uint8_t ele_idx,
  1329. struct l_queue *mods, struct l_queue *db_mods)
  1330. {
  1331. struct mesh_net *net = node_get_net(node);
  1332. const struct l_queue_entry *entry;
  1333. /* Allow empty elements */
  1334. if (!db_mods)
  1335. return true;
  1336. entry = l_queue_get_entries(db_mods);
  1337. for (; entry; entry = entry->next) {
  1338. struct mesh_model *mod;
  1339. struct mesh_config_model *db_mod;
  1340. uint32_t id;
  1341. db_mod = entry->data;
  1342. id = db_mod->vendor ? db_mod->id :
  1343. SET_ID(SIG_VENDOR, db_mod->id);
  1344. if (l_queue_find(mods, match_model_id, L_UINT_TO_PTR(id)))
  1345. return false;
  1346. mod = model_setup(net, ele_idx, db_mod);
  1347. if (!mod)
  1348. return false;
  1349. l_queue_insert(mods, mod, compare_model_id, NULL);
  1350. }
  1351. return true;
  1352. }
  1353. void mesh_model_convert_to_storage(struct l_queue *db_mods,
  1354. struct l_queue *mods)
  1355. {
  1356. const struct l_queue_entry *entry = l_queue_get_entries(mods);
  1357. for (; entry; entry = entry->next) {
  1358. struct mesh_model *mod = entry->data;
  1359. struct mesh_config_model *db_mod;
  1360. db_mod = l_new(struct mesh_config_model, 1);
  1361. db_mod->id = mod->id;
  1362. db_mod->vendor = IS_VENDOR(mod->id);
  1363. db_mod->pub_enabled = mod->pub_enabled;
  1364. db_mod->sub_enabled = mod->sub_enabled;
  1365. l_queue_push_tail(db_mods, db_mod);
  1366. }
  1367. }
  1368. uint16_t mesh_model_opcode_set(uint32_t opcode, uint8_t *buf)
  1369. {
  1370. if (opcode <= 0x7e) {
  1371. buf[0] = opcode;
  1372. return 1;
  1373. }
  1374. if (opcode >= 0x8000 && opcode <= 0xbfff) {
  1375. l_put_be16(opcode, buf);
  1376. return 2;
  1377. }
  1378. if (opcode >= 0xc00000 && opcode <= 0xffffff) {
  1379. buf[0] = (opcode >> 16) & 0xff;
  1380. l_put_be16(opcode, buf + 1);
  1381. return 3;
  1382. }
  1383. l_debug("Illegal Opcode %x", opcode);
  1384. return 0;
  1385. }
  1386. bool mesh_model_opcode_get(const uint8_t *buf, uint16_t size,
  1387. uint32_t *opcode, uint16_t *n)
  1388. {
  1389. if (!n || !opcode || size < 1)
  1390. return false;
  1391. switch (buf[0] & 0xc0) {
  1392. case 0x00:
  1393. case 0x40:
  1394. /* RFU */
  1395. if (buf[0] == 0x7f)
  1396. return false;
  1397. *n = 1;
  1398. *opcode = buf[0];
  1399. break;
  1400. case 0x80:
  1401. if (size < 2)
  1402. return false;
  1403. *n = 2;
  1404. *opcode = l_get_be16(buf);
  1405. break;
  1406. case 0xc0:
  1407. if (size < 3)
  1408. return false;
  1409. *n = 3;
  1410. *opcode = l_get_be16(buf + 1);
  1411. *opcode |= buf[0] << 16;
  1412. break;
  1413. default:
  1414. print_packet("Bad", buf, size);
  1415. return false;
  1416. }
  1417. return true;
  1418. }
  1419. void mesh_model_build_config(void *model, void *msg_builder)
  1420. {
  1421. struct l_dbus_message_builder *builder = msg_builder;
  1422. struct mesh_model *mod = model;
  1423. uint16_t id;
  1424. if (is_internal(mod->id))
  1425. return;
  1426. if (!l_queue_length(mod->subs) && !l_queue_length(mod->virtuals) &&
  1427. !mod->pub && !l_queue_length(mod->bindings))
  1428. return;
  1429. l_dbus_message_builder_enter_struct(builder, "qa{sv}");
  1430. /* Model id */
  1431. id = MODEL_ID(mod->id);
  1432. l_dbus_message_builder_append_basic(builder, 'q', &id);
  1433. l_dbus_message_builder_enter_array(builder, "{sv}");
  1434. /* For vendor models, add vendor id */
  1435. if (IS_VENDOR(mod->id)) {
  1436. uint16_t vendor = VENDOR_ID(mod->id);
  1437. dbus_append_dict_entry_basic(builder, "Vendor", "q", &vendor);
  1438. }
  1439. /* Model bindings, if present */
  1440. if (l_queue_length(mod->bindings))
  1441. append_dict_uint16_array(builder, mod->bindings, "Bindings");
  1442. /* Model periodic publication interval, if present */
  1443. if (mod->pub) {
  1444. uint32_t period = pub_period_to_ms(mod->pub->period);
  1445. dbus_append_dict_entry_basic(builder, "PublicationPeriod", "u",
  1446. &period);
  1447. }
  1448. if (l_queue_length(mod->subs) || l_queue_length(mod->virtuals))
  1449. append_dict_subs_array(builder, mod->subs, mod->virtuals,
  1450. "Subscriptions");
  1451. l_dbus_message_builder_leave_array(builder);
  1452. l_dbus_message_builder_leave_struct(builder);
  1453. }
  1454. void mesh_model_update_opts(struct mesh_node *node, uint8_t ele_idx,
  1455. struct l_queue *curr, struct l_queue *updated)
  1456. {
  1457. uint16_t primary;
  1458. const struct l_queue_entry *entry;
  1459. primary = node_get_primary(node);
  1460. entry = l_queue_get_entries(curr);
  1461. for (; entry; entry = entry->next) {
  1462. struct mesh_model *mod, *updated_mod = entry->data;
  1463. uint32_t id = updated_mod->id;
  1464. bool updated_opt, vendor = IS_VENDOR(id);
  1465. mod = l_queue_find(curr, match_model_id, L_UINT_TO_PTR(id));
  1466. if (!mod)
  1467. continue;
  1468. if (!vendor)
  1469. id = MODEL_ID(id);
  1470. updated_opt = updated_mod->pub_enabled;
  1471. if (mod->pub_enabled != updated_opt) {
  1472. model_enable_pub(mod, updated_opt);
  1473. mesh_config_model_pub_enable(node_config_get(node),
  1474. primary + ele_idx, id,
  1475. vendor, updated_opt);
  1476. }
  1477. updated_opt = updated_mod->sub_enabled;
  1478. if (mod->pub_enabled != updated_opt) {
  1479. model_enable_sub(node, mod, updated_opt);
  1480. mesh_config_model_sub_enable(node_config_get(node),
  1481. primary + ele_idx, id,
  1482. vendor, updated_opt);
  1483. }
  1484. }
  1485. }
  1486. /* Populate composition buffer with model IDs */
  1487. uint16_t mesh_model_generate_composition(struct l_queue *mods, uint16_t buf_sz,
  1488. uint8_t *buf)
  1489. {
  1490. const struct l_queue_entry *entry;
  1491. uint8_t num_s = 0, num_v = 0;
  1492. uint8_t *mod_buf;
  1493. uint16_t n;
  1494. /* Store models IDs, store num_s and num_v later */
  1495. mod_buf = buf;
  1496. n = 2;
  1497. entry = l_queue_get_entries(mods);
  1498. /* Get SIG models */
  1499. for (; entry; entry = entry->next) {
  1500. struct mesh_model *mod = entry->data;
  1501. if (n + 2 > buf_sz)
  1502. goto done;
  1503. if (IS_VENDOR(mod->id))
  1504. continue;
  1505. l_put_le16((uint16_t) (MODEL_ID(mod->id)), buf + n);
  1506. n += 2;
  1507. num_s++;
  1508. }
  1509. /* Get vendor models */
  1510. entry = l_queue_get_entries(mods);
  1511. for (; entry; entry = entry->next) {
  1512. struct mesh_model *mod = entry->data;
  1513. uint16_t vendor_id;
  1514. if (n + 4 > buf_sz)
  1515. goto done;
  1516. if (!IS_VENDOR(mod->id))
  1517. continue;
  1518. vendor_id = (uint16_t) (VENDOR_ID(mod->id));
  1519. l_put_le16(vendor_id, buf + n);
  1520. n += 2;
  1521. l_put_le16((uint16_t) (MODEL_ID(mod->id)), buf + n);
  1522. n += 2;
  1523. num_v++;
  1524. }
  1525. done:
  1526. mod_buf[0] = num_s;
  1527. mod_buf[1] = num_v;
  1528. return n;
  1529. }
  1530. void mesh_model_init(void)
  1531. {
  1532. mesh_virtuals = l_queue_new();
  1533. }
  1534. void mesh_model_cleanup(void)
  1535. {
  1536. l_queue_destroy(mesh_virtuals, l_free);
  1537. mesh_virtuals = NULL;
  1538. }