summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-bssap.c
blob: 6dcb25b42df5c4b280b007f889e2a0a2014c217c (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
/* packet-bssap.c
 * Routines for Base Station Subsystem Application Part (BSSAP/BSAP) dissection
 * Specifications from 3GPP2 (www.3gpp2.org) and 3GPP (www.3gpp.org)
 *  IOS 4.0.1 (BSAP)
 *  GSM 08.06 (BSSAP)
 *
 * Copyright 2003, Michael Lum <mlum [AT] telostech.com>
 * In association with Telos Technology Inc.
 *
 * Added BSSAP+ according to ETSI TS 129 018 V6.3.0 (2005-3GPP TS 29.018 version 6.3.0 Release 6)
 * Copyright 2006, Anders Broman <Anders.Broman [AT] ericsson.com>
 *
 * 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/expert.h>
#include <epan/prefs.h>
#include <epan/proto_data.h>
#include <wsutil/str_util.h>
#include <epan/tfs.h>
#include "packet-bssap.h"
#include "packet-gsm_a_common.h"
#include "packet-e212.h"

void proto_register_bssap(void);
void proto_reg_handoff_bssap(void);

static dissector_handle_t bssap_handle;
static dissector_handle_t bssap_plus_handle;
static dissector_handle_t bsap_handle;
static dissector_handle_t bssap_le_handle;

enum bssap_proto {
    BSSAP,
    BSAP,
    BSSAP_LE,
};

#define PDU_TYPE_OFFSET 0
#define PDU_TYPE_LENGTH 1

/* Same as below but with names typed out */
static const value_string bssap_pdu_type_values[] = {
    { BSSAP_PDU_TYPE_BSSMAP,    "BSS Management" },
    { BSSAP_PDU_TYPE_DTAP,  "Direct Transfer" },
    { 0,            NULL } };

static const value_string bsap_pdu_type_values[] = {
    { BSSAP_PDU_TYPE_BSSMAP,    "BS Management" },
    { BSSAP_PDU_TYPE_DTAP,  "Direct Transfer" },
    { 0,            NULL } };

/* Same as above but in acronym for (for the Info column) */
static const value_string bssap_pdu_type_acro_values[] = {
    { BSSAP_PDU_TYPE_BSSMAP,    "BSSMAP" },
    { BSSAP_PDU_TYPE_DTAP,  "DTAP" },
    { 0,            NULL } };

/* Same as above but in acronym for (for the Info column) */
static const value_string bsap_pdu_type_acro_values[] = {
    { BSSAP_PDU_TYPE_BSSMAP,    "BSMAP" },
    { BSSAP_PDU_TYPE_DTAP,  "DTAP" },
    { 0,            NULL } };

#define PARAMETER_DLCI      0x00
#define PARAMETER_LENGTH    0x01
#define PARAMETER_DATA      0x02

#define DLCI_LENGTH     1
#define LENGTH_LENGTH   1
#define DATA_LENGTH     1

#define CC_MASK         0xc0
#define SPARE_MASK      0x38
#define SAPI_MASK       0x07

#define BSSAP_PLUS_SSN 98

static const value_string bssap_cc_values[] = {
    { 0x00,     "not further specified" },
    { 0x02,     "FACCH or SDCCH" },
    { 0x03,     "SACCH" },
    { 0,        NULL } };

static const value_string bsap_cc_values[] = {
    { 0x00,     "default for TIA/EIA/IS-2000" },
    { 0,        NULL } };

static const value_string bssap_sapi_values[] = {
    { 0x00,     "RR/MM/CC" },
    { 0x03,     "SMS" },
    { 0,        NULL } };

static const value_string bsap_sapi_values[] = {
    { 0x00,     "Not used" },
    { 0,        NULL } };

#define BSSAP_PAGING_REQUEST                 1
#define BSSAP_PAGING_REJECT                  2                  /*  17.1.18 */
#define BSSAP_DOWNLINK_TUNNEL_REQUEST        7                  /*  17.1.4  */
#define BSSAP_UPLINK_TUNNEL_REQUEST          8                  /*  17.1.23 */
#define BSSAP_LOCATION_UPDATE_REQUEST        9                  /*  17.1.11 */
#define BSSAP_LOCATION_UPDATE_ACCEPT        10                  /*  17.1.9  */
#define BSSAP_LOCATION_UPDATE_REJECT        11                  /*  17.1.10 */
#define BSSAP_TMSI_REALLOCATION_COMPLETE    12                  /*  17.1.22 */
#define BSSAP_ALERT_REQUEST                 13                  /*  17.1.3  */
#define BSSAP_ALERT_ACK                     14                  /*  17.1.1  */
#define BSSAP_ALERT_REJECT                  15                  /*  17.1.2  */
#define BSSAP_MS_ACTIVITY_INDICATION        16                  /*  17.1.14 */
#define BSSAP_GPRS_DETACH_INDICATION        17                  /*  17.1.6  */
#define BSSAP_GPRS_DETACH_ACK               18                  /*  17.1.5  */
#define BSSAP_IMSI_DETACH_INDICATION        19                  /*  17.1.8  */
#define BSSAP_IMSI_DETACH_ACK               20                  /*  17.1.7  */
#define BSSAP_RESET_INDICATION              21                  /*  17.1.21 */
#define BSSAP_RESET_ACK                     22                  /*  17.1.20 */
#define BSSAP_MS_INFORMATION_REQUEST        23                  /*  17.1.15 */
#define BSSAP_MS_INFORMATION_RESPONSE       24                  /*  17.1.16 */
#define BSSAP_MM_INFORMATION_REQUEST        26                  /*  17.1.12 */
#define BSSAP_MOBILE_STATUS                 29                  /*  17.1.13 */
#define BSSAP_MS_UNREACHABLE                31                  /*  17.1.17 */

static const value_string bssap_plus_message_type_values[] = {
/*  0 */    { 0x00,                                 "Unassigned: treated as an unknown Message type." },
/*  1 */    { BSSAP_PAGING_REQUEST,                 "BSSAP+-PAGING-REQUEST" },                              /*  17.1.19 */
/*  2 */    { BSSAP_PAGING_REJECT,                  "BSSAP+-PAGING-REJECT" },                               /*  17.1.18 */
/*  3 */    { 0x03,                                 "Unassigned: treated as an unknown Message type." },
/*  4 */    { 0x04,                                 "Unassigned: treated as an unknown Message type." },
/*  5 */    { 0x05,                                 "Unassigned: treated as an unknown Message type." },
/*  6 */    { 0x06,                                 "Unassigned: treated as an unknown Message type." },
/*  7 */    { BSSAP_DOWNLINK_TUNNEL_REQUEST,        "BSSAP+-DOWNLINK-TUNNEL-REQUEST" },                     /*  17.1.4  */
/*  8 */    { BSSAP_UPLINK_TUNNEL_REQUEST,          "BSSAP+-UPLINK-TUNNEL-REQUEST" },                       /*  17.1.23 */
/*  9 */    { BSSAP_LOCATION_UPDATE_REQUEST,        "BSSAP+-LOCATION-UPDATE-REQUEST" },                     /*  17.1.11 */
/* 10 */    { BSSAP_LOCATION_UPDATE_ACCEPT,         "BSSAP+-LOCATION-UPDATE-ACCEPT" },                      /*  17.1.9  */
/* 11 */    { BSSAP_LOCATION_UPDATE_REJECT,         "BSSAP+-LOCATION-UPDATE-REJECT" },                      /*  17.1.10 */
/* 12 */    { BSSAP_TMSI_REALLOCATION_COMPLETE,     "BSSAP+-TMSI-REALLOCATION-COMPLETE" },                  /*  17.1.22 */
/* 13 */    { BSSAP_ALERT_REQUEST,                  "BSSAP+-ALERT-REQUEST" },                               /*  17.1.3  */
/* 14 */    { BSSAP_ALERT_ACK,                      "BSSAP+-ALERT-ACK" },                                   /*  17.1.1  */
/* 15 */    { BSSAP_ALERT_REJECT,                   "BSSAP+-ALERT-REJECT" },                                /*  17.1.2  */
/* 16 */    { BSSAP_MS_ACTIVITY_INDICATION,         "BSSAP+-MS-ACTIVITY-INDICATION" },                      /*  17.1.14 */
/* 17 */    { BSSAP_GPRS_DETACH_INDICATION,         "BSSAP+-GPRS-DETACH-INDICATION" },                      /*  17.1.6  */
/* 18 */    { BSSAP_GPRS_DETACH_ACK,                "BSSAP+-GPRS-DETACH-ACK" },                             /*  17.1.5  */
/* 19 */    { BSSAP_IMSI_DETACH_INDICATION,         "BSSAP+-IMSI-DETACH-INDICATION" },                      /*  17.1.8  */
/* 20 */    { BSSAP_IMSI_DETACH_ACK,                "BSSAP+-IMSI-DETACH-ACK" },                             /*  17.1.7  */
/* 21 */    { BSSAP_RESET_INDICATION,               "BSSAP+-RESET-INDICATION" },                            /*  17.1.21 */
/* 22 */    { BSSAP_RESET_ACK,                      "BSSAP+-RESET-ACK" },                                   /*  17.1.20 */
/* 23 */    { BSSAP_MS_INFORMATION_REQUEST,         "BSSAP+-MS-INFORMATION-REQUEST" },                      /*  17.1.15 */
/* 24 */    { BSSAP_MS_INFORMATION_RESPONSE,        "BSSAP+-MS-INFORMATION-RESPONSE" },                     /*  17.1.16 */
/* 25 */    { 0x19,                                 "Unassigned: treated as an unknown Message type." },
/* 26 */    { BSSAP_MM_INFORMATION_REQUEST,         "BSSAP+-MM-INFORMATION-REQUEST" },                      /*  17.1.12 */
/* 27 */    { 0x1b,                                 "Unassigned: treated as an unknown Message type." },
/* 28 */    { 0x1c,                                 "Unassigned: treated as an unknown Message type." },
/* 29 */    { BSSAP_MOBILE_STATUS,                  "BSSAP+-MOBILE-STATUS" },                               /*  17.1.13 */
/* 30 */    { 0x1e,                                 "Unassigned: treated as an unknown Message type." },
/* 31 */    { BSSAP_MS_UNREACHABLE,                 "BSSAP+-MS-UNREACHABLE" },                              /*  17.1.17 */
    { 0,        NULL }
};
static value_string_ext bssap_plus_message_type_values_ext = VALUE_STRING_EXT_INIT(bssap_plus_message_type_values);

#define BSSAP_IMSI                                1
#define BSSAP_VLR_NUMBER                          2
#define BSSAP_TMSI                                3
#define BSSAP_LOC_AREA_ID                         4
#define BSSAP_CHANNEL_NEEDED                      5
#define BSSAP_EMLPP_PRIORITY                      6
#define BSSAP_TMSI_STATUS                         7
#define BSSAP_GS_CAUSE                            8
#define BSSAP_SGSN_NUMBER                         9
#define BSSAP_GPRS_LOC_UPD_TYPE                0x0a
#define BSSAP_GLOBAL_CN_ID                     0x0b
#define BSSAP_MOBILE_STN_CLS_MRK1              0x0d
#define BSSAP_MOBILE_ID                        0x0e
#define BSSAP_REJECT_CAUSE                     0x0f
#define BSSAP_IMSI_DET_FROM_GPRS_SERV_TYPE     0x10
#define BSSAP_IMSI_DET_FROM_NON_GPRS_SERV_TYPE 0x11
#define BSSAP_INFO_REQ                         0x12
#define BSSAP_PTMSI                            0x13
#define BSSAP_IMEI                             0x14
#define BSSAP_IMEISV                           0x15
#define BSSAP_MM_INFORMATION                   0x17
#define BSSAP_CELL_GBL_ID                      0x18
#define BSSAP_LOC_INF_AGE                      0x19
#define BSSAP_MOBILE_STN_STATE                 0x1a
#define BSSAP_ERRONEOUS_MSG                    0x1b
#define BSSAP_DLINK_TNL_PLD_CTR_AND_INF        0x1c
#define BSSAP_ULINK_TNL_PLD_CTR_AND_INF        0x1d
#define BSSAP_SERVICE_AREA_ID                  0x1e
#define BSSAP_MSI_BASED_NRI_CON                0x1f

static const value_string bssap_plus_ie_id_values[] = {
    { BSSAP_IMSI,                               "IMSI" },                                       /* 18.4.10 */
    { BSSAP_VLR_NUMBER,                         "VLR number" },                                 /* 18.4.26 */
    { BSSAP_TMSI,                               "TMSI" },                                       /* 18.4.23 */
    { BSSAP_LOC_AREA_ID,                        "Location area identifier" },                   /* 18.4.14 */
    { BSSAP_CHANNEL_NEEDED,                     "Channel Needed" },                             /* 18.4.2  */
    { BSSAP_EMLPP_PRIORITY,                     "eMLPP Priority" },                             /* 18.4.4  */
    { BSSAP_TMSI_STATUS,                        "TMSI status" },                                /* 18.4.24 */
    { BSSAP_GS_CAUSE,                           "Gs cause" },                                   /* 18.4.7  */
    { BSSAP_SGSN_NUMBER,                        "SGSN number" },                                /* 18.4.22 */
    { BSSAP_GPRS_LOC_UPD_TYPE,                  "GPRS location update type" },                  /* 18.4.6  */
    { BSSAP_GLOBAL_CN_ID,                       "Global CN-Id" },                               /* 18.4.27 */
    { 0x0c,                                     "Unassigned: treated as an unknown IEI." },     /* 18 and 16 */
    { BSSAP_MOBILE_STN_CLS_MRK1,                "Mobile station classmark 1" },                 /* 18.4.18 */
    { BSSAP_MOBILE_ID,                          "Mobile identity" },                            /* 18.4.17 */
    { BSSAP_REJECT_CAUSE,                       "Reject cause" },                               /* 18.4.21 */
    { BSSAP_IMSI_DET_FROM_GPRS_SERV_TYPE,       "IMSI detach from GPRS service type" },         /* 18.4.11 */
    { BSSAP_IMSI_DET_FROM_NON_GPRS_SERV_TYPE,   "IMSI detach from non-GPRS service type" },     /* 18.4.12 */
    { BSSAP_INFO_REQ,                           "Information requested" },                      /* 18.4.13 */
    { BSSAP_PTMSI,                              "PTMSI" },                                      /* 18.4.20 */
    { BSSAP_IMEI,                               "IMEI" },                                       /* 18.4.8  */
    { BSSAP_IMEISV,                             "IMEISV" },                                     /* 18.4.9 */
    { 0x16,                                     "Unassigned: treated as an unknown IEI." },     /* 18 and 16 */
    { BSSAP_MM_INFORMATION,                     "MM information" },                             /* 18.4.16 */
    { BSSAP_CELL_GBL_ID,                        "Cell Global Identity" },                       /* 18.4.1 */
    { BSSAP_LOC_INF_AGE,                        "Location information age" },                   /* 18.4.15 */
    { BSSAP_MOBILE_STN_STATE,                   "Mobile station state" },                       /* 18.4.19 */
    { BSSAP_ERRONEOUS_MSG,                      "Erroneous message" },                          /* 18.4.5 */
    { BSSAP_DLINK_TNL_PLD_CTR_AND_INF,          "Downlink Tunnel Payload Control and Info" },   /* 18.4.3 */
    { BSSAP_ULINK_TNL_PLD_CTR_AND_INF,          "Uplink Tunnel Payload Control and Info" },     /* 18.4.25 */
    { BSSAP_SERVICE_AREA_ID,                    "Service Area Identification" },                /* 18.4.21b */
    { BSSAP_MSI_BASED_NRI_CON,                  "TMSI based NRI container" },                   /* 18.4.28 */
    { 0,                NULL }
};
static value_string_ext bssap_plus_ie_id_values_ext = VALUE_STRING_EXT_INIT(bssap_plus_ie_id_values);

/* Initialize the protocol and registered fields */
static int proto_bssap;
static int proto_bsap;
static int proto_bssap_le;
static int proto_bssap_plus;
static int hf_bssap_pdu_type;
static int hf_bsap_pdu_type;
static int hf_bssap_dlci_cc;
static int hf_bsap_dlci_cc;
static int hf_bssap_dlci_spare;
static int hf_bsap_dlci_rsvd;
static int hf_bssap_dlci_sapi;
static int hf_bsap_dlci_sapi;
static int hf_bssap_length;
static int hf_bssap_plus_ie;
static int hf_bssap_plus_ie_len;

static int hf_bssap_plus_message_type;
static int hf_bssap_imsi_ie;
static int hf_bssap_imsi_det_from_gprs_serv_type_ie;
static int hf_bssap_imsi_det_from_non_gprs_serv_type_ie;
static int hf_bssap_info_req_ie;
static int hf_bssap_loc_area_id_ie;
static int hf_bssap_loc_inf_age_ie;
static int hf_bssap_mm_information_ie;
static int hf_bssap_mobile_id_ie;
static int hf_bssap_mobile_stn_cls_mrk1_ie;
static int hf_bssap_mobile_station_state_ie;
static int hf_bssap_ptmsi_ie;
static int hf_bssap_reject_cause_ie;
static int hf_bssap_service_area_id_ie;
static int hf_bssap_sgsn_nr_ie;
static int hf_bssap_tmsi_ie;
static int hf_bssap_tmsi_status_ie;
static int hf_bssap_vlr_number_ie;
static int hf_bssap_global_cn_id_ie;
static int hf_bssap_plus_ie_data;

static int hf_bssap_extension;
static int hf_bssap_type_of_number;
static int hf_bssap_numbering_plan_id;
static int hf_bssap_sgsn_number;
static int hf_bssap_vlr_number;
static int hf_bssap_call_priority;
static int hf_bssap_gprs_loc_upd_type_ie;
static int hf_bssap_Gs_cause_ie;
static int hf_bssap_imei_ie;
static int hf_bssap_imeisv_ie;
static int hf_bssap_cell_global_id_ie;
static int hf_bssap_channel_needed_ie;
static int hf_bssap_dlink_tnl_pld_cntrl_amd_inf_ie;
static int hf_bssap_ulink_tnl_pld_cntrl_amd_inf_ie;
static int hf_bssap_emlpp_prio_ie;
static int hf_bssap_gprs_erroneous_msg_ie;

static int hf_bssap_gprs_loc_upd_type;
static int hf_bssap_Gs_cause;
static int hf_bssap_imei;
static int hf_bssap_imeisv;
static int hf_bssap_imsi_det_from_gprs_serv_type;
static int hf_bssap_info_req;
static int hf_bssap_loc_inf_age;
static int hf_bssap_mobile_station_state;
static int hf_bssap_ptmsi;
static int hf_bssap_tmsi;
static int hf_bssap_tmsi_status;
static int hf_bssap_tom_prot_disc;
static int hf_bssap_e_bit;
static int hf_bssap_tunnel_prio;
static int hf_bssap_global_cn_id;
static int hf_bssap_plmn_id;
static int hf_bssap_cn_id;
static int hf_bssap_cell_global_id;
static int hf_bssap_extraneous_data;
static int hf_bssap_conditional_ie;

/* Initialize the subtree pointers */
static int ett_bssap;
static int ett_bssap_dlci;
static int ett_bssap_imsi;
static int ett_bssap_imsi_det_from_gprs_serv_type;
static int ett_bssap_imsi_det_from_non_gprs_serv_type;
static int ett_bssap_info_req;
static int ett_bssap_loc_area_id;
static int ett_bssap_loc_inf_age;
static int ett_bssap_mm_information;
static int ett_bssap_mobile_id;
static int ett_bssap_sgsn_nr;
static int ett_bssap_tmsi;
static int ett_bssap_tmsi_status;
static int ett_bssap_vlr_number;
static int ett_bssap_global_cn;
static int ett_bssap_gprs_loc_upd;
static int ett_bassp_Gs_cause;
static int ett_bassp_imei;
static int ett_bassp_imeisv;
static int ett_bssap_cell_global_id;
static int ett_bssap_cgi;
static int ett_bssap_channel_needed;
static int ett_bssap_dlink_tnl_pld_cntrl_amd_inf;
static int ett_bssap_ulink_tnl_pld_cntrl_amd_inf;
static int ett_bssap_emlpp_prio;
static int ett_bssap_erroneous_msg;
static int ett_bssap_mobile_stn_cls_mrk1;
static int ett_bssap_mobile_station_state;
static int ett_bssap_ptmsi;
static int ett_bssap_reject_cause;
static int ett_bssap_service_area_id;
static int ett_bssap_global_cn_id;
static int ett_bssap_plmn;

static expert_field ei_bssap_unknown_message;
static expert_field ei_bssap_unknown_parameter;
static expert_field ei_bssap_mandatory_ie;


static dissector_handle_t rrlp_handle;

static dissector_table_t bssap_dissector_table;
static dissector_table_t bsap_dissector_table;
static dissector_table_t bssap_le_dissector_table;

static dissector_handle_t gsm_bssmap_le_dissector_handle;
static dissector_handle_t gsm_a_bssmap_dissector_handle;


typedef struct {
    uint8_t pdu_type;
    enum bssap_proto proto;
} bssap_info_t;

static int default_protocol_global = BSSAP;

static void
dissect_bssap_data_param(tvbuff_t *tvb, packet_info *pinfo,
            proto_tree *bssap_tree, proto_tree *tree, struct _sccp_msg_info_t* sccp_info)
{
    bssap_info_t* bssap_info;

    bssap_info = (bssap_info_t *)p_get_proto_data(pinfo->pool, pinfo, proto_bssap, 0);
    uint8_t pdu_type = bssap_info->pdu_type;

    switch (bssap_info->proto) {

    case BSSAP:
        if (dissector_try_uint_new(bssap_dissector_table, pdu_type, tvb, pinfo, tree, true, sccp_info))
            return;
        break;

    case BSSAP_LE:
        if (dissector_try_uint_new(bssap_le_dissector_table, pdu_type, tvb, pinfo, tree, true, sccp_info))
            return;
        break;

    case BSAP:
        if (dissector_try_uint_new(bsap_dissector_table, pdu_type, tvb, pinfo, tree, true, sccp_info))
            return;
        break;
    }

    /* No sub-dissection occurred, treat it as raw data */
    call_data_dissector(tvb, pinfo, bssap_tree);
}

static void
dissect_bssap_dlci_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, uint16_t length)
{
    proto_tree *dlci_tree;
    uint8_t     oct;
    bssap_info_t* bssap_info;

    bssap_info = (bssap_info_t *)p_get_proto_data(pinfo->pool, pinfo, proto_bssap, 0);

    dlci_tree =
        proto_tree_add_subtree(tree, tvb, 0, length,
                    ett_bssap_dlci, NULL, "Data Link Connection Identifier");

    oct = tvb_get_uint8(tvb, 0);

    if (bssap_info->proto != BSAP)
    {
        proto_tree_add_uint(dlci_tree, hf_bssap_dlci_cc, tvb, 0, length, oct);
        proto_tree_add_uint(dlci_tree, hf_bssap_dlci_spare, tvb, 0, length, oct);
        proto_tree_add_uint(dlci_tree, hf_bssap_dlci_sapi, tvb, 0, length, oct);
    }
    else
    {
        proto_tree_add_uint(dlci_tree, hf_bsap_dlci_cc, tvb, 0, length, oct);
        proto_tree_add_uint(dlci_tree, hf_bsap_dlci_rsvd, tvb, 0, length, oct);
        proto_tree_add_uint(dlci_tree, hf_bsap_dlci_sapi, tvb, 0, length, oct);
    }
}

