summaryrefslogtreecommitdiff
path: root/mesh/model.c
blob: 4ccafa17edd432c3e4bb0769a2025ed4d769c879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2018-2020  Intel Corporation. All rights reserved.
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/time.h>
#include <ell/ell.h>

#include "mesh/mesh-defs.h"

#include "mesh/mesh.h"
#include "mesh/crypto.h"
#include "mesh/node.h"
#include "mesh/mesh-config.h"
#include "mesh/net.h"
#include "mesh/appkey.h"
#include "mesh/cfgmod.h"
#include "mesh/prov.h"
#include "mesh/remprv.h"
#include "mesh/prv-beacon.h"
#include "mesh/error.h"
#include "mesh/dbus.h"
#include "mesh/util.h"
#include "mesh/model.h"
#include "mesh/keyring.h"

/* Divide and round to ceiling (up) to calculate segment count */
#define CEILDIV(val, div) (((val) + (div) - 1) / (div))

#define VIRTUAL_BASE			0x10000

struct mesh_model {
	const struct mesh_model_ops *cbs;
	void *user_data;
	struct l_queue *bindings;
	struct l_queue *subs;
	struct l_queue *virtuals;
	struct mesh_model_pub *pub;
	bool sub_enabled;
	bool pub_enabled;
	uint32_t id;
};

struct mesh_virtual {
	uint16_t ref_cnt;
	uint16_t addr; /* 16-bit virtual address, used in messages */
	uint8_t label[16]; /* 128 bit label UUID */
};

/* These struct is used to pass lots of params to l_queue_foreach */
struct mod_forward {
	struct mesh_virtual *virt;
	const uint8_t *data;
	uint16_t src;
	uint16_t dst;
	uint16_t unicast;
	uint16_t app_idx;
	uint16_t net_idx;
	uint16_t size;
	int8_t rssi;
	bool szmict;
	bool has_dst;
	bool done;
};

static struct l_queue *mesh_virtuals;

static bool is_internal(uint32_t id)
{
	if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
		return true;

	if (id == REM_PROV_SRV_MODEL || id == REM_PROV_CLI_MODEL)
		return true;

	if (id == PRV_BEACON_SRV_MODEL || id == PRV_BEACON_CLI_MODEL)
		return true;

	return false;
}

static void unref_virt(void *data)
{
	struct mesh_virtual *virt = data;

	if (virt->ref_cnt > 0)
		virt->ref_cnt--;

	if (virt->ref_cnt)
		return;

	l_queue_remove(mesh_virtuals, virt);
	l_free(virt);
}

static bool simple_match(const void *a, const void *b)
{
	return a == b;
}

static bool has_binding(struct l_queue *bindings, uint16_t idx)
{
	const struct l_queue_entry *entry;

	for (entry = l_queue_get_entries(bindings); entry; entry = entry->next)
		if (L_PTR_TO_INT(entry->data) == idx)
			return true;

	return false;
}

static bool find_virt_by_label(const void *a, const void *b)
{
	const struct mesh_virtual *virt = a;
	const uint8_t *label = b;

	return memcmp(virt->label, label, 16) == 0;
}

static bool match_model_id(const void *a, const void *b)
{
	const struct mesh_model *mod = a;
	uint32_t id = L_PTR_TO_UINT(b);

	return (mod->id == id);
}

static int compare_model_id(const void *a, const void *b, void *user_data)
{
	const struct mesh_model *mod_a = a;
	const struct mesh_model *mod_b = b;

	if (mod_a->id < mod_b->id)
		return -1;

	if (mod_a->id > mod_b->id)
		return 1;

	return 0;
}

static struct mesh_model *get_model(struct mesh_node *node, uint8_t ele_idx,
								uint32_t id)
{
	struct l_queue *mods;
	struct mesh_model *mod;

	mods = node_get_element_models(node, ele_idx);
	if (!mods)
		return NULL;

	mod = l_queue_find(mods, match_model_id, L_UINT_TO_PTR(id));

	if (!mod)
		l_debug("Model not found");

	return mod;
}

static uint32_t pub_period_to_ms(uint8_t pub_period)
{
	int step_res, num_steps;

	step_res = pub_period >> 6;
	num_steps = pub_period & 0x3f;

	switch (step_res) {
	default:
		return num_steps * 100;
	case 2:
		num_steps *= 10;
		/* Fall Through */
	case 1:
		return num_steps * 1000;
	case 3:
		return num_steps * 10 * 60 * 1000;
	}
}

static struct l_dbus_message *create_config_update_msg(struct mesh_node *node,
					uint8_t ele_idx, uint32_t id,
					struct l_dbus_message_builder **builder)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	const char *owner;
	const char *path;
	uint16_t model_id;

	owner = node_get_owner(node);
	path = node_get_element_path(node, ele_idx);
	if (!path || !owner)
		return NULL;

	l_debug("Send \"UpdateModelConfiguration\"");
	msg = l_dbus_message_new_method_call(dbus, owner, path,
						MESH_ELEMENT_INTERFACE,
						"UpdateModelConfiguration");

	*builder = l_dbus_message_builder_new(msg);

	model_id = (uint16_t) MODEL_ID(id);

	l_dbus_message_builder_append_basic(*builder, 'q', &model_id);

	l_dbus_message_builder_enter_array(*builder, "{sv}");

	if (IS_VENDOR(id)) {
		uint16_t vendor_id = (uint16_t) VENDOR_ID(id);

		dbus_append_dict_entry_basic(*builder, "Vendor", "q",
								&vendor_id);
	}

	return msg;
}

static void config_update_model_pub_period(struct mesh_node *node,
					uint8_t ele_idx, uint32_t model_id,
					uint32_t period)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	struct l_dbus_message_builder *builder;

	msg = create_config_update_msg(node, ele_idx, model_id, &builder);
	if (!msg)
		return;

	dbus_append_dict_entry_basic(builder, "PublicationPeriod", "u",
								&period);

	l_dbus_message_builder_leave_array(builder);
	l_dbus_message_builder_finalize(builder);
	l_dbus_message_builder_destroy(builder);
	l_dbus_send(dbus, msg);
}

static void append_dict_uint16_array(struct l_dbus_message_builder *builder,
					struct l_queue *q, const char *key)
{
	const struct l_queue_entry *entry;

	l_dbus_message_builder_enter_dict(builder, "sv");
	l_dbus_message_builder_append_basic(builder, 's', key);
	l_dbus_message_builder_enter_variant(builder, "aq");
	l_dbus_message_builder_enter_array(builder, "q");

	for (entry = l_queue_get_entries(q); entry; entry = entry->next) {
		uint16_t value = (uint16_t) L_PTR_TO_UINT(entry->data);

		l_dbus_message_builder_append_basic(builder,'q', &value);
	}

	l_dbus_message_builder_leave_array(builder);
	l_dbus_message_builder_leave_variant(builder);
	l_dbus_message_builder_leave_dict(builder);
}

static void cfg_update_mod_bindings(struct mesh_node *node, uint16_t ele_idx,
							struct mesh_model *mod)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	struct l_dbus_message_builder *builder;

	msg = create_config_update_msg(node, ele_idx, mod->id, &builder);
	if (!msg)
		return;

	append_dict_uint16_array(builder, mod->bindings, "Bindings");

	l_dbus_message_builder_leave_array(builder);
	l_dbus_message_builder_finalize(builder);
	l_dbus_message_builder_destroy(builder);
	l_dbus_send(dbus, msg);
}

