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