blob: 1fc986d6ee0d7eb9d4ae140e28f8db933871bd07 [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 *
Harald Welte1f0b8c22011-06-27 10:51:37 +020022 */
23
Harald Welte6bdf0b12011-08-17 18:22:08 +020024/*! \addtogroup lapdm
25 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020026 * \file lapdm.c */
Harald Welte6bdf0b12011-08-17 18:22:08 +020027
Harald Welte1f0b8c22011-06-27 10:51:37 +020028#include <stdio.h>
29#include <stdint.h>
Andreas Eversberg2d7119d2023-11-09 13:06:19 +010030#include <inttypes.h>
Harald Welte1f0b8c22011-06-27 10:51:37 +020031#include <string.h>
32#include <errno.h>
Harald Welte1f0b8c22011-06-27 10:51:37 +020033
34#include <osmocom/core/logging.h>
35#include <osmocom/core/timer.h>
36#include <osmocom/core/msgb.h>
37#include <osmocom/core/utils.h>
38
39#include <osmocom/gsm/tlv.h>
40#include <osmocom/gsm/rsl.h>
41#include <osmocom/gsm/prim.h>
42#include <osmocom/gsm/gsm_utils.h>
43#include <osmocom/gsm/lapdm.h>
44
45#include <osmocom/gsm/protocol/gsm_04_08.h>
46#include <osmocom/gsm/protocol/gsm_08_58.h>
47
Harald Welte1284c3e2018-05-01 18:11:02 +020048#define LAPD_U_SABM 0x7
49
Harald Welte1f0b8c22011-06-27 10:51:37 +020050/* TS 04.06 Figure 4 / Section 3.2 */
51#define LAPDm_LPD_NORMAL 0
52#define LAPDm_LPD_SMSCB 1
53#define LAPDm_SAPI_NORMAL 0
54#define LAPDm_SAPI_SMS 3
55#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
56
rootaf48bed2011-09-26 11:23:06 +020057#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020058#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
59#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
60#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
Andreas Eversberg03700182023-05-10 13:23:55 +020061#define LAPDm_ADDR_SHORT_L2(addr) ((addr) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020062
63/* TS 04.06 Table 3 / Section 3.4.3 */
64#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
65#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
66#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
67
68#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
69#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
70#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
71
72#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
73#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
74
75#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
76
77#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
78#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
79
Harald Welte1f0b8c22011-06-27 10:51:37 +020080#define LAPDm_LEN(len) ((len << 2) | 0x1)
81#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020082#define LAPDm_EL 0x1
83
84#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020085
86/* TS 04.06 Section 5.8.3 */
87#define N201_AB_SACCH 18
88#define N201_AB_SDCCH 20
89#define N201_AB_FACCH 20
90#define N201_Bbis 23
91#define N201_Bter_SACCH 21
92#define N201_Bter_SDCCH 23
93#define N201_Bter_FACCH 23
94#define N201_B4 19
95
96/* 5.8.2.1 N200 during establish and release */
97#define N200_EST_REL 5
98/* 5.8.2.1 N200 during timer recovery state */
99#define N200_TR_SACCH 5
100#define N200_TR_SDCCH 23
101#define N200_TR_FACCH_FR 34
102#define N200_TR_EFACCH_FR 48
103#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200104/* FIXME: set N200 depending on chan_nr */
105#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200106
107enum lapdm_format {
108 LAPDm_FMT_A,
109 LAPDm_FMT_B,
110 LAPDm_FMT_Bbis,
111 LAPDm_FMT_Bter,
112 LAPDm_FMT_B4,
113};
114
Maxadef12a2016-05-25 15:25:02 +0200115const struct value_string osmo_ph_prim_names[] = {
116 { PRIM_PH_DATA, "PH-DATA" },
117 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
118 { PRIM_PH_CONN, "PH-CONNECT" },
119 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
120 { PRIM_PH_RTS, "PH-RTS" },
121 { PRIM_MPH_INFO, "MPH-INFO" },
122 { PRIM_TCH, "TCH" },
123 { PRIM_TCH_RTS, "TCH-RTS" },
124 { 0, NULL }
125};
126
Harald Welte00b2faf2020-05-02 19:56:36 +0200127extern void *tall_lapd_ctx;
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 Welte00b2faf2020-05-02 19:56:36 +0200135 struct lapdm_entity *entity, int t200_ms, uint32_t n200,
136 const char *name)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200137{
138 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200139 dl->entity = entity;
Harald Welte00b2faf2020-05-02 19:56:36 +0200140 lapd_dl_init2(&dl->dl, 1, 8, 251, name); /* Section 5.8.5 of TS 04.06 */
rootaf48bed2011-09-26 11:23:06 +0200141 dl->dl.reestablish = 0; /* GSM uses no reestablish */
142 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
143 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100144 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200145 dl->dl.n200_est_rel = N200_EST_REL;
Harald Welte20de6202019-06-02 21:33:38 +0200146 dl->dl.n200 = n200;
rootaf48bed2011-09-26 11:23:06 +0200147 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Harald Welte20de6202019-06-02 21:33:38 +0200148 dl->dl.t200_sec = t200_ms / 1000; dl->dl.t200_usec = (t200_ms % 1000) * 1000;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200149}
150
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200151/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200152 * \param[in] le LAPDm entity
153 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200154 * \param[in] t200 T200 re-transmission timer for all SAPIs in seconds
155 *
156 * Don't use this function; It doesn't support different T200 values per API
157 * and doesn't permit the caller to specify the N200 counter, both of which
158 * are required by GSM specs and supported by lapdm_entity_init2().
Harald Welte6bdf0b12011-08-17 18:22:08 +0200159 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100160void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200161{
Harald Welte20de6202019-06-02 21:33:38 +0200162 /* convert from single full-second value to per-SAPI milli-second value */
163 int t200_ms_sapi_arr[_NR_DL_SAPI];
164 int i;
165
166 for (i = 0; i < ARRAY_SIZE(t200_ms_sapi_arr); i++)
167 t200_ms_sapi_arr[i] = t200 * 1000;
168
Harald Welte00b2faf2020-05-02 19:56:36 +0200169 return lapdm_entity_init3(le, mode, t200_ms_sapi_arr, N200, NULL);
Harald Welte20de6202019-06-02 21:33:38 +0200170}
171
172/*! initialize a LAPDm entity and all datalinks inside
173 * \param[in] le LAPDm entity
174 * \param[in] mode lapdm_mode (BTS/MS)
175 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
176 * \param[in] n200 N200 re-transmisison count
177 */
178void lapdm_entity_init2(struct lapdm_entity *le, enum lapdm_mode mode,
179 const int *t200_ms, int n200)
180{
Harald Welte00b2faf2020-05-02 19:56:36 +0200181 lapdm_entity_init3(le, mode, t200_ms, n200, NULL);
182}
183
184/*! initialize a LAPDm entity and all datalinks inside
185 * \param[in] le LAPDm entity
186 * \param[in] mode lapdm_mode (BTS/MS)
187 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
188 * \param[in] n200 N200 re-transmisison count
189 * \param[in] name human-readable name (will be copied internally + extended with SAPI)
190 */
191void lapdm_entity_init3(struct lapdm_entity *le, enum lapdm_mode mode,
192 const int *t200_ms, int n200, const char *name_pfx)
193{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200194 unsigned int i;
195
Harald Welte00b2faf2020-05-02 19:56:36 +0200196 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
197 char name[256];
198 if (name_pfx) {
199 snprintf(name, sizeof(name), "%s[%s]", name_pfx, i == 0 ? "0" : "3");
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100200 lapdm_dl_init(&le->datalink[i], le, (t200_ms) ? t200_ms[i] : 0, n200, name);
Harald Welte00b2faf2020-05-02 19:56:36 +0200201 } else
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100202 lapdm_dl_init(&le->datalink[i], le, (t200_ms) ? t200_ms[i] : 0, n200, NULL);
Andreas Eversbergf51f9162023-11-09 13:19:34 +0100203 INIT_LLIST_HEAD(&le->datalink[i].tx_ui_queue);
Harald Welte00b2faf2020-05-02 19:56:36 +0200204 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200205
206 lapdm_entity_set_mode(le, mode);
207}
208
Harald Welte20de6202019-06-02 21:33:38 +0200209static int get_n200_dcch(enum gsm_chan_t chan_t)
210{
211 switch (chan_t) {
212 case GSM_LCHAN_SDCCH:
213 return N200_TR_SDCCH;
214 case GSM_LCHAN_TCH_F:
215 return N200_TR_FACCH_FR;
216 case GSM_LCHAN_TCH_H:
217 return N200_TR_FACCH_HR;
218 default:
219 return -1;
220 }
221}
222
223/*! initialize a LAPDm channel and all its channels
224 * \param[in] lc lapdm_channel to be initialized
225 * \param[in] mode lapdm_mode (BTS/MS)
226 *
227 * Don't use this function; It doesn't support different T200 values per API
228 * and doesn't set the correct N200 counter, both of which
229 * are required by GSM specs and supported by lapdm_channel_init2().
230 */
231void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
232{
233 /* emulate old backwards-compatible behavior with 1s/2s */
234 const int t200_ms_dcch[_NR_DL_SAPI] = { 1000, 1000 };
235 const int t200_ms_acch[_NR_DL_SAPI] = { 2000, 2000 };
236
Harald Welte00b2faf2020-05-02 19:56:36 +0200237 lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, GSM_LCHAN_SDCCH, NULL);
Harald Welte20de6202019-06-02 21:33:38 +0200238}
239
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200240/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200241 * \param[in] lc \ref lapdm_channel to be initialized
242 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200243 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
244 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
245 * \param[in] chan_t GSM channel type (to correctly set N200)
Harald Welte6bdf0b12011-08-17 18:22:08 +0200246 */
Harald Welte20de6202019-06-02 21:33:38 +0200247int lapdm_channel_init2(struct lapdm_channel *lc, enum lapdm_mode mode,
248 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200249{
Harald Welte00b2faf2020-05-02 19:56:36 +0200250 return lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, chan_t, NULL);
251}
252
253/*! initialize a LAPDm channel and all its channels
254 * \param[in] lc \ref lapdm_channel to be initialized
255 * \param[in] mode \ref lapdm_mode (BTS/MS)
256 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
257 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
258 * \param[in] chan_t GSM channel type (to correctly set N200)
Vadim Yanitskiy64277a02023-02-28 03:30:27 +0700259 * \param[in] name_pfx human-readable name (copied by function + extended with ACCH/DCCH)
Harald Welte00b2faf2020-05-02 19:56:36 +0200260 */
261int lapdm_channel_init3(struct lapdm_channel *lc, enum lapdm_mode mode,
262 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t,
263 const char *name_pfx)
264{
Harald Welte20de6202019-06-02 21:33:38 +0200265 int n200_dcch = get_n200_dcch(chan_t);
Harald Welte00b2faf2020-05-02 19:56:36 +0200266 char namebuf[256];
267 char *name = NULL;
268
Harald Welte20de6202019-06-02 21:33:38 +0200269 if (n200_dcch < 0)
270 return -EINVAL;
271
Harald Welte00b2faf2020-05-02 19:56:36 +0200272 osmo_talloc_replace_string(tall_lapd_ctx, &lc->name, name_pfx);
273
274 if (name_pfx) {
275 snprintf(namebuf, sizeof(namebuf), "%s[ACCH]", name_pfx);
276 name = namebuf;
277 }
278 lapdm_entity_init3(&lc->lapdm_acch, mode, t200_ms_acch, N200_TR_SACCH, name);
Harald Welte3e8c5202018-05-04 20:58:48 +0200279 lc->lapdm_acch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200280
Harald Welte00b2faf2020-05-02 19:56:36 +0200281 if (name_pfx) {
282 snprintf(namebuf, sizeof(namebuf), "%s[DCCH]", name_pfx);
283 name = namebuf;
284 }
285 lapdm_entity_init3(&lc->lapdm_dcch, mode, t200_ms_dcch, n200_dcch, name);
Harald Welte3e8c5202018-05-04 20:58:48 +0200286 lc->lapdm_dcch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200287
288 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200289}
290
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200291/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200292void lapdm_entity_exit(struct lapdm_entity *le)
293{
294 unsigned int i;
295 struct lapdm_datalink *dl;
296
297 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
298 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200299 lapd_dl_exit(&dl->dl);
Andreas Eversbergf51f9162023-11-09 13:19:34 +0100300 msgb_queue_free(&dl->tx_ui_queue);
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) {
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100364 struct msgb *old_msg;
365
Andreas Eversberg285f3692023-11-29 12:03:00 +0100366 /* In 'RTS' mode there can be only one message. */
367 if (le->flags & LAPDM_ENT_F_RTS) {
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100368 /* Overwrite existing message by removing it first. */
369 if ((old_msg = msgb_dequeue(&dl->dl.tx_queue))) {
370 msgb_free(old_msg);
371 /* Reset V(S) to V(A), because there is no outstanding message now. */
372 dl->dl.v_send = dl->dl.v_ack;
373 }
374 }
375
rootaf48bed2011-09-26 11:23:06 +0200376 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200377 *msgb_push(msg, 1) = link_id;
378 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200379 msgb_enqueue(&dl->dl.tx_queue, msg);
Andreas Eversbergcb72e742023-06-07 13:18:08 +0200380 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200381 }
382
383 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
384 PRIM_OP_REQUEST, msg);
385 pp.u.data.chan_nr = chan_nr;
386 pp.u.data.link_id = link_id;
387
388 /* send the frame now */
389 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200390 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200391
392 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
393}
394
Andreas Eversbergf51f9162023-11-09 13:19:34 +0100395static int tx_ph_data_enqueue_ui(struct lapdm_datalink *dl, struct msgb *msg,
396 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
397{
398 struct lapdm_entity *le = dl->entity;
399 struct osmo_phsap_prim pp;
400
401 /* if there is a pending message, queue it */
402 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
403 *msgb_push(msg, 1) = pad;
404 *msgb_push(msg, 1) = link_id;
405 *msgb_push(msg, 1) = chan_nr;
406 msgb_enqueue(&dl->tx_ui_queue, msg);
407 return 0;
408 }
409
410 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
411 PRIM_OP_REQUEST, msg);
412 pp.u.data.chan_nr = chan_nr;
413 pp.u.data.link_id = link_id;
414
415 /* send the frame now */
416 le->tx_pending = 0; /* disabled flow control */
417 lapdm_pad_msgb(msg, pad);
418
419 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
420}
421
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100422/* Get transmit frame from queue, if any. In polling mode, indicate RTS to LAPD and start T200, if pending. */
423static struct msgb *tx_dequeue_msgb(struct lapdm_datalink *dl, uint32_t fn)
424{
425 struct msgb *msg;
426
Andreas Eversberg285f3692023-11-29 12:03:00 +0100427 /* Call RTS function of LAPD, to queue next frame. */
428 if (dl->entity->flags & LAPDM_ENT_F_RTS) {
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100429 struct lapd_msg_ctx lctx;
430 int rc;
431
432 /* Poll next frame. */
433 lctx.dl = &dl->dl;
434 rc = lapd_ph_rts_ind(&lctx);
435
436 /* If T200 has been started, calculate timeout FN. */
437 if (rc == 1) {
438 /* Set T200 in advance. */
439 dl->t200_timeout = fn;
440 ADD_MODULO(dl->t200_timeout, dl->t200_fn, GSM_MAX_FN);
441
442 LOGDL(&dl->dl, LOGL_INFO,
443 "T200 running from FN %"PRIu32" to FN %"PRIu32" (%"PRIu32" frames).\n",
444 fn, dl->t200_timeout, dl->t200_fn);
445 }
446 }
447
448 /* If there is no frame from LAPD, send UI frame, if any. */
449 msg = msgb_dequeue(&dl->dl.tx_queue);
450 if (msg)
451 LOGDL(&dl->dl, LOGL_INFO, "Sending frame from TX queue. (FN %"PRIu32")\n", fn);
Andreas Eversbergf51f9162023-11-09 13:19:34 +0100452 else {
453 msg = msgb_dequeue(&dl->tx_ui_queue);
454 if (msg)
455 LOGDL(&dl->dl, LOGL_INFO, "Sending UI frame from TX queue. (FN %"PRIu32")\n", fn);
456 }
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100457 return msg;
458}
459
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700460/* Dequeue a Downlink message for DCCH (dedicated channel) */
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100461static struct msgb *tx_dequeue_dcch_msgb(struct lapdm_entity *le, uint32_t fn)
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700462{
463 struct msgb *msg;
464
465 /* SAPI=0 always has higher priority than SAPI=3 */
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100466 msg = tx_dequeue_msgb(&le->datalink[DL_SAPI0], fn);
467 if (msg == NULL) { /* no SAPI=0 messages, dequeue SAPI=3 (if any) */
468 msg = tx_dequeue_msgb(&le->datalink[DL_SAPI3], fn);
469 }
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700470
471 return msg;
472}
473
474/* Dequeue a Downlink message for ACCH (associated channel) */
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100475static struct msgb *tx_dequeue_acch_msgb(struct lapdm_entity *le, uint32_t fn)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200476{
477 struct lapdm_datalink *dl;
478 int last = le->last_tx_dequeue;
479 int i = last, n = ARRAY_SIZE(le->datalink);
480 struct msgb *msg = NULL;
481
482 /* round-robin dequeue */
483 do {
484 /* next */
485 i = (i + 1) % n;
486 dl = &le->datalink[i];
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100487 if ((msg = tx_dequeue_msgb(dl, fn)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200488 break;
489 } while (i != last);
490
491 if (msg) {
492 /* Set last dequeue position */
493 le->last_tx_dequeue = i;
494 }
495
496 return msg;
497}
498
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200499/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200500 * a osmo_phsap_prim */
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100501int lapdm_phsap_dequeue_prim_fn(struct lapdm_entity *le, struct osmo_phsap_prim *pp, uint32_t fn)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200502{
503 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200504 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200505
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700506 /* Dequeue depending on channel type: DCCH or ACCH.
507 * See 3GPP TS 44.005, section 4.2.2 "Priority". */
508 if (le == &le->lapdm_ch->lapdm_dcch)
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100509 msg = tx_dequeue_dcch_msgb(le, fn);
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700510 else
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100511 msg = tx_dequeue_acch_msgb(le, fn);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200512 if (!msg)
513 return -ENODEV;
514
515 /* if we have a message, send PH-DATA.req */
516 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
517 PRIM_OP_REQUEST, msg);
518
519 /* Pull chan_nr and link_id */
520 pp->u.data.chan_nr = *msg->data;
521 msgb_pull(msg, 1);
522 pp->u.data.link_id = *msg->data;
523 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200524 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200525 msgb_pull(msg, 1);
526
527 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200528 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200529
530 return 0;
531}
532
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100533static void lapdm_t200_fn_dl(struct lapdm_datalink *dl, uint32_t fn)
534{
535 uint32_t diff;
536
537 OSMO_ASSERT((dl->dl.lapd_flags & LAPD_F_RTS));
538
539 /* If T200 is running, check if it has fired. */
540 if (dl->dl.t200_rts != LAPD_T200_RTS_RUNNING)
541 return;
542
543 /* Calculate how many frames fn is behind t200_timeout.
544 * If it is negative (>= GSM_MAX_FN / 2), we have not reached t200_timeout yet.
545 * If it is 0 or positive, we reached it or we are a bit too late, which is not a problem.
546 */
547 diff = fn;
548 ADD_MODULO(diff, GSM_MAX_FN - dl->t200_timeout, GSM_MAX_FN);
549 if (diff >= GSM_MAX_FN / 2)
550 return;
551
552 LOGDL(&dl->dl, LOGL_INFO, "T200 timeout at FN %"PRIu32", detected at FN %"PRIu32".\n", dl->t200_timeout, fn);
553
554 lapd_t200_timeout(&dl->dl);
555}
556
557/*! Get receive frame number from L1. It is used to check the T200 timeout.
558 * This function is used if LAPD is in RTS mode only. (Applies if the LAPDM_ENT_F_POLLING_ONLY flag is set.)
559 * This function must be called for every valid or invalid data frame received.
560 * The frame number fn must be the frame number of the first burst of a data frame.
561 * This function must be called after the frame is delivered to layer 2.
562 * In case of TCH, this this function must be called for every speech frame received, meaning that there was no valid
563 * data frame. */
564void lapdm_t200_fn(struct lapdm_entity *le, uint32_t fn)
565{
566 unsigned int i;
567
568 if (!(le->flags & LAPDM_ENT_F_POLLING_ONLY)) {
569 LOGP(DLLAPD, LOGL_ERROR, "Function call not allowed on timer based T200.\n");
570 return;
571 }
572
573 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
574 lapdm_t200_fn_dl(&le->datalink[i], fn);
575}
576
577/*! dequeue a msg that's pending transmission via L1 and wrap it into
578 * a osmo_phsap_prim */
579int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
580{
581 return lapdm_phsap_dequeue_prim_fn(le, pp, 0);
582}
583
Harald Welte1f0b8c22011-06-27 10:51:37 +0200584/* get next frame from the tx queue. because the ms has multiple datalinks,
585 * each datalink's queue is read round-robin.
586 */
587static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
588{
589 struct osmo_phsap_prim pp;
590
591 /* we may send again */
592 le->tx_pending = 0;
593
594 /* free confirm message */
595 if (msg)
596 msgb_free(msg);
597
598 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
599 /* no message in all queues */
600
601 /* If user didn't request PH-EMPTY_FRAME.req, abort */
602 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
603 return 0;
604
605 /* otherwise, send PH-EMPTY_FRAME.req */
606 osmo_prim_init(&pp.oph, SAP_GSM_PH,
607 PRIM_PH_EMPTY_FRAME,
608 PRIM_OP_REQUEST, NULL);
609 } else {
610 le->tx_pending = 1;
611 }
612
613 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
614}
615
Harald Welte542301b2018-04-19 16:11:14 +0200616/* Is a given msg_type "transparent" as per TS 48.058 Section 8.1 */
617static int rsl_is_transparent(uint8_t msg_type)
618{
619 switch (msg_type) {
620 case RSL_MT_DATA_IND:
621 case RSL_MT_UNIT_DATA_IND:
622 return 1;
623 case RSL_MT_DATA_REQ:
624 case RSL_MT_UNIT_DATA_REQ:
625 return 1;
626 default:
627 return 0;
628 }
629}
630
Harald Welte1f0b8c22011-06-27 10:51:37 +0200631/* Create RSLms various RSLms messages */
632static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
633 struct msgb *msg)
634{
Harald Welte542301b2018-04-19 16:11:14 +0200635 int transparent = rsl_is_transparent(msg_type);
636
Harald Welte1f0b8c22011-06-27 10:51:37 +0200637 /* Add the RSL + RLL header */
Pau Espin Pedrol06da40b2023-08-23 17:40:48 +0200638 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200639
640 /* send off the RSLms message to L3 */
641 return rslms_sendmsg(msg, mctx->dl->entity);
642}
643
644/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
645static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
646{
647 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200648
649 /* Add the RSL + RLL header */
650 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welted977f5f2018-05-08 21:53:28 +0200651
Harald Weltef1bdf782018-05-08 22:03:20 +0200652 /* Add two non-standard IEs carrying MS power and TA values for B4 (SACCH) */
653 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
654 msgb_tv_push(msg, RSL_IE_MS_POWER, mctx->tx_power_ind);
655 msgb_tv_push(msg, RSL_IE_TIMING_ADVANCE, mctx->ta_ind);
656 }
Harald Welted977f5f2018-05-08 21:53:28 +0200657
Harald Welte1f0b8c22011-06-27 10:51:37 +0200658 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
659 mctx->link_id, 1);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100660
Harald Welte1f0b8c22011-06-27 10:51:37 +0200661 return rslms_sendmsg(msg, mctx->dl->entity);
662}
663
664static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
665{
666 struct msgb *msg;
Harald Welte542301b2018-04-19 16:11:14 +0200667 int transparent = rsl_is_transparent(msg_type);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200668
Harald Welte542301b2018-04-19 16:11:14 +0200669 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200670
671 /* send off the RSLms message to L3 */
672 return rslms_sendmsg(msg, mctx->dl->entity);
673}
674
675static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
676{
677 struct msgb *msg;
678
Harald Welte00b2faf2020-05-02 19:56:36 +0200679 LOGDL(&mctx->dl->dl, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte542301b2018-04-19 16:11:14 +0200680 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200681 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
682 return rslms_sendmsg(msg, mctx->dl->entity);
683}
684
rootaf48bed2011-09-26 11:23:06 +0200685/* DLSAP L2 -> L3 (RSLms) */
686static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
687 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200688{
rootaf48bed2011-09-26 11:23:06 +0200689 struct lapd_datalink *dl = lctx->dl;
690 struct lapdm_datalink *mdl =
691 container_of(dl, struct lapdm_datalink, dl);
692 struct lapdm_msg_ctx *mctx = &mdl->mctx;
693 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200694
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200695 switch (OSMO_PRIM_HDR(&dp->oph)) {
696 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
697 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200698 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200699 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
700 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200701 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200702 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
703 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200704 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200705 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
706 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
707 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
708 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200709 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200710 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
711 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200712 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200713 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
714 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200715 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200716 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
717 rsl_rll_error(dp->u.error_ind.cause, mctx);
718 if (dp->oph.msg)
719 msgb_free(dp->oph.msg);
720 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200721 }
rootaf48bed2011-09-26 11:23:06 +0200722
723 if (!rll_msg) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200724 LOGDL(dl, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
rootaf48bed2011-09-26 11:23:06 +0200725 "fix!\n", dp->oph.primitive, dp->oph.operation);
726 return -EINVAL;
727 }
728
729 if (!dp->oph.msg)
730 return send_rll_simple(rll_msg, mctx);
731
732 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200733}
734
rootaf48bed2011-09-26 11:23:06 +0200735/* send a data frame to layer 1 */
736static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200737{
rootaf48bed2011-09-26 11:23:06 +0200738 uint8_t l3_len = msg->tail - msg->data;
739 struct lapd_datalink *dl = lctx->dl;
740 struct lapdm_datalink *mdl =
741 container_of(dl, struct lapdm_datalink, dl);
742 struct lapdm_msg_ctx *mctx = &mdl->mctx;
743 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200744
rootaf48bed2011-09-26 11:23:06 +0200745 /* prepend l2 header */
746 msg->l2h = msgb_push(msg, 3);
747 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
748 /* EA is set here too */
749 switch (format) {
750 case LAPD_FORM_I:
751 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
752 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200753 break;
rootaf48bed2011-09-26 11:23:06 +0200754 case LAPD_FORM_S:
755 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200756 break;
rootaf48bed2011-09-26 11:23:06 +0200757 case LAPD_FORM_U:
758 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200759 break;
760 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200761 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200762 return -EINVAL;
763 }
rootaf48bed2011-09-26 11:23:06 +0200764 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
765 if (lctx->more)
766 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200767
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100768 /* add ACCH header with last indicated tx-power and TA */
769 if ((mctx->link_id & 0x40)) {
770 struct lapdm_entity *le = mdl->entity;
771
772 msg->l2h = msgb_push(msg, 2);
773 msg->l2h[0] = le->tx_power;
774 msg->l2h[1] = le->ta;
775 }
776
rootaf48bed2011-09-26 11:23:06 +0200777 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
778 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200779}
780
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100781static int update_pending_frames(struct lapd_msg_ctx *lctx)
782{
783 struct lapd_datalink *dl = lctx->dl;
784 struct msgb *msg;
785 int rc = -1;
786
787 llist_for_each_entry(msg, &dl->tx_queue, list) {
788 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
789 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
790 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
791 rc = 0;
792 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
Andreas Eversberg1bb0b992023-11-09 12:09:21 +0100793 msg->l2h[1] = LAPDm_CTRL_S(dl->v_recv, LAPDm_CTRL_S_BITS(msg->l2h[1]),
794 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100795 }
796 }
797
798 return rc;
799}
800
Harald Welte1284c3e2018-05-01 18:11:02 +0200801/* determine if receiving a given LAPDm message is not permitted */
802static int lapdm_rx_not_permitted(const struct lapdm_entity *le,
803 const struct lapd_msg_ctx *lctx)
804{
805 /* we currently only implement SABM related checks here */
806 if (lctx->format != LAPD_FORM_U || lctx->s_u != LAPD_U_SABM)
807 return 0;
808
809 if (le->mode == LAPDM_MODE_BTS) {
810 if (le == &le->lapdm_ch->lapdm_acch) {
811 /* no contention resolution on SACCH */
812 if (lctx->length > 0)
813 return RLL_CAUSE_SABM_INFO_NOTALL;
814 } else {
815 switch (lctx->sapi) {
Harald Welte1284c3e2018-05-01 18:11:02 +0200816 case 3:
817 /* SAPI3 doesn't support contention resolution */
818 if (lctx->length > 0)
819 return RLL_CAUSE_SABM_INFO_NOTALL;
820 break;
Harald Welteb82a4072018-05-09 16:31:16 +0200821 default:
822 break;
Harald Welte1284c3e2018-05-01 18:11:02 +0200823 }
824 }
825 } else if (le->mode == LAPDM_MODE_MS) {
826 /* contention resolution (L3 present) is only sent by MS, but
827 * never received by it */
828 if (lctx->length > 0)
829 return RLL_CAUSE_SABM_INFO_NOTALL;
830 }
831 return 0;
832}
833
Harald Welte1f0b8c22011-06-27 10:51:37 +0200834/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200835static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
Pau Espin Pedrol1247aa12023-08-09 17:41:25 +0200836 uint8_t chan_nr, uint8_t link_id, uint32_t fn)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200837{
838 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200839 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200840 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200841 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200842 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200843 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200844
845 /* when we reach here, we have a msgb with l2h pointing to the raw
846 * 23byte mac block. The l1h has already been purged. */
847
rootaf48bed2011-09-26 11:23:06 +0200848 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200849 mctx.chan_nr = chan_nr;
850 mctx.link_id = link_id;
Pau Espin Pedrol1247aa12023-08-09 17:41:25 +0200851 mctx.fn = fn;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200852
Harald Welte1f0b8c22011-06-27 10:51:37 +0200853 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
854 if (cbits == 0x10 || cbits == 0x12) {
855 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
856 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200857 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200858 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200859 } else {
860 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200861 /* It was received from network on SACCH */
862
Andreas Eversberg03700182023-05-10 13:23:55 +0200863 /* A Short L3 header has both bits == 0. */
864 if (LAPDm_ADDR_SHORT_L2(msg->l2h[2]) == 0) {
865 mctx.lapdm_fmt = LAPDm_FMT_Bter;
866 n201 = N201_Bter_SACCH;
867 sapi = 0;
868 } else if (le->mode == LAPDM_MODE_MS
869 && LAPDm_CTRL_is_U(msg->l2h[3])
870 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
871 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
Harald Welte7ca604b2011-06-29 12:13:51 +0200872 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200873 n201 = N201_B4;
Andreas Eversberg03700182023-05-10 13:23:55 +0200874 /* sapi is found after two-btyte L1 header */
875 sapi = (msg->l2h[2] >> 2) & 7;
Harald Welte7ca604b2011-06-29 12:13:51 +0200876 } else {
877 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200878 n201 = N201_AB_SACCH;
Andreas Eversberg03700182023-05-10 13:23:55 +0200879 /* sapi is found after two-btyte L1 header */
880 sapi = (msg->l2h[2] >> 2) & 7;
Harald Welte7ca604b2011-06-29 12:13:51 +0200881 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200882 /* SACCH frames have a two-byte L1 header that
883 * OsmocomBB L1 doesn't strip */
884 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
885 mctx.ta_ind = msg->l2h[1];
886 msgb_pull(msg, 2);
887 msg->l2h += 2;
888 } else {
Andreas Eversberg03700182023-05-10 13:23:55 +0200889 /* A Short L3 header has both bits == 0. */
Andreas Eversberg16ad6c22023-09-08 19:31:06 +0200890 if (LAPDm_ADDR_SHORT_L2(msg->l2h[0]) == 0) {
Andreas Eversberg03700182023-05-10 13:23:55 +0200891 mctx.lapdm_fmt = LAPDm_FMT_Bter;
892 n201 = N201_Bter_SDCCH;
893 sapi = 0;
894 } else {
895 mctx.lapdm_fmt = LAPDm_FMT_B;
896 n201 = N201_AB_SDCCH;
897 sapi = (msg->l2h[0] >> 2) & 7;
898 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200899 }
900 }
901
Daniel Willmann55405fb2014-03-26 13:45:17 +0100902 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200903 /* G.2.1 No action on frames containing an unallocated SAPI. */
904 if (!mctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200905 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200906 msgb_free(msg);
907 return -EIO;
908 }
909
Harald Welte1f0b8c22011-06-27 10:51:37 +0200910 switch (mctx.lapdm_fmt) {
911 case LAPDm_FMT_A:
912 case LAPDm_FMT_B:
913 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200914 lctx.dl = &mctx.dl->dl;
915 /* obtain SAPI from address field */
916 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
917 /* G.2.3 EA bit set to "0" is not allowed in GSM */
918 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200919 LOGDL(lctx.dl, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200920 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200921 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200922 return -EINVAL;
923 }
rootaf48bed2011-09-26 11:23:06 +0200924 /* adress field */
925 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
926 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
927 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
928 /* command field */
929 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
930 lctx.format = LAPD_FORM_I;
931 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
932 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
933 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
934 lctx.format = LAPD_FORM_S;
935 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
936 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
937 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
938 lctx.format = LAPD_FORM_U;
939 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
940 } else
941 lctx.format = LAPD_FORM_UKN;
942 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
943 if (lctx.sapi != LAPDm_SAPI_NORMAL
944 && lctx.sapi != LAPDm_SAPI_SMS
945 && lctx.format == LAPD_FORM_U
946 && lctx.s_u == LAPDm_U_UI) {
947 /* 5.3.3 UI frames with invalid SAPI values shall be
948 * discarded
949 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200950 LOGDL(lctx.dl, LOGL_INFO, "sapi=%u (discarding)\n", lctx.sapi);
rootaf48bed2011-09-26 11:23:06 +0200951 msgb_free(msg);
952 return 0;
953 }
954 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
955 lctx.n201 = n201;
956 lctx.length = n201;
957 lctx.more = 0;
958 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100959 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200960 } else {
961 /* length field */
962 if (!(msg->l2h[2] & LAPDm_EL)) {
963 /* G.4.1 If the EL bit is set to "0", an
964 * MDL-ERROR-INDICATION primitive with cause
965 * "frame not implemented" is sent to the
966 * mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +0200967 LOGDL(lctx.dl, LOGL_NOTICE, "we don't support multi-octet length\n");
rootaf48bed2011-09-26 11:23:06 +0200968 msgb_free(msg);
969 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
970 return -EINVAL;
971 }
972 lctx.n201 = n201;
973 lctx.length = msg->l2h[2] >> 2;
974 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
975 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100976 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200977 }
978 /* store context for messages from lapd */
979 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200980 rc =lapdm_rx_not_permitted(le, &lctx);
981 if (rc > 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200982 LOGDL(lctx.dl, LOGL_NOTICE, "received message not permitted\n");
Harald Welte1284c3e2018-05-01 18:11:02 +0200983 msgb_free(msg);
984 rsl_rll_error(rc, &mctx);
985 return -EINVAL;
986 }
rootaf48bed2011-09-26 11:23:06 +0200987 /* send to LAPD */
Andreas Eversberg2d7119d2023-11-09 13:06:19 +0100988 LOGDL(lctx.dl, LOGL_DEBUG, "Frame received at FN %"PRIu32".\n", fn);
rootaf48bed2011-09-26 11:23:06 +0200989 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200990 break;
991 case LAPDm_FMT_Bter:
Andreas Eversberg03700182023-05-10 13:23:55 +0200992 /* fall-through */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200993 case LAPDm_FMT_Bbis:
Pau Espin Pedrol45ab0d72023-08-23 18:58:19 +0200994 /* Update context so that users can read fields like fn: */
995 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200996 /* directly pass up to layer3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200997 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100998 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200999 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
1000 break;
1001 default:
1002 msgb_free(msg);
1003 }
1004
1005 return rc;
1006}
1007
1008/* input into layer2 (from layer 1) */
1009static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
1010{
1011 struct abis_rsl_cchan_hdr *ch;
1012 struct gsm48_req_ref req_ref;
1013 struct gsm_time gt;
1014 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
1015
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +02001016 if (!msg)
1017 return -ENOMEM;
1018
Harald Welte1f0b8c22011-06-27 10:51:37 +02001019 msg->l2h = msgb_push(msg, sizeof(*ch));
1020 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1021 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
1022 ch->chan_nr = RSL_CHAN_RACH;
1023
1024 /* generate a RSL CHANNEL REQUIRED message */
1025 gsm_fn2gsmtime(&gt, fn);
1026 req_ref.ra = ra;
1027 req_ref.t1 = gt.t1; /* FIXME: modulo? */
1028 req_ref.t2 = gt.t2;
1029 req_ref.t3_low = gt.t3 & 7;
1030 req_ref.t3_high = gt.t3 >> 3;
1031
1032 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
1033 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
1034
1035 return rslms_sendmsg(msg, le);
1036}
1037
1038static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
1039
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001040/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001041int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
1042{
1043 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
1044 int rc = 0;
1045
1046 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +02001047 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001048 oph->sap);
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +07001049 msgb_free(oph->msg);
1050 return -ENODEV;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001051 }
1052
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +07001053 switch (OSMO_PRIM_HDR(oph)) {
1054 case OSMO_PRIM(PRIM_PH_DATA, PRIM_OP_INDICATION):
Harald Welte1f0b8c22011-06-27 10:51:37 +02001055 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
Pau Espin Pedrol1247aa12023-08-09 17:41:25 +02001056 pp->u.data.link_id, pp->u.data.fn);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001057 break;
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +07001058 case OSMO_PRIM(PRIM_PH_RTS, PRIM_OP_INDICATION):
Harald Welte1f0b8c22011-06-27 10:51:37 +02001059 rc = l2_ph_data_conf(oph->msg, le);
1060 break;
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +07001061 case OSMO_PRIM(PRIM_PH_RACH, PRIM_OP_INDICATION):
1062 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
1063 pp->u.rach_ind.acc_delay);
1064 break;
1065 case OSMO_PRIM(PRIM_PH_RACH, PRIM_OP_CONFIRM):
1066 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001067 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +01001068 default:
1069 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
1070 oph->primitive);
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +07001071 msgb_free(oph->msg);
1072 return -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001073 }
1074
1075 return rc;
1076}
1077
1078
1079/* L3 -> L2 / RSLMS -> LAPDm */
1080
rootaf48bed2011-09-26 11:23:06 +02001081/* Set LAPDm context for established connection */
1082static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
1083 uint8_t link_id, int n201, uint8_t sapi)
1084{
1085 memset(&dl->mctx, 0, sizeof(dl->mctx));
1086 dl->mctx.dl = dl;
1087 dl->mctx.chan_nr = chan_nr;
1088 dl->mctx.link_id = link_id;
1089 dl->dl.lctx.dl = &dl->dl;
1090 dl->dl.lctx.n201 = n201;
1091 dl->dl.lctx.sapi = sapi;
1092
1093 return 0;
1094}
1095
Harald Welte1f0b8c22011-06-27 10:51:37 +02001096/* L3 requests establishment of data link */
1097static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
1098{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001099 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1100 uint8_t chan_nr = rllh->chan_nr;
1101 uint8_t link_id = rllh->link_id;
1102 uint8_t sapi = rllh->link_id & 7;
1103 struct tlv_parsed tv;
1104 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001105 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001106 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001107
rootaf48bed2011-09-26 11:23:06 +02001108 /* Set LAPDm context for established connection */
1109 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001110
rootaf48bed2011-09-26 11:23:06 +02001111 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +02001112 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +02001113 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001114 /* contention resolution establishment procedure */
1115 if (sapi != 0) {
1116 /* According to clause 6, the contention resolution
1117 * procedure is only permitted with SAPI value 0 */
Harald Welte00b2faf2020-05-02 19:56:36 +02001118 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +02001119 "resolution (discarding)\n");
1120 msgb_free(msg);
1121 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1122 }
1123 /* transmit a SABM command with the P bit set to "1". The SABM
1124 * command shall contain the layer 3 message unit */
1125 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001126 } else {
1127 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +02001128 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001129 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001130 }
1131
1132 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001133 if (length > n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001134 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001135 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001136 msgb_free(msg);
1137 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1138 }
1139
rootaf48bed2011-09-26 11:23:06 +02001140 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001141 msgb_pull_to_l3(msg);
1142 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001143
rootaf48bed2011-09-26 11:23:06 +02001144 /* prepare prim */
1145 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001146
rootaf48bed2011-09-26 11:23:06 +02001147 /* send to L2 */
1148 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001149}
1150
1151/* L3 requests transfer of unnumbered information */
1152static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1153{
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001154 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001155 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1156 uint8_t chan_nr = rllh->chan_nr;
1157 uint8_t link_id = rllh->link_id;
1158 uint8_t sapi = link_id & 7;
1159 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +01001160 int length, ui_bts;
Andreas Eversberg67997412023-05-10 13:00:23 +02001161 bool use_b_ter;
Max777be2e2017-03-01 18:16:44 +01001162
1163 if (!le) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001164 LOGDL(&dl->dl, LOGL_ERROR, "lapdm_datalink without entity error\n");
Max777be2e2017-03-01 18:16:44 +01001165 msgb_free(msg);
1166 return -EMLINK;
1167 }
1168 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +02001169
1170 /* check if the layer3 message length exceeds N201 */
1171
1172 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1173
1174 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001175 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001176 }
1177 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001178 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001179 }
1180 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001181 LOGDL(&dl->dl, LOGL_ERROR, "unit data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001182 msgb_free(msg);
1183 return -EINVAL;
1184 }
rootaf48bed2011-09-26 11:23:06 +02001185 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001186 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Andreas Eversberg67997412023-05-10 13:00:23 +02001187 /* check for Bter frame */
1188 use_b_ter = (length == ((link_id & 0x40) ? 21 : 23) && sapi == 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001189 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg67997412023-05-10 13:00:23 +02001190 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23 && !use_b_ter) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001191 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +02001192 "(discarding)\n", length,
1193 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001194 msgb_free(msg);
1195 return -EIO;
1196 }
1197
Harald Welte00b2faf2020-05-02 19:56:36 +02001198 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 +02001199
rootaf48bed2011-09-26 11:23:06 +02001200 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001201 msgb_pull_to_l3(msg);
1202 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001203
1204 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg67997412023-05-10 13:00:23 +02001205 if (!use_b_ter) {
1206 msg->l2h = msgb_push(msg, 2 + !ui_bts);
1207 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
1208 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
1209 if (!ui_bts)
1210 msg->l2h[2] = LAPDm_LEN(length);
1211 } else
1212 msg->l2h = msg->data;
Andreas Eversberg5977db02013-06-12 09:34:51 +02001213 if (link_id & 0x40) {
1214 msg->l2h = msgb_push(msg, 2);
1215 msg->l2h[0] = le->tx_power;
1216 msg->l2h[1] = le->ta;
1217 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001218
1219 /* Tramsmit */
Andreas Eversbergf51f9162023-11-09 13:19:34 +01001220 return tx_ph_data_enqueue_ui(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001221}
1222
1223/* L3 requests transfer of acknowledged information */
1224static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1225{
1226 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1227 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +02001228 int length;
1229 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001230
1231 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1232 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001233 LOGDL(&dl->dl, LOGL_ERROR, "data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001234 msgb_free(msg);
1235 return -EINVAL;
1236 }
rootaf48bed2011-09-26 11:23:06 +02001237 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
1238 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001239
rootaf48bed2011-09-26 11:23:06 +02001240 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001241 msgb_pull_to_l3(msg);
1242 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001243
rootaf48bed2011-09-26 11:23:06 +02001244 /* prepare prim */
1245 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001246
rootaf48bed2011-09-26 11:23:06 +02001247 /* send to L2 */
1248 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001249}
1250
1251/* L3 requests suspension of data link */
1252static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1253{
1254 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1255 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001256 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001257
1258 if (sapi != 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001259 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001260 msgb_free(msg);
1261 return -EINVAL;
1262 }
1263
rootaf48bed2011-09-26 11:23:06 +02001264 /* prepare prim */
1265 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001266
rootaf48bed2011-09-26 11:23:06 +02001267 /* send to L2 */
1268 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001269}
1270
1271/* L3 requests resume of data link */
1272static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1273{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001274 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001275 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001276 uint8_t chan_nr = rllh->chan_nr;
1277 uint8_t link_id = rllh->link_id;
1278 uint8_t sapi = rllh->link_id & 7;
1279 struct tlv_parsed tv;
1280 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001281 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001282 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001283
rootaf48bed2011-09-26 11:23:06 +02001284 /* Set LAPDm context for established connection */
1285 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001286
1287 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1288 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001289 LOGDL(&dl->dl, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001290 msgb_free(msg);
1291 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1292 }
rootaf48bed2011-09-26 11:23:06 +02001293 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001294 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1295
rootaf48bed2011-09-26 11:23:06 +02001296 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001297 msgb_pull_to_l3(msg);
1298 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001299
rootaf48bed2011-09-26 11:23:06 +02001300 /* prepare prim */
1301 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1302 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001303
rootaf48bed2011-09-26 11:23:06 +02001304 /* send to L2 */
1305 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001306}
1307
1308/* L3 requests release of data link */
1309static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1310{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001311 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001312 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001313 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001314
1315 /* get release mode */
1316 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1317 mode = rllh->data[1] & 1;
1318
Harald Welte1f0b8c22011-06-27 10:51:37 +02001319 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001320 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001321
1322 /* 04.06 3.8.3: No information field is permitted with the DISC
1323 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001324 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001325
rootaf48bed2011-09-26 11:23:06 +02001326 /* prepare prim */
1327 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1328 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001329
rootaf48bed2011-09-26 11:23:06 +02001330 /* send to L2 */
1331 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001332}
1333
1334/* L3 requests channel in idle state */
1335static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1336{
1337 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1338 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1339 struct osmo_phsap_prim pp;
1340
1341 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1342 PRIM_OP_REQUEST, NULL);
1343
1344 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001345 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001346 return -EINVAL;
1347 }
1348 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001349 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001350 return -EINVAL;
1351 }
1352 pp.u.rach_req.ra = cch->data[1];
1353 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1354 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1355
1356 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001357 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001358 return -EINVAL;
1359 }
1360 /* TA = 0 - delay */
1361 pp.u.rach_req.ta = 0 - cch->data[5];
1362
1363 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001364 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001365 return -EINVAL;
1366 }
1367 pp.u.rach_req.tx_power = cch->data[7];
1368
1369 msgb_free(msg);
1370
1371 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1372}
1373
1374/* L1 confirms channel request */
1375static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1376{
1377 struct abis_rsl_cchan_hdr *ch;
1378 struct gsm_time tm;
1379 struct gsm48_req_ref *ref;
1380
1381 gsm_fn2gsmtime(&tm, frame_nr);
1382
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001383 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001384 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1385 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1386 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1387 ch->chan_nr = RSL_CHAN_RACH;
1388 ch->data[0] = RSL_IE_REQ_REFERENCE;
1389 ref = (struct gsm48_req_ref *) (ch->data + 1);
1390 ref->t1 = tm.t1;
1391 ref->t2 = tm.t2;
1392 ref->t3_low = tm.t3 & 0x7;
1393 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001394
Harald Welte1f0b8c22011-06-27 10:51:37 +02001395 return rslms_sendmsg(msg, le);
1396}
1397
Harald Welte1f0b8c22011-06-27 10:51:37 +02001398/* incoming RSLms RLL message from L3 */
1399static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1400{
1401 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1402 int msg_type = rllh->c.msg_type;
1403 uint8_t sapi = rllh->link_id & 7;
1404 struct lapdm_entity *le;
1405 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001406 int rc = 0;
1407
1408 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001409 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001410 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001411 return -EINVAL;
1412 }
1413
1414 if (rllh->link_id & 0x40)
1415 le = &lc->lapdm_acch;
1416 else
1417 le = &lc->lapdm_dcch;
1418
Harald Welte1a87c1b2015-12-14 15:26:07 +01001419 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1420 * BTS side */
1421 if (le->mode == LAPDM_MODE_BTS) {
1422 switch (msg_type) {
1423 case RSL_MT_SUSP_REQ:
1424 case RSL_MT_RES_REQ:
1425 case RSL_MT_RECON_REQ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001426 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' unsupported in BTS side LAPDm\n",
Harald Welte1a87c1b2015-12-14 15:26:07 +01001427 lc->name, rsl_msg_name(msg_type));
1428 msgb_free(msg);
1429 return -EINVAL;
1430 break;
1431 default:
1432 break;
1433 }
1434 }
1435
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001436 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001437 * SAPI.
1438 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001439 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001440 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001441 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001442 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001443 return -EINVAL;
1444 }
1445
Daniel Willmanne5233922012-12-25 23:15:50 +01001446 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001447 case RSL_MT_DATA_REQ:
1448 case RSL_MT_SUSP_REQ:
1449 case RSL_MT_REL_REQ:
1450 /* This is triggered in abnormal error conditions where
1451 * set_lapdm_context() was not called for the channel earlier. */
1452 if (!dl->dl.lctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001453 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001454 lc->name, rsl_msg_name(msg_type), sapi);
1455 msgb_free(msg);
1456 return -EINVAL;
1457 }
1458 break;
1459 default:
Harald Welte00b2faf2020-05-02 19:56:36 +02001460 LOGP(DLLAPD, LOGL_INFO, "(%s) RLL Message '%s' received. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001461 lc->name, rsl_msg_name(msg_type), sapi);
1462 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001463
rootaf48bed2011-09-26 11:23:06 +02001464 switch (msg_type) {
1465 case RSL_MT_UNIT_DATA_REQ:
1466 rc = rslms_rx_rll_udata_req(msg, dl);
1467 break;
1468 case RSL_MT_EST_REQ:
1469 rc = rslms_rx_rll_est_req(msg, dl);
1470 break;
1471 case RSL_MT_DATA_REQ:
1472 rc = rslms_rx_rll_data_req(msg, dl);
1473 break;
1474 case RSL_MT_SUSP_REQ:
1475 rc = rslms_rx_rll_susp_req(msg, dl);
1476 break;
1477 case RSL_MT_RES_REQ:
1478 rc = rslms_rx_rll_res_req(msg, dl);
1479 break;
1480 case RSL_MT_RECON_REQ:
1481 rc = rslms_rx_rll_res_req(msg, dl);
1482 break;
1483 case RSL_MT_REL_REQ:
1484 rc = rslms_rx_rll_rel_req(msg, dl);
1485 break;
1486 default:
1487 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001488 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001489 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001490 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001491
1492 return rc;
1493}
1494
1495/* incoming RSLms COMMON CHANNEL message from L3 */
1496static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1497{
1498 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1499 int msg_type = cch->c.msg_type;
1500 int rc = 0;
1501
1502 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001503 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001504 return -EINVAL;
1505 }
1506
1507 switch (msg_type) {
1508 case RSL_MT_CHAN_RQD:
1509 /* create and send RACH request */
1510 rc = rslms_rx_chan_rqd(lc, msg);
1511 break;
1512 default:
rootaf48bed2011-09-26 11:23:06 +02001513 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001514 msg_type);
1515 msgb_free(msg);
1516 return 0;
1517 }
1518
1519 return rc;
1520}
1521
Harald Welte7023aa02019-05-19 12:17:06 +02001522/*! Receive a RSLms \ref msgb from Layer 3. 'msg' ownership is transferred,
1523 * i.e. caller must not free it */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001524int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1525{
1526 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1527 int rc = 0;
1528
1529 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001530 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte7023aa02019-05-19 12:17:06 +02001531 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001532 return -EINVAL;
1533 }
1534
1535 switch (rslh->msg_discr & 0xfe) {
1536 case ABIS_RSL_MDISC_RLL:
1537 rc = rslms_rx_rll(msg, lc);
1538 break;
1539 case ABIS_RSL_MDISC_COM_CHAN:
1540 rc = rslms_rx_com_chan(msg, lc);
1541 break;
1542 default:
rootaf48bed2011-09-26 11:23:06 +02001543 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001544 "discriminator 0x%02x", rslh->msg_discr);
1545 msgb_free(msg);
1546 return -EINVAL;
1547 }
1548
1549 return rc;
1550}
1551
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001552/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001553int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1554{
rootaf48bed2011-09-26 11:23:06 +02001555 int i;
1556 enum lapd_mode lm;
1557
Harald Welte1f0b8c22011-06-27 10:51:37 +02001558 switch (mode) {
1559 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001560 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001561 break;
1562 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001563 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001564 break;
1565 default:
1566 return -EINVAL;
1567 }
1568
rootaf48bed2011-09-26 11:23:06 +02001569 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1570 lapd_set_mode(&le->datalink[i].dl, lm);
1571 }
1572
Harald Welte1f0b8c22011-06-27 10:51:37 +02001573 le->mode = mode;
1574
1575 return 0;
1576}
1577
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001578/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001579int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1580{
1581 int rc;
1582
1583 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1584 if (rc < 0)
1585 return rc;
1586
1587 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1588}
1589
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001590/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001591void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1592{
1593 lc->lapdm_dcch.l1_prim_cb = cb;
1594 lc->lapdm_acch.l1_prim_cb = cb;
1595 lc->lapdm_dcch.l1_ctx = ctx;
1596 lc->lapdm_acch.l1_ctx = ctx;
1597}
1598
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001599/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001600void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1601{
1602 lc->lapdm_dcch.l3_cb = cb;
1603 lc->lapdm_acch.l3_cb = cb;
1604 lc->lapdm_dcch.l3_ctx = ctx;
1605 lc->lapdm_acch.l3_ctx = ctx;
1606}
1607
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001608/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001609void lapdm_entity_reset(struct lapdm_entity *le)
1610{
1611 struct lapdm_datalink *dl;
1612 int i;
1613
1614 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1615 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001616 lapd_dl_reset(&dl->dl);
Andreas Eversbergf51f9162023-11-09 13:19:34 +01001617 msgb_queue_free(&dl->tx_ui_queue);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001618 }
1619}
1620
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001621/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001622void lapdm_channel_reset(struct lapdm_channel *lc)
1623{
1624 lapdm_entity_reset(&lc->lapdm_dcch);
1625 lapdm_entity_reset(&lc->lapdm_acch);
1626}
1627
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001628/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001629void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1630{
Andreas Eversberg2d7119d2023-11-09 13:06:19 +01001631 unsigned int dl_flags = 0;
1632 struct lapdm_datalink *dl;
1633 int i;
1634
Harald Welte1f0b8c22011-06-27 10:51:37 +02001635 le->flags = flags;
Andreas Eversberg2d7119d2023-11-09 13:06:19 +01001636
1637 /* Set flags at LAPD. */
Andreas Eversberg285f3692023-11-29 12:03:00 +01001638 if (le->flags & LAPDM_ENT_F_RTS)
Andreas Eversberg2d7119d2023-11-09 13:06:19 +01001639 dl_flags |= LAPD_F_RTS;
Andreas Eversbergbd2b8972023-11-15 14:33:53 +01001640 if (le->flags & LAPDM_ENT_F_DROP_2ND_REJ)
1641 dl_flags |= LAPD_F_DROP_2ND_REJ;
Andreas Eversberg2d7119d2023-11-09 13:06:19 +01001642
1643 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1644 dl = &le->datalink[i];
1645 lapd_dl_set_flags(&dl->dl, dl_flags);
1646 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001647}
1648
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001649/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001650void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1651{
1652 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1653 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1654}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001655
Andreas Eversberg2d7119d2023-11-09 13:06:19 +01001656/*! Set the T200 FN timer of a LAPDm entity
1657 * \param[in] \ref lapdm_entity
1658 * \param[in] t200_fn Array of T200 timeout in frame numbers for all SAPIs (0, 3) */
1659void lapdm_entity_set_t200_fn(struct lapdm_entity *le, const uint32_t *t200_fn)
1660{
1661 unsigned int i;
1662
1663 for (i = 0; i < ARRAY_SIZE(le->datalink); i++)
1664 le->datalink[i].t200_fn = t200_fn[i];
1665}
1666
1667/*! Set the T200 FN timer of all LAPDm entities in a LAPDm channel
1668 * \param[in] \ref lapdm_channel
1669 * \param[in] t200_fn_dcch Array of T200 timeout in frame numbers for all SAPIs (0, 3) on SDCCH/FACCH
1670 * \param[in] t200_fn_acch Array of T200 timeout in frame numbers for all SAPIs (0, 3) on SACCH */
1671void lapdm_channel_set_t200_fn(struct lapdm_channel *lc, const uint32_t *t200_fn_dcch, const uint32_t *t200_fn_acch)
1672{
1673 lapdm_entity_set_t200_fn(&lc->lapdm_dcch, t200_fn_dcch);
1674 lapdm_entity_set_t200_fn(&lc->lapdm_acch, t200_fn_acch);
1675}
1676
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001677/*! @} */