blob: 6a16012bd673c8b1fab1b9a75381943f88016696 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file lapdm.c
2 * GSM LAPDm (TS 04.06) implementation. */
3/*
Harald Weltee08da972017-11-13 01:00:26 +09004 * (C) 2010-2017 by Harald Welte <laforge@gnumonks.org>
rootaf48bed2011-09-26 11:23:06 +02005 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
Harald Weltee08da972017-11-13 01:00:26 +09006 * (C) 2014-2016 by sysmocom - s.f.m.c GmbH
Harald Welte1f0b8c22011-06-27 10:51:37 +02007 *
8 * All Rights Reserved
9 *
Harald Weltee08da972017-11-13 01:00:26 +090010 * SPDX-License-Identifier: GPL-2.0+
11 *
Harald Welte1f0b8c22011-06-27 10:51:37 +020012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 */
27
Harald Welte6bdf0b12011-08-17 18:22:08 +020028/*! \addtogroup lapdm
29 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020030 * \file lapdm.c */
Harald Welte6bdf0b12011-08-17 18:22:08 +020031
Harald Welte1f0b8c22011-06-27 10:51:37 +020032#include <stdio.h>
33#include <stdint.h>
34#include <string.h>
35#include <errno.h>
Harald Welte1f0b8c22011-06-27 10:51:37 +020036
37#include <osmocom/core/logging.h>
38#include <osmocom/core/timer.h>
39#include <osmocom/core/msgb.h>
40#include <osmocom/core/utils.h>
41
42#include <osmocom/gsm/tlv.h>
43#include <osmocom/gsm/rsl.h>
44#include <osmocom/gsm/prim.h>
45#include <osmocom/gsm/gsm_utils.h>
46#include <osmocom/gsm/lapdm.h>
47
48#include <osmocom/gsm/protocol/gsm_04_08.h>
49#include <osmocom/gsm/protocol/gsm_08_58.h>
50
51/* TS 04.06 Figure 4 / Section 3.2 */
52#define LAPDm_LPD_NORMAL 0
53#define LAPDm_LPD_SMSCB 1
54#define LAPDm_SAPI_NORMAL 0
55#define LAPDm_SAPI_SMS 3
56#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
57
rootaf48bed2011-09-26 11:23:06 +020058#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020059#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
60#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
61#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
62
63/* TS 04.06 Table 3 / Section 3.4.3 */
64#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
65#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
66#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
67
68#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
69#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
70#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
71
72#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
73#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
74
75#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
76
77#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
78#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
79
Harald Welte1f0b8c22011-06-27 10:51:37 +020080#define LAPDm_LEN(len) ((len << 2) | 0x1)
81#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020082#define LAPDm_EL 0x1
83
84#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020085
86/* TS 04.06 Section 5.8.3 */
87#define N201_AB_SACCH 18
88#define N201_AB_SDCCH 20
89#define N201_AB_FACCH 20
90#define N201_Bbis 23
91#define N201_Bter_SACCH 21
92#define N201_Bter_SDCCH 23
93#define N201_Bter_FACCH 23
94#define N201_B4 19
95
96/* 5.8.2.1 N200 during establish and release */
97#define N200_EST_REL 5
98/* 5.8.2.1 N200 during timer recovery state */
99#define N200_TR_SACCH 5
100#define N200_TR_SDCCH 23
101#define N200_TR_FACCH_FR 34
102#define N200_TR_EFACCH_FR 48
103#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200104/* FIXME: set N200 depending on chan_nr */
105#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200106
107enum lapdm_format {
108 LAPDm_FMT_A,
109 LAPDm_FMT_B,
110 LAPDm_FMT_Bbis,
111 LAPDm_FMT_Bter,
112 LAPDm_FMT_B4,
113};
114
Maxadef12a2016-05-25 15:25:02 +0200115const struct value_string osmo_ph_prim_names[] = {
116 { PRIM_PH_DATA, "PH-DATA" },
117 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
118 { PRIM_PH_CONN, "PH-CONNECT" },
119 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
120 { PRIM_PH_RTS, "PH-RTS" },
121 { PRIM_MPH_INFO, "MPH-INFO" },
122 { PRIM_TCH, "TCH" },
123 { PRIM_TCH_RTS, "TCH-RTS" },
124 { 0, NULL }
125};
126
rootaf48bed2011-09-26 11:23:06 +0200127static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
128static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
129 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100130static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200131
132static void lapdm_dl_init(struct lapdm_datalink *dl,
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100133 struct lapdm_entity *entity, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200134{
135 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200136 dl->entity = entity;
rootaf48bed2011-09-26 11:23:06 +0200137 lapd_dl_init(&dl->dl, 1, 8, 200);
138 dl->dl.reestablish = 0; /* GSM uses no reestablish */
139 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
140 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100141 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200142 dl->dl.n200_est_rel = N200_EST_REL;
143 dl->dl.n200 = N200;
144 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100145 dl->dl.t200_sec = t200; dl->dl.t200_usec = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200146}
147
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200148/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200149 * \param[in] le LAPDm entity
150 * \param[in] mode \ref lapdm_mode (BTS/MS)
151 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100152void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200153{
154 unsigned int i;
155
156 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100157 lapdm_dl_init(&le->datalink[i], le, t200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200158
159 lapdm_entity_set_mode(le, mode);
160}
161
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200162/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200163 * \param[in] lc \ref lapdm_channel to be initialized
164 * \param[in] mode \ref lapdm_mode (BTS/MS)
165 *
166 * This really is a convenience wrapper around calling \ref
167 * lapdm_entity_init twice.
168 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200169void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
170{
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100171 lapdm_entity_init(&lc->lapdm_acch, mode, 2);
rootaf48bed2011-09-26 11:23:06 +0200172 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100173 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200174}
175
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200176/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200177void lapdm_entity_exit(struct lapdm_entity *le)
178{
179 unsigned int i;
180 struct lapdm_datalink *dl;
181
182 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
183 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200184 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200185 }
186}
187
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200188/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200189 *
190 * A convenience wrapper calling \ref lapdm_entity_exit on both
191 * entities inside the \ref lapdm_channel
192 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200193void lapdm_channel_exit(struct lapdm_channel *lc)
194{
195 lapdm_entity_exit(&lc->lapdm_acch);
196 lapdm_entity_exit(&lc->lapdm_dcch);
197}
198
Daniel Willmann55405fb2014-03-26 13:45:17 +0100199struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200200{
201 switch (sapi) {
202 case LAPDm_SAPI_NORMAL:
203 return &le->datalink[0];
204 case LAPDm_SAPI_SMS:
205 return &le->datalink[1];
206 default:
207 return NULL;
208 }
209}
210
Harald Welte1f0b8c22011-06-27 10:51:37 +0200211/* Append padding (if required) */
212static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
213{
214 int pad_len = n201 - msgb_l2len(msg);
215 uint8_t *data;
216
217 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200218 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200219 "cannot pad message that is already too big!\n");
220 return;
221 }
222
223 data = msgb_put(msg, pad_len);
224 memset(data, 0x2B, pad_len);
225}
226
227/* input function that L2 calls when sending messages up to L3 */
228static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
229{
230 if (!le->l3_cb) {
231 msgb_free(msg);
232 return -EIO;
233 }
234
235 /* call the layer2 message handler that is registered */
236 return le->l3_cb(msg, le, le->l3_ctx);
237}
238
239/* write a frame into the tx queue */
240static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200241 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200242{
243 struct lapdm_entity *le = dl->entity;
244 struct osmo_phsap_prim pp;
245
246 /* if there is a pending message, queue it */
247 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200248 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200249 *msgb_push(msg, 1) = link_id;
250 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200251 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200252 return -EBUSY;
253 }
254
255 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
256 PRIM_OP_REQUEST, msg);
257 pp.u.data.chan_nr = chan_nr;
258 pp.u.data.link_id = link_id;
259
260 /* send the frame now */
261 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200262 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200263
264 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
265}
266
267static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
268{
269 struct lapdm_datalink *dl;
270 int last = le->last_tx_dequeue;
271 int i = last, n = ARRAY_SIZE(le->datalink);
272 struct msgb *msg = NULL;
273
274 /* round-robin dequeue */
275 do {
276 /* next */
277 i = (i + 1) % n;
278 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200279 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200280 break;
281 } while (i != last);
282
283 if (msg) {
284 /* Set last dequeue position */
285 le->last_tx_dequeue = i;
286 }
287
288 return msg;
289}
290
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200291/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200292 * a osmo_phsap_prim */
293int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
294{
295 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200296 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200297
298 msg = tx_dequeue_msgb(le);
299 if (!msg)
300 return -ENODEV;
301
302 /* if we have a message, send PH-DATA.req */
303 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
304 PRIM_OP_REQUEST, msg);
305
306 /* Pull chan_nr and link_id */
307 pp->u.data.chan_nr = *msg->data;
308 msgb_pull(msg, 1);
309 pp->u.data.link_id = *msg->data;
310 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200311 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200312 msgb_pull(msg, 1);
313
314 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200315 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200316
317 return 0;
318}
319
320/* get next frame from the tx queue. because the ms has multiple datalinks,
321 * each datalink's queue is read round-robin.
322 */
323static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
324{
325 struct osmo_phsap_prim pp;
326
327 /* we may send again */
328 le->tx_pending = 0;
329
330 /* free confirm message */
331 if (msg)
332 msgb_free(msg);
333
334 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
335 /* no message in all queues */
336
337 /* If user didn't request PH-EMPTY_FRAME.req, abort */
338 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
339 return 0;
340
341 /* otherwise, send PH-EMPTY_FRAME.req */
342 osmo_prim_init(&pp.oph, SAP_GSM_PH,
343 PRIM_PH_EMPTY_FRAME,
344 PRIM_OP_REQUEST, NULL);
345 } else {
346 le->tx_pending = 1;
347 }
348
349 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
350}
351
Harald Welte542301b2018-04-19 16:11:14 +0200352/* Is a given msg_type "transparent" as per TS 48.058 Section 8.1 */
353static int rsl_is_transparent(uint8_t msg_type)
354{
355 switch (msg_type) {
356 case RSL_MT_DATA_IND:
357 case RSL_MT_UNIT_DATA_IND:
358 return 1;
359 case RSL_MT_DATA_REQ:
360 case RSL_MT_UNIT_DATA_REQ:
361 return 1;
362 default:
363 return 0;
364 }
365}
366
Harald Welte1f0b8c22011-06-27 10:51:37 +0200367/* Create RSLms various RSLms messages */
368static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
369 struct msgb *msg)
370{
Harald Welte542301b2018-04-19 16:11:14 +0200371 int transparent = rsl_is_transparent(msg_type);
372
Harald Welte1f0b8c22011-06-27 10:51:37 +0200373 /* Add the RSL + RLL header */
Harald Welte542301b2018-04-19 16:11:14 +0200374 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200375
376 /* send off the RSLms message to L3 */
377 return rslms_sendmsg(msg, mctx->dl->entity);
378}
379
380/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
381static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
382{
383 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
384 struct abis_rsl_rll_hdr *rllh;
385
386 /* Add the RSL + RLL header */
387 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
388 msgb_push(msg, 2 + 2);
389 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
390 mctx->link_id, 1);
391 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
392
393 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
394 rllh->data[1] = mctx->ta_ind;
395
396 rllh->data[2] = RSL_IE_MS_POWER;
397 rllh->data[3] = mctx->tx_power_ind;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100398
Harald Welte1f0b8c22011-06-27 10:51:37 +0200399 return rslms_sendmsg(msg, mctx->dl->entity);
400}
401
402static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
403{
404 struct msgb *msg;
Harald Welte542301b2018-04-19 16:11:14 +0200405 int transparent = rsl_is_transparent(msg_type);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200406
Harald Welte542301b2018-04-19 16:11:14 +0200407 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200408
409 /* send off the RSLms message to L3 */
410 return rslms_sendmsg(msg, mctx->dl->entity);
411}
412
413static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
414{
415 struct msgb *msg;
416
rootaf48bed2011-09-26 11:23:06 +0200417 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte542301b2018-04-19 16:11:14 +0200418 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200419 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
420 return rslms_sendmsg(msg, mctx->dl->entity);
421}
422
rootaf48bed2011-09-26 11:23:06 +0200423/* DLSAP L2 -> L3 (RSLms) */
424static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
425 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200426{
rootaf48bed2011-09-26 11:23:06 +0200427 struct lapd_datalink *dl = lctx->dl;
428 struct lapdm_datalink *mdl =
429 container_of(dl, struct lapdm_datalink, dl);
430 struct lapdm_msg_ctx *mctx = &mdl->mctx;
431 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200432
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200433 switch (OSMO_PRIM_HDR(&dp->oph)) {
434 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
435 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200436 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200437 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
438 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200439 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200440 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
441 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200442 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200443 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
444 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
445 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
446 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200447 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200448 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
449 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200450 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200451 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
452 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200453 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200454 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
455 rsl_rll_error(dp->u.error_ind.cause, mctx);
456 if (dp->oph.msg)
457 msgb_free(dp->oph.msg);
458 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200459 }
rootaf48bed2011-09-26 11:23:06 +0200460
461 if (!rll_msg) {
462 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
463 "fix!\n", dp->oph.primitive, dp->oph.operation);
464 return -EINVAL;
465 }
466
467 if (!dp->oph.msg)
468 return send_rll_simple(rll_msg, mctx);
469
470 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200471}
472
rootaf48bed2011-09-26 11:23:06 +0200473/* send a data frame to layer 1 */
474static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200475{
rootaf48bed2011-09-26 11:23:06 +0200476 uint8_t l3_len = msg->tail - msg->data;
477 struct lapd_datalink *dl = lctx->dl;
478 struct lapdm_datalink *mdl =
479 container_of(dl, struct lapdm_datalink, dl);
480 struct lapdm_msg_ctx *mctx = &mdl->mctx;
481 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200482
rootaf48bed2011-09-26 11:23:06 +0200483 /* prepend l2 header */
484 msg->l2h = msgb_push(msg, 3);
485 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
486 /* EA is set here too */
487 switch (format) {
488 case LAPD_FORM_I:
489 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
490 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200491 break;
rootaf48bed2011-09-26 11:23:06 +0200492 case LAPD_FORM_S:
493 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200494 break;
rootaf48bed2011-09-26 11:23:06 +0200495 case LAPD_FORM_U:
496 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200497 break;
498 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200499 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200500 return -EINVAL;
501 }
rootaf48bed2011-09-26 11:23:06 +0200502 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
503 if (lctx->more)
504 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200505
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100506 /* add ACCH header with last indicated tx-power and TA */
507 if ((mctx->link_id & 0x40)) {
508 struct lapdm_entity *le = mdl->entity;
509
510 msg->l2h = msgb_push(msg, 2);
511 msg->l2h[0] = le->tx_power;
512 msg->l2h[1] = le->ta;
513 }
514
rootaf48bed2011-09-26 11:23:06 +0200515 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
516 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200517}
518
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100519static int update_pending_frames(struct lapd_msg_ctx *lctx)
520{
521 struct lapd_datalink *dl = lctx->dl;
522 struct msgb *msg;
523 int rc = -1;
524
525 llist_for_each_entry(msg, &dl->tx_queue, list) {
526 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
527 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
528 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
529 rc = 0;
530 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
531 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
532 }
533 }
534
535 return rc;
536}
537
Harald Welte1f0b8c22011-06-27 10:51:37 +0200538/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200539static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
540 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200541{
542 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200543 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200544 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200545 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200546 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200547 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200548
549 /* when we reach here, we have a msgb with l2h pointing to the raw
550 * 23byte mac block. The l1h has already been purged. */
551
rootaf48bed2011-09-26 11:23:06 +0200552 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200553 mctx.chan_nr = chan_nr;
554 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200555
Harald Welte1f0b8c22011-06-27 10:51:37 +0200556 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
557 if (cbits == 0x10 || cbits == 0x12) {
558 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
559 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200560 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200561 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200562 } else {
563 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200564 /* It was received from network on SACCH */
565
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100566 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
567 if (le->mode == LAPDM_MODE_MS
568 && LAPDm_CTRL_is_U(msg->l2h[3])
569 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200570 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200571 n201 = N201_B4;
572 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200573 } else {
574 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200575 n201 = N201_AB_SACCH;
576 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200577 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200578 /* SACCH frames have a two-byte L1 header that
579 * OsmocomBB L1 doesn't strip */
580 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
581 mctx.ta_ind = msg->l2h[1];
582 msgb_pull(msg, 2);
583 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200584 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200585 } else {
586 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200587 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100588 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200589 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200590 }
591 }
592
Daniel Willmann55405fb2014-03-26 13:45:17 +0100593 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200594 /* G.2.1 No action on frames containing an unallocated SAPI. */
595 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200596 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200597 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200598 msgb_free(msg);
599 return -EIO;
600 }
601
Harald Welte1f0b8c22011-06-27 10:51:37 +0200602 switch (mctx.lapdm_fmt) {
603 case LAPDm_FMT_A:
604 case LAPDm_FMT_B:
605 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200606 lctx.dl = &mctx.dl->dl;
607 /* obtain SAPI from address field */
608 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
609 /* G.2.3 EA bit set to "0" is not allowed in GSM */
610 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
611 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
612 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200613 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200614 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200615 return -EINVAL;
616 }
rootaf48bed2011-09-26 11:23:06 +0200617 /* adress field */
618 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
619 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
620 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
621 /* command field */
622 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
623 lctx.format = LAPD_FORM_I;
624 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
625 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
626 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
627 lctx.format = LAPD_FORM_S;
628 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
629 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
630 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
631 lctx.format = LAPD_FORM_U;
632 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
633 } else
634 lctx.format = LAPD_FORM_UKN;
635 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
636 if (lctx.sapi != LAPDm_SAPI_NORMAL
637 && lctx.sapi != LAPDm_SAPI_SMS
638 && lctx.format == LAPD_FORM_U
639 && lctx.s_u == LAPDm_U_UI) {
640 /* 5.3.3 UI frames with invalid SAPI values shall be
641 * discarded
642 */
643 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
644 lctx.sapi);
645 msgb_free(msg);
646 return 0;
647 }
648 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
649 lctx.n201 = n201;
650 lctx.length = n201;
651 lctx.more = 0;
652 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100653 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200654 } else {
655 /* length field */
656 if (!(msg->l2h[2] & LAPDm_EL)) {
657 /* G.4.1 If the EL bit is set to "0", an
658 * MDL-ERROR-INDICATION primitive with cause
659 * "frame not implemented" is sent to the
660 * mobile management entity. */
661 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
662 "multi-octet length\n");
663 msgb_free(msg);
664 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
665 return -EINVAL;
666 }
667 lctx.n201 = n201;
668 lctx.length = msg->l2h[2] >> 2;
669 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
670 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100671 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200672 }
673 /* store context for messages from lapd */
674 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
675 /* send to LAPD */
676 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200677 break;
678 case LAPDm_FMT_Bter:
679 /* FIXME */
680 msgb_free(msg);
681 break;
682 case LAPDm_FMT_Bbis:
683 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200684 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200685 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100686 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200687 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
688 break;
689 default:
690 msgb_free(msg);
691 }
692
693 return rc;
694}
695
696/* input into layer2 (from layer 1) */
697static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
698{
699 struct abis_rsl_cchan_hdr *ch;
700 struct gsm48_req_ref req_ref;
701 struct gsm_time gt;
702 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
703
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200704 if (!msg)
705 return -ENOMEM;
706
Harald Welte1f0b8c22011-06-27 10:51:37 +0200707 msg->l2h = msgb_push(msg, sizeof(*ch));
708 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
709 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
710 ch->chan_nr = RSL_CHAN_RACH;
711
712 /* generate a RSL CHANNEL REQUIRED message */
713 gsm_fn2gsmtime(&gt, fn);
714 req_ref.ra = ra;
715 req_ref.t1 = gt.t1; /* FIXME: modulo? */
716 req_ref.t2 = gt.t2;
717 req_ref.t3_low = gt.t3 & 7;
718 req_ref.t3_high = gt.t3 >> 3;
719
720 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
721 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
722
723 return rslms_sendmsg(msg, le);
724}
725
726static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
727
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200728/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200729int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
730{
731 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
732 int rc = 0;
733
734 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200735 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200736 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100737 rc = -ENODEV;
738 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200739 }
740
741 switch (oph->primitive) {
742 case PRIM_PH_DATA:
743 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200744 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200745 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100746 rc = -ENODEV;
747 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200748 }
749 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
750 pp->u.data.link_id);
751 break;
752 case PRIM_PH_RTS:
753 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200754 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200755 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100756 rc = -ENODEV;
757 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200758 }
759 rc = l2_ph_data_conf(oph->msg, le);
760 break;
761 case PRIM_PH_RACH:
762 switch (oph->operation) {
763 case PRIM_OP_INDICATION:
764 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
765 pp->u.rach_ind.acc_delay);
766 break;
767 case PRIM_OP_CONFIRM:
768 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
769 break;
770 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100771 rc = -EIO;
772 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200773 }
774 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100775 default:
776 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
777 oph->primitive);
778 rc = -EINVAL;
779 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200780 }
781
782 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100783
784free:
785 msgb_free(oph->msg);
786 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200787}
788
789
790/* L3 -> L2 / RSLMS -> LAPDm */
791
rootaf48bed2011-09-26 11:23:06 +0200792/* Set LAPDm context for established connection */
793static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
794 uint8_t link_id, int n201, uint8_t sapi)
795{
796 memset(&dl->mctx, 0, sizeof(dl->mctx));
797 dl->mctx.dl = dl;
798 dl->mctx.chan_nr = chan_nr;
799 dl->mctx.link_id = link_id;
800 dl->dl.lctx.dl = &dl->dl;
801 dl->dl.lctx.n201 = n201;
802 dl->dl.lctx.sapi = sapi;
803
804 return 0;
805}
806
Harald Welte1f0b8c22011-06-27 10:51:37 +0200807/* L3 requests establishment of data link */
808static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
809{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200810 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
811 uint8_t chan_nr = rllh->chan_nr;
812 uint8_t link_id = rllh->link_id;
813 uint8_t sapi = rllh->link_id & 7;
814 struct tlv_parsed tv;
815 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100816 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200817 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200818
rootaf48bed2011-09-26 11:23:06 +0200819 /* Set LAPDm context for established connection */
820 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200821
rootaf48bed2011-09-26 11:23:06 +0200822 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200823 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200824 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200825 /* contention resolution establishment procedure */
826 if (sapi != 0) {
827 /* According to clause 6, the contention resolution
828 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200829 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200830 "resolution (discarding)\n");
831 msgb_free(msg);
832 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
833 }
834 /* transmit a SABM command with the P bit set to "1". The SABM
835 * command shall contain the layer 3 message unit */
836 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200837 } else {
838 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200839 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200840 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200841 }
842
843 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100844 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200845 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100846 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200847 msgb_free(msg);
848 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
849 }
850
rootaf48bed2011-09-26 11:23:06 +0200851 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100852 msgb_pull_to_l3(msg);
853 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200854
rootaf48bed2011-09-26 11:23:06 +0200855 /* prepare prim */
856 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200857
rootaf48bed2011-09-26 11:23:06 +0200858 /* send to L2 */
859 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200860}
861
862/* L3 requests transfer of unnumbered information */
863static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
864{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100865 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200866 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
867 uint8_t chan_nr = rllh->chan_nr;
868 uint8_t link_id = rllh->link_id;
869 uint8_t sapi = link_id & 7;
870 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +0100871 int length, ui_bts;
872
873 if (!le) {
874 LOGP(DLLAPD, LOGL_ERROR, "lapdm_datalink without entity error\n");
875 msgb_free(msg);
876 return -EMLINK;
877 }
878 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200879
880 /* check if the layer3 message length exceeds N201 */
881
882 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
883
884 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100885 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200886 }
887 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100888 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200889 }
890 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200891 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200892 "error\n");
893 msgb_free(msg);
894 return -EINVAL;
895 }
rootaf48bed2011-09-26 11:23:06 +0200896 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200897 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
898 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200899 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200900 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200901 "(discarding)\n", length,
902 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200903 msgb_free(msg);
904 return -EIO;
905 }
906
rootaf48bed2011-09-26 11:23:06 +0200907 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100908 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200909
rootaf48bed2011-09-26 11:23:06 +0200910 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100911 msgb_pull_to_l3(msg);
912 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200913
914 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200915 msg->l2h = msgb_push(msg, 2 + !ui_bts);
916 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
917 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100918 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200919 msg->l2h[2] = LAPDm_LEN(length);
920 if (link_id & 0x40) {
921 msg->l2h = msgb_push(msg, 2);
922 msg->l2h[0] = le->tx_power;
923 msg->l2h[1] = le->ta;
924 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200925
926 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200927 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200928}
929
930/* L3 requests transfer of acknowledged information */
931static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
932{
933 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
934 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200935 int length;
936 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200937
938 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
939 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200940 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200941 "error\n");
942 msgb_free(msg);
943 return -EINVAL;
944 }
rootaf48bed2011-09-26 11:23:06 +0200945 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
946 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200947
rootaf48bed2011-09-26 11:23:06 +0200948 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100949 msgb_pull_to_l3(msg);
950 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200951
rootaf48bed2011-09-26 11:23:06 +0200952 /* prepare prim */
953 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200954
rootaf48bed2011-09-26 11:23:06 +0200955 /* send to L2 */
956 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200957}
958
959/* L3 requests suspension of data link */
960static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
961{
962 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
963 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200964 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200965
966 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200967 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200968 msgb_free(msg);
969 return -EINVAL;
970 }
971
rootaf48bed2011-09-26 11:23:06 +0200972 /* prepare prim */
973 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200974
rootaf48bed2011-09-26 11:23:06 +0200975 /* send to L2 */
976 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200977}
978
979/* L3 requests resume of data link */
980static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
981{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200982 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200983 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200984 uint8_t chan_nr = rllh->chan_nr;
985 uint8_t link_id = rllh->link_id;
986 uint8_t sapi = rllh->link_id & 7;
987 struct tlv_parsed tv;
988 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100989 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200990 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200991
rootaf48bed2011-09-26 11:23:06 +0200992 /* Set LAPDm context for established connection */
993 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200994
995 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
996 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200997 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200998 msgb_free(msg);
999 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1000 }
rootaf48bed2011-09-26 11:23:06 +02001001 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001002 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1003
rootaf48bed2011-09-26 11:23:06 +02001004 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001005 msgb_pull_to_l3(msg);
1006 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001007
rootaf48bed2011-09-26 11:23:06 +02001008 /* prepare prim */
1009 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1010 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001011
rootaf48bed2011-09-26 11:23:06 +02001012 /* send to L2 */
1013 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001014}
1015
1016/* L3 requests release of data link */
1017static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1018{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001019 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001020 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001021 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001022
1023 /* get release mode */
1024 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1025 mode = rllh->data[1] & 1;
1026
Harald Welte1f0b8c22011-06-27 10:51:37 +02001027 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001028 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001029
1030 /* 04.06 3.8.3: No information field is permitted with the DISC
1031 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001032 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001033
rootaf48bed2011-09-26 11:23:06 +02001034 /* prepare prim */
1035 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1036 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001037
rootaf48bed2011-09-26 11:23:06 +02001038 /* send to L2 */
1039 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001040}
1041
1042/* L3 requests channel in idle state */
1043static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1044{
1045 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1046 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1047 struct osmo_phsap_prim pp;
1048
1049 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1050 PRIM_OP_REQUEST, NULL);
1051
1052 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001053 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001054 return -EINVAL;
1055 }
1056 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001057 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001058 return -EINVAL;
1059 }
1060 pp.u.rach_req.ra = cch->data[1];
1061 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1062 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1063
1064 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001065 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001066 return -EINVAL;
1067 }
1068 /* TA = 0 - delay */
1069 pp.u.rach_req.ta = 0 - cch->data[5];
1070
1071 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001072 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001073 return -EINVAL;
1074 }
1075 pp.u.rach_req.tx_power = cch->data[7];
1076
1077 msgb_free(msg);
1078
1079 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1080}
1081
1082/* L1 confirms channel request */
1083static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1084{
1085 struct abis_rsl_cchan_hdr *ch;
1086 struct gsm_time tm;
1087 struct gsm48_req_ref *ref;
1088
1089 gsm_fn2gsmtime(&tm, frame_nr);
1090
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001091 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001092 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1093 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1094 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1095 ch->chan_nr = RSL_CHAN_RACH;
1096 ch->data[0] = RSL_IE_REQ_REFERENCE;
1097 ref = (struct gsm48_req_ref *) (ch->data + 1);
1098 ref->t1 = tm.t1;
1099 ref->t2 = tm.t2;
1100 ref->t3_low = tm.t3 & 0x7;
1101 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001102
Harald Welte1f0b8c22011-06-27 10:51:37 +02001103 return rslms_sendmsg(msg, le);
1104}
1105
Harald Welte1f0b8c22011-06-27 10:51:37 +02001106/* incoming RSLms RLL message from L3 */
1107static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1108{
1109 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1110 int msg_type = rllh->c.msg_type;
1111 uint8_t sapi = rllh->link_id & 7;
1112 struct lapdm_entity *le;
1113 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001114 int rc = 0;
1115
1116 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001117 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001118 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001119 return -EINVAL;
1120 }
1121
1122 if (rllh->link_id & 0x40)
1123 le = &lc->lapdm_acch;
1124 else
1125 le = &lc->lapdm_dcch;
1126
Harald Welte1a87c1b2015-12-14 15:26:07 +01001127 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1128 * BTS side */
1129 if (le->mode == LAPDM_MODE_BTS) {
1130 switch (msg_type) {
1131 case RSL_MT_SUSP_REQ:
1132 case RSL_MT_RES_REQ:
1133 case RSL_MT_RECON_REQ:
1134 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1135 lc->name, rsl_msg_name(msg_type));
1136 msgb_free(msg);
1137 return -EINVAL;
1138 break;
1139 default:
1140 break;
1141 }
1142 }
1143
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001144 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001145 * SAPI.
1146 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001147 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001148 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001149 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001150 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001151 return -EINVAL;
1152 }
1153
Daniel Willmanne5233922012-12-25 23:15:50 +01001154 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001155 case RSL_MT_DATA_REQ:
1156 case RSL_MT_SUSP_REQ:
1157 case RSL_MT_REL_REQ:
1158 /* This is triggered in abnormal error conditions where
1159 * set_lapdm_context() was not called for the channel earlier. */
1160 if (!dl->dl.lctx.dl) {
1161 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1162 lc->name, rsl_msg_name(msg_type), sapi);
1163 msgb_free(msg);
1164 return -EINVAL;
1165 }
1166 break;
1167 default:
1168 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1169 lc->name, rsl_msg_name(msg_type), sapi);
1170 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001171
rootaf48bed2011-09-26 11:23:06 +02001172 switch (msg_type) {
1173 case RSL_MT_UNIT_DATA_REQ:
1174 rc = rslms_rx_rll_udata_req(msg, dl);
1175 break;
1176 case RSL_MT_EST_REQ:
1177 rc = rslms_rx_rll_est_req(msg, dl);
1178 break;
1179 case RSL_MT_DATA_REQ:
1180 rc = rslms_rx_rll_data_req(msg, dl);
1181 break;
1182 case RSL_MT_SUSP_REQ:
1183 rc = rslms_rx_rll_susp_req(msg, dl);
1184 break;
1185 case RSL_MT_RES_REQ:
1186 rc = rslms_rx_rll_res_req(msg, dl);
1187 break;
1188 case RSL_MT_RECON_REQ:
1189 rc = rslms_rx_rll_res_req(msg, dl);
1190 break;
1191 case RSL_MT_REL_REQ:
1192 rc = rslms_rx_rll_rel_req(msg, dl);
1193 break;
1194 default:
1195 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001196 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001197 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001198 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001199
1200 return rc;
1201}
1202
1203/* incoming RSLms COMMON CHANNEL message from L3 */
1204static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1205{
1206 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1207 int msg_type = cch->c.msg_type;
1208 int rc = 0;
1209
1210 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001211 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001212 return -EINVAL;
1213 }
1214
1215 switch (msg_type) {
1216 case RSL_MT_CHAN_RQD:
1217 /* create and send RACH request */
1218 rc = rslms_rx_chan_rqd(lc, msg);
1219 break;
1220 default:
rootaf48bed2011-09-26 11:23:06 +02001221 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001222 msg_type);
1223 msgb_free(msg);
1224 return 0;
1225 }
1226
1227 return rc;
1228}
1229
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001230/*! Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001231int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1232{
1233 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1234 int rc = 0;
1235
1236 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001237 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001238 return -EINVAL;
1239 }
1240
1241 switch (rslh->msg_discr & 0xfe) {
1242 case ABIS_RSL_MDISC_RLL:
1243 rc = rslms_rx_rll(msg, lc);
1244 break;
1245 case ABIS_RSL_MDISC_COM_CHAN:
1246 rc = rslms_rx_com_chan(msg, lc);
1247 break;
1248 default:
rootaf48bed2011-09-26 11:23:06 +02001249 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001250 "discriminator 0x%02x", rslh->msg_discr);
1251 msgb_free(msg);
1252 return -EINVAL;
1253 }
1254
1255 return rc;
1256}
1257
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001258/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001259int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1260{
rootaf48bed2011-09-26 11:23:06 +02001261 int i;
1262 enum lapd_mode lm;
1263
Harald Welte1f0b8c22011-06-27 10:51:37 +02001264 switch (mode) {
1265 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001266 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001267 break;
1268 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001269 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001270 break;
1271 default:
1272 return -EINVAL;
1273 }
1274
rootaf48bed2011-09-26 11:23:06 +02001275 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1276 lapd_set_mode(&le->datalink[i].dl, lm);
1277 }
1278
Harald Welte1f0b8c22011-06-27 10:51:37 +02001279 le->mode = mode;
1280
1281 return 0;
1282}
1283
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001284/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001285int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1286{
1287 int rc;
1288
1289 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1290 if (rc < 0)
1291 return rc;
1292
1293 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1294}
1295
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001296/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001297void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1298{
1299 lc->lapdm_dcch.l1_prim_cb = cb;
1300 lc->lapdm_acch.l1_prim_cb = cb;
1301 lc->lapdm_dcch.l1_ctx = ctx;
1302 lc->lapdm_acch.l1_ctx = ctx;
1303}
1304
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001305/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001306void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1307{
1308 lc->lapdm_dcch.l3_cb = cb;
1309 lc->lapdm_acch.l3_cb = cb;
1310 lc->lapdm_dcch.l3_ctx = ctx;
1311 lc->lapdm_acch.l3_ctx = ctx;
1312}
1313
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001314/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001315void lapdm_entity_reset(struct lapdm_entity *le)
1316{
1317 struct lapdm_datalink *dl;
1318 int i;
1319
1320 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1321 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001322 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001323 }
1324}
1325
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001326/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001327void lapdm_channel_reset(struct lapdm_channel *lc)
1328{
1329 lapdm_entity_reset(&lc->lapdm_dcch);
1330 lapdm_entity_reset(&lc->lapdm_acch);
1331}
1332
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001333/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001334void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1335{
1336 le->flags = flags;
1337}
1338
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001339/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001340void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1341{
1342 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1343 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1344}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001345
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001346/*! @} */