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 | |
Harald Welte | bf5e8df | 2009-02-03 12:59:45 +0000 | [diff] [blame] | 4 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 498b0bb | 2009-01-09 21:27:43 +0000 | [diff] [blame] | 5 | * (C) 2008, 2009 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> |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 35 | #include <openbsc/tlv.h> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 36 | #include <openbsc/debug.h> |
| 37 | #include <openbsc/gsm_data.h> |
| 38 | #include <openbsc/gsm_subscriber.h> |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 39 | #include <openbsc/gsm_04_11.h> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 40 | #include <openbsc/gsm_04_08.h> |
| 41 | #include <openbsc/abis_rsl.h> |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 42 | #include <openbsc/chan_alloc.h> |
Harald Welte | 0b4c34e | 2009-02-09 17:54:43 +0000 | [diff] [blame] | 43 | #include <openbsc/paging.h> |
Holger Freyther | 053e09d | 2009-02-14 22:51:06 +0000 | [diff] [blame] | 44 | #include <openbsc/signal.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 45 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 46 | #define GSM48_ALLOC_SIZE 1024 |
| 47 | #define GSM48_ALLOC_HEADROOM 128 |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 48 | |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 49 | static const struct tlv_definition rsl_att_tlvdef = { |
| 50 | .def = { |
| 51 | [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV }, |
| 52 | [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV }, |
| 53 | [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV }, |
| 54 | [GSM48_IE_UTC] = { TLV_TYPE_TV }, |
| 55 | [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 }, |
| 56 | [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV }, |
| 57 | |
| 58 | [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV }, |
| 59 | [GSM48_IE_CAUSE] = { TLV_TYPE_TLV }, |
| 60 | [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV }, |
| 61 | [GSM48_IE_ALERT] = { TLV_TYPE_TLV }, |
| 62 | [GSM48_IE_FACILITY] = { TLV_TYPE_TLV }, |
| 63 | [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV }, |
| 64 | [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV }, |
| 65 | [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV }, |
| 66 | [GSM48_IE_SIGNAL] = { TLV_TYPE_TV }, |
| 67 | [GSM48_IE_CONN_NUM] = { TLV_TYPE_TLV }, |
| 68 | [GSM48_IE_CONN_SUBADDR] = { TLV_TYPE_TLV }, |
| 69 | [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV }, |
| 70 | [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV }, |
| 71 | [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV }, |
| 72 | [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV }, |
| 73 | [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV }, |
| 74 | [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV }, |
| 75 | [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV }, |
| 76 | [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV }, |
| 77 | [GSM48_IE_USER_USER] = { TLV_TYPE_TLV }, |
| 78 | [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV }, |
| 79 | [GSM48_IE_MORE_DATA] = { TLV_TYPE_T }, |
| 80 | [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T }, |
| 81 | [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T }, |
| 82 | [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T }, |
| 83 | /* FIXME: more elements */ |
| 84 | }, |
| 85 | }; |
| 86 | |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 87 | static inline int is_ipaccess_bts(struct gsm_bts *bts) |
| 88 | { |
| 89 | switch (bts->type) { |
| 90 | case GSM_BTS_TYPE_NANOBTS_900: |
| 91 | case GSM_BTS_TYPE_NANOBTS_1800: |
| 92 | return 1; |
| 93 | default: |
| 94 | break; |
| 95 | } |
| 96 | return 0; |
| 97 | } |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 98 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 99 | static int gsm48_tx_simple(struct gsm_lchan *lchan, |
| 100 | u_int8_t pdisc, u_int8_t msg_type); |
Holger Freyther | b7193e4 | 2008-12-29 17:44:08 +0000 | [diff] [blame] | 101 | static void schedule_reject(struct gsm_lchan *lchan); |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 102 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 103 | struct gsm_lai { |
| 104 | u_int16_t mcc; |
| 105 | u_int16_t mnc; |
| 106 | u_int16_t lac; |
| 107 | }; |
| 108 | |
Holger Freyther | 89824fc | 2008-12-30 16:18:18 +0000 | [diff] [blame] | 109 | static int authorize_everonye = 0; |
| 110 | void gsm0408_allow_everyone(int everyone) |
| 111 | { |
| 112 | printf("Allowing everyone?\n"); |
| 113 | authorize_everonye = everyone; |
| 114 | } |
| 115 | |
Holger Freyther | e97f7fb | 2008-12-31 18:52:11 +0000 | [diff] [blame] | 116 | static int reject_cause = 0; |
| 117 | void gsm0408_set_reject_cause(int cause) |
| 118 | { |
| 119 | reject_cause = cause; |
| 120 | } |
| 121 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 122 | static int authorize_subscriber(struct gsm_loc_updating_operation *loc, |
| 123 | struct gsm_subscriber *subscriber) |
Holger Freyther | 89824fc | 2008-12-30 16:18:18 +0000 | [diff] [blame] | 124 | { |
| 125 | if (!subscriber) |
| 126 | return 0; |
| 127 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 128 | /* |
| 129 | * Do not send accept yet as more information should arrive. Some |
| 130 | * phones will not send us the information and we will have to check |
| 131 | * what we want to do with that. |
| 132 | */ |
| 133 | if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei)) |
| 134 | return 0; |
| 135 | |
Holger Freyther | 89824fc | 2008-12-30 16:18:18 +0000 | [diff] [blame] | 136 | if (authorize_everonye) |
| 137 | return 1; |
| 138 | |
| 139 | return subscriber->authorized; |
| 140 | } |
Holger Freyther | 07cc8d8 | 2008-12-29 06:23:46 +0000 | [diff] [blame] | 141 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 142 | static void release_loc_updating_req(struct gsm_lchan *lchan) |
| 143 | { |
Harald Welte | 179f064 | 2008-12-31 23:59:18 +0000 | [diff] [blame] | 144 | if (!lchan->loc_operation) |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 145 | return; |
| 146 | |
| 147 | del_timer(&lchan->loc_operation->updating_timer); |
| 148 | free(lchan->loc_operation); |
| 149 | lchan->loc_operation = 0; |
Holger Freyther | 3eaa792 | 2009-01-01 02:59:03 +0000 | [diff] [blame] | 150 | put_lchan(lchan); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void allocate_loc_updating_req(struct gsm_lchan *lchan) |
| 154 | { |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 155 | use_lchan(lchan); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 156 | release_loc_updating_req(lchan); |
| 157 | |
| 158 | lchan->loc_operation = (struct gsm_loc_updating_operation *) |
| 159 | malloc(sizeof(*lchan->loc_operation)); |
| 160 | memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation)); |
| 161 | } |
Holger Freyther | 07cc8d8 | 2008-12-29 06:23:46 +0000 | [diff] [blame] | 162 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 163 | static void to_bcd(u_int8_t *bcd, u_int16_t val) |
| 164 | { |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 165 | bcd[2] = val % 10; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 166 | val = val / 10; |
| 167 | bcd[1] = val % 10; |
| 168 | val = val / 10; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 169 | bcd[0] = val % 10; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 170 | val = val / 10; |
| 171 | } |
| 172 | |
Holger Freyther | 1774661 | 2008-12-28 16:32:44 +0000 | [diff] [blame] | 173 | 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] | 174 | u_int16_t mnc, u_int16_t lac) |
| 175 | { |
| 176 | u_int8_t bcd[3]; |
| 177 | |
| 178 | to_bcd(bcd, mcc); |
| 179 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 180 | lai48->digits[1] = bcd[2]; |
| 181 | |
| 182 | to_bcd(bcd, mnc); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 183 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
| 184 | #if 0 |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 185 | lai48->digits[1] |= bcd[2] << 4; |
| 186 | lai48->digits[2] = bcd[0] | (bcd[1] << 4); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 187 | #else |
| 188 | lai48->digits[1] |= 0xf << 4; |
| 189 | lai48->digits[2] = bcd[1] | (bcd[2] << 4); |
| 190 | #endif |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 191 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 192 | lai48->lac = htons(lac); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 195 | #define TMSI_LEN 5 |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 196 | #define MID_TMSI_LEN (TMSI_LEN + 2) |
| 197 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 198 | 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] | 199 | { |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 200 | u_int32_t *tptr = (u_int32_t *) &buf[3]; |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 201 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 202 | buf[0] = GSM48_IE_MOBILE_ID; |
Harald Welte | 1a41218 | 2008-12-27 22:13:43 +0000 | [diff] [blame] | 203 | buf[1] = TMSI_LEN; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 204 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 205 | *tptr = htonl(tmsi); |
| 206 | |
| 207 | return 7; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 210 | static const char bcd_num_digits[] = { |
| 211 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 212 | '8', '9', '*', '#', 'a', 'b', 'c', '\0' |
| 213 | }; |
| 214 | |
| 215 | /* decode a 'called party BCD number' as in 10.5.4.7 */ |
| 216 | u_int8_t decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv) |
| 217 | { |
| 218 | u_int8_t in_len = bcd_lv[0]; |
| 219 | int i; |
| 220 | |
| 221 | if (in_len < 1) |
| 222 | return 0; |
| 223 | |
| 224 | for (i = 2; i <= in_len; i++) { |
| 225 | /* lower nibble */ |
| 226 | output_len--; |
| 227 | if (output_len <= 1) |
| 228 | break; |
| 229 | *output++ = bcd_num_digits[bcd_lv[i] & 0xf]; |
| 230 | |
| 231 | /* higher nibble */ |
| 232 | output_len--; |
| 233 | if (output_len <= 1) |
| 234 | break; |
| 235 | *output++ = bcd_num_digits[bcd_lv[i] >> 4]; |
| 236 | } |
| 237 | if (output_len >= 1) |
| 238 | *output++ = '\0'; |
| 239 | |
| 240 | /* return number type / calling plan */ |
| 241 | return bcd_lv[1] & 0x3f; |
| 242 | } |
| 243 | |
| 244 | /* convert a single ASCII character to call-control BCD */ |
| 245 | static int asc_to_bcd(const char asc) |
| 246 | { |
| 247 | int i; |
| 248 | |
| 249 | for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) { |
| 250 | if (bcd_num_digits[i] == asc) |
| 251 | return i; |
| 252 | } |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
| 256 | /* convert a ASCII phone number to 'called party BCD number' */ |
| 257 | int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len, |
| 258 | u_int8_t type, const char *input) |
| 259 | { |
| 260 | int in_len = strlen(input); |
| 261 | int i; |
| 262 | u_int8_t *bcd_cur = bcd_lv + 2; |
| 263 | |
| 264 | if (in_len/2 + 1 > max_len) |
| 265 | return -EIO; |
| 266 | |
| 267 | /* two digits per byte, plus type byte */ |
| 268 | bcd_lv[0] = in_len/2 + 1; |
| 269 | if (in_len % 2) |
| 270 | bcd_lv[0]++; |
| 271 | |
| 272 | /* if the caller wants to create a valid 'calling party BCD |
| 273 | * number', then the extension bit MUST NOT be set. For the |
| 274 | * 'called party BCD number' it MUST be set. *sigh */ |
| 275 | bcd_lv[1] = type; |
| 276 | |
| 277 | for (i = 0; i < in_len; i++) { |
| 278 | int rc = asc_to_bcd(input[i]); |
| 279 | if (rc < 0) |
| 280 | return rc; |
| 281 | if (i % 2 == 0) |
| 282 | *bcd_cur = rc; |
| 283 | else |
| 284 | *bcd_cur++ |= (rc << 4); |
| 285 | } |
| 286 | /* append padding nibble in case of odd length */ |
| 287 | if (i % 2) |
| 288 | *bcd_cur++ |= 0xf0; |
| 289 | |
| 290 | /* return how many bytes we used */ |
| 291 | return (bcd_cur - bcd_lv); |
| 292 | } |
| 293 | |
Holger Freyther | 819dd20 | 2009-01-04 03:52:50 +0000 | [diff] [blame] | 294 | struct msgb *gsm48_msgb_alloc(void) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 295 | { |
| 296 | return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM); |
| 297 | } |
| 298 | |
Holger Freyther | 3e2c323 | 2009-01-04 03:55:31 +0000 | [diff] [blame] | 299 | int gsm48_sendmsg(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 300 | { |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 301 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data; |
| 302 | |
| 303 | if (msg->lchan) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 304 | msg->trx = msg->lchan->ts->trx; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 305 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 306 | if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) { |
| 307 | /* Send a 04.08 call control message, add transaction |
| 308 | * ID and TI flag */ |
| 309 | gh->proto_discr |= msg->lchan->call.transaction_id; |
| 310 | |
| 311 | /* GSM 04.07 Section 11.2.3.1.3 */ |
| 312 | switch (msg->lchan->call.type) { |
| 313 | case GSM_CT_MO: |
| 314 | gh->proto_discr |= 0x80; |
| 315 | break; |
| 316 | case GSM_CT_MT: |
| 317 | break; |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 318 | case GSM_CT_NONE: |
| 319 | break; |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 324 | msg->l3h = msg->data; |
| 325 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 326 | return rsl_data_request(msg, 0); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 329 | |
Holger Freyther | 429e776 | 2008-12-30 13:28:30 +0000 | [diff] [blame] | 330 | /* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 331 | 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] | 332 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 333 | struct msgb *msg = gsm48_msgb_alloc(); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 334 | struct gsm48_hdr *gh; |
| 335 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 336 | msg->lchan = lchan; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 337 | |
| 338 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 339 | gh->proto_discr = GSM48_PDISC_MM; |
Harald Welte | 10b487b | 2008-12-27 19:53:37 +0000 | [diff] [blame] | 340 | gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 341 | gh->data[0] = cause; |
| 342 | |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 343 | DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr); |
| 344 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 345 | return gsm48_sendmsg(msg); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */ |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 349 | 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] | 350 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 351 | struct gsm_bts *bts = lchan->ts->trx->bts; |
| 352 | struct msgb *msg = gsm48_msgb_alloc(); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 353 | struct gsm48_hdr *gh; |
| 354 | struct gsm48_loc_area_id *lai; |
| 355 | u_int8_t *mid; |
Holger Freyther | 07cc8d8 | 2008-12-29 06:23:46 +0000 | [diff] [blame] | 356 | int ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 357 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 358 | msg->lchan = lchan; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 359 | |
| 360 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 361 | gh->proto_discr = GSM48_PDISC_MM; |
| 362 | gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT; |
| 363 | |
| 364 | lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai)); |
Holger Freyther | 1774661 | 2008-12-28 16:32:44 +0000 | [diff] [blame] | 365 | gsm0408_generate_lai(lai, bts->network->country_code, |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 366 | bts->network->network_code, bts->location_area_code); |
| 367 | |
| 368 | mid = msgb_put(msg, MID_TMSI_LEN); |
| 369 | generate_mid_from_tmsi(mid, tmsi); |
| 370 | |
| 371 | DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n"); |
| 372 | |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 373 | ret = gsm48_sendmsg(msg); |
| 374 | |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 375 | ret = gsm48_tx_mm_info(lchan); |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 376 | |
Holger Freyther | 07cc8d8 | 2008-12-29 06:23:46 +0000 | [diff] [blame] | 377 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 380 | static char bcd2char(u_int8_t bcd) |
| 381 | { |
| 382 | if (bcd < 0xa) |
| 383 | return '0' + bcd; |
| 384 | else |
| 385 | return 'A' + (bcd - 0xa); |
| 386 | } |
| 387 | |
Harald Welte | bf5e8df | 2009-02-03 12:59:45 +0000 | [diff] [blame] | 388 | /* Convert Mobile Identity (10.5.1.4) to string */ |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 389 | static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len) |
| 390 | { |
| 391 | int i; |
| 392 | u_int8_t mi_type; |
| 393 | char *str_cur = string; |
Harald Welte | 4ed0e92 | 2009-01-10 03:17:30 +0000 | [diff] [blame] | 394 | u_int32_t tmsi; |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 395 | |
| 396 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 397 | |
| 398 | switch (mi_type) { |
| 399 | case GSM_MI_TYPE_NONE: |
| 400 | break; |
| 401 | case GSM_MI_TYPE_TMSI: |
Harald Welte | 4ed0e92 | 2009-01-10 03:17:30 +0000 | [diff] [blame] | 402 | /* Table 10.5.4.3, reverse generate_mid_from_tmsi */ |
| 403 | if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) { |
| 404 | memcpy(&tmsi, &mi[1], 4); |
| 405 | tmsi = ntohl(tmsi); |
| 406 | return snprintf(string, str_len, "%u", tmsi); |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 407 | } |
| 408 | break; |
| 409 | case GSM_MI_TYPE_IMSI: |
| 410 | case GSM_MI_TYPE_IMEI: |
| 411 | case GSM_MI_TYPE_IMEISV: |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 412 | *str_cur++ = bcd2char(mi[0] >> 4); |
| 413 | |
| 414 | for (i = 1; i < mi_len; i++) { |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 415 | if (str_cur + 2 >= string + str_len) |
| 416 | return str_cur - string; |
| 417 | *str_cur++ = bcd2char(mi[i] & 0xf); |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 418 | /* skip last nibble in last input byte when GSM_EVEN */ |
| 419 | if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD)) |
| 420 | *str_cur++ = bcd2char(mi[i] >> 4); |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 421 | } |
| 422 | break; |
| 423 | default: |
| 424 | break; |
| 425 | } |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 426 | *str_cur++ = '\0'; |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 427 | |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 428 | return str_cur - string; |
| 429 | } |
| 430 | |
Harald Welte | bf5e8df | 2009-02-03 12:59:45 +0000 | [diff] [blame] | 431 | /* Transmit Chapter 9.2.10 Identity Request */ |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 432 | static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type) |
| 433 | { |
| 434 | struct msgb *msg = gsm48_msgb_alloc(); |
| 435 | struct gsm48_hdr *gh; |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 436 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 437 | msg->lchan = lchan; |
| 438 | |
| 439 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 440 | gh->proto_discr = GSM48_PDISC_MM; |
| 441 | gh->msg_type = GSM48_MT_MM_ID_REQ; |
| 442 | gh->data[0] = id_type; |
| 443 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 444 | return gsm48_sendmsg(msg); |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | #define MI_SIZE 32 |
| 448 | |
Harald Welte | bf5e8df | 2009-02-03 12:59:45 +0000 | [diff] [blame] | 449 | /* Parse Chapter 9.2.11 Identity Response */ |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 450 | static int mm_rx_id_resp(struct msgb *msg) |
| 451 | { |
| 452 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 453 | struct gsm_lchan *lchan = msg->lchan; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 454 | u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK; |
| 455 | char mi_string[MI_SIZE]; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 456 | u_int32_t tmsi; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 457 | |
| 458 | 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] | 459 | DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n", |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 460 | mi_type, mi_string); |
| 461 | |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 462 | /* |
| 463 | * Rogue messages could trick us but so is life |
| 464 | */ |
| 465 | put_lchan(lchan); |
| 466 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 467 | switch (mi_type) { |
| 468 | case GSM_MI_TYPE_IMSI: |
| 469 | if (!lchan->subscr) |
| 470 | lchan->subscr = db_create_subscriber(mi_string); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 471 | if (lchan->loc_operation) |
| 472 | lchan->loc_operation->waiting_for_imsi = 0; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 473 | break; |
| 474 | case GSM_MI_TYPE_IMEI: |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 475 | case GSM_MI_TYPE_IMEISV: |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 476 | /* update subscribe <-> IMEI mapping */ |
| 477 | if (lchan->subscr) |
| 478 | db_subscriber_assoc_imei(lchan->subscr, mi_string); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 479 | if (lchan->loc_operation) |
| 480 | lchan->loc_operation->waiting_for_imei = 0; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 481 | break; |
| 482 | } |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 483 | |
| 484 | /* Check if we can let the mobile station enter */ |
| 485 | if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) { |
| 486 | db_subscriber_alloc_tmsi(lchan->subscr); |
| 487 | tmsi = strtoul(lchan->subscr->tmsi, NULL, 10); |
| 488 | release_loc_updating_req(lchan); |
| 489 | return gsm0408_loc_upd_acc(msg->lchan, tmsi); |
| 490 | } |
| 491 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 492 | return 0; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 495 | |
| 496 | static void loc_upd_rej_cb(void *data) |
| 497 | { |
| 498 | struct gsm_lchan *lchan = data; |
| 499 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 500 | release_loc_updating_req(lchan); |
Holger Freyther | e97f7fb | 2008-12-31 18:52:11 +0000 | [diff] [blame] | 501 | gsm0408_loc_upd_rej(lchan, reject_cause); |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 502 | lchan_auto_release(lchan); |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Holger Freyther | b7193e4 | 2008-12-29 17:44:08 +0000 | [diff] [blame] | 505 | static void schedule_reject(struct gsm_lchan *lchan) |
| 506 | { |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 507 | lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb; |
| 508 | lchan->loc_operation->updating_timer.data = lchan; |
| 509 | schedule_timer(&lchan->loc_operation->updating_timer, 5, 0); |
Holger Freyther | b7193e4 | 2008-12-29 17:44:08 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 512 | #define MI_SIZE 32 |
Harald Welte | bf5e8df | 2009-02-03 12:59:45 +0000 | [diff] [blame] | 513 | /* Chapter 9.2.15: Receive Location Updating Request */ |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 514 | static int mm_rx_loc_upd_req(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 515 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 516 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 517 | struct gsm_bts *bts = msg->trx->bts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 518 | struct gsm48_loc_upd_req *lu; |
| 519 | struct gsm_subscriber *subscr; |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 520 | struct gsm_lchan *lchan = msg->lchan; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 521 | u_int8_t mi_type; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 522 | u_int32_t tmsi; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 523 | char mi_string[MI_SIZE]; |
| 524 | int rc; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 525 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 526 | lu = (struct gsm48_loc_upd_req *) gh->data; |
| 527 | |
| 528 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 529 | |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 530 | mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
| 531 | |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 532 | 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] | 533 | |
| 534 | allocate_loc_updating_req(lchan); |
| 535 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 536 | switch (mi_type) { |
| 537 | case GSM_MI_TYPE_IMSI: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 538 | /* we always want the IMEI, too */ |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 539 | use_lchan(lchan); |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 540 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 541 | lchan->loc_operation->waiting_for_imei = 1; |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 542 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 543 | /* look up subscriber based on IMSI */ |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 544 | subscr = db_create_subscriber(mi_string); |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 545 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 546 | case GSM_MI_TYPE_TMSI: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 547 | /* we always want the IMEI, too */ |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 548 | use_lchan(lchan); |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 549 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 550 | lchan->loc_operation->waiting_for_imei = 1; |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 551 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 552 | /* look up the subscriber based on TMSI, request IMSI if it fails */ |
Harald Welte | ba4cf16 | 2009-01-10 01:49:35 +0000 | [diff] [blame] | 553 | subscr = subscr_get_by_tmsi(mi_string); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 554 | if (!subscr) { |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 555 | /* send IDENTITY REQUEST message to get IMSI */ |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 556 | use_lchan(lchan); |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 557 | rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI); |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 558 | lchan->loc_operation->waiting_for_imsi = 1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 559 | } |
| 560 | break; |
| 561 | case GSM_MI_TYPE_IMEI: |
| 562 | case GSM_MI_TYPE_IMEISV: |
| 563 | /* no sim card... FIXME: what to do ? */ |
| 564 | fprintf(stderr, "Unimplemented mobile identity type\n"); |
| 565 | break; |
| 566 | default: |
| 567 | fprintf(stderr, "Unknown mobile identity type\n"); |
| 568 | break; |
| 569 | } |
| 570 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 571 | lchan->subscr = subscr; |
| 572 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 573 | /* |
| 574 | * Schedule the reject timer and check if we can let the |
| 575 | * subscriber into our network immediately or if we need to wait |
| 576 | * for identity responses. |
| 577 | */ |
| 578 | schedule_reject(lchan); |
| 579 | if (!authorize_subscriber(lchan->loc_operation, subscr)) |
Harald Welte | e872cb1 | 2009-01-01 00:33:37 +0000 | [diff] [blame] | 580 | return 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 581 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 582 | db_subscriber_alloc_tmsi(subscr); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 583 | subscr_update(subscr, bts); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 584 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 585 | tmsi = strtoul(subscr->tmsi, NULL, 10); |
| 586 | |
Holger Freyther | 73487a2 | 2008-12-31 18:53:57 +0000 | [diff] [blame] | 587 | release_loc_updating_req(lchan); |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 588 | return gsm0408_loc_upd_acc(lchan, tmsi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 591 | /* 9.1.5 Channel mode modify */ |
| 592 | int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode) |
| 593 | { |
| 594 | struct msgb *msg = gsm48_msgb_alloc(); |
| 595 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 596 | struct gsm48_chan_mode_modify *cmm = |
| 597 | (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm)); |
| 598 | u_int16_t arfcn = lchan->ts->trx->arfcn; |
| 599 | |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 600 | DEBUGP(DRR, "-> CHANNEL MODE MODIFY\n"); |
| 601 | |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 602 | msg->lchan = lchan; |
| 603 | gh->proto_discr = GSM48_PDISC_RR; |
| 604 | gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF; |
| 605 | |
| 606 | /* fill the channel information element, this code |
| 607 | * should probably be shared with rsl_rx_chan_rqd() */ |
| 608 | cmm->chan_desc.chan_nr = lchan2chan_nr(lchan); |
| 609 | cmm->chan_desc.h0.h = 0; |
| 610 | cmm->chan_desc.h0.arfcn_high = arfcn >> 8; |
| 611 | cmm->chan_desc.h0.arfcn_low = arfcn & 0xff; |
| 612 | cmm->mode = mode; |
| 613 | |
| 614 | return gsm48_sendmsg(msg); |
| 615 | } |
| 616 | |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 617 | /* Section 9.2.15a */ |
| 618 | int gsm48_tx_mm_info(struct gsm_lchan *lchan) |
| 619 | { |
| 620 | struct msgb *msg = gsm48_msgb_alloc(); |
| 621 | struct gsm48_hdr *gh; |
| 622 | struct gsm_network *net = lchan->ts->trx->bts->network; |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 623 | u_int8_t *ptr8; |
| 624 | u_int16_t *ptr16; |
| 625 | int name_len; |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 626 | int i; |
| 627 | |
| 628 | msg->lchan = lchan; |
| 629 | |
| 630 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 631 | gh->proto_discr = GSM48_PDISC_MM; |
| 632 | gh->msg_type = GSM48_MT_MM_INFO; |
| 633 | |
| 634 | if (net->name_long) { |
| 635 | name_len = strlen(net->name_long); |
| 636 | /* 10.5.3.5a */ |
| 637 | ptr8 = msgb_put(msg, 3); |
| 638 | ptr8[0] = GSM48_IE_NAME_LONG; |
| 639 | ptr8[1] = name_len*2 +1; |
| 640 | ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */ |
| 641 | |
| 642 | ptr16 = (u_int16_t *) msgb_put(msg, name_len*2); |
| 643 | for (i = 0; i < name_len; i++) |
Harald Welte | 179f064 | 2008-12-31 23:59:18 +0000 | [diff] [blame] | 644 | ptr16[i] = htons(net->name_long[i]); |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 645 | |
| 646 | /* FIXME: Use Cell Broadcast, not UCS-2, since |
| 647 | * UCS-2 is only supported by later revisions of the spec */ |
| 648 | } |
| 649 | |
| 650 | if (net->name_short) { |
| 651 | name_len = strlen(net->name_short); |
| 652 | /* 10.5.3.5a */ |
| 653 | ptr8 = (u_int8_t *) msgb_put(msg, 3); |
| 654 | ptr8[0] = GSM48_IE_NAME_LONG; |
| 655 | ptr8[1] = name_len*2 + 1; |
| 656 | ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */ |
| 657 | |
Harald Welte | e872cb1 | 2009-01-01 00:33:37 +0000 | [diff] [blame] | 658 | ptr16 = (u_int16_t *) msgb_put(msg, name_len*2); |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 659 | for (i = 0; i < name_len; i++) |
Harald Welte | 179f064 | 2008-12-31 23:59:18 +0000 | [diff] [blame] | 660 | ptr16[i] = htons(net->name_short[i]); |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | #if 0 |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 664 | /* move back to the top */ |
| 665 | time_t cur_t; |
| 666 | struct tm* cur_time; |
| 667 | int tz15min; |
Harald Welte | db253af | 2008-12-30 17:56:55 +0000 | [diff] [blame] | 668 | /* Section 10.5.3.9 */ |
| 669 | cur_t = time(NULL); |
| 670 | cur_time = gmtime(cur_t); |
| 671 | ptr8 = msgb_put(msg, 8); |
| 672 | ptr8[0] = GSM48_IE_NET_TIME_TZ; |
| 673 | ptr8[1] = to_bcd8(cur_time->tm_year % 100); |
| 674 | ptr8[2] = to_bcd8(cur_time->tm_mon); |
| 675 | ptr8[3] = to_bcd8(cur_time->tm_mday); |
| 676 | ptr8[4] = to_bcd8(cur_time->tm_hour); |
| 677 | ptr8[5] = to_bcd8(cur_time->tm_min); |
| 678 | ptr8[6] = to_bcd8(cur_time->tm_sec); |
| 679 | /* 02.42: coded as BCD encoded signed value in units of 15 minutes */ |
| 680 | tz15min = (cur_time->tm_gmtoff)/(60*15); |
| 681 | ptr8[6] = to_bcd8(tz15min); |
| 682 | if (tz15min < 0) |
| 683 | ptr8[6] |= 0x80; |
| 684 | #endif |
| 685 | |
| 686 | return gsm48_sendmsg(msg); |
| 687 | } |
| 688 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 689 | static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan) |
| 690 | { |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 691 | DEBUGP(DMM, "-> CM SERVICE ACK\n"); |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 692 | 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] | 693 | } |
Harald Welte | ba4cf16 | 2009-01-10 01:49:35 +0000 | [diff] [blame] | 694 | |
| 695 | /* 9.2.6 CM service reject */ |
| 696 | static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan, |
| 697 | enum gsm48_reject_value value) |
| 698 | { |
| 699 | struct msgb *msg = gsm48_msgb_alloc(); |
| 700 | struct gsm48_hdr *gh; |
| 701 | |
| 702 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 703 | |
| 704 | msg->lchan = lchan; |
| 705 | use_lchan(lchan); |
| 706 | |
| 707 | gh->proto_discr = GSM48_PDISC_MM; |
| 708 | gh->msg_type = GSM48_MT_MM_CM_SERV_REJ; |
| 709 | gh->data[0] = value; |
| 710 | DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value); |
| 711 | |
| 712 | return gsm48_sendmsg(msg); |
| 713 | } |
| 714 | |
Harald Welte | 4ed0e92 | 2009-01-10 03:17:30 +0000 | [diff] [blame] | 715 | |
| 716 | /* |
| 717 | * Handle CM Service Requests |
| 718 | * a) Verify that the packet is long enough to contain the information |
| 719 | * we require otherwsie reject with INCORRECT_MESSAGE |
| 720 | * b) Try to parse the TMSI. If we do not have one reject |
| 721 | * c) Check that we know the subscriber with the TMSI otherwise reject |
| 722 | * with a HLR cause |
| 723 | * d) Set the subscriber on the gsm_lchan and accept |
| 724 | */ |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 725 | static int gsm48_rx_mm_serv_req(struct msgb *msg) |
| 726 | { |
Harald Welte | ba4cf16 | 2009-01-10 01:49:35 +0000 | [diff] [blame] | 727 | u_int8_t mi_type; |
Harald Welte | 4ed0e92 | 2009-01-10 03:17:30 +0000 | [diff] [blame] | 728 | char mi_string[MI_SIZE]; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 729 | |
Harald Welte | ba4cf16 | 2009-01-10 01:49:35 +0000 | [diff] [blame] | 730 | struct gsm_subscriber *subscr; |
| 731 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 732 | struct gsm48_service_request *req = |
| 733 | (struct gsm48_service_request *)gh->data; |
| 734 | |
| 735 | if (msg->data_len < sizeof(struct gsm48_service_request*)) { |
| 736 | DEBUGP(DMM, "<- CM SERVICE REQUEST wrong sized message\n"); |
| 737 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 738 | GSM48_REJECT_INCORRECT_MESSAGE); |
| 739 | } |
| 740 | |
| 741 | if (msg->data_len < req->mi_len + 6) { |
| 742 | DEBUGP(DMM, "<- CM SERVICE REQUEST MI does not fit in package\n"); |
| 743 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 744 | GSM48_REJECT_INCORRECT_MESSAGE); |
| 745 | } |
| 746 | |
| 747 | mi_type = req->mi[0] & GSM_MI_TYPE_MASK; |
| 748 | if (mi_type != GSM_MI_TYPE_TMSI) { |
| 749 | DEBUGP(DMM, "<- CM SERVICE REQUEST mi type is not TMSI: %d\n", mi_type); |
| 750 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 751 | GSM48_REJECT_INCORRECT_MESSAGE); |
| 752 | } |
| 753 | |
Harald Welte | 4ed0e92 | 2009-01-10 03:17:30 +0000 | [diff] [blame] | 754 | mi_to_string(mi_string, sizeof(mi_string), req->mi, req->mi_len); |
| 755 | subscr = subscr_get_by_tmsi(mi_string); |
| 756 | DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x mi_type=0x%02x M(%s)\n", |
| 757 | req->cm_service_type, mi_type, mi_string); |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 758 | |
Harald Welte | 4ed0e92 | 2009-01-10 03:17:30 +0000 | [diff] [blame] | 759 | if (!subscr) |
| 760 | return gsm48_tx_mm_serv_rej(msg->lchan, |
| 761 | GSM48_REJECT_IMSI_UNKNOWN_IN_HLR); |
| 762 | |
| 763 | if (!msg->lchan->subscr) |
| 764 | msg->lchan->subscr = subscr; |
Harald Welte | 9bb7c70 | 2009-01-10 03:21:41 +0000 | [diff] [blame] | 765 | else if (msg->lchan->subscr != subscr) { |
| 766 | DEBUGP(DMM, "<- CM Channel already owned by someone else?\n"); |
| 767 | subscr_put(subscr); |
| 768 | } |
| 769 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 770 | return gsm48_tx_mm_serv_ack(msg->lchan); |
| 771 | } |
| 772 | |
Harald Welte | bf5e8df | 2009-02-03 12:59:45 +0000 | [diff] [blame] | 773 | /* Receive a GSM 04.08 Mobility Management (MM) message */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 774 | static int gsm0408_rcv_mm(struct msgb *msg) |
| 775 | { |
| 776 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 777 | int rc; |
| 778 | |
| 779 | switch (gh->msg_type & 0xbf) { |
| 780 | case GSM48_MT_MM_LOC_UPD_REQUEST: |
Holger Freyther | 429e776 | 2008-12-30 13:28:30 +0000 | [diff] [blame] | 781 | DEBUGP(DMM, "LOCATION UPDATING REQUEST\n"); |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 782 | rc = mm_rx_loc_upd_req(msg); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 783 | break; |
| 784 | case GSM48_MT_MM_ID_RESP: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 785 | rc = mm_rx_id_resp(msg); |
| 786 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 787 | case GSM48_MT_MM_CM_SERV_REQ: |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 788 | rc = gsm48_rx_mm_serv_req(msg); |
| 789 | break; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 790 | case GSM48_MT_MM_STATUS: |
| 791 | DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n"); |
| 792 | break; |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 793 | case GSM48_MT_MM_TMSI_REALL_COMPL: |
Harald Welte | 69b2af2 | 2009-01-06 19:47:00 +0000 | [diff] [blame] | 794 | DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n", |
| 795 | msg->lchan->subscr ? |
| 796 | msg->lchan->subscr->imsi : |
| 797 | "unknown subscriber"); |
| 798 | break; |
| 799 | case GSM48_MT_MM_CM_REEST_REQ: |
Harald Welte | 231ad4f | 2008-12-27 11:15:38 +0000 | [diff] [blame] | 800 | case GSM48_MT_MM_AUTH_RESP: |
| 801 | case GSM48_MT_MM_IMSI_DETACH_IND: |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 802 | fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n", |
| 803 | gh->msg_type); |
| 804 | break; |
| 805 | default: |
| 806 | fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n", |
| 807 | gh->msg_type); |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | return rc; |
| 812 | } |
Harald Welte | bf5e8df | 2009-02-03 12:59:45 +0000 | [diff] [blame] | 813 | |
Harald Welte | 2d35ae6 | 2009-02-06 12:02:13 +0000 | [diff] [blame] | 814 | /* Receive a PAGING RESPONSE message from the MS */ |
| 815 | static int gsm48_rr_rx_pag_resp(struct msgb *msg) |
| 816 | { |
| 817 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 818 | struct gsm48_paging_response *pr = |
| 819 | (struct gsm48_paging_response *) gh->data; |
| 820 | u_int8_t mi_type = pr->mi[0] & GSM_MI_TYPE_MASK; |
| 821 | char mi_string[MI_SIZE]; |
| 822 | struct gsm_subscriber *subscr; |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 823 | struct paging_signal_data sig_data; |
Harald Welte | 2d35ae6 | 2009-02-06 12:02:13 +0000 | [diff] [blame] | 824 | int rc = 0; |
| 825 | |
| 826 | mi_to_string(mi_string, sizeof(mi_string), &pr->mi[0], pr->mi_len); |
| 827 | DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n", |
| 828 | mi_type, mi_string); |
| 829 | subscr = subscr_get_by_tmsi(mi_string); |
| 830 | |
| 831 | if (!subscr) { |
| 832 | DEBUGP(DRR, "<- Can't find any subscriber for this ID\n"); |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 833 | /* FIXME: request id? close channel? */ |
Harald Welte | 2d35ae6 | 2009-02-06 12:02:13 +0000 | [diff] [blame] | 834 | return -EINVAL; |
| 835 | } |
| 836 | DEBUGP(DRR, "<- Channel was requested by %s\n", |
| 837 | subscr->name ? subscr->name : subscr->imsi); |
Holger Freyther | 053e09d | 2009-02-14 22:51:06 +0000 | [diff] [blame] | 838 | |
Holger Freyther | 2fa4cb5 | 2009-02-14 23:53:15 +0000 | [diff] [blame] | 839 | if (!msg->lchan->subscr) |
| 840 | msg->lchan->subscr = subscr; |
| 841 | else if (msg->lchan->subscr != subscr) { |
| 842 | DEBUGP(DRR, "<- Channel already owned by someone else?\n"); |
| 843 | subscr_put(subscr); |
| 844 | } |
| 845 | |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 846 | sig_data.subscr = subscr; |
| 847 | sig_data.bts = msg->lchan->ts->trx->bts; |
| 848 | sig_data.lchan = msg->lchan; |
| 849 | |
| 850 | dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 851 | paging_request_stop(msg->trx->bts, subscr, msg->lchan); |
Harald Welte | 2d35ae6 | 2009-02-06 12:02:13 +0000 | [diff] [blame] | 852 | |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 853 | /* FIXME: somehow signal the completion of the PAGING to |
| 854 | * the entity that requested the paging */ |
| 855 | |
Harald Welte | 2d35ae6 | 2009-02-06 12:02:13 +0000 | [diff] [blame] | 856 | return rc; |
| 857 | } |
| 858 | |
Harald Welte | bf5e8df | 2009-02-03 12:59:45 +0000 | [diff] [blame] | 859 | /* Receive a GSM 04.08 Radio Resource (RR) message */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 860 | static int gsm0408_rcv_rr(struct msgb *msg) |
| 861 | { |
| 862 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 2d35ae6 | 2009-02-06 12:02:13 +0000 | [diff] [blame] | 863 | int rc = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 864 | |
| 865 | switch (gh->msg_type) { |
| 866 | case GSM48_MT_RR_CLSM_CHG: |
| 867 | DEBUGP(DRR, "CLASSMARK CHANGE\n"); |
| 868 | /* FIXME: what to do ?!? */ |
| 869 | break; |
Harald Welte | fc977a8 | 2008-12-27 10:19:37 +0000 | [diff] [blame] | 870 | case GSM48_MT_RR_GPRS_SUSP_REQ: |
| 871 | DEBUGP(DRR, "GRPS SUSPEND REQUEST\n"); |
| 872 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 873 | case GSM48_MT_RR_PAG_RESP: |
Harald Welte | 2d35ae6 | 2009-02-06 12:02:13 +0000 | [diff] [blame] | 874 | rc = gsm48_rr_rx_pag_resp(msg); |
| 875 | break; |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 876 | case GSM48_MT_RR_CHAN_MODE_MODIF_ACK: |
| 877 | DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n"); |
Harald Welte | 2c38aa8 | 2009-02-18 03:44:24 +0000 | [diff] [blame] | 878 | rc = rsl_chan_mode_modify_req(msg->lchan); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 879 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 880 | default: |
Harald Welte | 2d35ae6 | 2009-02-06 12:02:13 +0000 | [diff] [blame] | 881 | fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n", |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 882 | gh->msg_type); |
| 883 | break; |
| 884 | } |
| 885 | |
Harald Welte | 2d35ae6 | 2009-02-06 12:02:13 +0000 | [diff] [blame] | 886 | return rc; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Holger Freyther | e64a7a3 | 2009-02-06 21:55:37 +0000 | [diff] [blame] | 889 | /* 7.1.7 and 9.1.7 Channel release*/ |
| 890 | int gsm48_send_rr_release(struct gsm_lchan *lchan) |
| 891 | { |
| 892 | struct msgb *msg = gsm48_msgb_alloc(); |
| 893 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 894 | u_int8_t *cause; |
| 895 | |
| 896 | msg->lchan = lchan; |
| 897 | gh->proto_discr = GSM48_PDISC_RR; |
| 898 | gh->msg_type = GSM48_MT_RR_CHAN_REL; |
| 899 | |
| 900 | cause = msgb_put(msg, 1); |
| 901 | cause[0] = GSM48_RR_CAUSE_NORMAL; |
| 902 | |
| 903 | DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n", |
| 904 | lchan->nr, lchan->type); |
| 905 | |
| 906 | return gsm48_sendmsg(msg); |
| 907 | } |
| 908 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 909 | /* Call Control */ |
| 910 | |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 911 | /* The entire call control code is written in accordance with Figure 7.10c |
| 912 | * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE |
| 913 | * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY |
| 914 | * it for voice */ |
| 915 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 916 | static int gsm48_cc_tx_status(struct gsm_lchan *lchan) |
| 917 | { |
| 918 | struct msgb *msg = gsm48_msgb_alloc(); |
| 919 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 920 | u_int8_t *cause, *call_state; |
| 921 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 922 | gh->proto_discr = GSM48_PDISC_CC; |
| 923 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 924 | msg->lchan = lchan; |
| 925 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 926 | gh->msg_type = GSM48_MT_CC_STATUS; |
| 927 | |
| 928 | cause = msgb_put(msg, 3); |
| 929 | cause[0] = 2; |
| 930 | cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER; |
| 931 | cause[2] = 0x80 | 30; /* response to status inquiry */ |
| 932 | |
| 933 | call_state = msgb_put(msg, 1); |
| 934 | call_state[0] = 0xc0 | 0x00; |
| 935 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 936 | return gsm48_sendmsg(msg); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Harald Welte | 6f4b753 | 2008-12-29 00:39:37 +0000 | [diff] [blame] | 939 | static int gsm48_tx_simple(struct gsm_lchan *lchan, |
| 940 | u_int8_t pdisc, u_int8_t msg_type) |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 941 | { |
| 942 | struct msgb *msg = gsm48_msgb_alloc(); |
| 943 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 944 | |
| 945 | msg->lchan = lchan; |
| 946 | |
Harald Welte | 6f4b753 | 2008-12-29 00:39:37 +0000 | [diff] [blame] | 947 | gh->proto_discr = pdisc; |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 948 | gh->msg_type = msg_type; |
| 949 | |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 950 | return gsm48_sendmsg(msg); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 953 | /* call-back from paging the B-end of the connection */ |
| 954 | static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event, |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 955 | struct msgb *msg, void *_lchan, void *param) |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 956 | { |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 957 | struct gsm_lchan *lchan = _lchan; |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 958 | struct gsm_call *remote_call = param; |
| 959 | struct gsm_call *call = &lchan->call; |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 960 | int rc = 0; |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 961 | |
| 962 | if (hooknum != GSM_HOOK_RR_PAGING) |
| 963 | return -EINVAL; |
| 964 | |
| 965 | switch (event) { |
| 966 | case GSM_PAGING_SUCCEEDED: |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 967 | DEBUGP(DCC, "paging succeeded!\n"); |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 968 | remote_call->remote_lchan = lchan; |
| 969 | call->remote_lchan = remote_call->local_lchan; |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 970 | /* send SETUP request to called party */ |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 971 | rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr); |
| 972 | if (is_ipaccess_bts(lchan->ts->trx->bts)) |
| 973 | rsl_ipacc_bind(lchan); |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 974 | break; |
| 975 | case GSM_PAGING_EXPIRED: |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 976 | DEBUGP(DCC, "paging expired!\n"); |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 977 | /* notify caller that we cannot reach called party */ |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 978 | /* FIXME: correct cause, etc */ |
Harald Welte | bfaf097 | 2009-02-17 17:43:33 +0000 | [diff] [blame] | 979 | rc = gsm48_tx_simple(remote_call->local_lchan, GSM48_PDISC_CC, |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 980 | GSM48_MT_CC_RELEASE_COMPL); |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 981 | break; |
| 982 | } |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 983 | return rc; |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 986 | static int gsm48_cc_rx_status_enq(struct msgb *msg) |
| 987 | { |
| 988 | return gsm48_cc_tx_status(msg->lchan); |
| 989 | } |
| 990 | |
| 991 | static int gsm48_cc_rx_setup(struct msgb *msg) |
| 992 | { |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 993 | struct gsm_call *call = &msg->lchan->call; |
| 994 | struct gsm48_hdr *gh = msgb_l3(msg); |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 995 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 996 | struct gsm_subscriber *called_subscr; |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 997 | char called_number[(43-2)*2 + 1] = "\0"; |
| 998 | struct tlv_parsed tp; |
| 999 | u_int8_t num_type; |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1000 | int ret; |
| 1001 | |
| 1002 | if (call->state == GSM_CSTATE_NULL || |
| 1003 | call->state == GSM_CSTATE_RELEASE_REQ) |
| 1004 | use_lchan(msg->lchan); |
| 1005 | |
| 1006 | call->type = GSM_CT_MO; |
| 1007 | call->state = GSM_CSTATE_INITIATED; |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1008 | call->local_lchan = msg->lchan; |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1009 | call->transaction_id = gh->proto_discr & 0xf0; |
| 1010 | |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 1011 | tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len); |
| 1012 | if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) |
| 1013 | goto err; |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1014 | |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 1015 | /* Parse the number that was dialed and lookup subscriber */ |
| 1016 | num_type = decode_bcd_number(called_number, sizeof(called_number), |
| 1017 | TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1018 | |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1019 | DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id, |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1020 | called_number); |
| 1021 | |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 1022 | called_subscr = subscr_get_by_extension(called_number); |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1023 | if (!called_subscr) { |
| 1024 | DEBUGP(DCC, "could not find subscriber, RELEASE\n"); |
| 1025 | put_lchan(msg->lchan); |
| 1026 | return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1027 | GSM48_MT_CC_RELEASE_COMPL); |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 1030 | subscr_get(msg->lchan->subscr); |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 1031 | call->called_subscr = called_subscr; |
| 1032 | |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1033 | /* start paging of the receiving end of the call */ |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 1034 | paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F, |
| 1035 | setup_trig_pag_evt, call); |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1036 | |
| 1037 | /* send a CALL PROCEEDING message to the MO */ |
| 1038 | ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1039 | GSM48_MT_CC_CALL_PROC); |
| 1040 | |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1041 | if (is_ipaccess_bts(msg->trx->bts)) |
| 1042 | rsl_ipacc_bind(msg->lchan); |
| 1043 | |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1044 | /* change TCH/F mode to voice */ |
Harald Welte | 2c38aa8 | 2009-02-18 03:44:24 +0000 | [diff] [blame] | 1045 | return gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR); |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 1046 | |
| 1047 | err: |
| 1048 | /* FIXME: send some kind of RELEASE */ |
| 1049 | return 0; |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1050 | } |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1051 | |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1052 | static int gsm48_cc_rx_alerting(struct msgb *msg) |
| 1053 | { |
| 1054 | struct gsm_call *call = &msg->lchan->call; |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1055 | |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1056 | DEBUGP(DCC, "A -> ALERTING\n"); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1057 | |
| 1058 | /* forward ALERTING to other party */ |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1059 | if (!call->remote_lchan) |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1060 | return -EIO; |
| 1061 | |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1062 | DEBUGP(DCC, "B <- ALERTING\n"); |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1063 | return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC, |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1064 | GSM48_MT_CC_ALERTING); |
| 1065 | } |
| 1066 | |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1067 | /* map two ipaccess RTP streams onto each other */ |
| 1068 | static int ipacc_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan) |
| 1069 | { |
| 1070 | struct gsm_bts_trx_ts *ts; |
| 1071 | |
| 1072 | ts = remote_lchan->ts; |
| 1073 | rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port, |
| 1074 | lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc); |
| 1075 | |
| 1076 | ts = lchan->ts; |
| 1077 | rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port, |
| 1078 | remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc); |
| 1079 | |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1083 | static int gsm48_cc_rx_connect(struct msgb *msg) |
| 1084 | { |
| 1085 | struct gsm_call *call = &msg->lchan->call; |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1086 | int rc; |
| 1087 | |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1088 | DEBUGP(DCC, "A -> CONNECT\n"); |
| 1089 | DEBUGP(DCC, "A <- CONNECT ACK\n"); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1090 | /* MT+MO: need to respond with CONNECT_ACK and pass on */ |
| 1091 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1092 | GSM48_MT_CC_CONNECT_ACK); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1093 | |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1094 | if (!call->remote_lchan) |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1095 | return -EIO; |
| 1096 | |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1097 | if (is_ipaccess_bts(msg->trx->bts)) |
| 1098 | ipacc_map(msg->lchan, call->remote_lchan); |
| 1099 | |
| 1100 | /* forward CONNECT to other party */ |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1101 | DEBUGP(DCC, "B <- CONNECT\n"); |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1102 | return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC, |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1103 | GSM48_MT_CC_CONNECT); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | static int gsm48_cc_rx_disconnect(struct msgb *msg) |
| 1107 | { |
| 1108 | struct gsm_call *call = &msg->lchan->call; |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1109 | int rc; |
| 1110 | |
| 1111 | |
| 1112 | /* Section 5.4.3.2 */ |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1113 | DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n"); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1114 | call->state = GSM_CSTATE_RELEASE_REQ; |
| 1115 | if (call->state != GSM_CSTATE_NULL) |
| 1116 | put_lchan(msg->lchan); |
| 1117 | /* FIXME: clear the network connection */ |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1118 | DEBUGP(DCC, "A <- RELEASE\n"); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1119 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1120 | GSM48_MT_CC_RELEASE); |
| 1121 | |
| 1122 | /* forward DISCONNECT to other party */ |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1123 | if (!call->remote_lchan) |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1124 | return -EIO; |
| 1125 | |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1126 | DEBUGP(DCC, "B <- DISCONNECT\n"); |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1127 | return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC, |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1128 | GSM48_MT_CC_DISCONNECT); |
| 1129 | } |
| 1130 | |
| 1131 | static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 }; |
| 1132 | |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1133 | int gsm48_cc_tx_setup(struct gsm_lchan *lchan, |
| 1134 | struct gsm_subscriber *calling_subscr) |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 1135 | { |
| 1136 | struct msgb *msg = gsm48_msgb_alloc(); |
| 1137 | struct gsm48_hdr *gh; |
| 1138 | struct gsm_call *call = &lchan->call; |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1139 | u_int8_t bcd_lv[19]; |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 1140 | |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1141 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 1142 | |
| 1143 | call->type = GSM_CT_MT; |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1144 | |
| 1145 | call->local_lchan = msg->lchan = lchan; |
Harald Welte | 498b0bb | 2009-01-09 21:27:43 +0000 | [diff] [blame] | 1146 | use_lchan(lchan); |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 1147 | |
| 1148 | gh->proto_discr = GSM48_PDISC_CC; |
| 1149 | gh->msg_type = GSM48_MT_CC_SETUP; |
Harald Welte | 09e38af | 2009-02-16 22:52:23 +0000 | [diff] [blame] | 1150 | |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1151 | msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE); |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1152 | if (calling_subscr) { |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1153 | encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9, |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1154 | calling_subscr->extension); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1155 | msgb_tlv_put(msg, GSM48_IE_CALLING_BCD, |
| 1156 | bcd_lv[0], bcd_lv+1); |
| 1157 | } |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1158 | if (lchan->subscr) { |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1159 | encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9, |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1160 | lchan->subscr->extension); |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1161 | msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, |
| 1162 | bcd_lv[0], bcd_lv+1); |
| 1163 | } |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 1164 | |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1165 | DEBUGP(DCC, "B <- SETUP\n"); |
Harald Welte | 65e74cc | 2008-12-29 01:55:35 +0000 | [diff] [blame] | 1166 | |
| 1167 | return gsm48_sendmsg(msg); |
| 1168 | } |
| 1169 | |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1170 | static int gsm0408_rcv_cc(struct msgb *msg) |
| 1171 | { |
| 1172 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1173 | u_int8_t msg_type = gh->msg_type & 0xbf; |
| 1174 | struct gsm_call *call = &msg->lchan->call; |
| 1175 | int rc = 0; |
| 1176 | |
| 1177 | switch (msg_type) { |
| 1178 | case GSM48_MT_CC_CALL_CONF: |
| 1179 | /* Response to SETUP */ |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1180 | DEBUGP(DCC, "-> CALL CONFIRM\n"); |
Harald Welte | 4b45815 | 2009-02-17 13:23:06 +0000 | [diff] [blame] | 1181 | /* we now need to MODIFY the channel */ |
Harald Welte | 2c38aa8 | 2009-02-18 03:44:24 +0000 | [diff] [blame] | 1182 | rc = gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1183 | break; |
| 1184 | case GSM48_MT_CC_RELEASE_COMPL: |
| 1185 | /* Answer from MS to RELEASE */ |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1186 | DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n"); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1187 | call->state = GSM_CSTATE_NULL; |
| 1188 | break; |
| 1189 | case GSM48_MT_CC_ALERTING: |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1190 | rc = gsm48_cc_rx_alerting(msg); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1191 | break; |
| 1192 | case GSM48_MT_CC_CONNECT: |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1193 | rc = gsm48_cc_rx_connect(msg); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1194 | break; |
| 1195 | case GSM48_MT_CC_CONNECT_ACK: |
| 1196 | /* MO: Answer to CONNECT */ |
| 1197 | call->state = GSM_CSTATE_ACTIVE; |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1198 | DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n"); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1199 | break; |
| 1200 | case GSM48_MT_CC_RELEASE: |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1201 | DEBUGP(DCC, "-> RELEASE\n"); |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1202 | DEBUGP(DCC, "<- RELEASE_COMPLETE\n"); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1203 | /* need to respond with RELEASE_COMPLETE */ |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1204 | rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC, |
| 1205 | GSM48_MT_CC_RELEASE_COMPL); |
Harald Welte | 49f48b8 | 2009-02-17 15:29:33 +0000 | [diff] [blame] | 1206 | put_lchan(msg->lchan); |
| 1207 | call->state = GSM_CSTATE_NULL; |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1208 | break; |
| 1209 | case GSM48_MT_CC_STATUS_ENQ: |
| 1210 | rc = gsm48_cc_rx_status_enq(msg); |
| 1211 | break; |
| 1212 | case GSM48_MT_CC_DISCONNECT: |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1213 | rc = gsm48_cc_rx_disconnect(msg); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1214 | break; |
| 1215 | case GSM48_MT_CC_SETUP: |
Harald Welte | 7584aea | 2009-02-11 11:44:12 +0000 | [diff] [blame] | 1216 | rc = gsm48_cc_rx_setup(msg); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1217 | break; |
| 1218 | case GSM48_MT_CC_EMERG_SETUP: |
Harald Welte | 7ccf778 | 2009-02-17 01:43:01 +0000 | [diff] [blame] | 1219 | DEBUGP(DCC, "-> EMERGENCY SETUP\n"); |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1220 | /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */ |
| 1221 | break; |
| 1222 | default: |
Harald Welte | 2d35ae6 | 2009-02-06 12:02:13 +0000 | [diff] [blame] | 1223 | fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n", |
Harald Welte | 4bc90a1 | 2008-12-27 16:32:52 +0000 | [diff] [blame] | 1224 | msg_type); |
| 1225 | break; |
| 1226 | } |
| 1227 | |
| 1228 | return rc; |
| 1229 | } |
| 1230 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 1231 | /* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */ |
| 1232 | int gsm0408_rcvmsg(struct msgb *msg) |
| 1233 | { |
| 1234 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1235 | u_int8_t pdisc = gh->proto_discr & 0x0f; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 1236 | int rc = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 1237 | |
| 1238 | switch (pdisc) { |
| 1239 | case GSM48_PDISC_CC: |
| 1240 | rc = gsm0408_rcv_cc(msg); |
| 1241 | break; |
| 1242 | case GSM48_PDISC_MM: |
| 1243 | rc = gsm0408_rcv_mm(msg); |
| 1244 | break; |
| 1245 | case GSM48_PDISC_RR: |
| 1246 | rc = gsm0408_rcv_rr(msg); |
| 1247 | break; |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 1248 | case GSM48_PDISC_SMS: |
Daniel Willmann | 8b3390e | 2008-12-28 00:31:09 +0000 | [diff] [blame] | 1249 | rc = gsm0411_rcv_sms(msg); |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 1250 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 1251 | case GSM48_PDISC_MM_GPRS: |
Harald Welte | bcae43f | 2008-12-27 21:45:37 +0000 | [diff] [blame] | 1252 | case GSM48_PDISC_SM_GPRS: |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 1253 | fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n", |
| 1254 | pdisc); |
| 1255 | break; |
| 1256 | default: |
| 1257 | fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n", |
| 1258 | pdisc); |
| 1259 | break; |
| 1260 | } |
| 1261 | |
| 1262 | return rc; |
| 1263 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 1264 | |
| 1265 | enum chreq_type { |
| 1266 | CHREQ_T_EMERG_CALL, |
| 1267 | CHREQ_T_CALL_REEST_TCH_F, |
| 1268 | CHREQ_T_CALL_REEST_TCH_H, |
| 1269 | CHREQ_T_CALL_REEST_TCH_H_DBL, |
| 1270 | CHREQ_T_SDCCH, |
| 1271 | CHREQ_T_TCH_F, |
| 1272 | CHREQ_T_VOICE_CALL_TCH_H, |
| 1273 | CHREQ_T_DATA_CALL_TCH_H, |
| 1274 | CHREQ_T_LOCATION_UPD, |
| 1275 | CHREQ_T_PAG_R_ANY, |
| 1276 | CHREQ_T_PAG_R_TCH_F, |
| 1277 | CHREQ_T_PAG_R_TCH_FH, |
| 1278 | }; |
| 1279 | |
| 1280 | /* Section 9.1.8 / Table 9.9 */ |
| 1281 | struct chreq { |
| 1282 | u_int8_t val; |
| 1283 | u_int8_t mask; |
| 1284 | enum chreq_type type; |
| 1285 | }; |
| 1286 | |
| 1287 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */ |
| 1288 | static const struct chreq chreq_type_neci1[] = { |
| 1289 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 1290 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F }, |
| 1291 | { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H }, |
| 1292 | { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL }, |
| 1293 | { 0xe0, 0xe0, CHREQ_T_SDCCH }, |
| 1294 | { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H }, |
| 1295 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 1296 | { 0x00, 0xf0, CHREQ_T_LOCATION_UPD }, |
| 1297 | { 0x10, 0xf0, CHREQ_T_SDCCH }, |
| 1298 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 1299 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 1300 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 1301 | }; |
| 1302 | |
| 1303 | /* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */ |
| 1304 | static const struct chreq chreq_type_neci0[] = { |
| 1305 | { 0xa0, 0xe0, CHREQ_T_EMERG_CALL }, |
| 1306 | { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H }, |
| 1307 | { 0xe0, 0xe0, CHREQ_T_TCH_F }, |
| 1308 | { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H }, |
| 1309 | { 0x00, 0xe0, CHREQ_T_LOCATION_UPD }, |
| 1310 | { 0x80, 0xe0, CHREQ_T_PAG_R_ANY }, |
| 1311 | { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F }, |
| 1312 | { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH }, |
| 1313 | }; |
| 1314 | |
| 1315 | static const enum gsm_chan_t ctype_by_chreq[] = { |
| 1316 | [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F, |
| 1317 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F, |
| 1318 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H, |
| 1319 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H, |
| 1320 | [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH, |
| 1321 | [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F, |
| 1322 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 1323 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H, |
| 1324 | [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH, |
| 1325 | [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH, |
| 1326 | [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F, |
| 1327 | [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F, |
| 1328 | }; |
| 1329 | |
Harald Welte | e14a57c | 2008-12-29 04:08:28 +0000 | [diff] [blame] | 1330 | static const enum gsm_chreq_reason_t reason_by_chreq[] = { |
| 1331 | [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG, |
| 1332 | [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL, |
| 1333 | [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL, |
| 1334 | [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL, |
| 1335 | [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER, |
| 1336 | [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER, |
| 1337 | [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 1338 | [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER, |
| 1339 | [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD, |
| 1340 | [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG, |
| 1341 | [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG, |
| 1342 | [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG, |
| 1343 | }; |
| 1344 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 1345 | enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 1346 | { |
| 1347 | int i; |
| 1348 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 1349 | |
| 1350 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 1351 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 1352 | if ((ra & chr->mask) == chr->val) |
| 1353 | return ctype_by_chreq[chr->type]; |
| 1354 | } |
| 1355 | fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra); |
| 1356 | return GSM_LCHAN_SDCCH; |
| 1357 | } |
Harald Welte | e14a57c | 2008-12-29 04:08:28 +0000 | [diff] [blame] | 1358 | |
| 1359 | enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra) |
| 1360 | { |
| 1361 | int i; |
| 1362 | /* FIXME: determine if we set NECI = 0 in the BTS SI4 */ |
| 1363 | |
| 1364 | for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) { |
| 1365 | const struct chreq *chr = &chreq_type_neci0[i]; |
| 1366 | if ((ra & chr->mask) == chr->val) |
| 1367 | return reason_by_chreq[chr->type]; |
| 1368 | } |
| 1369 | fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra); |
| 1370 | return GSM_CHREQ_REASON_OTHER; |
| 1371 | } |