diff options
author | root <root@nuedel.(none)> | 2011-09-26 11:23:06 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2011-10-10 08:38:58 +0200 |
commit | af48bed556079313074d8a2ea132fd689af8a100 (patch) | |
tree | c027cc6e8f93257ddcda20c4f794d83760e443d0 /src/gsm/lapdm.c | |
parent | 8a996b4844f8f89c16ce5062c74942d57f6f73b4 (diff) |
Split of LAPDm into a core part and a GSM specific part
Instead of mixing together the GSM layer 1 interface and RSL interface
with the implementation of LAPD, the core function of LAPD is now
extracted from LAPDm. The core implementation is now in lapd_core.c
and lapd_core.h respectively.
The lapd_core.c implements exactly one datalink instance for one SAP.
The surrounding implementation "lapdm.c" codes/decodes the layer 2
headers and handles multiplexing and datalink instances, as well as
translates primitives from/to RSL layer.
lapd_core.c can now be used for other LAPD implementations. (ISDN/ABIS)
Diffstat (limited to 'src/gsm/lapdm.c')
-rw-r--r-- | src/gsm/lapdm.c | 1947 |
1 files changed, 327 insertions, 1620 deletions
diff --git a/src/gsm/lapdm.c b/src/gsm/lapdm.c index a2212785..9e7172d6 100644 --- a/src/gsm/lapdm.c +++ b/src/gsm/lapdm.c @@ -1,7 +1,7 @@ /* GSM LAPDm (TS 04.06) implementation */ /* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org> - * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu> + * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu> * * All Rights Reserved * @@ -27,36 +27,6 @@ /*! \file lapdm.c */ -/*! - * Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue - * - * RX data is stored in the rcv_buffer (pointer). If the message is complete, it - * is removed from rcv_buffer pointer and forwarded to L3. If the RX data is - * received while there is an incomplete rcv_buffer, it is appended to it. - * - * TX data is stored in the send_queue first. When transmitting a frame, - * the first message in the send_queue is moved to the send_buffer. There it - * resides until all fragments are acknowledged. Fragments to be sent by I - * frames are stored in the tx_hist buffer for resend, if required. Also the - * current fragment is copied into the tx_queue. There it resides until it is - * forwarded to layer 1. - * - * In case we have SAPI 0, we only have a window size of 1, so the unack- - * nowledged message resides always in the send_buffer. In case of a suspend, - * it can be written back to the first position of the send_queue. - * - * The layer 1 normally sends a PH-READY-TO-SEND. But because we use - * asynchronous transfer between layer 1 and layer 2 (serial link), we must - * send a frame before layer 1 reaches the right timeslot to send it. So we - * move the tx_queue to layer 1 when there is not already a pending frame, and - * wait until acknowledge after the frame has been sent. If we receive an - * acknowledge, we can send the next frame from the buffer, if any. - * - * The moving of tx_queue to layer 1 may also trigger T200, if desired. Also it - * will trigger next I frame, if possible. - * - */ - #include <stdio.h> #include <stdint.h> #include <string.h> @@ -84,6 +54,7 @@ #define LAPDm_SAPI_SMS 3 #define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1) +#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3) #define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7) #define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1) #define LAPDm_ADDR_EA(addr) ((addr) & 0x1) @@ -105,19 +76,11 @@ #define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1) #define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5) -/* TS 04.06 Table 4 / Section 3.8.1 */ -#define LAPDm_U_SABM 0x7 -#define LAPDm_U_DM 0x3 -#define LAPDm_U_UI 0x0 -#define LAPDm_U_DISC 0x8 -#define LAPDm_U_UA 0xC - -#define LAPDm_S_RR 0x0 -#define LAPDm_S_RNR 0x1 -#define LAPDm_S_REJ 0x2 - #define LAPDm_LEN(len) ((len << 2) | 0x1) #define LAPDm_MORE 0x2 +#define LAPDm_EL 0x1 + +#define LAPDm_U_UI 0x0 /* TS 04.06 Section 5.8.3 */ #define N201_AB_SACCH 18 @@ -137,19 +100,8 @@ #define N200_TR_FACCH_FR 34 #define N200_TR_EFACCH_FR 48 #define N200_TR_FACCH_HR 29 -/* FIXME: this depends on chan type */ -#define N200 N200_TR_SACCH - -#define CR_MS2BS_CMD 0 -#define CR_MS2BS_RESP 1 -#define CR_BS2MS_CMD 1 -#define CR_BS2MS_RESP 0 - -/* Set T200 to 1 Second (OpenBTS uses 900ms) */ -#define T200 1, 0 - -/* k value for each SAPI */ -static uint8_t k_sapi[] = {1, 1, 1, 1, 1, 1, 1, 1}; +/* FIXME: set N200 depending on chan_nr */ +#define N200 N200_TR_SDCCH enum lapdm_format { LAPDm_FMT_A, @@ -159,36 +111,22 @@ enum lapdm_format { LAPDm_FMT_B4, }; -static void lapdm_t200_cb(void *data); -static int rslms_send_i(struct lapdm_msg_ctx *mctx, int line); - -/* UTILITY FUNCTIONS */ - -static inline uint8_t inc_mod8(uint8_t x) -{ - return (x + 1) & 7; -} - -static inline uint8_t add_mod8(uint8_t x, uint8_t y) -{ - return (x + y) & 7; -} - -static inline uint8_t sub_mod8(uint8_t x, uint8_t y) -{ - return (x - y) & 7; /* handle negative results correctly */ -} +static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg); +static int send_rslms_dlsap(struct osmo_dlsap_prim *dp, + struct lapd_msg_ctx *lctx); static void lapdm_dl_init(struct lapdm_datalink *dl, struct lapdm_entity *entity) { memset(dl, 0, sizeof(*dl)); - INIT_LLIST_HEAD(&dl->send_queue); - INIT_LLIST_HEAD(&dl->tx_queue); - dl->state = LAPDm_STATE_IDLE; - dl->t200.data = dl; - dl->t200.cb = &lapdm_t200_cb; dl->entity = entity; + lapd_dl_init(&dl->dl, 1, 8, 200); + dl->dl.reestablish = 0; /* GSM uses no reestablish */ + dl->dl.send_ph_data_req = lapdm_send_ph_data_req; + dl->dl.send_dlsap = send_rslms_dlsap; + dl->dl.n200_est_rel = N200_EST_REL; + dl->dl.n200 = N200; + dl->dl.t203_sec = 0; dl->dl.t203_usec = 0; } /*! \brief initialize a LAPDm entity and all datalinks inside @@ -215,35 +153,10 @@ void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode) void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode) { lapdm_entity_init(&lc->lapdm_acch, mode); + /* FIXME: this depends on chan type */ lapdm_entity_init(&lc->lapdm_dcch, mode); } -static void lapdm_dl_flush_send(struct lapdm_datalink *dl) -{ - struct msgb *msg; - - /* Flush send-queue */ - while ((msg = msgb_dequeue(&dl->send_queue))) - msgb_free(msg); - - /* Clear send-buffer */ - if (dl->send_buffer) { - msgb_free(dl->send_buffer); - dl->send_buffer = NULL; - } -} - -static void lapdm_dl_flush_tx(struct lapdm_datalink *dl) -{ - struct msgb *msg; - unsigned int i; - - while ((msg = msgb_dequeue(&dl->tx_queue))) - msgb_free(msg); - for (i = 0; i < 8; i++) - dl->tx_length[i] = 0; -} - /*! \brief flush and release all resoures in LAPDm entity */ void lapdm_entity_exit(struct lapdm_entity *le) { @@ -252,10 +165,7 @@ void lapdm_entity_exit(struct lapdm_entity *le) for (i = 0; i < ARRAY_SIZE(le->datalink); i++) { dl = &le->datalink[i]; - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - if (dl->rcv_buffer) - msgb_free(dl->rcv_buffer); + lapd_dl_exit(&dl->dl); } } @@ -270,14 +180,6 @@ void lapdm_channel_exit(struct lapdm_channel *lc) lapdm_entity_exit(&lc->lapdm_dcch); } -static void lapdm_dl_newstate(struct lapdm_datalink *dl, uint32_t state) -{ - LOGP(DLLAPDM, LOGL_INFO, "new state %s -> %s\n", - lapdm_state_names[dl->state], lapdm_state_names[state]); - - dl->state = state; -} - static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi) { switch (sapi) { @@ -305,7 +207,7 @@ static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201) uint8_t *data; if (pad_len < 0) { - LOGP(DLLAPDM, LOGL_ERROR, + LOGP(DLLAPD, LOGL_ERROR, "cannot pad message that is already too big!\n"); return; } @@ -328,17 +230,17 @@ static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le) /* write a frame into the tx queue */ static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg, - uint8_t chan_nr, uint8_t link_id, uint8_t n201) + uint8_t chan_nr, uint8_t link_id, uint8_t pad) { struct lapdm_entity *le = dl->entity; struct osmo_phsap_prim pp; /* if there is a pending message, queue it */ if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) { - *msgb_push(msg, 1) = n201; + *msgb_push(msg, 1) = pad; *msgb_push(msg, 1) = link_id; *msgb_push(msg, 1) = chan_nr; - msgb_enqueue(&dl->tx_queue, msg); + msgb_enqueue(&dl->dl.tx_queue, msg); return -EBUSY; } @@ -349,7 +251,7 @@ static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg, /* send the frame now */ le->tx_pending = 0; /* disabled flow control */ - lapdm_pad_msgb(msg, n201); + lapdm_pad_msgb(msg, pad); return le->l1_prim_cb(&pp.oph, le->l1_ctx); } @@ -366,7 +268,7 @@ static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le) /* next */ i = (i + 1) % n; dl = &le->datalink[i]; - if ((msg = msgb_dequeue(&dl->tx_queue))) + if ((msg = msgb_dequeue(&dl->dl.tx_queue))) break; } while (i != last); @@ -383,7 +285,7 @@ static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le) int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp) { struct msgb *msg; - uint8_t n201; + uint8_t pad; msg = tx_dequeue_msgb(le); if (!msg) @@ -398,11 +300,11 @@ int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp msgb_pull(msg, 1); pp->u.data.link_id = *msg->data; msgb_pull(msg, 1); - n201 = *msg->data; + pad = *msg->data; msgb_pull(msg, 1); /* Pad the frame, we can transmit now */ - lapdm_pad_msgb(msg, n201); + lapdm_pad_msgb(msg, pad); return 0; } @@ -486,1142 +388,161 @@ static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx) { struct msgb *msg; - LOGP(DLLAPDM, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause); + LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause); msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1); + msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr)); msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause); return rslms_sendmsg(msg, mctx->dl->entity); } -static int check_length_ind(struct lapdm_msg_ctx *mctx, uint8_t length_ind) -{ - if (!(length_ind & 0x01)) { - /* G.4.1 If the EL bit is set to "0", an MDL-ERROR-INDICATION - * primitive with cause "frame not implemented" is sent to the - * mobile management entity. */ - LOGP(DLLAPDM, LOGL_NOTICE, - "we don't support multi-octet length\n"); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - return 0; -} - -static int lapdm_send_resend(struct lapdm_datalink *dl) -{ - struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm resend"); - int length; - - /* Resend SABM/DISC from tx_hist */ - length = dl->tx_length[0]; - msg->l2h = msgb_put(msg, length); - memcpy(msg->l2h, dl->tx_hist[dl->V_send], length); - - return tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, dl->mctx.link_id, - dl->mctx.n201); -} - -static int lapdm_send_ua(struct lapdm_msg_ctx *mctx, uint8_t len, uint8_t *data) +/* DLSAP L2 -> L3 (RSLms) */ +static int send_rslms_dlsap(struct osmo_dlsap_prim *dp, + struct lapd_msg_ctx *lctx) { - uint8_t sapi = mctx->link_id & 7; - uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl); - struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm UA"); - struct lapdm_entity *le = mctx->dl->entity; - - msg->l2h = msgb_put(msg, 3 + len); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); - msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UA, f_bit); - msg->l2h[2] = LAPDm_LEN(len); - if (len) - memcpy(msg->l2h + 3, data, len); - - return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, - mctx->n201); -} - -static int lapdm_send_dm(struct lapdm_msg_ctx *mctx) -{ - uint8_t sapi = mctx->link_id & 7; - uint8_t f_bit = LAPDm_CTRL_PF_BIT(mctx->ctrl); - struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm DM"); - struct lapdm_entity *le = mctx->dl->entity; - - msg->l2h = msgb_put(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); - msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_DM, f_bit); - msg->l2h[2] = 0; - - return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, - mctx->n201); -} - -static int lapdm_send_rr(struct lapdm_msg_ctx *mctx, uint8_t f_bit) -{ - uint8_t sapi = mctx->link_id & 7; - struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RR"); - struct lapdm_entity *le = mctx->dl->entity; - - msg->l2h = msgb_put(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); - msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RR, f_bit); - msg->l2h[2] = LAPDm_LEN(0); - - return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, - mctx->n201); -} - -static int lapdm_send_rnr(struct lapdm_msg_ctx *mctx, uint8_t f_bit) -{ - uint8_t sapi = mctx->link_id & 7; - struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm RNR"); - struct lapdm_entity *le = mctx->dl->entity; - - msg->l2h = msgb_put(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); - msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_RNR, f_bit); - msg->l2h[2] = LAPDm_LEN(0); - - return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, - mctx->n201); -} - -static int lapdm_send_rej(struct lapdm_msg_ctx *mctx, uint8_t f_bit) -{ - uint8_t sapi = mctx->link_id & 7; - struct msgb *msg = msgb_alloc_headroom(23+10, 10, "LAPDm REJ"); - struct lapdm_entity *le = mctx->dl->entity; - - msg->l2h = msgb_put(msg, 3); - msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, le->cr.loc2rem.resp); - msg->l2h[1] = LAPDm_CTRL_S(mctx->dl->V_recv, LAPDm_S_REJ, f_bit); - msg->l2h[2] = LAPDm_LEN(0); - - return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, - mctx->n201); -} - -/* Timer callback on T200 expiry */ -static void lapdm_t200_cb(void *data) -{ - struct lapdm_datalink *dl = data; - - LOGP(DLLAPDM, LOGL_INFO, "lapdm_t200_cb(%p) state=%u\n", dl, dl->state); - - switch (dl->state) { - case LAPDm_STATE_SABM_SENT: - /* 5.4.1.3 */ - if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) { - /* send RELEASE INDICATION to L3 */ - send_rll_simple(RSL_MT_REL_IND, &dl->mctx); - /* send MDL ERROR INIDCATION to L3 */ - rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx); - /* flush tx buffers */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - /* go back to idle state */ - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - /* NOTE: we must not change any other states or buffers - * and queues, since we may reconnect after handover - * failure. the buffered messages is replaced there */ + struct lapd_datalink *dl = lctx->dl; + struct lapdm_datalink *mdl = + container_of(dl, struct lapdm_datalink, dl); + struct lapdm_msg_ctx *mctx = &mdl->mctx; + uint8_t rll_msg = 0; + + switch (dp->oph.primitive) { + case PRIM_DL_EST: + switch (dp->oph.operation) { + case PRIM_OP_INDICATION: + rll_msg = RSL_MT_EST_IND; break; - } - /* retransmit SABM command */ - lapdm_send_resend(dl); - /* increment re-transmission counter */ - dl->retrans_ctr++; - /* restart T200 (PH-READY-TO-SEND) */ - osmo_timer_schedule(&dl->t200, T200); - break; - case LAPDm_STATE_DISC_SENT: - /* 5.4.4.3 */ - if (dl->retrans_ctr + 1 >= N200_EST_REL + 1) { - /* send RELEASE INDICATION to L3 */ - send_rll_simple(RSL_MT_REL_CONF, &dl->mctx); - /* send MDL ERROR INIDCATION to L3 */ - rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx); - /* flush buffers */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - /* go back to idle state */ - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - /* NOTE: we must not change any other states or buffers - * and queues, since we may reconnect after handover - * failure. the buffered messages is replaced there */ + case PRIM_OP_CONFIRM: + rll_msg = RSL_MT_EST_CONF; break; - } - /* retransmit DISC command */ - lapdm_send_resend(dl); - /* increment re-transmission counter */ - dl->retrans_ctr++; - /* restart T200 (PH-READY-TO-SEND) */ - osmo_timer_schedule(&dl->t200, T200); - break; - case LAPDm_STATE_MF_EST: - /* 5.5.7 */ - dl->retrans_ctr = 0; - lapdm_dl_newstate(dl, LAPDm_STATE_TIMER_RECOV); - /* fall through */ - case LAPDm_STATE_TIMER_RECOV: - dl->retrans_ctr++; - if (dl->retrans_ctr < N200) { - /* retransmit I frame (V_s-1) with P=1, if any */ - if (dl->tx_length[sub_mod8(dl->V_send, 1)]) { - struct msgb *msg; - int length; - - LOGP(DLLAPDM, LOGL_INFO, "retransmit last frame " - "V(S)=%d\n", sub_mod8(dl->V_send, 1)); - /* Create I frame (segment) from tx_hist */ - length = dl->tx_length[sub_mod8(dl->V_send, 1)]; - msg = msgb_alloc_headroom(23+10, 10, "LAPDm I"); - msg->l2h = msgb_put(msg, length); - memcpy(msg->l2h, - dl->tx_hist[sub_mod8(dl->V_send, 1)], - length); - msg->l2h[1] = LAPDm_CTRL_I(dl->V_recv, - sub_mod8(dl->V_send, 1), 1); /* P=1 */ - tx_ph_data_enqueue(dl, msg, dl->mctx.chan_nr, - dl->mctx.link_id, dl->mctx.n201); - } else { - /* OR send appropriate supervision frame with P=1 */ - if (!dl->own_busy && !dl->seq_err_cond) { - lapdm_send_rr(&dl->mctx, 1); - /* NOTE: In case of sequence error - * condition, the REJ frame has been - * transmitted when entering the - * condition, so it has not be done - * here - */ - } else if (dl->own_busy) { - lapdm_send_rnr(&dl->mctx, 1); - } else { - LOGP(DLLAPDM, LOGL_INFO, "unhandled, " - "pls. fix\n"); - } - } - /* restart T200 (PH-READY-TO-SEND) */ - osmo_timer_schedule(&dl->t200, T200); - } else { - /* send MDL ERROR INIDCATION to L3 */ - rsl_rll_error(RLL_CAUSE_T200_EXPIRED, &dl->mctx); + default: + ; } break; - default: - LOGP(DLLAPDM, LOGL_INFO, "T200 expired in unexpected " - "dl->state %u\n", dl->state); - } -} - -/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */ -static void lapdm_acknowledge(struct lapdm_msg_ctx *mctx) -{ - struct lapdm_datalink *dl = mctx->dl; - uint8_t nr = LAPDm_CTRL_Nr(mctx->ctrl); - int s = 0, rej = 0, t200_reset = 0; - int i; - - /* supervisory frame ? */ - if (LAPDm_CTRL_is_S(mctx->ctrl)) - s = 1; - /* REJ frame ? */ - if (s && LAPDm_CTRL_S_BITS(mctx->ctrl) == LAPDm_S_REJ) - rej = 1; - - /* Flush all transmit buffers of acknowledged frames */ - for (i = dl->V_ack; i != nr; i = inc_mod8(i)) { - if (dl->tx_length[i]) { - dl->tx_length[i] = 0; - LOGP(DLLAPDM, LOGL_INFO, "ack frame %d\n", i); - } - } - - if (dl->state != LAPDm_STATE_TIMER_RECOV) { - /* When not in the timer recovery condition, the data - * link layer entity shall reset the timer T200 on - * receipt of a valid I frame with N(R) higher than V(A), - * or an REJ with an N(R) equal to V(A). */ - if ((!rej && nr != dl->V_ack) - || (rej && nr == dl->V_ack)) { - LOGP(DLLAPDM, LOGL_INFO, "reset t200\n"); - t200_reset = 1; - osmo_timer_del(&dl->t200); - /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */ - } - /* 5.7.4: N(R) sequence error - * N(R) is called valid, if and only if - * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8. - */ - if (sub_mod8(nr, dl->V_ack) > sub_mod8(dl->V_send, dl->V_ack)) { - LOGP(DLLAPDM, LOGL_NOTICE, "N(R) sequence error\n"); - rsl_rll_error(RLL_CAUSE_SEQ_ERR, mctx); - } - } - - /* V(A) shall be set to the value of N(R) */ - dl->V_ack = nr; - - /* If T200 has been reset by the receipt of an I, RR or RNR frame, - * and if there are outstanding I frames, restart T200 */ - if (t200_reset && !rej) { - if (dl->tx_length[dl->V_send - 1]) { - LOGP(DLLAPDM, LOGL_INFO, "start T200, due to unacked I " - "frame(s)\n"); - osmo_timer_schedule(&dl->t200, T200); - } - } -} - -/* L1 -> L2 */ - -/* Receive a LAPDm U (Unnumbered) message from L1 */ -static int lapdm_rx_u(struct msgb *msg, struct lapdm_msg_ctx *mctx) -{ - struct lapdm_datalink *dl = mctx->dl; - struct lapdm_entity *le = dl->entity; - uint8_t length; - int rc; - int rsl_msg; - - switch (LAPDm_CTRL_U_BITS(mctx->ctrl)) { - case LAPDm_U_SABM: - rsl_msg = RSL_MT_EST_IND; - - LOGP(DLLAPDM, LOGL_INFO, "SABM received\n"); - /* 5.7.1 */ - dl->seq_err_cond = 0; - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { - LOGP(DLLAPDM, LOGL_NOTICE, "SABM response error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - - length = msg->l2h[2] >> 2; - /* G.4.5 If SABM is received with L>N201 or with M bit - * set, AN MDL-ERROR-INDICATION is sent to MM. - */ - if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) { - LOGP(DLLAPDM, LOGL_NOTICE, "SABM too large error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx); - return -EIO; - } - - /* Must be Format B */ - rc = check_length_ind(mctx, msg->l2h[2]); - if (rc < 0) { - msgb_free(msg); - return rc; - } - switch (dl->state) { - case LAPDm_STATE_IDLE: - /* Set chan_nr and link_id for established connection */ - memset(&dl->mctx, 0, sizeof(dl->mctx)); - dl->mctx.dl = dl; - dl->mctx.chan_nr = mctx->chan_nr; - dl->mctx.link_id = mctx->link_id; - dl->mctx.n201 = mctx->n201; + case PRIM_DL_DATA: + switch (dp->oph.operation) { + case PRIM_OP_INDICATION: + rll_msg = RSL_MT_DATA_IND; break; - case LAPDm_STATE_MF_EST: - if (length == 0) { - rsl_msg = RSL_MT_EST_CONF; - break; - } - LOGP(DLLAPDM, LOGL_INFO, "SABM command, multiple " - "frame established state\n"); - /* check for contention resoultion */ - if (dl->tx_hist[0][2] >> 2) { - LOGP(DLLAPDM, LOGL_NOTICE, "SABM not allowed " - "during contention resolution\n"); - rsl_rll_error(RLL_CAUSE_SABM_INFO_NOTALL, mctx); - } - msgb_free(msg); - return 0; - case LAPDm_STATE_DISC_SENT: - /* 5.4.6.2 send DM with F=P */ - lapdm_send_dm(mctx); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - msgb_free(msg); - return send_rll_simple(RSL_MT_REL_CONF, mctx); default: - lapdm_send_ua(mctx, length, msg->l2h + 3); - msgb_free(msg); - return 0; - } - /* send UA response */ - lapdm_send_ua(mctx, length, msg->l2h + 3); - /* set Vs, Vr and Va to 0 */ - dl->V_send = dl->V_recv = dl->V_ack = 0; - /* clear tx_hist */ - dl->tx_length[0] = 0; - /* enter multiple-frame-established state */ - lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); - /* send notification to L3 */ - if (length == 0) { - /* 5.4.1.2 Normal establishment procedures */ - rc = send_rll_simple(rsl_msg, mctx); - msgb_free(msg); - } else { - /* 5.4.1.4 Contention resolution establishment */ - msg->l3h = msg->l2h + 3; - msgb_pull_l2h(msg); - rc = send_rslms_rll_l3(rsl_msg, mctx, msg); + ; } break; - case LAPDm_U_DM: - LOGP(DLLAPDM, LOGL_INFO, "DM received\n"); - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) { - LOGP(DLLAPDM, LOGL_NOTICE, "DM command error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - /* 5.4.1.2 DM responses with the F bit set to "0" - * shall be ignored. - */ - msgb_free(msg); - return 0; - } - switch (dl->state) { - case LAPDm_STATE_SABM_SENT: - break; - case LAPDm_STATE_MF_EST: - if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 1) { - LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM " - "response\n"); - rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP, mctx); - } else { - LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM " - "response, multiple frame established " - "state\n"); - rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx); - } - msgb_free(msg); - return 0; - case LAPDm_STATE_TIMER_RECOV: - /* DM is normal in case PF = 1 */ - if (LAPDm_CTRL_PF_BIT(mctx->ctrl) == 0) { - LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM " - "response, multiple frame established " - "state\n"); - rsl_rll_error(RLL_CAUSE_UNSOL_DM_RESP_MF, mctx); - msgb_free(msg); - return 0; - } - break; - case LAPDm_STATE_DISC_SENT: - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* go to idle state */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - rc = send_rll_simple(RSL_MT_REL_CONF, mctx); - msgb_free(msg); - return 0; - case LAPDm_STATE_IDLE: - /* 5.4.5 all other frame types shall be discarded */ + case PRIM_DL_UNIT_DATA: + switch (dp->oph.operation) { + case PRIM_OP_INDICATION: + return send_rslms_rll_l3_ui(mctx, dp->oph.msg); default: - LOGP(DLLAPDM, LOGL_INFO, "unsolicited DM response! " - "(discarding)\n"); - msgb_free(msg); - return 0; + ; } - /* reset T200 */ - osmo_timer_del(&dl->t200); - rc = send_rll_simple(RSL_MT_REL_IND, mctx); - msgb_free(msg); break; - case LAPDm_U_UI: - LOGP(DLLAPDM, LOGL_INFO, "UI received\n"); - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { - LOGP(DLLAPDM, LOGL_NOTICE, "UI indicates response " - "error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - - length = msg->l2h[2] >> 2; - /* FIXME: G.4.5 If UI is received with L>N201 or with M bit - * set, AN MDL-ERROR-INDICATION is sent to MM. - */ - - if (mctx->lapdm_fmt == LAPDm_FMT_B4) { - length = N201_B4; - msg->l3h = msg->l2h + 2; - } else { - rc = check_length_ind(mctx, msg->l2h[2]); - if (rc < 0) { - msgb_free(msg); - return rc; - } - length = msg->l2h[2] >> 2; - msg->l3h = msg->l2h + 3; - } - /* do some length checks */ - if (length == 0) { - /* 5.3.3 UI frames received with the length indicator - * set to "0" shall be ignored - */ - LOGP(DLLAPDM, LOGL_INFO, "length=0 (discarding)\n"); - msgb_free(msg); - return 0; - } - switch (LAPDm_ADDR_SAPI(mctx->addr)) { - case LAPDm_SAPI_NORMAL: - case LAPDm_SAPI_SMS: + case PRIM_DL_REL: + switch (dp->oph.operation) { + case PRIM_OP_INDICATION: + rll_msg = RSL_MT_REL_IND; + break; + case PRIM_OP_CONFIRM: + rll_msg = RSL_MT_REL_CONF; break; default: - /* 5.3.3 UI frames with invalid SAPI values shall be - * discarded - */ - LOGP(DLLAPDM, LOGL_INFO, "sapi=%u (discarding)\n", - LAPDm_ADDR_SAPI(mctx->addr)); - msgb_free(msg); - return 0; + ; } - msgb_pull_l2h(msg); - rc = send_rslms_rll_l3_ui(mctx, msg); break; - case LAPDm_U_DISC: - rsl_msg = RSL_MT_REL_IND; - - LOGP(DLLAPDM, LOGL_INFO, "DISC received\n"); - /* flush buffers */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - /* 5.7.1 */ - dl->seq_err_cond = 0; - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp) { - LOGP(DLLAPDM, LOGL_NOTICE, "DISC response error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - length = msg->l2h[2] >> 2; - if (length > 0 || msg->l2h[2] & 0x02) { - /* G.4.4 If a DISC or DM frame is received with L>0 or - * with the M bit set to "1", an MDL-ERROR-INDICATION - * primitive with cause "U frame with incorrect - * parameters" is sent to the mobile management entity. - */ - LOGP(DLLAPDM, LOGL_NOTICE, - "U frame iwth incorrect parameters "); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx); - return -EIO; - } - switch (dl->state) { - case LAPDm_STATE_IDLE: - LOGP(DLLAPDM, LOGL_INFO, "DISC in idle state\n"); - /* send DM with F=P */ - msgb_free(msg); - return lapdm_send_dm(mctx); - case LAPDm_STATE_SABM_SENT: - LOGP(DLLAPDM, LOGL_INFO, "DISC in SABM state\n"); - /* 5.4.6.2 send DM with F=P */ - lapdm_send_dm(mctx); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - msgb_free(msg); - return send_rll_simple(RSL_MT_REL_IND, mctx); - case LAPDm_STATE_MF_EST: - case LAPDm_STATE_TIMER_RECOV: - LOGP(DLLAPDM, LOGL_INFO, "DISC in est state\n"); - break; - case LAPDm_STATE_DISC_SENT: - LOGP(DLLAPDM, LOGL_INFO, "DISC in disc state\n"); - rsl_msg = RSL_MT_REL_CONF; + case PRIM_DL_SUSP: + switch (dp->oph.operation) { + case PRIM_OP_CONFIRM: + rll_msg = RSL_MT_SUSP_CONF; break; default: - lapdm_send_ua(mctx, length, msg->l2h + 3); - msgb_free(msg); - return 0; + ; } - /* send UA response */ - lapdm_send_ua(mctx, length, msg->l2h + 3); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* enter idle state */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - /* send notification to L3 */ - rc = send_rll_simple(rsl_msg, mctx); - msgb_free(msg); break; - case LAPDm_U_UA: - LOGP(DLLAPDM, LOGL_INFO, "UA received\n"); - /* G.2.2 Wrong value of the C/R bit */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd) { - LOGP(DLLAPDM, LOGL_NOTICE, "UA indicates command " - "error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; - } - - length = msg->l2h[2] >> 2; - /* G.4.5 If UA is received with L>N201 or with M bit - * set, AN MDL-ERROR-INDICATION is sent to MM. - */ - if ((msg->l2h[2] & LAPDm_MORE) || length + 3 > mctx->n201) { - LOGP(DLLAPDM, LOGL_NOTICE, "UA too large error\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_UFRM_INC_PARAM, mctx); - return -EIO; - } - - if (!LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - /* 5.4.1.2 A UA response with the F bit set to "0" - * shall be ignored. - */ - LOGP(DLLAPDM, LOGL_INFO, "F=0 (discarding)\n"); - msgb_free(msg); - return 0; - } - switch (dl->state) { - case LAPDm_STATE_SABM_SENT: - break; - case LAPDm_STATE_MF_EST: - case LAPDm_STATE_TIMER_RECOV: - LOGP(DLLAPDM, LOGL_INFO, "unsolicited UA response! " - "(discarding)\n"); - rsl_rll_error(RLL_CAUSE_UNSOL_UA_RESP, mctx); - msgb_free(msg); - return 0; - case LAPDm_STATE_DISC_SENT: - LOGP(DLLAPDM, LOGL_INFO, "UA in disconnect state\n"); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* go to idle state */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - rc = send_rll_simple(RSL_MT_REL_CONF, mctx); - msgb_free(msg); + case PRIM_MDL_ERROR: + switch (dp->oph.operation) { + case PRIM_OP_INDICATION: + rsl_rll_error(dp->u.error_ind.cause, mctx); + if (dp->oph.msg) + msgb_free(dp->oph.msg); return 0; - case LAPDm_STATE_IDLE: - /* 5.4.5 all other frame types shall be discarded */ default: - LOGP(DLLAPDM, LOGL_INFO, "unsolicited UA response! " - "(discarding)\n"); - msgb_free(msg); - return 0; - } - LOGP(DLLAPDM, LOGL_INFO, "UA in SABM state\n"); - /* reset Timer T200 */ - osmo_timer_del(&dl->t200); - /* compare UA with SABME if contention resolution is applied */ - if (dl->tx_hist[0][2] >> 2) { - rc = check_length_ind(mctx, msg->l2h[2]); - if (rc < 0) { - rc = send_rll_simple(RSL_MT_REL_IND, mctx); - msgb_free(msg); - /* go to idle state */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - return 0; - } - length = msg->l2h[2] >> 2; - if (length != (dl->tx_hist[0][2] >> 2) - || !!memcmp(dl->tx_hist[0] + 3, msg->l2h + 3, - length)) { - LOGP(DLLAPDM, LOGL_INFO, "**** UA response " - "mismatches ****\n"); - rc = send_rll_simple(RSL_MT_REL_IND, mctx); - msgb_free(msg); - /* go to idle state */ - lapdm_dl_flush_tx(dl); - lapdm_dl_flush_send(dl); - lapdm_dl_newstate(dl, LAPDm_STATE_IDLE); - return 0; - } + ; } - /* set Vs, Vr and Va to 0 */ - dl->V_send = dl->V_recv = dl->V_ack = 0; - /* clear tx_hist */ - dl->tx_length[0] = 0; - /* enter multiple-frame-established state */ - lapdm_dl_newstate(dl, LAPDm_STATE_MF_EST); - /* send outstanding frames, if any (resume / reconnect) */ - rslms_send_i(mctx, __LINE__); - /* send notification to L3 */ - rc = send_rll_simple(RSL_MT_EST_CONF, mctx); - msgb_free(msg); break; default: - /* G.3.1 */ - LOGP(DLLAPDM, LOGL_NOTICE, "Unnumbered frame not allowed.\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, mctx); - return -EINVAL; + ; } - return rc; -} -/* Receive a LAPDm S (Supervisory) message from L1 */ -static int lapdm_rx_s(struct msgb *msg, struct lapdm_msg_ctx *mctx) -{ - struct lapdm_datalink *dl = mctx->dl; - struct lapdm_entity *le = dl->entity; - uint8_t length; - - length = msg->l2h[2] >> 2; - if (length > 0 || msg->l2h[2] & 0x02) { - /* G.4.3 If a supervisory frame is received with L>0 or - * with the M bit set to "1", an MDL-ERROR-INDICATION - * primitive with cause "S frame with incorrect - * parameters" is sent to the mobile management entity. */ - LOGP(DLLAPDM, LOGL_NOTICE, - "S frame with incorrect parameters\n"); - msgb_free(msg); - rsl_rll_error(RLL_CAUSE_SFRM_INC_PARAM, mctx); - return -EIO; + if (!rll_msg) { + LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please " + "fix!\n", dp->oph.primitive, dp->oph.operation); + return -EINVAL; } - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.resp - && LAPDm_CTRL_PF_BIT(mctx->ctrl) - && dl->state != LAPDm_STATE_TIMER_RECOV) { - /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */ - LOGP(DLLAPDM, LOGL_NOTICE, "S frame response with F=1 error\n"); - rsl_rll_error(RLL_CAUSE_UNSOL_SPRV_RESP, mctx); - } + if (!dp->oph.msg) + return send_rll_simple(rll_msg, mctx); - switch (dl->state) { - case LAPDm_STATE_IDLE: - /* if P=1, respond DM with F=1 (5.2.2) */ - /* 5.4.5 all other frame types shall be discarded */ - if (LAPDm_CTRL_PF_BIT(mctx->ctrl)) - lapdm_send_dm(mctx); /* F=P */ - /* fall though */ - case LAPDm_STATE_SABM_SENT: - case LAPDm_STATE_DISC_SENT: - LOGP(DLLAPDM, LOGL_NOTICE, "S frame ignored in this state\n"); - msgb_free(msg); - return 0; - } - switch (LAPDm_CTRL_S_BITS(mctx->ctrl)) { - case LAPDm_S_RR: - LOGP(DLLAPDM, LOGL_INFO, "RR received\n"); - /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */ - lapdm_acknowledge(mctx); - - /* 5.5.3.2 */ - if (LAPDm_ADDR_CR(mctx->addr) == le->cr.rem2loc.cmd - && LAPDm_CTRL_PF_BIT(mctx->ctrl)) { - if (!dl->own_busy && !dl->seq_ |