blob: fa7769b20a48aa074d16d77204567678f24097fe [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 * @{
26 */
27
28/*! \file lapdm.c */
29
Harald Welte1f0b8c22011-06-27 10:51:37 +020030#include <stdio.h>
31#include <stdint.h>
32#include <string.h>
33#include <errno.h>
34#include <arpa/inet.h>
35
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
Harald Welte6bdf0b12011-08-17 18:22:08 +0200147/*! \brief initialize a LAPDm entity and all datalinks inside
148 * \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
Harald Welte6bdf0b12011-08-17 18:22:08 +0200161/*! \brief initialize a LAPDm channel and all its channels
162 * \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
Harald Welte6bdf0b12011-08-17 18:22:08 +0200175/*! \brief 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
Harald Welte6bdf0b12011-08-17 18:22:08 +0200187/* \brief lfush and release all resources in LAPDm channel
188 *
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
Harald Welte6bdf0b12011-08-17 18:22:08 +0200290/*! \brief 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
Harald Welte6bdf0b12011-08-17 18:22:08 +0200714/*! \brief 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;
Andreas Eversberg5977db02013-06-12 09:34:51 +0200855 int ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200856 uint8_t sapi = link_id & 7;
857 struct tlv_parsed tv;
858 int length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200859
860 /* check if the layer3 message length exceeds N201 */
861
862 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
863
864 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100865 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200866 }
867 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100868 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200869 }
870 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200871 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200872 "error\n");
873 msgb_free(msg);
874 return -EINVAL;
875 }
rootaf48bed2011-09-26 11:23:06 +0200876 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200877 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
878 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200879 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200880 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200881 "(discarding)\n", length,
882 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200883 msgb_free(msg);
884 return -EIO;
885 }
886
rootaf48bed2011-09-26 11:23:06 +0200887 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100888 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200889
rootaf48bed2011-09-26 11:23:06 +0200890 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100891 msgb_pull_to_l3(msg);
892 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200893
894 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200895 msg->l2h = msgb_push(msg, 2 + !ui_bts);
896 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
897 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100898 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200899 msg->l2h[2] = LAPDm_LEN(length);
900 if (link_id & 0x40) {
901 msg->l2h = msgb_push(msg, 2);
902 msg->l2h[0] = le->tx_power;
903 msg->l2h[1] = le->ta;
904 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200905
906 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200907 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200908}
909
910/* L3 requests transfer of acknowledged information */
911static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
912{
913 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
914 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200915 int length;
916 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200917
918 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
919 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200920 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200921 "error\n");
922 msgb_free(msg);
923 return -EINVAL;
924 }
rootaf48bed2011-09-26 11:23:06 +0200925 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
926 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200927
rootaf48bed2011-09-26 11:23:06 +0200928 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100929 msgb_pull_to_l3(msg);
930 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200931
rootaf48bed2011-09-26 11:23:06 +0200932 /* prepare prim */
933 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200934
rootaf48bed2011-09-26 11:23:06 +0200935 /* send to L2 */
936 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200937}
938
939/* L3 requests suspension of data link */
940static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
941{
942 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
943 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200944 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200945
946 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200947 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200948 msgb_free(msg);
949 return -EINVAL;
950 }
951
rootaf48bed2011-09-26 11:23:06 +0200952 /* prepare prim */
953 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200954
rootaf48bed2011-09-26 11:23:06 +0200955 /* send to L2 */
956 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200957}
958
959/* L3 requests resume of data link */
960static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
961{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200962 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200963 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200964 uint8_t chan_nr = rllh->chan_nr;
965 uint8_t link_id = rllh->link_id;
966 uint8_t sapi = rllh->link_id & 7;
967 struct tlv_parsed tv;
968 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100969 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200970 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200971
rootaf48bed2011-09-26 11:23:06 +0200972 /* Set LAPDm context for established connection */
973 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200974
975 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
976 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200977 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200978 msgb_free(msg);
979 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
980 }
rootaf48bed2011-09-26 11:23:06 +0200981 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200982 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
983
rootaf48bed2011-09-26 11:23:06 +0200984 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100985 msgb_pull_to_l3(msg);
986 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200987
rootaf48bed2011-09-26 11:23:06 +0200988 /* prepare prim */
989 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
990 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200991
rootaf48bed2011-09-26 11:23:06 +0200992 /* send to L2 */
993 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200994}
995
996/* L3 requests release of data link */
997static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
998{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200999 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001000 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001001 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001002
1003 /* get release mode */
1004 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1005 mode = rllh->data[1] & 1;
1006
Harald Welte1f0b8c22011-06-27 10:51:37 +02001007 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001008 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001009
1010 /* 04.06 3.8.3: No information field is permitted with the DISC
1011 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001012 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001013
rootaf48bed2011-09-26 11:23:06 +02001014 /* prepare prim */
1015 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1016 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001017
rootaf48bed2011-09-26 11:23:06 +02001018 /* send to L2 */
1019 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001020}
1021
1022/* L3 requests channel in idle state */
1023static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1024{
1025 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1026 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1027 struct osmo_phsap_prim pp;
1028
1029 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1030 PRIM_OP_REQUEST, NULL);
1031
1032 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001033 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001034 return -EINVAL;
1035 }
1036 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001037 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001038 return -EINVAL;
1039 }
1040 pp.u.rach_req.ra = cch->data[1];
1041 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1042 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1043
1044 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001045 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001046 return -EINVAL;
1047 }
1048 /* TA = 0 - delay */
1049 pp.u.rach_req.ta = 0 - cch->data[5];
1050
1051 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001052 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001053 return -EINVAL;
1054 }
1055 pp.u.rach_req.tx_power = cch->data[7];
1056
1057 msgb_free(msg);
1058
1059 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1060}
1061
1062/* L1 confirms channel request */
1063static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1064{
1065 struct abis_rsl_cchan_hdr *ch;
1066 struct gsm_time tm;
1067 struct gsm48_req_ref *ref;
1068
1069 gsm_fn2gsmtime(&tm, frame_nr);
1070
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001071 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001072 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1073 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1074 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1075 ch->chan_nr = RSL_CHAN_RACH;
1076 ch->data[0] = RSL_IE_REQ_REFERENCE;
1077 ref = (struct gsm48_req_ref *) (ch->data + 1);
1078 ref->t1 = tm.t1;
1079 ref->t2 = tm.t2;
1080 ref->t3_low = tm.t3 & 0x7;
1081 ref->t3_high = tm.t3 >> 3;
1082
1083 return rslms_sendmsg(msg, le);
1084}
1085
Harald Welte1f0b8c22011-06-27 10:51:37 +02001086/* incoming RSLms RLL message from L3 */
1087static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1088{
1089 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1090 int msg_type = rllh->c.msg_type;
1091 uint8_t sapi = rllh->link_id & 7;
1092 struct lapdm_entity *le;
1093 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001094 int rc = 0;
1095
1096 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001097 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001098 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001099 return -EINVAL;
1100 }
1101
1102 if (rllh->link_id & 0x40)
1103 le = &lc->lapdm_acch;
1104 else
1105 le = &lc->lapdm_dcch;
1106
Harald Welte1a87c1b2015-12-14 15:26:07 +01001107 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1108 * BTS side */
1109 if (le->mode == LAPDM_MODE_BTS) {
1110 switch (msg_type) {
1111 case RSL_MT_SUSP_REQ:
1112 case RSL_MT_RES_REQ:
1113 case RSL_MT_RECON_REQ:
1114 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1115 lc->name, rsl_msg_name(msg_type));
1116 msgb_free(msg);
1117 return -EINVAL;
1118 break;
1119 default:
1120 break;
1121 }
1122 }
1123
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001124 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001125 * SAPI.
1126 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001127 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001128 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001129 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001130 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001131 return -EINVAL;
1132 }
1133
Daniel Willmanne5233922012-12-25 23:15:50 +01001134 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001135 case RSL_MT_DATA_REQ:
1136 case RSL_MT_SUSP_REQ:
1137 case RSL_MT_REL_REQ:
1138 /* This is triggered in abnormal error conditions where
1139 * set_lapdm_context() was not called for the channel earlier. */
1140 if (!dl->dl.lctx.dl) {
1141 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1142 lc->name, rsl_msg_name(msg_type), sapi);
1143 msgb_free(msg);
1144 return -EINVAL;
1145 }
1146 break;
1147 default:
1148 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1149 lc->name, rsl_msg_name(msg_type), sapi);
1150 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001151
rootaf48bed2011-09-26 11:23:06 +02001152 switch (msg_type) {
1153 case RSL_MT_UNIT_DATA_REQ:
1154 rc = rslms_rx_rll_udata_req(msg, dl);
1155 break;
1156 case RSL_MT_EST_REQ:
1157 rc = rslms_rx_rll_est_req(msg, dl);
1158 break;
1159 case RSL_MT_DATA_REQ:
1160 rc = rslms_rx_rll_data_req(msg, dl);
1161 break;
1162 case RSL_MT_SUSP_REQ:
1163 rc = rslms_rx_rll_susp_req(msg, dl);
1164 break;
1165 case RSL_MT_RES_REQ:
1166 rc = rslms_rx_rll_res_req(msg, dl);
1167 break;
1168 case RSL_MT_RECON_REQ:
1169 rc = rslms_rx_rll_res_req(msg, dl);
1170 break;
1171 case RSL_MT_REL_REQ:
1172 rc = rslms_rx_rll_rel_req(msg, dl);
1173 break;
1174 default:
1175 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001176 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001177 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001178 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001179
1180 return rc;
1181}
1182
1183/* incoming RSLms COMMON CHANNEL message from L3 */
1184static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1185{
1186 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1187 int msg_type = cch->c.msg_type;
1188 int rc = 0;
1189
1190 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001191 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001192 return -EINVAL;
1193 }
1194
1195 switch (msg_type) {
1196 case RSL_MT_CHAN_RQD:
1197 /* create and send RACH request */
1198 rc = rslms_rx_chan_rqd(lc, msg);
1199 break;
1200 default:
rootaf48bed2011-09-26 11:23:06 +02001201 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001202 msg_type);
1203 msgb_free(msg);
1204 return 0;
1205 }
1206
1207 return rc;
1208}
1209
Harald Welte6bdf0b12011-08-17 18:22:08 +02001210/*! \brief Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001211int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1212{
1213 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1214 int rc = 0;
1215
1216 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001217 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001218 return -EINVAL;
1219 }
1220
1221 switch (rslh->msg_discr & 0xfe) {
1222 case ABIS_RSL_MDISC_RLL:
1223 rc = rslms_rx_rll(msg, lc);
1224 break;
1225 case ABIS_RSL_MDISC_COM_CHAN:
1226 rc = rslms_rx_com_chan(msg, lc);
1227 break;
1228 default:
rootaf48bed2011-09-26 11:23:06 +02001229 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001230 "discriminator 0x%02x", rslh->msg_discr);
1231 msgb_free(msg);
1232 return -EINVAL;
1233 }
1234
1235 return rc;
1236}
1237
Harald Welte6bdf0b12011-08-17 18:22:08 +02001238/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001239int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1240{
rootaf48bed2011-09-26 11:23:06 +02001241 int i;
1242 enum lapd_mode lm;
1243
Harald Welte1f0b8c22011-06-27 10:51:37 +02001244 switch (mode) {
1245 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001246 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001247 break;
1248 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001249 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001250 break;
1251 default:
1252 return -EINVAL;
1253 }
1254
rootaf48bed2011-09-26 11:23:06 +02001255 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1256 lapd_set_mode(&le->datalink[i].dl, lm);
1257 }
1258
Harald Welte1f0b8c22011-06-27 10:51:37 +02001259 le->mode = mode;
1260
1261 return 0;
1262}
1263
Harald Welte6bdf0b12011-08-17 18:22:08 +02001264/*! \brief Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001265int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1266{
1267 int rc;
1268
1269 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1270 if (rc < 0)
1271 return rc;
1272
1273 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1274}
1275
Harald Welte6bdf0b12011-08-17 18:22:08 +02001276/*! \brief Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001277void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1278{
1279 lc->lapdm_dcch.l1_prim_cb = cb;
1280 lc->lapdm_acch.l1_prim_cb = cb;
1281 lc->lapdm_dcch.l1_ctx = ctx;
1282 lc->lapdm_acch.l1_ctx = ctx;
1283}
1284
Harald Welte6bdf0b12011-08-17 18:22:08 +02001285/*! \brief Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001286void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1287{
1288 lc->lapdm_dcch.l3_cb = cb;
1289 lc->lapdm_acch.l3_cb = cb;
1290 lc->lapdm_dcch.l3_ctx = ctx;
1291 lc->lapdm_acch.l3_ctx = ctx;
1292}
1293
Harald Welte6bdf0b12011-08-17 18:22:08 +02001294/*! \brief Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001295void lapdm_entity_reset(struct lapdm_entity *le)
1296{
1297 struct lapdm_datalink *dl;
1298 int i;
1299
1300 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1301 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001302 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001303 }
1304}
1305
Harald Welte6bdf0b12011-08-17 18:22:08 +02001306/*! \brief Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001307void lapdm_channel_reset(struct lapdm_channel *lc)
1308{
1309 lapdm_entity_reset(&lc->lapdm_dcch);
1310 lapdm_entity_reset(&lc->lapdm_acch);
1311}
1312
Harald Welte6bdf0b12011-08-17 18:22:08 +02001313/*! \brief Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001314void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1315{
1316 le->flags = flags;
1317}
1318
Harald Welte6bdf0b12011-08-17 18:22:08 +02001319/*! \brief Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001320void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1321{
1322 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1323 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1324}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001325
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001326/*! @} */