static void append_dict_subs_array(struct l_dbus_message_builder *builder,
						struct l_queue *subs,
						struct l_queue *virts,
						const char *key)
{
	const struct l_queue_entry *entry;

	l_dbus_message_builder_enter_dict(builder, "sv");
	l_dbus_message_builder_append_basic(builder, 's', key);
	l_dbus_message_builder_enter_variant(builder, "av");
	l_dbus_message_builder_enter_array(builder, "v");

	if (l_queue_isempty(subs))
		goto virts;

	for (entry = l_queue_get_entries(subs); entry; entry = entry->next) {
		uint16_t grp = L_PTR_TO_UINT(entry->data);

		l_dbus_message_builder_enter_variant(builder, "q");
		l_dbus_message_builder_append_basic(builder, 'q', &grp);
		l_dbus_message_builder_leave_variant(builder);
	}

virts:
	if (l_queue_isempty(virts))
		goto done;

	for (entry = l_queue_get_entries(virts); entry; entry = entry->next) {
		const struct mesh_virtual *virt = entry->data;

		l_dbus_message_builder_enter_variant(builder, "ay");
		dbus_append_byte_array(builder, virt->label,
							sizeof(virt->label));
		l_dbus_message_builder_leave_variant(builder);

	}

done:
	l_dbus_message_builder_leave_array(builder);
	l_dbus_message_builder_leave_variant(builder);
	l_dbus_message_builder_leave_dict(builder);
}

static void cfg_update_model_subs(struct mesh_node *node, uint16_t ele_idx,
							struct mesh_model *mod)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	struct l_dbus_message_builder *builder;

	msg = create_config_update_msg(node, ele_idx, mod->id, &builder);
	if (!msg)
		return;

	append_dict_subs_array(builder, mod->subs, mod->virtuals,
							"Subscriptions");

	l_dbus_message_builder_leave_array(builder);
	l_dbus_message_builder_finalize(builder);
	l_dbus_message_builder_destroy(builder);
	l_dbus_send(dbus, msg);
}

static void forward_model(void *a, void *b)
{
	struct mesh_model *mod = a;
	struct mod_forward *fwd = b;
	struct mesh_virtual *virt;
	uint16_t dst;
	bool result;

	if (fwd->app_idx != APP_IDX_DEV_LOCAL &&
				fwd->app_idx != APP_IDX_DEV_REMOTE &&
				!has_binding(mod->bindings, fwd->app_idx))
		return;

	dst = fwd->dst;

	if (dst == fwd->unicast || IS_FIXED_GROUP_ADDRESS(dst)) {
		fwd->has_dst = true;
	} else if (fwd->virt) {
		virt = l_queue_find(mod->virtuals, simple_match, fwd->virt);
		if (virt) {
			fwd->has_dst = true;
			dst = virt->addr;
		}
	} else {
		if (l_queue_find(mod->subs, simple_match, L_UINT_TO_PTR(dst)))
			fwd->has_dst = true;
	}

	if (!fwd->has_dst)
		return;

	/* Return, if this is not a internal model */
	if (!mod->cbs)
		return;

	result = false;

	if (mod->cbs->recv)
		result = mod->cbs->recv(fwd->src, dst, fwd->app_idx,
				fwd->net_idx,
				fwd->data, fwd->size, mod->user_data);

	if (dst == fwd->unicast && result)
		fwd->done = true;
}

static int app_packet_decrypt(struct mesh_net *net, const uint8_t *data,
				uint16_t size, bool szmict, uint16_t src,
				uint16_t dst, uint8_t *virt, uint16_t virt_size,
				uint8_t key_aid, uint32_t seq,
				uint32_t iv_idx, uint8_t *out)
{
	struct l_queue *app_keys = mesh_net_get_app_keys(net);
	const struct l_queue_entry *entry;

	if (!app_keys)
		return -1;

	for (entry = l_queue_get_entries(app_keys); entry;
							entry = entry->next) {
		const uint8_t *old_key = NULL, *new_key = NULL;
		uint8_t old_key_aid, new_key_aid;
		int app_idx;
		bool decrypted;

		app_idx = appkey_get_key_idx(entry->data,
							&old_key, &old_key_aid,
							&new_key, &new_key_aid);

		if (app_idx < 0)
			continue;

		if (old_key && old_key_aid == key_aid) {
			decrypted = mesh_crypto_payload_decrypt(virt, virt_size,
					data, size, szmict, src, dst, key_aid,
						seq, iv_idx, out, old_key);

			if (decrypted) {
				print_packet("Used App Key", old_key, 16);
				return app_idx;
			}

			print_packet("Failed App Key", old_key, 16);
		}

		if (new_key && new_key_aid == key_aid) {
			decrypted = mesh_crypto_payload_decrypt(virt, virt_size,
					data, size, szmict, src, dst, key_aid,
						seq, iv_idx, out, new_key);

			if (decrypted) {
				print_packet("Used App Key", new_key, 16);
				return app_idx;
			}

			print_packet("Failed App Key", new_key, 16);
		}
	}

	return -1;
}

static int dev_packet_decrypt(struct mesh_node *node, const uint8_t *data,
				uint16_t size, bool szmict, uint16_t src,
				uint16_t dst, uint8_t key_aid, uint32_t seq,
				uint32_t iv_idx, uint8_t *out)
{
	uint8_t dev_key[16];
	const uint8_t *key;

	key = node_get_device_key(node);
	if (!key)
		return -1;

	if (mesh_crypto_payload_decrypt(NULL, 0, data, size, szmict, src,
					dst, key_aid, seq, iv_idx, out, key))
		return APP_IDX_DEV_LOCAL;

	key = dev_key;

	if (keyring_get_remote_dev_key(node, src, dev_key)) {
		if (mesh_crypto_payload_decrypt(NULL, 0, data, size, szmict,
				src, dst, key_aid, seq, iv_idx, out, key))
			return APP_IDX_DEV_REMOTE;
	}

	/* See if there is a local Device Key Candidate as last resort */
	if (!node_get_device_key_candidate(node, dev_key))
		return -1;

	if (mesh_crypto_payload_decrypt(NULL, 0, data, size, szmict,
				src, dst, key_aid, seq, iv_idx, out, key)) {

		/* If candidate dev_key worked, it is considered finalized */
		node_finalize_candidate(node);
		return APP_IDX_DEV_LOCAL;
	}

	return -1;
}

static int virt_packet_decrypt(struct mesh_net *net, const uint8_t *data,
				uint16_t size, bool szmict, uint16_t src,
				uint16_t dst, uint8_t key_aid, uint32_t seq,
				uint32_t iv_idx, uint8_t *out,
				struct mesh_virtual **decrypt_virt)
{
	const struct l_queue_entry *v;

	for (v = l_queue_get_entries(mesh_virtuals); v; v = v->next) {
		struct mesh_virtual *virt = v->data;
		int decrypt_idx;

		if (virt->addr != dst)
			continue;

		decrypt_idx = app_packet_decrypt(net, data, size, szmict, src,
							dst, virt->label, 16,
							key_aid, seq, iv_idx,
							out);

		if (decrypt_idx >= 0) {
			*decrypt_virt = virt;
			return decrypt_idx;
		}
	}

