forked from the-tcpdump-group/tcpdump
-
Notifications
You must be signed in to change notification settings - Fork 2
/
print-802_15_4.c
2532 lines (2389 loc) · 63.8 KB
/
print-802_15_4.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
/*
* Copyright (c) 2009
* Siemens AG, All rights reserved.
* Dmitry Eremin-Solenikov ([email protected])
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/* \summary: IEEE 802.15.4 printer */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "netdissect-stdinc.h"
#include "netdissect.h"
#include "addrtoname.h"
#include "extract.h"
#define CHECK_BIT(num,bit) (((num) >> (bit)) & 0x1)
#define BROKEN_6TISCH_PAN_ID_COMPRESSION 0
/* Frame types from Table 7-1 of 802.15.4-2015 */
static const char *ftypes[] = {
"Beacon", /* 0 */
"Data", /* 1 */
"ACK", /* 2 */
"Command", /* 3 */
"Reserved", /* 4 */
"Multipurpose", /* 5 */
"Fragment", /* 6 */
"Extended" /* 7 */
};
/* Element IDs for Header IEs from Table 7-7 of 802.15.4-2015 */
static const char *h_ie_names[] = {
"Vendor Specific Header IE", /* 0x00 */
"Reserved 0x01", /* 0x01 */
"Reserved 0x02", /* 0x02 */
"Reserved 0x03", /* 0x03 */
"Reserved 0x04", /* 0x04 */
"Reserved 0x05", /* 0x05 */
"Reserved 0x06", /* 0x06 */
"Reserved 0x07", /* 0x07 */
"Reserved 0x08", /* 0x08 */
"Reserved 0x09", /* 0x09 */
"Reserved 0x0a", /* 0x0a */
"Reserved 0x0b", /* 0x0b */
"Reserved 0x0c", /* 0x0c */
"Reserved 0x0d", /* 0x0d */
"Reserved 0x0e", /* 0x0e */
"Reserved 0x0f", /* 0x0f */
"Reserved 0x10", /* 0x10 */
"Reserved 0x11", /* 0x11 */
"Reserved 0x12", /* 0x12 */
"Reserved 0x13", /* 0x13 */
"Reserved 0x14", /* 0x14 */
"Reserved 0x15", /* 0x15 */
"Reserved 0x16", /* 0x16 */
"Reserved 0x17", /* 0x17 */
"Reserved 0x18", /* 0x18 */
"Reserved 0x19", /* 0x19 */
"LE CSL IE", /* 0x1a */
"LE RIT IE", /* 0x1b */
"DSME PAN descriptor IE", /* 0x1c */
"Rendezvous Time IE", /* 0x1d */
"Time Correction IE", /* 0x1e */
"Reserved 0x1f", /* 0x1f */
"Reserved 0x20", /* 0x20 */
"Extended DSME PAN descriptor IE", /* 0x21 */
"Fragment Sequence Context Description IE", /* 0x22 */
"Simplified Superframe Specification IE", /* 0x23 */
"Simplified GTS Specification IE", /* 0x24 */
"LECIM Capabilities IE", /* 0x25 */
"TRLE Descriptor IE", /* 0x26 */
"RCC Capabilities IE", /* 0x27 */
"RCCN Descriptor IE", /* 0x28 */
"Global Time IE", /* 0x29 */
"Omnibus Header IE", /* 0x2a */
"DA IE", /* 0x2b */
"Reserved 0x2c", /* 0x2c */
"Reserved 0x2d", /* 0x2d */
"Reserved 0x2e", /* 0x2e */
"Reserved 0x2f", /* 0x2f */
"Reserved 0x30", /* 0x30 */
"Reserved 0x31", /* 0x31 */
"Reserved 0x32", /* 0x32 */
"Reserved 0x33", /* 0x33 */
"Reserved 0x34", /* 0x34 */
"Reserved 0x35", /* 0x35 */
"Reserved 0x36", /* 0x36 */
"Reserved 0x37", /* 0x37 */
"Reserved 0x38", /* 0x38 */
"Reserved 0x39", /* 0x39 */
"Reserved 0x3a", /* 0x3a */
"Reserved 0x3b", /* 0x3b */
"Reserved 0x3c", /* 0x3c */
"Reserved 0x3d", /* 0x3d */
"Reserved 0x3e", /* 0x3e */
"Reserved 0x3f", /* 0x3f */
"Reserved 0x40", /* 0x40 */
"Reserved 0x41", /* 0x41 */
"Reserved 0x42", /* 0x42 */
"Reserved 0x43", /* 0x43 */
"Reserved 0x44", /* 0x44 */
"Reserved 0x45", /* 0x45 */
"Reserved 0x46", /* 0x46 */
"Reserved 0x47", /* 0x47 */
"Reserved 0x48", /* 0x48 */
"Reserved 0x49", /* 0x49 */
"Reserved 0x4a", /* 0x4a */
"Reserved 0x4b", /* 0x4b */
"Reserved 0x4c", /* 0x4c */
"Reserved 0x4d", /* 0x4d */
"Reserved 0x4e", /* 0x4e */
"Reserved 0x4f", /* 0x4f */
"Reserved 0x50", /* 0x50 */
"Reserved 0x51", /* 0x51 */
"Reserved 0x52", /* 0x52 */
"Reserved 0x53", /* 0x53 */
"Reserved 0x54", /* 0x54 */
"Reserved 0x55", /* 0x55 */
"Reserved 0x56", /* 0x56 */
"Reserved 0x57", /* 0x57 */
"Reserved 0x58", /* 0x58 */
"Reserved 0x59", /* 0x59 */
"Reserved 0x5a", /* 0x5a */
"Reserved 0x5b", /* 0x5b */
"Reserved 0x5c", /* 0x5c */
"Reserved 0x5d", /* 0x5d */
"Reserved 0x5e", /* 0x5e */
"Reserved 0x5f", /* 0x5f */
"Reserved 0x60", /* 0x60 */
"Reserved 0x61", /* 0x61 */
"Reserved 0x62", /* 0x62 */
"Reserved 0x63", /* 0x63 */
"Reserved 0x64", /* 0x64 */
"Reserved 0x65", /* 0x65 */
"Reserved 0x66", /* 0x66 */
"Reserved 0x67", /* 0x67 */
"Reserved 0x68", /* 0x68 */
"Reserved 0x69", /* 0x69 */
"Reserved 0x6a", /* 0x6a */
"Reserved 0x6b", /* 0x6b */
"Reserved 0x6c", /* 0x6c */
"Reserved 0x6d", /* 0x6d */
"Reserved 0x6e", /* 0x6e */
"Reserved 0x6f", /* 0x6f */
"Reserved 0x70", /* 0x70 */
"Reserved 0x71", /* 0x71 */
"Reserved 0x72", /* 0x72 */
"Reserved 0x73", /* 0x73 */
"Reserved 0x74", /* 0x74 */
"Reserved 0x75", /* 0x75 */
"Reserved 0x76", /* 0x76 */
"Reserved 0x77", /* 0x77 */
"Reserved 0x78", /* 0x78 */
"Reserved 0x79", /* 0x79 */
"Reserved 0x7a", /* 0x7a */
"Reserved 0x7b", /* 0x7b */
"Reserved 0x7c", /* 0x7c */
"Reserved 0x7d", /* 0x7d */
"Header Termination 1 IE", /* 0x7e */
"Header Termination 2 IE" /* 0x7f */
};
/* Payload IE Group IDs from Table 7-15 of 802.15.4-2015 */
static const char *p_ie_names[] = {
"ESDU IE", /* 0x00 */
"MLME IE", /* 0x01 */
"Vendor Specific Nested IE", /* 0x02 */
"Multiplexed IE (802.15.9)", /* 0x03 */
"Omnibus Payload Group IE", /* 0x04 */
"IETF IE", /* 0x05 */
"Reserved 0x06", /* 0x06 */
"Reserved 0x07", /* 0x07 */
"Reserved 0x08", /* 0x08 */
"Reserved 0x09", /* 0x09 */
"Reserved 0x0a", /* 0x0a */
"Reserved 0x0b", /* 0x0b */
"Reserved 0x0c", /* 0x0c */
"Reserved 0x0d", /* 0x0d */
"Reserved 0x0e", /* 0x0e */
"List termination" /* 0x0f */
};
/* Sub-ID for short format from Table 7-16 of 802.15.4-2015 */
static const char *p_mlme_short_names[] = {
"Reserved for long format 0x0", /* 0x00 */
"Reserved for long format 0x1", /* 0x01 */
"Reserved for long format 0x2", /* 0x02 */
"Reserved for long format 0x3", /* 0x03 */
"Reserved for long format 0x4", /* 0x04 */
"Reserved for long format 0x5", /* 0x05 */
"Reserved for long format 0x6", /* 0x06 */
"Reserved for long format 0x7", /* 0x07 */
"Reserved for long format 0x8", /* 0x08 */
"Reserved for long format 0x9", /* 0x09 */
"Reserved for long format 0xa", /* 0x0a */
"Reserved for long format 0xb", /* 0x0b */
"Reserved for long format 0xc", /* 0x0c */
"Reserved for long format 0xd", /* 0x0d */
"Reserved for long format 0xe", /* 0x0e */
"Reserved for long format 0xf", /* 0x0f */
"Reserved 0x10", /* 0x10 */
"Reserved 0x11", /* 0x11 */
"Reserved 0x12", /* 0x12 */
"Reserved 0x13", /* 0x13 */
"Reserved 0x14", /* 0x14 */
"Reserved 0x15", /* 0x15 */
"Reserved 0x16", /* 0x16 */
"Reserved 0x17", /* 0x17 */
"Reserved 0x18", /* 0x18 */
"Reserved 0x19", /* 0x19 */
"TSCH Synchronization IE", /* 0x1a */
"TSCH Slotframe and Link IE", /* 0x1b */
"TSCH Timeslot IE", /* 0x1c */
"Hopping timing IE", /* 0x1d */
"Enhanced Beacon Filter IE", /* 0x1e */
"MAC Metrics IE", /* 0x1f */
"All MAC Metrics IE", /* 0x20 */
"Coexistence Specification IE", /* 0x21 */
"SUN Device Capabilities IE", /* 0x22 */
"SUN FSK Generic PHY IE", /* 0x23 */
"Mode Switch Parameter IE", /* 0x24 */
"PHY Parameter Change IE", /* 0x25 */
"O-QPSK PHY Mode IE", /* 0x26 */
"PCA Allocation IE", /* 0x27 */
"LECIM DSSS Operating Mode IE", /* 0x28 */
"LECIM FSK Operating Mode IE", /* 0x29 */
"Reserved 0x2a", /* 0x2a */
"TVWS PHY Operating Mode Description IE", /* 0x2b */
"TVWS Device Capabilities IE", /* 0x2c */
"TVWS Device Category IE", /* 0x2d */
"TVWS Device Identiication IE", /* 0x2e */
"TVWS Device Location IE", /* 0x2f */
"TVWS Channel Information Query IE", /* 0x30 */
"TVWS Channel Information Source IE", /* 0x31 */
"CTM IE", /* 0x32 */
"Timestamp IE", /* 0x33 */
"Timestamp Difference IE", /* 0x34 */
"TMCTP Sepcification IE", /* 0x35 */
"RCC PHY Operating Mode IE", /* 0x36 */
"Reserved 0x37", /* 0x37 */
"Reserved 0x38", /* 0x38 */
"Reserved 0x39", /* 0x39 */
"Reserved 0x3a", /* 0x3a */
"Reserved 0x3b", /* 0x3b */
"Reserved 0x3c", /* 0x3c */
"Reserved 0x3d", /* 0x3d */
"Reserved 0x3e", /* 0x3e */
"Reserved 0x3f", /* 0x3f */
"Reserved 0x40", /* 0x40 */
"Reserved 0x41", /* 0x41 */
"Reserved 0x42", /* 0x42 */
"Reserved 0x43", /* 0x43 */
"Reserved 0x44", /* 0x44 */
"Reserved 0x45", /* 0x45 */
"Reserved 0x46", /* 0x46 */
"Reserved 0x47", /* 0x47 */
"Reserved 0x48", /* 0x48 */
"Reserved 0x49", /* 0x49 */
"Reserved 0x4a", /* 0x4a */
"Reserved 0x4b", /* 0x4b */
"Reserved 0x4c", /* 0x4c */
"Reserved 0x4d", /* 0x4d */
"Reserved 0x4e", /* 0x4e */
"Reserved 0x4f", /* 0x4f */
"Reserved 0x50", /* 0x50 */
"Reserved 0x51", /* 0x51 */
"Reserved 0x52", /* 0x52 */
"Reserved 0x53", /* 0x53 */
"Reserved 0x54", /* 0x54 */
"Reserved 0x55", /* 0x55 */
"Reserved 0x56", /* 0x56 */
"Reserved 0x57", /* 0x57 */
"Reserved 0x58", /* 0x58 */
"Reserved 0x59", /* 0x59 */
"Reserved 0x5a", /* 0x5a */
"Reserved 0x5b", /* 0x5b */
"Reserved 0x5c", /* 0x5c */
"Reserved 0x5d", /* 0x5d */
"Reserved 0x5e", /* 0x5e */
"Reserved 0x5f", /* 0x5f */
"Reserved 0x60", /* 0x60 */
"Reserved 0x61", /* 0x61 */
"Reserved 0x62", /* 0x62 */
"Reserved 0x63", /* 0x63 */
"Reserved 0x64", /* 0x64 */
"Reserved 0x65", /* 0x65 */
"Reserved 0x66", /* 0x66 */
"Reserved 0x67", /* 0x67 */
"Reserved 0x68", /* 0x68 */
"Reserved 0x69", /* 0x69 */
"Reserved 0x6a", /* 0x6a */
"Reserved 0x6b", /* 0x6b */
"Reserved 0x6c", /* 0x6c */
"Reserved 0x6d", /* 0x6d */
"Reserved 0x6e", /* 0x6e */
"Reserved 0x6f", /* 0x6f */
"Reserved 0x70", /* 0x70 */
"Reserved 0x71", /* 0x71 */
"Reserved 0x72", /* 0x72 */
"Reserved 0x73", /* 0x73 */
"Reserved 0x74", /* 0x74 */
"Reserved 0x75", /* 0x75 */
"Reserved 0x76", /* 0x76 */
"Reserved 0x77", /* 0x77 */
"Reserved 0x78", /* 0x78 */
"Reserved 0x79", /* 0x79 */
"Reserved 0x7a", /* 0x7a */
"Reserved 0x7b", /* 0x7b */
"Reserved 0x7c", /* 0x7c */
"Reserved 0x7d", /* 0x7d */
"Reserved 0x7e", /* 0x7e */
"Reserved 0x7f" /* 0x7f */
};
/* Sub-ID for long format from Table 7-17 of 802.15.4-2015 */
static const char *p_mlme_long_names[] = {
"Reserved 0x00", /* 0x00 */
"Reserved 0x01", /* 0x01 */
"Reserved 0x02", /* 0x02 */
"Reserved 0x03", /* 0x03 */
"Reserved 0x04", /* 0x04 */
"Reserved 0x05", /* 0x05 */
"Reserved 0x06", /* 0x06 */
"Reserved 0x07", /* 0x07 */
"Vendor Specific MLME Nested IE", /* 0x08 */
"Channel Hopping IE", /* 0x09 */
"Reserved 0x0a", /* 0x0a */
"Reserved 0x0b", /* 0x0b */
"Reserved 0x0c", /* 0x0c */
"Reserved 0x0d", /* 0x0d */
"Reserved 0x0e", /* 0x0e */
"Reserved 0x0f" /* 0x0f */
};
/* MAC commands from Table 7-49 of 802.15.4-2015 */
static const char *mac_c_names[] = {
"Reserved 0x00", /* 0x00 */
"Association Request command", /* 0x01 */
"Association Response command", /* 0x02 */
"Disassociation Notification command", /* 0x03 */
"Data Request command", /* 0x04 */
"PAN ID Conflict Notification command", /* 0x05 */
"Orphan Notification command", /* 0x06 */
"Beacon Request command", /* 0x07 */
"Coordinator realignment command", /* 0x08 */
"GTS request command", /* 0x09 */
"TRLE Management Request command", /* 0x0a */
"TRLE Management Response command", /* 0x0b */
"Reserved 0x0c", /* 0x0c */
"Reserved 0x0d", /* 0x0d */
"Reserved 0x0e", /* 0x0e */
"Reserved 0x0f", /* 0x0f */
"Reserved 0x10", /* 0x10 */
"Reserved 0x11", /* 0x11 */
"Reserved 0x12", /* 0x12 */
"DSME Association Request command", /* 0x13 */
"DSME Association Response command", /* 0x14 */
"DSME GTS Request command", /* 0x15 */
"DSME GTS Response command", /* 0x16 */
"DSME GTS Notify command", /* 0x17 */
"DSME Information Request command", /* 0x18 */
"DSME Information Response command", /* 0x19 */
"DSME Beacon Allocation Notification command", /* 0x1a */
"DSME Beacon Collision Notification command", /* 0x1b */
"DSME Link Report command", /* 0x1c */
"Reserved 0x1d", /* 0x1d */
"Reserved 0x1e", /* 0x1e */
"Reserved 0x1f", /* 0x1f */
"RIT Data Request command", /* 0x20 */
"DBS Request command", /* 0x21 */
"DBS Response command", /* 0x22 */
"RIT Data Response command", /* 0x23 */
"Vendor Specific command", /* 0x24 */
"Reserved 0x25", /* 0x25 */
"Reserved 0x26", /* 0x26 */
"Reserved 0x27", /* 0x27 */
"Reserved 0x28", /* 0x28 */
"Reserved 0x29", /* 0x29 */
"Reserved 0x2a", /* 0x2a */
"Reserved 0x2b", /* 0x2b */
"Reserved 0x2c", /* 0x2c */
"Reserved 0x2d", /* 0x2d */
"Reserved 0x2e", /* 0x2e */
"Reserved 0x2f" /* 0x2f */
};
/*
* Frame Control subfields.
*/
#define FC_FRAME_TYPE(fc) ((fc) & 0x7)
#define FC_FRAME_VERSION(fc) (((fc) >> 12) & 0x3)
#define FC_ADDRESSING_MODE_NONE 0x00
#define FC_ADDRESSING_MODE_RESERVED 0x01
#define FC_ADDRESSING_MODE_SHORT 0x02
#define FC_ADDRESSING_MODE_LONG 0x03
/*
* IEEE 802.15.4 CRC 16 function. This is using CCITT polynomical of 0x1021,
* but the initial value is 0, and the bits are reversed for both in and out.
* See secton 7.2.10 of 802.15.4-2015 for more information.
*/
static uint16_t
ieee802_15_4_crc16(const u_char *p,
u_int data_len)
{
uint16_t crc;
u_char x, y;
crc = 0x0000; /* Note, initial value is 0x0000 not 0xffff. */
while (data_len != 0){
y = *p++;
/* Reverse bits on input */
y = (((y & 0xaa) >> 1) | ((y & 0x55) << 1));
y = (((y & 0xcc) >> 2) | ((y & 0x33) << 2));
y = (((y & 0xf0) >> 4) | ((y & 0x0f) << 4));
/* Update CRC */
x = crc >> 8 ^ y;
x ^= x >> 4;
crc = ((uint16_t)(crc << 8)) ^
((uint16_t)(x << 12)) ^
((uint16_t)(x << 5)) ^
((uint16_t)x);
data_len--;
}
/* Reverse bits on output */
crc = (((crc & 0xaaaa) >> 1) | ((crc & 0x5555) << 1));
crc = (((crc & 0xcccc) >> 2) | ((crc & 0x3333) << 2));
crc = (((crc & 0xf0f0) >> 4) | ((crc & 0x0f0f) << 4));
crc = (((crc & 0xff00) >> 8) | ((crc & 0x00ff) << 8));
return crc;
}
/*
* Reverses the bits of the 32-bit word.
*/
static uint32_t
ieee802_15_4_reverse32(uint32_t x)
{
x = ((x & 0x55555555) << 1) | ((x >> 1) & 0x55555555);
x = ((x & 0x33333333) << 2) | ((x >> 2) & 0x33333333);
x = ((x & 0x0F0F0F0F) << 4) | ((x >> 4) & 0x0F0F0F0F);
x = (x << 24) | ((x & 0xFF00) << 8) |
((x >> 8) & 0xFF00) | (x >> 24);
return x;
}
/*
* IEEE 802.15.4 CRC 32 function. This is using ANSI X3.66-1979 polynomical of
* 0x04C11DB7, but the initial value is 0, and the bits are reversed for both
* in and out. See secton 7.2.10 of 802.15.4-2015 for more information.
*/
static uint32_t
ieee802_15_4_crc32(const u_char *p,
u_int data_len)
{
uint32_t crc, byte;
int b;
crc = 0x00000000; /* Note, initial value is 0x00000000 not 0xffffffff */
while (data_len != 0){
byte = *p++;
/* Reverse bits on input */
byte = ieee802_15_4_reverse32(byte);
/* Update CRC */
for(b = 0; b <= 7; b++) {
if ((int) (crc ^ byte) < 0)
crc = (crc << 1) ^ 0x04C11DB7;
else
crc = crc << 1;
byte = byte << 1;
}
data_len--;
}
/* Reverse bits on output */
crc = ieee802_15_4_reverse32(crc);
return crc;
}
/*
* Find out the address length based on the address type. See table 7-3 of
* 802.15.4-2015. Returns the address length.
*/
static int
ieee802_15_4_addr_len(uint16_t addr_type)
{
switch (addr_type) {
case FC_ADDRESSING_MODE_NONE: /* None. */
return 0;
break;
case FC_ADDRESSING_MODE_RESERVED: /* Reserved, there used to be 8-bit
* address type in one amendment, but
* that and the feature using it was
* removed during 802.15.4-2015
* maintenance process. */
return -1;
break;
case FC_ADDRESSING_MODE_SHORT: /* Short. */
return 2;
break;
case FC_ADDRESSING_MODE_LONG: /* Extended. */
return 8;
break;
}
return 0;
}
/*
* Print out the ieee 802.15.4 address.
*/
static void
ieee802_15_4_print_addr(netdissect_options *ndo, const u_char *p,
int dst_addr_len)
{
switch (dst_addr_len) {
case 0:
ND_PRINT("none");
break;
case 2:
ND_PRINT("%04x", GET_LE_U_2(p));
break;
case 8:
ND_PRINT("%s", GET_LE64ADDR_STRING(p));
break;
}
return;
}
/*
* Beacon frame superframe specification structure. Used in the old Beacon
* frames, and in the DSME PAN Descriptor IE. See section 7.3.1.3 of the
* 802.15.4-2015.
*/
static void
ieee802_15_4_print_superframe_specification(netdissect_options *ndo,
uint16_t ss)
{
if (ndo->ndo_vflag < 1) {
return;
}
ND_PRINT("\n\tBeacon order = %d, Superframe order = %d, ",
(ss & 0xf), ((ss >> 4) & 0xf));
ND_PRINT("Final CAP Slot = %d",
((ss >> 8) & 0xf));
if (CHECK_BIT(ss, 12)) { ND_PRINT(", BLE enabled"); }
if (CHECK_BIT(ss, 14)) { ND_PRINT(", PAN Coordinator"); }
if (CHECK_BIT(ss, 15)) { ND_PRINT(", Assocation Permit"); }
}
/*
* Beacon frame gts info structure. Used in the old Beacon frames, and
* in the DSME PAN Descriptor IE. See section 7.3.1.4 of 802.15.4-2015.
*
* Returns number of byts consumed from the packet or -1 in case of error.
*/
static int
ieee802_15_4_print_gts_info(netdissect_options *ndo,
const u_char *p,
u_int data_len)
{
uint8_t gts_spec, gts_cnt;
u_int len;
int i;
gts_spec = GET_U_1(p);
gts_cnt = gts_spec & 0x7;
if (gts_cnt == 0) {
if (ndo->ndo_vflag > 0) {
ND_PRINT("\n\tGTS Descriptor Count = %d, ", gts_cnt);
}
return 1;
}
len = 1 + 1 + gts_cnt * 3;
if (data_len < len) {
ND_PRINT(" [ERROR: Truncated GTS Info List]");
return -1;
}
if (ndo->ndo_vflag < 2) {
return len;
}
ND_PRINT("GTS Descriptor Count = %d, ", gts_cnt);
ND_PRINT("GTS Directions Mask = %02x, [ ",
GET_U_1(p + 1) & 0x7f);
for(i = 0; i < gts_cnt; i++) {
ND_PRINT("[ ");
ieee802_15_4_print_addr(ndo, p + 2 + i * 3, 2);
ND_PRINT(", Start slot = %d, Length = %d ] ",
GET_U_1(p + 2 + i * 3 + 1) & 0x0f,
(GET_U_1(p + 2 + i * 3 + 1) >> 4) & 0x0f);
}
ND_PRINT("]");
return len;
}
/*
* Beacon frame pending address structure. Used in the old Beacon frames, and
* in the DSME PAN Descriptor IE. See section 7.3.1.5 of 802.15.4-2015.
*
* Returns number of byts consumed from the packet or -1 in case of error.
*/
static int16_t
ieee802_15_4_print_pending_addresses(netdissect_options *ndo,
const u_char *p,
u_int data_len)
{
uint8_t pas, s_cnt, e_cnt, len, i;
pas = GET_U_1(p);
s_cnt = pas & 0x7;
e_cnt = (pas >> 4) & 0x7;
len = 1 + s_cnt * 2 + e_cnt * 8;
if (ndo->ndo_vflag > 0) {
ND_PRINT("\n\tPending address list, "
"# short addresses = %d, # extended addresses = %d",
s_cnt, e_cnt);
}
if (data_len < len) {
ND_PRINT(" [ERROR: Pending address list truncated]");
return -1;
}
if (ndo->ndo_vflag < 2) {
return len;
}
if (s_cnt != 0) {
ND_PRINT(", Short address list = [ ");
for(i = 0; i < s_cnt; i++) {
ieee802_15_4_print_addr(ndo, p + 1 + i * 2, 2);
ND_PRINT(" ");
}
ND_PRINT("]");
}
if (s_cnt != 0) {
ND_PRINT(", Extended address list = [ ");
for(i = 0; i < e_cnt; i++) {
ieee802_15_4_print_addr(ndo, p + 1 + s_cnt * 2 +
e_cnt * 8, 8);
ND_PRINT(" ");
}
ND_PRINT("]");
}
return len;
}
/*
* Print header ie content.
*/
static void
ieee802_15_4_print_header_ie(netdissect_options *ndo,
const u_char *p,
uint16_t ie_len,
int element_id)
{
int i;
switch (element_id) {
case 0x00: /* Vendor Specific Header IE */
if (ie_len < 3) {
ND_PRINT("[ERROR: Vendor OUI missing]");
} else {
ND_PRINT("OUI = 0x%02x%02x%02x, ", GET_U_1(p),
GET_U_1(p + 1), GET_U_1(p + 2));
ND_PRINT("Data = ");
for(i = 3; i < ie_len; i++) {
ND_PRINT("%02x ", GET_U_1(p + i));
}
}
break;
case 0x1a: /* LE CSL IE */
if (ie_len < 4) {
ND_PRINT("[ERROR: Truncated CSL IE]");
} else {
ND_PRINT("CSL Phase = %d, CSL Period = %d",
GET_LE_U_2(p), GET_LE_U_2(p + 2));
if (ie_len >= 6) {
ND_PRINT(", Rendezvous time = %d",
GET_LE_U_2(p + 4));
}
if (ie_len != 4 && ie_len != 6) {
ND_PRINT(" [ERROR: CSL IE length wrong]");
}
}
break;
case 0x1b: /* LE RIT IE */
if (ie_len < 4) {
ND_PRINT("[ERROR: Truncated RIT IE]");
} else {
ND_PRINT("Time to First Listen = %d, # of Repeat Listen = %d, Repeat Listen Interval = %d",
GET_U_1(p),
GET_U_1(p + 1),
GET_LE_U_2(p + 2));
}
break;
case 0x1c: /* DSME PAN Descriptor IE */
/*FALLTHROUGH*/
case 0x21: /* Extended DSME PAN descriptior IE */
if (ie_len < 2) {
ND_PRINT("[ERROR: Truncated DSME PAN IE]");
} else {
uint16_t ss, ptr, ulen;
int16_t len;
int hopping_present;
hopping_present = 0;
ss = GET_LE_U_2(p);
ieee802_15_4_print_superframe_specification(ndo, ss);
if (ie_len < 3) {
ND_PRINT("[ERROR: Truncated before pending addresses field]");
break;
}
ptr = 2;
len = ieee802_15_4_print_pending_addresses(ndo,
p + ptr,
ie_len -
ptr);
if (len < 0) {
break;
}
ptr += len;
if (element_id == 0x21) {
/* Extended version. */
if (ie_len < ptr + 2) {
ND_PRINT("[ERROR: Truncated before DSME Superframe Specification]");
break;
}
ss = GET_LE_U_2(p + ptr);
ptr += 2;
ND_PRINT("Multi-superframe Order = %d", ss & 0xff);
ND_PRINT(", %s", ((ss & 0x100) ?
"Channel hopping mode" :
"Channel adaptation mode"));
if (ss & 0x400) {
ND_PRINT(", CAP reduction enabled");
}
if (ss & 0x800) {
ND_PRINT(", Deferred beacon enabled");
}
if (ss & 0x1000) {
ND_PRINT(", Hopping Sequence Present");
hopping_present = 1;
}
} else {
if (ie_len < ptr + 1) {
ND_PRINT("[ERROR: Truncated before DSME Superframe Specification]");
break;
}
ss = GET_U_1(p + ptr);
ptr++;
ND_PRINT("Multi-superframe Order = %d",
ss & 0x0f);
ND_PRINT(", %s", ((ss & 0x10) ?
"Channel hopping mode" :
"Channel adaptation mode"));
if (ss & 0x40) {
ND_PRINT(", CAP reduction enabled");
}
if (ss & 0x80) {
ND_PRINT(", Deferred beacon enabled");
}
}
if (ie_len < ptr + 8) {
ND_PRINT(" [ERROR: Truncated before Time syncronization specification]");
break;
}
ND_PRINT("Beacon timestamp = %" PRIu64 ", offset = %d",
GET_LE_U_6(p + ptr),
GET_LE_U_2(p + ptr + 6));
ptr += 8;
if (ie_len < ptr + 4) {
ND_PRINT(" [ERROR: Truncated before Beacon Bitmap]");
break;
}
ulen = GET_LE_U_2(p + ptr + 2);
ND_PRINT("SD Index = %d, Bitmap len = %d, ",
GET_LE_U_2(p + ptr), ulen);
ptr += 4;
if (ie_len < ptr + ulen) {
ND_PRINT(" [ERROR: Truncated in SD bitmap]");
break;
}
ND_PRINT(" SD Bitmap = ");
for(i = 0; i < ulen; i++) {
ND_PRINT("%02x ", GET_U_1(p + ptr + i));
}
ptr += ulen;
if (ie_len < ptr + 5) {
ND_PRINT(" [ERROR: Truncated before Channel hopping specification]");
break;
}
ulen = GET_LE_U_2(p + ptr + 4);
ND_PRINT("Hopping Seq ID = %d, PAN Coordinator BSN = %d, "
"Channel offset = %d, Bitmap length = %d, ",
GET_U_1(p + ptr),
GET_U_1(p + ptr + 1),
GET_LE_U_2(p + ptr + 2),
ulen);
ptr += 5;
if (ie_len < ptr + ulen) {
ND_PRINT(" [ERROR: Truncated in Channel offset bitmap]");
break;
}
ND_PRINT(" Channel offset bitmap = ");
for(i = 0; i < ulen; i++) {
ND_PRINT("%02x ", GET_U_1(p + ptr + i));
}
ptr += ulen;
if (hopping_present) {
if (ie_len < ptr + 1) {
ND_PRINT(" [ERROR: Truncated in Hopping Sequence length]");
break;
}
ulen = GET_U_1(p + ptr);
ptr++;
ND_PRINT("Hopping Seq length = %d [ ", ulen);
/* The specification is not clear how the
hopping sequence is encoded, I assume two
octet unsigned integers for each channel. */
if (ie_len < ptr + ulen * 2) {
ND_PRINT(" [ERROR: Truncated in Channel offset bitmap]");
break;
}
for(i = 0; i < ulen; i++) {
ND_PRINT("%02x ",
GET_LE_U_2(p + ptr + i * 2));
}
ND_PRINT("]");
ptr += ulen * 2;
}
}
break;
case 0x1d: /* Rendezvous Tome IE */
if (ie_len != 4) {
ND_PRINT("[ERROR: Length != 2]");
} else {
uint16_t r_time, w_u_interval;
r_time = GET_LE_U_2(p);
w_u_interval = GET_LE_U_2(p + 2);
ND_PRINT("Rendezvous time = %d, Wake-up Interval = %d",
r_time, w_u_interval);
}
break;
case 0x1e: /* Time correction IE */
if (ie_len != 2) {
ND_PRINT("[ERROR: Length != 2]");
} else {
uint16_t val;
int16_t timecorr;
val = GET_LE_U_2(p);
if (val & 0x8000) { ND_PRINT("Negative "); }
val &= 0xfff;
val <<= 4;
timecorr = val;
timecorr >>= 4;
ND_PRINT("Ack time correction = %d, ", timecorr);
}
break;
case 0x22: /* Frament Sequence Content Description IE */
/* XXX Not implemented */
case 0x23: /* Simplified Superframe Specification IE */
/* XXX Not implemented */
case 0x24: /* Simplified GTS Specification IE */
/* XXX Not implemented */
case 0x25: /* LECIM Capabilities IE */
/* XXX Not implemented */
case 0x26: /* TRLE Descriptor IE */
/* XXX Not implemented */
case 0x27: /* RCC Capabilities IE */
/* XXX Not implemented */
case 0x28: /* RCCN Descriptor IE */
/* XXX Not implemented */
case 0x29: /* Global Time IE */
/* XXX Not implemented */
case 0x2b: /* DA IE */
/* XXX Not implemented */
default:
ND_PRINT("IE Data = ");
for(i = 0; i < ie_len; i++) {
ND_PRINT("%02x ", GET_U_1(p + i));
}
break;
}
}
/*
* Parse and print Header IE list. See 7.4.2 of 802.15.4-2015 for
* more information.
*
* Returns number of byts consumed from the packet or -1 in case of error.
*/
static int
ieee802_15_4_print_header_ie_list(netdissect_options *ndo,
const u_char *p,
u_int caplen,
int *payload_ie_present)
{
int len, ie, element_id, i;
uint16_t ie_len;
*payload_ie_present = 0;
len = 0;
do {
if (caplen < 2) {
ND_PRINT("[ERROR: Truncated header IE]");
return -1;
}
/* Extract IE Header */
ie = GET_LE_U_2(p);
if (CHECK_BIT(ie, 15)) {
ND_PRINT("[ERROR: Header IE with type 1] ");
}
/* Get length and Element ID */
ie_len = ie & 0x7f;
element_id = (ie >> 7) & 0xff;
if (element_id > 127) {
ND_PRINT("Reserved Element ID %02x, length = %d ",
element_id, ie_len);
} else {
if (ie_len == 0) {
ND_PRINT("\n\t%s [", h_ie_names[element_id]);
} else {
ND_PRINT("\n\t%s [ length = %d, ",
h_ie_names[element_id], ie_len);
}
}
if (caplen < ie_len) {
ND_PRINT("[ERROR: Truncated IE data]");
return -1;
}
/* Skip header */
p += 2;
/* Parse and print content. */
if (ndo->ndo_vflag > 3 && ie_len != 0) {
ieee802_15_4_print_header_ie(ndo, p,
ie_len, element_id);
} else {
if (ie_len != 0) {
ND_PRINT("IE Data = ");
for(i = 0; i < ie_len; i++) {
ND_PRINT("%02x ", GET_U_1(p + i));
}
}
}
ND_PRINT("] ");
len += 2 + ie_len;
p += ie_len;
caplen -= 2 + ie_len;
if (element_id == 0x7e) {
*payload_ie_present = 1;
break;
}
if (element_id == 0x7f) {
break;
}
} while (caplen > 0);
return len;
}
/*
* Print MLME ie content.
*/
static void
ieee802_15_4_print_mlme_ie(netdissect_options *ndo,
const u_char *p,
uint16_t sub_ie_len,