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> |
Holger Freyther | ba4d28a | 2008-12-29 06:23:44 +0000 | [diff] [blame] | 5 | * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 6 | * |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <errno.h> |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 30 | #include <time.h> |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 31 | #include <netinet/in.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 32 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 33 | #include <openbsc/db.h> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 34 | #include <openbsc/msgb.h> |
| 35 | #include <openbsc/debug.h> |
| 36 | #include <openbsc/gsm_data.h> |
| 37 | #include <openbsc/gsm_subscriber.h> |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 38 | #include <openbsc/gsm_04_11.h> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 39 | #include <openbsc/gsm_04_08.h> |
| 40 | #include <openbsc/abis_rsl.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 41 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 42 | #define GSM48_ALLOC_SIZE 1024 |
| 43 | #define GSM48_ALLOC_HEADROOM 128 |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 44 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 45 | static int gsm48_tx_simple(struct gsm_lchan *lchan, |
| 46 | u_int8_t pdisc, u_int8_t msg_type); |
Holger Freyther | b7193e4 | 2008-12-29 17:44:08 +0000 | [diff] [blame] | 47 | static void schedule_reject(struct gsm_lchan *lchan); |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 48 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 49 | struct gsm_lai { |
| 50 | u_int16_t mcc; |
| 51 | u_int16_t mnc; |
| 52 | u_int16_t lac; |
| 53 | }; |
| 54 | |
Holger Freyther | 89824fc | 2008-12-30 16:18:18 +0000 | [diff] [blame] | 55 | static int authorize_everonye = 0; |
| 56 | void gsm0408_allow_everyone(int everyone) |
| 57 | { |
| 58 | printf("Allowing everyone?\n"); |
| 59 | authorize_everonye = everyone; |
| 60 | } |
| 61 | |
Holger Freyther | e97f7fb | 2008-12-31 18:52:11 +0000 | [diff] [blame] | 62 | static int reject_cause = 0; |
| 63 | void gsm0408_set_reject_cause(int cause) |
| 64 | { |
| 65 | reject_cause = cause; |
| 66 | } |
| 67 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 68 | static int authorize_subscriber(struct gsm_loc_updating_operation *loc, |
| 69 | struct gsm_subscriber *subscriber) |
Holger Freyther | 89824fc | 2008-12-30 16:18:18 +0000 | [diff] [blame] | 70 | { |
| 71 | if (!subscriber) |
| 72 | return 0; |
| 73 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 74 | /* |
| 75 | * Do not send accept yet as more information should arrive. Some |
| 76 | * phones will not send us the information and we will have to check |
| 77 | * what we want to do with that. |
| 78 | */ |
| 79 | if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei)) |
| 80 | return 0; |
| 81 | |
Holger Freyther | 89824fc | 2008-12-30 16:18:18 +0000 | [diff] [blame] | 82 | if (authorize_everonye) |
| 83 | return 1; |
| 84 | |
| 85 | return subscriber->authorized; |
| 86 | } |
Holger Freyther | 07cc8d8 | 2008-12-29 06:23:46 +0000 | [diff] [blame] | 87 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 88 | static void release_loc_updating_req(struct gsm_lchan *lchan) |
| 89 | { |
Harald Welte | 179f064 | 2008-12-31 23:59:18 +0000 | [diff] [blame] | 90 | if (!lchan->loc_operation) |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 91 | return; |
| 92 | |
| 93 | del_timer(&lchan->loc_operation->updating_timer); |
| 94 | free(lchan->loc_operation); |
| 95 | lchan->loc_operation = 0; |
Holger Freyther | 3eaa792 | 2009-01-01 02:59:03 +0000 | [diff] [blame] | 96 | put_lchan(lchan); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void allocate_loc_updating_req(struct gsm_lchan *lchan) |
| 100 | { |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 101 | use_lchan(lchan); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 102 | release_loc_updating_req(lchan); |
| 103 | |
| 104 | lchan->loc_operation = (struct gsm_loc_updating_operation *) |
| 105 | malloc(sizeof(*lchan->loc_operation)); |
| 106 | memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation)); |
| 107 | } |
Holger Freyther | 07cc8d8 | 2008-12-29 06:23:46 +0000 | [diff] [blame] | 108 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 109 | static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48) |
| 110 | { |
| 111 | u_int8_t dig[4]; |
| 112 | |
| 113 | /* MCC */ |
| 114 | dig[1] = lai48->digits[0] & 0x0f; |
| 115 | dig[2] = lai48->digits[0] >> 4; |
| 116 | dig[3] = lai48->digits[1] & 0x0f; |
| 117 | lai->mcc = dig[3] * 100 + dig[2]; |
| 118 | |
| 119 | /* MNC */ |
| 120 | dig[1] = lai48->digits[1] >> 4; |
| 121 | dig[2] = lai48->digits[2] & 0x0f; |
| 122 | dig[3] = lai48->digits[2] >> 4; |
| 123 | lai->mnc = dig[3] * 100 + dig[2]; |
| 124 | |
| 125 | lai->lac = lai48->lac; |
| 126 | } |
| 127 | |
| 128 | static void to_bcd(u_int8_t *bcd, u_int16_t val) |
| 129 | { |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 130 | bcd[2] = val % 10; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 131 | val = val / 10; |
| 132 | bcd[1] = val % 10; |
| 133 | val = val / 10; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 134 | bcd[0] = val % 10; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 135 | val = val / 10; |
| 136 | } |
| 137 | |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 138 | static u_int8_t to_bcd8(unsigned int val) |
| 139 | { |
| 140 | u_int8_t bcd; |
| 141 | |
| 142 | bcd = (val % 10) & 0x0f; |
| 143 | val = val / 10; |
| 144 | bcd |= (val % 10) << 4; |
| 145 | |
| 146 | return bcd; |
| 147 | } |
| 148 | |
Holger Freyther | 1774661 | 2008-12-28 16:32:44 +0000 | [diff] [blame] | 149 | void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc, |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 150 | u_int16_t mnc, u_int16_t lac) |
| 151 | { |
| 152 | u_int8_t bcd[3]; |
| 153 | |
| 154 | to_bcd(bcd, mcc); |
| 155 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 156 | lai48->digits[1] = bcd[2]; |
| 157 | |
| 158 | to_bcd(bcd, mnc); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 159 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
| 160 | #if 0 |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 161 | lai48->digits[1] |= bcd[2] << 4; |
| 162 | lai48->digits[2] = bcd[0] | (bcd[1] << 4); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 163 | #else |
| 164 | lai48->digits[1] |= 0xf << 4; |
| 165 | lai48->digits[2] = bcd[1] | (bcd[2] << 4); |
| 166 | #endif |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 167 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 168 | lai48->lac = htons(lac); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 171 | #define TMSI_LEN 5 |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 172 | #define MID_TMSI_LEN (TMSI_LEN + 2) |
| 173 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 174 | int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 175 | { |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 176 | u_int32_t *tptr = (u_int32_t *) &buf[3]; |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 177 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 178 | buf[0] = GSM48_IE_MOBILE_ID; |
Harald Welte | 1a41218 | 2008-12-27 22:13:43 +0000 | [diff] [blame] | 179 | buf[1] = TMSI_LEN; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 180 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 181 | *tptr = htonl(tmsi); |
| 182 | |
| 183 | return 7; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Holger Freyther | 819dd20 | 2009-01-04 03:52:50 +0000 | [diff] [blame] | 186 | struct msgb *gsm48_msgb_alloc(void) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 187 | { |
| 188 | return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM); |
| 189 | } |
| 190 | |
Holger Freyther | 3e2c323 | 2009-01-04 03:55:31 +0000 | [diff] [blame^] | 191 | int gsm48_sendmsg(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 192 | { |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 193 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data; |
| 194 | |
| 195 | if (msg->lchan) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 196 | msg->trx = msg->lchan->ts->trx; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 197 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 198 | if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) { |
| 199 | /* Send a 04.08 call control message, add transaction |
| 200 | * ID and TI flag */ |
| 201 | gh->proto_discr |= msg->lchan->call.transaction_id; |
| 202 | |
| 203 | /* GSM 04.07 Section 11.2.3.1.3 */ |
| 204 | switch (msg->lchan->call.type) { |
| 205 | case GSM_CT_MO: |
| 206 | gh->proto_discr |= 0x80; |
| 207 | break; |
| 208 | case GSM_CT_MT: |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 214 | msg->l3h = msg->data; |
| 215 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 216 | return rsl_data_request(msg, 0); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 219 | |
Holger Freyther | 429e776 | 2008-12-30 13:28:30 +0000 | [diff] [blame] | 220 | /* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 221 | 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] | 222 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 223 | struct msgb *msg = gsm48_msgb_alloc(); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 224 | struct gsm48_hdr *gh; |
| 225 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 226 | msg->lchan = lchan; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 227 | |
| 228 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 229 | gh->proto_discr = GSM48_PDISC_MM; |
Harald Welte | 10b487b | 2008-12-27 19:53:37 +0000 | [diff] [blame] | 230 | gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 231 | gh->data[0] = cause; |
| 232 | |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 233 | DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr); |
| 234 | |
| 235 | //gsm0411_send_sms(lchan, NULL); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 236 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 237 | return gsm48_sendmsg(msg); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */ |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 241 | 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] | 242 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 243 | struct gsm_bts *bts = lchan->ts->trx->bts; |
| 244 | struct msgb *msg = gsm48_msgb_alloc(); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 245 | struct gsm48_hdr *gh; |
| 246 | struct gsm48_loc_area_id *lai; |
| 247 | u_int8_t *mid; |
Holger Freyther | 07cc8d8 | 2008-12-29 06:23:46 +0000 | [diff] [blame] | 248 | int ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 249 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 250 | msg->lchan = lchan; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 251 | |
| 252 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 253 | gh->proto_discr = GSM48_PDISC_MM; |
| 254 | gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT; |
| 255 | |
| 256 | lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai)); |
Holger Freyther | 1774661 | 2008-12-28 16:32:44 +0000 | [diff] [blame] | 257 | gsm0408_generate_lai(lai, bts->network->country_code, |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 258 | bts->network->network_code, bts->location_area_code); |
| 259 | |
| 260 | mid = msgb_put(msg, MID_TMSI_LEN); |
| 261 | generate_mid_from_tmsi(mid, tmsi); |
| 262 | |
| 263 | DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n"); |
| 264 | |
Holger Freyther | 07cc8d8 | 2008-12-29 06:23:46 +0000 | [diff] [blame] | 265 | /* inform the upper layer on the progress */ |
Holger Freyther | b7193e4 | 2008-12-29 17:44:08 +0000 | [diff] [blame] | 266 | if (bts->network->update_request) |
| 267 | (*bts->network->update_request)(bts, tmsi, 1); |
Holger Freyther | 07cc8d8 | 2008-12-29 06:23:46 +0000 | [diff] [blame] | 268 | |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 269 | ret = gsm48_sendmsg(msg); |
| 270 | |
| 271 | /* return gsm48_cc_tx_setup(lchan); */ |
| 272 | ret = gsm48_tx_mm_info(lchan); |
| 273 | ret = gsm0411_send_sms(lchan, NULL); |
| 274 | |
Holger Freyther | 07cc8d8 | 2008-12-29 06:23:46 +0000 | [diff] [blame] | 275 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 278 | static char bcd2char(u_int8_t bcd) |
| 279 | { |
| 280 | if (bcd < 0xa) |
| 281 | return '0' + bcd; |
| 282 | else |
| 283 | return 'A' + (bcd - 0xa); |
| 284 | } |
| 285 | |
| 286 | /* 10.5.1.4 */ |
| 287 | static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len) |
| 288 | { |
| 289 | int i; |
| 290 | u_int8_t mi_type; |
| 291 | char *str_cur = string; |
| 292 | |
| 293 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 294 | |
| 295 | switch (mi_type) { |
| 296 | case GSM_MI_TYPE_NONE: |
| 297 | break; |
| 298 | case GSM_MI_TYPE_TMSI: |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 299 | /* skip padding nibble at the beginning, start at offset 1... */ |
| 300 | for (i = 1; i < mi_len; i++) { |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 301 | if (str_cur + 2 >= string + str_len) |
| 302 | return str_cur - string; |
| 303 | *str_cur++ = bcd2char(mi[i] >> 4); |
| 304 | *str_cur++ = bcd2char(mi[i] & 0xf); |
| 305 | } |
| 306 | break; |
| 307 | case GSM_MI_TYPE_IMSI: |
| 308 | case GSM_MI_TYPE_IMEI: |
| 309 | case GSM_MI_TYPE_IMEISV: |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 310 | *str_cur++ = bcd2char(mi[0] >> 4); |
| 311 | |
| 312 | for (i = 1; i < mi_len; i++) { |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 313 | if (str_cur + 2 >= string + str_len) |
| 314 | return str_cur - string; |
| 315 | *str_cur++ = bcd2char(mi[i] & 0xf); |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 316 | /* skip last nibble in last input byte when GSM_EVEN */ |
| 317 | if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD)) |
| 318 | *str_cur++ = bcd2char(mi[i] >> 4); |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 319 | } |
| 320 | break; |
| 321 | default: |
| 322 | break; |
| 323 | } |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 324 | *str_cur++ = '\0'; |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 325 | |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 326 | return str_cur - string; |
| 327 | } |
| 328 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 329 | /* Chapter 9.2.10 */ |
| 330 | static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type) |
| 331 | { |
| 332 | struct msgb *msg = gsm48_msgb_alloc(); |
| 333 | struct gsm48_hdr *gh; |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 334 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 335 | msg->lchan = lchan; |
| 336 | |
| 337 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 338 | gh->proto_discr = GSM48_PDISC_MM; |
| 339 | gh->msg_type = GSM48_MT_MM_ID_REQ; |
| 340 | gh->data[0] = id_type; |
| 341 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 342 | return gsm48_sendmsg(msg); |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | #define MI_SIZE 32 |
| 346 | |
| 347 | /* Chapter 9.2.11 */ |
| 348 | static int mm_rx_id_resp(struct msgb *msg) |
| 349 | { |
| 350 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 351 | struct gsm_lchan *lchan = msg->lchan; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 352 | u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK; |
| 353 | char mi_string[MI_SIZE]; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 354 | u_int32_t tmsi; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 355 | |
| 356 | 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] | 357 | DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n", |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 358 | mi_type, mi_string); |
| 359 | |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 360 | /* |
| 361 | * Rogue messages could trick us but so is life |
| 362 | */ |
| 363 | put_lchan(lchan); |
| 364 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 365 | switch (mi_type) { |
| 366 | case GSM_MI_TYPE_IMSI: |
| 367 | if (!lchan->subscr) |
| 368 | lchan->subscr = db_create_subscriber(mi_string); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 369 | if (lchan->loc_operation) |
| 370 | lchan->loc_operation->waiting_for_imsi = 0; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 371 | break; |
| 372 | case GSM_MI_TYPE_IMEI: |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 373 | case GSM_MI_TYPE_IMEISV: |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 374 | /* update subscribe <-> IMEI mapping */ |
| 375 | if (lchan->subscr) |
| 376 | db_subscriber_assoc_imei(lchan->subscr, mi_string); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 377 | if (lchan->loc_operation) |
| 378 | lchan->loc_operation->waiting_for_imei = 0; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 379 | break; |
| 380 | } |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 381 | |
| 382 | /* Check if we can let the mobile station enter */ |
| 383 | if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) { |
| 384 | db_subscriber_alloc_tmsi(lchan->subscr); |
| 385 | tmsi = strtoul(lchan->subscr->tmsi, NULL, 10); |
| 386 | release_loc_updating_req(lchan); |
| 387 | return gsm0408_loc_upd_acc(msg->lchan, tmsi); |
| 388 | } |
| 389 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 390 | return 0; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 393 | |
| 394 | static void loc_upd_rej_cb(void *data) |
| 395 | { |
| 396 | struct gsm_lchan *lchan = data; |
| 397 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 398 | release_loc_updating_req(lchan); |
Holger Freyther | e97f7fb | 2008-12-31 18:52:11 +0000 | [diff] [blame] | 399 | gsm0408_loc_upd_rej(lchan, reject_cause); |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 400 | lchan_auto_release(lchan); |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Holger Freyther | b7193e4 | 2008-12-29 17:44:08 +0000 | [diff] [blame] | 403 | static void schedule_reject(struct gsm_lchan *lchan) |
| 404 | { |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 405 | lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb; |
| 406 | lchan->loc_operation->updating_timer.data = lchan; |
| 407 | schedule_timer(&lchan->loc_operation->updating_timer, 5, 0); |
Holger Freyther | b7193e4 | 2008-12-29 17:44:08 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 410 | #define MI_SIZE 32 |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 411 | /* Chapter 9.2.15 */ |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 412 | static int mm_rx_loc_upd_req(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 413 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 414 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 415 | struct gsm_bts *bts = msg->trx->bts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 416 | struct gsm48_loc_upd_req *lu; |
| 417 | struct gsm_subscriber *subscr; |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 418 | struct gsm_lchan *lchan = msg->lchan; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 419 | u_int8_t mi_type; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 420 | u_int32_t tmsi; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 421 | char mi_string[MI_SIZE]; |
| 422 | int rc; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 423 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 424 | lu = (struct gsm48_loc_upd_req *) gh->data; |
| 425 | |
| 426 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 427 | |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 428 | mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
| 429 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 430 | DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 431 | |
| 432 | allocate_loc_updating_req(lchan); |
| 433 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 434 | switch (mi_type) { |
| 435 | case GSM_MI_TYPE_IMSI: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 436 | /* we always want the IMEI, too */ |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 437 | use_lchan(lchan); |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 438 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 439 | lchan->loc_operation->waiting_for_imei = 1; |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 440 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 441 | /* look up subscriber based on IMSI */ |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 442 | subscr = db_create_subscriber(mi_string); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 443 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 444 | case GSM_MI_TYPE_TMSI: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 445 | /* we always want the IMEI, too */ |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 446 | use_lchan(lchan); |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 447 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 448 | lchan->loc_operation->waiting_for_imei = 1; |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 449 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 450 | /* look up the subscriber based on TMSI, request IMSI if it fails */ |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 451 | subscr = subscr_get_by_tmsi(lu->mi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 452 | if (!subscr) { |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 453 | /* send IDENTITY REQUEST message to get IMSI */ |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 454 | use_lchan(lchan); |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 455 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 456 | lchan->loc_operation->waiting_for_imsi = 1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 457 | } |
| 458 | break; |
| 459 | case GSM_MI_TYPE_IMEI: |
| 460 | case GSM_MI_TYPE_IMEISV: |
| 461 | /* no sim card... FIXME: what to do ? */ |
| 462 | fprintf(stderr, "Unimplemented mobile identity type\n"); |
| 463 | break; |
| 464 | default: |
| 465 | fprintf(stderr, "Unknown mobile identity type\n"); |
| 466 | break; |
| 467 | } |
| 468 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 469 | lchan->subscr = subscr; |
| 470 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 471 | /* |
| 472 | * Schedule the reject timer and check if we can let the |
| 473 | * subscriber into our network immediately or if we need to wait |
| 474 | * for identity responses. |
| 475 | */ |
| 476 | schedule_reject(lchan); |
| 477 | if (!authorize_subscriber(lchan->loc_operation, subscr)) |
Harald Welte | e872cb1 | 2009-01-01 00:33:37 +0000 | [diff] [blame] | 478 | return 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 479 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 480 | db_subscriber_alloc_tmsi(subscr); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 481 | subscr_update(subscr, bts); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 482 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 483 | tmsi = strtoul(subscr->tmsi, NULL, 10); |
| 484 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 485 | release_loc_updating_req(lchan); |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 486 | return gsm0408_loc_upd_acc(lchan, tmsi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 489 | /* Section 9.2.15a */ |
| 490 | int gsm48_tx_mm_info(struct gsm_lchan *lchan) |
| 491 | { |
| 492 | struct msgb *msg = gsm48_msgb_alloc(); |
| 493 | struct gsm48_hdr *gh; |
| 494 | struct gsm_network *net = lchan->ts->trx->bts->network; |
| 495 | time_t cur_t; |
| 496 | struct tm* cur_time; |
| 497 | u_int8_t *ptr8; |
| 498 | u_int16_t *ptr16; |
| 499 | int name_len; |
| 500 | int tz15min; |
| 501 | int i; |
| 502 | |
| 503 | msg->lchan = lchan; |
| 504 | |
| 505 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 506 | gh->proto_discr = GSM48_PDISC_MM; |
| 507 | gh->msg_type = GSM48_MT_MM_INFO; |
| 508 | |
| 509 | if (net->name_long) { |
| 510 | name_len = strlen(net->name_long); |
| 511 | /* 10.5.3.5a */ |
| 512 | ptr8 = msgb_put(msg, 3); |
| 513 | ptr8[0] = GSM48_IE_NAME_LONG; |
| 514 | ptr8[1] = name_len*2 +1; |
| 515 | ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */ |
| 516 | |
| 517 | ptr16 = (u_int16_t *) msgb_put(msg, name_len*2); |
| 518 | for (i = 0; i < name_len; i++) |
Harald Welte | 179f064 | 2008-12-31 23:59:18 +0000 | [diff] [blame] | 519 | ptr16[i] = htons(net->name_long[i]); |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 520 | |
| 521 | /* FIXME: Use Cell Broadcast, not UCS-2, since |
| 522 | * UCS-2 is only supported by later revisions of the spec */ |
| 523 | } |
| 524 | |
| 525 | if (net->name_short) { |
| 526 | name_len = strlen(net->name_short); |
| 527 | /* 10.5.3.5a */ |
| 528 | ptr8 = (u_int8_t *) msgb_put(msg, 3); |
| 529 | ptr8[0] = GSM48_IE_NAME_LONG; |
| 530 | ptr8[1] = name_len*2 + 1; |
| 531 | ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */ |
| 532 | |
Harald Welte | e872cb1 | 2009-01-01 00:33:37 +0000 | [diff] [blame] | 533 | ptr16 = (u_int16_t *) msgb_put(msg, name_len*2); |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 534 | for (i = 0; i < name_len; i++) |
Harald Welte | 179f064 | 2008-12-31 23:59:18 +0000 | [diff] [blame] | 535 | ptr16[i] = htons(net->name_short[i]); |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | #if 0 |
| 539 | /* Section 10.5.3.9 */ |
| 540 | cur_t = time(NULL); |
| 541 | cur_time = gmtime(cur_t); |
| 542 | ptr8 = msgb_put(msg, 8); |
| 543 | ptr8[0] = GSM48_IE_NET_TIME_TZ; |
| 544 | ptr8[1] = to_bcd8(cur_time->tm_year % 100); |
| 545 | ptr8[2] = to_bcd8(cur_time->tm_mon); |
| 546 | ptr8[3] = to_bcd8(cur_time->tm_mday); |
| 547 | ptr8[4] = to_bcd8(cur_time->tm_hour); |
| 548 | ptr8[5] = to_bcd8(cur_time->tm_min); |
| 549 | ptr8[6] = to_bcd8(cur_time->tm_sec); |
| 550 | /* 02.42: coded as BCD encoded signed value in units of 15 minutes */ |
| 551 | tz15min = (cur_time->tm_gmtoff)/(60*15); |
| 552 | ptr8[6] = to_bcd8(tz15min); |
| 553 | if (tz15min < 0) |
| 554 | ptr8[6] |= 0x80; |
| 555 | #endif |
| 556 | |
| 557 | return gsm48_sendmsg(msg); |
| 558 | } |
| 559 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 560 | static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan) |
| 561 | { |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 562 | DEBUGP(DMM, "-> CM SERVICE ACK\n"); |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 563 | return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static int gsm48_rx_mm_serv_req(struct msgb *msg) |
| 567 | { |
| 568 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 569 | u_int8_t serv_type = gh->data[0] & 0x0f; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 570 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 571 | DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x\n", serv_type); |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 572 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 573 | return gsm48_tx_mm_serv_ack(msg->lchan); |
| 574 | } |
| 575 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 576 | static int gsm0408_rcv_mm(struct msgb *msg) |
| 577 | { |
| 578 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 579 | int rc; |
| 580 | |
| 581 | switch (gh->msg_type & 0xbf) { |
| 582 | case GSM48_MT_MM_LOC_UPD_REQUEST: |
Holger Freyther | 429e776 | 2008-12-30 13:28:30 +0000 | [diff] [blame] | 583 | DEBUGP(DMM, "LOCATION UPDATING REQUEST\n"); |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 584 | rc = mm_rx_loc_upd_req(msg); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 585 | break; |
| 586 | case GSM48_MT_MM_ID_RESP: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 587 | rc = mm_rx_id_resp(msg); |
| 588 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 589 | case GSM48_MT_MM_CM_SERV_REQ: |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 590 | rc = gsm48_rx_mm_serv_req(msg); |
| 591 | break; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 592 | case GSM48_MT_MM_STATUS: |
| 593 | DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n"); |
| 594 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 595 | case GSM48_MT_MM_CM_REEST_REQ: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 596 | case GSM48_MT_MM_TMSI_REALL_COMPL: |
| 597 | case GSM48_MT_MM_AUTH_RESP: |
| 598 | case GSM48_MT_MM_IMSI_DETACH_IND: |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 599 | fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n", |
| 600 | gh->msg_type); |
| 601 | break; |
| 602 | default: |
| 603 | fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n", |
| 604 | gh->msg_type); |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | return rc; |
| 609 | } |
| 610 | static int gsm0408_rcv_rr(struct msgb *msg) |
| 611 | { |
| 612 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 613 | |
| 614 | switch (gh->msg_type) { |
| 615 | case GSM48_MT_RR_CLSM_CHG: |
| 616 | DEBUGP(DRR, "CLASSMARK CHANGE\n"); |
| 617 | /* FIXME: what to do ?!? */ |
| 618 | break; |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 619 | case GSM48_MT_RR_GPRS_SUSP_REQ: |
| 620 | DEBUGP(DRR, "GRPS SUSPEND REQUEST\n"); |
| 621 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 622 | case GSM48_MT_RR_PAG_RESP: |
| 623 | default: |
| 624 | fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n", |
| 625 | gh->msg_type); |
| 626 | break; |
| 627 | } |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 632 | /* Call Control */ |
| 633 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 634 | static int gsm48_cc_tx_status(struct gsm_lchan *lchan) |
| 635 | { |
| 636 | struct msgb *msg = gsm48_msgb_alloc(); |
| 637 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 638 | u_int8_t *cause, *call_state; |
| 639 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 640 | gh->proto_discr = GSM48_PDISC_CC; |
| 641 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 642 | msg->lchan = lchan; |
| 643 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 644 | gh->msg_type = GSM48_MT_CC_STATUS; |
| 645 | |
| 646 | cause = msgb_put(msg, 3); |
| 647 | cause[0] = 2; |
| 648 | cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER; |
| 649 | cause[2] = 0x80 | 30; /* response to status inquiry */ |
| 650 | |
| 651 | call_state = msgb_put(msg, 1); |
| 652 | call_state[0] = 0xc0 | 0x00; |
| 653 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 654 | return gsm48_sendmsg(msg); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Harald Welte | 6f4b753 | 2008-12-29 00:39:37 +0000 | [diff] [blame] | 657 | static int gsm48_tx_simple(struct gsm_lchan *lchan, |
| 658 | u_int8_t pdisc, u_int8_t msg_type) |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 659 | { |
| 660 | struct msgb *msg = gsm48_msgb_alloc(); |
| 661 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 662 | |
| 663 | msg->lchan = lchan; |
| 664 | |
Harald Welte | 6f4b753 | 2008-12-29 00:39:37 +0000 | [diff] [blame] | 665 | gh->proto_discr = pdisc; |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 666 | gh->msg_type = msg_type; |
| 667 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 668 | return gsm48_sendmsg(msg); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | static int gsm48_cc_rx_status_enq(struct msgb *msg) |
| 672 | { |
| 673 | return gsm48_cc_tx_status(msg->lchan); |
| 674 | } |
| 675 | |
| 676 | static int gsm48_cc_rx_setup(struct msgb *msg) |
| 677 | { |
Harald Welte | 6f4b753 | 2008-12-29 00:39:37 +0000 | [diff] [blame] | 678 | return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 679 | GSM48_MT_CC_CALL_CONF); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 682 | int gsm48_cc_tx_setup(struct gsm_lchan *lchan) |
| 683 | { |
| 684 | struct msgb *msg = gsm48_msgb_alloc(); |
| 685 | struct gsm48_hdr *gh; |
| 686 | struct gsm_call *call = &lchan->call; |
| 687 | |
| 688 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 8); |
| 689 | |
| 690 | call->type = GSM_CT_MT; |
| 691 | msg->lchan = lchan; |
| 692 | |
| 693 | gh->proto_discr = GSM48_PDISC_CC; |
| 694 | gh->msg_type = GSM48_MT_CC_SETUP; |
| 695 | gh->data[0] = 0x34; |
| 696 | gh->data[1] = 0x00; |
| 697 | gh->data[2] = 0x5c; |
| 698 | gh->data[3] = 0x04; |
| 699 | gh->data[4] = 0xb9; |
| 700 | gh->data[5] = 0x83; |
| 701 | gh->data[6] = 0x32; |
| 702 | gh->data[7] = 0x24; |
| 703 | |
| 704 | DEBUGP(DCC, "Sending SETUP\n"); |
| 705 | |
| 706 | return gsm48_sendmsg(msg); |
| 707 | } |
| 708 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 709 | static int gsm0408_rcv_cc(struct msgb *msg) |
| 710 | { |
| 711 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 712 | u_int8_t msg_type = gh->msg_type & 0xbf; |
| 713 | struct gsm_call *call = &msg->lchan->call; |
Holger Freyther | 2eafef5 | 2008-12-29 06:42:17 +0000 | [diff] [blame] | 714 | struct gsm_network *network = msg->lchan->ts->trx->bts->network; |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 715 | int rc = 0; |
| 716 | |
| 717 | switch (msg_type) { |
| 718 | case GSM48_MT_CC_CALL_CONF: |
| 719 | /* Response to SETUP */ |
| 720 | DEBUGP(DCC, "CALL CONFIRM\n"); |
| 721 | break; |
| 722 | case GSM48_MT_CC_RELEASE_COMPL: |
| 723 | /* Answer from MS to RELEASE */ |
| 724 | DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n"); |
Holger Freyther | b7193e4 | 2008-12-29 17:44:08 +0000 | [diff] [blame] | 725 | if (network->call_state_changed) |
| 726 | (*network->call_state_changed)(msg->lchan, call->state); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 727 | call->state = GSM_CSTATE_NULL; |
| 728 | break; |
| 729 | case GSM48_MT_CC_ALERTING: |
| 730 | DEBUGP(DCC, "ALERTING\n"); |
| 731 | break; |
| 732 | case GSM48_MT_CC_CONNECT: |
| 733 | DEBUGP(DCC, "CONNECT\n"); |
| 734 | /* MT: need to respond with CONNECT_ACK */ |
Harald Welte | 6f4b753 | 2008-12-29 00:39:37 +0000 | [diff] [blame] | 735 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 736 | GSM48_MT_CC_CONNECT_ACK); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 737 | break; |
| 738 | case GSM48_MT_CC_CONNECT_ACK: |
| 739 | /* MO: Answer to CONNECT */ |
| 740 | call->state = GSM_CSTATE_ACTIVE; |
| 741 | DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n"); |
| 742 | break; |
| 743 | case GSM48_MT_CC_RELEASE: |
| 744 | DEBUGP(DCC, "RELEASE\n"); |
| 745 | /* need to respond with RELEASE_COMPLETE */ |
| 746 | break; |
| 747 | case GSM48_MT_CC_STATUS_ENQ: |
| 748 | rc = gsm48_cc_rx_status_enq(msg); |
| 749 | break; |
| 750 | case GSM48_MT_CC_DISCONNECT: |
| 751 | /* Section 5.4.3.2 */ |
| 752 | DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n"); |
| 753 | call->state = GSM_CSTATE_RELEASE_REQ; |
| 754 | /* FIXME: clear the network connection */ |
Harald Welte | 6f4b753 | 2008-12-29 00:39:37 +0000 | [diff] [blame] | 755 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 756 | GSM48_MT_CC_RELEASE); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 757 | break; |
| 758 | case GSM48_MT_CC_SETUP: |
| 759 | call->type = GSM_CT_MO; |
| 760 | call->state = GSM_CSTATE_INITIATED; |
| 761 | call->transaction_id = gh->proto_discr & 0xf0; |
| 762 | DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id); |
Harald Welte | 6f4b753 | 2008-12-29 00:39:37 +0000 | [diff] [blame] | 763 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 764 | GSM48_MT_CC_CONNECT); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 765 | /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */ |
| 766 | break; |
| 767 | case GSM48_MT_CC_EMERG_SETUP: |
| 768 | DEBUGP(DCC, "EMERGENCY SETUP\n"); |
| 769 | /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */ |
| 770 | break; |
| 771 | default: |
| 772 | fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n", |
| 773 | msg_type); |
| 774 | break; |
| 775 | } |
| 776 | |
| 777 | return rc; |
| 778 | } |
| 779 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 780 | /* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */ |
| 781 | int gsm0408_rcvmsg(struct msgb *msg) |
| 782 | { |
| 783 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 784 | u_int8_t pdisc = gh->proto_discr & 0x0f; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 785 | int rc = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 786 | |
| 787 | switch (pdisc) { |
| 788 | case GSM48_PDISC_CC: |
| 789 | rc = gsm0408_rcv_cc(msg); |
| 790 | break; |
| 791 | case GSM48_PDISC_MM: |
| 792 | rc = gsm0408_rcv_mm(msg); |
| 793 | break; |
| 794 | case GSM48_PDISC_RR: |
| 795 | rc = gsm0408_rcv_rr(msg); |
| 796 | break; |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 797 | case GSM48_PDISC_SMS: |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 798 | rc = gsm0411_rcv_sms(msg); |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 799 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 800 | case GSM48_PDISC_MM_GPRS: |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 801 | case GSM48_PDISC_SM_GPRS: |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 802 | fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n", |
| 803 | pdisc); |
| 804 | break; |
| 805 | default: |
| 806 | fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n", |
| 807 | pdisc); |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | return rc; |
| 812 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 813 | |
| 814 | enum chreq_type { |
| 815 | CHREQ_T_EMERG_CALL, |
| 816 | CHREQ_T_CALL_REEST_TCH_F, |
| 817 | CHREQ_T_CALL_REEST_TCH_H, |
| 818 | CHREQ_T_CALL_REEST_TCH_H_DBL, |
| 819 | CHREQ_T_SDCCH, |
| 820 | CHREQ_T_TCH_F, |
| 821 | CHREQ_T_VOICE_CALL_TCH_H, |
| 822 | CHREQ_T_DATA_CALL_TCH_H, |
| 823 | CHREQ_T_LOCATION_UPD, |
| 824 | CHREQ_T_PAG_R_ANY, |
| 825 | CHREQ_T_PAG_R_TCH_F, |
| 826 | CHREQ_T_PAG_R_TCH_FH, |
| 827 | }; |
| 828 | |
| 829 | /* Section 9.1.8 / Table 9.9 */ |
| 830 | struct chreq { |
| 831 | u_int8_t val; |
| 832 | u_int8_t mask; |
| 833 | enum chreq_type type; |
| 834 | }; |
| 835 | |
| 836 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */ |
| 837 | static const struct chreq chreq_type_neci1[] = { |
| 838 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 839 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F }, |
| 840 | { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H }, |
| 841 | { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL }, |
| 842 | { 0xe0, 0xe0, CHREQ_T_SDCCH }, |
| 843 | { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H }, |
| 844 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 845 | { 0x00, 0xf0, CHREQ_T_LOCATION_UPD }, |
| 846 | { 0x10, 0xf0, CHREQ_T_SDCCH }, |
| 847 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 848 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 849 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 850 | }; |
| 851 | |
| 852 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */ |
| 853 | static const struct chreq chreq_type_neci0[] = { |
| 854 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 855 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H }, |
| 856 | { 0xe0, 0xe0, CHREQ_T_TCH_F }, |
| 857 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 858 | { 0x00, 0xe0, CHREQ_T_LOCATION_UPD }, |
| 859 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 860 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 861 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 862 | }; |
| 863 | |
| 864 | static const enum gsm_chan_t ctype_by_chreq[] = { |
| 865 | [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F, |
| 866 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F, |
| 867 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H, |
| 868 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H, |
| 869 | [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH, |
| 870 | [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F, |
| 871 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 872 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 873 | [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH, |
| 874 | [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH, |
| 875 | [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F, |
| 876 | [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F, |
| 877 | }; |
| 878 | |
Harald Welte | e14a57c | 2008-12-29 04:08:28 +0000 | [diff] [blame] | 879 | static const enum gsm_chreq_reason_t reason_by_chreq[] = { |
| 880 | [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG, |
| 881 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL, |
| 882 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL, |
| 883 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL, |
| 884 | [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER, |
| 885 | [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER, |
| 886 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 887 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 888 | [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD, |
| 889 | [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG, |
| 890 | [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG, |
| 891 | [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG, |
| 892 | }; |
| 893 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 894 | enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 895 | { |
| 896 | int i; |
| 897 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 898 | |
| 899 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 900 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 901 | if ((ra & chr->mask) == chr->val) |
| 902 | return ctype_by_chreq[chr->type]; |
| 903 | } |
| 904 | fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra); |
| 905 | return GSM_LCHAN_SDCCH; |
| 906 | } |
Harald Welte | e14a57c | 2008-12-29 04:08:28 +0000 | [diff] [blame] | 907 | |
| 908 | enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 909 | { |
| 910 | int i; |
| 911 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 912 | |
| 913 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 914 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 915 | if ((ra & chr->mask) == chr->val) |
| 916 | return reason_by_chreq[chr->type]; |
| 917 | } |
| 918 | fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra); |
| 919 | return GSM_CHREQ_REASON_OTHER; |
| 920 | } |