blob: 52290cbe018d417d70a1556fddf3e4e44908e646 [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);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200117
118static void lapdm_dl_init(struct lapdm_datalink *dl,
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100119 struct lapdm_entity *entity, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200120{
121 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200122 dl->entity = entity;
rootaf48bed2011-09-26 11:23:06 +0200123 lapd_dl_init(&dl->dl, 1, 8, 200);
124 dl->dl.reestablish = 0; /* GSM uses no reestablish */
125 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
126 dl->dl.send_dlsap = send_rslms_dlsap;
127 dl->dl.n200_est_rel = N200_EST_REL;
128 dl->dl.n200 = N200;
129 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100130 dl->dl.t200_sec = t200; dl->dl.t200_usec = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200131}
132
Harald Welte6bdf0b12011-08-17 18:22:08 +0200133/*! \brief initialize a LAPDm entity and all datalinks inside
134 * \param[in] le LAPDm entity
135 * \param[in] mode \ref lapdm_mode (BTS/MS)
136 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100137void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200138{
139 unsigned int i;
140
141 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100142 lapdm_dl_init(&le->datalink[i], le, t200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200143
144 lapdm_entity_set_mode(le, mode);
145}
146
Harald Welte6bdf0b12011-08-17 18:22:08 +0200147/*! \brief initialize a LAPDm channel and all its channels
148 * \param[in] lc \ref lapdm_channel to be initialized
149 * \param[in] mode \ref lapdm_mode (BTS/MS)
150 *
151 * This really is a convenience wrapper around calling \ref
152 * lapdm_entity_init twice.
153 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200154void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
155{
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100156 lapdm_entity_init(&lc->lapdm_acch, mode, 2);
rootaf48bed2011-09-26 11:23:06 +0200157 /* FIXME: this depends on chan type */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100158 lapdm_entity_init(&lc->lapdm_dcch, mode, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200159}
160
Harald Welte6bdf0b12011-08-17 18:22:08 +0200161/*! \brief flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200162void lapdm_entity_exit(struct lapdm_entity *le)
163{
164 unsigned int i;
165 struct lapdm_datalink *dl;
166
167 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
168 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200169 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200170 }
171}
172
Harald Welte6bdf0b12011-08-17 18:22:08 +0200173/* \brief lfush and release all resources in LAPDm channel
174 *
175 * A convenience wrapper calling \ref lapdm_entity_exit on both
176 * entities inside the \ref lapdm_channel
177 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200178void lapdm_channel_exit(struct lapdm_channel *lc)
179{
180 lapdm_entity_exit(&lc->lapdm_acch);
181 lapdm_entity_exit(&lc->lapdm_dcch);
182}
183
Harald Welte1f0b8c22011-06-27 10:51:37 +0200184static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
185{
186 switch (sapi) {
187 case LAPDm_SAPI_NORMAL:
188 return &le->datalink[0];
189 case LAPDm_SAPI_SMS:
190 return &le->datalink[1];
191 default:
192 return NULL;
193 }
194}
195
196/* remove the L2 header from a MSGB */
197static inline unsigned char *msgb_pull_l2h(struct msgb *msg)
198{
199 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h);
200 msg->l2h = NULL;
201 return ret;
202}
203
204/* Append padding (if required) */
205static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
206{
207 int pad_len = n201 - msgb_l2len(msg);
208 uint8_t *data;
209
210 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200211 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200212 "cannot pad message that is already too big!\n");
213 return;
214 }
215
216 data = msgb_put(msg, pad_len);
217 memset(data, 0x2B, pad_len);
218}
219
220/* input function that L2 calls when sending messages up to L3 */
221static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
222{
223 if (!le->l3_cb) {
224 msgb_free(msg);
225 return -EIO;
226 }
227
228 /* call the layer2 message handler that is registered */
229 return le->l3_cb(msg, le, le->l3_ctx);
230}
231
232/* write a frame into the tx queue */
233static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200234 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200235{
236 struct lapdm_entity *le = dl->entity;
237 struct osmo_phsap_prim pp;
238
239 /* if there is a pending message, queue it */
240 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200241 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200242 *msgb_push(msg, 1) = link_id;
243 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200244 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200245 return -EBUSY;
246 }
247
248 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
249 PRIM_OP_REQUEST, msg);
250 pp.u.data.chan_nr = chan_nr;
251 pp.u.data.link_id = link_id;
252
253 /* send the frame now */
254 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200255 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200256
257 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
258}
259
260static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
261{
262 struct lapdm_datalink *dl;
263 int last = le->last_tx_dequeue;
264 int i = last, n = ARRAY_SIZE(le->datalink);
265 struct msgb *msg = NULL;
266
267 /* round-robin dequeue */
268 do {
269 /* next */
270 i = (i + 1) % n;
271 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200272 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200273 break;
274 } while (i != last);
275
276 if (msg) {
277 /* Set last dequeue position */
278 le->last_tx_dequeue = i;
279 }
280
281 return msg;
282}
283
Harald Welte6bdf0b12011-08-17 18:22:08 +0200284/*! \brief dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200285 * a osmo_phsap_prim */
286int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
287{
288 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200289 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200290
291 msg = tx_dequeue_msgb(le);
292 if (!msg)
293 return -ENODEV;
294
295 /* if we have a message, send PH-DATA.req */
296 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
297 PRIM_OP_REQUEST, msg);
298
299 /* Pull chan_nr and link_id */
300 pp->u.data.chan_nr = *msg->data;
301 msgb_pull(msg, 1);
302 pp->u.data.link_id = *msg->data;
303 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200304 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200305 msgb_pull(msg, 1);
306
307 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200308 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200309
310 return 0;
311}
312
313/* get next frame from the tx queue. because the ms has multiple datalinks,
314 * each datalink's queue is read round-robin.
315 */
316static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
317{
318 struct osmo_phsap_prim pp;
319
320 /* we may send again */
321 le->tx_pending = 0;
322
323 /* free confirm message */
324 if (msg)
325 msgb_free(msg);
326
327 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
328 /* no message in all queues */
329
330 /* If user didn't request PH-EMPTY_FRAME.req, abort */
331 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
332 return 0;
333
334 /* otherwise, send PH-EMPTY_FRAME.req */
335 osmo_prim_init(&pp.oph, SAP_GSM_PH,
336 PRIM_PH_EMPTY_FRAME,
337 PRIM_OP_REQUEST, NULL);
338 } else {
339 le->tx_pending = 1;
340 }
341
342 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
343}
344
345/* Create RSLms various RSLms messages */
346static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
347 struct msgb *msg)
348{
349 /* Add the RSL + RLL header */
350 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
351
352 /* send off the RSLms message to L3 */
353 return rslms_sendmsg(msg, mctx->dl->entity);
354}
355
356/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
357static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
358{
359 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
360 struct abis_rsl_rll_hdr *rllh;
361
362 /* Add the RSL + RLL header */
363 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
364 msgb_push(msg, 2 + 2);
365 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
366 mctx->link_id, 1);
367 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
368
369 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
370 rllh->data[1] = mctx->ta_ind;
371
372 rllh->data[2] = RSL_IE_MS_POWER;
373 rllh->data[3] = mctx->tx_power_ind;
374
375 return rslms_sendmsg(msg, mctx->dl->entity);
376}
377
378static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
379{
380 struct msgb *msg;
381
382 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
383
384 /* send off the RSLms message to L3 */
385 return rslms_sendmsg(msg, mctx->dl->entity);
386}
387
388static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
389{
390 struct msgb *msg;
391
rootaf48bed2011-09-26 11:23:06 +0200392 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200393 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200394 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
395 return rslms_sendmsg(msg, mctx->dl->entity);
396}
397
rootaf48bed2011-09-26 11:23:06 +0200398/* DLSAP L2 -> L3 (RSLms) */
399static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
400 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200401{
rootaf48bed2011-09-26 11:23:06 +0200402 struct lapd_datalink *dl = lctx->dl;
403 struct lapdm_datalink *mdl =
404 container_of(dl, struct lapdm_datalink, dl);
405 struct lapdm_msg_ctx *mctx = &mdl->mctx;
406 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200407
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200408 switch (OSMO_PRIM_HDR(&dp->oph)) {
409 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
Andreas Eversberg5977db02013-06-12 09:34:51 +0200410 if (dp->oph.msg && dp->oph.msg->len == 0) {
411 /* omit L3 info by freeing message */
412 msgb_free(dp->oph.msg);
413 dp->oph.msg = NULL;
414 }
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200415 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200416 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200417 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
418 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200419 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200420 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
421 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200422 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200423 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
424 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
425 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
426 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200427 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200428 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
429 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200430 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200431 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
432 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200433 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200434 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
435 rsl_rll_error(dp->u.error_ind.cause, mctx);
436 if (dp->oph.msg)
437 msgb_free(dp->oph.msg);
438 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200439 }
rootaf48bed2011-09-26 11:23:06 +0200440
441 if (!rll_msg) {
442 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
443 "fix!\n", dp->oph.primitive, dp->oph.operation);
444 return -EINVAL;
445 }
446
447 if (!dp->oph.msg)
448 return send_rll_simple(rll_msg, mctx);
449
450 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200451}
452
rootaf48bed2011-09-26 11:23:06 +0200453/* send a data frame to layer 1 */
454static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200455{
rootaf48bed2011-09-26 11:23:06 +0200456 uint8_t l3_len = msg->tail - msg->data;
457 struct lapd_datalink *dl = lctx->dl;
458 struct lapdm_datalink *mdl =
459 container_of(dl, struct lapdm_datalink, dl);
460 struct lapdm_msg_ctx *mctx = &mdl->mctx;
461 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200462
rootaf48bed2011-09-26 11:23:06 +0200463 /* prepend l2 header */
464 msg->l2h = msgb_push(msg, 3);
465 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
466 /* EA is set here too */
467 switch (format) {
468 case LAPD_FORM_I:
469 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
470 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200471 break;
rootaf48bed2011-09-26 11:23:06 +0200472 case LAPD_FORM_S:
473 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200474 break;
rootaf48bed2011-09-26 11:23:06 +0200475 case LAPD_FORM_U:
476 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200477 break;
478 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200479 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200480 return -EINVAL;
481 }
rootaf48bed2011-09-26 11:23:06 +0200482 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
483 if (lctx->more)
484 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200485
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100486 /* add ACCH header with last indicated tx-power and TA */
487 if ((mctx->link_id & 0x40)) {
488 struct lapdm_entity *le = mdl->entity;
489
490 msg->l2h = msgb_push(msg, 2);
491 msg->l2h[0] = le->tx_power;
492 msg->l2h[1] = le->ta;
493 }
494
rootaf48bed2011-09-26 11:23:06 +0200495 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
496 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200497}
498
499/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200500static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
501 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200502{
503 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200504 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200505 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200506 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200507 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200508 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200509
510 /* when we reach here, we have a msgb with l2h pointing to the raw
511 * 23byte mac block. The l1h has already been purged. */
512
rootaf48bed2011-09-26 11:23:06 +0200513 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200514 mctx.chan_nr = chan_nr;
515 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200516
Harald Welte1f0b8c22011-06-27 10:51:37 +0200517 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
518 if (cbits == 0x10 || cbits == 0x12) {
519 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
520 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200521 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200522 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200523 } else {
524 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200525 /* It was received from network on SACCH */
526
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100527 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
528 if (le->mode == LAPDM_MODE_MS
529 && LAPDm_CTRL_is_U(msg->l2h[3])
530 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200531 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200532 n201 = N201_B4;
533 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200534 } else {
535 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200536 n201 = N201_AB_SACCH;
537 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200538 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200539 /* SACCH frames have a two-byte L1 header that
540 * OsmocomBB L1 doesn't strip */
541 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
542 mctx.ta_ind = msg->l2h[1];
543 msgb_pull(msg, 2);
544 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200545 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200546 } else {
547 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200548 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100549 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200550 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200551 }
552 }
553
Harald Welte64207742011-06-27 23:32:14 +0200554 mctx.dl = datalink_for_sapi(le, sapi);
555 /* G.2.1 No action on frames containing an unallocated SAPI. */
556 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200557 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200558 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200559 msgb_free(msg);
560 return -EIO;
561 }
562
Harald Welte1f0b8c22011-06-27 10:51:37 +0200563 switch (mctx.lapdm_fmt) {
564 case LAPDm_FMT_A:
565 case LAPDm_FMT_B:
566 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200567 lctx.dl = &mctx.dl->dl;
568 /* obtain SAPI from address field */
569 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
570 /* G.2.3 EA bit set to "0" is not allowed in GSM */
571 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
572 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
573 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200574 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200575 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200576 return -EINVAL;
577 }
rootaf48bed2011-09-26 11:23:06 +0200578 /* adress field */
579 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
580 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
581 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
582 /* command field */
583 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
584 lctx.format = LAPD_FORM_I;
585 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
586 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
587 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
588 lctx.format = LAPD_FORM_S;
589 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
590 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
591 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
592 lctx.format = LAPD_FORM_U;
593 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
594 } else
595 lctx.format = LAPD_FORM_UKN;
596 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
597 if (lctx.sapi != LAPDm_SAPI_NORMAL
598 && lctx.sapi != LAPDm_SAPI_SMS
599 && lctx.format == LAPD_FORM_U
600 && lctx.s_u == LAPDm_U_UI) {
601 /* 5.3.3 UI frames with invalid SAPI values shall be
602 * discarded
603 */
604 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
605 lctx.sapi);
606 msgb_free(msg);
607 return 0;
608 }
609 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
610 lctx.n201 = n201;
611 lctx.length = n201;
612 lctx.more = 0;
613 msg->l3h = msg->l2h + 2;
614 msgb_pull_l2h(msg);
615 } else {
616 /* length field */
617 if (!(msg->l2h[2] & LAPDm_EL)) {
618 /* G.4.1 If the EL bit is set to "0", an
619 * MDL-ERROR-INDICATION primitive with cause
620 * "frame not implemented" is sent to the
621 * mobile management entity. */
622 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
623 "multi-octet length\n");
624 msgb_free(msg);
625 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
626 return -EINVAL;
627 }
628 lctx.n201 = n201;
629 lctx.length = msg->l2h[2] >> 2;
630 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
631 msg->l3h = msg->l2h + 3;
632 msgb_pull_l2h(msg);
633 }
634 /* store context for messages from lapd */
635 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
636 /* send to LAPD */
637 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200638 break;
639 case LAPDm_FMT_Bter:
640 /* FIXME */
641 msgb_free(msg);
642 break;
643 case LAPDm_FMT_Bbis:
644 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200645 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200646 msg->l3h = msg->l2h;
647 msgb_pull_l2h(msg);
648 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
649 break;
650 default:
651 msgb_free(msg);
652 }
653
654 return rc;
655}
656
657/* input into layer2 (from layer 1) */
658static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
659{
660 struct abis_rsl_cchan_hdr *ch;
661 struct gsm48_req_ref req_ref;
662 struct gsm_time gt;
663 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
664
665 msg->l2h = msgb_push(msg, sizeof(*ch));
666 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
667 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
668 ch->chan_nr = RSL_CHAN_RACH;
669
670 /* generate a RSL CHANNEL REQUIRED message */
671 gsm_fn2gsmtime(&gt, fn);
672 req_ref.ra = ra;
673 req_ref.t1 = gt.t1; /* FIXME: modulo? */
674 req_ref.t2 = gt.t2;
675 req_ref.t3_low = gt.t3 & 7;
676 req_ref.t3_high = gt.t3 >> 3;
677
678 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
679 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
680
681 return rslms_sendmsg(msg, le);
682}
683
684static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
685
Harald Welte6bdf0b12011-08-17 18:22:08 +0200686/*! \brief Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200687int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
688{
689 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
690 int rc = 0;
691
692 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200693 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200694 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100695 rc = -ENODEV;
696 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200697 }
698
699 switch (oph->primitive) {
700 case PRIM_PH_DATA:
701 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200702 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200703 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100704 rc = -ENODEV;
705 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200706 }
707 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
708 pp->u.data.link_id);
709 break;
710 case PRIM_PH_RTS:
711 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200712 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200713 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100714 rc = -ENODEV;
715 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200716 }
717 rc = l2_ph_data_conf(oph->msg, le);
718 break;
719 case PRIM_PH_RACH:
720 switch (oph->operation) {
721 case PRIM_OP_INDICATION:
722 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
723 pp->u.rach_ind.acc_delay);
724 break;
725 case PRIM_OP_CONFIRM:
726 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
727 break;
728 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100729 rc = -EIO;
730 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200731 }
732 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100733 default:
734 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
735 oph->primitive);
736 rc = -EINVAL;
737 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200738 }
739
740 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100741
742free:
743 msgb_free(oph->msg);
744 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200745}
746
747
748/* L3 -> L2 / RSLMS -> LAPDm */
749
rootaf48bed2011-09-26 11:23:06 +0200750/* Set LAPDm context for established connection */
751static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
752 uint8_t link_id, int n201, uint8_t sapi)
753{
754 memset(&dl->mctx, 0, sizeof(dl->mctx));
755 dl->mctx.dl = dl;
756 dl->mctx.chan_nr = chan_nr;
757 dl->mctx.link_id = link_id;
758 dl->dl.lctx.dl = &dl->dl;
759 dl->dl.lctx.n201 = n201;
760 dl->dl.lctx.sapi = sapi;
761
762 return 0;
763}
764
Harald Welte1f0b8c22011-06-27 10:51:37 +0200765/* L3 requests establishment of data link */
766static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
767{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200768 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
769 uint8_t chan_nr = rllh->chan_nr;
770 uint8_t link_id = rllh->link_id;
771 uint8_t sapi = rllh->link_id & 7;
772 struct tlv_parsed tv;
773 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100774 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200775 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200776
rootaf48bed2011-09-26 11:23:06 +0200777 /* Set LAPDm context for established connection */
778 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200779
rootaf48bed2011-09-26 11:23:06 +0200780 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200781 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200782 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200783 /* contention resolution establishment procedure */
784 if (sapi != 0) {
785 /* According to clause 6, the contention resolution
786 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200787 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200788 "resolution (discarding)\n");
789 msgb_free(msg);
790 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
791 }
792 /* transmit a SABM command with the P bit set to "1". The SABM
793 * command shall contain the layer 3 message unit */
794 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200795 } else {
796 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200797 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200798 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200799 }
800
801 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100802 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200803 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100804 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200805 msgb_free(msg);
806 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
807 }
808
rootaf48bed2011-09-26 11:23:06 +0200809 /* Remove RLL header from msgb and set length to L3-info */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200810 msgb_pull_l2h(msg);
rootaf48bed2011-09-26 11:23:06 +0200811 msg->len = length;
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100812 msg->tail = msg->l3h + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200813
rootaf48bed2011-09-26 11:23:06 +0200814 /* prepare prim */
815 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200816
rootaf48bed2011-09-26 11:23:06 +0200817 /* send to L2 */
818 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200819}
820
821/* L3 requests transfer of unnumbered information */
822static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
823{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100824 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200825 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
826 uint8_t chan_nr = rllh->chan_nr;
827 uint8_t link_id = rllh->link_id;
Andreas Eversberg5977db02013-06-12 09:34:51 +0200828 int ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200829 uint8_t sapi = link_id & 7;
830 struct tlv_parsed tv;
831 int length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200832
833 /* check if the layer3 message length exceeds N201 */
834
835 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
836
837 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100838 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200839 }
840 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100841 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200842 }
843 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200844 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200845 "error\n");
846 msgb_free(msg);
847 return -EINVAL;
848 }
rootaf48bed2011-09-26 11:23:06 +0200849 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200850 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
851 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200852 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200853 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200854 "(discarding)\n", length,
855 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200856 msgb_free(msg);
857 return -EIO;
858 }
859
rootaf48bed2011-09-26 11:23:06 +0200860 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100861 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200862
rootaf48bed2011-09-26 11:23:06 +0200863 /* Remove RLL header from msgb and set length to L3-info */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200864 msgb_pull_l2h(msg);
rootaf48bed2011-09-26 11:23:06 +0200865 msg->len = length;
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100866 msg->tail = msg->l3h + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200867
868 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200869 msg->l2h = msgb_push(msg, 2 + !ui_bts);
870 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
871 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100872 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200873 msg->l2h[2] = LAPDm_LEN(length);
874 if (link_id & 0x40) {
875 msg->l2h = msgb_push(msg, 2);
876 msg->l2h[0] = le->tx_power;
877 msg->l2h[1] = le->ta;
878 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200879
880 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200881 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200882}
883
884/* L3 requests transfer of acknowledged information */
885static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
886{
887 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
888 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200889 int length;
890 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200891
892 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
893 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200894 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200895 "error\n");
896 msgb_free(msg);
897 return -EINVAL;
898 }
rootaf48bed2011-09-26 11:23:06 +0200899 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
900 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200901
rootaf48bed2011-09-26 11:23:06 +0200902 /* Remove RLL header from msgb and set length to L3-info */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200903 msgb_pull_l2h(msg);
rootaf48bed2011-09-26 11:23:06 +0200904 msg->len = length;
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100905 msg->tail = msg->l3h + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200906
rootaf48bed2011-09-26 11:23:06 +0200907 /* prepare prim */
908 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200909
rootaf48bed2011-09-26 11:23:06 +0200910 /* send to L2 */
911 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200912}
913
914/* L3 requests suspension of data link */
915static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
916{
917 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
918 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200919 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200920
921 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200922 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200923 msgb_free(msg);
924 return -EINVAL;
925 }
926
rootaf48bed2011-09-26 11:23:06 +0200927 /* prepare prim */
928 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200929
rootaf48bed2011-09-26 11:23:06 +0200930 /* send to L2 */
931 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200932}
933
934/* L3 requests resume of data link */
935static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
936{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200937 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200938 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200939 uint8_t chan_nr = rllh->chan_nr;
940 uint8_t link_id = rllh->link_id;
941 uint8_t sapi = rllh->link_id & 7;
942 struct tlv_parsed tv;
943 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100944 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200945 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200946
rootaf48bed2011-09-26 11:23:06 +0200947 /* Set LAPDm context for established connection */
948 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200949
950 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
951 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200952 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200953 msgb_free(msg);
954 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
955 }
rootaf48bed2011-09-26 11:23:06 +0200956 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200957 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
958
rootaf48bed2011-09-26 11:23:06 +0200959 /* Remove RLL header from msgb and set length to L3-info */
960 msgb_pull_l2h(msg);
961 msg->len = length;
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100962 msg->tail = msg->l3h + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200963
rootaf48bed2011-09-26 11:23:06 +0200964 /* prepare prim */
965 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
966 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200967
rootaf48bed2011-09-26 11:23:06 +0200968 /* send to L2 */
969 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200970}
971
972/* L3 requests release of data link */
973static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
974{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200975 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200976 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +0200977 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200978
979 /* get release mode */
980 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
981 mode = rllh->data[1] & 1;
982
Harald Welte1f0b8c22011-06-27 10:51:37 +0200983 /* Pull rllh */
Harald Welte973c3c32012-04-26 21:50:54 +0200984 msgb_pull_l2h(msg);
985
986 /* 04.06 3.8.3: No information field is permitted with the DISC
987 * command. */
988 msg->len = 0;
989 msg->tail = msg->l3h = msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200990
rootaf48bed2011-09-26 11:23:06 +0200991 /* prepare prim */
992 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
993 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200994
rootaf48bed2011-09-26 11:23:06 +0200995 /* send to L2 */
996 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200997}
998
999/* L3 requests channel in idle state */
1000static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1001{
1002 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1003 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1004 struct osmo_phsap_prim pp;
1005
1006 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1007 PRIM_OP_REQUEST, NULL);
1008
1009 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001010 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001011 return -EINVAL;
1012 }
1013 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001014 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001015 return -EINVAL;
1016 }
1017 pp.u.rach_req.ra = cch->data[1];
1018 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1019 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1020
1021 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001022 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001023 return -EINVAL;
1024 }
1025 /* TA = 0 - delay */
1026 pp.u.rach_req.ta = 0 - cch->data[5];
1027
1028 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001029 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001030 return -EINVAL;
1031 }
1032 pp.u.rach_req.tx_power = cch->data[7];
1033
1034 msgb_free(msg);
1035
1036 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1037}
1038
1039/* L1 confirms channel request */
1040static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1041{
1042 struct abis_rsl_cchan_hdr *ch;
1043 struct gsm_time tm;
1044 struct gsm48_req_ref *ref;
1045
1046 gsm_fn2gsmtime(&tm, frame_nr);
1047
1048 msgb_pull_l2h(msg);
1049 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1050 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1051 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1052 ch->chan_nr = RSL_CHAN_RACH;
1053 ch->data[0] = RSL_IE_REQ_REFERENCE;
1054 ref = (struct gsm48_req_ref *) (ch->data + 1);
1055 ref->t1 = tm.t1;
1056 ref->t2 = tm.t2;
1057 ref->t3_low = tm.t3 & 0x7;
1058 ref->t3_high = tm.t3 >> 3;
1059
1060 return rslms_sendmsg(msg, le);
1061}
1062
Harald Welte1f0b8c22011-06-27 10:51:37 +02001063/* incoming RSLms RLL message from L3 */
1064static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1065{
1066 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1067 int msg_type = rllh->c.msg_type;
1068 uint8_t sapi = rllh->link_id & 7;
1069 struct lapdm_entity *le;
1070 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001071 int rc = 0;
1072
1073 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001074 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001075 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001076 return -EINVAL;
1077 }
1078
1079 if (rllh->link_id & 0x40)
1080 le = &lc->lapdm_acch;
1081 else
1082 le = &lc->lapdm_dcch;
1083
1084 /* G.2.1 No action schall be taken on frames containing an unallocated
1085 * SAPI.
1086 */
1087 dl = datalink_for_sapi(le, sapi);
1088 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001089 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001090 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001091 return -EINVAL;
1092 }
1093
Daniel Willmanne5233922012-12-25 23:15:50 +01001094 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001095 case RSL_MT_DATA_REQ:
1096 case RSL_MT_SUSP_REQ:
1097 case RSL_MT_REL_REQ:
1098 /* This is triggered in abnormal error conditions where
1099 * set_lapdm_context() was not called for the channel earlier. */
1100 if (!dl->dl.lctx.dl) {
1101 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1102 lc->name, rsl_msg_name(msg_type), sapi);
1103 msgb_free(msg);
1104 return -EINVAL;
1105 }
1106 break;
1107 default:
1108 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1109 lc->name, rsl_msg_name(msg_type), sapi);
1110 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001111
rootaf48bed2011-09-26 11:23:06 +02001112 switch (msg_type) {
1113 case RSL_MT_UNIT_DATA_REQ:
1114 rc = rslms_rx_rll_udata_req(msg, dl);
1115 break;
1116 case RSL_MT_EST_REQ:
1117 rc = rslms_rx_rll_est_req(msg, dl);
1118 break;
1119 case RSL_MT_DATA_REQ:
1120 rc = rslms_rx_rll_data_req(msg, dl);
1121 break;
1122 case RSL_MT_SUSP_REQ:
1123 rc = rslms_rx_rll_susp_req(msg, dl);
1124 break;
1125 case RSL_MT_RES_REQ:
1126 rc = rslms_rx_rll_res_req(msg, dl);
1127 break;
1128 case RSL_MT_RECON_REQ:
1129 rc = rslms_rx_rll_res_req(msg, dl);
1130 break;
1131 case RSL_MT_REL_REQ:
1132 rc = rslms_rx_rll_rel_req(msg, dl);
1133 break;
1134 default:
1135 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001136 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001137 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001138 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001139
1140 return rc;
1141}
1142
1143/* incoming RSLms COMMON CHANNEL message from L3 */
1144static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1145{
1146 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1147 int msg_type = cch->c.msg_type;
1148 int rc = 0;
1149
1150 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001151 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001152 return -EINVAL;
1153 }
1154
1155 switch (msg_type) {
1156 case RSL_MT_CHAN_RQD:
1157 /* create and send RACH request */
1158 rc = rslms_rx_chan_rqd(lc, msg);
1159 break;
1160 default:
rootaf48bed2011-09-26 11:23:06 +02001161 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001162 msg_type);
1163 msgb_free(msg);
1164 return 0;
1165 }
1166
1167 return rc;
1168}
1169
Harald Welte6bdf0b12011-08-17 18:22:08 +02001170/*! \brief Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001171int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1172{
1173 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1174 int rc = 0;
1175
1176 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001177 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001178 return -EINVAL;
1179 }
1180
1181 switch (rslh->msg_discr & 0xfe) {
1182 case ABIS_RSL_MDISC_RLL:
1183 rc = rslms_rx_rll(msg, lc);
1184 break;
1185 case ABIS_RSL_MDISC_COM_CHAN:
1186 rc = rslms_rx_com_chan(msg, lc);
1187 break;
1188 default:
rootaf48bed2011-09-26 11:23:06 +02001189 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001190 "discriminator 0x%02x", rslh->msg_discr);
1191 msgb_free(msg);
1192 return -EINVAL;
1193 }
1194
1195 return rc;
1196}
1197
Harald Welte6bdf0b12011-08-17 18:22:08 +02001198/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001199int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1200{
rootaf48bed2011-09-26 11:23:06 +02001201 int i;
1202 enum lapd_mode lm;
1203
Harald Welte1f0b8c22011-06-27 10:51:37 +02001204 switch (mode) {
1205 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001206 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001207 break;
1208 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001209 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001210 break;
1211 default:
1212 return -EINVAL;
1213 }
1214
rootaf48bed2011-09-26 11:23:06 +02001215 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1216 lapd_set_mode(&le->datalink[i].dl, lm);
1217 }
1218
Harald Welte1f0b8c22011-06-27 10:51:37 +02001219 le->mode = mode;
1220
1221 return 0;
1222}
1223
Harald Welte6bdf0b12011-08-17 18:22:08 +02001224/*! \brief Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001225int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1226{
1227 int rc;
1228
1229 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1230 if (rc < 0)
1231 return rc;
1232
1233 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1234}
1235
Harald Welte6bdf0b12011-08-17 18:22:08 +02001236/*! \brief Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001237void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1238{
1239 lc->lapdm_dcch.l1_prim_cb = cb;
1240 lc->lapdm_acch.l1_prim_cb = cb;
1241 lc->lapdm_dcch.l1_ctx = ctx;
1242 lc->lapdm_acch.l1_ctx = ctx;
1243}
1244
Harald Welte6bdf0b12011-08-17 18:22:08 +02001245/*! \brief Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001246void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1247{
1248 lc->lapdm_dcch.l3_cb = cb;
1249 lc->lapdm_acch.l3_cb = cb;
1250 lc->lapdm_dcch.l3_ctx = ctx;
1251 lc->lapdm_acch.l3_ctx = ctx;
1252}
1253
Harald Welte6bdf0b12011-08-17 18:22:08 +02001254/*! \brief Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001255void lapdm_entity_reset(struct lapdm_entity *le)
1256{
1257 struct lapdm_datalink *dl;
1258 int i;
1259
1260 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1261 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001262 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001263 }
1264}
1265
Harald Welte6bdf0b12011-08-17 18:22:08 +02001266/*! \brief Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001267void lapdm_channel_reset(struct lapdm_channel *lc)
1268{
1269 lapdm_entity_reset(&lc->lapdm_dcch);
1270 lapdm_entity_reset(&lc->lapdm_acch);
1271}
1272
Harald Welte6bdf0b12011-08-17 18:22:08 +02001273/*! \brief Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001274void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1275{
1276 le->flags = flags;
1277}
1278
Harald Welte6bdf0b12011-08-17 18:22:08 +02001279/*! \brief Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001280void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1281{
1282 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1283 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1284}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001285
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001286/*! @} */