Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [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 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 5 | * |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 29 | #include <netinet/in.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 30 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 31 | #include <openbsc/msgb.h> |
| 32 | #include <openbsc/debug.h> |
| 33 | #include <openbsc/gsm_data.h> |
| 34 | #include <openbsc/gsm_subscriber.h> |
| 35 | #include <openbsc/gsm_04_08.h> |
| 36 | #include <openbsc/abis_rsl.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 37 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 38 | #define GSM48_ALLOC_SIZE 1024 |
| 39 | #define GSM48_ALLOC_HEADROOM 128 |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 40 | |
| 41 | struct gsm_lai { |
| 42 | u_int16_t mcc; |
| 43 | u_int16_t mnc; |
| 44 | u_int16_t lac; |
| 45 | }; |
| 46 | |
| 47 | static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48) |
| 48 | { |
| 49 | u_int8_t dig[4]; |
| 50 | |
| 51 | /* MCC */ |
| 52 | dig[1] = lai48->digits[0] & 0x0f; |
| 53 | dig[2] = lai48->digits[0] >> 4; |
| 54 | dig[3] = lai48->digits[1] & 0x0f; |
| 55 | lai->mcc = dig[3] * 100 + dig[2]; |
| 56 | |
| 57 | /* MNC */ |
| 58 | dig[1] = lai48->digits[1] >> 4; |
| 59 | dig[2] = lai48->digits[2] & 0x0f; |
| 60 | dig[3] = lai48->digits[2] >> 4; |
| 61 | lai->mnc = dig[3] * 100 + dig[2]; |
| 62 | |
| 63 | lai->lac = lai48->lac; |
| 64 | } |
| 65 | |
| 66 | static void to_bcd(u_int8_t *bcd, u_int16_t val) |
| 67 | { |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 68 | bcd[2] = val % 10; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 69 | val = val / 10; |
| 70 | bcd[1] = val % 10; |
| 71 | val = val / 10; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 72 | bcd[0] = val % 10; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 73 | val = val / 10; |
| 74 | } |
| 75 | |
| 76 | static void generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc, |
| 77 | u_int16_t mnc, u_int16_t lac) |
| 78 | { |
| 79 | u_int8_t bcd[3]; |
| 80 | |
| 81 | to_bcd(bcd, mcc); |
| 82 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 83 | lai48->digits[1] = bcd[2]; |
| 84 | |
| 85 | to_bcd(bcd, mnc); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 86 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
| 87 | #if 0 |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 88 | lai48->digits[1] |= bcd[2] << 4; |
| 89 | lai48->digits[2] = bcd[0] | (bcd[1] << 4); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 90 | #else |
| 91 | lai48->digits[1] |= 0xf << 4; |
| 92 | lai48->digits[2] = bcd[1] | (bcd[2] << 4); |
| 93 | #endif |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 94 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 95 | lai48->lac = htons(lac); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | #define TMSI_LEN 4 |
| 99 | #define MID_TMSI_LEN (TMSI_LEN + 2) |
| 100 | |
| 101 | static void generate_mid_from_tmsi(u_int8_t *buf, u_int8_t *tmsi_bcd) |
| 102 | { |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 103 | buf[0] = GSM48_IE_MOBILE_ID; |
| 104 | buf[1] = MID_TMSI_LEN; |
| 105 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
| 106 | buf[3] = tmsi_bcd[0]; |
| 107 | buf[4] = tmsi_bcd[1]; |
| 108 | buf[5] = tmsi_bcd[2]; |
| 109 | buf[6] = tmsi_bcd[3]; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 112 | static struct msgb *gsm48_msgb_alloc(void) |
| 113 | { |
| 114 | return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM); |
| 115 | } |
| 116 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 117 | static int gsm0408_sendmsg(struct msgb *msg) |
| 118 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 119 | if (msg->lchan) |
| 120 | msg->trx = msg->lchan->ts->trx; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 121 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 122 | msg->l3h = msg->data; |
| 123 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 124 | return rsl_data_request(msg, 0); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 127 | static int gsm48_cc_tx_status(struct gsm_lchan *lchan) |
| 128 | { |
| 129 | struct msgb *msg = gsm48_msgb_alloc(); |
| 130 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 131 | u_int8_t *cause, *call_state; |
| 132 | |
| 133 | msg->lchan = lchan; |
| 134 | |
| 135 | gh->proto_discr = GSM48_PDISC_CC; |
| 136 | gh->msg_type = GSM48_MT_CC_STATUS; |
| 137 | |
| 138 | cause = msgb_put(msg, 3); |
| 139 | cause[0] = 2; |
| 140 | cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER; |
| 141 | cause[2] = 0x80 | 30; /* response to status inquiry */ |
| 142 | |
| 143 | call_state = msgb_put(msg, 1); |
| 144 | call_state[0] = 0xc0 | 0x00; |
| 145 | |
| 146 | return gsm0408_sendmsg(msg); |
| 147 | } |
| 148 | |
| 149 | static int gsm48_cc_rx_status_enq(struct msgb *msg) |
| 150 | { |
| 151 | return gsm48_cc_tx_status(msg->lchan); |
| 152 | } |
| 153 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 154 | static int gsm0408_rcv_cc(struct msgb *msg) |
| 155 | { |
| 156 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 157 | u_int8_t msg_type = gh->msg_type & 0xbf; |
| 158 | int rc = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 159 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 160 | switch (msg_type) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 161 | case GSM48_MT_CC_CALL_CONF: |
| 162 | /* Response to SETUP */ |
| 163 | DEBUGP(DCC, "CALL CONFIRM\n"); |
| 164 | break; |
| 165 | case GSM48_MT_CC_RELEASE_COMPL: |
| 166 | DEBUGP(DCC, "RELEASE COMPLETE\n"); |
| 167 | break; |
| 168 | case GSM48_MT_CC_ALERTING: |
| 169 | DEBUGP(DCC, "ALERTING\n"); |
| 170 | break; |
| 171 | case GSM48_MT_CC_CONNECT: |
| 172 | DEBUGP(DCC, "CONNECT\n"); |
| 173 | /* need to respond with CONNECT_ACK */ |
| 174 | break; |
| 175 | case GSM48_MT_CC_RELEASE: |
| 176 | DEBUGP(DCC, "RELEASE\n"); |
| 177 | /* need to respond with RELEASE_COMPLETE */ |
| 178 | break; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 179 | case GSM48_MT_CC_STATUS_ENQ: |
| 180 | rc = gsm48_cc_rx_status_enq(msg); |
| 181 | break; |
| 182 | case GSM48_MT_CC_DISCONNECT: |
| 183 | DEBUGP(DCC, "DISCONNECT\n"); |
| 184 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 185 | case GSM48_MT_CC_SETUP: |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 186 | DEBUGP(DCC, "SETUP\n"); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 187 | /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */ |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 188 | break; |
| 189 | case GSM48_MT_CC_EMERG_SETUP: |
| 190 | DEBUGP(DCC, "EMERGENCY SETUP\n"); |
| 191 | /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */ |
| 192 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 193 | default: |
| 194 | fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n", |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 195 | msg_type); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 196 | break; |
| 197 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 198 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 199 | return rc; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* Chapter 9.2.14 : Send LOCATION UPDATE REJECT */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 203 | int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 204 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 205 | struct msgb *msg = gsm48_msgb_alloc(); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 206 | struct gsm48_hdr *gh; |
| 207 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 208 | msg->lchan = lchan; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 209 | |
| 210 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 211 | gh->proto_discr = GSM48_PDISC_MM; |
| 212 | gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT; |
| 213 | gh->data[0] = cause; |
| 214 | |
| 215 | DEBUGP(DMM, "-> LOCATION UPDATE REJECT\n"); |
| 216 | |
| 217 | return gsm0408_sendmsg(msg); |
| 218 | } |
| 219 | |
| 220 | /* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 221 | int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int8_t *tmsi) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 222 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 223 | struct gsm_bts *bts = lchan->ts->trx->bts; |
| 224 | struct msgb *msg = gsm48_msgb_alloc(); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 225 | struct gsm48_hdr *gh; |
| 226 | struct gsm48_loc_area_id *lai; |
| 227 | u_int8_t *mid; |
| 228 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 229 | msg->lchan = lchan; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 230 | |
| 231 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 232 | gh->proto_discr = GSM48_PDISC_MM; |
| 233 | gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT; |
| 234 | |
| 235 | lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai)); |
| 236 | generate_lai(lai, bts->network->country_code, |
| 237 | bts->network->network_code, bts->location_area_code); |
| 238 | |
| 239 | mid = msgb_put(msg, MID_TMSI_LEN); |
| 240 | generate_mid_from_tmsi(mid, tmsi); |
| 241 | |
| 242 | DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n"); |
| 243 | |
| 244 | return gsm0408_sendmsg(msg); |
| 245 | } |
| 246 | |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 247 | static char bcd2char(u_int8_t bcd) |
| 248 | { |
| 249 | if (bcd < 0xa) |
| 250 | return '0' + bcd; |
| 251 | else |
| 252 | return 'A' + (bcd - 0xa); |
| 253 | } |
| 254 | |
| 255 | /* 10.5.1.4 */ |
| 256 | static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len) |
| 257 | { |
| 258 | int i; |
| 259 | u_int8_t mi_type; |
| 260 | char *str_cur = string; |
| 261 | |
| 262 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 263 | |
| 264 | switch (mi_type) { |
| 265 | case GSM_MI_TYPE_NONE: |
| 266 | break; |
| 267 | case GSM_MI_TYPE_TMSI: |
| 268 | for (i = 1; i < mi_len - 1; i++) { |
| 269 | if (str_cur + 2 >= string + str_len) |
| 270 | return str_cur - string; |
| 271 | *str_cur++ = bcd2char(mi[i] >> 4); |
| 272 | *str_cur++ = bcd2char(mi[i] & 0xf); |
| 273 | } |
| 274 | break; |
| 275 | case GSM_MI_TYPE_IMSI: |
| 276 | case GSM_MI_TYPE_IMEI: |
| 277 | case GSM_MI_TYPE_IMEISV: |
| 278 | if (mi[0] & GSM_MI_ODD) |
| 279 | *str_cur++ = bcd2char(mi[0] >> 4); |
| 280 | |
| 281 | for (i = 1; i < mi_len - 1; i++) { |
| 282 | if (str_cur + 2 >= string + str_len) |
| 283 | return str_cur - string; |
| 284 | *str_cur++ = bcd2char(mi[i] & 0xf); |
| 285 | *str_cur++ = bcd2char(mi[i] >> 4); |
| 286 | } |
| 287 | break; |
| 288 | default: |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | *str_cur++ = '\0'; |
| 293 | return str_cur - string; |
| 294 | } |
| 295 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 296 | /* Chapter 9.2.10 */ |
| 297 | static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type) |
| 298 | { |
| 299 | struct msgb *msg = gsm48_msgb_alloc(); |
| 300 | struct gsm48_hdr *gh; |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 301 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 302 | msg->lchan = lchan; |
| 303 | |
| 304 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 305 | gh->proto_discr = GSM48_PDISC_MM; |
| 306 | gh->msg_type = GSM48_MT_MM_ID_REQ; |
| 307 | gh->data[0] = id_type; |
| 308 | |
| 309 | return gsm0408_sendmsg(msg); |
| 310 | } |
| 311 | |
| 312 | #define MI_SIZE 32 |
| 313 | |
| 314 | /* Chapter 9.2.11 */ |
| 315 | static int mm_rx_id_resp(struct msgb *msg) |
| 316 | { |
| 317 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 318 | u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK; |
| 319 | char mi_string[MI_SIZE]; |
| 320 | |
| 321 | mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]); |
| 322 | DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x (MI(%s)\n", |
| 323 | mi_type, mi_string); |
| 324 | |
| 325 | /* FIXME: update subscribe <-> IMEI mapping */ |
| 326 | } |
| 327 | |
| 328 | #define MI_SIZE 32 |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 329 | /* Chapter 9.2.15 */ |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 330 | static int mm_rx_loc_upd_req(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 331 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 332 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 333 | struct gsm_bts *bts = msg->trx->bts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 334 | struct gsm48_loc_upd_req *lu; |
| 335 | struct gsm_subscriber *subscr; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 336 | u_int8_t mi_type; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 337 | char mi_string[MI_SIZE]; |
| 338 | int rc; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 339 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 340 | lu = (struct gsm48_loc_upd_req *) gh->data; |
| 341 | |
| 342 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 343 | |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 344 | mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
| 345 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 346 | DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 347 | switch (mi_type) { |
| 348 | case GSM_MI_TYPE_IMSI: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 349 | /* we always want the IMEI, too */ |
| 350 | rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMEISV); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 351 | /* look up subscriber based on IMSI */ |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 352 | subscr = subscr_get_by_imsi(lu->mi); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 353 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 354 | case GSM_MI_TYPE_TMSI: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 355 | /* we always want the IMEI, too */ |
| 356 | rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMEISV); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 357 | /* look up the subscriber based on TMSI, request IMSI if it fails */ |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 358 | subscr = subscr_get_by_tmsi(lu->mi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 359 | if (!subscr) { |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 360 | /* send IDENTITY REQUEST message to get IMSI */ |
| 361 | rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMSI); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 362 | } |
| 363 | break; |
| 364 | case GSM_MI_TYPE_IMEI: |
| 365 | case GSM_MI_TYPE_IMEISV: |
| 366 | /* no sim card... FIXME: what to do ? */ |
| 367 | fprintf(stderr, "Unimplemented mobile identity type\n"); |
| 368 | break; |
| 369 | default: |
| 370 | fprintf(stderr, "Unknown mobile identity type\n"); |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | if (!subscr) { |
| 375 | /* 0x16 is congestion */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 376 | gsm0408_loc_upd_rej(msg->lchan, 0x16); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 377 | return -EINVAL; |
| 378 | } |
| 379 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 380 | msg->lchan->subscr = subscr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 381 | subscr_update(subscr, bts); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 382 | |
| 383 | return gsm0408_loc_upd_acc(msg->lchan, subscr->tmsi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 386 | static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan) |
| 387 | { |
| 388 | struct msgb *msg = gsm48_msgb_alloc(); |
| 389 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 390 | |
| 391 | msg->lchan = lchan; |
| 392 | |
| 393 | gh->proto_discr = GSM48_PDISC_MM; |
| 394 | gh->msg_type = GSM48_MT_MM_CM_SERV_ACC; |
| 395 | |
| 396 | DEBUGP(DMM, "-> CM SERVICE ACK\n"); |
| 397 | |
| 398 | return gsm0408_sendmsg(msg); |
| 399 | } |
| 400 | |
| 401 | static int gsm48_rx_mm_serv_req(struct msgb *msg) |
| 402 | { |
| 403 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 404 | |
| 405 | DEBUGP(DMM, "CM SERVICE REQUEST\n"); |
| 406 | return gsm48_tx_mm_serv_ack(msg->lchan); |
| 407 | } |
| 408 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 409 | static int gsm0408_rcv_mm(struct msgb *msg) |
| 410 | { |
| 411 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 412 | int rc; |
| 413 | |
| 414 | switch (gh->msg_type & 0xbf) { |
| 415 | case GSM48_MT_MM_LOC_UPD_REQUEST: |
| 416 | DEBUGP(DMM, "LOCATION UPDATE REQUEST\n"); |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 417 | rc = mm_rx_loc_upd_req(msg); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 418 | break; |
| 419 | case GSM48_MT_MM_ID_RESP: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 420 | rc = mm_rx_id_resp(msg); |
| 421 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 422 | case GSM48_MT_MM_CM_SERV_REQ: |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 423 | rc = gsm48_rx_mm_serv_req(msg); |
| 424 | break; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 425 | case GSM48_MT_MM_STATUS: |
| 426 | DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n"); |
| 427 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 428 | case GSM48_MT_MM_CM_REEST_REQ: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame^] | 429 | case GSM48_MT_MM_TMSI_REALL_COMPL: |
| 430 | case GSM48_MT_MM_AUTH_RESP: |
| 431 | case GSM48_MT_MM_IMSI_DETACH_IND: |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 432 | fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n", |
| 433 | gh->msg_type); |
| 434 | break; |
| 435 | default: |
| 436 | fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n", |
| 437 | gh->msg_type); |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | return rc; |
| 442 | } |
| 443 | static int gsm0408_rcv_rr(struct msgb *msg) |
| 444 | { |
| 445 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 446 | |
| 447 | switch (gh->msg_type) { |
| 448 | case GSM48_MT_RR_CLSM_CHG: |
| 449 | DEBUGP(DRR, "CLASSMARK CHANGE\n"); |
| 450 | /* FIXME: what to do ?!? */ |
| 451 | break; |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 452 | case GSM48_MT_RR_GPRS_SUSP_REQ: |
| 453 | DEBUGP(DRR, "GRPS SUSPEND REQUEST\n"); |
| 454 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 455 | case GSM48_MT_RR_PAG_RESP: |
| 456 | default: |
| 457 | fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n", |
| 458 | gh->msg_type); |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | /* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */ |
| 466 | int gsm0408_rcvmsg(struct msgb *msg) |
| 467 | { |
| 468 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 469 | u_int8_t pdisc = gh->proto_discr & 0x0f; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 470 | int rc = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 471 | |
| 472 | switch (pdisc) { |
| 473 | case GSM48_PDISC_CC: |
| 474 | rc = gsm0408_rcv_cc(msg); |
| 475 | break; |
| 476 | case GSM48_PDISC_MM: |
| 477 | rc = gsm0408_rcv_mm(msg); |
| 478 | break; |
| 479 | case GSM48_PDISC_RR: |
| 480 | rc = gsm0408_rcv_rr(msg); |
| 481 | break; |
| 482 | case GSM48_PDISC_MM_GPRS: |
| 483 | case GSM48_PDISC_SM: |
| 484 | fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n", |
| 485 | pdisc); |
| 486 | break; |
| 487 | default: |
| 488 | fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n", |
| 489 | pdisc); |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | return rc; |
| 494 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 495 | |
| 496 | enum chreq_type { |
| 497 | CHREQ_T_EMERG_CALL, |
| 498 | CHREQ_T_CALL_REEST_TCH_F, |
| 499 | CHREQ_T_CALL_REEST_TCH_H, |
| 500 | CHREQ_T_CALL_REEST_TCH_H_DBL, |
| 501 | CHREQ_T_SDCCH, |
| 502 | CHREQ_T_TCH_F, |
| 503 | CHREQ_T_VOICE_CALL_TCH_H, |
| 504 | CHREQ_T_DATA_CALL_TCH_H, |
| 505 | CHREQ_T_LOCATION_UPD, |
| 506 | CHREQ_T_PAG_R_ANY, |
| 507 | CHREQ_T_PAG_R_TCH_F, |
| 508 | CHREQ_T_PAG_R_TCH_FH, |
| 509 | }; |
| 510 | |
| 511 | /* Section 9.1.8 / Table 9.9 */ |
| 512 | struct chreq { |
| 513 | u_int8_t val; |
| 514 | u_int8_t mask; |
| 515 | enum chreq_type type; |
| 516 | }; |
| 517 | |
| 518 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */ |
| 519 | static const struct chreq chreq_type_neci1[] = { |
| 520 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 521 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F }, |
| 522 | { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H }, |
| 523 | { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL }, |
| 524 | { 0xe0, 0xe0, CHREQ_T_SDCCH }, |
| 525 | { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H }, |
| 526 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 527 | { 0x00, 0xf0, CHREQ_T_LOCATION_UPD }, |
| 528 | { 0x10, 0xf0, CHREQ_T_SDCCH }, |
| 529 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 530 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 531 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 532 | }; |
| 533 | |
| 534 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */ |
| 535 | static const struct chreq chreq_type_neci0[] = { |
| 536 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 537 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H }, |
| 538 | { 0xe0, 0xe0, CHREQ_T_TCH_F }, |
| 539 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 540 | { 0x00, 0xe0, CHREQ_T_LOCATION_UPD }, |
| 541 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 542 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 543 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 544 | }; |
| 545 | |
| 546 | static const enum gsm_chan_t ctype_by_chreq[] = { |
| 547 | [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F, |
| 548 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F, |
| 549 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H, |
| 550 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H, |
| 551 | [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH, |
| 552 | [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F, |
| 553 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 554 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 555 | [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH, |
| 556 | [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH, |
| 557 | [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F, |
| 558 | [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F, |
| 559 | }; |
| 560 | |
| 561 | enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 562 | { |
| 563 | int i; |
| 564 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 565 | |
| 566 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 567 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 568 | if ((ra & chr->mask) == chr->val) |
| 569 | return ctype_by_chreq[chr->type]; |
| 570 | } |
| 571 | fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra); |
| 572 | return GSM_LCHAN_SDCCH; |
| 573 | } |