[layer23] Fix in LAPDm: reset send_buffer offset at resume
[osmocom-bb.git] / src / host / layer23 / src / common / lapdm.c
1 /* GSM LAPDm (TS 04.06) implementation */
2
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
5  *
6  * All Rights Reserved
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  */
23
24 /* Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue
25  *
26  * RX data is stored in the rcv_buffer (pointer). If the message is complete, it
27  * is removed from rcv_buffer pointer and forwarded to L3. If the RX data is
28  * received while there is an incomplete rcv_buffer, it is appended to it.
29  *
30  * TX data is stored in the send_queue first. When transmitting a frame,
31  * the first message in the send_queue is moved to the send_buffer. There it
32  * resides until all fragments are acknowledged. Fragments to be sent by I
33  * frames are stored in the tx_hist buffer for resend, if required. Also the
34  * current fragment is copied into the tx_queue. There it resides until it is
35  * forwarded to layer 1.
36  *
37  * In case we have SAPI 0, we only have a window size of 1, so the unack-
38  * nowledged message resides always in the send_buffer. In case of a suspend,
39  * it can be written back to the first position of the send_queue.
40  *
41  * The layer 1 normally sends a PH-READY-TO-SEND. But because we use
42  * asynchronous transfer between layer 1 and layer 2 (serial link), we must
43  * send a frame before layer 1 reaches the right timeslot to send it. So we
44  * move the tx_queue to layer 1 when there is not already a pending frame, and
45  * wait until acknowledge after the frame has been sent. If we receive an
46  * acknowledge, we can send the next frame from the buffer, if any.
47  *
48  * The moving of tx_queue to layer 1 may also trigger T200, if desired. Also it
49  * will trigger next I frame, if possible.
50  *
51  */
52
53 #include <stdio.h>
54 #include <stdint.h>
55 #include <string.h>
56 #include <errno.h>
57 #include <arpa/inet.h>
58
59 #include <osmocore/logging.h>
60 #include <osmocore/timer.h>
61 #include <osmocore/msgb.h>
62 #include <osmocore/tlv.h>
63 #include <osmocore/utils.h>
64 #include <osmocore/rsl.h>
65 #include <osmocore/protocol/gsm_04_08.h>
66 #include <osmocore/protocol/gsm_08_58.h>
67
68 #include <osmocom/bb/common/osmocom_data.h>
69 #include <osmocom/bb/common/l1ctl.h>
70 #include <osmocom/bb/common/lapdm.h>
71 #include <osmocom/bb/common/logging.h>
72
73 #include <l1ctl_proto.h>
74
75 /* TS 04.06 Figure 4 / Section 3.2 */
76 #define LAPDm_LPD_NORMAL  0
77 #define LAPDm_LPD_SMSCB   1
78 #define LAPDm_SAPI_NORMAL 0
79 #define LAPDm_SAPI_SMS    3
80 #define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
81
82 #define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
83 #define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
84 #define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
85
86 /* TS 04.06 Table 3 / Section 3.4.3 */
87 #define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
88 #define LAPDm_CTRL_S(nr, s, p)  ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
89 #define LAPDm_CTRL_U(u, p)      ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
90
91 #define LAPDm_CTRL_is_I(ctrl)   (((ctrl) & 0x1) == 0)
92 #define LAPDm_CTRL_is_S(ctrl)   (((ctrl) & 0x3) == 1)
93 #define LAPDm_CTRL_is_U(ctrl)   (((ctrl) & 0x3) == 3)
94
95 #define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
96 #define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
97
98 #define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
99
100 #define LAPDm_CTRL_I_Ns(ctrl)   (((ctrl) & 0xE) >> 1)
101 #define LAPDm_CTRL_Nr(ctrl)     (((ctrl) & 0xE0) >> 5)
102
103 /* TS 04.06 Table 4 / Section 3.8.1 */
104 #define LAPDm_U_SABM    0x7
105 #define LAPDm_U_DM      0x3
106 #define LAPDm_U_UI      0x0
107 #define LAPDm_U_DISC    0x8
108 #define LAPDm_U_UA      0xC
109
110 #define LAPDm_S_RR      0x0
111 #define LAPDm_S_RNR     0x1
112 #define LAPDm_S_REJ     0x2
113
114 #define LAPDm_LEN(len)  ((len << 2) | 0x1)
115 #define LAPDm_MORE      0x2
116
117 /* TS 04.06 Section 5.8.3 */
118 #define N201_AB_SACCH           18
119 #define N201_AB_SDCCH           20
120 #define N201_AB_FACCH           20
121 #define N201_Bbis               23
122 #define N201_Bter_SACCH         21
123 #define N201_Bter_SDCCH         23
124 #define N201_Bter_FACCH         23
125 #define N201_B4                 19
126
127 /* 5.8.2.1 N200 during establish and release */
128 #define N200_EST_REL            5
129 /* 5.8.2.1 N200 during timer recovery state */
130 #define N200_TR_SACCH           5
131 #define N200_TR_SDCCH           23
132 #define N200_TR_FACCH_FR        34
133 #define N200_TR_EFACCH_FR       48
134 #define N200_TR_FACCH_HR        29
135 /* FIXME: this depends on chan type */
136 #define N200    N200_TR_SACCH
137
138 #define CR_MS2BS_CMD    0
139 #define CR_MS2BS_RESP   1
140 #define CR_BS2MS_CMD    1
141 #define CR_BS2MS_RESP   0
142
143 /* Set T200 to 1 Second (OpenBTS uses 900ms) */
144 #define T200    1, 0
145
146 /* k value for each SAPI */
147 static uint8_t k_sapi[] = {1, 1, 1, 1, 1, 1, 1, 1};
148
149 enum lapdm_format {
150         LAPDm_FMT_A,
151         LAPDm_FMT_B,
152         LAPDm_FMT_Bbis,
153         LAPDm_FMT_Bter,
154         LAPDm_FMT_B4,
155 };
156
157 static void lapdm_t200_cb(void *data);
158 static int rslms_send_i(struct lapdm_msg_ctx *mctx);
159
160 /* UTILITY FUNCTIONS */
161
162 static inline uint8_t inc_mod8(uint8_t x)
163 {
164         return (x + 1) & 7;
165 }
166
167 static inline uint8_t add_mod8(uint8_t x, uint8_t y)
168 {
169         return (x + y) & 7;
170 }
171
172 static inline uint8_t sub_mod8(uint8_t x, uint8_t y)
173 {
174         return (x - y) & 7; /* handle negative results correctly */
175 }
176
177 static void lapdm_dl_init(struct lapdm_datalink *dl,
178                           struct lapdm_entity *entity)
179 {
180         memset(dl, 0, sizeof(*dl));
181         INIT_LLIST_HEAD(&dl->send_queue);
182         INIT_LLIST_HEAD(&dl->tx_queue);
183         dl->state = LAPDm_STATE_IDLE;
184         dl->t200.data = dl;
185         dl->t200.cb = &lapdm_t200_cb;
186         dl->entity = entity;
187 }
188
189 void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms)
190 {
191         unsigned int i;
192
193         for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
194                 lapdm_dl_init(&le->datalink[i], le);
195
196         le->ms = ms;
197 }
198
199 static void lapdm_dl_flush_send(struct lapdm_datalink *dl)
200 {
201         struct msgb *msg;
202
203         /* Flush send-queue */
204         while ((msg = msgb_dequeue(&dl->send_queue)))
205                 msgb_free(msg);
206
207         /* Clear send-buffer */
208         if (dl->send_buffer) {
209                 msgb_free(dl->send_buffer);
210                 dl->send_buffer = NULL;
211         }
212 }
213
214 static void lapdm_dl_flush_tx(struct lapdm_datalink *dl)
215 {
216         struct msgb *msg;
217         unsigned int i;
218
219         while ((msg = msgb_dequeue(&dl->tx_queue)))
220                 msgb_free(msg);
221         for (i = 0; i < 8; i++)
222                 dl->tx_length[i] = 0;
223 }
224
225 void lapdm_exit(struct lapdm_entity *le)
226 {
227         unsigned int i;
228         struct lapdm_datalink *dl;
229
230         for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
231                 dl = &le->datalink[i];
232                 lapdm_dl_flush_tx(dl);
233                 lapdm_dl_flush_send(dl);
234                 if (dl->rcv_buffer)
235                         msgb_free(dl->rcv_buffer);
236         }
237 }
238
239 static void lapdm_dl_newstate(struct lapdm_datalink *dl, uint32_t state)
240 {
241         LOGP(DLAPDM, LOGL_INFO, "new state %s -> %s\n",
242                 lapdm_state_names[dl->state], lapdm_state_names[state]);
243         
244         dl->state = state;
245 }
246
247 static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
248 {
249         switch (sapi) {
250         case LAPDm_SAPI_NORMAL:
251                 return &le->datalink[0];
252         case LAPDm_SAPI_SMS:
253                 return &le->datalink[1];
254         default:
255                 return NULL;
256         }
257 }
258
259 /* remove the L2 header from a MSGB */
260 static inline unsigned char *msgb_pull_l2h(struct msgb *msg)
261 {
262         unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h);
263         msg->l2h = NULL;
264         return ret;
265 }
266
267 /* Append padding (if required) */
268 static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
269 {
270         int pad_len = n201 - msgb_l2len(msg);
271         uint8_t *data;
272
273         if (pad_len < 0) {
274                 LOGP(DLAPDM, LOGL_ERROR,
275                      "cannot pad message that is already too big!\n");
276                 return;
277         }
278
279         data = msgb_put(msg, pad_len);
280         memset(data, 0x2B, pad_len);
281 }
282
283 /* write a frame into the tx queue */
284 static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
285                                 uint8_t chan_nr, uint8_t link_id, uint8_t n201)
286 {
287         struct lapdm_entity *le = dl->entity;
288         struct osmocom_ms *ms = le->ms;
289
290         /* if there is a pending message, queue it */
291         if (le->tx_pending) {
292                 *msgb_push(msg, 1) = n201;
293                 *msgb_push(msg, 1) = link_id;
294                 *msgb_push(msg, 1) = chan_nr;
295                 msgb_enqueue(&dl->tx_queue, msg);
296                 return -EBUSY;
297         }
298
299         /* send the frame now */
300         le->tx_pending = 1;
301 #if 0
302 printf("-> tx chan_nr 0x%x link_id 0x%x len %d data", chan_nr, link_id, msgb_l2len(msg));
303 int i;
304 for (i = 0; i < msgb_l2len(msg); i++)
305         printf(" %02x", msg->l2h[i]);
306 printf("\n");
307 #endif
308         lapdm_pad_msgb(msg, n201);
309         return l1ctl_tx_data_req(ms, msg, chan_nr, link_id);
310 }
311
312 /* get next frame from the tx queue. because the ms has multiple datalinks,
313  * each datalink's queue is read round-robin.
314  */
315 int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
316 {
317         struct osmocom_ms *ms = le->ms;
318         struct lapdm_datalink *dl;
319         int last = le->last_tx_dequeue;
320         int i = last, n = ARRAY_SIZE(le->datalink);
321         uint8_t chan_nr, link_id, n201;
322
323         /* we may send again */
324         le->tx_pending = 0;
325
326 #if 0
327 printf("-> tx confirm\n");
328 #endif
329         /* free confirm message */
330         msgb_free(msg);
331
332         /* round-robin dequeue */
333         do {
334                 /* next */
335                 i = (i + 1) % n;
336                 dl = &le->datalink[i];
337                 if ((msg = msgb_dequeue(&dl->tx_queue)))
338                         break;
339         } while (i != last);
340
341         /* no message in all queues */
342         if (!msg)
343                 return 0;
344
345         /* Pull chan_nr and link_id */
346         chan_nr = *msg->data;
347         msgb_pull(msg, 1);
348         link_id = *msg->data;
349         msgb_pull(msg, 1);
350         n201 = *msg->data;
351         msgb_pull(msg, 1);
352
353         /* Set last dequeue position */
354         le->last_tx_dequeue = i;
355
356         /* Pad the frame, we can transmit now */
357         le->tx_pending = 1;
358 #if 0
359 printf("-> more tx chan_nr 0x%x link_id 0x%x len %d data", chan_nr, link_id, msgb_l2len(msg));
360 for (i = 0; i < msgb_l2len(msg); i++)
361         printf(" %02x", msg->l2h[i]);
362 printf("\n");
363 #endif
364         lapdm_pad_msgb(msg, n201);
365         return l1ctl_tx_data_req(ms, msg, chan_nr, link_id);
366 }
367
368 /* Create RSLms various RSLms messages */
369 static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
370                              struct msgb *msg)
371 {
372         /* Add the RSL + RLL header */
373         rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
374
375         /* send off the RSLms message to L3 */
376         return rslms_sendmsg(msg, mctx->dl->entity->ms);
377 }
378
379 /* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
380 static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
381 {
382         uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
383         struct abis_rsl_rll_hdr *rllh;
384
385         /* Add the RSL + RLL header */
386         msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
387         msgb_push(msg, 2 + 2);
388         rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
389                 mctx->link_id, 1);
390         rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
391
392         rllh->data[0] = RSL_IE_ACCESS_DELAY;
393         rllh->data[1] = mctx->ta_ind;
394
395         rllh->data[2] = RSL_IE_MS_POWER;
396         rllh->data[3] = mctx->tx_power_ind;
397         
398         return rslms_sendmsg(msg, mctx->dl->entity->ms);
399 }
400
401 static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
402 {
403         struct msgb *msg;
404
405         msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
406
407         /* send off the RSLms message to L3 */
408         return rslms_sendmsg(msg, mctx->dl->entity->ms);
409 }
410
411 static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
412 {
413         struct msgb *msg;
414         uint8_t *tlv;
415
416         LOGP(DLAPDM, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
417         msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
418         msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr) + 3);
419         tlv = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
420         tlv[0] = RSL_IE_RLM_CAUSE;
421         tlv[1] = 1;
422         tlv[2] = cause;
423         return rslms_sendmsg(msg, mctx->dl->entity->ms);
424 }
425
426 static int check_length_ind(struct lapdm_msg_ctx *mctx, uint8_t length_ind)
427 {
428         if (!(length_ind & 0x01)) {
429                 /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION
430                  * primitive with cause "frame not implemented" is sent to the
431                  * mobile management entity. */
432                 LOGP(DLAPDM, LOGL_NOTICE,
433                         "we don't support multi-octet length\n");
434                 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
435                 return -EINVAL;
436         }
437         return 0;
438 }
439
440 static int lapdm_send_resend(struct lapdm_datalink *dl)
441 {
442         struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm resend");
443         int length;
444
445         /* Resend SABM/DISC from tx_hist */
446         length = dl->tx_length[0];
447         msg->l2h = msgb_put(msg, length);
448         memcpy(msg->l2h, dl->tx_hist[dl->V_send], length);
449
450         return tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, dl->mctx.link_id,
451                         dl->mctx.n201);
452 }
453
454 static int lapdm_send_ua(struct lapdm_msg_ctx *mctx, uint8_t len, uint8_t *data)
455 {
456         uint8_t sapi = mctx->link_id & 7;
457         uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl);
458         struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm UA");
459         msg->l2h = msgb_put(msg, 3 + len);
460
461         msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
462         msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UA, f_bit);
463         msg->l2h[2] = LAPDm_LEN(len);
464         if (len)
465                 memcpy(msg->l2h + 3, data, len);
466
467         return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
468                         mctx->n201);
469 }
470
471 static int lapdm_send_dm(struct lapdm_msg_ctx *mctx)
472 {
473         uint8_t sapi = mctx->link_id & 7;
474         uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl);
475         struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm DM");
476         msg->l2h = msgb_put(msg, 3);
477
478         msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
479         msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DM, f_bit);
480         msg->l2h[2] = 0;
481
482         return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
483                         mctx->n201);
484 }
485
486 static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
487 {
488         uint8_t sapi = mctx->link_id & 7;
489         struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RR");
490         msg->l2h = msgb_put(msg, 3);
491
492         msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
493         msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit);
494         msg->l2h[2] = LAPDm_LEN(0);
495
496         return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
497                         mctx->n201);
498 }
499
500 static int lapdm_send_rnr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
501 {
502         uint8_t sapi = mctx->link_id & 7;
503         struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RNR");
504         msg->l2h = msgb_put(msg, 3);
505
506         msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
507         msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RNR, f_bit);
508         msg->l2h[2] = LAPDm_LEN(0);
509
510         return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
511                         mctx->n201);
512 }
513
514 static int lapdm_send_rej(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
515 {
516         uint8_t sapi = mctx->link_id & 7;
517         struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm REJ");
518         msg->l2h = msgb_put(msg, 3);
519
520         msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
521         msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_REJ, f_bit);
522         msg->l2h[2] = LAPDm_LEN(0);
523
524         return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
525                         mctx->n201);
526 }
527
528 /* Timer callback on T200 expiry */
529 static void lapdm_t200_cb(void *data)
530 {
531         struct lapdm_datalink *dl = data;
532
533         LOGP(DLAPDM, LOGL_INFO, "lapdm_t200_cb(%p) state=%u\n", dl, dl->state);
534
535         switch (dl->state) {
536         case LAPDm_STATE_SABM_SENT:
537                 /* 5.4.1.3 */
538                 if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) {
539                         /* send RELEASE INDICATION to L3 */
540                         send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
541                         /* send MDL ERROR INIDCATION to L3 */
542                         rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
543                         /* go back to idle state */
544                         lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
545                         /* NOTE: we must not change any other states or buffers
546                          * and queues, since we may reconnect after handover
547                          * failure. the buffered messages is replaced there */
548                         break;
549                 }
550                 /* retransmit SABM command */
551                 lapdm_send_resend(dl);
552                 /* increment re-transmission counter */
553                 dl->retrans_ctr++;
554                 /* restart T200 (PH-READY-TO-SEND) */
555                 bsc_schedule_timer(&dl->t200, T200);
556                 break;
557         case LAPDm_STATE_DISC_SENT:
558                 /* 5.4.4.3 */
559                 if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) {
560                         /* send RELEASE INDICATION to L3 */
561                         send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
562                         /* send MDL ERROR INIDCATION to L3 */
563                         rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
564                         /* go back to idle state */
565                         lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
566                         /* NOTE: we must not change any other states or buffers
567                          * and queues, since we may reconnect after handover
568                          * failure. the buffered messages is replaced there */
569                         break;
570                 }
571                 /* retransmit DISC command */
572                 lapdm_send_resend(dl);
573                 /* increment re-transmission counter */
574                 dl->retrans_ctr++;
575                 /* restart T200 (PH-READY-TO-SEND) */
576                 bsc_schedule_timer(&dl->t200, T200);
577                 break;
578         case LAPDm_STATE_MF_EST:
579                 /* 5.5.7 */
580                 dl->retrans_ctr = 0;
581                 lapdm_dl_newstate(dl, LAPDm_STATE_TIMER_RECOV);
582                 /* fall through */
583         case LAPDm_STATE_TIMER_RECOV:
584                 dl->retrans_ctr++;
585                 if (dl->retrans_ctr < N200) {
586                         /* retransmit I frame (V_s-1) with P=1, if any */
587                         if (dl->tx_length[dl->V_send - 1]) {
588                                 struct msgb *msg;
589                                 int length;
590
591                                 LOGP(DLAPDM, LOGL_INFO, "retransmit last frame "
592                                         "V(S)=%d\n", dl->V_send - 1);
593                                 /* Create I frame (segment) from tx_hist */
594                                 length = dl->tx_length[dl->V_send - 1];
595                                 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
596                                 msg->l2h = msgb_put(msg, length);
597                                 memcpy(msg->l2h, dl->tx_hist[dl->V_send - 1],
598                                         length);
599                                 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv,
600                                                 dl->V_send - 1, 1); /* P=1 */
601                                 tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr,
602                                         dl->mctx.link_id, dl->mctx.n201);
603                         } else {
604                         /* OR send appropriate supervision frame with P=1 */
605                                 if (!dl->own_busy && !dl->seq_err_cond) {
606                                         lapdm_send_rr(&dl->mctx, 1);
607                                         /* NOTE: In case of sequence error
608                                          * condition, the REJ frame has been
609                                          * transmitted when entering the
610                                          * condition, so it has not be done
611                                          * here
612                                          */
613                                 } else if (dl->own_busy) {
614                                         lapdm_send_rnr(&dl->mctx, 1);
615                                 } else {
616                                         LOGP(DLAPDM, LOGL_INFO, "unhandled, "
617                                                 "pls. fix\n");
618                                 }
619                         }
620                         /* restart T200 (PH-READY-TO-SEND) */
621                         bsc_schedule_timer(&dl->t200, T200);
622                 } else {
623                         /* send MDL ERROR INIDCATION to L3 */
624                         rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
625                 }
626                 break;
627         default:
628                 LOGP(DLAPDM, LOGL_INFO, "T200 expired in unexpected "
629                         "dl->state %u\n", dl->state);
630         }
631 }
632
633 /* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
634 static void lapdm_acknowledge(struct lapdm_msg_ctx *mctx)
635 {
636         struct lapdm_datalink *dl = mctx->dl;
637         uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl);
638         int s = 0, rej = 0, t200_reset = 0;
639         int i;
640
641         /* supervisory frame ? */
642         if (LAPDm_CTRL_is_S(mctx->ctrl))
643                 s = 1;
644         /* REJ frame ? */
645         if (s && LAPDm_CTRL_S_BITS(mctx->ctrl) == LAPDm_S_REJ)
646                 rej = 1;
647
648         /* Flush all transmit buffers of acknowledged frames */
649         for (i = dl->V_ack; i != nr; i = inc_mod8(i)) {
650                 if (dl->tx_length[i]) {
651                         dl->tx_length[i] = 0;
652                         LOGP(DLAPDM, LOGL_INFO, "ack frame %d\n", i);
653                 }
654         }
655
656         if (dl->state != LAPDm_STATE_TIMER_RECOV) {
657                 /* When not in the timer recovery condition, the data
658                  * link layer entity shall reset the timer T200 on
659                  * receipt of a valid I frame with N(R) higher than V(A),
660                  * or an REJ with an N(R) equal to V(A). */
661                 if ((!rej && nr != dl->V_ack)
662                  || (rej && nr == dl->V_ack)) {
663                         LOGP(DLAPDM, LOGL_INFO, "reset t200\n");
664                         t200_reset = 1;
665                         bsc_del_timer(&dl->t200);
666                         /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
667                 }
668                 /* 5.7.4: N(R) sequence error
669                  * N(R) is called valid, if and only if
670                  * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
671                  */
672                 if (sub_mod8(nr, dl->V_ack) > sub_mod8(dl->V_send, dl->V_ack)) {
673                         LOGP(DLAPDM, LOGL_NOTICE, "N(R) sequence error\n");
674                         rsl_rll_error(RLL_CAUSE_SEQ_ERR, mctx);
675                 }
676         }
677
678         /* V(A) shall be set to the value of N(R) */
679         dl->V_ack = nr;
680
681         /* If T200 has been reset by the receipt of an I, RR or RNR frame,
682          * and if there are outstanding I frames, restart T200 */
683         if (t200_reset && !rej) {
684                 if (dl->tx_length[dl->V_send - 1]) {
685                         LOGP(DLAPDM, LOGL_INFO, "start T200, due to unacked I "
686                                 "frame(s)\n");
687                         bsc_schedule_timer(&dl->t200, T200);
688                 }
689         }
690 }
691
692 /* L1 -> L2 */
693
694 /* Receive a LAPDm U (Unnumbered) message from L1 */
695 static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx)
696 {
697         struct lapdm_datalink *dl = mctx->dl;
698         uint8_t length;
699         int rc;
700         int rsl_msg;
701
702         switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) {
703         case LAPDm_U_SABM:
704                 rsl_msg = RSL_MT_EST_IND;
705
706                 LOGP(DLAPDM, LOGL_INFO, "SABM received\n");
707                 /* 5.7.1 */
708                 dl->seq_err_cond = 0;
709                 /* G.2.2 Wrong value of the C/R bit */
710                 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
711                         LOGP(DLAPDM, LOGL_NOTICE, "SABM response error\n");
712                         msgb_free(msg);
713                         rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
714                         return -EINVAL;
715                 }
716
717                 length = msg->l2h[2] >> 2;
718                 /* G.4.5 If SABM is received with L>N201 or with M bit
719                  * set, AN MDL-ERROR-INDICATION is sent to MM.
720                  */
721                 if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) {
722                         LOGP(DLAPDM, LOGL_NOTICE, "SABM too large error\n");
723                         msgb_free(msg);
724                         rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
725                         return -EIO;
726                 }
727
728                 /* Must be Format B */
729                 rc = check_length_ind(mctx, msg->l2h[2]);
730                 if (rc < 0) {
731                         msgb_free(msg);
732                         return rc;
733                 }
734                 switch (dl->state) {
735                 case LAPDm_STATE_IDLE:
736                         /* Set chan_nr and link_id for established connection */
737                         memset(&dl->mctx, 0, sizeof(dl->mctx));
738                         dl->mctx.dl = dl;
739                         dl->mctx.chan_nr = mctx->chan_nr;
740                         dl->mctx.link_id = mctx->link_id;
741                         break;
742                 case LAPDm_STATE_MF_EST:
743                         if (length == 0) {
744                                 rsl_msg = RSL_MT_EST_CONF;
745                                 break;
746                         }
747                         LOGP(DLAPDM, LOGL_INFO, "SABM command, multiple "
748                                 "frame established state\n");
749                         /* check for contention resoultion */
750                         if (dl->tx_hist[0][2] >> 2) {
751                                 LOGP(DLAPDM, LOGL_NOTICE, "SABM not allowed "
752                                         "during contention resolution\n");
753                                 rsl_rll_error(RLL_CAUSE_SABM_INFO_NOTALL, mctx);
754                         }
755                         msgb_free(msg);
756                         return 0;
757                 case LAPDm_STATE_DISC_SENT:
758                         /* 5.4.6.2 send DM with F=P */
759                         lapdm_send_dm(mctx);
760                         /* reset Timer T200 */
761                         bsc_del_timer(&dl->t200);
762                         msgb_free(msg);
763                         return send_rll_simple(RSL_MT_REL_CONF, mctx);
764                 default:
765                         lapdm_send_ua(mctx, length, msg->l2h + 3);
766                         msgb_free(msg);
767                         return 0;
768                 }
769                 /* send UA response */
770                 lapdm_send_ua(mctx, length, msg->l2h + 3);
771                 /* set Vs, Vr and Va to 0 */
772                 dl->V_send = dl->V_recv = dl->V_ack = 0;
773                 /* clear tx_hist */
774                 dl->tx_length[0] = 0;
775                 /* enter multiple-frame-established state */
776                 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
777                 /* send notification to L3 */
778                 if (length == 0) {
779                         /* 5.4.1.2 Normal establishment procedures */
780                         rc = send_rll_simple(rsl_msg, mctx);
781                         msgb_free(msg);
782                 } else {
783                         /* 5.4.1.4 Contention resolution establishment */
784                         msg->l3h = msg->l2h + 3;
785                         msgb_pull_l2h(msg);
786                         rc = send_rslms_rll_l3(rsl_msg, mctx, msg);
787                 }
788                 break;
789         case LAPDm_U_DM:
790                 LOGP(DLAPDM, LOGL_INFO, "DM received\n");
791                 /* G.2.2 Wrong value of the C/R bit */
792                 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
793                         LOGP(DLAPDM, LOGL_NOTICE, "DM command error\n");
794                         msgb_free(msg);
795                         rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
796                         return -EINVAL;
797                 }
798                 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
799                         /* 5.4.1.2 DM responses with the F bit set to "0"
800                          * shall be ignored.
801                          */
802                         msgb_free(msg);
803                         return 0;
804                 }
805                 switch (dl->state) {
806                 case LAPDm_STATE_SABM_SENT:
807                         break;
808                 case LAPDm_STATE_MF_EST:
809                         if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1) {
810                                 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
811                                         "response\n");
812                                 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP, mctx);
813                         } else {
814                                 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
815                                         "response, multiple frame established "
816                                         "state\n");
817                                 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx);
818                         }
819                         msgb_free(msg);
820                         return 0;
821                 case LAPDm_STATE_TIMER_RECOV:
822                         /* DM is normal in case PF = 1 */
823                         if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) {
824                                 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
825                                         "response, multiple frame established "
826                                         "state\n");
827                                 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx);
828                                 msgb_free(msg);
829                                 return 0;
830                         }
831                         break;
832                 case LAPDm_STATE_DISC_SENT:
833                         /* reset Timer T200 */
834                         bsc_del_timer(&dl->t200);
835                         /* go to idle state */
836                         lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
837                         rc = send_rll_simple(RSL_MT_REL_CONF, mctx);
838                         msgb_free(msg);
839                         return 0;
840                 case LAPDm_STATE_IDLE:
841                         /* 5.4.5 all other frame types shall be discarded */
842                 default:
843                         LOGP(DLAPDM, LOGL_INFO, "unsolicited DM response! "
844                                 "(discarding)\n");
845                         msgb_free(msg);
846                         return 0;
847                 }
848                 /* reset T200 */
849                 bsc_del_timer(&dl->t200);
850                 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
851                 msgb_free(msg);
852                 break;
853         case LAPDm_U_UI:
854                 LOGP(DLAPDM, LOGL_INFO, "UI received\n");
855                 /* G.2.2 Wrong value of the C/R bit */
856                 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
857                         LOGP(DLAPDM, LOGL_NOTICE, "UI indicates response "
858                                 "error\n");
859                         msgb_free(msg);
860                         rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
861                         return -EINVAL;
862                 }
863
864                 length = msg->l2h[2] >> 2;
865                 /* FIXME: G.4.5 If UI is received with L>N201 or with M bit
866                  * set, AN MDL-ERROR-INDICATION is sent to MM.
867                  */
868
869                 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
870                         length = N201_B4;
871                         msg->l3h = msg->l2h + 2;
872                 } else {
873                         rc = check_length_ind(mctx, msg->l2h[2]);
874                         if (rc < 0) {
875                                 msgb_free(msg);
876                                 return rc;
877                         }
878                         length = msg->l2h[2] >> 2;
879                         msg->l3h = msg->l2h + 3;
880                 }
881                 /* do some length checks */
882                 if (length == 0) {
883                         /* 5.3.3 UI frames received with the length indicator
884                          * set to "0" shall be ignored
885                          */
886                         LOGP(DLAPDM, LOGL_INFO, "length=0 (discarding)\n");
887                         msgb_free(msg);
888                         return 0;
889                 }
890                 switch (LAPDm_ADDR_SAPI(mctx->addr)) {
891                 case LAPDm_SAPI_NORMAL:
892                 case LAPDm_SAPI_SMS:
893                         break;
894                 default:
895                         /* 5.3.3 UI frames with invalid SAPI values shall be
896                          * discarded
897                          */
898                         LOGP(DLAPDM, LOGL_INFO, "sapi=%u (discarding)\n",
899                                 LAPDm_ADDR_SAPI(mctx->addr));
900                         msgb_free(msg);
901                         return 0;
902                 }
903                 msgb_pull_l2h(msg);
904                 rc = send_rslms_rll_l3_ui(mctx, msg);
905                 break;
906         case LAPDm_U_DISC:
907                 rsl_msg = RSL_MT_REL_IND;
908
909                 LOGP(DLAPDM, LOGL_INFO, "DISC received\n");
910                 /* flush buffers */
911                 lapdm_dl_flush_tx(dl);
912                 lapdm_dl_flush_send(dl);
913                 /* 5.7.1 */
914                 dl->seq_err_cond = 0;
915                 /* G.2.2 Wrong value of the C/R bit */
916                 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
917                         LOGP(DLAPDM, LOGL_NOTICE, "DISC response error\n");
918                         msgb_free(msg);
919                         rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
920                         return -EINVAL;
921                 }
922                 length = msg->l2h[2] >> 2;
923                 if (length > 0 || msg->l2h[2] & 0x02) {
924                         /* G.4.4 If a DISC or DM frame is received with L>0 or
925                          * with the M bit set to "1", an MDL-ERROR-INDICATION
926                          * primitive with cause "U frame with incorrect
927                          * parameters" is sent to the mobile management entity. */
928                         LOGP(DLAPDM, LOGL_NOTICE,
929                                 "U frame iwth incorrect parameters ");
930                         msgb_free(msg);
931                         rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
932                         return -EIO;
933                 }
934                 msgb_free(msg);
935                 switch (dl->state) {
936                 case LAPDm_STATE_IDLE:
937                         LOGP(DLAPDM, LOGL_INFO, "DISC in idle state\n");
938                         /* send DM with F=P */
939                         return lapdm_send_dm(mctx);
940                 case LAPDm_STATE_SABM_SENT:
941                         LOGP(DLAPDM, LOGL_INFO, "DISC in SABM state\n");
942                         /* 5.4.6.2 send DM with F=P */
943                         lapdm_send_dm(mctx);
944                         /* reset Timer T200 */
945                         bsc_del_timer(&dl->t200);
946                         return send_rll_simple(RSL_MT_REL_IND, mctx);
947                 case LAPDm_STATE_MF_EST:
948                 case LAPDm_STATE_TIMER_RECOV:
949                         LOGP(DLAPDM, LOGL_INFO, "DISC in est state\n");
950                         break;
951                 case LAPDm_STATE_DISC_SENT:
952                         LOGP(DLAPDM, LOGL_INFO, "DISC in disc state\n");
953                         rsl_msg = RSL_MT_REL_CONF;
954                         break;
955                 default:
956                         lapdm_send_ua(mctx, length, msg->l2h + 3);
957                         return 0;
958                 }
959                 /* send UA response */
960                 lapdm_send_ua(mctx, length, msg->l2h + 3);
961                 /* reset Timer T200 */
962                 bsc_del_timer(&dl->t200);
963                 /* enter idle state */
964                 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
965                 /* send notification to L3 */
966                 rc = send_rll_simple(rsl_msg, mctx);
967                 break;
968         case LAPDm_U_UA:
969                 LOGP(DLAPDM, LOGL_INFO, "UA received\n");
970                 /* G.2.2 Wrong value of the C/R bit */
971                 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
972                         LOGP(DLAPDM, LOGL_NOTICE, "UA indicates command "
973                                 "error\n");
974                         msgb_free(msg);
975                         rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
976                         return -EINVAL;
977                 }
978
979                 length = msg->l2h[2] >> 2;
980                 /* G.4.5 If UA is received with L>N201 or with M bit
981                  * set, AN MDL-ERROR-INDICATION is sent to MM.
982                  */
983                 if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) {
984                         LOGP(DLAPDM, LOGL_NOTICE, "UA too large error\n");
985                         msgb_free(msg);
986                         rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
987                         return -EIO;
988                 }
989
990                 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
991                         /* 5.4.1.2 A UA response with the F bit set to "0"
992                          * shall be ignored.
993                          */
994                         LOGP(DLAPDM, LOGL_INFO, "F=0 (discarding)\n");
995                         msgb_free(msg);
996                         return 0;
997                 }
998                 switch (dl->state) {
999                 case LAPDm_STATE_SABM_SENT:
1000                         break;
1001                 case LAPDm_STATE_MF_EST:
1002                 case LAPDm_STATE_TIMER_RECOV:
1003                         LOGP(DLAPDM, LOGL_INFO, "unsolicited UA response! "
1004                                 "(discarding)\n");
1005                         rsl_rll_error(RLL_CAUSE_UNSOL_UA_RESP, mctx);
1006                         msgb_free(msg);
1007                         return 0;
1008                 case LAPDm_STATE_DISC_SENT:
1009                         LOGP(DLAPDM, LOGL_INFO, "UA in disconnect state\n");
1010                         /* reset Timer T200 */
1011                         bsc_del_timer(&dl->t200);
1012                         /* go to idle state */
1013                         lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1014                         rc = send_rll_simple(RSL_MT_REL_CONF, mctx);
1015                         msgb_free(msg);
1016                         return 0;
1017                 case LAPDm_STATE_IDLE:
1018                         /* 5.4.5 all other frame types shall be discarded */
1019                 default:
1020                         LOGP(DLAPDM, LOGL_INFO, "unsolicited UA response! "
1021                                 "(discarding)\n");
1022                         msgb_free(msg);
1023                         return 0;
1024                 }
1025                 LOGP(DLAPDM, LOGL_INFO, "UA in SABM state\n");
1026                 /* reset Timer T200 */
1027                 bsc_del_timer(&dl->t200);
1028                 /* compare UA with SABME if contention resolution is applied */
1029                 if (dl->tx_hist[0][2] >> 2) {
1030                         rc = check_length_ind(mctx, msg->l2h[2]);
1031                         if (rc < 0) {
1032                                 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
1033                                 msgb_free(msg);
1034                                 /* go to idle state */
1035                                 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1036                                 return 0;
1037                         }
1038                         length = msg->l2h[2] >> 2;
1039                         if (length != (dl->tx_hist[0][2] >> 2)
1040                          || !!memcmp(dl->tx_hist[0] + 3, msg->l2h + 3,
1041                                         length)) {
1042                                 LOGP(DLAPDM, LOGL_INFO, "UA response "
1043                                         "mismatches\n");
1044 int i;
1045 for (i = 0; i < (dl->tx_hist[0][2] >> 2); i++)
1046         printf(" %02x", dl->tx_hist[0][3+i]);
1047 printf(" == SENT\n");
1048 for (i = 0; i < length; i++)
1049         printf(" %02x", msg->l2h[3+i]);
1050 printf(" == RECEIVED (len=%d,%d)\n", msg->l2h[2] >> 2, length);
1051                                 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
1052                                 msgb_free(msg);
1053                                 /* go to idle state */
1054                                 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1055                                 return 0;
1056                         }
1057                 }
1058                 /* set Vs, Vr and Va to 0 */
1059                 dl->V_send = dl->V_recv = dl->V_ack = 0;
1060                 /* clear tx_hist */
1061                 dl->tx_length[0] = 0;
1062                 /* enter multiple-frame-established state */
1063                 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1064                 /* send outstanding frames, if any (resume / reconnect) */
1065                 rslms_send_i(mctx);
1066                 /* send notification to L3 */
1067                 rc = send_rll_simple(RSL_MT_EST_CONF, mctx);
1068                 msgb_free(msg);
1069                 break;
1070         default:
1071                 /* G.3.1 */
1072                 LOGP(DLAPDM, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1073                 msgb_free(msg);
1074                 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1075                 return -EINVAL;
1076         }
1077         return rc;
1078 }
1079
1080 /* Receive a LAPDm S (Supervisory) message from L1 */
1081 static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1082 {
1083         struct lapdm_datalink *dl = mctx->dl;
1084         uint8_t length;
1085
1086         length = msg->l2h[2] >> 2;
1087         if (length > 0 || msg->l2h[2] & 0x02) {
1088                 /* G.4.3 If a supervisory frame is received with L>0 or
1089                  * with the M bit set to "1", an MDL-ERROR-INDICATION
1090                  * primitive with cause "S frame with incorrect
1091                  * parameters" is sent to the mobile management entity. */
1092                 LOGP(DLAPDM, LOGL_NOTICE,
1093                                 "S frame with incorrect parameters\n");
1094                 msgb_free(msg);
1095                 rsl_rll_error(RLL_CAUSE_SFRM_INC_PARAM, mctx);
1096                 return -EIO;
1097         }
1098         /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1099         if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1100          && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1101                 LOGP(DLAPDM, LOGL_NOTICE, "S frame response with F=1 error\n");
1102                 rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
1103         }
1104
1105         switch (dl->state) {
1106         case LAPDm_STATE_IDLE:
1107                 /* if P=1, respond DM with F=1 (5.2.2) */
1108                 /* 5.4.5 all other frame types shall be discarded */
1109                 if (LAPDm_CTRL_PF_BIT(mctx->ctrl))
1110                         lapdm_send_dm(mctx); /* F=P */
1111                 /* fall though */
1112         case LAPDm_STATE_SABM_SENT:
1113         case LAPDm_STATE_DISC_SENT:
1114                 LOGP(DLAPDM, LOGL_NOTICE, "S frame ignored in this state\n");
1115                 msgb_free(msg);
1116                 return 0;
1117         }
1118         switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) {
1119         case LAPDm_S_RR:
1120                 LOGP(DLAPDM, LOGL_INFO, "RR received\n");
1121                 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1122                 lapdm_acknowledge(mctx);
1123
1124                 /* 5.5.3.2 */
1125                 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1126                  && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1127                         if (!dl->own_busy && !dl->seq_err_cond) {
1128                                 LOGP(DLAPDM, LOGL_NOTICE, "RR frame command with polling bit set and we are not busy, so we reply with RR frame\n");
1129                                 lapdm_send_rr(mctx, 1);
1130                                 /* NOTE: In case of sequence error condition,
1131                                  * the REJ frame has been transmitted when
1132                                  * entering the condition, so it has not be
1133                                  * done here
1134                                  */
1135                         } else if (dl->own_busy) {
1136                                 LOGP(DLAPDM, LOGL_NOTICE, "RR frame command with polling bit set and we are busy, so we reply with RR frame\n");
1137                                 lapdm_send_rnr(mctx, 1);
1138                         }
1139                 }
1140                 /* Send message, if possible due to acknowledged data */
1141                 rslms_send_i(mctx);
1142
1143                 break;
1144         case LAPDm_S_RNR:
1145                 LOGP(DLAPDM, LOGL_INFO, "RNR received\n");
1146                 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1147                 lapdm_acknowledge(mctx);
1148
1149                 /* 5.5.5 */
1150                 /* Set peer receiver busy condition */
1151                 dl->peer_busy = 1;
1152
1153                 if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1154                         if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
1155                                 if (!dl->own_busy) {
1156                                         LOGP(DLAPDM, LOGL_INFO, "RNR poll "
1157                                                 "command and we are not busy, "
1158                                                 "so we reply with RR final "
1159                                                 "response\n");
1160                                         /* Send RR with F=1 */
1161                                         lapdm_send_rr(mctx, 1);
1162                                 } else {
1163                                         LOGP(DLAPDM, LOGL_INFO, "RNR poll "
1164                                                 "command and we are busy, so "
1165                                                 "we reply with RNR final "
1166                                                 "response\n");
1167                                         /* Send RNR with F=1 */
1168                                         lapdm_send_rnr(mctx, 1);
1169                                 }
1170                         } else if (dl->state == LAPDm_STATE_TIMER_RECOV) {
1171                                 LOGP(DLAPDM, LOGL_INFO, "RNR poll response "
1172                                         "and we in timer recovery state, so "
1173                                         "we leave that state\n");
1174                                 /* Clear timer recovery condition */
1175                                 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1176                                 /* V(S) to the N(R) in the RNR frame */
1177                                 dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl);
1178                         }
1179                 } else
1180                         LOGP(DLAPDM, LOGL_INFO, "RNR not polling/final state "
1181                                 "received\n");
1182
1183                 /* Send message, if possible due to acknowledged data */
1184                 rslms_send_i(mctx);
1185
1186                 break;
1187         case LAPDm_S_REJ:
1188                 LOGP(DLAPDM, LOGL_INFO, "REJ received\n");
1189                 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1190                 lapdm_acknowledge(mctx);
1191
1192                 /* 5.5.4.1 */
1193                 if (dl->state != LAPDm_STATE_TIMER_RECOV) {
1194                         /* Clear an existing peer receiver busy condition */
1195                         dl->peer_busy = 0;
1196                         /* V(S) and V(A) to the N(R) in the REJ frame */
1197                         dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1198                         /* reset Timer T200 */
1199                         bsc_del_timer(&dl->t200);
1200                         /* 5.5.3.2 */
1201                         if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1202                          && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1203                                 if (!dl->own_busy && !dl->seq_err_cond) {
1204                                         LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1205                                                 "command not in timer recovery "
1206                                                 "state and not in own busy "
1207                                                 "condition received, so we "
1208                                                 "respond with RR final "
1209                                                 "response\n");
1210                                         lapdm_send_rr(mctx, 1);
1211                                         /* NOTE: In case of sequence error
1212                                          * condition, the REJ frame has been
1213                                          * transmitted when entering the
1214                                          * condition, so it has not be done
1215                                          * here
1216                                          */
1217                                 } else if (dl->own_busy) {
1218                                         LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1219                                                 "command not in timer recovery "
1220                                                 "state and in own busy "
1221                                                 "condition received, so we "
1222                                                 "respond with RNR final "
1223                                                 "response\n");
1224                                         lapdm_send_rnr(mctx, 1);
1225                                 }
1226                         } else
1227                                 LOGP(DLAPDM, LOGL_INFO, "REJ response or not "
1228                                         "polling command not in timer recovery "
1229                                         "state received\n");
1230                         /* send MDL ERROR INIDCATION to L3 */
1231                         if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1232                          && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1233                                 rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
1234                         }
1235
1236                 } else if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1237                         && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1238                         LOGP(DLAPDM, LOGL_INFO, "REJ poll response in timer "
1239                                 "recovery state received\n");
1240                         /* Clear an existing peer receiver busy condition */
1241                         dl->peer_busy = 0;
1242                         /* Clear timer recovery condition */
1243                         lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1244                         /* V(S) and V(A) to the N(R) in the REJ frame */
1245                         dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1246                         /* reset Timer T200 */
1247                         bsc_del_timer(&dl->t200);
1248                 } else {
1249                         /* Clear an existing peer receiver busy condition */
1250                         dl->peer_busy = 0;
1251                         /* V(S) and V(A) to the N(R) in the REJ frame */
1252                         dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1253                         /* 5.5.3.2 */
1254                         if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1255                          && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1256                                 if (!dl->own_busy && !dl->seq_err_cond) {
1257                                         LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1258                                                 "command in timer recovery "
1259                                                 "state and not in own busy "
1260                                                 "condition received, so we "
1261                                                 "respond with RR final "
1262                                                 "response\n");
1263                                         lapdm_send_rr(mctx, 1);
1264                                         /* NOTE: In case of sequence error
1265                                          * condition, the REJ frame has been
1266                                          * transmitted when entering the
1267                                          * condition, so it has not be done
1268                                          * here
1269                                          */
1270                                 } else if (dl->own_busy) {
1271                                         LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1272                                                 "command in timer recovery "
1273                                                 "state and in own busy "
1274                                                 "condition received, so we "
1275                                                 "respond with RNR final "
1276                                                 "response\n");
1277                                         lapdm_send_rnr(mctx, 1);
1278                                 }
1279                         } else
1280                                 LOGP(DLAPDM, LOGL_INFO, "REJ response or not "
1281                                         "polling command in timer recovery "
1282                                         "state received\n");
1283                 }
1284
1285                 /* FIXME: 5.5.4.2 2) */
1286
1287                 /* Send message, if possible due to acknowledged data */
1288                 rslms_send_i(mctx);
1289
1290                 break;
1291         default:
1292                 /* G.3.1 */
1293                 LOGP(DLAPDM, LOGL_NOTICE, "Supervisory frame not allowed.\n");
1294                 msgb_free(msg);
1295                 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1296                 return -EINVAL;
1297         }
1298         msgb_free(msg);
1299         return 0;
1300 }
1301
1302 /* Receive a LAPDm I (Information) message from L1 */
1303 static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1304 {
1305         struct lapdm_datalink *dl = mctx->dl;
1306         //uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl);
1307         uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl);
1308         uint8_t length;
1309         int rc;
1310
1311         LOGP(DLAPDM, LOGL_NOTICE, "I received\n");
1312                 
1313         /* G.2.2 Wrong value of the C/R bit */
1314         if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
1315                 LOGP(DLAPDM, LOGL_NOTICE, "I frame response not allowed\n");
1316                 msgb_free(msg);
1317                 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1318                 return -EINVAL;
1319         }
1320
1321         length = msg->l2h[2] >> 2;
1322         if (length == 0 || length + 3 > mctx->n201) {
1323                 /* G.4.2 If the length indicator of an I frame is set
1324                  * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1325                  * primitive with cause "I frame with incorrect length"
1326                  * is sent to the mobile management entity. */
1327                 LOGP(DLAPDM, LOGL_NOTICE, "I frame length not allowed\n");
1328                 msgb_free(msg);
1329                 rsl_rll_error(RLL_CAUSE_IFRM_INC_LEN, mctx);
1330                 return -EIO;
1331         }
1332
1333         /* G.4.2 If the numerical value of L is L<N201 and the M
1334          * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1335          * cause "I frame with incorrect use of M bit" is sent to the
1336          * mobile management entity. */
1337         if ((msg->l2h[2] & LAPDm_MORE) && length + 3 < mctx->n201) {
1338                 LOGP(DLAPDM, LOGL_NOTICE, "I frame with M bit too short\n");
1339                 msgb_free(msg);
1340                 rsl_rll_error(RLL_CAUSE_IFRM_INC_MBITS, mctx);
1341                 return -EIO;
1342         }
1343
1344         switch (dl->state) {
1345         case LAPDm_STATE_IDLE:
1346                 /* if P=1, respond DM with F=1 (5.2.2) */
1347                 /* 5.4.5 all other frame types shall be discarded */
1348                 if (LAPDm_CTRL_PF_BIT(mctx->ctrl))
1349                         lapdm_send_dm(mctx); /* F=P */
1350                 /* fall though */
1351         case LAPDm_STATE_SABM_SENT:
1352         case LAPDm_STATE_DISC_SENT:
1353                 LOGP(DLAPDM, LOGL_NOTICE, "I frame ignored in this state\n");
1354                 msgb_free(msg);
1355                 return 0;
1356         }
1357
1358         /* 5.7.1: N(s) sequence error */
1359         if (ns != dl->V_recv) {
1360                 LOGP(DLAPDM, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1361                      "V(R)=%u\n", ns, dl->V_recv);
1362                 /* discard data */
1363                 msgb_free(msg);
1364                 if (!dl->seq_err_cond) {
1365                         /* FIXME: help me understand what exactly todo here
1366                         dl->seq_err_cond = 1;
1367                         */
1368                         lapdm_send_rej(mctx, LAPDm_CTRL_PF_BIT(mctx->ctrl));
1369                 } else {
1370                 }
1371                 return -EIO;
1372         }
1373         dl->seq_err_cond = 0;
1374
1375         /* Increment receiver state */
1376         dl->V_recv = inc_mod8(dl->V_recv);
1377         LOGP(DLAPDM, LOGL_NOTICE, "incrementing V(R) to %u\n", dl->V_recv);
1378
1379         /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1380         lapdm_acknowledge(mctx); /* V(A) is also set here */
1381
1382         /* Only if we are not in own receiver busy condition */
1383         if (!dl->own_busy) {
1384                 /* if the frame carries a complete segment */
1385                 if (!(msg->l2h[2] & LAPDm_MORE)
1386                  && !dl->rcv_buffer) {
1387                         LOGP(DLAPDM, LOGL_INFO, "message in single I frame\n");
1388                         /* send a DATA INDICATION to L3 */
1389                         msg->l3h = msg->l2h + 3;
1390                         msgb_pull_l2h(msg);
1391                         rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg);
1392                 } else {
1393                         /* create rcv_buffer */
1394                         if (!dl->rcv_buffer) {
1395                                 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1396                                         "frames (first message)\n");
1397                                 dl->rcv_buffer = msgb_alloc_headroom(200+56, 56,
1398                                                                 "LAPDm RX");
1399                                 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1400                         }
1401                         /* concat. rcv_buffer */
1402                         if (msgb_l3len(dl->rcv_buffer) + length > 200) {
1403                                 LOGP(DLAPDM, LOGL_NOTICE, "Received frame "
1404                                         "overflow!\n");
1405                         } else {
1406                                 memcpy(msgb_put(dl->rcv_buffer, length),
1407                                         msg->l2h + 3, length);
1408                         }
1409                         /* if the last segment was received */
1410                         if (!(msg->l2h[2] & LAPDm_MORE)) {
1411                                 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1412                                         "frames (next message)\n");
1413                                 rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx,
1414                                         dl->rcv_buffer);
1415                                 dl->rcv_buffer = NULL;
1416                         } else
1417                                 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1418                                         "frames (last message)\n");
1419                         msgb_free(msg);
1420
1421                 }
1422         } else
1423                 LOGP(DLAPDM, LOGL_INFO, "I frame ignored during own receiver "
1424                         "busy condition\n");
1425
1426         /* Check for P bit */
1427         if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1428                 /* 5.5.2.1 */
1429                 /* check if we are not in own receiver busy */
1430                 if (!dl->own_busy) {
1431                         LOGP(DLAPDM, LOGL_INFO, "we are not busy, send RR\n");
1432                         /* Send RR with F=1 */
1433                         rc = lapdm_send_rr(mctx, 1);
1434                 } else {
1435                         LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
1436                         /* Send RNR with F=1 */
1437                         rc = lapdm_send_rnr(mctx, 1);
1438                 }
1439         } else {
1440                 /* 5.5.2.2 */
1441                 /* check if we are not in own receiver busy */
1442                 if (!dl->own_busy) {
1443                         /* NOTE: V(R) is already set above */
1444                         rc = rslms_send_i(mctx);
1445                         if (rc) {
1446                                 LOGP(DLAPDM, LOGL_INFO, "we are not busy and "
1447                                         "have no pending data, send RR\n");
1448                                 /* Send RR with F=0 */
1449                                 return lapdm_send_rr(mctx, 0);
1450                         }
1451                 } else {
1452                         LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
1453                         /* Send RNR with F=0 */
1454                         rc = lapdm_send_rnr(mctx, 0);
1455                 }
1456         }
1457
1458         /* Send message, if possible due to acknowledged data */
1459         rslms_send_i(mctx);
1460
1461         return rc;
1462 }
1463
1464 /* Receive a LAPDm message from L1 */
1465 static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1466 {
1467         int rc;
1468
1469 #if 0
1470 printf("-> rx chan_nr 0x%x link_id 0x%x len %d data", mctx->chan_nr, mctx->link_id, msgb_l2len(msg));
1471 int i;
1472 for (i = 0; i < msgb_l2len(msg); i++)
1473         printf(" %02x", msg->l2h[i]);
1474 printf("\n");
1475 #endif
1476         /* G.2.3 EA bit set to "0" is not allowed in GSM */
1477         if (!LAPDm_ADDR_EA(mctx->addr)) {
1478                 LOGP(DLAPDM, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
1479                 msgb_free(msg);
1480                 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1481                 return -EINVAL;
1482         }
1483
1484         if (LAPDm_CTRL_is_U(mctx->ctrl))
1485                 rc = lapdm_rx_u(msg, mctx);
1486         else if (LAPDm_CTRL_is_S(mctx->ctrl))
1487                 rc = lapdm_rx_s(msg, mctx);
1488         else if (LAPDm_CTRL_is_I(mctx->ctrl))
1489                 rc = lapdm_rx_i(msg, mctx);
1490         else {
1491                 LOGP(DLAPDM, LOGL_NOTICE, "unknown LAPDm format\n");
1492                 msgb_free(msg);
1493                 rc = -EINVAL;
1494         }
1495         return rc;
1496 }
1497
1498 /* input into layer2 (from layer 1) */
1499 int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_dl *l1i)
1500 {
1501         uint8_t cbits = l1i->chan_nr >> 3;
1502         uint8_t sapi = l1i->link_id & 7;
1503         struct lapdm_msg_ctx mctx;
1504         int rc = 0;
1505
1506         /* when we reach here, we have a msgb with l2h pointing to the raw
1507          * 23byte mac block. The l1h has already been purged. */
1508
1509         mctx.dl = datalink_for_sapi(le, sapi);
1510         mctx.chan_nr = l1i->chan_nr;
1511         mctx.link_id = l1i->link_id;
1512         mctx.addr = mctx.ctrl = 0;
1513
1514         /* G.2.1 No action schall be taken on frames containing an unallocated
1515          * SAPI.
1516          */
1517         if (!mctx.dl) {
1518                 LOGP(DLAPDM, LOGL_NOTICE, "Received frame for unsupported "
1519                         "SAPI %d!\n", sapi);
1520                 return -EINVAL;
1521                 msgb_free(msg);
1522                 return -EIO;
1523         }
1524
1525         /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
1526         if (cbits == 0x10 || cbits == 0x12) {
1527                 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
1528                 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
1529                 mctx.n201 = N201_Bbis;
1530         } else {
1531                 if (mctx.link_id & 0x40) {
1532                         /* It was received from network on SACCH, thus
1533                          * lapdm_fmt must be B4 */
1534                         mctx.lapdm_fmt = LAPDm_FMT_B4;
1535                         mctx.n201 = N201_B4;
1536                         LOGP(DLAPDM, LOGL_INFO, "fmt=B4\n");
1537                         /* SACCH frames have a two-byte L1 header that
1538                          * OsmocomBB L1 doesn't strip */
1539                         mctx.tx_power_ind = msg->l2h[0] & 0x1f;
1540                         mctx.ta_ind = msg->l2h[1];
1541                         msgb_pull(msg, 2);
1542                         msg->l2h += 2;
1543                 } else {
1544                         mctx.lapdm_fmt = LAPDm_FMT_B;
1545                         LOGP(DLAPDM, LOGL_INFO, "fmt=B\n");
1546                         mctx.n201 = 23; // FIXME: select correct size by chan.
1547                 }
1548         }
1549
1550         switch (mctx.lapdm_fmt) {
1551         case LAPDm_FMT_A:
1552         case LAPDm_FMT_B:
1553         case LAPDm_FMT_B4:
1554                 mctx.addr = msg->l2h[0];
1555                 if (!(mctx.addr & 0x01)) {
1556                         LOGP(DLAPDM, LOGL_ERROR, "we don't support "
1557                                 "multibyte addresses (discarding)\n");
1558                         msgb_free(msg);
1559                         return -EINVAL;
1560                 }
1561                 mctx.ctrl = msg->l2h[1];
1562                 /* obtain SAPI from address field */
1563                 mctx.link_id |= LAPDm_ADDR_SAPI(mctx.addr);
1564                 rc = lapdm_ph_data_ind(msg, &mctx);
1565                 break;
1566         case LAPDm_FMT_Bter:
1567                 /* FIXME */
1568                 msgb_free(msg);
1569                 break;
1570         case LAPDm_FMT_Bbis:
1571                 /* directly pass up to layer3 */
1572                 LOGP(DLAPDM, LOGL_INFO, "fmt=Bbis UI\n");
1573                 msg->l3h = msg->l2h;
1574                 msgb_pull_l2h(msg);
1575                 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
1576                 break;
1577         default:
1578                 msgb_free(msg);
1579         }
1580
1581         return rc;
1582 }
1583
1584 /* L3 -> L2 / RSLMS -> LAPDm */
1585
1586 /* L3 requests establishment of data link */
1587 static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
1588 {
1589         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1590         uint8_t chan_nr = rllh->chan_nr;
1591         uint8_t link_id = rllh->link_id;
1592         uint8_t sapi = rllh->link_id & 7;
1593         struct tlv_parsed tv;
1594         uint8_t length;
1595         uint8_t n201 = 23; //FIXME
1596
1597         /* Set chan_nr and link_id for established connection */
1598         memset(&dl->mctx, 0, sizeof(dl->mctx));
1599         dl->mctx.dl = dl;
1600         dl->mctx.n201 = n201;
1601         dl->mctx.chan_nr = chan_nr;
1602         dl->mctx.link_id = link_id;
1603
1604         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1605         if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1606                 /* contention resolution establishment procedure */
1607                 if (sapi != 0) {
1608                         /* According to clause 6, the contention resolution
1609                          * procedure is only permitted with SAPI value 0 */
1610                         LOGP(DLAPDM, LOGL_ERROR, "SAPI != 0 but contention"
1611                                 "resolution (discarding)\n");
1612                         msgb_free(msg);
1613                         return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1614                 }
1615                 /* transmit a SABM command with the P bit set to "1". The SABM
1616                  * command shall contain the layer 3 message unit */
1617                 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1618                 LOGP(DLAPDM, LOGL_INFO, "perform establishment with content "
1619                         "(SAMB)\n");
1620         } else {
1621                 /* normal establishment procedure */
1622                 length = 0;
1623                 LOGP(DLAPDM, LOGL_INFO, "perform normal establishm. (SAMB)\n");
1624         }
1625
1626         /* check if the layer3 message length exceeds N201 */
1627         if (length + 3 > 21) { /* FIXME: do we know the channel N201? */
1628                 LOGP(DLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) "
1629                         "(discarding)\n", length + 3, 21);
1630                 msgb_free(msg);
1631                 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1632         }
1633
1634         /* Flush send-queue */
1635         /* Clear send-buffer */
1636         lapdm_dl_flush_send(dl);
1637
1638         /* Discard partly received L3 message */
1639         if (dl->rcv_buffer) {
1640                 msgb_free(dl->rcv_buffer);
1641                 dl->rcv_buffer = NULL;
1642         }
1643
1644         /* Remove RLL header from msgb */
1645         msgb_pull_l2h(msg);
1646
1647         /* Push LAPDm header on msgb */
1648         msg->l2h = msgb_push(msg, 3);
1649         msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1650         msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
1651         msg->l2h[2] = LAPDm_LEN(length);
1652         /* Transmit-buffer carries exactly one segment */
1653         memcpy(dl->tx_hist[0], msg->l2h, 3 + length);
1654         dl->tx_length[0] = 3 + length;
1655         
1656         /* Set states */
1657         dl->own_busy = dl->peer_busy = 0;
1658         dl->retrans_ctr = 0;
1659         lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT);
1660
1661         /* Tramsmit and start T200 */
1662         bsc_schedule_timer(&dl->t200, T200);
1663         return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1664 }
1665
1666 /* L3 requests transfer of unnumbered information */
1667 static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1668 {
1669         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1670         uint8_t chan_nr = rllh->chan_nr;
1671         uint8_t link_id = rllh->link_id;
1672         uint8_t sapi = link_id & 7;
1673         struct tlv_parsed tv;
1674         int length;
1675         uint8_t n201 = 23; //FIXME
1676
1677         /* check if the layer3 message length exceeds N201 */
1678
1679         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1680
1681         length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1682         /* check if the layer3 message length exceeds N201 */
1683         if (length + 3 > 18) { /* FIXME: do we know the channel N201? */
1684                 LOGP(DLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) "
1685                         "(discarding)\n", length + 3, 18);
1686                 msgb_free(msg);
1687                 return -EIO;
1688         }
1689
1690         LOGP(DLAPDM, LOGL_INFO, "sending unit data\n");
1691
1692         /* Remove RLL header from msgb */
1693         msgb_pull_l2h(msg);
1694
1695         /* Push LAPDm header on msgb */
1696         msg->l2h = msgb_push(msg, 3);
1697         msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1698         msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
1699         msg->l2h[2] = LAPDm_LEN(length);
1700         // FIXME: short L2 header support
1701
1702         /* Tramsmit */
1703         return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1704 }
1705
1706 /* L3 requests transfer of acknowledged information */
1707 static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1708 {
1709         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1710         struct tlv_parsed tv;
1711
1712         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1713         if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1714                 LOGP(DLAPDM, LOGL_ERROR, "data request without message error\n");
1715                 msgb_free(msg);
1716                 return -EINVAL;
1717         }
1718
1719         LOGP(DLAPDM, LOGL_INFO, "writing message to send-queue\n");
1720
1721         /* Remove the RSL/RLL header */
1722         msgb_pull_l2h(msg);
1723
1724         /* Write data into the send queue */
1725         msgb_enqueue(&dl->send_queue, msg);
1726
1727         /* Send message, if possible */
1728         rslms_send_i(&dl->mctx);
1729         return 0;
1730 }
1731
1732 /* Send next I frame from queued/buffered data */
1733 static int rslms_send_i(struct lapdm_msg_ctx *mctx)
1734 {
1735         struct lapdm_datalink *dl = mctx->dl;
1736         uint8_t chan_nr = mctx->chan_nr;
1737         uint8_t link_id = mctx->link_id;
1738         uint8_t sapi = link_id & 7;
1739         int k = k_sapi[sapi];
1740         struct msgb *msg;
1741         int length, left;
1742         int rc = -1; /* we sent nothing */
1743
1744         next_frame:
1745
1746         if (dl->peer_busy) {
1747                 LOGP(DLAPDM, LOGL_INFO, "peer busy, not sending\n");
1748                 return rc;
1749         }
1750
1751         if (dl->state == LAPDm_STATE_TIMER_RECOV) {
1752                 LOGP(DLAPDM, LOGL_INFO, "timer recovery, not sending\n");
1753                 return rc;
1754         }
1755
1756         /* If the send state variable V(S) is equal to V(A) plus k
1757          * (where k is the maximum number of outstanding I frames - see
1758          * subclause 5.8.4), the data link layer entity shall not transmit any
1759          * new I frames, but shall retransmit an I frame as a result
1760          * of the error recovery procedures as described in subclauses 5.5.4 and
1761          * 5.5.7. */
1762         if (dl->V_send == add_mod8(dl->V_ack, k)) {
1763                 LOGP(DLAPDM, LOGL_INFO, "k frames outstanding, not sending "
1764                         "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->V_send,
1765                         dl->V_ack);
1766                 return rc;
1767         }
1768
1769         /* if we have no tx_hist yet, we create it */
1770         if (!dl->tx_length[dl->V_send]) {
1771                 /* Get next message into send-buffer, if any */
1772                 if (!dl->send_buffer) {
1773                         next_message:
1774                         dl->send_out = 0;
1775                         dl->send_buffer = msgb_dequeue(&dl->send_queue);
1776                         /* No more data to be sent */
1777                         if (!dl->send_buffer)
1778                                 return rc;
1779                         LOGP(DLAPDM, LOGL_INFO, "get message from "
1780                                 "send-queue\n");
1781                 }
1782
1783                 /* How much is left in the send-buffer? */
1784                 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1785                 /* Segment, if data exceeds N201 */
1786                 length = left;
1787                 if (length > mctx->n201 - 3)
1788                         length = mctx->n201 - 3;
1789 printf("msg-len %d sent %d left %d N201 %d length %d first byte %02x\n", msgb_l3len(dl->send_buffer), dl->send_out, left, mctx->n201, length, dl->send_buffer->l3h[0]);
1790                 /* If message in send-buffer is completely sent */
1791                 if (left == 0) {
1792                         msgb_free(dl->send_buffer);
1793                         dl->send_buffer = NULL;
1794                         goto next_message;
1795                 }
1796
1797                 LOGP(DLAPDM, LOGL_INFO, "send I frame %sV(S)=%d\n",
1798                         (left > length) ? "segment " : "", dl->V_send);
1799
1800                 /* Create I frame (segment) and transmit-buffer content */
1801                 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
1802                 msg->l2h = msgb_put(msg, 3 + length);
1803                 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1804                 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
1805                 msg->l2h[2] = LAPDm_LEN(length);
1806                 if (left > length)
1807                         msg->l2h[2] |= LAPDm_MORE;
1808                 memcpy(msg->l2h + 3, dl->send_buffer->l3h + dl->send_out,
1809                         length);
1810                 memcpy(dl->tx_hist[dl->V_send], msg->l2h, 3 + length);
1811                 dl->tx_length[dl->V_send] = 3 + length;
1812                 /* Add length to track how much is already in the tx buffer */
1813                 dl->send_out += length;
1814         } else {
1815                 LOGP(DLAPDM, LOGL_INFO, "resend I frame from tx buffer "
1816                         "V(S)=%d\n", dl->V_send);
1817
1818                 /* Create I frame (segment) from tx_hist */
1819                 length = dl->tx_length[dl->V_send];
1820                 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
1821                 msg->l2h = msgb_put(msg, length);
1822                 memcpy(msg->l2h, dl->tx_hist[dl->V_send], length);
1823                 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
1824         }
1825
1826         /* The value of the send state variable V(S) shall be incremented by 1
1827          * at the end of the transmission of the I frame */
1828         dl->V_send = inc_mod8(dl->V_send);
1829
1830         /* If timer T200 is not running at the time right before transmitting a
1831          * frame, when the PH-READY-TO-SEND primitive is received from the
1832          * physical layer., it shall be set. */
1833         if (!bsc_timer_pending(&dl->t200))
1834                 bsc_schedule_timer(&dl->t200, T200);
1835
1836         tx_ph_data_enqueue(dl, msg, chan_nr, link_id, mctx->n201);
1837
1838         rc = 0; /* we sent something */
1839         goto next_frame;
1840 }
1841
1842 /* L3 requests suspension of data link */
1843 static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1844 {
1845         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1846         uint8_t sapi = rllh->link_id & 7;
1847
1848         if (sapi != 0) {
1849                 LOGP(DLAPDM, LOGL_ERROR, "SAPI != 0 while suspending\n");
1850                 msgb_free(msg);
1851                 return -EINVAL;
1852         }
1853
1854         LOGP(DLAPDM, LOGL_INFO, "perform suspension\n");
1855
1856         /* put back the send-buffer to the send-queue (first position) */
1857         if (dl->send_buffer) {
1858                 llist_add(&dl->send_buffer->list, &dl->send_queue);
1859                 dl->send_buffer = NULL;
1860         }
1861
1862         /* Clear transmit and send buffer, if any */
1863         lapdm_dl_flush_tx(dl);
1864         lapdm_dl_flush_send(dl);
1865
1866         msgb_free(msg);
1867
1868         return send_rll_simple(RSL_MT_SUSP_CONF, &dl->mctx);
1869 }
1870
1871 /* L3 requests resume of data link */
1872 static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1873 {
1874         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1875         uint8_t chan_nr = rllh->chan_nr;
1876         uint8_t link_id = rllh->link_id;
1877         uint8_t sapi = rllh->link_id & 7;
1878         struct tlv_parsed tv;
1879         uint8_t length;
1880         uint8_t n201 = 23; //FIXME
1881
1882         /* Set chan_nr and link_id for established connection */
1883         memset(&dl->mctx, 0, sizeof(dl->mctx));
1884         dl->mctx.dl = dl;
1885         dl->mctx.n201 = n201;
1886         dl->mctx.chan_nr = chan_nr;
1887         dl->mctx.link_id = link_id;
1888
1889         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1890         if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1891                 LOGP(DLAPDM, LOGL_ERROR, "resume without message error\n");
1892                 msgb_free(msg);
1893                 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1894         }
1895         length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1896
1897         LOGP(DLAPDM, LOGL_INFO, "perform re-establishment (SAMB)\n");
1898         
1899         /* Replace message in the send-buffer (reconnect) */
1900         if (dl->send_buffer)
1901                 msgb_free(dl->send_buffer);
1902         dl->send_out = 0;
1903         dl->send_buffer = msg;
1904
1905         /* Discard partly received L3 message */
1906         if (dl->rcv_buffer) {
1907                 msgb_free(dl->rcv_buffer);
1908                 dl->rcv_buffer = NULL;
1909         }
1910
1911         /* Create new msgb (old one is now free) */
1912         msg = msgb_alloc_headroom(23+10, 10, "LAPDm SABM");
1913         msg->l2h = msgb_put(msg, 3);
1914         msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1915         msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
1916         msg->l2h[2] = LAPDm_LEN(0);
1917         /* Transmit-buffer carries exactly one segment */
1918         memcpy(dl->tx_hist[0], msg->l2h, 3);
1919         dl->tx_length[0] = 3;
1920
1921         /* Set states */
1922         dl->own_busy = dl->peer_busy = 0;
1923         dl->retrans_ctr = 0;
1924         lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT);
1925
1926         /* Tramsmit and start T200 */
1927         bsc_schedule_timer(&dl->t200, T200);
1928         return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1929 }
1930
1931 /* L3 requests release of data link */
1932 static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1933 {
1934         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1935         uint8_t chan_nr = rllh->chan_nr;
1936         uint8_t link_id = rllh->link_id;
1937         uint8_t sapi = rllh->link_id & 7;
1938         uint8_t mode = 0;
1939
1940         /* get release mode */
1941         if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1942                 mode = rllh->data[1] & 1;
1943
1944         /* local release */
1945         if (mode) {
1946                 LOGP(DLAPDM, LOGL_INFO, "perform local release\n");
1947                 msgb_free(msg);
1948                 /* reset Timer T200 */
1949                 bsc_del_timer(&dl->t200);
1950                 /* enter idle state */
1951                 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1952                 /* send notification to L3 */
1953                 return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
1954         }
1955
1956         /* in case we are already disconnecting */
1957         if (dl->state == LAPDm_STATE_DISC_SENT)
1958                 return -EBUSY;
1959
1960         LOGP(DLAPDM, LOGL_INFO, "perform normal release (DISC)\n");
1961
1962         /* Pull rllh */
1963         msgb_pull(msg, msg->tail - msg->l2h);
1964
1965         /* Push LAPDm header on msgb */
1966         msg->l2h = msgb_push(msg, 3);
1967         msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1968         msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DISC, 1);
1969         msg->l2h[2] = LAPDm_LEN(0);
1970         /* Transmit-buffer carries exactly one segment */
1971         memcpy(dl->tx_hist[0], msg->l2h, 3);
1972         dl->tx_length[0] = 3;
1973         
1974         /* Set states */
1975         dl->own_busy = dl->peer_busy = 0;
1976         dl->retrans_ctr = 0;
1977         lapdm_dl_newstate(dl, LAPDm_STATE_DISC_SENT);
1978
1979         /* Tramsmit and start T200 */
1980         bsc_schedule_timer(&dl->t200, T200);
1981         return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, dl->mctx.n201);
1982 }
1983
1984 /* L3 requests release in idle state */
1985 static int rslms_rx_rll_rel_req_idle(struct msgb *msg, struct lapdm_datalink *dl)
1986 {
1987         msgb_free(msg);
1988
1989         /* send notification to L3 */
1990         return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
1991 }
1992
1993 /* L3 requests channel in idle state */
1994 static int rslms_rx_chan_rqd(struct osmocom_ms *ms, struct msgb *msg)
1995 {
1996         struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1997         int rc;
1998
1999         if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
2000                 LOGP(DRSL, LOGL_ERROR, "Message too short for CHAN RQD!\n");
2001                 return -EINVAL;
2002         }
2003         if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
2004                 LOGP(DRSL, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
2005                 return -EINVAL;
2006         }
2007         if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
2008                 LOGP(DRSL, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
2009                 return -EINVAL;
2010         }
2011         if (cch->data[6] != RSL_IE_MS_POWER) {
2012                 LOGP(DRSL, LOGL_ERROR, "Missing MS POWER IE\n");
2013                 return -EINVAL;
2014         }
2015
2016         /* TA = 0 - delay */
2017         rc = l1ctl_tx_param_req(ms, 0 - cch->data[5], cch->data[7]);
2018
2019         rc = l1ctl_tx_rach_req(ms, cch->data[1], cch->data[2], cch->data[3]);
2020
2021         msgb_free(msg);
2022
2023         return rc;
2024 }
2025
2026 /* L1 confirms channel request */
2027 int l2_ph_chan_conf(struct msgb *msg, struct osmocom_ms *ms,
2028                         struct l1ctl_info_dl *dl)
2029 {
2030         struct abis_rsl_cchan_hdr *ch;
2031         struct gsm_time tm;
2032         struct gsm48_req_ref *ref;
2033
2034         gsm_fn2gsmtime(&tm, htonl(dl->frame_nr));
2035
2036         msgb_pull_l2h(msg);
2037         msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
2038         ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
2039         rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
2040         ch->chan_nr = RSL_CHAN_RACH;
2041         ch->data[0] = RSL_IE_REQ_REFERENCE;
2042         ref = (struct gsm48_req_ref *) (ch->data + 1);
2043         ref->t1 = tm.t1;
2044         ref->t2 = tm.t2;
2045         ref->t3_low = tm.t3 & 0x3;
2046         ref->t3_high = tm.t3 >> 3;
2047         
2048         return rslms_sendmsg(msg, ms);
2049 }
2050
2051 /* Names for Radio Link Layer Management */
2052 static const struct value_string rsl_msg_names[] = {
2053         { RSL_MT_DATA_REQ,              "RSL_MT_DATA_REQ" },
2054         { RSL_MT_DATA_IND,              "RSL_MT_DATA_IND" },
2055         { RSL_MT_ERROR_IND,             "RSL_MT_ERROR_IND" },
2056         { RSL_MT_EST_REQ,               "RSL_MT_EST_REQ" },
2057         { RSL_MT_EST_CONF,              "RSL_MT_EST_CONF" },
2058         { RSL_MT_EST_IND,               "RSL_MT_EST_IND" },
2059         { RSL_MT_EST_IND,               "RSL_MT_REL_REQ" },
2060         { RSL_MT_REL_REQ,               "RSL_MT_REL_REQ" },
2061         { RSL_MT_REL_CONF,              "RSL_MT_REL_CONF" },
2062         { RSL_MT_REL_IND,               "RSL_MT_REL_IND" },
2063         { RSL_MT_UNIT_DATA_REQ,         "RSL_MT_UNIT_DATA_REQ" },
2064         { RSL_MT_UNIT_DATA_IND,         "RSL_MT_UNIT_DATA_IND" },
2065         { RSL_MT_SUSP_REQ,              "RSL_MT_SUSP_REQ" },
2066         { RSL_MT_SUSP_CONF,             "RSL_MT_SUSP_CONF" },
2067         { RSL_MT_RES_REQ,               "RSL_MT_RES_REQ" },
2068         { RSL_MT_RECON_REQ,             "RSL_MT_RECON_REQ" },
2069         { RSL_MT_CHAN_RQD,              "RSL_MT_CHAN_RQD" },
2070         { RSL_MT_CHAN_CONF,             "RSL_MT_CHAN_CONF" },
2071         { 0,                            NULL }
2072 };
2073
2074 const char *get_rsl_name(int value)
2075 {
2076         return get_value_string(rsl_msg_names, value);
2077 }
2078
2079 const char *lapdm_state_names[] = {
2080         "LAPDm_STATE_NULL",
2081         "LAPDm_STATE_IDLE",
2082         "LAPDm_STATE_SABM_SENT",
2083         "LAPDm_STATE_MF_EST",
2084         "LAPDm_STATE_TIMER_RECOV",
2085         "LAPDm_STATE_DISC_SENT",
2086 };
2087
2088 /* statefull handling for RSLms RLL messages from L3 */
2089 static struct l2downstate {
2090         uint32_t        states;
2091         int             type;
2092         int             (*rout) (struct msgb *msg, struct lapdm_datalink *dl);
2093 } l2downstatelist[] = {
2094         /* create and send UI command */
2095         {ALL_STATES,
2096          RSL_MT_UNIT_DATA_REQ, rslms_rx_rll_udata_req},
2097
2098         /* create and send SABM command */
2099         {SBIT(LAPDm_STATE_IDLE),
2100          RSL_MT_EST_REQ, rslms_rx_rll_est_req},
2101
2102         /* create and send I command */
2103         {SBIT(LAPDm_STATE_MF_EST) |
2104          SBIT(LAPDm_STATE_TIMER_RECOV),
2105          RSL_MT_DATA_REQ, rslms_rx_rll_data_req},
2106
2107         /* suspend datalink */
2108         {SBIT(LAPDm_STATE_MF_EST) |
2109          SBIT(LAPDm_STATE_TIMER_RECOV),
2110          RSL_MT_SUSP_REQ, rslms_rx_rll_susp_req},
2111
2112         /* create and send SABM command (resume) */
2113         {SBIT(LAPDm_STATE_MF_EST) |
2114          SBIT(LAPDm_STATE_TIMER_RECOV),
2115          RSL_MT_RES_REQ, rslms_rx_rll_res_req},
2116
2117         /* create and send SABM command (reconnect) */
2118         {SBIT(LAPDm_STATE_MF_EST) |
2119          SBIT(LAPDm_STATE_TIMER_RECOV),
2120          RSL_MT_RECON_REQ, rslms_rx_rll_res_req},
2121
2122         /* create and send DISC command */
2123         {SBIT(LAPDm_STATE_SABM_SENT) |
2124          SBIT(LAPDm_STATE_MF_EST) |
2125          SBIT(LAPDm_STATE_TIMER_RECOV) |
2126          SBIT(LAPDm_STATE_DISC_SENT),
2127          RSL_MT_REL_REQ, rslms_rx_rll_rel_req},
2128
2129         /* release in idle state */
2130         {SBIT(LAPDm_STATE_IDLE),
2131          RSL_MT_REL_REQ, rslms_rx_rll_rel_req_idle},
2132 };
2133
2134 #define L2DOWNSLLEN \
2135         (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2136
2137 /* incoming RSLms RLL message from L3 */
2138 static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
2139 {
2140         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2141         int msg_type = rllh->c.msg_type;
2142         uint8_t sapi = rllh->link_id & 7;
2143         struct lapdm_entity *le;
2144         struct lapdm_datalink *dl;
2145         int i, supported = 0;
2146         int rc = 0;
2147
2148         if (msgb_l2len(msg) < sizeof(*rllh)) {
2149                 LOGP(DRSL, LOGL_ERROR, "Message too short for RLL hdr!\n");
2150                 return -EINVAL;
2151         }
2152
2153         if (rllh->link_id & 0x40)
2154                 le = &ms->l2_entity.lapdm_acch;
2155         else
2156                 le = &ms->l2_entity.lapdm_dcch;
2157
2158         /* G.2.1 No action schall be taken on frames containing an unallocated
2159          * SAPI.
2160          */
2161         dl = datalink_for_sapi(le, sapi);
2162         if (!dl) {
2163                 LOGP(DRSL, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
2164                 return -EINVAL;
2165         }
2166
2167         LOGP(DRSL, LOGL_INFO, "(ms %s) RLL Message '%s' received in state %s\n",
2168                 ms->name, get_rsl_name(msg_type), lapdm_state_names[dl->state]);
2169
2170         /* find function for current state and message */
2171         for (i = 0; i < L2DOWNSLLEN; i++) {
2172                 if (msg_type == l2downstatelist[i].type)
2173                         supported = 1;
2174                 if ((msg_type == l2downstatelist[i].type)
2175                  && ((1 << dl->state) & l2downstatelist[i].states))
2176                         break;
2177         }
2178         if (!supported) {
2179                 LOGP(DRSL, LOGL_NOTICE, "Message unsupported.\n");
2180                 msgb_free(msg);
2181                 return 0;
2182         }
2183         if (i == L2DOWNSLLEN) {
2184                 LOGP(DRSL, LOGL_NOTICE, "Message unhandled at this state.\n");
2185                 msgb_free(msg);
2186                 return 0;
2187         }
2188
2189         rc = l2downstatelist[i].rout(msg, dl);
2190
2191         return rc;
2192 }
2193
2194 /* incoming RSLms COMMON CHANNEL message from L3 */
2195 static int rslms_rx_com_chan(struct msgb *msg, struct osmocom_ms *ms)
2196 {
2197         struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
2198         int msg_type = cch->c.msg_type;
2199         int rc = 0;
2200
2201         if (msgb_l2len(msg) < sizeof(*cch)) {
2202                 LOGP(DRSL, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
2203                 return -EINVAL;
2204         }
2205
2206         switch (msg_type) {
2207         case RSL_MT_CHAN_RQD:
2208                 /* create and send RACH request */
2209                 rc = rslms_rx_chan_rqd(ms, msg);
2210                 break;
2211         default:
2212                 LOGP(DRSL, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
2213                         msg_type);
2214                 msgb_free(msg);
2215                 return 0;
2216         }
2217
2218         return rc;
2219 }
2220
2221 /* input into layer2 (from layer 3) */
2222 int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms)
2223 {
2224         struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
2225         int rc = 0;
2226
2227         if (msgb_l2len(msg) < sizeof(*rslh)) {
2228                 LOGP(DRSL, LOGL_ERROR, "Message too short RSL hdr!\n");
2229                 return -EINVAL;
2230         }
2231
2232         switch (rslh->msg_discr & 0xfe) {
2233         case ABIS_RSL_MDISC_RLL:
2234                 rc = rslms_rx_rll(msg, ms);
2235                 break;
2236         case ABIS_RSL_MDISC_COM_CHAN:
2237                 rc = rslms_rx_com_chan(msg, ms);
2238                 break;
2239         default:
2240                 LOGP(DRSL, LOGL_ERROR, "unknown RSLms message "
2241                         "discriminator 0x%02x", rslh->msg_discr);
2242                 msgb_free(msg);
2243                 return -EINVAL;
2244         }
2245
2246         return rc;
2247 }
2248
2249 /* input function that L2 calls when sending messages up to L3 */
2250 int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms)
2251 {
2252         if (!ms->l2_entity.msg_handler) {
2253                 msgb_free(msg);
2254                 return -EIO;
2255         }
2256
2257         /* call the layer2 message handler that is registered */
2258         return ms->l2_entity.msg_handler(msg, ms);
2259 }
2260
2261 /* register message handler for messages that are sent from L2->L3 */
2262 int osmol2_register_handler(struct osmocom_ms *ms, osmol2_cb_t cb)
2263 {
2264         ms->l2_entity.msg_handler = cb;
2265
2266         return 0;
2267 }