Holger Hans Peter Freyther | 1494a76 | 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> |
| 28 | #include <netinet/in.h> |
| 29 | |
| 30 | #include <openbsc/msgb.h> |
| 31 | #include <openbsc/debug.h> |
| 32 | #include <openbsc/gsm_04_08.h> |
| 33 | #include <openbsc/transaction.h> |
| 34 | |
| 35 | #define GSM48_ALLOC_SIZE 1024 |
| 36 | #define GSM48_ALLOC_HEADROOM 128 |
| 37 | |
| 38 | /* should ip.access BTS use direct RTP streams between each other (1), |
| 39 | * or should OpenBSC always act as RTP relay/proxy in between (0) ? */ |
| 40 | int ipacc_rtp_direct = 1; |
| 41 | |
| 42 | |
| 43 | const char *gsm0408_cc_msg_names[] = { |
| 44 | "unknown 0x00", |
| 45 | "ALERTING", |
| 46 | "CALL_PROC", |
| 47 | "PROGRESS", |
| 48 | "ESTAB", |
| 49 | "SETUP", |
| 50 | "ESTAB_CONF", |
| 51 | "CONNECT", |
| 52 | "CALL_CONF", |
| 53 | "START_CC", |
| 54 | "unknown 0x0a", |
| 55 | "RECALL", |
| 56 | "unknown 0x0c", |
| 57 | "unknown 0x0d", |
| 58 | "EMERG_SETUP", |
| 59 | "CONNECT_ACK", |
| 60 | "USER_INFO", |
| 61 | "unknown 0x11", |
| 62 | "unknown 0x12", |
| 63 | "MODIFY_REJECT", |
| 64 | "unknown 0x14", |
| 65 | "unknown 0x15", |
| 66 | "unknown 0x16", |
| 67 | "MODIFY", |
| 68 | "HOLD", |
| 69 | "HOLD_ACK", |
| 70 | "HOLD_REJ", |
| 71 | "unknown 0x1b", |
| 72 | "RETR", |
| 73 | "RETR_ACK", |
| 74 | "RETR_REJ", |
| 75 | "MODIFY_COMPL", |
| 76 | "unknown 0x20", |
| 77 | "unknown 0x21", |
| 78 | "unknown 0x22", |
| 79 | "unknown 0x23", |
| 80 | "unknown 0x24", |
| 81 | "DISCONNECT", |
| 82 | "unknown 0x26", |
| 83 | "unknown 0x27", |
| 84 | "unknown 0x28", |
| 85 | "unknown 0x29", |
| 86 | "RELEASE_COMPL", |
| 87 | "unknown 0x2b", |
| 88 | "unknown 0x2c", |
| 89 | "RELEASE", |
| 90 | "unknown 0x2e", |
| 91 | "unknown 0x2f", |
| 92 | "unknown 0x30", |
| 93 | "STOP_DTMF", |
| 94 | "STOP_DTMF_ACK", |
| 95 | "unknown 0x33", |
| 96 | "STATUS_ENQ", |
| 97 | "START_DTMF", |
| 98 | "START_DTMF_ACK", |
| 99 | "START_DTMF_REJ", |
| 100 | "unknown 0x38", |
| 101 | "CONG_CTRL", |
| 102 | "FACILITY", |
| 103 | "unknown 0x3b", |
| 104 | "STATUS", |
| 105 | "unknown 0x3c", |
| 106 | "NOTIFY", |
| 107 | "unknown 0x3f", |
| 108 | }; |
| 109 | |
| 110 | |
| 111 | struct msgb *gsm48_msgb_alloc(void) |
| 112 | { |
| 113 | return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM, |
| 114 | "GSM 04.08"); |
| 115 | } |
| 116 | |
| 117 | int gsm48_sendmsg(struct msgb *msg, struct gsm_trans *trans) |
| 118 | { |
| 119 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data; |
| 120 | |
| 121 | /* if we get passed a transaction reference, do some common |
| 122 | * work that the caller no longer has to do */ |
| 123 | if (trans) { |
| 124 | gh->proto_discr = trans->protocol | (trans->transaction_id << 4); |
| 125 | msg->lchan = trans->lchan; |
| 126 | } |
| 127 | |
| 128 | if (msg->lchan) { |
| 129 | msg->trx = msg->lchan->ts->trx; |
| 130 | |
| 131 | if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) |
| 132 | DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) " |
| 133 | "Sending '%s' to MS.\n", msg->trx->bts->nr, |
| 134 | msg->trx->nr, msg->lchan->ts->nr, |
| 135 | gh->proto_discr & 0xf0, |
| 136 | gsm0408_cc_msg_names[gh->msg_type & 0x3f]); |
| 137 | else |
| 138 | DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) " |
| 139 | "Sending 0x%02x to MS.\n", msg->trx->bts->nr, |
| 140 | msg->trx->nr, msg->lchan->ts->nr, |
| 141 | gh->proto_discr, gh->msg_type); |
| 142 | } |
| 143 | |
| 144 | msg->l3h = msg->data; |
| 145 | |
| 146 | return rsl_data_request(msg, 0); |
| 147 | } |
| 148 | |
| 149 | static void to_bcd(u_int8_t *bcd, u_int16_t val) |
| 150 | { |
| 151 | bcd[2] = val % 10; |
| 152 | val = val / 10; |
| 153 | bcd[1] = val % 10; |
| 154 | val = val / 10; |
| 155 | bcd[0] = val % 10; |
| 156 | val = val / 10; |
| 157 | } |
| 158 | |
| 159 | void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc, |
| 160 | u_int16_t mnc, u_int16_t lac) |
| 161 | { |
| 162 | u_int8_t bcd[3]; |
| 163 | |
| 164 | to_bcd(bcd, mcc); |
| 165 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 166 | lai48->digits[1] = bcd[2]; |
| 167 | |
| 168 | to_bcd(bcd, mnc); |
| 169 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
| 170 | #if 0 |
| 171 | lai48->digits[1] |= bcd[2] << 4; |
| 172 | lai48->digits[2] = bcd[0] | (bcd[1] << 4); |
| 173 | #else |
| 174 | lai48->digits[1] |= 0xf << 4; |
| 175 | lai48->digits[2] = bcd[1] | (bcd[2] << 4); |
| 176 | #endif |
| 177 | |
| 178 | lai48->lac = htons(lac); |
| 179 | } |
| 180 | |
| 181 | int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi) |
| 182 | { |
| 183 | u_int32_t *tptr = (u_int32_t *) &buf[3]; |
| 184 | |
| 185 | buf[0] = GSM48_IE_MOBILE_ID; |
| 186 | buf[1] = GSM48_TMSI_LEN; |
| 187 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
| 188 | *tptr = htonl(tmsi); |
| 189 | |
| 190 | return 7; |
| 191 | } |
| 192 | |
| 193 | /* Section 9.1.8 / Table 9.9 */ |
| 194 | struct chreq { |
| 195 | u_int8_t val; |
| 196 | u_int8_t mask; |
| 197 | enum chreq_type type; |
| 198 | }; |
| 199 | |
| 200 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */ |
| 201 | static const struct chreq chreq_type_neci1[] = { |
| 202 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 203 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F }, |
| 204 | { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H }, |
| 205 | { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL }, |
| 206 | { 0xe0, 0xe0, CHREQ_T_SDCCH }, |
| 207 | { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H }, |
| 208 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 209 | { 0x00, 0xf0, CHREQ_T_LOCATION_UPD }, |
| 210 | { 0x10, 0xf0, CHREQ_T_SDCCH }, |
| 211 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 212 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 213 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 214 | }; |
| 215 | |
| 216 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */ |
| 217 | static const struct chreq chreq_type_neci0[] = { |
| 218 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 219 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H }, |
| 220 | { 0xe0, 0xe0, CHREQ_T_TCH_F }, |
| 221 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 222 | { 0x00, 0xe0, CHREQ_T_LOCATION_UPD }, |
| 223 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 224 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 225 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 226 | }; |
| 227 | |
| 228 | static const enum gsm_chan_t ctype_by_chreq[] = { |
| 229 | [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F, |
| 230 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F, |
| 231 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H, |
| 232 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H, |
| 233 | [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH, |
| 234 | [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F, |
| 235 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 236 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 237 | [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH, |
| 238 | [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH, |
| 239 | [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F, |
| 240 | [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F, |
| 241 | }; |
| 242 | |
| 243 | static const enum gsm_chreq_reason_t reason_by_chreq[] = { |
| 244 | [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG, |
| 245 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL, |
| 246 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL, |
| 247 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL, |
| 248 | [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER, |
| 249 | [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER, |
| 250 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 251 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 252 | [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD, |
| 253 | [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG, |
| 254 | [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG, |
| 255 | [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG, |
| 256 | }; |
| 257 | |
| 258 | enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 259 | { |
| 260 | int i; |
| 261 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 262 | |
| 263 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 264 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 265 | if ((ra & chr->mask) == chr->val) |
| 266 | return ctype_by_chreq[chr->type]; |
| 267 | } |
| 268 | fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra); |
| 269 | return GSM_LCHAN_SDCCH; |
| 270 | } |
| 271 | |
| 272 | enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 273 | { |
| 274 | int i; |
| 275 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 276 | |
| 277 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 278 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 279 | if ((ra & chr->mask) == chr->val) |
| 280 | return reason_by_chreq[chr->type]; |
| 281 | } |
| 282 | fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra); |
| 283 | return GSM_CHREQ_REASON_OTHER; |
| 284 | } |
| 285 | |
| 286 | /* 7.1.7 and 9.1.7: RR CHANnel RELease */ |
| 287 | int gsm48_send_rr_release(struct gsm_lchan *lchan) |
| 288 | { |
| 289 | struct msgb *msg = gsm48_msgb_alloc(); |
| 290 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 291 | u_int8_t *cause; |
| 292 | |
| 293 | msg->lchan = lchan; |
| 294 | gh->proto_discr = GSM48_PDISC_RR; |
| 295 | gh->msg_type = GSM48_MT_RR_CHAN_REL; |
| 296 | |
| 297 | cause = msgb_put(msg, 1); |
| 298 | cause[0] = GSM48_RR_CAUSE_NORMAL; |
| 299 | |
| 300 | DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n", |
| 301 | lchan->nr, lchan->type); |
| 302 | |
| 303 | /* Send actual release request to MS */ |
| 304 | gsm48_sendmsg(msg, NULL); |
| 305 | /* FIXME: Start Timer T3109 */ |
| 306 | |
| 307 | /* Deactivate the SACCH on the BTS side */ |
| 308 | return rsl_deact_sacch(lchan); |
| 309 | } |
| 310 | |