	return -1;
}

static bool msg_send(struct mesh_node *node, bool cred, uint16_t src,
			uint16_t dst, uint16_t app_idx, uint16_t net_idx,
			uint8_t *label, uint8_t ttl, uint8_t cnt,
			uint16_t interval, bool segmented, const void *msg,
			uint16_t msg_len)
{
	uint8_t dev_key[16];
	uint32_t iv_index, seq_num;
	const uint8_t *key;
	uint8_t *out;
	uint8_t key_aid = APP_AID_DEV;
	bool szmic = false;
	bool ret = false;
	uint16_t out_len = msg_len + sizeof(uint32_t);
	struct mesh_net *net = node_get_net(node);

	/* Use large MIC if it doesn't affect segmentation */
	if (msg_len > 11 && msg_len <= 376) {
		if (CEILDIV(out_len, 12) == CEILDIV(out_len + 4, 12)) {
			szmic = true;
			out_len = msg_len + sizeof(uint64_t);
		}
	}

	if (app_idx == APP_IDX_DEV_LOCAL) {
		key = node_get_device_key(node);
		if (!key)
			return false;
	} else if (app_idx == APP_IDX_DEV_REMOTE) {
		if (!keyring_get_remote_dev_key(node, dst, dev_key))
			return false;

		key = dev_key;
	} else {
		key = appkey_get_key(node_get_net(node), app_idx, &key_aid);
		if (!key) {
			l_debug("no app key for (%x)", app_idx);
			return false;
		}
	}

	out = l_malloc(out_len);

	iv_index = mesh_net_get_iv_index(net);

	seq_num = mesh_net_next_seq_num(net);

	if (!mesh_crypto_payload_encrypt(label, msg, out, msg_len, src, dst,
				key_aid, seq_num, iv_index, szmic, key)) {
		l_error("Failed to Encrypt Payload");
		goto done;
	}

	ret =  mesh_net_app_send(net, cred, src, dst, key_aid, net_idx, ttl,
					cnt, interval, seq_num, iv_index,
					segmented, szmic, out, out_len);
done:
	l_free(out);
	return ret;
}

static void remove_pub(struct mesh_node *node, uint16_t ele_idx,
							struct mesh_model *mod)
{
	if (mod->pub) {
		if (mod->pub->virt)
			unref_virt(mod->pub->virt);

		l_free(mod->pub);
		mod->pub = NULL;
	}

	if (!mod->cbs)
		/* External models */
		config_update_model_pub_period(node, ele_idx, mod->id, 0);
	else if (mod->cbs && mod->cbs->pub)
		/* Internal models */
		mod->cbs->pub(NULL);
}

static void model_unbind_idx(struct mesh_node *node, uint16_t ele_idx,
					struct mesh_model *mod, uint16_t idx)
{
	l_queue_remove(mod->bindings, L_UINT_TO_PTR(idx));

	if (!mod->cbs)
		/* External model */
		cfg_update_mod_bindings(node, ele_idx, mod);
	else if (mod->cbs->bind)
		/* Internal model */
		mod->cbs->bind(idx, ACTION_DELETE);

	/* Remove model publication if the publication key is unbound */
	if (mod->pub && idx == mod->pub->idx)
		remove_pub(node, ele_idx, mod);
}

static void model_bind_idx(struct mesh_node *node, uint16_t ele_idx,
					struct mesh_model *mod, uint16_t idx)
{
	if (!mod->bindings)
		mod->bindings = l_queue_new();

	l_queue_push_tail(mod->bindings, L_UINT_TO_PTR(idx));

	l_debug("Bind key %4.4x to model %8.8x", idx, mod->id);

	if (!mod->cbs)
		/* External model */
		cfg_update_mod_bindings(node, ele_idx, mod);
	else if (mod->cbs->bind)
		/* Internal model */
		mod->cbs->bind(idx, ACTION_ADD);
}

static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,
						uint16_t app_idx, bool unbind)
{
	struct mesh_model *mod;
	bool vendor;
	int ele_idx = node_get_element_idx(node, addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod) {
		return MESH_STATUS_INVALID_MODEL;
	}

	if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
		return MESH_STATUS_INVALID_MODEL;

	if (id == PRV_BEACON_SRV_MODEL || id == PRV_BEACON_CLI_MODEL)
		return MESH_STATUS_INVALID_MODEL;

	if (!appkey_have_key(node_get_net(node), app_idx))
		return MESH_STATUS_INVALID_APPKEY;

	/*
	 * If deleting a binding that is not present, return success.
	 * If adding a binding that already exists, return success.
	 */
	if (unbind ^ has_binding(mod->bindings, app_idx))
		return MESH_STATUS_SUCCESS;

	vendor = IS_VENDOR(id);
	id = vendor ? id : MODEL_ID(id);

	if (unbind) {
		model_unbind_idx(node, ele_idx, mod, app_idx);

		if (!mesh_config_model_binding_del(node_config_get(node),
							addr, id, vendor,
								app_idx))
			return MESH_STATUS_STORAGE_FAIL;

		l_debug("Unbind key %4.4x to model %8.8x", app_idx, mod->id);
		return MESH_STATUS_SUCCESS;
	}

	if (l_queue_length(mod->bindings) >= MAX_MODEL_BINDINGS)
		return MESH_STATUS_INSUFF_RESOURCES;

	if (!mesh_config_model_binding_add(node_config_get(node), addr,
							id, vendor, app_idx))
		return MESH_STATUS_STORAGE_FAIL;

	model_bind_idx(node, ele_idx, mod, app_idx);

	return MESH_STATUS_SUCCESS;
}

static struct mesh_virtual *add_virtual(const uint8_t *v)
{
	struct mesh_virtual *virt = l_queue_find(mesh_virtuals,
						find_virt_by_label, v);

	if (virt) {
		virt->ref_cnt++;
		return virt;
	}

	virt = l_new(struct mesh_virtual, 1);

	if (!mesh_crypto_virtual_addr(v, &virt->addr)) {
		l_free(virt);
		return NULL;
	}

	memcpy(virt->label, v, 16);
	virt->ref_cnt = 1;
	l_queue_push_head(mesh_virtuals, virt);

	return virt;
}

static int set_pub(struct mesh_model *mod, uint16_t pub_addr,
			uint16_t idx, bool cred_flag, uint8_t ttl,
			uint8_t period, uint8_t cnt, uint16_t interval)
{
	if (!mod->pub)
		mod->pub = l_new(struct mesh_model_pub, 1);

	mod->pub->addr = pub_addr;
	mod->pub->credential = cred_flag;
	mod->pub->idx = idx;
	mod->pub->ttl = ttl;
	mod->pub->period = period;
	mod->pub->rtx.cnt = cnt;
	mod->pub->rtx.interval = interval;

	return MESH_STATUS_SUCCESS;
}

