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