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