Inter-Layer intergration work
[osmocom-bb.git] / src / host / layer2 / src / lapdm.c
1 /* GSM LAPDm (TS 04.06) implementation */
2
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  *
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include <osmocore/timer.h>
28 #include <osmocore/msgb.h>
29 #include <osmocore/tlv.h>
30 #include <osmocore/utils.h>
31 #include <osmocore/rsl.h>
32 #include <osmocore/protocol/gsm_04_08.h>
33 #include <osmocore/protocol/gsm_08_58.h>
34
35 #include <osmocom/debug.h>
36 #include <osmocom/osmocom_data.h>
37 #include <osmocom/osmocom_layer2.h>
38 #include <osmocom/lapdm.h>
39
40 #include <l1a_l23_interface.h>
41
42 /* TS 04.06 Figure 4 / Section 3.2 */
43 #define LAPDm_LPD_NORMAL  0
44 #define LAPDm_LPD_SMSCB   1
45 #define LAPDm_SAPI_NORMAL 0
46 #define LAPDm_SAPI_SMS    3
47 #define LAPDm_ADDR(lpd, sapi, cr) (((lpd & 0x3) << 5) | ((sapi & 0x7) << 2) | ((cr & 0x1) << 1) | 0x1)
48
49 #define LAPDm_ADDR_SAPI(addr) ((addr >> 2) & 0x7)
50
51 /* TS 04.06 Table 3 / Section 3.4.3 */
52 #define LAPDm_CTRL_I(nr, ns, p) (((nr & 0x7) << 5) | ((p & 0x1) << 4) | ((ns & 0x7) << 1))
53 #define LAPDm_CTRL_S(nr, s, p)  (((nr & 0x7) << 5) | ((p & 0x1) << 4) | ((s & 0x3) << 2) | 0x1)
54 #define LAPDm_CTRL_U(u, p)      (((u & 0x1c) << 5) | ((p & 0x1) << 4) | ((u & 0x3) << 2) | 0x3)
55
56 #define LAPDm_CTRL_is_I(ctrl)   ((ctrl & 0x1) == 0)
57 #define LAPDm_CTRL_is_S(ctrl)   ((ctrl & 0x3) == 1)
58 #define LAPDm_CTRL_is_U(ctrl)   ((ctrl & 0x3) == 3)
59
60 #define LAPDm_CTRL_U_BITS(ctrl) (((ctrl & 0xC) >> 2) | (ctrl & 0xE) >> 3)
61 #define LAPDm_CTRL_PF_BIT(ctrl) ((ctrl >> 4) & 0x1)
62
63 #define LAPDm_CTRL_S_BITS(ctrl) ((ctrl & 0xC) >> 2)
64
65 #define LAPDm_CTRL_I_Ns(ctrl)   ((ctrl & 0xE) >> 1)
66 #define LAPDm_CTRL_I_Nr(ctrl)   ((ctrl & 0xE0) >> 5)
67
68 /* TS 04.06 Table 4 / Section 3.8.1 */
69 #define LAPDm_U_SABM    0x7
70 #define LAPDm_U_DM      0x3
71 #define LAPDm_U_UI      0x0
72 #define LAPDm_U_DISC    0x8
73 #define LAPDm_U_UA      0xC
74
75 #define LAPDm_S_RR      0x0
76 #define LAPDm_S_RNR     0x1
77 #define LAPDm_S_REJ     0x2
78
79 #define LAPDm_LEN(len)  ((len << 2) | 0x1)
80
81 /* TS 04.06 Section 5.8.3 */
82 #define N201_AB_SACCH           18
83 #define N201_AB_SDCCH           20
84 #define N201_AB_FACCH           20
85 #define N201_Bbis               23
86 #define N201_Bter_SACCH         21
87 #define N201_Bter_SDCCH         23
88 #define N201_Bter_FACCH         23
89 #define N201_B4                 19
90
91 /* 5.8.2.1 N200 during establish and release */
92 #define N200_EST_REL            5
93 /* 5.8.2.1 N200 during timer recovery state */
94 #define N200_TR_SACCH           5
95 #define N200_TR_SDCCH           23
96 #define N200_TR_FACCH_FR        34
97 #define N200_TR_EFACCH_FR       48
98 #define N200_TR_FACCH_HR        29
99 /* FIXME: this depends on chan type */
100 #define N200    N200_TR_SACCH
101
102 #define CR_MS2BS_CMD    0
103 #define CR_MS2BS_RESP   1
104 #define CR_BS2MS_CMD    1
105 #define CR_BS2MS_RESP   0
106
107 /* Set T200 to 1 Second (OpenBTS uses 900ms) */
108 #define T200    1, 0
109
110 enum lapdm_format {
111         LAPDm_FMT_A,
112         LAPDm_FMT_B,
113         LAPDm_FMT_Bbis,
114         LAPDm_FMT_Bter,
115         LAPDm_FMT_B4,
116 };
117
118 struct lapdm_msg_ctx {
119         struct lapdm_datalink *dl;
120         enum lapdm_format lapdm_fmt;
121         uint8_t chan_nr;
122         uint8_t link_id;
123         uint8_t addr;
124         uint8_t ctrl;
125 };
126
127 static void lapdm_t200_cb(void *data);
128
129 /* UTILITY FUNCTIONS */
130
131 static inline uint8_t inc_mod8(uint8_t x)
132 {
133         return (x + 1) % 8;
134 }
135
136 static void lapdm_dl_init(struct lapdm_datalink *dl,
137                           struct lapdm_entity *entity)
138 {
139         memset(dl, 0, sizeof(*dl));
140         dl->t200.data = dl;
141         dl->t200.cb = &lapdm_t200_cb;
142         dl->entity = entity;
143 }
144
145 void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms)
146 {
147         unsigned int i;
148
149         for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
150                 lapdm_dl_init(&le->datalink[i], le);
151
152         le->ms = ms;
153 }
154
155 static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
156 {
157         switch (sapi) {
158         case LAPDm_SAPI_NORMAL:
159                 return &le->datalink[0];
160         case LAPDm_SAPI_SMS:
161                 return &le->datalink[1];
162         default:
163                 return NULL;
164         }
165 }
166
167 /* remove the L2 header from a MSGB */
168 static inline unsigned char *msgb_pull_l2h(struct msgb *msg)
169 {
170         unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h);
171         msg->l2h = NULL;
172         return ret;
173 }
174
175 /* Take a Bbis format message from L1 and create RSLms UNIT DATA IND */
176 static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
177                              struct msgb *msg)
178 {
179         uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
180         struct abis_rsl_rll_hdr *rh;
181
182         /* construct a RSLms RLL message (DATA INDICATION, UNIT DATA
183          * INDICATION) and send it off via RSLms */
184
185         /* Push the L3 IE tag and lengh */
186         msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
187
188         /* Then push the RSL header */
189         rh = (struct abis_rsl_rll_hdr *) msgb_push(msg, sizeof(*rh));
190         rsl_init_rll_hdr(rh, msg_type);
191         rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
192         rh->chan_nr = mctx->chan_nr;
193         rh->link_id = mctx->link_id;
194
195         /* set the l2 header pointer */
196         msg->l2h = (uint8_t *)rh;
197
198         /* send off the RSLms message to L3 */
199         return rslms_sendmsg(msg, mctx->dl->entity->ms);
200 }
201
202 static int send_rslms_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
203 {
204         struct abis_rsl_rll_hdr *rh;
205         struct msgb *msg = msgb_alloc(sizeof(*rh), "rslms_rll_simple");
206
207         /* put the RSL header */
208         rh = (struct abis_rsl_rll_hdr *) msgb_put(msg, sizeof(*rh));
209         rsl_init_rll_hdr(rh, msg_type);
210         rh->c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
211         rh->chan_nr = mctx->chan_nr;
212         rh->link_id = mctx->link_id;
213
214         /* set the l2 header pointer */
215         msg->l2h = (uint8_t *)rh;
216
217         /* send off the RSLms message to L3 */
218         return rslms_sendmsg(msg, mctx->dl->entity->ms);
219 }
220
221 static int check_length_ind(uint8_t length_ind)
222 {
223         if (!(length_ind & 0x01)) {
224                 /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION
225                  * primitive with cause "frame not implemented" is sent to the
226                  * mobile management entity. */
227                 printf("we don't support multi-octet length\n");
228                 return -EINVAL;
229         }
230         if (length_ind & 0x02) {
231                 printf("we don't support LAPDm fragmentation yet\n");
232                 return -EINVAL;
233         }
234         return 0;
235 }
236
237 /* Timer callback on T200 expiry */
238 static void lapdm_t200_cb(void *data)
239 {
240         struct lapdm_datalink *dl = data;
241
242         switch (dl->state) {
243         case LAPDm_STATE_SABM_SENT:
244                 /* 5.4.1.3 */
245                 if (dl->retrans_ctr >= N200_EST_REL + 1) {
246                         /* FIXME: send RELEASE INDICATION to L3 */
247                         dl->retrans_ctr = 0;
248                         dl->state = LAPDm_STATE_IDLE;
249                 }
250                 /* FIXME: retransmit SABM command */
251
252                 /* increment re-transmission counter */
253                 dl->retrans_ctr++;
254                 /* restart T200 (PH-READY-TO-SEND) */
255                 bsc_schedule_timer(&dl->t200, T200);
256                 break;
257         case LAPDm_STATE_MF_EST:
258                 /* 5.5.7 */
259                 dl->retrans_ctr = 0;
260                 dl->state = LAPDm_STATE_TIMER_RECOV;
261         case LAPDm_STATE_TIMER_RECOV:
262                 dl->retrans_ctr++;
263                 if (dl->retrans_ctr < N200) {
264                         /* FIXME: retransmit I frame (V_s-1) with P=1 */
265                         /* FIXME: send appropriate supervision frame with P=1 */
266                         /* restart T200 (PH-READY-TO-SEND) */
267                         bsc_schedule_timer(&dl->t200, T200);
268                 } else {
269                         /* FIXME: send ERROR INDICATION to L3 */
270                 }
271                 break;
272         default:
273                 printf("T200 expired in dl->state %u\n", dl->state);
274         }
275 }
276
277 static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
278 {
279         uint8_t sapi = mctx->link_id & 7;
280         struct msgb *msg = msgb_alloc(24, "LAPDm RR");
281         uint8_t *data = msgb_put(msg, 3);
282
283         data[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
284         data[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit);
285         data[2] = LAPDm_LEN(0);
286
287         return tx_ph_data_req(mctx->dl->entity->ms, msg, mctx->chan_nr, mctx->link_id);
288 }
289
290 /* L1 -> L2 */
291
292 /* Receive a LAPDm S (Unnumbered) message from L1 */
293 static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx)
294 {
295         struct lapdm_datalink *dl = mctx->dl;
296         uint8_t length;
297         int rc;
298
299         switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) {
300         case LAPDm_U_SABM:
301                 printf("SABM ");
302                 /* Must be Format B */
303                 rc = check_length_ind(msg->l2h[2]);
304                 if (rc < 0)
305                         return rc;
306                 length = msg->l2h[2] >> 2;
307                 /* FIXME: G.4.5 check */
308                 if (dl->state == LAPDm_STATE_MF_EST) {
309                         if (length == 0) {
310                                 /* FIXME: re-establishment procedure 5.6 */
311                         } else {
312                                 /* FIXME: check for contention resoultion */
313                                 printf("SABM command, multiple frame established state\n");
314                                 return 0;
315                         }
316                 }
317                 if (length == 0) {
318                         /* 5.4.1.2 Normal establishment procedures */
319                         rc = send_rslms_rll_simple(RSL_MT_EST_IND, mctx);
320                 } else {
321                         /* 5.4.1.4 Contention resolution establishment */
322                         msg->l3h = msg->l2h + 3;
323                         msgb_pull_l2h(msg);
324                         rc = send_rslms_rll_l3(RSL_MT_EST_IND, mctx, msg);
325                 }
326                 if (rc == 0)
327                         dl->state = LAPDm_STATE_SABM_SENT;
328                 break;
329         case LAPDm_U_DM:
330                 printf("DM ");
331                 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
332                         /* 5.4.1.2 DM responses with the F bit set to "0" shall be ignored. */
333                         return 0;
334                 }
335                 switch (dl->state) {
336                 case LAPDm_STATE_IDLE:
337                         /* 5.4.5 all other frame types shall be discarded */
338                         printf("state=IDLE (discarding) ");
339                         return 0;
340                 case LAPDm_STATE_MF_EST:
341                         if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1)
342                                 printf("unsolicited DM resposne ");
343                         else
344                                 printf("unsolicited DM resposne, multiple frame established state ");
345                         return 0;
346                 case LAPDm_STATE_TIMER_RECOV:
347                         /* DM is normal in case PF = 1 */
348                         if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) {
349                                 printf("unsolicited DM resposne, multiple frame established state ");
350                                 return 0;
351                         }
352                         break;
353                 }
354                 /* reset T200 */
355                 bsc_del_timer(&dl->t200);
356                 rc = send_rslms_rll_simple(RSL_MT_REL_IND, mctx);
357                 break;
358         case LAPDm_U_UI:
359                 printf("UI ");
360                 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
361                         length = N201_B4;
362                         msg->l3h = msg->l2h + 2;
363                 } else {
364                         rc = check_length_ind(msg->l2h[2]);
365                         if (rc < 0)
366                                 return rc;
367                         length = msg->l2h[2] >> 2;
368                         msg->l3h = msg->l2h + 3;
369                 }
370                 /* do some length checks */
371                 if (length == 0) {
372                         /* 5.3.3 UI frames received with the length indicator set to "0" shall be ignored */
373                         printf("length=0 (discarding) ");
374                         return 0;
375                 }
376                 /* FIXME: G.4.5 check */
377                 switch (LAPDm_ADDR_SAPI(mctx->ctrl)) {
378                 case LAPDm_SAPI_NORMAL:
379                 case LAPDm_SAPI_SMS:
380                         break;
381                 default:
382                         /* 5.3.3 UI frames with invalid SAPI values shall be discarded */
383                         printf("sapi=%u (discarding) ", LAPDm_ADDR_SAPI(mctx->ctrl));
384                         return 0;
385                 }
386                 msgb_pull_l2h(msg);
387                 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, mctx, msg);
388                 break;
389         case LAPDm_U_DISC:
390                 printf("DISC ");
391                 length = msg->l2h[2] >> 2;
392                 if (length > 0 || msg->l2h[2] & 0x02) {
393                         /* G.4.4 If a DISC or DM frame is received with L>0 or
394                          * with the M bit set to "1", an MDL-ERROR-INDICATION
395                          * primitive with cause "U frame with incorrect
396                          * parameters" is sent to the mobile management entity. */
397                         printf("U frame iwth incorrect parameters ");
398                         return -EIO;
399                 }
400                 switch (dl->state) {
401                 case LAPDm_STATE_IDLE:
402                         /* FIXME: send DM with F=P */
403                         break;
404                 default:
405                         /* FIXME */
406                         break;
407                 }
408                 break;
409         case LAPDm_U_UA:
410                 printf("UA ");
411                 /* FIXME: G.4.5 check */
412                 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
413                         /* 5.4.1.2 A UA response with the F bit set to "0" shall be ignored. */
414                         printf("F=0 (discarding) ");
415                         return 0;
416                 }
417                 switch (dl->state) {
418                 case LAPDm_STATE_SABM_SENT:
419                         break;
420                 case LAPDm_STATE_IDLE:
421                         /* 5.4.5 all other frame types shall be discarded */
422                 default:
423                         printf("unsolicited UA response! (discarding) ");
424                         return 0;
425                 }
426                 /* reset Timer T200 */
427                 bsc_del_timer(&dl->t200);
428                 /* set Vs, Vr and Va to 0 */
429                 dl->V_send = dl->V_recv = dl->V_ack = 0;
430                 /* enter multiple-frame-established state */
431                 dl->state = LAPDm_STATE_MF_EST;
432                 /* send notification to L3 */
433                 rc = send_rslms_rll_simple(RSL_MT_EST_CONF, mctx);
434                 break;
435         }
436         return rc;
437 }
438
439 /* Receive a LAPDm S (Supervisory) message from L1 */
440 static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
441 {
442         struct lapdm_datalink *dl = mctx->dl;
443         uint8_t length;
444
445         length = msg->l2h[2] >> 2;
446         if (length > 0 || msg->l2h[2] & 0x02) {
447                 /* G.4.3 If a supervisory frame is received with L>0 or
448                  * with the M bit set to "1", an MDL-ERROR-INDICATION
449                  * primitive with cause "S frame with incorrect
450                  * parameters" is sent to the mobile management entity. */
451                 return -EIO;
452         }
453         switch (dl->state) {
454         case LAPDm_STATE_IDLE:
455                 /* FIXME: if P=1, respond DM with F=1 (5.2.2) */
456                 /* 5.4.5 all other frame types shall be discarded */
457                 break;
458         }
459         switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) {
460         case LAPDm_S_RR:
461                 /* FIXME */
462                 break;
463         case LAPDm_S_RNR:
464                 /* FIXME */
465                 break;
466         case LAPDm_S_REJ:
467                 /* FIXME */
468                 break;
469         }
470         return 0;
471 }
472
473 /* Receive a LAPDm I (Information) message from L1 */
474 static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx)
475 {
476         struct lapdm_datalink *dl = mctx->dl;
477         uint8_t nr = LAPDm_CTRL_I_Nr(mctx->ctrl);
478         uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl);
479         uint8_t length;
480         int rc;
481
482         length = msg->l2h[2] >> 2;
483         /* FIXME: check for length > N201 */
484         if (length == 0) {
485                 /* G.4.2 If the length indicator of an I frame is set
486                  * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
487                  * primitive with cause "I frame with incorrect length"
488                  * is sent to the mobile management entity. */
489                 return -EIO;
490         }
491         /* FIXME: G.4.2 If the numerical value of L is L<N201 and the M
492          * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
493          * cause "I frame with incorrect use of M bit" is sent to the
494          * mobile management entity. */
495         switch (dl->state) {
496         case LAPDm_STATE_IDLE:
497                 /* FIXME: if P=1, respond DM with F=1 (5.2.2) */
498                 /* 5.4.5 all other frame types shall be discarded */
499                 break;
500         }
501
502         /* processing of Nr, Ns and P fields */
503         if (ns == dl->V_recv) {
504                 /* FIXME: check for M bit! */
505                 dl->V_recv = inc_mod8(dl->V_recv);
506
507                 /* send a DATA INDICATION to L3 */
508                 msg->l3h = msg->l2h + 2;
509                 msgb_pull_l2h(msg);
510                 rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg);
511         } else {
512                 printf("N(s) sequence error: Ns=%u, V_recv=%u ", ns, dl->V_recv);
513                 /* FIXME: 5.7.1: N(s) sequence error */
514                 /* discard data */
515                 return -EIO;
516         }
517
518         /* Check for P bit */
519         if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
520                 /* 5.5.2.1 */
521                 /* FIXME: check ifwe are in own receiver busy */
522                 /* FIXME: Send RR with F=1 */
523                 rc = lapdm_send_rr(mctx, 1);
524         } else {
525                 /* 5.5.2.2 */
526                 /* FIXME: check ifwe are in own receiver busy */
527                 //if (we_have_I_frame_pending) {
528                 if (0) {
529                         /* FIXME: send that I frame with Nr=Vr */
530                 } else {
531                         /* Send RR with F=0 */
532                         rc = lapdm_send_rr(mctx, 0);
533                 }
534         }
535
536         if (dl->state != LAPDm_STATE_TIMER_RECOV) {
537                 /* When not in the timer recovery condition, the data
538                  * link layer entity shall reset the timer T200 on
539                  * receipt of a valid I frame with N(R) higher than V(A) */
540                 if (nr > dl->V_ack) {
541                         /* FIXME: 5.5.3.1 Note 1 + 2 */
542                         bsc_del_timer(&dl->t200);
543                         /* FIXME: if there are outstanding I frames
544                          * still unacknowledged, the data link layer
545                          * entity shall set timer T200 */
546                 }
547
548                 /* FIXME: 5.7.4: N(R) sequence error */
549                 /* N(R) is called valid, if and only if (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8. */
550         }
551
552         /* V(A) shall be set to the value of N(R) */
553         dl->V_ack = LAPDm_CTRL_I_Nr(mctx->ctrl);
554
555         return rc;
556 }
557
558 /* Receive a LAPDm message from L1 */
559 static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx)
560 {
561         int rc;
562
563         if (LAPDm_CTRL_is_U(mctx->ctrl))
564                 rc = lapdm_rx_u(msg, mctx);
565         else if (LAPDm_CTRL_is_S(mctx->ctrl))
566                 rc = lapdm_rx_s(msg, mctx);
567         else if (LAPDm_CTRL_is_I(mctx->ctrl))
568                 rc = lapdm_rx_i(msg, mctx);
569         else {
570                 printf("unknown LAPDm format ");
571                 rc = -EINVAL;
572         }
573         return rc;
574 }
575
576 /* input into layer2 (from layer 1) */
577 int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_dl *l1i)
578 {
579         uint8_t cbits = l1i->chan_nr >> 3;
580         uint8_t sapi = l1i->link_id & 7;
581         struct lapdm_msg_ctx mctx;
582         int rc;
583
584         printf("l2_ph_data_ind() ");
585         /* when we reach here, we have a msgb with l2h pointing to the raw
586          * 23byte mac block. The l1h has already been purged. */
587
588         mctx.dl = datalink_for_sapi(le, sapi);
589         mctx.chan_nr = l1i->chan_nr;
590         mctx.link_id = l1i->link_id;
591         mctx.addr = mctx.ctrl = 0;
592
593         /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
594         if (cbits == 0x10 || cbits == 0x12) {
595                 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
596                 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
597                 printf("fmt=Bbis ");
598         } else {
599                 if (mctx.link_id & 0x40) {
600                         /* It was received from network on SACCH, thus
601                          * lapdm_fmt must be B4 */
602                         mctx.lapdm_fmt = LAPDm_FMT_B4;
603                         printf("fmt=B4 ");
604                         /* SACCH frames have a two-byte L1 header that OsmocomBB L1 doesn't
605                          * strip */
606                         msg->l2h += 2;
607                 } else {
608                         mctx.lapdm_fmt = LAPDm_FMT_B;
609                         printf("fmt=B ");
610                 }
611         }
612
613         switch (mctx.lapdm_fmt) {
614         case LAPDm_FMT_A:
615         case LAPDm_FMT_B:
616         case LAPDm_FMT_B4:
617                 mctx.addr = msg->l2h[0];
618                 if (!(mctx.addr & 0x01)) {
619                         printf("we don't support multibyte addresses (discarding)\n");
620                         return -EINVAL;
621                 }
622                 mctx.ctrl = msg->l2h[1];
623                 /* obtain SAPI from address field */
624                 mctx.link_id |= LAPDm_ADDR_SAPI(mctx.addr);
625                 rc = lapdm_ph_data_ind(msg, &mctx);
626                 break;
627         case LAPDm_FMT_Bter:
628                 /* FIXME */
629                 break;
630         case LAPDm_FMT_Bbis:
631                 /* directly pass up to layer3 */
632                 printf("UI ");
633                 msg->l3h = msg->l2h;
634                 msgb_pull_l2h(msg);
635                 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
636                 break;
637         }
638         printf("\n");
639
640         return rc;
641 }
642
643 /* L3 -> L2 / RSLMS -> LAPDm */
644
645 /* L3 requests establishment of data link */
646 static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
647 {
648         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
649         uint8_t chan_nr = rllh->chan_nr;
650         uint8_t link_id = rllh->link_id;
651         uint8_t sapi = rllh->link_id & 7;
652         struct tlv_parsed tv;
653         uint8_t len;
654         uint8_t *lapdh;
655
656         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
657         if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
658                 /* contention resolution establishment procedure */
659                 if (dl->state != LAPDm_STATE_IDLE) {
660                         /* 5.4.1.4: The data link layer shall, however, ignore any such
661                          * service request if it is not in the idle state when the
662                          * request is received. */
663                         msgb_free(msg);
664                         return 0;
665                 }
666                 if (sapi != 0) {
667                         /* According to clause 6, the contention resolution
668                          * procedure is only permitted with SAPI value 0 */
669                         msgb_free(msg);
670                         return -EINVAL;
671                 }
672                 /* transmit a SABM command with the P bit set to "1". The SABM
673                  * command shall contain the layer 3 message unit */
674                 len = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO));
675
676                 /* FIXME: store information field in dl entity */
677         } else {
678                 /* normal establishment procedure */
679                 len = LAPDm_LEN(0);
680         }
681
682         /* Remove RLL header from msgb */
683         msgb_pull_l2h(msg);
684
685         /* Push LAPDm header on msgb */
686         lapdh = msgb_push(msg, 3);
687         lapdh[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
688         lapdh[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
689         lapdh[2] = len;
690
691         /* Tramsmit and start T200 */
692         bsc_schedule_timer(&dl->t200, T200);
693         return tx_ph_data_req(dl->entity->ms, msg, chan_nr, link_id);
694 }
695
696 /* L3 requests transfer of unnumbered information */
697 static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
698 {
699         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
700         uint8_t chan_nr = rllh->chan_nr;
701         uint8_t link_id = rllh->link_id;
702         uint8_t sapi = link_id & 7;
703         struct tlv_parsed tv;
704         uint8_t *lapdh;
705
706         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
707
708         /* Remove RLL header from msgb */
709         msgb_pull_l2h(msg);
710
711         /* Push LAPDm header on msgb */
712         lapdh = msgb_push(msg, 3);
713         lapdh[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
714         lapdh[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
715         lapdh[2] = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO));
716
717         /* Tramsmit and start T200 */
718         bsc_schedule_timer(&dl->t200, T200);
719         return tx_ph_data_req(dl->entity->ms, msg, chan_nr, link_id);
720 }
721
722 /* L3 requests transfer of acknowledged information */
723 static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
724 {
725         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
726         uint8_t chan_nr = rllh->chan_nr;
727         uint8_t link_id = rllh->link_id;
728         uint8_t sapi = rllh->link_id & 7;
729         struct tlv_parsed tv;
730         uint8_t *lapdh;
731
732         switch (dl->state) {
733         case LAPDm_STATE_MF_EST:
734                 break;
735         default:
736                 printf("refusing RLL DATA REQ during DL state %u\n", dl->state);
737                 return -EIO;
738                 break;
739         }
740
741         /* FIXME: check if the layer3 message length exceeds N201 */
742
743         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
744
745         /* Remove the RSL/RLL header */
746         msgb_pull_l2h(msg);
747
748         /* Push the LAPDm header */
749         lapdh = msgb_put(msg, 3);
750         lapdh[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
751         lapdh[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
752         lapdh[2] = LAPDm_LEN(TLVP_LEN(&tv, RSL_IE_L3_INFO));
753
754         /* The value of the send state variable V(S) shall be incremented by 1
755          * at the end of the transmission of the I frame */
756         dl->V_send = inc_mod8(dl->V_send);
757
758         /* If timer T200 is not running at the time right before transmitting a
759          * frame, when the PH-READY-TO-SEND primitive is received from the
760          * physical layer., it shall be set. */
761         if (!bsc_timer_pending(&dl->t200))
762                 bsc_schedule_timer(&dl->t200, T200);
763
764         /* FIXME: If the send state variable V(S) is equal to V(A) plus k
765          * (where k is the maximum number of outstanding I frames - see
766          * subclause 5.8.4), the data link layer entity shall not transmit any
767          * new I frames, but shall retransmit an I frame as a result
768          * of the error recovery procedures as described in subclauses 5.5.4 and
769          * 5.5.7. */
770
771         return tx_ph_data_req(dl->entity->ms, msg, chan_nr, link_id);
772 }
773
774 /* incoming RSLms RLL message from L3 */
775 static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
776 {
777         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
778         int rc = 0;
779         uint8_t sapi = rllh->link_id & 7;
780         struct lapdm_entity *le;
781         struct lapdm_datalink *dl;
782
783         if (rllh->link_id & 0x40)
784                 le = &ms->lapdm_acch;
785         else
786                 le = &ms->lapdm_dcch;
787         dl = datalink_for_sapi(le, sapi);
788
789         switch (rllh->c.msg_type) {
790         case RSL_MT_UNIT_DATA_REQ:
791                 /* create and send UI command */
792                 rc = rslms_rx_rll_udata_req(msg, dl);
793                 break;
794         case RSL_MT_EST_REQ:
795                 /* create and send SABM command */
796                 rc = rslms_rx_rll_est_req(msg, dl);
797                 break;
798         case RSL_MT_DATA_REQ:
799                 /* create and send I command */
800                 rc = rslms_rx_rll_data_req(msg, dl);
801                 break;
802         case RSL_MT_REL_REQ:
803                 /* FIXME: create and send DISC command */
804         default:
805                 printf("unknown RLL message type 0x%02x\n",
806                         rllh->c.msg_type);
807                 break;
808         }
809
810         return rc;
811 }
812
813 /* input into layer2 (from layer 3) */
814 int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms)
815 {
816         struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
817         int rc = 0;
818
819         switch (rslh->msg_discr & 0xfe) {
820         case ABIS_RSL_MDISC_RLL:
821                 rc = rslms_rx_rll(msg, ms);
822                 break;
823         default:
824                 printf("unknown RSLms message discriminator 0x%02x",
825                         rslh->msg_discr);
826                 msgb_free(msg);
827                 return -EINVAL;
828         }
829
830         return rc;
831 }
832