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