blob: 7ca2b3e36f92ac3c72044178e4521e88857f124f [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file lapdm.c
2 * GSM LAPDm (TS 04.06) implementation. */
3/*
Harald Weltee08da972017-11-13 01:00:26 +09004 * (C) 2010-2017 by Harald Welte <laforge@gnumonks.org>
rootaf48bed2011-09-26 11:23:06 +02005 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
Harald Weltee08da972017-11-13 01:00:26 +09006 * (C) 2014-2016 by sysmocom - s.f.m.c GmbH
Harald Welte1f0b8c22011-06-27 10:51:37 +02007 *
8 * All Rights Reserved
9 *
Harald Weltee08da972017-11-13 01:00:26 +090010 * SPDX-License-Identifier: GPL-2.0+
11 *
Harald Welte1f0b8c22011-06-27 10:51:37 +020012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 */
27
Harald Welte6bdf0b12011-08-17 18:22:08 +020028/*! \addtogroup lapdm
29 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020030 * \file lapdm.c */
Harald Welte6bdf0b12011-08-17 18:22:08 +020031
Harald Welte1f0b8c22011-06-27 10:51:37 +020032#include <stdio.h>
33#include <stdint.h>
34#include <string.h>
35#include <errno.h>
Harald Welte1f0b8c22011-06-27 10:51:37 +020036
37#include <osmocom/core/logging.h>
38#include <osmocom/core/timer.h>
39#include <osmocom/core/msgb.h>
40#include <osmocom/core/utils.h>
41
42#include <osmocom/gsm/tlv.h>
43#include <osmocom/gsm/rsl.h>
44#include <osmocom/gsm/prim.h>
45#include <osmocom/gsm/gsm_utils.h>
46#include <osmocom/gsm/lapdm.h>
47
48#include <osmocom/gsm/protocol/gsm_04_08.h>
49#include <osmocom/gsm/protocol/gsm_08_58.h>
50
51/* TS 04.06 Figure 4 / Section 3.2 */
52#define LAPDm_LPD_NORMAL 0
53#define LAPDm_LPD_SMSCB 1
54#define LAPDm_SAPI_NORMAL 0
55#define LAPDm_SAPI_SMS 3
56#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
57
rootaf48bed2011-09-26 11:23:06 +020058#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020059#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
60#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
61#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
62
63/* TS 04.06 Table 3 / Section 3.4.3 */
64#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
65#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
66#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
67
68#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
69#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
70#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
71
72#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
73#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
74
75#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
76
77#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
78#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
79
Harald Welte1f0b8c22011-06-27 10:51:37 +020080#define LAPDm_LEN(len) ((len << 2) | 0x1)
81#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020082#define LAPDm_EL 0x1
83
84#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020085
86/* TS 04.06 Section 5.8.3 */
87#define N201_AB_SACCH 18
88#define N201_AB_SDCCH 20
89#define N201_AB_FACCH 20
90#define N201_Bbis 23
91#define N201_Bter_SACCH 21
92#define N201_Bter_SDCCH 23
93#define N201_Bter_FACCH 23
94#define N201_B4 19
95
96/* 5.8.2.1 N200 during establish and release */
97#define N200_EST_REL 5
98/* 5.8.2.1 N200 during timer recovery state */
99#define N200_TR_SACCH 5
100#define N200_TR_SDCCH 23
101#define N200_TR_FACCH_FR 34
102#define N200_TR_EFACCH_FR 48
103#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200104/* FIXME: set N200 depending on chan_nr */
105#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200106
107enum lapdm_format {
108 LAPDm_FMT_A,
109 LAPDm_FMT_B,
110 LAPDm_FMT_Bbis,
111 LAPDm_FMT_Bter,
112 LAPDm_FMT_B4,
113};
114
Maxadef12a2016-05-25 15:25:02 +0200115const struct value_string osmo_ph_prim_names[] = {
116 { PRIM_PH_DATA, "PH-DATA" },
117 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
118 { PRIM_PH_CONN, "PH-CONNECT" },
119 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
120 { PRIM_PH_RTS, "PH-RTS" },
121 { PRIM_MPH_INFO, "MPH-INFO" },
122 { PRIM_TCH, "TCH" },
123 { PRIM_TCH_RTS, "TCH-RTS" },
124 { 0, NULL }
125};
126
rootaf48bed2011-09-26 11:23:06 +0200127static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
128static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
129 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100130static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200131
132static void lapdm_dl_init(struct lapdm_datalink *dl,
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100133 struct lapdm_entity *entity, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200134{
135 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200136 dl->entity = entity;
rootaf48bed2011-09-26 11:23:06 +0200137 lapd_dl_init(&dl->dl, 1, 8, 200);
138 dl->dl.reestablish = 0; /* GSM uses no reestablish */
139 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
140 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100141 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200142 dl->dl.n200_est_rel = N200_EST_REL;
143 dl->dl.n200 = N200;
144 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100145 dl->dl.t200_sec = t200; dl->dl.t200_usec = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200146}
147
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200148/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200149 * \param[in] le LAPDm entity
150 * \param[in] mode \ref lapdm_mode (BTS/MS)
151 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100152void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200153{
154 unsigned int i;
155
156 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100157 lapdm_dl_init(&le->datalink[i], le, t200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200158
159 lapdm_entity_set_mode(le, mode);
160}
161
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200162/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200163 * \param[in] lc \ref lapdm_channel to be initialized
164 * \param[in] mode \ref lapdm_mode (BTS/MS)
165 *
166 * This really is a convenience wrapper around calling \ref
167 * lapdm_entity_init twice.
168 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200169void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
170{
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100171 lapdm_entity_init(&lc->lapdm_acch, mode, 2);
rootaf48bed2011-09-26 11:23:06 +0200172 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100173 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200174}
175
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200176/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200177void lapdm_entity_exit(struct lapdm_entity *le)
178{
179 unsigned int i;
180 struct lapdm_datalink *dl;
181
182 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
183 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200184 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200185 }
186}
187
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200188/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200189 *
190 * A convenience wrapper calling \ref lapdm_entity_exit on both
191 * entities inside the \ref lapdm_channel
192 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200193void lapdm_channel_exit(struct lapdm_channel *lc)
194{
195 lapdm_entity_exit(&lc->lapdm_acch);
196 lapdm_entity_exit(&lc->lapdm_dcch);
197}
198
Daniel Willmann55405fb2014-03-26 13:45:17 +0100199struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200200{
201 switch (sapi) {
202 case LAPDm_SAPI_NORMAL:
203 return &le->datalink[0];
204 case LAPDm_SAPI_SMS:
205 return &le->datalink[1];
206 default:
207 return NULL;
208 }
209}
210
Harald Welte1f0b8c22011-06-27 10:51:37 +0200211/* Append padding (if required) */
212static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
213{
214 int pad_len = n201 - msgb_l2len(msg);
215 uint8_t *data;
216
217 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200218 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200219 "cannot pad message that is already too big!\n");
220 return;
221 }
222
223 data = msgb_put(msg, pad_len);
224 memset(data, 0x2B, pad_len);
225}
226
227/* input function that L2 calls when sending messages up to L3 */
228static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
229{
230 if (!le->l3_cb) {
231 msgb_free(msg);
232 return -EIO;
233 }
234
235 /* call the layer2 message handler that is registered */
236 return le->l3_cb(msg, le, le->l3_ctx);
237}
238
239/* write a frame into the tx queue */
240static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200241 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200242{
243 struct lapdm_entity *le = dl->entity;
244 struct osmo_phsap_prim pp;
245
246 /* if there is a pending message, queue it */
247 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200248 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200249 *msgb_push(msg, 1) = link_id;
250 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200251 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200252 return -EBUSY;
253 }
254
255 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
256 PRIM_OP_REQUEST, msg);
257 pp.u.data.chan_nr = chan_nr;
258 pp.u.data.link_id = link_id;
259
260 /* send the frame now */
261 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200262 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200263
264 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
265}
266
267static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
268{
269 struct lapdm_datalink *dl;
270 int last = le->last_tx_dequeue;
271 int i = last, n = ARRAY_SIZE(le->datalink);
272 struct msgb *msg = NULL;
273
274 /* round-robin dequeue */
275 do {
276 /* next */
277 i = (i + 1) % n;
278 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200279 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200280 break;
281 } while (i != last);
282
283 if (msg) {
284 /* Set last dequeue position */
285 le->last_tx_dequeue = i;
286 }
287
288 return msg;
289}
290
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200291/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200292 * a osmo_phsap_prim */
293int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
294{
295 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200296 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200297
298 msg = tx_dequeue_msgb(le);
299 if (!msg)
300 return -ENODEV;
301
302 /* if we have a message, send PH-DATA.req */
303 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
304 PRIM_OP_REQUEST, msg);
305
306 /* Pull chan_nr and link_id */
307 pp->u.data.chan_nr = *msg->data;
308 msgb_pull(msg, 1);
309 pp->u.data.link_id = *msg->data;
310 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200311 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200312 msgb_pull(msg, 1);
313
314 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200315 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200316
317 return 0;
318}
319
320/* get next frame from the tx queue. because the ms has multiple datalinks,
321 * each datalink's queue is read round-robin.
322 */
323static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
324{
325 struct osmo_phsap_prim pp;
326
327 /* we may send again */
328 le->tx_pending = 0;
329
330 /* free confirm message */
331 if (msg)
332 msgb_free(msg);
333
334 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
335 /* no message in all queues */
336
337 /* If user didn't request PH-EMPTY_FRAME.req, abort */
338 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
339 return 0;
340
341 /* otherwise, send PH-EMPTY_FRAME.req */
342 osmo_prim_init(&pp.oph, SAP_GSM_PH,
343 PRIM_PH_EMPTY_FRAME,
344 PRIM_OP_REQUEST, NULL);
345 } else {
346 le->tx_pending = 1;
347 }
348
349 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
350}
351
352/* Create RSLms various RSLms messages */
353static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
354 struct msgb *msg)
355{
356 /* Add the RSL + RLL header */
357 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
358
359 /* send off the RSLms message to L3 */
360 return rslms_sendmsg(msg, mctx->dl->entity);
361}
362
363/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
364static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
365{
366 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
367 struct abis_rsl_rll_hdr *rllh;
368
369 /* Add the RSL + RLL header */
370 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
371 msgb_push(msg, 2 + 2);
372 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
373 mctx->link_id, 1);
374 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
375
376 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
377 rllh->data[1] = mctx->ta_ind;
378
379 rllh->data[2] = RSL_IE_MS_POWER;
380 rllh->data[3] = mctx->tx_power_ind;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100381
Harald Welte1f0b8c22011-06-27 10:51:37 +0200382 return rslms_sendmsg(msg, mctx->dl->entity);
383}
384
385static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
386{
387 struct msgb *msg;
388
389 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
390
391 /* send off the RSLms message to L3 */
392 return rslms_sendmsg(msg, mctx->dl->entity);
393}
394
395static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
396{
397 struct msgb *msg;
398
rootaf48bed2011-09-26 11:23:06 +0200399 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200400 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200401 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
402 return rslms_sendmsg(msg, mctx->dl->entity);
403}
404
rootaf48bed2011-09-26 11:23:06 +0200405/* DLSAP L2 -> L3 (RSLms) */
406static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
407 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200408{
rootaf48bed2011-09-26 11:23:06 +0200409 struct lapd_datalink *dl = lctx->dl;
410 struct lapdm_datalink *mdl =
411 container_of(dl, struct lapdm_datalink, dl);
412 struct lapdm_msg_ctx *mctx = &mdl->mctx;
413 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200414
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200415 switch (OSMO_PRIM_HDR(&dp->oph)) {
416 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
Andreas Eversberg5977db02013-06-12 09:34:51 +0200417 if (dp->oph.msg && dp->oph.msg->len == 0) {
418 /* omit L3 info by freeing message */
419 msgb_free(dp->oph.msg);
420 dp->oph.msg = NULL;
421 }
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200422 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200423 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200424 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
425 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200426 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200427 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
428 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200429 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200430 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
431 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
432 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
433 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200434 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200435 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
436 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200437 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200438 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
439 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200440 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200441 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
442 rsl_rll_error(dp->u.error_ind.cause, mctx);
443 if (dp->oph.msg)
444 msgb_free(dp->oph.msg);
445 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200446 }
rootaf48bed2011-09-26 11:23:06 +0200447
448 if (!rll_msg) {
449 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
450 "fix!\n", dp->oph.primitive, dp->oph.operation);
451 return -EINVAL;
452 }
453
454 if (!dp->oph.msg)
455 return send_rll_simple(rll_msg, mctx);
456
457 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200458}
459
rootaf48bed2011-09-26 11:23:06 +0200460/* send a data frame to layer 1 */
461static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200462{
rootaf48bed2011-09-26 11:23:06 +0200463 uint8_t l3_len = msg->tail - msg->data;
464 struct lapd_datalink *dl = lctx->dl;
465 struct lapdm_datalink *mdl =
466 container_of(dl, struct lapdm_datalink, dl);
467 struct lapdm_msg_ctx *mctx = &mdl->mctx;
468 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200469
rootaf48bed2011-09-26 11:23:06 +0200470 /* prepend l2 header */
471 msg->l2h = msgb_push(msg, 3);
472 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
473 /* EA is set here too */
474 switch (format) {
475 case LAPD_FORM_I:
476 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
477 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200478 break;
rootaf48bed2011-09-26 11:23:06 +0200479 case LAPD_FORM_S:
480 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200481 break;
rootaf48bed2011-09-26 11:23:06 +0200482 case LAPD_FORM_U:
483 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200484 break;
485 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200486 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200487 return -EINVAL;
488 }
rootaf48bed2011-09-26 11:23:06 +0200489 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
490 if (lctx->more)
491 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200492
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100493 /* add ACCH header with last indicated tx-power and TA */
494 if ((mctx->link_id & 0x40)) {
495 struct lapdm_entity *le = mdl->entity;
496
497 msg->l2h = msgb_push(msg, 2);
498 msg->l2h[0] = le->tx_power;
499 msg->l2h[1] = le->ta;
500 }
501
rootaf48bed2011-09-26 11:23:06 +0200502 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
503 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200504}
505
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100506static int update_pending_frames(struct lapd_msg_ctx *lctx)
507{
508 struct lapd_datalink *dl = lctx->dl;
509 struct msgb *msg;
510 int rc = -1;
511
512 llist_for_each_entry(msg, &dl->tx_queue, list) {
513 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
514 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
515 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
516 rc = 0;
517 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
518 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
519 }
520 }
521
522 return rc;
523}
524
Harald Welte1f0b8c22011-06-27 10:51:37 +0200525/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200526static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
527 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200528{
529 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200530 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200531 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200532 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200533 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200534 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200535
536 /* when we reach here, we have a msgb with l2h pointing to the raw
537 * 23byte mac block. The l1h has already been purged. */
538
rootaf48bed2011-09-26 11:23:06 +0200539 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200540 mctx.chan_nr = chan_nr;
541 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200542
Harald Welte1f0b8c22011-06-27 10:51:37 +0200543 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
544 if (cbits == 0x10 || cbits == 0x12) {
545 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
546 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200547 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200548 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200549 } else {
550 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200551 /* It was received from network on SACCH */
552
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100553 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
554 if (le->mode == LAPDM_MODE_MS
555 && LAPDm_CTRL_is_U(msg->l2h[3])
556 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200557 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200558 n201 = N201_B4;
559 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200560 } else {
561 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200562 n201 = N201_AB_SACCH;
563 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200564 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200565 /* SACCH frames have a two-byte L1 header that
566 * OsmocomBB L1 doesn't strip */
567 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
568 mctx.ta_ind = msg->l2h[1];
569 msgb_pull(msg, 2);
570 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200571 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200572 } else {
573 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200574 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100575 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200576 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200577 }
578 }
579
Daniel Willmann55405fb2014-03-26 13:45:17 +0100580 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200581 /* G.2.1 No action on frames containing an unallocated SAPI. */
582 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200583 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200584 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200585 msgb_free(msg);
586 return -EIO;
587 }
588
Harald Welte1f0b8c22011-06-27 10:51:37 +0200589 switch (mctx.lapdm_fmt) {
590 case LAPDm_FMT_A:
591 case LAPDm_FMT_B:
592 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200593 lctx.dl = &mctx.dl->dl;
594 /* obtain SAPI from address field */
595 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
596 /* G.2.3 EA bit set to "0" is not allowed in GSM */
597 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
598 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
599 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200600 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200601 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200602 return -EINVAL;
603 }
rootaf48bed2011-09-26 11:23:06 +0200604 /* adress field */
605 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
606 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
607 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
608 /* command field */
609 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
610 lctx.format = LAPD_FORM_I;
611 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
612 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
613 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
614 lctx.format = LAPD_FORM_S;
615 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
616 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
617 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
618 lctx.format = LAPD_FORM_U;
619 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
620 } else
621 lctx.format = LAPD_FORM_UKN;
622 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
623 if (lctx.sapi != LAPDm_SAPI_NORMAL
624 && lctx.sapi != LAPDm_SAPI_SMS
625 && lctx.format == LAPD_FORM_U
626 && lctx.s_u == LAPDm_U_UI) {
627 /* 5.3.3 UI frames with invalid SAPI values shall be
628 * discarded
629 */
630 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
631 lctx.sapi);
632 msgb_free(msg);
633 return 0;
634 }
635 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
636 lctx.n201 = n201;
637 lctx.length = n201;
638 lctx.more = 0;
639 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100640 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200641 } else {
642 /* length field */
643 if (!(msg->l2h[2] & LAPDm_EL)) {
644 /* G.4.1 If the EL bit is set to "0", an
645 * MDL-ERROR-INDICATION primitive with cause
646 * "frame not implemented" is sent to the
647 * mobile management entity. */
648 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
649 "multi-octet length\n");
650 msgb_free(msg);
651 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
652 return -EINVAL;
653 }
654 lctx.n201 = n201;
655 lctx.length = msg->l2h[2] >> 2;
656 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
657 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100658 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200659 }
660 /* store context for messages from lapd */
661 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
662 /* send to LAPD */
663 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200664 break;
665 case LAPDm_FMT_Bter:
666 /* FIXME */
667 msgb_free(msg);
668 break;
669 case LAPDm_FMT_Bbis:
670 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200671 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200672 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100673 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200674 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
675 break;
676 default:
677 msgb_free(msg);
678 }
679
680 return rc;
681}
682
683/* input into layer2 (from layer 1) */
684static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
685{
686 struct abis_rsl_cchan_hdr *ch;
687 struct gsm48_req_ref req_ref;
688 struct gsm_time gt;
689 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
690
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200691 if (!msg)
692 return -ENOMEM;
693
Harald Welte1f0b8c22011-06-27 10:51:37 +0200694 msg->l2h = msgb_push(msg, sizeof(*ch));
695 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
696 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
697 ch->chan_nr = RSL_CHAN_RACH;
698
699 /* generate a RSL CHANNEL REQUIRED message */
700 gsm_fn2gsmtime(&gt, fn);
701 req_ref.ra = ra;
702 req_ref.t1 = gt.t1; /* FIXME: modulo? */
703 req_ref.t2 = gt.t2;
704 req_ref.t3_low = gt.t3 & 7;
705 req_ref.t3_high = gt.t3 >> 3;
706
707 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
708 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
709
710 return rslms_sendmsg(msg, le);
711}
712
713static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
714
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200715/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200716int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
717{
718 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
719 int rc = 0;
720
721 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200722 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200723 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100724 rc = -ENODEV;
725 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200726 }
727
728 switch (oph->primitive) {
729 case PRIM_PH_DATA:
730 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200731 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200732 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100733 rc = -ENODEV;
734 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200735 }
736 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
737 pp->u.data.link_id);
738 break;
739 case PRIM_PH_RTS:
740 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200741 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200742 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100743 rc = -ENODEV;
744 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200745 }
746 rc = l2_ph_data_conf(oph->msg, le);
747 break;
748 case PRIM_PH_RACH:
749 switch (oph->operation) {
750 case PRIM_OP_INDICATION:
751 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
752 pp->u.rach_ind.acc_delay);
753 break;
754 case PRIM_OP_CONFIRM:
755 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
756 break;
757 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100758 rc = -EIO;
759 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200760 }
761 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100762 default:
763 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
764 oph->primitive);
765 rc = -EINVAL;
766 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200767 }
768
769 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100770
771free:
772 msgb_free(oph->msg);
773 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200774}
775
776
777/* L3 -> L2 / RSLMS -> LAPDm */
778
rootaf48bed2011-09-26 11:23:06 +0200779/* Set LAPDm context for established connection */
780static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
781 uint8_t link_id, int n201, uint8_t sapi)
782{
783 memset(&dl->mctx, 0, sizeof(dl->mctx));
784 dl->mctx.dl = dl;
785 dl->mctx.chan_nr = chan_nr;
786 dl->mctx.link_id = link_id;
787 dl->dl.lctx.dl = &dl->dl;
788 dl->dl.lctx.n201 = n201;
789 dl->dl.lctx.sapi = sapi;
790
791 return 0;
792}
793
Harald Welte1f0b8c22011-06-27 10:51:37 +0200794/* L3 requests establishment of data link */
795static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
796{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200797 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
798 uint8_t chan_nr = rllh->chan_nr;
799 uint8_t link_id = rllh->link_id;
800 uint8_t sapi = rllh->link_id & 7;
801 struct tlv_parsed tv;
802 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100803 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200804 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200805
rootaf48bed2011-09-26 11:23:06 +0200806 /* Set LAPDm context for established connection */
807 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200808
rootaf48bed2011-09-26 11:23:06 +0200809 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200810 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200811 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200812 /* contention resolution establishment procedure */
813 if (sapi != 0) {
814 /* According to clause 6, the contention resolution
815 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200816 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200817 "resolution (discarding)\n");
818 msgb_free(msg);
819 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
820 }
821 /* transmit a SABM command with the P bit set to "1". The SABM
822 * command shall contain the layer 3 message unit */
823 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200824 } else {
825 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200826 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200827 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200828 }
829
830 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100831 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200832 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100833 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200834 msgb_free(msg);
835 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
836 }
837
rootaf48bed2011-09-26 11:23:06 +0200838 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100839 msgb_pull_to_l3(msg);
840 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200841
rootaf48bed2011-09-26 11:23:06 +0200842 /* prepare prim */
843 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200844
rootaf48bed2011-09-26 11:23:06 +0200845 /* send to L2 */
846 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200847}
848
849/* L3 requests transfer of unnumbered information */
850static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
851{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100852 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200853 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
854 uint8_t chan_nr = rllh->chan_nr;
855 uint8_t link_id = rllh->link_id;
856 uint8_t sapi = link_id & 7;
857 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +0100858 int length, ui_bts;
859
860 if (!le) {
861 LOGP(DLLAPD, LOGL_ERROR, "lapdm_datalink without entity error\n");
862 msgb_free(msg);
863 return -EMLINK;
864 }
865 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200866
867 /* check if the layer3 message length exceeds N201 */
868
869 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
870
871 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100872 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200873 }
874 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100875 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200876 }
877 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200878 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200879 "error\n");
880 msgb_free(msg);
881 return -EINVAL;
882 }
rootaf48bed2011-09-26 11:23:06 +0200883 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200884 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
885 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200886 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200887 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200888 "(discarding)\n", length,
889 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200890 msgb_free(msg);
891 return -EIO;
892 }
893
rootaf48bed2011-09-26 11:23:06 +0200894 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100895 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200896
rootaf48bed2011-09-26 11:23:06 +0200897 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100898 msgb_pull_to_l3(msg);
899 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200900
901 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200902 msg->l2h = msgb_push(msg, 2 + !ui_bts);
903 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
904 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100905 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200906 msg->l2h[2] = LAPDm_LEN(length);
907 if (link_id & 0x40) {
908 msg->l2h = msgb_push(msg, 2);
909 msg->l2h[0] = le->tx_power;
910 msg->l2h[1] = le->ta;
911 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200912
913 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200914 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200915}
916
917/* L3 requests transfer of acknowledged information */
918static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
919{
920 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
921 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200922 int length;
923 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200924
925 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
926 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200927 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200928 "error\n");
929 msgb_free(msg);
930 return -EINVAL;
931 }
rootaf48bed2011-09-26 11:23:06 +0200932 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
933 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200934
rootaf48bed2011-09-26 11:23:06 +0200935 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100936 msgb_pull_to_l3(msg);
937 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200938
rootaf48bed2011-09-26 11:23:06 +0200939 /* prepare prim */
940 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200941
rootaf48bed2011-09-26 11:23:06 +0200942 /* send to L2 */
943 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200944}
945
946/* L3 requests suspension of data link */
947static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
948{
949 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
950 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200951 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200952
953 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200954 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200955 msgb_free(msg);
956 return -EINVAL;
957 }
958
rootaf48bed2011-09-26 11:23:06 +0200959 /* prepare prim */
960 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200961
rootaf48bed2011-09-26 11:23:06 +0200962 /* send to L2 */
963 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200964}
965
966/* L3 requests resume of data link */
967static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
968{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200969 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200970 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200971 uint8_t chan_nr = rllh->chan_nr;
972 uint8_t link_id = rllh->link_id;
973 uint8_t sapi = rllh->link_id & 7;
974 struct tlv_parsed tv;
975 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100976 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200977 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200978
rootaf48bed2011-09-26 11:23:06 +0200979 /* Set LAPDm context for established connection */
980 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200981
982 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
983 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200984 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200985 msgb_free(msg);
986 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
987 }
rootaf48bed2011-09-26 11:23:06 +0200988 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200989 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
990
rootaf48bed2011-09-26 11:23:06 +0200991 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100992 msgb_pull_to_l3(msg);
993 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200994
rootaf48bed2011-09-26 11:23:06 +0200995 /* prepare prim */
996 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
997 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200998
rootaf48bed2011-09-26 11:23:06 +0200999 /* send to L2 */
1000 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001001}
1002
1003/* L3 requests release of data link */
1004static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1005{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001006 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001007 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001008 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001009
1010 /* get release mode */
1011 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1012 mode = rllh->data[1] & 1;
1013
Harald Welte1f0b8c22011-06-27 10:51:37 +02001014 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001015 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001016
1017 /* 04.06 3.8.3: No information field is permitted with the DISC
1018 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001019 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001020
rootaf48bed2011-09-26 11:23:06 +02001021 /* prepare prim */
1022 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1023 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001024
rootaf48bed2011-09-26 11:23:06 +02001025 /* send to L2 */
1026 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001027}
1028
1029/* L3 requests channel in idle state */
1030static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1031{
1032 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1033 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1034 struct osmo_phsap_prim pp;
1035
1036 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1037 PRIM_OP_REQUEST, NULL);
1038
1039 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001040 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001041 return -EINVAL;
1042 }
1043 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001044 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001045 return -EINVAL;
1046 }
1047 pp.u.rach_req.ra = cch->data[1];
1048 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1049 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1050
1051 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001052 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001053 return -EINVAL;
1054 }
1055 /* TA = 0 - delay */
1056 pp.u.rach_req.ta = 0 - cch->data[5];
1057
1058 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001059 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001060 return -EINVAL;
1061 }
1062 pp.u.rach_req.tx_power = cch->data[7];
1063
1064 msgb_free(msg);
1065
1066 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1067}
1068
1069/* L1 confirms channel request */
1070static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1071{
1072 struct abis_rsl_cchan_hdr *ch;
1073 struct gsm_time tm;
1074 struct gsm48_req_ref *ref;
1075
1076 gsm_fn2gsmtime(&tm, frame_nr);
1077
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001078 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001079 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1080 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1081 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1082 ch->chan_nr = RSL_CHAN_RACH;
1083 ch->data[0] = RSL_IE_REQ_REFERENCE;
1084 ref = (struct gsm48_req_ref *) (ch->data + 1);
1085 ref->t1 = tm.t1;
1086 ref->t2 = tm.t2;
1087 ref->t3_low = tm.t3 & 0x7;
1088 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001089
Harald Welte1f0b8c22011-06-27 10:51:37 +02001090 return rslms_sendmsg(msg, le);
1091}
1092
Harald Welte1f0b8c22011-06-27 10:51:37 +02001093/* incoming RSLms RLL message from L3 */
1094static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1095{
1096 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1097 int msg_type = rllh->c.msg_type;
1098 uint8_t sapi = rllh->link_id & 7;
1099 struct lapdm_entity *le;
1100 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001101 int rc = 0;
1102
1103 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001104 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001105 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001106 return -EINVAL;
1107 }
1108
1109 if (rllh->link_id & 0x40)
1110 le = &lc->lapdm_acch;
1111 else
1112 le = &lc->lapdm_dcch;
1113
Harald Welte1a87c1b2015-12-14 15:26:07 +01001114 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1115 * BTS side */
1116 if (le->mode == LAPDM_MODE_BTS) {
1117 switch (msg_type) {
1118 case RSL_MT_SUSP_REQ:
1119 case RSL_MT_RES_REQ:
1120 case RSL_MT_RECON_REQ:
1121 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1122 lc->name, rsl_msg_name(msg_type));
1123 msgb_free(msg);
1124 return -EINVAL;
1125 break;
1126 default:
1127 break;
1128 }
1129 }
1130
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001131 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001132 * SAPI.
1133 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001134 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001135 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001136 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001137 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001138 return -EINVAL;
1139 }
1140
Daniel Willmanne5233922012-12-25 23:15:50 +01001141 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001142 case RSL_MT_DATA_REQ:
1143 case RSL_MT_SUSP_REQ:
1144 case RSL_MT_REL_REQ:
1145 /* This is triggered in abnormal error conditions where
1146 * set_lapdm_context() was not called for the channel earlier. */
1147 if (!dl->dl.lctx.dl) {
1148 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1149 lc->name, rsl_msg_name(msg_type), sapi);
1150 msgb_free(msg);
1151 return -EINVAL;
1152 }
1153 break;
1154 default:
1155 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1156 lc->name, rsl_msg_name(msg_type), sapi);
1157 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001158
rootaf48bed2011-09-26 11:23:06 +02001159 switch (msg_type) {
1160 case RSL_MT_UNIT_DATA_REQ:
1161 rc = rslms_rx_rll_udata_req(msg, dl);
1162 break;
1163 case RSL_MT_EST_REQ:
1164 rc = rslms_rx_rll_est_req(msg, dl);
1165 break;
1166 case RSL_MT_DATA_REQ:
1167 rc = rslms_rx_rll_data_req(msg, dl);
1168 break;
1169 case RSL_MT_SUSP_REQ:
1170 rc = rslms_rx_rll_susp_req(msg, dl);
1171 break;
1172 case RSL_MT_RES_REQ:
1173 rc = rslms_rx_rll_res_req(msg, dl);
1174 break;
1175 case RSL_MT_RECON_REQ:
1176 rc = rslms_rx_rll_res_req(msg, dl);
1177 break;
1178 case RSL_MT_REL_REQ:
1179 rc = rslms_rx_rll_rel_req(msg, dl);
1180 break;
1181 default:
1182 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001183 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001184 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001185 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001186
1187 return rc;
1188}
1189
1190/* incoming RSLms COMMON CHANNEL message from L3 */
1191static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1192{
1193 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1194 int msg_type = cch->c.msg_type;
1195 int rc = 0;
1196
1197 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001198 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001199 return -EINVAL;
1200 }
1201
1202 switch (msg_type) {
1203 case RSL_MT_CHAN_RQD:
1204 /* create and send RACH request */
1205 rc = rslms_rx_chan_rqd(lc, msg);
1206 break;
1207 default:
rootaf48bed2011-09-26 11:23:06 +02001208 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001209 msg_type);
1210 msgb_free(msg);
1211 return 0;
1212 }
1213
1214 return rc;
1215}
1216
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001217/*! Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001218int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1219{
1220 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1221 int rc = 0;
1222
1223 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001224 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001225 return -EINVAL;
1226 }
1227
1228 switch (rslh->msg_discr & 0xfe) {
1229 case ABIS_RSL_MDISC_RLL:
1230 rc = rslms_rx_rll(msg, lc);
1231 break;
1232 case ABIS_RSL_MDISC_COM_CHAN:
1233 rc = rslms_rx_com_chan(msg, lc);
1234 break;
1235 default:
rootaf48bed2011-09-26 11:23:06 +02001236 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001237 "discriminator 0x%02x", rslh->msg_discr);
1238 msgb_free(msg);
1239 return -EINVAL;
1240 }
1241
1242 return rc;
1243}
1244
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001245/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001246int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1247{
rootaf48bed2011-09-26 11:23:06 +02001248 int i;
1249 enum lapd_mode lm;
1250
Harald Welte1f0b8c22011-06-27 10:51:37 +02001251 switch (mode) {
1252 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001253 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001254 break;
1255 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001256 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001257 break;
1258 default:
1259 return -EINVAL;
1260 }
1261
rootaf48bed2011-09-26 11:23:06 +02001262 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1263 lapd_set_mode(&le->datalink[i].dl, lm);
1264 }
1265
Harald Welte1f0b8c22011-06-27 10:51:37 +02001266 le->mode = mode;
1267
1268 return 0;
1269}
1270
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001271/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001272int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1273{
1274 int rc;
1275
1276 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1277 if (rc < 0)
1278 return rc;
1279
1280 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1281}
1282
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001283/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001284void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1285{
1286 lc->lapdm_dcch.l1_prim_cb = cb;
1287 lc->lapdm_acch.l1_prim_cb = cb;
1288 lc->lapdm_dcch.l1_ctx = ctx;
1289 lc->lapdm_acch.l1_ctx = ctx;
1290}
1291
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001292/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001293void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1294{
1295 lc->lapdm_dcch.l3_cb = cb;
1296 lc->lapdm_acch.l3_cb = cb;
1297 lc->lapdm_dcch.l3_ctx = ctx;
1298 lc->lapdm_acch.l3_ctx = ctx;
1299}
1300
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001301/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001302void lapdm_entity_reset(struct lapdm_entity *le)
1303{
1304 struct lapdm_datalink *dl;
1305 int i;
1306
1307 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1308 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001309 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001310 }
1311}
1312
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001313/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001314void lapdm_channel_reset(struct lapdm_channel *lc)
1315{
1316 lapdm_entity_reset(&lc->lapdm_dcch);
1317 lapdm_entity_reset(&lc->lapdm_acch);
1318}
1319
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001320/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001321void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1322{
1323 le->flags = flags;
1324}
1325
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001326/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001327void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1328{
1329 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1330 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1331}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001332
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001333/*! @} */