blob: e5fbaf0d4958867c2e93812d81c454f776f5bf17 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file lapdm.c
2 * GSM LAPDm (TS 04.06) implementation. */
3/*
Harald Welte20de6202019-06-02 21:33:38 +02004 * (C) 2010-2019 by Harald Welte <laforge@gnumonks.org>
rootaf48bed2011-09-26 11:23:06 +02005 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
Harald Weltee08da972017-11-13 01:00:26 +09006 * (C) 2014-2016 by sysmocom - s.f.m.c GmbH
Harald Welte1f0b8c22011-06-27 10:51:37 +02007 *
8 * All Rights Reserved
9 *
Harald Weltee08da972017-11-13 01:00:26 +090010 * SPDX-License-Identifier: GPL-2.0+
11 *
Harald Welte1f0b8c22011-06-27 10:51:37 +020012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 */
27
Harald Welte6bdf0b12011-08-17 18:22:08 +020028/*! \addtogroup lapdm
29 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020030 * \file lapdm.c */
Harald Welte6bdf0b12011-08-17 18:22:08 +020031
Harald Welte1f0b8c22011-06-27 10:51:37 +020032#include <stdio.h>
33#include <stdint.h>
34#include <string.h>
35#include <errno.h>
Harald Welte1f0b8c22011-06-27 10:51:37 +020036
37#include <osmocom/core/logging.h>
38#include <osmocom/core/timer.h>
39#include <osmocom/core/msgb.h>
40#include <osmocom/core/utils.h>
41
42#include <osmocom/gsm/tlv.h>
43#include <osmocom/gsm/rsl.h>
44#include <osmocom/gsm/prim.h>
45#include <osmocom/gsm/gsm_utils.h>
46#include <osmocom/gsm/lapdm.h>
47
48#include <osmocom/gsm/protocol/gsm_04_08.h>
49#include <osmocom/gsm/protocol/gsm_08_58.h>
50
Harald Welte1284c3e2018-05-01 18:11:02 +020051#define LAPD_U_SABM 0x7
52
Harald Welte1f0b8c22011-06-27 10:51:37 +020053/* TS 04.06 Figure 4 / Section 3.2 */
54#define LAPDm_LPD_NORMAL 0
55#define LAPDm_LPD_SMSCB 1
56#define LAPDm_SAPI_NORMAL 0
57#define LAPDm_SAPI_SMS 3
58#define LAPDm_ADDR(lpd, sapi, cr) ((((lpd) & 0x3) << 5) | (((sapi) & 0x7) << 2) | (((cr) & 0x1) << 1) | 0x1)
59
rootaf48bed2011-09-26 11:23:06 +020060#define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3)
Harald Welte1f0b8c22011-06-27 10:51:37 +020061#define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7)
62#define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1)
63#define LAPDm_ADDR_EA(addr) ((addr) & 0x1)
64
65/* TS 04.06 Table 3 / Section 3.4.3 */
66#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
67#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
68#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
69
70#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
71#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
72#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
73
74#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
75#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
76
77#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
78
79#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
80#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
81
Harald Welte1f0b8c22011-06-27 10:51:37 +020082#define LAPDm_LEN(len) ((len << 2) | 0x1)
83#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020084#define LAPDm_EL 0x1
85
86#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020087
88/* TS 04.06 Section 5.8.3 */
89#define N201_AB_SACCH 18
90#define N201_AB_SDCCH 20
91#define N201_AB_FACCH 20
92#define N201_Bbis 23
93#define N201_Bter_SACCH 21
94#define N201_Bter_SDCCH 23
95#define N201_Bter_FACCH 23
96#define N201_B4 19
97
98/* 5.8.2.1 N200 during establish and release */
99#define N200_EST_REL 5
100/* 5.8.2.1 N200 during timer recovery state */
101#define N200_TR_SACCH 5
102#define N200_TR_SDCCH 23
103#define N200_TR_FACCH_FR 34
104#define N200_TR_EFACCH_FR 48
105#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200106/* FIXME: set N200 depending on chan_nr */
107#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200108
109enum lapdm_format {
110 LAPDm_FMT_A,
111 LAPDm_FMT_B,
112 LAPDm_FMT_Bbis,
113 LAPDm_FMT_Bter,
114 LAPDm_FMT_B4,
115};
116
Maxadef12a2016-05-25 15:25:02 +0200117const struct value_string osmo_ph_prim_names[] = {
118 { PRIM_PH_DATA, "PH-DATA" },
119 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
120 { PRIM_PH_CONN, "PH-CONNECT" },
121 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
122 { PRIM_PH_RTS, "PH-RTS" },
123 { PRIM_MPH_INFO, "MPH-INFO" },
124 { PRIM_TCH, "TCH" },
125 { PRIM_TCH_RTS, "TCH-RTS" },
126 { 0, NULL }
127};
128
Harald Welte00b2faf2020-05-02 19:56:36 +0200129extern void *tall_lapd_ctx;
130
rootaf48bed2011-09-26 11:23:06 +0200131static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
132static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
133 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100134static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200135
136static void lapdm_dl_init(struct lapdm_datalink *dl,
Harald Welte00b2faf2020-05-02 19:56:36 +0200137 struct lapdm_entity *entity, int t200_ms, uint32_t n200,
138 const char *name)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200139{
140 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200141 dl->entity = entity;
Harald Welte00b2faf2020-05-02 19:56:36 +0200142 lapd_dl_init2(&dl->dl, 1, 8, 251, name); /* Section 5.8.5 of TS 04.06 */
rootaf48bed2011-09-26 11:23:06 +0200143 dl->dl.reestablish = 0; /* GSM uses no reestablish */
144 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
145 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100146 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200147 dl->dl.n200_est_rel = N200_EST_REL;
Harald Welte20de6202019-06-02 21:33:38 +0200148 dl->dl.n200 = n200;
rootaf48bed2011-09-26 11:23:06 +0200149 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Harald Welte20de6202019-06-02 21:33:38 +0200150 dl->dl.t200_sec = t200_ms / 1000; dl->dl.t200_usec = (t200_ms % 1000) * 1000;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200151}
152
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200153/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200154 * \param[in] le LAPDm entity
155 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200156 * \param[in] t200 T200 re-transmission timer for all SAPIs in seconds
157 *
158 * Don't use this function; It doesn't support different T200 values per API
159 * and doesn't permit the caller to specify the N200 counter, both of which
160 * are required by GSM specs and supported by lapdm_entity_init2().
Harald Welte6bdf0b12011-08-17 18:22:08 +0200161 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100162void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200163{
Harald Welte20de6202019-06-02 21:33:38 +0200164 /* convert from single full-second value to per-SAPI milli-second value */
165 int t200_ms_sapi_arr[_NR_DL_SAPI];
166 int i;
167
168 for (i = 0; i < ARRAY_SIZE(t200_ms_sapi_arr); i++)
169 t200_ms_sapi_arr[i] = t200 * 1000;
170
Harald Welte00b2faf2020-05-02 19:56:36 +0200171 return lapdm_entity_init3(le, mode, t200_ms_sapi_arr, N200, NULL);
Harald Welte20de6202019-06-02 21:33:38 +0200172}
173
174/*! initialize a LAPDm entity and all datalinks inside
175 * \param[in] le LAPDm entity
176 * \param[in] mode lapdm_mode (BTS/MS)
177 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
178 * \param[in] n200 N200 re-transmisison count
179 */
180void lapdm_entity_init2(struct lapdm_entity *le, enum lapdm_mode mode,
181 const int *t200_ms, int n200)
182{
Harald Welte00b2faf2020-05-02 19:56:36 +0200183 lapdm_entity_init3(le, mode, t200_ms, n200, NULL);
184}
185
186/*! initialize a LAPDm entity and all datalinks inside
187 * \param[in] le LAPDm entity
188 * \param[in] mode lapdm_mode (BTS/MS)
189 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
190 * \param[in] n200 N200 re-transmisison count
191 * \param[in] name human-readable name (will be copied internally + extended with SAPI)
192 */
193void lapdm_entity_init3(struct lapdm_entity *le, enum lapdm_mode mode,
194 const int *t200_ms, int n200, const char *name_pfx)
195{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200196 unsigned int i;
197
Harald Welte00b2faf2020-05-02 19:56:36 +0200198 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
199 char name[256];
200 if (name_pfx) {
201 snprintf(name, sizeof(name), "%s[%s]", name_pfx, i == 0 ? "0" : "3");
202 lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200, name);
203 } else
204 lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200, NULL);
205 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200206
207 lapdm_entity_set_mode(le, mode);
208}
209
Harald Welte20de6202019-06-02 21:33:38 +0200210static int get_n200_dcch(enum gsm_chan_t chan_t)
211{
212 switch (chan_t) {
213 case GSM_LCHAN_SDCCH:
214 return N200_TR_SDCCH;
215 case GSM_LCHAN_TCH_F:
216 return N200_TR_FACCH_FR;
217 case GSM_LCHAN_TCH_H:
218 return N200_TR_FACCH_HR;
219 default:
220 return -1;
221 }
222}
223
224/*! initialize a LAPDm channel and all its channels
225 * \param[in] lc lapdm_channel to be initialized
226 * \param[in] mode lapdm_mode (BTS/MS)
227 *
228 * Don't use this function; It doesn't support different T200 values per API
229 * and doesn't set the correct N200 counter, both of which
230 * are required by GSM specs and supported by lapdm_channel_init2().
231 */
232void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
233{
234 /* emulate old backwards-compatible behavior with 1s/2s */
235 const int t200_ms_dcch[_NR_DL_SAPI] = { 1000, 1000 };
236 const int t200_ms_acch[_NR_DL_SAPI] = { 2000, 2000 };
237
Harald Welte00b2faf2020-05-02 19:56:36 +0200238 lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, GSM_LCHAN_SDCCH, NULL);
Harald Welte20de6202019-06-02 21:33:38 +0200239}
240
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200241/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200242 * \param[in] lc \ref lapdm_channel to be initialized
243 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200244 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
245 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
246 * \param[in] chan_t GSM channel type (to correctly set N200)
Harald Welte6bdf0b12011-08-17 18:22:08 +0200247 */
Harald Welte20de6202019-06-02 21:33:38 +0200248int lapdm_channel_init2(struct lapdm_channel *lc, enum lapdm_mode mode,
249 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200250{
Harald Welte00b2faf2020-05-02 19:56:36 +0200251 return lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, chan_t, NULL);
252}
253
254/*! initialize a LAPDm channel and all its channels
255 * \param[in] lc \ref lapdm_channel to be initialized
256 * \param[in] mode \ref lapdm_mode (BTS/MS)
257 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
258 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
259 * \param[in] chan_t GSM channel type (to correctly set N200)
260 * \parma[in] name_pfx human-readable name (copied by function + extended with ACCH/DCCH)
261 */
262int lapdm_channel_init3(struct lapdm_channel *lc, enum lapdm_mode mode,
263 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t,
264 const char *name_pfx)
265{
Harald Welte20de6202019-06-02 21:33:38 +0200266 int n200_dcch = get_n200_dcch(chan_t);
Harald Welte00b2faf2020-05-02 19:56:36 +0200267 char namebuf[256];
268 char *name = NULL;
269
Harald Welte20de6202019-06-02 21:33:38 +0200270 if (n200_dcch < 0)
271 return -EINVAL;
272
Harald Welte00b2faf2020-05-02 19:56:36 +0200273 osmo_talloc_replace_string(tall_lapd_ctx, &lc->name, name_pfx);
274
275 if (name_pfx) {
276 snprintf(namebuf, sizeof(namebuf), "%s[ACCH]", name_pfx);
277 name = namebuf;
278 }
279 lapdm_entity_init3(&lc->lapdm_acch, mode, t200_ms_acch, N200_TR_SACCH, name);
Harald Welte3e8c5202018-05-04 20:58:48 +0200280 lc->lapdm_acch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200281
Harald Welte00b2faf2020-05-02 19:56:36 +0200282 if (name_pfx) {
283 snprintf(namebuf, sizeof(namebuf), "%s[DCCH]", name_pfx);
284 name = namebuf;
285 }
286 lapdm_entity_init3(&lc->lapdm_dcch, mode, t200_ms_dcch, n200_dcch, name);
Harald Welte3e8c5202018-05-04 20:58:48 +0200287 lc->lapdm_dcch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200288
289 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200290}
291
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200292/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200293void lapdm_entity_exit(struct lapdm_entity *le)
294{
295 unsigned int i;
296 struct lapdm_datalink *dl;
297
298 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
299 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200300 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200301 }
302}
303
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200304/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200305 *
306 * A convenience wrapper calling \ref lapdm_entity_exit on both
307 * entities inside the \ref lapdm_channel
308 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200309void lapdm_channel_exit(struct lapdm_channel *lc)
310{
311 lapdm_entity_exit(&lc->lapdm_acch);
312 lapdm_entity_exit(&lc->lapdm_dcch);
313}
314
Daniel Willmann55405fb2014-03-26 13:45:17 +0100315struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200316{
317 switch (sapi) {
318 case LAPDm_SAPI_NORMAL:
319 return &le->datalink[0];
320 case LAPDm_SAPI_SMS:
321 return &le->datalink[1];
322 default:
323 return NULL;
324 }
325}
326
Harald Welte1f0b8c22011-06-27 10:51:37 +0200327/* Append padding (if required) */
328static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
329{
330 int pad_len = n201 - msgb_l2len(msg);
331 uint8_t *data;
332
333 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200334 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200335 "cannot pad message that is already too big!\n");
336 return;
337 }
338
339 data = msgb_put(msg, pad_len);
340 memset(data, 0x2B, pad_len);
341}
342
343/* input function that L2 calls when sending messages up to L3 */
344static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
345{
346 if (!le->l3_cb) {
347 msgb_free(msg);
348 return -EIO;
349 }
350
351 /* call the layer2 message handler that is registered */
352 return le->l3_cb(msg, le, le->l3_ctx);
353}
354
355/* write a frame into the tx queue */
356static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200357 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200358{
359 struct lapdm_entity *le = dl->entity;
360 struct osmo_phsap_prim pp;
361
362 /* if there is a pending message, queue it */
363 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200364 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200365 *msgb_push(msg, 1) = link_id;
366 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200367 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200368 return -EBUSY;
369 }
370
371 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
372 PRIM_OP_REQUEST, msg);
373 pp.u.data.chan_nr = chan_nr;
374 pp.u.data.link_id = link_id;
375
376 /* send the frame now */
377 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200378 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200379
380 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
381}
382
383static struct msgb *tx_dequeue_msgb(struct lapdm_entity *le)
384{
385 struct lapdm_datalink *dl;
386 int last = le->last_tx_dequeue;
387 int i = last, n = ARRAY_SIZE(le->datalink);
388 struct msgb *msg = NULL;
389
390 /* round-robin dequeue */
391 do {
392 /* next */
393 i = (i + 1) % n;
394 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200395 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200396 break;
397 } while (i != last);
398
399 if (msg) {
400 /* Set last dequeue position */
401 le->last_tx_dequeue = i;
402 }
403
404 return msg;
405}
406
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200407/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200408 * a osmo_phsap_prim */
409int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
410{
411 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200412 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200413
414 msg = tx_dequeue_msgb(le);
415 if (!msg)
416 return -ENODEV;
417
418 /* if we have a message, send PH-DATA.req */
419 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
420 PRIM_OP_REQUEST, msg);
421
422 /* Pull chan_nr and link_id */
423 pp->u.data.chan_nr = *msg->data;
424 msgb_pull(msg, 1);
425 pp->u.data.link_id = *msg->data;
426 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200427 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200428 msgb_pull(msg, 1);
429
430 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200431 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200432
433 return 0;
434}
435
436/* get next frame from the tx queue. because the ms has multiple datalinks,
437 * each datalink's queue is read round-robin.
438 */
439static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
440{
441 struct osmo_phsap_prim pp;
442
443 /* we may send again */
444 le->tx_pending = 0;
445
446 /* free confirm message */
447 if (msg)
448 msgb_free(msg);
449
450 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
451 /* no message in all queues */
452
453 /* If user didn't request PH-EMPTY_FRAME.req, abort */
454 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
455 return 0;
456
457 /* otherwise, send PH-EMPTY_FRAME.req */
458 osmo_prim_init(&pp.oph, SAP_GSM_PH,
459 PRIM_PH_EMPTY_FRAME,
460 PRIM_OP_REQUEST, NULL);
461 } else {
462 le->tx_pending = 1;
463 }
464
465 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
466}
467
Harald Welte542301b2018-04-19 16:11:14 +0200468/* Is a given msg_type "transparent" as per TS 48.058 Section 8.1 */
469static int rsl_is_transparent(uint8_t msg_type)
470{
471 switch (msg_type) {
472 case RSL_MT_DATA_IND:
473 case RSL_MT_UNIT_DATA_IND:
474 return 1;
475 case RSL_MT_DATA_REQ:
476 case RSL_MT_UNIT_DATA_REQ:
477 return 1;
478 default:
479 return 0;
480 }
481}
482
Harald Welte1f0b8c22011-06-27 10:51:37 +0200483/* Create RSLms various RSLms messages */
484static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
485 struct msgb *msg)
486{
Harald Welte542301b2018-04-19 16:11:14 +0200487 int transparent = rsl_is_transparent(msg_type);
488
Harald Welte1f0b8c22011-06-27 10:51:37 +0200489 /* Add the RSL + RLL header */
Harald Welte542301b2018-04-19 16:11:14 +0200490 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200491
492 /* send off the RSLms message to L3 */
493 return rslms_sendmsg(msg, mctx->dl->entity);
494}
495
496/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
497static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
498{
499 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200500
501 /* Add the RSL + RLL header */
502 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welted977f5f2018-05-08 21:53:28 +0200503
Harald Weltef1bdf782018-05-08 22:03:20 +0200504 /* Add two non-standard IEs carrying MS power and TA values for B4 (SACCH) */
505 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
506 msgb_tv_push(msg, RSL_IE_MS_POWER, mctx->tx_power_ind);
507 msgb_tv_push(msg, RSL_IE_TIMING_ADVANCE, mctx->ta_ind);
508 }
Harald Welted977f5f2018-05-08 21:53:28 +0200509
Harald Welte1f0b8c22011-06-27 10:51:37 +0200510 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
511 mctx->link_id, 1);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100512
Harald Welte1f0b8c22011-06-27 10:51:37 +0200513 return rslms_sendmsg(msg, mctx->dl->entity);
514}
515
516static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
517{
518 struct msgb *msg;
Harald Welte542301b2018-04-19 16:11:14 +0200519 int transparent = rsl_is_transparent(msg_type);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200520
Harald Welte542301b2018-04-19 16:11:14 +0200521 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200522
523 /* send off the RSLms message to L3 */
524 return rslms_sendmsg(msg, mctx->dl->entity);
525}
526
527static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
528{
529 struct msgb *msg;
530
Harald Welte00b2faf2020-05-02 19:56:36 +0200531 LOGDL(&mctx->dl->dl, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte542301b2018-04-19 16:11:14 +0200532 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200533 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
534 return rslms_sendmsg(msg, mctx->dl->entity);
535}
536
rootaf48bed2011-09-26 11:23:06 +0200537/* DLSAP L2 -> L3 (RSLms) */
538static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
539 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200540{
rootaf48bed2011-09-26 11:23:06 +0200541 struct lapd_datalink *dl = lctx->dl;
542 struct lapdm_datalink *mdl =
543 container_of(dl, struct lapdm_datalink, dl);
544 struct lapdm_msg_ctx *mctx = &mdl->mctx;
545 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200546
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200547 switch (OSMO_PRIM_HDR(&dp->oph)) {
548 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
549 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200550 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200551 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
552 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200553 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200554 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
555 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200556 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200557 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
558 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
559 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
560 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200561 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200562 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
563 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200564 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200565 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
566 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200567 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200568 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
569 rsl_rll_error(dp->u.error_ind.cause, mctx);
570 if (dp->oph.msg)
571 msgb_free(dp->oph.msg);
572 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200573 }
rootaf48bed2011-09-26 11:23:06 +0200574
575 if (!rll_msg) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200576 LOGDL(dl, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
rootaf48bed2011-09-26 11:23:06 +0200577 "fix!\n", dp->oph.primitive, dp->oph.operation);
578 return -EINVAL;
579 }
580
581 if (!dp->oph.msg)
582 return send_rll_simple(rll_msg, mctx);
583
584 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200585}
586
rootaf48bed2011-09-26 11:23:06 +0200587/* send a data frame to layer 1 */
588static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200589{
rootaf48bed2011-09-26 11:23:06 +0200590 uint8_t l3_len = msg->tail - msg->data;
591 struct lapd_datalink *dl = lctx->dl;
592 struct lapdm_datalink *mdl =
593 container_of(dl, struct lapdm_datalink, dl);
594 struct lapdm_msg_ctx *mctx = &mdl->mctx;
595 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200596
rootaf48bed2011-09-26 11:23:06 +0200597 /* prepend l2 header */
598 msg->l2h = msgb_push(msg, 3);
599 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
600 /* EA is set here too */
601 switch (format) {
602 case LAPD_FORM_I:
603 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
604 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200605 break;
rootaf48bed2011-09-26 11:23:06 +0200606 case LAPD_FORM_S:
607 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200608 break;
rootaf48bed2011-09-26 11:23:06 +0200609 case LAPD_FORM_U:
610 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200611 break;
612 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200613 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200614 return -EINVAL;
615 }
rootaf48bed2011-09-26 11:23:06 +0200616 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
617 if (lctx->more)
618 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200619
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100620 /* add ACCH header with last indicated tx-power and TA */
621 if ((mctx->link_id & 0x40)) {
622 struct lapdm_entity *le = mdl->entity;
623
624 msg->l2h = msgb_push(msg, 2);
625 msg->l2h[0] = le->tx_power;
626 msg->l2h[1] = le->ta;
627 }
628
rootaf48bed2011-09-26 11:23:06 +0200629 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
630 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200631}
632
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100633static int update_pending_frames(struct lapd_msg_ctx *lctx)
634{
635 struct lapd_datalink *dl = lctx->dl;
636 struct msgb *msg;
637 int rc = -1;
638
639 llist_for_each_entry(msg, &dl->tx_queue, list) {
640 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
641 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
642 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
643 rc = 0;
644 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200645 LOGDL(dl, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100646 }
647 }
648
649 return rc;
650}
651
Harald Welte1284c3e2018-05-01 18:11:02 +0200652/* determine if receiving a given LAPDm message is not permitted */
653static int lapdm_rx_not_permitted(const struct lapdm_entity *le,
654 const struct lapd_msg_ctx *lctx)
655{
656 /* we currently only implement SABM related checks here */
657 if (lctx->format != LAPD_FORM_U || lctx->s_u != LAPD_U_SABM)
658 return 0;
659
660 if (le->mode == LAPDM_MODE_BTS) {
661 if (le == &le->lapdm_ch->lapdm_acch) {
662 /* no contention resolution on SACCH */
663 if (lctx->length > 0)
664 return RLL_CAUSE_SABM_INFO_NOTALL;
665 } else {
666 switch (lctx->sapi) {
Harald Welte1284c3e2018-05-01 18:11:02 +0200667 case 3:
668 /* SAPI3 doesn't support contention resolution */
669 if (lctx->length > 0)
670 return RLL_CAUSE_SABM_INFO_NOTALL;
671 break;
Harald Welteb82a4072018-05-09 16:31:16 +0200672 default:
673 break;
Harald Welte1284c3e2018-05-01 18:11:02 +0200674 }
675 }
676 } else if (le->mode == LAPDM_MODE_MS) {
677 /* contention resolution (L3 present) is only sent by MS, but
678 * never received by it */
679 if (lctx->length > 0)
680 return RLL_CAUSE_SABM_INFO_NOTALL;
681 }
682 return 0;
683}
684
Harald Welte1f0b8c22011-06-27 10:51:37 +0200685/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200686static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
687 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200688{
689 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200690 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200691 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200692 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200693 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200694 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200695
696 /* when we reach here, we have a msgb with l2h pointing to the raw
697 * 23byte mac block. The l1h has already been purged. */
698
rootaf48bed2011-09-26 11:23:06 +0200699 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200700 mctx.chan_nr = chan_nr;
701 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200702
Harald Welte1f0b8c22011-06-27 10:51:37 +0200703 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
704 if (cbits == 0x10 || cbits == 0x12) {
705 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
706 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200707 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200708 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200709 } else {
710 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200711 /* It was received from network on SACCH */
712
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100713 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
714 if (le->mode == LAPDM_MODE_MS
715 && LAPDm_CTRL_is_U(msg->l2h[3])
716 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200717 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200718 n201 = N201_B4;
719 LOGP(DLLAPD, LOGL_INFO, "fmt=B4\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200720 } else {
721 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200722 n201 = N201_AB_SACCH;
723 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Harald Welte7ca604b2011-06-29 12:13:51 +0200724 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200725 /* SACCH frames have a two-byte L1 header that
726 * OsmocomBB L1 doesn't strip */
727 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
728 mctx.ta_ind = msg->l2h[1];
729 msgb_pull(msg, 2);
730 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200731 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200732 } else {
733 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200734 LOGP(DLLAPD, LOGL_INFO, "fmt=B\n");
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100735 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200736 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200737 }
738 }
739
Daniel Willmann55405fb2014-03-26 13:45:17 +0100740 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200741 /* G.2.1 No action on frames containing an unallocated SAPI. */
742 if (!mctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200743 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200744 msgb_free(msg);
745 return -EIO;
746 }
747
Harald Welte1f0b8c22011-06-27 10:51:37 +0200748 switch (mctx.lapdm_fmt) {
749 case LAPDm_FMT_A:
750 case LAPDm_FMT_B:
751 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200752 lctx.dl = &mctx.dl->dl;
753 /* obtain SAPI from address field */
754 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
755 /* G.2.3 EA bit set to "0" is not allowed in GSM */
756 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200757 LOGDL(lctx.dl, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200758 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200759 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200760 return -EINVAL;
761 }
rootaf48bed2011-09-26 11:23:06 +0200762 /* adress field */
763 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
764 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
765 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
766 /* command field */
767 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
768 lctx.format = LAPD_FORM_I;
769 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
770 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
771 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
772 lctx.format = LAPD_FORM_S;
773 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
774 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
775 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
776 lctx.format = LAPD_FORM_U;
777 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
778 } else
779 lctx.format = LAPD_FORM_UKN;
780 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
781 if (lctx.sapi != LAPDm_SAPI_NORMAL
782 && lctx.sapi != LAPDm_SAPI_SMS
783 && lctx.format == LAPD_FORM_U
784 && lctx.s_u == LAPDm_U_UI) {
785 /* 5.3.3 UI frames with invalid SAPI values shall be
786 * discarded
787 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200788 LOGDL(lctx.dl, LOGL_INFO, "sapi=%u (discarding)\n", lctx.sapi);
rootaf48bed2011-09-26 11:23:06 +0200789 msgb_free(msg);
790 return 0;
791 }
792 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
793 lctx.n201 = n201;
794 lctx.length = n201;
795 lctx.more = 0;
796 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100797 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200798 } else {
799 /* length field */
800 if (!(msg->l2h[2] & LAPDm_EL)) {
801 /* G.4.1 If the EL bit is set to "0", an
802 * MDL-ERROR-INDICATION primitive with cause
803 * "frame not implemented" is sent to the
804 * mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +0200805 LOGDL(lctx.dl, LOGL_NOTICE, "we don't support multi-octet length\n");
rootaf48bed2011-09-26 11:23:06 +0200806 msgb_free(msg);
807 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
808 return -EINVAL;
809 }
810 lctx.n201 = n201;
811 lctx.length = msg->l2h[2] >> 2;
812 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
813 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100814 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200815 }
816 /* store context for messages from lapd */
817 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200818 rc =lapdm_rx_not_permitted(le, &lctx);
819 if (rc > 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200820 LOGDL(lctx.dl, LOGL_NOTICE, "received message not permitted\n");
Harald Welte1284c3e2018-05-01 18:11:02 +0200821 msgb_free(msg);
822 rsl_rll_error(rc, &mctx);
823 return -EINVAL;
824 }
rootaf48bed2011-09-26 11:23:06 +0200825 /* send to LAPD */
826 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200827 break;
828 case LAPDm_FMT_Bter:
829 /* FIXME */
830 msgb_free(msg);
831 break;
832 case LAPDm_FMT_Bbis:
833 /* directly pass up to layer3 */
rootaf48bed2011-09-26 11:23:06 +0200834 LOGP(DLLAPD, LOGL_INFO, "fmt=Bbis UI\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200835 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100836 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200837 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
838 break;
839 default:
840 msgb_free(msg);
841 }
842
843 return rc;
844}
845
846/* input into layer2 (from layer 1) */
847static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
848{
849 struct abis_rsl_cchan_hdr *ch;
850 struct gsm48_req_ref req_ref;
851 struct gsm_time gt;
852 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
853
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200854 if (!msg)
855 return -ENOMEM;
856
Harald Welte1f0b8c22011-06-27 10:51:37 +0200857 msg->l2h = msgb_push(msg, sizeof(*ch));
858 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
859 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
860 ch->chan_nr = RSL_CHAN_RACH;
861
862 /* generate a RSL CHANNEL REQUIRED message */
863 gsm_fn2gsmtime(&gt, fn);
864 req_ref.ra = ra;
865 req_ref.t1 = gt.t1; /* FIXME: modulo? */
866 req_ref.t2 = gt.t2;
867 req_ref.t3_low = gt.t3 & 7;
868 req_ref.t3_high = gt.t3 >> 3;
869
870 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
871 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
872
873 return rslms_sendmsg(msg, le);
874}
875
876static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
877
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200878/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200879int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
880{
881 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
882 int rc = 0;
883
884 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200885 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200886 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100887 rc = -ENODEV;
888 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200889 }
890
891 switch (oph->primitive) {
892 case PRIM_PH_DATA:
893 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200894 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200895 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100896 rc = -ENODEV;
897 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200898 }
899 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
900 pp->u.data.link_id);
901 break;
902 case PRIM_PH_RTS:
903 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200904 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200905 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100906 rc = -ENODEV;
907 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200908 }
909 rc = l2_ph_data_conf(oph->msg, le);
910 break;
911 case PRIM_PH_RACH:
912 switch (oph->operation) {
913 case PRIM_OP_INDICATION:
914 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
915 pp->u.rach_ind.acc_delay);
916 break;
917 case PRIM_OP_CONFIRM:
918 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
919 break;
920 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100921 rc = -EIO;
922 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200923 }
924 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100925 default:
926 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
927 oph->primitive);
928 rc = -EINVAL;
929 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200930 }
931
932 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100933
934free:
935 msgb_free(oph->msg);
936 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200937}
938
939
940/* L3 -> L2 / RSLMS -> LAPDm */
941
rootaf48bed2011-09-26 11:23:06 +0200942/* Set LAPDm context for established connection */
943static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
944 uint8_t link_id, int n201, uint8_t sapi)
945{
946 memset(&dl->mctx, 0, sizeof(dl->mctx));
947 dl->mctx.dl = dl;
948 dl->mctx.chan_nr = chan_nr;
949 dl->mctx.link_id = link_id;
950 dl->dl.lctx.dl = &dl->dl;
951 dl->dl.lctx.n201 = n201;
952 dl->dl.lctx.sapi = sapi;
953
954 return 0;
955}
956
Harald Welte1f0b8c22011-06-27 10:51:37 +0200957/* L3 requests establishment of data link */
958static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
959{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200960 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
961 uint8_t chan_nr = rllh->chan_nr;
962 uint8_t link_id = rllh->link_id;
963 uint8_t sapi = rllh->link_id & 7;
964 struct tlv_parsed tv;
965 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100966 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200967 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200968
rootaf48bed2011-09-26 11:23:06 +0200969 /* Set LAPDm context for established connection */
970 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200971
rootaf48bed2011-09-26 11:23:06 +0200972 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200973 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200974 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200975 /* contention resolution establishment procedure */
976 if (sapi != 0) {
977 /* According to clause 6, the contention resolution
978 * procedure is only permitted with SAPI value 0 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200979 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200980 "resolution (discarding)\n");
981 msgb_free(msg);
982 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
983 }
984 /* transmit a SABM command with the P bit set to "1". The SABM
985 * command shall contain the layer 3 message unit */
986 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200987 } else {
988 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +0200989 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200990 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200991 }
992
993 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100994 if (length > n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200995 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100996 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200997 msgb_free(msg);
998 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
999 }
1000
rootaf48bed2011-09-26 11:23:06 +02001001 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001002 msgb_pull_to_l3(msg);
1003 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001004
rootaf48bed2011-09-26 11:23:06 +02001005 /* prepare prim */
1006 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001007
rootaf48bed2011-09-26 11:23:06 +02001008 /* send to L2 */
1009 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001010}
1011
1012/* L3 requests transfer of unnumbered information */
1013static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1014{
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001015 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001016 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1017 uint8_t chan_nr = rllh->chan_nr;
1018 uint8_t link_id = rllh->link_id;
1019 uint8_t sapi = link_id & 7;
1020 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +01001021 int length, ui_bts;
1022
1023 if (!le) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001024 LOGDL(&dl->dl, LOGL_ERROR, "lapdm_datalink without entity error\n");
Max777be2e2017-03-01 18:16:44 +01001025 msgb_free(msg);
1026 return -EMLINK;
1027 }
1028 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +02001029
1030 /* check if the layer3 message length exceeds N201 */
1031
1032 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1033
1034 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001035 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001036 }
1037 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001038 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001039 }
1040 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001041 LOGDL(&dl->dl, LOGL_ERROR, "unit data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001042 msgb_free(msg);
1043 return -EINVAL;
1044 }
rootaf48bed2011-09-26 11:23:06 +02001045 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001046 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1047 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +02001048 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001049 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +02001050 "(discarding)\n", length,
1051 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001052 msgb_free(msg);
1053 return -EIO;
1054 }
1055
Harald Welte00b2faf2020-05-02 19:56:36 +02001056 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 +02001057
rootaf48bed2011-09-26 11:23:06 +02001058 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001059 msgb_pull_to_l3(msg);
1060 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001061
1062 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +02001063 msg->l2h = msgb_push(msg, 2 + !ui_bts);
1064 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
1065 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001066 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +02001067 msg->l2h[2] = LAPDm_LEN(length);
1068 if (link_id & 0x40) {
1069 msg->l2h = msgb_push(msg, 2);
1070 msg->l2h[0] = le->tx_power;
1071 msg->l2h[1] = le->ta;
1072 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001073
1074 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +02001075 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001076}
1077
1078/* L3 requests transfer of acknowledged information */
1079static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1080{
1081 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1082 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +02001083 int length;
1084 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001085
1086 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1087 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001088 LOGDL(&dl->dl, LOGL_ERROR, "data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001089 msgb_free(msg);
1090 return -EINVAL;
1091 }
rootaf48bed2011-09-26 11:23:06 +02001092 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
1093 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001094
rootaf48bed2011-09-26 11:23:06 +02001095 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001096 msgb_pull_to_l3(msg);
1097 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001098
rootaf48bed2011-09-26 11:23:06 +02001099 /* prepare prim */
1100 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001101
rootaf48bed2011-09-26 11:23:06 +02001102 /* send to L2 */
1103 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001104}
1105
1106/* L3 requests suspension of data link */
1107static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1108{
1109 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1110 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001111 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001112
1113 if (sapi != 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001114 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001115 msgb_free(msg);
1116 return -EINVAL;
1117 }
1118
rootaf48bed2011-09-26 11:23:06 +02001119 /* prepare prim */
1120 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001121
rootaf48bed2011-09-26 11:23:06 +02001122 /* send to L2 */
1123 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001124}
1125
1126/* L3 requests resume of data link */
1127static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1128{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001129 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001130 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001131 uint8_t chan_nr = rllh->chan_nr;
1132 uint8_t link_id = rllh->link_id;
1133 uint8_t sapi = rllh->link_id & 7;
1134 struct tlv_parsed tv;
1135 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001136 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001137 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001138
rootaf48bed2011-09-26 11:23:06 +02001139 /* Set LAPDm context for established connection */
1140 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001141
1142 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1143 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001144 LOGDL(&dl->dl, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001145 msgb_free(msg);
1146 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1147 }
rootaf48bed2011-09-26 11:23:06 +02001148 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001149 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1150
rootaf48bed2011-09-26 11:23:06 +02001151 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001152 msgb_pull_to_l3(msg);
1153 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001154
rootaf48bed2011-09-26 11:23:06 +02001155 /* prepare prim */
1156 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1157 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001158
rootaf48bed2011-09-26 11:23:06 +02001159 /* send to L2 */
1160 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001161}
1162
1163/* L3 requests release of data link */
1164static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1165{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001166 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001167 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001168 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001169
1170 /* get release mode */
1171 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1172 mode = rllh->data[1] & 1;
1173
Harald Welte1f0b8c22011-06-27 10:51:37 +02001174 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001175 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001176
1177 /* 04.06 3.8.3: No information field is permitted with the DISC
1178 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001179 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001180
rootaf48bed2011-09-26 11:23:06 +02001181 /* prepare prim */
1182 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1183 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001184
rootaf48bed2011-09-26 11:23:06 +02001185 /* send to L2 */
1186 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001187}
1188
1189/* L3 requests channel in idle state */
1190static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1191{
1192 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1193 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1194 struct osmo_phsap_prim pp;
1195
1196 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1197 PRIM_OP_REQUEST, NULL);
1198
1199 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001200 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001201 return -EINVAL;
1202 }
1203 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001204 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001205 return -EINVAL;
1206 }
1207 pp.u.rach_req.ra = cch->data[1];
1208 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1209 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1210
1211 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001212 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001213 return -EINVAL;
1214 }
1215 /* TA = 0 - delay */
1216 pp.u.rach_req.ta = 0 - cch->data[5];
1217
1218 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001219 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001220 return -EINVAL;
1221 }
1222 pp.u.rach_req.tx_power = cch->data[7];
1223
1224 msgb_free(msg);
1225
1226 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1227}
1228
1229/* L1 confirms channel request */
1230static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1231{
1232 struct abis_rsl_cchan_hdr *ch;
1233 struct gsm_time tm;
1234 struct gsm48_req_ref *ref;
1235
1236 gsm_fn2gsmtime(&tm, frame_nr);
1237
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001238 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001239 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1240 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1241 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1242 ch->chan_nr = RSL_CHAN_RACH;
1243 ch->data[0] = RSL_IE_REQ_REFERENCE;
1244 ref = (struct gsm48_req_ref *) (ch->data + 1);
1245 ref->t1 = tm.t1;
1246 ref->t2 = tm.t2;
1247 ref->t3_low = tm.t3 & 0x7;
1248 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001249
Harald Welte1f0b8c22011-06-27 10:51:37 +02001250 return rslms_sendmsg(msg, le);
1251}
1252
Harald Welte1f0b8c22011-06-27 10:51:37 +02001253/* incoming RSLms RLL message from L3 */
1254static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1255{
1256 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1257 int msg_type = rllh->c.msg_type;
1258 uint8_t sapi = rllh->link_id & 7;
1259 struct lapdm_entity *le;
1260 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001261 int rc = 0;
1262
1263 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001264 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001265 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001266 return -EINVAL;
1267 }
1268
1269 if (rllh->link_id & 0x40)
1270 le = &lc->lapdm_acch;
1271 else
1272 le = &lc->lapdm_dcch;
1273
Harald Welte1a87c1b2015-12-14 15:26:07 +01001274 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1275 * BTS side */
1276 if (le->mode == LAPDM_MODE_BTS) {
1277 switch (msg_type) {
1278 case RSL_MT_SUSP_REQ:
1279 case RSL_MT_RES_REQ:
1280 case RSL_MT_RECON_REQ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001281 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' unsupported in BTS side LAPDm\n",
Harald Welte1a87c1b2015-12-14 15:26:07 +01001282 lc->name, rsl_msg_name(msg_type));
1283 msgb_free(msg);
1284 return -EINVAL;
1285 break;
1286 default:
1287 break;
1288 }
1289 }
1290
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001291 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001292 * SAPI.
1293 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001294 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001295 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001296 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001297 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001298 return -EINVAL;
1299 }
1300
Daniel Willmanne5233922012-12-25 23:15:50 +01001301 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001302 case RSL_MT_DATA_REQ:
1303 case RSL_MT_SUSP_REQ:
1304 case RSL_MT_REL_REQ:
1305 /* This is triggered in abnormal error conditions where
1306 * set_lapdm_context() was not called for the channel earlier. */
1307 if (!dl->dl.lctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001308 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001309 lc->name, rsl_msg_name(msg_type), sapi);
1310 msgb_free(msg);
1311 return -EINVAL;
1312 }
1313 break;
1314 default:
Harald Welte00b2faf2020-05-02 19:56:36 +02001315 LOGP(DLLAPD, LOGL_INFO, "(%s) RLL Message '%s' received. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001316 lc->name, rsl_msg_name(msg_type), sapi);
1317 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001318
rootaf48bed2011-09-26 11:23:06 +02001319 switch (msg_type) {
1320 case RSL_MT_UNIT_DATA_REQ:
1321 rc = rslms_rx_rll_udata_req(msg, dl);
1322 break;
1323 case RSL_MT_EST_REQ:
1324 rc = rslms_rx_rll_est_req(msg, dl);
1325 break;
1326 case RSL_MT_DATA_REQ:
1327 rc = rslms_rx_rll_data_req(msg, dl);
1328 break;
1329 case RSL_MT_SUSP_REQ:
1330 rc = rslms_rx_rll_susp_req(msg, dl);
1331 break;
1332 case RSL_MT_RES_REQ:
1333 rc = rslms_rx_rll_res_req(msg, dl);
1334 break;
1335 case RSL_MT_RECON_REQ:
1336 rc = rslms_rx_rll_res_req(msg, dl);
1337 break;
1338 case RSL_MT_REL_REQ:
1339 rc = rslms_rx_rll_rel_req(msg, dl);
1340 break;
1341 default:
1342 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001343 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001344 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001345 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001346
1347 return rc;
1348}
1349
1350/* incoming RSLms COMMON CHANNEL message from L3 */
1351static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1352{
1353 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1354 int msg_type = cch->c.msg_type;
1355 int rc = 0;
1356
1357 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001358 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001359 return -EINVAL;
1360 }
1361
1362 switch (msg_type) {
1363 case RSL_MT_CHAN_RQD:
1364 /* create and send RACH request */
1365 rc = rslms_rx_chan_rqd(lc, msg);
1366 break;
1367 default:
rootaf48bed2011-09-26 11:23:06 +02001368 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001369 msg_type);
1370 msgb_free(msg);
1371 return 0;
1372 }
1373
1374 return rc;
1375}
1376
Harald Welte7023aa02019-05-19 12:17:06 +02001377/*! Receive a RSLms \ref msgb from Layer 3. 'msg' ownership is transferred,
1378 * i.e. caller must not free it */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001379int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1380{
1381 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1382 int rc = 0;
1383
1384 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001385 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte7023aa02019-05-19 12:17:06 +02001386 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001387 return -EINVAL;
1388 }
1389
1390 switch (rslh->msg_discr & 0xfe) {
1391 case ABIS_RSL_MDISC_RLL:
1392 rc = rslms_rx_rll(msg, lc);
1393 break;
1394 case ABIS_RSL_MDISC_COM_CHAN:
1395 rc = rslms_rx_com_chan(msg, lc);
1396 break;
1397 default:
rootaf48bed2011-09-26 11:23:06 +02001398 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001399 "discriminator 0x%02x", rslh->msg_discr);
1400 msgb_free(msg);
1401 return -EINVAL;
1402 }
1403
1404 return rc;
1405}
1406
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001407/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001408int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1409{
rootaf48bed2011-09-26 11:23:06 +02001410 int i;
1411 enum lapd_mode lm;
1412
Harald Welte1f0b8c22011-06-27 10:51:37 +02001413 switch (mode) {
1414 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001415 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001416 break;
1417 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001418 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001419 break;
1420 default:
1421 return -EINVAL;
1422 }
1423
rootaf48bed2011-09-26 11:23:06 +02001424 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1425 lapd_set_mode(&le->datalink[i].dl, lm);
1426 }
1427
Harald Welte1f0b8c22011-06-27 10:51:37 +02001428 le->mode = mode;
1429
1430 return 0;
1431}
1432
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001433/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001434int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1435{
1436 int rc;
1437
1438 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1439 if (rc < 0)
1440 return rc;
1441
1442 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1443}
1444
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001445/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001446void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1447{
1448 lc->lapdm_dcch.l1_prim_cb = cb;
1449 lc->lapdm_acch.l1_prim_cb = cb;
1450 lc->lapdm_dcch.l1_ctx = ctx;
1451 lc->lapdm_acch.l1_ctx = ctx;
1452}
1453
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001454/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001455void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1456{
1457 lc->lapdm_dcch.l3_cb = cb;
1458 lc->lapdm_acch.l3_cb = cb;
1459 lc->lapdm_dcch.l3_ctx = ctx;
1460 lc->lapdm_acch.l3_ctx = ctx;
1461}
1462
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001463/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001464void lapdm_entity_reset(struct lapdm_entity *le)
1465{
1466 struct lapdm_datalink *dl;
1467 int i;
1468
1469 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1470 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001471 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001472 }
1473}
1474
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001475/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001476void lapdm_channel_reset(struct lapdm_channel *lc)
1477{
1478 lapdm_entity_reset(&lc->lapdm_dcch);
1479 lapdm_entity_reset(&lc->lapdm_acch);
1480}
1481
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001482/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001483void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1484{
1485 le->flags = flags;
1486}
1487
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001488/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001489void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1490{
1491 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1492 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1493}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001494
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001495/*! @} */