blob: 8620cabece1096a5daf440b0d0093ed0d54b5a6c [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
Harald Welte00b2faf2020-05-02 19:56:36 +0200129extern void *tall_lapd_ctx;
130
rootaf48bed2011-09-26 11:23:06 +0200131static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
132static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
133 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100134static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200135
136static void lapdm_dl_init(struct lapdm_datalink *dl,
Harald Welte00b2faf2020-05-02 19:56:36 +0200137 struct lapdm_entity *entity, int t200_ms, uint32_t n200,
138 const char *name)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200139{
140 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200141 dl->entity = entity;
Harald Welte00b2faf2020-05-02 19:56:36 +0200142 lapd_dl_init2(&dl->dl, 1, 8, 251, name); /* Section 5.8.5 of TS 04.06 */
rootaf48bed2011-09-26 11:23:06 +0200143 dl->dl.reestablish = 0; /* GSM uses no reestablish */
144 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
145 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100146 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200147 dl->dl.n200_est_rel = N200_EST_REL;
Harald Welte20de6202019-06-02 21:33:38 +0200148 dl->dl.n200 = n200;
rootaf48bed2011-09-26 11:23:06 +0200149 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Harald Welte20de6202019-06-02 21:33:38 +0200150 dl->dl.t200_sec = t200_ms / 1000; dl->dl.t200_usec = (t200_ms % 1000) * 1000;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200151}
152
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200153/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200154 * \param[in] le LAPDm entity
155 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200156 * \param[in] t200 T200 re-transmission timer for all SAPIs in seconds
157 *
158 * Don't use this function; It doesn't support different T200 values per API
159 * and doesn't permit the caller to specify the N200 counter, both of which
160 * are required by GSM specs and supported by lapdm_entity_init2().
Harald Welte6bdf0b12011-08-17 18:22:08 +0200161 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100162void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200163{
Harald Welte20de6202019-06-02 21:33:38 +0200164 /* convert from single full-second value to per-SAPI milli-second value */
165 int t200_ms_sapi_arr[_NR_DL_SAPI];
166 int i;
167
168 for (i = 0; i < ARRAY_SIZE(t200_ms_sapi_arr); i++)
169 t200_ms_sapi_arr[i] = t200 * 1000;
170
Harald Welte00b2faf2020-05-02 19:56:36 +0200171 return lapdm_entity_init3(le, mode, t200_ms_sapi_arr, N200, NULL);
Harald Welte20de6202019-06-02 21:33:38 +0200172}
173
174/*! initialize a LAPDm entity and all datalinks inside
175 * \param[in] le LAPDm entity
176 * \param[in] mode lapdm_mode (BTS/MS)
177 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
178 * \param[in] n200 N200 re-transmisison count
179 */
180void lapdm_entity_init2(struct lapdm_entity *le, enum lapdm_mode mode,
181 const int *t200_ms, int n200)
182{
Harald Welte00b2faf2020-05-02 19:56:36 +0200183 lapdm_entity_init3(le, mode, t200_ms, n200, NULL);
184}
185
186/*! initialize a LAPDm entity and all datalinks inside
187 * \param[in] le LAPDm entity
188 * \param[in] mode lapdm_mode (BTS/MS)
189 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
190 * \param[in] n200 N200 re-transmisison count
191 * \param[in] name human-readable name (will be copied internally + extended with SAPI)
192 */
193void lapdm_entity_init3(struct lapdm_entity *le, enum lapdm_mode mode,
194 const int *t200_ms, int n200, const char *name_pfx)
195{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200196 unsigned int i;
197
Harald Welte00b2faf2020-05-02 19:56:36 +0200198 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
199 char name[256];
200 if (name_pfx) {
201 snprintf(name, sizeof(name), "%s[%s]", name_pfx, i == 0 ? "0" : "3");
202 lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200, name);
203 } else
204 lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200, NULL);
205 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200206
207 lapdm_entity_set_mode(le, mode);
208}
209
Harald Welte20de6202019-06-02 21:33:38 +0200210static int get_n200_dcch(enum gsm_chan_t chan_t)
211{
212 switch (chan_t) {
213 case GSM_LCHAN_SDCCH:
214 return N200_TR_SDCCH;
215 case GSM_LCHAN_TCH_F:
216 return N200_TR_FACCH_FR;
217 case GSM_LCHAN_TCH_H:
218 return N200_TR_FACCH_HR;
219 default:
220 return -1;
221 }
222}
223
224/*! initialize a LAPDm channel and all its channels
225 * \param[in] lc lapdm_channel to be initialized
226 * \param[in] mode lapdm_mode (BTS/MS)
227 *
228 * Don't use this function; It doesn't support different T200 values per API
229 * and doesn't set the correct N200 counter, both of which
230 * are required by GSM specs and supported by lapdm_channel_init2().
231 */
232void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
233{
234 /* emulate old backwards-compatible behavior with 1s/2s */
235 const int t200_ms_dcch[_NR_DL_SAPI] = { 1000, 1000 };
236 const int t200_ms_acch[_NR_DL_SAPI] = { 2000, 2000 };
237
Harald Welte00b2faf2020-05-02 19:56:36 +0200238 lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, GSM_LCHAN_SDCCH, NULL);
Harald Welte20de6202019-06-02 21:33:38 +0200239}
240
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200241/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200242 * \param[in] lc \ref lapdm_channel to be initialized
243 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200244 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
245 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
246 * \param[in] chan_t GSM channel type (to correctly set N200)
Harald Welte6bdf0b12011-08-17 18:22:08 +0200247 */
Harald Welte20de6202019-06-02 21:33:38 +0200248int lapdm_channel_init2(struct lapdm_channel *lc, enum lapdm_mode mode,
249 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200250{
Harald Welte00b2faf2020-05-02 19:56:36 +0200251 return lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, chan_t, NULL);
252}
253
254/*! initialize a LAPDm channel and all its channels
255 * \param[in] lc \ref lapdm_channel to be initialized
256 * \param[in] mode \ref lapdm_mode (BTS/MS)
257 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
258 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
259 * \param[in] chan_t GSM channel type (to correctly set N200)
260 * \parma[in] name_pfx human-readable name (copied by function + extended with ACCH/DCCH)
261 */
262int lapdm_channel_init3(struct lapdm_channel *lc, enum lapdm_mode mode,
263 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t,
264 const char *name_pfx)
265{
Harald Welte20de6202019-06-02 21:33:38 +0200266 int n200_dcch = get_n200_dcch(chan_t);
Harald Welte00b2faf2020-05-02 19:56:36 +0200267 char namebuf[256];
268 char *name = NULL;
269
Harald Welte20de6202019-06-02 21:33:38 +0200270 if (n200_dcch < 0)
271 return -EINVAL;
272
Harald Welte00b2faf2020-05-02 19:56:36 +0200273 osmo_talloc_replace_string(tall_lapd_ctx, &lc->name, name_pfx);
274
275 if (name_pfx) {
276 snprintf(namebuf, sizeof(namebuf), "%s[ACCH]", name_pfx);
277 name = namebuf;
278 }
279 lapdm_entity_init3(&lc->lapdm_acch, mode, t200_ms_acch, N200_TR_SACCH, name);
Harald Welte3e8c5202018-05-04 20:58:48 +0200280 lc->lapdm_acch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200281
Harald Welte00b2faf2020-05-02 19:56:36 +0200282 if (name_pfx) {
283 snprintf(namebuf, sizeof(namebuf), "%s[DCCH]", name_pfx);
284 name = namebuf;
285 }
286 lapdm_entity_init3(&lc->lapdm_dcch, mode, t200_ms_dcch, n200_dcch, name);
Harald Welte3e8c5202018-05-04 20:58:48 +0200287 lc->lapdm_dcch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200288
289 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200290}
291
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200292/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200293void lapdm_entity_exit(struct lapdm_entity *le)
294{
295 unsigned int i;
296 struct lapdm_datalink *dl;
297
298 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
299 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200300 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200301 }
302}
303
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200304/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200305 *
306 * A convenience wrapper calling \ref lapdm_entity_exit on both
307 * entities inside the \ref lapdm_channel
308 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200309void lapdm_channel_exit(struct lapdm_channel *lc)
310{
311 lapdm_entity_exit(&lc->lapdm_acch);
312 lapdm_entity_exit(&lc->lapdm_dcch);
313}
314
Daniel Willmann55405fb2014-03-26 13:45:17 +0100315struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200316{
317 switch (sapi) {
318 case LAPDm_SAPI_NORMAL:
319 return &le->datalink[0];
320 case LAPDm_SAPI_SMS:
321 return &le->datalink[1];
322 default:
323 return NULL;
324 }
325}
326
Harald Welte1f0b8c22011-06-27 10:51:37 +0200327/* Append padding (if required) */
328static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
329{
330 int pad_len = n201 - msgb_l2len(msg);
331 uint8_t *data;
332
333 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200334 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200335 "cannot pad message that is already too big!\n");
336 return;
337 }
338
Vadim Yanitskiy29ecabe2020-08-27 02:12:23 +0700339 data = msgb_put(msg, pad_len); /* TODO: random padding */
340 memset(data, GSM_MACBLOCK_PADDING, pad_len);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200341}
342
343/* input function that L2 calls when sending messages up to L3 */
344static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
345{
346 if (!le->l3_cb) {
347 msgb_free(msg);
348 return -EIO;
349 }
350
351 /* call the layer2 message handler that is registered */
352 return le->l3_cb(msg, le, le->l3_ctx);
353}
354
355/* write a frame into the tx queue */
356static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200357 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200358{
359 struct lapdm_entity *le = dl->entity;
360 struct osmo_phsap_prim pp;
361
362 /* if there is a pending message, queue it */
363 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200364 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200365 *msgb_push(msg, 1) = link_id;
366 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200367 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200368 return -EBUSY;
369 }
370
371 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
372 PRIM_OP_REQUEST, msg);
373 pp.u.data.chan_nr = chan_nr;
374 pp.u.data.link_id = link_id;
375
376 /* send the frame now */
377 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200378 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200379
380 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
381}
382
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700383/* Dequeue a Downlink message for DCCH (dedicated channel) */
384static struct msgb *tx_dequeue_dcch_msgb(struct lapdm_entity *le)
385{
386 struct msgb *msg;
387
388 /* SAPI=0 always has higher priority than SAPI=3 */
389 msg = msgb_dequeue(&le->datalink[DL_SAPI0].dl.tx_queue);
390 if (msg == NULL) /* no SAPI=0 messages, dequeue SAPI=3 (if any) */
391 msg = msgb_dequeue(&le->datalink[DL_SAPI3].dl.tx_queue);
392
393 return msg;
394}
395
396/* Dequeue a Downlink message for ACCH (associated channel) */
397static struct msgb *tx_dequeue_acch_msgb(struct lapdm_entity *le)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200398{
399 struct lapdm_datalink *dl;
400 int last = le->last_tx_dequeue;
401 int i = last, n = ARRAY_SIZE(le->datalink);
402 struct msgb *msg = NULL;
403
404 /* round-robin dequeue */
405 do {
406 /* next */
407 i = (i + 1) % n;
408 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200409 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200410 break;
411 } while (i != last);
412
413 if (msg) {
414 /* Set last dequeue position */
415 le->last_tx_dequeue = i;
416 }
417
418 return msg;
419}
420
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200421/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200422 * a osmo_phsap_prim */
423int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
424{
425 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200426 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200427
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700428 /* Dequeue depending on channel type: DCCH or ACCH.
429 * See 3GPP TS 44.005, section 4.2.2 "Priority". */
430 if (le == &le->lapdm_ch->lapdm_dcch)
431 msg = tx_dequeue_dcch_msgb(le);
432 else
433 msg = tx_dequeue_acch_msgb(le);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200434 if (!msg)
435 return -ENODEV;
436
437 /* if we have a message, send PH-DATA.req */
438 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
439 PRIM_OP_REQUEST, msg);
440
441 /* Pull chan_nr and link_id */
442 pp->u.data.chan_nr = *msg->data;
443 msgb_pull(msg, 1);
444 pp->u.data.link_id = *msg->data;
445 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200446 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200447 msgb_pull(msg, 1);
448
449 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200450 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200451
452 return 0;
453}
454
455/* get next frame from the tx queue. because the ms has multiple datalinks,
456 * each datalink's queue is read round-robin.
457 */
458static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
459{
460 struct osmo_phsap_prim pp;
461
462 /* we may send again */
463 le->tx_pending = 0;
464
465 /* free confirm message */
466 if (msg)
467 msgb_free(msg);
468
469 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
470 /* no message in all queues */
471
472 /* If user didn't request PH-EMPTY_FRAME.req, abort */
473 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
474 return 0;
475
476 /* otherwise, send PH-EMPTY_FRAME.req */
477 osmo_prim_init(&pp.oph, SAP_GSM_PH,
478 PRIM_PH_EMPTY_FRAME,
479 PRIM_OP_REQUEST, NULL);
480 } else {
481 le->tx_pending = 1;
482 }
483
484 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
485}
486
Harald Welte542301b2018-04-19 16:11:14 +0200487/* Is a given msg_type "transparent" as per TS 48.058 Section 8.1 */
488static int rsl_is_transparent(uint8_t msg_type)
489{
490 switch (msg_type) {
491 case RSL_MT_DATA_IND:
492 case RSL_MT_UNIT_DATA_IND:
493 return 1;
494 case RSL_MT_DATA_REQ:
495 case RSL_MT_UNIT_DATA_REQ:
496 return 1;
497 default:
498 return 0;
499 }
500}
501
Harald Welte1f0b8c22011-06-27 10:51:37 +0200502/* Create RSLms various RSLms messages */
503static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
504 struct msgb *msg)
505{
Harald Welte542301b2018-04-19 16:11:14 +0200506 int transparent = rsl_is_transparent(msg_type);
507
Harald Welte1f0b8c22011-06-27 10:51:37 +0200508 /* Add the RSL + RLL header */
Harald Welte542301b2018-04-19 16:11:14 +0200509 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200510
511 /* send off the RSLms message to L3 */
512 return rslms_sendmsg(msg, mctx->dl->entity);
513}
514
515/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
516static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
517{
518 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200519
520 /* Add the RSL + RLL header */
521 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welted977f5f2018-05-08 21:53:28 +0200522
Harald Weltef1bdf782018-05-08 22:03:20 +0200523 /* Add two non-standard IEs carrying MS power and TA values for B4 (SACCH) */
524 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
525 msgb_tv_push(msg, RSL_IE_MS_POWER, mctx->tx_power_ind);
526 msgb_tv_push(msg, RSL_IE_TIMING_ADVANCE, mctx->ta_ind);
527 }
Harald Welted977f5f2018-05-08 21:53:28 +0200528
Harald Welte1f0b8c22011-06-27 10:51:37 +0200529 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
530 mctx->link_id, 1);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100531
Harald Welte1f0b8c22011-06-27 10:51:37 +0200532 return rslms_sendmsg(msg, mctx->dl->entity);
533}
534
535static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
536{
537 struct msgb *msg;
Harald Welte542301b2018-04-19 16:11:14 +0200538 int transparent = rsl_is_transparent(msg_type);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200539
Harald Welte542301b2018-04-19 16:11:14 +0200540 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200541
542 /* send off the RSLms message to L3 */
543 return rslms_sendmsg(msg, mctx->dl->entity);
544}
545
546static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
547{
548 struct msgb *msg;
549
Harald Welte00b2faf2020-05-02 19:56:36 +0200550 LOGDL(&mctx->dl->dl, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte542301b2018-04-19 16:11:14 +0200551 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200552 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
553 return rslms_sendmsg(msg, mctx->dl->entity);
554}
555
rootaf48bed2011-09-26 11:23:06 +0200556/* DLSAP L2 -> L3 (RSLms) */
557static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
558 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200559{
rootaf48bed2011-09-26 11:23:06 +0200560 struct lapd_datalink *dl = lctx->dl;
561 struct lapdm_datalink *mdl =
562 container_of(dl, struct lapdm_datalink, dl);
563 struct lapdm_msg_ctx *mctx = &mdl->mctx;
564 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200565
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200566 switch (OSMO_PRIM_HDR(&dp->oph)) {
567 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
568 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200569 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200570 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
571 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200572 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200573 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
574 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200575 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200576 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
577 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
578 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
579 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200580 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200581 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
582 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200583 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200584 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
585 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200586 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200587 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
588 rsl_rll_error(dp->u.error_ind.cause, mctx);
589 if (dp->oph.msg)
590 msgb_free(dp->oph.msg);
591 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200592 }
rootaf48bed2011-09-26 11:23:06 +0200593
594 if (!rll_msg) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200595 LOGDL(dl, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
rootaf48bed2011-09-26 11:23:06 +0200596 "fix!\n", dp->oph.primitive, dp->oph.operation);
597 return -EINVAL;
598 }
599
600 if (!dp->oph.msg)
601 return send_rll_simple(rll_msg, mctx);
602
603 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200604}
605
rootaf48bed2011-09-26 11:23:06 +0200606/* send a data frame to layer 1 */
607static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200608{
rootaf48bed2011-09-26 11:23:06 +0200609 uint8_t l3_len = msg->tail - msg->data;
610 struct lapd_datalink *dl = lctx->dl;
611 struct lapdm_datalink *mdl =
612 container_of(dl, struct lapdm_datalink, dl);
613 struct lapdm_msg_ctx *mctx = &mdl->mctx;
614 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200615
rootaf48bed2011-09-26 11:23:06 +0200616 /* prepend l2 header */
617 msg->l2h = msgb_push(msg, 3);
618 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
619 /* EA is set here too */
620 switch (format) {
621 case LAPD_FORM_I:
622 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
623 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200624 break;
rootaf48bed2011-09-26 11:23:06 +0200625 case LAPD_FORM_S:
626 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200627 break;
rootaf48bed2011-09-26 11:23:06 +0200628 case LAPD_FORM_U:
629 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200630 break;
631 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200632 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200633 return -EINVAL;
634 }
rootaf48bed2011-09-26 11:23:06 +0200635 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
636 if (lctx->more)
637 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200638
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100639 /* add ACCH header with last indicated tx-power and TA */
640 if ((mctx->link_id & 0x40)) {
641 struct lapdm_entity *le = mdl->entity;
642
643 msg->l2h = msgb_push(msg, 2);
644 msg->l2h[0] = le->tx_power;
645 msg->l2h[1] = le->ta;
646 }
647
rootaf48bed2011-09-26 11:23:06 +0200648 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
649 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200650}
651
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100652static int update_pending_frames(struct lapd_msg_ctx *lctx)
653{
654 struct lapd_datalink *dl = lctx->dl;
655 struct msgb *msg;
656 int rc = -1;
657
658 llist_for_each_entry(msg, &dl->tx_queue, list) {
659 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
660 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
661 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
662 rc = 0;
663 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200664 LOGDL(dl, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100665 }
666 }
667
668 return rc;
669}
670
Harald Welte1284c3e2018-05-01 18:11:02 +0200671/* determine if receiving a given LAPDm message is not permitted */
672static int lapdm_rx_not_permitted(const struct lapdm_entity *le,
673 const struct lapd_msg_ctx *lctx)
674{
675 /* we currently only implement SABM related checks here */
676 if (lctx->format != LAPD_FORM_U || lctx->s_u != LAPD_U_SABM)
677 return 0;
678
679 if (le->mode == LAPDM_MODE_BTS) {
680 if (le == &le->lapdm_ch->lapdm_acch) {
681 /* no contention resolution on SACCH */
682 if (lctx->length > 0)
683 return RLL_CAUSE_SABM_INFO_NOTALL;
684 } else {
685 switch (lctx->sapi) {
Harald Welte1284c3e2018-05-01 18:11:02 +0200686 case 3:
687 /* SAPI3 doesn't support contention resolution */
688 if (lctx->length > 0)
689 return RLL_CAUSE_SABM_INFO_NOTALL;
690 break;
Harald Welteb82a4072018-05-09 16:31:16 +0200691 default:
692 break;
Harald Welte1284c3e2018-05-01 18:11:02 +0200693 }
694 }
695 } else if (le->mode == LAPDM_MODE_MS) {
696 /* contention resolution (L3 present) is only sent by MS, but
697 * never received by it */
698 if (lctx->length > 0)
699 return RLL_CAUSE_SABM_INFO_NOTALL;
700 }
701 return 0;
702}
703
Harald Welte1f0b8c22011-06-27 10:51:37 +0200704/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200705static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
706 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200707{
708 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200709 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200710 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200711 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200712 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200713 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200714
715 /* when we reach here, we have a msgb with l2h pointing to the raw
716 * 23byte mac block. The l1h has already been purged. */
717
rootaf48bed2011-09-26 11:23:06 +0200718 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200719 mctx.chan_nr = chan_nr;
720 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200721
Harald Welte1f0b8c22011-06-27 10:51:37 +0200722 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
723 if (cbits == 0x10 || cbits == 0x12) {
724 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
725 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200726 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200727 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200728 } else {
729 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200730 /* It was received from network on SACCH */
731
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100732 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
733 if (le->mode == LAPDM_MODE_MS
734 && LAPDm_CTRL_is_U(msg->l2h[3])
735 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200736 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200737 n201 = N201_B4;
Harald Welte7ca604b2011-06-29 12:13:51 +0200738 } else {
739 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200740 n201 = N201_AB_SACCH;
Harald Welte7ca604b2011-06-29 12:13:51 +0200741 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200742 /* SACCH frames have a two-byte L1 header that
743 * OsmocomBB L1 doesn't strip */
744 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
745 mctx.ta_ind = msg->l2h[1];
746 msgb_pull(msg, 2);
747 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200748 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200749 } else {
750 mctx.lapdm_fmt = LAPDm_FMT_B;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100751 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200752 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200753 }
754 }
755
Daniel Willmann55405fb2014-03-26 13:45:17 +0100756 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200757 /* G.2.1 No action on frames containing an unallocated SAPI. */
758 if (!mctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200759 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200760 msgb_free(msg);
761 return -EIO;
762 }
763
Harald Welte1f0b8c22011-06-27 10:51:37 +0200764 switch (mctx.lapdm_fmt) {
765 case LAPDm_FMT_A:
766 case LAPDm_FMT_B:
767 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200768 lctx.dl = &mctx.dl->dl;
769 /* obtain SAPI from address field */
770 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
771 /* G.2.3 EA bit set to "0" is not allowed in GSM */
772 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200773 LOGDL(lctx.dl, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200774 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200775 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200776 return -EINVAL;
777 }
rootaf48bed2011-09-26 11:23:06 +0200778 /* adress field */
779 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
780 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
781 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
782 /* command field */
783 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
784 lctx.format = LAPD_FORM_I;
785 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
786 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
787 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
788 lctx.format = LAPD_FORM_S;
789 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
790 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
791 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
792 lctx.format = LAPD_FORM_U;
793 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
794 } else
795 lctx.format = LAPD_FORM_UKN;
796 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
797 if (lctx.sapi != LAPDm_SAPI_NORMAL
798 && lctx.sapi != LAPDm_SAPI_SMS
799 && lctx.format == LAPD_FORM_U
800 && lctx.s_u == LAPDm_U_UI) {
801 /* 5.3.3 UI frames with invalid SAPI values shall be
802 * discarded
803 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200804 LOGDL(lctx.dl, LOGL_INFO, "sapi=%u (discarding)\n", lctx.sapi);
rootaf48bed2011-09-26 11:23:06 +0200805 msgb_free(msg);
806 return 0;
807 }
808 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
809 lctx.n201 = n201;
810 lctx.length = n201;
811 lctx.more = 0;
812 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100813 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200814 } else {
815 /* length field */
816 if (!(msg->l2h[2] & LAPDm_EL)) {
817 /* G.4.1 If the EL bit is set to "0", an
818 * MDL-ERROR-INDICATION primitive with cause
819 * "frame not implemented" is sent to the
820 * mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +0200821 LOGDL(lctx.dl, LOGL_NOTICE, "we don't support multi-octet length\n");
rootaf48bed2011-09-26 11:23:06 +0200822 msgb_free(msg);
823 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
824 return -EINVAL;
825 }
826 lctx.n201 = n201;
827 lctx.length = msg->l2h[2] >> 2;
828 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
829 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100830 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200831 }
832 /* store context for messages from lapd */
833 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200834 rc =lapdm_rx_not_permitted(le, &lctx);
835 if (rc > 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200836 LOGDL(lctx.dl, LOGL_NOTICE, "received message not permitted\n");
Harald Welte1284c3e2018-05-01 18:11:02 +0200837 msgb_free(msg);
838 rsl_rll_error(rc, &mctx);
839 return -EINVAL;
840 }
rootaf48bed2011-09-26 11:23:06 +0200841 /* send to LAPD */
842 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200843 break;
844 case LAPDm_FMT_Bter:
845 /* FIXME */
846 msgb_free(msg);
847 break;
848 case LAPDm_FMT_Bbis:
849 /* directly pass up to layer3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200850 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100851 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200852 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
853 break;
854 default:
855 msgb_free(msg);
856 }
857
858 return rc;
859}
860
861/* input into layer2 (from layer 1) */
862static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
863{
864 struct abis_rsl_cchan_hdr *ch;
865 struct gsm48_req_ref req_ref;
866 struct gsm_time gt;
867 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
868
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200869 if (!msg)
870 return -ENOMEM;
871
Harald Welte1f0b8c22011-06-27 10:51:37 +0200872 msg->l2h = msgb_push(msg, sizeof(*ch));
873 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
874 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
875 ch->chan_nr = RSL_CHAN_RACH;
876
877 /* generate a RSL CHANNEL REQUIRED message */
878 gsm_fn2gsmtime(&gt, fn);
879 req_ref.ra = ra;
880 req_ref.t1 = gt.t1; /* FIXME: modulo? */
881 req_ref.t2 = gt.t2;
882 req_ref.t3_low = gt.t3 & 7;
883 req_ref.t3_high = gt.t3 >> 3;
884
885 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
886 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
887
888 return rslms_sendmsg(msg, le);
889}
890
891static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
892
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200893/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200894int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
895{
896 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
897 int rc = 0;
898
899 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200900 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200901 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100902 rc = -ENODEV;
903 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200904 }
905
906 switch (oph->primitive) {
907 case PRIM_PH_DATA:
908 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200909 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200910 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100911 rc = -ENODEV;
912 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200913 }
914 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
915 pp->u.data.link_id);
916 break;
917 case PRIM_PH_RTS:
918 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200919 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200920 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100921 rc = -ENODEV;
922 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200923 }
924 rc = l2_ph_data_conf(oph->msg, le);
925 break;
926 case PRIM_PH_RACH:
927 switch (oph->operation) {
928 case PRIM_OP_INDICATION:
929 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
930 pp->u.rach_ind.acc_delay);
931 break;
932 case PRIM_OP_CONFIRM:
933 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
934 break;
935 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100936 rc = -EIO;
937 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200938 }
939 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100940 default:
941 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
942 oph->primitive);
943 rc = -EINVAL;
944 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200945 }
946
947 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100948
949free:
950 msgb_free(oph->msg);
951 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200952}
953
954
955/* L3 -> L2 / RSLMS -> LAPDm */
956
rootaf48bed2011-09-26 11:23:06 +0200957/* Set LAPDm context for established connection */
958static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
959 uint8_t link_id, int n201, uint8_t sapi)
960{
961 memset(&dl->mctx, 0, sizeof(dl->mctx));
962 dl->mctx.dl = dl;
963 dl->mctx.chan_nr = chan_nr;
964 dl->mctx.link_id = link_id;
965 dl->dl.lctx.dl = &dl->dl;
966 dl->dl.lctx.n201 = n201;
967 dl->dl.lctx.sapi = sapi;
968
969 return 0;
970}
971
Harald Welte1f0b8c22011-06-27 10:51:37 +0200972/* L3 requests establishment of data link */
973static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
974{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200975 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
976 uint8_t chan_nr = rllh->chan_nr;
977 uint8_t link_id = rllh->link_id;
978 uint8_t sapi = rllh->link_id & 7;
979 struct tlv_parsed tv;
980 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100981 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200982 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200983
rootaf48bed2011-09-26 11:23:06 +0200984 /* Set LAPDm context for established connection */
985 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200986
rootaf48bed2011-09-26 11:23:06 +0200987 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200988 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200989 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200990 /* contention resolution establishment procedure */
991 if (sapi != 0) {
992 /* According to clause 6, the contention resolution
993 * procedure is only permitted with SAPI value 0 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200994 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200995 "resolution (discarding)\n");
996 msgb_free(msg);
997 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
998 }
999 /* transmit a SABM command with the P bit set to "1". The SABM
1000 * command shall contain the layer 3 message unit */
1001 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001002 } else {
1003 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +02001004 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001005 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001006 }
1007
1008 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001009 if (length > n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001010 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001011 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001012 msgb_free(msg);
1013 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1014 }
1015
rootaf48bed2011-09-26 11:23:06 +02001016 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001017 msgb_pull_to_l3(msg);
1018 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001019
rootaf48bed2011-09-26 11:23:06 +02001020 /* prepare prim */
1021 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001022
rootaf48bed2011-09-26 11:23:06 +02001023 /* send to L2 */
1024 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001025}
1026
1027/* L3 requests transfer of unnumbered information */
1028static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1029{
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001030 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001031 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1032 uint8_t chan_nr = rllh->chan_nr;
1033 uint8_t link_id = rllh->link_id;
1034 uint8_t sapi = link_id & 7;
1035 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +01001036 int length, ui_bts;
1037
1038 if (!le) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001039 LOGDL(&dl->dl, LOGL_ERROR, "lapdm_datalink without entity error\n");
Max777be2e2017-03-01 18:16:44 +01001040 msgb_free(msg);
1041 return -EMLINK;
1042 }
1043 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +02001044
1045 /* check if the layer3 message length exceeds N201 */
1046
1047 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1048
1049 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001050 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001051 }
1052 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001053 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001054 }
1055 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001056 LOGDL(&dl->dl, LOGL_ERROR, "unit data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001057 msgb_free(msg);
1058 return -EINVAL;
1059 }
rootaf48bed2011-09-26 11:23:06 +02001060 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001061 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1062 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +02001063 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001064 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +02001065 "(discarding)\n", length,
1066 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001067 msgb_free(msg);
1068 return -EIO;
1069 }
1070
Harald Welte00b2faf2020-05-02 19:56:36 +02001071 LOGDL(&dl->dl, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n", le->tx_power, le->ta);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001072
rootaf48bed2011-09-26 11:23:06 +02001073 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001074 msgb_pull_to_l3(msg);
1075 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001076
1077 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +02001078 msg->l2h = msgb_push(msg, 2 + !ui_bts);
1079 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
1080 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001081 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +02001082 msg->l2h[2] = LAPDm_LEN(length);
1083 if (link_id & 0x40) {
1084 msg->l2h = msgb_push(msg, 2);
1085 msg->l2h[0] = le->tx_power;
1086 msg->l2h[1] = le->ta;
1087 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001088
1089 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +02001090 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001091}
1092
1093/* L3 requests transfer of acknowledged information */
1094static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1095{
1096 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1097 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +02001098 int length;
1099 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001100
1101 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1102 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001103 LOGDL(&dl->dl, LOGL_ERROR, "data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001104 msgb_free(msg);
1105 return -EINVAL;
1106 }
rootaf48bed2011-09-26 11:23:06 +02001107 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
1108 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001109
rootaf48bed2011-09-26 11:23:06 +02001110 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001111 msgb_pull_to_l3(msg);
1112 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001113
rootaf48bed2011-09-26 11:23:06 +02001114 /* prepare prim */
1115 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001116
rootaf48bed2011-09-26 11:23:06 +02001117 /* send to L2 */
1118 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001119}
1120
1121/* L3 requests suspension of data link */
1122static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1123{
1124 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1125 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001126 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001127
1128 if (sapi != 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001129 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001130 msgb_free(msg);
1131 return -EINVAL;
1132 }
1133
rootaf48bed2011-09-26 11:23:06 +02001134 /* prepare prim */
1135 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001136
rootaf48bed2011-09-26 11:23:06 +02001137 /* send to L2 */
1138 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001139}
1140
1141/* L3 requests resume of data link */
1142static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1143{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001144 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001145 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001146 uint8_t chan_nr = rllh->chan_nr;
1147 uint8_t link_id = rllh->link_id;
1148 uint8_t sapi = rllh->link_id & 7;
1149 struct tlv_parsed tv;
1150 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001151 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001152 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001153
rootaf48bed2011-09-26 11:23:06 +02001154 /* Set LAPDm context for established connection */
1155 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001156
1157 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1158 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001159 LOGDL(&dl->dl, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001160 msgb_free(msg);
1161 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1162 }
rootaf48bed2011-09-26 11:23:06 +02001163 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001164 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1165
rootaf48bed2011-09-26 11:23:06 +02001166 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001167 msgb_pull_to_l3(msg);
1168 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001169
rootaf48bed2011-09-26 11:23:06 +02001170 /* prepare prim */
1171 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1172 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001173
rootaf48bed2011-09-26 11:23:06 +02001174 /* send to L2 */
1175 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001176}
1177
1178/* L3 requests release of data link */
1179static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1180{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001181 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001182 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001183 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001184
1185 /* get release mode */
1186 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1187 mode = rllh->data[1] & 1;
1188
Harald Welte1f0b8c22011-06-27 10:51:37 +02001189 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001190 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001191
1192 /* 04.06 3.8.3: No information field is permitted with the DISC
1193 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001194 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001195
rootaf48bed2011-09-26 11:23:06 +02001196 /* prepare prim */
1197 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1198 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001199
rootaf48bed2011-09-26 11:23:06 +02001200 /* send to L2 */
1201 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001202}
1203
1204/* L3 requests channel in idle state */
1205static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1206{
1207 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1208 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1209 struct osmo_phsap_prim pp;
1210
1211 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1212 PRIM_OP_REQUEST, NULL);
1213
1214 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001215 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001216 return -EINVAL;
1217 }
1218 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001219 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001220 return -EINVAL;
1221 }
1222 pp.u.rach_req.ra = cch->data[1];
1223 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1224 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1225
1226 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001227 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001228 return -EINVAL;
1229 }
1230 /* TA = 0 - delay */
1231 pp.u.rach_req.ta = 0 - cch->data[5];
1232
1233 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001234 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001235 return -EINVAL;
1236 }
1237 pp.u.rach_req.tx_power = cch->data[7];
1238
1239 msgb_free(msg);
1240
1241 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1242}
1243
1244/* L1 confirms channel request */
1245static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1246{
1247 struct abis_rsl_cchan_hdr *ch;
1248 struct gsm_time tm;
1249 struct gsm48_req_ref *ref;
1250
1251 gsm_fn2gsmtime(&tm, frame_nr);
1252
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001253 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001254 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1255 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1256 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1257 ch->chan_nr = RSL_CHAN_RACH;
1258 ch->data[0] = RSL_IE_REQ_REFERENCE;
1259 ref = (struct gsm48_req_ref *) (ch->data + 1);
1260 ref->t1 = tm.t1;
1261 ref->t2 = tm.t2;
1262 ref->t3_low = tm.t3 & 0x7;
1263 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001264
Harald Welte1f0b8c22011-06-27 10:51:37 +02001265 return rslms_sendmsg(msg, le);
1266}
1267
Harald Welte1f0b8c22011-06-27 10:51:37 +02001268/* incoming RSLms RLL message from L3 */
1269static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1270{
1271 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1272 int msg_type = rllh->c.msg_type;
1273 uint8_t sapi = rllh->link_id & 7;
1274 struct lapdm_entity *le;
1275 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001276 int rc = 0;
1277
1278 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001279 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001280 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001281 return -EINVAL;
1282 }
1283
1284 if (rllh->link_id & 0x40)
1285 le = &lc->lapdm_acch;
1286 else
1287 le = &lc->lapdm_dcch;
1288
Harald Welte1a87c1b2015-12-14 15:26:07 +01001289 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1290 * BTS side */
1291 if (le->mode == LAPDM_MODE_BTS) {
1292 switch (msg_type) {
1293 case RSL_MT_SUSP_REQ:
1294 case RSL_MT_RES_REQ:
1295 case RSL_MT_RECON_REQ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001296 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' unsupported in BTS side LAPDm\n",
Harald Welte1a87c1b2015-12-14 15:26:07 +01001297 lc->name, rsl_msg_name(msg_type));
1298 msgb_free(msg);
1299 return -EINVAL;
1300 break;
1301 default:
1302 break;
1303 }
1304 }
1305
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001306 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001307 * SAPI.
1308 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001309 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001310 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001311 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001312 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001313 return -EINVAL;
1314 }
1315
Daniel Willmanne5233922012-12-25 23:15:50 +01001316 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001317 case RSL_MT_DATA_REQ:
1318 case RSL_MT_SUSP_REQ:
1319 case RSL_MT_REL_REQ:
1320 /* This is triggered in abnormal error conditions where
1321 * set_lapdm_context() was not called for the channel earlier. */
1322 if (!dl->dl.lctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001323 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001324 lc->name, rsl_msg_name(msg_type), sapi);
1325 msgb_free(msg);
1326 return -EINVAL;
1327 }
1328 break;
1329 default:
Harald Welte00b2faf2020-05-02 19:56:36 +02001330 LOGP(DLLAPD, LOGL_INFO, "(%s) RLL Message '%s' received. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001331 lc->name, rsl_msg_name(msg_type), sapi);
1332 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001333
rootaf48bed2011-09-26 11:23:06 +02001334 switch (msg_type) {
1335 case RSL_MT_UNIT_DATA_REQ:
1336 rc = rslms_rx_rll_udata_req(msg, dl);
1337 break;
1338 case RSL_MT_EST_REQ:
1339 rc = rslms_rx_rll_est_req(msg, dl);
1340 break;
1341 case RSL_MT_DATA_REQ:
1342 rc = rslms_rx_rll_data_req(msg, dl);
1343 break;
1344 case RSL_MT_SUSP_REQ:
1345 rc = rslms_rx_rll_susp_req(msg, dl);
1346 break;
1347 case RSL_MT_RES_REQ:
1348 rc = rslms_rx_rll_res_req(msg, dl);
1349 break;
1350 case RSL_MT_RECON_REQ:
1351 rc = rslms_rx_rll_res_req(msg, dl);
1352 break;
1353 case RSL_MT_REL_REQ:
1354 rc = rslms_rx_rll_rel_req(msg, dl);
1355 break;
1356 default:
1357 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001358 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001359 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001360 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001361
1362 return rc;
1363}
1364
1365/* incoming RSLms COMMON CHANNEL message from L3 */
1366static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1367{
1368 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1369 int msg_type = cch->c.msg_type;
1370 int rc = 0;
1371
1372 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001373 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001374 return -EINVAL;
1375 }
1376
1377 switch (msg_type) {
1378 case RSL_MT_CHAN_RQD:
1379 /* create and send RACH request */
1380 rc = rslms_rx_chan_rqd(lc, msg);
1381 break;
1382 default:
rootaf48bed2011-09-26 11:23:06 +02001383 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001384 msg_type);
1385 msgb_free(msg);
1386 return 0;
1387 }
1388
1389 return rc;
1390}
1391
Harald Welte7023aa02019-05-19 12:17:06 +02001392/*! Receive a RSLms \ref msgb from Layer 3. 'msg' ownership is transferred,
1393 * i.e. caller must not free it */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001394int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1395{
1396 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1397 int rc = 0;
1398
1399 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001400 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte7023aa02019-05-19 12:17:06 +02001401 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001402 return -EINVAL;
1403 }
1404
1405 switch (rslh->msg_discr & 0xfe) {
1406 case ABIS_RSL_MDISC_RLL:
1407 rc = rslms_rx_rll(msg, lc);
1408 break;
1409 case ABIS_RSL_MDISC_COM_CHAN:
1410 rc = rslms_rx_com_chan(msg, lc);
1411 break;
1412 default:
rootaf48bed2011-09-26 11:23:06 +02001413 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001414 "discriminator 0x%02x", rslh->msg_discr);
1415 msgb_free(msg);
1416 return -EINVAL;
1417 }
1418
1419 return rc;
1420}
1421
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001422/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001423int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1424{
rootaf48bed2011-09-26 11:23:06 +02001425 int i;
1426 enum lapd_mode lm;
1427
Harald Welte1f0b8c22011-06-27 10:51:37 +02001428 switch (mode) {
1429 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001430 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001431 break;
1432 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001433 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001434 break;
1435 default:
1436 return -EINVAL;
1437 }
1438
rootaf48bed2011-09-26 11:23:06 +02001439 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1440 lapd_set_mode(&le->datalink[i].dl, lm);
1441 }
1442
Harald Welte1f0b8c22011-06-27 10:51:37 +02001443 le->mode = mode;
1444
1445 return 0;
1446}
1447
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001448/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001449int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1450{
1451 int rc;
1452
1453 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1454 if (rc < 0)
1455 return rc;
1456
1457 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1458}
1459
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001460/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001461void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1462{
1463 lc->lapdm_dcch.l1_prim_cb = cb;
1464 lc->lapdm_acch.l1_prim_cb = cb;
1465 lc->lapdm_dcch.l1_ctx = ctx;
1466 lc->lapdm_acch.l1_ctx = ctx;
1467}
1468
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001469/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001470void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1471{
1472 lc->lapdm_dcch.l3_cb = cb;
1473 lc->lapdm_acch.l3_cb = cb;
1474 lc->lapdm_dcch.l3_ctx = ctx;
1475 lc->lapdm_acch.l3_ctx = ctx;
1476}
1477
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001478/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001479void lapdm_entity_reset(struct lapdm_entity *le)
1480{
1481 struct lapdm_datalink *dl;
1482 int i;
1483
1484 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1485 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001486 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001487 }
1488}
1489
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001490/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001491void lapdm_channel_reset(struct lapdm_channel *lc)
1492{
1493 lapdm_entity_reset(&lc->lapdm_dcch);
1494 lapdm_entity_reset(&lc->lapdm_acch);
1495}
1496
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001497/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001498void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1499{
1500 le->flags = flags;
1501}
1502
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001503/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001504void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1505{
1506 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1507 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1508}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001509
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001510/*! @} */