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 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | static int gsm48_rx_mm_status(struct msgb *msg) |
| 1512 | { |
| 1513 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1514 | |
| 1515 | DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]); |
| 1516 | |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | /* Receive a GSM 04.08 Mobility Management (MM) message */ |
| 1521 | static int gsm0408_rcv_mm(struct msgb *msg) |
| 1522 | { |
| 1523 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1524 | int rc = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1525 | |
| 1526 | switch (gh->msg_type & 0xbf) { |
| 1527 | case GSM48_MT_MM_LOC_UPD_REQUEST: |
| 1528 | DEBUGP(DMM, "LOCATION UPDATING REQUEST\n"); |
| 1529 | rc = mm_rx_loc_upd_req(msg); |
| 1530 | break; |
| 1531 | case GSM48_MT_MM_ID_RESP: |
| 1532 | rc = mm_rx_id_resp(msg); |
| 1533 | break; |
| 1534 | case GSM48_MT_MM_CM_SERV_REQ: |
| 1535 | rc = gsm48_rx_mm_serv_req(msg); |
| 1536 | break; |
| 1537 | case GSM48_MT_MM_STATUS: |
| 1538 | rc = gsm48_rx_mm_status(msg); |
| 1539 | break; |
| 1540 | case GSM48_MT_MM_TMSI_REALL_COMPL: |
| 1541 | DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n", |
| 1542 | msg->lchan->subscr ? |
| 1543 | msg->lchan->subscr->imsi : |
| 1544 | "unknown subscriber"); |
| 1545 | break; |
| 1546 | case GSM48_MT_MM_IMSI_DETACH_IND: |
| 1547 | rc = gsm48_rx_mm_imsi_detach_ind(msg); |
| 1548 | break; |
| 1549 | case GSM48_MT_MM_CM_REEST_REQ: |
| 1550 | DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n"); |
| 1551 | break; |
| 1552 | case GSM48_MT_MM_AUTH_RESP: |
| 1553 | DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n"); |
| 1554 | break; |
| 1555 | default: |
| 1556 | fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n", |
| 1557 | gh->msg_type); |
| 1558 | break; |
| 1559 | } |
| 1560 | |
| 1561 | return rc; |
| 1562 | } |
| 1563 | |
| 1564 | /* Receive a PAGING RESPONSE message from the MS */ |
| 1565 | static int gsm48_rr_rx_pag_resp(struct msgb *msg) |
| 1566 | { |
| 1567 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1568 | u_int8_t *classmark2_lv = gh->data + 1; |
| 1569 | u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv; |
| 1570 | u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK; |
| 1571 | char mi_string[MI_SIZE]; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1572 | struct gsm_subscriber *subscr = NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1573 | struct paging_signal_data sig_data; |
| 1574 | int rc = 0; |
| 1575 | |
| 1576 | mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv); |
| 1577 | DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n", |
| 1578 | mi_type, mi_string); |
| 1579 | switch (mi_type) { |
| 1580 | case GSM_MI_TYPE_TMSI: |
| 1581 | subscr = subscr_get_by_tmsi(mi_string); |
| 1582 | break; |
| 1583 | case GSM_MI_TYPE_IMSI: |
| 1584 | subscr = subscr_get_by_imsi(mi_string); |
| 1585 | break; |
| 1586 | } |
| 1587 | |
| 1588 | if (!subscr) { |
| 1589 | DEBUGP(DRR, "<- Can't find any subscriber for this ID\n"); |
| 1590 | /* FIXME: request id? close channel? */ |
| 1591 | return -EINVAL; |
| 1592 | } |
| 1593 | DEBUGP(DRR, "<- Channel was requested by %s\n", |
| 1594 | subscr->name ? subscr->name : subscr->imsi); |
| 1595 | |
| 1596 | subscr->classmark2_len = *classmark2_lv; |
| 1597 | memcpy(subscr->classmark2, classmark2_lv+1, *classmark2_lv); |
| 1598 | |
| 1599 | if (!msg->lchan->subscr) { |
| 1600 | msg->lchan->subscr = subscr; |
| 1601 | } else if (msg->lchan->subscr != subscr) { |
| 1602 | DEBUGP(DRR, "<- Channel already owned by someone else?\n"); |
| 1603 | subscr_put(subscr); |
| 1604 | return -EINVAL; |
| 1605 | } else { |
| 1606 | DEBUGP(DRR, "<- Channel already owned by us\n"); |
| 1607 | subscr_put(subscr); |
| 1608 | subscr = msg->lchan->subscr; |
| 1609 | } |
| 1610 | |
| 1611 | sig_data.subscr = subscr; |
| 1612 | sig_data.bts = msg->lchan->ts->trx->bts; |
| 1613 | sig_data.lchan = msg->lchan; |
| 1614 | |
| 1615 | dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data); |
Harald Welte | 876312f | 2009-06-10 11:21:55 +0800 | [diff] [blame] | 1616 | |
| 1617 | /* Stop paging on the bts we received the paging response */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1618 | paging_request_stop(msg->trx->bts, subscr, msg->lchan); |
| 1619 | |
| 1620 | /* FIXME: somehow signal the completion of the PAGING to |
| 1621 | * the entity that requested the paging */ |
| 1622 | |
| 1623 | return rc; |
| 1624 | } |
| 1625 | |
| 1626 | static int gsm48_rx_rr_classmark(struct msgb *msg) |
| 1627 | { |
| 1628 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1629 | struct gsm_subscriber *subscr = msg->lchan->subscr; |
| 1630 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 1631 | u_int8_t cm2_len, cm3_len = 0; |
| 1632 | u_int8_t *cm2, *cm3 = NULL; |
| 1633 | |
| 1634 | DEBUGP(DRR, "CLASSMARK CHANGE "); |
| 1635 | |
| 1636 | /* classmark 2 */ |
| 1637 | cm2_len = gh->data[0]; |
| 1638 | cm2 = &gh->data[1]; |
| 1639 | DEBUGPC(DRR, "CM2(len=%u) ", cm2_len); |
| 1640 | |
| 1641 | if (payload_len > cm2_len + 1) { |
| 1642 | /* we must have a classmark3 */ |
| 1643 | if (gh->data[cm2_len+1] != 0x20) { |
| 1644 | DEBUGPC(DRR, "ERR CM3 TAG\n"); |
| 1645 | return -EINVAL; |
| 1646 | } |
| 1647 | if (cm2_len > 3) { |
| 1648 | DEBUGPC(DRR, "CM2 too long!\n"); |
| 1649 | return -EINVAL; |
| 1650 | } |
| 1651 | |
| 1652 | cm3_len = gh->data[cm2_len+2]; |
| 1653 | cm3 = &gh->data[cm2_len+3]; |
| 1654 | if (cm3_len > 14) { |
| 1655 | DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len); |
| 1656 | return -EINVAL; |
| 1657 | } |
| 1658 | DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len); |
| 1659 | } |
| 1660 | if (subscr) { |
| 1661 | subscr->classmark2_len = cm2_len; |
| 1662 | memcpy(subscr->classmark2, cm2, cm2_len); |
| 1663 | if (cm3) { |
| 1664 | subscr->classmark3_len = cm3_len; |
| 1665 | memcpy(subscr->classmark3, cm3, cm3_len); |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | /* FIXME: store the classmark2/3 values with the equipment register */ |
| 1670 | |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | static int gsm48_rx_rr_status(struct msgb *msg) |
| 1675 | { |
| 1676 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1677 | |
| 1678 | DEBUGP(DRR, "STATUS rr_cause = %s\n", |
| 1679 | rr_cause_name(gh->data[0])); |
| 1680 | |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
| 1684 | static int gsm48_rx_rr_meas_rep(struct msgb *msg) |
| 1685 | { |
| 1686 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1687 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 1688 | static struct gsm_meas_rep meas_rep; |
| 1689 | |
| 1690 | DEBUGP(DRR, "MEASUREMENT REPORT "); |
| 1691 | parse_meas_rep(&meas_rep, gh->data, payload_len); |
| 1692 | if (meas_rep.flags & MEAS_REP_F_DTX) |
| 1693 | DEBUGPC(DRR, "DTX "); |
| 1694 | if (meas_rep.flags & MEAS_REP_F_BA1) |
| 1695 | DEBUGPC(DRR, "BA1 "); |
| 1696 | if (!(meas_rep.flags & MEAS_REP_F_VALID)) |
| 1697 | DEBUGPC(DRR, "NOT VALID "); |
| 1698 | else |
| 1699 | DEBUGPC(DRR, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ", |
| 1700 | meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub, |
| 1701 | meas_rep.rxqual_sub); |
| 1702 | |
| 1703 | DEBUGPC(DRR, "NUM_NEIGH=%u\n", meas_rep.num_cell); |
| 1704 | |
| 1705 | /* FIXME: put the results somwhere */ |
| 1706 | |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | /* Receive a GSM 04.08 Radio Resource (RR) message */ |
| 1711 | static int gsm0408_rcv_rr(struct msgb *msg) |
| 1712 | { |
| 1713 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1714 | int rc = 0; |
| 1715 | |
| 1716 | switch (gh->msg_type) { |
| 1717 | case GSM48_MT_RR_CLSM_CHG: |
| 1718 | rc = gsm48_rx_rr_classmark(msg); |
| 1719 | break; |
| 1720 | case GSM48_MT_RR_GPRS_SUSP_REQ: |
| 1721 | DEBUGP(DRR, "GRPS SUSPEND REQUEST\n"); |
| 1722 | break; |
| 1723 | case GSM48_MT_RR_PAG_RESP: |
| 1724 | rc = gsm48_rr_rx_pag_resp(msg); |
| 1725 | break; |
| 1726 | case GSM48_MT_RR_CHAN_MODE_MODIF_ACK: |
| 1727 | DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n"); |
| 1728 | rc = rsl_chan_mode_modify_req(msg->lchan); |
| 1729 | break; |
| 1730 | case GSM48_MT_RR_STATUS: |
| 1731 | rc = gsm48_rx_rr_status(msg); |
| 1732 | break; |
| 1733 | case GSM48_MT_RR_MEAS_REP: |
| 1734 | rc = gsm48_rx_rr_meas_rep(msg); |
| 1735 | break; |
| 1736 | default: |
| 1737 | fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n", |
| 1738 | gh->msg_type); |
| 1739 | break; |
| 1740 | } |
| 1741 | |
| 1742 | return rc; |
| 1743 | } |
| 1744 | |
| 1745 | /* 7.1.7 and 9.1.7 Channel release*/ |
| 1746 | int gsm48_send_rr_release(struct gsm_lchan *lchan) |
| 1747 | { |
| 1748 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1749 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1750 | u_int8_t *cause; |
| 1751 | |
| 1752 | msg->lchan = lchan; |
| 1753 | gh->proto_discr = GSM48_PDISC_RR; |
| 1754 | gh->msg_type = GSM48_MT_RR_CHAN_REL; |
| 1755 | |
| 1756 | cause = msgb_put(msg, 1); |
| 1757 | cause[0] = GSM48_RR_CAUSE_NORMAL; |
| 1758 | |
| 1759 | DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n", |
| 1760 | lchan->nr, lchan->type); |
| 1761 | |
| 1762 | return gsm48_sendmsg(msg); |
| 1763 | } |
| 1764 | |
| 1765 | /* Call Control */ |
| 1766 | |
| 1767 | /* The entire call control code is written in accordance with Figure 7.10c |
| 1768 | * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE |
| 1769 | * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY |
| 1770 | * it for voice */ |
| 1771 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1772 | static void new_cc_state(struct gsm_trans *trans, int state) |
| 1773 | { |
| 1774 | if (state > 31 || state < 0) |
| 1775 | return; |
| 1776 | |
| 1777 | DEBUGP(DCC, "new state %s -> %s\n", |
| 1778 | cc_state_names[trans->state], cc_state_names[state]); |
| 1779 | |
| 1780 | trans->state = state; |
| 1781 | } |
| 1782 | |
| 1783 | static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1784 | { |
| 1785 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1786 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1787 | u_int8_t *cause, *call_state; |
| 1788 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1789 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 1790 | msg->lchan = trans->lchan; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1791 | gh->msg_type = GSM48_MT_CC_STATUS; |
| 1792 | |
| 1793 | cause = msgb_put(msg, 3); |
| 1794 | cause[0] = 2; |
| 1795 | cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER; |
| 1796 | cause[2] = 0x80 | 30; /* response to status inquiry */ |
| 1797 | |
| 1798 | call_state = msgb_put(msg, 1); |
| 1799 | call_state[0] = 0xc0 | 0x00; |
| 1800 | |
| 1801 | return gsm48_sendmsg(msg); |
| 1802 | } |
| 1803 | |
| 1804 | static int gsm48_tx_simple(struct gsm_lchan *lchan, |
| 1805 | u_int8_t pdisc, u_int8_t msg_type) |
| 1806 | { |
| 1807 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1808 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1809 | |
| 1810 | msg->lchan = lchan; |
| 1811 | |
| 1812 | gh->proto_discr = pdisc; |
| 1813 | gh->msg_type = msg_type; |
| 1814 | |
| 1815 | return gsm48_sendmsg(msg); |
| 1816 | } |
| 1817 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1818 | static void gsm48_stop_cc_timer(struct gsm_trans *trans) |
| 1819 | { |
| 1820 | if (bsc_timer_pending(&trans->cc_timer)) { |
| 1821 | DEBUGP(DCC, "stopping pending timer T%x\n", trans->Tcurrent); |
| 1822 | bsc_del_timer(&trans->cc_timer); |
| 1823 | trans->Tcurrent = 0; |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans, |
| 1828 | int msg_type, struct gsm_mncc *mncc) |
| 1829 | { |
| 1830 | struct msgb *msg; |
| 1831 | |
| 1832 | if (trans) |
| 1833 | if (trans->lchan) |
| 1834 | DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) " |
| 1835 | "Sending '%s' to MNCC.\n", |
| 1836 | trans->lchan->ts->trx->bts->nr, |
| 1837 | trans->lchan->ts->trx->nr, |
| 1838 | trans->lchan->ts->nr, trans->transaction_id, |
| 1839 | (trans->subscr)?(trans->subscr->extension):"-", |
| 1840 | get_mncc_name(msg_type)); |
| 1841 | else |
| 1842 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) " |
| 1843 | "Sending '%s' to MNCC.\n", |
| 1844 | (trans->subscr)?(trans->subscr->extension):"-", |
| 1845 | get_mncc_name(msg_type)); |
| 1846 | else |
| 1847 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) " |
| 1848 | "Sending '%s' to MNCC.\n", get_mncc_name(msg_type)); |
| 1849 | |
| 1850 | mncc->msg_type = msg_type; |
| 1851 | |
| 1852 | msg = msgb_alloc(sizeof(struct gsm_mncc)); |
| 1853 | if (!msg) |
| 1854 | return -ENOMEM; |
| 1855 | memcpy(msg->data, mncc, sizeof(struct gsm_mncc)); |
| 1856 | msgb_enqueue(&net->upqueue, msg); |
| 1857 | |
| 1858 | return 0; |
| 1859 | } |
| 1860 | |
| 1861 | int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans, |
| 1862 | u_int32_t callref, int location, int value) |
| 1863 | { |
| 1864 | struct gsm_mncc rel; |
| 1865 | |
Harald Welte | cb0595f | 2009-06-12 01:54:08 +0800 | [diff] [blame] | 1866 | memset(&rel, 0, sizeof(rel)); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1867 | rel.callref = callref; |
Andreas Eversberg | b992a8a | 2009-06-14 22:14:12 +0800 | [diff] [blame] | 1868 | mncc_set_cause(&rel, location, value); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1869 | return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel); |
| 1870 | } |
| 1871 | |
| 1872 | void free_trans(struct gsm_trans *trans) |
| 1873 | { |
| 1874 | struct gsm_bts *bts; |
| 1875 | |
| 1876 | gsm48_stop_cc_timer(trans); |
| 1877 | |
| 1878 | /* send release to L4, if callref still exists */ |
| 1879 | if (trans->callref) { |
| 1880 | /* Ressource unavailable */ |
Andreas Eversberg | b992a8a | 2009-06-14 22:14:12 +0800 | [diff] [blame] | 1881 | mncc_release_ind(trans->network, trans, trans->callref, |
| 1882 | GSM48_CAUSE_LOC_PRN_S_LU, |
| 1883 | GSM48_CC_CAUSE_RESOURCE_UNAVAIL); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1884 | if (trans->state != GSM_CSTATE_NULL) |
| 1885 | new_cc_state(trans, GSM_CSTATE_NULL); |
| 1886 | } |
| 1887 | |
| 1888 | if (!trans->lchan && trans->subscr && trans->subscr->net) { |
| 1889 | /* Stop paging on all bts' */ |
| 1890 | bts = NULL; |
| 1891 | do { |
| 1892 | bts = gsm_bts_by_lac(trans->subscr->net, |
| 1893 | trans->subscr->lac, bts); |
| 1894 | if (!bts) |
| 1895 | break; |
| 1896 | /* Stop paging */ |
| 1897 | paging_request_stop(bts, trans->subscr, NULL); |
| 1898 | } while (1); |
| 1899 | } |
| 1900 | |
| 1901 | if (trans->lchan) { |
| 1902 | trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref); |
| 1903 | put_lchan(trans->lchan); |
| 1904 | } |
| 1905 | |
| 1906 | if (trans->subscr) |
| 1907 | subscr_put(trans->subscr); |
| 1908 | |
| 1909 | if (trans->state != GSM_CSTATE_NULL) |
| 1910 | new_cc_state(trans, GSM_CSTATE_NULL); |
| 1911 | |
| 1912 | llist_del(&trans->entry); |
| 1913 | |
| 1914 | free(trans); |
| 1915 | } |
| 1916 | |
| 1917 | static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg); |
| 1918 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1919 | /* call-back from paging the B-end of the connection */ |
| 1920 | static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event, |
| 1921 | struct msgb *msg, void *_lchan, void *param) |
| 1922 | { |
| 1923 | struct gsm_lchan *lchan = _lchan; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1924 | struct gsm_subscriber *subscr = param; |
| 1925 | struct gsm_trans *transt, *tmp; |
| 1926 | struct gsm_network *net; |
| 1927 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1928 | if (hooknum != GSM_HOOK_RR_PAGING) |
| 1929 | return -EINVAL; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1930 | |
| 1931 | if (!subscr) |
| 1932 | return -EINVAL; |
| 1933 | net = subscr->net; |
| 1934 | if (!net) { |
| 1935 | DEBUGP(DCC, "Error Network not set!\n"); |
| 1936 | return -EINVAL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1937 | } |
| 1938 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 1939 | /* check all tranactions (without lchan) for subscriber */ |
| 1940 | llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) { |
| 1941 | if (transt->subscr != subscr || transt->lchan) |
| 1942 | continue; |
| 1943 | switch (event) { |
| 1944 | case GSM_PAGING_SUCCEEDED: |
| 1945 | if (!lchan) // paranoid |
| 1946 | break; |
| 1947 | DEBUGP(DCC, "Paging subscr %s succeeded!\n", |
| 1948 | subscr->extension); |
| 1949 | /* Assign lchan */ |
| 1950 | if (!transt->lchan) { |
| 1951 | transt->lchan = lchan; |
| 1952 | use_lchan(lchan); |
| 1953 | } |
| 1954 | /* send SETUP request to called party */ |
| 1955 | gsm48_cc_tx_setup(transt, &transt->cc_msg); |
| 1956 | if (is_ipaccess_bts(lchan->ts->trx->bts)) |
| 1957 | rsl_ipacc_bind(lchan); |
| 1958 | break; |
| 1959 | case GSM_PAGING_EXPIRED: |
| 1960 | DEBUGP(DCC, "Paging subscr %s expired!\n", |
| 1961 | subscr->extension); |
| 1962 | /* Temporarily out of order */ |
| 1963 | mncc_release_ind(transt->network, transt, transt->callref, |
Andreas Eversberg | b992a8a | 2009-06-14 22:14:12 +0800 | [diff] [blame] | 1964 | GSM48_CAUSE_LOC_PRN_S_LU, |
| 1965 | GSM48_CC_CAUSE_DEST_OOO); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 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, |
Andreas Eversberg | b992a8a | 2009-06-14 22:14:12 +0800 | [diff] [blame] | 2273 | GSM48_CAUSE_LOC_PRN_S_LU, |
| 2274 | GSM48_CC_CAUSE_RESOURCE_UNAVAIL); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2275 | trans->callref = 0; |
| 2276 | free_trans(trans); |
| 2277 | return rc; |
| 2278 | } |
| 2279 | |
| 2280 | /* Get free transaction_id */ |
| 2281 | llist_for_each_entry(transt, &trans->network->trans_list, entry) { |
| 2282 | /* Transaction of our lchan? */ |
| 2283 | if (transt->lchan == trans->lchan && |
| 2284 | transt->transaction_id != 0xff) |
| 2285 | trans_id_mask |= (1 << (transt->transaction_id >> 4)); |
| 2286 | } |
| 2287 | /* Assign free transaction ID */ |
| 2288 | if ((trans_id_mask & 0x007f) == 0x7f) { |
| 2289 | /* no free transaction ID */ |
| 2290 | rc = mncc_release_ind(trans->network, trans, trans->callref, |
Andreas Eversberg | b992a8a | 2009-06-14 22:14:12 +0800 | [diff] [blame] | 2291 | GSM48_CAUSE_LOC_PRN_S_LU, |
| 2292 | GSM48_CC_CAUSE_RESOURCE_UNAVAIL); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2293 | trans->callref = 0; |
| 2294 | free_trans(trans); |
| 2295 | return rc; |
| 2296 | } |
| 2297 | for (i = 0; i < 7; i++) { |
| 2298 | if ((trans_id_mask & (1 << i)) == 0) { |
| 2299 | trans->transaction_id = i << 4; /* flag = 0 */ |
| 2300 | break; |
| 2301 | } |
| 2302 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2303 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2304 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2305 | msg->lchan = trans->lchan; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2306 | gh->msg_type = GSM48_MT_CC_SETUP; |
| 2307 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2308 | gsm48_start_cc_timer(trans, 0x303, GSM48_T303); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2309 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2310 | /* bearer capability */ |
| 2311 | if (setup->fields & MNCC_F_BEARER_CAP) |
| 2312 | encode_bearer_cap(msg, 0, &setup->bearer_cap); |
| 2313 | /* facility */ |
| 2314 | if (setup->fields & MNCC_F_FACILITY) |
| 2315 | encode_facility(msg, 0, &setup->facility); |
| 2316 | /* progress */ |
| 2317 | if (setup->fields & MNCC_F_PROGRESS) |
| 2318 | encode_progress(msg, 0, &setup->progress); |
| 2319 | /* calling party BCD number */ |
| 2320 | if (setup->fields & MNCC_F_CALLING) |
| 2321 | encode_calling(msg, &setup->calling); |
| 2322 | /* called party BCD number */ |
| 2323 | if (setup->fields & MNCC_F_CALLED) |
| 2324 | encode_called(msg, &setup->called); |
| 2325 | /* user-user */ |
| 2326 | if (setup->fields & MNCC_F_USERUSER) |
| 2327 | encode_useruser(msg, 0, &setup->useruser); |
| 2328 | /* redirecting party BCD number */ |
| 2329 | if (setup->fields & MNCC_F_REDIRECTING) |
| 2330 | encode_redirecting(msg, &setup->redirecting); |
| 2331 | /* signal */ |
| 2332 | if (setup->fields & MNCC_F_SIGNAL) |
| 2333 | encode_signal(msg, setup->signal); |
| 2334 | |
| 2335 | new_cc_state(trans, GSM_CSTATE_CALL_PRESENT); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2336 | |
| 2337 | return gsm48_sendmsg(msg); |
| 2338 | } |
| 2339 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2340 | static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg) |
| 2341 | { |
| 2342 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2343 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2344 | struct tlv_parsed tp; |
| 2345 | struct gsm_mncc call_conf; |
| 2346 | |
| 2347 | gsm48_stop_cc_timer(trans); |
| 2348 | gsm48_start_cc_timer(trans, 0x310, GSM48_T310); |
| 2349 | |
| 2350 | memset(&call_conf, 0, sizeof(struct gsm_mncc)); |
| 2351 | call_conf.callref = trans->callref; |
| 2352 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2353 | #if 0 |
| 2354 | /* repeat */ |
| 2355 | if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR)) |
| 2356 | call_conf.repeat = 1; |
| 2357 | if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ)) |
| 2358 | call_conf.repeat = 2; |
| 2359 | #endif |
| 2360 | /* bearer capability */ |
| 2361 | if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { |
| 2362 | call_conf.fields |= MNCC_F_BEARER_CAP; |
| 2363 | decode_bearer_cap(&call_conf.bearer_cap, |
| 2364 | TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1); |
| 2365 | } |
| 2366 | /* cause */ |
| 2367 | if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) { |
| 2368 | call_conf.fields |= MNCC_F_CAUSE; |
| 2369 | decode_cause(&call_conf.cause, |
| 2370 | TLVP_VAL(&tp, GSM48_IE_CAUSE)-1); |
| 2371 | } |
| 2372 | /* cc cap */ |
| 2373 | if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) { |
| 2374 | call_conf.fields |= MNCC_F_CCCAP; |
| 2375 | decode_cccap(&call_conf.cccap, |
| 2376 | TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1); |
| 2377 | } |
| 2378 | |
| 2379 | new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF); |
| 2380 | |
| 2381 | return mncc_recvmsg(trans->network, trans, MNCC_CALL_CONF_IND, &call_conf); |
| 2382 | } |
| 2383 | |
| 2384 | static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg) |
| 2385 | { |
| 2386 | struct gsm_mncc *proceeding = arg; |
| 2387 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2388 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2389 | |
| 2390 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2391 | msg->lchan = trans->lchan; |
| 2392 | gh->msg_type = GSM48_MT_CC_CALL_PROC; |
| 2393 | |
| 2394 | new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC); |
| 2395 | |
| 2396 | /* bearer capability */ |
| 2397 | if (proceeding->fields & MNCC_F_BEARER_CAP) |
| 2398 | encode_bearer_cap(msg, 0, &proceeding->bearer_cap); |
| 2399 | /* facility */ |
| 2400 | if (proceeding->fields & MNCC_F_FACILITY) |
| 2401 | encode_facility(msg, 0, &proceeding->facility); |
| 2402 | /* progress */ |
| 2403 | if (proceeding->fields & MNCC_F_PROGRESS) |
| 2404 | encode_progress(msg, 0, &proceeding->progress); |
| 2405 | |
| 2406 | return gsm48_sendmsg(msg); |
| 2407 | } |
| 2408 | |
| 2409 | static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg) |
| 2410 | { |
| 2411 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2412 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2413 | struct tlv_parsed tp; |
| 2414 | struct gsm_mncc alerting; |
| 2415 | |
| 2416 | gsm48_stop_cc_timer(trans); |
| 2417 | gsm48_start_cc_timer(trans, 0x301, GSM48_T301); |
| 2418 | |
| 2419 | memset(&alerting, 0, sizeof(struct gsm_mncc)); |
| 2420 | alerting.callref = trans->callref; |
| 2421 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2422 | /* facility */ |
| 2423 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2424 | alerting.fields |= MNCC_F_FACILITY; |
| 2425 | decode_facility(&alerting.facility, |
| 2426 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2427 | } |
| 2428 | |
| 2429 | /* progress */ |
| 2430 | if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) { |
| 2431 | alerting.fields |= MNCC_F_PROGRESS; |
| 2432 | decode_progress(&alerting.progress, |
| 2433 | TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1); |
| 2434 | } |
| 2435 | /* ss-version */ |
| 2436 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2437 | alerting.fields |= MNCC_F_SSVERSION; |
| 2438 | decode_ssversion(&alerting.ssversion, |
| 2439 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2440 | } |
| 2441 | |
| 2442 | new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED); |
| 2443 | |
| 2444 | return mncc_recvmsg(trans->network, trans, MNCC_ALERT_IND, &alerting); |
| 2445 | } |
| 2446 | |
| 2447 | static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg) |
| 2448 | { |
| 2449 | struct gsm_mncc *alerting = arg; |
| 2450 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2451 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2452 | |
| 2453 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2454 | msg->lchan = trans->lchan; |
| 2455 | gh->msg_type = GSM48_MT_CC_ALERTING; |
| 2456 | |
| 2457 | /* facility */ |
| 2458 | if (alerting->fields & MNCC_F_FACILITY) |
| 2459 | encode_facility(msg, 0, &alerting->facility); |
| 2460 | /* progress */ |
| 2461 | if (alerting->fields & MNCC_F_PROGRESS) |
| 2462 | encode_progress(msg, 0, &alerting->progress); |
| 2463 | /* user-user */ |
| 2464 | if (alerting->fields & MNCC_F_USERUSER) |
| 2465 | encode_useruser(msg, 0, &alerting->useruser); |
| 2466 | |
| 2467 | new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED); |
| 2468 | |
| 2469 | return gsm48_sendmsg(msg); |
| 2470 | } |
| 2471 | |
| 2472 | static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg) |
| 2473 | { |
| 2474 | struct gsm_mncc *progress = arg; |
| 2475 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2476 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2477 | |
| 2478 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2479 | msg->lchan = trans->lchan; |
| 2480 | gh->msg_type = GSM48_MT_CC_PROGRESS; |
| 2481 | |
| 2482 | /* progress */ |
| 2483 | encode_progress(msg, 1, &progress->progress); |
| 2484 | /* user-user */ |
| 2485 | if (progress->fields & MNCC_F_USERUSER) |
| 2486 | encode_useruser(msg, 0, &progress->useruser); |
| 2487 | |
| 2488 | return gsm48_sendmsg(msg); |
| 2489 | } |
| 2490 | |
| 2491 | static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg) |
| 2492 | { |
| 2493 | struct gsm_mncc *connect = arg; |
| 2494 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2495 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2496 | |
| 2497 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2498 | msg->lchan = trans->lchan; |
| 2499 | gh->msg_type = GSM48_MT_CC_CONNECT; |
| 2500 | |
| 2501 | gsm48_stop_cc_timer(trans); |
| 2502 | gsm48_start_cc_timer(trans, 0x313, GSM48_T313); |
| 2503 | |
| 2504 | /* facility */ |
| 2505 | if (connect->fields & MNCC_F_FACILITY) |
| 2506 | encode_facility(msg, 0, &connect->facility); |
| 2507 | /* progress */ |
| 2508 | if (connect->fields & MNCC_F_PROGRESS) |
| 2509 | encode_progress(msg, 0, &connect->progress); |
| 2510 | /* connected number */ |
| 2511 | if (connect->fields & MNCC_F_CONNECTED) |
| 2512 | encode_connected(msg, &connect->connected); |
| 2513 | /* user-user */ |
| 2514 | if (connect->fields & MNCC_F_USERUSER) |
| 2515 | encode_useruser(msg, 0, &connect->useruser); |
| 2516 | |
| 2517 | new_cc_state(trans, GSM_CSTATE_CONNECT_IND); |
| 2518 | |
| 2519 | return gsm48_sendmsg(msg); |
| 2520 | } |
| 2521 | |
| 2522 | static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg) |
| 2523 | { |
| 2524 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2525 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2526 | struct tlv_parsed tp; |
| 2527 | struct gsm_mncc connect; |
| 2528 | |
| 2529 | gsm48_stop_cc_timer(trans); |
| 2530 | |
| 2531 | memset(&connect, 0, sizeof(struct gsm_mncc)); |
| 2532 | connect.callref = trans->callref; |
| 2533 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2534 | /* use subscriber as connected party number */ |
| 2535 | if (trans->subscr) { |
| 2536 | connect.fields |= MNCC_F_CONNECTED; |
| 2537 | strncpy(connect.connected.number, trans->subscr->extension, |
| 2538 | sizeof(connect.connected.number)-1); |
| 2539 | } |
| 2540 | /* facility */ |
| 2541 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2542 | connect.fields |= MNCC_F_FACILITY; |
| 2543 | decode_facility(&connect.facility, |
| 2544 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2545 | } |
| 2546 | /* user-user */ |
| 2547 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 2548 | connect.fields |= MNCC_F_USERUSER; |
| 2549 | decode_useruser(&connect.useruser, |
| 2550 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 2551 | } |
| 2552 | /* ss-version */ |
| 2553 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2554 | connect.fields |= MNCC_F_SSVERSION; |
| 2555 | decode_ssversion(&connect.ssversion, |
| 2556 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2557 | } |
| 2558 | |
| 2559 | new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST); |
| 2560 | |
| 2561 | return mncc_recvmsg(trans->network, trans, MNCC_SETUP_CNF, &connect); |
| 2562 | } |
| 2563 | |
| 2564 | |
| 2565 | static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg) |
| 2566 | { |
| 2567 | struct gsm_mncc connect_ack; |
| 2568 | |
| 2569 | gsm48_stop_cc_timer(trans); |
| 2570 | |
| 2571 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 2572 | |
| 2573 | memset(&connect_ack, 0, sizeof(struct gsm_mncc)); |
| 2574 | connect_ack.callref = trans->callref; |
| 2575 | return mncc_recvmsg(trans->network, trans, MNCC_SETUP_COMPL_IND, |
| 2576 | &connect_ack); |
| 2577 | } |
| 2578 | |
| 2579 | static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg) |
| 2580 | { |
| 2581 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2582 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2583 | |
| 2584 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2585 | msg->lchan = trans->lchan; |
| 2586 | gh->msg_type = GSM48_MT_CC_CONNECT_ACK; |
| 2587 | |
| 2588 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 2589 | |
| 2590 | return gsm48_sendmsg(msg); |
| 2591 | } |
| 2592 | |
| 2593 | static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg) |
| 2594 | { |
| 2595 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2596 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2597 | struct tlv_parsed tp; |
| 2598 | struct gsm_mncc disc; |
| 2599 | |
| 2600 | gsm48_stop_cc_timer(trans); |
| 2601 | |
| 2602 | new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ); |
| 2603 | |
| 2604 | memset(&disc, 0, sizeof(struct gsm_mncc)); |
| 2605 | disc.callref = trans->callref; |
| 2606 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0); |
| 2607 | /* cause */ |
| 2608 | if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) { |
| 2609 | disc.fields |= MNCC_F_CAUSE; |
| 2610 | decode_cause(&disc.cause, |
| 2611 | TLVP_VAL(&tp, GSM48_IE_CAUSE)-1); |
| 2612 | } |
| 2613 | /* facility */ |
| 2614 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2615 | disc.fields |= MNCC_F_FACILITY; |
| 2616 | decode_facility(&disc.facility, |
| 2617 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2618 | } |
| 2619 | /* user-user */ |
| 2620 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 2621 | disc.fields |= MNCC_F_USERUSER; |
| 2622 | decode_useruser(&disc.useruser, |
| 2623 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 2624 | } |
| 2625 | /* ss-version */ |
| 2626 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2627 | disc.fields |= MNCC_F_SSVERSION; |
| 2628 | decode_ssversion(&disc.ssversion, |
| 2629 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2630 | } |
| 2631 | |
| 2632 | return mncc_recvmsg(trans->network, trans, MNCC_DISC_IND, &disc); |
| 2633 | |
| 2634 | } |
| 2635 | |
Harald Welte | bbc636a | 2009-06-11 14:23:20 +0800 | [diff] [blame] | 2636 | static struct gsm_mncc_cause default_cause = { |
| 2637 | .location = GSM48_CAUSE_LOC_PRN_S_LU, |
| 2638 | .coding = 0, |
| 2639 | .rec = 0, |
| 2640 | .rec_val = 0, |
| 2641 | .value = GSM48_CC_CAUSE_NORMAL_UNSPEC, |
| 2642 | .diag_len = 0, |
| 2643 | .diag = { 0 }, |
| 2644 | }; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 2645 | |
| 2646 | static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg) |
| 2647 | { |
| 2648 | struct gsm_mncc *disc = arg; |
| 2649 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2650 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2651 | |
| 2652 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2653 | msg->lchan = trans->lchan; |
| 2654 | gh->msg_type = GSM48_MT_CC_DISCONNECT; |
| 2655 | |
| 2656 | gsm48_stop_cc_timer(trans); |
| 2657 | gsm48_start_cc_timer(trans, 0x306, GSM48_T306); |
| 2658 | |
| 2659 | /* cause */ |
| 2660 | if (disc->fields & MNCC_F_CAUSE) |
| 2661 | encode_cause(msg, 1, &disc->cause); |
| 2662 | else |
| 2663 | encode_cause(msg, 1, &default_cause); |
| 2664 | |
| 2665 | /* facility */ |
| 2666 | if (disc->fields & MNCC_F_FACILITY) |
| 2667 | encode_facility(msg, 0, &disc->facility); |
| 2668 | /* progress */ |
| 2669 | if (disc->fields & MNCC_F_PROGRESS) |
| 2670 | encode_progress(msg, 0, &disc->progress); |
| 2671 | /* user-user */ |
| 2672 | if (disc->fields & MNCC_F_USERUSER) |
| 2673 | encode_useruser(msg, 0, &disc->useruser); |
| 2674 | |
| 2675 | /* store disconnect cause for T306 expiry */ |
| 2676 | memcpy(&trans->cc_msg, disc, sizeof(struct gsm_mncc)); |
| 2677 | |
| 2678 | new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND); |
| 2679 | |
| 2680 | return gsm48_sendmsg(msg); |
| 2681 | } |
| 2682 | |
| 2683 | static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg) |
| 2684 | { |
| 2685 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2686 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2687 | struct tlv_parsed tp; |
| 2688 | struct gsm_mncc rel; |
| 2689 | int rc; |
| 2690 | |
| 2691 | gsm48_stop_cc_timer(trans); |
| 2692 | |
| 2693 | memset(&rel, 0, sizeof(struct gsm_mncc)); |
| 2694 | rel.callref = trans->callref; |
| 2695 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2696 | /* cause */ |
| 2697 | if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) { |
| 2698 | rel.fields |= MNCC_F_CAUSE; |
| 2699 | decode_cause(&rel.cause, |
| 2700 | TLVP_VAL(&tp, GSM48_IE_CAUSE)-1); |
| 2701 | } |
| 2702 | /* facility */ |
| 2703 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2704 | rel.fields |= MNCC_F_FACILITY; |
| 2705 | decode_facility(&rel.facility, |
| 2706 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2707 | } |
| 2708 | /* user-user */ |
| 2709 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 2710 | rel.fields |= MNCC_F_USERUSER; |
| 2711 | decode_useruser(&rel.useruser, |
| 2712 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 2713 | } |
| 2714 | /* ss-version */ |
| 2715 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2716 | rel.fields |= MNCC_F_SSVERSION; |
| 2717 | decode_ssversion(&rel.ssversion, |
| 2718 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2719 | } |
| 2720 | |
| 2721 | if (trans->state == GSM_CSTATE_RELEASE_REQ) { |
| 2722 | /* release collision 5.4.5 */ |
| 2723 | rc = mncc_recvmsg(trans->network, trans, MNCC_REL_CNF, &rel); |
| 2724 | } else { |
| 2725 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC | trans->transaction_id, |
| 2726 | GSM48_MT_CC_RELEASE_COMPL); |
| 2727 | rc = mncc_recvmsg(trans->network, trans, MNCC_REL_IND, &rel); |
| 2728 | } |
| 2729 | |
| 2730 | new_cc_state(trans, GSM_CSTATE_NULL); |
| 2731 | |
| 2732 | trans->callref = 0; |
| 2733 | free_trans(trans); |
| 2734 | |
| 2735 | return rc; |
| 2736 | } |
| 2737 | |
| 2738 | static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg) |
| 2739 | { |
| 2740 | struct gsm_mncc *rel = arg; |
| 2741 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2742 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2743 | |
| 2744 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2745 | msg->lchan = trans->lchan; |
| 2746 | gh->msg_type = GSM48_MT_CC_RELEASE; |
| 2747 | |
| 2748 | trans->callref = 0; |
| 2749 | |
| 2750 | gsm48_stop_cc_timer(trans); |
| 2751 | gsm48_start_cc_timer(trans, 0x308, GSM48_T308); |
| 2752 | |
| 2753 | /* cause */ |
| 2754 | if (rel->fields & MNCC_F_CAUSE) |
| 2755 | encode_cause(msg, 0, &rel->cause); |
| 2756 | /* facility */ |
| 2757 | if (rel->fields & MNCC_F_FACILITY) |
| 2758 | encode_facility(msg, 0, &rel->facility); |
| 2759 | /* user-user */ |
| 2760 | if (rel->fields & MNCC_F_USERUSER) |
| 2761 | encode_useruser(msg, 0, &rel->useruser); |
| 2762 | |
| 2763 | trans->T308_second = 0; |
| 2764 | memcpy(&trans->cc_msg, rel, sizeof(struct gsm_mncc)); |
| 2765 | |
| 2766 | if (trans->state != GSM_CSTATE_RELEASE_REQ) |
| 2767 | new_cc_state(trans, GSM_CSTATE_RELEASE_REQ); |
| 2768 | |
| 2769 | return gsm48_sendmsg(msg); |
| 2770 | } |
| 2771 | |
| 2772 | static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg) |
| 2773 | { |
| 2774 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2775 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2776 | struct tlv_parsed tp; |
| 2777 | struct gsm_mncc rel; |
| 2778 | int rc = 0; |
| 2779 | |
| 2780 | gsm48_stop_cc_timer(trans); |
| 2781 | |
| 2782 | memset(&rel, 0, sizeof(struct gsm_mncc)); |
| 2783 | rel.callref = trans->callref; |
| 2784 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2785 | /* cause */ |
| 2786 | if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) { |
| 2787 | rel.fields |= MNCC_F_CAUSE; |
| 2788 | decode_cause(&rel.cause, |
| 2789 | TLVP_VAL(&tp, GSM48_IE_CAUSE)-1); |
| 2790 | } |
| 2791 | /* facility */ |
| 2792 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2793 | rel.fields |= MNCC_F_FACILITY; |
| 2794 | decode_facility(&rel.facility, |
| 2795 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2796 | } |
| 2797 | /* user-user */ |
| 2798 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 2799 | rel.fields |= MNCC_F_USERUSER; |
| 2800 | decode_useruser(&rel.useruser, |
| 2801 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 2802 | } |
| 2803 | /* ss-version */ |
| 2804 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2805 | rel.fields |= MNCC_F_SSVERSION; |
| 2806 | decode_ssversion(&rel.ssversion, |
| 2807 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2808 | } |
| 2809 | |
| 2810 | if (trans->callref) { |
| 2811 | switch (trans->state) { |
| 2812 | case GSM_CSTATE_CALL_PRESENT: |
| 2813 | rc = mncc_recvmsg(trans->network, trans, |
| 2814 | MNCC_REJ_IND, &rel); |
| 2815 | break; |
| 2816 | case GSM_CSTATE_RELEASE_REQ: |
| 2817 | rc = mncc_recvmsg(trans->network, trans, |
| 2818 | MNCC_REL_CNF, &rel); |
| 2819 | break; |
| 2820 | default: |
| 2821 | rc = mncc_recvmsg(trans->network, trans, |
| 2822 | MNCC_REL_IND, &rel); |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | trans->callref = 0; |
| 2827 | free_trans(trans); |
| 2828 | |
| 2829 | return rc; |
| 2830 | } |
| 2831 | |
| 2832 | static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg) |
| 2833 | { |
| 2834 | struct gsm_mncc *rel = arg; |
| 2835 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2836 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2837 | |
| 2838 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2839 | msg->lchan = trans->lchan; |
| 2840 | gh->msg_type = GSM48_MT_CC_RELEASE_COMPL; |
| 2841 | |
| 2842 | trans->callref = 0; |
| 2843 | |
| 2844 | gsm48_stop_cc_timer(trans); |
| 2845 | |
| 2846 | /* cause */ |
| 2847 | if (rel->fields & MNCC_F_CAUSE) |
| 2848 | encode_cause(msg, 0, &rel->cause); |
| 2849 | /* facility */ |
| 2850 | if (rel->fields & MNCC_F_FACILITY) |
| 2851 | encode_facility(msg, 0, &rel->facility); |
| 2852 | /* user-user */ |
| 2853 | if (rel->fields & MNCC_F_USERUSER) |
| 2854 | encode_useruser(msg, 0, &rel->useruser); |
| 2855 | |
| 2856 | free_trans(trans); |
| 2857 | |
| 2858 | return gsm48_sendmsg(msg); |
| 2859 | } |
| 2860 | |
| 2861 | static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg) |
| 2862 | { |
| 2863 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2864 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2865 | struct tlv_parsed tp; |
| 2866 | struct gsm_mncc fac; |
| 2867 | |
| 2868 | memset(&fac, 0, sizeof(struct gsm_mncc)); |
| 2869 | fac.callref = trans->callref; |
| 2870 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0); |
| 2871 | /* facility */ |
| 2872 | if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) { |
| 2873 | fac.fields |= MNCC_F_FACILITY; |
| 2874 | decode_facility(&fac.facility, |
| 2875 | TLVP_VAL(&tp, GSM48_IE_FACILITY)-1); |
| 2876 | } |
| 2877 | /* ss-version */ |
| 2878 | if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) { |
| 2879 | fac.fields |= MNCC_F_SSVERSION; |
| 2880 | decode_ssversion(&fac.ssversion, |
| 2881 | TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1); |
| 2882 | } |
| 2883 | |
| 2884 | return mncc_recvmsg(trans->network, trans, MNCC_FACILITY_IND, &fac); |
| 2885 | } |
| 2886 | |
| 2887 | static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg) |
| 2888 | { |
| 2889 | struct gsm_mncc *fac = arg; |
| 2890 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2891 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2892 | |
| 2893 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2894 | msg->lchan = trans->lchan; |
| 2895 | gh->msg_type = GSM48_MT_CC_FACILITY; |
| 2896 | |
| 2897 | /* facility */ |
| 2898 | encode_facility(msg, 1, &fac->facility); |
| 2899 | |
| 2900 | return gsm48_sendmsg(msg); |
| 2901 | } |
| 2902 | |
| 2903 | static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg) |
| 2904 | { |
| 2905 | struct gsm_mncc hold; |
| 2906 | |
| 2907 | memset(&hold, 0, sizeof(struct gsm_mncc)); |
| 2908 | hold.callref = trans->callref; |
| 2909 | return mncc_recvmsg(trans->network, trans, MNCC_HOLD_IND, &hold); |
| 2910 | } |
| 2911 | |
| 2912 | static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg) |
| 2913 | { |
| 2914 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2915 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2916 | |
| 2917 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2918 | msg->lchan = trans->lchan; |
| 2919 | gh->msg_type = GSM48_MT_CC_HOLD_ACK; |
| 2920 | |
| 2921 | return gsm48_sendmsg(msg); |
| 2922 | } |
| 2923 | |
| 2924 | static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg) |
| 2925 | { |
| 2926 | struct gsm_mncc *hold_rej = arg; |
| 2927 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2928 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2929 | |
| 2930 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2931 | msg->lchan = trans->lchan; |
| 2932 | gh->msg_type = GSM48_MT_CC_HOLD_REJ; |
| 2933 | |
| 2934 | /* cause */ |
| 2935 | if (hold_rej->fields & MNCC_F_CAUSE) |
| 2936 | encode_cause(msg, 1, &hold_rej->cause); |
| 2937 | else |
| 2938 | encode_cause(msg, 1, &default_cause); |
| 2939 | |
| 2940 | return gsm48_sendmsg(msg); |
| 2941 | } |
| 2942 | |
| 2943 | static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg) |
| 2944 | { |
| 2945 | struct gsm_mncc retrieve; |
| 2946 | |
| 2947 | memset(&retrieve, 0, sizeof(struct gsm_mncc)); |
| 2948 | retrieve.callref = trans->callref; |
| 2949 | return mncc_recvmsg(trans->network, trans, MNCC_RETRIEVE_IND, &retrieve); |
| 2950 | } |
| 2951 | |
| 2952 | static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg) |
| 2953 | { |
| 2954 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2955 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2956 | |
| 2957 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2958 | msg->lchan = trans->lchan; |
| 2959 | gh->msg_type = GSM48_MT_CC_RETR_ACK; |
| 2960 | |
| 2961 | return gsm48_sendmsg(msg); |
| 2962 | } |
| 2963 | |
| 2964 | static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg) |
| 2965 | { |
| 2966 | struct gsm_mncc *retrieve_rej = arg; |
| 2967 | struct msgb *msg = gsm48_msgb_alloc(); |
| 2968 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2969 | |
| 2970 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 2971 | msg->lchan = trans->lchan; |
| 2972 | gh->msg_type = GSM48_MT_CC_RETR_REJ; |
| 2973 | |
| 2974 | /* cause */ |
| 2975 | if (retrieve_rej->fields & MNCC_F_CAUSE) |
| 2976 | encode_cause(msg, 1, &retrieve_rej->cause); |
| 2977 | else |
| 2978 | encode_cause(msg, 1, &default_cause); |
| 2979 | |
| 2980 | return gsm48_sendmsg(msg); |
| 2981 | } |
| 2982 | |
| 2983 | static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg) |
| 2984 | { |
| 2985 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2986 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 2987 | struct tlv_parsed tp; |
| 2988 | struct gsm_mncc dtmf; |
| 2989 | |
| 2990 | memset(&dtmf, 0, sizeof(struct gsm_mncc)); |
| 2991 | dtmf.callref = trans->callref; |
| 2992 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 2993 | /* keypad facility */ |
| 2994 | if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) { |
| 2995 | dtmf.fields |= MNCC_F_KEYPAD; |
| 2996 | decode_keypad(&dtmf.keypad, |
| 2997 | TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1); |
| 2998 | } |
| 2999 | |
| 3000 | return mncc_recvmsg(trans->network, trans, MNCC_START_DTMF_IND, &dtmf); |
| 3001 | } |
| 3002 | |
| 3003 | static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg) |
| 3004 | { |
| 3005 | struct gsm_mncc *dtmf = arg; |
| 3006 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3007 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3008 | |
| 3009 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3010 | msg->lchan = trans->lchan; |
| 3011 | gh->msg_type = GSM48_MT_CC_START_DTMF_ACK; |
| 3012 | |
| 3013 | /* keypad */ |
| 3014 | if (dtmf->fields & MNCC_F_KEYPAD) |
| 3015 | encode_keypad(msg, dtmf->keypad); |
| 3016 | |
| 3017 | return gsm48_sendmsg(msg); |
| 3018 | } |
| 3019 | |
| 3020 | static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg) |
| 3021 | { |
| 3022 | struct gsm_mncc *dtmf = arg; |
| 3023 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3024 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3025 | |
| 3026 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3027 | msg->lchan = trans->lchan; |
| 3028 | gh->msg_type = GSM48_MT_CC_START_DTMF_REJ; |
| 3029 | |
| 3030 | /* cause */ |
| 3031 | if (dtmf->fields & MNCC_F_CAUSE) |
| 3032 | encode_cause(msg, 1, &dtmf->cause); |
| 3033 | else |
| 3034 | encode_cause(msg, 1, &default_cause); |
| 3035 | |
| 3036 | return gsm48_sendmsg(msg); |
| 3037 | } |
| 3038 | |
| 3039 | static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg) |
| 3040 | { |
| 3041 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3042 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3043 | |
| 3044 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3045 | msg->lchan = trans->lchan; |
| 3046 | gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK; |
| 3047 | |
| 3048 | return gsm48_sendmsg(msg); |
| 3049 | } |
| 3050 | |
| 3051 | static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg) |
| 3052 | { |
| 3053 | struct gsm_mncc dtmf; |
| 3054 | |
| 3055 | memset(&dtmf, 0, sizeof(struct gsm_mncc)); |
| 3056 | dtmf.callref = trans->callref; |
| 3057 | |
| 3058 | return mncc_recvmsg(trans->network, trans, MNCC_STOP_DTMF_IND, &dtmf); |
| 3059 | } |
| 3060 | |
| 3061 | static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg) |
| 3062 | { |
| 3063 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3064 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 3065 | struct tlv_parsed tp; |
| 3066 | struct gsm_mncc modify; |
| 3067 | |
| 3068 | memset(&modify, 0, sizeof(struct gsm_mncc)); |
| 3069 | modify.callref = trans->callref; |
| 3070 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0); |
| 3071 | /* bearer capability */ |
| 3072 | if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { |
| 3073 | modify.fields |= MNCC_F_BEARER_CAP; |
| 3074 | decode_bearer_cap(&modify.bearer_cap, |
| 3075 | TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1); |
| 3076 | } |
| 3077 | |
| 3078 | new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY); |
| 3079 | |
| 3080 | return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_IND, &modify); |
| 3081 | } |
| 3082 | |
| 3083 | static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg) |
| 3084 | { |
| 3085 | struct gsm_mncc *modify = arg; |
| 3086 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3087 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3088 | |
| 3089 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3090 | msg->lchan = trans->lchan; |
| 3091 | gh->msg_type = GSM48_MT_CC_MODIFY; |
| 3092 | |
| 3093 | gsm48_start_cc_timer(trans, 0x323, GSM48_T323); |
| 3094 | |
| 3095 | /* bearer capability */ |
| 3096 | encode_bearer_cap(msg, 1, &modify->bearer_cap); |
| 3097 | |
| 3098 | new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY); |
| 3099 | |
| 3100 | return gsm48_sendmsg(msg); |
| 3101 | } |
| 3102 | |
| 3103 | static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg) |
| 3104 | { |
| 3105 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3106 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 3107 | struct tlv_parsed tp; |
| 3108 | struct gsm_mncc modify; |
| 3109 | |
| 3110 | gsm48_stop_cc_timer(trans); |
| 3111 | |
| 3112 | memset(&modify, 0, sizeof(struct gsm_mncc)); |
| 3113 | modify.callref = trans->callref; |
| 3114 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0); |
| 3115 | /* bearer capability */ |
| 3116 | if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { |
| 3117 | modify.fields |= MNCC_F_BEARER_CAP; |
| 3118 | decode_bearer_cap(&modify.bearer_cap, |
| 3119 | TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1); |
| 3120 | } |
| 3121 | |
| 3122 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 3123 | |
| 3124 | return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_CNF, &modify); |
| 3125 | } |
| 3126 | |
| 3127 | static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg) |
| 3128 | { |
| 3129 | struct gsm_mncc *modify = arg; |
| 3130 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3131 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3132 | |
| 3133 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3134 | msg->lchan = trans->lchan; |
| 3135 | gh->msg_type = GSM48_MT_CC_MODIFY_COMPL; |
| 3136 | |
| 3137 | /* bearer capability */ |
| 3138 | encode_bearer_cap(msg, 1, &modify->bearer_cap); |
| 3139 | |
| 3140 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 3141 | |
| 3142 | return gsm48_sendmsg(msg); |
| 3143 | } |
| 3144 | |
| 3145 | static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg) |
| 3146 | { |
| 3147 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3148 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 3149 | struct tlv_parsed tp; |
| 3150 | struct gsm_mncc modify; |
| 3151 | |
| 3152 | gsm48_stop_cc_timer(trans); |
| 3153 | |
| 3154 | memset(&modify, 0, sizeof(struct gsm_mncc)); |
| 3155 | modify.callref = trans->callref; |
| 3156 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE); |
| 3157 | /* bearer capability */ |
| 3158 | if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) { |
| 3159 | modify.fields |= GSM48_IE_BEARER_CAP; |
| 3160 | decode_bearer_cap(&modify.bearer_cap, |
| 3161 | TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1); |
| 3162 | } |
| 3163 | /* cause */ |
| 3164 | if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) { |
| 3165 | modify.fields |= MNCC_F_CAUSE; |
| 3166 | decode_cause(&modify.cause, |
| 3167 | TLVP_VAL(&tp, GSM48_IE_CAUSE)-1); |
| 3168 | } |
| 3169 | |
| 3170 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 3171 | |
| 3172 | return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_REJ, &modify); |
| 3173 | } |
| 3174 | |
| 3175 | static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg) |
| 3176 | { |
| 3177 | struct gsm_mncc *modify = arg; |
| 3178 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3179 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3180 | |
| 3181 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3182 | msg->lchan = trans->lchan; |
| 3183 | gh->msg_type = GSM48_MT_CC_MODIFY_REJECT; |
| 3184 | |
| 3185 | /* bearer capability */ |
| 3186 | encode_bearer_cap(msg, 1, &modify->bearer_cap); |
| 3187 | /* cause */ |
| 3188 | encode_cause(msg, 1, &modify->cause); |
| 3189 | |
| 3190 | new_cc_state(trans, GSM_CSTATE_ACTIVE); |
| 3191 | |
| 3192 | return gsm48_sendmsg(msg); |
| 3193 | } |
| 3194 | |
| 3195 | static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg) |
| 3196 | { |
| 3197 | struct gsm_mncc *notify = arg; |
| 3198 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3199 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3200 | |
| 3201 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3202 | msg->lchan = trans->lchan; |
| 3203 | gh->msg_type = GSM48_MT_CC_NOTIFY; |
| 3204 | |
| 3205 | /* notify */ |
| 3206 | encode_notify(msg, notify->notify); |
| 3207 | |
| 3208 | return gsm48_sendmsg(msg); |
| 3209 | } |
| 3210 | |
| 3211 | static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg) |
| 3212 | { |
| 3213 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3214 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 3215 | // struct tlv_parsed tp; |
| 3216 | struct gsm_mncc notify; |
| 3217 | |
| 3218 | memset(¬ify, 0, sizeof(struct gsm_mncc)); |
| 3219 | notify.callref = trans->callref; |
| 3220 | // tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len); |
| 3221 | if (payload_len >= 1) |
| 3222 | decode_notify(¬ify.notify, gh->data); |
| 3223 | |
| 3224 | return mncc_recvmsg(trans->network, trans, MNCC_NOTIFY_IND, ¬ify); |
| 3225 | } |
| 3226 | |
| 3227 | static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg) |
| 3228 | { |
| 3229 | struct gsm_mncc *user = arg; |
| 3230 | struct msgb *msg = gsm48_msgb_alloc(); |
| 3231 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 3232 | |
| 3233 | gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id; |
| 3234 | msg->lchan = trans->lchan; |
| 3235 | gh->msg_type = GSM48_MT_CC_USER_INFO; |
| 3236 | |
| 3237 | /* user-user */ |
| 3238 | if (user->fields & MNCC_F_USERUSER) |
| 3239 | encode_useruser(msg, 1, &user->useruser); |
| 3240 | /* more data */ |
| 3241 | if (user->more) |
| 3242 | encode_more(msg); |
| 3243 | |
| 3244 | return gsm48_sendmsg(msg); |
| 3245 | } |
| 3246 | |
| 3247 | static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg) |
| 3248 | { |
| 3249 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3250 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 3251 | struct tlv_parsed tp; |
| 3252 | struct gsm_mncc user; |
| 3253 | |
| 3254 | memset(&user, 0, sizeof(struct gsm_mncc)); |
| 3255 | user.callref = trans->callref; |
| 3256 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0); |
| 3257 | /* user-user */ |
| 3258 | if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) { |
| 3259 | user.fields |= MNCC_F_USERUSER; |
| 3260 | decode_useruser(&user.useruser, |
| 3261 | TLVP_VAL(&tp, GSM48_IE_USER_USER)-1); |
| 3262 | } |
| 3263 | /* more data */ |
| 3264 | if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA)) |
| 3265 | user.more = 1; |
| 3266 | |
| 3267 | return mncc_recvmsg(trans->network, trans, MNCC_USERINFO_IND, &user); |
| 3268 | } |
| 3269 | |
| 3270 | static int gsm48_lchan_modify(struct gsm_trans *trans, void *arg) |
| 3271 | { |
| 3272 | struct gsm_mncc *mode = arg; |
| 3273 | |
| 3274 | return gsm48_tx_chan_mode_modify(trans->lchan, mode->lchan_mode); |
| 3275 | } |
| 3276 | |
| 3277 | static struct downstate { |
| 3278 | u_int32_t states; |
| 3279 | int type; |
| 3280 | int (*rout) (struct gsm_trans *trans, void *arg); |
| 3281 | } downstatelist[] = { |
| 3282 | /* mobile originating call establishment */ |
| 3283 | {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */ |
| 3284 | MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc}, |
| 3285 | {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */ |
| 3286 | MNCC_ALERT_REQ, gsm48_cc_tx_alerting}, |
| 3287 | {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 */ |
| 3288 | MNCC_SETUP_RSP, gsm48_cc_tx_connect}, |
| 3289 | {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */ |
| 3290 | MNCC_PROGRESS_REQ, gsm48_cc_tx_progress}, |
| 3291 | /* mobile terminating call establishment */ |
| 3292 | {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */ |
| 3293 | MNCC_SETUP_REQ, gsm48_cc_tx_setup}, |
| 3294 | {SBIT(GSM_CSTATE_CONNECT_REQUEST), |
| 3295 | MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack}, |
| 3296 | /* signalling during call */ |
| 3297 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3298 | MNCC_NOTIFY_REQ, gsm48_cc_tx_notify}, |
| 3299 | {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), |
| 3300 | MNCC_FACILITY_REQ, gsm48_cc_tx_facility}, |
| 3301 | {ALL_STATES, |
| 3302 | MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack}, |
| 3303 | {ALL_STATES, |
| 3304 | MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej}, |
| 3305 | {ALL_STATES, |
| 3306 | MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack}, |
| 3307 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3308 | MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack}, |
| 3309 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3310 | MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej}, |
| 3311 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3312 | MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack}, |
| 3313 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3314 | MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej}, |
| 3315 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3316 | MNCC_MODIFY_REQ, gsm48_cc_tx_modify}, |
| 3317 | {SBIT(GSM_CSTATE_MO_ORIG_MODIFY), |
| 3318 | MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete}, |
| 3319 | {SBIT(GSM_CSTATE_MO_ORIG_MODIFY), |
| 3320 | MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject}, |
| 3321 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3322 | MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo}, |
| 3323 | /* clearing */ |
| 3324 | {SBIT(GSM_CSTATE_INITIATED), |
| 3325 | MNCC_REJ_REQ, gsm48_cc_tx_release_compl}, |
| 3326 | {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */ |
| 3327 | MNCC_DISC_REQ, gsm48_cc_tx_disconnect}, |
| 3328 | {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */ |
| 3329 | MNCC_REL_REQ, gsm48_cc_tx_release}, |
| 3330 | /* special */ |
| 3331 | {ALL_STATES, |
| 3332 | MNCC_LCHAN_MODIFY, gsm48_lchan_modify}, |
| 3333 | }; |
| 3334 | |
| 3335 | #define DOWNSLLEN \ |
| 3336 | (sizeof(downstatelist) / sizeof(struct downstate)) |
| 3337 | |
| 3338 | |
| 3339 | int mncc_send(struct gsm_network *net, int msg_type, void *arg) |
| 3340 | { |
| 3341 | int i, j, k, l, rc = 0; |
| 3342 | struct gsm_trans *trans = NULL, *transt; |
| 3343 | struct gsm_subscriber *subscr; |
| 3344 | struct gsm_lchan *lchan = NULL, *lchant; |
| 3345 | struct gsm_bts *bts = NULL; |
| 3346 | struct gsm_bts_trx *trx; |
| 3347 | struct gsm_bts_trx_ts *ts; |
| 3348 | struct gsm_mncc *data = arg, rel; |
| 3349 | |
| 3350 | /* handle special messages */ |
| 3351 | switch(msg_type) { |
| 3352 | case MNCC_BRIDGE: |
| 3353 | return tch_bridge(net, arg); |
| 3354 | case MNCC_FRAME_DROP: |
| 3355 | return tch_recv(net, arg, 0); |
| 3356 | case MNCC_FRAME_RECV: |
| 3357 | return tch_recv(net, arg, 1); |
| 3358 | case GSM_TRAU_FRAME: |
| 3359 | return tch_frame(net, arg); |
| 3360 | } |
| 3361 | |
| 3362 | memset(&rel, 0, sizeof(struct gsm_mncc)); |
| 3363 | rel.callref = data->callref; |
| 3364 | |
| 3365 | /* Find callref */ |
| 3366 | trans = get_trans_ref(net, data->callref); |
| 3367 | |
| 3368 | /* Callref unknown */ |
| 3369 | if (!trans) { |
| 3370 | if (msg_type != MNCC_SETUP_REQ || !data->called.number[0]) { |
| 3371 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) " |
| 3372 | "Received '%s' from MNCC with " |
| 3373 | "unknown callref %d\n", data->called.number, |
| 3374 | get_mncc_name(msg_type), data->callref); |
| 3375 | /* Invalid call reference */ |
Andreas Eversberg | b992a8a | 2009-06-14 22:14:12 +0800 | [diff] [blame] | 3376 | return mncc_release_ind(net, NULL, data->callref, |
| 3377 | GSM48_CAUSE_LOC_PRN_S_LU, |
| 3378 | GSM48_CC_CAUSE_INVAL_TRANS_ID); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3379 | } |
| 3380 | /* New transaction due to setup, find subscriber */ |
| 3381 | subscr = subscr_get_by_extension(data->called.number); |
| 3382 | /* If subscriber is not found */ |
| 3383 | if (!subscr) { |
| 3384 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) " |
| 3385 | "Received '%s' from MNCC with " |
| 3386 | "unknown subscriber %s\n", data->called.number, |
| 3387 | get_mncc_name(msg_type), data->called.number); |
| 3388 | /* Unknown subscriber */ |
Andreas Eversberg | b992a8a | 2009-06-14 22:14:12 +0800 | [diff] [blame] | 3389 | return mncc_release_ind(net, NULL, data->callref, |
| 3390 | GSM48_CAUSE_LOC_PRN_S_LU, |
| 3391 | GSM48_CC_CAUSE_UNASSIGNED_NR); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3392 | } |
| 3393 | /* If subscriber is not "attached" */ |
| 3394 | if (!subscr->lac) { |
| 3395 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) " |
| 3396 | "Received '%s' from MNCC with " |
| 3397 | "detached subscriber %s\n", data->called.number, |
| 3398 | get_mncc_name(msg_type), data->called.number); |
| 3399 | subscr_put(subscr); |
| 3400 | /* Temporarily out of order */ |
Andreas Eversberg | b992a8a | 2009-06-14 22:14:12 +0800 | [diff] [blame] | 3401 | return mncc_release_ind(net, NULL, data->callref, |
| 3402 | GSM48_CAUSE_LOC_PRN_S_LU, |
| 3403 | GSM48_CC_CAUSE_DEST_OOO); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3404 | } |
| 3405 | /* Create transaction */ |
| 3406 | if (!(trans = calloc(1, sizeof(struct gsm_trans)))) { |
| 3407 | DEBUGP(DCC, "No memory for trans.\n"); |
| 3408 | subscr_put(subscr); |
| 3409 | /* Ressource unavailable */ |
Andreas Eversberg | b992a8a | 2009-06-14 22:14:12 +0800 | [diff] [blame] | 3410 | mncc_release_ind(net, NULL, data->callref, |
| 3411 | GSM48_CAUSE_LOC_PRN_S_LU, |
| 3412 | GSM48_CC_CAUSE_RESOURCE_UNAVAIL); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3413 | return -ENOMEM; |
| 3414 | } |
| 3415 | trans->callref = data->callref; |
| 3416 | trans->network = net; |
| 3417 | trans->transaction_id = 0xff; /* unassigned */ |
| 3418 | llist_add_tail(&trans->entry, &net->trans_list); |
| 3419 | /* Assign subscriber to transaction */ |
| 3420 | trans->subscr = subscr; |
| 3421 | /* Find lchan */ |
| 3422 | for (i = 0; i < net->num_bts; i++) { |
| 3423 | bts = &net->bts[i]; |
| 3424 | for (j = 0; j < bts->num_trx; j++) { |
| 3425 | trx = &bts->trx[j]; |
| 3426 | for (k = 0; k < TRX_NR_TS; k++) { |
| 3427 | ts = &trx->ts[k]; |
| 3428 | for (l = 0; l < TS_MAX_LCHAN; l++) { |
| 3429 | lchant = &ts->lchan[l]; |
| 3430 | if (lchant->subscr == subscr) { |
| 3431 | lchan = lchant; |
| 3432 | break; |
| 3433 | } |
| 3434 | } |
| 3435 | } |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | /* If subscriber has no lchan */ |
| 3440 | if (!lchan) { |
| 3441 | /* find transaction with this subscriber already paging */ |
| 3442 | llist_for_each_entry(transt, &net->trans_list, entry) { |
| 3443 | /* Transaction of our lchan? */ |
| 3444 | if (transt == trans || |
| 3445 | transt->subscr != subscr) |
| 3446 | continue; |
| 3447 | DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) " |
| 3448 | "Received '%s' from MNCC with " |
| 3449 | "unallocated channel, paging already " |
| 3450 | "started.\n", bts->nr, |
| 3451 | data->called.number, |
| 3452 | get_mncc_name(msg_type)); |
| 3453 | return 0; |
| 3454 | } |
| 3455 | /* store setup informations until paging was successfull */ |
| 3456 | memcpy(&trans->cc_msg, data, sizeof(struct gsm_mncc)); |
| 3457 | /* start paging subscriber on all BTS with her location */ |
| 3458 | subscr->net = net; |
| 3459 | bts = NULL; |
| 3460 | do { |
| 3461 | bts = gsm_bts_by_lac(net, subscr->lac, bts); |
| 3462 | if (!bts) |
| 3463 | break; |
| 3464 | DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) " |
| 3465 | "Received '%s' from MNCC with " |
| 3466 | "unallocated channel, paging.\n", |
| 3467 | bts->nr, data->called.number, |
| 3468 | get_mncc_name(msg_type)); |
| 3469 | /* Trigger paging */ |
Harald Welte | cb0595f | 2009-06-12 01:54:08 +0800 | [diff] [blame] | 3470 | paging_request(net, subscr, RSL_CHANNEED_TCH_F, |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3471 | setup_trig_pag_evt, subscr); |
| 3472 | } while (1); |
| 3473 | return 0; |
| 3474 | } |
| 3475 | /* Assign lchan */ |
| 3476 | trans->lchan = lchan; |
| 3477 | use_lchan(lchan); |
| 3478 | } |
| 3479 | lchan = trans->lchan; |
| 3480 | |
| 3481 | /* if paging did not respond yet */ |
| 3482 | if (!lchan) { |
| 3483 | DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) " |
| 3484 | "Received '%s' from MNCC in paging state\n", |
| 3485 | (trans->subscr)?(trans->subscr->extension):"-", |
| 3486 | get_mncc_name(msg_type)); |
Harald Welte | bbc636a | 2009-06-11 14:23:20 +0800 | [diff] [blame] | 3487 | mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, |
| 3488 | GSM48_CC_CAUSE_NORM_CALL_CLEAR); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3489 | if (msg_type == MNCC_REL_REQ) |
| 3490 | rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel); |
| 3491 | else |
| 3492 | rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel); |
| 3493 | trans->callref = 0; |
| 3494 | free_trans(trans); |
| 3495 | return rc; |
| 3496 | } |
| 3497 | |
| 3498 | DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) " |
| 3499 | "Received '%s' from MNCC in state %d (%s)\n", |
| 3500 | lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr, |
| 3501 | trans->transaction_id, |
| 3502 | (lchan->subscr)?(lchan->subscr->extension):"-", |
| 3503 | get_mncc_name(msg_type), trans->state, |
| 3504 | cc_state_names[trans->state]); |
| 3505 | |
| 3506 | /* Find function for current state and message */ |
| 3507 | for (i = 0; i < DOWNSLLEN; i++) |
| 3508 | if ((msg_type == downstatelist[i].type) |
| 3509 | && ((1 << trans->state) & downstatelist[i].states)) |
| 3510 | break; |
| 3511 | if (i == DOWNSLLEN) { |
| 3512 | DEBUGP(DCC, "Message unhandled at this state.\n"); |
| 3513 | return 0; |
| 3514 | } |
| 3515 | |
| 3516 | rc = downstatelist[i].rout(trans, arg); |
| 3517 | |
| 3518 | return rc; |
| 3519 | } |
| 3520 | |
| 3521 | |
| 3522 | static struct datastate { |
| 3523 | u_int32_t states; |
| 3524 | int type; |
| 3525 | int (*rout) (struct gsm_trans *trans, struct msgb *msg); |
| 3526 | } datastatelist[] = { |
| 3527 | /* mobile originating call establishment */ |
| 3528 | {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */ |
| 3529 | GSM48_MT_CC_SETUP, gsm48_cc_rx_setup}, |
| 3530 | {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */ |
| 3531 | GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup}, |
| 3532 | {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */ |
| 3533 | GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack}, |
| 3534 | /* mobile terminating call establishment */ |
| 3535 | {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */ |
| 3536 | GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf}, |
| 3537 | {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */ |
| 3538 | GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting}, |
| 3539 | {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 */ |
| 3540 | GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect}, |
| 3541 | /* signalling during call */ |
| 3542 | {ALL_STATES - SBIT(GSM_CSTATE_NULL), |
| 3543 | GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility}, |
| 3544 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3545 | GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify}, |
| 3546 | {ALL_STATES, |
| 3547 | GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf}, |
| 3548 | {ALL_STATES, |
| 3549 | GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf}, |
| 3550 | {ALL_STATES, |
| 3551 | GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq}, |
| 3552 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3553 | GSM48_MT_CC_HOLD, gsm48_cc_rx_hold}, |
| 3554 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3555 | GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve}, |
| 3556 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3557 | GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify}, |
| 3558 | {SBIT(GSM_CSTATE_MO_TERM_MODIFY), |
| 3559 | GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete}, |
| 3560 | {SBIT(GSM_CSTATE_MO_TERM_MODIFY), |
| 3561 | GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject}, |
| 3562 | {SBIT(GSM_CSTATE_ACTIVE), |
| 3563 | GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo}, |
| 3564 | /* clearing */ |
| 3565 | {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */ |
| 3566 | GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect}, |
| 3567 | {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */ |
| 3568 | GSM48_MT_CC_RELEASE, gsm48_cc_rx_release}, |
| 3569 | {ALL_STATES, /* 5.4.3.4 */ |
| 3570 | GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl}, |
| 3571 | }; |
| 3572 | |
| 3573 | #define DATASLLEN \ |
| 3574 | (sizeof(datastatelist) / sizeof(struct datastate)) |
| 3575 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3576 | static int gsm0408_rcv_cc(struct msgb *msg) |
| 3577 | { |
| 3578 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3579 | u_int8_t msg_type = gh->msg_type & 0xbf; |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3580 | u_int8_t transaction_id = (gh->proto_discr & 0xf0) ^ 0x80; /* flip */ |
| 3581 | struct gsm_lchan *lchan = msg->lchan; |
| 3582 | struct gsm_trans *trans = NULL, *transt; |
| 3583 | struct gsm_network *net = lchan->ts->trx->bts->network; |
| 3584 | int i, rc = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3585 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3586 | if (msg_type & 0x80) { |
| 3587 | DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type); |
| 3588 | return -EINVAL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3589 | } |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3590 | |
| 3591 | /* Find transaction */ |
| 3592 | llist_for_each_entry(transt, &net->trans_list, entry) { |
| 3593 | /* Transaction of our lchan? */ |
| 3594 | if (transt->lchan == lchan |
| 3595 | && transt->transaction_id == transaction_id) { |
| 3596 | trans = transt; |
| 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) " |
| 3601 | "Received '%s' from MS in state %d (%s)\n", |
| 3602 | lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr, |
| 3603 | transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-", |
| 3604 | cc_msg_names[msg_type], trans?(trans->state):0, |
| 3605 | cc_state_names[trans?(trans->state):0]); |
| 3606 | |
| 3607 | /* Create transaction */ |
| 3608 | if (!trans) { |
| 3609 | DEBUGP(DCC, "Unknown transaction ID %02x, " |
| 3610 | "creating new trans.\n", transaction_id); |
| 3611 | /* Create transaction */ |
| 3612 | if (!(trans = calloc(1, sizeof(struct gsm_trans)))) { |
| 3613 | DEBUGP(DCC, "No memory for trans.\n"); |
| 3614 | rc = gsm48_tx_simple(msg->lchan, |
| 3615 | GSM48_PDISC_CC | transaction_id, |
| 3616 | GSM48_MT_CC_RELEASE_COMPL); |
| 3617 | return -ENOMEM; |
| 3618 | } |
| 3619 | llist_add_tail(&trans->entry, &net->trans_list); |
| 3620 | /* Assign transaction */ |
| 3621 | trans->callref = new_callref++; |
| 3622 | trans->network = net; |
| 3623 | trans->transaction_id = transaction_id; |
| 3624 | trans->lchan = lchan; |
| 3625 | use_lchan(lchan); |
| 3626 | if (lchan->subscr) { |
| 3627 | trans->subscr = lchan->subscr; |
| 3628 | subscr_get(trans->subscr); |
| 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | /* find function for current state and message */ |
| 3633 | for (i = 0; i < DATASLLEN; i++) |
| 3634 | if ((msg_type == datastatelist[i].type) |
| 3635 | && ((1 << trans->state) & datastatelist[i].states)) |
| 3636 | break; |
| 3637 | if (i == DATASLLEN) { |
| 3638 | DEBUGP(DCC, "Message unhandled at this state.\n"); |
| 3639 | return 0; |
| 3640 | } |
| 3641 | |
| 3642 | rc = datastatelist[i].rout(trans, msg); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3643 | |
| 3644 | return rc; |
| 3645 | } |
| 3646 | |
| 3647 | /* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */ |
| 3648 | int gsm0408_rcvmsg(struct msgb *msg) |
| 3649 | { |
| 3650 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 3651 | u_int8_t pdisc = gh->proto_discr & 0x0f; |
| 3652 | int rc = 0; |
| 3653 | |
| 3654 | switch (pdisc) { |
| 3655 | case GSM48_PDISC_CC: |
| 3656 | rc = gsm0408_rcv_cc(msg); |
| 3657 | break; |
| 3658 | case GSM48_PDISC_MM: |
| 3659 | rc = gsm0408_rcv_mm(msg); |
| 3660 | break; |
| 3661 | case GSM48_PDISC_RR: |
| 3662 | rc = gsm0408_rcv_rr(msg); |
| 3663 | break; |
| 3664 | case GSM48_PDISC_SMS: |
| 3665 | rc = gsm0411_rcv_sms(msg); |
| 3666 | break; |
| 3667 | case GSM48_PDISC_MM_GPRS: |
| 3668 | case GSM48_PDISC_SM_GPRS: |
| 3669 | fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n", |
| 3670 | pdisc); |
| 3671 | break; |
| 3672 | default: |
| 3673 | fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n", |
| 3674 | pdisc); |
| 3675 | break; |
| 3676 | } |
| 3677 | |
| 3678 | return rc; |
| 3679 | } |
| 3680 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 3681 | /* Section 9.1.8 / Table 9.9 */ |
| 3682 | struct chreq { |
| 3683 | u_int8_t val; |
| 3684 | u_int8_t mask; |
| 3685 | enum chreq_type type; |
| 3686 | }; |
| 3687 | |
| 3688 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */ |
| 3689 | static const struct chreq chreq_type_neci1[] = { |
| 3690 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 3691 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F }, |
| 3692 | { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H }, |
| 3693 | { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL }, |
| 3694 | { 0xe0, 0xe0, CHREQ_T_SDCCH }, |
| 3695 | { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H }, |
| 3696 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 3697 | { 0x00, 0xf0, CHREQ_T_LOCATION_UPD }, |
| 3698 | { 0x10, 0xf0, CHREQ_T_SDCCH }, |
| 3699 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 3700 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 3701 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 3702 | }; |
| 3703 | |
| 3704 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */ |
| 3705 | static const struct chreq chreq_type_neci0[] = { |
| 3706 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 3707 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H }, |
| 3708 | { 0xe0, 0xe0, CHREQ_T_TCH_F }, |
| 3709 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 3710 | { 0x00, 0xe0, CHREQ_T_LOCATION_UPD }, |
| 3711 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 3712 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 3713 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 3714 | }; |
| 3715 | |
| 3716 | static const enum gsm_chan_t ctype_by_chreq[] = { |
| 3717 | [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F, |
| 3718 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F, |
| 3719 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H, |
| 3720 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H, |
| 3721 | [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH, |
| 3722 | [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F, |
| 3723 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 3724 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 3725 | [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH, |
| 3726 | [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH, |
| 3727 | [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F, |
| 3728 | [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F, |
| 3729 | }; |
| 3730 | |
| 3731 | static const enum gsm_chreq_reason_t reason_by_chreq[] = { |
| 3732 | [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG, |
| 3733 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL, |
| 3734 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL, |
| 3735 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL, |
| 3736 | [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER, |
| 3737 | [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER, |
| 3738 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 3739 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 3740 | [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD, |
| 3741 | [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG, |
| 3742 | [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG, |
| 3743 | [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG, |
| 3744 | }; |
| 3745 | |
| 3746 | enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 3747 | { |
| 3748 | int i; |
| 3749 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 3750 | |
| 3751 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 3752 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 3753 | if ((ra & chr->mask) == chr->val) |
| 3754 | return ctype_by_chreq[chr->type]; |
| 3755 | } |
| 3756 | fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra); |
| 3757 | return GSM_LCHAN_SDCCH; |
| 3758 | } |
| 3759 | |
| 3760 | enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 3761 | { |
| 3762 | int i; |
| 3763 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 3764 | |
| 3765 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 3766 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 3767 | if ((ra & chr->mask) == chr->val) |
| 3768 | return reason_by_chreq[chr->type]; |
| 3769 | } |
| 3770 | fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra); |
| 3771 | return GSM_CHREQ_REASON_OTHER; |
| 3772 | } |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 3773 | |
| 3774 | /* dequeue messages to layer 4 */ |
| 3775 | int bsc_upqueue(struct gsm_network *net) |
| 3776 | { |
| 3777 | struct gsm_mncc *mncc; |
| 3778 | struct msgb *msg; |
| 3779 | int work = 0; |
| 3780 | |
| 3781 | if (net) |
| 3782 | while ((msg = msgb_dequeue(&net->upqueue))) { |
| 3783 | mncc = (struct gsm_mncc *)msg->data; |
| 3784 | if (net->mncc_recv) |
| 3785 | net->mncc_recv(net, mncc->msg_type, mncc); |
| 3786 | work = 1; /* work done */ |
| 3787 | } |
| 3788 | |
| 3789 | return work; |
| 3790 | } |