-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathpacket-bacnet.c
1739 lines (1630 loc) · 53.9 KB
/
packet-bacnet.c
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
/* packet-bacnet.c
* Routines for BACnet (NPDU) dissection
* Copyright 2001, Hartmut Mueller <[email protected]>, FH Dortmund
* Enhanced by Steve Karg, 2005, <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* 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/tfs.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_challenged = {
"Message is challenged",
"Message is not challenged"
};
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;
static int hf_bacnet_version;
static int hf_bacnet_control;
static int hf_bacnet_control_net;
static int hf_bacnet_control_res1;
static int hf_bacnet_control_dest;
static int hf_bacnet_control_res2;
static int hf_bacnet_control_src;
static int hf_bacnet_control_expect;
static int hf_bacnet_control_prio_high;
static int hf_bacnet_control_prio_low;
static int hf_bacnet_dnet;
static int hf_bacnet_dlen;
static int hf_bacnet_dadr_eth;
static int hf_bacnet_dadr_mstp;
static int hf_bacnet_dadr_tmp;
static int hf_bacnet_snet;
static int hf_bacnet_slen;
static int hf_bacnet_sadr_eth;
static int hf_bacnet_sadr_mstp;
static int hf_bacnet_sadr_tmp;
static int hf_bacnet_hopc;
static int hf_bacnet_mesgtyp;
static int hf_bacnet_vendor;
static int hf_bacnet_perf;
static int hf_bacnet_rejectreason;
static int hf_bacnet_rportnum;
static int hf_bacnet_portid;
static int hf_bacnet_pinfo;
static int hf_bacnet_pinfolen;
static int hf_bacnet_term_time_value;
static int hf_bacnet_netno_status;
static int hf_bacnet_wrapper_control;
static int hf_bacnet_wrapper_control_secured_by_router;
static int hf_bacnet_wrapper_control_non_trusted_source;
static int hf_bacnet_wrapper_control_do_not_decrypt;
static int hf_bacnet_wrapper_control_do_not_unwrap;
static int hf_bacnet_wrapper_control_auth_data_present;
static int hf_bacnet_wrapper_control_reserved;
static int hf_bacnet_wrapper_control_msg_is_encrypted;
static int hf_bacnet_wrapper_control_msg_is_networklayer;
static int hf_bacnet_wrapper_key_revision;
static int hf_bacnet_wrapper_key_identifier;
static int hf_bacnet_wrapper_src_dev_instance;
static int hf_bacnet_wrapper_message_id;
static int hf_bacnet_wrapper_time_stamp;
static int hf_bacnet_wrapper_dst_dev_instance;
static int hf_bacnet_wrapper_dnet;
static int hf_bacnet_wrapper_dlen;
static int hf_bacnet_wrapper_dadr;
static int hf_bacnet_wrapper_snet;
static int hf_bacnet_wrapper_slen;
static int hf_bacnet_wrapper_sadr;
static int hf_bacnet_wrapper_auth_mech;
static int hf_bacnet_wrapper_auth_usr_id;
static int hf_bacnet_wrapper_auth_usr_role;
static int hf_bacnet_wrapper_auth_len;
static int hf_bacnet_wrapper_auth_data;
static int hf_bacnet_wrapper_signature;
static int hf_bacnet_wrapper_encrypted_data;
static int hf_bacnet_msg_is_challenged;
static int hf_bacnet_security_original_message_id;
static int hf_bacnet_security_original_time_stamp;
static int hf_bacnet_security_msg_len;
static int hf_bacnet_security_response_code;
static int hf_bacnet_security_response_expected_time_stamp;
static int hf_bacnet_security_response_key_algo;
static int hf_bacnet_security_response_key_id;
static int hf_bacnet_security_response_original_authentication_mech;
static int hf_bacnet_security_response_vendor_id;
static int hf_bacnet_security_response_key_revision;
static int hf_bacnet_security_response_number_keys;
static int hf_bacnet_security_set1_key_reveision;
static int hf_bacnet_security_set1_activation_time_stamp;
static int hf_bacnet_security_set1_expiration_time_stamp;
static int hf_bacnet_security_set1_key_algo;
static int hf_bacnet_security_set1_key_id;
static int hf_bacnet_security_set1_key_data;
static int hf_bacnet_security_set2_key_reveision;
static int hf_bacnet_security_set2_activation_time_stamp;
static int hf_bacnet_security_set2_expiration_time_stamp;
static int hf_bacnet_security_set2_key_algo;
static int hf_bacnet_security_set2_key_id;
static int hf_bacnet_security_set2_key_data;
static int hf_bacnet_security_dist_key_revision;
static int hf_bacnet_security_dist_key_algo;
static int hf_bacnet_security_dist_key_id;
static int hf_bacnet_security_dist_key_data;
static int hf_bacnet_security_master_key_algo;
static int hf_bacnet_security_master_key_id;
static int hf_bacnet_security_master_key_data;
static int hf_bacnet_update_control;
static int hf_bacnet_update_control_remove;
static int hf_bacnet_update_control_more_follows;
static int hf_bacnet_update_control_clear_set2;
static int hf_bacnet_update_control_set2_params_present;
static int hf_bacnet_update_control_set2_times_present;
static int hf_bacnet_update_control_clear_set1;
static int hf_bacnet_update_control_set1_params_present;
static int hf_bacnet_update_control_set1_times_present;
static int ett_bacnet;
static int ett_bacnet_control;
static int ett_bacnet_wrapper_control;
static int ett_bacnet_update_control;
static dissector_handle_t bacnet_handle;
static int * const 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 int * const 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 int * const 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,
int offset, bool *pis_net_msg_flg)
{
uint8_t bacnet_dlen;
uint8_t bacnet_wrapper_control;
uint16_t bacnet_len;
int len;
/* get control octet from wrapper */
bacnet_wrapper_control = tvb_get_uint8(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_uint8(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_uint8(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_uint8(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_uint16(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
// NOLINTNEXTLINE(misc-no-recursion)
dissect_bacnet_npdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset)
{
proto_item *ti;
proto_tree *bacnet_tree;
uint8_t bacnet_version;
uint8_t bacnet_control;
uint8_t bacnet_update_control;
uint8_t bacnet_dlen;
uint8_t bacnet_slen;
uint8_t bacnet_mesgtyp;
uint8_t bacnet_rportnum;
uint8_t bacnet_pinfolen;
uint8_t i;
tvbuff_t *next_tvb;
uint32_t 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_uint8(tvb, offset);
bacnet_control = tvb_get_uint8(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_uint8(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_uint8(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_uint8(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_uint8(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_uint8(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_challenged,
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:
{
bool is_net_msg_flg;
uint16_t 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_uint16(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 */
increment_dissection_depth(pinfo);
int npdu_len = dissect_bacnet_npdu(tvb, pinfo, tree, offset);
decrement_dissection_depth(pinfo);
return npdu_len;
}
/* 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:
{
uint8_t 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_uint8(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_uint8(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_uint8(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) {
uint8_t keycount;
keycount = tvb_get_uint8(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_uint8(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) {
uint8_t keycount;
keycount = tvb_get_uint8(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_uint8(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_uint8(tvb, offset);
offset++;
proto_tree_add_item(tree,
hf_bacnet_security_dist_key_data, tvb, offset,
bacnet_dlen, ENC_NA);
offset += bacnet_dlen;
break;