summaryrefslogtreecommitdiffstats
path: root/src/host/gsm48-andreas/gsm48_rr.c
blob: bb7b58f657a6c854a556adee62344f26e41605b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
/*
 * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

/* Very short description of some of the procedures:
 *
 * A radio ressource request causes sendig a channel request on RACH.
 * After receiving of an immediate assignment the link will be establised.
 * After the link is established, the dedicated mode is entered and confirmed.
 *
 * A Paging request also triggers the channel request as above...
 * After the link is established, the dedicated mode is entered and indicated.
 *
 * During dedicated mode, messages are transferred.
 *
 * When an assignment command or a handover command is received, the current
 * link is released. After release, the new channel is activated and the
 * link is established again. After link is establised, pending messages from
 * radio ressource are sent.
 *
 * When the assignment or handover fails, the old channel is activate and the
 * link is established again. Also pending messages are sent.
 *
 */

/*
 * state transition
 */

static const char *gsm48_rr_state_names[] = {
	"IDLE",
	"CONN PEND",
	"DEDICATED",
};

static void new_rr_state(struct gsm_rrlayer *rr, int state)
{
	if (state < 0 || state >= (sizeof(gsm48_rr_state_names) / sizeof(char *)))
		return;

	if (state == GSM_RRSTATE_IDLE) {
		struct msgb *msg;

		/* free establish message, if any */
		rr->rr_est_req = 0;
		if (rr->rr_est_msg) {
			msgb_free(rr->rr_est_msg);
			rr->rr_est_msg = NULL;
		}
		/* free all pending messages */
		while((msg = msgb_dequeue(&rr->downqueue)))
			free_msgb(msg);
	}

	DEBUGP(DRR, "new state %s -> %s\n",
		rr_state_names[rr->state], rr_state_names[state]);

	rr->state = state;
}

/*
 * messages
 */

#define RR_ALLOC_SIZE          200
#define RR_ALLOC_HEADROOM      56

/* names of RR-SAP */
static const struct value_string gsm48_rr_msg_names[] = {
	{ GSM48_RR_EST_REQ,		"RR_EST_REQ" },
	{ GSM48_RR_EST_IND,		"RR_EST_IND" },
	{ GSM48_RR_EST_CNF,		"RR_EST_CNF" },
	{ GSM48_RR_REL_IND,		"RR_REL_IND" },
	{ GSM48_RR_SYNC_IND,		"RR_SYNC_IND" },
	{ GSM48_RR_DATA_REQ,		"RR_DATA_REQ" },
	{ GSM48_RR_DATA_IND,		"RR_DATA_IND" },
	{ GSM48_RR_UNIT_DATA_IND,	"RR_UNIT_DATA_IND" },
	{ GSM48_RR_ABORT_REQ,		"RR_ABORT_REQ" },
	{ GSM48_RR_ABORT_IND,		"RR_ABORT_IND" },
	{ GSM48_RR_ACT_REQ,		"RR_ACT_REQ" },
	{ 0,				NULL }
};

const char *get_rr_name(int value)
{
	return get_value_string(gsm48_rr_msg_names, value);
}

/* allocate GSM 04.08 layer 3 message */
struct msgb *gsm48_l3_msgb_alloc(void)
{
	struct msgb *msg;

	msg = msgb_alloc_headroom(RR_ALLOC_SIZE+RR_ALLOC_HEADROOM,
		RR_ALLOC_HEADROOM, "GSM 04.08 L3");
	if (!msg)
		return NULL;

	return msg;
}

/* allocate GSM 04.08 message (RR-SAP) */
static struct msgb *gsm48_rr_msgb_alloc(void)
{
	struct msgb *msg;

	msg = msgb_alloc_headroom(RR_ALLOC_SIZE+RR_ALLOC_HEADROOM,
		RR_ALLOC_HEADROOM, "GSM 04.08 RR");
	if (!msg)
		return NULL;

	return msg;
}

/* queue message (RR-SAP) */
int gsm48_rr_upmsg(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;

	msgb_enqueue(&rr->rr_upqueue, msg);
}

/* dequeue messages (RSL-SAP) */
int gsm48_rsl_dequeue(struct osmocom_ms *ms)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct msgb *msg;
	int work = 0;
	
	while ((msg = msgb_dequeue(&rsl->rsl_upqueue))) {
		/* msg is freed there */
		gsm48_rcv_rsl(ms, msg);
		work = 1; /* work done */
	}
	
	return work;
}

/*
 * timers handling
 */

static void start_rr_t3122(struct gsm_rrlayer *rr, int sec, int micro)
{
	DEBUGP(DRR, "starting T3122 with %d seconds\n", current, sec);
	rr->t3122.cb = timeout_rr_t3122;
	rr->t3122.data = rr;
	bsc_schedule_timer(&rr->t3122, sec, micro);
}

static void start_rr_t3126(struct gsm_rrlayer *rr, int sec, int micro)
{
	DEBUGP(DRR, "starting T3126 with %d seconds\n", current, sec);
	rr->t3126.cb = timeout_rr_t3126;
	rr->t3126.data = rr;
	bsc_schedule_timer(&rr->t3126, sec, micro);
}

static void stop_rr_t3122(struct gsm_rrlayer *rr)
{
	if (timer_pending(rr->t3122)) {
		DEBUGP(DRR, "stopping pending timer T3122\n");
		bsc_del_timer(&rr->t3122);
	}
	rr->t3122_running = 0;
}

static void stop_rr_t3126(struct gsm_rrlayer *rr)
{
	if (bsc_timer_pending(rr->t3126)) {
		DEBUGP(DRR, "stopping pending timer T3126\n");
		bsc_del_timer(&rr->t3126);
	}
}

static void timeout_rr_t3122(void *arg)
{
}

static void timeout_rr_t3126(void *arg)
{
	struct gsm_rrlayer *rr = arg;

	if (rr->rr_est_req) {
		struct msgb *msg = gsm48_mm_msgb_alloc();
		struct gsm_mm_hdr *mmh;

		if (!msg)
			return -ENOMEM;
		mmh = (struct gsm_mm_hdr *)msg->data;
		mmh->msg_type RR_REL_IND;
		mmh->cause = GSM_MM_CAUSE_RA_FAILURE;
		gsm48_mm_upmsg(ms, msg);
	}

	new_rr_state(rr, GSM_RRSTATE_IDLE);
}

/*
 * status
 */

/* send rr status request */
static int gsm_rr_tx_rr_status(struct osmocom_ms *ms, uint8_t cause)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct msgb *nmsg;
	struct gsm48_hdr *gh;
	struct gsm48_rr_status *st;

	nmsg = gsm48_l3_msgb_alloc();
	if (!nmsg)
		return -ENOMEM;
	gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
	st = (struct gsm48_rr_status *) msgb_put(nmsg, sizeof(*st));

	gh->proto = GSM48_PDISC_RR;
	gh->msg_type = GSM48_MT_RR_CIPH_M_COMPL;

	/* rr cause */
	st->rr_cause = cause;

	return rslms_tx_rll_req_l3(ms, RSL_MT_DATA_REQ, chan_nr, 0, nmsg);
}

/*
 * ciphering
 */

/* send chiperhing mode complete */
static int gsm_rr_tx_cip_mode_cpl(struct osmocom_ms *ms, uint8_t cr)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm_subscriber *subcr = ms->subscr;
	struct msgb *nmsg;
	struct gsm48_hdr *gh;
	u_int8_t buf[11], *ie;

	nmsg = gsm48_l3_msgb_alloc();
	if (!nmsg)
		return -ENOMEM;
	gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));

	gh->proto = GSM48_PDISC_RR;
	gh->msg_type = GSM48_MT_RR_CIPH_M_COMPL;

	/* MI */
	if (cr) {
		gsm48_generate_mid_from_imsi(ie, subscr->imei);
		ie = msgb_put(nmsg, 1 + buf[1]);
		memcpy(ie, buf + 1, 1 + buf[1]);
	}

	return rslms_tx_rll_req_l3(ms, RSL_MT_DATA_REQ, chan_nr, 0, nmsg);
}

/* receive ciphering mode command */
static int gsm_rr_rx_cip_mode_cmd(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_hdr *gh = msgb_l3(msg);
	struct gsm48_cip_mode_cmd *cm = (struct gsm48_cip_mode_cmd *)gh->data;
	int payload_len = msgb_l3len(msg) - sizeof(*gh) - sizeof(*cm);
	uint8_t sc, alg_id, cr;

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of CIPHERING MODE COMMAND message.\n");
		return gsm_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
	}

	/* cipher mode setting */
	sc = cm->sc;
	alg_id = cm->alg_id;
	/* cipher mode response */
	cr = cm->cr;

	/* 3.4.7.2 */
	if (rr->sc && sc)
		return gsm_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);

	/* change to ciphering */
	tx_ph_cipher_req(ms, sc, alg_id);
	rr->sc = sc, rr->alg_id = alg_id;

	/* response */
	return gsm_rr_tx_cip_mode_cpl(ms, cr);
}

/*
 * classmark
 */

/* Encode  "Classmark 3" (10.5.2.20) */
static int gsm_rr_enc_cm3(struct osmocom_sm *ms, uint8_t *buf, uint8_t *len)
{
	struct gsm_support *sup = &ms->support;
	struct bitvec bv;

	memset(&bv, 0, sizeof(bv));
	bv.data = data;
	bv.data_len = 12;

	/* spare bit */
	bitvec_set_bit(&bv, 0);
	/* band 3 supported */
	if (sup->dcs_1800)
		bitvec_set_bit(&bv, ONE);
	else
		bitvec_set_bit(&bv, ZERO);
	/* band 2 supported */
	if (sup->e_gsm || sup->r_gsm)
		bitvec_set_bit(&bv, ONE);
	else
		bitvec_set_bit(&bv, ZERO);
	/* band 1 supported */
	if (sup->p_gsm && !(sup->e_gsm || sup->r_gsm))
		bitvec_set_bit(&bv, ONE);
	else
		bitvec_set_bit(&bv, ZERO);
	/* a5 bits */
	if (sup->a5_7)
		bitvec_set_bit(&bv, ONE);
	else
		bitvec_set_bit(&bv, ZERO);
	if (sup->a5_6)
		bitvec_set_bit(&bv, ONE);
	else
		bitvec_set_bit(&bv, ZERO);
	if (sup->a5_5)
		bitvec_set_bit(&bv, ONE);
	else
		bitvec_set_bit(&bv, ZERO);
	if (sup->a5_4)
		bitvec_set_bit(&bv, ONE);
	else
		bitvec_set_bit(&bv, ZERO);
	/* radio capability */
	if (sup->dcs_1800 && !sup->p_gsm && !(sup->e_gsm || sup->r_gsm)) {
		/* dcs only */
		bitvec_set_uint(&bv, 0, 4);
		bitvec_set_uint(&bv, sup->dcs_capa, 4);
	} else
	if (sup->dcs_1800 && (sup->p_gsm || (sup->e_gsm || sup->r_gsm))) {
		/* dcs */
		bitvec_set_uint(&bv, sup->dcs_capa, 4);
		/* low band */
		bitvec_set_uint(&bv, sup->low_capa, 4);
	} else {
		/* low band only */
		bitvec_set_uint(&bv, 0, 4);
		bitvec_set_uint(&bv, sup->low_capa, 4);
	}
	/* r support */
	if (sup->r_gsm) {
		bitvec_set_bit(&bv, ONE);
		bitvec_set_uint(&bv, sup->r_capa, 3);
	} else {
		bitvec_set_bit(&bv, ZERO);
	}
	/* multi slot support */
	if (sup->ms_sup) {
		bitvec_set_bit(&bv, ONE);
		bitvec_set_uint(&bv, sup->ms_capa, 5);
	} else {
		bitvec_set_bit(&bv, ZERO);
	}
	/* ucs2 treatment */
	if (sup->ucs2_treat) {
		bitvec_set_bit(&bv, ONE);
	} else {
		bitvec_set_bit(&bv, ZERO);
	}
	/* support extended measurements */
	if (sup->ext_meas) {
		bitvec_set_bit(&bv, ONE);
	} else {
		bitvec_set_bit(&bv, ZERO);
	}
	/* support measurement capability */
	if (sup->meas_cap) {
		bitvec_set_bit(&bv, ONE);
		bitvec_set_uint(&bv, sup->sms_val, 4);
		bitvec_set_uint(&bv, sup->sm_val, 4);
	} else {
		bitvec_set_bit(&bv, ZERO);
	}
	/* positioning method capability */
	if (sup->loc_serv) {
		bitvec_set_bit(&bv, ONE);
		bitvec_set_bit(&bv, sup->e_otd_ass == 1);
		bitvec_set_bit(&bv, sup->e_otd_based == 1);
		bitvec_set_bit(&bv, sup->gps_ass == 1);
		bitvec_set_bit(&bv, sup->gps_based == 1);
		bitvec_set_bit(&bv, sup->gps_conv == 1);
	} else {
		bitvec_set_bit(&bv, ZERO);
	}

	/* partitial bytes will be completed */
	*len = (bv.cur_bit + 7) >> 3;
	bitvec_spare_padding(&bv, (*len * 8) - 1);

	return 0;
}