static int set_virt_pub(struct mesh_model *mod, const uint8_t *label,
			uint16_t idx, bool cred_flag, uint8_t ttl,
			uint8_t period, uint8_t cnt, uint16_t interval)
{
	struct mesh_virtual *virt = NULL;

	virt = add_virtual(label);
	if (!virt)
		return MESH_STATUS_STORAGE_FAIL;

	if (!mod->pub)
		mod->pub = l_new(struct mesh_model_pub, 1);

	mod->pub->virt = virt;
	return set_pub(mod, virt->addr, idx, cred_flag, ttl, period, cnt,
								interval);
}

static int add_virt_sub(struct mesh_net *net, struct mesh_model *mod,
				const uint8_t *label, uint16_t *addr)
{
	struct mesh_virtual *virt = l_queue_find(mod->virtuals,
						find_virt_by_label, label);

	if (!virt) {
		virt = add_virtual(label);
		if (!virt)
			return MESH_STATUS_INSUFF_RESOURCES;

		l_queue_push_head(mod->virtuals, virt);
		mesh_net_dst_reg(net, virt->addr);
		l_debug("Added virtual sub addr %4.4x", virt->addr);
	}

	if (addr)
		*addr = virt->addr;

	return MESH_STATUS_SUCCESS;
}

static int add_sub(struct mesh_net *net, struct mesh_model *mod,
							uint16_t addr)
{
	if (!mod->subs)
		mod->subs = l_queue_new();

	if (l_queue_find(mod->subs, simple_match, L_UINT_TO_PTR(addr)))
		return MESH_STATUS_SUCCESS;

	if ((l_queue_length(mod->subs) + l_queue_length(mod->virtuals)) >=
						MAX_MODEL_SUBS)
		return MESH_STATUS_INSUFF_RESOURCES;

	l_queue_push_tail(mod->subs, L_UINT_TO_PTR(addr));
	mesh_net_dst_reg(net, addr);
	l_debug("Added group subscription %4.4x", addr);

	return MESH_STATUS_SUCCESS;
}

static void send_dev_key_msg_rcvd(struct mesh_node *node, uint8_t ele_idx,
					uint16_t src, uint16_t app_idx,
					uint16_t net_idx, uint16_t size,
					const uint8_t *data)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	struct l_dbus_message_builder *builder;
	const char *owner;
	const char *path;
	bool remote = (app_idx != APP_IDX_DEV_LOCAL);

	owner = node_get_owner(node);
	path = node_get_element_path(node, ele_idx);
	if (!path || !owner)
		return;

	l_debug("Send \"DevKeyMessageReceived\"");

	msg = l_dbus_message_new_method_call(dbus, owner, path,
						MESH_ELEMENT_INTERFACE,
						"DevKeyMessageReceived");

	builder = l_dbus_message_builder_new(msg);

	l_dbus_message_builder_append_basic(builder, 'q', &src);
	l_dbus_message_builder_append_basic(builder, 'b', &remote);
	l_dbus_message_builder_append_basic(builder, 'q', &net_idx);
	dbus_append_byte_array(builder, data, size);

	l_dbus_message_builder_finalize(builder);
	l_dbus_message_builder_destroy(builder);

	l_dbus_send(dbus, msg);
}

static void send_msg_rcvd(struct mesh_node *node, uint8_t ele_idx,
					uint16_t src, uint16_t dst,
					const struct mesh_virtual *virt,
					uint16_t app_idx,
					uint16_t size, const uint8_t *data)
{
	struct l_dbus *dbus = dbus_get_bus();
	struct l_dbus_message *msg;
	struct l_dbus_message_builder *builder;
	const char *owner;
	const char *path;

	owner = node_get_owner(node);
	path = node_get_element_path(node, ele_idx);
	if (!path || !owner)
		return;

	l_debug("Send \"MessageReceived\"");

	msg = l_dbus_message_new_method_call(dbus, owner, path,
				MESH_ELEMENT_INTERFACE, "MessageReceived");

	builder = l_dbus_message_builder_new(msg);

	l_dbus_message_builder_append_basic(builder, 'q', &src);
	l_dbus_message_builder_append_basic(builder, 'q', &app_idx);

	if (virt) {
		l_dbus_message_builder_enter_variant(builder, "ay");
		dbus_append_byte_array(builder, virt->label,
							sizeof(virt->label));
		l_dbus_message_builder_leave_variant(builder);
	} else {
		l_dbus_message_builder_enter_variant(builder, "q");
		l_dbus_message_builder_append_basic(builder, 'q', &dst);
		l_dbus_message_builder_leave_variant(builder);
	}

	dbus_append_byte_array(builder, data, size);

	l_dbus_message_builder_finalize(builder);
	l_dbus_message_builder_destroy(builder);
	l_dbus_send(dbus, msg);
}

bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,
			uint32_t iv_index, uint16_t net_idx, uint16_t src,
			uint16_t dst, uint8_t key_aid, const uint8_t *data,
								uint16_t size)
{
	uint8_t *clear_text;
	struct mod_forward forward = {
		.src = src,
		.dst = dst,
		.data = NULL,
		.size = size - (szmict ? 8 : 4),
		.virt = NULL,
	};
	struct mesh_net *net = node_get_net(node);
	uint8_t num_ele;
	int decrypt_idx, i, ele_idx;
	uint16_t addr;
	struct mesh_virtual *decrypt_virt = NULL;
	bool result = false;
	bool is_subscription;

	l_debug("iv_index %8.8x key_aid = %2.2x", iv_index, key_aid);
	if (!dst)
		return false;

	ele_idx = node_get_element_idx(node, dst);

	if (dst < 0x8000 && ele_idx < 0)
		/* Unicast and not addressed to us */
		return false;

	clear_text = l_malloc(size);
	forward.data = clear_text;

	/*
	 * The packet needs to be decoded by the correct key which
	 * is hinted by key_aid, but is not necessarily definitive
	 */
	if (key_aid == APP_AID_DEV)
		decrypt_idx = dev_packet_decrypt(node, data, size, szmict, src,
						dst, key_aid, seq0, iv_index,
						clear_text);
	else if ((dst & 0xc000) == 0x8000)
		decrypt_idx = virt_packet_decrypt(net, data, size, szmict, src,
							dst, key_aid, seq0,
							iv_index, clear_text,
							&decrypt_virt);
	else
		decrypt_idx = app_packet_decrypt(net, data, size, szmict, src,
						dst, NULL, 0,
						key_aid, seq0, iv_index,
						clear_text);

	if (decrypt_idx < 0) {
		l_error("model.c - Failed to decrypt application payload");
		result = false;
		goto done;
	}

	print_packet("Clr Rx", clear_text, size - (szmict ? 8 : 4));

	forward.virt = decrypt_virt;
	forward.app_idx = decrypt_idx;
	forward.net_idx = net_idx;
	num_ele = node_get_num_elements(node);
	addr = node_get_primary(node);

	if (!num_ele || IS_UNASSIGNED(addr))
		goto done;

	is_subscription = !(IS_UNICAST(dst));

	for (i = 0; i < num_ele; i++) {
		struct l_queue *models;

		if (!is_subscription && ele_idx != i)
			continue;

		forward.unicast = addr + i;
		forward.has_dst = false;

		models = node_get_element_models(node, i);

		/* Internal models */
		l_queue_foreach(models, forward_model, &forward);

		/*
		 * Cycle through external models if the message has not been
		 * handled by internal models
		 */
		if (forward.has_dst && !forward.done) {
			if ((decrypt_idx & APP_IDX_MASK) == decrypt_idx)
				send_msg_rcvd(node, i, src, dst, decrypt_virt,
						forward.app_idx, forward.size,
						forward.data);
			else if (decrypt_idx == APP_IDX_DEV_REMOTE ||
				 decrypt_idx == APP_IDX_DEV_LOCAL)
				send_dev_key_msg_rcvd(node, i, src, decrypt_idx,
							net_idx, forward.size,
								forward.data);
		}

		/*
		 * Either the message has been processed internally or
		 * has been passed on to an external model.
		 */
		result |= forward.has_dst | forward.done;

		/* If the message was to unicast address, we are done */
		if (!is_subscription && ele_idx == i)
			break;

		/*
		 * For the fixed group addresses, i.e., all-proxies,
		 * all-friends, all-relays, all-nodes, the message is delivered
		 * to a primary element only.
		 */
		if (IS_FIXED_GROUP_ADDRESS(dst))
			break;
	}

done:
	l_free(clear_text);

	return result;
}

