Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface |
| 2 | * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */ |
| 3 | |
| 4 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 5 | * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | #include <time.h> |
| 31 | #include <netinet/in.h> |
| 32 | |
| 33 | #include <openbsc/db.h> |
| 34 | #include <openbsc/msgb.h> |
| 35 | #include <openbsc/tlv.h> |
| 36 | #include <openbsc/debug.h> |
| 37 | #include <openbsc/gsm_data.h> |
| 38 | #include <openbsc/gsm_subscriber.h> |
| 39 | #include <openbsc/gsm_04_11.h> |
| 40 | #include <openbsc/gsm_04_08.h> |
| 41 | #include <openbsc/abis_rsl.h> |
| 42 | #include <openbsc/chan_alloc.h> |
| 43 | #include <openbsc/paging.h> |
| 44 | #include <openbsc/signal.h> |
| 45 | #include <openbsc/trau_frame.h> |
| 46 | #include <openbsc/trau_mux.h> |
| 47 | |
| 48 | #define GSM48_ALLOC_SIZE 1024 |
| 49 | #define GSM48_ALLOC_HEADROOM 128 |
| 50 | |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 51 | #define GSM_MAX_FACILITY 128 |
| 52 | #define GSM_MAX_SSVERSION 128 |
| 53 | #define GSM_MAX_USERUSER 128 |
| 54 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 55 | static const struct tlv_definition rsl_att_tlvdef = { |
| 56 | .def = { |
| 57 | [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV }, |
| 58 | [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV }, |
| 59 | [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV }, |
| 60 | [GSM48_IE_UTC] = { TLV_TYPE_TV }, |
| 61 | [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 }, |
| 62 | [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV }, |
| 63 | |
| 64 | [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV }, |
| 65 | [GSM48_IE_CAUSE] = { TLV_TYPE_TLV }, |
| 66 | [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV }, |
| 67 | [GSM48_IE_ALERT] = { TLV_TYPE_TLV }, |
| 68 | [GSM48_IE_FACILITY] = { TLV_TYPE_TLV }, |
| 69 | [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV }, |
| 70 | [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV }, |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 71 | [GSM48_IE_NOTIFY] = { TLV_TYPE_TV }, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 72 | [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV }, |
| 73 | [GSM48_IE_SIGNAL] = { TLV_TYPE_TV }, |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 74 | [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV }, |
| 75 | [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV }, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 76 | [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV }, |
| 77 | [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV }, |
| 78 | [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV }, |
| 79 | [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV }, |
| 80 | [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV }, |
| 81 | [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV }, |
| 82 | [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV }, |
| 83 | [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV }, |
| 84 | [GSM48_IE_USER_USER] = { TLV_TYPE_TLV }, |
| 85 | [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV }, |
| 86 | [GSM48_IE_MORE_DATA] = { TLV_TYPE_T }, |
| 87 | [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T }, |
| 88 | [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T }, |
| 89 | [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T }, |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 90 | [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T }, |
| 91 | [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T }, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 92 | /* FIXME: more elements */ |
| 93 | }, |
| 94 | }; |
| 95 | |
| 96 | static const char *rr_cause_names[] = { |
| 97 | [GSM48_RR_CAUSE_NORMAL] = "Normal event", |
| 98 | [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified", |
| 99 | [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable", |
| 100 | [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired", |
| 101 | [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path", |
| 102 | [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release", |
| 103 | [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range", |
| 104 | [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable", |
| 105 | [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented", |
| 106 | [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared", |
| 107 | [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message", |
| 108 | [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information", |
| 109 | [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented", |
| 110 | [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state", |
| 111 | [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error", |
| 112 | [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available", |
| 113 | [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified", |
| 114 | }; |
| 115 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 116 | static const char *cc_state_names[] = { |
| 117 | "NULL", |
| 118 | "INITIATED", |
| 119 | "illegal state 2", |
| 120 | "MO_CALL_PROC", |
| 121 | "CALL_DELIVERED", |
| 122 | "illegal state 5", |
| 123 | "CALL_PRESENT", |
| 124 | "CALL_RECEIVED", |
| 125 | "CONNECT_REQUEST", |
| 126 | "MO_TERM_CALL_CONF", |
| 127 | "ACTIVE", |
| 128 | "DISCONNECT_REQ", |
| 129 | "DISCONNECT_IND", |
| 130 | "illegal state 13", |
| 131 | "illegal state 14", |
| 132 | "illegal state 15", |
| 133 | "illegal state 16", |
| 134 | "illegal state 17", |
| 135 | "illegal state 18", |
| 136 | "RELEASE_REQ", |
| 137 | "illegal state 20", |
| 138 | "illegal state 21", |
| 139 | "illegal state 22", |
| 140 | "illegal state 23", |
| 141 | "illegal state 24", |
| 142 | "illegal state 25", |
| 143 | "MO_ORIG_MODIFY", |
| 144 | "MO_TERM_MODIFY", |
| 145 | "CONNECT_IND", |
| 146 | "illegal state 29", |
| 147 | "illegal state 30", |
| 148 | "illegal state 31", |
| 149 | }; |
| 150 | |
| 151 | static const char *cc_msg_names[] = { |
| 152 | "unknown 0x00", |
| 153 | "ALERTING", |
| 154 | "CALL_PROC", |
| 155 | "PROGRESS", |
| 156 | "ESTAB", |
| 157 | "SETUP", |
| 158 | "ESTAB_CONF", |
| 159 | "CONNECT", |
| 160 | "CALL_CONF", |
| 161 | "START_CC", |
| 162 | "unknown 0x0a", |
| 163 | "RECALL", |
| 164 | "unknown 0x0c", |
| 165 | "unknown 0x0d", |
| 166 | "EMERG_SETUP", |
| 167 | "CONNECT_ACK", |
| 168 | "USER_INFO", |
| 169 | "unknown 0x11", |
| 170 | "unknown 0x12", |
| 171 | "MODIFY_REJECT", |
| 172 | "unknown 0x14", |
| 173 | "unknown 0x15", |
| 174 | "unknown 0x16", |
| 175 | "MODIFY", |
| 176 | "HOLD", |
| 177 | "HOLD_ACK", |
| 178 | "HOLD_REJ", |
| 179 | "unknown 0x1b", |
| 180 | "RETR", |
| 181 | "RETR_ACK", |
| 182 | "RETR_REJ", |
| 183 | "MODIFY_COMPL", |
| 184 | "unknown 0x20", |
| 185 | "unknown 0x21", |
| 186 | "unknown 0x22", |
| 187 | "unknown 0x23", |
| 188 | "unknown 0x24", |
| 189 | "DISCONNECT", |
| 190 | "unknown 0x26", |
| 191 | "unknown 0x27", |
| 192 | "unknown 0x28", |
| 193 | "unknown 0x29", |
| 194 | "RELEASE_COMPL", |
| 195 | "unknown 0x2b", |
| 196 | "unknown 0x2c", |
| 197 | "RELEASE", |
| 198 | "unknown 0x2e", |
| 199 | "unknown 0x2f", |
| 200 | "unknown 0x30", |
| 201 | "STOP_DTMF", |
| 202 | "STOP_DTMF_ACK", |
| 203 | "unknown 0x33", |
| 204 | "STATUS_ENQ", |
| 205 | "START_DTMF", |
| 206 | "START_DTMF_ACK", |
| 207 | "START_DTMF_REJ", |
| 208 | "unknown 0x38", |
| 209 | "CONG_CTRL", |
| 210 | "FACILITY", |
| 211 | "unknown 0x3b", |
| 212 | "STATUS", |
| 213 | "unknown 0x3c", |
| 214 | "NOTIFY", |
| 215 | "unknown 0x3f", |
| 216 | }; |
| 217 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 218 | static char strbuf[64]; |
| 219 | |
| 220 | static const char *rr_cause_name(u_int8_t cause) |
| 221 | { |
| 222 | if (cause < ARRAY_SIZE(rr_cause_names) && |
| 223 | rr_cause_names[cause]) |
| 224 | return rr_cause_names[cause]; |
| 225 | |
| 226 | snprintf(strbuf, sizeof(strbuf), "0x%02x", cause); |
| 227 | return strbuf; |
| 228 | } |
| 229 | |
| 230 | static void parse_meas_rep(struct gsm_meas_rep *rep, const u_int8_t *data, |
| 231 | int len) |
| 232 | { |
| 233 | memset(rep, 0, sizeof(*rep)); |
| 234 | |
| 235 | if (data[0] & 0x80) |
| 236 | rep->flags |= MEAS_REP_F_BA1; |
| 237 | if (data[0] & 0x40) |
| 238 | rep->flags |= MEAS_REP_F_DTX; |
| 239 | if (data[1] & 0x40) |
| 240 | rep->flags |= MEAS_REP_F_VALID; |
| 241 | |
| 242 | rep->rxlev_full = data[0] & 0x3f; |
| 243 | rep->rxlev_sub = data[1] & 0x3f; |
| 244 | rep->rxqual_full = (data[3] >> 4) & 0x7; |
| 245 | rep->rxqual_sub = (data[3] >> 1) & 0x7; |
| 246 | rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2); |
| 247 | if (rep->num_cell < 1) |
| 248 | return; |
| 249 | |
| 250 | /* an encoding nightmare in perfection */ |
| 251 | |
| 252 | rep->cell[0].rxlev = data[4] & 0x3f; |
| 253 | rep->cell[0].bcch_freq = data[5] >> 2; |
| 254 | rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5); |
| 255 | if (rep->num_cell < 2) |
| 256 | return; |
| 257 | |
| 258 | rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7); |
| 259 | rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f; |
| 260 | rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4); |
| 261 | if (rep->num_cell < 3) |
| 262 | return; |
| 263 | |
| 264 | rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6); |
| 265 | rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f; |
| 266 | rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3); |
| 267 | if (rep->num_cell < 4) |
| 268 | return; |
| 269 | |
| 270 | rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5); |
| 271 | rep->cell[3].bcch_freq = data[11] & 0x1f; |
| 272 | rep->cell[3].bsic = data[12] >> 2; |
| 273 | if (rep->num_cell < 5) |
| 274 | return; |
| 275 | |
| 276 | rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4); |
| 277 | rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7); |
| 278 | rep->cell[4].bsic = (data[14] >> 1) & 0x3f; |
| 279 | if (rep->num_cell < 6) |
| 280 | return; |
| 281 | |
| 282 | rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3); |
| 283 | rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6); |
| 284 | rep->cell[5].bsic = data[16] & 0x3f; |
| 285 | } |
| 286 | |
| 287 | int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi); |
| 288 | static int gsm48_tx_simple(struct gsm_lchan *lchan, |
| 289 | u_int8_t pdisc, u_int8_t msg_type); |
| 290 | static void schedule_reject(struct gsm_lchan *lchan); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 291 | void free_trans(struct gsm_trans *trans); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 292 | |
| 293 | struct gsm_lai { |
| 294 | u_int16_t mcc; |
| 295 | u_int16_t mnc; |
| 296 | u_int16_t lac; |
| 297 | }; |
| 298 | |
| 299 | static int authorize_everonye = 0; |
| 300 | void gsm0408_allow_everyone(int everyone) |
| 301 | { |
| 302 | printf("Allowing everyone?\n"); |
| 303 | authorize_everonye = everyone; |
| 304 | } |
| 305 | |
| 306 | static int reject_cause = 0; |
| 307 | void gsm0408_set_reject_cause(int cause) |
| 308 | { |
| 309 | reject_cause = cause; |
| 310 | } |
| 311 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 312 | static u_int32_t new_callref = 0x80000001; |
| 313 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 314 | static int authorize_subscriber(struct gsm_loc_updating_operation *loc, |
| 315 | struct gsm_subscriber *subscriber) |
| 316 | { |
| 317 | if (!subscriber) |
| 318 | return 0; |
| 319 | |
| 320 | /* |
| 321 | * Do not send accept yet as more information should arrive. Some |
| 322 | * phones will not send us the information and we will have to check |
| 323 | * what we want to do with that. |
| 324 | */ |
| 325 | if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei)) |
| 326 | return 0; |
| 327 | |
| 328 | if (authorize_everonye) |
| 329 | return 1; |
| 330 | |
| 331 | return subscriber->authorized; |
| 332 | } |
| 333 | |
| 334 | static void release_loc_updating_req(struct gsm_lchan *lchan) |
| 335 | { |
| 336 | if (!lchan->loc_operation) |
| 337 | return; |
| 338 | |
| 339 | bsc_del_timer(&lchan->loc_operation->updating_timer); |
| 340 | free(lchan->loc_operation); |
| 341 | lchan->loc_operation = 0; |
| 342 | put_lchan(lchan); |
| 343 | } |
| 344 | |
| 345 | static void allocate_loc_updating_req(struct gsm_lchan *lchan) |
| 346 | { |
| 347 | use_lchan(lchan); |
| 348 | release_loc_updating_req(lchan); |
| 349 | |
| 350 | lchan->loc_operation = (struct gsm_loc_updating_operation *) |
| 351 | malloc(sizeof(*lchan->loc_operation)); |
| 352 | memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation)); |
| 353 | } |
| 354 | |
| 355 | static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg) |
| 356 | { |
| 357 | u_int32_t tmsi; |
| 358 | |
| 359 | if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) { |
| 360 | db_subscriber_alloc_tmsi(lchan->subscr); |
| 361 | subscr_update(lchan->subscr, msg->trx->bts, GSM_SUBSCRIBER_UPDATE_ATTACHED); |
| 362 | tmsi = strtoul(lchan->subscr->tmsi, NULL, 10); |
| 363 | release_loc_updating_req(lchan); |
| 364 | return gsm0408_loc_upd_acc(msg->lchan, tmsi); |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal, |
| 371 | void *handler_data, void *signal_data) |
| 372 | { |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 373 | struct gsm_trans *trans, *temp; |
| 374 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 375 | if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE) |
| 376 | return 0; |
| 377 | |
| 378 | /* |
| 379 | * Cancel any outstanding location updating request |
| 380 | * operation taking place on the lchan. |
| 381 | */ |
| 382 | struct gsm_lchan *lchan = (struct gsm_lchan *)handler_data; |
| 383 | release_loc_updating_req(lchan); |
| 384 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 385 | /* Free all transactions that are associated with the released lchan */ |
| 386 | llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) { |
| 387 | if (trans->lchan == lchan) |
| 388 | free_trans(trans); |
| 389 | } |
| 390 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * This will be ran by the linker when loading the DSO. We use it to |
| 396 | * do system initialization, e.g. registration of signal handlers. |
| 397 | */ |
| 398 | static __attribute__((constructor)) void on_dso_load_0408(void) |
| 399 | { |
| 400 | register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL); |
| 401 | } |
| 402 | |
| 403 | static void to_bcd(u_int8_t *bcd, u_int16_t val) |
| 404 | { |
| 405 | bcd[2] = val % 10; |
| 406 | val = val / 10; |
| 407 | bcd[1] = val % 10; |
| 408 | val = val / 10; |
| 409 | bcd[0] = val % 10; |
| 410 | val = val / 10; |
| 411 | } |
| 412 | |
| 413 | void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc, |
| 414 | u_int16_t mnc, u_int16_t lac) |
| 415 | { |
| 416 | u_int8_t bcd[3]; |
| 417 | |
| 418 | to_bcd(bcd, mcc); |
| 419 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 420 | lai48->digits[1] = bcd[2]; |
| 421 | |
| 422 | to_bcd(bcd, mnc); |
| 423 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
| 424 | #if 0 |
| 425 | lai48->digits[1] |= bcd[2] << 4; |
| 426 | lai48->digits[2] = bcd[0] | (bcd[1] << 4); |
| 427 | #else |
| 428 | lai48->digits[1] |= 0xf << 4; |
| 429 | lai48->digits[2] = bcd[1] | (bcd[2] << 4); |
| 430 | #endif |
| 431 | |
| 432 | lai48->lac = htons(lac); |
| 433 | } |
| 434 | |
| 435 | #define TMSI_LEN 5 |
| 436 | #define MID_TMSI_LEN (TMSI_LEN + 2) |
| 437 | |
| 438 | int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi) |
| 439 | { |
| 440 | u_int32_t *tptr = (u_int32_t *) &buf[3]; |
| 441 | |
| 442 | buf[0] = GSM48_IE_MOBILE_ID; |
| 443 | buf[1] = TMSI_LEN; |
| 444 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
| 445 | *tptr = htonl(tmsi); |
| 446 | |
| 447 | return 7; |
| 448 | } |
| 449 | |
| 450 | static const char bcd_num_digits[] = { |
| 451 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 452 | '8', '9', '*', '#', 'a', 'b', 'c', '\0' |
| 453 | }; |
| 454 | |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 455 | /* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */ |
| 456 | int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv, |
| 457 | int h_len) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 458 | { |
| 459 | u_int8_t in_len = bcd_lv[0]; |
| 460 | int i; |
| 461 | |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 462 | for (i = 1 + h_len; i <= in_len; i++) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 463 | /* lower nibble */ |
| 464 | output_len--; |
| 465 | if (output_len <= 1) |
| 466 | break; |
| 467 | *output++ = bcd_num_digits[bcd_lv[i] & 0xf]; |
| 468 | |
| 469 | /* higher nibble */ |
| 470 | output_len--; |
| 471 | if (output_len <= 1) |
| 472 | break; |
| 473 | *output++ = bcd_num_digits[bcd_lv[i] >> 4]; |
| 474 | } |
| 475 | if (output_len >= 1) |
| 476 | *output++ = '\0'; |
| 477 | |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 478 | return 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* convert a single ASCII character to call-control BCD */ |
| 482 | static int asc_to_bcd(const char asc) |
| 483 | { |
| 484 | int i; |
| 485 | |
| 486 | for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) { |
| 487 | if (bcd_num_digits[i] == asc) |
| 488 | return i; |
| 489 | } |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 493 | /* convert a ASCII phone number to 'called/calling/connect party BCD number' */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 494 | int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len, |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 495 | int h_len, const char *input) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 496 | { |
| 497 | int in_len = strlen(input); |
| 498 | int i; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 499 | u_int8_t *bcd_cur = bcd_lv + 1 + h_len; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 500 | |
| 501 | /* two digits per byte, plus type byte */ |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 502 | bcd_lv[0] = in_len/2 + h_len; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 503 | if (in_len % 2) |
| 504 | bcd_lv[0]++; |
| 505 | |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 506 | if (bcd_lv[0] > max_len) |
| 507 | return -EIO; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 508 | |
| 509 | for (i = 0; i < in_len; i++) { |
| 510 | int rc = asc_to_bcd(input[i]); |
| 511 | if (rc < 0) |
| 512 | return rc; |
| 513 | if (i % 2 == 0) |
| 514 | *bcd_cur = rc; |
| 515 | else |
| 516 | *bcd_cur++ |= (rc << 4); |
| 517 | } |
| 518 | /* append padding nibble in case of odd length */ |
| 519 | if (i % 2) |
| 520 | *bcd_cur++ |= 0xf0; |
| 521 | |
| 522 | /* return how many bytes we used */ |
| 523 | return (bcd_cur - bcd_lv); |
| 524 | } |
| 525 | |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 526 | /* decode 'bearer capability' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 527 | static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap, |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 528 | const u_int8_t *lv) |
| 529 | { |
| 530 | u_int8_t in_len = lv[0]; |
| 531 | int i, s; |
| 532 | |
| 533 | if (in_len < 1) |
| 534 | return -EINVAL; |
| 535 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 536 | bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */ |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 537 | |
| 538 | /* octet 3 */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 539 | bcap->transfer = lv[1] & 0x07; |
| 540 | bcap->mode = (lv[1] & 0x08) >> 3; |
| 541 | bcap->coding = (lv[1] & 0x10) >> 4; |
| 542 | bcap->radio = (lv[1] & 0x60) >> 5; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 543 | |
| 544 | i = 1; |
| 545 | s = 0; |
| 546 | while(!(lv[i] & 0x80)) { |
| 547 | i++; /* octet 3a etc */ |
| 548 | if (in_len < i) |
| 549 | return 0; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 550 | bcap->speech_ver[s++] = lv[i] & 0x0f; |
| 551 | bcap->speech_ver[s] = -1; /* end of list */ |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 552 | if (i == 2) /* octet 3a */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 553 | bcap->speech_ctm = (lv[i] & 0x20) >> 5; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 554 | if (s == 7) /* maximum speech versions + end of list */ |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | /* encode 'bearer capability' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 562 | static int encode_bearer_cap(struct msgb *msg, int lv_only, |
| 563 | const struct gsm_mncc_bearer_cap *bcap) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 564 | { |
| 565 | u_int8_t lv[32 + 1]; |
| 566 | int i, s; |
| 567 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 568 | lv[1] = bcap->transfer; |
| 569 | lv[1] |= bcap->mode << 3; |
| 570 | lv[1] |= bcap->coding << 4; |
| 571 | lv[1] |= bcap->radio << 5; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 572 | |
| 573 | i = 1; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 574 | for (s = 0; bcap->speech_ver[s] >= 0; s++) { |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 575 | i++; /* octet 3a etc */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 576 | lv[i] = bcap->speech_ver[s]; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 577 | if (i == 2) /* octet 3a */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 578 | lv[i] |= bcap->speech_ctm << 5; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 579 | } |
| 580 | lv[i] |= 0x80; /* last IE of octet 3 etc */ |
| 581 | |
| 582 | lv[0] = i; |
| 583 | if (lv_only) |
| 584 | msgb_lv_put(msg, lv[0], lv+1); |
| 585 | else |
| 586 | msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1); |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | /* decode 'call control cap' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 592 | static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 593 | { |
| 594 | u_int8_t in_len = lv[0]; |
| 595 | |
| 596 | if (in_len < 1) |
| 597 | return -EINVAL; |
| 598 | |
| 599 | /* octet 3 */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 600 | ccap->dtmf = lv[1] & 0x01; |
| 601 | ccap->pcp = (lv[1] & 0x02) >> 1; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | /* decode 'called party BCD number' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 607 | static int decode_called(struct gsm_mncc_number *called, |
| 608 | const u_int8_t *lv) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 609 | { |
| 610 | u_int8_t in_len = lv[0]; |
| 611 | |
| 612 | if (in_len < 1) |
| 613 | return -EINVAL; |
| 614 | |
| 615 | /* octet 3 */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 616 | called->plan = lv[1] & 0x0f; |
| 617 | called->type = (lv[1] & 0x70) >> 4; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 618 | |
| 619 | /* octet 4..N */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 620 | decode_bcd_number(called->number, sizeof(called->number), lv, 1); |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | /* encode 'called party BCD number' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 626 | static int encode_called(struct msgb *msg, |
| 627 | const struct gsm_mncc_number *called) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 628 | { |
| 629 | u_int8_t lv[18]; |
| 630 | int ret; |
| 631 | |
| 632 | /* octet 3 */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 633 | lv[1] = called->plan; |
| 634 | lv[1] |= called->type << 4; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 635 | |
| 636 | /* octet 4..N, octet 2 */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 637 | ret = encode_bcd_number(lv, sizeof(lv), 1, called->number); |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 638 | if (ret < 0) |
| 639 | return ret; |
| 640 | |
| 641 | msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1); |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | /* encode callerid of various IEs */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 647 | static int encode_callerid(struct msgb *msg, int ie, |
| 648 | const struct gsm_mncc_number *callerid) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 649 | { |
| 650 | u_int8_t lv[13]; |
| 651 | int h_len = 1; |
| 652 | int ret; |
| 653 | |
| 654 | /* octet 3 */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 655 | lv[1] = callerid->plan; |
| 656 | lv[1] |= callerid->type << 4; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 657 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 658 | if (callerid->present || callerid->screen) { |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 659 | /* octet 3a */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 660 | lv[2] = callerid->screen; |
| 661 | lv[2] |= callerid->present << 5; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 662 | lv[2] |= 0x80; |
| 663 | h_len++; |
| 664 | } else |
| 665 | lv[1] |= 0x80; |
| 666 | |
| 667 | /* octet 4..N, octet 2 */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 668 | ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number); |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 669 | if (ret < 0) |
| 670 | return ret; |
| 671 | |
| 672 | msgb_tlv_put(msg, ie, lv[0], lv+1); |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* decode 'cause' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 678 | static int decode_cause(struct gsm_mncc_cause *cause, |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 679 | const u_int8_t *lv) |
| 680 | { |
| 681 | u_int8_t in_len = lv[0]; |
| 682 | int i; |
| 683 | |
| 684 | if (in_len < 2) |
| 685 | return -EINVAL; |
| 686 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 687 | cause->diag_len = 0; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 688 | |
| 689 | /* octet 3 */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 690 | cause->location = lv[1] & 0x0f; |
| 691 | cause->coding = (lv[1] & 0x60) >> 5; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 692 | |
| 693 | i = 1; |
| 694 | if (!(lv[i] & 0x80)) { |
| 695 | i++; /* octet 3a */ |
| 696 | if (in_len < i+1) |
| 697 | return 0; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 698 | cause->rec = 1; |
| 699 | cause->rec_val = lv[i] & 0x7f; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 700 | |
| 701 | } |
| 702 | i++; |
| 703 | |
| 704 | /* octet 4 */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 705 | cause->value = lv[i] & 0x7f; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 706 | i++; |
| 707 | |
| 708 | if (in_len < i) /* no diag */ |
| 709 | return 0; |
| 710 | |
| 711 | if (in_len - (i-1) > 32) /* maximum 32 octets */ |
| 712 | return 0; |
| 713 | |
| 714 | /* octet 5-N */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 715 | memcpy(cause->diag, lv + i, in_len - (i-1)); |
| 716 | cause->diag_len = in_len - (i-1); |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | /* encode 'cause' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 722 | static int encode_cause(struct msgb *msg, int lv_only, |
| 723 | const struct gsm_mncc_cause *cause) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 724 | { |
| 725 | u_int8_t lv[32+4]; |
| 726 | int i; |
| 727 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 728 | if (cause->diag_len > 32) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 729 | return -EINVAL; |
| 730 | |
| 731 | /* octet 3 */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 732 | lv[1] = cause->location; |
| 733 | lv[1] |= cause->coding << 5; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 734 | |
| 735 | i = 1; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 736 | if (cause->rec) { |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 737 | i++; /* octet 3a */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 738 | lv[i] = cause->rec_val; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 739 | } |
| 740 | lv[i] |= 0x80; /* end of octet 3 */ |
| 741 | |
| 742 | /* octet 4 */ |
| 743 | i++; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 744 | lv[i] = 0x80 | cause->value; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 745 | |
| 746 | /* octet 5-N */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 747 | if (cause->diag_len) { |
| 748 | memcpy(lv + i, cause->diag, cause->diag_len); |
| 749 | i += cause->diag_len; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | lv[0] = i; |
| 753 | if (lv_only) |
| 754 | msgb_lv_put(msg, lv[0], lv+1); |
| 755 | else |
| 756 | msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1); |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | /* encode 'calling number' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 762 | static int encode_calling(struct msgb *msg, |
| 763 | const struct gsm_mncc_number *calling) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 764 | { |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 765 | return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling); |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /* encode 'connected number' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 769 | static int encode_connected(struct msgb *msg, |
| 770 | const struct gsm_mncc_number *connected) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 771 | { |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 772 | return encode_callerid(msg, GSM48_IE_CONN_BCD, connected); |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* encode 'redirecting number' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 776 | static int encode_redirecting(struct msgb *msg, |
| 777 | const struct gsm_mncc_number *redirecting) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 778 | { |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 779 | return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting); |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* decode 'facility' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 783 | static int decode_facility(struct gsm_mncc_facility *facility, |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 784 | const u_int8_t *lv) |
| 785 | { |
| 786 | u_int8_t in_len = lv[0]; |
| 787 | |
| 788 | if (in_len < 1) |
| 789 | return -EINVAL; |
| 790 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 791 | if (in_len > sizeof(facility->info)) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 792 | return -EINVAL; |
| 793 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 794 | memcpy(facility->info, lv+1, in_len); |
| 795 | facility->len = in_len; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | /* encode 'facility' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 801 | static int encode_facility(struct msgb *msg, int lv_only, |
| 802 | const struct gsm_mncc_facility *facility) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 803 | { |
| 804 | u_int8_t lv[GSM_MAX_FACILITY + 1]; |
| 805 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 806 | if (facility->len < 1 || facility->len > GSM_MAX_FACILITY) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 807 | return -EINVAL; |
| 808 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 809 | memcpy(lv+1, facility->info, facility->len); |
| 810 | lv[0] = facility->len; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 811 | if (lv_only) |
| 812 | msgb_lv_put(msg, lv[0], lv+1); |
| 813 | else |
| 814 | msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1); |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | /* decode 'notify' */ |
| 820 | static int decode_notify(int *notify, const u_int8_t *v) |
| 821 | { |
| 822 | *notify = v[0] & 0x7f; |
| 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | /* encode 'notify' */ |
| 828 | static int encode_notify(struct msgb *msg, int notify) |
| 829 | { |
| 830 | msgb_v_put(msg, notify | 0x80); |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | /* encode 'signal' */ |
| 836 | static int encode_signal(struct msgb *msg, int signal) |
| 837 | { |
| 838 | msgb_tv_put(msg, GSM48_IE_SIGNAL, signal); |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | /* decode 'keypad' */ |
| 844 | static int decode_keypad(int *keypad, const u_int8_t *lv) |
| 845 | { |
| 846 | u_int8_t in_len = lv[0]; |
| 847 | |
| 848 | if (in_len < 1) |
| 849 | return -EINVAL; |
| 850 | |
| 851 | *keypad = lv[1] & 0x7f; |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | /* encode 'keypad' */ |
| 857 | static int encode_keypad(struct msgb *msg, int keypad) |
| 858 | { |
| 859 | msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad); |
| 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | /* decode 'progress' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 865 | static int decode_progress(struct gsm_mncc_progress *progress, |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 866 | const u_int8_t *lv) |
| 867 | { |
| 868 | u_int8_t in_len = lv[0]; |
| 869 | |
| 870 | if (in_len < 2) |
| 871 | return -EINVAL; |
| 872 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 873 | progress->coding = (lv[1] & 0x60) >> 5; |
| 874 | progress->location = lv[1] & 0x0f; |
| 875 | progress->descr = lv[2] & 0x7f; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | /* encode 'progress' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 881 | static int encode_progress(struct msgb *msg, int lv_only, |
| 882 | const struct gsm_mncc_progress *p) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 883 | { |
| 884 | u_int8_t lv[3]; |
| 885 | |
| 886 | lv[0] = 2; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 887 | lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf); |
| 888 | lv[2] = 0x80 | (p->descr & 0x7f); |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 889 | if (lv_only) |
| 890 | msgb_lv_put(msg, lv[0], lv+1); |
| 891 | else |
| 892 | msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1); |
| 893 | |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | /* decode 'user-user' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 898 | static int decode_useruser(struct gsm_mncc_useruser *uu, |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 899 | const u_int8_t *lv) |
| 900 | { |
| 901 | u_int8_t in_len = lv[0]; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 902 | char *info = uu->info; |
| 903 | int info_len = sizeof(uu->info); |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 904 | int i; |
| 905 | |
| 906 | if (in_len < 1) |
| 907 | return -EINVAL; |
| 908 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 909 | uu->proto = lv[1]; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 910 | |
| 911 | for (i = 2; i <= in_len; i++) { |
| 912 | info_len--; |
| 913 | if (info_len <= 1) |
| 914 | break; |
| 915 | *info++ = lv[i]; |
| 916 | } |
| 917 | if (info_len >= 1) |
| 918 | *info++ = '\0'; |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /* encode 'useruser' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 924 | static int encode_useruser(struct msgb *msg, int lv_only, |
| 925 | const struct gsm_mncc_useruser *uu) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 926 | { |
| 927 | u_int8_t lv[GSM_MAX_USERUSER + 2]; |
| 928 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 929 | if (strlen(uu->info) > GSM_MAX_USERUSER) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 930 | return -EINVAL; |
| 931 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 932 | lv[0] = 1 + strlen(uu->info); |
| 933 | lv[1] = uu->proto; |
| 934 | memcpy(lv + 2, uu->info, strlen(uu->info)); |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 935 | if (lv_only) |
| 936 | msgb_lv_put(msg, lv[0], lv+1); |
| 937 | else |
| 938 | msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1); |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | /* decode 'ss version' */ |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 944 | static int decode_ssversion(struct gsm_mncc_ssversion *ssv, |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 945 | const u_int8_t *lv) |
| 946 | { |
| 947 | u_int8_t in_len = lv[0]; |
| 948 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 949 | if (in_len < 1 || in_len < sizeof(ssv->info)) |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 950 | return -EINVAL; |
| 951 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 952 | memcpy(ssv->info, lv + 1, in_len); |
| 953 | ssv->len = in_len; |
Harald Welte | b8a18b5 | 2009-06-10 12:08:54 +0800 | [diff] [blame] | 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | /* encode 'more data' */ |
| 959 | static int encode_more(struct msgb *msg) |
| 960 | { |
| 961 | u_int8_t *ie; |
| 962 | |
| 963 | ie = msgb_put(msg, 1); |
| 964 | ie[0] = GSM48_IE_MORE_DATA; |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 969 | struct msgb *gsm48_msgb_alloc(void) |
| 970 | { |
| 971 | return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM); |
| 972 | } |
| 973 | |
| 974 | int gsm48_sendmsg(struct msgb *msg) |
| 975 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 976 | if (msg->lchan) { |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 977 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 978 | msg->trx = msg->lchan->ts->trx; |
| 979 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 980 | if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) |
| 981 | DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) " |
| 982 | "Sending '%s' to MS.\n", msg->trx->bts->nr, |
| 983 | msg->trx->nr, msg->lchan->ts->nr, |
| 984 | gh->proto_discr & 0xf0, |
| 985 | cc_msg_names[gh->msg_type & 0x3f]); |
| 986 | else |
| 987 | DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) " |
| 988 | "Sending 0x%02x to MS.\n", msg->trx->bts->nr, |
| 989 | msg->trx->nr, msg->lchan->ts->nr, |
| 990 | gh->proto_discr, gh->msg_type); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | msg->l3h = msg->data; |
| 994 | |
| 995 | return rsl_data_request(msg, 0); |
| 996 | } |
| 997 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 998 | /* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */ |
| 999 | int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause) |
| 1000 | { |
| 1001 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1002 | struct gsm48_hdr *gh; |
| 1003 | |
| 1004 | msg->lchan = lchan; |
| 1005 | |
| 1006 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 1007 | gh->proto_discr = GSM48_PDISC_MM; |
| 1008 | gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT; |
| 1009 | gh->data[0] = cause; |
| 1010 | |
| 1011 | DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr); |
| 1012 | |
| 1013 | return gsm48_sendmsg(msg); |
| 1014 | } |
| 1015 | |
| 1016 | /* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */ |
| 1017 | int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi) |
| 1018 | { |
| 1019 | struct gsm_bts *bts = lchan->ts->trx->bts; |
| 1020 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1021 | struct gsm48_hdr *gh; |
| 1022 | struct gsm48_loc_area_id *lai; |
| 1023 | u_int8_t *mid; |
| 1024 | int ret; |
| 1025 | |
| 1026 | msg->lchan = lchan; |
| 1027 | |
| 1028 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1029 | gh->proto_discr = GSM48_PDISC_MM; |
| 1030 | gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT; |
| 1031 | |
| 1032 | lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai)); |
| 1033 | gsm0408_generate_lai(lai, bts->network->country_code, |
| 1034 | bts->network->network_code, bts->location_area_code); |
| 1035 | |
| 1036 | mid = msgb_put(msg, MID_TMSI_LEN); |
| 1037 | generate_mid_from_tmsi(mid, tmsi); |
| 1038 | |
| 1039 | DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n"); |
| 1040 | |
| 1041 | ret = gsm48_sendmsg(msg); |
| 1042 | |
| 1043 | ret = gsm48_tx_mm_info(lchan); |
| 1044 | |
| 1045 | return ret; |
| 1046 | } |
| 1047 | |
| 1048 | static char bcd2char(u_int8_t bcd) |
| 1049 | { |
| 1050 | if (bcd < 0xa) |
| 1051 | return '0' + bcd; |
| 1052 | else |
| 1053 | return 'A' + (bcd - 0xa); |
| 1054 | } |
| 1055 | |
| 1056 | /* Convert Mobile Identity (10.5.1.4) to string */ |
| 1057 | static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len) |
| 1058 | { |
| 1059 | int i; |
| 1060 | u_int8_t mi_type; |
| 1061 | char *str_cur = string; |
| 1062 | u_int32_t tmsi; |
| 1063 | |
| 1064 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 1065 | |
| 1066 | switch (mi_type) { |
| 1067 | case GSM_MI_TYPE_NONE: |
| 1068 | break; |
| 1069 | case GSM_MI_TYPE_TMSI: |
| 1070 | /* Table 10.5.4.3, reverse generate_mid_from_tmsi */ |
| 1071 | if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) { |
| 1072 | memcpy(&tmsi, &mi[1], 4); |
| 1073 | tmsi = ntohl(tmsi); |
| 1074 | return snprintf(string, str_len, "%u", tmsi); |
| 1075 | } |
| 1076 | break; |
| 1077 | case GSM_MI_TYPE_IMSI: |
| 1078 | case GSM_MI_TYPE_IMEI: |
| 1079 | case GSM_MI_TYPE_IMEISV: |
| 1080 | *str_cur++ = bcd2char(mi[0] >> 4); |
| 1081 | |
| 1082 | for (i = 1; i < mi_len; i++) { |
| 1083 | if (str_cur + 2 >= string + str_len) |
| 1084 | return str_cur - string; |
| 1085 | *str_cur++ = bcd2char(mi[i] & 0xf); |
| 1086 | /* skip last nibble in last input byte when GSM_EVEN */ |
| 1087 | if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD)) |
| 1088 | *str_cur++ = bcd2char(mi[i] >> 4); |
| 1089 | } |
| 1090 | break; |
| 1091 | default: |
| 1092 | break; |
| 1093 | } |
| 1094 | *str_cur++ = '\0'; |
| 1095 | |
| 1096 | return str_cur - string; |
| 1097 | } |
| 1098 | |
| 1099 | /* Transmit Chapter 9.2.10 Identity Request */ |
| 1100 | static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type) |
| 1101 | { |
| 1102 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1103 | struct gsm48_hdr *gh; |
| 1104 | |
| 1105 | msg->lchan = lchan; |
| 1106 | |
| 1107 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 1108 | gh->proto_discr = GSM48_PDISC_MM; |
| 1109 | gh->msg_type = GSM48_MT_MM_ID_REQ; |
| 1110 | gh->data[0] = id_type; |
| 1111 | |
| 1112 | return gsm48_sendmsg(msg); |
| 1113 | } |
| 1114 | |
| 1115 | #define MI_SIZE 32 |
| 1116 | |
| 1117 | /* Parse Chapter 9.2.11 Identity Response */ |
| 1118 | static int mm_rx_id_resp(struct msgb *msg) |
| 1119 | { |
| 1120 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1121 | struct gsm_lchan *lchan = msg->lchan; |
| 1122 | u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK; |
| 1123 | char mi_string[MI_SIZE]; |
| 1124 | |
| 1125 | mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]); |
| 1126 | DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n", |
| 1127 | mi_type, mi_string); |
| 1128 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1129 | switch (mi_type) { |
| 1130 | case GSM_MI_TYPE_IMSI: |
| 1131 | if (!lchan->subscr) |
| 1132 | lchan->subscr = db_create_subscriber(mi_string); |
| 1133 | if (lchan->loc_operation) |
| 1134 | lchan->loc_operation->waiting_for_imsi = 0; |
| 1135 | break; |
| 1136 | case GSM_MI_TYPE_IMEI: |
| 1137 | case GSM_MI_TYPE_IMEISV: |
| 1138 | /* update subscribe <-> IMEI mapping */ |
| 1139 | if (lchan->subscr) |
| 1140 | db_subscriber_assoc_imei(lchan->subscr, mi_string); |
| 1141 | if (lchan->loc_operation) |
| 1142 | lchan->loc_operation->waiting_for_imei = 0; |
| 1143 | break; |
| 1144 | } |
| 1145 | |
| 1146 | /* Check if we can let the mobile station enter */ |
| 1147 | return gsm0408_authorize(lchan, msg); |
| 1148 | } |
| 1149 | |
| 1150 | |
| 1151 | static void loc_upd_rej_cb(void *data) |
| 1152 | { |
| 1153 | struct gsm_lchan *lchan = data; |
| 1154 | |
| 1155 | release_loc_updating_req(lchan); |
| 1156 | gsm0408_loc_upd_rej(lchan, reject_cause); |
| 1157 | lchan_auto_release(lchan); |
| 1158 | } |
| 1159 | |
| 1160 | static void schedule_reject(struct gsm_lchan *lchan) |
| 1161 | { |
| 1162 | lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb; |
| 1163 | lchan->loc_operation->updating_timer.data = lchan; |
| 1164 | bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0); |
| 1165 | } |
| 1166 | |
| 1167 | static const char *lupd_name(u_int8_t type) |
| 1168 | { |
| 1169 | switch (type) { |
| 1170 | case GSM48_LUPD_NORMAL: |
| 1171 | return "NORMAL"; |
| 1172 | case GSM48_LUPD_PERIODIC: |
| 1173 | return "PEROIDOC"; |
| 1174 | case GSM48_LUPD_IMSI_ATT: |
| 1175 | return "IMSI ATTACH"; |
| 1176 | default: |
| 1177 | return "UNKNOWN"; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | #define MI_SIZE 32 |
| 1182 | /* Chapter 9.2.15: Receive Location Updating Request */ |
| 1183 | static int mm_rx_loc_upd_req(struct msgb *msg) |
| 1184 | { |
| 1185 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1186 | struct gsm48_loc_upd_req *lu; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1187 | struct gsm_subscriber *subscr = NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1188 | struct gsm_lchan *lchan = msg->lchan; |
| 1189 | u_int8_t mi_type; |
| 1190 | char mi_string[MI_SIZE]; |
| 1191 | int rc; |
| 1192 | |
| 1193 | lu = (struct gsm48_loc_upd_req *) gh->data; |
| 1194 | |
| 1195 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
| 1196 | |
| 1197 | mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
| 1198 | |
| 1199 | DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s) type=%s\n", mi_type, mi_string, |
| 1200 | lupd_name(lu->type)); |
| 1201 | |
| 1202 | /* |
| 1203 | * Pseudo Spoof detection: Just drop a second/concurrent |
| 1204 | * location updating request. |
| 1205 | */ |
| 1206 | if (lchan->loc_operation) { |
| 1207 | DEBUGP(DMM, "LUPDREQ: ignoring request due an existing one: %p.\n", |
| 1208 | lchan->loc_operation); |
| 1209 | gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR); |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | allocate_loc_updating_req(lchan); |
| 1214 | |
| 1215 | switch (mi_type) { |
| 1216 | case GSM_MI_TYPE_IMSI: |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1217 | DEBUGP(DMM, "\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1218 | /* we always want the IMEI, too */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1219 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI); |
| 1220 | lchan->loc_operation->waiting_for_imei = 1; |
| 1221 | |
| 1222 | /* look up subscriber based on IMSI */ |
| 1223 | subscr = db_create_subscriber(mi_string); |
| 1224 | break; |
| 1225 | case GSM_MI_TYPE_TMSI: |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1226 | DEBUGP(DMM, "\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1227 | /* we always want the IMEI, too */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1228 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI); |
| 1229 | lchan->loc_operation->waiting_for_imei = 1; |
| 1230 | |
| 1231 | /* look up the subscriber based on TMSI, request IMSI if it fails */ |
| 1232 | subscr = subscr_get_by_tmsi(mi_string); |
| 1233 | if (!subscr) { |
| 1234 | /* send IDENTITY REQUEST message to get IMSI */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1235 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI); |
| 1236 | lchan->loc_operation->waiting_for_imsi = 1; |
| 1237 | } |
| 1238 | break; |
| 1239 | case GSM_MI_TYPE_IMEI: |
| 1240 | case GSM_MI_TYPE_IMEISV: |
| 1241 | /* no sim card... FIXME: what to do ? */ |
| 1242 | DEBUGP(DMM, "unimplemented mobile identity type\n"); |
| 1243 | break; |
| 1244 | default: |
| 1245 | DEBUGP(DMM, "unknown mobile identity type\n"); |
| 1246 | break; |
| 1247 | } |
| 1248 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1249 | if (!subscr) { |
| 1250 | DEBUGP(DRR, "<- Can't find any subscriber for this ID\n"); |
| 1251 | /* FIXME: request id? close channel? */ |
| 1252 | return -EINVAL; |
| 1253 | } |
| 1254 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1255 | lchan->subscr = subscr; |
| 1256 | |
| 1257 | /* |
| 1258 | * Schedule the reject timer and check if we can let the |
| 1259 | * subscriber into our network immediately or if we need to wait |
| 1260 | * for identity responses. |
| 1261 | */ |
| 1262 | schedule_reject(lchan); |
| 1263 | return gsm0408_authorize(lchan, msg); |
| 1264 | } |
| 1265 | |
| 1266 | /* 9.1.5 Channel mode modify */ |
| 1267 | int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode) |
| 1268 | { |
| 1269 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1270 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1271 | struct gsm48_chan_mode_modify *cmm = |
| 1272 | (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm)); |
| 1273 | u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff; |
| 1274 | |
| 1275 | DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode); |
| 1276 | |
| 1277 | lchan->tch_mode = mode; |
| 1278 | msg->lchan = lchan; |
| 1279 | gh->proto_discr = GSM48_PDISC_RR; |
| 1280 | gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF; |
| 1281 | |
| 1282 | /* fill the channel information element, this code |
| 1283 | * should probably be shared with rsl_rx_chan_rqd() */ |
| 1284 | cmm->chan_desc.chan_nr = lchan2chan_nr(lchan); |
| 1285 | cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc; |
| 1286 | cmm->chan_desc.h0.h = 0; |
| 1287 | cmm->chan_desc.h0.arfcn_high = arfcn >> 8; |
| 1288 | cmm->chan_desc.h0.arfcn_low = arfcn & 0xff; |
| 1289 | cmm->mode = mode; |
| 1290 | |
| 1291 | return gsm48_sendmsg(msg); |
| 1292 | } |
| 1293 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1294 | #if 0 |
| 1295 | static u_int8_t to_bcd8(u_int8_t val) |
| 1296 | { |
| 1297 | return ((val / 10) << 4) | (val % 10); |
| 1298 | } |
| 1299 | #endif |
| 1300 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1301 | /* Section 9.2.15a */ |
| 1302 | int gsm48_tx_mm_info(struct gsm_lchan *lchan) |
| 1303 | { |
| 1304 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1305 | struct gsm48_hdr *gh; |
| 1306 | struct gsm_network *net = lchan->ts->trx->bts->network; |
| 1307 | u_int8_t *ptr8; |
| 1308 | u_int16_t *ptr16; |
| 1309 | int name_len; |
| 1310 | int i; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1311 | #if 0 |
| 1312 | time_t cur_t; |
| 1313 | struct tm* cur_time; |
| 1314 | int tz15min; |
| 1315 | #endif |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1316 | |
| 1317 | msg->lchan = lchan; |
| 1318 | |
| 1319 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1320 | gh->proto_discr = GSM48_PDISC_MM; |
| 1321 | gh->msg_type = GSM48_MT_MM_INFO; |
| 1322 | |
| 1323 | if (net->name_long) { |
| 1324 | name_len = strlen(net->name_long); |
| 1325 | /* 10.5.3.5a */ |
| 1326 | ptr8 = msgb_put(msg, 3); |
| 1327 | ptr8[0] = GSM48_IE_NAME_LONG; |
| 1328 | ptr8[1] = name_len*2 +1; |
| 1329 | ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */ |
| 1330 | |
| 1331 | ptr16 = (u_int16_t *) msgb_put(msg, name_len*2); |
| 1332 | for (i = 0; i < name_len; i++) |
| 1333 | ptr16[i] = htons(net->name_long[i]); |
| 1334 | |
| 1335 | /* FIXME: Use Cell Broadcast, not UCS-2, since |
| 1336 | * UCS-2 is only supported by later revisions of the spec */ |
| 1337 | } |
| 1338 | |
| 1339 | if (net->name_short) { |
| 1340 | name_len = strlen(net->name_short); |
| 1341 | /* 10.5.3.5a */ |
| 1342 | ptr8 = (u_int8_t *) msgb_put(msg, 3); |
| 1343 | ptr8[0] = GSM48_IE_NAME_LONG; |
| 1344 | ptr8[1] = name_len*2 + 1; |
| 1345 | ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */ |
| 1346 | |
| 1347 | ptr16 = (u_int16_t *) msgb_put(msg, name_len*2); |
| 1348 | for (i = 0; i < name_len; i++) |
| 1349 | ptr16[i] = htons(net->name_short[i]); |
| 1350 | } |
| 1351 | |
| 1352 | #if 0 |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1353 | /* Section 10.5.3.9 */ |
| 1354 | cur_t = time(NULL); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1355 | cur_time = gmtime(&cur_t); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1356 | ptr8 = msgb_put(msg, 8); |
| 1357 | ptr8[0] = GSM48_IE_NET_TIME_TZ; |
| 1358 | ptr8[1] = to_bcd8(cur_time->tm_year % 100); |
| 1359 | ptr8[2] = to_bcd8(cur_time->tm_mon); |
| 1360 | ptr8[3] = to_bcd8(cur_time->tm_mday); |
| 1361 | ptr8[4] = to_bcd8(cur_time->tm_hour); |
| 1362 | ptr8[5] = to_bcd8(cur_time->tm_min); |
| 1363 | ptr8[6] = to_bcd8(cur_time->tm_sec); |
| 1364 | /* 02.42: coded as BCD encoded signed value in units of 15 minutes */ |
| 1365 | tz15min = (cur_time->tm_gmtoff)/(60*15); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1366 | ptr8[7] = to_bcd8(tz15min); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1367 | if (tz15min < 0) |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1368 | ptr8[7] |= 0x80; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1369 | #endif |
| 1370 | |
| 1371 | return gsm48_sendmsg(msg); |
| 1372 | } |
| 1373 | |
| 1374 | static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan) |
| 1375 | { |
| 1376 | DEBUGP(DMM, "-> CM SERVICE ACK\n"); |
| 1377 | return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC); |
| 1378 | } |
| 1379 | |
| 1380 | /* 9.2.6 CM service reject */ |
| 1381 | static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan, |
| 1382 | enum gsm48_reject_value value) |
| 1383 | { |
| 1384 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1385 | struct gsm48_hdr *gh; |
| 1386 | |
| 1387 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 1388 | |
| 1389 | msg->lchan = lchan; |
| 1390 | use_lchan(lchan); |
| 1391 | |
| 1392 | gh->proto_discr = GSM48_PDISC_MM; |
| 1393 | gh->msg_type = GSM48_MT_MM_CM_SERV_REJ; |
| 1394 | gh->data[0] = value; |
| 1395 | DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value); |
| 1396 | |
| 1397 | return gsm48_sendmsg(msg); |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | /* |
| 1402 | * Handle CM Service Requests |
| 1403 | * a) Verify that the packet is long enough to contain the information |
| 1404 | * we require otherwsie reject with INCORRECT_MESSAGE |
| 1405 | * b) Try to parse the TMSI. If we do not have one reject |
| 1406 | * c) Check that we know the subscriber with the TMSI otherwise reject |
| 1407 | * with a HLR cause |
| 1408 | * d) Set the subscriber on the gsm_lchan and accept |
| 1409 | */ |
| 1410 | static int gsm48_rx_mm_serv_req(struct msgb *msg) |
| 1411 | { |
| 1412 | u_int8_t mi_type; |
| 1413 | char mi_string[MI_SIZE]; |
| 1414 | |
| 1415 | struct gsm_subscriber *subscr; |
| 1416 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1417 | struct gsm48_service_request *req = |
| 1418 | (struct gsm48_service_request *)gh->data; |
| 1419 | /* unfortunately in Phase1 the classmar2 length is variable */ |
| 1420 | u_int8_t classmark2_len = gh->data[1]; |
| 1421 | u_int8_t *classmark2 = gh->data+2; |
| 1422 | u_int8_t mi_len = *(classmark2 + classmark2_len); |
| 1423 | u_int8_t *mi = (classmark2 + classmark2_len + 1); |
| 1424 | |
| 1425 | DEBUGP(DMM, "<- CM SERVICE REQUEST "); |
| 1426 | if (msg->data_len < sizeof(struct gsm48_service_request*)) { |
| 1427 | DEBUGPC(DMM, "wrong sized message\n"); |
| 1428 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 1429 | GSM48_REJECT_INCORRECT_MESSAGE); |
| 1430 | } |
| 1431 | |
| 1432 | if (msg->data_len < req->mi_len + 6) { |
| 1433 | DEBUGPC(DMM, "does not fit in packet\n"); |
| 1434 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 1435 | GSM48_REJECT_INCORRECT_MESSAGE); |
| 1436 | } |
| 1437 | |
| 1438 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 1439 | if (mi_type != GSM_MI_TYPE_TMSI) { |
| 1440 | DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type); |
| 1441 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 1442 | GSM48_REJECT_INCORRECT_MESSAGE); |
| 1443 | } |
| 1444 | |
| 1445 | mi_to_string(mi_string, sizeof(mi_string), mi, mi_len); |
| 1446 | DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n", |
| 1447 | req->cm_service_type, mi_type, mi_string); |
| 1448 | |
| 1449 | subscr = subscr_get_by_tmsi(mi_string); |
| 1450 | |
| 1451 | /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */ |
| 1452 | if (!subscr) |
| 1453 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 1454 | GSM48_REJECT_IMSI_UNKNOWN_IN_HLR); |
| 1455 | |
| 1456 | if (!msg->lchan->subscr) |
| 1457 | msg->lchan->subscr = subscr; |
| 1458 | else if (msg->lchan->subscr != subscr) { |
| 1459 | DEBUGP(DMM, "<- CM Channel already owned by someone else?\n"); |
| 1460 | subscr_put(subscr); |
| 1461 | } |
| 1462 | |
| 1463 | subscr->classmark2_len = classmark2_len; |
| 1464 | memcpy(subscr->classmark2, classmark2, classmark2_len); |
| 1465 | |
| 1466 | return gsm48_tx_mm_serv_ack(msg->lchan); |
| 1467 | } |
| 1468 | |
| 1469 | static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg) |
| 1470 | { |
| 1471 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1472 | struct gsm48_imsi_detach_ind *idi = |
| 1473 | (struct gsm48_imsi_detach_ind *) gh->data; |
| 1474 | u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK; |
| 1475 | char mi_string[MI_SIZE]; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1476 | struct gsm_subscriber *subscr = NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1477 | |
| 1478 | mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len); |
| 1479 | DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ", |
| 1480 | mi_type, mi_string); |
| 1481 | |
| 1482 | switch (mi_type) { |
| 1483 | case GSM_MI_TYPE_TMSI: |
| 1484 | subscr = subscr_get_by_tmsi(mi_string); |
| 1485 | break; |
| 1486 | case GSM_MI_TYPE_IMSI: |
| 1487 | subscr = subscr_get_by_imsi(mi_string); |
| 1488 | break; |
| 1489 | case GSM_MI_TYPE_IMEI: |
| 1490 | case GSM_MI_TYPE_IMEISV: |
| 1491 | /* no sim card... FIXME: what to do ? */ |
| 1492 | DEBUGPC(DMM, "unimplemented mobile identity type\n"); |
| 1493 | break; |
| 1494 | default: |
| 1495 | DEBUGPC(DMM, "unknown mobile identity type\n"); |
| 1496 | break; |
| 1497 | } |
| 1498 | |
| 1499 | if (subscr) { |
| 1500 | subscr_update(subscr, msg->trx->bts, |
| 1501 | GSM_SUBSCRIBER_UPDATE_DETACHED); |
| 1502 | DEBUGP(DMM, "Subscriber: %s\n", |
| 1503 | subscr->name ? subscr->name : subscr->imsi); |
| 1504 | subscr_put(subscr); |
| 1505 | } else |
| 1506 | DEBUGP(DMM, "Unknown Subscriber ?!?\n"); |
| 1507 | |
| 1508 | put_lchan(msg->lchan); |
| 1509 | |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
| 1513 | static int gsm48_rx_mm_status(struct msgb *msg) |
| 1514 | { |
| 1515 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1516 | |
| 1517 | DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]); |
| 1518 | |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
| 1522 | /* Receive a GSM 04.08 Mobility Management (MM) message */ |
| 1523 | static int gsm0408_rcv_mm(struct msgb *msg) |
| 1524 | { |
| 1525 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1526 | int rc = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1527 | |
| 1528 | switch (gh->msg_type & 0xbf) { |
| 1529 | case GSM48_MT_MM_LOC_UPD_REQUEST: |
| 1530 | DEBUGP(DMM, "LOCATION UPDATING REQUEST\n"); |
| 1531 | rc = mm_rx_loc_upd_req(msg); |
| 1532 | break; |
| 1533 | case GSM48_MT_MM_ID_RESP: |
| 1534 | rc = mm_rx_id_resp(msg); |
| 1535 | break; |
| 1536 | case GSM48_MT_MM_CM_SERV_REQ: |
| 1537 | rc = gsm48_rx_mm_serv_req(msg); |
| 1538 | break; |
| 1539 | case GSM48_MT_MM_STATUS: |
| 1540 | rc = gsm48_rx_mm_status(msg); |
| 1541 | break; |
| 1542 | case GSM48_MT_MM_TMSI_REALL_COMPL: |
| 1543 | DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n", |
| 1544 | msg->lchan->subscr ? |
| 1545 | msg->lchan->subscr->imsi : |
| 1546 | "unknown subscriber"); |
| 1547 | break; |
| 1548 | case GSM48_MT_MM_IMSI_DETACH_IND: |
| 1549 | rc = gsm48_rx_mm_imsi_detach_ind(msg); |
| 1550 | break; |
| 1551 | case GSM48_MT_MM_CM_REEST_REQ: |
| 1552 | DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n"); |
| 1553 | break; |
| 1554 | case GSM48_MT_MM_AUTH_RESP: |
| 1555 | DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n"); |
| 1556 | break; |
| 1557 | default: |
| 1558 | fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n", |
| 1559 | gh->msg_type); |
| 1560 | break; |
| 1561 | } |
| 1562 | |
| 1563 | return rc; |
| 1564 | } |
| 1565 | |
| 1566 | /* Receive a PAGING RESPONSE message from the MS */ |
| 1567 | static int gsm48_rr_rx_pag_resp(struct msgb *msg) |
| 1568 | { |
| 1569 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1570 | u_int8_t *classmark2_lv = gh->data + 1; |
| 1571 | u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv; |
| 1572 | u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK; |
| 1573 | char mi_string[MI_SIZE]; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1574 | struct gsm_subscriber *subscr = NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1575 | struct paging_signal_data sig_data; |
| 1576 | int rc = 0; |
| 1577 | |
| 1578 | mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv); |
| 1579 | DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n", |
| 1580 | mi_type, mi_string); |
| 1581 | switch (mi_type) { |
| 1582 | case GSM_MI_TYPE_TMSI: |
| 1583 | subscr = subscr_get_by_tmsi(mi_string); |
| 1584 | break; |
| 1585 | case GSM_MI_TYPE_IMSI: |
| 1586 | subscr = subscr_get_by_imsi(mi_string); |
| 1587 | break; |
| 1588 | } |
| 1589 | |
| 1590 | if (!subscr) { |
| 1591 | DEBUGP(DRR, "<- Can't find any subscriber for this ID\n"); |
| 1592 | /* FIXME: request id? close channel? */ |
| 1593 | return -EINVAL; |
| 1594 | } |
| 1595 | DEBUGP(DRR, "<- Channel was requested by %s\n", |
| 1596 | subscr->name ? subscr->name : subscr->imsi); |
| 1597 | |
| 1598 | subscr->classmark2_len = *classmark2_lv; |
| 1599 | memcpy(subscr->classmark2, classmark2_lv+1, *classmark2_lv); |
| 1600 | |
| 1601 | if (!msg->lchan->subscr) { |
| 1602 | msg->lchan->subscr = subscr; |
| 1603 | } else if (msg->lchan->subscr != subscr) { |
| 1604 | DEBUGP(DRR, "<- Channel already owned by someone else?\n"); |
| 1605 | subscr_put(subscr); |
| 1606 | return -EINVAL; |
| 1607 | } else { |
| 1608 | DEBUGP(DRR, "<- Channel already owned by us\n"); |
| 1609 | subscr_put(subscr); |
| 1610 | subscr = msg->lchan->subscr; |
| 1611 | } |
| 1612 | |
| 1613 | sig_data.subscr = subscr; |
| 1614 | sig_data.bts = msg->lchan->ts->trx->bts; |
| 1615 | sig_data.lchan = msg->lchan; |
| 1616 | |
| 1617 | dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data); |
Harald Welte | 876312f | 2009-06-10 11:21:55 +0800 | [diff] [blame] | 1618 | |
| 1619 | /* Stop paging on the bts we received the paging response */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1620 | paging_request_stop(msg->trx->bts, subscr, msg->lchan); |
| 1621 | |
| 1622 | /* FIXME: somehow signal the completion of the PAGING to |
| 1623 | * the entity that requested the paging */ |
| 1624 | |
| 1625 | return rc; |
| 1626 | } |
| 1627 | |
| 1628 | static int gsm48_rx_rr_classmark(struct msgb *msg) |
| 1629 | { |
| 1630 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1631 | struct gsm_subscriber *subscr = msg->lchan->subscr; |
| 1632 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 1633 | u_int8_t cm2_len, cm3_len = 0; |
| 1634 | u_int8_t *cm2, *cm3 = NULL; |
| 1635 | |
| 1636 | DEBUGP(DRR, "CLASSMARK CHANGE "); |
| 1637 | |
| 1638 | /* classmark 2 */ |
| 1639 | cm2_len = gh->data[0]; |
| 1640 | cm2 = &gh->data[1]; |
| 1641 | DEBUGPC(DRR, "CM2(len=%u) ", cm2_len); |
| 1642 | |
| 1643 | if (payload_len > cm2_len + 1) { |
| 1644 | /* we must have a classmark3 */ |
| 1645 | if (gh->data[cm2_len+1] != 0x20) { |
| 1646 | DEBUGPC(DRR, "ERR CM3 TAG\n"); |
| 1647 | return -EINVAL; |
| 1648 | } |
| 1649 | if (cm2_len > 3) { |
| 1650 | DEBUGPC(DRR, "CM2 too long!\n"); |
| 1651 | return -EINVAL; |
| 1652 | } |
| 1653 | |
| 1654 | cm3_len = gh->data[cm2_len+2]; |
| 1655 | cm3 = &gh->data[cm2_len+3]; |
| 1656 | if (cm3_len > 14) { |
| 1657 | DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len); |
| 1658 | return -EINVAL; |
| 1659 | } |
| 1660 | DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len); |
| 1661 | } |
| 1662 | if (subscr) { |
| 1663 | subscr->classmark2_len = cm2_len; |
| 1664 | memcpy(subscr->classmark2, cm2, cm2_len); |
| 1665 | if (cm3) { |
| 1666 | subscr->classmark3_len = cm3_len; |
| 1667 | memcpy(subscr->classmark3, cm3, cm3_len); |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | /* FIXME: store the classmark2/3 values with the equipment register */ |
| 1672 | |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
| 1676 | static int gsm48_rx_rr_status(struct msgb *msg) |
| 1677 | { |
| 1678 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1679 | |
| 1680 | DEBUGP(DRR, "STATUS rr_cause = %s\n", |
| 1681 | rr_cause_name(gh->data[0])); |
| 1682 | |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | static int gsm48_rx_rr_meas_rep(struct msgb *msg) |
| 1687 | { |
| 1688 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1689 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 1690 | static struct gsm_meas_rep meas_rep; |
| 1691 | |
| 1692 | DEBUGP(DRR, "MEASUREMENT REPORT "); |
| 1693 | parse_meas_rep(&meas_rep, gh->data, payload_len); |
| 1694 | if (meas_rep.flags & MEAS_REP_F_DTX) |
| 1695 | DEBUGPC(DRR, "DTX "); |
| 1696 | if (meas_rep.flags & MEAS_REP_F_BA1) |
| 1697 | DEBUGPC(DRR, "BA1 "); |
| 1698 | if (!(meas_rep.flags & MEAS_REP_F_VALID)) |
| 1699 | DEBUGPC(DRR, "NOT VALID "); |
| 1700 | else |
| 1701 | DEBUGPC(DRR, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ", |
| 1702 | meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub, |
| 1703 | meas_rep.rxqual_sub); |
| 1704 | |
| 1705 | DEBUGPC(DRR, "NUM_NEIGH=%u\n", meas_rep.num_cell); |
| 1706 | |
| 1707 | /* FIXME: put the results somwhere */ |
| 1708 | |
| 1709 | return 0; |
| 1710 | } |
| 1711 | |
| 1712 | /* Receive a GSM 04.08 Radio Resource (RR) message */ |
| 1713 | static int gsm0408_rcv_rr(struct msgb *msg) |
| 1714 | { |
| 1715 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1716 | int rc = 0; |
| 1717 | |
| 1718 | switch (gh->msg_type) { |
| 1719 | case GSM48_MT_RR_CLSM_CHG: |
| 1720 | rc = gsm48_rx_rr_classmark(msg); |
| 1721 | break; |
| 1722 | case GSM48_MT_RR_GPRS_SUSP_REQ: |
| 1723 | DEBUGP(DRR, "GRPS SUSPEND REQUEST\n"); |
| 1724 | break; |
| 1725 | case GSM48_MT_RR_PAG_RESP: |
| 1726 | rc = gsm48_rr_rx_pag_resp(msg); |
| 1727 | break; |
| 1728 | case GSM48_MT_RR_CHAN_MODE_MODIF_ACK: |
| 1729 | DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n"); |
| 1730 | rc = rsl_chan_mode_modify_req(msg->lchan); |
| 1731 | break; |
| 1732 | case GSM48_MT_RR_STATUS: |
| 1733 | rc = gsm48_rx_rr_status(msg); |
| 1734 | break; |
| 1735 | case GSM48_MT_RR_MEAS_REP: |
| 1736 | rc = gsm48_rx_rr_meas_rep(msg); |
| 1737 | break; |
| 1738 | default: |
| 1739 | fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n", |
| 1740 | gh->msg_type); |
| 1741 | break; |
| 1742 | } |
| 1743 | |
| 1744 | return rc; |
| 1745 | } |
| 1746 | |
| 1747 | /* 7.1.7 and 9.1.7 Channel release*/ |
| 1748 | int gsm48_send_rr_release(struct gsm_lchan *lchan) |
| 1749 | { |
| 1750 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1751 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1752 | u_int8_t *cause; |
| 1753 | |
| 1754 | msg->lchan = lchan; |
| 1755 | gh->proto_discr = GSM48_PDISC_RR; |
| 1756 | gh->msg_type = GSM48_MT_RR_CHAN_REL; |
| 1757 | |
| 1758 | cause = msgb_put(msg, 1); |
| 1759 | cause[0] = GSM48_RR_CAUSE_NORMAL; |
| 1760 | |
| 1761 | DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n", |
| 1762 | lchan->nr, lchan->type); |
| 1763 | |
| 1764 | return gsm48_sendmsg(msg); |
| 1765 | } |
| 1766 | |
| 1767 | /* Call Control */ |
| 1768 | |
| 1769 | /* The entire call control code is written in accordance with Figure 7.10c |
| 1770 | * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE |
| 1771 | * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY |
| 1772 | * it for voice */ |
| 1773 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1774 | static void new_cc_state(struct gsm_trans *trans, int state) |
| 1775 | { |
| 1776 | if (state > 31 || state < 0) |
| 1777 | return; |
| 1778 | |
| 1779 | DEBUGP(DCC, "new state %s -> %s\n", |
| 1780 | cc_state_names[trans->state], cc_state_names[state]); |
| 1781 | |
| 1782 | trans->state = state; |
| 1783 | } |
| 1784 | |
| 1785 | static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1786 | { |
| 1787 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1788 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1789 | u_int8_t *cause, *call_state; |
| 1790 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1791 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 1792 | msg->lchan = trans->lchan; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1793 | gh->msg_type = GSM48_MT_CC_STATUS; |
| 1794 | |
| 1795 | cause = msgb_put(msg, 3); |
| 1796 | cause[0] = 2; |
| 1797 | cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER; |
| 1798 | cause[2] = 0x80 | 30; /* response to status inquiry */ |
| 1799 | |
| 1800 | call_state = msgb_put(msg, 1); |
| 1801 | call_state[0] = 0xc0 | 0x00; |
| 1802 | |
| 1803 | return gsm48_sendmsg(msg); |
| 1804 | } |
| 1805 | |
| 1806 | static int gsm48_tx_simple(struct gsm_lchan *lchan, |
| 1807 | u_int8_t pdisc, u_int8_t msg_type) |
| 1808 | { |
| 1809 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1810 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1811 | |
| 1812 | msg->lchan = lchan; |
| 1813 | |
| 1814 | gh->proto_discr = pdisc; |
| 1815 | gh->msg_type = msg_type; |
| 1816 | |
| 1817 | return gsm48_sendmsg(msg); |
| 1818 | } |
| 1819 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1820 | static void gsm48_stop_cc_timer(struct gsm_trans *trans) |
| 1821 | { |
| 1822 | if (bsc_timer_pending(&trans->cc_timer)) { |
| 1823 | DEBUGP(DCC, "stopping pending timer T%x\n", trans->Tcurrent); |
| 1824 | bsc_del_timer(&trans->cc_timer); |
| 1825 | trans->Tcurrent = 0; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans, |
| 1830 | int msg_type, struct gsm_mncc *mncc) |
| 1831 | { |
| 1832 | struct msgb *msg; |
| 1833 | |
| 1834 | if (trans) |
| 1835 | if (trans->lchan) |
| 1836 | DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) " |
| 1837 | "Sending '%s' to MNCC.\n", |
| 1838 | trans->lchan->ts->trx->bts->nr, |
| 1839 | trans->lchan->ts->trx->nr, |
| 1840 | trans->lchan->ts->nr, trans->transaction_id, |
| 1841 | (trans->subscr)?(trans->subscr->extension):"-", |
| 1842 | get_mncc_name(msg_type)); |
| 1843 | else |
| 1844 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) " |
| 1845 | "Sending '%s' to MNCC.\n", |
| 1846 | (trans->subscr)?(trans->subscr->extension):"-", |
| 1847 | get_mncc_name(msg_type)); |
| 1848 | else |
| 1849 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) " |
| 1850 | "Sending '%s' to MNCC.\n", get_mncc_name(msg_type)); |
| 1851 | |
| 1852 | mncc->msg_type = msg_type; |
| 1853 | |
| 1854 | msg = msgb_alloc(sizeof(struct gsm_mncc)); |
| 1855 | if (!msg) |
| 1856 | return -ENOMEM; |
| 1857 | memcpy(msg->data, mncc, sizeof(struct gsm_mncc)); |
| 1858 | msgb_enqueue(&net->upqueue, msg); |
| 1859 | |
| 1860 | return 0; |
| 1861 | } |
| 1862 | |
| 1863 | int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans, |
| 1864 | u_int32_t callref, int location, int value) |
| 1865 | { |
| 1866 | struct gsm_mncc rel; |
| 1867 | |
| 1868 | memset(&rel, 0, sizeof(&rel)); |
| 1869 | rel.callref = callref; |
Harald Welte | bbc636a | 2009-06-11 14:23:20 +0800 | [diff] [blame] | 1870 | mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, |
| 1871 | GSM48_CC_CAUSE_UNASSIGNED_NR); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1872 | return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel); |
| 1873 | } |
| 1874 | |
| 1875 | void free_trans(struct gsm_trans *trans) |
| 1876 | { |
| 1877 | struct gsm_bts *bts; |
| 1878 | |
| 1879 | gsm48_stop_cc_timer(trans); |
| 1880 | |
| 1881 | /* send release to L4, if callref still exists */ |
| 1882 | if (trans->callref) { |
| 1883 | /* Ressource unavailable */ |
| 1884 | mncc_release_ind(trans->network, trans, trans->callref, 1, 47); |
| 1885 | if (trans->state != GSM_CSTATE_NULL) |
| 1886 | new_cc_state(trans, GSM_CSTATE_NULL); |
| 1887 | } |
| 1888 | |
| 1889 | if (!trans->lchan && trans->subscr && trans->subscr->net) { |
| 1890 | /* Stop paging on all bts' */ |
| 1891 | bts = NULL; |
| 1892 | do { |
| 1893 | bts = gsm_bts_by_lac(trans->subscr->net, |
| 1894 | trans->subscr->lac, bts); |
| 1895 | if (!bts) |
| 1896 | break; |
| 1897 | /* Stop paging */ |
| 1898 | paging_request_stop(bts, trans->subscr, NULL); |
| 1899 | } while (1); |
| 1900 | } |
| 1901 | |
| 1902 | if (trans->lchan) { |
| 1903 | trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref); |
| 1904 | put_lchan(trans->lchan); |
| 1905 | } |
| 1906 | |
| 1907 | if (trans->subscr) |
| 1908 | subscr_put(trans->subscr); |
| 1909 | |
| 1910 | if (trans->state != GSM_CSTATE_NULL) |
| 1911 | new_cc_state(trans, GSM_CSTATE_NULL); |
| 1912 | |
| 1913 | llist_del(&trans->entry); |
| 1914 | |
| 1915 | free(trans); |
| 1916 | } |
| 1917 | |
| 1918 | static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg); |
| 1919 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1920 | /* call-back from paging the B-end of the connection */ |
| 1921 | static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event, |
| 1922 | struct msgb *msg, void *_lchan, void *param) |
| 1923 | { |
| 1924 | struct gsm_lchan *lchan = _lchan; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1925 | struct gsm_subscriber *subscr = param; |
| 1926 | struct gsm_trans *transt, *tmp; |
| 1927 | struct gsm_network *net; |
| 1928 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1929 | if (hooknum != GSM_HOOK_RR_PAGING) |
| 1930 | return -EINVAL; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1931 | |
| 1932 | if (!subscr) |
| 1933 | return -EINVAL; |
| 1934 | net = subscr->net; |
| 1935 | if (!net) { |
| 1936 | DEBUGP(DCC, "Error Network not set!\n"); |
| 1937 | return -EINVAL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1938 | } |
| 1939 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1940 | /* check all tranactions (without lchan) for subscriber */ |
| 1941 | llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) { |
| 1942 | if (transt->subscr != subscr || transt->lchan) |
| 1943 | continue; |
| 1944 | switch (event) { |
| 1945 | case GSM_PAGING_SUCCEEDED: |
| 1946 | if (!lchan) // paranoid |
| 1947 | break; |
| 1948 | DEBUGP(DCC, "Paging subscr %s succeeded!\n", |
| 1949 | subscr->extension); |
| 1950 | /* Assign lchan */ |
| 1951 | if (!transt->lchan) { |
| 1952 | transt->lchan = lchan; |
| 1953 | use_lchan(lchan); |
| 1954 | } |
| 1955 | /* send SETUP request to called party */ |
| 1956 | gsm48_cc_tx_setup(transt, &transt->cc_msg); |
| 1957 | if (is_ipaccess_bts(lchan->ts->trx->bts)) |
| 1958 | rsl_ipacc_bind(lchan); |
| 1959 | break; |
| 1960 | case GSM_PAGING_EXPIRED: |
| 1961 | DEBUGP(DCC, "Paging subscr %s expired!\n", |
| 1962 | subscr->extension); |
| 1963 | /* Temporarily out of order */ |
| 1964 | mncc_release_ind(transt->network, transt, transt->callref, |
| 1965 | 1, 27); |
| 1966 | transt->callref = 0; |
| 1967 | free_trans(transt); |
| 1968 | break; |
| 1969 | } |
| 1970 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1971 | return 0; |
| 1972 | } |
| 1973 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1974 | /* map two ipaccess RTP streams onto each other */ |
| 1975 | static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan) |
| 1976 | { |
| 1977 | struct gsm_bts *bts = lchan->ts->trx->bts; |
| 1978 | struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts; |
| 1979 | struct gsm_bts_trx_ts *ts; |
| 1980 | |
| 1981 | DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n", |
| 1982 | bts->nr, lchan->ts->trx->nr, lchan->ts->nr, |
| 1983 | remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr); |
| 1984 | |
| 1985 | if (bts->type != remote_bts->type) { |
| 1986 | DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n"); |
| 1987 | return -EINVAL; |
| 1988 | } |
| 1989 | |
| 1990 | switch (bts->type) { |
| 1991 | case GSM_BTS_TYPE_NANOBTS_900: |
| 1992 | case GSM_BTS_TYPE_NANOBTS_1800: |
| 1993 | ts = remote_lchan->ts; |
| 1994 | rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port, |
| 1995 | lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc); |
| 1996 | |
| 1997 | ts = lchan->ts; |
| 1998 | rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port, |
| 1999 | remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc); |
| 2000 | break; |
| 2001 | case GSM_BTS_TYPE_BS11: |
| 2002 | trau_mux_map_lchan(lchan, remote_lchan); |
| 2003 | break; |
| 2004 | default: |
| 2005 | DEBUGP(DCC, "Unknown BTS type %u\n", bts->type); |
| 2006 | break; |
| 2007 | } |
| 2008 | |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2012 | static struct gsm_trans *get_trans_ref(struct gsm_network *net, u_int32_t callref) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2013 | { |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2014 | struct gsm_trans *trans; |
| 2015 | llist_for_each_entry(trans, &net->trans_list, entry) { |
| 2016 | if (trans->callref == callref) |
| 2017 | return trans; |
| 2018 | } |
| 2019 | return NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2020 | } |
| 2021 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2022 | /* bridge channels of two transactions */ |
| 2023 | static int tch_bridge(struct gsm_network *net, u_int32_t *refs) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2024 | { |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2025 | struct gsm_trans *trans1 = get_trans_ref(net, refs[0]); |
| 2026 | struct gsm_trans *trans2 = get_trans_ref(net, refs[1]); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2027 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2028 | if (!trans1 || !trans2) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2029 | return -EIO; |
| 2030 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2031 | if (!trans1->lchan || !trans2->lchan) |
| 2032 | return -EIO; |
| 2033 | |
| 2034 | /* through-connect channel */ |
| 2035 | return tch_map(trans1->lchan, trans2->lchan); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2036 | } |
| 2037 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2038 | /* enable receive of channels to upqueue */ |
| 2039 | static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable) |
| 2040 | { |
| 2041 | struct gsm_trans *trans; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2042 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2043 | /* Find callref */ |
| 2044 | trans = get_trans_ref(net, data->callref); |
| 2045 | if (!trans) |
| 2046 | return -EIO; |
| 2047 | if (!trans->lchan) |
| 2048 | return 0; |
| 2049 | |
| 2050 | // todo IPACCESS |
| 2051 | if (enable) |
| 2052 | return trau_recv_lchan(trans->lchan, data->callref); |
| 2053 | return trau_mux_unmap(NULL, data->callref); |
| 2054 | } |
| 2055 | |
| 2056 | /* send a frame to channel */ |
| 2057 | static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame) |
| 2058 | { |
| 2059 | struct gsm_trans *trans; |
| 2060 | |
| 2061 | /* Find callref */ |
| 2062 | trans = get_trans_ref(net, frame->callref); |
| 2063 | if (!trans) |
| 2064 | return -EIO; |
| 2065 | if (!trans->lchan) |
| 2066 | return 0; |
| 2067 | if (trans->lchan->type != GSM_LCHAN_TCH_F && |
| 2068 | trans->lchan->type != GSM_LCHAN_TCH_H) |
| 2069 | return 0; |
| 2070 | |
| 2071 | // todo IPACCESS |
| 2072 | return trau_send_lchan(trans->lchan, |
| 2073 | (struct decoded_trau_frame *)frame->data); |
| 2074 | } |
| 2075 | |
| 2076 | |
| 2077 | static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg) |
| 2078 | { |
| 2079 | DEBUGP(DCC, "-> STATUS ENQ\n"); |
| 2080 | return gsm48_cc_tx_status(trans, msg); |
| 2081 | } |
| 2082 | |
| 2083 | static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg); |
| 2084 | static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg); |
| 2085 | |
| 2086 | static void gsm48_cc_timeout(void *arg) |
| 2087 | { |
| 2088 | struct gsm_trans *trans = arg; |
| 2089 | int disconnect = 0, release = 0; |
Harald Welte | bbc636a | 2009-06-11 14:23:20 +0800 | [diff] [blame] | 2090 | int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER; |
| 2091 | int mo_location = GSM48_CAUSE_LOC_USER; |
| 2092 | int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC; |
| 2093 | int l4_location = GSM48_CAUSE_LOC_PRN_S_LU; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2094 | struct gsm_mncc mo_rel, l4_rel; |
| 2095 | |
| 2096 | memset(&mo_rel, 0, sizeof(struct gsm_mncc)); |
| 2097 | mo_rel.callref = trans->callref; |
| 2098 | memset(&l4_rel, 0, sizeof(struct gsm_mncc)); |
| 2099 | l4_rel.callref = trans->callref; |
| 2100 | |
| 2101 | switch(trans->Tcurrent) { |
| 2102 | case 0x303: |
| 2103 | release = 1; |
Harald Welte | bbc636a | 2009-06-11 14:23:20 +0800 | [diff] [blame] | 2104 | l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2105 | break; |
| 2106 | case 0x310: |
| 2107 | disconnect = 1; |
Harald Welte | bbc636a | 2009-06-11 14:23:20 +0800 | [diff] [blame] | 2108 | l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2109 | break; |
| 2110 | case 0x313: |
| 2111 | disconnect = 1; |
| 2112 | /* unknown, did not find it in the specs */ |
| 2113 | break; |
| 2114 | case 0x301: |
| 2115 | disconnect = 1; |
Harald Welte | bbc636a | 2009-06-11 14:23:20 +0800 | [diff] [blame] | 2116 | l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2117 | break; |
| 2118 | case 0x308: |
| 2119 | if (!trans->T308_second) { |
| 2120 | /* restart T308 a second time */ |
| 2121 | gsm48_cc_tx_release(trans, &trans->cc_msg); |
| 2122 | trans->T308_second = 1; |
| 2123 | break; /* stay in release state */ |
| 2124 | } |
| 2125 | free_trans(trans); |
| 2126 | return; |
| 2127 | // release = 1; |
| 2128 | // l4_cause = 14; |
| 2129 | // break; |
| 2130 | case 0x306: |
| 2131 | release = 1; |
| 2132 | mo_cause = trans->cc_msg.cause.value; |
| 2133 | mo_location = trans->cc_msg.cause.location; |
| 2134 | break; |
| 2135 | case 0x323: |
| 2136 | disconnect = 1; |
| 2137 | break; |
| 2138 | default: |
| 2139 | release = 1; |
| 2140 | } |
| 2141 | |
| 2142 | if (release && trans->callref) { |
| 2143 | /* process release towards layer 4 */ |
| 2144 | mncc_release_ind(trans->network, trans, trans->callref, |
| 2145 | l4_location, l4_cause); |
| 2146 | trans->callref = 0; |
| 2147 | } |
| 2148 | |
| 2149 | if (disconnect && trans->callref) { |
| 2150 | /* process disconnect towards layer 4 */ |
| 2151 | mncc_set_cause(&l4_rel, l4_location, l4_cause); |
| 2152 | mncc_recvmsg(trans->network, trans, MNCC_DISC_IND, &l4_rel); |
| 2153 | } |
| 2154 | |
| 2155 | /* process disconnect towards mobile station */ |
| 2156 | if (disconnect || release) { |
| 2157 | mncc_set_cause(&mo_rel, mo_location, mo_cause); |
| 2158 | mo_rel.cause.diag[0] = ((trans->Tcurrent & 0xf00) >> 8) + '0'; |
| 2159 | mo_rel.cause.diag[1] = ((trans->Tcurrent & 0x0f0) >> 4) + '0'; |
| 2160 | mo_rel.cause.diag[2] = (trans->Tcurrent & 0x00f) + '0'; |
| 2161 | mo_rel.cause.diag_len = 3; |
| 2162 | |
| 2163 | if (disconnect) |
| 2164 | gsm48_cc_tx_disconnect(trans, &mo_rel); |
| 2165 | if (release) |
| 2166 | gsm48_cc_tx_release(trans, &mo_rel); |
| 2167 | } |
| 2168 | |
| 2169 | } |
| 2170 | |
| 2171 | static void gsm48_start_cc_timer(struct gsm_trans *trans, int current, |
| 2172 | int sec, int micro) |
| 2173 | { |
| 2174 | DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec); |
| 2175 | trans->cc_timer.cb = gsm48_cc_timeout; |
| 2176 | trans->cc_timer.data = trans; |
| 2177 | bsc_schedule_timer(&trans->cc_timer, sec, micro); |
| 2178 | trans->Tcurrent = current; |
| 2179 | } |
| 2180 | |
| 2181 | static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg) |
| 2182 | { |
| 2183 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2184 | u_int8_t msg_type = gh->msg_type & 0xbf; |
| 2185 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2186 | struct tlv_parsed tp; |
| 2187 | struct gsm_mncc setup; |
| 2188 | |
| 2189 | memset(&setup, 0, sizeof(struct gsm_mncc)); |
| 2190 | setup.callref = trans->callref; |
| 2191 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2192 | /* emergency setup is identified by msg_type */ |
| 2193 | if (msg_type == GSM48_MT_CC_EMERG_SETUP) |
| 2194 | setup.emergency = 1; |
| 2195 | |
| 2196 | /* use subscriber as calling party number */ |
| 2197 | if (trans->subscr) { |
| 2198 | setup.fields |= MNCC_F_CALLING; |
| 2199 | strncpy(setup.calling.number, trans->subscr->extension, |
| 2200 | sizeof(setup.calling.number)-1); |
| 2201 | } |
| 2202 | /* bearer capability */ |
| 2203 | if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { |
| 2204 | setup.fields |= MNCC_F_BEARER_CAP; |
| 2205 | decode_bearer_cap(&setup.bearer_cap, |
| 2206 | TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1); |
| 2207 | } |
| 2208 | /* facility */ |
| 2209 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2210 | setup.fields |= MNCC_F_FACILITY; |
| 2211 | decode_facility(&setup.facility, |
| 2212 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2213 | } |
| 2214 | /* called party bcd number */ |
| 2215 | if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) { |
| 2216 | setup.fields |= MNCC_F_CALLED; |
| 2217 | decode_called(&setup.called, |
| 2218 | TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1); |
| 2219 | } |
| 2220 | /* user-user */ |
| 2221 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 2222 | setup.fields |= MNCC_F_USERUSER; |
| 2223 | decode_useruser(&setup.useruser, |
| 2224 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 2225 | } |
| 2226 | /* ss-version */ |
| 2227 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2228 | setup.fields |= MNCC_F_SSVERSION; |
| 2229 | decode_ssversion(&setup.ssversion, |
| 2230 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2231 | } |
| 2232 | /* CLIR suppression */ |
| 2233 | if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP)) |
| 2234 | setup.clir.sup = 1; |
| 2235 | /* CLIR invocation */ |
| 2236 | if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC)) |
| 2237 | setup.clir.inv = 1; |
| 2238 | /* cc cap */ |
| 2239 | if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) { |
| 2240 | setup.fields |= MNCC_F_CCCAP; |
| 2241 | decode_cccap(&setup.cccap, |
| 2242 | TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1); |
| 2243 | } |
| 2244 | |
| 2245 | if (is_ipaccess_bts(msg->trx->bts)) |
| 2246 | rsl_ipacc_bind(msg->lchan); |
| 2247 | |
| 2248 | new_cc_state(trans, GSM_CSTATE_INITIATED); |
| 2249 | |
| 2250 | /* indicate setup to MNCC */ |
| 2251 | mncc_recvmsg(trans->network, trans, MNCC_SETUP_IND, &setup); |
| 2252 | |
| 2253 | return 0; |
| 2254 | } |
| 2255 | |
| 2256 | static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2257 | { |
| 2258 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2259 | struct gsm48_hdr *gh; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2260 | struct gsm_mncc *setup = arg; |
| 2261 | struct gsm_trans *transt; |
| 2262 | u_int16_t trans_id_mask = 0; |
| 2263 | int rc, i; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2264 | |
| 2265 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2266 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2267 | /* transaction id must not be assigned */ |
| 2268 | if (trans->transaction_id != 0xff) { /* unasssigned */ |
| 2269 | DEBUGP(DCC, "TX Setup with assigned transaction. " |
| 2270 | "This is not allowed!\n"); |
| 2271 | /* Temporarily out of order */ |
| 2272 | rc = mncc_release_ind(trans->network, trans, trans->callref, |
| 2273 | 1, 47); |
| 2274 | trans->callref = 0; |
| 2275 | free_trans(trans); |
| 2276 | return rc; |
| 2277 | } |
| 2278 | |
| 2279 | /* Get free transaction_id */ |
| 2280 | llist_for_each_entry(transt, &trans->network->trans_list, entry) { |
| 2281 | /* Transaction of our lchan? */ |
| 2282 | if (transt->lchan == trans->lchan && |
| 2283 | transt->transaction_id != 0xff) |
| 2284 | trans_id_mask |= (1 << (transt->transaction_id >> 4)); |
| 2285 | } |
| 2286 | /* Assign free transaction ID */ |
| 2287 | if ((trans_id_mask & 0x007f) == 0x7f) { |
| 2288 | /* no free transaction ID */ |
| 2289 | rc = mncc_release_ind(trans->network, trans, trans->callref, |
| 2290 | 1, 47); |
| 2291 | trans->callref = 0; |
| 2292 | free_trans(trans); |
| 2293 | return rc; |
| 2294 | } |
| 2295 | for (i = 0; i < 7; i++) { |
| 2296 | if ((trans_id_mask & (1 << i)) == 0) { |
| 2297 | trans->transaction_id = i << 4; /* flag = 0 */ |
| 2298 | break; |
| 2299 | } |
| 2300 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2301 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2302 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2303 | msg->lchan = trans->lchan; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2304 | gh->msg_type = GSM48_MT_CC_SETUP; |
| 2305 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2306 | gsm48_start_cc_timer(trans, 0x303, GSM48_T303); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2307 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2308 | /* bearer capability */ |
| 2309 | if (setup->fields & MNCC_F_BEARER_CAP) |
| 2310 | encode_bearer_cap(msg, 0, &setup->bearer_cap); |
| 2311 | /* facility */ |
| 2312 | if (setup->fields & MNCC_F_FACILITY) |
| 2313 | encode_facility(msg, 0, &setup->facility); |
| 2314 | /* progress */ |
| 2315 | if (setup->fields & MNCC_F_PROGRESS) |
| 2316 | encode_progress(msg, 0, &setup->progress); |
| 2317 | /* calling party BCD number */ |
| 2318 | if (setup->fields & MNCC_F_CALLING) |
| 2319 | encode_calling(msg, &setup->calling); |
| 2320 | /* called party BCD number */ |
| 2321 | if (setup->fields & MNCC_F_CALLED) |
| 2322 | encode_called(msg, &setup->called); |
| 2323 | /* user-user */ |
| 2324 | if (setup->fields & MNCC_F_USERUSER) |
| 2325 | encode_useruser(msg, 0, &setup->useruser); |
| 2326 | /* redirecting party BCD number */ |
| 2327 | if (setup->fields & MNCC_F_REDIRECTING) |
| 2328 | encode_redirecting(msg, &setup->redirecting); |
| 2329 | /* signal */ |
| 2330 | if (setup->fields & MNCC_F_SIGNAL) |
| 2331 | encode_signal(msg, setup->signal); |
| 2332 | |
| 2333 | new_cc_state(trans, GSM_CSTATE_CALL_PRESENT); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2334 | |
| 2335 | return gsm48_sendmsg(msg); |
| 2336 | } |
| 2337 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2338 | static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg) |
| 2339 | { |
| 2340 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2341 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2342 | struct tlv_parsed tp; |
| 2343 | struct gsm_mncc call_conf; |
| 2344 | |
| 2345 | gsm48_stop_cc_timer(trans); |
| 2346 | gsm48_start_cc_timer(trans, 0x310, GSM48_T310); |
| 2347 | |
| 2348 | memset(&call_conf, 0, sizeof(struct gsm_mncc)); |
| 2349 | call_conf.callref = trans->callref; |
| 2350 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2351 | #if 0 |
| 2352 | /* repeat */ |
| 2353 | if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR)) |
| 2354 | call_conf.repeat = 1; |
| 2355 | if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ)) |
| 2356 | call_conf.repeat = 2; |
| 2357 | #endif |
| 2358 | /* bearer capability */ |
| 2359 | if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { |
| 2360 | call_conf.fields |= MNCC_F_BEARER_CAP; |
| 2361 | decode_bearer_cap(&call_conf.bearer_cap, |
| 2362 | TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1); |
| 2363 | } |
| 2364 | /* cause */ |
| 2365 | if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) { |
| 2366 | call_conf.fields |= MNCC_F_CAUSE; |
| 2367 | decode_cause(&call_conf.cause, |
| 2368 | TLVP_VAL(&tp, GSM48_IE_CAUSE)-1); |
| 2369 | } |
| 2370 | /* cc cap */ |
| 2371 | if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) { |
| 2372 | call_conf.fields |= MNCC_F_CCCAP; |
| 2373 | decode_cccap(&call_conf.cccap, |
| 2374 | TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1); |
| 2375 | } |
| 2376 | |
| 2377 | new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF); |
| 2378 | |
| 2379 | return mncc_recvmsg(trans->network, trans, MNCC_CALL_CONF_IND, &call_conf); |
| 2380 | } |
| 2381 | |
| 2382 | static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg) |
| 2383 | { |
| 2384 | struct gsm_mncc *proceeding = arg; |
| 2385 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2386 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2387 | |
| 2388 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2389 | msg->lchan = trans->lchan; |
| 2390 | gh->msg_type = GSM48_MT_CC_CALL_PROC; |
| 2391 | |
| 2392 | new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC); |
| 2393 | |
| 2394 | /* bearer capability */ |
| 2395 | if (proceeding->fields & MNCC_F_BEARER_CAP) |
| 2396 | encode_bearer_cap(msg, 0, &proceeding->bearer_cap); |
| 2397 | /* facility */ |
| 2398 | if (proceeding->fields & MNCC_F_FACILITY) |
| 2399 | encode_facility(msg, 0, &proceeding->facility); |
| 2400 | /* progress */ |
| 2401 | if (proceeding->fields & MNCC_F_PROGRESS) |
| 2402 | encode_progress(msg, 0, &proceeding->progress); |
| 2403 | |
| 2404 | return gsm48_sendmsg(msg); |
| 2405 | } |
| 2406 | |
| 2407 | static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg) |
| 2408 | { |
| 2409 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2410 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2411 | struct tlv_parsed tp; |
| 2412 | struct gsm_mncc alerting; |
| 2413 | |
| 2414 | gsm48_stop_cc_timer(trans); |
| 2415 | gsm48_start_cc_timer(trans, 0x301, GSM48_T301); |
| 2416 | |
| 2417 | memset(&alerting, 0, sizeof(struct gsm_mncc)); |
| 2418 | alerting.callref = trans->callref; |
| 2419 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2420 | /* facility */ |
| 2421 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2422 | alerting.fields |= MNCC_F_FACILITY; |
| 2423 | decode_facility(&alerting.facility, |
| 2424 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2425 | } |
| 2426 | |
| 2427 | /* progress */ |
| 2428 | if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) { |
| 2429 | alerting.fields |= MNCC_F_PROGRESS; |
| 2430 | decode_progress(&alerting.progress, |
| 2431 | TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1); |
| 2432 | } |
| 2433 | /* ss-version */ |
| 2434 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2435 | alerting.fields |= MNCC_F_SSVERSION; |
| 2436 | decode_ssversion(&alerting.ssversion, |
| 2437 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2438 | } |
| 2439 | |
| 2440 | new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED); |
| 2441 | |
| 2442 | return mncc_recvmsg(trans->network, trans, MNCC_ALERT_IND, &alerting); |
| 2443 | } |
| 2444 | |
| 2445 | static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg) |
| 2446 | { |
| 2447 | struct gsm_mncc *alerting = arg; |
| 2448 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2449 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2450 | |
| 2451 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2452 | msg->lchan = trans->lchan; |
| 2453 | gh->msg_type = GSM48_MT_CC_ALERTING; |
| 2454 | |
| 2455 | /* facility */ |
| 2456 | if (alerting->fields & MNCC_F_FACILITY) |
| 2457 | encode_facility(msg, 0, &alerting->facility); |
| 2458 | /* progress */ |
| 2459 | if (alerting->fields & MNCC_F_PROGRESS) |
| 2460 | encode_progress(msg, 0, &alerting->progress); |
| 2461 | /* user-user */ |
| 2462 | if (alerting->fields & MNCC_F_USERUSER) |
| 2463 | encode_useruser(msg, 0, &alerting->useruser); |
| 2464 | |
| 2465 | new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED); |
| 2466 | |
| 2467 | return gsm48_sendmsg(msg); |
| 2468 | } |
| 2469 | |
| 2470 | static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg) |
| 2471 | { |
| 2472 | struct gsm_mncc *progress = arg; |
| 2473 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2474 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2475 | |
| 2476 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2477 | msg->lchan = trans->lchan; |
| 2478 | gh->msg_type = GSM48_MT_CC_PROGRESS; |
| 2479 | |
| 2480 | /* progress */ |
| 2481 | encode_progress(msg, 1, &progress->progress); |
| 2482 | /* user-user */ |
| 2483 | if (progress->fields & MNCC_F_USERUSER) |
| 2484 | encode_useruser(msg, 0, &progress->useruser); |
| 2485 | |
| 2486 | return gsm48_sendmsg(msg); |
| 2487 | } |
| 2488 | |
| 2489 | static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg) |
| 2490 | { |
| 2491 | struct gsm_mncc *connect = arg; |
| 2492 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2493 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2494 | |
| 2495 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2496 | msg->lchan = trans->lchan; |
| 2497 | gh->msg_type = GSM48_MT_CC_CONNECT; |
| 2498 | |
| 2499 | gsm48_stop_cc_timer(trans); |
| 2500 | gsm48_start_cc_timer(trans, 0x313, GSM48_T313); |
| 2501 | |
| 2502 | /* facility */ |
| 2503 | if (connect->fields & MNCC_F_FACILITY) |
| 2504 | encode_facility(msg, 0, &connect->facility); |
| 2505 | /* progress */ |
| 2506 | if (connect->fields & MNCC_F_PROGRESS) |
| 2507 | encode_progress(msg, 0, &connect->progress); |
| 2508 | /* connected number */ |
| 2509 | if (connect->fields & MNCC_F_CONNECTED) |
| 2510 | encode_connected(msg, &connect->connected); |
| 2511 | /* user-user */ |
| 2512 | if (connect->fields & MNCC_F_USERUSER) |
| 2513 | encode_useruser(msg, 0, &connect->useruser); |
| 2514 | |
| 2515 | new_cc_state(trans, GSM_CSTATE_CONNECT_IND); |
| 2516 | |
| 2517 | return gsm48_sendmsg(msg); |
| 2518 | } |
| 2519 | |
| 2520 | static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg) |
| 2521 | { |
| 2522 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2523 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2524 | struct tlv_parsed tp; |
| 2525 | struct gsm_mncc connect; |
| 2526 | |
| 2527 | gsm48_stop_cc_timer(trans); |
| 2528 | |
| 2529 | memset(&connect, 0, sizeof(struct gsm_mncc)); |
| 2530 | connect.callref = trans->callref; |
| 2531 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2532 | /* use subscriber as connected party number */ |
| 2533 | if (trans->subscr) { |
| 2534 | connect.fields |= MNCC_F_CONNECTED; |
| 2535 | strncpy(connect.connected.number, trans->subscr->extension, |
| 2536 | sizeof(connect.connected.number)-1); |
| 2537 | } |
| 2538 | /* facility */ |
| 2539 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2540 | connect.fields |= MNCC_F_FACILITY; |
| 2541 | decode_facility(&connect.facility, |
| 2542 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2543 | } |
| 2544 | /* user-user */ |
| 2545 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 2546 | connect.fields |= MNCC_F_USERUSER; |
| 2547 | decode_useruser(&connect.useruser, |
| 2548 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 2549 | } |
| 2550 | /* ss-version */ |
| 2551 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2552 | connect.fields |= MNCC_F_SSVERSION; |
| 2553 | decode_ssversion(&connect.ssversion, |
| 2554 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2555 | } |
| 2556 | |
| 2557 | new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST); |
| 2558 | |
| 2559 | return mncc_recvmsg(trans->network, trans, MNCC_SETUP_CNF, &connect); |
| 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg) |
| 2564 | { |
| 2565 | struct gsm_mncc connect_ack; |
| 2566 | |
| 2567 | gsm48_stop_cc_timer(trans); |
| 2568 | |
| 2569 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 2570 | |
| 2571 | memset(&connect_ack, 0, sizeof(struct gsm_mncc)); |
| 2572 | connect_ack.callref = trans->callref; |
| 2573 | return mncc_recvmsg(trans->network, trans, MNCC_SETUP_COMPL_IND, |
| 2574 | &connect_ack); |
| 2575 | } |
| 2576 | |
| 2577 | static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg) |
| 2578 | { |
| 2579 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2580 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2581 | |
| 2582 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2583 | msg->lchan = trans->lchan; |
| 2584 | gh->msg_type = GSM48_MT_CC_CONNECT_ACK; |
| 2585 | |
| 2586 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 2587 | |
| 2588 | return gsm48_sendmsg(msg); |
| 2589 | } |
| 2590 | |
| 2591 | static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg) |
| 2592 | { |
| 2593 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2594 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2595 | struct tlv_parsed tp; |
| 2596 | struct gsm_mncc disc; |
| 2597 | |
| 2598 | gsm48_stop_cc_timer(trans); |
| 2599 | |
| 2600 | new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ); |
| 2601 | |
| 2602 | memset(&disc, 0, sizeof(struct gsm_mncc)); |
| 2603 | disc.callref = trans->callref; |
| 2604 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0); |
| 2605 | /* cause */ |
| 2606 | if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) { |
| 2607 | disc.fields |= MNCC_F_CAUSE; |
| 2608 | decode_cause(&disc.cause, |
| 2609 | TLVP_VAL(&tp, GSM48_IE_CAUSE)-1); |
| 2610 | } |
| 2611 | /* facility */ |
| 2612 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2613 | disc.fields |= MNCC_F_FACILITY; |
| 2614 | decode_facility(&disc.facility, |
| 2615 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2616 | } |
| 2617 | /* user-user */ |
| 2618 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 2619 | disc.fields |= MNCC_F_USERUSER; |
| 2620 | decode_useruser(&disc.useruser, |
| 2621 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 2622 | } |
| 2623 | /* ss-version */ |
| 2624 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2625 | disc.fields |= MNCC_F_SSVERSION; |
| 2626 | decode_ssversion(&disc.ssversion, |
| 2627 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2628 | } |
| 2629 | |
| 2630 | return mncc_recvmsg(trans->network, trans, MNCC_DISC_IND, &disc); |
| 2631 | |
| 2632 | } |
| 2633 | |
Harald Welte | bbc636a | 2009-06-11 14:23:20 +0800 | [diff] [blame] | 2634 | static struct gsm_mncc_cause default_cause = { |
| 2635 | .location = GSM48_CAUSE_LOC_PRN_S_LU, |
| 2636 | .coding = 0, |
| 2637 | .rec = 0, |
| 2638 | .rec_val = 0, |
| 2639 | .value = GSM48_CC_CAUSE_NORMAL_UNSPEC, |
| 2640 | .diag_len = 0, |
| 2641 | .diag = { 0 }, |
| 2642 | }; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2643 | |
| 2644 | static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg) |
| 2645 | { |
| 2646 | struct gsm_mncc *disc = arg; |
| 2647 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2648 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2649 | |
| 2650 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2651 | msg->lchan = trans->lchan; |
| 2652 | gh->msg_type = GSM48_MT_CC_DISCONNECT; |
| 2653 | |
| 2654 | gsm48_stop_cc_timer(trans); |
| 2655 | gsm48_start_cc_timer(trans, 0x306, GSM48_T306); |
| 2656 | |
| 2657 | /* cause */ |
| 2658 | if (disc->fields & MNCC_F_CAUSE) |
| 2659 | encode_cause(msg, 1, &disc->cause); |
| 2660 | else |
| 2661 | encode_cause(msg, 1, &default_cause); |
| 2662 | |
| 2663 | /* facility */ |
| 2664 | if (disc->fields & MNCC_F_FACILITY) |
| 2665 | encode_facility(msg, 0, &disc->facility); |
| 2666 | /* progress */ |
| 2667 | if (disc->fields & MNCC_F_PROGRESS) |
| 2668 | encode_progress(msg, 0, &disc->progress); |
| 2669 | /* user-user */ |
| 2670 | if (disc->fields & MNCC_F_USERUSER) |
| 2671 | encode_useruser(msg, 0, &disc->useruser); |
| 2672 | |
| 2673 | /* store disconnect cause for T306 expiry */ |
| 2674 | memcpy(&trans->cc_msg, disc, sizeof(struct gsm_mncc)); |
| 2675 | |
| 2676 | new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND); |
| 2677 | |
| 2678 | return gsm48_sendmsg(msg); |
| 2679 | } |
| 2680 | |
| 2681 | static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg) |
| 2682 | { |
| 2683 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2684 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2685 | struct tlv_parsed tp; |
| 2686 | struct gsm_mncc rel; |
| 2687 | int rc; |
| 2688 | |
| 2689 | gsm48_stop_cc_timer(trans); |
| 2690 | |
| 2691 | memset(&rel, 0, sizeof(struct gsm_mncc)); |
| 2692 | rel.callref = trans->callref; |
| 2693 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2694 | /* cause */ |
| 2695 | if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) { |
| 2696 | rel.fields |= MNCC_F_CAUSE; |
| 2697 | decode_cause(&rel.cause, |
| 2698 | TLVP_VAL(&tp, GSM48_IE_CAUSE)-1); |
| 2699 | } |
| 2700 | /* facility */ |
| 2701 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2702 | rel.fields |= MNCC_F_FACILITY; |
| 2703 | decode_facility(&rel.facility, |
| 2704 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2705 | } |
| 2706 | /* user-user */ |
| 2707 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 2708 | rel.fields |= MNCC_F_USERUSER; |
| 2709 | decode_useruser(&rel.useruser, |
| 2710 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 2711 | } |
| 2712 | /* ss-version */ |
| 2713 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2714 | rel.fields |= MNCC_F_SSVERSION; |
| 2715 | decode_ssversion(&rel.ssversion, |
| 2716 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2717 | } |
| 2718 | |
| 2719 | if (trans->state == GSM_CSTATE_RELEASE_REQ) { |
| 2720 | /* release collision 5.4.5 */ |
| 2721 | rc = mncc_recvmsg(trans->network, trans, MNCC_REL_CNF, &rel); |
| 2722 | } else { |
| 2723 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC | trans->transaction_id, |
| 2724 | GSM48_MT_CC_RELEASE_COMPL); |
| 2725 | rc = mncc_recvmsg(trans->network, trans, MNCC_REL_IND, &rel); |
| 2726 | } |
| 2727 | |
| 2728 | new_cc_state(trans, GSM_CSTATE_NULL); |
| 2729 | |
| 2730 | trans->callref = 0; |
| 2731 | free_trans(trans); |
| 2732 | |
| 2733 | return rc; |
| 2734 | } |
| 2735 | |
| 2736 | static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg) |
| 2737 | { |
| 2738 | struct gsm_mncc *rel = arg; |
| 2739 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2740 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2741 | |
| 2742 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2743 | msg->lchan = trans->lchan; |
| 2744 | gh->msg_type = GSM48_MT_CC_RELEASE; |
| 2745 | |
| 2746 | trans->callref = 0; |
| 2747 | |
| 2748 | gsm48_stop_cc_timer(trans); |
| 2749 | gsm48_start_cc_timer(trans, 0x308, GSM48_T308); |
| 2750 | |
| 2751 | /* cause */ |
| 2752 | if (rel->fields & MNCC_F_CAUSE) |
| 2753 | encode_cause(msg, 0, &rel->cause); |
| 2754 | /* facility */ |
| 2755 | if (rel->fields & MNCC_F_FACILITY) |
| 2756 | encode_facility(msg, 0, &rel->facility); |
| 2757 | /* user-user */ |
| 2758 | if (rel->fields & MNCC_F_USERUSER) |
| 2759 | encode_useruser(msg, 0, &rel->useruser); |
| 2760 | |
| 2761 | trans->T308_second = 0; |
| 2762 | memcpy(&trans->cc_msg, rel, sizeof(struct gsm_mncc)); |
| 2763 | |
| 2764 | if (trans->state != GSM_CSTATE_RELEASE_REQ) |
| 2765 | new_cc_state(trans, GSM_CSTATE_RELEASE_REQ); |
| 2766 | |
| 2767 | return gsm48_sendmsg(msg); |
| 2768 | } |
| 2769 | |
| 2770 | static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg) |
| 2771 | { |
| 2772 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2773 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2774 | struct tlv_parsed tp; |
| 2775 | struct gsm_mncc rel; |
| 2776 | int rc = 0; |
| 2777 | |
| 2778 | gsm48_stop_cc_timer(trans); |
| 2779 | |
| 2780 | memset(&rel, 0, sizeof(struct gsm_mncc)); |
| 2781 | rel.callref = trans->callref; |
| 2782 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2783 | /* cause */ |
| 2784 | if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) { |
| 2785 | rel.fields |= MNCC_F_CAUSE; |
| 2786 | decode_cause(&rel.cause, |
| 2787 | TLVP_VAL(&tp, GSM48_IE_CAUSE)-1); |
| 2788 | } |
| 2789 | /* facility */ |
| 2790 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2791 | rel.fields |= MNCC_F_FACILITY; |
| 2792 | decode_facility(&rel.facility, |
| 2793 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2794 | } |
| 2795 | /* user-user */ |
| 2796 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 2797 | rel.fields |= MNCC_F_USERUSER; |
| 2798 | decode_useruser(&rel.useruser, |
| 2799 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 2800 | } |
| 2801 | /* ss-version */ |
| 2802 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2803 | rel.fields |= MNCC_F_SSVERSION; |
| 2804 | decode_ssversion(&rel.ssversion, |
| 2805 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2806 | } |
| 2807 | |
| 2808 | if (trans->callref) { |
| 2809 | switch (trans->state) { |
| 2810 | case GSM_CSTATE_CALL_PRESENT: |
| 2811 | rc = mncc_recvmsg(trans->network, trans, |
| 2812 | MNCC_REJ_IND, &rel); |
| 2813 | break; |
| 2814 | case GSM_CSTATE_RELEASE_REQ: |
| 2815 | rc = mncc_recvmsg(trans->network, trans, |
| 2816 | MNCC_REL_CNF, &rel); |
| 2817 | break; |
| 2818 | default: |
| 2819 | rc = mncc_recvmsg(trans->network, trans, |
| 2820 | MNCC_REL_IND, &rel); |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | trans->callref = 0; |
| 2825 | free_trans(trans); |
| 2826 | |
| 2827 | return rc; |
| 2828 | } |
| 2829 | |
| 2830 | static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg) |
| 2831 | { |
| 2832 | struct gsm_mncc *rel = arg; |
| 2833 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2834 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2835 | |
| 2836 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2837 | msg->lchan = trans->lchan; |
| 2838 | gh->msg_type = GSM48_MT_CC_RELEASE_COMPL; |
| 2839 | |
| 2840 | trans->callref = 0; |
| 2841 | |
| 2842 | gsm48_stop_cc_timer(trans); |
| 2843 | |
| 2844 | /* cause */ |
| 2845 | if (rel->fields & MNCC_F_CAUSE) |
| 2846 | encode_cause(msg, 0, &rel->cause); |
| 2847 | /* facility */ |
| 2848 | if (rel->fields & MNCC_F_FACILITY) |
| 2849 | encode_facility(msg, 0, &rel->facility); |
| 2850 | /* user-user */ |
| 2851 | if (rel->fields & MNCC_F_USERUSER) |
| 2852 | encode_useruser(msg, 0, &rel->useruser); |
| 2853 | |
| 2854 | free_trans(trans); |
| 2855 | |
| 2856 | return gsm48_sendmsg(msg); |
| 2857 | } |
| 2858 | |
| 2859 | static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg) |
| 2860 | { |
| 2861 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2862 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2863 | struct tlv_parsed tp; |
| 2864 | struct gsm_mncc fac; |
| 2865 | |
| 2866 | memset(&fac, 0, sizeof(struct gsm_mncc)); |
| 2867 | fac.callref = trans->callref; |
| 2868 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0); |
| 2869 | /* facility */ |
| 2870 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2871 | fac.fields |= MNCC_F_FACILITY; |
| 2872 | decode_facility(&fac.facility, |
| 2873 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2874 | } |
| 2875 | /* ss-version */ |
| 2876 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2877 | fac.fields |= MNCC_F_SSVERSION; |
| 2878 | decode_ssversion(&fac.ssversion, |
| 2879 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2880 | } |
| 2881 | |
| 2882 | return mncc_recvmsg(trans->network, trans, MNCC_FACILITY_IND, &fac); |
| 2883 | } |
| 2884 | |
| 2885 | static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg) |
| 2886 | { |
| 2887 | struct gsm_mncc *fac = arg; |
| 2888 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2889 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2890 | |
| 2891 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2892 | msg->lchan = trans->lchan; |
| 2893 | gh->msg_type = GSM48_MT_CC_FACILITY; |
| 2894 | |
| 2895 | /* facility */ |
| 2896 | encode_facility(msg, 1, &fac->facility); |
| 2897 | |
| 2898 | return gsm48_sendmsg(msg); |
| 2899 | } |
| 2900 | |
| 2901 | static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg) |
| 2902 | { |
| 2903 | struct gsm_mncc hold; |
| 2904 | |
| 2905 | memset(&hold, 0, sizeof(struct gsm_mncc)); |
| 2906 | hold.callref = trans->callref; |
| 2907 | return mncc_recvmsg(trans->network, trans, MNCC_HOLD_IND, &hold); |
| 2908 | } |
| 2909 | |
| 2910 | static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg) |
| 2911 | { |
| 2912 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2913 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2914 | |
| 2915 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2916 | msg->lchan = trans->lchan; |
| 2917 | gh->msg_type = GSM48_MT_CC_HOLD_ACK; |
| 2918 | |
| 2919 | return gsm48_sendmsg(msg); |
| 2920 | } |
| 2921 | |
| 2922 | static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg) |
| 2923 | { |
| 2924 | struct gsm_mncc *hold_rej = arg; |
| 2925 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2926 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2927 | |
| 2928 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2929 | msg->lchan = trans->lchan; |
| 2930 | gh->msg_type = GSM48_MT_CC_HOLD_REJ; |
| 2931 | |
| 2932 | /* cause */ |
| 2933 | if (hold_rej->fields & MNCC_F_CAUSE) |
| 2934 | encode_cause(msg, 1, &hold_rej->cause); |
| 2935 | else |
| 2936 | encode_cause(msg, 1, &default_cause); |
| 2937 | |
| 2938 | return gsm48_sendmsg(msg); |
| 2939 | } |
| 2940 | |
| 2941 | static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg) |
| 2942 | { |
| 2943 | struct gsm_mncc retrieve; |
| 2944 | |
| 2945 | memset(&retrieve, 0, sizeof(struct gsm_mncc)); |
| 2946 | retrieve.callref = trans->callref; |
| 2947 | return mncc_recvmsg(trans->network, trans, MNCC_RETRIEVE_IND, &retrieve); |
| 2948 | } |
| 2949 | |
| 2950 | static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg) |
| 2951 | { |
| 2952 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2953 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2954 | |
| 2955 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2956 | msg->lchan = trans->lchan; |
| 2957 | gh->msg_type = GSM48_MT_CC_RETR_ACK; |
| 2958 | |
| 2959 | return gsm48_sendmsg(msg); |
| 2960 | } |
| 2961 | |
| 2962 | static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg) |
| 2963 | { |
| 2964 | struct gsm_mncc *retrieve_rej = arg; |
| 2965 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2966 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2967 | |
| 2968 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2969 | msg->lchan = trans->lchan; |
| 2970 | gh->msg_type = GSM48_MT_CC_RETR_REJ; |
| 2971 | |
| 2972 | /* cause */ |
| 2973 | if (retrieve_rej->fields & MNCC_F_CAUSE) |
| 2974 | encode_cause(msg, 1, &retrieve_rej->cause); |
| 2975 | else |
| 2976 | encode_cause(msg, 1, &default_cause); |
| 2977 | |
| 2978 | return gsm48_sendmsg(msg); |
| 2979 | } |
| 2980 | |
| 2981 | static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg) |
| 2982 | { |
| 2983 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2984 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2985 | struct tlv_parsed tp; |
| 2986 | struct gsm_mncc dtmf; |
| 2987 | |
| 2988 | memset(&dtmf, 0, sizeof(struct gsm_mncc)); |
| 2989 | dtmf.callref = trans->callref; |
| 2990 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2991 | /* keypad facility */ |
| 2992 | if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) { |
| 2993 | dtmf.fields |= MNCC_F_KEYPAD; |
| 2994 | decode_keypad(&dtmf.keypad, |
| 2995 | TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1); |
| 2996 | } |
| 2997 | |
| 2998 | return mncc_recvmsg(trans->network, trans, MNCC_START_DTMF_IND, &dtmf); |
| 2999 | } |
| 3000 | |
| 3001 | static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg) |
| 3002 | { |
| 3003 | struct gsm_mncc *dtmf = arg; |
| 3004 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3005 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3006 | |
| 3007 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3008 | msg->lchan = trans->lchan; |
| 3009 | gh->msg_type = GSM48_MT_CC_START_DTMF_ACK; |
| 3010 | |
| 3011 | /* keypad */ |
| 3012 | if (dtmf->fields & MNCC_F_KEYPAD) |
| 3013 | encode_keypad(msg, dtmf->keypad); |
| 3014 | |
| 3015 | return gsm48_sendmsg(msg); |
| 3016 | } |
| 3017 | |
| 3018 | static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg) |
| 3019 | { |
| 3020 | struct gsm_mncc *dtmf = arg; |
| 3021 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3022 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3023 | |
| 3024 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3025 | msg->lchan = trans->lchan; |
| 3026 | gh->msg_type = GSM48_MT_CC_START_DTMF_REJ; |
| 3027 | |
| 3028 | /* cause */ |
| 3029 | if (dtmf->fields & MNCC_F_CAUSE) |
| 3030 | encode_cause(msg, 1, &dtmf->cause); |
| 3031 | else |
| 3032 | encode_cause(msg, 1, &default_cause); |
| 3033 | |
| 3034 | return gsm48_sendmsg(msg); |
| 3035 | } |
| 3036 | |
| 3037 | static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg) |
| 3038 | { |
| 3039 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3040 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3041 | |
| 3042 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3043 | msg->lchan = trans->lchan; |
| 3044 | gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK; |
| 3045 | |
| 3046 | return gsm48_sendmsg(msg); |
| 3047 | } |
| 3048 | |
| 3049 | static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg) |
| 3050 | { |
| 3051 | struct gsm_mncc dtmf; |
| 3052 | |
| 3053 | memset(&dtmf, 0, sizeof(struct gsm_mncc)); |
| 3054 | dtmf.callref = trans->callref; |
| 3055 | |
| 3056 | return mncc_recvmsg(trans->network, trans, MNCC_STOP_DTMF_IND, &dtmf); |
| 3057 | } |
| 3058 | |
| 3059 | static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg) |
| 3060 | { |
| 3061 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3062 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 3063 | struct tlv_parsed tp; |
| 3064 | struct gsm_mncc modify; |
| 3065 | |
| 3066 | memset(&modify, 0, sizeof(struct gsm_mncc)); |
| 3067 | modify.callref = trans->callref; |
| 3068 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0); |
| 3069 | /* bearer capability */ |
| 3070 | if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { |
| 3071 | modify.fields |= MNCC_F_BEARER_CAP; |
| 3072 | decode_bearer_cap(&modify.bearer_cap, |
| 3073 | TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1); |
| 3074 | } |
| 3075 | |
| 3076 | new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY); |
| 3077 | |
| 3078 | return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_IND, &modify); |
| 3079 | } |
| 3080 | |
| 3081 | static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg) |
| 3082 | { |
| 3083 | struct gsm_mncc *modify = arg; |
| 3084 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3085 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3086 | |
| 3087 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3088 | msg->lchan = trans->lchan; |
| 3089 | gh->msg_type = GSM48_MT_CC_MODIFY; |
| 3090 | |
| 3091 | gsm48_start_cc_timer(trans, 0x323, GSM48_T323); |
| 3092 | |
| 3093 | /* bearer capability */ |
| 3094 | encode_bearer_cap(msg, 1, &modify->bearer_cap); |
| 3095 | |
| 3096 | new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY); |
| 3097 | |
| 3098 | return gsm48_sendmsg(msg); |
| 3099 | } |
| 3100 | |
| 3101 | static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg) |
| 3102 | { |
| 3103 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3104 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 3105 | struct tlv_parsed tp; |
| 3106 | struct gsm_mncc modify; |
| 3107 | |
| 3108 | gsm48_stop_cc_timer(trans); |
| 3109 | |
| 3110 | memset(&modify, 0, sizeof(struct gsm_mncc)); |
| 3111 | modify.callref = trans->callref; |
| 3112 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0); |
| 3113 | /* bearer capability */ |
| 3114 | if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { |
| 3115 | modify.fields |= MNCC_F_BEARER_CAP; |
| 3116 | decode_bearer_cap(&modify.bearer_cap, |
| 3117 | TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1); |
| 3118 | } |
| 3119 | |
| 3120 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 3121 | |
| 3122 | return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_CNF, &modify); |
| 3123 | } |
| 3124 | |
| 3125 | static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg) |
| 3126 | { |
| 3127 | struct gsm_mncc *modify = arg; |
| 3128 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3129 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3130 | |
| 3131 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3132 | msg->lchan = trans->lchan; |
| 3133 | gh->msg_type = GSM48_MT_CC_MODIFY_COMPL; |
| 3134 | |
| 3135 | /* bearer capability */ |
| 3136 | encode_bearer_cap(msg, 1, &modify->bearer_cap); |
| 3137 | |
| 3138 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 3139 | |
| 3140 | return gsm48_sendmsg(msg); |
| 3141 | } |
| 3142 | |
| 3143 | static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg) |
| 3144 | { |
| 3145 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3146 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 3147 | struct tlv_parsed tp; |
| 3148 | struct gsm_mncc modify; |
| 3149 | |
| 3150 | gsm48_stop_cc_timer(trans); |
| 3151 | |
| 3152 | memset(&modify, 0, sizeof(struct gsm_mncc)); |
| 3153 | modify.callref = trans->callref; |
| 3154 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE); |
| 3155 | /* bearer capability */ |
| 3156 | if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { |
| 3157 | modify.fields |= GSM48_IE_BEARER_CAP; |
| 3158 | decode_bearer_cap(&modify.bearer_cap, |
| 3159 | TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1); |
| 3160 | } |
| 3161 | /* cause */ |
| 3162 | if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) { |
| 3163 | modify.fields |= MNCC_F_CAUSE; |
| 3164 | decode_cause(&modify.cause, |
| 3165 | TLVP_VAL(&tp, GSM48_IE_CAUSE)-1); |
| 3166 | } |
| 3167 | |
| 3168 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 3169 | |
| 3170 | return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_REJ, &modify); |
| 3171 | } |
| 3172 | |
| 3173 | static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg) |
| 3174 | { |
| 3175 | struct gsm_mncc *modify = arg; |
| 3176 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3177 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3178 | |
| 3179 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3180 | msg->lchan = trans->lchan; |
| 3181 | gh->msg_type = GSM48_MT_CC_MODIFY_REJECT; |
| 3182 | |
| 3183 | /* bearer capability */ |
| 3184 | encode_bearer_cap(msg, 1, &modify->bearer_cap); |
| 3185 | /* cause */ |
| 3186 | encode_cause(msg, 1, &modify->cause); |
| 3187 | |
| 3188 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 3189 | |
| 3190 | return gsm48_sendmsg(msg); |
| 3191 | } |
| 3192 | |
| 3193 | static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg) |
| 3194 | { |
| 3195 | struct gsm_mncc *notify = arg; |
| 3196 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3197 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3198 | |
| 3199 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3200 | msg->lchan = trans->lchan; |
| 3201 | gh->msg_type = GSM48_MT_CC_NOTIFY; |
| 3202 | |
| 3203 | /* notify */ |
| 3204 | encode_notify(msg, notify->notify); |
| 3205 | |
| 3206 | return gsm48_sendmsg(msg); |
| 3207 | } |
| 3208 | |
| 3209 | static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg) |
| 3210 | { |
| 3211 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3212 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 3213 | // struct tlv_parsed tp; |
| 3214 | struct gsm_mncc notify; |
| 3215 | |
| 3216 | memset(¬ify, 0, sizeof(struct gsm_mncc)); |
| 3217 | notify.callref = trans->callref; |
| 3218 | // tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len); |
| 3219 | if (payload_len >= 1) |
| 3220 | decode_notify(¬ify.notify, gh->data); |
| 3221 | |
| 3222 | return mncc_recvmsg(trans->network, trans, MNCC_NOTIFY_IND, ¬ify); |
| 3223 | } |
| 3224 | |
| 3225 | static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg) |
| 3226 | { |
| 3227 | struct gsm_mncc *user = arg; |
| 3228 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3229 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3230 | |
| 3231 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3232 | msg->lchan = trans->lchan; |
| 3233 | gh->msg_type = GSM48_MT_CC_USER_INFO; |
| 3234 | |
| 3235 | /* user-user */ |
| 3236 | if (user->fields & MNCC_F_USERUSER) |
| 3237 | encode_useruser(msg, 1, &user->useruser); |
| 3238 | /* more data */ |
| 3239 | if (user->more) |
| 3240 | encode_more(msg); |
| 3241 | |
| 3242 | return gsm48_sendmsg(msg); |
| 3243 | } |
| 3244 | |
| 3245 | static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg) |
| 3246 | { |
| 3247 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3248 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 3249 | struct tlv_parsed tp; |
| 3250 | struct gsm_mncc user; |
| 3251 | |
| 3252 | memset(&user, 0, sizeof(struct gsm_mncc)); |
| 3253 | user.callref = trans->callref; |
| 3254 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0); |
| 3255 | /* user-user */ |
| 3256 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 3257 | user.fields |= MNCC_F_USERUSER; |
| 3258 | decode_useruser(&user.useruser, |
| 3259 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 3260 | } |
| 3261 | /* more data */ |
| 3262 | if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA)) |
| 3263 | user.more = 1; |
| 3264 | |
| 3265 | return mncc_recvmsg(trans->network, trans, MNCC_USERINFO_IND, &user); |
| 3266 | } |
| 3267 | |
| 3268 | static int gsm48_lchan_modify(struct gsm_trans *trans, void *arg) |
| 3269 | { |
| 3270 | struct gsm_mncc *mode = arg; |
| 3271 | |
| 3272 | return gsm48_tx_chan_mode_modify(trans->lchan, mode->lchan_mode); |
| 3273 | } |
| 3274 | |
| 3275 | static struct downstate { |
| 3276 | u_int32_t states; |
| 3277 | int type; |
| 3278 | int (*rout) (struct gsm_trans *trans, void *arg); |
| 3279 | } downstatelist[] = { |
| 3280 | /* mobile originating call establishment */ |
| 3281 | {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */ |
| 3282 | MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc}, |
| 3283 | {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */ |
| 3284 | MNCC_ALERT_REQ, gsm48_cc_tx_alerting}, |
| 3285 | {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC) | SBIT(GSM_CSTATE_CALL_DELIVERED), /* 5.2.1.2 | 5.2.1.6 | 5.2.1.6 */ |
| 3286 | MNCC_SETUP_RSP, gsm48_cc_tx_connect}, |
| 3287 | {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */ |
| 3288 | MNCC_PROGRESS_REQ, gsm48_cc_tx_progress}, |
| 3289 | /* mobile terminating call establishment */ |
| 3290 | {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */ |
| 3291 | MNCC_SETUP_REQ, gsm48_cc_tx_setup}, |
| 3292 | {SBIT(GSM_CSTATE_CONNECT_REQUEST), |
| 3293 | MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack}, |
| 3294 | /* signalling during call */ |
| 3295 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3296 | MNCC_NOTIFY_REQ, gsm48_cc_tx_notify}, |
| 3297 | {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), |
| 3298 | MNCC_FACILITY_REQ, gsm48_cc_tx_facility}, |
| 3299 | {ALL_STATES, |
| 3300 | MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack}, |
| 3301 | {ALL_STATES, |
| 3302 | MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej}, |
| 3303 | {ALL_STATES, |
| 3304 | MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack}, |
| 3305 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3306 | MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack}, |
| 3307 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3308 | MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej}, |
| 3309 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3310 | MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack}, |
| 3311 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3312 | MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej}, |
| 3313 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3314 | MNCC_MODIFY_REQ, gsm48_cc_tx_modify}, |
| 3315 | {SBIT(GSM_CSTATE_MO_ORIG_MODIFY), |
| 3316 | MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete}, |
| 3317 | {SBIT(GSM_CSTATE_MO_ORIG_MODIFY), |
| 3318 | MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject}, |
| 3319 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3320 | MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo}, |
| 3321 | /* clearing */ |
| 3322 | {SBIT(GSM_CSTATE_INITIATED), |
| 3323 | MNCC_REJ_REQ, gsm48_cc_tx_release_compl}, |
| 3324 | {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */ |
| 3325 | MNCC_DISC_REQ, gsm48_cc_tx_disconnect}, |
| 3326 | {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */ |
| 3327 | MNCC_REL_REQ, gsm48_cc_tx_release}, |
| 3328 | /* special */ |
| 3329 | {ALL_STATES, |
| 3330 | MNCC_LCHAN_MODIFY, gsm48_lchan_modify}, |
| 3331 | }; |
| 3332 | |
| 3333 | #define DOWNSLLEN \ |
| 3334 | (sizeof(downstatelist) / sizeof(struct downstate)) |
| 3335 | |
| 3336 | |
| 3337 | int mncc_send(struct gsm_network *net, int msg_type, void *arg) |
| 3338 | { |
| 3339 | int i, j, k, l, rc = 0; |
| 3340 | struct gsm_trans *trans = NULL, *transt; |
| 3341 | struct gsm_subscriber *subscr; |
| 3342 | struct gsm_lchan *lchan = NULL, *lchant; |
| 3343 | struct gsm_bts *bts = NULL; |
| 3344 | struct gsm_bts_trx *trx; |
| 3345 | struct gsm_bts_trx_ts *ts; |
| 3346 | struct gsm_mncc *data = arg, rel; |
| 3347 | |
| 3348 | /* handle special messages */ |
| 3349 | switch(msg_type) { |
| 3350 | case MNCC_BRIDGE: |
| 3351 | return tch_bridge(net, arg); |
| 3352 | case MNCC_FRAME_DROP: |
| 3353 | return tch_recv(net, arg, 0); |
| 3354 | case MNCC_FRAME_RECV: |
| 3355 | return tch_recv(net, arg, 1); |
| 3356 | case GSM_TRAU_FRAME: |
| 3357 | return tch_frame(net, arg); |
| 3358 | } |
| 3359 | |
| 3360 | memset(&rel, 0, sizeof(struct gsm_mncc)); |
| 3361 | rel.callref = data->callref; |
| 3362 | |
| 3363 | /* Find callref */ |
| 3364 | trans = get_trans_ref(net, data->callref); |
| 3365 | |
| 3366 | /* Callref unknown */ |
| 3367 | if (!trans) { |
| 3368 | if (msg_type != MNCC_SETUP_REQ || !data->called.number[0]) { |
| 3369 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) " |
| 3370 | "Received '%s' from MNCC with " |
| 3371 | "unknown callref %d\n", data->called.number, |
| 3372 | get_mncc_name(msg_type), data->callref); |
| 3373 | /* Invalid call reference */ |
| 3374 | return mncc_release_ind(net, NULL, data->callref, 1, 81); |
| 3375 | } |
| 3376 | /* New transaction due to setup, find subscriber */ |
| 3377 | subscr = subscr_get_by_extension(data->called.number); |
| 3378 | /* If subscriber is not found */ |
| 3379 | if (!subscr) { |
| 3380 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) " |
| 3381 | "Received '%s' from MNCC with " |
| 3382 | "unknown subscriber %s\n", data->called.number, |
| 3383 | get_mncc_name(msg_type), data->called.number); |
| 3384 | /* Unknown subscriber */ |
| 3385 | return mncc_release_ind(net, NULL, data->callref, 1, 1); |
| 3386 | } |
| 3387 | /* If subscriber is not "attached" */ |
| 3388 | if (!subscr->lac) { |
| 3389 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) " |
| 3390 | "Received '%s' from MNCC with " |
| 3391 | "detached subscriber %s\n", data->called.number, |
| 3392 | get_mncc_name(msg_type), data->called.number); |
| 3393 | subscr_put(subscr); |
| 3394 | /* Temporarily out of order */ |
| 3395 | return mncc_release_ind(net, NULL, data->callref, 1, 27); |
| 3396 | } |
| 3397 | /* Create transaction */ |
| 3398 | if (!(trans = calloc(1, sizeof(struct gsm_trans)))) { |
| 3399 | DEBUGP(DCC, "No memory for trans.\n"); |
| 3400 | subscr_put(subscr); |
| 3401 | /* Ressource unavailable */ |
| 3402 | mncc_release_ind(net, NULL, data->callref, 1, 47); |
| 3403 | return -ENOMEM; |
| 3404 | } |
| 3405 | trans->callref = data->callref; |
| 3406 | trans->network = net; |
| 3407 | trans->transaction_id = 0xff; /* unassigned */ |
| 3408 | llist_add_tail(&trans->entry, &net->trans_list); |
| 3409 | /* Assign subscriber to transaction */ |
| 3410 | trans->subscr = subscr; |
| 3411 | /* Find lchan */ |
| 3412 | for (i = 0; i < net->num_bts; i++) { |
| 3413 | bts = &net->bts[i]; |
| 3414 | for (j = 0; j < bts->num_trx; j++) { |
| 3415 | trx = &bts->trx[j]; |
| 3416 | for (k = 0; k < TRX_NR_TS; k++) { |
| 3417 | ts = &trx->ts[k]; |
| 3418 | for (l = 0; l < TS_MAX_LCHAN; l++) { |
| 3419 | lchant = &ts->lchan[l]; |
| 3420 | if (lchant->subscr == subscr) { |
| 3421 | lchan = lchant; |
| 3422 | break; |
| 3423 | } |
| 3424 | } |
| 3425 | } |
| 3426 | } |
| 3427 | } |
| 3428 | |
| 3429 | /* If subscriber has no lchan */ |
| 3430 | if (!lchan) { |
| 3431 | /* find transaction with this subscriber already paging */ |
| 3432 | llist_for_each_entry(transt, &net->trans_list, entry) { |
| 3433 | /* Transaction of our lchan? */ |
| 3434 | if (transt == trans || |
| 3435 | transt->subscr != subscr) |
| 3436 | continue; |
| 3437 | DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) " |
| 3438 | "Received '%s' from MNCC with " |
| 3439 | "unallocated channel, paging already " |
| 3440 | "started.\n", bts->nr, |
| 3441 | data->called.number, |
| 3442 | get_mncc_name(msg_type)); |
| 3443 | return 0; |
| 3444 | } |
| 3445 | /* store setup informations until paging was successfull */ |
| 3446 | memcpy(&trans->cc_msg, data, sizeof(struct gsm_mncc)); |
| 3447 | /* start paging subscriber on all BTS with her location */ |
| 3448 | subscr->net = net; |
| 3449 | bts = NULL; |
| 3450 | do { |
| 3451 | bts = gsm_bts_by_lac(net, subscr->lac, bts); |
| 3452 | if (!bts) |
| 3453 | break; |
| 3454 | DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) " |
| 3455 | "Received '%s' from MNCC with " |
| 3456 | "unallocated channel, paging.\n", |
| 3457 | bts->nr, data->called.number, |
| 3458 | get_mncc_name(msg_type)); |
| 3459 | /* Trigger paging */ |
| 3460 | paging_request(bts, subscr, RSL_CHANNEED_TCH_F, |
| 3461 | setup_trig_pag_evt, subscr); |
| 3462 | } while (1); |
| 3463 | return 0; |
| 3464 | } |
| 3465 | /* Assign lchan */ |
| 3466 | trans->lchan = lchan; |
| 3467 | use_lchan(lchan); |
| 3468 | } |
| 3469 | lchan = trans->lchan; |
| 3470 | |
| 3471 | /* if paging did not respond yet */ |
| 3472 | if (!lchan) { |
| 3473 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) " |
| 3474 | "Received '%s' from MNCC in paging state\n", |
| 3475 | (trans->subscr)?(trans->subscr->extension):"-", |
| 3476 | get_mncc_name(msg_type)); |
Harald Welte | bbc636a | 2009-06-11 14:23:20 +0800 | [diff] [blame] | 3477 | mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, |
| 3478 | GSM48_CC_CAUSE_NORM_CALL_CLEAR); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3479 | if (msg_type == MNCC_REL_REQ) |
| 3480 | rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel); |
| 3481 | else |
| 3482 | rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel); |
| 3483 | trans->callref = 0; |
| 3484 | free_trans(trans); |
| 3485 | return rc; |
| 3486 | } |
| 3487 | |
| 3488 | DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) " |
| 3489 | "Received '%s' from MNCC in state %d (%s)\n", |
| 3490 | lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr, |
| 3491 | trans->transaction_id, |
| 3492 | (lchan->subscr)?(lchan->subscr->extension):"-", |
| 3493 | get_mncc_name(msg_type), trans->state, |
| 3494 | cc_state_names[trans->state]); |
| 3495 | |
| 3496 | /* Find function for current state and message */ |
| 3497 | for (i = 0; i < DOWNSLLEN; i++) |
| 3498 | if ((msg_type == downstatelist[i].type) |
| 3499 | && ((1 << trans->state) & downstatelist[i].states)) |
| 3500 | break; |
| 3501 | if (i == DOWNSLLEN) { |
| 3502 | DEBUGP(DCC, "Message unhandled at this state.\n"); |
| 3503 | return 0; |
| 3504 | } |
| 3505 | |
| 3506 | rc = downstatelist[i].rout(trans, arg); |
| 3507 | |
| 3508 | return rc; |
| 3509 | } |
| 3510 | |
| 3511 | |
| 3512 | static struct datastate { |
| 3513 | u_int32_t states; |
| 3514 | int type; |
| 3515 | int (*rout) (struct gsm_trans *trans, struct msgb *msg); |
| 3516 | } datastatelist[] = { |
| 3517 | /* mobile originating call establishment */ |
| 3518 | {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */ |
| 3519 | GSM48_MT_CC_SETUP, gsm48_cc_rx_setup}, |
| 3520 | {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */ |
| 3521 | GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup}, |
| 3522 | {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */ |
| 3523 | GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack}, |
| 3524 | /* mobile terminating call establishment */ |
| 3525 | {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */ |
| 3526 | GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf}, |
| 3527 | {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */ |
| 3528 | GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting}, |
| 3529 | {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */ |
| 3530 | GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect}, |
| 3531 | /* signalling during call */ |
| 3532 | {ALL_STATES - SBIT(GSM_CSTATE_NULL), |
| 3533 | GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility}, |
| 3534 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3535 | GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify}, |
| 3536 | {ALL_STATES, |
| 3537 | GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf}, |
| 3538 | {ALL_STATES, |
| 3539 | GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf}, |
| 3540 | {ALL_STATES, |
| 3541 | GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq}, |
| 3542 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3543 | GSM48_MT_CC_HOLD, gsm48_cc_rx_hold}, |
| 3544 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3545 | GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve}, |
| 3546 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3547 | GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify}, |
| 3548 | {SBIT(GSM_CSTATE_MO_TERM_MODIFY), |
| 3549 | GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete}, |
| 3550 | {SBIT(GSM_CSTATE_MO_TERM_MODIFY), |
| 3551 | GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject}, |
| 3552 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3553 | GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo}, |
| 3554 | /* clearing */ |
| 3555 | {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */ |
| 3556 | GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect}, |
| 3557 | {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */ |
| 3558 | GSM48_MT_CC_RELEASE, gsm48_cc_rx_release}, |
| 3559 | {ALL_STATES, /* 5.4.3.4 */ |
| 3560 | GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl}, |
| 3561 | }; |
| 3562 | |
| 3563 | #define DATASLLEN \ |
| 3564 | (sizeof(datastatelist) / sizeof(struct datastate)) |
| 3565 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3566 | static int gsm0408_rcv_cc(struct msgb *msg) |
| 3567 | { |
| 3568 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3569 | u_int8_t msg_type = gh->msg_type & 0xbf; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3570 | u_int8_t transaction_id = (gh->proto_discr & 0xf0) ^ 0x80; /* flip */ |
| 3571 | struct gsm_lchan *lchan = msg->lchan; |
| 3572 | struct gsm_trans *trans = NULL, *transt; |
| 3573 | struct gsm_network *net = lchan->ts->trx->bts->network; |
| 3574 | int i, rc = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3575 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3576 | if (msg_type & 0x80) { |
| 3577 | DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type); |
| 3578 | return -EINVAL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3579 | } |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3580 | |
| 3581 | /* Find transaction */ |
| 3582 | llist_for_each_entry(transt, &net->trans_list, entry) { |
| 3583 | /* Transaction of our lchan? */ |
| 3584 | if (transt->lchan == lchan |
| 3585 | && transt->transaction_id == transaction_id) { |
| 3586 | trans = transt; |
| 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) " |
| 3591 | "Received '%s' from MS in state %d (%s)\n", |
| 3592 | lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr, |
| 3593 | transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-", |
| 3594 | cc_msg_names[msg_type], trans?(trans->state):0, |
| 3595 | cc_state_names[trans?(trans->state):0]); |
| 3596 | |
| 3597 | /* Create transaction */ |
| 3598 | if (!trans) { |
| 3599 | DEBUGP(DCC, "Unknown transaction ID %02x, " |
| 3600 | "creating new trans.\n", transaction_id); |
| 3601 | /* Create transaction */ |
| 3602 | if (!(trans = calloc(1, sizeof(struct gsm_trans)))) { |
| 3603 | DEBUGP(DCC, "No memory for trans.\n"); |
| 3604 | rc = gsm48_tx_simple(msg->lchan, |
| 3605 | GSM48_PDISC_CC | transaction_id, |
| 3606 | GSM48_MT_CC_RELEASE_COMPL); |
| 3607 | return -ENOMEM; |
| 3608 | } |
| 3609 | llist_add_tail(&trans->entry, &net->trans_list); |
| 3610 | /* Assign transaction */ |
| 3611 | trans->callref = new_callref++; |
| 3612 | trans->network = net; |
| 3613 | trans->transaction_id = transaction_id; |
| 3614 | trans->lchan = lchan; |
| 3615 | use_lchan(lchan); |
| 3616 | if (lchan->subscr) { |
| 3617 | trans->subscr = lchan->subscr; |
| 3618 | subscr_get(trans->subscr); |
| 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | /* find function for current state and message */ |
| 3623 | for (i = 0; i < DATASLLEN; i++) |
| 3624 | if ((msg_type == datastatelist[i].type) |
| 3625 | && ((1 << trans->state) & datastatelist[i].states)) |
| 3626 | break; |
| 3627 | if (i == DATASLLEN) { |
| 3628 | DEBUGP(DCC, "Message unhandled at this state.\n"); |
| 3629 | return 0; |
| 3630 | } |
| 3631 | |
| 3632 | rc = datastatelist[i].rout(trans, msg); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3633 | |
| 3634 | return rc; |
| 3635 | } |
| 3636 | |
| 3637 | /* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */ |
| 3638 | int gsm0408_rcvmsg(struct msgb *msg) |
| 3639 | { |
| 3640 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3641 | u_int8_t pdisc = gh->proto_discr & 0x0f; |
| 3642 | int rc = 0; |
| 3643 | |
| 3644 | switch (pdisc) { |
| 3645 | case GSM48_PDISC_CC: |
| 3646 | rc = gsm0408_rcv_cc(msg); |
| 3647 | break; |
| 3648 | case GSM48_PDISC_MM: |
| 3649 | rc = gsm0408_rcv_mm(msg); |
| 3650 | break; |
| 3651 | case GSM48_PDISC_RR: |
| 3652 | rc = gsm0408_rcv_rr(msg); |
| 3653 | break; |
| 3654 | case GSM48_PDISC_SMS: |
| 3655 | rc = gsm0411_rcv_sms(msg); |
| 3656 | break; |
| 3657 | case GSM48_PDISC_MM_GPRS: |
| 3658 | case GSM48_PDISC_SM_GPRS: |
| 3659 | fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n", |
| 3660 | pdisc); |
| 3661 | break; |
| 3662 | default: |
| 3663 | fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n", |
| 3664 | pdisc); |
| 3665 | break; |
| 3666 | } |
| 3667 | |
| 3668 | return rc; |
| 3669 | } |
| 3670 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3671 | /* Section 9.1.8 / Table 9.9 */ |
| 3672 | struct chreq { |
| 3673 | u_int8_t val; |
| 3674 | u_int8_t mask; |
| 3675 | enum chreq_type type; |
| 3676 | }; |
| 3677 | |
| 3678 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */ |
| 3679 | static const struct chreq chreq_type_neci1[] = { |
| 3680 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 3681 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F }, |
| 3682 | { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H }, |
| 3683 | { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL }, |
| 3684 | { 0xe0, 0xe0, CHREQ_T_SDCCH }, |
| 3685 | { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H }, |
| 3686 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 3687 | { 0x00, 0xf0, CHREQ_T_LOCATION_UPD }, |
| 3688 | { 0x10, 0xf0, CHREQ_T_SDCCH }, |
| 3689 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 3690 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 3691 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 3692 | }; |
| 3693 | |
| 3694 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */ |
| 3695 | static const struct chreq chreq_type_neci0[] = { |
| 3696 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 3697 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H }, |
| 3698 | { 0xe0, 0xe0, CHREQ_T_TCH_F }, |
| 3699 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 3700 | { 0x00, 0xe0, CHREQ_T_LOCATION_UPD }, |
| 3701 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 3702 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 3703 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 3704 | }; |
| 3705 | |
| 3706 | static const enum gsm_chan_t ctype_by_chreq[] = { |
| 3707 | [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F, |
| 3708 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F, |
| 3709 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H, |
| 3710 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H, |
| 3711 | [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH, |
| 3712 | [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F, |
| 3713 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 3714 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 3715 | [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH, |
| 3716 | [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH, |
| 3717 | [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F, |
| 3718 | [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F, |
| 3719 | }; |
| 3720 | |
| 3721 | static const enum gsm_chreq_reason_t reason_by_chreq[] = { |
| 3722 | [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG, |
| 3723 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL, |
| 3724 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL, |
| 3725 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL, |
| 3726 | [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER, |
| 3727 | [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER, |
| 3728 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 3729 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 3730 | [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD, |
| 3731 | [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG, |
| 3732 | [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG, |
| 3733 | [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG, |
| 3734 | }; |
| 3735 | |
| 3736 | enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 3737 | { |
| 3738 | int i; |
| 3739 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 3740 | |
| 3741 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 3742 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 3743 | if ((ra & chr->mask) == chr->val) |
| 3744 | return ctype_by_chreq[chr->type]; |
| 3745 | } |
| 3746 | fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra); |
| 3747 | return GSM_LCHAN_SDCCH; |
| 3748 | } |
| 3749 | |
| 3750 | enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 3751 | { |
| 3752 | int i; |
| 3753 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 3754 | |
| 3755 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 3756 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 3757 | if ((ra & chr->mask) == chr->val) |
| 3758 | return reason_by_chreq[chr->type]; |
| 3759 | } |
| 3760 | fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra); |
| 3761 | return GSM_CHREQ_REASON_OTHER; |
| 3762 | } |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3763 | |
| 3764 | /* dequeue messages to layer 4 */ |
| 3765 | int bsc_upqueue(struct gsm_network *net) |
| 3766 | { |
| 3767 | struct gsm_mncc *mncc; |
| 3768 | struct msgb *msg; |
| 3769 | int work = 0; |
| 3770 | |
| 3771 | if (net) |
| 3772 | while ((msg = msgb_dequeue(&net->upqueue))) { |
| 3773 | mncc = (struct gsm_mncc *)msg->data; |
| 3774 | if (net->mncc_recv) |
| 3775 | net->mncc_recv(net, mncc->msg_type, mncc); |
| 3776 | work = 1; /* work done */ |
| 3777 | } |
| 3778 | |
| 3779 | return work; |
| 3780 | } |