blob: 99099d196fb854e8abe6aabfb2fac0a779a54757 [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>
Harald Welte1f0b8c22011-06-27 10:51:37 +020034
35#include <osmocom/core/logging.h>
36#include <osmocom/core/timer.h>
37#include <osmocom/core/msgb.h>
38#include <osmocom/core/utils.h>
39
40#include <osmocom/gsm/tlv.h>
41#include <osmocom/gsm/rsl.h>
42#include <osmocom/gsm/prim.h>
43#include <osmocom/gsm/gsm_utils.h>
44#include <osmocom/gsm/lapdm.h>
45
46#include <osmocom/gsm/protocol/gsm_04_08.h>
47#include <osmocom/gsm/protocol/gsm_08_58.h>
48
49/* TS 04.06 Figure 4 / Section 3.2 */
50#define LAPDm_LPD_NORMAL 0
51#define LAPDm_LPD_SMSCB 1
52#define LAPDm_SAPI_NORMAL 0
53#define LAPDm_SAPI_SMS 3
54#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
55
rootaf48bed2011-09-26 11:23:06 +020056#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020057#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
58#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
59#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
60
61/* TS 04.06 Table 3 / Section 3.4.3 */
62#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
63#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
64#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
65
66#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
67#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
68#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
69
70#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
71#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
72
73#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
74
75#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
76#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
77
Harald Welte1f0b8c22011-06-27 10:51:37 +020078#define LAPDm_LEN(len) ((len << 2) | 0x1)
79#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020080#define LAPDm_EL 0x1
81
82#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020083
84/* TS 04.06 Section 5.8.3 */
85#define N201_AB_SACCH 18
86#define N201_AB_SDCCH 20
87#define N201_AB_FACCH 20
88#define N201_Bbis 23
89#define N201_Bter_SACCH 21
90#define N201_Bter_SDCCH 23
91#define N201_Bter_FACCH 23
92#define N201_B4 19
93
94/* 5.8.2.1 N200 during establish and release */
95#define N200_EST_REL 5
96/* 5.8.2.1 N200 during timer recovery state */
97#define N200_TR_SACCH 5
98#define N200_TR_SDCCH 23
99#define N200_TR_FACCH_FR 34
100#define N200_TR_EFACCH_FR 48
101#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200102/* FIXME: set N200 depending on chan_nr */
103#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200104
105enum lapdm_format {
106 LAPDm_FMT_A,
107 LAPDm_FMT_B,
108 LAPDm_FMT_Bbis,
109 LAPDm_FMT_Bter,
110 LAPDm_FMT_B4,
111};
112
Maxadef12a2016-05-25 15:25:02 +0200113const struct value_string osmo_ph_prim_names[] = {
114 { PRIM_PH_DATA, "PH-DATA" },
115 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
116 { PRIM_PH_CONN, "PH-CONNECT" },
117 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
118 { PRIM_PH_RTS, "PH-RTS" },
119 { PRIM_MPH_INFO, "MPH-INFO" },
120 { PRIM_TCH, "TCH" },
121 { PRIM_TCH_RTS, "TCH-RTS" },
122 { 0, NULL }
123};
124
rootaf48bed2011-09-26 11:23:06 +0200125static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
126static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
127 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100128static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200129
130static void lapdm_dl_init(struct lapdm_datalink *dl,
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100131 struct lapdm_entity *entity, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200132{
133 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200134 dl->entity = entity;
rootaf48bed2011-09-26 11:23:06 +0200135 lapd_dl_init(&dl->dl, 1, 8, 200);
136 dl->dl.reestablish = 0; /* GSM uses no reestablish */
137 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
138 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100139 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200140 dl->dl.n200_est_rel = N200_EST_REL;
141 dl->dl.n200 = N200;
142 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100143 dl->dl.t200_sec = t200; dl->dl.t200_usec = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200144}
145
Harald Welte6bdf0b12011-08-17 18:22:08 +0200146/*! \brief initialize a LAPDm entity and all datalinks inside
147 * \param[in] le LAPDm entity
148 * \param[in] mode \ref lapdm_mode (BTS/MS)
149 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100150void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200151{
152 unsigned int i;
153
154 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100155 lapdm_dl_init(&le->datalink[i], le, t200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200156
157 lapdm_entity_set_mode(le, mode);
158}
159
Harald Welte6bdf0b12011-08-17 18:22:08 +0200160/*! \brief initialize a LAPDm channel and all its channels
161 * \param[in] lc \ref lapdm_channel to be initialized
162 * \param[in] mode \ref lapdm_mode (BTS/MS)
163 *
164 * This really is a convenience wrapper around calling \ref
165 * lapdm_entity_init twice.
166 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200167void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
168{
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100169 lapdm_entity_init(&lc->lapdm_acch, mode, 2);
rootaf48bed2011-09-26 11:23:06 +0200170 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100171 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200172}
173
Harald Welte6bdf0b12011-08-17 18:22:08 +0200174/*! \brief flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200175void lapdm_entity_exit(struct lapdm_entity *le)
176{
177 unsigned int i;
178 struct lapdm_datalink *dl;
179
180 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
181 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200182 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200183 }
184}
185
Harald Welte6bdf0b12011-08-17 18:22:08 +0200186/* \brief lfush and release all resources in LAPDm channel
187 *
188 * A convenience wrapper calling \ref lapdm_entity_exit on both
189 * entities inside the \ref lapdm_channel
190 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200191void lapdm_channel_exit(struct lapdm_channel *lc)
192{
193 lapdm_entity_exit(&lc->lapdm_acch);
194 lapdm_entity_exit(&lc->lapdm_dcch);
195}
196
Daniel Willmann55405fb2014-03-26 13:45:17 +0100197struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200198{
199 switch (sapi) {
200 case LAPDm_SAPI_NORMAL:
201 return &le->datalink[0];
202 case LAPDm_SAPI_SMS:
203 return &le->datalink[1];
204 default:
205 return NULL;
206 }
207}
208
Harald Welte1f0b8c22011-06-27 10:51:37 +0200209/* Append padding (if required) */
210static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
211{
212 int pad_len = n201 - msgb_l2len(msg);
213 uint8_t *data;
214
215 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200216 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200217 "cannot pad message that is already too big!\n");
218 return;
219 }
220
221 data = msgb_put(msg, pad_len);
222 memset(data, 0x2B, pad_len);
223}
224
225/* input function that L2 calls when sending messages up to L3 */
226static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
227{
228 if (!le->l3_cb) {
229 msgb_free(msg);
230 return -EIO;
231 }
232
233 /* call the layer2 message handler that is registered */
234 return le->l3_cb(msg, le, le->l3_ctx);
235}
236
237/* write a frame into the tx queue */
238static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200239 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200240{
241 struct lapdm_entity *le = dl->entity;
242 struct osmo_phsap_prim pp;
243
244 /* if there is a pending message, queue it */
245 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200246 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200247 *msgb_push(msg, 1) = link_id;
248 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200249 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200250 return -EBUSY;
251 }
252
253 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
254 PRIM_OP_REQUEST, msg);
255 pp.u.data.chan_nr = chan_nr;
256 pp.u.data.link_id = link_id;
257
258 /* send the frame now */
259 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200260 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200261
262 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
263}
264
265static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
266{
267 struct lapdm_datalink *dl;
268 int last = le->last_tx_dequeue;
269 int i = last, n = ARRAY_SIZE(le->datalink);
270 struct msgb *msg = NULL;
271
272 /* round-robin dequeue */
273 do {
274 /* next */
275 i = (i + 1) % n;
276 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200277 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200278 break;
279 } while (i != last);
280
281 if (msg) {
282 /* Set last dequeue position */
283 le->last_tx_dequeue = i;
284 }
285
286 return msg;
287}
288
Harald Welte6bdf0b12011-08-17 18:22:08 +0200289/*! \brief dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200290 * a osmo_phsap_prim */
291int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
292{
293 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200294 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200295
296 msg = tx_dequeue_msgb(le);
297 if (!msg)
298 return -ENODEV;
299
300 /* if we have a message, send PH-DATA.req */
301 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
302 PRIM_OP_REQUEST, msg);
303
304 /* Pull chan_nr and link_id */
305 pp->u.data.chan_nr = *msg->data;
306 msgb_pull(msg, 1);
307 pp->u.data.link_id = *msg->data;
308 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200309 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200310 msgb_pull(msg, 1);
311
312 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200313 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200314
315 return 0;
316}
317
318/* get next frame from the tx queue. because the ms has multiple datalinks,
319 * each datalink's queue is read round-robin.
320 */
321static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
322{
323 struct osmo_phsap_prim pp;
324
325 /* we may send again */
326 le->tx_pending = 0;
327
328 /* free confirm message */
329 if (msg)
330 msgb_free(msg);
331
332 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
333 /* no message in all queues */
334
335 /* If user didn't request PH-EMPTY_FRAME.req, abort */
336 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
337 return 0;
338
339 /* otherwise, send PH-EMPTY_FRAME.req */
340 osmo_prim_init(&pp.oph, SAP_GSM_PH,
341 PRIM_PH_EMPTY_FRAME,
342 PRIM_OP_REQUEST, NULL);
343 } else {
344 le->tx_pending = 1;
345 }
346
347 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
348}
349
350/* Create RSLms various RSLms messages */
351static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
352 struct msgb *msg)
353{
354 /* Add the RSL + RLL header */
355 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
356
357 /* send off the RSLms message to L3 */
358 return rslms_sendmsg(msg, mctx->dl->entity);
359}
360
361/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
362static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
363{
364 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
365 struct abis_rsl_rll_hdr *rllh;
366
367 /* Add the RSL + RLL header */
368 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
369 msgb_push(msg, 2 + 2);
370 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
371 mctx->link_id, 1);
372 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
373
374 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
375 rllh->data[1] = mctx->ta_ind;
376
377 rllh->data[2] = RSL_IE_MS_POWER;
378 rllh->data[3] = mctx->tx_power_ind;
379
380 return rslms_sendmsg(msg, mctx->dl->entity);
381}
382
383static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
384{
385 struct msgb *msg;
386
387 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
388
389 /* send off the RSLms message to L3 */
390 return rslms_sendmsg(msg, mctx->dl->entity);
391}
392
393static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
394{
395 struct msgb *msg;
396
rootaf48bed2011-09-26 11:23:06 +0200397 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200398 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200399 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
400 return rslms_sendmsg(msg, mctx->dl->entity);
401}
402
rootaf48bed2011-09-26 11:23:06 +0200403/* DLSAP L2 -> L3 (RSLms) */
404static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
405 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200406{
rootaf48bed2011-09-26 11:23:06 +0200407 struct lapd_datalink *dl = lctx->dl;
408 struct lapdm_datalink *mdl =
409 container_of(dl, struct lapdm_datalink, dl);
410 struct lapdm_msg_ctx *mctx = &mdl->mctx;
411 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200412
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200413 switch (OSMO_PRIM_HDR(&dp->oph)) {
414 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
Andreas Eversberg5977db02013-06-12 09:34:51 +0200415 if (dp->oph.msg && dp->oph.msg->len == 0) {
416 /* omit L3 info by freeing message */
417 msgb_free(dp->oph.msg);
418 dp->oph.msg = NULL;
419 }
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200420 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200421 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200422 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
423 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200424 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200425 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
426 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200427 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200428 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
429 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
430 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
431 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200432 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200433 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
434 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200435 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200436 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
437 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200438 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200439 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
440 rsl_rll_error(dp->u.error_ind.cause, mctx);
441 if (dp->oph.msg)
442 msgb_free(dp->oph.msg);
443 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200444 }
rootaf48bed2011-09-26 11:23:06 +0200445
446 if (!rll_msg) {
447 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
448 "fix!\n", dp->oph.primitive, dp->oph.operation);
449 return -EINVAL;
450 }
451
452 if (!dp->oph.msg)
453 return send_rll_simple(rll_msg, mctx);
454
455 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200456}
457
rootaf48bed2011-09-26 11:23:06 +0200458/* send a data frame to layer 1 */
459static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200460{
rootaf48bed2011-09-26 11:23:06 +0200461 uint8_t l3_len = msg->tail - msg->data;
462 struct lapd_datalink *dl = lctx->dl;
463 struct lapdm_datalink *mdl =
464 container_of(dl, struct lapdm_datalink, dl);
465 struct lapdm_msg_ctx *mctx = &mdl->mctx;
466 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200467
rootaf48bed2011-09-26 11:23:06 +0200468 /* prepend l2 header */
469 msg->l2h = msgb_push(msg, 3);
470 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
471 /* EA is set here too */
472 switch (format) {
473 case LAPD_FORM_I:
474 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
475 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200476 break;
rootaf48bed2011-09-26 11:23:06 +0200477 case LAPD_FORM_S:
478 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200479 break;
rootaf48bed2011-09-26 11:23:06 +0200480 case LAPD_FORM_U:
481 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200482 break;
483 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200484 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200485 return -EINVAL;
486 }
rootaf48bed2011-09-26 11:23:06 +0200487 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
488 if (lctx->more)
489 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200490
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100491 /* add ACCH header with last indicated tx-power and TA */
492 if ((mctx->link_id & 0x40)) {
493 struct lapdm_entity *le = mdl->entity;
494
495 msg->l2h = msgb_push(msg, 2);
496 msg->l2h[0] = le->tx_power;
497 msg->l2h[1] = le->ta;
498 }
499
rootaf48bed2011-09-26 11:23:06 +0200500 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
501 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200502}
503
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100504static int update_pending_frames(struct lapd_msg_ctx *lctx)
505{
506 struct lapd_datalink *dl = lctx->dl;
507 struct msgb *msg;
508 int rc = -1;
509
510 llist_for_each_entry(msg, &dl->tx_queue, list) {
511 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
512 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
513 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
514 rc = 0;
515 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
516 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
517 }
518 }
519
520 return rc;
521}
522
Harald Welte1f0b8c22011-06-27 10:51:37 +0200523/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200524static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
525 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200526{
527 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200528 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200529 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200530 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200531 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200532 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200533
534 /* when we reach here, we have a msgb with l2h pointing to the raw
535 * 23byte mac block. The l1h has already been purged. */
536
rootaf48bed2011-09-26 11:23:06 +0200537 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200538 mctx.chan_nr = chan_nr;
539 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200540
Harald Welte1f0b8c22011-06-27 10:51:37 +0200541 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
542 if (cbits == 0x10 || cbits == 0x12) {
543 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
544 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200545 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200546 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200547 } else {
548 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200549 /* It was received from network on SACCH */
550
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100551 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
552 if (le->mode == LAPDM_MODE_MS
553 && LAPDm_CTRL_is_U(msg->l2h[3])
554 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200555 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200556 n201 = N201_B4;
557 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200558 } else {
559 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200560 n201 = N201_AB_SACCH;
561 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200562 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200563 /* SACCH frames have a two-byte L1 header that
564 * OsmocomBB L1 doesn't strip */
565 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
566 mctx.ta_ind = msg->l2h[1];
567 msgb_pull(msg, 2);
568 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200569 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200570 } else {
571 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200572 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100573 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200574 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200575 }
576 }
577
Daniel Willmann55405fb2014-03-26 13:45:17 +0100578 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200579 /* G.2.1 No action on frames containing an unallocated SAPI. */
580 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200581 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200582 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200583 msgb_free(msg);
584 return -EIO;
585 }
586
Harald Welte1f0b8c22011-06-27 10:51:37 +0200587 switch (mctx.lapdm_fmt) {
588 case LAPDm_FMT_A:
589 case LAPDm_FMT_B:
590 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200591 lctx.dl = &mctx.dl->dl;
592 /* obtain SAPI from address field */
593 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
594 /* G.2.3 EA bit set to "0" is not allowed in GSM */
595 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
596 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
597 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200598 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200599 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200600 return -EINVAL;
601 }
rootaf48bed2011-09-26 11:23:06 +0200602 /* adress field */
603 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
604 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
605 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
606 /* command field */
607 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
608 lctx.format = LAPD_FORM_I;
609 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
610 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
611 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
612 lctx.format = LAPD_FORM_S;
613 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
614 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
615 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
616 lctx.format = LAPD_FORM_U;
617 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
618 } else
619 lctx.format = LAPD_FORM_UKN;
620 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
621 if (lctx.sapi != LAPDm_SAPI_NORMAL
622 && lctx.sapi != LAPDm_SAPI_SMS
623 && lctx.format == LAPD_FORM_U
624 && lctx.s_u == LAPDm_U_UI) {
625 /* 5.3.3 UI frames with invalid SAPI values shall be
626 * discarded
627 */
628 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
629 lctx.sapi);
630 msgb_free(msg);
631 return 0;
632 }
633 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
634 lctx.n201 = n201;
635 lctx.length = n201;
636 lctx.more = 0;
637 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100638 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200639 } else {
640 /* length field */
641 if (!(msg->l2h[2] & LAPDm_EL)) {
642 /* G.4.1 If the EL bit is set to "0", an
643 * MDL-ERROR-INDICATION primitive with cause
644 * "frame not implemented" is sent to the
645 * mobile management entity. */
646 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
647 "multi-octet length\n");
648 msgb_free(msg);
649 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
650 return -EINVAL;
651 }
652 lctx.n201 = n201;
653 lctx.length = msg->l2h[2] >> 2;
654 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
655 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100656 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200657 }
658 /* store context for messages from lapd */
659 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
660 /* send to LAPD */
661 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200662 break;
663 case LAPDm_FMT_Bter:
664 /* FIXME */
665 msgb_free(msg);
666 break;
667 case LAPDm_FMT_Bbis:
668 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200669 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200670 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100671 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200672 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
673 break;
674 default:
675 msgb_free(msg);
676 }
677
678 return rc;
679}
680
681/* input into layer2 (from layer 1) */
682static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
683{
684 struct abis_rsl_cchan_hdr *ch;
685 struct gsm48_req_ref req_ref;
686 struct gsm_time gt;
687 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
688
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200689 if (!msg)
690 return -ENOMEM;
691
Harald Welte1f0b8c22011-06-27 10:51:37 +0200692 msg->l2h = msgb_push(msg, sizeof(*ch));
693 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
694 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
695 ch->chan_nr = RSL_CHAN_RACH;
696
697 /* generate a RSL CHANNEL REQUIRED message */
698 gsm_fn2gsmtime(&gt, fn);
699 req_ref.ra = ra;
700 req_ref.t1 = gt.t1; /* FIXME: modulo? */
701 req_ref.t2 = gt.t2;
702 req_ref.t3_low = gt.t3 & 7;
703 req_ref.t3_high = gt.t3 >> 3;
704
705 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
706 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
707
708 return rslms_sendmsg(msg, le);
709}
710
711static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
712
Harald Welte6bdf0b12011-08-17 18:22:08 +0200713/*! \brief Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200714int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
715{
716 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
717 int rc = 0;
718
719 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200720 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200721 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100722 rc = -ENODEV;
723 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200724 }
725
726 switch (oph->primitive) {
727 case PRIM_PH_DATA:
728 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200729 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200730 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100731 rc = -ENODEV;
732 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200733 }
734 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
735 pp->u.data.link_id);
736 break;
737 case PRIM_PH_RTS:
738 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200739 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200740 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100741 rc = -ENODEV;
742 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200743 }
744 rc = l2_ph_data_conf(oph->msg, le);
745 break;
746 case PRIM_PH_RACH:
747 switch (oph->operation) {
748 case PRIM_OP_INDICATION:
749 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
750 pp->u.rach_ind.acc_delay);
751 break;
752 case PRIM_OP_CONFIRM:
753 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
754 break;
755 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100756 rc = -EIO;
757 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200758 }
759 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100760 default:
761 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
762 oph->primitive);
763 rc = -EINVAL;
764 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200765 }
766
767 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100768
769free:
770 msgb_free(oph->msg);
771 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200772}
773
774
775/* L3 -> L2 / RSLMS -> LAPDm */
776
rootaf48bed2011-09-26 11:23:06 +0200777/* Set LAPDm context for established connection */
778static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
779 uint8_t link_id, int n201, uint8_t sapi)
780{
781 memset(&dl->mctx, 0, sizeof(dl->mctx));
782 dl->mctx.dl = dl;
783 dl->mctx.chan_nr = chan_nr;
784 dl->mctx.link_id = link_id;
785 dl->dl.lctx.dl = &dl->dl;
786 dl->dl.lctx.n201 = n201;
787 dl->dl.lctx.sapi = sapi;
788
789 return 0;
790}
791
Harald Welte1f0b8c22011-06-27 10:51:37 +0200792/* L3 requests establishment of data link */
793static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
794{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200795 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
796 uint8_t chan_nr = rllh->chan_nr;
797 uint8_t link_id = rllh->link_id;
798 uint8_t sapi = rllh->link_id & 7;
799 struct tlv_parsed tv;
800 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100801 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200802 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200803
rootaf48bed2011-09-26 11:23:06 +0200804 /* Set LAPDm context for established connection */
805 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200806
rootaf48bed2011-09-26 11:23:06 +0200807 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200808 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200809 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200810 /* contention resolution establishment procedure */
811 if (sapi != 0) {
812 /* According to clause 6, the contention resolution
813 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200814 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200815 "resolution (discarding)\n");
816 msgb_free(msg);
817 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
818 }
819 /* transmit a SABM command with the P bit set to "1". The SABM
820 * command shall contain the layer 3 message unit */
821 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200822 } else {
823 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200824 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200825 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200826 }
827
828 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100829 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200830 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100831 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200832 msgb_free(msg);
833 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
834 }
835
rootaf48bed2011-09-26 11:23:06 +0200836 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100837 msgb_pull_to_l3(msg);
838 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200839
rootaf48bed2011-09-26 11:23:06 +0200840 /* prepare prim */
841 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200842
rootaf48bed2011-09-26 11:23:06 +0200843 /* send to L2 */
844 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200845}
846
847/* L3 requests transfer of unnumbered information */
848static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
849{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100850 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200851 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
852 uint8_t chan_nr = rllh->chan_nr;
853 uint8_t link_id = rllh->link_id;
854 uint8_t sapi = link_id & 7;
855 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +0100856 int length, ui_bts;
857
858 if (!le) {
859 LOGP(DLLAPD, LOGL_ERROR, "lapdm_datalink without entity error\n");
860 msgb_free(msg);
861 return -EMLINK;
862 }
863 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200864
865 /* check if the layer3 message length exceeds N201 */
866
867 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
868
869 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100870 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200871 }
872 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100873 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200874 }
875 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200876 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200877 "error\n");
878 msgb_free(msg);
879 return -EINVAL;
880 }
rootaf48bed2011-09-26 11:23:06 +0200881 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200882 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
883 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200884 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200885 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200886 "(discarding)\n", length,
887 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200888 msgb_free(msg);
889 return -EIO;
890 }
891
rootaf48bed2011-09-26 11:23:06 +0200892 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100893 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200894
rootaf48bed2011-09-26 11:23:06 +0200895 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100896 msgb_pull_to_l3(msg);
897 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200898
899 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200900 msg->l2h = msgb_push(msg, 2 + !ui_bts);
901 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
902 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100903 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200904 msg->l2h[2] = LAPDm_LEN(length);
905 if (link_id & 0x40) {
906 msg->l2h = msgb_push(msg, 2);
907 msg->l2h[0] = le->tx_power;
908 msg->l2h[1] = le->ta;
909 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200910
911 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200912 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200913}
914
915/* L3 requests transfer of acknowledged information */
916static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
917{
918 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
919 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200920 int length;
921 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200922
923 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
924 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200925 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200926 "error\n");
927 msgb_free(msg);
928 return -EINVAL;
929 }
rootaf48bed2011-09-26 11:23:06 +0200930 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
931 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200932
rootaf48bed2011-09-26 11:23:06 +0200933 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100934 msgb_pull_to_l3(msg);
935 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200936
rootaf48bed2011-09-26 11:23:06 +0200937 /* prepare prim */
938 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200939
rootaf48bed2011-09-26 11:23:06 +0200940 /* send to L2 */
941 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200942}
943
944/* L3 requests suspension of data link */
945static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
946{
947 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
948 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200949 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200950
951 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200952 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200953 msgb_free(msg);
954 return -EINVAL;
955 }
956
rootaf48bed2011-09-26 11:23:06 +0200957 /* prepare prim */
958 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200959
rootaf48bed2011-09-26 11:23:06 +0200960 /* send to L2 */
961 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200962}
963
964/* L3 requests resume of data link */
965static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
966{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200967 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200968 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200969 uint8_t chan_nr = rllh->chan_nr;
970 uint8_t link_id = rllh->link_id;
971 uint8_t sapi = rllh->link_id & 7;
972 struct tlv_parsed tv;
973 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100974 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200975 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200976
rootaf48bed2011-09-26 11:23:06 +0200977 /* Set LAPDm context for established connection */
978 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200979
980 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
981 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200982 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200983 msgb_free(msg);
984 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
985 }
rootaf48bed2011-09-26 11:23:06 +0200986 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200987 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
988
rootaf48bed2011-09-26 11:23:06 +0200989 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100990 msgb_pull_to_l3(msg);
991 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200992
rootaf48bed2011-09-26 11:23:06 +0200993 /* prepare prim */
994 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
995 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200996
rootaf48bed2011-09-26 11:23:06 +0200997 /* send to L2 */
998 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200999}
1000
1001/* L3 requests release of data link */
1002static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1003{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001004 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001005 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001006 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001007
1008 /* get release mode */
1009 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1010 mode = rllh->data[1] & 1;
1011
Harald Welte1f0b8c22011-06-27 10:51:37 +02001012 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001013 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001014
1015 /* 04.06 3.8.3: No information field is permitted with the DISC
1016 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001017 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001018
rootaf48bed2011-09-26 11:23:06 +02001019 /* prepare prim */
1020 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1021 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001022
rootaf48bed2011-09-26 11:23:06 +02001023 /* send to L2 */
1024 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001025}
1026
1027/* L3 requests channel in idle state */
1028static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1029{
1030 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1031 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1032 struct osmo_phsap_prim pp;
1033
1034 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1035 PRIM_OP_REQUEST, NULL);
1036
1037 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001038 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001039 return -EINVAL;
1040 }
1041 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001042 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001043 return -EINVAL;
1044 }
1045 pp.u.rach_req.ra = cch->data[1];
1046 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1047 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1048
1049 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001050 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001051 return -EINVAL;
1052 }
1053 /* TA = 0 - delay */
1054 pp.u.rach_req.ta = 0 - cch->data[5];
1055
1056 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001057 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001058 return -EINVAL;
1059 }
1060 pp.u.rach_req.tx_power = cch->data[7];
1061
1062 msgb_free(msg);
1063
1064 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1065}
1066
1067/* L1 confirms channel request */
1068static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1069{
1070 struct abis_rsl_cchan_hdr *ch;
1071 struct gsm_time tm;
1072 struct gsm48_req_ref *ref;
1073
1074 gsm_fn2gsmtime(&tm, frame_nr);
1075
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001076 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001077 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1078 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1079 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1080 ch->chan_nr = RSL_CHAN_RACH;
1081 ch->data[0] = RSL_IE_REQ_REFERENCE;
1082 ref = (struct gsm48_req_ref *) (ch->data + 1);
1083 ref->t1 = tm.t1;
1084 ref->t2 = tm.t2;
1085 ref->t3_low = tm.t3 & 0x7;
1086 ref->t3_high = tm.t3 >> 3;
1087
1088 return rslms_sendmsg(msg, le);
1089}
1090
Harald Welte1f0b8c22011-06-27 10:51:37 +02001091/* incoming RSLms RLL message from L3 */
1092static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1093{
1094 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1095 int msg_type = rllh->c.msg_type;
1096 uint8_t sapi = rllh->link_id & 7;
1097 struct lapdm_entity *le;
1098 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001099 int rc = 0;
1100
1101 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001102 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001103 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001104 return -EINVAL;
1105 }
1106
1107 if (rllh->link_id & 0x40)
1108 le = &lc->lapdm_acch;
1109 else
1110 le = &lc->lapdm_dcch;
1111
Harald Welte1a87c1b2015-12-14 15:26:07 +01001112 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1113 * BTS side */
1114 if (le->mode == LAPDM_MODE_BTS) {
1115 switch (msg_type) {
1116 case RSL_MT_SUSP_REQ:
1117 case RSL_MT_RES_REQ:
1118 case RSL_MT_RECON_REQ:
1119 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1120 lc->name, rsl_msg_name(msg_type));
1121 msgb_free(msg);
1122 return -EINVAL;
1123 break;
1124 default:
1125 break;
1126 }
1127 }
1128
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001129 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001130 * SAPI.
1131 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001132 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001133 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001134 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001135 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001136 return -EINVAL;
1137 }
1138
Daniel Willmanne5233922012-12-25 23:15:50 +01001139 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001140 case RSL_MT_DATA_REQ:
1141 case RSL_MT_SUSP_REQ:
1142 case RSL_MT_REL_REQ:
1143 /* This is triggered in abnormal error conditions where
1144 * set_lapdm_context() was not called for the channel earlier. */
1145 if (!dl->dl.lctx.dl) {
1146 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1147 lc->name, rsl_msg_name(msg_type), sapi);
1148 msgb_free(msg);
1149 return -EINVAL;
1150 }
1151 break;
1152 default:
1153 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1154 lc->name, rsl_msg_name(msg_type), sapi);
1155 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001156
rootaf48bed2011-09-26 11:23:06 +02001157 switch (msg_type) {
1158 case RSL_MT_UNIT_DATA_REQ:
1159 rc = rslms_rx_rll_udata_req(msg, dl);
1160 break;
1161 case RSL_MT_EST_REQ:
1162 rc = rslms_rx_rll_est_req(msg, dl);
1163 break;
1164 case RSL_MT_DATA_REQ:
1165 rc = rslms_rx_rll_data_req(msg, dl);
1166 break;
1167 case RSL_MT_SUSP_REQ:
1168 rc = rslms_rx_rll_susp_req(msg, dl);
1169 break;
1170 case RSL_MT_RES_REQ:
1171 rc = rslms_rx_rll_res_req(msg, dl);
1172 break;
1173 case RSL_MT_RECON_REQ:
1174 rc = rslms_rx_rll_res_req(msg, dl);
1175 break;
1176 case RSL_MT_REL_REQ:
1177 rc = rslms_rx_rll_rel_req(msg, dl);
1178 break;
1179 default:
1180 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001181 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001182 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001183 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001184
1185 return rc;
1186}
1187
1188/* incoming RSLms COMMON CHANNEL message from L3 */
1189static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1190{
1191 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1192 int msg_type = cch->c.msg_type;
1193 int rc = 0;
1194
1195 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001196 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001197 return -EINVAL;
1198 }
1199
1200 switch (msg_type) {
1201 case RSL_MT_CHAN_RQD:
1202 /* create and send RACH request */
1203 rc = rslms_rx_chan_rqd(lc, msg);
1204 break;
1205 default:
rootaf48bed2011-09-26 11:23:06 +02001206 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001207 msg_type);
1208 msgb_free(msg);
1209 return 0;
1210 }
1211
1212 return rc;
1213}
1214
Harald Welte6bdf0b12011-08-17 18:22:08 +02001215/*! \brief Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001216int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1217{
1218 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1219 int rc = 0;
1220
1221 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001222 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001223 return -EINVAL;
1224 }
1225
1226 switch (rslh->msg_discr & 0xfe) {
1227 case ABIS_RSL_MDISC_RLL:
1228 rc = rslms_rx_rll(msg, lc);
1229 break;
1230 case ABIS_RSL_MDISC_COM_CHAN:
1231 rc = rslms_rx_com_chan(msg, lc);
1232 break;
1233 default:
rootaf48bed2011-09-26 11:23:06 +02001234 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001235 "discriminator 0x%02x", rslh->msg_discr);
1236 msgb_free(msg);
1237 return -EINVAL;
1238 }
1239
1240 return rc;
1241}
1242
Harald Welte6bdf0b12011-08-17 18:22:08 +02001243/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001244int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1245{
rootaf48bed2011-09-26 11:23:06 +02001246 int i;
1247 enum lapd_mode lm;
1248
Harald Welte1f0b8c22011-06-27 10:51:37 +02001249 switch (mode) {
1250 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001251 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001252 break;
1253 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001254 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001255 break;
1256 default:
1257 return -EINVAL;
1258 }
1259
rootaf48bed2011-09-26 11:23:06 +02001260 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1261 lapd_set_mode(&le->datalink[i].dl, lm);
1262 }
1263
Harald Welte1f0b8c22011-06-27 10:51:37 +02001264 le->mode = mode;
1265
1266 return 0;
1267}
1268
Harald Welte6bdf0b12011-08-17 18:22:08 +02001269/*! \brief Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001270int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1271{
1272 int rc;
1273
1274 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1275 if (rc < 0)
1276 return rc;
1277
1278 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1279}
1280
Harald Welte6bdf0b12011-08-17 18:22:08 +02001281/*! \brief Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001282void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1283{
1284 lc->lapdm_dcch.l1_prim_cb = cb;
1285 lc->lapdm_acch.l1_prim_cb = cb;
1286 lc->lapdm_dcch.l1_ctx = ctx;
1287 lc->lapdm_acch.l1_ctx = ctx;
1288}
1289
Harald Welte6bdf0b12011-08-17 18:22:08 +02001290/*! \brief Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001291void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1292{
1293 lc->lapdm_dcch.l3_cb = cb;
1294 lc->lapdm_acch.l3_cb = cb;
1295 lc->lapdm_dcch.l3_ctx = ctx;
1296 lc->lapdm_acch.l3_ctx = ctx;
1297}
1298
Harald Welte6bdf0b12011-08-17 18:22:08 +02001299/*! \brief Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001300void lapdm_entity_reset(struct lapdm_entity *le)
1301{
1302 struct lapdm_datalink *dl;
1303 int i;
1304
1305 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1306 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001307 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001308 }
1309}
1310
Harald Welte6bdf0b12011-08-17 18:22:08 +02001311/*! \brief Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001312void lapdm_channel_reset(struct lapdm_channel *lc)
1313{
1314 lapdm_entity_reset(&lc->lapdm_dcch);
1315 lapdm_entity_reset(&lc->lapdm_acch);
1316}
1317
Harald Welte6bdf0b12011-08-17 18:22:08 +02001318/*! \brief Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001319void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1320{
1321 le->flags = flags;
1322}
1323
Harald Welte6bdf0b12011-08-17 18:22:08 +02001324/*! \brief Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001325void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1326{
1327 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1328 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1329}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001330
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001331/*! @} */