Merge branch 'master' of gitosis@git.osmocom.org:osmocom-bb
[osmocom-bb.git] / src / host / layer23 / src / gsm48_rr.c
1 /*
2  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
3  *
4  * All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21
22 /* Very short description of some of the procedures:
23  *
24  * A radio ressource request causes sendig a channel request on RACH.
25  * After receiving of an immediate assignment the link will be establised.
26  * After the link is established, the dedicated mode is entered and confirmed.
27  *
28  * A Paging request also triggers the channel request as above...
29  * After the link is established, the dedicated mode is entered and indicated.
30  *
31  * During dedicated mode, messages are transferred.
32  *
33  * When an assignment command or a handover command is received, the current
34  * link is released. After release, the new channel is activated and the
35  * link is established again. After link is establised, pending messages from
36  * radio ressource are sent.
37  *
38  * When the assignment or handover fails, the old channel is activate and the
39  * link is established again. Also pending messages are sent.
40  *
41  */
42
43 #include <stdint.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <arpa/inet.h>
49
50 #include <osmocore/msgb.h>
51 #include <osmocore/utils.h>
52 #include <osmocore/rsl.h>
53 #include <osmocore/gsm48.h>
54 #include <osmocore/bitvec.h>
55
56 #include <osmocom/logging.h>
57 #include <osmocom/osmocom_data.h>
58
59 static int gsm48_rcv_rsl(struct osmocom_ms *ms, struct msgb *msg);
60 static int gsm48_rr_dl_est(struct osmocom_ms *ms);
61
62 /*
63  * support
64  */
65
66 #define MIN(a, b) ((a < b) ? a : b)
67
68 int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
69         uint16_t *mnc, uint16_t *lac)
70 {
71         *mcc = (lai->digits[0] & 0x0f) * 100
72                 + (lai->digits[0] >> 4) * 10
73                 + (lai->digits[1] & 0x0f);
74         *mnc = (lai->digits[2] & 0x0f) * 10
75                 + (lai->digits[2] >> 4);
76         if ((lai->digits[1] >> 4) != 0xf) /* 3 digits MNC */
77                 *mnc += (lai->digits[1] >> 4) * 100;
78         *lac = ntohs(lai->lac);
79
80         return 0;
81 }
82
83 static int gsm48_encode_chan_h0(struct gsm48_chan_desc *cd, uint8_t tsc,
84         uint16_t arfcn)
85 {
86         cd->h0.tsc = tsc;
87         cd->h0.h = 0;
88         cd->h0.arfcn_low = arfcn & 0xff;
89         cd->h0.arfcn_high = arfcn >> 8;
90
91         return 0;
92 }
93
94 static int gsm48_encode_chan_h1(struct gsm48_chan_desc *cd, uint8_t tsc,
95         uint8_t maio, uint8_t hsn)
96 {
97         cd->h1.tsc = tsc;
98         cd->h1.h = 1;
99         cd->h1.maio_low = maio & 0x03;
100         cd->h1.maio_high = maio >> 2;
101         cd->h1.hsn = hsn;
102
103         return 0;
104 }
105
106
107 static int gsm48_decode_chan_h0(struct gsm48_chan_desc *cd, uint8_t *tsc, 
108         uint16_t *arfcn)
109 {
110         *tsc = cd->h0.tsc;
111         *arfcn = cd->h0.arfcn_low | (cd->h0.arfcn_high << 8);
112
113         return 0;
114 }
115
116 static int gsm48_decode_chan_h1(struct gsm48_chan_desc *cd, uint8_t *tsc,
117         uint8_t *maio, uint8_t *hsn)
118 {
119         *tsc = cd->h1.tsc;
120         *maio = cd->h1.maio_low | (cd->h1.maio_high << 2);
121         *hsn = cd->h1.hsn;
122
123         return 0;
124 }
125
126 /* 10.5.2.38 decode Starting time IE */
127 static int gsm48_decode_start_time(struct gsm48_rr_cd *cd,
128         struct gsm48_start_time *st)
129 {
130         cd->start_t1 = st->t1;
131         cd->start_t2 = st->t2;
132         cd->start_t3 = (st->t3_high << 3) | st->t3_low;
133
134         return 0;
135 }
136
137 /*
138  * state transition
139  */
140
141 static const char *gsm48_rr_state_names[] = {
142         "IDLE",
143         "CONN PEND",
144         "DEDICATED",
145 };
146
147 static void new_rr_state(struct gsm48_rrlayer *rr, int state)
148 {
149         if (state < 0 || state >= 
150                 (sizeof(gsm48_rr_state_names) / sizeof(char *)))
151                 return;
152
153         if (state == GSM48_RR_ST_IDLE) {
154                 struct msgb *msg, *nmsg;
155
156                 /* free establish message, if any */
157                 rr->rr_est_req = 0;
158                 if (rr->rr_est_msg) {
159                         msgb_free(rr->rr_est_msg);
160                         rr->rr_est_msg = NULL;
161                 }
162                 /* free all pending messages */
163                 while((msg = msgb_dequeue(&rr->downqueue)))
164                         msgb_free(msg);
165                 /* clear all descriptions of last channel */
166                 memset(&rr->cd_now, 0, sizeof(rr->cd_now));
167                 /* reset cipering */
168                 rr->cipher_on = 0;
169                 /* tell cell selection process to return to idle mode
170                  * NOTE: this must be sent unbuffered, because it will
171                  * leave camping state, so it locks against subsequent
172                  * establishment of dedicated channel, before the
173                  * cell selection process returned to camping state
174                  * again.
175                  */
176                 nmsg = gsm322_msgb_alloc(GSM322_EVENT_LEAVE_IDLE);
177                 if (!nmsg)
178                         return;
179                 gsm322_c_event(rr->ms, nmsg);
180                 msgb_free(nmsg);
181         }
182
183         LOGP(DRR, LOGL_INFO, "new state %s -> %s\n",
184                 gsm48_rr_state_names[rr->state], gsm48_rr_state_names[state]);
185
186         rr->state = state;
187 }
188
189 /*
190  * messages
191  */
192
193 /* names of RR-SAP */
194 static const struct value_string gsm48_rr_msg_names[] = {
195         { GSM48_RR_EST_REQ,             "RR_EST_REQ" },
196         { GSM48_RR_EST_IND,             "RR_EST_IND" },
197         { GSM48_RR_EST_CNF,             "RR_EST_CNF" },
198         { GSM48_RR_REL_IND,             "RR_REL_IND" },
199         { GSM48_RR_SYNC_IND,            "RR_SYNC_IND" },
200         { GSM48_RR_DATA_REQ,            "RR_DATA_REQ" },
201         { GSM48_RR_DATA_IND,            "RR_DATA_IND" },
202         { GSM48_RR_UNIT_DATA_IND,       "RR_UNIT_DATA_IND" },
203         { GSM48_RR_ABORT_REQ,           "RR_ABORT_REQ" },
204         { GSM48_RR_ABORT_IND,           "RR_ABORT_IND" },
205         { GSM48_RR_ACT_REQ,             "RR_ACT_REQ" },
206         { 0,                            NULL }
207 };
208
209 const char *get_rr_name(int value)
210 {
211         return get_value_string(gsm48_rr_msg_names, value);
212 }
213
214 /* allocate GSM 04.08 layer 3 message */
215 struct msgb *gsm48_l3_msgb_alloc(void)
216 {
217         struct msgb *msg;
218
219         msg = msgb_alloc_headroom(L3_ALLOC_SIZE+L3_ALLOC_HEADROOM,
220                 L3_ALLOC_HEADROOM, "GSM 04.08 L3");
221         if (!msg)
222                 return NULL;
223         msg->l3h = msg->data;
224
225         return msg;
226 }
227
228 /* allocate GSM 04.08 message (RR-SAP) */
229 struct msgb *gsm48_rr_msgb_alloc(int msg_type)
230 {
231         struct msgb *msg;
232         struct gsm48_rr_hdr *rrh;
233
234         msg = msgb_alloc_headroom(RR_ALLOC_SIZE+RR_ALLOC_HEADROOM,
235                 RR_ALLOC_HEADROOM, "GSM 04.08 RR");
236         if (!msg)
237                 return NULL;
238
239         rrh = (struct gsm48_rr_hdr *) msgb_put(msg, sizeof(*rrh));
240         rrh->msg_type = msg_type;
241
242         return msg;
243 }
244
245 /* queue message (RR-SAP) */
246 int gsm48_rr_upmsg(struct osmocom_ms *ms, struct msgb *msg)
247 {
248         struct gsm48_mmlayer *mm = &ms->mmlayer;
249
250         msgb_enqueue(&mm->rr_upqueue, msg);
251
252         return 0;
253 }
254
255 /* push rsl header and send (RSL-SAP) */
256 static int gsm48_send_rsl(struct osmocom_ms *ms, uint8_t msg_type,
257                                 struct msgb *msg)
258 {
259         struct gsm48_rrlayer *rr = &ms->rrlayer;
260
261         rsl_rll_push_l3(msg, msg_type, rr->cd_now.chan_nr,
262                 rr->cd_now.link_id, 1);
263
264         return rslms_recvmsg(msg, ms);
265 }
266
267 /* enqueue messages (RSL-SAP) */
268 static int gsm48_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
269 {
270         struct gsm48_rrlayer *rr = &ms->rrlayer;
271
272 #warning HACK!!!!!!
273 return gsm48_rcv_rsl(ms, msg);
274         msgb_enqueue(&rr->rsl_upqueue, msg);
275
276         return 0;
277 }
278
279 /* input function that L2 calls when sending messages up to L3 */
280 static int gsm48_rx_rsl(struct msgb *msg, struct osmocom_ms *ms)
281 {
282         struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
283         int rc = 0;
284
285         switch (rslh->msg_discr & 0xfe) {
286         case ABIS_RSL_MDISC_RLL:
287                 rc = gsm48_rx_rll(msg, ms);
288                 break;
289         default:
290                 /* FIXME: implement this */
291                 LOGP(DRSL, LOGL_NOTICE, "unknown RSLms msg_discr 0x%02x\n",
292                         rslh->msg_discr);
293                 rc = -EINVAL;
294                 break;
295         }
296
297         return rc;
298 }
299
300 /* dequeue messages (RSL-SAP) */
301 int gsm48_rsl_dequeue(struct osmocom_ms *ms)
302 {
303         struct gsm48_rrlayer *rr = &ms->rrlayer;
304         struct msgb *msg;
305         int work = 0;
306         
307         while ((msg = msgb_dequeue(&rr->rsl_upqueue))) {
308                 /* msg is freed there */
309                 gsm48_rcv_rsl(ms, msg);
310                 work = 1; /* work done */
311         }
312         
313         return work;
314 }
315
316 /*
317  * timers handling
318  */
319
320 static void timeout_rr_t3122(void *arg)
321 {
322         LOGP(DRR, LOGL_INFO, "timer T3122 has fired\n");
323 }
324
325 static void timeout_rr_t3126(void *arg)
326 {
327         struct gsm48_rrlayer *rr = arg;
328         struct osmocom_ms *ms = rr->ms;
329
330         LOGP(DRR, LOGL_INFO, "timer T3126 has fired\n");
331         if (rr->rr_est_req) {
332                 struct msgb *msg = gsm48_rr_msgb_alloc(GSM48_RR_REL_IND);
333                 struct gsm48_rr_hdr *rrh;
334
335                 if (!msg)
336                         return;
337                 rrh = (struct gsm48_rr_hdr *)msg->data;
338                 rrh->cause = RR_REL_CAUSE_RA_FAILURE;
339                 gsm48_rr_upmsg(ms, msg);
340         }
341
342         new_rr_state(rr, GSM48_RR_ST_IDLE);
343 }
344
345 static void start_rr_t3122(struct gsm48_rrlayer *rr, int sec, int micro)
346 {
347         LOGP(DRR, LOGL_INFO, "starting T3122 with %d seconds\n", sec);
348         rr->t3122.cb = timeout_rr_t3122;
349         rr->t3122.data = rr;
350         bsc_schedule_timer(&rr->t3122, sec, micro);
351 }
352
353 static void start_rr_t3126(struct gsm48_rrlayer *rr, int sec, int micro)
354 {
355         LOGP(DRR, LOGL_INFO, "starting T3126 with %d seconds\n", sec);
356         rr->t3126.cb = timeout_rr_t3126;
357         rr->t3126.data = rr;
358         bsc_schedule_timer(&rr->t3126, sec, micro);
359 }
360
361 static void stop_rr_t3122(struct gsm48_rrlayer *rr)
362 {
363         if (bsc_timer_pending(&rr->t3122)) {
364                 LOGP(DRR, LOGL_INFO, "stopping pending timer T3122\n");
365                 bsc_del_timer(&rr->t3122);
366         }
367 }
368
369 static void stop_rr_t3126(struct gsm48_rrlayer *rr)
370 {
371         if (bsc_timer_pending(&rr->t3126)) {
372                 LOGP(DRR, LOGL_INFO, "stopping pending timer T3126\n");
373                 bsc_del_timer(&rr->t3126);
374         }
375 }
376
377 /*
378  * status
379  */
380
381 /* send rr status request */
382 static int gsm48_rr_tx_rr_status(struct osmocom_ms *ms, uint8_t cause)
383 {
384         struct msgb *nmsg;
385         struct gsm48_hdr *gh;
386         struct gsm48_rr_status *st;
387
388         LOGP(DRR, LOGL_INFO, "RR STATUS (cause #%d)\n", cause);
389
390         nmsg = gsm48_l3_msgb_alloc();
391         if (!nmsg)
392                 return -ENOMEM;
393         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
394         st = (struct gsm48_rr_status *) msgb_put(nmsg, sizeof(*st));
395
396         gh->proto_discr = GSM48_PDISC_RR;
397         gh->msg_type = GSM48_MT_RR_CIPH_M_COMPL;
398
399         /* rr cause */
400         st->rr_cause = cause;
401
402         return gsm48_send_rsl(ms, RSL_MT_DATA_REQ, nmsg);
403 }
404
405 /*
406  * ciphering
407  */
408
409 /* send chiperhing mode complete */
410 static int gsm48_rr_tx_cip_mode_cpl(struct osmocom_ms *ms, uint8_t cr)
411 {
412         struct gsm_support *sup = &ms->support;
413         struct msgb *nmsg;
414         struct gsm48_hdr *gh;
415         uint8_t buf[11], *tlv;
416
417         LOGP(DRR, LOGL_INFO, "CIPHERING MODE COMPLETE (cr %d)\n", cr);
418
419         nmsg = gsm48_l3_msgb_alloc();
420         if (!nmsg)
421                 return -ENOMEM;
422         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
423
424         gh->proto_discr = GSM48_PDISC_RR;
425         gh->msg_type = GSM48_MT_RR_CIPH_M_COMPL;
426
427         /* MI */
428         if (cr) {
429                 gsm48_generate_mid_from_imsi(buf, sup->imeisv);
430                 tlv = msgb_put(nmsg, 2 + buf[1]);
431                 memcpy(tlv, buf, 2 + buf[1]);
432         }
433
434         return gsm48_send_rsl(ms, RSL_MT_DATA_REQ, nmsg);
435 }
436
437 /* receive ciphering mode command */
438 static int gsm48_rr_rx_cip_mode_cmd(struct osmocom_ms *ms, struct msgb *msg)
439 {
440         struct gsm48_rrlayer *rr = &ms->rrlayer;
441         struct gsm_support *sup = &ms->support;
442         struct gsm48_hdr *gh = msgb_l3(msg);
443         struct gsm48_cip_mode_cmd *cm = (struct gsm48_cip_mode_cmd *)gh->data;
444         int payload_len = msgb_l3len(msg) - sizeof(*gh) - sizeof(*cm);
445         uint8_t sc, alg_id, cr;
446
447         if (payload_len < 0) {
448                 LOGP(DRR, LOGL_NOTICE, "Short read of CIPHERING MODE COMMAND "
449                         "message.\n");
450                 return gsm48_rr_tx_rr_status(ms,
451                         GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
452         }
453
454         /* cipher mode setting */
455         sc = cm->sc;
456         alg_id = cm->alg_id;
457         /* cipher mode response */
458         cr = cm->cr;
459
460         if (sc)
461                 LOGP(DRR, LOGL_INFO, "CIPHERING MODE COMMAND (sc=%u, cr=%u)",
462                         sc, cr);
463         else
464                 LOGP(DRR, LOGL_INFO, "CIPHERING MODE COMMAND (sc=%u, "
465                         "algo=A5/%d cr=%u)", sc, alg_id + 1, cr);
466
467         /* 3.4.7.2 */
468         if (rr->cipher_on && sc) {
469                 LOGP(DRR, LOGL_INFO, "cipering already applied.\n");
470                 return gsm48_rr_tx_rr_status(ms,
471                         GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
472         }
473
474         /* check if we actually support this cipher */
475         if ((alg_id == GSM_CIPHER_A5_1 && !sup->a5_1)
476          || (alg_id == GSM_CIPHER_A5_2 && !sup->a5_2)
477          || (alg_id == GSM_CIPHER_A5_3 && !sup->a5_3)
478          || (alg_id == GSM_CIPHER_A5_4 && !sup->a5_4)
479          || (alg_id == GSM_CIPHER_A5_5 && !sup->a5_5)
480          || (alg_id == GSM_CIPHER_A5_6 && !sup->a5_6)
481          || (alg_id == GSM_CIPHER_A5_7 && !sup->a5_7))
482                 return gsm48_rr_tx_rr_status(ms,
483                         GSM48_RR_CAUSE_CHAN_MODE_UNACCT);
484
485         /* change to ciphering */
486 #ifdef TODO
487         rsl command to activate ciperhing
488 #endif
489         rr->cipher_on = sc, rr->cipher_type = alg_id;
490
491         /* response */
492         return gsm48_rr_tx_cip_mode_cpl(ms, cr);
493 }
494
495 /*
496  * classmark
497  */
498
499 /* Encode  "Classmark 3" (10.5.1.7) */
500 static int gsm48_rr_enc_cm3(struct osmocom_ms *ms, uint8_t *buf, uint8_t *len)
501 {
502         struct gsm_support *sup = &ms->support;
503         struct bitvec bv;
504
505         memset(&bv, 0, sizeof(bv));
506         bv.data = buf;
507         bv.data_len = 12;
508
509         /* spare bit */
510         bitvec_set_bit(&bv, 0);
511         /* band 3 supported */
512         if (sup->dcs_1800)
513                 bitvec_set_bit(&bv, ONE);
514         else
515                 bitvec_set_bit(&bv, ZERO);
516         /* band 2 supported */
517         if (sup->e_gsm || sup->r_gsm)
518                 bitvec_set_bit(&bv, ONE);
519         else
520                 bitvec_set_bit(&bv, ZERO);
521         /* band 1 supported */
522         if (sup->p_gsm && !(sup->e_gsm || sup->r_gsm))
523                 bitvec_set_bit(&bv, ONE);
524         else
525                 bitvec_set_bit(&bv, ZERO);
526         /* a5 bits */
527         if (sup->a5_7)
528                 bitvec_set_bit(&bv, ONE);
529         else
530                 bitvec_set_bit(&bv, ZERO);
531         if (sup->a5_6)
532                 bitvec_set_bit(&bv, ONE);
533         else
534                 bitvec_set_bit(&bv, ZERO);
535         if (sup->a5_5)
536                 bitvec_set_bit(&bv, ONE);
537         else
538                 bitvec_set_bit(&bv, ZERO);
539         if (sup->a5_4)
540                 bitvec_set_bit(&bv, ONE);
541         else
542                 bitvec_set_bit(&bv, ZERO);
543         /* radio capability */
544         if (sup->dcs_1800 && !sup->p_gsm && !(sup->e_gsm || sup->r_gsm)) {
545                 /* dcs only */
546                 bitvec_set_uint(&bv, 0, 4);
547                 bitvec_set_uint(&bv, sup->dcs_capa, 4);
548         } else
549         if (sup->dcs_1800 && (sup->p_gsm || (sup->e_gsm || sup->r_gsm))) {
550                 /* dcs */
551                 bitvec_set_uint(&bv, sup->dcs_capa, 4);
552                 /* low band */
553                 bitvec_set_uint(&bv, sup->low_capa, 4);
554         } else {
555                 /* low band only */
556                 bitvec_set_uint(&bv, 0, 4);
557                 bitvec_set_uint(&bv, sup->low_capa, 4);
558         }
559         /* r support */
560         if (sup->r_gsm) {
561                 bitvec_set_bit(&bv, ONE);
562                 bitvec_set_uint(&bv, sup->r_capa, 3);
563         } else {
564                 bitvec_set_bit(&bv, ZERO);
565         }
566         /* multi slot support */
567         if (sup->ms_sup) {
568                 bitvec_set_bit(&bv, ONE);
569                 bitvec_set_uint(&bv, sup->ms_sup, 5);
570         } else {
571                 bitvec_set_bit(&bv, ZERO);
572         }
573         /* ucs2 treatment */
574         if (sup->ucs2_treat) {
575                 bitvec_set_bit(&bv, ONE);
576         } else {
577                 bitvec_set_bit(&bv, ZERO);
578         }
579         /* support extended measurements */
580         if (sup->ext_meas) {
581                 bitvec_set_bit(&bv, ONE);
582         } else {
583                 bitvec_set_bit(&bv, ZERO);
584         }
585         /* support measurement capability */
586         if (sup->meas_cap) {
587                 bitvec_set_bit(&bv, ONE);
588                 bitvec_set_uint(&bv, sup->sms_val, 4);
589                 bitvec_set_uint(&bv, sup->sm_val, 4);
590         } else {
591                 bitvec_set_bit(&bv, ZERO);
592         }
593         /* positioning method capability */
594         if (sup->loc_serv) {
595                 bitvec_set_bit(&bv, ONE);
596                 bitvec_set_bit(&bv, sup->e_otd_ass == 1);
597                 bitvec_set_bit(&bv, sup->e_otd_based == 1);
598                 bitvec_set_bit(&bv, sup->gps_ass == 1);
599                 bitvec_set_bit(&bv, sup->gps_based == 1);
600                 bitvec_set_bit(&bv, sup->gps_conv == 1);
601         } else {
602                 bitvec_set_bit(&bv, ZERO);
603         }
604
605         /* partitial bytes will be completed */
606         *len = (bv.cur_bit + 7) >> 3;
607         bitvec_spare_padding(&bv, (*len * 8) - 1);
608
609         return 0;
610 }
611
612 /* encode classmark 2 */
613 int gsm48_rr_enc_cm2(struct osmocom_ms *ms, struct gsm48_classmark2 *cm)
614 {
615         struct gsm48_rrlayer *rr = &ms->rrlayer;
616         struct gsm_support *sup = &ms->support;
617
618         if (rr->arfcn >= 512 && rr->arfcn <= 885)
619                 cm->pwr_lev = sup->pwr_lev_1800;
620         else
621                 cm->pwr_lev = sup->pwr_lev_900;
622         cm->a5_1 = sup->a5_1;
623         cm->es_ind = sup->es_ind;
624         cm->rev_lev = sup->rev_lev;
625         cm->fc = (sup->r_gsm || sup->e_gsm);
626         cm->vgcs = sup->vgcs;
627         cm->vbs = sup->vbs;
628         cm->sm_cap = sup->sms_ptp;
629         cm->ss_scr = sup->ss_ind;
630         cm->ps_cap = sup->ps_cap;
631         cm->a5_2 = sup->a5_2;
632         cm->a5_3 = sup->a5_3;
633         cm->cmsp = sup->cmsp;
634         cm->solsa = sup->solsa;
635         cm->lcsva_cap = sup->lcsva;
636
637         return 0;
638 }
639
640 /* send classmark change */
641 static int gsm48_rr_tx_cm_change(struct osmocom_ms *ms)
642 {
643         struct gsm_support *sup = &ms->support;
644         struct msgb *nmsg;
645         struct gsm48_hdr *gh;
646         struct gsm48_cm_change *cc;
647         uint8_t cm3[14], *tlv;
648
649         LOGP(DRR, LOGL_INFO, "CLASSMARK CHANGE\n");
650
651         nmsg = gsm48_l3_msgb_alloc();
652         if (!nmsg)
653                 return -ENOMEM;
654         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
655         cc = (struct gsm48_cm_change *) msgb_put(nmsg, sizeof(*cc));
656
657         gh->proto_discr = GSM48_PDISC_RR;
658         gh->msg_type = GSM48_MT_RR_CLSM_CHG;
659
660         /* classmark 2 */
661         cc->cm2_len = sizeof(cc->cm2);
662         gsm48_rr_enc_cm2(ms, &cc->cm2);
663
664         /* classmark 3 */
665         if (sup->dcs_1800 || sup->e_gsm || sup->r_gsm
666          || sup->a5_7 || sup->a5_6 || sup->a5_5 || sup->a5_4
667          || sup->ms_sup
668          || sup->ucs2_treat
669          || sup->ext_meas || sup->meas_cap
670          || sup->loc_serv) {
671                 cc->cm2.cm3 = 1;
672                 cm3[0] = GSM48_IE_CLASSMARK3;
673                 gsm48_rr_enc_cm3(ms, cm3 + 2, &cm3[1]);
674                 tlv = msgb_put(nmsg, 2 + cm3[1]);
675                 memcpy(tlv, cm3, 2 + cm3[1]);
676         }
677
678         return gsm48_send_rsl(ms, RSL_MT_DATA_REQ, nmsg);
679 }
680
681 /* receiving classmark enquiry */
682 static int gsm48_rr_rx_cm_enq(struct osmocom_ms *ms, struct msgb *msg)
683 {
684         /* send classmark */
685         return gsm48_rr_tx_cm_change(ms);
686 }
687
688 /*
689  * random access
690  */
691
692 /* temporary timer until we have time control over channnel request */
693 #define RSL_MT_CHAN_CNF 0x19
694 #include <osmocom/l1ctl.h>
695 static void temp_rach_to(void *arg)
696 {
697         struct gsm48_rrlayer *rr = arg;
698         struct osmocom_ms *ms = rr->ms;
699         struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RR");
700         struct abis_rsl_rll_hdr *rllh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rllh));
701
702         rllh->c.msg_type = RSL_MT_CHAN_CNF;
703         gsm48_rcv_rsl(ms, msg);
704
705         return;
706 }
707
708 /* send channel request burst message */
709 static int gsm48_rr_tx_chan_req(struct osmocom_ms *ms, int cause, int paging)
710 {
711         struct gsm48_rrlayer *rr = &ms->rrlayer;
712         struct gsm322_cellsel *cs = &ms->cellsel;
713         struct gsm48_sysinfo *s = &ms->sysinfo;
714         struct msgb *nmsg;
715         struct l1ctl_info_ul *nul;
716         struct l1ctl_rach_req *nra;
717         struct gsm48_rr_hdr *nrrh;
718         uint8_t chan_req, chan_req_val, chan_req_mask;
719         int t;
720
721         /* ignore paging, if not camping */
722         if (paging && cs->state != GSM322_C3_CAMPED_NORMALLY) {
723                 return -EBUSY;
724         }
725
726         /* tell cell selection process to leave idle mode
727          * NOTE: this must be sent unbuffered, because the state may not
728          * change until idle mode is left
729          */
730         nmsg = gsm322_msgb_alloc(GSM322_EVENT_LEAVE_IDLE);
731         if (!nmsg)
732                 return -ENOMEM;
733         gsm322_c_event(ms, nmsg);
734         msgb_free(nmsg);
735
736         /* 3.3.1.1.2 */
737         new_rr_state(rr, GSM48_RR_ST_CONN_PEND);
738
739         /* number of retransmissions (without first transmission) */
740         rr->n_chan_req = s->max_retrans;
741
742         /* generate CHAN REQ (9.1.8) */
743         switch (cause) {
744         case RR_EST_CAUSE_EMERGENCY:
745                 /* 101xxxxx */
746                 chan_req_mask = 0x1f;
747                 chan_req_val = 0xa0;
748                 LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (Emergency call)\n",
749                         chan_req_val);
750                 break;
751         case RR_EST_CAUSE_REESTAB_TCH_F:
752                 chan_req_mask = 0x1f;
753                 chan_req_val = 0xc0;
754                 LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (re-establish "
755                         "TCH/F)\n", chan_req_val);
756                 break;
757         case RR_EST_CAUSE_REESTAB_TCH_H:
758                 if (s->neci) {
759                         chan_req_mask = 0x03;
760                         chan_req_val = 0x68;
761                         LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x "
762                                 "(re-establish TCH/H with NECI)\n",
763                                 chan_req_val);
764                 } else {
765                         chan_req_mask = 0x1f;
766                         chan_req_val = 0xc0;
767                         LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x "
768                                 "(re-establish TCH/H no NECI)\n", chan_req_val);
769                 }
770                 break;
771         case RR_EST_CAUSE_REESTAB_2_TCH_H:
772                 if (s->neci) {
773                         chan_req_mask = 0x03;
774                         chan_req_val = 0x6c;
775                         LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x "
776                                 "(re-establish TCH/H+TCH/H with NECI)\n",
777                                 chan_req_val);
778                 } else {
779                         chan_req_mask = 0x1f;
780                         chan_req_val = 0xc0;
781                         LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x "
782                                 "(re-establish TCH/H+TCH/H no NECI)\n",
783                                 chan_req_val);
784                 }
785                 break;
786         case RR_EST_CAUSE_ANS_PAG_ANY:
787                 chan_req_mask = 0x1f;
788                 chan_req_val = 0x80;
789                 LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (PAGING "
790                         "Any channel)\n", chan_req_val);
791                 break;
792         case RR_EST_CAUSE_ANS_PAG_SDCCH:
793                 chan_req_mask = 0x0f;
794                 chan_req_val = 0x10;
795                 LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (PAGING SDCCH)\n",
796                         chan_req_val);
797                 break;
798         case RR_EST_CAUSE_ANS_PAG_TCH_F:
799                 /* ms supports no dual rate */
800                 chan_req_mask = 0x1f;
801                 chan_req_val = 0x80;
802                 LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (PAGING TCH/F)\n",
803                         chan_req_val);
804                 break;
805         case RR_EST_CAUSE_ANS_PAG_TCH_ANY:
806                 /* ms supports no dual rate */
807                 chan_req_mask = 0x1f;
808                 chan_req_val = 0x80;
809                 LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (PAGING TCH/H or "
810                                 "TCH/F)\n", chan_req_val);
811                 break;
812         case RR_EST_CAUSE_ORIG_TCHF:
813                 /* ms supports no dual rate */
814                 chan_req_mask = 0x1f;
815                 chan_req_val = 0xe0;
816                 LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (Orig TCH/F)\n",
817                         chan_req_val);
818                 break;
819         case RR_EST_CAUSE_LOC_UPD:
820                 if (s->neci) {
821                         chan_req_mask = 0x0f;
822                         chan_req_val = 0x00;
823                         LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (Location "
824                                 "Update with NECI)\n", chan_req_val);
825                 } else {
826                         chan_req_mask = 0x1f;
827                         chan_req_val = 0x00;
828                         LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (Location "
829                                 "Update no NECI)\n", chan_req_val);
830                 }
831                 break;
832         case RR_EST_CAUSE_OTHER_SDCCH:
833                 if (s->neci) {
834                         chan_req_mask = 0x0f;
835                         chan_req_val = 0x01;
836                         LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (OHTER "
837                                 "with NECI)\n", chan_req_val);
838                 } else {
839                         chan_req_mask = 0x1f;
840                         chan_req_val = 0xe0;
841                         LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: %02x (OTHER "
842                                 "no NECI)\n", chan_req_val);
843                 }
844                 break;
845         default:
846                 if (!rr->rr_est_req) /* no request from MM */
847                         return -EINVAL;
848
849                 LOGP(DRR, LOGL_INFO, "CHANNEL REQUEST: with unknown "
850                         "establishment cause: %d\n", cause);
851                 nmsg = gsm48_rr_msgb_alloc(GSM48_RR_REL_IND);
852                 if (!nmsg)
853                         return -ENOMEM;
854                 nrrh = (struct gsm48_rr_hdr *)nmsg->data;
855                 nrrh->cause = RR_REL_CAUSE_UNDEFINED;
856                 gsm48_rr_upmsg(ms, nmsg);
857                 new_rr_state(rr, GSM48_RR_ST_IDLE);
858                 return -EINVAL;
859         }
860
861         rr->wait_assign = 1;
862
863         /* create and send RACH msg */
864 #ifdef TODO
865         nmsg = gsm48_rsl_msgb_alloc();
866 #else
867         nmsg = msgb_alloc_headroom(64, 48, "RAND_ACC");
868         struct l1ctl_hdr *l1h;
869         nmsg->l1h = msgb_put(nmsg, sizeof(*l1h));
870         l1h = (struct l1ctl_hdr *) nmsg->l1h;
871         l1h->msg_type = L1CTL_RACH_REQ;
872         if (!nmsg)
873                 return -ENOMEM;
874         nul = (struct l1ctl_info_ul *) msgb_put(nmsg, sizeof(*nul));
875 #endif
876         nra = (struct l1ctl_rach_req *) msgb_put(nmsg, sizeof(*nra));
877
878         chan_req = random();
879         chan_req &= chan_req_mask;
880         chan_req |= chan_req_val;
881         nra->ra = chan_req;
882         t = s->tx_integer;
883         if (t < 8)
884                 t = 8;
885 #ifdef TODO
886         at this point we require chan req to be sent at a given delay
887         also we require a confirm from radio part
888         nra->delay = random() % t;
889 #else
890         rr->temp_rach_ti.cb = temp_rach_to;
891         rr->temp_rach_ti.data = rr;
892         bsc_schedule_timer(&rr->temp_rach_ti, 0, 900000);
893 #endif
894
895         /* store */
896         rr->chan_req_val = chan_req_val;
897         rr->chan_req_mask = chan_req_mask;
898         rr->cr_hist[3] = -1;
899         rr->cr_hist[2] = -1;
900         rr->cr_hist[1] = chan_req;
901
902 #ifdef TODO
903         add layer 1 conrols to RSL...
904         return gsm48_send_rsl(ms, RSL_MT_CHAN_REQ, nmsg);
905 #else
906         return osmo_send_l1(ms, nmsg);
907 #endif
908 }
909
910 /* send next channel request in conn pend state */
911 static int gsm48_rr_rand_acc_cnf(struct osmocom_ms *ms, struct msgb *msg)
912 {
913         struct gsm48_rrlayer *rr = &ms->rrlayer;
914         struct gsm48_sysinfo *s = &ms->sysinfo;
915         struct msgb *nmsg;
916         struct l1ctl_info_ul *nul;
917         struct l1ctl_rach_req *nra;
918         int slots;
919         uint8_t chan_req;
920
921         LOGP(DRR, LOGL_INFO, "RANDOM ACCESS confirm (requests left %d)\n",
922                 rr->n_chan_req);
923
924         if (!rr->n_chan_req) {
925                 LOGP(DRR, LOGL_INFO, "Done with sending RANDOM ACCESS "
926                         "bursts\n");
927                 if (!bsc_timer_pending(&rr->t3126))
928                         start_rr_t3126(rr, 5, 0); /* TODO improve! */
929                 return 0;
930         }
931         rr->n_chan_req--;
932
933         /* table 3.1 */
934         switch(s->tx_integer) {
935         case 3: case 8: case 14: case 50:
936                 if (s->ccch_conf != 1) /* not combined CCCH */
937                         slots = 55;
938                 else
939                         slots = 41;
940         case 4: case 9: case 16:
941                 if (s->ccch_conf != 1)
942                         slots = 76;
943                 else
944                         slots = 52;
945         case 5: case 10: case 20:
946                 if (s->ccch_conf != 1)
947                         slots = 109;
948                 else
949                         slots = 58;
950         case 6: case 11: case 25:
951                 if (s->ccch_conf != 1)
952                         slots = 163;
953                 else
954                         slots = 86;
955         default:
956                 if (s->ccch_conf != 1)
957                         slots = 217;
958                 else
959                         slots = 115;
960         }
961
962         /* resend chan_req with new randiom */
963 #ifdef TODO
964         nmsg = gsm48_rsl_msgb_alloc();
965 #else
966         nmsg = msgb_alloc_headroom(64, 48, "RAND_ACC");
967         struct l1ctl_hdr *l1h;
968         nmsg->l1h = msgb_put(nmsg, sizeof(*l1h));
969         l1h = (struct l1ctl_hdr *) nmsg->l1h;
970         l1h->msg_type = L1CTL_RACH_REQ;
971         if (!nmsg)
972                 return -ENOMEM;
973         nul = (struct l1ctl_info_ul *) msgb_put(nmsg, sizeof(*nul));
974 #endif
975         nra = (struct l1ctl_rach_req *) msgb_put(nmsg, sizeof(*nra));
976         chan_req = random();
977         chan_req &= rr->chan_req_mask;
978         chan_req |= rr->chan_req_val;
979         nra->ra = chan_req;
980 #ifdef TODO
981         at this point we require chan req to be sent at a given delay
982         also we require a confirm from radio part
983         nra->delay = (random() % s->tx_integer) + slots;
984
985         LOGP(DRR, LOGL_INFO, "RANDOM ACCESS (ra 0x%02x delay %d)\n", nra->ra,
986                 nra->delay);
987 #else
988         rr->temp_rach_ti.cb = temp_rach_to;
989         rr->temp_rach_ti.data = rr;
990         bsc_schedule_timer(&rr->temp_rach_ti, 0, 900000);
991
992         LOGP(DRR, LOGL_INFO, "RANDOM ACCESS (ra 0x%02x)\n", nra->ra);
993 #endif
994
995         /* shift history and store */
996         rr->cr_hist[3] = rr->cr_hist[2];
997         rr->cr_hist[2] = rr->cr_hist[1];
998         rr->cr_hist[1] = chan_req;
999
1000 #ifdef TODO
1001         add layer 1 conrols to RSL...
1002         return gsm48_send_rsl(ms, RSL_MT_CHAN_REQ, nmsg);
1003 #else
1004         return osmo_send_l1(ms, nmsg);
1005 #endif
1006 }
1007
1008 /*
1009  * system information
1010  */
1011
1012 /* decode "Cell Channel Description" (10.5.2.1b) and other frequency lists */
1013 static int gsm48_decode_freq_list(struct gsm_support *sup,
1014         struct gsm_sysinfo_freq *f, uint8_t *cd, uint8_t len, uint8_t mask,
1015         uint8_t frqt)
1016 {
1017         int i;
1018
1019         /* NOTES:
1020          *
1021          * The Range format uses "SMOD" computation.
1022          * e.g. "n SMOD m" equals "((n - 1) % m) + 1"
1023          * A cascade of multiple SMOD computations is simpified:
1024          * "(n SMOD m) SMOD o" equals "(((n - 1) % m) % o) + 1"
1025          *
1026          * The Range format uses 16 octets of data in SYSTEM INFORMATION.
1027          * When used in dedicated messages, the length can be less.
1028          * In this case the ranges are decoded for all frequencies that
1029          * fit in the block of given length.
1030          */
1031
1032         /* tabula rasa */
1033         for (i = 0; i < 1024; i++)
1034                 f[i].mask &= ~frqt;
1035
1036         /* 00..XXX. */
1037         if ((cd[0] & 0xc0 & mask) == 0x00) {
1038                 /* Bit map 0 format */
1039                 if (len < 16)
1040                         return -EINVAL;
1041                 for (i = 1; i <= 124; i++)
1042                         if ((cd[15 - ((i-1) >> 3)] & (1 << ((i-1) & 7))))
1043                                 f[i].mask |= frqt;
1044
1045                 return 0;
1046         }
1047
1048         /* only Bit map 0 format for P-GSM */
1049         if (sup->p_gsm && !sup->e_gsm && !sup->r_gsm && !sup->dcs_1800)
1050                 return 0;
1051
1052         /* 10..0XX. */
1053         if ((cd[0] & 0xc8 & mask) == 0x80) {
1054                 /* Range 1024 format */
1055                 uint16_t w[17]; /* 1..16 */
1056                 struct gsm48_range_1024 *r = (struct gsm48_range_1024 *)cd;
1057
1058                 if (len < 2)
1059                         return -EINVAL;
1060                 memset(w, 0, sizeof(w));
1061                 if (r->f0)
1062                         f[0].mask |= frqt;
1063                 w[1] = (r->w1_hi << 8) | r->w1_lo;
1064                 if (len >= 4)
1065                         w[2] = (r->w2_hi << 1) | r->w2_lo;
1066                 if (len >= 5)
1067                         w[3] = (r->w3_hi << 2) | r->w3_lo;
1068                 if (len >= 6)
1069                         w[4] = (r->w4_hi << 2) | r->w4_lo;
1070                 if (len >= 7)
1071                         w[5] = (r->w5_hi << 2) | r->w5_lo;
1072                 if (len >= 8)
1073                         w[6] = (r->w6_hi << 2) | r->w6_lo;
1074                 if (len >= 9)
1075                         w[7] = (r->w7_hi << 2) | r->w7_lo;
1076                 if (len >= 10)
1077                         w[8] = (r->w8_hi << 1) | r->w8_lo;
1078                 if (len >= 10)
1079                         w[9] = r->w9;
1080                 if (len >= 11)
1081                         w[10] = r->w10;
1082                 if (len >= 12)
1083                         w[11] = (r->w11_hi << 6) | r->w11_lo;
1084                 if (len >= 13)
1085                         w[12] = (r->w12_hi << 5) | r->w12_lo;
1086                 if (len >= 14)
1087                         w[13] = (r->w13_hi << 4) | r->w13_lo;
1088                 if (len >= 15)
1089                         w[14] = (r->w14_hi << 3) | r->w14_lo;
1090                 if (len >= 16)
1091                         w[15] = (r->w15_hi << 2) | r->w15_lo;
1092                 if (len >= 16)
1093                         w[16] = r->w16;
1094                 if (w[1])
1095                         f[w[1]].mask |= frqt;
1096                 if (w[2])
1097                         f[((w[1] - 512 + w[2] - 1) % 1023) + 1].mask |= frqt;
1098                 if (w[3])
1099                         f[((w[1]       + w[3] - 1) % 1023) + 1].mask |= frqt;
1100                 if (w[4])
1101                         f[((w[1] - 512 + ((w[2] - 256 + w[4] - 1) % 511)) % 1023) + 1].mask |= frqt;
1102                 if (w[5])
1103                         f[((w[1]       + ((w[3] - 256 - w[5] - 1) % 511)) % 1023) + 1].mask |= frqt;
1104                 if (w[6])
1105                         f[((w[1] - 512 + ((w[2]       + w[6] - 1) % 511)) % 1023) + 1].mask |= frqt;
1106                 if (w[7])
1107                         f[((w[1]       + ((w[3]       + w[7] - 1) % 511)) % 1023) + 1].mask |= frqt;
1108                 if (w[8])
1109                         f[((w[1] - 512 + ((w[2] - 256 + ((w[4] - 128 + w[8] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
1110                 if (w[9])
1111                         f[((w[1]       + ((w[3] - 256 + ((w[5] - 128 + w[9] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
1112                 if (w[10])
1113                         f[((w[1] - 512 + ((w[2]       + ((w[6] - 128 + w[10] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
1114                 if (w[11])
1115                         f[((w[1]       + ((w[3]       + ((w[7] - 128 + w[11] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
1116                 if (w[12])
1117                         f[((w[1] - 512 + ((w[2] - 256 + ((w[4]       + w[12] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
1118                 if (w[13])
1119                         f[((w[1]       + ((w[3] - 256 + ((w[5]       + w[13] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
1120                 if (w[14])
1121                         f[((w[1] - 512 + ((w[2]       + ((w[6]       + w[14] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
1122                 if (w[15])
1123                         f[((w[1]       + ((w[3]       + ((w[7]       + w[15] - 1) % 255)) % 511)) % 1023) + 1].mask |= frqt;
1124                 if (w[16])
1125                         f[((w[1] - 512 + ((w[2] - 256 + ((w[4] - 128 + ((w[8] - 64 + w[16] - 1) % 127)) % 255)) % 511)) % 1023) + 1].mask |= frqt;
1126
1127                 return 0;
1128         }
1129         /* 10..100. */
1130         if ((cd[0] & 0xce & mask) == 0x88) {
1131                 /* Range 512 format */
1132                 uint16_t w[18]; /* 1..17 */
1133                 struct gsm48_range_512 *r = (struct gsm48_range_512 *)cd;
1134
1135                 if (len < 4)
1136                         return -EINVAL;
1137                 memset(w, 0, sizeof(w));
1138                 w[0] = (r->orig_arfcn_hi << 9) || (r->orig_arfcn_mid << 1) || r->orig_arfcn_lo;
1139                 w[1] = (r->w1_hi << 2) || r->w1_lo;
1140                 if (len >= 5)
1141                         w[2] = (r->w2_hi << 2) || r->w2_lo;
1142                 if (len >= 6)
1143                         w[3] = (r->w3_hi << 2) || r->w3_lo;
1144                 if (len >= 7)
1145                         w[4] = (r->w4_hi << 1) || r->w4_lo;
1146                 if (len >= 7)
1147                         w[5] = r->w5;
1148                 if (len >= 8)
1149                         w[6] = r->w6;
1150                 if (len >= 9)
1151                         w[7] = (r->w7_hi << 6) || r->w7_lo;
1152                 if (len >= 10)
1153                         w[8] = (r->w8_hi << 4) || r->w8_lo;
1154                 if (len >= 11)
1155                         w[9] = (r->w9_hi << 2) || r->w9_lo;
1156                 if (len >= 11)
1157                         w[10] = r->w10;
1158                 if (len >= 12)
1159                         w[11] = r->w11;
1160                 if (len >= 13)
1161                         w[12] = (r->w12_hi << 4) || r->w12_lo;
1162                 if (len >= 14)
1163                         w[13] = (r->w13_hi << 2) || r->w13_lo;
1164                 if (len >= 14)
1165                         w[14] = r->w14;
1166                 if (len >= 15)
1167                         w[15] = r->w15;
1168                 if (len >= 16)
1169                         w[16] = (r->w16_hi << 3) || r->w16_lo;
1170                 if (len >= 16)
1171                         w[17] = r->w17;
1172                 if (w[0])
1173                         f[w[0]].mask |= frqt;
1174                 if (w[1])
1175                         f[(w[0] + w[1]) % 1024].mask |= frqt;
1176                 if (w[2])
1177                         f[(w[0] + ((w[1] - 256 + w[2] - 1) % 511) + 1) % 1024].mask |= frqt;
1178                 if (w[3])
1179                         f[(w[0] + ((w[1]       + w[3] - 1) % 511) + 1) % 1024].mask |= frqt;
1180                 if (w[4])
1181                         f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + w[4] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
1182                 if (w[5])
1183                         f[(w[0] + ((w[1]       + ((w[3] - 128 + w[5] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
1184                 if (w[6])
1185                         f[(w[0] + ((w[1] - 256 + ((w[2]       + w[6] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
1186                 if (w[7])
1187                         f[(w[0] + ((w[1]       + ((w[3]       + w[7] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
1188                 if (w[8])
1189                         f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + ((w[4] - 64 + w[8] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1190                 if (w[9])
1191                         f[(w[0] + ((w[1]       + ((w[3] - 128 + ((w[5] - 64 + w[9] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1192                 if (w[10])
1193                         f[(w[0] + ((w[1] - 256 + ((w[2]       + ((w[6] - 64 + w[10] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1194                 if (w[11])
1195                         f[(w[0] + ((w[1]       + ((w[3]       + ((w[7] - 64 + w[11] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1196                 if (w[12])
1197                         f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + ((w[4]      + w[12] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1198                 if (w[13])
1199                         f[(w[0] + ((w[1]       + ((w[3] - 128 + ((w[5]      + w[13] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1200                 if (w[14])
1201                         f[(w[0] + ((w[1] - 256 + ((w[2]       + ((w[6]      + w[14] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1202                 if (w[15])
1203                         f[(w[0] + ((w[1]       + ((w[3]       + ((w[7]      + w[15] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1204                 if (w[16])
1205                         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;
1206                 if (w[17])
1207                         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;
1208
1209                 return 0;
1210         }
1211         /* 10..101. */
1212         if ((cd[0] & 0xce & mask) == 0x8a) {
1213                 /* Range 256 format */
1214                 uint16_t w[22]; /* 1..21 */
1215                 struct gsm48_range_256 *r = (struct gsm48_range_256 *)cd;
1216
1217                 if (len < 4)
1218                         return -EINVAL;
1219                 memset(w, 0, sizeof(w));
1220                 w[0] = (r->orig_arfcn_hi << 9) || (r->orig_arfcn_mid << 1) || r->orig_arfcn_lo;
1221                 w[1] = (r->w1_hi << 1) || r->w1_lo;
1222                 if (len >= 4)
1223                         w[2] = r->w2;
1224                 if (len >= 5)
1225                         w[3] = r->w3;
1226                 if (len >= 6)
1227                         w[4] = (r->w4_hi << 5) || r->w4_lo;
1228                 if (len >= 7)
1229                         w[5] = (r->w5_hi << 3) || r->w5_lo;
1230                 if (len >= 8)
1231                         w[6] = (r->w6_hi << 1) || r->w6_lo;
1232                 if (len >= 8)
1233                         w[7] = r->w7;
1234                 if (len >= 9)
1235                         w[8] = (r->w8_hi << 4) || r->w8_lo;
1236                 if (len >= 10)
1237                         w[9] = (r->w9_hi << 1) || r->w9_lo;
1238                 if (len >= 10)
1239                         w[10] = r->w10;
1240                 if (len >= 11)
1241                         w[11] = (r->w11_hi << 3) || r->w11_lo;
1242                 if (len >= 11)
1243                         w[12] = r->w12;
1244                 if (len >= 12)
1245                         w[13] = r->w13;
1246                 if (len >= 13)
1247                         w[14] = r->w15;
1248                 if (len >= 13)
1249                         w[15] = (r->w14_hi << 2) || r->w14_lo;
1250                 if (len >= 14)
1251                         w[16] = (r->w16_hi << 3) || r->w16_lo;
1252                 if (len >= 14)
1253                         w[17] = r->w17;
1254                 if (len >= 15)
1255                         w[18] = r->w19;
1256                 if (len >= 15)
1257                         w[19] = (r->w18_hi << 3) || r->w18_lo;
1258                 if (len >= 16)
1259                         w[20] = (r->w20_hi << 3) || r->w20_lo;
1260                 if (len >= 16)
1261                         w[21] = r->w21;
1262                 if (w[0])
1263                         f[w[0]].mask |= frqt;
1264                 if (w[1])
1265                         f[(w[0] + w[1]) % 1024].mask |= frqt;
1266                 if (w[2])
1267                         f[(w[0] + ((w[1] - 128 + w[2] - 1) % 255) + 1) % 1024].mask |= frqt;
1268                 if (w[3])
1269                         f[(w[0] + ((w[1]       + w[3] - 1) % 255) + 1) % 1024].mask |= frqt;
1270                 if (w[4])
1271                         f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + w[4] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
1272                 if (w[5])
1273                         f[(w[0] + ((w[1]       + ((w[3] - 64 + w[5] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
1274                 if (w[6])
1275                         f[(w[0] + ((w[1] - 128 + ((w[2]      + w[6] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
1276                 if (w[7])
1277                         f[(w[0] + ((w[1]       + ((w[3]      + w[7] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
1278                 if (w[8])
1279                         f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4] - 32 + w[8] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1280                 if (w[9])
1281                         f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5] - 32 + w[9] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1282                 if (w[10])
1283                         f[(w[0] + ((w[1] - 128 + ((w[2]      + ((w[6] - 32 + w[10] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1284                 if (w[11])
1285                         f[(w[0] + ((w[1]       + ((w[3]      + ((w[7] - 32 + w[11] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1286                 if (w[12])
1287                         f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4]      + w[12] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1288                 if (w[13])
1289                         f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5]      + w[13] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1290                 if (w[14])
1291                         f[(w[0] + ((w[1] - 128 + ((w[2]      + ((w[6]      + w[14] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1292                 if (w[15])
1293                         f[(w[0] + ((w[1]       + ((w[3]      + ((w[7]      + w[15] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1294                 if (w[16])
1295                         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;
1296                 if (w[17])
1297                         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;
1298                 if (w[18])
1299                         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;
1300                 if (w[19])
1301                         f[(w[0] + ((w[1]       + ((w[3]      + ((w[7] - 32 + ((w[11] - 16 + w[19] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1302                 if (w[20])
1303                         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;
1304                 if (w[21])
1305                         f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5]      + ((w[13] - 16 + w[21] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1306
1307                 return 0;
1308         }
1309         /* 10..110. */
1310         if ((cd[0] & 0xce & mask) == 0x8c) {
1311                 /* Range 128 format */
1312                 uint16_t w[29]; /* 1..28 */
1313                 struct gsm48_range_128 *r = (struct gsm48_range_128 *)cd;
1314
1315                 if (len < 3)
1316                         return -EINVAL;
1317                 memset(w, 0, sizeof(w));
1318                 w[0] = (r->orig_arfcn_hi << 9) || (r->orig_arfcn_mid << 1) || r->orig_arfcn_lo;
1319                 w[1] = r->w1;
1320                 if (len >= 4)
1321                         w[2] = r->w2;
1322                 if (len >= 5)
1323                         w[3] = (r->w3_hi << 4) || r->w3_lo;
1324                 if (len >= 6)
1325                         w[4] = (r->w4_hi << 1) || r->w4_lo;
1326                 if (len >= 6)
1327                         w[5] = r->w5;
1328                 if (len >= 7)
1329                         w[6] = (r->w6_hi << 3) || r->w6_lo;
1330                 if (len >= 7)
1331                         w[7] = r->w7;
1332                 if (len >= 8)
1333                         w[8] = r->w8;
1334                 if (len >= 8)
1335                         w[9] = r->w9;
1336                 if (len >= 9)
1337                         w[10] = r->w10;
1338                 if (len >= 9)
1339                         w[11] = r->w11;
1340                 if (len >= 10)
1341                         w[12] = r->w12;
1342                 if (len >= 10)
1343                         w[13] = r->w13;
1344                 if (len >= 11)
1345                         w[14] = r->w14;
1346                 if (len >= 11)
1347                         w[15] = r->w15;
1348                 if (len >= 12)
1349                         w[16] = r->w16;
1350                 if (len >= 12)
1351                         w[17] = r->w17;
1352                 if (len >= 13)
1353                         w[18] = (r->w18_hi << 1) || r->w18_lo;
1354                 if (len >= 13)
1355                         w[19] = r->w19;
1356                 if (len >= 13)
1357                         w[20] = r->w20;
1358                 if (len >= 14)
1359                         w[21] = (r->w21_hi << 2) || r->w21_lo;
1360                 if (len >= 14)
1361                         w[22] = r->w22;
1362                 if (len >= 14)
1363                         w[23] = r->w23;
1364                 if (len >= 15)
1365                         w[24] = r->w24;
1366                 if (len >= 15)
1367                         w[25] = r->w25;
1368                 if (len >= 16)
1369                         w[26] = (r->w26_hi << 1) || r->w26_lo;
1370                 if (len >= 16)
1371                         w[27] = r->w27;
1372                 if (len >= 16)
1373                         w[28] = r->w28;
1374                 if (w[0])
1375                         f[w[0]].mask |= frqt;
1376                 if (w[1])
1377                         f[(w[0] + w[1]) % 1024].mask |= frqt;
1378                 if (w[2])
1379                         f[(w[0] + ((w[1] - 64 + w[2] - 1) % 127) + 1) % 1024].mask |= frqt;
1380                 if (w[3])
1381                         f[(w[0] + ((w[1]      + w[3] - 1) % 127) + 1) % 1024].mask |= frqt;
1382                 if (w[4])
1383                         f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + w[4] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
1384                 if (w[5])
1385                         f[(w[0] + ((w[1]      + ((w[3] - 32 + w[5] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
1386                 if (w[6])
1387                         f[(w[0] + ((w[1] - 64 + ((w[2]      + w[6] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
1388                 if (w[7])
1389                         f[(w[0] + ((w[1]      + ((w[3]      + w[7] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
1390                 if (w[8])
1391                         f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4] - 16 + w[8] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1392                 if (w[9])
1393                         f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5] - 16 + w[9] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1394                 if (w[10])
1395                         f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6] - 16 + w[10] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1396                 if (w[11])
1397                         f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + w[11] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1398                 if (w[12])
1399                         f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4]      + w[12] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1400                 if (w[13])
1401                         f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5]      + w[13] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1402                 if (w[14])
1403                         f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6]      + w[14] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1404                 if (w[15])
1405                         f[(w[0] + ((w[1]      + ((w[3]      + ((w[7]      + w[15] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1406                 if (w[16])
1407                         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;
1408                 if (w[17])
1409                         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;
1410                 if (w[18])
1411                         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;
1412                 if (w[19])
1413                         f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + ((w[11] - 8 + w[19] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1414                 if (w[20])
1415                         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;
1416                 if (w[21])
1417                         f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5]      + ((w[13] - 8 + w[21] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1418                 if (w[22])
1419                         f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6]      + ((w[14] - 8 + w[22] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1420                 if (w[23])
1421                         f[(w[0] + ((w[1]      + ((w[3]      + ((w[7]      + ((w[15] - 8 + w[23] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1422                 if (w[24])
1423                         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;
1424                 if (w[25])
1425                         f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5] - 16 + ((w[9]     + w[25] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1426                 if (w[26])
1427                         f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6] - 16 + ((w[10]     + w[26] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1428                 if (w[27])
1429                         f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + ((w[11]     + w[27] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1430                 if (w[28])
1431                         f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4]      + ((w[12]     + w[28] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1432
1433                 return 0;
1434         }
1435         /* 10..111. */
1436         if ((cd[0] & 0xce & mask) == 0x8e) {
1437                 /* Variable bitmap format (can be any length >= 3) */
1438                 uint16_t orig = 0;
1439                 struct gsm48_var_bit *r = (struct gsm48_var_bit *)cd;
1440
1441                 if (len < 3)
1442                         return -EINVAL;
1443                 orig = (r->orig_arfcn_hi << 9) || (r->orig_arfcn_mid << 1) || r->orig_arfcn_lo;
1444                 f[orig].mask |= frqt;
1445                 for (i = 1; 2 + (i >> 3) < len; i++)
1446                         if ((cd[2 + (i >> 3)] & (0x80 >> (i & 7))))
1447                                 f[(orig + 1) % 1024].mask |= frqt;
1448
1449                 return 0;
1450         }
1451
1452         return 0;
1453 }
1454
1455 /* decode "Cell Selection Parameters" (10.5.2.4) */
1456 static int gsm48_decode_cell_sel_param(struct gsm48_sysinfo *s,
1457         struct gsm48_cell_sel_par *cs)
1458 {
1459 #ifdef TODO
1460         convert ms_txpwr_max_ccch dependant on the current frequenc and support
1461         to the right powe level
1462 #endif
1463         s->ms_txpwr_max_ccch = cs->ms_txpwr_max_ccch;
1464         s->cell_resel_hyst_db = cs->cell_resel_hyst * 2;
1465         s->rxlev_acc_min_db = cs->rxlev_acc_min - 110;
1466         s->neci = cs->neci;
1467         s->acs = cs->acs;
1468
1469         return 0;
1470 }
1471
1472 /* decode "Cell Options (BCCH)" (10.5.2.3) */
1473 static int gsm48_decode_cellopt_bcch(struct gsm48_sysinfo *s,
1474         struct gsm48_cell_options *co)
1475 {
1476         s->bcch_radio_link_timeout = (co->radio_link_timeout + 1) * 4;
1477         s->bcch_dtx = co->dtx;
1478         s->bcch_pwrc = co->pwrc;
1479
1480         return 0;
1481 }
1482
1483 /* decode "Cell Options (SACCH)" (10.5.2.3a) */
1484 static int gsm48_decode_cellopt_sacch(struct gsm48_sysinfo *s,
1485         struct gsm48_cell_options *co)
1486 {
1487         s->sacch_radio_link_timeout = (co->radio_link_timeout + 1) * 4;
1488         s->sacch_dtx = co->dtx;
1489         s->sacch_pwrc = co->pwrc;
1490
1491         return 0;
1492 }
1493
1494 /* decode "Control Channel Description" (10.5.2.11) */
1495 static int gsm48_decode_ccd(struct gsm48_sysinfo *s,
1496         struct gsm48_control_channel_descr *cc)
1497 {
1498         s->ccch_conf = cc->ccch_conf;
1499         s->bs_ag_blks_res = cc->bs_ag_blks_res;
1500         s->att_allowed = cc->att;
1501         s->pag_mf_periods = cc->bs_pa_mfrms + 2;
1502         s->t3212 = cc->t3212 * 360; /* convert deci-hours to seconds */
1503
1504         return 0;
1505 }
1506
1507 /* decode "Mobile Allocation" (10.5.2.21) */
1508 static int gsm48_decode_mobile_alloc(struct gsm48_sysinfo *s,
1509         uint8_t *ma, uint8_t len)
1510 {
1511         int i, j = 0;
1512         uint16_t f[len << 3];
1513
1514         /* not more than 64 hopping indexes allowed in IE */
1515         if (len > 8)
1516                 return -EINVAL;
1517
1518         /* tabula rasa */
1519         s->hopp_len = 0;
1520         for (i = 0; i < 1024; i++)
1521                 s->freq[i].mask &= ~FREQ_TYPE_HOPP;
1522
1523         /* generating list of all frequencies (1..1023,0) */
1524         for (i = 1; i <= 1024; i++) {
1525                 if ((s->freq[i & 1023].mask & FREQ_TYPE_SERV)) {
1526                         f[j++] = i & 1023;
1527                         if (j == (len << 3))
1528                                 break;
1529                 }
1530         }
1531
1532         /* fill hopping table with frequency index given by IE
1533          * and set hopping type bits
1534          */
1535         for (i = 0; i < (len << 3); i++) {
1536                 /* if bit is set, this frequency index is used for hopping */
1537                 if ((ma[len - 1 - (i >> 3)] & (1 << (i & 7)))) {
1538                         /* index higher than entries in list ? */
1539                         if (i >= j) {
1540                                 LOGP(DRR, LOGL_NOTICE, "Mobile Allocation "
1541                                         "hopping index %d exceeds maximum "
1542                                         "number of cell frequencies. (%d)\n",
1543                                         i + 1, j);
1544                                 break;
1545                         }
1546                         s->hopping[s->hopp_len++] = f[i];
1547                         s->freq[f[i]].mask |= FREQ_TYPE_HOPP;
1548                 }
1549         }
1550
1551         return 0;
1552 }
1553
1554 /* Rach Control decode tables */
1555 static uint8_t gsm48_max_retrans[4] = {
1556         1, 2, 4, 7
1557 };
1558 static uint8_t gsm48_tx_integer[16] = {
1559         3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 20, 25, 32, 50
1560 };
1561
1562 /* decode "RACH Control Parameter" (10.5.2.29) */
1563 static int gsm48_decode_rach_ctl_param(struct gsm48_sysinfo *s,
1564         struct gsm48_rach_control *rc)
1565 {
1566         s->reest_denied = rc->re;
1567         s->cell_barr = rc->cell_bar;
1568         s->tx_integer = gsm48_tx_integer[rc->tx_integer];
1569         s->max_retrans = gsm48_max_retrans[rc->max_trans];
1570         s->class_barr = (rc->t2 << 8) | rc->t3;
1571
1572         return 0;
1573 }
1574 static int gsm48_decode_rach_ctl_neigh(struct gsm48_sysinfo *s,
1575         struct gsm48_rach_control *rc)
1576 {
1577         s->nb_reest_denied = rc->re;
1578         s->nb_cell_barr = rc->cell_bar;
1579         s->nb_tx_integer = gsm48_tx_integer[rc->tx_integer];
1580         s->nb_max_retrans = gsm48_max_retrans[rc->max_trans];
1581         s->nb_class_barr = (rc->t2 << 8) | rc->t3;
1582
1583         return 0;
1584 }
1585
1586 /* decode "SI 1 Rest Octets" (10.5.2.32) */
1587 static int gsm48_decode_si1_rest(struct gsm48_sysinfo *s, uint8_t *si,
1588         uint8_t len)
1589 {
1590         return 0;
1591 }
1592
1593 /* decode "SI 3 Rest Octets" (10.5.2.34) */
1594 static int gsm48_decode_si3_rest(struct gsm48_sysinfo *s, uint8_t *si,
1595         uint8_t len)
1596 {
1597         return 0;
1598 }
1599
1600 /* decode "SI 4 Rest Octets" (10.5.2.35) */
1601 static int gsm48_decode_si4_rest(struct gsm48_sysinfo *s, uint8_t *si,
1602         uint8_t len)
1603 {
1604         return 0;
1605 }
1606
1607 /* decode "SI 6 Rest Octets" (10.5.2.35a) */
1608 static int gsm48_decode_si6_rest(struct gsm48_sysinfo *s, uint8_t *si,
1609         uint8_t len)
1610 {
1611         return 0;
1612 }
1613
1614 /* send sysinfo event to other layers */
1615 static int gsm48_send_sysinfo(struct osmocom_ms *ms, uint8_t type)
1616 {
1617         struct msgb *nmsg;
1618         struct gsm322_msg *em;
1619
1620         nmsg = gsm322_msgb_alloc(GSM322_EVENT_SYSINFO);
1621         if (!nmsg)
1622                 return -ENOMEM;
1623         em = (struct gsm322_msg *) nmsg->data;
1624         em->sysinfo = type;
1625         gsm322_cs_sendmsg(ms, nmsg);
1626
1627         /* send timer info to location update process */
1628         nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_SYSINFO);
1629         if (!nmsg)
1630                 return -ENOMEM;
1631         gsm48_mmevent_msg(ms, nmsg);
1632
1633         return 0;
1634 }
1635
1636 /* receive "SYSTEM INFORMATION 1" message (9.1.31) */
1637 static int gsm48_rr_rx_sysinfo1(struct osmocom_ms *ms, struct msgb *msg)
1638 {
1639         struct gsm48_system_information_type_1 *si = msgb_l3(msg);
1640         struct gsm48_sysinfo *s = &ms->sysinfo;
1641         int payload_len = msgb_l3len(msg) - sizeof(*si);
1642
1643         if (payload_len < 0) {
1644                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 1 "
1645                         "message.\n");
1646                 return -EINVAL;
1647         }
1648
1649         if (!memcmp(si, s->si1_msg, MIN(msgb_l3len(msg), sizeof(s->si1_msg))))
1650                 return 0;
1651         memcpy(s->si1_msg, si, MIN(msgb_l3len(msg), sizeof(s->si1_msg)));
1652
1653         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 1\n");
1654
1655         /* Cell Channel Description */
1656         gsm48_decode_freq_list(&ms->support, s->freq,
1657                 si->cell_channel_description,
1658                 sizeof(si->cell_channel_description), 0xce, FREQ_TYPE_SERV);
1659         /* RACH Control Parameter */
1660         gsm48_decode_rach_ctl_param(s, &si->rach_control);
1661         /* SI 1 Rest Octets */
1662         if (payload_len)
1663                 gsm48_decode_si1_rest(s, si->rest_octets, payload_len);
1664
1665         s->si1 = 1;
1666
1667         return gsm48_send_sysinfo(ms, si->header.system_information);
1668 }
1669
1670 /* receive "SYSTEM INFORMATION 2" message (9.1.32) */
1671 static int gsm48_rr_rx_sysinfo2(struct osmocom_ms *ms, struct msgb *msg)
1672 {
1673         struct gsm48_system_information_type_2 *si = msgb_l3(msg);
1674         struct gsm48_sysinfo *s = &ms->sysinfo;
1675         int payload_len = msgb_l3len(msg) - sizeof(*si);
1676
1677         if (payload_len < 0) {
1678                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 2 "
1679                         "message.\n");
1680                 return -EINVAL;
1681         }
1682 //printf("len = %d\n", MIN(msgb_l3len(msg), sizeof(s->si2_msg)));
1683
1684         if (!memcmp(si, s->si2_msg, MIN(msgb_l3len(msg), sizeof(s->si2_msg))))
1685                 return 0;
1686         memcpy(s->si2_msg, si, MIN(msgb_l3len(msg), sizeof(s->si2_msg)));
1687
1688         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 2\n");
1689
1690         /* Neighbor Cell Description */
1691         s->nb_ext_ind_si2 = (si->bcch_frequency_list[0] >> 6) & 1;
1692         s->nb_ba_ind_si2 = (si->bcch_frequency_list[0] >> 5) & 1;
1693         gsm48_decode_freq_list(&ms->support, s->freq, si->bcch_frequency_list,
1694                 sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_NCELL_2);
1695         /* NCC Permitted */
1696         s->nb_ncc_permitted = si->ncc_permitted;
1697         /* RACH Control Parameter */
1698         gsm48_decode_rach_ctl_neigh(s, &si->rach_control);
1699
1700         s->si2 = 1;
1701
1702         return gsm48_send_sysinfo(ms, si->header.system_information);
1703 }
1704
1705 /* receive "SYSTEM INFORMATION 2bis" message (9.1.33) */
1706 static int gsm48_rr_rx_sysinfo2bis(struct osmocom_ms *ms, struct msgb *msg)
1707 {
1708         struct gsm48_system_information_type_2bis *si = msgb_l3(msg);
1709         struct gsm48_sysinfo *s = &ms->sysinfo;
1710         int payload_len = msgb_l3len(msg) - sizeof(*si);
1711
1712         if (payload_len < 0) {
1713                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 2bis "
1714                         "message.\n");
1715                 return -EINVAL;
1716         }
1717
1718         if (!memcmp(si, s->si2b_msg, MIN(msgb_l3len(msg),
1719                         sizeof(s->si2b_msg))))
1720                 return 0;
1721         memcpy(s->si2b_msg, si, MIN(msgb_l3len(msg), sizeof(s->si2b_msg)));
1722
1723         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 2bis\n");
1724
1725         /* Neighbor Cell Description */
1726         s->nb_ext_ind_si2bis = (si->bcch_frequency_list[0] >> 6) & 1;
1727         s->nb_ba_ind_si2bis = (si->bcch_frequency_list[0] >> 5) & 1;
1728         gsm48_decode_freq_list(&ms->support, s->freq,
1729                 si->bcch_frequency_list,
1730                 sizeof(si->bcch_frequency_list), 0x8e,
1731                 FREQ_TYPE_NCELL_2bis);
1732         /* RACH Control Parameter */
1733         gsm48_decode_rach_ctl_neigh(s, &si->rach_control);
1734
1735         s->si2bis = 1;
1736
1737         return gsm48_send_sysinfo(ms, si->header.system_information);
1738 }
1739
1740 /* receive "SYSTEM INFORMATION 2ter" message (9.1.34) */
1741 static int gsm48_rr_rx_sysinfo2ter(struct osmocom_ms *ms, struct msgb *msg)
1742 {
1743         struct gsm48_system_information_type_2ter *si = msgb_l3(msg);
1744         struct gsm48_sysinfo *s = &ms->sysinfo;
1745         int payload_len = msgb_l3len(msg) - sizeof(*si);
1746
1747         if (payload_len < 0) {
1748                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 2ter "
1749                         "message.\n");
1750                 return -EINVAL;
1751         }
1752
1753         if (!memcmp(si, s->si2t_msg, MIN(msgb_l3len(msg),
1754                         sizeof(s->si2t_msg))))
1755                 return 0;
1756         memcpy(s->si2t_msg, si, MIN(msgb_l3len(msg), sizeof(s->si2t_msg)));
1757
1758         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 2ter\n");
1759
1760         /* Neighbor Cell Description 2 */
1761         s->nb_multi_rep_si2ter = (si->ext_bcch_frequency_list[0] >> 6) & 3;
1762         gsm48_decode_freq_list(&ms->support, s->freq,
1763                 si->ext_bcch_frequency_list,
1764                 sizeof(si->ext_bcch_frequency_list), 0x8e,
1765                 FREQ_TYPE_NCELL_2ter);
1766
1767         s->si2ter = 1;
1768
1769         return gsm48_send_sysinfo(ms, si->header.system_information);
1770 }
1771
1772 /* receive "SYSTEM INFORMATION 3" message (9.1.35) */
1773 static int gsm48_rr_rx_sysinfo3(struct osmocom_ms *ms, struct msgb *msg)
1774 {
1775         struct gsm48_system_information_type_3 *si = msgb_l3(msg);
1776         struct gsm48_sysinfo *s = &ms->sysinfo;
1777         int payload_len = msgb_l3len(msg) - sizeof(*si);
1778
1779         if (payload_len < 0) {
1780                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 3 "
1781                         "message.\n");
1782                 return -EINVAL;
1783         }
1784
1785         if (!memcmp(si, s->si3_msg, MIN(msgb_l3len(msg), sizeof(s->si3_msg))))
1786                 return 0;
1787         memcpy(s->si3_msg, si, MIN(msgb_l3len(msg), sizeof(s->si3_msg)));
1788
1789         /* Cell Identity */
1790         s->cell_id = ntohs(si->cell_identity);
1791         /* LAI */
1792         gsm48_decode_lai(&si->lai, &s->mcc, &s->mnc, &s->lac);
1793         /* Control Channel Description */
1794         gsm48_decode_ccd(s, &si->control_channel_desc);
1795         /* Cell Options (BCCH) */
1796         gsm48_decode_cellopt_bcch(s, &si->cell_options);
1797         /* Cell Selection Parameters */
1798         gsm48_decode_cell_sel_param(s, &si->cell_sel_par);
1799         /* RACH Control Parameter */
1800         gsm48_decode_rach_ctl_param(s, &si->rach_control);
1801         /* SI 3 Rest Octets */
1802         if (payload_len >= 4)
1803                 gsm48_decode_si3_rest(s, si->rest_octets, payload_len);
1804
1805         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 3 (mcc %03d mnc %02d "
1806                 "lac 0x%04x)\n", s->mcc, s->mnc, s->lac);
1807
1808         s->si3 = 1;
1809
1810         return gsm48_send_sysinfo(ms, si->header.system_information);
1811 }
1812
1813 /* receive "SYSTEM INFORMATION 4" message (9.1.36) */
1814 static int gsm48_rr_rx_sysinfo4(struct osmocom_ms *ms, struct msgb *msg)
1815 {
1816         /* NOTE: pseudo length is not in this structure, so we skip */
1817         struct gsm48_system_information_type_4 *si = msgb_l3(msg);
1818         struct gsm48_sysinfo *s = &ms->sysinfo;
1819         int payload_len = msgb_l3len(msg) - sizeof(*si);
1820         uint8_t *data = si->data;
1821         struct gsm48_chan_desc *cd;
1822
1823         if (payload_len < 0) {
1824                 short_read:
1825                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 4 "
1826                         "message.\n");
1827                 return -EINVAL;
1828         }
1829
1830         if (!memcmp(si, s->si4_msg, MIN(msgb_l3len(msg), sizeof(s->si4_msg))))
1831                 return 0;
1832         memcpy(s->si4_msg, si, MIN(msgb_l3len(msg), sizeof(s->si4_msg)));
1833
1834         /* LAI */
1835         gsm48_decode_lai(&si->lai, &s->mcc, &s->mnc, &s->lac);
1836         /* Cell Selection Parameters */
1837         gsm48_decode_cell_sel_param(s, &si->cell_sel_par);
1838         /* RACH Control Parameter */
1839         gsm48_decode_rach_ctl_param(s, &si->rach_control);
1840         /* CBCH Channel Description */
1841         if (payload_len >= 1 && data[0] == GSM48_IE_CBCH_CHAN_DESC) {
1842                 if (payload_len < 4)
1843                         goto short_read;
1844                 cd = (struct gsm48_chan_desc *) (data + 1);
1845                 if (cd->h0.h) {
1846                         s->h = 1;
1847                         gsm48_decode_chan_h1(cd, &s->tsc, &s->maio, &s->hsn);
1848                 } else {
1849                         s->h = 0;
1850                         gsm48_decode_chan_h0(cd, &s->tsc, &s->arfcn);
1851                 }
1852                 payload_len -= 4;
1853                 data += 4;
1854         }
1855         /* CBCH Mobile Allocation */
1856         if (payload_len >= 1 && data[0] == GSM48_IE_CBCH_MOB_AL) {
1857                 if (payload_len < 1 || payload_len < 2 + data[1])
1858                         goto short_read;
1859                 gsm48_decode_mobile_alloc(s, data + 2, si->data[1]);
1860                 payload_len -= 2 + data[1];
1861                 data += 2 + data[1];
1862         }
1863         /* SI 4 Rest Octets */
1864         if (payload_len > 0)
1865                 gsm48_decode_si4_rest(s, data, payload_len);
1866
1867         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 4 (mcc %03d mnc %02d "
1868                 "lac 0x%04x)\n", s->mcc, s->mnc, s->lac);
1869
1870         s->si4 = 1;
1871
1872         return gsm48_send_sysinfo(ms, si->header.system_information);
1873 }
1874
1875 /* receive "SYSTEM INFORMATION 5" message (9.1.37) */
1876 static int gsm48_rr_rx_sysinfo5(struct osmocom_ms *ms, struct msgb *msg)
1877 {
1878         /* NOTE: pseudo length is not in this structure, so we skip */
1879         struct gsm48_system_information_type_5 *si = msgb_l3(msg) + 1;
1880         struct gsm48_sysinfo *s = &ms->sysinfo;
1881         int payload_len = msgb_l3len(msg) - sizeof(*si) - 1;
1882
1883         if (payload_len < 0) {
1884                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 5 "
1885                         "message.\n");
1886                 return -EINVAL;
1887         }
1888
1889         if (!memcmp(si, s->si5_msg, MIN(msgb_l3len(msg), sizeof(s->si5_msg))))
1890                 return 0;
1891         memcpy(s->si5_msg, si, MIN(msgb_l3len(msg), sizeof(s->si5_msg)));
1892
1893         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 5\n");
1894
1895         /* Neighbor Cell Description */
1896         s->nb_ext_ind_si5 = (si->bcch_frequency_list[0] >> 6) & 1;
1897         s->nb_ba_ind_si5 = (si->bcch_frequency_list[0] >> 5) & 1;
1898         gsm48_decode_freq_list(&ms->support, s->freq, si->bcch_frequency_list,
1899                 sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_REP_5);
1900
1901         s->si5 = 1;
1902
1903         return gsm48_send_sysinfo(ms, si->system_information);
1904 }
1905
1906 /* receive "SYSTEM INFORMATION 5bis" message (9.1.38) */
1907 static int gsm48_rr_rx_sysinfo5bis(struct osmocom_ms *ms, struct msgb *msg)
1908 {
1909         /* NOTE: pseudo length is not in this structure, so we skip */
1910         struct gsm48_system_information_type_5bis *si = msgb_l3(msg) + 1;
1911         struct gsm48_sysinfo *s = &ms->sysinfo;
1912         int payload_len = msgb_l3len(msg) - sizeof(*si) - 1;
1913
1914         if (payload_len < 0) {
1915                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 5bis "
1916                         "message.\n");
1917                 return -EINVAL;
1918         }
1919
1920         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 5bis\n");
1921
1922         if (!memcmp(si, s->si5b_msg, MIN(msgb_l3len(msg),
1923                         sizeof(s->si5b_msg))))
1924                 return 0;
1925         memcpy(s->si5b_msg, si, MIN(msgb_l3len(msg), sizeof(s->si5b_msg)));
1926
1927         /* Neighbor Cell Description */
1928         s->nb_ext_ind_si5bis = (si->bcch_frequency_list[0] >> 6) & 1;
1929         s->nb_ba_ind_si5bis = (si->bcch_frequency_list[0] >> 5) & 1;
1930         gsm48_decode_freq_list(&ms->support, s->freq, si->bcch_frequency_list,
1931                 sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_REP_5bis);
1932
1933         s->si5bis = 1;
1934
1935         return gsm48_send_sysinfo(ms, si->system_information);
1936 }
1937
1938 /* receive "SYSTEM INFORMATION 5ter" message (9.1.39) */
1939 static int gsm48_rr_rx_sysinfo5ter(struct osmocom_ms *ms, struct msgb *msg)
1940 {
1941         /* NOTE: pseudo length is not in this structure, so we skip */
1942         struct gsm48_system_information_type_5ter *si = msgb_l3(msg) + 1;
1943         struct gsm48_sysinfo *s = &ms->sysinfo;
1944         int payload_len = msgb_l3len(msg) - sizeof(*si) - 1;
1945
1946         if (payload_len < 0) {
1947                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 5ter "
1948                         "message.\n");
1949                 return -EINVAL;
1950         }
1951
1952         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 5ter\n");
1953
1954         if (!memcmp(si, s->si5t_msg, MIN(msgb_l3len(msg),
1955                         sizeof(s->si5t_msg))))
1956                 return 0;
1957         memcpy(s->si5t_msg, si, MIN(msgb_l3len(msg), sizeof(s->si5t_msg)));
1958
1959         /* Neighbor Cell Description */
1960         gsm48_decode_freq_list(&ms->support, s->freq, si->bcch_frequency_list,
1961                 sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_REP_5ter);
1962
1963         s->si5ter = 1;
1964
1965         return gsm48_send_sysinfo(ms, si->system_information);
1966 }
1967
1968 /* receive "SYSTEM INFORMATION 6" message (9.1.39) */
1969 static int gsm48_rr_rx_sysinfo6(struct osmocom_ms *ms, struct msgb *msg)
1970 {
1971         /* NOTE: pseudo length is not in this structure, so we skip */
1972         struct gsm48_system_information_type_6 *si = msgb_l3(msg) + 1;
1973         struct gsm48_sysinfo *s = &ms->sysinfo;
1974         int payload_len = msgb_l3len(msg) - sizeof(*si) - 1;
1975
1976         if (payload_len < 0) {
1977                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 6 "
1978                         "message.\n");
1979                 return -EINVAL;
1980         }
1981
1982         if (!memcmp(si, s->si6_msg, MIN(msgb_l3len(msg), sizeof(s->si6_msg))))
1983                 return 0;
1984         memcpy(s->si6_msg, si, MIN(msgb_l3len(msg), sizeof(s->si6_msg)));
1985
1986         /* Cell Identity */
1987         if (s->si6 && s->cell_id != ntohs(si->cell_identity))
1988                 LOGP(DRR, LOGL_INFO, "Cell ID on SI 6 differs from previous "
1989                         "read.\n");
1990         s->cell_id = ntohs(si->cell_identity);
1991         /* LAI */
1992         gsm48_decode_lai(&si->lai, &s->mcc, &s->mnc, &s->lac);
1993         /* Cell Options (SACCH) */
1994         gsm48_decode_cellopt_sacch(s, &si->cell_options);
1995         /* NCC Permitted */
1996         s->nb_ncc_permitted = si->ncc_permitted;
1997         /* SI 6 Rest Octets */
1998         if (payload_len >= 4)
1999                 gsm48_decode_si6_rest(s, si->rest_octets, payload_len);
2000
2001         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 6 (mcc %03d mnc %02d "
2002                 "lac 0x%04x)\n", s->mcc, s->mnc, s->lac);
2003
2004         s->si6 = 1;
2005
2006         return gsm48_send_sysinfo(ms, si->system_information);
2007 }
2008
2009 /*
2010  * paging
2011  */
2012
2013 /* paging channel request */
2014 static int gsm48_rr_chan2cause[4] = {
2015         RR_EST_CAUSE_ANS_PAG_ANY,
2016         RR_EST_CAUSE_ANS_PAG_SDCCH,
2017         RR_EST_CAUSE_ANS_PAG_TCH_F,
2018         RR_EST_CAUSE_ANS_PAG_TCH_ANY
2019 };
2020
2021 /* given LV of mobile identity is checked agains ms */
2022 static int gsm_match_mi(struct osmocom_ms *ms, uint8_t *mi)
2023 {
2024         char imsi[16];
2025         uint32_t tmsi;
2026         uint8_t mi_type;
2027
2028         if (mi[0] < 1)
2029                 return 0;
2030         mi_type = mi[1] & GSM_MI_TYPE_MASK;
2031         switch (mi_type) {
2032         case GSM_MI_TYPE_TMSI:
2033                 if (mi[0] < 5)
2034                         return 0;
2035                 memcpy(&tmsi, mi+2, 4);
2036                 if (ms->subscr.tmsi == ntohl(tmsi)
2037                  && ms->subscr.tmsi_valid) {
2038                         LOGP(DRR, LOGL_INFO, "TMSI %08x matches\n", ntohl(tmsi));
2039
2040                         return 1;
2041                 } else
2042                         LOGP(DRR, LOGL_INFO, "TMSI %08x (not for us)\n",
2043                                 ntohl(tmsi));
2044                 break;
2045         case GSM_MI_TYPE_IMSI:
2046                 gsm48_mi_to_string(imsi, sizeof(imsi), mi + 1, mi[0]);
2047                 if (!strcmp(imsi, ms->subscr.imsi)) {
2048                         LOGP(DRR, LOGL_INFO, "IMSI %s matches\n", imsi);
2049
2050                         return 1;
2051                 } else
2052                         LOGP(DRR, LOGL_INFO, "IMSI %s (not for us)\n", imsi);
2053                 break;
2054         default:
2055                 LOGP(DRR, LOGL_NOTICE, "Paging with unsupported MI type %d.\n",
2056                         mi_type);
2057         }
2058
2059         return 0;
2060 }
2061
2062 /* 9.1.22 PAGING REQUEST 1 message received */
2063 static int gsm48_rr_rx_pag_req_1(struct osmocom_ms *ms, struct msgb *msg)
2064 {
2065         struct gsm48_rrlayer *rr = &ms->rrlayer;
2066         struct gsm322_cellsel *cs = &ms->cellsel;
2067         struct gsm48_paging1 *pa = msgb_l3(msg);
2068         int payload_len = msgb_l3len(msg) - sizeof(*pa);
2069         int chan_1, chan_2;
2070         uint8_t *mi;
2071
2072         /* 3.3.1.1.2: ignore paging while not camping on a cell */
2073         if (rr->state != GSM48_RR_ST_IDLE
2074          && cs->state != GSM322_C3_CAMPED_NORMALLY) {
2075                 LOGP(DRR, LOGL_INFO, "PAGING ignored, we are not camping "
2076                         "normally.\n");
2077                 return 0;
2078         }
2079
2080         if (payload_len < 2) {
2081                 short_read:
2082                 LOGP(DRR, LOGL_NOTICE, "Short read of PAGING REQUEST 1 "
2083                         "message.\n");
2084                 return -EINVAL;
2085         }
2086
2087         /* channel needed */
2088         chan_1 = pa->cneed1;
2089         chan_2 = pa->cneed2;
2090         /* first MI */
2091         mi = pa->data + 1;
2092         if (payload_len < mi[0] + 1)
2093                 goto short_read;
2094         if (gsm_match_mi(ms, mi) > 0)
2095                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_1], 1);
2096         /* second MI */
2097         payload_len -= mi[0] + 1;
2098         mi = pa->data + mi[0] + 1;
2099         if (payload_len < 2)
2100                 return 0;
2101         if (mi[0] != GSM48_IE_MOBILE_ID)
2102                 return 0;
2103         if (payload_len < mi[1] + 2)
2104                 goto short_read;
2105         if (gsm_match_mi(ms, mi + 1) > 0)
2106                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_2], 1);
2107
2108         return 0;
2109 }
2110
2111 /* 9.1.23 PAGING REQUEST 2 message received */
2112 static int gsm48_rr_rx_pag_req_2(struct osmocom_ms *ms, struct msgb *msg)
2113 {
2114         struct gsm48_rrlayer *rr = &ms->rrlayer;
2115         struct gsm322_cellsel *cs = &ms->cellsel;
2116         struct gsm48_paging2 *pa = msgb_l3(msg);
2117         int payload_len = msgb_l3len(msg) - sizeof(*pa);
2118         uint8_t *mi;
2119         int chan_1, chan_2, chan_3;
2120
2121         /* 3.3.1.1.2: ignore paging while not camping on a cell */
2122         if (rr->state != GSM48_RR_ST_IDLE
2123          && cs->state != GSM322_C3_CAMPED_NORMALLY) {
2124                 LOGP(DRR, LOGL_INFO, "PAGING ignored, we are not camping "
2125                         "normally.\n");
2126                 return 0;
2127         }
2128
2129         if (payload_len < 0) {
2130                 short_read:
2131                 LOGP(DRR, LOGL_NOTICE, "Short read of PAGING REQUEST 2 "
2132                         "message .\n");
2133                 return -EINVAL;
2134         }
2135
2136         /* channel needed */
2137         chan_1 = pa->cneed1;
2138         chan_2 = pa->cneed2;
2139         /* first MI */
2140         if (ms->subscr.tmsi == ntohl(pa->tmsi1)
2141          && ms->subscr.tmsi_valid)
2142                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_1], 1);
2143         /* second MI */
2144         if (ms->subscr.tmsi == ntohl(pa->tmsi2)
2145          && ms->subscr.tmsi_valid)
2146                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_2], 1);
2147         /* third MI */
2148         mi = pa->data;
2149         if (payload_len < 2)
2150                 return 0;
2151         if (mi[0] != GSM48_IE_MOBILE_ID)
2152                 return 0;
2153         if (payload_len < mi[1] + 2 + 1) /* must include "channel needed" */
2154                 goto short_read;
2155         chan_3 = mi[mi[1] + 2] & 0x03; /* channel needed */
2156         if (gsm_match_mi(ms, mi + 1) > 0)
2157                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_3], 1);
2158
2159         return 0;
2160 }
2161
2162 /* 9.1.24 PAGING REQUEST 3 message received */
2163 static int gsm48_rr_rx_pag_req_3(struct osmocom_ms *ms, struct msgb *msg)
2164 {
2165         struct gsm48_rrlayer *rr = &ms->rrlayer;
2166         struct gsm322_cellsel *cs = &ms->cellsel;
2167         struct gsm48_paging3 *pa = msgb_l3(msg);
2168         int payload_len = msgb_l3len(msg) - sizeof(*pa);
2169         int chan_1, chan_2, chan_3, chan_4;
2170
2171         /* 3.3.1.1.2: ignore paging while not camping on a cell */
2172         if (rr->state != GSM48_RR_ST_IDLE
2173          && cs->state != GSM322_C3_CAMPED_NORMALLY) {
2174                 LOGP(DRR, LOGL_INFO, "PAGING ignored, we are not camping "
2175                         "normally.\n");
2176                 return 0;
2177         }
2178
2179         if (payload_len < 0) { /* must include "channel needed", part of *pa */
2180                 LOGP(DRR, LOGL_NOTICE, "Short read of PAGING REQUEST 3 "
2181                         "message .\n");
2182                 return -EINVAL;
2183         }
2184
2185         /* channel needed */
2186         chan_1 = pa->cneed1;
2187         chan_2 = pa->cneed2;
2188         chan_3 = pa->cneed3;
2189         chan_4 = pa->cneed4;
2190         /* first MI */
2191         if (ms->subscr.tmsi == ntohl(pa->tmsi1)
2192          && ms->subscr.tmsi_valid)
2193                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_1], 1);
2194         /* second MI */
2195         if (ms->subscr.tmsi == ntohl(pa->tmsi2)
2196          && ms->subscr.tmsi_valid)
2197                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_2], 1);
2198         /* thrid MI */
2199         if (ms->subscr.tmsi == ntohl(pa->tmsi3)
2200          && ms->subscr.tmsi_valid)
2201                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_3], 1);
2202         /* fourth MI */
2203         if (ms->subscr.tmsi == ntohl(pa->tmsi4)
2204          && ms->subscr.tmsi_valid)
2205                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_4], 1);
2206
2207         return 0;
2208 }
2209
2210 /*
2211  * (immediate) assignment
2212  */
2213
2214 /* match request reference agains request history */
2215 static int gsm48_match_ra(struct osmocom_ms *ms, struct gsm48_req_ref *ref)
2216 {
2217         struct gsm48_rrlayer *rr = &ms->rrlayer;
2218         int i;
2219
2220         for (i = 0; i < 3; i++) {
2221                 if (rr->cr_hist[i] >= 0
2222                  && ref->ra == rr->cr_hist[i]) {
2223                         LOGP(DRR, LOGL_INFO, "request %02x matches\n", ref->ra);
2224                         // todo: match timeslot
2225                         return 1;
2226                 }
2227         }
2228
2229         return 0;
2230 }
2231
2232 /* 9.1.3 sending ASSIGNMENT COMPLETE */
2233 static int gsm48_rr_tx_ass_cpl(struct osmocom_ms *ms, uint8_t cause)
2234 {
2235         struct msgb *nmsg;
2236         struct gsm48_hdr *gh;
2237         struct gsm48_ass_cpl *ac;
2238
2239         LOGP(DRR, LOGL_INFO, "ASSIGNMENT COMPLETE (cause #%d)\n", cause);
2240
2241         nmsg = gsm48_l3_msgb_alloc();
2242         if (!nmsg)
2243                 return -ENOMEM;
2244         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
2245         ac = (struct gsm48_ass_cpl *) msgb_put(nmsg, sizeof(*ac));
2246
2247         gh->proto_discr = GSM48_PDISC_RR;
2248         gh->msg_type = GSM48_MT_RR_ASS_COMPL;
2249
2250         /* RR_CAUSE */
2251         ac->rr_cause = cause;
2252
2253         return gsm48_send_rsl(ms, RSL_MT_DATA_REQ, nmsg);
2254 }
2255
2256 /* 9.1.4 sending ASSIGNMENT FAILURE */
2257 static int gsm48_rr_tx_ass_fail(struct osmocom_ms *ms, uint8_t cause)
2258 {
2259         struct msgb *nmsg;
2260         struct gsm48_hdr *gh;
2261         struct gsm48_ass_fail *af;
2262
2263         LOGP(DRR, LOGL_INFO, "ASSIGNMENT FAILURE (cause #%d)\n", cause);
2264
2265         nmsg = gsm48_l3_msgb_alloc();
2266         if (!nmsg)
2267                 return -ENOMEM;
2268         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
2269         af = (struct gsm48_ass_fail *) msgb_put(nmsg, sizeof(*af));
2270
2271         gh->proto_discr = GSM48_PDISC_RR;
2272         gh->msg_type = GSM48_MT_RR_ASS_COMPL;
2273
2274         /* RR_CAUSE */
2275         af->rr_cause = cause;
2276
2277         return gsm48_send_rsl(ms, RSL_MT_DATA_REQ, nmsg);
2278 }
2279
2280 /* 9.1.18 IMMEDIATE ASSIGNMENT is received */
2281 static int gsm48_rr_rx_imm_ass(struct osmocom_ms *ms, struct msgb *msg)
2282 {
2283         struct gsm48_rrlayer *rr = &ms->rrlayer;
2284         struct gsm48_imm_ass *ia = msgb_l3(msg);
2285         int ma_len = msgb_l3len(msg) - sizeof(*ia);
2286         uint8_t ch_type, ch_subch, ch_ts;
2287         struct gsm48_rr_cd cd;
2288         uint8_t *st, st_len;
2289
2290         memset(&cd, 0, sizeof(cd));
2291
2292         if (ma_len < 0 /* mobile allocation IE must be included */
2293          || ia->mob_alloc_len > ma_len) { /* short read of IE */
2294                 LOGP(DRR, LOGL_NOTICE, "Short read of IMMEDIATE ASSIGNMENT "
2295                         "message.\n");
2296                 return -EINVAL;
2297         }
2298         if (ia->mob_alloc_len > 8) {
2299                 LOGP(DRR, LOGL_NOTICE, "Moble allocation in IMMEDIATE "
2300                         "ASSIGNMENT too large.\n");
2301                 return -EINVAL;
2302         }
2303
2304         /* starting time */
2305         st_len = ma_len - ia->mob_alloc_len;
2306         st = ia->mob_alloc + ia->mob_alloc_len;
2307         if (st_len >= 3 && st[0] == GSM48_IE_START_TIME)
2308                 gsm48_decode_start_time(&cd, (struct gsm48_start_time *)(st+1));
2309
2310         /* decode channel description */
2311         rsl_dec_chan_nr(ia->chan_desc.chan_nr, &ch_type, &ch_subch, &ch_ts);
2312         if (ia->chan_desc.h0.h) {
2313                 cd.h = 1;
2314                 gsm48_decode_chan_h1(&ia->chan_desc, &cd.tsc, &cd.maio,
2315                         &cd.hsn);
2316                 LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT (ra=0x%02x, "
2317                         "chan_nr=0x%02x, MAIO=%u, HSN=%u, TS=%u, SS=%u, "
2318                         "TSC=%u)\n", ia->req_ref.ra, ia->chan_desc.chan_nr,
2319                         cd.maio, cd.hsn, ch_ts, ch_subch, cd.tsc);
2320         } else {
2321                 cd.h = 0;
2322                 gsm48_decode_chan_h0(&ia->chan_desc, &cd.tsc, &cd.arfcn);
2323                 LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT (ra=0x%02x, "
2324                         "chan_nr=0x%02x, ARFCN=%u, TS=%u, SS=%u, TSC=%u)\n",
2325                         ia->req_ref.ra, ia->chan_desc.chan_nr, cd.arfcn,
2326                         ch_ts, ch_subch, cd.tsc);
2327         }
2328
2329         /* 3.3.1.1.2: ignore assignment while idle */
2330         if (rr->state != GSM48_RR_ST_CONN_PEND || !rr->wait_assign) {
2331                 LOGP(DRR, LOGL_INFO, "no previous request\n");
2332                 return 0;
2333         }
2334
2335         /* request ref */
2336         if (gsm48_match_ra(ms, &ia->req_ref)) {
2337                 /* channel description */
2338                 memcpy(&rr->cd_now, &cd, sizeof(rr->cd_now));
2339                 /* timing advance */
2340                 rr->cd_now.ta = ia->timing_advance;
2341                 /* mobile allocation */
2342                 memcpy(&rr->cd_now.mob_alloc_lv, &ia->mob_alloc_len, 
2343                         ia->mob_alloc_len + 1);
2344                 rr->wait_assign = 0;
2345                 return gsm48_rr_dl_est(ms);
2346         }
2347         LOGP(DRR, LOGL_INFO, "does not match our request\n");
2348
2349         return 0;
2350 }
2351
2352 /* 9.1.19 IMMEDIATE ASSIGNMENT EXTENDED is received */
2353 static int gsm48_rr_rx_imm_ass_ext(struct osmocom_ms *ms, struct msgb *msg)
2354 {
2355         struct gsm48_rrlayer *rr = &ms->rrlayer;
2356         struct gsm48_imm_ass_ext *ia = msgb_l3(msg);
2357         int ma_len = msgb_l3len(msg) - sizeof(*ia);
2358         uint8_t ch_type, ch_subch, ch_ts;
2359         struct gsm48_rr_cd cd1, cd2;
2360         uint8_t *st, st_len;
2361
2362         memset(&cd1, 0, sizeof(cd1));
2363         memset(&cd2, 0, sizeof(cd2));
2364
2365         if (ma_len < 0 /* mobile allocation IE must be included */
2366          || ia->mob_alloc_len > ma_len) { /* short read of IE */
2367                 LOGP(DRR, LOGL_NOTICE, "Short read of IMMEDIATE ASSIGNMENT "
2368                         "EXTENDED message.\n");
2369                 return -EINVAL;
2370         }
2371         if (ia->mob_alloc_len > 4) {
2372                 LOGP(DRR, LOGL_NOTICE, "Moble allocation in IMMEDIATE "
2373                         "ASSIGNMENT EXTENDED too large.\n");
2374                 return -EINVAL;
2375         }
2376
2377         /* starting time */
2378         st_len = ma_len - ia->mob_alloc_len;
2379         st = ia->mob_alloc + ia->mob_alloc_len;
2380         if (st_len >= 3 && st[0] == GSM48_IE_START_TIME) {
2381                 gsm48_decode_start_time(&cd1,
2382                         (struct gsm48_start_time *)(st+1));
2383                 memcpy(&cd2, &cd1, sizeof(cd2));
2384         }
2385
2386         /* decode channel description */
2387         LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT ");
2388         rsl_dec_chan_nr(ia->chan_desc1.chan_nr, &ch_type, &ch_subch, &ch_ts);
2389         if (ia->chan_desc1.h0.h) {
2390                 cd1.h = 1;
2391                 gsm48_decode_chan_h1(&ia->chan_desc1, &cd1.tsc, &cd1.maio,
2392                         &cd1.hsn);
2393                 LOGP(DRR, LOGL_INFO, "1(ra=0x%02x, "
2394                         "chan_nr=0x%02x, MAIO=%u, HSN=%u, TS=%u, SS=%u, "
2395                         "TSC=%u)\n", ia->req_ref1.ra, ia->chan_desc1.chan_nr,
2396                         cd1.maio, cd1.hsn, ch_ts, ch_subch, cd1.tsc);
2397         } else {
2398                 cd1.h = 0;
2399                 gsm48_decode_chan_h0(&ia->chan_desc1, &cd1.tsc, &cd1.arfcn);
2400                 LOGP(DRR, LOGL_INFO, "1(ra=0x%02x, "
2401                         "chan_nr=0x%02x, ARFCN=%u, TS=%u, SS=%u, TSC=%u)\n",
2402                         ia->req_ref1.ra, ia->chan_desc1.chan_nr, cd1.arfcn,
2403                         ch_ts, ch_subch, cd1.tsc);
2404         }
2405         rsl_dec_chan_nr(ia->chan_desc2.chan_nr, &ch_type, &ch_subch, &ch_ts);
2406         if (ia->chan_desc2.h0.h) {
2407                 cd2.h = 1;
2408                 gsm48_decode_chan_h1(&ia->chan_desc2, &cd1.tsc, &cd1.maio,
2409                         &cd1.hsn);
2410                 LOGP(DRR, LOGL_INFO, "2(ra=0x%02x, "
2411                         "chan_nr=0x%02x, MAIO=%u, HSN=%u, TS=%u, SS=%u, "
2412                         "TSC=%u)\n", ia->req_ref2.ra, ia->chan_desc2.chan_nr,
2413                         cd2.maio, cd2.hsn, ch_ts, ch_subch, cd2.tsc);
2414         } else {
2415                 cd2.h = 0;
2416                 gsm48_decode_chan_h0(&ia->chan_desc2, &cd2.tsc, &cd2.arfcn);
2417                 LOGP(DRR, LOGL_INFO, "2(ra=0x%02x, "
2418                         "chan_nr=0x%02x, ARFCN=%u, TS=%u, SS=%u, TSC=%u)\n",
2419                         ia->req_ref2.ra, ia->chan_desc2.chan_nr, cd2.arfcn,
2420                         ch_ts, ch_subch, cd2.tsc);
2421         }
2422
2423         /* 3.3.1.1.2: ignore assignment while idle */
2424         if (rr->state != GSM48_RR_ST_CONN_PEND || !rr->wait_assign) {
2425                 LOGP(DRR, LOGL_INFO, "no previous request\n");
2426                 return 0;
2427         }
2428
2429         /* request ref 1 */
2430         if (gsm48_match_ra(ms, &ia->req_ref1)) {
2431                 /* channel description */
2432                 memcpy(&rr->cd_now, &cd1, sizeof(rr->cd_now));
2433                 /* timing advance */
2434                 rr->cd_now.ta = ia->timing_advance1;
2435                 /* mobile allocation */
2436                 memcpy(&rr->cd_now.mob_alloc_lv, &ia->mob_alloc_len,
2437                         ia->mob_alloc_len + 1);
2438                 rr->wait_assign = 0;
2439                 return gsm48_rr_dl_est(ms);
2440         }
2441         /* request ref 1 */
2442         if (gsm48_match_ra(ms, &ia->req_ref2)) {
2443                 /* channel description */
2444                 memcpy(&rr->cd_now, &cd2, sizeof(rr->cd_now));
2445                 /* timing advance */
2446                 rr->cd_now.ta = ia->timing_advance2;
2447                 /* mobile allocation */
2448                 memcpy(&rr->cd_now.mob_alloc_lv, &ia->mob_alloc_len,
2449                         ia->mob_alloc_len + 1);
2450                 rr->wait_assign = 0;
2451                 return gsm48_rr_dl_est(ms);
2452         }
2453         LOGP(DRR, LOGL_INFO, "does not match our request\n");
2454
2455         return 0;
2456 }
2457
2458 /* 9.1.20 IMMEDIATE ASSIGNMENT REJECT is received */
2459 static int gsm48_rr_rx_imm_ass_rej(struct osmocom_ms *ms, struct msgb *msg)
2460 {
2461         struct gsm48_rrlayer *rr = &ms->rrlayer;
2462         struct gsm48_imm_ass_rej *ia = msgb_l3(msg);
2463         int payload_len = msgb_l3len(msg) - sizeof(*ia);
2464         int i;
2465         struct gsm48_req_ref *req_ref;
2466         uint8_t t3122_value;
2467
2468         /* 3.3.1.1.2: ignore assignment while idle */
2469         if (rr->state != GSM48_RR_ST_CONN_PEND || !rr->wait_assign)
2470                 return 0;
2471
2472         if (payload_len < 0) {
2473                 LOGP(DRR, LOGL_NOTICE, "Short read of IMMEDIATE ASSIGNMENT "
2474                         "REJECT message.\n");
2475                 return -EINVAL;
2476         }
2477
2478         for (i = 0; i < 4; i++) {
2479                 /* request reference */
2480                 req_ref = (struct gsm48_req_ref *)
2481                                 (((uint8_t *)&ia->req_ref1) + i * 4);
2482                 LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT REJECT "
2483                         "(ref 0x%02x)\n", req_ref->ra);
2484                 if (gsm48_match_ra(ms, req_ref)) {
2485                         /* wait indication */
2486                         t3122_value = *(((uint8_t *)&ia->wait_ind1) + i * 4);
2487                         if (t3122_value)
2488                                 start_rr_t3122(rr, t3122_value, 0);
2489                         /* start timer 3126 if not already */
2490                         if (!bsc_timer_pending(&rr->t3126))
2491                                 start_rr_t3126(rr, 5, 0); /* TODO improve! */
2492                         /* stop assignmnet requests */
2493                         rr->n_chan_req = 0;
2494
2495                         /* wait until timer 3126 expires, then release
2496                          * or wait for channel assignment */
2497                         return 0;
2498                 }
2499         }
2500
2501         return 0;
2502 }
2503
2504 /* 9.1.1 ADDITIONAL ASSIGMENT is received  */
2505 static int gsm48_rr_rx_add_ass(struct osmocom_ms *ms, struct msgb *msg)
2506 {
2507         struct gsm48_hdr *gh = msgb_l3(msg);
2508         struct gsm48_add_ass *aa = (struct gsm48_add_ass *)gh->data;
2509         int payload_len = msgb_l3len(msg) - sizeof(*gh) - sizeof(*aa);
2510         struct tlv_parsed tp;
2511
2512         if (payload_len < 0) {
2513                 LOGP(DRR, LOGL_NOTICE, "Short read of ADDITIONAL ASSIGNMENT "
2514                         "message.\n");
2515                 return gsm48_rr_tx_rr_status(ms,
2516                         GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
2517         }
2518         tlv_parse(&tp, &gsm48_rr_att_tlvdef, aa->data, payload_len, 0, 0);
2519
2520         return gsm48_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
2521 }
2522
2523 /*
2524  * measturement reports
2525  */
2526
2527 static int gsm48_rr_tx_meas_rep(struct osmocom_ms *ms)
2528 {
2529         struct gsm48_rrlayer *rr = &ms->rrlayer;
2530         struct gsm48_rr_meas *meas = &rr->meas;
2531         struct msgb *nmsg;
2532         struct gsm48_hdr *gh;
2533         struct gsm48_meas_res *mr;
2534
2535         nmsg = gsm48_l3_msgb_alloc();
2536         if (!nmsg)
2537                 return -ENOMEM;
2538         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
2539         mr = (struct gsm48_meas_res *) msgb_put(nmsg, sizeof(*mr));
2540
2541         gh->proto_discr = GSM48_PDISC_RR;
2542         gh->msg_type = GSM48_MT_RR_MEAS_REP;
2543
2544         /* measurement results */
2545         mr->rxlev_full = meas->rxlev_full;
2546         mr->rxlev_sub = meas->rxlev_sub;
2547         mr->rxqual_full = meas->rxqual_full;
2548         mr->rxqual_sub = meas->rxqual_sub;
2549         mr->dtx_used = meas->dtx;
2550         mr->ba_used = meas->ba;
2551         mr->meas_valid = meas->meas_valid;
2552         if (meas->ncell_na) {
2553                 /* no results for serving cells */
2554                 mr->no_nc_n_hi = 1;
2555                 mr->no_nc_n_lo = 3;
2556         } else {
2557                 mr->no_nc_n_hi = meas->count >> 2;
2558                 mr->no_nc_n_lo = meas->count & 3;
2559         }
2560         mr->rxlev_nc1 = meas->rxlev_nc[0];
2561         mr->rxlev_nc2_hi = meas->rxlev_nc[1] >> 1;
2562         mr->rxlev_nc2_lo = meas->rxlev_nc[1] & 1;
2563         mr->rxlev_nc3_hi = meas->rxlev_nc[2] >> 2;
2564         mr->rxlev_nc3_lo = meas->rxlev_nc[2] & 3;
2565         mr->rxlev_nc4_hi = meas->rxlev_nc[3] >> 3;
2566         mr->rxlev_nc4_lo = meas->rxlev_nc[3] & 7;
2567         mr->rxlev_nc5_hi = meas->rxlev_nc[4] >> 4;
2568         mr->rxlev_nc5_lo = meas->rxlev_nc[4] & 15;
2569         mr->rxlev_nc6_hi = meas->rxlev_nc[5] >> 5;
2570         mr->rxlev_nc6_lo = meas->rxlev_nc[5] & 31;
2571         mr->bsic_nc1_hi = meas->bsic_nc[0] >> 3;
2572         mr->bsic_nc1_lo = meas->bsic_nc[0] & 7;
2573         mr->bsic_nc2_hi = meas->bsic_nc[1] >> 4;
2574         mr->bsic_nc2_lo = meas->bsic_nc[1] & 15;
2575         mr->bsic_nc3_hi = meas->bsic_nc[2] >> 5;
2576         mr->bsic_nc3_lo = meas->bsic_nc[2] & 31;
2577         mr->bsic_nc4 = meas->bsic_nc[3];
2578         mr->bsic_nc5 = meas->bsic_nc[4];
2579         mr->bsic_nc6 = meas->bsic_nc[5];
2580         mr->bcch_f_nc1 = meas->bcch_f_nc[0];
2581         mr->bcch_f_nc2 = meas->bcch_f_nc[1];
2582         mr->bcch_f_nc3 = meas->bcch_f_nc[2];
2583         mr->bcch_f_nc4 = meas->bcch_f_nc[3];
2584         mr->bcch_f_nc5_hi = meas->bcch_f_nc[4] >> 1;
2585         mr->bcch_f_nc5_lo = meas->bcch_f_nc[4] & 1;
2586         mr->bcch_f_nc6_hi = meas->bcch_f_nc[5] >> 2;
2587         mr->bcch_f_nc6_lo = meas->bcch_f_nc[5] & 3;
2588
2589         return gsm48_send_rsl(ms, RSL_MT_UNIT_DATA_REQ, nmsg);
2590 }
2591
2592 /*
2593  * link establishment and release
2594  */
2595
2596 /* activate link and send establish request */
2597 static int gsm48_rr_dl_est(struct osmocom_ms *ms)
2598 {
2599         struct gsm48_rrlayer *rr = &ms->rrlayer;
2600         struct gsm_subscriber *subscr = &ms->subscr;
2601         struct msgb *nmsg;
2602         struct gsm48_hdr *gh;
2603         struct gsm48_pag_rsp *pr;
2604         uint8_t mi[11];
2605
2606         /* 3.3.1.1.3.1 */
2607         stop_rr_t3126(rr);
2608
2609         /* flush pending RACH requests */
2610 #ifdef TODO
2611         rr->n_chan_req = 0; // just to be safe
2612         nmsg = msgb_alloc_headroom(20, 16, "RAND_FLUSH");
2613         if (!nmsg)
2614                 return -ENOMEM;
2615         gsm48_send_rsl(ms, RSL_MT_RAND_ACC_FLSH, msg);
2616 #else
2617         if (bsc_timer_pending(&rr->temp_rach_ti))
2618                 bsc_del_timer(&rr->temp_rach_ti);
2619 #endif
2620
2621         /* send DL_EST_REQ */
2622         if (rr->rr_est_msg) {
2623                 /* use queued message */
2624                 nmsg = rr->rr_est_msg;
2625                 rr->rr_est_msg = 0;
2626                 LOGP(DRR, LOGL_INFO, "sending establish message\n");
2627         } else {
2628                 /* create paging response */
2629                 nmsg = gsm48_l3_msgb_alloc();
2630                 if (!nmsg)
2631                         return -ENOMEM;
2632                 gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
2633                 pr = (struct gsm48_pag_rsp *) msgb_put(nmsg, sizeof(*pr));
2634                 /* key sequence */
2635                 pr->key_seq = subscr->key_seq;
2636                 /* classmark 2 */
2637                 pr->cm2_len = sizeof(pr->cm2);
2638                 gsm48_rr_enc_cm2(ms, &pr->cm2);
2639                 /* mobile identity */
2640                 if (ms->subscr.tmsi_valid) {
2641                         gsm48_generate_mid_from_tmsi(mi, subscr->tmsi);
2642                         LOGP(DRR, LOGL_INFO, "sending paging response with "
2643                                 "TMSI\n");
2644                 } else if (subscr->imsi[0]) {
2645                         gsm48_generate_mid_from_imsi(mi, subscr->imsi);
2646                         LOGP(DRR, LOGL_INFO, "sending paging response with "
2647                                 "IMSI\n");
2648                 } else {
2649                         mi[1] = 1;
2650                         mi[2] = 0xf0 | GSM_MI_TYPE_NONE;
2651                         LOGP(DRR, LOGL_INFO, "sending paging response without "
2652                                 "TMSI/IMSI\n");
2653                 }
2654                 msgb_put(nmsg, 1 + mi[1]);
2655                 memcpy(pr->data, mi + 1, 1 + mi[1]);
2656         }
2657
2658         /* activate channel */
2659 #ifdef TODO
2660         RSL_MT_ to activate channel with all the cd_now informations
2661 #else
2662         rr->arfcn = rr->cd_now.chan_nr;
2663         tx_ph_dm_est_req(ms, rr->cd_now.arfcn, rr->arfcn);
2664 #endif
2665
2666         /* start establishmnet */
2667         return gsm48_send_rsl(ms, RSL_MT_EST_REQ, nmsg);
2668 }
2669
2670 /* the link is established */
2671 static int gsm48_rr_estab_cnf(struct osmocom_ms *ms, struct msgb *msg)
2672 {
2673         struct gsm48_rrlayer *rr = &ms->rrlayer;
2674         struct msgb *nmsg;
2675
2676         /* if MM has releases before confirm, we start release */
2677         if (rr->state == GSM48_RR_ST_IDLE) {
2678                 LOGP(DRR, LOGL_INFO, "MM already released RR.\n");
2679                 /* release message */
2680                 nmsg = gsm48_l3_msgb_alloc();
2681                 if (!nmsg)
2682                         return -ENOMEM;
2683                 /* start release */
2684                 return gsm48_send_rsl(ms, RSL_MT_REL_REQ, nmsg);
2685         }
2686
2687         /* 3.3.1.1.4 */
2688         new_rr_state(rr, GSM48_RR_ST_DEDICATED);
2689
2690         /* send confirm to upper layer */
2691         nmsg = gsm48_rr_msgb_alloc(
2692                 (rr->rr_est_req) ? GSM48_RR_EST_CNF : GSM48_RR_EST_IND);
2693         if (!nmsg)
2694                 return -ENOMEM;
2695         return gsm48_rr_upmsg(ms, nmsg);
2696 }
2697
2698 /* the link is released */
2699 static int gsm48_rr_rel_cnf(struct osmocom_ms *ms, struct msgb *msg)
2700 {
2701         struct gsm48_rrlayer *rr = &ms->rrlayer;
2702
2703         /* deactivate channel */
2704         LOGP(DRR, LOGL_INFO, "deactivating channel (arfcn %d)\n", rr->arfcn);
2705 #ifdef TODO
2706         release and give new arfcn
2707         tx_ph_dm_rel_req(ms, arfcn, rr->chan_desc.chan_desc.chan_nr);
2708 #else
2709         l1ctl_tx_ccch_req(ms, rr->arfcn);
2710 #endif
2711
2712         /* do nothing, because we aleady IDLE
2713          * or we received the rel cnf of the last connection
2714          * while already requesting a new one (CONN PEND)
2715          */
2716
2717         return 0;
2718 }
2719
2720 /*
2721  * radio ressource requests 
2722  */
2723
2724 /* establish request for dedicated mode */
2725 static int gsm48_rr_est_req(struct osmocom_ms *ms, struct msgb *msg)
2726 {
2727         struct gsm48_rrlayer *rr = &ms->rrlayer;
2728         struct gsm322_cellsel *cs = &ms->cellsel;
2729         struct gsm48_sysinfo *s = &ms->sysinfo;
2730         struct gsm_subscriber *subscr = &ms->subscr;
2731         struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *) msg->data;
2732         struct gsm48_hdr *gh = msgb_l3(msg);
2733         uint8_t cause;
2734         struct msgb *nmsg;
2735         struct gsm48_rr_hdr *nrrh;
2736         uint16_t acc_class;
2737
2738         /* 3.3.1.1.3.2 */
2739         if (bsc_timer_pending(&rr->t3122)) {
2740                 if (rrh->cause != RR_EST_CAUSE_EMERGENCY) {
2741                         LOGP(DRR, LOGL_INFO, "T3122 running, rejecting!\n");
2742                         cause = RR_REL_CAUSE_T3122;
2743                         reject:
2744                         nmsg = gsm48_rr_msgb_alloc(GSM48_RR_REL_IND);
2745                         if (!nmsg)
2746                                 return -ENOMEM;
2747                         nrrh = (struct gsm48_rr_hdr *)nmsg->data;
2748                         nrrh->cause = cause;
2749                         return gsm48_rr_upmsg(ms, nmsg);
2750                 }
2751                 LOGP(DRR, LOGL_INFO, "T3122 running, but emergency call\n");
2752                 stop_rr_t3122(rr);
2753         }
2754
2755         /* check if camping */
2756         if (cs->state != GSM322_C3_CAMPED_NORMALLY
2757          && rrh->cause != RR_EST_CAUSE_EMERGENCY) {
2758                 LOGP(DRR, LOGL_INFO, "Not camping normally, rejecting!\n");
2759                 cause = RR_REL_CAUSE_EMERGENCY_ONLY;
2760                 goto reject;
2761         }
2762         if (cs->state != GSM322_C3_CAMPED_NORMALLY
2763          && cs->state != GSM322_C7_CAMPED_ANY_CELL) {
2764                 LOGP(DRR, LOGL_INFO, "Not camping, rejecting!\n");
2765                 cause = RR_REL_CAUSE_TRY_LATER;
2766                 goto reject;
2767         }
2768
2769         /* check for relevant informations */
2770         if (!s->si3) {
2771                 LOGP(DRR, LOGL_INFO, "Not enough SI, rejecting!\n");
2772                 cause = RR_REL_CAUSE_TRY_LATER;
2773                 goto reject;
2774         }
2775
2776         /* 3.3.1.1.1 */
2777         if (!subscr->acc_barr && s->cell_barr) {
2778                 LOGP(DRR, LOGL_INFO, "Cell barred, rejecting!\n");
2779                 cause = RR_REL_CAUSE_NOT_AUTHORIZED;
2780                 goto reject;
2781         }
2782         if (rrh->cause == RR_EST_CAUSE_EMERGENCY)
2783                 acc_class = subscr->acc_class | 0x0400;
2784         else
2785                 acc_class = subscr->acc_class & 0xfbff;
2786         if (!subscr->acc_barr && !(acc_class & (s->class_barr ^ 0xffff))) {
2787                 LOGP(DRR, LOGL_INFO, "Cell barred for our access class (access "
2788                         "%04x barred %04x)!\n", acc_class, s->class_barr);
2789                 cause = RR_REL_CAUSE_NOT_AUTHORIZED;
2790                 goto reject;
2791         }
2792
2793         /* requested by RR */
2794         rr->rr_est_req = 1;
2795
2796         /* clone and store REQUEST message */
2797         if (!gh) {
2798                 LOGP(DRR, LOGL_ERROR, "Error, missing l3 message\n");
2799                 return -EINVAL;
2800         }
2801         rr->rr_est_msg = msgb_alloc_headroom(256, 16, "EST_REQ");
2802         if (!rr->rr_est_msg)
2803                 return -ENOMEM;
2804         memcpy(msgb_put(rr->rr_est_msg, msgb_l3len(msg)),
2805                 msgb_l3(msg), msgb_l3len(msg));
2806
2807         /* request channel */
2808         return gsm48_rr_tx_chan_req(ms, rrh->cause, 0);
2809 }
2810
2811 /* send all queued messages down to layer 2 */
2812 static int gsm48_rr_dequeue_down(struct osmocom_ms *ms)
2813 {
2814         struct gsm48_rrlayer *rr = &ms->rrlayer;
2815         struct msgb *msg;
2816
2817         while((msg = msgb_dequeue(&rr->downqueue))) {
2818                 LOGP(DRR, LOGL_INFO, "Sending queued message.\n");
2819                 gsm48_send_rsl(ms, RSL_MT_DATA_REQ, msg);
2820         }
2821
2822         return 0;
2823 }
2824
2825 /* 3.4.2 transfer data in dedicated mode */
2826 static int gsm48_rr_data_req(struct osmocom_ms *ms, struct msgb *msg)
2827 {
2828         struct gsm48_rrlayer *rr = &ms->rrlayer;
2829
2830         if (rr->state != GSM48_RR_ST_DEDICATED) {
2831                 msgb_free(msg);
2832                 return -EINVAL;
2833         }
2834         
2835         /* pull RR header */
2836         msgb_pull(msg, sizeof(struct gsm48_rr_hdr));
2837
2838         /* queue message, during handover or assignment procedure */
2839         if (rr->hando_susp_state || rr->assign_susp_state) {
2840                 LOGP(DRR, LOGL_INFO, "Queueing message during suspend.\n");
2841                 msgb_enqueue(&rr->downqueue, msg);
2842                 return 0;
2843         }
2844
2845         /* forward message */
2846         return gsm48_send_rsl(ms, RSL_MT_DATA_REQ, msg);
2847 }
2848
2849 /*
2850  * data indications from data link
2851  */
2852
2853 /* 3.4.2 data from layer 2 to RR and upper layer*/
2854 static int gsm48_rr_data_ind(struct osmocom_ms *ms, struct msgb *msg)
2855 {
2856         struct gsm48_hdr *gh = msgb_l3(msg);
2857         struct gsm48_rr_hdr *rrh;
2858         uint8_t pdisc = gh->proto_discr & 0x0f;
2859
2860         if (pdisc == GSM48_PDISC_RR) {
2861                 int rc = -EINVAL;
2862                 uint8_t skip_ind = (gh->proto_discr & 0xf0) >> 4;
2863
2864                 /* ignore if skip indicator is not B'0000' */
2865                 if (skip_ind)
2866                         return 0;
2867
2868                 switch(gh->msg_type) {
2869                 case GSM48_MT_RR_ADD_ASS:
2870                         rc = gsm48_rr_rx_add_ass(ms, msg);
2871                         break;
2872 #if 0
2873                 case GSM48_MT_RR_ASS_CMD:
2874                         rc = gsm48_rr_rx_ass_cmd(ms, msg);
2875                         break;
2876                 case GSM48_MT_RR_CIP_MODE_CMD:
2877                         rc = gsm48_rr_rx_cip_mode_cmd(ms, msg);
2878                         break;
2879 #endif
2880                 case GSM48_MT_RR_CLSM_ENQ:
2881                         rc = gsm48_rr_rx_cm_enq(ms, msg);
2882                         break;
2883 #if 0
2884                 case GSM48_MT_RR_HANDO_CMD:
2885                         rc = gsm48_rr_rx_hando_cmd(ms, msg);
2886                         break;
2887                 case GSM48_MT_RR_FREQ_REDEF:
2888                         rc = gsm48_rr_rx_freq_redef(ms, msg);
2889                         break;
2890 #endif
2891                 default:
2892                         LOGP(DRR, LOGL_NOTICE, "Message type 0x%02x unknown.\n",
2893                                 gh->msg_type);
2894
2895                         /* status message */
2896                         gsm48_rr_tx_rr_status(ms, GSM48_RR_CAUSE_MSG_TYPE_N);
2897                 }
2898
2899                 msgb_free(msg);
2900                 return rc;
2901         }
2902
2903         /* pull off RSL header up to L3 message */
2904         msgb_pull(msg, (long)msgb_l3(msg) - (long)msg->data);
2905
2906         /* push RR header */
2907         msgb_push(msg, sizeof(struct gsm48_rr_hdr));
2908         rrh = (struct gsm48_rr_hdr *)msg->data;
2909         rrh->msg_type = GSM48_RR_DATA_IND;
2910
2911         return gsm48_rr_upmsg(ms, msg);
2912 }
2913
2914 /* receive BCCH at RR layer */
2915 static int gsm48_rr_rx_bcch(struct osmocom_ms *ms, struct msgb *msg)
2916 {
2917         struct gsm48_system_information_type_header *sih = msgb_l3(msg);
2918
2919         switch (sih->system_information) {
2920         case GSM48_MT_RR_SYSINFO_1:
2921                 return gsm48_rr_rx_sysinfo1(ms, msg);
2922         case GSM48_MT_RR_SYSINFO_2:
2923                 return gsm48_rr_rx_sysinfo2(ms, msg);
2924         case GSM48_MT_RR_SYSINFO_2bis:
2925                 return gsm48_rr_rx_sysinfo2bis(ms, msg);
2926         case GSM48_MT_RR_SYSINFO_2ter:
2927                 return gsm48_rr_rx_sysinfo2ter(ms, msg);
2928         case GSM48_MT_RR_SYSINFO_3:
2929                 return gsm48_rr_rx_sysinfo3(ms, msg);
2930         case GSM48_MT_RR_SYSINFO_4:
2931                 return gsm48_rr_rx_sysinfo4(ms, msg);
2932         default:
2933                 LOGP(DRR, LOGL_NOTICE, "BCCH message type 0x%02x unknown.\n",
2934                         sih->system_information);
2935                 return -EINVAL;
2936         }
2937 }
2938
2939 /* receive CCCH at RR layer */
2940 static int gsm48_rr_rx_ccch(struct osmocom_ms *ms, struct msgb *msg)
2941 {
2942         struct gsm48_system_information_type_header *sih = msgb_l3(msg);
2943         struct gsm322_cellsel *cs = &ms->cellsel;
2944
2945         /* when changing/deactivating ccch, ignore pending messages */
2946         if (!cs->ccch_active)
2947                 return -EINVAL;
2948
2949         switch (sih->system_information) {
2950         case GSM48_MT_RR_SYSINFO_5:
2951                 return gsm48_rr_rx_sysinfo5(ms, msg);
2952         case GSM48_MT_RR_SYSINFO_5bis:
2953                 return gsm48_rr_rx_sysinfo5bis(ms, msg);
2954         case GSM48_MT_RR_SYSINFO_5ter:
2955                 return gsm48_rr_rx_sysinfo5ter(ms, msg);
2956         case GSM48_MT_RR_SYSINFO_6:
2957                 return gsm48_rr_rx_sysinfo6(ms, msg);
2958
2959         case GSM48_MT_RR_PAG_REQ_1:
2960                 return gsm48_rr_rx_pag_req_1(ms, msg);
2961         case GSM48_MT_RR_PAG_REQ_2:
2962                 return gsm48_rr_rx_pag_req_2(ms, msg);
2963         case GSM48_MT_RR_PAG_REQ_3:
2964                 return gsm48_rr_rx_pag_req_3(ms, msg);
2965         case GSM48_MT_RR_IMM_ASS:
2966                 return gsm48_rr_rx_imm_ass(ms, msg);
2967         case GSM48_MT_RR_IMM_ASS_EXT:
2968                 return gsm48_rr_rx_imm_ass_ext(ms, msg);
2969         case GSM48_MT_RR_IMM_ASS_REJ:
2970                 return gsm48_rr_rx_imm_ass_rej(ms, msg);
2971         default:
2972                 LOGP(DRR, LOGL_NOTICE, "CCCH message type 0x%02x unknown.\n",
2973                         sih->system_information);
2974                 return -EINVAL;
2975         }
2976 }
2977
2978 /* unit data from layer 2 to RR layer */
2979 static int gsm48_rr_unit_data_ind(struct osmocom_ms *ms, struct msgb *msg)
2980 {
2981         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2982         struct tlv_parsed tv;
2983         
2984         DEBUGP(DRSL, "RSLms UNIT DATA IND chan_nr=0x%02x link_id=0x%02x\n",
2985                 rllh->chan_nr, rllh->link_id);
2986
2987         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
2988         if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
2989                 DEBUGP(DRSL, "UNIT_DATA_IND without L3 INFO ?!?\n");
2990                 return -EIO;
2991         }
2992         msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
2993
2994         switch (rllh->chan_nr) {
2995         case RSL_CHAN_PCH_AGCH:
2996                 return gsm48_rr_rx_ccch(ms, msg);
2997         case RSL_CHAN_BCCH:
2998                 return gsm48_rr_rx_bcch(ms, msg);
2999         default:
3000                 LOGP(DRSL, LOGL_NOTICE, "RSL with chan_nr 0x%02x unknown.\n",
3001                         rllh->chan_nr);
3002                 return -EINVAL;
3003         }
3004 }
3005
3006 /*
3007  * state machines
3008  */
3009
3010 /* state trasitions for link layer messages (lower layer) */
3011 static struct dldatastate {
3012         uint32_t        states;
3013         int             type;
3014         const char      *type_name;
3015         int             (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3016 } dldatastatelist[] = {
3017         {SBIT(GSM48_RR_ST_IDLE) | SBIT(GSM48_RR_ST_CONN_PEND),
3018          RSL_MT_UNIT_DATA_IND, "UNIT_DATA_IND", gsm48_rr_unit_data_ind},
3019         {SBIT(GSM48_RR_ST_DEDICATED), /* 3.4.2 */
3020          RSL_MT_DATA_IND, "DATA_IND", gsm48_rr_data_ind},
3021         {SBIT(GSM48_RR_ST_IDLE) | SBIT(GSM48_RR_ST_CONN_PEND),
3022          RSL_MT_EST_CONF, "EST_CONF", gsm48_rr_estab_cnf},
3023 #if 0
3024         {SBIT(GSM48_RR_ST_DEDICATED),
3025          RSL_MT_EST_CONF, "EST_CONF", gsm48_rr_estab_cnf_dedicated},
3026         {SBIT(GSM_RRSTATE),
3027          RSL_MT_CONNECT_CNF, "CONNECT_CNF", gsm48_rr_connect_cnf},
3028         {SBIT(GSM_RRSTATE),
3029          RSL_MT_RELEASE_IND, "REL_IND", gsm48_rr_rel_ind},
3030 #endif
3031         {SBIT(GSM48_RR_ST_IDLE) | SBIT(GSM48_RR_ST_CONN_PEND),
3032          RSL_MT_REL_CONF, "REL_CONF", gsm48_rr_rel_cnf},
3033 #if 0
3034         {SBIT(GSM48_RR_ST_DEDICATED),
3035          RSL_MT_REL_CONF, "REL_CONF", gsm48_rr_rel_cnf_dedicated},
3036 #endif
3037         {SBIT(GSM48_RR_ST_CONN_PEND), /* 3.3.1.1.2 */
3038          RSL_MT_CHAN_CNF, "CHAN_CNF", gsm48_rr_rand_acc_cnf},
3039 #if 0
3040         {SBIT(GSM48_RR_ST_DEDICATED),
3041          RSL_MT_CHAN_CNF, "CHAN_CNF", gsm48_rr_rand_acc_cnf_dedicated},
3042         {SBIT(GSM_RRSTATE),
3043          RSL_MT_MDL_ERROR_IND, "MDL_ERROR_IND", gsm48_rr_mdl_error_ind},
3044 #endif
3045 };
3046
3047 #define DLDATASLLEN \
3048         (sizeof(dldatastatelist) / sizeof(struct dldatastate))
3049
3050 static int gsm48_rcv_rsl(struct osmocom_ms *ms, struct msgb *msg)
3051 {
3052         struct gsm48_rrlayer *rr = &ms->rrlayer;
3053         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
3054         int msg_type = rllh->c.msg_type;
3055         int i;
3056         int rc;
3057
3058         /* find function for current state and message */
3059         for (i = 0; i < DLDATASLLEN; i++)
3060                 if ((msg_type == dldatastatelist[i].type)
3061                  && ((1 << rr->state) & dldatastatelist[i].states))
3062                         break;
3063         if (i == DLDATASLLEN) {
3064                 LOGP(DRSL, LOGL_NOTICE, "RSLms message 0x%02x unhandled at "
3065                 "state %s.\n", msg_type, gsm48_rr_state_names[rr->state]);
3066                 msgb_free(msg);
3067                 return 0;
3068         }
3069         LOGP(DRSL, LOGL_INFO, "(ms %s) Received 'RSL_MT_%s' from RSL in state "
3070                 "%s\n", ms->name, dldatastatelist[i].type_name,
3071                 gsm48_rr_state_names[rr->state]);
3072
3073         rc = dldatastatelist[i].rout(ms, msg);
3074
3075         /* free msgb unless it is forwarded */
3076         if (dldatastatelist[i].rout != gsm48_rr_data_ind)
3077 #warning HACK!!!!!!
3078 return rc;
3079                 msgb_free(msg);
3080
3081         return rc;
3082 }
3083
3084 /* state trasitions for RR-SAP messages from up */
3085 static struct rrdownstate {
3086         uint32_t        states;
3087         int             type;
3088         int             (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3089 } rrdownstatelist[] = {
3090         {SBIT(GSM48_RR_ST_IDLE), /* 3.3.1.1 */
3091          GSM48_RR_EST_REQ, gsm48_rr_est_req},
3092         {SBIT(GSM48_RR_ST_DEDICATED), /* 3.4.2 */
3093          GSM48_RR_DATA_REQ, gsm48_rr_data_req},
3094 #if 0
3095         {SBIT(GSM48_RR_ST_CONN_PEND) | SBIT(GSM48_RR_ST_DEDICATED),
3096          GSM48_RR_ABORT_REQ, gsm48_rr_abort_req},
3097         {SBIT(GSM48_RR_ST_DEDICATED),
3098          GSM48_RR_ACT_REQ, gsm48_rr_act_req},
3099 #endif
3100 };
3101
3102 #define RRDOWNSLLEN \
3103         (sizeof(rrdownstatelist) / sizeof(struct rrdownstate))
3104
3105 int gsm48_rr_downmsg(struct osmocom_ms *ms, struct msgb *msg)
3106 {
3107         struct gsm48_rrlayer *rr = &ms->rrlayer;
3108         struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *) msg->data;
3109         int msg_type = rrh->msg_type;
3110         int i;
3111         int rc;
3112
3113         LOGP(DRR, LOGL_INFO, "(ms %s) Message '%s' received in state %s\n",
3114                 ms->name, get_rr_name(msg_type),
3115                 gsm48_rr_state_names[rr->state]);
3116
3117         /* find function for current state and message */
3118         for (i = 0; i < RRDOWNSLLEN; i++)
3119                 if ((msg_type == rrdownstatelist[i].type)
3120                  && ((1 << rr->state) & rrdownstatelist[i].states))
3121                         break;
3122         if (i == RRDOWNSLLEN) {
3123                 LOGP(DRR, LOGL_NOTICE, "Message unhandled at this state.\n");
3124                 msgb_free(msg);
3125                 return 0;
3126         }
3127
3128         rc = rrdownstatelist[i].rout(ms, msg);
3129
3130         /* free msgb uless it is forwarded */
3131         if (rrdownstatelist[i].rout != gsm48_rr_data_req)
3132                 msgb_free(msg);
3133
3134         return rc;
3135 }
3136
3137 /*
3138  * init/exit
3139  */
3140
3141 int gsm48_rr_init(struct osmocom_ms *ms)
3142 {
3143         struct gsm48_rrlayer *rr = &ms->rrlayer;
3144
3145         memset(rr, 0, sizeof(*rr));
3146         rr->ms = ms;
3147
3148         LOGP(DRR, LOGL_INFO, "init Radio Ressource process\n");
3149
3150         INIT_LLIST_HEAD(&rr->rsl_upqueue);
3151         INIT_LLIST_HEAD(&rr->downqueue);
3152         /* downqueue is handled here, so don't add_work */
3153
3154         osmol2_register_handler(ms, &gsm48_rx_rsl);
3155
3156         return 0;
3157 }
3158
3159 int gsm48_rr_exit(struct osmocom_ms *ms)
3160 {
3161         struct gsm48_rrlayer *rr = &ms->rrlayer;
3162         struct msgb *msg;
3163
3164         LOGP(DRR, LOGL_INFO, "exit Radio Ressource process\n");
3165
3166         /* flush queues */
3167         while ((msg = msgb_dequeue(&rr->rsl_upqueue)))
3168                 msgb_free(msg);
3169         while ((msg = msgb_dequeue(&rr->downqueue)))
3170                 msgb_free(msg);
3171
3172         if (rr->rr_est_msg) {
3173                 msgb_free(rr->rr_est_msg);
3174                 rr->rr_est_msg = NULL;
3175         }
3176
3177         stop_rr_t3122(rr);
3178         stop_rr_t3126(rr);
3179
3180         return 0;
3181 }
3182
3183 #if 0
3184
3185 the process above is complete
3186 ------------------------------------------------------------------------------
3187 incomplete
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207 todo:
3208
3209 add support structure
3210 initialize support structure
3211
3212 queue messages (rslms_data_req) if channel changes
3213
3214 flush rach msg in all cases: during sending, after its done, and when aborted
3215 stop timers on abort
3216 debugging. (wenn dies todo erledigt ist, bitte in den anderen code moven)
3217 wird beim abbruch immer der gepufferte cm-service-request entfernt, auch beim verschicken?:
3218 measurement reports
3219 todo rr_sync_ind when receiving ciph, re ass, channel mode modify
3220
3221 todo change procedures, release procedure
3222
3223 during procedures, like "channel assignment" or "handover", rr requests must be queued
3224 they must be dequeued when complete
3225 they queue must be flushed when rr fails
3226
3227 #include <osmocore/protocol/gsm_04_08.h>
3228 #include <osmocore/msgb.h>
3229 #include <osmocore/utils.h>
3230 #include <osmocore/gsm48.h>
3231
3232 static int gsm48_rr_abort_req(struct osmocom_ms *ms, struct gsm48_rr *rrmsg)
3233 {
3234         struct gsm48_rrlayer *rr = ms->rrlayer;
3235         stop_rr_t3126(rr);
3236         if (rr->state == GSM48_RR_ST_DEDICATED) {
3237                 struct gsm_dl dlmsg;
3238
3239                 memset(&dlmsg, 0, sizeof(dlmsg));
3240                 return gsm48_send_rsl(ms, RSL_MT_REL_REQ, nmsg);
3241         }
3242         new_rr_state(rr, GSM48_RR_ST_IDLE);
3243 }
3244
3245 static int gsm48_rr_act_req(struct osmocom_ms *ms, struct gsm48_rr *rrmsg)
3246 {
3247 }
3248
3249
3250 }
3251
3252 /* memcopy of LV of given IE from tlv_parsed structure */
3253 static int tlv_copy(void *dest, int dest_len, struct tlv_parsed *tp, uint8_t ie)
3254 {
3255         uint8_t *lv = dest;
3256         uint8_t len;
3257
3258         if (dest_len < 1)
3259                 return -EINVAL;
3260         lv[0] = 0;
3261
3262         if (!TLVP_PRESENT(tp, ie))
3263                 return 0;
3264
3265         len = TLVP_LEN(tp, ie);
3266         if (len < 1)
3267                 return 0;
3268         if (len + 1 > dest_len)
3269                 return -ENOMEM;
3270
3271         memcpy(dest, TLVP_VAL(tp, ie) - 1, len + 1);
3272         return 0;
3273 }
3274
3275 static int gsm48_rr_rx_ass_cmd(struct osmocom_ms *ms, struct msgb *msg)
3276 {
3277         struct gsm48_rrlayer *rr = ms->rrlayer;
3278         struct gsm48_hdr *gh = msgb_l3(msg);
3279         struct gsm48_ass_cmd *ac = (struct gsm48_ass_cmd *)gh->data;
3280         int payload_len = msgb_l3len(msg) - sizeof(*gh) - sizeof(*ac);
3281         struct tlv_parsed tp;
3282         struct gsm48_rr_chan_desc cd;
3283         struct msgb *nmsg;
3284
3285         memset(&cd, 0, sizeof(cd));
3286
3287         if (payload_len < 0) {
3288                 LOGP(DRR, LOGL_NOTICE, "Short read of ASSIGNMENT COMMAND message.\n");
3289                 return gsm48_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
3290         }
3291         tlv_parse(&tp, &gsm48_rr_att_tlvdef, ac->data, payload_len, 0, 0);
3292
3293         /* channel description */
3294         memcpy(&cd.chan_desc, &ac->chan_desc, sizeof(chan_desc));
3295         /* power command */
3296         cd.power_command = ac->power_command;
3297         /* frequency list, after timer */
3298         tlv_copy(&cd.fl, sizeof(fl_after), &tp, GSM48_IE_FRQLIST_AFTER);
3299         /* cell channel description */
3300         tlv_copy(&cd.ccd, sizeof(ccd), &tp, GSM48_IE_CELL_CH_DESC);
3301         /* multislot allocation */
3302         tlv_copy(&cd.multia, sizeof(ma), &tp, GSM48_IE_MSLOT_DESC);
3303         /* channel mode */
3304         tlv_copy(&cd.chanmode, sizeof(chanmode), &tp, GSM48_IE_CHANMODE_1);
3305         /* mobile allocation, after time */
3306         tlv_copy(&cd.moba_after, sizeof(moba_after), &tp, GSM48_IE_MOB_AL_AFTER);
3307         /* starting time */
3308         tlv_copy(&cd.start, sizeof(start), &tp, GSM_IE_START_TIME);
3309         /* frequency list, before time */
3310         tlv_copy(&cd.fl_before, sizeof(fl_before), &tp, GSM48_IE_FRQLIST_BEFORE);
3311         /* channel description, before time */
3312         tlv_copy(&cd.chan_desc_before, sizeof(cd_before), &tp, GSM48_IE_CHDES_1_BEFORE);
3313         /* frequency channel sequence, before time */
3314         tlv_copy(&cd.fcs_before, sizeof(fcs_before), &tp, GSM48_IE_FRQSEQ_BEFORE);
3315         /* mobile allocation, before time */
3316         tlv_copy(&cd.moba_before, sizeof(moba_before), &tp, GSM48_IE_MOB_AL_BEFORE);
3317         /* cipher mode setting */
3318         if (TLVP_PRESENT(&tp, GSM48_IE_CIP_MODE_SET))
3319                 cd.cipher = *TLVP_VAL(&tp, GSM48_IE_CIP_MODE_SET);
3320         else
3321                 cd.cipher = 0;
3322
3323         if (no CA) {
3324                 LOGP(DRR, LOGL_INFO, "No current cell allocation available.\n");
3325                 return gsm48_rr_tx_rr_status(ms, GSM48_RR_CAUSE_NO_CELL_ALLOC_A);
3326         }
3327         
3328         if (not supported) {
3329                 LOGP(DRR, LOGL_INFO, "New channel is not supported.\n");
3330                 return gsm48_rr_tx_rr_status(ms, RR_CAUSE_CHAN_MODE_UNACCEPT);
3331         }
3332
3333         if (freq not supported) {
3334                 LOGP(DRR, LOGL_INFO, "New frequency is not supported.\n");
3335                 return gsm48_rr_tx_rr_status(ms, RR_CAUSE_FREQ_NOT_IMPLEMENTED);
3336         }
3337
3338         /* store current channel descriptions, to return in case of failure */
3339         memcpy(&rr->chan_last, &rr->chan_desc, sizeof(*cd));
3340         /* copy new description */
3341         memcpy(&rr->chan_desc, cd, sizeof(cd));
3342
3343         /* start suspension of current link */
3344         nmsg = gsm48_l3_msgb_alloc();
3345         if (!nmsg)
3346                 return -ENOMEM;
3347         gsm48_send_rsl(ms, RSL_MT_SUSP_REQ, msg);
3348
3349         /* change into special assignment suspension state */
3350         rr->assign_susp_state = 1;
3351         rr->resume_last_state = 0;
3352
3353         return 0;
3354 }
3355
3356 /* decode "BA Range" (10.5.2.1a) */
3357 static int gsm48_decode_ba_range(uint8_t *ba, uint8_t, ba_len, uint32_t *range,
3358         uint8_t *ranges, int max_ranges)
3359 {
3360         /* ba = pointer to IE without IE type and length octets
3361          * ba_len = number of octets
3362          * range = pointer to store decoded range
3363          * ranges = number of ranges decoded
3364          * max_ranges = maximum number of decoded ranges that can be stored
3365          */
3366         uint16_t lower, higher;
3367         int i, n, required_octets;
3368         
3369         /* find out how much ba ranges will be decoded */
3370         n = *ba++;
3371         ba_len --;
3372         required_octets = 5 * (n >> 1) + 3 * (n & 1);
3373         if (required_octets > n) {
3374                 *ranges = 0;
3375                 return -EINVAL;
3376         }
3377         if (max_ranges > n)
3378                 n = max_ranges;
3379
3380         /* decode ranges */
3381         for (i = 0; i < n; i++) {
3382                 if (!(i & 1)) {
3383                         /* decode even range number */
3384                         lower = *ba++ << 2;
3385                         lower |= (*ba >> 6);
3386                         higher = (*ba++ & 0x3f) << 4;
3387                         higher |= *ba >> 4;
3388                 } else {
3389                         lower = (*ba++ & 0x0f) << 6;
3390                         lower |= *ba >> 2;
3391                         higher = (*ba++ & 0x03) << 8;
3392                         higher |= *ba++;
3393                         /* decode odd range number */
3394                 }
3395                 *range++ = (higher << 16) | lower;
3396         }
3397         *ranges = n;
3398
3399         return 0;
3400 }
3401
3402
3403 /* decode "Cell Description" (10.5.2.2) */
3404 static int gsm48_decode_cell_desc(struct gsm48_cell_desc *cd, uint16_t *arfcn, uint8_t *ncc uint8_t *bcc)
3405 {
3406         *arfcn = (cd->bcch_hi << 8) + cd->bcch_lo;
3407         *ncc = cd->ncc;
3408         *bcc = cd->bcc;
3409 }
3410
3411 /* decode "Power Command" (10.5.2.28) and (10.5.2.28a) */
3412 static int gsm48_decode_power_cmd_acc(struct gsm48_power_cmd *pc, uint8_t *power_level uint8_t *atc)
3413 {
3414         *power_level = pc->power_level;
3415         if (atc) /* only in case of 10.5.2.28a */
3416                 *atc = pc->atc;
3417 }
3418
3419 /* decode "Synchronization Indication" (10.5.2.39) */
3420 static int gsm48_decode_power_cmd_acc(struct gsm48_rrlayer *rr, struct gsm48_rr_sync_ind *si)
3421 {
3422         rr->ho_sync_ind = si->si;
3423         rr->ho_rot = si->rot;
3424         rr->ho_nci = si->nci;
3425 }
3426
3427 /* receiving HANDOVER COMMAND message (9.1.15) */
3428 static int gsm48_rr_rx_hando_cmd(struct osmocom_ms *ms, struct msgb *msg)
3429 {
3430         struct gsm48_rrlayer *rr = ms->rrlayer;
3431         struct gsm48_hdr *gh = msgb_l3(msg);
3432         struct gsm48_ho_cmd *ho = (struct gsm48_ho_cmd *)gh->data;
3433         int payload_len = msgb_l3len(msg) - sizeof(*gh) - wirklich sizeof(*ho);
3434         struct tlv_parsed tp;
3435         struct gsm48_rr_chan_desc cd;
3436         struct msgb *nmsg;
3437
3438         memset(&cd, 0, sizeof(cd));
3439
3440         if (payload_len < 0) {
3441                 LOGP(DRR, LOGL_NOTICE, "Short read of HANDOVER COMMAND message.\n");
3442                 return gsm48_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
3443         }
3444         tlv_parse(&tp, &gsm48_rr_att_tlvdef, ho->data, payload_len, 0, 0);
3445
3446         /* decode Cell Description */
3447         gsm_decode_cell_desc(&ho->cell_desc, &cd.bcch_arfcn, &cd.ncc, &cd.bcc);
3448         /* Channel Description */
3449         memcpy(&rr->chan_desc.chan_desc, ho->chan_desc, 3);
3450         /* Handover Reference */
3451         rr->hando_ref = ho->ho_ref;
3452         /* Power Command and access type */
3453         gsm_decode_power_cmd_acc((struct gsm48_power_cmd *)&ho->power_command,
3454                 &cd.power_level, cd.atc);
3455         /* Synchronization Indication */
3456         if (TLVP_PRESENT(&tp, GSM48_IE_SYNC_IND))
3457                 gsm48_decode_sync_ind(rr,
3458                         TLVP_VAL(&tp, GSM48_IE_MOBILE_ALLOC)-1, &cd);
3459         /* Frequency Sort List */
3460         if (TLVP_PRESENT(&tp, GSM48_IE_FREQ_SHORT_LIST))
3461                 gsm48_decode_freq_list(&ms->support, s->freq,
3462                         TLVP_VAL(&tp, GSM48_IE_MOBILE_ALLOC),
3463                         *(TLVP_VAL(&tp, GSM48_IE_MOBILE_ALLOC)-1),
3464                                 0xce, FREQ_TYPE_SERV);
3465
3466
3467 today: more IE parsing
3468
3469         /* store current channel descriptions, to return in case of failure */
3470         memcpy(&rr->chan_last, &rr->chan_desc, sizeof(*cd));
3471         /* copy new description */
3472         memcpy(&rr->chan_desc, cd, sizeof(cd));
3473
3474         /* start suspension of current link */
3475         nmsg = gsm48_l3_msgb_alloc();
3476         if (!nmsg)
3477                 return -ENOMEM;
3478         gsm48_send_rsl(ms, RSL_MT_SUSP_REQ, msg);
3479
3480         /* change into special handover suspension state */
3481         rr->hando_susp_state = 1;
3482         rr->resume_last_state = 0;
3483
3484         return 0;
3485 }
3486
3487 static int gsm48_rr_estab_cnf_dedicated(struct osmocom_ms *ms, struct msgb *msg)
3488 {
3489         if (rr->hando_susp_state || rr->assign_susp_state) {
3490                 if (rr->resume_last_state) {
3491                         rr->resume_last_state = 0;
3492                         gsm48_rr_tx_ass_cpl(ms, GSM48_RR_CAUSE_NORMAL);
3493                 } else {
3494                         gsm48_rr_tx_ass_fail(ms, RR_CAUSE_PROTO_ERR_UNSPEC);
3495                 }
3496                 /* transmit queued frames during ho / ass transition */
3497                 gsm48_rr_dequeue_down(ms);
3498         }
3499
3500         return 0;
3501 }
3502
3503 static int gsm48_rr_connect_cnf(struct osmocom_ms *ms, struct msgbl *msg)
3504 {
3505 }
3506
3507 static int gsm48_rr_rel_ind(struct osmocom_ms *ms, struct msgb *msg)
3508 {
3509 }
3510
3511 static int gsm48_rr_rel_cnf_dedicated(struct osmocom_ms *ms, struct msgb *msg)
3512 {
3513         struct gsm48_rrlayer *rr = ms->rrlayer;
3514         struct msgb *nmsg;
3515
3516         if (rr->hando_susp_state || rr->assign_susp_state) {
3517                 struct msgb *msg;
3518
3519                 /* change radio to new channel */
3520                 rr->arfcn = rr->chan_desc.chan_desc.chan_nr;
3521                 tx_ph_dm_est_req(ms, arfcn, rr->arfcn);
3522
3523                 nmsg = gsm48_l3_msgb_alloc();
3524                 if (!nmsg)
3525                         return -ENOMEM;
3526                 /* send DL-ESTABLISH REQUEST */
3527                 gsm48_send_rsl(ms, RSL_MT_EST_REQ, nmsg);
3528
3529         }
3530         if (rr->hando_susp_state) {
3531                 gsm48_rr_tx_hando_access(ms);
3532                 rr->hando_acc_left = 3;
3533         }
3534         return 0;
3535 }
3536
3537 static int gsm48_rr_mdl_error_ind(struct osmocom_ms *ms, struct msgb *msg)
3538 {
3539         struct gsm48_rrlayer *rr = ms->rrlayer;
3540         struct msgb *nmsg;
3541         struct gsm_rr_hdr *nrrh;
3542
3543         if (rr->hando_susp_state || rr->assign_susp_state) {
3544                 if (!rr->resume_last_state) {
3545                         rr->resume_last_state = 1;
3546
3547                         /* get old channel description */
3548                         memcpy(&rr->chan_desc, &rr->chan_last, sizeof(*cd));
3549
3550                         /* change radio to old channel */
3551                         rr->arfcn = rr->chan_desc.chan_desc.chan_nr;
3552                         tx_ph_dm_est_req(ms, arfcn, rr->arfcn);
3553
3554                         /* re-establish old link */
3555                         nmsg = gsm48_l3_msgb_alloc();
3556                         if (!nmsg)
3557                                 return -ENOMEM;
3558                         return gsm48_send_rsl(ms, RSL_MT_EST_REQ, nmsg);
3559                 }
3560                 rr->resume_last_state = 0;
3561         }
3562
3563         /* deactivate channel */
3564         tx_ph_dm_rel_req(ms, arfcn, rr->chan_desc.chan_desc.chan_nr);
3565
3566         /* send abort ind to upper layer */
3567         nmsg = gsm48_mm_msgb_alloc();
3568
3569         if (!msg)
3570                 return -ENOMEM;
3571         nrrh = (struct gsm_mm_hdr *)nmsg->data;
3572         nrrh->msg_type = RR_ABORT_IND;
3573         nrrh->cause = GSM_MM_CAUSE_LINK_FAILURE;
3574         return gsm48_rr_upmsg(ms, msg);
3575 }
3576
3577 static void timeout_rr_t3124(void *arg)
3578 {
3579         struct gsm48_rrlayer *rr = arg;
3580         struct msgb *nmsg;
3581
3582         /* stop sending more access bursts when timer expired */
3583         hando_acc_left = 0;
3584
3585         /* get old channel description */
3586         memcpy(&rr->chan_desc, &rr->chan_last, sizeof(*cd));
3587
3588         /* change radio to old channel */
3589         rr->arfcn = rr->chan_desc.chan_desc.chan_nr;
3590         tx_ph_dm_est_req(ms, arfcn, rr->arfcn);
3591
3592         /* re-establish old link */
3593         nmsg = gsm48_l3_msgb_alloc();
3594         if (!nmsg)
3595                 return -ENOMEM;
3596         return gsm48_send_rsl(ms, RSL_MT_EST_REQ, nmsg);
3597
3598         todo
3599 }
3600
3601 /* send HANDOVER ACCESS burst (9.1.14) */
3602 static int gsm48_rr_tx_hando_access(struct osmocom_ms *ms)
3603 {
3604         nmsg = msgb_alloc_headroom(20, 16, "HAND_ACCESS");
3605         if (!nmsg)
3606                 return -ENOMEM;
3607         *msgb_put(nmsg, 1) = rr->hando_ref;
3608         todo burst
3609         return gsm48_send_rsl(ms, RSL_MT_RAND_ACC_REQ, nmsg);
3610 }
3611
3612 /* send next channel request in dedicated state */
3613 static int gsm48_rr_rand_acc_cnf_dedicated(struct osmocom_ms *ms, struct msgb *msg)
3614 {
3615         struct gsm48_rrlayer *rr = &ms->rrlayer;
3616         struct msgb *nmsg;
3617         int s;
3618
3619         if (!rr->hando_susp_state) {
3620                 LOGP(DRR, LOGL_NOTICE, "Random acces confirm, but not in handover state.\n");
3621                 return 0;
3622         }
3623
3624         /* send up to four handover access bursts */
3625         if (rr->hando_acc_left) {
3626                 rr->hando_acc_left--;
3627                 gsm48_rr_tx_hando_access(ms);
3628                 return;
3629         }
3630
3631         /* start timer for sending next HANDOVER ACCESS bursts afterwards */
3632         if (!bsc_timer_pending(&rr->t3124)) {
3633                 if (allocated channel is SDCCH)
3634                         start_rr_t3124(rr, GSM_T3124_675);
3635                 else
3636                         start_rr_t3124(rr, GSM_T3124_320);
3637         if (!rr->n_chan_req) {
3638                 start_rr_t3126(rr, 5, 0); /* TODO improve! */
3639                 return 0;
3640         }
3641         rr->n_chan_req--;
3642
3643         /* wait for PHYSICAL INFORMATION message or T3124 timeout */
3644         return 0;
3645
3646 }
3647
3648 #endif
3649
3650