static void
dissect_bssap_length_param(tvbuff_t *tvb, proto_tree *tree, uint16_t length)
{
    uint8_t data_length;

    data_length = tvb_get_uint8(tvb, 0);
    proto_tree_add_uint(tree, hf_bssap_length, tvb, 0, length, data_length);
}

/*
 * Dissect a parameter given its type, offset into tvb, and length.
 */
static uint16_t
dissect_bssap_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *bssap_tree,
               proto_tree *tree, uint8_t parameter_type, int offset,
               uint16_t parameter_length, struct _sccp_msg_info_t* sccp_info)
{
    tvbuff_t *parameter_tvb;

    parameter_tvb = tvb_new_subset_length(tvb, offset, parameter_length);

    switch (parameter_type)
    {
    case PARAMETER_DLCI:
        dissect_bssap_dlci_param(parameter_tvb, pinfo, bssap_tree, parameter_length);
        break;

    case PARAMETER_LENGTH:
        dissect_bssap_length_param(parameter_tvb, bssap_tree, parameter_length);
        break;

    case PARAMETER_DATA:
        dissect_bssap_data_param(parameter_tvb, pinfo, bssap_tree, tree, sccp_info);
        break;

    default:
        proto_tree_add_expert_format(bssap_tree, pinfo, &ei_bssap_unknown_parameter, parameter_tvb, 0, parameter_length,
                "Unknown parameter 0x%x (%u byte%s)", parameter_type, parameter_length, plurality(parameter_length, "", "s"));
        break;
    }

    return parameter_length;
}