int mesh_model_publish(struct mesh_node *node, uint32_t id, uint16_t src,
			bool segmented, uint16_t msg_len, const void *msg)
{
	struct mesh_net *net = node_get_net(node);
	struct mesh_model *mod;
	uint8_t *label = NULL;
	uint16_t net_idx;
	bool result;
	int ele_idx;

	if (!net || msg_len > 380)
		return MESH_ERROR_INVALID_ARGS;

	/* If SRC is 0, use the Primary Element */
	if (src == 0)
		src = mesh_net_get_address(net);

	ele_idx = node_get_element_idx(node, src);
	if (ele_idx < 0)
		return MESH_ERROR_NOT_FOUND;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod)
		return MESH_ERROR_NOT_FOUND;

	if (!mod->pub) {
		l_debug("publication doesn't exist (model %x)", id);
		return MESH_ERROR_DOES_NOT_EXIST;
	}

	if (IS_UNASSIGNED(mod->pub->addr))
		return MESH_ERROR_DOES_NOT_EXIST;

	if (mod->pub->virt)
		label = mod->pub->virt->label;

	net_idx = appkey_net_idx(net, mod->pub->idx);

	result = msg_send(node, mod->pub->credential != 0, src, mod->pub->addr,
				mod->pub->idx, net_idx, label, mod->pub->ttl,
				mod->pub->rtx.cnt, mod->pub->rtx.interval,
				segmented, msg, msg_len);

	return result ? MESH_ERROR_NONE : MESH_ERROR_FAILED;
}

bool mesh_model_send(struct mesh_node *node, uint16_t src, uint16_t dst,
					uint16_t app_idx, uint16_t net_idx,
					uint8_t ttl, bool segmented,
					uint16_t msg_len, const void *msg)
{
	struct mesh_net *net = node_get_net(node);
	uint8_t cnt;
	uint16_t interval;

	/* If SRC is 0, use the Primary Element */
	if (src == 0)
		src = node_get_primary(node);

	if (IS_UNASSIGNED(dst))
		return false;

	mesh_net_transmit_params_get(net, &cnt, &interval);

	return msg_send(node, false, src, dst, app_idx, net_idx, NULL, ttl, cnt,
					interval, segmented, msg, msg_len);
}

int mesh_model_pub_set(struct mesh_node *node, uint16_t addr, uint32_t id,
			const uint8_t *pub_addr, uint16_t idx, bool cred_flag,
			uint8_t ttl, uint8_t period, uint8_t cnt,
			uint16_t interval, bool is_virt, uint16_t *pub_dst)
{
	struct mesh_model *mod;
	int status, ele_idx = node_get_element_idx(node, addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod)
		return MESH_STATUS_INVALID_MODEL;

	if (!mod->pub_enabled || (mod->cbs && !(mod->cbs->pub)))
		return MESH_STATUS_INVALID_PUB_PARAM;

	if (!appkey_have_key(node_get_net(node), idx) ||
			!has_binding(mod->bindings, idx))
		return MESH_STATUS_INVALID_APPKEY;

	/*
	 * If the publication address is set to unassigned address value,
	 * remove the publication
	 */
	if (!is_virt && IS_UNASSIGNED(l_get_le16(pub_addr))) {
		remove_pub(node, ele_idx, mod);
		return MESH_STATUS_SUCCESS;
	}

	if (cred_flag && node_lpn_mode_get(node) != MESH_MODE_ENABLED)
		return MESH_STATUS_FEATURE_NO_SUPPORT;

	/* Check if the old publication destination is a virtual label */
	if (mod->pub && mod->pub->virt) {
		unref_virt(mod->pub->virt);
		mod->pub->virt = NULL;
	}

	if (!is_virt) {
		status = set_pub(mod, l_get_le16(pub_addr), idx, cred_flag,
						ttl, period, cnt, interval);
	} else
		status = set_virt_pub(mod, pub_addr, idx, cred_flag, ttl,
							period, cnt, interval);

	if (status != MESH_STATUS_SUCCESS)
		return status;

	*pub_dst = mod->pub->addr;

	if (!mod->cbs)
		/* External model */
		config_update_model_pub_period(node, ele_idx, id,
						pub_period_to_ms(period));
	else {
		/* Internal model, call registered callbacks */
		if (mod->cbs->pub)
			mod->cbs->pub(mod->pub);
	}

	return MESH_STATUS_SUCCESS;
}

struct mesh_model_pub *mesh_model_pub_get(struct mesh_node *node, uint16_t addr,
						uint32_t mod_id, int *status)
{
	struct mesh_model *mod;
	int ele_idx = node_get_element_idx(node, addr);

	if (ele_idx < 0) {
		*status = MESH_STATUS_INVALID_ADDRESS;
		return NULL;
	}

	mod = get_model(node, (uint8_t) ele_idx, mod_id);
	if (!mod) {
		*status = MESH_STATUS_INVALID_MODEL;
		return NULL;
	}

	if (!mod->pub_enabled || (mod->cbs && !(mod->cbs->pub)))
		*status = MESH_STATUS_INVALID_PUB_PARAM;
	else
		*status = MESH_STATUS_SUCCESS;

	return mod->pub;
}

void mesh_model_free(void *data)
{
	struct mesh_model *mod = data;

	l_queue_destroy(mod->bindings, NULL);
	l_queue_destroy(mod->subs, NULL);
	l_queue_destroy(mod->virtuals, unref_virt);
	l_free(mod->pub);
	l_free(mod);
}

static void remove_subs(struct mesh_node *node, struct mesh_model *mod)
{
	const struct l_queue_entry *entry;
	struct mesh_net *net = node_get_net(node);

	entry = l_queue_get_entries(mod->subs);

	for (; entry; entry = entry->next)
		mesh_net_dst_unreg(net, (uint16_t) L_PTR_TO_UINT(entry->data));

	l_queue_clear(mod->subs, NULL);
	l_queue_clear(mod->virtuals, unref_virt);
}

static struct mesh_model *model_new(uint32_t id)
{
	struct mesh_model *mod = l_new(struct mesh_model, 1);

	mod->id = id;
	mod->virtuals = l_queue_new();

	/*
	 * Unless specifically indicated by an app, subscriptions and
	 * publications are enabled by default
	 */
	mod->sub_enabled = true;
	mod->pub_enabled = true;
	return mod;
}

