summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-6lowpan.c
blob: 9544fbe647f84d66e6c6445f6cc0634d49ce3a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
/* packet-6lowpan.c
 *
 * Add Selective Fragment Recovery per
 * https://tools.ietf.org/html/draft-ietf-6lo-fragment-recovery-02
 * By James Ko <jck@exegin.com>
 * Copyright 2019 Exegin Technologies Limited
 *
 * Routines for 6LoWPAN packet disassembly
 * By Owen Kirby <osk@exegin.com>
 * Copyright 2009 Owen Kirby
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
#include "config.h"

#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/expert.h>
#include <epan/reassemble.h>
#include <epan/ipproto.h>
#include <epan/addr_resolv.h>
#include <epan/proto_data.h>
#include <epan/etypes.h>
#include <epan/tfs.h>
#include "packet-6lowpan.h"
#include "packet-btl2cap.h"
#include "packet-ipv6.h"
#include "packet-zbee.h"

void proto_register_6lowpan(void);
void proto_reg_handoff_6lowpan(void);

/* Definitions for 6lowpan packet disassembly structures and routines */

/* 6LoWPAN Patterns */
#define LOWPAN_PATTERN_NALP             0x00
#define LOWPAN_PATTERN_NALP_BITS        2
#define LOWPAN_PATTERN_IPV6             0x41
#define LOWPAN_PATTERN_IPV6_BITS        8
#define LOWPAN_PATTERN_HC1              0x42    /* Deprecated - replaced with IPHC. */
#define LOWPAN_PATTERN_HC1_BITS         8
#define LOWPAN_PATTERN_BC0              0x50
#define LOWPAN_PATTERN_BC0_BITS         8
#define LOWPAN_PATTERN_IPHC             0x03    /* See draft-ietf-6lowpan-hc-15.txt */
#define LOWPAN_PATTERN_IPHC_BITS        3
#define LOWPAN_PATTERN_ESC              0x7f
#define LOWPAN_PATTERN_ESC_BITS         8
#define LOWPAN_PATTERN_MESH             0x02
#define LOWPAN_PATTERN_MESH_BITS        2
#define LOWPAN_PATTERN_FRAG1            0x18
#define LOWPAN_PATTERN_FRAGN            0x1c
#define LOWPAN_PATTERN_FRAG_BITS        5
#define LOWPAN_PATTERN_RFRAG            0x74
#define LOWPAN_PATTERN_RFRAG_ACK        0x75
#define LOWPAN_PATTERN_RFRAG_BITS       7

#define LOWPAN_RFRAG_SEQUENCE_BITS      5
#define LOWPAN_RFRAG_FRAG_SZ_BITS      10

/* RFC8025 and RFC8138 */
#define LOWPAN_PATTERN_PAGING_DISPATCH          0xf
#define LOWPAN_PATTERN_PAGING_DISPATCH_BITS     4
#define LOWPAN_PATTERN_6LORHC                   0x04
#define LOWPAN_PATTERN_6LORHE                   0x05
#define LOWPAN_PATTERN_6LORHE_CLASS             0xe000
#define LOWPAN_PATTERN_6LORHE_CLASS_BITS        13
#define LOWPAN_PATTERN_6LORHE_LENGTH            0x1f00
#define LOWPAN_PATTERN_6LORHE_LENGTH_BITS       8
#define LOWPAN_PATTERN_6LORHE_TYPE              0x00ff
#define LOWPAN_PATTERN_6LORH_TYPE0              0x00
#define LOWPAN_PATTERN_6LORH_TYPE1              0x01
#define LOWPAN_PATTERN_6LORH_TYPE2              0x02
#define LOWPAN_PATTERN_6LORH_TYPE3              0x03
#define LOWPAN_PATTERN_6LORH_TYPE4              0x04
#define LOWPAN_PATTERN_6LORH_TYPE5              0x05
#define LOWPAN_PATTERN_6LORH_TYPE6              0x06
#define LOWPAN_PATTERN_6LORH_TYPE15             0x0F
#define LOWPAN_PATTERN_6LORH_TYPE16             0x10
#define LOWPAN_PATTERN_6LORH_TYPE17             0x11
#define LOWPAN_PATTERN_6LORH_TYPE18             0x12
#define LOWPAN_PATTERN_6LORH_TYPE19             0x13
#define LOWPAN_5_RPI_BIT_O                      0x1000
#define LOWPAN_5_RPI_BIT_R                      0x0800
#define LOWPAN_5_RPI_BIT_F                      0x0400
#define LOWPAN_5_RPI_BIT_I                      0x0200
#define LOWPAN_5_RPI_BIT_K                      0x0100
#define LOWPAN_5_RPI_BITS_IK                    0x0300
#define LOWPAN_6LORH_GENERAL_FORMAT             0x8000
#define LOWPAN_IP_IN_IP_6LORH                   6
#define BITS_IK_0                               0
#define BITS_IK_1                               1
#define BITS_IK_2                               2
#define BITS_IK_3                               3
#define BITS_IK_4                               4
#define IPV6_ADDR_COMPRESSED_1_BYTE             0
#define IPV6_ADDR_COMPRESSED_2_BYTE             1
#define IPV6_ADDR_COMPRESSED_4_BYTE             2
#define IPV6_ADDR_COMPRESSED_8_BYTE             3
#define IPV6_ADDR_COMPRESSED_16_BYTE            4

/* 6LoWPAN HC1 Header */
#define LOWPAN_HC1_SOURCE_PREFIX        0x80
#define LOWPAN_HC1_SOURCE_IFC           0x40
#define LOWPAN_HC1_DEST_PREFIX          0x20
#define LOWPAN_HC1_DEST_IFC             0x10
#define LOWPAN_HC1_TRAFFIC_CLASS        0x08
#define LOWPAN_HC1_NEXT                 0x06
#define LOWPAN_HC1_MORE                 0x01

/* IPv6 header field lengths (in bits) */
#define LOWPAN_IPV6_TRAFFIC_CLASS_BITS  8
#define LOWPAN_IPV6_FLOW_LABEL_BITS     20
#define LOWPAN_IPV6_NEXT_HEADER_BITS    8
#define LOWPAN_IPV6_HOP_LIMIT_BITS      8
#define LOWPAN_IPV6_PREFIX_BITS         64
#define LOWPAN_IPV6_INTERFACE_BITS      64

/* HC_UDP header field lengths (in bits). */
#define LOWPAN_UDP_PORT_BITS            16
#define LOWPAN_UDP_PORT_COMPRESSED_BITS 4
#define LOWPAN_UDP_LENGTH_BITS          16
#define LOWPAN_UDP_CHECKSUM_BITS        16

/* HC1 Next Header compression modes. */
#define LOWPAN_HC1_NEXT_NONE            0x00
#define LOWPAN_HC1_NEXT_UDP             0x01
#define LOWPAN_HC1_NEXT_ICMP            0x02
#define LOWPAN_HC1_NEXT_TCP             0x03

/* HC_UDP Header */
#define LOWPAN_HC2_UDP_SRCPORT          0x80
#define LOWPAN_HC2_UDP_DSTPORT          0x40
#define LOWPAN_HC2_UDP_LENGTH           0x20
#define LOWPAN_HC2_UDP_RESERVED         0x1f

/* IPHC Base flags */
#define LOWPAN_IPHC_FLAG_FLOW           0x1800
#define LOWPAN_IPHC_FLAG_NHDR           0x0400
#define LOWPAN_IPHC_FLAG_HLIM           0x0300
#define LOWPAN_IPHC_FLAG_CONTEXT_ID     0x0080
#define LOWPAN_IPHC_FLAG_SRC_COMP       0x0040
#define LOWPAN_IPHC_FLAG_SRC_MODE       0x0030
#define LOWPAN_IPHC_FLAG_MCAST_COMP     0x0008
#define LOWPAN_IPHC_FLAG_DST_COMP       0x0004
#define LOWPAN_IPHC_FLAG_DST_MODE       0x0003
#define LOWPAN_IPHC_FLAG_SCI            0xf0
#define LOWPAN_IPHC_FLAG_DCI            0x0f
/* Offsets for extracting integer fields. */
#define LOWPAN_IPHC_FLAG_OFFSET_FLOW    11
#define LOWPAN_IPHC_FLAG_OFFSET_HLIM    8
#define LOWPAN_IPHC_FLAG_OFFSET_SRC_MODE 4
#define LOWPAN_IPHC_FLAG_OFFSET_DST_MODE 0
#define LOWPAN_IPHC_FLAG_OFFSET_SCI      4
#define LOWPAN_IPHC_FLAG_OFFSET_DCI      0

/* IPHC Flow encoding values. */
#define LOWPAN_IPHC_FLOW_CLASS_LABEL    0x0
#define LOWPAN_IPHC_FLOW_ECN_LABEL      0x1
#define LOWPAN_IPHC_FLOW_CLASS          0x2
#define LOWPAN_IPHC_FLOW_COMPRESSED     0x3

/* IPHC Hop limit encoding. */
#define LOWPAN_IPHC_HLIM_INLINE         0x0
#define LOWPAN_IPHC_HLIM_1              0x1
#define LOWPAN_IPHC_HLIM_64             0x2
#define LOWPAN_IPHC_HLIM_255            0x3

/* IPHC address modes. */
#define LOWPAN_IPHC_ADDR_SRC_UNSPEC     0x0
#define LOWPAN_IPHC_ADDR_FULL_INLINE    0x0
#define LOWPAN_IPHC_ADDR_64BIT_INLINE   0x1
#define LOWPAN_IPHC_ADDR_16BIT_INLINE   0x2
#define LOWPAN_IPHC_ADDR_COMPRESSED     0x3

/* IPHC multicast address modes. */
#define LOWPAN_IPHC_MCAST_FULL          0x0
#define LOWPAN_IPHC_MCAST_48BIT         0x1
#define LOWPAN_IPHC_MCAST_32BIT         0x2
#define LOWPAN_IPHC_MCAST_8BIT          0x3

#define LOWPAN_IPHC_MCAST_STATEFUL_48BIT 0x0

/* IPHC Traffic class and flow label field sizes (in bits) */
#define LOWPAN_IPHC_ECN_BITS            2
#define LOWPAN_IPHC_DSCP_BITS           6
#define LOWPAN_IPHC_LABEL_BITS          20

/* NHC Patterns. */
#define LOWPAN_NHC_PATTERN_EXT          0x0e
#define LOWPAN_NHC_PATTERN_EXT_BITS     4
#define LOWPAN_NHC_PATTERN_UDP          0x1e
#define LOWPAN_NHC_PATTERN_UDP_BITS     5
/* IP-in-IP tunneling is handled as a separate NHC pattern.  */
#define LOWPAN_NHC_PATTERN_EXT_IPV6     ((LOWPAN_NHC_PATTERN_EXT << LOWPAN_NHC_EXT_EID_BITS) | LOWPAN_NHC_EID_IPV6)
#define LOWPAN_NHC_PATTERN_EXT_IPV6_BITS (LOWPAN_NHC_PATTERN_EXT_BITS + LOWPAN_NHC_EXT_EID_BITS)

/* NHC Extension header fields. */
#define LOWPAN_NHC_EXT_EID              0x0e
#define LOWPAN_NHC_EXT_EID_OFFSET       1
#define LOWPAN_NHC_EXT_EID_BITS         3
#define LOWPAN_NHC_EXT_NHDR             0x01

/* Extension header ID codes. */
#define LOWPAN_NHC_EID_HOP_BY_HOP       0x00
#define LOWPAN_NHC_EID_ROUTING          0x01
#define LOWPAN_NHC_EID_FRAGMENT         0x02
#define LOWPAN_NHC_EID_DEST_OPTIONS     0x03
#define LOWPAN_NHC_EID_MOBILITY         0x04
#define LOWPAN_NHC_EID_IPV6             0x07

/* NHC UDP fields. */
#define LOWPAN_NHC_UDP_CHECKSUM         0x04
#define LOWPAN_NHC_UDP_PORTS            0x03

/* 6LoWPAN Mesh Header */
#define LOWPAN_MESH_HEADER_V            0x20
#define LOWPAN_MESH_HEADER_F            0x10
#define LOWPAN_MESH_HEADER_HOPS         0x0f

/* 6LoWPAN First Fragment Header */
#define LOWPAN_FRAG_DGRAM_SIZE_BITS     11

/* Uncompressed IPv6 Option types */
#define IP6OPT_PAD1                     0x00
#define IP6OPT_PADN                     0x01

/* UDP port compression encoding */
#define LOWPAN_NHC_UDP_PORT_INLINE      0x0
#define LOWPAN_NHC_UDP_PORT_8BIT_DST    0x1
#define LOWPAN_NHC_UDP_PORT_8BIT_SRC    0x2
#define LOWPAN_NHC_UDP_PORT_12BIT       0x3

/* Compressed port number offset. */
#define LOWPAN_PORT_8BIT_OFFSET         0xf000
#define LOWPAN_PORT_12BIT_OFFSET        0xf0b0

/* 6LoWPAN interface identifier length. */
#define LOWPAN_IFC_ID_LEN               8

/* Protocol fields handles. */
static int proto_6lowpan;
static int hf_6lowpan_pattern;
static int hf_6lowpan_nhc_pattern;
static int hf_6lowpan_padding;

/* Header compression fields. */
static int hf_6lowpan_hc1_encoding;
static int hf_6lowpan_hc1_source_prefix;
static int hf_6lowpan_hc1_source_ifc;
static int hf_6lowpan_hc1_dest_prefix;
static int hf_6lowpan_hc1_dest_ifc;
static int hf_6lowpan_hc1_class;
static int hf_6lowpan_hc1_next;
static int hf_6lowpan_hc1_more;
static int hf_6lowpan_hc2_udp_encoding;
static int hf_6lowpan_hc2_udp_src;
static int hf_6lowpan_hc2_udp_dst;
static int hf_6lowpan_hc2_udp_len;

/* 6loRH */
static int hf_6lowpan_pagenb;
static int hf_6lowpan_routing_header;
static int hf_6lowpan_6lorhe_length;
static int hf_6lowpan_6lorhe_size;
static int hf_6lowpan_6lorhc_size;
static int hf_6lowpan_6lorhe_type;
static int hf_6lowpan_6lorhe_hoplimit;
static int hf_6lowpan_6lorhe_bitmap;
static int hf_6lowpan_5_bit_o;
static int hf_6lowpan_5_bit_r;
static int hf_6lowpan_5_bit_f;
static int hf_6lowpan_5_bit_i;
static int hf_6lowpan_5_bit_k;
static int hf_6lowpan_sender_rank1;
static int hf_6lowpan_sender_rank2;
static int hf_6lowpan_rpl_instance;
static int hf_6lowpan_6lorhc_address_hop0;
static int hf_6lowpan_6lorhc_address_hop2;
static int hf_6lowpan_6lorhc_address_hop3;
static int hf_6lowpan_6lorhc_address_hop4;
static int hf_6lowpan_6lorhc_address_hop1;
static int hf_6lowpan_6lorhc_address_src;

/* IPHC header field. */
static int hf_6lowpan_iphc_flag_tf;
static int hf_6lowpan_iphc_flag_nhdr;
static int hf_6lowpan_iphc_flag_hlim;
static int hf_6lowpan_iphc_flag_cid;
static int hf_6lowpan_iphc_flag_sac;
static int hf_6lowpan_iphc_flag_sam;
static int hf_6lowpan_iphc_flag_mcast;
static int hf_6lowpan_iphc_flag_dac;
static int hf_6lowpan_iphc_flag_dam;
static int hf_6lowpan_iphc_sci;
static int hf_6lowpan_iphc_dci;

static int hf_6lowpan_iphc_sctx_prefix;
static int hf_6lowpan_iphc_sctx_origin;
static int hf_6lowpan_iphc_dctx_prefix;
static int hf_6lowpan_iphc_dctx_origin;

/* NHC IPv6 extension header fields. */
static int hf_6lowpan_nhc_ext_eid;
static int hf_6lowpan_nhc_ext_nh;
static int hf_6lowpan_nhc_ext_next;
static int hf_6lowpan_nhc_ext_length;
static int hf_6lowpan_nhc_ext_reserved;

/* NHC UDP compression header fields. */
static int hf_6lowpan_nhc_udp_checksum;
static int hf_6lowpan_nhc_udp_ports;

/* Inline IPv6 header fields. */
static int hf_6lowpan_traffic_class;
static int hf_6lowpan_flow_label;
static int hf_6lowpan_ecn;
static int hf_6lowpan_dscp;
static int hf_6lowpan_next_header;
static int hf_6lowpan_hop_limit;
static int hf_6lowpan_source;
static int hf_6lowpan_dest;

/* Inline UDP header fields. */
static int hf_6lowpan_udp_src;
static int hf_6lowpan_udp_dst;
static int hf_6lowpan_udp_len;
static int hf_6lowpan_udp_checksum;

/* Broadcast header fields. */
static int hf_6lowpan_bcast_seqnum;

/* Mesh header fields. */
static int hf_6lowpan_mesh_v;
static int hf_6lowpan_mesh_f;
static int hf_6lowpan_mesh_hops;
static int hf_6lowpan_mesh_hops8;
static int hf_6lowpan_mesh_orig16;
static int hf_6lowpan_mesh_orig64;
static int hf_6lowpan_mesh_dest16;
static int hf_6lowpan_mesh_dest64;

/* Fragmentation header fields. */
static int hf_6lowpan_frag_dgram_size;
static int hf_6lowpan_frag_dgram_tag;
static int hf_6lowpan_frag_dgram_offset;

/* Recoverable Fragmentation header fields. */
static int hf_6lowpan_rfrag_congestion;
static int hf_6lowpan_rfrag_ack_requested;
static int hf_6lowpan_rfrag_dgram_tag;
static int hf_6lowpan_rfrag_sequence;
static int hf_6lowpan_rfrag_size;
static int hf_6lowpan_rfrag_dgram_size;
static int hf_6lowpan_rfrag_offset;
static int hf_6lowpan_rfrag_ack_bitmap;

/* Protocol tree handles.  */
static int ett_6lowpan;
static int ett_6lowpan_hc1;
static int ett_6lowpan_hc1_encoding;
static int ett_6lowpan_hc2_udp;
static int ett_6lowpan_iphc;
static int ett_lowpan_routing_header_dispatch;
static int ett_6lowpan_nhc_ext;
static int ett_6lowpan_nhc_udp;
static int ett_6lowpan_bcast;
static int ett_6lowpan_mesh;
static int ett_6lowpan_mesh_flags;
static int ett_6lowpan_frag;

static expert_field ei_6lowpan_hc1_more_bits;
static expert_field ei_6lowpan_illegal_dest_addr_mode;
static expert_field ei_6lowpan_bad_ipv6_header_length;
static expert_field ei_6lowpan_bad_ext_header_length;

/* Subdissector handles. */
static dissector_handle_t       handle_6lowpan;
static dissector_handle_t       ipv6_handle;

