blob: ae21ccdfdc6286e438c72f01340da9d05a6cfbff [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);
Harald Welte3e8c5202018-05-04 20:58:48 +0200172 lc->lapdm_acch.lapdm_ch = lc;
rootaf48bed2011-09-26 11:23:06 +0200173 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100174 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte3e8c5202018-05-04 20:58:48 +0200175 lc->lapdm_dcch.lapdm_ch = lc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200176}
177
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200178/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200179void lapdm_entity_exit(struct lapdm_entity *le)
180{
181 unsigned int i;
182 struct lapdm_datalink *dl;
183
184 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
185 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200186 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200187 }
188}
189
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200190/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200191 *
192 * A convenience wrapper calling \ref lapdm_entity_exit on both
193 * entities inside the \ref lapdm_channel
194 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200195void lapdm_channel_exit(struct lapdm_channel *lc)
196{
197 lapdm_entity_exit(&lc->lapdm_acch);
198 lapdm_entity_exit(&lc->lapdm_dcch);
199}
200
Daniel Willmann55405fb2014-03-26 13:45:17 +0100201struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200202{
203 switch (sapi) {
204 case LAPDm_SAPI_NORMAL:
205 return &le->datalink[0];
206 case LAPDm_SAPI_SMS:
207 return &le->datalink[1];
208 default:
209 return NULL;
210 }
211}
212
Harald Welte1f0b8c22011-06-27 10:51:37 +0200213/* Append padding (if required) */
214static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
215{
216 int pad_len = n201 - msgb_l2len(msg);
217 uint8_t *data;
218
219 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200220 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200221 "cannot pad message that is already too big!\n");
222 return;
223 }
224
225 data = msgb_put(msg, pad_len);
226 memset(data, 0x2B, pad_len);
227}
228
229/* input function that L2 calls when sending messages up to L3 */
230static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
231{
232 if (!le->l3_cb) {
233 msgb_free(msg);
234 return -EIO;
235 }
236
237 /* call the layer2 message handler that is registered */
238 return le->l3_cb(msg, le, le->l3_ctx);
239}
240
241/* write a frame into the tx queue */
242static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200243 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200244{
245 struct lapdm_entity *le = dl->entity;
246 struct osmo_phsap_prim pp;
247
248 /* if there is a pending message, queue it */
249 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200250 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200251 *msgb_push(msg, 1) = link_id;
252 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200253 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200254 return -EBUSY;
255 }
256
257 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
258 PRIM_OP_REQUEST, msg);
259 pp.u.data.chan_nr = chan_nr;
260 pp.u.data.link_id = link_id;
261
262 /* send the frame now */
263 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200264 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200265
266 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
267}
268
269static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
270{
271 struct lapdm_datalink *dl;
272 int last = le->last_tx_dequeue;
273 int i = last, n = ARRAY_SIZE(le->datalink);
274 struct msgb *msg = NULL;
275
276 /* round-robin dequeue */
277 do {
278 /* next */
279 i = (i + 1) % n;
280 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200281 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200282 break;
283 } while (i != last);
284
285 if (msg) {
286 /* Set last dequeue position */
287 le->last_tx_dequeue = i;
288 }
289
290 return msg;
291}
292
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200293/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200294 * a osmo_phsap_prim */
295int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
296{
297 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200298 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200299
300 msg = tx_dequeue_msgb(le);
301 if (!msg)
302 return -ENODEV;
303
304 /* if we have a message, send PH-DATA.req */
305 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
306 PRIM_OP_REQUEST, msg);
307
308 /* Pull chan_nr and link_id */
309 pp->u.data.chan_nr = *msg->data;
310 msgb_pull(msg, 1);
311 pp->u.data.link_id = *msg->data;
312 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200313 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200314 msgb_pull(msg, 1);
315
316 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200317 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200318
319 return 0;
320}
321
322/* get next frame from the tx queue. because the ms has multiple datalinks,
323 * each datalink's queue is read round-robin.
324 */
325static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
326{
327 struct osmo_phsap_prim pp;
328
329 /* we may send again */
330 le->tx_pending = 0;
331
332 /* free confirm message */
333 if (msg)
334 msgb_free(msg);
335
336 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
337 /* no message in all queues */
338
339 /* If user didn't request PH-EMPTY_FRAME.req, abort */
340 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
341 return 0;
342
343 /* otherwise, send PH-EMPTY_FRAME.req */
344 osmo_prim_init(&pp.oph, SAP_GSM_PH,
345 PRIM_PH_EMPTY_FRAME,
346 PRIM_OP_REQUEST, NULL);
347 } else {
348 le->tx_pending = 1;
349 }
350
351 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
352}
353
Harald Welte542301b2018-04-19 16:11:14 +0200354/* Is a given msg_type "transparent" as per TS 48.058 Section 8.1 */
355static int rsl_is_transparent(uint8_t msg_type)
356{
357 switch (msg_type) {
358 case RSL_MT_DATA_IND:
359 case RSL_MT_UNIT_DATA_IND:
360 return 1;
361 case RSL_MT_DATA_REQ:
362 case RSL_MT_UNIT_DATA_REQ:
363 return 1;
364 default:
365 return 0;
366 }
367}
368
Harald Welte1f0b8c22011-06-27 10:51:37 +0200369/* Create RSLms various RSLms messages */
370static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
371 struct msgb *msg)
372{
Harald Welte542301b2018-04-19 16:11:14 +0200373 int transparent = rsl_is_transparent(msg_type);
374
Harald Welte1f0b8c22011-06-27 10:51:37 +0200375 /* Add the RSL + RLL header */
Harald Welte542301b2018-04-19 16:11:14 +0200376 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200377
378 /* send off the RSLms message to L3 */
379 return rslms_sendmsg(msg, mctx->dl->entity);
380}
381
382/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
383static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
384{
385 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
386 struct abis_rsl_rll_hdr *rllh;
387
388 /* Add the RSL + RLL header */
389 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
390 msgb_push(msg, 2 + 2);
391 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
392 mctx->link_id, 1);
393 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
394
395 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
396 rllh->data[1] = mctx->ta_ind;
397
398 rllh->data[2] = RSL_IE_MS_POWER;
399 rllh->data[3] = mctx->tx_power_ind;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100400
Harald Welte1f0b8c22011-06-27 10:51:37 +0200401 return rslms_sendmsg(msg, mctx->dl->entity);
402}
403
404static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
405{
406 struct msgb *msg;
Harald Welte542301b2018-04-19 16:11:14 +0200407 int transparent = rsl_is_transparent(msg_type);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200408
Harald Welte542301b2018-04-19 16:11:14 +0200409 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200410
411 /* send off the RSLms message to L3 */
412 return rslms_sendmsg(msg, mctx->dl->entity);
413}
414
415static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
416{
417 struct msgb *msg;
418
rootaf48bed2011-09-26 11:23:06 +0200419 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte542301b2018-04-19 16:11:14 +0200420 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200421 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
422 return rslms_sendmsg(msg, mctx->dl->entity);
423}
424
rootaf48bed2011-09-26 11:23:06 +0200425/* DLSAP L2 -> L3 (RSLms) */
426static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
427 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200428{
rootaf48bed2011-09-26 11:23:06 +0200429 struct lapd_datalink *dl = lctx->dl;
430 struct lapdm_datalink *mdl =
431 container_of(dl, struct lapdm_datalink, dl);
432 struct lapdm_msg_ctx *mctx = &mdl->mctx;
433 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200434
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200435 switch (OSMO_PRIM_HDR(&dp->oph)) {
436 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
437 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200438 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200439 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
440 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200441 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200442 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
443 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200444 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200445 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
446 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
447 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
448 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200449 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200450 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
451 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200452 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200453 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
454 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200455 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200456 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
457 rsl_rll_error(dp->u.error_ind.cause, mctx);
458 if (dp->oph.msg)
459 msgb_free(dp->oph.msg);
460 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200461 }
rootaf48bed2011-09-26 11:23:06 +0200462
463 if (!rll_msg) {
464 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
465 "fix!\n", dp->oph.primitive, dp->oph.operation);
466 return -EINVAL;
467 }
468
469 if (!dp->oph.msg)
470 return send_rll_simple(rll_msg, mctx);
471
472 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200473}
474
rootaf48bed2011-09-26 11:23:06 +0200475/* send a data frame to layer 1 */
476static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200477{
rootaf48bed2011-09-26 11:23:06 +0200478 uint8_t l3_len = msg->tail - msg->data;
479 struct lapd_datalink *dl = lctx->dl;
480 struct lapdm_datalink *mdl =
481 container_of(dl, struct lapdm_datalink, dl);
482 struct lapdm_msg_ctx *mctx = &mdl->mctx;
483 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200484
rootaf48bed2011-09-26 11:23:06 +0200485 /* prepend l2 header */
486 msg->l2h = msgb_push(msg, 3);
487 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
488 /* EA is set here too */
489 switch (format) {
490 case LAPD_FORM_I:
491 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
492 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200493 break;
rootaf48bed2011-09-26 11:23:06 +0200494 case LAPD_FORM_S:
495 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200496 break;
rootaf48bed2011-09-26 11:23:06 +0200497 case LAPD_FORM_U:
498 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200499 break;
500 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200501 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200502 return -EINVAL;
503 }
rootaf48bed2011-09-26 11:23:06 +0200504 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
505 if (lctx->more)
506 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200507
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100508 /* add ACCH header with last indicated tx-power and TA */
509 if ((mctx->link_id & 0x40)) {
510 struct lapdm_entity *le = mdl->entity;
511
512 msg->l2h = msgb_push(msg, 2);
513 msg->l2h[0] = le->tx_power;
514 msg->l2h[1] = le->ta;
515 }
516
rootaf48bed2011-09-26 11:23:06 +0200517 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
518 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200519}
520
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100521static int update_pending_frames(struct lapd_msg_ctx *lctx)
522{
523 struct lapd_datalink *dl = lctx->dl;
524 struct msgb *msg;
525 int rc = -1;
526
527 llist_for_each_entry(msg, &dl->tx_queue, list) {
528 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
529 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
530 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
531 rc = 0;
532 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
533 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
534 }
535 }
536
537 return rc;
538}
539
Harald Welte1f0b8c22011-06-27 10:51:37 +0200540/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200541static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
542 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200543{
544 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200545 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200546 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200547 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200548 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200549 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200550
551 /* when we reach here, we have a msgb with l2h pointing to the raw
552 * 23byte mac block. The l1h has already been purged. */
553
rootaf48bed2011-09-26 11:23:06 +0200554 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200555 mctx.chan_nr = chan_nr;
556 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200557
Harald Welte1f0b8c22011-06-27 10:51:37 +0200558 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
559 if (cbits == 0x10 || cbits == 0x12) {
560 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
561 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200562 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200563 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200564 } else {
565 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200566 /* It was received from network on SACCH */
567
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100568 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
569 if (le->mode == LAPDM_MODE_MS
570 && LAPDm_CTRL_is_U(msg->l2h[3])
571 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200572 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200573 n201 = N201_B4;
574 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200575 } else {
576 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200577 n201 = N201_AB_SACCH;
578 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200579 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200580 /* SACCH frames have a two-byte L1 header that
581 * OsmocomBB L1 doesn't strip */
582 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
583 mctx.ta_ind = msg->l2h[1];
584 msgb_pull(msg, 2);
585 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200586 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200587 } else {
588 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200589 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100590 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200591 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200592 }
593 }
594
Daniel Willmann55405fb2014-03-26 13:45:17 +0100595 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200596 /* G.2.1 No action on frames containing an unallocated SAPI. */
597 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200598 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200599 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200600 msgb_free(msg);
601 return -EIO;
602 }
603
Harald Welte1f0b8c22011-06-27 10:51:37 +0200604 switch (mctx.lapdm_fmt) {
605 case LAPDm_FMT_A:
606 case LAPDm_FMT_B:
607 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200608 lctx.dl = &mctx.dl->dl;
609 /* obtain SAPI from address field */
610 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
611 /* G.2.3 EA bit set to "0" is not allowed in GSM */
612 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
613 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
614 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200615 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200616 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200617 return -EINVAL;
618 }
rootaf48bed2011-09-26 11:23:06 +0200619 /* adress field */
620 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
621 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
622 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
623 /* command field */
624 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
625 lctx.format = LAPD_FORM_I;
626 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
627 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
628 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
629 lctx.format = LAPD_FORM_S;
630 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
631 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
632 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
633 lctx.format = LAPD_FORM_U;
634 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
635 } else
636 lctx.format = LAPD_FORM_UKN;
637 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
638 if (lctx.sapi != LAPDm_SAPI_NORMAL
639 && lctx.sapi != LAPDm_SAPI_SMS
640 && lctx.format == LAPD_FORM_U
641 && lctx.s_u == LAPDm_U_UI) {
642 /* 5.3.3 UI frames with invalid SAPI values shall be
643 * discarded
644 */
645 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
646 lctx.sapi);
647 msgb_free(msg);
648 return 0;
649 }
650 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
651 lctx.n201 = n201;
652 lctx.length = n201;
653 lctx.more = 0;
654 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100655 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200656 } else {
657 /* length field */
658 if (!(msg->l2h[2] & LAPDm_EL)) {
659 /* G.4.1 If the EL bit is set to "0", an
660 * MDL-ERROR-INDICATION primitive with cause
661 * "frame not implemented" is sent to the
662 * mobile management entity. */
663 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
664 "multi-octet length\n");
665 msgb_free(msg);
666 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
667 return -EINVAL;
668 }
669 lctx.n201 = n201;
670 lctx.length = msg->l2h[2] >> 2;
671 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
672 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100673 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200674 }
675 /* store context for messages from lapd */
676 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
677 /* send to LAPD */
678 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200679 break;
680 case LAPDm_FMT_Bter:
681 /* FIXME */
682 msgb_free(msg);
683 break;
684 case LAPDm_FMT_Bbis:
685 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200686 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200687 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100688 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200689 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
690 break;
691 default:
692 msgb_free(msg);
693 }
694
695 return rc;
696}
697
698/* input into layer2 (from layer 1) */
699static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
700{
701 struct abis_rsl_cchan_hdr *ch;
702 struct gsm48_req_ref req_ref;
703 struct gsm_time gt;
704 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
705
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200706 if (!msg)
707 return -ENOMEM;
708
Harald Welte1f0b8c22011-06-27 10:51:37 +0200709 msg->l2h = msgb_push(msg, sizeof(*ch));
710 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
711 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
712 ch->chan_nr = RSL_CHAN_RACH;
713
714 /* generate a RSL CHANNEL REQUIRED message */
715 gsm_fn2gsmtime(&gt, fn);
716 req_ref.ra = ra;
717 req_ref.t1 = gt.t1; /* FIXME: modulo? */
718 req_ref.t2 = gt.t2;
719 req_ref.t3_low = gt.t3 & 7;
720 req_ref.t3_high = gt.t3 >> 3;
721
722 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
723 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
724
725 return rslms_sendmsg(msg, le);
726}
727
728static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
729
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200730/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200731int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
732{
733 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
734 int rc = 0;
735
736 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200737 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200738 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100739 rc = -ENODEV;
740 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200741 }
742
743 switch (oph->primitive) {
744 case PRIM_PH_DATA:
745 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200746 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200747 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100748 rc = -ENODEV;
749 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200750 }
751 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
752 pp->u.data.link_id);
753 break;
754 case PRIM_PH_RTS:
755 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200756 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200757 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100758 rc = -ENODEV;
759 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200760 }
761 rc = l2_ph_data_conf(oph->msg, le);
762 break;
763 case PRIM_PH_RACH:
764 switch (oph->operation) {
765 case PRIM_OP_INDICATION:
766 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
767 pp->u.rach_ind.acc_delay);
768 break;
769 case PRIM_OP_CONFIRM:
770 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
771 break;
772 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100773 rc = -EIO;
774 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200775 }
776 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100777 default:
778 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
779 oph->primitive);
780 rc = -EINVAL;
781 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200782 }
783
784 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100785
786free:
787 msgb_free(oph->msg);
788 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200789}
790
791
792/* L3 -> L2 / RSLMS -> LAPDm */
793
rootaf48bed2011-09-26 11:23:06 +0200794/* Set LAPDm context for established connection */
795static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
796 uint8_t link_id, int n201, uint8_t sapi)
797{
798 memset(&dl->mctx, 0, sizeof(dl->mctx));
799 dl->mctx.dl = dl;
800 dl->mctx.chan_nr = chan_nr;
801 dl->mctx.link_id = link_id;
802 dl->dl.lctx.dl = &dl->dl;
803 dl->dl.lctx.n201 = n201;
804 dl->dl.lctx.sapi = sapi;
805
806 return 0;
807}
808
Harald Welte1f0b8c22011-06-27 10:51:37 +0200809/* L3 requests establishment of data link */
810static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
811{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200812 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
813 uint8_t chan_nr = rllh->chan_nr;
814 uint8_t link_id = rllh->link_id;
815 uint8_t sapi = rllh->link_id & 7;
816 struct tlv_parsed tv;
817 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100818 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200819 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200820
rootaf48bed2011-09-26 11:23:06 +0200821 /* Set LAPDm context for established connection */
822 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200823
rootaf48bed2011-09-26 11:23:06 +0200824 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200825 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200826 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200827 /* contention resolution establishment procedure */
828 if (sapi != 0) {
829 /* According to clause 6, the contention resolution
830 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200831 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200832 "resolution (discarding)\n");
833 msgb_free(msg);
834 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
835 }
836 /* transmit a SABM command with the P bit set to "1". The SABM
837 * command shall contain the layer 3 message unit */
838 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200839 } else {
840 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200841 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200842 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200843 }
844
845 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100846 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200847 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100848 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200849 msgb_free(msg);
850 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
851 }
852
rootaf48bed2011-09-26 11:23:06 +0200853 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100854 msgb_pull_to_l3(msg);
855 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200856
rootaf48bed2011-09-26 11:23:06 +0200857 /* prepare prim */
858 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200859
rootaf48bed2011-09-26 11:23:06 +0200860 /* send to L2 */
861 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200862}
863
864/* L3 requests transfer of unnumbered information */
865static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
866{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100867 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200868 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
869 uint8_t chan_nr = rllh->chan_nr;
870 uint8_t link_id = rllh->link_id;
871 uint8_t sapi = link_id & 7;
872 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +0100873 int length, ui_bts;
874
875 if (!le) {
876 LOGP(DLLAPD, LOGL_ERROR, "lapdm_datalink without entity error\n");
877 msgb_free(msg);
878 return -EMLINK;
879 }
880 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200881
882 /* check if the layer3 message length exceeds N201 */
883
884 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
885
886 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100887 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200888 }
889 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100890 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200891 }
892 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200893 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200894 "error\n");
895 msgb_free(msg);
896 return -EINVAL;
897 }
rootaf48bed2011-09-26 11:23:06 +0200898 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200899 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
900 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200901 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200902 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200903 "(discarding)\n", length,
904 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200905 msgb_free(msg);
906 return -EIO;
907 }
908
rootaf48bed2011-09-26 11:23:06 +0200909 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100910 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200911
rootaf48bed2011-09-26 11:23:06 +0200912 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100913 msgb_pull_to_l3(msg);
914 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200915
916 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200917 msg->l2h = msgb_push(msg, 2 + !ui_bts);
918 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
919 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100920 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200921 msg->l2h[2] = LAPDm_LEN(length);
922 if (link_id & 0x40) {
923 msg->l2h = msgb_push(msg, 2);
924 msg->l2h[0] = le->tx_power;
925 msg->l2h[1] = le->ta;
926 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200927
928 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200929 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200930}
931
932/* L3 requests transfer of acknowledged information */
933static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
934{
935 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
936 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200937 int length;
938 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200939
940 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
941 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200942 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200943 "error\n");
944 msgb_free(msg);
945 return -EINVAL;
946 }
rootaf48bed2011-09-26 11:23:06 +0200947 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
948 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200949
rootaf48bed2011-09-26 11:23:06 +0200950 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100951 msgb_pull_to_l3(msg);
952 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200953
rootaf48bed2011-09-26 11:23:06 +0200954 /* prepare prim */
955 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200956
rootaf48bed2011-09-26 11:23:06 +0200957 /* send to L2 */
958 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200959}
960
961/* L3 requests suspension of data link */
962static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
963{
964 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
965 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200966 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200967
968 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200969 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200970 msgb_free(msg);
971 return -EINVAL;
972 }
973
rootaf48bed2011-09-26 11:23:06 +0200974 /* prepare prim */
975 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200976
rootaf48bed2011-09-26 11:23:06 +0200977 /* send to L2 */
978 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200979}
980
981/* L3 requests resume of data link */
982static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
983{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200984 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200985 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200986 uint8_t chan_nr = rllh->chan_nr;
987 uint8_t link_id = rllh->link_id;
988 uint8_t sapi = rllh->link_id & 7;
989 struct tlv_parsed tv;
990 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100991 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200992 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200993
rootaf48bed2011-09-26 11:23:06 +0200994 /* Set LAPDm context for established connection */
995 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200996
997 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
998 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200999 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001000 msgb_free(msg);
1001 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1002 }
rootaf48bed2011-09-26 11:23:06 +02001003 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001004 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1005
rootaf48bed2011-09-26 11:23:06 +02001006 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001007 msgb_pull_to_l3(msg);
1008 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001009
rootaf48bed2011-09-26 11:23:06 +02001010 /* prepare prim */
1011 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1012 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001013
rootaf48bed2011-09-26 11:23:06 +02001014 /* send to L2 */
1015 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001016}
1017
1018/* L3 requests release of data link */
1019static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1020{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001021 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001022 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001023 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001024
1025 /* get release mode */
1026 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1027 mode = rllh->data[1] & 1;
1028
Harald Welte1f0b8c22011-06-27 10:51:37 +02001029 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001030 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001031
1032 /* 04.06 3.8.3: No information field is permitted with the DISC
1033 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001034 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001035
rootaf48bed2011-09-26 11:23:06 +02001036 /* prepare prim */
1037 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1038 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001039
rootaf48bed2011-09-26 11:23:06 +02001040 /* send to L2 */
1041 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001042}
1043
1044/* L3 requests channel in idle state */
1045static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1046{
1047 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1048 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1049 struct osmo_phsap_prim pp;
1050
1051 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1052 PRIM_OP_REQUEST, NULL);
1053
1054 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001055 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001056 return -EINVAL;
1057 }
1058 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001059 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001060 return -EINVAL;
1061 }
1062 pp.u.rach_req.ra = cch->data[1];
1063 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1064 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1065
1066 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001067 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001068 return -EINVAL;
1069 }
1070 /* TA = 0 - delay */
1071 pp.u.rach_req.ta = 0 - cch->data[5];
1072
1073 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001074 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001075 return -EINVAL;
1076 }
1077 pp.u.rach_req.tx_power = cch->data[7];
1078
1079 msgb_free(msg);
1080
1081 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1082}
1083
1084/* L1 confirms channel request */
1085static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1086{
1087 struct abis_rsl_cchan_hdr *ch;
1088 struct gsm_time tm;
1089 struct gsm48_req_ref *ref;
1090
1091 gsm_fn2gsmtime(&tm, frame_nr);
1092
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001093 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001094 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1095 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1096 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1097 ch->chan_nr = RSL_CHAN_RACH;
1098 ch->data[0] = RSL_IE_REQ_REFERENCE;
1099 ref = (struct gsm48_req_ref *) (ch->data + 1);
1100 ref->t1 = tm.t1;
1101 ref->t2 = tm.t2;
1102 ref->t3_low = tm.t3 & 0x7;
1103 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001104
Harald Welte1f0b8c22011-06-27 10:51:37 +02001105 return rslms_sendmsg(msg, le);
1106}
1107
Harald Welte1f0b8c22011-06-27 10:51:37 +02001108/* incoming RSLms RLL message from L3 */
1109static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1110{
1111 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1112 int msg_type = rllh->c.msg_type;
1113 uint8_t sapi = rllh->link_id & 7;
1114 struct lapdm_entity *le;
1115 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001116 int rc = 0;
1117
1118 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001119 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001120 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001121 return -EINVAL;
1122 }
1123
1124 if (rllh->link_id & 0x40)
1125 le = &lc->lapdm_acch;
1126 else
1127 le = &lc->lapdm_dcch;
1128
Harald Welte1a87c1b2015-12-14 15:26:07 +01001129 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1130 * BTS side */
1131 if (le->mode == LAPDM_MODE_BTS) {
1132 switch (msg_type) {
1133 case RSL_MT_SUSP_REQ:
1134 case RSL_MT_RES_REQ:
1135 case RSL_MT_RECON_REQ:
1136 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1137 lc->name, rsl_msg_name(msg_type));
1138 msgb_free(msg);
1139 return -EINVAL;
1140 break;
1141 default:
1142 break;
1143 }
1144 }
1145
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001146 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001147 * SAPI.
1148 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001149 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001150 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001151 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001152 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001153 return -EINVAL;
1154 }
1155
Daniel Willmanne5233922012-12-25 23:15:50 +01001156 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001157 case RSL_MT_DATA_REQ:
1158 case RSL_MT_SUSP_REQ:
1159 case RSL_MT_REL_REQ:
1160 /* This is triggered in abnormal error conditions where
1161 * set_lapdm_context() was not called for the channel earlier. */
1162 if (!dl->dl.lctx.dl) {
1163 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1164 lc->name, rsl_msg_name(msg_type), sapi);
1165 msgb_free(msg);
1166 return -EINVAL;
1167 }
1168 break;
1169 default:
1170 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1171 lc->name, rsl_msg_name(msg_type), sapi);
1172 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001173
rootaf48bed2011-09-26 11:23:06 +02001174 switch (msg_type) {
1175 case RSL_MT_UNIT_DATA_REQ:
1176 rc = rslms_rx_rll_udata_req(msg, dl);
1177 break;
1178 case RSL_MT_EST_REQ:
1179 rc = rslms_rx_rll_est_req(msg, dl);
1180 break;
1181 case RSL_MT_DATA_REQ:
1182 rc = rslms_rx_rll_data_req(msg, dl);
1183 break;
1184 case RSL_MT_SUSP_REQ:
1185 rc = rslms_rx_rll_susp_req(msg, dl);
1186 break;
1187 case RSL_MT_RES_REQ:
1188 rc = rslms_rx_rll_res_req(msg, dl);
1189 break;
1190 case RSL_MT_RECON_REQ:
1191 rc = rslms_rx_rll_res_req(msg, dl);
1192 break;
1193 case RSL_MT_REL_REQ:
1194 rc = rslms_rx_rll_rel_req(msg, dl);
1195 break;
1196 default:
1197 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001198 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001199 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001200 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001201
1202 return rc;
1203}
1204
1205/* incoming RSLms COMMON CHANNEL message from L3 */
1206static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1207{
1208 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1209 int msg_type = cch->c.msg_type;
1210 int rc = 0;
1211
1212 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001213 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001214 return -EINVAL;
1215 }
1216
1217 switch (msg_type) {
1218 case RSL_MT_CHAN_RQD:
1219 /* create and send RACH request */
1220 rc = rslms_rx_chan_rqd(lc, msg);
1221 break;
1222 default:
rootaf48bed2011-09-26 11:23:06 +02001223 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001224 msg_type);
1225 msgb_free(msg);
1226 return 0;
1227 }
1228
1229 return rc;
1230}
1231
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001232/*! Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001233int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1234{
1235 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1236 int rc = 0;
1237
1238 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001239 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001240 return -EINVAL;
1241 }
1242
1243 switch (rslh->msg_discr & 0xfe) {
1244 case ABIS_RSL_MDISC_RLL:
1245 rc = rslms_rx_rll(msg, lc);
1246 break;
1247 case ABIS_RSL_MDISC_COM_CHAN:
1248 rc = rslms_rx_com_chan(msg, lc);
1249 break;
1250 default:
rootaf48bed2011-09-26 11:23:06 +02001251 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001252 "discriminator 0x%02x", rslh->msg_discr);
1253 msgb_free(msg);
1254 return -EINVAL;
1255 }
1256
1257 return rc;
1258}
1259
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001260/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001261int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1262{
rootaf48bed2011-09-26 11:23:06 +02001263 int i;
1264 enum lapd_mode lm;
1265
Harald Welte1f0b8c22011-06-27 10:51:37 +02001266 switch (mode) {
1267 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001268 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001269 break;
1270 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001271 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001272 break;
1273 default:
1274 return -EINVAL;
1275 }
1276
rootaf48bed2011-09-26 11:23:06 +02001277 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1278 lapd_set_mode(&le->datalink[i].dl, lm);
1279 }
1280
Harald Welte1f0b8c22011-06-27 10:51:37 +02001281 le->mode = mode;
1282
1283 return 0;
1284}
1285
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001286/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001287int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1288{
1289 int rc;
1290
1291 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1292 if (rc < 0)
1293 return rc;
1294
1295 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1296}
1297
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001298/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001299void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1300{
1301 lc->lapdm_dcch.l1_prim_cb = cb;
1302 lc->lapdm_acch.l1_prim_cb = cb;
1303 lc->lapdm_dcch.l1_ctx = ctx;
1304 lc->lapdm_acch.l1_ctx = ctx;
1305}
1306
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001307/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001308void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1309{
1310 lc->lapdm_dcch.l3_cb = cb;
1311 lc->lapdm_acch.l3_cb = cb;
1312 lc->lapdm_dcch.l3_ctx = ctx;
1313 lc->lapdm_acch.l3_ctx = ctx;
1314}
1315
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001316/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001317void lapdm_entity_reset(struct lapdm_entity *le)
1318{
1319 struct lapdm_datalink *dl;
1320 int i;
1321
1322 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1323 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001324 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001325 }
1326}
1327
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001328/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001329void lapdm_channel_reset(struct lapdm_channel *lc)
1330{
1331 lapdm_entity_reset(&lc->lapdm_dcch);
1332 lapdm_entity_reset(&lc->lapdm_acch);
1333}
1334
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001335/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001336void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1337{
1338 le->flags = flags;
1339}
1340
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001341/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001342void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1343{
1344 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1345 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1346}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001347
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001348/*! @} */