3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2018-2019 Intel Corporation. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
26 #include <json-c/json.h>
28 #include "mesh/mesh-defs.h"
30 #include "mesh/mesh.h"
31 #include "mesh/crypto.h"
32 #include "mesh/node.h"
33 #include "mesh/mesh-db.h"
35 #include "mesh/appkey.h"
36 #include "mesh/cfgmod.h"
37 #include "mesh/storage.h"
38 #include "mesh/error.h"
39 #include "mesh/dbus.h"
40 #include "mesh/util.h"
41 #include "mesh/model.h"
42 #include "mesh/keyring.h"
44 /* Divide and round to ceiling (up) to calculate segment count */
45 #define CEILDIV(val, div) (((val) + (div) - 1) / (div))
48 const struct mesh_model_ops *cbs;
50 struct l_queue *bindings;
52 struct l_queue *virtuals;
53 struct mesh_model_pub *pub;
59 uint32_t id; /* Internal ID of a stored virtual addr, min val 0x10000 */
61 uint16_t addr; /* 16-bit virtual address, used in messages */
62 uint8_t label[16]; /* 128 bit label UUID */
65 /* These struct is used to pass lots of params to l_queue_foreach */
67 struct mesh_virtual *virt;
81 static struct l_queue *mesh_virtuals;
83 static uint32_t virt_id_next = VIRTUAL_BASE;
84 static struct timeval tx_start;
86 static bool is_internal(uint32_t id)
88 if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
94 static void unref_virt(void *data)
96 struct mesh_virtual *virt = data;
98 if (virt->ref_cnt > 0)
104 l_queue_remove(mesh_virtuals, virt);
108 static bool simple_match(const void *a, const void *b)
113 static bool has_binding(struct l_queue *bindings, uint16_t idx)
115 const struct l_queue_entry *l;
117 for (l = l_queue_get_entries(bindings); l; l = l->next) {
118 if (L_PTR_TO_UINT(l->data) == idx)
124 static bool find_virt_by_id(const void *a, const void *b)
126 const struct mesh_virtual *virt = a;
127 uint32_t id = L_PTR_TO_UINT(b);
129 return virt->id == id;
132 static bool find_virt_by_label(const void *a, const void *b)
134 const struct mesh_virtual *virt = a;
135 const uint8_t *label = b;
137 return memcmp(virt->label, label, 16) == 0;
140 static bool match_model_id(const void *a, const void *b)
142 const struct mesh_model *model = a;
143 uint32_t id = L_PTR_TO_UINT(b);
145 return (mesh_model_get_model_id(model) == id);
148 static struct mesh_model *get_model(struct mesh_node *node, uint8_t ele_idx,
149 uint32_t id, int *status)
151 struct l_queue *models;
152 struct mesh_model *model;
154 models = node_get_element_models(node, ele_idx, status);
156 *status = MESH_STATUS_INVALID_MODEL;
160 model = l_queue_find(models, match_model_id, L_UINT_TO_PTR(id));
162 *status = (model) ? MESH_STATUS_SUCCESS : MESH_STATUS_INVALID_MODEL;
167 static struct mesh_model *find_model(struct mesh_node *node, uint16_t addr,
168 uint32_t mod_id, int *status)
172 ele_idx = node_get_element_idx(node, addr);
175 *status = MESH_STATUS_INVALID_ADDRESS;
179 return get_model(node, (uint8_t) ele_idx, mod_id, status);
182 static uint32_t pub_period_to_ms(uint8_t pub_period)
188 switch (pub_period & 0x3) {
197 return n * 10 * 60 * 1000;
201 static struct l_dbus_message *create_config_update_msg(struct mesh_node *node,
202 uint8_t ele_idx, uint32_t id,
203 struct l_dbus_message_builder **builder)
205 struct l_dbus *dbus = dbus_get_bus();
206 struct l_dbus_message *msg;
211 owner = node_get_owner(node);
212 path = node_get_element_path(node, ele_idx);
216 l_debug("Send \"UpdateModelConfiguration\"");
217 msg = l_dbus_message_new_method_call(dbus, owner, path,
218 MESH_ELEMENT_INTERFACE,
219 "UpdateModelConfiguration");
221 *builder = l_dbus_message_builder_new(msg);
223 model_id = (uint16_t) id;
225 l_dbus_message_builder_append_basic(*builder, 'q', &model_id);
227 l_dbus_message_builder_enter_array(*builder, "{sv}");
229 if ((id & VENDOR_ID_MASK) != VENDOR_ID_MASK) {
230 uint16_t vendor = id >> 16;
231 dbus_append_dict_entry_basic(*builder, "Vendor", "q", &vendor);
237 static void config_update_model_pub_period(struct mesh_node *node,
238 uint8_t ele_idx, uint32_t model_id,
241 struct l_dbus *dbus = dbus_get_bus();
242 struct l_dbus_message *msg;
243 struct l_dbus_message_builder *builder;
245 msg = create_config_update_msg(node, ele_idx, model_id, &builder);
249 dbus_append_dict_entry_basic(builder, "PublicationPeriod", "u",
252 l_dbus_message_builder_leave_array(builder);
253 l_dbus_message_builder_finalize(builder);
254 l_dbus_message_builder_destroy(builder);
255 l_dbus_send(dbus, msg);
258 static void append_dict_uint16_array(struct l_dbus_message_builder *builder,
259 struct l_queue *q, const char *key)
261 const struct l_queue_entry *entry;
263 l_dbus_message_builder_enter_dict(builder, "sv");
264 l_dbus_message_builder_append_basic(builder, 's', key);
265 l_dbus_message_builder_enter_variant(builder, "aq");
266 l_dbus_message_builder_enter_array(builder, "q");
268 for (entry = l_queue_get_entries(q); entry; entry = entry->next) {
269 uint16_t value = (uint16_t) L_PTR_TO_UINT(entry->data);
271 l_dbus_message_builder_append_basic(builder,'q', &value);
274 l_dbus_message_builder_leave_array(builder);
275 l_dbus_message_builder_leave_variant(builder);
276 l_dbus_message_builder_leave_dict(builder);
279 static void config_update_model_bindings(struct mesh_node *node,
280 struct mesh_model *mod)
282 struct l_dbus *dbus = dbus_get_bus();
283 struct l_dbus_message *msg;
284 struct l_dbus_message_builder *builder;
286 msg = create_config_update_msg(node, mod->ele_idx, mod->id,
291 append_dict_uint16_array(builder, mod->bindings, "Bindings");
293 l_dbus_message_builder_leave_array(builder);
294 l_dbus_message_builder_finalize(builder);
295 l_dbus_message_builder_destroy(builder);
296 l_dbus_send(dbus, msg);
299 static void forward_model(void *a, void *b)
301 struct mesh_model *mod = a;
302 struct mod_forward *fwd = b;
303 struct mesh_virtual *virt;
307 l_debug("model %8.8x with idx %3.3x", mod->id, fwd->idx);
309 if (fwd->idx != APP_IDX_DEV_LOCAL && fwd->idx != APP_IDX_DEV_REMOTE &&
310 !has_binding(mod->bindings, fwd->idx))
314 if (dst == fwd->unicast || IS_ALL_NODES(dst))
316 else if (fwd->virt) {
317 virt = l_queue_find(mod->virtuals, simple_match, fwd->virt);
319 /* Check that this is not own publication */
320 if (mod->pub && (virt && virt->id == mod->pub->addr))
325 * Map Virtual addresses to a usable namespace that
326 * prevents us for forwarding a false positive
327 * (multiple Virtual Addresses that map to the same
328 * 16-bit virtual address identifier)
334 if (l_queue_find(mod->subs, simple_match, L_UINT_TO_PTR(dst)))
341 /* Return, if this is not a internal model */
348 result = mod->cbs->recv(fwd->src, dst, fwd->unicast, fwd->idx,
349 fwd->data, fwd->size, fwd->ttl, mod->user_data);
351 if (dst == fwd->unicast && result)
355 static int dev_packet_decrypt(struct mesh_node *node, const uint8_t *data,
356 uint16_t size, bool szmict, uint16_t src,
357 uint16_t dst, uint8_t key_id, uint32_t seq,
358 uint32_t iv_idx, uint8_t *out)
363 key = node_get_device_key(node);
367 if (mesh_crypto_payload_decrypt(NULL, 0, data, size, szmict, src,
368 dst, key_id, seq, iv_idx, out, key))
369 return APP_IDX_DEV_LOCAL;
371 if (!keyring_get_remote_dev_key(node, src, dev_key))
375 if (mesh_crypto_payload_decrypt(NULL, 0, data, size, szmict, src,
376 dst, key_id, seq, iv_idx, out, key))
377 return APP_IDX_DEV_REMOTE;
382 static int virt_packet_decrypt(struct mesh_net *net, const uint8_t *data,
383 uint16_t size, bool szmict, uint16_t src,
384 uint16_t dst, uint8_t key_id, uint32_t seq,
385 uint32_t iv_idx, uint8_t *out,
386 struct mesh_virtual **decrypt_virt)
388 const struct l_queue_entry *v;
390 for (v = l_queue_get_entries(mesh_virtuals); v; v = v->next) {
391 struct mesh_virtual *virt = v->data;
394 if (virt->addr != dst)
397 decrypt_idx = appkey_packet_decrypt(net, szmict, seq,
399 virt->label, 16, key_id,
402 if (decrypt_idx >= 0) {
403 *decrypt_virt = virt;
411 static void cmplt(uint16_t remote, uint8_t status,
412 void *data, uint16_t size,
415 struct timeval tx_end;
418 l_debug("Tx-->%4.4x (%d octets) Failed (%d)",
419 remote, size, status);
421 l_debug("Tx-->%4.4x (%d octets) Succeeded", remote, size);
423 /* print_packet("Sent Data", data, size); */
425 gettimeofday(&tx_end, NULL);
426 if (tx_end.tv_sec == tx_start.tv_sec) {
427 l_debug("Duration 0.%6.6lu seconds",
428 tx_end.tv_usec - tx_start.tv_usec);
430 if (tx_start.tv_usec > tx_end.tv_usec)
431 l_debug("Duration %lu.%6.6lu seconds",
432 tx_end.tv_sec - tx_start.tv_sec - 1,
433 tx_end.tv_usec + 1000000 - tx_start.tv_usec);
435 l_debug("Duration %lu.%6.6lu seconds",
436 tx_end.tv_sec - tx_start.tv_sec,
437 tx_end.tv_usec - tx_start.tv_usec);
441 static bool msg_send(struct mesh_node *node, bool credential, uint16_t src,
442 uint32_t dst, uint8_t key_id, const uint8_t *key,
443 uint8_t *label, uint8_t ttl, const void *msg, uint16_t msg_len)
446 uint32_t iv_index, seq_num;
449 uint16_t out_len = msg_len + sizeof(uint32_t);
450 struct mesh_net *net = node_get_net(node);
452 /* Use large MIC if it doesn't affect segmentation */
453 if (msg_len > 11 && msg_len <= 376) {
454 if (CEILDIV(out_len, 12) == CEILDIV(out_len + 4, 12)) {
456 out_len = msg_len + sizeof(uint64_t);
460 out = l_malloc(out_len);
462 iv_index = mesh_net_get_iv_index(net);
464 seq_num = mesh_net_get_seq_num(net);
465 if (!mesh_crypto_payload_encrypt(label, msg, out, msg_len, src, dst,
466 key_id, seq_num, iv_index, szmic, key)) {
467 l_error("Failed to Encrypt Payload");
471 /* print_packet("Encrypted with", key, 16); */
473 ret = mesh_net_app_send(net, credential, src, dst, key_id, ttl,
474 seq_num, iv_index, szmic, out, out_len,
481 static void remove_pub(struct mesh_node *node, struct mesh_model *mod)
487 /* External models */
488 config_update_model_pub_period(node, mod->ele_idx, mod->id, 0);
489 else if (mod->cbs && mod->cbs->pub)
490 /* Internal models */
494 static void model_unbind_idx(struct mesh_node *node, struct mesh_model *mod,
497 l_queue_remove(mod->bindings, L_UINT_TO_PTR(idx));
501 config_update_model_bindings(node, mod);
502 else if (mod->cbs->bind)
504 mod->cbs->bind(idx, ACTION_DELETE);
506 if (mod->pub && idx != mod->pub->idx)
509 /* Remove model publication if the publication key is unbound */
510 remove_pub(node, mod);
513 static void model_bind_idx(struct mesh_node *node, struct mesh_model *mod,
517 mod->bindings = l_queue_new();
519 l_queue_push_tail(mod->bindings, L_UINT_TO_PTR(idx));
521 l_debug("Add %4.4x to model %8.8x", idx, mod->id);
525 config_update_model_bindings(node, mod);
526 else if (mod->cbs->bind)
528 mod->cbs->bind(idx, ACTION_ADD);
531 static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,
532 uint16_t app_idx, bool unbind)
535 struct mesh_model *mod;
538 mod = find_model(node, addr, id, &status);
540 l_debug("Model not found");
544 id = (id >= VENDOR_ID_MASK) ? (id & 0xffff) : id;
546 if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
547 return MESH_STATUS_INVALID_MODEL;
549 if (!appkey_have_key(node_get_net(node), app_idx))
550 return MESH_STATUS_INVALID_APPKEY;
552 is_present = has_binding(mod->bindings, app_idx);
554 if (!is_present && unbind)
555 return MESH_STATUS_SUCCESS;
557 if (is_present && !unbind)
558 return MESH_STATUS_SUCCESS;
561 model_unbind_idx(node, mod, app_idx);
563 if (!storage_model_bind(node, addr, id, app_idx, true))
564 return MESH_STATUS_STORAGE_FAIL;
566 return MESH_STATUS_SUCCESS;
569 if (l_queue_length(mod->bindings) >= MAX_BINDINGS)
570 return MESH_STATUS_INSUFF_RESOURCES;
572 if (!storage_model_bind(node, addr, id, app_idx, false))
573 return MESH_STATUS_STORAGE_FAIL;
575 model_bind_idx(node, mod, app_idx);
577 return MESH_STATUS_SUCCESS;
581 static struct mesh_virtual *add_virtual(const uint8_t *v)
583 struct mesh_virtual *virt = l_queue_find(mesh_virtuals,
584 find_virt_by_label, v);
591 virt = l_new(struct mesh_virtual, 1);
593 if (!mesh_crypto_virtual_addr(v, &virt->addr)) {
598 memcpy(virt->label, v, 16);
600 virt->id = virt_id_next++;
601 l_queue_push_head(mesh_virtuals, virt);
606 static int set_pub(struct mesh_model *mod, const uint8_t *pub_addr,
607 uint16_t idx, bool cred_flag, uint8_t ttl,
608 uint8_t period, uint8_t retransmit, bool b_virt,
611 struct mesh_virtual *virt = NULL;
618 *dst = l_get_le16(pub_addr);
622 virt = add_virtual(pub_addr);
624 return MESH_STATUS_STORAGE_FAIL;
628 /* If the old publication address is virtual, remove it from lists */
629 if (mod->pub && mod->pub->addr >= VIRTUAL_BASE) {
630 struct mesh_virtual *old_virt;
632 old_virt = l_queue_find(mod->virtuals, find_virt_by_id,
633 L_UINT_TO_PTR(mod->pub->addr));
635 l_queue_remove(mod->virtuals, old_virt);
636 unref_virt(old_virt);
640 mod->pub = l_new(struct mesh_model_pub, 1);
643 l_queue_push_head(mod->virtuals, virt);
645 mod->pub->addr = virt->id;
647 grp = l_get_le16(pub_addr);
648 mod->pub->addr = grp;
654 mod->pub->credential = cred_flag;
657 mod->pub->period = period;
658 mod->pub->retransmit = retransmit;
660 return MESH_STATUS_SUCCESS;
663 static int add_sub(struct mesh_net *net, struct mesh_model *mod,
664 const uint8_t *group, bool b_virt, uint16_t *dst)
666 struct mesh_virtual *virt = NULL;
670 virt = add_virtual(group);
672 return MESH_STATUS_STORAGE_FAIL;
676 grp = l_get_le16(group);
683 mod->subs = l_queue_new();
685 /* Check if this group already exists */
686 if (l_queue_find(mod->subs, simple_match, L_UINT_TO_PTR(grp))) {
690 return MESH_STATUS_SUCCESS;
694 l_queue_push_head(mod->virtuals, virt);
696 l_queue_push_tail(mod->subs, L_UINT_TO_PTR(grp));
698 l_debug("Added %4.4x", grp);
700 mesh_net_dst_reg(net, grp);
702 return MESH_STATUS_SUCCESS;
705 static void send_dev_key_msg_rcvd(struct mesh_node *node, uint8_t ele_idx,
706 uint16_t src, uint16_t net_idx,
707 uint16_t size, const uint8_t *data)
709 struct l_dbus *dbus = dbus_get_bus();
710 struct l_dbus_message *msg;
711 struct l_dbus_message_builder *builder;
715 owner = node_get_owner(node);
716 path = node_get_element_path(node, ele_idx);
720 l_debug("Send \"DevKeyMessageReceived\"");
722 msg = l_dbus_message_new_method_call(dbus, owner, path,
723 MESH_ELEMENT_INTERFACE,
724 "DevKeyMessageReceived");
726 builder = l_dbus_message_builder_new(msg);
728 l_dbus_message_builder_append_basic(builder, 'q', &src);
729 l_dbus_message_builder_append_basic(builder, 'q', &net_idx);
730 dbus_append_byte_array(builder, data, size);
732 l_dbus_message_builder_finalize(builder);
733 l_dbus_message_builder_destroy(builder);
735 l_dbus_send(dbus, msg);
738 static void send_msg_rcvd(struct mesh_node *node, uint8_t ele_idx, bool is_sub,
739 uint16_t src, uint16_t key_idx,
740 uint16_t size, const uint8_t *data)
742 struct l_dbus *dbus = dbus_get_bus();
743 struct l_dbus_message *msg;
744 struct l_dbus_message_builder *builder;
748 owner = node_get_owner(node);
749 path = node_get_element_path(node, ele_idx);
753 l_debug("Send \"MessageReceived\"");
755 msg = l_dbus_message_new_method_call(dbus, owner, path,
756 MESH_ELEMENT_INTERFACE, "MessageReceived");
758 builder = l_dbus_message_builder_new(msg);
760 l_dbus_message_builder_append_basic(builder, 'q', &src);
761 l_dbus_message_builder_append_basic(builder, 'q', &key_idx);
762 l_dbus_message_builder_append_basic(builder, 'b', &is_sub);
764 dbus_append_byte_array(builder, data, size);
766 l_dbus_message_builder_finalize(builder);
767 l_dbus_message_builder_destroy(builder);
768 l_dbus_send(dbus, msg);
771 bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,
772 uint32_t seq, uint32_t iv_index, uint8_t ttl,
773 uint16_t src, uint16_t dst, uint8_t key_id,
774 const uint8_t *data, uint16_t size)
777 struct mod_forward forward = {
781 .size = size - (szmict ? 8 : 4),
785 struct mesh_net *net = node_get_net(node);
787 int decrypt_idx, i, ele_idx;
789 struct mesh_virtual *decrypt_virt = NULL;
791 bool is_subscription;
793 l_debug("iv_index %8.8x key_id = %2.2x", iv_index, key_id);
797 ele_idx = node_get_element_idx(node, dst);
799 if (dst < 0x8000 && ele_idx < 0)
800 /* Unicast and not addressed to us */
803 clear_text = l_malloc(size);
807 forward.data = clear_text;
810 * The packet needs to be decoded by the correct key which
811 * is hinted by key_id, but is not necessarily definitive
813 if (key_id == APP_ID_DEV || mesh_net_provisioner_mode_get(net))
814 decrypt_idx = dev_packet_decrypt(node, data, size, szmict, src,
815 dst, key_id, seq0, iv_index,
817 else if ((dst & 0xc000) == 0x8000)
818 decrypt_idx = virt_packet_decrypt(net, data, size, szmict, src,
820 iv_index, clear_text,
823 decrypt_idx = appkey_packet_decrypt(net, szmict, seq0,
825 NULL, 0, key_id, data,
828 if (decrypt_idx < 0) {
829 l_error("model.c - Failed to decrypt application payload");
834 /* print_packet("Clr Rx (pre-cache-check)", clear_text, size - 4); */
836 if (key_id != APP_ID_DEV) {
837 uint16_t crpl = node_get_crpl(node);
839 if (appkey_msg_in_replay_cache(net, (uint16_t) decrypt_idx, src,
840 crpl, seq, iv_index)) {
846 print_packet("Clr Rx", clear_text, size - (szmict ? 8 : 4));
848 forward.virt = decrypt_virt;
849 forward.idx = decrypt_idx;
850 num_ele = node_get_num_elements(node);
851 addr = node_get_primary(node);
853 if (!num_ele || IS_UNASSIGNED(addr))
856 is_subscription = !(IS_UNICAST(dst));
858 for (i = 0; i < num_ele; i++) {
859 struct l_queue *models;
861 if (!is_subscription && ele_idx != i)
864 forward.unicast = addr + i;
865 forward.has_dst = false;
867 models = node_get_element_models(node, i, NULL);
869 /* Internal models */
870 l_queue_foreach(models, forward_model, &forward);
873 * Cycle through external models if the message has not been
874 * handled by internal models
876 if (forward.has_dst && !forward.done) {
877 if ((decrypt_idx & APP_IDX_MASK) == decrypt_idx)
878 send_msg_rcvd(node, i, is_subscription, src,
879 forward.idx, forward.size,
881 else if (decrypt_idx == APP_IDX_DEV_REMOTE ||
882 (decrypt_idx == APP_IDX_DEV_LOCAL &&
883 mesh_net_is_local_address(net, src)))
884 send_dev_key_msg_rcvd(node, i, src, 0,
885 forward.size, forward.data);
889 * Either the message has been processed internally or
890 * has been passed on to an external model.
892 result = forward.has_dst | forward.done;
894 /* If the message was to unicast address, we are done */
895 if (!is_subscription && ele_idx == i)
904 int mesh_model_publish(struct mesh_node *node, uint32_t mod_id,
905 uint16_t src, uint8_t ttl,
906 const void *msg, uint16_t msg_len)
908 struct mesh_net *net = node_get_net(node);
909 struct mesh_model *mod;
911 uint8_t *label = NULL;
918 /* print_packet("Mod Tx", msg, msg_len); */
920 if (!net || msg_len > 380)
921 return MESH_ERROR_INVALID_ARGS;
923 /* If SRC is 0, use the Primary Element */
925 src = mesh_net_get_address(net);
927 mod = find_model(node, src, mod_id, &status);
929 l_debug("model %x not found", mod_id);
930 return MESH_ERROR_NOT_FOUND;
934 l_debug("publication doesn't exist (model %x)", mod_id);
935 return MESH_ERROR_DOES_NOT_EXIST;
938 gettimeofday(&tx_start, NULL);
940 target = mod->pub->addr;
942 if (IS_UNASSIGNED(target))
943 return MESH_ERROR_DOES_NOT_EXIST;
945 if (target >= VIRTUAL_BASE) {
946 struct mesh_virtual *virt;
948 virt = l_queue_find(mesh_virtuals, find_virt_by_id,
949 L_UINT_TO_PTR(target));
951 return MESH_ERROR_NOT_FOUND;
959 l_debug("publish dst=%x", dst);
961 key = appkey_get_key(net, mod->pub->idx, &key_id);
963 l_debug("no app key for (%x)", mod->pub->idx);
967 l_debug("(%x) %p", mod->pub->idx, key);
968 l_debug("key_id %x", key_id);
970 result = msg_send(node, mod->pub->credential != 0, src,
971 dst, key_id, key, label, ttl, msg, msg_len);
973 return result ? MESH_ERROR_NONE : MESH_ERROR_FAILED;
977 bool mesh_model_send(struct mesh_node *node, uint16_t src, uint16_t target,
978 uint16_t app_idx, uint8_t ttl,
979 const void *msg, uint16_t msg_len)
985 /* print_packet("Mod Tx", msg, msg_len); */
987 /* If SRC is 0, use the Primary Element */
989 src = node_get_primary(node);
991 gettimeofday(&tx_start, NULL);
993 if (IS_UNASSIGNED(target))
996 if (app_idx == APP_IDX_DEV_LOCAL) {
997 key = node_get_device_key(node);
1001 key_id = APP_ID_DEV;
1002 } else if (app_idx == APP_IDX_DEV_REMOTE) {
1003 if (!keyring_get_remote_dev_key(node, target, dev_key))
1007 key_id = APP_ID_DEV;
1009 key = appkey_get_key(node_get_net(node), app_idx, &key_id);
1011 l_debug("no app key for (%x)", app_idx);
1015 l_debug("(%x) %p", app_idx, key);
1016 l_debug("key_id %x", key_id);
1019 return msg_send(node, false, src, target, key_id, key, NULL, ttl,
1023 int mesh_model_pub_set(struct mesh_node *node, uint16_t addr, uint32_t id,
1024 const uint8_t *pub_addr, uint16_t idx, bool cred_flag,
1025 uint8_t ttl, uint8_t period, uint8_t retransmit,
1026 bool b_virt, uint16_t *dst)
1028 struct mesh_model *mod;
1031 mod = find_model(node, addr, id, &status);
1035 if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
1036 return MESH_STATUS_INVALID_PUB_PARAM;
1038 if (!appkey_have_key(node_get_net(node), idx))
1039 return MESH_STATUS_INVALID_APPKEY;
1042 * If the publication address is set to unassigned address value,
1043 * remove the publication
1045 if (!b_virt && IS_UNASSIGNED(l_get_le16(pub_addr))) {
1046 remove_pub(node, mod);
1047 return MESH_STATUS_SUCCESS;
1050 status = set_pub(mod, pub_addr, idx, cred_flag, ttl, period, retransmit,
1053 if (status != MESH_STATUS_SUCCESS)
1057 /* External model */
1058 config_update_model_pub_period(node, mod->ele_idx, id,
1059 pub_period_to_ms(period));
1061 /* Internal model, call registered callbacks */
1062 mod->cbs->pub(mod->pub);
1064 return MESH_STATUS_SUCCESS;
1067 struct mesh_model_pub *mesh_model_pub_get(struct mesh_node *node, uint16_t addr,
1068 uint32_t mod_id, int *status)
1070 struct mesh_model *mod;
1072 mod = find_model(node, addr, mod_id, status);
1079 void mesh_model_free(void *data)
1081 struct mesh_model *mod = data;
1083 l_queue_destroy(mod->bindings, NULL);
1084 l_queue_destroy(mod->subs, NULL);
1085 l_queue_destroy(mod->virtuals, unref_virt);
1090 static struct mesh_model *model_new(uint8_t ele_idx, uint32_t id)
1092 struct mesh_model *mod = l_new(struct mesh_model, 1);
1095 mod->ele_idx = ele_idx;
1096 mod->virtuals = l_queue_new();
1100 struct mesh_model *mesh_model_new(uint8_t ele_idx, uint16_t id)
1102 return model_new(ele_idx, id | VENDOR_ID_MASK);
1105 struct mesh_model *mesh_model_vendor_new(uint8_t ele_idx, uint16_t vendor_id,
1108 uint32_t id = mod_id | (vendor_id << 16);
1110 return model_new(ele_idx, id);
1113 /* Internal models only */
1114 static void restore_model_state(struct mesh_model *mod)
1116 const struct mesh_model_ops *cbs;
1117 const struct l_queue_entry *b;
1123 if (!l_queue_isempty(mod->bindings) && cbs->bind) {
1124 for (b = l_queue_get_entries(mod->bindings); b; b = b->next) {
1125 if (cbs->bind(L_PTR_TO_UINT(b->data), ACTION_ADD) !=
1126 MESH_STATUS_SUCCESS)
1131 if (mod->pub && cbs->pub)
1136 uint32_t mesh_model_get_model_id(const struct mesh_model *model)
1141 /* This registers an internal model, i.e. implemented within meshd */
1142 bool mesh_model_register(struct mesh_node *node, uint8_t ele_idx,
1144 const struct mesh_model_ops *cbs,
1147 struct mesh_model *mod;
1150 /* Internal models are always SIG models */
1151 mod_id = VENDOR_ID_MASK | mod_id;
1153 mod = get_model(node, ele_idx, mod_id, &status);
1158 mod->user_data = user_data;
1160 restore_model_state(mod);
1165 void mesh_model_app_key_delete(struct mesh_node *node, struct l_queue *models,
1168 const struct l_queue_entry *entry = l_queue_get_entries(models);
1170 for (; entry; entry = entry->next) {
1171 struct mesh_model *model = entry->data;
1173 model_unbind_idx(node, model, app_idx);
1177 int mesh_model_binding_del(struct mesh_node *node, uint16_t addr, uint32_t id,
1180 l_debug("0x%x, 0x%x, %d", addr, id, app_idx);
1181 return update_binding(node, addr, id, app_idx, true);
1184 int mesh_model_binding_add(struct mesh_node *node, uint16_t addr, uint32_t id,
1187 l_debug("0x%x, 0x%x, %d", addr, id, app_idx);
1188 return update_binding(node, addr, id, app_idx, false);
1191 int mesh_model_get_bindings(struct mesh_node *node, uint16_t addr, uint32_t id,
1192 uint8_t *buf, uint16_t buf_size, uint16_t *size)
1195 struct mesh_model *mod;
1196 const struct l_queue_entry *entry;
1201 mod = find_model(node, addr, id, &status);
1208 entry = l_queue_get_entries(mod->bindings);
1213 for (; entry; entry = entry->next) {
1214 uint16_t app_idx = (uint16_t) (L_PTR_TO_UINT(entry->data));
1220 idx_pair += app_idx;
1222 /* Unlikely, but check for overflow*/
1223 if ((n + 3) > buf_size) {
1224 l_warn("Binding list too large");
1228 l_put_le32(idx_pair, buf);
1236 /* Process the last app key if present */
1237 if (i & 0x1 && ((n + 2) <= buf_size)) {
1238 l_put_le16(idx_pair, buf);
1244 return MESH_STATUS_SUCCESS;
1247 int mesh_model_sub_get(struct mesh_node *node, uint16_t addr, uint32_t id,
1248 uint8_t *buf, uint16_t buf_size, uint16_t *size)
1252 struct mesh_model *mod;
1253 const struct l_queue_entry *entry;
1255 mod = find_model(node, addr, id, &status);
1259 entry = l_queue_get_entries(mod->subs);
1263 for (; entry; entry = entry->next) {
1264 if ((n + 2) > buf_size)
1265 return MESH_STATUS_UNSPECIFIED_ERROR;
1267 l_put_le16((uint16_t) L_PTR_TO_UINT(entry->data), buf);
1273 return MESH_STATUS_SUCCESS;
1276 int mesh_model_sub_add(struct mesh_node *node, uint16_t addr, uint32_t id,
1277 const uint8_t *group, bool b_virt, uint16_t *dst)
1280 struct mesh_model *mod;
1282 mod = find_model(node, addr, id, &status);
1286 return add_sub(node_get_net(node), mod, group, b_virt, dst);
1289 int mesh_model_sub_ovr(struct mesh_node *node, uint16_t addr, uint32_t id,
1290 const uint8_t *group, bool b_virt, uint16_t *dst)
1293 struct l_queue *virtuals, *subs;
1294 struct mesh_virtual *virt;
1295 struct mesh_model *mod;
1297 mod = find_model(node, addr, id, &status);
1302 virtuals = mod->virtuals;
1303 mod->subs = l_queue_new();
1304 mod->virtuals = l_queue_new();
1306 if (!mod->subs || !mod->virtuals)
1307 return MESH_STATUS_INSUFF_RESOURCES;
1310 * When overwriting the Subscription List,
1311 * make sure any virtual Publication address is preserved
1313 if (mod->pub && mod->pub->addr >= VIRTUAL_BASE) {
1314 virt = l_queue_find(virtuals, find_virt_by_id,
1315 L_UINT_TO_PTR(mod->pub->addr));
1318 l_queue_push_head(mod->virtuals, virt);
1322 status = mesh_model_sub_add(node, addr, id, group, b_virt, dst);
1324 if (status != MESH_STATUS_SUCCESS) {
1325 /* Adding new group failed, so revert to old lists */
1326 l_queue_destroy(mod->subs, NULL);
1328 l_queue_destroy(mod->virtuals, unref_virt);
1329 mod->virtuals = virtuals;
1331 const struct l_queue_entry *entry;
1332 struct mesh_net *net = node_get_net(node);
1334 entry = l_queue_get_entries(subs);
1336 for (; entry; entry = entry->next)
1337 mesh_net_dst_unreg(net,
1338 (uint16_t) L_PTR_TO_UINT(entry->data));
1340 /* Destroy old lists */
1341 l_queue_destroy(subs, NULL);
1342 l_queue_destroy(virtuals, unref_virt);
1348 int mesh_model_sub_del(struct mesh_node *node, uint16_t addr, uint32_t id,
1349 const uint8_t *group, bool b_virt, uint16_t *dst)
1353 struct mesh_model *mod;
1355 mod = find_model(node, addr, id, &status);
1360 struct mesh_virtual *virt;
1362 virt = l_queue_find(mod->virtuals, find_virt_by_label, group);
1364 l_queue_remove(mod->virtuals, virt);
1368 if (!mesh_crypto_virtual_addr(group, &grp))
1369 return MESH_STATUS_STORAGE_FAIL;
1372 grp = l_get_le16(group);
1377 if (l_queue_remove(mod->subs, L_UINT_TO_PTR(grp)))
1378 mesh_net_dst_unreg(node_get_net(node), grp);
1380 return MESH_STATUS_SUCCESS;
1383 int mesh_model_sub_del_all(struct mesh_node *node, uint16_t addr, uint32_t id)
1386 struct mesh_model *mod;
1387 const struct l_queue_entry *entry;
1388 struct mesh_net *net = node_get_net(node);
1390 mod = find_model(node, addr, id, &status);
1394 entry = l_queue_get_entries(mod->subs);
1396 for (; entry; entry = entry->next)
1397 mesh_net_dst_unreg(net, (uint16_t) L_PTR_TO_UINT(entry->data));
1399 l_queue_destroy(mod->subs, NULL);
1400 l_queue_destroy(mod->virtuals, unref_virt);
1401 mod->virtuals = l_queue_new();
1403 return MESH_STATUS_SUCCESS;
1406 struct mesh_model *mesh_model_setup(struct mesh_node *node, uint8_t ele_idx,
1409 struct mesh_db_model *db_mod = data;
1410 struct mesh_model *mod;
1411 struct mesh_net *net;
1412 struct mesh_db_pub *pub = db_mod->pub;
1415 if (db_mod->num_bindings > MAX_BINDINGS) {
1416 l_warn("Binding list too long %u (max %u)",
1417 db_mod->num_bindings, MAX_BINDINGS);
1421 mod = model_new(ele_idx, db_mod->vendor ? db_mod->id :
1422 db_mod->id | VENDOR_ID_MASK);
1424 /* Implicitly bind config server model to device key */
1425 if (db_mod->id == CONFIG_SRV_MODEL) {
1427 if (ele_idx != PRIMARY_ELE_IDX)
1430 l_queue_push_head(mod->bindings,
1431 L_UINT_TO_PTR(APP_IDX_DEV_LOCAL));
1435 if (db_mod->id == CONFIG_CLI_MODEL) {
1436 l_queue_push_head(mod->bindings,
1437 L_UINT_TO_PTR(APP_IDX_DEV_LOCAL));
1441 net = node_get_net(node);
1443 /* Add application key bindings if present */
1444 if (db_mod->bindings) {
1445 mod->bindings = l_queue_new();
1446 for (i = 0; i < db_mod->num_bindings; i++)
1447 model_bind_idx(node, mod, db_mod->bindings[i]);
1450 /* Add publication if present */
1451 if (pub && (pub->virt || !(IS_UNASSIGNED(pub->addr)))) {
1452 uint8_t mod_addr[2];
1454 uint8_t retransmit = (pub->count << 5) +
1455 (pub->interval / 50 - 1);
1457 /* Add publication */
1458 l_put_le16(pub->addr, &mod_addr);
1459 pub_addr = pub->virt ? pub->virt_addr : mod_addr;
1461 if (set_pub(mod, pub_addr, pub->idx, pub->credential, pub->ttl,
1462 pub->period, retransmit, pub->virt, NULL) !=
1463 MESH_STATUS_SUCCESS) {
1464 mesh_model_free(mod);
1469 /* Add subscriptions if present */
1473 for (i = 0; i < db_mod->num_subs; i++) {
1478 * To keep calculations for virtual label coherent,
1479 * convert to little endian.
1481 l_put_le16(db_mod->subs[i].src.addr, &group);
1482 src = db_mod->subs[i].virt ? db_mod->subs[i].src.virt_addr :
1485 if (add_sub(net, mod, src, db_mod->subs[i].virt, NULL) !=
1486 MESH_STATUS_SUCCESS) {
1487 mesh_model_free(mod);
1495 uint16_t mesh_model_opcode_set(uint32_t opcode, uint8_t *buf)
1497 if (opcode <= 0x7e) {
1502 if (opcode >= 0x8000 && opcode <= 0xbfff) {
1503 l_put_be16(opcode, buf);
1507 if (opcode >= 0xc00000 && opcode <= 0xffffff) {
1508 buf[0] = (opcode >> 16) & 0xff;
1509 l_put_be16(opcode, buf + 1);
1513 l_debug("Illegal Opcode %x", opcode);
1517 bool mesh_model_opcode_get(const uint8_t *buf, uint16_t size,
1518 uint32_t *opcode, uint16_t *n)
1520 if (!n || !opcode || size < 1)
1523 switch (buf[0] & 0xc0) {
1539 *opcode = l_get_be16(buf);
1547 *opcode = l_get_be16(buf + 1);
1548 *opcode |= buf[0] << 16;
1552 print_packet("Bad", buf, size);
1559 void model_build_config(void *model, void *msg_builder)
1561 struct l_dbus_message_builder *builder = msg_builder;
1562 struct mesh_model *mod = model;
1565 if (is_internal(mod->id))
1568 if (!l_queue_length(mod->subs) && !l_queue_length(mod->virtuals) &&
1569 !mod->pub && !l_queue_length(mod->bindings))
1572 l_dbus_message_builder_enter_struct(builder, "qa{sv}");
1575 id = mod->id & 0xffff;
1576 l_dbus_message_builder_append_basic(builder, 'q', &id);
1578 l_dbus_message_builder_enter_array(builder, "{sv}");
1580 /* For vendor models, add vendor id */
1581 if ((mod->id & VENDOR_ID_MASK) != VENDOR_ID_MASK) {
1582 uint16_t vendor = mod->id >> 16;
1583 dbus_append_dict_entry_basic(builder, "Vendor", "q", &vendor);
1586 /* Model bindings, if present */
1587 if (l_queue_length(mod->bindings))
1588 append_dict_uint16_array(builder, mod->bindings, "Bindings");
1590 /* Model periodic publication interval, if present */
1592 uint32_t period = pub_period_to_ms(mod->pub->period);
1593 dbus_append_dict_entry_basic(builder, "PublicationPeriod", "u",
1597 l_dbus_message_builder_leave_array(builder);
1598 l_dbus_message_builder_leave_struct(builder);
1601 void mesh_model_init(void)
1603 mesh_virtuals = l_queue_new();
1606 void mesh_model_cleanup(void)
1608 l_queue_destroy(mesh_virtuals, l_free);
1609 mesh_virtuals = NULL;