blob: e7d46e6315a38764ef811b2edddfcf27cd6808f4 [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)
60
61/* TS 04.06 Table 3 / Section 3.4.3 */
62#define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1))
63#define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1)
64#define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3)
65
66#define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0)
67#define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1)
68#define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3)
69
70#define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3)
71#define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1)
72
73#define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2)
74
75#define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1)
76#define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5)
77
Harald Welte1f0b8c22011-06-27 10:51:37 +020078#define LAPDm_LEN(len) ((len << 2) | 0x1)
79#define LAPDm_MORE 0x2
rootaf48bed2011-09-26 11:23:06 +020080#define LAPDm_EL 0x1
81
82#define LAPDm_U_UI 0x0
Harald Welte1f0b8c22011-06-27 10:51:37 +020083
84/* TS 04.06 Section 5.8.3 */
85#define N201_AB_SACCH 18
86#define N201_AB_SDCCH 20
87#define N201_AB_FACCH 20
88#define N201_Bbis 23
89#define N201_Bter_SACCH 21
90#define N201_Bter_SDCCH 23
91#define N201_Bter_FACCH 23
92#define N201_B4 19
93
94/* 5.8.2.1 N200 during establish and release */
95#define N200_EST_REL 5
96/* 5.8.2.1 N200 during timer recovery state */
97#define N200_TR_SACCH 5
98#define N200_TR_SDCCH 23
99#define N200_TR_FACCH_FR 34
100#define N200_TR_EFACCH_FR 48
101#define N200_TR_FACCH_HR 29
rootaf48bed2011-09-26 11:23:06 +0200102/* FIXME: set N200 depending on chan_nr */
103#define N200 N200_TR_SDCCH
Harald Welte1f0b8c22011-06-27 10:51:37 +0200104
105enum lapdm_format {
106 LAPDm_FMT_A,
107 LAPDm_FMT_B,
108 LAPDm_FMT_Bbis,
109 LAPDm_FMT_Bter,
110 LAPDm_FMT_B4,
111};
112
Maxadef12a2016-05-25 15:25:02 +0200113const struct value_string osmo_ph_prim_names[] = {
114 { PRIM_PH_DATA, "PH-DATA" },
115 { PRIM_PH_RACH, "PH-RANDOM_ACCESS" },
116 { PRIM_PH_CONN, "PH-CONNECT" },
117 { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" },
118 { PRIM_PH_RTS, "PH-RTS" },
119 { PRIM_MPH_INFO, "MPH-INFO" },
120 { PRIM_TCH, "TCH" },
121 { PRIM_TCH_RTS, "TCH-RTS" },
122 { 0, NULL }
123};
124
Harald Welte00b2faf2020-05-02 19:56:36 +0200125extern void *tall_lapd_ctx;
126
rootaf48bed2011-09-26 11:23:06 +0200127static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg);
128static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
129 struct lapd_msg_ctx *lctx);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100130static int update_pending_frames(struct lapd_msg_ctx *lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200131
132static void lapdm_dl_init(struct lapdm_datalink *dl,
Harald Welte00b2faf2020-05-02 19:56:36 +0200133 struct lapdm_entity *entity, int t200_ms, uint32_t n200,
134 const char *name)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200135{
136 memset(dl, 0, sizeof(*dl));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200137 dl->entity = entity;
Harald Welte00b2faf2020-05-02 19:56:36 +0200138 lapd_dl_init2(&dl->dl, 1, 8, 251, name); /* Section 5.8.5 of TS 04.06 */
rootaf48bed2011-09-26 11:23:06 +0200139 dl->dl.reestablish = 0; /* GSM uses no reestablish */
140 dl->dl.send_ph_data_req = lapdm_send_ph_data_req;
141 dl->dl.send_dlsap = send_rslms_dlsap;
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100142 dl->dl.update_pending_frames = update_pending_frames;
rootaf48bed2011-09-26 11:23:06 +0200143 dl->dl.n200_est_rel = N200_EST_REL;
Harald Welte20de6202019-06-02 21:33:38 +0200144 dl->dl.n200 = n200;
rootaf48bed2011-09-26 11:23:06 +0200145 dl->dl.t203_sec = 0; dl->dl.t203_usec = 0;
Harald Welte20de6202019-06-02 21:33:38 +0200146 dl->dl.t200_sec = t200_ms / 1000; dl->dl.t200_usec = (t200_ms % 1000) * 1000;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200147}
148
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200149/*! initialize a LAPDm entity and all datalinks inside
Harald Welte6bdf0b12011-08-17 18:22:08 +0200150 * \param[in] le LAPDm entity
151 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200152 * \param[in] t200 T200 re-transmission timer for all SAPIs in seconds
153 *
154 * Don't use this function; It doesn't support different T200 values per API
155 * and doesn't permit the caller to specify the N200 counter, both of which
156 * are required by GSM specs and supported by lapdm_entity_init2().
Harald Welte6bdf0b12011-08-17 18:22:08 +0200157 */
Andreas.Eversberg5ac44782011-11-06 20:35:48 +0100158void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200159{
Harald Welte20de6202019-06-02 21:33:38 +0200160 /* convert from single full-second value to per-SAPI milli-second value */
161 int t200_ms_sapi_arr[_NR_DL_SAPI];
162 int i;
163
164 for (i = 0; i < ARRAY_SIZE(t200_ms_sapi_arr); i++)
165 t200_ms_sapi_arr[i] = t200 * 1000;
166
Harald Welte00b2faf2020-05-02 19:56:36 +0200167 return lapdm_entity_init3(le, mode, t200_ms_sapi_arr, N200, NULL);
Harald Welte20de6202019-06-02 21:33:38 +0200168}
169
170/*! initialize a LAPDm entity and all datalinks inside
171 * \param[in] le LAPDm entity
172 * \param[in] mode lapdm_mode (BTS/MS)
173 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
174 * \param[in] n200 N200 re-transmisison count
175 */
176void lapdm_entity_init2(struct lapdm_entity *le, enum lapdm_mode mode,
177 const int *t200_ms, int n200)
178{
Harald Welte00b2faf2020-05-02 19:56:36 +0200179 lapdm_entity_init3(le, mode, t200_ms, n200, NULL);
180}
181
182/*! initialize a LAPDm entity and all datalinks inside
183 * \param[in] le LAPDm entity
184 * \param[in] mode lapdm_mode (BTS/MS)
185 * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds
186 * \param[in] n200 N200 re-transmisison count
187 * \param[in] name human-readable name (will be copied internally + extended with SAPI)
188 */
189void lapdm_entity_init3(struct lapdm_entity *le, enum lapdm_mode mode,
190 const int *t200_ms, int n200, const char *name_pfx)
191{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200192 unsigned int i;
193
Harald Welte00b2faf2020-05-02 19:56:36 +0200194 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
195 char name[256];
196 if (name_pfx) {
197 snprintf(name, sizeof(name), "%s[%s]", name_pfx, i == 0 ? "0" : "3");
198 lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200, name);
199 } else
200 lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200, NULL);
201 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200202
203 lapdm_entity_set_mode(le, mode);
204}
205
Harald Welte20de6202019-06-02 21:33:38 +0200206static int get_n200_dcch(enum gsm_chan_t chan_t)
207{
208 switch (chan_t) {
209 case GSM_LCHAN_SDCCH:
210 return N200_TR_SDCCH;
211 case GSM_LCHAN_TCH_F:
212 return N200_TR_FACCH_FR;
213 case GSM_LCHAN_TCH_H:
214 return N200_TR_FACCH_HR;
215 default:
216 return -1;
217 }
218}
219
220/*! initialize a LAPDm channel and all its channels
221 * \param[in] lc lapdm_channel to be initialized
222 * \param[in] mode lapdm_mode (BTS/MS)
223 *
224 * Don't use this function; It doesn't support different T200 values per API
225 * and doesn't set the correct N200 counter, both of which
226 * are required by GSM specs and supported by lapdm_channel_init2().
227 */
228void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode)
229{
230 /* emulate old backwards-compatible behavior with 1s/2s */
231 const int t200_ms_dcch[_NR_DL_SAPI] = { 1000, 1000 };
232 const int t200_ms_acch[_NR_DL_SAPI] = { 2000, 2000 };
233
Harald Welte00b2faf2020-05-02 19:56:36 +0200234 lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, GSM_LCHAN_SDCCH, NULL);
Harald Welte20de6202019-06-02 21:33:38 +0200235}
236
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200237/*! initialize a LAPDm channel and all its channels
Harald Welte6bdf0b12011-08-17 18:22:08 +0200238 * \param[in] lc \ref lapdm_channel to be initialized
239 * \param[in] mode \ref lapdm_mode (BTS/MS)
Harald Welte20de6202019-06-02 21:33:38 +0200240 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
241 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
242 * \param[in] chan_t GSM channel type (to correctly set N200)
Harald Welte6bdf0b12011-08-17 18:22:08 +0200243 */
Harald Welte20de6202019-06-02 21:33:38 +0200244int lapdm_channel_init2(struct lapdm_channel *lc, enum lapdm_mode mode,
245 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200246{
Harald Welte00b2faf2020-05-02 19:56:36 +0200247 return lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, chan_t, NULL);
248}
249
250/*! initialize a LAPDm channel and all its channels
251 * \param[in] lc \ref lapdm_channel to be initialized
252 * \param[in] mode \ref lapdm_mode (BTS/MS)
253 * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH
254 * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH
255 * \param[in] chan_t GSM channel type (to correctly set N200)
256 * \parma[in] name_pfx human-readable name (copied by function + extended with ACCH/DCCH)
257 */
258int lapdm_channel_init3(struct lapdm_channel *lc, enum lapdm_mode mode,
259 const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t,
260 const char *name_pfx)
261{
Harald Welte20de6202019-06-02 21:33:38 +0200262 int n200_dcch = get_n200_dcch(chan_t);
Harald Welte00b2faf2020-05-02 19:56:36 +0200263 char namebuf[256];
264 char *name = NULL;
265
Harald Welte20de6202019-06-02 21:33:38 +0200266 if (n200_dcch < 0)
267 return -EINVAL;
268
Harald Welte00b2faf2020-05-02 19:56:36 +0200269 osmo_talloc_replace_string(tall_lapd_ctx, &lc->name, name_pfx);
270
271 if (name_pfx) {
272 snprintf(namebuf, sizeof(namebuf), "%s[ACCH]", name_pfx);
273 name = namebuf;
274 }
275 lapdm_entity_init3(&lc->lapdm_acch, mode, t200_ms_acch, N200_TR_SACCH, name);
Harald Welte3e8c5202018-05-04 20:58:48 +0200276 lc->lapdm_acch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200277
Harald Welte00b2faf2020-05-02 19:56:36 +0200278 if (name_pfx) {
279 snprintf(namebuf, sizeof(namebuf), "%s[DCCH]", name_pfx);
280 name = namebuf;
281 }
282 lapdm_entity_init3(&lc->lapdm_dcch, mode, t200_ms_dcch, n200_dcch, name);
Harald Welte3e8c5202018-05-04 20:58:48 +0200283 lc->lapdm_dcch.lapdm_ch = lc;
Harald Welte20de6202019-06-02 21:33:38 +0200284
285 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200286}
287
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200288/*! flush and release all resoures in LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200289void lapdm_entity_exit(struct lapdm_entity *le)
290{
291 unsigned int i;
292 struct lapdm_datalink *dl;
293
294 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
295 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200296 lapd_dl_exit(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200297 }
298}
299
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200300/* lfush and release all resources in LAPDm channel
Harald Welte6bdf0b12011-08-17 18:22:08 +0200301 *
302 * A convenience wrapper calling \ref lapdm_entity_exit on both
303 * entities inside the \ref lapdm_channel
304 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200305void lapdm_channel_exit(struct lapdm_channel *lc)
306{
307 lapdm_entity_exit(&lc->lapdm_acch);
308 lapdm_entity_exit(&lc->lapdm_dcch);
309}
310
Daniel Willmann55405fb2014-03-26 13:45:17 +0100311struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200312{
313 switch (sapi) {
314 case LAPDm_SAPI_NORMAL:
315 return &le->datalink[0];
316 case LAPDm_SAPI_SMS:
317 return &le->datalink[1];
318 default:
319 return NULL;
320 }
321}
322
Harald Welte1f0b8c22011-06-27 10:51:37 +0200323/* Append padding (if required) */
324static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201)
325{
326 int pad_len = n201 - msgb_l2len(msg);
327 uint8_t *data;
328
329 if (pad_len < 0) {
rootaf48bed2011-09-26 11:23:06 +0200330 LOGP(DLLAPD, LOGL_ERROR,
Harald Welte1f0b8c22011-06-27 10:51:37 +0200331 "cannot pad message that is already too big!\n");
332 return;
333 }
334
Vadim Yanitskiy29ecabe2020-08-27 02:12:23 +0700335 data = msgb_put(msg, pad_len); /* TODO: random padding */
336 memset(data, GSM_MACBLOCK_PADDING, pad_len);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200337}
338
339/* input function that L2 calls when sending messages up to L3 */
340static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le)
341{
342 if (!le->l3_cb) {
343 msgb_free(msg);
344 return -EIO;
345 }
346
347 /* call the layer2 message handler that is registered */
348 return le->l3_cb(msg, le, le->l3_ctx);
349}
350
351/* write a frame into the tx queue */
352static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg,
rootaf48bed2011-09-26 11:23:06 +0200353 uint8_t chan_nr, uint8_t link_id, uint8_t pad)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200354{
355 struct lapdm_entity *le = dl->entity;
356 struct osmo_phsap_prim pp;
357
358 /* if there is a pending message, queue it */
359 if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) {
rootaf48bed2011-09-26 11:23:06 +0200360 *msgb_push(msg, 1) = pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200361 *msgb_push(msg, 1) = link_id;
362 *msgb_push(msg, 1) = chan_nr;
rootaf48bed2011-09-26 11:23:06 +0200363 msgb_enqueue(&dl->dl.tx_queue, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200364 return -EBUSY;
365 }
366
367 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
368 PRIM_OP_REQUEST, msg);
369 pp.u.data.chan_nr = chan_nr;
370 pp.u.data.link_id = link_id;
371
372 /* send the frame now */
373 le->tx_pending = 0; /* disabled flow control */
rootaf48bed2011-09-26 11:23:06 +0200374 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200375
376 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
377}
378
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700379/* Dequeue a Downlink message for DCCH (dedicated channel) */
380static struct msgb *tx_dequeue_dcch_msgb(struct lapdm_entity *le)
381{
382 struct msgb *msg;
383
384 /* SAPI=0 always has higher priority than SAPI=3 */
385 msg = msgb_dequeue(&le->datalink[DL_SAPI0].dl.tx_queue);
386 if (msg == NULL) /* no SAPI=0 messages, dequeue SAPI=3 (if any) */
387 msg = msgb_dequeue(&le->datalink[DL_SAPI3].dl.tx_queue);
388
389 return msg;
390}
391
392/* Dequeue a Downlink message for ACCH (associated channel) */
393static struct msgb *tx_dequeue_acch_msgb(struct lapdm_entity *le)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200394{
395 struct lapdm_datalink *dl;
396 int last = le->last_tx_dequeue;
397 int i = last, n = ARRAY_SIZE(le->datalink);
398 struct msgb *msg = NULL;
399
400 /* round-robin dequeue */
401 do {
402 /* next */
403 i = (i + 1) % n;
404 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +0200405 if ((msg = msgb_dequeue(&dl->dl.tx_queue)))
Harald Welte1f0b8c22011-06-27 10:51:37 +0200406 break;
407 } while (i != last);
408
409 if (msg) {
410 /* Set last dequeue position */
411 le->last_tx_dequeue = i;
412 }
413
414 return msg;
415}
416
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200417/*! dequeue a msg that's pending transmission via L1 and wrap it into
Harald Welte1f0b8c22011-06-27 10:51:37 +0200418 * a osmo_phsap_prim */
419int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp)
420{
421 struct msgb *msg;
rootaf48bed2011-09-26 11:23:06 +0200422 uint8_t pad;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200423
Vadim Yanitskiy776c5b12020-08-27 20:58:49 +0700424 /* Dequeue depending on channel type: DCCH or ACCH.
425 * See 3GPP TS 44.005, section 4.2.2 "Priority". */
426 if (le == &le->lapdm_ch->lapdm_dcch)
427 msg = tx_dequeue_dcch_msgb(le);
428 else
429 msg = tx_dequeue_acch_msgb(le);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200430 if (!msg)
431 return -ENODEV;
432
433 /* if we have a message, send PH-DATA.req */
434 osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA,
435 PRIM_OP_REQUEST, msg);
436
437 /* Pull chan_nr and link_id */
438 pp->u.data.chan_nr = *msg->data;
439 msgb_pull(msg, 1);
440 pp->u.data.link_id = *msg->data;
441 msgb_pull(msg, 1);
rootaf48bed2011-09-26 11:23:06 +0200442 pad = *msg->data;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200443 msgb_pull(msg, 1);
444
445 /* Pad the frame, we can transmit now */
rootaf48bed2011-09-26 11:23:06 +0200446 lapdm_pad_msgb(msg, pad);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200447
448 return 0;
449}
450
451/* get next frame from the tx queue. because the ms has multiple datalinks,
452 * each datalink's queue is read round-robin.
453 */
454static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le)
455{
456 struct osmo_phsap_prim pp;
457
458 /* we may send again */
459 le->tx_pending = 0;
460
461 /* free confirm message */
462 if (msg)
463 msgb_free(msg);
464
465 if (lapdm_phsap_dequeue_prim(le, &pp) < 0) {
466 /* no message in all queues */
467
468 /* If user didn't request PH-EMPTY_FRAME.req, abort */
469 if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME))
470 return 0;
471
472 /* otherwise, send PH-EMPTY_FRAME.req */
473 osmo_prim_init(&pp.oph, SAP_GSM_PH,
474 PRIM_PH_EMPTY_FRAME,
475 PRIM_OP_REQUEST, NULL);
476 } else {
477 le->tx_pending = 1;
478 }
479
480 return le->l1_prim_cb(&pp.oph, le->l1_ctx);
481}
482
Harald Welte542301b2018-04-19 16:11:14 +0200483/* Is a given msg_type "transparent" as per TS 48.058 Section 8.1 */
484static int rsl_is_transparent(uint8_t msg_type)
485{
486 switch (msg_type) {
487 case RSL_MT_DATA_IND:
488 case RSL_MT_UNIT_DATA_IND:
489 return 1;
490 case RSL_MT_DATA_REQ:
491 case RSL_MT_UNIT_DATA_REQ:
492 return 1;
493 default:
494 return 0;
495 }
496}
497
Harald Welte1f0b8c22011-06-27 10:51:37 +0200498/* Create RSLms various RSLms messages */
499static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx,
500 struct msgb *msg)
501{
Harald Welte542301b2018-04-19 16:11:14 +0200502 int transparent = rsl_is_transparent(msg_type);
503
Harald Welte1f0b8c22011-06-27 10:51:37 +0200504 /* Add the RSL + RLL header */
Harald Welte542301b2018-04-19 16:11:14 +0200505 rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200506
507 /* send off the RSLms message to L3 */
508 return rslms_sendmsg(msg, mctx->dl->entity);
509}
510
511/* Take a B4 format message from L1 and create RSLms UNIT DATA IND */
512static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg)
513{
514 uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200515
516 /* Add the RSL + RLL header */
517 msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len);
Harald Welted977f5f2018-05-08 21:53:28 +0200518
Harald Weltef1bdf782018-05-08 22:03:20 +0200519 /* Add two non-standard IEs carrying MS power and TA values for B4 (SACCH) */
520 if (mctx->lapdm_fmt == LAPDm_FMT_B4) {
521 msgb_tv_push(msg, RSL_IE_MS_POWER, mctx->tx_power_ind);
522 msgb_tv_push(msg, RSL_IE_TIMING_ADVANCE, mctx->ta_ind);
523 }
Harald Welted977f5f2018-05-08 21:53:28 +0200524
Harald Welte1f0b8c22011-06-27 10:51:37 +0200525 rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr,
526 mctx->link_id, 1);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100527
Harald Welte1f0b8c22011-06-27 10:51:37 +0200528 return rslms_sendmsg(msg, mctx->dl->entity);
529}
530
531static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx)
532{
533 struct msgb *msg;
Harald Welte542301b2018-04-19 16:11:14 +0200534 int transparent = rsl_is_transparent(msg_type);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200535
Harald Welte542301b2018-04-19 16:11:14 +0200536 msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200537
538 /* send off the RSLms message to L3 */
539 return rslms_sendmsg(msg, mctx->dl->entity);
540}
541
542static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx)
543{
544 struct msgb *msg;
545
Harald Welte00b2faf2020-05-02 19:56:36 +0200546 LOGDL(&mctx->dl->dl, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause);
Harald Welte542301b2018-04-19 16:11:14 +0200547 msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200548 msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause);
549 return rslms_sendmsg(msg, mctx->dl->entity);
550}
551
rootaf48bed2011-09-26 11:23:06 +0200552/* DLSAP L2 -> L3 (RSLms) */
553static int send_rslms_dlsap(struct osmo_dlsap_prim *dp,
554 struct lapd_msg_ctx *lctx)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200555{
rootaf48bed2011-09-26 11:23:06 +0200556 struct lapd_datalink *dl = lctx->dl;
557 struct lapdm_datalink *mdl =
558 container_of(dl, struct lapdm_datalink, dl);
559 struct lapdm_msg_ctx *mctx = &mdl->mctx;
560 uint8_t rll_msg = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200561
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200562 switch (OSMO_PRIM_HDR(&dp->oph)) {
563 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION):
564 rll_msg = RSL_MT_EST_IND;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200565 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200566 case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM):
567 rll_msg = RSL_MT_EST_CONF;
rootaf48bed2011-09-26 11:23:06 +0200568 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200569 case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION):
570 rll_msg = RSL_MT_DATA_IND;
rootaf48bed2011-09-26 11:23:06 +0200571 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200572 case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION):
573 return send_rslms_rll_l3_ui(mctx, dp->oph.msg);
574 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION):
575 rll_msg = RSL_MT_REL_IND;
rootaf48bed2011-09-26 11:23:06 +0200576 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200577 case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM):
578 rll_msg = RSL_MT_REL_CONF;
rootaf48bed2011-09-26 11:23:06 +0200579 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200580 case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM):
581 rll_msg = RSL_MT_SUSP_CONF;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200582 break;
Andreas Eversberg78122ab2011-09-27 12:06:55 +0200583 case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION):
584 rsl_rll_error(dp->u.error_ind.cause, mctx);
585 if (dp->oph.msg)
586 msgb_free(dp->oph.msg);
587 return 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200588 }
rootaf48bed2011-09-26 11:23:06 +0200589
590 if (!rll_msg) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200591 LOGDL(dl, LOGL_ERROR, "Unsupported op %d, prim %d. Please "
rootaf48bed2011-09-26 11:23:06 +0200592 "fix!\n", dp->oph.primitive, dp->oph.operation);
593 return -EINVAL;
594 }
595
596 if (!dp->oph.msg)
597 return send_rll_simple(rll_msg, mctx);
598
599 return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200600}
601
rootaf48bed2011-09-26 11:23:06 +0200602/* send a data frame to layer 1 */
603static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200604{
rootaf48bed2011-09-26 11:23:06 +0200605 uint8_t l3_len = msg->tail - msg->data;
606 struct lapd_datalink *dl = lctx->dl;
607 struct lapdm_datalink *mdl =
608 container_of(dl, struct lapdm_datalink, dl);
609 struct lapdm_msg_ctx *mctx = &mdl->mctx;
610 int format = lctx->format;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200611
rootaf48bed2011-09-26 11:23:06 +0200612 /* prepend l2 header */
613 msg->l2h = msgb_push(msg, 3);
614 msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr);
615 /* EA is set here too */
616 switch (format) {
617 case LAPD_FORM_I:
618 msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send,
619 lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200620 break;
rootaf48bed2011-09-26 11:23:06 +0200621 case LAPD_FORM_S:
622 msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200623 break;
rootaf48bed2011-09-26 11:23:06 +0200624 case LAPD_FORM_U:
625 msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200626 break;
627 default:
Harald Welte1f0b8c22011-06-27 10:51:37 +0200628 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200629 return -EINVAL;
630 }
rootaf48bed2011-09-26 11:23:06 +0200631 msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */
632 if (lctx->more)
633 msg->l2h[2] |= LAPDm_MORE;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200634
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +0100635 /* add ACCH header with last indicated tx-power and TA */
636 if ((mctx->link_id & 0x40)) {
637 struct lapdm_entity *le = mdl->entity;
638
639 msg->l2h = msgb_push(msg, 2);
640 msg->l2h[0] = le->tx_power;
641 msg->l2h[1] = le->ta;
642 }
643
rootaf48bed2011-09-26 11:23:06 +0200644 return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id,
645 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200646}
647
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100648static int update_pending_frames(struct lapd_msg_ctx *lctx)
649{
650 struct lapd_datalink *dl = lctx->dl;
651 struct msgb *msg;
652 int rc = -1;
653
654 llist_for_each_entry(msg, &dl->tx_queue, list) {
655 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
656 msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]),
657 LAPDm_CTRL_PF_BIT(msg->l2h[1]));
658 rc = 0;
659 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200660 LOGDL(dl, LOGL_ERROR, "Supervisory frame in queue, this shouldn't happen\n");
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100661 }
662 }
663
664 return rc;
665}
666
Harald Welte1284c3e2018-05-01 18:11:02 +0200667/* determine if receiving a given LAPDm message is not permitted */
668static int lapdm_rx_not_permitted(const struct lapdm_entity *le,
669 const struct lapd_msg_ctx *lctx)
670{
671 /* we currently only implement SABM related checks here */
672 if (lctx->format != LAPD_FORM_U || lctx->s_u != LAPD_U_SABM)
673 return 0;
674
675 if (le->mode == LAPDM_MODE_BTS) {
676 if (le == &le->lapdm_ch->lapdm_acch) {
677 /* no contention resolution on SACCH */
678 if (lctx->length > 0)
679 return RLL_CAUSE_SABM_INFO_NOTALL;
680 } else {
681 switch (lctx->sapi) {
Harald Welte1284c3e2018-05-01 18:11:02 +0200682 case 3:
683 /* SAPI3 doesn't support contention resolution */
684 if (lctx->length > 0)
685 return RLL_CAUSE_SABM_INFO_NOTALL;
686 break;
Harald Welteb82a4072018-05-09 16:31:16 +0200687 default:
688 break;
Harald Welte1284c3e2018-05-01 18:11:02 +0200689 }
690 }
691 } else if (le->mode == LAPDM_MODE_MS) {
692 /* contention resolution (L3 present) is only sent by MS, but
693 * never received by it */
694 if (lctx->length > 0)
695 return RLL_CAUSE_SABM_INFO_NOTALL;
696 }
697 return 0;
698}
699
Harald Welte1f0b8c22011-06-27 10:51:37 +0200700/* input into layer2 (from layer 1) */
rootaf48bed2011-09-26 11:23:06 +0200701static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le,
702 uint8_t chan_nr, uint8_t link_id)
Harald Welte1f0b8c22011-06-27 10:51:37 +0200703{
704 uint8_t cbits = chan_nr >> 3;
Harald Welte64207742011-06-27 23:32:14 +0200705 uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200706 struct lapdm_msg_ctx mctx;
rootaf48bed2011-09-26 11:23:06 +0200707 struct lapd_msg_ctx lctx;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200708 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200709 int n201;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200710
711 /* when we reach here, we have a msgb with l2h pointing to the raw
712 * 23byte mac block. The l1h has already been purged. */
713
rootaf48bed2011-09-26 11:23:06 +0200714 memset(&mctx, 0, sizeof(mctx));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200715 mctx.chan_nr = chan_nr;
716 mctx.link_id = link_id;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200717
Harald Welte1f0b8c22011-06-27 10:51:37 +0200718 /* check for L1 chan_nr/link_id and determine LAPDm hdr format */
719 if (cbits == 0x10 || cbits == 0x12) {
720 /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */
721 mctx.lapdm_fmt = LAPDm_FMT_Bbis;
rootaf48bed2011-09-26 11:23:06 +0200722 n201 = N201_Bbis;
Harald Welte64207742011-06-27 23:32:14 +0200723 sapi = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200724 } else {
725 if (mctx.link_id & 0x40) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200726 /* It was received from network on SACCH */
727
Andreas.Eversberg816e1782011-11-06 20:46:30 +0100728 /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */
729 if (le->mode == LAPDM_MODE_MS
730 && LAPDm_CTRL_is_U(msg->l2h[3])
731 && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) {
Harald Welte7ca604b2011-06-29 12:13:51 +0200732 mctx.lapdm_fmt = LAPDm_FMT_B4;
rootaf48bed2011-09-26 11:23:06 +0200733 n201 = N201_B4;
Harald Welte7ca604b2011-06-29 12:13:51 +0200734 } else {
735 mctx.lapdm_fmt = LAPDm_FMT_B;
rootaf48bed2011-09-26 11:23:06 +0200736 n201 = N201_AB_SACCH;
Harald Welte7ca604b2011-06-29 12:13:51 +0200737 }
Harald Welte1f0b8c22011-06-27 10:51:37 +0200738 /* SACCH frames have a two-byte L1 header that
739 * OsmocomBB L1 doesn't strip */
740 mctx.tx_power_ind = msg->l2h[0] & 0x1f;
741 mctx.ta_ind = msg->l2h[1];
742 msgb_pull(msg, 2);
743 msg->l2h += 2;
Harald Welte64207742011-06-27 23:32:14 +0200744 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200745 } else {
746 mctx.lapdm_fmt = LAPDm_FMT_B;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100747 n201 = N201_AB_SDCCH;
Harald Welte64207742011-06-27 23:32:14 +0200748 sapi = (msg->l2h[0] >> 2) & 7;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200749 }
750 }
751
Daniel Willmann55405fb2014-03-26 13:45:17 +0100752 mctx.dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte64207742011-06-27 23:32:14 +0200753 /* G.2.1 No action on frames containing an unallocated SAPI. */
754 if (!mctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200755 LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported SAPI %d!\n", sapi);
Harald Welte64207742011-06-27 23:32:14 +0200756 msgb_free(msg);
757 return -EIO;
758 }
759
Harald Welte1f0b8c22011-06-27 10:51:37 +0200760 switch (mctx.lapdm_fmt) {
761 case LAPDm_FMT_A:
762 case LAPDm_FMT_B:
763 case LAPDm_FMT_B4:
rootaf48bed2011-09-26 11:23:06 +0200764 lctx.dl = &mctx.dl->dl;
765 /* obtain SAPI from address field */
766 mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]);
767 /* G.2.3 EA bit set to "0" is not allowed in GSM */
768 if (!LAPDm_ADDR_EA(msg->l2h[0])) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200769 LOGDL(lctx.dl, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +0200770 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +0200771 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200772 return -EINVAL;
773 }
rootaf48bed2011-09-26 11:23:06 +0200774 /* adress field */
775 lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]);
776 lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]);
777 lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]);
778 /* command field */
779 if (LAPDm_CTRL_is_I(msg->l2h[1])) {
780 lctx.format = LAPD_FORM_I;
781 lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]);
782 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
783 } else if (LAPDm_CTRL_is_S(msg->l2h[1])) {
784 lctx.format = LAPD_FORM_S;
785 lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]);
786 lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]);
787 } else if (LAPDm_CTRL_is_U(msg->l2h[1])) {
788 lctx.format = LAPD_FORM_U;
789 lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]);
790 } else
791 lctx.format = LAPD_FORM_UKN;
792 lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]);
793 if (lctx.sapi != LAPDm_SAPI_NORMAL
794 && lctx.sapi != LAPDm_SAPI_SMS
795 && lctx.format == LAPD_FORM_U
796 && lctx.s_u == LAPDm_U_UI) {
797 /* 5.3.3 UI frames with invalid SAPI values shall be
798 * discarded
799 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200800 LOGDL(lctx.dl, LOGL_INFO, "sapi=%u (discarding)\n", lctx.sapi);
rootaf48bed2011-09-26 11:23:06 +0200801 msgb_free(msg);
802 return 0;
803 }
804 if (mctx.lapdm_fmt == LAPDm_FMT_B4) {
805 lctx.n201 = n201;
806 lctx.length = n201;
807 lctx.more = 0;
808 msg->l3h = msg->l2h + 2;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100809 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200810 } else {
811 /* length field */
812 if (!(msg->l2h[2] & LAPDm_EL)) {
813 /* G.4.1 If the EL bit is set to "0", an
814 * MDL-ERROR-INDICATION primitive with cause
815 * "frame not implemented" is sent to the
816 * mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +0200817 LOGDL(lctx.dl, LOGL_NOTICE, "we don't support multi-octet length\n");
rootaf48bed2011-09-26 11:23:06 +0200818 msgb_free(msg);
819 rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx);
820 return -EINVAL;
821 }
822 lctx.n201 = n201;
823 lctx.length = msg->l2h[2] >> 2;
824 lctx.more = !!(msg->l2h[2] & LAPDm_MORE);
825 msg->l3h = msg->l2h + 3;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100826 msgb_pull_to_l3(msg);
rootaf48bed2011-09-26 11:23:06 +0200827 }
828 /* store context for messages from lapd */
829 memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx));
Harald Welte1284c3e2018-05-01 18:11:02 +0200830 rc =lapdm_rx_not_permitted(le, &lctx);
831 if (rc > 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200832 LOGDL(lctx.dl, LOGL_NOTICE, "received message not permitted\n");
Harald Welte1284c3e2018-05-01 18:11:02 +0200833 msgb_free(msg);
834 rsl_rll_error(rc, &mctx);
835 return -EINVAL;
836 }
rootaf48bed2011-09-26 11:23:06 +0200837 /* send to LAPD */
838 rc = lapd_ph_data_ind(msg, &lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200839 break;
840 case LAPDm_FMT_Bter:
841 /* FIXME */
842 msgb_free(msg);
843 break;
844 case LAPDm_FMT_Bbis:
845 /* directly pass up to layer3 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200846 msg->l3h = msg->l2h;
Jacob Erlbeck8dac4152014-01-28 11:03:11 +0100847 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200848 rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg);
849 break;
850 default:
851 msgb_free(msg);
852 }
853
854 return rc;
855}
856
857/* input into layer2 (from layer 1) */
858static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay)
859{
860 struct abis_rsl_cchan_hdr *ch;
861 struct gsm48_req_ref req_ref;
862 struct gsm_time gt;
863 struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD");
864
Jacob Erlbeckd154f8b2015-04-09 14:22:21 +0200865 if (!msg)
866 return -ENOMEM;
867
Harald Welte1f0b8c22011-06-27 10:51:37 +0200868 msg->l2h = msgb_push(msg, sizeof(*ch));
869 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
870 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD);
871 ch->chan_nr = RSL_CHAN_RACH;
872
873 /* generate a RSL CHANNEL REQUIRED message */
874 gsm_fn2gsmtime(&gt, fn);
875 req_ref.ra = ra;
876 req_ref.t1 = gt.t1; /* FIXME: modulo? */
877 req_ref.t2 = gt.t2;
878 req_ref.t3_low = gt.t3 & 7;
879 req_ref.t3_high = gt.t3 >> 3;
880
881 msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref);
882 msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay);
883
884 return rslms_sendmsg(msg, le);
885}
886
887static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr);
888
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200889/*! Receive a PH-SAP primitive from L1 */
Harald Welte1f0b8c22011-06-27 10:51:37 +0200890int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le)
891{
892 struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph;
893 int rc = 0;
894
895 if (oph->sap != SAP_GSM_PH) {
rootaf48bed2011-09-26 11:23:06 +0200896 LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200897 oph->sap);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100898 rc = -ENODEV;
899 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200900 }
901
902 switch (oph->primitive) {
903 case PRIM_PH_DATA:
904 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200905 LOGP(DLLAPD, LOGL_ERROR, "PH_DATA is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200906 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100907 rc = -ENODEV;
908 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200909 }
910 rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr,
911 pp->u.data.link_id);
912 break;
913 case PRIM_PH_RTS:
914 if (oph->operation != PRIM_OP_INDICATION) {
rootaf48bed2011-09-26 11:23:06 +0200915 LOGP(DLLAPD, LOGL_ERROR, "PH_RTS is not INDICATION %u\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200916 oph->operation);
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100917 rc = -ENODEV;
918 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200919 }
920 rc = l2_ph_data_conf(oph->msg, le);
921 break;
922 case PRIM_PH_RACH:
923 switch (oph->operation) {
924 case PRIM_OP_INDICATION:
925 rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn,
926 pp->u.rach_ind.acc_delay);
927 break;
928 case PRIM_OP_CONFIRM:
929 rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn);
930 break;
931 default:
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100932 rc = -EIO;
933 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200934 }
935 break;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100936 default:
937 LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n",
938 oph->primitive);
939 rc = -EINVAL;
940 goto free;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200941 }
942
943 return rc;
Andreas Eversbergb36ad2d2013-02-05 12:01:32 +0100944
945free:
946 msgb_free(oph->msg);
947 return rc;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200948}
949
950
951/* L3 -> L2 / RSLMS -> LAPDm */
952
rootaf48bed2011-09-26 11:23:06 +0200953/* Set LAPDm context for established connection */
954static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr,
955 uint8_t link_id, int n201, uint8_t sapi)
956{
957 memset(&dl->mctx, 0, sizeof(dl->mctx));
958 dl->mctx.dl = dl;
959 dl->mctx.chan_nr = chan_nr;
960 dl->mctx.link_id = link_id;
961 dl->dl.lctx.dl = &dl->dl;
962 dl->dl.lctx.n201 = n201;
963 dl->dl.lctx.sapi = sapi;
964
965 return 0;
966}
967
Harald Welte1f0b8c22011-06-27 10:51:37 +0200968/* L3 requests establishment of data link */
969static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl)
970{
Harald Welte1f0b8c22011-06-27 10:51:37 +0200971 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
972 uint8_t chan_nr = rllh->chan_nr;
973 uint8_t link_id = rllh->link_id;
974 uint8_t sapi = rllh->link_id & 7;
975 struct tlv_parsed tv;
976 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +0100977 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +0200978 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +0200979
rootaf48bed2011-09-26 11:23:06 +0200980 /* Set LAPDm context for established connection */
981 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200982
rootaf48bed2011-09-26 11:23:06 +0200983 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh));
Harald Welte1f0b8c22011-06-27 10:51:37 +0200984 if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
rootaf48bed2011-09-26 11:23:06 +0200985 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200986 /* contention resolution establishment procedure */
987 if (sapi != 0) {
988 /* According to clause 6, the contention resolution
989 * procedure is only permitted with SAPI value 0 */
Harald Welte00b2faf2020-05-02 19:56:36 +0200990 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 but contention"
Harald Welte1f0b8c22011-06-27 10:51:37 +0200991 "resolution (discarding)\n");
992 msgb_free(msg);
993 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
994 }
995 /* transmit a SABM command with the P bit set to "1". The SABM
996 * command shall contain the layer 3 message unit */
997 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +0200998 } else {
999 /* normal establishment procedure */
rootaf48bed2011-09-26 11:23:06 +02001000 msg->l3h = msg->l2h + sizeof(*rllh);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001001 length = 0;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001002 }
1003
1004 /* check if the layer3 message length exceeds N201 */
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001005 if (length > n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001006 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001007 "(discarding)\n", length, n201);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001008 msgb_free(msg);
1009 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1010 }
1011
rootaf48bed2011-09-26 11:23:06 +02001012 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001013 msgb_pull_to_l3(msg);
1014 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001015
rootaf48bed2011-09-26 11:23:06 +02001016 /* prepare prim */
1017 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001018
rootaf48bed2011-09-26 11:23:06 +02001019 /* send to L2 */
1020 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001021}
1022
1023/* L3 requests transfer of unnumbered information */
1024static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl)
1025{
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001026 struct lapdm_entity *le = dl->entity;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001027 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1028 uint8_t chan_nr = rllh->chan_nr;
1029 uint8_t link_id = rllh->link_id;
1030 uint8_t sapi = link_id & 7;
1031 struct tlv_parsed tv;
Max777be2e2017-03-01 18:16:44 +01001032 int length, ui_bts;
1033
1034 if (!le) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001035 LOGDL(&dl->dl, LOGL_ERROR, "lapdm_datalink without entity error\n");
Max777be2e2017-03-01 18:16:44 +01001036 msgb_free(msg);
1037 return -EMLINK;
1038 }
1039 ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40));
Harald Welte1f0b8c22011-06-27 10:51:37 +02001040
1041 /* check if the layer3 message length exceeds N201 */
1042
1043 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1044
1045 if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001046 le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001047 }
1048 if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) {
Andreas.Eversbergf1f80de2011-11-06 20:45:29 +01001049 le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001050 }
1051 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001052 LOGDL(&dl->dl, LOGL_ERROR, "unit data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001053 msgb_free(msg);
1054 return -EINVAL;
1055 }
rootaf48bed2011-09-26 11:23:06 +02001056 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001057 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1058 /* check if the layer3 message length exceeds N201 */
Andreas Eversberg5977db02013-06-12 09:34:51 +02001059 if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001060 LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) "
Andreas Eversberg5977db02013-06-12 09:34:51 +02001061 "(discarding)\n", length,
1062 ((link_id & 0x40) ? 18 : 20) + ui_bts);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001063 msgb_free(msg);
1064 return -EIO;
1065 }
1066
Harald Welte00b2faf2020-05-02 19:56:36 +02001067 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 +02001068
rootaf48bed2011-09-26 11:23:06 +02001069 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001070 msgb_pull_to_l3(msg);
1071 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001072
1073 /* Push L1 + LAPDm header on msgb */
Andreas Eversberg5977db02013-06-12 09:34:51 +02001074 msg->l2h = msgb_push(msg, 2 + !ui_bts);
1075 msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd);
1076 msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0);
Andreas.Eversberg816e1782011-11-06 20:46:30 +01001077 if (!ui_bts)
Andreas Eversberg5977db02013-06-12 09:34:51 +02001078 msg->l2h[2] = LAPDm_LEN(length);
1079 if (link_id & 0x40) {
1080 msg->l2h = msgb_push(msg, 2);
1081 msg->l2h[0] = le->tx_power;
1082 msg->l2h[1] = le->ta;
1083 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001084
1085 /* Tramsmit */
rootaf48bed2011-09-26 11:23:06 +02001086 return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001087}
1088
1089/* L3 requests transfer of acknowledged information */
1090static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl)
1091{
1092 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1093 struct tlv_parsed tv;
rootaf48bed2011-09-26 11:23:06 +02001094 int length;
1095 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001096
1097 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1098 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001099 LOGDL(&dl->dl, LOGL_ERROR, "data request without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001100 msgb_free(msg);
1101 return -EINVAL;
1102 }
rootaf48bed2011-09-26 11:23:06 +02001103 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
1104 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001105
rootaf48bed2011-09-26 11:23:06 +02001106 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001107 msgb_pull_to_l3(msg);
1108 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001109
rootaf48bed2011-09-26 11:23:06 +02001110 /* prepare prim */
1111 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001112
rootaf48bed2011-09-26 11:23:06 +02001113 /* send to L2 */
1114 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001115}
1116
1117/* L3 requests suspension of data link */
1118static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl)
1119{
1120 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1121 uint8_t sapi = rllh->link_id & 7;
rootaf48bed2011-09-26 11:23:06 +02001122 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001123
1124 if (sapi != 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001125 LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 while suspending\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001126 msgb_free(msg);
1127 return -EINVAL;
1128 }
1129
rootaf48bed2011-09-26 11:23:06 +02001130 /* prepare prim */
1131 osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001132
rootaf48bed2011-09-26 11:23:06 +02001133 /* send to L2 */
1134 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001135}
1136
1137/* L3 requests resume of data link */
1138static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl)
1139{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001140 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
rootaf48bed2011-09-26 11:23:06 +02001141 int msg_type = rllh->c.msg_type;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001142 uint8_t chan_nr = rllh->chan_nr;
1143 uint8_t link_id = rllh->link_id;
1144 uint8_t sapi = rllh->link_id & 7;
1145 struct tlv_parsed tv;
1146 uint8_t length;
Andreas.Eversbergcbed3272011-11-06 20:43:08 +01001147 uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH;
rootaf48bed2011-09-26 11:23:06 +02001148 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001149
rootaf48bed2011-09-26 11:23:06 +02001150 /* Set LAPDm context for established connection */
1151 set_lapdm_context(dl, chan_nr, link_id, n201, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001152
1153 rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
1154 if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001155 LOGDL(&dl->dl, LOGL_ERROR, "resume without message error\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001156 msgb_free(msg);
1157 return send_rll_simple(RSL_MT_REL_IND, &dl->mctx);
1158 }
rootaf48bed2011-09-26 11:23:06 +02001159 msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001160 length = TLVP_LEN(&tv, RSL_IE_L3_INFO);
1161
rootaf48bed2011-09-26 11:23:06 +02001162 /* Remove RLL header from msgb and set length to L3-info */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001163 msgb_pull_to_l3(msg);
1164 msgb_trim(msg, length);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001165
rootaf48bed2011-09-26 11:23:06 +02001166 /* prepare prim */
1167 osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES
1168 : PRIM_DL_RECON, PRIM_OP_REQUEST, msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001169
rootaf48bed2011-09-26 11:23:06 +02001170 /* send to L2 */
1171 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001172}
1173
1174/* L3 requests release of data link */
1175static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl)
1176{
Harald Welte1f0b8c22011-06-27 10:51:37 +02001177 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001178 uint8_t mode = 0;
rootaf48bed2011-09-26 11:23:06 +02001179 struct osmo_dlsap_prim dp;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001180
1181 /* get release mode */
1182 if (rllh->data[0] == RSL_IE_RELEASE_MODE)
1183 mode = rllh->data[1] & 1;
1184
Harald Welte1f0b8c22011-06-27 10:51:37 +02001185 /* Pull rllh */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001186 msgb_pull_to_l3(msg);
Harald Welte973c3c32012-04-26 21:50:54 +02001187
1188 /* 04.06 3.8.3: No information field is permitted with the DISC
1189 * command. */
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001190 msgb_trim(msg, 0);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001191
rootaf48bed2011-09-26 11:23:06 +02001192 /* prepare prim */
1193 osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg);
1194 dp.u.rel_req.mode = mode;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001195
rootaf48bed2011-09-26 11:23:06 +02001196 /* send to L2 */
1197 return lapd_recv_dlsap(&dp, &dl->dl.lctx);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001198}
1199
1200/* L3 requests channel in idle state */
1201static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg)
1202{
1203 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1204 void *l1ctx = lc->lapdm_dcch.l1_ctx;
1205 struct osmo_phsap_prim pp;
1206
1207 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH,
1208 PRIM_OP_REQUEST, NULL);
1209
1210 if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) {
rootaf48bed2011-09-26 11:23:06 +02001211 LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001212 return -EINVAL;
1213 }
1214 if (cch->data[0] != RSL_IE_REQ_REFERENCE) {
rootaf48bed2011-09-26 11:23:06 +02001215 LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001216 return -EINVAL;
1217 }
1218 pp.u.rach_req.ra = cch->data[1];
1219 pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3];
1220 pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7;
1221
1222 if (cch->data[4] != RSL_IE_ACCESS_DELAY) {
rootaf48bed2011-09-26 11:23:06 +02001223 LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001224 return -EINVAL;
1225 }
1226 /* TA = 0 - delay */
1227 pp.u.rach_req.ta = 0 - cch->data[5];
1228
1229 if (cch->data[6] != RSL_IE_MS_POWER) {
rootaf48bed2011-09-26 11:23:06 +02001230 LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001231 return -EINVAL;
1232 }
1233 pp.u.rach_req.tx_power = cch->data[7];
1234
1235 msgb_free(msg);
1236
1237 return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx);
1238}
1239
1240/* L1 confirms channel request */
1241static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr)
1242{
1243 struct abis_rsl_cchan_hdr *ch;
1244 struct gsm_time tm;
1245 struct gsm48_req_ref *ref;
1246
1247 gsm_fn2gsmtime(&tm, frame_nr);
1248
Jacob Erlbeck8dac4152014-01-28 11:03:11 +01001249 msgb_pull_to_l3(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001250 msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref));
1251 ch = (struct abis_rsl_cchan_hdr *)msg->l2h;
1252 rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF);
1253 ch->chan_nr = RSL_CHAN_RACH;
1254 ch->data[0] = RSL_IE_REQ_REFERENCE;
1255 ref = (struct gsm48_req_ref *) (ch->data + 1);
1256 ref->t1 = tm.t1;
1257 ref->t2 = tm.t2;
1258 ref->t3_low = tm.t3 & 0x7;
1259 ref->t3_high = tm.t3 >> 3;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001260
Harald Welte1f0b8c22011-06-27 10:51:37 +02001261 return rslms_sendmsg(msg, le);
1262}
1263
Harald Welte1f0b8c22011-06-27 10:51:37 +02001264/* incoming RSLms RLL message from L3 */
1265static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc)
1266{
1267 struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
1268 int msg_type = rllh->c.msg_type;
1269 uint8_t sapi = rllh->link_id & 7;
1270 struct lapdm_entity *le;
1271 struct lapdm_datalink *dl;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001272 int rc = 0;
1273
1274 if (msgb_l2len(msg) < sizeof(*rllh)) {
rootaf48bed2011-09-26 11:23:06 +02001275 LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n");
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001276 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001277 return -EINVAL;
1278 }
1279
1280 if (rllh->link_id & 0x40)
1281 le = &lc->lapdm_acch;
1282 else
1283 le = &lc->lapdm_dcch;
1284
Harald Welte1a87c1b2015-12-14 15:26:07 +01001285 /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not
1286 * BTS side */
1287 if (le->mode == LAPDM_MODE_BTS) {
1288 switch (msg_type) {
1289 case RSL_MT_SUSP_REQ:
1290 case RSL_MT_RES_REQ:
1291 case RSL_MT_RECON_REQ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001292 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' unsupported in BTS side LAPDm\n",
Harald Welte1a87c1b2015-12-14 15:26:07 +01001293 lc->name, rsl_msg_name(msg_type));
1294 msgb_free(msg);
1295 return -EINVAL;
1296 break;
1297 default:
1298 break;
1299 }
1300 }
1301
Holger Hans Peter Freytherc6206042014-01-23 15:00:55 +01001302 /* G.2.1 No action shall be taken on frames containing an unallocated
Harald Welte1f0b8c22011-06-27 10:51:37 +02001303 * SAPI.
1304 */
Daniel Willmann55405fb2014-03-26 13:45:17 +01001305 dl = lapdm_datalink_for_sapi(le, sapi);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001306 if (!dl) {
rootaf48bed2011-09-26 11:23:06 +02001307 LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi);
Andreas.Eversberga42b6992011-11-06 20:31:47 +01001308 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001309 return -EINVAL;
1310 }
1311
Daniel Willmanne5233922012-12-25 23:15:50 +01001312 switch (msg_type) {
Daniel Willmanne5233922012-12-25 23:15:50 +01001313 case RSL_MT_DATA_REQ:
1314 case RSL_MT_SUSP_REQ:
1315 case RSL_MT_REL_REQ:
1316 /* This is triggered in abnormal error conditions where
1317 * set_lapdm_context() was not called for the channel earlier. */
1318 if (!dl->dl.lctx.dl) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001319 LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' received without LAPDm context. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001320 lc->name, rsl_msg_name(msg_type), sapi);
1321 msgb_free(msg);
1322 return -EINVAL;
1323 }
1324 break;
1325 default:
Harald Welte00b2faf2020-05-02 19:56:36 +02001326 LOGP(DLLAPD, LOGL_INFO, "(%s) RLL Message '%s' received. (sapi %d)\n",
Daniel Willmanne5233922012-12-25 23:15:50 +01001327 lc->name, rsl_msg_name(msg_type), sapi);
1328 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001329
rootaf48bed2011-09-26 11:23:06 +02001330 switch (msg_type) {
1331 case RSL_MT_UNIT_DATA_REQ:
1332 rc = rslms_rx_rll_udata_req(msg, dl);
1333 break;
1334 case RSL_MT_EST_REQ:
1335 rc = rslms_rx_rll_est_req(msg, dl);
1336 break;
1337 case RSL_MT_DATA_REQ:
1338 rc = rslms_rx_rll_data_req(msg, dl);
1339 break;
1340 case RSL_MT_SUSP_REQ:
1341 rc = rslms_rx_rll_susp_req(msg, dl);
1342 break;
1343 case RSL_MT_RES_REQ:
1344 rc = rslms_rx_rll_res_req(msg, dl);
1345 break;
1346 case RSL_MT_RECON_REQ:
1347 rc = rslms_rx_rll_res_req(msg, dl);
1348 break;
1349 case RSL_MT_REL_REQ:
1350 rc = rslms_rx_rll_rel_req(msg, dl);
1351 break;
1352 default:
1353 LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001354 msgb_free(msg);
rootaf48bed2011-09-26 11:23:06 +02001355 rc = -EINVAL;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001356 }
Harald Welte1f0b8c22011-06-27 10:51:37 +02001357
1358 return rc;
1359}
1360
1361/* incoming RSLms COMMON CHANNEL message from L3 */
1362static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc)
1363{
1364 struct abis_rsl_cchan_hdr *cch = msgb_l2(msg);
1365 int msg_type = cch->c.msg_type;
1366 int rc = 0;
1367
1368 if (msgb_l2len(msg) < sizeof(*cch)) {
rootaf48bed2011-09-26 11:23:06 +02001369 LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n");
Harald Welte1f0b8c22011-06-27 10:51:37 +02001370 return -EINVAL;
1371 }
1372
1373 switch (msg_type) {
1374 case RSL_MT_CHAN_RQD:
1375 /* create and send RACH request */
1376 rc = rslms_rx_chan_rqd(lc, msg);
1377 break;
1378 default:
rootaf48bed2011-09-26 11:23:06 +02001379 LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n",
Harald Welte1f0b8c22011-06-27 10:51:37 +02001380 msg_type);
1381 msgb_free(msg);
1382 return 0;
1383 }
1384
1385 return rc;
1386}
1387
Harald Welte7023aa02019-05-19 12:17:06 +02001388/*! Receive a RSLms \ref msgb from Layer 3. 'msg' ownership is transferred,
1389 * i.e. caller must not free it */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001390int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc)
1391{
1392 struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
1393 int rc = 0;
1394
1395 if (msgb_l2len(msg) < sizeof(*rslh)) {
rootaf48bed2011-09-26 11:23:06 +02001396 LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n");
Harald Welte7023aa02019-05-19 12:17:06 +02001397 msgb_free(msg);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001398 return -EINVAL;
1399 }
1400
1401 switch (rslh->msg_discr & 0xfe) {
1402 case ABIS_RSL_MDISC_RLL:
1403 rc = rslms_rx_rll(msg, lc);
1404 break;
1405 case ABIS_RSL_MDISC_COM_CHAN:
1406 rc = rslms_rx_com_chan(msg, lc);
1407 break;
1408 default:
rootaf48bed2011-09-26 11:23:06 +02001409 LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message "
Harald Welte1f0b8c22011-06-27 10:51:37 +02001410 "discriminator 0x%02x", rslh->msg_discr);
1411 msgb_free(msg);
1412 return -EINVAL;
1413 }
1414
1415 return rc;
1416}
1417
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001418/*! Set the \ref lapdm_mode of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001419int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode)
1420{
rootaf48bed2011-09-26 11:23:06 +02001421 int i;
1422 enum lapd_mode lm;
1423
Harald Welte1f0b8c22011-06-27 10:51:37 +02001424 switch (mode) {
1425 case LAPDM_MODE_MS:
rootaf48bed2011-09-26 11:23:06 +02001426 lm = LAPD_MODE_USER;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001427 break;
1428 case LAPDM_MODE_BTS:
rootaf48bed2011-09-26 11:23:06 +02001429 lm = LAPD_MODE_NETWORK;
Harald Welte1f0b8c22011-06-27 10:51:37 +02001430 break;
1431 default:
1432 return -EINVAL;
1433 }
1434
rootaf48bed2011-09-26 11:23:06 +02001435 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1436 lapd_set_mode(&le->datalink[i].dl, lm);
1437 }
1438
Harald Welte1f0b8c22011-06-27 10:51:37 +02001439 le->mode = mode;
1440
1441 return 0;
1442}
1443
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001444/*! Set the \ref lapdm_mode of a LAPDm channel*/
Harald Welte1f0b8c22011-06-27 10:51:37 +02001445int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode)
1446{
1447 int rc;
1448
1449 rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode);
1450 if (rc < 0)
1451 return rc;
1452
1453 return lapdm_entity_set_mode(&lc->lapdm_acch, mode);
1454}
1455
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001456/*! Set the L1 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001457void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx)
1458{
1459 lc->lapdm_dcch.l1_prim_cb = cb;
1460 lc->lapdm_acch.l1_prim_cb = cb;
1461 lc->lapdm_dcch.l1_ctx = ctx;
1462 lc->lapdm_acch.l1_ctx = ctx;
1463}
1464
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001465/*! Set the L3 callback and context of a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001466void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx)
1467{
1468 lc->lapdm_dcch.l3_cb = cb;
1469 lc->lapdm_acch.l3_cb = cb;
1470 lc->lapdm_dcch.l3_ctx = ctx;
1471 lc->lapdm_acch.l3_ctx = ctx;
1472}
1473
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001474/*! Reset an entire LAPDm entity and all its datalinks */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001475void lapdm_entity_reset(struct lapdm_entity *le)
1476{
1477 struct lapdm_datalink *dl;
1478 int i;
1479
1480 for (i = 0; i < ARRAY_SIZE(le->datalink); i++) {
1481 dl = &le->datalink[i];
rootaf48bed2011-09-26 11:23:06 +02001482 lapd_dl_reset(&dl->dl);
Harald Welte1f0b8c22011-06-27 10:51:37 +02001483 }
1484}
1485
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001486/*! Reset a LAPDm channel with all its entities */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001487void lapdm_channel_reset(struct lapdm_channel *lc)
1488{
1489 lapdm_entity_reset(&lc->lapdm_dcch);
1490 lapdm_entity_reset(&lc->lapdm_acch);
1491}
1492
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001493/*! Set the flags of a LAPDm entity */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001494void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags)
1495{
1496 le->flags = flags;
1497}
1498
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001499/*! Set the flags of all LAPDm entities in a LAPDm channel */
Harald Welte1f0b8c22011-06-27 10:51:37 +02001500void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags)
1501{
1502 lapdm_entity_set_flags(&lc->lapdm_dcch, flags);
1503 lapdm_entity_set_flags(&lc->lapdm_acch, flags);
1504}
Harald Welte6bdf0b12011-08-17 18:22:08 +02001505
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001506/*! @} */