blob: e96e218244379bee552e05089e84f327982ab689 [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>
30#include <string.h>
31#include <errno.h>
Harald Welte1f0b8c22011-06-27 10:51:37 +020032
33#include <osmocom/core/logging.h>
34#include <osmocom/core/timer.h>
35#include <osmocom/core/msgb.h>
36#include <osmocom/core/utils.h>
37
38#include <osmocom/gsm/tlv.h>
39#include <osmocom/gsm/rsl.h>
40#include <osmocom/gsm/prim.h>
41#include <osmocom/gsm/gsm_utils.h>
42#include <osmocom/gsm/lapdm.h>
43
44#include <osmocom/gsm/protocol/gsm_04_08.h>
45#include <osmocom/gsm/protocol/gsm_08_58.h>
46
Harald Welte1284c3e2018-05-01 18:11:02 +020047#define LAPD_U_SABM 0x7
48
Harald Welte1f0b8c22011-06-27 10:51:37 +020049/* TS 04.06 Figure 4 / Section 3.2 */
50#define LAPDm_LPD_NORMAL 0
51#define LAPDm_LPD_SMSCB 1
52#define LAPDm_SAPI_NORMAL 0
53#define LAPDm_SAPI_SMS 3
54#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
55
rootaf48bed2011-09-26 11:23:06 +020056#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020057#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
58#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
59#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
Andreas Eversberg03700182023-05-10 13:23:55 +020060#define LAPDm_ADDR_SHORT_L2(addr) ((addr) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020061
62/* TS 04.06 Table 3 / Section 3.4.3 */
63#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
64#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
65#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
66
67#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
68#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
69#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
70
71#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
72#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
73
74#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
75
76#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
77#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
78
Harald Welte1f0b8c22011-06-27 10:51:37 +020079#define LAPDm_LEN(len) ((len << 2) | 0x1)
80#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020081#define LAPDm_EL 0x1
82
83#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020084
85/* TS 04.06 Section 5.8.3 */
86#define N201_AB_SACCH 18
87#define N201_AB_SDCCH 20
88#define N201_AB_FACCH 20
89#define N201_Bbis 23
90#define N201_Bter_SACCH 21
91#define N201_Bter_SDCCH 23
92#define N201_Bter_FACCH 23
93#define N201_B4 19
94
95/* 5.8.2.1 N200 during establish and release */
96#define N200_EST_REL 5
97/* 5.8.2.1 N200 during timer recovery state */
98#define N200_TR_SACCH 5
99#define N200_TR_SDCCH 23
100#define N200_TR_FACCH_FR 34
101#define N200_TR_EFACCH_FR 48
102#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200103/* FIXME: set N200 depending on chan_nr */
104#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200105
106enum lapdm_format {
107 LAPDm_FMT_A,
108 LAPDm_FMT_B,
109 LAPDm_FMT_Bbis,
110 LAPDm_FMT_Bter,
111 LAPDm_FMT_B4,
112};
113
Maxadef12a2016-05-25 15:25:02 +0200114const struct value_string osmo_ph_prim_names[] = {
115 { PRIM_PH_DATA, "PH-DATA" },
116 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
117 { PRIM_PH_CONN, "PH-CONNECT" },
118 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
119 { PRIM_PH_RTS, "PH-RTS" },
120 { PRIM_MPH_INFO, "MPH-INFO" },
121 { PRIM_TCH, "TCH" },
122 { PRIM_TCH_RTS, "TCH-RTS" },
123 { 0, NULL }
124};
125
Harald Welte00b2faf2020-05-02 19:56:36 +0200126extern void *tall_lapd_ctx;
127
rootaf48bed2011-09-26 11:23:06 +0200128static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
129static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
130 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100131static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200132
133static void lapdm_dl_init(struct lapdm_datalink *dl,
Harald Welte00b2faf2020-05-02 19:56:36 +0200134 struct lapdm_entity *entity, int t200_ms, uint32_t n200,
135 const char *name)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200136{
137 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200138 dl->entity = entity;
Harald Welte00b2faf2020-05-02 19:56:36 +0200139 lapd_dl_init2(&dl->dl, 1, 8, 251, name); /* Section 5.8.5 of TS 04.06 */
rootaf48bed2011-09-26 11:23:06 +0200140 dl->dl.reestablish = 0; /* GSM uses no reestablish */
141 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
142 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100143 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200144 dl->dl.n200_est_rel = N200_EST_REL;
Harald Welte20de6202019-06-02 21:33:38 +0200145 dl->dl.n200 = n200;
rootaf48bed2011-09-26 11:23:06 +0200146 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Harald Welte20de6202019-06-02 21:33:38 +0200147 dl->dl.t200_sec = t200_ms / 1000; dl->dl.t200_usec = (t200_ms % 1000) * 1000;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200148}
149
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200150/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200151 * \param[in] le LAPDm entity
152 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200153 * \param[in] t200 T200 re-transmission timer for all SAPIs in seconds
154 *
155 * Don't use this function; It doesn't support different T200 values per API
156 * and doesn't permit the caller to specify the N200 counter, both of which
157 * are required by GSM specs and supported by lapdm_entity_init2().
Harald Welte6bdf0b12011-08-17 18:22:08 +0200158 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100159void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200160{
Harald Welte20de6202019-06-02 21:33:38 +0200161 /* convert from single full-second value to per-SAPI milli-second value */
162 int t200_ms_sapi_arr[_NR_DL_SAPI];
163 int i;
164
165 for (i = 0; i < ARRAY_SIZE(t200_ms_sapi_arr); i++)
166 t200_ms_sapi_arr[i] = t200 * 1000;
167
Harald Welte00b2faf2020-05-02 19:56:36 +0200168 return lapdm_entity_init3(le, mode, t200_ms_sapi_arr, N200, NULL);
Harald Welte20de6202019-06-02 21:33:38 +0200169}
170
171/*! initialize a LAPDm entity and all datalinks inside
172 * \param[in] le LAPDm entity
173 * \param[in] mode lapdm_mode (BTS/MS)
174 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
175 * \param[in] n200 N200 re-transmisison count
176 */
177void lapdm_entity_init2(struct lapdm_entity *le, enum lapdm_mode mode,
178 const int *t200_ms, int n200)
179{
Harald Welte00b2faf2020-05-02 19:56:36 +0200180 lapdm_entity_init3(le, mode, t200_ms, n200, NULL);
181}
182
183/*! initialize a LAPDm entity and all datalinks inside
184 * \param[in] le LAPDm entity
185 * \param[in] mode lapdm_mode (BTS/MS)
186 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
187 * \param[in] n200 N200 re-transmisison count
188 * \param[in] name human-readable name (will be copied internally + extended with SAPI)
189 */
190void lapdm_entity_init3(struct lapdm_entity *le, enum lapdm_mode mode,
191 const int *t200_ms, int n200, const char *name_pfx)
192{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200193 unsigned int i;
194
Harald Welte00b2faf2020-05-02 19:56:36 +0200195 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
196 char name[256];
197 if (name_pfx) {
198 snprintf(name, sizeof(name), "%s[%s]", name_pfx, i == 0 ? "0" : "3");
199 lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200, name);
200 } else
201 lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200, NULL);
202 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200203
204 lapdm_entity_set_mode(le, mode);
205}
206
Harald Welte20de6202019-06-02 21:33:38 +0200207static int get_n200_dcch(enum gsm_chan_t chan_t)
208{
209 switch (chan_t) {
210 case GSM_LCHAN_SDCCH:
211 return N200_TR_SDCCH;
212 case GSM_LCHAN_TCH_F:
213 return N200_TR_FACCH_FR;
214 case GSM_LCHAN_TCH_H:
215 return N200_TR_FACCH_HR;
216 default:
217 return -1;
218 }
219}
220
221/*! initialize a LAPDm channel and all its channels
222 * \param[in] lc lapdm_channel to be initialized
223 * \param[in] mode lapdm_mode (BTS/MS)
224 *
225 * Don't use this function; It doesn't support different T200 values per API
226 * and doesn't set the correct N200 counter, both of which
227 * are required by GSM specs and supported by lapdm_channel_init2().
228 */
229void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
230{
231 /* emulate old backwards-compatible behavior with 1s/2s */
232 const int t200_ms_dcch[_NR_DL_SAPI] = { 1000, 1000 };
233 const int t200_ms_acch[_NR_DL_SAPI] = { 2000, 2000 };
234
Harald Welte00b2faf2020-05-02 19:56:36 +0200235 lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, GSM_LCHAN_SDCCH, NULL);
Harald Welte20de6202019-06-02 21:33:38 +0200236}
237
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200238/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200239 * \param[in] lc \ref lapdm_channel to be initialized
240 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200241 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
242 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
243 * \param[in] chan_t GSM channel type (to correctly set N200)
Harald Welte6bdf0b12011-08-17 18:22:08 +0200244 */
Harald Welte20de6202019-06-02 21:33:38 +0200245int lapdm_channel_init2(struct lapdm_channel *lc, enum lapdm_mode mode,
246 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200247{
Harald Welte00b2faf2020-05-02 19:56:36 +0200248 return lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, chan_t, NULL);
249}
250
251/*! initialize a LAPDm channel and all its channels
252 * \param[in] lc \ref lapdm_channel to be initialized
253 * \param[in] mode \ref lapdm_mode (BTS/MS)
254 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
255 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
256 * \param[in] chan_t GSM channel type (to correctly set N200)
Vadim Yanitskiy64277a02023-02-28 03:30:27 +0700257 * \param[in] name_pfx human-readable name (copied by function + extended with ACCH/DCCH)
Harald Welte00b2faf2020-05-02 19:56:36 +0200258 */
259int lapdm_channel_init3(struct lapdm_channel *lc, enum lapdm_mode mode,
260 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t,
261 const char *name_pfx)
262{
Harald Welte20de6202019-06-02 21:33:38 +0200263 int n200_dcch = get_n200_dcch(chan_t);
Harald Welte00b2faf2020-05-02 19:56:36 +0200264 char namebuf[256];
265 char *name = NULL;
266
Harald Welte20de6202019-06-02 21:33:38 +0200267 if (n200_dcch < 0)
268 return -EINVAL;
269
Harald Welte00b2faf2020-05-02 19:56:36 +0200270 osmo_talloc_replace_string(tall_lapd_ctx, &lc->name, name_pfx);
271
272 if (name_pfx) {
273 snprintf(namebuf, sizeof(namebuf), "%s[ACCH]", name_pfx);
274 name = namebuf;
275 }
276 lapdm_entity_init3(&lc->lapdm_acch, mode, t200_ms_acch, N200_TR_SACCH, name);
Harald Welte3e8c5202018-05-04 20:58:48 +0200277 lc->lapdm_acch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200278
Harald Welte00b2faf2020-05-02 19:56:36 +0200279 if (name_pfx) {
280 snprintf(namebuf, sizeof(namebuf), "%s[DCCH]", name_pfx);
281 name = namebuf;
282 }
283 lapdm_entity_init3(&lc->lapdm_dcch, mode, t200_ms_dcch, n200_dcch, name);
Harald Welte3e8c5202018-05-04 20:58:48 +0200284 lc->lapdm_dcch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200285
286 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200287}
288
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200289/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200290void lapdm_entity_exit(struct lapdm_entity *le)
291{
292 unsigned int i;
293 struct lapdm_datalink *dl;
294
295 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
296 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200297 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200298 }
299}
300
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200301/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200302 *
303 * A convenience wrapper calling \ref lapdm_entity_exit on both
304 * entities inside the \ref lapdm_channel
305 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200306void lapdm_channel_exit(struct lapdm_channel *lc)
307{
308 lapdm_entity_exit(&lc->lapdm_acch);
309 lapdm_entity_exit(&lc->lapdm_dcch);
310}
311
Daniel Willmann55405fb2014-03-26 13:45:17 +0100312struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200313{
314 switch (sapi) {
315 case LAPDm_SAPI_NORMAL:
316 return &le->datalink[0];
317 case LAPDm_SAPI_SMS:
318 return &le->datalink[1];
319 default:
320 return NULL;
321 }
322}
323
Harald Welte1f0b8c22011-06-27 10:51:37 +0200324/* Append padding (if required) */
325static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
326{
327 int pad_len = n201 - msgb_l2len(msg);
328 uint8_t *data;
329
330 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200331 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200332 "cannot pad message that is already too big!\n");
333 return;
334 }
335
Vadim Yanitskiy29ecabe2020-08-27 02:12:23 +0700336 data = msgb_put(msg, pad_len); /* TODO: random padding */
337 memset(data, GSM_MACBLOCK_PADDING, pad_len);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200338}
339
340/* input function that L2 calls when sending messages up to L3 */
341static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
342{
343 if (!le->l3_cb) {
344 msgb_free(msg);
345 return -EIO;
346 }
347
348 /* call the layer2 message handler that is registered */
349 return le->l3_cb(msg, le, le->l3_ctx);
350}
351
352/* write a frame into the tx queue */
353static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200354 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200355{
356 struct lapdm_entity *le = dl->entity;
357 struct osmo_phsap_prim pp;
358
359 /* if there is a pending message, queue it */
360 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200361 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200362 *msgb_push(msg, 1) = link_id;
363 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200364 msgb_enqueue(&dl->dl.tx_queue, msg);
Andreas Eversbergcb72e742023-06-07 13:18:08 +0200365 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200366 }
367
368 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
369 PRIM_OP_REQUEST, msg);
370 pp.u.data.chan_nr = chan_nr;
371 pp.u.data.link_id = link_id;
372
373 /* send the frame now */
374 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200375 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200376
377 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
378}
379
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700380/* Dequeue a Downlink message for DCCH (dedicated channel) */
381static struct msgb *tx_dequeue_dcch_msgb(struct lapdm_entity *le)
382{
383 struct msgb *msg;
384
385 /* SAPI=0 always has higher priority than SAPI=3 */
386 msg = msgb_dequeue(&le->datalink[DL_SAPI0].dl.tx_queue);
387 if (msg == NULL) /* no SAPI=0 messages, dequeue SAPI=3 (if any) */
388 msg = msgb_dequeue(&le->datalink[DL_SAPI3].dl.tx_queue);
389
390 return msg;
391}
392
393/* Dequeue a Downlink message for ACCH (associated channel) */
394static struct msgb *tx_dequeue_acch_msgb(struct lapdm_entity *le)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200395{
396 struct lapdm_datalink *dl;
397 int last = le->last_tx_dequeue;
398 int i = last, n = ARRAY_SIZE(le->datalink);
399 struct msgb *msg = NULL;
400
401 /* round-robin dequeue */
402 do {
403 /* next */
404 i = (i + 1) % n;
405 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200406 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200407 break;
408 } while (i != last);
409
410 if (msg) {
411 /* Set last dequeue position */
412 le->last_tx_dequeue = i;
413 }
414
415 return msg;
416}
417
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200418/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200419 * a osmo_phsap_prim */
420int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
421{
422 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200423 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200424
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700425 /* Dequeue depending on channel type: DCCH or ACCH.
426 * See 3GPP TS 44.005, section 4.2.2 "Priority". */
427 if (le == &le->lapdm_ch->lapdm_dcch)
428 msg = tx_dequeue_dcch_msgb(le);
429 else
430 msg = tx_dequeue_acch_msgb(le);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200431 if (!msg)
432 return -ENODEV;
433
434 /* if we have a message, send PH-DATA.req */
435 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
436 PRIM_OP_REQUEST, msg);
437
438 /* Pull chan_nr and link_id */
439 pp->u.data.chan_nr = *msg->data;
440 msgb_pull(msg, 1);
441 pp->u.data.link_id = *msg->data;
442 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200443 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200444 msgb_pull(msg, 1);
445
446 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200447 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200448
449 return 0;
450}
451
452/* get next frame from the tx queue. because the ms has multiple datalinks,
453 * each datalink's queue is read round-robin.
454 */
455static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
456{
457 struct osmo_phsap_prim pp;
458
459 /* we may send again */
460 le->tx_pending = 0;
461
462 /* free confirm message */
463 if (msg)
464 msgb_free(msg);
465
466 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
467 /* no message in all queues */
468
469 /* If user didn't request PH-EMPTY_FRAME.req, abort */
470 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
471 return 0;
472
473 /* otherwise, send PH-EMPTY_FRAME.req */
474 osmo_prim_init(&pp.oph, SAP_GSM_PH,
475 PRIM_PH_EMPTY_FRAME,
476 PRIM_OP_REQUEST, NULL);
477 } else {
478 le->tx_pending = 1;
479 }
480
481 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
482}
483
Harald Welte542301b2018-04-19 16:11:14 +0200484/* Is a given msg_type "transparent" as per TS 48.058 Section 8.1 */
485static int rsl_is_transparent(uint8_t msg_type)
486{
487 switch (msg_type) {
488 case RSL_MT_DATA_IND:
489 case RSL_MT_UNIT_DATA_IND:
490 return 1;
491 case RSL_MT_DATA_REQ:
492 case RSL_MT_UNIT_DATA_REQ:
493 return 1;
494 default:
495 return 0;
496 }
497}
498
Harald Welte1f0b8c22011-06-27 10:51:37 +0200499/* Create RSLms various RSLms messages */
500static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
501 struct msgb *msg)
502{
Harald Welte542301b2018-04-19 16:11:14 +0200503 int transparent = rsl_is_transparent(msg_type);
504
Harald Welte1f0b8c22011-06-27 10:51:37 +0200505 /* Add the RSL + RLL header */
Harald Welte542301b2018-04-19 16:11:14 +0200506 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200507
508 /* send off the RSLms message to L3 */
509 return rslms_sendmsg(msg, mctx->dl->entity);
510}
511
512/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
513static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
514{
515 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200516
517 /* Add the RSL + RLL header */
518 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welted977f5f2018-05-08 21:53:28 +0200519
Harald Weltef1bdf782018-05-08 22:03:20 +0200520 /* Add two non-standard IEs carrying MS power and TA values for B4 (SACCH) */
521 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
522 msgb_tv_push(msg, RSL_IE_MS_POWER, mctx->tx_power_ind);
523 msgb_tv_push(msg, RSL_IE_TIMING_ADVANCE, mctx->ta_ind);
524 }
Harald Welted977f5f2018-05-08 21:53:28 +0200525
Harald Welte1f0b8c22011-06-27 10:51:37 +0200526 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
527 mctx->link_id, 1);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100528
Harald Welte1f0b8c22011-06-27 10:51:37 +0200529 return rslms_sendmsg(msg, mctx->dl->entity);
530}
531
532static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
533{
534 struct msgb *msg;
Harald Welte542301b2018-04-19 16:11:14 +0200535 int transparent = rsl_is_transparent(msg_type);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200536
Harald Welte542301b2018-04-19 16:11:14 +0200537 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200538
539 /* send off the RSLms message to L3 */
540 return rslms_sendmsg(msg, mctx->dl->entity);
541}
542
543static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
544{
545 struct msgb *msg;
546
Harald Welte00b2faf2020-05-02 19:56:36 +0200547 LOGDL(&mctx->dl->dl, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte542301b2018-04-19 16:11:14 +0200548 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200549 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
550 return rslms_sendmsg(msg, mctx->dl->entity);
551}
552
rootaf48bed2011-09-26 11:23:06 +0200553/* DLSAP L2 -> L3 (RSLms) */
554static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
555 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200556{
rootaf48bed2011-09-26 11:23:06 +0200557 struct lapd_datalink *dl = lctx->dl;
558 struct lapdm_datalink *mdl =
559 container_of(dl, struct lapdm_datalink, dl);
560 struct lapdm_msg_ctx *mctx = &mdl->mctx;
561 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200562
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200563 switch (OSMO_PRIM_HDR(&dp->oph)) {
564 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
565 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200566 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200567 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
568 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200569 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200570 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
571 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200572 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200573 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
574 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
575 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
576 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200577 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200578 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
579 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200580 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200581 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
582 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200583 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200584 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
585 rsl_rll_error(dp->u.error_ind.cause, mctx);
586 if (dp->oph.msg)
587 msgb_free(dp->oph.msg);
588 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200589 }
rootaf48bed2011-09-26 11:23:06 +0200590
591 if (!rll_msg) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200592 LOGDL(dl, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
rootaf48bed2011-09-26 11:23:06 +0200593 "fix!\n", dp->oph.primitive, dp->oph.operation);
594 return -EINVAL;
595 }
596
597 if (!dp->oph.msg)
598 return send_rll_simple(rll_msg, mctx);
599
600 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200601}
602
rootaf48bed2011-09-26 11:23:06 +0200603/* send a data frame to layer 1 */
604static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200605{
rootaf48bed2011-09-26 11:23:06 +0200606 uint8_t l3_len = msg->tail - msg->data;
607 struct lapd_datalink *dl = lctx->dl;
608 struct lapdm_datalink *mdl =
609 container_of(dl, struct lapdm_datalink, dl);
610 struct lapdm_msg_ctx *mctx = &mdl->mctx;
611 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200612
rootaf48bed2011-09-26 11:23:06 +0200613 /* prepend l2 header */
614 msg->l2h = msgb_push(msg, 3);
615 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
616 /* EA is set here too */
617 switch (format) {
618 case LAPD_FORM_I:
619 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
620 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200621 break;
rootaf48bed2011-09-26 11:23:06 +0200622 case LAPD_FORM_S:
623 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200624 break;
rootaf48bed2011-09-26 11:23:06 +0200625 case LAPD_FORM_U:
626 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200627 break;
628 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200629 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200630 return -EINVAL;
631 }
rootaf48bed2011-09-26 11:23:06 +0200632 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
633 if (lctx->more)
634 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200635
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100636 /* add ACCH header with last indicated tx-power and TA */
637 if ((mctx->link_id & 0x40)) {
638 struct lapdm_entity *le = mdl->entity;
639
640 msg->l2h = msgb_push(msg, 2);
641 msg->l2h[0] = le->tx_power;
642 msg->l2h[1] = le->ta;
643 }
644
rootaf48bed2011-09-26 11:23:06 +0200645 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
646 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200647}
648
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100649static int update_pending_frames(struct lapd_msg_ctx *lctx)
650{
651 struct lapd_datalink *dl = lctx->dl;
652 struct msgb *msg;
653 int rc = -1;
654
655 llist_for_each_entry(msg, &dl->tx_queue, list) {
656 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
657 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
658 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
659 rc = 0;
660 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200661 LOGDL(dl, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100662 }
663 }
664
665 return rc;
666}
667
Harald Welte1284c3e2018-05-01 18:11:02 +0200668/* determine if receiving a given LAPDm message is not permitted */
669static int lapdm_rx_not_permitted(const struct lapdm_entity *le,
670 const struct lapd_msg_ctx *lctx)
671{
672 /* we currently only implement SABM related checks here */
673 if (lctx->format != LAPD_FORM_U || lctx->s_u != LAPD_U_SABM)
674 return 0;
675
676 if (le->mode == LAPDM_MODE_BTS) {
677 if (le == &le->lapdm_ch->lapdm_acch) {
678 /* no contention resolution on SACCH */
679 if (lctx->length > 0)
680 return RLL_CAUSE_SABM_INFO_NOTALL;
681 } else {
682 switch (lctx->sapi) {
Harald Welte1284c3e2018-05-01 18:11:02 +0200683 case 3:
684 /* SAPI3 doesn't support contention resolution */
685 if (lctx->length > 0)
686 return RLL_CAUSE_SABM_INFO_NOTALL;
687 break;
Harald Welteb82a4072018-05-09 16:31:16 +0200688 default:
689 break;
Harald Welte1284c3e2018-05-01 18:11:02 +0200690 }
691 }
692 } else if (le->mode == LAPDM_MODE_MS) {
693 /* contention resolution (L3 present) is only sent by MS, but
694 * never received by it */
695 if (lctx->length > 0)
696 return RLL_CAUSE_SABM_INFO_NOTALL;
697 }
698 return 0;
699}
700
Harald Welte1f0b8c22011-06-27 10:51:37 +0200701/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200702static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
Pau Espin Pedrol1247aa12023-08-09 17:41:25 +0200703 uint8_t chan_nr, uint8_t link_id, uint32_t fn)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200704{
705 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200706 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200707 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200708 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200709 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200710 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200711
712 /* when we reach here, we have a msgb with l2h pointing to the raw
713 * 23byte mac block. The l1h has already been purged. */
714
rootaf48bed2011-09-26 11:23:06 +0200715 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200716 mctx.chan_nr = chan_nr;
717 mctx.link_id = link_id;
Pau Espin Pedrol1247aa12023-08-09 17:41:25 +0200718 mctx.fn = fn;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200719
Harald Welte1f0b8c22011-06-27 10:51:37 +0200720 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
721 if (cbits == 0x10 || cbits == 0x12) {
722 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
723 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200724 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200725 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200726 } else {
727 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200728 /* It was received from network on SACCH */
729
Andreas Eversberg03700182023-05-10 13:23:55 +0200730 /* A Short L3 header has both bits == 0. */
731 if (LAPDm_ADDR_SHORT_L2(msg->l2h[2]) == 0) {
732 mctx.lapdm_fmt = LAPDm_FMT_Bter;
733 n201 = N201_Bter_SACCH;
734 sapi = 0;
735 } else if (le->mode == LAPDM_MODE_MS
736 && LAPDm_CTRL_is_U(msg->l2h[3])
737 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
738 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
Harald Welte7ca604b2011-06-29 12:13:51 +0200739 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200740 n201 = N201_B4;
Andreas Eversberg03700182023-05-10 13:23:55 +0200741 /* sapi is found after two-btyte L1 header */
742 sapi = (msg->l2h[2] >> 2) & 7;
Harald Welte7ca604b2011-06-29 12:13:51 +0200743 } else {
744 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200745 n201 = N201_AB_SACCH;
Andreas Eversberg03700182023-05-10 13:23:55 +0200746 /* sapi is found after two-btyte L1 header */
747 sapi = (msg->l2h[2] >> 2) & 7;
Harald Welte7ca604b2011-06-29 12:13:51 +0200748 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200749 /* SACCH frames have a two-byte L1 header that
750 * OsmocomBB L1 doesn't strip */
751 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
752 mctx.ta_ind = msg->l2h[1];
753 msgb_pull(msg, 2);
754 msg->l2h += 2;
755 } else {
Andreas Eversberg03700182023-05-10 13:23:55 +0200756 /* A Short L3 header has both bits == 0. */
757 if (LAPDm_ADDR_SHORT_L2(msg->l2h[2]) == 0) {
758 mctx.lapdm_fmt = LAPDm_FMT_Bter;
759 n201 = N201_Bter_SDCCH;
760 sapi = 0;
761 } else {
762 mctx.lapdm_fmt = LAPDm_FMT_B;
763 n201 = N201_AB_SDCCH;
764 sapi = (msg->l2h[0] >> 2) & 7;
765 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200766 }
767 }
768
Daniel Willmann55405fb2014-03-26 13:45:17 +0100769 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200770 /* G.2.1 No action on frames containing an unallocated SAPI. */
771 if (!mctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200772 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200773 msgb_free(msg);
774 return -EIO;
775 }
776
Harald Welte1f0b8c22011-06-27 10:51:37 +0200777 switch (mctx.lapdm_fmt) {
778 case LAPDm_FMT_A:
779 case LAPDm_FMT_B:
780 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200781 lctx.dl = &mctx.dl->dl;
782 /* obtain SAPI from address field */
783 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
784 /* G.2.3 EA bit set to "0" is not allowed in GSM */
785 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200786 LOGDL(lctx.dl, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200787 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200788 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200789 return -EINVAL;
790 }
rootaf48bed2011-09-26 11:23:06 +0200791 /* adress field */
792 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
793 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
794 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
795 /* command field */
796 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
797 lctx.format = LAPD_FORM_I;
798 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
799 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
800 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
801 lctx.format = LAPD_FORM_S;
802 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
803 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
804 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
805 lctx.format = LAPD_FORM_U;
806 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
807 } else
808 lctx.format = LAPD_FORM_UKN;
809 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
810 if (lctx.sapi != LAPDm_SAPI_NORMAL
811 && lctx.sapi != LAPDm_SAPI_SMS
812 && lctx.format == LAPD_FORM_U
813 && lctx.s_u == LAPDm_U_UI) {
814 /* 5.3.3 UI frames with invalid SAPI values shall be
815 * discarded
816 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200817 LOGDL(lctx.dl, LOGL_INFO, "sapi=%u (discarding)\n", lctx.sapi);
rootaf48bed2011-09-26 11:23:06 +0200818 msgb_free(msg);
819 return 0;
820 }
821 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
822 lctx.n201 = n201;
823 lctx.length = n201;
824 lctx.more = 0;
825 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100826 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200827 } else {
828 /* length field */
829 if (!(msg->l2h[2] & LAPDm_EL)) {
830 /* G.4.1 If the EL bit is set to "0", an
831 * MDL-ERROR-INDICATION primitive with cause
832 * "frame not implemented" is sent to the
833 * mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +0200834 LOGDL(lctx.dl, LOGL_NOTICE, "we don't support multi-octet length\n");
rootaf48bed2011-09-26 11:23:06 +0200835 msgb_free(msg);
836 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
837 return -EINVAL;
838 }
839 lctx.n201 = n201;
840 lctx.length = msg->l2h[2] >> 2;
841 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
842 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100843 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200844 }
845 /* store context for messages from lapd */
846 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200847 rc =lapdm_rx_not_permitted(le, &lctx);
848 if (rc > 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200849 LOGDL(lctx.dl, LOGL_NOTICE, "received message not permitted\n");
Harald Welte1284c3e2018-05-01 18:11:02 +0200850 msgb_free(msg);
851 rsl_rll_error(rc, &mctx);
852 return -EINVAL;
853 }
rootaf48bed2011-09-26 11:23:06 +0200854 /* send to LAPD */
855 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200856 break;
857 case LAPDm_FMT_Bter:
Andreas Eversberg03700182023-05-10 13:23:55 +0200858 /* fall-through */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200859 case LAPDm_FMT_Bbis:
860 /* directly pass up to layer3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200861 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100862 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200863 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
864 break;
865 default:
866 msgb_free(msg);
867 }
868
869 return rc;
870}
871
872/* input into layer2 (from layer 1) */
873static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
874{
875 struct abis_rsl_cchan_hdr *ch;
876 struct gsm48_req_ref req_ref;
877 struct gsm_time gt;
878 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
879
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200880 if (!msg)
881 return -ENOMEM;
882
Harald Welte1f0b8c22011-06-27 10:51:37 +0200883 msg->l2h = msgb_push(msg, sizeof(*ch));
884 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
885 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
886 ch->chan_nr = RSL_CHAN_RACH;
887
888 /* generate a RSL CHANNEL REQUIRED message */
889 gsm_fn2gsmtime(&gt, fn);
890 req_ref.ra = ra;
891 req_ref.t1 = gt.t1; /* FIXME: modulo? */
892 req_ref.t2 = gt.t2;
893 req_ref.t3_low = gt.t3 & 7;
894 req_ref.t3_high = gt.t3 >> 3;
895
896 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
897 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
898
899 return rslms_sendmsg(msg, le);
900}
901
902static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
903
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200904/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200905int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
906{
907 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
908 int rc = 0;
909
910 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200911 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200912 oph->sap);
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +0700913 msgb_free(oph->msg);
914 return -ENODEV;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200915 }
916
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +0700917 switch (OSMO_PRIM_HDR(oph)) {
918 case OSMO_PRIM(PRIM_PH_DATA, PRIM_OP_INDICATION):
Harald Welte1f0b8c22011-06-27 10:51:37 +0200919 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
Pau Espin Pedrol1247aa12023-08-09 17:41:25 +0200920 pp->u.data.link_id, pp->u.data.fn);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200921 break;
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +0700922 case OSMO_PRIM(PRIM_PH_RTS, PRIM_OP_INDICATION):
Harald Welte1f0b8c22011-06-27 10:51:37 +0200923 rc = l2_ph_data_conf(oph->msg, le);
924 break;
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +0700925 case OSMO_PRIM(PRIM_PH_RACH, PRIM_OP_INDICATION):
926 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
927 pp->u.rach_ind.acc_delay);
928 break;
929 case OSMO_PRIM(PRIM_PH_RACH, PRIM_OP_CONFIRM):
930 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200931 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100932 default:
933 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
934 oph->primitive);
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +0700935 msgb_free(oph->msg);
936 return -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200937 }
938
939 return rc;
940}
941
942
943/* L3 -> L2 / RSLMS -> LAPDm */
944
rootaf48bed2011-09-26 11:23:06 +0200945/* Set LAPDm context for established connection */
946static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
947 uint8_t link_id, int n201, uint8_t sapi)
948{
949 memset(&dl->mctx, 0, sizeof(dl->mctx));
950 dl->mctx.dl = dl;
951 dl->mctx.chan_nr = chan_nr;
952 dl->mctx.link_id = link_id;
953 dl->dl.lctx.dl = &dl->dl;
954 dl->dl.lctx.n201 = n201;
955 dl->dl.lctx.sapi = sapi;
956
957 return 0;
958}
959
Harald Welte1f0b8c22011-06-27 10:51:37 +0200960/* L3 requests establishment of data link */
961static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
962{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200963 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
964 uint8_t chan_nr = rllh->chan_nr;
965 uint8_t link_id = rllh->link_id;
966 uint8_t sapi = rllh->link_id & 7;
967 struct tlv_parsed tv;
968 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100969 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200970 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200971
rootaf48bed2011-09-26 11:23:06 +0200972 /* Set LAPDm context for established connection */
973 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200974
rootaf48bed2011-09-26 11:23:06 +0200975 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200976 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200977 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200978 /* contention resolution establishment procedure */
979 if (sapi != 0) {
980 /* According to clause 6, the contention resolution
981 * procedure is only permitted with SAPI value 0 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200982 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200983 "resolution (discarding)\n");
984 msgb_free(msg);
985 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
986 }
987 /* transmit a SABM command with the P bit set to "1". The SABM
988 * command shall contain the layer 3 message unit */
989 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200990 } else {
991 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200992 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200993 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200994 }
995
996 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100997 if (length > n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200998 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100999 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001000 msgb_free(msg);
1001 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1002 }
1003
rootaf48bed2011-09-26 11:23:06 +02001004 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001005 msgb_pull_to_l3(msg);
1006 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001007
rootaf48bed2011-09-26 11:23:06 +02001008 /* prepare prim */
1009 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001010
rootaf48bed2011-09-26 11:23:06 +02001011 /* send to L2 */
1012 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001013}
1014
1015/* L3 requests transfer of unnumbered information */
1016static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1017{
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001018 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001019 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1020 uint8_t chan_nr = rllh->chan_nr;
1021 uint8_t link_id = rllh->link_id;
1022 uint8_t sapi = link_id & 7;
1023 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +01001024 int length, ui_bts;
Andreas Eversberg67997412023-05-10 13:00:23 +02001025 bool use_b_ter;
Max777be2e2017-03-01 18:16:44 +01001026
1027 if (!le) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001028 LOGDL(&dl->dl, LOGL_ERROR, "lapdm_datalink without entity error\n");
Max777be2e2017-03-01 18:16:44 +01001029 msgb_free(msg);
1030 return -EMLINK;
1031 }
1032 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +02001033
1034 /* check if the layer3 message length exceeds N201 */
1035
1036 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1037
1038 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001039 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001040 }
1041 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001042 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001043 }
1044 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001045 LOGDL(&dl->dl, LOGL_ERROR, "unit data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001046 msgb_free(msg);
1047 return -EINVAL;
1048 }
rootaf48bed2011-09-26 11:23:06 +02001049 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001050 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Andreas Eversberg67997412023-05-10 13:00:23 +02001051 /* check for Bter frame */
1052 use_b_ter = (length == ((link_id & 0x40) ? 21 : 23) && sapi == 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001053 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg67997412023-05-10 13:00:23 +02001054 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23 && !use_b_ter) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001055 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +02001056 "(discarding)\n", length,
1057 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001058 msgb_free(msg);
1059 return -EIO;
1060 }
1061
Harald Welte00b2faf2020-05-02 19:56:36 +02001062 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 +02001063
rootaf48bed2011-09-26 11:23:06 +02001064 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001065 msgb_pull_to_l3(msg);
1066 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001067
1068 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg67997412023-05-10 13:00:23 +02001069 if (!use_b_ter) {
1070 msg->l2h = msgb_push(msg, 2 + !ui_bts);
1071 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
1072 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
1073 if (!ui_bts)
1074 msg->l2h[2] = LAPDm_LEN(length);
1075 } else
1076 msg->l2h = msg->data;
Andreas Eversberg5977db02013-06-12 09:34:51 +02001077 if (link_id & 0x40) {
1078 msg->l2h = msgb_push(msg, 2);
1079 msg->l2h[0] = le->tx_power;
1080 msg->l2h[1] = le->ta;
1081 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001082
1083 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +02001084 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001085}
1086
1087/* L3 requests transfer of acknowledged information */
1088static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1089{
1090 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1091 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +02001092 int length;
1093 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001094
1095 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1096 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001097 LOGDL(&dl->dl, LOGL_ERROR, "data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001098 msgb_free(msg);
1099 return -EINVAL;
1100 }
rootaf48bed2011-09-26 11:23:06 +02001101 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
1102 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001103
rootaf48bed2011-09-26 11:23:06 +02001104 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001105 msgb_pull_to_l3(msg);
1106 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001107
rootaf48bed2011-09-26 11:23:06 +02001108 /* prepare prim */
1109 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001110
rootaf48bed2011-09-26 11:23:06 +02001111 /* send to L2 */
1112 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001113}
1114
1115/* L3 requests suspension of data link */
1116static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1117{
1118 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1119 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001120 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001121
1122 if (sapi != 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001123 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001124 msgb_free(msg);
1125 return -EINVAL;
1126 }
1127
rootaf48bed2011-09-26 11:23:06 +02001128 /* prepare prim */
1129 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001130
rootaf48bed2011-09-26 11:23:06 +02001131 /* send to L2 */
1132 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001133}
1134
1135/* L3 requests resume of data link */
1136static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1137{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001138 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001139 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001140 uint8_t chan_nr = rllh->chan_nr;
1141 uint8_t link_id = rllh->link_id;
1142 uint8_t sapi = rllh->link_id & 7;
1143 struct tlv_parsed tv;
1144 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001145 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001146 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001147
rootaf48bed2011-09-26 11:23:06 +02001148 /* Set LAPDm context for established connection */
1149 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001150
1151 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1152 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001153 LOGDL(&dl->dl, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001154 msgb_free(msg);
1155 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1156 }
rootaf48bed2011-09-26 11:23:06 +02001157 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001158 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1159
rootaf48bed2011-09-26 11:23:06 +02001160 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001161 msgb_pull_to_l3(msg);
1162 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001163
rootaf48bed2011-09-26 11:23:06 +02001164 /* prepare prim */
1165 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1166 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001167
rootaf48bed2011-09-26 11:23:06 +02001168 /* send to L2 */
1169 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001170}
1171
1172/* L3 requests release of data link */
1173static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1174{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001175 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001176 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001177 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001178
1179 /* get release mode */
1180 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1181 mode = rllh->data[1] & 1;
1182
Harald Welte1f0b8c22011-06-27 10:51:37 +02001183 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001184 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001185
1186 /* 04.06 3.8.3: No information field is permitted with the DISC
1187 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001188 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001189
rootaf48bed2011-09-26 11:23:06 +02001190 /* prepare prim */
1191 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1192 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001193
rootaf48bed2011-09-26 11:23:06 +02001194 /* send to L2 */
1195 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001196}
1197
1198/* L3 requests channel in idle state */
1199static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1200{
1201 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1202 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1203 struct osmo_phsap_prim pp;
1204
1205 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1206 PRIM_OP_REQUEST, NULL);
1207
1208 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001209 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001210 return -EINVAL;
1211 }
1212 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001213 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001214 return -EINVAL;
1215 }
1216 pp.u.rach_req.ra = cch->data[1];
1217 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1218 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1219
1220 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001221 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001222 return -EINVAL;
1223 }
1224 /* TA = 0 - delay */
1225 pp.u.rach_req.ta = 0 - cch->data[5];
1226
1227 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001228 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001229 return -EINVAL;
1230 }
1231 pp.u.rach_req.tx_power = cch->data[7];
1232
1233 msgb_free(msg);
1234
1235 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1236}
1237
1238/* L1 confirms channel request */
1239static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1240{
1241 struct abis_rsl_cchan_hdr *ch;
1242 struct gsm_time tm;
1243 struct gsm48_req_ref *ref;
1244
1245 gsm_fn2gsmtime(&tm, frame_nr);
1246
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001247 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001248 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1249 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1250 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1251 ch->chan_nr = RSL_CHAN_RACH;
1252 ch->data[0] = RSL_IE_REQ_REFERENCE;
1253 ref = (struct gsm48_req_ref *) (ch->data + 1);
1254 ref->t1 = tm.t1;
1255 ref->t2 = tm.t2;
1256 ref->t3_low = tm.t3 & 0x7;
1257 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001258
Harald Welte1f0b8c22011-06-27 10:51:37 +02001259 return rslms_sendmsg(msg, le);
1260}
1261
Harald Welte1f0b8c22011-06-27 10:51:37 +02001262/* incoming RSLms RLL message from L3 */
1263static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1264{
1265 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1266 int msg_type = rllh->c.msg_type;
1267 uint8_t sapi = rllh->link_id & 7;
1268 struct lapdm_entity *le;
1269 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001270 int rc = 0;
1271
1272 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001273 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001274 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001275 return -EINVAL;
1276 }
1277
1278 if (rllh->link_id & 0x40)
1279 le = &lc->lapdm_acch;
1280 else
1281 le = &lc->lapdm_dcch;
1282
Harald Welte1a87c1b2015-12-14 15:26:07 +01001283 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1284 * BTS side */
1285 if (le->mode == LAPDM_MODE_BTS) {
1286 switch (msg_type) {
1287 case RSL_MT_SUSP_REQ:
1288 case RSL_MT_RES_REQ:
1289 case RSL_MT_RECON_REQ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001290 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' unsupported in BTS side LAPDm\n",
Harald Welte1a87c1b2015-12-14 15:26:07 +01001291 lc->name, rsl_msg_name(msg_type));
1292 msgb_free(msg);
1293 return -EINVAL;
1294 break;
1295 default:
1296 break;
1297 }
1298 }
1299
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001300 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001301 * SAPI.
1302 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001303 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001304 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001305 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001306 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001307 return -EINVAL;
1308 }
1309
Daniel Willmanne5233922012-12-25 23:15:50 +01001310 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001311 case RSL_MT_DATA_REQ:
1312 case RSL_MT_SUSP_REQ:
1313 case RSL_MT_REL_REQ:
1314 /* This is triggered in abnormal error conditions where
1315 * set_lapdm_context() was not called for the channel earlier. */
1316 if (!dl->dl.lctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001317 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001318 lc->name, rsl_msg_name(msg_type), sapi);
1319 msgb_free(msg);
1320 return -EINVAL;
1321 }
1322 break;
1323 default:
Harald Welte00b2faf2020-05-02 19:56:36 +02001324 LOGP(DLLAPD, LOGL_INFO, "(%s) RLL Message '%s' received. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001325 lc->name, rsl_msg_name(msg_type), sapi);
1326 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001327
rootaf48bed2011-09-26 11:23:06 +02001328 switch (msg_type) {
1329 case RSL_MT_UNIT_DATA_REQ:
1330 rc = rslms_rx_rll_udata_req(msg, dl);
1331 break;
1332 case RSL_MT_EST_REQ:
1333 rc = rslms_rx_rll_est_req(msg, dl);
1334 break;
1335 case RSL_MT_DATA_REQ:
1336 rc = rslms_rx_rll_data_req(msg, dl);
1337 break;
1338 case RSL_MT_SUSP_REQ:
1339 rc = rslms_rx_rll_susp_req(msg, dl);
1340 break;
1341 case RSL_MT_RES_REQ:
1342 rc = rslms_rx_rll_res_req(msg, dl);
1343 break;
1344 case RSL_MT_RECON_REQ:
1345 rc = rslms_rx_rll_res_req(msg, dl);
1346 break;
1347 case RSL_MT_REL_REQ:
1348 rc = rslms_rx_rll_rel_req(msg, dl);
1349 break;
1350 default:
1351 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001352 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001353 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001354 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001355
1356 return rc;
1357}
1358
1359/* incoming RSLms COMMON CHANNEL message from L3 */
1360static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1361{
1362 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1363 int msg_type = cch->c.msg_type;
1364 int rc = 0;
1365
1366 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001367 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001368 return -EINVAL;
1369 }
1370
1371 switch (msg_type) {
1372 case RSL_MT_CHAN_RQD:
1373 /* create and send RACH request */
1374 rc = rslms_rx_chan_rqd(lc, msg);
1375 break;
1376 default:
rootaf48bed2011-09-26 11:23:06 +02001377 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001378 msg_type);
1379 msgb_free(msg);
1380 return 0;
1381 }
1382
1383 return rc;
1384}
1385
Harald Welte7023aa02019-05-19 12:17:06 +02001386/*! Receive a RSLms \ref msgb from Layer 3. 'msg' ownership is transferred,
1387 * i.e. caller must not free it */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001388int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1389{
1390 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1391 int rc = 0;
1392
1393 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001394 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte7023aa02019-05-19 12:17:06 +02001395 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001396 return -EINVAL;
1397 }
1398
1399 switch (rslh->msg_discr & 0xfe) {
1400 case ABIS_RSL_MDISC_RLL:
1401 rc = rslms_rx_rll(msg, lc);
1402 break;
1403 case ABIS_RSL_MDISC_COM_CHAN:
1404 rc = rslms_rx_com_chan(msg, lc);
1405 break;
1406 default:
rootaf48bed2011-09-26 11:23:06 +02001407 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001408 "discriminator 0x%02x", rslh->msg_discr);
1409 msgb_free(msg);
1410 return -EINVAL;
1411 }
1412
1413 return rc;
1414}
1415
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001416/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001417int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1418{
rootaf48bed2011-09-26 11:23:06 +02001419 int i;
1420 enum lapd_mode lm;
1421
Harald Welte1f0b8c22011-06-27 10:51:37 +02001422 switch (mode) {
1423 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001424 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001425 break;
1426 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001427 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001428 break;
1429 default:
1430 return -EINVAL;
1431 }
1432
rootaf48bed2011-09-26 11:23:06 +02001433 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1434 lapd_set_mode(&le->datalink[i].dl, lm);
1435 }
1436
Harald Welte1f0b8c22011-06-27 10:51:37 +02001437 le->mode = mode;
1438
1439 return 0;
1440}
1441
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001442/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001443int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1444{
1445 int rc;
1446
1447 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1448 if (rc < 0)
1449 return rc;
1450
1451 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1452}
1453
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001454/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001455void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1456{
1457 lc->lapdm_dcch.l1_prim_cb = cb;
1458 lc->lapdm_acch.l1_prim_cb = cb;
1459 lc->lapdm_dcch.l1_ctx = ctx;
1460 lc->lapdm_acch.l1_ctx = ctx;
1461}
1462
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001463/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001464void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1465{
1466 lc->lapdm_dcch.l3_cb = cb;
1467 lc->lapdm_acch.l3_cb = cb;
1468 lc->lapdm_dcch.l3_ctx = ctx;
1469 lc->lapdm_acch.l3_ctx = ctx;
1470}
1471
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001472/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001473void lapdm_entity_reset(struct lapdm_entity *le)
1474{
1475 struct lapdm_datalink *dl;
1476 int i;
1477
1478 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1479 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001480 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001481 }
1482}
1483
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001484/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001485void lapdm_channel_reset(struct lapdm_channel *lc)
1486{
1487 lapdm_entity_reset(&lc->lapdm_dcch);
1488 lapdm_entity_reset(&lc->lapdm_acch);
1489}
1490
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001491/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001492void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1493{
1494 le->flags = flags;
1495}
1496
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001497/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001498void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1499{
1500 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1501 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1502}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001503
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001504/*! @} */