static uint16_t
dissect_bssap_var_parameter(tvbuff_t *tvb, packet_info *pinfo,
                proto_tree *bssap_tree, proto_tree *tree,
                uint8_t parameter_type, int offset, struct _sccp_msg_info_t* sccp_info)
{
    uint16_t parameter_length;
    uint8_t length_length;

    parameter_length = tvb_get_uint8(tvb, offset);
    length_length = LENGTH_LENGTH;

    offset += length_length;

    dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree, parameter_type,
                offset, parameter_length, sccp_info);

    return parameter_length + length_length;
}

static int
dissect_bssap_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *bssap_tree,
             proto_tree *tree, struct _sccp_msg_info_t* sccp_info)
{
    int offset;
    proto_item* type_item;
    bssap_info_t* bssap_info;

    bssap_info = (bssap_info_t *)p_get_proto_data(pinfo->pool, pinfo, proto_bssap, 0);

    /*
     * Extract the PDU type
     */
    bssap_info->pdu_type = tvb_get_uint8(tvb, PDU_TYPE_OFFSET);
    offset = PDU_TYPE_LENGTH;

    /*
        * add the message type to the protocol tree
        */
    type_item = proto_tree_add_uint(bssap_tree,
                (bssap_info->proto != BSAP) ? hf_bssap_pdu_type : hf_bsap_pdu_type,
                tvb, PDU_TYPE_OFFSET, PDU_TYPE_LENGTH, bssap_info->pdu_type);

    /* Starting a new message dissection */

    switch (bssap_info->pdu_type)
    {
    case BSSAP_PDU_TYPE_BSSMAP:
        offset += dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree,
                          PARAMETER_LENGTH, offset,
                          LENGTH_LENGTH, sccp_info);
        offset += dissect_bssap_var_parameter(tvb, pinfo, bssap_tree, tree,
                              PARAMETER_DATA,
                              (offset - LENGTH_LENGTH), sccp_info);
        break;

    case BSSAP_PDU_TYPE_DTAP:
        offset += dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree,
                          PARAMETER_DLCI,
                          offset, DLCI_LENGTH, sccp_info);
        offset += dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree,
                          PARAMETER_LENGTH, offset,
                          LENGTH_LENGTH, sccp_info);
        offset += dissect_bssap_var_parameter(tvb, pinfo, bssap_tree, tree,
                              PARAMETER_DATA,
                              (offset - LENGTH_LENGTH), sccp_info);
        break;

    default:
        {
        uint32_t message_length;

        col_append_fstr(pinfo->cinfo, COL_INFO, "%s ",
                val_to_str_const(bssap_info->pdu_type,
                                 ((bssap_info->proto != BSAP) ?
                                  bssap_pdu_type_acro_values : bsap_pdu_type_acro_values),
                                 "Unknown"));

        message_length = tvb_reported_length(tvb);

        expert_add_info_format(pinfo, type_item, &ei_bssap_unknown_message, "Unknown message (%u byte%s)",
                        message_length, plurality(message_length, "", "s"));
        }
        break;
    }
    return offset;
}

static int
dissect_bssap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    proto_item  *bssap_item;
    proto_tree  *bssap_tree;
    struct _sccp_msg_info_t* sccp_info = (struct _sccp_msg_info_t*)data;
    bssap_info_t *bssap_info;

    /*
     * Make entry in the Protocol column on summary display
     */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "BSSAP");

    if (sccp_info && sccp_info->data.co.assoc )
        sccp_info->data.co.assoc->payload = SCCP_PLOAD_BSSAP;

    /*
     * create the bssap protocol tree
     */
    bssap_item = proto_tree_add_item(tree, proto_bssap, tvb, 0, -1, ENC_NA);
    bssap_tree = proto_item_add_subtree(bssap_item, ett_bssap);

    bssap_info = wmem_new(pinfo->pool, bssap_info_t);
    bssap_info->proto = BSSAP;
    p_add_proto_data(pinfo->pool, pinfo, proto_bssap, 0, bssap_info);

    /* dissect the message */

    return dissect_bssap_message(tvb, pinfo, bssap_tree, tree, sccp_info);
}

static int
dissect_bsap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    proto_item  *bssap_item;
    proto_tree  *bssap_tree;
    struct _sccp_msg_info_t* sccp_info = (struct _sccp_msg_info_t*)data;
    bssap_info_t *bssap_info;

    /*
     * Make entry in the Protocol column on summary display
     */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "BSAP");

    if (sccp_info && sccp_info->data.co.assoc )
        sccp_info->data.co.assoc->payload = SCCP_PLOAD_BSSAP;

    /*
     * create the bsap protocol tree
     */
    bssap_item = proto_tree_add_protocol_format(tree, proto_bssap, tvb, 0, -1, "BSAP");
    bssap_tree = proto_item_add_subtree(bssap_item, ett_bssap);

    bssap_info = wmem_new(pinfo->pool, bssap_info_t);
    bssap_info->proto = BSAP;
    p_add_proto_data(pinfo->pool, pinfo, proto_bssap, 0, bssap_info);

    /* dissect the message */

    return dissect_bssap_message(tvb, pinfo, bssap_tree, tree, sccp_info);
}

static int
dissect_bssap_le(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    proto_item  *bssap_item;
    proto_tree  *bssap_tree;
    struct _sccp_msg_info_t* sccp_info = (struct _sccp_msg_info_t*)data;
    bssap_info_t *bssap_info;

    /*
     * Make entry in the Protocol column on summary display
     */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "BSSAP-LE");

    if (sccp_info && sccp_info->data.co.assoc )
        sccp_info->data.co.assoc->payload = SCCP_PLOAD_BSSAP;

    /*
     * create the bssap_le protocol tree
     */
    bssap_item = proto_tree_add_protocol_format(tree, proto_bssap, tvb, 0, -1, "BSSAP-LE");
    bssap_tree = proto_item_add_subtree(bssap_item, ett_bssap);

    bssap_info = wmem_new(pinfo->pool, bssap_info_t);
    bssap_info->proto = BSSAP_LE;
    p_add_proto_data(pinfo->pool, pinfo, proto_bssap, 0, bssap_info);

    /* dissect the message */

    return dissect_bssap_message(tvb, pinfo, bssap_tree, tree, sccp_info);
}

/*
 * BSSAP+ Routines
 */

static bool
check_ie(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, uint8_t expected_ie)
{
    uint8_t ie_type;
    uint8_t ie_len;

    ie_type = tvb_get_uint8(tvb, *offset);
    if (ie_type != expected_ie) {
        proto_tree_add_expert_format(tree, pinfo, &ei_bssap_mandatory_ie, tvb, *offset, 1, "Mandatory IE %s expected but IE %s Found",
                            val_to_str_ext(expected_ie, &bssap_plus_ie_id_values_ext, "Unknown %u"),
                            val_to_str_ext(ie_type, &bssap_plus_ie_id_values_ext, "Unknown %u"));
        (*offset)++;
        ie_len = tvb_get_uint8(tvb, *offset);
        *offset = *offset + ie_len;
        return false;
    }

    return true;

}

static bool
check_optional_ie(tvbuff_t *tvb, int offset, uint8_t expected_ie)
{
    uint8_t ie_type;

    ie_type = tvb_get_uint8(tvb, offset);
    if (ie_type != expected_ie) {
        return false;
    }
    return true;

}

