Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file lapdm.c |
| 2 | * GSM LAPDm (TS 04.06) implementation. */ |
| 3 | /* |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 4 | * (C) 2010-2019 by Harald Welte <laforge@gnumonks.org> |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 5 | * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu> |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | * (C) 2014-2016 by sysmocom - s.f.m.c GmbH |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
| 11 | * |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 12 | * 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 Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 22 | */ |
| 23 | |
Harald Welte | 6bdf0b1 | 2011-08-17 18:22:08 +0200 | [diff] [blame] | 24 | /*! \addtogroup lapdm |
| 25 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 26 | * \file lapdm.c */ |
Harald Welte | 6bdf0b1 | 2011-08-17 18:22:08 +0200 | [diff] [blame] | 27 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdint.h> |
| 30 | #include <string.h> |
| 31 | #include <errno.h> |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 32 | |
| 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 Welte | 1284c3e | 2018-05-01 18:11:02 +0200 | [diff] [blame] | 47 | #define LAPD_U_SABM 0x7 |
| 48 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 49 | /* 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 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 56 | #define LAPDm_ADDR_LPD(addr) (((addr) >> 5) & 0x3) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 57 | #define LAPDm_ADDR_SAPI(addr) (((addr) >> 2) & 0x7) |
| 58 | #define LAPDm_ADDR_CR(addr) (((addr) >> 1) & 0x1) |
| 59 | #define LAPDm_ADDR_EA(addr) ((addr) & 0x1) |
Andreas Eversberg | 0370018 | 2023-05-10 13:23:55 +0200 | [diff] [blame] | 60 | #define LAPDm_ADDR_SHORT_L2(addr) ((addr) & 0x3) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 61 | |
| 62 | /* TS 04.06 Table 3 / Section 3.4.3 */ |
| 63 | #define LAPDm_CTRL_I(nr, ns, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((ns) & 0x7) << 1)) |
| 64 | #define LAPDm_CTRL_S(nr, s, p) ((((nr) & 0x7) << 5) | (((p) & 0x1) << 4) | (((s) & 0x3) << 2) | 0x1) |
| 65 | #define LAPDm_CTRL_U(u, p) ((((u) & 0x1c) << (5-2)) | (((p) & 0x1) << 4) | (((u) & 0x3) << 2) | 0x3) |
| 66 | |
| 67 | #define LAPDm_CTRL_is_I(ctrl) (((ctrl) & 0x1) == 0) |
| 68 | #define LAPDm_CTRL_is_S(ctrl) (((ctrl) & 0x3) == 1) |
| 69 | #define LAPDm_CTRL_is_U(ctrl) (((ctrl) & 0x3) == 3) |
| 70 | |
| 71 | #define LAPDm_CTRL_U_BITS(ctrl) ((((ctrl) & 0xC) >> 2) | ((ctrl) & 0xE0) >> 3) |
| 72 | #define LAPDm_CTRL_PF_BIT(ctrl) (((ctrl) >> 4) & 0x1) |
| 73 | |
| 74 | #define LAPDm_CTRL_S_BITS(ctrl) (((ctrl) & 0xC) >> 2) |
| 75 | |
| 76 | #define LAPDm_CTRL_I_Ns(ctrl) (((ctrl) & 0xE) >> 1) |
| 77 | #define LAPDm_CTRL_Nr(ctrl) (((ctrl) & 0xE0) >> 5) |
| 78 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 79 | #define LAPDm_LEN(len) ((len << 2) | 0x1) |
| 80 | #define LAPDm_MORE 0x2 |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 81 | #define LAPDm_EL 0x1 |
| 82 | |
| 83 | #define LAPDm_U_UI 0x0 |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 84 | |
| 85 | /* TS 04.06 Section 5.8.3 */ |
| 86 | #define N201_AB_SACCH 18 |
| 87 | #define N201_AB_SDCCH 20 |
| 88 | #define N201_AB_FACCH 20 |
| 89 | #define N201_Bbis 23 |
| 90 | #define N201_Bter_SACCH 21 |
| 91 | #define N201_Bter_SDCCH 23 |
| 92 | #define N201_Bter_FACCH 23 |
| 93 | #define N201_B4 19 |
| 94 | |
| 95 | /* 5.8.2.1 N200 during establish and release */ |
| 96 | #define N200_EST_REL 5 |
| 97 | /* 5.8.2.1 N200 during timer recovery state */ |
| 98 | #define N200_TR_SACCH 5 |
| 99 | #define N200_TR_SDCCH 23 |
| 100 | #define N200_TR_FACCH_FR 34 |
| 101 | #define N200_TR_EFACCH_FR 48 |
| 102 | #define N200_TR_FACCH_HR 29 |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 103 | /* FIXME: set N200 depending on chan_nr */ |
| 104 | #define N200 N200_TR_SDCCH |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 105 | |
| 106 | enum lapdm_format { |
| 107 | LAPDm_FMT_A, |
| 108 | LAPDm_FMT_B, |
| 109 | LAPDm_FMT_Bbis, |
| 110 | LAPDm_FMT_Bter, |
| 111 | LAPDm_FMT_B4, |
| 112 | }; |
| 113 | |
Max | adef12a | 2016-05-25 15:25:02 +0200 | [diff] [blame] | 114 | const struct value_string osmo_ph_prim_names[] = { |
| 115 | { PRIM_PH_DATA, "PH-DATA" }, |
| 116 | { PRIM_PH_RACH, "PH-RANDOM_ACCESS" }, |
| 117 | { PRIM_PH_CONN, "PH-CONNECT" }, |
| 118 | { PRIM_PH_EMPTY_FRAME, "PH-EMPTY_FRAME" }, |
| 119 | { PRIM_PH_RTS, "PH-RTS" }, |
| 120 | { PRIM_MPH_INFO, "MPH-INFO" }, |
| 121 | { PRIM_TCH, "TCH" }, |
| 122 | { PRIM_TCH_RTS, "TCH-RTS" }, |
| 123 | { 0, NULL } |
| 124 | }; |
| 125 | |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 126 | extern void *tall_lapd_ctx; |
| 127 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 128 | static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg); |
| 129 | static int send_rslms_dlsap(struct osmo_dlsap_prim *dp, |
| 130 | struct lapd_msg_ctx *lctx); |
Daniel Willmann | 3dc4e16 | 2014-03-20 19:24:48 +0100 | [diff] [blame] | 131 | static int update_pending_frames(struct lapd_msg_ctx *lctx); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 132 | |
| 133 | static void lapdm_dl_init(struct lapdm_datalink *dl, |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 134 | struct lapdm_entity *entity, int t200_ms, uint32_t n200, |
| 135 | const char *name) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 136 | { |
| 137 | memset(dl, 0, sizeof(*dl)); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 138 | dl->entity = entity; |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 139 | lapd_dl_init2(&dl->dl, 1, 8, 251, name); /* Section 5.8.5 of TS 04.06 */ |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 140 | dl->dl.reestablish = 0; /* GSM uses no reestablish */ |
| 141 | dl->dl.send_ph_data_req = lapdm_send_ph_data_req; |
| 142 | dl->dl.send_dlsap = send_rslms_dlsap; |
Daniel Willmann | 3dc4e16 | 2014-03-20 19:24:48 +0100 | [diff] [blame] | 143 | dl->dl.update_pending_frames = update_pending_frames; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 144 | dl->dl.n200_est_rel = N200_EST_REL; |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 145 | dl->dl.n200 = n200; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 146 | dl->dl.t203_sec = 0; dl->dl.t203_usec = 0; |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 147 | dl->dl.t200_sec = t200_ms / 1000; dl->dl.t200_usec = (t200_ms % 1000) * 1000; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 150 | /*! initialize a LAPDm entity and all datalinks inside |
Harald Welte | 6bdf0b1 | 2011-08-17 18:22:08 +0200 | [diff] [blame] | 151 | * \param[in] le LAPDm entity |
| 152 | * \param[in] mode \ref lapdm_mode (BTS/MS) |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 153 | * \param[in] t200 T200 re-transmission timer for all SAPIs in seconds |
| 154 | * |
| 155 | * Don't use this function; It doesn't support different T200 values per API |
| 156 | * and doesn't permit the caller to specify the N200 counter, both of which |
| 157 | * are required by GSM specs and supported by lapdm_entity_init2(). |
Harald Welte | 6bdf0b1 | 2011-08-17 18:22:08 +0200 | [diff] [blame] | 158 | */ |
Andreas.Eversberg | 5ac4478 | 2011-11-06 20:35:48 +0100 | [diff] [blame] | 159 | void lapdm_entity_init(struct lapdm_entity *le, enum lapdm_mode mode, int t200) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 160 | { |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 161 | /* convert from single full-second value to per-SAPI milli-second value */ |
| 162 | int t200_ms_sapi_arr[_NR_DL_SAPI]; |
| 163 | int i; |
| 164 | |
| 165 | for (i = 0; i < ARRAY_SIZE(t200_ms_sapi_arr); i++) |
| 166 | t200_ms_sapi_arr[i] = t200 * 1000; |
| 167 | |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 168 | return lapdm_entity_init3(le, mode, t200_ms_sapi_arr, N200, NULL); |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /*! initialize a LAPDm entity and all datalinks inside |
| 172 | * \param[in] le LAPDm entity |
| 173 | * \param[in] mode lapdm_mode (BTS/MS) |
| 174 | * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds |
| 175 | * \param[in] n200 N200 re-transmisison count |
| 176 | */ |
| 177 | void lapdm_entity_init2(struct lapdm_entity *le, enum lapdm_mode mode, |
| 178 | const int *t200_ms, int n200) |
| 179 | { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 180 | lapdm_entity_init3(le, mode, t200_ms, n200, NULL); |
| 181 | } |
| 182 | |
| 183 | /*! initialize a LAPDm entity and all datalinks inside |
| 184 | * \param[in] le LAPDm entity |
| 185 | * \param[in] mode lapdm_mode (BTS/MS) |
| 186 | * \param[in] t200_ms per-SAPI array of T200 re-transmission timer in milli-seconds |
| 187 | * \param[in] n200 N200 re-transmisison count |
| 188 | * \param[in] name human-readable name (will be copied internally + extended with SAPI) |
| 189 | */ |
| 190 | void lapdm_entity_init3(struct lapdm_entity *le, enum lapdm_mode mode, |
| 191 | const int *t200_ms, int n200, const char *name_pfx) |
| 192 | { |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 193 | unsigned int i; |
| 194 | |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 195 | for (i = 0; i < ARRAY_SIZE(le->datalink); i++) { |
| 196 | char name[256]; |
| 197 | if (name_pfx) { |
| 198 | snprintf(name, sizeof(name), "%s[%s]", name_pfx, i == 0 ? "0" : "3"); |
| 199 | lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200, name); |
| 200 | } else |
| 201 | lapdm_dl_init(&le->datalink[i], le, t200_ms[i], n200, NULL); |
| 202 | } |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 203 | |
| 204 | lapdm_entity_set_mode(le, mode); |
| 205 | } |
| 206 | |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 207 | static int get_n200_dcch(enum gsm_chan_t chan_t) |
| 208 | { |
| 209 | switch (chan_t) { |
| 210 | case GSM_LCHAN_SDCCH: |
| 211 | return N200_TR_SDCCH; |
| 212 | case GSM_LCHAN_TCH_F: |
| 213 | return N200_TR_FACCH_FR; |
| 214 | case GSM_LCHAN_TCH_H: |
| 215 | return N200_TR_FACCH_HR; |
| 216 | default: |
| 217 | return -1; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /*! initialize a LAPDm channel and all its channels |
| 222 | * \param[in] lc lapdm_channel to be initialized |
| 223 | * \param[in] mode lapdm_mode (BTS/MS) |
| 224 | * |
| 225 | * Don't use this function; It doesn't support different T200 values per API |
| 226 | * and doesn't set the correct N200 counter, both of which |
| 227 | * are required by GSM specs and supported by lapdm_channel_init2(). |
| 228 | */ |
| 229 | void lapdm_channel_init(struct lapdm_channel *lc, enum lapdm_mode mode) |
| 230 | { |
| 231 | /* emulate old backwards-compatible behavior with 1s/2s */ |
| 232 | const int t200_ms_dcch[_NR_DL_SAPI] = { 1000, 1000 }; |
| 233 | const int t200_ms_acch[_NR_DL_SAPI] = { 2000, 2000 }; |
| 234 | |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 235 | lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, GSM_LCHAN_SDCCH, NULL); |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 238 | /*! initialize a LAPDm channel and all its channels |
Harald Welte | 6bdf0b1 | 2011-08-17 18:22:08 +0200 | [diff] [blame] | 239 | * \param[in] lc \ref lapdm_channel to be initialized |
| 240 | * \param[in] mode \ref lapdm_mode (BTS/MS) |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 241 | * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH |
| 242 | * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH |
| 243 | * \param[in] chan_t GSM channel type (to correctly set N200) |
Harald Welte | 6bdf0b1 | 2011-08-17 18:22:08 +0200 | [diff] [blame] | 244 | */ |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 245 | int lapdm_channel_init2(struct lapdm_channel *lc, enum lapdm_mode mode, |
| 246 | const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 247 | { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 248 | return lapdm_channel_init3(lc, mode, t200_ms_dcch, t200_ms_acch, chan_t, NULL); |
| 249 | } |
| 250 | |
| 251 | /*! initialize a LAPDm channel and all its channels |
| 252 | * \param[in] lc \ref lapdm_channel to be initialized |
| 253 | * \param[in] mode \ref lapdm_mode (BTS/MS) |
| 254 | * \param[in] t200_ms_dcch per-SAPI array of T200 in milli-seconds for DCCH |
| 255 | * \param[in] t200_ms_acch per-SAPI array of T200 in milli-seconds for SACCH |
| 256 | * \param[in] chan_t GSM channel type (to correctly set N200) |
Vadim Yanitskiy | 64277a0 | 2023-02-28 03:30:27 +0700 | [diff] [blame] | 257 | * \param[in] name_pfx human-readable name (copied by function + extended with ACCH/DCCH) |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 258 | */ |
| 259 | int lapdm_channel_init3(struct lapdm_channel *lc, enum lapdm_mode mode, |
| 260 | const int *t200_ms_dcch, const int *t200_ms_acch, enum gsm_chan_t chan_t, |
| 261 | const char *name_pfx) |
| 262 | { |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 263 | int n200_dcch = get_n200_dcch(chan_t); |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 264 | char namebuf[256]; |
| 265 | char *name = NULL; |
| 266 | |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 267 | if (n200_dcch < 0) |
| 268 | return -EINVAL; |
| 269 | |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 270 | osmo_talloc_replace_string(tall_lapd_ctx, &lc->name, name_pfx); |
| 271 | |
| 272 | if (name_pfx) { |
| 273 | snprintf(namebuf, sizeof(namebuf), "%s[ACCH]", name_pfx); |
| 274 | name = namebuf; |
| 275 | } |
| 276 | lapdm_entity_init3(&lc->lapdm_acch, mode, t200_ms_acch, N200_TR_SACCH, name); |
Harald Welte | 3e8c520 | 2018-05-04 20:58:48 +0200 | [diff] [blame] | 277 | lc->lapdm_acch.lapdm_ch = lc; |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 278 | |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 279 | if (name_pfx) { |
| 280 | snprintf(namebuf, sizeof(namebuf), "%s[DCCH]", name_pfx); |
| 281 | name = namebuf; |
| 282 | } |
| 283 | lapdm_entity_init3(&lc->lapdm_dcch, mode, t200_ms_dcch, n200_dcch, name); |
Harald Welte | 3e8c520 | 2018-05-04 20:58:48 +0200 | [diff] [blame] | 284 | lc->lapdm_dcch.lapdm_ch = lc; |
Harald Welte | 20de620 | 2019-06-02 21:33:38 +0200 | [diff] [blame] | 285 | |
| 286 | return 0; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 289 | /*! flush and release all resoures in LAPDm entity */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 290 | void lapdm_entity_exit(struct lapdm_entity *le) |
| 291 | { |
| 292 | unsigned int i; |
| 293 | struct lapdm_datalink *dl; |
| 294 | |
| 295 | for (i = 0; i < ARRAY_SIZE(le->datalink); i++) { |
| 296 | dl = &le->datalink[i]; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 297 | lapd_dl_exit(&dl->dl); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 301 | /* lfush and release all resources in LAPDm channel |
Harald Welte | 6bdf0b1 | 2011-08-17 18:22:08 +0200 | [diff] [blame] | 302 | * |
| 303 | * A convenience wrapper calling \ref lapdm_entity_exit on both |
| 304 | * entities inside the \ref lapdm_channel |
| 305 | */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 306 | void lapdm_channel_exit(struct lapdm_channel *lc) |
| 307 | { |
| 308 | lapdm_entity_exit(&lc->lapdm_acch); |
| 309 | lapdm_entity_exit(&lc->lapdm_dcch); |
| 310 | } |
| 311 | |
Daniel Willmann | 55405fb | 2014-03-26 13:45:17 +0100 | [diff] [blame] | 312 | struct lapdm_datalink *lapdm_datalink_for_sapi(struct lapdm_entity *le, uint8_t sapi) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 313 | { |
| 314 | switch (sapi) { |
| 315 | case LAPDm_SAPI_NORMAL: |
| 316 | return &le->datalink[0]; |
| 317 | case LAPDm_SAPI_SMS: |
| 318 | return &le->datalink[1]; |
| 319 | default: |
| 320 | return NULL; |
| 321 | } |
| 322 | } |
| 323 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 324 | /* Append padding (if required) */ |
| 325 | static void lapdm_pad_msgb(struct msgb *msg, uint8_t n201) |
| 326 | { |
| 327 | int pad_len = n201 - msgb_l2len(msg); |
| 328 | uint8_t *data; |
| 329 | |
| 330 | if (pad_len < 0) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 331 | LOGP(DLLAPD, LOGL_ERROR, |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 332 | "cannot pad message that is already too big!\n"); |
| 333 | return; |
| 334 | } |
| 335 | |
Vadim Yanitskiy | 29ecabe | 2020-08-27 02:12:23 +0700 | [diff] [blame] | 336 | data = msgb_put(msg, pad_len); /* TODO: random padding */ |
| 337 | memset(data, GSM_MACBLOCK_PADDING, pad_len); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* input function that L2 calls when sending messages up to L3 */ |
| 341 | static int rslms_sendmsg(struct msgb *msg, struct lapdm_entity *le) |
| 342 | { |
| 343 | if (!le->l3_cb) { |
| 344 | msgb_free(msg); |
| 345 | return -EIO; |
| 346 | } |
| 347 | |
| 348 | /* call the layer2 message handler that is registered */ |
| 349 | return le->l3_cb(msg, le, le->l3_ctx); |
| 350 | } |
| 351 | |
| 352 | /* write a frame into the tx queue */ |
| 353 | static int tx_ph_data_enqueue(struct lapdm_datalink *dl, struct msgb *msg, |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 354 | uint8_t chan_nr, uint8_t link_id, uint8_t pad) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 355 | { |
| 356 | struct lapdm_entity *le = dl->entity; |
| 357 | struct osmo_phsap_prim pp; |
| 358 | |
| 359 | /* if there is a pending message, queue it */ |
| 360 | if (le->tx_pending || le->flags & LAPDM_ENT_F_POLLING_ONLY) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 361 | *msgb_push(msg, 1) = pad; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 362 | *msgb_push(msg, 1) = link_id; |
| 363 | *msgb_push(msg, 1) = chan_nr; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 364 | msgb_enqueue(&dl->dl.tx_queue, msg); |
Andreas Eversberg | cb72e74 | 2023-06-07 13:18:08 +0200 | [diff] [blame] | 365 | return 0; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA, |
| 369 | PRIM_OP_REQUEST, msg); |
| 370 | pp.u.data.chan_nr = chan_nr; |
| 371 | pp.u.data.link_id = link_id; |
| 372 | |
| 373 | /* send the frame now */ |
| 374 | le->tx_pending = 0; /* disabled flow control */ |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 375 | lapdm_pad_msgb(msg, pad); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 376 | |
| 377 | return le->l1_prim_cb(&pp.oph, le->l1_ctx); |
| 378 | } |
| 379 | |
Vadim Yanitskiy | 776c5b1 | 2020-08-27 20:58:49 +0700 | [diff] [blame] | 380 | /* Dequeue a Downlink message for DCCH (dedicated channel) */ |
| 381 | static struct msgb *tx_dequeue_dcch_msgb(struct lapdm_entity *le) |
| 382 | { |
| 383 | struct msgb *msg; |
| 384 | |
| 385 | /* SAPI=0 always has higher priority than SAPI=3 */ |
| 386 | msg = msgb_dequeue(&le->datalink[DL_SAPI0].dl.tx_queue); |
| 387 | if (msg == NULL) /* no SAPI=0 messages, dequeue SAPI=3 (if any) */ |
| 388 | msg = msgb_dequeue(&le->datalink[DL_SAPI3].dl.tx_queue); |
| 389 | |
| 390 | return msg; |
| 391 | } |
| 392 | |
| 393 | /* Dequeue a Downlink message for ACCH (associated channel) */ |
| 394 | static struct msgb *tx_dequeue_acch_msgb(struct lapdm_entity *le) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 395 | { |
| 396 | struct lapdm_datalink *dl; |
| 397 | int last = le->last_tx_dequeue; |
| 398 | int i = last, n = ARRAY_SIZE(le->datalink); |
| 399 | struct msgb *msg = NULL; |
| 400 | |
| 401 | /* round-robin dequeue */ |
| 402 | do { |
| 403 | /* next */ |
| 404 | i = (i + 1) % n; |
| 405 | dl = &le->datalink[i]; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 406 | if ((msg = msgb_dequeue(&dl->dl.tx_queue))) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 407 | break; |
| 408 | } while (i != last); |
| 409 | |
| 410 | if (msg) { |
| 411 | /* Set last dequeue position */ |
| 412 | le->last_tx_dequeue = i; |
| 413 | } |
| 414 | |
| 415 | return msg; |
| 416 | } |
| 417 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 418 | /*! dequeue a msg that's pending transmission via L1 and wrap it into |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 419 | * a osmo_phsap_prim */ |
| 420 | int lapdm_phsap_dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp) |
| 421 | { |
| 422 | struct msgb *msg; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 423 | uint8_t pad; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 424 | |
Vadim Yanitskiy | 776c5b1 | 2020-08-27 20:58:49 +0700 | [diff] [blame] | 425 | /* Dequeue depending on channel type: DCCH or ACCH. |
| 426 | * See 3GPP TS 44.005, section 4.2.2 "Priority". */ |
| 427 | if (le == &le->lapdm_ch->lapdm_dcch) |
| 428 | msg = tx_dequeue_dcch_msgb(le); |
| 429 | else |
| 430 | msg = tx_dequeue_acch_msgb(le); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 431 | if (!msg) |
| 432 | return -ENODEV; |
| 433 | |
| 434 | /* if we have a message, send PH-DATA.req */ |
| 435 | osmo_prim_init(&pp->oph, SAP_GSM_PH, PRIM_PH_DATA, |
| 436 | PRIM_OP_REQUEST, msg); |
| 437 | |
| 438 | /* Pull chan_nr and link_id */ |
| 439 | pp->u.data.chan_nr = *msg->data; |
| 440 | msgb_pull(msg, 1); |
| 441 | pp->u.data.link_id = *msg->data; |
| 442 | msgb_pull(msg, 1); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 443 | pad = *msg->data; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 444 | msgb_pull(msg, 1); |
| 445 | |
| 446 | /* Pad the frame, we can transmit now */ |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 447 | lapdm_pad_msgb(msg, pad); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /* get next frame from the tx queue. because the ms has multiple datalinks, |
| 453 | * each datalink's queue is read round-robin. |
| 454 | */ |
| 455 | static int l2_ph_data_conf(struct msgb *msg, struct lapdm_entity *le) |
| 456 | { |
| 457 | struct osmo_phsap_prim pp; |
| 458 | |
| 459 | /* we may send again */ |
| 460 | le->tx_pending = 0; |
| 461 | |
| 462 | /* free confirm message */ |
| 463 | if (msg) |
| 464 | msgb_free(msg); |
| 465 | |
| 466 | if (lapdm_phsap_dequeue_prim(le, &pp) < 0) { |
| 467 | /* no message in all queues */ |
| 468 | |
| 469 | /* If user didn't request PH-EMPTY_FRAME.req, abort */ |
| 470 | if (!(le->flags & LAPDM_ENT_F_EMPTY_FRAME)) |
| 471 | return 0; |
| 472 | |
| 473 | /* otherwise, send PH-EMPTY_FRAME.req */ |
| 474 | osmo_prim_init(&pp.oph, SAP_GSM_PH, |
| 475 | PRIM_PH_EMPTY_FRAME, |
| 476 | PRIM_OP_REQUEST, NULL); |
| 477 | } else { |
| 478 | le->tx_pending = 1; |
| 479 | } |
| 480 | |
| 481 | return le->l1_prim_cb(&pp.oph, le->l1_ctx); |
| 482 | } |
| 483 | |
Harald Welte | 542301b | 2018-04-19 16:11:14 +0200 | [diff] [blame] | 484 | /* Is a given msg_type "transparent" as per TS 48.058 Section 8.1 */ |
| 485 | static int rsl_is_transparent(uint8_t msg_type) |
| 486 | { |
| 487 | switch (msg_type) { |
| 488 | case RSL_MT_DATA_IND: |
| 489 | case RSL_MT_UNIT_DATA_IND: |
| 490 | return 1; |
| 491 | case RSL_MT_DATA_REQ: |
| 492 | case RSL_MT_UNIT_DATA_REQ: |
| 493 | return 1; |
| 494 | default: |
| 495 | return 0; |
| 496 | } |
| 497 | } |
| 498 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 499 | /* Create RSLms various RSLms messages */ |
| 500 | static int send_rslms_rll_l3(uint8_t msg_type, struct lapdm_msg_ctx *mctx, |
| 501 | struct msgb *msg) |
| 502 | { |
Harald Welte | 542301b | 2018-04-19 16:11:14 +0200 | [diff] [blame] | 503 | int transparent = rsl_is_transparent(msg_type); |
| 504 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 505 | /* Add the RSL + RLL header */ |
Pau Espin Pedrol | 06da40b | 2023-08-23 17:40:48 +0200 | [diff] [blame] | 506 | rsl_rll_push_l3(msg, msg_type, mctx->chan_nr, mctx->link_id, transparent); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 507 | |
| 508 | /* send off the RSLms message to L3 */ |
| 509 | return rslms_sendmsg(msg, mctx->dl->entity); |
| 510 | } |
| 511 | |
| 512 | /* Take a B4 format message from L1 and create RSLms UNIT DATA IND */ |
| 513 | static int send_rslms_rll_l3_ui(struct lapdm_msg_ctx *mctx, struct msgb *msg) |
| 514 | { |
| 515 | uint8_t l3_len = msg->tail - (uint8_t *)msgb_l3(msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 516 | |
| 517 | /* Add the RSL + RLL header */ |
| 518 | msgb_tv16_push(msg, RSL_IE_L3_INFO, l3_len); |
Harald Welte | d977f5f | 2018-05-08 21:53:28 +0200 | [diff] [blame] | 519 | |
Harald Welte | f1bdf78 | 2018-05-08 22:03:20 +0200 | [diff] [blame] | 520 | /* Add two non-standard IEs carrying MS power and TA values for B4 (SACCH) */ |
| 521 | if (mctx->lapdm_fmt == LAPDm_FMT_B4) { |
| 522 | msgb_tv_push(msg, RSL_IE_MS_POWER, mctx->tx_power_ind); |
| 523 | msgb_tv_push(msg, RSL_IE_TIMING_ADVANCE, mctx->ta_ind); |
| 524 | } |
Harald Welte | d977f5f | 2018-05-08 21:53:28 +0200 | [diff] [blame] | 525 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 526 | rsl_rll_push_hdr(msg, RSL_MT_UNIT_DATA_IND, mctx->chan_nr, |
| 527 | mctx->link_id, 1); |
Pau Espin Pedrol | a99e110 | 2017-12-08 14:30:47 +0100 | [diff] [blame] | 528 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 529 | return rslms_sendmsg(msg, mctx->dl->entity); |
| 530 | } |
| 531 | |
| 532 | static int send_rll_simple(uint8_t msg_type, struct lapdm_msg_ctx *mctx) |
| 533 | { |
| 534 | struct msgb *msg; |
Harald Welte | 542301b | 2018-04-19 16:11:14 +0200 | [diff] [blame] | 535 | int transparent = rsl_is_transparent(msg_type); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 536 | |
Harald Welte | 542301b | 2018-04-19 16:11:14 +0200 | [diff] [blame] | 537 | msg = rsl_rll_simple(msg_type, mctx->chan_nr, mctx->link_id, transparent); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 538 | |
| 539 | /* send off the RSLms message to L3 */ |
| 540 | return rslms_sendmsg(msg, mctx->dl->entity); |
| 541 | } |
| 542 | |
| 543 | static int rsl_rll_error(uint8_t cause, struct lapdm_msg_ctx *mctx) |
| 544 | { |
| 545 | struct msgb *msg; |
| 546 | |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 547 | LOGDL(&mctx->dl->dl, LOGL_NOTICE, "sending MDL-ERROR-IND %d\n", cause); |
Harald Welte | 542301b | 2018-04-19 16:11:14 +0200 | [diff] [blame] | 548 | msg = rsl_rll_simple(RSL_MT_ERROR_IND, mctx->chan_nr, mctx->link_id, 0); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 549 | msgb_tlv_put(msg, RSL_IE_RLM_CAUSE, 1, &cause); |
| 550 | return rslms_sendmsg(msg, mctx->dl->entity); |
| 551 | } |
| 552 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 553 | /* DLSAP L2 -> L3 (RSLms) */ |
| 554 | static int send_rslms_dlsap(struct osmo_dlsap_prim *dp, |
| 555 | struct lapd_msg_ctx *lctx) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 556 | { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 557 | struct lapd_datalink *dl = lctx->dl; |
| 558 | struct lapdm_datalink *mdl = |
| 559 | container_of(dl, struct lapdm_datalink, dl); |
| 560 | struct lapdm_msg_ctx *mctx = &mdl->mctx; |
| 561 | uint8_t rll_msg = 0; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 562 | |
Andreas Eversberg | 78122ab | 2011-09-27 12:06:55 +0200 | [diff] [blame] | 563 | switch (OSMO_PRIM_HDR(&dp->oph)) { |
| 564 | case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_INDICATION): |
| 565 | rll_msg = RSL_MT_EST_IND; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 566 | break; |
Andreas Eversberg | 78122ab | 2011-09-27 12:06:55 +0200 | [diff] [blame] | 567 | case OSMO_PRIM(PRIM_DL_EST, PRIM_OP_CONFIRM): |
| 568 | rll_msg = RSL_MT_EST_CONF; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 569 | break; |
Andreas Eversberg | 78122ab | 2011-09-27 12:06:55 +0200 | [diff] [blame] | 570 | case OSMO_PRIM(PRIM_DL_DATA, PRIM_OP_INDICATION): |
| 571 | rll_msg = RSL_MT_DATA_IND; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 572 | break; |
Andreas Eversberg | 78122ab | 2011-09-27 12:06:55 +0200 | [diff] [blame] | 573 | case OSMO_PRIM(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION): |
| 574 | return send_rslms_rll_l3_ui(mctx, dp->oph.msg); |
| 575 | case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_INDICATION): |
| 576 | rll_msg = RSL_MT_REL_IND; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 577 | break; |
Andreas Eversberg | 78122ab | 2011-09-27 12:06:55 +0200 | [diff] [blame] | 578 | case OSMO_PRIM(PRIM_DL_REL, PRIM_OP_CONFIRM): |
| 579 | rll_msg = RSL_MT_REL_CONF; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 580 | break; |
Andreas Eversberg | 78122ab | 2011-09-27 12:06:55 +0200 | [diff] [blame] | 581 | case OSMO_PRIM(PRIM_DL_SUSP, PRIM_OP_CONFIRM): |
| 582 | rll_msg = RSL_MT_SUSP_CONF; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 583 | break; |
Andreas Eversberg | 78122ab | 2011-09-27 12:06:55 +0200 | [diff] [blame] | 584 | case OSMO_PRIM(PRIM_MDL_ERROR, PRIM_OP_INDICATION): |
| 585 | rsl_rll_error(dp->u.error_ind.cause, mctx); |
| 586 | if (dp->oph.msg) |
| 587 | msgb_free(dp->oph.msg); |
| 588 | return 0; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 589 | } |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 590 | |
| 591 | if (!rll_msg) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 592 | LOGDL(dl, LOGL_ERROR, "Unsupported op %d, prim %d. Please " |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 593 | "fix!\n", dp->oph.primitive, dp->oph.operation); |
| 594 | return -EINVAL; |
| 595 | } |
| 596 | |
| 597 | if (!dp->oph.msg) |
| 598 | return send_rll_simple(rll_msg, mctx); |
| 599 | |
| 600 | return send_rslms_rll_l3(rll_msg, mctx, dp->oph.msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 601 | } |
| 602 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 603 | /* send a data frame to layer 1 */ |
| 604 | static int lapdm_send_ph_data_req(struct lapd_msg_ctx *lctx, struct msgb *msg) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 605 | { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 606 | uint8_t l3_len = msg->tail - msg->data; |
| 607 | struct lapd_datalink *dl = lctx->dl; |
| 608 | struct lapdm_datalink *mdl = |
| 609 | container_of(dl, struct lapdm_datalink, dl); |
| 610 | struct lapdm_msg_ctx *mctx = &mdl->mctx; |
| 611 | int format = lctx->format; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 612 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 613 | /* prepend l2 header */ |
| 614 | msg->l2h = msgb_push(msg, 3); |
| 615 | msg->l2h[0] = LAPDm_ADDR(lctx->lpd, lctx->sapi, lctx->cr); |
| 616 | /* EA is set here too */ |
| 617 | switch (format) { |
| 618 | case LAPD_FORM_I: |
| 619 | msg->l2h[1] = LAPDm_CTRL_I(lctx->n_recv, lctx->n_send, |
| 620 | lctx->p_f); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 621 | break; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 622 | case LAPD_FORM_S: |
| 623 | msg->l2h[1] = LAPDm_CTRL_S(lctx->n_recv, lctx->s_u, lctx->p_f); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 624 | break; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 625 | case LAPD_FORM_U: |
| 626 | msg->l2h[1] = LAPDm_CTRL_U(lctx->s_u, lctx->p_f); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 627 | break; |
| 628 | default: |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 629 | msgb_free(msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 630 | return -EINVAL; |
| 631 | } |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 632 | msg->l2h[2] = LAPDm_LEN(l3_len); /* EL is set here too */ |
| 633 | if (lctx->more) |
| 634 | msg->l2h[2] |= LAPDm_MORE; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 635 | |
Andreas.Eversberg | f1f80de | 2011-11-06 20:45:29 +0100 | [diff] [blame] | 636 | /* add ACCH header with last indicated tx-power and TA */ |
| 637 | if ((mctx->link_id & 0x40)) { |
| 638 | struct lapdm_entity *le = mdl->entity; |
| 639 | |
| 640 | msg->l2h = msgb_push(msg, 2); |
| 641 | msg->l2h[0] = le->tx_power; |
| 642 | msg->l2h[1] = le->ta; |
| 643 | } |
| 644 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 645 | return tx_ph_data_enqueue(mctx->dl, msg, mctx->chan_nr, mctx->link_id, |
| 646 | 23); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 647 | } |
| 648 | |
Daniel Willmann | 3dc4e16 | 2014-03-20 19:24:48 +0100 | [diff] [blame] | 649 | static int update_pending_frames(struct lapd_msg_ctx *lctx) |
| 650 | { |
| 651 | struct lapd_datalink *dl = lctx->dl; |
| 652 | struct msgb *msg; |
| 653 | int rc = -1; |
| 654 | |
| 655 | llist_for_each_entry(msg, &dl->tx_queue, list) { |
| 656 | if (LAPDm_CTRL_is_I(msg->l2h[1])) { |
| 657 | msg->l2h[1] = LAPDm_CTRL_I(dl->v_recv, LAPDm_CTRL_I_Ns(msg->l2h[1]), |
| 658 | LAPDm_CTRL_PF_BIT(msg->l2h[1])); |
| 659 | rc = 0; |
| 660 | } else if (LAPDm_CTRL_is_S(msg->l2h[1])) { |
Andreas Eversberg | 1bb0b99 | 2023-11-09 12:09:21 +0100 | [diff] [blame] | 661 | msg->l2h[1] = LAPDm_CTRL_S(dl->v_recv, LAPDm_CTRL_S_BITS(msg->l2h[1]), |
| 662 | LAPDm_CTRL_PF_BIT(msg->l2h[1])); |
Daniel Willmann | 3dc4e16 | 2014-03-20 19:24:48 +0100 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | return rc; |
| 667 | } |
| 668 | |
Harald Welte | 1284c3e | 2018-05-01 18:11:02 +0200 | [diff] [blame] | 669 | /* determine if receiving a given LAPDm message is not permitted */ |
| 670 | static int lapdm_rx_not_permitted(const struct lapdm_entity *le, |
| 671 | const struct lapd_msg_ctx *lctx) |
| 672 | { |
| 673 | /* we currently only implement SABM related checks here */ |
| 674 | if (lctx->format != LAPD_FORM_U || lctx->s_u != LAPD_U_SABM) |
| 675 | return 0; |
| 676 | |
| 677 | if (le->mode == LAPDM_MODE_BTS) { |
| 678 | if (le == &le->lapdm_ch->lapdm_acch) { |
| 679 | /* no contention resolution on SACCH */ |
| 680 | if (lctx->length > 0) |
| 681 | return RLL_CAUSE_SABM_INFO_NOTALL; |
| 682 | } else { |
| 683 | switch (lctx->sapi) { |
Harald Welte | 1284c3e | 2018-05-01 18:11:02 +0200 | [diff] [blame] | 684 | case 3: |
| 685 | /* SAPI3 doesn't support contention resolution */ |
| 686 | if (lctx->length > 0) |
| 687 | return RLL_CAUSE_SABM_INFO_NOTALL; |
| 688 | break; |
Harald Welte | b82a407 | 2018-05-09 16:31:16 +0200 | [diff] [blame] | 689 | default: |
| 690 | break; |
Harald Welte | 1284c3e | 2018-05-01 18:11:02 +0200 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | } else if (le->mode == LAPDM_MODE_MS) { |
| 694 | /* contention resolution (L3 present) is only sent by MS, but |
| 695 | * never received by it */ |
| 696 | if (lctx->length > 0) |
| 697 | return RLL_CAUSE_SABM_INFO_NOTALL; |
| 698 | } |
| 699 | return 0; |
| 700 | } |
| 701 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 702 | /* input into layer2 (from layer 1) */ |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 703 | static int l2_ph_data_ind(struct msgb *msg, struct lapdm_entity *le, |
Pau Espin Pedrol | 1247aa1 | 2023-08-09 17:41:25 +0200 | [diff] [blame] | 704 | uint8_t chan_nr, uint8_t link_id, uint32_t fn) |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 705 | { |
| 706 | uint8_t cbits = chan_nr >> 3; |
Harald Welte | 6420774 | 2011-06-27 23:32:14 +0200 | [diff] [blame] | 707 | uint8_t sapi; /* we cannot take SAPI from link_id, as L1 has no clue */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 708 | struct lapdm_msg_ctx mctx; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 709 | struct lapd_msg_ctx lctx; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 710 | int rc = 0; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 711 | int n201; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 712 | |
| 713 | /* when we reach here, we have a msgb with l2h pointing to the raw |
| 714 | * 23byte mac block. The l1h has already been purged. */ |
| 715 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 716 | memset(&mctx, 0, sizeof(mctx)); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 717 | mctx.chan_nr = chan_nr; |
| 718 | mctx.link_id = link_id; |
Pau Espin Pedrol | 1247aa1 | 2023-08-09 17:41:25 +0200 | [diff] [blame] | 719 | mctx.fn = fn; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 720 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 721 | /* check for L1 chan_nr/link_id and determine LAPDm hdr format */ |
| 722 | if (cbits == 0x10 || cbits == 0x12) { |
| 723 | /* Format Bbis is used on BCCH and CCCH(PCH, NCH and AGCH) */ |
| 724 | mctx.lapdm_fmt = LAPDm_FMT_Bbis; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 725 | n201 = N201_Bbis; |
Harald Welte | 6420774 | 2011-06-27 23:32:14 +0200 | [diff] [blame] | 726 | sapi = 0; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 727 | } else { |
| 728 | if (mctx.link_id & 0x40) { |
Harald Welte | 7ca604b | 2011-06-29 12:13:51 +0200 | [diff] [blame] | 729 | /* It was received from network on SACCH */ |
| 730 | |
Andreas Eversberg | 0370018 | 2023-05-10 13:23:55 +0200 | [diff] [blame] | 731 | /* A Short L3 header has both bits == 0. */ |
| 732 | if (LAPDm_ADDR_SHORT_L2(msg->l2h[2]) == 0) { |
| 733 | mctx.lapdm_fmt = LAPDm_FMT_Bter; |
| 734 | n201 = N201_Bter_SACCH; |
| 735 | sapi = 0; |
| 736 | } else if (le->mode == LAPDM_MODE_MS |
| 737 | && LAPDm_CTRL_is_U(msg->l2h[3]) |
| 738 | && LAPDm_CTRL_U_BITS(msg->l2h[3]) == 0) { |
| 739 | /* If UI on SACCH sent by BTS, lapdm_fmt must be B4 */ |
Harald Welte | 7ca604b | 2011-06-29 12:13:51 +0200 | [diff] [blame] | 740 | mctx.lapdm_fmt = LAPDm_FMT_B4; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 741 | n201 = N201_B4; |
Andreas Eversberg | 0370018 | 2023-05-10 13:23:55 +0200 | [diff] [blame] | 742 | /* sapi is found after two-btyte L1 header */ |
| 743 | sapi = (msg->l2h[2] >> 2) & 7; |
Harald Welte | 7ca604b | 2011-06-29 12:13:51 +0200 | [diff] [blame] | 744 | } else { |
| 745 | mctx.lapdm_fmt = LAPDm_FMT_B; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 746 | n201 = N201_AB_SACCH; |
Andreas Eversberg | 0370018 | 2023-05-10 13:23:55 +0200 | [diff] [blame] | 747 | /* sapi is found after two-btyte L1 header */ |
| 748 | sapi = (msg->l2h[2] >> 2) & 7; |
Harald Welte | 7ca604b | 2011-06-29 12:13:51 +0200 | [diff] [blame] | 749 | } |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 750 | /* SACCH frames have a two-byte L1 header that |
| 751 | * OsmocomBB L1 doesn't strip */ |
| 752 | mctx.tx_power_ind = msg->l2h[0] & 0x1f; |
| 753 | mctx.ta_ind = msg->l2h[1]; |
| 754 | msgb_pull(msg, 2); |
| 755 | msg->l2h += 2; |
| 756 | } else { |
Andreas Eversberg | 0370018 | 2023-05-10 13:23:55 +0200 | [diff] [blame] | 757 | /* A Short L3 header has both bits == 0. */ |
Andreas Eversberg | 16ad6c2 | 2023-09-08 19:31:06 +0200 | [diff] [blame] | 758 | if (LAPDm_ADDR_SHORT_L2(msg->l2h[0]) == 0) { |
Andreas Eversberg | 0370018 | 2023-05-10 13:23:55 +0200 | [diff] [blame] | 759 | mctx.lapdm_fmt = LAPDm_FMT_Bter; |
| 760 | n201 = N201_Bter_SDCCH; |
| 761 | sapi = 0; |
| 762 | } else { |
| 763 | mctx.lapdm_fmt = LAPDm_FMT_B; |
| 764 | n201 = N201_AB_SDCCH; |
| 765 | sapi = (msg->l2h[0] >> 2) & 7; |
| 766 | } |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Daniel Willmann | 55405fb | 2014-03-26 13:45:17 +0100 | [diff] [blame] | 770 | mctx.dl = lapdm_datalink_for_sapi(le, sapi); |
Harald Welte | 6420774 | 2011-06-27 23:32:14 +0200 | [diff] [blame] | 771 | /* G.2.1 No action on frames containing an unallocated SAPI. */ |
| 772 | if (!mctx.dl) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 773 | LOGP(DLLAPD, LOGL_NOTICE, "Received frame for unsupported SAPI %d!\n", sapi); |
Harald Welte | 6420774 | 2011-06-27 23:32:14 +0200 | [diff] [blame] | 774 | msgb_free(msg); |
| 775 | return -EIO; |
| 776 | } |
| 777 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 778 | switch (mctx.lapdm_fmt) { |
| 779 | case LAPDm_FMT_A: |
| 780 | case LAPDm_FMT_B: |
| 781 | case LAPDm_FMT_B4: |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 782 | lctx.dl = &mctx.dl->dl; |
| 783 | /* obtain SAPI from address field */ |
| 784 | mctx.link_id |= LAPDm_ADDR_SAPI(msg->l2h[0]); |
| 785 | /* G.2.3 EA bit set to "0" is not allowed in GSM */ |
| 786 | if (!LAPDm_ADDR_EA(msg->l2h[0])) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 787 | LOGDL(lctx.dl, LOGL_NOTICE, "EA bit 0 is not allowed in GSM\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 788 | msgb_free(msg); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 789 | rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 790 | return -EINVAL; |
| 791 | } |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 792 | /* adress field */ |
| 793 | lctx.lpd = LAPDm_ADDR_LPD(msg->l2h[0]); |
| 794 | lctx.sapi = LAPDm_ADDR_SAPI(msg->l2h[0]); |
| 795 | lctx.cr = LAPDm_ADDR_CR(msg->l2h[0]); |
| 796 | /* command field */ |
| 797 | if (LAPDm_CTRL_is_I(msg->l2h[1])) { |
| 798 | lctx.format = LAPD_FORM_I; |
| 799 | lctx.n_send = LAPDm_CTRL_I_Ns(msg->l2h[1]); |
| 800 | lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]); |
| 801 | } else if (LAPDm_CTRL_is_S(msg->l2h[1])) { |
| 802 | lctx.format = LAPD_FORM_S; |
| 803 | lctx.n_recv = LAPDm_CTRL_Nr(msg->l2h[1]); |
| 804 | lctx.s_u = LAPDm_CTRL_S_BITS(msg->l2h[1]); |
| 805 | } else if (LAPDm_CTRL_is_U(msg->l2h[1])) { |
| 806 | lctx.format = LAPD_FORM_U; |
| 807 | lctx.s_u = LAPDm_CTRL_U_BITS(msg->l2h[1]); |
| 808 | } else |
| 809 | lctx.format = LAPD_FORM_UKN; |
| 810 | lctx.p_f = LAPDm_CTRL_PF_BIT(msg->l2h[1]); |
| 811 | if (lctx.sapi != LAPDm_SAPI_NORMAL |
| 812 | && lctx.sapi != LAPDm_SAPI_SMS |
| 813 | && lctx.format == LAPD_FORM_U |
| 814 | && lctx.s_u == LAPDm_U_UI) { |
| 815 | /* 5.3.3 UI frames with invalid SAPI values shall be |
| 816 | * discarded |
| 817 | */ |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 818 | LOGDL(lctx.dl, LOGL_INFO, "sapi=%u (discarding)\n", lctx.sapi); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 819 | msgb_free(msg); |
| 820 | return 0; |
| 821 | } |
| 822 | if (mctx.lapdm_fmt == LAPDm_FMT_B4) { |
| 823 | lctx.n201 = n201; |
| 824 | lctx.length = n201; |
| 825 | lctx.more = 0; |
| 826 | msg->l3h = msg->l2h + 2; |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 827 | msgb_pull_to_l3(msg); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 828 | } else { |
| 829 | /* length field */ |
| 830 | if (!(msg->l2h[2] & LAPDm_EL)) { |
| 831 | /* G.4.1 If the EL bit is set to "0", an |
| 832 | * MDL-ERROR-INDICATION primitive with cause |
| 833 | * "frame not implemented" is sent to the |
| 834 | * mobile management entity. */ |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 835 | LOGDL(lctx.dl, LOGL_NOTICE, "we don't support multi-octet length\n"); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 836 | msgb_free(msg); |
| 837 | rsl_rll_error(RLL_CAUSE_FRM_UNIMPL, &mctx); |
| 838 | return -EINVAL; |
| 839 | } |
| 840 | lctx.n201 = n201; |
| 841 | lctx.length = msg->l2h[2] >> 2; |
| 842 | lctx.more = !!(msg->l2h[2] & LAPDm_MORE); |
| 843 | msg->l3h = msg->l2h + 3; |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 844 | msgb_pull_to_l3(msg); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 845 | } |
| 846 | /* store context for messages from lapd */ |
| 847 | memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx)); |
Harald Welte | 1284c3e | 2018-05-01 18:11:02 +0200 | [diff] [blame] | 848 | rc =lapdm_rx_not_permitted(le, &lctx); |
| 849 | if (rc > 0) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 850 | LOGDL(lctx.dl, LOGL_NOTICE, "received message not permitted\n"); |
Harald Welte | 1284c3e | 2018-05-01 18:11:02 +0200 | [diff] [blame] | 851 | msgb_free(msg); |
| 852 | rsl_rll_error(rc, &mctx); |
| 853 | return -EINVAL; |
| 854 | } |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 855 | /* send to LAPD */ |
| 856 | rc = lapd_ph_data_ind(msg, &lctx); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 857 | break; |
| 858 | case LAPDm_FMT_Bter: |
Andreas Eversberg | 0370018 | 2023-05-10 13:23:55 +0200 | [diff] [blame] | 859 | /* fall-through */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 860 | case LAPDm_FMT_Bbis: |
Pau Espin Pedrol | 45ab0d7 | 2023-08-23 18:58:19 +0200 | [diff] [blame] | 861 | /* Update context so that users can read fields like fn: */ |
| 862 | memcpy(&mctx.dl->mctx, &mctx, sizeof(mctx.dl->mctx)); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 863 | /* directly pass up to layer3 */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 864 | msg->l3h = msg->l2h; |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 865 | msgb_pull_to_l3(msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 866 | rc = send_rslms_rll_l3(RSL_MT_UNIT_DATA_IND, &mctx, msg); |
| 867 | break; |
| 868 | default: |
| 869 | msgb_free(msg); |
| 870 | } |
| 871 | |
| 872 | return rc; |
| 873 | } |
| 874 | |
| 875 | /* input into layer2 (from layer 1) */ |
| 876 | static int l2_ph_rach_ind(struct lapdm_entity *le, uint8_t ra, uint32_t fn, uint8_t acc_delay) |
| 877 | { |
| 878 | struct abis_rsl_cchan_hdr *ch; |
| 879 | struct gsm48_req_ref req_ref; |
| 880 | struct gsm_time gt; |
| 881 | struct msgb *msg = msgb_alloc_headroom(512, 64, "RSL CHAN RQD"); |
| 882 | |
Jacob Erlbeck | d154f8b | 2015-04-09 14:22:21 +0200 | [diff] [blame] | 883 | if (!msg) |
| 884 | return -ENOMEM; |
| 885 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 886 | msg->l2h = msgb_push(msg, sizeof(*ch)); |
| 887 | ch = (struct abis_rsl_cchan_hdr *)msg->l2h; |
| 888 | rsl_init_cchan_hdr(ch, RSL_MT_CHAN_RQD); |
| 889 | ch->chan_nr = RSL_CHAN_RACH; |
| 890 | |
| 891 | /* generate a RSL CHANNEL REQUIRED message */ |
| 892 | gsm_fn2gsmtime(>, fn); |
| 893 | req_ref.ra = ra; |
| 894 | req_ref.t1 = gt.t1; /* FIXME: modulo? */ |
| 895 | req_ref.t2 = gt.t2; |
| 896 | req_ref.t3_low = gt.t3 & 7; |
| 897 | req_ref.t3_high = gt.t3 >> 3; |
| 898 | |
| 899 | msgb_tv_fixed_put(msg, RSL_IE_REQ_REFERENCE, 3, (uint8_t *) &req_ref); |
| 900 | msgb_tv_put(msg, RSL_IE_ACCESS_DELAY, acc_delay); |
| 901 | |
| 902 | return rslms_sendmsg(msg, le); |
| 903 | } |
| 904 | |
| 905 | static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr); |
| 906 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 907 | /*! Receive a PH-SAP primitive from L1 */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 908 | int lapdm_phsap_up(struct osmo_prim_hdr *oph, struct lapdm_entity *le) |
| 909 | { |
| 910 | struct osmo_phsap_prim *pp = (struct osmo_phsap_prim *) oph; |
| 911 | int rc = 0; |
| 912 | |
| 913 | if (oph->sap != SAP_GSM_PH) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 914 | LOGP(DLLAPD, LOGL_ERROR, "primitive for unknown SAP %u\n", |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 915 | oph->sap); |
Vadim Yanitskiy | 9ec7749 | 2023-06-23 15:32:15 +0700 | [diff] [blame] | 916 | msgb_free(oph->msg); |
| 917 | return -ENODEV; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 918 | } |
| 919 | |
Vadim Yanitskiy | 9ec7749 | 2023-06-23 15:32:15 +0700 | [diff] [blame] | 920 | switch (OSMO_PRIM_HDR(oph)) { |
| 921 | case OSMO_PRIM(PRIM_PH_DATA, PRIM_OP_INDICATION): |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 922 | rc = l2_ph_data_ind(oph->msg, le, pp->u.data.chan_nr, |
Pau Espin Pedrol | 1247aa1 | 2023-08-09 17:41:25 +0200 | [diff] [blame] | 923 | pp->u.data.link_id, pp->u.data.fn); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 924 | break; |
Vadim Yanitskiy | 9ec7749 | 2023-06-23 15:32:15 +0700 | [diff] [blame] | 925 | case OSMO_PRIM(PRIM_PH_RTS, PRIM_OP_INDICATION): |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 926 | rc = l2_ph_data_conf(oph->msg, le); |
| 927 | break; |
Vadim Yanitskiy | 9ec7749 | 2023-06-23 15:32:15 +0700 | [diff] [blame] | 928 | case OSMO_PRIM(PRIM_PH_RACH, PRIM_OP_INDICATION): |
| 929 | rc = l2_ph_rach_ind(le, pp->u.rach_ind.ra, pp->u.rach_ind.fn, |
| 930 | pp->u.rach_ind.acc_delay); |
| 931 | break; |
| 932 | case OSMO_PRIM(PRIM_PH_RACH, PRIM_OP_CONFIRM): |
| 933 | rc = l2_ph_chan_conf(oph->msg, le, pp->u.rach_ind.fn); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 934 | break; |
Andreas Eversberg | b36ad2d | 2013-02-05 12:01:32 +0100 | [diff] [blame] | 935 | default: |
| 936 | LOGP(DLLAPD, LOGL_ERROR, "Unknown primitive %u\n", |
| 937 | oph->primitive); |
Vadim Yanitskiy | 9ec7749 | 2023-06-23 15:32:15 +0700 | [diff] [blame] | 938 | msgb_free(oph->msg); |
| 939 | return -EINVAL; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | return rc; |
| 943 | } |
| 944 | |
| 945 | |
| 946 | /* L3 -> L2 / RSLMS -> LAPDm */ |
| 947 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 948 | /* Set LAPDm context for established connection */ |
| 949 | static int set_lapdm_context(struct lapdm_datalink *dl, uint8_t chan_nr, |
| 950 | uint8_t link_id, int n201, uint8_t sapi) |
| 951 | { |
| 952 | memset(&dl->mctx, 0, sizeof(dl->mctx)); |
| 953 | dl->mctx.dl = dl; |
| 954 | dl->mctx.chan_nr = chan_nr; |
| 955 | dl->mctx.link_id = link_id; |
| 956 | dl->dl.lctx.dl = &dl->dl; |
| 957 | dl->dl.lctx.n201 = n201; |
| 958 | dl->dl.lctx.sapi = sapi; |
| 959 | |
| 960 | return 0; |
| 961 | } |
| 962 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 963 | /* L3 requests establishment of data link */ |
| 964 | static int rslms_rx_rll_est_req(struct msgb *msg, struct lapdm_datalink *dl) |
| 965 | { |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 966 | struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); |
| 967 | uint8_t chan_nr = rllh->chan_nr; |
| 968 | uint8_t link_id = rllh->link_id; |
| 969 | uint8_t sapi = rllh->link_id & 7; |
| 970 | struct tlv_parsed tv; |
| 971 | uint8_t length; |
Andreas.Eversberg | cbed327 | 2011-11-06 20:43:08 +0100 | [diff] [blame] | 972 | uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 973 | struct osmo_dlsap_prim dp; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 974 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 975 | /* Set LAPDm context for established connection */ |
| 976 | set_lapdm_context(dl, chan_nr, link_id, n201, sapi); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 977 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 978 | rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg) - sizeof(*rllh)); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 979 | if (TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 980 | msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 981 | /* contention resolution establishment procedure */ |
| 982 | if (sapi != 0) { |
| 983 | /* According to clause 6, the contention resolution |
| 984 | * procedure is only permitted with SAPI value 0 */ |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 985 | LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 but contention" |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 986 | "resolution (discarding)\n"); |
| 987 | msgb_free(msg); |
| 988 | return send_rll_simple(RSL_MT_REL_IND, &dl->mctx); |
| 989 | } |
| 990 | /* transmit a SABM command with the P bit set to "1". The SABM |
| 991 | * command shall contain the layer 3 message unit */ |
| 992 | length = TLVP_LEN(&tv, RSL_IE_L3_INFO); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 993 | } else { |
| 994 | /* normal establishment procedure */ |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 995 | msg->l3h = msg->l2h + sizeof(*rllh); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 996 | length = 0; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /* check if the layer3 message length exceeds N201 */ |
Andreas.Eversberg | cbed327 | 2011-11-06 20:43:08 +0100 | [diff] [blame] | 1000 | if (length > n201) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1001 | LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) " |
Andreas.Eversberg | cbed327 | 2011-11-06 20:43:08 +0100 | [diff] [blame] | 1002 | "(discarding)\n", length, n201); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1003 | msgb_free(msg); |
| 1004 | return send_rll_simple(RSL_MT_REL_IND, &dl->mctx); |
| 1005 | } |
| 1006 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1007 | /* Remove RLL header from msgb and set length to L3-info */ |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 1008 | msgb_pull_to_l3(msg); |
| 1009 | msgb_trim(msg, length); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1010 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1011 | /* prepare prim */ |
| 1012 | osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1013 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1014 | /* send to L2 */ |
| 1015 | return lapd_recv_dlsap(&dp, &dl->dl.lctx); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | /* L3 requests transfer of unnumbered information */ |
| 1019 | static int rslms_rx_rll_udata_req(struct msgb *msg, struct lapdm_datalink *dl) |
| 1020 | { |
Andreas.Eversberg | 816e178 | 2011-11-06 20:46:30 +0100 | [diff] [blame] | 1021 | struct lapdm_entity *le = dl->entity; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1022 | struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); |
| 1023 | uint8_t chan_nr = rllh->chan_nr; |
| 1024 | uint8_t link_id = rllh->link_id; |
| 1025 | uint8_t sapi = link_id & 7; |
| 1026 | struct tlv_parsed tv; |
Max | 777be2e | 2017-03-01 18:16:44 +0100 | [diff] [blame] | 1027 | int length, ui_bts; |
Andreas Eversberg | 6799741 | 2023-05-10 13:00:23 +0200 | [diff] [blame] | 1028 | bool use_b_ter; |
Max | 777be2e | 2017-03-01 18:16:44 +0100 | [diff] [blame] | 1029 | |
| 1030 | if (!le) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1031 | LOGDL(&dl->dl, LOGL_ERROR, "lapdm_datalink without entity error\n"); |
Max | 777be2e | 2017-03-01 18:16:44 +0100 | [diff] [blame] | 1032 | msgb_free(msg); |
| 1033 | return -EMLINK; |
| 1034 | } |
| 1035 | ui_bts = (le->mode == LAPDM_MODE_BTS && (link_id & 0x40)); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1036 | |
| 1037 | /* check if the layer3 message length exceeds N201 */ |
| 1038 | |
| 1039 | rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); |
| 1040 | |
| 1041 | if (TLVP_PRESENT(&tv, RSL_IE_TIMING_ADVANCE)) { |
Andreas.Eversberg | f1f80de | 2011-11-06 20:45:29 +0100 | [diff] [blame] | 1042 | le->ta = *TLVP_VAL(&tv, RSL_IE_TIMING_ADVANCE); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1043 | } |
| 1044 | if (TLVP_PRESENT(&tv, RSL_IE_MS_POWER)) { |
Andreas.Eversberg | f1f80de | 2011-11-06 20:45:29 +0100 | [diff] [blame] | 1045 | le->tx_power = *TLVP_VAL(&tv, RSL_IE_MS_POWER); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1046 | } |
| 1047 | if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1048 | LOGDL(&dl->dl, LOGL_ERROR, "unit data request without message error\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1049 | msgb_free(msg); |
| 1050 | return -EINVAL; |
| 1051 | } |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1052 | msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1053 | length = TLVP_LEN(&tv, RSL_IE_L3_INFO); |
Andreas Eversberg | 6799741 | 2023-05-10 13:00:23 +0200 | [diff] [blame] | 1054 | /* check for Bter frame */ |
| 1055 | use_b_ter = (length == ((link_id & 0x40) ? 21 : 23) && sapi == 0); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1056 | /* check if the layer3 message length exceeds N201 */ |
Andreas Eversberg | 6799741 | 2023-05-10 13:00:23 +0200 | [diff] [blame] | 1057 | if (length + ((link_id & 0x40) ? 4 : 2) + !ui_bts > 23 && !use_b_ter) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1058 | LOGDL(&dl->dl, LOGL_ERROR, "frame too large: %d > N201(%d) " |
Andreas Eversberg | 5977db0 | 2013-06-12 09:34:51 +0200 | [diff] [blame] | 1059 | "(discarding)\n", length, |
| 1060 | ((link_id & 0x40) ? 18 : 20) + ui_bts); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1061 | msgb_free(msg); |
| 1062 | return -EIO; |
| 1063 | } |
| 1064 | |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1065 | LOGDL(&dl->dl, LOGL_INFO, "sending unit data (tx_power=%d, ta=%d)\n", le->tx_power, le->ta); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1066 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1067 | /* Remove RLL header from msgb and set length to L3-info */ |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 1068 | msgb_pull_to_l3(msg); |
| 1069 | msgb_trim(msg, length); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1070 | |
| 1071 | /* Push L1 + LAPDm header on msgb */ |
Andreas Eversberg | 6799741 | 2023-05-10 13:00:23 +0200 | [diff] [blame] | 1072 | if (!use_b_ter) { |
| 1073 | msg->l2h = msgb_push(msg, 2 + !ui_bts); |
| 1074 | msg->l2h[0] = LAPDm_ADDR(LAPDm_LPD_NORMAL, sapi, dl->dl.cr.loc2rem.cmd); |
| 1075 | msg->l2h[1] = LAPDm_CTRL_U(LAPDm_U_UI, 0); |
| 1076 | if (!ui_bts) |
| 1077 | msg->l2h[2] = LAPDm_LEN(length); |
| 1078 | } else |
| 1079 | msg->l2h = msg->data; |
Andreas Eversberg | 5977db0 | 2013-06-12 09:34:51 +0200 | [diff] [blame] | 1080 | if (link_id & 0x40) { |
| 1081 | msg->l2h = msgb_push(msg, 2); |
| 1082 | msg->l2h[0] = le->tx_power; |
| 1083 | msg->l2h[1] = le->ta; |
| 1084 | } |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1085 | |
| 1086 | /* Tramsmit */ |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1087 | return tx_ph_data_enqueue(dl, msg, chan_nr, link_id, 23); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /* L3 requests transfer of acknowledged information */ |
| 1091 | static int rslms_rx_rll_data_req(struct msgb *msg, struct lapdm_datalink *dl) |
| 1092 | { |
| 1093 | struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); |
| 1094 | struct tlv_parsed tv; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1095 | int length; |
| 1096 | struct osmo_dlsap_prim dp; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1097 | |
| 1098 | rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); |
| 1099 | if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1100 | LOGDL(&dl->dl, LOGL_ERROR, "data request without message error\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1101 | msgb_free(msg); |
| 1102 | return -EINVAL; |
| 1103 | } |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1104 | msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO); |
| 1105 | length = TLVP_LEN(&tv, RSL_IE_L3_INFO); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1106 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1107 | /* Remove RLL header from msgb and set length to L3-info */ |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 1108 | msgb_pull_to_l3(msg); |
| 1109 | msgb_trim(msg, length); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1110 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1111 | /* prepare prim */ |
| 1112 | osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1113 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1114 | /* send to L2 */ |
| 1115 | return lapd_recv_dlsap(&dp, &dl->dl.lctx); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | /* L3 requests suspension of data link */ |
| 1119 | static int rslms_rx_rll_susp_req(struct msgb *msg, struct lapdm_datalink *dl) |
| 1120 | { |
| 1121 | struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); |
| 1122 | uint8_t sapi = rllh->link_id & 7; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1123 | struct osmo_dlsap_prim dp; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1124 | |
| 1125 | if (sapi != 0) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1126 | LOGDL(&dl->dl, LOGL_ERROR, "SAPI != 0 while suspending\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1127 | msgb_free(msg); |
| 1128 | return -EINVAL; |
| 1129 | } |
| 1130 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1131 | /* prepare prim */ |
| 1132 | osmo_prim_init(&dp.oph, 0, PRIM_DL_SUSP, PRIM_OP_REQUEST, msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1133 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1134 | /* send to L2 */ |
| 1135 | return lapd_recv_dlsap(&dp, &dl->dl.lctx); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /* L3 requests resume of data link */ |
| 1139 | static int rslms_rx_rll_res_req(struct msgb *msg, struct lapdm_datalink *dl) |
| 1140 | { |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1141 | struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1142 | int msg_type = rllh->c.msg_type; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1143 | uint8_t chan_nr = rllh->chan_nr; |
| 1144 | uint8_t link_id = rllh->link_id; |
| 1145 | uint8_t sapi = rllh->link_id & 7; |
| 1146 | struct tlv_parsed tv; |
| 1147 | uint8_t length; |
Andreas.Eversberg | cbed327 | 2011-11-06 20:43:08 +0100 | [diff] [blame] | 1148 | uint8_t n201 = (rllh->link_id & 0x40) ? N201_AB_SACCH : N201_AB_SDCCH; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1149 | struct osmo_dlsap_prim dp; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1150 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1151 | /* Set LAPDm context for established connection */ |
| 1152 | set_lapdm_context(dl, chan_nr, link_id, n201, sapi); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1153 | |
| 1154 | rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh)); |
| 1155 | if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1156 | LOGDL(&dl->dl, LOGL_ERROR, "resume without message error\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1157 | msgb_free(msg); |
| 1158 | return send_rll_simple(RSL_MT_REL_IND, &dl->mctx); |
| 1159 | } |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1160 | msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1161 | length = TLVP_LEN(&tv, RSL_IE_L3_INFO); |
| 1162 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1163 | /* Remove RLL header from msgb and set length to L3-info */ |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 1164 | msgb_pull_to_l3(msg); |
| 1165 | msgb_trim(msg, length); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1166 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1167 | /* prepare prim */ |
| 1168 | osmo_prim_init(&dp.oph, 0, (msg_type == RSL_MT_RES_REQ) ? PRIM_DL_RES |
| 1169 | : PRIM_DL_RECON, PRIM_OP_REQUEST, msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1170 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1171 | /* send to L2 */ |
| 1172 | return lapd_recv_dlsap(&dp, &dl->dl.lctx); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /* L3 requests release of data link */ |
| 1176 | static int rslms_rx_rll_rel_req(struct msgb *msg, struct lapdm_datalink *dl) |
| 1177 | { |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1178 | struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1179 | uint8_t mode = 0; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1180 | struct osmo_dlsap_prim dp; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1181 | |
| 1182 | /* get release mode */ |
| 1183 | if (rllh->data[0] == RSL_IE_RELEASE_MODE) |
| 1184 | mode = rllh->data[1] & 1; |
| 1185 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1186 | /* Pull rllh */ |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 1187 | msgb_pull_to_l3(msg); |
Harald Welte | 973c3c3 | 2012-04-26 21:50:54 +0200 | [diff] [blame] | 1188 | |
| 1189 | /* 04.06 3.8.3: No information field is permitted with the DISC |
| 1190 | * command. */ |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 1191 | msgb_trim(msg, 0); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1192 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1193 | /* prepare prim */ |
| 1194 | osmo_prim_init(&dp.oph, 0, PRIM_DL_REL, PRIM_OP_REQUEST, msg); |
| 1195 | dp.u.rel_req.mode = mode; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1196 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1197 | /* send to L2 */ |
| 1198 | return lapd_recv_dlsap(&dp, &dl->dl.lctx); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | /* L3 requests channel in idle state */ |
| 1202 | static int rslms_rx_chan_rqd(struct lapdm_channel *lc, struct msgb *msg) |
| 1203 | { |
| 1204 | struct abis_rsl_cchan_hdr *cch = msgb_l2(msg); |
| 1205 | void *l1ctx = lc->lapdm_dcch.l1_ctx; |
| 1206 | struct osmo_phsap_prim pp; |
| 1207 | |
| 1208 | osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_RACH, |
| 1209 | PRIM_OP_REQUEST, NULL); |
| 1210 | |
| 1211 | if (msgb_l2len(msg) < sizeof(*cch) + 4 + 2 + 2) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1212 | LOGP(DLLAPD, LOGL_ERROR, "Message too short for CHAN RQD!\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1213 | return -EINVAL; |
| 1214 | } |
| 1215 | if (cch->data[0] != RSL_IE_REQ_REFERENCE) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1216 | LOGP(DLLAPD, LOGL_ERROR, "Missing REQ REFERENCE IE\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1217 | return -EINVAL; |
| 1218 | } |
| 1219 | pp.u.rach_req.ra = cch->data[1]; |
| 1220 | pp.u.rach_req.offset = ((cch->data[2] & 0x7f) << 8) | cch->data[3]; |
| 1221 | pp.u.rach_req.is_combined_ccch = cch->data[2] >> 7; |
| 1222 | |
| 1223 | if (cch->data[4] != RSL_IE_ACCESS_DELAY) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1224 | LOGP(DLLAPD, LOGL_ERROR, "Missing ACCESS_DELAY IE\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1225 | return -EINVAL; |
| 1226 | } |
| 1227 | /* TA = 0 - delay */ |
| 1228 | pp.u.rach_req.ta = 0 - cch->data[5]; |
| 1229 | |
| 1230 | if (cch->data[6] != RSL_IE_MS_POWER) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1231 | LOGP(DLLAPD, LOGL_ERROR, "Missing MS POWER IE\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1232 | return -EINVAL; |
| 1233 | } |
| 1234 | pp.u.rach_req.tx_power = cch->data[7]; |
| 1235 | |
| 1236 | msgb_free(msg); |
| 1237 | |
| 1238 | return lc->lapdm_dcch.l1_prim_cb(&pp.oph, l1ctx); |
| 1239 | } |
| 1240 | |
| 1241 | /* L1 confirms channel request */ |
| 1242 | static int l2_ph_chan_conf(struct msgb *msg, struct lapdm_entity *le, uint32_t frame_nr) |
| 1243 | { |
| 1244 | struct abis_rsl_cchan_hdr *ch; |
| 1245 | struct gsm_time tm; |
| 1246 | struct gsm48_req_ref *ref; |
| 1247 | |
| 1248 | gsm_fn2gsmtime(&tm, frame_nr); |
| 1249 | |
Jacob Erlbeck | 8dac415 | 2014-01-28 11:03:11 +0100 | [diff] [blame] | 1250 | msgb_pull_to_l3(msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1251 | msg->l2h = msgb_push(msg, sizeof(*ch) + sizeof(*ref)); |
| 1252 | ch = (struct abis_rsl_cchan_hdr *)msg->l2h; |
| 1253 | rsl_init_cchan_hdr(ch, RSL_MT_CHAN_CONF); |
| 1254 | ch->chan_nr = RSL_CHAN_RACH; |
| 1255 | ch->data[0] = RSL_IE_REQ_REFERENCE; |
| 1256 | ref = (struct gsm48_req_ref *) (ch->data + 1); |
| 1257 | ref->t1 = tm.t1; |
| 1258 | ref->t2 = tm.t2; |
| 1259 | ref->t3_low = tm.t3 & 0x7; |
| 1260 | ref->t3_high = tm.t3 >> 3; |
Pau Espin Pedrol | a99e110 | 2017-12-08 14:30:47 +0100 | [diff] [blame] | 1261 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1262 | return rslms_sendmsg(msg, le); |
| 1263 | } |
| 1264 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1265 | /* incoming RSLms RLL message from L3 */ |
| 1266 | static int rslms_rx_rll(struct msgb *msg, struct lapdm_channel *lc) |
| 1267 | { |
| 1268 | struct abis_rsl_rll_hdr *rllh = msgb_l2(msg); |
| 1269 | int msg_type = rllh->c.msg_type; |
| 1270 | uint8_t sapi = rllh->link_id & 7; |
| 1271 | struct lapdm_entity *le; |
| 1272 | struct lapdm_datalink *dl; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1273 | int rc = 0; |
| 1274 | |
| 1275 | if (msgb_l2len(msg) < sizeof(*rllh)) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1276 | LOGP(DLLAPD, LOGL_ERROR, "Message too short for RLL hdr!\n"); |
Andreas.Eversberg | a42b699 | 2011-11-06 20:31:47 +0100 | [diff] [blame] | 1277 | msgb_free(msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1278 | return -EINVAL; |
| 1279 | } |
| 1280 | |
| 1281 | if (rllh->link_id & 0x40) |
| 1282 | le = &lc->lapdm_acch; |
| 1283 | else |
| 1284 | le = &lc->lapdm_dcch; |
| 1285 | |
Harald Welte | 1a87c1b | 2015-12-14 15:26:07 +0100 | [diff] [blame] | 1286 | /* 4.1.1.5 / 4.1.1.6 / 4.1.1.7 all only exist on MS side, not |
| 1287 | * BTS side */ |
| 1288 | if (le->mode == LAPDM_MODE_BTS) { |
| 1289 | switch (msg_type) { |
| 1290 | case RSL_MT_SUSP_REQ: |
| 1291 | case RSL_MT_RES_REQ: |
| 1292 | case RSL_MT_RECON_REQ: |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1293 | LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' unsupported in BTS side LAPDm\n", |
Harald Welte | 1a87c1b | 2015-12-14 15:26:07 +0100 | [diff] [blame] | 1294 | lc->name, rsl_msg_name(msg_type)); |
| 1295 | msgb_free(msg); |
| 1296 | return -EINVAL; |
| 1297 | break; |
| 1298 | default: |
| 1299 | break; |
| 1300 | } |
| 1301 | } |
| 1302 | |
Holger Hans Peter Freyther | c620604 | 2014-01-23 15:00:55 +0100 | [diff] [blame] | 1303 | /* G.2.1 No action shall be taken on frames containing an unallocated |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1304 | * SAPI. |
| 1305 | */ |
Daniel Willmann | 55405fb | 2014-03-26 13:45:17 +0100 | [diff] [blame] | 1306 | dl = lapdm_datalink_for_sapi(le, sapi); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1307 | if (!dl) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1308 | LOGP(DLLAPD, LOGL_ERROR, "No instance for SAPI %d!\n", sapi); |
Andreas.Eversberg | a42b699 | 2011-11-06 20:31:47 +0100 | [diff] [blame] | 1309 | msgb_free(msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1310 | return -EINVAL; |
| 1311 | } |
| 1312 | |
Daniel Willmann | e523392 | 2012-12-25 23:15:50 +0100 | [diff] [blame] | 1313 | switch (msg_type) { |
Daniel Willmann | e523392 | 2012-12-25 23:15:50 +0100 | [diff] [blame] | 1314 | case RSL_MT_DATA_REQ: |
| 1315 | case RSL_MT_SUSP_REQ: |
| 1316 | case RSL_MT_REL_REQ: |
| 1317 | /* This is triggered in abnormal error conditions where |
| 1318 | * set_lapdm_context() was not called for the channel earlier. */ |
| 1319 | if (!dl->dl.lctx.dl) { |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1320 | LOGP(DLLAPD, LOGL_NOTICE, "(%s) RLL Message '%s' received without LAPDm context. (sapi %d)\n", |
Daniel Willmann | e523392 | 2012-12-25 23:15:50 +0100 | [diff] [blame] | 1321 | lc->name, rsl_msg_name(msg_type), sapi); |
| 1322 | msgb_free(msg); |
| 1323 | return -EINVAL; |
| 1324 | } |
| 1325 | break; |
| 1326 | default: |
Harald Welte | 00b2faf | 2020-05-02 19:56:36 +0200 | [diff] [blame] | 1327 | LOGP(DLLAPD, LOGL_INFO, "(%s) RLL Message '%s' received. (sapi %d)\n", |
Daniel Willmann | e523392 | 2012-12-25 23:15:50 +0100 | [diff] [blame] | 1328 | lc->name, rsl_msg_name(msg_type), sapi); |
| 1329 | } |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1330 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1331 | switch (msg_type) { |
| 1332 | case RSL_MT_UNIT_DATA_REQ: |
| 1333 | rc = rslms_rx_rll_udata_req(msg, dl); |
| 1334 | break; |
| 1335 | case RSL_MT_EST_REQ: |
| 1336 | rc = rslms_rx_rll_est_req(msg, dl); |
| 1337 | break; |
| 1338 | case RSL_MT_DATA_REQ: |
| 1339 | rc = rslms_rx_rll_data_req(msg, dl); |
| 1340 | break; |
| 1341 | case RSL_MT_SUSP_REQ: |
| 1342 | rc = rslms_rx_rll_susp_req(msg, dl); |
| 1343 | break; |
| 1344 | case RSL_MT_RES_REQ: |
| 1345 | rc = rslms_rx_rll_res_req(msg, dl); |
| 1346 | break; |
| 1347 | case RSL_MT_RECON_REQ: |
| 1348 | rc = rslms_rx_rll_res_req(msg, dl); |
| 1349 | break; |
| 1350 | case RSL_MT_REL_REQ: |
| 1351 | rc = rslms_rx_rll_rel_req(msg, dl); |
| 1352 | break; |
| 1353 | default: |
| 1354 | LOGP(DLLAPD, LOGL_NOTICE, "Message unsupported.\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1355 | msgb_free(msg); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1356 | rc = -EINVAL; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1357 | } |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1358 | |
| 1359 | return rc; |
| 1360 | } |
| 1361 | |
| 1362 | /* incoming RSLms COMMON CHANNEL message from L3 */ |
| 1363 | static int rslms_rx_com_chan(struct msgb *msg, struct lapdm_channel *lc) |
| 1364 | { |
| 1365 | struct abis_rsl_cchan_hdr *cch = msgb_l2(msg); |
| 1366 | int msg_type = cch->c.msg_type; |
| 1367 | int rc = 0; |
| 1368 | |
| 1369 | if (msgb_l2len(msg) < sizeof(*cch)) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1370 | LOGP(DLLAPD, LOGL_ERROR, "Message too short for COM CHAN hdr!\n"); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1371 | return -EINVAL; |
| 1372 | } |
| 1373 | |
| 1374 | switch (msg_type) { |
| 1375 | case RSL_MT_CHAN_RQD: |
| 1376 | /* create and send RACH request */ |
| 1377 | rc = rslms_rx_chan_rqd(lc, msg); |
| 1378 | break; |
| 1379 | default: |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1380 | LOGP(DLLAPD, LOGL_NOTICE, "Unknown COMMON CHANNEL msg %d!\n", |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1381 | msg_type); |
| 1382 | msgb_free(msg); |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | return rc; |
| 1387 | } |
| 1388 | |
Harald Welte | 7023aa0 | 2019-05-19 12:17:06 +0200 | [diff] [blame] | 1389 | /*! Receive a RSLms \ref msgb from Layer 3. 'msg' ownership is transferred, |
| 1390 | * i.e. caller must not free it */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1391 | int lapdm_rslms_recvmsg(struct msgb *msg, struct lapdm_channel *lc) |
| 1392 | { |
| 1393 | struct abis_rsl_common_hdr *rslh = msgb_l2(msg); |
| 1394 | int rc = 0; |
| 1395 | |
| 1396 | if (msgb_l2len(msg) < sizeof(*rslh)) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1397 | LOGP(DLLAPD, LOGL_ERROR, "Message too short RSL hdr!\n"); |
Harald Welte | 7023aa0 | 2019-05-19 12:17:06 +0200 | [diff] [blame] | 1398 | msgb_free(msg); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1399 | return -EINVAL; |
| 1400 | } |
| 1401 | |
| 1402 | switch (rslh->msg_discr & 0xfe) { |
| 1403 | case ABIS_RSL_MDISC_RLL: |
| 1404 | rc = rslms_rx_rll(msg, lc); |
| 1405 | break; |
| 1406 | case ABIS_RSL_MDISC_COM_CHAN: |
| 1407 | rc = rslms_rx_com_chan(msg, lc); |
| 1408 | break; |
| 1409 | default: |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1410 | LOGP(DLLAPD, LOGL_ERROR, "unknown RSLms message " |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1411 | "discriminator 0x%02x", rslh->msg_discr); |
| 1412 | msgb_free(msg); |
| 1413 | return -EINVAL; |
| 1414 | } |
| 1415 | |
| 1416 | return rc; |
| 1417 | } |
| 1418 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1419 | /*! Set the \ref lapdm_mode of a LAPDm entity */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1420 | int lapdm_entity_set_mode(struct lapdm_entity *le, enum lapdm_mode mode) |
| 1421 | { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1422 | int i; |
| 1423 | enum lapd_mode lm; |
| 1424 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1425 | switch (mode) { |
| 1426 | case LAPDM_MODE_MS: |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1427 | lm = LAPD_MODE_USER; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1428 | break; |
| 1429 | case LAPDM_MODE_BTS: |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1430 | lm = LAPD_MODE_NETWORK; |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1431 | break; |
| 1432 | default: |
| 1433 | return -EINVAL; |
| 1434 | } |
| 1435 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1436 | for (i = 0; i < ARRAY_SIZE(le->datalink); i++) { |
| 1437 | lapd_set_mode(&le->datalink[i].dl, lm); |
| 1438 | } |
| 1439 | |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1440 | le->mode = mode; |
| 1441 | |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1445 | /*! Set the \ref lapdm_mode of a LAPDm channel*/ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1446 | int lapdm_channel_set_mode(struct lapdm_channel *lc, enum lapdm_mode mode) |
| 1447 | { |
| 1448 | int rc; |
| 1449 | |
| 1450 | rc = lapdm_entity_set_mode(&lc->lapdm_dcch, mode); |
| 1451 | if (rc < 0) |
| 1452 | return rc; |
| 1453 | |
| 1454 | return lapdm_entity_set_mode(&lc->lapdm_acch, mode); |
| 1455 | } |
| 1456 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1457 | /*! Set the L1 callback and context of a LAPDm channel */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1458 | void lapdm_channel_set_l1(struct lapdm_channel *lc, osmo_prim_cb cb, void *ctx) |
| 1459 | { |
| 1460 | lc->lapdm_dcch.l1_prim_cb = cb; |
| 1461 | lc->lapdm_acch.l1_prim_cb = cb; |
| 1462 | lc->lapdm_dcch.l1_ctx = ctx; |
| 1463 | lc->lapdm_acch.l1_ctx = ctx; |
| 1464 | } |
| 1465 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1466 | /*! Set the L3 callback and context of a LAPDm channel */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1467 | void lapdm_channel_set_l3(struct lapdm_channel *lc, lapdm_cb_t cb, void *ctx) |
| 1468 | { |
| 1469 | lc->lapdm_dcch.l3_cb = cb; |
| 1470 | lc->lapdm_acch.l3_cb = cb; |
| 1471 | lc->lapdm_dcch.l3_ctx = ctx; |
| 1472 | lc->lapdm_acch.l3_ctx = ctx; |
| 1473 | } |
| 1474 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1475 | /*! Reset an entire LAPDm entity and all its datalinks */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1476 | void lapdm_entity_reset(struct lapdm_entity *le) |
| 1477 | { |
| 1478 | struct lapdm_datalink *dl; |
| 1479 | int i; |
| 1480 | |
| 1481 | for (i = 0; i < ARRAY_SIZE(le->datalink); i++) { |
| 1482 | dl = &le->datalink[i]; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1483 | lapd_dl_reset(&dl->dl); |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1487 | /*! Reset a LAPDm channel with all its entities */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1488 | void lapdm_channel_reset(struct lapdm_channel *lc) |
| 1489 | { |
| 1490 | lapdm_entity_reset(&lc->lapdm_dcch); |
| 1491 | lapdm_entity_reset(&lc->lapdm_acch); |
| 1492 | } |
| 1493 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1494 | /*! Set the flags of a LAPDm entity */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1495 | void lapdm_entity_set_flags(struct lapdm_entity *le, unsigned int flags) |
| 1496 | { |
| 1497 | le->flags = flags; |
| 1498 | } |
| 1499 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1500 | /*! Set the flags of all LAPDm entities in a LAPDm channel */ |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 1501 | void lapdm_channel_set_flags(struct lapdm_channel *lc, unsigned int flags) |
| 1502 | { |
| 1503 | lapdm_entity_set_flags(&lc->lapdm_dcch, flags); |
| 1504 | lapdm_entity_set_flags(&lc->lapdm_acch, flags); |
| 1505 | } |
Harald Welte | 6bdf0b1 | 2011-08-17 18:22:08 +0200 | [diff] [blame] | 1506 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 1507 | /*! @} */ |