blob: db950a68966426b1b12945d6072401959fad221c [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file lapdm.c
2 * GSM LAPDm (TS 04.06) implementation. */
3/*
4 * (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
rootaf48bed2011-09-26 11:23:06 +02005 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
Harald Welte1f0b8c22011-06-27 10:51:37 +02006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
Harald Welte6bdf0b12011-08-17 18:22:08 +020025/*! \addtogroup lapdm
26 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020027 * \file lapdm.c */
Harald Welte6bdf0b12011-08-17 18:22:08 +020028
Harald Welte1f0b8c22011-06-27 10:51:37 +020029#include <stdio.h>
30#include <stdint.h>
31#include <string.h>
32#include <errno.h>
Harald Welte1f0b8c22011-06-27 10:51:37 +020033
34#include <osmocom/core/logging.h>
35#include <osmocom/core/timer.h>
36#include <osmocom/core/msgb.h>
37#include <osmocom/core/utils.h>
38
39#include <osmocom/gsm/tlv.h>
40#include <osmocom/gsm/rsl.h>
41#include <osmocom/gsm/prim.h>
42#include <osmocom/gsm/gsm_utils.h>
43#include <osmocom/gsm/lapdm.h>
44
45#include <osmocom/gsm/protocol/gsm_04_08.h>
46#include <osmocom/gsm/protocol/gsm_08_58.h>
47
48/* TS 04.06 Figure 4 / Section 3.2 */
49#define LAPDm_LPD_NORMAL 0
50#define LAPDm_LPD_SMSCB 1
51#define LAPDm_SAPI_NORMAL 0
52#define LAPDm_SAPI_SMS 3
53#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
54
rootaf48bed2011-09-26 11:23:06 +020055#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020056#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
57#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
58#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
59
60/* TS 04.06 Table 3 / Section 3.4.3 */
61#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
62#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
63#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
64
65#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
66#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
67#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
68
69#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
70#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
71
72#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
73
74#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
75#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
76
Harald Welte1f0b8c22011-06-27 10:51:37 +020077#define LAPDm_LEN(len) ((len << 2) | 0x1)
78#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020079#define LAPDm_EL 0x1
80
81#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020082
83/* TS 04.06 Section 5.8.3 */
84#define N201_AB_SACCH 18
85#define N201_AB_SDCCH 20
86#define N201_AB_FACCH 20
87#define N201_Bbis 23
88#define N201_Bter_SACCH 21
89#define N201_Bter_SDCCH 23
90#define N201_Bter_FACCH 23
91#define N201_B4 19
92
93/* 5.8.2.1 N200 during establish and release */
94#define N200_EST_REL 5
95/* 5.8.2.1 N200 during timer recovery state */
96#define N200_TR_SACCH 5
97#define N200_TR_SDCCH 23
98#define N200_TR_FACCH_FR 34
99#define N200_TR_EFACCH_FR 48
100#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200101/* FIXME: set N200 depending on chan_nr */
102#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200103
104enum lapdm_format {
105 LAPDm_FMT_A,
106 LAPDm_FMT_B,
107 LAPDm_FMT_Bbis,
108 LAPDm_FMT_Bter,
109 LAPDm_FMT_B4,
110};
111
Maxadef12a2016-05-25 15:25:02 +0200112const struct value_string osmo_ph_prim_names[] = {
113 { PRIM_PH_DATA, "PH-DATA" },
114 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
115 { PRIM_PH_CONN, "PH-CONNECT" },
116 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
117 { PRIM_PH_RTS, "PH-RTS" },
118 { PRIM_MPH_INFO, "MPH-INFO" },
119 { PRIM_TCH, "TCH" },
120 { PRIM_TCH_RTS, "TCH-RTS" },
121 { 0, NULL }
122};
123
rootaf48bed2011-09-26 11:23:06 +0200124static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
125static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
126 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100127static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200128
129static void lapdm_dl_init(struct lapdm_datalink *dl,
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100130 struct lapdm_entity *entity, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200131{
132 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200133 dl->entity = entity;
rootaf48bed2011-09-26 11:23:06 +0200134 lapd_dl_init(&dl->dl, 1, 8, 200);
135 dl->dl.reestablish = 0; /* GSM uses no reestablish */
136 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
137 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100138 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200139 dl->dl.n200_est_rel = N200_EST_REL;
140 dl->dl.n200 = N200;
141 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100142 dl->dl.t200_sec = t200; dl->dl.t200_usec = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200143}
144
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200145/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200146 * \param[in] le LAPDm entity
147 * \param[in] mode \ref lapdm_mode (BTS/MS)
148 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100149void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200150{
151 unsigned int i;
152
153 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100154 lapdm_dl_init(&le->datalink[i], le, t200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200155
156 lapdm_entity_set_mode(le, mode);
157}
158
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200159/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200160 * \param[in] lc \ref lapdm_channel to be initialized
161 * \param[in] mode \ref lapdm_mode (BTS/MS)
162 *
163 * This really is a convenience wrapper around calling \ref
164 * lapdm_entity_init twice.
165 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200166void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
167{
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100168 lapdm_entity_init(&lc->lapdm_acch, mode, 2);
rootaf48bed2011-09-26 11:23:06 +0200169 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100170 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200171}
172
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200173/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200174void lapdm_entity_exit(struct lapdm_entity *le)
175{
176 unsigned int i;
177 struct lapdm_datalink *dl;
178
179 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
180 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200181 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200182 }
183}
184
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200185/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200186 *
187 * A convenience wrapper calling \ref lapdm_entity_exit on both
188 * entities inside the \ref lapdm_channel
189 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200190void lapdm_channel_exit(struct lapdm_channel *lc)
191{
192 lapdm_entity_exit(&lc->lapdm_acch);
193 lapdm_entity_exit(&lc->lapdm_dcch);
194}
195
Daniel Willmann55405fb2014-03-26 13:45:17 +0100196struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200197{
198 switch (sapi) {
199 case LAPDm_SAPI_NORMAL:
200 return &le->datalink[0];
201 case LAPDm_SAPI_SMS:
202 return &le->datalink[1];
203 default:
204 return NULL;
205 }
206}
207
Harald Welte1f0b8c22011-06-27 10:51:37 +0200208/* Append padding (if required) */
209static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
210{
211 int pad_len = n201 - msgb_l2len(msg);
212 uint8_t *data;
213
214 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200215 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200216 "cannot pad message that is already too big!\n");
217 return;
218 }
219
220 data = msgb_put(msg, pad_len);
221 memset(data, 0x2B, pad_len);
222}
223
224/* input function that L2 calls when sending messages up to L3 */
225static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
226{
227 if (!le->l3_cb) {
228 msgb_free(msg);
229 return -EIO;
230 }
231
232 /* call the layer2 message handler that is registered */
233 return le->l3_cb(msg, le, le->l3_ctx);
234}
235
236/* write a frame into the tx queue */
237static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200238 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200239{
240 struct lapdm_entity *le = dl->entity;
241 struct osmo_phsap_prim pp;
242
243 /* if there is a pending message, queue it */
244 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200245 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200246 *msgb_push(msg, 1) = link_id;
247 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200248 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200249 return -EBUSY;
250 }
251
252 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
253 PRIM_OP_REQUEST, msg);
254 pp.u.data.chan_nr = chan_nr;
255 pp.u.data.link_id = link_id;
256
257 /* send the frame now */
258 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200259 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200260
261 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
262}
263
264static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
265{
266 struct lapdm_datalink *dl;
267 int last = le->last_tx_dequeue;
268 int i = last, n = ARRAY_SIZE(le->datalink);
269 struct msgb *msg = NULL;
270
271 /* round-robin dequeue */
272 do {
273 /* next */
274 i = (i + 1) % n;
275 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200276 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200277 break;
278 } while (i != last);
279
280 if (msg) {
281 /* Set last dequeue position */
282 le->last_tx_dequeue = i;
283 }
284
285 return msg;
286}
287
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200288/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200289 * a osmo_phsap_prim */
290int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
291{
292 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200293 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200294
295 msg = tx_dequeue_msgb(le);
296 if (!msg)
297 return -ENODEV;
298
299 /* if we have a message, send PH-DATA.req */
300 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
301 PRIM_OP_REQUEST, msg);
302
303 /* Pull chan_nr and link_id */
304 pp->u.data.chan_nr = *msg->data;
305 msgb_pull(msg, 1);
306 pp->u.data.link_id = *msg->data;
307 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200308 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200309 msgb_pull(msg, 1);
310
311 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200312 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200313
314 return 0;
315}
316
317/* get next frame from the tx queue. because the ms has multiple datalinks,
318 * each datalink's queue is read round-robin.
319 */
320static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
321{
322 struct osmo_phsap_prim pp;
323
324 /* we may send again */
325 le->tx_pending = 0;
326
327 /* free confirm message */
328 if (msg)
329 msgb_free(msg);
330
331 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
332 /* no message in all queues */
333
334 /* If user didn't request PH-EMPTY_FRAME.req, abort */
335 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
336 return 0;
337
338 /* otherwise, send PH-EMPTY_FRAME.req */
339 osmo_prim_init(&pp.oph, SAP_GSM_PH,
340 PRIM_PH_EMPTY_FRAME,
341 PRIM_OP_REQUEST, NULL);
342 } else {
343 le->tx_pending = 1;
344 }
345
346 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
347}
348
349/* Create RSLms various RSLms messages */
350static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
351 struct msgb *msg)
352{
353 /* Add the RSL + RLL header */
354 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
355
356 /* send off the RSLms message to L3 */
357 return rslms_sendmsg(msg, mctx->dl->entity);
358}
359
360/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
361static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
362{
363 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
364 struct abis_rsl_rll_hdr *rllh;
365
366 /* Add the RSL + RLL header */
367 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
368 msgb_push(msg, 2 + 2);
369 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
370 mctx->link_id, 1);
371 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
372
373 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
374 rllh->data[1] = mctx->ta_ind;
375
376 rllh->data[2] = RSL_IE_MS_POWER;
377 rllh->data[3] = mctx->tx_power_ind;
378
379 return rslms_sendmsg(msg, mctx->dl->entity);
380}
381
382static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
383{
384 struct msgb *msg;
385
386 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
387
388 /* send off the RSLms message to L3 */
389 return rslms_sendmsg(msg, mctx->dl->entity);
390}
391
392static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
393{
394 struct msgb *msg;
395
rootaf48bed2011-09-26 11:23:06 +0200396 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200397 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200398 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
399 return rslms_sendmsg(msg, mctx->dl->entity);
400}
401
rootaf48bed2011-09-26 11:23:06 +0200402/* DLSAP L2 -> L3 (RSLms) */
403static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
404 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200405{
rootaf48bed2011-09-26 11:23:06 +0200406 struct lapd_datalink *dl = lctx->dl;
407 struct lapdm_datalink *mdl =
408 container_of(dl, struct lapdm_datalink, dl);
409 struct lapdm_msg_ctx *mctx = &mdl->mctx;
410 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200411
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200412 switch (OSMO_PRIM_HDR(&dp->oph)) {
413 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
Andreas Eversberg5977db02013-06-12 09:34:51 +0200414 if (dp->oph.msg && dp->oph.msg->len == 0) {
415 /* omit L3 info by freeing message */
416 msgb_free(dp->oph.msg);
417 dp->oph.msg = NULL;
418 }
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200419 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200420 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200421 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
422 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200423 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200424 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
425 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200426 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200427 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
428 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
429 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
430 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200431 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200432 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
433 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200434 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200435 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
436 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200437 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200438 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
439 rsl_rll_error(dp->u.error_ind.cause, mctx);
440 if (dp->oph.msg)
441 msgb_free(dp->oph.msg);
442 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200443 }
rootaf48bed2011-09-26 11:23:06 +0200444
445 if (!rll_msg) {
446 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
447 "fix!\n", dp->oph.primitive, dp->oph.operation);
448 return -EINVAL;
449 }
450
451 if (!dp->oph.msg)
452 return send_rll_simple(rll_msg, mctx);
453
454 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200455}
456
rootaf48bed2011-09-26 11:23:06 +0200457/* send a data frame to layer 1 */
458static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200459{
rootaf48bed2011-09-26 11:23:06 +0200460 uint8_t l3_len = msg->tail - msg->data;
461 struct lapd_datalink *dl = lctx->dl;
462 struct lapdm_datalink *mdl =
463 container_of(dl, struct lapdm_datalink, dl);
464 struct lapdm_msg_ctx *mctx = &mdl->mctx;
465 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200466
rootaf48bed2011-09-26 11:23:06 +0200467 /* prepend l2 header */
468 msg->l2h = msgb_push(msg, 3);
469 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
470 /* EA is set here too */
471 switch (format) {
472 case LAPD_FORM_I:
473 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
474 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200475 break;
rootaf48bed2011-09-26 11:23:06 +0200476 case LAPD_FORM_S:
477 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200478 break;
rootaf48bed2011-09-26 11:23:06 +0200479 case LAPD_FORM_U:
480 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200481 break;
482 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200483 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200484 return -EINVAL;
485 }
rootaf48bed2011-09-26 11:23:06 +0200486 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
487 if (lctx->more)
488 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200489
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100490 /* add ACCH header with last indicated tx-power and TA */
491 if ((mctx->link_id & 0x40)) {
492 struct lapdm_entity *le = mdl->entity;
493
494 msg->l2h = msgb_push(msg, 2);
495 msg->l2h[0] = le->tx_power;
496 msg->l2h[1] = le->ta;
497 }
498
rootaf48bed2011-09-26 11:23:06 +0200499 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
500 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200501}
502
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100503static int update_pending_frames(struct lapd_msg_ctx *lctx)
504{
505 struct lapd_datalink *dl = lctx->dl;
506 struct msgb *msg;
507 int rc = -1;
508
509 llist_for_each_entry(msg, &dl->tx_queue, list) {
510 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
511 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
512 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
513 rc = 0;
514 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
515 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
516 }
517 }
518
519 return rc;
520}
521
Harald Welte1f0b8c22011-06-27 10:51:37 +0200522/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200523static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
524 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200525{
526 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200527 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200528 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200529 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200530 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200531 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200532
533 /* when we reach here, we have a msgb with l2h pointing to the raw
534 * 23byte mac block. The l1h has already been purged. */
535
rootaf48bed2011-09-26 11:23:06 +0200536 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200537 mctx.chan_nr = chan_nr;
538 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200539
Harald Welte1f0b8c22011-06-27 10:51:37 +0200540 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
541 if (cbits == 0x10 || cbits == 0x12) {
542 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
543 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200544 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200545 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200546 } else {
547 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200548 /* It was received from network on SACCH */
549
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100550 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
551 if (le->mode == LAPDM_MODE_MS
552 && LAPDm_CTRL_is_U(msg->l2h[3])
553 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200554 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200555 n201 = N201_B4;
556 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200557 } else {
558 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200559 n201 = N201_AB_SACCH;
560 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200561 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200562 /* SACCH frames have a two-byte L1 header that
563 * OsmocomBB L1 doesn't strip */
564 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
565 mctx.ta_ind = msg->l2h[1];
566 msgb_pull(msg, 2);
567 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200568 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200569 } else {
570 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200571 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100572 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200573 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200574 }
575 }
576
Daniel Willmann55405fb2014-03-26 13:45:17 +0100577 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200578 /* G.2.1 No action on frames containing an unallocated SAPI. */
579 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200580 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200581 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200582 msgb_free(msg);
583 return -EIO;
584 }
585
Harald Welte1f0b8c22011-06-27 10:51:37 +0200586 switch (mctx.lapdm_fmt) {
587 case LAPDm_FMT_A:
588 case LAPDm_FMT_B:
589 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200590 lctx.dl = &mctx.dl->dl;
591 /* obtain SAPI from address field */
592 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
593 /* G.2.3 EA bit set to "0" is not allowed in GSM */
594 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
595 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
596 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200597 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200598 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200599 return -EINVAL;
600 }
rootaf48bed2011-09-26 11:23:06 +0200601 /* adress field */
602 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
603 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
604 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
605 /* command field */
606 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
607 lctx.format = LAPD_FORM_I;
608 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
609 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
610 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
611 lctx.format = LAPD_FORM_S;
612 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
613 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
614 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
615 lctx.format = LAPD_FORM_U;
616 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
617 } else
618 lctx.format = LAPD_FORM_UKN;
619 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
620 if (lctx.sapi != LAPDm_SAPI_NORMAL
621 && lctx.sapi != LAPDm_SAPI_SMS
622 && lctx.format == LAPD_FORM_U
623 && lctx.s_u == LAPDm_U_UI) {
624 /* 5.3.3 UI frames with invalid SAPI values shall be
625 * discarded
626 */
627 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
628 lctx.sapi);
629 msgb_free(msg);
630 return 0;
631 }
632 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
633 lctx.n201 = n201;
634 lctx.length = n201;
635 lctx.more = 0;
636 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100637 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200638 } else {
639 /* length field */
640 if (!(msg->l2h[2] & LAPDm_EL)) {
641 /* G.4.1 If the EL bit is set to "0", an
642 * MDL-ERROR-INDICATION primitive with cause
643 * "frame not implemented" is sent to the
644 * mobile management entity. */
645 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
646 "multi-octet length\n");
647 msgb_free(msg);
648 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
649 return -EINVAL;
650 }
651 lctx.n201 = n201;
652 lctx.length = msg->l2h[2] >> 2;
653 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
654 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100655 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200656 }
657 /* store context for messages from lapd */
658 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
659 /* send to LAPD */
660 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200661 break;
662 case LAPDm_FMT_Bter:
663 /* FIXME */
664 msgb_free(msg);
665 break;
666 case LAPDm_FMT_Bbis:
667 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200668 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200669 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100670 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200671 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
672 break;
673 default:
674 msgb_free(msg);
675 }
676
677 return rc;
678}
679
680/* input into layer2 (from layer 1) */
681static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
682{
683 struct abis_rsl_cchan_hdr *ch;
684 struct gsm48_req_ref req_ref;
685 struct gsm_time gt;
686 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
687
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200688 if (!msg)
689 return -ENOMEM;
690
Harald Welte1f0b8c22011-06-27 10:51:37 +0200691 msg->l2h = msgb_push(msg, sizeof(*ch));
692 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
693 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
694 ch->chan_nr = RSL_CHAN_RACH;
695
696 /* generate a RSL CHANNEL REQUIRED message */
697 gsm_fn2gsmtime(&gt, fn);
698 req_ref.ra = ra;
699 req_ref.t1 = gt.t1; /* FIXME: modulo? */
700 req_ref.t2 = gt.t2;
701 req_ref.t3_low = gt.t3 & 7;
702 req_ref.t3_high = gt.t3 >> 3;
703
704 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
705 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
706
707 return rslms_sendmsg(msg, le);
708}
709
710static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
711
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200712/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200713int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
714{
715 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
716 int rc = 0;
717
718 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200719 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200720 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100721 rc = -ENODEV;
722 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200723 }
724
725 switch (oph->primitive) {
726 case PRIM_PH_DATA:
727 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200728 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA 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_ind(oph->msg, le, pp->u.data.chan_nr,
734 pp->u.data.link_id);
735 break;
736 case PRIM_PH_RTS:
737 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200738 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200739 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100740 rc = -ENODEV;
741 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200742 }
743 rc = l2_ph_data_conf(oph->msg, le);
744 break;
745 case PRIM_PH_RACH:
746 switch (oph->operation) {
747 case PRIM_OP_INDICATION:
748 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
749 pp->u.rach_ind.acc_delay);
750 break;
751 case PRIM_OP_CONFIRM:
752 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
753 break;
754 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100755 rc = -EIO;
756 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200757 }
758 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100759 default:
760 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
761 oph->primitive);
762 rc = -EINVAL;
763 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200764 }
765
766 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100767
768free:
769 msgb_free(oph->msg);
770 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200771}
772
773
774/* L3 -> L2 / RSLMS -> LAPDm */
775
rootaf48bed2011-09-26 11:23:06 +0200776/* Set LAPDm context for established connection */
777static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
778 uint8_t link_id, int n201, uint8_t sapi)
779{
780 memset(&dl->mctx, 0, sizeof(dl->mctx));
781 dl->mctx.dl = dl;
782 dl->mctx.chan_nr = chan_nr;
783 dl->mctx.link_id = link_id;
784 dl->dl.lctx.dl = &dl->dl;
785 dl->dl.lctx.n201 = n201;
786 dl->dl.lctx.sapi = sapi;
787
788 return 0;
789}
790
Harald Welte1f0b8c22011-06-27 10:51:37 +0200791/* L3 requests establishment of data link */
792static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
793{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200794 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
795 uint8_t chan_nr = rllh->chan_nr;
796 uint8_t link_id = rllh->link_id;
797 uint8_t sapi = rllh->link_id & 7;
798 struct tlv_parsed tv;
799 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100800 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200801 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200802
rootaf48bed2011-09-26 11:23:06 +0200803 /* Set LAPDm context for established connection */
804 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200805
rootaf48bed2011-09-26 11:23:06 +0200806 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200807 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200808 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200809 /* contention resolution establishment procedure */
810 if (sapi != 0) {
811 /* According to clause 6, the contention resolution
812 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200813 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200814 "resolution (discarding)\n");
815 msgb_free(msg);
816 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
817 }
818 /* transmit a SABM command with the P bit set to "1". The SABM
819 * command shall contain the layer 3 message unit */
820 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200821 } else {
822 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200823 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200824 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200825 }
826
827 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100828 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200829 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100830 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200831 msgb_free(msg);
832 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
833 }
834
rootaf48bed2011-09-26 11:23:06 +0200835 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100836 msgb_pull_to_l3(msg);
837 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200838
rootaf48bed2011-09-26 11:23:06 +0200839 /* prepare prim */
840 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200841
rootaf48bed2011-09-26 11:23:06 +0200842 /* send to L2 */
843 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200844}
845
846/* L3 requests transfer of unnumbered information */
847static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
848{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100849 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200850 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
851 uint8_t chan_nr = rllh->chan_nr;
852 uint8_t link_id = rllh->link_id;
853 uint8_t sapi = link_id & 7;
854 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +0100855 int length, ui_bts;
856
857 if (!le) {
858 LOGP(DLLAPD, LOGL_ERROR, "lapdm_datalink without entity error\n");
859 msgb_free(msg);
860 return -EMLINK;
861 }
862 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200863
864 /* check if the layer3 message length exceeds N201 */
865
866 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
867
868 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100869 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200870 }
871 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100872 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200873 }
874 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200875 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200876 "error\n");
877 msgb_free(msg);
878 return -EINVAL;
879 }
rootaf48bed2011-09-26 11:23:06 +0200880 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200881 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
882 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200883 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200884 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200885 "(discarding)\n", length,
886 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200887 msgb_free(msg);
888 return -EIO;
889 }
890
rootaf48bed2011-09-26 11:23:06 +0200891 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100892 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200893
rootaf48bed2011-09-26 11:23:06 +0200894 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100895 msgb_pull_to_l3(msg);
896 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200897
898 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200899 msg->l2h = msgb_push(msg, 2 + !ui_bts);
900 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
901 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100902 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200903 msg->l2h[2] = LAPDm_LEN(length);
904 if (link_id & 0x40) {
905 msg->l2h = msgb_push(msg, 2);
906 msg->l2h[0] = le->tx_power;
907 msg->l2h[1] = le->ta;
908 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200909
910 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200911 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200912}
913
914/* L3 requests transfer of acknowledged information */
915static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
916{
917 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
918 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200919 int length;
920 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200921
922 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
923 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200924 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200925 "error\n");
926 msgb_free(msg);
927 return -EINVAL;
928 }
rootaf48bed2011-09-26 11:23:06 +0200929 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
930 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200931
rootaf48bed2011-09-26 11:23:06 +0200932 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100933 msgb_pull_to_l3(msg);
934 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200935
rootaf48bed2011-09-26 11:23:06 +0200936 /* prepare prim */
937 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200938
rootaf48bed2011-09-26 11:23:06 +0200939 /* send to L2 */
940 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200941}
942
943/* L3 requests suspension of data link */
944static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
945{
946 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
947 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200948 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200949
950 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200951 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200952 msgb_free(msg);
953 return -EINVAL;
954 }
955
rootaf48bed2011-09-26 11:23:06 +0200956 /* prepare prim */
957 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200958
rootaf48bed2011-09-26 11:23:06 +0200959 /* send to L2 */
960 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200961}
962
963/* L3 requests resume of data link */
964static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
965{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200966 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200967 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200968 uint8_t chan_nr = rllh->chan_nr;
969 uint8_t link_id = rllh->link_id;
970 uint8_t sapi = rllh->link_id & 7;
971 struct tlv_parsed tv;
972 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100973 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200974 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200975
rootaf48bed2011-09-26 11:23:06 +0200976 /* Set LAPDm context for established connection */
977 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200978
979 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
980 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200981 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200982 msgb_free(msg);
983 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
984 }
rootaf48bed2011-09-26 11:23:06 +0200985 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200986 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
987
rootaf48bed2011-09-26 11:23:06 +0200988 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100989 msgb_pull_to_l3(msg);
990 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200991
rootaf48bed2011-09-26 11:23:06 +0200992 /* prepare prim */
993 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
994 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200995
rootaf48bed2011-09-26 11:23:06 +0200996 /* send to L2 */
997 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200998}
999
1000/* L3 requests release of data link */
1001static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1002{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001003 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001004 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001005 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001006
1007 /* get release mode */
1008 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1009 mode = rllh->data[1] & 1;
1010
Harald Welte1f0b8c22011-06-27 10:51:37 +02001011 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001012 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001013
1014 /* 04.06 3.8.3: No information field is permitted with the DISC
1015 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001016 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001017
rootaf48bed2011-09-26 11:23:06 +02001018 /* prepare prim */
1019 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1020 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001021
rootaf48bed2011-09-26 11:23:06 +02001022 /* send to L2 */
1023 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001024}
1025
1026/* L3 requests channel in idle state */
1027static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1028{
1029 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1030 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1031 struct osmo_phsap_prim pp;
1032
1033 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1034 PRIM_OP_REQUEST, NULL);
1035
1036 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001037 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001038 return -EINVAL;
1039 }
1040 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001041 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001042 return -EINVAL;
1043 }
1044 pp.u.rach_req.ra = cch->data[1];
1045 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1046 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1047
1048 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001049 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001050 return -EINVAL;
1051 }
1052 /* TA = 0 - delay */
1053 pp.u.rach_req.ta = 0 - cch->data[5];
1054
1055 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001056 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001057 return -EINVAL;
1058 }
1059 pp.u.rach_req.tx_power = cch->data[7];
1060
1061 msgb_free(msg);
1062
1063 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1064}
1065
1066/* L1 confirms channel request */
1067static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1068{
1069 struct abis_rsl_cchan_hdr *ch;
1070 struct gsm_time tm;
1071 struct gsm48_req_ref *ref;
1072
1073 gsm_fn2gsmtime(&tm, frame_nr);
1074
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001075 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001076 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1077 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1078 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1079 ch->chan_nr = RSL_CHAN_RACH;
1080 ch->data[0] = RSL_IE_REQ_REFERENCE;
1081 ref = (struct gsm48_req_ref *) (ch->data + 1);
1082 ref->t1 = tm.t1;
1083 ref->t2 = tm.t2;
1084 ref->t3_low = tm.t3 & 0x7;
1085 ref->t3_high = tm.t3 >> 3;
1086
1087 return rslms_sendmsg(msg, le);
1088}
1089
Harald Welte1f0b8c22011-06-27 10:51:37 +02001090/* incoming RSLms RLL message from L3 */
1091static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1092{
1093 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1094 int msg_type = rllh->c.msg_type;
1095 uint8_t sapi = rllh->link_id & 7;
1096 struct lapdm_entity *le;
1097 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001098 int rc = 0;
1099
1100 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001101 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001102 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001103 return -EINVAL;
1104 }
1105
1106 if (rllh->link_id & 0x40)
1107 le = &lc->lapdm_acch;
1108 else
1109 le = &lc->lapdm_dcch;
1110
Harald Welte1a87c1b2015-12-14 15:26:07 +01001111 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1112 * BTS side */
1113 if (le->mode == LAPDM_MODE_BTS) {
1114 switch (msg_type) {
1115 case RSL_MT_SUSP_REQ:
1116 case RSL_MT_RES_REQ:
1117 case RSL_MT_RECON_REQ:
1118 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1119 lc->name, rsl_msg_name(msg_type));
1120 msgb_free(msg);
1121 return -EINVAL;
1122 break;
1123 default:
1124 break;
1125 }
1126 }
1127
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001128 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001129 * SAPI.
1130 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001131 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001132 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001133 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001134 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001135 return -EINVAL;
1136 }
1137
Daniel Willmanne5233922012-12-25 23:15:50 +01001138 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001139 case RSL_MT_DATA_REQ:
1140 case RSL_MT_SUSP_REQ:
1141 case RSL_MT_REL_REQ:
1142 /* This is triggered in abnormal error conditions where
1143 * set_lapdm_context() was not called for the channel earlier. */
1144 if (!dl->dl.lctx.dl) {
1145 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1146 lc->name, rsl_msg_name(msg_type), sapi);
1147 msgb_free(msg);
1148 return -EINVAL;
1149 }
1150 break;
1151 default:
1152 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1153 lc->name, rsl_msg_name(msg_type), sapi);
1154 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001155
rootaf48bed2011-09-26 11:23:06 +02001156 switch (msg_type) {
1157 case RSL_MT_UNIT_DATA_REQ:
1158 rc = rslms_rx_rll_udata_req(msg, dl);
1159 break;
1160 case RSL_MT_EST_REQ:
1161 rc = rslms_rx_rll_est_req(msg, dl);
1162 break;
1163 case RSL_MT_DATA_REQ:
1164 rc = rslms_rx_rll_data_req(msg, dl);
1165 break;
1166 case RSL_MT_SUSP_REQ:
1167 rc = rslms_rx_rll_susp_req(msg, dl);
1168 break;
1169 case RSL_MT_RES_REQ:
1170 rc = rslms_rx_rll_res_req(msg, dl);
1171 break;
1172 case RSL_MT_RECON_REQ:
1173 rc = rslms_rx_rll_res_req(msg, dl);
1174 break;
1175 case RSL_MT_REL_REQ:
1176 rc = rslms_rx_rll_rel_req(msg, dl);
1177 break;
1178 default:
1179 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001180 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001181 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001182 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001183
1184 return rc;
1185}
1186
1187/* incoming RSLms COMMON CHANNEL message from L3 */
1188static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1189{
1190 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1191 int msg_type = cch->c.msg_type;
1192 int rc = 0;
1193
1194 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001195 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001196 return -EINVAL;
1197 }
1198
1199 switch (msg_type) {
1200 case RSL_MT_CHAN_RQD:
1201 /* create and send RACH request */
1202 rc = rslms_rx_chan_rqd(lc, msg);
1203 break;
1204 default:
rootaf48bed2011-09-26 11:23:06 +02001205 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001206 msg_type);
1207 msgb_free(msg);
1208 return 0;
1209 }
1210
1211 return rc;
1212}
1213
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001214/*! Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001215int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1216{
1217 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1218 int rc = 0;
1219
1220 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001221 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001222 return -EINVAL;
1223 }
1224
1225 switch (rslh->msg_discr & 0xfe) {
1226 case ABIS_RSL_MDISC_RLL:
1227 rc = rslms_rx_rll(msg, lc);
1228 break;
1229 case ABIS_RSL_MDISC_COM_CHAN:
1230 rc = rslms_rx_com_chan(msg, lc);
1231 break;
1232 default:
rootaf48bed2011-09-26 11:23:06 +02001233 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001234 "discriminator 0x%02x", rslh->msg_discr);
1235 msgb_free(msg);
1236 return -EINVAL;
1237 }
1238
1239 return rc;
1240}
1241
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001242/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001243int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1244{
rootaf48bed2011-09-26 11:23:06 +02001245 int i;
1246 enum lapd_mode lm;
1247
Harald Welte1f0b8c22011-06-27 10:51:37 +02001248 switch (mode) {
1249 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001250 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001251 break;
1252 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001253 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001254 break;
1255 default:
1256 return -EINVAL;
1257 }
1258
rootaf48bed2011-09-26 11:23:06 +02001259 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1260 lapd_set_mode(&le->datalink[i].dl, lm);
1261 }
1262
Harald Welte1f0b8c22011-06-27 10:51:37 +02001263 le->mode = mode;
1264
1265 return 0;
1266}
1267
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001268/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001269int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1270{
1271 int rc;
1272
1273 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1274 if (rc < 0)
1275 return rc;
1276
1277 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1278}
1279
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001280/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001281void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1282{
1283 lc->lapdm_dcch.l1_prim_cb = cb;
1284 lc->lapdm_acch.l1_prim_cb = cb;
1285 lc->lapdm_dcch.l1_ctx = ctx;
1286 lc->lapdm_acch.l1_ctx = ctx;
1287}
1288
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001289/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001290void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1291{
1292 lc->lapdm_dcch.l3_cb = cb;
1293 lc->lapdm_acch.l3_cb = cb;
1294 lc->lapdm_dcch.l3_ctx = ctx;
1295 lc->lapdm_acch.l3_ctx = ctx;
1296}
1297
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001298/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001299void lapdm_entity_reset(struct lapdm_entity *le)
1300{
1301 struct lapdm_datalink *dl;
1302 int i;
1303
1304 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1305 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001306 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001307 }
1308}
1309
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001310/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001311void lapdm_channel_reset(struct lapdm_channel *lc)
1312{
1313 lapdm_entity_reset(&lc->lapdm_dcch);
1314 lapdm_entity_reset(&lc->lapdm_acch);
1315}
1316
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001317/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001318void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1319{
1320 le->flags = flags;
1321}
1322
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001323/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001324void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1325{
1326 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1327 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1328}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001329
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001330/*! @} */