/* 18.4.1 Cell global identity */
static int
dissect_bssap_cell_global_id(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    proto_item *cgi_item;
    proto_tree *cgi_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_cell_global_id_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_cell_global_id);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*
     * The rest of the information element is coded as the value part
     * of the cell global id IE defined in 3GPP TS 48.018 (not including
     * 3GPP TS 48.018 IEI and 3GPP TS 48.018 length indicator).
     */
    cgi_item = proto_tree_add_item(ie_tree, hf_bssap_cell_global_id, tvb, offset, ie_len, ENC_NA);
    cgi_tree = proto_item_add_subtree(cgi_item, ett_bssap_cgi);
    /*  octets 3-8 Octets 3 to 8 contain the value part (starting with octet 2) of the
     *  Routing Area Identification IE defined in 3GPP TS 24.008, not
     *  including 3GPP TS 24.008 IEI
     */
    de_gmm_rai(tvb, cgi_tree, pinfo, offset, ie_len, NULL, 0);
    /*  Octets 9 and 10 contain the value part (starting with octet 2) of the
     *  Cell Identity IE defined in 3GPP TS 24.008, not including
     *  3GPP TS 24.008 IEI
     */
    offset = offset + 6;
    de_cell_id(tvb, cgi_tree, pinfo, offset, ie_len, NULL, 0);
    offset = offset + 2;

    return offset;

}
/* 18.4.2 Channel needed */
static int
dissect_bssap_channel_needed(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_channel_needed_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_channel_needed);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*
     * The rest of the information element is coded as the IEI part and the
     * value part of the Channel Needed IE defined in 3GPP TS 44.018.
     * 10.5.2.8 Channel Needed
     */
    de_rr_chnl_needed(tvb, ie_tree, pinfo, offset, ie_len, NULL, 0);

    return offset + ie_len;

}
/* 18.4.3 Downlink Tunnel Payload Control and Info */
static int
dissect_bssap_dlink_tunnel_payload_control_and_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    tvbuff_t   *next_tvb;
    uint8_t     ie_len;
    uint8_t     octet;
    uint8_t     prot_disc;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_dlink_tnl_pld_cntrl_amd_inf_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_dlink_tnl_pld_cntrl_amd_inf);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* Bit 8 Spare */
    /* Bit 7 - 4
     * TOM Protocol Discriminator: Identifies the protocol using tunnelling of non-GSM signalling.
     * For coding, see 3GPP TS 44.064.
     */

    proto_tree_add_item(ie_tree, hf_bssap_tom_prot_disc, tvb, offset, 1, ENC_BIG_ENDIAN);
    octet = tvb_get_uint8(tvb, offset);
    prot_disc = (octet&0x78)>>3;

    /* octet 3 bit 3 E: Cipher Request. When set to 1 indicates that the SGSN received the payload in ciphered form,
     * when set to 0 indicates that the SGSN did not receive the payload in ciphered form.
     */
    proto_tree_add_item(ie_tree, hf_bssap_e_bit, tvb, offset, 1, ENC_BIG_ENDIAN);

    /* octet 3 bit 2 - 1
     * Tunnel Priority: Indicates the priority of the Tunnel Payload. For coding, see Table 20.1: Association
     * between Tunnel Priority and LLC SAPs.
     */
    proto_tree_add_item(ie_tree, hf_bssap_tunnel_prio, tvb, offset, 1, ENC_BIG_ENDIAN);
    /* Tunnel payload */
    next_tvb = tvb_new_subset_length(tvb, offset, ie_len-4);

    if ((prot_disc == 2)&&(rrlp_handle))
        call_dissector(rrlp_handle, next_tvb, pinfo, ie_tree);
    else
        call_data_dissector(next_tvb, pinfo, ie_tree);


    return offset + ie_len;

}

/* 18.4.4 eMLPP Priority */
/* Call priority */
static const value_string bssap_call_priority_values[] = {
    { 0x00,             "No priority applied" },
    { 0x01,             "Call priority level 4" },
    { 0x02,             "Call priority level 3" },
    { 0x03,             "Call priority level 2" },
    { 0x04,             "Call priority level 1" },
    { 0x05,             "Call priority level 0" },
    { 0x06,             "Call priority level B" },
    { 0x07,             "Call priority level A" },
    { 0,                NULL }
};
static int
dissect_bssap_emlpp_priority(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_emlpp_prio_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_emlpp_prio);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*  The rest of the information element is coded as the value part of
        the eMLPP-Priority IE defined in 3GPP TS 48.008 (not including
        3GPP TS 48.008 IEI and 3GPP TS 48.008 length indicator).
        3.2.2.56 eMLPP Priority
        The call priority field (bit 3 to 1 of octet 2) is coded in the same way
        as the call priority field (bit 3 to 1 of octet 5) in the Descriptive group
        or broadcast call reference information element as defined in 3GPP TS 24.008.
     */
    proto_tree_add_item(ie_tree, hf_bssap_call_priority, tvb, offset, ie_len, ENC_BIG_ENDIAN);

    return offset + ie_len;

}
/* 18.4.5 Erroneous message */
    /* Erroneous message including the message type. */

static int
dissect_bssap_gprs_erroneous_msg(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_gprs_erroneous_msg_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_erroneous_msg);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;

    /* Erroneous message including the message type. */
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_data, tvb, offset, ie_len, ENC_NA);

    return offset + ie_len;

}


static const value_string bssap_plus_GPRS_loc_upd_type_values[] = {
    { 0x00,             "Shall not be sent in this version of the protocol. If received, shall be treated as '00000010'." },
    { 0x01,             "IMSI attach" },
    { 0x02,             "Normal location update" },
    { 0,                NULL }
};
/* 18.4.6 GPRS location update type */
static int
dissect_bssap_gprs_location_update_type(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_gprs_loc_upd_type_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_gprs_loc_upd);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;

    /* GPRS location update type value (octet 3) */
    proto_tree_add_item(ie_tree, hf_bssap_gprs_loc_upd_type, tvb, offset, ie_len, ENC_BIG_ENDIAN);

    return offset + ie_len;
}

/* Gs Cause value (octet 3) */
static const value_string bssap_Gs_cause_values[] = {

    { 0x00,     "Normal, unspecified in this version of the protocol." },
    { 0x01,     "IMSI detached for GPRS services" },
    { 0x02,     "IMSI detached for GPRS and non-GPRS services" },
    { 0x03,     "IMSI unknown" },
    { 0x04,     "IMSI detached for non-GPRS services" },
    { 0x05,     "IMSI implicitly detached for non-GPRS services" },
    { 0x06,     "MS unreachable" },
    { 0x07,     "Message not compatible with the protocol state" },
    { 0x08,     "Missing mandatory information element" },
    { 0x09,     "Invalid mandatory information" },
    { 0x0a,     "Conditional IE error" },
    { 0x0b,     "Semantically incorrect message" },
    { 0x0c,     "Message unknown" },
    { 0x0d,     "Address error" },
    { 0x0e,     "TOM functionality not supported" },
    { 0x0f,     "Ciphering request cannot be accommodated" },
    { 0,        NULL }
};

/* 18.4.7 Gs cause */
static int
dissect_bssap_Gs_cause(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_Gs_cause_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bassp_Gs_cause);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* Gs Cause value (octet 3) */
    proto_tree_add_item(ie_tree, hf_bssap_Gs_cause,    tvb, offset, ie_len, ENC_BIG_ENDIAN);


    return offset + ie_len;

}
/* 18.4.8 IMEI */
static int
dissect_bssap_imei(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;
    tvbuff_t   *ie_tvb;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_imei_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bassp_imei);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* The IMEI is coded as a sequence of BCD digits, compressed two into each octet.
     * The IMEI consists of 15 digits (see 3GPP TS 23.003).
     */
    ie_tvb = tvb_new_subset_length(tvb, offset, ie_len);
    proto_tree_add_item(ie_tree, hf_bssap_imei, ie_tvb, 0, -1, ENC_BCD_DIGITS_0_9|ENC_LITTLE_ENDIAN);

    return offset + ie_len;

}
static int
dissect_bssap_imei_dissector(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
{
    return dissect_bssap_imei(tvb, tree, 0);
}
/* 18.4.9 IMEISV */
static int
dissect_bssap_imeisv(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;
    tvbuff_t   *ie_tvb;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_imeisv_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bassp_imeisv);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*  The IMEISV is coded as a sequence of BCD digits, compressed two into each octet.
     *  The IMEISV consists of 16 digits (see 3GPP TS 23.003).
     */
    ie_tvb = tvb_new_subset_length(tvb, offset, ie_len);
    proto_tree_add_item(ie_tree, hf_bssap_imeisv, ie_tvb, 0, -1, ENC_BCD_DIGITS_0_9|ENC_LITTLE_ENDIAN);

    return offset + ie_len;

}
/* 18.4.10 IMSI
 * The IMSI is coded as a sequence of BCD digits, compressed two into each octet.
 * This is a variable length element, and includes a length indicator.
 * The IMSI is defined in 3GPP TS 23.003. It shall not exceed 15 digits (see 3GPP TS 23.003).
 */


static int
dissect_bssap_imsi(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;
    tvbuff_t   *ie_tvb;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_imsi_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_imsi);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    ie_tvb = tvb_new_subset_length(tvb, offset, ie_len);
    dissect_e212_imsi(ie_tvb, pinfo, tree,  0, ie_len, true);

    return offset + ie_len;

}
static const value_string bssap_imsi_det_from_gprs_serv_type_values[] _U_ = {
    { 0x00,     "Interpreted as reserved in this version of the protocol" },
    { 0x01,     "Network initiated IMSI detach from GPRS service" },
    { 0x02,     "MS initiated IMSI detach from GPRS service" },
    { 0x03,     "GPRS services not allowed" },
    { 0,                NULL }
};

