blob: 1152e0d4b26ff610e226e454ab251ce62aa994d8 [file] [log] [blame]
Harald Welte1f0b8c22011-06-27 10:51:37 +02001/* GSM LAPDm (TS 04.06) implementation */
2
3/* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
rootaf48bed2011-09-26 11:23:06 +02004 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
Harald Welte1f0b8c22011-06-27 10:51:37 +02005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Harald Welte6bdf0b12011-08-17 18:22:08 +020024/*! \addtogroup lapdm
25 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020026 * GSM LAPDm (TS 04.06) implementation
Harald Welte6bdf0b12011-08-17 18:22:08 +020027 */
28
29/*! \file lapdm.c */
30
Harald Welte1f0b8c22011-06-27 10:51:37 +020031#include <stdio.h>
32#include <stdint.h>
33#include <string.h>
34#include <errno.h>
Harald Welte1f0b8c22011-06-27 10:51:37 +020035
36#include <osmocom/core/logging.h>
37#include <osmocom/core/timer.h>
38#include <osmocom/core/msgb.h>
39#include <osmocom/core/utils.h>
40
41#include <osmocom/gsm/tlv.h>
42#include <osmocom/gsm/rsl.h>
43#include <osmocom/gsm/prim.h>
44#include <osmocom/gsm/gsm_utils.h>
45#include <osmocom/gsm/lapdm.h>
46
47#include <osmocom/gsm/protocol/gsm_04_08.h>
48#include <osmocom/gsm/protocol/gsm_08_58.h>
49
50/* TS 04.06 Figure 4 / Section 3.2 */
51#define LAPDm_LPD_NORMAL 0
52#define LAPDm_LPD_SMSCB 1
53#define LAPDm_SAPI_NORMAL 0
54#define LAPDm_SAPI_SMS 3
55#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
56
rootaf48bed2011-09-26 11:23:06 +020057#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020058#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
59#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
60#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
61
62/* TS 04.06 Table 3 / Section 3.4.3 */
63#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
64#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
65#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
66
67#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
68#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
69#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
70
71#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
72#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
73
74#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
75
76#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
77#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
78
Harald Welte1f0b8c22011-06-27 10:51:37 +020079#define LAPDm_LEN(len) ((len << 2) | 0x1)
80#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020081#define LAPDm_EL 0x1
82
83#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020084
85/* TS 04.06 Section 5.8.3 */
86#define N201_AB_SACCH 18
87#define N201_AB_SDCCH 20
88#define N201_AB_FACCH 20
89#define N201_Bbis 23
90#define N201_Bter_SACCH 21
91#define N201_Bter_SDCCH 23
92#define N201_Bter_FACCH 23
93#define N201_B4 19
94
95/* 5.8.2.1 N200 during establish and release */
96#define N200_EST_REL 5
97/* 5.8.2.1 N200 during timer recovery state */
98#define N200_TR_SACCH 5
99#define N200_TR_SDCCH 23
100#define N200_TR_FACCH_FR 34
101#define N200_TR_EFACCH_FR 48
102#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200103/* FIXME: set N200 depending on chan_nr */
104#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200105
106enum lapdm_format {
107 LAPDm_FMT_A,
108 LAPDm_FMT_B,
109 LAPDm_FMT_Bbis,
110 LAPDm_FMT_Bter,
111 LAPDm_FMT_B4,
112};
113
Maxadef12a2016-05-25 15:25:02 +0200114const struct value_string osmo_ph_prim_names[] = {
115 { PRIM_PH_DATA, "PH-DATA" },
116 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
117 { PRIM_PH_CONN, "PH-CONNECT" },
118 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
119 { PRIM_PH_RTS, "PH-RTS" },
120 { PRIM_MPH_INFO, "MPH-INFO" },
121 { PRIM_TCH, "TCH" },
122 { PRIM_TCH_RTS, "TCH-RTS" },
123 { 0, NULL }
124};
125
rootaf48bed2011-09-26 11:23:06 +0200126static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
127static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
128 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100129static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200130
131static void lapdm_dl_init(struct lapdm_datalink *dl,
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100132 struct lapdm_entity *entity, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200133{
134 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200135 dl->entity = entity;
rootaf48bed2011-09-26 11:23:06 +0200136 lapd_dl_init(&dl->dl, 1, 8, 200);
137 dl->dl.reestablish = 0; /* GSM uses no reestablish */
138 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
139 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100140 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200141 dl->dl.n200_est_rel = N200_EST_REL;
142 dl->dl.n200 = N200;
143 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100144 dl->dl.t200_sec = t200; dl->dl.t200_usec = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200145}
146
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200147/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200148 * \param[in] le LAPDm entity
149 * \param[in] mode \ref lapdm_mode (BTS/MS)
150 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100151void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200152{
153 unsigned int i;
154
155 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100156 lapdm_dl_init(&le->datalink[i], le, t200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200157
158 lapdm_entity_set_mode(le, mode);
159}
160
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200161/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200162 * \param[in] lc \ref lapdm_channel to be initialized
163 * \param[in] mode \ref lapdm_mode (BTS/MS)
164 *
165 * This really is a convenience wrapper around calling \ref
166 * lapdm_entity_init twice.
167 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200168void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
169{
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100170 lapdm_entity_init(&lc->lapdm_acch, mode, 2);
rootaf48bed2011-09-26 11:23:06 +0200171 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100172 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200173}
174
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200175/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200176void lapdm_entity_exit(struct lapdm_entity *le)
177{
178 unsigned int i;
179 struct lapdm_datalink *dl;
180
181 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
182 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200183 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200184 }
185}
186
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200187/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200188 *
189 * A convenience wrapper calling \ref lapdm_entity_exit on both
190 * entities inside the \ref lapdm_channel
191 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200192void lapdm_channel_exit(struct lapdm_channel *lc)
193{
194 lapdm_entity_exit(&lc->lapdm_acch);
195 lapdm_entity_exit(&lc->lapdm_dcch);
196}
197
Daniel Willmann55405fb2014-03-26 13:45:17 +0100198struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200199{
200 switch (sapi) {
201 case LAPDm_SAPI_NORMAL:
202 return &le->datalink[0];
203 case LAPDm_SAPI_SMS:
204 return &le->datalink[1];
205 default:
206 return NULL;
207 }
208}
209
Harald Welte1f0b8c22011-06-27 10:51:37 +0200210/* Append padding (if required) */
211static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
212{
213 int pad_len = n201 - msgb_l2len(msg);
214 uint8_t *data;
215
216 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200217 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200218 "cannot pad message that is already too big!\n");
219 return;
220 }
221
222 data = msgb_put(msg, pad_len);
223 memset(data, 0x2B, pad_len);
224}
225
226/* input function that L2 calls when sending messages up to L3 */
227static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
228{
229 if (!le->l3_cb) {
230 msgb_free(msg);
231 return -EIO;
232 }
233
234 /* call the layer2 message handler that is registered */
235 return le->l3_cb(msg, le, le->l3_ctx);
236}
237
238/* write a frame into the tx queue */
239static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200240 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200241{
242 struct lapdm_entity *le = dl->entity;
243 struct osmo_phsap_prim pp;
244
245 /* if there is a pending message, queue it */
246 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200247 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200248 *msgb_push(msg, 1) = link_id;
249 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200250 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200251 return -EBUSY;
252 }
253
254 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
255 PRIM_OP_REQUEST, msg);
256 pp.u.data.chan_nr = chan_nr;
257 pp.u.data.link_id = link_id;
258
259 /* send the frame now */
260 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200261 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200262
263 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
264}
265
266static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
267{
268 struct lapdm_datalink *dl;
269 int last = le->last_tx_dequeue;
270 int i = last, n = ARRAY_SIZE(le->datalink);
271 struct msgb *msg = NULL;
272
273 /* round-robin dequeue */
274 do {
275 /* next */
276 i = (i + 1) % n;
277 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200278 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200279 break;
280 } while (i != last);
281
282 if (msg) {
283 /* Set last dequeue position */
284 le->last_tx_dequeue = i;
285 }
286
287 return msg;
288}
289
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200290/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200291 * a osmo_phsap_prim */
292int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
293{
294 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200295 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200296
297 msg = tx_dequeue_msgb(le);
298 if (!msg)
299 return -ENODEV;
300
301 /* if we have a message, send PH-DATA.req */
302 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
303 PRIM_OP_REQUEST, msg);
304
305 /* Pull chan_nr and link_id */
306 pp->u.data.chan_nr = *msg->data;
307 msgb_pull(msg, 1);
308 pp->u.data.link_id = *msg->data;
309 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200310 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200311 msgb_pull(msg, 1);
312
313 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200314 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200315
316 return 0;
317}
318
319/* get next frame from the tx queue. because the ms has multiple datalinks,
320 * each datalink's queue is read round-robin.
321 */
322static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
323{
324 struct osmo_phsap_prim pp;
325
326 /* we may send again */
327 le->tx_pending = 0;
328
329 /* free confirm message */
330 if (msg)
331 msgb_free(msg);
332
333 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
334 /* no message in all queues */
335
336 /* If user didn't request PH-EMPTY_FRAME.req, abort */
337 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
338 return 0;
339
340 /* otherwise, send PH-EMPTY_FRAME.req */
341 osmo_prim_init(&pp.oph, SAP_GSM_PH,
342 PRIM_PH_EMPTY_FRAME,
343 PRIM_OP_REQUEST, NULL);
344 } else {
345 le->tx_pending = 1;
346 }
347
348 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
349}
350
351/* Create RSLms various RSLms messages */
352static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
353 struct msgb *msg)
354{
355 /* Add the RSL + RLL header */
356 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
357
358 /* send off the RSLms message to L3 */
359 return rslms_sendmsg(msg, mctx->dl->entity);
360}
361
362/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
363static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
364{
365 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
366 struct abis_rsl_rll_hdr *rllh;
367
368 /* Add the RSL + RLL header */
369 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
370 msgb_push(msg, 2 + 2);
371 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
372 mctx->link_id, 1);
373 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
374
375 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
376 rllh->data[1] = mctx->ta_ind;
377
378 rllh->data[2] = RSL_IE_MS_POWER;
379 rllh->data[3] = mctx->tx_power_ind;
380
381 return rslms_sendmsg(msg, mctx->dl->entity);
382}
383
384static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
385{
386 struct msgb *msg;
387
388 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
389
390 /* send off the RSLms message to L3 */
391 return rslms_sendmsg(msg, mctx->dl->entity);
392}
393
394static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
395{
396 struct msgb *msg;
397
rootaf48bed2011-09-26 11:23:06 +0200398 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200399 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200400 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
401 return rslms_sendmsg(msg, mctx->dl->entity);
402}
403
rootaf48bed2011-09-26 11:23:06 +0200404/* DLSAP L2 -> L3 (RSLms) */
405static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
406 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200407{
rootaf48bed2011-09-26 11:23:06 +0200408 struct lapd_datalink *dl = lctx->dl;
409 struct lapdm_datalink *mdl =
410 container_of(dl, struct lapdm_datalink, dl);
411 struct lapdm_msg_ctx *mctx = &mdl->mctx;
412 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200413
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200414 switch (OSMO_PRIM_HDR(&dp->oph)) {
415 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
Andreas Eversberg5977db02013-06-12 09:34:51 +0200416 if (dp->oph.msg && dp->oph.msg->len == 0) {
417 /* omit L3 info by freeing message */
418 msgb_free(dp->oph.msg);
419 dp->oph.msg = NULL;
420 }
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200421 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200422 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200423 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
424 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200425 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200426 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
427 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200428 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200429 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
430 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
431 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
432 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200433 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200434 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
435 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200436 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200437 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
438 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200439 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200440 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
441 rsl_rll_error(dp->u.error_ind.cause, mctx);
442 if (dp->oph.msg)
443 msgb_free(dp->oph.msg);
444 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200445 }
rootaf48bed2011-09-26 11:23:06 +0200446
447 if (!rll_msg) {
448 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
449 "fix!\n", dp->oph.primitive, dp->oph.operation);
450 return -EINVAL;
451 }
452
453 if (!dp->oph.msg)
454 return send_rll_simple(rll_msg, mctx);
455
456 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200457}
458
rootaf48bed2011-09-26 11:23:06 +0200459/* send a data frame to layer 1 */
460static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200461{
rootaf48bed2011-09-26 11:23:06 +0200462 uint8_t l3_len = msg->tail - msg->data;
463 struct lapd_datalink *dl = lctx->dl;
464 struct lapdm_datalink *mdl =
465 container_of(dl, struct lapdm_datalink, dl);
466 struct lapdm_msg_ctx *mctx = &mdl->mctx;
467 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200468
rootaf48bed2011-09-26 11:23:06 +0200469 /* prepend l2 header */
470 msg->l2h = msgb_push(msg, 3);
471 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
472 /* EA is set here too */
473 switch (format) {
474 case LAPD_FORM_I:
475 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
476 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200477 break;
rootaf48bed2011-09-26 11:23:06 +0200478 case LAPD_FORM_S:
479 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200480 break;
rootaf48bed2011-09-26 11:23:06 +0200481 case LAPD_FORM_U:
482 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200483 break;
484 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200485 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200486 return -EINVAL;
487 }
rootaf48bed2011-09-26 11:23:06 +0200488 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
489 if (lctx->more)
490 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200491
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100492 /* add ACCH header with last indicated tx-power and TA */
493 if ((mctx->link_id & 0x40)) {
494 struct lapdm_entity *le = mdl->entity;
495
496 msg->l2h = msgb_push(msg, 2);
497 msg->l2h[0] = le->tx_power;
498 msg->l2h[1] = le->ta;
499 }
500
rootaf48bed2011-09-26 11:23:06 +0200501 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
502 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200503}
504
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100505static int update_pending_frames(struct lapd_msg_ctx *lctx)
506{
507 struct lapd_datalink *dl = lctx->dl;
508 struct msgb *msg;
509 int rc = -1;
510
511 llist_for_each_entry(msg, &dl->tx_queue, list) {
512 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
513 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
514 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
515 rc = 0;
516 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
517 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
518 }
519 }
520
521 return rc;
522}
523
Harald Welte1f0b8c22011-06-27 10:51:37 +0200524/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200525static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
526 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200527{
528 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200529 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200530 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200531 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200532 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200533 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200534
535 /* when we reach here, we have a msgb with l2h pointing to the raw
536 * 23byte mac block. The l1h has already been purged. */
537
rootaf48bed2011-09-26 11:23:06 +0200538 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200539 mctx.chan_nr = chan_nr;
540 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200541
Harald Welte1f0b8c22011-06-27 10:51:37 +0200542 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
543 if (cbits == 0x10 || cbits == 0x12) {
544 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
545 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200546 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200547 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200548 } else {
549 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200550 /* It was received from network on SACCH */
551
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100552 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
553 if (le->mode == LAPDM_MODE_MS
554 && LAPDm_CTRL_is_U(msg->l2h[3])
555 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200556 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200557 n201 = N201_B4;
558 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200559 } else {
560 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200561 n201 = N201_AB_SACCH;
562 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200563 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200564 /* SACCH frames have a two-byte L1 header that
565 * OsmocomBB L1 doesn't strip */
566 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
567 mctx.ta_ind = msg->l2h[1];
568 msgb_pull(msg, 2);
569 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200570 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200571 } else {
572 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200573 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100574 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200575 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200576 }
577 }
578
Daniel Willmann55405fb2014-03-26 13:45:17 +0100579 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200580 /* G.2.1 No action on frames containing an unallocated SAPI. */
581 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200582 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200583 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200584 msgb_free(msg);
585 return -EIO;
586 }
587
Harald Welte1f0b8c22011-06-27 10:51:37 +0200588 switch (mctx.lapdm_fmt) {
589 case LAPDm_FMT_A:
590 case LAPDm_FMT_B:
591 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200592 lctx.dl = &mctx.dl->dl;
593 /* obtain SAPI from address field */
594 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
595 /* G.2.3 EA bit set to "0" is not allowed in GSM */
596 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
597 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
598 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200599 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200600 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200601 return -EINVAL;
602 }
rootaf48bed2011-09-26 11:23:06 +0200603 /* adress field */
604 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
605 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
606 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
607 /* command field */
608 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
609 lctx.format = LAPD_FORM_I;
610 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
611 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
612 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
613 lctx.format = LAPD_FORM_S;
614 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
615 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
616 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
617 lctx.format = LAPD_FORM_U;
618 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
619 } else
620 lctx.format = LAPD_FORM_UKN;
621 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
622 if (lctx.sapi != LAPDm_SAPI_NORMAL
623 && lctx.sapi != LAPDm_SAPI_SMS
624 && lctx.format == LAPD_FORM_U
625 && lctx.s_u == LAPDm_U_UI) {
626 /* 5.3.3 UI frames with invalid SAPI values shall be
627 * discarded
628 */
629 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
630 lctx.sapi);
631 msgb_free(msg);
632 return 0;
633 }
634 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
635 lctx.n201 = n201;
636 lctx.length = n201;
637 lctx.more = 0;
638 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100639 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200640 } else {
641 /* length field */
642 if (!(msg->l2h[2] & LAPDm_EL)) {
643 /* G.4.1 If the EL bit is set to "0", an
644 * MDL-ERROR-INDICATION primitive with cause
645 * "frame not implemented" is sent to the
646 * mobile management entity. */
647 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
648 "multi-octet length\n");
649 msgb_free(msg);
650 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
651 return -EINVAL;
652 }
653 lctx.n201 = n201;
654 lctx.length = msg->l2h[2] >> 2;
655 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
656 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100657 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200658 }
659 /* store context for messages from lapd */
660 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
661 /* send to LAPD */
662 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200663 break;
664 case LAPDm_FMT_Bter:
665 /* FIXME */
666 msgb_free(msg);
667 break;
668 case LAPDm_FMT_Bbis:
669 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200670 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200671 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100672 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200673 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
674 break;
675 default:
676 msgb_free(msg);
677 }
678
679 return rc;
680}
681
682/* input into layer2 (from layer 1) */
683static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
684{
685 struct abis_rsl_cchan_hdr *ch;
686 struct gsm48_req_ref req_ref;
687 struct gsm_time gt;
688 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
689
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200690 if (!msg)
691 return -ENOMEM;
692
Harald Welte1f0b8c22011-06-27 10:51:37 +0200693 msg->l2h = msgb_push(msg, sizeof(*ch));
694 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
695 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
696 ch->chan_nr = RSL_CHAN_RACH;
697
698 /* generate a RSL CHANNEL REQUIRED message */
699 gsm_fn2gsmtime(&gt, fn);
700 req_ref.ra = ra;
701 req_ref.t1 = gt.t1; /* FIXME: modulo? */
702 req_ref.t2 = gt.t2;
703 req_ref.t3_low = gt.t3 & 7;
704 req_ref.t3_high = gt.t3 >> 3;
705
706 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
707 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
708
709 return rslms_sendmsg(msg, le);
710}
711
712static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
713
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200714/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200715int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
716{
717 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
718 int rc = 0;
719
720 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200721 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200722 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100723 rc = -ENODEV;
724 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200725 }
726
727 switch (oph->primitive) {
728 case PRIM_PH_DATA:
729 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200730 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200731 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100732 rc = -ENODEV;
733 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200734 }
735 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
736 pp->u.data.link_id);
737 break;
738 case PRIM_PH_RTS:
739 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200740 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200741 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100742 rc = -ENODEV;
743 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200744 }
745 rc = l2_ph_data_conf(oph->msg, le);
746 break;
747 case PRIM_PH_RACH:
748 switch (oph->operation) {
749 case PRIM_OP_INDICATION:
750 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
751 pp->u.rach_ind.acc_delay);
752 break;
753 case PRIM_OP_CONFIRM:
754 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
755 break;
756 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100757 rc = -EIO;
758 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200759 }
760 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100761 default:
762 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
763 oph->primitive);
764 rc = -EINVAL;
765 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200766 }
767
768 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100769
770free:
771 msgb_free(oph->msg);
772 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200773}
774
775
776/* L3 -> L2 / RSLMS -> LAPDm */
777
rootaf48bed2011-09-26 11:23:06 +0200778/* Set LAPDm context for established connection */
779static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
780 uint8_t link_id, int n201, uint8_t sapi)
781{
782 memset(&dl->mctx, 0, sizeof(dl->mctx));
783 dl->mctx.dl = dl;
784 dl->mctx.chan_nr = chan_nr;
785 dl->mctx.link_id = link_id;
786 dl->dl.lctx.dl = &dl->dl;
787 dl->dl.lctx.n201 = n201;
788 dl->dl.lctx.sapi = sapi;
789
790 return 0;
791}
792
Harald Welte1f0b8c22011-06-27 10:51:37 +0200793/* L3 requests establishment of data link */
794static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
795{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200796 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
797 uint8_t chan_nr = rllh->chan_nr;
798 uint8_t link_id = rllh->link_id;
799 uint8_t sapi = rllh->link_id & 7;
800 struct tlv_parsed tv;
801 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100802 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200803 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200804
rootaf48bed2011-09-26 11:23:06 +0200805 /* Set LAPDm context for established connection */
806 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200807
rootaf48bed2011-09-26 11:23:06 +0200808 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200809 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200810 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200811 /* contention resolution establishment procedure */
812 if (sapi != 0) {
813 /* According to clause 6, the contention resolution
814 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200815 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200816 "resolution (discarding)\n");
817 msgb_free(msg);
818 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
819 }
820 /* transmit a SABM command with the P bit set to "1". The SABM
821 * command shall contain the layer 3 message unit */
822 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200823 } else {
824 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200825 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200826 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200827 }
828
829 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100830 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200831 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100832 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200833 msgb_free(msg);
834 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
835 }
836
rootaf48bed2011-09-26 11:23:06 +0200837 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100838 msgb_pull_to_l3(msg);
839 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200840
rootaf48bed2011-09-26 11:23:06 +0200841 /* prepare prim */
842 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200843
rootaf48bed2011-09-26 11:23:06 +0200844 /* send to L2 */
845 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200846}
847
848/* L3 requests transfer of unnumbered information */
849static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
850{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100851 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200852 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
853 uint8_t chan_nr = rllh->chan_nr;
854 uint8_t link_id = rllh->link_id;
855 uint8_t sapi = link_id & 7;
856 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +0100857 int length, ui_bts;
858
859 if (!le) {
860 LOGP(DLLAPD, LOGL_ERROR, "lapdm_datalink without entity error\n");
861 msgb_free(msg);
862 return -EMLINK;
863 }
864 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200865
866 /* check if the layer3 message length exceeds N201 */
867
868 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
869
870 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100871 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200872 }
873 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100874 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200875 }
876 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200877 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200878 "error\n");
879 msgb_free(msg);
880 return -EINVAL;
881 }
rootaf48bed2011-09-26 11:23:06 +0200882 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200883 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
884 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200885 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200886 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200887 "(discarding)\n", length,
888 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200889 msgb_free(msg);
890 return -EIO;
891 }
892
rootaf48bed2011-09-26 11:23:06 +0200893 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100894 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200895
rootaf48bed2011-09-26 11:23:06 +0200896 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100897 msgb_pull_to_l3(msg);
898 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200899
900 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200901 msg->l2h = msgb_push(msg, 2 + !ui_bts);
902 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
903 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100904 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200905 msg->l2h[2] = LAPDm_LEN(length);
906 if (link_id & 0x40) {
907 msg->l2h = msgb_push(msg, 2);
908 msg->l2h[0] = le->tx_power;
909 msg->l2h[1] = le->ta;
910 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200911
912 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200913 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200914}
915
916/* L3 requests transfer of acknowledged information */
917static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
918{
919 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
920 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200921 int length;
922 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200923
924 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
925 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200926 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200927 "error\n");
928 msgb_free(msg);
929 return -EINVAL;
930 }
rootaf48bed2011-09-26 11:23:06 +0200931 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
932 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200933
rootaf48bed2011-09-26 11:23:06 +0200934 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100935 msgb_pull_to_l3(msg);
936 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200937
rootaf48bed2011-09-26 11:23:06 +0200938 /* prepare prim */
939 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200940
rootaf48bed2011-09-26 11:23:06 +0200941 /* send to L2 */
942 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200943}
944
945/* L3 requests suspension of data link */
946static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
947{
948 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
949 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200950 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200951
952 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200953 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200954 msgb_free(msg);
955 return -EINVAL;
956 }
957
rootaf48bed2011-09-26 11:23:06 +0200958 /* prepare prim */
959 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200960
rootaf48bed2011-09-26 11:23:06 +0200961 /* send to L2 */
962 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200963}
964
965/* L3 requests resume of data link */
966static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
967{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200968 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200969 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200970 uint8_t chan_nr = rllh->chan_nr;
971 uint8_t link_id = rllh->link_id;
972 uint8_t sapi = rllh->link_id & 7;
973 struct tlv_parsed tv;
974 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100975 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200976 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200977
rootaf48bed2011-09-26 11:23:06 +0200978 /* Set LAPDm context for established connection */
979 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200980
981 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
982 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200983 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200984 msgb_free(msg);
985 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
986 }
rootaf48bed2011-09-26 11:23:06 +0200987 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200988 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
989
rootaf48bed2011-09-26 11:23:06 +0200990 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100991 msgb_pull_to_l3(msg);
992 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200993
rootaf48bed2011-09-26 11:23:06 +0200994 /* prepare prim */
995 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
996 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200997
rootaf48bed2011-09-26 11:23:06 +0200998 /* send to L2 */
999 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001000}
1001
1002/* L3 requests release of data link */
1003static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1004{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001005 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001006 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001007 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001008
1009 /* get release mode */
1010 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1011 mode = rllh->data[1] & 1;
1012
Harald Welte1f0b8c22011-06-27 10:51:37 +02001013 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001014 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001015
1016 /* 04.06 3.8.3: No information field is permitted with the DISC
1017 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001018 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001019
rootaf48bed2011-09-26 11:23:06 +02001020 /* prepare prim */
1021 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1022 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001023
rootaf48bed2011-09-26 11:23:06 +02001024 /* send to L2 */
1025 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001026}
1027
1028/* L3 requests channel in idle state */
1029static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1030{
1031 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1032 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1033 struct osmo_phsap_prim pp;
1034
1035 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1036 PRIM_OP_REQUEST, NULL);
1037
1038 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001039 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001040 return -EINVAL;
1041 }
1042 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001043 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001044 return -EINVAL;
1045 }
1046 pp.u.rach_req.ra = cch->data[1];
1047 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1048 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1049
1050 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001051 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001052 return -EINVAL;
1053 }
1054 /* TA = 0 - delay */
1055 pp.u.rach_req.ta = 0 - cch->data[5];
1056
1057 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001058 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001059 return -EINVAL;
1060 }
1061 pp.u.rach_req.tx_power = cch->data[7];
1062
1063 msgb_free(msg);
1064
1065 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1066}
1067
1068/* L1 confirms channel request */
1069static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1070{
1071 struct abis_rsl_cchan_hdr *ch;
1072 struct gsm_time tm;
1073 struct gsm48_req_ref *ref;
1074
1075 gsm_fn2gsmtime(&tm, frame_nr);
1076
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001077 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001078 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1079 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1080 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1081 ch->chan_nr = RSL_CHAN_RACH;
1082 ch->data[0] = RSL_IE_REQ_REFERENCE;
1083 ref = (struct gsm48_req_ref *) (ch->data + 1);
1084 ref->t1 = tm.t1;
1085 ref->t2 = tm.t2;
1086 ref->t3_low = tm.t3 & 0x7;
1087 ref->t3_high = tm.t3 >> 3;
1088
1089 return rslms_sendmsg(msg, le);
1090}
1091
Harald Welte1f0b8c22011-06-27 10:51:37 +02001092/* incoming RSLms RLL message from L3 */
1093static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1094{
1095 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1096 int msg_type = rllh->c.msg_type;
1097 uint8_t sapi = rllh->link_id & 7;
1098 struct lapdm_entity *le;
1099 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001100 int rc = 0;
1101
1102 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001103 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001104 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001105 return -EINVAL;
1106 }
1107
1108 if (rllh->link_id & 0x40)
1109 le = &lc->lapdm_acch;
1110 else
1111 le = &lc->lapdm_dcch;
1112
Harald Welte1a87c1b2015-12-14 15:26:07 +01001113 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1114 * BTS side */
1115 if (le->mode == LAPDM_MODE_BTS) {
1116 switch (msg_type) {
1117 case RSL_MT_SUSP_REQ:
1118 case RSL_MT_RES_REQ:
1119 case RSL_MT_RECON_REQ:
1120 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1121 lc->name, rsl_msg_name(msg_type));
1122 msgb_free(msg);
1123 return -EINVAL;
1124 break;
1125 default:
1126 break;
1127 }
1128 }
1129
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001130 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001131 * SAPI.
1132 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001133 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001134 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001135 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001136 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001137 return -EINVAL;
1138 }
1139
Daniel Willmanne5233922012-12-25 23:15:50 +01001140 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001141 case RSL_MT_DATA_REQ:
1142 case RSL_MT_SUSP_REQ:
1143 case RSL_MT_REL_REQ:
1144 /* This is triggered in abnormal error conditions where
1145 * set_lapdm_context() was not called for the channel earlier. */
1146 if (!dl->dl.lctx.dl) {
1147 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1148 lc->name, rsl_msg_name(msg_type), sapi);
1149 msgb_free(msg);
1150 return -EINVAL;
1151 }
1152 break;
1153 default:
1154 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1155 lc->name, rsl_msg_name(msg_type), sapi);
1156 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001157
rootaf48bed2011-09-26 11:23:06 +02001158 switch (msg_type) {
1159 case RSL_MT_UNIT_DATA_REQ:
1160 rc = rslms_rx_rll_udata_req(msg, dl);
1161 break;
1162 case RSL_MT_EST_REQ:
1163 rc = rslms_rx_rll_est_req(msg, dl);
1164 break;
1165 case RSL_MT_DATA_REQ:
1166 rc = rslms_rx_rll_data_req(msg, dl);
1167 break;
1168 case RSL_MT_SUSP_REQ:
1169 rc = rslms_rx_rll_susp_req(msg, dl);
1170 break;
1171 case RSL_MT_RES_REQ:
1172 rc = rslms_rx_rll_res_req(msg, dl);
1173 break;
1174 case RSL_MT_RECON_REQ:
1175 rc = rslms_rx_rll_res_req(msg, dl);
1176 break;
1177 case RSL_MT_REL_REQ:
1178 rc = rslms_rx_rll_rel_req(msg, dl);
1179 break;
1180 default:
1181 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001182 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001183 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001184 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001185
1186 return rc;
1187}
1188
1189/* incoming RSLms COMMON CHANNEL message from L3 */
1190static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1191{
1192 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1193 int msg_type = cch->c.msg_type;
1194 int rc = 0;
1195
1196 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001197 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001198 return -EINVAL;
1199 }
1200
1201 switch (msg_type) {
1202 case RSL_MT_CHAN_RQD:
1203 /* create and send RACH request */
1204 rc = rslms_rx_chan_rqd(lc, msg);
1205 break;
1206 default:
rootaf48bed2011-09-26 11:23:06 +02001207 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001208 msg_type);
1209 msgb_free(msg);
1210 return 0;
1211 }
1212
1213 return rc;
1214}
1215
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001216/*! Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001217int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1218{
1219 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1220 int rc = 0;
1221
1222 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001223 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001224 return -EINVAL;
1225 }
1226
1227 switch (rslh->msg_discr & 0xfe) {
1228 case ABIS_RSL_MDISC_RLL:
1229 rc = rslms_rx_rll(msg, lc);
1230 break;
1231 case ABIS_RSL_MDISC_COM_CHAN:
1232 rc = rslms_rx_com_chan(msg, lc);
1233 break;
1234 default:
rootaf48bed2011-09-26 11:23:06 +02001235 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001236 "discriminator 0x%02x", rslh->msg_discr);
1237 msgb_free(msg);
1238 return -EINVAL;
1239 }
1240
1241 return rc;
1242}
1243
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001244/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001245int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1246{
rootaf48bed2011-09-26 11:23:06 +02001247 int i;
1248 enum lapd_mode lm;
1249
Harald Welte1f0b8c22011-06-27 10:51:37 +02001250 switch (mode) {
1251 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001252 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001253 break;
1254 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001255 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001256 break;
1257 default:
1258 return -EINVAL;
1259 }
1260
rootaf48bed2011-09-26 11:23:06 +02001261 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1262 lapd_set_mode(&le->datalink[i].dl, lm);
1263 }
1264
Harald Welte1f0b8c22011-06-27 10:51:37 +02001265 le->mode = mode;
1266
1267 return 0;
1268}
1269
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001270/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001271int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1272{
1273 int rc;
1274
1275 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1276 if (rc < 0)
1277 return rc;
1278
1279 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1280}
1281
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001282/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001283void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1284{
1285 lc->lapdm_dcch.l1_prim_cb = cb;
1286 lc->lapdm_acch.l1_prim_cb = cb;
1287 lc->lapdm_dcch.l1_ctx = ctx;
1288 lc->lapdm_acch.l1_ctx = ctx;
1289}
1290
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001291/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001292void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1293{
1294 lc->lapdm_dcch.l3_cb = cb;
1295 lc->lapdm_acch.l3_cb = cb;
1296 lc->lapdm_dcch.l3_ctx = ctx;
1297 lc->lapdm_acch.l3_ctx = ctx;
1298}
1299
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001300/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001301void lapdm_entity_reset(struct lapdm_entity *le)
1302{
1303 struct lapdm_datalink *dl;
1304 int i;
1305
1306 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1307 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001308 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001309 }
1310}
1311
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001312/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001313void lapdm_channel_reset(struct lapdm_channel *lc)
1314{
1315 lapdm_entity_reset(&lc->lapdm_dcch);
1316 lapdm_entity_reset(&lc->lapdm_acch);
1317}
1318
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001319/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001320void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1321{
1322 le->flags = flags;
1323}
1324
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001325/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001326void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1327{
1328 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1329 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1330}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001331
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001332/*! @} */