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