blob: 698f8502891f98bdf6111de7f77309dc8c3a1f72 [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
rootaf48bed2011-09-26 11:23:06 +0200114static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
115static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
116 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100117static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200118
119static void lapdm_dl_init(struct lapdm_datalink *dl,
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100120 struct lapdm_entity *entity, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200121{
122 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200123 dl->entity = entity;
rootaf48bed2011-09-26 11:23:06 +0200124 lapd_dl_init(&dl->dl, 1, 8, 200);
125 dl->dl.reestablish = 0; /* GSM uses no reestablish */
126 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
127 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100128 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200129 dl->dl.n200_est_rel = N200_EST_REL;
130 dl->dl.n200 = N200;
131 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100132 dl->dl.t200_sec = t200; dl->dl.t200_usec = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200133}
134
Harald Welte6bdf0b12011-08-17 18:22:08 +0200135/*! \brief initialize a LAPDm entity and all datalinks inside
136 * \param[in] le LAPDm entity
137 * \param[in] mode \ref lapdm_mode (BTS/MS)
138 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100139void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200140{
141 unsigned int i;
142
143 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100144 lapdm_dl_init(&le->datalink[i], le, t200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200145
146 lapdm_entity_set_mode(le, mode);
147}
148
Harald Welte6bdf0b12011-08-17 18:22:08 +0200149/*! \brief initialize a LAPDm channel and all its channels
150 * \param[in] lc \ref lapdm_channel to be initialized
151 * \param[in] mode \ref lapdm_mode (BTS/MS)
152 *
153 * This really is a convenience wrapper around calling \ref
154 * lapdm_entity_init twice.
155 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200156void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
157{
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100158 lapdm_entity_init(&lc->lapdm_acch, mode, 2);
rootaf48bed2011-09-26 11:23:06 +0200159 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100160 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200161}
162
Harald Welte6bdf0b12011-08-17 18:22:08 +0200163/*! \brief flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200164void lapdm_entity_exit(struct lapdm_entity *le)
165{
166 unsigned int i;
167 struct lapdm_datalink *dl;
168
169 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
170 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200171 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200172 }
173}
174
Harald Welte6bdf0b12011-08-17 18:22:08 +0200175/* \brief lfush and release all resources in LAPDm channel
176 *
177 * A convenience wrapper calling \ref lapdm_entity_exit on both
178 * entities inside the \ref lapdm_channel
179 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200180void lapdm_channel_exit(struct lapdm_channel *lc)
181{
182 lapdm_entity_exit(&lc->lapdm_acch);
183 lapdm_entity_exit(&lc->lapdm_dcch);
184}
185
Daniel Willmann55405fb2014-03-26 13:45:17 +0100186struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200187{
188 switch (sapi) {
189 case LAPDm_SAPI_NORMAL:
190 return &le->datalink[0];
191 case LAPDm_SAPI_SMS:
192 return &le->datalink[1];
193 default:
194 return NULL;
195 }
196}
197
Harald Welte1f0b8c22011-06-27 10:51:37 +0200198/* Append padding (if required) */
199static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
200{
201 int pad_len = n201 - msgb_l2len(msg);
202 uint8_t *data;
203
204 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200205 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200206 "cannot pad message that is already too big!\n");
207 return;
208 }
209
210 data = msgb_put(msg, pad_len);
211 memset(data, 0x2B, pad_len);
212}
213
214/* input function that L2 calls when sending messages up to L3 */
215static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
216{
217 if (!le->l3_cb) {
218 msgb_free(msg);
219 return -EIO;
220 }
221
222 /* call the layer2 message handler that is registered */
223 return le->l3_cb(msg, le, le->l3_ctx);
224}
225
226/* write a frame into the tx queue */
227static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200228 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200229{
230 struct lapdm_entity *le = dl->entity;
231 struct osmo_phsap_prim pp;
232
233 /* if there is a pending message, queue it */
234 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200235 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200236 *msgb_push(msg, 1) = link_id;
237 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200238 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200239 return -EBUSY;
240 }
241
242 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
243 PRIM_OP_REQUEST, msg);
244 pp.u.data.chan_nr = chan_nr;
245 pp.u.data.link_id = link_id;
246
247 /* send the frame now */
248 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200249 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200250
251 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
252}
253
254static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
255{
256 struct lapdm_datalink *dl;
257 int last = le->last_tx_dequeue;
258 int i = last, n = ARRAY_SIZE(le->datalink);
259 struct msgb *msg = NULL;
260
261 /* round-robin dequeue */
262 do {
263 /* next */
264 i = (i + 1) % n;
265 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200266 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200267 break;
268 } while (i != last);
269
270 if (msg) {
271 /* Set last dequeue position */
272 le->last_tx_dequeue = i;
273 }
274
275 return msg;
276}
277
Harald Welte6bdf0b12011-08-17 18:22:08 +0200278/*! \brief dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200279 * a osmo_phsap_prim */
280int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
281{
282 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200283 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200284
285 msg = tx_dequeue_msgb(le);
286 if (!msg)
287 return -ENODEV;
288
289 /* if we have a message, send PH-DATA.req */
290 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
291 PRIM_OP_REQUEST, msg);
292
293 /* Pull chan_nr and link_id */
294 pp->u.data.chan_nr = *msg->data;
295 msgb_pull(msg, 1);
296 pp->u.data.link_id = *msg->data;
297 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200298 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200299 msgb_pull(msg, 1);
300
301 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200302 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200303
304 return 0;
305}
306
307/* get next frame from the tx queue. because the ms has multiple datalinks,
308 * each datalink's queue is read round-robin.
309 */
310static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
311{
312 struct osmo_phsap_prim pp;
313
314 /* we may send again */
315 le->tx_pending = 0;
316
317 /* free confirm message */
318 if (msg)
319 msgb_free(msg);
320
321 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
322 /* no message in all queues */
323
324 /* If user didn't request PH-EMPTY_FRAME.req, abort */
325 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
326 return 0;
327
328 /* otherwise, send PH-EMPTY_FRAME.req */
329 osmo_prim_init(&pp.oph, SAP_GSM_PH,
330 PRIM_PH_EMPTY_FRAME,
331 PRIM_OP_REQUEST, NULL);
332 } else {
333 le->tx_pending = 1;
334 }
335
336 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
337}
338
339/* Create RSLms various RSLms messages */
340static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
341 struct msgb *msg)
342{
343 /* Add the RSL + RLL header */
344 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
345
346 /* send off the RSLms message to L3 */
347 return rslms_sendmsg(msg, mctx->dl->entity);
348}
349
350/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
351static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
352{
353 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
354 struct abis_rsl_rll_hdr *rllh;
355
356 /* Add the RSL + RLL header */
357 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
358 msgb_push(msg, 2 + 2);
359 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
360 mctx->link_id, 1);
361 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
362
363 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
364 rllh->data[1] = mctx->ta_ind;
365
366 rllh->data[2] = RSL_IE_MS_POWER;
367 rllh->data[3] = mctx->tx_power_ind;
368
369 return rslms_sendmsg(msg, mctx->dl->entity);
370}
371
372static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
373{
374 struct msgb *msg;
375
376 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
377
378 /* send off the RSLms message to L3 */
379 return rslms_sendmsg(msg, mctx->dl->entity);
380}
381
382static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
383{
384 struct msgb *msg;
385
rootaf48bed2011-09-26 11:23:06 +0200386 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200387 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200388 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
389 return rslms_sendmsg(msg, mctx->dl->entity);
390}
391
rootaf48bed2011-09-26 11:23:06 +0200392/* DLSAP L2 -> L3 (RSLms) */
393static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
394 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200395{
rootaf48bed2011-09-26 11:23:06 +0200396 struct lapd_datalink *dl = lctx->dl;
397 struct lapdm_datalink *mdl =
398 container_of(dl, struct lapdm_datalink, dl);
399 struct lapdm_msg_ctx *mctx = &mdl->mctx;
400 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200401
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200402 switch (OSMO_PRIM_HDR(&dp->oph)) {
403 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
Andreas Eversberg5977db02013-06-12 09:34:51 +0200404 if (dp->oph.msg && dp->oph.msg->len == 0) {
405 /* omit L3 info by freeing message */
406 msgb_free(dp->oph.msg);
407 dp->oph.msg = NULL;
408 }
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200409 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200410 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200411 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
412 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200413 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200414 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
415 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200416 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200417 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
418 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
419 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
420 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200421 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200422 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
423 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200424 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200425 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
426 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200427 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200428 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
429 rsl_rll_error(dp->u.error_ind.cause, mctx);
430 if (dp->oph.msg)
431 msgb_free(dp->oph.msg);
432 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200433 }
rootaf48bed2011-09-26 11:23:06 +0200434
435 if (!rll_msg) {
436 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
437 "fix!\n", dp->oph.primitive, dp->oph.operation);
438 return -EINVAL;
439 }
440
441 if (!dp->oph.msg)
442 return send_rll_simple(rll_msg, mctx);
443
444 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200445}
446
rootaf48bed2011-09-26 11:23:06 +0200447/* send a data frame to layer 1 */
448static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200449{
rootaf48bed2011-09-26 11:23:06 +0200450 uint8_t l3_len = msg->tail - msg->data;
451 struct lapd_datalink *dl = lctx->dl;
452 struct lapdm_datalink *mdl =
453 container_of(dl, struct lapdm_datalink, dl);
454 struct lapdm_msg_ctx *mctx = &mdl->mctx;
455 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200456
rootaf48bed2011-09-26 11:23:06 +0200457 /* prepend l2 header */
458 msg->l2h = msgb_push(msg, 3);
459 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
460 /* EA is set here too */
461 switch (format) {
462 case LAPD_FORM_I:
463 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
464 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200465 break;
rootaf48bed2011-09-26 11:23:06 +0200466 case LAPD_FORM_S:
467 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200468 break;
rootaf48bed2011-09-26 11:23:06 +0200469 case LAPD_FORM_U:
470 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200471 break;
472 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200473 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200474 return -EINVAL;
475 }
rootaf48bed2011-09-26 11:23:06 +0200476 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
477 if (lctx->more)
478 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200479
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100480 /* add ACCH header with last indicated tx-power and TA */
481 if ((mctx->link_id & 0x40)) {
482 struct lapdm_entity *le = mdl->entity;
483
484 msg->l2h = msgb_push(msg, 2);
485 msg->l2h[0] = le->tx_power;
486 msg->l2h[1] = le->ta;
487 }
488
rootaf48bed2011-09-26 11:23:06 +0200489 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
490 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200491}
492
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100493static int update_pending_frames(struct lapd_msg_ctx *lctx)
494{
495 struct lapd_datalink *dl = lctx->dl;
496 struct msgb *msg;
497 int rc = -1;
498
499 llist_for_each_entry(msg, &dl->tx_queue, list) {
500 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
501 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
502 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
503 rc = 0;
504 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
505 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
506 }
507 }
508
509 return rc;
510}
511
Harald Welte1f0b8c22011-06-27 10:51:37 +0200512/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200513static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
514 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200515{
516 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200517 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200518 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200519 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200520 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200521 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200522
523 /* when we reach here, we have a msgb with l2h pointing to the raw
524 * 23byte mac block. The l1h has already been purged. */
525
rootaf48bed2011-09-26 11:23:06 +0200526 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200527 mctx.chan_nr = chan_nr;
528 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200529
Harald Welte1f0b8c22011-06-27 10:51:37 +0200530 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
531 if (cbits == 0x10 || cbits == 0x12) {
532 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
533 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200534 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200535 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200536 } else {
537 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200538 /* It was received from network on SACCH */
539
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100540 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
541 if (le->mode == LAPDM_MODE_MS
542 && LAPDm_CTRL_is_U(msg->l2h[3])
543 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200544 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200545 n201 = N201_B4;
546 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200547 } else {
548 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200549 n201 = N201_AB_SACCH;
550 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200551 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200552 /* SACCH frames have a two-byte L1 header that
553 * OsmocomBB L1 doesn't strip */
554 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
555 mctx.ta_ind = msg->l2h[1];
556 msgb_pull(msg, 2);
557 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200558 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200559 } else {
560 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200561 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100562 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200563 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200564 }
565 }
566
Daniel Willmann55405fb2014-03-26 13:45:17 +0100567 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200568 /* G.2.1 No action on frames containing an unallocated SAPI. */
569 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200570 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200571 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200572 msgb_free(msg);
573 return -EIO;
574 }
575
Harald Welte1f0b8c22011-06-27 10:51:37 +0200576 switch (mctx.lapdm_fmt) {
577 case LAPDm_FMT_A:
578 case LAPDm_FMT_B:
579 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200580 lctx.dl = &mctx.dl->dl;
581 /* obtain SAPI from address field */
582 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
583 /* G.2.3 EA bit set to "0" is not allowed in GSM */
584 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
585 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
586 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200587 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200588 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200589 return -EINVAL;
590 }
rootaf48bed2011-09-26 11:23:06 +0200591 /* adress field */
592 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
593 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
594 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
595 /* command field */
596 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
597 lctx.format = LAPD_FORM_I;
598 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
599 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
600 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
601 lctx.format = LAPD_FORM_S;
602 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
603 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
604 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
605 lctx.format = LAPD_FORM_U;
606 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
607 } else
608 lctx.format = LAPD_FORM_UKN;
609 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
610 if (lctx.sapi != LAPDm_SAPI_NORMAL
611 && lctx.sapi != LAPDm_SAPI_SMS
612 && lctx.format == LAPD_FORM_U
613 && lctx.s_u == LAPDm_U_UI) {
614 /* 5.3.3 UI frames with invalid SAPI values shall be
615 * discarded
616 */
617 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
618 lctx.sapi);
619 msgb_free(msg);
620 return 0;
621 }
622 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
623 lctx.n201 = n201;
624 lctx.length = n201;
625 lctx.more = 0;
626 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100627 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200628 } else {
629 /* length field */
630 if (!(msg->l2h[2] & LAPDm_EL)) {
631 /* G.4.1 If the EL bit is set to "0", an
632 * MDL-ERROR-INDICATION primitive with cause
633 * "frame not implemented" is sent to the
634 * mobile management entity. */
635 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
636 "multi-octet length\n");
637 msgb_free(msg);
638 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
639 return -EINVAL;
640 }
641 lctx.n201 = n201;
642 lctx.length = msg->l2h[2] >> 2;
643 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
644 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100645 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200646 }
647 /* store context for messages from lapd */
648 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
649 /* send to LAPD */
650 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200651 break;
652 case LAPDm_FMT_Bter:
653 /* FIXME */
654 msgb_free(msg);
655 break;
656 case LAPDm_FMT_Bbis:
657 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200658 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200659 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100660 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200661 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
662 break;
663 default:
664 msgb_free(msg);
665 }
666
667 return rc;
668}
669
670/* input into layer2 (from layer 1) */
671static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
672{
673 struct abis_rsl_cchan_hdr *ch;
674 struct gsm48_req_ref req_ref;
675 struct gsm_time gt;
676 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
677
678 msg->l2h = msgb_push(msg, sizeof(*ch));
679 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
680 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
681 ch->chan_nr = RSL_CHAN_RACH;
682
683 /* generate a RSL CHANNEL REQUIRED message */
684 gsm_fn2gsmtime(&gt, fn);
685 req_ref.ra = ra;
686 req_ref.t1 = gt.t1; /* FIXME: modulo? */
687 req_ref.t2 = gt.t2;
688 req_ref.t3_low = gt.t3 & 7;
689 req_ref.t3_high = gt.t3 >> 3;
690
691 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
692 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
693
694 return rslms_sendmsg(msg, le);
695}
696
697static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
698
Harald Welte6bdf0b12011-08-17 18:22:08 +0200699/*! \brief Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200700int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
701{
702 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
703 int rc = 0;
704
705 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200706 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200707 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100708 rc = -ENODEV;
709 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200710 }
711
712 switch (oph->primitive) {
713 case PRIM_PH_DATA:
714 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200715 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200716 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100717 rc = -ENODEV;
718 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200719 }
720 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
721 pp->u.data.link_id);
722 break;
723 case PRIM_PH_RTS:
724 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200725 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200726 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100727 rc = -ENODEV;
728 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200729 }
730 rc = l2_ph_data_conf(oph->msg, le);
731 break;
732 case PRIM_PH_RACH:
733 switch (oph->operation) {
734 case PRIM_OP_INDICATION:
735 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
736 pp->u.rach_ind.acc_delay);
737 break;
738 case PRIM_OP_CONFIRM:
739 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
740 break;
741 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100742 rc = -EIO;
743 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200744 }
745 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100746 default:
747 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
748 oph->primitive);
749 rc = -EINVAL;
750 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200751 }
752
753 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100754
755free:
756 msgb_free(oph->msg);
757 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200758}
759
760
761/* L3 -> L2 / RSLMS -> LAPDm */
762
rootaf48bed2011-09-26 11:23:06 +0200763/* Set LAPDm context for established connection */
764static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
765 uint8_t link_id, int n201, uint8_t sapi)
766{
767 memset(&dl->mctx, 0, sizeof(dl->mctx));
768 dl->mctx.dl = dl;
769 dl->mctx.chan_nr = chan_nr;
770 dl->mctx.link_id = link_id;
771 dl->dl.lctx.dl = &dl->dl;
772 dl->dl.lctx.n201 = n201;
773 dl->dl.lctx.sapi = sapi;
774
775 return 0;
776}
777
Harald Welte1f0b8c22011-06-27 10:51:37 +0200778/* L3 requests establishment of data link */
779static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
780{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200781 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
782 uint8_t chan_nr = rllh->chan_nr;
783 uint8_t link_id = rllh->link_id;
784 uint8_t sapi = rllh->link_id & 7;
785 struct tlv_parsed tv;
786 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100787 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200788 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200789
rootaf48bed2011-09-26 11:23:06 +0200790 /* Set LAPDm context for established connection */
791 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200792
rootaf48bed2011-09-26 11:23:06 +0200793 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200794 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200795 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200796 /* contention resolution establishment procedure */
797 if (sapi != 0) {
798 /* According to clause 6, the contention resolution
799 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200800 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200801 "resolution (discarding)\n");
802 msgb_free(msg);
803 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
804 }
805 /* transmit a SABM command with the P bit set to "1". The SABM
806 * command shall contain the layer 3 message unit */
807 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200808 } else {
809 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200810 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200811 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200812 }
813
814 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100815 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200816 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100817 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200818 msgb_free(msg);
819 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
820 }
821
rootaf48bed2011-09-26 11:23:06 +0200822 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100823 msgb_pull_to_l3(msg);
824 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200825
rootaf48bed2011-09-26 11:23:06 +0200826 /* prepare prim */
827 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200828
rootaf48bed2011-09-26 11:23:06 +0200829 /* send to L2 */
830 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200831}
832
833/* L3 requests transfer of unnumbered information */
834static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
835{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100836 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200837 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
838 uint8_t chan_nr = rllh->chan_nr;
839 uint8_t link_id = rllh->link_id;
Andreas Eversberg5977db02013-06-12 09:34:51 +0200840 int ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200841 uint8_t sapi = link_id & 7;
842 struct tlv_parsed tv;
843 int length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200844
845 /* check if the layer3 message length exceeds N201 */
846
847 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
848
849 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100850 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200851 }
852 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100853 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200854 }
855 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200856 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200857 "error\n");
858 msgb_free(msg);
859 return -EINVAL;
860 }
rootaf48bed2011-09-26 11:23:06 +0200861 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200862 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
863 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200864 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200865 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200866 "(discarding)\n", length,
867 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200868 msgb_free(msg);
869 return -EIO;
870 }
871
rootaf48bed2011-09-26 11:23:06 +0200872 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100873 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200874
rootaf48bed2011-09-26 11:23:06 +0200875 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100876 msgb_pull_to_l3(msg);
877 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200878
879 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200880 msg->l2h = msgb_push(msg, 2 + !ui_bts);
881 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
882 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100883 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200884 msg->l2h[2] = LAPDm_LEN(length);
885 if (link_id & 0x40) {
886 msg->l2h = msgb_push(msg, 2);
887 msg->l2h[0] = le->tx_power;
888 msg->l2h[1] = le->ta;
889 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200890
891 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200892 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200893}
894
895/* L3 requests transfer of acknowledged information */
896static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
897{
898 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
899 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200900 int length;
901 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200902
903 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
904 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200905 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200906 "error\n");
907 msgb_free(msg);
908 return -EINVAL;
909 }
rootaf48bed2011-09-26 11:23:06 +0200910 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
911 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200912
rootaf48bed2011-09-26 11:23:06 +0200913 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100914 msgb_pull_to_l3(msg);
915 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200916
rootaf48bed2011-09-26 11:23:06 +0200917 /* prepare prim */
918 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200919
rootaf48bed2011-09-26 11:23:06 +0200920 /* send to L2 */
921 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200922}
923
924/* L3 requests suspension of data link */
925static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
926{
927 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
928 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200929 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200930
931 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200932 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200933 msgb_free(msg);
934 return -EINVAL;
935 }
936
rootaf48bed2011-09-26 11:23:06 +0200937 /* prepare prim */
938 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200939
rootaf48bed2011-09-26 11:23:06 +0200940 /* send to L2 */
941 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200942}
943
944/* L3 requests resume of data link */
945static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
946{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200947 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200948 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200949 uint8_t chan_nr = rllh->chan_nr;
950 uint8_t link_id = rllh->link_id;
951 uint8_t sapi = rllh->link_id & 7;
952 struct tlv_parsed tv;
953 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100954 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200955 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200956
rootaf48bed2011-09-26 11:23:06 +0200957 /* Set LAPDm context for established connection */
958 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200959
960 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
961 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200962 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200963 msgb_free(msg);
964 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
965 }
rootaf48bed2011-09-26 11:23:06 +0200966 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200967 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
968
rootaf48bed2011-09-26 11:23:06 +0200969 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100970 msgb_pull_to_l3(msg);
971 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200972
rootaf48bed2011-09-26 11:23:06 +0200973 /* prepare prim */
974 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
975 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200976
rootaf48bed2011-09-26 11:23:06 +0200977 /* send to L2 */
978 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200979}
980
981/* L3 requests release of data link */
982static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
983{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200984 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200985 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +0200986 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200987
988 /* get release mode */
989 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
990 mode = rllh->data[1] & 1;
991
Harald Welte1f0b8c22011-06-27 10:51:37 +0200992 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100993 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +0200994
995 /* 04.06 3.8.3: No information field is permitted with the DISC
996 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100997 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200998
rootaf48bed2011-09-26 11:23:06 +0200999 /* prepare prim */
1000 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1001 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001002
rootaf48bed2011-09-26 11:23:06 +02001003 /* send to L2 */
1004 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001005}
1006
1007/* L3 requests channel in idle state */
1008static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1009{
1010 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1011 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1012 struct osmo_phsap_prim pp;
1013
1014 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1015 PRIM_OP_REQUEST, NULL);
1016
1017 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001018 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001019 return -EINVAL;
1020 }
1021 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001022 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001023 return -EINVAL;
1024 }
1025 pp.u.rach_req.ra = cch->data[1];
1026 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1027 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1028
1029 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001030 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001031 return -EINVAL;
1032 }
1033 /* TA = 0 - delay */
1034 pp.u.rach_req.ta = 0 - cch->data[5];
1035
1036 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001037 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001038 return -EINVAL;
1039 }
1040 pp.u.rach_req.tx_power = cch->data[7];
1041
1042 msgb_free(msg);
1043
1044 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1045}
1046
1047/* L1 confirms channel request */
1048static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1049{
1050 struct abis_rsl_cchan_hdr *ch;
1051 struct gsm_time tm;
1052 struct gsm48_req_ref *ref;
1053
1054 gsm_fn2gsmtime(&tm, frame_nr);
1055
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001056 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001057 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1058 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1059 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1060 ch->chan_nr = RSL_CHAN_RACH;
1061 ch->data[0] = RSL_IE_REQ_REFERENCE;
1062 ref = (struct gsm48_req_ref *) (ch->data + 1);
1063 ref->t1 = tm.t1;
1064 ref->t2 = tm.t2;
1065 ref->t3_low = tm.t3 & 0x7;
1066 ref->t3_high = tm.t3 >> 3;
1067
1068 return rslms_sendmsg(msg, le);
1069}
1070
Harald Welte1f0b8c22011-06-27 10:51:37 +02001071/* incoming RSLms RLL message from L3 */
1072static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1073{
1074 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1075 int msg_type = rllh->c.msg_type;
1076 uint8_t sapi = rllh->link_id & 7;
1077 struct lapdm_entity *le;
1078 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001079 int rc = 0;
1080
1081 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001082 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001083 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001084 return -EINVAL;
1085 }
1086
1087 if (rllh->link_id & 0x40)
1088 le = &lc->lapdm_acch;
1089 else
1090 le = &lc->lapdm_dcch;
1091
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001092 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001093 * SAPI.
1094 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001095 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001096 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001097 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001098 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001099 return -EINVAL;
1100 }
1101
Daniel Willmanne5233922012-12-25 23:15:50 +01001102 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001103 case RSL_MT_DATA_REQ:
1104 case RSL_MT_SUSP_REQ:
1105 case RSL_MT_REL_REQ:
1106 /* This is triggered in abnormal error conditions where
1107 * set_lapdm_context() was not called for the channel earlier. */
1108 if (!dl->dl.lctx.dl) {
1109 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1110 lc->name, rsl_msg_name(msg_type), sapi);
1111 msgb_free(msg);
1112 return -EINVAL;
1113 }
1114 break;
1115 default:
1116 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1117 lc->name, rsl_msg_name(msg_type), sapi);
1118 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001119
rootaf48bed2011-09-26 11:23:06 +02001120 switch (msg_type) {
1121 case RSL_MT_UNIT_DATA_REQ:
1122 rc = rslms_rx_rll_udata_req(msg, dl);
1123 break;
1124 case RSL_MT_EST_REQ:
1125 rc = rslms_rx_rll_est_req(msg, dl);
1126 break;
1127 case RSL_MT_DATA_REQ:
1128 rc = rslms_rx_rll_data_req(msg, dl);
1129 break;
1130 case RSL_MT_SUSP_REQ:
1131 rc = rslms_rx_rll_susp_req(msg, dl);
1132 break;
1133 case RSL_MT_RES_REQ:
1134 rc = rslms_rx_rll_res_req(msg, dl);
1135 break;
1136 case RSL_MT_RECON_REQ:
1137 rc = rslms_rx_rll_res_req(msg, dl);
1138 break;
1139 case RSL_MT_REL_REQ:
1140 rc = rslms_rx_rll_rel_req(msg, dl);
1141 break;
1142 default:
1143 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001144 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001145 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001146 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001147
1148 return rc;
1149}
1150
1151/* incoming RSLms COMMON CHANNEL message from L3 */
1152static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1153{
1154 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1155 int msg_type = cch->c.msg_type;
1156 int rc = 0;
1157
1158 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001159 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001160 return -EINVAL;
1161 }
1162
1163 switch (msg_type) {
1164 case RSL_MT_CHAN_RQD:
1165 /* create and send RACH request */
1166 rc = rslms_rx_chan_rqd(lc, msg);
1167 break;
1168 default:
rootaf48bed2011-09-26 11:23:06 +02001169 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001170 msg_type);
1171 msgb_free(msg);
1172 return 0;
1173 }
1174
1175 return rc;
1176}
1177
Harald Welte6bdf0b12011-08-17 18:22:08 +02001178/*! \brief Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001179int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1180{
1181 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1182 int rc = 0;
1183
1184 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001185 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001186 return -EINVAL;
1187 }
1188
1189 switch (rslh->msg_discr & 0xfe) {
1190 case ABIS_RSL_MDISC_RLL:
1191 rc = rslms_rx_rll(msg, lc);
1192 break;
1193 case ABIS_RSL_MDISC_COM_CHAN:
1194 rc = rslms_rx_com_chan(msg, lc);
1195 break;
1196 default:
rootaf48bed2011-09-26 11:23:06 +02001197 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001198 "discriminator 0x%02x", rslh->msg_discr);
1199 msgb_free(msg);
1200 return -EINVAL;
1201 }
1202
1203 return rc;
1204}
1205
Harald Welte6bdf0b12011-08-17 18:22:08 +02001206/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001207int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1208{
rootaf48bed2011-09-26 11:23:06 +02001209 int i;
1210 enum lapd_mode lm;
1211
Harald Welte1f0b8c22011-06-27 10:51:37 +02001212 switch (mode) {
1213 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001214 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001215 break;
1216 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001217 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001218 break;
1219 default:
1220 return -EINVAL;
1221 }
1222
rootaf48bed2011-09-26 11:23:06 +02001223 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1224 lapd_set_mode(&le->datalink[i].dl, lm);
1225 }
1226
Harald Welte1f0b8c22011-06-27 10:51:37 +02001227 le->mode = mode;
1228
1229 return 0;
1230}
1231
Harald Welte6bdf0b12011-08-17 18:22:08 +02001232/*! \brief Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001233int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1234{
1235 int rc;
1236
1237 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1238 if (rc < 0)
1239 return rc;
1240
1241 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1242}
1243
Harald Welte6bdf0b12011-08-17 18:22:08 +02001244/*! \brief Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001245void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1246{
1247 lc->lapdm_dcch.l1_prim_cb = cb;
1248 lc->lapdm_acch.l1_prim_cb = cb;
1249 lc->lapdm_dcch.l1_ctx = ctx;
1250 lc->lapdm_acch.l1_ctx = ctx;
1251}
1252
Harald Welte6bdf0b12011-08-17 18:22:08 +02001253/*! \brief Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001254void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1255{
1256 lc->lapdm_dcch.l3_cb = cb;
1257 lc->lapdm_acch.l3_cb = cb;
1258 lc->lapdm_dcch.l3_ctx = ctx;
1259 lc->lapdm_acch.l3_ctx = ctx;
1260}
1261
Harald Welte6bdf0b12011-08-17 18:22:08 +02001262/*! \brief Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001263void lapdm_entity_reset(struct lapdm_entity *le)
1264{
1265 struct lapdm_datalink *dl;
1266 int i;
1267
1268 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1269 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001270 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001271 }
1272}
1273
Harald Welte6bdf0b12011-08-17 18:22:08 +02001274/*! \brief Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001275void lapdm_channel_reset(struct lapdm_channel *lc)
1276{
1277 lapdm_entity_reset(&lc->lapdm_dcch);
1278 lapdm_entity_reset(&lc->lapdm_acch);
1279}
1280
Harald Welte6bdf0b12011-08-17 18:22:08 +02001281/*! \brief Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001282void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1283{
1284 le->flags = flags;
1285}
1286
Harald Welte6bdf0b12011-08-17 18:22:08 +02001287/*! \brief Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001288void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1289{
1290 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1291 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1292}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001293
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001294/*! @} */