Holger Hans Peter Freyther | 3689710 | 2009-08-01 07:26:59 +0200 | [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 | * utility functions |
| 4 | */ |
| 5 | |
| 6 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 7 | * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | * |
| 25 | */ |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Holger Hans Peter Freyther | a269ece | 2009-08-21 04:57:35 +0200 | [diff] [blame^] | 28 | #include <errno.h> |
Holger Hans Peter Freyther | 3689710 | 2009-08-01 07:26:59 +0200 | [diff] [blame] | 29 | #include <netinet/in.h> |
| 30 | |
| 31 | #include <openbsc/msgb.h> |
| 32 | #include <openbsc/debug.h> |
| 33 | #include <openbsc/gsm_04_08.h> |
| 34 | #include <openbsc/transaction.h> |
| 35 | |
| 36 | #define GSM48_ALLOC_SIZE 1024 |
| 37 | #define GSM48_ALLOC_HEADROOM 128 |
| 38 | |
| 39 | /* should ip.access BTS use direct RTP streams between each other (1), |
| 40 | * or should OpenBSC always act as RTP relay/proxy in between (0) ? */ |
| 41 | int ipacc_rtp_direct = 1; |
| 42 | |
| 43 | |
| 44 | const char *gsm0408_cc_msg_names[] = { |
| 45 | "unknown 0x00", |
| 46 | "ALERTING", |
| 47 | "CALL_PROC", |
| 48 | "PROGRESS", |
| 49 | "ESTAB", |
| 50 | "SETUP", |
| 51 | "ESTAB_CONF", |
| 52 | "CONNECT", |
| 53 | "CALL_CONF", |
| 54 | "START_CC", |
| 55 | "unknown 0x0a", |
| 56 | "RECALL", |
| 57 | "unknown 0x0c", |
| 58 | "unknown 0x0d", |
| 59 | "EMERG_SETUP", |
| 60 | "CONNECT_ACK", |
| 61 | "USER_INFO", |
| 62 | "unknown 0x11", |
| 63 | "unknown 0x12", |
| 64 | "MODIFY_REJECT", |
| 65 | "unknown 0x14", |
| 66 | "unknown 0x15", |
| 67 | "unknown 0x16", |
| 68 | "MODIFY", |
| 69 | "HOLD", |
| 70 | "HOLD_ACK", |
| 71 | "HOLD_REJ", |
| 72 | "unknown 0x1b", |
| 73 | "RETR", |
| 74 | "RETR_ACK", |
| 75 | "RETR_REJ", |
| 76 | "MODIFY_COMPL", |
| 77 | "unknown 0x20", |
| 78 | "unknown 0x21", |
| 79 | "unknown 0x22", |
| 80 | "unknown 0x23", |
| 81 | "unknown 0x24", |
| 82 | "DISCONNECT", |
| 83 | "unknown 0x26", |
| 84 | "unknown 0x27", |
| 85 | "unknown 0x28", |
| 86 | "unknown 0x29", |
| 87 | "RELEASE_COMPL", |
| 88 | "unknown 0x2b", |
| 89 | "unknown 0x2c", |
| 90 | "RELEASE", |
| 91 | "unknown 0x2e", |
| 92 | "unknown 0x2f", |
| 93 | "unknown 0x30", |
| 94 | "STOP_DTMF", |
| 95 | "STOP_DTMF_ACK", |
| 96 | "unknown 0x33", |
| 97 | "STATUS_ENQ", |
| 98 | "START_DTMF", |
| 99 | "START_DTMF_ACK", |
| 100 | "START_DTMF_REJ", |
| 101 | "unknown 0x38", |
| 102 | "CONG_CTRL", |
| 103 | "FACILITY", |
| 104 | "unknown 0x3b", |
| 105 | "STATUS", |
| 106 | "unknown 0x3c", |
| 107 | "NOTIFY", |
| 108 | "unknown 0x3f", |
| 109 | }; |
| 110 | |
| 111 | |
| 112 | struct msgb *gsm48_msgb_alloc(void) |
| 113 | { |
| 114 | return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM, |
| 115 | "GSM 04.08"); |
| 116 | } |
| 117 | |
| 118 | int gsm48_sendmsg(struct msgb *msg, struct gsm_trans *trans) |
| 119 | { |
| 120 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data; |
| 121 | |
| 122 | /* if we get passed a transaction reference, do some common |
| 123 | * work that the caller no longer has to do */ |
| 124 | if (trans) { |
| 125 | gh->proto_discr = trans->protocol | (trans->transaction_id << 4); |
| 126 | msg->lchan = trans->lchan; |
| 127 | } |
| 128 | |
| 129 | if (msg->lchan) { |
| 130 | msg->trx = msg->lchan->ts->trx; |
| 131 | |
| 132 | if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) |
| 133 | DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) " |
| 134 | "Sending '%s' to MS.\n", msg->trx->bts->nr, |
| 135 | msg->trx->nr, msg->lchan->ts->nr, |
| 136 | gh->proto_discr & 0xf0, |
| 137 | gsm0408_cc_msg_names[gh->msg_type & 0x3f]); |
| 138 | else |
| 139 | DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) " |
| 140 | "Sending 0x%02x to MS.\n", msg->trx->bts->nr, |
| 141 | msg->trx->nr, msg->lchan->ts->nr, |
| 142 | gh->proto_discr, gh->msg_type); |
| 143 | } |
| 144 | |
| 145 | msg->l3h = msg->data; |
| 146 | |
| 147 | return rsl_data_request(msg, 0); |
| 148 | } |
| 149 | |
| 150 | static void to_bcd(u_int8_t *bcd, u_int16_t val) |
| 151 | { |
| 152 | bcd[2] = val % 10; |
| 153 | val = val / 10; |
| 154 | bcd[1] = val % 10; |
| 155 | val = val / 10; |
| 156 | bcd[0] = val % 10; |
| 157 | val = val / 10; |
| 158 | } |
| 159 | |
Holger Hans Peter Freyther | 1e6ef9f | 2009-08-19 07:54:59 +0200 | [diff] [blame] | 160 | static char bcd2char(u_int8_t bcd) |
| 161 | { |
| 162 | if (bcd < 0xa) |
| 163 | return '0' + bcd; |
| 164 | else |
| 165 | return 'A' + (bcd - 0xa); |
| 166 | } |
| 167 | |
Holger Hans Peter Freyther | 20a0f32 | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 168 | /* only works for numbers in ascci */ |
| 169 | static u_int8_t char2bcd(char c) |
| 170 | { |
| 171 | return c - 0x30; |
| 172 | } |
| 173 | |
Holger Hans Peter Freyther | 1e6ef9f | 2009-08-19 07:54:59 +0200 | [diff] [blame] | 174 | |
Holger Hans Peter Freyther | 3689710 | 2009-08-01 07:26:59 +0200 | [diff] [blame] | 175 | void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc, |
| 176 | u_int16_t mnc, u_int16_t lac) |
| 177 | { |
| 178 | u_int8_t bcd[3]; |
| 179 | |
| 180 | to_bcd(bcd, mcc); |
| 181 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 182 | lai48->digits[1] = bcd[2]; |
| 183 | |
| 184 | to_bcd(bcd, mnc); |
| 185 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
| 186 | #if 0 |
| 187 | lai48->digits[1] |= bcd[2] << 4; |
| 188 | lai48->digits[2] = bcd[0] | (bcd[1] << 4); |
| 189 | #else |
| 190 | lai48->digits[1] |= 0xf << 4; |
| 191 | lai48->digits[2] = bcd[1] | (bcd[2] << 4); |
| 192 | #endif |
| 193 | |
| 194 | lai48->lac = htons(lac); |
| 195 | } |
| 196 | |
Holger Hans Peter Freyther | 20a0f32 | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 197 | int gsm48_generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi) |
Holger Hans Peter Freyther | 3689710 | 2009-08-01 07:26:59 +0200 | [diff] [blame] | 198 | { |
| 199 | u_int32_t *tptr = (u_int32_t *) &buf[3]; |
| 200 | |
| 201 | buf[0] = GSM48_IE_MOBILE_ID; |
| 202 | buf[1] = GSM48_TMSI_LEN; |
| 203 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
| 204 | *tptr = htonl(tmsi); |
| 205 | |
| 206 | return 7; |
| 207 | } |
| 208 | |
Holger Hans Peter Freyther | 20a0f32 | 2009-08-20 08:41:24 +0200 | [diff] [blame] | 209 | int gsm48_generate_mid_from_imsi(u_int8_t *buf, const char *imsi) |
| 210 | { |
| 211 | unsigned int length = strlen(imsi), i, off = 0; |
| 212 | u_int8_t odd = (length & 0x1) == 1; |
| 213 | |
| 214 | buf[0] = GSM48_IE_MOBILE_ID; |
| 215 | buf[2] = char2bcd(imsi[0]) << 4 | GSM_MI_TYPE_IMSI | (odd << 3); |
| 216 | |
| 217 | /* if the length is even we will fill half of the last octet */ |
| 218 | if (odd) |
| 219 | buf[1] = (length + 1) >> 1; |
| 220 | else |
| 221 | buf[1] = (length + 2) >> 1; |
| 222 | |
| 223 | for (i = 1; i < buf[1]; ++i) { |
| 224 | u_int8_t lower, upper; |
| 225 | |
| 226 | lower = char2bcd(imsi[++off]); |
| 227 | if (!odd && off + 1 == length) |
| 228 | upper = 0x0f; |
| 229 | else |
| 230 | upper = char2bcd(imsi[++off]) & 0x0f; |
| 231 | |
| 232 | buf[2 + i] = (upper << 4) | lower; |
| 233 | } |
| 234 | |
| 235 | return 2 + buf[1]; |
| 236 | } |
| 237 | |
Holger Hans Peter Freyther | 3689710 | 2009-08-01 07:26:59 +0200 | [diff] [blame] | 238 | /* Section 9.1.8 / Table 9.9 */ |
| 239 | struct chreq { |
| 240 | u_int8_t val; |
| 241 | u_int8_t mask; |
| 242 | enum chreq_type type; |
| 243 | }; |
| 244 | |
| 245 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */ |
| 246 | static const struct chreq chreq_type_neci1[] = { |
| 247 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 248 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F }, |
| 249 | { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H }, |
| 250 | { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL }, |
| 251 | { 0xe0, 0xe0, CHREQ_T_SDCCH }, |
| 252 | { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H }, |
| 253 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 254 | { 0x00, 0xf0, CHREQ_T_LOCATION_UPD }, |
| 255 | { 0x10, 0xf0, CHREQ_T_SDCCH }, |
| 256 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 257 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 258 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 259 | }; |
| 260 | |
| 261 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */ |
| 262 | static const struct chreq chreq_type_neci0[] = { |
| 263 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 264 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H }, |
| 265 | { 0xe0, 0xe0, CHREQ_T_TCH_F }, |
| 266 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 267 | { 0x00, 0xe0, CHREQ_T_LOCATION_UPD }, |
| 268 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 269 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 270 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 271 | }; |
| 272 | |
| 273 | static const enum gsm_chan_t ctype_by_chreq[] = { |
| 274 | [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F, |
| 275 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F, |
| 276 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H, |
| 277 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H, |
| 278 | [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH, |
| 279 | [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F, |
| 280 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 281 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 282 | [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH, |
| 283 | [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH, |
| 284 | [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F, |
| 285 | [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F, |
| 286 | }; |
| 287 | |
| 288 | static const enum gsm_chreq_reason_t reason_by_chreq[] = { |
| 289 | [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG, |
| 290 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL, |
| 291 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL, |
| 292 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL, |
| 293 | [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER, |
| 294 | [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER, |
| 295 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 296 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 297 | [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD, |
| 298 | [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG, |
| 299 | [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG, |
| 300 | [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG, |
| 301 | }; |
| 302 | |
| 303 | enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 304 | { |
| 305 | int i; |
| 306 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 307 | |
| 308 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 309 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 310 | if ((ra & chr->mask) == chr->val) |
| 311 | return ctype_by_chreq[chr->type]; |
| 312 | } |
| 313 | fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra); |
| 314 | return GSM_LCHAN_SDCCH; |
| 315 | } |
| 316 | |
| 317 | enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 318 | { |
| 319 | int i; |
| 320 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 321 | |
| 322 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 323 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 324 | if ((ra & chr->mask) == chr->val) |
| 325 | return reason_by_chreq[chr->type]; |
| 326 | } |
| 327 | fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra); |
| 328 | return GSM_CHREQ_REASON_OTHER; |
| 329 | } |
| 330 | |
| 331 | /* 7.1.7 and 9.1.7: RR CHANnel RELease */ |
| 332 | int gsm48_send_rr_release(struct gsm_lchan *lchan) |
| 333 | { |
| 334 | struct msgb *msg = gsm48_msgb_alloc(); |
| 335 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 336 | u_int8_t *cause; |
| 337 | |
| 338 | msg->lchan = lchan; |
| 339 | gh->proto_discr = GSM48_PDISC_RR; |
| 340 | gh->msg_type = GSM48_MT_RR_CHAN_REL; |
| 341 | |
| 342 | cause = msgb_put(msg, 1); |
| 343 | cause[0] = GSM48_RR_CAUSE_NORMAL; |
| 344 | |
| 345 | DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n", |
| 346 | lchan->nr, lchan->type); |
| 347 | |
| 348 | /* Send actual release request to MS */ |
| 349 | gsm48_sendmsg(msg, NULL); |
| 350 | /* FIXME: Start Timer T3109 */ |
| 351 | |
| 352 | /* Deactivate the SACCH on the BTS side */ |
| 353 | return rsl_deact_sacch(lchan); |
| 354 | } |
| 355 | |
Holger Hans Peter Freyther | 1e6ef9f | 2009-08-19 07:54:59 +0200 | [diff] [blame] | 356 | /* Convert Mobile Identity (10.5.1.4) to string */ |
| 357 | int gsm48_mi_to_string(char *string, const int str_len, const u_int8_t *mi, const int mi_len) |
| 358 | { |
| 359 | int i; |
| 360 | u_int8_t mi_type; |
| 361 | char *str_cur = string; |
| 362 | u_int32_t tmsi; |
| 363 | |
| 364 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 365 | |
| 366 | switch (mi_type) { |
| 367 | case GSM_MI_TYPE_NONE: |
| 368 | break; |
| 369 | case GSM_MI_TYPE_TMSI: |
| 370 | /* Table 10.5.4.3, reverse generate_mid_from_tmsi */ |
| 371 | if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) { |
| 372 | memcpy(&tmsi, &mi[1], 4); |
| 373 | tmsi = ntohl(tmsi); |
| 374 | return snprintf(string, str_len, "%u", tmsi); |
| 375 | } |
| 376 | break; |
| 377 | case GSM_MI_TYPE_IMSI: |
| 378 | case GSM_MI_TYPE_IMEI: |
| 379 | case GSM_MI_TYPE_IMEISV: |
| 380 | *str_cur++ = bcd2char(mi[0] >> 4); |
| 381 | |
| 382 | for (i = 1; i < mi_len; i++) { |
| 383 | if (str_cur + 2 >= string + str_len) |
| 384 | return str_cur - string; |
| 385 | *str_cur++ = bcd2char(mi[i] & 0xf); |
| 386 | /* skip last nibble in last input byte when GSM_EVEN */ |
| 387 | if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD)) |
| 388 | *str_cur++ = bcd2char(mi[i] >> 4); |
| 389 | } |
| 390 | break; |
| 391 | default: |
| 392 | break; |
| 393 | } |
| 394 | *str_cur++ = '\0'; |
| 395 | |
| 396 | return str_cur - string; |
| 397 | } |
| 398 | |
Holger Hans Peter Freyther | a269ece | 2009-08-21 04:57:35 +0200 | [diff] [blame^] | 399 | |
| 400 | int send_siemens_mrpci(struct gsm_lchan *lchan, |
| 401 | u_int8_t *classmark2_lv) |
| 402 | { |
| 403 | struct rsl_mrpci mrpci; |
| 404 | |
| 405 | if (classmark2_lv[0] < 2) |
| 406 | return -EINVAL; |
| 407 | |
| 408 | mrpci.power_class = classmark2_lv[1] & 0x7; |
| 409 | mrpci.vgcs_capable = classmark2_lv[2] & (1 << 1); |
| 410 | mrpci.vbs_capable = classmark2_lv[2] & (1 <<2); |
| 411 | mrpci.gsm_phase = (classmark2_lv[1]) >> 5 & 0x3; |
| 412 | |
| 413 | return rsl_siemens_mrpci(lchan, &mrpci); |
| 414 | } |
| 415 | |