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 | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 31 | #include <openbsc/db.h> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 32 | #include <openbsc/msgb.h> |
| 33 | #include <openbsc/debug.h> |
| 34 | #include <openbsc/gsm_data.h> |
| 35 | #include <openbsc/gsm_subscriber.h> |
| 36 | #include <openbsc/gsm_04_08.h> |
| 37 | #include <openbsc/abis_rsl.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 38 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 39 | #define GSM48_ALLOC_SIZE 1024 |
| 40 | #define GSM48_ALLOC_HEADROOM 128 |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 41 | |
| 42 | struct gsm_lai { |
| 43 | u_int16_t mcc; |
| 44 | u_int16_t mnc; |
| 45 | u_int16_t lac; |
| 46 | }; |
| 47 | |
| 48 | static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48) |
| 49 | { |
| 50 | u_int8_t dig[4]; |
| 51 | |
| 52 | /* MCC */ |
| 53 | dig[1] = lai48->digits[0] & 0x0f; |
| 54 | dig[2] = lai48->digits[0] >> 4; |
| 55 | dig[3] = lai48->digits[1] & 0x0f; |
| 56 | lai->mcc = dig[3] * 100 + dig[2]; |
| 57 | |
| 58 | /* MNC */ |
| 59 | dig[1] = lai48->digits[1] >> 4; |
| 60 | dig[2] = lai48->digits[2] & 0x0f; |
| 61 | dig[3] = lai48->digits[2] >> 4; |
| 62 | lai->mnc = dig[3] * 100 + dig[2]; |
| 63 | |
| 64 | lai->lac = lai48->lac; |
| 65 | } |
| 66 | |
| 67 | static void to_bcd(u_int8_t *bcd, u_int16_t val) |
| 68 | { |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 69 | bcd[2] = val % 10; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 70 | val = val / 10; |
| 71 | bcd[1] = val % 10; |
| 72 | val = val / 10; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 73 | bcd[0] = val % 10; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 74 | val = val / 10; |
| 75 | } |
| 76 | |
| 77 | static void generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc, |
| 78 | u_int16_t mnc, u_int16_t lac) |
| 79 | { |
| 80 | u_int8_t bcd[3]; |
| 81 | |
| 82 | to_bcd(bcd, mcc); |
| 83 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 84 | lai48->digits[1] = bcd[2]; |
| 85 | |
| 86 | to_bcd(bcd, mnc); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 87 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
| 88 | #if 0 |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 89 | lai48->digits[1] |= bcd[2] << 4; |
| 90 | lai48->digits[2] = bcd[0] | (bcd[1] << 4); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 91 | #else |
| 92 | lai48->digits[1] |= 0xf << 4; |
| 93 | lai48->digits[2] = bcd[1] | (bcd[2] << 4); |
| 94 | #endif |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 95 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 96 | lai48->lac = htons(lac); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | #define TMSI_LEN 4 |
| 100 | #define MID_TMSI_LEN (TMSI_LEN + 2) |
| 101 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 102 | static void generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 103 | { |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 104 | buf[0] = GSM48_IE_MOBILE_ID; |
Harald Welte | 1a41218 | 2008-12-27 22:13:43 +0000 | [diff] [blame^] | 105 | buf[1] = TMSI_LEN; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 106 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 107 | *((u_int32_t *) &buf[3]) = htonl(tmsi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 110 | static struct msgb *gsm48_msgb_alloc(void) |
| 111 | { |
| 112 | return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM); |
| 113 | } |
| 114 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 115 | static int gsm0408_sendmsg(struct msgb *msg) |
| 116 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 117 | if (msg->lchan) |
| 118 | msg->trx = msg->lchan->ts->trx; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 119 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 120 | msg->l3h = msg->data; |
| 121 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 122 | return rsl_data_request(msg, 0); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 125 | |
| 126 | /* Chapter 9.2.14 : Send LOCATION UPDATE REJECT */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 127 | 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] | 128 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 129 | struct msgb *msg = gsm48_msgb_alloc(); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 130 | struct gsm48_hdr *gh; |
| 131 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 132 | msg->lchan = lchan; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 133 | |
| 134 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 135 | gh->proto_discr = GSM48_PDISC_MM; |
Harald Welte | 10b487b | 2008-12-27 19:53:37 +0000 | [diff] [blame] | 136 | gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 137 | gh->data[0] = cause; |
| 138 | |
| 139 | DEBUGP(DMM, "-> LOCATION UPDATE REJECT\n"); |
| 140 | |
| 141 | return gsm0408_sendmsg(msg); |
| 142 | } |
| 143 | |
| 144 | /* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */ |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 145 | int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 146 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 147 | struct gsm_bts *bts = lchan->ts->trx->bts; |
| 148 | struct msgb *msg = gsm48_msgb_alloc(); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 149 | struct gsm48_hdr *gh; |
| 150 | struct gsm48_loc_area_id *lai; |
| 151 | u_int8_t *mid; |
| 152 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 153 | msg->lchan = lchan; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 154 | |
| 155 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 156 | gh->proto_discr = GSM48_PDISC_MM; |
| 157 | gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT; |
| 158 | |
| 159 | lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai)); |
| 160 | generate_lai(lai, bts->network->country_code, |
| 161 | bts->network->network_code, bts->location_area_code); |
| 162 | |
| 163 | mid = msgb_put(msg, MID_TMSI_LEN); |
| 164 | generate_mid_from_tmsi(mid, tmsi); |
| 165 | |
| 166 | DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n"); |
| 167 | |
| 168 | return gsm0408_sendmsg(msg); |
| 169 | } |
| 170 | |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 171 | static char bcd2char(u_int8_t bcd) |
| 172 | { |
| 173 | if (bcd < 0xa) |
| 174 | return '0' + bcd; |
| 175 | else |
| 176 | return 'A' + (bcd - 0xa); |
| 177 | } |
| 178 | |
| 179 | /* 10.5.1.4 */ |
| 180 | static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len) |
| 181 | { |
| 182 | int i; |
| 183 | u_int8_t mi_type; |
| 184 | char *str_cur = string; |
| 185 | |
| 186 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 187 | |
| 188 | switch (mi_type) { |
| 189 | case GSM_MI_TYPE_NONE: |
| 190 | break; |
| 191 | case GSM_MI_TYPE_TMSI: |
| 192 | for (i = 1; i < mi_len - 1; i++) { |
| 193 | if (str_cur + 2 >= string + str_len) |
| 194 | return str_cur - string; |
| 195 | *str_cur++ = bcd2char(mi[i] >> 4); |
| 196 | *str_cur++ = bcd2char(mi[i] & 0xf); |
| 197 | } |
| 198 | break; |
| 199 | case GSM_MI_TYPE_IMSI: |
| 200 | case GSM_MI_TYPE_IMEI: |
| 201 | case GSM_MI_TYPE_IMEISV: |
| 202 | if (mi[0] & GSM_MI_ODD) |
| 203 | *str_cur++ = bcd2char(mi[0] >> 4); |
| 204 | |
| 205 | for (i = 1; i < mi_len - 1; i++) { |
| 206 | if (str_cur + 2 >= string + str_len) |
| 207 | return str_cur - string; |
| 208 | *str_cur++ = bcd2char(mi[i] & 0xf); |
| 209 | *str_cur++ = bcd2char(mi[i] >> 4); |
| 210 | } |
| 211 | break; |
| 212 | default: |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | *str_cur++ = '\0'; |
| 217 | return str_cur - string; |
| 218 | } |
| 219 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 220 | /* Chapter 9.2.10 */ |
| 221 | static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type) |
| 222 | { |
| 223 | struct msgb *msg = gsm48_msgb_alloc(); |
| 224 | struct gsm48_hdr *gh; |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 225 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 226 | msg->lchan = lchan; |
| 227 | |
| 228 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 229 | gh->proto_discr = GSM48_PDISC_MM; |
| 230 | gh->msg_type = GSM48_MT_MM_ID_REQ; |
| 231 | gh->data[0] = id_type; |
| 232 | |
| 233 | return gsm0408_sendmsg(msg); |
| 234 | } |
| 235 | |
| 236 | #define MI_SIZE 32 |
| 237 | |
| 238 | /* Chapter 9.2.11 */ |
| 239 | static int mm_rx_id_resp(struct msgb *msg) |
| 240 | { |
| 241 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 242 | struct gsm_lchan *lchan = msg->lchan; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 243 | u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK; |
| 244 | char mi_string[MI_SIZE]; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 245 | u_int32_t tmsi; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 246 | |
| 247 | mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]); |
Harald Welte | 6125306 | 2008-12-27 11:25:50 +0000 | [diff] [blame] | 248 | DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n", |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 249 | mi_type, mi_string); |
| 250 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 251 | switch (mi_type) { |
| 252 | case GSM_MI_TYPE_IMSI: |
| 253 | if (!lchan->subscr) |
| 254 | lchan->subscr = db_create_subscriber(mi_string); |
| 255 | if (lchan->subscr && lchan->subscr->authorized) { |
| 256 | /* FIXME: check if we've recently received UPDATE REQUEST */ |
| 257 | db_subscriber_alloc_tmsi(lchan->subscr); |
| 258 | tmsi = strtoul(lchan->subscr->tmsi, NULL, 16); |
| 259 | return gsm0408_loc_upd_acc(msg->lchan, tmsi); |
| 260 | } |
| 261 | break; |
| 262 | case GSM_MI_TYPE_IMEI: |
| 263 | /* update subscribe <-> IMEI mapping */ |
| 264 | if (lchan->subscr) |
| 265 | db_subscriber_assoc_imei(lchan->subscr, mi_string); |
| 266 | break; |
| 267 | } |
| 268 | return 0; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | #define MI_SIZE 32 |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 272 | /* Chapter 9.2.15 */ |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 273 | static int mm_rx_loc_upd_req(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 274 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 275 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 276 | struct gsm_bts *bts = msg->trx->bts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 277 | struct gsm48_loc_upd_req *lu; |
| 278 | struct gsm_subscriber *subscr; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 279 | u_int8_t mi_type; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 280 | u_int32_t tmsi; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 281 | char mi_string[MI_SIZE]; |
| 282 | int rc; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 283 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 284 | lu = (struct gsm48_loc_upd_req *) gh->data; |
| 285 | |
| 286 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 287 | |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 288 | mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
| 289 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 290 | 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] | 291 | switch (mi_type) { |
| 292 | case GSM_MI_TYPE_IMSI: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 293 | /* we always want the IMEI, too */ |
| 294 | rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMEISV); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 295 | /* look up subscriber based on IMSI */ |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 296 | subscr = db_create_subscriber(mi_string); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 297 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 298 | case GSM_MI_TYPE_TMSI: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 299 | /* we always want the IMEI, too */ |
| 300 | rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMEISV); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 301 | /* look up the subscriber based on TMSI, request IMSI if it fails */ |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 302 | subscr = subscr_get_by_tmsi(lu->mi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 303 | if (!subscr) { |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 304 | /* send IDENTITY REQUEST message to get IMSI */ |
| 305 | rc = mm_tx_identity_req(msg->lchan, GSM_MI_TYPE_IMSI); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 306 | } |
| 307 | break; |
| 308 | case GSM_MI_TYPE_IMEI: |
| 309 | case GSM_MI_TYPE_IMEISV: |
| 310 | /* no sim card... FIXME: what to do ? */ |
| 311 | fprintf(stderr, "Unimplemented mobile identity type\n"); |
| 312 | break; |
| 313 | default: |
| 314 | fprintf(stderr, "Unknown mobile identity type\n"); |
| 315 | break; |
| 316 | } |
| 317 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 318 | if (!subscr || !subscr->authorized) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 319 | /* 0x16 is congestion */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 320 | gsm0408_loc_upd_rej(msg->lchan, 0x16); |
Harald Welte | 98243f8 | 2008-12-27 19:46:41 +0000 | [diff] [blame] | 321 | rsl_chan_release(msg->lchan); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 322 | return -EINVAL; |
| 323 | } |
| 324 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 325 | db_subscriber_alloc_tmsi(subscr); |
| 326 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 327 | msg->lchan->subscr = subscr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 328 | subscr_update(subscr, bts); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 329 | tmsi = strtoul(subscr->tmsi, NULL, 16); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 330 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 331 | return gsm0408_loc_upd_acc(msg->lchan, tmsi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 334 | static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan) |
| 335 | { |
| 336 | struct msgb *msg = gsm48_msgb_alloc(); |
| 337 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 338 | |
| 339 | msg->lchan = lchan; |
| 340 | |
| 341 | gh->proto_discr = GSM48_PDISC_MM; |
| 342 | gh->msg_type = GSM48_MT_MM_CM_SERV_ACC; |
| 343 | |
| 344 | DEBUGP(DMM, "-> CM SERVICE ACK\n"); |
| 345 | |
| 346 | return gsm0408_sendmsg(msg); |
| 347 | } |
| 348 | |
| 349 | static int gsm48_rx_mm_serv_req(struct msgb *msg) |
| 350 | { |
| 351 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 352 | u_int8_t serv_type = gh->data[0] & 0x0f; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 353 | |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 354 | DEBUGP(DMM, "CM SERVICE REQUEST serv_type=0x%02x\n", serv_type); |
| 355 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 356 | return gsm48_tx_mm_serv_ack(msg->lchan); |
| 357 | } |
| 358 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 359 | static int gsm0408_rcv_mm(struct msgb *msg) |
| 360 | { |
| 361 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 362 | int rc; |
| 363 | |
| 364 | switch (gh->msg_type & 0xbf) { |
| 365 | case GSM48_MT_MM_LOC_UPD_REQUEST: |
| 366 | DEBUGP(DMM, "LOCATION UPDATE REQUEST\n"); |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 367 | rc = mm_rx_loc_upd_req(msg); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 368 | break; |
| 369 | case GSM48_MT_MM_ID_RESP: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 370 | rc = mm_rx_id_resp(msg); |
| 371 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 372 | case GSM48_MT_MM_CM_SERV_REQ: |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 373 | rc = gsm48_rx_mm_serv_req(msg); |
| 374 | break; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 375 | case GSM48_MT_MM_STATUS: |
| 376 | DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n"); |
| 377 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 378 | case GSM48_MT_MM_CM_REEST_REQ: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 379 | case GSM48_MT_MM_TMSI_REALL_COMPL: |
| 380 | case GSM48_MT_MM_AUTH_RESP: |
| 381 | case GSM48_MT_MM_IMSI_DETACH_IND: |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 382 | fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n", |
| 383 | gh->msg_type); |
| 384 | break; |
| 385 | default: |
| 386 | fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n", |
| 387 | gh->msg_type); |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | return rc; |
| 392 | } |
| 393 | static int gsm0408_rcv_rr(struct msgb *msg) |
| 394 | { |
| 395 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 396 | |
| 397 | switch (gh->msg_type) { |
| 398 | case GSM48_MT_RR_CLSM_CHG: |
| 399 | DEBUGP(DRR, "CLASSMARK CHANGE\n"); |
| 400 | /* FIXME: what to do ?!? */ |
| 401 | break; |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 402 | case GSM48_MT_RR_GPRS_SUSP_REQ: |
| 403 | DEBUGP(DRR, "GRPS SUSPEND REQUEST\n"); |
| 404 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 405 | case GSM48_MT_RR_PAG_RESP: |
| 406 | default: |
| 407 | fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n", |
| 408 | gh->msg_type); |
| 409 | break; |
| 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 415 | /* Call Control */ |
| 416 | |
| 417 | /* Send a 04.08 call control message, add transaction ID and TI flag */ |
| 418 | static int gsm48_cc_sendmsg(struct msgb *msg) |
| 419 | { |
| 420 | struct gsm48_hdr *gh = msg->data; |
| 421 | struct gsm_call *call = &msg->lchan->call; |
| 422 | |
| 423 | gh->proto_discr |= msg->lchan->call.transaction_id; |
| 424 | |
| 425 | /* GSM 04.07 Section 11.2.3.1.3 */ |
| 426 | switch (call->type) { |
| 427 | case GSM_CT_MO: |
| 428 | gh->proto_discr |= 0x80; |
| 429 | break; |
| 430 | case GSM_CT_MT: |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | return gsm0408_sendmsg(msg); |
| 435 | } |
| 436 | |
| 437 | |
| 438 | static int gsm48_cc_tx_status(struct gsm_lchan *lchan) |
| 439 | { |
| 440 | struct msgb *msg = gsm48_msgb_alloc(); |
| 441 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 442 | u_int8_t *cause, *call_state; |
| 443 | |
| 444 | msg->lchan = lchan; |
| 445 | |
| 446 | gh->proto_discr = GSM48_PDISC_CC; |
| 447 | gh->msg_type = GSM48_MT_CC_STATUS; |
| 448 | |
| 449 | cause = msgb_put(msg, 3); |
| 450 | cause[0] = 2; |
| 451 | cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER; |
| 452 | cause[2] = 0x80 | 30; /* response to status inquiry */ |
| 453 | |
| 454 | call_state = msgb_put(msg, 1); |
| 455 | call_state[0] = 0xc0 | 0x00; |
| 456 | |
| 457 | return gsm48_cc_sendmsg(msg); |
| 458 | } |
| 459 | |
| 460 | static int gsm48_cc_tx_simple(struct gsm_lchan *lchan, u_int8_t msg_type) |
| 461 | { |
| 462 | struct msgb *msg = gsm48_msgb_alloc(); |
| 463 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 464 | |
| 465 | msg->lchan = lchan; |
| 466 | |
| 467 | gh->proto_discr = GSM48_PDISC_CC; |
| 468 | gh->msg_type = msg_type; |
| 469 | |
| 470 | return gsm48_cc_sendmsg(msg); |
| 471 | } |
| 472 | |
| 473 | static int gsm48_cc_rx_status_enq(struct msgb *msg) |
| 474 | { |
| 475 | return gsm48_cc_tx_status(msg->lchan); |
| 476 | } |
| 477 | |
| 478 | static int gsm48_cc_rx_setup(struct msgb *msg) |
| 479 | { |
| 480 | return gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CALL_CONF); |
| 481 | } |
| 482 | |
| 483 | static int gsm0408_rcv_cc(struct msgb *msg) |
| 484 | { |
| 485 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 486 | u_int8_t msg_type = gh->msg_type & 0xbf; |
| 487 | struct gsm_call *call = &msg->lchan->call; |
| 488 | int rc = 0; |
| 489 | |
| 490 | switch (msg_type) { |
| 491 | case GSM48_MT_CC_CALL_CONF: |
| 492 | /* Response to SETUP */ |
| 493 | DEBUGP(DCC, "CALL CONFIRM\n"); |
| 494 | break; |
| 495 | case GSM48_MT_CC_RELEASE_COMPL: |
| 496 | /* Answer from MS to RELEASE */ |
| 497 | DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n"); |
| 498 | call->state = GSM_CSTATE_NULL; |
| 499 | break; |
| 500 | case GSM48_MT_CC_ALERTING: |
| 501 | DEBUGP(DCC, "ALERTING\n"); |
| 502 | break; |
| 503 | case GSM48_MT_CC_CONNECT: |
| 504 | DEBUGP(DCC, "CONNECT\n"); |
| 505 | /* MT: need to respond with CONNECT_ACK */ |
| 506 | rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CONNECT_ACK); |
| 507 | break; |
| 508 | case GSM48_MT_CC_CONNECT_ACK: |
| 509 | /* MO: Answer to CONNECT */ |
| 510 | call->state = GSM_CSTATE_ACTIVE; |
| 511 | DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n"); |
| 512 | break; |
| 513 | case GSM48_MT_CC_RELEASE: |
| 514 | DEBUGP(DCC, "RELEASE\n"); |
| 515 | /* need to respond with RELEASE_COMPLETE */ |
| 516 | break; |
| 517 | case GSM48_MT_CC_STATUS_ENQ: |
| 518 | rc = gsm48_cc_rx_status_enq(msg); |
| 519 | break; |
| 520 | case GSM48_MT_CC_DISCONNECT: |
| 521 | /* Section 5.4.3.2 */ |
| 522 | DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n"); |
| 523 | call->state = GSM_CSTATE_RELEASE_REQ; |
| 524 | /* FIXME: clear the network connection */ |
| 525 | rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_RELEASE); |
| 526 | break; |
| 527 | case GSM48_MT_CC_SETUP: |
| 528 | call->type = GSM_CT_MO; |
| 529 | call->state = GSM_CSTATE_INITIATED; |
| 530 | call->transaction_id = gh->proto_discr & 0xf0; |
| 531 | DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id); |
| 532 | rc = gsm48_cc_tx_simple(msg->lchan, GSM48_MT_CC_CONNECT); |
| 533 | /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */ |
| 534 | break; |
| 535 | case GSM48_MT_CC_EMERG_SETUP: |
| 536 | DEBUGP(DCC, "EMERGENCY SETUP\n"); |
| 537 | /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */ |
| 538 | break; |
| 539 | default: |
| 540 | fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n", |
| 541 | msg_type); |
| 542 | break; |
| 543 | } |
| 544 | |
| 545 | return rc; |
| 546 | } |
| 547 | |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 548 | static int gsm0408_rcv_sms(struct msgb *msg) |
| 549 | { |
| 550 | DEBUGP(DSMS, "SMS Message\n"); |
| 551 | return 0; |
| 552 | } |
| 553 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 554 | /* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */ |
| 555 | int gsm0408_rcvmsg(struct msgb *msg) |
| 556 | { |
| 557 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 558 | u_int8_t pdisc = gh->proto_discr & 0x0f; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 559 | int rc = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 560 | |
| 561 | switch (pdisc) { |
| 562 | case GSM48_PDISC_CC: |
| 563 | rc = gsm0408_rcv_cc(msg); |
| 564 | break; |
| 565 | case GSM48_PDISC_MM: |
| 566 | rc = gsm0408_rcv_mm(msg); |
| 567 | break; |
| 568 | case GSM48_PDISC_RR: |
| 569 | rc = gsm0408_rcv_rr(msg); |
| 570 | break; |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 571 | case GSM48_PDISC_SMS: |
| 572 | rc = gsm0408_rcv_sms(msg); |
| 573 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 574 | case GSM48_PDISC_MM_GPRS: |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 575 | case GSM48_PDISC_SM_GPRS: |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 576 | fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n", |
| 577 | pdisc); |
| 578 | break; |
| 579 | default: |
| 580 | fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n", |
| 581 | pdisc); |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | return rc; |
| 586 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 587 | |
| 588 | enum chreq_type { |
| 589 | CHREQ_T_EMERG_CALL, |
| 590 | CHREQ_T_CALL_REEST_TCH_F, |
| 591 | CHREQ_T_CALL_REEST_TCH_H, |
| 592 | CHREQ_T_CALL_REEST_TCH_H_DBL, |
| 593 | CHREQ_T_SDCCH, |
| 594 | CHREQ_T_TCH_F, |
| 595 | CHREQ_T_VOICE_CALL_TCH_H, |
| 596 | CHREQ_T_DATA_CALL_TCH_H, |
| 597 | CHREQ_T_LOCATION_UPD, |
| 598 | CHREQ_T_PAG_R_ANY, |
| 599 | CHREQ_T_PAG_R_TCH_F, |
| 600 | CHREQ_T_PAG_R_TCH_FH, |
| 601 | }; |
| 602 | |
| 603 | /* Section 9.1.8 / Table 9.9 */ |
| 604 | struct chreq { |
| 605 | u_int8_t val; |
| 606 | u_int8_t mask; |
| 607 | enum chreq_type type; |
| 608 | }; |
| 609 | |
| 610 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */ |
| 611 | static const struct chreq chreq_type_neci1[] = { |
| 612 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 613 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F }, |
| 614 | { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H }, |
| 615 | { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL }, |
| 616 | { 0xe0, 0xe0, CHREQ_T_SDCCH }, |
| 617 | { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H }, |
| 618 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 619 | { 0x00, 0xf0, CHREQ_T_LOCATION_UPD }, |
| 620 | { 0x10, 0xf0, CHREQ_T_SDCCH }, |
| 621 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 622 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 623 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 624 | }; |
| 625 | |
| 626 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */ |
| 627 | static const struct chreq chreq_type_neci0[] = { |
| 628 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 629 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H }, |
| 630 | { 0xe0, 0xe0, CHREQ_T_TCH_F }, |
| 631 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 632 | { 0x00, 0xe0, CHREQ_T_LOCATION_UPD }, |
| 633 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 634 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 635 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 636 | }; |
| 637 | |
| 638 | static const enum gsm_chan_t ctype_by_chreq[] = { |
| 639 | [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F, |
| 640 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F, |
| 641 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H, |
| 642 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H, |
| 643 | [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH, |
| 644 | [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F, |
| 645 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 646 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 647 | [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH, |
| 648 | [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH, |
| 649 | [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F, |
| 650 | [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F, |
| 651 | }; |
| 652 | |
| 653 | enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 654 | { |
| 655 | int i; |
| 656 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 657 | |
| 658 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 659 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 660 | if ((ra & chr->mask) == chr->val) |
| 661 | return ctype_by_chreq[chr->type]; |
| 662 | } |
| 663 | fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra); |
| 664 | return GSM_LCHAN_SDCCH; |
| 665 | } |