static void model_enable_pub(struct mesh_model *mod, bool enable)
{
	mod->pub_enabled = enable;

	if (!mod->pub_enabled && mod->pub) {
		if (mod->pub->virt)
			unref_virt(mod->pub->virt);

		l_free(mod->pub);
		mod->pub = NULL;
	}
}

static void model_enable_sub(struct mesh_node *node, struct mesh_model *mod,
								bool enable)
{
	mod->sub_enabled = enable;

	if (!mod->sub_enabled)
		remove_subs(node, mod);
}

static bool get_model_options(struct mesh_model *mod,
					struct l_dbus_message_iter *opts)
{
	const char *key;
	struct l_dbus_message_iter var;
	bool opt;

	while (l_dbus_message_iter_next_entry(opts, &key, &var)) {

		if (!strcmp(key, "Publish")) {
			if (!l_dbus_message_iter_get_variant(&var, "b", &opt))
				return false;

			mod->pub_enabled = opt;
		} else if (!strcmp(key, "Subscribe")) {
			if (!l_dbus_message_iter_get_variant(&var, "b", &opt))
				return false;

			mod->sub_enabled = opt;
		} else
			return false;
	}

	return true;
}

bool mesh_model_add(struct mesh_node *node, struct l_queue *mods,
			uint32_t id, struct l_dbus_message_iter *opts)
{
	struct mesh_model *mod;

	/* Disallow duplicates */
	mod = l_queue_find(mods, match_model_id, L_UINT_TO_PTR(id));
	if (mod)
		return false;

	mod = model_new(id);

	if (opts && !get_model_options(mod, opts)) {
		mesh_model_free(mod);
		return false;
	}

	l_queue_insert(mods, mod, compare_model_id, NULL);
	return true;
}

/* Internal models only */
static void restore_model_state(struct mesh_model *mod)
{
	const struct mesh_model_ops *cbs;
	const struct l_queue_entry *b;

	cbs = mod->cbs;
	if (!cbs)
		return;

	if (!l_queue_isempty(mod->bindings) && cbs->bind) {
		for (b = l_queue_get_entries(mod->bindings); b; b = b->next) {
			if (cbs->bind(L_PTR_TO_UINT(b->data), ACTION_ADD) !=
							MESH_STATUS_SUCCESS)
				break;
		}
	}

	if (mod->pub && cbs->pub)
		cbs->pub(mod->pub);

}

/* This registers an internal model, i.e. implemented within meshd */
bool mesh_model_register(struct mesh_node *node, uint8_t ele_idx, uint32_t id,
					const struct mesh_model_ops *cbs,
					void *user_data)
{
	struct mesh_model *mod;

	mod = get_model(node, ele_idx, id);
	if (!mod)
		return MESH_STATUS_INVALID_MODEL;

	mod->cbs = cbs;
	mod->user_data = user_data;

	restore_model_state(mod);

	return true;
}

void mesh_model_app_key_delete(struct mesh_node *node, uint16_t ele_idx,
				struct l_queue *models, uint16_t app_idx)
{
	const struct l_queue_entry *entry = l_queue_get_entries(models);

	for (; entry; entry = entry->next) {
		struct mesh_model *mod = entry->data;

		model_unbind_idx(node, ele_idx, mod, app_idx);
	}
}

int mesh_model_binding_del(struct mesh_node *node, uint16_t addr, uint32_t id,
						uint16_t app_idx)
{
	return update_binding(node, addr, id, app_idx, true);
}

int mesh_model_binding_add(struct mesh_node *node, uint16_t addr, uint32_t id,
						uint16_t app_idx)
{
	return update_binding(node, addr, id, app_idx, false);
}

int mesh_model_get_bindings(struct mesh_node *node, uint16_t addr, uint32_t id,
				uint8_t *buf, uint16_t buf_size, uint16_t *size)
{
	struct mesh_model *mod;
	const struct l_queue_entry *entry;
	uint16_t n;
	uint32_t idx_pair;
	int i, ele_idx = node_get_element_idx(node, addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);

	if (!mod) {
		*size = 0;
		return MESH_STATUS_INVALID_MODEL;
	}

	entry = l_queue_get_entries(mod->bindings);
	n = 0;
	i = 0;
	idx_pair = 0;

	for (; entry; entry = entry->next) {
		uint16_t app_idx = (uint16_t) (L_PTR_TO_UINT(entry->data));

		if (!(i & 0x1)) {
			idx_pair = app_idx;
		} else {
			idx_pair <<= 12;
			idx_pair += app_idx;

			/* Unlikely, but check for overflow*/
			if ((n + 3) > buf_size) {
				l_warn("Binding list too large");
				goto done;
			}

			l_put_le32(idx_pair, buf);
			buf += 3;
			n += 3;
		}

		i++;
	}

	/* Process the last app key if present */
	if (i & 0x1 && ((n + 2) <= buf_size)) {
		l_put_le16(idx_pair, buf);
		n += 2;
	}

done:
	*size = n;
	return MESH_STATUS_SUCCESS;
}

int mesh_model_sub_get(struct mesh_node *node, uint16_t ele_addr, uint32_t id,
			uint8_t *buf, uint16_t buf_size, uint16_t *size)
{
	int16_t n;
	struct mesh_model *mod;
	const struct l_queue_entry *entry;
	int ele_idx = node_get_element_idx(node, ele_addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod)
		return MESH_STATUS_INVALID_MODEL;

	if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
		return MESH_STATUS_NOT_SUB_MOD;

	entry = l_queue_get_entries(mod->subs);
	*size = 0;
	n = 0;

	for (; entry; entry = entry->next) {
		if ((n + 2) > buf_size)
			return MESH_STATUS_UNSPECIFIED_ERROR;

		l_put_le16((uint16_t) L_PTR_TO_UINT(entry->data), buf);
		buf += 2;
		n += 2;
	}

	entry = l_queue_get_entries(mod->virtuals);

	for (; entry; entry = entry->next) {
		struct mesh_virtual *virt = entry->data;

		if ((n + 2) > buf_size)
			return MESH_STATUS_UNSPECIFIED_ERROR;

		l_put_le16((uint16_t) L_PTR_TO_UINT(virt->addr), buf);
		buf += 2;
		n += 2;
	}

	*size = n;
	return MESH_STATUS_SUCCESS;
}

int mesh_model_sub_add(struct mesh_node *node, uint16_t ele_addr, uint32_t id,
								uint16_t addr)
{
	struct mesh_model *mod;
	int status, ele_idx = node_get_element_idx(node, ele_addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod)
		return MESH_STATUS_INVALID_MODEL;

	if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
		return MESH_STATUS_NOT_SUB_MOD;

	status = add_sub(node_get_net(node), mod, addr);
	if (status != MESH_STATUS_SUCCESS)
		return status;

	if (!mod->cbs)
		/* External models */
		cfg_update_model_subs(node, ele_idx, mod);

	return MESH_STATUS_SUCCESS;
}

