Fixes, especially frequency list parsing.
[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                 f[w[0]].mask |= frqt;
1173                 if (w[1])
1174                         f[(w[0] + w[1]) % 1024].mask |= frqt;
1175                 if (w[2])
1176                         f[(w[0] + ((w[1] - 256 + w[2] - 1) % 511) + 1) % 1024].mask |= frqt;
1177                 if (w[3])
1178                         f[(w[0] + ((w[1]       + w[3] - 1) % 511) + 1) % 1024].mask |= frqt;
1179                 if (w[4])
1180                         f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + w[4] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
1181                 if (w[5])
1182                         f[(w[0] + ((w[1]       + ((w[3] - 128 + w[5] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
1183                 if (w[6])
1184                         f[(w[0] + ((w[1] - 256 + ((w[2]       + w[6] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
1185                 if (w[7])
1186                         f[(w[0] + ((w[1]       + ((w[3]       + w[7] - 1) % 255)) % 511) + 1) % 1024].mask |= frqt;
1187                 if (w[8])
1188                         f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + ((w[4] - 64 + w[8] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1189                 if (w[9])
1190                         f[(w[0] + ((w[1]       + ((w[3] - 128 + ((w[5] - 64 + w[9] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1191                 if (w[10])
1192                         f[(w[0] + ((w[1] - 256 + ((w[2]       + ((w[6] - 64 + w[10] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1193                 if (w[11])
1194                         f[(w[0] + ((w[1]       + ((w[3]       + ((w[7] - 64 + w[11] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1195                 if (w[12])
1196                         f[(w[0] + ((w[1] - 256 + ((w[2] - 128 + ((w[4]      + w[12] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1197                 if (w[13])
1198                         f[(w[0] + ((w[1]       + ((w[3] - 128 + ((w[5]      + w[13] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1199                 if (w[14])
1200                         f[(w[0] + ((w[1] - 256 + ((w[2]       + ((w[6]      + w[14] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1201                 if (w[15])
1202                         f[(w[0] + ((w[1]       + ((w[3]       + ((w[7]      + w[15] - 1) % 127)) % 255)) % 511) + 1) % 1024].mask |= frqt;
1203                 if (w[16])
1204                         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;
1205                 if (w[17])
1206                         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;
1207
1208                 return 0;
1209         }
1210         /* 10..101. */
1211         if ((cd[0] & 0xce & mask) == 0x8a) {
1212                 /* Range 256 format */
1213                 uint16_t w[22]; /* 1..21 */
1214                 struct gsm48_range_256 *r = (struct gsm48_range_256 *)cd;
1215
1216                 if (len < 4)
1217                         return -EINVAL;
1218                 memset(w, 0, sizeof(w));
1219                 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1220                 w[1] = (r->w1_hi << 1) | r->w1_lo;
1221                 if (len >= 4)
1222                         w[2] = r->w2;
1223                 if (len >= 5)
1224                         w[3] = r->w3;
1225                 if (len >= 6)
1226                         w[4] = (r->w4_hi << 5) | r->w4_lo;
1227                 if (len >= 7)
1228                         w[5] = (r->w5_hi << 3) | r->w5_lo;
1229                 if (len >= 8)
1230                         w[6] = (r->w6_hi << 1) | r->w6_lo;
1231                 if (len >= 8)
1232                         w[7] = r->w7;
1233                 if (len >= 9)
1234                         w[8] = (r->w8_hi << 4) | r->w8_lo;
1235                 if (len >= 10)
1236                         w[9] = (r->w9_hi << 1) | r->w9_lo;
1237                 if (len >= 10)
1238                         w[10] = r->w10;
1239                 if (len >= 11)
1240                         w[11] = (r->w11_hi << 3) | r->w11_lo;
1241                 if (len >= 11)
1242                         w[12] = r->w12;
1243                 if (len >= 12)
1244                         w[13] = r->w13;
1245                 if (len >= 13)
1246                         w[14] = r->w15;
1247                 if (len >= 13)
1248                         w[15] = (r->w14_hi << 2) | r->w14_lo;
1249                 if (len >= 14)
1250                         w[16] = (r->w16_hi << 3) | r->w16_lo;
1251                 if (len >= 14)
1252                         w[17] = r->w17;
1253                 if (len >= 15)
1254                         w[18] = r->w19;
1255                 if (len >= 15)
1256                         w[19] = (r->w18_hi << 3) | r->w18_lo;
1257                 if (len >= 16)
1258                         w[20] = (r->w20_hi << 3) | r->w20_lo;
1259                 if (len >= 16)
1260                         w[21] = r->w21;
1261                 f[w[0]].mask |= frqt;
1262                 if (w[1])
1263                         f[(w[0] + w[1]) % 1024].mask |= frqt;
1264                 if (w[2])
1265                         f[(w[0] + ((w[1] - 128 + w[2] - 1) % 255) + 1) % 1024].mask |= frqt;
1266                 if (w[3])
1267                         f[(w[0] + ((w[1]       + w[3] - 1) % 255) + 1) % 1024].mask |= frqt;
1268                 if (w[4])
1269                         f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + w[4] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
1270                 if (w[5])
1271                         f[(w[0] + ((w[1]       + ((w[3] - 64 + w[5] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
1272                 if (w[6])
1273                         f[(w[0] + ((w[1] - 128 + ((w[2]      + w[6] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
1274                 if (w[7])
1275                         f[(w[0] + ((w[1]       + ((w[3]      + w[7] - 1) % 127)) % 255) + 1) % 1024].mask |= frqt;
1276                 if (w[8])
1277                         f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4] - 32 + w[8] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1278                 if (w[9])
1279                         f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5] - 32 + w[9] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1280                 if (w[10])
1281                         f[(w[0] + ((w[1] - 128 + ((w[2]      + ((w[6] - 32 + w[10] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1282                 if (w[11])
1283                         f[(w[0] + ((w[1]       + ((w[3]      + ((w[7] - 32 + w[11] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1284                 if (w[12])
1285                         f[(w[0] + ((w[1] - 128 + ((w[2] - 64 + ((w[4]      + w[12] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1286                 if (w[13])
1287                         f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5]      + w[13] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1288                 if (w[14])
1289                         f[(w[0] + ((w[1] - 128 + ((w[2]      + ((w[6]      + w[14] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1290                 if (w[15])
1291                         f[(w[0] + ((w[1]       + ((w[3]      + ((w[7]      + w[15] - 1) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1292                 if (w[16])
1293                         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;
1294                 if (w[17])
1295                         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;
1296                 if (w[18])
1297                         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;
1298                 if (w[19])
1299                         f[(w[0] + ((w[1]       + ((w[3]      + ((w[7] - 32 + ((w[11] - 16 + w[19] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1300                 if (w[20])
1301                         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;
1302                 if (w[21])
1303                         f[(w[0] + ((w[1]       + ((w[3] - 64 + ((w[5]      + ((w[13] - 16 + w[21] - 1) % 31)) % 63)) % 127)) % 255) + 1) % 1024].mask |= frqt;
1304
1305                 return 0;
1306         }
1307         /* 10..110. */
1308         if ((cd[0] & 0xce & mask) == 0x8c) {
1309                 /* Range 128 format */
1310                 uint16_t w[29]; /* 1..28 */
1311                 struct gsm48_range_128 *r = (struct gsm48_range_128 *)cd;
1312
1313                 if (len < 3)
1314                         return -EINVAL;
1315                 memset(w, 0, sizeof(w));
1316                 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1317                 w[1] = r->w1;
1318                 if (len >= 4)
1319                         w[2] = r->w2;
1320                 if (len >= 5)
1321                         w[3] = (r->w3_hi << 4) | r->w3_lo;
1322                 if (len >= 6)
1323                         w[4] = (r->w4_hi << 1) | r->w4_lo;
1324                 if (len >= 6)
1325                         w[5] = r->w5;
1326                 if (len >= 7)
1327                         w[6] = (r->w6_hi << 3) | r->w6_lo;
1328                 if (len >= 7)
1329                         w[7] = r->w7;
1330                 if (len >= 8)
1331                         w[8] = r->w8;
1332                 if (len >= 8)
1333                         w[9] = r->w9;
1334                 if (len >= 9)
1335                         w[10] = r->w10;
1336                 if (len >= 9)
1337                         w[11] = r->w11;
1338                 if (len >= 10)
1339                         w[12] = r->w12;
1340                 if (len >= 10)
1341                         w[13] = r->w13;
1342                 if (len >= 11)
1343                         w[14] = r->w14;
1344                 if (len >= 11)
1345                         w[15] = r->w15;
1346                 if (len >= 12)
1347                         w[16] = r->w16;
1348                 if (len >= 12)
1349                         w[17] = r->w17;
1350                 if (len >= 13)
1351                         w[18] = (r->w18_hi << 1) | r->w18_lo;
1352                 if (len >= 13)
1353                         w[19] = r->w19;
1354                 if (len >= 13)
1355                         w[20] = r->w20;
1356                 if (len >= 14)
1357                         w[21] = (r->w21_hi << 2) | r->w21_lo;
1358                 if (len >= 14)
1359                         w[22] = r->w22;
1360                 if (len >= 14)
1361                         w[23] = r->w23;
1362                 if (len >= 15)
1363                         w[24] = r->w24;
1364                 if (len >= 15)
1365                         w[25] = r->w25;
1366                 if (len >= 16)
1367                         w[26] = (r->w26_hi << 1) | r->w26_lo;
1368                 if (len >= 16)
1369                         w[27] = r->w27;
1370                 if (len >= 16)
1371                         w[28] = r->w28;
1372                 f[w[0]].mask |= frqt;
1373                 if (w[1])
1374                         f[(w[0] + w[1]) % 1024].mask |= frqt;
1375                 if (w[2])
1376                         f[(w[0] + ((w[1] - 64 + w[2] - 1) % 127) + 1) % 1024].mask |= frqt;
1377                 if (w[3])
1378                         f[(w[0] + ((w[1]      + w[3] - 1) % 127) + 1) % 1024].mask |= frqt;
1379                 if (w[4])
1380                         f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + w[4] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
1381                 if (w[5])
1382                         f[(w[0] + ((w[1]      + ((w[3] - 32 + w[5] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
1383                 if (w[6])
1384                         f[(w[0] + ((w[1] - 64 + ((w[2]      + w[6] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
1385                 if (w[7])
1386                         f[(w[0] + ((w[1]      + ((w[3]      + w[7] - 1) % 63)) % 127) + 1) % 1024].mask |= frqt;
1387                 if (w[8])
1388                         f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4] - 16 + w[8] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1389                 if (w[9])
1390                         f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5] - 16 + w[9] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1391                 if (w[10])
1392                         f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6] - 16 + w[10] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1393                 if (w[11])
1394                         f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + w[11] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1395                 if (w[12])
1396                         f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4]      + w[12] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1397                 if (w[13])
1398                         f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5]      + w[13] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1399                 if (w[14])
1400                         f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6]      + w[14] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1401                 if (w[15])
1402                         f[(w[0] + ((w[1]      + ((w[3]      + ((w[7]      + w[15] - 1) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1403                 if (w[16])
1404                         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;
1405                 if (w[17])
1406                         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;
1407                 if (w[18])
1408                         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;
1409                 if (w[19])
1410                         f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + ((w[11] - 8 + w[19] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1411                 if (w[20])
1412                         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;
1413                 if (w[21])
1414                         f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5]      + ((w[13] - 8 + w[21] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1415                 if (w[22])
1416                         f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6]      + ((w[14] - 8 + w[22] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1417                 if (w[23])
1418                         f[(w[0] + ((w[1]      + ((w[3]      + ((w[7]      + ((w[15] - 8 + w[23] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1419                 if (w[24])
1420                         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;
1421                 if (w[25])
1422                         f[(w[0] + ((w[1]      + ((w[3] - 32 + ((w[5] - 16 + ((w[9]     + w[25] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1423                 if (w[26])
1424                         f[(w[0] + ((w[1] - 64 + ((w[2]      + ((w[6] - 16 + ((w[10]     + w[26] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1425                 if (w[27])
1426                         f[(w[0] + ((w[1]      + ((w[3]      + ((w[7] - 16 + ((w[11]     + w[27] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1427                 if (w[28])
1428                         f[(w[0] + ((w[1] - 64 + ((w[2] - 32 + ((w[4]      + ((w[12]     + w[28] - 1) % 15)) % 31)) % 63)) % 127) + 1) % 1024].mask |= frqt;
1429
1430                 return 0;
1431         }
1432         /* 10..111. */
1433         if ((cd[0] & 0xce & mask) == 0x8e) {
1434                 /* Variable bitmap format (can be any length >= 3) */
1435                 uint16_t orig = 0;
1436                 struct gsm48_var_bit *r = (struct gsm48_var_bit *)cd;
1437
1438                 if (len < 3)
1439                         return -EINVAL;
1440                 orig = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1441                 f[orig].mask |= frqt;
1442                 for (i = 1; 2 + (i >> 3) < len; i++)
1443                         if ((cd[2 + (i >> 3)] & (0x80 >> (i & 7))))
1444                                 f[(orig + i) % 1024].mask |= frqt;
1445
1446                 return 0;
1447         }
1448
1449         return 0;
1450 }
1451
1452 /* decode "Cell Selection Parameters" (10.5.2.4) */
1453 static int gsm48_decode_cell_sel_param(struct gsm48_sysinfo *s,
1454         struct gsm48_cell_sel_par *cs)
1455 {
1456 #ifdef TODO
1457         convert ms_txpwr_max_ccch dependant on the current frequenc and support
1458         to the right powe level
1459 #endif
1460         s->ms_txpwr_max_ccch = cs->ms_txpwr_max_ccch;
1461         s->cell_resel_hyst_db = cs->cell_resel_hyst * 2;
1462         s->rxlev_acc_min_db = cs->rxlev_acc_min - 110;
1463         s->neci = cs->neci;
1464         s->acs = cs->acs;
1465
1466         return 0;
1467 }
1468
1469 /* decode "Cell Options (BCCH)" (10.5.2.3) */
1470 static int gsm48_decode_cellopt_bcch(struct gsm48_sysinfo *s,
1471         struct gsm48_cell_options *co)
1472 {
1473         s->bcch_radio_link_timeout = (co->radio_link_timeout + 1) * 4;
1474         s->bcch_dtx = co->dtx;
1475         s->bcch_pwrc = co->pwrc;
1476
1477         return 0;
1478 }
1479
1480 /* decode "Cell Options (SACCH)" (10.5.2.3a) */
1481 static int gsm48_decode_cellopt_sacch(struct gsm48_sysinfo *s,
1482         struct gsm48_cell_options *co)
1483 {
1484         s->sacch_radio_link_timeout = (co->radio_link_timeout + 1) * 4;
1485         s->sacch_dtx = co->dtx;
1486         s->sacch_pwrc = co->pwrc;
1487
1488         return 0;
1489 }
1490
1491 /* decode "Control Channel Description" (10.5.2.11) */
1492 static int gsm48_decode_ccd(struct gsm48_sysinfo *s,
1493         struct gsm48_control_channel_descr *cc)
1494 {
1495         s->ccch_conf = cc->ccch_conf;
1496         s->bs_ag_blks_res = cc->bs_ag_blks_res;
1497         s->att_allowed = cc->att;
1498         s->pag_mf_periods = cc->bs_pa_mfrms + 2;
1499         s->t3212 = cc->t3212 * 360; /* convert deci-hours to seconds */
1500
1501         return 0;
1502 }
1503
1504 /* decode "Mobile Allocation" (10.5.2.21) */
1505 static int gsm48_decode_mobile_alloc(struct gsm48_sysinfo *s,
1506         uint8_t *ma, uint8_t len)
1507 {
1508         int i, j = 0;
1509         uint16_t f[len << 3];
1510
1511         /* not more than 64 hopping indexes allowed in IE */
1512         if (len > 8)
1513                 return -EINVAL;
1514
1515         /* tabula rasa */
1516         s->hopp_len = 0;
1517         for (i = 0; i < 1024; i++)
1518                 s->freq[i].mask &= ~FREQ_TYPE_HOPP;
1519
1520         /* generating list of all frequencies (1..1023,0) */
1521         for (i = 1; i <= 1024; i++) {
1522                 if ((s->freq[i & 1023].mask & FREQ_TYPE_SERV)) {
1523                         f[j++] = i & 1023;
1524                         if (j == (len << 3))
1525                                 break;
1526                 }
1527         }
1528
1529         /* fill hopping table with frequency index given by IE
1530          * and set hopping type bits
1531          */
1532         for (i = 0; i < (len << 3); i++) {
1533                 /* if bit is set, this frequency index is used for hopping */
1534                 if ((ma[len - 1 - (i >> 3)] & (1 << (i & 7)))) {
1535                         /* index higher than entries in list ? */
1536                         if (i >= j) {
1537                                 LOGP(DRR, LOGL_NOTICE, "Mobile Allocation "
1538                                         "hopping index %d exceeds maximum "
1539                                         "number of cell frequencies. (%d)\n",
1540                                         i + 1, j);
1541                                 break;
1542                         }
1543                         s->hopping[s->hopp_len++] = f[i];
1544                         s->freq[f[i]].mask |= FREQ_TYPE_HOPP;
1545                 }
1546         }
1547
1548         return 0;
1549 }
1550
1551 /* Rach Control decode tables */
1552 static uint8_t gsm48_max_retrans[4] = {
1553         1, 2, 4, 7
1554 };
1555 static uint8_t gsm48_tx_integer[16] = {
1556         3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 20, 25, 32, 50
1557 };
1558
1559 /* decode "RACH Control Parameter" (10.5.2.29) */
1560 static int gsm48_decode_rach_ctl_param(struct gsm48_sysinfo *s,
1561         struct gsm48_rach_control *rc)
1562 {
1563         s->reest_denied = rc->re;
1564         s->cell_barr = rc->cell_bar;
1565         s->tx_integer = gsm48_tx_integer[rc->tx_integer];
1566         s->max_retrans = gsm48_max_retrans[rc->max_trans];
1567         s->class_barr = (rc->t2 << 8) | rc->t3;
1568
1569         return 0;
1570 }
1571 static int gsm48_decode_rach_ctl_neigh(struct gsm48_sysinfo *s,
1572         struct gsm48_rach_control *rc)
1573 {
1574         s->nb_reest_denied = rc->re;
1575         s->nb_cell_barr = rc->cell_bar;
1576         s->nb_tx_integer = gsm48_tx_integer[rc->tx_integer];
1577         s->nb_max_retrans = gsm48_max_retrans[rc->max_trans];
1578         s->nb_class_barr = (rc->t2 << 8) | rc->t3;
1579
1580         return 0;
1581 }
1582
1583 /* decode "SI 1 Rest Octets" (10.5.2.32) */
1584 static int gsm48_decode_si1_rest(struct gsm48_sysinfo *s, uint8_t *si,
1585         uint8_t len)
1586 {
1587         return 0;
1588 }
1589
1590 /* decode "SI 3 Rest Octets" (10.5.2.34) */
1591 static int gsm48_decode_si3_rest(struct gsm48_sysinfo *s, uint8_t *si,
1592         uint8_t len)
1593 {
1594         return 0;
1595 }
1596
1597 /* decode "SI 4 Rest Octets" (10.5.2.35) */
1598 static int gsm48_decode_si4_rest(struct gsm48_sysinfo *s, uint8_t *si,
1599         uint8_t len)
1600 {
1601         return 0;
1602 }
1603
1604 /* decode "SI 6 Rest Octets" (10.5.2.35a) */
1605 static int gsm48_decode_si6_rest(struct gsm48_sysinfo *s, uint8_t *si,
1606         uint8_t len)
1607 {
1608         return 0;
1609 }
1610
1611 /* send sysinfo event to other layers */
1612 static int gsm48_send_sysinfo(struct osmocom_ms *ms, uint8_t type)
1613 {
1614         struct msgb *nmsg;
1615         struct gsm322_msg *em;
1616
1617         nmsg = gsm322_msgb_alloc(GSM322_EVENT_SYSINFO);
1618         if (!nmsg)
1619                 return -ENOMEM;
1620         em = (struct gsm322_msg *) nmsg->data;
1621         em->sysinfo = type;
1622         gsm322_cs_sendmsg(ms, nmsg);
1623
1624         /* send timer info to location update process */
1625         nmsg = gsm48_mmevent_msgb_alloc(GSM48_MM_EVENT_SYSINFO);
1626         if (!nmsg)
1627                 return -ENOMEM;
1628         gsm48_mmevent_msg(ms, nmsg);
1629
1630         return 0;
1631 }
1632
1633 /* receive "SYSTEM INFORMATION 1" message (9.1.31) */
1634 static int gsm48_rr_rx_sysinfo1(struct osmocom_ms *ms, struct msgb *msg)
1635 {
1636         struct gsm48_system_information_type_1 *si = msgb_l3(msg);
1637         struct gsm48_sysinfo *s = &ms->sysinfo;
1638         int payload_len = msgb_l3len(msg) - sizeof(*si);
1639
1640         if (payload_len < 0) {
1641                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 1 "
1642                         "message.\n");
1643                 return -EINVAL;
1644         }
1645
1646         if (!memcmp(si, s->si1_msg, MIN(msgb_l3len(msg), sizeof(s->si1_msg))))
1647                 return 0;
1648         memcpy(s->si1_msg, si, MIN(msgb_l3len(msg), sizeof(s->si1_msg)));
1649
1650         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 1\n");
1651
1652         /* Cell Channel Description */
1653         gsm48_decode_freq_list(&ms->support, s->freq,
1654                 si->cell_channel_description,
1655                 sizeof(si->cell_channel_description), 0xce, FREQ_TYPE_SERV);
1656         /* RACH Control Parameter */
1657         gsm48_decode_rach_ctl_param(s, &si->rach_control);
1658         /* SI 1 Rest Octets */
1659         if (payload_len)
1660                 gsm48_decode_si1_rest(s, si->rest_octets, payload_len);
1661
1662         s->si1 = 1;
1663
1664         return gsm48_send_sysinfo(ms, si->header.system_information);
1665 }
1666
1667 /* receive "SYSTEM INFORMATION 2" message (9.1.32) */
1668 static int gsm48_rr_rx_sysinfo2(struct osmocom_ms *ms, struct msgb *msg)
1669 {
1670         struct gsm48_system_information_type_2 *si = msgb_l3(msg);
1671         struct gsm48_sysinfo *s = &ms->sysinfo;
1672         int payload_len = msgb_l3len(msg) - sizeof(*si);
1673
1674         if (payload_len < 0) {
1675                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 2 "
1676                         "message.\n");
1677                 return -EINVAL;
1678         }
1679 //printf("len = %d\n", MIN(msgb_l3len(msg), sizeof(s->si2_msg)));
1680
1681         if (!memcmp(si, s->si2_msg, MIN(msgb_l3len(msg), sizeof(s->si2_msg))))
1682                 return 0;
1683         memcpy(s->si2_msg, si, MIN(msgb_l3len(msg), sizeof(s->si2_msg)));
1684
1685         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 2\n");
1686
1687         /* Neighbor Cell Description */
1688         s->nb_ext_ind_si2 = (si->bcch_frequency_list[0] >> 6) & 1;
1689         s->nb_ba_ind_si2 = (si->bcch_frequency_list[0] >> 5) & 1;
1690         gsm48_decode_freq_list(&ms->support, s->freq, si->bcch_frequency_list,
1691                 sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_NCELL_2);
1692         /* NCC Permitted */
1693         s->nb_ncc_permitted = si->ncc_permitted;
1694         /* RACH Control Parameter */
1695         gsm48_decode_rach_ctl_neigh(s, &si->rach_control);
1696
1697         s->si2 = 1;
1698
1699         return gsm48_send_sysinfo(ms, si->header.system_information);
1700 }
1701
1702 /* receive "SYSTEM INFORMATION 2bis" message (9.1.33) */
1703 static int gsm48_rr_rx_sysinfo2bis(struct osmocom_ms *ms, struct msgb *msg)
1704 {
1705         struct gsm48_system_information_type_2bis *si = msgb_l3(msg);
1706         struct gsm48_sysinfo *s = &ms->sysinfo;
1707         int payload_len = msgb_l3len(msg) - sizeof(*si);
1708
1709         if (payload_len < 0) {
1710                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 2bis "
1711                         "message.\n");
1712                 return -EINVAL;
1713         }
1714
1715         if (!memcmp(si, s->si2b_msg, MIN(msgb_l3len(msg),
1716                         sizeof(s->si2b_msg))))
1717                 return 0;
1718         memcpy(s->si2b_msg, si, MIN(msgb_l3len(msg), sizeof(s->si2b_msg)));
1719
1720         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 2bis\n");
1721
1722         /* Neighbor Cell Description */
1723         s->nb_ext_ind_si2bis = (si->bcch_frequency_list[0] >> 6) & 1;
1724         s->nb_ba_ind_si2bis = (si->bcch_frequency_list[0] >> 5) & 1;
1725         gsm48_decode_freq_list(&ms->support, s->freq,
1726                 si->bcch_frequency_list,
1727                 sizeof(si->bcch_frequency_list), 0x8e,
1728                 FREQ_TYPE_NCELL_2bis);
1729         /* RACH Control Parameter */
1730         gsm48_decode_rach_ctl_neigh(s, &si->rach_control);
1731
1732         s->si2bis = 1;
1733
1734         return gsm48_send_sysinfo(ms, si->header.system_information);
1735 }
1736
1737 /* receive "SYSTEM INFORMATION 2ter" message (9.1.34) */
1738 static int gsm48_rr_rx_sysinfo2ter(struct osmocom_ms *ms, struct msgb *msg)
1739 {
1740         struct gsm48_system_information_type_2ter *si = msgb_l3(msg);
1741         struct gsm48_sysinfo *s = &ms->sysinfo;
1742         int payload_len = msgb_l3len(msg) - sizeof(*si);
1743
1744         if (payload_len < 0) {
1745                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 2ter "
1746                         "message.\n");
1747                 return -EINVAL;
1748         }
1749
1750         if (!memcmp(si, s->si2t_msg, MIN(msgb_l3len(msg),
1751                         sizeof(s->si2t_msg))))
1752                 return 0;
1753         memcpy(s->si2t_msg, si, MIN(msgb_l3len(msg), sizeof(s->si2t_msg)));
1754
1755         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 2ter\n");
1756
1757         /* Neighbor Cell Description 2 */
1758         s->nb_multi_rep_si2ter = (si->ext_bcch_frequency_list[0] >> 6) & 3;
1759         gsm48_decode_freq_list(&ms->support, s->freq,
1760                 si->ext_bcch_frequency_list,
1761                 sizeof(si->ext_bcch_frequency_list), 0x8e,
1762                 FREQ_TYPE_NCELL_2ter);
1763
1764         s->si2ter = 1;
1765
1766         return gsm48_send_sysinfo(ms, si->header.system_information);
1767 }
1768
1769 /* receive "SYSTEM INFORMATION 3" message (9.1.35) */
1770 static int gsm48_rr_rx_sysinfo3(struct osmocom_ms *ms, struct msgb *msg)
1771 {
1772         struct gsm48_system_information_type_3 *si = msgb_l3(msg);
1773         struct gsm48_sysinfo *s = &ms->sysinfo;
1774         int payload_len = msgb_l3len(msg) - sizeof(*si);
1775
1776         if (payload_len < 0) {
1777                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 3 "
1778                         "message.\n");
1779                 return -EINVAL;
1780         }
1781
1782         if (!memcmp(si, s->si3_msg, MIN(msgb_l3len(msg), sizeof(s->si3_msg))))
1783                 return 0;
1784         memcpy(s->si3_msg, si, MIN(msgb_l3len(msg), sizeof(s->si3_msg)));
1785
1786         /* Cell Identity */
1787         s->cell_id = ntohs(si->cell_identity);
1788         /* LAI */
1789         gsm48_decode_lai(&si->lai, &s->mcc, &s->mnc, &s->lac);
1790         /* Control Channel Description */
1791         gsm48_decode_ccd(s, &si->control_channel_desc);
1792         /* Cell Options (BCCH) */
1793         gsm48_decode_cellopt_bcch(s, &si->cell_options);
1794         /* Cell Selection Parameters */
1795         gsm48_decode_cell_sel_param(s, &si->cell_sel_par);
1796         /* RACH Control Parameter */
1797         gsm48_decode_rach_ctl_param(s, &si->rach_control);
1798         /* SI 3 Rest Octets */
1799         if (payload_len >= 4)
1800                 gsm48_decode_si3_rest(s, si->rest_octets, payload_len);
1801
1802         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 3 (mcc %03d mnc %02d "
1803                 "lac 0x%04x)\n", s->mcc, s->mnc, s->lac);
1804
1805         s->si3 = 1;
1806
1807         return gsm48_send_sysinfo(ms, si->header.system_information);
1808 }
1809
1810 /* receive "SYSTEM INFORMATION 4" message (9.1.36) */
1811 static int gsm48_rr_rx_sysinfo4(struct osmocom_ms *ms, struct msgb *msg)
1812 {
1813         /* NOTE: pseudo length is not in this structure, so we skip */
1814         struct gsm48_system_information_type_4 *si = msgb_l3(msg);
1815         struct gsm48_sysinfo *s = &ms->sysinfo;
1816         int payload_len = msgb_l3len(msg) - sizeof(*si);
1817         uint8_t *data = si->data;
1818         struct gsm48_chan_desc *cd;
1819
1820         if (payload_len < 0) {
1821                 short_read:
1822                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 4 "
1823                         "message.\n");
1824                 return -EINVAL;
1825         }
1826
1827         if (!memcmp(si, s->si4_msg, MIN(msgb_l3len(msg), sizeof(s->si4_msg))))
1828                 return 0;
1829         memcpy(s->si4_msg, si, MIN(msgb_l3len(msg), sizeof(s->si4_msg)));
1830
1831         /* LAI */
1832         gsm48_decode_lai(&si->lai, &s->mcc, &s->mnc, &s->lac);
1833         /* Cell Selection Parameters */
1834         gsm48_decode_cell_sel_param(s, &si->cell_sel_par);
1835         /* RACH Control Parameter */
1836         gsm48_decode_rach_ctl_param(s, &si->rach_control);
1837         /* CBCH Channel Description */
1838         if (payload_len >= 1 && data[0] == GSM48_IE_CBCH_CHAN_DESC) {
1839                 if (payload_len < 4)
1840                         goto short_read;
1841                 cd = (struct gsm48_chan_desc *) (data + 1);
1842                 if (cd->h0.h) {
1843                         s->h = 1;
1844                         gsm48_decode_chan_h1(cd, &s->tsc, &s->maio, &s->hsn);
1845                 } else {
1846                         s->h = 0;
1847                         gsm48_decode_chan_h0(cd, &s->tsc, &s->arfcn);
1848                 }
1849                 payload_len -= 4;
1850                 data += 4;
1851         }
1852         /* CBCH Mobile Allocation */
1853         if (payload_len >= 1 && data[0] == GSM48_IE_CBCH_MOB_AL) {
1854                 if (payload_len < 1 || payload_len < 2 + data[1])
1855                         goto short_read;
1856                 gsm48_decode_mobile_alloc(s, data + 2, si->data[1]);
1857                 payload_len -= 2 + data[1];
1858                 data += 2 + data[1];
1859         }
1860         /* SI 4 Rest Octets */
1861         if (payload_len > 0)
1862                 gsm48_decode_si4_rest(s, data, payload_len);
1863
1864         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 4 (mcc %03d mnc %02d "
1865                 "lac 0x%04x)\n", s->mcc, s->mnc, s->lac);
1866
1867         s->si4 = 1;
1868
1869         return gsm48_send_sysinfo(ms, si->header.system_information);
1870 }
1871
1872 /* receive "SYSTEM INFORMATION 5" message (9.1.37) */
1873 static int gsm48_rr_rx_sysinfo5(struct osmocom_ms *ms, struct msgb *msg)
1874 {
1875         /* NOTE: pseudo length is not in this structure, so we skip */
1876         struct gsm48_system_information_type_5 *si = msgb_l3(msg) + 1;
1877         struct gsm48_sysinfo *s = &ms->sysinfo;
1878         int payload_len = msgb_l3len(msg) - sizeof(*si) - 1;
1879
1880         if (payload_len < 0) {
1881                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 5 "
1882                         "message.\n");
1883                 return -EINVAL;
1884         }
1885
1886         if (!memcmp(si, s->si5_msg, MIN(msgb_l3len(msg), sizeof(s->si5_msg))))
1887                 return 0;
1888         memcpy(s->si5_msg, si, MIN(msgb_l3len(msg), sizeof(s->si5_msg)));
1889
1890         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 5\n");
1891
1892         /* Neighbor Cell Description */
1893         s->nb_ext_ind_si5 = (si->bcch_frequency_list[0] >> 6) & 1;
1894         s->nb_ba_ind_si5 = (si->bcch_frequency_list[0] >> 5) & 1;
1895         gsm48_decode_freq_list(&ms->support, s->freq, si->bcch_frequency_list,
1896                 sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_REP_5);
1897
1898         s->si5 = 1;
1899
1900         return gsm48_send_sysinfo(ms, si->system_information);
1901 }
1902
1903 /* receive "SYSTEM INFORMATION 5bis" message (9.1.38) */
1904 static int gsm48_rr_rx_sysinfo5bis(struct osmocom_ms *ms, struct msgb *msg)
1905 {
1906         /* NOTE: pseudo length is not in this structure, so we skip */
1907         struct gsm48_system_information_type_5bis *si = msgb_l3(msg) + 1;
1908         struct gsm48_sysinfo *s = &ms->sysinfo;
1909         int payload_len = msgb_l3len(msg) - sizeof(*si) - 1;
1910
1911         if (payload_len < 0) {
1912                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 5bis "
1913                         "message.\n");
1914                 return -EINVAL;
1915         }
1916
1917         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 5bis\n");
1918
1919         if (!memcmp(si, s->si5b_msg, MIN(msgb_l3len(msg),
1920                         sizeof(s->si5b_msg))))
1921                 return 0;
1922         memcpy(s->si5b_msg, si, MIN(msgb_l3len(msg), sizeof(s->si5b_msg)));
1923
1924         /* Neighbor Cell Description */
1925         s->nb_ext_ind_si5bis = (si->bcch_frequency_list[0] >> 6) & 1;
1926         s->nb_ba_ind_si5bis = (si->bcch_frequency_list[0] >> 5) & 1;
1927         gsm48_decode_freq_list(&ms->support, s->freq, si->bcch_frequency_list,
1928                 sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_REP_5bis);
1929
1930         s->si5bis = 1;
1931
1932         return gsm48_send_sysinfo(ms, si->system_information);
1933 }
1934
1935 /* receive "SYSTEM INFORMATION 5ter" message (9.1.39) */
1936 static int gsm48_rr_rx_sysinfo5ter(struct osmocom_ms *ms, struct msgb *msg)
1937 {
1938         /* NOTE: pseudo length is not in this structure, so we skip */
1939         struct gsm48_system_information_type_5ter *si = msgb_l3(msg) + 1;
1940         struct gsm48_sysinfo *s = &ms->sysinfo;
1941         int payload_len = msgb_l3len(msg) - sizeof(*si) - 1;
1942
1943         if (payload_len < 0) {
1944                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 5ter "
1945                         "message.\n");
1946                 return -EINVAL;
1947         }
1948
1949         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 5ter\n");
1950
1951         if (!memcmp(si, s->si5t_msg, MIN(msgb_l3len(msg),
1952                         sizeof(s->si5t_msg))))
1953                 return 0;
1954         memcpy(s->si5t_msg, si, MIN(msgb_l3len(msg), sizeof(s->si5t_msg)));
1955
1956         /* Neighbor Cell Description */
1957         gsm48_decode_freq_list(&ms->support, s->freq, si->bcch_frequency_list,
1958                 sizeof(si->bcch_frequency_list), 0xce, FREQ_TYPE_REP_5ter);
1959
1960         s->si5ter = 1;
1961
1962         return gsm48_send_sysinfo(ms, si->system_information);
1963 }
1964
1965 /* receive "SYSTEM INFORMATION 6" message (9.1.39) */
1966 static int gsm48_rr_rx_sysinfo6(struct osmocom_ms *ms, struct msgb *msg)
1967 {
1968         /* NOTE: pseudo length is not in this structure, so we skip */
1969         struct gsm48_system_information_type_6 *si = msgb_l3(msg) + 1;
1970         struct gsm48_sysinfo *s = &ms->sysinfo;
1971         int payload_len = msgb_l3len(msg) - sizeof(*si) - 1;
1972
1973         if (payload_len < 0) {
1974                 LOGP(DRR, LOGL_NOTICE, "Short read of SYSTEM INFORMATION 6 "
1975                         "message.\n");
1976                 return -EINVAL;
1977         }
1978
1979         if (!memcmp(si, s->si6_msg, MIN(msgb_l3len(msg), sizeof(s->si6_msg))))
1980                 return 0;
1981         memcpy(s->si6_msg, si, MIN(msgb_l3len(msg), sizeof(s->si6_msg)));
1982
1983         /* Cell Identity */
1984         if (s->si6 && s->cell_id != ntohs(si->cell_identity))
1985                 LOGP(DRR, LOGL_INFO, "Cell ID on SI 6 differs from previous "
1986                         "read.\n");
1987         s->cell_id = ntohs(si->cell_identity);
1988         /* LAI */
1989         gsm48_decode_lai(&si->lai, &s->mcc, &s->mnc, &s->lac);
1990         /* Cell Options (SACCH) */
1991         gsm48_decode_cellopt_sacch(s, &si->cell_options);
1992         /* NCC Permitted */
1993         s->nb_ncc_permitted = si->ncc_permitted;
1994         /* SI 6 Rest Octets */
1995         if (payload_len >= 4)
1996                 gsm48_decode_si6_rest(s, si->rest_octets, payload_len);
1997
1998         LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 6 (mcc %03d mnc %02d "
1999                 "lac 0x%04x)\n", s->mcc, s->mnc, s->lac);
2000
2001         s->si6 = 1;
2002
2003         return gsm48_send_sysinfo(ms, si->system_information);
2004 }
2005
2006 /*
2007  * paging
2008  */
2009
2010 /* paging channel request */
2011 static int gsm48_rr_chan2cause[4] = {
2012         RR_EST_CAUSE_ANS_PAG_ANY,
2013         RR_EST_CAUSE_ANS_PAG_SDCCH,
2014         RR_EST_CAUSE_ANS_PAG_TCH_F,
2015         RR_EST_CAUSE_ANS_PAG_TCH_ANY
2016 };
2017
2018 /* given LV of mobile identity is checked agains ms */
2019 static int gsm_match_mi(struct osmocom_ms *ms, uint8_t *mi)
2020 {
2021         char imsi[16];
2022         uint32_t tmsi;
2023         uint8_t mi_type;
2024
2025         if (mi[0] < 1)
2026                 return 0;
2027         mi_type = mi[1] & GSM_MI_TYPE_MASK;
2028         switch (mi_type) {
2029         case GSM_MI_TYPE_TMSI:
2030                 if (mi[0] < 5)
2031                         return 0;
2032                 memcpy(&tmsi, mi+2, 4);
2033                 if (ms->subscr.tmsi == ntohl(tmsi)
2034                  && ms->subscr.tmsi_valid) {
2035                         LOGP(DRR, LOGL_INFO, "TMSI %08x matches\n", ntohl(tmsi));
2036
2037                         return 1;
2038                 } else
2039                         LOGP(DRR, LOGL_INFO, "TMSI %08x (not for us)\n",
2040                                 ntohl(tmsi));
2041                 break;
2042         case GSM_MI_TYPE_IMSI:
2043                 gsm48_mi_to_string(imsi, sizeof(imsi), mi + 1, mi[0]);
2044                 if (!strcmp(imsi, ms->subscr.imsi)) {
2045                         LOGP(DRR, LOGL_INFO, "IMSI %s matches\n", imsi);
2046
2047                         return 1;
2048                 } else
2049                         LOGP(DRR, LOGL_INFO, "IMSI %s (not for us)\n", imsi);
2050                 break;
2051         default:
2052                 LOGP(DRR, LOGL_NOTICE, "Paging with unsupported MI type %d.\n",
2053                         mi_type);
2054         }
2055
2056         return 0;
2057 }
2058
2059 /* 9.1.22 PAGING REQUEST 1 message received */
2060 static int gsm48_rr_rx_pag_req_1(struct osmocom_ms *ms, struct msgb *msg)
2061 {
2062         struct gsm48_rrlayer *rr = &ms->rrlayer;
2063         struct gsm322_cellsel *cs = &ms->cellsel;
2064         struct gsm48_paging1 *pa = msgb_l3(msg);
2065         int payload_len = msgb_l3len(msg) - sizeof(*pa);
2066         int chan_1, chan_2;
2067         uint8_t *mi;
2068
2069         /* 3.3.1.1.2: ignore paging while not camping on a cell */
2070         if (rr->state != GSM48_RR_ST_IDLE
2071          && cs->state != GSM322_C3_CAMPED_NORMALLY) {
2072                 LOGP(DRR, LOGL_INFO, "PAGING ignored, we are not camping "
2073                         "normally.\n");
2074                 return 0;
2075         }
2076
2077         if (payload_len < 2) {
2078                 short_read:
2079                 LOGP(DRR, LOGL_NOTICE, "Short read of PAGING REQUEST 1 "
2080                         "message.\n");
2081                 return -EINVAL;
2082         }
2083
2084         /* channel needed */
2085         chan_1 = pa->cneed1;
2086         chan_2 = pa->cneed2;
2087         /* first MI */
2088         mi = pa->data + 1;
2089         if (payload_len < mi[0] + 1)
2090                 goto short_read;
2091         if (gsm_match_mi(ms, mi) > 0)
2092                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_1], 1);
2093         /* second MI */
2094         payload_len -= mi[0] + 1;
2095         mi = pa->data + mi[0] + 1;
2096         if (payload_len < 2)
2097                 return 0;
2098         if (mi[0] != GSM48_IE_MOBILE_ID)
2099                 return 0;
2100         if (payload_len < mi[1] + 2)
2101                 goto short_read;
2102         if (gsm_match_mi(ms, mi + 1) > 0)
2103                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_2], 1);
2104
2105         return 0;
2106 }
2107
2108 /* 9.1.23 PAGING REQUEST 2 message received */
2109 static int gsm48_rr_rx_pag_req_2(struct osmocom_ms *ms, struct msgb *msg)
2110 {
2111         struct gsm48_rrlayer *rr = &ms->rrlayer;
2112         struct gsm322_cellsel *cs = &ms->cellsel;
2113         struct gsm48_paging2 *pa = msgb_l3(msg);
2114         int payload_len = msgb_l3len(msg) - sizeof(*pa);
2115         uint8_t *mi;
2116         int chan_1, chan_2, chan_3;
2117
2118         /* 3.3.1.1.2: ignore paging while not camping on a cell */
2119         if (rr->state != GSM48_RR_ST_IDLE
2120          && cs->state != GSM322_C3_CAMPED_NORMALLY) {
2121                 LOGP(DRR, LOGL_INFO, "PAGING ignored, we are not camping "
2122                         "normally.\n");
2123                 return 0;
2124         }
2125
2126         if (payload_len < 0) {
2127                 short_read:
2128                 LOGP(DRR, LOGL_NOTICE, "Short read of PAGING REQUEST 2 "
2129                         "message .\n");
2130                 return -EINVAL;
2131         }
2132
2133         /* channel needed */
2134         chan_1 = pa->cneed1;
2135         chan_2 = pa->cneed2;
2136         /* first MI */
2137         if (ms->subscr.tmsi == ntohl(pa->tmsi1)
2138          && ms->subscr.tmsi_valid)
2139                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_1], 1);
2140         /* second MI */
2141         if (ms->subscr.tmsi == ntohl(pa->tmsi2)
2142          && ms->subscr.tmsi_valid)
2143                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_2], 1);
2144         /* third MI */
2145         mi = pa->data;
2146         if (payload_len < 2)
2147                 return 0;
2148         if (mi[0] != GSM48_IE_MOBILE_ID)
2149                 return 0;
2150         if (payload_len < mi[1] + 2 + 1) /* must include "channel needed" */
2151                 goto short_read;
2152         chan_3 = mi[mi[1] + 2] & 0x03; /* channel needed */
2153         if (gsm_match_mi(ms, mi + 1) > 0)
2154                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_3], 1);
2155
2156         return 0;
2157 }
2158
2159 /* 9.1.24 PAGING REQUEST 3 message received */
2160 static int gsm48_rr_rx_pag_req_3(struct osmocom_ms *ms, struct msgb *msg)
2161 {
2162         struct gsm48_rrlayer *rr = &ms->rrlayer;
2163         struct gsm322_cellsel *cs = &ms->cellsel;
2164         struct gsm48_paging3 *pa = msgb_l3(msg);
2165         int payload_len = msgb_l3len(msg) - sizeof(*pa);
2166         int chan_1, chan_2, chan_3, chan_4;
2167
2168         /* 3.3.1.1.2: ignore paging while not camping on a cell */
2169         if (rr->state != GSM48_RR_ST_IDLE
2170          && cs->state != GSM322_C3_CAMPED_NORMALLY) {
2171                 LOGP(DRR, LOGL_INFO, "PAGING ignored, we are not camping "
2172                         "normally.\n");
2173                 return 0;
2174         }
2175
2176         if (payload_len < 0) { /* must include "channel needed", part of *pa */
2177                 LOGP(DRR, LOGL_NOTICE, "Short read of PAGING REQUEST 3 "
2178                         "message .\n");
2179                 return -EINVAL;
2180         }
2181
2182         /* channel needed */
2183         chan_1 = pa->cneed1;
2184         chan_2 = pa->cneed2;
2185         chan_3 = pa->cneed3;
2186         chan_4 = pa->cneed4;
2187         /* first MI */
2188         if (ms->subscr.tmsi == ntohl(pa->tmsi1)
2189          && ms->subscr.tmsi_valid)
2190                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_1], 1);
2191         /* second MI */
2192         if (ms->subscr.tmsi == ntohl(pa->tmsi2)
2193          && ms->subscr.tmsi_valid)
2194                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_2], 1);
2195         /* thrid MI */
2196         if (ms->subscr.tmsi == ntohl(pa->tmsi3)
2197          && ms->subscr.tmsi_valid)
2198                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_3], 1);
2199         /* fourth MI */
2200         if (ms->subscr.tmsi == ntohl(pa->tmsi4)
2201          && ms->subscr.tmsi_valid)
2202                 return gsm48_rr_tx_chan_req(ms, gsm48_rr_chan2cause[chan_4], 1);
2203
2204         return 0;
2205 }
2206
2207 /*
2208  * (immediate) assignment
2209  */
2210
2211 /* match request reference agains request history */
2212 static int gsm48_match_ra(struct osmocom_ms *ms, struct gsm48_req_ref *ref)
2213 {
2214         struct gsm48_rrlayer *rr = &ms->rrlayer;
2215         int i;
2216
2217         for (i = 0; i < 3; i++) {
2218                 if (rr->cr_hist[i] >= 0
2219                  && ref->ra == rr->cr_hist[i]) {
2220                         LOGP(DRR, LOGL_INFO, "request %02x matches\n", ref->ra);
2221                         // todo: match timeslot
2222                         return 1;
2223                 }
2224         }
2225
2226         return 0;
2227 }
2228
2229 /* 9.1.3 sending ASSIGNMENT COMPLETE */
2230 static int gsm48_rr_tx_ass_cpl(struct osmocom_ms *ms, uint8_t cause)
2231 {
2232         struct msgb *nmsg;
2233         struct gsm48_hdr *gh;
2234         struct gsm48_ass_cpl *ac;
2235
2236         LOGP(DRR, LOGL_INFO, "ASSIGNMENT COMPLETE (cause #%d)\n", cause);
2237
2238         nmsg = gsm48_l3_msgb_alloc();
2239         if (!nmsg)
2240                 return -ENOMEM;
2241         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
2242         ac = (struct gsm48_ass_cpl *) msgb_put(nmsg, sizeof(*ac));
2243
2244         gh->proto_discr = GSM48_PDISC_RR;
2245         gh->msg_type = GSM48_MT_RR_ASS_COMPL;
2246
2247         /* RR_CAUSE */
2248         ac->rr_cause = cause;
2249
2250         return gsm48_send_rsl(ms, RSL_MT_DATA_REQ, nmsg);
2251 }
2252
2253 /* 9.1.4 sending ASSIGNMENT FAILURE */
2254 static int gsm48_rr_tx_ass_fail(struct osmocom_ms *ms, uint8_t cause)
2255 {
2256         struct msgb *nmsg;
2257         struct gsm48_hdr *gh;
2258         struct gsm48_ass_fail *af;
2259
2260         LOGP(DRR, LOGL_INFO, "ASSIGNMENT FAILURE (cause #%d)\n", cause);
2261
2262         nmsg = gsm48_l3_msgb_alloc();
2263         if (!nmsg)
2264                 return -ENOMEM;
2265         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
2266         af = (struct gsm48_ass_fail *) msgb_put(nmsg, sizeof(*af));
2267
2268         gh->proto_discr = GSM48_PDISC_RR;
2269         gh->msg_type = GSM48_MT_RR_ASS_COMPL;
2270
2271         /* RR_CAUSE */
2272         af->rr_cause = cause;
2273
2274         return gsm48_send_rsl(ms, RSL_MT_DATA_REQ, nmsg);
2275 }
2276
2277 /* 9.1.18 IMMEDIATE ASSIGNMENT is received */
2278 static int gsm48_rr_rx_imm_ass(struct osmocom_ms *ms, struct msgb *msg)
2279 {
2280         struct gsm48_rrlayer *rr = &ms->rrlayer;
2281         struct gsm48_imm_ass *ia = msgb_l3(msg);
2282         int ma_len = msgb_l3len(msg) - sizeof(*ia);
2283         uint8_t ch_type, ch_subch, ch_ts;
2284         struct gsm48_rr_cd cd;
2285         uint8_t *st, st_len;
2286
2287         memset(&cd, 0, sizeof(cd));
2288
2289         if (ma_len < 0 /* mobile allocation IE must be included */
2290          || ia->mob_alloc_len > ma_len) { /* short read of IE */
2291                 LOGP(DRR, LOGL_NOTICE, "Short read of IMMEDIATE ASSIGNMENT "
2292                         "message.\n");
2293                 return -EINVAL;
2294         }
2295         if (ia->mob_alloc_len > 8) {
2296                 LOGP(DRR, LOGL_NOTICE, "Moble allocation in IMMEDIATE "
2297                         "ASSIGNMENT too large.\n");
2298                 return -EINVAL;
2299         }
2300
2301         /* starting time */
2302         st_len = ma_len - ia->mob_alloc_len;
2303         st = ia->mob_alloc + ia->mob_alloc_len;
2304         if (st_len >= 3 && st[0] == GSM48_IE_START_TIME)
2305                 gsm48_decode_start_time(&cd, (struct gsm48_start_time *)(st+1));
2306
2307         /* decode channel description */
2308         rsl_dec_chan_nr(ia->chan_desc.chan_nr, &ch_type, &ch_subch, &ch_ts);
2309         if (ia->chan_desc.h0.h) {
2310                 cd.h = 1;
2311                 gsm48_decode_chan_h1(&ia->chan_desc, &cd.tsc, &cd.maio,
2312                         &cd.hsn);
2313                 LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT (ra=0x%02x, "
2314                         "chan_nr=0x%02x, MAIO=%u, HSN=%u, TS=%u, SS=%u, "
2315                         "TSC=%u)\n", ia->req_ref.ra, ia->chan_desc.chan_nr,
2316                         cd.maio, cd.hsn, ch_ts, ch_subch, cd.tsc);
2317         } else {
2318                 cd.h = 0;
2319                 gsm48_decode_chan_h0(&ia->chan_desc, &cd.tsc, &cd.arfcn);
2320                 LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT (ra=0x%02x, "
2321                         "chan_nr=0x%02x, ARFCN=%u, TS=%u, SS=%u, TSC=%u)\n",
2322                         ia->req_ref.ra, ia->chan_desc.chan_nr, cd.arfcn,
2323                         ch_ts, ch_subch, cd.tsc);
2324         }
2325
2326         /* 3.3.1.1.2: ignore assignment while idle */
2327         if (rr->state != GSM48_RR_ST_CONN_PEND || !rr->wait_assign) {
2328                 LOGP(DRR, LOGL_INFO, "no previous request\n");
2329                 return 0;
2330         }
2331
2332         /* request ref */
2333         if (gsm48_match_ra(ms, &ia->req_ref)) {
2334                 /* channel description */
2335                 memcpy(&rr->cd_now, &cd, sizeof(rr->cd_now));
2336                 /* timing advance */
2337                 rr->cd_now.ta = ia->timing_advance;
2338                 /* mobile allocation */
2339                 memcpy(&rr->cd_now.mob_alloc_lv, &ia->mob_alloc_len, 
2340                         ia->mob_alloc_len + 1);
2341                 rr->wait_assign = 0;
2342                 return gsm48_rr_dl_est(ms);
2343         }
2344         LOGP(DRR, LOGL_INFO, "does not match our request\n");
2345
2346         return 0;
2347 }
2348
2349 /* 9.1.19 IMMEDIATE ASSIGNMENT EXTENDED is received */
2350 static int gsm48_rr_rx_imm_ass_ext(struct osmocom_ms *ms, struct msgb *msg)
2351 {
2352         struct gsm48_rrlayer *rr = &ms->rrlayer;
2353         struct gsm48_imm_ass_ext *ia = msgb_l3(msg);
2354         int ma_len = msgb_l3len(msg) - sizeof(*ia);
2355         uint8_t ch_type, ch_subch, ch_ts;
2356         struct gsm48_rr_cd cd1, cd2;
2357         uint8_t *st, st_len;
2358
2359         memset(&cd1, 0, sizeof(cd1));
2360         memset(&cd2, 0, sizeof(cd2));
2361
2362         if (ma_len < 0 /* mobile allocation IE must be included */
2363          || ia->mob_alloc_len > ma_len) { /* short read of IE */
2364                 LOGP(DRR, LOGL_NOTICE, "Short read of IMMEDIATE ASSIGNMENT "
2365                         "EXTENDED message.\n");
2366                 return -EINVAL;
2367         }
2368         if (ia->mob_alloc_len > 4) {
2369                 LOGP(DRR, LOGL_NOTICE, "Moble allocation in IMMEDIATE "
2370                         "ASSIGNMENT EXTENDED too large.\n");
2371                 return -EINVAL;
2372         }
2373
2374         /* starting time */
2375         st_len = ma_len - ia->mob_alloc_len;
2376         st = ia->mob_alloc + ia->mob_alloc_len;
2377         if (st_len >= 3 && st[0] == GSM48_IE_START_TIME) {
2378                 gsm48_decode_start_time(&cd1,
2379                         (struct gsm48_start_time *)(st+1));
2380                 memcpy(&cd2, &cd1, sizeof(cd2));
2381         }
2382
2383         /* decode channel description */
2384         LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT ");
2385         rsl_dec_chan_nr(ia->chan_desc1.chan_nr, &ch_type, &ch_subch, &ch_ts);
2386         if (ia->chan_desc1.h0.h) {
2387                 cd1.h = 1;
2388                 gsm48_decode_chan_h1(&ia->chan_desc1, &cd1.tsc, &cd1.maio,
2389                         &cd1.hsn);
2390                 LOGP(DRR, LOGL_INFO, "1(ra=0x%02x, "
2391                         "chan_nr=0x%02x, MAIO=%u, HSN=%u, TS=%u, SS=%u, "
2392                         "TSC=%u)\n", ia->req_ref1.ra, ia->chan_desc1.chan_nr,
2393                         cd1.maio, cd1.hsn, ch_ts, ch_subch, cd1.tsc);
2394         } else {
2395                 cd1.h = 0;
2396                 gsm48_decode_chan_h0(&ia->chan_desc1, &cd1.tsc, &cd1.arfcn);
2397                 LOGP(DRR, LOGL_INFO, "1(ra=0x%02x, "
2398                         "chan_nr=0x%02x, ARFCN=%u, TS=%u, SS=%u, TSC=%u)\n",
2399                         ia->req_ref1.ra, ia->chan_desc1.chan_nr, cd1.arfcn,
2400                         ch_ts, ch_subch, cd1.tsc);
2401         }
2402         rsl_dec_chan_nr(ia->chan_desc2.chan_nr, &ch_type, &ch_subch, &ch_ts);
2403         if (ia->chan_desc2.h0.h) {
2404                 cd2.h = 1;
2405                 gsm48_decode_chan_h1(&ia->chan_desc2, &cd1.tsc, &cd1.maio,
2406                         &cd1.hsn);
2407                 LOGP(DRR, LOGL_INFO, "2(ra=0x%02x, "
2408                         "chan_nr=0x%02x, MAIO=%u, HSN=%u, TS=%u, SS=%u, "
2409                         "TSC=%u)\n", ia->req_ref2.ra, ia->chan_desc2.chan_nr,
2410                         cd2.maio, cd2.hsn, ch_ts, ch_subch, cd2.tsc);
2411         } else {
2412                 cd2.h = 0;
2413                 gsm48_decode_chan_h0(&ia->chan_desc2, &cd2.tsc, &cd2.arfcn);
2414                 LOGP(DRR, LOGL_INFO, "2(ra=0x%02x, "
2415                         "chan_nr=0x%02x, ARFCN=%u, TS=%u, SS=%u, TSC=%u)\n",
2416                         ia->req_ref2.ra, ia->chan_desc2.chan_nr, cd2.arfcn,
2417                         ch_ts, ch_subch, cd2.tsc);
2418         }
2419
2420         /* 3.3.1.1.2: ignore assignment while idle */
2421         if (rr->state != GSM48_RR_ST_CONN_PEND || !rr->wait_assign) {
2422                 LOGP(DRR, LOGL_INFO, "no previous request\n");
2423                 return 0;
2424         }
2425
2426         /* request ref 1 */
2427         if (gsm48_match_ra(ms, &ia->req_ref1)) {
2428                 /* channel description */
2429                 memcpy(&rr->cd_now, &cd1, sizeof(rr->cd_now));
2430                 /* timing advance */
2431                 rr->cd_now.ta = ia->timing_advance1;
2432                 /* mobile allocation */
2433                 memcpy(&rr->cd_now.mob_alloc_lv, &ia->mob_alloc_len,
2434                         ia->mob_alloc_len + 1);
2435                 rr->wait_assign = 0;
2436                 return gsm48_rr_dl_est(ms);
2437         }
2438         /* request ref 1 */
2439         if (gsm48_match_ra(ms, &ia->req_ref2)) {
2440                 /* channel description */
2441                 memcpy(&rr->cd_now, &cd2, sizeof(rr->cd_now));
2442                 /* timing advance */
2443                 rr->cd_now.ta = ia->timing_advance2;
2444                 /* mobile allocation */
2445                 memcpy(&rr->cd_now.mob_alloc_lv, &ia->mob_alloc_len,
2446                         ia->mob_alloc_len + 1);
2447                 rr->wait_assign = 0;
2448                 return gsm48_rr_dl_est(ms);
2449         }
2450         LOGP(DRR, LOGL_INFO, "does not match our request\n");
2451
2452         return 0;
2453 }
2454
2455 /* 9.1.20 IMMEDIATE ASSIGNMENT REJECT is received */
2456 static int gsm48_rr_rx_imm_ass_rej(struct osmocom_ms *ms, struct msgb *msg)
2457 {
2458         struct gsm48_rrlayer *rr = &ms->rrlayer;
2459         struct gsm48_imm_ass_rej *ia = msgb_l3(msg);
2460         int payload_len = msgb_l3len(msg) - sizeof(*ia);
2461         int i;
2462         struct gsm48_req_ref *req_ref;
2463         uint8_t t3122_value;
2464
2465         /* 3.3.1.1.2: ignore assignment while idle */
2466         if (rr->state != GSM48_RR_ST_CONN_PEND || !rr->wait_assign)
2467                 return 0;
2468
2469         if (payload_len < 0) {
2470                 LOGP(DRR, LOGL_NOTICE, "Short read of IMMEDIATE ASSIGNMENT "
2471                         "REJECT message.\n");
2472                 return -EINVAL;
2473         }
2474
2475         for (i = 0; i < 4; i++) {
2476                 /* request reference */
2477                 req_ref = (struct gsm48_req_ref *)
2478                                 (((uint8_t *)&ia->req_ref1) + i * 4);
2479                 LOGP(DRR, LOGL_INFO, "IMMEDIATE ASSIGNMENT REJECT "
2480                         "(ref 0x%02x)\n", req_ref->ra);
2481                 if (gsm48_match_ra(ms, req_ref)) {
2482                         /* wait indication */
2483                         t3122_value = *(((uint8_t *)&ia->wait_ind1) + i * 4);
2484                         if (t3122_value)
2485                                 start_rr_t3122(rr, t3122_value, 0);
2486                         /* start timer 3126 if not already */
2487                         if (!bsc_timer_pending(&rr->t3126))
2488                                 start_rr_t3126(rr, 5, 0); /* TODO improve! */
2489                         /* stop assignmnet requests */
2490                         rr->n_chan_req = 0;
2491
2492                         /* wait until timer 3126 expires, then release
2493                          * or wait for channel assignment */
2494                         return 0;
2495                 }
2496         }
2497
2498         return 0;
2499 }
2500
2501 /* 9.1.1 ADDITIONAL ASSIGMENT is received  */
2502 static int gsm48_rr_rx_add_ass(struct osmocom_ms *ms, struct msgb *msg)
2503 {
2504         struct gsm48_hdr *gh = msgb_l3(msg);
2505         struct gsm48_add_ass *aa = (struct gsm48_add_ass *)gh->data;
2506         int payload_len = msgb_l3len(msg) - sizeof(*gh) - sizeof(*aa);
2507         struct tlv_parsed tp;
2508
2509         if (payload_len < 0) {
2510                 LOGP(DRR, LOGL_NOTICE, "Short read of ADDITIONAL ASSIGNMENT "
2511                         "message.\n");
2512                 return gsm48_rr_tx_rr_status(ms,
2513                         GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
2514         }
2515         tlv_parse(&tp, &gsm48_rr_att_tlvdef, aa->data, payload_len, 0, 0);
2516
2517         return gsm48_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
2518 }
2519
2520 /*
2521  * measturement reports
2522  */
2523
2524 static int gsm48_rr_tx_meas_rep(struct osmocom_ms *ms)
2525 {
2526         struct gsm48_rrlayer *rr = &ms->rrlayer;
2527         struct gsm48_rr_meas *meas = &rr->meas;
2528         struct msgb *nmsg;
2529         struct gsm48_hdr *gh;
2530         struct gsm48_meas_res *mr;
2531
2532         nmsg = gsm48_l3_msgb_alloc();
2533         if (!nmsg)
2534                 return -ENOMEM;
2535         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
2536         mr = (struct gsm48_meas_res *) msgb_put(nmsg, sizeof(*mr));
2537
2538         gh->proto_discr = GSM48_PDISC_RR;
2539         gh->msg_type = GSM48_MT_RR_MEAS_REP;
2540
2541         /* measurement results */
2542         mr->rxlev_full = meas->rxlev_full;
2543         mr->rxlev_sub = meas->rxlev_sub;
2544         mr->rxqual_full = meas->rxqual_full;
2545         mr->rxqual_sub = meas->rxqual_sub;
2546         mr->dtx_used = meas->dtx;
2547         mr->ba_used = meas->ba;
2548         mr->meas_valid = meas->meas_valid;
2549         if (meas->ncell_na) {
2550                 /* no results for serving cells */
2551                 mr->no_nc_n_hi = 1;
2552                 mr->no_nc_n_lo = 3;
2553         } else {
2554                 mr->no_nc_n_hi = meas->count >> 2;
2555                 mr->no_nc_n_lo = meas->count & 3;
2556         }
2557         mr->rxlev_nc1 = meas->rxlev_nc[0];
2558         mr->rxlev_nc2_hi = meas->rxlev_nc[1] >> 1;
2559         mr->rxlev_nc2_lo = meas->rxlev_nc[1] & 1;
2560         mr->rxlev_nc3_hi = meas->rxlev_nc[2] >> 2;
2561         mr->rxlev_nc3_lo = meas->rxlev_nc[2] & 3;
2562         mr->rxlev_nc4_hi = meas->rxlev_nc[3] >> 3;
2563         mr->rxlev_nc4_lo = meas->rxlev_nc[3] & 7;
2564         mr->rxlev_nc5_hi = meas->rxlev_nc[4] >> 4;
2565         mr->rxlev_nc5_lo = meas->rxlev_nc[4] & 15;
2566         mr->rxlev_nc6_hi = meas->rxlev_nc[5] >> 5;
2567         mr->rxlev_nc6_lo = meas->rxlev_nc[5] & 31;
2568         mr->bsic_nc1_hi = meas->bsic_nc[0] >> 3;
2569         mr->bsic_nc1_lo = meas->bsic_nc[0] & 7;
2570         mr->bsic_nc2_hi = meas->bsic_nc[1] >> 4;
2571         mr->bsic_nc2_lo = meas->bsic_nc[1] & 15;
2572         mr->bsic_nc3_hi = meas->bsic_nc[2] >> 5;
2573         mr->bsic_nc3_lo = meas->bsic_nc[2] & 31;
2574         mr->bsic_nc4 = meas->bsic_nc[3];
2575         mr->bsic_nc5 = meas->bsic_nc[4];
2576         mr->bsic_nc6 = meas->bsic_nc[5];
2577         mr->bcch_f_nc1 = meas->bcch_f_nc[0];
2578         mr->bcch_f_nc2 = meas->bcch_f_nc[1];
2579         mr->bcch_f_nc3 = meas->bcch_f_nc[2];
2580         mr->bcch_f_nc4 = meas->bcch_f_nc[3];
2581         mr->bcch_f_nc5_hi = meas->bcch_f_nc[4] >> 1;
2582         mr->bcch_f_nc5_lo = meas->bcch_f_nc[4] & 1;
2583         mr->bcch_f_nc6_hi = meas->bcch_f_nc[5] >> 2;
2584         mr->bcch_f_nc6_lo = meas->bcch_f_nc[5] & 3;
2585
2586         return gsm48_send_rsl(ms, RSL_MT_UNIT_DATA_REQ, nmsg);
2587 }
2588
2589 /*
2590  * link establishment and release
2591  */
2592
2593 /* activate link and send establish request */
2594 static int gsm48_rr_dl_est(struct osmocom_ms *ms)
2595 {
2596         struct gsm48_rrlayer *rr = &ms->rrlayer;
2597         struct gsm_subscriber *subscr = &ms->subscr;
2598         struct msgb *nmsg;
2599         struct gsm48_hdr *gh;
2600         struct gsm48_pag_rsp *pr;
2601         uint8_t mi[11];
2602
2603         /* 3.3.1.1.3.1 */
2604         stop_rr_t3126(rr);
2605
2606         /* flush pending RACH requests */
2607 #ifdef TODO
2608         rr->n_chan_req = 0; // just to be safe
2609         nmsg = msgb_alloc_headroom(20, 16, "RAND_FLUSH");
2610         if (!nmsg)
2611                 return -ENOMEM;
2612         gsm48_send_rsl(ms, RSL_MT_RAND_ACC_FLSH, msg);
2613 #else
2614         if (bsc_timer_pending(&rr->temp_rach_ti))
2615                 bsc_del_timer(&rr->temp_rach_ti);
2616 #endif
2617
2618         /* send DL_EST_REQ */
2619         if (rr->rr_est_msg) {
2620                 /* use queued message */
2621                 nmsg = rr->rr_est_msg;
2622                 rr->rr_est_msg = 0;
2623                 LOGP(DRR, LOGL_INFO, "sending establish message\n");
2624         } else {
2625                 /* create paging response */
2626                 nmsg = gsm48_l3_msgb_alloc();
2627                 if (!nmsg)
2628                         return -ENOMEM;
2629                 gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
2630                 pr = (struct gsm48_pag_rsp *) msgb_put(nmsg, sizeof(*pr));
2631                 /* key sequence */
2632                 pr->key_seq = subscr->key_seq;
2633                 /* classmark 2 */
2634                 pr->cm2_len = sizeof(pr->cm2);
2635                 gsm48_rr_enc_cm2(ms, &pr->cm2);
2636                 /* mobile identity */
2637                 if (ms->subscr.tmsi_valid) {
2638                         gsm48_generate_mid_from_tmsi(mi, subscr->tmsi);
2639                         LOGP(DRR, LOGL_INFO, "sending paging response with "
2640                                 "TMSI\n");
2641                 } else if (subscr->imsi[0]) {
2642                         gsm48_generate_mid_from_imsi(mi, subscr->imsi);
2643                         LOGP(DRR, LOGL_INFO, "sending paging response with "
2644                                 "IMSI\n");
2645                 } else {
2646                         mi[1] = 1;
2647                         mi[2] = 0xf0 | GSM_MI_TYPE_NONE;
2648                         LOGP(DRR, LOGL_INFO, "sending paging response without "
2649                                 "TMSI/IMSI\n");
2650                 }
2651                 msgb_put(nmsg, 1 + mi[1]);
2652                 memcpy(pr->data, mi + 1, 1 + mi[1]);
2653         }
2654
2655         /* activate channel */
2656 #ifdef TODO
2657         RSL_MT_ to activate channel with all the cd_now informations
2658 #else
2659         rr->arfcn = rr->cd_now.chan_nr;
2660         tx_ph_dm_est_req(ms, rr->cd_now.arfcn, rr->arfcn);
2661 #endif
2662
2663         /* start establishmnet */
2664         return gsm48_send_rsl(ms, RSL_MT_EST_REQ, nmsg);
2665 }
2666
2667 /* the link is established */
2668 static int gsm48_rr_estab_cnf(struct osmocom_ms *ms, struct msgb *msg)
2669 {
2670         struct gsm48_rrlayer *rr = &ms->rrlayer;
2671         struct msgb *nmsg;
2672
2673         /* if MM has releases before confirm, we start release */
2674         if (rr->state == GSM48_RR_ST_IDLE) {
2675                 LOGP(DRR, LOGL_INFO, "MM already released RR.\n");
2676                 /* release message */
2677                 nmsg = gsm48_l3_msgb_alloc();
2678                 if (!nmsg)
2679                         return -ENOMEM;
2680                 /* start release */
2681                 return gsm48_send_rsl(ms, RSL_MT_REL_REQ, nmsg);
2682         }
2683
2684         /* 3.3.1.1.4 */
2685         new_rr_state(rr, GSM48_RR_ST_DEDICATED);
2686
2687         /* send confirm to upper layer */
2688         nmsg = gsm48_rr_msgb_alloc(
2689                 (rr->rr_est_req) ? GSM48_RR_EST_CNF : GSM48_RR_EST_IND);
2690         if (!nmsg)
2691                 return -ENOMEM;
2692         return gsm48_rr_upmsg(ms, nmsg);
2693 }
2694
2695 /* the link is released */
2696 static int gsm48_rr_rel_cnf(struct osmocom_ms *ms, struct msgb *msg)
2697 {
2698         struct gsm48_rrlayer *rr = &ms->rrlayer;
2699
2700         /* deactivate channel */
2701         LOGP(DRR, LOGL_INFO, "deactivating channel (arfcn %d)\n", rr->arfcn);
2702 #ifdef TODO
2703         release and give new arfcn
2704         tx_ph_dm_rel_req(ms, arfcn, rr->chan_desc.chan_desc.chan_nr);
2705 #else
2706         l1ctl_tx_ccch_req(ms, rr->arfcn);
2707 #endif
2708
2709         /* do nothing, because we aleady IDLE
2710          * or we received the rel cnf of the last connection
2711          * while already requesting a new one (CONN PEND)
2712          */
2713
2714         return 0;
2715 }
2716
2717 /*
2718  * radio ressource requests 
2719  */
2720
2721 /* establish request for dedicated mode */
2722 static int gsm48_rr_est_req(struct osmocom_ms *ms, struct msgb *msg)
2723 {
2724         struct gsm48_rrlayer *rr = &ms->rrlayer;
2725         struct gsm322_cellsel *cs = &ms->cellsel;
2726         struct gsm48_sysinfo *s = &ms->sysinfo;
2727         struct gsm_subscriber *subscr = &ms->subscr;
2728         struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *) msg->data;
2729         struct gsm48_hdr *gh = msgb_l3(msg);
2730         uint8_t cause;
2731         struct msgb *nmsg;
2732         struct gsm48_rr_hdr *nrrh;
2733         uint16_t acc_class;
2734
2735         /* 3.3.1.1.3.2 */
2736         if (bsc_timer_pending(&rr->t3122)) {
2737                 if (rrh->cause != RR_EST_CAUSE_EMERGENCY) {
2738                         LOGP(DRR, LOGL_INFO, "T3122 running, rejecting!\n");
2739                         cause = RR_REL_CAUSE_T3122;
2740                         reject:
2741                         nmsg = gsm48_rr_msgb_alloc(GSM48_RR_REL_IND);
2742                         if (!nmsg)
2743                                 return -ENOMEM;
2744                         nrrh = (struct gsm48_rr_hdr *)nmsg->data;
2745                         nrrh->cause = cause;
2746                         return gsm48_rr_upmsg(ms, nmsg);
2747                 }
2748                 LOGP(DRR, LOGL_INFO, "T3122 running, but emergency call\n");
2749                 stop_rr_t3122(rr);
2750         }
2751
2752         /* check if camping */
2753         if (cs->state != GSM322_C3_CAMPED_NORMALLY
2754          && rrh->cause != RR_EST_CAUSE_EMERGENCY) {
2755                 LOGP(DRR, LOGL_INFO, "Not camping normally, rejecting!\n");
2756                 cause = RR_REL_CAUSE_EMERGENCY_ONLY;
2757                 goto reject;
2758         }
2759         if (cs->state != GSM322_C3_CAMPED_NORMALLY
2760          && cs->state != GSM322_C7_CAMPED_ANY_CELL) {
2761                 LOGP(DRR, LOGL_INFO, "Not camping, rejecting!\n");
2762                 cause = RR_REL_CAUSE_TRY_LATER;
2763                 goto reject;
2764         }
2765
2766         /* check for relevant informations */
2767         if (!s->si3) {
2768                 LOGP(DRR, LOGL_INFO, "Not enough SI, rejecting!\n");
2769                 cause = RR_REL_CAUSE_TRY_LATER;
2770                 goto reject;
2771         }
2772
2773         /* 3.3.1.1.1 */
2774         if (!subscr->acc_barr && s->cell_barr) {
2775                 LOGP(DRR, LOGL_INFO, "Cell barred, rejecting!\n");
2776                 cause = RR_REL_CAUSE_NOT_AUTHORIZED;
2777                 goto reject;
2778         }
2779         if (rrh->cause == RR_EST_CAUSE_EMERGENCY)
2780                 acc_class = subscr->acc_class | 0x0400;
2781         else
2782                 acc_class = subscr->acc_class & 0xfbff;
2783         if (!subscr->acc_barr && !(acc_class & (s->class_barr ^ 0xffff))) {
2784                 LOGP(DRR, LOGL_INFO, "Cell barred for our access class (access "
2785                         "%04x barred %04x)!\n", acc_class, s->class_barr);
2786                 cause = RR_REL_CAUSE_NOT_AUTHORIZED;
2787                 goto reject;
2788         }
2789
2790         /* requested by RR */
2791         rr->rr_est_req = 1;
2792
2793         /* clone and store REQUEST message */
2794         if (!gh) {
2795                 LOGP(DRR, LOGL_ERROR, "Error, missing l3 message\n");
2796                 return -EINVAL;
2797         }
2798         rr->rr_est_msg = msgb_alloc_headroom(256, 16, "EST_REQ");
2799         if (!rr->rr_est_msg)
2800                 return -ENOMEM;
2801         memcpy(msgb_put(rr->rr_est_msg, msgb_l3len(msg)),
2802                 msgb_l3(msg), msgb_l3len(msg));
2803
2804         /* request channel */
2805         return gsm48_rr_tx_chan_req(ms, rrh->cause, 0);
2806 }
2807
2808 /* send all queued messages down to layer 2 */
2809 static int gsm48_rr_dequeue_down(struct osmocom_ms *ms)
2810 {
2811         struct gsm48_rrlayer *rr = &ms->rrlayer;
2812         struct msgb *msg;
2813
2814         while((msg = msgb_dequeue(&rr->downqueue))) {
2815                 LOGP(DRR, LOGL_INFO, "Sending queued message.\n");
2816                 gsm48_send_rsl(ms, RSL_MT_DATA_REQ, msg);
2817         }
2818
2819         return 0;
2820 }
2821
2822 /* 3.4.2 transfer data in dedicated mode */
2823 static int gsm48_rr_data_req(struct osmocom_ms *ms, struct msgb *msg)
2824 {
2825         struct gsm48_rrlayer *rr = &ms->rrlayer;
2826
2827         if (rr->state != GSM48_RR_ST_DEDICATED) {
2828                 msgb_free(msg);
2829                 return -EINVAL;
2830         }
2831         
2832         /* pull RR header */
2833         msgb_pull(msg, sizeof(struct gsm48_rr_hdr));
2834
2835         /* queue message, during handover or assignment procedure */
2836         if (rr->hando_susp_state || rr->assign_susp_state) {
2837                 LOGP(DRR, LOGL_INFO, "Queueing message during suspend.\n");
2838                 msgb_enqueue(&rr->downqueue, msg);
2839                 return 0;
2840         }
2841
2842         /* forward message */
2843         return gsm48_send_rsl(ms, RSL_MT_DATA_REQ, msg);
2844 }
2845
2846 /*
2847  * data indications from data link
2848  */
2849
2850 /* 3.4.2 data from layer 2 to RR and upper layer*/
2851 static int gsm48_rr_data_ind(struct osmocom_ms *ms, struct msgb *msg)
2852 {
2853         struct gsm48_hdr *gh = msgb_l3(msg);
2854         struct gsm48_rr_hdr *rrh;
2855         uint8_t pdisc = gh->proto_discr & 0x0f;
2856
2857         if (pdisc == GSM48_PDISC_RR) {
2858                 int rc = -EINVAL;
2859                 uint8_t skip_ind = (gh->proto_discr & 0xf0) >> 4;
2860
2861                 /* ignore if skip indicator is not B'0000' */
2862                 if (skip_ind)
2863                         return 0;
2864
2865                 switch(gh->msg_type) {
2866                 case GSM48_MT_RR_ADD_ASS:
2867                         rc = gsm48_rr_rx_add_ass(ms, msg);
2868                         break;
2869 #if 0
2870                 case GSM48_MT_RR_ASS_CMD:
2871                         rc = gsm48_rr_rx_ass_cmd(ms, msg);
2872                         break;
2873                 case GSM48_MT_RR_CIP_MODE_CMD:
2874                         rc = gsm48_rr_rx_cip_mode_cmd(ms, msg);
2875                         break;
2876 #endif
2877                 case GSM48_MT_RR_CLSM_ENQ:
2878                         rc = gsm48_rr_rx_cm_enq(ms, msg);
2879                         break;
2880 #if 0
2881                 case GSM48_MT_RR_HANDO_CMD:
2882                         rc = gsm48_rr_rx_hando_cmd(ms, msg);
2883                         break;
2884                 case GSM48_MT_RR_FREQ_REDEF:
2885                         rc = gsm48_rr_rx_freq_redef(ms, msg);
2886                         break;
2887 #endif
2888                 default:
2889                         LOGP(DRR, LOGL_NOTICE, "Message type 0x%02x unknown.\n",
2890                                 gh->msg_type);
2891
2892                         /* status message */
2893                         gsm48_rr_tx_rr_status(ms, GSM48_RR_CAUSE_MSG_TYPE_N);
2894                 }
2895
2896                 msgb_free(msg);
2897                 return rc;
2898         }
2899
2900         /* pull off RSL header up to L3 message */
2901         msgb_pull(msg, (long)msgb_l3(msg) - (long)msg->data);
2902
2903         /* push RR header */
2904         msgb_push(msg, sizeof(struct gsm48_rr_hdr));
2905         rrh = (struct gsm48_rr_hdr *)msg->data;
2906         rrh->msg_type = GSM48_RR_DATA_IND;
2907
2908         return gsm48_rr_upmsg(ms, msg);
2909 }
2910
2911 /* receive BCCH at RR layer */
2912 static int gsm48_rr_rx_bcch(struct osmocom_ms *ms, struct msgb *msg)
2913 {
2914         struct gsm48_system_information_type_header *sih = msgb_l3(msg);
2915
2916         switch (sih->system_information) {
2917         case GSM48_MT_RR_SYSINFO_1:
2918                 return gsm48_rr_rx_sysinfo1(ms, msg);
2919         case GSM48_MT_RR_SYSINFO_2:
2920                 return gsm48_rr_rx_sysinfo2(ms, msg);
2921         case GSM48_MT_RR_SYSINFO_2bis:
2922                 return gsm48_rr_rx_sysinfo2bis(ms, msg);
2923         case GSM48_MT_RR_SYSINFO_2ter:
2924                 return gsm48_rr_rx_sysinfo2ter(ms, msg);
2925         case GSM48_MT_RR_SYSINFO_3:
2926                 return gsm48_rr_rx_sysinfo3(ms, msg);
2927         case GSM48_MT_RR_SYSINFO_4:
2928                 return gsm48_rr_rx_sysinfo4(ms, msg);
2929         default:
2930                 LOGP(DRR, LOGL_NOTICE, "BCCH message type 0x%02x unknown.\n",
2931                         sih->system_information);
2932                 return -EINVAL;
2933         }
2934 }
2935
2936 /* receive CCCH at RR layer */
2937 static int gsm48_rr_rx_ccch(struct osmocom_ms *ms, struct msgb *msg)
2938 {
2939         struct gsm48_system_information_type_header *sih = msgb_l3(msg);
2940         struct gsm322_cellsel *cs = &ms->cellsel;
2941
2942         /* when changing/deactivating ccch, ignore pending messages */
2943         if (!cs->ccch_active)
2944                 return -EINVAL;
2945
2946         switch (sih->system_information) {
2947         case GSM48_MT_RR_SYSINFO_5:
2948                 return gsm48_rr_rx_sysinfo5(ms, msg);
2949         case GSM48_MT_RR_SYSINFO_5bis:
2950                 return gsm48_rr_rx_sysinfo5bis(ms, msg);
2951         case GSM48_MT_RR_SYSINFO_5ter:
2952                 return gsm48_rr_rx_sysinfo5ter(ms, msg);
2953         case GSM48_MT_RR_SYSINFO_6:
2954                 return gsm48_rr_rx_sysinfo6(ms, msg);
2955
2956         case GSM48_MT_RR_PAG_REQ_1:
2957                 return gsm48_rr_rx_pag_req_1(ms, msg);
2958         case GSM48_MT_RR_PAG_REQ_2:
2959                 return gsm48_rr_rx_pag_req_2(ms, msg);
2960         case GSM48_MT_RR_PAG_REQ_3:
2961                 return gsm48_rr_rx_pag_req_3(ms, msg);
2962         case GSM48_MT_RR_IMM_ASS:
2963                 return gsm48_rr_rx_imm_ass(ms, msg);
2964         case GSM48_MT_RR_IMM_ASS_EXT:
2965                 return gsm48_rr_rx_imm_ass_ext(ms, msg);
2966         case GSM48_MT_RR_IMM_ASS_REJ:
2967                 return gsm48_rr_rx_imm_ass_rej(ms, msg);
2968         default:
2969                 LOGP(DRR, LOGL_NOTICE, "CCCH message type 0x%02x unknown.\n",
2970                         sih->system_information);
2971                 return -EINVAL;
2972         }
2973 }
2974
2975 /* unit data from layer 2 to RR layer */
2976 static int gsm48_rr_unit_data_ind(struct osmocom_ms *ms, struct msgb *msg)
2977 {
2978         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2979         struct tlv_parsed tv;
2980         
2981         DEBUGP(DRSL, "RSLms UNIT DATA IND chan_nr=0x%02x link_id=0x%02x\n",
2982                 rllh->chan_nr, rllh->link_id);
2983
2984         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
2985         if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
2986                 DEBUGP(DRSL, "UNIT_DATA_IND without L3 INFO ?!?\n");
2987                 return -EIO;
2988         }
2989         msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
2990
2991         switch (rllh->chan_nr) {
2992         case RSL_CHAN_PCH_AGCH:
2993                 return gsm48_rr_rx_ccch(ms, msg);
2994         case RSL_CHAN_BCCH:
2995                 return gsm48_rr_rx_bcch(ms, msg);
2996         default:
2997                 LOGP(DRSL, LOGL_NOTICE, "RSL with chan_nr 0x%02x unknown.\n",
2998                         rllh->chan_nr);
2999                 return -EINVAL;
3000         }
3001 }
3002
3003 /*
3004  * state machines
3005  */
3006
3007 /* state trasitions for link layer messages (lower layer) */
3008 static struct dldatastate {
3009         uint32_t        states;
3010         int             type;
3011         const char      *type_name;
3012         int             (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3013 } dldatastatelist[] = {
3014         {SBIT(GSM48_RR_ST_IDLE) | SBIT(GSM48_RR_ST_CONN_PEND),
3015          RSL_MT_UNIT_DATA_IND, "UNIT_DATA_IND", gsm48_rr_unit_data_ind},
3016         {SBIT(GSM48_RR_ST_DEDICATED), /* 3.4.2 */
3017          RSL_MT_DATA_IND, "DATA_IND", gsm48_rr_data_ind},
3018         {SBIT(GSM48_RR_ST_IDLE) | SBIT(GSM48_RR_ST_CONN_PEND),
3019          RSL_MT_EST_CONF, "EST_CONF", gsm48_rr_estab_cnf},
3020 #if 0
3021         {SBIT(GSM48_RR_ST_DEDICATED),
3022          RSL_MT_EST_CONF, "EST_CONF", gsm48_rr_estab_cnf_dedicated},
3023         {SBIT(GSM_RRSTATE),
3024          RSL_MT_CONNECT_CNF, "CONNECT_CNF", gsm48_rr_connect_cnf},
3025         {SBIT(GSM_RRSTATE),
3026          RSL_MT_RELEASE_IND, "REL_IND", gsm48_rr_rel_ind},
3027 #endif
3028         {SBIT(GSM48_RR_ST_IDLE) | SBIT(GSM48_RR_ST_CONN_PEND),
3029          RSL_MT_REL_CONF, "REL_CONF", gsm48_rr_rel_cnf},
3030 #if 0
3031         {SBIT(GSM48_RR_ST_DEDICATED),
3032          RSL_MT_REL_CONF, "REL_CONF", gsm48_rr_rel_cnf_dedicated},
3033 #endif
3034         {SBIT(GSM48_RR_ST_CONN_PEND), /* 3.3.1.1.2 */
3035          RSL_MT_CHAN_CNF, "CHAN_CNF", gsm48_rr_rand_acc_cnf},
3036 #if 0
3037         {SBIT(GSM48_RR_ST_DEDICATED),
3038          RSL_MT_CHAN_CNF, "CHAN_CNF", gsm48_rr_rand_acc_cnf_dedicated},
3039         {SBIT(GSM_RRSTATE),
3040          RSL_MT_MDL_ERROR_IND, "MDL_ERROR_IND", gsm48_rr_mdl_error_ind},
3041 #endif
3042 };
3043
3044 #define DLDATASLLEN \
3045         (sizeof(dldatastatelist) / sizeof(struct dldatastate))
3046
3047 static int gsm48_rcv_rsl(struct osmocom_ms *ms, struct msgb *msg)
3048 {
3049         struct gsm48_rrlayer *rr = &ms->rrlayer;
3050         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
3051         int msg_type = rllh->c.msg_type;
3052         int i;
3053         int rc;
3054
3055         /* find function for current state and message */
3056         for (i = 0; i < DLDATASLLEN; i++)
3057                 if ((msg_type == dldatastatelist[i].type)
3058                  && ((1 << rr->state) & dldatastatelist[i].states))
3059                         break;
3060         if (i == DLDATASLLEN) {
3061                 LOGP(DRSL, LOGL_NOTICE, "RSLms message 0x%02x unhandled at "
3062                 "state %s.\n", msg_type, gsm48_rr_state_names[rr->state]);
3063                 msgb_free(msg);
3064                 return 0;
3065         }
3066         LOGP(DRSL, LOGL_INFO, "(ms %s) Received 'RSL_MT_%s' from RSL in state "
3067                 "%s\n", ms->name, dldatastatelist[i].type_name,
3068                 gsm48_rr_state_names[rr->state]);
3069
3070         rc = dldatastatelist[i].rout(ms, msg);
3071
3072         /* free msgb unless it is forwarded */
3073         if (dldatastatelist[i].rout != gsm48_rr_data_ind)
3074 #warning HACK!!!!!!
3075 return rc;
3076                 msgb_free(msg);
3077
3078         return rc;
3079 }
3080
3081 /* state trasitions for RR-SAP messages from up */
3082 static struct rrdownstate {
3083         uint32_t        states;
3084         int             type;
3085         int             (*rout) (struct osmocom_ms *ms, struct msgb *msg);
3086 } rrdownstatelist[] = {
3087         {SBIT(GSM48_RR_ST_IDLE), /* 3.3.1.1 */
3088          GSM48_RR_EST_REQ, gsm48_rr_est_req},
3089         {SBIT(GSM48_RR_ST_DEDICATED), /* 3.4.2 */
3090          GSM48_RR_DATA_REQ, gsm48_rr_data_req},
3091 #if 0
3092         {SBIT(GSM48_RR_ST_CONN_PEND) | SBIT(GSM48_RR_ST_DEDICATED),
3093          GSM48_RR_ABORT_REQ, gsm48_rr_abort_req},
3094         {SBIT(GSM48_RR_ST_DEDICATED),
3095          GSM48_RR_ACT_REQ, gsm48_rr_act_req},
3096 #endif
3097 };
3098
3099 #define RRDOWNSLLEN \
3100         (sizeof(rrdownstatelist) / sizeof(struct rrdownstate))
3101
3102 int gsm48_rr_downmsg(struct osmocom_ms *ms, struct msgb *msg)
3103 {
3104         struct gsm48_rrlayer *rr = &ms->rrlayer;
3105         struct gsm48_rr_hdr *rrh = (struct gsm48_rr_hdr *) msg->data;
3106         int msg_type = rrh->msg_type;
3107         int i;
3108         int rc;
3109
3110         LOGP(DRR, LOGL_INFO, "(ms %s) Message '%s' received in state %s\n",
3111                 ms->name, get_rr_name(msg_type),
3112                 gsm48_rr_state_names[rr->state]);
3113
3114         /* find function for current state and message */
3115         for (i = 0; i < RRDOWNSLLEN; i++)
3116                 if ((msg_type == rrdownstatelist[i].type)
3117                  && ((1 << rr->state) & rrdownstatelist[i].states))
3118                         break;
3119         if (i == RRDOWNSLLEN) {
3120                 LOGP(DRR, LOGL_NOTICE, "Message unhandled at this state.\n");
3121                 msgb_free(msg);
3122                 return 0;
3123         }
3124
3125         rc = rrdownstatelist[i].rout(ms, msg);
3126
3127         /* free msgb uless it is forwarded */
3128         if (rrdownstatelist[i].rout != gsm48_rr_data_req)
3129                 msgb_free(msg);
3130
3131         return rc;
3132 }
3133
3134 /*
3135  * init/exit
3136  */
3137
3138 int gsm48_rr_init(struct osmocom_ms *ms)
3139 {
3140         struct gsm48_rrlayer *rr = &ms->rrlayer;
3141
3142         memset(rr, 0, sizeof(*rr));
3143         rr->ms = ms;
3144
3145         LOGP(DRR, LOGL_INFO, "init Radio Ressource process\n");
3146
3147         INIT_LLIST_HEAD(&rr->rsl_upqueue);
3148         INIT_LLIST_HEAD(&rr->downqueue);
3149         /* downqueue is handled here, so don't add_work */
3150
3151         osmol2_register_handler(ms, &gsm48_rx_rsl);
3152
3153         return 0;
3154 }
3155
3156 int gsm48_rr_exit(struct osmocom_ms *ms)
3157 {
3158         struct gsm48_rrlayer *rr = &ms->rrlayer;
3159         struct msgb *msg;
3160
3161         LOGP(DRR, LOGL_INFO, "exit Radio Ressource process\n");
3162
3163         /* flush queues */
3164         while ((msg = msgb_dequeue(&rr->rsl_upqueue)))
3165                 msgb_free(msg);
3166         while ((msg = msgb_dequeue(&rr->downqueue)))
3167                 msgb_free(msg);
3168
3169         if (rr->rr_est_msg) {
3170                 msgb_free(rr->rr_est_msg);
3171                 rr->rr_est_msg = NULL;
3172         }
3173
3174         stop_rr_t3122(rr);
3175         stop_rr_t3126(rr);
3176
3177         return 0;
3178 }
3179
3180 #if 0
3181
3182 the process above is complete
3183 ------------------------------------------------------------------------------
3184 incomplete
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204 todo:
3205
3206 add support structure
3207 initialize support structure
3208
3209 queue messages (rslms_data_req) if channel changes
3210
3211 flush rach msg in all cases: during sending, after its done, and when aborted
3212 stop timers on abort
3213 debugging. (wenn dies todo erledigt ist, bitte in den anderen code moven)
3214 wird beim abbruch immer der gepufferte cm-service-request entfernt, auch beim verschicken?:
3215 measurement reports
3216 todo rr_sync_ind when receiving ciph, re ass, channel mode modify
3217
3218 todo change procedures, release procedure
3219
3220 during procedures, like "channel assignment" or "handover", rr requests must be queued
3221 they must be dequeued when complete
3222 they queue must be flushed when rr fails
3223
3224 #include <osmocore/protocol/gsm_04_08.h>
3225 #include <osmocore/msgb.h>
3226 #include <osmocore/utils.h>
3227 #include <osmocore/gsm48.h>
3228
3229 static int gsm48_rr_abort_req(struct osmocom_ms *ms, struct gsm48_rr *rrmsg)
3230 {
3231         struct gsm48_rrlayer *rr = ms->rrlayer;
3232         stop_rr_t3126(rr);
3233         if (rr->state == GSM48_RR_ST_DEDICATED) {
3234                 struct gsm_dl dlmsg;
3235
3236                 memset(&dlmsg, 0, sizeof(dlmsg));
3237                 return gsm48_send_rsl(ms, RSL_MT_REL_REQ, nmsg);
3238         }
3239         new_rr_state(rr, GSM48_RR_ST_IDLE);
3240 }
3241
3242 static int gsm48_rr_act_req(struct osmocom_ms *ms, struct gsm48_rr *rrmsg)
3243 {
3244 }
3245
3246
3247 }
3248
3249 /* memcopy of LV of given IE from tlv_parsed structure */
3250 static int tlv_copy(void *dest, int dest_len, struct tlv_parsed *tp, uint8_t ie)
3251 {
3252         uint8_t *lv = dest;
3253         uint8_t len;
3254
3255         if (dest_len < 1)
3256                 return -EINVAL;
3257         lv[0] = 0;
3258
3259         if (!TLVP_PRESENT(tp, ie))
3260                 return 0;
3261
3262         len = TLVP_LEN(tp, ie);
3263         if (len < 1)
3264                 return 0;
3265         if (len + 1 > dest_len)
3266                 return -ENOMEM;
3267
3268         memcpy(dest, TLVP_VAL(tp, ie) - 1, len + 1);
3269         return 0;
3270 }
3271
3272 static int gsm48_rr_rx_ass_cmd(struct osmocom_ms *ms, struct msgb *msg)
3273 {
3274         struct gsm48_rrlayer *rr = ms->rrlayer;
3275         struct gsm48_hdr *gh = msgb_l3(msg);
3276         struct gsm48_ass_cmd *ac = (struct gsm48_ass_cmd *)gh->data;
3277         int payload_len = msgb_l3len(msg) - sizeof(*gh) - sizeof(*ac);
3278         struct tlv_parsed tp;
3279         struct gsm48_rr_chan_desc cd;
3280         struct msgb *nmsg;
3281
3282         memset(&cd, 0, sizeof(cd));
3283
3284         if (payload_len < 0) {
3285                 LOGP(DRR, LOGL_NOTICE, "Short read of ASSIGNMENT COMMAND message.\n");
3286                 return gsm48_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
3287         }
3288         tlv_parse(&tp, &gsm48_rr_att_tlvdef, ac->data, payload_len, 0, 0);
3289
3290         /* channel description */
3291         memcpy(&cd.chan_desc, &ac->chan_desc, sizeof(chan_desc));
3292         /* power command */
3293         cd.power_command = ac->power_command;
3294         /* frequency list, after timer */
3295         tlv_copy(&cd.fl, sizeof(fl_after), &tp, GSM48_IE_FRQLIST_AFTER);
3296         /* cell channel description */
3297         tlv_copy(&cd.ccd, sizeof(ccd), &tp, GSM48_IE_CELL_CH_DESC);
3298         /* multislot allocation */
3299         tlv_copy(&cd.multia, sizeof(ma), &tp, GSM48_IE_MSLOT_DESC);
3300         /* channel mode */
3301         tlv_copy(&cd.chanmode, sizeof(chanmode), &tp, GSM48_IE_CHANMODE_1);
3302         /* mobile allocation, after time */
3303         tlv_copy(&cd.moba_after, sizeof(moba_after), &tp, GSM48_IE_MOB_AL_AFTER);
3304         /* starting time */
3305         tlv_copy(&cd.start, sizeof(start), &tp, GSM_IE_START_TIME);
3306         /* frequency list, before time */
3307         tlv_copy(&cd.fl_before, sizeof(fl_before), &tp, GSM48_IE_FRQLIST_BEFORE);
3308         /* channel description, before time */
3309         tlv_copy(&cd.chan_desc_before, sizeof(cd_before), &tp, GSM48_IE_CHDES_1_BEFORE);
3310         /* frequency channel sequence, before time */
3311         tlv_copy(&cd.fcs_before, sizeof(fcs_before), &tp, GSM48_IE_FRQSEQ_BEFORE);
3312         /* mobile allocation, before time */
3313         tlv_copy(&cd.moba_before, sizeof(moba_before), &tp, GSM48_IE_MOB_AL_BEFORE);
3314         /* cipher mode setting */
3315         if (TLVP_PRESENT(&tp, GSM48_IE_CIP_MODE_SET))
3316                 cd.cipher = *TLVP_VAL(&tp, GSM48_IE_CIP_MODE_SET);
3317         else
3318                 cd.cipher = 0;
3319
3320         if (no CA) {
3321                 LOGP(DRR, LOGL_INFO, "No current cell allocation available.\n");
3322                 return gsm48_rr_tx_rr_status(ms, GSM48_RR_CAUSE_NO_CELL_ALLOC_A);
3323         }
3324         
3325         if (not supported) {
3326                 LOGP(DRR, LOGL_INFO, "New channel is not supported.\n");
3327                 return gsm48_rr_tx_rr_status(ms, RR_CAUSE_CHAN_MODE_UNACCEPT);
3328         }
3329
3330         if (freq not supported) {
3331                 LOGP(DRR, LOGL_INFO, "New frequency is not supported.\n");
3332                 return gsm48_rr_tx_rr_status(ms, RR_CAUSE_FREQ_NOT_IMPLEMENTED);
3333         }
3334
3335         /* store current channel descriptions, to return in case of failure */
3336         memcpy(&rr->chan_last, &rr->chan_desc, sizeof(*cd));
3337         /* copy new description */
3338         memcpy(&rr->chan_desc, cd, sizeof(cd));
3339
3340         /* start suspension of current link */
3341         nmsg = gsm48_l3_msgb_alloc();
3342         if (!nmsg)
3343                 return -ENOMEM;
3344         gsm48_send_rsl(ms, RSL_MT_SUSP_REQ, msg);
3345
3346         /* change into special assignment suspension state */
3347         rr->assign_susp_state = 1;
3348         rr->resume_last_state = 0;
3349
3350         return 0;
3351 }
3352
3353 /* decode "BA Range" (10.5.2.1a) */
3354 static int gsm48_decode_ba_range(uint8_t *ba, uint8_t, ba_len, uint32_t *range,
3355         uint8_t *ranges, int max_ranges)
3356 {
3357         /* ba = pointer to IE without IE type and length octets
3358          * ba_len = number of octets
3359          * range = pointer to store decoded range
3360          * ranges = number of ranges decoded
3361          * max_ranges = maximum number of decoded ranges that can be stored
3362          */
3363         uint16_t lower, higher;
3364         int i, n, required_octets;
3365         
3366         /* find out how much ba ranges will be decoded */
3367         n = *ba++;
3368         ba_len --;
3369         required_octets = 5 * (n >> 1) + 3 * (n & 1);
3370         if (required_octets > n) {
3371                 *ranges = 0;
3372                 return -EINVAL;
3373         }
3374         if (max_ranges > n)
3375                 n = max_ranges;
3376
3377         /* decode ranges */
3378         for (i = 0; i < n; i++) {
3379                 if (!(i & 1)) {
3380                         /* decode even range number */
3381                         lower = *ba++ << 2;
3382                         lower |= (*ba >> 6);
3383                         higher = (*ba++ & 0x3f) << 4;
3384                         higher |= *ba >> 4;
3385                 } else {
3386                         lower = (*ba++ & 0x0f) << 6;
3387                         lower |= *ba >> 2;
3388                         higher = (*ba++ & 0x03) << 8;
3389                         higher |= *ba++;
3390                         /* decode odd range number */
3391                 }
3392                 *range++ = (higher << 16) | lower;
3393         }
3394         *ranges = n;
3395
3396         return 0;
3397 }
3398
3399
3400 /* decode "Cell Description" (10.5.2.2) */
3401 static int gsm48_decode_cell_desc(struct gsm48_cell_desc *cd, uint16_t *arfcn, uint8_t *ncc uint8_t *bcc)
3402 {
3403         *arfcn = (cd->bcch_hi << 8) + cd->bcch_lo;
3404         *ncc = cd->ncc;
3405         *bcc = cd->bcc;
3406 }
3407
3408 /* decode "Power Command" (10.5.2.28) and (10.5.2.28a) */
3409 static int gsm48_decode_power_cmd_acc(struct gsm48_power_cmd *pc, uint8_t *power_level uint8_t *atc)
3410 {
3411         *power_level = pc->power_level;
3412         if (atc) /* only in case of 10.5.2.28a */
3413                 *atc = pc->atc;
3414 }
3415
3416 /* decode "Synchronization Indication" (10.5.2.39) */
3417 static int gsm48_decode_power_cmd_acc(struct gsm48_rrlayer *rr, struct gsm48_rr_sync_ind *si)
3418 {
3419         rr->ho_sync_ind = si->si;
3420         rr->ho_rot = si->rot;
3421         rr->ho_nci = si->nci;
3422 }
3423
3424 /* receiving HANDOVER COMMAND message (9.1.15) */
3425 static int gsm48_rr_rx_hando_cmd(struct osmocom_ms *ms, struct msgb *msg)
3426 {
3427         struct gsm48_rrlayer *rr = ms->rrlayer;
3428         struct gsm48_hdr *gh = msgb_l3(msg);
3429         struct gsm48_ho_cmd *ho = (struct gsm48_ho_cmd *)gh->data;
3430         int payload_len = msgb_l3len(msg) - sizeof(*gh) - wirklich sizeof(*ho);
3431         struct tlv_parsed tp;
3432         struct gsm48_rr_chan_desc cd;
3433         struct msgb *nmsg;
3434
3435         memset(&cd, 0, sizeof(cd));
3436
3437         if (payload_len < 0) {
3438                 LOGP(DRR, LOGL_NOTICE, "Short read of HANDOVER COMMAND message.\n");
3439                 return gsm48_rr_tx_rr_status(ms, GSM48_RR_CAUSE_PROT_ERROR_UNSPC);
3440         }
3441         tlv_parse(&tp, &gsm48_rr_att_tlvdef, ho->data, payload_len, 0, 0);
3442
3443         /* decode Cell Description */
3444         gsm_decode_cell_desc(&ho->cell_desc, &cd.bcch_arfcn, &cd.ncc, &cd.bcc);
3445         /* Channel Description */
3446         memcpy(&rr->chan_desc.chan_desc, ho->chan_desc, 3);
3447         /* Handover Reference */
3448         rr->hando_ref = ho->ho_ref;
3449         /* Power Command and access type */
3450         gsm_decode_power_cmd_acc((struct gsm48_power_cmd *)&ho->power_command,
3451                 &cd.power_level, cd.atc);
3452         /* Synchronization Indication */
3453         if (TLVP_PRESENT(&tp, GSM48_IE_SYNC_IND))
3454                 gsm48_decode_sync_ind(rr,
3455                         TLVP_VAL(&tp, GSM48_IE_MOBILE_ALLOC)-1, &cd);
3456         /* Frequency Sort List */
3457         if (TLVP_PRESENT(&tp, GSM48_IE_FREQ_SHORT_LIST))
3458                 gsm48_decode_freq_list(&ms->support, s->freq,
3459                         TLVP_VAL(&tp, GSM48_IE_MOBILE_ALLOC),
3460                         *(TLVP_VAL(&tp, GSM48_IE_MOBILE_ALLOC)-1),
3461                                 0xce, FREQ_TYPE_SERV);
3462
3463
3464 today: more IE parsing
3465
3466         /* store current channel descriptions, to return in case of failure */
3467         memcpy(&rr->chan_last, &rr->chan_desc, sizeof(*cd));
3468         /* copy new description */
3469         memcpy(&rr->chan_desc, cd, sizeof(cd));
3470
3471         /* start suspension of current link */
3472         nmsg = gsm48_l3_msgb_alloc();
3473         if (!nmsg)
3474                 return -ENOMEM;
3475         gsm48_send_rsl(ms, RSL_MT_SUSP_REQ, msg);
3476
3477         /* change into special handover suspension state */
3478         rr->hando_susp_state = 1;
3479         rr->resume_last_state = 0;
3480
3481         return 0;
3482 }
3483
3484 static int gsm48_rr_estab_cnf_dedicated(struct osmocom_ms *ms, struct msgb *msg)
3485 {
3486         if (rr->hando_susp_state || rr->assign_susp_state) {
3487                 if (rr->resume_last_state) {
3488                         rr->resume_last_state = 0;
3489                         gsm48_rr_tx_ass_cpl(ms, GSM48_RR_CAUSE_NORMAL);
3490                 } else {
3491                         gsm48_rr_tx_ass_fail(ms, RR_CAUSE_PROTO_ERR_UNSPEC);
3492                 }
3493                 /* transmit queued frames during ho / ass transition */
3494                 gsm48_rr_dequeue_down(ms);
3495         }
3496
3497         return 0;
3498 }
3499
3500 static int gsm48_rr_connect_cnf(struct osmocom_ms *ms, struct msgbl *msg)
3501 {
3502 }
3503
3504 static int gsm48_rr_rel_ind(struct osmocom_ms *ms, struct msgb *msg)
3505 {
3506 }
3507
3508 static int gsm48_rr_rel_cnf_dedicated(struct osmocom_ms *ms, struct msgb *msg)
3509 {
3510         struct gsm48_rrlayer *rr = ms->rrlayer;
3511         struct msgb *nmsg;
3512
3513         if (rr->hando_susp_state || rr->assign_susp_state) {
3514                 struct msgb *msg;
3515
3516                 /* change radio to new channel */
3517                 rr->arfcn = rr->chan_desc.chan_desc.chan_nr;
3518                 tx_ph_dm_est_req(ms, arfcn, rr->arfcn);
3519
3520                 nmsg = gsm48_l3_msgb_alloc();
3521                 if (!nmsg)
3522                         return -ENOMEM;
3523                 /* send DL-ESTABLISH REQUEST */
3524                 gsm48_send_rsl(ms, RSL_MT_EST_REQ, nmsg);
3525
3526         }
3527         if (rr->hando_susp_state) {
3528                 gsm48_rr_tx_hando_access(ms);
3529                 rr->hando_acc_left = 3;
3530         }
3531         return 0;
3532 }
3533
3534 static int gsm48_rr_mdl_error_ind(struct osmocom_ms *ms, struct msgb *msg)
3535 {
3536         struct gsm48_rrlayer *rr = ms->rrlayer;
3537         struct msgb *nmsg;
3538         struct gsm_rr_hdr *nrrh;
3539
3540         if (rr->hando_susp_state || rr->assign_susp_state) {
3541                 if (!rr->resume_last_state) {
3542                         rr->resume_last_state = 1;
3543
3544                         /* get old channel description */
3545                         memcpy(&rr->chan_desc, &rr->chan_last, sizeof(*cd));
3546
3547                         /* change radio to old channel */
3548                         rr->arfcn = rr->chan_desc.chan_desc.chan_nr;
3549                         tx_ph_dm_est_req(ms, arfcn, rr->arfcn);
3550
3551                         /* re-establish old link */
3552                         nmsg = gsm48_l3_msgb_alloc();
3553                         if (!nmsg)
3554                                 return -ENOMEM;
3555                         return gsm48_send_rsl(ms, RSL_MT_EST_REQ, nmsg);
3556                 }
3557                 rr->resume_last_state = 0;
3558         }
3559
3560         /* deactivate channel */
3561         tx_ph_dm_rel_req(ms, arfcn, rr->chan_desc.chan_desc.chan_nr);
3562
3563         /* send abort ind to upper layer */
3564         nmsg = gsm48_mm_msgb_alloc();
3565
3566         if (!msg)
3567                 return -ENOMEM;
3568         nrrh = (struct gsm_mm_hdr *)nmsg->data;
3569         nrrh->msg_type = RR_ABORT_IND;
3570         nrrh->cause = GSM_MM_CAUSE_LINK_FAILURE;
3571         return gsm48_rr_upmsg(ms, msg);
3572 }
3573
3574 static void timeout_rr_t3124(void *arg)
3575 {
3576         struct gsm48_rrlayer *rr = arg;
3577         struct msgb *nmsg;
3578
3579         /* stop sending more access bursts when timer expired */
3580         hando_acc_left = 0;
3581
3582         /* get old channel description */
3583         memcpy(&rr->chan_desc, &rr->chan_last, sizeof(*cd));
3584
3585         /* change radio to old channel */
3586         rr->arfcn = rr->chan_desc.chan_desc.chan_nr;
3587         tx_ph_dm_est_req(ms, arfcn, rr->arfcn);
3588
3589         /* re-establish old link */
3590         nmsg = gsm48_l3_msgb_alloc();
3591         if (!nmsg)
3592                 return -ENOMEM;
3593         return gsm48_send_rsl(ms, RSL_MT_EST_REQ, nmsg);
3594
3595         todo
3596 }
3597
3598 /* send HANDOVER ACCESS burst (9.1.14) */
3599 static int gsm48_rr_tx_hando_access(struct osmocom_ms *ms)
3600 {
3601         nmsg = msgb_alloc_headroom(20, 16, "HAND_ACCESS");
3602         if (!nmsg)
3603                 return -ENOMEM;
3604         *msgb_put(nmsg, 1) = rr->hando_ref;
3605         todo burst
3606         return gsm48_send_rsl(ms, RSL_MT_RAND_ACC_REQ, nmsg);
3607 }
3608
3609 /* send next channel request in dedicated state */
3610 static int gsm48_rr_rand_acc_cnf_dedicated(struct osmocom_ms *ms, struct msgb *msg)
3611 {
3612         struct gsm48_rrlayer *rr = &ms->rrlayer;
3613         struct msgb *nmsg;
3614         int s;
3615
3616         if (!rr->hando_susp_state) {
3617                 LOGP(DRR, LOGL_NOTICE, "Random acces confirm, but not in handover state.\n");
3618                 return 0;
3619         }
3620
3621         /* send up to four handover access bursts */
3622         if (rr->hando_acc_left) {
3623                 rr->hando_acc_left--;
3624                 gsm48_rr_tx_hando_access(ms);
3625                 return;
3626         }
3627
3628         /* start timer for sending next HANDOVER ACCESS bursts afterwards */
3629         if (!bsc_timer_pending(&rr->t3124)) {
3630                 if (allocated channel is SDCCH)
3631                         start_rr_t3124(rr, GSM_T3124_675);
3632                 else
3633                         start_rr_t3124(rr, GSM_T3124_320);
3634         if (!rr->n_chan_req) {
3635                 start_rr_t3126(rr, 5, 0); /* TODO improve! */
3636                 return 0;
3637         }
3638         rr->n_chan_req--;
3639
3640         /* wait for PHYSICAL INFORMATION message or T3124 timeout */
3641         return 0;
3642
3643 }
3644
3645 #endif
3646
3647