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, enum lapdm_mode mode)
191 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
192 lapdm_dl_init(&le->datalink[i], le);
194 lapdm_entity_set_mode(le, mode);
197 void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
199 lapdm_entity_init(&lc->lapdm_acch, mode);
200 lapdm_entity_init(&lc->lapdm_dcch, mode);
204 static void lapdm_dl_flush_send(struct lapdm_datalink *dl)
208 /* Flush send-queue */
209 while ((msg = msgb_dequeue(&dl->send_queue)))
212 /* Clear send-buffer */
213 if (dl->send_buffer) {
214 msgb_free(dl->send_buffer);
215 dl->send_buffer = NULL;
219 static void lapdm_dl_flush_tx(struct lapdm_datalink *dl)
224 while ((msg = msgb_dequeue(&dl->tx_queue)))
226 for (i = 0; i < 8; i++)
227 dl->tx_length[i] = 0;
230 void lapdm_entity_exit(struct lapdm_entity *le)
233 struct lapdm_datalink *dl;
235 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
236 dl = &le->datalink[i];
237 lapdm_dl_flush_tx(dl);
238 lapdm_dl_flush_send(dl);
240 msgb_free(dl->rcv_buffer);
244 void lapdm_channel_exit(struct lapdm_channel *lc)
246 lapdm_entity_exit(&lc->lapdm_acch);
247 lapdm_entity_exit(&lc->lapdm_dcch);
250 static void lapdm_dl_newstate(struct lapdm_datalink *dl, uint32_t state)
252 LOGP(DLAPDM, LOGL_INFO, "new state %s -> %s\n",
253 lapdm_state_names[dl->state], lapdm_state_names[state]);
258 static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
261 case LAPDm_SAPI_NORMAL:
262 return &le->datalink[0];
264 return &le->datalink[1];
270 /* remove the L2 header from a MSGB */
271 static inline unsigned char *msgb_pull_l2h(struct msgb *msg)
273 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h);
278 /* Append padding (if required) */
279 static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
281 int pad_len = n201 - msgb_l2len(msg);
285 LOGP(DLAPDM, LOGL_ERROR,
286 "cannot pad message that is already too big!\n");
290 data = msgb_put(msg, pad_len);
291 memset(data, 0x2B, pad_len);
294 /* input function that L2 calls when sending messages up to L3 */
295 static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
302 /* call the layer2 message handler that is registered */
303 return le->l3_cb(msg, le, le->l3_ctx);
306 /* write a frame into the tx queue */
307 static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
308 uint8_t chan_nr, uint8_t link_id, uint8_t n201)
310 struct lapdm_entity *le = dl->entity;
311 struct osmo_phsap_prim pp;
313 /* if there is a pending message, queue it */
314 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
315 *msgb_push(msg, 1) = n201;
316 *msgb_push(msg, 1) = link_id;
317 *msgb_push(msg, 1) = chan_nr;
318 msgb_enqueue(&dl->tx_queue, msg);
322 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
323 PRIM_OP_REQUEST, msg);
324 pp.u.data.chan_nr = chan_nr;
325 pp.u.data.link_id = link_id;
327 /* send the frame now */
328 le->tx_pending = 0; /* disabled flow control */
329 lapdm_pad_msgb(msg, n201);
331 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
334 static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
336 struct lapdm_datalink *dl;
337 int last = le->last_tx_dequeue;
338 int i = last, n = ARRAY_SIZE(le->datalink);
339 struct msgb *msg = NULL;
341 /* round-robin dequeue */
345 dl = &le->datalink[i];
346 if ((msg = msgb_dequeue(&dl->tx_queue)))
351 /* Set last dequeue position */
352 le->last_tx_dequeue = i;
358 /* dequeue a msg that's pending transmission via L1 and wrap it into
359 * a osmo_phsap_prim */
360 int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
365 msg = tx_dequeue_msgb(le);
369 /* if we have a message, send PH-DATA.req */
370 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
371 PRIM_OP_REQUEST, msg);
373 /* Pull chan_nr and link_id */
374 pp->u.data.chan_nr = *msg->data;
376 pp->u.data.link_id = *msg->data;
381 /* Pad the frame, we can transmit now */
382 lapdm_pad_msgb(msg, n201);
387 /* get next frame from the tx queue. because the ms has multiple datalinks,
388 * each datalink's queue is read round-robin.
390 static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
392 struct osmo_phsap_prim pp;
394 /* we may send again */
397 /* free confirm message */
401 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
402 /* no message in all queues */
404 /* If user didn't request PH-EMPTY_FRAME.req, abort */
405 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
408 /* otherwise, send PH-EMPTY_FRAME.req */
409 osmo_prim_init(&pp.oph, SAP_GSM_PH,
411 PRIM_OP_REQUEST, NULL);
416 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
419 /* Create RSLms various RSLms messages */
420 static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
423 /* Add the RSL + RLL header */
424 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
426 /* send off the RSLms message to L3 */
427 return rslms_sendmsg(msg, mctx->dl->entity);
430 /* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
431 static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
433 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
434 struct abis_rsl_rll_hdr *rllh;
436 /* Add the RSL + RLL header */
437 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
438 msgb_push(msg, 2 + 2);
439 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
441 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
443 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
444 rllh->data[1] = mctx->ta_ind;
446 rllh->data[2] = RSL_IE_MS_POWER;
447 rllh->data[3] = mctx->tx_power_ind;
449 return rslms_sendmsg(msg, mctx->dl->entity);
452 static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
456 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
458 /* send off the RSLms message to L3 */
459 return rslms_sendmsg(msg, mctx->dl->entity);
462 static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
466 LOGP(DLAPDM, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
467 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
468 msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr));
469 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
470 return rslms_sendmsg(msg, mctx->dl->entity);
473 static int check_length_ind(struct lapdm_msg_ctx *mctx, uint8_t length_ind)
475 if (!(length_ind & 0x01)) {
476 /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION
477 * primitive with cause "frame not implemented" is sent to the
478 * mobile management entity. */
479 LOGP(DLAPDM, LOGL_NOTICE,
480 "we don't support multi-octet length\n");
481 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
487 static int lapdm_send_resend(struct lapdm_datalink *dl)
489 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm resend");
492 /* Resend SABM/DISC from tx_hist */
493 length = dl->tx_length[0];
494 msg->l2h = msgb_put(msg, length);
495 memcpy(msg->l2h, dl->tx_hist[dl->V_send], length);
497 return tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, dl->mctx.link_id,
501 static int lapdm_send_ua(struct lapdm_msg_ctx *mctx, uint8_t len, uint8_t *data)
503 uint8_t sapi = mctx->link_id & 7;
504 uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl);
505 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm UA");
506 struct lapdm_entity *le = mctx->dl->entity;
508 msg->l2h = msgb_put(msg, 3 + len);
509 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp);
510 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UA, f_bit);
511 msg->l2h[2] = LAPDm_LEN(len);
513 memcpy(msg->l2h + 3, data, len);
515 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
519 static int lapdm_send_dm(struct lapdm_msg_ctx *mctx)
521 uint8_t sapi = mctx->link_id & 7;
522 uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl);
523 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm DM");
524 struct lapdm_entity *le = mctx->dl->entity;
526 msg->l2h = msgb_put(msg, 3);
527 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp);
528 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DM, f_bit);
531 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
535 static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
537 uint8_t sapi = mctx->link_id & 7;
538 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RR");
539 struct lapdm_entity *le = mctx->dl->entity;
541 msg->l2h = msgb_put(msg, 3);
542 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp);
543 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit);
544 msg->l2h[2] = LAPDm_LEN(0);
546 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
550 static int lapdm_send_rnr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
552 uint8_t sapi = mctx->link_id & 7;
553 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RNR");
554 struct lapdm_entity *le = mctx->dl->entity;
556 msg->l2h = msgb_put(msg, 3);
557 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp);
558 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RNR, f_bit);
559 msg->l2h[2] = LAPDm_LEN(0);
561 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
565 static int lapdm_send_rej(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
567 uint8_t sapi = mctx->link_id & 7;
568 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm REJ");
569 struct lapdm_entity *le = mctx->dl->entity;
571 msg->l2h = msgb_put(msg, 3);
572 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp);
573 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_REJ, f_bit);
574 msg->l2h[2] = LAPDm_LEN(0);
576 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
580 /* Timer callback on T200 expiry */
581 static void lapdm_t200_cb(void *data)
583 struct lapdm_datalink *dl = data;
585 LOGP(DLAPDM, LOGL_INFO, "lapdm_t200_cb(%p) state=%u\n", dl, dl->state);
588 case LAPDm_STATE_SABM_SENT:
590 if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) {
591 /* send RELEASE INDICATION to L3 */
592 send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
593 /* send MDL ERROR INIDCATION to L3 */
594 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
595 /* flush tx buffers */
596 lapdm_dl_flush_tx(dl);
597 /* go back to idle state */
598 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
599 /* NOTE: we must not change any other states or buffers
600 * and queues, since we may reconnect after handover
601 * failure. the buffered messages is replaced there */
604 /* retransmit SABM command */
605 lapdm_send_resend(dl);
606 /* increment re-transmission counter */
608 /* restart T200 (PH-READY-TO-SEND) */
609 osmo_timer_schedule(&dl->t200, T200);
611 case LAPDm_STATE_DISC_SENT:
613 if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) {
614 /* send RELEASE INDICATION to L3 */
615 send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
616 /* send MDL ERROR INIDCATION to L3 */
617 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
619 lapdm_dl_flush_tx(dl);
620 lapdm_dl_flush_send(dl);
621 /* go back to idle state */
622 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
623 /* NOTE: we must not change any other states or buffers
624 * and queues, since we may reconnect after handover
625 * failure. the buffered messages is replaced there */
628 /* retransmit DISC command */
629 lapdm_send_resend(dl);
630 /* increment re-transmission counter */
632 /* restart T200 (PH-READY-TO-SEND) */
633 osmo_timer_schedule(&dl->t200, T200);
635 case LAPDm_STATE_MF_EST:
638 lapdm_dl_newstate(dl, LAPDm_STATE_TIMER_RECOV);
640 case LAPDm_STATE_TIMER_RECOV:
642 if (dl->retrans_ctr < N200) {
643 /* retransmit I frame (V_s-1) with P=1, if any */
644 if (dl->tx_length[sub_mod8(dl->V_send, 1)]) {
648 LOGP(DLAPDM, LOGL_INFO, "retransmit last frame "
649 "V(S)=%d\n", sub_mod8(dl->V_send, 1));
650 /* Create I frame (segment) from tx_hist */
651 length = dl->tx_length[sub_mod8(dl->V_send, 1)];
652 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
653 msg->l2h = msgb_put(msg, length);
655 dl->tx_hist[sub_mod8(dl->V_send, 1)],
657 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv,
658 sub_mod8(dl->V_send, 1), 1); /* P=1 */
659 tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr,
660 dl->mctx.link_id, dl->mctx.n201);
662 /* OR send appropriate supervision frame with P=1 */
663 if (!dl->own_busy && !dl->seq_err_cond) {
664 lapdm_send_rr(&dl->mctx, 1);
665 /* NOTE: In case of sequence error
666 * condition, the REJ frame has been
667 * transmitted when entering the
668 * condition, so it has not be done
671 } else if (dl->own_busy) {
672 lapdm_send_rnr(&dl->mctx, 1);
674 LOGP(DLAPDM, LOGL_INFO, "unhandled, "
678 /* restart T200 (PH-READY-TO-SEND) */
679 osmo_timer_schedule(&dl->t200, T200);
681 /* send MDL ERROR INIDCATION to L3 */
682 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
686 LOGP(DLAPDM, LOGL_INFO, "T200 expired in unexpected "
687 "dl->state %u\n", dl->state);
691 /* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
692 static void lapdm_acknowledge(struct lapdm_msg_ctx *mctx)
694 struct lapdm_datalink *dl = mctx->dl;
695 uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl);
696 int s = 0, rej = 0, t200_reset = 0;
699 /* supervisory frame ? */
700 if (LAPDm_CTRL_is_S(mctx->ctrl))
703 if (s && LAPDm_CTRL_S_BITS(mctx->ctrl) == LAPDm_S_REJ)
706 /* Flush all transmit buffers of acknowledged frames */
707 for (i = dl->V_ack; i != nr; i = inc_mod8(i)) {
708 if (dl->tx_length[i]) {
709 dl->tx_length[i] = 0;
710 LOGP(DLAPDM, LOGL_INFO, "ack frame %d\n", i);
714 if (dl->state != LAPDm_STATE_TIMER_RECOV) {
715 /* When not in the timer recovery condition, the data
716 * link layer entity shall reset the timer T200 on
717 * receipt of a valid I frame with N(R) higher than V(A),
718 * or an REJ with an N(R) equal to V(A). */
719 if ((!rej && nr != dl->V_ack)
720 || (rej && nr == dl->V_ack)) {
721 LOGP(DLAPDM, LOGL_INFO, "reset t200\n");
723 osmo_timer_del(&dl->t200);
724 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
726 /* 5.7.4: N(R) sequence error
727 * N(R) is called valid, if and only if
728 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
730 if (sub_mod8(nr, dl->V_ack) > sub_mod8(dl->V_send, dl->V_ack)) {
731 LOGP(DLAPDM, LOGL_NOTICE, "N(R) sequence error\n");
732 rsl_rll_error(RLL_CAUSE_SEQ_ERR, mctx);
736 /* V(A) shall be set to the value of N(R) */
739 /* If T200 has been reset by the receipt of an I, RR or RNR frame,
740 * and if there are outstanding I frames, restart T200 */
741 if (t200_reset && !rej) {
742 if (dl->tx_length[dl->V_send - 1]) {
743 LOGP(DLAPDM, LOGL_INFO, "start T200, due to unacked I "
745 osmo_timer_schedule(&dl->t200, T200);
752 /* Receive a LAPDm U (Unnumbered) message from L1 */
753 static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx)
755 struct lapdm_datalink *dl = mctx->dl;
756 struct lapdm_entity *le = dl->entity;
761 switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) {
763 rsl_msg = RSL_MT_EST_IND;
765 LOGP(DLAPDM, LOGL_INFO, "SABM received\n");
767 dl->seq_err_cond = 0;
768 /* G.2.2 Wrong value of the C/R bit */
769 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) {
770 LOGP(DLAPDM, LOGL_NOTICE, "SABM response error\n");
772 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
776 length = msg->l2h[2] >> 2;
777 /* G.4.5 If SABM is received with L>N201 or with M bit
778 * set, AN MDL-ERROR-INDICATION is sent to MM.
780 if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) {
781 LOGP(DLAPDM, LOGL_NOTICE, "SABM too large error\n");
783 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
787 /* Must be Format B */
788 rc = check_length_ind(mctx, msg->l2h[2]);
794 case LAPDm_STATE_IDLE:
795 /* Set chan_nr and link_id for established connection */
796 memset(&dl->mctx, 0, sizeof(dl->mctx));
798 dl->mctx.chan_nr = mctx->chan_nr;
799 dl->mctx.link_id = mctx->link_id;
800 dl->mctx.n201 = mctx->n201;
802 case LAPDm_STATE_MF_EST:
804 rsl_msg = RSL_MT_EST_CONF;
807 LOGP(DLAPDM, LOGL_INFO, "SABM command, multiple "
808 "frame established state\n");
809 /* check for contention resoultion */
810 if (dl->tx_hist[0][2] >> 2) {
811 LOGP(DLAPDM, LOGL_NOTICE, "SABM not allowed "
812 "during contention resolution\n");
813 rsl_rll_error(RLL_CAUSE_SABM_INFO_NOTALL, mctx);
817 case LAPDm_STATE_DISC_SENT:
818 /* 5.4.6.2 send DM with F=P */
820 /* reset Timer T200 */
821 osmo_timer_del(&dl->t200);
823 return send_rll_simple(RSL_MT_REL_CONF, mctx);
825 lapdm_send_ua(mctx, length, msg->l2h + 3);
829 /* send UA response */
830 lapdm_send_ua(mctx, length, msg->l2h + 3);
831 /* set Vs, Vr and Va to 0 */
832 dl->V_send = dl->V_recv = dl->V_ack = 0;
834 dl->tx_length[0] = 0;
835 /* enter multiple-frame-established state */
836 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
837 /* send notification to L3 */
839 /* 5.4.1.2 Normal establishment procedures */
840 rc = send_rll_simple(rsl_msg, mctx);
843 /* 5.4.1.4 Contention resolution establishment */
844 msg->l3h = msg->l2h + 3;
846 rc = send_rslms_rll_l3(rsl_msg, mctx, msg);
850 LOGP(DLAPDM, LOGL_INFO, "DM received\n");
851 /* G.2.2 Wrong value of the C/R bit */
852 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) {
853 LOGP(DLAPDM, LOGL_NOTICE, "DM command error\n");
855 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
858 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
859 /* 5.4.1.2 DM responses with the F bit set to "0"
866 case LAPDm_STATE_SABM_SENT:
868 case LAPDm_STATE_MF_EST:
869 if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1) {
870 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
872 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP, mctx);
874 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
875 "response, multiple frame established "
877 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx);
881 case LAPDm_STATE_TIMER_RECOV:
882 /* DM is normal in case PF = 1 */
883 if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) {
884 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
885 "response, multiple frame established "
887 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx);
892 case LAPDm_STATE_DISC_SENT:
893 /* reset Timer T200 */
894 osmo_timer_del(&dl->t200);
895 /* go to idle state */
896 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
897 rc = send_rll_simple(RSL_MT_REL_CONF, mctx);
900 case LAPDm_STATE_IDLE:
901 /* 5.4.5 all other frame types shall be discarded */
903 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM response! "
909 osmo_timer_del(&dl->t200);
910 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
914 LOGP(DLAPDM, LOGL_INFO, "UI received\n");
915 /* G.2.2 Wrong value of the C/R bit */
916 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) {
917 LOGP(DLAPDM, LOGL_NOTICE, "UI indicates response "
920 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
924 length = msg->l2h[2] >> 2;
925 /* FIXME: G.4.5 If UI is received with L>N201 or with M bit
926 * set, AN MDL-ERROR-INDICATION is sent to MM.
929 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
931 msg->l3h = msg->l2h + 2;
933 rc = check_length_ind(mctx, msg->l2h[2]);
938 length = msg->l2h[2] >> 2;
939 msg->l3h = msg->l2h + 3;
941 /* do some length checks */
943 /* 5.3.3 UI frames received with the length indicator
944 * set to "0" shall be ignored
946 LOGP(DLAPDM, LOGL_INFO, "length=0 (discarding)\n");
950 switch (LAPDm_ADDR_SAPI(mctx->addr)) {
951 case LAPDm_SAPI_NORMAL:
955 /* 5.3.3 UI frames with invalid SAPI values shall be
958 LOGP(DLAPDM, LOGL_INFO, "sapi=%u (discarding)\n",
959 LAPDm_ADDR_SAPI(mctx->addr));
964 rc = send_rslms_rll_l3_ui(mctx, msg);
967 rsl_msg = RSL_MT_REL_IND;
969 LOGP(DLAPDM, LOGL_INFO, "DISC received\n");
971 lapdm_dl_flush_tx(dl);
972 lapdm_dl_flush_send(dl);
974 dl->seq_err_cond = 0;
975 /* G.2.2 Wrong value of the C/R bit */
976 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) {
977 LOGP(DLAPDM, LOGL_NOTICE, "DISC response error\n");
979 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
982 length = msg->l2h[2] >> 2;
983 if (length > 0 || msg->l2h[2] & 0x02) {
984 /* G.4.4 If a DISC or DM frame is received with L>0 or
985 * with the M bit set to "1", an MDL-ERROR-INDICATION
986 * primitive with cause "U frame with incorrect
987 * parameters" is sent to the mobile management entity.
989 LOGP(DLAPDM, LOGL_NOTICE,
990 "U frame iwth incorrect parameters ");
992 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
997 case LAPDm_STATE_IDLE:
998 LOGP(DLAPDM, LOGL_INFO, "DISC in idle state\n");
999 /* send DM with F=P */
1000 return lapdm_send_dm(mctx);
1001 case LAPDm_STATE_SABM_SENT:
1002 LOGP(DLAPDM, LOGL_INFO, "DISC in SABM state\n");
1003 /* 5.4.6.2 send DM with F=P */
1004 lapdm_send_dm(mctx);
1005 /* reset Timer T200 */
1006 osmo_timer_del(&dl->t200);
1007 return send_rll_simple(RSL_MT_REL_IND, mctx);
1008 case LAPDm_STATE_MF_EST:
1009 case LAPDm_STATE_TIMER_RECOV:
1010 LOGP(DLAPDM, LOGL_INFO, "DISC in est state\n");
1012 case LAPDm_STATE_DISC_SENT:
1013 LOGP(DLAPDM, LOGL_INFO, "DISC in disc state\n");
1014 rsl_msg = RSL_MT_REL_CONF;
1017 lapdm_send_ua(mctx, length, msg->l2h + 3);
1020 /* send UA response */
1021 lapdm_send_ua(mctx, length, msg->l2h + 3);
1022 /* reset Timer T200 */
1023 osmo_timer_del(&dl->t200);
1024 /* enter idle state */
1025 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1026 /* send notification to L3 */
1027 rc = send_rll_simple(rsl_msg, mctx);
1030 LOGP(DLAPDM, LOGL_INFO, "UA received\n");
1031 /* G.2.2 Wrong value of the C/R bit */
1032 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) {
1033 LOGP(DLAPDM, LOGL_NOTICE, "UA indicates command "
1036 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1040 length = msg->l2h[2] >> 2;
1041 /* G.4.5 If UA is received with L>N201 or with M bit
1042 * set, AN MDL-ERROR-INDICATION is sent to MM.
1044 if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) {
1045 LOGP(DLAPDM, LOGL_NOTICE, "UA too large error\n");
1047 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
1051 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1052 /* 5.4.1.2 A UA response with the F bit set to "0"
1055 LOGP(DLAPDM, LOGL_INFO, "F=0 (discarding)\n");
1059 switch (dl->state) {
1060 case LAPDm_STATE_SABM_SENT:
1062 case LAPDm_STATE_MF_EST:
1063 case LAPDm_STATE_TIMER_RECOV:
1064 LOGP(DLAPDM, LOGL_INFO, "unsolicited UA response! "
1066 rsl_rll_error(RLL_CAUSE_UNSOL_UA_RESP, mctx);
1069 case LAPDm_STATE_DISC_SENT:
1070 LOGP(DLAPDM, LOGL_INFO, "UA in disconnect state\n");
1071 /* reset Timer T200 */
1072 osmo_timer_del(&dl->t200);
1073 /* go to idle state */
1074 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1075 rc = send_rll_simple(RSL_MT_REL_CONF, mctx);
1078 case LAPDm_STATE_IDLE:
1079 /* 5.4.5 all other frame types shall be discarded */
1081 LOGP(DLAPDM, LOGL_INFO, "unsolicited UA response! "
1086 LOGP(DLAPDM, LOGL_INFO, "UA in SABM state\n");
1087 /* reset Timer T200 */
1088 osmo_timer_del(&dl->t200);
1089 /* compare UA with SABME if contention resolution is applied */
1090 if (dl->tx_hist[0][2] >> 2) {
1091 rc = check_length_ind(mctx, msg->l2h[2]);
1093 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
1095 /* go to idle state */
1096 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1099 length = msg->l2h[2] >> 2;
1100 if (length != (dl->tx_hist[0][2] >> 2)
1101 || !!memcmp(dl->tx_hist[0] + 3, msg->l2h + 3,
1103 LOGP(DLAPDM, LOGL_INFO, "**** UA response "
1104 "mismatches ****\n");
1105 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
1107 /* go to idle state */
1108 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1112 /* set Vs, Vr and Va to 0 */
1113 dl->V_send = dl->V_recv = dl->V_ack = 0;
1115 dl->tx_length[0] = 0;
1116 /* enter multiple-frame-established state */
1117 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1118 /* send outstanding frames, if any (resume / reconnect) */
1119 rslms_send_i(mctx, __LINE__);
1120 /* send notification to L3 */
1121 rc = send_rll_simple(RSL_MT_EST_CONF, mctx);
1126 LOGP(DLAPDM, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1128 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1134 /* Receive a LAPDm S (Supervisory) message from L1 */
1135 static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1137 struct lapdm_datalink *dl = mctx->dl;
1138 struct lapdm_entity *le = dl->entity;
1141 length = msg->l2h[2] >> 2;
1142 if (length > 0 || msg->l2h[2] & 0x02) {
1143 /* G.4.3 If a supervisory frame is received with L>0 or
1144 * with the M bit set to "1", an MDL-ERROR-INDICATION
1145 * primitive with cause "S frame with incorrect
1146 * parameters" is sent to the mobile management entity. */
1147 LOGP(DLAPDM, LOGL_NOTICE,
1148 "S frame with incorrect parameters\n");
1150 rsl_rll_error(RLL_CAUSE_SFRM_INC_PARAM, mctx);
1154 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp
1155 && LAPDm_CTRL_PF_BIT(mctx->ctrl)
1156 && dl->state != LAPDm_STATE_TIMER_RECOV) {
1157 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1158 LOGP(DLAPDM, LOGL_NOTICE, "S frame response with F=1 error\n");
1159 rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
1162 switch (dl->state) {
1163 case LAPDm_STATE_IDLE:
1164 /* if P=1, respond DM with F=1 (5.2.2) */
1165 /* 5.4.5 all other frame types shall be discarded */
1166 if (LAPDm_CTRL_PF_BIT(mctx->ctrl))
1167 lapdm_send_dm(mctx); /* F=P */
1169 case LAPDm_STATE_SABM_SENT:
1170 case LAPDm_STATE_DISC_SENT:
1171 LOGP(DLAPDM, LOGL_NOTICE, "S frame ignored in this state\n");
1175 switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) {
1177 LOGP(DLAPDM, LOGL_INFO, "RR received\n");
1178 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1179 lapdm_acknowledge(mctx);
1182 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd
1183 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1184 if (!dl->own_busy && !dl->seq_err_cond) {
1185 LOGP(DLAPDM, LOGL_NOTICE, "RR frame command "
1186 "with polling bit set and we are not "
1187 "busy, so we reply with RR frame\n");
1188 lapdm_send_rr(mctx, 1);
1189 /* NOTE: In case of sequence error condition,
1190 * the REJ frame has been transmitted when
1191 * entering the condition, so it has not be
1194 } else if (dl->own_busy) {
1195 LOGP(DLAPDM, LOGL_NOTICE, "RR frame command "
1196 "with polling bit set and we are busy, "
1197 "so we reply with RR frame\n");
1198 lapdm_send_rnr(mctx, 1);
1200 } else if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp
1201 && LAPDm_CTRL_PF_BIT(mctx->ctrl)
1202 && dl->state == LAPDm_STATE_TIMER_RECOV) {
1203 LOGP(DLAPDM, LOGL_INFO, "RR response with F==1, "
1204 "and we are in timer recovery state, so "
1205 "we leave that state\n");
1206 /* V(S) to the N(R) in the RR frame */
1207 dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl);
1208 /* reset Timer T200 */
1209 osmo_timer_del(&dl->t200);
1210 /* 5.5.7 Clear timer recovery condition */
1211 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1213 /* Send message, if possible due to acknowledged data */
1214 rslms_send_i(mctx, __LINE__);
1218 LOGP(DLAPDM, LOGL_INFO, "RNR received\n");
1219 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1220 lapdm_acknowledge(mctx);
1223 /* Set peer receiver busy condition */
1226 if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1227 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) {
1228 if (!dl->own_busy) {
1229 LOGP(DLAPDM, LOGL_INFO, "RNR poll "
1230 "command and we are not busy, "
1231 "so we reply with RR final "
1233 /* Send RR with F=1 */
1234 lapdm_send_rr(mctx, 1);
1236 LOGP(DLAPDM, LOGL_INFO, "RNR poll "
1237 "command and we are busy, so "
1238 "we reply with RNR final "
1240 /* Send RNR with F=1 */
1241 lapdm_send_rnr(mctx, 1);
1243 } else if (dl->state == LAPDm_STATE_TIMER_RECOV) {
1244 LOGP(DLAPDM, LOGL_INFO, "RNR poll response "
1245 "and we in timer recovery state, so "
1246 "we leave that state\n");
1247 /* 5.5.7 Clear timer recovery condition */
1248 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1249 /* V(S) to the N(R) in the RNR frame */
1250 dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl);
1253 LOGP(DLAPDM, LOGL_INFO, "RNR not polling/final state "
1256 /* Send message, if possible due to acknowledged data */
1257 rslms_send_i(mctx, __LINE__);
1261 LOGP(DLAPDM, LOGL_INFO, "REJ received\n");
1262 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1263 lapdm_acknowledge(mctx);
1266 if (dl->state != LAPDm_STATE_TIMER_RECOV) {
1267 /* Clear an existing peer receiver busy condition */
1269 /* V(S) and V(A) to the N(R) in the REJ frame */
1270 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1271 /* reset Timer T200 */
1272 osmo_timer_del(&dl->t200);
1274 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd
1275 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1276 if (!dl->own_busy && !dl->seq_err_cond) {
1277 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1278 "command not in timer recovery "
1279 "state and not in own busy "
1280 "condition received, so we "
1281 "respond with RR final "
1283 lapdm_send_rr(mctx, 1);
1284 /* NOTE: In case of sequence error
1285 * condition, the REJ frame has been
1286 * transmitted when entering the
1287 * condition, so it has not be done
1290 } else if (dl->own_busy) {
1291 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1292 "command not in timer recovery "
1293 "state and in own busy "
1294 "condition received, so we "
1295 "respond with RNR final "
1297 lapdm_send_rnr(mctx, 1);
1300 LOGP(DLAPDM, LOGL_INFO, "REJ response or not "
1301 "polling command not in timer recovery "
1302 "state received\n");
1303 /* send MDL ERROR INIDCATION to L3 */
1304 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp
1305 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1306 rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
1309 } else if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp
1310 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1311 LOGP(DLAPDM, LOGL_INFO, "REJ poll response in timer "
1312 "recovery state received\n");
1313 /* Clear an existing peer receiver busy condition */
1315 /* 5.5.7 Clear timer recovery condition */
1316 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1317 /* V(S) and V(A) to the N(R) in the REJ frame */
1318 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1319 /* reset Timer T200 */
1320 osmo_timer_del(&dl->t200);
1322 /* Clear an existing peer receiver busy condition */
1324 /* V(S) and V(A) to the N(R) in the REJ frame */
1325 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1327 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd
1328 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1329 if (!dl->own_busy && !dl->seq_err_cond) {
1330 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1331 "command in timer recovery "
1332 "state and not in own busy "
1333 "condition received, so we "
1334 "respond with RR final "
1336 lapdm_send_rr(mctx, 1);
1337 /* NOTE: In case of sequence error
1338 * condition, the REJ frame has been
1339 * transmitted when entering the
1340 * condition, so it has not be done
1343 } else if (dl->own_busy) {
1344 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1345 "command in timer recovery "
1346 "state and in own busy "
1347 "condition received, so we "
1348 "respond with RNR final "
1350 lapdm_send_rnr(mctx, 1);
1353 LOGP(DLAPDM, LOGL_INFO, "REJ response or not "
1354 "polling command in timer recovery "
1355 "state received\n");
1358 /* FIXME: 5.5.4.2 2) */
1360 /* Send message, if possible due to acknowledged data */
1361 rslms_send_i(mctx, __LINE__);
1366 LOGP(DLAPDM, LOGL_NOTICE, "Supervisory frame not allowed.\n");
1368 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1375 /* Receive a LAPDm I (Information) message from L1 */
1376 static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1378 struct lapdm_datalink *dl = mctx->dl;
1379 struct lapdm_entity *le = dl->entity;
1380 //uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl);
1381 uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl);
1385 LOGP(DLAPDM, LOGL_NOTICE, "I received\n");
1387 /* G.2.2 Wrong value of the C/R bit */
1388 if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) {
1389 LOGP(DLAPDM, LOGL_NOTICE, "I frame response not allowed\n");
1391 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1395 length = msg->l2h[2] >> 2;
1396 if (length == 0 || length + 3 > mctx->n201) {
1397 /* G.4.2 If the length indicator of an I frame is set
1398 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1399 * primitive with cause "I frame with incorrect length"
1400 * is sent to the mobile management entity. */
1401 LOGP(DLAPDM, LOGL_NOTICE, "I frame length not allowed\n");
1403 rsl_rll_error(RLL_CAUSE_IFRM_INC_LEN, mctx);
1407 /* G.4.2 If the numerical value of L is L<N201 and the M
1408 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1409 * cause "I frame with incorrect use of M bit" is sent to the
1410 * mobile management entity. */
1411 if ((msg->l2h[2] & LAPDm_MORE) && length + 3 < mctx->n201) {
1412 LOGP(DLAPDM, LOGL_NOTICE, "I frame with M bit too short\n");
1414 rsl_rll_error(RLL_CAUSE_IFRM_INC_MBITS, mctx);
1418 switch (dl->state) {
1419 case LAPDm_STATE_IDLE:
1420 /* if P=1, respond DM with F=1 (5.2.2) */
1421 /* 5.4.5 all other frame types shall be discarded */
1422 if (LAPDm_CTRL_PF_BIT(mctx->ctrl))
1423 lapdm_send_dm(mctx); /* F=P */
1425 case LAPDm_STATE_SABM_SENT:
1426 case LAPDm_STATE_DISC_SENT:
1427 LOGP(DLAPDM, LOGL_NOTICE, "I frame ignored in this state\n");
1432 /* 5.7.1: N(s) sequence error */
1433 if (ns != dl->V_recv) {
1434 LOGP(DLAPDM, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1435 "V(R)=%u\n", ns, dl->V_recv);
1438 if (!dl->seq_err_cond) {
1439 /* FIXME: help me understand what exactly todo here
1440 dl->seq_err_cond = 1;
1442 lapdm_send_rej(mctx, LAPDm_CTRL_PF_BIT(mctx->ctrl));
1447 dl->seq_err_cond = 0;
1449 /* Increment receiver state */
1450 dl->V_recv = inc_mod8(dl->V_recv);
1451 LOGP(DLAPDM, LOGL_NOTICE, "incrementing V(R) to %u\n", dl->V_recv);
1453 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1454 lapdm_acknowledge(mctx); /* V(A) is also set here */
1456 /* Only if we are not in own receiver busy condition */
1457 if (!dl->own_busy) {
1458 /* if the frame carries a complete segment */
1459 if (!(msg->l2h[2] & LAPDm_MORE)
1460 && !dl->rcv_buffer) {
1461 LOGP(DLAPDM, LOGL_INFO, "message in single I frame\n");
1462 /* send a DATA INDICATION to L3 */
1463 msg->l3h = msg->l2h + 3;
1466 msg->tail = msg->data + length;
1467 rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg);
1469 /* create rcv_buffer */
1470 if (!dl->rcv_buffer) {
1471 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1472 "frames (first message)\n");
1473 dl->rcv_buffer = msgb_alloc_headroom(200+56, 56,
1475 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1477 /* concat. rcv_buffer */
1478 if (msgb_l3len(dl->rcv_buffer) + length > 200) {
1479 LOGP(DLAPDM, LOGL_NOTICE, "Received frame "
1482 memcpy(msgb_put(dl->rcv_buffer, length),
1483 msg->l2h + 3, length);
1485 /* if the last segment was received */
1486 if (!(msg->l2h[2] & LAPDm_MORE)) {
1487 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1488 "frames (last message)\n");
1489 rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx,
1491 dl->rcv_buffer = NULL;
1493 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1494 "frames (next message)\n");
1499 LOGP(DLAPDM, LOGL_INFO, "I frame ignored during own receiver "
1500 "busy condition\n");
1502 /* Check for P bit */
1503 if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1505 /* check if we are not in own receiver busy */
1506 if (!dl->own_busy) {
1507 LOGP(DLAPDM, LOGL_INFO, "we are not busy, send RR\n");
1508 /* Send RR with F=1 */
1509 rc = lapdm_send_rr(mctx, 1);
1511 LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
1512 /* Send RNR with F=1 */
1513 rc = lapdm_send_rnr(mctx, 1);
1517 /* check if we are not in own receiver busy */
1518 if (!dl->own_busy) {
1519 /* NOTE: V(R) is already set above */
1520 rc = rslms_send_i(mctx, __LINE__);
1522 LOGP(DLAPDM, LOGL_INFO, "we are not busy and "
1523 "have no pending data, send RR\n");
1524 /* Send RR with F=0 */
1525 return lapdm_send_rr(mctx, 0);
1527 /* all I or one RR is sent, we are done */
1530 LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
1531 /* Send RNR with F=0 */
1532 rc = lapdm_send_rnr(mctx, 0);
1536 /* Send message, if possible due to acknowledged data */
1537 rslms_send_i(mctx, __LINE__);
1542 /* Receive a LAPDm message from L1 */
1543 static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1547 /* G.2.3 EA bit set to "0" is not allowed in GSM */
1548 if (!LAPDm_ADDR_EA(mctx->addr)) {
1549 LOGP(DLAPDM, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
1551 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1555 if (LAPDm_CTRL_is_U(mctx->ctrl))
1556 rc = lapdm_rx_u(msg, mctx);
1557 else if (LAPDm_CTRL_is_S(mctx->ctrl))
1558 rc = lapdm_rx_s(msg, mctx);
1559 else if (LAPDm_CTRL_is_I(mctx->ctrl))
1560 rc = lapdm_rx_i(msg, mctx);
1562 LOGP(DLAPDM, LOGL_NOTICE, "unknown LAPDm format\n");
1569 /* input into layer2 (from layer 1) */
1570 static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, uint8_t chan_nr, uint8_t link_id)
1572 uint8_t cbits = chan_nr >> 3;
1573 uint8_t sapi = link_id & 7;
1574 struct lapdm_msg_ctx mctx;
1577 /* when we reach here, we have a msgb with l2h pointing to the raw
1578 * 23byte mac block. The l1h has already been purged. */
1580 mctx.dl = datalink_for_sapi(le, sapi);
1581 mctx.chan_nr = chan_nr;
1582 mctx.link_id = link_id;
1583 mctx.addr = mctx.ctrl = 0;
1585 /* G.2.1 No action schall be taken on frames containing an unallocated
1589 LOGP(DLAPDM, LOGL_NOTICE, "Received frame for unsupported "
1590 "SAPI %d!\n", sapi);
1596 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
1597 if (cbits == 0x10 || cbits == 0x12) {
1598 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
1599 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
1600 mctx.n201 = N201_Bbis;
1602 if (mctx.link_id & 0x40) {
1603 /* It was received from network on SACCH, thus
1604 * lapdm_fmt must be B4 */
1605 mctx.lapdm_fmt = LAPDm_FMT_B4;
1606 mctx.n201 = N201_B4;
1607 LOGP(DLAPDM, LOGL_INFO, "fmt=B4\n");
1608 /* SACCH frames have a two-byte L1 header that
1609 * OsmocomBB L1 doesn't strip */
1610 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
1611 mctx.ta_ind = msg->l2h[1];
1615 mctx.lapdm_fmt = LAPDm_FMT_B;
1616 LOGP(DLAPDM, LOGL_INFO, "fmt=B\n");
1617 mctx.n201 = 23; // FIXME: select correct size by chan.
1621 switch (mctx.lapdm_fmt) {
1625 mctx.addr = msg->l2h[0];
1626 if (!(mctx.addr & 0x01)) {
1627 LOGP(DLAPDM, LOGL_ERROR, "we don't support "
1628 "multibyte addresses (discarding)\n");
1632 mctx.ctrl = msg->l2h[1];
1633 /* obtain SAPI from address field */
1634 mctx.link_id |= LAPDm_ADDR_SAPI(mctx.addr);
1635 rc = lapdm_ph_data_ind(msg, &mctx);
1637 case LAPDm_FMT_Bter:
1641 case LAPDm_FMT_Bbis:
1642 /* directly pass up to layer3 */
1643 LOGP(DLAPDM, LOGL_INFO, "fmt=Bbis UI\n");
1644 msg->l3h = msg->l2h;
1646 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
1655 /* input into layer2 (from layer 1) */
1656 static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
1658 struct abis_rsl_cchan_hdr *ch;
1659 struct gsm48_req_ref req_ref;
1661 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
1663 msg->l2h = msgb_push(msg, sizeof(*ch));
1664 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1665 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
1666 ch->chan_nr = RSL_CHAN_RACH;
1668 /* generate a RSL CHANNEL REQUIRED message */
1669 gsm_fn2gsmtime(>, fn);
1671 req_ref.t1 = gt.t1; /* FIXME: modulo? */
1673 req_ref.t3_low = gt.t3 & 7;
1674 req_ref.t3_high = gt.t3 >> 3;
1676 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
1677 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
1679 return rslms_sendmsg(msg, le);
1682 static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
1684 int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
1686 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
1689 if (oph->sap != SAP_GSM_PH) {
1690 LOGP(DLAPDM, LOGL_ERROR, "primitive for unknown SAP %u\n",
1695 switch (oph->primitive) {
1697 if (oph->operation != PRIM_OP_INDICATION) {
1698 LOGP(DLAPDM, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
1702 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
1703 pp->u.data.link_id);
1706 if (oph->operation != PRIM_OP_INDICATION) {
1707 LOGP(DLAPDM, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
1711 rc = l2_ph_data_conf(oph->msg, le);
1714 switch (oph->operation) {
1715 case PRIM_OP_INDICATION:
1716 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
1717 pp->u.rach_ind.acc_delay);
1719 case PRIM_OP_CONFIRM:
1720 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
1732 /* L3 -> L2 / RSLMS -> LAPDm */
1734 /* L3 requests establishment of data link */
1735 static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
1737 struct lapdm_entity *le = dl->entity;
1738 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1739 uint8_t chan_nr = rllh->chan_nr;
1740 uint8_t link_id = rllh->link_id;
1741 uint8_t sapi = rllh->link_id & 7;
1742 struct tlv_parsed tv;
1744 uint8_t n201 = 23; //FIXME
1746 /* Set chan_nr and link_id for established connection */
1747 memset(&dl->mctx, 0, sizeof(dl->mctx));
1749 dl->mctx.n201 = n201;
1750 dl->mctx.chan_nr = chan_nr;
1751 dl->mctx.link_id = link_id;
1753 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1754 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1755 /* contention resolution establishment procedure */
1757 /* According to clause 6, the contention resolution
1758 * procedure is only permitted with SAPI value 0 */
1759 LOGP(DLAPDM, LOGL_ERROR, "SAPI != 0 but contention"
1760 "resolution (discarding)\n");
1762 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1764 /* transmit a SABM command with the P bit set to "1". The SABM
1765 * command shall contain the layer 3 message unit */
1766 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1767 LOGP(DLAPDM, LOGL_INFO, "perform establishment with content "
1770 /* normal establishment procedure */
1772 LOGP(DLAPDM, LOGL_INFO, "perform normal establishm. (SABM)\n");
1775 /* check if the layer3 message length exceeds N201 */
1776 if (length + 3 > 21) { /* FIXME: do we know the channel N201? */
1777 LOGP(DLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) "
1778 "(discarding)\n", length + 3, 21);
1780 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1783 /* Flush send-queue */
1784 /* Clear send-buffer */
1785 lapdm_dl_flush_send(dl);
1787 /* Discard partly received L3 message */
1788 if (dl->rcv_buffer) {
1789 msgb_free(dl->rcv_buffer);
1790 dl->rcv_buffer = NULL;
1793 /* Remove RLL header from msgb */
1796 /* Push LAPDm header on msgb */
1797 msg->l2h = msgb_push(msg, 3);
1798 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd);
1799 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
1800 msg->l2h[2] = LAPDm_LEN(length);
1801 /* Transmit-buffer carries exactly one segment */
1802 memcpy(dl->tx_hist[0], msg->l2h, 3 + length);
1803 dl->tx_length[0] = 3 + length;
1804 /* set Vs to 0, because it is used as index when resending SABM */
1808 dl->own_busy = dl->peer_busy = 0;
1809 dl->retrans_ctr = 0;
1810 lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT);
1812 /* Tramsmit and start T200 */
1813 osmo_timer_schedule(&dl->t200, T200);
1814 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1817 /* L3 requests transfer of unnumbered information */
1818 static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1820 struct lapdm_entity *le = dl->entity;
1821 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1822 uint8_t chan_nr = rllh->chan_nr;
1823 uint8_t link_id = rllh->link_id;
1824 uint8_t sapi = link_id & 7;
1825 struct tlv_parsed tv;
1827 uint8_t n201 = 23; //FIXME
1828 uint8_t ta = 0, tx_power = 0;
1830 /* check if the layer3 message length exceeds N201 */
1832 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1834 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
1835 ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
1837 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
1838 tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
1840 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1841 LOGP(DLAPDM, LOGL_ERROR, "unit data request without message "
1846 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1847 /* check if the layer3 message length exceeds N201 */
1848 if (length + 5 > 23) { /* FIXME: do we know the channel N201? */
1849 LOGP(DLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) "
1850 "(discarding)\n", length + 5, 23);
1855 LOGP(DLAPDM, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
1858 /* Remove RLL header from msgb */
1861 /* Push L1 + LAPDm header on msgb */
1862 msg->l2h = msgb_push(msg, 2 + 3);
1863 msg->l2h[0] = tx_power;
1865 msg->l2h[2] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd);
1866 msg->l2h[3] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
1867 msg->l2h[4] = LAPDm_LEN(length);
1868 // FIXME: short L2 header support
1871 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1874 /* L3 requests transfer of acknowledged information */
1875 static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1877 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1878 struct tlv_parsed tv;
1880 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1881 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1882 LOGP(DLAPDM, LOGL_ERROR, "data request without message "
1888 LOGP(DLAPDM, LOGL_INFO, "writing message to send-queue\n");
1890 /* Remove the RSL/RLL header */
1893 /* Write data into the send queue */
1894 msgb_enqueue(&dl->send_queue, msg);
1896 /* Send message, if possible */
1897 rslms_send_i(&dl->mctx, __LINE__);
1901 /* Send next I frame from queued/buffered data */
1902 static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line)
1904 struct lapdm_datalink *dl = mctx->dl;
1905 struct lapdm_entity *le = dl->entity;
1906 uint8_t chan_nr = mctx->chan_nr;
1907 uint8_t link_id = mctx->link_id;
1908 uint8_t sapi = link_id & 7;
1909 int k = k_sapi[sapi];
1912 int rc = - 1; /* we sent nothing */
1914 LOGP(DLAPDM, LOGL_INFO, "%s() called from line %d\n", __func__, line);
1918 if (dl->peer_busy) {
1919 LOGP(DLAPDM, LOGL_INFO, "peer busy, not sending\n");
1923 if (dl->state == LAPDm_STATE_TIMER_RECOV) {
1924 LOGP(DLAPDM, LOGL_INFO, "timer recovery, not sending\n");
1928 /* If the send state variable V(S) is equal to V(A) plus k
1929 * (where k is the maximum number of outstanding I frames - see
1930 * subclause 5.8.4), the data link layer entity shall not transmit any
1931 * new I frames, but shall retransmit an I frame as a result
1932 * of the error recovery procedures as described in subclauses 5.5.4 and
1934 if (dl->V_send == add_mod8(dl->V_ack, k)) {
1935 LOGP(DLAPDM, LOGL_INFO, "k frames outstanding, not sending "
1936 "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->V_send,
1941 /* if we have no tx_hist yet, we create it */
1942 if (!dl->tx_length[dl->V_send]) {
1943 /* Get next message into send-buffer, if any */
1944 if (!dl->send_buffer) {
1947 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1948 /* No more data to be sent */
1949 if (!dl->send_buffer)
1951 LOGP(DLAPDM, LOGL_INFO, "get message from "
1955 /* How much is left in the send-buffer? */
1956 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1957 /* Segment, if data exceeds N201 */
1959 if (length > mctx->n201 - 3)
1960 length = mctx->n201 - 3;
1961 LOGP(DLAPDM, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
1962 "length %d first byte %02x\n",
1963 msgb_l3len(dl->send_buffer), dl->send_out, left,
1964 mctx->n201, length, dl->send_buffer->l3h[0]);
1965 /* If message in send-buffer is completely sent */
1967 msgb_free(dl->send_buffer);
1968 dl->send_buffer = NULL;
1972 LOGP(DLAPDM, LOGL_INFO, "send I frame %sV(S)=%d\n",
1973 (left > length) ? "segment " : "", dl->V_send);
1975 /* Create I frame (segment) and transmit-buffer content */
1976 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
1977 msg->l2h = msgb_put(msg, 3 + length);
1978 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd);
1979 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
1980 msg->l2h[2] = LAPDm_LEN(length);
1982 msg->l2h[2] |= LAPDm_MORE;
1983 memcpy(msg->l2h + 3, dl->send_buffer->l3h + dl->send_out,
1985 memcpy(dl->tx_hist[dl->V_send], msg->l2h, 3 + length);
1986 dl->tx_length[dl->V_send] = 3 + length;
1987 /* Add length to track how much is already in the tx buffer */
1988 dl->send_out += length;
1990 LOGP(DLAPDM, LOGL_INFO, "resend I frame from tx buffer "
1991 "V(S)=%d\n", dl->V_send);
1993 /* Create I frame (segment) from tx_hist */
1994 length = dl->tx_length[dl->V_send];
1995 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
1996 msg->l2h = msgb_put(msg, length);
1997 memcpy(msg->l2h, dl->tx_hist[dl->V_send], length);
1998 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
2001 /* The value of the send state variable V(S) shall be incremented by 1
2002 * at the end of the transmission of the I frame */
2003 dl->V_send = inc_mod8(dl->V_send);
2005 /* If timer T200 is not running at the time right before transmitting a
2006 * frame, when the PH-READY-TO-SEND primitive is received from the
2007 * physical layer., it shall be set. */
2008 if (!osmo_timer_pending(&dl->t200))
2009 osmo_timer_schedule(&dl->t200, T200);
2011 tx_ph_data_enqueue(dl, msg, chan_nr, link_id, mctx->n201);
2013 rc = 0; /* we sent something */
2017 /* L3 requests suspension of data link */
2018 static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
2020 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2021 uint8_t sapi = rllh->link_id & 7;
2024 LOGP(DLAPDM, LOGL_ERROR, "SAPI != 0 while suspending\n");
2029 LOGP(DLAPDM, LOGL_INFO, "perform suspension\n");
2031 /* put back the send-buffer to the send-queue (first position) */
2032 if (dl->send_buffer) {
2033 LOGP(DLAPDM, LOGL_INFO, "put frame in sendbuffer back to "
2035 llist_add(&dl->send_buffer->list, &dl->send_queue);
2036 dl->send_buffer = NULL;
2038 LOGP(DLAPDM, LOGL_INFO, "no frame in sendbuffer\n");
2040 /* Clear transmit buffer, but keep send buffer */
2041 lapdm_dl_flush_tx(dl);
2045 return send_rll_simple(RSL_MT_SUSP_CONF, &dl->mctx);
2048 /* L3 requests resume of data link */
2049 static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
2051 struct lapdm_entity *le = dl->entity;
2052 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2053 uint8_t chan_nr = rllh->chan_nr;
2054 uint8_t link_id = rllh->link_id;
2055 uint8_t sapi = rllh->link_id & 7;
2056 struct tlv_parsed tv;
2058 uint8_t n201 = 23; //FIXME
2060 /* Set chan_nr and link_id for established connection */
2061 memset(&dl->mctx, 0, sizeof(dl->mctx));
2063 dl->mctx.n201 = n201;
2064 dl->mctx.chan_nr = chan_nr;
2065 dl->mctx.link_id = link_id;
2067 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
2068 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
2069 LOGP(DLAPDM, LOGL_ERROR, "resume without message error\n");
2071 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
2073 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
2075 LOGP(DLAPDM, LOGL_INFO, "perform re-establishment (SABM) length=%d\n",
2078 /* Replace message in the send-buffer (reconnect) */
2079 if (dl->send_buffer)
2080 msgb_free(dl->send_buffer);
2083 /* Remove the RSL/RLL header */
2085 /* Write data into the send buffer, to be sent first */
2086 dl->send_buffer = msg;
2089 /* Discard partly received L3 message */
2090 if (dl->rcv_buffer) {
2091 msgb_free(dl->rcv_buffer);
2092 dl->rcv_buffer = NULL;
2095 /* Create new msgb (old one is now free) */
2096 msg = msgb_alloc_headroom(23+10, 10, "LAPDm SABM");
2097 msg->l2h = msgb_put(msg, 3);
2098 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd);
2099 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
2100 msg->l2h[2] = LAPDm_LEN(0);
2101 /* Transmit-buffer carries exactly one segment */
2102 memcpy(dl->tx_hist[0], msg->l2h, 3);
2103 dl->tx_length[0] = 3;
2104 /* set Vs to 0, because it is used as index when resending SABM */
2108 dl->own_busy = dl->peer_busy = 0;
2109 dl->retrans_ctr = 0;
2110 lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT);
2112 /* Tramsmit and start T200 */
2113 osmo_timer_schedule(&dl->t200, T200);
2114 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
2117 /* L3 requests release of data link */
2118 static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
2120 struct lapdm_entity *le = dl->entity;
2121 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2122 uint8_t chan_nr = rllh->chan_nr;
2123 uint8_t link_id = rllh->link_id;
2124 uint8_t sapi = rllh->link_id & 7;
2127 /* get release mode */
2128 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
2129 mode = rllh->data[1] & 1;
2133 LOGP(DLAPDM, LOGL_INFO, "perform local release\n");
2135 /* reset Timer T200 */
2136 osmo_timer_del(&dl->t200);
2137 /* enter idle state */
2138 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
2140 lapdm_dl_flush_tx(dl);
2141 lapdm_dl_flush_send(dl);
2142 /* send notification to L3 */
2143 return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
2146 /* in case we are already disconnecting */
2147 if (dl->state == LAPDm_STATE_DISC_SENT)
2150 LOGP(DLAPDM, LOGL_INFO, "perform normal release (DISC)\n");
2153 msgb_pull(msg, msg->tail - msg->l2h);
2155 /* Push LAPDm header on msgb */
2156 msg->l2h = msgb_push(msg, 3);
2157 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.cmd);
2158 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DISC, 1);
2159 msg->l2h[2] = LAPDm_LEN(0);
2160 /* Transmit-buffer carries exactly one segment */
2161 memcpy(dl->tx_hist[0], msg->l2h, 3);
2162 dl->tx_length[0] = 3;
2165 dl->own_busy = dl->peer_busy = 0;
2166 dl->retrans_ctr = 0;
2167 lapdm_dl_newstate(dl, LAPDm_STATE_DISC_SENT);
2169 /* Tramsmit and start T200 */
2170 osmo_timer_schedule(&dl->t200, T200);
2171 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, dl->mctx.n201);
2174 /* L3 requests release in idle state */
2175 static int rslms_rx_rll_rel_req_idle(struct msgb *msg, struct lapdm_datalink *dl)
2179 /* send notification to L3 */
2180 return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
2183 /* L3 requests channel in idle state */
2184 static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
2186 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
2187 void *l1ctx = lc->lapdm_dcch.l1_ctx;
2188 struct osmo_phsap_prim pp;
2190 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
2191 PRIM_OP_REQUEST, NULL);
2193 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
2194 LOGP(DRSL, LOGL_ERROR, "Message too short for CHAN RQD!\n");
2197 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
2198 LOGP(DRSL, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
2201 pp.u.rach_req.ra = cch->data[1];
2202 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
2203 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
2205 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
2206 LOGP(DRSL, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
2209 /* TA = 0 - delay */
2210 pp.u.rach_req.ta = 0 - cch->data[5];
2212 if (cch->data[6] != RSL_IE_MS_POWER) {
2213 LOGP(DRSL, LOGL_ERROR, "Missing MS POWER IE\n");
2216 pp.u.rach_req.tx_power = cch->data[7];
2220 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
2223 /* L1 confirms channel request */
2224 static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
2226 struct abis_rsl_cchan_hdr *ch;
2228 struct gsm48_req_ref *ref;
2230 gsm_fn2gsmtime(&tm, frame_nr);
2233 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
2234 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
2235 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
2236 ch->chan_nr = RSL_CHAN_RACH;
2237 ch->data[0] = RSL_IE_REQ_REFERENCE;
2238 ref = (struct gsm48_req_ref *) (ch->data + 1);
2241 ref->t3_low = tm.t3 & 0x7;
2242 ref->t3_high = tm.t3 >> 3;
2244 return rslms_sendmsg(msg, le);
2247 const char *lapdm_state_names[] = {
2250 "LAPDm_STATE_SABM_SENT",
2251 "LAPDm_STATE_MF_EST",
2252 "LAPDm_STATE_TIMER_RECOV",
2253 "LAPDm_STATE_DISC_SENT",
2256 /* statefull handling for RSLms RLL messages from L3 */
2257 static struct l2downstate {
2260 int (*rout) (struct msgb *msg, struct lapdm_datalink *dl);
2261 } l2downstatelist[] = {
2262 /* create and send UI command */
2264 RSL_MT_UNIT_DATA_REQ, rslms_rx_rll_udata_req},
2266 /* create and send SABM command */
2267 {SBIT(LAPDm_STATE_IDLE),
2268 RSL_MT_EST_REQ, rslms_rx_rll_est_req},
2270 /* create and send I command */
2271 {SBIT(LAPDm_STATE_MF_EST) |
2272 SBIT(LAPDm_STATE_TIMER_RECOV),
2273 RSL_MT_DATA_REQ, rslms_rx_rll_data_req},
2275 /* suspend datalink */
2276 {SBIT(LAPDm_STATE_MF_EST) |
2277 SBIT(LAPDm_STATE_TIMER_RECOV),
2278 RSL_MT_SUSP_REQ, rslms_rx_rll_susp_req},
2280 /* create and send SABM command (resume) */
2281 {SBIT(LAPDm_STATE_MF_EST) |
2282 SBIT(LAPDm_STATE_TIMER_RECOV),
2283 RSL_MT_RES_REQ, rslms_rx_rll_res_req},
2285 /* create and send SABM command (reconnect) */
2286 {SBIT(LAPDm_STATE_IDLE) |
2287 SBIT(LAPDm_STATE_MF_EST) |
2288 SBIT(LAPDm_STATE_TIMER_RECOV),
2289 RSL_MT_RECON_REQ, rslms_rx_rll_res_req},
2291 /* create and send DISC command */
2292 {SBIT(LAPDm_STATE_SABM_SENT) |
2293 SBIT(LAPDm_STATE_MF_EST) |
2294 SBIT(LAPDm_STATE_TIMER_RECOV) |
2295 SBIT(LAPDm_STATE_DISC_SENT),
2296 RSL_MT_REL_REQ, rslms_rx_rll_rel_req},
2298 /* release in idle state */
2299 {SBIT(LAPDm_STATE_IDLE),
2300 RSL_MT_REL_REQ, rslms_rx_rll_rel_req_idle},
2303 #define L2DOWNSLLEN \
2304 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2306 /* incoming RSLms RLL message from L3 */
2307 static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
2309 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2310 int msg_type = rllh->c.msg_type;
2311 uint8_t sapi = rllh->link_id & 7;
2312 struct lapdm_entity *le;
2313 struct lapdm_datalink *dl;
2314 int i, supported = 0;
2317 if (msgb_l2len(msg) < sizeof(*rllh)) {
2318 LOGP(DRSL, LOGL_ERROR, "Message too short for RLL hdr!\n");
2322 if (rllh->link_id & 0x40)
2323 le = &lc->lapdm_acch;
2325 le = &lc->lapdm_dcch;
2327 /* G.2.1 No action schall be taken on frames containing an unallocated
2330 dl = datalink_for_sapi(le, sapi);
2332 LOGP(DRSL, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
2336 LOGP(DRSL, LOGL_INFO, "(%p) RLL Message '%s' received in state %s\n",
2337 lc->name, rsl_msg_name(msg_type), lapdm_state_names[dl->state]);
2339 /* find function for current state and message */
2340 for (i = 0; i < L2DOWNSLLEN; i++) {
2341 if (msg_type == l2downstatelist[i].type)
2343 if ((msg_type == l2downstatelist[i].type)
2344 && ((1 << dl->state) & l2downstatelist[i].states))
2348 LOGP(DRSL, LOGL_NOTICE, "Message unsupported.\n");
2352 if (i == L2DOWNSLLEN) {
2353 LOGP(DRSL, LOGL_NOTICE, "Message unhandled at this state.\n");
2358 rc = l2downstatelist[i].rout(msg, dl);
2363 /* incoming RSLms COMMON CHANNEL message from L3 */
2364 static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
2366 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
2367 int msg_type = cch->c.msg_type;
2370 if (msgb_l2len(msg) < sizeof(*cch)) {
2371 LOGP(DRSL, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
2376 case RSL_MT_CHAN_RQD:
2377 /* create and send RACH request */
2378 rc = rslms_rx_chan_rqd(lc, msg);
2381 LOGP(DRSL, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
2390 /* input into layer2 (from layer 3) */
2391 int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
2393 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
2396 if (msgb_l2len(msg) < sizeof(*rslh)) {
2397 LOGP(DRSL, LOGL_ERROR, "Message too short RSL hdr!\n");
2401 switch (rslh->msg_discr & 0xfe) {
2402 case ABIS_RSL_MDISC_RLL:
2403 rc = rslms_rx_rll(msg, lc);
2405 case ABIS_RSL_MDISC_COM_CHAN:
2406 rc = rslms_rx_com_chan(msg, lc);
2409 LOGP(DRSL, LOGL_ERROR, "unknown RSLms message "
2410 "discriminator 0x%02x", rslh->msg_discr);
2418 int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
2422 le->cr.loc2rem.cmd = CR_MS2BS_CMD;
2423 le->cr.loc2rem.resp = CR_MS2BS_RESP;
2424 le->cr.rem2loc.cmd = CR_BS2MS_CMD;
2425 le->cr.rem2loc.resp = CR_BS2MS_RESP;
2427 case LAPDM_MODE_BTS:
2428 le->cr.loc2rem.cmd = CR_BS2MS_CMD;
2429 le->cr.loc2rem.resp = CR_BS2MS_RESP;
2430 le->cr.rem2loc.cmd = CR_MS2BS_CMD;
2431 le->cr.rem2loc.resp = CR_MS2BS_RESP;
2442 int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
2446 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
2450 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
2453 void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
2455 lc->lapdm_dcch.l1_prim_cb = cb;
2456 lc->lapdm_acch.l1_prim_cb = cb;
2457 lc->lapdm_dcch.l1_ctx = ctx;
2458 lc->lapdm_acch.l1_ctx = ctx;
2461 void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
2463 lc->lapdm_dcch.l3_cb = cb;
2464 lc->lapdm_acch.l3_cb = cb;
2465 lc->lapdm_dcch.l3_ctx = ctx;
2466 lc->lapdm_acch.l3_ctx = ctx;
2469 void lapdm_entity_reset(struct lapdm_entity *le)
2471 struct lapdm_datalink *dl;
2474 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
2475 dl = &le->datalink[i];
2476 if (dl->state == LAPDm_STATE_IDLE)
2478 LOGP(DLAPDM, LOGL_INFO, "Resetting LAPDm instance\n");
2479 /* reset Timer T200 */
2480 osmo_timer_del(&dl->t200);
2481 /* enter idle state */
2482 dl->state = LAPDm_STATE_IDLE;
2484 lapdm_dl_flush_tx(dl);
2485 lapdm_dl_flush_send(dl);
2489 void lapdm_channel_reset(struct lapdm_channel *lc)
2491 lapdm_entity_reset(&lc->lapdm_dcch);
2492 lapdm_entity_reset(&lc->lapdm_acch);
2495 void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
2500 void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
2502 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
2503 lapdm_entity_set_flags(&lc->lapdm_acch, flags);