blob: 82f8b0c182ffeb06aad102fbb32e05a83d1935a8 [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,
119 struct lapdm_entity *entity)
120{
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;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200130}
131
Harald Welte6bdf0b12011-08-17 18:22:08 +0200132/*! \brief initialize a LAPDm entity and all datalinks inside
133 * \param[in] le LAPDm entity
134 * \param[in] mode \ref lapdm_mode (BTS/MS)
135 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200136void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode)
137{
138 unsigned int i;
139
140 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
141 lapdm_dl_init(&le->datalink[i], le);
142
143 lapdm_entity_set_mode(le, mode);
144}
145
Harald Welte6bdf0b12011-08-17 18:22:08 +0200146/*! \brief initialize a LAPDm channel and all its channels
147 * \param[in] lc \ref lapdm_channel to be initialized
148 * \param[in] mode \ref lapdm_mode (BTS/MS)
149 *
150 * This really is a convenience wrapper around calling \ref
151 * lapdm_entity_init twice.
152 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200153void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
154{
155 lapdm_entity_init(&lc->lapdm_acch, mode);
rootaf48bed2011-09-26 11:23:06 +0200156 /* FIXME: this depends on chan type */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200157 lapdm_entity_init(&lc->lapdm_dcch, mode);
158}
159
Harald Welte6bdf0b12011-08-17 18:22:08 +0200160/*! \brief flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200161void lapdm_entity_exit(struct lapdm_entity *le)
162{
163 unsigned int i;
164 struct lapdm_datalink *dl;
165
166 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
167 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200168 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200169 }
170}
171
Harald Welte6bdf0b12011-08-17 18:22:08 +0200172/* \brief lfush and release all resources in LAPDm channel
173 *
174 * A convenience wrapper calling \ref lapdm_entity_exit on both
175 * entities inside the \ref lapdm_channel
176 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200177void lapdm_channel_exit(struct lapdm_channel *lc)
178{
179 lapdm_entity_exit(&lc->lapdm_acch);
180 lapdm_entity_exit(&lc->lapdm_dcch);
181}
182
Harald Welte1f0b8c22011-06-27 10:51:37 +0200183static struct lapdm_datalink *datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
184{
185 switch (sapi) {
186 case LAPDm_SAPI_NORMAL:
187 return &le->datalink[0];
188 case LAPDm_SAPI_SMS:
189 return &le->datalink[1];
190 default:
191 return NULL;
192 }
193}
194
195/* remove the L2 header from a MSGB */
196static inline unsigned char *msgb_pull_l2h(struct msgb *msg)
197{
198 unsigned char *ret = msgb_pull(msg, msg->l3h - msg->l2h);
199 msg->l2h = NULL;
200 return ret;
201}
202
203/* Append padding (if required) */
204static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
205{
206 int pad_len = n201 - msgb_l2len(msg);
207 uint8_t *data;
208
209 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200210 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200211 "cannot pad message that is already too big!\n");
212 return;
213 }
214
215 data = msgb_put(msg, pad_len);
216 memset(data, 0x2B, pad_len);
217}
218
219/* input function that L2 calls when sending messages up to L3 */
220static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
221{
222 if (!le->l3_cb) {
223 msgb_free(msg);
224 return -EIO;
225 }
226
227 /* call the layer2 message handler that is registered */
228 return le->l3_cb(msg, le, le->l3_ctx);
229}
230
231/* write a frame into the tx queue */
232static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200233 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200234{
235 struct lapdm_entity *le = dl->entity;
236 struct osmo_phsap_prim pp;
237
238 /* if there is a pending message, queue it */
239 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200240 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200241 *msgb_push(msg, 1) = link_id;
242 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200243 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200244 return -EBUSY;
245 }
246
247 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
248 PRIM_OP_REQUEST, msg);
249 pp.u.data.chan_nr = chan_nr;
250 pp.u.data.link_id = link_id;
251
252 /* send the frame now */
253 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200254 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200255
256 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
257}
258
259static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
260{
261 struct lapdm_datalink *dl;
262 int last = le->last_tx_dequeue;
263 int i = last, n = ARRAY_SIZE(le->datalink);
264 struct msgb *msg = NULL;
265
266 /* round-robin dequeue */
267 do {
268 /* next */
269 i = (i + 1) % n;
270 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200271 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200272 break;
273 } while (i != last);
274
275 if (msg) {
276 /* Set last dequeue position */
277 le->last_tx_dequeue = i;
278 }
279
280 return msg;
281}
282
Harald Welte6bdf0b12011-08-17 18:22:08 +0200283/*! \brief dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200284 * a osmo_phsap_prim */
285int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
286{
287 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200288 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200289
290 msg = tx_dequeue_msgb(le);
291 if (!msg)
292 return -ENODEV;
293
294 /* if we have a message, send PH-DATA.req */
295 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
296 PRIM_OP_REQUEST, msg);
297
298 /* Pull chan_nr and link_id */
299 pp->u.data.chan_nr = *msg->data;
300 msgb_pull(msg, 1);
301 pp->u.data.link_id = *msg->data;
302 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200303 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200304 msgb_pull(msg, 1);
305
306 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200307 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200308
309 return 0;
310}
311
312/* get next frame from the tx queue. because the ms has multiple datalinks,
313 * each datalink's queue is read round-robin.
314 */
315static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
316{
317 struct osmo_phsap_prim pp;
318
319 /* we may send again */
320 le->tx_pending = 0;
321
322 /* free confirm message */
323 if (msg)
324 msgb_free(msg);
325
326 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
327 /* no message in all queues */
328
329 /* If user didn't request PH-EMPTY_FRAME.req, abort */
330 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
331 return 0;
332
333 /* otherwise, send PH-EMPTY_FRAME.req */
334 osmo_prim_init(&pp.oph, SAP_GSM_PH,
335 PRIM_PH_EMPTY_FRAME,
336 PRIM_OP_REQUEST, NULL);
337 } else {
338 le->tx_pending = 1;
339 }
340
341 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
342}
343
344/* Create RSLms various RSLms messages */
345static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
346 struct msgb *msg)
347{
348 /* Add the RSL + RLL header */
349 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, 1);
350
351 /* send off the RSLms message to L3 */
352 return rslms_sendmsg(msg, mctx->dl->entity);
353}
354
355/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
356static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
357{
358 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
359 struct abis_rsl_rll_hdr *rllh;
360
361 /* Add the RSL + RLL header */
362 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
363 msgb_push(msg, 2 + 2);
364 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
365 mctx->link_id, 1);
366 rllh = (struct abis_rsl_rll_hdr *)msgb_l2(msg);
367
368 rllh->data[0] = RSL_IE_TIMING_ADVANCE;
369 rllh->data[1] = mctx->ta_ind;
370
371 rllh->data[2] = RSL_IE_MS_POWER;
372 rllh->data[3] = mctx->tx_power_ind;
373
374 return rslms_sendmsg(msg, mctx->dl->entity);
375}
376
377static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
378{
379 struct msgb *msg;
380
381 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, 1);
382
383 /* send off the RSLms message to L3 */
384 return rslms_sendmsg(msg, mctx->dl->entity);
385}
386
387static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
388{
389 struct msgb *msg;
390
rootaf48bed2011-09-26 11:23:06 +0200391 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200392 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 1);
rootaf48bed2011-09-26 11:23:06 +0200393 msg->l2h = msgb_put(msg, sizeof(struct abis_rsl_rll_hdr));
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
rootaf48bed2011-09-26 11:23:06 +0200481 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
482 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200483}
484
485/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200486static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
487 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200488{
489 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200490 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200491 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200492 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200493 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200494 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200495
496 /* when we reach here, we have a msgb with l2h pointing to the raw
497 * 23byte mac block. The l1h has already been purged. */
498
rootaf48bed2011-09-26 11:23:06 +0200499 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200500 mctx.chan_nr = chan_nr;
501 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200502
Harald Welte1f0b8c22011-06-27 10:51:37 +0200503 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
504 if (cbits == 0x10 || cbits == 0x12) {
505 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
506 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200507 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200508 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200509 } else {
510 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200511 /* It was received from network on SACCH */
512
513 /* If sent by BTS, lapdm_fmt must be B4 */
514 if (le->mode == LAPDM_MODE_MS) {
515 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200516 n201 = N201_B4;
517 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200518 } else {
519 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200520 n201 = N201_AB_SACCH;
521 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200522 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200523 /* SACCH frames have a two-byte L1 header that
524 * OsmocomBB L1 doesn't strip */
525 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
526 mctx.ta_ind = msg->l2h[1];
527 msgb_pull(msg, 2);
528 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200529 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200530 } else {
531 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200532 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
533 n201 = 20; // FIXME: select correct size by chan.
Harald Welte64207742011-06-27 23:32:14 +0200534 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200535 }
536 }
537
Harald Welte64207742011-06-27 23:32:14 +0200538 mctx.dl = datalink_for_sapi(le, sapi);
539 /* G.2.1 No action on frames containing an unallocated SAPI. */
540 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200541 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200542 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200543 msgb_free(msg);
544 return -EIO;
545 }
546
Harald Welte1f0b8c22011-06-27 10:51:37 +0200547 switch (mctx.lapdm_fmt) {
548 case LAPDm_FMT_A:
549 case LAPDm_FMT_B:
550 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200551 lctx.dl = &mctx.dl->dl;
552 /* obtain SAPI from address field */
553 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
554 /* G.2.3 EA bit set to "0" is not allowed in GSM */
555 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
556 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
557 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200558 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200559 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200560 return -EINVAL;
561 }
rootaf48bed2011-09-26 11:23:06 +0200562 /* adress field */
563 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
564 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
565 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
566 /* command field */
567 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
568 lctx.format = LAPD_FORM_I;
569 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
570 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
571 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
572 lctx.format = LAPD_FORM_S;
573 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
574 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
575 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
576 lctx.format = LAPD_FORM_U;
577 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
578 } else
579 lctx.format = LAPD_FORM_UKN;
580 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
581 if (lctx.sapi != LAPDm_SAPI_NORMAL
582 && lctx.sapi != LAPDm_SAPI_SMS
583 && lctx.format == LAPD_FORM_U
584 && lctx.s_u == LAPDm_U_UI) {
585 /* 5.3.3 UI frames with invalid SAPI values shall be
586 * discarded
587 */
588 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
589 lctx.sapi);
590 msgb_free(msg);
591 return 0;
592 }
593 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
594 lctx.n201 = n201;
595 lctx.length = n201;
596 lctx.more = 0;
597 msg->l3h = msg->l2h + 2;
598 msgb_pull_l2h(msg);
599 } else {
600 /* length field */
601 if (!(msg->l2h[2] & LAPDm_EL)) {
602 /* G.4.1 If the EL bit is set to "0", an
603 * MDL-ERROR-INDICATION primitive with cause
604 * "frame not implemented" is sent to the
605 * mobile management entity. */
606 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
607 "multi-octet length\n");
608 msgb_free(msg);
609 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
610 return -EINVAL;
611 }
612 lctx.n201 = n201;
613 lctx.length = msg->l2h[2] >> 2;
614 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
615 msg->l3h = msg->l2h + 3;
616 msgb_pull_l2h(msg);
617 }
618 /* store context for messages from lapd */
619 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
620 /* send to LAPD */
621 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200622 break;
623 case LAPDm_FMT_Bter:
624 /* FIXME */
625 msgb_free(msg);
626 break;
627 case LAPDm_FMT_Bbis:
628 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200629 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200630 msg->l3h = msg->l2h;
631 msgb_pull_l2h(msg);
632 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
633 break;
634 default:
635 msgb_free(msg);
636 }
637
638 return rc;
639}
640
641/* input into layer2 (from layer 1) */
642static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
643{
644 struct abis_rsl_cchan_hdr *ch;
645 struct gsm48_req_ref req_ref;
646 struct gsm_time gt;
647 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
648
649 msg->l2h = msgb_push(msg, sizeof(*ch));
650 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
651 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
652 ch->chan_nr = RSL_CHAN_RACH;
653
654 /* generate a RSL CHANNEL REQUIRED message */
655 gsm_fn2gsmtime(&gt, fn);
656 req_ref.ra = ra;
657 req_ref.t1 = gt.t1; /* FIXME: modulo? */
658 req_ref.t2 = gt.t2;
659 req_ref.t3_low = gt.t3 & 7;
660 req_ref.t3_high = gt.t3 >> 3;
661
662 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
663 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
664
665 return rslms_sendmsg(msg, le);
666}
667
668static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
669
Harald Welte6bdf0b12011-08-17 18:22:08 +0200670/*! \brief Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200671int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
672{
673 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
674 int rc = 0;
675
676 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200677 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200678 oph->sap);
679 return -ENODEV;
680 }
681
682 switch (oph->primitive) {
683 case PRIM_PH_DATA:
684 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200685 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200686 oph->operation);
687 return -ENODEV;
688 }
689 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
690 pp->u.data.link_id);
691 break;
692 case PRIM_PH_RTS:
693 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200694 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200695 oph->operation);
696 return -ENODEV;
697 }
698 rc = l2_ph_data_conf(oph->msg, le);
699 break;
700 case PRIM_PH_RACH:
701 switch (oph->operation) {
702 case PRIM_OP_INDICATION:
703 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
704 pp->u.rach_ind.acc_delay);
705 break;
706 case PRIM_OP_CONFIRM:
707 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
708 break;
709 default:
710 return -EIO;
711 }
712 break;
713 }
714
715 return rc;
716}
717
718
719/* L3 -> L2 / RSLMS -> LAPDm */
720
rootaf48bed2011-09-26 11:23:06 +0200721/* Set LAPDm context for established connection */
722static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
723 uint8_t link_id, int n201, uint8_t sapi)
724{
725 memset(&dl->mctx, 0, sizeof(dl->mctx));
726 dl->mctx.dl = dl;
727 dl->mctx.chan_nr = chan_nr;
728 dl->mctx.link_id = link_id;
729 dl->dl.lctx.dl = &dl->dl;
730 dl->dl.lctx.n201 = n201;
731 dl->dl.lctx.sapi = sapi;
732
733 return 0;
734}
735
Harald Welte1f0b8c22011-06-27 10:51:37 +0200736/* L3 requests establishment of data link */
737static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
738{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200739 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
740 uint8_t chan_nr = rllh->chan_nr;
741 uint8_t link_id = rllh->link_id;
742 uint8_t sapi = rllh->link_id & 7;
743 struct tlv_parsed tv;
744 uint8_t length;
rootaf48bed2011-09-26 11:23:06 +0200745 int n201 = 20; //FIXME
746 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200747
rootaf48bed2011-09-26 11:23:06 +0200748 /* Set LAPDm context for established connection */
749 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200750
rootaf48bed2011-09-26 11:23:06 +0200751 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200752 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200753 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200754 /* contention resolution establishment procedure */
755 if (sapi != 0) {
756 /* According to clause 6, the contention resolution
757 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200758 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200759 "resolution (discarding)\n");
760 msgb_free(msg);
761 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
762 }
763 /* transmit a SABM command with the P bit set to "1". The SABM
764 * command shall contain the layer 3 message unit */
765 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200766 } else {
767 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200768 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200769 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200770 }
771
772 /* check if the layer3 message length exceeds N201 */
773 if (length + 3 > 21) { /* FIXME: do we know the channel N201? */
rootaf48bed2011-09-26 11:23:06 +0200774 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200775 "(discarding)\n", length + 3, 21);
776 msgb_free(msg);
777 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
778 }
779
rootaf48bed2011-09-26 11:23:06 +0200780 /* Remove RLL header from msgb and set length to L3-info */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200781 msgb_pull_l2h(msg);
rootaf48bed2011-09-26 11:23:06 +0200782 msg->len = length;
783 msg->tail = msg->data + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200784
rootaf48bed2011-09-26 11:23:06 +0200785 /* prepare prim */
786 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200787
rootaf48bed2011-09-26 11:23:06 +0200788 /* send to L2 */
789 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200790}
791
792/* L3 requests transfer of unnumbered information */
793static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
794{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200795 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
796 uint8_t chan_nr = rllh->chan_nr;
797 uint8_t link_id = rllh->link_id;
798 uint8_t sapi = link_id & 7;
799 struct tlv_parsed tv;
800 int length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200801 uint8_t ta = 0, tx_power = 0;
802
803 /* check if the layer3 message length exceeds N201 */
804
805 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
806
807 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
808 ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
809 }
810 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
811 tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
812 }
813 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200814 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200815 "error\n");
816 msgb_free(msg);
817 return -EINVAL;
818 }
rootaf48bed2011-09-26 11:23:06 +0200819 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200820 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
821 /* check if the layer3 message length exceeds N201 */
822 if (length + 5 > 23) { /* FIXME: do we know the channel N201? */
rootaf48bed2011-09-26 11:23:06 +0200823 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200824 "(discarding)\n", length + 5, 23);
825 msgb_free(msg);
826 return -EIO;
827 }
828
rootaf48bed2011-09-26 11:23:06 +0200829 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200830 tx_power, ta);
831
rootaf48bed2011-09-26 11:23:06 +0200832 /* Remove RLL header from msgb and set length to L3-info */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200833 msgb_pull_l2h(msg);
rootaf48bed2011-09-26 11:23:06 +0200834 msg->len = length;
835 msg->tail = msg->data + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200836
837 /* Push L1 + LAPDm header on msgb */
838 msg->l2h = msgb_push(msg, 2 + 3);
839 msg->l2h[0] = tx_power;
840 msg->l2h[1] = ta;
rootaf48bed2011-09-26 11:23:06 +0200841 msg->l2h[2] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200842 msg->l2h[3] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
843 msg->l2h[4] = LAPDm_LEN(length);
844 // FIXME: short L2 header support
845
846 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +0200847 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200848}
849
850/* L3 requests transfer of acknowledged information */
851static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
852{
853 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
854 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +0200855 int length;
856 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200857
858 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
859 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200860 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200861 "error\n");
862 msgb_free(msg);
863 return -EINVAL;
864 }
rootaf48bed2011-09-26 11:23:06 +0200865 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
866 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200867
rootaf48bed2011-09-26 11:23:06 +0200868 /* Remove RLL header from msgb and set length to L3-info */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200869 msgb_pull_l2h(msg);
rootaf48bed2011-09-26 11:23:06 +0200870 msg->len = length;
871 msg->tail = msg->data + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200872
rootaf48bed2011-09-26 11:23:06 +0200873 /* prepare prim */
874 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200875
rootaf48bed2011-09-26 11:23:06 +0200876 /* send to L2 */
877 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200878}
879
880/* L3 requests suspension of data link */
881static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
882{
883 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
884 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +0200885 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200886
887 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +0200888 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200889 msgb_free(msg);
890 return -EINVAL;
891 }
892
rootaf48bed2011-09-26 11:23:06 +0200893 /* prepare prim */
894 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200895
rootaf48bed2011-09-26 11:23:06 +0200896 /* send to L2 */
897 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200898}
899
900/* L3 requests resume of data link */
901static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
902{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200903 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +0200904 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200905 uint8_t chan_nr = rllh->chan_nr;
906 uint8_t link_id = rllh->link_id;
907 uint8_t sapi = rllh->link_id & 7;
908 struct tlv_parsed tv;
909 uint8_t length;
rootaf48bed2011-09-26 11:23:06 +0200910 uint8_t n201 = 20; //FIXME
911 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200912
rootaf48bed2011-09-26 11:23:06 +0200913 /* Set LAPDm context for established connection */
914 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200915
916 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
917 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200918 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200919 msgb_free(msg);
920 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
921 }
rootaf48bed2011-09-26 11:23:06 +0200922 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200923 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
924
rootaf48bed2011-09-26 11:23:06 +0200925 /* Remove RLL header from msgb and set length to L3-info */
926 msgb_pull_l2h(msg);
927 msg->len = length;
928 msg->tail = msg->data + length;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200929
rootaf48bed2011-09-26 11:23:06 +0200930 /* prepare prim */
931 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
932 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200933
rootaf48bed2011-09-26 11:23:06 +0200934 /* send to L2 */
935 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200936}
937
938/* L3 requests release of data link */
939static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
940{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200941 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200942 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +0200943 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200944
945 /* get release mode */
946 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
947 mode = rllh->data[1] & 1;
948
Harald Welte1f0b8c22011-06-27 10:51:37 +0200949 /* Pull rllh */
950 msgb_pull(msg, msg->tail - msg->l2h);
951
rootaf48bed2011-09-26 11:23:06 +0200952 /* prepare prim */
953 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
954 dp.u.rel_req.mode = mode;
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 channel in idle state */
961static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
962{
963 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
964 void *l1ctx = lc->lapdm_dcch.l1_ctx;
965 struct osmo_phsap_prim pp;
966
967 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
968 PRIM_OP_REQUEST, NULL);
969
970 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +0200971 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200972 return -EINVAL;
973 }
974 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +0200975 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200976 return -EINVAL;
977 }
978 pp.u.rach_req.ra = cch->data[1];
979 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
980 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
981
982 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +0200983 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200984 return -EINVAL;
985 }
986 /* TA = 0 - delay */
987 pp.u.rach_req.ta = 0 - cch->data[5];
988
989 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +0200990 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200991 return -EINVAL;
992 }
993 pp.u.rach_req.tx_power = cch->data[7];
994
995 msgb_free(msg);
996
997 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
998}
999
1000/* L1 confirms channel request */
1001static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1002{
1003 struct abis_rsl_cchan_hdr *ch;
1004 struct gsm_time tm;
1005 struct gsm48_req_ref *ref;
1006
1007 gsm_fn2gsmtime(&tm, frame_nr);
1008
1009 msgb_pull_l2h(msg);
1010 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1011 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1012 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1013 ch->chan_nr = RSL_CHAN_RACH;
1014 ch->data[0] = RSL_IE_REQ_REFERENCE;
1015 ref = (struct gsm48_req_ref *) (ch->data + 1);
1016 ref->t1 = tm.t1;
1017 ref->t2 = tm.t2;
1018 ref->t3_low = tm.t3 & 0x7;
1019 ref->t3_high = tm.t3 >> 3;
1020
1021 return rslms_sendmsg(msg, le);
1022}
1023
Harald Welte1f0b8c22011-06-27 10:51:37 +02001024/* incoming RSLms RLL message from L3 */
1025static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1026{
1027 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1028 int msg_type = rllh->c.msg_type;
1029 uint8_t sapi = rllh->link_id & 7;
1030 struct lapdm_entity *le;
1031 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001032 int rc = 0;
1033
1034 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001035 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001036 return -EINVAL;
1037 }
1038
1039 if (rllh->link_id & 0x40)
1040 le = &lc->lapdm_acch;
1041 else
1042 le = &lc->lapdm_dcch;
1043
1044 /* G.2.1 No action schall be taken on frames containing an unallocated
1045 * SAPI.
1046 */
1047 dl = datalink_for_sapi(le, sapi);
1048 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001049 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001050 return -EINVAL;
1051 }
1052
rootaf48bed2011-09-26 11:23:06 +02001053 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received.\n",
1054 lc->name, rsl_msg_name(msg_type));
Harald Welte1f0b8c22011-06-27 10:51:37 +02001055
rootaf48bed2011-09-26 11:23:06 +02001056 switch (msg_type) {
1057 case RSL_MT_UNIT_DATA_REQ:
1058 rc = rslms_rx_rll_udata_req(msg, dl);
1059 break;
1060 case RSL_MT_EST_REQ:
1061 rc = rslms_rx_rll_est_req(msg, dl);
1062 break;
1063 case RSL_MT_DATA_REQ:
1064 rc = rslms_rx_rll_data_req(msg, dl);
1065 break;
1066 case RSL_MT_SUSP_REQ:
1067 rc = rslms_rx_rll_susp_req(msg, dl);
1068 break;
1069 case RSL_MT_RES_REQ:
1070 rc = rslms_rx_rll_res_req(msg, dl);
1071 break;
1072 case RSL_MT_RECON_REQ:
1073 rc = rslms_rx_rll_res_req(msg, dl);
1074 break;
1075 case RSL_MT_REL_REQ:
1076 rc = rslms_rx_rll_rel_req(msg, dl);
1077 break;
1078 default:
1079 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001080 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001081 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001082 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001083
1084 return rc;
1085}
1086
1087/* incoming RSLms COMMON CHANNEL message from L3 */
1088static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1089{
1090 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1091 int msg_type = cch->c.msg_type;
1092 int rc = 0;
1093
1094 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001095 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001096 return -EINVAL;
1097 }
1098
1099 switch (msg_type) {
1100 case RSL_MT_CHAN_RQD:
1101 /* create and send RACH request */
1102 rc = rslms_rx_chan_rqd(lc, msg);
1103 break;
1104 default:
rootaf48bed2011-09-26 11:23:06 +02001105 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001106 msg_type);
1107 msgb_free(msg);
1108 return 0;
1109 }
1110
1111 return rc;
1112}
1113
Harald Welte6bdf0b12011-08-17 18:22:08 +02001114/*! \brief Receive a RSLms \ref msgb from Layer 3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001115int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1116{
1117 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1118 int rc = 0;
1119
1120 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001121 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001122 return -EINVAL;
1123 }
1124
1125 switch (rslh->msg_discr & 0xfe) {
1126 case ABIS_RSL_MDISC_RLL:
1127 rc = rslms_rx_rll(msg, lc);
1128 break;
1129 case ABIS_RSL_MDISC_COM_CHAN:
1130 rc = rslms_rx_com_chan(msg, lc);
1131 break;
1132 default:
rootaf48bed2011-09-26 11:23:06 +02001133 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001134 "discriminator 0x%02x", rslh->msg_discr);
1135 msgb_free(msg);
1136 return -EINVAL;
1137 }
1138
1139 return rc;
1140}
1141
Harald Welte6bdf0b12011-08-17 18:22:08 +02001142/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001143int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1144{
rootaf48bed2011-09-26 11:23:06 +02001145 int i;
1146 enum lapd_mode lm;
1147
Harald Welte1f0b8c22011-06-27 10:51:37 +02001148 switch (mode) {
1149 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001150 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001151 break;
1152 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001153 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001154 break;
1155 default:
1156 return -EINVAL;
1157 }
1158
rootaf48bed2011-09-26 11:23:06 +02001159 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1160 lapd_set_mode(&le->datalink[i].dl, lm);
1161 }
1162
Harald Welte1f0b8c22011-06-27 10:51:37 +02001163 le->mode = mode;
1164
1165 return 0;
1166}
1167
Harald Welte6bdf0b12011-08-17 18:22:08 +02001168/*! \brief Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001169int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1170{
1171 int rc;
1172
1173 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1174 if (rc < 0)
1175 return rc;
1176
1177 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1178}
1179
Harald Welte6bdf0b12011-08-17 18:22:08 +02001180/*! \brief Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001181void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1182{
1183 lc->lapdm_dcch.l1_prim_cb = cb;
1184 lc->lapdm_acch.l1_prim_cb = cb;
1185 lc->lapdm_dcch.l1_ctx = ctx;
1186 lc->lapdm_acch.l1_ctx = ctx;
1187}
1188
Harald Welte6bdf0b12011-08-17 18:22:08 +02001189/*! \brief Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001190void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1191{
1192 lc->lapdm_dcch.l3_cb = cb;
1193 lc->lapdm_acch.l3_cb = cb;
1194 lc->lapdm_dcch.l3_ctx = ctx;
1195 lc->lapdm_acch.l3_ctx = ctx;
1196}
1197
Harald Welte6bdf0b12011-08-17 18:22:08 +02001198/*! \brief Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001199void lapdm_entity_reset(struct lapdm_entity *le)
1200{
1201 struct lapdm_datalink *dl;
1202 int i;
1203
1204 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1205 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001206 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001207 }
1208}
1209
Harald Welte6bdf0b12011-08-17 18:22:08 +02001210/*! \brief Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001211void lapdm_channel_reset(struct lapdm_channel *lc)
1212{
1213 lapdm_entity_reset(&lc->lapdm_dcch);
1214 lapdm_entity_reset(&lc->lapdm_acch);
1215}
1216
Harald Welte6bdf0b12011-08-17 18:22:08 +02001217/*! \brief Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001218void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1219{
1220 le->flags = flags;
1221}
1222
Harald Welte6bdf0b12011-08-17 18:22:08 +02001223/*! \brief Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001224void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1225{
1226 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1227 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1228}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001229
1230/*! }@ */