blob: 43f5662d0524ef2fc749efee684b0bd44aaf6262 [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 */
Pau Espin Pedrol06da40b2023-08-23 17:40:48 +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])) {
Andreas Eversberg1bb0b992023-11-09 12:09:21 +0100661 msg->l2h[1] = LAPDm_CTRL_S(dl->v_recv, LAPDm_CTRL_S_BITS(msg->l2h[1]),
662 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100663 }
664 }
665
666 return rc;
667}
668
Harald Welte1284c3e2018-05-01 18:11:02 +0200669/* determine if receiving a given LAPDm message is not permitted */
670static int lapdm_rx_not_permitted(const struct lapdm_entity *le,
671 const struct lapd_msg_ctx *lctx)
672{
673 /* we currently only implement SABM related checks here */
674 if (lctx->format != LAPD_FORM_U || lctx->s_u != LAPD_U_SABM)
675 return 0;
676
677 if (le->mode == LAPDM_MODE_BTS) {
678 if (le == &le->lapdm_ch->lapdm_acch) {
679 /* no contention resolution on SACCH */
680 if (lctx->length > 0)
681 return RLL_CAUSE_SABM_INFO_NOTALL;
682 } else {
683 switch (lctx->sapi) {
Harald Welte1284c3e2018-05-01 18:11:02 +0200684 case 3:
685 /* SAPI3 doesn't support contention resolution */
686 if (lctx->length > 0)
687 return RLL_CAUSE_SABM_INFO_NOTALL;
688 break;
Harald Welteb82a4072018-05-09 16:31:16 +0200689 default:
690 break;
Harald Welte1284c3e2018-05-01 18:11:02 +0200691 }
692 }
693 } else if (le->mode == LAPDM_MODE_MS) {
694 /* contention resolution (L3 present) is only sent by MS, but
695 * never received by it */
696 if (lctx->length > 0)
697 return RLL_CAUSE_SABM_INFO_NOTALL;
698 }
699 return 0;
700}
701
Harald Welte1f0b8c22011-06-27 10:51:37 +0200702/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200703static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
Pau Espin Pedrol1247aa12023-08-09 17:41:25 +0200704 uint8_t chan_nr, uint8_t link_id, uint32_t fn)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200705{
706 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200707 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200708 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200709 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200710 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200711 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200712
713 /* when we reach here, we have a msgb with l2h pointing to the raw
714 * 23byte mac block. The l1h has already been purged. */
715
rootaf48bed2011-09-26 11:23:06 +0200716 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200717 mctx.chan_nr = chan_nr;
718 mctx.link_id = link_id;
Pau Espin Pedrol1247aa12023-08-09 17:41:25 +0200719 mctx.fn = fn;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200720
Harald Welte1f0b8c22011-06-27 10:51:37 +0200721 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
722 if (cbits == 0x10 || cbits == 0x12) {
723 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
724 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200725 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200726 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200727 } else {
728 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200729 /* It was received from network on SACCH */
730
Andreas Eversberg03700182023-05-10 13:23:55 +0200731 /* A Short L3 header has both bits == 0. */
732 if (LAPDm_ADDR_SHORT_L2(msg->l2h[2]) == 0) {
733 mctx.lapdm_fmt = LAPDm_FMT_Bter;
734 n201 = N201_Bter_SACCH;
735 sapi = 0;
736 } else if (le->mode == LAPDM_MODE_MS
737 && LAPDm_CTRL_is_U(msg->l2h[3])
738 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
739 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
Harald Welte7ca604b2011-06-29 12:13:51 +0200740 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200741 n201 = N201_B4;
Andreas Eversberg03700182023-05-10 13:23:55 +0200742 /* sapi is found after two-btyte L1 header */
743 sapi = (msg->l2h[2] >> 2) & 7;
Harald Welte7ca604b2011-06-29 12:13:51 +0200744 } else {
745 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200746 n201 = N201_AB_SACCH;
Andreas Eversberg03700182023-05-10 13:23:55 +0200747 /* sapi is found after two-btyte L1 header */
748 sapi = (msg->l2h[2] >> 2) & 7;
Harald Welte7ca604b2011-06-29 12:13:51 +0200749 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200750 /* SACCH frames have a two-byte L1 header that
751 * OsmocomBB L1 doesn't strip */
752 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
753 mctx.ta_ind = msg->l2h[1];
754 msgb_pull(msg, 2);
755 msg->l2h += 2;
756 } else {
Andreas Eversberg03700182023-05-10 13:23:55 +0200757 /* A Short L3 header has both bits == 0. */
Andreas Eversberg16ad6c22023-09-08 19:31:06 +0200758 if (LAPDm_ADDR_SHORT_L2(msg->l2h[0]) == 0) {
Andreas Eversberg03700182023-05-10 13:23:55 +0200759 mctx.lapdm_fmt = LAPDm_FMT_Bter;
760 n201 = N201_Bter_SDCCH;
761 sapi = 0;
762 } else {
763 mctx.lapdm_fmt = LAPDm_FMT_B;
764 n201 = N201_AB_SDCCH;
765 sapi = (msg->l2h[0] >> 2) & 7;
766 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200767 }
768 }
769
Daniel Willmann55405fb2014-03-26 13:45:17 +0100770 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200771 /* G.2.1 No action on frames containing an unallocated SAPI. */
772 if (!mctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200773 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200774 msgb_free(msg);
775 return -EIO;
776 }
777
Harald Welte1f0b8c22011-06-27 10:51:37 +0200778 switch (mctx.lapdm_fmt) {
779 case LAPDm_FMT_A:
780 case LAPDm_FMT_B:
781 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200782 lctx.dl = &mctx.dl->dl;
783 /* obtain SAPI from address field */
784 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
785 /* G.2.3 EA bit set to "0" is not allowed in GSM */
786 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200787 LOGDL(lctx.dl, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200788 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200789 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200790 return -EINVAL;
791 }
rootaf48bed2011-09-26 11:23:06 +0200792 /* adress field */
793 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
794 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
795 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
796 /* command field */
797 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
798 lctx.format = LAPD_FORM_I;
799 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
800 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
801 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
802 lctx.format = LAPD_FORM_S;
803 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
804 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
805 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
806 lctx.format = LAPD_FORM_U;
807 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
808 } else
809 lctx.format = LAPD_FORM_UKN;
810 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
811 if (lctx.sapi != LAPDm_SAPI_NORMAL
812 && lctx.sapi != LAPDm_SAPI_SMS
813 && lctx.format == LAPD_FORM_U
814 && lctx.s_u == LAPDm_U_UI) {
815 /* 5.3.3 UI frames with invalid SAPI values shall be
816 * discarded
817 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200818 LOGDL(lctx.dl, LOGL_INFO, "sapi=%u (discarding)\n", lctx.sapi);
rootaf48bed2011-09-26 11:23:06 +0200819 msgb_free(msg);
820 return 0;
821 }
822 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
823 lctx.n201 = n201;
824 lctx.length = n201;
825 lctx.more = 0;
826 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100827 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200828 } else {
829 /* length field */
830 if (!(msg->l2h[2] & LAPDm_EL)) {
831 /* G.4.1 If the EL bit is set to "0", an
832 * MDL-ERROR-INDICATION primitive with cause
833 * "frame not implemented" is sent to the
834 * mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +0200835 LOGDL(lctx.dl, LOGL_NOTICE, "we don't support multi-octet length\n");
rootaf48bed2011-09-26 11:23:06 +0200836 msgb_free(msg);
837 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
838 return -EINVAL;
839 }
840 lctx.n201 = n201;
841 lctx.length = msg->l2h[2] >> 2;
842 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
843 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100844 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200845 }
846 /* store context for messages from lapd */
847 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200848 rc =lapdm_rx_not_permitted(le, &lctx);
849 if (rc > 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200850 LOGDL(lctx.dl, LOGL_NOTICE, "received message not permitted\n");
Harald Welte1284c3e2018-05-01 18:11:02 +0200851 msgb_free(msg);
852 rsl_rll_error(rc, &mctx);
853 return -EINVAL;
854 }
rootaf48bed2011-09-26 11:23:06 +0200855 /* send to LAPD */
856 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200857 break;
858 case LAPDm_FMT_Bter:
Andreas Eversberg03700182023-05-10 13:23:55 +0200859 /* fall-through */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200860 case LAPDm_FMT_Bbis:
Pau Espin Pedrol45ab0d72023-08-23 18:58:19 +0200861 /* Update context so that users can read fields like fn: */
862 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200863 /* directly pass up to layer3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200864 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100865 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200866 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
867 break;
868 default:
869 msgb_free(msg);
870 }
871
872 return rc;
873}
874
875/* input into layer2 (from layer 1) */
876static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
877{
878 struct abis_rsl_cchan_hdr *ch;
879 struct gsm48_req_ref req_ref;
880 struct gsm_time gt;
881 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
882
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200883 if (!msg)
884 return -ENOMEM;
885
Harald Welte1f0b8c22011-06-27 10:51:37 +0200886 msg->l2h = msgb_push(msg, sizeof(*ch));
887 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
888 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
889 ch->chan_nr = RSL_CHAN_RACH;
890
891 /* generate a RSL CHANNEL REQUIRED message */
892 gsm_fn2gsmtime(&gt, fn);
893 req_ref.ra = ra;
894 req_ref.t1 = gt.t1; /* FIXME: modulo? */
895 req_ref.t2 = gt.t2;
896 req_ref.t3_low = gt.t3 & 7;
897 req_ref.t3_high = gt.t3 >> 3;
898
899 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
900 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
901
902 return rslms_sendmsg(msg, le);
903}
904
905static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
906
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200907/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200908int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
909{
910 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
911 int rc = 0;
912
913 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200914 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200915 oph->sap);
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +0700916 msgb_free(oph->msg);
917 return -ENODEV;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200918 }
919
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +0700920 switch (OSMO_PRIM_HDR(oph)) {
921 case OSMO_PRIM(PRIM_PH_DATA, PRIM_OP_INDICATION):
Harald Welte1f0b8c22011-06-27 10:51:37 +0200922 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
Pau Espin Pedrol1247aa12023-08-09 17:41:25 +0200923 pp->u.data.link_id, pp->u.data.fn);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200924 break;
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +0700925 case OSMO_PRIM(PRIM_PH_RTS, PRIM_OP_INDICATION):
Harald Welte1f0b8c22011-06-27 10:51:37 +0200926 rc = l2_ph_data_conf(oph->msg, le);
927 break;
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +0700928 case OSMO_PRIM(PRIM_PH_RACH, PRIM_OP_INDICATION):
929 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
930 pp->u.rach_ind.acc_delay);
931 break;
932 case OSMO_PRIM(PRIM_PH_RACH, PRIM_OP_CONFIRM):
933 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200934 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100935 default:
936 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
937 oph->primitive);
Vadim Yanitskiy9ec77492023-06-23 15:32:15 +0700938 msgb_free(oph->msg);
939 return -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200940 }
941
942 return rc;
943}
944
945
946/* L3 -> L2 / RSLMS -> LAPDm */
947
rootaf48bed2011-09-26 11:23:06 +0200948/* Set LAPDm context for established connection */
949static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
950 uint8_t link_id, int n201, uint8_t sapi)
951{
952 memset(&dl->mctx, 0, sizeof(dl->mctx));
953 dl->mctx.dl = dl;
954 dl->mctx.chan_nr = chan_nr;
955 dl->mctx.link_id = link_id;
956 dl->dl.lctx.dl = &dl->dl;
957 dl->dl.lctx.n201 = n201;
958 dl->dl.lctx.sapi = sapi;
959
960 return 0;
961}
962
Harald Welte1f0b8c22011-06-27 10:51:37 +0200963/* L3 requests establishment of data link */
964static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
965{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200966 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
967 uint8_t chan_nr = rllh->chan_nr;
968 uint8_t link_id = rllh->link_id;
969 uint8_t sapi = rllh->link_id & 7;
970 struct tlv_parsed tv;
971 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100972 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200973 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200974
rootaf48bed2011-09-26 11:23:06 +0200975 /* Set LAPDm context for established connection */
976 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200977
rootaf48bed2011-09-26 11:23:06 +0200978 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200979 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200980 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200981 /* contention resolution establishment procedure */
982 if (sapi != 0) {
983 /* According to clause 6, the contention resolution
984 * procedure is only permitted with SAPI value 0 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200985 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200986 "resolution (discarding)\n");
987 msgb_free(msg);
988 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
989 }
990 /* transmit a SABM command with the P bit set to "1". The SABM
991 * command shall contain the layer 3 message unit */
992 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200993 } else {
994 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200995 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200996 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200997 }
998
999 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001000 if (length > n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001001 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001002 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001003 msgb_free(msg);
1004 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1005 }
1006
rootaf48bed2011-09-26 11:23:06 +02001007 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001008 msgb_pull_to_l3(msg);
1009 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001010
rootaf48bed2011-09-26 11:23:06 +02001011 /* prepare prim */
1012 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001013
rootaf48bed2011-09-26 11:23:06 +02001014 /* send to L2 */
1015 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001016}
1017
1018/* L3 requests transfer of unnumbered information */
1019static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1020{
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001021 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001022 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1023 uint8_t chan_nr = rllh->chan_nr;
1024 uint8_t link_id = rllh->link_id;
1025 uint8_t sapi = link_id & 7;
1026 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +01001027 int length, ui_bts;
Andreas Eversberg67997412023-05-10 13:00:23 +02001028 bool use_b_ter;
Max777be2e2017-03-01 18:16:44 +01001029
1030 if (!le) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001031 LOGDL(&dl->dl, LOGL_ERROR, "lapdm_datalink without entity error\n");
Max777be2e2017-03-01 18:16:44 +01001032 msgb_free(msg);
1033 return -EMLINK;
1034 }
1035 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +02001036
1037 /* check if the layer3 message length exceeds N201 */
1038
1039 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1040
1041 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001042 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001043 }
1044 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001045 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001046 }
1047 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001048 LOGDL(&dl->dl, LOGL_ERROR, "unit data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001049 msgb_free(msg);
1050 return -EINVAL;
1051 }
rootaf48bed2011-09-26 11:23:06 +02001052 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001053 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Andreas Eversberg67997412023-05-10 13:00:23 +02001054 /* check for Bter frame */
1055 use_b_ter = (length == ((link_id & 0x40) ? 21 : 23) && sapi == 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001056 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg67997412023-05-10 13:00:23 +02001057 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23 && !use_b_ter) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001058 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +02001059 "(discarding)\n", length,
1060 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001061 msgb_free(msg);
1062 return -EIO;
1063 }
1064
Harald Welte00b2faf2020-05-02 19:56:36 +02001065 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 +02001066
rootaf48bed2011-09-26 11:23:06 +02001067 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001068 msgb_pull_to_l3(msg);
1069 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001070
1071 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg67997412023-05-10 13:00:23 +02001072 if (!use_b_ter) {
1073 msg->l2h = msgb_push(msg, 2 + !ui_bts);
1074 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
1075 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
1076 if (!ui_bts)
1077 msg->l2h[2] = LAPDm_LEN(length);
1078 } else
1079 msg->l2h = msg->data;
Andreas Eversberg5977db02013-06-12 09:34:51 +02001080 if (link_id & 0x40) {
1081 msg->l2h = msgb_push(msg, 2);
1082 msg->l2h[0] = le->tx_power;
1083 msg->l2h[1] = le->ta;
1084 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001085
1086 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +02001087 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001088}
1089
1090/* L3 requests transfer of acknowledged information */
1091static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1092{
1093 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1094 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +02001095 int length;
1096 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001097
1098 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1099 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001100 LOGDL(&dl->dl, LOGL_ERROR, "data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001101 msgb_free(msg);
1102 return -EINVAL;
1103 }
rootaf48bed2011-09-26 11:23:06 +02001104 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
1105 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001106
rootaf48bed2011-09-26 11:23:06 +02001107 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001108 msgb_pull_to_l3(msg);
1109 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001110
rootaf48bed2011-09-26 11:23:06 +02001111 /* prepare prim */
1112 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001113
rootaf48bed2011-09-26 11:23:06 +02001114 /* send to L2 */
1115 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001116}
1117
1118/* L3 requests suspension of data link */
1119static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1120{
1121 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1122 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001123 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001124
1125 if (sapi != 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001126 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001127 msgb_free(msg);
1128 return -EINVAL;
1129 }
1130
rootaf48bed2011-09-26 11:23:06 +02001131 /* prepare prim */
1132 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001133
rootaf48bed2011-09-26 11:23:06 +02001134 /* send to L2 */
1135 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001136}
1137
1138/* L3 requests resume of data link */
1139static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1140{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001141 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001142 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001143 uint8_t chan_nr = rllh->chan_nr;
1144 uint8_t link_id = rllh->link_id;
1145 uint8_t sapi = rllh->link_id & 7;
1146 struct tlv_parsed tv;
1147 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001148 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001149 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001150
rootaf48bed2011-09-26 11:23:06 +02001151 /* Set LAPDm context for established connection */
1152 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001153
1154 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1155 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001156 LOGDL(&dl->dl, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001157 msgb_free(msg);
1158 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1159 }
rootaf48bed2011-09-26 11:23:06 +02001160 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001161 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1162
rootaf48bed2011-09-26 11:23:06 +02001163 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001164 msgb_pull_to_l3(msg);
1165 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001166
rootaf48bed2011-09-26 11:23:06 +02001167 /* prepare prim */
1168 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1169 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001170
rootaf48bed2011-09-26 11:23:06 +02001171 /* send to L2 */
1172 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001173}
1174
1175/* L3 requests release of data link */
1176static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1177{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001178 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001179 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001180 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001181
1182 /* get release mode */
1183 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1184 mode = rllh->data[1] & 1;
1185
Harald Welte1f0b8c22011-06-27 10:51:37 +02001186 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001187 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001188
1189 /* 04.06 3.8.3: No information field is permitted with the DISC
1190 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001191 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001192
rootaf48bed2011-09-26 11:23:06 +02001193 /* prepare prim */
1194 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1195 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001196
rootaf48bed2011-09-26 11:23:06 +02001197 /* send to L2 */
1198 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001199}
1200
1201/* L3 requests channel in idle state */
1202static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1203{
1204 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1205 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1206 struct osmo_phsap_prim pp;
1207
1208 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1209 PRIM_OP_REQUEST, NULL);
1210
1211 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001212 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001213 return -EINVAL;
1214 }
1215 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001216 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001217 return -EINVAL;
1218 }
1219 pp.u.rach_req.ra = cch->data[1];
1220 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1221 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1222
1223 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001224 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001225 return -EINVAL;
1226 }
1227 /* TA = 0 - delay */
1228 pp.u.rach_req.ta = 0 - cch->data[5];
1229
1230 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001231 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001232 return -EINVAL;
1233 }
1234 pp.u.rach_req.tx_power = cch->data[7];
1235
1236 msgb_free(msg);
1237
1238 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1239}
1240
1241/* L1 confirms channel request */
1242static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1243{
1244 struct abis_rsl_cchan_hdr *ch;
1245 struct gsm_time tm;
1246 struct gsm48_req_ref *ref;
1247
1248 gsm_fn2gsmtime(&tm, frame_nr);
1249
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001250 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001251 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1252 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1253 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1254 ch->chan_nr = RSL_CHAN_RACH;
1255 ch->data[0] = RSL_IE_REQ_REFERENCE;
1256 ref = (struct gsm48_req_ref *) (ch->data + 1);
1257 ref->t1 = tm.t1;
1258 ref->t2 = tm.t2;
1259 ref->t3_low = tm.t3 & 0x7;
1260 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001261
Harald Welte1f0b8c22011-06-27 10:51:37 +02001262 return rslms_sendmsg(msg, le);
1263}
1264
Harald Welte1f0b8c22011-06-27 10:51:37 +02001265/* incoming RSLms RLL message from L3 */
1266static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1267{
1268 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1269 int msg_type = rllh->c.msg_type;
1270 uint8_t sapi = rllh->link_id & 7;
1271 struct lapdm_entity *le;
1272 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001273 int rc = 0;
1274
1275 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001276 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001277 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001278 return -EINVAL;
1279 }
1280
1281 if (rllh->link_id & 0x40)
1282 le = &lc->lapdm_acch;
1283 else
1284 le = &lc->lapdm_dcch;
1285
Harald Welte1a87c1b2015-12-14 15:26:07 +01001286 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1287 * BTS side */
1288 if (le->mode == LAPDM_MODE_BTS) {
1289 switch (msg_type) {
1290 case RSL_MT_SUSP_REQ:
1291 case RSL_MT_RES_REQ:
1292 case RSL_MT_RECON_REQ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001293 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' unsupported in BTS side LAPDm\n",
Harald Welte1a87c1b2015-12-14 15:26:07 +01001294 lc->name, rsl_msg_name(msg_type));
1295 msgb_free(msg);
1296 return -EINVAL;
1297 break;
1298 default:
1299 break;
1300 }
1301 }
1302
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001303 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001304 * SAPI.
1305 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001306 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001307 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001308 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001309 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001310 return -EINVAL;
1311 }
1312
Daniel Willmanne5233922012-12-25 23:15:50 +01001313 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001314 case RSL_MT_DATA_REQ:
1315 case RSL_MT_SUSP_REQ:
1316 case RSL_MT_REL_REQ:
1317 /* This is triggered in abnormal error conditions where
1318 * set_lapdm_context() was not called for the channel earlier. */
1319 if (!dl->dl.lctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001320 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001321 lc->name, rsl_msg_name(msg_type), sapi);
1322 msgb_free(msg);
1323 return -EINVAL;
1324 }
1325 break;
1326 default:
Harald Welte00b2faf2020-05-02 19:56:36 +02001327 LOGP(DLLAPD, LOGL_INFO, "(%s) RLL Message '%s' received. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001328 lc->name, rsl_msg_name(msg_type), sapi);
1329 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001330
rootaf48bed2011-09-26 11:23:06 +02001331 switch (msg_type) {
1332 case RSL_MT_UNIT_DATA_REQ:
1333 rc = rslms_rx_rll_udata_req(msg, dl);
1334 break;
1335 case RSL_MT_EST_REQ:
1336 rc = rslms_rx_rll_est_req(msg, dl);
1337 break;
1338 case RSL_MT_DATA_REQ:
1339 rc = rslms_rx_rll_data_req(msg, dl);
1340 break;
1341 case RSL_MT_SUSP_REQ:
1342 rc = rslms_rx_rll_susp_req(msg, dl);
1343 break;
1344 case RSL_MT_RES_REQ:
1345 rc = rslms_rx_rll_res_req(msg, dl);
1346 break;
1347 case RSL_MT_RECON_REQ:
1348 rc = rslms_rx_rll_res_req(msg, dl);
1349 break;
1350 case RSL_MT_REL_REQ:
1351 rc = rslms_rx_rll_rel_req(msg, dl);
1352 break;
1353 default:
1354 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001355 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001356 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001357 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001358
1359 return rc;
1360}
1361
1362/* incoming RSLms COMMON CHANNEL message from L3 */
1363static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1364{
1365 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1366 int msg_type = cch->c.msg_type;
1367 int rc = 0;
1368
1369 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001370 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001371 return -EINVAL;
1372 }
1373
1374 switch (msg_type) {
1375 case RSL_MT_CHAN_RQD:
1376 /* create and send RACH request */
1377 rc = rslms_rx_chan_rqd(lc, msg);
1378 break;
1379 default:
rootaf48bed2011-09-26 11:23:06 +02001380 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001381 msg_type);
1382 msgb_free(msg);
1383 return 0;
1384 }
1385
1386 return rc;
1387}
1388
Harald Welte7023aa02019-05-19 12:17:06 +02001389/*! Receive a RSLms \ref msgb from Layer 3. 'msg' ownership is transferred,
1390 * i.e. caller must not free it */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001391int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1392{
1393 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1394 int rc = 0;
1395
1396 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001397 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte7023aa02019-05-19 12:17:06 +02001398 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001399 return -EINVAL;
1400 }
1401
1402 switch (rslh->msg_discr & 0xfe) {
1403 case ABIS_RSL_MDISC_RLL:
1404 rc = rslms_rx_rll(msg, lc);
1405 break;
1406 case ABIS_RSL_MDISC_COM_CHAN:
1407 rc = rslms_rx_com_chan(msg, lc);
1408 break;
1409 default:
rootaf48bed2011-09-26 11:23:06 +02001410 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001411 "discriminator 0x%02x", rslh->msg_discr);
1412 msgb_free(msg);
1413 return -EINVAL;
1414 }
1415
1416 return rc;
1417}
1418
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001419/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001420int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1421{
rootaf48bed2011-09-26 11:23:06 +02001422 int i;
1423 enum lapd_mode lm;
1424
Harald Welte1f0b8c22011-06-27 10:51:37 +02001425 switch (mode) {
1426 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001427 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001428 break;
1429 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001430 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001431 break;
1432 default:
1433 return -EINVAL;
1434 }
1435
rootaf48bed2011-09-26 11:23:06 +02001436 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1437 lapd_set_mode(&le->datalink[i].dl, lm);
1438 }
1439
Harald Welte1f0b8c22011-06-27 10:51:37 +02001440 le->mode = mode;
1441
1442 return 0;
1443}
1444
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001445/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001446int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1447{
1448 int rc;
1449
1450 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1451 if (rc < 0)
1452 return rc;
1453
1454 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1455}
1456
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001457/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001458void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1459{
1460 lc->lapdm_dcch.l1_prim_cb = cb;
1461 lc->lapdm_acch.l1_prim_cb = cb;
1462 lc->lapdm_dcch.l1_ctx = ctx;
1463 lc->lapdm_acch.l1_ctx = ctx;
1464}
1465
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001466/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001467void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1468{
1469 lc->lapdm_dcch.l3_cb = cb;
1470 lc->lapdm_acch.l3_cb = cb;
1471 lc->lapdm_dcch.l3_ctx = ctx;
1472 lc->lapdm_acch.l3_ctx = ctx;
1473}
1474
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001475/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001476void lapdm_entity_reset(struct lapdm_entity *le)
1477{
1478 struct lapdm_datalink *dl;
1479 int i;
1480
1481 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1482 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001483 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001484 }
1485}
1486
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001487/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001488void lapdm_channel_reset(struct lapdm_channel *lc)
1489{
1490 lapdm_entity_reset(&lc->lapdm_dcch);
1491 lapdm_entity_reset(&lc->lapdm_acch);
1492}
1493
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001494/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001495void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1496{
1497 le->flags = flags;
1498}
1499
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001500/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001501void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1502{
1503 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1504 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1505}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001506
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001507/*! @} */