1 /* GSM LAPDm (TS 04.06) implementation */
3 /* (C) 2010 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/protocol/gsm_04_08.h>
66 #include <osmocom/gsm/protocol/gsm_08_58.h>
68 #include <osmocom/bb/common/osmocom_data.h>
69 #include <osmocom/bb/common/l1ctl.h>
70 #include <osmocom/bb/common/lapdm.h>
71 #include <osmocom/bb/common/logging.h>
73 #include <l1ctl_proto.h>
75 /* TS 04.06 Figure 4 / Section 3.2 */
76 #define LAPDm_LPD_NORMAL 0
77 #define LAPDm_LPD_SMSCB 1
78 #define LAPDm_SAPI_NORMAL 0
79 #define LAPDm_SAPI_SMS 3
80 #define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
82 #define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
83 #define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
84 #define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
86 /* TS 04.06 Table 3 / Section 3.4.3 */
87 #define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
88 #define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
89 #define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
91 #define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
92 #define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
93 #define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
95 #define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
96 #define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
98 #define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
100 #define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
101 #define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
103 /* TS 04.06 Table 4 / Section 3.8.1 */
104 #define LAPDm_U_SABM 0x7
105 #define LAPDm_U_DM 0x3
106 #define LAPDm_U_UI 0x0
107 #define LAPDm_U_DISC 0x8
108 #define LAPDm_U_UA 0xC
110 #define LAPDm_S_RR 0x0
111 #define LAPDm_S_RNR 0x1
112 #define LAPDm_S_REJ 0x2
114 #define LAPDm_LEN(len) ((len << 2) | 0x1)
115 #define LAPDm_MORE 0x2
117 /* TS 04.06 Section 5.8.3 */
118 #define N201_AB_SACCH 18
119 #define N201_AB_SDCCH 20
120 #define N201_AB_FACCH 20
122 #define N201_Bter_SACCH 21
123 #define N201_Bter_SDCCH 23
124 #define N201_Bter_FACCH 23
127 /* 5.8.2.1 N200 during establish and release */
128 #define N200_EST_REL 5
129 /* 5.8.2.1 N200 during timer recovery state */
130 #define N200_TR_SACCH 5
131 #define N200_TR_SDCCH 23
132 #define N200_TR_FACCH_FR 34
133 #define N200_TR_EFACCH_FR 48
134 #define N200_TR_FACCH_HR 29
135 /* FIXME: this depends on chan type */
136 #define N200 N200_TR_SACCH
138 #define CR_MS2BS_CMD 0
139 #define CR_MS2BS_RESP 1
140 #define CR_BS2MS_CMD 1
141 #define CR_BS2MS_RESP 0
143 /* Set T200 to 1 Second (OpenBTS uses 900ms) */
146 /* k value for each SAPI */
147 static uint8_t k_sapi[] = {1, 1, 1, 1, 1, 1, 1, 1};
157 static void lapdm_t200_cb(void *data);
158 static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line);
160 /* UTILITY FUNCTIONS */
162 static inline uint8_t inc_mod8(uint8_t x)
167 static inline uint8_t add_mod8(uint8_t x, uint8_t y)
172 static inline uint8_t sub_mod8(uint8_t x, uint8_t y)
174 return (x - y) & 7; /* handle negative results correctly */
177 static void lapdm_dl_init(struct lapdm_datalink *dl,
178 struct lapdm_entity *entity)
180 memset(dl, 0, sizeof(*dl));
181 INIT_LLIST_HEAD(&dl->send_queue);
182 INIT_LLIST_HEAD(&dl->tx_queue);
183 dl->state = LAPDm_STATE_IDLE;
185 dl->t200.cb = &lapdm_t200_cb;
189 void lapdm_init(struct lapdm_entity *le, struct osmocom_ms *ms)
193 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
194 lapdm_dl_init(&le->datalink[i], le);
199 static void lapdm_dl_flush_send(struct lapdm_datalink *dl)
203 /* Flush send-queue */
204 while ((msg = msgb_dequeue(&dl->send_queue)))
207 /* Clear send-buffer */
208 if (dl->send_buffer) {
209 msgb_free(dl->send_buffer);
210 dl->send_buffer = NULL;
214 static void lapdm_dl_flush_tx(struct lapdm_datalink *dl)
219 while ((msg = msgb_dequeue(&dl->tx_queue)))
221 for (i = 0; i < 8; i++)
222 dl->tx_length[i] = 0;
225 void lapdm_exit(struct lapdm_entity *le)
228 struct lapdm_datalink *dl;
230 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
231 dl = &le->datalink[i];
232 lapdm_dl_flush_tx(dl);
233 lapdm_dl_flush_send(dl);
235 msgb_free(dl->rcv_buffer);
239 static void lapdm_dl_newstate(struct lapdm_datalink *dl, uint32_t state)
241 LOGP(DLAPDM, LOGL_INFO, "new state %s -> %s\n",
242 lapdm_state_names[dl->state], lapdm_state_names[state]);
247 static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
250 case LAPDm_SAPI_NORMAL:
251 return &le->datalink[0];
253 return &le->datalink[1];
259 /* remove the L2 header from a MSGB */
260 static inline unsigned char *msgb_pull_l2h(struct msgb *msg)
262 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h);
267 /* Append padding (if required) */
268 static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
270 int pad_len = n201 - msgb_l2len(msg);
274 LOGP(DLAPDM, LOGL_ERROR,
275 "cannot pad message that is already too big!\n");
279 data = msgb_put(msg, pad_len);
280 memset(data, 0x2B, pad_len);
283 /* write a frame into the tx queue */
284 static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
285 uint8_t chan_nr, uint8_t link_id, uint8_t n201)
287 struct lapdm_entity *le = dl->entity;
288 struct osmocom_ms *ms = le->ms;
290 /* if there is a pending message, queue it */
291 if (le->tx_pending) {
292 *msgb_push(msg, 1) = n201;
293 *msgb_push(msg, 1) = link_id;
294 *msgb_push(msg, 1) = chan_nr;
295 msgb_enqueue(&dl->tx_queue, msg);
299 /* send the frame now */
300 le->tx_pending = 0; /* disabled flow control */
301 lapdm_pad_msgb(msg, n201);
302 return l1ctl_tx_data_req(ms, msg, chan_nr, link_id);
305 /* get next frame from the tx queue. because the ms has multiple datalinks,
306 * each datalink's queue is read round-robin.
308 int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
310 struct osmocom_ms *ms = le->ms;
311 struct lapdm_datalink *dl;
312 int last = le->last_tx_dequeue;
313 int i = last, n = ARRAY_SIZE(le->datalink);
314 uint8_t chan_nr, link_id, n201;
316 /* we may send again */
319 /* free confirm message */
322 /* round-robin dequeue */
326 dl = &le->datalink[i];
327 if ((msg = msgb_dequeue(&dl->tx_queue)))
331 /* no message in all queues */
335 /* Pull chan_nr and link_id */
336 chan_nr = *msg->data;
338 link_id = *msg->data;
343 /* Set last dequeue position */
344 le->last_tx_dequeue = i;
346 /* Pad the frame, we can transmit now */
348 lapdm_pad_msgb(msg, n201);
349 return l1ctl_tx_data_req(ms, msg, chan_nr, link_id);
352 /* Create RSLms various RSLms messages */
353 static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
356 /* Add the RSL + RLL header */
357 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
359 /* send off the RSLms message to L3 */
360 return rslms_sendmsg(msg, mctx->dl->entity->ms);
363 /* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
364 static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
366 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
367 struct abis_rsl_rll_hdr *rllh;
369 /* Add the RSL + RLL header */
370 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
371 msgb_push(msg, 2 + 2);
372 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
374 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
376 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
377 rllh->data[1] = mctx->ta_ind;
379 rllh->data[2] = RSL_IE_MS_POWER;
380 rllh->data[3] = mctx->tx_power_ind;
382 return rslms_sendmsg(msg, mctx->dl->entity->ms);
385 static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
389 msg = rsl_rll_simple(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->ms);
395 static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
400 LOGP(DLAPDM, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
401 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
402 msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr) + 3);
403 tlv = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
404 tlv[0] = RSL_IE_RLM_CAUSE;
407 return rslms_sendmsg(msg, mctx->dl->entity->ms);
410 static int check_length_ind(struct lapdm_msg_ctx *mctx, uint8_t length_ind)
412 if (!(length_ind & 0x01)) {
413 /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION
414 * primitive with cause "frame not implemented" is sent to the
415 * mobile management entity. */
416 LOGP(DLAPDM, LOGL_NOTICE,
417 "we don't support multi-octet length\n");
418 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
424 static int lapdm_send_resend(struct lapdm_datalink *dl)
426 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm resend");
429 /* Resend SABM/DISC from tx_hist */
430 length = dl->tx_length[0];
431 msg->l2h = msgb_put(msg, length);
432 memcpy(msg->l2h, dl->tx_hist[dl->V_send], length);
434 return tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, dl->mctx.link_id,
438 static int lapdm_send_ua(struct lapdm_msg_ctx *mctx, uint8_t len, uint8_t *data)
440 uint8_t sapi = mctx->link_id & 7;
441 uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl);
442 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm UA");
443 msg->l2h = msgb_put(msg, 3 + len);
445 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
446 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UA, f_bit);
447 msg->l2h[2] = LAPDm_LEN(len);
449 memcpy(msg->l2h + 3, data, len);
451 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
455 static int lapdm_send_dm(struct lapdm_msg_ctx *mctx)
457 uint8_t sapi = mctx->link_id & 7;
458 uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl);
459 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm DM");
460 msg->l2h = msgb_put(msg, 3);
462 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
463 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DM, f_bit);
466 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
470 static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
472 uint8_t sapi = mctx->link_id & 7;
473 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RR");
474 msg->l2h = msgb_put(msg, 3);
476 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
477 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit);
478 msg->l2h[2] = LAPDm_LEN(0);
480 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
484 static int lapdm_send_rnr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
486 uint8_t sapi = mctx->link_id & 7;
487 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RNR");
488 msg->l2h = msgb_put(msg, 3);
490 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
491 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RNR, f_bit);
492 msg->l2h[2] = LAPDm_LEN(0);
494 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
498 static int lapdm_send_rej(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 REJ");
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_REJ, 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 /* Timer callback on T200 expiry */
513 static void lapdm_t200_cb(void *data)
515 struct lapdm_datalink *dl = data;
517 LOGP(DLAPDM, LOGL_INFO, "lapdm_t200_cb(%p) state=%u\n", dl, dl->state);
520 case LAPDm_STATE_SABM_SENT:
522 if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) {
523 /* send RELEASE INDICATION to L3 */
524 send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
525 /* send MDL ERROR INIDCATION to L3 */
526 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
527 /* flush tx buffers */
528 lapdm_dl_flush_tx(dl);
529 /* go back to idle state */
530 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
531 /* NOTE: we must not change any other states or buffers
532 * and queues, since we may reconnect after handover
533 * failure. the buffered messages is replaced there */
536 /* retransmit SABM command */
537 lapdm_send_resend(dl);
538 /* increment re-transmission counter */
540 /* restart T200 (PH-READY-TO-SEND) */
541 osmo_timer_schedule(&dl->t200, T200);
543 case LAPDm_STATE_DISC_SENT:
545 if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) {
546 /* send RELEASE INDICATION to L3 */
547 send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
548 /* send MDL ERROR INIDCATION to L3 */
549 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
551 lapdm_dl_flush_tx(dl);
552 lapdm_dl_flush_send(dl);
553 /* go back to idle state */
554 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
555 /* NOTE: we must not change any other states or buffers
556 * and queues, since we may reconnect after handover
557 * failure. the buffered messages is replaced there */
560 /* retransmit DISC command */
561 lapdm_send_resend(dl);
562 /* increment re-transmission counter */
564 /* restart T200 (PH-READY-TO-SEND) */
565 osmo_timer_schedule(&dl->t200, T200);
567 case LAPDm_STATE_MF_EST:
570 lapdm_dl_newstate(dl, LAPDm_STATE_TIMER_RECOV);
572 case LAPDm_STATE_TIMER_RECOV:
574 if (dl->retrans_ctr < N200) {
575 /* retransmit I frame (V_s-1) with P=1, if any */
576 if (dl->tx_length[sub_mod8(dl->V_send, 1)]) {
580 LOGP(DLAPDM, LOGL_INFO, "retransmit last frame "
581 "V(S)=%d\n", sub_mod8(dl->V_send, 1));
582 /* Create I frame (segment) from tx_hist */
583 length = dl->tx_length[sub_mod8(dl->V_send, 1)];
584 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
585 msg->l2h = msgb_put(msg, length);
587 dl->tx_hist[sub_mod8(dl->V_send, 1)],
589 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv,
590 sub_mod8(dl->V_send, 1), 1); /* P=1 */
591 tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr,
592 dl->mctx.link_id, dl->mctx.n201);
594 /* OR send appropriate supervision frame with P=1 */
595 if (!dl->own_busy && !dl->seq_err_cond) {
596 lapdm_send_rr(&dl->mctx, 1);
597 /* NOTE: In case of sequence error
598 * condition, the REJ frame has been
599 * transmitted when entering the
600 * condition, so it has not be done
603 } else if (dl->own_busy) {
604 lapdm_send_rnr(&dl->mctx, 1);
606 LOGP(DLAPDM, LOGL_INFO, "unhandled, "
610 /* restart T200 (PH-READY-TO-SEND) */
611 osmo_timer_schedule(&dl->t200, T200);
613 /* send MDL ERROR INIDCATION to L3 */
614 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
618 LOGP(DLAPDM, LOGL_INFO, "T200 expired in unexpected "
619 "dl->state %u\n", dl->state);
623 /* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
624 static void lapdm_acknowledge(struct lapdm_msg_ctx *mctx)
626 struct lapdm_datalink *dl = mctx->dl;
627 uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl);
628 int s = 0, rej = 0, t200_reset = 0;
631 /* supervisory frame ? */
632 if (LAPDm_CTRL_is_S(mctx->ctrl))
635 if (s && LAPDm_CTRL_S_BITS(mctx->ctrl) == LAPDm_S_REJ)
638 /* Flush all transmit buffers of acknowledged frames */
639 for (i = dl->V_ack; i != nr; i = inc_mod8(i)) {
640 if (dl->tx_length[i]) {
641 dl->tx_length[i] = 0;
642 LOGP(DLAPDM, LOGL_INFO, "ack frame %d\n", i);
646 if (dl->state != LAPDm_STATE_TIMER_RECOV) {
647 /* When not in the timer recovery condition, the data
648 * link layer entity shall reset the timer T200 on
649 * receipt of a valid I frame with N(R) higher than V(A),
650 * or an REJ with an N(R) equal to V(A). */
651 if ((!rej && nr != dl->V_ack)
652 || (rej && nr == dl->V_ack)) {
653 LOGP(DLAPDM, LOGL_INFO, "reset t200\n");
655 osmo_timer_del(&dl->t200);
656 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
658 /* 5.7.4: N(R) sequence error
659 * N(R) is called valid, if and only if
660 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
662 if (sub_mod8(nr, dl->V_ack) > sub_mod8(dl->V_send, dl->V_ack)) {
663 LOGP(DLAPDM, LOGL_NOTICE, "N(R) sequence error\n");
664 rsl_rll_error(RLL_CAUSE_SEQ_ERR, mctx);
668 /* V(A) shall be set to the value of N(R) */
671 /* If T200 has been reset by the receipt of an I, RR or RNR frame,
672 * and if there are outstanding I frames, restart T200 */
673 if (t200_reset && !rej) {
674 if (dl->tx_length[dl->V_send - 1]) {
675 LOGP(DLAPDM, LOGL_INFO, "start T200, due to unacked I "
677 osmo_timer_schedule(&dl->t200, T200);
684 /* Receive a LAPDm U (Unnumbered) message from L1 */
685 static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx)
687 struct lapdm_datalink *dl = mctx->dl;
692 switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) {
694 rsl_msg = RSL_MT_EST_IND;
696 LOGP(DLAPDM, LOGL_INFO, "SABM received\n");
698 dl->seq_err_cond = 0;
699 /* G.2.2 Wrong value of the C/R bit */
700 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
701 LOGP(DLAPDM, LOGL_NOTICE, "SABM response error\n");
703 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
707 length = msg->l2h[2] >> 2;
708 /* G.4.5 If SABM is received with L>N201 or with M bit
709 * set, AN MDL-ERROR-INDICATION is sent to MM.
711 if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) {
712 LOGP(DLAPDM, LOGL_NOTICE, "SABM too large error\n");
714 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
718 /* Must be Format B */
719 rc = check_length_ind(mctx, msg->l2h[2]);
725 case LAPDm_STATE_IDLE:
726 /* Set chan_nr and link_id for established connection */
727 memset(&dl->mctx, 0, sizeof(dl->mctx));
729 dl->mctx.chan_nr = mctx->chan_nr;
730 dl->mctx.link_id = mctx->link_id;
732 case LAPDm_STATE_MF_EST:
734 rsl_msg = RSL_MT_EST_CONF;
737 LOGP(DLAPDM, LOGL_INFO, "SABM command, multiple "
738 "frame established state\n");
739 /* check for contention resoultion */
740 if (dl->tx_hist[0][2] >> 2) {
741 LOGP(DLAPDM, LOGL_NOTICE, "SABM not allowed "
742 "during contention resolution\n");
743 rsl_rll_error(RLL_CAUSE_SABM_INFO_NOTALL, mctx);
747 case LAPDm_STATE_DISC_SENT:
748 /* 5.4.6.2 send DM with F=P */
750 /* reset Timer T200 */
751 osmo_timer_del(&dl->t200);
753 return send_rll_simple(RSL_MT_REL_CONF, mctx);
755 lapdm_send_ua(mctx, length, msg->l2h + 3);
759 /* send UA response */
760 lapdm_send_ua(mctx, length, msg->l2h + 3);
761 /* set Vs, Vr and Va to 0 */
762 dl->V_send = dl->V_recv = dl->V_ack = 0;
764 dl->tx_length[0] = 0;
765 /* enter multiple-frame-established state */
766 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
767 /* send notification to L3 */
769 /* 5.4.1.2 Normal establishment procedures */
770 rc = send_rll_simple(rsl_msg, mctx);
773 /* 5.4.1.4 Contention resolution establishment */
774 msg->l3h = msg->l2h + 3;
776 rc = send_rslms_rll_l3(rsl_msg, mctx, msg);
780 LOGP(DLAPDM, LOGL_INFO, "DM received\n");
781 /* G.2.2 Wrong value of the C/R bit */
782 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
783 LOGP(DLAPDM, LOGL_NOTICE, "DM command error\n");
785 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
788 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
789 /* 5.4.1.2 DM responses with the F bit set to "0"
796 case LAPDm_STATE_SABM_SENT:
798 case LAPDm_STATE_MF_EST:
799 if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1) {
800 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
802 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP, mctx);
804 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
805 "response, multiple frame established "
807 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx);
811 case LAPDm_STATE_TIMER_RECOV:
812 /* DM is normal in case PF = 1 */
813 if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) {
814 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
815 "response, multiple frame established "
817 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx);
822 case LAPDm_STATE_DISC_SENT:
823 /* reset Timer T200 */
824 osmo_timer_del(&dl->t200);
825 /* go to idle state */
826 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
827 rc = send_rll_simple(RSL_MT_REL_CONF, mctx);
830 case LAPDm_STATE_IDLE:
831 /* 5.4.5 all other frame types shall be discarded */
833 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM response! "
839 osmo_timer_del(&dl->t200);
840 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
844 LOGP(DLAPDM, LOGL_INFO, "UI received\n");
845 /* G.2.2 Wrong value of the C/R bit */
846 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
847 LOGP(DLAPDM, LOGL_NOTICE, "UI indicates response "
850 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
854 length = msg->l2h[2] >> 2;
855 /* FIXME: G.4.5 If UI is received with L>N201 or with M bit
856 * set, AN MDL-ERROR-INDICATION is sent to MM.
859 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
861 msg->l3h = msg->l2h + 2;
863 rc = check_length_ind(mctx, msg->l2h[2]);
868 length = msg->l2h[2] >> 2;
869 msg->l3h = msg->l2h + 3;
871 /* do some length checks */
873 /* 5.3.3 UI frames received with the length indicator
874 * set to "0" shall be ignored
876 LOGP(DLAPDM, LOGL_INFO, "length=0 (discarding)\n");
880 switch (LAPDm_ADDR_SAPI(mctx->addr)) {
881 case LAPDm_SAPI_NORMAL:
885 /* 5.3.3 UI frames with invalid SAPI values shall be
888 LOGP(DLAPDM, LOGL_INFO, "sapi=%u (discarding)\n",
889 LAPDm_ADDR_SAPI(mctx->addr));
894 rc = send_rslms_rll_l3_ui(mctx, msg);
897 rsl_msg = RSL_MT_REL_IND;
899 LOGP(DLAPDM, LOGL_INFO, "DISC received\n");
901 lapdm_dl_flush_tx(dl);
902 lapdm_dl_flush_send(dl);
904 dl->seq_err_cond = 0;
905 /* G.2.2 Wrong value of the C/R bit */
906 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
907 LOGP(DLAPDM, LOGL_NOTICE, "DISC response error\n");
909 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
912 length = msg->l2h[2] >> 2;
913 if (length > 0 || msg->l2h[2] & 0x02) {
914 /* G.4.4 If a DISC or DM frame is received with L>0 or
915 * with the M bit set to "1", an MDL-ERROR-INDICATION
916 * primitive with cause "U frame with incorrect
917 * parameters" is sent to the mobile management entity.
919 LOGP(DLAPDM, LOGL_NOTICE,
920 "U frame iwth incorrect parameters ");
922 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
927 case LAPDm_STATE_IDLE:
928 LOGP(DLAPDM, LOGL_INFO, "DISC in idle state\n");
929 /* send DM with F=P */
930 return lapdm_send_dm(mctx);
931 case LAPDm_STATE_SABM_SENT:
932 LOGP(DLAPDM, LOGL_INFO, "DISC in SABM state\n");
933 /* 5.4.6.2 send DM with F=P */
935 /* reset Timer T200 */
936 osmo_timer_del(&dl->t200);
937 return send_rll_simple(RSL_MT_REL_IND, mctx);
938 case LAPDm_STATE_MF_EST:
939 case LAPDm_STATE_TIMER_RECOV:
940 LOGP(DLAPDM, LOGL_INFO, "DISC in est state\n");
942 case LAPDm_STATE_DISC_SENT:
943 LOGP(DLAPDM, LOGL_INFO, "DISC in disc state\n");
944 rsl_msg = RSL_MT_REL_CONF;
947 lapdm_send_ua(mctx, length, msg->l2h + 3);
950 /* send UA response */
951 lapdm_send_ua(mctx, length, msg->l2h + 3);
952 /* reset Timer T200 */
953 osmo_timer_del(&dl->t200);
954 /* enter idle state */
955 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
956 /* send notification to L3 */
957 rc = send_rll_simple(rsl_msg, mctx);
960 LOGP(DLAPDM, LOGL_INFO, "UA received\n");
961 /* G.2.2 Wrong value of the C/R bit */
962 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
963 LOGP(DLAPDM, LOGL_NOTICE, "UA indicates command "
966 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
970 length = msg->l2h[2] >> 2;
971 /* G.4.5 If UA is received with L>N201 or with M bit
972 * set, AN MDL-ERROR-INDICATION is sent to MM.
974 if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) {
975 LOGP(DLAPDM, LOGL_NOTICE, "UA too large error\n");
977 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
981 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
982 /* 5.4.1.2 A UA response with the F bit set to "0"
985 LOGP(DLAPDM, LOGL_INFO, "F=0 (discarding)\n");
990 case LAPDm_STATE_SABM_SENT:
992 case LAPDm_STATE_MF_EST:
993 case LAPDm_STATE_TIMER_RECOV:
994 LOGP(DLAPDM, LOGL_INFO, "unsolicited UA response! "
996 rsl_rll_error(RLL_CAUSE_UNSOL_UA_RESP, mctx);
999 case LAPDm_STATE_DISC_SENT:
1000 LOGP(DLAPDM, LOGL_INFO, "UA in disconnect state\n");
1001 /* reset Timer T200 */
1002 osmo_timer_del(&dl->t200);
1003 /* go to idle state */
1004 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1005 rc = send_rll_simple(RSL_MT_REL_CONF, mctx);
1008 case LAPDm_STATE_IDLE:
1009 /* 5.4.5 all other frame types shall be discarded */
1011 LOGP(DLAPDM, LOGL_INFO, "unsolicited UA response! "
1016 LOGP(DLAPDM, LOGL_INFO, "UA in SABM state\n");
1017 /* reset Timer T200 */
1018 osmo_timer_del(&dl->t200);
1019 /* compare UA with SABME if contention resolution is applied */
1020 if (dl->tx_hist[0][2] >> 2) {
1021 rc = check_length_ind(mctx, msg->l2h[2]);
1023 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
1025 /* go to idle state */
1026 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1029 length = msg->l2h[2] >> 2;
1030 if (length != (dl->tx_hist[0][2] >> 2)
1031 || !!memcmp(dl->tx_hist[0] + 3, msg->l2h + 3,
1033 LOGP(DLAPDM, LOGL_INFO, "**** UA response "
1034 "mismatches ****\n");
1035 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
1037 /* go to idle state */
1038 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1042 /* set Vs, Vr and Va to 0 */
1043 dl->V_send = dl->V_recv = dl->V_ack = 0;
1045 dl->tx_length[0] = 0;
1046 /* enter multiple-frame-established state */
1047 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1048 /* send outstanding frames, if any (resume / reconnect) */
1049 rslms_send_i(mctx, __LINE__);
1050 /* send notification to L3 */
1051 rc = send_rll_simple(RSL_MT_EST_CONF, mctx);
1056 LOGP(DLAPDM, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1058 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1064 /* Receive a LAPDm S (Supervisory) message from L1 */
1065 static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1067 struct lapdm_datalink *dl = mctx->dl;
1070 length = msg->l2h[2] >> 2;
1071 if (length > 0 || msg->l2h[2] & 0x02) {
1072 /* G.4.3 If a supervisory frame is received with L>0 or
1073 * with the M bit set to "1", an MDL-ERROR-INDICATION
1074 * primitive with cause "S frame with incorrect
1075 * parameters" is sent to the mobile management entity. */
1076 LOGP(DLAPDM, LOGL_NOTICE,
1077 "S frame with incorrect parameters\n");
1079 rsl_rll_error(RLL_CAUSE_SFRM_INC_PARAM, mctx);
1083 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1084 && LAPDm_CTRL_PF_BIT(mctx->ctrl)
1085 && dl->state != LAPDm_STATE_TIMER_RECOV) {
1086 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1087 LOGP(DLAPDM, LOGL_NOTICE, "S frame response with F=1 error\n");
1088 rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
1091 switch (dl->state) {
1092 case LAPDm_STATE_IDLE:
1093 /* if P=1, respond DM with F=1 (5.2.2) */
1094 /* 5.4.5 all other frame types shall be discarded */
1095 if (LAPDm_CTRL_PF_BIT(mctx->ctrl))
1096 lapdm_send_dm(mctx); /* F=P */
1098 case LAPDm_STATE_SABM_SENT:
1099 case LAPDm_STATE_DISC_SENT:
1100 LOGP(DLAPDM, LOGL_NOTICE, "S frame ignored in this state\n");
1104 switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) {
1106 LOGP(DLAPDM, LOGL_INFO, "RR received\n");
1107 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1108 lapdm_acknowledge(mctx);
1111 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1112 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1113 if (!dl->own_busy && !dl->seq_err_cond) {
1114 LOGP(DLAPDM, LOGL_NOTICE, "RR frame command "
1115 "with polling bit set and we are not "
1116 "busy, so we reply with RR frame\n");
1117 lapdm_send_rr(mctx, 1);
1118 /* NOTE: In case of sequence error condition,
1119 * the REJ frame has been transmitted when
1120 * entering the condition, so it has not be
1123 } else if (dl->own_busy) {
1124 LOGP(DLAPDM, LOGL_NOTICE, "RR frame command "
1125 "with polling bit set and we are busy, "
1126 "so we reply with RR frame\n");
1127 lapdm_send_rnr(mctx, 1);
1129 } else if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1130 && LAPDm_CTRL_PF_BIT(mctx->ctrl)
1131 && dl->state == LAPDm_STATE_TIMER_RECOV) {
1132 LOGP(DLAPDM, LOGL_INFO, "RR response with F==1, "
1133 "and we are in timer recovery state, so "
1134 "we leave that state\n");
1135 /* V(S) to the N(R) in the RR frame */
1136 dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl);
1137 /* reset Timer T200 */
1138 osmo_timer_del(&dl->t200);
1139 /* 5.5.7 Clear timer recovery condition */
1140 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1142 /* Send message, if possible due to acknowledged data */
1143 rslms_send_i(mctx, __LINE__);
1147 LOGP(DLAPDM, LOGL_INFO, "RNR received\n");
1148 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1149 lapdm_acknowledge(mctx);
1152 /* Set peer receiver busy condition */
1155 if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1156 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
1157 if (!dl->own_busy) {
1158 LOGP(DLAPDM, LOGL_INFO, "RNR poll "
1159 "command and we are not busy, "
1160 "so we reply with RR final "
1162 /* Send RR with F=1 */
1163 lapdm_send_rr(mctx, 1);
1165 LOGP(DLAPDM, LOGL_INFO, "RNR poll "
1166 "command and we are busy, so "
1167 "we reply with RNR final "
1169 /* Send RNR with F=1 */
1170 lapdm_send_rnr(mctx, 1);
1172 } else if (dl->state == LAPDm_STATE_TIMER_RECOV) {
1173 LOGP(DLAPDM, LOGL_INFO, "RNR poll response "
1174 "and we in timer recovery state, so "
1175 "we leave that state\n");
1176 /* 5.5.7 Clear timer recovery condition */
1177 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1178 /* V(S) to the N(R) in the RNR frame */
1179 dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl);
1182 LOGP(DLAPDM, LOGL_INFO, "RNR not polling/final state "
1185 /* Send message, if possible due to acknowledged data */
1186 rslms_send_i(mctx, __LINE__);
1190 LOGP(DLAPDM, LOGL_INFO, "REJ received\n");
1191 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1192 lapdm_acknowledge(mctx);
1195 if (dl->state != LAPDm_STATE_TIMER_RECOV) {
1196 /* Clear an existing peer receiver busy condition */
1198 /* V(S) and V(A) to the N(R) in the REJ frame */
1199 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1200 /* reset Timer T200 */
1201 osmo_timer_del(&dl->t200);
1203 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1204 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1205 if (!dl->own_busy && !dl->seq_err_cond) {
1206 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1207 "command not in timer recovery "
1208 "state and not in own busy "
1209 "condition received, so we "
1210 "respond with RR final "
1212 lapdm_send_rr(mctx, 1);
1213 /* NOTE: In case of sequence error
1214 * condition, the REJ frame has been
1215 * transmitted when entering the
1216 * condition, so it has not be done
1219 } else if (dl->own_busy) {
1220 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1221 "command not in timer recovery "
1222 "state and in own busy "
1223 "condition received, so we "
1224 "respond with RNR final "
1226 lapdm_send_rnr(mctx, 1);
1229 LOGP(DLAPDM, LOGL_INFO, "REJ response or not "
1230 "polling command not in timer recovery "
1231 "state received\n");
1232 /* send MDL ERROR INIDCATION to L3 */
1233 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1234 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1235 rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
1238 } else if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1239 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1240 LOGP(DLAPDM, LOGL_INFO, "REJ poll response in timer "
1241 "recovery state received\n");
1242 /* Clear an existing peer receiver busy condition */
1244 /* 5.5.7 Clear timer recovery condition */
1245 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1246 /* V(S) and V(A) to the N(R) in the REJ frame */
1247 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1248 /* reset Timer T200 */
1249 osmo_timer_del(&dl->t200);
1251 /* Clear an existing peer receiver busy condition */
1253 /* V(S) and V(A) to the N(R) in the REJ frame */
1254 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1256 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1257 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1258 if (!dl->own_busy && !dl->seq_err_cond) {
1259 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1260 "command in timer recovery "
1261 "state and not in own busy "
1262 "condition received, so we "
1263 "respond with RR final "
1265 lapdm_send_rr(mctx, 1);
1266 /* NOTE: In case of sequence error
1267 * condition, the REJ frame has been
1268 * transmitted when entering the
1269 * condition, so it has not be done
1272 } else if (dl->own_busy) {
1273 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1274 "command in timer recovery "
1275 "state and in own busy "
1276 "condition received, so we "
1277 "respond with RNR final "
1279 lapdm_send_rnr(mctx, 1);
1282 LOGP(DLAPDM, LOGL_INFO, "REJ response or not "
1283 "polling command in timer recovery "
1284 "state received\n");
1287 /* FIXME: 5.5.4.2 2) */
1289 /* Send message, if possible due to acknowledged data */
1290 rslms_send_i(mctx, __LINE__);
1295 LOGP(DLAPDM, LOGL_NOTICE, "Supervisory frame not allowed.\n");
1297 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1304 /* Receive a LAPDm I (Information) message from L1 */
1305 static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1307 struct lapdm_datalink *dl = mctx->dl;
1308 //uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl);
1309 uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl);
1313 LOGP(DLAPDM, LOGL_NOTICE, "I received\n");
1315 /* G.2.2 Wrong value of the C/R bit */
1316 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
1317 LOGP(DLAPDM, LOGL_NOTICE, "I frame response not allowed\n");
1319 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1323 length = msg->l2h[2] >> 2;
1324 if (length == 0 || length + 3 > mctx->n201) {
1325 /* G.4.2 If the length indicator of an I frame is set
1326 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1327 * primitive with cause "I frame with incorrect length"
1328 * is sent to the mobile management entity. */
1329 LOGP(DLAPDM, LOGL_NOTICE, "I frame length not allowed\n");
1331 rsl_rll_error(RLL_CAUSE_IFRM_INC_LEN, mctx);
1335 /* G.4.2 If the numerical value of L is L<N201 and the M
1336 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1337 * cause "I frame with incorrect use of M bit" is sent to the
1338 * mobile management entity. */
1339 if ((msg->l2h[2] & LAPDm_MORE) && length + 3 < mctx->n201) {
1340 LOGP(DLAPDM, LOGL_NOTICE, "I frame with M bit too short\n");
1342 rsl_rll_error(RLL_CAUSE_IFRM_INC_MBITS, mctx);
1346 switch (dl->state) {
1347 case LAPDm_STATE_IDLE:
1348 /* if P=1, respond DM with F=1 (5.2.2) */
1349 /* 5.4.5 all other frame types shall be discarded */
1350 if (LAPDm_CTRL_PF_BIT(mctx->ctrl))
1351 lapdm_send_dm(mctx); /* F=P */
1353 case LAPDm_STATE_SABM_SENT:
1354 case LAPDm_STATE_DISC_SENT:
1355 LOGP(DLAPDM, LOGL_NOTICE, "I frame ignored in this state\n");
1360 /* 5.7.1: N(s) sequence error */
1361 if (ns != dl->V_recv) {
1362 LOGP(DLAPDM, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1363 "V(R)=%u\n", ns, dl->V_recv);
1366 if (!dl->seq_err_cond) {
1367 /* FIXME: help me understand what exactly todo here
1368 dl->seq_err_cond = 1;
1370 lapdm_send_rej(mctx, LAPDm_CTRL_PF_BIT(mctx->ctrl));
1375 dl->seq_err_cond = 0;
1377 /* Increment receiver state */
1378 dl->V_recv = inc_mod8(dl->V_recv);
1379 LOGP(DLAPDM, LOGL_NOTICE, "incrementing V(R) to %u\n", dl->V_recv);
1381 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1382 lapdm_acknowledge(mctx); /* V(A) is also set here */
1384 /* Only if we are not in own receiver busy condition */
1385 if (!dl->own_busy) {
1386 /* if the frame carries a complete segment */
1387 if (!(msg->l2h[2] & LAPDm_MORE)
1388 && !dl->rcv_buffer) {
1389 LOGP(DLAPDM, LOGL_INFO, "message in single I frame\n");
1390 /* send a DATA INDICATION to L3 */
1391 msg->l3h = msg->l2h + 3;
1394 msg->tail = msg->data + length;
1395 rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg);
1397 /* create rcv_buffer */
1398 if (!dl->rcv_buffer) {
1399 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1400 "frames (first message)\n");
1401 dl->rcv_buffer = msgb_alloc_headroom(200+56, 56,
1403 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1405 /* concat. rcv_buffer */
1406 if (msgb_l3len(dl->rcv_buffer) + length > 200) {
1407 LOGP(DLAPDM, LOGL_NOTICE, "Received frame "
1410 memcpy(msgb_put(dl->rcv_buffer, length),
1411 msg->l2h + 3, length);
1413 /* if the last segment was received */
1414 if (!(msg->l2h[2] & LAPDm_MORE)) {
1415 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1416 "frames (last message)\n");
1417 rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx,
1419 dl->rcv_buffer = NULL;
1421 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1422 "frames (next message)\n");
1427 LOGP(DLAPDM, LOGL_INFO, "I frame ignored during own receiver "
1428 "busy condition\n");
1430 /* Check for P bit */
1431 if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1433 /* check if we are not in own receiver busy */
1434 if (!dl->own_busy) {
1435 LOGP(DLAPDM, LOGL_INFO, "we are not busy, send RR\n");
1436 /* Send RR with F=1 */
1437 rc = lapdm_send_rr(mctx, 1);
1439 LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
1440 /* Send RNR with F=1 */
1441 rc = lapdm_send_rnr(mctx, 1);
1445 /* check if we are not in own receiver busy */
1446 if (!dl->own_busy) {
1447 /* NOTE: V(R) is already set above */
1448 rc = rslms_send_i(mctx, __LINE__);
1450 LOGP(DLAPDM, LOGL_INFO, "we are not busy and "
1451 "have no pending data, send RR\n");
1452 /* Send RR with F=0 */
1453 return lapdm_send_rr(mctx, 0);
1455 /* all I or one RR is sent, we are done */
1458 LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
1459 /* Send RNR with F=0 */
1460 rc = lapdm_send_rnr(mctx, 0);
1464 /* Send message, if possible due to acknowledged data */
1465 rslms_send_i(mctx, __LINE__);
1470 /* Receive a LAPDm message from L1 */
1471 static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1475 /* G.2.3 EA bit set to "0" is not allowed in GSM */
1476 if (!LAPDm_ADDR_EA(mctx->addr)) {
1477 LOGP(DLAPDM, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
1479 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1483 if (LAPDm_CTRL_is_U(mctx->ctrl))
1484 rc = lapdm_rx_u(msg, mctx);
1485 else if (LAPDm_CTRL_is_S(mctx->ctrl))
1486 rc = lapdm_rx_s(msg, mctx);
1487 else if (LAPDm_CTRL_is_I(mctx->ctrl))
1488 rc = lapdm_rx_i(msg, mctx);
1490 LOGP(DLAPDM, LOGL_NOTICE, "unknown LAPDm format\n");
1497 /* input into layer2 (from layer 1) */
1498 int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_dl *l1i)
1500 uint8_t cbits = l1i->chan_nr >> 3;
1501 uint8_t sapi = l1i->link_id & 7;
1502 struct lapdm_msg_ctx mctx;
1505 /* when we reach here, we have a msgb with l2h pointing to the raw
1506 * 23byte mac block. The l1h has already been purged. */
1508 mctx.dl = datalink_for_sapi(le, sapi);
1509 mctx.chan_nr = l1i->chan_nr;
1510 mctx.link_id = l1i->link_id;
1511 mctx.addr = mctx.ctrl = 0;
1513 /* G.2.1 No action schall be taken on frames containing an unallocated
1517 LOGP(DLAPDM, LOGL_NOTICE, "Received frame for unsupported "
1518 "SAPI %d!\n", sapi);
1524 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
1525 if (cbits == 0x10 || cbits == 0x12) {
1526 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
1527 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
1528 mctx.n201 = N201_Bbis;
1530 if (mctx.link_id & 0x40) {
1531 /* It was received from network on SACCH, thus
1532 * lapdm_fmt must be B4 */
1533 mctx.lapdm_fmt = LAPDm_FMT_B4;
1534 mctx.n201 = N201_B4;
1535 LOGP(DLAPDM, LOGL_INFO, "fmt=B4\n");
1536 /* SACCH frames have a two-byte L1 header that
1537 * OsmocomBB L1 doesn't strip */
1538 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
1539 mctx.ta_ind = msg->l2h[1];
1543 mctx.lapdm_fmt = LAPDm_FMT_B;
1544 LOGP(DLAPDM, LOGL_INFO, "fmt=B\n");
1545 mctx.n201 = 23; // FIXME: select correct size by chan.
1549 switch (mctx.lapdm_fmt) {
1553 mctx.addr = msg->l2h[0];
1554 if (!(mctx.addr & 0x01)) {
1555 LOGP(DLAPDM, LOGL_ERROR, "we don't support "
1556 "multibyte addresses (discarding)\n");
1560 mctx.ctrl = msg->l2h[1];
1561 /* obtain SAPI from address field */
1562 mctx.link_id |= LAPDm_ADDR_SAPI(mctx.addr);
1563 rc = lapdm_ph_data_ind(msg, &mctx);
1565 case LAPDm_FMT_Bter:
1569 case LAPDm_FMT_Bbis:
1570 /* directly pass up to layer3 */
1571 LOGP(DLAPDM, LOGL_INFO, "fmt=Bbis UI\n");
1572 msg->l3h = msg->l2h;
1574 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
1583 /* L3 -> L2 / RSLMS -> LAPDm */
1585 /* L3 requests establishment of data link */
1586 static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
1588 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1589 uint8_t chan_nr = rllh->chan_nr;
1590 uint8_t link_id = rllh->link_id;
1591 uint8_t sapi = rllh->link_id & 7;
1592 struct tlv_parsed tv;
1594 uint8_t n201 = 23; //FIXME
1596 /* Set chan_nr and link_id for established connection */
1597 memset(&dl->mctx, 0, sizeof(dl->mctx));
1599 dl->mctx.n201 = n201;
1600 dl->mctx.chan_nr = chan_nr;
1601 dl->mctx.link_id = link_id;
1603 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1604 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1605 /* contention resolution establishment procedure */
1607 /* According to clause 6, the contention resolution
1608 * procedure is only permitted with SAPI value 0 */
1609 LOGP(DLAPDM, LOGL_ERROR, "SAPI != 0 but contention"
1610 "resolution (discarding)\n");
1612 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1614 /* transmit a SABM command with the P bit set to "1". The SABM
1615 * command shall contain the layer 3 message unit */
1616 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1617 LOGP(DLAPDM, LOGL_INFO, "perform establishment with content "
1620 /* normal establishment procedure */
1622 LOGP(DLAPDM, LOGL_INFO, "perform normal establishm. (SABM)\n");
1625 /* check if the layer3 message length exceeds N201 */
1626 if (length + 3 > 21) { /* FIXME: do we know the channel N201? */
1627 LOGP(DLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) "
1628 "(discarding)\n", length + 3, 21);
1630 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1633 /* Flush send-queue */
1634 /* Clear send-buffer */
1635 lapdm_dl_flush_send(dl);
1637 /* Discard partly received L3 message */
1638 if (dl->rcv_buffer) {
1639 msgb_free(dl->rcv_buffer);
1640 dl->rcv_buffer = NULL;
1643 /* Remove RLL header from msgb */
1646 /* Push LAPDm header on msgb */
1647 msg->l2h = msgb_push(msg, 3);
1648 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1649 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
1650 msg->l2h[2] = LAPDm_LEN(length);
1651 /* Transmit-buffer carries exactly one segment */
1652 memcpy(dl->tx_hist[0], msg->l2h, 3 + length);
1653 dl->tx_length[0] = 3 + length;
1654 /* set Vs to 0, because it is used as index when resending SABM */
1658 dl->own_busy = dl->peer_busy = 0;
1659 dl->retrans_ctr = 0;
1660 lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT);
1662 /* Tramsmit and start T200 */
1663 osmo_timer_schedule(&dl->t200, T200);
1664 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1667 /* L3 requests transfer of unnumbered information */
1668 static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1670 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1671 uint8_t chan_nr = rllh->chan_nr;
1672 uint8_t link_id = rllh->link_id;
1673 uint8_t sapi = link_id & 7;
1674 struct tlv_parsed tv;
1676 uint8_t n201 = 23; //FIXME
1677 uint8_t ta = 0, tx_power = 0;
1679 /* check if the layer3 message length exceeds N201 */
1681 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1683 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
1684 ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
1686 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
1687 tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
1689 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1690 LOGP(DLAPDM, LOGL_ERROR, "unit data request without message "
1695 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1696 /* check if the layer3 message length exceeds N201 */
1697 if (length + 5 > 23) { /* FIXME: do we know the channel N201? */
1698 LOGP(DLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) "
1699 "(discarding)\n", length + 5, 23);
1704 LOGP(DLAPDM, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
1707 /* Remove RLL header from msgb */
1710 /* Push L1 + LAPDm header on msgb */
1711 msg->l2h = msgb_push(msg, 2 + 3);
1712 msg->l2h[0] = tx_power;
1714 msg->l2h[2] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1715 msg->l2h[3] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
1716 msg->l2h[4] = LAPDm_LEN(length);
1717 // FIXME: short L2 header support
1720 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1723 /* L3 requests transfer of acknowledged information */
1724 static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1726 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1727 struct tlv_parsed tv;
1729 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1730 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1731 LOGP(DLAPDM, LOGL_ERROR, "data request without message "
1737 LOGP(DLAPDM, LOGL_INFO, "writing message to send-queue\n");
1739 /* Remove the RSL/RLL header */
1742 /* Write data into the send queue */
1743 msgb_enqueue(&dl->send_queue, msg);
1745 /* Send message, if possible */
1746 rslms_send_i(&dl->mctx, __LINE__);
1750 /* Send next I frame from queued/buffered data */
1751 static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line)
1753 struct lapdm_datalink *dl = mctx->dl;
1754 uint8_t chan_nr = mctx->chan_nr;
1755 uint8_t link_id = mctx->link_id;
1756 uint8_t sapi = link_id & 7;
1757 int k = k_sapi[sapi];
1760 int rc = - 1; /* we sent nothing */
1762 LOGP(DLAPDM, LOGL_INFO, "%s() called from line %d\n", __func__, line);
1766 if (dl->peer_busy) {
1767 LOGP(DLAPDM, LOGL_INFO, "peer busy, not sending\n");
1771 if (dl->state == LAPDm_STATE_TIMER_RECOV) {
1772 LOGP(DLAPDM, LOGL_INFO, "timer recovery, not sending\n");
1776 /* If the send state variable V(S) is equal to V(A) plus k
1777 * (where k is the maximum number of outstanding I frames - see
1778 * subclause 5.8.4), the data link layer entity shall not transmit any
1779 * new I frames, but shall retransmit an I frame as a result
1780 * of the error recovery procedures as described in subclauses 5.5.4 and
1782 if (dl->V_send == add_mod8(dl->V_ack, k)) {
1783 LOGP(DLAPDM, LOGL_INFO, "k frames outstanding, not sending "
1784 "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->V_send,
1789 /* if we have no tx_hist yet, we create it */
1790 if (!dl->tx_length[dl->V_send]) {
1791 /* Get next message into send-buffer, if any */
1792 if (!dl->send_buffer) {
1795 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1796 /* No more data to be sent */
1797 if (!dl->send_buffer)
1799 LOGP(DLAPDM, LOGL_INFO, "get message from "
1803 /* How much is left in the send-buffer? */
1804 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1805 /* Segment, if data exceeds N201 */
1807 if (length > mctx->n201 - 3)
1808 length = mctx->n201 - 3;
1809 LOGP(DLAPDM, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
1810 "length %d first byte %02x\n",
1811 msgb_l3len(dl->send_buffer), dl->send_out, left,
1812 mctx->n201, length, dl->send_buffer->l3h[0]);
1813 /* If message in send-buffer is completely sent */
1815 msgb_free(dl->send_buffer);
1816 dl->send_buffer = NULL;
1820 LOGP(DLAPDM, LOGL_INFO, "send I frame %sV(S)=%d\n",
1821 (left > length) ? "segment " : "", dl->V_send);
1823 /* Create I frame (segment) and transmit-buffer content */
1824 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
1825 msg->l2h = msgb_put(msg, 3 + length);
1826 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1827 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
1828 msg->l2h[2] = LAPDm_LEN(length);
1830 msg->l2h[2] |= LAPDm_MORE;
1831 memcpy(msg->l2h + 3, dl->send_buffer->l3h + dl->send_out,
1833 memcpy(dl->tx_hist[dl->V_send], msg->l2h, 3 + length);
1834 dl->tx_length[dl->V_send] = 3 + length;
1835 /* Add length to track how much is already in the tx buffer */
1836 dl->send_out += length;
1838 LOGP(DLAPDM, LOGL_INFO, "resend I frame from tx buffer "
1839 "V(S)=%d\n", dl->V_send);
1841 /* Create I frame (segment) from tx_hist */
1842 length = dl->tx_length[dl->V_send];
1843 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
1844 msg->l2h = msgb_put(msg, length);
1845 memcpy(msg->l2h, dl->tx_hist[dl->V_send], length);
1846 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
1849 /* The value of the send state variable V(S) shall be incremented by 1
1850 * at the end of the transmission of the I frame */
1851 dl->V_send = inc_mod8(dl->V_send);
1853 /* If timer T200 is not running at the time right before transmitting a
1854 * frame, when the PH-READY-TO-SEND primitive is received from the
1855 * physical layer., it shall be set. */
1856 if (!osmo_timer_pending(&dl->t200))
1857 osmo_timer_schedule(&dl->t200, T200);
1859 tx_ph_data_enqueue(dl, msg, chan_nr, link_id, mctx->n201);
1861 rc = 0; /* we sent something */
1865 /* L3 requests suspension of data link */
1866 static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1868 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1869 uint8_t sapi = rllh->link_id & 7;
1872 LOGP(DLAPDM, LOGL_ERROR, "SAPI != 0 while suspending\n");
1877 LOGP(DLAPDM, LOGL_INFO, "perform suspension\n");
1879 /* put back the send-buffer to the send-queue (first position) */
1880 if (dl->send_buffer) {
1881 LOGP(DLAPDM, LOGL_INFO, "put frame in sendbuffer back to "
1883 llist_add(&dl->send_buffer->list, &dl->send_queue);
1884 dl->send_buffer = NULL;
1886 LOGP(DLAPDM, LOGL_INFO, "no frame in sendbuffer\n");
1888 /* Clear transmit buffer, but keep send buffer */
1889 lapdm_dl_flush_tx(dl);
1893 return send_rll_simple(RSL_MT_SUSP_CONF, &dl->mctx);
1896 /* L3 requests resume of data link */
1897 static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1899 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1900 uint8_t chan_nr = rllh->chan_nr;
1901 uint8_t link_id = rllh->link_id;
1902 uint8_t sapi = rllh->link_id & 7;
1903 struct tlv_parsed tv;
1905 uint8_t n201 = 23; //FIXME
1907 /* Set chan_nr and link_id for established connection */
1908 memset(&dl->mctx, 0, sizeof(dl->mctx));
1910 dl->mctx.n201 = n201;
1911 dl->mctx.chan_nr = chan_nr;
1912 dl->mctx.link_id = link_id;
1914 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1915 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1916 LOGP(DLAPDM, LOGL_ERROR, "resume without message error\n");
1918 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1920 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1922 LOGP(DLAPDM, LOGL_INFO, "perform re-establishment (SABM) length=%d\n",
1925 /* Replace message in the send-buffer (reconnect) */
1926 if (dl->send_buffer)
1927 msgb_free(dl->send_buffer);
1930 /* Remove the RSL/RLL header */
1932 /* Write data into the send buffer, to be sent first */
1933 dl->send_buffer = msg;
1936 /* Discard partly received L3 message */
1937 if (dl->rcv_buffer) {
1938 msgb_free(dl->rcv_buffer);
1939 dl->rcv_buffer = NULL;
1942 /* Create new msgb (old one is now free) */
1943 msg = msgb_alloc_headroom(23+10, 10, "LAPDm SABM");
1944 msg->l2h = msgb_put(msg, 3);
1945 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1946 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
1947 msg->l2h[2] = LAPDm_LEN(0);
1948 /* Transmit-buffer carries exactly one segment */
1949 memcpy(dl->tx_hist[0], msg->l2h, 3);
1950 dl->tx_length[0] = 3;
1951 /* set Vs to 0, because it is used as index when resending SABM */
1955 dl->own_busy = dl->peer_busy = 0;
1956 dl->retrans_ctr = 0;
1957 lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT);
1959 /* Tramsmit and start T200 */
1960 osmo_timer_schedule(&dl->t200, T200);
1961 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1964 /* L3 requests release of data link */
1965 static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1967 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1968 uint8_t chan_nr = rllh->chan_nr;
1969 uint8_t link_id = rllh->link_id;
1970 uint8_t sapi = rllh->link_id & 7;
1973 /* get release mode */
1974 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1975 mode = rllh->data[1] & 1;
1979 LOGP(DLAPDM, LOGL_INFO, "perform local release\n");
1981 /* reset Timer T200 */
1982 osmo_timer_del(&dl->t200);
1983 /* enter idle state */
1984 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1986 lapdm_dl_flush_tx(dl);
1987 lapdm_dl_flush_send(dl);
1988 /* send notification to L3 */
1989 return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
1992 /* in case we are already disconnecting */
1993 if (dl->state == LAPDm_STATE_DISC_SENT)
1996 LOGP(DLAPDM, LOGL_INFO, "perform normal release (DISC)\n");
1999 msgb_pull(msg, msg->tail - msg->l2h);
2001 /* Push LAPDm header on msgb */
2002 msg->l2h = msgb_push(msg, 3);
2003 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
2004 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DISC, 1);
2005 msg->l2h[2] = LAPDm_LEN(0);
2006 /* Transmit-buffer carries exactly one segment */
2007 memcpy(dl->tx_hist[0], msg->l2h, 3);
2008 dl->tx_length[0] = 3;
2011 dl->own_busy = dl->peer_busy = 0;
2012 dl->retrans_ctr = 0;
2013 lapdm_dl_newstate(dl, LAPDm_STATE_DISC_SENT);
2015 /* Tramsmit and start T200 */
2016 osmo_timer_schedule(&dl->t200, T200);
2017 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, dl->mctx.n201);
2020 /* L3 requests release in idle state */
2021 static int rslms_rx_rll_rel_req_idle(struct msgb *msg, struct lapdm_datalink *dl)
2025 /* send notification to L3 */
2026 return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
2029 /* L3 requests channel in idle state */
2030 static int rslms_rx_chan_rqd(struct osmocom_ms *ms, struct msgb *msg)
2032 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
2035 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
2036 LOGP(DRSL, LOGL_ERROR, "Message too short for CHAN RQD!\n");
2039 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
2040 LOGP(DRSL, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
2043 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
2044 LOGP(DRSL, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
2047 if (cch->data[6] != RSL_IE_MS_POWER) {
2048 LOGP(DRSL, LOGL_ERROR, "Missing MS POWER IE\n");
2052 /* TA = 0 - delay */
2053 rc = l1ctl_tx_param_req(ms, 0 - cch->data[5], cch->data[7]);
2055 rc = l1ctl_tx_rach_req(ms, cch->data[1],
2056 ((cch->data[2] & 0x7f) << 8) | cch->data[3], cch->data[2] >> 7);
2063 /* L1 confirms channel request */
2064 int l2_ph_chan_conf(struct msgb *msg, struct osmocom_ms *ms,
2065 struct l1ctl_info_dl *dl)
2067 struct abis_rsl_cchan_hdr *ch;
2069 struct gsm48_req_ref *ref;
2071 gsm_fn2gsmtime(&tm, htonl(dl->frame_nr));
2074 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
2075 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
2076 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
2077 ch->chan_nr = RSL_CHAN_RACH;
2078 ch->data[0] = RSL_IE_REQ_REFERENCE;
2079 ref = (struct gsm48_req_ref *) (ch->data + 1);
2082 ref->t3_low = tm.t3 & 0x7;
2083 ref->t3_high = tm.t3 >> 3;
2085 return rslms_sendmsg(msg, ms);
2088 /* Names for Radio Link Layer Management */
2089 static const struct value_string rsl_msg_names[] = {
2090 { RSL_MT_DATA_REQ, "RSL_MT_DATA_REQ" },
2091 { RSL_MT_DATA_IND, "RSL_MT_DATA_IND" },
2092 { RSL_MT_ERROR_IND, "RSL_MT_ERROR_IND" },
2093 { RSL_MT_EST_REQ, "RSL_MT_EST_REQ" },
2094 { RSL_MT_EST_CONF, "RSL_MT_EST_CONF" },
2095 { RSL_MT_EST_IND, "RSL_MT_EST_IND" },
2096 { RSL_MT_EST_IND, "RSL_MT_REL_REQ" },
2097 { RSL_MT_REL_REQ, "RSL_MT_REL_REQ" },
2098 { RSL_MT_REL_CONF, "RSL_MT_REL_CONF" },
2099 { RSL_MT_REL_IND, "RSL_MT_REL_IND" },
2100 { RSL_MT_UNIT_DATA_REQ, "RSL_MT_UNIT_DATA_REQ" },
2101 { RSL_MT_UNIT_DATA_IND, "RSL_MT_UNIT_DATA_IND" },
2102 { RSL_MT_SUSP_REQ, "RSL_MT_SUSP_REQ" },
2103 { RSL_MT_SUSP_CONF, "RSL_MT_SUSP_CONF" },
2104 { RSL_MT_RES_REQ, "RSL_MT_RES_REQ" },
2105 { RSL_MT_RECON_REQ, "RSL_MT_RECON_REQ" },
2106 { RSL_MT_CHAN_RQD, "RSL_MT_CHAN_RQD" },
2107 { RSL_MT_CHAN_CONF, "RSL_MT_CHAN_CONF" },
2111 const char *get_rsl_name(int value)
2113 return get_value_string(rsl_msg_names, value);
2116 const char *lapdm_state_names[] = {
2119 "LAPDm_STATE_SABM_SENT",
2120 "LAPDm_STATE_MF_EST",
2121 "LAPDm_STATE_TIMER_RECOV",
2122 "LAPDm_STATE_DISC_SENT",
2125 /* statefull handling for RSLms RLL messages from L3 */
2126 static struct l2downstate {
2129 int (*rout) (struct msgb *msg, struct lapdm_datalink *dl);
2130 } l2downstatelist[] = {
2131 /* create and send UI command */
2133 RSL_MT_UNIT_DATA_REQ, rslms_rx_rll_udata_req},
2135 /* create and send SABM command */
2136 {SBIT(LAPDm_STATE_IDLE),
2137 RSL_MT_EST_REQ, rslms_rx_rll_est_req},
2139 /* create and send I command */
2140 {SBIT(LAPDm_STATE_MF_EST) |
2141 SBIT(LAPDm_STATE_TIMER_RECOV),
2142 RSL_MT_DATA_REQ, rslms_rx_rll_data_req},
2144 /* suspend datalink */
2145 {SBIT(LAPDm_STATE_MF_EST) |
2146 SBIT(LAPDm_STATE_TIMER_RECOV),
2147 RSL_MT_SUSP_REQ, rslms_rx_rll_susp_req},
2149 /* create and send SABM command (resume) */
2150 {SBIT(LAPDm_STATE_MF_EST) |
2151 SBIT(LAPDm_STATE_TIMER_RECOV),
2152 RSL_MT_RES_REQ, rslms_rx_rll_res_req},
2154 /* create and send SABM command (reconnect) */
2155 {SBIT(LAPDm_STATE_IDLE) |
2156 SBIT(LAPDm_STATE_MF_EST) |
2157 SBIT(LAPDm_STATE_TIMER_RECOV),
2158 RSL_MT_RECON_REQ, rslms_rx_rll_res_req},
2160 /* create and send DISC command */
2161 {SBIT(LAPDm_STATE_SABM_SENT) |
2162 SBIT(LAPDm_STATE_MF_EST) |
2163 SBIT(LAPDm_STATE_TIMER_RECOV) |
2164 SBIT(LAPDm_STATE_DISC_SENT),
2165 RSL_MT_REL_REQ, rslms_rx_rll_rel_req},
2167 /* release in idle state */
2168 {SBIT(LAPDm_STATE_IDLE),
2169 RSL_MT_REL_REQ, rslms_rx_rll_rel_req_idle},
2172 #define L2DOWNSLLEN \
2173 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2175 /* incoming RSLms RLL message from L3 */
2176 static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
2178 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2179 int msg_type = rllh->c.msg_type;
2180 uint8_t sapi = rllh->link_id & 7;
2181 struct lapdm_entity *le;
2182 struct lapdm_datalink *dl;
2183 int i, supported = 0;
2186 if (msgb_l2len(msg) < sizeof(*rllh)) {
2187 LOGP(DRSL, LOGL_ERROR, "Message too short for RLL hdr!\n");
2191 if (rllh->link_id & 0x40)
2192 le = &ms->l2_entity.lapdm_acch;
2194 le = &ms->l2_entity.lapdm_dcch;
2196 /* G.2.1 No action schall be taken on frames containing an unallocated
2199 dl = datalink_for_sapi(le, sapi);
2201 LOGP(DRSL, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
2205 LOGP(DRSL, LOGL_INFO, "(ms %s) RLL Message '%s' received in state %s\n",
2206 ms->name, get_rsl_name(msg_type), lapdm_state_names[dl->state]);
2208 /* find function for current state and message */
2209 for (i = 0; i < L2DOWNSLLEN; i++) {
2210 if (msg_type == l2downstatelist[i].type)
2212 if ((msg_type == l2downstatelist[i].type)
2213 && ((1 << dl->state) & l2downstatelist[i].states))
2217 LOGP(DRSL, LOGL_NOTICE, "Message unsupported.\n");
2221 if (i == L2DOWNSLLEN) {
2222 LOGP(DRSL, LOGL_NOTICE, "Message unhandled at this state.\n");
2227 rc = l2downstatelist[i].rout(msg, dl);
2232 /* incoming RSLms COMMON CHANNEL message from L3 */
2233 static int rslms_rx_com_chan(struct msgb *msg, struct osmocom_ms *ms)
2235 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
2236 int msg_type = cch->c.msg_type;
2239 if (msgb_l2len(msg) < sizeof(*cch)) {
2240 LOGP(DRSL, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
2245 case RSL_MT_CHAN_RQD:
2246 /* create and send RACH request */
2247 rc = rslms_rx_chan_rqd(ms, msg);
2250 LOGP(DRSL, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
2259 /* input into layer2 (from layer 3) */
2260 int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms)
2262 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
2265 if (msgb_l2len(msg) < sizeof(*rslh)) {
2266 LOGP(DRSL, LOGL_ERROR, "Message too short RSL hdr!\n");
2270 switch (rslh->msg_discr & 0xfe) {
2271 case ABIS_RSL_MDISC_RLL:
2272 rc = rslms_rx_rll(msg, ms);
2274 case ABIS_RSL_MDISC_COM_CHAN:
2275 rc = rslms_rx_com_chan(msg, ms);
2278 LOGP(DRSL, LOGL_ERROR, "unknown RSLms message "
2279 "discriminator 0x%02x", rslh->msg_discr);
2287 /* input function that L2 calls when sending messages up to L3 */
2288 int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms)
2290 if (!ms->l2_entity.msg_handler) {
2295 /* call the layer2 message handler that is registered */
2296 return ms->l2_entity.msg_handler(msg, ms);
2299 /* register message handler for messages that are sent from L2->L3 */
2300 int osmol2_register_handler(struct osmocom_ms *ms, osmol2_cb_t cb)
2302 ms->l2_entity.msg_handler = cb;