/* 18.4.11 IMSI detach from GPRS service type */
static int
dissect_bssap_imsi_det_from_gprs_serv_type(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_imsi_det_from_gprs_serv_type_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_imsi_det_from_gprs_serv_type);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* IMSI detach from GPRS service type value (octet 3)*/
    proto_tree_add_item(ie_tree, hf_bssap_imsi_det_from_gprs_serv_type, tvb, offset, ie_len, ENC_BIG_ENDIAN);


    return offset + ie_len;

}
/* 18.4.12 IMSI detach from non-GPRS service type */
static int
dissect_bssap_imsi_det_from_non_gprs_serv_type(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_imsi_det_from_non_gprs_serv_type_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_imsi_det_from_non_gprs_serv_type);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,      tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len,  tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_data, tvb, offset, ie_len, ENC_NA);


    return offset + ie_len;

}
static const value_string bssap_info_req_values[] = {
    { 0x00,     "Interpreted as Not supported in this version of the protocol." },
    { 0x01,     "PTMSI" },
    { 0x02,     "IMEI" },
    { 0x03,     "IMEISV" },
    { 0x04,     "PTMSI and IMEI" },
    { 0x05,     "PTMSI and IMEISV" },
    { 0x06,     "IMEI and IMEISV" },
    { 0x07,     "PTMSI, IMEI, and IMEISV" },
    { 0x08,     "Mobile location information" },
    { 0x09,     "TMSI" },
    { 0,                NULL }
};
/* 18.4.13 Information requested */
static int
dissect_bssap_info_req(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_info_req_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_info_req);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*Information requested value (octet 3) */
    proto_tree_add_item(ie_tree, hf_bssap_info_req,    tvb, offset, ie_len, ENC_BIG_ENDIAN);


    return offset + ie_len;

}
/* 18.4.14 Location area identifier */
static int
dissect_bssap_loc_area_id(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_loc_area_id_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_loc_area_id);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*  The rest of the information element is coded as the value part of
     *  the location area identifier IE defined in 3GPP TS 48.018 (not
     *  including 3GPP TS 48.018 IEI and 3GPP TS 48.018 length
     *  indicator).
     */
    de_lai(tvb, ie_tree, pinfo, offset, ie_len, NULL, 0);

    return offset + ie_len;

}
/* 18.4.15 Location information age */
static int
dissect_bssap_location_information_age(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_loc_inf_age_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_loc_inf_age);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* The rest of the IE is coded as the value part of the
     * AgeOfLocationInformation as specified in 3GPP TS 29.002.:
     *   AgeOfLocationInformation ::= INTEGER (0..32767)
     * -- the value represents the elapsed time in minutes since the last
     * -- network contact of the mobile station (i.e. the actuality of the
     * -- location information).
     * -- value '0' indicates that the MS is currently in contact with the
     * -- network
     * -- value '32767' indicates that the location information is at least
     * -- 32767 minutes old
     */
    proto_tree_add_item(ie_tree, hf_bssap_loc_inf_age, tvb, offset, ie_len, ENC_BIG_ENDIAN);


    return offset + ie_len;

}
/* 18.4.16 MM information */
static int
dissect_bssap_MM_information(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_mm_information_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_mm_information);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* User information: This field is composed of one or more of the
     *  information elements of the MM information message as defined
     *  3GPP TS 24.008, excluding the Protocol discriminator, Skip
     *  indicator and Message type. This field includes the IEI and length
     *  indicatior of the other information elements.
     */
    dtap_mm_mm_info(tvb, ie_tree, pinfo, offset, ie_len);


    return offset + ie_len;

}
/* 18.4.17 Mobile identity */
static int
dissect_bssap_mobile_id(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    unsigned    ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_mobile_id_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_mobile_id);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*  The rest of the information element is coded as the value part of
     *  the mobile identity IE defined in 3GPP TS 24.008 (not including
     *  3GPP TS 24.008 IEI and 3GPP TS 24.008 length indicator).
     */
    de_mid(tvb, ie_tree, pinfo, offset, ie_len, NULL, 0);


    return offset + ie_len;

}
/* 18.4.18 Mobile station classmark 1 */
static int
dissect_bssap_mobile_stn_cls_mrk1(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_mobile_stn_cls_mrk1_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_mobile_stn_cls_mrk1);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*  The rest of the information element is coded as the value part of
     *  the mobile station classmark 1 IE defined in 3GPP TS 24.008 (not
     *  including 3GPP TS 24.008 IEI)
     */
    de_ms_cm_1(tvb, ie_tree, pinfo, offset, ie_len, NULL, 0);

    return offset + ie_len;

}
/* 18.4.19 Mobile station state */
static const value_string bssap_mobile_station_state_values[] = {
    { 0x00,     "IDLE or PMM-DETACHED" },
    { 0x01,     "STANDBY or PMM-IDLE, 0 PDP contexts active" },
    { 0x02,     "STANDBY or PMM-IDLE, 1 or more PDP contexts active" },
    { 0x03,     "SUSPENDED, 0 PDP contexts active" },
    { 0x04,     "SUSPENDED, 1 or more PDP contexts active" },
    { 0x05,     "READY or PMM-CONNECTED, 0 PDP contexts active" },
    { 0x06,     "READY or PMM-CONNECTED, 1 or more PDP contexts active" },
    { 0x07,     "IMSI unknown" },
    { 0x08,     "Information requested not supported" },
    { 0,                NULL }
};
static int
dissect_bssap_mobile_station_state(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_mobile_station_state_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_mobile_station_state);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* Mobile station state value (octet 3) */
    proto_tree_add_item(ie_tree, hf_bssap_mobile_station_state, tvb, offset, ie_len, ENC_BIG_ENDIAN);

    return offset + ie_len;

}
/* 18.4.20 PTMSI */
static int
dissect_bssap_ptmsi(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_ptmsi_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_ptmsi);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* The PTMSI consists of 4 octets. It can be coded using a full hexadecimal representation
     * (see 3GPP TS 23.003).
     */
    proto_tree_add_item(ie_tree, hf_bssap_ptmsi, tvb, offset, ie_len, ENC_NA);

    return offset + ie_len;

}
/* 18.4.21 Reject cause */
static int
dissect_bssap_reject_cause(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_reject_cause_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_reject_cause);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*  The rest of the information element is coded as the value part of
     *  the reject cause IE defined in 3GPP TS 24.008, not including
     *  3GPP TS 24.008 IEI.
     */
    de_rej_cause(tvb, ie_tree, pinfo, offset, ie_len, NULL, 0);

    return offset + ie_len;

}

/* 18.4.21b Service Area Identification */
static int
dissect_bssap_service_area_id(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_service_area_id_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_service_area_id);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*  The rest of the information element is coded as the value part
     *  of the SAI IE defined in 3GPP TS 25.413 (not including
     *  3GPP TS 25.413 IEI and 3GPP TS 25.413 length indicator).
     */
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_data, tvb, offset, ie_len, ENC_NA);

    return offset + ie_len;

}

/* 18.4.22 SGSN number */

static int
dissect_bssap_sgsn_number(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;
    tvbuff_t   *number_tvb;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_sgsn_nr_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_sgsn_nr);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* The SGSN number is coded as a sequence of TBCD digits (as specified in 3GPP TS 29.002),
     * compressed two into each octet. The Number is in international E.164 format as indicated by Octet 3
     * which coding is specified in 3GPP TS 29.002. This is a variable length information element,
     * and includes a length indicator. The value part of the SGSN number information element
     * (not including IEI, Length indicator and Octet 3) shall not exceed 15 digits.
     */
    proto_tree_add_item(ie_tree, hf_bssap_extension,         tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(ie_tree, hf_bssap_type_of_number,    tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(ie_tree, hf_bssap_numbering_plan_id, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    number_tvb = tvb_new_subset_length(tvb, offset, ie_len-1);
    proto_tree_add_item(ie_tree, hf_bssap_sgsn_number, number_tvb, 0, -1, ENC_BCD_DIGITS_0_9|ENC_LITTLE_ENDIAN);


    return offset + ie_len-1;

}
/* 18.4.23 TMSI */
static int
dissect_bssap_tmsi(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_tmsi_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_tmsi);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* The TMSI consists of 4 octets. It can be coded using a full hexadecimal representation
     * (see 3GPP TS 23.003).
     */
    proto_tree_add_item(ie_tree, hf_bssap_tmsi, tvb, offset, ie_len, ENC_NA);


    return offset + ie_len;

}

/* 18.4.24 TMSI status */
static const true_false_string bssap_tmsi_flag = {
  "Valid TMSI available",
  "No valid TMSI available"
};
static int
dissect_bssap_tmsi_status(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_tmsi_status_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_tmsi_status);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* TMSI flag (octet 3) */
    proto_tree_add_item(ie_tree, hf_bssap_tmsi_status, tvb, offset, ie_len, ENC_BIG_ENDIAN);


    return offset + ie_len;

}
/* 18.4.25 Uplink Tunnel Payload Control and Info */
static const true_false_string bssap_E_flag = {
  "SGSN received the payload in ciphered",
  "SGSN did not receive the payload in ciphered form"
};
/* 3GPP TS 44.064 B.1.1 TOM Protocol Discriminator */
static const value_string bssap_tom_prot_disc_values[] = {
    { 0x00,     "Not specified" },
    { 0x01,     "TIA/EIA-136" },
    { 0x02,     "RRLP" },
    { 0x03,     "Reserved for extension" },
    { 0,                NULL }
};
static int
dissect_bssap_ulink_tunnel_payload_control_and_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    tvbuff_t   *next_tvb;
    uint8_t     ie_len;
    uint8_t     octet;
    uint8_t     prot_disc;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_ulink_tnl_pld_cntrl_amd_inf_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_ulink_tnl_pld_cntrl_amd_inf);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* octet 3 bit 8 Spare */
    /* octet 3 bit 7 - 4
     * TOM Protocol Discriminator: Identifies the protocol using tunnelling of non-GSM signalling.
     * For coding, see 3GPP TS 44.064.
     */
    proto_tree_add_item(ie_tree, hf_bssap_tom_prot_disc, tvb, offset, 1, ENC_BIG_ENDIAN);
    octet     = tvb_get_uint8(tvb, offset);
    prot_disc = (octet&0x78)>>3;

    /* octet 3 bit 3 E: Cipher Request. When set to 1 indicates that the SGSN received the payload in ciphered form,
     * when set to 0 indicates that the SGSN did not receive the payload in ciphered form.
     */
    proto_tree_add_item(ie_tree, hf_bssap_e_bit, tvb, offset, 1, ENC_BIG_ENDIAN);

    /* octet 3 bit 2 - 1
     * Tunnel Priority: Indicates the priority of the Tunnel Payload. For coding, see Table 20.1: Association
     * between Tunnel Priority and LLC SAPs.
     */
    proto_tree_add_item(ie_tree, hf_bssap_tunnel_prio, tvb, offset, 1, ENC_BIG_ENDIAN);
    /* Tunnel payload */
    next_tvb = tvb_new_subset_length(tvb, offset, ie_len - 4);

    if ((prot_disc == 2)&&(rrlp_handle))
        call_dissector(rrlp_handle, next_tvb, pinfo, ie_tree);
    else
        call_data_dissector(next_tvb, pinfo, ie_tree);

    return offset + ie_len;

}

