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