/* Value Strings */
static const value_string lowpan_patterns [] = {
    { LOWPAN_PATTERN_NALP,          "Not a LoWPAN frame" },
    { LOWPAN_PATTERN_IPV6,          "Uncompressed IPv6" },
    { LOWPAN_PATTERN_HC1,           "Header compression" },
    { LOWPAN_PATTERN_BC0,           "Broadcast" },
    { LOWPAN_PATTERN_IPHC,          "IP header compression" },
    { LOWPAN_PATTERN_ESC,           "Escape" },
    { LOWPAN_PATTERN_MESH,          "Mesh" },
    { LOWPAN_PATTERN_FRAG1,         "First fragment" },
    { LOWPAN_PATTERN_FRAGN,         "Fragment" },
    { LOWPAN_PATTERN_RFRAG,         "Recoverable Fragment" },
    { LOWPAN_PATTERN_RFRAG_ACK,     "Recoverable Fragment ACK" },
    { 0, NULL }
};
static const true_false_string lowpan_compression = {
    "Compressed",
    "Inline"
};
static const value_string lowpan_hc1_next [] = {
    { LOWPAN_HC1_NEXT_NONE,         "Inline" },
    { LOWPAN_HC1_NEXT_UDP,          "UDP" },
    { LOWPAN_HC1_NEXT_ICMP,         "ICMP" },
    { LOWPAN_HC1_NEXT_TCP,          "TCP" },
    { 0, NULL }
};
static const value_string lowpan_iphc_traffic [] = {
    { LOWPAN_IPHC_FLOW_CLASS_LABEL, "Traffic class and flow label inline" },
    { LOWPAN_IPHC_FLOW_ECN_LABEL,   "ECN and flow label inline" },
    { LOWPAN_IPHC_FLOW_CLASS,       "Traffic class inline" },
    { LOWPAN_IPHC_FLOW_COMPRESSED,  "Version, traffic class, and flow label compressed" },
    { 0, NULL }
};
static const value_string lowpan_iphc_hop_limit [] = {
    { LOWPAN_IPHC_HLIM_INLINE,      "Inline" },
    { LOWPAN_IPHC_HLIM_1,           "1" },
    { LOWPAN_IPHC_HLIM_64,          "64" },
    { LOWPAN_IPHC_HLIM_255,         "255" },
    { 0, NULL }
};
static const true_false_string lowpan_iphc_addr_compression = {
    "Stateful",
    "Stateless"
};
static const value_string lowpan_iphc_addr_modes [] = {
    { LOWPAN_IPHC_ADDR_FULL_INLINE, "Inline" },
    { LOWPAN_IPHC_ADDR_64BIT_INLINE,"64-bits inline" },
    { LOWPAN_IPHC_ADDR_16BIT_INLINE,"16-bits inline" },
    { LOWPAN_IPHC_ADDR_COMPRESSED,  "Compressed" },
    { 0, NULL }
};
static const value_string lowpan_iphc_saddr_stateful_modes [] = {
    { LOWPAN_IPHC_ADDR_FULL_INLINE, "Unspecified address (::)" },
    { LOWPAN_IPHC_ADDR_64BIT_INLINE,"64-bits inline" },
    { LOWPAN_IPHC_ADDR_16BIT_INLINE,"16-bits inline" },
    { LOWPAN_IPHC_ADDR_COMPRESSED,  "Compressed" },
    { 0, NULL }
};
static const value_string lowpan_iphc_daddr_stateful_modes [] = {
    { LOWPAN_IPHC_ADDR_64BIT_INLINE,"64-bits inline" },
    { LOWPAN_IPHC_ADDR_16BIT_INLINE,"16-bits inline" },
    { LOWPAN_IPHC_ADDR_COMPRESSED,  "Compressed" },
    { 0, NULL }
};
static const value_string lowpan_iphc_mcast_modes [] = {
    { LOWPAN_IPHC_MCAST_FULL,       "Inline" },
    { LOWPAN_IPHC_MCAST_48BIT,      "48-bits inline" },
    { LOWPAN_IPHC_MCAST_32BIT,      "32-bits inline" },
    { LOWPAN_IPHC_MCAST_8BIT,       "8-bits inline" },
    { 0, NULL }
};
static const value_string lowpan_iphc_mcast_stateful_modes [] = {
    { LOWPAN_IPHC_MCAST_STATEFUL_48BIT, "48-bits inline" },
    { 0, NULL }
};
static const value_string lowpan_nhc_patterns [] = {
    { LOWPAN_NHC_PATTERN_EXT,       "IPv6 extension header" },
    { LOWPAN_NHC_PATTERN_UDP,       "UDP compression header" },
    { 0, NULL }
};
static const value_string lowpan_nhc_eid [] = {
    { LOWPAN_NHC_EID_HOP_BY_HOP,    "IPv6 hop-by-hop options" },
    { LOWPAN_NHC_EID_ROUTING,       "IPv6 routing" },
    { LOWPAN_NHC_EID_FRAGMENT,      "IPv6 fragment" },
    { LOWPAN_NHC_EID_DEST_OPTIONS,  "IPv6 destination options" },
    { LOWPAN_NHC_EID_MOBILITY,      "IPv6 mobility header" },
    { LOWPAN_NHC_EID_IPV6,          "IPv6 header" },
    { 0, NULL }
};
static const value_string lowpan_udp_ports [] = {
    { LOWPAN_NHC_UDP_PORT_INLINE,   "Inline" },
    { LOWPAN_NHC_UDP_PORT_8BIT_DST, "Source port inline, first 8 bits of destination port elided" },
    { LOWPAN_NHC_UDP_PORT_8BIT_SRC, "Destination port inline, first 8 bits of source port elided" },
    { LOWPAN_NHC_UDP_PORT_12BIT,    "12 bits of both ports elided" },
    { 0, NULL }
};
/* 6loRH */
static const value_string lowpan_patterns_rh_type [] = {
        { LOWPAN_PATTERN_6LORH_TYPE0,        "Routing Header 3, 1 byte compression" },
        { LOWPAN_PATTERN_6LORH_TYPE1,        "Routing Header 3, 2 byte compression" },
        { LOWPAN_PATTERN_6LORH_TYPE2,        "Routing Header 3, 4 byte compression" },
        { LOWPAN_PATTERN_6LORH_TYPE3,        "Routing Header 3, 8 byte compression" },
        { LOWPAN_PATTERN_6LORH_TYPE4,        "Routing Header 3, 16 byte compression" },
        { LOWPAN_PATTERN_6LORH_TYPE5,        "Routing Protocol Information" },
        { LOWPAN_PATTERN_6LORH_TYPE6,        "IP in IP" },
        { LOWPAN_PATTERN_6LORH_TYPE15,       "BIER Header, bit-by-bit encoding, no control fields, 32 bits word size" },
        { LOWPAN_PATTERN_6LORH_TYPE16,       "BIER Header, Bloom filter encoding, 2* 1-byte HashID control fields, 32 bits word size" },
        { LOWPAN_PATTERN_6LORH_TYPE17,       "BIER Header, bit-by-bit encoding, no control fields, 128 bits word size" },
        { LOWPAN_PATTERN_6LORH_TYPE18,       "BIER Header, Bloom filter encoding, 8* 1-byte HashID control fields, 128 bits word size" },
        { LOWPAN_PATTERN_6LORH_TYPE19,       "BIER Header, bit-by-bit encoding, 1-byte GroupID control fields, 128 bits word size" },
        { 0, NULL }
};
static const value_string lowpan_patterns_rh [] = {
        { LOWPAN_PATTERN_6LORHC,        "Critical Routing Header" },
        { LOWPAN_PATTERN_6LORHE,        "Elective Routing Header" },
        { 0, NULL }
};
static const true_false_string bit_I_RPL = {
    "Elided (RPL Instance ID: 0)",
    "Present"
};
static const true_false_string bit_K_RPL = {
    "1 byte",
    "2 bytes"
};

/* Reassembly Data */
static int hf_6lowpan_fragments;
static int hf_6lowpan_fragment;
static int hf_6lowpan_fragment_overlap;
static int hf_6lowpan_fragment_overlap_conflicts;
static int hf_6lowpan_fragment_multiple_tails;
static int hf_6lowpan_fragment_too_long_fragment;
static int hf_6lowpan_fragment_error;
static int hf_6lowpan_fragment_count;
static int hf_6lowpan_reassembled_in;
static int hf_6lowpan_reassembled_length;
static int ett_6lowpan_fragment;
static int ett_6lowpan_fragments;

static const fragment_items lowpan_frag_items = {
    /* Fragment subtrees */
    &ett_6lowpan_fragment,
    &ett_6lowpan_fragments,
    /* Fragment fields */
    &hf_6lowpan_fragments,
    &hf_6lowpan_fragment,
    &hf_6lowpan_fragment_overlap,
    &hf_6lowpan_fragment_overlap_conflicts,
    &hf_6lowpan_fragment_multiple_tails,
    &hf_6lowpan_fragment_too_long_fragment,
    &hf_6lowpan_fragment_error,
    &hf_6lowpan_fragment_count,
    /* Reassembled in field */
    &hf_6lowpan_reassembled_in,
    /* Reassembled length field */
    &hf_6lowpan_reassembled_length,
    /* Reassembled data field */
    NULL,
    /* Tag */
    "6LoWPAN fragments"
};

static reassembly_table lowpan_reassembly_table;
static GHashTable *lowpan_context_table;

/* Link-Local prefix used by 6LoWPAN (FF80::/10) */
static const uint8_t lowpan_llprefix[8] = {
    0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

/* Context hash table map key. */
typedef struct {
    uint16_t pan;    /* PAN Identifier */
    uint8_t cid;    /* Context Identifier */
} lowpan_context_key;

/* Context hash table map data. */
typedef struct {
    unsigned   frame;  /* Frame where the context was discovered. */
    uint8_t plen;   /* Prefix length. */
    ws_in6_addr prefix;   /* Compression context. */
} lowpan_context_data;

/* 6LoWPAN contexts. */
#define LOWPAN_CONTEXT_MAX              16
#define LOWPAN_CONTEXT_DEFAULT          0
#define LOWPAN_CONTEXT_LINK_LOCAL       LOWPAN_CONTEXT_MAX
#define LOWPAN_CONTEXT_LINK_LOCAL_BITS  10
static lowpan_context_data  lowpan_context_local;
static lowpan_context_data  lowpan_context_default;
static const char *        lowpan_context_prefs[LOWPAN_CONTEXT_MAX];

/* Preferences */
static bool rfc4944_short_address_format;
static bool iid_has_universal_local_bit;
static bool ipv6_summary_in_tree = true;

/* Helper macro to convert a bit offset/length into a byte count. */
#define BITS_TO_BYTE_LEN(bitoff, bitlen)    ((bitlen)?(((bitlen) + ((bitoff)&0x07) + 7) >> 3):(0))

/* Structure for rebuilding UDP datagrams. */
struct udp_hdr {
    uint16_t            src_port;
    uint16_t            dst_port;
    uint16_t            length;
    uint16_t            checksum;
};

/* Structure used to store decompressed header chains until reassembly. */
struct lowpan_nhdr {
    /* List Linking */
    struct lowpan_nhdr  *next;
    /* Next Header */
    uint8_t             proto;
    unsigned            length;
    unsigned            reported;
};
#define LOWPAN_NHDR_DATA(nhdr)  ((uint8_t *)(nhdr) + sizeof (struct lowpan_nhdr))

/* Dissector prototypes */
static void         proto_init_6lowpan          (void);
static void         prefs_6lowpan_apply         (void);
static int          dissect_6lowpan             (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data);
static tvbuff_t *   dissect_6lowpan_ipv6        (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
static tvbuff_t *   dissect_6lowpan_hc1         (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int dgram_size, const uint8_t *siid, const uint8_t *diid);
static tvbuff_t *   dissect_6lowpan_bc0         (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
static tvbuff_t *   dissect_6lowpan_iphc        (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int dgram_size, const uint8_t *siid, const uint8_t *diid);
static struct lowpan_nhdr *
                    dissect_6lowpan_iphc_nhc    (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, int dgram_size, const uint8_t *siid, const uint8_t *diid);
static tvbuff_t *   dissect_6lowpan_mesh        (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, uint8_t *siid, uint8_t *diid);
static tvbuff_t *   dissect_6lowpan_rfrag       (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, const uint8_t *siid, const uint8_t *diid);
static tvbuff_t *   dissect_6lowpan_rfrag_ack   (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
static tvbuff_t *   dissect_6lowpan_frag_first  (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, const uint8_t *siid, const uint8_t *diid);
static tvbuff_t *   dissect_6lowpan_frag_middle (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
static void         dissect_6lowpan_unknown     (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
static tvbuff_t *   dissect_6lowpan_6loRH       (tvbuff_t *tvb, unsigned offset, proto_tree *tree);


/* Helper functions. */
static bool         lowpan_dlsrc_to_ifcid   (packet_info *pinfo, uint8_t *ifcid);
static bool         lowpan_dldst_to_ifcid   (packet_info *pinfo, uint8_t *ifcid);
static void         lowpan_addr16_to_ifcid  (uint16_t addr, uint8_t *ifcid);
static void         lowpan_addr16_with_panid_to_ifcid(uint16_t panid, uint16_t addr, uint8_t *ifcid);
static void         lowpan_addr48_to_ifcid  (const uint8_t *addr, uint8_t *ifcid);
static tvbuff_t *   lowpan_reassemble_ipv6  (tvbuff_t *tvb, packet_info *pinfo, struct ws_ip6_hdr *ipv6, struct lowpan_nhdr *nhdr_list);
static uint8_t      lowpan_parse_nhc_proto  (tvbuff_t *tvb, int offset);

/* Context table helpers */
static unsigned     lowpan_context_hash     (const void *key);
static gboolean         lowpan_context_equal    (const void *a, const void *b);
static lowpan_context_data *lowpan_context_find(uint8_t cid, uint16_t pan);

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_pfxcpy
 *  DESCRIPTION
 *      A version of memcpy that takes a length in bits. If the
 *      length is not byte-aligned, the final byte will be
 *      manipulated so that only the desired number of bits are
 *      copied.
 *  PARAMETERS
 *      dst             ; Destination.
 *      src             ; Source.
 *      bits            ; Number of bits to copy.
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
static void
lowpan_pfxcpy(void *dst, const void *src, size_t bits)
{
    memcpy(dst, src, bits>>3);
    if (bits & 0x7) {
        uint8_t mask = ((0xff00) >> (bits & 0x7));
        uint8_t last = ((const uint8_t *)src)[bits>>3] & mask;
        ((uint8_t *)dst)[bits>>3] &= ~mask;
        ((uint8_t *)dst)[bits>>3] |= last;
    }
} /* lowpan_pfxcpy */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_context_hash
 *  DESCRIPTION
 *      Context table hash function.
 *  PARAMETERS
 *      key             ; Pointer to a lowpan_context_key type.
 *  RETURNS
 *      unsigned        ; The hashed key value.
 *---------------------------------------------------------------
 */
static unsigned
lowpan_context_hash(const void *key)
{
    return (((const lowpan_context_key *)key)->cid) | (((const lowpan_context_key *)key)->pan << 8);
} /* lowpan_context_hash */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_context_equal
 *  DESCRIPTION
 *      Context table equals function.
 *  PARAMETERS
 *      key             ; Pointer to a lowpan_context_key type.
 *  RETURNS
 *      bool            ;
 *---------------------------------------------------------------
 */
static gboolean
lowpan_context_equal(const void *a, const void *b)
{
    return (((const lowpan_context_key *)a)->pan == ((const lowpan_context_key *)b)->pan) &&
           (((const lowpan_context_key *)a)->cid == ((const lowpan_context_key *)b)->cid);
} /* lowpan_context_equal */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_context_find
 *  DESCRIPTION
 *      Context table lookup function.
 *  PARAMETERS
 *      cid             ; Context identifier.
 *      pan             ; PAN identifier.
 *  RETURNS
 *      lowpan_context_data *;
 *---------------------------------------------------------------
 */
static lowpan_context_data *
lowpan_context_find(uint8_t cid, uint16_t pan)
{
    lowpan_context_key  key;
    lowpan_context_data *data;

    /* Check for the internal link-local context. */
    if (cid == LOWPAN_CONTEXT_LINK_LOCAL) return &lowpan_context_local;

    /* Lookup the context from the table. */
    key.pan = pan;
    key.cid = cid;
    data = (lowpan_context_data *)g_hash_table_lookup(lowpan_context_table, &key);
    if (data) return data;

    /* If we didn't find a match, try again with the broadcast PAN. */
    if (pan != IEEE802154_BCAST_PAN) {
        key.pan = IEEE802154_BCAST_PAN;
        data = (lowpan_context_data *)g_hash_table_lookup(lowpan_context_table, &key);
        if (data) return data;
    }

    /* If the lookup failed, return the default context (::/0) */
    return &lowpan_context_default;
} /* lowpan_context_find */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_context_insert
 *  DESCRIPTION
 *      Context table insert function.
 *  PARAMETERS
 *      cid             ; Context identifier.
 *      pan             ; PAN identifier.
 *      plen            ; Prefix length.
 *      prefix          ; Compression prefix.
 *      frame           ; Frame number.
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
void
lowpan_context_insert(uint8_t cid, uint16_t pan, uint8_t plen, ws_in6_addr *prefix, unsigned frame)
{
    lowpan_context_key  key;
    lowpan_context_data *data;
    void *              pkey;
    void *              pdata;

    /* Sanity! */
    if (plen > 128) return;
    if (!prefix) return;
    if (!lowpan_context_table) return;

    /* Search the context table for an existing entry. */
    key.pan = pan;
    key.cid = cid;
    if (g_hash_table_lookup_extended(lowpan_context_table, &key, NULL, &pdata)) {
        /* Context already exists. */
        data = (lowpan_context_data *)pdata;
        if ( (data->plen == plen) && (memcmp(&data->prefix, prefix, (plen+7)/8) == 0) ) {
            /* Context already exists with no change. */
            return;
        }
    }
    pkey = wmem_memdup(NULL, &key, sizeof(key));

    /* Create a new context */
    data = wmem_new(NULL, lowpan_context_data);
    data->frame = frame;
    data->plen = plen;
    memset(&data->prefix, 0, sizeof(ws_in6_addr)); /* Ensure zero padding */
    lowpan_pfxcpy(&data->prefix, prefix, plen);
    g_hash_table_insert(lowpan_context_table, pkey, data);
} /* lowpan_context_insert */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_context_free
 *  DESCRIPTION
 *     Frees the allocated memory for the context hash table
 *  PARAMETERS
 *      data            ; Pointer to key or value
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
static void
lowpan_context_free(void *data)
{
    wmem_free(NULL, data);
} /* lowpan_context_free */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_addr16_to_ifcid
 *  DESCRIPTION
 *      Converts a short address to in interface identifier as
 *      per rfc 6282 section 3.2.2.
 *  PARAMETERS
 *      addr            ; 16-bit short address.
 *      ifcid           ; interface identifier (output).
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
static void
lowpan_addr16_to_ifcid(uint16_t addr, uint8_t *ifcid)
{
    /* Note: The PANID is no longer used in building the IID. */
    ifcid[0] = 0x00; /* the U/L bit must be cleared. */
    ifcid[1] = 0x00;
    ifcid[2] = 0x00;
    ifcid[3] = 0xff;
    ifcid[4] = 0xfe;
    ifcid[5] = 0x00;
    ifcid[6] = (addr >> 8) & 0xff;
    ifcid[7] = (addr >> 0) & 0xff;
} /* lowpan_addr16_to_ifcid  */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_addr16_with_panid_to_ifcid
 *  DESCRIPTION
 *      Converts a short address to in interface identifier as
 *      per rfc 4944 section 6.
 *  PARAMETERS
 *      panid           ; 16-bit PAN ID.
 *      addr            ; 16-bit short address.
 *      ifcid           ; interface identifier (output).
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
static void
lowpan_addr16_with_panid_to_ifcid(uint16_t panid, uint16_t addr, uint8_t *ifcid)
{
    /* Note: The PANID is used in building the IID following RFC 2464 section 4. */
    ifcid[0] = (panid >> 8) & 0xfd; /* the U/L bit must be cleared. */
    ifcid[1] = (panid >> 0) & 0xff;
    ifcid[2] = 0x00;
    ifcid[3] = 0xff;
    ifcid[4] = 0xfe;
    ifcid[5] = 0x00;
    ifcid[6] = (addr >> 8) & 0xff;
    ifcid[7] = (addr >> 0) & 0xff;
} /* lowpan_addr16_with_panid_to_ifcid  */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_addr48_to_ifcid
 *  DESCRIPTION
 *      Converts an IEEE 48-bit MAC identifier to an interface
 *      identifier as per RFC 4291 Appendix A.
 *  PARAMETERS
 *      addr            ; 48-bit MAC identifier.
 *      ifcid           ; interface identifier (output).
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
static void
lowpan_addr48_to_ifcid(const uint8_t *addr, uint8_t *ifcid)
{
    static const uint8_t unknown_addr[] = { 0, 0, 0, 0, 0, 0 };

    /* Don't convert unknown addresses */
    if (memcmp(addr, unknown_addr, sizeof(unknown_addr)) != 0) {
        ifcid[0] = addr[0];
        ifcid[1] = addr[1];
        ifcid[2] = addr[2];
        ifcid[3] = 0xff;
        ifcid[4] = 0xfe;
        ifcid[5] = addr[3];
        ifcid[6] = addr[4];
        ifcid[7] = addr[5];
        if (iid_has_universal_local_bit) {
            ifcid[0] ^= 0x02; /* Invert the U/L bit. */
        }
    } else {
        memset(ifcid, 0, LOWPAN_IFC_ID_LEN);
    }
} /* lowpan_ether_to_ifcid */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_dlsrc_to_ifcid
 *  DESCRIPTION
 *      Finds an interface identifier from the data-link source
 *      addressing.
 *  PARAMETERS
 *      pinfo           ; packet information.
 *      ifcid           ; interface identifier (output).
 *  RETURNS
 *      bool            ; true if an interface identifier could
 *                          be found.
 *---------------------------------------------------------------
 */
static bool
lowpan_dlsrc_to_ifcid(packet_info *pinfo, uint8_t *ifcid)
{
    ieee802154_hints_t  *hints;

    /* Check the link-layer address field. */
    if (pinfo->dl_src.type == AT_EUI64) {
        memcpy(ifcid, pinfo->dl_src.data, LOWPAN_IFC_ID_LEN);
        /* RFC2464: Invert the U/L bit when using an EUI64 address. */
        ifcid[0] ^= 0x02;
        return true;
    } else if (pinfo->dl_src.type == AT_ETHER) {
        lowpan_addr48_to_ifcid((const uint8_t *)pinfo->dl_src.data, ifcid);
        return true;
    }

    /* Lookup the IEEE 802.15.4 addressing hints. */
    hints = (ieee802154_hints_t *)p_get_proto_data(wmem_file_scope(), pinfo,
                proto_get_id_by_filter_name(IEEE802154_PROTOABBREV_WPAN), 0);
    if (hints) {

        /* Convert the 16-bit short address to an IID using the PAN ID (RFC 4944) or not depending on the preference */
        if (rfc4944_short_address_format) {
            lowpan_addr16_with_panid_to_ifcid(hints->src_pan, hints->src16, ifcid);
        } else {
            lowpan_addr16_to_ifcid(hints->src16, ifcid);
        }

        return true;
    } else {
        /* Failed to find a link-layer source address. */
        memset(ifcid, 0, LOWPAN_IFC_ID_LEN);
        return false;
    }
} /* lowpan_dlsrc_to_ifcid */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_dldst_to_ifcid
 *  DESCRIPTION
 *      Finds an interface identifier from the data-link destination
 *      addressing.
 *  PARAMETERS
 *      pinfo           ; packet information.
 *      ifcid           ; interface identifier (output).
 *  RETURNS
 *      bool            ; true if an interface identifier could
 *                          be found.
 *---------------------------------------------------------------
 */
static bool
lowpan_dldst_to_ifcid(packet_info *pinfo, uint8_t *ifcid)
{
    ieee802154_hints_t  *hints;

    /* Check the link-layer address field. */
    if (pinfo->dl_dst.type == AT_EUI64) {
        memcpy(ifcid, pinfo->dl_dst.data, LOWPAN_IFC_ID_LEN);
        /* RFC2464: Invert the U/L bit when using an EUI64 address. */
        ifcid[0] ^= 0x02;
        return true;
    } else if (pinfo->dl_dst.type == AT_ETHER) {
        lowpan_addr48_to_ifcid((const uint8_t *)pinfo->dl_dst.data, ifcid);
        return true;
    }

    /* Lookup the IEEE 802.15.4 addressing hints. */
    hints = (ieee802154_hints_t *)p_get_proto_data(wmem_file_scope(), pinfo,
                proto_get_id_by_filter_name(IEEE802154_PROTOABBREV_WPAN), 0);
    if (hints) {

        /* Convert the 16-bit short address to an IID using the PAN ID (RFC 4944) or not depending on the preference */
        if (rfc4944_short_address_format) {
            lowpan_addr16_with_panid_to_ifcid(hints->src_pan, hints->dst16, ifcid);
        } else {
            lowpan_addr16_to_ifcid(hints->dst16, ifcid);
        }

        return true;
    } else {
        /* Failed to find a link-layer destination address. */
        memset(ifcid, 0, LOWPAN_IFC_ID_LEN);
        return false;
    }
} /* lowpan_dldst_to_ifcid */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_reassemble_ipv6
 *  DESCRIPTION
 *      Helper function to rebuild an IPv6 packet from the IPv6
 *      header structure, and a list of next header structures.
 *  PARAMETERS
 *      ipv6            ; IPv6 Header.
 *      nhdr_list       ; Next header list.
 *  RETURNS
 *      tvbuff_t *      ; Reassembled IPv6 packet.
 *---------------------------------------------------------------
 */
static tvbuff_t *
lowpan_reassemble_ipv6(tvbuff_t *tvb, packet_info *pinfo, struct ws_ip6_hdr *ipv6, struct lowpan_nhdr *nhdr_list)
{
    int                 length = 0;
    int                 reported = 0;
    uint8_t *            buffer;
    uint8_t *            cursor;
    struct lowpan_nhdr *nhdr;

    /* Compute the real and reported lengths. */
    for (nhdr = nhdr_list; nhdr; nhdr = nhdr->next) {
        length += nhdr->length;
        reported += nhdr->reported;
    }
    ipv6->ip6h_plen = g_ntohs(reported);

    /* Allocate a buffer for the packet and copy in the IPv6 header. */
    buffer = (uint8_t *)wmem_alloc(pinfo->pool, length + IPv6_HDR_SIZE);
    memcpy(buffer, ipv6, IPv6_HDR_SIZE);
    cursor = buffer + IPv6_HDR_SIZE;

    /* Add the next headers into the buffer. */
    for (nhdr = nhdr_list; nhdr; nhdr = nhdr->next) {
        memcpy(cursor, LOWPAN_NHDR_DATA(nhdr), nhdr->length);
        cursor += nhdr->length;
    };

    /* Return the reassembled packet. */
    return tvb_new_child_real_data(tvb, buffer, length + IPv6_HDR_SIZE, reported + IPv6_HDR_SIZE);
} /* lowpan_reassemble_ipv6 */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_parse_nhc_proto
 *  DESCRIPTION
 *      Parses the start of an 6LoWPAN NHC header to determine the
 *      next header protocol identifier. Will return IP_PROTO_NONE
 *      if no valid protocol could be determined.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      offset          ; offset of the NHC.
 *  RETURNS
 *      uint8_t         ; IP_PROTO_* of the next header's protocol.
 *---------------------------------------------------------------
 */
static uint8_t
lowpan_parse_nhc_proto(tvbuff_t *tvb, int offset)
{
    /* Ensure that at least one byte exists. */
    if (!tvb_bytes_exist(tvb, offset, 1)) return IP_PROTO_NONE;

    /* Check for IPv6 extension headers. */
    if (tvb_get_bits8(tvb, offset<<3, LOWPAN_NHC_PATTERN_EXT_BITS) == LOWPAN_NHC_PATTERN_EXT) {
        uint8_t     eid = (tvb_get_uint8(tvb, offset) & LOWPAN_NHC_EXT_EID) >> LOWPAN_NHC_EXT_EID_OFFSET;
        switch (eid) {
            case LOWPAN_NHC_EID_HOP_BY_HOP:
                return IP_PROTO_HOPOPTS;
            case LOWPAN_NHC_EID_ROUTING:
                return IP_PROTO_ROUTING;
            case LOWPAN_NHC_EID_FRAGMENT:
                return IP_PROTO_FRAGMENT;
            case LOWPAN_NHC_EID_DEST_OPTIONS:
                return IP_PROTO_DSTOPTS;
            case LOWPAN_NHC_EID_MOBILITY:
                return IP_PROTO_MIPV6;
            case LOWPAN_NHC_EID_IPV6:
                return IP_PROTO_IPV6;
            default:
                /* Unknown protocol type. */
                return IP_PROTO_NONE;
        };
    }
    /* Check for compressed UDP headers. */
    if (tvb_get_bits8(tvb, offset<<3, LOWPAN_NHC_PATTERN_UDP_BITS) == LOWPAN_NHC_PATTERN_UDP) {
        return IP_PROTO_UDP;
    }
    /* Unknown header type. */
    return IP_PROTO_NONE;
} /* lowpan_parse_nhc_proto */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      lowpan_reassembly_id
 *  DESCRIPTION
 *      Creates an identifier that groups fragments based on the given datagram
 *      tag and the link layer destination address (to differentiate packets
 *      forwarded over different links in a mesh network).
 *  PARAMETERS
 *      pinfo           : packet info.
 *      dgram_tag       ; datagram tag (from the Fragmentation Header).
 *  RETURNS
 *      uint32_t        ; identifier for this group of fragments.
 *---------------------------------------------------------------
 */
static uint32_t
lowpan_reassembly_id(packet_info *pinfo, uint16_t dgram_tag)
{
    /* Start with the datagram tag for identification. If the packet is not
     * being forwarded, then this should be sufficient to prevent collisions
     * which could break reassembly. */
    uint32_t    frag_id = dgram_tag;
    ieee802154_hints_t  *hints;

    /* Forwarded packets in a mesh network have the same datagram tag, mix
     * the IEEE 802.15.4 destination link layer address. */
    if (pinfo->dl_dst.type == AT_EUI64) {
        /* IEEE 64-bit extended address */
        frag_id = add_address_to_hash(frag_id, &pinfo->dl_dst);
    } else {
        /* 16-bit short address */
        hints = (ieee802154_hints_t *)p_get_proto_data(wmem_file_scope(), pinfo,
                    proto_get_id_by_filter_name(IEEE802154_PROTOABBREV_WPAN), 0);
        if (hints) {
            frag_id |= hints->dst16 << 16;
        }
    }
    return frag_id;
} /* lowpan_reassembly_id */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_heur
 *  DESCRIPTION
 *      Heuristic dissector for 6LoWPAN. Checks if the pattern is
 *      a valid 6LoWPAN type, and not NALP.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; protocol display tree.
 *      data            : ieee802154_packet,
 *  RETURNS
 *      boolean         ; true if the tvbuff was dissected as a
 *                          6LoWPAN packet. If this returns false,
 *                          then no dissection will be attempted.
 *---------------------------------------------------------------
 */
static bool
dissect_6lowpan_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    unsigned offset = 0;

    /* Check for valid patterns. */
    for (;;) {
        /* Parse patterns until we find a match. */
        if (!tvb_reported_length_remaining(tvb, offset)) return false;
        if (tvb_get_bits8(tvb, offset*8, LOWPAN_PATTERN_IPV6_BITS) == LOWPAN_PATTERN_IPV6) break;
        if (tvb_get_bits8(tvb, offset*8, LOWPAN_PATTERN_HC1_BITS)  == LOWPAN_PATTERN_HC1) break;
        if (tvb_get_bits8(tvb, offset*8, LOWPAN_PATTERN_BC0_BITS)  == LOWPAN_PATTERN_BC0) {
            /* Broadcast headers must be followed by another valid header. */
            offset += 2;
            continue;
        }
        if (tvb_get_bits8(tvb, offset*8, LOWPAN_PATTERN_IPHC_BITS) == LOWPAN_PATTERN_IPHC) break;
        if (tvb_get_bits8(tvb, offset*8, LOWPAN_PATTERN_MESH_BITS) == LOWPAN_PATTERN_MESH) {
            /* Mesh headers must be followed by another valid header. */
            uint8_t mesh = tvb_get_uint8(tvb, offset++);
            offset += (mesh & LOWPAN_MESH_HEADER_V) ? 2 : 8;
            offset += (mesh & LOWPAN_MESH_HEADER_F) ? 2 : 8;
            if ((mesh & LOWPAN_MESH_HEADER_HOPS) == LOWPAN_MESH_HEADER_HOPS) offset++;
            continue;
        }
        if (tvb_get_bits8(tvb, offset*8, LOWPAN_PATTERN_RFRAG_BITS) == LOWPAN_PATTERN_RFRAG) break;
        if (tvb_get_bits8(tvb, offset*8, LOWPAN_PATTERN_RFRAG_BITS) == LOWPAN_PATTERN_RFRAG_ACK) break;
        if (tvb_get_bits8(tvb, offset*8, LOWPAN_PATTERN_FRAG_BITS) == LOWPAN_PATTERN_FRAG1) {
            /* First fragment headers must be followed by another valid header. */
            offset += 4;
            continue;
        }
        if (tvb_get_bits8(tvb, offset*8, LOWPAN_PATTERN_FRAG_BITS) == LOWPAN_PATTERN_FRAGN) break;

        /* If we get here, then we couldn't match to any pattern. */
        return false;
    } /* for */

    /* If we get here, then we found a matching pattern. */
    dissect_6lowpan(tvb, pinfo, tree, data);
    return true;
} /* dissect_6lowpan_heur */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan
 *  DESCRIPTION
 *      Dissector routine for 6LoWPAN packets.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; protocol display tree.
 *      data            ; Packet data (ieee 802.15.4).
 *  RETURNS
 *      int             ; Length of data processed, or 0 if not 6LoWPAN.
 *---------------------------------------------------------------
 */
static int
dissect_6lowpan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    proto_tree *lowpan_tree;
    proto_item *lowpan_root;
    tvbuff_t   *next = tvb;
    unsigned    offset = 0;
    /* Interface identifier of the encapsulating layer. */
    uint8_t     src_iid[LOWPAN_IFC_ID_LEN];
    uint8_t     dst_iid[LOWPAN_IFC_ID_LEN];

    /* Get the interface identifiers from the encapsulating layer. */
    lowpan_dlsrc_to_ifcid(pinfo, src_iid);
    lowpan_dldst_to_ifcid(pinfo, dst_iid);

    /* Create the protocol tree. */
    lowpan_root = proto_tree_add_protocol_format(tree, proto_6lowpan, tvb, 0, -1, "6LoWPAN");
    lowpan_tree = proto_item_add_subtree(lowpan_root, ett_6lowpan);

    /* Add the protocol name. */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "6LoWPAN");

    /* Mesh and Broadcast headers always come first in a 6LoWPAN frame. */
    if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_MESH_BITS) == LOWPAN_PATTERN_MESH) {
        next = dissect_6lowpan_mesh(next, pinfo, lowpan_tree, src_iid, dst_iid);
        if (!next) return tvb_captured_length(tvb);
    }
    if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_BC0_BITS) == LOWPAN_PATTERN_BC0) {
        next = dissect_6lowpan_bc0(next, pinfo, lowpan_tree);
        if (!next) return tvb_captured_length(tvb);
    }

    /* After the mesh and broadcast headers, process dispatch codes recursively. */
    /* Recoverable Fragmentation headers.*/
    if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_RFRAG_BITS) == LOWPAN_PATTERN_RFRAG) {
        next = dissect_6lowpan_rfrag(next, pinfo, lowpan_tree, src_iid, dst_iid);
        if (!next) return tvb_captured_length(tvb);
    }
    else if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_RFRAG_BITS) == LOWPAN_PATTERN_RFRAG_ACK) {
        next = dissect_6lowpan_rfrag_ack(next, pinfo, lowpan_tree);
        if (!next) return tvb_captured_length(tvb);
    }
    /* Fragmentation headers.*/
    if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_FRAG_BITS) == LOWPAN_PATTERN_FRAG1) {
        next = dissect_6lowpan_frag_first(next, pinfo, lowpan_tree, src_iid, dst_iid);
    }
    else if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_FRAG_BITS) == LOWPAN_PATTERN_FRAGN) {
        next = dissect_6lowpan_frag_middle(next, pinfo, lowpan_tree);
    }
    /* Uncompressed IPv6 packets. */
    else if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_IPV6_BITS) == LOWPAN_PATTERN_IPV6) {
        next = dissect_6lowpan_ipv6(next, pinfo, lowpan_tree);
    }
    else if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_PAGING_DISPATCH_BITS) == LOWPAN_PATTERN_PAGING_DISPATCH) {
        proto_tree_add_bits_item(lowpan_tree, hf_6lowpan_pagenb, tvb, 4, 4, ENC_BIG_ENDIAN);
        offset += 1;
        next = dissect_6lowpan_6loRH(next, offset, lowpan_tree);
        if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_IPHC_BITS) == LOWPAN_PATTERN_IPHC) {
            next = dissect_6lowpan_iphc(next, pinfo, lowpan_tree, -1, src_iid, dst_iid);
            if (!next) return tvb_captured_length(tvb);
        }
        if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_HC1_BITS) == LOWPAN_PATTERN_HC1) {
            next = dissect_6lowpan_hc1(next, pinfo, lowpan_tree, -1, src_iid, dst_iid);
        }
    }
    /* Compressed IPv6 packets. */
    else if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_HC1_BITS) == LOWPAN_PATTERN_HC1) {
        next = dissect_6lowpan_hc1(next, pinfo, lowpan_tree, -1, src_iid, dst_iid);
    }
    else if (tvb_get_bits8(next, 0, LOWPAN_PATTERN_IPHC_BITS) == LOWPAN_PATTERN_IPHC) {
        next = dissect_6lowpan_iphc(next, pinfo, lowpan_tree, -1, src_iid, dst_iid);
    }
    /* Unknown 6LoWPAN dispatch type */
    else {
        dissect_6lowpan_unknown(next, pinfo, lowpan_tree);
        return tvb_captured_length(tvb);
    }

    /* The last step should have returned an uncompressed IPv6 datagram. */
    if (next) {
        call_dissector(ipv6_handle, next, pinfo, tree);
    }
    return tvb_captured_length(tvb);
} /* dissect_6lowpan */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_6loRH
 *  DESCRIPTION
 *      Dissector routine for 6loRH fields in 6LoWPAN packets.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      offset          ; offset of the 6loRH fields
 *      tree            ; protocol display tree.
 *  RETURNS
 *      tvbuff_t *      ; The remaining payload to be parsed.
 *---------------------------------------------------------------
 */
