blob: 85af5020e1f09ebcce7260e52d27500a5e8bb53f [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* 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 Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * 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 Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/msgb.h>
Harald Welte7584aea2009-02-11 11:44:12 +000035#include <openbsc/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
41#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000042#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000043#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000044#include <openbsc/signal.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000045#include <openbsc/trau_mux.h>
Harald Welte52b1f982008-12-23 20:25:15 +000046
Harald Welte8470bf22008-12-25 23:28:35 +000047#define GSM48_ALLOC_SIZE 1024
48#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000049
Harald Welte09e38af2009-02-16 22:52:23 +000050static const struct tlv_definition rsl_att_tlvdef = {
51 .def = {
52 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
53 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
54 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
55 [GSM48_IE_UTC] = { TLV_TYPE_TV },
56 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
57 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
58
59 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
60 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
61 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
62 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
63 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
64 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
65 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
66 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
67 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
68 [GSM48_IE_CONN_NUM] = { TLV_TYPE_TLV },
69 [GSM48_IE_CONN_SUBADDR] = { TLV_TYPE_TLV },
70 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
71 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
72 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
73 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
74 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
75 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
76 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
77 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
78 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
79 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
80 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
81 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
82 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
83 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
84 /* FIXME: more elements */
85 },
86};
87
Harald Welte65e74cc2008-12-29 01:55:35 +000088static int gsm48_tx_simple(struct gsm_lchan *lchan,
89 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +000090static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +000091
Harald Welte52b1f982008-12-23 20:25:15 +000092struct gsm_lai {
93 u_int16_t mcc;
94 u_int16_t mnc;
95 u_int16_t lac;
96};
97
Holger Freyther89824fc2008-12-30 16:18:18 +000098static int authorize_everonye = 0;
99void gsm0408_allow_everyone(int everyone)
100{
101 printf("Allowing everyone?\n");
102 authorize_everonye = everyone;
103}
104
Holger Freythere97f7fb2008-12-31 18:52:11 +0000105static int reject_cause = 0;
106void gsm0408_set_reject_cause(int cause)
107{
108 reject_cause = cause;
109}
110
Holger Freyther73487a22008-12-31 18:53:57 +0000111static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
112 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000113{
114 if (!subscriber)
115 return 0;
116
Holger Freyther73487a22008-12-31 18:53:57 +0000117 /*
118 * Do not send accept yet as more information should arrive. Some
119 * phones will not send us the information and we will have to check
120 * what we want to do with that.
121 */
122 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
123 return 0;
124
Holger Freyther89824fc2008-12-30 16:18:18 +0000125 if (authorize_everonye)
126 return 1;
127
128 return subscriber->authorized;
129}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000130
Holger Freyther73487a22008-12-31 18:53:57 +0000131static void release_loc_updating_req(struct gsm_lchan *lchan)
132{
Harald Welte179f0642008-12-31 23:59:18 +0000133 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000134 return;
135
136 del_timer(&lchan->loc_operation->updating_timer);
137 free(lchan->loc_operation);
138 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000139 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000140}
141
142static void allocate_loc_updating_req(struct gsm_lchan *lchan)
143{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000144 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000145 release_loc_updating_req(lchan);
146
147 lchan->loc_operation = (struct gsm_loc_updating_operation *)
148 malloc(sizeof(*lchan->loc_operation));
149 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
150}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000151
Harald Welte52b1f982008-12-23 20:25:15 +0000152static void to_bcd(u_int8_t *bcd, u_int16_t val)
153{
Harald Welte4b634542008-12-27 01:55:51 +0000154 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000155 val = val / 10;
156 bcd[1] = val % 10;
157 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000158 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000159 val = val / 10;
160}
161
Holger Freyther17746612008-12-28 16:32:44 +0000162void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000163 u_int16_t mnc, u_int16_t lac)
164{
165 u_int8_t bcd[3];
166
167 to_bcd(bcd, mcc);
168 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
169 lai48->digits[1] = bcd[2];
170
171 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000172 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
173#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000174 lai48->digits[1] |= bcd[2] << 4;
175 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000176#else
177 lai48->digits[1] |= 0xf << 4;
178 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
179#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000180
Harald Welte4b634542008-12-27 01:55:51 +0000181 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000182}
183
Harald Welte255539c2008-12-28 02:26:27 +0000184#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000185#define MID_TMSI_LEN (TMSI_LEN + 2)
186
Harald Welte255539c2008-12-28 02:26:27 +0000187int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000188{
Harald Welte65e74cc2008-12-29 01:55:35 +0000189 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000190
Harald Welte4b634542008-12-27 01:55:51 +0000191 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000192 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000193 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000194 *tptr = htonl(tmsi);
195
196 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000197}
198
Harald Welte09e38af2009-02-16 22:52:23 +0000199static const char bcd_num_digits[] = {
200 '0', '1', '2', '3', '4', '5', '6', '7',
201 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
202};
203
204/* decode a 'called party BCD number' as in 10.5.4.7 */
205u_int8_t decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv)
206{
207 u_int8_t in_len = bcd_lv[0];
208 int i;
209
210 if (in_len < 1)
211 return 0;
212
213 for (i = 2; i <= in_len; i++) {
214 /* lower nibble */
215 output_len--;
216 if (output_len <= 1)
217 break;
218 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
219
220 /* higher nibble */
221 output_len--;
222 if (output_len <= 1)
223 break;
224 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
225 }
226 if (output_len >= 1)
227 *output++ = '\0';
228
229 /* return number type / calling plan */
230 return bcd_lv[1] & 0x3f;
231}
232
233/* convert a single ASCII character to call-control BCD */
234static int asc_to_bcd(const char asc)
235{
236 int i;
237
238 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
239 if (bcd_num_digits[i] == asc)
240 return i;
241 }
242 return -EINVAL;
243}
244
245/* convert a ASCII phone number to 'called party BCD number' */
246int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
247 u_int8_t type, const char *input)
248{
249 int in_len = strlen(input);
250 int i;
251 u_int8_t *bcd_cur = bcd_lv + 2;
252
253 if (in_len/2 + 1 > max_len)
254 return -EIO;
255
256 /* two digits per byte, plus type byte */
257 bcd_lv[0] = in_len/2 + 1;
258 if (in_len % 2)
259 bcd_lv[0]++;
260
261 /* if the caller wants to create a valid 'calling party BCD
262 * number', then the extension bit MUST NOT be set. For the
263 * 'called party BCD number' it MUST be set. *sigh */
264 bcd_lv[1] = type;
265
266 for (i = 0; i < in_len; i++) {
267 int rc = asc_to_bcd(input[i]);
268 if (rc < 0)
269 return rc;
270 if (i % 2 == 0)
271 *bcd_cur = rc;
272 else
273 *bcd_cur++ |= (rc << 4);
274 }
275 /* append padding nibble in case of odd length */
276 if (i % 2)
277 *bcd_cur++ |= 0xf0;
278
279 /* return how many bytes we used */
280 return (bcd_cur - bcd_lv);
281}
282
Holger Freyther819dd202009-01-04 03:52:50 +0000283struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000284{
285 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
286}
287
Holger Freyther3e2c3232009-01-04 03:55:31 +0000288int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000289{
Harald Welte65e74cc2008-12-29 01:55:35 +0000290 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
291
292 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000293 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000294
Harald Welte65e74cc2008-12-29 01:55:35 +0000295 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
296 /* Send a 04.08 call control message, add transaction
297 * ID and TI flag */
298 gh->proto_discr |= msg->lchan->call.transaction_id;
299
300 /* GSM 04.07 Section 11.2.3.1.3 */
301 switch (msg->lchan->call.type) {
302 case GSM_CT_MO:
303 gh->proto_discr |= 0x80;
304 break;
305 case GSM_CT_MT:
306 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000307 case GSM_CT_NONE:
308 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000309 }
310 }
311 }
312
Harald Welte4b634542008-12-27 01:55:51 +0000313 msg->l3h = msg->data;
314
Harald Welte8470bf22008-12-25 23:28:35 +0000315 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000316}
317
Harald Welte52b1f982008-12-23 20:25:15 +0000318
Holger Freyther429e7762008-12-30 13:28:30 +0000319/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000320int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000321{
Harald Welte8470bf22008-12-25 23:28:35 +0000322 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000323 struct gsm48_hdr *gh;
324
Harald Welte8470bf22008-12-25 23:28:35 +0000325 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000326
327 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
328 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000329 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000330 gh->data[0] = cause;
331
Harald Weltedb253af2008-12-30 17:56:55 +0000332 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
333
Harald Welte65e74cc2008-12-29 01:55:35 +0000334 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000335}
336
337/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000338int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000339{
Harald Welte8470bf22008-12-25 23:28:35 +0000340 struct gsm_bts *bts = lchan->ts->trx->bts;
341 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000342 struct gsm48_hdr *gh;
343 struct gsm48_loc_area_id *lai;
344 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000345 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000346
Harald Welte8470bf22008-12-25 23:28:35 +0000347 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000348
349 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
350 gh->proto_discr = GSM48_PDISC_MM;
351 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
352
353 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000354 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000355 bts->network->network_code, bts->location_area_code);
356
357 mid = msgb_put(msg, MID_TMSI_LEN);
358 generate_mid_from_tmsi(mid, tmsi);
359
360 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
361
Harald Weltedb253af2008-12-30 17:56:55 +0000362 ret = gsm48_sendmsg(msg);
363
Harald Weltedb253af2008-12-30 17:56:55 +0000364 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000365
Holger Freyther07cc8d82008-12-29 06:23:46 +0000366 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000367}
368
Harald Weltefc977a82008-12-27 10:19:37 +0000369static char bcd2char(u_int8_t bcd)
370{
371 if (bcd < 0xa)
372 return '0' + bcd;
373 else
374 return 'A' + (bcd - 0xa);
375}
376
Harald Weltebf5e8df2009-02-03 12:59:45 +0000377/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000378static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
379{
380 int i;
381 u_int8_t mi_type;
382 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000383 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000384
385 mi_type = mi[0] & GSM_MI_TYPE_MASK;
386
387 switch (mi_type) {
388 case GSM_MI_TYPE_NONE:
389 break;
390 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000391 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
392 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
393 memcpy(&tmsi, &mi[1], 4);
394 tmsi = ntohl(tmsi);
395 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000396 }
397 break;
398 case GSM_MI_TYPE_IMSI:
399 case GSM_MI_TYPE_IMEI:
400 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000401 *str_cur++ = bcd2char(mi[0] >> 4);
402
403 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000404 if (str_cur + 2 >= string + str_len)
405 return str_cur - string;
406 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000407 /* skip last nibble in last input byte when GSM_EVEN */
408 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
409 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000410 }
411 break;
412 default:
413 break;
414 }
Harald Weltefc977a82008-12-27 10:19:37 +0000415 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000416
Harald Weltefc977a82008-12-27 10:19:37 +0000417 return str_cur - string;
418}
419
Harald Weltebf5e8df2009-02-03 12:59:45 +0000420/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000421static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
422{
423 struct msgb *msg = gsm48_msgb_alloc();
424 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000425
Harald Welte231ad4f2008-12-27 11:15:38 +0000426 msg->lchan = lchan;
427
428 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
429 gh->proto_discr = GSM48_PDISC_MM;
430 gh->msg_type = GSM48_MT_MM_ID_REQ;
431 gh->data[0] = id_type;
432
Harald Welte65e74cc2008-12-29 01:55:35 +0000433 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000434}
435
436#define MI_SIZE 32
437
Harald Weltebf5e8df2009-02-03 12:59:45 +0000438/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000439static int mm_rx_id_resp(struct msgb *msg)
440{
441 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000442 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000443 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
444 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000445 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000446
447 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000448 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000449 mi_type, mi_string);
450
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000451 /*
452 * Rogue messages could trick us but so is life
453 */
454 put_lchan(lchan);
455
Harald Welte75a983f2008-12-27 21:34:06 +0000456 switch (mi_type) {
457 case GSM_MI_TYPE_IMSI:
458 if (!lchan->subscr)
459 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000460 if (lchan->loc_operation)
461 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000462 break;
463 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000464 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000465 /* update subscribe <-> IMEI mapping */
466 if (lchan->subscr)
467 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000468 if (lchan->loc_operation)
469 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000470 break;
471 }
Holger Freyther73487a22008-12-31 18:53:57 +0000472
473 /* Check if we can let the mobile station enter */
474 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
475 db_subscriber_alloc_tmsi(lchan->subscr);
476 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
477 release_loc_updating_req(lchan);
478 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
479 }
480
Harald Welte75a983f2008-12-27 21:34:06 +0000481 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000482}
483
Harald Welte255539c2008-12-28 02:26:27 +0000484
485static void loc_upd_rej_cb(void *data)
486{
487 struct gsm_lchan *lchan = data;
488
Holger Freyther73487a22008-12-31 18:53:57 +0000489 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000490 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000491 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000492}
493
Holger Freytherb7193e42008-12-29 17:44:08 +0000494static void schedule_reject(struct gsm_lchan *lchan)
495{
Holger Freyther73487a22008-12-31 18:53:57 +0000496 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
497 lchan->loc_operation->updating_timer.data = lchan;
498 schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000499}
500
Harald Welte2a139372009-02-22 21:14:55 +0000501static const char *lupd_name(u_int8_t type)
502{
503 switch (type) {
504 case GSM48_LUPD_NORMAL:
505 return "NORMAL";
506 case GSM48_LUPD_PERIODIC:
507 return "PEROIDOC";
508 case GSM48_LUPD_IMSI_ATT:
509 return "IMSI ATTACH";
510 default:
511 return "UNKNOWN";
512 }
513}
514
Harald Welte231ad4f2008-12-27 11:15:38 +0000515#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000516/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000517static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000518{
Harald Welte8470bf22008-12-25 23:28:35 +0000519 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000520 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000521 struct gsm48_loc_upd_req *lu;
522 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000523 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000524 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000525 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000526 char mi_string[MI_SIZE];
527 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000528
Harald Welte8470bf22008-12-25 23:28:35 +0000529 lu = (struct gsm48_loc_upd_req *) gh->data;
530
531 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000532
Harald Weltefc977a82008-12-27 10:19:37 +0000533 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
534
Harald Welte2a139372009-02-22 21:14:55 +0000535 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
536 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000537
538 allocate_loc_updating_req(lchan);
539
Harald Welte52b1f982008-12-23 20:25:15 +0000540 switch (mi_type) {
541 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000542 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000543 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +0000544 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000545 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000546
Harald Welte52b1f982008-12-23 20:25:15 +0000547 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000548 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000549 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000550 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000551 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000552 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +0000553 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000554 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000555
Harald Welte52b1f982008-12-23 20:25:15 +0000556 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000557 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000558 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000559 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000560 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000561 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000562 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000563 }
564 break;
565 case GSM_MI_TYPE_IMEI:
566 case GSM_MI_TYPE_IMEISV:
567 /* no sim card... FIXME: what to do ? */
Harald Welte2a139372009-02-22 21:14:55 +0000568 DEBUGP(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000569 break;
570 default:
Harald Welte2a139372009-02-22 21:14:55 +0000571 DEBUGP(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000572 break;
573 }
574
Harald Welte255539c2008-12-28 02:26:27 +0000575 lchan->subscr = subscr;
576
Holger Freyther73487a22008-12-31 18:53:57 +0000577 /*
578 * Schedule the reject timer and check if we can let the
579 * subscriber into our network immediately or if we need to wait
580 * for identity responses.
581 */
582 schedule_reject(lchan);
583 if (!authorize_subscriber(lchan->loc_operation, subscr))
Harald Weltee872cb12009-01-01 00:33:37 +0000584 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000585
Harald Welte75a983f2008-12-27 21:34:06 +0000586 db_subscriber_alloc_tmsi(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000587 subscr_update(subscr, bts, GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welte8470bf22008-12-25 23:28:35 +0000588
Harald Welte255539c2008-12-28 02:26:27 +0000589 tmsi = strtoul(subscr->tmsi, NULL, 10);
590
Holger Freyther73487a22008-12-31 18:53:57 +0000591 release_loc_updating_req(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000592 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000593}
594
Harald Welte7584aea2009-02-11 11:44:12 +0000595/* 9.1.5 Channel mode modify */
596int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
597{
598 struct msgb *msg = gsm48_msgb_alloc();
599 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
600 struct gsm48_chan_mode_modify *cmm =
601 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +0000602 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +0000603
Harald Welte4a543e82009-02-28 13:17:55 +0000604 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +0000605
Harald Welte7584aea2009-02-11 11:44:12 +0000606 msg->lchan = lchan;
607 gh->proto_discr = GSM48_PDISC_RR;
608 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
609
610 /* fill the channel information element, this code
611 * should probably be shared with rsl_rx_chan_rqd() */
612 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +0000613 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +0000614 cmm->chan_desc.h0.h = 0;
615 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
616 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
617 cmm->mode = mode;
618
619 return gsm48_sendmsg(msg);
620}
621
Harald Weltedb253af2008-12-30 17:56:55 +0000622/* Section 9.2.15a */
623int gsm48_tx_mm_info(struct gsm_lchan *lchan)
624{
625 struct msgb *msg = gsm48_msgb_alloc();
626 struct gsm48_hdr *gh;
627 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000628 u_int8_t *ptr8;
629 u_int16_t *ptr16;
630 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000631 int i;
632
633 msg->lchan = lchan;
634
635 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
636 gh->proto_discr = GSM48_PDISC_MM;
637 gh->msg_type = GSM48_MT_MM_INFO;
638
639 if (net->name_long) {
640 name_len = strlen(net->name_long);
641 /* 10.5.3.5a */
642 ptr8 = msgb_put(msg, 3);
643 ptr8[0] = GSM48_IE_NAME_LONG;
644 ptr8[1] = name_len*2 +1;
645 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
646
647 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
648 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000649 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000650
651 /* FIXME: Use Cell Broadcast, not UCS-2, since
652 * UCS-2 is only supported by later revisions of the spec */
653 }
654
655 if (net->name_short) {
656 name_len = strlen(net->name_short);
657 /* 10.5.3.5a */
658 ptr8 = (u_int8_t *) msgb_put(msg, 3);
659 ptr8[0] = GSM48_IE_NAME_LONG;
660 ptr8[1] = name_len*2 + 1;
661 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
662
Harald Weltee872cb12009-01-01 00:33:37 +0000663 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000664 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000665 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000666 }
667
668#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000669 /* move back to the top */
670 time_t cur_t;
671 struct tm* cur_time;
672 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000673 /* Section 10.5.3.9 */
674 cur_t = time(NULL);
675 cur_time = gmtime(cur_t);
676 ptr8 = msgb_put(msg, 8);
677 ptr8[0] = GSM48_IE_NET_TIME_TZ;
678 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
679 ptr8[2] = to_bcd8(cur_time->tm_mon);
680 ptr8[3] = to_bcd8(cur_time->tm_mday);
681 ptr8[4] = to_bcd8(cur_time->tm_hour);
682 ptr8[5] = to_bcd8(cur_time->tm_min);
683 ptr8[6] = to_bcd8(cur_time->tm_sec);
684 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
685 tz15min = (cur_time->tm_gmtoff)/(60*15);
686 ptr8[6] = to_bcd8(tz15min);
687 if (tz15min < 0)
688 ptr8[6] |= 0x80;
689#endif
690
691 return gsm48_sendmsg(msg);
692}
693
Harald Welte4b634542008-12-27 01:55:51 +0000694static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
695{
Harald Welte4b634542008-12-27 01:55:51 +0000696 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000697 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000698}
Harald Welteba4cf162009-01-10 01:49:35 +0000699
700/* 9.2.6 CM service reject */
701static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
702 enum gsm48_reject_value value)
703{
704 struct msgb *msg = gsm48_msgb_alloc();
705 struct gsm48_hdr *gh;
706
707 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
708
709 msg->lchan = lchan;
710 use_lchan(lchan);
711
712 gh->proto_discr = GSM48_PDISC_MM;
713 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
714 gh->data[0] = value;
715 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
716
717 return gsm48_sendmsg(msg);
718}
719
Harald Welte4ed0e922009-01-10 03:17:30 +0000720
721/*
722 * Handle CM Service Requests
723 * a) Verify that the packet is long enough to contain the information
724 * we require otherwsie reject with INCORRECT_MESSAGE
725 * b) Try to parse the TMSI. If we do not have one reject
726 * c) Check that we know the subscriber with the TMSI otherwise reject
727 * with a HLR cause
728 * d) Set the subscriber on the gsm_lchan and accept
729 */
Harald Welte4b634542008-12-27 01:55:51 +0000730static int gsm48_rx_mm_serv_req(struct msgb *msg)
731{
Harald Welteba4cf162009-01-10 01:49:35 +0000732 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000733 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000734
Harald Welteba4cf162009-01-10 01:49:35 +0000735 struct gsm_subscriber *subscr;
736 struct gsm48_hdr *gh = msgb_l3(msg);
737 struct gsm48_service_request *req =
738 (struct gsm48_service_request *)gh->data;
739
740 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
741 DEBUGP(DMM, "<- CM SERVICE REQUEST wrong sized message\n");
742 return gsm48_tx_mm_serv_rej(msg->lchan,
743 GSM48_REJECT_INCORRECT_MESSAGE);
744 }
745
746 if (msg->data_len < req->mi_len + 6) {
747 DEBUGP(DMM, "<- CM SERVICE REQUEST MI does not fit in package\n");
748 return gsm48_tx_mm_serv_rej(msg->lchan,
749 GSM48_REJECT_INCORRECT_MESSAGE);
750 }
751
752 mi_type = req->mi[0] & GSM_MI_TYPE_MASK;
753 if (mi_type != GSM_MI_TYPE_TMSI) {
754 DEBUGP(DMM, "<- CM SERVICE REQUEST mi type is not TMSI: %d\n", mi_type);
755 return gsm48_tx_mm_serv_rej(msg->lchan,
756 GSM48_REJECT_INCORRECT_MESSAGE);
757 }
758
Harald Welte4ed0e922009-01-10 03:17:30 +0000759 mi_to_string(mi_string, sizeof(mi_string), req->mi, req->mi_len);
760 subscr = subscr_get_by_tmsi(mi_string);
761 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x mi_type=0x%02x M(%s)\n",
762 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000763
Harald Welte2a139372009-02-22 21:14:55 +0000764 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000765 if (!subscr)
766 return gsm48_tx_mm_serv_rej(msg->lchan,
767 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
768
769 if (!msg->lchan->subscr)
770 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000771 else if (msg->lchan->subscr != subscr) {
772 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
773 subscr_put(subscr);
774 }
775
Harald Welte4b634542008-12-27 01:55:51 +0000776 return gsm48_tx_mm_serv_ack(msg->lchan);
777}
778
Harald Welte2a139372009-02-22 21:14:55 +0000779static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
780{
781 struct gsm48_hdr *gh = msgb_l3(msg);
782 struct gsm48_imsi_detach_ind *idi =
783 (struct gsm48_imsi_detach_ind *) gh->data;
784 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
785 char mi_string[MI_SIZE];
786 struct gsm_subscriber *subscr;
787
788 mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
789 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
790 mi_type, mi_string);
791
792 switch (mi_type) {
793 case GSM_MI_TYPE_TMSI:
794 subscr = subscr_get_by_tmsi(mi_string);
795 break;
796 case GSM_MI_TYPE_IMSI:
797 subscr = subscr_get_by_imsi(mi_string);
798 break;
799 case GSM_MI_TYPE_IMEI:
800 case GSM_MI_TYPE_IMEISV:
801 /* no sim card... FIXME: what to do ? */
802 fprintf(stderr, "Unimplemented mobile identity type\n");
803 break;
804 default:
805 fprintf(stderr, "Unknown mobile identity type\n");
806 break;
807 }
808
Holger Freyther4a49e772009-04-12 05:37:29 +0000809 if (subscr) {
810 subscr_update(subscr, msg->trx->bts,
811 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +0000812 DEBUGP(DMM, "Subscriber: %s\n",
813 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther4a49e772009-04-12 05:37:29 +0000814 } else
Harald Welte2a139372009-02-22 21:14:55 +0000815 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
816
817 put_lchan(msg->lchan);
818
819 return 0;
820}
821
Harald Weltebf5e8df2009-02-03 12:59:45 +0000822/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000823static int gsm0408_rcv_mm(struct msgb *msg)
824{
825 struct gsm48_hdr *gh = msgb_l3(msg);
826 int rc;
827
828 switch (gh->msg_type & 0xbf) {
829 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000830 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000831 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000832 break;
833 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000834 rc = mm_rx_id_resp(msg);
835 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000836 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000837 rc = gsm48_rx_mm_serv_req(msg);
838 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000839 case GSM48_MT_MM_STATUS:
Harald Welte2a139372009-02-22 21:14:55 +0000840 DEBUGP(DMM, "STATUS: FIXME parse error cond.\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000841 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000842 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000843 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
844 msg->lchan->subscr ?
845 msg->lchan->subscr->imsi :
846 "unknown subscriber");
847 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000848 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000849 rc = gsm48_rx_mm_imsi_detach_ind(msg);
850 break;
851 case GSM48_MT_MM_CM_REEST_REQ:
852 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
853 break;
854 case GSM48_MT_MM_AUTH_RESP:
855 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000856 break;
857 default:
858 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
859 gh->msg_type);
860 break;
861 }
862
863 return rc;
864}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000865
Harald Welte2d35ae62009-02-06 12:02:13 +0000866/* Receive a PAGING RESPONSE message from the MS */
867static int gsm48_rr_rx_pag_resp(struct msgb *msg)
868{
869 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +0000870 u_int8_t *classmark2_lv = gh->data + 1;
871 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
872 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +0000873 char mi_string[MI_SIZE];
874 struct gsm_subscriber *subscr;
Harald Welte595ad7b2009-02-16 22:05:44 +0000875 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +0000876 int rc = 0;
877
Harald Welte61548982009-02-22 21:26:29 +0000878 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +0000879 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
880 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000881 switch (mi_type) {
882 case GSM_MI_TYPE_TMSI:
883 subscr = subscr_get_by_tmsi(mi_string);
884 break;
885 case GSM_MI_TYPE_IMSI:
886 subscr = subscr_get_by_imsi(mi_string);
887 break;
888 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000889
890 if (!subscr) {
891 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000892 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000893 return -EINVAL;
894 }
895 DEBUGP(DRR, "<- Channel was requested by %s\n",
896 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000897
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000898 if (!msg->lchan->subscr)
899 msg->lchan->subscr = subscr;
900 else if (msg->lchan->subscr != subscr) {
901 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
902 subscr_put(subscr);
903 }
904
Harald Welte595ad7b2009-02-16 22:05:44 +0000905 sig_data.subscr = subscr;
906 sig_data.bts = msg->lchan->ts->trx->bts;
907 sig_data.lchan = msg->lchan;
908
909 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Welte7ccf7782009-02-17 01:43:01 +0000910 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +0000911
Harald Welte7584aea2009-02-11 11:44:12 +0000912 /* FIXME: somehow signal the completion of the PAGING to
913 * the entity that requested the paging */
914
Harald Welte2d35ae62009-02-06 12:02:13 +0000915 return rc;
916}
917
Harald Weltebf5e8df2009-02-03 12:59:45 +0000918/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000919static int gsm0408_rcv_rr(struct msgb *msg)
920{
921 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000922 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000923
924 switch (gh->msg_type) {
925 case GSM48_MT_RR_CLSM_CHG:
926 DEBUGP(DRR, "CLASSMARK CHANGE\n");
927 /* FIXME: what to do ?!? */
928 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000929 case GSM48_MT_RR_GPRS_SUSP_REQ:
930 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
931 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000932 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +0000933 rc = gsm48_rr_rx_pag_resp(msg);
934 break;
Harald Welte7ccf7782009-02-17 01:43:01 +0000935 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
936 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +0000937 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +0000938 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000939 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000940 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000941 gh->msg_type);
942 break;
943 }
944
Harald Welte2d35ae62009-02-06 12:02:13 +0000945 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000946}
947
Holger Freythere64a7a32009-02-06 21:55:37 +0000948/* 7.1.7 and 9.1.7 Channel release*/
949int gsm48_send_rr_release(struct gsm_lchan *lchan)
950{
951 struct msgb *msg = gsm48_msgb_alloc();
952 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
953 u_int8_t *cause;
954
955 msg->lchan = lchan;
956 gh->proto_discr = GSM48_PDISC_RR;
957 gh->msg_type = GSM48_MT_RR_CHAN_REL;
958
959 cause = msgb_put(msg, 1);
960 cause[0] = GSM48_RR_CAUSE_NORMAL;
961
962 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
963 lchan->nr, lchan->type);
964
965 return gsm48_sendmsg(msg);
966}
967
Harald Welte4bc90a12008-12-27 16:32:52 +0000968/* Call Control */
969
Harald Welte7584aea2009-02-11 11:44:12 +0000970/* The entire call control code is written in accordance with Figure 7.10c
971 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
972 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
973 * it for voice */
974
Harald Welte4bc90a12008-12-27 16:32:52 +0000975static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
976{
977 struct msgb *msg = gsm48_msgb_alloc();
978 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
979 u_int8_t *cause, *call_state;
980
Harald Welte65e74cc2008-12-29 01:55:35 +0000981 gh->proto_discr = GSM48_PDISC_CC;
982
Harald Welte4bc90a12008-12-27 16:32:52 +0000983 msg->lchan = lchan;
984
Harald Welte4bc90a12008-12-27 16:32:52 +0000985 gh->msg_type = GSM48_MT_CC_STATUS;
986
987 cause = msgb_put(msg, 3);
988 cause[0] = 2;
989 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
990 cause[2] = 0x80 | 30; /* response to status inquiry */
991
992 call_state = msgb_put(msg, 1);
993 call_state[0] = 0xc0 | 0x00;
994
Harald Welte65e74cc2008-12-29 01:55:35 +0000995 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000996}
997
Harald Welte6f4b7532008-12-29 00:39:37 +0000998static int gsm48_tx_simple(struct gsm_lchan *lchan,
999 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001000{
1001 struct msgb *msg = gsm48_msgb_alloc();
1002 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1003
1004 msg->lchan = lchan;
1005
Harald Welte6f4b7532008-12-29 00:39:37 +00001006 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001007 gh->msg_type = msg_type;
1008
Harald Welte65e74cc2008-12-29 01:55:35 +00001009 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001010}
1011
Harald Welte09e38af2009-02-16 22:52:23 +00001012/* call-back from paging the B-end of the connection */
1013static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001014 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001015{
Harald Welte7ccf7782009-02-17 01:43:01 +00001016 struct gsm_lchan *lchan = _lchan;
Harald Welte49f48b82009-02-17 15:29:33 +00001017 struct gsm_call *remote_call = param;
1018 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001019 int rc = 0;
Harald Welte09e38af2009-02-16 22:52:23 +00001020
1021 if (hooknum != GSM_HOOK_RR_PAGING)
1022 return -EINVAL;
1023
1024 switch (event) {
1025 case GSM_PAGING_SUCCEEDED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001026 DEBUGP(DCC, "paging succeeded!\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001027 remote_call->remote_lchan = lchan;
1028 call->remote_lchan = remote_call->local_lchan;
Harald Welte09e38af2009-02-16 22:52:23 +00001029 /* send SETUP request to called party */
Harald Welte49f48b82009-02-17 15:29:33 +00001030 rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr);
1031 if (is_ipaccess_bts(lchan->ts->trx->bts))
1032 rsl_ipacc_bind(lchan);
Harald Welte09e38af2009-02-16 22:52:23 +00001033 break;
1034 case GSM_PAGING_EXPIRED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001035 DEBUGP(DCC, "paging expired!\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001036 /* notify caller that we cannot reach called party */
Harald Welte7ccf7782009-02-17 01:43:01 +00001037 /* FIXME: correct cause, etc */
Harald Weltebfaf0972009-02-17 17:43:33 +00001038 rc = gsm48_tx_simple(remote_call->local_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001039 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte09e38af2009-02-16 22:52:23 +00001040 break;
1041 }
Harald Welte7ccf7782009-02-17 01:43:01 +00001042 return rc;
Harald Welte09e38af2009-02-16 22:52:23 +00001043}
1044
Harald Welte4bc90a12008-12-27 16:32:52 +00001045static int gsm48_cc_rx_status_enq(struct msgb *msg)
1046{
Harald Welte4a543e82009-02-28 13:17:55 +00001047 DEBUGP(DCC, "-> STATUS ENQ\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001048 return gsm48_cc_tx_status(msg->lchan);
1049}
1050
1051static int gsm48_cc_rx_setup(struct msgb *msg)
1052{
Harald Welte7584aea2009-02-11 11:44:12 +00001053 struct gsm_call *call = &msg->lchan->call;
1054 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte09e38af2009-02-16 22:52:23 +00001055 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Harald Welte7584aea2009-02-11 11:44:12 +00001056 struct gsm_subscriber *called_subscr;
Harald Welte09e38af2009-02-16 22:52:23 +00001057 char called_number[(43-2)*2 + 1] = "\0";
1058 struct tlv_parsed tp;
1059 u_int8_t num_type;
Harald Welte7584aea2009-02-11 11:44:12 +00001060 int ret;
1061
1062 if (call->state == GSM_CSTATE_NULL ||
1063 call->state == GSM_CSTATE_RELEASE_REQ)
1064 use_lchan(msg->lchan);
1065
1066 call->type = GSM_CT_MO;
1067 call->state = GSM_CSTATE_INITIATED;
Harald Welte49f48b82009-02-17 15:29:33 +00001068 call->local_lchan = msg->lchan;
Harald Welte7584aea2009-02-11 11:44:12 +00001069 call->transaction_id = gh->proto_discr & 0xf0;
1070
Harald Welte09e38af2009-02-16 22:52:23 +00001071 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
1072 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
1073 goto err;
Harald Welte7584aea2009-02-11 11:44:12 +00001074
Harald Welte09e38af2009-02-16 22:52:23 +00001075 /* Parse the number that was dialed and lookup subscriber */
1076 num_type = decode_bcd_number(called_number, sizeof(called_number),
1077 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
Harald Welte7ccf7782009-02-17 01:43:01 +00001078
Harald Welte4b458152009-02-17 13:23:06 +00001079 DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id,
Harald Welte7ccf7782009-02-17 01:43:01 +00001080 called_number);
1081
Harald Welte09e38af2009-02-16 22:52:23 +00001082 called_subscr = subscr_get_by_extension(called_number);
Harald Welte7584aea2009-02-11 11:44:12 +00001083 if (!called_subscr) {
1084 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
1085 put_lchan(msg->lchan);
1086 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001087 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte7584aea2009-02-11 11:44:12 +00001088 }
Harald Welte5a065df2009-02-22 21:13:18 +00001089 if (called_subscr == msg->lchan->subscr) {
1090 DEBUGP(DCC, "subscriber calling himself ?!?\n");
1091 put_lchan(msg->lchan);
1092 subscr_put(called_subscr);
1093 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1094 GSM48_MT_CC_RELEASE_COMPL);
1095 }
Harald Welte7584aea2009-02-11 11:44:12 +00001096
Harald Welte09e38af2009-02-16 22:52:23 +00001097 subscr_get(msg->lchan->subscr);
Harald Welte09e38af2009-02-16 22:52:23 +00001098 call->called_subscr = called_subscr;
1099
Harald Welte7584aea2009-02-11 11:44:12 +00001100 /* start paging of the receiving end of the call */
Harald Welte09e38af2009-02-16 22:52:23 +00001101 paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F,
1102 setup_trig_pag_evt, call);
Harald Welte7584aea2009-02-11 11:44:12 +00001103
1104 /* send a CALL PROCEEDING message to the MO */
1105 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1106 GSM48_MT_CC_CALL_PROC);
1107
Harald Welte49f48b82009-02-17 15:29:33 +00001108 if (is_ipaccess_bts(msg->trx->bts))
1109 rsl_ipacc_bind(msg->lchan);
1110
Harald Welte7584aea2009-02-11 11:44:12 +00001111 /* change TCH/F mode to voice */
Harald Welte2c38aa82009-02-18 03:44:24 +00001112 return gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte09e38af2009-02-16 22:52:23 +00001113
1114err:
1115 /* FIXME: send some kind of RELEASE */
1116 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001117}
Harald Welte7584aea2009-02-11 11:44:12 +00001118
Harald Welte7ccf7782009-02-17 01:43:01 +00001119static int gsm48_cc_rx_alerting(struct msgb *msg)
1120{
1121 struct gsm_call *call = &msg->lchan->call;
Harald Welte4bc90a12008-12-27 16:32:52 +00001122
Harald Welte4b458152009-02-17 13:23:06 +00001123 DEBUGP(DCC, "A -> ALERTING\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001124
1125 /* forward ALERTING to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001126 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001127 return -EIO;
1128
Harald Welte4b458152009-02-17 13:23:06 +00001129 DEBUGP(DCC, "B <- ALERTING\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001130 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001131 GSM48_MT_CC_ALERTING);
1132}
1133
Harald Welte49f48b82009-02-17 15:29:33 +00001134/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001135static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001136{
Harald Welte11fa29c2009-02-19 17:24:39 +00001137 struct gsm_bts *bts = lchan->ts->trx->bts;
1138 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001139 struct gsm_bts_trx_ts *ts;
1140
Harald Welte11fa29c2009-02-19 17:24:39 +00001141 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1142 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1143 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1144
1145 if (bts->type != remote_bts->type) {
1146 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1147 return -EINVAL;
1148 }
Harald Welte49f48b82009-02-17 15:29:33 +00001149
Harald Welte11fa29c2009-02-19 17:24:39 +00001150 switch (bts->type) {
1151 case GSM_BTS_TYPE_NANOBTS_900:
1152 case GSM_BTS_TYPE_NANOBTS_1800:
1153 ts = remote_lchan->ts;
1154 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1155 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1156
1157 ts = lchan->ts;
1158 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1159 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1160 break;
1161 case GSM_BTS_TYPE_BS11:
1162 trau_mux_map_lchan(lchan, remote_lchan);
1163 break;
1164 default:
1165 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1166 break;
1167 }
Harald Welte49f48b82009-02-17 15:29:33 +00001168
1169 return 0;
1170}
1171
Harald Welte7ccf7782009-02-17 01:43:01 +00001172static int gsm48_cc_rx_connect(struct msgb *msg)
1173{
1174 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001175 int rc;
1176
Harald Welte4b458152009-02-17 13:23:06 +00001177 DEBUGP(DCC, "A -> CONNECT\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001178
1179 rc = tch_map(msg->lchan, call->remote_lchan);
1180 if (rc)
1181 return -EIO;
1182
1183 if (!call->remote_lchan)
1184 return -EIO;
1185
Harald Welte4b458152009-02-17 13:23:06 +00001186 DEBUGP(DCC, "A <- CONNECT ACK\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001187 /* MT+MO: need to respond with CONNECT_ACK and pass on */
1188 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001189 GSM48_MT_CC_CONNECT_ACK);
Harald Welte7ccf7782009-02-17 01:43:01 +00001190
Harald Welte49f48b82009-02-17 15:29:33 +00001191
1192 /* forward CONNECT to other party */
Harald Welte4b458152009-02-17 13:23:06 +00001193 DEBUGP(DCC, "B <- CONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001194 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001195 GSM48_MT_CC_CONNECT);
Harald Welte7ccf7782009-02-17 01:43:01 +00001196}
1197
1198static int gsm48_cc_rx_disconnect(struct msgb *msg)
1199{
1200 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001201 int rc;
1202
1203
1204 /* Section 5.4.3.2 */
Harald Welte4b458152009-02-17 13:23:06 +00001205 DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001206 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte7ccf7782009-02-17 01:43:01 +00001207 /* FIXME: clear the network connection */
Harald Welte4b458152009-02-17 13:23:06 +00001208 DEBUGP(DCC, "A <- RELEASE\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001209 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1210 GSM48_MT_CC_RELEASE);
Holger Freythera1f92f02009-04-12 05:37:52 +00001211
1212 /*
1213 * FIXME: This looks wrong! We should have a single
1214 * place to do MMCC-REL-CNF/-REQ/-IND and then switch
1215 * to the NULL state and relase the call
1216 */
1217 subscr_put_channel(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001218
1219 /* forward DISCONNECT to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001220 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001221 return -EIO;
1222
Harald Welte4b458152009-02-17 13:23:06 +00001223 DEBUGP(DCC, "B <- DISCONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001224 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001225 GSM48_MT_CC_DISCONNECT);
1226}
1227
1228static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 };
1229
Harald Welte49f48b82009-02-17 15:29:33 +00001230int gsm48_cc_tx_setup(struct gsm_lchan *lchan,
1231 struct gsm_subscriber *calling_subscr)
Harald Welte65e74cc2008-12-29 01:55:35 +00001232{
1233 struct msgb *msg = gsm48_msgb_alloc();
1234 struct gsm48_hdr *gh;
1235 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001236 u_int8_t bcd_lv[19];
Harald Welte65e74cc2008-12-29 01:55:35 +00001237
Harald Welte7ccf7782009-02-17 01:43:01 +00001238 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001239
1240 call->type = GSM_CT_MT;
Harald Welte49f48b82009-02-17 15:29:33 +00001241
1242 call->local_lchan = msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +00001243 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +00001244
1245 gh->proto_discr = GSM48_PDISC_CC;
1246 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001247
Harald Welte7584aea2009-02-11 11:44:12 +00001248 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
Harald Welte49f48b82009-02-17 15:29:33 +00001249 if (calling_subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001250 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001251 calling_subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001252 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
1253 bcd_lv[0], bcd_lv+1);
1254 }
Harald Welte49f48b82009-02-17 15:29:33 +00001255 if (lchan->subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001256 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001257 lchan->subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001258 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD,
1259 bcd_lv[0], bcd_lv+1);
1260 }
Harald Welte65e74cc2008-12-29 01:55:35 +00001261
Harald Welte4b458152009-02-17 13:23:06 +00001262 DEBUGP(DCC, "B <- SETUP\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001263
1264 return gsm48_sendmsg(msg);
1265}
1266
Harald Welte4bc90a12008-12-27 16:32:52 +00001267static int gsm0408_rcv_cc(struct msgb *msg)
1268{
1269 struct gsm48_hdr *gh = msgb_l3(msg);
1270 u_int8_t msg_type = gh->msg_type & 0xbf;
1271 struct gsm_call *call = &msg->lchan->call;
1272 int rc = 0;
1273
1274 switch (msg_type) {
1275 case GSM48_MT_CC_CALL_CONF:
1276 /* Response to SETUP */
Harald Welte7ccf7782009-02-17 01:43:01 +00001277 DEBUGP(DCC, "-> CALL CONFIRM\n");
Harald Welte4b458152009-02-17 13:23:06 +00001278 /* we now need to MODIFY the channel */
Harald Welte2c38aa82009-02-18 03:44:24 +00001279 rc = gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte4bc90a12008-12-27 16:32:52 +00001280 break;
1281 case GSM48_MT_CC_RELEASE_COMPL:
1282 /* Answer from MS to RELEASE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001283 DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001284 call->state = GSM_CSTATE_NULL;
1285 break;
1286 case GSM48_MT_CC_ALERTING:
Harald Welte7ccf7782009-02-17 01:43:01 +00001287 rc = gsm48_cc_rx_alerting(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001288 break;
1289 case GSM48_MT_CC_CONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001290 rc = gsm48_cc_rx_connect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001291 break;
1292 case GSM48_MT_CC_CONNECT_ACK:
1293 /* MO: Answer to CONNECT */
1294 call->state = GSM_CSTATE_ACTIVE;
Harald Welte7ccf7782009-02-17 01:43:01 +00001295 DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001296 break;
1297 case GSM48_MT_CC_RELEASE:
Harald Welte7ccf7782009-02-17 01:43:01 +00001298 DEBUGP(DCC, "-> RELEASE\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001299 DEBUGP(DCC, "<- RELEASE_COMPLETE\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001300 /* need to respond with RELEASE_COMPLETE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001301 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1302 GSM48_MT_CC_RELEASE_COMPL);
Holger Freythera1f92f02009-04-12 05:37:52 +00001303 subscr_put_channel(msg->lchan);
Harald Welte49f48b82009-02-17 15:29:33 +00001304 call->state = GSM_CSTATE_NULL;
Harald Welte4bc90a12008-12-27 16:32:52 +00001305 break;
1306 case GSM48_MT_CC_STATUS_ENQ:
1307 rc = gsm48_cc_rx_status_enq(msg);
1308 break;
1309 case GSM48_MT_CC_DISCONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001310 rc = gsm48_cc_rx_disconnect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001311 break;
1312 case GSM48_MT_CC_SETUP:
Harald Welte4a543e82009-02-28 13:17:55 +00001313 /* MO: wants to establish a call */
Harald Welte7584aea2009-02-11 11:44:12 +00001314 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001315 break;
1316 case GSM48_MT_CC_EMERG_SETUP:
Harald Welte7ccf7782009-02-17 01:43:01 +00001317 DEBUGP(DCC, "-> EMERGENCY SETUP\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001318 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
1319 break;
Harald Welte4a543e82009-02-28 13:17:55 +00001320 case GSM48_MT_CC_HOLD:
1321 DEBUGP(DCC, "-> HOLD (rejecting)\n");
1322 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1323 GSM48_MT_CC_HOLD_REJ);
1324 break;
1325 case GSM48_MT_CC_RETR:
1326 DEBUGP(DCC, "-> RETR (rejecting)\n");
1327 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1328 GSM48_MT_CC_RETR_REJ);
1329 break;
Harald Welte4bc90a12008-12-27 16:32:52 +00001330 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001331 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +00001332 msg_type);
1333 break;
1334 }
1335
1336 return rc;
1337}
1338
Harald Welte52b1f982008-12-23 20:25:15 +00001339/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
1340int gsm0408_rcvmsg(struct msgb *msg)
1341{
1342 struct gsm48_hdr *gh = msgb_l3(msg);
1343 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00001344 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001345
1346 switch (pdisc) {
1347 case GSM48_PDISC_CC:
1348 rc = gsm0408_rcv_cc(msg);
1349 break;
1350 case GSM48_PDISC_MM:
1351 rc = gsm0408_rcv_mm(msg);
1352 break;
1353 case GSM48_PDISC_RR:
1354 rc = gsm0408_rcv_rr(msg);
1355 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001356 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001357 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001358 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001359 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001360 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00001361 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
1362 pdisc);
1363 break;
1364 default:
1365 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
1366 pdisc);
1367 break;
1368 }
1369
1370 return rc;
1371}
Harald Welte8470bf22008-12-25 23:28:35 +00001372
1373enum chreq_type {
1374 CHREQ_T_EMERG_CALL,
1375 CHREQ_T_CALL_REEST_TCH_F,
1376 CHREQ_T_CALL_REEST_TCH_H,
1377 CHREQ_T_CALL_REEST_TCH_H_DBL,
1378 CHREQ_T_SDCCH,
1379 CHREQ_T_TCH_F,
1380 CHREQ_T_VOICE_CALL_TCH_H,
1381 CHREQ_T_DATA_CALL_TCH_H,
1382 CHREQ_T_LOCATION_UPD,
1383 CHREQ_T_PAG_R_ANY,
1384 CHREQ_T_PAG_R_TCH_F,
1385 CHREQ_T_PAG_R_TCH_FH,
1386};
1387
1388/* Section 9.1.8 / Table 9.9 */
1389struct chreq {
1390 u_int8_t val;
1391 u_int8_t mask;
1392 enum chreq_type type;
1393};
1394
1395/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
1396static const struct chreq chreq_type_neci1[] = {
1397 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1398 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
1399 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
1400 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
1401 { 0xe0, 0xe0, CHREQ_T_SDCCH },
1402 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
1403 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1404 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
1405 { 0x10, 0xf0, CHREQ_T_SDCCH },
1406 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1407 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1408 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1409};
1410
1411/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
1412static const struct chreq chreq_type_neci0[] = {
1413 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1414 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
1415 { 0xe0, 0xe0, CHREQ_T_TCH_F },
1416 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1417 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
1418 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1419 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1420 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1421};
1422
1423static const enum gsm_chan_t ctype_by_chreq[] = {
1424 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
1425 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
1426 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
1427 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
1428 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
1429 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
1430 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1431 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1432 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
1433 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
1434 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
1435 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
1436};
1437
Harald Weltee14a57c2008-12-29 04:08:28 +00001438static const enum gsm_chreq_reason_t reason_by_chreq[] = {
1439 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
1440 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
1441 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
1442 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
1443 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
1444 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
1445 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1446 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1447 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
1448 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
1449 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
1450 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
1451};
1452
Harald Welte8470bf22008-12-25 23:28:35 +00001453enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1454{
1455 int i;
1456 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1457
1458 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1459 const struct chreq *chr = &chreq_type_neci0[i];
1460 if ((ra & chr->mask) == chr->val)
1461 return ctype_by_chreq[chr->type];
1462 }
1463 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1464 return GSM_LCHAN_SDCCH;
1465}
Harald Weltee14a57c2008-12-29 04:08:28 +00001466
1467enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1468{
1469 int i;
1470 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1471
1472 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1473 const struct chreq *chr = &chreq_type_neci0[i];
1474 if ((ra & chr->mask) == chr->val)
1475 return reason_by_chreq[chr->type];
1476 }
1477 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1478 return GSM_CHREQ_REASON_OTHER;
1479}