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