static tvbuff_t *
dissect_6lowpan_6loRH(tvbuff_t *tvb, unsigned offset, proto_tree *tree)
{

    uint16_t            check;
    int                 IK;
    uint16_t            loRH_flags;
    proto_tree *        loRH_tree;
    uint16_t            loRHE_length;
    uint8_t             loRHE_type;
    uint16_t            loRHE_class;
    uint8_t             rpl_instance;
    int                 condition = 1;
    int16_t             loRHE_unitnums;

    struct ws_ip6_hdr      ipv6;
    static int * const bits_RHC[] = {
        &hf_6lowpan_5_bit_o,
        &hf_6lowpan_5_bit_r,
        &hf_6lowpan_5_bit_f,
        &hf_6lowpan_5_bit_i,
        &hf_6lowpan_5_bit_k,
        NULL
    };

    loRH_flags  = tvb_get_ntohs(tvb, offset);
    check       = loRH_flags & 0xC000;

    if (check == LOWPAN_6LORH_GENERAL_FORMAT) {

        memset(&ipv6.ip6h_src, 0, sizeof(ipv6.ip6h_src));

        while(condition > 0){
            condition -= 1 ;
            /*Create the tree*/
            loRH_tree = proto_tree_add_subtree(tree, tvb, offset, 2, ett_lowpan_routing_header_dispatch, NULL, "6LoRH:");

            /* Get and display the pattern. */
            proto_tree_add_bits_item(loRH_tree, hf_6lowpan_routing_header, tvb, 8*offset, LOWPAN_PATTERN_IPHC_BITS, ENC_BIG_ENDIAN);
            /*=====================================================
             * Parse 6LoRH Header flags.
             *=====================================================
             */

            loRHE_class     = (loRH_flags & LOWPAN_PATTERN_6LORHE_CLASS) >> LOWPAN_PATTERN_6LORHE_CLASS_BITS;
            loRHE_length    = (loRH_flags & LOWPAN_PATTERN_6LORHE_LENGTH) >> LOWPAN_PATTERN_6LORHE_LENGTH_BITS;
            loRHE_unitnums  = loRHE_length + 1;
            loRHE_type      = (loRH_flags & LOWPAN_PATTERN_6LORHE_TYPE);
            IK              = (loRH_flags & LOWPAN_5_RPI_BITS_IK) >> 8;

            proto_item_append_text(loRH_tree, " %s", val_to_str_const(loRHE_type, lowpan_patterns_rh_type, "Unknown"));

            switch (loRHE_class){
                case (LOWPAN_PATTERN_6LORHE):/*Elective Routing Header*/
                    condition = 1 ;
                    if (loRHE_type >= 15) { /* BIER implementation */
                        proto_tree_add_uint             (loRH_tree, hf_6lowpan_6lorhe_size, tvb, offset, 2, loRH_flags & LOWPAN_PATTERN_6LORHE_LENGTH);
                        proto_tree_add_uint             (loRH_tree, hf_6lowpan_6lorhe_type, tvb, offset, 2, loRHE_type);
                        offset += 2 ;
                        if (loRHE_type == 15) {
                            for (int i=0; i<loRHE_unitnums; i++) {
                                proto_tree_add_item(loRH_tree, hf_6lowpan_6lorhe_bitmap, tvb, offset, 4, ENC_BIG_ENDIAN);
                                offset += 4;
                            }
                        }
                    }
                    else if (loRHE_type == LOWPAN_IP_IN_IP_6LORH) {
                        memset(&ipv6.ip6h_src, 0, sizeof(ipv6.ip6h_src));
                        proto_tree_add_item(loRH_tree, hf_6lowpan_6lorhe_length, tvb, offset, 2, ENC_BIG_ENDIAN);
                        proto_tree_add_item(loRH_tree, hf_6lowpan_6lorhe_type, tvb, offset, 2, ENC_BIG_ENDIAN);
                        proto_tree_add_item(loRH_tree, hf_6lowpan_6lorhe_hoplimit, tvb, offset + 2, 1, ENC_BIG_ENDIAN);

                        if (loRHE_length > 1) {
                            for (int i = 0; i < 16; ++i) {
                                ipv6.ip6h_src.bytes[i] = tvb_get_uint8(tvb, offset + 3 + i);
                            }
                            proto_tree_add_ipv6(loRH_tree, hf_6lowpan_6lorhc_address_src, tvb, offset + 3, 16,
                                                &ipv6.ip6h_src);
                        }
                        offset += 2 + loRHE_length;
                    }
                    else {
                        condition -= 1;
                    }
                    break; /* case LOWPAN_PATTERN_6LORHE */

                case (LOWPAN_PATTERN_6LORHC): /*Critical Routing Header*/
                    condition = 1 ;
                    if (loRHE_type == 5){
                        proto_tree_add_bitmask_list (loRH_tree, tvb, offset, 2, bits_RHC, ENC_NA);
                        proto_tree_add_item         (loRH_tree, hf_6lowpan_6lorhe_type, tvb, offset, 2, ENC_BIG_ENDIAN);
                        offset += 2;
                        switch (IK){
                            case  BITS_IK_0:
                                proto_tree_add_item             (loRH_tree, hf_6lowpan_rpl_instance, tvb, offset, 1, ENC_BIG_ENDIAN);
                                proto_tree_add_item             (loRH_tree, hf_6lowpan_sender_rank2, tvb, offset+1, 2, ENC_BIG_ENDIAN);
                                offset += 3;
                                break;
                            case BITS_IK_1:
                                proto_tree_add_item             (loRH_tree, hf_6lowpan_rpl_instance, tvb, offset, 1, ENC_BIG_ENDIAN);
                                proto_tree_add_item             (loRH_tree, hf_6lowpan_sender_rank1, tvb, offset+1, 1, ENC_BIG_ENDIAN);
                                offset += 2;
                                break;
                            case BITS_IK_2:
                                rpl_instance = 0x00;
                                proto_tree_add_uint             (loRH_tree, hf_6lowpan_rpl_instance, tvb, offset, 0, rpl_instance);
                                proto_tree_add_item             (loRH_tree, hf_6lowpan_sender_rank2, tvb, offset, 2, ENC_BIG_ENDIAN);
                                offset += 2;
                                break;
                            case BITS_IK_3:
                                rpl_instance = 0x00;
                                proto_tree_add_uint             (loRH_tree, hf_6lowpan_rpl_instance, tvb, offset, 0, rpl_instance);
                                proto_tree_add_item             (loRH_tree, hf_6lowpan_sender_rank1, tvb, offset, 1, ENC_BIG_ENDIAN);
                                offset +=1;
                                break;
                            }
                        }
                    else if (loRHE_type <= 4){
                        memset(&ipv6.ip6h_src, 0, sizeof(ipv6.ip6h_src));
                        proto_tree_add_uint             (loRH_tree, hf_6lowpan_6lorhc_size, tvb, offset, 2, loRH_flags & LOWPAN_PATTERN_6LORHE_LENGTH);
                        proto_tree_add_uint             (loRH_tree, hf_6lowpan_6lorhe_type, tvb, offset, 2, loRHE_type);
                        offset += 2 ;
                        switch (loRHE_type){
                            case IPV6_ADDR_COMPRESSED_1_BYTE: /* IPv6 address compressed to 1 byte */
                                for (int i=0; i<loRHE_unitnums; i++) {
                                    for (int j = 0; j < 1; j++){
                                        ipv6.ip6h_src.bytes[15-j] = tvb_get_uint8(tvb, offset);
                                    }
                                    proto_tree_add_ipv6(tree, hf_6lowpan_6lorhc_address_hop0, tvb, offset, 1, &ipv6.ip6h_src);
                                    offset +=1;
                                }
                                break;

                            case IPV6_ADDR_COMPRESSED_2_BYTE: /* IPv6 address compressed to 2 bytes */
                                for (int i=0; i<loRHE_unitnums; i++) {
                                    for (int j = 0; j < 2; ++j){
                                        ipv6.ip6h_src.bytes[15-1+j] = tvb_get_uint8(tvb, offset);
                                        offset +=1;
                                    }
                                    proto_tree_add_ipv6(tree, hf_6lowpan_6lorhc_address_hop1, tvb, offset - 2, 2, &ipv6.ip6h_src);
                                }
                                break;

                            case IPV6_ADDR_COMPRESSED_4_BYTE: /* IPv6 address compressed to 4 bytes */
                                for (int i=0; i<loRHE_unitnums; i++) {
                                    for (int j = 0; j < 4; j++){
                                        ipv6.ip6h_src.bytes[15-3+j] = tvb_get_uint8(tvb, offset);
                                        offset +=1;
                                    }
                                    proto_tree_add_ipv6(tree, hf_6lowpan_6lorhc_address_hop2, tvb, offset - 4, 4, &ipv6.ip6h_src);
                                }
                                break;

                            case IPV6_ADDR_COMPRESSED_8_BYTE: /* IPv6 address compressed to 8 bytes */
                                for (int i=0; i<loRHE_unitnums; i++) {
                                    for (int j = 0; j < 8; j++){
                                        ipv6.ip6h_src.bytes[15-7+j] = tvb_get_uint8(tvb, offset);
                                        offset +=1;
                                    }
                                    proto_tree_add_ipv6(tree, hf_6lowpan_6lorhc_address_hop3, tvb, offset - 8, 8, &ipv6.ip6h_src);
                                }
                                break;
                            case IPV6_ADDR_COMPRESSED_16_BYTE: /* IPv6 address compressed to 16 bytes */
                                for (int i=0; i<loRHE_unitnums; i++) {
                                    for (int j = 0; j < 16; j++){
                                        ipv6.ip6h_src.bytes[j] = tvb_get_uint8(tvb, offset);
                                        offset +=1;
                                    }
                                    proto_tree_add_ipv6(tree, hf_6lowpan_6lorhc_address_hop4, tvb, offset - 16, 16, &ipv6.ip6h_src);
                                }
                                break; /**/
                            } /* switch loRHE_type */
                        } /* else if (loRHE_type <= 4) */
                    else {
                        condition -= 1;
                    }
                    break; /* case LOWPAN_PATTERN_6LORHC */

                    default:
                        condition -= 1 ;
                        break;
                }  /* switch loRHE_class */
            loRH_flags  = tvb_get_ntohs(tvb, offset);
            loRHE_class = (loRH_flags & LOWPAN_PATTERN_6LORHE_CLASS) >> 13;

            if ((loRHE_class) != LOWPAN_PATTERN_6LORHE){
                if ((loRHE_class) != LOWPAN_PATTERN_6LORHC){
                    condition -= 1;
                }
            }
        } /* while (condition > 0)*/
    }
    return tvb_new_subset_remaining(tvb, offset);
} /* dissect_6lowpan_6loRH */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_ipv6
 *  DESCRIPTION
 *      Dissector routine for an uncompressed IPv6 header type.
 *
 *      This is one of the final encapsulation types, and will
 *      returned an uncompressed IPv6 datagram (or fragment
 *      thereof).
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *      offset          ; offset to the start of the header.
 *  RETURNS
 *      tvbuff_t *      ; The remaining payload to be parsed.
 *---------------------------------------------------------------
 */
