blob: 1e81bff7093ac1ddeececb9cf9e57a5260c19ca3 [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);
388 struct abis_rsl_rll_hdr *rllh;
389
390 /* Add the RSL + RLL header */
391 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
392 msgb_push(msg, 2 + 2);
393 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
394 mctx->link_id, 1);
395 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
396
397 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
398 rllh->data[1] = mctx->ta_ind;
399
400 rllh->data[2] = RSL_IE_MS_POWER;
401 rllh->data[3] = mctx->tx_power_ind;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100402
Harald Welte1f0b8c22011-06-27 10:51:37 +0200403 return rslms_sendmsg(msg, mctx->dl->entity);
404}
405
406static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
407{
408 struct msgb *msg;
Harald Welte542301b2018-04-19 16:11:14 +0200409 int transparent = rsl_is_transparent(msg_type);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200410
Harald Welte542301b2018-04-19 16:11:14 +0200411 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200412
413 /* send off the RSLms message to L3 */
414 return rslms_sendmsg(msg, mctx->dl->entity);
415}
416
417static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
418{
419 struct msgb *msg;
420
rootaf48bed2011-09-26 11:23:06 +0200421 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte542301b2018-04-19 16:11:14 +0200422 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200423 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
424 return rslms_sendmsg(msg, mctx->dl->entity);
425}
426
rootaf48bed2011-09-26 11:23:06 +0200427/* DLSAP L2 -> L3 (RSLms) */
428static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
429 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200430{
rootaf48bed2011-09-26 11:23:06 +0200431 struct lapd_datalink *dl = lctx->dl;
432 struct lapdm_datalink *mdl =
433 container_of(dl, struct lapdm_datalink, dl);
434 struct lapdm_msg_ctx *mctx = &mdl->mctx;
435 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200436
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200437 switch (OSMO_PRIM_HDR(&dp->oph)) {
438 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
439 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200440 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200441 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
442 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200443 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200444 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
445 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200446 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200447 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
448 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
449 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
450 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200451 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200452 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
453 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200454 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200455 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
456 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200457 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200458 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
459 rsl_rll_error(dp->u.error_ind.cause, mctx);
460 if (dp->oph.msg)
461 msgb_free(dp->oph.msg);
462 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200463 }
rootaf48bed2011-09-26 11:23:06 +0200464
465 if (!rll_msg) {
466 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
467 "fix!\n", dp->oph.primitive, dp->oph.operation);
468 return -EINVAL;
469 }
470
471 if (!dp->oph.msg)
472 return send_rll_simple(rll_msg, mctx);
473
474 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200475}
476
rootaf48bed2011-09-26 11:23:06 +0200477/* send a data frame to layer 1 */
478static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200479{
rootaf48bed2011-09-26 11:23:06 +0200480 uint8_t l3_len = msg->tail - msg->data;
481 struct lapd_datalink *dl = lctx->dl;
482 struct lapdm_datalink *mdl =
483 container_of(dl, struct lapdm_datalink, dl);
484 struct lapdm_msg_ctx *mctx = &mdl->mctx;
485 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200486
rootaf48bed2011-09-26 11:23:06 +0200487 /* prepend l2 header */
488 msg->l2h = msgb_push(msg, 3);
489 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
490 /* EA is set here too */
491 switch (format) {
492 case LAPD_FORM_I:
493 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
494 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200495 break;
rootaf48bed2011-09-26 11:23:06 +0200496 case LAPD_FORM_S:
497 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200498 break;
rootaf48bed2011-09-26 11:23:06 +0200499 case LAPD_FORM_U:
500 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200501 break;
502 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200503 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200504 return -EINVAL;
505 }
rootaf48bed2011-09-26 11:23:06 +0200506 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
507 if (lctx->more)
508 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200509
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100510 /* add ACCH header with last indicated tx-power and TA */
511 if ((mctx->link_id & 0x40)) {
512 struct lapdm_entity *le = mdl->entity;
513
514 msg->l2h = msgb_push(msg, 2);
515 msg->l2h[0] = le->tx_power;
516 msg->l2h[1] = le->ta;
517 }
518
rootaf48bed2011-09-26 11:23:06 +0200519 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
520 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200521}
522
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100523static int update_pending_frames(struct lapd_msg_ctx *lctx)
524{
525 struct lapd_datalink *dl = lctx->dl;
526 struct msgb *msg;
527 int rc = -1;
528
529 llist_for_each_entry(msg, &dl->tx_queue, list) {
530 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
531 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
532 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
533 rc = 0;
534 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
535 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
536 }
537 }
538
539 return rc;
540}
541
Harald Welte1284c3e2018-05-01 18:11:02 +0200542/* determine if receiving a given LAPDm message is not permitted */
543static int lapdm_rx_not_permitted(const struct lapdm_entity *le,
544 const struct lapd_msg_ctx *lctx)
545{
546 /* we currently only implement SABM related checks here */
547 if (lctx->format != LAPD_FORM_U || lctx->s_u != LAPD_U_SABM)
548 return 0;
549
550 if (le->mode == LAPDM_MODE_BTS) {
551 if (le == &le->lapdm_ch->lapdm_acch) {
552 /* no contention resolution on SACCH */
553 if (lctx->length > 0)
554 return RLL_CAUSE_SABM_INFO_NOTALL;
555 } else {
556 switch (lctx->sapi) {
557 case 0:
558 /* SAPI0 must use contention resolution, i.e. L3 payload must exist */
559 if (lctx->length == 0)
560 return RLL_CAUSE_UFRM_INC_PARAM;
561 break;
562 case 3:
563 /* SAPI3 doesn't support contention resolution */
564 if (lctx->length > 0)
565 return RLL_CAUSE_SABM_INFO_NOTALL;
566 break;
567 }
568 }
569 } else if (le->mode == LAPDM_MODE_MS) {
570 /* contention resolution (L3 present) is only sent by MS, but
571 * never received by it */
572 if (lctx->length > 0)
573 return RLL_CAUSE_SABM_INFO_NOTALL;
574 }
575 return 0;
576}
577
Harald Welte1f0b8c22011-06-27 10:51:37 +0200578/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200579static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
580 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200581{
582 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200583 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200584 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200585 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200586 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200587 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200588
589 /* when we reach here, we have a msgb with l2h pointing to the raw
590 * 23byte mac block. The l1h has already been purged. */
591
rootaf48bed2011-09-26 11:23:06 +0200592 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200593 mctx.chan_nr = chan_nr;
594 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200595
Harald Welte1f0b8c22011-06-27 10:51:37 +0200596 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
597 if (cbits == 0x10 || cbits == 0x12) {
598 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
599 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200600 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200601 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200602 } else {
603 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200604 /* It was received from network on SACCH */
605
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100606 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
607 if (le->mode == LAPDM_MODE_MS
608 && LAPDm_CTRL_is_U(msg->l2h[3])
609 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200610 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200611 n201 = N201_B4;
612 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200613 } else {
614 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200615 n201 = N201_AB_SACCH;
616 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200617 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200618 /* SACCH frames have a two-byte L1 header that
619 * OsmocomBB L1 doesn't strip */
620 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
621 mctx.ta_ind = msg->l2h[1];
622 msgb_pull(msg, 2);
623 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200624 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200625 } else {
626 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200627 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100628 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200629 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200630 }
631 }
632
Daniel Willmann55405fb2014-03-26 13:45:17 +0100633 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200634 /* G.2.1 No action on frames containing an unallocated SAPI. */
635 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200636 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200637 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200638 msgb_free(msg);
639 return -EIO;
640 }
641
Harald Welte1f0b8c22011-06-27 10:51:37 +0200642 switch (mctx.lapdm_fmt) {
643 case LAPDm_FMT_A:
644 case LAPDm_FMT_B:
645 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200646 lctx.dl = &mctx.dl->dl;
647 /* obtain SAPI from address field */
648 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
649 /* G.2.3 EA bit set to "0" is not allowed in GSM */
650 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
651 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
652 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200653 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200654 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200655 return -EINVAL;
656 }
rootaf48bed2011-09-26 11:23:06 +0200657 /* adress field */
658 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
659 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
660 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
661 /* command field */
662 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
663 lctx.format = LAPD_FORM_I;
664 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
665 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
666 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
667 lctx.format = LAPD_FORM_S;
668 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
669 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
670 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
671 lctx.format = LAPD_FORM_U;
672 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
673 } else
674 lctx.format = LAPD_FORM_UKN;
675 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
676 if (lctx.sapi != LAPDm_SAPI_NORMAL
677 && lctx.sapi != LAPDm_SAPI_SMS
678 && lctx.format == LAPD_FORM_U
679 && lctx.s_u == LAPDm_U_UI) {
680 /* 5.3.3 UI frames with invalid SAPI values shall be
681 * discarded
682 */
683 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
684 lctx.sapi);
685 msgb_free(msg);
686 return 0;
687 }
688 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
689 lctx.n201 = n201;
690 lctx.length = n201;
691 lctx.more = 0;
692 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100693 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200694 } else {
695 /* length field */
696 if (!(msg->l2h[2] & LAPDm_EL)) {
697 /* G.4.1 If the EL bit is set to "0", an
698 * MDL-ERROR-INDICATION primitive with cause
699 * "frame not implemented" is sent to the
700 * mobile management entity. */
701 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
702 "multi-octet length\n");
703 msgb_free(msg);
704 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
705 return -EINVAL;
706 }
707 lctx.n201 = n201;
708 lctx.length = msg->l2h[2] >> 2;
709 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
710 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100711 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200712 }
713 /* store context for messages from lapd */
714 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200715 rc =lapdm_rx_not_permitted(le, &lctx);
716 if (rc > 0) {
717 LOGP(DLLAPD, LOGL_NOTICE, "received message not permitted");
718 msgb_free(msg);
719 rsl_rll_error(rc, &mctx);
720 return -EINVAL;
721 }
rootaf48bed2011-09-26 11:23:06 +0200722 /* send to LAPD */
723 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200724 break;
725 case LAPDm_FMT_Bter:
726 /* FIXME */
727 msgb_free(msg);
728 break;
729 case LAPDm_FMT_Bbis:
730 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200731 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200732 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100733 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200734 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
735 break;
736 default:
737 msgb_free(msg);
738 }
739
740 return rc;
741}
742
743/* input into layer2 (from layer 1) */
744static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
745{
746 struct abis_rsl_cchan_hdr *ch;
747 struct gsm48_req_ref req_ref;
748 struct gsm_time gt;
749 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
750
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200751 if (!msg)
752 return -ENOMEM;
753
Harald Welte1f0b8c22011-06-27 10:51:37 +0200754 msg->l2h = msgb_push(msg, sizeof(*ch));
755 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
756 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
757 ch->chan_nr = RSL_CHAN_RACH;
758
759 /* generate a RSL CHANNEL REQUIRED message */
760 gsm_fn2gsmtime(&gt, fn);
761 req_ref.ra = ra;
762 req_ref.t1 = gt.t1; /* FIXME: modulo? */
763 req_ref.t2 = gt.t2;
764 req_ref.t3_low = gt.t3 & 7;
765 req_ref.t3_high = gt.t3 >> 3;
766
767 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
768 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
769
770 return rslms_sendmsg(msg, le);
771}
772
773static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
774
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200775/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200776int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
777{
778 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
779 int rc = 0;
780
781 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200782 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200783 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100784 rc = -ENODEV;
785 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200786 }
787
788 switch (oph->primitive) {
789 case PRIM_PH_DATA:
790 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200791 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200792 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100793 rc = -ENODEV;
794 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200795 }
796 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
797 pp->u.data.link_id);
798 break;
799 case PRIM_PH_RTS:
800 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200801 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200802 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100803 rc = -ENODEV;
804 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200805 }
806 rc = l2_ph_data_conf(oph->msg, le);
807 break;
808 case PRIM_PH_RACH:
809 switch (oph->operation) {
810 case PRIM_OP_INDICATION:
811 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
812 pp->u.rach_ind.acc_delay);
813 break;
814 case PRIM_OP_CONFIRM:
815 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
816 break;
817 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100818 rc = -EIO;
819 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200820 }
821 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100822 default:
823 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
824 oph->primitive);
825 rc = -EINVAL;
826 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200827 }
828
829 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100830
831free:
832 msgb_free(oph->msg);
833 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200834}
835
836
837/* L3 -> L2 / RSLMS -> LAPDm */
838
rootaf48bed2011-09-26 11:23:06 +0200839/* Set LAPDm context for established connection */
840static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
841 uint8_t link_id, int n201, uint8_t sapi)
842{
843 memset(&dl->mctx, 0, sizeof(dl->mctx));
844 dl->mctx.dl = dl;
845 dl->mctx.chan_nr = chan_nr;
846 dl->mctx.link_id = link_id;
847 dl->dl.lctx.dl = &dl->dl;
848 dl->dl.lctx.n201 = n201;
849 dl->dl.lctx.sapi = sapi;
850
851 return 0;
852}
853
Harald Welte1f0b8c22011-06-27 10:51:37 +0200854/* L3 requests establishment of data link */
855static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
856{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200857 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
858 uint8_t chan_nr = rllh->chan_nr;
859 uint8_t link_id = rllh->link_id;
860 uint8_t sapi = rllh->link_id & 7;
861 struct tlv_parsed tv;
862 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100863 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200864 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200865
rootaf48bed2011-09-26 11:23:06 +0200866 /* Set LAPDm context for established connection */
867 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200868
rootaf48bed2011-09-26 11:23:06 +0200869 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200870 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200871 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200872 /* contention resolution establishment procedure */
873 if (sapi != 0) {
874 /* According to clause 6, the contention resolution
875 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200876 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200877 "resolution (discarding)\n");
878 msgb_free(msg);
879 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
880 }
881 /* transmit a SABM command with the P bit set to "1". The SABM
882 * command shall contain the layer 3 message unit */
883 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200884 } else {
885 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200886 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200887 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200888 }
889
890 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100891 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200892 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100893 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200894 msgb_free(msg);
895 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
896 }
897
rootaf48bed2011-09-26 11:23:06 +0200898 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100899 msgb_pull_to_l3(msg);
900 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200901
rootaf48bed2011-09-26 11:23:06 +0200902 /* prepare prim */
903 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200904
rootaf48bed2011-09-26 11:23:06 +0200905 /* send to L2 */
906 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200907}
908
909/* L3 requests transfer of unnumbered information */
910static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
911{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100912 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200913 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
914 uint8_t chan_nr = rllh->chan_nr;
915 uint8_t link_id = rllh->link_id;
916 uint8_t sapi = link_id & 7;
917 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +0100918 int length, ui_bts;
919
920 if (!le) {
921 LOGP(DLLAPD, LOGL_ERROR, "lapdm_datalink without entity error\n");
922 msgb_free(msg);
923 return -EMLINK;
924 }
925 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200926
927 /* check if the layer3 message length exceeds N201 */
928
929 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
930
931 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100932 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200933 }
934 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100935 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200936 }
937 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200938 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200939 "error\n");
940 msgb_free(msg);
941 return -EINVAL;
942 }
rootaf48bed2011-09-26 11:23:06 +0200943 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200944 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
945 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200946 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200947 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200948 "(discarding)\n", length,
949 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200950 msgb_free(msg);
951 return -EIO;
952 }
953
rootaf48bed2011-09-26 11:23:06 +0200954 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100955 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200956
rootaf48bed2011-09-26 11:23:06 +0200957 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100958 msgb_pull_to_l3(msg);
959 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200960
961 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200962 msg->l2h = msgb_push(msg, 2 + !ui_bts);
963 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
964 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100965 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200966 msg->l2h[2] = LAPDm_LEN(length);
967 if (link_id & 0x40) {
968 msg->l2h = msgb_push(msg, 2);
969 msg->l2h[0] = le->tx_power;
970 msg->l2h[1] = le->ta;
971 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200972
973 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200974 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200975}
976
977/* L3 requests transfer of acknowledged information */
978static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
979{
980 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
981 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200982 int length;
983 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200984
985 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
986 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200987 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200988 "error\n");
989 msgb_free(msg);
990 return -EINVAL;
991 }
rootaf48bed2011-09-26 11:23:06 +0200992 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
993 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200994
rootaf48bed2011-09-26 11:23:06 +0200995 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100996 msgb_pull_to_l3(msg);
997 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200998
rootaf48bed2011-09-26 11:23:06 +0200999 /* prepare prim */
1000 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001001
rootaf48bed2011-09-26 11:23:06 +02001002 /* send to L2 */
1003 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001004}
1005
1006/* L3 requests suspension of data link */
1007static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1008{
1009 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1010 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001011 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001012
1013 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +02001014 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001015 msgb_free(msg);
1016 return -EINVAL;
1017 }
1018
rootaf48bed2011-09-26 11:23:06 +02001019 /* prepare prim */
1020 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001021
rootaf48bed2011-09-26 11:23:06 +02001022 /* send to L2 */
1023 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001024}
1025
1026/* L3 requests resume of data link */
1027static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1028{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001029 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001030 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001031 uint8_t chan_nr = rllh->chan_nr;
1032 uint8_t link_id = rllh->link_id;
1033 uint8_t sapi = rllh->link_id & 7;
1034 struct tlv_parsed tv;
1035 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001036 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001037 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001038
rootaf48bed2011-09-26 11:23:06 +02001039 /* Set LAPDm context for established connection */
1040 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001041
1042 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1043 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +02001044 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001045 msgb_free(msg);
1046 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1047 }
rootaf48bed2011-09-26 11:23:06 +02001048 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001049 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1050
rootaf48bed2011-09-26 11:23:06 +02001051 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001052 msgb_pull_to_l3(msg);
1053 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001054
rootaf48bed2011-09-26 11:23:06 +02001055 /* prepare prim */
1056 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1057 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001058
rootaf48bed2011-09-26 11:23:06 +02001059 /* send to L2 */
1060 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001061}
1062
1063/* L3 requests release of data link */
1064static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1065{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001066 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001067 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001068 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001069
1070 /* get release mode */
1071 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1072 mode = rllh->data[1] & 1;
1073
Harald Welte1f0b8c22011-06-27 10:51:37 +02001074 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001075 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001076
1077 /* 04.06 3.8.3: No information field is permitted with the DISC
1078 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001079 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001080
rootaf48bed2011-09-26 11:23:06 +02001081 /* prepare prim */
1082 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1083 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001084
rootaf48bed2011-09-26 11:23:06 +02001085 /* send to L2 */
1086 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001087}
1088
1089/* L3 requests channel in idle state */
1090static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1091{
1092 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1093 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1094 struct osmo_phsap_prim pp;
1095
1096 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1097 PRIM_OP_REQUEST, NULL);
1098
1099 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001100 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001101 return -EINVAL;
1102 }
1103 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001104 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001105 return -EINVAL;
1106 }
1107 pp.u.rach_req.ra = cch->data[1];
1108 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1109 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1110
1111 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001112 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001113 return -EINVAL;
1114 }
1115 /* TA = 0 - delay */
1116 pp.u.rach_req.ta = 0 - cch->data[5];
1117
1118 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001119 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001120 return -EINVAL;
1121 }
1122 pp.u.rach_req.tx_power = cch->data[7];
1123
1124 msgb_free(msg);
1125
1126 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1127}
1128
1129/* L1 confirms channel request */
1130static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1131{
1132 struct abis_rsl_cchan_hdr *ch;
1133 struct gsm_time tm;
1134 struct gsm48_req_ref *ref;
1135
1136 gsm_fn2gsmtime(&tm, frame_nr);
1137
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001138 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001139 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1140 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1141 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1142 ch->chan_nr = RSL_CHAN_RACH;
1143 ch->data[0] = RSL_IE_REQ_REFERENCE;
1144 ref = (struct gsm48_req_ref *) (ch->data + 1);
1145 ref->t1 = tm.t1;
1146 ref->t2 = tm.t2;
1147 ref->t3_low = tm.t3 & 0x7;
1148 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001149
Harald Welte1f0b8c22011-06-27 10:51:37 +02001150 return rslms_sendmsg(msg, le);
1151}
1152
Harald Welte1f0b8c22011-06-27 10:51:37 +02001153/* incoming RSLms RLL message from L3 */
1154static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1155{
1156 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1157 int msg_type = rllh->c.msg_type;
1158 uint8_t sapi = rllh->link_id & 7;
1159 struct lapdm_entity *le;
1160 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001161 int rc = 0;
1162
1163 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001164 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001165 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001166 return -EINVAL;
1167 }
1168
1169 if (rllh->link_id & 0x40)
1170 le = &lc->lapdm_acch;
1171 else
1172 le = &lc->lapdm_dcch;
1173
Harald Welte1a87c1b2015-12-14 15:26:07 +01001174 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1175 * BTS side */
1176 if (le->mode == LAPDM_MODE_BTS) {
1177 switch (msg_type) {
1178 case RSL_MT_SUSP_REQ:
1179 case RSL_MT_RES_REQ:
1180 case RSL_MT_RECON_REQ:
1181 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1182 lc->name, rsl_msg_name(msg_type));
1183 msgb_free(msg);
1184 return -EINVAL;
1185 break;
1186 default:
1187 break;
1188 }
1189 }
1190
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001191 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001192 * SAPI.
1193 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001194 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001195 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001196 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001197 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001198 return -EINVAL;
1199 }
1200
Daniel Willmanne5233922012-12-25 23:15:50 +01001201 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001202 case RSL_MT_DATA_REQ:
1203 case RSL_MT_SUSP_REQ:
1204 case RSL_MT_REL_REQ:
1205 /* This is triggered in abnormal error conditions where
1206 * set_lapdm_context() was not called for the channel earlier. */
1207 if (!dl->dl.lctx.dl) {
1208 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1209 lc->name, rsl_msg_name(msg_type), sapi);
1210 msgb_free(msg);
1211 return -EINVAL;
1212 }
1213 break;
1214 default:
1215 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1216 lc->name, rsl_msg_name(msg_type), sapi);
1217 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001218
rootaf48bed2011-09-26 11:23:06 +02001219 switch (msg_type) {
1220 case RSL_MT_UNIT_DATA_REQ:
1221 rc = rslms_rx_rll_udata_req(msg, dl);
1222 break;
1223 case RSL_MT_EST_REQ:
1224 rc = rslms_rx_rll_est_req(msg, dl);
1225 break;
1226 case RSL_MT_DATA_REQ:
1227 rc = rslms_rx_rll_data_req(msg, dl);
1228 break;
1229 case RSL_MT_SUSP_REQ:
1230 rc = rslms_rx_rll_susp_req(msg, dl);
1231 break;
1232 case RSL_MT_RES_REQ:
1233 rc = rslms_rx_rll_res_req(msg, dl);
1234 break;
1235 case RSL_MT_RECON_REQ:
1236 rc = rslms_rx_rll_res_req(msg, dl);
1237 break;
1238 case RSL_MT_REL_REQ:
1239 rc = rslms_rx_rll_rel_req(msg, dl);
1240 break;
1241 default:
1242 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001243 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001244 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001245 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001246
1247 return rc;
1248}
1249
1250/* incoming RSLms COMMON CHANNEL message from L3 */
1251static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1252{
1253 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1254 int msg_type = cch->c.msg_type;
1255 int rc = 0;
1256
1257 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001258 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001259 return -EINVAL;
1260 }
1261
1262 switch (msg_type) {
1263 case RSL_MT_CHAN_RQD:
1264 /* create and send RACH request */
1265 rc = rslms_rx_chan_rqd(lc, msg);
1266 break;
1267 default:
rootaf48bed2011-09-26 11:23:06 +02001268 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001269 msg_type);
1270 msgb_free(msg);
1271 return 0;
1272 }
1273
1274 return rc;
1275}
1276
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001277/*! Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001278int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1279{
1280 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1281 int rc = 0;
1282
1283 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001284 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001285 return -EINVAL;
1286 }
1287
1288 switch (rslh->msg_discr & 0xfe) {
1289 case ABIS_RSL_MDISC_RLL:
1290 rc = rslms_rx_rll(msg, lc);
1291 break;
1292 case ABIS_RSL_MDISC_COM_CHAN:
1293 rc = rslms_rx_com_chan(msg, lc);
1294 break;
1295 default:
rootaf48bed2011-09-26 11:23:06 +02001296 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001297 "discriminator 0x%02x", rslh->msg_discr);
1298 msgb_free(msg);
1299 return -EINVAL;
1300 }
1301
1302 return rc;
1303}
1304
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001305/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001306int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1307{
rootaf48bed2011-09-26 11:23:06 +02001308 int i;
1309 enum lapd_mode lm;
1310
Harald Welte1f0b8c22011-06-27 10:51:37 +02001311 switch (mode) {
1312 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001313 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001314 break;
1315 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001316 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001317 break;
1318 default:
1319 return -EINVAL;
1320 }
1321
rootaf48bed2011-09-26 11:23:06 +02001322 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1323 lapd_set_mode(&le->datalink[i].dl, lm);
1324 }
1325
Harald Welte1f0b8c22011-06-27 10:51:37 +02001326 le->mode = mode;
1327
1328 return 0;
1329}
1330
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001331/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001332int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1333{
1334 int rc;
1335
1336 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1337 if (rc < 0)
1338 return rc;
1339
1340 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1341}
1342
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001343/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001344void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1345{
1346 lc->lapdm_dcch.l1_prim_cb = cb;
1347 lc->lapdm_acch.l1_prim_cb = cb;
1348 lc->lapdm_dcch.l1_ctx = ctx;
1349 lc->lapdm_acch.l1_ctx = ctx;
1350}
1351
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001352/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001353void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1354{
1355 lc->lapdm_dcch.l3_cb = cb;
1356 lc->lapdm_acch.l3_cb = cb;
1357 lc->lapdm_dcch.l3_ctx = ctx;
1358 lc->lapdm_acch.l3_ctx = ctx;
1359}
1360
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001361/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001362void lapdm_entity_reset(struct lapdm_entity *le)
1363{
1364 struct lapdm_datalink *dl;
1365 int i;
1366
1367 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1368 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001369 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001370 }
1371}
1372
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001373/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001374void lapdm_channel_reset(struct lapdm_channel *lc)
1375{
1376 lapdm_entity_reset(&lc->lapdm_dcch);
1377 lapdm_entity_reset(&lc->lapdm_acch);
1378}
1379
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001380/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001381void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1382{
1383 le->flags = flags;
1384}
1385
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001386/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001387void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1388{
1389 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1390 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1391}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001392
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001393/*! @} */