blob: 80c0d1787a331ee9e0450d2878b5def56d0ea52e [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file lapdm.c
2 * GSM LAPDm (TS 04.06) implementation. */
3/*
Harald Weltee08da972017-11-13 01:00:26 +09004 * (C) 2010-2017 by Harald Welte <laforge@gnumonks.org>
rootaf48bed2011-09-26 11:23:06 +02005 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
Harald Weltee08da972017-11-13 01:00:26 +09006 * (C) 2014-2016 by sysmocom - s.f.m.c GmbH
Harald Welte1f0b8c22011-06-27 10:51:37 +02007 *
8 * All Rights Reserved
9 *
Harald Weltee08da972017-11-13 01:00:26 +090010 * SPDX-License-Identifier: GPL-2.0+
11 *
Harald Welte1f0b8c22011-06-27 10:51:37 +020012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 */
27
Harald Welte6bdf0b12011-08-17 18:22:08 +020028/*! \addtogroup lapdm
29 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020030 * \file lapdm.c */
Harald Welte6bdf0b12011-08-17 18:22:08 +020031
Harald Welte1f0b8c22011-06-27 10:51:37 +020032#include <stdio.h>
33#include <stdint.h>
34#include <string.h>
35#include <errno.h>
Harald Welte1f0b8c22011-06-27 10:51:37 +020036
37#include <osmocom/core/logging.h>
38#include <osmocom/core/timer.h>
39#include <osmocom/core/msgb.h>
40#include <osmocom/core/utils.h>
41
42#include <osmocom/gsm/tlv.h>
43#include <osmocom/gsm/rsl.h>
44#include <osmocom/gsm/prim.h>
45#include <osmocom/gsm/gsm_utils.h>
46#include <osmocom/gsm/lapdm.h>
47
48#include <osmocom/gsm/protocol/gsm_04_08.h>
49#include <osmocom/gsm/protocol/gsm_08_58.h>
50
51/* TS 04.06 Figure 4 / Section 3.2 */
52#define LAPDm_LPD_NORMAL 0
53#define LAPDm_LPD_SMSCB 1
54#define LAPDm_SAPI_NORMAL 0
55#define LAPDm_SAPI_SMS 3
56#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
57
rootaf48bed2011-09-26 11:23:06 +020058#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020059#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
60#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
61#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
62
63/* TS 04.06 Table 3 / Section 3.4.3 */
64#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
65#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
66#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
67
68#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
69#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
70#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
71
72#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
73#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
74
75#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
76
77#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
78#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
79
Harald Welte1f0b8c22011-06-27 10:51:37 +020080#define LAPDm_LEN(len) ((len << 2) | 0x1)
81#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020082#define LAPDm_EL 0x1
83
84#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020085
86/* TS 04.06 Section 5.8.3 */
87#define N201_AB_SACCH 18
88#define N201_AB_SDCCH 20
89#define N201_AB_FACCH 20
90#define N201_Bbis 23
91#define N201_Bter_SACCH 21
92#define N201_Bter_SDCCH 23
93#define N201_Bter_FACCH 23
94#define N201_B4 19
95
96/* 5.8.2.1 N200 during establish and release */
97#define N200_EST_REL 5
98/* 5.8.2.1 N200 during timer recovery state */
99#define N200_TR_SACCH 5
100#define N200_TR_SDCCH 23
101#define N200_TR_FACCH_FR 34
102#define N200_TR_EFACCH_FR 48
103#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200104/* FIXME: set N200 depending on chan_nr */
105#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200106
107enum lapdm_format {
108 LAPDm_FMT_A,
109 LAPDm_FMT_B,
110 LAPDm_FMT_Bbis,
111 LAPDm_FMT_Bter,
112 LAPDm_FMT_B4,
113};
114
Maxadef12a2016-05-25 15:25:02 +0200115const struct value_string osmo_ph_prim_names[] = {
116 { PRIM_PH_DATA, "PH-DATA" },
117 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
118 { PRIM_PH_CONN, "PH-CONNECT" },
119 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
120 { PRIM_PH_RTS, "PH-RTS" },
121 { PRIM_MPH_INFO, "MPH-INFO" },
122 { PRIM_TCH, "TCH" },
123 { PRIM_TCH_RTS, "TCH-RTS" },
124 { 0, NULL }
125};
126
rootaf48bed2011-09-26 11:23:06 +0200127static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
128static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
129 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100130static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200131
132static void lapdm_dl_init(struct lapdm_datalink *dl,
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100133 struct lapdm_entity *entity, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200134{
135 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200136 dl->entity = entity;
rootaf48bed2011-09-26 11:23:06 +0200137 lapd_dl_init(&dl->dl, 1, 8, 200);
138 dl->dl.reestablish = 0; /* GSM uses no reestablish */
139 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
140 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100141 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200142 dl->dl.n200_est_rel = N200_EST_REL;
143 dl->dl.n200 = N200;
144 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100145 dl->dl.t200_sec = t200; dl->dl.t200_usec = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200146}
147
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200148/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200149 * \param[in] le LAPDm entity
150 * \param[in] mode \ref lapdm_mode (BTS/MS)
151 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100152void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200153{
154 unsigned int i;
155
156 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100157 lapdm_dl_init(&le->datalink[i], le, t200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200158
159 lapdm_entity_set_mode(le, mode);
160}
161
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200162/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200163 * \param[in] lc \ref lapdm_channel to be initialized
164 * \param[in] mode \ref lapdm_mode (BTS/MS)
165 *
166 * This really is a convenience wrapper around calling \ref
167 * lapdm_entity_init twice.
168 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200169void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
170{
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100171 lapdm_entity_init(&lc->lapdm_acch, mode, 2);
rootaf48bed2011-09-26 11:23:06 +0200172 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100173 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200174}
175
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200176/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200177void lapdm_entity_exit(struct lapdm_entity *le)
178{
179 unsigned int i;
180 struct lapdm_datalink *dl;
181
182 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
183 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200184 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200185 }
186}
187
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200188/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200189 *
190 * A convenience wrapper calling \ref lapdm_entity_exit on both
191 * entities inside the \ref lapdm_channel
192 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200193void lapdm_channel_exit(struct lapdm_channel *lc)
194{
195 lapdm_entity_exit(&lc->lapdm_acch);
196 lapdm_entity_exit(&lc->lapdm_dcch);
197}
198
Daniel Willmann55405fb2014-03-26 13:45:17 +0100199struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200200{
201 switch (sapi) {
202 case LAPDm_SAPI_NORMAL:
203 return &le->datalink[0];
204 case LAPDm_SAPI_SMS:
205 return &le->datalink[1];
206 default:
207 return NULL;
208 }
209}
210
Harald Welte1f0b8c22011-06-27 10:51:37 +0200211/* Append padding (if required) */
212static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
213{
214 int pad_len = n201 - msgb_l2len(msg);
215 uint8_t *data;
216
217 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200218 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200219 "cannot pad message that is already too big!\n");
220 return;
221 }
222
223 data = msgb_put(msg, pad_len);
224 memset(data, 0x2B, pad_len);
225}
226
227/* input function that L2 calls when sending messages up to L3 */
228static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
229{
230 if (!le->l3_cb) {
231 msgb_free(msg);
232 return -EIO;
233 }
234
235 /* call the layer2 message handler that is registered */
236 return le->l3_cb(msg, le, le->l3_ctx);
237}
238
239/* write a frame into the tx queue */
240static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200241 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200242{
243 struct lapdm_entity *le = dl->entity;
244 struct osmo_phsap_prim pp;
245
246 /* if there is a pending message, queue it */
247 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200248 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200249 *msgb_push(msg, 1) = link_id;
250 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200251 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200252 return -EBUSY;
253 }
254
255 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
256 PRIM_OP_REQUEST, msg);
257 pp.u.data.chan_nr = chan_nr;
258 pp.u.data.link_id = link_id;
259
260 /* send the frame now */
261 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200262 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200263
264 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
265}
266
267static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
268{
269 struct lapdm_datalink *dl;
270 int last = le->last_tx_dequeue;
271 int i = last, n = ARRAY_SIZE(le->datalink);
272 struct msgb *msg = NULL;
273
274 /* round-robin dequeue */
275 do {
276 /* next */
277 i = (i + 1) % n;
278 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200279 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200280 break;
281 } while (i != last);
282
283 if (msg) {
284 /* Set last dequeue position */
285 le->last_tx_dequeue = i;
286 }
287
288 return msg;
289}
290
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200291/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200292 * a osmo_phsap_prim */
293int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
294{
295 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200296 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200297
298 msg = tx_dequeue_msgb(le);
299 if (!msg)
300 return -ENODEV;
301
302 /* if we have a message, send PH-DATA.req */
303 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
304 PRIM_OP_REQUEST, msg);
305
306 /* Pull chan_nr and link_id */
307 pp->u.data.chan_nr = *msg->data;
308 msgb_pull(msg, 1);
309 pp->u.data.link_id = *msg->data;
310 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200311 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200312 msgb_pull(msg, 1);
313
314 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200315 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200316
317 return 0;
318}
319
320/* get next frame from the tx queue. because the ms has multiple datalinks,
321 * each datalink's queue is read round-robin.
322 */
323static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
324{
325 struct osmo_phsap_prim pp;
326
327 /* we may send again */
328 le->tx_pending = 0;
329
330 /* free confirm message */
331 if (msg)
332 msgb_free(msg);
333
334 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
335 /* no message in all queues */
336
337 /* If user didn't request PH-EMPTY_FRAME.req, abort */
338 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
339 return 0;
340
341 /* otherwise, send PH-EMPTY_FRAME.req */
342 osmo_prim_init(&pp.oph, SAP_GSM_PH,
343 PRIM_PH_EMPTY_FRAME,
344 PRIM_OP_REQUEST, NULL);
345 } else {
346 le->tx_pending = 1;
347 }
348
349 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
350}
351
352/* Create RSLms various RSLms messages */
353static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
354 struct msgb *msg)
355{
356 /* Add the RSL + RLL header */
357 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
358
359 /* send off the RSLms message to L3 */
360 return rslms_sendmsg(msg, mctx->dl->entity);
361}
362
363/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
364static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
365{
366 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
367 struct abis_rsl_rll_hdr *rllh;
368
369 /* Add the RSL + RLL header */
370 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
371 msgb_push(msg, 2 + 2);
372 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
373 mctx->link_id, 1);
374 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
375
376 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
377 rllh->data[1] = mctx->ta_ind;
378
379 rllh->data[2] = RSL_IE_MS_POWER;
380 rllh->data[3] = mctx->tx_power_ind;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100381
Harald Welte1f0b8c22011-06-27 10:51:37 +0200382 return rslms_sendmsg(msg, mctx->dl->entity);
383}
384
385static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
386{
387 struct msgb *msg;
388
389 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
390
391 /* send off the RSLms message to L3 */
392 return rslms_sendmsg(msg, mctx->dl->entity);
393}
394
395static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
396{
397 struct msgb *msg;
398
rootaf48bed2011-09-26 11:23:06 +0200399 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200400 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200401 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
402 return rslms_sendmsg(msg, mctx->dl->entity);
403}
404
rootaf48bed2011-09-26 11:23:06 +0200405/* DLSAP L2 -> L3 (RSLms) */
406static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
407 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200408{
rootaf48bed2011-09-26 11:23:06 +0200409 struct lapd_datalink *dl = lctx->dl;
410 struct lapdm_datalink *mdl =
411 container_of(dl, struct lapdm_datalink, dl);
412 struct lapdm_msg_ctx *mctx = &mdl->mctx;
413 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200414
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200415 switch (OSMO_PRIM_HDR(&dp->oph)) {
416 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
417 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200418 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200419 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
420 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200421 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200422 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
423 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200424 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200425 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
426 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
427 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
428 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200429 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200430 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
431 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200432 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200433 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
434 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200435 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200436 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
437 rsl_rll_error(dp->u.error_ind.cause, mctx);
438 if (dp->oph.msg)
439 msgb_free(dp->oph.msg);
440 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200441 }
rootaf48bed2011-09-26 11:23:06 +0200442
443 if (!rll_msg) {
444 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
445 "fix!\n", dp->oph.primitive, dp->oph.operation);
446 return -EINVAL;
447 }
448
449 if (!dp->oph.msg)
450 return send_rll_simple(rll_msg, mctx);
451
452 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200453}
454
rootaf48bed2011-09-26 11:23:06 +0200455/* send a data frame to layer 1 */
456static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200457{
rootaf48bed2011-09-26 11:23:06 +0200458 uint8_t l3_len = msg->tail - msg->data;
459 struct lapd_datalink *dl = lctx->dl;
460 struct lapdm_datalink *mdl =
461 container_of(dl, struct lapdm_datalink, dl);
462 struct lapdm_msg_ctx *mctx = &mdl->mctx;
463 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200464
rootaf48bed2011-09-26 11:23:06 +0200465 /* prepend l2 header */
466 msg->l2h = msgb_push(msg, 3);
467 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
468 /* EA is set here too */
469 switch (format) {
470 case LAPD_FORM_I:
471 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
472 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200473 break;
rootaf48bed2011-09-26 11:23:06 +0200474 case LAPD_FORM_S:
475 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200476 break;
rootaf48bed2011-09-26 11:23:06 +0200477 case LAPD_FORM_U:
478 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200479 break;
480 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200481 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200482 return -EINVAL;
483 }
rootaf48bed2011-09-26 11:23:06 +0200484 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
485 if (lctx->more)
486 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200487
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100488 /* add ACCH header with last indicated tx-power and TA */
489 if ((mctx->link_id & 0x40)) {
490 struct lapdm_entity *le = mdl->entity;
491
492 msg->l2h = msgb_push(msg, 2);
493 msg->l2h[0] = le->tx_power;
494 msg->l2h[1] = le->ta;
495 }
496
rootaf48bed2011-09-26 11:23:06 +0200497 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
498 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200499}
500
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100501static int update_pending_frames(struct lapd_msg_ctx *lctx)
502{
503 struct lapd_datalink *dl = lctx->dl;
504 struct msgb *msg;
505 int rc = -1;
506
507 llist_for_each_entry(msg, &dl->tx_queue, list) {
508 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
509 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
510 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
511 rc = 0;
512 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
513 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
514 }
515 }
516
517 return rc;
518}
519
Harald Welte1f0b8c22011-06-27 10:51:37 +0200520/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200521static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
522 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200523{
524 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200525 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200526 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200527 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200528 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200529 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200530
531 /* when we reach here, we have a msgb with l2h pointing to the raw
532 * 23byte mac block. The l1h has already been purged. */
533
rootaf48bed2011-09-26 11:23:06 +0200534 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200535 mctx.chan_nr = chan_nr;
536 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200537
Harald Welte1f0b8c22011-06-27 10:51:37 +0200538 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
539 if (cbits == 0x10 || cbits == 0x12) {
540 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
541 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200542 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200543 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200544 } else {
545 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200546 /* It was received from network on SACCH */
547
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100548 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
549 if (le->mode == LAPDM_MODE_MS
550 && LAPDm_CTRL_is_U(msg->l2h[3])
551 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200552 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200553 n201 = N201_B4;
554 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200555 } else {
556 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200557 n201 = N201_AB_SACCH;
558 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200559 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200560 /* SACCH frames have a two-byte L1 header that
561 * OsmocomBB L1 doesn't strip */
562 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
563 mctx.ta_ind = msg->l2h[1];
564 msgb_pull(msg, 2);
565 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200566 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200567 } else {
568 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200569 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100570 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200571 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200572 }
573 }
574
Daniel Willmann55405fb2014-03-26 13:45:17 +0100575 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200576 /* G.2.1 No action on frames containing an unallocated SAPI. */
577 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200578 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200579 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200580 msgb_free(msg);
581 return -EIO;
582 }
583
Harald Welte1f0b8c22011-06-27 10:51:37 +0200584 switch (mctx.lapdm_fmt) {
585 case LAPDm_FMT_A:
586 case LAPDm_FMT_B:
587 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200588 lctx.dl = &mctx.dl->dl;
589 /* obtain SAPI from address field */
590 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
591 /* G.2.3 EA bit set to "0" is not allowed in GSM */
592 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
593 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
594 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200595 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200596 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200597 return -EINVAL;
598 }
rootaf48bed2011-09-26 11:23:06 +0200599 /* adress field */
600 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
601 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
602 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
603 /* command field */
604 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
605 lctx.format = LAPD_FORM_I;
606 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
607 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
608 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
609 lctx.format = LAPD_FORM_S;
610 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
611 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
612 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
613 lctx.format = LAPD_FORM_U;
614 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
615 } else
616 lctx.format = LAPD_FORM_UKN;
617 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
618 if (lctx.sapi != LAPDm_SAPI_NORMAL
619 && lctx.sapi != LAPDm_SAPI_SMS
620 && lctx.format == LAPD_FORM_U
621 && lctx.s_u == LAPDm_U_UI) {
622 /* 5.3.3 UI frames with invalid SAPI values shall be
623 * discarded
624 */
625 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
626 lctx.sapi);
627 msgb_free(msg);
628 return 0;
629 }
630 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
631 lctx.n201 = n201;
632 lctx.length = n201;
633 lctx.more = 0;
634 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100635 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200636 } else {
637 /* length field */
638 if (!(msg->l2h[2] & LAPDm_EL)) {
639 /* G.4.1 If the EL bit is set to "0", an
640 * MDL-ERROR-INDICATION primitive with cause
641 * "frame not implemented" is sent to the
642 * mobile management entity. */
643 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
644 "multi-octet length\n");
645 msgb_free(msg);
646 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
647 return -EINVAL;
648 }
649 lctx.n201 = n201;
650 lctx.length = msg->l2h[2] >> 2;
651 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
652 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100653 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200654 }
655 /* store context for messages from lapd */
656 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
657 /* send to LAPD */
658 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200659 break;
660 case LAPDm_FMT_Bter:
661 /* FIXME */
662 msgb_free(msg);
663 break;
664 case LAPDm_FMT_Bbis:
665 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200666 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200667 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100668 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200669 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
670 break;
671 default:
672 msgb_free(msg);
673 }
674
675 return rc;
676}
677
678/* input into layer2 (from layer 1) */
679static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
680{
681 struct abis_rsl_cchan_hdr *ch;
682 struct gsm48_req_ref req_ref;
683 struct gsm_time gt;
684 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
685
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200686 if (!msg)
687 return -ENOMEM;
688
Harald Welte1f0b8c22011-06-27 10:51:37 +0200689 msg->l2h = msgb_push(msg, sizeof(*ch));
690 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
691 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
692 ch->chan_nr = RSL_CHAN_RACH;
693
694 /* generate a RSL CHANNEL REQUIRED message */
695 gsm_fn2gsmtime(&gt, fn);
696 req_ref.ra = ra;
697 req_ref.t1 = gt.t1; /* FIXME: modulo? */
698 req_ref.t2 = gt.t2;
699 req_ref.t3_low = gt.t3 & 7;
700 req_ref.t3_high = gt.t3 >> 3;
701
702 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
703 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
704
705 return rslms_sendmsg(msg, le);
706}
707
708static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
709
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200710/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200711int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
712{
713 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
714 int rc = 0;
715
716 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200717 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200718 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100719 rc = -ENODEV;
720 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200721 }
722
723 switch (oph->primitive) {
724 case PRIM_PH_DATA:
725 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200726 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200727 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100728 rc = -ENODEV;
729 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200730 }
731 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
732 pp->u.data.link_id);
733 break;
734 case PRIM_PH_RTS:
735 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200736 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200737 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100738 rc = -ENODEV;
739 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200740 }
741 rc = l2_ph_data_conf(oph->msg, le);
742 break;
743 case PRIM_PH_RACH:
744 switch (oph->operation) {
745 case PRIM_OP_INDICATION:
746 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
747 pp->u.rach_ind.acc_delay);
748 break;
749 case PRIM_OP_CONFIRM:
750 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
751 break;
752 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100753 rc = -EIO;
754 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200755 }
756 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100757 default:
758 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
759 oph->primitive);
760 rc = -EINVAL;
761 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200762 }
763
764 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100765
766free:
767 msgb_free(oph->msg);
768 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200769}
770
771
772/* L3 -> L2 / RSLMS -> LAPDm */
773
rootaf48bed2011-09-26 11:23:06 +0200774/* Set LAPDm context for established connection */
775static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
776 uint8_t link_id, int n201, uint8_t sapi)
777{
778 memset(&dl->mctx, 0, sizeof(dl->mctx));
779 dl->mctx.dl = dl;
780 dl->mctx.chan_nr = chan_nr;
781 dl->mctx.link_id = link_id;
782 dl->dl.lctx.dl = &dl->dl;
783 dl->dl.lctx.n201 = n201;
784 dl->dl.lctx.sapi = sapi;
785
786 return 0;
787}
788
Harald Welte1f0b8c22011-06-27 10:51:37 +0200789/* L3 requests establishment of data link */
790static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
791{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200792 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
793 uint8_t chan_nr = rllh->chan_nr;
794 uint8_t link_id = rllh->link_id;
795 uint8_t sapi = rllh->link_id & 7;
796 struct tlv_parsed tv;
797 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100798 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200799 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200800
rootaf48bed2011-09-26 11:23:06 +0200801 /* Set LAPDm context for established connection */
802 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200803
rootaf48bed2011-09-26 11:23:06 +0200804 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200805 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200806 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200807 /* contention resolution establishment procedure */
808 if (sapi != 0) {
809 /* According to clause 6, the contention resolution
810 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200811 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200812 "resolution (discarding)\n");
813 msgb_free(msg);
814 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
815 }
816 /* transmit a SABM command with the P bit set to "1". The SABM
817 * command shall contain the layer 3 message unit */
818 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200819 } else {
820 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200821 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200822 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200823 }
824
825 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100826 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200827 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100828 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200829 msgb_free(msg);
830 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
831 }
832
rootaf48bed2011-09-26 11:23:06 +0200833 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100834 msgb_pull_to_l3(msg);
835 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200836
rootaf48bed2011-09-26 11:23:06 +0200837 /* prepare prim */
838 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200839
rootaf48bed2011-09-26 11:23:06 +0200840 /* send to L2 */
841 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200842}
843
844/* L3 requests transfer of unnumbered information */
845static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
846{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100847 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200848 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
849 uint8_t chan_nr = rllh->chan_nr;
850 uint8_t link_id = rllh->link_id;
851 uint8_t sapi = link_id & 7;
852 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +0100853 int length, ui_bts;
854
855 if (!le) {
856 LOGP(DLLAPD, LOGL_ERROR, "lapdm_datalink without entity error\n");
857 msgb_free(msg);
858 return -EMLINK;
859 }
860 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200861
862 /* check if the layer3 message length exceeds N201 */
863
864 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
865
866 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100867 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200868 }
869 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100870 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200871 }
872 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200873 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200874 "error\n");
875 msgb_free(msg);
876 return -EINVAL;
877 }
rootaf48bed2011-09-26 11:23:06 +0200878 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200879 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
880 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200881 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200882 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200883 "(discarding)\n", length,
884 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200885 msgb_free(msg);
886 return -EIO;
887 }
888
rootaf48bed2011-09-26 11:23:06 +0200889 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100890 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200891
rootaf48bed2011-09-26 11:23:06 +0200892 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100893 msgb_pull_to_l3(msg);
894 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200895
896 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200897 msg->l2h = msgb_push(msg, 2 + !ui_bts);
898 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
899 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100900 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200901 msg->l2h[2] = LAPDm_LEN(length);
902 if (link_id & 0x40) {
903 msg->l2h = msgb_push(msg, 2);
904 msg->l2h[0] = le->tx_power;
905 msg->l2h[1] = le->ta;
906 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200907
908 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200909 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200910}
911
912/* L3 requests transfer of acknowledged information */
913static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
914{
915 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
916 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200917 int length;
918 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200919
920 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
921 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200922 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200923 "error\n");
924 msgb_free(msg);
925 return -EINVAL;
926 }
rootaf48bed2011-09-26 11:23:06 +0200927 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
928 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200929
rootaf48bed2011-09-26 11:23:06 +0200930 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100931 msgb_pull_to_l3(msg);
932 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200933
rootaf48bed2011-09-26 11:23:06 +0200934 /* prepare prim */
935 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200936
rootaf48bed2011-09-26 11:23:06 +0200937 /* send to L2 */
938 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200939}
940
941/* L3 requests suspension of data link */
942static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
943{
944 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
945 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200946 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200947
948 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200949 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200950 msgb_free(msg);
951 return -EINVAL;
952 }
953
rootaf48bed2011-09-26 11:23:06 +0200954 /* prepare prim */
955 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200956
rootaf48bed2011-09-26 11:23:06 +0200957 /* send to L2 */
958 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200959}
960
961/* L3 requests resume of data link */
962static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
963{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200964 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200965 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200966 uint8_t chan_nr = rllh->chan_nr;
967 uint8_t link_id = rllh->link_id;
968 uint8_t sapi = rllh->link_id & 7;
969 struct tlv_parsed tv;
970 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100971 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200972 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200973
rootaf48bed2011-09-26 11:23:06 +0200974 /* Set LAPDm context for established connection */
975 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200976
977 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
978 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200979 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200980 msgb_free(msg);
981 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
982 }
rootaf48bed2011-09-26 11:23:06 +0200983 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200984 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
985
rootaf48bed2011-09-26 11:23:06 +0200986 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100987 msgb_pull_to_l3(msg);
988 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200989
rootaf48bed2011-09-26 11:23:06 +0200990 /* prepare prim */
991 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
992 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200993
rootaf48bed2011-09-26 11:23:06 +0200994 /* send to L2 */
995 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200996}
997
998/* L3 requests release of data link */
999static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1000{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001001 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001002 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001003 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001004
1005 /* get release mode */
1006 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1007 mode = rllh->data[1] & 1;
1008
Harald Welte1f0b8c22011-06-27 10:51:37 +02001009 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001010 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001011
1012 /* 04.06 3.8.3: No information field is permitted with the DISC
1013 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001014 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001015
rootaf48bed2011-09-26 11:23:06 +02001016 /* prepare prim */
1017 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1018 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001019
rootaf48bed2011-09-26 11:23:06 +02001020 /* send to L2 */
1021 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001022}
1023
1024/* L3 requests channel in idle state */
1025static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1026{
1027 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1028 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1029 struct osmo_phsap_prim pp;
1030
1031 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1032 PRIM_OP_REQUEST, NULL);
1033
1034 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001035 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001036 return -EINVAL;
1037 }
1038 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001039 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001040 return -EINVAL;
1041 }
1042 pp.u.rach_req.ra = cch->data[1];
1043 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1044 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1045
1046 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001047 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001048 return -EINVAL;
1049 }
1050 /* TA = 0 - delay */
1051 pp.u.rach_req.ta = 0 - cch->data[5];
1052
1053 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001054 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001055 return -EINVAL;
1056 }
1057 pp.u.rach_req.tx_power = cch->data[7];
1058
1059 msgb_free(msg);
1060
1061 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1062}
1063
1064/* L1 confirms channel request */
1065static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1066{
1067 struct abis_rsl_cchan_hdr *ch;
1068 struct gsm_time tm;
1069 struct gsm48_req_ref *ref;
1070
1071 gsm_fn2gsmtime(&tm, frame_nr);
1072
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001073 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001074 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1075 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1076 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1077 ch->chan_nr = RSL_CHAN_RACH;
1078 ch->data[0] = RSL_IE_REQ_REFERENCE;
1079 ref = (struct gsm48_req_ref *) (ch->data + 1);
1080 ref->t1 = tm.t1;
1081 ref->t2 = tm.t2;
1082 ref->t3_low = tm.t3 & 0x7;
1083 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001084
Harald Welte1f0b8c22011-06-27 10:51:37 +02001085 return rslms_sendmsg(msg, le);
1086}
1087
Harald Welte1f0b8c22011-06-27 10:51:37 +02001088/* incoming RSLms RLL message from L3 */
1089static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1090{
1091 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1092 int msg_type = rllh->c.msg_type;
1093 uint8_t sapi = rllh->link_id & 7;
1094 struct lapdm_entity *le;
1095 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001096 int rc = 0;
1097
1098 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001099 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001100 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001101 return -EINVAL;
1102 }
1103
1104 if (rllh->link_id & 0x40)
1105 le = &lc->lapdm_acch;
1106 else
1107 le = &lc->lapdm_dcch;
1108
Harald Welte1a87c1b2015-12-14 15:26:07 +01001109 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1110 * BTS side */
1111 if (le->mode == LAPDM_MODE_BTS) {
1112 switch (msg_type) {
1113 case RSL_MT_SUSP_REQ:
1114 case RSL_MT_RES_REQ:
1115 case RSL_MT_RECON_REQ:
1116 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1117 lc->name, rsl_msg_name(msg_type));
1118 msgb_free(msg);
1119 return -EINVAL;
1120 break;
1121 default:
1122 break;
1123 }
1124 }
1125
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001126 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001127 * SAPI.
1128 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001129 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001130 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001131 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001132 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001133 return -EINVAL;
1134 }
1135
Daniel Willmanne5233922012-12-25 23:15:50 +01001136 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001137 case RSL_MT_DATA_REQ:
1138 case RSL_MT_SUSP_REQ:
1139 case RSL_MT_REL_REQ:
1140 /* This is triggered in abnormal error conditions where
1141 * set_lapdm_context() was not called for the channel earlier. */
1142 if (!dl->dl.lctx.dl) {
1143 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1144 lc->name, rsl_msg_name(msg_type), sapi);
1145 msgb_free(msg);
1146 return -EINVAL;
1147 }
1148 break;
1149 default:
1150 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1151 lc->name, rsl_msg_name(msg_type), sapi);
1152 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001153
rootaf48bed2011-09-26 11:23:06 +02001154 switch (msg_type) {
1155 case RSL_MT_UNIT_DATA_REQ:
1156 rc = rslms_rx_rll_udata_req(msg, dl);
1157 break;
1158 case RSL_MT_EST_REQ:
1159 rc = rslms_rx_rll_est_req(msg, dl);
1160 break;
1161 case RSL_MT_DATA_REQ:
1162 rc = rslms_rx_rll_data_req(msg, dl);
1163 break;
1164 case RSL_MT_SUSP_REQ:
1165 rc = rslms_rx_rll_susp_req(msg, dl);
1166 break;
1167 case RSL_MT_RES_REQ:
1168 rc = rslms_rx_rll_res_req(msg, dl);
1169 break;
1170 case RSL_MT_RECON_REQ:
1171 rc = rslms_rx_rll_res_req(msg, dl);
1172 break;
1173 case RSL_MT_REL_REQ:
1174 rc = rslms_rx_rll_rel_req(msg, dl);
1175 break;
1176 default:
1177 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001178 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001179 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001180 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001181
1182 return rc;
1183}
1184
1185/* incoming RSLms COMMON CHANNEL message from L3 */
1186static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1187{
1188 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1189 int msg_type = cch->c.msg_type;
1190 int rc = 0;
1191
1192 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001193 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001194 return -EINVAL;
1195 }
1196
1197 switch (msg_type) {
1198 case RSL_MT_CHAN_RQD:
1199 /* create and send RACH request */
1200 rc = rslms_rx_chan_rqd(lc, msg);
1201 break;
1202 default:
rootaf48bed2011-09-26 11:23:06 +02001203 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001204 msg_type);
1205 msgb_free(msg);
1206 return 0;
1207 }
1208
1209 return rc;
1210}
1211
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001212/*! Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001213int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1214{
1215 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1216 int rc = 0;
1217
1218 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001219 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001220 return -EINVAL;
1221 }
1222
1223 switch (rslh->msg_discr & 0xfe) {
1224 case ABIS_RSL_MDISC_RLL:
1225 rc = rslms_rx_rll(msg, lc);
1226 break;
1227 case ABIS_RSL_MDISC_COM_CHAN:
1228 rc = rslms_rx_com_chan(msg, lc);
1229 break;
1230 default:
rootaf48bed2011-09-26 11:23:06 +02001231 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001232 "discriminator 0x%02x", rslh->msg_discr);
1233 msgb_free(msg);
1234 return -EINVAL;
1235 }
1236
1237 return rc;
1238}
1239
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001240/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001241int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1242{
rootaf48bed2011-09-26 11:23:06 +02001243 int i;
1244 enum lapd_mode lm;
1245
Harald Welte1f0b8c22011-06-27 10:51:37 +02001246 switch (mode) {
1247 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001248 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001249 break;
1250 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001251 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001252 break;
1253 default:
1254 return -EINVAL;
1255 }
1256
rootaf48bed2011-09-26 11:23:06 +02001257 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1258 lapd_set_mode(&le->datalink[i].dl, lm);
1259 }
1260
Harald Welte1f0b8c22011-06-27 10:51:37 +02001261 le->mode = mode;
1262
1263 return 0;
1264}
1265
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001266/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001267int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1268{
1269 int rc;
1270
1271 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1272 if (rc < 0)
1273 return rc;
1274
1275 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1276}
1277
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001278/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001279void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1280{
1281 lc->lapdm_dcch.l1_prim_cb = cb;
1282 lc->lapdm_acch.l1_prim_cb = cb;
1283 lc->lapdm_dcch.l1_ctx = ctx;
1284 lc->lapdm_acch.l1_ctx = ctx;
1285}
1286
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001287/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001288void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1289{
1290 lc->lapdm_dcch.l3_cb = cb;
1291 lc->lapdm_acch.l3_cb = cb;
1292 lc->lapdm_dcch.l3_ctx = ctx;
1293 lc->lapdm_acch.l3_ctx = ctx;
1294}
1295
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001296/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001297void lapdm_entity_reset(struct lapdm_entity *le)
1298{
1299 struct lapdm_datalink *dl;
1300 int i;
1301
1302 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1303 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001304 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001305 }
1306}
1307
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001308/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001309void lapdm_channel_reset(struct lapdm_channel *lc)
1310{
1311 lapdm_entity_reset(&lc->lapdm_dcch);
1312 lapdm_entity_reset(&lc->lapdm_acch);
1313}
1314
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001315/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001316void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1317{
1318 le->flags = flags;
1319}
1320
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001321/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001322void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1323{
1324 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1325 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1326}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001327
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001328/*! @} */