blob: b9e7304eee47d9a9c7a8a4dfd3cb9809942bc82a [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
Harald Welte1284c3e2018-05-01 18:11:02 +020051#define LAPD_U_SABM 0x7
52
Harald Welte1f0b8c22011-06-27 10:51:37 +020053/* TS 04.06 Figure 4 / Section 3.2 */
54#define LAPDm_LPD_NORMAL 0
55#define LAPDm_LPD_SMSCB 1
56#define LAPDm_SAPI_NORMAL 0
57#define LAPDm_SAPI_SMS 3
58#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
59
rootaf48bed2011-09-26 11:23:06 +020060#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020061#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
62#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
63#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
64
65/* TS 04.06 Table 3 / Section 3.4.3 */
66#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
67#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
68#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
69
70#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
71#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
72#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
73
74#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
75#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
76
77#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
78
79#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
80#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
81
Harald Welte1f0b8c22011-06-27 10:51:37 +020082#define LAPDm_LEN(len) ((len << 2) | 0x1)
83#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020084#define LAPDm_EL 0x1
85
86#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020087
88/* TS 04.06 Section 5.8.3 */
89#define N201_AB_SACCH 18
90#define N201_AB_SDCCH 20
91#define N201_AB_FACCH 20
92#define N201_Bbis 23
93#define N201_Bter_SACCH 21
94#define N201_Bter_SDCCH 23
95#define N201_Bter_FACCH 23
96#define N201_B4 19
97
98/* 5.8.2.1 N200 during establish and release */
99#define N200_EST_REL 5
100/* 5.8.2.1 N200 during timer recovery state */
101#define N200_TR_SACCH 5
102#define N200_TR_SDCCH 23
103#define N200_TR_FACCH_FR 34
104#define N200_TR_EFACCH_FR 48
105#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200106/* FIXME: set N200 depending on chan_nr */
107#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200108
109enum lapdm_format {
110 LAPDm_FMT_A,
111 LAPDm_FMT_B,
112 LAPDm_FMT_Bbis,
113 LAPDm_FMT_Bter,
114 LAPDm_FMT_B4,
115};
116
Maxadef12a2016-05-25 15:25:02 +0200117const struct value_string osmo_ph_prim_names[] = {
118 { PRIM_PH_DATA, "PH-DATA" },
119 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
120 { PRIM_PH_CONN, "PH-CONNECT" },
121 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
122 { PRIM_PH_RTS, "PH-RTS" },
123 { PRIM_MPH_INFO, "MPH-INFO" },
124 { PRIM_TCH, "TCH" },
125 { PRIM_TCH_RTS, "TCH-RTS" },
126 { 0, NULL }
127};
128
rootaf48bed2011-09-26 11:23:06 +0200129static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
130static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
131 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100132static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200133
134static void lapdm_dl_init(struct lapdm_datalink *dl,
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100135 struct lapdm_entity *entity, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200136{
137 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200138 dl->entity = entity;
rootaf48bed2011-09-26 11:23:06 +0200139 lapd_dl_init(&dl->dl, 1, 8, 200);
140 dl->dl.reestablish = 0; /* GSM uses no reestablish */
141 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
142 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100143 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200144 dl->dl.n200_est_rel = N200_EST_REL;
145 dl->dl.n200 = N200;
146 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100147 dl->dl.t200_sec = t200; dl->dl.t200_usec = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200148}
149
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200150/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200151 * \param[in] le LAPDm entity
152 * \param[in] mode \ref lapdm_mode (BTS/MS)
153 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100154void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200155{
156 unsigned int i;
157
158 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100159 lapdm_dl_init(&le->datalink[i], le, t200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200160
161 lapdm_entity_set_mode(le, mode);
162}
163
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200164/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200165 * \param[in] lc \ref lapdm_channel to be initialized
166 * \param[in] mode \ref lapdm_mode (BTS/MS)
167 *
168 * This really is a convenience wrapper around calling \ref
169 * lapdm_entity_init twice.
170 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200171void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
172{
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100173 lapdm_entity_init(&lc->lapdm_acch, mode, 2);
Harald Welte3e8c5202018-05-04 20:58:48 +0200174 lc->lapdm_acch.lapdm_ch = lc;
rootaf48bed2011-09-26 11:23:06 +0200175 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100176 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte3e8c5202018-05-04 20:58:48 +0200177 lc->lapdm_dcch.lapdm_ch = lc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200178}
179
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200180/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200181void lapdm_entity_exit(struct lapdm_entity *le)
182{
183 unsigned int i;
184 struct lapdm_datalink *dl;
185
186 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
187 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200188 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200189 }
190}
191
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200192/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200193 *
194 * A convenience wrapper calling \ref lapdm_entity_exit on both
195 * entities inside the \ref lapdm_channel
196 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200197void lapdm_channel_exit(struct lapdm_channel *lc)
198{
199 lapdm_entity_exit(&lc->lapdm_acch);
200 lapdm_entity_exit(&lc->lapdm_dcch);
201}
202
Daniel Willmann55405fb2014-03-26 13:45:17 +0100203struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200204{
205 switch (sapi) {
206 case LAPDm_SAPI_NORMAL:
207 return &le->datalink[0];
208 case LAPDm_SAPI_SMS:
209 return &le->datalink[1];
210 default:
211 return NULL;
212 }
213}
214
Harald Welte1f0b8c22011-06-27 10:51:37 +0200215/* Append padding (if required) */
216static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
217{
218 int pad_len = n201 - msgb_l2len(msg);
219 uint8_t *data;
220
221 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200222 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200223 "cannot pad message that is already too big!\n");
224 return;
225 }
226
227 data = msgb_put(msg, pad_len);
228 memset(data, 0x2B, pad_len);
229}
230
231/* input function that L2 calls when sending messages up to L3 */
232static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
233{
234 if (!le->l3_cb) {
235 msgb_free(msg);
236 return -EIO;
237 }
238
239 /* call the layer2 message handler that is registered */
240 return le->l3_cb(msg, le, le->l3_ctx);
241}
242
243/* write a frame into the tx queue */
244static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200245 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200246{
247 struct lapdm_entity *le = dl->entity;
248 struct osmo_phsap_prim pp;
249
250 /* if there is a pending message, queue it */
251 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200252 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200253 *msgb_push(msg, 1) = link_id;
254 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200255 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200256 return -EBUSY;
257 }
258
259 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
260 PRIM_OP_REQUEST, msg);
261 pp.u.data.chan_nr = chan_nr;
262 pp.u.data.link_id = link_id;
263
264 /* send the frame now */
265 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200266 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200267
268 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
269}
270
271static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
272{
273 struct lapdm_datalink *dl;
274 int last = le->last_tx_dequeue;
275 int i = last, n = ARRAY_SIZE(le->datalink);
276 struct msgb *msg = NULL;
277
278 /* round-robin dequeue */
279 do {
280 /* next */
281 i = (i + 1) % n;
282 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200283 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200284 break;
285 } while (i != last);
286
287 if (msg) {
288 /* Set last dequeue position */
289 le->last_tx_dequeue = i;
290 }
291
292 return msg;
293}
294
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200295/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200296 * a osmo_phsap_prim */
297int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
298{
299 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200300 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200301
302 msg = tx_dequeue_msgb(le);
303 if (!msg)
304 return -ENODEV;
305
306 /* if we have a message, send PH-DATA.req */
307 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
308 PRIM_OP_REQUEST, msg);
309
310 /* Pull chan_nr and link_id */
311 pp->u.data.chan_nr = *msg->data;
312 msgb_pull(msg, 1);
313 pp->u.data.link_id = *msg->data;
314 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200315 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200316 msgb_pull(msg, 1);
317
318 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200319 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200320
321 return 0;
322}
323
324/* get next frame from the tx queue. because the ms has multiple datalinks,
325 * each datalink's queue is read round-robin.
326 */
327static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
328{
329 struct osmo_phsap_prim pp;
330
331 /* we may send again */
332 le->tx_pending = 0;
333
334 /* free confirm message */
335 if (msg)
336 msgb_free(msg);
337
338 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
339 /* no message in all queues */
340
341 /* If user didn't request PH-EMPTY_FRAME.req, abort */
342 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
343 return 0;
344
345 /* otherwise, send PH-EMPTY_FRAME.req */
346 osmo_prim_init(&pp.oph, SAP_GSM_PH,
347 PRIM_PH_EMPTY_FRAME,
348 PRIM_OP_REQUEST, NULL);
349 } else {
350 le->tx_pending = 1;
351 }
352
353 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
354}
355
Harald Welte542301b2018-04-19 16:11:14 +0200356/* Is a given msg_type "transparent" as per TS 48.058 Section 8.1 */
357static int rsl_is_transparent(uint8_t msg_type)
358{
359 switch (msg_type) {
360 case RSL_MT_DATA_IND:
361 case RSL_MT_UNIT_DATA_IND:
362 return 1;
363 case RSL_MT_DATA_REQ:
364 case RSL_MT_UNIT_DATA_REQ:
365 return 1;
366 default:
367 return 0;
368 }
369}
370
Harald Welte1f0b8c22011-06-27 10:51:37 +0200371/* Create RSLms various RSLms messages */
372static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
373 struct msgb *msg)
374{
Harald Welte542301b2018-04-19 16:11:14 +0200375 int transparent = rsl_is_transparent(msg_type);
376
Harald Welte1f0b8c22011-06-27 10:51:37 +0200377 /* Add the RSL + RLL header */
Harald Welte542301b2018-04-19 16:11:14 +0200378 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200379
380 /* send off the RSLms message to L3 */
381 return rslms_sendmsg(msg, mctx->dl->entity);
382}
383
384/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
385static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
386{
387 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200388
389 /* Add the RSL + RLL header */
390 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welted977f5f2018-05-08 21:53:28 +0200391
Harald Weltef1bdf782018-05-08 22:03:20 +0200392 /* Add two non-standard IEs carrying MS power and TA values for B4 (SACCH) */
393 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
394 msgb_tv_push(msg, RSL_IE_MS_POWER, mctx->tx_power_ind);
395 msgb_tv_push(msg, RSL_IE_TIMING_ADVANCE, mctx->ta_ind);
396 }
Harald Welted977f5f2018-05-08 21:53:28 +0200397
Harald Welte1f0b8c22011-06-27 10:51:37 +0200398 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
399 mctx->link_id, 1);
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 Welte1284c3e2018-05-01 18:11:02 +0200540/* determine if receiving a given LAPDm message is not permitted */
541static int lapdm_rx_not_permitted(const struct lapdm_entity *le,
542 const struct lapd_msg_ctx *lctx)
543{
544 /* we currently only implement SABM related checks here */
545 if (lctx->format != LAPD_FORM_U || lctx->s_u != LAPD_U_SABM)
546 return 0;
547
548 if (le->mode == LAPDM_MODE_BTS) {
549 if (le == &le->lapdm_ch->lapdm_acch) {
550 /* no contention resolution on SACCH */
551 if (lctx->length > 0)
552 return RLL_CAUSE_SABM_INFO_NOTALL;
553 } else {
554 switch (lctx->sapi) {
555 case 0:
556 /* SAPI0 must use contention resolution, i.e. L3 payload must exist */
557 if (lctx->length == 0)
558 return RLL_CAUSE_UFRM_INC_PARAM;
559 break;
560 case 3:
561 /* SAPI3 doesn't support contention resolution */
562 if (lctx->length > 0)
563 return RLL_CAUSE_SABM_INFO_NOTALL;
564 break;
565 }
566 }
567 } else if (le->mode == LAPDM_MODE_MS) {
568 /* contention resolution (L3 present) is only sent by MS, but
569 * never received by it */
570 if (lctx->length > 0)
571 return RLL_CAUSE_SABM_INFO_NOTALL;
572 }
573 return 0;
574}
575
Harald Welte1f0b8c22011-06-27 10:51:37 +0200576/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200577static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
578 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200579{
580 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200581 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200582 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200583 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200584 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200585 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200586
587 /* when we reach here, we have a msgb with l2h pointing to the raw
588 * 23byte mac block. The l1h has already been purged. */
589
rootaf48bed2011-09-26 11:23:06 +0200590 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200591 mctx.chan_nr = chan_nr;
592 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200593
Harald Welte1f0b8c22011-06-27 10:51:37 +0200594 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
595 if (cbits == 0x10 || cbits == 0x12) {
596 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
597 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200598 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200599 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200600 } else {
601 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200602 /* It was received from network on SACCH */
603
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100604 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
605 if (le->mode == LAPDM_MODE_MS
606 && LAPDm_CTRL_is_U(msg->l2h[3])
607 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200608 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200609 n201 = N201_B4;
610 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200611 } else {
612 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200613 n201 = N201_AB_SACCH;
614 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200615 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200616 /* SACCH frames have a two-byte L1 header that
617 * OsmocomBB L1 doesn't strip */
618 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
619 mctx.ta_ind = msg->l2h[1];
620 msgb_pull(msg, 2);
621 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200622 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200623 } else {
624 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200625 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100626 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200627 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200628 }
629 }
630
Daniel Willmann55405fb2014-03-26 13:45:17 +0100631 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200632 /* G.2.1 No action on frames containing an unallocated SAPI. */
633 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200634 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200635 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200636 msgb_free(msg);
637 return -EIO;
638 }
639
Harald Welte1f0b8c22011-06-27 10:51:37 +0200640 switch (mctx.lapdm_fmt) {
641 case LAPDm_FMT_A:
642 case LAPDm_FMT_B:
643 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200644 lctx.dl = &mctx.dl->dl;
645 /* obtain SAPI from address field */
646 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
647 /* G.2.3 EA bit set to "0" is not allowed in GSM */
648 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
649 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
650 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200651 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200652 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200653 return -EINVAL;
654 }
rootaf48bed2011-09-26 11:23:06 +0200655 /* adress field */
656 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
657 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
658 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
659 /* command field */
660 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
661 lctx.format = LAPD_FORM_I;
662 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
663 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
664 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
665 lctx.format = LAPD_FORM_S;
666 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
667 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
668 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
669 lctx.format = LAPD_FORM_U;
670 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
671 } else
672 lctx.format = LAPD_FORM_UKN;
673 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
674 if (lctx.sapi != LAPDm_SAPI_NORMAL
675 && lctx.sapi != LAPDm_SAPI_SMS
676 && lctx.format == LAPD_FORM_U
677 && lctx.s_u == LAPDm_U_UI) {
678 /* 5.3.3 UI frames with invalid SAPI values shall be
679 * discarded
680 */
681 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
682 lctx.sapi);
683 msgb_free(msg);
684 return 0;
685 }
686 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
687 lctx.n201 = n201;
688 lctx.length = n201;
689 lctx.more = 0;
690 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100691 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200692 } else {
693 /* length field */
694 if (!(msg->l2h[2] & LAPDm_EL)) {
695 /* G.4.1 If the EL bit is set to "0", an
696 * MDL-ERROR-INDICATION primitive with cause
697 * "frame not implemented" is sent to the
698 * mobile management entity. */
699 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
700 "multi-octet length\n");
701 msgb_free(msg);
702 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
703 return -EINVAL;
704 }
705 lctx.n201 = n201;
706 lctx.length = msg->l2h[2] >> 2;
707 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
708 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100709 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200710 }
711 /* store context for messages from lapd */
712 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200713 rc =lapdm_rx_not_permitted(le, &lctx);
714 if (rc > 0) {
715 LOGP(DLLAPD, LOGL_NOTICE, "received message not permitted");
716 msgb_free(msg);
717 rsl_rll_error(rc, &mctx);
718 return -EINVAL;
719 }
rootaf48bed2011-09-26 11:23:06 +0200720 /* send to LAPD */
721 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200722 break;
723 case LAPDm_FMT_Bter:
724 /* FIXME */
725 msgb_free(msg);
726 break;
727 case LAPDm_FMT_Bbis:
728 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200729 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200730 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100731 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200732 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
733 break;
734 default:
735 msgb_free(msg);
736 }
737
738 return rc;
739}
740
741/* input into layer2 (from layer 1) */
742static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
743{
744 struct abis_rsl_cchan_hdr *ch;
745 struct gsm48_req_ref req_ref;
746 struct gsm_time gt;
747 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
748
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200749 if (!msg)
750 return -ENOMEM;
751
Harald Welte1f0b8c22011-06-27 10:51:37 +0200752 msg->l2h = msgb_push(msg, sizeof(*ch));
753 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
754 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
755 ch->chan_nr = RSL_CHAN_RACH;
756
757 /* generate a RSL CHANNEL REQUIRED message */
758 gsm_fn2gsmtime(&gt, fn);
759 req_ref.ra = ra;
760 req_ref.t1 = gt.t1; /* FIXME: modulo? */
761 req_ref.t2 = gt.t2;
762 req_ref.t3_low = gt.t3 & 7;
763 req_ref.t3_high = gt.t3 >> 3;
764
765 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
766 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
767
768 return rslms_sendmsg(msg, le);
769}
770
771static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
772
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200773/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200774int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
775{
776 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
777 int rc = 0;
778
779 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200780 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200781 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100782 rc = -ENODEV;
783 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200784 }
785
786 switch (oph->primitive) {
787 case PRIM_PH_DATA:
788 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200789 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200790 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100791 rc = -ENODEV;
792 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200793 }
794 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
795 pp->u.data.link_id);
796 break;
797 case PRIM_PH_RTS:
798 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200799 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200800 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100801 rc = -ENODEV;
802 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200803 }
804 rc = l2_ph_data_conf(oph->msg, le);
805 break;
806 case PRIM_PH_RACH:
807 switch (oph->operation) {
808 case PRIM_OP_INDICATION:
809 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
810 pp->u.rach_ind.acc_delay);
811 break;
812 case PRIM_OP_CONFIRM:
813 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
814 break;
815 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100816 rc = -EIO;
817 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200818 }
819 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100820 default:
821 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
822 oph->primitive);
823 rc = -EINVAL;
824 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200825 }
826
827 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100828
829free:
830 msgb_free(oph->msg);
831 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200832}
833
834
835/* L3 -> L2 / RSLMS -> LAPDm */
836
rootaf48bed2011-09-26 11:23:06 +0200837/* Set LAPDm context for established connection */
838static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
839 uint8_t link_id, int n201, uint8_t sapi)
840{
841 memset(&dl->mctx, 0, sizeof(dl->mctx));
842 dl->mctx.dl = dl;
843 dl->mctx.chan_nr = chan_nr;
844 dl->mctx.link_id = link_id;
845 dl->dl.lctx.dl = &dl->dl;
846 dl->dl.lctx.n201 = n201;
847 dl->dl.lctx.sapi = sapi;
848
849 return 0;
850}
851
Harald Welte1f0b8c22011-06-27 10:51:37 +0200852/* L3 requests establishment of data link */
853static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
854{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200855 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
856 uint8_t chan_nr = rllh->chan_nr;
857 uint8_t link_id = rllh->link_id;
858 uint8_t sapi = rllh->link_id & 7;
859 struct tlv_parsed tv;
860 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100861 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200862 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200863
rootaf48bed2011-09-26 11:23:06 +0200864 /* Set LAPDm context for established connection */
865 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200866
rootaf48bed2011-09-26 11:23:06 +0200867 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200868 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200869 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200870 /* contention resolution establishment procedure */
871 if (sapi != 0) {
872 /* According to clause 6, the contention resolution
873 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200874 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200875 "resolution (discarding)\n");
876 msgb_free(msg);
877 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
878 }
879 /* transmit a SABM command with the P bit set to "1". The SABM
880 * command shall contain the layer 3 message unit */
881 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200882 } else {
883 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200884 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200885 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200886 }
887
888 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100889 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200890 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100891 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200892 msgb_free(msg);
893 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
894 }
895
rootaf48bed2011-09-26 11:23:06 +0200896 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100897 msgb_pull_to_l3(msg);
898 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200899
rootaf48bed2011-09-26 11:23:06 +0200900 /* prepare prim */
901 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200902
rootaf48bed2011-09-26 11:23:06 +0200903 /* send to L2 */
904 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200905}
906
907/* L3 requests transfer of unnumbered information */
908static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
909{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100910 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200911 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
912 uint8_t chan_nr = rllh->chan_nr;
913 uint8_t link_id = rllh->link_id;
914 uint8_t sapi = link_id & 7;
915 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +0100916 int length, ui_bts;
917
918 if (!le) {
919 LOGP(DLLAPD, LOGL_ERROR, "lapdm_datalink without entity error\n");
920 msgb_free(msg);
921 return -EMLINK;
922 }
923 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200924
925 /* check if the layer3 message length exceeds N201 */
926
927 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
928
929 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100930 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200931 }
932 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100933 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200934 }
935 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200936 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200937 "error\n");
938 msgb_free(msg);
939 return -EINVAL;
940 }
rootaf48bed2011-09-26 11:23:06 +0200941 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200942 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
943 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200944 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200945 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200946 "(discarding)\n", length,
947 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200948 msgb_free(msg);
949 return -EIO;
950 }
951
rootaf48bed2011-09-26 11:23:06 +0200952 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100953 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200954
rootaf48bed2011-09-26 11:23:06 +0200955 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100956 msgb_pull_to_l3(msg);
957 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200958
959 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200960 msg->l2h = msgb_push(msg, 2 + !ui_bts);
961 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
962 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100963 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200964 msg->l2h[2] = LAPDm_LEN(length);
965 if (link_id & 0x40) {
966 msg->l2h = msgb_push(msg, 2);
967 msg->l2h[0] = le->tx_power;
968 msg->l2h[1] = le->ta;
969 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200970
971 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200972 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200973}
974
975/* L3 requests transfer of acknowledged information */
976static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
977{
978 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
979 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200980 int length;
981 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200982
983 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
984 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200985 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200986 "error\n");
987 msgb_free(msg);
988 return -EINVAL;
989 }
rootaf48bed2011-09-26 11:23:06 +0200990 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
991 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200992
rootaf48bed2011-09-26 11:23:06 +0200993 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100994 msgb_pull_to_l3(msg);
995 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200996
rootaf48bed2011-09-26 11:23:06 +0200997 /* prepare prim */
998 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200999
rootaf48bed2011-09-26 11:23:06 +02001000 /* send to L2 */
1001 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001002}
1003
1004/* L3 requests suspension of data link */
1005static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1006{
1007 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1008 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001009 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001010
1011 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +02001012 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001013 msgb_free(msg);
1014 return -EINVAL;
1015 }
1016
rootaf48bed2011-09-26 11:23:06 +02001017 /* prepare prim */
1018 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001019
rootaf48bed2011-09-26 11:23:06 +02001020 /* send to L2 */
1021 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001022}
1023
1024/* L3 requests resume of data link */
1025static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1026{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001027 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001028 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001029 uint8_t chan_nr = rllh->chan_nr;
1030 uint8_t link_id = rllh->link_id;
1031 uint8_t sapi = rllh->link_id & 7;
1032 struct tlv_parsed tv;
1033 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001034 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001035 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001036
rootaf48bed2011-09-26 11:23:06 +02001037 /* Set LAPDm context for established connection */
1038 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001039
1040 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1041 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +02001042 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001043 msgb_free(msg);
1044 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1045 }
rootaf48bed2011-09-26 11:23:06 +02001046 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001047 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1048
rootaf48bed2011-09-26 11:23:06 +02001049 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001050 msgb_pull_to_l3(msg);
1051 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001052
rootaf48bed2011-09-26 11:23:06 +02001053 /* prepare prim */
1054 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1055 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001056
rootaf48bed2011-09-26 11:23:06 +02001057 /* send to L2 */
1058 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001059}
1060
1061/* L3 requests release of data link */
1062static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1063{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001064 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001065 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001066 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001067
1068 /* get release mode */
1069 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1070 mode = rllh->data[1] & 1;
1071
Harald Welte1f0b8c22011-06-27 10:51:37 +02001072 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001073 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001074
1075 /* 04.06 3.8.3: No information field is permitted with the DISC
1076 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001077 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001078
rootaf48bed2011-09-26 11:23:06 +02001079 /* prepare prim */
1080 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1081 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001082
rootaf48bed2011-09-26 11:23:06 +02001083 /* send to L2 */
1084 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001085}
1086
1087/* L3 requests channel in idle state */
1088static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1089{
1090 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1091 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1092 struct osmo_phsap_prim pp;
1093
1094 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1095 PRIM_OP_REQUEST, NULL);
1096
1097 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001098 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001099 return -EINVAL;
1100 }
1101 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001102 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001103 return -EINVAL;
1104 }
1105 pp.u.rach_req.ra = cch->data[1];
1106 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1107 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1108
1109 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001110 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001111 return -EINVAL;
1112 }
1113 /* TA = 0 - delay */
1114 pp.u.rach_req.ta = 0 - cch->data[5];
1115
1116 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001117 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001118 return -EINVAL;
1119 }
1120 pp.u.rach_req.tx_power = cch->data[7];
1121
1122 msgb_free(msg);
1123
1124 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1125}
1126
1127/* L1 confirms channel request */
1128static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1129{
1130 struct abis_rsl_cchan_hdr *ch;
1131 struct gsm_time tm;
1132 struct gsm48_req_ref *ref;
1133
1134 gsm_fn2gsmtime(&tm, frame_nr);
1135
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001136 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001137 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1138 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1139 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1140 ch->chan_nr = RSL_CHAN_RACH;
1141 ch->data[0] = RSL_IE_REQ_REFERENCE;
1142 ref = (struct gsm48_req_ref *) (ch->data + 1);
1143 ref->t1 = tm.t1;
1144 ref->t2 = tm.t2;
1145 ref->t3_low = tm.t3 & 0x7;
1146 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001147
Harald Welte1f0b8c22011-06-27 10:51:37 +02001148 return rslms_sendmsg(msg, le);
1149}
1150
Harald Welte1f0b8c22011-06-27 10:51:37 +02001151/* incoming RSLms RLL message from L3 */
1152static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1153{
1154 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1155 int msg_type = rllh->c.msg_type;
1156 uint8_t sapi = rllh->link_id & 7;
1157 struct lapdm_entity *le;
1158 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001159 int rc = 0;
1160
1161 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001162 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001163 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001164 return -EINVAL;
1165 }
1166
1167 if (rllh->link_id & 0x40)
1168 le = &lc->lapdm_acch;
1169 else
1170 le = &lc->lapdm_dcch;
1171
Harald Welte1a87c1b2015-12-14 15:26:07 +01001172 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1173 * BTS side */
1174 if (le->mode == LAPDM_MODE_BTS) {
1175 switch (msg_type) {
1176 case RSL_MT_SUSP_REQ:
1177 case RSL_MT_RES_REQ:
1178 case RSL_MT_RECON_REQ:
1179 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1180 lc->name, rsl_msg_name(msg_type));
1181 msgb_free(msg);
1182 return -EINVAL;
1183 break;
1184 default:
1185 break;
1186 }
1187 }
1188
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001189 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001190 * SAPI.
1191 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001192 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001193 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001194 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001195 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001196 return -EINVAL;
1197 }
1198
Daniel Willmanne5233922012-12-25 23:15:50 +01001199 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001200 case RSL_MT_DATA_REQ:
1201 case RSL_MT_SUSP_REQ:
1202 case RSL_MT_REL_REQ:
1203 /* This is triggered in abnormal error conditions where
1204 * set_lapdm_context() was not called for the channel earlier. */
1205 if (!dl->dl.lctx.dl) {
1206 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1207 lc->name, rsl_msg_name(msg_type), sapi);
1208 msgb_free(msg);
1209 return -EINVAL;
1210 }
1211 break;
1212 default:
1213 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1214 lc->name, rsl_msg_name(msg_type), sapi);
1215 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001216
rootaf48bed2011-09-26 11:23:06 +02001217 switch (msg_type) {
1218 case RSL_MT_UNIT_DATA_REQ:
1219 rc = rslms_rx_rll_udata_req(msg, dl);
1220 break;
1221 case RSL_MT_EST_REQ:
1222 rc = rslms_rx_rll_est_req(msg, dl);
1223 break;
1224 case RSL_MT_DATA_REQ:
1225 rc = rslms_rx_rll_data_req(msg, dl);
1226 break;
1227 case RSL_MT_SUSP_REQ:
1228 rc = rslms_rx_rll_susp_req(msg, dl);
1229 break;
1230 case RSL_MT_RES_REQ:
1231 rc = rslms_rx_rll_res_req(msg, dl);
1232 break;
1233 case RSL_MT_RECON_REQ:
1234 rc = rslms_rx_rll_res_req(msg, dl);
1235 break;
1236 case RSL_MT_REL_REQ:
1237 rc = rslms_rx_rll_rel_req(msg, dl);
1238 break;
1239 default:
1240 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001241 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001242 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001243 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001244
1245 return rc;
1246}
1247
1248/* incoming RSLms COMMON CHANNEL message from L3 */
1249static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1250{
1251 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1252 int msg_type = cch->c.msg_type;
1253 int rc = 0;
1254
1255 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001256 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001257 return -EINVAL;
1258 }
1259
1260 switch (msg_type) {
1261 case RSL_MT_CHAN_RQD:
1262 /* create and send RACH request */
1263 rc = rslms_rx_chan_rqd(lc, msg);
1264 break;
1265 default:
rootaf48bed2011-09-26 11:23:06 +02001266 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001267 msg_type);
1268 msgb_free(msg);
1269 return 0;
1270 }
1271
1272 return rc;
1273}
1274
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001275/*! Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001276int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1277{
1278 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1279 int rc = 0;
1280
1281 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001282 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001283 return -EINVAL;
1284 }
1285
1286 switch (rslh->msg_discr & 0xfe) {
1287 case ABIS_RSL_MDISC_RLL:
1288 rc = rslms_rx_rll(msg, lc);
1289 break;
1290 case ABIS_RSL_MDISC_COM_CHAN:
1291 rc = rslms_rx_com_chan(msg, lc);
1292 break;
1293 default:
rootaf48bed2011-09-26 11:23:06 +02001294 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001295 "discriminator 0x%02x", rslh->msg_discr);
1296 msgb_free(msg);
1297 return -EINVAL;
1298 }
1299
1300 return rc;
1301}
1302
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001303/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001304int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1305{
rootaf48bed2011-09-26 11:23:06 +02001306 int i;
1307 enum lapd_mode lm;
1308
Harald Welte1f0b8c22011-06-27 10:51:37 +02001309 switch (mode) {
1310 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001311 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001312 break;
1313 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001314 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001315 break;
1316 default:
1317 return -EINVAL;
1318 }
1319
rootaf48bed2011-09-26 11:23:06 +02001320 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1321 lapd_set_mode(&le->datalink[i].dl, lm);
1322 }
1323
Harald Welte1f0b8c22011-06-27 10:51:37 +02001324 le->mode = mode;
1325
1326 return 0;
1327}
1328
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001329/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001330int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1331{
1332 int rc;
1333
1334 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1335 if (rc < 0)
1336 return rc;
1337
1338 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1339}
1340
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001341/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001342void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1343{
1344 lc->lapdm_dcch.l1_prim_cb = cb;
1345 lc->lapdm_acch.l1_prim_cb = cb;
1346 lc->lapdm_dcch.l1_ctx = ctx;
1347 lc->lapdm_acch.l1_ctx = ctx;
1348}
1349
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001350/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001351void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1352{
1353 lc->lapdm_dcch.l3_cb = cb;
1354 lc->lapdm_acch.l3_cb = cb;
1355 lc->lapdm_dcch.l3_ctx = ctx;
1356 lc->lapdm_acch.l3_ctx = ctx;
1357}
1358
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001359/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001360void lapdm_entity_reset(struct lapdm_entity *le)
1361{
1362 struct lapdm_datalink *dl;
1363 int i;
1364
1365 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1366 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001367 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001368 }
1369}
1370
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001371/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001372void lapdm_channel_reset(struct lapdm_channel *lc)
1373{
1374 lapdm_entity_reset(&lc->lapdm_dcch);
1375 lapdm_entity_reset(&lc->lapdm_acch);
1376}
1377
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001378/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001379void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1380{
1381 le->flags = flags;
1382}
1383
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001384/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001385void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1386{
1387 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1388 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1389}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001390
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001391/*! @} */