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