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