blob: d44335fe7d6b32e410801dcc0096dc276b1d5ac8 [file] [log] [blame]
Harald Welte1f0b8c22011-06-27 10:51:37 +02001/* GSM LAPDm (TS 04.06) implementation */
2
3/* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
rootaf48bed2011-09-26 11:23:06 +02004 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
Harald Welte1f0b8c22011-06-27 10:51:37 +02005 *
6 * All Rights Reserved
7 *
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.
12 *
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.
17 *
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.
21 *
22 */
23
Harald Welte6bdf0b12011-08-17 18:22:08 +020024/*! \addtogroup lapdm
25 * @{
26 */
27
28/*! \file lapdm.c */
29
Harald Welte1f0b8c22011-06-27 10:51:37 +020030#include <stdio.h>
31#include <stdint.h>
32#include <string.h>
33#include <errno.h>
34#include <arpa/inet.h>
35
36#include <osmocom/core/logging.h>
37#include <osmocom/core/timer.h>
38#include <osmocom/core/msgb.h>
39#include <osmocom/core/utils.h>
40
41#include <osmocom/gsm/tlv.h>
42#include <osmocom/gsm/rsl.h>
43#include <osmocom/gsm/prim.h>
44#include <osmocom/gsm/gsm_utils.h>
45#include <osmocom/gsm/lapdm.h>
46
47#include <osmocom/gsm/protocol/gsm_04_08.h>
48#include <osmocom/gsm/protocol/gsm_08_58.h>
49
50/* TS 04.06 Figure 4 / Section 3.2 */
51#define LAPDm_LPD_NORMAL 0
52#define LAPDm_LPD_SMSCB 1
53#define LAPDm_SAPI_NORMAL 0
54#define LAPDm_SAPI_SMS 3
55#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
56
rootaf48bed2011-09-26 11:23:06 +020057#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020058#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
59#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
60#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
61
62/* TS 04.06 Table 3 / Section 3.4.3 */
63#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
64#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
65#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
66
67#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
68#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
69#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
70
71#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
72#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
73
74#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
75
76#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
77#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
78
Harald Welte1f0b8c22011-06-27 10:51:37 +020079#define LAPDm_LEN(len) ((len << 2) | 0x1)
80#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020081#define LAPDm_EL 0x1
82
83#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020084
85/* TS 04.06 Section 5.8.3 */
86#define N201_AB_SACCH 18
87#define N201_AB_SDCCH 20
88#define N201_AB_FACCH 20
89#define N201_Bbis 23
90#define N201_Bter_SACCH 21
91#define N201_Bter_SDCCH 23
92#define N201_Bter_FACCH 23
93#define N201_B4 19
94
95/* 5.8.2.1 N200 during establish and release */
96#define N200_EST_REL 5
97/* 5.8.2.1 N200 during timer recovery state */
98#define N200_TR_SACCH 5
99#define N200_TR_SDCCH 23
100#define N200_TR_FACCH_FR 34
101#define N200_TR_EFACCH_FR 48
102#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200103/* FIXME: set N200 depending on chan_nr */
104#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200105
106enum lapdm_format {
107 LAPDm_FMT_A,
108 LAPDm_FMT_B,
109 LAPDm_FMT_Bbis,
110 LAPDm_FMT_Bter,
111 LAPDm_FMT_B4,
112};
113
rootaf48bed2011-09-26 11:23:06 +0200114static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
115static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
116 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100117static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200118
119static void lapdm_dl_init(struct lapdm_datalink *dl,
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100120 struct lapdm_entity *entity, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200121{
122 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200123 dl->entity = entity;
rootaf48bed2011-09-26 11:23:06 +0200124 lapd_dl_init(&dl->dl, 1, 8, 200);
125 dl->dl.reestablish = 0; /* GSM uses no reestablish */
126 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
127 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100128 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200129 dl->dl.n200_est_rel = N200_EST_REL;
130 dl->dl.n200 = N200;
131 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100132 dl->dl.t200_sec = t200; dl->dl.t200_usec = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200133}
134
Harald Welte6bdf0b12011-08-17 18:22:08 +0200135/*! \brief initialize a LAPDm entity and all datalinks inside
136 * \param[in] le LAPDm entity
137 * \param[in] mode \ref lapdm_mode (BTS/MS)
138 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100139void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200140{
141 unsigned int i;
142
143 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100144 lapdm_dl_init(&le->datalink[i], le, t200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200145
146 lapdm_entity_set_mode(le, mode);
147}
148
Harald Welte6bdf0b12011-08-17 18:22:08 +0200149/*! \brief initialize a LAPDm channel and all its channels
150 * \param[in] lc \ref lapdm_channel to be initialized
151 * \param[in] mode \ref lapdm_mode (BTS/MS)
152 *
153 * This really is a convenience wrapper around calling \ref
154 * lapdm_entity_init twice.
155 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200156void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
157{
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100158 lapdm_entity_init(&lc->lapdm_acch, mode, 2);
rootaf48bed2011-09-26 11:23:06 +0200159 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100160 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200161}
162
Harald Welte6bdf0b12011-08-17 18:22:08 +0200163/*! \brief flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200164void lapdm_entity_exit(struct lapdm_entity *le)
165{
166 unsigned int i;
167 struct lapdm_datalink *dl;
168
169 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
170 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200171 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200172 }
173}
174
Harald Welte6bdf0b12011-08-17 18:22:08 +0200175/* \brief lfush and release all resources in LAPDm channel
176 *
177 * A convenience wrapper calling \ref lapdm_entity_exit on both
178 * entities inside the \ref lapdm_channel
179 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200180void lapdm_channel_exit(struct lapdm_channel *lc)
181{
182 lapdm_entity_exit(&lc->lapdm_acch);
183 lapdm_entity_exit(&lc->lapdm_dcch);
184}
185
Daniel Willmann55405fb2014-03-26 13:45:17 +0100186struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200187{
188 switch (sapi) {
189 case LAPDm_SAPI_NORMAL:
190 return &le->datalink[0];
191 case LAPDm_SAPI_SMS:
192 return &le->datalink[1];
193 default:
194 return NULL;
195 }
196}
197
Harald Welte1f0b8c22011-06-27 10:51:37 +0200198/* Append padding (if required) */
199static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
200{
201 int pad_len = n201 - msgb_l2len(msg);
202 uint8_t *data;
203
204 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200205 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200206 "cannot pad message that is already too big!\n");
207 return;
208 }
209
210 data = msgb_put(msg, pad_len);
211 memset(data, 0x2B, pad_len);
212}
213
214/* input function that L2 calls when sending messages up to L3 */
215static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
216{
217 if (!le->l3_cb) {
218 msgb_free(msg);
219 return -EIO;
220 }
221
222 /* call the layer2 message handler that is registered */
223 return le->l3_cb(msg, le, le->l3_ctx);
224}
225
226/* write a frame into the tx queue */
227static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200228 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200229{
230 struct lapdm_entity *le = dl->entity;
231 struct osmo_phsap_prim pp;
232
233 /* if there is a pending message, queue it */
234 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200235 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200236 *msgb_push(msg, 1) = link_id;
237 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200238 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200239 return -EBUSY;
240 }
241
242 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
243 PRIM_OP_REQUEST, msg);
244 pp.u.data.chan_nr = chan_nr;
245 pp.u.data.link_id = link_id;
246
247 /* send the frame now */
248 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200249 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200250
251 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
252}
253
254static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
255{
256 struct lapdm_datalink *dl;
257 int last = le->last_tx_dequeue;
258 int i = last, n = ARRAY_SIZE(le->datalink);
259 struct msgb *msg = NULL;
260
261 /* round-robin dequeue */
262 do {
263 /* next */
264 i = (i + 1) % n;
265 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200266 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200267 break;
268 } while (i != last);
269
270 if (msg) {
271 /* Set last dequeue position */
272 le->last_tx_dequeue = i;
273 }
274
275 return msg;
276}
277
Harald Welte6bdf0b12011-08-17 18:22:08 +0200278/*! \brief dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200279 * a osmo_phsap_prim */
280int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
281{
282 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200283 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200284
285 msg = tx_dequeue_msgb(le);
286 if (!msg)
287 return -ENODEV;
288
289 /* if we have a message, send PH-DATA.req */
290 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
291 PRIM_OP_REQUEST, msg);
292
293 /* Pull chan_nr and link_id */
294 pp->u.data.chan_nr = *msg->data;
295 msgb_pull(msg, 1);
296 pp->u.data.link_id = *msg->data;
297 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200298 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200299 msgb_pull(msg, 1);
300
301 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200302 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200303
304 return 0;
305}
306
307/* get next frame from the tx queue. because the ms has multiple datalinks,
308 * each datalink's queue is read round-robin.
309 */
310static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
311{
312 struct osmo_phsap_prim pp;
313
314 /* we may send again */
315 le->tx_pending = 0;
316
317 /* free confirm message */
318 if (msg)
319 msgb_free(msg);
320
321 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
322 /* no message in all queues */
323
324 /* If user didn't request PH-EMPTY_FRAME.req, abort */
325 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
326 return 0;
327
328 /* otherwise, send PH-EMPTY_FRAME.req */
329 osmo_prim_init(&pp.oph, SAP_GSM_PH,
330 PRIM_PH_EMPTY_FRAME,
331 PRIM_OP_REQUEST, NULL);
332 } else {
333 le->tx_pending = 1;
334 }
335
336 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
337}
338
339/* Create RSLms various RSLms messages */
340static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
341 struct msgb *msg)
342{
343 /* Add the RSL + RLL header */
344 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
345
346 /* send off the RSLms message to L3 */
347 return rslms_sendmsg(msg, mctx->dl->entity);
348}
349
350/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
351static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
352{
353 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
354 struct abis_rsl_rll_hdr *rllh;
355
356 /* Add the RSL + RLL header */
357 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
358 msgb_push(msg, 2 + 2);
359 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
360 mctx->link_id, 1);
361 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
362
363 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
364 rllh->data[1] = mctx->ta_ind;
365
366 rllh->data[2] = RSL_IE_MS_POWER;
367 rllh->data[3] = mctx->tx_power_ind;
368
369 return rslms_sendmsg(msg, mctx->dl->entity);
370}
371
372static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
373{
374 struct msgb *msg;
375
376 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
377
378 /* send off the RSLms message to L3 */
379 return rslms_sendmsg(msg, mctx->dl->entity);
380}
381
382static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
383{
384 struct msgb *msg;
385
rootaf48bed2011-09-26 11:23:06 +0200386 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200387 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200388 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
389 return rslms_sendmsg(msg, mctx->dl->entity);
390}
391
rootaf48bed2011-09-26 11:23:06 +0200392/* DLSAP L2 -> L3 (RSLms) */
393static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
394 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200395{
rootaf48bed2011-09-26 11:23:06 +0200396 struct lapd_datalink *dl = lctx->dl;
397 struct lapdm_datalink *mdl =
398 container_of(dl, struct lapdm_datalink, dl);
399 struct lapdm_msg_ctx *mctx = &mdl->mctx;
400 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200401
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200402 switch (OSMO_PRIM_HDR(&dp->oph)) {
403 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
Andreas Eversberg5977db02013-06-12 09:34:51 +0200404 if (dp->oph.msg && dp->oph.msg->len == 0) {
405 /* omit L3 info by freeing message */
406 msgb_free(dp->oph.msg);
407 dp->oph.msg = NULL;
408 }
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200409 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200410 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200411 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
412 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200413 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200414 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
415 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200416 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200417 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
418 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
419 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
420 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200421 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200422 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
423 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200424 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200425 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
426 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200427 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200428 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
429 rsl_rll_error(dp->u.error_ind.cause, mctx);
430 if (dp->oph.msg)
431 msgb_free(dp->oph.msg);
432 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200433 }
rootaf48bed2011-09-26 11:23:06 +0200434
435 if (!rll_msg) {
436 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
437 "fix!\n", dp->oph.primitive, dp->oph.operation);
438 return -EINVAL;
439 }
440
441 if (!dp->oph.msg)
442 return send_rll_simple(rll_msg, mctx);
443
444 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200445}
446
rootaf48bed2011-09-26 11:23:06 +0200447/* send a data frame to layer 1 */
448static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200449{
rootaf48bed2011-09-26 11:23:06 +0200450 uint8_t l3_len = msg->tail - msg->data;
451 struct lapd_datalink *dl = lctx->dl;
452 struct lapdm_datalink *mdl =
453 container_of(dl, struct lapdm_datalink, dl);
454 struct lapdm_msg_ctx *mctx = &mdl->mctx;
455 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200456
rootaf48bed2011-09-26 11:23:06 +0200457 /* prepend l2 header */
458 msg->l2h = msgb_push(msg, 3);
459 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
460 /* EA is set here too */
461 switch (format) {
462 case LAPD_FORM_I:
463 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
464 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200465 break;
rootaf48bed2011-09-26 11:23:06 +0200466 case LAPD_FORM_S:
467 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200468 break;
rootaf48bed2011-09-26 11:23:06 +0200469 case LAPD_FORM_U:
470 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200471 break;
472 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200473 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200474 return -EINVAL;
475 }
rootaf48bed2011-09-26 11:23:06 +0200476 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
477 if (lctx->more)
478 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200479
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100480 /* add ACCH header with last indicated tx-power and TA */
481 if ((mctx->link_id & 0x40)) {
482 struct lapdm_entity *le = mdl->entity;
483
484 msg->l2h = msgb_push(msg, 2);
485 msg->l2h[0] = le->tx_power;
486 msg->l2h[1] = le->ta;
487 }
488
rootaf48bed2011-09-26 11:23:06 +0200489 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
490 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200491}
492
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100493static int update_pending_frames(struct lapd_msg_ctx *lctx)
494{
495 struct lapd_datalink *dl = lctx->dl;
496 struct msgb *msg;
497 int rc = -1;
498
499 llist_for_each_entry(msg, &dl->tx_queue, list) {
500 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
501 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
502 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
503 rc = 0;
504 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
505 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
506 }
507 }
508
509 return rc;
510}
511
Harald Welte1f0b8c22011-06-27 10:51:37 +0200512/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200513static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
514 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200515{
516 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200517 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200518 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200519 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200520 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200521 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200522
523 /* when we reach here, we have a msgb with l2h pointing to the raw
524 * 23byte mac block. The l1h has already been purged. */
525
rootaf48bed2011-09-26 11:23:06 +0200526 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200527 mctx.chan_nr = chan_nr;
528 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200529
Harald Welte1f0b8c22011-06-27 10:51:37 +0200530 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
531 if (cbits == 0x10 || cbits == 0x12) {
532 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
533 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200534 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200535 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200536 } else {
537 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200538 /* It was received from network on SACCH */
539
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100540 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
541 if (le->mode == LAPDM_MODE_MS
542 && LAPDm_CTRL_is_U(msg->l2h[3])
543 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200544 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200545 n201 = N201_B4;
546 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200547 } else {
548 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200549 n201 = N201_AB_SACCH;
550 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200551 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200552 /* SACCH frames have a two-byte L1 header that
553 * OsmocomBB L1 doesn't strip */
554 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
555 mctx.ta_ind = msg->l2h[1];
556 msgb_pull(msg, 2);
557 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200558 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200559 } else {
560 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200561 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100562 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200563 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200564 }
565 }
566
Daniel Willmann55405fb2014-03-26 13:45:17 +0100567 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200568 /* G.2.1 No action on frames containing an unallocated SAPI. */
569 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200570 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200571 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200572 msgb_free(msg);
573 return -EIO;
574 }
575
Harald Welte1f0b8c22011-06-27 10:51:37 +0200576 switch (mctx.lapdm_fmt) {
577 case LAPDm_FMT_A:
578 case LAPDm_FMT_B:
579 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200580 lctx.dl = &mctx.dl->dl;
581 /* obtain SAPI from address field */
582 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
583 /* G.2.3 EA bit set to "0" is not allowed in GSM */
584 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
585 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
586 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200587 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200588 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200589 return -EINVAL;
590 }
rootaf48bed2011-09-26 11:23:06 +0200591 /* adress field */
592 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
593 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
594 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
595 /* command field */
596 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
597 lctx.format = LAPD_FORM_I;
598 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
599 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
600 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
601 lctx.format = LAPD_FORM_S;
602 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
603 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
604 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
605 lctx.format = LAPD_FORM_U;
606 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
607 } else
608 lctx.format = LAPD_FORM_UKN;
609 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
610 if (lctx.sapi != LAPDm_SAPI_NORMAL
611 && lctx.sapi != LAPDm_SAPI_SMS
612 && lctx.format == LAPD_FORM_U
613 && lctx.s_u == LAPDm_U_UI) {
614 /* 5.3.3 UI frames with invalid SAPI values shall be
615 * discarded
616 */
617 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
618 lctx.sapi);
619 msgb_free(msg);
620 return 0;
621 }
622 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
623 lctx.n201 = n201;
624 lctx.length = n201;
625 lctx.more = 0;
626 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100627 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200628 } else {
629 /* length field */
630 if (!(msg->l2h[2] & LAPDm_EL)) {
631 /* G.4.1 If the EL bit is set to "0", an
632 * MDL-ERROR-INDICATION primitive with cause
633 * "frame not implemented" is sent to the
634 * mobile management entity. */
635 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
636 "multi-octet length\n");
637 msgb_free(msg);
638 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
639 return -EINVAL;
640 }
641 lctx.n201 = n201;
642 lctx.length = msg->l2h[2] >> 2;
643 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
644 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100645 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200646 }
647 /* store context for messages from lapd */
648 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
649 /* send to LAPD */
650 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200651 break;
652 case LAPDm_FMT_Bter:
653 /* FIXME */
654 msgb_free(msg);
655 break;
656 case LAPDm_FMT_Bbis:
657 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200658 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200659 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100660 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200661 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
662 break;
663 default:
664 msgb_free(msg);
665 }
666
667 return rc;
668}
669
670/* input into layer2 (from layer 1) */
671static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
672{
673 struct abis_rsl_cchan_hdr *ch;
674 struct gsm48_req_ref req_ref;
675 struct gsm_time gt;
676 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
677
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200678 if (!msg)
679 return -ENOMEM;
680
Harald Welte1f0b8c22011-06-27 10:51:37 +0200681 msg->l2h = msgb_push(msg, sizeof(*ch));
682 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
683 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
684 ch->chan_nr = RSL_CHAN_RACH;
685
686 /* generate a RSL CHANNEL REQUIRED message */
687 gsm_fn2gsmtime(&gt, fn);
688 req_ref.ra = ra;
689 req_ref.t1 = gt.t1; /* FIXME: modulo? */
690 req_ref.t2 = gt.t2;
691 req_ref.t3_low = gt.t3 & 7;
692 req_ref.t3_high = gt.t3 >> 3;
693
694 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
695 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
696
697 return rslms_sendmsg(msg, le);
698}
699
700static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
701
Harald Welte6bdf0b12011-08-17 18:22:08 +0200702/*! \brief Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200703int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
704{
705 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
706 int rc = 0;
707
708 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200709 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200710 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100711 rc = -ENODEV;
712 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200713 }
714
715 switch (oph->primitive) {
716 case PRIM_PH_DATA:
717 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200718 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200719 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100720 rc = -ENODEV;
721 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200722 }
723 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
724 pp->u.data.link_id);
725 break;
726 case PRIM_PH_RTS:
727 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200728 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200729 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100730 rc = -ENODEV;
731 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200732 }
733 rc = l2_ph_data_conf(oph->msg, le);
734 break;
735 case PRIM_PH_RACH:
736 switch (oph->operation) {
737 case PRIM_OP_INDICATION:
738 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
739 pp->u.rach_ind.acc_delay);
740 break;
741 case PRIM_OP_CONFIRM:
742 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
743 break;
744 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100745 rc = -EIO;
746 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200747 }
748 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100749 default:
750 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
751 oph->primitive);
752 rc = -EINVAL;
753 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200754 }
755
756 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100757
758free:
759 msgb_free(oph->msg);
760 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200761}
762
763
764/* L3 -> L2 / RSLMS -> LAPDm */
765
rootaf48bed2011-09-26 11:23:06 +0200766/* Set LAPDm context for established connection */
767static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
768 uint8_t link_id, int n201, uint8_t sapi)
769{
770 memset(&dl->mctx, 0, sizeof(dl->mctx));
771 dl->mctx.dl = dl;
772 dl->mctx.chan_nr = chan_nr;
773 dl->mctx.link_id = link_id;
774 dl->dl.lctx.dl = &dl->dl;
775 dl->dl.lctx.n201 = n201;
776 dl->dl.lctx.sapi = sapi;
777
778 return 0;
779}
780
Harald Welte1f0b8c22011-06-27 10:51:37 +0200781/* L3 requests establishment of data link */
782static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
783{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200784 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
785 uint8_t chan_nr = rllh->chan_nr;
786 uint8_t link_id = rllh->link_id;
787 uint8_t sapi = rllh->link_id & 7;
788 struct tlv_parsed tv;
789 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100790 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200791 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200792
rootaf48bed2011-09-26 11:23:06 +0200793 /* Set LAPDm context for established connection */
794 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200795
rootaf48bed2011-09-26 11:23:06 +0200796 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200797 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200798 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200799 /* contention resolution establishment procedure */
800 if (sapi != 0) {
801 /* According to clause 6, the contention resolution
802 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200803 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200804 "resolution (discarding)\n");
805 msgb_free(msg);
806 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
807 }
808 /* transmit a SABM command with the P bit set to "1". The SABM
809 * command shall contain the layer 3 message unit */
810 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200811 } else {
812 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200813 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200814 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200815 }
816
817 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100818 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200819 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100820 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200821 msgb_free(msg);
822 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
823 }
824
rootaf48bed2011-09-26 11:23:06 +0200825 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100826 msgb_pull_to_l3(msg);
827 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200828
rootaf48bed2011-09-26 11:23:06 +0200829 /* prepare prim */
830 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200831
rootaf48bed2011-09-26 11:23:06 +0200832 /* send to L2 */
833 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200834}
835
836/* L3 requests transfer of unnumbered information */
837static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
838{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100839 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200840 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
841 uint8_t chan_nr = rllh->chan_nr;
842 uint8_t link_id = rllh->link_id;
Andreas Eversberg5977db02013-06-12 09:34:51 +0200843 int ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200844 uint8_t sapi = link_id & 7;
845 struct tlv_parsed tv;
846 int length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200847
848 /* check if the layer3 message length exceeds N201 */
849
850 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
851
852 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100853 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200854 }
855 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100856 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200857 }
858 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200859 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200860 "error\n");
861 msgb_free(msg);
862 return -EINVAL;
863 }
rootaf48bed2011-09-26 11:23:06 +0200864 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200865 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
866 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200867 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200868 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200869 "(discarding)\n", length,
870 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200871 msgb_free(msg);
872 return -EIO;
873 }
874
rootaf48bed2011-09-26 11:23:06 +0200875 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100876 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200877
rootaf48bed2011-09-26 11:23:06 +0200878 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100879 msgb_pull_to_l3(msg);
880 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200881
882 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200883 msg->l2h = msgb_push(msg, 2 + !ui_bts);
884 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
885 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100886 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200887 msg->l2h[2] = LAPDm_LEN(length);
888 if (link_id & 0x40) {
889 msg->l2h = msgb_push(msg, 2);
890 msg->l2h[0] = le->tx_power;
891 msg->l2h[1] = le->ta;
892 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200893
894 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200895 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200896}
897
898/* L3 requests transfer of acknowledged information */
899static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
900{
901 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
902 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200903 int length;
904 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200905
906 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
907 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200908 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200909 "error\n");
910 msgb_free(msg);
911 return -EINVAL;
912 }
rootaf48bed2011-09-26 11:23:06 +0200913 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
914 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200915
rootaf48bed2011-09-26 11:23:06 +0200916 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100917 msgb_pull_to_l3(msg);
918 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200919
rootaf48bed2011-09-26 11:23:06 +0200920 /* prepare prim */
921 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200922
rootaf48bed2011-09-26 11:23:06 +0200923 /* send to L2 */
924 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200925}
926
927/* L3 requests suspension of data link */
928static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
929{
930 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
931 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200932 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200933
934 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200935 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200936 msgb_free(msg);
937 return -EINVAL;
938 }
939
rootaf48bed2011-09-26 11:23:06 +0200940 /* prepare prim */
941 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200942
rootaf48bed2011-09-26 11:23:06 +0200943 /* send to L2 */
944 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200945}
946
947/* L3 requests resume of data link */
948static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
949{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200950 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200951 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200952 uint8_t chan_nr = rllh->chan_nr;
953 uint8_t link_id = rllh->link_id;
954 uint8_t sapi = rllh->link_id & 7;
955 struct tlv_parsed tv;
956 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100957 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200958 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200959
rootaf48bed2011-09-26 11:23:06 +0200960 /* Set LAPDm context for established connection */
961 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200962
963 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
964 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200965 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200966 msgb_free(msg);
967 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
968 }
rootaf48bed2011-09-26 11:23:06 +0200969 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200970 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
971
rootaf48bed2011-09-26 11:23:06 +0200972 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100973 msgb_pull_to_l3(msg);
974 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200975
rootaf48bed2011-09-26 11:23:06 +0200976 /* prepare prim */
977 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
978 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200979
rootaf48bed2011-09-26 11:23:06 +0200980 /* send to L2 */
981 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200982}
983
984/* L3 requests release of data link */
985static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
986{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200987 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200988 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +0200989 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200990
991 /* get release mode */
992 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
993 mode = rllh->data[1] & 1;
994
Harald Welte1f0b8c22011-06-27 10:51:37 +0200995 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100996 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +0200997
998 /* 04.06 3.8.3: No information field is permitted with the DISC
999 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001000 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001001
rootaf48bed2011-09-26 11:23:06 +02001002 /* prepare prim */
1003 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1004 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001005
rootaf48bed2011-09-26 11:23:06 +02001006 /* send to L2 */
1007 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001008}
1009
1010/* L3 requests channel in idle state */
1011static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1012{
1013 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1014 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1015 struct osmo_phsap_prim pp;
1016
1017 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1018 PRIM_OP_REQUEST, NULL);
1019
1020 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001021 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001022 return -EINVAL;
1023 }
1024 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001025 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001026 return -EINVAL;
1027 }
1028 pp.u.rach_req.ra = cch->data[1];
1029 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1030 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1031
1032 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001033 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001034 return -EINVAL;
1035 }
1036 /* TA = 0 - delay */
1037 pp.u.rach_req.ta = 0 - cch->data[5];
1038
1039 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001040 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001041 return -EINVAL;
1042 }
1043 pp.u.rach_req.tx_power = cch->data[7];
1044
1045 msgb_free(msg);
1046
1047 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1048}
1049
1050/* L1 confirms channel request */
1051static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1052{
1053 struct abis_rsl_cchan_hdr *ch;
1054 struct gsm_time tm;
1055 struct gsm48_req_ref *ref;
1056
1057 gsm_fn2gsmtime(&tm, frame_nr);
1058
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001059 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001060 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1061 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1062 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1063 ch->chan_nr = RSL_CHAN_RACH;
1064 ch->data[0] = RSL_IE_REQ_REFERENCE;
1065 ref = (struct gsm48_req_ref *) (ch->data + 1);
1066 ref->t1 = tm.t1;
1067 ref->t2 = tm.t2;
1068 ref->t3_low = tm.t3 & 0x7;
1069 ref->t3_high = tm.t3 >> 3;
1070
1071 return rslms_sendmsg(msg, le);
1072}
1073
Harald Welte1f0b8c22011-06-27 10:51:37 +02001074/* incoming RSLms RLL message from L3 */
1075static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1076{
1077 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1078 int msg_type = rllh->c.msg_type;
1079 uint8_t sapi = rllh->link_id & 7;
1080 struct lapdm_entity *le;
1081 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001082 int rc = 0;
1083
1084 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001085 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001086 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001087 return -EINVAL;
1088 }
1089
1090 if (rllh->link_id & 0x40)
1091 le = &lc->lapdm_acch;
1092 else
1093 le = &lc->lapdm_dcch;
1094
Harald Welte1a87c1b2015-12-14 15:26:07 +01001095 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1096 * BTS side */
1097 if (le->mode == LAPDM_MODE_BTS) {
1098 switch (msg_type) {
1099 case RSL_MT_SUSP_REQ:
1100 case RSL_MT_RES_REQ:
1101 case RSL_MT_RECON_REQ:
1102 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1103 lc->name, rsl_msg_name(msg_type));
1104 msgb_free(msg);
1105 return -EINVAL;
1106 break;
1107 default:
1108 break;
1109 }
1110 }
1111
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001112 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001113 * SAPI.
1114 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001115 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001116 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001117 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001118 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001119 return -EINVAL;
1120 }
1121
Daniel Willmanne5233922012-12-25 23:15:50 +01001122 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001123 case RSL_MT_DATA_REQ:
1124 case RSL_MT_SUSP_REQ:
1125 case RSL_MT_REL_REQ:
1126 /* This is triggered in abnormal error conditions where
1127 * set_lapdm_context() was not called for the channel earlier. */
1128 if (!dl->dl.lctx.dl) {
1129 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1130 lc->name, rsl_msg_name(msg_type), sapi);
1131 msgb_free(msg);
1132 return -EINVAL;
1133 }
1134 break;
1135 default:
1136 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1137 lc->name, rsl_msg_name(msg_type), sapi);
1138 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001139
rootaf48bed2011-09-26 11:23:06 +02001140 switch (msg_type) {
1141 case RSL_MT_UNIT_DATA_REQ:
1142 rc = rslms_rx_rll_udata_req(msg, dl);
1143 break;
1144 case RSL_MT_EST_REQ:
1145 rc = rslms_rx_rll_est_req(msg, dl);
1146 break;
1147 case RSL_MT_DATA_REQ:
1148 rc = rslms_rx_rll_data_req(msg, dl);
1149 break;
1150 case RSL_MT_SUSP_REQ:
1151 rc = rslms_rx_rll_susp_req(msg, dl);
1152 break;
1153 case RSL_MT_RES_REQ:
1154 rc = rslms_rx_rll_res_req(msg, dl);
1155 break;
1156 case RSL_MT_RECON_REQ:
1157 rc = rslms_rx_rll_res_req(msg, dl);
1158 break;
1159 case RSL_MT_REL_REQ:
1160 rc = rslms_rx_rll_rel_req(msg, dl);
1161 break;
1162 default:
1163 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001164 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001165 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001166 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001167
1168 return rc;
1169}
1170
1171/* incoming RSLms COMMON CHANNEL message from L3 */
1172static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1173{
1174 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1175 int msg_type = cch->c.msg_type;
1176 int rc = 0;
1177
1178 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001179 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001180 return -EINVAL;
1181 }
1182
1183 switch (msg_type) {
1184 case RSL_MT_CHAN_RQD:
1185 /* create and send RACH request */
1186 rc = rslms_rx_chan_rqd(lc, msg);
1187 break;
1188 default:
rootaf48bed2011-09-26 11:23:06 +02001189 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001190 msg_type);
1191 msgb_free(msg);
1192 return 0;
1193 }
1194
1195 return rc;
1196}
1197
Harald Welte6bdf0b12011-08-17 18:22:08 +02001198/*! \brief Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001199int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1200{
1201 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1202 int rc = 0;
1203
1204 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001205 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001206 return -EINVAL;
1207 }
1208
1209 switch (rslh->msg_discr & 0xfe) {
1210 case ABIS_RSL_MDISC_RLL:
1211 rc = rslms_rx_rll(msg, lc);
1212 break;
1213 case ABIS_RSL_MDISC_COM_CHAN:
1214 rc = rslms_rx_com_chan(msg, lc);
1215 break;
1216 default:
rootaf48bed2011-09-26 11:23:06 +02001217 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001218 "discriminator 0x%02x", rslh->msg_discr);
1219 msgb_free(msg);
1220 return -EINVAL;
1221 }
1222
1223 return rc;
1224}
1225
Harald Welte6bdf0b12011-08-17 18:22:08 +02001226/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001227int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1228{
rootaf48bed2011-09-26 11:23:06 +02001229 int i;
1230 enum lapd_mode lm;
1231
Harald Welte1f0b8c22011-06-27 10:51:37 +02001232 switch (mode) {
1233 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001234 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001235 break;
1236 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001237 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001238 break;
1239 default:
1240 return -EINVAL;
1241 }
1242
rootaf48bed2011-09-26 11:23:06 +02001243 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1244 lapd_set_mode(&le->datalink[i].dl, lm);
1245 }
1246
Harald Welte1f0b8c22011-06-27 10:51:37 +02001247 le->mode = mode;
1248
1249 return 0;
1250}
1251
Harald Welte6bdf0b12011-08-17 18:22:08 +02001252/*! \brief Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001253int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1254{
1255 int rc;
1256
1257 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1258 if (rc < 0)
1259 return rc;
1260
1261 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1262}
1263
Harald Welte6bdf0b12011-08-17 18:22:08 +02001264/*! \brief Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001265void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1266{
1267 lc->lapdm_dcch.l1_prim_cb = cb;
1268 lc->lapdm_acch.l1_prim_cb = cb;
1269 lc->lapdm_dcch.l1_ctx = ctx;
1270 lc->lapdm_acch.l1_ctx = ctx;
1271}
1272
Harald Welte6bdf0b12011-08-17 18:22:08 +02001273/*! \brief Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001274void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1275{
1276 lc->lapdm_dcch.l3_cb = cb;
1277 lc->lapdm_acch.l3_cb = cb;
1278 lc->lapdm_dcch.l3_ctx = ctx;
1279 lc->lapdm_acch.l3_ctx = ctx;
1280}
1281
Harald Welte6bdf0b12011-08-17 18:22:08 +02001282/*! \brief Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001283void lapdm_entity_reset(struct lapdm_entity *le)
1284{
1285 struct lapdm_datalink *dl;
1286 int i;
1287
1288 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1289 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001290 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001291 }
1292}
1293
Harald Welte6bdf0b12011-08-17 18:22:08 +02001294/*! \brief Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001295void lapdm_channel_reset(struct lapdm_channel *lc)
1296{
1297 lapdm_entity_reset(&lc->lapdm_dcch);
1298 lapdm_entity_reset(&lc->lapdm_acch);
1299}
1300
Harald Welte6bdf0b12011-08-17 18:22:08 +02001301/*! \brief Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001302void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1303{
1304 le->flags = flags;
1305}
1306
Harald Welte6bdf0b12011-08-17 18:22:08 +02001307/*! \brief Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001308void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1309{
1310 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1311 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1312}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001313
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001314/*! @} */