/* encode classmark 2 */
static int gsm_rr_enc_cm2(struct osmocom_sm *ms, struct gsm48_classmark2 *cm)
{
	struct gsm_support *sup = &ms->support;

	cm->pwr_lev = sup->pwr_lev;
	cm->a5_1 = sup->a5_1;
	cm->es_ind = sup->es_ind;
	cm->rev_lev = sup->rev_lev;
	cm->fc = (sup->r_gsm || sup->e_gsm);
	cm->vgcs = sup->vgcs;
	cm->vbs = sup->vbs;
	cm->sm = sup->sms_ptp;
	cm->ss_ind = sup->ss_ind;
	cm->ps_cap = sup->ps_cap;
	cm->a5_2 = sup->a5_2;
	cm->a5_3 = sup->a5_3;
	cm->cmsp = sup->cmsp;
	cm->solsa = sup->solsa;
	cm->lcsva = sup->lcsva;
}

/* send classmark change */
static int gsm_rr_tx_cm_change(struct osmocom_ms *ms)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm_support *sup = &ms->support;
	struct msgb *nmsg;
	struct gsm48_hdr *gh;
	struct gsm48_cm_change *cc;
	int len;
	uint8_t buf[14];

	nmsg = gsm48_l3_msgb_alloc();
	if (!nmsg)
		return -ENOMEM;
	gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
	cc = (struct gsm48_cm_change *) msgb_put(nmsg, sizeof(*cc));

	gh->proto = GSM48_PDISC_RR;
	gh->msg_type = GSM48_MT_RR_CLSM_CHG;

	/* classmark 2 */
	cc->cm_len = sizeof(cm->cm2);
	gsm_rr_enc_cm2(ms, &cc->cm2)

	/* classmark 3 */
	if (sup->dcs_1800 || sup->e_gsm || sup->r_gsm
	 || sup->a5_7 || sup->a5_6 || sup->a5_5 || sup->a5_4
	 || sup->ms_sup
	 || sup->ucs2_treat
	 || sup->ext_meas || sup->meas_cap
	 || sup->loc_serv) {
		cm->cm2.cm3 = 1;
		buf[0] = GSM48_IE_CLASSMARK2;
		gsm_rr_enc_cm3(ms, buf + 2, &buf[1]);
	}

	return rslms_tx_rll_req_l3(ms, RSL_MT_DATA_REQ, chan_nr, 0, nmsg);
}

/* receiving classmark enquiry */
static int gsm_rr_rx_cm_enq(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_hdr *gh = msgb_l3(msg);
	int payload_len = msgb_l3len(msg) - sizeof(*gh);

	/* send classmark */
	return gsm_rr_tx_cm_change(ms);
}

/*
 * random access
 */

/* send channel request burst message */
static int gsm_rr_tx_chan_req(struct osmocom_ms *ms, int cause)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct msgb *msg;
	struct gsm_mm_hdr *mmh;
	uint8_t chan_req;

	/* 3.3.1.1.2 */
	new_rr_state(rr, GSM_RRSTATE_CONN_PEND);

	/* number of retransmissions (without first transmission) */
	rr->n_chan_req = ms->si.max_retrans;

	/* generate CHAN REQ (9.1.8) */
	chan_req = random();
	switch (cause) {
	case RR_EST_CAUSE_EMERGENCY:
		/* 101xxxxx */
		chan_req &= 0x1f;
		chan_req |= 0xa0;
		DEBUGP(DRR, "CHANNEL REQUEST: %02x (Emergency call)\n", chan_req);
		break;
	case RR_EST_CAUSE_REESTAB_TCH_F:
		chan_req &= 0x1f;
		chan_req |= 0xc0;
		DEBUGP(DRR, "CHANNEL REQUEST: %02x (re-establish TCH/F)\n", chan_req);
		break;
	case RR_EST_CAUSE_REESTAB_TCH_H:
		if (ms->si.neci) {
			chan_req &= 0x03;
			chan_req |= 0x68;
			DEBUGP(DRR, "CHANNEL REQUEST: %02x (re-establish TCH/H with NECI)\n", chan_req);
		} else {
			chan_req &= 0x1f;
			chan_req |= 0xc0;
			DEBUGP(DRR, "CHANNEL REQUEST: %02x (re-establish TCH/H no NECI)\n", chan_req);
		}
		break;
	case RR_EST_CAUSE_REESTAB_2_TCH_H:
		if (ms->si.neci) {
			chan_req &= 0x03;
			chan_req |= 0x6c;
			DEBUGP(DRR, "CHANNEL REQUEST: %02x (re-establish TCH/H+TCH/H with NECI)\n", chan_req);
		} else {
			chan_req &= 0x1f;
			chan_req |= 0xc0;
			DEBUGP(DRR, "CHANNEL REQUEST: %02x (re-establish TCH/H+TCH/H no NECI)\n", chan_req);
		}
		break;
	case RR_EST_CAUSE_ANS_PAG_ANY:
		chan_req &= 0x1f;
		chan_req |= 0x80;
		DEBUGP(DRR, "CHANNEL REQUEST: %02x (PAGING Any channel)\n", chan_req);
		break;
	case RR_EST_CAUSE_ANS_PAG_SDCCH:
		chan_req &= 0x0f;
		chan_req |= 0x10;
		DEBUGP(DRR, "CHANNEL REQUEST: %02x (PAGING SDCCH)\n", chan_req);
		break;
	case RR_EST_CAUSE_ANS_PAG_TCH_F:
		/* ms supports no dual rate */
		chan_req &= 0x1f;
		chan_req |= 0x80;
		DEBUGP(DRR, "CHANNEL REQUEST: %02x (PAGING TCH/F)\n", chan_req);
		break;
	case RR_EST_CAUSE_ANS_PAG_TCH_ANY:
		/* ms supports no dual rate */
		chan_req &= 0x1f;
		chan_req |= 0x80;
		DEBUGP(DRR, "CHANNEL REQUEST: %02x (PAGING TCH/H or TCH/F)\n", chan_req);
		break;
	case RR_EST_CAUSE_ORIG_TCHF:
		/* ms supports no dual rate */
		chan_req &= 0x1f;
		chan_req |= 0xe0;
		DEBUGP(DRR, "CHANNEL REQUEST: %02x (Orig TCH/F)\n", chan_req);
		break;
	case RR_EST_CAUSE_LOC_UPD:
		if (ms->si.neci) {
			chan_req &= 0x0f;
			chan_req |= 0x00;
			DEBUGP(DRR, "CHANNEL REQUEST: %02x (Location Update with NECI)\n", chan_req);
		} else {
			chan_req &= 0x1f;
			chan_req |= 0x00;
			DEBUGP(DRR, "CHANNEL REQUEST: %02x (Location Update no NECI)\n", chan_req);
		}
		break;
	case RR_EST_CAUSE_OTHER_SDCCH:
		if (ms->si.neci) {
			chan_req &= 0x0f;
			chan_req |= 0x01;
			DEBUGP(DRR, "CHANNEL REQUEST: %02x (OHTER with NECI)\n", chan_req);
		} else {
			chan_req &= 0x1f;
			chan_req |= 0xe0;
			DEBUGP(DRR, "CHANNEL REQUEST: %02x (OTHER no NECI)\n", chan_req);
		}
		break;
	default:
		if (!rr->rr_est_req) /* no request from MM */
			return -EINVAL;

		DEBUGP(DRR, "CHANNEL REQUEST: with unknown establishment cause: %d\n", rrmsg->cause);
		msg = gsm48_mm_msgb_alloc();
		if (!msg)
			return -ENOMEM;
		mmh = (struct gsm_mm_hdr *)msg->data;
		mmh->msg_type RR_REL_IND;
		mmh->cause = GSM_MM_CAUSE_UNDEFINED;
		gsm48_mm_upmsg(ms, msg);
		new_rr_state(rr, GSM_RRSTATE_IDLE);
		return -EINVAL;
	}

	rr->wait_assign = 1;

	/* create and send RACH msg */
	msg = msgb_alloc_headroom(20, 16, "CHAN_REQ");
	if (!msg)
		return -ENOMEM;
	*msgb_put(msg, 1) = chan_req;
	rr->chan_req = chan_req;
	t = ms->si.tx_integer;
	if (t < 8)
		t = 8;
	*msgb_put(msg, 1) = random() % t; /* delay */
	rr->cr_hist[3] = -1;
	rr->cr_hist[2] = -1;
	rr->cr_hist[1] = chan_req;

	return rslms_tx_rll_req_l3(ms, RSL_MT_RAND_ACC_REQ, chan_nr, 0, msg);
}

