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