static tvbuff_t *
dissect_6lowpan_ipv6(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
{
    /* Get and display the pattern. */
    proto_tree_add_bits_item(tree, hf_6lowpan_pattern,
            tvb, 0, LOWPAN_PATTERN_IPV6_BITS, ENC_BIG_ENDIAN);

    /* Create a tvbuff subset for the ipv6 datagram. */
    return tvb_new_subset_remaining(tvb, 1);
} /* dissect_6lowpan_ipv6 */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_hc1
 *  DESCRIPTION
 *      Dissector routine for a 6LoWPAN HC1 header.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *      dgram_size      ; Datagram size (or <0 if not fragmented).
 *      siid            ; Source Interface ID.
 *      diid            ; Destination Interface ID.
 *  RETURNS
 *      tvbuff_t *      ; The remaining payload to be parsed.
 *---------------------------------------------------------------
 */
static tvbuff_t *
dissect_6lowpan_hc1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int dgram_size, const uint8_t *siid, const uint8_t *diid)
{
    int                 offset = 0;
    int                 bit_offset;
    int                 i;
    uint8_t             hc1_encoding;
    uint8_t             hc_udp_encoding = 0;
    uint8_t             next_header;
    proto_tree *        hc_tree;
    proto_item *        hc_item;
    tvbuff_t *          ipv6_tvb;
    /* IPv6 header. */
    uint8_t             ipv6_class;
    uint32_t            ipv6_flow;
    struct ws_ip6_hdr   ipv6;
    struct lowpan_nhdr *nhdr_list;
    static int * const hc1_encodings[] = {
        &hf_6lowpan_hc1_source_prefix,
        &hf_6lowpan_hc1_source_ifc,
        &hf_6lowpan_hc1_dest_prefix,
        &hf_6lowpan_hc1_dest_ifc,
        &hf_6lowpan_hc1_class,
        &hf_6lowpan_hc1_next,
        &hf_6lowpan_hc1_more,
        NULL
    };
    static int * const hc2_encodings[] = {
        &hf_6lowpan_hc2_udp_src,
        &hf_6lowpan_hc2_udp_dst,
        &hf_6lowpan_hc2_udp_len,
        NULL
    };

    /*=====================================================
     * Parse HC Encoding Flags
     *=====================================================
     */
    /* Create a tree for the HC1 Header. */
    hc_tree = proto_tree_add_subtree(tree, tvb, 0, 2, ett_6lowpan_hc1, &hc_item, "HC1 Encoding");

    /* Get and display the pattern. */
    proto_tree_add_bits_item(hc_tree, hf_6lowpan_pattern, tvb, 0, LOWPAN_PATTERN_HC1_BITS, ENC_BIG_ENDIAN);
    offset += 1;

    /* Get and display the HC1 encoding bits. */
    hc1_encoding = tvb_get_uint8(tvb, offset);
    next_header = ((hc1_encoding & LOWPAN_HC1_NEXT) >> 1);
    proto_tree_add_bitmask(hc_tree, tvb, offset, hf_6lowpan_hc1_encoding,
                   ett_6lowpan_hc1_encoding, hc1_encodings, ENC_NA);
    offset += 1;

    /* Get and display the HC2 encoding bits, if present. */
    if (hc1_encoding & LOWPAN_HC1_MORE) {
        if (next_header == LOWPAN_HC1_NEXT_UDP) {
            hc_udp_encoding = tvb_get_uint8(tvb, offset);
            proto_tree_add_bitmask(tree, tvb, offset, hf_6lowpan_hc2_udp_encoding,
                   ett_6lowpan_hc2_udp, hc2_encodings, ENC_NA);
            offset += 1;
        }
        else {
            /* HC1 states there are more bits, but an illegal next header was defined. */
            expert_add_info(pinfo, hc_item, &ei_6lowpan_hc1_more_bits);
            return NULL;
        }
    }

    /*=====================================================
     * Parse Uncompressed IPv6 Header Fields
     *=====================================================
     */
    /*
     * And now all hell breaks loose. After the header encoding fields, we are
     * left with an assortment of optional fields from the IPv6 header,
     * depending on which fields are present or not, the headers may not be
     * aligned to an octet boundary.
     *
     * From now on we have to parse the uncompressed fields relative to a bit
     * offset.
     */
    bit_offset = offset << 3;

    /* Parse hop limit */
    ipv6.ip6h_hlim = tvb_get_bits8(tvb, bit_offset, LOWPAN_IPV6_HOP_LIMIT_BITS);
    proto_tree_add_uint(tree, hf_6lowpan_hop_limit, tvb, bit_offset>>3,
            BITS_TO_BYTE_LEN(bit_offset, LOWPAN_IPV6_HOP_LIMIT_BITS), ipv6.ip6h_hlim);
    bit_offset += LOWPAN_IPV6_HOP_LIMIT_BITS;

    /*=====================================================
     * Parse/Decompress IPv6 Source Address
     *=====================================================
     */
    offset = bit_offset;
    if (!(hc1_encoding & LOWPAN_HC1_SOURCE_PREFIX)) {
        for (i=0; i<8; i++, bit_offset += 8) {
            ipv6.ip6h_src.bytes[i] = tvb_get_bits8(tvb, bit_offset, 8);
        }
    }
    else {
        memcpy(ipv6.ip6h_src.bytes, lowpan_llprefix, sizeof(lowpan_llprefix));
    }
    if (!(hc1_encoding & LOWPAN_HC1_SOURCE_IFC)) {
        for (i=8; i<16; i++, bit_offset += 8) {
            ipv6.ip6h_src.bytes[i] = tvb_get_bits8(tvb, bit_offset, 8);
        }
    }
    else {
        memcpy(&ipv6.ip6h_src.bytes[sizeof(ipv6.ip6h_src) - LOWPAN_IFC_ID_LEN], siid, LOWPAN_IFC_ID_LEN);
    }
    /* Display the source address. */
    proto_tree_add_ipv6(tree, hf_6lowpan_source, tvb, offset>>3,
            BITS_TO_BYTE_LEN(offset, (bit_offset-offset)), &ipv6.ip6h_src);

    /*
     * Do not set the address columns until after defragmentation, since we have
     * to do decompression before reassembly, and changing the address will cause
     * wireshark to think that the middle fragments came from another device.
     */

    /*=====================================================
     * Parse/Decompress IPv6 Destination Address
     *=====================================================
     */
    offset = bit_offset;
    if (!(hc1_encoding & LOWPAN_HC1_DEST_PREFIX)) {
        for (i=0; i<8; i++, bit_offset += 8) {
            ipv6.ip6h_dst.bytes[i] = tvb_get_bits8(tvb, bit_offset, 8);
        }
    }
    else {
        memcpy(ipv6.ip6h_dst.bytes, lowpan_llprefix, sizeof(lowpan_llprefix));
    }
    if (!(hc1_encoding & LOWPAN_HC1_DEST_IFC)) {
        for (i=8; i<16; i++, bit_offset += 8) {
            ipv6.ip6h_dst.bytes[i] = tvb_get_bits8(tvb, bit_offset, 8);
        }
    }
    else {
        memcpy(&ipv6.ip6h_dst.bytes[sizeof(ipv6.ip6h_dst) - LOWPAN_IFC_ID_LEN], diid, LOWPAN_IFC_ID_LEN);
    }
    /* Display the destination address. */
    proto_tree_add_ipv6(tree, hf_6lowpan_dest, tvb, offset>>3,
            BITS_TO_BYTE_LEN(offset, (bit_offset-offset)), &ipv6.ip6h_dst);

    /*
     * Do not set the address columns until after defragmentation, since we have
     * to do decompression before reassembly, and changing the address will cause
     * wireshark to think that the middle fragments came from another device.
     */

    /* Parse the traffic class and flow label. */
    ipv6_class = 0;
    ipv6_flow = 0;
    if (!(hc1_encoding & LOWPAN_HC1_TRAFFIC_CLASS)) {
        /* Parse the traffic class. */
        ipv6_class = tvb_get_bits8(tvb, bit_offset, LOWPAN_IPV6_TRAFFIC_CLASS_BITS);
        proto_tree_add_uint(tree, hf_6lowpan_traffic_class, tvb, bit_offset>>3,
                BITS_TO_BYTE_LEN(bit_offset, LOWPAN_IPV6_TRAFFIC_CLASS_BITS), ipv6_class);
        bit_offset += LOWPAN_IPV6_TRAFFIC_CLASS_BITS;

        /* Parse the flow label. */
        ipv6_flow = tvb_get_bits32(tvb, bit_offset, LOWPAN_IPV6_FLOW_LABEL_BITS, ENC_BIG_ENDIAN);
        proto_tree_add_uint(tree, hf_6lowpan_flow_label, tvb, bit_offset>>3,
                BITS_TO_BYTE_LEN(bit_offset, LOWPAN_IPV6_FLOW_LABEL_BITS), ipv6_flow);
        bit_offset += LOWPAN_IPV6_FLOW_LABEL_BITS;
    }

    /* Rebuild the IPv6 flow label, traffic class and version fields. */
    ipv6.ip6h_vc_flow = ipv6_flow;
    ipv6.ip6h_vc_flow |= ((uint32_t)ipv6_class << LOWPAN_IPV6_FLOW_LABEL_BITS);
    ipv6.ip6h_vc_flow |= ((uint32_t)0x6 << (LOWPAN_IPV6_TRAFFIC_CLASS_BITS + LOWPAN_IPV6_FLOW_LABEL_BITS));
    ipv6.ip6h_vc_flow = g_ntohl(ipv6.ip6h_vc_flow);

    /* Parse the IPv6 next header field. */
    if (next_header == LOWPAN_HC1_NEXT_UDP) {
        ipv6.ip6h_nxt = IP_PROTO_UDP;
    }
    else if (next_header == LOWPAN_HC1_NEXT_ICMP) {
        ipv6.ip6h_nxt = IP_PROTO_ICMPV6;
    }
    else if (next_header == LOWPAN_HC1_NEXT_TCP) {
        ipv6.ip6h_nxt = IP_PROTO_TCP;
    }
    else {
        /* Parse the next header field. */
        ipv6.ip6h_nxt = tvb_get_bits8(tvb, bit_offset, LOWPAN_IPV6_NEXT_HEADER_BITS);
        proto_tree_add_uint_format_value(tree, hf_6lowpan_next_header, tvb, bit_offset>>3,
                BITS_TO_BYTE_LEN(bit_offset, LOWPAN_IPV6_NEXT_HEADER_BITS), ipv6.ip6h_nxt,
                "%s (0x%02x)", ipprotostr(ipv6.ip6h_nxt), ipv6.ip6h_nxt);
        bit_offset += LOWPAN_IPV6_NEXT_HEADER_BITS;
    }

    /*=====================================================
     * Parse and Reconstruct the UDP Header
     *=====================================================
     */
    if ((hc1_encoding & LOWPAN_HC1_MORE) && (next_header == LOWPAN_HC1_NEXT_UDP)) {
        struct udp_hdr  udp;
        int             length;

        /* Parse the source port. */
        offset = bit_offset;
        if (hc_udp_encoding & LOWPAN_HC2_UDP_SRCPORT) {
            udp.src_port = tvb_get_bits8(tvb, bit_offset, LOWPAN_UDP_PORT_COMPRESSED_BITS) + LOWPAN_PORT_12BIT_OFFSET;
            bit_offset += LOWPAN_UDP_PORT_COMPRESSED_BITS;
        }
        else {
            udp.src_port = tvb_get_bits16(tvb, bit_offset, LOWPAN_UDP_PORT_BITS, ENC_BIG_ENDIAN);
            bit_offset += LOWPAN_UDP_PORT_BITS;
        }
        proto_tree_add_uint(tree, hf_6lowpan_udp_src, tvb, offset>>3,
                BITS_TO_BYTE_LEN(offset, (bit_offset-offset)), udp.src_port);
        udp.src_port = g_ntohs(udp.src_port);

        /* Parse the destination port. */
        offset = bit_offset;
        if (hc_udp_encoding & LOWPAN_HC2_UDP_DSTPORT) {
            udp.dst_port = tvb_get_bits8(tvb, bit_offset, LOWPAN_UDP_PORT_COMPRESSED_BITS) + LOWPAN_PORT_12BIT_OFFSET;
            bit_offset += LOWPAN_UDP_PORT_COMPRESSED_BITS;
        }
        else {
            udp.dst_port = tvb_get_bits16(tvb, bit_offset, LOWPAN_UDP_PORT_BITS, ENC_BIG_ENDIAN);
            bit_offset += LOWPAN_UDP_PORT_BITS;
        }
        proto_tree_add_uint(tree, hf_6lowpan_udp_dst, tvb, offset>>3,
                BITS_TO_BYTE_LEN(offset, (bit_offset-offset)), udp.dst_port);
        udp.dst_port = g_ntohs(udp.dst_port);

        /* Parse the length, if present. */
        if (!(hc_udp_encoding & LOWPAN_HC2_UDP_LENGTH)) {
            udp.length = tvb_get_bits16(tvb, bit_offset, LOWPAN_UDP_LENGTH_BITS, ENC_BIG_ENDIAN);
            proto_tree_add_uint(tree, hf_6lowpan_udp_len, tvb, bit_offset>>3,
                    BITS_TO_BYTE_LEN(bit_offset, LOWPAN_UDP_LENGTH_BITS), udp.length);

            bit_offset += LOWPAN_UDP_LENGTH_BITS;
        }
        /* Compute the length from the fragmentation headers. */
        else if (dgram_size >= 0) {
            if (dgram_size < IPv6_HDR_SIZE) {
                /* Datagram size is too small */
                return NULL;
            }
            udp.length = dgram_size - IPv6_HDR_SIZE;
        }
        /* Compute the length from the tvbuff size. */
        else {
            udp.length = tvb_reported_length(tvb);
            udp.length -= BITS_TO_BYTE_LEN(0, bit_offset + LOWPAN_UDP_CHECKSUM_BITS);
            udp.length += (int)sizeof(struct udp_hdr);
        }
        udp.length = g_ntohs(udp.length);

        /* Parse the checksum. */
        udp.checksum = tvb_get_bits16(tvb, bit_offset, LOWPAN_UDP_CHECKSUM_BITS, ENC_BIG_ENDIAN);
        proto_tree_add_uint(tree, hf_6lowpan_udp_checksum, tvb, bit_offset>>3,
                BITS_TO_BYTE_LEN(bit_offset, LOWPAN_UDP_CHECKSUM_BITS), udp.checksum);
        bit_offset += LOWPAN_UDP_CHECKSUM_BITS;
        udp.checksum = g_ntohs(udp.checksum);

        /* Construct the next header for the UDP datagram. */
        offset = BITS_TO_BYTE_LEN(0, bit_offset);
        length = tvb_captured_length_remaining(tvb, offset);
        nhdr_list = (struct lowpan_nhdr *)wmem_alloc(pinfo->pool, sizeof(struct lowpan_nhdr) + sizeof(struct udp_hdr) + length);
        nhdr_list->next = NULL;
        nhdr_list->proto = IP_PROTO_UDP;
        nhdr_list->length = length + (int)sizeof(struct udp_hdr);
        nhdr_list->reported = g_ntohs(udp.length);

        /* Copy the UDP header into the buffer. */
        memcpy(LOWPAN_NHDR_DATA(nhdr_list), &udp, sizeof(struct udp_hdr));
        tvb_memcpy(tvb, LOWPAN_NHDR_DATA(nhdr_list) + sizeof(struct udp_hdr), offset, length);
    }
    /*=====================================================
     * Reconstruct the IPv6 Packet
     *=====================================================
     */
    else {
        int length;
        offset = BITS_TO_BYTE_LEN(0, bit_offset);
        length = tvb_captured_length_remaining(tvb, offset);
        nhdr_list = (struct lowpan_nhdr *)wmem_alloc(pinfo->pool, sizeof(struct lowpan_nhdr) + length);
        nhdr_list->next = NULL;
        nhdr_list->proto = ipv6.ip6h_nxt;
        nhdr_list->length = length;
        if (dgram_size < 0) {
            nhdr_list->reported = tvb_reported_length_remaining(tvb, offset);
        }
        else {
            nhdr_list->reported = dgram_size - IPv6_HDR_SIZE;
        }
        tvb_memcpy(tvb, LOWPAN_NHDR_DATA(nhdr_list), offset, nhdr_list->length);
    }

    /* Link the reassembled tvbuff together.  */
    ipv6_tvb = lowpan_reassemble_ipv6(tvb, pinfo, &ipv6, nhdr_list);

    /* Add a new data source for it. */
    add_new_data_source(pinfo, ipv6_tvb, "Decompressed 6LoWPAN HC1");

    return ipv6_tvb;
} /* dissect_6lowpan_hc1 */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_iphc
 *  DESCRIPTION
 *      Dissector routine for a 6LoWPAN IPHC header.
 *
 *      This header is still in the draft phase, but is expected
 *      to replace HC1.
 *
 *      See draft-ietf-6lowpan-hc-15.txt
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *      dgram_size      ; Datagram size (or <0 if not fragmented).
 *      siid            ; Source Interface ID.
 *      diid            ; Destination Interface ID.
 *  RETURNS
 *      tvbuff_t *      ; The remaining payload to be parsed or NULL on error.
 *---------------------------------------------------------------
 */
