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