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