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 <osmocore/logging.h>
60 #include <osmocore/timer.h>
61 #include <osmocore/msgb.h>
62 #include <osmocore/tlv.h>
63 #include <osmocore/utils.h>
64 #include <osmocore/rsl.h>
65 #include <osmocore/protocol/gsm_04_08.h>
66 #include <osmocore/protocol/gsm_08_58.h>
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);
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 */
302 printf("-> tx chan_nr 0x%x link_id 0x%x len %d data", chan_nr, link_id, msgb_l2len(msg));
304 for (i = 0; i < msgb_l2len(msg); i++)
305 printf(" %02x", msg->l2h[i]);
308 lapdm_pad_msgb(msg, n201);
309 return l1ctl_tx_data_req(ms, msg, chan_nr, link_id);
312 /* get next frame from the tx queue. because the ms has multiple datalinks,
313 * each datalink's queue is read round-robin.
315 int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
317 struct osmocom_ms *ms = le->ms;
318 struct lapdm_datalink *dl;
319 int last = le->last_tx_dequeue;
320 int i = last, n = ARRAY_SIZE(le->datalink);
321 uint8_t chan_nr, link_id, n201;
323 /* we may send again */
327 printf("-> tx confirm\n");
329 /* free confirm message */
332 /* round-robin dequeue */
336 dl = &le->datalink[i];
337 if ((msg = msgb_dequeue(&dl->tx_queue)))
341 /* no message in all queues */
345 /* Pull chan_nr and link_id */
346 chan_nr = *msg->data;
348 link_id = *msg->data;
353 /* Set last dequeue position */
354 le->last_tx_dequeue = i;
356 /* Pad the frame, we can transmit now */
359 printf("-> more tx chan_nr 0x%x link_id 0x%x len %d data", chan_nr, link_id, msgb_l2len(msg));
360 for (i = 0; i < msgb_l2len(msg); i++)
361 printf(" %02x", msg->l2h[i]);
364 lapdm_pad_msgb(msg, n201);
365 return l1ctl_tx_data_req(ms, msg, chan_nr, link_id);
368 /* Create RSLms various RSLms messages */
369 static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
372 /* Add the RSL + RLL header */
373 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
375 /* send off the RSLms message to L3 */
376 return rslms_sendmsg(msg, mctx->dl->entity->ms);
379 /* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
380 static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
382 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
383 struct abis_rsl_rll_hdr *rllh;
385 /* Add the RSL + RLL header */
386 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
387 msgb_push(msg, 2 + 2);
388 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
390 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
392 rllh->data[0] = RSL_IE_ACCESS_DELAY;
393 rllh->data[1] = mctx->ta_ind;
395 rllh->data[2] = RSL_IE_MS_POWER;
396 rllh->data[3] = mctx->tx_power_ind;
398 return rslms_sendmsg(msg, mctx->dl->entity->ms);
401 static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
405 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
407 /* send off the RSLms message to L3 */
408 return rslms_sendmsg(msg, mctx->dl->entity->ms);
411 static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
416 LOGP(DLAPDM, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
417 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
418 msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr) + 3);
419 tlv = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
420 tlv[0] = RSL_IE_RLM_CAUSE;
423 return rslms_sendmsg(msg, mctx->dl->entity->ms);
426 static int check_length_ind(struct lapdm_msg_ctx *mctx, uint8_t length_ind)
428 if (!(length_ind & 0x01)) {
429 /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION
430 * primitive with cause "frame not implemented" is sent to the
431 * mobile management entity. */
432 LOGP(DLAPDM, LOGL_NOTICE,
433 "we don't support multi-octet length\n");
434 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
440 static int lapdm_send_resend(struct lapdm_datalink *dl)
442 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm resend");
445 /* Resend SABM/DISC from tx_hist */
446 length = dl->tx_length[0];
447 msg->l2h = msgb_put(msg, length);
448 memcpy(msg->l2h, dl->tx_hist[dl->V_send], length);
450 return tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, dl->mctx.link_id,
454 static int lapdm_send_ua(struct lapdm_msg_ctx *mctx, uint8_t len, uint8_t *data)
456 uint8_t sapi = mctx->link_id & 7;
457 uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl);
458 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm UA");
459 msg->l2h = msgb_put(msg, 3 + len);
461 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
462 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UA, f_bit);
463 msg->l2h[2] = LAPDm_LEN(len);
465 memcpy(msg->l2h + 3, data, len);
467 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
471 static int lapdm_send_dm(struct lapdm_msg_ctx *mctx)
473 uint8_t sapi = mctx->link_id & 7;
474 uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl);
475 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm DM");
476 msg->l2h = msgb_put(msg, 3);
478 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
479 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DM, f_bit);
482 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
486 static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
488 uint8_t sapi = mctx->link_id & 7;
489 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RR");
490 msg->l2h = msgb_put(msg, 3);
492 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
493 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit);
494 msg->l2h[2] = LAPDm_LEN(0);
496 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
500 static int lapdm_send_rnr(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
502 uint8_t sapi = mctx->link_id & 7;
503 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RNR");
504 msg->l2h = msgb_put(msg, 3);
506 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
507 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RNR, f_bit);
508 msg->l2h[2] = LAPDm_LEN(0);
510 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
514 static int lapdm_send_rej(struct lapdm_msg_ctx *mctx, uint8_t f_bit)
516 uint8_t sapi = mctx->link_id & 7;
517 struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm REJ");
518 msg->l2h = msgb_put(msg, 3);
520 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_RESP);
521 msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_REJ, f_bit);
522 msg->l2h[2] = LAPDm_LEN(0);
524 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
528 /* Timer callback on T200 expiry */
529 static void lapdm_t200_cb(void *data)
531 struct lapdm_datalink *dl = data;
533 LOGP(DLAPDM, LOGL_INFO, "lapdm_t200_cb(%p) state=%u\n", dl, dl->state);
536 case LAPDm_STATE_SABM_SENT:
538 if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) {
539 /* send RELEASE INDICATION to L3 */
540 send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
541 /* send MDL ERROR INIDCATION to L3 */
542 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
543 /* go back to idle state */
544 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
545 /* NOTE: we must not change any other states or buffers
546 * and queues, since we may reconnect after handover
547 * failure. the buffered messages is replaced there */
550 /* retransmit SABM command */
551 lapdm_send_resend(dl);
552 /* increment re-transmission counter */
554 /* restart T200 (PH-READY-TO-SEND) */
555 bsc_schedule_timer(&dl->t200, T200);
557 case LAPDm_STATE_DISC_SENT:
559 if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) {
560 /* send RELEASE INDICATION to L3 */
561 send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
562 /* send MDL ERROR INIDCATION to L3 */
563 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
564 /* go back to idle state */
565 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
566 /* NOTE: we must not change any other states or buffers
567 * and queues, since we may reconnect after handover
568 * failure. the buffered messages is replaced there */
571 /* retransmit DISC command */
572 lapdm_send_resend(dl);
573 /* increment re-transmission counter */
575 /* restart T200 (PH-READY-TO-SEND) */
576 bsc_schedule_timer(&dl->t200, T200);
578 case LAPDm_STATE_MF_EST:
581 lapdm_dl_newstate(dl, LAPDm_STATE_TIMER_RECOV);
583 case LAPDm_STATE_TIMER_RECOV:
585 if (dl->retrans_ctr < N200) {
586 /* retransmit I frame (V_s-1) with P=1, if any */
587 if (dl->tx_length[dl->V_send - 1]) {
591 LOGP(DLAPDM, LOGL_INFO, "retransmit last frame "
592 "V(S)=%d\n", dl->V_send - 1);
593 /* Create I frame (segment) from tx_hist */
594 length = dl->tx_length[dl->V_send - 1];
595 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
596 msg->l2h = msgb_put(msg, length);
597 memcpy(msg->l2h, dl->tx_hist[dl->V_send - 1],
599 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv,
600 dl->V_send - 1, 1); /* P=1 */
601 tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr,
602 dl->mctx.link_id, dl->mctx.n201);
604 /* OR send appropriate supervision frame with P=1 */
605 if (!dl->own_busy && !dl->seq_err_cond) {
606 lapdm_send_rr(&dl->mctx, 1);
607 /* NOTE: In case of sequence error
608 * condition, the REJ frame has been
609 * transmitted when entering the
610 * condition, so it has not be done
613 } else if (dl->own_busy) {
614 lapdm_send_rnr(&dl->mctx, 1);
616 LOGP(DLAPDM, LOGL_INFO, "unhandled, "
620 /* restart T200 (PH-READY-TO-SEND) */
621 bsc_schedule_timer(&dl->t200, T200);
623 /* send MDL ERROR INIDCATION to L3 */
624 rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx);
628 LOGP(DLAPDM, LOGL_INFO, "T200 expired in unexpected "
629 "dl->state %u\n", dl->state);
633 /* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
634 static void lapdm_acknowledge(struct lapdm_msg_ctx *mctx)
636 struct lapdm_datalink *dl = mctx->dl;
637 uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl);
638 int s = 0, rej = 0, t200_reset = 0;
641 /* supervisory frame ? */
642 if (LAPDm_CTRL_is_S(mctx->ctrl))
645 if (s && LAPDm_CTRL_S_BITS(mctx->ctrl) == LAPDm_S_REJ)
648 /* Flush all transmit buffers of acknowledged frames */
649 for (i = dl->V_ack; i != nr; i = inc_mod8(i)) {
650 if (dl->tx_length[i]) {
651 dl->tx_length[i] = 0;
652 LOGP(DLAPDM, LOGL_INFO, "ack frame %d\n", i);
656 if (dl->state != LAPDm_STATE_TIMER_RECOV) {
657 /* When not in the timer recovery condition, the data
658 * link layer entity shall reset the timer T200 on
659 * receipt of a valid I frame with N(R) higher than V(A),
660 * or an REJ with an N(R) equal to V(A). */
661 if ((!rej && nr != dl->V_ack)
662 || (rej && nr == dl->V_ack)) {
663 LOGP(DLAPDM, LOGL_INFO, "reset t200\n");
665 bsc_del_timer(&dl->t200);
666 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
668 /* 5.7.4: N(R) sequence error
669 * N(R) is called valid, if and only if
670 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
672 if (sub_mod8(nr, dl->V_ack) > sub_mod8(dl->V_send, dl->V_ack)) {
673 LOGP(DLAPDM, LOGL_NOTICE, "N(R) sequence error\n");
674 rsl_rll_error(RLL_CAUSE_SEQ_ERR, mctx);
678 /* V(A) shall be set to the value of N(R) */
681 /* If T200 has been reset by the receipt of an I, RR or RNR frame,
682 * and if there are outstanding I frames, restart T200 */
683 if (t200_reset && !rej) {
684 if (dl->tx_length[dl->V_send - 1]) {
685 LOGP(DLAPDM, LOGL_INFO, "start T200, due to unacked I "
687 bsc_schedule_timer(&dl->t200, T200);
694 /* Receive a LAPDm U (Unnumbered) message from L1 */
695 static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx)
697 struct lapdm_datalink *dl = mctx->dl;
702 switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) {
704 rsl_msg = RSL_MT_EST_IND;
706 LOGP(DLAPDM, LOGL_INFO, "SABM received\n");
708 dl->seq_err_cond = 0;
709 /* G.2.2 Wrong value of the C/R bit */
710 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
711 LOGP(DLAPDM, LOGL_NOTICE, "SABM response error\n");
713 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
717 length = msg->l2h[2] >> 2;
718 /* G.4.5 If SABM is received with L>N201 or with M bit
719 * set, AN MDL-ERROR-INDICATION is sent to MM.
721 if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) {
722 LOGP(DLAPDM, LOGL_NOTICE, "SABM too large error\n");
724 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
728 /* Must be Format B */
729 rc = check_length_ind(mctx, msg->l2h[2]);
735 case LAPDm_STATE_IDLE:
736 /* Set chan_nr and link_id for established connection */
737 memset(&dl->mctx, 0, sizeof(dl->mctx));
739 dl->mctx.chan_nr = mctx->chan_nr;
740 dl->mctx.link_id = mctx->link_id;
742 case LAPDm_STATE_MF_EST:
744 rsl_msg = RSL_MT_EST_CONF;
747 LOGP(DLAPDM, LOGL_INFO, "SABM command, multiple "
748 "frame established state\n");
749 /* check for contention resoultion */
750 if (dl->tx_hist[0][2] >> 2) {
751 LOGP(DLAPDM, LOGL_NOTICE, "SABM not allowed "
752 "during contention resolution\n");
753 rsl_rll_error(RLL_CAUSE_SABM_INFO_NOTALL, mctx);
757 case LAPDm_STATE_DISC_SENT:
758 /* 5.4.6.2 send DM with F=P */
760 /* reset Timer T200 */
761 bsc_del_timer(&dl->t200);
763 return send_rll_simple(RSL_MT_REL_CONF, mctx);
765 lapdm_send_ua(mctx, length, msg->l2h + 3);
769 /* send UA response */
770 lapdm_send_ua(mctx, length, msg->l2h + 3);
771 /* set Vs, Vr and Va to 0 */
772 dl->V_send = dl->V_recv = dl->V_ack = 0;
774 dl->tx_length[0] = 0;
775 /* enter multiple-frame-established state */
776 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
777 /* send notification to L3 */
779 /* 5.4.1.2 Normal establishment procedures */
780 rc = send_rll_simple(rsl_msg, mctx);
783 /* 5.4.1.4 Contention resolution establishment */
784 msg->l3h = msg->l2h + 3;
786 rc = send_rslms_rll_l3(rsl_msg, mctx, msg);
790 LOGP(DLAPDM, LOGL_INFO, "DM received\n");
791 /* G.2.2 Wrong value of the C/R bit */
792 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
793 LOGP(DLAPDM, LOGL_NOTICE, "DM command error\n");
795 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
798 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
799 /* 5.4.1.2 DM responses with the F bit set to "0"
806 case LAPDm_STATE_SABM_SENT:
808 case LAPDm_STATE_MF_EST:
809 if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1) {
810 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
812 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP, mctx);
814 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
815 "response, multiple frame established "
817 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx);
821 case LAPDm_STATE_TIMER_RECOV:
822 /* DM is normal in case PF = 1 */
823 if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) {
824 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM "
825 "response, multiple frame established "
827 rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx);
832 case LAPDm_STATE_DISC_SENT:
833 /* reset Timer T200 */
834 bsc_del_timer(&dl->t200);
835 /* go to idle state */
836 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
837 rc = send_rll_simple(RSL_MT_REL_CONF, mctx);
840 case LAPDm_STATE_IDLE:
841 /* 5.4.5 all other frame types shall be discarded */
843 LOGP(DLAPDM, LOGL_INFO, "unsolicited DM response! "
849 bsc_del_timer(&dl->t200);
850 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
854 LOGP(DLAPDM, LOGL_INFO, "UI received\n");
855 /* G.2.2 Wrong value of the C/R bit */
856 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
857 LOGP(DLAPDM, LOGL_NOTICE, "UI indicates response "
860 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
864 length = msg->l2h[2] >> 2;
865 /* FIXME: G.4.5 If UI is received with L>N201 or with M bit
866 * set, AN MDL-ERROR-INDICATION is sent to MM.
869 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
871 msg->l3h = msg->l2h + 2;
873 rc = check_length_ind(mctx, msg->l2h[2]);
878 length = msg->l2h[2] >> 2;
879 msg->l3h = msg->l2h + 3;
881 /* do some length checks */
883 /* 5.3.3 UI frames received with the length indicator
884 * set to "0" shall be ignored
886 LOGP(DLAPDM, LOGL_INFO, "length=0 (discarding)\n");
890 switch (LAPDm_ADDR_SAPI(mctx->addr)) {
891 case LAPDm_SAPI_NORMAL:
895 /* 5.3.3 UI frames with invalid SAPI values shall be
898 LOGP(DLAPDM, LOGL_INFO, "sapi=%u (discarding)\n",
899 LAPDm_ADDR_SAPI(mctx->addr));
904 rc = send_rslms_rll_l3_ui(mctx, msg);
907 rsl_msg = RSL_MT_REL_IND;
909 LOGP(DLAPDM, LOGL_INFO, "DISC received\n");
911 lapdm_dl_flush_tx(dl);
912 lapdm_dl_flush_send(dl);
914 dl->seq_err_cond = 0;
915 /* G.2.2 Wrong value of the C/R bit */
916 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
917 LOGP(DLAPDM, LOGL_NOTICE, "DISC response error\n");
919 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
922 length = msg->l2h[2] >> 2;
923 if (length > 0 || msg->l2h[2] & 0x02) {
924 /* G.4.4 If a DISC or DM frame is received with L>0 or
925 * with the M bit set to "1", an MDL-ERROR-INDICATION
926 * primitive with cause "U frame with incorrect
927 * parameters" is sent to the mobile management entity. */
928 LOGP(DLAPDM, LOGL_NOTICE,
929 "U frame iwth incorrect parameters ");
931 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
936 case LAPDm_STATE_IDLE:
937 LOGP(DLAPDM, LOGL_INFO, "DISC in idle state\n");
938 /* send DM with F=P */
939 return lapdm_send_dm(mctx);
940 case LAPDm_STATE_SABM_SENT:
941 LOGP(DLAPDM, LOGL_INFO, "DISC in SABM state\n");
942 /* 5.4.6.2 send DM with F=P */
944 /* reset Timer T200 */
945 bsc_del_timer(&dl->t200);
946 return send_rll_simple(RSL_MT_REL_IND, mctx);
947 case LAPDm_STATE_MF_EST:
948 case LAPDm_STATE_TIMER_RECOV:
949 LOGP(DLAPDM, LOGL_INFO, "DISC in est state\n");
951 case LAPDm_STATE_DISC_SENT:
952 LOGP(DLAPDM, LOGL_INFO, "DISC in disc state\n");
953 rsl_msg = RSL_MT_REL_CONF;
956 lapdm_send_ua(mctx, length, msg->l2h + 3);
959 /* send UA response */
960 lapdm_send_ua(mctx, length, msg->l2h + 3);
961 /* reset Timer T200 */
962 bsc_del_timer(&dl->t200);
963 /* enter idle state */
964 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
965 /* send notification to L3 */
966 rc = send_rll_simple(rsl_msg, mctx);
969 LOGP(DLAPDM, LOGL_INFO, "UA received\n");
970 /* G.2.2 Wrong value of the C/R bit */
971 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
972 LOGP(DLAPDM, LOGL_NOTICE, "UA indicates command "
975 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
979 length = msg->l2h[2] >> 2;
980 /* G.4.5 If UA is received with L>N201 or with M bit
981 * set, AN MDL-ERROR-INDICATION is sent to MM.
983 if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) {
984 LOGP(DLAPDM, LOGL_NOTICE, "UA too large error\n");
986 rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx);
990 if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
991 /* 5.4.1.2 A UA response with the F bit set to "0"
994 LOGP(DLAPDM, LOGL_INFO, "F=0 (discarding)\n");
999 case LAPDm_STATE_SABM_SENT:
1001 case LAPDm_STATE_MF_EST:
1002 case LAPDm_STATE_TIMER_RECOV:
1003 LOGP(DLAPDM, LOGL_INFO, "unsolicited UA response! "
1005 rsl_rll_error(RLL_CAUSE_UNSOL_UA_RESP, mctx);
1008 case LAPDm_STATE_DISC_SENT:
1009 LOGP(DLAPDM, LOGL_INFO, "UA in disconnect state\n");
1010 /* reset Timer T200 */
1011 bsc_del_timer(&dl->t200);
1012 /* go to idle state */
1013 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1014 rc = send_rll_simple(RSL_MT_REL_CONF, mctx);
1017 case LAPDm_STATE_IDLE:
1018 /* 5.4.5 all other frame types shall be discarded */
1020 LOGP(DLAPDM, LOGL_INFO, "unsolicited UA response! "
1025 LOGP(DLAPDM, LOGL_INFO, "UA in SABM state\n");
1026 /* reset Timer T200 */
1027 bsc_del_timer(&dl->t200);
1028 /* compare UA with SABME if contention resolution is applied */
1029 if (dl->tx_hist[0][2] >> 2) {
1030 rc = check_length_ind(mctx, msg->l2h[2]);
1032 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
1034 /* go to idle state */
1035 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1038 length = msg->l2h[2] >> 2;
1039 if (length != (dl->tx_hist[0][2] >> 2)
1040 || !!memcmp(dl->tx_hist[0] + 3, msg->l2h + 3,
1042 LOGP(DLAPDM, LOGL_INFO, "UA response "
1045 for (i = 0; i < (dl->tx_hist[0][2] >> 2); i++)
1046 printf(" %02x", dl->tx_hist[0][3+i]);
1047 printf(" == SENT\n");
1048 for (i = 0; i < length; i++)
1049 printf(" %02x", msg->l2h[3+i]);
1050 printf(" == RECEIVED (len=%d,%d)\n", msg->l2h[2] >> 2, length);
1051 rc = send_rll_simple(RSL_MT_REL_IND, mctx);
1053 /* go to idle state */
1054 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1058 /* set Vs, Vr and Va to 0 */
1059 dl->V_send = dl->V_recv = dl->V_ack = 0;
1061 dl->tx_length[0] = 0;
1062 /* enter multiple-frame-established state */
1063 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1064 /* send outstanding frames, if any (resume / reconnect) */
1066 /* send notification to L3 */
1067 rc = send_rll_simple(RSL_MT_EST_CONF, mctx);
1072 LOGP(DLAPDM, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1074 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1080 /* Receive a LAPDm S (Supervisory) message from L1 */
1081 static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1083 struct lapdm_datalink *dl = mctx->dl;
1086 length = msg->l2h[2] >> 2;
1087 if (length > 0 || msg->l2h[2] & 0x02) {
1088 /* G.4.3 If a supervisory frame is received with L>0 or
1089 * with the M bit set to "1", an MDL-ERROR-INDICATION
1090 * primitive with cause "S frame with incorrect
1091 * parameters" is sent to the mobile management entity. */
1092 LOGP(DLAPDM, LOGL_NOTICE,
1093 "S frame with incorrect parameters\n");
1095 rsl_rll_error(RLL_CAUSE_SFRM_INC_PARAM, mctx);
1098 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1099 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1100 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1101 LOGP(DLAPDM, LOGL_NOTICE, "S frame response with F=1 error\n");
1102 rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
1105 switch (dl->state) {
1106 case LAPDm_STATE_IDLE:
1107 /* if P=1, respond DM with F=1 (5.2.2) */
1108 /* 5.4.5 all other frame types shall be discarded */
1109 if (LAPDm_CTRL_PF_BIT(mctx->ctrl))
1110 lapdm_send_dm(mctx); /* F=P */
1112 case LAPDm_STATE_SABM_SENT:
1113 case LAPDm_STATE_DISC_SENT:
1114 LOGP(DLAPDM, LOGL_NOTICE, "S frame ignored in this state\n");
1118 switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) {
1120 LOGP(DLAPDM, LOGL_INFO, "RR received\n");
1121 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1122 lapdm_acknowledge(mctx);
1125 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1126 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1127 if (!dl->own_busy && !dl->seq_err_cond) {
1128 LOGP(DLAPDM, LOGL_NOTICE, "RR frame command with polling bit set and we are not busy, so we reply with RR frame\n");
1129 lapdm_send_rr(mctx, 1);
1130 /* NOTE: In case of sequence error condition,
1131 * the REJ frame has been transmitted when
1132 * entering the condition, so it has not be
1135 } else if (dl->own_busy) {
1136 LOGP(DLAPDM, LOGL_NOTICE, "RR frame command with polling bit set and we are busy, so we reply with RR frame\n");
1137 lapdm_send_rnr(mctx, 1);
1140 /* Send message, if possible due to acknowledged data */
1145 LOGP(DLAPDM, LOGL_INFO, "RNR received\n");
1146 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1147 lapdm_acknowledge(mctx);
1150 /* Set peer receiver busy condition */
1153 if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1154 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD) {
1155 if (!dl->own_busy) {
1156 LOGP(DLAPDM, LOGL_INFO, "RNR poll "
1157 "command and we are not busy, "
1158 "so we reply with RR final "
1160 /* Send RR with F=1 */
1161 lapdm_send_rr(mctx, 1);
1163 LOGP(DLAPDM, LOGL_INFO, "RNR poll "
1164 "command and we are busy, so "
1165 "we reply with RNR final "
1167 /* Send RNR with F=1 */
1168 lapdm_send_rnr(mctx, 1);
1170 } else if (dl->state == LAPDm_STATE_TIMER_RECOV) {
1171 LOGP(DLAPDM, LOGL_INFO, "RNR poll response "
1172 "and we in timer recovery state, so "
1173 "we leave that state\n");
1174 /* Clear timer recovery condition */
1175 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1176 /* V(S) to the N(R) in the RNR frame */
1177 dl->V_send = LAPDm_CTRL_Nr(mctx->ctrl);
1180 LOGP(DLAPDM, LOGL_INFO, "RNR not polling/final state "
1183 /* Send message, if possible due to acknowledged data */
1188 LOGP(DLAPDM, LOGL_INFO, "REJ received\n");
1189 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1190 lapdm_acknowledge(mctx);
1193 if (dl->state != LAPDm_STATE_TIMER_RECOV) {
1194 /* Clear an existing peer receiver busy condition */
1196 /* V(S) and V(A) to the N(R) in the REJ frame */
1197 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1198 /* reset Timer T200 */
1199 bsc_del_timer(&dl->t200);
1201 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1202 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1203 if (!dl->own_busy && !dl->seq_err_cond) {
1204 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1205 "command not in timer recovery "
1206 "state and not in own busy "
1207 "condition received, so we "
1208 "respond with RR final "
1210 lapdm_send_rr(mctx, 1);
1211 /* NOTE: In case of sequence error
1212 * condition, the REJ frame has been
1213 * transmitted when entering the
1214 * condition, so it has not be done
1217 } else if (dl->own_busy) {
1218 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1219 "command not in timer recovery "
1220 "state and in own busy "
1221 "condition received, so we "
1222 "respond with RNR final "
1224 lapdm_send_rnr(mctx, 1);
1227 LOGP(DLAPDM, LOGL_INFO, "REJ response or not "
1228 "polling command not in timer recovery "
1229 "state received\n");
1230 /* send MDL ERROR INIDCATION to L3 */
1231 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1232 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1233 rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx);
1236 } else if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP
1237 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1238 LOGP(DLAPDM, LOGL_INFO, "REJ poll response in timer "
1239 "recovery state received\n");
1240 /* Clear an existing peer receiver busy condition */
1242 /* Clear timer recovery condition */
1243 lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST);
1244 /* V(S) and V(A) to the N(R) in the REJ frame */
1245 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1246 /* reset Timer T200 */
1247 bsc_del_timer(&dl->t200);
1249 /* Clear an existing peer receiver busy condition */
1251 /* V(S) and V(A) to the N(R) in the REJ frame */
1252 dl->V_send = dl->V_ack = LAPDm_CTRL_Nr(mctx->ctrl);
1254 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_CMD
1255 && LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1256 if (!dl->own_busy && !dl->seq_err_cond) {
1257 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1258 "command in timer recovery "
1259 "state and not in own busy "
1260 "condition received, so we "
1261 "respond with RR final "
1263 lapdm_send_rr(mctx, 1);
1264 /* NOTE: In case of sequence error
1265 * condition, the REJ frame has been
1266 * transmitted when entering the
1267 * condition, so it has not be done
1270 } else if (dl->own_busy) {
1271 LOGP(DLAPDM, LOGL_INFO, "REJ poll "
1272 "command in timer recovery "
1273 "state and in own busy "
1274 "condition received, so we "
1275 "respond with RNR final "
1277 lapdm_send_rnr(mctx, 1);
1280 LOGP(DLAPDM, LOGL_INFO, "REJ response or not "
1281 "polling command in timer recovery "
1282 "state received\n");
1285 /* FIXME: 5.5.4.2 2) */
1287 /* Send message, if possible due to acknowledged data */
1293 LOGP(DLAPDM, LOGL_NOTICE, "Supervisory frame not allowed.\n");
1295 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1302 /* Receive a LAPDm I (Information) message from L1 */
1303 static int lapdm_rx_i(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1305 struct lapdm_datalink *dl = mctx->dl;
1306 //uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl);
1307 uint8_t ns = LAPDm_CTRL_I_Ns(mctx->ctrl);
1311 LOGP(DLAPDM, LOGL_NOTICE, "I received\n");
1313 /* G.2.2 Wrong value of the C/R bit */
1314 if (LAPDm_ADDR_CR(mctx->addr) == CR_BS2MS_RESP) {
1315 LOGP(DLAPDM, LOGL_NOTICE, "I frame response not allowed\n");
1317 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1321 length = msg->l2h[2] >> 2;
1322 if (length == 0 || length + 3 > mctx->n201) {
1323 /* G.4.2 If the length indicator of an I frame is set
1324 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1325 * primitive with cause "I frame with incorrect length"
1326 * is sent to the mobile management entity. */
1327 LOGP(DLAPDM, LOGL_NOTICE, "I frame length not allowed\n");
1329 rsl_rll_error(RLL_CAUSE_IFRM_INC_LEN, mctx);
1333 /* G.4.2 If the numerical value of L is L<N201 and the M
1334 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1335 * cause "I frame with incorrect use of M bit" is sent to the
1336 * mobile management entity. */
1337 if ((msg->l2h[2] & LAPDm_MORE) && length + 3 < mctx->n201) {
1338 LOGP(DLAPDM, LOGL_NOTICE, "I frame with M bit too short\n");
1340 rsl_rll_error(RLL_CAUSE_IFRM_INC_MBITS, mctx);
1344 switch (dl->state) {
1345 case LAPDm_STATE_IDLE:
1346 /* if P=1, respond DM with F=1 (5.2.2) */
1347 /* 5.4.5 all other frame types shall be discarded */
1348 if (LAPDm_CTRL_PF_BIT(mctx->ctrl))
1349 lapdm_send_dm(mctx); /* F=P */
1351 case LAPDm_STATE_SABM_SENT:
1352 case LAPDm_STATE_DISC_SENT:
1353 LOGP(DLAPDM, LOGL_NOTICE, "I frame ignored in this state\n");
1358 /* 5.7.1: N(s) sequence error */
1359 if (ns != dl->V_recv) {
1360 LOGP(DLAPDM, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1361 "V(R)=%u\n", ns, dl->V_recv);
1364 if (!dl->seq_err_cond) {
1365 /* FIXME: help me understand what exactly todo here
1366 dl->seq_err_cond = 1;
1368 lapdm_send_rej(mctx, LAPDm_CTRL_PF_BIT(mctx->ctrl));
1373 dl->seq_err_cond = 0;
1375 /* Increment receiver state */
1376 dl->V_recv = inc_mod8(dl->V_recv);
1377 LOGP(DLAPDM, LOGL_NOTICE, "incrementing V(R) to %u\n", dl->V_recv);
1379 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1380 lapdm_acknowledge(mctx); /* V(A) is also set here */
1382 /* Only if we are not in own receiver busy condition */
1383 if (!dl->own_busy) {
1384 /* if the frame carries a complete segment */
1385 if (!(msg->l2h[2] & LAPDm_MORE)
1386 && !dl->rcv_buffer) {
1387 LOGP(DLAPDM, LOGL_INFO, "message in single I frame\n");
1388 /* send a DATA INDICATION to L3 */
1389 msg->l3h = msg->l2h + 3;
1391 rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx, msg);
1393 /* create rcv_buffer */
1394 if (!dl->rcv_buffer) {
1395 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1396 "frames (first message)\n");
1397 dl->rcv_buffer = msgb_alloc_headroom(200+56, 56,
1399 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1401 /* concat. rcv_buffer */
1402 if (msgb_l3len(dl->rcv_buffer) + length > 200) {
1403 LOGP(DLAPDM, LOGL_NOTICE, "Received frame "
1406 memcpy(msgb_put(dl->rcv_buffer, length),
1407 msg->l2h + 3, length);
1409 /* if the last segment was received */
1410 if (!(msg->l2h[2] & LAPDm_MORE)) {
1411 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1412 "frames (next message)\n");
1413 rc = send_rslms_rll_l3(RSL_MT_DATA_IND, mctx,
1415 dl->rcv_buffer = NULL;
1417 LOGP(DLAPDM, LOGL_INFO, "message in multiple I "
1418 "frames (last message)\n");
1423 LOGP(DLAPDM, LOGL_INFO, "I frame ignored during own receiver "
1424 "busy condition\n");
1426 /* Check for P bit */
1427 if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) {
1429 /* check if we are not in own receiver busy */
1430 if (!dl->own_busy) {
1431 LOGP(DLAPDM, LOGL_INFO, "we are not busy, send RR\n");
1432 /* Send RR with F=1 */
1433 rc = lapdm_send_rr(mctx, 1);
1435 LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
1436 /* Send RNR with F=1 */
1437 rc = lapdm_send_rnr(mctx, 1);
1441 /* check if we are not in own receiver busy */
1442 if (!dl->own_busy) {
1443 /* NOTE: V(R) is already set above */
1444 rc = rslms_send_i(mctx);
1446 LOGP(DLAPDM, LOGL_INFO, "we are not busy and "
1447 "have no pending data, send RR\n");
1448 /* Send RR with F=0 */
1449 return lapdm_send_rr(mctx, 0);
1452 LOGP(DLAPDM, LOGL_INFO, "we are busy, send RNR\n");
1453 /* Send RNR with F=0 */
1454 rc = lapdm_send_rnr(mctx, 0);
1458 /* Send message, if possible due to acknowledged data */
1464 /* Receive a LAPDm message from L1 */
1465 static int lapdm_ph_data_ind(struct msgb *msg, struct lapdm_msg_ctx *mctx)
1470 printf("-> rx chan_nr 0x%x link_id 0x%x len %d data", mctx->chan_nr, mctx->link_id, msgb_l2len(msg));
1472 for (i = 0; i < msgb_l2len(msg); i++)
1473 printf(" %02x", msg->l2h[i]);
1476 /* G.2.3 EA bit set to "0" is not allowed in GSM */
1477 if (!LAPDm_ADDR_EA(mctx->addr)) {
1478 LOGP(DLAPDM, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
1480 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx);
1484 if (LAPDm_CTRL_is_U(mctx->ctrl))
1485 rc = lapdm_rx_u(msg, mctx);
1486 else if (LAPDm_CTRL_is_S(mctx->ctrl))
1487 rc = lapdm_rx_s(msg, mctx);
1488 else if (LAPDm_CTRL_is_I(mctx->ctrl))
1489 rc = lapdm_rx_i(msg, mctx);
1491 LOGP(DLAPDM, LOGL_NOTICE, "unknown LAPDm format\n");
1498 /* input into layer2 (from layer 1) */
1499 int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, struct l1ctl_info_dl *l1i)
1501 uint8_t cbits = l1i->chan_nr >> 3;
1502 uint8_t sapi = l1i->link_id & 7;
1503 struct lapdm_msg_ctx mctx;
1506 /* when we reach here, we have a msgb with l2h pointing to the raw
1507 * 23byte mac block. The l1h has already been purged. */
1509 mctx.dl = datalink_for_sapi(le, sapi);
1510 mctx.chan_nr = l1i->chan_nr;
1511 mctx.link_id = l1i->link_id;
1512 mctx.addr = mctx.ctrl = 0;
1514 /* G.2.1 No action schall be taken on frames containing an unallocated
1518 LOGP(DLAPDM, LOGL_NOTICE, "Received frame for unsupported "
1519 "SAPI %d!\n", sapi);
1525 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
1526 if (cbits == 0x10 || cbits == 0x12) {
1527 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
1528 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
1529 mctx.n201 = N201_Bbis;
1531 if (mctx.link_id & 0x40) {
1532 /* It was received from network on SACCH, thus
1533 * lapdm_fmt must be B4 */
1534 mctx.lapdm_fmt = LAPDm_FMT_B4;
1535 mctx.n201 = N201_B4;
1536 LOGP(DLAPDM, LOGL_INFO, "fmt=B4\n");
1537 /* SACCH frames have a two-byte L1 header that
1538 * OsmocomBB L1 doesn't strip */
1539 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
1540 mctx.ta_ind = msg->l2h[1];
1544 mctx.lapdm_fmt = LAPDm_FMT_B;
1545 LOGP(DLAPDM, LOGL_INFO, "fmt=B\n");
1546 mctx.n201 = 23; // FIXME: select correct size by chan.
1550 switch (mctx.lapdm_fmt) {
1554 mctx.addr = msg->l2h[0];
1555 if (!(mctx.addr & 0x01)) {
1556 LOGP(DLAPDM, LOGL_ERROR, "we don't support "
1557 "multibyte addresses (discarding)\n");
1561 mctx.ctrl = msg->l2h[1];
1562 /* obtain SAPI from address field */
1563 mctx.link_id |= LAPDm_ADDR_SAPI(mctx.addr);
1564 rc = lapdm_ph_data_ind(msg, &mctx);
1566 case LAPDm_FMT_Bter:
1570 case LAPDm_FMT_Bbis:
1571 /* directly pass up to layer3 */
1572 LOGP(DLAPDM, LOGL_INFO, "fmt=Bbis UI\n");
1573 msg->l3h = msg->l2h;
1575 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
1584 /* L3 -> L2 / RSLMS -> LAPDm */
1586 /* L3 requests establishment of data link */
1587 static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
1589 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1590 uint8_t chan_nr = rllh->chan_nr;
1591 uint8_t link_id = rllh->link_id;
1592 uint8_t sapi = rllh->link_id & 7;
1593 struct tlv_parsed tv;
1595 uint8_t n201 = 23; //FIXME
1597 /* Set chan_nr and link_id for established connection */
1598 memset(&dl->mctx, 0, sizeof(dl->mctx));
1600 dl->mctx.n201 = n201;
1601 dl->mctx.chan_nr = chan_nr;
1602 dl->mctx.link_id = link_id;
1604 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1605 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1606 /* contention resolution establishment procedure */
1608 /* According to clause 6, the contention resolution
1609 * procedure is only permitted with SAPI value 0 */
1610 LOGP(DLAPDM, LOGL_ERROR, "SAPI != 0 but contention"
1611 "resolution (discarding)\n");
1613 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1615 /* transmit a SABM command with the P bit set to "1". The SABM
1616 * command shall contain the layer 3 message unit */
1617 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1618 LOGP(DLAPDM, LOGL_INFO, "perform establishment with content "
1621 /* normal establishment procedure */
1623 LOGP(DLAPDM, LOGL_INFO, "perform normal establishm. (SAMB)\n");
1626 /* check if the layer3 message length exceeds N201 */
1627 if (length + 3 > 21) { /* FIXME: do we know the channel N201? */
1628 LOGP(DLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) "
1629 "(discarding)\n", length + 3, 21);
1631 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1634 /* Flush send-queue */
1635 /* Clear send-buffer */
1636 lapdm_dl_flush_send(dl);
1638 /* Discard partly received L3 message */
1639 if (dl->rcv_buffer) {
1640 msgb_free(dl->rcv_buffer);
1641 dl->rcv_buffer = NULL;
1644 /* Remove RLL header from msgb */
1647 /* Push LAPDm header on msgb */
1648 msg->l2h = msgb_push(msg, 3);
1649 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1650 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
1651 msg->l2h[2] = LAPDm_LEN(length);
1652 /* Transmit-buffer carries exactly one segment */
1653 memcpy(dl->tx_hist[0], msg->l2h, 3 + length);
1654 dl->tx_length[0] = 3 + length;
1655 /* set Vs to 0, because it is used as index when resending SAMB */
1659 dl->own_busy = dl->peer_busy = 0;
1660 dl->retrans_ctr = 0;
1661 lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT);
1663 /* Tramsmit and start T200 */
1664 bsc_schedule_timer(&dl->t200, T200);
1665 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1668 /* L3 requests transfer of unnumbered information */
1669 static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1671 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1672 uint8_t chan_nr = rllh->chan_nr;
1673 uint8_t link_id = rllh->link_id;
1674 uint8_t sapi = link_id & 7;
1675 struct tlv_parsed tv;
1677 uint8_t n201 = 23; //FIXME
1679 /* check if the layer3 message length exceeds N201 */
1681 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1683 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1684 /* check if the layer3 message length exceeds N201 */
1685 if (length + 3 > 18) { /* FIXME: do we know the channel N201? */
1686 LOGP(DLAPDM, LOGL_ERROR, "frame too large: %d > N201(%d) "
1687 "(discarding)\n", length + 3, 18);
1692 LOGP(DLAPDM, LOGL_INFO, "sending unit data\n");
1694 /* Remove RLL header from msgb */
1697 /* Push LAPDm header on msgb */
1698 msg->l2h = msgb_push(msg, 3);
1699 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1700 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
1701 msg->l2h[2] = LAPDm_LEN(length);
1702 // FIXME: short L2 header support
1705 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1708 /* L3 requests transfer of acknowledged information */
1709 static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1711 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1712 struct tlv_parsed tv;
1714 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1715 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1716 LOGP(DLAPDM, LOGL_ERROR, "data request without message error\n");
1721 LOGP(DLAPDM, LOGL_INFO, "writing message to send-queue\n");
1723 /* Remove the RSL/RLL header */
1726 /* Write data into the send queue */
1727 msgb_enqueue(&dl->send_queue, msg);
1729 /* Send message, if possible */
1730 rslms_send_i(&dl->mctx);
1734 /* Send next I frame from queued/buffered data */
1735 static int rslms_send_i(struct lapdm_msg_ctx *mctx)
1737 struct lapdm_datalink *dl = mctx->dl;
1738 uint8_t chan_nr = mctx->chan_nr;
1739 uint8_t link_id = mctx->link_id;
1740 uint8_t sapi = link_id & 7;
1741 int k = k_sapi[sapi];
1744 int rc = -1; /* we sent nothing */
1748 if (dl->peer_busy) {
1749 LOGP(DLAPDM, LOGL_INFO, "peer busy, not sending\n");
1753 if (dl->state == LAPDm_STATE_TIMER_RECOV) {
1754 LOGP(DLAPDM, LOGL_INFO, "timer recovery, not sending\n");
1758 /* If the send state variable V(S) is equal to V(A) plus k
1759 * (where k is the maximum number of outstanding I frames - see
1760 * subclause 5.8.4), the data link layer entity shall not transmit any
1761 * new I frames, but shall retransmit an I frame as a result
1762 * of the error recovery procedures as described in subclauses 5.5.4 and
1764 if (dl->V_send == add_mod8(dl->V_ack, k)) {
1765 LOGP(DLAPDM, LOGL_INFO, "k frames outstanding, not sending "
1766 "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->V_send,
1771 /* if we have no tx_hist yet, we create it */
1772 if (!dl->tx_length[dl->V_send]) {
1773 /* Get next message into send-buffer, if any */
1774 if (!dl->send_buffer) {
1777 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1778 /* No more data to be sent */
1779 if (!dl->send_buffer)
1781 LOGP(DLAPDM, LOGL_INFO, "get message from "
1785 /* How much is left in the send-buffer? */
1786 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1787 /* Segment, if data exceeds N201 */
1789 if (length > mctx->n201 - 3)
1790 length = mctx->n201 - 3;
1791 printf("msg-len %d sent %d left %d N201 %d length %d first byte %02x\n", msgb_l3len(dl->send_buffer), dl->send_out, left, mctx->n201, length, dl->send_buffer->l3h[0]);
1792 /* If message in send-buffer is completely sent */
1794 msgb_free(dl->send_buffer);
1795 dl->send_buffer = NULL;
1799 LOGP(DLAPDM, LOGL_INFO, "send I frame %sV(S)=%d\n",
1800 (left > length) ? "segment " : "", dl->V_send);
1802 /* Create I frame (segment) and transmit-buffer content */
1803 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
1804 msg->l2h = msgb_put(msg, 3 + length);
1805 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1806 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
1807 msg->l2h[2] = LAPDm_LEN(length);
1809 msg->l2h[2] |= LAPDm_MORE;
1810 memcpy(msg->l2h + 3, dl->send_buffer->l3h + dl->send_out,
1812 memcpy(dl->tx_hist[dl->V_send], msg->l2h, 3 + length);
1813 dl->tx_length[dl->V_send] = 3 + length;
1814 /* Add length to track how much is already in the tx buffer */
1815 dl->send_out += length;
1817 LOGP(DLAPDM, LOGL_INFO, "resend I frame from tx buffer "
1818 "V(S)=%d\n", dl->V_send);
1820 /* Create I frame (segment) from tx_hist */
1821 length = dl->tx_length[dl->V_send];
1822 msg = msgb_alloc_headroom(23+10, 10, "LAPDm I");
1823 msg->l2h = msgb_put(msg, length);
1824 memcpy(msg->l2h, dl->tx_hist[dl->V_send], length);
1825 msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, dl->V_send, 0);
1828 /* The value of the send state variable V(S) shall be incremented by 1
1829 * at the end of the transmission of the I frame */
1830 dl->V_send = inc_mod8(dl->V_send);
1832 /* If timer T200 is not running at the time right before transmitting a
1833 * frame, when the PH-READY-TO-SEND primitive is received from the
1834 * physical layer., it shall be set. */
1835 if (!bsc_timer_pending(&dl->t200))
1836 bsc_schedule_timer(&dl->t200, T200);
1838 tx_ph_data_enqueue(dl, msg, chan_nr, link_id, mctx->n201);
1840 rc = 0; /* we sent something */
1844 /* L3 requests suspension of data link */
1845 static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1847 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1848 uint8_t sapi = rllh->link_id & 7;
1851 LOGP(DLAPDM, LOGL_ERROR, "SAPI != 0 while suspending\n");
1856 LOGP(DLAPDM, LOGL_INFO, "perform suspension\n");
1858 /* put back the send-buffer to the send-queue (first position) */
1859 if (dl->send_buffer) {
1860 llist_add(&dl->send_buffer->list, &dl->send_queue);
1861 dl->send_buffer = NULL;
1864 /* Clear transmit and send buffer, if any */
1865 lapdm_dl_flush_tx(dl);
1866 lapdm_dl_flush_send(dl);
1870 return send_rll_simple(RSL_MT_SUSP_CONF, &dl->mctx);
1873 /* L3 requests resume of data link */
1874 static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1876 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1877 uint8_t chan_nr = rllh->chan_nr;
1878 uint8_t link_id = rllh->link_id;
1879 uint8_t sapi = rllh->link_id & 7;
1880 struct tlv_parsed tv;
1882 uint8_t n201 = 23; //FIXME
1884 /* Set chan_nr and link_id for established connection */
1885 memset(&dl->mctx, 0, sizeof(dl->mctx));
1887 dl->mctx.n201 = n201;
1888 dl->mctx.chan_nr = chan_nr;
1889 dl->mctx.link_id = link_id;
1891 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1892 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
1893 LOGP(DLAPDM, LOGL_ERROR, "resume without message error\n");
1895 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1897 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1899 LOGP(DLAPDM, LOGL_INFO, "perform re-establishment (SAMB)\n");
1901 /* Replace message in the send-buffer (reconnect) */
1902 if (dl->send_buffer)
1903 msgb_free(dl->send_buffer);
1905 dl->send_buffer = msg;
1907 /* Discard partly received L3 message */
1908 if (dl->rcv_buffer) {
1909 msgb_free(dl->rcv_buffer);
1910 dl->rcv_buffer = NULL;
1913 /* Create new msgb (old one is now free) */
1914 msg = msgb_alloc_headroom(23+10, 10, "LAPDm SABM");
1915 msg->l2h = msgb_put(msg, 3);
1916 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1917 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_SABM, 1);
1918 msg->l2h[2] = LAPDm_LEN(0);
1919 /* Transmit-buffer carries exactly one segment */
1920 memcpy(dl->tx_hist[0], msg->l2h, 3);
1921 dl->tx_length[0] = 3;
1922 /* set Vs to 0, because it is used as index when resending SAMB */
1926 dl->own_busy = dl->peer_busy = 0;
1927 dl->retrans_ctr = 0;
1928 lapdm_dl_newstate(dl, LAPDm_STATE_SABM_SENT);
1930 /* Tramsmit and start T200 */
1931 bsc_schedule_timer(&dl->t200, T200);
1932 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, n201);
1935 /* L3 requests release of data link */
1936 static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1938 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1939 uint8_t chan_nr = rllh->chan_nr;
1940 uint8_t link_id = rllh->link_id;
1941 uint8_t sapi = rllh->link_id & 7;
1944 /* get release mode */
1945 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1946 mode = rllh->data[1] & 1;
1950 LOGP(DLAPDM, LOGL_INFO, "perform local release\n");
1952 /* reset Timer T200 */
1953 bsc_del_timer(&dl->t200);
1954 /* enter idle state */
1955 lapdm_dl_newstate(dl, LAPDm_STATE_IDLE);
1956 /* send notification to L3 */
1957 return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
1960 /* in case we are already disconnecting */
1961 if (dl->state == LAPDm_STATE_DISC_SENT)
1964 LOGP(DLAPDM, LOGL_INFO, "perform normal release (DISC)\n");
1967 msgb_pull(msg, msg->tail - msg->l2h);
1969 /* Push LAPDm header on msgb */
1970 msg->l2h = msgb_push(msg, 3);
1971 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, CR_MS2BS_CMD);
1972 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DISC, 1);
1973 msg->l2h[2] = LAPDm_LEN(0);
1974 /* Transmit-buffer carries exactly one segment */
1975 memcpy(dl->tx_hist[0], msg->l2h, 3);
1976 dl->tx_length[0] = 3;
1979 dl->own_busy = dl->peer_busy = 0;
1980 dl->retrans_ctr = 0;
1981 lapdm_dl_newstate(dl, LAPDm_STATE_DISC_SENT);
1983 /* Tramsmit and start T200 */
1984 bsc_schedule_timer(&dl->t200, T200);
1985 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, dl->mctx.n201);
1988 /* L3 requests release in idle state */
1989 static int rslms_rx_rll_rel_req_idle(struct msgb *msg, struct lapdm_datalink *dl)
1993 /* send notification to L3 */
1994 return send_rll_simple(RSL_MT_REL_CONF, &dl->mctx);
1997 /* L3 requests channel in idle state */
1998 static int rslms_rx_chan_rqd(struct osmocom_ms *ms, struct msgb *msg)
2000 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
2003 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
2004 LOGP(DRSL, LOGL_ERROR, "Message too short for CHAN RQD!\n");
2007 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
2008 LOGP(DRSL, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
2011 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
2012 LOGP(DRSL, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
2015 if (cch->data[6] != RSL_IE_MS_POWER) {
2016 LOGP(DRSL, LOGL_ERROR, "Missing MS POWER IE\n");
2020 /* TA = 0 - delay */
2021 rc = l1ctl_tx_param_req(ms, 0 - cch->data[5], cch->data[7]);
2023 rc = l1ctl_tx_rach_req(ms, cch->data[1], cch->data[2], cch->data[3]);
2030 /* L1 confirms channel request */
2031 int l2_ph_chan_conf(struct msgb *msg, struct osmocom_ms *ms,
2032 struct l1ctl_info_dl *dl)
2034 struct abis_rsl_cchan_hdr *ch;
2036 struct gsm48_req_ref *ref;
2038 gsm_fn2gsmtime(&tm, htonl(dl->frame_nr));
2041 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
2042 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
2043 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
2044 ch->chan_nr = RSL_CHAN_RACH;
2045 ch->data[0] = RSL_IE_REQ_REFERENCE;
2046 ref = (struct gsm48_req_ref *) (ch->data + 1);
2049 ref->t3_low = tm.t3 & 0x3;
2050 ref->t3_high = tm.t3 >> 3;
2052 return rslms_sendmsg(msg, ms);
2055 /* Names for Radio Link Layer Management */
2056 static const struct value_string rsl_msg_names[] = {
2057 { RSL_MT_DATA_REQ, "RSL_MT_DATA_REQ" },
2058 { RSL_MT_DATA_IND, "RSL_MT_DATA_IND" },
2059 { RSL_MT_ERROR_IND, "RSL_MT_ERROR_IND" },
2060 { RSL_MT_EST_REQ, "RSL_MT_EST_REQ" },
2061 { RSL_MT_EST_CONF, "RSL_MT_EST_CONF" },
2062 { RSL_MT_EST_IND, "RSL_MT_EST_IND" },
2063 { RSL_MT_EST_IND, "RSL_MT_REL_REQ" },
2064 { RSL_MT_REL_REQ, "RSL_MT_REL_REQ" },
2065 { RSL_MT_REL_CONF, "RSL_MT_REL_CONF" },
2066 { RSL_MT_REL_IND, "RSL_MT_REL_IND" },
2067 { RSL_MT_UNIT_DATA_REQ, "RSL_MT_UNIT_DATA_REQ" },
2068 { RSL_MT_UNIT_DATA_IND, "RSL_MT_UNIT_DATA_IND" },
2069 { RSL_MT_SUSP_REQ, "RSL_MT_SUSP_REQ" },
2070 { RSL_MT_SUSP_CONF, "RSL_MT_SUSP_CONF" },
2071 { RSL_MT_RES_REQ, "RSL_MT_RES_REQ" },
2072 { RSL_MT_RECON_REQ, "RSL_MT_RECON_REQ" },
2073 { RSL_MT_CHAN_RQD, "RSL_MT_CHAN_RQD" },
2074 { RSL_MT_CHAN_CONF, "RSL_MT_CHAN_CONF" },
2078 const char *get_rsl_name(int value)
2080 return get_value_string(rsl_msg_names, value);
2083 const char *lapdm_state_names[] = {
2086 "LAPDm_STATE_SABM_SENT",
2087 "LAPDm_STATE_MF_EST",
2088 "LAPDm_STATE_TIMER_RECOV",
2089 "LAPDm_STATE_DISC_SENT",
2092 /* statefull handling for RSLms RLL messages from L3 */
2093 static struct l2downstate {
2096 int (*rout) (struct msgb *msg, struct lapdm_datalink *dl);
2097 } l2downstatelist[] = {
2098 /* create and send UI command */
2100 RSL_MT_UNIT_DATA_REQ, rslms_rx_rll_udata_req},
2102 /* create and send SABM command */
2103 {SBIT(LAPDm_STATE_IDLE),
2104 RSL_MT_EST_REQ, rslms_rx_rll_est_req},
2106 /* create and send I command */
2107 {SBIT(LAPDm_STATE_MF_EST) |
2108 SBIT(LAPDm_STATE_TIMER_RECOV),
2109 RSL_MT_DATA_REQ, rslms_rx_rll_data_req},
2111 /* suspend datalink */
2112 {SBIT(LAPDm_STATE_MF_EST) |
2113 SBIT(LAPDm_STATE_TIMER_RECOV),
2114 RSL_MT_SUSP_REQ, rslms_rx_rll_susp_req},
2116 /* create and send SABM command (resume) */
2117 {SBIT(LAPDm_STATE_MF_EST) |
2118 SBIT(LAPDm_STATE_TIMER_RECOV),
2119 RSL_MT_RES_REQ, rslms_rx_rll_res_req},
2121 /* create and send SABM command (reconnect) */
2122 {SBIT(LAPDm_STATE_MF_EST) |
2123 SBIT(LAPDm_STATE_TIMER_RECOV),
2124 RSL_MT_RECON_REQ, rslms_rx_rll_res_req},
2126 /* create and send DISC command */
2127 {SBIT(LAPDm_STATE_SABM_SENT) |
2128 SBIT(LAPDm_STATE_MF_EST) |
2129 SBIT(LAPDm_STATE_TIMER_RECOV) |
2130 SBIT(LAPDm_STATE_DISC_SENT),
2131 RSL_MT_REL_REQ, rslms_rx_rll_rel_req},
2133 /* release in idle state */
2134 {SBIT(LAPDm_STATE_IDLE),
2135 RSL_MT_REL_REQ, rslms_rx_rll_rel_req_idle},
2138 #define L2DOWNSLLEN \
2139 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2141 /* incoming RSLms RLL message from L3 */
2142 static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
2144 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
2145 int msg_type = rllh->c.msg_type;
2146 uint8_t sapi = rllh->link_id & 7;
2147 struct lapdm_entity *le;
2148 struct lapdm_datalink *dl;
2149 int i, supported = 0;
2152 if (msgb_l2len(msg) < sizeof(*rllh)) {
2153 LOGP(DRSL, LOGL_ERROR, "Message too short for RLL hdr!\n");
2157 if (rllh->link_id & 0x40)
2158 le = &ms->l2_entity.lapdm_acch;
2160 le = &ms->l2_entity.lapdm_dcch;
2162 /* G.2.1 No action schall be taken on frames containing an unallocated
2165 dl = datalink_for_sapi(le, sapi);
2167 LOGP(DRSL, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
2171 LOGP(DRSL, LOGL_INFO, "(ms %s) RLL Message '%s' received in state %s\n",
2172 ms->name, get_rsl_name(msg_type), lapdm_state_names[dl->state]);
2174 /* find function for current state and message */
2175 for (i = 0; i < L2DOWNSLLEN; i++) {
2176 if (msg_type == l2downstatelist[i].type)
2178 if ((msg_type == l2downstatelist[i].type)
2179 && ((1 << dl->state) & l2downstatelist[i].states))
2183 LOGP(DRSL, LOGL_NOTICE, "Message unsupported.\n");
2187 if (i == L2DOWNSLLEN) {
2188 LOGP(DRSL, LOGL_NOTICE, "Message unhandled at this state.\n");
2193 rc = l2downstatelist[i].rout(msg, dl);
2198 /* incoming RSLms COMMON CHANNEL message from L3 */
2199 static int rslms_rx_com_chan(struct msgb *msg, struct osmocom_ms *ms)
2201 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
2202 int msg_type = cch->c.msg_type;
2205 if (msgb_l2len(msg) < sizeof(*cch)) {
2206 LOGP(DRSL, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
2211 case RSL_MT_CHAN_RQD:
2212 /* create and send RACH request */
2213 rc = rslms_rx_chan_rqd(ms, msg);
2216 LOGP(DRSL, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
2225 /* input into layer2 (from layer 3) */
2226 int rslms_recvmsg(struct msgb *msg, struct osmocom_ms *ms)
2228 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
2231 if (msgb_l2len(msg) < sizeof(*rslh)) {
2232 LOGP(DRSL, LOGL_ERROR, "Message too short RSL hdr!\n");
2236 switch (rslh->msg_discr & 0xfe) {
2237 case ABIS_RSL_MDISC_RLL:
2238 rc = rslms_rx_rll(msg, ms);
2240 case ABIS_RSL_MDISC_COM_CHAN:
2241 rc = rslms_rx_com_chan(msg, ms);
2244 LOGP(DRSL, LOGL_ERROR, "unknown RSLms message "
2245 "discriminator 0x%02x", rslh->msg_discr);
2253 /* input function that L2 calls when sending messages up to L3 */
2254 int rslms_sendmsg(struct msgb *msg, struct osmocom_ms *ms)
2256 if (!ms->l2_entity.msg_handler) {
2261 /* call the layer2 message handler that is registered */
2262 return ms->l2_entity.msg_handler(msg, ms);
2265 /* register message handler for messages that are sent from L2->L3 */
2266 int osmol2_register_handler(struct osmocom_ms *ms, osmol2_cb_t cb)
2268 ms->l2_entity.msg_handler = cb;