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