1 /* GSM LAPDm (TS 04.06) implementation */
3 /* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
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.
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.
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.
24 /* Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue
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.
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.
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.
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.
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.
57 #include <arpa/inet.h>
59 #include <osmocom/core/logging.h>
60 #include <osmocom/core/timer.h>
61 #include <osmocom/core/msgb.h>
62 #include <osmocom/gsm/tlv.h>
63 #include <osmocom/core/utils.h>
64 #include <osmocom/gsm/rsl.h>
65 #include <osmocom/gsm/prim.h>
66 #include <osmocom/gsm/gsm_utils.h>
67 #include <osmocom/gsm/protocol/gsm_04_08.h>
68 #include <osmocom/gsm/protocol/gsm_08_58.h>
70 #include <osmocom/bb/common/lapdm.h>
71 #include <osmocom/bb/common/logging.h>
73 /* TS 04.06 Figure 4 / Section 3.2 */
74 #define LAPDm_LPD_NORMAL 0
75 #define LAPDm_LPD_SMSCB 1
76 #define LAPDm_SAPI_NORMAL 0
77 #define LAPDm_SAPI_SMS 3
78 #define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
80 #define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
81 #define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
82 #define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
84 /* TS 04.06 Table 3 / Section 3.4.3 */
85 #define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
86 #define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
87 #define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
89 #define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
90 #define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
91 #define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
93 #define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
94 #define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
96 #define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
98 #define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
99 #define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
101 /* TS 04.06 Table 4 / Section 3.8.1 */
102 #define LAPDm_U_SABM 0x7
103 #define LAPDm_U_DM 0x3
104 #define LAPDm_U_UI 0x0
105 #define LAPDm_U_DISC 0x8
106 #define LAPDm_U_UA 0xC
108 #define LAPDm_S_RR 0x0
109 #define LAPDm_S_RNR 0x1
110 #define LAPDm_S_REJ 0x2
112 #define LAPDm_LEN(len) ((len << 2) | 0x1)
113 #define LAPDm_MORE 0x2
115 /* TS 04.06 Section 5.8.3 */
116 #define N201_AB_SACCH 18
117 #define N201_AB_SDCCH 20
118 #define N201_AB_FACCH 20
120 #define N201_Bter_SACCH 21
121 #define N201_Bter_SDCCH 23
122 #define N201_Bter_FACCH 23
125 /* 5.8.2.1 N200 during establish and release */
126 #define N200_EST_REL 5
127 /* 5.8.2.1 N200 during timer recovery state */
128 #define N200_TR_SACCH 5
129 #define N200_TR_SDCCH 23
130 #define N200_TR_FACCH_FR 34
131 #define N200_TR_EFACCH_FR 48
132 #define N200_TR_FACCH_HR 29
133 /* FIXME: this depends on chan type */
134 #define N200 N200_TR_SACCH
136 #define CR_MS2BS_CMD 0
137 #define CR_MS2BS_RESP 1
138 #define CR_BS2MS_CMD 1
139 #define CR_BS2MS_RESP 0
141 /* Set T200 to 1 Second (OpenBTS uses 900ms) */
144 /* k value for each SAPI */
145 static uint8_t k_sapi[] = {1, 1, 1, 1, 1, 1, 1, 1};
155 static void lapdm_t200_cb(void *data);
156 static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line);
158 /* UTILITY FUNCTIONS */
160 static inline uint8_t inc_mod8(uint8_t x)
165 static inline uint8_t add_mod8(uint8_t x, uint8_t y)
170 static inline uint8_t sub_mod8(uint8_t x, uint8_t y)
172 return (x - y) & 7; /* handle negative results correctly */
175 static void lapdm_dl_init(struct lapdm_datalink *dl,
176 struct lapdm_entity *entity)
178 memset(dl, 0, sizeof(*dl));
179 INIT_LLIST_HEAD(&dl->send_queue);
180 INIT_LLIST_HEAD(&dl->tx_queue);
181 dl->state = LAPDm_STATE_IDLE;
183 dl->t200.cb = &lapdm_t200_cb;
187 void lapdm_entity_init(struct lapdm_entity *le)
191 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
192 lapdm_dl_init(&le->datalink[i], le);
195 void lapdm_channel_init(struct lapdm_channel *lc)
197 lapdm_entity_init(&lc->lapdm_acch);
198 lapdm_entity_init(&lc->lapdm_dcch);
202 static void lapdm_dl_flush_send(struct lapdm_datalink *dl)
206 /* Flush send-queue */
207 while ((msg = msgb_dequeue(&dl->send_queue)))
210 /* Clear send-buffer */
211 if (dl->send_buffer) {
212 msgb_free(dl->send_buffer);
213 dl->send_buffer = NULL;
217 static void lapdm_dl_flush_tx(struct lapdm_datalink *dl)
222 while ((msg = msgb_dequeue(&dl->tx_queue)))
224 for (i = 0; i < 8; i++)
225 dl->tx_length[i] = 0;
228 void lapdm_entity_exit(struct lapdm_entity *le)
231 struct lapdm_datalink *dl;
233 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
234 dl = &le->datalink[i];
235 lapdm_dl_flush_tx(dl);
236 lapdm_dl_flush_send(dl);
238 msgb_free(dl->rcv_buffer);
242 void lapdm_channel_exit(struct lapdm_channel *lc)
244 lapdm_entity_exit(&lc->lapdm_acch);
245 lapdm_entity_exit(&lc->lapdm_dcch);
248 static void lapdm_dl_newstate(struct lapdm_datalink *dl, uint32_t state)
250 LOGP(DLAPDM, LOGL_INFO, "new state %s -> %s\n",
251 lapdm_state_names[dl->state], lapdm_state_names[state]);
256 static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
259 case LAPDm_SAPI_NORMAL:
260 return &le->datalink[0];
262 return &le->datalink[1];
268 /* remove the L2 header from a MSGB */
269 static inline unsigned char *msgb_pull_l2h(struct msgb *msg)
271 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h);
276 /* Append padding (if required) */
277 static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
279 int pad_len = n201 - msgb_l2len(msg);
283 LOGP(DLAPDM, LOGL_ERROR,
284 "cannot pad message that is already too big!\n");
288 data = msgb_put(msg, pad_len);
289 memset(data, 0x2B, pad_len);
292 /* input function that L2 calls when sending messages up to L3 */
293 static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
300 /* call the layer2 message handler that is registered */
301 return le->l3_cb(msg, le, le->l3_ctx);
304 /* write a frame into the tx queue */
305 static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
306 uint8_t chan_nr, uint8_t link_id, uint8_t n201)
308 struct lapdm_entity *le = dl->entity;
309 struct osmo_phsap_prim pp;
311 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
312 PRIM_OP_REQUEST, msg);
313 pp.u.data.chan_nr = chan_nr;
314 pp.u.data.link_id = link_id;
316 /* if there is a pending message, queue it */
317 if (le->tx_pending) {
318 *msgb_push(msg, 1) = n201;
319 *msgb_push(msg, 1) = link_id;
320 *msgb_push(msg, 1) = chan_nr;
321 msgb_enqueue(&dl->tx_queue, msg);
325 /* send the frame now */
326 le->tx_pending = 0; /* disabled flow control */
327 lapdm_pad_msgb(msg, n201);
329 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
332 /* get next frame from the tx queue. because the ms has multiple datalinks,
333 * each datalink's queue is read round-robin.
335 static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
337 struct lapdm_datalink *dl;
338 int last = le->last_tx_dequeue;
339 int i = last, n = ARRAY_SIZE(le->datalink);
340 struct osmo_phsap_prim pp;
343 /* we may send again */
346 /* free confirm message */
350 /* round-robin dequeue */
354 dl = &le->datalink[i];
355 if ((msg = msgb_dequeue(&dl->tx_queue)))
359 /* no message in all queues */
363 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
364 PRIM_OP_REQUEST, msg);
366 /* Pull chan_nr and link_id */
367 pp.u.data.chan_nr = *msg->data;
369 pp.u.data.link_id = *msg->data;
374 /* Set last dequeue position */
375 le->last_tx_dequeue = i;
377 /* Pad the frame, we can transmit now */
379 lapdm_pad_msgb(msg, n201);
381 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
384 /* Create RSLms various RSLms messages */
385 static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
388 /* Add the RSL + RLL header */
389 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
391 /* send off the RSLms message to L3 */
392 return rslms_sendmsg(msg, mctx->dl->entity);
395 /* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
396 static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
398 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
399 struct abis_rsl_rll_hdr *rllh;
401 /* Add the RSL + RLL header */
402 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
403 msgb_push(msg, 2 + 2);
404 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
406 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
408 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
409 rllh->data[1] = mctx->ta_ind;
411 rllh->data[2] = RSL_IE_MS_POWER;
412 rllh->data[3] = mctx->tx_power_ind;
414 return rslms_sendmsg(msg, mctx->dl->entity);
417 static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
421 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
423 /* send off the RSLms message to L3 */
424 return rslms_sendmsg(msg, mctx->dl->entity);
427 static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
431 LOGP(DLAPDM, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
432 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
433 msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr));
434 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
435 return rslms_sendmsg(msg, mctx->dl->entity);
438 static int check_length_ind(struct lapdm_msg_ctx *mctx, uint8_t length_ind)
440 if (!(length_ind & 0x01)) {
441 /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION
442 * primitive with cause "frame not implemented" is sent to the
443 * mobile management entity. */
444 LOGP(DLAPDM, LOGL_NOTICE,
445 "we don't support multi-octet length\n");
446 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
452 static int lapdm_send_resend(struct lapdm_datalink *dl)
454 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm resend");
457 /* Resend SABM/DISC from tx_hist */
458 length = dl->tx_length[0];
459 msg->l2h = msgb_put(msg, length);
460 memcpy(msg->l2h, dl->tx_hist[dl->V_send], length);
462 return tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, dl->mctx.link_id,
466 static int lapdm_send_ua(struct lapdm_msg_ctx *mctx, uint8_t len, uint8_t *data)
468 uint8_t sapi = mctx->link_id & 7;
469 uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl);
470 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm UA");
471 msg->l2h = msgb_put(msg, 3 + len);
473 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
474 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UA, f_bit);
475 msg->l2h[2] = LAPDm_LEN(len);
477 memcpy(msg->l2h + 3, data, len);
479 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
483 static int lapdm_send_dm(struct lapdm_msg_ctx *mctx)
485 uint8_t sapi = mctx->link_id & 7;
486 uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl);
487 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm DM");
488 msg->l2h = msgb_put(msg, 3);
490 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
491 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DM, f_bit);
494 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
498 static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
500 uint8_t sapi = mctx->link_id & 7;
501 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RR");
502 msg->l2h = msgb_put(msg, 3);
504 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
505 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit);
506 msg->l2h[2] = LAPDm_LEN(0);
508 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
512 static int lapdm_send_rnr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
514 uint8_t sapi = mctx->link_id & 7;
515 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RNR");
516 msg->l2h = msgb_put(msg, 3);
518 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
519 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RNR, f_bit);
520 msg->l2h[2] = LAPDm_LEN(0);
522 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
526 static int lapdm_send_rej(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
528 uint8_t sapi = mctx->link_id & 7;
529 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm REJ");
530 msg->l2h = msgb_put(msg, 3);
532 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
533 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_REJ, f_bit);
534 msg->l2h[2] = LAPDm_LEN(0);
536 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
540 /* Timer callback on T200 expiry */
541 static void lapdm_t200_cb(void *data)
543 struct lapdm_datalink *dl = data;
545 LOGP(DLAPDM, LOGL_INFO, "lapdm_t200_cb(%p) state=%u\n", dl, dl->state);
548 case LAPDm_STATE_SABM_SENT:
550 if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) {
551 /* send RELEASE INDICATION to L3 */
552 send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
553 /* send MDL ERROR INIDCATION to L3 */
554 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
555 /* flush tx buffers */
556 lapdm_dl_flush_tx(dl);
557 /* go back to idle state */
558 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
559 /* NOTE: we must not change any other states or buffers
560 * and queues, since we may reconnect after handover
561 * failure. the buffered messages is replaced there */
564 /* retransmit SABM command */
565 lapdm_send_resend(dl);
566 /* increment re-transmission counter */
568 /* restart T200 (PH-READY-TO-SEND) */
569 osmo_timer_schedule(&dl->t200, T200);
571 case LAPDm_STATE_DISC_SENT:
573 if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) {
574 /* send RELEASE INDICATION to L3 */
575 send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
576 /* send MDL ERROR INIDCATION to L3 */
577 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
579 lapdm_dl_flush_tx(dl);
580 lapdm_dl_flush_send(dl);
581 /* go back to idle state */
582 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
583 /* NOTE: we must not change any other states or buffers
584 * and queues, since we may reconnect after handover
585 * failure. the buffered messages is replaced there */
588 /* retransmit DISC command */
589 lapdm_send_resend(dl);
590 /* increment re-transmission counter */
592 /* restart T200 (PH-READY-TO-SEND) */
593 osmo_timer_schedule(&dl->t200, T200);
595 case LAPDm_STATE_MF_EST:
598 lapdm_dl_newstate(dl, LAPDm_STATE_TIMER_RECOV);
600 case LAPDm_STATE_TIMER_RECOV:
602 if (dl->retrans_ctr < N200) {
603 /* retransmit I frame (V_s-1) with P=1, if any */
604 if (dl->tx_length[sub_mod8(dl->V_send, 1)]) {
608 LOGP(DLAPDM, LOGL_INFO, "retransmit last frame "
609 "V(S)=%d\n", sub_mod8(dl->V_send, 1));
610 /* Create I frame (segment) from tx_hist */
611 length = dl->tx_length[sub_mod8(dl->V_send, 1)];
612 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
613 msg->l2h = msgb_put(msg, length);
615 dl->tx_hist[sub_mod8(dl->V_send, 1)],
617 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv,
618 sub_mod8(dl->V_send, 1), 1); /* P=1 */
619 tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr,
620 dl->mctx.link_id, dl->mctx.n201);
622 /* OR send appropriate supervision frame with P=1 */
623 if (!dl->own_busy && !dl->seq_err_cond) {
624 lapdm_send_rr(&dl->mctx, 1);
625 /* NOTE: In case of sequence error
626 * condition, the REJ frame has been
627 * transmitted when entering the
628 * condition, so it has not be done
631 } else if (dl->own_busy) {
632 lapdm_send_rnr(&dl->mctx, 1);
634 LOGP(DLAPDM, LOGL_INFO, "unhandled, "
638 /* restart T200 (PH-READY-TO-SEND) */
639 osmo_timer_schedule(&dl->t200, T200);
641 /* send MDL ERROR INIDCATION to L3 */
642 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
646 LOGP(DLAPDM, LOGL_INFO, "T200 expired in unexpected "
647 "dl->state %u\n", dl->state);
651 /* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
652 static void lapdm_acknowledge(struct lapdm_msg_ctx *mctx)
654 struct lapdm_datalink *dl = mctx->dl;
655 uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl);
656 int s = 0, rej = 0, t200_reset = 0;
659 /* supervisory frame ? */
660 if (LAPDm_CTRL_is_S(mctx->ctrl))
663 if (s && LAPDm_CTRL_S_BITS(mctx->ctrl) == LAPDm_S_REJ)
666 /* Flush all transmit buffers of acknowledged frames */
667 for (i = dl->V_ack; i != nr; i = inc_mod8(i)) {
668 if (dl->tx_length[i]) {
669 dl->tx_length[i] = 0;
670 LOGP(DLAPDM, LOGL_INFO, "ack frame %d\n", i);
674 if (dl->state != LAPDm_STATE_TIMER_RECOV) {
675 /* When not in the timer recovery condition, the data
676 * link layer entity shall reset the timer T200 on
677 * receipt of a valid I frame with N(R) higher than V(A),
678 * or an REJ with an N(R) equal to V(A). */
679 if ((!rej && nr != dl->V_ack)
680 || (rej && nr == dl->V_ack)) {
681 LOGP(DLAPDM, LOGL_INFO, "reset t200\n");
683 osmo_timer_del(&dl->t200);
684 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
686 /* 5.7.4: N(R) sequence error
687 * N(R) is called valid, if and only if
688 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
690 if (sub_mod8(nr, dl->V_ack) > sub_mod8(dl->V_send, dl->V_ack)) {
691 LOGP(DLAPDM, LOGL_NOTICE, "N(R) sequence error\n");
692 rsl_rll_error(RLL_CAUSE_SEQ_ERR, mctx);
696 /* V(A) shall be set to the value of N(R) */
699 /* If T200 has been reset by the receipt of an I, RR or RNR frame,
700 * and if there are outstanding I frames, restart T200 */
701 if (t200_reset && !rej) {
702 if (dl->tx_length[dl->V_send - 1]) {
703 LOGP(DLAPDM, LOGL_INFO, "start T200, due to unacked I "
705 osmo_timer_schedule(&dl->t200, T200);
712 /* Receive a LAPDm U (Unnumbered) message from L1 */
713 static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx)
715 struct lapdm_datalink *dl = mctx->dl;
720 switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) {
722 rsl_msg = RSL_MT_EST_IND;
724 LOGP(DLAPDM, LOGL_INFO, "SABM received\n");
726 dl->seq_err_cond = 0;
727 /* G.2.2 Wrong value of the C/R bit */
728 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
729 LOGP(DLAPDM, LOGL_NOTICE, "SABM response error\n");
731 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
735 length = msg->l2h[2] >> 2;
736 /* G.4.5 If SABM is received with L>N201 or with M bit
737 * set, AN MDL-ERROR-INDICATION is sent to MM.
739 if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) {
740 LOGP(DLAPDM, LOGL_NOTICE, "SABM too large error\n");
742 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
746 /* Must be Format B */
747 rc = check_length_ind(mctx, msg->l2h[2]);
753 case LAPDm_STATE_IDLE:
754 /* Set chan_nr and link_id for established connection */
755 memset(&dl->mctx, 0, sizeof(dl->mctx));
757 dl->mctx.chan_nr = mctx->chan_nr;
758 dl->mctx.link_id = mctx->link_id;
760 case LAPDm_STATE_MF_EST:
762 rsl_msg = RSL_MT_EST_CONF;
765 LOGP(DLAPDM, LOGL_INFO, "SABM command, multiple "
766 "frame established state\n");
767 /* check for contention resoultion */
768 if (dl->tx_hist[0][2] >> 2) {
769 LOGP(DLAPDM, LOGL_NOTICE, "SABM not allowed "
770 "during contention resolution\n");
771 rsl_rll_error(RLL_CAUSE_SABM_INFO_NOTALL, mctx);
775 case LAPDm_STATE_DISC_SENT:
776 /* 5.4.6.2 send DM with F=P */
778 /* reset Timer T200 */
779 osmo_timer_del(&dl->t200);
781 return send_rll_simple(RSL_MT_REL_CONF, mctx);
783 lapdm_send_ua(mctx, length, msg->l2h + 3);
787 /* send UA response */
788 lapdm_send_ua(mctx, length, msg->l2h + 3);
789 /* set Vs, Vr and Va to 0 */
790 dl->V_send = dl->V_recv = dl->V_ack = 0;
792 dl->tx_length[0] = 0;
793 /* enter multiple-frame-established state */
794 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
795 /* send notification to L3 */
797 /* 5.4.1.2 Normal establishment procedures */
798 rc = send_rll_simple(rsl_msg, mctx);
801 /* 5.4.1.4 Contention resolution establishment */
802 msg->l3h = msg->l2h + 3;
804 rc = send_rslms_rll_l3(rsl_msg, mctx, msg);
808 LOGP(DLAPDM, LOGL_INFO, "DM received\n");
809 /* G.2.2 Wrong value of the C/R bit */
810 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
811 LOGP(DLAPDM, LOGL_NOTICE, "DM command error\n");
813 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
816 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
817 /* 5.4.1.2 DM responses with the F bit set to "0"
824 case LAPDm_STATE_SABM_SENT:
826 case LAPDm_STATE_MF_EST:
827 if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1) {
828 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
830 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP, mctx);
832 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
833 "response, multiple frame established "
835 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx);
839 case LAPDm_STATE_TIMER_RECOV:
840 /* DM is normal in case PF = 1 */
841 if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) {
842 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
843 "response, multiple frame established "
845 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx);
850 case LAPDm_STATE_DISC_SENT:
851 /* reset Timer T200 */
852 osmo_timer_del(&dl->t200);
853 /* go to idle state */
854 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
855 rc = send_rll_simple(RSL_MT_REL_CONF, mctx);
858 case LAPDm_STATE_IDLE:
859 /* 5.4.5 all other frame types shall be discarded */
861 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM response! "
867 osmo_timer_del(&dl->t200);
868 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
872 LOGP(DLAPDM, LOGL_INFO, "UI received\n");
873 /* G.2.2 Wrong value of the C/R bit */
874 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
875 LOGP(DLAPDM, LOGL_NOTICE, "UI indicates response "
878 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
882 length = msg->l2h[2] >> 2;
883 /* FIXME: G.4.5 If UI is received with L>N201 or with M bit
884 * set, AN MDL-ERROR-INDICATION is sent to MM.
887 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
889 msg->l3h = msg->l2h + 2;
891 rc = check_length_ind(mctx, msg->l2h[2]);
896 length = msg->l2h[2] >> 2;
897 msg->l3h = msg->l2h + 3;
899 /* do some length checks */
901 /* 5.3.3 UI frames received with the length indicator
902 * set to "0" shall be ignored
904 LOGP(DLAPDM, LOGL_INFO, "length=0 (discarding)\n");
908 switch (LAPDm_ADDR_SAPI(mctx->addr)) {
909 case LAPDm_SAPI_NORMAL:
913 /* 5.3.3 UI frames with invalid SAPI values shall be
916 LOGP(DLAPDM, LOGL_INFO, "sapi=%u (discarding)\n",
917 LAPDm_ADDR_SAPI(mctx->addr));
922 rc = send_rslms_rll_l3_ui(mctx, msg);
925 rsl_msg = RSL_MT_REL_IND;
927 LOGP(DLAPDM, LOGL_INFO, "DISC received\n");
929 lapdm_dl_flush_tx(dl);
930 lapdm_dl_flush_send(dl);
932 dl->seq_err_cond = 0;
933 /* G.2.2 Wrong value of the C/R bit */
934 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
935 LOGP(DLAPDM, LOGL_NOTICE, "DISC response error\n");
937 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
940 length = msg->l2h[2] >> 2;
941 if (length > 0 || msg->l2h[2] & 0x02) {
942 /* G.4.4 If a DISC or DM frame is received with L>0 or
943 * with the M bit set to "1", an MDL-ERROR-INDICATION
944 * primitive with cause "U frame with incorrect
945 * parameters" is sent to the mobile management entity.
947 LOGP(DLAPDM, LOGL_NOTICE,
948 "U frame iwth incorrect parameters ");
950 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
955 case LAPDm_STATE_IDLE:
956 LOGP(DLAPDM, LOGL_INFO, "DISC in idle state\n");
957 /* send DM with F=P */
958 return lapdm_send_dm(mctx);
959 case LAPDm_STATE_SABM_SENT:
960 LOGP(DLAPDM, LOGL_INFO, "DISC in SABM state\n");
961 /* 5.4.6.2 send DM with F=P */
963 /* reset Timer T200 */
964 osmo_timer_del(&dl->t200);
965 return send_rll_simple(RSL_MT_REL_IND, mctx);
966 case LAPDm_STATE_MF_EST:
967 case LAPDm_STATE_TIMER_RECOV:
968 LOGP(DLAPDM, LOGL_INFO, "DISC in est state\n");
970 case LAPDm_STATE_DISC_SENT:
971 LOGP(DLAPDM, LOGL_INFO, "DISC in disc state\n");
972 rsl_msg = RSL_MT_REL_CONF;
975 lapdm_send_ua(mctx, length, msg->l2h + 3);
978 /* send UA response */
979 lapdm_send_ua(mctx, length, msg->l2h + 3);
980 /* reset Timer T200 */
981 osmo_timer_del(&dl->t200);
982 /* enter idle state */
983 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
984 /* send notification to L3 */
985 rc = send_rll_simple(rsl_msg, mctx);
988 LOGP(DLAPDM, LOGL_INFO, "UA received\n");
989 /* G.2.2 Wrong value of the C/R bit */
990 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
991 LOGP(DLAPDM, LOGL_NOTICE, "UA indicates command "
994 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
998 length = msg->l2h[2] >> 2;
999 /* G.4.5 If UA is received with L>N201 or with M bit
1000 * set, AN MDL-ERROR-INDICATION is sent to MM.
1002 if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) {
1003 LOGP(DLAPDM, LOGL_NOTICE, "UA too large error\n");
1005 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
1009 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1010 /* 5.4.1.2 A UA response with the F bit set to "0"
1013 LOGP(DLAPDM, LOGL_INFO, "F=0 (discarding)\n");
1017 switch (dl->state) {
1018 case LAPDm_STATE_SABM_SENT:
1020 case LAPDm_STATE_MF_EST:
1021 case LAPDm_STATE_TIMER_RECOV:
1022 LOGP(DLAPDM, LOGL_INFO, "unsolicited UA response! "
1024 rsl_rll_error(RLL_CAUSE_UNSOL_UA_RESP, mctx);
1027 case LAPDm_STATE_DISC_SENT:
1028 LOGP(DLAPDM, LOGL_INFO, "UA in disconnect state\n");
1029 /* reset Timer T200 */
1030 osmo_timer_del(&dl->t200);
1031 /* go to idle state */
1032 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1033 rc = send_rll_simple(RSL_MT_REL_CONF, mctx);
1036 case LAPDm_STATE_IDLE:
1037 /* 5.4.5 all other frame types shall be discarded */
1039 LOGP(DLAPDM, LOGL_INFO, "unsolicited UA response! "
1044 LOGP(DLAPDM, LOGL_INFO, "UA in SABM state\n");
1045 /* reset Timer T200 */
1046 osmo_timer_del(&dl->t200);
1047 /* compare UA with SABME if contention resolution is applied */
1048 if (dl->tx_hist[0][2] >> 2) {
1049 rc = check_length_ind(mctx, msg->l2h[2]);
1051 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
1053 /* go to idle state */
1054 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1057 length = msg->l2h[2] >> 2;
1058 if (length != (dl->tx_hist[0][2] >> 2)
1059 || !!memcmp(dl->tx_hist[0] + 3, msg->l2h + 3,
1061 LOGP(DLAPDM, LOGL_INFO, "**** UA response "
1062 "mismatches ****\n");
1063 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
1065 /* go to idle state */
1066 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1070 /* set Vs, Vr and Va to 0 */
1071 dl->V_send = dl->V_recv = dl->V_ack = 0;
1073 dl->tx_length[0] = 0;
1074 /* enter multiple-frame-established state */
1075 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1076 /* send outstanding frames, if any (resume / reconnect) */
1077 rslms_send_i(mctx, __LINE__);
1078 /* send notification to L3 */
1079 rc = send_rll_simple(RSL_MT_EST_CONF, mctx);
1084 LOGP(DLAPDM, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1086 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1092 /* Receive a LAPDm S (Supervisory) message from L1 */
1093 static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1095 struct lapdm_datalink *dl = mctx->dl;
1098 length = msg->l2h[2] >> 2;
1099 if (length > 0 || msg->l2h[2] & 0x02) {
1100 /* G.4.3 If a supervisory frame is received with L>0 or
1101 * with the M bit set to "1", an MDL-ERROR-INDICATION
1102 * primitive with cause "S frame with incorrect
1103 * parameters" is sent to the mobile management entity. */
1104 LOGP(DLAPDM, LOGL_NOTICE,
1105 "S frame with incorrect parameters\n");
1107 rsl_rll_error(RLL_CAUSE_SFRM_INC_PARAM, mctx);
1111 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1112 && LAPDm_CTRL_PF_BIT(mctx->ctrl)
1113 && dl->state != LAPDm_STATE_TIMER_RECOV) {
1114 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1115 LOGP(DLAPDM, LOGL_NOTICE, "S frame response with F=1 error\n");
1116 rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
1119 switch (dl->state) {
1120 case LAPDm_STATE_IDLE:
1121 /* if P=1, respond DM with F=1 (5.2.2) */
1122 /* 5.4.5 all other frame types shall be discarded */
1123 if (LAPDm_CTRL_PF_BIT(mctx->ctrl))
1124 lapdm_send_dm(mctx); /* F=P */
1126 case LAPDm_STATE_SABM_SENT:
1127 case LAPDm_STATE_DISC_SENT:
1128 LOGP(DLAPDM, LOGL_NOTICE, "S frame ignored in this state\n");
1132 switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) {
1134 LOGP(DLAPDM, LOGL_INFO, "RR received\n");
1135 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1136 lapdm_acknowledge(mctx);
1139 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1140 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1141 if (!dl->own_busy && !dl->seq_err_cond) {
1142 LOGP(DLAPDM, LOGL_NOTICE, "RR frame command "
1143 "with polling bit set and we are not "
1144 "busy, so we reply with RR frame\n");
1145 lapdm_send_rr(mctx, 1);
1146 /* NOTE: In case of sequence error condition,
1147 * the REJ frame has been transmitted when
1148 * entering the condition, so it has not be
1151 } else if (dl->own_busy) {
1152 LOGP(DLAPDM, LOGL_NOTICE, "RR frame command "
1153 "with polling bit set and we are busy, "
1154 "so we reply with RR frame\n");
1155 lapdm_send_rnr(mctx, 1);
1157 } else if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1158 && LAPDm_CTRL_PF_BIT(mctx->ctrl)
1159 && dl->state == LAPDm_STATE_TIMER_RECOV) {
1160 LOGP(DLAPDM, LOGL_INFO, "RR response with F==1, "
1161 "and we are in timer recovery state, so "
1162 "we leave that state\n");
1163 /* V(S) to the N(R) in the RR frame */
1164 dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl);
1165 /* reset Timer T200 */
1166 osmo_timer_del(&dl->t200);
1167 /* 5.5.7 Clear timer recovery condition */
1168 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1170 /* Send message, if possible due to acknowledged data */
1171 rslms_send_i(mctx, __LINE__);
1175 LOGP(DLAPDM, LOGL_INFO, "RNR received\n");
1176 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1177 lapdm_acknowledge(mctx);
1180 /* Set peer receiver busy condition */
1183 if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1184 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
1185 if (!dl->own_busy) {
1186 LOGP(DLAPDM, LOGL_INFO, "RNR poll "
1187 "command and we are not busy, "
1188 "so we reply with RR final "
1190 /* Send RR with F=1 */
1191 lapdm_send_rr(mctx, 1);
1193 LOGP(DLAPDM, LOGL_INFO, "RNR poll "
1194 "command and we are busy, so "
1195 "we reply with RNR final "
1197 /* Send RNR with F=1 */
1198 lapdm_send_rnr(mctx, 1);
1200 } else if (dl->state == LAPDm_STATE_TIMER_RECOV) {
1201 LOGP(DLAPDM, LOGL_INFO, "RNR poll response "
1202 "and we in timer recovery state, so "
1203 "we leave that state\n");
1204 /* 5.5.7 Clear timer recovery condition */
1205 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1206 /* V(S) to the N(R) in the RNR frame */
1207 dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl);
1210 LOGP(DLAPDM, LOGL_INFO, "RNR not polling/final state "
1213 /* Send message, if possible due to acknowledged data */
1214 rslms_send_i(mctx, __LINE__);
1218 LOGP(DLAPDM, LOGL_INFO, "REJ received\n");
1219 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1220 lapdm_acknowledge(mctx);
1223 if (dl->state != LAPDm_STATE_TIMER_RECOV) {
1224 /* Clear an existing peer receiver busy condition */
1226 /* V(S) and V(A) to the N(R) in the REJ frame */
1227 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1228 /* reset Timer T200 */
1229 osmo_timer_del(&dl->t200);
1231 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1232 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1233 if (!dl->own_busy && !dl->seq_err_cond) {
1234 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1235 "command not in timer recovery "
1236 "state and not in own busy "
1237 "condition received, so we "
1238 "respond with RR final "
1240 lapdm_send_rr(mctx, 1);
1241 /* NOTE: In case of sequence error
1242 * condition, the REJ frame has been
1243 * transmitted when entering the
1244 * condition, so it has not be done
1247 } else if (dl->own_busy) {
1248 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1249 "command not in timer recovery "
1250 "state and in own busy "
1251 "condition received, so we "
1252 "respond with RNR final "
1254 lapdm_send_rnr(mctx, 1);
1257 LOGP(DLAPDM, LOGL_INFO, "REJ response or not "
1258 "polling command not in timer recovery "
1259 "state received\n");
1260 /* send MDL ERROR INIDCATION to L3 */
1261 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1262 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1263 rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
1266 } else if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1267 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1268 LOGP(DLAPDM, LOGL_INFO, "REJ poll response in timer "
1269 "recovery state received\n");
1270 /* Clear an existing peer receiver busy condition */
1272 /* 5.5.7 Clear timer recovery condition */
1273 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1274 /* V(S) and V(A) to the N(R) in the REJ frame */
1275 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1276 /* reset Timer T200 */
1277 osmo_timer_del(&dl->t200);
1279 /* Clear an existing peer receiver busy condition */
1281 /* V(S) and V(A) to the N(R) in the REJ frame */
1282 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1284 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1285 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1286 if (!dl->own_busy && !dl->seq_err_cond) {
1287 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1288 "command in timer recovery "
1289 "state and not in own busy "
1290 "condition received, so we "
1291 "respond with RR final "
1293 lapdm_send_rr(mctx, 1);
1294 /* NOTE: In case of sequence error
1295 * condition, the REJ frame has been
1296 * transmitted when entering the
1297 * condition, so it has not be done
1300 } else if (dl->own_busy) {
1301 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1302 "command in timer recovery "
1303 "state and in own busy "
1304 "condition received, so we "
1305 "respond with RNR final "
1307 lapdm_send_rnr(mctx, 1);
1310 LOGP(DLAPDM, LOGL_INFO, "REJ response or not "
1311 "polling command in timer recovery "
1312 "state received\n");
1315 /* FIXME: 5.5.4.2 2) */
1317 /* Send message, if possible due to acknowledged data */
1318 rslms_send_i(mctx, __LINE__);
1323 LOGP(DLAPDM, LOGL_NOTICE, "Supervisory frame not allowed.\n");
1325 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1332 /* Receive a LAPDm I (Information) message from L1 */
1333 static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1335 struct lapdm_datalink *dl = mctx->dl;
1336 //uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl);
1337 uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl);
1341 LOGP(DLAPDM, LOGL_NOTICE, "I received\n");
1343 /* G.2.2 Wrong value of the C/R bit */
1344 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
1345 LOGP(DLAPDM, LOGL_NOTICE, "I frame response not allowed\n");
1347 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1351 length = msg->l2h[2] >> 2;
1352 if (length == 0 || length + 3 > mctx->n201) {
1353 /* G.4.2 If the length indicator of an I frame is set
1354 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1355 * primitive with cause "I frame with incorrect length"
1356 * is sent to the mobile management entity. */
1357 LOGP(DLAPDM, LOGL_NOTICE, "I frame length not allowed\n");
1359 rsl_rll_error(RLL_CAUSE_IFRM_INC_LEN, mctx);
1363 /* G.4.2 If the numerical value of L is L<N201 and the M
1364 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1365 * cause "I frame with incorrect use of M bit" is sent to the
1366 * mobile management entity. */
1367 if ((msg->l2h[2] & LAPDm_MORE) && length + 3 < mctx->n201) {
1368 LOGP(DLAPDM, LOGL_NOTICE, "I frame with M bit too short\n");
1370 rsl_rll_error(RLL_CAUSE_IFRM_INC_MBITS, mctx);
1374 switch (dl->state) {
1375 case LAPDm_STATE_IDLE:
1376 /* if P=1, respond DM with F=1 (5.2.2) */
1377 /* 5.4.5 all other frame types shall be discarded */
1378 if (LAPDm_CTRL_PF_BIT(mctx->ctrl))
1379 lapdm_send_dm(mctx); /* F=P */
1381 case LAPDm_STATE_SABM_SENT:
1382 case LAPDm_STATE_DISC_SENT:
1383 LOGP(DLAPDM, LOGL_NOTICE, "I frame ignored in this state\n");
1388 /* 5.7.1: N(s) sequence error */
1389 if (ns != dl->V_recv) {
1390 LOGP(DLAPDM, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1391 "V(R)=%u\n", ns, dl->V_recv);
1394 if (!dl->seq_err_cond) {
1395 /* FIXME: help me understand what exactly todo here
1396 dl->seq_err_cond = 1;
1398 lapdm_send_rej(mctx, LAPDm_CTRL_PF_BIT(mctx->ctrl));
1403 dl->seq_err_cond = 0;
1405 /* Increment receiver state */
1406 dl->V_recv = inc_mod8(dl->V_recv);
1407 LOGP(DLAPDM, LOGL_NOTICE, "incrementing V(R) to %u\n", dl->V_recv);
1409 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1410 lapdm_acknowledge(mctx); /* V(A) is also set here */
1412 /* Only if we are not in own receiver busy condition */
1413 if (!dl->own_busy) {
1414 /* if the frame carries a complete segment */
1415 if (!(msg->l2h[2] & LAPDm_MORE)
1416 && !dl->rcv_buffer) {
1417 LOGP(DLAPDM, LOGL_INFO, "message in single I frame\n");
1418 /* send a DATA INDICATION to L3 */
1419 msg->l3h = msg->l2h + 3;
1422 msg->tail = msg->data + length;
1423 rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg);
1425 /* create rcv_buffer */
1426 if (!dl->rcv_buffer) {
1427 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1428 "frames (first message)\n");
1429 dl->rcv_buffer = msgb_alloc_headroom(200+56, 56,
1431 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1433 /* concat. rcv_buffer */
1434 if (msgb_l3len(dl->rcv_buffer) + length > 200) {
1435 LOGP(DLAPDM, LOGL_NOTICE, "Received frame "
1438 memcpy(msgb_put(dl->rcv_buffer, length),
1439 msg->l2h + 3, length);
1441 /* if the last segment was received */
1442 if (!(msg->l2h[2] & LAPDm_MORE)) {
1443 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1444 "frames (last message)\n");
1445 rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx,
1447 dl->rcv_buffer = NULL;
1449 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1450 "frames (next message)\n");
1455 LOGP(DLAPDM, LOGL_INFO, "I frame ignored during own receiver "
1456 "busy condition\n");
1458 /* Check for P bit */
1459 if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1461 /* check if we are not in own receiver busy */
1462 if (!dl->own_busy) {
1463 LOGP(DLAPDM, LOGL_INFO, "we are not busy, send RR\n");
1464 /* Send RR with F=1 */
1465 rc = lapdm_send_rr(mctx, 1);
1467 LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
1468 /* Send RNR with F=1 */
1469 rc = lapdm_send_rnr(mctx, 1);
1473 /* check if we are not in own receiver busy */
1474 if (!dl->own_busy) {
1475 /* NOTE: V(R) is already set above */
1476 rc = rslms_send_i(mctx, __LINE__);
1478 LOGP(DLAPDM, LOGL_INFO, "we are not busy and "
1479 "have no pending data, send RR\n");
1480 /* Send RR with F=0 */
1481 return lapdm_send_rr(mctx, 0);
1483 /* all I or one RR is sent, we are done */
1486 LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
1487 /* Send RNR with F=0 */
1488 rc = lapdm_send_rnr(mctx, 0);
1492 /* Send message, if possible due to acknowledged data */
1493 rslms_send_i(mctx, __LINE__);
1498 /* Receive a LAPDm message from L1 */
1499 static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1503 /* G.2.3 EA bit set to "0" is not allowed in GSM */
1504 if (!LAPDm_ADDR_EA(mctx->addr)) {
1505 LOGP(DLAPDM, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
1507 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1511 if (LAPDm_CTRL_is_U(mctx->ctrl))
1512 rc = lapdm_rx_u(msg, mctx);
1513 else if (LAPDm_CTRL_is_S(mctx->ctrl))
1514 rc = lapdm_rx_s(msg, mctx);
1515 else if (LAPDm_CTRL_is_I(mctx->ctrl))
1516 rc = lapdm_rx_i(msg, mctx);
1518 LOGP(DLAPDM, LOGL_NOTICE, "unknown LAPDm format\n");
1525 /* input into layer2 (from layer 1) */
1526 static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t chan_nr, uint8_t link_id)
1528 uint8_t cbits = chan_nr >> 3;
1529 uint8_t sapi = link_id & 7;
1530 struct lapdm_msg_ctx mctx;
1533 /* when we reach here, we have a msgb with l2h pointing to the raw
1534 * 23byte mac block. The l1h has already been purged. */
1536 mctx.dl = datalink_for_sapi(le, sapi);
1537 mctx.chan_nr = chan_nr;
1538 mctx.link_id = link_id;
1539 mctx.addr = mctx.ctrl = 0;
1541 /* G.2.1 No action schall be taken on frames containing an unallocated
1545 LOGP(DLAPDM, LOGL_NOTICE, "Received frame for unsupported "
1546 "SAPI %d!\n", sapi);
1552 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
1553 if (cbits == 0x10 || cbits == 0x12) {
1554 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
1555 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
1556 mctx.n201 = N201_Bbis;
1558 if (mctx.link_id & 0x40) {
1559 /* It was received from network on SACCH, thus
1560 * lapdm_fmt must be B4 */
1561 mctx.lapdm_fmt = LAPDm_FMT_B4;
1562 mctx.n201 = N201_B4;
1563 LOGP(DLAPDM, LOGL_INFO, "fmt=B4\n");
1564 /* SACCH frames have a two-byte L1 header that
1565 * OsmocomBB L1 doesn't strip */
1566 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
1567 mctx.ta_ind = msg->l2h[1];
1571 mctx.lapdm_fmt = LAPDm_FMT_B;
1572 LOGP(DLAPDM, LOGL_INFO, "fmt=B\n");
1573 mctx.n201 = 23; // FIXME: select correct size by chan.
1577 switch (mctx.lapdm_fmt) {
1581 mctx.addr = msg->l2h[0];
1582 if (!(mctx.addr & 0x01)) {
1583 LOGP(DLAPDM, LOGL_ERROR, "we don't support "
1584 "multibyte addresses (discarding)\n");
1588 mctx.ctrl = msg->l2h[1];
1589 /* obtain SAPI from address field */
1590 mctx.link_id |= LAPDm_ADDR_SAPI(mctx.addr);
1591 rc = lapdm_ph_data_ind(msg, &mctx);
1593 case LAPDm_FMT_Bter:
1597 case LAPDm_FMT_Bbis:
1598 /* directly pass up to layer3 */
1599 LOGP(DLAPDM, LOGL_INFO, "fmt=Bbis UI\n");
1600 msg->l3h = msg->l2h;
1602 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
1611 static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
1613 int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
1615 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
1618 if (oph->sap != SAP_GSM_PH)
1621 if (oph->operation != PRIM_OP_INDICATION)
1624 switch (oph->primitive) {
1626 if (oph->operation != PRIM_OP_INDICATION)
1628 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
1629 pp->u.data.link_id);
1632 if (oph->operation != PRIM_OP_INDICATION)
1634 rc = l2_ph_data_conf(oph->msg, le);
1637 switch (oph->operation) {
1638 case PRIM_OP_INDICATION:
1640 //rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn);
1642 case PRIM_OP_CONFIRM:
1643 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
1655 /* L3 -> L2 / RSLMS -> LAPDm */
1657 /* L3 requests establishment of data link */
1658 static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
1660 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1661 uint8_t chan_nr = rllh->chan_nr;
1662 uint8_t link_id = rllh->link_id;
1663 uint8_t sapi = rllh->link_id & 7;
1664 struct tlv_parsed tv;
1666 uint8_t n201 = 23; //FIXME
1668 /* Set chan_nr and link_id for established connection */
1669 memset(&dl->mctx, 0, sizeof(dl->mctx));
1671 dl->mctx.n201 = n201;
1672 dl->mctx.chan_nr = chan_nr;
1673 dl->mctx.link_id = link_id;
1675 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1676 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1677 /* contention resolution establishment procedure */
1679 /* According to clause 6, the contention resolution
1680 * procedure is only permitted with SAPI value 0 */
1681 LOGP(DLAPDM, LOGL_ERROR, "SAPI != 0 but contention"
1682 "resolution (discarding)\n");
1684 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1686 /* transmit a SABM command with the P bit set to "1". The SABM
1687 * command shall contain the layer 3 message unit */
1688 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1689 LOGP(DLAPDM, LOGL_INFO, "perform establishment with content "
1692 /* normal establishment procedure */
1694 LOGP(DLAPDM, LOGL_INFO, "perform normal establishm. (SABM)\n");
1697 /* check if the layer3 message length exceeds N201 */
1698 if (length + 3 > 21) { /* FIXME: do we know the channel N201? */
1699 LOGP(DLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) "
1700 "(discarding)\n", length + 3, 21);
1702 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1705 /* Flush send-queue */
1706 /* Clear send-buffer */
1707 lapdm_dl_flush_send(dl);
1709 /* Discard partly received L3 message */
1710 if (dl->rcv_buffer) {
1711 msgb_free(dl->rcv_buffer);
1712 dl->rcv_buffer = NULL;
1715 /* Remove RLL header from msgb */
1718 /* Push LAPDm header on msgb */
1719 msg->l2h = msgb_push(msg, 3);
1720 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1721 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
1722 msg->l2h[2] = LAPDm_LEN(length);
1723 /* Transmit-buffer carries exactly one segment */
1724 memcpy(dl->tx_hist[0], msg->l2h, 3 + length);
1725 dl->tx_length[0] = 3 + length;
1726 /* set Vs to 0, because it is used as index when resending SABM */
1730 dl->own_busy = dl->peer_busy = 0;
1731 dl->retrans_ctr = 0;
1732 lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT);
1734 /* Tramsmit and start T200 */
1735 osmo_timer_schedule(&dl->t200, T200);
1736 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1739 /* L3 requests transfer of unnumbered information */
1740 static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1742 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1743 uint8_t chan_nr = rllh->chan_nr;
1744 uint8_t link_id = rllh->link_id;
1745 uint8_t sapi = link_id & 7;
1746 struct tlv_parsed tv;
1748 uint8_t n201 = 23; //FIXME
1749 uint8_t ta = 0, tx_power = 0;
1751 /* check if the layer3 message length exceeds N201 */
1753 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1755 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
1756 ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
1758 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
1759 tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
1761 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1762 LOGP(DLAPDM, LOGL_ERROR, "unit data request without message "
1767 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1768 /* check if the layer3 message length exceeds N201 */
1769 if (length + 5 > 23) { /* FIXME: do we know the channel N201? */
1770 LOGP(DLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) "
1771 "(discarding)\n", length + 5, 23);
1776 LOGP(DLAPDM, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
1779 /* Remove RLL header from msgb */
1782 /* Push L1 + LAPDm header on msgb */
1783 msg->l2h = msgb_push(msg, 2 + 3);
1784 msg->l2h[0] = tx_power;
1786 msg->l2h[2] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1787 msg->l2h[3] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
1788 msg->l2h[4] = LAPDm_LEN(length);
1789 // FIXME: short L2 header support
1792 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1795 /* L3 requests transfer of acknowledged information */
1796 static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1798 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1799 struct tlv_parsed tv;
1801 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1802 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1803 LOGP(DLAPDM, LOGL_ERROR, "data request without message "
1809 LOGP(DLAPDM, LOGL_INFO, "writing message to send-queue\n");
1811 /* Remove the RSL/RLL header */
1814 /* Write data into the send queue */
1815 msgb_enqueue(&dl->send_queue, msg);
1817 /* Send message, if possible */
1818 rslms_send_i(&dl->mctx, __LINE__);
1822 /* Send next I frame from queued/buffered data */
1823 static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line)
1825 struct lapdm_datalink *dl = mctx->dl;
1826 uint8_t chan_nr = mctx->chan_nr;
1827 uint8_t link_id = mctx->link_id;
1828 uint8_t sapi = link_id & 7;
1829 int k = k_sapi[sapi];
1832 int rc = - 1; /* we sent nothing */
1834 LOGP(DLAPDM, LOGL_INFO, "%s() called from line %d\n", __func__, line);
1838 if (dl->peer_busy) {
1839 LOGP(DLAPDM, LOGL_INFO, "peer busy, not sending\n");
1843 if (dl->state == LAPDm_STATE_TIMER_RECOV) {
1844 LOGP(DLAPDM, LOGL_INFO, "timer recovery, not sending\n");
1848 /* If the send state variable V(S) is equal to V(A) plus k
1849 * (where k is the maximum number of outstanding I frames - see
1850 * subclause 5.8.4), the data link layer entity shall not transmit any
1851 * new I frames, but shall retransmit an I frame as a result
1852 * of the error recovery procedures as described in subclauses 5.5.4 and
1854 if (dl->V_send == add_mod8(dl->V_ack, k)) {
1855 LOGP(DLAPDM, LOGL_INFO, "k frames outstanding, not sending "
1856 "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->V_send,
1861 /* if we have no tx_hist yet, we create it */
1862 if (!dl->tx_length[dl->V_send]) {
1863 /* Get next message into send-buffer, if any */
1864 if (!dl->send_buffer) {
1867 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1868 /* No more data to be sent */
1869 if (!dl->send_buffer)
1871 LOGP(DLAPDM, LOGL_INFO, "get message from "
1875 /* How much is left in the send-buffer? */
1876 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1877 /* Segment, if data exceeds N201 */
1879 if (length > mctx->n201 - 3)
1880 length = mctx->n201 - 3;
1881 LOGP(DLAPDM, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
1882 "length %d first byte %02x\n",
1883 msgb_l3len(dl->send_buffer), dl->send_out, left,
1884 mctx->n201, length, dl->send_buffer->l3h[0]);
1885 /* If message in send-buffer is completely sent */
1887 msgb_free(dl->send_buffer);
1888 dl->send_buffer = NULL;
1892 LOGP(DLAPDM, LOGL_INFO, "send I frame %sV(S)=%d\n",
1893 (left > length) ? "segment " : "", dl->V_send);
1895 /* Create I frame (segment) and transmit-buffer content */
1896 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
1897 msg->l2h = msgb_put(msg, 3 + length);
1898 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1899 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
1900 msg->l2h[2] = LAPDm_LEN(length);
1902 msg->l2h[2] |= LAPDm_MORE;
1903 memcpy(msg->l2h + 3, dl->send_buffer->l3h + dl->send_out,
1905 memcpy(dl->tx_hist[dl->V_send], msg->l2h, 3 + length);
1906 dl->tx_length[dl->V_send] = 3 + length;
1907 /* Add length to track how much is already in the tx buffer */
1908 dl->send_out += length;
1910 LOGP(DLAPDM, LOGL_INFO, "resend I frame from tx buffer "
1911 "V(S)=%d\n", dl->V_send);
1913 /* Create I frame (segment) from tx_hist */
1914 length = dl->tx_length[dl->V_send];
1915 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
1916 msg->l2h = msgb_put(msg, length);
1917 memcpy(msg->l2h, dl->tx_hist[dl->V_send], length);
1918 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
1921 /* The value of the send state variable V(S) shall be incremented by 1
1922 * at the end of the transmission of the I frame */
1923 dl->V_send = inc_mod8(dl->V_send);
1925 /* If timer T200 is not running at the time right before transmitting a
1926 * frame, when the PH-READY-TO-SEND primitive is received from the
1927 * physical layer., it shall be set. */
1928 if (!osmo_timer_pending(&dl->t200))
1929 osmo_timer_schedule(&dl->t200, T200);
1931 tx_ph_data_enqueue(dl, msg, chan_nr, link_id, mctx->n201);
1933 rc = 0; /* we sent something */
1937 /* L3 requests suspension of data link */
1938 static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1940 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1941 uint8_t sapi = rllh->link_id & 7;
1944 LOGP(DLAPDM, LOGL_ERROR, "SAPI != 0 while suspending\n");
1949 LOGP(DLAPDM, LOGL_INFO, "perform suspension\n");
1951 /* put back the send-buffer to the send-queue (first position) */
1952 if (dl->send_buffer) {
1953 LOGP(DLAPDM, LOGL_INFO, "put frame in sendbuffer back to "
1955 llist_add(&dl->send_buffer->list, &dl->send_queue);
1956 dl->send_buffer = NULL;
1958 LOGP(DLAPDM, LOGL_INFO, "no frame in sendbuffer\n");
1960 /* Clear transmit buffer, but keep send buffer */
1961 lapdm_dl_flush_tx(dl);
1965 return send_rll_simple(RSL_MT_SUSP_CONF, &dl->mctx);
1968 /* L3 requests resume of data link */
1969 static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1971 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1972 uint8_t chan_nr = rllh->chan_nr;
1973 uint8_t link_id = rllh->link_id;
1974 uint8_t sapi = rllh->link_id & 7;
1975 struct tlv_parsed tv;
1977 uint8_t n201 = 23; //FIXME
1979 /* Set chan_nr and link_id for established connection */
1980 memset(&dl->mctx, 0, sizeof(dl->mctx));
1982 dl->mctx.n201 = n201;
1983 dl->mctx.chan_nr = chan_nr;
1984 dl->mctx.link_id = link_id;
1986 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1987 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1988 LOGP(DLAPDM, LOGL_ERROR, "resume without message error\n");
1990 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1992 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1994 LOGP(DLAPDM, LOGL_INFO, "perform re-establishment (SABM) length=%d\n",
1997 /* Replace message in the send-buffer (reconnect) */
1998 if (dl->send_buffer)
1999 msgb_free(dl->send_buffer);
2002 /* Remove the RSL/RLL header */
2004 /* Write data into the send buffer, to be sent first */
2005 dl->send_buffer = msg;
2008 /* Discard partly received L3 message */
2009 if (dl->rcv_buffer) {
2010 msgb_free(dl->rcv_buffer);
2011 dl->rcv_buffer = NULL;
2014 /* Create new msgb (old one is now free) */
2015 msg = msgb_alloc_headroom(23+10, 10, "LAPDm SABM");
2016 msg->l2h = msgb_put(msg, 3);
2017 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
2018 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
2019 msg->l2h[2] = LAPDm_LEN(0);
2020 /* Transmit-buffer carries exactly one segment */
2021 memcpy(dl->tx_hist[0], msg->l2h, 3);
2022 dl->tx_length[0] = 3;
2023 /* set Vs to 0, because it is used as index when resending SABM */
2027 dl->own_busy = dl->peer_busy = 0;
2028 dl->retrans_ctr = 0;
2029 lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT);
2031 /* Tramsmit and start T200 */
2032 osmo_timer_schedule(&dl->t200, T200);
2033 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
2036 /* L3 requests release of data link */
2037 static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
2039 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2040 uint8_t chan_nr = rllh->chan_nr;
2041 uint8_t link_id = rllh->link_id;
2042 uint8_t sapi = rllh->link_id & 7;
2045 /* get release mode */
2046 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
2047 mode = rllh->data[1] & 1;
2051 LOGP(DLAPDM, LOGL_INFO, "perform local release\n");
2053 /* reset Timer T200 */
2054 osmo_timer_del(&dl->t200);
2055 /* enter idle state */
2056 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
2058 lapdm_dl_flush_tx(dl);
2059 lapdm_dl_flush_send(dl);
2060 /* send notification to L3 */
2061 return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
2064 /* in case we are already disconnecting */
2065 if (dl->state == LAPDm_STATE_DISC_SENT)
2068 LOGP(DLAPDM, LOGL_INFO, "perform normal release (DISC)\n");
2071 msgb_pull(msg, msg->tail - msg->l2h);
2073 /* Push LAPDm header on msgb */
2074 msg->l2h = msgb_push(msg, 3);
2075 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
2076 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DISC, 1);
2077 msg->l2h[2] = LAPDm_LEN(0);
2078 /* Transmit-buffer carries exactly one segment */
2079 memcpy(dl->tx_hist[0], msg->l2h, 3);
2080 dl->tx_length[0] = 3;
2083 dl->own_busy = dl->peer_busy = 0;
2084 dl->retrans_ctr = 0;
2085 lapdm_dl_newstate(dl, LAPDm_STATE_DISC_SENT);
2087 /* Tramsmit and start T200 */
2088 osmo_timer_schedule(&dl->t200, T200);
2089 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, dl->mctx.n201);
2092 /* L3 requests release in idle state */
2093 static int rslms_rx_rll_rel_req_idle(struct msgb *msg, struct lapdm_datalink *dl)
2097 /* send notification to L3 */
2098 return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
2101 /* L3 requests channel in idle state */
2102 static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
2104 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
2105 void *l1ctx = lc->lapdm_dcch.l1_ctx;
2106 struct osmo_phsap_prim pp;
2108 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
2109 PRIM_OP_REQUEST, NULL);
2111 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
2112 LOGP(DRSL, LOGL_ERROR, "Message too short for CHAN RQD!\n");
2115 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
2116 LOGP(DRSL, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
2119 pp.u.rach_req.ra = cch->data[1];
2120 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
2121 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
2123 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
2124 LOGP(DRSL, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
2127 /* TA = 0 - delay */
2128 pp.u.rach_req.ta = 0 - cch->data[5];
2130 if (cch->data[6] != RSL_IE_MS_POWER) {
2131 LOGP(DRSL, LOGL_ERROR, "Missing MS POWER IE\n");
2134 pp.u.rach_req.tx_power = cch->data[7];
2138 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
2141 /* L1 confirms channel request */
2142 static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
2144 struct abis_rsl_cchan_hdr *ch;
2146 struct gsm48_req_ref *ref;
2148 gsm_fn2gsmtime(&tm, frame_nr);
2151 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
2152 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
2153 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
2154 ch->chan_nr = RSL_CHAN_RACH;
2155 ch->data[0] = RSL_IE_REQ_REFERENCE;
2156 ref = (struct gsm48_req_ref *) (ch->data + 1);
2159 ref->t3_low = tm.t3 & 0x7;
2160 ref->t3_high = tm.t3 >> 3;
2162 return rslms_sendmsg(msg, le);
2165 const char *lapdm_state_names[] = {
2168 "LAPDm_STATE_SABM_SENT",
2169 "LAPDm_STATE_MF_EST",
2170 "LAPDm_STATE_TIMER_RECOV",
2171 "LAPDm_STATE_DISC_SENT",
2174 /* statefull handling for RSLms RLL messages from L3 */
2175 static struct l2downstate {
2178 int (*rout) (struct msgb *msg, struct lapdm_datalink *dl);
2179 } l2downstatelist[] = {
2180 /* create and send UI command */
2182 RSL_MT_UNIT_DATA_REQ, rslms_rx_rll_udata_req},
2184 /* create and send SABM command */
2185 {SBIT(LAPDm_STATE_IDLE),
2186 RSL_MT_EST_REQ, rslms_rx_rll_est_req},
2188 /* create and send I command */
2189 {SBIT(LAPDm_STATE_MF_EST) |
2190 SBIT(LAPDm_STATE_TIMER_RECOV),
2191 RSL_MT_DATA_REQ, rslms_rx_rll_data_req},
2193 /* suspend datalink */
2194 {SBIT(LAPDm_STATE_MF_EST) |
2195 SBIT(LAPDm_STATE_TIMER_RECOV),
2196 RSL_MT_SUSP_REQ, rslms_rx_rll_susp_req},
2198 /* create and send SABM command (resume) */
2199 {SBIT(LAPDm_STATE_MF_EST) |
2200 SBIT(LAPDm_STATE_TIMER_RECOV),
2201 RSL_MT_RES_REQ, rslms_rx_rll_res_req},
2203 /* create and send SABM command (reconnect) */
2204 {SBIT(LAPDm_STATE_IDLE) |
2205 SBIT(LAPDm_STATE_MF_EST) |
2206 SBIT(LAPDm_STATE_TIMER_RECOV),
2207 RSL_MT_RECON_REQ, rslms_rx_rll_res_req},
2209 /* create and send DISC command */
2210 {SBIT(LAPDm_STATE_SABM_SENT) |
2211 SBIT(LAPDm_STATE_MF_EST) |
2212 SBIT(LAPDm_STATE_TIMER_RECOV) |
2213 SBIT(LAPDm_STATE_DISC_SENT),
2214 RSL_MT_REL_REQ, rslms_rx_rll_rel_req},
2216 /* release in idle state */
2217 {SBIT(LAPDm_STATE_IDLE),
2218 RSL_MT_REL_REQ, rslms_rx_rll_rel_req_idle},
2221 #define L2DOWNSLLEN \
2222 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2224 /* incoming RSLms RLL message from L3 */
2225 static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
2227 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2228 int msg_type = rllh->c.msg_type;
2229 uint8_t sapi = rllh->link_id & 7;
2230 struct lapdm_entity *le;
2231 struct lapdm_datalink *dl;
2232 int i, supported = 0;
2235 if (msgb_l2len(msg) < sizeof(*rllh)) {
2236 LOGP(DRSL, LOGL_ERROR, "Message too short for RLL hdr!\n");
2240 if (rllh->link_id & 0x40)
2241 le = &lc->lapdm_acch;
2243 le = &lc->lapdm_dcch;
2245 /* G.2.1 No action schall be taken on frames containing an unallocated
2248 dl = datalink_for_sapi(le, sapi);
2250 LOGP(DRSL, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
2254 LOGP(DRSL, LOGL_INFO, "(%p) RLL Message '%s' received in state %s\n",
2255 lc->name, rsl_msg_name(msg_type), lapdm_state_names[dl->state]);
2257 /* find function for current state and message */
2258 for (i = 0; i < L2DOWNSLLEN; i++) {
2259 if (msg_type == l2downstatelist[i].type)
2261 if ((msg_type == l2downstatelist[i].type)
2262 && ((1 << dl->state) & l2downstatelist[i].states))
2266 LOGP(DRSL, LOGL_NOTICE, "Message unsupported.\n");
2270 if (i == L2DOWNSLLEN) {
2271 LOGP(DRSL, LOGL_NOTICE, "Message unhandled at this state.\n");
2276 rc = l2downstatelist[i].rout(msg, dl);
2281 /* incoming RSLms COMMON CHANNEL message from L3 */
2282 static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
2284 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
2285 int msg_type = cch->c.msg_type;
2288 if (msgb_l2len(msg) < sizeof(*cch)) {
2289 LOGP(DRSL, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
2294 case RSL_MT_CHAN_RQD:
2295 /* create and send RACH request */
2296 rc = rslms_rx_chan_rqd(lc, msg);
2299 LOGP(DRSL, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
2308 /* input into layer2 (from layer 3) */
2309 int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
2311 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
2314 if (msgb_l2len(msg) < sizeof(*rslh)) {
2315 LOGP(DRSL, LOGL_ERROR, "Message too short RSL hdr!\n");
2319 switch (rslh->msg_discr & 0xfe) {
2320 case ABIS_RSL_MDISC_RLL:
2321 rc = rslms_rx_rll(msg, lc);
2323 case ABIS_RSL_MDISC_COM_CHAN:
2324 rc = rslms_rx_com_chan(msg, lc);
2327 LOGP(DRSL, LOGL_ERROR, "unknown RSLms message "
2328 "discriminator 0x%02x", rslh->msg_discr);
2336 void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
2338 lc->lapdm_dcch.l1_prim_cb = cb;
2339 lc->lapdm_acch.l1_prim_cb = cb;
2340 lc->lapdm_dcch.l1_ctx = ctx;
2341 lc->lapdm_acch.l1_ctx = ctx;
2344 void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
2346 lc->lapdm_dcch.l3_cb = cb;
2347 lc->lapdm_acch.l3_cb = cb;
2348 lc->lapdm_dcch.l3_ctx = ctx;
2349 lc->lapdm_acch.l3_ctx = ctx;