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