Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface |
| 2 | * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */ |
| 3 | |
| 4 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 5 | * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | #include <time.h> |
| 31 | #include <netinet/in.h> |
| 32 | |
| 33 | #include <openbsc/db.h> |
| 34 | #include <openbsc/msgb.h> |
| 35 | #include <openbsc/tlv.h> |
| 36 | #include <openbsc/debug.h> |
| 37 | #include <openbsc/gsm_data.h> |
| 38 | #include <openbsc/gsm_subscriber.h> |
| 39 | #include <openbsc/gsm_04_11.h> |
| 40 | #include <openbsc/gsm_04_08.h> |
| 41 | #include <openbsc/abis_rsl.h> |
| 42 | #include <openbsc/chan_alloc.h> |
| 43 | #include <openbsc/paging.h> |
| 44 | #include <openbsc/signal.h> |
| 45 | #include <openbsc/trau_frame.h> |
| 46 | #include <openbsc/trau_mux.h> |
| 47 | |
| 48 | #define GSM48_ALLOC_SIZE 1024 |
| 49 | #define GSM48_ALLOC_HEADROOM 128 |
| 50 | |
| 51 | static const struct tlv_definition rsl_att_tlvdef = { |
| 52 | .def = { |
| 53 | [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV }, |
| 54 | [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV }, |
| 55 | [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV }, |
| 56 | [GSM48_IE_UTC] = { TLV_TYPE_TV }, |
| 57 | [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 }, |
| 58 | [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV }, |
| 59 | |
| 60 | [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV }, |
| 61 | [GSM48_IE_CAUSE] = { TLV_TYPE_TLV }, |
| 62 | [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV }, |
| 63 | [GSM48_IE_ALERT] = { TLV_TYPE_TLV }, |
| 64 | [GSM48_IE_FACILITY] = { TLV_TYPE_TLV }, |
| 65 | [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV }, |
| 66 | [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV }, |
| 67 | [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV }, |
| 68 | [GSM48_IE_SIGNAL] = { TLV_TYPE_TV }, |
| 69 | [GSM48_IE_CONN_NUM] = { TLV_TYPE_TLV }, |
| 70 | [GSM48_IE_CONN_SUBADDR] = { TLV_TYPE_TLV }, |
| 71 | [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV }, |
| 72 | [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV }, |
| 73 | [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV }, |
| 74 | [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV }, |
| 75 | [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV }, |
| 76 | [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV }, |
| 77 | [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV }, |
| 78 | [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV }, |
| 79 | [GSM48_IE_USER_USER] = { TLV_TYPE_TLV }, |
| 80 | [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV }, |
| 81 | [GSM48_IE_MORE_DATA] = { TLV_TYPE_T }, |
| 82 | [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T }, |
| 83 | [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T }, |
| 84 | [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T }, |
| 85 | /* FIXME: more elements */ |
| 86 | }, |
| 87 | }; |
| 88 | |
| 89 | static const char *rr_cause_names[] = { |
| 90 | [GSM48_RR_CAUSE_NORMAL] = "Normal event", |
| 91 | [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified", |
| 92 | [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable", |
| 93 | [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired", |
| 94 | [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path", |
| 95 | [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release", |
| 96 | [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range", |
| 97 | [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable", |
| 98 | [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented", |
| 99 | [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared", |
| 100 | [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message", |
| 101 | [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information", |
| 102 | [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented", |
| 103 | [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state", |
| 104 | [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error", |
| 105 | [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available", |
| 106 | [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified", |
| 107 | }; |
| 108 | |
| 109 | static char strbuf[64]; |
| 110 | |
| 111 | static const char *rr_cause_name(u_int8_t cause) |
| 112 | { |
| 113 | if (cause < ARRAY_SIZE(rr_cause_names) && |
| 114 | rr_cause_names[cause]) |
| 115 | return rr_cause_names[cause]; |
| 116 | |
| 117 | snprintf(strbuf, sizeof(strbuf), "0x%02x", cause); |
| 118 | return strbuf; |
| 119 | } |
| 120 | |
| 121 | static void parse_meas_rep(struct gsm_meas_rep *rep, const u_int8_t *data, |
| 122 | int len) |
| 123 | { |
| 124 | memset(rep, 0, sizeof(*rep)); |
| 125 | |
| 126 | if (data[0] & 0x80) |
| 127 | rep->flags |= MEAS_REP_F_BA1; |
| 128 | if (data[0] & 0x40) |
| 129 | rep->flags |= MEAS_REP_F_DTX; |
| 130 | if (data[1] & 0x40) |
| 131 | rep->flags |= MEAS_REP_F_VALID; |
| 132 | |
| 133 | rep->rxlev_full = data[0] & 0x3f; |
| 134 | rep->rxlev_sub = data[1] & 0x3f; |
| 135 | rep->rxqual_full = (data[3] >> 4) & 0x7; |
| 136 | rep->rxqual_sub = (data[3] >> 1) & 0x7; |
| 137 | rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2); |
| 138 | if (rep->num_cell < 1) |
| 139 | return; |
| 140 | |
| 141 | /* an encoding nightmare in perfection */ |
| 142 | |
| 143 | rep->cell[0].rxlev = data[4] & 0x3f; |
| 144 | rep->cell[0].bcch_freq = data[5] >> 2; |
| 145 | rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5); |
| 146 | if (rep->num_cell < 2) |
| 147 | return; |
| 148 | |
| 149 | rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7); |
| 150 | rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f; |
| 151 | rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4); |
| 152 | if (rep->num_cell < 3) |
| 153 | return; |
| 154 | |
| 155 | rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6); |
| 156 | rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f; |
| 157 | rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3); |
| 158 | if (rep->num_cell < 4) |
| 159 | return; |
| 160 | |
| 161 | rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5); |
| 162 | rep->cell[3].bcch_freq = data[11] & 0x1f; |
| 163 | rep->cell[3].bsic = data[12] >> 2; |
| 164 | if (rep->num_cell < 5) |
| 165 | return; |
| 166 | |
| 167 | rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4); |
| 168 | rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7); |
| 169 | rep->cell[4].bsic = (data[14] >> 1) & 0x3f; |
| 170 | if (rep->num_cell < 6) |
| 171 | return; |
| 172 | |
| 173 | rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3); |
| 174 | rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6); |
| 175 | rep->cell[5].bsic = data[16] & 0x3f; |
| 176 | } |
| 177 | |
| 178 | int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi); |
| 179 | static int gsm48_tx_simple(struct gsm_lchan *lchan, |
| 180 | u_int8_t pdisc, u_int8_t msg_type); |
| 181 | static void schedule_reject(struct gsm_lchan *lchan); |
| 182 | |
| 183 | struct gsm_lai { |
| 184 | u_int16_t mcc; |
| 185 | u_int16_t mnc; |
| 186 | u_int16_t lac; |
| 187 | }; |
| 188 | |
| 189 | static int authorize_everonye = 0; |
| 190 | void gsm0408_allow_everyone(int everyone) |
| 191 | { |
| 192 | printf("Allowing everyone?\n"); |
| 193 | authorize_everonye = everyone; |
| 194 | } |
| 195 | |
| 196 | static int reject_cause = 0; |
| 197 | void gsm0408_set_reject_cause(int cause) |
| 198 | { |
| 199 | reject_cause = cause; |
| 200 | } |
| 201 | |
| 202 | static int authorize_subscriber(struct gsm_loc_updating_operation *loc, |
| 203 | struct gsm_subscriber *subscriber) |
| 204 | { |
| 205 | if (!subscriber) |
| 206 | return 0; |
| 207 | |
| 208 | /* |
| 209 | * Do not send accept yet as more information should arrive. Some |
| 210 | * phones will not send us the information and we will have to check |
| 211 | * what we want to do with that. |
| 212 | */ |
| 213 | if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei)) |
| 214 | return 0; |
| 215 | |
| 216 | if (authorize_everonye) |
| 217 | return 1; |
| 218 | |
| 219 | return subscriber->authorized; |
| 220 | } |
| 221 | |
| 222 | static void release_loc_updating_req(struct gsm_lchan *lchan) |
| 223 | { |
| 224 | if (!lchan->loc_operation) |
| 225 | return; |
| 226 | |
| 227 | bsc_del_timer(&lchan->loc_operation->updating_timer); |
| 228 | free(lchan->loc_operation); |
| 229 | lchan->loc_operation = 0; |
| 230 | put_lchan(lchan); |
| 231 | } |
| 232 | |
| 233 | static void allocate_loc_updating_req(struct gsm_lchan *lchan) |
| 234 | { |
| 235 | use_lchan(lchan); |
| 236 | release_loc_updating_req(lchan); |
| 237 | |
| 238 | lchan->loc_operation = (struct gsm_loc_updating_operation *) |
| 239 | malloc(sizeof(*lchan->loc_operation)); |
| 240 | memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation)); |
| 241 | } |
| 242 | |
| 243 | static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg) |
| 244 | { |
| 245 | u_int32_t tmsi; |
| 246 | |
| 247 | if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) { |
| 248 | db_subscriber_alloc_tmsi(lchan->subscr); |
| 249 | subscr_update(lchan->subscr, msg->trx->bts, GSM_SUBSCRIBER_UPDATE_ATTACHED); |
| 250 | tmsi = strtoul(lchan->subscr->tmsi, NULL, 10); |
| 251 | release_loc_updating_req(lchan); |
| 252 | return gsm0408_loc_upd_acc(msg->lchan, tmsi); |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal, |
| 259 | void *handler_data, void *signal_data) |
| 260 | { |
| 261 | if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE) |
| 262 | return 0; |
| 263 | |
| 264 | /* |
| 265 | * Cancel any outstanding location updating request |
| 266 | * operation taking place on the lchan. |
| 267 | */ |
| 268 | struct gsm_lchan *lchan = (struct gsm_lchan *)handler_data; |
| 269 | release_loc_updating_req(lchan); |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * This will be ran by the linker when loading the DSO. We use it to |
| 276 | * do system initialization, e.g. registration of signal handlers. |
| 277 | */ |
| 278 | static __attribute__((constructor)) void on_dso_load_0408(void) |
| 279 | { |
| 280 | register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL); |
| 281 | } |
| 282 | |
| 283 | static void to_bcd(u_int8_t *bcd, u_int16_t val) |
| 284 | { |
| 285 | bcd[2] = val % 10; |
| 286 | val = val / 10; |
| 287 | bcd[1] = val % 10; |
| 288 | val = val / 10; |
| 289 | bcd[0] = val % 10; |
| 290 | val = val / 10; |
| 291 | } |
| 292 | |
| 293 | void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc, |
| 294 | u_int16_t mnc, u_int16_t lac) |
| 295 | { |
| 296 | u_int8_t bcd[3]; |
| 297 | |
| 298 | to_bcd(bcd, mcc); |
| 299 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 300 | lai48->digits[1] = bcd[2]; |
| 301 | |
| 302 | to_bcd(bcd, mnc); |
| 303 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
| 304 | #if 0 |
| 305 | lai48->digits[1] |= bcd[2] << 4; |
| 306 | lai48->digits[2] = bcd[0] | (bcd[1] << 4); |
| 307 | #else |
| 308 | lai48->digits[1] |= 0xf << 4; |
| 309 | lai48->digits[2] = bcd[1] | (bcd[2] << 4); |
| 310 | #endif |
| 311 | |
| 312 | lai48->lac = htons(lac); |
| 313 | } |
| 314 | |
| 315 | #define TMSI_LEN 5 |
| 316 | #define MID_TMSI_LEN (TMSI_LEN + 2) |
| 317 | |
| 318 | int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi) |
| 319 | { |
| 320 | u_int32_t *tptr = (u_int32_t *) &buf[3]; |
| 321 | |
| 322 | buf[0] = GSM48_IE_MOBILE_ID; |
| 323 | buf[1] = TMSI_LEN; |
| 324 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
| 325 | *tptr = htonl(tmsi); |
| 326 | |
| 327 | return 7; |
| 328 | } |
| 329 | |
| 330 | static const char bcd_num_digits[] = { |
| 331 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 332 | '8', '9', '*', '#', 'a', 'b', 'c', '\0' |
| 333 | }; |
| 334 | |
| 335 | /* decode a 'called party BCD number' as in 10.5.4.7 */ |
| 336 | u_int8_t decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv) |
| 337 | { |
| 338 | u_int8_t in_len = bcd_lv[0]; |
| 339 | int i; |
| 340 | |
| 341 | if (in_len < 1) |
| 342 | return 0; |
| 343 | |
| 344 | for (i = 2; i <= in_len; i++) { |
| 345 | /* lower nibble */ |
| 346 | output_len--; |
| 347 | if (output_len <= 1) |
| 348 | break; |
| 349 | *output++ = bcd_num_digits[bcd_lv[i] & 0xf]; |
| 350 | |
| 351 | /* higher nibble */ |
| 352 | output_len--; |
| 353 | if (output_len <= 1) |
| 354 | break; |
| 355 | *output++ = bcd_num_digits[bcd_lv[i] >> 4]; |
| 356 | } |
| 357 | if (output_len >= 1) |
| 358 | *output++ = '\0'; |
| 359 | |
| 360 | /* return number type / calling plan */ |
| 361 | return bcd_lv[1] & 0x3f; |
| 362 | } |
| 363 | |
| 364 | /* convert a single ASCII character to call-control BCD */ |
| 365 | static int asc_to_bcd(const char asc) |
| 366 | { |
| 367 | int i; |
| 368 | |
| 369 | for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) { |
| 370 | if (bcd_num_digits[i] == asc) |
| 371 | return i; |
| 372 | } |
| 373 | return -EINVAL; |
| 374 | } |
| 375 | |
| 376 | /* convert a ASCII phone number to 'called party BCD number' */ |
| 377 | int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len, |
| 378 | u_int8_t type, const char *input) |
| 379 | { |
| 380 | int in_len = strlen(input); |
| 381 | int i; |
| 382 | u_int8_t *bcd_cur = bcd_lv + 2; |
| 383 | |
| 384 | if (in_len/2 + 1 > max_len) |
| 385 | return -EIO; |
| 386 | |
| 387 | /* two digits per byte, plus type byte */ |
| 388 | bcd_lv[0] = in_len/2 + 1; |
| 389 | if (in_len % 2) |
| 390 | bcd_lv[0]++; |
| 391 | |
| 392 | /* if the caller wants to create a valid 'calling party BCD |
| 393 | * number', then the extension bit MUST NOT be set. For the |
| 394 | * 'called party BCD number' it MUST be set. *sigh */ |
| 395 | bcd_lv[1] = type; |
| 396 | |
| 397 | for (i = 0; i < in_len; i++) { |
| 398 | int rc = asc_to_bcd(input[i]); |
| 399 | if (rc < 0) |
| 400 | return rc; |
| 401 | if (i % 2 == 0) |
| 402 | *bcd_cur = rc; |
| 403 | else |
| 404 | *bcd_cur++ |= (rc << 4); |
| 405 | } |
| 406 | /* append padding nibble in case of odd length */ |
| 407 | if (i % 2) |
| 408 | *bcd_cur++ |= 0xf0; |
| 409 | |
| 410 | /* return how many bytes we used */ |
| 411 | return (bcd_cur - bcd_lv); |
| 412 | } |
| 413 | |
| 414 | struct msgb *gsm48_msgb_alloc(void) |
| 415 | { |
| 416 | return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM); |
| 417 | } |
| 418 | |
| 419 | int gsm48_sendmsg(struct msgb *msg) |
| 420 | { |
| 421 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data; |
| 422 | |
| 423 | if (msg->lchan) { |
| 424 | msg->trx = msg->lchan->ts->trx; |
| 425 | |
| 426 | if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) { |
| 427 | /* Send a 04.08 call control message, add transaction |
| 428 | * ID and TI flag */ |
| 429 | gh->proto_discr |= msg->lchan->call.transaction_id; |
| 430 | |
| 431 | /* GSM 04.07 Section 11.2.3.1.3 */ |
| 432 | switch (msg->lchan->call.type) { |
| 433 | case GSM_CT_MO: |
| 434 | gh->proto_discr |= 0x80; |
| 435 | break; |
| 436 | case GSM_CT_MT: |
| 437 | break; |
| 438 | case GSM_CT_NONE: |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | msg->l3h = msg->data; |
| 445 | |
| 446 | return rsl_data_request(msg, 0); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */ |
| 451 | int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause) |
| 452 | { |
| 453 | struct msgb *msg = gsm48_msgb_alloc(); |
| 454 | struct gsm48_hdr *gh; |
| 455 | |
| 456 | msg->lchan = lchan; |
| 457 | |
| 458 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 459 | gh->proto_discr = GSM48_PDISC_MM; |
| 460 | gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT; |
| 461 | gh->data[0] = cause; |
| 462 | |
| 463 | DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr); |
| 464 | |
| 465 | return gsm48_sendmsg(msg); |
| 466 | } |
| 467 | |
| 468 | /* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */ |
| 469 | int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi) |
| 470 | { |
| 471 | struct gsm_bts *bts = lchan->ts->trx->bts; |
| 472 | struct msgb *msg = gsm48_msgb_alloc(); |
| 473 | struct gsm48_hdr *gh; |
| 474 | struct gsm48_loc_area_id *lai; |
| 475 | u_int8_t *mid; |
| 476 | int ret; |
| 477 | |
| 478 | msg->lchan = lchan; |
| 479 | |
| 480 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 481 | gh->proto_discr = GSM48_PDISC_MM; |
| 482 | gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT; |
| 483 | |
| 484 | lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai)); |
| 485 | gsm0408_generate_lai(lai, bts->network->country_code, |
| 486 | bts->network->network_code, bts->location_area_code); |
| 487 | |
| 488 | mid = msgb_put(msg, MID_TMSI_LEN); |
| 489 | generate_mid_from_tmsi(mid, tmsi); |
| 490 | |
| 491 | DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n"); |
| 492 | |
| 493 | ret = gsm48_sendmsg(msg); |
| 494 | |
| 495 | ret = gsm48_tx_mm_info(lchan); |
| 496 | |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | static char bcd2char(u_int8_t bcd) |
| 501 | { |
| 502 | if (bcd < 0xa) |
| 503 | return '0' + bcd; |
| 504 | else |
| 505 | return 'A' + (bcd - 0xa); |
| 506 | } |
| 507 | |
| 508 | /* Convert Mobile Identity (10.5.1.4) to string */ |
| 509 | static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len) |
| 510 | { |
| 511 | int i; |
| 512 | u_int8_t mi_type; |
| 513 | char *str_cur = string; |
| 514 | u_int32_t tmsi; |
| 515 | |
| 516 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 517 | |
| 518 | switch (mi_type) { |
| 519 | case GSM_MI_TYPE_NONE: |
| 520 | break; |
| 521 | case GSM_MI_TYPE_TMSI: |
| 522 | /* Table 10.5.4.3, reverse generate_mid_from_tmsi */ |
| 523 | if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) { |
| 524 | memcpy(&tmsi, &mi[1], 4); |
| 525 | tmsi = ntohl(tmsi); |
| 526 | return snprintf(string, str_len, "%u", tmsi); |
| 527 | } |
| 528 | break; |
| 529 | case GSM_MI_TYPE_IMSI: |
| 530 | case GSM_MI_TYPE_IMEI: |
| 531 | case GSM_MI_TYPE_IMEISV: |
| 532 | *str_cur++ = bcd2char(mi[0] >> 4); |
| 533 | |
| 534 | for (i = 1; i < mi_len; i++) { |
| 535 | if (str_cur + 2 >= string + str_len) |
| 536 | return str_cur - string; |
| 537 | *str_cur++ = bcd2char(mi[i] & 0xf); |
| 538 | /* skip last nibble in last input byte when GSM_EVEN */ |
| 539 | if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD)) |
| 540 | *str_cur++ = bcd2char(mi[i] >> 4); |
| 541 | } |
| 542 | break; |
| 543 | default: |
| 544 | break; |
| 545 | } |
| 546 | *str_cur++ = '\0'; |
| 547 | |
| 548 | return str_cur - string; |
| 549 | } |
| 550 | |
| 551 | /* Transmit Chapter 9.2.10 Identity Request */ |
| 552 | static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type) |
| 553 | { |
| 554 | struct msgb *msg = gsm48_msgb_alloc(); |
| 555 | struct gsm48_hdr *gh; |
| 556 | |
| 557 | msg->lchan = lchan; |
| 558 | |
| 559 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 560 | gh->proto_discr = GSM48_PDISC_MM; |
| 561 | gh->msg_type = GSM48_MT_MM_ID_REQ; |
| 562 | gh->data[0] = id_type; |
| 563 | |
| 564 | return gsm48_sendmsg(msg); |
| 565 | } |
| 566 | |
| 567 | #define MI_SIZE 32 |
| 568 | |
| 569 | /* Parse Chapter 9.2.11 Identity Response */ |
| 570 | static int mm_rx_id_resp(struct msgb *msg) |
| 571 | { |
| 572 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 573 | struct gsm_lchan *lchan = msg->lchan; |
| 574 | u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK; |
| 575 | char mi_string[MI_SIZE]; |
| 576 | |
| 577 | mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]); |
| 578 | DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n", |
| 579 | mi_type, mi_string); |
| 580 | |
| 581 | /* |
| 582 | * Rogue messages could trick us but so is life |
| 583 | */ |
| 584 | put_lchan(lchan); |
| 585 | |
| 586 | switch (mi_type) { |
| 587 | case GSM_MI_TYPE_IMSI: |
| 588 | if (!lchan->subscr) |
| 589 | lchan->subscr = db_create_subscriber(mi_string); |
| 590 | if (lchan->loc_operation) |
| 591 | lchan->loc_operation->waiting_for_imsi = 0; |
| 592 | break; |
| 593 | case GSM_MI_TYPE_IMEI: |
| 594 | case GSM_MI_TYPE_IMEISV: |
| 595 | /* update subscribe <-> IMEI mapping */ |
| 596 | if (lchan->subscr) |
| 597 | db_subscriber_assoc_imei(lchan->subscr, mi_string); |
| 598 | if (lchan->loc_operation) |
| 599 | lchan->loc_operation->waiting_for_imei = 0; |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | /* Check if we can let the mobile station enter */ |
| 604 | return gsm0408_authorize(lchan, msg); |
| 605 | } |
| 606 | |
| 607 | |
| 608 | static void loc_upd_rej_cb(void *data) |
| 609 | { |
| 610 | struct gsm_lchan *lchan = data; |
| 611 | |
| 612 | release_loc_updating_req(lchan); |
| 613 | gsm0408_loc_upd_rej(lchan, reject_cause); |
| 614 | lchan_auto_release(lchan); |
| 615 | } |
| 616 | |
| 617 | static void schedule_reject(struct gsm_lchan *lchan) |
| 618 | { |
| 619 | lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb; |
| 620 | lchan->loc_operation->updating_timer.data = lchan; |
| 621 | bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0); |
| 622 | } |
| 623 | |
| 624 | static const char *lupd_name(u_int8_t type) |
| 625 | { |
| 626 | switch (type) { |
| 627 | case GSM48_LUPD_NORMAL: |
| 628 | return "NORMAL"; |
| 629 | case GSM48_LUPD_PERIODIC: |
| 630 | return "PEROIDOC"; |
| 631 | case GSM48_LUPD_IMSI_ATT: |
| 632 | return "IMSI ATTACH"; |
| 633 | default: |
| 634 | return "UNKNOWN"; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | #define MI_SIZE 32 |
| 639 | /* Chapter 9.2.15: Receive Location Updating Request */ |
| 640 | static int mm_rx_loc_upd_req(struct msgb *msg) |
| 641 | { |
| 642 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 643 | struct gsm48_loc_upd_req *lu; |
| 644 | struct gsm_subscriber *subscr; |
| 645 | struct gsm_lchan *lchan = msg->lchan; |
| 646 | u_int8_t mi_type; |
| 647 | char mi_string[MI_SIZE]; |
| 648 | int rc; |
| 649 | |
| 650 | lu = (struct gsm48_loc_upd_req *) gh->data; |
| 651 | |
| 652 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
| 653 | |
| 654 | mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
| 655 | |
| 656 | DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s) type=%s\n", mi_type, mi_string, |
| 657 | lupd_name(lu->type)); |
| 658 | |
| 659 | /* |
| 660 | * Pseudo Spoof detection: Just drop a second/concurrent |
| 661 | * location updating request. |
| 662 | */ |
| 663 | if (lchan->loc_operation) { |
| 664 | DEBUGP(DMM, "LUPDREQ: ignoring request due an existing one: %p.\n", |
| 665 | lchan->loc_operation); |
| 666 | gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR); |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | allocate_loc_updating_req(lchan); |
| 671 | |
| 672 | switch (mi_type) { |
| 673 | case GSM_MI_TYPE_IMSI: |
| 674 | /* we always want the IMEI, too */ |
| 675 | use_lchan(lchan); |
| 676 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI); |
| 677 | lchan->loc_operation->waiting_for_imei = 1; |
| 678 | |
| 679 | /* look up subscriber based on IMSI */ |
| 680 | subscr = db_create_subscriber(mi_string); |
| 681 | break; |
| 682 | case GSM_MI_TYPE_TMSI: |
| 683 | /* we always want the IMEI, too */ |
| 684 | use_lchan(lchan); |
| 685 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI); |
| 686 | lchan->loc_operation->waiting_for_imei = 1; |
| 687 | |
| 688 | /* look up the subscriber based on TMSI, request IMSI if it fails */ |
| 689 | subscr = subscr_get_by_tmsi(mi_string); |
| 690 | if (!subscr) { |
| 691 | /* send IDENTITY REQUEST message to get IMSI */ |
| 692 | use_lchan(lchan); |
| 693 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI); |
| 694 | lchan->loc_operation->waiting_for_imsi = 1; |
| 695 | } |
| 696 | break; |
| 697 | case GSM_MI_TYPE_IMEI: |
| 698 | case GSM_MI_TYPE_IMEISV: |
| 699 | /* no sim card... FIXME: what to do ? */ |
| 700 | DEBUGP(DMM, "unimplemented mobile identity type\n"); |
| 701 | break; |
| 702 | default: |
| 703 | DEBUGP(DMM, "unknown mobile identity type\n"); |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | lchan->subscr = subscr; |
| 708 | |
| 709 | /* |
| 710 | * Schedule the reject timer and check if we can let the |
| 711 | * subscriber into our network immediately or if we need to wait |
| 712 | * for identity responses. |
| 713 | */ |
| 714 | schedule_reject(lchan); |
| 715 | return gsm0408_authorize(lchan, msg); |
| 716 | } |
| 717 | |
| 718 | /* 9.1.5 Channel mode modify */ |
| 719 | int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode) |
| 720 | { |
| 721 | struct msgb *msg = gsm48_msgb_alloc(); |
| 722 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 723 | struct gsm48_chan_mode_modify *cmm = |
| 724 | (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm)); |
| 725 | u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff; |
| 726 | |
| 727 | DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode); |
| 728 | |
| 729 | lchan->tch_mode = mode; |
| 730 | msg->lchan = lchan; |
| 731 | gh->proto_discr = GSM48_PDISC_RR; |
| 732 | gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF; |
| 733 | |
| 734 | /* fill the channel information element, this code |
| 735 | * should probably be shared with rsl_rx_chan_rqd() */ |
| 736 | cmm->chan_desc.chan_nr = lchan2chan_nr(lchan); |
| 737 | cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc; |
| 738 | cmm->chan_desc.h0.h = 0; |
| 739 | cmm->chan_desc.h0.arfcn_high = arfcn >> 8; |
| 740 | cmm->chan_desc.h0.arfcn_low = arfcn & 0xff; |
| 741 | cmm->mode = mode; |
| 742 | |
| 743 | return gsm48_sendmsg(msg); |
| 744 | } |
| 745 | |
| 746 | /* Section 9.2.15a */ |
| 747 | int gsm48_tx_mm_info(struct gsm_lchan *lchan) |
| 748 | { |
| 749 | struct msgb *msg = gsm48_msgb_alloc(); |
| 750 | struct gsm48_hdr *gh; |
| 751 | struct gsm_network *net = lchan->ts->trx->bts->network; |
| 752 | u_int8_t *ptr8; |
| 753 | u_int16_t *ptr16; |
| 754 | int name_len; |
| 755 | int i; |
| 756 | |
| 757 | msg->lchan = lchan; |
| 758 | |
| 759 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 760 | gh->proto_discr = GSM48_PDISC_MM; |
| 761 | gh->msg_type = GSM48_MT_MM_INFO; |
| 762 | |
| 763 | if (net->name_long) { |
| 764 | name_len = strlen(net->name_long); |
| 765 | /* 10.5.3.5a */ |
| 766 | ptr8 = msgb_put(msg, 3); |
| 767 | ptr8[0] = GSM48_IE_NAME_LONG; |
| 768 | ptr8[1] = name_len*2 +1; |
| 769 | ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */ |
| 770 | |
| 771 | ptr16 = (u_int16_t *) msgb_put(msg, name_len*2); |
| 772 | for (i = 0; i < name_len; i++) |
| 773 | ptr16[i] = htons(net->name_long[i]); |
| 774 | |
| 775 | /* FIXME: Use Cell Broadcast, not UCS-2, since |
| 776 | * UCS-2 is only supported by later revisions of the spec */ |
| 777 | } |
| 778 | |
| 779 | if (net->name_short) { |
| 780 | name_len = strlen(net->name_short); |
| 781 | /* 10.5.3.5a */ |
| 782 | ptr8 = (u_int8_t *) msgb_put(msg, 3); |
| 783 | ptr8[0] = GSM48_IE_NAME_LONG; |
| 784 | ptr8[1] = name_len*2 + 1; |
| 785 | ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */ |
| 786 | |
| 787 | ptr16 = (u_int16_t *) msgb_put(msg, name_len*2); |
| 788 | for (i = 0; i < name_len; i++) |
| 789 | ptr16[i] = htons(net->name_short[i]); |
| 790 | } |
| 791 | |
| 792 | #if 0 |
| 793 | /* move back to the top */ |
| 794 | time_t cur_t; |
| 795 | struct tm* cur_time; |
| 796 | int tz15min; |
| 797 | /* Section 10.5.3.9 */ |
| 798 | cur_t = time(NULL); |
| 799 | cur_time = gmtime(cur_t); |
| 800 | ptr8 = msgb_put(msg, 8); |
| 801 | ptr8[0] = GSM48_IE_NET_TIME_TZ; |
| 802 | ptr8[1] = to_bcd8(cur_time->tm_year % 100); |
| 803 | ptr8[2] = to_bcd8(cur_time->tm_mon); |
| 804 | ptr8[3] = to_bcd8(cur_time->tm_mday); |
| 805 | ptr8[4] = to_bcd8(cur_time->tm_hour); |
| 806 | ptr8[5] = to_bcd8(cur_time->tm_min); |
| 807 | ptr8[6] = to_bcd8(cur_time->tm_sec); |
| 808 | /* 02.42: coded as BCD encoded signed value in units of 15 minutes */ |
| 809 | tz15min = (cur_time->tm_gmtoff)/(60*15); |
| 810 | ptr8[6] = to_bcd8(tz15min); |
| 811 | if (tz15min < 0) |
| 812 | ptr8[6] |= 0x80; |
| 813 | #endif |
| 814 | |
| 815 | return gsm48_sendmsg(msg); |
| 816 | } |
| 817 | |
| 818 | static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan) |
| 819 | { |
| 820 | DEBUGP(DMM, "-> CM SERVICE ACK\n"); |
| 821 | return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC); |
| 822 | } |
| 823 | |
| 824 | /* 9.2.6 CM service reject */ |
| 825 | static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan, |
| 826 | enum gsm48_reject_value value) |
| 827 | { |
| 828 | struct msgb *msg = gsm48_msgb_alloc(); |
| 829 | struct gsm48_hdr *gh; |
| 830 | |
| 831 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 832 | |
| 833 | msg->lchan = lchan; |
| 834 | use_lchan(lchan); |
| 835 | |
| 836 | gh->proto_discr = GSM48_PDISC_MM; |
| 837 | gh->msg_type = GSM48_MT_MM_CM_SERV_REJ; |
| 838 | gh->data[0] = value; |
| 839 | DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value); |
| 840 | |
| 841 | return gsm48_sendmsg(msg); |
| 842 | } |
| 843 | |
| 844 | |
| 845 | /* |
| 846 | * Handle CM Service Requests |
| 847 | * a) Verify that the packet is long enough to contain the information |
| 848 | * we require otherwsie reject with INCORRECT_MESSAGE |
| 849 | * b) Try to parse the TMSI. If we do not have one reject |
| 850 | * c) Check that we know the subscriber with the TMSI otherwise reject |
| 851 | * with a HLR cause |
| 852 | * d) Set the subscriber on the gsm_lchan and accept |
| 853 | */ |
| 854 | static int gsm48_rx_mm_serv_req(struct msgb *msg) |
| 855 | { |
| 856 | u_int8_t mi_type; |
| 857 | char mi_string[MI_SIZE]; |
| 858 | |
| 859 | struct gsm_subscriber *subscr; |
| 860 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 861 | struct gsm48_service_request *req = |
| 862 | (struct gsm48_service_request *)gh->data; |
| 863 | /* unfortunately in Phase1 the classmar2 length is variable */ |
| 864 | u_int8_t classmark2_len = gh->data[1]; |
| 865 | u_int8_t *classmark2 = gh->data+2; |
| 866 | u_int8_t mi_len = *(classmark2 + classmark2_len); |
| 867 | u_int8_t *mi = (classmark2 + classmark2_len + 1); |
| 868 | |
| 869 | DEBUGP(DMM, "<- CM SERVICE REQUEST "); |
| 870 | if (msg->data_len < sizeof(struct gsm48_service_request*)) { |
| 871 | DEBUGPC(DMM, "wrong sized message\n"); |
| 872 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 873 | GSM48_REJECT_INCORRECT_MESSAGE); |
| 874 | } |
| 875 | |
| 876 | if (msg->data_len < req->mi_len + 6) { |
| 877 | DEBUGPC(DMM, "does not fit in packet\n"); |
| 878 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 879 | GSM48_REJECT_INCORRECT_MESSAGE); |
| 880 | } |
| 881 | |
| 882 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 883 | if (mi_type != GSM_MI_TYPE_TMSI) { |
| 884 | DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type); |
| 885 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 886 | GSM48_REJECT_INCORRECT_MESSAGE); |
| 887 | } |
| 888 | |
| 889 | mi_to_string(mi_string, sizeof(mi_string), mi, mi_len); |
| 890 | DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n", |
| 891 | req->cm_service_type, mi_type, mi_string); |
| 892 | |
| 893 | subscr = subscr_get_by_tmsi(mi_string); |
| 894 | |
| 895 | /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */ |
| 896 | if (!subscr) |
| 897 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 898 | GSM48_REJECT_IMSI_UNKNOWN_IN_HLR); |
| 899 | |
| 900 | if (!msg->lchan->subscr) |
| 901 | msg->lchan->subscr = subscr; |
| 902 | else if (msg->lchan->subscr != subscr) { |
| 903 | DEBUGP(DMM, "<- CM Channel already owned by someone else?\n"); |
| 904 | subscr_put(subscr); |
| 905 | } |
| 906 | |
| 907 | subscr->classmark2_len = classmark2_len; |
| 908 | memcpy(subscr->classmark2, classmark2, classmark2_len); |
| 909 | |
| 910 | return gsm48_tx_mm_serv_ack(msg->lchan); |
| 911 | } |
| 912 | |
| 913 | static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg) |
| 914 | { |
| 915 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 916 | struct gsm48_imsi_detach_ind *idi = |
| 917 | (struct gsm48_imsi_detach_ind *) gh->data; |
| 918 | u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK; |
| 919 | char mi_string[MI_SIZE]; |
| 920 | struct gsm_subscriber *subscr; |
| 921 | |
| 922 | mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len); |
| 923 | DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ", |
| 924 | mi_type, mi_string); |
| 925 | |
| 926 | switch (mi_type) { |
| 927 | case GSM_MI_TYPE_TMSI: |
| 928 | subscr = subscr_get_by_tmsi(mi_string); |
| 929 | break; |
| 930 | case GSM_MI_TYPE_IMSI: |
| 931 | subscr = subscr_get_by_imsi(mi_string); |
| 932 | break; |
| 933 | case GSM_MI_TYPE_IMEI: |
| 934 | case GSM_MI_TYPE_IMEISV: |
| 935 | /* no sim card... FIXME: what to do ? */ |
| 936 | DEBUGPC(DMM, "unimplemented mobile identity type\n"); |
| 937 | break; |
| 938 | default: |
| 939 | DEBUGPC(DMM, "unknown mobile identity type\n"); |
| 940 | break; |
| 941 | } |
| 942 | |
| 943 | if (subscr) { |
| 944 | subscr_update(subscr, msg->trx->bts, |
| 945 | GSM_SUBSCRIBER_UPDATE_DETACHED); |
| 946 | DEBUGP(DMM, "Subscriber: %s\n", |
| 947 | subscr->name ? subscr->name : subscr->imsi); |
| 948 | subscr_put(subscr); |
| 949 | } else |
| 950 | DEBUGP(DMM, "Unknown Subscriber ?!?\n"); |
| 951 | |
| 952 | put_lchan(msg->lchan); |
| 953 | |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | static int gsm48_rx_mm_status(struct msgb *msg) |
| 958 | { |
| 959 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 960 | |
| 961 | DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]); |
| 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | /* Receive a GSM 04.08 Mobility Management (MM) message */ |
| 967 | static int gsm0408_rcv_mm(struct msgb *msg) |
| 968 | { |
| 969 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 970 | int rc; |
| 971 | |
| 972 | switch (gh->msg_type & 0xbf) { |
| 973 | case GSM48_MT_MM_LOC_UPD_REQUEST: |
| 974 | DEBUGP(DMM, "LOCATION UPDATING REQUEST\n"); |
| 975 | rc = mm_rx_loc_upd_req(msg); |
| 976 | break; |
| 977 | case GSM48_MT_MM_ID_RESP: |
| 978 | rc = mm_rx_id_resp(msg); |
| 979 | break; |
| 980 | case GSM48_MT_MM_CM_SERV_REQ: |
| 981 | rc = gsm48_rx_mm_serv_req(msg); |
| 982 | break; |
| 983 | case GSM48_MT_MM_STATUS: |
| 984 | rc = gsm48_rx_mm_status(msg); |
| 985 | break; |
| 986 | case GSM48_MT_MM_TMSI_REALL_COMPL: |
| 987 | DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n", |
| 988 | msg->lchan->subscr ? |
| 989 | msg->lchan->subscr->imsi : |
| 990 | "unknown subscriber"); |
| 991 | break; |
| 992 | case GSM48_MT_MM_IMSI_DETACH_IND: |
| 993 | rc = gsm48_rx_mm_imsi_detach_ind(msg); |
| 994 | break; |
| 995 | case GSM48_MT_MM_CM_REEST_REQ: |
| 996 | DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n"); |
| 997 | break; |
| 998 | case GSM48_MT_MM_AUTH_RESP: |
| 999 | DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n"); |
| 1000 | break; |
| 1001 | default: |
| 1002 | fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n", |
| 1003 | gh->msg_type); |
| 1004 | break; |
| 1005 | } |
| 1006 | |
| 1007 | return rc; |
| 1008 | } |
| 1009 | |
| 1010 | /* Receive a PAGING RESPONSE message from the MS */ |
| 1011 | static int gsm48_rr_rx_pag_resp(struct msgb *msg) |
| 1012 | { |
| 1013 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1014 | u_int8_t *classmark2_lv = gh->data + 1; |
| 1015 | u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv; |
| 1016 | u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK; |
| 1017 | char mi_string[MI_SIZE]; |
| 1018 | struct gsm_subscriber *subscr; |
Harald Welte | 876312f | 2009-06-10 11:21:55 +0800 | [diff] [blame] | 1019 | struct gsm_bts *bts; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1020 | struct paging_signal_data sig_data; |
| 1021 | int rc = 0; |
| 1022 | |
| 1023 | mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv); |
| 1024 | DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n", |
| 1025 | mi_type, mi_string); |
| 1026 | switch (mi_type) { |
| 1027 | case GSM_MI_TYPE_TMSI: |
| 1028 | subscr = subscr_get_by_tmsi(mi_string); |
| 1029 | break; |
| 1030 | case GSM_MI_TYPE_IMSI: |
| 1031 | subscr = subscr_get_by_imsi(mi_string); |
| 1032 | break; |
| 1033 | } |
| 1034 | |
| 1035 | if (!subscr) { |
| 1036 | DEBUGP(DRR, "<- Can't find any subscriber for this ID\n"); |
| 1037 | /* FIXME: request id? close channel? */ |
| 1038 | return -EINVAL; |
| 1039 | } |
| 1040 | DEBUGP(DRR, "<- Channel was requested by %s\n", |
| 1041 | subscr->name ? subscr->name : subscr->imsi); |
| 1042 | |
| 1043 | subscr->classmark2_len = *classmark2_lv; |
| 1044 | memcpy(subscr->classmark2, classmark2_lv+1, *classmark2_lv); |
| 1045 | |
| 1046 | if (!msg->lchan->subscr) { |
| 1047 | msg->lchan->subscr = subscr; |
| 1048 | } else if (msg->lchan->subscr != subscr) { |
| 1049 | DEBUGP(DRR, "<- Channel already owned by someone else?\n"); |
| 1050 | subscr_put(subscr); |
| 1051 | return -EINVAL; |
| 1052 | } else { |
| 1053 | DEBUGP(DRR, "<- Channel already owned by us\n"); |
| 1054 | subscr_put(subscr); |
| 1055 | subscr = msg->lchan->subscr; |
| 1056 | } |
| 1057 | |
| 1058 | sig_data.subscr = subscr; |
| 1059 | sig_data.bts = msg->lchan->ts->trx->bts; |
| 1060 | sig_data.lchan = msg->lchan; |
| 1061 | |
| 1062 | dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data); |
Harald Welte | 876312f | 2009-06-10 11:21:55 +0800 | [diff] [blame] | 1063 | |
| 1064 | /* Stop paging on the bts we received the paging response */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1065 | paging_request_stop(msg->trx->bts, subscr, msg->lchan); |
| 1066 | |
Harald Welte | 876312f | 2009-06-10 11:21:55 +0800 | [diff] [blame] | 1067 | /* Stop paging on all other bts' */ |
| 1068 | bts = NULL; |
| 1069 | do { |
| 1070 | bts = gsm_bts_by_lac(msg->trx->bts->network, subscr->lac, bts); |
| 1071 | if (!bts) |
| 1072 | break; |
| 1073 | if (bts == msg->trx->bts) |
| 1074 | continue; |
| 1075 | /* Stop paging */ |
| 1076 | paging_request_stop(bts, subscr, NULL); |
| 1077 | } while (1); |
| 1078 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1079 | /* FIXME: somehow signal the completion of the PAGING to |
| 1080 | * the entity that requested the paging */ |
| 1081 | |
| 1082 | return rc; |
| 1083 | } |
| 1084 | |
| 1085 | static int gsm48_rx_rr_classmark(struct msgb *msg) |
| 1086 | { |
| 1087 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1088 | struct gsm_subscriber *subscr = msg->lchan->subscr; |
| 1089 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 1090 | u_int8_t cm2_len, cm3_len = 0; |
| 1091 | u_int8_t *cm2, *cm3 = NULL; |
| 1092 | |
| 1093 | DEBUGP(DRR, "CLASSMARK CHANGE "); |
| 1094 | |
| 1095 | /* classmark 2 */ |
| 1096 | cm2_len = gh->data[0]; |
| 1097 | cm2 = &gh->data[1]; |
| 1098 | DEBUGPC(DRR, "CM2(len=%u) ", cm2_len); |
| 1099 | |
| 1100 | if (payload_len > cm2_len + 1) { |
| 1101 | /* we must have a classmark3 */ |
| 1102 | if (gh->data[cm2_len+1] != 0x20) { |
| 1103 | DEBUGPC(DRR, "ERR CM3 TAG\n"); |
| 1104 | return -EINVAL; |
| 1105 | } |
| 1106 | if (cm2_len > 3) { |
| 1107 | DEBUGPC(DRR, "CM2 too long!\n"); |
| 1108 | return -EINVAL; |
| 1109 | } |
| 1110 | |
| 1111 | cm3_len = gh->data[cm2_len+2]; |
| 1112 | cm3 = &gh->data[cm2_len+3]; |
| 1113 | if (cm3_len > 14) { |
| 1114 | DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len); |
| 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len); |
| 1118 | } |
| 1119 | if (subscr) { |
| 1120 | subscr->classmark2_len = cm2_len; |
| 1121 | memcpy(subscr->classmark2, cm2, cm2_len); |
| 1122 | if (cm3) { |
| 1123 | subscr->classmark3_len = cm3_len; |
| 1124 | memcpy(subscr->classmark3, cm3, cm3_len); |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | /* FIXME: store the classmark2/3 values with the equipment register */ |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | static int gsm48_rx_rr_status(struct msgb *msg) |
| 1134 | { |
| 1135 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1136 | |
| 1137 | DEBUGP(DRR, "STATUS rr_cause = %s\n", |
| 1138 | rr_cause_name(gh->data[0])); |
| 1139 | |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | static int gsm48_rx_rr_meas_rep(struct msgb *msg) |
| 1144 | { |
| 1145 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1146 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 1147 | static struct gsm_meas_rep meas_rep; |
| 1148 | |
| 1149 | DEBUGP(DRR, "MEASUREMENT REPORT "); |
| 1150 | parse_meas_rep(&meas_rep, gh->data, payload_len); |
| 1151 | if (meas_rep.flags & MEAS_REP_F_DTX) |
| 1152 | DEBUGPC(DRR, "DTX "); |
| 1153 | if (meas_rep.flags & MEAS_REP_F_BA1) |
| 1154 | DEBUGPC(DRR, "BA1 "); |
| 1155 | if (!(meas_rep.flags & MEAS_REP_F_VALID)) |
| 1156 | DEBUGPC(DRR, "NOT VALID "); |
| 1157 | else |
| 1158 | DEBUGPC(DRR, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ", |
| 1159 | meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub, |
| 1160 | meas_rep.rxqual_sub); |
| 1161 | |
| 1162 | DEBUGPC(DRR, "NUM_NEIGH=%u\n", meas_rep.num_cell); |
| 1163 | |
| 1164 | /* FIXME: put the results somwhere */ |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | /* Receive a GSM 04.08 Radio Resource (RR) message */ |
| 1170 | static int gsm0408_rcv_rr(struct msgb *msg) |
| 1171 | { |
| 1172 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1173 | int rc = 0; |
| 1174 | |
| 1175 | switch (gh->msg_type) { |
| 1176 | case GSM48_MT_RR_CLSM_CHG: |
| 1177 | rc = gsm48_rx_rr_classmark(msg); |
| 1178 | break; |
| 1179 | case GSM48_MT_RR_GPRS_SUSP_REQ: |
| 1180 | DEBUGP(DRR, "GRPS SUSPEND REQUEST\n"); |
| 1181 | break; |
| 1182 | case GSM48_MT_RR_PAG_RESP: |
| 1183 | rc = gsm48_rr_rx_pag_resp(msg); |
| 1184 | break; |
| 1185 | case GSM48_MT_RR_CHAN_MODE_MODIF_ACK: |
| 1186 | DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n"); |
| 1187 | rc = rsl_chan_mode_modify_req(msg->lchan); |
| 1188 | break; |
| 1189 | case GSM48_MT_RR_STATUS: |
| 1190 | rc = gsm48_rx_rr_status(msg); |
| 1191 | break; |
| 1192 | case GSM48_MT_RR_MEAS_REP: |
| 1193 | rc = gsm48_rx_rr_meas_rep(msg); |
| 1194 | break; |
| 1195 | default: |
| 1196 | fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n", |
| 1197 | gh->msg_type); |
| 1198 | break; |
| 1199 | } |
| 1200 | |
| 1201 | return rc; |
| 1202 | } |
| 1203 | |
| 1204 | /* 7.1.7 and 9.1.7 Channel release*/ |
| 1205 | int gsm48_send_rr_release(struct gsm_lchan *lchan) |
| 1206 | { |
| 1207 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1208 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1209 | u_int8_t *cause; |
| 1210 | |
| 1211 | msg->lchan = lchan; |
| 1212 | gh->proto_discr = GSM48_PDISC_RR; |
| 1213 | gh->msg_type = GSM48_MT_RR_CHAN_REL; |
| 1214 | |
| 1215 | cause = msgb_put(msg, 1); |
| 1216 | cause[0] = GSM48_RR_CAUSE_NORMAL; |
| 1217 | |
| 1218 | DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n", |
| 1219 | lchan->nr, lchan->type); |
| 1220 | |
| 1221 | return gsm48_sendmsg(msg); |
| 1222 | } |
| 1223 | |
| 1224 | /* Call Control */ |
| 1225 | |
| 1226 | /* The entire call control code is written in accordance with Figure 7.10c |
| 1227 | * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE |
| 1228 | * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY |
| 1229 | * it for voice */ |
| 1230 | |
| 1231 | static int gsm48_cc_tx_status(struct gsm_lchan *lchan) |
| 1232 | { |
| 1233 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1234 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1235 | u_int8_t *cause, *call_state; |
| 1236 | |
| 1237 | gh->proto_discr = GSM48_PDISC_CC; |
| 1238 | |
| 1239 | msg->lchan = lchan; |
| 1240 | |
| 1241 | gh->msg_type = GSM48_MT_CC_STATUS; |
| 1242 | |
| 1243 | cause = msgb_put(msg, 3); |
| 1244 | cause[0] = 2; |
| 1245 | cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER; |
| 1246 | cause[2] = 0x80 | 30; /* response to status inquiry */ |
| 1247 | |
| 1248 | call_state = msgb_put(msg, 1); |
| 1249 | call_state[0] = 0xc0 | 0x00; |
| 1250 | |
| 1251 | return gsm48_sendmsg(msg); |
| 1252 | } |
| 1253 | |
| 1254 | static int gsm48_tx_simple(struct gsm_lchan *lchan, |
| 1255 | u_int8_t pdisc, u_int8_t msg_type) |
| 1256 | { |
| 1257 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1258 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1259 | |
| 1260 | msg->lchan = lchan; |
| 1261 | |
| 1262 | gh->proto_discr = pdisc; |
| 1263 | gh->msg_type = msg_type; |
| 1264 | |
| 1265 | return gsm48_sendmsg(msg); |
| 1266 | } |
| 1267 | |
| 1268 | /* call-back from paging the B-end of the connection */ |
| 1269 | static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event, |
| 1270 | struct msgb *msg, void *_lchan, void *param) |
| 1271 | { |
| 1272 | struct gsm_lchan *lchan = _lchan; |
| 1273 | struct gsm_call *remote_call = param; |
| 1274 | struct gsm_call *call = &lchan->call; |
| 1275 | int rc = 0; |
| 1276 | |
| 1277 | if (hooknum != GSM_HOOK_RR_PAGING) |
| 1278 | return -EINVAL; |
| 1279 | |
| 1280 | switch (event) { |
| 1281 | case GSM_PAGING_SUCCEEDED: |
| 1282 | DEBUGP(DCC, "paging succeeded!\n"); |
| 1283 | remote_call->remote_lchan = lchan; |
| 1284 | call->remote_lchan = remote_call->local_lchan; |
| 1285 | /* send SETUP request to called party */ |
| 1286 | rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr); |
| 1287 | if (is_ipaccess_bts(lchan->ts->trx->bts)) |
| 1288 | rsl_ipacc_bind(lchan); |
| 1289 | break; |
| 1290 | case GSM_PAGING_EXPIRED: |
| 1291 | DEBUGP(DCC, "paging expired!\n"); |
| 1292 | /* notify caller that we cannot reach called party */ |
| 1293 | /* FIXME: correct cause, etc */ |
| 1294 | rc = gsm48_tx_simple(remote_call->local_lchan, GSM48_PDISC_CC, |
| 1295 | GSM48_MT_CC_RELEASE_COMPL); |
| 1296 | break; |
| 1297 | } |
| 1298 | return rc; |
| 1299 | } |
| 1300 | |
| 1301 | static int gsm48_cc_rx_status_enq(struct msgb *msg) |
| 1302 | { |
| 1303 | DEBUGP(DCC, "-> STATUS ENQ\n"); |
| 1304 | return gsm48_cc_tx_status(msg->lchan); |
| 1305 | } |
| 1306 | |
| 1307 | static int gsm48_cc_rx_setup(struct msgb *msg) |
| 1308 | { |
| 1309 | struct gsm_call *call = &msg->lchan->call; |
| 1310 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1311 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 1312 | struct gsm_subscriber *called_subscr; |
Harald Welte | 876312f | 2009-06-10 11:21:55 +0800 | [diff] [blame] | 1313 | struct gsm_bts *bts; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1314 | char called_number[(43-2)*2 + 1] = "\0"; |
| 1315 | struct tlv_parsed tp; |
| 1316 | u_int8_t num_type; |
| 1317 | int ret; |
| 1318 | |
| 1319 | if (call->state == GSM_CSTATE_NULL || |
| 1320 | call->state == GSM_CSTATE_RELEASE_REQ) |
| 1321 | use_lchan(msg->lchan); |
| 1322 | |
| 1323 | call->type = GSM_CT_MO; |
| 1324 | call->state = GSM_CSTATE_INITIATED; |
| 1325 | call->local_lchan = msg->lchan; |
| 1326 | call->transaction_id = gh->proto_discr & 0xf0; |
| 1327 | |
| 1328 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0); |
| 1329 | if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) |
| 1330 | goto err; |
| 1331 | |
| 1332 | /* Parse the number that was dialed and lookup subscriber */ |
| 1333 | num_type = decode_bcd_number(called_number, sizeof(called_number), |
| 1334 | TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1); |
| 1335 | |
| 1336 | DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id, |
| 1337 | called_number); |
| 1338 | |
| 1339 | called_subscr = subscr_get_by_extension(called_number); |
| 1340 | if (!called_subscr) { |
| 1341 | DEBUGP(DCC, "could not find subscriber, RELEASE\n"); |
| 1342 | put_lchan(msg->lchan); |
| 1343 | return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1344 | GSM48_MT_CC_RELEASE_COMPL); |
| 1345 | } |
| 1346 | if (called_subscr == msg->lchan->subscr) { |
| 1347 | DEBUGP(DCC, "subscriber calling himself ?!?\n"); |
| 1348 | put_lchan(msg->lchan); |
| 1349 | subscr_put(called_subscr); |
| 1350 | return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1351 | GSM48_MT_CC_RELEASE_COMPL); |
| 1352 | } |
| 1353 | |
| 1354 | subscr_get(msg->lchan->subscr); |
| 1355 | call->called_subscr = called_subscr; |
| 1356 | |
Harald Welte | 876312f | 2009-06-10 11:21:55 +0800 | [diff] [blame] | 1357 | /* Start paging subscriber on all BTS in LAC of subscriber */ |
| 1358 | bts = NULL; |
| 1359 | do { |
Holger Hans Peter Freyther | a4a5eac | 2009-06-10 02:52:31 +0200 | [diff] [blame^] | 1360 | bts = gsm_bts_by_lac(msg->trx->bts->network, called_subscr->lac, bts); |
Harald Welte | 876312f | 2009-06-10 11:21:55 +0800 | [diff] [blame] | 1361 | if (!bts) |
| 1362 | break; |
| 1363 | /* Trigger paging */ |
| 1364 | paging_request(bts, called_subscr, RSL_CHANNEED_TCH_F, |
| 1365 | setup_trig_pag_evt, call); |
| 1366 | } while (1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1367 | |
| 1368 | /* send a CALL PROCEEDING message to the MO */ |
| 1369 | ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1370 | GSM48_MT_CC_CALL_PROC); |
| 1371 | |
| 1372 | if (is_ipaccess_bts(msg->trx->bts)) |
| 1373 | rsl_ipacc_bind(msg->lchan); |
| 1374 | |
| 1375 | /* change TCH/F mode to voice */ |
| 1376 | return gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR); |
| 1377 | |
| 1378 | err: |
| 1379 | /* FIXME: send some kind of RELEASE */ |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
| 1383 | static int gsm48_cc_rx_alerting(struct msgb *msg) |
| 1384 | { |
| 1385 | struct gsm_call *call = &msg->lchan->call; |
| 1386 | |
| 1387 | DEBUGP(DCC, "A -> ALERTING\n"); |
| 1388 | |
| 1389 | /* forward ALERTING to other party */ |
| 1390 | if (!call->remote_lchan) |
| 1391 | return -EIO; |
| 1392 | |
| 1393 | DEBUGP(DCC, "B <- ALERTING\n"); |
| 1394 | return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC, |
| 1395 | GSM48_MT_CC_ALERTING); |
| 1396 | } |
| 1397 | |
| 1398 | /* map two ipaccess RTP streams onto each other */ |
| 1399 | static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan) |
| 1400 | { |
| 1401 | struct gsm_bts *bts = lchan->ts->trx->bts; |
| 1402 | struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts; |
| 1403 | struct gsm_bts_trx_ts *ts; |
| 1404 | |
| 1405 | DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n", |
| 1406 | bts->nr, lchan->ts->trx->nr, lchan->ts->nr, |
| 1407 | remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr); |
| 1408 | |
| 1409 | if (bts->type != remote_bts->type) { |
| 1410 | DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n"); |
| 1411 | return -EINVAL; |
| 1412 | } |
| 1413 | |
| 1414 | switch (bts->type) { |
| 1415 | case GSM_BTS_TYPE_NANOBTS_900: |
| 1416 | case GSM_BTS_TYPE_NANOBTS_1800: |
| 1417 | ts = remote_lchan->ts; |
| 1418 | rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port, |
| 1419 | lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc); |
| 1420 | |
| 1421 | ts = lchan->ts; |
| 1422 | rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port, |
| 1423 | remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc); |
| 1424 | break; |
| 1425 | case GSM_BTS_TYPE_BS11: |
| 1426 | trau_mux_map_lchan(lchan, remote_lchan); |
| 1427 | break; |
| 1428 | default: |
| 1429 | DEBUGP(DCC, "Unknown BTS type %u\n", bts->type); |
| 1430 | break; |
| 1431 | } |
| 1432 | |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
| 1436 | static int gsm48_cc_rx_connect(struct msgb *msg) |
| 1437 | { |
| 1438 | struct gsm_call *call = &msg->lchan->call; |
| 1439 | int rc; |
| 1440 | |
| 1441 | DEBUGP(DCC, "A -> CONNECT\n"); |
| 1442 | |
| 1443 | rc = tch_map(msg->lchan, call->remote_lchan); |
| 1444 | if (rc) |
| 1445 | return -EIO; |
| 1446 | |
| 1447 | if (!call->remote_lchan) |
| 1448 | return -EIO; |
| 1449 | |
| 1450 | DEBUGP(DCC, "A <- CONNECT ACK\n"); |
| 1451 | /* MT+MO: need to respond with CONNECT_ACK and pass on */ |
| 1452 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1453 | GSM48_MT_CC_CONNECT_ACK); |
| 1454 | |
| 1455 | |
| 1456 | /* forward CONNECT to other party */ |
| 1457 | DEBUGP(DCC, "B <- CONNECT\n"); |
| 1458 | return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC, |
| 1459 | GSM48_MT_CC_CONNECT); |
| 1460 | } |
| 1461 | |
| 1462 | static int gsm48_cc_rx_disconnect(struct msgb *msg) |
| 1463 | { |
| 1464 | struct gsm_call *call = &msg->lchan->call; |
| 1465 | int rc; |
| 1466 | |
| 1467 | |
| 1468 | /* Section 5.4.3.2 */ |
| 1469 | DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n"); |
| 1470 | call->state = GSM_CSTATE_RELEASE_REQ; |
| 1471 | /* FIXME: clear the network connection */ |
| 1472 | DEBUGP(DCC, "A <- RELEASE\n"); |
| 1473 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1474 | GSM48_MT_CC_RELEASE); |
| 1475 | |
| 1476 | /* |
| 1477 | * FIXME: This looks wrong! We should have a single |
| 1478 | * place to do MMCC-REL-CNF/-REQ/-IND and then switch |
| 1479 | * to the NULL state and relase the call |
| 1480 | */ |
| 1481 | subscr_put_channel(msg->lchan); |
| 1482 | |
| 1483 | /* forward DISCONNECT to other party */ |
| 1484 | if (!call->remote_lchan) |
| 1485 | return -EIO; |
| 1486 | |
| 1487 | DEBUGP(DCC, "B <- DISCONNECT\n"); |
| 1488 | return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC, |
| 1489 | GSM48_MT_CC_DISCONNECT); |
| 1490 | } |
| 1491 | |
| 1492 | static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 }; |
| 1493 | |
| 1494 | int gsm48_cc_tx_setup(struct gsm_lchan *lchan, |
| 1495 | struct gsm_subscriber *calling_subscr) |
| 1496 | { |
| 1497 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1498 | struct gsm48_hdr *gh; |
| 1499 | struct gsm_call *call = &lchan->call; |
| 1500 | u_int8_t bcd_lv[19]; |
| 1501 | |
| 1502 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1503 | |
| 1504 | call->type = GSM_CT_MT; |
| 1505 | |
| 1506 | call->local_lchan = msg->lchan = lchan; |
| 1507 | use_lchan(lchan); |
| 1508 | |
| 1509 | gh->proto_discr = GSM48_PDISC_CC; |
| 1510 | gh->msg_type = GSM48_MT_CC_SETUP; |
| 1511 | |
| 1512 | msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE); |
| 1513 | if (calling_subscr) { |
| 1514 | encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9, |
| 1515 | calling_subscr->extension); |
| 1516 | msgb_tlv_put(msg, GSM48_IE_CALLING_BCD, |
| 1517 | bcd_lv[0], bcd_lv+1); |
| 1518 | } |
| 1519 | if (lchan->subscr) { |
| 1520 | encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9, |
| 1521 | lchan->subscr->extension); |
| 1522 | msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, |
| 1523 | bcd_lv[0], bcd_lv+1); |
| 1524 | } |
| 1525 | |
| 1526 | DEBUGP(DCC, "B <- SETUP\n"); |
| 1527 | |
| 1528 | return gsm48_sendmsg(msg); |
| 1529 | } |
| 1530 | |
| 1531 | static int gsm0408_rcv_cc(struct msgb *msg) |
| 1532 | { |
| 1533 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1534 | u_int8_t msg_type = gh->msg_type & 0xbf; |
| 1535 | struct gsm_call *call = &msg->lchan->call; |
| 1536 | int rc = 0; |
| 1537 | |
| 1538 | switch (msg_type) { |
| 1539 | case GSM48_MT_CC_CALL_CONF: |
| 1540 | /* Response to SETUP */ |
| 1541 | DEBUGP(DCC, "-> CALL CONFIRM\n"); |
| 1542 | /* we now need to MODIFY the channel */ |
| 1543 | rc = gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR); |
| 1544 | break; |
| 1545 | case GSM48_MT_CC_RELEASE_COMPL: |
| 1546 | /* Answer from MS to RELEASE */ |
| 1547 | DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n"); |
| 1548 | call->state = GSM_CSTATE_NULL; |
| 1549 | break; |
| 1550 | case GSM48_MT_CC_ALERTING: |
| 1551 | rc = gsm48_cc_rx_alerting(msg); |
| 1552 | break; |
| 1553 | case GSM48_MT_CC_CONNECT: |
| 1554 | rc = gsm48_cc_rx_connect(msg); |
| 1555 | break; |
| 1556 | case GSM48_MT_CC_CONNECT_ACK: |
| 1557 | /* MO: Answer to CONNECT */ |
| 1558 | call->state = GSM_CSTATE_ACTIVE; |
| 1559 | DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n"); |
| 1560 | break; |
| 1561 | case GSM48_MT_CC_RELEASE: |
| 1562 | DEBUGP(DCC, "-> RELEASE\n"); |
| 1563 | DEBUGP(DCC, "<- RELEASE_COMPLETE\n"); |
| 1564 | /* need to respond with RELEASE_COMPLETE */ |
| 1565 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1566 | GSM48_MT_CC_RELEASE_COMPL); |
| 1567 | subscr_put_channel(msg->lchan); |
| 1568 | call->state = GSM_CSTATE_NULL; |
| 1569 | break; |
| 1570 | case GSM48_MT_CC_STATUS_ENQ: |
| 1571 | rc = gsm48_cc_rx_status_enq(msg); |
| 1572 | break; |
| 1573 | case GSM48_MT_CC_DISCONNECT: |
| 1574 | rc = gsm48_cc_rx_disconnect(msg); |
| 1575 | break; |
| 1576 | case GSM48_MT_CC_SETUP: |
| 1577 | /* MO: wants to establish a call */ |
| 1578 | rc = gsm48_cc_rx_setup(msg); |
| 1579 | break; |
| 1580 | case GSM48_MT_CC_EMERG_SETUP: |
| 1581 | DEBUGP(DCC, "-> EMERGENCY SETUP\n"); |
| 1582 | /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */ |
| 1583 | break; |
| 1584 | case GSM48_MT_CC_HOLD: |
| 1585 | DEBUGP(DCC, "-> HOLD (rejecting)\n"); |
| 1586 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1587 | GSM48_MT_CC_HOLD_REJ); |
| 1588 | break; |
| 1589 | case GSM48_MT_CC_RETR: |
| 1590 | DEBUGP(DCC, "-> RETR (rejecting)\n"); |
| 1591 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1592 | GSM48_MT_CC_RETR_REJ); |
| 1593 | break; |
| 1594 | default: |
| 1595 | fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n", |
| 1596 | msg_type); |
| 1597 | break; |
| 1598 | } |
| 1599 | |
| 1600 | return rc; |
| 1601 | } |
| 1602 | |
| 1603 | /* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */ |
| 1604 | int gsm0408_rcvmsg(struct msgb *msg) |
| 1605 | { |
| 1606 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1607 | u_int8_t pdisc = gh->proto_discr & 0x0f; |
| 1608 | int rc = 0; |
| 1609 | |
| 1610 | switch (pdisc) { |
| 1611 | case GSM48_PDISC_CC: |
| 1612 | rc = gsm0408_rcv_cc(msg); |
| 1613 | break; |
| 1614 | case GSM48_PDISC_MM: |
| 1615 | rc = gsm0408_rcv_mm(msg); |
| 1616 | break; |
| 1617 | case GSM48_PDISC_RR: |
| 1618 | rc = gsm0408_rcv_rr(msg); |
| 1619 | break; |
| 1620 | case GSM48_PDISC_SMS: |
| 1621 | rc = gsm0411_rcv_sms(msg); |
| 1622 | break; |
| 1623 | case GSM48_PDISC_MM_GPRS: |
| 1624 | case GSM48_PDISC_SM_GPRS: |
| 1625 | fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n", |
| 1626 | pdisc); |
| 1627 | break; |
| 1628 | default: |
| 1629 | fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n", |
| 1630 | pdisc); |
| 1631 | break; |
| 1632 | } |
| 1633 | |
| 1634 | return rc; |
| 1635 | } |
| 1636 | |
| 1637 | enum chreq_type { |
| 1638 | CHREQ_T_EMERG_CALL, |
| 1639 | CHREQ_T_CALL_REEST_TCH_F, |
| 1640 | CHREQ_T_CALL_REEST_TCH_H, |
| 1641 | CHREQ_T_CALL_REEST_TCH_H_DBL, |
| 1642 | CHREQ_T_SDCCH, |
| 1643 | CHREQ_T_TCH_F, |
| 1644 | CHREQ_T_VOICE_CALL_TCH_H, |
| 1645 | CHREQ_T_DATA_CALL_TCH_H, |
| 1646 | CHREQ_T_LOCATION_UPD, |
| 1647 | CHREQ_T_PAG_R_ANY, |
| 1648 | CHREQ_T_PAG_R_TCH_F, |
| 1649 | CHREQ_T_PAG_R_TCH_FH, |
| 1650 | }; |
| 1651 | |
| 1652 | /* Section 9.1.8 / Table 9.9 */ |
| 1653 | struct chreq { |
| 1654 | u_int8_t val; |
| 1655 | u_int8_t mask; |
| 1656 | enum chreq_type type; |
| 1657 | }; |
| 1658 | |
| 1659 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */ |
| 1660 | static const struct chreq chreq_type_neci1[] = { |
| 1661 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 1662 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F }, |
| 1663 | { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H }, |
| 1664 | { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL }, |
| 1665 | { 0xe0, 0xe0, CHREQ_T_SDCCH }, |
| 1666 | { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H }, |
| 1667 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 1668 | { 0x00, 0xf0, CHREQ_T_LOCATION_UPD }, |
| 1669 | { 0x10, 0xf0, CHREQ_T_SDCCH }, |
| 1670 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 1671 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 1672 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 1673 | }; |
| 1674 | |
| 1675 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */ |
| 1676 | static const struct chreq chreq_type_neci0[] = { |
| 1677 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 1678 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H }, |
| 1679 | { 0xe0, 0xe0, CHREQ_T_TCH_F }, |
| 1680 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 1681 | { 0x00, 0xe0, CHREQ_T_LOCATION_UPD }, |
| 1682 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 1683 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 1684 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 1685 | }; |
| 1686 | |
| 1687 | static const enum gsm_chan_t ctype_by_chreq[] = { |
| 1688 | [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F, |
| 1689 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F, |
| 1690 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H, |
| 1691 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H, |
| 1692 | [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH, |
| 1693 | [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F, |
| 1694 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 1695 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 1696 | [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH, |
| 1697 | [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH, |
| 1698 | [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F, |
| 1699 | [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F, |
| 1700 | }; |
| 1701 | |
| 1702 | static const enum gsm_chreq_reason_t reason_by_chreq[] = { |
| 1703 | [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG, |
| 1704 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL, |
| 1705 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL, |
| 1706 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL, |
| 1707 | [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER, |
| 1708 | [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER, |
| 1709 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 1710 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 1711 | [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD, |
| 1712 | [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG, |
| 1713 | [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG, |
| 1714 | [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG, |
| 1715 | }; |
| 1716 | |
| 1717 | enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 1718 | { |
| 1719 | int i; |
| 1720 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 1721 | |
| 1722 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 1723 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 1724 | if ((ra & chr->mask) == chr->val) |
| 1725 | return ctype_by_chreq[chr->type]; |
| 1726 | } |
| 1727 | fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra); |
| 1728 | return GSM_LCHAN_SDCCH; |
| 1729 | } |
| 1730 | |
| 1731 | enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 1732 | { |
| 1733 | int i; |
| 1734 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 1735 | |
| 1736 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 1737 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 1738 | if ((ra & chr->mask) == chr->val) |
| 1739 | return reason_by_chreq[chr->type]; |
| 1740 | } |
| 1741 | fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra); |
| 1742 | return GSM_CHREQ_REASON_OTHER; |
| 1743 | } |