blob: bfd6d2633447ae34e6c1ff13a2da9b21dcbd16dd [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;
738 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200739 } else {
740 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200741 n201 = N201_AB_SACCH;
742 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200743 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200744 /* SACCH frames have a two-byte L1 header that
745 * OsmocomBB L1 doesn't strip */
746 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
747 mctx.ta_ind = msg->l2h[1];
748 msgb_pull(msg, 2);
749 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200750 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200751 } else {
752 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200753 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100754 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200755 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200756 }
757 }
758
Daniel Willmann55405fb2014-03-26 13:45:17 +0100759 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200760 /* G.2.1 No action on frames containing an unallocated SAPI. */
761 if (!mctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200762 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200763 msgb_free(msg);
764 return -EIO;
765 }
766
Harald Welte1f0b8c22011-06-27 10:51:37 +0200767 switch (mctx.lapdm_fmt) {
768 case LAPDm_FMT_A:
769 case LAPDm_FMT_B:
770 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200771 lctx.dl = &mctx.dl->dl;
772 /* obtain SAPI from address field */
773 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
774 /* G.2.3 EA bit set to "0" is not allowed in GSM */
775 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200776 LOGDL(lctx.dl, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200777 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200778 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200779 return -EINVAL;
780 }
rootaf48bed2011-09-26 11:23:06 +0200781 /* adress field */
782 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
783 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
784 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
785 /* command field */
786 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
787 lctx.format = LAPD_FORM_I;
788 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
789 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
790 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
791 lctx.format = LAPD_FORM_S;
792 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
793 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
794 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
795 lctx.format = LAPD_FORM_U;
796 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
797 } else
798 lctx.format = LAPD_FORM_UKN;
799 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
800 if (lctx.sapi != LAPDm_SAPI_NORMAL
801 && lctx.sapi != LAPDm_SAPI_SMS
802 && lctx.format == LAPD_FORM_U
803 && lctx.s_u == LAPDm_U_UI) {
804 /* 5.3.3 UI frames with invalid SAPI values shall be
805 * discarded
806 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200807 LOGDL(lctx.dl, LOGL_INFO, "sapi=%u (discarding)\n", lctx.sapi);
rootaf48bed2011-09-26 11:23:06 +0200808 msgb_free(msg);
809 return 0;
810 }
811 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
812 lctx.n201 = n201;
813 lctx.length = n201;
814 lctx.more = 0;
815 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100816 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200817 } else {
818 /* length field */
819 if (!(msg->l2h[2] & LAPDm_EL)) {
820 /* G.4.1 If the EL bit is set to "0", an
821 * MDL-ERROR-INDICATION primitive with cause
822 * "frame not implemented" is sent to the
823 * mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +0200824 LOGDL(lctx.dl, LOGL_NOTICE, "we don't support multi-octet length\n");
rootaf48bed2011-09-26 11:23:06 +0200825 msgb_free(msg);
826 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
827 return -EINVAL;
828 }
829 lctx.n201 = n201;
830 lctx.length = msg->l2h[2] >> 2;
831 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
832 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100833 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200834 }
835 /* store context for messages from lapd */
836 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200837 rc =lapdm_rx_not_permitted(le, &lctx);
838 if (rc > 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200839 LOGDL(lctx.dl, LOGL_NOTICE, "received message not permitted\n");
Harald Welte1284c3e2018-05-01 18:11:02 +0200840 msgb_free(msg);
841 rsl_rll_error(rc, &mctx);
842 return -EINVAL;
843 }
rootaf48bed2011-09-26 11:23:06 +0200844 /* send to LAPD */
845 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200846 break;
847 case LAPDm_FMT_Bter:
848 /* FIXME */
849 msgb_free(msg);
850 break;
851 case LAPDm_FMT_Bbis:
852 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200853 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200854 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100855 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200856 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
857 break;
858 default:
859 msgb_free(msg);
860 }
861
862 return rc;
863}
864
865/* input into layer2 (from layer 1) */
866static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
867{
868 struct abis_rsl_cchan_hdr *ch;
869 struct gsm48_req_ref req_ref;
870 struct gsm_time gt;
871 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
872
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200873 if (!msg)
874 return -ENOMEM;
875
Harald Welte1f0b8c22011-06-27 10:51:37 +0200876 msg->l2h = msgb_push(msg, sizeof(*ch));
877 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
878 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
879 ch->chan_nr = RSL_CHAN_RACH;
880
881 /* generate a RSL CHANNEL REQUIRED message */
882 gsm_fn2gsmtime(&gt, fn);
883 req_ref.ra = ra;
884 req_ref.t1 = gt.t1; /* FIXME: modulo? */
885 req_ref.t2 = gt.t2;
886 req_ref.t3_low = gt.t3 & 7;
887 req_ref.t3_high = gt.t3 >> 3;
888
889 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
890 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
891
892 return rslms_sendmsg(msg, le);
893}
894
895static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
896
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200897/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200898int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
899{
900 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
901 int rc = 0;
902
903 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200904 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200905 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100906 rc = -ENODEV;
907 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200908 }
909
910 switch (oph->primitive) {
911 case PRIM_PH_DATA:
912 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200913 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200914 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100915 rc = -ENODEV;
916 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200917 }
918 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
919 pp->u.data.link_id);
920 break;
921 case PRIM_PH_RTS:
922 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200923 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200924 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100925 rc = -ENODEV;
926 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200927 }
928 rc = l2_ph_data_conf(oph->msg, le);
929 break;
930 case PRIM_PH_RACH:
931 switch (oph->operation) {
932 case PRIM_OP_INDICATION:
933 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
934 pp->u.rach_ind.acc_delay);
935 break;
936 case PRIM_OP_CONFIRM:
937 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
938 break;
939 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100940 rc = -EIO;
941 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200942 }
943 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100944 default:
945 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
946 oph->primitive);
947 rc = -EINVAL;
948 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200949 }
950
951 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100952
953free:
954 msgb_free(oph->msg);
955 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200956}
957
958
959/* L3 -> L2 / RSLMS -> LAPDm */
960
rootaf48bed2011-09-26 11:23:06 +0200961/* Set LAPDm context for established connection */
962static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
963 uint8_t link_id, int n201, uint8_t sapi)
964{
965 memset(&dl->mctx, 0, sizeof(dl->mctx));
966 dl->mctx.dl = dl;
967 dl->mctx.chan_nr = chan_nr;
968 dl->mctx.link_id = link_id;
969 dl->dl.lctx.dl = &dl->dl;
970 dl->dl.lctx.n201 = n201;
971 dl->dl.lctx.sapi = sapi;
972
973 return 0;
974}
975
Harald Welte1f0b8c22011-06-27 10:51:37 +0200976/* L3 requests establishment of data link */
977static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
978{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200979 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
980 uint8_t chan_nr = rllh->chan_nr;
981 uint8_t link_id = rllh->link_id;
982 uint8_t sapi = rllh->link_id & 7;
983 struct tlv_parsed tv;
984 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100985 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200986 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200987
rootaf48bed2011-09-26 11:23:06 +0200988 /* Set LAPDm context for established connection */
989 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200990
rootaf48bed2011-09-26 11:23:06 +0200991 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200992 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200993 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200994 /* contention resolution establishment procedure */
995 if (sapi != 0) {
996 /* According to clause 6, the contention resolution
997 * procedure is only permitted with SAPI value 0 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200998 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200999 "resolution (discarding)\n");
1000 msgb_free(msg);
1001 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1002 }
1003 /* transmit a SABM command with the P bit set to "1". The SABM
1004 * command shall contain the layer 3 message unit */
1005 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001006 } else {
1007 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +02001008 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001009 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001010 }
1011
1012 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001013 if (length > n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001014 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001015 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001016 msgb_free(msg);
1017 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1018 }
1019
rootaf48bed2011-09-26 11:23:06 +02001020 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001021 msgb_pull_to_l3(msg);
1022 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001023
rootaf48bed2011-09-26 11:23:06 +02001024 /* prepare prim */
1025 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001026
rootaf48bed2011-09-26 11:23:06 +02001027 /* send to L2 */
1028 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001029}
1030
1031/* L3 requests transfer of unnumbered information */
1032static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1033{
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001034 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001035 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1036 uint8_t chan_nr = rllh->chan_nr;
1037 uint8_t link_id = rllh->link_id;
1038 uint8_t sapi = link_id & 7;
1039 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +01001040 int length, ui_bts;
1041
1042 if (!le) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001043 LOGDL(&dl->dl, LOGL_ERROR, "lapdm_datalink without entity error\n");
Max777be2e2017-03-01 18:16:44 +01001044 msgb_free(msg);
1045 return -EMLINK;
1046 }
1047 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +02001048
1049 /* check if the layer3 message length exceeds N201 */
1050
1051 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1052
1053 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001054 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001055 }
1056 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001057 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001058 }
1059 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001060 LOGDL(&dl->dl, LOGL_ERROR, "unit data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001061 msgb_free(msg);
1062 return -EINVAL;
1063 }
rootaf48bed2011-09-26 11:23:06 +02001064 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001065 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1066 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +02001067 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001068 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +02001069 "(discarding)\n", length,
1070 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001071 msgb_free(msg);
1072 return -EIO;
1073 }
1074
Harald Welte00b2faf2020-05-02 19:56:36 +02001075 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 +02001076
rootaf48bed2011-09-26 11:23:06 +02001077 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001078 msgb_pull_to_l3(msg);
1079 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001080
1081 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +02001082 msg->l2h = msgb_push(msg, 2 + !ui_bts);
1083 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
1084 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001085 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +02001086 msg->l2h[2] = LAPDm_LEN(length);
1087 if (link_id & 0x40) {
1088 msg->l2h = msgb_push(msg, 2);
1089 msg->l2h[0] = le->tx_power;
1090 msg->l2h[1] = le->ta;
1091 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001092
1093 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +02001094 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001095}
1096
1097/* L3 requests transfer of acknowledged information */
1098static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1099{
1100 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1101 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +02001102 int length;
1103 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001104
1105 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1106 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001107 LOGDL(&dl->dl, LOGL_ERROR, "data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001108 msgb_free(msg);
1109 return -EINVAL;
1110 }
rootaf48bed2011-09-26 11:23:06 +02001111 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
1112 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001113
rootaf48bed2011-09-26 11:23:06 +02001114 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001115 msgb_pull_to_l3(msg);
1116 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001117
rootaf48bed2011-09-26 11:23:06 +02001118 /* prepare prim */
1119 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001120
rootaf48bed2011-09-26 11:23:06 +02001121 /* send to L2 */
1122 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001123}
1124
1125/* L3 requests suspension of data link */
1126static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1127{
1128 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1129 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001130 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001131
1132 if (sapi != 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001133 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001134 msgb_free(msg);
1135 return -EINVAL;
1136 }
1137
rootaf48bed2011-09-26 11:23:06 +02001138 /* prepare prim */
1139 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001140
rootaf48bed2011-09-26 11:23:06 +02001141 /* send to L2 */
1142 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001143}
1144
1145/* L3 requests resume of data link */
1146static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1147{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001148 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001149 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001150 uint8_t chan_nr = rllh->chan_nr;
1151 uint8_t link_id = rllh->link_id;
1152 uint8_t sapi = rllh->link_id & 7;
1153 struct tlv_parsed tv;
1154 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001155 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001156 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001157
rootaf48bed2011-09-26 11:23:06 +02001158 /* Set LAPDm context for established connection */
1159 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001160
1161 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1162 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001163 LOGDL(&dl->dl, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001164 msgb_free(msg);
1165 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1166 }
rootaf48bed2011-09-26 11:23:06 +02001167 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001168 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1169
rootaf48bed2011-09-26 11:23:06 +02001170 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001171 msgb_pull_to_l3(msg);
1172 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001173
rootaf48bed2011-09-26 11:23:06 +02001174 /* prepare prim */
1175 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1176 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001177
rootaf48bed2011-09-26 11:23:06 +02001178 /* send to L2 */
1179 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001180}
1181
1182/* L3 requests release of data link */
1183static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1184{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001185 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001186 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001187 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001188
1189 /* get release mode */
1190 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1191 mode = rllh->data[1] & 1;
1192
Harald Welte1f0b8c22011-06-27 10:51:37 +02001193 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001194 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001195
1196 /* 04.06 3.8.3: No information field is permitted with the DISC
1197 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001198 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001199
rootaf48bed2011-09-26 11:23:06 +02001200 /* prepare prim */
1201 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1202 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001203
rootaf48bed2011-09-26 11:23:06 +02001204 /* send to L2 */
1205 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001206}
1207
1208/* L3 requests channel in idle state */
1209static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1210{
1211 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1212 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1213 struct osmo_phsap_prim pp;
1214
1215 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1216 PRIM_OP_REQUEST, NULL);
1217
1218 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001219 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001220 return -EINVAL;
1221 }
1222 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001223 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001224 return -EINVAL;
1225 }
1226 pp.u.rach_req.ra = cch->data[1];
1227 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1228 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1229
1230 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001231 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001232 return -EINVAL;
1233 }
1234 /* TA = 0 - delay */
1235 pp.u.rach_req.ta = 0 - cch->data[5];
1236
1237 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001238 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001239 return -EINVAL;
1240 }
1241 pp.u.rach_req.tx_power = cch->data[7];
1242
1243 msgb_free(msg);
1244
1245 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1246}
1247
1248/* L1 confirms channel request */
1249static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1250{
1251 struct abis_rsl_cchan_hdr *ch;
1252 struct gsm_time tm;
1253 struct gsm48_req_ref *ref;
1254
1255 gsm_fn2gsmtime(&tm, frame_nr);
1256
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001257 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001258 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1259 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1260 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1261 ch->chan_nr = RSL_CHAN_RACH;
1262 ch->data[0] = RSL_IE_REQ_REFERENCE;
1263 ref = (struct gsm48_req_ref *) (ch->data + 1);
1264 ref->t1 = tm.t1;
1265 ref->t2 = tm.t2;
1266 ref->t3_low = tm.t3 & 0x7;
1267 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001268
Harald Welte1f0b8c22011-06-27 10:51:37 +02001269 return rslms_sendmsg(msg, le);
1270}
1271
Harald Welte1f0b8c22011-06-27 10:51:37 +02001272/* incoming RSLms RLL message from L3 */
1273static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1274{
1275 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1276 int msg_type = rllh->c.msg_type;
1277 uint8_t sapi = rllh->link_id & 7;
1278 struct lapdm_entity *le;
1279 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001280 int rc = 0;
1281
1282 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001283 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001284 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001285 return -EINVAL;
1286 }
1287
1288 if (rllh->link_id & 0x40)
1289 le = &lc->lapdm_acch;
1290 else
1291 le = &lc->lapdm_dcch;
1292
Harald Welte1a87c1b2015-12-14 15:26:07 +01001293 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1294 * BTS side */
1295 if (le->mode == LAPDM_MODE_BTS) {
1296 switch (msg_type) {
1297 case RSL_MT_SUSP_REQ:
1298 case RSL_MT_RES_REQ:
1299 case RSL_MT_RECON_REQ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001300 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' unsupported in BTS side LAPDm\n",
Harald Welte1a87c1b2015-12-14 15:26:07 +01001301 lc->name, rsl_msg_name(msg_type));
1302 msgb_free(msg);
1303 return -EINVAL;
1304 break;
1305 default:
1306 break;
1307 }
1308 }
1309
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001310 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001311 * SAPI.
1312 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001313 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001314 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001315 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001316 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001317 return -EINVAL;
1318 }
1319
Daniel Willmanne5233922012-12-25 23:15:50 +01001320 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001321 case RSL_MT_DATA_REQ:
1322 case RSL_MT_SUSP_REQ:
1323 case RSL_MT_REL_REQ:
1324 /* This is triggered in abnormal error conditions where
1325 * set_lapdm_context() was not called for the channel earlier. */
1326 if (!dl->dl.lctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001327 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001328 lc->name, rsl_msg_name(msg_type), sapi);
1329 msgb_free(msg);
1330 return -EINVAL;
1331 }
1332 break;
1333 default:
Harald Welte00b2faf2020-05-02 19:56:36 +02001334 LOGP(DLLAPD, LOGL_INFO, "(%s) RLL Message '%s' received. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001335 lc->name, rsl_msg_name(msg_type), sapi);
1336 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001337
rootaf48bed2011-09-26 11:23:06 +02001338 switch (msg_type) {
1339 case RSL_MT_UNIT_DATA_REQ:
1340 rc = rslms_rx_rll_udata_req(msg, dl);
1341 break;
1342 case RSL_MT_EST_REQ:
1343 rc = rslms_rx_rll_est_req(msg, dl);
1344 break;
1345 case RSL_MT_DATA_REQ:
1346 rc = rslms_rx_rll_data_req(msg, dl);
1347 break;
1348 case RSL_MT_SUSP_REQ:
1349 rc = rslms_rx_rll_susp_req(msg, dl);
1350 break;
1351 case RSL_MT_RES_REQ:
1352 rc = rslms_rx_rll_res_req(msg, dl);
1353 break;
1354 case RSL_MT_RECON_REQ:
1355 rc = rslms_rx_rll_res_req(msg, dl);
1356 break;
1357 case RSL_MT_REL_REQ:
1358 rc = rslms_rx_rll_rel_req(msg, dl);
1359 break;
1360 default:
1361 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001362 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001363 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001364 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001365
1366 return rc;
1367}
1368
1369/* incoming RSLms COMMON CHANNEL message from L3 */
1370static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1371{
1372 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1373 int msg_type = cch->c.msg_type;
1374 int rc = 0;
1375
1376 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001377 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001378 return -EINVAL;
1379 }
1380
1381 switch (msg_type) {
1382 case RSL_MT_CHAN_RQD:
1383 /* create and send RACH request */
1384 rc = rslms_rx_chan_rqd(lc, msg);
1385 break;
1386 default:
rootaf48bed2011-09-26 11:23:06 +02001387 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001388 msg_type);
1389 msgb_free(msg);
1390 return 0;
1391 }
1392
1393 return rc;
1394}
1395
Harald Welte7023aa02019-05-19 12:17:06 +02001396/*! Receive a RSLms \ref msgb from Layer 3. 'msg' ownership is transferred,
1397 * i.e. caller must not free it */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001398int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1399{
1400 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1401 int rc = 0;
1402
1403 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001404 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte7023aa02019-05-19 12:17:06 +02001405 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001406 return -EINVAL;
1407 }
1408
1409 switch (rslh->msg_discr & 0xfe) {
1410 case ABIS_RSL_MDISC_RLL:
1411 rc = rslms_rx_rll(msg, lc);
1412 break;
1413 case ABIS_RSL_MDISC_COM_CHAN:
1414 rc = rslms_rx_com_chan(msg, lc);
1415 break;
1416 default:
rootaf48bed2011-09-26 11:23:06 +02001417 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001418 "discriminator 0x%02x", rslh->msg_discr);
1419 msgb_free(msg);
1420 return -EINVAL;
1421 }
1422
1423 return rc;
1424}
1425
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001426/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001427int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1428{
rootaf48bed2011-09-26 11:23:06 +02001429 int i;
1430 enum lapd_mode lm;
1431
Harald Welte1f0b8c22011-06-27 10:51:37 +02001432 switch (mode) {
1433 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001434 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001435 break;
1436 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001437 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001438 break;
1439 default:
1440 return -EINVAL;
1441 }
1442
rootaf48bed2011-09-26 11:23:06 +02001443 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1444 lapd_set_mode(&le->datalink[i].dl, lm);
1445 }
1446
Harald Welte1f0b8c22011-06-27 10:51:37 +02001447 le->mode = mode;
1448
1449 return 0;
1450}
1451
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001452/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001453int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1454{
1455 int rc;
1456
1457 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1458 if (rc < 0)
1459 return rc;
1460
1461 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1462}
1463
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001464/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001465void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1466{
1467 lc->lapdm_dcch.l1_prim_cb = cb;
1468 lc->lapdm_acch.l1_prim_cb = cb;
1469 lc->lapdm_dcch.l1_ctx = ctx;
1470 lc->lapdm_acch.l1_ctx = ctx;
1471}
1472
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001473/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001474void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1475{
1476 lc->lapdm_dcch.l3_cb = cb;
1477 lc->lapdm_acch.l3_cb = cb;
1478 lc->lapdm_dcch.l3_ctx = ctx;
1479 lc->lapdm_acch.l3_ctx = ctx;
1480}
1481
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001482/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001483void lapdm_entity_reset(struct lapdm_entity *le)
1484{
1485 struct lapdm_datalink *dl;
1486 int i;
1487
1488 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1489 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001490 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001491 }
1492}
1493
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001494/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001495void lapdm_channel_reset(struct lapdm_channel *lc)
1496{
1497 lapdm_entity_reset(&lc->lapdm_dcch);
1498 lapdm_entity_reset(&lc->lapdm_acch);
1499}
1500
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001501/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001502void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1503{
1504 le->flags = flags;
1505}
1506
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001507/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001508void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1509{
1510 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1511 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1512}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001513
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001514/*! @} */