blob: 67766321e877a1420d6aa9c8bc1dedc28dc00490 [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 Welte52b1f982008-12-23 20:25:15 +000045
Harald Welte8470bf22008-12-25 23:28:35 +000046#define GSM48_ALLOC_SIZE 1024
47#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000048
Harald Welte65e74cc2008-12-29 01:55:35 +000049static int gsm48_tx_simple(struct gsm_lchan *lchan,
50 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +000051static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +000052
Harald Welte52b1f982008-12-23 20:25:15 +000053struct gsm_lai {
54 u_int16_t mcc;
55 u_int16_t mnc;
56 u_int16_t lac;
57};
58
Holger Freyther89824fc2008-12-30 16:18:18 +000059static int authorize_everonye = 0;
60void gsm0408_allow_everyone(int everyone)
61{
62 printf("Allowing everyone?\n");
63 authorize_everonye = everyone;
64}
65
Holger Freythere97f7fb2008-12-31 18:52:11 +000066static int reject_cause = 0;
67void gsm0408_set_reject_cause(int cause)
68{
69 reject_cause = cause;
70}
71
Holger Freyther73487a22008-12-31 18:53:57 +000072static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
73 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +000074{
75 if (!subscriber)
76 return 0;
77
Holger Freyther73487a22008-12-31 18:53:57 +000078 /*
79 * Do not send accept yet as more information should arrive. Some
80 * phones will not send us the information and we will have to check
81 * what we want to do with that.
82 */
83 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
84 return 0;
85
Holger Freyther89824fc2008-12-30 16:18:18 +000086 if (authorize_everonye)
87 return 1;
88
89 return subscriber->authorized;
90}
Holger Freyther07cc8d82008-12-29 06:23:46 +000091
Holger Freyther73487a22008-12-31 18:53:57 +000092static void release_loc_updating_req(struct gsm_lchan *lchan)
93{
Harald Welte179f0642008-12-31 23:59:18 +000094 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +000095 return;
96
97 del_timer(&lchan->loc_operation->updating_timer);
98 free(lchan->loc_operation);
99 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000100 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000101}
102
103static void allocate_loc_updating_req(struct gsm_lchan *lchan)
104{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000105 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000106 release_loc_updating_req(lchan);
107
108 lchan->loc_operation = (struct gsm_loc_updating_operation *)
109 malloc(sizeof(*lchan->loc_operation));
110 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
111}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000112
Harald Welte52b1f982008-12-23 20:25:15 +0000113static void to_bcd(u_int8_t *bcd, u_int16_t val)
114{
Harald Welte4b634542008-12-27 01:55:51 +0000115 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000116 val = val / 10;
117 bcd[1] = val % 10;
118 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000119 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000120 val = val / 10;
121}
122
Holger Freyther17746612008-12-28 16:32:44 +0000123void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000124 u_int16_t mnc, u_int16_t lac)
125{
126 u_int8_t bcd[3];
127
128 to_bcd(bcd, mcc);
129 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
130 lai48->digits[1] = bcd[2];
131
132 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000133 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
134#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000135 lai48->digits[1] |= bcd[2] << 4;
136 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000137#else
138 lai48->digits[1] |= 0xf << 4;
139 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
140#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000141
Harald Welte4b634542008-12-27 01:55:51 +0000142 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000143}
144
Harald Welte255539c2008-12-28 02:26:27 +0000145#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000146#define MID_TMSI_LEN (TMSI_LEN + 2)
147
Harald Welte255539c2008-12-28 02:26:27 +0000148int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000149{
Harald Welte65e74cc2008-12-29 01:55:35 +0000150 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000151
Harald Welte4b634542008-12-27 01:55:51 +0000152 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000153 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000154 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000155 *tptr = htonl(tmsi);
156
157 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000158}
159
Holger Freyther819dd202009-01-04 03:52:50 +0000160struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000161{
162 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
163}
164
Holger Freyther3e2c3232009-01-04 03:55:31 +0000165int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000166{
Harald Welte65e74cc2008-12-29 01:55:35 +0000167 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
168
169 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000170 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000171
Harald Welte65e74cc2008-12-29 01:55:35 +0000172 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
173 /* Send a 04.08 call control message, add transaction
174 * ID and TI flag */
175 gh->proto_discr |= msg->lchan->call.transaction_id;
176
177 /* GSM 04.07 Section 11.2.3.1.3 */
178 switch (msg->lchan->call.type) {
179 case GSM_CT_MO:
180 gh->proto_discr |= 0x80;
181 break;
182 case GSM_CT_MT:
183 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000184 case GSM_CT_NONE:
185 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000186 }
187 }
188 }
189
Harald Welte4b634542008-12-27 01:55:51 +0000190 msg->l3h = msg->data;
191
Harald Welte8470bf22008-12-25 23:28:35 +0000192 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000193}
194
Harald Welte52b1f982008-12-23 20:25:15 +0000195
Holger Freyther429e7762008-12-30 13:28:30 +0000196/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000197int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000198{
Harald Welte8470bf22008-12-25 23:28:35 +0000199 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000200 struct gsm48_hdr *gh;
201
Harald Welte8470bf22008-12-25 23:28:35 +0000202 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000203
204 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
205 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000206 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000207 gh->data[0] = cause;
208
Harald Weltedb253af2008-12-30 17:56:55 +0000209 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
210
Harald Welte65e74cc2008-12-29 01:55:35 +0000211 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000212}
213
214/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000215int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000216{
Harald Welte8470bf22008-12-25 23:28:35 +0000217 struct gsm_bts *bts = lchan->ts->trx->bts;
218 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000219 struct gsm48_hdr *gh;
220 struct gsm48_loc_area_id *lai;
221 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000222 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000223
Harald Welte8470bf22008-12-25 23:28:35 +0000224 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000225
226 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
227 gh->proto_discr = GSM48_PDISC_MM;
228 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
229
230 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000231 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000232 bts->network->network_code, bts->location_area_code);
233
234 mid = msgb_put(msg, MID_TMSI_LEN);
235 generate_mid_from_tmsi(mid, tmsi);
236
237 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
238
Harald Weltedb253af2008-12-30 17:56:55 +0000239 ret = gsm48_sendmsg(msg);
240
Harald Welteba4cf162009-01-10 01:49:35 +0000241 ret = gsm48_cc_tx_setup(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000242 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000243
Holger Freyther07cc8d82008-12-29 06:23:46 +0000244 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000245}
246
Harald Weltefc977a82008-12-27 10:19:37 +0000247static char bcd2char(u_int8_t bcd)
248{
249 if (bcd < 0xa)
250 return '0' + bcd;
251 else
252 return 'A' + (bcd - 0xa);
253}
254
Harald Weltebf5e8df2009-02-03 12:59:45 +0000255/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000256static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
257{
258 int i;
259 u_int8_t mi_type;
260 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000261 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000262
263 mi_type = mi[0] & GSM_MI_TYPE_MASK;
264
265 switch (mi_type) {
266 case GSM_MI_TYPE_NONE:
267 break;
268 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000269 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
270 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
271 memcpy(&tmsi, &mi[1], 4);
272 tmsi = ntohl(tmsi);
273 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000274 }
275 break;
276 case GSM_MI_TYPE_IMSI:
277 case GSM_MI_TYPE_IMEI:
278 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000279 *str_cur++ = bcd2char(mi[0] >> 4);
280
281 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000282 if (str_cur + 2 >= string + str_len)
283 return str_cur - string;
284 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000285 /* skip last nibble in last input byte when GSM_EVEN */
286 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
287 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000288 }
289 break;
290 default:
291 break;
292 }
Harald Weltefc977a82008-12-27 10:19:37 +0000293 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000294
Harald Weltefc977a82008-12-27 10:19:37 +0000295 return str_cur - string;
296}
297
Harald Weltebf5e8df2009-02-03 12:59:45 +0000298/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000299static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
300{
301 struct msgb *msg = gsm48_msgb_alloc();
302 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000303
Harald Welte231ad4f2008-12-27 11:15:38 +0000304 msg->lchan = lchan;
305
306 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
307 gh->proto_discr = GSM48_PDISC_MM;
308 gh->msg_type = GSM48_MT_MM_ID_REQ;
309 gh->data[0] = id_type;
310
Harald Welte65e74cc2008-12-29 01:55:35 +0000311 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000312}
313
314#define MI_SIZE 32
315
Harald Weltebf5e8df2009-02-03 12:59:45 +0000316/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000317static int mm_rx_id_resp(struct msgb *msg)
318{
319 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000320 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000321 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
322 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000323 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000324
325 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000326 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000327 mi_type, mi_string);
328
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000329 /*
330 * Rogue messages could trick us but so is life
331 */
332 put_lchan(lchan);
333
Harald Welte75a983f2008-12-27 21:34:06 +0000334 switch (mi_type) {
335 case GSM_MI_TYPE_IMSI:
336 if (!lchan->subscr)
337 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000338 if (lchan->loc_operation)
339 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000340 break;
341 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000342 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000343 /* update subscribe <-> IMEI mapping */
344 if (lchan->subscr)
345 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000346 if (lchan->loc_operation)
347 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000348 break;
349 }
Holger Freyther73487a22008-12-31 18:53:57 +0000350
351 /* Check if we can let the mobile station enter */
352 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
353 db_subscriber_alloc_tmsi(lchan->subscr);
354 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
355 release_loc_updating_req(lchan);
356 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
357 }
358
Harald Welte75a983f2008-12-27 21:34:06 +0000359 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000360}
361
Harald Welte255539c2008-12-28 02:26:27 +0000362
363static void loc_upd_rej_cb(void *data)
364{
365 struct gsm_lchan *lchan = data;
366
Holger Freyther73487a22008-12-31 18:53:57 +0000367 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000368 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000369 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000370}
371
Holger Freytherb7193e42008-12-29 17:44:08 +0000372static void schedule_reject(struct gsm_lchan *lchan)
373{
Holger Freyther73487a22008-12-31 18:53:57 +0000374 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
375 lchan->loc_operation->updating_timer.data = lchan;
376 schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000377}
378
Harald Welte231ad4f2008-12-27 11:15:38 +0000379#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000380/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000381static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000382{
Harald Welte8470bf22008-12-25 23:28:35 +0000383 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000384 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000385 struct gsm48_loc_upd_req *lu;
386 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000387 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000388 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000389 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000390 char mi_string[MI_SIZE];
391 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000392
Harald Welte8470bf22008-12-25 23:28:35 +0000393 lu = (struct gsm48_loc_upd_req *) gh->data;
394
395 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000396
Harald Weltefc977a82008-12-27 10:19:37 +0000397 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
398
Harald Welte231ad4f2008-12-27 11:15:38 +0000399 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000400
401 allocate_loc_updating_req(lchan);
402
Harald Welte52b1f982008-12-23 20:25:15 +0000403 switch (mi_type) {
404 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000405 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000406 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000407 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000408 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000409
Harald Welte52b1f982008-12-23 20:25:15 +0000410 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000411 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000412 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000413 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000414 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000415 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000416 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000417 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000418
Harald Welte52b1f982008-12-23 20:25:15 +0000419 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000420 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000421 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000422 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000423 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000424 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000425 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000426 }
427 break;
428 case GSM_MI_TYPE_IMEI:
429 case GSM_MI_TYPE_IMEISV:
430 /* no sim card... FIXME: what to do ? */
431 fprintf(stderr, "Unimplemented mobile identity type\n");
432 break;
433 default:
434 fprintf(stderr, "Unknown mobile identity type\n");
435 break;
436 }
437
Harald Welte255539c2008-12-28 02:26:27 +0000438 lchan->subscr = subscr;
439
Holger Freyther73487a22008-12-31 18:53:57 +0000440 /*
441 * Schedule the reject timer and check if we can let the
442 * subscriber into our network immediately or if we need to wait
443 * for identity responses.
444 */
445 schedule_reject(lchan);
446 if (!authorize_subscriber(lchan->loc_operation, subscr))
Harald Weltee872cb12009-01-01 00:33:37 +0000447 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000448
Harald Welte75a983f2008-12-27 21:34:06 +0000449 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000450 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000451
Harald Welte255539c2008-12-28 02:26:27 +0000452 tmsi = strtoul(subscr->tmsi, NULL, 10);
453
Holger Freyther73487a22008-12-31 18:53:57 +0000454 release_loc_updating_req(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000455 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000456}
457
Harald Welte7584aea2009-02-11 11:44:12 +0000458/* 9.1.5 Channel mode modify */
459int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
460{
461 struct msgb *msg = gsm48_msgb_alloc();
462 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
463 struct gsm48_chan_mode_modify *cmm =
464 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
465 u_int16_t arfcn = lchan->ts->trx->arfcn;
466
467 msg->lchan = lchan;
468 gh->proto_discr = GSM48_PDISC_RR;
469 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
470
471 /* fill the channel information element, this code
472 * should probably be shared with rsl_rx_chan_rqd() */
473 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
474 cmm->chan_desc.h0.h = 0;
475 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
476 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
477 cmm->mode = mode;
478
479 return gsm48_sendmsg(msg);
480}
481
Harald Weltedb253af2008-12-30 17:56:55 +0000482/* Section 9.2.15a */
483int gsm48_tx_mm_info(struct gsm_lchan *lchan)
484{
485 struct msgb *msg = gsm48_msgb_alloc();
486 struct gsm48_hdr *gh;
487 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000488 u_int8_t *ptr8;
489 u_int16_t *ptr16;
490 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000491 int i;
492
493 msg->lchan = lchan;
494
495 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
496 gh->proto_discr = GSM48_PDISC_MM;
497 gh->msg_type = GSM48_MT_MM_INFO;
498
499 if (net->name_long) {
500 name_len = strlen(net->name_long);
501 /* 10.5.3.5a */
502 ptr8 = msgb_put(msg, 3);
503 ptr8[0] = GSM48_IE_NAME_LONG;
504 ptr8[1] = name_len*2 +1;
505 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
506
507 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
508 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000509 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000510
511 /* FIXME: Use Cell Broadcast, not UCS-2, since
512 * UCS-2 is only supported by later revisions of the spec */
513 }
514
515 if (net->name_short) {
516 name_len = strlen(net->name_short);
517 /* 10.5.3.5a */
518 ptr8 = (u_int8_t *) msgb_put(msg, 3);
519 ptr8[0] = GSM48_IE_NAME_LONG;
520 ptr8[1] = name_len*2 + 1;
521 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
522
Harald Weltee872cb12009-01-01 00:33:37 +0000523 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000524 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000525 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000526 }
527
528#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000529 /* move back to the top */
530 time_t cur_t;
531 struct tm* cur_time;
532 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000533 /* Section 10.5.3.9 */
534 cur_t = time(NULL);
535 cur_time = gmtime(cur_t);
536 ptr8 = msgb_put(msg, 8);
537 ptr8[0] = GSM48_IE_NET_TIME_TZ;
538 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
539 ptr8[2] = to_bcd8(cur_time->tm_mon);
540 ptr8[3] = to_bcd8(cur_time->tm_mday);
541 ptr8[4] = to_bcd8(cur_time->tm_hour);
542 ptr8[5] = to_bcd8(cur_time->tm_min);
543 ptr8[6] = to_bcd8(cur_time->tm_sec);
544 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
545 tz15min = (cur_time->tm_gmtoff)/(60*15);
546 ptr8[6] = to_bcd8(tz15min);
547 if (tz15min < 0)
548 ptr8[6] |= 0x80;
549#endif
550
551 return gsm48_sendmsg(msg);
552}
553
Harald Welte4b634542008-12-27 01:55:51 +0000554static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
555{
Harald Welte4b634542008-12-27 01:55:51 +0000556 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000557 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000558}
Harald Welteba4cf162009-01-10 01:49:35 +0000559
560/* 9.2.6 CM service reject */
561static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
562 enum gsm48_reject_value value)
563{
564 struct msgb *msg = gsm48_msgb_alloc();
565 struct gsm48_hdr *gh;
566
567 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
568
569 msg->lchan = lchan;
570 use_lchan(lchan);
571
572 gh->proto_discr = GSM48_PDISC_MM;
573 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
574 gh->data[0] = value;
575 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
576
577 return gsm48_sendmsg(msg);
578}
579
Harald Welte4ed0e922009-01-10 03:17:30 +0000580
581/*
582 * Handle CM Service Requests
583 * a) Verify that the packet is long enough to contain the information
584 * we require otherwsie reject with INCORRECT_MESSAGE
585 * b) Try to parse the TMSI. If we do not have one reject
586 * c) Check that we know the subscriber with the TMSI otherwise reject
587 * with a HLR cause
588 * d) Set the subscriber on the gsm_lchan and accept
589 */
Harald Welte4b634542008-12-27 01:55:51 +0000590static int gsm48_rx_mm_serv_req(struct msgb *msg)
591{
Harald Welteba4cf162009-01-10 01:49:35 +0000592 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000593 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000594
Harald Welteba4cf162009-01-10 01:49:35 +0000595 struct gsm_subscriber *subscr;
596 struct gsm48_hdr *gh = msgb_l3(msg);
597 struct gsm48_service_request *req =
598 (struct gsm48_service_request *)gh->data;
599
600 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
601 DEBUGP(DMM, "<- CM SERVICE REQUEST wrong sized message\n");
602 return gsm48_tx_mm_serv_rej(msg->lchan,
603 GSM48_REJECT_INCORRECT_MESSAGE);
604 }
605
606 if (msg->data_len < req->mi_len + 6) {
607 DEBUGP(DMM, "<- CM SERVICE REQUEST MI does not fit in package\n");
608 return gsm48_tx_mm_serv_rej(msg->lchan,
609 GSM48_REJECT_INCORRECT_MESSAGE);
610 }
611
612 mi_type = req->mi[0] & GSM_MI_TYPE_MASK;
613 if (mi_type != GSM_MI_TYPE_TMSI) {
614 DEBUGP(DMM, "<- CM SERVICE REQUEST mi type is not TMSI: %d\n", mi_type);
615 return gsm48_tx_mm_serv_rej(msg->lchan,
616 GSM48_REJECT_INCORRECT_MESSAGE);
617 }
618
Harald Welte4ed0e922009-01-10 03:17:30 +0000619 mi_to_string(mi_string, sizeof(mi_string), req->mi, req->mi_len);
620 subscr = subscr_get_by_tmsi(mi_string);
621 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x mi_type=0x%02x M(%s)\n",
622 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000623
Harald Welte4ed0e922009-01-10 03:17:30 +0000624 if (!subscr)
625 return gsm48_tx_mm_serv_rej(msg->lchan,
626 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
627
628 if (!msg->lchan->subscr)
629 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000630 else if (msg->lchan->subscr != subscr) {
631 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
632 subscr_put(subscr);
633 }
634
Harald Welte4b634542008-12-27 01:55:51 +0000635 return gsm48_tx_mm_serv_ack(msg->lchan);
636}
637
Harald Weltebf5e8df2009-02-03 12:59:45 +0000638/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000639static int gsm0408_rcv_mm(struct msgb *msg)
640{
641 struct gsm48_hdr *gh = msgb_l3(msg);
642 int rc;
643
644 switch (gh->msg_type & 0xbf) {
645 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000646 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000647 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000648 break;
649 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000650 rc = mm_rx_id_resp(msg);
651 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000652 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000653 rc = gsm48_rx_mm_serv_req(msg);
654 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000655 case GSM48_MT_MM_STATUS:
656 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
657 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000658 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000659 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
660 msg->lchan->subscr ?
661 msg->lchan->subscr->imsi :
662 "unknown subscriber");
663 break;
664 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000665 case GSM48_MT_MM_AUTH_RESP:
666 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000667 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
668 gh->msg_type);
669 break;
670 default:
671 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
672 gh->msg_type);
673 break;
674 }
675
676 return rc;
677}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000678
Harald Welte2d35ae62009-02-06 12:02:13 +0000679/* Receive a PAGING RESPONSE message from the MS */
680static int gsm48_rr_rx_pag_resp(struct msgb *msg)
681{
682 struct gsm48_hdr *gh = msgb_l3(msg);
683 struct gsm48_paging_response *pr =
684 (struct gsm48_paging_response *) gh->data;
685 u_int8_t mi_type = pr->mi[0] & GSM_MI_TYPE_MASK;
686 char mi_string[MI_SIZE];
687 struct gsm_subscriber *subscr;
688 int rc = 0;
689
690 mi_to_string(mi_string, sizeof(mi_string), &pr->mi[0], pr->mi_len);
691 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
692 mi_type, mi_string);
693 subscr = subscr_get_by_tmsi(mi_string);
694
695 if (!subscr) {
696 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
697 /* FIXME: close channel? */
698 return -EINVAL;
699 }
700 DEBUGP(DRR, "<- Channel was requested by %s\n",
701 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000702
703 struct paging_signal_data sig_data = {
704 .subscr = subscr,
705 .bts = msg->lchan->ts->trx->bts,
706 .lchan = msg->lchan,
707 };
708 dispatch_signal(S_PAGING, &sig_data.data);
Holger Freyther1fd34142009-02-09 23:42:03 +0000709 paging_request_stop(msg->trx->bts, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000710
711 if (!msg->lchan->subscr)
712 msg->lchan->subscr = subscr;
713 else if (msg->lchan->subscr != subscr) {
714 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
715 subscr_put(subscr);
716 }
717
Harald Welte7584aea2009-02-11 11:44:12 +0000718 /* FIXME: somehow signal the completion of the PAGING to
719 * the entity that requested the paging */
720
Harald Welte2d35ae62009-02-06 12:02:13 +0000721 return rc;
722}
723
Harald Weltebf5e8df2009-02-03 12:59:45 +0000724/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000725static int gsm0408_rcv_rr(struct msgb *msg)
726{
727 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000728 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000729
730 switch (gh->msg_type) {
731 case GSM48_MT_RR_CLSM_CHG:
732 DEBUGP(DRR, "CLASSMARK CHANGE\n");
733 /* FIXME: what to do ?!? */
734 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000735 case GSM48_MT_RR_GPRS_SUSP_REQ:
736 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
737 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000738 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +0000739 rc = gsm48_rr_rx_pag_resp(msg);
740 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000741 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000742 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000743 gh->msg_type);
744 break;
745 }
746
Harald Welte2d35ae62009-02-06 12:02:13 +0000747 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000748}
749
Holger Freythere64a7a32009-02-06 21:55:37 +0000750/* 7.1.7 and 9.1.7 Channel release*/
751int gsm48_send_rr_release(struct gsm_lchan *lchan)
752{
753 struct msgb *msg = gsm48_msgb_alloc();
754 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
755 u_int8_t *cause;
756
757 msg->lchan = lchan;
758 gh->proto_discr = GSM48_PDISC_RR;
759 gh->msg_type = GSM48_MT_RR_CHAN_REL;
760
761 cause = msgb_put(msg, 1);
762 cause[0] = GSM48_RR_CAUSE_NORMAL;
763
764 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
765 lchan->nr, lchan->type);
766
767 return gsm48_sendmsg(msg);
768}
769
Harald Welte4bc90a12008-12-27 16:32:52 +0000770/* Call Control */
771
Harald Welte7584aea2009-02-11 11:44:12 +0000772/* The entire call control code is written in accordance with Figure 7.10c
773 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
774 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
775 * it for voice */
776
Harald Welte4bc90a12008-12-27 16:32:52 +0000777static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
778{
779 struct msgb *msg = gsm48_msgb_alloc();
780 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
781 u_int8_t *cause, *call_state;
782
Harald Welte65e74cc2008-12-29 01:55:35 +0000783 gh->proto_discr = GSM48_PDISC_CC;
784
Harald Welte4bc90a12008-12-27 16:32:52 +0000785 msg->lchan = lchan;
786
Harald Welte4bc90a12008-12-27 16:32:52 +0000787 gh->msg_type = GSM48_MT_CC_STATUS;
788
789 cause = msgb_put(msg, 3);
790 cause[0] = 2;
791 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
792 cause[2] = 0x80 | 30; /* response to status inquiry */
793
794 call_state = msgb_put(msg, 1);
795 call_state[0] = 0xc0 | 0x00;
796
Harald Welte65e74cc2008-12-29 01:55:35 +0000797 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000798}
799
Harald Welte6f4b7532008-12-29 00:39:37 +0000800static int gsm48_tx_simple(struct gsm_lchan *lchan,
801 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000802{
803 struct msgb *msg = gsm48_msgb_alloc();
804 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
805
806 msg->lchan = lchan;
807
Harald Welte6f4b7532008-12-29 00:39:37 +0000808 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000809 gh->msg_type = msg_type;
810
Harald Welte65e74cc2008-12-29 01:55:35 +0000811 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000812}
813
814static int gsm48_cc_rx_status_enq(struct msgb *msg)
815{
816 return gsm48_cc_tx_status(msg->lchan);
817}
818
819static int gsm48_cc_rx_setup(struct msgb *msg)
820{
Harald Welte7584aea2009-02-11 11:44:12 +0000821 struct gsm_call *call = &msg->lchan->call;
822 struct gsm48_hdr *gh = msgb_l3(msg);
823 struct gsm_subscriber *called_subscr;
824 int ret;
825
826 if (call->state == GSM_CSTATE_NULL ||
827 call->state == GSM_CSTATE_RELEASE_REQ)
828 use_lchan(msg->lchan);
829
830 call->type = GSM_CT_MO;
831 call->state = GSM_CSTATE_INITIATED;
832 call->transaction_id = gh->proto_discr & 0xf0;
833
834 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
835
836 /* Parse the number that was dialed and lookup subscriber */
837 called_subscr = NULL;
838
839 if (!called_subscr) {
840 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
841 put_lchan(msg->lchan);
842 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
843 GSM48_MT_CC_RELEASE_COMPL);
844 }
845
846 /* start paging of the receiving end of the call */
847 paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F);
848
849 /* send a CALL PROCEEDING message to the MO */
850 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
851 GSM48_MT_CC_CALL_PROC);
852
853 /* change TCH/F mode to voice */
854 return gsm48_tx_chan_mode_modify(msg->lchan, 0x01);
Harald Welte4bc90a12008-12-27 16:32:52 +0000855}
Harald Welte7584aea2009-02-11 11:44:12 +0000856
857static const u_int8_t calling_bcd[] = { 0xb9, 0x83, 0x32, 0x24 };
Harald Welte4bc90a12008-12-27 16:32:52 +0000858
Harald Welte65e74cc2008-12-29 01:55:35 +0000859int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
860{
861 struct msgb *msg = gsm48_msgb_alloc();
862 struct gsm48_hdr *gh;
863 struct gsm_call *call = &lchan->call;
864
Harald Welte7584aea2009-02-11 11:44:12 +0000865 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 4 +
866 sizeof(calling_bcd));
Harald Welte65e74cc2008-12-29 01:55:35 +0000867
868 call->type = GSM_CT_MT;
869 msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +0000870 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000871
872 gh->proto_discr = GSM48_PDISC_CC;
873 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte7584aea2009-02-11 11:44:12 +0000874 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
875 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
876 sizeof(calling_bcd), calling_bcd);
Harald Welte65e74cc2008-12-29 01:55:35 +0000877
878 DEBUGP(DCC, "Sending SETUP\n");
879
880 return gsm48_sendmsg(msg);
881}
882
Harald Welte4bc90a12008-12-27 16:32:52 +0000883static int gsm0408_rcv_cc(struct msgb *msg)
884{
885 struct gsm48_hdr *gh = msgb_l3(msg);
886 u_int8_t msg_type = gh->msg_type & 0xbf;
887 struct gsm_call *call = &msg->lchan->call;
888 int rc = 0;
889
890 switch (msg_type) {
891 case GSM48_MT_CC_CALL_CONF:
892 /* Response to SETUP */
893 DEBUGP(DCC, "CALL CONFIRM\n");
894 break;
895 case GSM48_MT_CC_RELEASE_COMPL:
896 /* Answer from MS to RELEASE */
897 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
898 call->state = GSM_CSTATE_NULL;
899 break;
900 case GSM48_MT_CC_ALERTING:
901 DEBUGP(DCC, "ALERTING\n");
902 break;
903 case GSM48_MT_CC_CONNECT:
904 DEBUGP(DCC, "CONNECT\n");
905 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000906 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
907 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000908 break;
909 case GSM48_MT_CC_CONNECT_ACK:
910 /* MO: Answer to CONNECT */
911 call->state = GSM_CSTATE_ACTIVE;
912 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
913 break;
914 case GSM48_MT_CC_RELEASE:
915 DEBUGP(DCC, "RELEASE\n");
916 /* need to respond with RELEASE_COMPLETE */
917 break;
918 case GSM48_MT_CC_STATUS_ENQ:
919 rc = gsm48_cc_rx_status_enq(msg);
920 break;
921 case GSM48_MT_CC_DISCONNECT:
922 /* Section 5.4.3.2 */
923 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
924 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte498b0bb2009-01-09 21:27:43 +0000925 if (call->state != GSM_CSTATE_NULL)
926 put_lchan(msg->lchan);
Harald Welte4bc90a12008-12-27 16:32:52 +0000927 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000928 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
929 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000930 break;
931 case GSM48_MT_CC_SETUP:
Harald Welte7584aea2009-02-11 11:44:12 +0000932 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000933 break;
934 case GSM48_MT_CC_EMERG_SETUP:
935 DEBUGP(DCC, "EMERGENCY SETUP\n");
936 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
937 break;
938 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000939 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +0000940 msg_type);
941 break;
942 }
943
944 return rc;
945}
946
Harald Welte52b1f982008-12-23 20:25:15 +0000947/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
948int gsm0408_rcvmsg(struct msgb *msg)
949{
950 struct gsm48_hdr *gh = msgb_l3(msg);
951 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000952 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000953
954 switch (pdisc) {
955 case GSM48_PDISC_CC:
956 rc = gsm0408_rcv_cc(msg);
957 break;
958 case GSM48_PDISC_MM:
959 rc = gsm0408_rcv_mm(msg);
960 break;
961 case GSM48_PDISC_RR:
962 rc = gsm0408_rcv_rr(msg);
963 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000964 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000965 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000966 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000967 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000968 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000969 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
970 pdisc);
971 break;
972 default:
973 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
974 pdisc);
975 break;
976 }
977
978 return rc;
979}
Harald Welte8470bf22008-12-25 23:28:35 +0000980
981enum chreq_type {
982 CHREQ_T_EMERG_CALL,
983 CHREQ_T_CALL_REEST_TCH_F,
984 CHREQ_T_CALL_REEST_TCH_H,
985 CHREQ_T_CALL_REEST_TCH_H_DBL,
986 CHREQ_T_SDCCH,
987 CHREQ_T_TCH_F,
988 CHREQ_T_VOICE_CALL_TCH_H,
989 CHREQ_T_DATA_CALL_TCH_H,
990 CHREQ_T_LOCATION_UPD,
991 CHREQ_T_PAG_R_ANY,
992 CHREQ_T_PAG_R_TCH_F,
993 CHREQ_T_PAG_R_TCH_FH,
994};
995
996/* Section 9.1.8 / Table 9.9 */
997struct chreq {
998 u_int8_t val;
999 u_int8_t mask;
1000 enum chreq_type type;
1001};
1002
1003/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
1004static const struct chreq chreq_type_neci1[] = {
1005 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1006 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
1007 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
1008 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
1009 { 0xe0, 0xe0, CHREQ_T_SDCCH },
1010 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
1011 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1012 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
1013 { 0x10, 0xf0, CHREQ_T_SDCCH },
1014 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1015 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1016 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1017};
1018
1019/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
1020static const struct chreq chreq_type_neci0[] = {
1021 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1022 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
1023 { 0xe0, 0xe0, CHREQ_T_TCH_F },
1024 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1025 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
1026 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1027 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1028 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1029};
1030
1031static const enum gsm_chan_t ctype_by_chreq[] = {
1032 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
1033 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
1034 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
1035 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
1036 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
1037 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
1038 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1039 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1040 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
1041 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
1042 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
1043 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
1044};
1045
Harald Weltee14a57c2008-12-29 04:08:28 +00001046static const enum gsm_chreq_reason_t reason_by_chreq[] = {
1047 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
1048 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
1049 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
1050 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
1051 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
1052 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
1053 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1054 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1055 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
1056 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
1057 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
1058 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
1059};
1060
Harald Welte8470bf22008-12-25 23:28:35 +00001061enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1062{
1063 int i;
1064 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1065
1066 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1067 const struct chreq *chr = &chreq_type_neci0[i];
1068 if ((ra & chr->mask) == chr->val)
1069 return ctype_by_chreq[chr->type];
1070 }
1071 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1072 return GSM_LCHAN_SDCCH;
1073}
Harald Weltee14a57c2008-12-29 04:08:28 +00001074
1075enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1076{
1077 int i;
1078 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1079
1080 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1081 const struct chreq *chr = &chreq_type_neci0[i];
1082 if ((ra & chr->mask) == chr->val)
1083 return reason_by_chreq[chr->type];
1084 }
1085 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1086 return GSM_CHREQ_REASON_OTHER;
1087}