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