blob: 8ca1342703dd2e38dcbcd998b27fd0ca1f80c9d2 [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);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200365 return -EBUSY;
366 }
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,
703 uint8_t chan_nr, uint8_t link_id)
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;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200718
Harald Welte1f0b8c22011-06-27 10:51:37 +0200719 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
720 if (cbits == 0x10 || cbits == 0x12) {
721 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
722 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200723 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200724 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200725 } else {
726 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200727 /* It was received from network on SACCH */
728
Andreas Eversberg03700182023-05-10 13:23:55 +0200729 /* A Short L3 header has both bits == 0. */
730 if (LAPDm_ADDR_SHORT_L2(msg->l2h[2]) == 0) {
731 mctx.lapdm_fmt = LAPDm_FMT_Bter;
732 n201 = N201_Bter_SACCH;
733 sapi = 0;
734 } else if (le->mode == LAPDM_MODE_MS
735 && LAPDm_CTRL_is_U(msg->l2h[3])
736 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
737 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
Harald Welte7ca604b2011-06-29 12:13:51 +0200738 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200739 n201 = N201_B4;
Andreas Eversberg03700182023-05-10 13:23:55 +0200740 /* sapi is found after two-btyte L1 header */
741 sapi = (msg->l2h[2] >> 2) & 7;
Harald Welte7ca604b2011-06-29 12:13:51 +0200742 } else {
743 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200744 n201 = N201_AB_SACCH;
Andreas Eversberg03700182023-05-10 13:23:55 +0200745 /* sapi is found after two-btyte L1 header */
746 sapi = (msg->l2h[2] >> 2) & 7;
Harald Welte7ca604b2011-06-29 12:13:51 +0200747 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200748 /* SACCH frames have a two-byte L1 header that
749 * OsmocomBB L1 doesn't strip */
750 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
751 mctx.ta_ind = msg->l2h[1];
752 msgb_pull(msg, 2);
753 msg->l2h += 2;
754 } else {
Andreas Eversberg03700182023-05-10 13:23:55 +0200755 /* A Short L3 header has both bits == 0. */
756 if (LAPDm_ADDR_SHORT_L2(msg->l2h[2]) == 0) {
757 mctx.lapdm_fmt = LAPDm_FMT_Bter;
758 n201 = N201_Bter_SDCCH;
759 sapi = 0;
760 } else {
761 mctx.lapdm_fmt = LAPDm_FMT_B;
762 n201 = N201_AB_SDCCH;
763 sapi = (msg->l2h[0] >> 2) & 7;
764 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200765 }
766 }
767
Daniel Willmann55405fb2014-03-26 13:45:17 +0100768 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200769 /* G.2.1 No action on frames containing an unallocated SAPI. */
770 if (!mctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200771 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200772 msgb_free(msg);
773 return -EIO;
774 }
775
Harald Welte1f0b8c22011-06-27 10:51:37 +0200776 switch (mctx.lapdm_fmt) {
777 case LAPDm_FMT_A:
778 case LAPDm_FMT_B:
779 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200780 lctx.dl = &mctx.dl->dl;
781 /* obtain SAPI from address field */
782 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
783 /* G.2.3 EA bit set to "0" is not allowed in GSM */
784 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200785 LOGDL(lctx.dl, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200786 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200787 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200788 return -EINVAL;
789 }
rootaf48bed2011-09-26 11:23:06 +0200790 /* adress field */
791 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
792 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
793 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
794 /* command field */
795 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
796 lctx.format = LAPD_FORM_I;
797 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
798 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
799 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
800 lctx.format = LAPD_FORM_S;
801 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
802 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
803 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
804 lctx.format = LAPD_FORM_U;
805 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
806 } else
807 lctx.format = LAPD_FORM_UKN;
808 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
809 if (lctx.sapi != LAPDm_SAPI_NORMAL
810 && lctx.sapi != LAPDm_SAPI_SMS
811 && lctx.format == LAPD_FORM_U
812 && lctx.s_u == LAPDm_U_UI) {
813 /* 5.3.3 UI frames with invalid SAPI values shall be
814 * discarded
815 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200816 LOGDL(lctx.dl, LOGL_INFO, "sapi=%u (discarding)\n", lctx.sapi);
rootaf48bed2011-09-26 11:23:06 +0200817 msgb_free(msg);
818 return 0;
819 }
820 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
821 lctx.n201 = n201;
822 lctx.length = n201;
823 lctx.more = 0;
824 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100825 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200826 } else {
827 /* length field */
828 if (!(msg->l2h[2] & LAPDm_EL)) {
829 /* G.4.1 If the EL bit is set to "0", an
830 * MDL-ERROR-INDICATION primitive with cause
831 * "frame not implemented" is sent to the
832 * mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +0200833 LOGDL(lctx.dl, LOGL_NOTICE, "we don't support multi-octet length\n");
rootaf48bed2011-09-26 11:23:06 +0200834 msgb_free(msg);
835 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
836 return -EINVAL;
837 }
838 lctx.n201 = n201;
839 lctx.length = msg->l2h[2] >> 2;
840 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
841 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100842 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200843 }
844 /* store context for messages from lapd */
845 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200846 rc =lapdm_rx_not_permitted(le, &lctx);
847 if (rc > 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200848 LOGDL(lctx.dl, LOGL_NOTICE, "received message not permitted\n");
Harald Welte1284c3e2018-05-01 18:11:02 +0200849 msgb_free(msg);
850 rsl_rll_error(rc, &mctx);
851 return -EINVAL;
852 }
rootaf48bed2011-09-26 11:23:06 +0200853 /* send to LAPD */
854 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200855 break;
856 case LAPDm_FMT_Bter:
Andreas Eversberg03700182023-05-10 13:23:55 +0200857 /* fall-through */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200858 case LAPDm_FMT_Bbis:
859 /* directly pass up to layer3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200860 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100861 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200862 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
863 break;
864 default:
865 msgb_free(msg);
866 }
867
868 return rc;
869}
870
871/* input into layer2 (from layer 1) */
872static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
873{
874 struct abis_rsl_cchan_hdr *ch;
875 struct gsm48_req_ref req_ref;
876 struct gsm_time gt;
877 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
878
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200879 if (!msg)
880 return -ENOMEM;
881
Harald Welte1f0b8c22011-06-27 10:51:37 +0200882 msg->l2h = msgb_push(msg, sizeof(*ch));
883 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
884 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
885 ch->chan_nr = RSL_CHAN_RACH;
886
887 /* generate a RSL CHANNEL REQUIRED message */
888 gsm_fn2gsmtime(&gt, fn);
889 req_ref.ra = ra;
890 req_ref.t1 = gt.t1; /* FIXME: modulo? */
891 req_ref.t2 = gt.t2;
892 req_ref.t3_low = gt.t3 & 7;
893 req_ref.t3_high = gt.t3 >> 3;
894
895 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
896 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
897
898 return rslms_sendmsg(msg, le);
899}
900
901static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
902
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200903/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200904int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
905{
906 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
907 int rc = 0;
908
909 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200910 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200911 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100912 rc = -ENODEV;
913 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200914 }
915
916 switch (oph->primitive) {
917 case PRIM_PH_DATA:
918 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200919 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200920 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100921 rc = -ENODEV;
922 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200923 }
924 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
925 pp->u.data.link_id);
926 break;
927 case PRIM_PH_RTS:
928 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200929 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200930 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100931 rc = -ENODEV;
932 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200933 }
934 rc = l2_ph_data_conf(oph->msg, le);
935 break;
936 case PRIM_PH_RACH:
937 switch (oph->operation) {
938 case PRIM_OP_INDICATION:
939 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
940 pp->u.rach_ind.acc_delay);
941 break;
942 case PRIM_OP_CONFIRM:
943 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
944 break;
945 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100946 rc = -EIO;
947 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200948 }
949 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100950 default:
951 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
952 oph->primitive);
953 rc = -EINVAL;
954 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200955 }
956
957 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100958
959free:
960 msgb_free(oph->msg);
961 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200962}
963
964
965/* L3 -> L2 / RSLMS -> LAPDm */
966
rootaf48bed2011-09-26 11:23:06 +0200967/* Set LAPDm context for established connection */
968static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
969 uint8_t link_id, int n201, uint8_t sapi)
970{
971 memset(&dl->mctx, 0, sizeof(dl->mctx));
972 dl->mctx.dl = dl;
973 dl->mctx.chan_nr = chan_nr;
974 dl->mctx.link_id = link_id;
975 dl->dl.lctx.dl = &dl->dl;
976 dl->dl.lctx.n201 = n201;
977 dl->dl.lctx.sapi = sapi;
978
979 return 0;
980}
981
Harald Welte1f0b8c22011-06-27 10:51:37 +0200982/* L3 requests establishment of data link */
983static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
984{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200985 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
986 uint8_t chan_nr = rllh->chan_nr;
987 uint8_t link_id = rllh->link_id;
988 uint8_t sapi = rllh->link_id & 7;
989 struct tlv_parsed tv;
990 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100991 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200992 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200993
rootaf48bed2011-09-26 11:23:06 +0200994 /* Set LAPDm context for established connection */
995 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200996
rootaf48bed2011-09-26 11:23:06 +0200997 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200998 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200999 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001000 /* contention resolution establishment procedure */
1001 if (sapi != 0) {
1002 /* According to clause 6, the contention resolution
1003 * procedure is only permitted with SAPI value 0 */
Harald Welte00b2faf2020-05-02 19:56:36 +02001004 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +02001005 "resolution (discarding)\n");
1006 msgb_free(msg);
1007 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1008 }
1009 /* transmit a SABM command with the P bit set to "1". The SABM
1010 * command shall contain the layer 3 message unit */
1011 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001012 } else {
1013 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +02001014 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001015 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001016 }
1017
1018 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001019 if (length > n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001020 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001021 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001022 msgb_free(msg);
1023 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1024 }
1025
rootaf48bed2011-09-26 11:23:06 +02001026 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001027 msgb_pull_to_l3(msg);
1028 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001029
rootaf48bed2011-09-26 11:23:06 +02001030 /* prepare prim */
1031 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001032
rootaf48bed2011-09-26 11:23:06 +02001033 /* send to L2 */
1034 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001035}
1036
1037/* L3 requests transfer of unnumbered information */
1038static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1039{
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001040 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001041 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1042 uint8_t chan_nr = rllh->chan_nr;
1043 uint8_t link_id = rllh->link_id;
1044 uint8_t sapi = link_id & 7;
1045 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +01001046 int length, ui_bts;
Andreas Eversberg67997412023-05-10 13:00:23 +02001047 bool use_b_ter;
Max777be2e2017-03-01 18:16:44 +01001048
1049 if (!le) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001050 LOGDL(&dl->dl, LOGL_ERROR, "lapdm_datalink without entity error\n");
Max777be2e2017-03-01 18:16:44 +01001051 msgb_free(msg);
1052 return -EMLINK;
1053 }
1054 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +02001055
1056 /* check if the layer3 message length exceeds N201 */
1057
1058 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1059
1060 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001061 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001062 }
1063 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001064 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001065 }
1066 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001067 LOGDL(&dl->dl, LOGL_ERROR, "unit data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001068 msgb_free(msg);
1069 return -EINVAL;
1070 }
rootaf48bed2011-09-26 11:23:06 +02001071 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001072 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Andreas Eversberg67997412023-05-10 13:00:23 +02001073 /* check for Bter frame */
1074 use_b_ter = (length == ((link_id & 0x40) ? 21 : 23) && sapi == 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001075 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg67997412023-05-10 13:00:23 +02001076 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23 && !use_b_ter) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001077 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +02001078 "(discarding)\n", length,
1079 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001080 msgb_free(msg);
1081 return -EIO;
1082 }
1083
Harald Welte00b2faf2020-05-02 19:56:36 +02001084 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 +02001085
rootaf48bed2011-09-26 11:23:06 +02001086 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001087 msgb_pull_to_l3(msg);
1088 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001089
1090 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg67997412023-05-10 13:00:23 +02001091 if (!use_b_ter) {
1092 msg->l2h = msgb_push(msg, 2 + !ui_bts);
1093 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
1094 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
1095 if (!ui_bts)
1096 msg->l2h[2] = LAPDm_LEN(length);
1097 } else
1098 msg->l2h = msg->data;
Andreas Eversberg5977db02013-06-12 09:34:51 +02001099 if (link_id & 0x40) {
1100 msg->l2h = msgb_push(msg, 2);
1101 msg->l2h[0] = le->tx_power;
1102 msg->l2h[1] = le->ta;
1103 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001104
1105 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +02001106 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001107}
1108
1109/* L3 requests transfer of acknowledged information */
1110static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1111{
1112 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1113 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +02001114 int length;
1115 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001116
1117 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1118 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001119 LOGDL(&dl->dl, LOGL_ERROR, "data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001120 msgb_free(msg);
1121 return -EINVAL;
1122 }
rootaf48bed2011-09-26 11:23:06 +02001123 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
1124 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001125
rootaf48bed2011-09-26 11:23:06 +02001126 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001127 msgb_pull_to_l3(msg);
1128 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001129
rootaf48bed2011-09-26 11:23:06 +02001130 /* prepare prim */
1131 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001132
rootaf48bed2011-09-26 11:23:06 +02001133 /* send to L2 */
1134 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001135}
1136
1137/* L3 requests suspension of data link */
1138static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1139{
1140 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1141 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001142 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001143
1144 if (sapi != 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001145 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001146 msgb_free(msg);
1147 return -EINVAL;
1148 }
1149
rootaf48bed2011-09-26 11:23:06 +02001150 /* prepare prim */
1151 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001152
rootaf48bed2011-09-26 11:23:06 +02001153 /* send to L2 */
1154 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001155}
1156
1157/* L3 requests resume of data link */
1158static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1159{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001160 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001161 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001162 uint8_t chan_nr = rllh->chan_nr;
1163 uint8_t link_id = rllh->link_id;
1164 uint8_t sapi = rllh->link_id & 7;
1165 struct tlv_parsed tv;
1166 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001167 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001168 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001169
rootaf48bed2011-09-26 11:23:06 +02001170 /* Set LAPDm context for established connection */
1171 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001172
1173 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1174 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001175 LOGDL(&dl->dl, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001176 msgb_free(msg);
1177 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1178 }
rootaf48bed2011-09-26 11:23:06 +02001179 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001180 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1181
rootaf48bed2011-09-26 11:23:06 +02001182 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001183 msgb_pull_to_l3(msg);
1184 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001185
rootaf48bed2011-09-26 11:23:06 +02001186 /* prepare prim */
1187 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1188 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001189
rootaf48bed2011-09-26 11:23:06 +02001190 /* send to L2 */
1191 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001192}
1193
1194/* L3 requests release of data link */
1195static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1196{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001197 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001198 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001199 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001200
1201 /* get release mode */
1202 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1203 mode = rllh->data[1] & 1;
1204
Harald Welte1f0b8c22011-06-27 10:51:37 +02001205 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001206 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001207
1208 /* 04.06 3.8.3: No information field is permitted with the DISC
1209 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001210 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001211
rootaf48bed2011-09-26 11:23:06 +02001212 /* prepare prim */
1213 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1214 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001215
rootaf48bed2011-09-26 11:23:06 +02001216 /* send to L2 */
1217 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001218}
1219
1220/* L3 requests channel in idle state */
1221static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1222{
1223 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1224 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1225 struct osmo_phsap_prim pp;
1226
1227 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1228 PRIM_OP_REQUEST, NULL);
1229
1230 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001231 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001232 return -EINVAL;
1233 }
1234 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001235 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001236 return -EINVAL;
1237 }
1238 pp.u.rach_req.ra = cch->data[1];
1239 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1240 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1241
1242 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001243 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001244 return -EINVAL;
1245 }
1246 /* TA = 0 - delay */
1247 pp.u.rach_req.ta = 0 - cch->data[5];
1248
1249 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001250 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001251 return -EINVAL;
1252 }
1253 pp.u.rach_req.tx_power = cch->data[7];
1254
1255 msgb_free(msg);
1256
1257 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1258}
1259
1260/* L1 confirms channel request */
1261static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1262{
1263 struct abis_rsl_cchan_hdr *ch;
1264 struct gsm_time tm;
1265 struct gsm48_req_ref *ref;
1266
1267 gsm_fn2gsmtime(&tm, frame_nr);
1268
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001269 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001270 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1271 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1272 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1273 ch->chan_nr = RSL_CHAN_RACH;
1274 ch->data[0] = RSL_IE_REQ_REFERENCE;
1275 ref = (struct gsm48_req_ref *) (ch->data + 1);
1276 ref->t1 = tm.t1;
1277 ref->t2 = tm.t2;
1278 ref->t3_low = tm.t3 & 0x7;
1279 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001280
Harald Welte1f0b8c22011-06-27 10:51:37 +02001281 return rslms_sendmsg(msg, le);
1282}
1283
Harald Welte1f0b8c22011-06-27 10:51:37 +02001284/* incoming RSLms RLL message from L3 */
1285static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1286{
1287 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1288 int msg_type = rllh->c.msg_type;
1289 uint8_t sapi = rllh->link_id & 7;
1290 struct lapdm_entity *le;
1291 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001292 int rc = 0;
1293
1294 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001295 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001296 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001297 return -EINVAL;
1298 }
1299
1300 if (rllh->link_id & 0x40)
1301 le = &lc->lapdm_acch;
1302 else
1303 le = &lc->lapdm_dcch;
1304
Harald Welte1a87c1b2015-12-14 15:26:07 +01001305 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1306 * BTS side */
1307 if (le->mode == LAPDM_MODE_BTS) {
1308 switch (msg_type) {
1309 case RSL_MT_SUSP_REQ:
1310 case RSL_MT_RES_REQ:
1311 case RSL_MT_RECON_REQ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001312 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' unsupported in BTS side LAPDm\n",
Harald Welte1a87c1b2015-12-14 15:26:07 +01001313 lc->name, rsl_msg_name(msg_type));
1314 msgb_free(msg);
1315 return -EINVAL;
1316 break;
1317 default:
1318 break;
1319 }
1320 }
1321
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001322 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001323 * SAPI.
1324 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001325 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001326 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001327 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001328 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001329 return -EINVAL;
1330 }
1331
Daniel Willmanne5233922012-12-25 23:15:50 +01001332 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001333 case RSL_MT_DATA_REQ:
1334 case RSL_MT_SUSP_REQ:
1335 case RSL_MT_REL_REQ:
1336 /* This is triggered in abnormal error conditions where
1337 * set_lapdm_context() was not called for the channel earlier. */
1338 if (!dl->dl.lctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001339 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001340 lc->name, rsl_msg_name(msg_type), sapi);
1341 msgb_free(msg);
1342 return -EINVAL;
1343 }
1344 break;
1345 default:
Harald Welte00b2faf2020-05-02 19:56:36 +02001346 LOGP(DLLAPD, LOGL_INFO, "(%s) RLL Message '%s' received. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001347 lc->name, rsl_msg_name(msg_type), sapi);
1348 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001349
rootaf48bed2011-09-26 11:23:06 +02001350 switch (msg_type) {
1351 case RSL_MT_UNIT_DATA_REQ:
1352 rc = rslms_rx_rll_udata_req(msg, dl);
1353 break;
1354 case RSL_MT_EST_REQ:
1355 rc = rslms_rx_rll_est_req(msg, dl);
1356 break;
1357 case RSL_MT_DATA_REQ:
1358 rc = rslms_rx_rll_data_req(msg, dl);
1359 break;
1360 case RSL_MT_SUSP_REQ:
1361 rc = rslms_rx_rll_susp_req(msg, dl);
1362 break;
1363 case RSL_MT_RES_REQ:
1364 rc = rslms_rx_rll_res_req(msg, dl);
1365 break;
1366 case RSL_MT_RECON_REQ:
1367 rc = rslms_rx_rll_res_req(msg, dl);
1368 break;
1369 case RSL_MT_REL_REQ:
1370 rc = rslms_rx_rll_rel_req(msg, dl);
1371 break;
1372 default:
1373 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001374 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001375 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001376 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001377
1378 return rc;
1379}
1380
1381/* incoming RSLms COMMON CHANNEL message from L3 */
1382static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1383{
1384 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1385 int msg_type = cch->c.msg_type;
1386 int rc = 0;
1387
1388 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001389 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001390 return -EINVAL;
1391 }
1392
1393 switch (msg_type) {
1394 case RSL_MT_CHAN_RQD:
1395 /* create and send RACH request */
1396 rc = rslms_rx_chan_rqd(lc, msg);
1397 break;
1398 default:
rootaf48bed2011-09-26 11:23:06 +02001399 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001400 msg_type);
1401 msgb_free(msg);
1402 return 0;
1403 }
1404
1405 return rc;
1406}
1407
Harald Welte7023aa02019-05-19 12:17:06 +02001408/*! Receive a RSLms \ref msgb from Layer 3. 'msg' ownership is transferred,
1409 * i.e. caller must not free it */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001410int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1411{
1412 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1413 int rc = 0;
1414
1415 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001416 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte7023aa02019-05-19 12:17:06 +02001417 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001418 return -EINVAL;
1419 }
1420
1421 switch (rslh->msg_discr & 0xfe) {
1422 case ABIS_RSL_MDISC_RLL:
1423 rc = rslms_rx_rll(msg, lc);
1424 break;
1425 case ABIS_RSL_MDISC_COM_CHAN:
1426 rc = rslms_rx_com_chan(msg, lc);
1427 break;
1428 default:
rootaf48bed2011-09-26 11:23:06 +02001429 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001430 "discriminator 0x%02x", rslh->msg_discr);
1431 msgb_free(msg);
1432 return -EINVAL;
1433 }
1434
1435 return rc;
1436}
1437
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001438/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001439int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1440{
rootaf48bed2011-09-26 11:23:06 +02001441 int i;
1442 enum lapd_mode lm;
1443
Harald Welte1f0b8c22011-06-27 10:51:37 +02001444 switch (mode) {
1445 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001446 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001447 break;
1448 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001449 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001450 break;
1451 default:
1452 return -EINVAL;
1453 }
1454
rootaf48bed2011-09-26 11:23:06 +02001455 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1456 lapd_set_mode(&le->datalink[i].dl, lm);
1457 }
1458
Harald Welte1f0b8c22011-06-27 10:51:37 +02001459 le->mode = mode;
1460
1461 return 0;
1462}
1463
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001464/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001465int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1466{
1467 int rc;
1468
1469 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1470 if (rc < 0)
1471 return rc;
1472
1473 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1474}
1475
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001476/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001477void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1478{
1479 lc->lapdm_dcch.l1_prim_cb = cb;
1480 lc->lapdm_acch.l1_prim_cb = cb;
1481 lc->lapdm_dcch.l1_ctx = ctx;
1482 lc->lapdm_acch.l1_ctx = ctx;
1483}
1484
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001485/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001486void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1487{
1488 lc->lapdm_dcch.l3_cb = cb;
1489 lc->lapdm_acch.l3_cb = cb;
1490 lc->lapdm_dcch.l3_ctx = ctx;
1491 lc->lapdm_acch.l3_ctx = ctx;
1492}
1493
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001494/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001495void lapdm_entity_reset(struct lapdm_entity *le)
1496{
1497 struct lapdm_datalink *dl;
1498 int i;
1499
1500 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1501 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001502 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001503 }
1504}
1505
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001506/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001507void lapdm_channel_reset(struct lapdm_channel *lc)
1508{
1509 lapdm_entity_reset(&lc->lapdm_dcch);
1510 lapdm_entity_reset(&lc->lapdm_acch);
1511}
1512
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001513/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001514void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1515{
1516 le->flags = flags;
1517}
1518
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001519/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001520void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1521{
1522 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1523 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1524}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001525
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001526/*! @} */