static tvbuff_t *
// NOLINTNEXTLINE(misc-no-recursion)
dissect_6lowpan_iphc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int dgram_size, const uint8_t *siid, const uint8_t *diid)
{
    ieee802154_hints_t  *hints;
    uint16_t            hint_panid;
    int                 offset = 0;
    int                 length = 0;
    proto_tree *        iphc_tree;
    proto_item *        ti_dam = NULL;
    proto_item *        ti;
    /* IPHC header fields. */
    uint16_t            iphc_flags;
    uint8_t             iphc_traffic;
    uint8_t             iphc_hop_limit;
    uint8_t             iphc_src_mode;
    uint8_t             iphc_dst_mode;
    uint8_t             iphc_ctx = 0;
    /* Contexts to use for address decompression. */
    int                 iphc_sci = LOWPAN_CONTEXT_DEFAULT;
    int                 iphc_dci = LOWPAN_CONTEXT_DEFAULT;
    lowpan_context_data *sctx;
    lowpan_context_data *dctx;
    /* IPv6 header */
    uint8_t             ipv6_dscp = 0;
    uint8_t             ipv6_ecn = 0;
    uint32_t            ipv6_flowlabel = 0;
    struct ws_ip6_hdr   ipv6;
    tvbuff_t *          ipv6_tvb;
    /* Next header chain */
    struct lowpan_nhdr  *nhdr_list;

    /* Lookup the IEEE 802.15.4 addressing hints. */
    hints = (ieee802154_hints_t *)p_get_proto_data(wmem_file_scope(), pinfo,
                proto_get_id_by_filter_name(IEEE802154_PROTOABBREV_WPAN), 0);
    hint_panid = (hints) ? (hints->src_pan) : (IEEE802154_BCAST_PAN);

    /* Create a tree for the IPHC header. */
    iphc_tree = proto_tree_add_subtree(tree, tvb, 0, 2, ett_6lowpan_iphc, NULL, "IPHC Header");

    /* Display the pattern. */
    proto_tree_add_bits_item(iphc_tree, hf_6lowpan_pattern, tvb, 0, LOWPAN_PATTERN_IPHC_BITS, ENC_BIG_ENDIAN);

    /*=====================================================
     * Parse IPHC Header flags.
     *=====================================================
     */
    iphc_flags      = tvb_get_ntohs(tvb, offset);
    iphc_traffic    = (iphc_flags & LOWPAN_IPHC_FLAG_FLOW) >> LOWPAN_IPHC_FLAG_OFFSET_FLOW;
    iphc_hop_limit  = (iphc_flags & LOWPAN_IPHC_FLAG_HLIM) >> LOWPAN_IPHC_FLAG_OFFSET_HLIM;
    iphc_src_mode   = (iphc_flags & LOWPAN_IPHC_FLAG_SRC_MODE) >> LOWPAN_IPHC_FLAG_OFFSET_SRC_MODE;
    iphc_dst_mode   = (iphc_flags & LOWPAN_IPHC_FLAG_DST_MODE) >> LOWPAN_IPHC_FLAG_OFFSET_DST_MODE;
    if (tree) {
        const value_string *am_vs;
        proto_tree_add_uint         (iphc_tree, hf_6lowpan_iphc_flag_tf,    tvb, offset, 2, iphc_flags & LOWPAN_IPHC_FLAG_FLOW);
        proto_tree_add_boolean      (iphc_tree, hf_6lowpan_iphc_flag_nhdr,  tvb, offset, 2, iphc_flags & LOWPAN_IPHC_FLAG_NHDR);
        proto_tree_add_uint         (iphc_tree, hf_6lowpan_iphc_flag_hlim,  tvb, offset, 2, iphc_flags & LOWPAN_IPHC_FLAG_HLIM);
        proto_tree_add_boolean      (iphc_tree, hf_6lowpan_iphc_flag_cid,   tvb, offset, 2, iphc_flags & LOWPAN_IPHC_FLAG_CONTEXT_ID);
        proto_tree_add_boolean      (iphc_tree, hf_6lowpan_iphc_flag_sac,   tvb, offset, 2, iphc_flags & LOWPAN_IPHC_FLAG_SRC_COMP);
        am_vs = iphc_flags & LOWPAN_IPHC_FLAG_SRC_COMP ? lowpan_iphc_saddr_stateful_modes : lowpan_iphc_addr_modes;
        proto_tree_add_uint_format_value(iphc_tree, hf_6lowpan_iphc_flag_sam, tvb, offset, 2, iphc_flags & LOWPAN_IPHC_FLAG_SRC_MODE,
                                         "%s (0x%04x)", val_to_str_const(iphc_src_mode, am_vs, "Reserved"), iphc_src_mode);
        proto_tree_add_boolean      (iphc_tree, hf_6lowpan_iphc_flag_mcast, tvb, offset, 2, iphc_flags & LOWPAN_IPHC_FLAG_MCAST_COMP);
        proto_tree_add_boolean      (iphc_tree, hf_6lowpan_iphc_flag_dac,   tvb, offset, 2, iphc_flags & LOWPAN_IPHC_FLAG_DST_COMP);
        /* Destination address mode changes meanings depending on multicast compression. */
        if (iphc_flags & LOWPAN_IPHC_FLAG_MCAST_COMP) {
            if (iphc_flags & LOWPAN_IPHC_FLAG_DST_COMP) {
                am_vs = lowpan_iphc_mcast_stateful_modes;
            } else {
                am_vs = lowpan_iphc_mcast_modes;
            }
        } else {
            if (iphc_flags & LOWPAN_IPHC_FLAG_DST_COMP) {
                am_vs = lowpan_iphc_daddr_stateful_modes;
            } else {
                am_vs = lowpan_iphc_addr_modes;
            }
        }
        ti_dam = proto_tree_add_uint_format_value(iphc_tree, hf_6lowpan_iphc_flag_dam, tvb, offset, 2,
            iphc_flags & LOWPAN_IPHC_FLAG_DST_MODE, "%s (0x%04x)", val_to_str_const(iphc_dst_mode, am_vs, "Reserved"), iphc_dst_mode);
    }
    offset += 2;

    /* Display the context identifier extension, if present. */
    if (iphc_flags & LOWPAN_IPHC_FLAG_CONTEXT_ID) {
        iphc_ctx = tvb_get_uint8(tvb, offset);
        iphc_sci = (iphc_ctx & LOWPAN_IPHC_FLAG_SCI) >> LOWPAN_IPHC_FLAG_OFFSET_SCI;
        iphc_dci = (iphc_ctx & LOWPAN_IPHC_FLAG_DCI) >> LOWPAN_IPHC_FLAG_OFFSET_DCI;
        proto_tree_add_uint(iphc_tree, hf_6lowpan_iphc_sci, tvb, offset, 1, iphc_ctx & LOWPAN_IPHC_FLAG_SCI);
        proto_tree_add_uint(iphc_tree, hf_6lowpan_iphc_dci, tvb, offset, 1, iphc_ctx & LOWPAN_IPHC_FLAG_DCI);
        offset +=  1;
    }
    /* Use link-local contexts if stateless. */
    if (!(iphc_flags & LOWPAN_IPHC_FLAG_SRC_COMP)) {
        iphc_sci = LOWPAN_CONTEXT_LINK_LOCAL;
    }
    if (!(iphc_flags & LOWPAN_IPHC_FLAG_DST_COMP)) {
        iphc_dci = LOWPAN_CONTEXT_LINK_LOCAL;
    }
    /* Lookup the contexts. */
    /*
     * Don't display their origin until after we decompress the address in case
     * the address modes indicate that we should use a different context.
     */
    sctx = lowpan_context_find(iphc_sci, hint_panid);
    dctx = lowpan_context_find(iphc_dci, hint_panid);

    /*=====================================================
     * Parse Traffic Class and Flow Label
     *=====================================================
     */
    offset <<= 3;
    /* Parse the ECN field. */
    if (iphc_traffic != LOWPAN_IPHC_FLOW_COMPRESSED) {
        ipv6_ecn = tvb_get_bits8(tvb, offset, LOWPAN_IPHC_ECN_BITS);
        proto_tree_add_bits_item(tree, hf_6lowpan_ecn, tvb, offset, LOWPAN_IPHC_ECN_BITS, ENC_BIG_ENDIAN);
        offset += LOWPAN_IPHC_ECN_BITS;
    }
    /* Parse the DSCP field. */
    if ((iphc_traffic == LOWPAN_IPHC_FLOW_CLASS_LABEL) || (iphc_traffic == LOWPAN_IPHC_FLOW_CLASS)) {
        ipv6_dscp = tvb_get_bits8(tvb, offset, LOWPAN_IPHC_DSCP_BITS);
        proto_tree_add_bits_item(tree, hf_6lowpan_dscp, tvb, offset, LOWPAN_IPHC_DSCP_BITS, LOWPAN_IPHC_DSCP_BITS);
        offset += LOWPAN_IPHC_DSCP_BITS;
    }
    /* Add a generated entry to show the IPv6 traffic class byte. */
    if (ipv6_dscp || ipv6_ecn) {
        proto_item *tclass_item;
        tclass_item = proto_tree_add_uint(tree, hf_6lowpan_traffic_class, tvb, 0, 0,
                                          (ipv6_dscp << LOWPAN_IPHC_ECN_BITS) | ipv6_ecn);
        proto_item_set_generated(tclass_item);
    }

    /* Parse the flow label. */
    if ((iphc_traffic == LOWPAN_IPHC_FLOW_CLASS_LABEL) || (iphc_traffic == LOWPAN_IPHC_FLOW_ECN_LABEL)) {
        /* Pad to 4-bits past the start of the byte. */
        unsigned pad_bits = ((4 - offset) & 0x7);
        if (pad_bits) {
            proto_tree_add_bits_item(tree, hf_6lowpan_padding, tvb, offset, pad_bits, ENC_BIG_ENDIAN);
        }
        offset += pad_bits;
        ipv6_flowlabel = tvb_get_bits32(tvb, offset, LOWPAN_IPHC_LABEL_BITS, ENC_BIG_ENDIAN);
        proto_tree_add_bits_item(tree, hf_6lowpan_flow_label, tvb, offset, LOWPAN_IPHC_LABEL_BITS, ENC_BIG_ENDIAN);
        offset += LOWPAN_IPHC_LABEL_BITS;
    }

    /* Rebuild the IPv6 flow label, traffic class and version fields. */
    ipv6.ip6h_vc_flow = ipv6_flowlabel;
    ipv6.ip6h_vc_flow |= ((uint32_t)ipv6_ecn << LOWPAN_IPV6_FLOW_LABEL_BITS);
    ipv6.ip6h_vc_flow |= ((uint32_t)ipv6_dscp << (LOWPAN_IPHC_ECN_BITS + LOWPAN_IPV6_FLOW_LABEL_BITS));
    ipv6.ip6h_vc_flow |= ((uint32_t)0x6 << (LOWPAN_IPV6_TRAFFIC_CLASS_BITS + LOWPAN_IPV6_FLOW_LABEL_BITS));
    ipv6.ip6h_vc_flow = g_ntohl(ipv6.ip6h_vc_flow);

    /* Convert back to byte offsets. */
    offset >>= 3;

    /*=====================================================
     * Parse Next Header and Hop Limit
     *=====================================================
     */
    /* Get the next header field, if present. */
    if (!(iphc_flags & LOWPAN_IPHC_FLAG_NHDR)) {
        ipv6.ip6h_nxt = tvb_get_uint8(tvb, offset);
        proto_tree_add_uint_format_value(tree, hf_6lowpan_next_header, tvb, offset, 1, ipv6.ip6h_nxt,
                "%s (0x%02x)", ipprotostr(ipv6.ip6h_nxt), ipv6.ip6h_nxt);
        offset += 1;
    }

    /* Get the hop limit field, if present. */
    if (iphc_hop_limit == LOWPAN_IPHC_HLIM_1) {
        ipv6.ip6h_hlim = 1;
    }
    else if (iphc_hop_limit == LOWPAN_IPHC_HLIM_64) {
        ipv6.ip6h_hlim = 64;
    }
    else if (iphc_hop_limit == LOWPAN_IPHC_HLIM_255) {
        ipv6.ip6h_hlim = 255;
    }
    else {
        ipv6.ip6h_hlim = tvb_get_uint8(tvb, offset);
        proto_tree_add_uint(tree, hf_6lowpan_hop_limit, tvb, offset, 1, ipv6.ip6h_hlim);
        offset += 1;
    }

    /*=====================================================
     * Parse and decompress the source address.
     *=====================================================
     */
    length = 0;
    memset(&ipv6.ip6h_src, 0, sizeof(ipv6.ip6h_src));
    /* (SAC=1 && SAM=00) -> the unspecified address (::). */
    if ((iphc_flags & LOWPAN_IPHC_FLAG_SRC_COMP) && (iphc_src_mode == LOWPAN_IPHC_ADDR_SRC_UNSPEC)) {
        sctx = &lowpan_context_default;
    }
    /* The IID is derived from the encapsulating layer. */
    else if (iphc_src_mode == LOWPAN_IPHC_ADDR_COMPRESSED) {
        memcpy(&ipv6.ip6h_src.bytes[sizeof(ipv6.ip6h_src) - LOWPAN_IFC_ID_LEN], siid, LOWPAN_IFC_ID_LEN);
    }
    /* Full Address inline. */
    else if (iphc_src_mode == LOWPAN_IPHC_ADDR_FULL_INLINE) {
        if (!(iphc_flags & LOWPAN_IPHC_FLAG_SRC_COMP)) sctx = &lowpan_context_default;
        length = (int)sizeof(ipv6.ip6h_src);
        tvb_memcpy(tvb, &ipv6.ip6h_src, offset, length);
    }
    /* 64-bits inline. */
    else if (iphc_src_mode == LOWPAN_IPHC_ADDR_64BIT_INLINE) {
        length = 8;
        tvb_memcpy(tvb, &ipv6.ip6h_src.bytes[sizeof(ipv6.ip6h_src) - length], offset, length);
    }
    /* 16-bits inline. */
    else if (iphc_src_mode == LOWPAN_IPHC_ADDR_16BIT_INLINE) {
        length = 2;
        /* Format becomes ff:fe00:xxxx */
        ipv6.ip6h_src.bytes[11] = 0xff;
        ipv6.ip6h_src.bytes[12] = 0xfe;
        tvb_memcpy(tvb, &ipv6.ip6h_src.bytes[sizeof(ipv6.ip6h_src) - length], offset, length);

    }
    /* Copy the context bits. */
    lowpan_pfxcpy(&ipv6.ip6h_src, &sctx->prefix, sctx->plen);
    /* Update the IID of the encapsulating layer. */
    siid = &ipv6.ip6h_src.bytes[sizeof(ipv6.ip6h_src) - LOWPAN_IFC_ID_LEN];

    /* Display the source IPv6 address. */
    ti = proto_tree_add_ipv6(tree, hf_6lowpan_source, tvb, offset, length, &ipv6.ip6h_src);
    if (length == 0) {
        proto_item_set_generated(ti);
    }
    if (ipv6_summary_in_tree) {
        address src_addr = ADDRESS_INIT(AT_IPv6, sizeof(ipv6.ip6h_src), &ipv6.ip6h_src);
        proto_item_append_text(tree, ", Src: %s", address_with_resolution_to_str(pinfo->pool, &src_addr));
    }

    /* Add information about where the context came from. */
    /* TODO: We should display the prefix length too. */
    if (sctx->plen) {
        ti = proto_tree_add_ipv6(iphc_tree, hf_6lowpan_iphc_sctx_prefix, tvb, 0, 0, &sctx->prefix);
        proto_item_set_generated(ti);
        if ( sctx->frame ) {
            ti = proto_tree_add_uint(iphc_tree, hf_6lowpan_iphc_sctx_origin, tvb, 0, 0, sctx->frame);
            proto_item_set_generated(ti);
        }
    }
    offset += length;
    /*
     * Do not set the address columns until after defragmentation, since we have
     * to do decompression before reassembly, and changing the address will cause
     * wireshark to think that the middle fragments came from another device.
     */

    /*=====================================================
     * Parse and decompress a multicast address.
     *=====================================================
     */
    length = 0;
    memset(&ipv6.ip6h_dst, 0, sizeof(ipv6.ip6h_dst));
    /* Stateless multicast compression. */
    if ((iphc_flags & LOWPAN_IPHC_FLAG_MCAST_COMP) && !(iphc_flags & LOWPAN_IPHC_FLAG_DST_COMP)) {
        if (iphc_dst_mode == LOWPAN_IPHC_ADDR_FULL_INLINE) {
            length = (int)sizeof(ipv6.ip6h_dst);
            tvb_memcpy(tvb, &ipv6.ip6h_dst.bytes[sizeof(ipv6.ip6h_dst) - length], offset, length);
        }
        else if (iphc_dst_mode == LOWPAN_IPHC_MCAST_48BIT) {
            ipv6.ip6h_dst.bytes[0] = 0xff;
            ipv6.ip6h_dst.bytes[1] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[11] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[12] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[13] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[14] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[15] = tvb_get_uint8(tvb, offset + (length++));
        }
        else if (iphc_dst_mode == LOWPAN_IPHC_MCAST_32BIT) {
            ipv6.ip6h_dst.bytes[0] = 0xff;
            ipv6.ip6h_dst.bytes[1] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[13] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[14] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[15] = tvb_get_uint8(tvb, offset + (length++));
        }
        else if (iphc_dst_mode == LOWPAN_IPHC_MCAST_8BIT) {
            ipv6.ip6h_dst.bytes[0] = 0xff;
            ipv6.ip6h_dst.bytes[1] = 0x02;
            ipv6.ip6h_dst.bytes[15] = tvb_get_uint8(tvb, offset + (length++));
        }
        else {
            /* Illegal destination address compression mode. */
            expert_add_info(pinfo, ti_dam, &ei_6lowpan_illegal_dest_addr_mode);
            return NULL;
        }
    }
    /* Stateful multicast compression. */
    else if ((iphc_flags & LOWPAN_IPHC_FLAG_MCAST_COMP) && (iphc_flags & LOWPAN_IPHC_FLAG_DST_COMP)) {
        if (iphc_dst_mode == LOWPAN_IPHC_MCAST_STATEFUL_48BIT) {
            /* RFC 3306 unicast-prefix based multicast address of the form:
             *      ffXX:XXLL:PPPP:PPPP:PPPP:PPPP:XXXX:XXXX
             * XX = inline byte.
             * LL = prefix/context length (up to 64-bits).
             * PP = prefix/context byte.
             */
            ipv6.ip6h_dst.bytes[0] = 0xff;
            ipv6.ip6h_dst.bytes[1] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[2] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[3] = (dctx->plen > 64) ? (64) : (dctx->plen);
            memcpy(&ipv6.ip6h_dst.bytes[4], &dctx->prefix, 8);
            ipv6.ip6h_dst.bytes[12] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[13] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[14] = tvb_get_uint8(tvb, offset + (length++));
            ipv6.ip6h_dst.bytes[15] = tvb_get_uint8(tvb, offset + (length++));
        }
        else {
            /* Illegal destination address compression mode. */
            expert_add_info(pinfo, ti_dam, &ei_6lowpan_illegal_dest_addr_mode);
            return NULL;
        }
    }

    /*=====================================================
     * Parse and decompress a unicast destination address.
     *=====================================================
     */
    else {
        /* (DAC=1 && DAM=00) -> reserved value. */
        if ((iphc_flags & LOWPAN_IPHC_FLAG_DST_COMP) && (iphc_dst_mode == LOWPAN_IPHC_ADDR_FULL_INLINE)) {
            /* Illegal destination address compression mode. */
            expert_add_info(pinfo, ti_dam, &ei_6lowpan_illegal_dest_addr_mode);
            return NULL;
        }
        /* The IID is derived from the link-layer source. */
        else if (iphc_dst_mode == LOWPAN_IPHC_ADDR_COMPRESSED) {
            memcpy(&ipv6.ip6h_dst.bytes[sizeof(ipv6.ip6h_dst) - LOWPAN_IFC_ID_LEN], diid, LOWPAN_IFC_ID_LEN);
        }
        /* Full Address inline. */
        else if (iphc_dst_mode == LOWPAN_IPHC_ADDR_FULL_INLINE) {
            dctx = &lowpan_context_default;
            length = (int)sizeof(ipv6.ip6h_dst);
            tvb_memcpy(tvb, &ipv6.ip6h_dst, offset, length);
        }
        /* 64-bits inline. */
        else if (iphc_dst_mode == LOWPAN_IPHC_ADDR_64BIT_INLINE) {
            length = 8;
            tvb_memcpy(tvb, &ipv6.ip6h_dst.bytes[sizeof(ipv6.ip6h_dst) - length], offset, length);
        }
        /* 16-bits inline. */
        else if (iphc_dst_mode == LOWPAN_IPHC_ADDR_16BIT_INLINE) {
            length = 2;
            /* Format becomes ff:fe00:xxxx */
            ipv6.ip6h_dst.bytes[11] = 0xff;
            ipv6.ip6h_dst.bytes[12] = 0xfe;
            tvb_memcpy(tvb, &ipv6.ip6h_dst.bytes[sizeof(ipv6.ip6h_dst) - length], offset, length);
        }
        /* Copy the context bits. */
        lowpan_pfxcpy(&ipv6.ip6h_dst, &dctx->prefix, dctx->plen);
        /* Update the interface id of the encapsulating layer. */
        diid = &ipv6.ip6h_dst.bytes[sizeof(ipv6.ip6h_dst) - LOWPAN_IFC_ID_LEN];
    }

    /* Display the destination IPv6 address. */
    ti = proto_tree_add_ipv6(tree, hf_6lowpan_dest, tvb, offset, length, &ipv6.ip6h_dst);
    if (length == 0) {
        proto_item_set_generated(ti);
    }
    if (ipv6_summary_in_tree) {
        address dst_addr = ADDRESS_INIT(AT_IPv6, sizeof(ipv6.ip6h_dst), &ipv6.ip6h_dst);
        proto_item_append_text(tree, ", Dest: %s", address_with_resolution_to_str(pinfo->pool, &dst_addr));
    }

    /* Add information about where the context came from. */
    /* TODO: We should display the prefix length too. */
    if (dctx->plen) {
        ti = proto_tree_add_ipv6(iphc_tree, hf_6lowpan_iphc_dctx_prefix, tvb, 0, 0, &dctx->prefix);
        proto_item_set_generated(ti);
        if ( dctx->frame ) {
            ti = proto_tree_add_uint(iphc_tree, hf_6lowpan_iphc_dctx_origin, tvb, 0, 0, dctx->frame);
            proto_item_set_generated(ti);
        }
    }
    offset += length;
    /*
     * Do not set the address columns until after defragmentation, since we have
     * to do decompression before reassembly, and changing the address will cause
     * wireshark to think that the middle fragments came from another device.
     */

    /*=====================================================
     * Decompress extension headers.
     *=====================================================
     */
    /* Parse the list of extension headers. */
    if (iphc_flags & LOWPAN_IPHC_FLAG_NHDR) {
        /* Parse the next header protocol identifier. */
        ipv6.ip6h_nxt = lowpan_parse_nhc_proto(tvb, offset);

        /* Parse the 6LoWPAN NHC fields. */
        nhdr_list = dissect_6lowpan_iphc_nhc(tvb, pinfo, tree, offset, dgram_size - IPv6_HDR_SIZE, siid, diid);
    }
    /* Create an extension header for the remaining payload. */
    else {
        length = tvb_captured_length_remaining(tvb, offset);
        nhdr_list = (struct lowpan_nhdr *)wmem_alloc(pinfo->pool, sizeof(struct lowpan_nhdr) + length);
        nhdr_list->next = NULL;
        nhdr_list->proto = ipv6.ip6h_nxt;
        nhdr_list->length = length;
        if (dgram_size < 0) {
            nhdr_list->reported = tvb_reported_length_remaining(tvb, offset);
        }
        else {
            nhdr_list->reported = dgram_size - IPv6_HDR_SIZE;
        }
        tvb_memcpy(tvb, LOWPAN_NHDR_DATA(nhdr_list), offset, nhdr_list->length);
    }

    /*=====================================================
     * Rebuild the IPv6 packet.
     *=====================================================
     */
    /* Reassemble the IPv6 packet. */
    ipv6_tvb = lowpan_reassemble_ipv6(tvb, pinfo, &ipv6, nhdr_list);

    /* Add a new data source for it. */
    add_new_data_source(pinfo, ipv6_tvb, "Decompressed 6LoWPAN IPHC");

    return ipv6_tvb;
} /* dissect_6lowpan_iphc */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_iphc_nhc
 *  DESCRIPTION
 *      Dissector routine for a 6LoWPAN IPHC next header structure(s).
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *      offset          ; packet buffer offset.
 *      dgram_size      ; Remaining datagram size (or <0 if unknown).
 *      siid            ; Source Interface ID.
 *      diid            ; Destination Interface ID.
 *  RETURNS
 *      lowpan_nhdr *   ; List of wmem_alloc'd next header structures.
 *---------------------------------------------------------------
 */