int mesh_model_virt_sub_add(struct mesh_node *node, uint16_t ele_addr,
				uint32_t id, const uint8_t *label,
				uint16_t *pub_addr)
{
	struct mesh_model *mod;
	int status, ele_idx = node_get_element_idx(node, ele_addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod)
		return MESH_STATUS_INVALID_MODEL;

	if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
		return MESH_STATUS_NOT_SUB_MOD;

	status = add_virt_sub(node_get_net(node), mod, label, pub_addr);

	if (status != MESH_STATUS_SUCCESS)
		return status;

	if (!mod->cbs)
		/* External models */
		cfg_update_model_subs(node, ele_idx, mod);

	return MESH_STATUS_SUCCESS;
}

int mesh_model_sub_ovrt(struct mesh_node *node, uint16_t ele_addr, uint32_t id,
								uint16_t addr)
{
	struct mesh_model *mod;
	int ele_idx = node_get_element_idx(node, ele_addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod)
		return MESH_STATUS_INVALID_MODEL;

	if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
		return MESH_STATUS_NOT_SUB_MOD;

	l_queue_clear(mod->subs, NULL);
	l_queue_clear(mod->virtuals, unref_virt);

	add_sub(node_get_net(node), mod, addr);

	if (!mod->cbs)
		/* External models */
		cfg_update_model_subs(node, ele_idx, mod);

	return MESH_STATUS_SUCCESS;
}

int mesh_model_virt_sub_ovrt(struct mesh_node *node, uint16_t ele_addr,
					uint32_t id, const uint8_t *label,
					uint16_t *addr)
{
	struct mesh_model *mod;
	int status, ele_idx = node_get_element_idx(node, ele_addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod)
		return MESH_STATUS_INVALID_MODEL;

	if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
		return MESH_STATUS_NOT_SUB_MOD;

	l_queue_clear(mod->subs, NULL);
	l_queue_clear(mod->virtuals, unref_virt);

	status = add_virt_sub(node_get_net(node), mod, label, addr);

	if (!mod->cbs)
		/* External models */
		cfg_update_model_subs(node, ele_idx, mod);

	return status;
}

int mesh_model_sub_del(struct mesh_node *node, uint16_t ele_addr, uint32_t id,
								uint16_t addr)
{
	struct mesh_model *mod;
	int ele_idx = node_get_element_idx(node, ele_addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod)
		return MESH_STATUS_INVALID_MODEL;

	if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
		return MESH_STATUS_NOT_SUB_MOD;

	if (l_queue_remove(mod->subs, L_UINT_TO_PTR(addr))) {
		mesh_net_dst_unreg(node_get_net(node), addr);

		if (!mod->cbs)
			/* External models */
			cfg_update_model_subs(node, ele_idx, mod);
	}

	return MESH_STATUS_SUCCESS;
}

int mesh_model_virt_sub_del(struct mesh_node *node, uint16_t ele_addr,
					uint32_t id, const uint8_t *label,
					uint16_t *addr)
{
	struct mesh_model *mod;
	struct mesh_virtual *virt;
	int ele_idx = node_get_element_idx(node, ele_addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod)
		return MESH_STATUS_INVALID_MODEL;

	if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
		return MESH_STATUS_NOT_SUB_MOD;

	virt = l_queue_remove_if(mod->virtuals, find_virt_by_label, label);

	if (virt) {
		*addr = virt->addr;
		unref_virt(virt);
	} else {
		*addr = UNASSIGNED_ADDRESS;
		return MESH_STATUS_SUCCESS;
	}

	if (l_queue_remove(mod->subs, L_UINT_TO_PTR(*addr))) {
		mesh_net_dst_unreg(node_get_net(node), *addr);

		if (!mod->cbs)
			/* External models */
			cfg_update_model_subs(node, ele_idx, mod);
	}

	return MESH_STATUS_SUCCESS;
}

int mesh_model_sub_del_all(struct mesh_node *node, uint16_t ele_addr,
								uint32_t id)
{
	struct mesh_model *mod;
	int ele_idx = node_get_element_idx(node, ele_addr);

	if (ele_idx < 0)
		return MESH_STATUS_INVALID_ADDRESS;

	mod = get_model(node, (uint8_t) ele_idx, id);
	if (!mod)
		return MESH_STATUS_INVALID_MODEL;

	if (!mod->sub_enabled || (mod->cbs && !(mod->cbs->sub)))
		return MESH_STATUS_NOT_SUB_MOD;

	remove_subs(node, mod);

	if (!mod->cbs)
		/* External models */
		cfg_update_model_subs(node, ele_idx, mod);

	return MESH_STATUS_SUCCESS;
}

static struct mesh_model *model_setup(struct mesh_net *net, uint8_t ele_idx,
					struct mesh_config_model *db_mod)
{
	struct mesh_model *mod;
	struct mesh_config_pub *pub = db_mod->pub;
	uint32_t i;

	if (db_mod->num_bindings > MAX_MODEL_BINDINGS) {
		l_warn("Binding list too long %u (max %u)",
				db_mod->num_bindings, MAX_MODEL_BINDINGS);
		return NULL;
	}

	mod = model_new(db_mod->vendor ? db_mod->id :
						SET_ID(SIG_VENDOR, db_mod->id));

	/* Implicitly bind config server model to device key */
	if (db_mod->id == CONFIG_SRV_MODEL ||
					db_mod->id == PRV_BEACON_SRV_MODEL) {

		if (ele_idx != PRIMARY_ELE_IDX) {
			l_free(mod);
			return NULL;
		}

		l_queue_push_head(mod->bindings,
					L_UINT_TO_PTR(APP_IDX_DEV_LOCAL));
		return mod;
	}

	if (db_mod->id == CONFIG_CLI_MODEL) {
		l_queue_push_head(mod->bindings,
					L_UINT_TO_PTR(APP_IDX_DEV_LOCAL));
		return mod;
	}

	/* Add application key bindings if present */
	if (db_mod->bindings) {
		mod->bindings = l_queue_new();
		for (i = 0; i < db_mod->num_bindings; i++)
			l_queue_push_tail(mod->bindings,
					L_UINT_TO_PTR(db_mod->bindings[i]));
	}

	mod->pub_enabled = db_mod->pub_enabled;

	/* Add publication if enabled and present */
	if (mod->pub_enabled && pub) {
		if (pub->virt)
			set_virt_pub(mod, pub->virt_addr, pub->idx,
					pub->credential, pub->ttl, pub->period,
					pub->cnt, pub->interval);
		else if (!IS_UNASSIGNED(pub->addr))
			set_pub(mod, pub->addr, pub->idx, pub->credential,
				pub->ttl, pub->period, pub->cnt, pub->interval);
	}

	mod->sub_enabled = db_mod->sub_enabled;

	/* Add subscriptions if enabled and present */
	if (!db_mod->subs || !mod->sub_enabled)
		return mod;

	for (i = 0; i < db_mod->num_subs; i++) {
		struct mesh_config_sub *sub = &db_mod->subs[i];

		if (!sub->virt)
			add_sub(net, mod, sub->addr.grp);
		else
			add_virt_sub(net, mod, sub->addr.label, NULL);
	}

	return mod;
}

bool mesh_model_add_from_storage(struct mesh_node *node, uint8_t ele_idx,
				struct l_queue *mods, struct l_queue *db_mods)
{
	struct mesh_net *net = node_get_net(node);
	const struct l_queue_entry *entry;

	/* Allow empty elements */
	if (!db_mods)
		return true;

