blob: 80840293df5ee7351d47d18d22a306d9eae108ff [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file lapdm.c
2 * GSM LAPDm (TS 04.06) implementation. */
3/*
Harald Welte20de6202019-06-02 21:33:38 +02004 * (C) 2010-2019 by Harald Welte <laforge@gnumonks.org>
rootaf48bed2011-09-26 11:23:06 +02005 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
Harald Weltee08da972017-11-13 01:00:26 +09006 * (C) 2014-2016 by sysmocom - s.f.m.c GmbH
Harald Welte1f0b8c22011-06-27 10:51:37 +02007 *
8 * All Rights Reserved
9 *
Harald Weltee08da972017-11-13 01:00:26 +090010 * SPDX-License-Identifier: GPL-2.0+
11 *
Harald Welte1f0b8c22011-06-27 10:51:37 +020012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 */
27
Harald Welte6bdf0b12011-08-17 18:22:08 +020028/*! \addtogroup lapdm
29 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020030 * \file lapdm.c */
Harald Welte6bdf0b12011-08-17 18:22:08 +020031
Harald Welte1f0b8c22011-06-27 10:51:37 +020032#include <stdio.h>
33#include <stdint.h>
34#include <string.h>
35#include <errno.h>
Harald Welte1f0b8c22011-06-27 10:51:37 +020036
37#include <osmocom/core/logging.h>
38#include <osmocom/core/timer.h>
39#include <osmocom/core/msgb.h>
40#include <osmocom/core/utils.h>
41
42#include <osmocom/gsm/tlv.h>
43#include <osmocom/gsm/rsl.h>
44#include <osmocom/gsm/prim.h>
45#include <osmocom/gsm/gsm_utils.h>
46#include <osmocom/gsm/lapdm.h>
47
48#include <osmocom/gsm/protocol/gsm_04_08.h>
49#include <osmocom/gsm/protocol/gsm_08_58.h>
50
Harald Welte1284c3e2018-05-01 18:11:02 +020051#define LAPD_U_SABM 0x7
52
Harald Welte1f0b8c22011-06-27 10:51:37 +020053/* TS 04.06 Figure 4 / Section 3.2 */
54#define LAPDm_LPD_NORMAL 0
55#define LAPDm_LPD_SMSCB 1
56#define LAPDm_SAPI_NORMAL 0
57#define LAPDm_SAPI_SMS 3
58#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
59
rootaf48bed2011-09-26 11:23:06 +020060#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020061#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
62#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
63#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
64
65/* TS 04.06 Table 3 / Section 3.4.3 */
66#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
67#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
68#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
69
70#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
71#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
72#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
73
74#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
75#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
76
77#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
78
79#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
80#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
81
Harald Welte1f0b8c22011-06-27 10:51:37 +020082#define LAPDm_LEN(len) ((len << 2) | 0x1)
83#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020084#define LAPDm_EL 0x1
85
86#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020087
88/* TS 04.06 Section 5.8.3 */
89#define N201_AB_SACCH 18
90#define N201_AB_SDCCH 20
91#define N201_AB_FACCH 20
92#define N201_Bbis 23
93#define N201_Bter_SACCH 21
94#define N201_Bter_SDCCH 23
95#define N201_Bter_FACCH 23
96#define N201_B4 19
97
98/* 5.8.2.1 N200 during establish and release */
99#define N200_EST_REL 5
100/* 5.8.2.1 N200 during timer recovery state */
101#define N200_TR_SACCH 5
102#define N200_TR_SDCCH 23
103#define N200_TR_FACCH_FR 34
104#define N200_TR_EFACCH_FR 48
105#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200106/* FIXME: set N200 depending on chan_nr */
107#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200108
109enum lapdm_format {
110 LAPDm_FMT_A,
111 LAPDm_FMT_B,
112 LAPDm_FMT_Bbis,
113 LAPDm_FMT_Bter,
114 LAPDm_FMT_B4,
115};
116
Maxadef12a2016-05-25 15:25:02 +0200117const struct value_string osmo_ph_prim_names[] = {
118 { PRIM_PH_DATA, "PH-DATA" },
119 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
120 { PRIM_PH_CONN, "PH-CONNECT" },
121 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
122 { PRIM_PH_RTS, "PH-RTS" },
123 { PRIM_MPH_INFO, "MPH-INFO" },
124 { PRIM_TCH, "TCH" },
125 { PRIM_TCH_RTS, "TCH-RTS" },
126 { 0, NULL }
127};
128
rootaf48bed2011-09-26 11:23:06 +0200129static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
130static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
131 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100132static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200133
134static void lapdm_dl_init(struct lapdm_datalink *dl,
Harald Welte20de6202019-06-02 21:33:38 +0200135 struct lapdm_entity *entity, int t200_ms, uint32_t n200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200136{
137 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200138 dl->entity = entity;
Harald Welte2e78f902019-06-01 11:44:27 +0200139 lapd_dl_init(&dl->dl, 1, 8, 251); /* Section 5.8.5 of TS 04.06 */
rootaf48bed2011-09-26 11:23:06 +0200140 dl->dl.reestablish = 0; /* GSM uses no reestablish */
141 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
142 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100143 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200144 dl->dl.n200_est_rel = N200_EST_REL;
Harald Welte20de6202019-06-02 21:33:38 +0200145 dl->dl.n200 = n200;
rootaf48bed2011-09-26 11:23:06 +0200146 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Harald Welte20de6202019-06-02 21:33:38 +0200147 dl->dl.t200_sec = t200_ms / 1000; dl->dl.t200_usec = (t200_ms % 1000) * 1000;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200148}
149
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200150/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200151 * \param[in] le LAPDm entity
152 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200153 * \param[in] t200 T200 re-transmission timer for all SAPIs in seconds
154 *
155 * Don't use this function; It doesn't support different T200 values per API
156 * and doesn't permit the caller to specify the N200 counter, both of which
157 * are required by GSM specs and supported by lapdm_entity_init2().
Harald Welte6bdf0b12011-08-17 18:22:08 +0200158 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100159void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200160{
Harald Welte20de6202019-06-02 21:33:38 +0200161 /* convert from single full-second value to per-SAPI milli-second value */
162 int t200_ms_sapi_arr[_NR_DL_SAPI];
163 int i;
164
165 for (i = 0; i < ARRAY_SIZE(t200_ms_sapi_arr); i++)
166 t200_ms_sapi_arr[i] = t200 * 1000;
167
168 return lapdm_entity_init2(le, mode, t200_ms_sapi_arr, N200);
169}
170
171/*! initialize a LAPDm entity and all datalinks inside
172 * \param[in] le LAPDm entity
173 * \param[in] mode lapdm_mode (BTS/MS)
174 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
175 * \param[in] n200 N200 re-transmisison count
176 */
177void lapdm_entity_init2(struct lapdm_entity *le, enum lapdm_mode mode,
178 const int *t200_ms, int n200)
179{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200180 unsigned int i;
181
182 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
Harald Welte20de6202019-06-02 21:33:38 +0200183 lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200184
185 lapdm_entity_set_mode(le, mode);
186}
187
Harald Welte20de6202019-06-02 21:33:38 +0200188static int get_n200_dcch(enum gsm_chan_t chan_t)
189{
190 switch (chan_t) {
191 case GSM_LCHAN_SDCCH:
192 return N200_TR_SDCCH;
193 case GSM_LCHAN_TCH_F:
194 return N200_TR_FACCH_FR;
195 case GSM_LCHAN_TCH_H:
196 return N200_TR_FACCH_HR;
197 default:
198 return -1;
199 }
200}
201
202/*! initialize a LAPDm channel and all its channels
203 * \param[in] lc lapdm_channel to be initialized
204 * \param[in] mode lapdm_mode (BTS/MS)
205 *
206 * Don't use this function; It doesn't support different T200 values per API
207 * and doesn't set the correct N200 counter, both of which
208 * are required by GSM specs and supported by lapdm_channel_init2().
209 */
210void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
211{
212 /* emulate old backwards-compatible behavior with 1s/2s */
213 const int t200_ms_dcch[_NR_DL_SAPI] = { 1000, 1000 };
214 const int t200_ms_acch[_NR_DL_SAPI] = { 2000, 2000 };
215
216 lapdm_channel_init2(lc, mode, t200_ms_dcch, t200_ms_acch, GSM_LCHAN_SDCCH);
217}
218
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200219/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200220 * \param[in] lc \ref lapdm_channel to be initialized
221 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200222 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
223 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
224 * \param[in] chan_t GSM channel type (to correctly set N200)
Harald Welte6bdf0b12011-08-17 18:22:08 +0200225 */
Harald Welte20de6202019-06-02 21:33:38 +0200226int lapdm_channel_init2(struct lapdm_channel *lc, enum lapdm_mode mode,
227 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200228{
Harald Welte20de6202019-06-02 21:33:38 +0200229 int n200_dcch = get_n200_dcch(chan_t);
230 if (n200_dcch < 0)
231 return -EINVAL;
232
233 lapdm_entity_init2(&lc->lapdm_acch, mode, t200_ms_acch, N200_TR_SACCH);
Harald Welte3e8c5202018-05-04 20:58:48 +0200234 lc->lapdm_acch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200235
236 lapdm_entity_init2(&lc->lapdm_dcch, mode, t200_ms_dcch, n200_dcch);
Harald Welte3e8c5202018-05-04 20:58:48 +0200237 lc->lapdm_dcch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200238
239 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200240}
241
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200242/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200243void lapdm_entity_exit(struct lapdm_entity *le)
244{
245 unsigned int i;
246 struct lapdm_datalink *dl;
247
248 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
249 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200250 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200251 }
252}
253
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200254/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200255 *
256 * A convenience wrapper calling \ref lapdm_entity_exit on both
257 * entities inside the \ref lapdm_channel
258 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200259void lapdm_channel_exit(struct lapdm_channel *lc)
260{
261 lapdm_entity_exit(&lc->lapdm_acch);
262 lapdm_entity_exit(&lc->lapdm_dcch);
263}
264
Daniel Willmann55405fb2014-03-26 13:45:17 +0100265struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200266{
267 switch (sapi) {
268 case LAPDm_SAPI_NORMAL:
269 return &le->datalink[0];
270 case LAPDm_SAPI_SMS:
271 return &le->datalink[1];
272 default:
273 return NULL;
274 }
275}
276
Harald Welte1f0b8c22011-06-27 10:51:37 +0200277/* Append padding (if required) */
278static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
279{
280 int pad_len = n201 - msgb_l2len(msg);
281 uint8_t *data;
282
283 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200284 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200285 "cannot pad message that is already too big!\n");
286 return;
287 }
288
289 data = msgb_put(msg, pad_len);
290 memset(data, 0x2B, pad_len);
291}
292
293/* input function that L2 calls when sending messages up to L3 */
294static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
295{
296 if (!le->l3_cb) {
297 msgb_free(msg);
298 return -EIO;
299 }
300
301 /* call the layer2 message handler that is registered */
302 return le->l3_cb(msg, le, le->l3_ctx);
303}
304
305/* write a frame into the tx queue */
306static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200307 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200308{
309 struct lapdm_entity *le = dl->entity;
310 struct osmo_phsap_prim pp;
311
312 /* if there is a pending message, queue it */
313 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200314 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200315 *msgb_push(msg, 1) = link_id;
316 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200317 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200318 return -EBUSY;
319 }
320
321 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
322 PRIM_OP_REQUEST, msg);
323 pp.u.data.chan_nr = chan_nr;
324 pp.u.data.link_id = link_id;
325
326 /* send the frame now */
327 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200328 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200329
330 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
331}
332
333static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
334{
335 struct lapdm_datalink *dl;
336 int last = le->last_tx_dequeue;
337 int i = last, n = ARRAY_SIZE(le->datalink);
338 struct msgb *msg = NULL;
339
340 /* round-robin dequeue */
341 do {
342 /* next */
343 i = (i + 1) % n;
344 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200345 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200346 break;
347 } while (i != last);
348
349 if (msg) {
350 /* Set last dequeue position */
351 le->last_tx_dequeue = i;
352 }
353
354 return msg;
355}
356
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200357/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200358 * a osmo_phsap_prim */
359int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
360{
361 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200362 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200363
364 msg = tx_dequeue_msgb(le);
365 if (!msg)
366 return -ENODEV;
367
368 /* if we have a message, send PH-DATA.req */
369 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
370 PRIM_OP_REQUEST, msg);
371
372 /* Pull chan_nr and link_id */
373 pp->u.data.chan_nr = *msg->data;
374 msgb_pull(msg, 1);
375 pp->u.data.link_id = *msg->data;
376 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200377 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200378 msgb_pull(msg, 1);
379
380 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200381 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200382
383 return 0;
384}
385
386/* get next frame from the tx queue. because the ms has multiple datalinks,
387 * each datalink's queue is read round-robin.
388 */
389static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
390{
391 struct osmo_phsap_prim pp;
392
393 /* we may send again */
394 le->tx_pending = 0;
395
396 /* free confirm message */
397 if (msg)
398 msgb_free(msg);
399
400 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
401 /* no message in all queues */
402
403 /* If user didn't request PH-EMPTY_FRAME.req, abort */
404 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
405 return 0;
406
407 /* otherwise, send PH-EMPTY_FRAME.req */
408 osmo_prim_init(&pp.oph, SAP_GSM_PH,
409 PRIM_PH_EMPTY_FRAME,
410 PRIM_OP_REQUEST, NULL);
411 } else {
412 le->tx_pending = 1;
413 }
414
415 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
416}
417
Harald Welte542301b2018-04-19 16:11:14 +0200418/* Is a given msg_type "transparent" as per TS 48.058 Section 8.1 */
419static int rsl_is_transparent(uint8_t msg_type)
420{
421 switch (msg_type) {
422 case RSL_MT_DATA_IND:
423 case RSL_MT_UNIT_DATA_IND:
424 return 1;
425 case RSL_MT_DATA_REQ:
426 case RSL_MT_UNIT_DATA_REQ:
427 return 1;
428 default:
429 return 0;
430 }
431}
432
Harald Welte1f0b8c22011-06-27 10:51:37 +0200433/* Create RSLms various RSLms messages */
434static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
435 struct msgb *msg)
436{
Harald Welte542301b2018-04-19 16:11:14 +0200437 int transparent = rsl_is_transparent(msg_type);
438
Harald Welte1f0b8c22011-06-27 10:51:37 +0200439 /* Add the RSL + RLL header */
Harald Welte542301b2018-04-19 16:11:14 +0200440 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200441
442 /* send off the RSLms message to L3 */
443 return rslms_sendmsg(msg, mctx->dl->entity);
444}
445
446/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
447static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
448{
449 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200450
451 /* Add the RSL + RLL header */
452 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welted977f5f2018-05-08 21:53:28 +0200453
Harald Weltef1bdf782018-05-08 22:03:20 +0200454 /* Add two non-standard IEs carrying MS power and TA values for B4 (SACCH) */
455 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
456 msgb_tv_push(msg, RSL_IE_MS_POWER, mctx->tx_power_ind);
457 msgb_tv_push(msg, RSL_IE_TIMING_ADVANCE, mctx->ta_ind);
458 }
Harald Welted977f5f2018-05-08 21:53:28 +0200459
Harald Welte1f0b8c22011-06-27 10:51:37 +0200460 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
461 mctx->link_id, 1);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100462
Harald Welte1f0b8c22011-06-27 10:51:37 +0200463 return rslms_sendmsg(msg, mctx->dl->entity);
464}
465
466static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
467{
468 struct msgb *msg;
Harald Welte542301b2018-04-19 16:11:14 +0200469 int transparent = rsl_is_transparent(msg_type);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200470
Harald Welte542301b2018-04-19 16:11:14 +0200471 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200472
473 /* send off the RSLms message to L3 */
474 return rslms_sendmsg(msg, mctx->dl->entity);
475}
476
477static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
478{
479 struct msgb *msg;
480
rootaf48bed2011-09-26 11:23:06 +0200481 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte542301b2018-04-19 16:11:14 +0200482 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200483 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
484 return rslms_sendmsg(msg, mctx->dl->entity);
485}
486
rootaf48bed2011-09-26 11:23:06 +0200487/* DLSAP L2 -> L3 (RSLms) */
488static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
489 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200490{
rootaf48bed2011-09-26 11:23:06 +0200491 struct lapd_datalink *dl = lctx->dl;
492 struct lapdm_datalink *mdl =
493 container_of(dl, struct lapdm_datalink, dl);
494 struct lapdm_msg_ctx *mctx = &mdl->mctx;
495 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200496
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200497 switch (OSMO_PRIM_HDR(&dp->oph)) {
498 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
499 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200500 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200501 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
502 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200503 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200504 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
505 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200506 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200507 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
508 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
509 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
510 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200511 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200512 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
513 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200514 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200515 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
516 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200517 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200518 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
519 rsl_rll_error(dp->u.error_ind.cause, mctx);
520 if (dp->oph.msg)
521 msgb_free(dp->oph.msg);
522 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200523 }
rootaf48bed2011-09-26 11:23:06 +0200524
525 if (!rll_msg) {
526 LOGP(DLLAPD, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
527 "fix!\n", dp->oph.primitive, dp->oph.operation);
528 return -EINVAL;
529 }
530
531 if (!dp->oph.msg)
532 return send_rll_simple(rll_msg, mctx);
533
534 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200535}
536
rootaf48bed2011-09-26 11:23:06 +0200537/* send a data frame to layer 1 */
538static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200539{
rootaf48bed2011-09-26 11:23:06 +0200540 uint8_t l3_len = msg->tail - msg->data;
541 struct lapd_datalink *dl = lctx->dl;
542 struct lapdm_datalink *mdl =
543 container_of(dl, struct lapdm_datalink, dl);
544 struct lapdm_msg_ctx *mctx = &mdl->mctx;
545 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200546
rootaf48bed2011-09-26 11:23:06 +0200547 /* prepend l2 header */
548 msg->l2h = msgb_push(msg, 3);
549 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
550 /* EA is set here too */
551 switch (format) {
552 case LAPD_FORM_I:
553 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
554 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200555 break;
rootaf48bed2011-09-26 11:23:06 +0200556 case LAPD_FORM_S:
557 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200558 break;
rootaf48bed2011-09-26 11:23:06 +0200559 case LAPD_FORM_U:
560 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200561 break;
562 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200563 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200564 return -EINVAL;
565 }
rootaf48bed2011-09-26 11:23:06 +0200566 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
567 if (lctx->more)
568 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200569
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100570 /* add ACCH header with last indicated tx-power and TA */
571 if ((mctx->link_id & 0x40)) {
572 struct lapdm_entity *le = mdl->entity;
573
574 msg->l2h = msgb_push(msg, 2);
575 msg->l2h[0] = le->tx_power;
576 msg->l2h[1] = le->ta;
577 }
578
rootaf48bed2011-09-26 11:23:06 +0200579 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
580 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200581}
582
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100583static int update_pending_frames(struct lapd_msg_ctx *lctx)
584{
585 struct lapd_datalink *dl = lctx->dl;
586 struct msgb *msg;
587 int rc = -1;
588
589 llist_for_each_entry(msg, &dl->tx_queue, list) {
590 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
591 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
592 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
593 rc = 0;
594 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
595 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
596 }
597 }
598
599 return rc;
600}
601
Harald Welte1284c3e2018-05-01 18:11:02 +0200602/* determine if receiving a given LAPDm message is not permitted */
603static int lapdm_rx_not_permitted(const struct lapdm_entity *le,
604 const struct lapd_msg_ctx *lctx)
605{
606 /* we currently only implement SABM related checks here */
607 if (lctx->format != LAPD_FORM_U || lctx->s_u != LAPD_U_SABM)
608 return 0;
609
610 if (le->mode == LAPDM_MODE_BTS) {
611 if (le == &le->lapdm_ch->lapdm_acch) {
612 /* no contention resolution on SACCH */
613 if (lctx->length > 0)
614 return RLL_CAUSE_SABM_INFO_NOTALL;
615 } else {
616 switch (lctx->sapi) {
Harald Welte1284c3e2018-05-01 18:11:02 +0200617 case 3:
618 /* SAPI3 doesn't support contention resolution */
619 if (lctx->length > 0)
620 return RLL_CAUSE_SABM_INFO_NOTALL;
621 break;
Harald Welteb82a4072018-05-09 16:31:16 +0200622 default:
623 break;
Harald Welte1284c3e2018-05-01 18:11:02 +0200624 }
625 }
626 } else if (le->mode == LAPDM_MODE_MS) {
627 /* contention resolution (L3 present) is only sent by MS, but
628 * never received by it */
629 if (lctx->length > 0)
630 return RLL_CAUSE_SABM_INFO_NOTALL;
631 }
632 return 0;
633}
634
Harald Welte1f0b8c22011-06-27 10:51:37 +0200635/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200636static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
637 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200638{
639 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200640 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200641 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200642 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200643 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200644 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200645
646 /* when we reach here, we have a msgb with l2h pointing to the raw
647 * 23byte mac block. The l1h has already been purged. */
648
rootaf48bed2011-09-26 11:23:06 +0200649 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200650 mctx.chan_nr = chan_nr;
651 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200652
Harald Welte1f0b8c22011-06-27 10:51:37 +0200653 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
654 if (cbits == 0x10 || cbits == 0x12) {
655 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
656 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200657 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200658 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200659 } else {
660 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200661 /* It was received from network on SACCH */
662
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100663 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
664 if (le->mode == LAPDM_MODE_MS
665 && LAPDm_CTRL_is_U(msg->l2h[3])
666 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200667 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200668 n201 = N201_B4;
669 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200670 } else {
671 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200672 n201 = N201_AB_SACCH;
673 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200674 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200675 /* SACCH frames have a two-byte L1 header that
676 * OsmocomBB L1 doesn't strip */
677 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
678 mctx.ta_ind = msg->l2h[1];
679 msgb_pull(msg, 2);
680 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200681 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200682 } else {
683 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200684 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100685 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200686 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200687 }
688 }
689
Daniel Willmann55405fb2014-03-26 13:45:17 +0100690 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200691 /* G.2.1 No action on frames containing an unallocated SAPI. */
692 if (!mctx.dl) {
rootaf48bed2011-09-26 11:23:06 +0200693 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported "
Harald Welte64207742011-06-27 23:32:14 +0200694 "SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200695 msgb_free(msg);
696 return -EIO;
697 }
698
Harald Welte1f0b8c22011-06-27 10:51:37 +0200699 switch (mctx.lapdm_fmt) {
700 case LAPDm_FMT_A:
701 case LAPDm_FMT_B:
702 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200703 lctx.dl = &mctx.dl->dl;
704 /* obtain SAPI from address field */
705 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
706 /* G.2.3 EA bit set to "0" is not allowed in GSM */
707 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
708 LOGP(DLLAPD, LOGL_NOTICE, "EA bit 0 is not allowed in "
709 "GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200710 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200711 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200712 return -EINVAL;
713 }
rootaf48bed2011-09-26 11:23:06 +0200714 /* adress field */
715 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
716 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
717 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
718 /* command field */
719 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
720 lctx.format = LAPD_FORM_I;
721 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
722 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
723 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
724 lctx.format = LAPD_FORM_S;
725 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
726 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
727 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
728 lctx.format = LAPD_FORM_U;
729 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
730 } else
731 lctx.format = LAPD_FORM_UKN;
732 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
733 if (lctx.sapi != LAPDm_SAPI_NORMAL
734 && lctx.sapi != LAPDm_SAPI_SMS
735 && lctx.format == LAPD_FORM_U
736 && lctx.s_u == LAPDm_U_UI) {
737 /* 5.3.3 UI frames with invalid SAPI values shall be
738 * discarded
739 */
740 LOGP(DLLAPD, LOGL_INFO, "sapi=%u (discarding)\n",
741 lctx.sapi);
742 msgb_free(msg);
743 return 0;
744 }
745 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
746 lctx.n201 = n201;
747 lctx.length = n201;
748 lctx.more = 0;
749 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100750 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200751 } else {
752 /* length field */
753 if (!(msg->l2h[2] & LAPDm_EL)) {
754 /* G.4.1 If the EL bit is set to "0", an
755 * MDL-ERROR-INDICATION primitive with cause
756 * "frame not implemented" is sent to the
757 * mobile management entity. */
758 LOGP(DLLAPD, LOGL_NOTICE, "we don't support "
759 "multi-octet length\n");
760 msgb_free(msg);
761 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
762 return -EINVAL;
763 }
764 lctx.n201 = n201;
765 lctx.length = msg->l2h[2] >> 2;
766 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
767 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100768 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200769 }
770 /* store context for messages from lapd */
771 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200772 rc =lapdm_rx_not_permitted(le, &lctx);
773 if (rc > 0) {
Pau Espin Pedrol1eb270b2018-06-18 19:23:11 +0200774 LOGP(DLLAPD, LOGL_NOTICE, "received message not permitted\n");
Harald Welte1284c3e2018-05-01 18:11:02 +0200775 msgb_free(msg);
776 rsl_rll_error(rc, &mctx);
777 return -EINVAL;
778 }
rootaf48bed2011-09-26 11:23:06 +0200779 /* send to LAPD */
780 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200781 break;
782 case LAPDm_FMT_Bter:
783 /* FIXME */
784 msgb_free(msg);
785 break;
786 case LAPDm_FMT_Bbis:
787 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200788 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200789 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100790 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200791 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
792 break;
793 default:
794 msgb_free(msg);
795 }
796
797 return rc;
798}
799
800/* input into layer2 (from layer 1) */
801static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
802{
803 struct abis_rsl_cchan_hdr *ch;
804 struct gsm48_req_ref req_ref;
805 struct gsm_time gt;
806 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
807
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200808 if (!msg)
809 return -ENOMEM;
810
Harald Welte1f0b8c22011-06-27 10:51:37 +0200811 msg->l2h = msgb_push(msg, sizeof(*ch));
812 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
813 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
814 ch->chan_nr = RSL_CHAN_RACH;
815
816 /* generate a RSL CHANNEL REQUIRED message */
817 gsm_fn2gsmtime(&gt, fn);
818 req_ref.ra = ra;
819 req_ref.t1 = gt.t1; /* FIXME: modulo? */
820 req_ref.t2 = gt.t2;
821 req_ref.t3_low = gt.t3 & 7;
822 req_ref.t3_high = gt.t3 >> 3;
823
824 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
825 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
826
827 return rslms_sendmsg(msg, le);
828}
829
830static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
831
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200832/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200833int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
834{
835 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
836 int rc = 0;
837
838 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200839 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200840 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100841 rc = -ENODEV;
842 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200843 }
844
845 switch (oph->primitive) {
846 case PRIM_PH_DATA:
847 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200848 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200849 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100850 rc = -ENODEV;
851 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200852 }
853 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
854 pp->u.data.link_id);
855 break;
856 case PRIM_PH_RTS:
857 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200858 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200859 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100860 rc = -ENODEV;
861 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200862 }
863 rc = l2_ph_data_conf(oph->msg, le);
864 break;
865 case PRIM_PH_RACH:
866 switch (oph->operation) {
867 case PRIM_OP_INDICATION:
868 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
869 pp->u.rach_ind.acc_delay);
870 break;
871 case PRIM_OP_CONFIRM:
872 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
873 break;
874 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100875 rc = -EIO;
876 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200877 }
878 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100879 default:
880 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
881 oph->primitive);
882 rc = -EINVAL;
883 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200884 }
885
886 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100887
888free:
889 msgb_free(oph->msg);
890 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200891}
892
893
894/* L3 -> L2 / RSLMS -> LAPDm */
895
rootaf48bed2011-09-26 11:23:06 +0200896/* Set LAPDm context for established connection */
897static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
898 uint8_t link_id, int n201, uint8_t sapi)
899{
900 memset(&dl->mctx, 0, sizeof(dl->mctx));
901 dl->mctx.dl = dl;
902 dl->mctx.chan_nr = chan_nr;
903 dl->mctx.link_id = link_id;
904 dl->dl.lctx.dl = &dl->dl;
905 dl->dl.lctx.n201 = n201;
906 dl->dl.lctx.sapi = sapi;
907
908 return 0;
909}
910
Harald Welte1f0b8c22011-06-27 10:51:37 +0200911/* L3 requests establishment of data link */
912static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
913{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200914 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
915 uint8_t chan_nr = rllh->chan_nr;
916 uint8_t link_id = rllh->link_id;
917 uint8_t sapi = rllh->link_id & 7;
918 struct tlv_parsed tv;
919 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100920 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200921 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200922
rootaf48bed2011-09-26 11:23:06 +0200923 /* Set LAPDm context for established connection */
924 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200925
rootaf48bed2011-09-26 11:23:06 +0200926 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200927 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200928 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200929 /* contention resolution establishment procedure */
930 if (sapi != 0) {
931 /* According to clause 6, the contention resolution
932 * procedure is only permitted with SAPI value 0 */
rootaf48bed2011-09-26 11:23:06 +0200933 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200934 "resolution (discarding)\n");
935 msgb_free(msg);
936 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
937 }
938 /* transmit a SABM command with the P bit set to "1". The SABM
939 * command shall contain the layer 3 message unit */
940 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200941 } else {
942 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200943 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200944 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200945 }
946
947 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100948 if (length > n201) {
rootaf48bed2011-09-26 11:23:06 +0200949 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100950 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200951 msgb_free(msg);
952 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
953 }
954
rootaf48bed2011-09-26 11:23:06 +0200955 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100956 msgb_pull_to_l3(msg);
957 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200958
rootaf48bed2011-09-26 11:23:06 +0200959 /* prepare prim */
960 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200961
rootaf48bed2011-09-26 11:23:06 +0200962 /* send to L2 */
963 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200964}
965
966/* L3 requests transfer of unnumbered information */
967static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
968{
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100969 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200970 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
971 uint8_t chan_nr = rllh->chan_nr;
972 uint8_t link_id = rllh->link_id;
973 uint8_t sapi = link_id & 7;
974 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +0100975 int length, ui_bts;
976
977 if (!le) {
978 LOGP(DLLAPD, LOGL_ERROR, "lapdm_datalink without entity error\n");
979 msgb_free(msg);
980 return -EMLINK;
981 }
982 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200983
984 /* check if the layer3 message length exceeds N201 */
985
986 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
987
988 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100989 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200990 }
991 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100992 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200993 }
994 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200995 LOGP(DLLAPD, LOGL_ERROR, "unit data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +0200996 "error\n");
997 msgb_free(msg);
998 return -EINVAL;
999 }
rootaf48bed2011-09-26 11:23:06 +02001000 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001001 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1002 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +02001003 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
rootaf48bed2011-09-26 11:23:06 +02001004 LOGP(DLLAPD, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +02001005 "(discarding)\n", length,
1006 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001007 msgb_free(msg);
1008 return -EIO;
1009 }
1010
rootaf48bed2011-09-26 11:23:06 +02001011 LOGP(DLLAPD, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n",
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001012 le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001013
rootaf48bed2011-09-26 11:23:06 +02001014 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001015 msgb_pull_to_l3(msg);
1016 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001017
1018 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +02001019 msg->l2h = msgb_push(msg, 2 + !ui_bts);
1020 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
1021 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001022 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +02001023 msg->l2h[2] = LAPDm_LEN(length);
1024 if (link_id & 0x40) {
1025 msg->l2h = msgb_push(msg, 2);
1026 msg->l2h[0] = le->tx_power;
1027 msg->l2h[1] = le->ta;
1028 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001029
1030 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +02001031 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001032}
1033
1034/* L3 requests transfer of acknowledged information */
1035static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1036{
1037 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1038 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +02001039 int length;
1040 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001041
1042 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1043 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +02001044 LOGP(DLLAPD, LOGL_ERROR, "data request without message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001045 "error\n");
1046 msgb_free(msg);
1047 return -EINVAL;
1048 }
rootaf48bed2011-09-26 11:23:06 +02001049 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
1050 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001051
rootaf48bed2011-09-26 11:23:06 +02001052 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001053 msgb_pull_to_l3(msg);
1054 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001055
rootaf48bed2011-09-26 11:23:06 +02001056 /* prepare prim */
1057 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001058
rootaf48bed2011-09-26 11:23:06 +02001059 /* send to L2 */
1060 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001061}
1062
1063/* L3 requests suspension of data link */
1064static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1065{
1066 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1067 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001068 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001069
1070 if (sapi != 0) {
rootaf48bed2011-09-26 11:23:06 +02001071 LOGP(DLLAPD, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001072 msgb_free(msg);
1073 return -EINVAL;
1074 }
1075
rootaf48bed2011-09-26 11:23:06 +02001076 /* prepare prim */
1077 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001078
rootaf48bed2011-09-26 11:23:06 +02001079 /* send to L2 */
1080 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001081}
1082
1083/* L3 requests resume of data link */
1084static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1085{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001086 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001087 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001088 uint8_t chan_nr = rllh->chan_nr;
1089 uint8_t link_id = rllh->link_id;
1090 uint8_t sapi = rllh->link_id & 7;
1091 struct tlv_parsed tv;
1092 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001093 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001094 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001095
rootaf48bed2011-09-26 11:23:06 +02001096 /* Set LAPDm context for established connection */
1097 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001098
1099 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1100 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +02001101 LOGP(DLLAPD, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001102 msgb_free(msg);
1103 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1104 }
rootaf48bed2011-09-26 11:23:06 +02001105 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001106 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1107
rootaf48bed2011-09-26 11:23:06 +02001108 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001109 msgb_pull_to_l3(msg);
1110 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001111
rootaf48bed2011-09-26 11:23:06 +02001112 /* prepare prim */
1113 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1114 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001115
rootaf48bed2011-09-26 11:23:06 +02001116 /* send to L2 */
1117 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001118}
1119
1120/* L3 requests release of data link */
1121static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1122{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001123 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001124 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001125 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001126
1127 /* get release mode */
1128 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1129 mode = rllh->data[1] & 1;
1130
Harald Welte1f0b8c22011-06-27 10:51:37 +02001131 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001132 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001133
1134 /* 04.06 3.8.3: No information field is permitted with the DISC
1135 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001136 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001137
rootaf48bed2011-09-26 11:23:06 +02001138 /* prepare prim */
1139 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1140 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001141
rootaf48bed2011-09-26 11:23:06 +02001142 /* send to L2 */
1143 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001144}
1145
1146/* L3 requests channel in idle state */
1147static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1148{
1149 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1150 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1151 struct osmo_phsap_prim pp;
1152
1153 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1154 PRIM_OP_REQUEST, NULL);
1155
1156 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001157 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001158 return -EINVAL;
1159 }
1160 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001161 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001162 return -EINVAL;
1163 }
1164 pp.u.rach_req.ra = cch->data[1];
1165 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1166 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1167
1168 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001169 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001170 return -EINVAL;
1171 }
1172 /* TA = 0 - delay */
1173 pp.u.rach_req.ta = 0 - cch->data[5];
1174
1175 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001176 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001177 return -EINVAL;
1178 }
1179 pp.u.rach_req.tx_power = cch->data[7];
1180
1181 msgb_free(msg);
1182
1183 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1184}
1185
1186/* L1 confirms channel request */
1187static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1188{
1189 struct abis_rsl_cchan_hdr *ch;
1190 struct gsm_time tm;
1191 struct gsm48_req_ref *ref;
1192
1193 gsm_fn2gsmtime(&tm, frame_nr);
1194
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001195 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001196 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1197 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1198 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1199 ch->chan_nr = RSL_CHAN_RACH;
1200 ch->data[0] = RSL_IE_REQ_REFERENCE;
1201 ref = (struct gsm48_req_ref *) (ch->data + 1);
1202 ref->t1 = tm.t1;
1203 ref->t2 = tm.t2;
1204 ref->t3_low = tm.t3 & 0x7;
1205 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001206
Harald Welte1f0b8c22011-06-27 10:51:37 +02001207 return rslms_sendmsg(msg, le);
1208}
1209
Harald Welte1f0b8c22011-06-27 10:51:37 +02001210/* incoming RSLms RLL message from L3 */
1211static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1212{
1213 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1214 int msg_type = rllh->c.msg_type;
1215 uint8_t sapi = rllh->link_id & 7;
1216 struct lapdm_entity *le;
1217 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001218 int rc = 0;
1219
1220 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001221 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001222 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001223 return -EINVAL;
1224 }
1225
1226 if (rllh->link_id & 0x40)
1227 le = &lc->lapdm_acch;
1228 else
1229 le = &lc->lapdm_dcch;
1230
Harald Welte1a87c1b2015-12-14 15:26:07 +01001231 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1232 * BTS side */
1233 if (le->mode == LAPDM_MODE_BTS) {
1234 switch (msg_type) {
1235 case RSL_MT_SUSP_REQ:
1236 case RSL_MT_RES_REQ:
1237 case RSL_MT_RECON_REQ:
1238 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' unsupported in BTS side LAPDm\n",
1239 lc->name, rsl_msg_name(msg_type));
1240 msgb_free(msg);
1241 return -EINVAL;
1242 break;
1243 default:
1244 break;
1245 }
1246 }
1247
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001248 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001249 * SAPI.
1250 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001251 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001252 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001253 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001254 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001255 return -EINVAL;
1256 }
1257
Daniel Willmanne5233922012-12-25 23:15:50 +01001258 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001259 case RSL_MT_DATA_REQ:
1260 case RSL_MT_SUSP_REQ:
1261 case RSL_MT_REL_REQ:
1262 /* This is triggered in abnormal error conditions where
1263 * set_lapdm_context() was not called for the channel earlier. */
1264 if (!dl->dl.lctx.dl) {
1265 LOGP(DLLAPD, LOGL_NOTICE, "(%p) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
1266 lc->name, rsl_msg_name(msg_type), sapi);
1267 msgb_free(msg);
1268 return -EINVAL;
1269 }
1270 break;
1271 default:
1272 LOGP(DLLAPD, LOGL_INFO, "(%p) RLL Message '%s' received. (sapi %d)\n",
1273 lc->name, rsl_msg_name(msg_type), sapi);
1274 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001275
rootaf48bed2011-09-26 11:23:06 +02001276 switch (msg_type) {
1277 case RSL_MT_UNIT_DATA_REQ:
1278 rc = rslms_rx_rll_udata_req(msg, dl);
1279 break;
1280 case RSL_MT_EST_REQ:
1281 rc = rslms_rx_rll_est_req(msg, dl);
1282 break;
1283 case RSL_MT_DATA_REQ:
1284 rc = rslms_rx_rll_data_req(msg, dl);
1285 break;
1286 case RSL_MT_SUSP_REQ:
1287 rc = rslms_rx_rll_susp_req(msg, dl);
1288 break;
1289 case RSL_MT_RES_REQ:
1290 rc = rslms_rx_rll_res_req(msg, dl);
1291 break;
1292 case RSL_MT_RECON_REQ:
1293 rc = rslms_rx_rll_res_req(msg, dl);
1294 break;
1295 case RSL_MT_REL_REQ:
1296 rc = rslms_rx_rll_rel_req(msg, dl);
1297 break;
1298 default:
1299 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001300 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001301 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001302 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001303
1304 return rc;
1305}
1306
1307/* incoming RSLms COMMON CHANNEL message from L3 */
1308static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1309{
1310 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1311 int msg_type = cch->c.msg_type;
1312 int rc = 0;
1313
1314 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001315 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001316 return -EINVAL;
1317 }
1318
1319 switch (msg_type) {
1320 case RSL_MT_CHAN_RQD:
1321 /* create and send RACH request */
1322 rc = rslms_rx_chan_rqd(lc, msg);
1323 break;
1324 default:
rootaf48bed2011-09-26 11:23:06 +02001325 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001326 msg_type);
1327 msgb_free(msg);
1328 return 0;
1329 }
1330
1331 return rc;
1332}
1333
Harald Welte7023aa02019-05-19 12:17:06 +02001334/*! Receive a RSLms \ref msgb from Layer 3. 'msg' ownership is transferred,
1335 * i.e. caller must not free it */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001336int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1337{
1338 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1339 int rc = 0;
1340
1341 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001342 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte7023aa02019-05-19 12:17:06 +02001343 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001344 return -EINVAL;
1345 }
1346
1347 switch (rslh->msg_discr & 0xfe) {
1348 case ABIS_RSL_MDISC_RLL:
1349 rc = rslms_rx_rll(msg, lc);
1350 break;
1351 case ABIS_RSL_MDISC_COM_CHAN:
1352 rc = rslms_rx_com_chan(msg, lc);
1353 break;
1354 default:
rootaf48bed2011-09-26 11:23:06 +02001355 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001356 "discriminator 0x%02x", rslh->msg_discr);
1357 msgb_free(msg);
1358 return -EINVAL;
1359 }
1360
1361 return rc;
1362}
1363
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001364/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001365int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1366{
rootaf48bed2011-09-26 11:23:06 +02001367 int i;
1368 enum lapd_mode lm;
1369
Harald Welte1f0b8c22011-06-27 10:51:37 +02001370 switch (mode) {
1371 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001372 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001373 break;
1374 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001375 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001376 break;
1377 default:
1378 return -EINVAL;
1379 }
1380
rootaf48bed2011-09-26 11:23:06 +02001381 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1382 lapd_set_mode(&le->datalink[i].dl, lm);
1383 }
1384
Harald Welte1f0b8c22011-06-27 10:51:37 +02001385 le->mode = mode;
1386
1387 return 0;
1388}
1389
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001390/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001391int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1392{
1393 int rc;
1394
1395 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1396 if (rc < 0)
1397 return rc;
1398
1399 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1400}
1401
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001402/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001403void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1404{
1405 lc->lapdm_dcch.l1_prim_cb = cb;
1406 lc->lapdm_acch.l1_prim_cb = cb;
1407 lc->lapdm_dcch.l1_ctx = ctx;
1408 lc->lapdm_acch.l1_ctx = ctx;
1409}
1410
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001411/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001412void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1413{
1414 lc->lapdm_dcch.l3_cb = cb;
1415 lc->lapdm_acch.l3_cb = cb;
1416 lc->lapdm_dcch.l3_ctx = ctx;
1417 lc->lapdm_acch.l3_ctx = ctx;
1418}
1419
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001420/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001421void lapdm_entity_reset(struct lapdm_entity *le)
1422{
1423 struct lapdm_datalink *dl;
1424 int i;
1425
1426 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1427 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001428 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001429 }
1430}
1431
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001432/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001433void lapdm_channel_reset(struct lapdm_channel *lc)
1434{
1435 lapdm_entity_reset(&lc->lapdm_dcch);
1436 lapdm_entity_reset(&lc->lapdm_acch);
1437}
1438
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001439/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001440void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1441{
1442 le->flags = flags;
1443}
1444
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001445/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001446void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1447{
1448 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1449 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1450}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001451
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001452/*! @} */