static struct lowpan_nhdr *
// NOLINTNEXTLINE(misc-no-recursion)
dissect_6lowpan_iphc_nhc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, int dgram_size, const uint8_t *siid, const uint8_t *diid)
{
    int                 length;
    proto_item *        ti = NULL;
    proto_tree *        nhc_tree = NULL;
    struct lowpan_nhdr *nhdr;

    /*=====================================================
     * IP-in-IP Tunneling
     *=====================================================
     */
    if (tvb_get_bits8(tvb, offset<<3, LOWPAN_NHC_PATTERN_EXT_IPV6_BITS) == LOWPAN_NHC_PATTERN_EXT_IPV6) {
        uint8_t         ext_flags;
        tvbuff_t       *iphc_tvb;

        /* Create a tree for the IPv6 extension header. */
        nhc_tree = proto_tree_add_subtree(tree, tvb, offset, 2, ett_6lowpan_nhc_ext, &ti, "IPv6 extension header");
        /* Display the IPv6 Extension Header NHC ID pattern. */
        proto_tree_add_bits_item(nhc_tree, hf_6lowpan_nhc_pattern, tvb, offset<<3, LOWPAN_NHC_PATTERN_EXT_BITS, ENC_BIG_ENDIAN);

        /* Get and display the extension header compression flags. */
        ext_flags = tvb_get_uint8(tvb, offset);
        proto_tree_add_uint(nhc_tree, hf_6lowpan_nhc_ext_eid, tvb, offset, 1, ext_flags & LOWPAN_NHC_EXT_EID);
        proto_tree_add_boolean(nhc_tree, hf_6lowpan_nhc_ext_nh, tvb, offset, 1, ext_flags & LOWPAN_NHC_EXT_NHDR);
        if (ext_flags & LOWPAN_NHC_EXT_NHDR) {
            /* TODO: Flag a warning, the NH bit MUST be 0 when EID==0x7 (IP-in-IP). */
        }
        offset += 1;

        /* Decode the remainder of the packet using IPHC encoding. */
        increment_dissection_depth(pinfo);
        iphc_tvb = dissect_6lowpan_iphc(tvb_new_subset_remaining(tvb, offset), pinfo, tree, dgram_size, siid, diid);
        decrement_dissection_depth(pinfo);

        if (!iphc_tvb) return NULL;

        /* Create the next header structure for the tunneled IPv6 header. */
        nhdr = (struct lowpan_nhdr *)wmem_alloc0(pinfo->pool, sizeof(struct lowpan_nhdr) + tvb_captured_length(iphc_tvb));
        nhdr->next = NULL;
        nhdr->proto = IP_PROTO_IPV6;
        nhdr->length = tvb_captured_length(iphc_tvb);
        nhdr->reported = tvb_reported_length(iphc_tvb);
        tvb_memcpy(iphc_tvb, LOWPAN_NHDR_DATA(nhdr), 0, nhdr->length);
        return nhdr;
    }
    /*=====================================================
     * IPv6 Extension Header
     *=====================================================
     */
    if (tvb_get_bits8(tvb, offset<<3, LOWPAN_NHC_PATTERN_EXT_BITS) == LOWPAN_NHC_PATTERN_EXT) {
        struct ws_ip6_ext  ipv6_ext = {0, 0};
        uint8_t         ext_flags;
        uint8_t         ext_hlen;
        uint8_t         ext_len;
        uint8_t         ext_proto;
        proto_item      *ti_ext_len = NULL;

        /* Parse the IPv6 extension header protocol. */
        ext_proto = lowpan_parse_nhc_proto(tvb, offset);

        /* Create a tree for the IPv6 extension header. */
        nhc_tree = proto_tree_add_subtree(tree, tvb, offset, 2, ett_6lowpan_nhc_ext, NULL, "IPv6 extension header");
        /* Display the IPv6 Extension Header NHC ID pattern. */
        proto_tree_add_bits_item(nhc_tree, hf_6lowpan_nhc_pattern, tvb, offset<<3, LOWPAN_NHC_PATTERN_EXT_BITS, ENC_BIG_ENDIAN);

        /* Get and display the extension header compression flags. */
        ext_flags = tvb_get_uint8(tvb, offset);
        proto_tree_add_uint(nhc_tree, hf_6lowpan_nhc_ext_eid, tvb, offset, 1, ext_flags & LOWPAN_NHC_EXT_EID);
        proto_tree_add_boolean(nhc_tree, hf_6lowpan_nhc_ext_nh, tvb, offset, 1, ext_flags & LOWPAN_NHC_EXT_NHDR);
        offset += 1;

        /* Get and display the next header field, if present. */
        if (!(ext_flags & LOWPAN_NHC_EXT_NHDR)) {
            ipv6_ext.ip6e_nxt = tvb_get_uint8(tvb, offset);
            proto_tree_add_uint_format_value(nhc_tree, hf_6lowpan_nhc_ext_next, tvb, offset, 1, ipv6_ext.ip6e_nxt,
                    "%s (0x%02x)", ipprotostr(ipv6_ext.ip6e_nxt), ipv6_ext.ip6e_nxt);
            proto_item_set_end(ti, tvb, offset+1);
            offset += 1;
        }

        if (ext_proto == IP_PROTO_FRAGMENT) {
            /* Fragment header has a reserved byte in place of the Length field. */
            ext_hlen = 1;
            length = (uint8_t)sizeof(struct ws_ip6_frag);
            ext_len = length - ext_hlen;

            proto_tree_add_item(nhc_tree, hf_6lowpan_nhc_ext_reserved, tvb, offset, 1, ENC_NA);

        } else {
            /* Get and display the extension header length. */
            ext_hlen = (uint8_t)sizeof(struct ws_ip6_ext);
            ext_len = tvb_get_uint8(tvb, offset);
            ti_ext_len = proto_tree_add_uint(nhc_tree, hf_6lowpan_nhc_ext_length, tvb, offset, 1, ext_len);
            offset += 1;

            /* Compute the length of the extension header padded to an 8-byte alignment. */
            length = ext_hlen + ext_len;
            length = (length + 7) & ~0x7;
            ipv6_ext.ip6e_len = length>>3;          /* Convert to units of 8 bytes. */
            ipv6_ext.ip6e_len -= 1;                 /* Don't include the first 8 bytes. */
       }

        /* Create the next header structure for the IPv6 extension header. */
        nhdr = (struct lowpan_nhdr *)wmem_alloc0(pinfo->pool, sizeof(struct lowpan_nhdr) + length);
        nhdr->next = NULL;
        nhdr->proto = ext_proto;
        nhdr->length = length;
        nhdr->reported = length;

        /* Add the IPv6 extension header to the buffer. */
        if (ext_flags & LOWPAN_NHC_EXT_NHDR) {
            ipv6_ext.ip6e_nxt = lowpan_parse_nhc_proto(tvb, offset+ext_len);
        }
        memcpy(LOWPAN_NHDR_DATA(nhdr), &ipv6_ext, ext_hlen);

        /*
         * If the extension header was truncated, display the remainder using
         * the data dissector, and end NHC dissection here.
         */
        if (!tvb_bytes_exist(tvb, offset, ext_len)) {
            /* Call the data dissector for the remainder. */
            call_data_dissector(tvb_new_subset_remaining(tvb, offset), pinfo, nhc_tree);

            /* Copy the remainder, and truncate the real buffer length. */
            nhdr->length = tvb_captured_length_remaining(tvb, offset) + ext_hlen;
            tvb_memcpy(tvb, LOWPAN_NHDR_DATA(nhdr) + ext_hlen, offset, tvb_captured_length_remaining(tvb, offset));

            /* There is nothing more we can do. */
            return nhdr;
        }

        if (ext_proto == IP_PROTO_FRAGMENT) {
            /* Display the extension header using the data dissector. */
            call_data_dissector(tvb_new_subset_length(tvb, offset+1, ext_len-1), pinfo, nhc_tree);
        } else {
            /* Display the extension header using the data dissector. */
            call_data_dissector(tvb_new_subset_length(tvb, offset, ext_len), pinfo, nhc_tree);
        }

        /* Copy the extension header into the struct. */
        tvb_memcpy(tvb, LOWPAN_NHDR_DATA(nhdr) + ext_hlen, offset, ext_len);
        offset += ext_len;

        /* Add padding option */
        if (length > ext_hlen + ext_len) {
            uint8_t padding = length - (ext_hlen + ext_len);
            uint8_t *pad_ptr = LOWPAN_NHDR_DATA(nhdr) + ext_hlen + ext_len;
            if (ext_proto != IP_PROTO_HOPOPTS && ext_proto != IP_PROTO_DSTOPTS) {
                expert_add_info(pinfo, ti_ext_len, &ei_6lowpan_bad_ext_header_length);
            }
            if (padding == 1) {
                pad_ptr[0] = IP6OPT_PAD1;
            } else {
                pad_ptr[0] = IP6OPT_PADN;
                pad_ptr[1] = padding - 2;
                /* No need to write pad data, as buffer is zero-initialised */
            }
        }

        if (ext_flags & LOWPAN_NHC_EXT_NHDR) {
            /*
             * There are more LOWPAN_NHC structures to parse. Call ourself again
             * recursively to parse them and build the linked list.
             */
            increment_dissection_depth(pinfo);
            nhdr->next = dissect_6lowpan_iphc_nhc(tvb, pinfo, tree, offset, dgram_size - nhdr->reported, siid, diid);
            decrement_dissection_depth(pinfo);
        }
        else if (ipv6_ext.ip6e_nxt != IP_PROTO_NONE) {
            /* Create another next header structure for the remaining payload. */
            length = tvb_captured_length_remaining(tvb, offset);
            nhdr->next = (struct lowpan_nhdr *)wmem_alloc(pinfo->pool, sizeof(struct lowpan_nhdr) + length);
            nhdr->next->next = NULL;
            nhdr->next->proto = ipv6_ext.ip6e_nxt;
            nhdr->next->length = length;
            if (dgram_size < 0) {
                nhdr->next->reported = tvb_reported_length_remaining(tvb, offset);
            }
            else {
                nhdr->next->reported = dgram_size - nhdr->reported;
            }
            tvb_memcpy(tvb, LOWPAN_NHDR_DATA(nhdr->next), offset, nhdr->next->length);
        }

        /* Done. */
        return nhdr;
    }
    /*=====================================================
     * UDP Header
     *=====================================================
     */
    if (tvb_get_bits8(tvb, offset<<3, LOWPAN_NHC_PATTERN_UDP_BITS) == LOWPAN_NHC_PATTERN_UDP) {
        struct udp_hdr  udp;
        int             src_bitlen;
        int             dst_bitlen;
        uint8_t         udp_flags;
        uint16_t        udp_src_port, udp_dst_port;

        /* Create a tree for the UDP header. */
        nhc_tree = proto_tree_add_subtree(tree, tvb, offset, 1, ett_6lowpan_nhc_udp, NULL, "UDP header compression");
        /* Display the UDP NHC ID pattern. */
        proto_tree_add_bits_item(nhc_tree, hf_6lowpan_nhc_pattern, tvb, offset<<3, LOWPAN_NHC_PATTERN_UDP_BITS, ENC_BIG_ENDIAN);

        /* Get and display the UDP header compression options */
        proto_tree_add_item(nhc_tree, hf_6lowpan_nhc_udp_checksum, tvb, offset, 1, ENC_NA);
        proto_tree_add_item(nhc_tree, hf_6lowpan_nhc_udp_ports, tvb, offset, 1, ENC_NA);
        udp_flags = tvb_get_uint8(tvb, offset);
        offset += 1;

        /* Get and display the ports. */
        switch (udp_flags & LOWPAN_NHC_UDP_PORTS) {
            case LOWPAN_NHC_UDP_PORT_INLINE:
                udp_src_port = tvb_get_ntohs(tvb, offset);
                udp_dst_port = tvb_get_ntohs(tvb, offset+2);
                src_bitlen = 16;
                dst_bitlen = 16;
                break;

            case LOWPAN_NHC_UDP_PORT_8BIT_DST:
                udp_src_port = tvb_get_ntohs(tvb, offset);
                udp_dst_port = LOWPAN_PORT_8BIT_OFFSET + tvb_get_uint8(tvb, offset + 2);
                src_bitlen = 16;
                dst_bitlen = 8;
                break;

            case LOWPAN_NHC_UDP_PORT_8BIT_SRC:
                udp_src_port = LOWPAN_PORT_8BIT_OFFSET + tvb_get_uint8(tvb, offset);
                udp_dst_port = tvb_get_ntohs(tvb, offset + 1);
                src_bitlen = 8;
                dst_bitlen = 16;
                break;

            case LOWPAN_NHC_UDP_PORT_12BIT:
                udp_src_port = LOWPAN_PORT_12BIT_OFFSET + (tvb_get_uint8(tvb, offset) >> 4);
                udp_dst_port = LOWPAN_PORT_12BIT_OFFSET + (tvb_get_uint8(tvb, offset) & 0x0f);
                src_bitlen = 4;
                dst_bitlen = 4;
                break;

            default:
                DISSECTOR_ASSERT_NOT_REACHED();
                break;
        } /* switch */

        proto_tree_add_uint(tree, hf_6lowpan_udp_src, tvb, offset, BITS_TO_BYTE_LEN(offset<<3, src_bitlen), udp_src_port);
        proto_tree_add_uint(tree, hf_6lowpan_udp_dst, tvb, offset+(src_bitlen>>3), BITS_TO_BYTE_LEN((offset<<3)+src_bitlen, dst_bitlen), udp_dst_port);
        offset += ((src_bitlen + dst_bitlen)>>3);
        udp.src_port = g_htons(udp_src_port);
        udp.dst_port = g_htons(udp_dst_port);

        /* Get and display the checksum. */
        if (!(udp_flags & LOWPAN_NHC_UDP_CHECKSUM)) {
            /* Parse the checksum. */
            tvb_memcpy(tvb, &udp.checksum, offset, sizeof(udp.checksum));
            proto_tree_add_checksum(tree, tvb, offset, hf_6lowpan_udp_checksum, -1, NULL, pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
            offset += 2;
        }
        else {
            /* Checksum must be != 0 or the UDP dissector will flag the packet with a PI_ERROR */
            udp.checksum = 0xffff;
        }

        /* Compute the datagram length. */
        if (dgram_size < 0) {
            length = tvb_reported_length_remaining(tvb, offset);
            udp.length = g_htons(length + (int)sizeof(struct udp_hdr));
        }
        else {
            udp.length = g_htons(dgram_size);
        }

        /*
         * Although rfc768 (udp) allows a packet to be sent with a checksum of
         * 0 to mean that no checksum was computed, apparently IPv6 specifically
         * disallows sending UDP datagrams without checksums. Likewise, 6LoWPAN
         * requires that we recompute the checksum.
         *
         * If the datagram is incomplete, then leave the checksum at 0xffff.
         */
#if 0
        /*
         * This has been disabled, since we might only be dissecting a fragment
         * of the packet, and thus we might not have the entire UDP payload at
         * this time.
         *
         * If we want to display the checksums, they will have to be recomputed
         * after packet reassembly. Lots of work for not much gain, since we can
         * just set the UDP checksum to 0xffff (anything != 0) and Wireshark
         * doesn't care.
         */
        if ((udp_flags & LOWPAN_NHC_UDP_CHECKSUM) && tvb_bytes_exist(tvb, offset, length)) {
            vec_t      cksum_vec[3];
            struct {
                ws_in6_addr   src;
                ws_in6_addr   dst;
                uint32_t            length;
                uint8_t             zero[3];
                uint8_t             proto;
            } cksum_phdr;

            /* Fill in the pseudo-header. */
            memcpy(&cksum_phdr.src, pinfo->src.data, sizeof(ws_in6_addr));
            memcpy(&cksum_phdr.dst, pinfo->dst.data, sizeof(ws_in6_addr));
            cksum_phdr.length = g_htonl(length + (int)sizeof(struct udp_hdr));
            memset(cksum_phdr.zero, 0, sizeof(cksum_phdr.zero));
            cksum_phdr.proto = IP_PROTO_UDP;

            /* Compute the checksum. */
            SET_CKSUM_VEC_PTR(cksum_vec[0], (const uint8_t *)&cksum_phdr, sizeof(cksum_phdr));
            SET_CKSUM_VEC_PTR(cksum_vec[1], (const uint8_t *)&udp, sizeof(struct udp_hdr));
            SET_CKSUM_VEC_TVB(cksum_vec[2], tvb, offset, length);
            udp.checksum = in_cksum(cksum_vec, 3);
            if (udp.checksum == 0) udp.checksum = 0xffff;
        }
#endif

        /* Create the next header structure for the UDP datagram. */
        length = tvb_captured_length_remaining(tvb, offset);
        nhdr = (struct lowpan_nhdr *)wmem_alloc(pinfo->pool, sizeof(struct lowpan_nhdr) + sizeof(struct udp_hdr) + length);
        nhdr->next = NULL;
        nhdr->proto = IP_PROTO_UDP;
        nhdr->length = length + (int)sizeof(struct udp_hdr);
        nhdr->reported = g_ntohs(udp.length);

        /* Copy the UDP header and payload into the buffer. */
        memcpy(LOWPAN_NHDR_DATA(nhdr), &udp, sizeof(struct udp_hdr));
        tvb_memcpy(tvb, LOWPAN_NHDR_DATA(nhdr) + sizeof(struct udp_hdr), offset, tvb_captured_length_remaining(tvb, offset));
        return nhdr;
    }
    /*=====================================================
     * Unknown Next Header Type
     *=====================================================
     */
    return NULL;
} /* dissect_6lowpan_iphc_nhc */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_bc0
 *  DESCRIPTION
 *      Dissector routine for a 6LoWPAN broadcast header.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *  RETURNS
 *      tvbuff_t *      ; The remaining payload to be parsed.
 *---------------------------------------------------------------
 */
static tvbuff_t *
dissect_6lowpan_bc0(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
{
    uint8_t             seqnum;
    proto_tree *        bcast_tree;

    /* Create a tree for the broadcast header. */
    bcast_tree = proto_tree_add_subtree(tree, tvb, 0, 2, ett_6lowpan_bcast, NULL, "Broadcast Header");

    /* Get and display the pattern. */
    proto_tree_add_bits_item(bcast_tree, hf_6lowpan_pattern, tvb, 0, LOWPAN_PATTERN_BC0_BITS, ENC_BIG_ENDIAN);

    /* Get and display the sequence number. */
    seqnum = tvb_get_uint8(tvb, 1);
    proto_tree_add_uint(bcast_tree, hf_6lowpan_bcast_seqnum, tvb, 1, 1, seqnum);

    /* Return the remaining buffer. */
    return tvb_new_subset_remaining(tvb, 2);
} /* dissect_6lowpan_bc0 */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_mesh
 *  DESCRIPTION
 *      Dissector routine for a 6LoWPAN mesh header.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *      offset          ; offset to the start of the header.
 *      siid            ; Source Interface ID.
 *      diid            ; Destination Interface ID.
 *  RETURNS
 *      tvbuff_t *      ; The remaining payload to be parsed.
 *---------------------------------------------------------------
 */
static tvbuff_t *
dissect_6lowpan_mesh(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, uint8_t *siid, uint8_t *diid)
{
    int                 offset = 0;
    uint8_t             mesh_header;
    proto_tree *        mesh_tree;
    proto_tree *        flag_tree;
    proto_item *        ti;

    ieee802154_hints_t  *hints;

    /* Create a tree for the mesh header. */
    mesh_tree = proto_tree_add_subtree(tree, tvb, offset, 0, ett_6lowpan_mesh, &ti, "Mesh Header");

    /* Get and display the mesh flags. */
    mesh_header = tvb_get_uint8(tvb, offset);

    /*  Create the mesh header subtree. */
    flag_tree = proto_tree_add_subtree(mesh_tree, tvb, offset, 1, ett_6lowpan_mesh, NULL, "Flags");

    /* Add the mesh header fields. */
    proto_tree_add_bits_item(flag_tree, hf_6lowpan_pattern, tvb, offset * 8, LOWPAN_PATTERN_MESH_BITS, ENC_BIG_ENDIAN);
    proto_tree_add_boolean(flag_tree, hf_6lowpan_mesh_v, tvb, offset, 1, mesh_header & LOWPAN_MESH_HEADER_V);
    proto_tree_add_boolean(flag_tree, hf_6lowpan_mesh_f, tvb, offset, 1, mesh_header & LOWPAN_MESH_HEADER_F);
    proto_tree_add_uint(flag_tree, hf_6lowpan_mesh_hops, tvb, offset, 1, mesh_header & LOWPAN_MESH_HEADER_HOPS);
    offset += 1;

    if ((mesh_header & LOWPAN_MESH_HEADER_HOPS) == LOWPAN_MESH_HEADER_HOPS) {
        proto_tree_add_item(mesh_tree, hf_6lowpan_mesh_hops8, tvb, offset, 1, ENC_BIG_ENDIAN);
        offset += 1;
    }

    /* Get and display the originator address. */
    if (!(mesh_header & LOWPAN_MESH_HEADER_V)) {
        proto_tree_add_item(mesh_tree, hf_6lowpan_mesh_orig64,
                tvb, offset, 8, ENC_BIG_ENDIAN);

        set_address_tvb(&pinfo->src, AT_EUI64, 8, tvb, offset);
        copy_address_shallow(&pinfo->net_src, &pinfo->src);

        /* Update source IID */
        tvb_memcpy(tvb, siid, offset, LOWPAN_IFC_ID_LEN);
        /* RFC2464: Invert the U/L bit when using an EUI64 address. */
        siid[0] ^= 0x02;
        offset += 8;
    }
    else {
        uint16_t        addr16 = tvb_get_ntohs(tvb, offset);
        uint8_t *        ifcid;

        proto_tree_add_uint(mesh_tree, hf_6lowpan_mesh_orig16, tvb, offset, 2, addr16);
        ifcid = (uint8_t *)wmem_alloc(pinfo->pool, 8);

        /* Lookup the IEEE 802.15.4 addressing hints wanting RFC 2464 compatibility. */
        hints = (ieee802154_hints_t *)p_get_proto_data(wmem_file_scope(), pinfo,
                                proto_get_id_by_filter_name(IEEE802154_PROTOABBREV_WPAN), 0);

        /* Convert the 16-bit short address to an IID using the PAN ID (RFC 4944) or not depending on the preference and the presence of hints from lower layers */
        if (hints && rfc4944_short_address_format) {
            lowpan_addr16_with_panid_to_ifcid(hints->src_pan, addr16, ifcid);
        } else {
            lowpan_addr16_to_ifcid(addr16, ifcid);
        }

        set_address(&pinfo->src,  AT_EUI64, 8, ifcid);
        copy_address_shallow(&pinfo->net_src, &pinfo->src);

        /* Update source IID */
        memcpy(siid, ifcid, LOWPAN_IFC_ID_LEN);
        offset += 2;
    }

    /* Get and display the destination address. */
    if (!(mesh_header & LOWPAN_MESH_HEADER_F)) {
        proto_tree_add_item(mesh_tree, hf_6lowpan_mesh_dest64,
                tvb, offset, 8, ENC_BIG_ENDIAN);

        set_address_tvb(&pinfo->dst, AT_EUI64, 8, tvb, offset);
        copy_address_shallow(&pinfo->net_dst, &pinfo->dst);

        /* Update destination IID */
        tvb_memcpy(tvb, diid, offset, LOWPAN_IFC_ID_LEN);
        /* RFC2464: Invert the U/L bit when using an EUI64 address. */
        diid[0] ^= 0x02;
        offset += 8;
    }
    else  {
        uint16_t        addr16 = tvb_get_ntohs(tvb, offset);
        uint8_t *        ifcid;

        proto_tree_add_uint(mesh_tree, hf_6lowpan_mesh_dest16, tvb, offset, 2, addr16);

        ifcid = (uint8_t *)wmem_alloc(pinfo->pool, 8);

        /* Lookup the IEEE 802.15.4 addressing hints wanting RFC 2464 compatibility. */
        hints = (ieee802154_hints_t *)p_get_proto_data(wmem_file_scope(), pinfo,
                                proto_get_id_by_filter_name(IEEE802154_PROTOABBREV_WPAN), 0);

        /* Convert the 16-bit short address to an IID using the PAN ID (RFC 4944) or not depending on the preference and the presence of hints from lower layers */
        if (hints && rfc4944_short_address_format) {
            lowpan_addr16_with_panid_to_ifcid(hints->src_pan, addr16, ifcid);
        } else {
            lowpan_addr16_to_ifcid(addr16, ifcid);
        }

        set_address(&pinfo->dst,  AT_EUI64, 8, ifcid);
        copy_address_shallow(&pinfo->net_dst, &pinfo->dst);

        /* Update destination IID */
        memcpy(diid, ifcid, LOWPAN_IFC_ID_LEN);
        offset += 2;
    }

    /* Adjust the mesh header length. */
    proto_item_set_end(ti, tvb, offset);

    /* Return the remaining buffer. */
    return tvb_new_subset_remaining(tvb, offset);
} /* dissect_6lowpan_mesh */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_frag_headers
 *  DESCRIPTION
 *      Dissector routine for headers in the first fragment.
 *      The first fragment can contain an uncompressed IPv6, HC1 or IPHC fragment.
 *  PARAMETERS
 *      tvb             ; fragment buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *      siid            ; Source Interface ID.
 *      diid            ; Destination Interface ID.
 *  RETURNS
 *      tvbuff_t *      ; buffer containing the uncompressed IPv6 headers
 *---------------------------------------------------------------
 */
static tvbuff_t *
dissect_6lowpan_frag_headers(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, proto_item *length_item, const uint8_t *siid, const uint8_t *diid)
{
    tvbuff_t *frag_tvb = NULL;

    /* The first fragment can contain an uncompressed IPv6, HC1 or IPHC fragment.  */
    if (tvb_get_bits8(tvb, 0, LOWPAN_PATTERN_IPV6_BITS) == LOWPAN_PATTERN_IPV6) {
        frag_tvb = dissect_6lowpan_ipv6(tvb, pinfo, tree);
    }
    else if (tvb_get_bits8(tvb, 0, LOWPAN_PATTERN_HC1_BITS) == LOWPAN_PATTERN_HC1) {
        /* Check if the datagram size is sane. */
        if (tvb_reported_length(tvb) < IPv6_HDR_SIZE) {
            expert_add_info_format(pinfo, length_item, &ei_6lowpan_bad_ipv6_header_length,
                "Length is less than IPv6 header length %u", IPv6_HDR_SIZE);
        }
        frag_tvb = dissect_6lowpan_hc1(tvb, pinfo, tree, tvb_reported_length(tvb), siid, diid);
    }
    else if (tvb_get_bits8(tvb, 0, LOWPAN_PATTERN_IPHC_BITS) == LOWPAN_PATTERN_IPHC) {
        /* Check if the datagram size is sane. */
        if (tvb_reported_length(tvb) < IPv6_HDR_SIZE) {
            expert_add_info_format(pinfo, length_item, &ei_6lowpan_bad_ipv6_header_length,
                "Length is less than IPv6 header length %u", IPv6_HDR_SIZE);
        }
        frag_tvb = dissect_6lowpan_iphc(tvb, pinfo, tree, tvb_reported_length(tvb), siid, diid);
    }
    /* Unknown 6LoWPAN dispatch type */
    else {
        dissect_6lowpan_unknown(tvb, pinfo, tree);
    }
    return frag_tvb;
} /* dissect_6lowpan_frag_headers */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_rfrag
 *  DESCRIPTION
 *      Dissector routine for a 6LoWPAN Recoverable Fragment headers.
 *
 *      If reassembly could be completed, this should return an
 *      uncompressed IPv6 packet. If reassembly had to be delayed
 *      for more packets, this will return NULL.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *      siid            ; Source Interface ID.
 *      diid            ; Destination Interface ID.
 *  RETURNS
 *      tvbuff_t *      ; reassembled IPv6 packet.
 *---------------------------------------------------------------
 */
static tvbuff_t *
dissect_6lowpan_rfrag(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, const uint8_t *siid, const uint8_t *diid)
{
    int                 offset = 0;
    uint32_t            frag_size;
    uint32_t            dgram_tag;
    proto_tree *        frag_tree;
    proto_item *        ti;
    proto_item *        length_item;
    /* Reassembly parameters. */
    tvbuff_t *          new_tvb;
    tvbuff_t *          frag_tvb;
    fragment_head *     frag_data;
    bool                save_fragmented;
    uint16_t            sequence;
    uint32_t            frag_offset;

    /* Create a tree for the fragmentation header. */
    frag_tree = proto_tree_add_subtree(tree, tvb, offset, 0, ett_6lowpan_frag, &ti, "RFRAG Header");

    /* Get and display the pattern and explicit congestion bit. */
    proto_tree_add_bits_item(frag_tree, hf_6lowpan_pattern, tvb, offset * 8, LOWPAN_PATTERN_RFRAG_BITS, ENC_BIG_ENDIAN);
    proto_tree_add_item(frag_tree, hf_6lowpan_rfrag_congestion, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset += 1;

    /* Get and display the datagram tag. */
    proto_tree_add_item_ret_uint(frag_tree, hf_6lowpan_rfrag_dgram_tag, tvb, offset, 1, ENC_BIG_ENDIAN, &dgram_tag);
    offset += 1;

    proto_tree_add_item(frag_tree, hf_6lowpan_rfrag_ack_requested, tvb, offset, 2, ENC_BIG_ENDIAN);
    sequence = tvb_get_bits16(tvb, (offset * 8) + 1, LOWPAN_RFRAG_SEQUENCE_BITS, ENC_BIG_ENDIAN);
    proto_tree_add_item(frag_tree, hf_6lowpan_rfrag_sequence, tvb, offset, 2, ENC_BIG_ENDIAN);

    frag_size = tvb_get_bits16(tvb, (offset * 8) + 1 + LOWPAN_RFRAG_SEQUENCE_BITS, LOWPAN_RFRAG_FRAG_SZ_BITS, ENC_BIG_ENDIAN);
    length_item = proto_tree_add_uint(frag_tree, hf_6lowpan_rfrag_size, tvb, offset * 8, 2, frag_size);
    offset += 2;

    if (sequence) {
        proto_tree_add_item_ret_uint(frag_tree, hf_6lowpan_rfrag_offset, tvb, offset, 2, ENC_BIG_ENDIAN, &frag_offset);
    }
    else {
        proto_tree_add_item_ret_uint(frag_tree, hf_6lowpan_rfrag_dgram_size, tvb, offset, 2, ENC_BIG_ENDIAN, &frag_offset);
    }
    offset += 2;

    /* Adjust the fragmentation header length. */
    proto_item_set_end(ti, tvb, offset);

    frag_tvb = tvb_new_subset_length(tvb, offset, frag_size);
    if (sequence == 0) {
        dissect_6lowpan_frag_headers(frag_tvb, pinfo, tree, length_item, siid, diid);
    }

    /* Add this datagram to the fragment table. */
    save_fragmented = pinfo->fragmented;
    pinfo->fragmented = true;
    uint32_t frag_id = lowpan_reassembly_id(pinfo, dgram_tag);
    if (sequence == 0) {
        frag_data = fragment_add_check(&lowpan_reassembly_table,
                    frag_tvb, 0, pinfo, frag_id, NULL,
                    0, frag_size, true);
        fragment_set_tot_len(&lowpan_reassembly_table, pinfo, frag_id, NULL, frag_offset);
    }
    else {
        uint32_t dgram_size = fragment_get_tot_len(&lowpan_reassembly_table, pinfo, frag_id, NULL);
        frag_data = fragment_add_check(&lowpan_reassembly_table,
                    frag_tvb, 0, pinfo, frag_id, NULL,
                    frag_offset, frag_size, (frag_offset+frag_size) < dgram_size);
    }

    /* Attempt reassembly. */
    new_tvb = process_reassembled_data(frag_tvb, 0, pinfo,
                    "Reassembled 6LoWPAN", frag_data, &lowpan_frag_items,
                    NULL, tree);

    pinfo->fragmented = save_fragmented;

    if (new_tvb) {
        /* Reassembly was successful; return the completed datagram. */
        return new_tvb;
    } else {
        /* Reassembly was unsuccessful; show this fragment.  This may
           just mean that we don't yet have all the fragments, so
           we should not just continue dissecting. */
        call_data_dissector(frag_tvb, pinfo, proto_tree_get_root(tree));
        return NULL;
    }
} /* dissect_6lowpan_rfrag */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_rfrag_ack
 *  DESCRIPTION
 *      Dissector routine for a 6LoWPAN ACK Dispatch type and header
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *  RETURNS
 *      tvbuff_t *      ; reassembled IPv6 packet.
 *---------------------------------------------------------------
 */
static tvbuff_t *
dissect_6lowpan_rfrag_ack(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    int                 offset = 0;
    proto_tree *        frag_tree;
    proto_item *        ti;
    (void)pinfo;

    /* Create a tree for the fragmentation header. */
    frag_tree = proto_tree_add_subtree(tree, tvb, offset, 0, ett_6lowpan_frag, &ti, "RFRAG ACK Header");

    /* Get and display the pattern and explicit congestion bit. */
    proto_tree_add_bits_item(frag_tree, hf_6lowpan_pattern, tvb, offset * 8, LOWPAN_PATTERN_RFRAG_BITS, ENC_BIG_ENDIAN);
    proto_tree_add_item(frag_tree, hf_6lowpan_rfrag_congestion, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset += 1;

    /* Get and display the datagram tag. */
    proto_tree_add_item(frag_tree, hf_6lowpan_rfrag_dgram_tag, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset += 1;

    proto_tree_add_bits_item(frag_tree, hf_6lowpan_rfrag_ack_bitmap, tvb, offset * 8, 32, ENC_BIG_ENDIAN);
    offset += 4;

    /* TODO: Match ACK bits to original fragments? */

    return tvb_new_subset_remaining(tvb, offset);
} /* dissect_6lowpan_rfrag_ack */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_frag_first
 *  DESCRIPTION
 *      Dissector routine for a 6LoWPAN FRAG1 headers.
 *
 *      If reassembly could be completed, this should return an
 *      uncompressed IPv6 packet. If reassembly had to be delayed
 *      for more packets, this will return NULL.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *      siid            ; Source Interface ID.
 *      diid            ; Destination Interface ID.
 *  RETURNS
 *      tvbuff_t *      ; reassembled IPv6 packet.
 *---------------------------------------------------------------
 */
static tvbuff_t *
dissect_6lowpan_frag_first(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, const uint8_t *siid, const uint8_t *diid)
{
    int                 offset = 0;
    int                 frag_size;
    uint16_t            dgram_size;
    uint16_t            dgram_tag;
    proto_tree *        frag_tree;
    proto_item *        ti;
    proto_item *        length_item;
    /* Reassembly parameters. */
    tvbuff_t *          new_tvb;
    tvbuff_t *          frag_tvb;
    fragment_head *     frag_data;
    bool                save_fragmented;

    /* Create a tree for the fragmentation header. */
    frag_tree = proto_tree_add_subtree(tree, tvb, offset, 0, ett_6lowpan_frag, &ti, "Fragmentation Header");

    /* Get and display the pattern and datagram size. */
    dgram_size = tvb_get_bits16(tvb, (offset * 8) + LOWPAN_PATTERN_FRAG_BITS, LOWPAN_FRAG_DGRAM_SIZE_BITS, ENC_BIG_ENDIAN);
    proto_tree_add_bits_item(frag_tree, hf_6lowpan_pattern, tvb, offset * 8, LOWPAN_PATTERN_FRAG_BITS, ENC_BIG_ENDIAN);
    length_item = proto_tree_add_uint(frag_tree, hf_6lowpan_frag_dgram_size, tvb, offset, 2, dgram_size);
    offset += 2;

    /* Get and display the datagram tag. */
    dgram_tag = tvb_get_ntohs(tvb, offset);
    proto_tree_add_uint(frag_tree, hf_6lowpan_frag_dgram_tag, tvb, offset, 2, dgram_tag);
    offset += 2;

    /* Adjust the fragmentation header length. */
    proto_item_set_end(ti, tvb, offset);


    frag_tvb = tvb_new_subset_length(tvb, offset, dgram_size);
    frag_tvb = dissect_6lowpan_frag_headers(frag_tvb, pinfo, tree, length_item, siid, diid);
    /* Check call to dissect_6lowpan_xxx was successful */
    if (frag_tvb == NULL) {
        return NULL;
    }

    /* Add this datagram to the fragment table. */
    frag_size = tvb_captured_length(frag_tvb);
    tvb_set_reported_length(frag_tvb, frag_size);
    save_fragmented = pinfo->fragmented;
    pinfo->fragmented = true;
    uint32_t frag_id = lowpan_reassembly_id(pinfo, dgram_tag);
    frag_data = fragment_add_check(&lowpan_reassembly_table,
                    frag_tvb, 0, pinfo, frag_id, NULL,
                    0, frag_size, (frag_size < dgram_size));

    /* Attempt reassembly. */
    new_tvb = process_reassembled_data(frag_tvb, 0, pinfo,
                    "Reassembled 6LoWPAN", frag_data, &lowpan_frag_items,
                    NULL, tree);

    pinfo->fragmented = save_fragmented;

    if (new_tvb) {
        /* Reassembly was successful; return the completed datagram. */
        return new_tvb;
    } else {
        /* Reassembly was unsuccessful; show this fragment.  This may
           just mean that we don't yet have all the fragments, so
           we should not just continue dissecting. */
        call_data_dissector(frag_tvb, pinfo, proto_tree_get_root(tree));
        return NULL;
    }
} /* dissect_6lowpan_frag_first */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_frag_middle
 *  DESCRIPTION
 *      Dissector routine for a 6LoWPAN FRAGN headers.
 *
 *      If reassembly could be completed, this should return an
 *      uncompressed IPv6 packet. If reassembly had to be delayed
 *      for more packets, this will return NULL.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *  RETURNS
 *      tvbuff_t *      ; reassembled IPv6 packet.
 *---------------------------------------------------------------
 */
static tvbuff_t *
dissect_6lowpan_frag_middle(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    int                 offset = 0;
    int                 frag_size;
    uint16_t            dgram_size;
    uint16_t            dgram_tag;
    uint16_t            dgram_offset = 0;
    proto_tree *        frag_tree;
    proto_item *        ti;
    /* Reassembly parameters. */
    tvbuff_t *          new_tvb;
    fragment_head *     frag_data;
    bool                save_fragmented;

    /* Create a tree for the fragmentation header. */
    frag_tree = proto_tree_add_subtree(tree, tvb, offset, 0, ett_6lowpan_frag, &ti, "Fragmentation Header");

    /* Get and display the pattern and datagram size. */
    dgram_size = tvb_get_bits16(tvb, (offset * 8) + LOWPAN_PATTERN_FRAG_BITS, LOWPAN_FRAG_DGRAM_SIZE_BITS, ENC_BIG_ENDIAN);
    proto_tree_add_bits_item(frag_tree, hf_6lowpan_pattern, tvb, offset * 8, LOWPAN_PATTERN_FRAG_BITS, ENC_BIG_ENDIAN);
    proto_tree_add_uint(frag_tree, hf_6lowpan_frag_dgram_size, tvb, offset, 2, dgram_size);
    offset += 2;

    /* Get and display the datagram tag. */
    dgram_tag = tvb_get_ntohs(tvb, offset);
    proto_tree_add_uint(frag_tree, hf_6lowpan_frag_dgram_tag, tvb, offset, 2, dgram_tag);
    offset += 2;

    /* Get and display the datagram offset. */
    dgram_offset = tvb_get_uint8(tvb, offset) * 8;
    proto_tree_add_uint(frag_tree, hf_6lowpan_frag_dgram_offset, tvb, offset, 1, dgram_offset);
    offset += 1;

    /* Adjust the fragmentation header length. */
    frag_size = tvb_reported_length_remaining(tvb, offset);
    proto_item_set_end(ti, tvb, offset);

    /* Add this datagram to the fragment table. */
    save_fragmented = pinfo->fragmented;
    pinfo->fragmented = true;
    uint32_t frag_id = lowpan_reassembly_id(pinfo, dgram_tag);
    frag_data = fragment_add_check(&lowpan_reassembly_table,
                    tvb, offset, pinfo, frag_id, NULL,
                    dgram_offset, frag_size, ((dgram_offset + frag_size) < dgram_size));

    /* Attempt reassembly. */
    new_tvb = process_reassembled_data(tvb, offset, pinfo,
                    "Reassembled 6LoWPAN", frag_data, &lowpan_frag_items,
                    NULL, tree);

    pinfo->fragmented = save_fragmented;

    /* If reassembly was successful, then return the completed datagram. */
    if (new_tvb) {
        return new_tvb;
    }
    /* If reassembly failed, display the payload fragment using the data dissector. */
    else {
        new_tvb = tvb_new_subset_remaining(tvb, offset);
        call_data_dissector(new_tvb, pinfo, proto_tree_get_root(tree));
        return NULL;
    }
} /* dissect_6lowpan_frag_middle */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      dissect_6lowpan_unknown
 *  DESCRIPTION
 *      Dissector routine for 6LoWPAN packets after encountering
 *      an unknown header.
 *  PARAMETERS
 *      tvb             ; packet buffer.
 *      pinfo           ; packet info.
 *      tree            ; 6LoWPAN display tree.
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
void
dissect_6lowpan_unknown(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    tvbuff_t *          data_tvb;

    /* Get and display the pattern. */

    /* Give a special case for NALP. */
    if (tvb_get_bits8(tvb, 0, LOWPAN_PATTERN_IPHC_BITS) == LOWPAN_PATTERN_IPHC) {
        proto_tree_add_bits_item(tree, hf_6lowpan_pattern, tvb, 0, LOWPAN_PATTERN_IPHC_BITS, ENC_BIG_ENDIAN);
    }
    else {
        uint8_t pattern = tvb_get_uint8(tvb, 0);
        proto_tree_add_uint_bits_format_value(tree, hf_6lowpan_pattern, tvb, 0, 8, pattern, ENC_BIG_ENDIAN, "Unknown (0x%02x)", pattern);
    }

    /* Create a tvbuff subset for the remaining data. */
    data_tvb = tvb_new_subset_remaining(tvb, 1);
    call_data_dissector(data_tvb, pinfo, proto_tree_get_root(tree));
} /* dissect_6lowpan_unknown */

static void
proto_shutdown_6lowpan(void)
{
    g_hash_table_destroy(lowpan_context_table);
}

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      proto_register_6lowpan
 *  DESCRIPTION
 *      Protocol registration routine for 6LoWPAN. Called during
 *      Wireshark initialization.
 *  PARAMETERS
 *      none            ;
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
void
proto_register_6lowpan(void)
{
    static hf_register_info hf[] = {
        /* Common 6LoWPAN fields. */
        { &hf_6lowpan_pattern,
          { "Pattern",                        "6lowpan.pattern",
            FT_UINT8, BASE_HEX, VALS(lowpan_patterns), 0x0, NULL, HFILL }},
        { &hf_6lowpan_nhc_pattern,
          { "Pattern",                        "6lowpan.nhc.pattern",
            FT_UINT8, BASE_HEX, VALS(lowpan_nhc_patterns), 0x0, NULL, HFILL }},
        { &hf_6lowpan_padding,
          { "Padding",                        "6lowpan.padding",
            FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},

        /* HC1 header fields. */
        { &hf_6lowpan_hc1_encoding,
          { "HC1 Encoding",                  "6lowpan.hc1.encoding",
            FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_hc1_source_prefix,
          { "Source prefix",                  "6lowpan.hc1.src_prefix",
            FT_BOOLEAN, 8, TFS(&lowpan_compression), LOWPAN_HC1_SOURCE_PREFIX, NULL, HFILL }},
        { &hf_6lowpan_hc1_source_ifc,
          { "Source interface",               "6lowpan.hc1.src_ifc",
            FT_BOOLEAN, 8, TFS(&lowpan_compression), LOWPAN_HC1_SOURCE_IFC, NULL, HFILL }},
        { &hf_6lowpan_hc1_dest_prefix,
          { "Destination prefix",             "6lowpan.hc1.dst_prefix",
            FT_BOOLEAN, 8, TFS(&lowpan_compression), LOWPAN_HC1_DEST_PREFIX, NULL, HFILL }},
        { &hf_6lowpan_hc1_dest_ifc,
          { "Destination interface",          "6lowpan.hc1.dst_ifc",
            FT_BOOLEAN, 8, TFS(&lowpan_compression), LOWPAN_HC1_DEST_IFC, NULL, HFILL }},
        { &hf_6lowpan_hc1_class,
          { "Traffic class and flow label",   "6lowpan.hc1.class",
            FT_BOOLEAN, 8, TFS(&lowpan_compression), LOWPAN_HC1_TRAFFIC_CLASS, NULL, HFILL }},
        { &hf_6lowpan_hc1_next,
          { "Next header",                    "6lowpan.hc1.next",
            FT_UINT8, BASE_HEX, VALS(lowpan_hc1_next), LOWPAN_HC1_NEXT, NULL, HFILL }},
        { &hf_6lowpan_hc1_more,
          { "More HC bits",                   "6lowpan.hc1.more",
            FT_BOOLEAN, 8, NULL, LOWPAN_HC1_MORE, NULL, HFILL }},

        /* HC_UDP header fields. */
        { &hf_6lowpan_hc2_udp_encoding,
          { "HC_UDP Encoding",                    "6lowpan.hc2.udp.encoding",
            FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_hc2_udp_src,
          { "Source port",                    "6lowpan.hc2.udp.src",
            FT_BOOLEAN, 8, TFS(&lowpan_compression), LOWPAN_HC2_UDP_SRCPORT, NULL, HFILL }},
        { &hf_6lowpan_hc2_udp_dst,
          { "Destination port",               "6lowpan.hc2.udp.dst",
            FT_BOOLEAN, 8, TFS(&lowpan_compression), LOWPAN_HC2_UDP_DSTPORT, NULL, HFILL }},
        { &hf_6lowpan_hc2_udp_len,
          { "Length",                         "6lowpan.hc2.udp.length",
            FT_BOOLEAN, 8, TFS(&lowpan_compression), LOWPAN_HC2_UDP_LENGTH, NULL, HFILL }},

        /* IPHC header fields. */
        { &hf_6lowpan_iphc_flag_tf,
          { "Traffic class and flow label",   "6lowpan.iphc.tf",
            FT_UINT16, BASE_HEX, VALS(lowpan_iphc_traffic), LOWPAN_IPHC_FLAG_FLOW, "traffic class and flow control encoding", HFILL }},
        { &hf_6lowpan_iphc_flag_nhdr,
          { "Next header",                    "6lowpan.iphc.nh",
            FT_BOOLEAN, 16, TFS(&lowpan_compression), LOWPAN_IPHC_FLAG_NHDR, NULL, HFILL }},
        { &hf_6lowpan_iphc_flag_hlim,
          { "Hop limit",                      "6lowpan.iphc.hlim",
            FT_UINT16, BASE_HEX, VALS(lowpan_iphc_hop_limit), LOWPAN_IPHC_FLAG_HLIM, NULL, HFILL }},
        { &hf_6lowpan_iphc_flag_cid,
          { "Context identifier extension",   "6lowpan.iphc.cid",
            FT_BOOLEAN, 16, NULL, LOWPAN_IPHC_FLAG_CONTEXT_ID, NULL, HFILL }},
        { &hf_6lowpan_iphc_flag_sac,
          { "Source address compression",     "6lowpan.iphc.sac",
            FT_BOOLEAN, 16, TFS(&lowpan_iphc_addr_compression), LOWPAN_IPHC_FLAG_SRC_COMP, NULL, HFILL }},
        { &hf_6lowpan_iphc_flag_sam,
          { "Source address mode",            "6lowpan.iphc.sam",
            FT_UINT16, BASE_HEX, VALS(lowpan_iphc_addr_modes), LOWPAN_IPHC_FLAG_SRC_MODE, NULL, HFILL }},
        { &hf_6lowpan_iphc_flag_mcast,
          { "Multicast address compression",  "6lowpan.iphc.m",
            FT_BOOLEAN, 16, NULL, LOWPAN_IPHC_FLAG_MCAST_COMP, NULL, HFILL }},
        { &hf_6lowpan_iphc_flag_dac,
          { "Destination address compression","6lowpan.iphc.dac",
            FT_BOOLEAN, 16, TFS(&lowpan_iphc_addr_compression), LOWPAN_IPHC_FLAG_DST_COMP, NULL, HFILL }},
        { &hf_6lowpan_iphc_flag_dam,
          { "Destination address mode",       "6lowpan.iphc.dam",
            FT_UINT16, BASE_HEX, VALS(lowpan_iphc_addr_modes), LOWPAN_IPHC_FLAG_DST_MODE, NULL, HFILL }},
        { &hf_6lowpan_iphc_sci,
          { "Source context identifier",      "6lowpan.iphc.sci",
            FT_UINT8, BASE_HEX, NULL, LOWPAN_IPHC_FLAG_SCI, NULL, HFILL }},
        { &hf_6lowpan_iphc_dci,
          { "Destination context identifier", "6lowpan.iphc.dci",
            FT_UINT8, BASE_HEX, NULL, LOWPAN_IPHC_FLAG_DCI, NULL, HFILL }},

        /* Context information fields. */
        { &hf_6lowpan_iphc_sctx_prefix,
        { "Source context",                   "6lowpan.iphc.sctx.prefix", FT_IPv6, BASE_NONE, NULL, 0x0,
            NULL, HFILL }},
        { &hf_6lowpan_iphc_sctx_origin,
        { "Origin",                           "6lowpan.iphc.sctx.origin", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
            NULL, HFILL }},
        { &hf_6lowpan_iphc_dctx_prefix,
        { "Destination context",              "6lowpan.iphc.dctx.prefix", FT_IPv6, BASE_NONE, NULL, 0x0,
            NULL, HFILL }},
        { &hf_6lowpan_iphc_dctx_origin,
        { "Origin",                           "6lowpan.iphc.dctx.origin", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
            NULL, HFILL }},

        /* NHC IPv6 extension header fields. */
        { &hf_6lowpan_nhc_ext_eid,
          { "Header ID",                      "6lowpan.nhc.ext.eid",
            FT_UINT8, BASE_HEX, VALS(lowpan_nhc_eid), LOWPAN_NHC_EXT_EID, NULL, HFILL }},
        { &hf_6lowpan_nhc_ext_nh,
          { "Next header",                    "6lowpan.nhc.ext.nh",
            FT_BOOLEAN, 8, TFS(&lowpan_compression), LOWPAN_NHC_EXT_NHDR, NULL, HFILL }},
        { &hf_6lowpan_nhc_ext_next,
          { "Next header",                    "6lowpan.nhc.ext.next",
            FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_nhc_ext_length,
          { "Header length",                  "6lowpan.nhc.ext.length",
            FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_nhc_ext_reserved,
          { "Reserved octet",                  "6lowpan.nhc.ext.reserved",
            FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},

        /* NHC UDP header fields. */
        { &hf_6lowpan_nhc_udp_checksum,
          { "Checksum",                       "6lowpan.nhc.udp.checksum",
            FT_BOOLEAN, 8, TFS(&lowpan_compression), LOWPAN_NHC_UDP_CHECKSUM, NULL, HFILL }},
        { &hf_6lowpan_nhc_udp_ports,
          { "Ports",                          "6lowpan.nhc.udp.ports",
            FT_UINT8, BASE_DEC, VALS(lowpan_udp_ports), LOWPAN_NHC_UDP_PORTS, NULL, HFILL }},

        /* Uncompressed IPv6 fields. */
        { &hf_6lowpan_traffic_class,
          { "Traffic class",                  "6lowpan.class",
            FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_flow_label,
          { "Flow label",                     "6lowpan.flow",
            FT_UINT24, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_ecn,
          { "ECN",                            "6lowpan.ecn",
            FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_dscp,
          { "DSCP",                           "6lowpan.dscp",
            FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_next_header,
          { "Next header",                    "6lowpan.next",
            FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_hop_limit,
          { "Hop limit",                      "6lowpan.hops",
            FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_source,
          { "Source",                         "6lowpan.src",
            FT_IPv6, BASE_NONE, NULL, 0x0, "Source IPv6 address", HFILL }},
        { &hf_6lowpan_dest,
          { "Destination",                    "6lowpan.dst",
            FT_IPv6, BASE_NONE, NULL, 0x0, "Destination IPv6 address", HFILL }},

        /* Uncompressed UDP fields. */
        { &hf_6lowpan_udp_src,
          { "Source port",                    "6lowpan.udp.src",
            FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_udp_dst,
          { "Destination port",               "6lowpan.udp.dst",
            FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_udp_len,
          { "UDP length",                     "6lowpan.udp.length",
            FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_udp_checksum,
          { "UDP checksum",                   "6lowpan.udp.checksum",
            FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},

        /* Broadcast header fields. */
        { &hf_6lowpan_bcast_seqnum,
          { "Sequence number",                "6lowpan.bcast.seqnum",
            FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},

        /* Mesh header fields. */
        { &hf_6lowpan_mesh_v,
          { "V",                              "6lowpan.mesh.v",
            FT_BOOLEAN, 8, NULL, LOWPAN_MESH_HEADER_V, "short originator address present", HFILL }},
        { &hf_6lowpan_mesh_f,
          { "D",                              "6lowpan.mesh.f",
            FT_BOOLEAN, 8, NULL, LOWPAN_MESH_HEADER_F, "short destination address present", HFILL }},
        { &hf_6lowpan_mesh_hops,
          { "Hops left",                      "6lowpan.mesh.hops",
            FT_UINT8, BASE_DEC, NULL, LOWPAN_MESH_HEADER_HOPS, NULL, HFILL }},
        { &hf_6lowpan_mesh_hops8,
          { "Deep Hops left (Flags.Hops left == 15)", "6lowpan.mesh.hops8",
            FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_mesh_orig16,
          { "Originator",                     "6lowpan.mesh.orig16",
            FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_mesh_orig64,
          { "Originator",                     "6lowpan.mesh.orig64",
            FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_mesh_dest16,
          { "Destination",                    "6lowpan.mesh.dest16",
            FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_mesh_dest64,
          { "Destination",                    "6lowpan.mesh.dest64",
            FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},

        /* Fragmentation header fields. */
        { &hf_6lowpan_frag_dgram_size,
          { "Datagram size",                  "6lowpan.frag.size",
            FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_frag_dgram_tag,
          { "Datagram tag",                   "6lowpan.frag.tag",
            FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_frag_dgram_offset,
          { "Datagram offset",                "6lowpan.frag.offset",
            FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},

        /* Recoverable Fragmentation header fields. */
        { &hf_6lowpan_rfrag_congestion,
          { "Congestion",                     "6lowpan.rfrag.congestion",
            FT_BOOLEAN, 8, TFS(&tfs_yes_no), 0x01, NULL, HFILL }},
        { &hf_6lowpan_rfrag_ack_requested,
          { "Ack requested",                  "6lowpan.rfrag.ack_requested",
            FT_BOOLEAN, 16, TFS(&tfs_yes_no), 0x8000, NULL, HFILL }},
        { &hf_6lowpan_rfrag_dgram_tag,
          { "Datagram tag",                   "6lowpan.rfrag.tag",
            FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_rfrag_sequence,
          { "Fragment sequence",              "6lowpan.rfrag.sequence",
            FT_UINT16, BASE_DEC, NULL, 0x7C00, NULL, HFILL }},
        { &hf_6lowpan_rfrag_size,
          { "Fragment size",                  "6lowpan.rfrag.size",
            FT_UINT16, BASE_DEC, NULL, 0x03FF, NULL, HFILL }},
        { &hf_6lowpan_rfrag_dgram_size,
          { "Datagram size",                "6lowpan.rfrag.datagram_size",
            FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_rfrag_offset,
          { "Fragment offset",                "6lowpan.rfrag.offset",
            FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_rfrag_ack_bitmap,
          { "Fragment ACK bitmask",                "6lowpan.rfrag.ack_bitmask",
            FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},

        /* Reassembly fields. */
        { &hf_6lowpan_fragments,
          { "Message fragments",              "6lowpan.fragments",
            FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL }},
        { &hf_6lowpan_fragment,
          { "Message fragment",               "6lowpan.fragment",
            FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }},
        { &hf_6lowpan_fragment_overlap,
          { "Message fragment overlap",       "6lowpan.fragment.overlap",
            FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL }},
        { &hf_6lowpan_fragment_overlap_conflicts,
          { "Message fragment overlapping with conflicting data", "6lowpan.fragment.overlap.conflicts",
            FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL }},
        { &hf_6lowpan_fragment_multiple_tails,
          { "Message has multiple tail fragments", "6lowpan.fragment.multiple_tails",
            FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL }},
        { &hf_6lowpan_fragment_too_long_fragment,
          { "Message fragment too long",      "6lowpan.fragment.too_long_fragment",
            FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL }},
        { &hf_6lowpan_fragment_error,
          { "Message defragmentation error",  "6lowpan.fragment.error",
            FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }},
        { &hf_6lowpan_fragment_count,
          { "Message fragment count",         "6lowpan.fragment.count",
            FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL }},
        { &hf_6lowpan_reassembled_in,
          { "Reassembled in",                 "6lowpan.reassembled.in",
            FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }},
        { &hf_6lowpan_reassembled_length,
          { "Reassembled 6LoWPAN length",     "6lowpan.reassembled.length",
            FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL }},

        /* 6loRH fields */
        { &hf_6lowpan_6lorhc_address_src,
          { "Encapsulator Address",           "6lowpan.src",
            FT_IPv6, BASE_NONE, NULL, 0x0, "Source IPv6 address", HFILL }},
        { &hf_6lowpan_6lorhc_address_hop0,
          { "Source/15, Delta",               "6lowpan.src",
            FT_IPv6, BASE_NONE, NULL, 0x0, "Source IPv6 address", HFILL }},
        { &hf_6lowpan_6lorhc_address_hop1,
          { "Source/14, Delta",               "6lowpan.src",
            FT_IPv6, BASE_NONE, NULL, 0x0, "Source IPv6 address", HFILL }},
        { &hf_6lowpan_6lorhc_address_hop2,
          { "Source/12, Delta",               "6lowpan.src",
            FT_IPv6, BASE_NONE, NULL, 0x0, "Source IPv6 address", HFILL }},
        { &hf_6lowpan_6lorhc_address_hop3,
          { "Source/8, Delta",                "6lowpan.src",
            FT_IPv6, BASE_NONE, NULL, 0x0, "Source IPv6 address", HFILL }},
        { &hf_6lowpan_6lorhc_address_hop4,
          { "Source/0 Delta",                 "6lowpan.src",
            FT_IPv6, BASE_NONE, NULL, 0x0, "Source IPv6 address", HFILL }},
        { &hf_6lowpan_sender_rank1,
          { "Sender Rank",                    "6lowpan.sender.rank",
            FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_sender_rank2,
          { "Sender Rank",                    "6lowpan.sender.rank",
            FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_rpl_instance,
          { "RPL Instance",                   "6lowpan.rpl.instance",
            FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_5_bit_o,
          { "Packet direction (bit O)",             "6lowpan.6loRH.bitO",
            FT_BOOLEAN, 16, TFS(&tfs_down_up), LOWPAN_5_RPI_BIT_O, NULL, HFILL }},
        { &hf_6lowpan_5_bit_r,
          { "Rank-Error (bit R)",               "6lowpan.6loRH.bitR",
            FT_BOOLEAN, 16, TFS(&tfs_yes_no), LOWPAN_5_RPI_BIT_R, NULL, HFILL }},
        { &hf_6lowpan_5_bit_f,
          { "Forwarding-Error (bit F)",         "6lowpan.6loRH.bitF",
            FT_BOOLEAN, 16, TFS(&tfs_yes_no), LOWPAN_5_RPI_BIT_F, NULL, HFILL }},
        { &hf_6lowpan_5_bit_i,
          { "RPL Instance (bit I)",                 "6lowpan.6loRH.bitI",
            FT_BOOLEAN, 16, TFS(&bit_I_RPL), LOWPAN_5_RPI_BIT_I, NULL, HFILL }},
        { &hf_6lowpan_5_bit_k,
          { "Sender Rank Compression size (bit K)",     "6lowpan.6loRH.bitK",
            FT_BOOLEAN, 16, TFS(&bit_K_RPL), LOWPAN_5_RPI_BIT_K, NULL, HFILL }},
        { &hf_6lowpan_6lorhe_hoplimit,
          { "6loRH Hop Limit",                "6lowpan.rhhop.limit",
            FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_6lorhe_bitmap,
          { "6loRH BIER Bitmap",              "6lowpan.bitmap",
            FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_6lowpan_6lorhe_type,
          { "6loRH Type",                     "6lowpan.rhtype",
            FT_UINT16, BASE_HEX, VALS(lowpan_patterns_rh_type), LOWPAN_PATTERN_6LORHE_TYPE, NULL, HFILL }},
        { &hf_6lowpan_6lorhc_size,
          { "6loRH Hop Number-1",             "6lowpan.HopNuevo",
            FT_UINT16, BASE_HEX, NULL, LOWPAN_PATTERN_6LORHE_LENGTH, NULL, HFILL }},
        { &hf_6lowpan_6lorhe_size,
          { "6loRH Bitmap Word Number-1",     "6lowpan.WordNuevo",
            FT_UINT16, BASE_HEX, NULL, LOWPAN_PATTERN_6LORHE_LENGTH, NULL, HFILL }},
        { &hf_6lowpan_6lorhe_length,
          { "6loRH Elective Length",          "6lowpan.rhElength",
            FT_UINT16, BASE_DEC, NULL, LOWPAN_PATTERN_6LORHE_LENGTH, NULL, HFILL }},
        { &hf_6lowpan_routing_header,
          { "Routing Header 6lo",             "6lowpan.routingheader",
            FT_UINT8, BASE_HEX, VALS(lowpan_patterns_rh), 0x0, NULL, HFILL }},
        { &hf_6lowpan_pagenb,
          { "Page Number",                    "6lowpan.pagenb",
            FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }}
    };

    static int *ett[] = {
        &ett_6lowpan,
        &ett_6lowpan_hc1,
        &ett_6lowpan_hc1_encoding,
        &ett_6lowpan_hc2_udp,
        &ett_6lowpan_iphc,
        &ett_lowpan_routing_header_dispatch,
        &ett_6lowpan_nhc_ext,
        &ett_6lowpan_nhc_udp,
        &ett_6lowpan_bcast,
        &ett_6lowpan_mesh,
        &ett_6lowpan_mesh_flags,
        &ett_6lowpan_frag,
        /* Reassembly subtrees. */
        &ett_6lowpan_fragment,
        &ett_6lowpan_fragments
    };

    static ei_register_info ei[] = {
        { &ei_6lowpan_hc1_more_bits, { "6lowpan.hc1_more_bits", PI_MALFORMED, PI_ERROR, "HC1 more bits expected for illegal next header type.", EXPFILL }},
        { &ei_6lowpan_illegal_dest_addr_mode, { "6lowpan.illegal_dest_addr_mode", PI_MALFORMED, PI_ERROR, "Illegal destination address mode", EXPFILL }},
        { &ei_6lowpan_bad_ipv6_header_length, { "6lowpan.bad_ipv6_header_length", PI_MALFORMED, PI_ERROR, "Length is less than IPv6 header length", EXPFILL }},
        { &ei_6lowpan_bad_ext_header_length, { "6lowpan.bad_ext_header_length", PI_MALFORMED, PI_ERROR, "Extension header not 8-octet aligned", EXPFILL }},
    };

    int         i;
    module_t    *prefs_module;
    expert_module_t* expert_6lowpan;

    lowpan_context_table = g_hash_table_new_full(lowpan_context_hash, lowpan_context_equal, lowpan_context_free, lowpan_context_free);

    proto_6lowpan = proto_register_protocol("IPv6 over Low power Wireless Personal Area Networks", "6LoWPAN", "6lowpan");
    proto_register_field_array(proto_6lowpan, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
    expert_6lowpan = expert_register_protocol(proto_6lowpan);
    expert_register_field_array(expert_6lowpan, ei, array_length(ei));

    /* Register the dissector with wireshark. */
    handle_6lowpan = register_dissector("6lowpan", dissect_6lowpan, proto_6lowpan);

    /* Initialize the fragment reassembly table. */
    reassembly_table_register(&lowpan_reassembly_table, &addresses_reassembly_table_functions);

    /* Register the dissector init function */
    register_init_routine(proto_init_6lowpan);
    register_shutdown_routine(proto_shutdown_6lowpan);

    /* Initialize the context preferences. */
    memset((char*)lowpan_context_prefs, 0, sizeof(lowpan_context_prefs));

    /* Register preferences. */
    prefs_module = prefs_register_protocol(proto_6lowpan, prefs_6lowpan_apply);

    prefs_register_bool_preference(prefs_module, "rfc4944_short_address_format",
                                   "Derive IID according to RFC 4944",
                                   "Derive IID from a short 16-bit address according to RFC 4944 (using the PAN ID).",
                                   &rfc4944_short_address_format);
    prefs_register_bool_preference(prefs_module, "iid_has_universal_local_bit",
                                   "IID has Universal/Local bit",
                                   "Linux kernels before version 4.12 does toggle the Universal/Local bit.",
                                   &iid_has_universal_local_bit);
    prefs_register_bool_preference(prefs_module, "summary_in_tree",
                                   "Show IPv6 summary in protocol tree",
                                   "Whether the IPv6 summary line should be shown in the protocol tree",
                                   &ipv6_summary_in_tree);

    for (i = 0; i < LOWPAN_CONTEXT_MAX; i++) {
        char *pref_name, *pref_title;

        /*
         * Inspired by the IEEE 802.11 dissector - the preferences are expecting
         * that each pref has a unique string passed in, and will crash if we
         * try to reuse any for multiple preferences.
         */
        pref_name  = wmem_strdup_printf(wmem_epan_scope(), "context%d", i);
        pref_title = wmem_strdup_printf(wmem_epan_scope(), "Context %d", i);
        prefs_register_string_preference(prefs_module, pref_name, pref_title,
            "IPv6 prefix to use for stateful address decompression.",
            &lowpan_context_prefs[i]);
    }
} /* proto_register_6lowpan */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      proto_init_6lowpan
 *  DESCRIPTION
 *      6LoWPAN initialization function.
 *  PARAMETERS
 *      none            ;
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
static void
proto_init_6lowpan(void)
{
    /* Initialize the link-local context. */
    lowpan_context_local.frame = 0;
    lowpan_context_local.plen = LOWPAN_CONTEXT_LINK_LOCAL_BITS;
    memcpy(&lowpan_context_local.prefix, lowpan_llprefix, sizeof(lowpan_llprefix));

    /* Reload static contexts from our preferences. */
    prefs_6lowpan_apply();
} /* proto_init_6lowpan */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      prefs_6lowpan_apply
 *  DESCRIPTION
 *      Prefs "apply" callback. Parses the context table for
 *      IPv6 addresses/prefixes.
 *  PARAMETERS
 *      none            ;
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
void
prefs_6lowpan_apply(void)
{
    int                 i;
    ws_in6_addr   prefix;
    char                *prefix_str;
    char                *prefix_len_str;
    uint32_t            prefix_len;
    char                prefix_buf[48]; /* max length of IPv6 str. plus a bit */

    for (i = 0; i < LOWPAN_CONTEXT_MAX; i++) {
        if (!lowpan_context_prefs[i]) continue;
        (void) g_strlcpy(prefix_buf, lowpan_context_prefs[i], 48);
        if ((prefix_str = strtok(prefix_buf, "/")) == NULL) continue;
        if ((prefix_len_str = strtok(NULL, "/")) == NULL) continue;
        if (sscanf(prefix_len_str, "%u", &prefix_len) != 1) continue;
        if (!str_to_ip6(prefix_str, &prefix)) continue;
        /* Set the prefix */
        lowpan_context_insert(i, IEEE802154_BCAST_PAN, prefix_len, &prefix, 0);
    } /* for */
} /* prefs_6lowpan_apply */

/*FUNCTION:------------------------------------------------------
 *  NAME
 *      proto_reg_handoff_6lowpan
 *  DESCRIPTION
 *      Protocol handoff routine for 6LoWPAN. Called after all
 *      protocols have been loaded.
 *  PARAMETERS
 *      none            ;
 *  RETURNS
 *      void            ;
 *---------------------------------------------------------------
 */
void
proto_reg_handoff_6lowpan(void)
{
    ipv6_handle = find_dissector_add_dependency("ipv6", proto_6lowpan);

    /* Register the 6LoWPAN dissector with IEEE 802.15.4 */
    dissector_add_for_decode_as(IEEE802154_PROTOABBREV_WPAN_PANID, handle_6lowpan);
    heur_dissector_add(IEEE802154_PROTOABBREV_WPAN, dissect_6lowpan_heur, "6LoWPAN over IEEE 802.15.4", "6lowpan_wlan", proto_6lowpan, HEURISTIC_ENABLE);

    /* Register Ethertype (RFC 7973) */
    dissector_add_uint("ethertype", ETHERTYPE_6LOWPAN, handle_6lowpan);

    dissector_add_uint("btl2cap.psm", BTL2CAP_PSM_LE_IPSP, handle_6lowpan);
    dissector_add_for_decode_as("btl2cap.cid", handle_6lowpan);
} /* proto_reg_handoff_6lowpan */


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