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