	entry = l_queue_get_entries(db_mods);

	for (; entry; entry = entry->next) {
		struct mesh_model *mod;
		struct mesh_config_model *db_mod;
		uint32_t id;

		db_mod = entry->data;

		id = db_mod->vendor ? db_mod->id :
						SET_ID(SIG_VENDOR, db_mod->id);

		if (l_queue_find(mods, match_model_id, L_UINT_TO_PTR(id)))
			return false;

		mod = model_setup(net, ele_idx, db_mod);
		if (!mod)
			return false;

		l_queue_insert(mods, mod, compare_model_id, NULL);
	}

	return true;
}

void mesh_model_convert_to_storage(struct l_queue *db_mods,
							struct l_queue *mods)
{

	const struct l_queue_entry *entry = l_queue_get_entries(mods);

	for (; entry; entry = entry->next) {
		struct mesh_model *mod = entry->data;
		struct mesh_config_model *db_mod;

		db_mod = l_new(struct mesh_config_model, 1);
		db_mod->id = mod->id;
		db_mod->vendor = IS_VENDOR(mod->id);
		db_mod->pub_enabled = mod->pub_enabled;
		db_mod->sub_enabled = mod->sub_enabled;
		l_queue_push_tail(db_mods, db_mod);
	}
}

uint16_t mesh_model_opcode_set(uint32_t opcode, uint8_t *buf)
{
	if (opcode <= 0x7e) {
		buf[0] = opcode;
		return 1;
	}

	if (opcode >= 0x8000 && opcode <= 0xbfff) {
		l_put_be16(opcode, buf);
		return 2;
	}

	if (opcode >= 0xc00000 && opcode <= 0xffffff) {
		buf[0] = (opcode >> 16) & 0xff;
		l_put_be16(opcode, buf + 1);
		return 3;
	}

	l_debug("Illegal Opcode %x", opcode);
	return 0;
}

bool mesh_model_opcode_get(const uint8_t *buf, uint16_t size,
					uint32_t *opcode, uint16_t *n)
{
	if (!n || !opcode || size < 1)
		return false;

	switch (buf[0] & 0xc0) {
	case 0x00:
	case 0x40:
		/* RFU */
		if (buf[0] == 0x7f)
			return false;

		*n = 1;
		*opcode = buf[0];
		break;

	case 0x80:
		if (size < 2)
			return false;

		*n = 2;
		*opcode = l_get_be16(buf);
		break;

	case 0xc0:
		if (size < 3)
			return false;

		*n = 3;
		*opcode = l_get_be16(buf + 1);
		*opcode |= buf[0] << 16;
		break;

	default:
		print_packet("Bad", buf, size);
		return false;
	}

	return true;
}

void mesh_model_build_config(void *model, void *msg_builder)
{
	struct l_dbus_message_builder *builder = msg_builder;
	struct mesh_model *mod = model;
	uint16_t id;

	if (is_internal(mod->id))
		return;

	if (!l_queue_length(mod->subs) && !l_queue_length(mod->virtuals) &&
				!mod->pub && !l_queue_length(mod->bindings))
		return;

	l_dbus_message_builder_enter_struct(builder, "qa{sv}");

	/* Model id */
	id = MODEL_ID(mod->id);
	l_dbus_message_builder_append_basic(builder, 'q', &id);

	l_dbus_message_builder_enter_array(builder, "{sv}");

	/* For vendor models, add vendor id */
	if (IS_VENDOR(mod->id)) {
		uint16_t vendor = VENDOR_ID(mod->id);
		dbus_append_dict_entry_basic(builder, "Vendor", "q", &vendor);
	}

	/* Model bindings, if present */
	if (l_queue_length(mod->bindings))
		append_dict_uint16_array(builder, mod->bindings, "Bindings");

	/* Model periodic publication interval, if present */
	if (mod->pub) {
		uint32_t period = pub_period_to_ms(mod->pub->period);
		dbus_append_dict_entry_basic(builder, "PublicationPeriod", "u",
								&period);
	}

	if (l_queue_length(mod->subs) || l_queue_length(mod->virtuals))
		append_dict_subs_array(builder, mod->subs, mod->virtuals,
							"Subscriptions");

	l_dbus_message_builder_leave_array(builder);
	l_dbus_message_builder_leave_struct(builder);
}

void mesh_model_update_opts(struct mesh_node *node, uint8_t ele_idx,
				struct l_queue *curr, struct l_queue *updated)
{
	uint16_t primary;
	const struct l_queue_entry *entry;

	primary = node_get_primary(node);
	entry = l_queue_get_entries(curr);

	for (; entry; entry = entry->next) {
		struct mesh_model *mod, *updated_mod = entry->data;
		uint32_t id = updated_mod->id;
		bool updated_opt, vendor = IS_VENDOR(id);

		mod = l_queue_find(curr, match_model_id, L_UINT_TO_PTR(id));
		if (!mod)
			continue;

		if (!vendor)
			id = MODEL_ID(id);

		updated_opt = updated_mod->pub_enabled;
		if (mod->pub_enabled != updated_opt) {
			model_enable_pub(mod, updated_opt);
			mesh_config_model_pub_enable(node_config_get(node),
							primary + ele_idx, id,
							vendor, updated_opt);
		}

		updated_opt = updated_mod->sub_enabled;

		if (mod->pub_enabled != updated_opt) {
			model_enable_sub(node, mod, updated_opt);
			mesh_config_model_sub_enable(node_config_get(node),
							primary + ele_idx, id,
							vendor, updated_opt);
			}
		}
}

/* Populate composition buffer with model IDs */
uint16_t mesh_model_generate_composition(struct l_queue *mods, uint16_t buf_sz,
								uint8_t *buf)
{
	const struct l_queue_entry *entry;
	uint8_t num_s = 0, num_v = 0;
	uint8_t *mod_buf;
	uint16_t n;

	/* Store models IDs, store num_s and num_v later */
	mod_buf = buf;
	n = 2;

	entry = l_queue_get_entries(mods);

	/* Get SIG models */
	for (; entry; entry = entry->next) {
		struct mesh_model *mod = entry->data;

		if (n + 2 > buf_sz)
			goto done;

		if (IS_VENDOR(mod->id))
			continue;

		l_put_le16((uint16_t) (MODEL_ID(mod->id)), buf + n);
		n += 2;
		num_s++;
	}

	/* Get vendor models */
	entry = l_queue_get_entries(mods);

	for (; entry; entry = entry->next) {
		struct mesh_model *mod = entry->data;
		uint16_t vendor_id;

		if (n + 4 > buf_sz)
			goto done;

		if (!IS_VENDOR(mod->id))
			continue;

		vendor_id = (uint16_t) (VENDOR_ID(mod->id));
		l_put_le16(vendor_id, buf + n);
		n += 2;
		l_put_le16((uint16_t) (MODEL_ID(mod->id)), buf + n);
		n += 2;
		num_v++;
	}

done:
	mod_buf[0] = num_s;
	mod_buf[1] = num_v;
	return n;
}

void mesh_model_init(void)
{
	mesh_virtuals = l_queue_new();
}

void mesh_model_cleanup(void)
{
	l_queue_destroy(mesh_virtuals, l_free);
	mesh_virtuals = NULL;
}