aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-bacnet.c
blob: 1078528c11cc6cc37c2c489b62131b734b9b4213 (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
/* packet-bacnet.c
 * Routines for BACnet (NPDU) dissection
 * Copyright 2001, Hartmut Mueller <hartmut@abmlinux.org>, FH Dortmund
 * Enhanced by Steve Karg, 2005, <skarg@users.sourceforge.net>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from README.developer,v 1.23
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>

#include <epan/llcsaps.h>
#include "packet-bacnet.h"

void proto_register_bacnet(void);
void proto_reg_handoff_bacnet(void);

static dissector_handle_t bacapp_handle;
/* Defined to allow vendor identifier registration of private transfer dissectors */
static dissector_table_t bacnet_dissector_table;

static const range_string bacnet_msgtype_rvals[] = {
	{ 0x00, 0x00, "Who-Is-Router-To-Network" },
	{ 0x01, 0x01, "I-Am-Router-To-Network" },
	{ 0x02, 0x02, "I-Could-Be-Router-To-Network" },
	{ 0x03, 0x03, "Reject-Message-To-Network" },
	{ 0x04, 0x04, "Router-Busy-To-Network" },
	{ 0x05, 0x05, "Router-Available-To-Network" },
	{ 0x06, 0x06, "Initialize-Routing-Table" },
	{ 0x07, 0x07, "Initialize-Routing-Table-Ack" },
	{ 0x08, 0x08, "Establish-Connection-To-Network" },
	{ 0x09, 0x09, "Disconnect-Connection-To-Network" },
	{ 0x0A, 0x0A, "Challenge-Request" },
	{ 0x0B, 0x0B, "Security-Payload" },
	{ 0x0C, 0x0C, "Security-Response" },
	{ 0x0D, 0x0D, "Request-Key-Update" },
	{ 0x0E, 0x0E, "Update-Keyset" },
	{ 0x0F, 0x0F, "Update-distribution-Key" },
	{ 0x10, 0x10, "Request-Masterkey" },
	{ 0x11, 0x11, "Set-Masterkey" },
	{ 0x12, 0x12, "What-Is-Networknumber" },
	{ 0x13, 0x13, "Networknumber-Is" },
	{ 0x14, 0x7F, "Reserved for Use by ASHRAE" },
	{ 0x80, 0xFF, "Vendor Proprietary Message" },
	{ 0, 0, NULL }
};

static const range_string bacnet_rejectreason_name_rvals[] = {
	{ 0x00, 0x00, "Other error." },
	{ 0x01, 0x01, "The router is not directly connected to DNET and cannot find a router to DNET on any directly connected network using Who-Is-Router-To-Network messages." },
	{ 0x02, 0x02, "The router is busy and unable to accept messages for the specified DNET at the present time." },
	{ 0x03, 0x03, "It is an unknown network layer message type." },
	{ 0x04, 0x04, "The message is too long to be routed to this DNET." },
	{ 0x05, 0x05, "The router is no longer directly connected to DNET but can reconnect if requested." },
	{ 0x06, 0x06, "The router is no longer directly connected to DNET and cannot reconnect even if requested." },
	{ 0x07, 0xFF, "Invalid Rejection Reason." },
	{ 0, 0, NULL }
};

/* Network Layer Control Information */
#define BAC_CONTROL_NET		0x80
#define BAC_CONTROL_RES1	0x40
#define BAC_CONTROL_DEST	0x20
#define BAC_CONTROL_RES2	0x10
#define BAC_CONTROL_SRC		0x08
#define BAC_CONTROL_EXPECT	0x04
#define BAC_CONTROL_PRIO_HIGH	0x02
#define BAC_CONTROL_PRIO_LOW	0x01

/* Network Layer Wrapper Control Information */
#define BAC_WRAPPER_CONTROL_NET		0x80
#define BAC_WRAPPER_MSG_ENCRYPED	0x40
#define BAC_WRAPPER_RESERVED		0x20
#define BAC_WRAPPER_AUTHD_PRESENT	0x10
#define BAC_WRAPPER_DO_NOT_UNWRAP	0x08
#define BAC_WRAPPER_DO_NOT_DECRPT	0x04
#define BAC_WRAPPER_NO_TRUST_SRC	0x02
#define BAC_WRAPPER_SECURE_BY_RTR	0x01

/* Network Layer Update Keyset Control Information */
#define BAC_UPDATE_CONTROL_SET1_TIMES_PRESENT		0x80
#define BAC_UPDATE_CONTROL_SET1_PARAMS_PRESENT		0x40
#define BAC_UPDATE_CONTROL_CLEAR_SET1				0x20
#define BAC_UPDATE_CONTROL_SET2_TIMES_PRESENT		0x10
#define BAC_UPDATE_CONTROL_SET2_PARAMS_PRESENT		0x08
#define BAC_UPDATE_CONTROL_CLEAR_SET2				0x04
#define BAC_UPDATE_CONTROL_MORE_FOLLOWS				0x02
#define BAC_UPDATE_CONTROL_REMOVE_KEYS				0x01

/* Network Layer Message Types */
#define BAC_NET_WHO_R		0x00
#define BAC_NET_IAM_R		0x01
#define BAC_NET_ICB_R		0x02
#define BAC_NET_REJ			0x03
#define BAC_NET_R_BUSY		0x04
#define BAC_NET_R_AVA		0x05
#define BAC_NET_INIT_RTAB	0x06
#define BAC_NET_INIT_RTAB_ACK	0x07
#define BAC_NET_EST_CON		0x08
#define BAC_NET_DISC_CON	0x09
#define BAC_NET_CHALL_REQ	0x0A
#define BAC_NET_SECUR_PAY	0x0B
#define BAC_NET_SECUR_RESP	0x0C
#define BAC_NET_REQ_KEY_UP	0x0D
#define BAC_NET_UPD_KEYSET	0x0E
#define BAC_NET_UPD_DKEY	0x0F
#define BAC_NET_REQ_MKEY	0x10
#define BAC_NET_SET_MKEY	0x11
#define BAC_NET_WHAT_NETNR	0x12
#define BAC_NET_NETNR_IS	0x13


static const true_false_string control_net_set_high = {
	"network layer message, message type field present.",
	"BACnet APDU, message type field absent."
};

static const true_false_string control_res_high = {
	"Shall be zero, but is one.",
	"Shall be zero and is zero."
};
static const true_false_string control_dest_high = {
	"DNET, DLEN and Hop Count present. If DLEN=0: broadcast, dest. address field absent.",
	"DNET, DLEN, DADR and Hop Count absent."
};

static const true_false_string control_src_high = {
	"SNET, SLEN and SADR present, SLEN=0 invalid, SLEN specifies length of SADR",
	"SNET, SLEN and SADR absent"
};

static const true_false_string control_expect_high = {
	"BACnet-Confirmed-Request-PDU, a segment of BACnet-ComplexACK-PDU or Network Message expecting a reply present.",
	"Other than a BACnet-Confirmed-Request-PDU, segment of BACnet-ComplexACK-PDU or network layer message expecting a reply present."
};

static const true_false_string control_prio_high_high = {
	"Life Safety or Critical Equipment message.",
	"Not a Life Safety or Critical Equipment message."
};

static const true_false_string control_prio_low_high = {
	"Urgent message",
	"Normal message"
};

static const true_false_string wrapper_control_msg_net = {
	"Message is networklayer message",
	"Message is applicationlayer message"
};

static const true_false_string wrapper_control_msg_crypted = {
	"Message is encrypted message",
	"Message is not encrypted message"
};

static const true_false_string wrapper_control_reserved = {
	"Shall be zero, but is one.",
	"Shall be zero and is zero."
};

static const true_false_string wrapper_control_do_not_unwrap = {
	"Do not unwrap message",
	"Message may be unwrapped"
};

static const true_false_string wrapper_control_do_not_decrypt = {
	"Do not decrypt message",
	"Message may be decrypted"
};

static const true_false_string wrapper_control_trusted_source = {
	"Message received from trusted source",
	"Message received from untrusted source"
};

static const true_false_string security_msg_challanged = {
	"Message is challanged",
	"Message is not challanged"
};

static const true_false_string update_key_control_remove_keys = {
	"Do Remove Keys",
	"Do Not Remove Keys"
};

static const true_false_string tfs_clear_do_not_clear = {
	"Clear",
	"Do Not Clear"
};

static int proto_bacnet = -1;
static int hf_bacnet_version = -1;
static int hf_bacnet_control = -1;
static int hf_bacnet_control_net = -1;
static int hf_bacnet_control_res1 = -1;
static int hf_bacnet_control_dest = -1;
static int hf_bacnet_control_res2 = -1;
static int hf_bacnet_control_src = -1;
static int hf_bacnet_control_expect = -1;
static int hf_bacnet_control_prio_high = -1;
static int hf_bacnet_control_prio_low = -1;
static int hf_bacnet_dnet = -1;
static int hf_bacnet_dlen = -1;
static int hf_bacnet_dadr_eth = -1;
static int hf_bacnet_dadr_mstp = -1;
static int hf_bacnet_dadr_tmp = -1;
static int hf_bacnet_snet = -1;
static int hf_bacnet_slen = -1;
static int hf_bacnet_sadr_eth = -1;
static int hf_bacnet_sadr_mstp = -1;
static int hf_bacnet_sadr_tmp = -1;
static int hf_bacnet_hopc = -1;
static int hf_bacnet_mesgtyp = -1;
static int hf_bacnet_vendor = -1;
static int hf_bacnet_perf = -1;
static int hf_bacnet_rejectreason = -1;
static int hf_bacnet_rportnum = -1;
static int hf_bacnet_portid = -1;
static int hf_bacnet_pinfo = -1;
static int hf_bacnet_pinfolen = -1;
static int hf_bacnet_term_time_value = -1;
static int hf_bacnet_netno_status = -1;

static int hf_bacnet_wrapper_control = -1;
static int hf_bacnet_wrapper_control_secured_by_router = -1;
static int hf_bacnet_wrapper_control_non_trusted_source = -1;
static int hf_bacnet_wrapper_control_do_not_decrypt = -1;
static int hf_bacnet_wrapper_control_do_not_unwrap = -1;
static int hf_bacnet_wrapper_control_auth_data_present = -1;
static int hf_bacnet_wrapper_control_reserved = -1;
static int hf_bacnet_wrapper_control_msg_is_encrypted = -1;
static int hf_bacnet_wrapper_control_msg_is_networklayer = -1;
static int hf_bacnet_wrapper_key_revision = -1;
static int hf_bacnet_wrapper_key_identifier = -1;
static int hf_bacnet_wrapper_src_dev_instance = -1;
static int hf_bacnet_wrapper_message_id = -1;
static int hf_bacnet_wrapper_time_stamp = -1;
static int hf_bacnet_wrapper_dst_dev_instance = -1;
static int hf_bacnet_wrapper_dnet = -1;
static int hf_bacnet_wrapper_dlen = -1;
static int hf_bacnet_wrapper_dadr = -1;
static int hf_bacnet_wrapper_snet = -1;
static int hf_bacnet_wrapper_slen = -1;
static int hf_bacnet_wrapper_sadr = -1;
static int hf_bacnet_wrapper_auth_mech = -1;
static int hf_bacnet_wrapper_auth_usr_id = -1;
static int hf_bacnet_wrapper_auth_usr_role = -1;
static int hf_bacnet_wrapper_auth_len = -1;
static int hf_bacnet_wrapper_auth_data = -1;
static int hf_bacnet_wrapper_signature = -1;
static int hf_bacnet_wrapper_encrypted_data = -1;
static int hf_bacnet_msg_is_challanged = -1;
static int hf_bacnet_security_original_message_id = -1;
static int hf_bacnet_security_original_time_stamp = -1;
static int hf_bacnet_security_msg_len = -1;
static int hf_bacnet_security_response_code = -1;
static int hf_bacnet_security_response_expected_time_stamp = -1;
static int hf_bacnet_security_response_key_algo = -1;
static int hf_bacnet_security_response_key_id = -1;
static int hf_bacnet_security_response_original_authentication_mech = -1;
static int hf_bacnet_security_response_vendor_id = -1;
static int hf_bacnet_security_response_key_revision = -1;
static int hf_bacnet_security_response_number_keys = -1;
static int hf_bacnet_security_set1_key_reveision = -1;
static int hf_bacnet_security_set1_activation_time_stamp = -1;
static int hf_bacnet_security_set1_expiration_time_stamp = -1;
static int hf_bacnet_security_set1_key_algo = -1;
static int hf_bacnet_security_set1_key_id = -1;
static int hf_bacnet_security_set1_key_data = -1;
static int hf_bacnet_security_set2_key_reveision = -1;
static int hf_bacnet_security_set2_activation_time_stamp = -1;
static int hf_bacnet_security_set2_expiration_time_stamp = -1;
static int hf_bacnet_security_set2_key_algo = -1;
static int hf_bacnet_security_set2_key_id = -1;
static int hf_bacnet_security_set2_key_data = -1;
static int hf_bacnet_security_dist_key_revision = -1;
static int hf_bacnet_security_dist_key_algo = -1;
static int hf_bacnet_security_dist_key_id = -1;
static int hf_bacnet_security_dist_key_data = -1;
static int hf_bacnet_security_master_key_algo = -1;
static int hf_bacnet_security_master_key_id = -1;
static int hf_bacnet_security_master_key_data = -1;
static int hf_bacnet_update_control = -1;
static int hf_bacnet_update_control_remove = -1;
static int hf_bacnet_update_control_more_follows = -1;
static int hf_bacnet_update_control_clear_set2 = -1;
static int hf_bacnet_update_control_set2_params_present = -1;
static int hf_bacnet_update_control_set2_times_present = -1;
static int hf_bacnet_update_control_clear_set1 = -1;
static int hf_bacnet_update_control_set1_params_present = -1;
static int hf_bacnet_update_control_set1_times_present = -1;

static gint ett_bacnet = -1;
static gint ett_bacnet_control = -1;
static gint ett_bacnet_wrapper_control = -1;
static gint ett_bacnet_update_control = -1;

static dissector_handle_t bacnet_handle = NULL;

static const int * control_flags[] = {
	&hf_bacnet_control_net,
	&hf_bacnet_control_res1,
	&hf_bacnet_control_dest,
	&hf_bacnet_control_res2,
	&hf_bacnet_control_src,
	&hf_bacnet_control_expect,
	&hf_bacnet_control_prio_high,
	&hf_bacnet_control_prio_low,
	NULL
};

static const int * update_control_flags[] = {
	&hf_bacnet_update_control_remove,
	&hf_bacnet_update_control_more_follows,
	&hf_bacnet_update_control_clear_set2,
	&hf_bacnet_update_control_set2_params_present,
	&hf_bacnet_update_control_set2_times_present,
	&hf_bacnet_update_control_clear_set1,
	&hf_bacnet_update_control_set1_params_present,
	&hf_bacnet_update_control_set1_times_present,
	NULL
};

static const int * wrapper_control_flags[] = {
	&hf_bacnet_wrapper_control_secured_by_router,
	&hf_bacnet_wrapper_control_non_trusted_source,
	&hf_bacnet_wrapper_control_do_not_decrypt,
	&hf_bacnet_wrapper_control_do_not_unwrap,
	&hf_bacnet_wrapper_control_auth_data_present,
	&hf_bacnet_wrapper_control_reserved,
	&hf_bacnet_wrapper_control_msg_is_encrypted,
	&hf_bacnet_wrapper_control_msg_is_networklayer,
	NULL
};


int
bacnet_dissect_sec_wrapper(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree,
					gint offset, gboolean *pis_net_msg_flg)
{
	guint8 bacnet_dlen;
	guint8 bacnet_wrapper_control;
	guint16 bacnet_len;
	gint len;

	/* get control octet from wrapper */
	bacnet_wrapper_control = tvb_get_guint8(tvb, offset);
	if (pis_net_msg_flg)
		*pis_net_msg_flg = (bacnet_wrapper_control & BAC_WRAPPER_CONTROL_NET) != 0;

	proto_tree_add_bitmask(tree, tvb, offset, hf_bacnet_wrapper_control,
		ett_bacnet_wrapper_control, wrapper_control_flags, ENC_NA);
	offset++;

	proto_tree_add_item(tree, hf_bacnet_wrapper_key_revision,
		tvb, offset, 1, ENC_BIG_ENDIAN);
	offset++;

	proto_tree_add_item(tree, hf_bacnet_wrapper_key_identifier,
		tvb, offset, 2, ENC_BIG_ENDIAN);
	offset += 2;

	proto_tree_add_item(tree, hf_bacnet_wrapper_src_dev_instance,
		tvb, offset, 3, ENC_BIG_ENDIAN);
	offset += 3;

	proto_tree_add_item(tree, hf_bacnet_wrapper_message_id,
		tvb, offset, 4, ENC_BIG_ENDIAN);
	offset += 4;

	proto_tree_add_item(tree, hf_bacnet_wrapper_time_stamp,
		tvb, offset, 4, ENC_BIG_ENDIAN);
	offset += 4;

	/* we only can use unencrypted data here */
	if ((bacnet_wrapper_control & BAC_WRAPPER_MSG_ENCRYPED) == 0) {
		proto_tree_add_item(tree, hf_bacnet_wrapper_dst_dev_instance,
			tvb, offset, 3, ENC_BIG_ENDIAN);
		offset += 3;

		proto_tree_add_item(tree, hf_bacnet_wrapper_dnet,
			tvb, offset, 2, ENC_BIG_ENDIAN);
		offset += 2;

		bacnet_dlen = tvb_get_guint8(tvb, offset);
		proto_tree_add_item(tree, hf_bacnet_wrapper_dlen,
			tvb, offset, 1, ENC_BIG_ENDIAN);
		offset++;

		proto_tree_add_item(tree,
			hf_bacnet_wrapper_dadr, tvb, offset,
			bacnet_dlen, ENC_NA);
		offset += bacnet_dlen;

		proto_tree_add_item(tree, hf_bacnet_wrapper_snet,
			tvb, offset, 2, ENC_BIG_ENDIAN);
		offset += 2;

		bacnet_dlen = tvb_get_guint8(tvb, offset);
		proto_tree_add_item(tree, hf_bacnet_wrapper_slen,
			tvb, offset, 1, ENC_BIG_ENDIAN);
		offset++;

		proto_tree_add_item(tree,
			hf_bacnet_wrapper_sadr, tvb, offset,
			bacnet_dlen, ENC_NA);
		offset += bacnet_dlen;

		/* additional authentication data is optional */
		if ((bacnet_wrapper_control & BAC_WRAPPER_AUTHD_PRESENT) != 0) {
			bacnet_dlen = tvb_get_guint8(tvb, offset);
			proto_tree_add_item(tree, hf_bacnet_wrapper_auth_mech,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;

			proto_tree_add_item(tree, hf_bacnet_wrapper_auth_usr_id,
				tvb, offset, 2, ENC_BIG_ENDIAN);
			offset += 2;

			proto_tree_add_item(tree, hf_bacnet_wrapper_auth_usr_role,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;

			/* extra authentication data present if authentication mechanism != 0 */
			if (bacnet_dlen != 0) {
				bacnet_len = tvb_get_guint16(tvb, offset, ENC_BIG_ENDIAN);
				proto_tree_add_item(tree, hf_bacnet_wrapper_auth_len,
					tvb, offset, 2, ENC_BIG_ENDIAN);
				offset += 2;

				proto_tree_add_item(tree,
					hf_bacnet_wrapper_auth_data, tvb, offset,
					bacnet_len, ENC_NA);
				offset += bacnet_len;
			}
		}

		/* signature is always present and not encryped in the last 16
		   bytes of a secured BACnet frame */
		len = tvb_reported_length_remaining(tvb, 0) - 16;
		proto_tree_add_item(tree,
			hf_bacnet_wrapper_signature, tvb, len,
			16, ENC_NA);

		/* offset is pointing to the start of the secured service data which
		   is followed by the signature which we already have listed as part
		   of the wrapper so we remove the signature now */
		tvb_set_reported_length(tvb, len);
	}
	else {
		/* signature is always present and not encryped in the last 16
		bytes of a secured BACnet frame */
		len = tvb_reported_length_remaining(tvb, 0) - 16;
		proto_tree_add_item(tree,
			hf_bacnet_wrapper_signature, tvb, len,
			16, ENC_NA);
		/* print the encrypted data now because we are not able to decode it anyway */
		len = tvb_reported_length_remaining(tvb, offset) - 16;
		proto_tree_add_item(tree,
			hf_bacnet_wrapper_encrypted_data, tvb, offset,
			len, ENC_NA);
		/* no further decoding possible */
		tvb_set_reported_length(tvb, 0);
		offset = -1;
	}

	return offset;
}


static int
dissect_bacnet_npdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint offset)
{
	proto_item *ti;
	proto_tree *bacnet_tree;

	guint8 bacnet_version;
	guint8 bacnet_control;
	guint8 bacnet_update_control;
	guint8 bacnet_dlen;
	guint8 bacnet_slen;
	guint8 bacnet_mesgtyp;
	guint8 bacnet_rportnum;
	guint8 bacnet_pinfolen;
	guint8 i;
	tvbuff_t *next_tvb;
	guint32 vendor_id;

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "BACnet-NPDU");
	col_set_str(pinfo->cinfo, COL_INFO, "Building Automation and Control Network NPDU");

	bacnet_version = tvb_get_guint8(tvb, offset);
	bacnet_control = tvb_get_guint8(tvb, offset+1);

	/* I don't know the length of the NPDU yet; Setting the length after dissection */
	ti = proto_tree_add_item(tree, proto_bacnet, tvb, 0, -1, ENC_NA);

	bacnet_tree = proto_item_add_subtree(ti, ett_bacnet);

	proto_tree_add_uint_format_value(bacnet_tree, hf_bacnet_version, tvb,
					 offset, 1,
					 bacnet_version,"0x%02x (%s)",bacnet_version,
					 (bacnet_version == 0x01)?"ASHRAE 135-1995":"unknown");
	offset ++;
	proto_tree_add_bitmask(bacnet_tree, tvb, offset, hf_bacnet_control,
					ett_bacnet_control, control_flags, ENC_NA);
	offset ++;
	if (bacnet_control & BAC_CONTROL_DEST) { /* DNET, DLEN, DADR */
		proto_tree_add_item(bacnet_tree, hf_bacnet_dnet,
			tvb, offset, 2, ENC_BIG_ENDIAN);
		offset += 2;
		bacnet_dlen = tvb_get_guint8(tvb, offset);
		/* DLEN = 0 is broadcast on dest.network */
		if( bacnet_dlen == 0) {
			/* append to hf_bacnet_dlen: broadcast */
			proto_tree_add_uint_format_value(bacnet_tree,
			    hf_bacnet_dlen, tvb, offset, 1, bacnet_dlen,
			    "%d indicates Broadcast on Destination Network",
			    bacnet_dlen);
			offset ++;
			/* going to SNET */
		} else if (bacnet_dlen==6) {
			proto_tree_add_item(bacnet_tree, hf_bacnet_dlen,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset ++;
			/* Ethernet MAC */
			proto_tree_add_item(bacnet_tree,
				hf_bacnet_dadr_eth, tvb, offset,
				bacnet_dlen, ENC_NA);
			offset += bacnet_dlen;
		} else if (bacnet_dlen==1) {
			proto_tree_add_item(bacnet_tree, hf_bacnet_dlen,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset ++;
			/* MS/TP or ARCNET MAC */
			proto_tree_add_item(bacnet_tree,
				hf_bacnet_dadr_mstp, tvb, offset,
				bacnet_dlen, ENC_BIG_ENDIAN);
			offset += bacnet_dlen;
		} else if (bacnet_dlen<7) {
			proto_tree_add_item(bacnet_tree, hf_bacnet_dlen,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset ++;
			/* Other MAC formats should be included here */
			proto_tree_add_item(bacnet_tree,
				hf_bacnet_dadr_tmp, tvb, offset,
				bacnet_dlen, ENC_NA);
			offset += bacnet_dlen;
		} else {
			proto_tree_add_uint_format_value(bacnet_tree,
			    hf_bacnet_dlen, tvb, offset, 1, bacnet_dlen,
			    "%d invalid!",
			    bacnet_dlen);
		}
	}
	if (bacnet_control & BAC_CONTROL_SRC) { /* SNET, SLEN, SADR */
		/* SNET */
		proto_tree_add_item(bacnet_tree, hf_bacnet_snet,
			tvb, offset, 2, ENC_BIG_ENDIAN);
		offset += 2;
		bacnet_slen = tvb_get_guint8(tvb, offset);
		if( bacnet_slen == 0) { /* SLEN = 0 invalid */
			proto_tree_add_uint_format_value(bacnet_tree,
			    hf_bacnet_slen, tvb, offset, 1, bacnet_slen,
			    "%d invalid!",
			    bacnet_slen);
			offset ++;
		} else if (bacnet_slen==6) {
			/* SLEN */
			 proto_tree_add_item(bacnet_tree, hf_bacnet_slen,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset ++;
			/* Ethernet MAC */
			proto_tree_add_item(bacnet_tree,
				hf_bacnet_sadr_eth, tvb, offset,
				bacnet_slen, ENC_NA);
			offset += bacnet_slen;
		} else if (bacnet_slen==1) {
			/* SLEN */
			 proto_tree_add_item(bacnet_tree, hf_bacnet_slen,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset ++;
			/* MS/TP or ARCNET MAC */
			proto_tree_add_item(bacnet_tree,
				hf_bacnet_sadr_mstp, tvb, offset,
				bacnet_slen, ENC_BIG_ENDIAN);
			offset += bacnet_slen;
		} else if (bacnet_slen<6) { /* LON MAC */
			/* SLEN */
			 proto_tree_add_item(bacnet_tree, hf_bacnet_slen,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset ++;
			/* Other MAC formats should be included here */
			proto_tree_add_item(bacnet_tree,
				hf_bacnet_sadr_tmp, tvb, offset,
				bacnet_slen, ENC_NA);
			offset += bacnet_slen;
		} else {
			proto_tree_add_uint_format_value(bacnet_tree,
			hf_bacnet_slen, tvb, offset, 1, bacnet_slen,
			    "%d invalid!",
			    bacnet_slen);
			offset ++;
		}
	}
	if (bacnet_control & BAC_CONTROL_DEST) { /* Hopcount */
		proto_tree_add_item(bacnet_tree, hf_bacnet_hopc,
			tvb, offset, 1, ENC_BIG_ENDIAN);
		offset ++;
	}
	/* Network Layer Message Type */
	if (bacnet_control & BAC_CONTROL_NET) {
		bacnet_mesgtyp =  tvb_get_guint8(tvb, offset);
		proto_tree_add_uint(bacnet_tree, hf_bacnet_mesgtyp, tvb, offset, 1, bacnet_mesgtyp);
		/* Put the NPDU Type in the info column */
		col_add_str(pinfo->cinfo, COL_INFO, rval_to_str_const(bacnet_mesgtyp, bacnet_msgtype_rvals, "Unknown"));
		offset++;
		switch (bacnet_mesgtyp) {
		/* Performance Index (in I-Could-Be-Router-To-Network) */
		case BAC_NET_ICB_R:
			proto_tree_add_item(bacnet_tree, hf_bacnet_dnet,
				tvb, offset, 2, ENC_BIG_ENDIAN);
			offset += 2;
			proto_tree_add_item(bacnet_tree, hf_bacnet_perf,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset ++;
			break;
		/* Reason, DNET (in Reject-Message-To-Network) */
		case BAC_NET_REJ:
			proto_tree_add_item(bacnet_tree,
				hf_bacnet_rejectreason,
				tvb, offset, 1, ENC_NA);
			offset ++;
			proto_tree_add_item(bacnet_tree, hf_bacnet_dnet,
				tvb, offset, 2, ENC_BIG_ENDIAN);
			offset += 2;
			break;
		/* N*DNET (in Router-Busy-To-Network,Router-Available-To-Network) */
		case BAC_NET_R_BUSY:
		case BAC_NET_WHO_R:
		case BAC_NET_R_AVA:
		case BAC_NET_IAM_R:
			while(tvb_reported_length_remaining(tvb, offset) > 1 ) {
				proto_tree_add_item(bacnet_tree, hf_bacnet_dnet,
					tvb, offset, 2, ENC_BIG_ENDIAN);
				offset += 2;
			}
			break;
		/* Initialize-Routing-Table */
		case BAC_NET_INIT_RTAB:
		case BAC_NET_INIT_RTAB_ACK:
			bacnet_rportnum = tvb_get_guint8(tvb, offset);
			/* number of ports */
			proto_tree_add_item(bacnet_tree, hf_bacnet_rportnum,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset ++;
			for (i = 0; tvb_reported_length_remaining(tvb, offset) > 1 && i < bacnet_rportnum; i++) {
					/* Connected DNET */
					proto_tree_add_item(bacnet_tree, hf_bacnet_dnet,
					tvb, offset, 2, ENC_BIG_ENDIAN);
					offset += 2;
					/* Port ID */
					proto_tree_add_item(bacnet_tree, hf_bacnet_portid,
					tvb, offset, 1, ENC_BIG_ENDIAN);
					offset ++;
					/* Port Info Length */
					bacnet_pinfolen = tvb_get_guint8(tvb, offset);
					proto_tree_add_item(bacnet_tree, hf_bacnet_pinfolen,
					tvb, offset, 1, ENC_BIG_ENDIAN);
					offset ++;
					proto_tree_add_item(bacnet_tree, hf_bacnet_pinfo, tvb, offset,
					bacnet_pinfolen, ENC_NA);
					offset += bacnet_pinfolen;
			}
			break;
		/* Establish-Connection-To-Network */
		case BAC_NET_EST_CON:
			proto_tree_add_item(bacnet_tree, hf_bacnet_dnet,
				tvb, offset, 2, ENC_BIG_ENDIAN);
			offset += 2;
			proto_tree_add_item(bacnet_tree, hf_bacnet_term_time_value,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset ++;
			break;
		/* Disconnect-Connection-To-Network */
		case BAC_NET_DISC_CON:
			proto_tree_add_item(bacnet_tree, hf_bacnet_dnet,
				tvb, offset, 2, ENC_BIG_ENDIAN);
			offset += 2;
			break;
		/* What-Is-Networknumber */
		case BAC_NET_WHAT_NETNR:
			break;
		/* Networknumber-Is */
		case BAC_NET_NETNR_IS:
			proto_tree_add_item(bacnet_tree, hf_bacnet_dnet,
				tvb, offset, 2, ENC_BIG_ENDIAN);
			offset += 2;
			proto_tree_add_item(bacnet_tree, hf_bacnet_netno_status,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;
			break;
		/* Challenge-Request */
		case BAC_NET_CHALL_REQ:
			offset = bacnet_dissect_sec_wrapper(tvb, pinfo, tree, offset, NULL);
			if (offset < 0) {
				call_data_dissector(tvb, pinfo, tree);
				return tvb_captured_length(tvb);
			}

			proto_tree_add_item(tree, hf_bacnet_msg_is_challanged,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;

			proto_tree_add_item(tree, hf_bacnet_security_original_message_id,
				tvb, offset, 4, ENC_BIG_ENDIAN);
			offset += 4;

			proto_tree_add_item(tree, hf_bacnet_security_original_time_stamp,
				tvb, offset, 4, ENC_BIG_ENDIAN);
			offset += 4;
			break;
		/* Security-Payload */
		case BAC_NET_SECUR_PAY:
		{
			gboolean is_net_msg_flg;
			guint16 bacnet_len;

			offset = bacnet_dissect_sec_wrapper(tvb, pinfo, tree, offset, &is_net_msg_flg);
			if (offset < 0) {
				call_data_dissector(tvb, pinfo, tree);
				return tvb_captured_length(tvb);
			}
			/* get payload length */
			bacnet_len = tvb_get_guint16(tvb, offset, ENC_BIG_ENDIAN);
			proto_tree_add_item(tree, hf_bacnet_security_msg_len,
				tvb, offset, 2, ENC_BIG_ENDIAN);
			offset += 2;
			/* set length to reported length in header */
			tvb_set_reported_length(tvb, bacnet_len);
			if (is_net_msg_flg) {
				/* decode network layer message */
				return dissect_bacnet_npdu(tvb, pinfo, tree, offset);
			}
			/* APDU - call the APDU dissector */
			next_tvb = tvb_new_subset_remaining(tvb, offset);
			call_dissector(bacapp_handle, next_tvb, pinfo, tree);
			return tvb_captured_length(tvb);
		}
		/* Security-Response */
		case BAC_NET_SECUR_RESP:
		{
			guint8 bacnet_responsecode;

			offset = bacnet_dissect_sec_wrapper(tvb, pinfo, tree, offset, NULL);
			if (offset < 0) {
				call_data_dissector(tvb, pinfo, tree);
				return tvb_captured_length(tvb);
			}

			bacnet_responsecode = tvb_get_guint8(tvb, offset);
			proto_tree_add_item(tree, hf_bacnet_security_response_code,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;

			proto_tree_add_item(tree, hf_bacnet_security_original_message_id,
				tvb, offset, 4, ENC_BIG_ENDIAN);
			offset += 4;

			proto_tree_add_item(tree, hf_bacnet_security_original_time_stamp,
				tvb, offset, 4, ENC_BIG_ENDIAN);
			offset += 4;

			switch (bacnet_responsecode)
			{
			case 0x00: /* success */
			case 0x01: /* accessDenied */
			case 0x02: /* badDestinationAddress */
			case 0x03: /* badDestinationDeviceId */
			case 0x04: /* badSignature */
			case 0x05: /* badSourceAddress */
			case 0x08: /* cannotVerifyMessageId */
			case 0x09: /* correctKeyRevision */
			case 0x0A: /* destinationDeviceIdRequired */
			case 0x0B: /* duplicateMessage */
			case 0x0C: /* encryptionNotConfigured */
			case 0x0D: /* encryptionRequired */
			case 0x10: /* keyUpdateInProgress */
			case 0x11: /* malformedMessage */
			case 0x12: /* notKeyServer */
			case 0x13: /* securityNotConfigured */
			case 0x14: /* sourceSecurityRequired */
			case 0x19: /* unknownSourceMessage */
			default:
				/* no parameters are expected here */
				break;
			case 0x06: /* badTimestamp */
				proto_tree_add_item(tree, hf_bacnet_security_response_expected_time_stamp,
					tvb, offset, 4, ENC_BIG_ENDIAN);
				offset += 4;
				break;
			case 0x07: /* cannotUseKey */
			case 0x0F: /* invalidKeyData */
			case 0x17: /* unknownKey */
				proto_tree_add_item(tree, hf_bacnet_security_response_key_algo,
					tvb, offset, 1, ENC_BIG_ENDIAN);
				offset++;
				proto_tree_add_item(tree, hf_bacnet_security_response_key_id,
					tvb, offset, 1, ENC_BIG_ENDIAN);
				offset++;
				break;
			case 0x0E: /* incorrectKey */
				bacnet_responsecode = tvb_get_guint8(tvb, offset);
				offset++;
				while (tvb_reported_length_remaining(tvb, offset) > 1 && bacnet_responsecode > 0) {
					proto_tree_add_item(tree, hf_bacnet_security_response_key_algo,
						tvb, offset, 1, ENC_BIG_ENDIAN);
					offset++;
					proto_tree_add_item(tree, hf_bacnet_security_response_key_id,
						tvb, offset, 1, ENC_BIG_ENDIAN);
					offset++;
					bacnet_responsecode--;
				}
				break;
			case 0x16: /* unknownAuthenticationType */
				proto_tree_add_item(tree, hf_bacnet_security_response_original_authentication_mech,
					tvb, offset, 1, ENC_BIG_ENDIAN);
				offset++;
				proto_tree_add_item(tree, hf_bacnet_security_response_vendor_id,
					tvb, offset, 2, ENC_BIG_ENDIAN);
				offset += 2;
				break;
			case 0x18: /* unknownKeyRevision */
				proto_tree_add_item(tree, hf_bacnet_security_response_key_revision,
					tvb, offset, 1, ENC_BIG_ENDIAN);
				offset++;
				break;
			case 0x15: /* tooManyKeys */
				proto_tree_add_item(tree, hf_bacnet_security_response_number_keys,
					tvb, offset, 1, ENC_BIG_ENDIAN);
				offset++;
				break;
			}
		}
			break;
		/* Request-Key-Update */
		case BAC_NET_REQ_KEY_UP:
			offset = bacnet_dissect_sec_wrapper(tvb, pinfo, tree, offset, NULL);
			if (offset < 0) {
				call_data_dissector(tvb, pinfo, tree);
				return tvb_captured_length(tvb);
			}

			proto_tree_add_item(tree, hf_bacnet_security_set1_key_reveision,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;
			proto_tree_add_item(tree, hf_bacnet_security_set1_activation_time_stamp,
				tvb, offset, 4, ENC_BIG_ENDIAN);
			offset += 4;
			proto_tree_add_item(tree, hf_bacnet_security_set1_expiration_time_stamp,
				tvb, offset, 4, ENC_BIG_ENDIAN);
			offset += 4;

			proto_tree_add_item(tree, hf_bacnet_security_set2_key_reveision,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;
			proto_tree_add_item(tree, hf_bacnet_security_set2_activation_time_stamp,
				tvb, offset, 4, ENC_BIG_ENDIAN);
			offset += 4;
			proto_tree_add_item(tree, hf_bacnet_security_set2_expiration_time_stamp,
				tvb, offset, 4, ENC_BIG_ENDIAN);
			offset += 4;

			proto_tree_add_item(tree, hf_bacnet_security_dist_key_revision,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;
			break;
		/* Update-Keyset */
		case BAC_NET_UPD_KEYSET:
			offset = bacnet_dissect_sec_wrapper(tvb, pinfo, tree, offset, NULL);
			if (offset < 0) {
				call_data_dissector(tvb, pinfo, tree);
				return tvb_captured_length(tvb);
			}

			bacnet_update_control = tvb_get_guint8(tvb, offset);
			proto_tree_add_bitmask(tree, tvb, offset, hf_bacnet_update_control,
				ett_bacnet_update_control, update_control_flags, ENC_NA);
			offset++;

			if (bacnet_update_control & BAC_UPDATE_CONTROL_SET1_TIMES_PRESENT) {
				proto_tree_add_item(tree, hf_bacnet_security_set1_key_reveision,
					tvb, offset, 1, ENC_BIG_ENDIAN);
				offset++;
				proto_tree_add_item(tree, hf_bacnet_security_set1_activation_time_stamp,
					tvb, offset, 4, ENC_BIG_ENDIAN);
				offset += 4;
				proto_tree_add_item(tree, hf_bacnet_security_set1_expiration_time_stamp,
					tvb, offset, 4, ENC_BIG_ENDIAN);
				offset += 4;
			}

			if (bacnet_update_control & BAC_UPDATE_CONTROL_SET1_PARAMS_PRESENT) {
				guint8 keycount;

				keycount = tvb_get_guint8(tvb, offset);
				offset++;

				for (i = 0; tvb_reported_length_remaining(tvb, offset) > 1 && i < keycount; i++)	{
					proto_tree_add_item(tree, hf_bacnet_security_set1_key_algo,
						tvb, offset, 1, ENC_BIG_ENDIAN);
					offset++;
					proto_tree_add_item(tree, hf_bacnet_security_set1_key_id,
						tvb, offset, 1, ENC_BIG_ENDIAN);
					offset++;

					bacnet_dlen = tvb_get_guint8(tvb, offset);
					offset++;

					proto_tree_add_item(tree,
						hf_bacnet_security_set1_key_data, tvb, offset,
						bacnet_dlen, ENC_NA);
					offset += bacnet_dlen;
				}
			}

			if (bacnet_update_control & BAC_UPDATE_CONTROL_SET2_TIMES_PRESENT) {
				proto_tree_add_item(tree, hf_bacnet_security_set2_key_reveision,
					tvb, offset, 1, ENC_BIG_ENDIAN);
				offset++;
				proto_tree_add_item(tree, hf_bacnet_security_set2_activation_time_stamp,
					tvb, offset, 4, ENC_BIG_ENDIAN);
				offset += 4;
				proto_tree_add_item(tree, hf_bacnet_security_set2_expiration_time_stamp,
					tvb, offset, 4, ENC_BIG_ENDIAN);
				offset += 4;
			}

			if (bacnet_update_control & BAC_UPDATE_CONTROL_SET2_PARAMS_PRESENT) {
				guint8 keycount;

				keycount = tvb_get_guint8(tvb, offset);
				offset++;

				for (i = 0; tvb_reported_length_remaining(tvb, offset) > 1 && i < keycount; i++)	{
					proto_tree_add_item(tree, hf_bacnet_security_set2_key_algo,
						tvb, offset, 1, ENC_BIG_ENDIAN);
					offset++;
					proto_tree_add_item(tree, hf_bacnet_security_set2_key_id,
						tvb, offset, 1, ENC_BIG_ENDIAN);
					offset++;

					bacnet_dlen = tvb_get_guint8(tvb, offset);
					offset++;

					proto_tree_add_item(tree,
						hf_bacnet_security_set2_key_data, tvb, offset,
						bacnet_dlen, ENC_NA);
					offset += bacnet_dlen;
				}
			}
			break;
		/* Update-distribution-Key */
		case BAC_NET_UPD_DKEY:
			offset = bacnet_dissect_sec_wrapper(tvb, pinfo, tree, offset, NULL);
			if (offset < 0) {
				call_data_dissector(tvb, pinfo, tree);
				return tvb_captured_length(tvb);
			}

			proto_tree_add_item(tree, hf_bacnet_security_dist_key_revision,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;
			proto_tree_add_item(tree, hf_bacnet_security_dist_key_algo,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;
			proto_tree_add_item(tree, hf_bacnet_security_dist_key_id,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;

			bacnet_dlen = tvb_get_guint8(tvb, offset);
			offset++;

			proto_tree_add_item(tree,
				hf_bacnet_security_dist_key_data, tvb, offset,
				bacnet_dlen, ENC_NA);
			offset += bacnet_dlen;
			break;
		/* Request-Masterkey */
		case BAC_NET_REQ_MKEY:
		{
			guint8 keycount;

			offset = bacnet_dissect_sec_wrapper(tvb, pinfo, tree, offset, NULL);
			if (offset < 0) {
				call_data_dissector(tvb, pinfo, tree);
				return tvb_captured_length(tvb);
			}

			keycount = tvb_get_guint8(tvb, offset);
			offset++;
			while (tvb_reported_length_remaining(tvb, offset) > 1 && keycount > 0) {
				proto_tree_add_item(tree, hf_bacnet_security_master_key_algo,
					tvb, offset, 1, ENC_BIG_ENDIAN);
				offset++;
				keycount--;
			}
			break;
		}
		/* Set-Masterkey */
		case BAC_NET_SET_MKEY:
			offset = bacnet_dissect_sec_wrapper(tvb, pinfo, tree, offset, NULL);
			if (offset < 0) {
				call_data_dissector(tvb, pinfo, tree);
				return tvb_captured_length(tvb);
			}

			proto_tree_add_item(tree, hf_bacnet_security_master_key_algo,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;
			proto_tree_add_item(tree, hf_bacnet_security_master_key_id,
				tvb, offset, 1, ENC_BIG_ENDIAN);
			offset++;

			bacnet_dlen = tvb_get_guint8(tvb, offset);
			offset++;

			proto_tree_add_item(tree,
				hf_bacnet_security_master_key_data, tvb, offset,
				bacnet_dlen, ENC_NA);
			offset += bacnet_dlen;
			break;
		default:
			/* Vendor ID
			* The standard says: "If Bit 7 of the control octet is 1 and
			* the Message Type field contains a value in the range
			* X'80' - X'FF', then a Vendor ID field shall be present (...)."
			* We should not go any further in dissecting the packet if it's
			* not present, but we don't know about that: No length field...
			*/
			if (bacnet_mesgtyp > 0x7f) {
				/* Note: our next_tvb includes message type and vendor id! */
				next_tvb = tvb_new_subset_remaining(tvb, offset-1);
				vendor_id = tvb_get_ntohs(tvb, offset);
				proto_tree_add_item(bacnet_tree, hf_bacnet_vendor, tvb,
						offset, 2, ENC_BIG_ENDIAN);
				offset += 2;	/* vendor_id */
				if (dissector_try_uint(bacnet_dissector_table,
						vendor_id, next_tvb, pinfo, bacnet_tree)) {
						/* we parsed it so skip over length and we are done */
						/* Note: offset has now been bumped for message type and vendor
						   id so we take that out of our next_tvb size */
						offset += tvb_reported_length(next_tvb) -3;
				}
			}
			break;
		}
	}

	/* Now set NPDU length */
	proto_item_set_len(ti, offset);

	/* dissect BACnet APDU */
	next_tvb = tvb_new_subset_remaining(tvb,offset);
	if (bacnet_control & BAC_CONTROL_NET) {
		/* Unknown function - dissect the payload as data */
		call_data_dissector(next_tvb, pinfo, tree);
	} else {
		/* APDU - call the APDU dissector */
		call_dissector(bacapp_handle, next_tvb, pinfo, tree);
	}
	return tvb_captured_length(tvb);
}

static int
dissect_bacnet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
	return dissect_bacnet_npdu(tvb, pinfo, tree, 0);
}

void
proto_register_bacnet(void)
{
	static hf_register_info hf[] = {
		{ &hf_bacnet_version,
			{ "Version",
			"bacnet.version",
			FT_UINT8, BASE_DEC, NULL, 0,
			"BACnet Version", HFILL }
		},
		{ &hf_bacnet_control,
			{ "Control",
			"bacnet.control",
			FT_UINT8, BASE_HEX, NULL, 0,
			"BACnet Control", HFILL }
		},
		{ &hf_bacnet_control_net,
			{ "NSDU contains",
			"bacnet.control_net",
			FT_BOOLEAN, 8, TFS(&control_net_set_high),
			BAC_CONTROL_NET, "BACnet Control", HFILL }
		},
		{ &hf_bacnet_control_res1,
			{ "Reserved",
			"bacnet.control_res1",
			FT_BOOLEAN, 8, TFS(&control_res_high),
			BAC_CONTROL_RES1, "BACnet Control", HFILL }
		},
		{ &hf_bacnet_control_dest,
			{ "Destination Specifier",
			"bacnet.control_dest",
			FT_BOOLEAN, 8, TFS(&control_dest_high),
			BAC_CONTROL_DEST, "BACnet Control", HFILL }
		},
		{ &hf_bacnet_control_res2,
			{ "Reserved",
			"bacnet.control_res2",
			FT_BOOLEAN, 8, TFS(&control_res_high),
			BAC_CONTROL_RES2, "BACnet Control", HFILL }
		},
		{ &hf_bacnet_control_src,
			{ "Source specifier",
			"bacnet.control_src",
			FT_BOOLEAN, 8, TFS(&control_src_high),
			BAC_CONTROL_SRC, "BACnet Control", HFILL }
		},
		{ &hf_bacnet_control_expect,
			{ "Expecting Reply",
			"bacnet.control_expect",
			FT_BOOLEAN, 8, TFS(&control_expect_high),
			BAC_CONTROL_EXPECT, "BACnet Control", HFILL }
		},
		{ &hf_bacnet_control_prio_high,
			{ "Priority",
			"bacnet.control_prio_high",
			FT_BOOLEAN, 8, TFS(&control_prio_high_high),
			BAC_CONTROL_PRIO_HIGH, "BACnet Control", HFILL }
		},
		{ &hf_bacnet_control_prio_low,
			{ "Priority",
			"bacnet.control_prio_low",
			FT_BOOLEAN, 8, TFS(&control_prio_low_high),
			BAC_CONTROL_PRIO_LOW, "BACnet Control", HFILL }
		},
		{ &hf_bacnet_dnet,
			{ "Destination Network Address",
			"bacnet.dnet",
			FT_UINT16, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_dlen,
			{ "Destination MAC Layer Address Length",
			"bacnet.dlen",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_dadr_eth,
			{ "Destination ISO 8802-3 MAC Address",
			"bacnet.dadr_eth",
			FT_ETHER, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_dadr_mstp,
			{ "DADR",
			"bacnet.dadr_mstp",
			FT_UINT8, BASE_DEC, NULL, 0,
			"Destination MS/TP or ARCNET MAC Address", HFILL }
		},
		{ &hf_bacnet_dadr_tmp,
			{ "Unknown Destination MAC",
			"bacnet.dadr_tmp",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_snet,
			{ "Source Network Address",
			"bacnet.snet",
			FT_UINT16, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_slen,
			{ "Source MAC Layer Address Length",
			"bacnet.slen",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_sadr_eth,
			{ "SADR",
			"bacnet.sadr_eth",
			FT_ETHER, BASE_NONE, NULL, 0,
			"Source ISO 8802-3 MAC Address", HFILL }
		},
		{ &hf_bacnet_sadr_mstp,
			{ "SADR",
			"bacnet.sadr_mstp",
			FT_UINT8, BASE_DEC, NULL, 0,
			"Source MS/TP or ARCNET MAC Address", HFILL }
		},
		{ &hf_bacnet_sadr_tmp,
			{ "Unknown Source MAC",
			"bacnet.sadr_tmp",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_hopc,
			{ "Hop Count",
			"bacnet.hopc",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_mesgtyp,
			{ "Network Layer Message Type",
			"bacnet.mesgtyp",
			FT_UINT8, BASE_HEX | BASE_RANGE_STRING, RVALS(bacnet_msgtype_rvals), 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_vendor,
			{ "Vendor ID",
			"bacnet.vendor",
			FT_UINT16, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_perf,
			{ "Performance Index",
			"bacnet.perf",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_rejectreason,
			{ "Reject Reason",
			"bacnet.rejectreason",
			FT_UINT8, BASE_DEC| BASE_RANGE_STRING, RVALS(bacnet_rejectreason_name_rvals), 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_rportnum,
			{ "Number of Port Mappings",
			"bacnet.rportnum",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_pinfolen,
			{ "Port Info Length",
			"bacnet.pinfolen",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_pinfo,
			{ "Port Inf",
			"bacnet.pinfo",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_portid,
			{ "Port ID",
			"bacnet.portid",
			FT_UINT8, BASE_HEX, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_term_time_value,
			{ "Termination Time Value (seconds)",
			"bacnet.term_time_value",
			FT_UINT8, BASE_DEC, NULL, 0,
			"Termination Time Value", HFILL }
		},
		{ &hf_bacnet_netno_status,
			{ "Network number status (enumerated)",
			"bacnet.netno_status",
			FT_UINT8, BASE_DEC, NULL, 0,
			"Network number status", HFILL }
		},
		{ &hf_bacnet_wrapper_control,
			{ "Wrapper control",
			"bacnet.wrappercontrol",
			FT_UINT8, BASE_HEX, NULL, 0,
			"BACnet wrapper control", HFILL }
		},
		{ &hf_bacnet_wrapper_control_secured_by_router,
			{ "Secured by router",
			"bacnet.wrappercontrol_secured_by_router",
			FT_BOOLEAN, 8, TFS(&tfs_yes_no),
			BAC_WRAPPER_SECURE_BY_RTR, "BACnet wrapper control", HFILL }
		},
		{ &hf_bacnet_wrapper_control_non_trusted_source,
			{ "Non trusted source",
			"bacnet.wrappercontrol_non_trusted_source",
			FT_BOOLEAN, 8, TFS(&wrapper_control_trusted_source),
			BAC_WRAPPER_NO_TRUST_SRC, "BACnet wrapper control", HFILL }
		},
		{ &hf_bacnet_wrapper_control_do_not_decrypt,
			{ "Do not decrypt",
			"bacnet.wrappercontrol_do_not_decrypt",
			FT_BOOLEAN, 8, TFS(&wrapper_control_do_not_decrypt),
			BAC_WRAPPER_DO_NOT_DECRPT, "BACnet wrapper control", HFILL }
		},
		{ &hf_bacnet_wrapper_control_do_not_unwrap,
			{ "Do not unwrap",
			"bacnet.wrappercontrol_do_not_unwrap",
			FT_BOOLEAN, 8, TFS(&wrapper_control_do_not_unwrap),
			BAC_WRAPPER_DO_NOT_UNWRAP, "BACnet wrapper control", HFILL }
		},
		{ &hf_bacnet_wrapper_control_auth_data_present,
			{ "Authentication data present",
			"bacnet.wrappercontrol_auth_data_present",
			FT_BOOLEAN, 8, TFS(&tfs_present_not_present),
			BAC_WRAPPER_AUTHD_PRESENT, "BACnet wrapper control", HFILL }
		},
		{ &hf_bacnet_wrapper_control_reserved,
			{ "Reserved",
			"bacnet.wrappercontrol_reserved",
			FT_BOOLEAN, 8, TFS(&wrapper_control_reserved),
			BAC_WRAPPER_RESERVED, "BACnet wrapper control", HFILL }
		},
		{ &hf_bacnet_wrapper_control_msg_is_encrypted,
			{ "Message is encrypted message",
			"bacnet.wrappercontrol_msg_is_crypted",
			FT_BOOLEAN, 8, TFS(&wrapper_control_msg_crypted),
			BAC_WRAPPER_MSG_ENCRYPED, "BACnet wrapper control", HFILL }
		},
		{ &hf_bacnet_wrapper_control_msg_is_networklayer,
			{ "Message is networklayer message",
			"bacnet.wrappercontrol_msg_is_netlayer",
			FT_BOOLEAN, 8, TFS(&wrapper_control_msg_net),
			BAC_WRAPPER_CONTROL_NET, "BACnet wrapper control", HFILL }
		},
		{ &hf_bacnet_wrapper_key_revision,
			{ "Wrapper Key Revision",
			"bacnet.wrapper_key_revision",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_key_identifier,
			{ "Wrapper Key Identifier",
			"bacnet.wrapper_key_identifier",
			FT_UINT16, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_src_dev_instance,
			{ "Wrapper Source Device Instance",
			"bacnet.wrapper_src_device_instance",
			FT_UINT24, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_message_id,
			{ "Wrapper Message Id",
			"bacnet.wrapper_msg_id",
			FT_UINT32, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_time_stamp,
			{ "Wrapper Message Timestamp",
			"bacnet.wrapper_time_stamp",
			FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_dst_dev_instance,
			{ "Wrapper Destination Device Instance",
			"bacnet.wrapper_dst_device_instance",
			FT_UINT24, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_dnet,
			{ "Wrapper Destination Network Address",
			"bacnet.wrapper_dnet",
			FT_UINT16, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_dlen,
			{ "Wrapper Destination MAC Layer Address Length",
			"bacnet.wrapper_dlen",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_dadr,
			{ "Wrapper Destination MAC",
			"bacnet.wrapper_dadr",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_snet,
			{ "Wrapper Source Network Address",
			"bacnet.wrapper_snet",
			FT_UINT16, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_slen,
			{ "Wrapper Source MAC Layer Address Length",
			"bacnet.wrapper_slen",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_sadr,
			{ "Wrapper Source MAC",
			"bacnet.wrapper_sadr",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_auth_mech,
			{ "Wrapper Authentication Mechanism",
			"bacnet.wrapper_auth_mech",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_auth_usr_id,
			{ "Wrapper Authentication User Id",
			"bacnet.wrapper_auth_usr_id",
			FT_UINT16, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_auth_usr_role,
			{ "Wrapper Authentication User Role",
			"bacnet.wrapper_auth_usr_role",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_auth_len,
			{ "Wrapper Authentication Length",
			"bacnet.wrapper_auth_len",
			FT_UINT16, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_auth_data,
			{ "Wrapper Authentication Data",
			"bacnet.wrapper_auth_data",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_signature,
			{ "Wrapper Signature",
			"bacnet.wrapper_signature",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_wrapper_encrypted_data,
			{ "Wrapper Encrypted Data",
			"bacnet.wrapper_encrypted_data",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_msg_is_challanged,
			{ "Message is challanged message",
			"bacnet.is_challanged_message",
			FT_BOOLEAN, 8, TFS(&security_msg_challanged),
			1, "BACnet security", HFILL }
		},
		{ &hf_bacnet_security_original_message_id,
			{ "Security Original Message Id",
			"bacnet.security_original_message_id",
			FT_UINT32, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_original_time_stamp,
			{ "Security Original Message Timestamp",
			"bacnet.security_original_time_stamp",
			FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_msg_len,
			{ "Security Message Length",
			"bacnet.security_message_length",
			FT_UINT16, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_response_code,
			{ "Security Response Code",
			"bacnet.security_response_code",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_response_expected_time_stamp,
			{ "Security Expected Timestamp",
			"bacnet.security_response_expected_time_stamp",
			FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_response_key_algo,
			{ "Security Response Key Algorithm",
			"bacnet.security_response_key_algorithm",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_response_key_id,
			{ "Security Response Key ID",
			"bacnet.security_response_key_id",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_response_original_authentication_mech,
			{ "Security Response Original Authentication Mechanism",
			"bacnet.security_response_original_authentication_mechanism",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_response_vendor_id,
			{ "Security Response Vendor ID",
			"bacnet.security_response_vendor_id",
			FT_UINT16, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_response_key_revision,
			{ "Security Response Key Revision",
			"bacnet.security_response_key_revision",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_response_number_keys,
			{ "Security Response Number Of Keys",
			"bacnet.security_response_number_of_keys",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set1_key_reveision,
			{ "Security Set 1 Key Revision",
			"bacnet.security_set1_key_revision",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set1_activation_time_stamp,
			{ "Security Set 1 Activation Timestamp",
			"bacnet.security_set1_activation_time_stamp",
			FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set1_expiration_time_stamp,
			{ "Security Set 1 Expiration Timestamp",
			"bacnet.security_set1_expiration_time_stamp",
			FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set1_key_algo,
			{ "Security Keyset 1 Algorithm",
			"bacnet.security_set1_key_algorithm",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set1_key_id,
			{ "Security Keyset 1 Key ID",
			"bacnet.security_set1_key_id",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set1_key_data,
			{ "Security Keyset 1 Key Data",
			"bacnet.security_set1_key_data",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set2_key_reveision,
			{ "Security Set 2 Key Revision",
			"bacnet.security_set2_key_revision",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set2_activation_time_stamp,
			{ "Security Set 2 Activation Timestamp",
			"bacnet.security_set2_activation_time_stamp",
			FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set2_expiration_time_stamp,
			{ "Security Set 2 Expiration Timestamp",
			"bacnet.security_set2_expiration_time_stamp",
			FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set2_key_algo,
			{ "Security Keyset 2 Algorithm",
			"bacnet.security_set2_key_algorithm",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set2_key_id,
			{ "Security Keyset 2 Key ID",
			"bacnet.security_set2_key_id",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_set2_key_data,
			{ "Security Keyset 2 Key Data",
			"bacnet.security_set2_key_data",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_dist_key_revision,
			{ "Security Distribution Key Revision",
			"bacnet.security_distribution_key_revision",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_dist_key_algo,
			{ "Security Keyset 2 Algorithm",
			"bacnet.security_distribution_key_algorithm",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_dist_key_id,
			{ "Security Keyset 2 Key ID",
			"bacnet.security_distribution_key_id",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_dist_key_data,
			{ "Security Keyset 2 Key Data",
			"bacnet.security_distribution_key_data",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_master_key_algo,
			{ "Security Master Key Algorithm",
			"bacnet.security_master_key_algorithm",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_master_key_id,
			{ "Security Master Key ID",
			"bacnet.security_master_key_id",
			FT_UINT8, BASE_DEC, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_security_master_key_data,
			{ "Security Master Key Data",
			"bacnet.security_master_key_data",
			FT_BYTES, BASE_NONE, NULL, 0,
			NULL, HFILL }
		},
		{ &hf_bacnet_update_control,
			{ "Update control",
			"bacnet.update_control",
			FT_UINT8, BASE_HEX, NULL, 0,
			"BACnet update control", HFILL }
		},
		{ &hf_bacnet_update_control_remove,
			{ "Key Update Control Remove Keys",
			"bacnet.update_control_remove_keys",
			FT_BOOLEAN, 8, TFS(&update_key_control_remove_keys),
			BAC_UPDATE_CONTROL_REMOVE_KEYS, "BACnet update keys control", HFILL }
		},
		{ &hf_bacnet_update_control_more_follows,
			{ "Key Update Control More Keys Follow",
			"bacnet.update_control_more_keys_follow",
			FT_BOOLEAN, 8, TFS(&tfs_yes_no),
			BAC_UPDATE_CONTROL_MORE_FOLLOWS, "BACnet update keys control", HFILL }
		},
		{ &hf_bacnet_update_control_clear_set2,
			{ "Key Update Control Set 2 Clear",
			"bacnet.update_control_set2_clear",
			FT_BOOLEAN, 8, TFS(&tfs_clear_do_not_clear),
			BAC_UPDATE_CONTROL_CLEAR_SET2, "BACnet update keys control", HFILL }
		},
		{ &hf_bacnet_update_control_set2_params_present,
			{ "Key Update Control Set 2 Params Present",
			"bacnet.update_control_set2_params_present",
			FT_BOOLEAN, 8, TFS(&tfs_present_not_present),
			BAC_UPDATE_CONTROL_SET2_PARAMS_PRESENT, "BACnet update keys control", HFILL }
		},
		{ &hf_bacnet_update_control_set2_times_present,
			{ "Key Update Control Set 2 Time Present",
			"bacnet.update_control_set2_time_present",
			FT_BOOLEAN, 8, TFS(&tfs_present_not_present),
			BAC_UPDATE_CONTROL_SET2_TIMES_PRESENT, "BACnet update keys control", HFILL }
		},
		{ &hf_bacnet_update_control_clear_set1,
			{ "Key Update Control Set 1 Clear",
			"bacnet.update_control_set1_clear",
			FT_BOOLEAN, 8, TFS(&tfs_clear_do_not_clear),
			BAC_UPDATE_CONTROL_CLEAR_SET1, "BACnet update keys control", HFILL }
		},
		{ &hf_bacnet_update_control_set1_params_present,
			{ "Key Update Control Set 1 Params Present",
			"bacnet.update_control_set1_params_present",
			FT_BOOLEAN, 8, TFS(&tfs_present_not_present),
			BAC_UPDATE_CONTROL_SET1_PARAMS_PRESENT, "BACnet update keys control", HFILL }
		},
		{ &hf_bacnet_update_control_set1_times_present,
			{ "Key Update Control Set 1 Time Present",
			"bacnet.update_control_set1_time_present",
			FT_BOOLEAN, 8, TFS(&tfs_present_not_present),
			BAC_UPDATE_CONTROL_SET1_TIMES_PRESENT, "BACnet update keys control", HFILL }
		},
	};

	static gint *ett[] = {
		&ett_bacnet,
		&ett_bacnet_control,
		&ett_bacnet_wrapper_control,
		&ett_bacnet_update_control,
	};

	proto_bacnet = proto_register_protocol("Building Automation and Control Network NPDU", "BACnet", "bacnet");

	proto_register_field_array(proto_bacnet, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));

	bacnet_handle = register_dissector("bacnet", dissect_bacnet, proto_bacnet);

	bacnet_dissector_table = register_dissector_table("bacnet.vendor",
							  "BACnet Vendor Identifier", proto_bacnet,
							  FT_UINT8, BASE_HEX);
}

void
proto_reg_handoff_bacnet(void)
{
	dissector_add_uint("bvlc.function", 0x04, bacnet_handle);
	dissector_add_uint("bvlc.function", 0x09, bacnet_handle);
	dissector_add_uint("bvlc.function", 0x0a, bacnet_handle);
	dissector_add_uint("bvlc.function", 0x0b, bacnet_handle);
	dissector_add_uint("bvlc.function_ipv6", 0x01, bacnet_handle);
	dissector_add_uint("bvlc.function_ipv6", 0x02, bacnet_handle);
	dissector_add_uint("bvlc.function_ipv6", 0x0c, bacnet_handle);
	dissector_add_uint("bvlc.function_ipv6", 0x08, bacnet_handle);
	dissector_add_uint("llc.dsap", SAP_BACNET, bacnet_handle);
	bacapp_handle = find_dissector_add_dependency("bacapp", proto_bacnet);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */