blob: 71045aa16a77b839ac17bc52fbe53b0ba87d25f0 [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):
410 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200411 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200412 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
413 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200414 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200415 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
416 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200417 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200418 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
419 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
420 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
421 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200422 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200423 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
424 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200425 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200426 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
427 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200428 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200429 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
430 rsl_rll_error(dp->u.error_ind.cause, mctx);
431 if (dp->oph.msg)
432 msgb_free(dp->oph.msg);
433 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200434 }
rootaf48bed2011-09-26 11:23:06 +0200435
436 if (!rll_msg) {
437 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
438 "fix!\n", dp->oph.primitive, dp->oph.operation);
439 return -EINVAL;
440 }
441
442 if (!dp->oph.msg)
443 return send_rll_simple(rll_msg, mctx);
444
445 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200446}
447
rootaf48bed2011-09-26 11:23:06 +0200448/* send a data frame to layer 1 */
449static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200450{
rootaf48bed2011-09-26 11:23:06 +0200451 uint8_t l3_len = msg->tail - msg->data;
452 struct lapd_datalink *dl = lctx->dl;
453 struct lapdm_datalink *mdl =
454 container_of(dl, struct lapdm_datalink, dl);
455 struct lapdm_msg_ctx *mctx = &mdl->mctx;
456 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200457
rootaf48bed2011-09-26 11:23:06 +0200458 /* prepend l2 header */
459 msg->l2h = msgb_push(msg, 3);
460 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
461 /* EA is set here too */
462 switch (format) {
463 case LAPD_FORM_I:
464 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
465 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200466 break;
rootaf48bed2011-09-26 11:23:06 +0200467 case LAPD_FORM_S:
468 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200469 break;
rootaf48bed2011-09-26 11:23:06 +0200470 case LAPD_FORM_U:
471 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200472 break;
473 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200474 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200475 return -EINVAL;
476 }
rootaf48bed2011-09-26 11:23:06 +0200477 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
478 if (lctx->more)
479 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200480
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100481 /* add ACCH header with last indicated tx-power and TA */
482 if ((mctx->link_id & 0x40)) {
483 struct lapdm_entity *le = mdl->entity;
484
485 msg->l2h = msgb_push(msg, 2);
486 msg->l2h[0] = le->tx_power;
487 msg->l2h[1] = le->ta;
488 }
489
rootaf48bed2011-09-26 11:23:06 +0200490 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
491 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200492}
493
494/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200495static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
496 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200497{
498 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200499 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200500 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200501 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200502 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200503 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200504
505 /* when we reach here, we have a msgb with l2h pointing to the raw
506 * 23byte mac block. The l1h has already been purged. */
507
rootaf48bed2011-09-26 11:23:06 +0200508 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200509 mctx.chan_nr = chan_nr;
510 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200511
Harald Welte1f0b8c22011-06-27 10:51:37 +0200512 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
513 if (cbits == 0x10 || cbits == 0x12) {
514 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
515 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200516 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200517 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200518 } else {
519 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200520 /* It was received from network on SACCH */
521
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100522 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
523 if (le->mode == LAPDM_MODE_MS
524 && LAPDm_CTRL_is_U(msg->l2h[3])
525 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200526 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200527 n201 = N201_B4;
528 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200529 } else {
530 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200531 n201 = N201_AB_SACCH;
532 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200533 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200534 /* SACCH frames have a two-byte L1 header that
535 * OsmocomBB L1 doesn't strip */
536 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
537 mctx.ta_ind = msg->l2h[1];
538 msgb_pull(msg, 2);
539 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200540 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200541 } else {
542 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200543 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100544 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200545 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200546 }
547 }
548
Harald Welte64207742011-06-27 23:32:14 +0200549 mctx.dl = datalink_for_sapi(le, sapi);
550 /* G.2.1 No action on frames containing an unallocated SAPI. */
551 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200552 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200553 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200554 msgb_free(msg);
555 return -EIO;
556 }
557
Harald Welte1f0b8c22011-06-27 10:51:37 +0200558 switch (mctx.lapdm_fmt) {
559 case LAPDm_FMT_A:
560 case LAPDm_FMT_B:
561 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200562 lctx.dl = &mctx.dl->dl;
563 /* obtain SAPI from address field */
564 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
565 /* G.2.3 EA bit set to "0" is not allowed in GSM */
566 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
567 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
568 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200569 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200570 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200571 return -EINVAL;
572 }
rootaf48bed2011-09-26 11:23:06 +0200573 /* adress field */
574 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
575 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
576 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
577 /* command field */
578 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
579 lctx.format = LAPD_FORM_I;
580 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
581 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
582 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
583 lctx.format = LAPD_FORM_S;
584 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
585 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
586 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
587 lctx.format = LAPD_FORM_U;
588 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
589 } else
590 lctx.format = LAPD_FORM_UKN;
591 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
592 if (lctx.sapi != LAPDm_SAPI_NORMAL
593 && lctx.sapi != LAPDm_SAPI_SMS
594 && lctx.format == LAPD_FORM_U
595 && lctx.s_u == LAPDm_U_UI) {
596 /* 5.3.3 UI frames with invalid SAPI values shall be
597 * discarded
598 */
599 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
600 lctx.sapi);
601 msgb_free(msg);
602 return 0;
603 }
604 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
605 lctx.n201 = n201;
606 lctx.length = n201;
607 lctx.more = 0;
608 msg->l3h = msg->l2h + 2;
609 msgb_pull_l2h(msg);
610 } else {
611 /* length field */
612 if (!(msg->l2h[2] & LAPDm_EL)) {
613 /* G.4.1 If the EL bit is set to "0", an
614 * MDL-ERROR-INDICATION primitive with cause
615 * "frame not implemented" is sent to the
616 * mobile management entity. */
617 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
618 "multi-octet length\n");
619 msgb_free(msg);
620 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
621 return -EINVAL;
622 }
623 lctx.n201 = n201;
624 lctx.length = msg->l2h[2] >> 2;
625 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
626 msg->l3h = msg->l2h + 3;
627 msgb_pull_l2h(msg);
628 }
629 /* store context for messages from lapd */
630 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
631 /* send to LAPD */
632 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200633 break;
634 case LAPDm_FMT_Bter:
635 /* FIXME */
636 msgb_free(msg);
637 break;
638 case LAPDm_FMT_Bbis:
639 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200640 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200641 msg->l3h = msg->l2h;
642 msgb_pull_l2h(msg);
643 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
644 break;
645 default:
646 msgb_free(msg);
647 }
648
649 return rc;
650}
651
652/* input into layer2 (from layer 1) */
653static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
654{
655 struct abis_rsl_cchan_hdr *ch;
656 struct gsm48_req_ref req_ref;
657 struct gsm_time gt;
658 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
659
660 msg->l2h = msgb_push(msg, sizeof(*ch));
661 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
662 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
663 ch->chan_nr = RSL_CHAN_RACH;
664
665 /* generate a RSL CHANNEL REQUIRED message */
666 gsm_fn2gsmtime(&gt, fn);
667 req_ref.ra = ra;
668 req_ref.t1 = gt.t1; /* FIXME: modulo? */
669 req_ref.t2 = gt.t2;
670 req_ref.t3_low = gt.t3 & 7;
671 req_ref.t3_high = gt.t3 >> 3;
672
673 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
674 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
675
676 return rslms_sendmsg(msg, le);
677}
678
679static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
680
Harald Welte6bdf0b12011-08-17 18:22:08 +0200681/*! \brief Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200682int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
683{
684 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
685 int rc = 0;
686
687 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200688 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200689 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100690 rc = -ENODEV;
691 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200692 }
693
694 switch (oph->primitive) {
695 case PRIM_PH_DATA:
696 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200697 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200698 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100699 rc = -ENODEV;
700 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200701 }
702 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
703 pp->u.data.link_id);
704 break;
705 case PRIM_PH_RTS:
706 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200707 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200708 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100709 rc = -ENODEV;
710 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200711 }
712 rc = l2_ph_data_conf(oph->msg, le);
713 break;
714 case PRIM_PH_RACH:
715 switch (oph->operation) {
716 case PRIM_OP_INDICATION:
717 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
718 pp->u.rach_ind.acc_delay);
719 break;
720 case PRIM_OP_CONFIRM:
721 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
722 break;
723 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100724 rc = -EIO;
725 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200726 }
727 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100728 default:
729 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
730 oph->primitive);
731 rc = -EINVAL;
732 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200733 }
734
735 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100736
737free:
738 msgb_free(oph->msg);
739 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200740}
741
742
743/* L3 -> L2 / RSLMS -> LAPDm */
744
rootaf48bed2011-09-26 11:23:06 +0200745/* Set LAPDm context for established connection */
746static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
747 uint8_t link_id, int n201, uint8_t sapi)
748{
749 memset(&dl->mctx, 0, sizeof(dl->mctx));
750 dl->mctx.dl = dl;
751 dl->mctx.chan_nr = chan_nr;
752 dl->mctx.link_id = link_id;
753 dl->dl.lctx.dl = &dl->dl;
754 dl->dl.lctx.n201 = n201;
755 dl->dl.lctx.sapi = sapi;
756
757 return 0;
758}
759
Harald Welte1f0b8c22011-06-27 10:51:37 +0200760/* L3 requests establishment of data link */
761static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
762{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200763 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
764 uint8_t chan_nr = rllh->chan_nr;
765 uint8_t link_id = rllh->link_id;
766 uint8_t sapi = rllh->link_id & 7;
767 struct tlv_parsed tv;
768 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100769 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200770 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200771
rootaf48bed2011-09-26 11:23:06 +0200772 /* Set LAPDm context for established connection */
773 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200774
rootaf48bed2011-09-26 11:23:06 +0200775 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200776 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200777 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200778 /* contention resolution establishment procedure */
779 if (sapi != 0) {
780 /* According to clause 6, the contention resolution
781 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200782 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200783 "resolution (discarding)\n");
784 msgb_free(msg);
785 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
786 }
787 /* transmit a SABM command with the P bit set to "1". The SABM
788 * command shall contain the layer 3 message unit */
789 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200790 } else {
791 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200792 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200793 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200794 }
795
796 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100797 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200798 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100799 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200800 msgb_free(msg);
801 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
802 }
803
rootaf48bed2011-09-26 11:23:06 +0200804 /* Remove RLL header from msgb and set length to L3-info */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200805 msgb_pull_l2h(msg);
rootaf48bed2011-09-26 11:23:06 +0200806 msg->len = length;
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100807 msg->tail = msg->l3h + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200808
rootaf48bed2011-09-26 11:23:06 +0200809 /* prepare prim */
810 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200811
rootaf48bed2011-09-26 11:23:06 +0200812 /* send to L2 */
813 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200814}
815
816/* L3 requests transfer of unnumbered information */
817static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
818{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100819 struct lapdm_entity *le = dl->entity;
820 int ui_bts = (le->mode == LAPDM_MODE_BTS);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200821 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
822 uint8_t chan_nr = rllh->chan_nr;
823 uint8_t link_id = rllh->link_id;
824 uint8_t sapi = link_id & 7;
825 struct tlv_parsed tv;
826 int length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200827
828 /* check if the layer3 message length exceeds N201 */
829
830 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
831
832 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100833 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200834 }
835 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100836 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200837 }
838 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200839 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200840 "error\n");
841 msgb_free(msg);
842 return -EINVAL;
843 }
rootaf48bed2011-09-26 11:23:06 +0200844 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200845 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
846 /* check if the layer3 message length exceeds N201 */
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100847 if (length + 4 + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200848 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100849 "(discarding)\n", length, 18 + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200850 msgb_free(msg);
851 return -EIO;
852 }
853
rootaf48bed2011-09-26 11:23:06 +0200854 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100855 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200856
rootaf48bed2011-09-26 11:23:06 +0200857 /* Remove RLL header from msgb and set length to L3-info */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200858 msgb_pull_l2h(msg);
rootaf48bed2011-09-26 11:23:06 +0200859 msg->len = length;
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100860 msg->tail = msg->l3h + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200861
862 /* Push L1 + LAPDm header on msgb */
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100863 msg->l2h = msgb_push(msg, 4 + !ui_bts);
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100864 msg->l2h[0] = le->tx_power;
865 msg->l2h[1] = le->ta;
rootaf48bed2011-09-26 11:23:06 +0200866 msg->l2h[2] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200867 msg->l2h[3] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100868 if (!ui_bts)
869 msg->l2h[4] = LAPDm_LEN(length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200870
871 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200872 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200873}
874
875/* L3 requests transfer of acknowledged information */
876static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
877{
878 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
879 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200880 int length;
881 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200882
883 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
884 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200885 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200886 "error\n");
887 msgb_free(msg);
888 return -EINVAL;
889 }
rootaf48bed2011-09-26 11:23:06 +0200890 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
891 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200892
rootaf48bed2011-09-26 11:23:06 +0200893 /* Remove RLL header from msgb and set length to L3-info */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200894 msgb_pull_l2h(msg);
rootaf48bed2011-09-26 11:23:06 +0200895 msg->len = length;
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100896 msg->tail = msg->l3h + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200897
rootaf48bed2011-09-26 11:23:06 +0200898 /* prepare prim */
899 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200900
rootaf48bed2011-09-26 11:23:06 +0200901 /* send to L2 */
902 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200903}
904
905/* L3 requests suspension of data link */
906static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
907{
908 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
909 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200910 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200911
912 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200913 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200914 msgb_free(msg);
915 return -EINVAL;
916 }
917
rootaf48bed2011-09-26 11:23:06 +0200918 /* prepare prim */
919 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200920
rootaf48bed2011-09-26 11:23:06 +0200921 /* send to L2 */
922 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200923}
924
925/* L3 requests resume of data link */
926static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
927{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200928 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200929 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200930 uint8_t chan_nr = rllh->chan_nr;
931 uint8_t link_id = rllh->link_id;
932 uint8_t sapi = rllh->link_id & 7;
933 struct tlv_parsed tv;
934 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100935 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200936 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200937
rootaf48bed2011-09-26 11:23:06 +0200938 /* Set LAPDm context for established connection */
939 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200940
941 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
942 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200943 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200944 msgb_free(msg);
945 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
946 }
rootaf48bed2011-09-26 11:23:06 +0200947 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200948 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
949
rootaf48bed2011-09-26 11:23:06 +0200950 /* Remove RLL header from msgb and set length to L3-info */
951 msgb_pull_l2h(msg);
952 msg->len = length;
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100953 msg->tail = msg->l3h + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200954
rootaf48bed2011-09-26 11:23:06 +0200955 /* prepare prim */
956 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
957 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200958
rootaf48bed2011-09-26 11:23:06 +0200959 /* send to L2 */
960 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200961}
962
963/* L3 requests release of data link */
964static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
965{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200966 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200967 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +0200968 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200969
970 /* get release mode */
971 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
972 mode = rllh->data[1] & 1;
973
Harald Welte1f0b8c22011-06-27 10:51:37 +0200974 /* Pull rllh */
Harald Welte973c3c32012-04-26 21:50:54 +0200975 msgb_pull_l2h(msg);
976
977 /* 04.06 3.8.3: No information field is permitted with the DISC
978 * command. */
979 msg->len = 0;
980 msg->tail = msg->l3h = msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200981
rootaf48bed2011-09-26 11:23:06 +0200982 /* prepare prim */
983 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
984 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200985
rootaf48bed2011-09-26 11:23:06 +0200986 /* send to L2 */
987 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200988}
989
990/* L3 requests channel in idle state */
991static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
992{
993 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
994 void *l1ctx = lc->lapdm_dcch.l1_ctx;
995 struct osmo_phsap_prim pp;
996
997 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
998 PRIM_OP_REQUEST, NULL);
999
1000 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001001 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001002 return -EINVAL;
1003 }
1004 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001005 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001006 return -EINVAL;
1007 }
1008 pp.u.rach_req.ra = cch->data[1];
1009 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1010 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1011
1012 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001013 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001014 return -EINVAL;
1015 }
1016 /* TA = 0 - delay */
1017 pp.u.rach_req.ta = 0 - cch->data[5];
1018
1019 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001020 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001021 return -EINVAL;
1022 }
1023 pp.u.rach_req.tx_power = cch->data[7];
1024
1025 msgb_free(msg);
1026
1027 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1028}
1029
1030/* L1 confirms channel request */
1031static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1032{
1033 struct abis_rsl_cchan_hdr *ch;
1034 struct gsm_time tm;
1035 struct gsm48_req_ref *ref;
1036
1037 gsm_fn2gsmtime(&tm, frame_nr);
1038
1039 msgb_pull_l2h(msg);
1040 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1041 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1042 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1043 ch->chan_nr = RSL_CHAN_RACH;
1044 ch->data[0] = RSL_IE_REQ_REFERENCE;
1045 ref = (struct gsm48_req_ref *) (ch->data + 1);
1046 ref->t1 = tm.t1;
1047 ref->t2 = tm.t2;
1048 ref->t3_low = tm.t3 & 0x7;
1049 ref->t3_high = tm.t3 >> 3;
1050
1051 return rslms_sendmsg(msg, le);
1052}
1053
Harald Welte1f0b8c22011-06-27 10:51:37 +02001054/* incoming RSLms RLL message from L3 */
1055static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1056{
1057 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1058 int msg_type = rllh->c.msg_type;
1059 uint8_t sapi = rllh->link_id & 7;
1060 struct lapdm_entity *le;
1061 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001062 int rc = 0;
1063
1064 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001065 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001066 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001067 return -EINVAL;
1068 }
1069
1070 if (rllh->link_id & 0x40)
1071 le = &lc->lapdm_acch;
1072 else
1073 le = &lc->lapdm_dcch;
1074
1075 /* G.2.1 No action schall be taken on frames containing an unallocated
1076 * SAPI.
1077 */
1078 dl = datalink_for_sapi(le, sapi);
1079 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001080 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001081 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001082 return -EINVAL;
1083 }
1084
Daniel Willmanne5233922012-12-25 23:15:50 +01001085 switch (msg_type) {
1086 case RSL_MT_UNIT_DATA_REQ:
1087 case RSL_MT_DATA_REQ:
1088 case RSL_MT_SUSP_REQ:
1089 case RSL_MT_REL_REQ:
1090 /* This is triggered in abnormal error conditions where
1091 * set_lapdm_context() was not called for the channel earlier. */
1092 if (!dl->dl.lctx.dl) {
1093 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1094 lc->name, rsl_msg_name(msg_type), sapi);
1095 msgb_free(msg);
1096 return -EINVAL;
1097 }
1098 break;
1099 default:
1100 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1101 lc->name, rsl_msg_name(msg_type), sapi);
1102 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001103
rootaf48bed2011-09-26 11:23:06 +02001104 switch (msg_type) {
1105 case RSL_MT_UNIT_DATA_REQ:
1106 rc = rslms_rx_rll_udata_req(msg, dl);
1107 break;
1108 case RSL_MT_EST_REQ:
1109 rc = rslms_rx_rll_est_req(msg, dl);
1110 break;
1111 case RSL_MT_DATA_REQ:
1112 rc = rslms_rx_rll_data_req(msg, dl);
1113 break;
1114 case RSL_MT_SUSP_REQ:
1115 rc = rslms_rx_rll_susp_req(msg, dl);
1116 break;
1117 case RSL_MT_RES_REQ:
1118 rc = rslms_rx_rll_res_req(msg, dl);
1119 break;
1120 case RSL_MT_RECON_REQ:
1121 rc = rslms_rx_rll_res_req(msg, dl);
1122 break;
1123 case RSL_MT_REL_REQ:
1124 rc = rslms_rx_rll_rel_req(msg, dl);
1125 break;
1126 default:
1127 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001128 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001129 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001130 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001131
1132 return rc;
1133}
1134
1135/* incoming RSLms COMMON CHANNEL message from L3 */
1136static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1137{
1138 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1139 int msg_type = cch->c.msg_type;
1140 int rc = 0;
1141
1142 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001143 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001144 return -EINVAL;
1145 }
1146
1147 switch (msg_type) {
1148 case RSL_MT_CHAN_RQD:
1149 /* create and send RACH request */
1150 rc = rslms_rx_chan_rqd(lc, msg);
1151 break;
1152 default:
rootaf48bed2011-09-26 11:23:06 +02001153 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001154 msg_type);
1155 msgb_free(msg);
1156 return 0;
1157 }
1158
1159 return rc;
1160}
1161
Harald Welte6bdf0b12011-08-17 18:22:08 +02001162/*! \brief Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001163int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1164{
1165 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1166 int rc = 0;
1167
1168 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001169 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001170 return -EINVAL;
1171 }
1172
1173 switch (rslh->msg_discr & 0xfe) {
1174 case ABIS_RSL_MDISC_RLL:
1175 rc = rslms_rx_rll(msg, lc);
1176 break;
1177 case ABIS_RSL_MDISC_COM_CHAN:
1178 rc = rslms_rx_com_chan(msg, lc);
1179 break;
1180 default:
rootaf48bed2011-09-26 11:23:06 +02001181 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001182 "discriminator 0x%02x", rslh->msg_discr);
1183 msgb_free(msg);
1184 return -EINVAL;
1185 }
1186
1187 return rc;
1188}
1189
Harald Welte6bdf0b12011-08-17 18:22:08 +02001190/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001191int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1192{
rootaf48bed2011-09-26 11:23:06 +02001193 int i;
1194 enum lapd_mode lm;
1195
Harald Welte1f0b8c22011-06-27 10:51:37 +02001196 switch (mode) {
1197 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001198 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001199 break;
1200 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001201 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001202 break;
1203 default:
1204 return -EINVAL;
1205 }
1206
rootaf48bed2011-09-26 11:23:06 +02001207 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1208 lapd_set_mode(&le->datalink[i].dl, lm);
1209 }
1210
Harald Welte1f0b8c22011-06-27 10:51:37 +02001211 le->mode = mode;
1212
1213 return 0;
1214}
1215
Harald Welte6bdf0b12011-08-17 18:22:08 +02001216/*! \brief Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001217int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1218{
1219 int rc;
1220
1221 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1222 if (rc < 0)
1223 return rc;
1224
1225 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1226}
1227
Harald Welte6bdf0b12011-08-17 18:22:08 +02001228/*! \brief Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001229void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1230{
1231 lc->lapdm_dcch.l1_prim_cb = cb;
1232 lc->lapdm_acch.l1_prim_cb = cb;
1233 lc->lapdm_dcch.l1_ctx = ctx;
1234 lc->lapdm_acch.l1_ctx = ctx;
1235}
1236
Harald Welte6bdf0b12011-08-17 18:22:08 +02001237/*! \brief Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001238void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1239{
1240 lc->lapdm_dcch.l3_cb = cb;
1241 lc->lapdm_acch.l3_cb = cb;
1242 lc->lapdm_dcch.l3_ctx = ctx;
1243 lc->lapdm_acch.l3_ctx = ctx;
1244}
1245
Harald Welte6bdf0b12011-08-17 18:22:08 +02001246/*! \brief Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001247void lapdm_entity_reset(struct lapdm_entity *le)
1248{
1249 struct lapdm_datalink *dl;
1250 int i;
1251
1252 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1253 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001254 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001255 }
1256}
1257
Harald Welte6bdf0b12011-08-17 18:22:08 +02001258/*! \brief Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001259void lapdm_channel_reset(struct lapdm_channel *lc)
1260{
1261 lapdm_entity_reset(&lc->lapdm_dcch);
1262 lapdm_entity_reset(&lc->lapdm_acch);
1263}
1264
Harald Welte6bdf0b12011-08-17 18:22:08 +02001265/*! \brief Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001266void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1267{
1268 le->flags = flags;
1269}
1270
Harald Welte6bdf0b12011-08-17 18:22:08 +02001271/*! \brief Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001272void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1273{
1274 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1275 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1276}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001277
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001278/*! @} */