blob: 41f4be71cb70a973b24bf6c2538edbed65815e80 [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
Harald Welte1f0b8c22011-06-27 10:51:37 +0200196/* Append padding (if required) */
197static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
198{
199 int pad_len = n201 - msgb_l2len(msg);
200 uint8_t *data;
201
202 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200203 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200204 "cannot pad message that is already too big!\n");
205 return;
206 }
207
208 data = msgb_put(msg, pad_len);
209 memset(data, 0x2B, pad_len);
210}
211
212/* input function that L2 calls when sending messages up to L3 */
213static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
214{
215 if (!le->l3_cb) {
216 msgb_free(msg);
217 return -EIO;
218 }
219
220 /* call the layer2 message handler that is registered */
221 return le->l3_cb(msg, le, le->l3_ctx);
222}
223
224/* write a frame into the tx queue */
225static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200226 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200227{
228 struct lapdm_entity *le = dl->entity;
229 struct osmo_phsap_prim pp;
230
231 /* if there is a pending message, queue it */
232 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200233 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200234 *msgb_push(msg, 1) = link_id;
235 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200236 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200237 return -EBUSY;
238 }
239
240 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
241 PRIM_OP_REQUEST, msg);
242 pp.u.data.chan_nr = chan_nr;
243 pp.u.data.link_id = link_id;
244
245 /* send the frame now */
246 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200247 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200248
249 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
250}
251
252static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
253{
254 struct lapdm_datalink *dl;
255 int last = le->last_tx_dequeue;
256 int i = last, n = ARRAY_SIZE(le->datalink);
257 struct msgb *msg = NULL;
258
259 /* round-robin dequeue */
260 do {
261 /* next */
262 i = (i + 1) % n;
263 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200264 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200265 break;
266 } while (i != last);
267
268 if (msg) {
269 /* Set last dequeue position */
270 le->last_tx_dequeue = i;
271 }
272
273 return msg;
274}
275
Harald Welte6bdf0b12011-08-17 18:22:08 +0200276/*! \brief dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200277 * a osmo_phsap_prim */
278int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
279{
280 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200281 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200282
283 msg = tx_dequeue_msgb(le);
284 if (!msg)
285 return -ENODEV;
286
287 /* if we have a message, send PH-DATA.req */
288 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
289 PRIM_OP_REQUEST, msg);
290
291 /* Pull chan_nr and link_id */
292 pp->u.data.chan_nr = *msg->data;
293 msgb_pull(msg, 1);
294 pp->u.data.link_id = *msg->data;
295 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200296 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200297 msgb_pull(msg, 1);
298
299 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200300 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200301
302 return 0;
303}
304
305/* get next frame from the tx queue. because the ms has multiple datalinks,
306 * each datalink's queue is read round-robin.
307 */
308static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
309{
310 struct osmo_phsap_prim pp;
311
312 /* we may send again */
313 le->tx_pending = 0;
314
315 /* free confirm message */
316 if (msg)
317 msgb_free(msg);
318
319 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
320 /* no message in all queues */
321
322 /* If user didn't request PH-EMPTY_FRAME.req, abort */
323 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
324 return 0;
325
326 /* otherwise, send PH-EMPTY_FRAME.req */
327 osmo_prim_init(&pp.oph, SAP_GSM_PH,
328 PRIM_PH_EMPTY_FRAME,
329 PRIM_OP_REQUEST, NULL);
330 } else {
331 le->tx_pending = 1;
332 }
333
334 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
335}
336
337/* Create RSLms various RSLms messages */
338static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
339 struct msgb *msg)
340{
341 /* Add the RSL + RLL header */
342 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
343
344 /* send off the RSLms message to L3 */
345 return rslms_sendmsg(msg, mctx->dl->entity);
346}
347
348/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
349static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
350{
351 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
352 struct abis_rsl_rll_hdr *rllh;
353
354 /* Add the RSL + RLL header */
355 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
356 msgb_push(msg, 2 + 2);
357 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
358 mctx->link_id, 1);
359 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
360
361 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
362 rllh->data[1] = mctx->ta_ind;
363
364 rllh->data[2] = RSL_IE_MS_POWER;
365 rllh->data[3] = mctx->tx_power_ind;
366
367 return rslms_sendmsg(msg, mctx->dl->entity);
368}
369
370static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
371{
372 struct msgb *msg;
373
374 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
375
376 /* send off the RSLms message to L3 */
377 return rslms_sendmsg(msg, mctx->dl->entity);
378}
379
380static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
381{
382 struct msgb *msg;
383
rootaf48bed2011-09-26 11:23:06 +0200384 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200385 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200386 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
387 return rslms_sendmsg(msg, mctx->dl->entity);
388}
389
rootaf48bed2011-09-26 11:23:06 +0200390/* DLSAP L2 -> L3 (RSLms) */
391static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
392 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200393{
rootaf48bed2011-09-26 11:23:06 +0200394 struct lapd_datalink *dl = lctx->dl;
395 struct lapdm_datalink *mdl =
396 container_of(dl, struct lapdm_datalink, dl);
397 struct lapdm_msg_ctx *mctx = &mdl->mctx;
398 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200399
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200400 switch (OSMO_PRIM_HDR(&dp->oph)) {
401 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
Andreas Eversberg5977db02013-06-12 09:34:51 +0200402 if (dp->oph.msg && dp->oph.msg->len == 0) {
403 /* omit L3 info by freeing message */
404 msgb_free(dp->oph.msg);
405 dp->oph.msg = NULL;
406 }
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200407 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200408 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200409 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
410 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200411 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200412 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
413 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200414 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200415 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
416 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
417 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
418 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200419 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200420 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
421 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200422 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200423 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
424 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200425 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200426 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
427 rsl_rll_error(dp->u.error_ind.cause, mctx);
428 if (dp->oph.msg)
429 msgb_free(dp->oph.msg);
430 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200431 }
rootaf48bed2011-09-26 11:23:06 +0200432
433 if (!rll_msg) {
434 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
435 "fix!\n", dp->oph.primitive, dp->oph.operation);
436 return -EINVAL;
437 }
438
439 if (!dp->oph.msg)
440 return send_rll_simple(rll_msg, mctx);
441
442 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200443}
444
rootaf48bed2011-09-26 11:23:06 +0200445/* send a data frame to layer 1 */
446static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200447{
rootaf48bed2011-09-26 11:23:06 +0200448 uint8_t l3_len = msg->tail - msg->data;
449 struct lapd_datalink *dl = lctx->dl;
450 struct lapdm_datalink *mdl =
451 container_of(dl, struct lapdm_datalink, dl);
452 struct lapdm_msg_ctx *mctx = &mdl->mctx;
453 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200454
rootaf48bed2011-09-26 11:23:06 +0200455 /* prepend l2 header */
456 msg->l2h = msgb_push(msg, 3);
457 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
458 /* EA is set here too */
459 switch (format) {
460 case LAPD_FORM_I:
461 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
462 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200463 break;
rootaf48bed2011-09-26 11:23:06 +0200464 case LAPD_FORM_S:
465 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200466 break;
rootaf48bed2011-09-26 11:23:06 +0200467 case LAPD_FORM_U:
468 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200469 break;
470 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200471 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200472 return -EINVAL;
473 }
rootaf48bed2011-09-26 11:23:06 +0200474 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
475 if (lctx->more)
476 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200477
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100478 /* add ACCH header with last indicated tx-power and TA */
479 if ((mctx->link_id & 0x40)) {
480 struct lapdm_entity *le = mdl->entity;
481
482 msg->l2h = msgb_push(msg, 2);
483 msg->l2h[0] = le->tx_power;
484 msg->l2h[1] = le->ta;
485 }
486
rootaf48bed2011-09-26 11:23:06 +0200487 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
488 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200489}
490
491/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200492static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
493 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200494{
495 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200496 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200497 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200498 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200499 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200500 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200501
502 /* when we reach here, we have a msgb with l2h pointing to the raw
503 * 23byte mac block. The l1h has already been purged. */
504
rootaf48bed2011-09-26 11:23:06 +0200505 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200506 mctx.chan_nr = chan_nr;
507 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200508
Harald Welte1f0b8c22011-06-27 10:51:37 +0200509 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
510 if (cbits == 0x10 || cbits == 0x12) {
511 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
512 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200513 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200514 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200515 } else {
516 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200517 /* It was received from network on SACCH */
518
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100519 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
520 if (le->mode == LAPDM_MODE_MS
521 && LAPDm_CTRL_is_U(msg->l2h[3])
522 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200523 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200524 n201 = N201_B4;
525 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200526 } else {
527 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200528 n201 = N201_AB_SACCH;
529 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200530 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200531 /* SACCH frames have a two-byte L1 header that
532 * OsmocomBB L1 doesn't strip */
533 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
534 mctx.ta_ind = msg->l2h[1];
535 msgb_pull(msg, 2);
536 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200537 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200538 } else {
539 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200540 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100541 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200542 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200543 }
544 }
545
Harald Welte64207742011-06-27 23:32:14 +0200546 mctx.dl = datalink_for_sapi(le, sapi);
547 /* G.2.1 No action on frames containing an unallocated SAPI. */
548 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200549 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200550 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200551 msgb_free(msg);
552 return -EIO;
553 }
554
Harald Welte1f0b8c22011-06-27 10:51:37 +0200555 switch (mctx.lapdm_fmt) {
556 case LAPDm_FMT_A:
557 case LAPDm_FMT_B:
558 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200559 lctx.dl = &mctx.dl->dl;
560 /* obtain SAPI from address field */
561 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
562 /* G.2.3 EA bit set to "0" is not allowed in GSM */
563 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
564 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
565 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200566 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200567 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200568 return -EINVAL;
569 }
rootaf48bed2011-09-26 11:23:06 +0200570 /* adress field */
571 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
572 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
573 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
574 /* command field */
575 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
576 lctx.format = LAPD_FORM_I;
577 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
578 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
579 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
580 lctx.format = LAPD_FORM_S;
581 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
582 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
583 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
584 lctx.format = LAPD_FORM_U;
585 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
586 } else
587 lctx.format = LAPD_FORM_UKN;
588 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
589 if (lctx.sapi != LAPDm_SAPI_NORMAL
590 && lctx.sapi != LAPDm_SAPI_SMS
591 && lctx.format == LAPD_FORM_U
592 && lctx.s_u == LAPDm_U_UI) {
593 /* 5.3.3 UI frames with invalid SAPI values shall be
594 * discarded
595 */
596 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
597 lctx.sapi);
598 msgb_free(msg);
599 return 0;
600 }
601 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
602 lctx.n201 = n201;
603 lctx.length = n201;
604 lctx.more = 0;
605 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100606 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200607 } else {
608 /* length field */
609 if (!(msg->l2h[2] & LAPDm_EL)) {
610 /* G.4.1 If the EL bit is set to "0", an
611 * MDL-ERROR-INDICATION primitive with cause
612 * "frame not implemented" is sent to the
613 * mobile management entity. */
614 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
615 "multi-octet length\n");
616 msgb_free(msg);
617 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
618 return -EINVAL;
619 }
620 lctx.n201 = n201;
621 lctx.length = msg->l2h[2] >> 2;
622 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
623 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100624 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200625 }
626 /* store context for messages from lapd */
627 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
628 /* send to LAPD */
629 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200630 break;
631 case LAPDm_FMT_Bter:
632 /* FIXME */
633 msgb_free(msg);
634 break;
635 case LAPDm_FMT_Bbis:
636 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200637 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200638 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100639 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200640 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
641 break;
642 default:
643 msgb_free(msg);
644 }
645
646 return rc;
647}
648
649/* input into layer2 (from layer 1) */
650static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
651{
652 struct abis_rsl_cchan_hdr *ch;
653 struct gsm48_req_ref req_ref;
654 struct gsm_time gt;
655 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
656
657 msg->l2h = msgb_push(msg, sizeof(*ch));
658 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
659 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
660 ch->chan_nr = RSL_CHAN_RACH;
661
662 /* generate a RSL CHANNEL REQUIRED message */
663 gsm_fn2gsmtime(&gt, fn);
664 req_ref.ra = ra;
665 req_ref.t1 = gt.t1; /* FIXME: modulo? */
666 req_ref.t2 = gt.t2;
667 req_ref.t3_low = gt.t3 & 7;
668 req_ref.t3_high = gt.t3 >> 3;
669
670 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
671 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
672
673 return rslms_sendmsg(msg, le);
674}
675
676static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
677
Harald Welte6bdf0b12011-08-17 18:22:08 +0200678/*! \brief Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200679int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
680{
681 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
682 int rc = 0;
683
684 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200685 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200686 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100687 rc = -ENODEV;
688 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200689 }
690
691 switch (oph->primitive) {
692 case PRIM_PH_DATA:
693 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200694 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200695 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100696 rc = -ENODEV;
697 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200698 }
699 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
700 pp->u.data.link_id);
701 break;
702 case PRIM_PH_RTS:
703 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200704 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200705 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100706 rc = -ENODEV;
707 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200708 }
709 rc = l2_ph_data_conf(oph->msg, le);
710 break;
711 case PRIM_PH_RACH:
712 switch (oph->operation) {
713 case PRIM_OP_INDICATION:
714 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
715 pp->u.rach_ind.acc_delay);
716 break;
717 case PRIM_OP_CONFIRM:
718 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
719 break;
720 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100721 rc = -EIO;
722 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200723 }
724 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100725 default:
726 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
727 oph->primitive);
728 rc = -EINVAL;
729 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200730 }
731
732 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100733
734free:
735 msgb_free(oph->msg);
736 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200737}
738
739
740/* L3 -> L2 / RSLMS -> LAPDm */
741
rootaf48bed2011-09-26 11:23:06 +0200742/* Set LAPDm context for established connection */
743static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
744 uint8_t link_id, int n201, uint8_t sapi)
745{
746 memset(&dl->mctx, 0, sizeof(dl->mctx));
747 dl->mctx.dl = dl;
748 dl->mctx.chan_nr = chan_nr;
749 dl->mctx.link_id = link_id;
750 dl->dl.lctx.dl = &dl->dl;
751 dl->dl.lctx.n201 = n201;
752 dl->dl.lctx.sapi = sapi;
753
754 return 0;
755}
756
Harald Welte1f0b8c22011-06-27 10:51:37 +0200757/* L3 requests establishment of data link */
758static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
759{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200760 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
761 uint8_t chan_nr = rllh->chan_nr;
762 uint8_t link_id = rllh->link_id;
763 uint8_t sapi = rllh->link_id & 7;
764 struct tlv_parsed tv;
765 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100766 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200767 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200768
rootaf48bed2011-09-26 11:23:06 +0200769 /* Set LAPDm context for established connection */
770 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200771
rootaf48bed2011-09-26 11:23:06 +0200772 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200773 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200774 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200775 /* contention resolution establishment procedure */
776 if (sapi != 0) {
777 /* According to clause 6, the contention resolution
778 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200779 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200780 "resolution (discarding)\n");
781 msgb_free(msg);
782 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
783 }
784 /* transmit a SABM command with the P bit set to "1". The SABM
785 * command shall contain the layer 3 message unit */
786 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200787 } else {
788 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200789 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200790 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200791 }
792
793 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100794 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200795 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100796 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200797 msgb_free(msg);
798 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
799 }
800
rootaf48bed2011-09-26 11:23:06 +0200801 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100802 msgb_pull_to_l3(msg);
803 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200804
rootaf48bed2011-09-26 11:23:06 +0200805 /* prepare prim */
806 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200807
rootaf48bed2011-09-26 11:23:06 +0200808 /* send to L2 */
809 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200810}
811
812/* L3 requests transfer of unnumbered information */
813static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
814{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100815 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200816 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
817 uint8_t chan_nr = rllh->chan_nr;
818 uint8_t link_id = rllh->link_id;
Andreas Eversberg5977db02013-06-12 09:34:51 +0200819 int ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200820 uint8_t sapi = link_id & 7;
821 struct tlv_parsed tv;
822 int length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200823
824 /* check if the layer3 message length exceeds N201 */
825
826 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
827
828 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100829 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200830 }
831 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100832 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200833 }
834 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200835 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200836 "error\n");
837 msgb_free(msg);
838 return -EINVAL;
839 }
rootaf48bed2011-09-26 11:23:06 +0200840 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200841 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
842 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200843 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +0200844 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +0200845 "(discarding)\n", length,
846 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200847 msgb_free(msg);
848 return -EIO;
849 }
850
rootaf48bed2011-09-26 11:23:06 +0200851 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100852 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200853
rootaf48bed2011-09-26 11:23:06 +0200854 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100855 msgb_pull_to_l3(msg);
856 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200857
858 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +0200859 msg->l2h = msgb_push(msg, 2 + !ui_bts);
860 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
861 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100862 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +0200863 msg->l2h[2] = LAPDm_LEN(length);
864 if (link_id & 0x40) {
865 msg->l2h = msgb_push(msg, 2);
866 msg->l2h[0] = le->tx_power;
867 msg->l2h[1] = le->ta;
868 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200869
870 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200871 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200872}
873
874/* L3 requests transfer of acknowledged information */
875static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
876{
877 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
878 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200879 int length;
880 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200881
882 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
883 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200884 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200885 "error\n");
886 msgb_free(msg);
887 return -EINVAL;
888 }
rootaf48bed2011-09-26 11:23:06 +0200889 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
890 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200891
rootaf48bed2011-09-26 11:23:06 +0200892 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100893 msgb_pull_to_l3(msg);
894 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200895
rootaf48bed2011-09-26 11:23:06 +0200896 /* prepare prim */
897 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200898
rootaf48bed2011-09-26 11:23:06 +0200899 /* send to L2 */
900 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200901}
902
903/* L3 requests suspension of data link */
904static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
905{
906 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
907 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200908 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200909
910 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200911 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200912 msgb_free(msg);
913 return -EINVAL;
914 }
915
rootaf48bed2011-09-26 11:23:06 +0200916 /* prepare prim */
917 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200918
rootaf48bed2011-09-26 11:23:06 +0200919 /* send to L2 */
920 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200921}
922
923/* L3 requests resume of data link */
924static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
925{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200926 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200927 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200928 uint8_t chan_nr = rllh->chan_nr;
929 uint8_t link_id = rllh->link_id;
930 uint8_t sapi = rllh->link_id & 7;
931 struct tlv_parsed tv;
932 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100933 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200934 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200935
rootaf48bed2011-09-26 11:23:06 +0200936 /* Set LAPDm context for established connection */
937 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200938
939 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
940 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200941 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200942 msgb_free(msg);
943 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
944 }
rootaf48bed2011-09-26 11:23:06 +0200945 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200946 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
947
rootaf48bed2011-09-26 11:23:06 +0200948 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100949 msgb_pull_to_l3(msg);
950 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200951
rootaf48bed2011-09-26 11:23:06 +0200952 /* prepare prim */
953 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
954 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200955
rootaf48bed2011-09-26 11:23:06 +0200956 /* send to L2 */
957 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200958}
959
960/* L3 requests release of data link */
961static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
962{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200963 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200964 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +0200965 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200966
967 /* get release mode */
968 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
969 mode = rllh->data[1] & 1;
970
Harald Welte1f0b8c22011-06-27 10:51:37 +0200971 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100972 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +0200973
974 /* 04.06 3.8.3: No information field is permitted with the DISC
975 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100976 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200977
rootaf48bed2011-09-26 11:23:06 +0200978 /* prepare prim */
979 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
980 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200981
rootaf48bed2011-09-26 11:23:06 +0200982 /* send to L2 */
983 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200984}
985
986/* L3 requests channel in idle state */
987static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
988{
989 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
990 void *l1ctx = lc->lapdm_dcch.l1_ctx;
991 struct osmo_phsap_prim pp;
992
993 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
994 PRIM_OP_REQUEST, NULL);
995
996 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +0200997 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200998 return -EINVAL;
999 }
1000 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001001 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001002 return -EINVAL;
1003 }
1004 pp.u.rach_req.ra = cch->data[1];
1005 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1006 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1007
1008 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001009 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001010 return -EINVAL;
1011 }
1012 /* TA = 0 - delay */
1013 pp.u.rach_req.ta = 0 - cch->data[5];
1014
1015 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001016 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001017 return -EINVAL;
1018 }
1019 pp.u.rach_req.tx_power = cch->data[7];
1020
1021 msgb_free(msg);
1022
1023 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1024}
1025
1026/* L1 confirms channel request */
1027static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1028{
1029 struct abis_rsl_cchan_hdr *ch;
1030 struct gsm_time tm;
1031 struct gsm48_req_ref *ref;
1032
1033 gsm_fn2gsmtime(&tm, frame_nr);
1034
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001035 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001036 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1037 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1038 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1039 ch->chan_nr = RSL_CHAN_RACH;
1040 ch->data[0] = RSL_IE_REQ_REFERENCE;
1041 ref = (struct gsm48_req_ref *) (ch->data + 1);
1042 ref->t1 = tm.t1;
1043 ref->t2 = tm.t2;
1044 ref->t3_low = tm.t3 & 0x7;
1045 ref->t3_high = tm.t3 >> 3;
1046
1047 return rslms_sendmsg(msg, le);
1048}
1049
Harald Welte1f0b8c22011-06-27 10:51:37 +02001050/* incoming RSLms RLL message from L3 */
1051static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1052{
1053 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1054 int msg_type = rllh->c.msg_type;
1055 uint8_t sapi = rllh->link_id & 7;
1056 struct lapdm_entity *le;
1057 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001058 int rc = 0;
1059
1060 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001061 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001062 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001063 return -EINVAL;
1064 }
1065
1066 if (rllh->link_id & 0x40)
1067 le = &lc->lapdm_acch;
1068 else
1069 le = &lc->lapdm_dcch;
1070
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001071 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001072 * SAPI.
1073 */
1074 dl = datalink_for_sapi(le, sapi);
1075 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001076 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001077 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001078 return -EINVAL;
1079 }
1080
Daniel Willmanne5233922012-12-25 23:15:50 +01001081 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001082 case RSL_MT_DATA_REQ:
1083 case RSL_MT_SUSP_REQ:
1084 case RSL_MT_REL_REQ:
1085 /* This is triggered in abnormal error conditions where
1086 * set_lapdm_context() was not called for the channel earlier. */
1087 if (!dl->dl.lctx.dl) {
1088 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1089 lc->name, rsl_msg_name(msg_type), sapi);
1090 msgb_free(msg);
1091 return -EINVAL;
1092 }
1093 break;
1094 default:
1095 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1096 lc->name, rsl_msg_name(msg_type), sapi);
1097 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001098
rootaf48bed2011-09-26 11:23:06 +02001099 switch (msg_type) {
1100 case RSL_MT_UNIT_DATA_REQ:
1101 rc = rslms_rx_rll_udata_req(msg, dl);
1102 break;
1103 case RSL_MT_EST_REQ:
1104 rc = rslms_rx_rll_est_req(msg, dl);
1105 break;
1106 case RSL_MT_DATA_REQ:
1107 rc = rslms_rx_rll_data_req(msg, dl);
1108 break;
1109 case RSL_MT_SUSP_REQ:
1110 rc = rslms_rx_rll_susp_req(msg, dl);
1111 break;
1112 case RSL_MT_RES_REQ:
1113 rc = rslms_rx_rll_res_req(msg, dl);
1114 break;
1115 case RSL_MT_RECON_REQ:
1116 rc = rslms_rx_rll_res_req(msg, dl);
1117 break;
1118 case RSL_MT_REL_REQ:
1119 rc = rslms_rx_rll_rel_req(msg, dl);
1120 break;
1121 default:
1122 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001123 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001124 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001125 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001126
1127 return rc;
1128}
1129
1130/* incoming RSLms COMMON CHANNEL message from L3 */
1131static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1132{
1133 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1134 int msg_type = cch->c.msg_type;
1135 int rc = 0;
1136
1137 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001138 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001139 return -EINVAL;
1140 }
1141
1142 switch (msg_type) {
1143 case RSL_MT_CHAN_RQD:
1144 /* create and send RACH request */
1145 rc = rslms_rx_chan_rqd(lc, msg);
1146 break;
1147 default:
rootaf48bed2011-09-26 11:23:06 +02001148 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001149 msg_type);
1150 msgb_free(msg);
1151 return 0;
1152 }
1153
1154 return rc;
1155}
1156
Harald Welte6bdf0b12011-08-17 18:22:08 +02001157/*! \brief Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001158int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1159{
1160 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1161 int rc = 0;
1162
1163 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001164 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001165 return -EINVAL;
1166 }
1167
1168 switch (rslh->msg_discr & 0xfe) {
1169 case ABIS_RSL_MDISC_RLL:
1170 rc = rslms_rx_rll(msg, lc);
1171 break;
1172 case ABIS_RSL_MDISC_COM_CHAN:
1173 rc = rslms_rx_com_chan(msg, lc);
1174 break;
1175 default:
rootaf48bed2011-09-26 11:23:06 +02001176 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001177 "discriminator 0x%02x", rslh->msg_discr);
1178 msgb_free(msg);
1179 return -EINVAL;
1180 }
1181
1182 return rc;
1183}
1184
Harald Welte6bdf0b12011-08-17 18:22:08 +02001185/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001186int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1187{
rootaf48bed2011-09-26 11:23:06 +02001188 int i;
1189 enum lapd_mode lm;
1190
Harald Welte1f0b8c22011-06-27 10:51:37 +02001191 switch (mode) {
1192 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001193 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001194 break;
1195 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001196 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001197 break;
1198 default:
1199 return -EINVAL;
1200 }
1201
rootaf48bed2011-09-26 11:23:06 +02001202 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1203 lapd_set_mode(&le->datalink[i].dl, lm);
1204 }
1205
Harald Welte1f0b8c22011-06-27 10:51:37 +02001206 le->mode = mode;
1207
1208 return 0;
1209}
1210
Harald Welte6bdf0b12011-08-17 18:22:08 +02001211/*! \brief Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001212int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1213{
1214 int rc;
1215
1216 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1217 if (rc < 0)
1218 return rc;
1219
1220 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1221}
1222
Harald Welte6bdf0b12011-08-17 18:22:08 +02001223/*! \brief Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001224void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1225{
1226 lc->lapdm_dcch.l1_prim_cb = cb;
1227 lc->lapdm_acch.l1_prim_cb = cb;
1228 lc->lapdm_dcch.l1_ctx = ctx;
1229 lc->lapdm_acch.l1_ctx = ctx;
1230}
1231
Harald Welte6bdf0b12011-08-17 18:22:08 +02001232/*! \brief Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001233void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1234{
1235 lc->lapdm_dcch.l3_cb = cb;
1236 lc->lapdm_acch.l3_cb = cb;
1237 lc->lapdm_dcch.l3_ctx = ctx;
1238 lc->lapdm_acch.l3_ctx = ctx;
1239}
1240
Harald Welte6bdf0b12011-08-17 18:22:08 +02001241/*! \brief Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001242void lapdm_entity_reset(struct lapdm_entity *le)
1243{
1244 struct lapdm_datalink *dl;
1245 int i;
1246
1247 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1248 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001249 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001250 }
1251}
1252
Harald Welte6bdf0b12011-08-17 18:22:08 +02001253/*! \brief Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001254void lapdm_channel_reset(struct lapdm_channel *lc)
1255{
1256 lapdm_entity_reset(&lc->lapdm_dcch);
1257 lapdm_entity_reset(&lc->lapdm_acch);
1258}
1259
Harald Welte6bdf0b12011-08-17 18:22:08 +02001260/*! \brief Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001261void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1262{
1263 le->flags = flags;
1264}
1265
Harald Welte6bdf0b12011-08-17 18:22:08 +02001266/*! \brief Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001267void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1268{
1269 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1270 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1271}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001272
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001273/*! @} */