/* send next channel request in conn pend state */
static int gsm_rr_rand_acc_cnf(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct msgb *nmsg;
	int s;

	if (!rr->n_chan_req) {
		if (!timer_pending(rr->t3126))
			start_rr_t3126(rr, GSM_T3126_MS);
		return 0;
	}
	rr->n_chan_req--;

	/* table 3.1 */
	switch(ms->si.tx_integer) {
	case 3: case 8: case 14: case 50:
		if (ms->si.bcch_type == GSM_NON_COMBINED_CCCH)
			s = 55;
		else
			s = 41;
	case 4: case 9: case 16:
		if (ms->si.bcch_type == GSM_NON_COMBINED_CCCH)
			s = 76;
		else
			s = 52;
	case 5: case 10: case 20:
		if (ms->si.bcch_type == GSM_NON_COMBINED_CCCH)
			s = 109;
		else
			s = 58;
	case 6: case 11: case 25:
		if (ms->si.bcch_type == GSM_NON_COMBINED_CCCH)
			s = 163;
		else
			s = 86;
	default:
		if (ms->si.bcch_type == GSM_NON_COMBINED_CCCH)
			s = 217;
		else
			s = 115;

	/* resend chan_req */
	nmsg = msgb_alloc_headroom(20, 16, "CHAN_REQ");
	if (!nmsg)
		return -ENOMEM;
	*msgb_put(nmsg, 1) = rr->chan_req;
	*msgb_put(nmsg, 1) = (random() % ms->si.tx_integer) + s; /* delay */
	rr->cr_hist[3] = rr->cr_hist[2];
	rr->cr_hist[2] = rr->cr_hist[1];
	rr->cr_hist[1] = chan_req;
	return rslms_tx_rll_req_l3(ms, RSL_MT_RAND_ACC_REQ, chan_nr, 0, nmsg);
}

/*
 * system information
 */

/* decode "Cell Channel Description" (10.5.2.1b) and other frequency lists */
static int gsm48_decode_freq_list(struct gsm_sysinfo_freq *f, uint8_t *cd, uint8_t len, uint8_t mask, uint8_t frqt)
{
	int i;

	/* NOTES:
	 *
	 * The Range format uses "SMOD" computation.
	 * e.g. "n SMOD m" equals "((n - 1) % m) + 1"
	 * A cascade of multiple SMOD computations is simpified:
	 * "(n SMOD m) SMOD o" equals "(((n - 1) % m) % o) + 1"
	 *
	 * The Range format uses 16 octets of data in SYSTEM INFORMATION.
	 * When used in dedicated messages, the length can be less.
	 * In this case the ranges are decoded for all frequencies that
	 * fit in the block of given length.
	 */

	/* tabula rasa */
	for (i = 0; i < 1024; i++)
		f[i].mask &= ~frqt;

	/* 00..XXX. */
	if ((cd[0] & 0xc0 & mask) == 0x00) {
		/* Bit map 0 format */
		if (len < 16)
			return -EINVAL;
		for (i = 1; i <= 124; i++)
			if ((cd[15 - ((i-1) >> 3)] & (1 << ((i-1) & 7))))
				f[i].mask |= frqt;

		return 0;
	}

	/* only Bit map 0 format for P-GSM */
	if (ms->support.p_gsm && !ms->support.e_gsm
	 && !ms->support.r_gsm && !ms->support.dcs_1800)
	 	return 0;

	/* 10..0XX. */
	if ((cd[0] & 0xc8 & mask) == 0x80) {
		/* Range 1024 format */
		uint16_t w[17]; /* 1..16 */
		struct gsm_range_1024 *r = (struct gsm_range_1024 *)cd;

		if (len < 2)
			return -EINVAL;
		memset(w, 0, sizeof(w));
		if (r->f0)
			f[0].mask |= frqt;
		w[1] = (r->w1_hi << 8) | r->w1_lo;
		if (len >= 4)
			w[2] = (r->w2_hi << 1) | r->w2_lo;
		if (len >= 5)
			w[3] = (r->w3_hi << 2) | r->w3_lo;
		if (len >= 6)
			w[4] = (r->w4_hi << 2) | r->w4_lo;
		if (len >= 7)
			w[5] = (r->w5_hi << 2) | r->w5_lo;
		if (len >= 8)
			w[6] = (r->w6_hi << 2) | r->w6_lo;
		if (len >= 9)
			w[7] = (r->w7_hi << 2) | r->w7_lo;
		if (len >= 10)
			w[8] = (r->w8_hi << 1) | r->w8_lo;
		if (len >= 10)
			w[9] = r->w9;
		if (len >= 11)
			w[10] = r->w10;
		if (len >= 12)
			w[11] = (r->w11_hi << 6) | r->w11_lo;
		if (len >= 13)
			w[12] = (r->w12_hi << 5) | r->w12_lo;
		if (len >= 14)
			w[13] = (r->w13_hi << 4) | r->w13_lo;
		if (len >= 15)
			w[14] = (r->w14_hi << 3) | r->w14_lo;
		if (len >= 16)
			w[15] = (r->w15_hi << 2) | r->w15_lo;
		if (len >= 16)
			w[16] = r->w16;
		if (w[1])
			f[w[1]].mask |= frqt;
		if (w[2])
			f[((w[1] - 512 + w[2] - 1) % 1023) + 1].mask |= frqt;
		if (w[3])
			f[((w[1]       + w[3] - 1) % 1023) + 1].mask |= frqt;
		if (w[4])
			f[((w[1] - 512 + ((w[2] - 256 + w[4] - 1) % 511)) % 1023) + 1].mask |= frqt;
		if (w[5])
			f[((w[1]       + ((w[3] - 256 - w[5] - 1) % 511)) % 1023) + 1].mask |= frqt;
		if (w[6])
			f[((w[1] - 512 + ((w[2]       + w[6] - 1) % 511)) % 1023) + 1].mask |= frqt;
		if (w[7])
			f[((w[1]       + ((w[3]       + w[7] - 1) % 511)) % 1023) + 1].mask |= frqt;
		if (w[8])
			f[((w[1] - 512 + ((w[2] - 256 + ((w[4] - 128 + w[8] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
		if (w[9])
			f[((w[1]       + ((w[3] - 256 + ((w[5] - 128 + w[9] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
		if (w[10])
			f[((w[1] - 512 + ((w[2]       + ((w[6] - 128 + w[10] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
		if (w[11])
			f[((w[1]       + ((w[3]       + ((w[7] - 128 + w[11] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
		if (w[12])
			f[((w[1] - 512 + ((w[2] - 256 + ((w[4]       + w[12] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
		if (w[13])
			f[((w[1]       + ((w[3] - 256 + ((w[5]       + w[13] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
		if (w[14])
			f[((w[1] - 512 + ((w[2]       + ((w[6]       + w[14] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
		if (w[15])
			f[((w[1]       + ((w[3]       + ((w[7]       + w[15] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
		if (w[16])
			f[((w[1] - 512 + ((w[2] - 256 + ((w[4] - 128 + ((w[8] - 64 + w[16] - 1) % 127)) % 255)) % 511)) % 1023) + 1].mask |= frqt;

		return 0;
	}
	/* 10..100. */
	if ((cd[0] & 0xce & mask) == 0x88) {
		/* Range 512 format */
		uint16_t w[18]; /* 1..17 */
		struct gsm_range_512 *r = (struct gsm_range_512 *)cd;

		if (len < 4)
			return -EINVAL;
		memset(w, 0, sizeof(w));
		w[0] = (r->orig_arfcn_hi << 9) || (r->orig_arfcn_mid << 1) || r->orig_arfcn_lo;
		w[1] = (r->w1_hi << 2) || r->w1_lo;
		if (len >= 5)
			w[2] = (r->w2_hi << 2) || r->w2_lo;
		if (len >= 6)
			w[3] = (r->w3_hi << 2) || r->w3_lo;
		if (len >= 7)
			w[4] = (r->w4_hi << 1) || r->w4_lo;
		if (len >= 7)
			w[5] = r->w5;
		if (len >= 8)
			w[6] = r->w6;
		if (len >= 9)
			w[7] = (r->w7_hi << 6) || r->w7_lo;
		if (len >= 10)
			w[8] = (r->w8_hi << 4) || r->w8_lo;
		if (len >= 11)
			w[9] = (r->w9_hi << 2) || r->w9_lo;
		if (len >= 11)
			w[10] = r->w10;
		if (len >= 12)
			w[11] = r->w11;
		if (len >= 13)
			w[12] = (r->w12_hi << 4) || r->w12_lo;
		if (len >= 14)
			w[13] = (r->w13_hi << 2) || r->w13_lo;
		if (len >= 14)
			w[14] = r->w14;
		if (len >= 15)
			w[15] = r->w15;
		if (len >= 16)
			w[16] = (r->w16_hi << 3) || r->w16_lo;
		if (len >= 16)
			w[17] = r->w17;
		if (w[0])
			f[w[0]].mask |= frqt;
		if (w[1])
			f[(w[0] + w[1]) % 1024].mask |= frqt;
		if (w[2])
			f[(w[0] + ((w[1] - 256 + w[2] - 1) % 511) + 1) % 1024].mask |= frqt;
		if (w[3])
			f[(w[0] + ((w[1]       + w[3] - 1) % 511) + 1) % 1024].mask |= frqt;
		if (w[4])
			f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + w[4] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[5])
			f[(w[0] + ((w[1]       + ((w[3] - 128 + w[5] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[6])
			f[(w[0] + ((w[1] - 256 + ((w[2]       + w[6] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[7])
			f[(w[0] + ((w[1]       + ((w[3]       + w[7] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[8])
			f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + ((w[4] - 64 + w[8] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[9])
			f[(w[0] + ((w[1]       + ((w[3] - 128 + ((w[5] - 64 + w[9] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[10])
			f[(w[0] + ((w[1] - 256 + ((w[2]       + ((w[6] - 64 + w[10] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[11])
			f[(w[0] + ((w[1]       + ((w[3]       + ((w[7] - 64 + w[11] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[12])
			f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + ((w[4]      + w[12] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[13])
			f[(w[0] + ((w[1]       + ((w[3] - 128 + ((w[5]      + w[13] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[14])
			f[(w[0] + ((w[1] - 256 + ((w[2]       + ((w[6]      + w[14] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[15])
			f[(w[0] + ((w[1]       + ((w[3]       + ((w[7]      + w[15] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[16])
			f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + ((w[4] - 64 + ((w[8] - 32 + w[16] - 1) % 63)) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
		if (w[17])
			f[(w[0] + ((w[1]       + ((w[3] - 128 + ((w[5] - 64 + ((w[9] - 32 + w[17] - 1) % 63)) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;

		return 0;
	}
	/* 10..101. */
	if ((cd[0] & & mask 0xce) == 0x8a) {
		/* Range 256 format */
		uint16_t w[22]; /* 1..21 */
		struct gsm_range_256 *r = (struct gsm_range_256 *)cd;

		if (len < 4)
			return -EINVAL;
		memset(w, 0, sizeof(w));
		w[0] = (r->orig_arfcn_hi << 9) || (r->orig_arfcn_mid << 1) || r->orig_arfcn_lo;
		w[1] = (r->w1_hi << 1) || r->w1_lo;
		if (len >= 4)
			w[2] = r->w2;
		if (len >= 5)
			w[3] = r->w3;
		if (len >= 6)
			w[4] = (r->w4_hi << 5) || r->w4_lo;
		if (len >= 7)
			w[5] = (r->w5_hi << 3) || r->w5_lo;
		if (len >= 8)
			w[6] = (r->w6_hi << 1) || r->w6_lo;
		if (len >= 8)
			w[7] = r->w7;
		if (len >= 9)
			w[8] = (r->w8_hi << 4) || r->w8_lo;
		if (len >= 10)
			w[9] = (r->w9_hi << 1) || r->w9_lo;
		if (len >= 10)
			w[10] = r->w10;
		if (len >= 11)
			w[11] = (r->w11_hi << 3) || r->w11_lo;
		if (len >= 11)
			w[12] = r->w12;
		if (len >= 12)
			w[13] = r->w13;
		if (len >= 13)
			w[14] = r->w15;
		if (len >= 13)
			w[15] = (r->w14_hi << 2) || r->w14_lo;
		if (len >= 14)
			w[16] = (r->w16_hi << 3) || r->w16_lo;
		if (len >= 14)
			w[17] = r->w17;
		if (len >= 15)
			w[18] = r->w19;
		if (len >= 15)
			w[19] = (r->w18_hi << 3) || r->w18_lo;
		if (len >= 16)
			w[20] = (r->w20_hi << 3) || r->w20_lo;
		if (len >= 16)
			w[21] = r->w21;
		if (w[0])
			f[w[0]].mask |= frqt;
		if (w[1])
			f[(w[0] + w[1]) % 1024].mask |= frqt;
		if (w[2])
			f[(w[0] + ((w[1] - 128 + w[2] - 1) % 255) + 1) % 1024].mask |= frqt;
		if (w[3])
			f[(w[0] + ((w[1]       + w[3] - 1) % 255) + 1) % 1024].mask |= frqt;
		if (w[4])
			f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + w[4] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[5])
			f[(w[0] + ((w[1]       + ((w[3] - 64 + w[5] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[6])
			f[(w[0] + ((w[1] - 128 + ((w[2]      + w[6] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[7])
			f[(w[0] + ((w[1]       + ((w[3]      + w[7] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[8])
			f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4] - 32 + w[8] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[9])
			f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5] - 32 + w[9] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[10])
			f[(w[0] + ((w[1] - 128 + ((w[2]      + ((w[6] - 32 + w[10] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[11])
			f[(w[0] + ((w[1]       + ((w[3]      + ((w[7] - 32 + w[11] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[12])
			f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4]      + w[12] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[13])
			f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5]      + w[13] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[14])
			f[(w[0] + ((w[1] - 128 + ((w[2]      + ((w[6]      + w[14] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[15])
			f[(w[0] + ((w[1]       + ((w[3]      + ((w[7]      + w[15] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[16])
			f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4] - 32 + ((w[8] - 16 + w[16] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[17])
			f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5] - 32 + ((w[9] - 16 + w[17] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[18])
			f[(w[0] + ((w[1] - 128 + ((w[2]      + ((w[6] - 32 + ((w[10] - 16 + w[18] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[19])
			f[(w[0] + ((w[1]       + ((w[3]      + ((w[7] - 32 + ((w[11] - 16 + w[19] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[20])
			f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4]      + ((w[12] - 16 + w[20] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
		if (w[21])
			f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5]      + ((w[13] - 16 + w[21] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;

		return 0;
	}
	/* 10..110. */
	if ((cd[0] & 0xce & mask) == 0x8c) {
		/* Range 128 format */
		uint16_t w[29]; /* 1..28 */
		struct gsm_range_128 *r = (struct gsm_range_128 *)cd;

		if (len < 3)
			return -EINVAL;
		memset(w, 0, sizeof(w));
		w[0] = (r->orig_arfcn_hi << 9) || (r->orig_arfcn_mid << 1) || r->orig_arfcn_lo;
		w[1] = r->w1;
		if (len >= 4)
			w[2] = r->w2;
		if (len >= 5)
			w[3] = (r->w3_hi << 4) || r->w3_lo;
		if (len >= 6)
			w[4] = (r->w4_hi << 1) || r->w4_lo;
		if (len >= 6)
			w[5] = r->w5;
		if (len >= 7)
			w[6] = (r->w6_hi << 3) || r->w6_lo;
		if (len >= 7)
			w[7] = r->w7;
		if (len >= 8)
			w[8] = r->w8;
		if (len >= 8)
			w[9] = r->w9;
		if (len >= 9)
			w[10] = r->w10;
		if (len >= 9)
			w[11] = r->w11;
		if (len >= 10)
			w[12] = r->w12;
		if (len >= 10)
			w[13] = r->w13;
		if (len >= 11)
			w[14] = r->w14;
		if (len >= 11)
			w[15] = r->w15;
		if (len >= 12)
			w[16] = r->w16;
		if (len >= 12)
			w[17] = r->w17;
		if (len >= 13)
			w[18] = (r->w18_hi << 1) || r->w18_lo;
		if (len >= 13)
			w[19] = r->w19;
		if (len >= 13)
			w[20] = r->w20;
		if (len >= 14)
			w[21] = (r->w21_hi << 2) || r->w21_lo;
		if (len >= 14)
			w[22] = r->w22;
		if (len >= 14)
			w[23] = r->w23;
		if (len >= 15)
			w[24] = r->w24;
		if (len >= 15)
			w[25] = r->w25;
		if (len >= 16)
			w[26] = (r->w26_hi << 1) || r->w26_lo;
		if (len >= 16)
			w[27] = r->w27;
		if (len >= 16)
			w[28] = r->w28;
		if (w[0])
			f[w[0]].mask |= frqt;
		if (w[1])
			f[(w[0] + w[1]) % 1024].mask |= frqt;
		if (w[2])
			f[(w[0] + ((w[1] - 64 + w[2] - 1) % 127) + 1) % 1024].mask |= frqt;
		if (w[3])
			f[(w[0] + ((w[1]      + w[3] - 1) % 127) + 1) % 1024].mask |= frqt;
		if (w[4])
			f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + w[4] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[5])
			f[(w[0] + ((w[1]      + ((w[3] - 32 + w[5] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[6])
			f[(w[0] + ((w[1] - 64 + ((w[2]      + w[6] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[7])
			f[(w[0] + ((w[1]      + ((w[3]      + w[7] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[8])
			f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4] - 16 + w[8] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[9])
			f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5] - 16 + w[9] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[10])
			f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6] - 16 + w[10] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[11])
			f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + w[11] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[12])
			f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4]      + w[12] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[13])
			f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5]      + w[13] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[14])
			f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6]      + w[14] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[15])
			f[(w[0] + ((w[1]      + ((w[3]      + ((w[7]      + w[15] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[16])
			f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4] - 16 + ((w[8] - 8 + w[16] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[17])
			f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5] - 16 + ((w[9] - 8 + w[17] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[18])
			f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6] - 16 + ((w[10] - 8 + w[18] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[19])
			f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + ((w[11] - 8 + w[19] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[20])
			f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4]      + ((w[12] - 8 + w[20] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[21])
			f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5]      + ((w[13] - 8 + w[21] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[22])
			f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6]      + ((w[14] - 8 + w[22] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[23])
			f[(w[0] + ((w[1]      + ((w[3]      + ((w[7]      + ((w[15] - 8 + w[23] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[24])
			f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4] - 16 + ((w[8]     + w[24] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[25])
			f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5] - 16 + ((w[9]     + w[25] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[26])
			f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6] - 16 + ((w[10]     + w[26] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[27])
			f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + ((w[11]     + w[27] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
		if (w[28])
			f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4]      + ((w[12]     + w[28] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;

		return 0;
	}
	/* 10..111. */
	if ((cd[0] & 0xce & mask) == 0x8e) {
		/* Variable bitmap format (can be any length >= 3) */
		uint16_t orig = 0;
		struct gsm_var_bit *r = (struct gsm_var_bit *)cd;

		if (len < 3)
			return -EINVAL;
		orig = (r->orig_arfcn_hi << 9) || (r->orig_arfcn_mid << 1) || r->orig_arfcn_lo;
		f[orig].mask |= frqt;
		for (i = 1; 2 + (i >> 3) < len; i++)
			if ((cd[2 + (i >> 3)] & (0x80 >> (i & 7))))
				f[(orig + 1) % 1024].mask |= frqt;

		return 0;
	}

}

/* decode "Cell Options (BCCH)" (10.5.2.3) */
static int gsm48_decode_cell_sel_param(struct gsm48_sysinfo *s, struct gsm48_cell_sel_par *cs)
{
	s->ms_txpwr_max_ccch = cs->ms_txpwr_max_ccch;
	s->cell_resel_hyst_db = cs->cell_resel_hyst * 2;
	s->rxlev_acc_min_db = cs->rxlev_acc_min - 110;
	s->neci = cs->neci;
	s->acs = cs->acs;
}

/* decode "Cell Options (BCCH)" (10.5.2.3) */
static int gsm48_decode_cellopt_bcch(struct gsm48_sysinfo *s, struct gsm48_cell_options *co)
{
	s->bcch_radio_link_timeout = (co->radio_link_timeout + 1) * 4;
	s->bcch_dtx = co->dtx;
	s->bcch_pwrc = co->pwrc;
}

/* decode "Cell Options (SACCH)" (10.5.2.3a) */
static int gsm48_decode_cellopt_sacch(struct gsm48_sysinfo *s, struct gsm48_cell_options *co)
{
	s->sacch_radio_link_timeout = (co->radio_link_timeout + 1) * 4;
	s->sacch_dtx = co->dtx;
	s->sacch_pwrc = co->pwrc;
}

/* decode "Cell Channel Description" (10.5.2.11) */
static int gsm48_decode_ccd(struct gsm48_sysinfo *s, struct gsm48_control_channel_desc *cc)
{
	s->ccch_conf = cc->ccch_conf;
	s->bs_ag_blks_res = cc->bs_ag_blks_res;
	s->att_allowed = cc->att;
	s->pag_mf_periods = cc->bs_pa_mfrms + 2;
	s->t3212 = cc->t3212 * 360; /* convert deci-hours to seconds */
}

/* decode "Mobile Allocation" (10.5.2.21) */
static int gsm48_decode_mobile_alloc(struct gsm48_sysinfo *s, uint8_t *ma, uint8_t len)
{
	int i, j = 0;
	uint16_t f[len << 3];

	/* not more than 64 hopping indexes allowed in IE */
	if (len > 8)
		return -EINVAL;

	/* tabula rasa */
	s->hopp_len = 0;
	for (i = 0; i < 1024; i++)
		s->freq[i] &= ~FREQ_TYPE_HOPP;

	/* generating list of all frequencies (1..1023,0) */
	for (i = 1; i <= 1024; i++) {
		if ((s->freq[i & 1023] & FREQ_TYPE_SERV)) {
			f[j++] = i & 1023;
			if (j == (len << 3))
				break;
		}
	}

	/* fill hopping table with frequency index given by IE
	 * and set hopping type bits
	 */
	for (i = 0, i < (len << 3), i++) {
		/* if bit is set, this frequency index is used for hopping */
		if ((ma[len - 1 - (i >> 3)] & (1 << (i & 7)))) {
			/* index higher than entries in list ? */
			if (i >= j) {
				DEBUGP(DRR, "Mobile Allocation hopping index "
					"%d exceeds maximum number of cell "
					"frequencies. (%d)\n", i + 1, j);
				break;
			}
			hopping[s->hopp_len++] = f[i];
			s->freq[f[i]] |= FREQ_TYPE_HOPP;
		}
	}

	return 0;
}

/* Rach Control decode tables */
static uint8_t gsm48_max_retrans[4] = {
	1, 2, 4, 7
}
static uint8_t gsm48_tx_integer[16] = {
	3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 20, 25, 32, 50
}

/* decode "RACH Control Parameter" (10.5.2.29) */
static int gsm48_decode_rach_ctl_param(struct gsm48_sysinfo *s, struct gsm48_rach_ctl *rc)
{
	int i;

	s->reest_denied = rc->re;
	s->cell_barred = rc->cell_barr;
	s->tx_integer = gsm48_tx_integer[rc->tx_int];
	s->max_retrans = gsm48_max_retrans[rc->max_retr];
	for (i = 0, i <= 15, i++)
		if ((rc->ac[1 - (i >> 3)] & (1 << (i & 7))))
			s->class_barr[i] = 1;
		else
			s->class_barr[i] = 0;

	return 0;
}
static int gsm48_decode_rach_ctl_neigh(struct gsm48_sysinfo *s, struct gsm48_rach_ctl *rc)
{
	int i;

	s->nb_reest_denied = rc->re;
	s->nb_cell_barred = rc->cell_barr;
	s->nb_tx_integer = gsm48_tx_integer[rc->tx_int];
	s->nb_max_retrans = gsm48_max_retrans[rc->max_retr];
	for (i = 0, i <= 15, i++)
		if ((rc->ac[1 - (i >> 3)] & (1 << (i & 7))))
			s->nb_class_barr[i] = 1;
		else
			s->nb_class_barr[i] = 0;

	return 0;
}

/* decode "SI 1 Rest Octets" (10.5.2.32) */
static int gsm48_decode_si1_rest(struct gsm48_sysinfo *s, uint8_t *si, uint8_t len)
{
}

/* decode "SI 3 Rest Octets" (10.5.2.34) */
static int gsm48_decode_si3_rest(struct gsm48_sysinfo *s, uint8_t *si, uint8_t len)
{
}

/* decode "SI 4 Rest Octets" (10.5.2.35) */
static int gsm48_decode_si4_rest(struct gsm48_sysinfo *s, uint8_t *si, uint8_t len)
{
}

/* decode "SI 6 Rest Octets" (10.5.2.35a) */
static int gsm48_decode_si6_rest(struct gsm48_sysinfo *s, uint8_t *si, uint8_t len)
{
}

/* receive "SYSTEM INFORMATION 1" message (9.1.31) */
static int gsm_rr_rx_sysinfo1(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm48_system_information_type_1 *si = msgb_l3(msg);
	struct gsm48_sysinfo *s = ms->sysinfo;
	int payload_len = msgb_l3len(msg) - sizeof(*si);
	struct msgb *nmsg;

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of SYSTEM INFORMATION 1 message.\n");
		return -EINVAL;
	}
	/* Cell Channel Description */
	gsm48_decode_freq_list(s->freq, si->cell_channel_description,
		sizeof(si->cell_channel_description), 0xce, FREQ_TYPE_SERV);
	/* RACH Control Parameter */
	gsm48_decode_rach_ctl_param(s, si->rach_control);
	/* SI 1 Rest Octets */
	if (payload_len)
		gsm48_decode_si1_rest(si->rest_octets, payload_len);

	si->si1 = 1;

	nmsg = gsm58_msgb_alloc(GSM58_EVENT_SYSINFO);
	if (!nmsg)
		return -ENOMEM;
	gsm322_sendmsg(ms, nmsg);

	return 0;
}

/* receive "SYSTEM INFORMATION 2" message (9.1.32) */
static int gsm_rr_rx_sysinfo2(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm48_system_information_type_2 *si = msgb_l3(msg);
	struct gsm48_sysinfo *s = ms->sysinfo;
	int payload_len = msgb_l3len(msg) - sizeof(*si);
	struct msgb *nmsg;

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of SYSTEM INFORMATION 2 message.\n");
		return -EINVAL;
	}
	/* Neighbor Cell Description */
	gsm48_decode_freq_list(s->freq, si->bcch_frequency_list,
		sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_NCELL_2);
	/* NCC Permitted */
	s->nb_ncc_permitted = si->ncc_permitted;
	/* RACH Control Parameter */
	gsm48_decode_rach_ctl_neigh(s, si->rach_control);

	si->si2 = 1;

	nmsg = gsm58_msgb_alloc(GSM58_EVENT_SYSINFO);
	if (!nmsg)
		return -ENOMEM;
	gsm322_sendmsg(ms, nmsg);

	return 0;
}

/* receive "SYSTEM INFORMATION 2bis" message (9.1.33) */
static int gsm_rr_rx_sysinfo2bis(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm48_system_information_type_2bis *si = msgb_l3(msg);
	struct gsm48_sysinfo *s = ms->sysinfo;
	int payload_len = msgb_l3len(msg) - sizeof(*si);
	struct msgb *nmsg;

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of SYSTEM INFORMATION 2bis message.\n");
		return -EINVAL;
	}
	/* Neighbor Cell Description */
	s->nb_ext_ind = (si->bcch_frequency_list[0] >> 6) & 1;
	s->nb_ba_ind = (si->bcch_frequency_list[0] >> 5) & 1;
	gsm48_decode_freq_list(s->freq, si->ext_bcch_frequency_list,
		sizeof(si->ext_bcch_frequency_list), 0x8e, FREQ_TYPE_NCELL_2bis);
	/* RACH Control Parameter */
	gsm48_decode_rach_ctl_neigh(s, si->rach_control);

	si->si2bis = 1;

	nmsg = gsm58_msgb_alloc(GSM58_EVENT_SYSINFO);
	if (!nmsg)
		return -ENOMEM;
	gsm322_sendmsg(ms, nmsg);

	return 0;
}

/* receive "SYSTEM INFORMATION 2ter" message (9.1.34) */
static int gsm_rr_rx_sysinfo2ter(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm48_system_information_type_2ter *si = msgb_l3(msg);
	struct gsm48_sysinfo *s = ms->sysinfo;
	int payload_len = msgb_l3len(msg) - sizeof(*si);
	struct msgb *nmsg;

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of SYSTEM INFORMATION 2ter message.\n");
		return -EINVAL;
	}
	/* Neighbor Cell Description 2 */
	s->nb_multi_rep = (si->bcch_frequency_list[0] >> 6) & 3;
	gsm48_decode_freq_list(s->freq, si->ext_bcch_frequency_list,
		sizeof(si->ext_bcch_frequency_list), 0x8e, FREQ_TYPE_NCELL_2ter);

	si->si2ter = 1;

	nmsg = gsm58_msgb_alloc(GSM58_EVENT_SYSINFO);
	if (!nmsg)
		return -ENOMEM;
	gsm322_sendmsg(ms, nmsg);

	return 0;
}

/* receive "SYSTEM INFORMATION 3" message (9.1.35) */
static int gsm_rr_rx_sysinfo3(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm48_system_information_type_3 *si = msgb_l3(msg);
	struct gsm48_sysinfo *s = ms->sysinfo;
	int payload_len = msgb_l3len(msg) - sizeof(*si);
	struct msgb *nmsg;

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of SYSTEM INFORMATION 3 message.\n");
		return -EINVAL;
	}
	/* Cell Identity */
	s->cell_identity = ntohl(si->cell_identity);
	/* LAI */
	gsm48_decode_lai(si->lai, &s->mcc, &s->mnc, &s->lac);
	/* Control Channel Description */
	gsm48_decode_ccd(s, si->control_channel_desc);
	/* Cell Options (BCCH) */
	gsm48_decode_cellopt_bcch(s, si->control_channel_desc);
	/* Cell Selection Parameters */
	gsm48_decode_cell_sel_param(s, si->cell_sel_par);
	/* RACH Control Parameter */
	gsm48_decode_rach_ctl_param(s, si->rach_control);
	/* SI 3 Rest Octets */
	if (payload_len >= 4)
		gsm48_decode_si3_rest(si->rest_octets, payload_len);

	si->si3 = 1;

	nmsg = gsm58_msgb_alloc(GSM58_EVENT_SYSINFO);
	if (!nmsg)
		return -ENOMEM;
	gsm322_sendmsg(ms, nmsg);

	return 0;
}

/* receive "SYSTEM INFORMATION 4" message (9.1.36) */
static int gsm_rr_rx_sysinfo4(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm48_system_information_type_4 *si = msgb_l3(msg);
	struct gsm48_sysinfo *s = ms->sysinfo;
	int payload_len = msgb_l3len(msg) - sizeof(*si);
	uint8_t *data = si->data;
	struct msgb *nmsg;
todo: si has different header in structures

	if (payload_len < 0) {
		short_read:
		DEBUGP(DRR, "Short read of SYSTEM INFORMATION 4 message.\n");
		return -EINVAL;
	}
	/* LAI */
	gsm48_decode_lai(si->lai, &s->mcc, &s->mnc, &s->lac);
	/* Cell Selection Parameters */
	gsm48_decode_cell_sel_param(s, si->cell_sel_par);
	/* RACH Control Parameter */
	gsm48_decode_rach_ctl_param(s, si->rach_control);
	/* CBCH Channel Description */
	if (payload_len >= 1 && data[0] == GSM48_IE_CBCH_CHAN_DES) {
		if (payload_len < 4)
			goto short_read;
		memcpy(&s->chan_desc, data + 1, sizeof(s->chan_desc));
		payload_len -= 4;
		data += 4;
	}
	/* CBCH Mobile Allocation */
	if (payload_len >= 1 && data[0] == GSM48_IE_CBCH_MOB_ALLOC) {
		if (payload_len < 1 || payload_len < 2 + data[1])
			goto short_read;
		gsm48_decode_mobile_alloc(&s, data + 2, si->data[1]);
		payload_len -= 2 + data[1];
		data += 2 + data[1];
	}
	/* SI 4 Rest Octets */
	if (payload_len > 0)
		gsm48_decode_si3_rest(data, payload_len);

	si->si4 = 1;

	nmsg = gsm58_msgb_alloc(GSM58_EVENT_SYSINFO);
	if (!nmsg)
		return -ENOMEM;
	gsm322_sendmsg(ms, nmsg);

	return 0;
}

/* receive "SYSTEM INFORMATION 5" message (9.1.37) */
static int gsm_rr_rx_sysinfo5(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm48_system_information_type_5 *si = msgb_l3(msg);
	struct gsm48_sysinfo *s = ms->sysinfo;
	int payload_len = msgb_l3len(msg) - sizeof(*si);
	struct msgb *nmsg;

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of SYSTEM INFORMATION 5 message.\n");
		return -EINVAL;
	}
	/* Neighbor Cell Description */
	gsm48_decode_freq_list(s->freq, si->bcch_frequency_list,
		sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_REP_5);

	si->si5 = 1;

	nmsg = gsm58_msgb_alloc(GSM58_EVENT_SYSINFO);
	if (!nmsg)
		return -ENOMEM;
	gsm322_sendmsg(ms, nmsg);

	return 0;
}

/* receive "SYSTEM INFORMATION 5bis" message (9.1.38) */
static int gsm_rr_rx_sysinfo5bis(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm48_system_information_type_5bis *si = msgb_l3(msg);
	struct gsm48_sysinfo *s = ms->sysinfo;
	int payload_len = msgb_l3len(msg) - sizeof(*si);
	struct msgb *nmsg;

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of SYSTEM INFORMATION 5bis message.\n");
		return -EINVAL;
	}
	/* Neighbor Cell Description */
	gsm48_decode_freq_list(s->freq, si->bcch_frequency_list,
		sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_REP_5bis);

	si->si5bis = 1;

	nmsg = gsm58_msgb_alloc(GSM58_EVENT_SYSINFO);
	if (!nmsg)
		return -ENOMEM;
	gsm322_sendmsg(ms, nmsg);

	return 0;
}

/* receive "SYSTEM INFORMATION 5ter" message (9.1.39) */
static int gsm_rr_rx_sysinfo5ter(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm48_system_information_type_5ter *si = msgb_l3(msg);
	struct gsm48_sysinfo *s = ms->sysinfo;
	int payload_len = msgb_l3len(msg) - sizeof(*si);
	struct msgb *nmsg;

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of SYSTEM INFORMATION 5ter message.\n");
		return -EINVAL;
	}
	/* Neighbor Cell Description */
	gsm48_decode_freq_list(s->freq, si->bcch_frequency_list,
		sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_REP_5ter);

	si->si5ter = 1;

	nmsg = gsm58_msgb_alloc(GSM58_EVENT_SYSINFO);
	if (!nmsg)
		return -ENOMEM;
	gsm322_sendmsg(ms, nmsg);

	return 0;
}

/* receive "SYSTEM INFORMATION 6" message (9.1.39) */
static int gsm_rr_rx_sysinfo6(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm48_system_information_type_6 *si = msgb_l3(msg);
	struct gsm48_sysinfo *s = ms->sysinfo;
	int payload_len = msgb_l3len(msg) - sizeof(*si);
	struct msgb *nmsg;

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of SYSTEM INFORMATION 6 message.\n");
		return -EINVAL;
	}
	/* Cell Identity */
	s->cell_identity = ntohl(si->cell_identity);
	/* LAI */
	gsm48_decode_lai(si->lai, &s->mcc, &s->mnc, &s->lac);
	/* Cell Options (SACCH) */
	gsm48_decode_cellopt_sacch(s, si->control_channel_desc);
	/* NCC Permitted */
	s->nb_ncc_permitted = si->ncc_permitted;
	/* SI 6 Rest Octets */
	if (payload_len >= 4)
		gsm48_decode_si6_rest(si->rest_octets, payload_len);

	si->si6 = 1;

	nmsg = gsm58_msgb_alloc(GSM58_EVENT_SYSINFO);
	if (!nmsg)
		return -ENOMEM;
	gsm322_sendmsg(ms, nmsg);

	return 0;
}

/*
 * paging
 */

/* paging channel request */
static int gsm_rr_chan2cause[4] = {
	RR_EST_CAUSE_ANS_PAG_ANY,
	RR_EST_CAUSE_ANS_PAG_SDCCH,
	RR_EST_CAUSE_ANS_PAG_TCH_F,
	RR_EST_CAUSE_ANS_PAG_TCH_ANY
};

/* given LV of mobile identity is checked agains ms */
static int gsm_match_mi(struct osmocom_ms *ms, u_int8_t mi)
{
	char imsi[16];
	uint32_t tmsi;

	if (mi[0] < 1)
		return 0;
	mi_type = mi[1] & GSM_MI_TYPE_MASK;
	switch (mi_type) {
	case GSM_MI_TYPE_TMSI:
		if (mi[0] < 5)
			return;
		memcpy(&tmsi, mi+2, 4);
		if (ms->subscr.tmsi == ntohl(tmsi)
		 && ms->subscr.tmsi_valid)
			return 1;
		break;
	case GSM_MI_TYPE_IMSI:
		gsm48_mi_to_string(imsi, sizeof(imsi), mi + 1, mi[0]);
		if (!strcmp(imsi, ms->subscr.imsi))
			return 1;
		break;
	default:
		DEBUGP(DRR, "paging with unsupported MI type %d.\n", mi_type);
	}

	return 0;
}

/* paging request 1 message */
static int gsm_rr_rx_pag_req_1(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_rr_paging1 *pa = msgb_l3(msg);
	int payload_len = msgb_l3len(msg) - sizeof(*pa);
	int chan_first, chan_second;
	uint8_t mi;

	/* 3.3.1.1.2: ignore paging while establishing */
	if (rr->state != GSM_RRSTATE_IDLE)
		return 0;

	if (payload_len < 2) {
		short:
		DEBUGP(DRR, "Short read of paging request 1 message .\n");
		return -EINVAL;
	}

	/* channel needed */
	chan_first = pa->cneed1;
	chan_second = pa->cneed2;
	/* first MI */
	mi = pa->data + 1;
	if (payload_len < mi[0] + 1)
		goto short;
	if (gsm_match_mi(ms, mi) > 0)
		return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_first]);
	/* second MI */
	payload_len -= mi[0] + 1;
	mi = pa->data + mi[0] + 1;
	if (payload_len < 2)
		return 0;
	if (mi[0] != GSM48_IE_MOBILE_ID)
		return 0;
	if (payload_len < mi[1] + 2)
		goto short;
	if (gsm_match_mi(ms, mi + 1) > 0)
		return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_second]);

	return 0;
}

/* paging request 2 message */
static int gsm_rr_rx_pag_req_2(struct osmocom_ms *ms, struct gsm_msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_rr_paging2 *pa = msgb_l3(msg);
	int payload_len = msgb_l3len(msg) - sizeof(*pa);
	uint32_t tmsi;
	int chan_first, chan_second, chan_third;

	/* 3.3.1.1.2: ignore paging while establishing */
	if (rr->state != GSM_RRSTATE_IDLE)
		return 0;

	if (payload_len < 0) {
		short:
		DEBUGP(DRR, "Short read of paging request 2 message .\n");
		return -EINVAL;
	}

	/* channel needed */
	chan_first = pa->cneed1;
	chan_second = pa->cneed2;
	/* first MI */
	if (ms->subscr.tmsi == ntohl(pa->tmsi1)
	 && ms->subscr.tmsi_valid)
		return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_first]);
	/* second MI */
	if (ms->subscr.tmsi == ntohl(pa->tmsi2)
	 && ms->subscr.tmsi_valid)
		return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_second]);
	/* third MI */
	mi = pa->data;
	if (payload_len < 2)
		return 0;
	if (mi[0] != GSM48_IE_MOBILE_ID)
		return 0;
	if (payload_len < mi[1] + 2 + 1) /* must include "channel needed" */
		goto short;
	chan_third = mi[mi[1] + 2] & 0x03; /* channel needed */
	if (gsm_match_mi(ms, mi + 1) > 0)
		return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_third]);

	return 0;
}

/* paging request 3 message */
static int gsm_rr_rx_pag_req_3(struct osmocom_ms *ms, struct gsm_msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_rr_paging3 *pa = msgb_l3(msg);
	int payload_len = msgb_l3len(msg) - sizeof(*pa);
	uint32_t tmsi;
	int chan_first, chan_second, chan_third, chan_fourth;

	/* 3.3.1.1.2: ignore paging while establishing */
	if (rr->state != GSM_RRSTATE_IDLE)
		return 0;

	if (payload_len < 0) { /* must include "channel needed", part of *pa */
		short:
		DEBUGP(DRR, "Short read of paging request 3 message .\n");
		return -EINVAL;
	}

	/* channel needed */
	chan_first = pa->cneed1;
	chan_second = pa->cneed2;
	chan_third = pa->cneed3;
	chan_fourth = pa->cneed4;
	/* first MI */
	if (ms->subscr.tmsi == ntohl(pa->tmsi1)
	 && ms->subscr.tmsi_valid)
		return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_first]);
	/* second MI */
	if (ms->subscr.tmsi == ntohl(pa->tmsi2)
	 && ms->subscr.tmsi_valid)
		return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_second]);
	/* thrid MI */
	if (ms->subscr.tmsi == ntohl(pa->tmsi3)
	 && ms->subscr.tmsi_valid)
		return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_third]);
	/* fourth MI */
	if (ms->subscr.tmsi == ntohl(pa->tmsi4)
	 && ms->subscr.tmsi_valid)
		return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_fourth]);

	return 0;
}

/*
 * (immediate) assignment
 */

/* match request reference agains request history */
static int gsm_match_ra(struct osmocom_ms *ms, struct gsm48_req_ref *req)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	int i;

	for (i = 0; i < 3; i++) {
		if (rr->cr_hist[i] >= 0
		 && ref->ra == rr->cr_hist[i]) {
		 	// todo: match timeslot
			return 1;
		}
	}

	return 0;
}

/* transmit assignment complete after establishing link */
static int gsm_rr_tx_ass_cpl(struct osmocom_ms *ms, uint8_t cause)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct msgb *nmsg;
	struct gsm48_hdr *gh;
	struct gsm48_ass_cpl *ac;

	nmsg = gsm48_l3_msgb_alloc();
	if (!msg)
		return -ENOMEM;
	gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
	ac = (struct gsm48_ass_cpl *) msgb_put(nmsg, sizeof(*ac));

	gh->proto = GSM48_PDISC_RR;
	gh->msg_type = GSM48_MT_RR_ASS_COMPL;

	/* RR_CAUSE */
	ac->rr_cause = cause;

	return rslms_tx_rll_req_l3(ms, RSL_MT_DATA_REQ, chan_nr, 0, nmsg);
}

/* transmit failure to old link */
static int gsm_rr_tx_ass_fail(struct osmocom_ms *ms, uint8_t cause)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct msgb *nmsg;
	struct gsm48_hdr *gh;
	struct gsm48_ass_fail *ac;

	nmsg = gsm48_l3_msgb_alloc();
	if (!nmsg)
		return -ENOMEM;
	gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
	af = (struct gsm48_ass_fail *) msgb_put(nmsg, sizeof(*af));

	gh->proto = GSM48_PDISC_RR;
	gh->msg_type = GSM48_MT_RR_ASS_COMPL;

	/* RR_CAUSE */
	af->rr_cause = cause;

	return rslms_tx_rll_req_l3(ms, RSL_MT_DATA_REQ, chan_nr, 0, nmsg);
}

/* receive immediate assignment */
static int gsm_rr_rx_imm_ass(struct osmocom_ms *ms, struct gsm_msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_imm_ass *ia = msgb_l3(msg);
	int payload_len = msgb_l3len(msg) - sizeof(*ia);

	/* 3.3.1.1.2: ignore assignment while idle */
	if (rr->state != GSM_RRSTATE_CONN_PEND || !rr->wait_assign)
		return 0;

	if (payload_len < 1 /* mobile allocation IE must be included */
	 || *gh->data + 1 > payload_len) { /* short read of IE */
		DEBUGP(DRR, "Short read of immediate assignment message.\n");
		return -EINVAL;
	}
	if (*gh->data > 8) {
		DEBUGP(DRR, "moble allocation in immediate assignment too large.\n");
		return -EINVAL;
	}

	/* request ref */
	if (gsm_match_ra(ms, ia->req_ref)) {
		/* channel description */
todo channel structure and right management of channel IEs
		memset(&rr->chan_desc, 0, sizeof(cd));
		/* timing advance */
		rr->timing_advance = ia->timing_advance;
		/* mobile allocation */
		memcpy(rr->mobile_alloc_lv, gh->data, *gh->data + 1);
		rr->wait_assing = 0;
		return gsm_rr_dl_est(ms);
	}

	return 0;
}

/* receive immediate assignment extended */
static int gsm_rr_rx_imm_ass_ext(struct osmocom_ms *ms, struct gsm_msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_imm_ass_ext *ia = msgb_l3(msg);
	int payload_len = msgb_l3len(msg) - sizeof(*ia);

	/* 3.3.1.1.2: ignore assignment while idle */
	if (rr->state != GSM_RRSTATE_CONN_PEND || !rr->wait_assign)
		return 0;

	if (payload_len < 1 /* mobile allocation IE must be included */
	 || *gh->data + 1 > payload_len) { /* short read of IE */
		DEBUGP(DRR, "Short read of immediate assignment extended message.\n");
		return -EINVAL;
	}
	if (*gh->data > 4) {
		DEBUGP(DRR, "moble allocation in immediate assignment extended too large.\n");
		return -EINVAL;
	}

	/* request ref 1 */
	if (gsm_match_ra(ms, ia->req_ref1)) {
		/* channel description */
		memset(&rr->chan_desc, 0, sizeof(cd));
		memcpy(rr->chan_desc.chan_desc, ia->chan_desc1, 3);
		/* timing advance */
		rr->timing_advance = ia->timing_advance1;
		/* mobile allocation */
		memcpy(rr->mobile_alloc_lv, gh->data, *gh->data + 1);
		rr->wait_assing = 0;
		return gsm_rr_dl_est(ms);
	}
	/* request ref 1 */
	if (gsm_match_ra(ms, ia->req_ref2)) {
		/* channel description */
		memset(&rr->chan_desc, 0, sizeof(cd));
		memcpy(rr->chan_desc.chan_desc, ia->chan_desc2, 3);
		/* timing advance */
		rr->timing_advance = ia->timing_advance2;
		/* mobile allocation */
		memcpy(rr->mobile_alloc_lv, gh->data, *gh->data + 1);
		rr->wait_assing = 0;
		return gsm_rr_dl_est(ms);
	}

	return 0;
}

/* receive immediate assignment reject */
static int gsm_rr_rx_imm_ass_rej(struct osmocom_ms *ms, struct gsm_msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_imm_ass_rej *ia = msgb_l3(msg);
	int payload_len = msgb_l3len(msg) - sizeof(*ia);
	int i;
	struct gsm48_req_ref *req_ref;
	uint8_t t3122_value;

	/* 3.3.1.1.2: ignore assignment while idle */
	if (rr->state != GSM_RRSTATE_CONN_PEND || !rr->wait_assign)
		return 0;

	if (payload_len < 0) {
		short:
		DEBUGP(DRR, "Short read of immediate assignment reject message.\n");
		return -EINVAL;
	}

	for (i = 0; i < 4; i++) {
		/* request reference */
		req_ref = (struct gsm48_req_ref *)(((uint8_t *)&ia->req_ref1) + i * 4);
		if (gsm_match_ra(ms, req_ref)) {
			/* wait indication */
			t3122_value = ((uint8_t *)&ia->wait_ind1) + i * 4;
			if (t3122_value)
				start_rr_t3122(rr, t3122_value, 0);
			/* start timer 3126 if not already */
			if (!timer_pending(rr->t3126))
				start_rr_t3126(rr, GSM_T3126_MS);
			/* stop assignmnet requests */
			rr->n_chan_req = 0;

			/* wait until timer 3126 expires, then release
			 * or wait for channel assignment */
			return 0;
		}
	}

	return 0;
}

/* receive additional assignment */
static int gsm_rr_rx_add_ass(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_hdr *gh = msgb_l3(msg);
	struct gsm48_add_ass *aa = (struct gsm48_add_ass *)gh->data;
	int payload_len = msgb_l3len(msg) - sizeof(*gh) - sizeof(*aa);

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of ADDITIONAL ASSIGNMENT message.\n");
		return gsm_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
	}
	tlv_parse(&tp, &rsl_att_tlvdef, aa->data, payload_len, 0, 0);

	return gsm_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
}

/*
 * measturement reports
 */

static int gsm_rr_tx_meas_rep(struct osmocom_ms *ms)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm_rr_meas *meas = &rr->meas;
	struct msgb *nmsg;
	struct gsm48_hdr *gh;
	struct gsm48_meas_res *mr;

	nmsg = gsm48_l3_msgb_alloc();
	if (!nmsg)
		return -ENOMEM;
	gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
	mr = (struct gsm48_meas_res *) msgb_put(nmsg, sizeof(*mr));

	gh->proto = GSM48_PDISC_RR;
	gh->msg_type = GSM48_MT_RR_MEAS_RES;

	/* measurement results */
	mr->rxlev_full = meas->rxlev_full;
	mr->rxlev_sub = meas->rxlev_sub;
	mr->rxqual_full = meas->rxqual_full;
	mr->rxqual_sub = meas->rxqual_sub;
	mr->dtx = meas->dtx;
	mr->ba = meas->ba;
	mr->meas_valid = meas->meas_valid;
	if (meas->ncell_na) {
		/* no results for serving cells */
		mr->no_n_hi = 1;
		mr->no_n_lo = 3;
	} else {
		mr->no_n_hi = meas->count >> 2;
		mr->no_n_lo = meas->count & 3;
	}
	rxlev_nc1 = meas->rxlev_nc[0];
	rxlev_nc2_hi = meas->rxlev_nc[1] >> 1;
	rxlev_nc2_lo = meas->rxlev_nc[1] & 1;
	rxlev_nc3_hi = meas->rxlev_nc[2] >> 2;
	rxlev_nc3_lo = meas->rxlev_nc[2] & 3;
	rxlev_nc4_hi = meas->rxlev_nc[3] >> 3;
	rxlev_nc4_lo = meas->rxlev_nc[3] & 7;
	rxlev_nc5_hi = meas->rxlev_nc[4] >> 4;
	rxlev_nc5_lo = meas->rxlev_nc[4] & 15;
	rxlev_nc6_hi = meas->rxlev_nc[5] >> 5;
	rxlev_nc6_lo = meas->rxlev_nc[5] & 31;
	bsic_nc1_hi = meas->bsic_nc[0] >> 3;
	bsic_nc1_lo = meas->bsic_nc[0] & 7;
	bsic_nc2_hi = meas->bsic_nc[1] >> 4;
	bsic_nc2_lo = meas->bsic_nc[1] & 15;
	bsic_nc3_hi = meas->bsic_nc[2] >> 5;
	bsic_nc3_lo = meas->bsic_nc[2] & 31;
	bsic_nc4 = meas->bsic_nc[3];
	bsic_nc5 = meas->bsic_nc[4];
	bsic_nc6 = meas->bsic_nc[5];
	bcch_f_nc1 = meas->bcch_f_nc[0];
	bcch_f_nc2 = meas->bcch_f_nc[1];
	bcch_f_nc3 = meas->bcch_f_nc[2];
	bcch_f_nc4 = meas->bcch_f_nc[3];
	bcch_f_nc5_hi = meas->bcch_f_nc[4] >> 1;
	bcch_f_nc5_lo = meas->bcch_f_nc[4] & 1;
	bcch_f_nc6_hi = meas->bcch_f_nc[5] >> 2;
	bcch_f_nc6_lo = meas->bcch_f_nc[5] & 3;

	return rslms_tx_rll_req_l3(ms, RSL_MT_, chan_nr, 0, nmsg);
}

/*
 * link establishment and release
 */

/* activate link and send establish request */
static int gsm_rr_dl_est(struct osmocom_ms *ms)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm_subscriber *subcr = ms->subscr;
	struct msgb *nmsg;
	struct gsm48_hdr *gh;
	struct gsm48_pag_rsp *pa;

	/* 3.3.1.1.3.1 */
	stop_rr_t3126(rr);

	/* flush pending RACH requests */
	rr->n_chan_req = 0; // just to be safe
	nmsg = msgb_alloc_headroom(20, 16, "RAND_FLUSH");
	if (!nmsg)
		return -ENOMEM;
	rslms_tx_rll_req_l3(ms, RSL_MT_RAND_ACC_FLSH, chan_nr, 0, msg);

	/* send DL_EST_REQ */
	if (rr->l3_est_msg) {
		/* use queued message */
		nmsg = rr->l3_est_msg;
		rr->l3_est_msg = 0;
	} else {
		uint8_t mi[11];

		/* create paging response */
		nmsg = gsm48_l3_msgb_alloc();
		if (!nmsg)
			return -ENOMEM;
		gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
		pr = (struct gsm48_pag_rsp *) msgb_put(nmsg, sizeof(*pr));
		/* key sequence */
		if (subscr->key_valid)
			pr->key_seq = subscr->key_seq;
		else
			pr->key_seq = 7;
		/* classmark 2 */
		cc->cm_len = sizeof(cm->cm2);
		gsm_rr_enc_cm2(ms, &cc->cm2)
		/* mobile identity */
		if (ms->subscr.tmsi_valid) {
			gsm48_generate_mid_from_tmsi(mi, subscr->tmsi);
		} else if (subscr->imsi[0])
			gsm48_generate_mid_from_imsi(mi, subscr->imsi);
		else {
			mi[1] = 1;
			mi[2] = 0xf0 | GSM_MI_TYPE_NONE;
		}
		msgb_put(msg, 1 + mi[1]);
		memcpy(cm->data, mi + 1, 1 + mi[1]);
	}

	/* activate channel */
	tx_ph_dm_est_req(ms, arfcn, chan_nr);

	/* start establishmnet */
	return rslms_tx_rll_req_l3(ms, RSL_MT_EST_REQ, chan_nr, 0, nmsg);
}

/* the link is established */
static int gsm_rr_estab_cnf(struct osmocom_ms *ms, struct msgb *msg)
{
	struct msgb *nmsg;
	struct gsm_mm_hdr *nmmh;

	/* if MM has releases before confirm, we start release */
	if (rr->state == GSM_RRSTATE_IDLE) {
		/* release message */
		nmsg = gsm48_l3_msgb_alloc();
		if (!nmsg)
			return -ENOMEM;
		/* start release */
		return rslms_tx_rll_req_l3(ms, RSL_MT_REL_REQ, 0, 0, nmsg);
	}

	/* 3.3.1.1.4 */
	new_rr_state(rr, GSM_RRSTATE_DEDICATED);

	/* send confirm to upper layer */
	nmsg = gsm48_mm_msgb_alloc();
	if (!nmsg)
		return -ENOMEM;
	nmmh = (struct gsm_mm_hdr *)nmsg->data;
	nmmh->msg_type = (rr->rr_est_req) ? RR_EST_CNF : RR_EST_IND;
	return gsm48_mm_upmsg(ms, nmsg);
}

/* the link is released */
static int gsm_rr_rel_cnf(struct osmocom_ms *ms, struct gsm_dl *dlmsg)
{
	/* deactivate channel */
	tx_ph_dm_rel_req(ms, arfcn, rr->chan_desc.chan_desc.chan_nr);

	/* do nothing, because we aleady IDLE
	 * or we received the rel cnf of the last connection
	 * while already requesting a new one (CONN PEND)
	 */

	return 0;
}

/*
 * radio ressource requests 
 */

/* establish request for dedicated mode */
static int gsm_rr_est_req(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm_mm_hdr *mmh = msgb->data;
	struct gsm48_hdr *gh = msgb_l3(msg);

	/* 3.3.1.1.3.2 */
	if (timer_pending(rr->t3122)) {
		if (rrmsg->cause != RR_EST_CAUSE_EMERGENCY) {
			struct msgb *nmsg;
			struct gsm_mm_hdr *nmmh;

			nmsg = gsm48_mm_msgb_alloc();
			if (!nmsg)
				return -ENOMEM;
			nmmh = (struct gsm_mm_hdr *)nmsg->data;
			nmmh->msg_type RR_REL_IND;
			nmmh->cause = GSM_MM_CAUSE_T3122_PEND;
			return gsm48_mm_upmsg(ms, nmsg);
		} else
			stop_rr_t3122(rr);
	}

	/* 3.3.1.1.1 */
	if (rrmsg->cause != RR_EST_CAUSE_EMERGENCY) {
		if (!(ms->access_class & ms->si.access_class)) {
			reject:
			if (!ms->opt.access_class_override) {
				struct msgb *nmsg;
				struct gsm_mm_hdr *nmmh;

				nmsg = gsm48_mm_msgb_alloc();
				if (!nmsg)
					return -ENOMEM;
				nmmh = (struct gsm_mm_hdr *)nmsg->data;
				nmmh->msg_type RR_REL_IND;
				nmmh->cause = GSM_MM_CAUSE_NOT_AUTHORIZED;
				return gsm48_mm_upmsg(ms, nmsg);
			}
		}
	} else {
		if (!(ms->access_class & ms->si.access_class)
		 && !ms->si.emergency)
		 	goto reject;
	}

	/* requested by RR */
	rr->rr_est_req = 1;

	/* clone and store REQUEST message */
	if (!gh) {
		printf("Error, missing l3 message\n");
		return -EINVAL;
	}
	rr->rr_est_msg = msgb_alloc_headroom(256, 16, "EST_REQ");
	if (!rr->rr_est_msg)
		return -ENOMEM;
	memcpy(msgb_put(rr_est_msg, msgb_l3len(msg)),
		msgb_l3(msg), msgb_l3len(msg));

	/* request channel */
	return gsm_rr_tx_chan_req(ms, mmh->cause);
}

/* send all queued messages down to layer 2 */
static int gsm_rr_dequeue_down(struct osmocom_ms *ms)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct msgb *msg;

	while((msg = msgb_dequeue(&rr->downqueue))) {
		rslms_tx_rll_req_l3(ms, RSL_MT_DATA_REQ, chan_nr, 0, msg);
	}

	return 0;
}

/* 3.4.2 transfer data in dedicated mode */
static int gsm_rr_data_req(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;

	if (rr->state != GSM_RRSTATE_DEDICATED) {
		msgb_free(msg)
		return -EINVAL;
	}
	
	/* pull header */
	msgb_pull(msg, sizeof(struct gsm_mm_hdr));

	/* queue message, during handover or assignment procedure */
	if (rr->hando_susp_state || rr->assign_susp_state) {
		msgb_enqueue(&rr->downqueue, msg);
		return 0;
	}

	/* forward message */
	return rslms_tx_rll_req_l3(ms, RSL_MT_DATA_REQ, chan_nr, 0, msg);
}

/*
 * data indications from data link
 */

/* 3.4.2 data from layer 2 to RR and upper layer*/
static int gsm_rr_data_ind(struct osmocom_ms *ms, struct msbg *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_hdr *gh = msgb_l3(msg);
	int payload_len = msgb_l3len(msg) - sizeof(*ia);
	u_int8_t pdisc = gh->proto_discr & 0x0f;

	if (pdisc == GSM48_PDISC_RR) {
		int rc = -EINVAL;

		switch(gh->msg_type) {
		case GSM48_MT_RR_ADD_ASS:
			rc = gsm_rr_rx_add_ass(ms, msg);
			break;
		case GSM48_MT_RR_ASS_CMD:
			rc = gsm_rr_rx_ass_cmd(ms, msg);
			break;
		case GSM48_MT_RR_CIP_MODE_CMD:
			rc = gsm_rr_rx_cip_mode_cmd(ms, msg);
			break;
		case GSM48_MT_RR_CLSM_ENQ:
			rc = gsm_rr_rx_cm_enq(ms, msg);
			break;
		case GSM48_MT_RR_HANDO_CMD:
			rc = gsm_rr_rx_hando_cmd(ms, msg);
			break;
		case GSM48_MT_RR_FREQ_REDEF:
			rc = gsm_rr_rx_freq_redef(ms, msg);
			break;
		default:
			DEBUGP(DRR, "Message type 0x%02x unknown.\n", gh->msg_type);
		}

		free_msgb(msg);
		return rc;
	}

	/* pull off RSL header up to L3 message */
	msgb_pull(msg, msgb_l3(msg) - msg->data);

	/* push RR header */
	msgb_push(msg, sizeof(struct gsm48_rr_hdr));
	rrh = (struct gsm48_rr_hdr *)msg->data;
	rrh->msg_type = GSM48_RR_DATA_IND;

	return gsm48_mm_upmsg(ms, msg);
}

/* unit data from layer 2 to RR layer */
** MUST BE RECEIVED FROM QUEUE
static int gsm_rr_unit_data_ind(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm48_hdr *gh = msgb_l3(msg);

	switch (gh->msg_type) {
	case GSM48_MT_RR_SYSINFO_1:
		return gsm_rr_rx_sysinfo1(ms, dlmsg->msg);
	case GSM48_MT_RR_SYSINFO_2:
		return gsm_rr_rx_sysinfo2(ms, dlmsg->msg);
	case GSM48_MT_RR_SYSINFO_2bis:
		return gsm_rr_rx_sysinfo2bis(ms, dlmsg->msg);
	case GSM48_MT_RR_SYSINFO_2ter:
		return gsm_rr_rx_sysinfo2ter(ms, dlmsg->msg);
	case GSM48_MT_RR_SYSINFO_3:
		return gsm_rr_rx_sysinfo3(ms, dlmsg->msg);
	case GSM48_MT_RR_SYSINFO_4:
		return gsm_rr_rx_sysinfo4(ms, dlmsg->msg);
	case GSM48_MT_RR_SYSINFO_5:
		return gsm_rr_rx_sysinfo5(ms, dlmsg->msg);
	case GSM48_MT_RR_SYSINFO_5bis:
		return gsm_rr_rx_sysinfo5bis(ms, dlmsg->msg);
	case GSM48_MT_RR_SYSINFO_5ter:
		return gsm_rr_rx_sysinfo5ter(ms, dlmsg->msg);
	case GSM48_MT_RR_SYSINFO_6:
		return gsm_rr_rx_sysinfo6(ms, dlmsg->msg);
	case GSM48_MT_RR_PAG_REQ_1:
		return gsm_rr_rx_pag_req_1(ms, dlmsg->msg);
	case GSM48_MT_RR_PAG_REQ_2:
		return gsm_rr_rx_pag_req_2(ms, dlmsg->msg);
	case GSM48_MT_RR_PAG_REQ_3:
		return gsm_rr_rx_pag_req_3(ms, dlmsg->msg);
	case GSM48_MT_RR_IMM_ASS:
		return gsm_rr_rx_imm_ass(ms, dlmsg->msg);
	case GSM48_MT_RR_IMM_ASS_EXT:
		return gsm_rr_rx_imm_ass_ext(ms, dlmsg->msg);
	case GSM48_MT_RR_IMM_ASS_REJ:
		return gsm_rr_rx_imm_ass_rej(ms, dlmsg->msg);
	default:
		DEBUGP(DRR, "Message type 0x%02x unknown.\n", gh->msg_type);
		return -EINVAL;
	}
}



the process above is complete
------------------------------------------------------------------------------
incomplete



















todo:

add support structure
initialize support structure

queue messages (rslms_data_req) if channel changes

flush rach msg in all cases: during sending, after its done, and when aborted
stop timers on abort
debugging. (wenn dies todo erledigt ist, bitte in den anderen code moven)
wird beim abbruch immer der gepufferte cm-service-request entfernt, auch beim verschicken?:
measurement reports
todo rr_sync_ind when receiving ciph, re ass, channel mode modify

todo change procedures, release procedure

during procedures, like "channel assignment" or "handover", rr requests must be queued
they must be dequeued when complete
they queue must be flushed when rr fails

#include <osmocore/protocol/gsm_04_08.h>
#include <osmocore/msgb.h>
#include <osmocore/utils.h>
#include <osmocore/gsm48.h>

static int gsm_rr_abort_req(struct osmocom_ms *ms, struct gsm_rr *rrmsg)
{
	struct gsm_rrlayer *rr = ms->rrlayer;
	stop_rr_t3126(rr);
	if (rr->state == GSM_RRSTATE_DEDICATED) {
		struct gsm_dl dlmsg;

		memset(&dlmsg, 0, sizeof(dlmsg));
		return rslms_tx_rll_req_l3(ms, RSL_MT_REL_REQ, chan_nr, 0, nmsg);
	}
	new_rr_state(rr, GSM_RRSTATE_IDLE);
}

static int gsm_rr_act_req(struct osmocom_ms *ms, struct gsm_rr *rrmsg)
{
}

/* state trasitions for RR-SAP messages from up */
static struct rrdownstate {
	uint32_t	states;
	int		type;
	int		(*rout) (struct osmocom_ms *ms, struct gsm_dl *rrmsg);
} rrdownstatelist[] = {
	{SBIT(GSM_RRSTATE_IDLE), /* 3.3.1.1 */
	 GSM48_RR_EST_REQ, gsm_rr_est_req},
	{SBIT(GSM_RRSTATE_DEDICATED), /* 3.4.2 */
	 GSM48_RR_DATA_REQ, gsm_rr_data_req},
	{SBIT(GSM_RRSTATE_CONN_PEND) | SBIT(GSM_RRSTATE_DEDICATED),
	 GSM48_RR_ABORT_REQ, gsm_rr_abort_req},
	{SBIT(GSM_RRSTATE_DEDICATED),
	 GSM48_RR_ACT_REQ, gsm_rr_act_req},
};

#define RRDOWNSLLEN \
	(sizeof(rrdownstatelist) / sizeof(struct rrdownstate))

static int gsm48_rr_downmsg(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct gsm_rr_hdr *rrh = msgb->data;
	int msg_type = rrh->msg_type;

	DEBUGP(DRR, "(ms %s) Message '%s' received in state %s\n", ms->name,
		gsm48_rr_msg_names(msg_type), gsm48_rr_state_names[rr->state]);

	/* find function for current state and message */
	for (i = 0; i < RRDOWNSLLEN; i++)
		if ((msg_type == rrdownstatelist[i].type)
		 && ((1 << rr->state) & rrdownstatelist[i].states))
			break;
	if (i == RRDOWNSLLEN) {
		DEBUGP(DRR, "Message unhandled at this state.\n");
		free_msgb(msg);
		return 0;
	}

	rc = rrdownstatelist[i].rout(ms, dlmsg);

	/* free msgb uless it is forwarded */
	if (rrdownstatelist[i].rout != gsm_rr_data_req)
		free_msgb(msg);

	return rc;
}

remove the rest
	/* channel description */
	rsl_dec_chan_nr(aa->chan_desc.chan_nr, &ch_type, &ch_subch, &ch_ts);
	h = aa->chan_desc.h0.h;
	if (h)
		rsl_dec_chan_h1(&aa->chan_desc, &tsc, &maio, &hsn);
	else
		rsl_dec_chan_h0(&aa->chan_desc, &tsc, &arfcn);
	/* mobile allocation */
	if (h) {
		if (!TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ALLOC))
			return gsm_rr_tx_rr_status(ms, ...);
		gsm48_decode_mobile_alloc(&ma,
			TLVP_VAL(&tp, GSM48_IE_MOBILE_ALLOC)-1);
	}
	/* starting time */
	if (TLVP_PRESENT(&tp, GSM48_IE_START_TIME)) {
		gsm48_decode_start_time(&frame,
			TLVP_VAL(&tp, GSM48_IE_START_TIME)-1);
	}

}

/* memcopy of LV of given IE from tlv_parsed structure */
static int tlv_copy(void *dest, int dest_len, struct tlv_parsed *tp, uint8_t ie)
{
	uint8_t *lv = dest;
	uint8_t len;

	if (dest_len < 1)
		return -EINVAL;
	lv[0] = 0;

	if (!TLVP_PRESENT(tp, ie))
		return 0;

	len = TLVP_LEN(tp, ie);
	if (len < 1)
		return 0;
	if (len + 1 > dest_len)
		return -ENOMEM;

	memcpy(dest, TLVP_VAL(tp, ie) - 1, len + 1);
	return 0;
}

static int gsm_rr_rx_ass_cmd(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = ms->rrlayer;
	struct gsm48_hdr *gh = msgb_l3(msg);
	struct gsm48_ass_cmd *ac = (struct gsm48_ass_cmd *)gh->data;
	int payload_len = msgb_l3len(msg) - sizeof(*gh) - sizeof(*ac);
	struct tlv_parsed tp;
	struct gsm_rr_chan_desc cd;
	struct msgb *nmsg;

	memset(&cd, 0, sizeof(cd));

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of ASSIGNMENT COMMAND message.\n");
		return gsm_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
	}
	tlv_parse(&tp, &rsl_att_tlvdef, ac->data, payload_len, 0, 0);

	/* channel description */
	memcpy(&cd.chan_desc, &ac->chan_desc, sizeof(chan_desc));
	/* power command */
	cd.power_command = ac->power_command;
	/* frequency list, after timer */
	tlv_copy(&cd.fl, sizeof(fl_after), &tp, GSM48_IE_FRQLIST_AFTER);
	/* cell channel description */
	tlv_copy(&cd.ccd, sizeof(ccd), &tp, GSM48_IE_CELL_CH_DESC);
	/* multislot allocation */
	tlv_copy(&cd.multia, sizeof(ma), &tp, GSM48_IE_MSLOT_DESC);
	/* channel mode */
	tlv_copy(&cd.chanmode, sizeof(chanmode), &tp, GSM48_IE_CHANMODE_1);
	/* mobile allocation, after time */
	tlv_copy(&cd.moba_after, sizeof(moba_after), &tp, GSM48_IE_MOB_AL_AFTER);
	/* starting time */
	tlv_copy(&cd.start, sizeof(start), &tp, GSM_IE_START_TIME);
	/* frequency list, before time */
	tlv_copy(&cd.fl_before, sizeof(fl_before), &tp, GSM48_IE_FRQLIST_BEFORE);
	/* channel description, before time */
	tlv_copy(&cd.chan_desc_before, sizeof(cd_before), &tp, GSM48_IE_CHDES_1_BEFORE);
	/* frequency channel sequence, before time */
	tlv_copy(&cd.fcs_before, sizeof(fcs_before), &tp, GSM48_IE_FRQSEQ_BEFORE);
	/* mobile allocation, before time */
	tlv_copy(&cd.moba_before, sizeof(moba_before), &tp, GSM48_IE_MOB_AL_BEFORE);
	/* cipher mode setting */
	if (TLVP_PRESENT(&tp, GSM48_IE_CIP_MODE_SET))
		cd.cipher = *TLVP_VAL(&tp, GSM48_IE_CIP_MODE_SET);
	else
		cd.cipher = 0;

	if (no CA) {
		DEBUGP(DRR, "No current cell allocation available.\n");
		return gsm_rr_tx_rr_status(ms, GSM48_RR_CAUSE_NO_CELL_ALLOC_A);
	}
	
	if (not supported) {
		DEBUGP(DRR, "New channel is not supported.\n");
		return gsm_rr_tx_rr_status(ms, RR_CAUSE_CHAN_MODE_UNACCEPT);
	}

	if (freq not supported) {
		DEBUGP(DRR, "New frequency is not supported.\n");
		return gsm_rr_tx_rr_status(ms, RR_CAUSE_FREQ_NOT_IMPLEMENTED);
	}

	/* store current channel descriptions, to return in case of failure */
	memcpy(&rr->chan_last, &rr->chan_desc, sizeof(*cd));
	/* copy new description */
	memcpy(&rr->chan_desc, cd, sizeof(cd));

	/* start suspension of current link */
	nmsg = gsm48_l3_msgb_alloc();
	if (!nmsg)
		return -ENOMEM;
	rslms_tx_rll_req_l3(ms, RSL_MT_SUSP_REQ, chan_nr, 0, msg);

	/* change into special assignment suspension state */
	rr->assign_susp_state = 1;
	rr->resume_last_state = 0;

	return 0;
}

/* decode "Cell Description" (10.5.2.2) */
static int gsm48_decode_cell_desc(struct gsm48_cell_desc *cd, uint16_t *arfcn, uint8_t *ncc uint8_t *bcc)
{
	*arfcn = (cd->bcch_hi << 8) + cd->bcch_lo;
	*ncc = cd->ncc;
	*bcc = cd->bcc;
}

/* decode "Power Command" (10.5.2.28) and (10.5.2.28a) */
static int gsm48_decode_power_cmd_acc(struct gsm48_power_cmd *pc, uint8_t *power_level uint8_t *atc)
{
	*power_level = pc->power_level;
	if (atc) /* only in case of 10.5.2.28a */
		*atc = pc->atc;
}

/* decode "Synchronization Indication" (10.5.2.39) */
static int gsm48_decode_power_cmd_acc(struct gsm_rrlayer *rr, struct gsm_rr_sync_ind *si)
{
	rr->ho_sync_ind = si->si;
	rr->ho_rot = si->rot;
	rr->ho_nci = si->nci;
}

/* receiving HANDOVER COMMAND message (9.1.15) */
static int gsm_rr_rx_hando_cmd(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = ms->rrlayer;
	struct gsm48_hdr *gh = msgb_l3(msg);
	struct gsm48_ho_cmd *ho = (struct gsm48_ho_cmd *)gh->data;
	int payload_len = msgb_l3len(msg) - sizeof(*gh) - sizeof(*ho);
	struct tlv_parsed tp;
	struct gsm_rr_chan_desc cd;
	struct msgb *nmsg;

	memset(&cd, 0, sizeof(cd));

	if (payload_len < 0) {
		DEBUGP(DRR, "Short read of HANDOVER COMMAND message.\n");
		return gsm_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
	}
	tlv_parse(&tp, &rsl_att_tlvdef, ho->data, payload_len, 0, 0);

	/* decode Cell Description */
	gsm_decode_cell_desc(&ho->cell_desc, &cd.bcch_arfcn, &cd.ncc, &cd.bcc);
	/* Channel Description */
	memcpy(&rr->chan_desc.chan_desc, ho->chan_desc, 3);
	/* Handover Reference */
	rr->hando_ref = ho->ho_ref;
	/* Power Command and access type */
	gsm_decode_power_cmd_acc((struct gsm48_power_cmd *)&ho->power_command,
		&cd.power_level, cd.atc);
	/* Synchronization Indication */
	if (TLVP_PRESENT(&tp, GSM48_IE_SYNC_IND))
		gsm48_decode_sync_ind(rr,
			TLVP_VAL(&tp, GSM48_IE_MOBILE_ALLOC)-1, &cd);
	/* Frequency Sort List */
	if (TLVP_PRESENT(&tp, GSM48_IE_FREQ_SHORT_LIST))
		gsm48_decode_freq_list(s->freq,
			TLVP_VAL(&tp, GSM48_IE_MOBILE_ALLOC),
			*(TLVP_VAL(&tp, GSM48_IE_MOBILE_ALLOC)-1),
				0xce, FREQ_TYPE_SERV);


today: more IE parsing

	/* store current channel descriptions, to return in case of failure */
	memcpy(&rr->chan_last, &rr->chan_desc, sizeof(*cd));
	/* copy new description */
	memcpy(&rr->chan_desc, cd, sizeof(cd));

	/* start suspension of current link */
	nmsg = gsm48_l3_msgb_alloc();
	if (!nmsg)
		return -ENOMEM;
	rslms_tx_rll_req_l3(ms, RSL_MT_SUSP_REQ, chan_nr, 0, msg);

	/* change into special handover suspension state */
	rr->hando_susp_state = 1;
	rr->resume_last_state = 0;

	return 0;
}

static int gsm_rr_rx_hando_cmd(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = ms->rrlayer;
	struct gsm48_hdr *gh = msgb_l3(msg);
	int payload_len = msgb_l3len(msg) - sizeof(*gh);

static int gsm_rr_estab_cnf_dedicated(struct osmocom_ms *ms, struct msgb *msg)
{
	if (rr->hando_susp_state || rr->assign_susp_state) {
		if (rr->resume_last_state) {
			rr->resume_last_state = 0;
			gsm_rr_tx_ass_cpl(ms, GSM48_RR_CAUSE_NORMAL);
		} else {
			gsm_rr_tx_ass_fail(ms, RR_CAUSE_PROTO_ERR_UNSPEC);
		}
		/* transmit queued frames during ho / ass transition */
		gsm_rr_dequeue_down(ms);
	}

	return 0;
}

static int gsm_rr_connect_cnf(struct osmocom_ms *ms, struct msgbl *msg)
{
}

static int gsm_rr_rel_ind(struct osmocom_ms *ms, struct msgb *msg)
{
}

static int gsm_rr_rel_cnf_dedicated(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = ms->rrlayer;
	struct msgb *nmsg;

	if (rr->hando_susp_state || rr->assign_susp_state) {
		struct msgb *msg;

		/* change radio to new channel */
		tx_ph_dm_est_req(ms, arfcn, rr->chan_desc.chan_desc.chan_nr);

		nmsg = gsm48_l3_msgb_alloc();
		if (!nmsg)
			return -ENOMEM;
		/* send DL-ESTABLISH REQUEST */
		rslms_tx_rll_req_l3(ms, RSL_MT_EST_REQ, chan_nr, 0, nmsg);

	}
	if (rr->hando_susp_state) {
		gsm_rr_tx_hando_access(ms);
		rr->hando_acc_left = 3;
	}
	return 0;
}

static int gsm_rr_mdl_error_ind(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = ms->rrlayer;
	struct msgb *nmsg;
	struct gsm_mm_hdr *nmmh;

	if (rr->hando_susp_state || rr->assign_susp_state) {
		if (!rr->resume_last_state) {
			rr->resume_last_state = 1;

			/* get old channel description */
			memcpy(&rr->chan_desc, &rr->chan_last, sizeof(*cd));

			/* change radio to old channel */
			tx_ph_dm_est_req(ms, arfcn, rr->chan_desc.chan_desc.chan_nr);

			/* re-establish old link */
			nmsg = gsm48_l3_msgb_alloc();
			if (!nmsg)
				return -ENOMEM;
			return rslms_tx_rll_req_l3(ms, RSL_MT_EST_REQ, chan_nr, 0, nmsg);
		}
		rr->resume_last_state = 0;
	}

	/* deactivate channel */
	tx_ph_dm_rel_req(ms, arfcn, rr->chan_desc.chan_desc.chan_nr);

	/* send abort ind to upper layer */
	nmsg = gsm48_mm_msgb_alloc();

	if (!msg)
		return -ENOMEM;
	nmmh = (struct gsm_mm_hdr *)nmsg->data;
	nmmh->msg_type = RR_ABORT_IND;
	nmmh->cause = GSM_MM_CAUSE_LINK_FAILURE;
	return gsm48_mm_upmsg(ms, msg);
}

/* state trasitions for link layer messages (lower layer) */
static struct dldatastate {
	uint32_t	states;
	int		type;
	int		(*rout) (struct osmocom_ms *ms, struct gsm_dl *dlmsg);
} dldatastatelist[] = {
	{SBIT(GSM_RRSTATE_IDLE) | SBIT(GSM_RRSTATE_CONN_PEND),
	 RSL_MT_UNIT_DATA_IND, gsm_rr_unit_data_ind},
	{SBIT(GSM_RRSTATE_DEDICATED), /* 3.4.2 */
	 RSL_MT_DATA_IND, gsm_rr_data_ind},
	{SBIT(GSM_RRSTATE_IDLE) | SBIT(GSM_RRSTATE_CONN_PEND),
	 RSL_MT_ESTABLISH_CNF, gsm_rr_estab_cnf},
	{SBIT(GSM_RRSTATE_DEDICATED),
	 RSL_MT_ESTABLISH_CNF, gsm_rr_estab_cnf_dedicated},
	{SBIT(GSM_RRSTATE),
	 RSL_MT_CONNECT_CNF, gsm_rr_connect_cnf},
	{SBIT(GSM_RRSTATE),
	 RSL_MT_RELEASE_IND, gsm_rr_rel_ind},
	{SBIT(GSM_RRSTATE_IDLE) | SBIT(GSM_RRSTATE_CONN_PENDING),
	 RSL_MT_RELEASE_CNF, gsm_rr_rel_cnf},
	{SBIT(GSM_RRSTATE_DEDICATED),
	 RSL_MT_RELEASE_CNF, gsm_rr_rel_cnf_dedicated},
	{SBIT(GSM_RRSTATE_CONN_PEND), /* 3.3.1.1.2 */
	 RSL_MT_RANDOM_ACCESS_CNF, gsm_rr_rand_acc_cnf},
	{SBIT(GSM_RRSTATE_DEDICATED),
	 RSL_MT_RANDOM_ACCESS_CNF, gsm_rr_rand_acc_cnf_dedicated},
	{SBIT(GSM_RRSTATE),
	 RSL_MT_MDL_ERROR_IND, gsm_rr_mdl_error_ind},
};

#define DLDATASLLEN \
	(sizeof(dldatastatelist) / sizeof(struct dldatastate))

static int gsm48_rcv_rsl(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct abis_rsl_rll_hdr *rlh = msgb_l2(msg);
	int msg_type = rlh->msg_type;

	DEBUGP(DRR, "(ms %s) Received '%s' from RSL in state %s\n", ms->name,
		gsm48_rsl_msg_name(msg_type), rr_state_names[rr->state]);

	/* find function for current state and message */
	for (i = 0; i < DLDATASLLEN; i++)
		if ((msg_type == dldatastatelist[i].type)
		 && ((1 << rr->state) & dldatastatelist[i].states))
			break;
	if (i == DLDATASLLEN) {
		DEBUGP(DRR, "Message unhandled at this state.\n");
		free_msgb(msg);
		return 0;
	}

	rc = dldatastatelist[i].rout(ms, dlmsg);

	/* free msgb uless it is forwarded */
	if (dldatastatelist[i].rout != gsm_rr_data_ind)
		free_msgb(msg);

	return rc;
}

static void timeout_rr_t3124(void *arg)
{
	struct gsm_rrlayer *rr = arg;
	struct msgb *nmsg;

	/* stop sending more access bursts when timer expired */
	hando_acc_left = 0;

	/* get old channel description */
	memcpy(&rr->chan_desc, &rr->chan_last, sizeof(*cd));

	/* change radio to old channel */
	tx_ph_dm_est_req(ms, arfcn, rr->chan_desc.chan_desc.chan_nr);

	/* re-establish old link */
	nmsg = gsm48_l3_msgb_alloc();
	if (!nmsg)
		return -ENOMEM;
	return rslms_tx_rll_req_l3(ms, RSL_MT_EST_REQ, chan_nr, 0, nmsg);

	todo
}

int gsm48_init_rr(struct osmocom_ms *ms)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;

	memset(rr, 0, sizeof(*rr));
	rr->ms = ms;

	INIT_LLIST_HEAD(&rr->rr_upqueue);
	INIT_LLIST_HEAD(&rr->downqueue);

	return 0;
}

int gsm_exit_rr(struct osmocom_ms *ms)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;

	flush queues

	stop_rr_t3122(rr);
	stop_rr_t3126(rr);
alle timer gestoppt?:
todo stop t3122 when cell change

	return 0;
}

/* send HANDOVER ACCESS burst (9.1.14) */
static int gsm_rr_tx_hando_access(struct osmocom_ms *ms)
{
	nmsg = msgb_alloc_headroom(20, 16, "HAND_ACCESS");
	if (!nmsg)
		return -ENOMEM;
	*msgb_put(nmsg, 1) = rr->hando_ref;
	return rslms_tx_rll_req_l3(ms, RSL_MT_RAND_ACC_REQ, chan_nr, 0, nmsg);
}

/* send next channel request in dedicated state */
static int gsm_rr_rand_acc_cnf_dedicated(struct osmocom_ms *ms, struct msgb *msg)
{
	struct gsm_rrlayer *rr = &ms->rrlayer;
	struct msgb *nmsg;
	int s;

	if (!rr->hando_susp_state) {
		DEBUGP(DRR, "Random acces confirm, but not in handover state.\n");
		return 0;
	}

	/* send up to four handover access bursts */
	if (rr->hando_acc_left) {
		rr->hando_acc_left--;
		gsm_rr_tx_hando_access(ms);
		return;
	}

	/* start timer for sending next HANDOVER ACCESS bursts afterwards */
	if (!timer_pending(&rr->t3124)) {
		if (allocated channel is SDCCH)
			start_rr_t3124(rr, GSM_T3124_675);
		else
			start_rr_t3124(rr, GSM_T3124_320);
	if (!rr->n_chan_req) {
		start_rr_t3126(rr, GSM_T3126_MS);
		return 0;
	}
	rr->n_chan_req--;

	/* wait for PHYSICAL INFORMATION message or T3124 timeout */
	return 0;

}