/* 18.4.26 VLR number */
static int
dissect_bssap_vlr_number(tvbuff_t *tvb, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    uint8_t     ie_len;
    tvbuff_t   *number_tvb;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_vlr_number_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_vlr_number);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /*  The VLR number is coded as a sequence of TBCD digits (as specified in 3GPP TS 29.002),
     *  compressed two into each octet. The Number is in international E.164 format as indicated by Octet 3
     *  which coding is specified in 3GPP TS 29.002. This is a variable length information element,
     *  and includes a length indicator. The value part of the VLR number information element
     *  (not including IEI, length indicator and Octet 3), shall not exceed 15 digits.
     */

    proto_tree_add_item(ie_tree, hf_bssap_extension,         tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(ie_tree, hf_bssap_type_of_number,    tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(ie_tree, hf_bssap_numbering_plan_id, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    number_tvb = tvb_new_subset_length(tvb, offset, ie_len - 1);
    proto_tree_add_item(ie_tree, hf_bssap_vlr_number, number_tvb, 0, -1, ENC_BCD_DIGITS_0_9|ENC_LITTLE_ENDIAN);

    return offset + ie_len - 1;

}
/* 18.4.27 Global CN-Id */
static int
dissect_bssap_global_cn_id(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset)
{
    proto_item *item;
    proto_tree *ie_tree;
    proto_item *plmn_item;
    proto_tree *plmn_tree;
    proto_item *global_cn_id_item;
    proto_tree *global_cn_id_tree;
    uint8_t     ie_len;

    ie_len  = tvb_get_uint8(tvb, offset+1);
    item    = proto_tree_add_item(tree, hf_bssap_global_cn_id_ie, tvb, offset, ie_len+2, ENC_NA);
    ie_tree = proto_item_add_subtree(item, ett_bssap_global_cn);

    proto_tree_add_item(ie_tree, hf_bssap_plus_ie,     tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(ie_tree, hf_bssap_plus_ie_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    /* The Global CN-Id consists of a PLMN-Id and a CN-Id, see 3GPP TS 23.003.
     *  The PLMN-Id consists of MCC and MNC coded according to Location Area Identification
     * in 3GPP TS 24.008. The CN-Id is an integer defined by O&M.
     * The least significant bit of the CN-Id field is bit 1 of octet 7 and
     * the most significant bit is bit 8 of octet 6. If the CN-Id does not fill the field reserved for it,
     * the rest of the bits are set to '0'.
     */
    global_cn_id_item = proto_tree_add_item(ie_tree, hf_bssap_global_cn_id, tvb, offset, ie_len, ENC_NA);
    global_cn_id_tree = proto_item_add_subtree(global_cn_id_item, ett_bssap_global_cn_id);
    /* Octet 3 - 5 PLMN-Id Coded as octets 2 to 4 of the Location Area Identification IE,
     * defined in 3GPP TS 24.008 (not including 3GPP TS 24.008 IEI and LAC).
     */
    plmn_item = proto_tree_add_item(global_cn_id_tree, hf_bssap_plmn_id, tvb, offset, 3, ENC_NA);
    plmn_tree = proto_item_add_subtree(plmn_item, ett_bssap_plmn);
    dissect_e212_mcc_mnc(tvb, pinfo, plmn_tree, offset, E212_NONE, true);
    offset = offset + 3;

    /* Octet 6 - 7 CN-Id (INTEGER 0..4095) */
    proto_tree_add_item(global_cn_id_tree, hf_bssap_cn_id, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset = offset+2;

    return offset;

}

static int dissect_bssap_plus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
{
    proto_item *bssap_item;
    proto_tree *bssap_tree;
    uint8_t     message_type;
    int         offset = 0;
    struct _sccp_msg_info_t* sccp_info = (struct _sccp_msg_info_t*)data;

    /*
     * Make entry in the Protocol column on summary display
     */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "BSSAP+");

    if (sccp_info && sccp_info->data.co.assoc)
        sccp_info->data.co.assoc->payload = SCCP_PLOAD_BSSAP;

    /* create the BSSAP+ protocol tree */
    bssap_item = proto_tree_add_item(tree, proto_bssap_plus, tvb, 0, -1, ENC_NA);
    bssap_tree = proto_item_add_subtree(bssap_item, ett_bssap);

    message_type = tvb_get_uint8(tvb, offset);
    proto_tree_add_item(bssap_tree, hf_bssap_plus_message_type, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;

    col_add_str(pinfo->cinfo, COL_INFO, val_to_str_ext(message_type, &bssap_plus_message_type_values_ext, "Unknown %u"));

    switch (message_type) {
    case BSSAP_PAGING_REQUEST:
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        /* VLR number VLR number 18.4.26 M TLV 5-11 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_VLR_NUMBER))
            offset = dissect_bssap_vlr_number(tvb, bssap_tree, offset);

        /* End of mandatory elements */
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* TMSI TMSI 18.4.23 O TLV 6 */
        if (check_optional_ie(tvb, offset, BSSAP_TMSI))
            offset = dissect_bssap_tmsi(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Location area identifier Location area identifier 18.4.14 O TLV 7 */
        if (check_optional_ie(tvb, offset, BSSAP_LOC_AREA_ID))
            offset = dissect_bssap_loc_area_id(tvb, bssap_tree, pinfo, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Channel needed Channel needed 18.4.2 O TLV 3 */
        if (check_optional_ie(tvb, offset, BSSAP_CHANNEL_NEEDED))
            offset = dissect_bssap_channel_needed(tvb, bssap_tree, pinfo, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* eMLPP Priority eMLPP Priority 18.4.4 O TLV 3 */
        if (check_optional_ie(tvb, offset, BSSAP_EMLPP_PRIORITY))
            offset = dissect_bssap_emlpp_priority(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Global CN-Id Global CN-Id 18.4.27 O TLV 7 */
        if (check_optional_ie(tvb, offset, BSSAP_GLOBAL_CN_ID))
            offset = dissect_bssap_global_cn_id(tvb, pinfo, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_PAGING_REJECT:                   /*  17.1.18 */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);
        /* Gs Cause Gs Cause 18.4.7 M TLV 3 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_GS_CAUSE))
            offset = dissect_bssap_Gs_cause(tvb, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_DOWNLINK_TUNNEL_REQUEST:         /*  17.1.4  */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        /* VLR number VLR number 18.4.26 M TLV 5-11 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_VLR_NUMBER))
            offset = dissect_bssap_vlr_number(tvb, bssap_tree, offset);

        /* Downlink Tunnel Payload Control and Info 18.4.3 M TLV 3-223 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_DLINK_TNL_PLD_CTR_AND_INF))
            offset = dissect_bssap_dlink_tunnel_payload_control_and_info(tvb, pinfo, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_UPLINK_TUNNEL_REQUEST:           /*  17.1.23 */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        /* SGSN number 18.4.22 M TLV 5-11 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_SGSN_NUMBER))
            offset = dissect_bssap_sgsn_number(tvb, bssap_tree, offset);

        /* Uplink Tunnel Payload Control and Info 18.4.25 M TLV 3-223 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_ULINK_TNL_PLD_CTR_AND_INF))
            offset = dissect_bssap_ulink_tunnel_payload_control_and_info(tvb, pinfo, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_LOCATION_UPDATE_REQUEST:         /*  17.1.11 BSSAP+-LOCATION-UPDATE-REQUEST */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        /* SGSN number SGSN number 18.4.22 M TLV 5-11 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_SGSN_NUMBER))
            offset = dissect_bssap_sgsn_number(tvb, bssap_tree, offset);

        /* Update type GPRS location update type 18.4.6 M TLV 3 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_GPRS_LOC_UPD_TYPE))
            offset = dissect_bssap_gprs_location_update_type(tvb, bssap_tree, offset);

        /* New Cell global identity Cell global identity 18.4.1 M TLV 10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_CELL_GBL_ID))
            offset = dissect_bssap_cell_global_id(tvb, bssap_tree, pinfo, offset);

        /* Mobile station classmark Mobile station classmark 1 18.4.18 M TLV 3 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_MOBILE_STN_CLS_MRK1))
            offset = dissect_bssap_mobile_stn_cls_mrk1(tvb, bssap_tree, pinfo, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Old location area identifier Location area identifier 18.4.14 O TLV 7 */
        if (check_optional_ie(tvb, offset, BSSAP_LOC_AREA_ID))
            offset = dissect_bssap_loc_area_id(tvb, bssap_tree, pinfo, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* TMSI status TMSI status 18.4.24 O TLV 3 */
        if (check_optional_ie(tvb, offset, BSSAP_TMSI_STATUS))
            offset = dissect_bssap_tmsi_status(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* New service area identification Service area identification 18.4.21b O TLV 9 */
        if (check_optional_ie(tvb, offset, BSSAP_SERVICE_AREA_ID))
            offset = dissect_bssap_service_area_id(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* IMEISV IMEISV 18.4.9 O TLV 10 */
        if (check_optional_ie(tvb, offset, BSSAP_IMEISV))
            offset = dissect_bssap_imeisv(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_LOCATION_UPDATE_ACCEPT:          /*  17.1.9  */
        /* IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        /* Location area identifier Location area identifier 18.4.14 M TLV 7 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_LOC_AREA_ID))
            offset = dissect_bssap_loc_area_id(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* New TMSI, or IMSI Mobile identity 18.4.17 O TLV 6-10 */
        if (check_optional_ie(tvb, offset, BSSAP_MOBILE_ID))
            offset = dissect_bssap_mobile_id(tvb, bssap_tree, pinfo, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_LOCATION_UPDATE_REJECT:          /*  17.1.10 */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);
        /* Reject cause Reject cause 18.4.21 M TLV 3 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_REJECT_CAUSE))
            offset = dissect_bssap_reject_cause(tvb, bssap_tree, pinfo, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_TMSI_REALLOCATION_COMPLETE:      /*  17.1.22 */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Cell global identity Cell global identity 18.4.1 O TLV 10 */
        if (check_optional_ie(tvb, offset, BSSAP_CELL_GBL_ID))
            offset = dissect_bssap_cell_global_id(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Service area identification Service area identification 18.4.21b O TLV 9 */
        if (check_optional_ie(tvb, offset, BSSAP_SERVICE_AREA_ID))
            offset = dissect_bssap_service_area_id(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_ALERT_REQUEST:                   /*  17.1.3  */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_ALERT_ACK:                       /*  17.1.1  */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_ALERT_REJECT:                    /*  17.1.2  */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        /* Gs Cause Gs Cause 18.4.7 M TLV 3 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_GS_CAUSE))
            offset = dissect_bssap_Gs_cause(tvb, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_MS_ACTIVITY_INDICATION:          /*  17.1.14 */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Cell global identity Cell global identity 18.4.1 O TLV 10 */
        if (check_optional_ie(tvb, offset, BSSAP_CELL_GBL_ID))
            offset = dissect_bssap_cell_global_id(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Service area identification Service area identification 18.4.21b O TLV 9 */
        if (check_optional_ie(tvb, offset, BSSAP_SERVICE_AREA_ID))
            offset = dissect_bssap_service_area_id(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_GPRS_DETACH_INDICATION:          /*  17.1.6  */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        /* SGSN number SGSN number 18.4.22 M TLV 5-11 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_SGSN_NUMBER))
            offset = dissect_bssap_sgsn_number(tvb, bssap_tree, offset);

        /* IMSI detach from GPRS service type IMSI detach from GPRS service type 18.4.17 M TLV 3 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI_DET_FROM_GPRS_SERV_TYPE))
            offset = dissect_bssap_imsi_det_from_gprs_serv_type(tvb, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Cell global identity Cell global identity 18.4.1 O TLV 10 */
        if (check_optional_ie(tvb, offset, BSSAP_CELL_GBL_ID))
            offset = dissect_bssap_cell_global_id(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Service area identification Service area identification 18.4.21b O TLV 9 */
        if (check_optional_ie(tvb, offset, BSSAP_SERVICE_AREA_ID))
            offset = dissect_bssap_service_area_id(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_GPRS_DETACH_ACK:                 /*  17.1.5  */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_IMSI_DETACH_INDICATION:          /*  17.1.8  */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        /* SGSN number SGSN number 18.4.22 M TLV 5-11 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_SGSN_NUMBER))
            offset = dissect_bssap_sgsn_number(tvb, bssap_tree, offset);

        /* Detach type IMSI detach from non-GPRS service type 18.4.11 M TLV 3 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI_DET_FROM_NON_GPRS_SERV_TYPE))
            offset = dissect_bssap_imsi_det_from_non_gprs_serv_type(tvb, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Cell global identity Cell global identity 18.4.1 O TLV 10 */
        if (check_optional_ie(tvb, offset, BSSAP_CELL_GBL_ID))
            offset = dissect_bssap_cell_global_id(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Location information age Location information age 18.4.14 O TLV 4 */
        if (check_optional_ie(tvb, offset, BSSAP_LOC_INF_AGE))
            offset = dissect_bssap_location_information_age(tvb, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Service area identification Service area identification 18.4.21b O TLV 9 */
        if (check_optional_ie(tvb, offset, BSSAP_SERVICE_AREA_ID))
            offset = dissect_bssap_service_area_id(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_IMSI_DETACH_ACK:                 /*  17.1.7  */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_RESET_INDICATION:                /*  17.1.21 */
        /* Conditional IE:s */
        /* SGSN number SGSN number 18.4.22 C TLV 5-11 */
        if (check_optional_ie(tvb, offset, BSSAP_SGSN_NUMBER)) {
            offset = dissect_bssap_sgsn_number(tvb, bssap_tree, offset);
            if (tvb_reported_length_remaining(tvb, offset) <= 0)
                return tvb_reported_length(tvb);
            proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        }else{
            /* VLR number VLR number 18.4.26 C TLV 5-11 */
            if (check_optional_ie(tvb, offset, BSSAP_VLR_NUMBER)) {
                offset = dissect_bssap_vlr_number(tvb, bssap_tree, offset);
                if (tvb_reported_length_remaining(tvb, offset) <= 0)
                    return tvb_reported_length(tvb);
                proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
            }
        }
        proto_tree_add_item(tree, hf_bssap_conditional_ie, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_RESET_ACK:                       /*  17.1.20 */
        /* Conditional IE:s */
        /* SGSN number SGSN number 18.4.22 C TLV 5-11 */
        if (check_optional_ie(tvb, offset, BSSAP_SGSN_NUMBER)) {
            offset = dissect_bssap_sgsn_number(tvb, bssap_tree, offset);
            if (tvb_reported_length_remaining(tvb, offset) <= 0)
                return tvb_reported_length(tvb);
            proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        }else{
            /* VLR number VLR number 18.4.26 C TLV 5-11 */
            if (check_optional_ie(tvb, offset, BSSAP_VLR_NUMBER)) {
                offset = dissect_bssap_vlr_number(tvb, bssap_tree, offset);
                if (tvb_reported_length_remaining(tvb, offset) <= 0)
                    return tvb_reported_length(tvb);
                proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
            }
        }
        proto_tree_add_item(tree, hf_bssap_conditional_ie, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_MS_INFORMATION_REQUEST:          /*  17.1.15 */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        /* Information requested Information requested 18.4.13 M TLV 3 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_INFO_REQ))
            offset = dissect_bssap_info_req(tvb, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_MS_INFORMATION_RESPONSE:         /*  17.1.16 */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* TMSI TMSI 18.4.23 O TLV 6 */
        if (check_optional_ie(tvb, offset, BSSAP_TMSI))
            offset = dissect_bssap_tmsi(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* PTMSI PTMSI 18.4.20 O TLV 6 BSSAP_PTMSI*/
        if (check_optional_ie(tvb, offset, BSSAP_PTMSI))
            offset = dissect_bssap_ptmsi(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* IMEI IMEI 18.4.8 O TLV 10 */
        if (check_optional_ie(tvb, offset, BSSAP_IMEI))
            offset = dissect_bssap_imei(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        /* IMEISV IMEISV 18.4.9 O TLV 10 BSSAP_IMEISV*/
        if (check_optional_ie(tvb, offset, BSSAP_IMEISV))
            offset = dissect_bssap_imeisv(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Cell global identity Cell global identity 18.4.1 O TLV 10 */
        if (check_optional_ie(tvb, offset, BSSAP_CELL_GBL_ID))
            offset = dissect_bssap_cell_global_id(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        /* Location information age Location information age 18.4.15 O TLV 4 */
        if (check_optional_ie(tvb, offset, BSSAP_LOC_INF_AGE))
            offset = dissect_bssap_location_information_age(tvb, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Mobile station state Mobile station state 18.4.19 O TLV 3 */
        if (check_optional_ie(tvb, offset, BSSAP_MOBILE_STN_STATE))
            offset = dissect_bssap_mobile_station_state(tvb, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);

        /* Service area identification Service area identification 18.4.21b O TLV 9 */
        if (check_optional_ie(tvb, offset, BSSAP_SERVICE_AREA_ID))
            offset = dissect_bssap_service_area_id(tvb, bssap_tree, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_MM_INFORMATION_REQUEST:          /*  17.1.12 */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        /* MM information MM information 18.4.16 O TLV 3-n */
        if (check_optional_ie(tvb, offset, BSSAP_MM_INFORMATION))
            offset = dissect_bssap_MM_information(tvb, bssap_tree, pinfo, offset);
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_MOBILE_STATUS:                   /*  17.1.13 */
        /* IMSI IMSI 18.4.10 O TLV 6-10 */
        if (check_optional_ie(tvb, offset, BSSAP_IMSI))
                offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);
        /* Gs Cause Gs Cause 18.4.7 M TLV 3 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_GS_CAUSE))
            offset = dissect_bssap_Gs_cause(tvb, bssap_tree, offset);

        /* Erroneous message Erroneous message 18.4.5 M TLV 3-n BSSAP_ERRONEOUS_MSG*/
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_ERRONEOUS_MSG))
            offset = dissect_bssap_gprs_erroneous_msg(tvb, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    case BSSAP_MS_UNREACHABLE:                  /*  17.1.17 */
        /* IMSI IMSI 18.4.10 M TLV 6-10 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_IMSI))
            offset = dissect_bssap_imsi(tvb, bssap_tree, pinfo, offset);

        /* Gs Cause Gs Cause 18.4.7 M TLV 3 */
        if (check_ie(tvb, pinfo, tree, &offset, BSSAP_GS_CAUSE))
            offset = dissect_bssap_Gs_cause(tvb, bssap_tree, offset);

        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return tvb_reported_length(tvb);
        proto_tree_add_item(tree, hf_bssap_extraneous_data, tvb, offset, -1, ENC_NA);
        break;
    default:
        break;
    }

    return tvb_reported_length(tvb);
}

static bool
dissect_bssap_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    /* Is it a BSSAP/BSAP packet?
     *    If octet_1 == 0x00 and octet_2 == length(tvb) - 2
     * or if octet_1 == 0x01 and octet_3 == length(tvb) - 3
     * then we'll assume it is a bssap packet
     *    If octet_1 == 0x00 a further check is done
     *    to differentiate a BSSMAP BLOCK message from a
     *    RANAP DirectTransfer (under certain conditions)
     */
    switch (tvb_get_uint8(tvb, 0))
    {
    case 0x00:
        if (tvb_get_uint8(tvb, 1) != (tvb_reported_length(tvb) - 2)) {
            return false;
        }
        if (tvb_get_uint8(tvb, 2) == 0x40 && tvb_get_uint8(tvb, 3) != 0x01) {
            return false;
        }
        break;

    case 0x01:
        if (tvb_get_uint8(tvb, 2) != (tvb_reported_length(tvb) - 3)) {
            return false;
        }
        break;

    default:
        return false;
    }

    switch (default_protocol_global) {

    case BSSAP:
        dissect_bssap(tvb, pinfo, tree, data);
        break;

    case BSAP:
        dissect_bsap(tvb, pinfo, tree, data);
        break;

    case BSSAP_LE:
        dissect_bssap_le(tvb, pinfo, tree, data);
        break;

    }

    return true;
}

/* Register the protocol with Wireshark */
void
proto_register_bssap(void)
{
    module_t    *bssap_module;
    module_t    *bssap_plus_module;
    expert_module_t* expert_bssap;

    /* Setup list of header fields */
    static hf_register_info hf[] = {
        { &hf_bssap_pdu_type,
          { "Message Type", "bssap.pdu_type",
        FT_UINT8, BASE_HEX, VALS(bssap_pdu_type_values), 0x0,
        NULL, HFILL}},

        { &hf_bsap_pdu_type,
          { "Message Type", "bsap.pdu_type",
        FT_UINT8, BASE_HEX, VALS(bsap_pdu_type_values), 0x0,
        NULL, HFILL}},

        { &hf_bssap_dlci_cc,
          { "Control Channel", "bssap.dlci.cc",
        FT_UINT8, BASE_HEX, VALS(bssap_cc_values), CC_MASK,
        NULL, HFILL}},

        { &hf_bsap_dlci_cc,
          { "Control Channel", "bsap.dlci.cc",
        FT_UINT8, BASE_HEX, VALS(bsap_cc_values), CC_MASK,
        NULL, HFILL}},

        { &hf_bssap_dlci_spare,
          { "Spare", "bssap.dlci.spare",
        FT_UINT8, BASE_HEX, NULL, SPARE_MASK,
        NULL, HFILL}},

        { &hf_bsap_dlci_rsvd,
          { "Reserved", "bsap.dlci.rsvd",
        FT_UINT8, BASE_HEX, NULL, SPARE_MASK,
        NULL, HFILL}},

        { &hf_bssap_dlci_sapi,
          { "SAPI", "bssap.dlci.sapi",
        FT_UINT8, BASE_HEX, VALS(bssap_sapi_values), SAPI_MASK,
        NULL, HFILL}},

        { &hf_bsap_dlci_sapi,
          { "SAPI", "bsap.dlci.sapi",
        FT_UINT8, BASE_HEX, VALS(bsap_sapi_values), SAPI_MASK,
        NULL, HFILL}},

        { &hf_bssap_length,
          { "Length", "bssap.length",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL}},


        { &hf_bssap_plus_message_type,
          { "Message Type", "bssap_plus.msg_type",
        FT_UINT8, BASE_DEC | BASE_EXT_STRING, &bssap_plus_message_type_values_ext, 0x0,
        NULL, HFILL}},

        { &hf_bssap_plus_ie,
          { "IEI", "bssap_plus.iei",
        FT_UINT8, BASE_DEC | BASE_EXT_STRING, &bssap_plus_ie_id_values_ext, 0x0,
        NULL, HFILL}},

        { &hf_bssap_plus_ie_len,
          { "Length indicator", "bssap_plus.ie_len",
        FT_UINT8, BASE_DEC, NULL, 0x0,
        NULL, HFILL}},

        { &hf_bssap_extension,
          { "Extension", "bssap.extension",
            FT_BOOLEAN, 8, TFS(&tfs_no_extension_extension), 0x80,
            NULL, HFILL }},

        { &hf_bssap_type_of_number,
          { "Type of number", "bssap.type_of_number",
            FT_UINT8, BASE_HEX, VALS(gsm_a_dtap_type_of_number_values), 0x70,
            NULL, HFILL }},

        { &hf_bssap_numbering_plan_id,
          { "Numbering plan identification", "bssap.number_plan",
            FT_UINT8, BASE_HEX, VALS(gsm_a_dtap_numbering_plan_id_values), 0x0f,
            NULL, HFILL }},

        { &hf_bssap_sgsn_number,
          { "SGSN number", "bssap.sgsn_number",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_vlr_number,
          { "VLR number", "bssap.vlr_number",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_cell_global_id_ie,
          { "Cell global identity IE", "bssap.cell_global_id_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_channel_needed_ie,
          { "Channel needed IE", "bssap.channel_needed_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_dlink_tnl_pld_cntrl_amd_inf_ie,
          { "Downlink Tunnel Payload Control and Info IE", "bssap.dlink_tnl_pld_cntrl_amd_inf_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_ulink_tnl_pld_cntrl_amd_inf_ie,
          { "Uplink Tunnel Payload Control and Info IE", "bssap.ulink_tnl_pld_cntrl_amd_inf_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_emlpp_prio_ie,
          { "eMLPP Priority IE", "bssap.emlpp_prio_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_gprs_erroneous_msg_ie,
          { "Erroneous message IE", "bssap.erroneous_msg_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_gprs_loc_upd_type_ie,
          { "GPRS location update type IE", "bssap.loc_upd_type_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_Gs_cause_ie,
          { "Gs Cause IE", "bssap.Gs_cause_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_imei_ie,
          { "IMEI IE", "bssap.imei_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_imeisv_ie,
          { "IMEISV IE", "bssap.imeisv_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_imsi_ie,
          { "IMSI IE", "bssap.imsi_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_imsi_det_from_gprs_serv_type_ie,
          { "IMSI detach from GPRS service type IE", "bssap.msi_det_from_gprs_serv_type_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_imsi_det_from_non_gprs_serv_type_ie,
          { "IMSI detach from non-GPRS service IE", "bssap.msi_det_from_non_gprs_serv_type_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_info_req_ie,
          { "Information requested IE", "bssap.info_req_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_loc_area_id_ie,
          { "Location area identifier IE", "bssap.loc_area_id_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_loc_inf_age_ie,
          { "Location information age IE", "bssap.loc_inf_age_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_mm_information_ie,
          { "MM information IE", "bssap.mm_information",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_mobile_id_ie,
          { "Mobile identity IE", "bssap.mobile_id_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_mobile_stn_cls_mrk1_ie,
          { "Mobile station classmark 1 IE", "bssap.mobile_stn_cls_mrk1_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_mobile_station_state_ie,
          { "Mobile station state IE", "bssap.mobile_station_state_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_ptmsi_ie,
          { "PTMSI IE", "bssap.ptmsi_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_reject_cause_ie,
          { "Reject cause IE", "bssap.reject_cause_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_service_area_id_ie,
          { "Service area identification IE", "bssap.service_area_id_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            "Mobile station classmark 1", HFILL }},

        { &hf_bssap_sgsn_nr_ie,
          { "SGSN number IE", "bssap.sgsn_nr_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_tmsi_ie,
          { "TMSI IE", "bssap.tmsi_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_tmsi_status_ie,
          { "TMSI status IE", "bssap.tmsi_status_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_vlr_number_ie,
          { "VLR number IE", "bssap.vlr_number_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_global_cn_id_ie,
          { "Global CN-Id IE", "bssap.global_cn_id_ie",
            FT_NONE, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_plus_ie_data,
          { "IE Data", "bssap.ie_data",
            FT_BYTES, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_call_priority,
          { "eMLPP Priority", "bssap.call_priority",
            FT_UINT8, BASE_DEC, VALS(bssap_call_priority_values), 0x07,
            NULL, HFILL}},

        { &hf_bssap_gprs_loc_upd_type,
          { "GPRS location update type", "bssap.gprs_loc_upd_type",
            FT_UINT8, BASE_DEC, VALS(bssap_plus_GPRS_loc_upd_type_values), 0x0,
            NULL, HFILL}},

        { &hf_bssap_Gs_cause,
          { "Gs cause", "bssap.Gs_cause",
            FT_UINT8, BASE_DEC, VALS(bssap_Gs_cause_values), 0x0,
            NULL, HFILL}},

        { &hf_bssap_imei,
          { "IMEI", "bssap.imei",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_imeisv,
          { "IMEISV", "bssap.imeisv",
            FT_STRING, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        { &hf_bssap_imsi_det_from_gprs_serv_type,
          { "IMSI detach from GPRS service type", "bssap.imsi_det_from_gprs_serv_type",
            FT_UINT8, BASE_DEC, VALS(bssap_Gs_cause_values), 0x0,
            NULL, HFILL}},

        { &hf_bssap_info_req,
          { "Information requested", "bssap.info_req",
            FT_UINT8, BASE_DEC, VALS(bssap_info_req_values), 0x0,
            NULL, HFILL}},

        { &hf_bssap_loc_inf_age,
          { "AgeOfLocationInformation in minutes", "bssap.loc_inf_age",
            FT_UINT16, BASE_DEC, NULL, 0x0,
            NULL, HFILL}},

        { &hf_bssap_mobile_station_state,
          { "Mobile station state", "bssap.mobile_station_state",
            FT_UINT8, BASE_DEC, VALS(bssap_mobile_station_state_values), 0x0,
            NULL, HFILL}},

        { &hf_bssap_ptmsi,
          { "PTMSI", "bssap.ptmsi",
            FT_BYTES, BASE_NONE, NULL, 0x0,
            NULL, HFILL}},

        { &hf_bssap_tmsi,
          { "TMSI", "bssap.tmsi",
            FT_BYTES, BASE_NONE, NULL, 0x0,
            NULL, HFILL}},

        { &hf_bssap_tmsi_status,
          { "TMSI status", "bssap.tmsi_status",
            FT_BOOLEAN, 8, TFS(&bssap_tmsi_flag), 0x01,
            NULL, HFILL }},

        { &hf_bssap_tom_prot_disc,
          { "TOM Protocol Discriminator", "bssap.Tom_prot_disc",
            FT_UINT8, BASE_DEC, VALS(bssap_tom_prot_disc_values), 0x78,
            NULL, HFILL}},

        { &hf_bssap_e_bit,
          { "E: Cipher Request", "bssap.e_bit",
            FT_BOOLEAN, 8, TFS(&bssap_E_flag), 0x04,
            NULL, HFILL }},

        { &hf_bssap_tunnel_prio,
          { "Tunnel Priority", "bssap.tunnel_prio",
            FT_UINT8, BASE_DEC, NULL, 0x0,
            NULL, HFILL}},

        { &hf_bssap_global_cn_id,
          { "Global CN-Id", "bssap.global_cn_id",
            FT_BYTES, BASE_NONE, NULL, 0x0,
            NULL, HFILL}},

        { &hf_bssap_plmn_id,
          { "PLMN-Id", "bssap.plmn_id",
            FT_BYTES, BASE_NONE, NULL, 0x0,
            NULL, HFILL}},

        { &hf_bssap_cn_id,
          { "CN-Id", "bssap.cn_id",
            FT_UINT16, BASE_DEC, NULL, 0x0,
            NULL, HFILL}},

        { &hf_bssap_cell_global_id,
          { "Cell global identity", "bssap.cell_global_id",
            FT_BYTES, BASE_NONE, NULL, 0x0,
            NULL, HFILL}},

        { &hf_bssap_extraneous_data,
          { "Extraneous data", "bssap.extraneous_data",
            FT_BYTES, BASE_NONE, NULL, 0x0,
            NULL, HFILL}},

        { &hf_bssap_conditional_ie,
          { "Conditional IE", "bssap.conditional_ie",
            FT_BYTES, BASE_NONE, NULL, 0x0,
            NULL, HFILL}},
    };

    /* Setup protocol subtree array */
    static int *ett[] = {
        &ett_bssap,
        &ett_bssap_dlci,
        &ett_bssap_imsi,
        &ett_bssap_imsi_det_from_gprs_serv_type,
        &ett_bssap_imsi_det_from_non_gprs_serv_type,
        &ett_bssap_info_req,
        &ett_bssap_loc_area_id,
        &ett_bssap_loc_inf_age,
        &ett_bssap_mm_information,
        &ett_bssap_mobile_id,
        &ett_bssap_sgsn_nr,
        &ett_bssap_tmsi,
        &ett_bssap_tmsi_status,
        &ett_bssap_vlr_number,
        &ett_bssap_global_cn,
        &ett_bssap_gprs_loc_upd,
        &ett_bassp_Gs_cause,
        &ett_bassp_imei,
        &ett_bassp_imeisv,
        &ett_bssap_cell_global_id,
        &ett_bssap_cgi,
        &ett_bssap_channel_needed,
        &ett_bssap_dlink_tnl_pld_cntrl_amd_inf,
        &ett_bssap_ulink_tnl_pld_cntrl_amd_inf,
        &ett_bssap_emlpp_prio,
        &ett_bssap_erroneous_msg,
        &ett_bssap_mobile_stn_cls_mrk1,
        &ett_bssap_mobile_station_state,
        &ett_bssap_ptmsi,
        &ett_bssap_reject_cause,
        &ett_bssap_service_area_id,
        &ett_bssap_global_cn_id,
        &ett_bssap_plmn,
    };

    static const enum_val_t default_protocol_options[] = {
        { "bssap",    "BSSAP (GSM A-I/F)",    BSSAP },
        { "bsap",     "BSAP (CDMA2000 A-I/F)",     BSAP  },
        { "bssap_le", "BSSAP-LE (GSM Lb-I/F)", BSSAP_LE  },
        { NULL,     NULL,       0 }
    };

    static ei_register_info ei[] = {
        { &ei_bssap_unknown_message, { "bssap.unknown_message", PI_PROTOCOL, PI_WARN, "Unknown message", EXPFILL }},
        { &ei_bssap_unknown_parameter, { "bssap.unknown_parameter", PI_PROTOCOL, PI_WARN, "Unknown parameter", EXPFILL }},
        { &ei_bssap_mandatory_ie, { "bssap.mandatory_ie", PI_PROTOCOL, PI_WARN, "Mandatory IE expected", EXPFILL }},
    };

    /* Register the protocol name and description */
    proto_bssap = proto_register_protocol("BSSAP", "BSSAP", "bssap");
    proto_bssap_plus = proto_register_protocol("BSSAP2", "BSSAP2", "bssap_plus");
    proto_bsap = proto_register_protocol_in_name_only("BSAP", "BSAP", "bsap", proto_bssap, FT_PROTOCOL);
    proto_bssap_le = proto_register_protocol_in_name_only("BSSAP-LE", "BSSAP-LE", "bssap_le", proto_bssap, FT_PROTOCOL);

    bssap_handle = register_dissector("bssap", dissect_bssap, proto_bssap);
    bsap_handle = register_dissector("bsap", dissect_bsap, proto_bsap);
    bssap_le_handle = register_dissector("bssap_le", dissect_bssap_le, proto_bssap_le);
    register_dissector("bssap.imei", dissect_bssap_imei_dissector, proto_bssap);
    bssap_plus_handle = register_dissector("bssap_plus", dissect_bssap_plus, proto_bssap_plus);

    /* Required function calls to register the header fields and subtrees used */
    proto_register_field_array(proto_bssap, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
    expert_bssap = expert_register_protocol(proto_bssap);
    expert_register_field_array(expert_bssap, ei, array_length(ei));

    bssap_module = prefs_register_protocol(proto_bssap, NULL);

    prefs_register_obsolete_preference(bssap_module, "bsap_or_bssap");
    prefs_register_obsolete_preference(bssap_module, "gsm_or_lb_interface");

    prefs_register_enum_preference(bssap_module,
                       "default_protocol",
                       "Default protocol",
                       "The default protocol assumed by the heuristic dissector,"
                       "which does not easily distinguish between BSSAP "
                       "(on the GSM A interface between the BSC and the MSC), "
                       "BSSAP-LE (on the GSM Lb interface between the BSC and the SMLC), "
                       "and BSAP (on the CDMA2000 A interface between the BS and MSC).",
                       &default_protocol_global,
                       default_protocol_options,
                       false);

    /* No explicit preferences anymore, but it does have an automatic Decode As
     * preference, so we don't register the module itself obsolete */
    bssap_plus_module = prefs_register_protocol(proto_bssap_plus, NULL);
    prefs_register_obsolete_preference(bssap_plus_module, "ssn");

    bssap_dissector_table = register_dissector_table("bssap.pdu_type", "BSSAP Message Type", proto_bssap, FT_UINT8, BASE_DEC);
    bsap_dissector_table  = register_dissector_table("bsap.pdu_type", "BSAP Message Type", proto_bssap, FT_UINT8, BASE_DEC);
    bssap_le_dissector_table = register_dissector_table("bssap_le.pdu_type", "BSSAP Message Type", proto_bssap, FT_UINT8, BASE_DEC);
}

void
proto_reg_handoff_bssap(void)
{

    heur_dissector_add("sccp", dissect_bssap_heur, "BSSAP over SCCP", "bssap_sccp", proto_bssap, HEURISTIC_ENABLE);
    heur_dissector_add("sua", dissect_bssap_heur, "BSSAP over SUA", "bssap_sua", proto_bssap, HEURISTIC_ENABLE);
    /* BSSAP+ */

    rrlp_handle = find_dissector_add_dependency("rrlp", proto_bssap_plus);
    gsm_bssmap_le_dissector_handle = find_dissector_add_dependency("gsm_bssmap_le", proto_bssap);
    gsm_a_bssmap_dissector_handle = find_dissector_add_dependency("gsm_a_bssmap", proto_bssap);

    dissector_add_for_decode_as("sccp.ssn", bssap_handle);
    dissector_add_for_decode_as("sccp.ssn", bsap_handle);
    dissector_add_for_decode_as("sccp.ssn", bssap_le_handle);
    dissector_add_uint_with_preference("sccp.ssn", BSSAP_PLUS_SSN, bssap_plus_handle);
}

/*
 * 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:
 */