blob: c84e7618674994e275706bc5774f94eaf7d4134c [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
4/* (C) 2008 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>
35#include <openbsc/debug.h>
36#include <openbsc/gsm_data.h>
37#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000038#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/gsm_04_08.h>
40#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000041#include <openbsc/chan_alloc.h>
Harald Welte52b1f982008-12-23 20:25:15 +000042
Harald Welte8470bf22008-12-25 23:28:35 +000043#define GSM48_ALLOC_SIZE 1024
44#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000045
Harald Welte65e74cc2008-12-29 01:55:35 +000046static int gsm48_tx_simple(struct gsm_lchan *lchan,
47 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +000048static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +000049
Harald Welte52b1f982008-12-23 20:25:15 +000050struct gsm_lai {
51 u_int16_t mcc;
52 u_int16_t mnc;
53 u_int16_t lac;
54};
55
Holger Freyther89824fc2008-12-30 16:18:18 +000056static int authorize_everonye = 0;
57void gsm0408_allow_everyone(int everyone)
58{
59 printf("Allowing everyone?\n");
60 authorize_everonye = everyone;
61}
62
Holger Freythere97f7fb2008-12-31 18:52:11 +000063static int reject_cause = 0;
64void gsm0408_set_reject_cause(int cause)
65{
66 reject_cause = cause;
67}
68
Holger Freyther73487a22008-12-31 18:53:57 +000069static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
70 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +000071{
72 if (!subscriber)
73 return 0;
74
Holger Freyther73487a22008-12-31 18:53:57 +000075 /*
76 * Do not send accept yet as more information should arrive. Some
77 * phones will not send us the information and we will have to check
78 * what we want to do with that.
79 */
80 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
81 return 0;
82
Holger Freyther89824fc2008-12-30 16:18:18 +000083 if (authorize_everonye)
84 return 1;
85
86 return subscriber->authorized;
87}
Holger Freyther07cc8d82008-12-29 06:23:46 +000088
Holger Freyther73487a22008-12-31 18:53:57 +000089static void release_loc_updating_req(struct gsm_lchan *lchan)
90{
Harald Welte179f0642008-12-31 23:59:18 +000091 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +000092 return;
93
94 del_timer(&lchan->loc_operation->updating_timer);
95 free(lchan->loc_operation);
96 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +000097 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +000098}
99
100static void allocate_loc_updating_req(struct gsm_lchan *lchan)
101{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000102 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000103 release_loc_updating_req(lchan);
104
105 lchan->loc_operation = (struct gsm_loc_updating_operation *)
106 malloc(sizeof(*lchan->loc_operation));
107 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
108}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000109
Harald Welte52b1f982008-12-23 20:25:15 +0000110static void to_bcd(u_int8_t *bcd, u_int16_t val)
111{
Harald Welte4b634542008-12-27 01:55:51 +0000112 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000113 val = val / 10;
114 bcd[1] = val % 10;
115 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000116 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000117 val = val / 10;
118}
119
Holger Freyther17746612008-12-28 16:32:44 +0000120void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000121 u_int16_t mnc, u_int16_t lac)
122{
123 u_int8_t bcd[3];
124
125 to_bcd(bcd, mcc);
126 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
127 lai48->digits[1] = bcd[2];
128
129 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000130 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
131#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000132 lai48->digits[1] |= bcd[2] << 4;
133 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000134#else
135 lai48->digits[1] |= 0xf << 4;
136 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
137#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000138
Harald Welte4b634542008-12-27 01:55:51 +0000139 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000140}
141
Harald Welte255539c2008-12-28 02:26:27 +0000142#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000143#define MID_TMSI_LEN (TMSI_LEN + 2)
144
Harald Welte255539c2008-12-28 02:26:27 +0000145int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000146{
Harald Welte65e74cc2008-12-29 01:55:35 +0000147 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000148
Harald Welte4b634542008-12-27 01:55:51 +0000149 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000150 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000151 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000152 *tptr = htonl(tmsi);
153
154 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000155}
156
Holger Freyther819dd202009-01-04 03:52:50 +0000157struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000158{
159 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
160}
161
Holger Freyther3e2c3232009-01-04 03:55:31 +0000162int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000163{
Harald Welte65e74cc2008-12-29 01:55:35 +0000164 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
165
166 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000167 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000168
Harald Welte65e74cc2008-12-29 01:55:35 +0000169 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
170 /* Send a 04.08 call control message, add transaction
171 * ID and TI flag */
172 gh->proto_discr |= msg->lchan->call.transaction_id;
173
174 /* GSM 04.07 Section 11.2.3.1.3 */
175 switch (msg->lchan->call.type) {
176 case GSM_CT_MO:
177 gh->proto_discr |= 0x80;
178 break;
179 case GSM_CT_MT:
180 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000181 case GSM_CT_NONE:
182 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000183 }
184 }
185 }
186
Harald Welte4b634542008-12-27 01:55:51 +0000187 msg->l3h = msg->data;
188
Harald Welte8470bf22008-12-25 23:28:35 +0000189 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000190}
191
Harald Welte52b1f982008-12-23 20:25:15 +0000192
Holger Freyther429e7762008-12-30 13:28:30 +0000193/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000194int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000195{
Harald Welte8470bf22008-12-25 23:28:35 +0000196 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000197 struct gsm48_hdr *gh;
198
Harald Welte8470bf22008-12-25 23:28:35 +0000199 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000200
201 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
202 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000203 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000204 gh->data[0] = cause;
205
Harald Weltedb253af2008-12-30 17:56:55 +0000206 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
207
208 //gsm0411_send_sms(lchan, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000209
Harald Welte65e74cc2008-12-29 01:55:35 +0000210 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000211}
212
213/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000214int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000215{
Harald Welte8470bf22008-12-25 23:28:35 +0000216 struct gsm_bts *bts = lchan->ts->trx->bts;
217 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000218 struct gsm48_hdr *gh;
219 struct gsm48_loc_area_id *lai;
220 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000221 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000222
Harald Welte8470bf22008-12-25 23:28:35 +0000223 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000224
225 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
226 gh->proto_discr = GSM48_PDISC_MM;
227 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
228
229 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000230 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000231 bts->network->network_code, bts->location_area_code);
232
233 mid = msgb_put(msg, MID_TMSI_LEN);
234 generate_mid_from_tmsi(mid, tmsi);
235
236 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
237
Harald Weltedb253af2008-12-30 17:56:55 +0000238 ret = gsm48_sendmsg(msg);
239
Harald Welteba4cf162009-01-10 01:49:35 +0000240 ret = gsm48_cc_tx_setup(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000241 ret = gsm48_tx_mm_info(lchan);
Harald Welteba4cf162009-01-10 01:49:35 +0000242 //ret = gsm0411_send_sms(lchan, NULL);
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
255/* 10.5.1.4 */
256static 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 Welte231ad4f2008-12-27 11:15:38 +0000298/* Chapter 9.2.10 */
299static 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
316/* Chapter 9.2.11 */
317static 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 Welte52b1f982008-12-23 20:25:15 +0000380/* Chapter 9.2.15 */
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 Weltedb253af2008-12-30 17:56:55 +0000458/* Section 9.2.15a */
459int gsm48_tx_mm_info(struct gsm_lchan *lchan)
460{
461 struct msgb *msg = gsm48_msgb_alloc();
462 struct gsm48_hdr *gh;
463 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000464 u_int8_t *ptr8;
465 u_int16_t *ptr16;
466 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000467 int i;
468
469 msg->lchan = lchan;
470
471 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
472 gh->proto_discr = GSM48_PDISC_MM;
473 gh->msg_type = GSM48_MT_MM_INFO;
474
475 if (net->name_long) {
476 name_len = strlen(net->name_long);
477 /* 10.5.3.5a */
478 ptr8 = msgb_put(msg, 3);
479 ptr8[0] = GSM48_IE_NAME_LONG;
480 ptr8[1] = name_len*2 +1;
481 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
482
483 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
484 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000485 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000486
487 /* FIXME: Use Cell Broadcast, not UCS-2, since
488 * UCS-2 is only supported by later revisions of the spec */
489 }
490
491 if (net->name_short) {
492 name_len = strlen(net->name_short);
493 /* 10.5.3.5a */
494 ptr8 = (u_int8_t *) msgb_put(msg, 3);
495 ptr8[0] = GSM48_IE_NAME_LONG;
496 ptr8[1] = name_len*2 + 1;
497 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
498
Harald Weltee872cb12009-01-01 00:33:37 +0000499 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000500 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000501 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000502 }
503
504#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000505 /* move back to the top */
506 time_t cur_t;
507 struct tm* cur_time;
508 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000509 /* Section 10.5.3.9 */
510 cur_t = time(NULL);
511 cur_time = gmtime(cur_t);
512 ptr8 = msgb_put(msg, 8);
513 ptr8[0] = GSM48_IE_NET_TIME_TZ;
514 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
515 ptr8[2] = to_bcd8(cur_time->tm_mon);
516 ptr8[3] = to_bcd8(cur_time->tm_mday);
517 ptr8[4] = to_bcd8(cur_time->tm_hour);
518 ptr8[5] = to_bcd8(cur_time->tm_min);
519 ptr8[6] = to_bcd8(cur_time->tm_sec);
520 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
521 tz15min = (cur_time->tm_gmtoff)/(60*15);
522 ptr8[6] = to_bcd8(tz15min);
523 if (tz15min < 0)
524 ptr8[6] |= 0x80;
525#endif
526
527 return gsm48_sendmsg(msg);
528}
529
Harald Welte4b634542008-12-27 01:55:51 +0000530static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
531{
Harald Welte4b634542008-12-27 01:55:51 +0000532 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000533 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000534}
Harald Welteba4cf162009-01-10 01:49:35 +0000535
536/* 9.2.6 CM service reject */
537static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
538 enum gsm48_reject_value value)
539{
540 struct msgb *msg = gsm48_msgb_alloc();
541 struct gsm48_hdr *gh;
542
543 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
544
545 msg->lchan = lchan;
546 use_lchan(lchan);
547
548 gh->proto_discr = GSM48_PDISC_MM;
549 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
550 gh->data[0] = value;
551 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
552
553 return gsm48_sendmsg(msg);
554}
555
Harald Welte4ed0e922009-01-10 03:17:30 +0000556
557/*
558 * Handle CM Service Requests
559 * a) Verify that the packet is long enough to contain the information
560 * we require otherwsie reject with INCORRECT_MESSAGE
561 * b) Try to parse the TMSI. If we do not have one reject
562 * c) Check that we know the subscriber with the TMSI otherwise reject
563 * with a HLR cause
564 * d) Set the subscriber on the gsm_lchan and accept
565 */
Harald Welte4b634542008-12-27 01:55:51 +0000566static int gsm48_rx_mm_serv_req(struct msgb *msg)
567{
Harald Welteba4cf162009-01-10 01:49:35 +0000568 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000569 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000570
Harald Welteba4cf162009-01-10 01:49:35 +0000571 struct gsm_subscriber *subscr;
572 struct gsm48_hdr *gh = msgb_l3(msg);
573 struct gsm48_service_request *req =
574 (struct gsm48_service_request *)gh->data;
575
576 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
577 DEBUGP(DMM, "<- CM SERVICE REQUEST wrong sized message\n");
578 return gsm48_tx_mm_serv_rej(msg->lchan,
579 GSM48_REJECT_INCORRECT_MESSAGE);
580 }
581
582 if (msg->data_len < req->mi_len + 6) {
583 DEBUGP(DMM, "<- CM SERVICE REQUEST MI does not fit in package\n");
584 return gsm48_tx_mm_serv_rej(msg->lchan,
585 GSM48_REJECT_INCORRECT_MESSAGE);
586 }
587
588 mi_type = req->mi[0] & GSM_MI_TYPE_MASK;
589 if (mi_type != GSM_MI_TYPE_TMSI) {
590 DEBUGP(DMM, "<- CM SERVICE REQUEST mi type is not TMSI: %d\n", mi_type);
591 return gsm48_tx_mm_serv_rej(msg->lchan,
592 GSM48_REJECT_INCORRECT_MESSAGE);
593 }
594
Harald Welte4ed0e922009-01-10 03:17:30 +0000595 mi_to_string(mi_string, sizeof(mi_string), req->mi, req->mi_len);
596 subscr = subscr_get_by_tmsi(mi_string);
597 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x mi_type=0x%02x M(%s)\n",
598 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000599
Harald Welte4ed0e922009-01-10 03:17:30 +0000600 if (!subscr)
601 return gsm48_tx_mm_serv_rej(msg->lchan,
602 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
603
604 if (!msg->lchan->subscr)
605 msg->lchan->subscr = subscr;
Harald Welte4b634542008-12-27 01:55:51 +0000606 return gsm48_tx_mm_serv_ack(msg->lchan);
607}
608
Harald Welte52b1f982008-12-23 20:25:15 +0000609static int gsm0408_rcv_mm(struct msgb *msg)
610{
611 struct gsm48_hdr *gh = msgb_l3(msg);
612 int rc;
613
614 switch (gh->msg_type & 0xbf) {
615 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000616 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000617 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000618 break;
619 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000620 rc = mm_rx_id_resp(msg);
621 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000622 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000623 rc = gsm48_rx_mm_serv_req(msg);
624 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000625 case GSM48_MT_MM_STATUS:
626 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
627 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000628 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000629 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
630 msg->lchan->subscr ?
631 msg->lchan->subscr->imsi :
632 "unknown subscriber");
633 break;
634 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000635 case GSM48_MT_MM_AUTH_RESP:
636 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000637 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
638 gh->msg_type);
639 break;
640 default:
641 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
642 gh->msg_type);
643 break;
644 }
645
646 return rc;
647}
648static int gsm0408_rcv_rr(struct msgb *msg)
649{
650 struct gsm48_hdr *gh = msgb_l3(msg);
651
652 switch (gh->msg_type) {
653 case GSM48_MT_RR_CLSM_CHG:
654 DEBUGP(DRR, "CLASSMARK CHANGE\n");
655 /* FIXME: what to do ?!? */
656 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000657 case GSM48_MT_RR_GPRS_SUSP_REQ:
658 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
659 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000660 case GSM48_MT_RR_PAG_RESP:
661 default:
662 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
663 gh->msg_type);
664 break;
665 }
666
667 return 0;
668}
669
Harald Welte4bc90a12008-12-27 16:32:52 +0000670/* Call Control */
671
Harald Welte4bc90a12008-12-27 16:32:52 +0000672static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
673{
674 struct msgb *msg = gsm48_msgb_alloc();
675 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
676 u_int8_t *cause, *call_state;
677
Harald Welte65e74cc2008-12-29 01:55:35 +0000678 gh->proto_discr = GSM48_PDISC_CC;
679
Harald Welte4bc90a12008-12-27 16:32:52 +0000680 msg->lchan = lchan;
681
Harald Welte4bc90a12008-12-27 16:32:52 +0000682 gh->msg_type = GSM48_MT_CC_STATUS;
683
684 cause = msgb_put(msg, 3);
685 cause[0] = 2;
686 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
687 cause[2] = 0x80 | 30; /* response to status inquiry */
688
689 call_state = msgb_put(msg, 1);
690 call_state[0] = 0xc0 | 0x00;
691
Harald Welte65e74cc2008-12-29 01:55:35 +0000692 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000693}
694
Harald Welte6f4b7532008-12-29 00:39:37 +0000695static int gsm48_tx_simple(struct gsm_lchan *lchan,
696 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000697{
698 struct msgb *msg = gsm48_msgb_alloc();
699 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
700
701 msg->lchan = lchan;
702
Harald Welte6f4b7532008-12-29 00:39:37 +0000703 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000704 gh->msg_type = msg_type;
705
Harald Welte65e74cc2008-12-29 01:55:35 +0000706 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000707}
708
709static int gsm48_cc_rx_status_enq(struct msgb *msg)
710{
711 return gsm48_cc_tx_status(msg->lchan);
712}
713
Holger Freytherca362a62009-01-04 21:05:01 +0000714#if 0
Harald Welte4bc90a12008-12-27 16:32:52 +0000715static int gsm48_cc_rx_setup(struct msgb *msg)
716{
Harald Welte6f4b7532008-12-29 00:39:37 +0000717 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
718 GSM48_MT_CC_CALL_CONF);
Harald Welte4bc90a12008-12-27 16:32:52 +0000719}
Holger Freytherca362a62009-01-04 21:05:01 +0000720#endif
Harald Welte4bc90a12008-12-27 16:32:52 +0000721
Harald Welte65e74cc2008-12-29 01:55:35 +0000722int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
723{
724 struct msgb *msg = gsm48_msgb_alloc();
725 struct gsm48_hdr *gh;
726 struct gsm_call *call = &lchan->call;
727
728 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 8);
729
730 call->type = GSM_CT_MT;
731 msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +0000732 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000733
734 gh->proto_discr = GSM48_PDISC_CC;
735 gh->msg_type = GSM48_MT_CC_SETUP;
736 gh->data[0] = 0x34;
737 gh->data[1] = 0x00;
738 gh->data[2] = 0x5c;
739 gh->data[3] = 0x04;
740 gh->data[4] = 0xb9;
741 gh->data[5] = 0x83;
742 gh->data[6] = 0x32;
743 gh->data[7] = 0x24;
744
745 DEBUGP(DCC, "Sending SETUP\n");
746
747 return gsm48_sendmsg(msg);
748}
749
Harald Welte4bc90a12008-12-27 16:32:52 +0000750static int gsm0408_rcv_cc(struct msgb *msg)
751{
752 struct gsm48_hdr *gh = msgb_l3(msg);
753 u_int8_t msg_type = gh->msg_type & 0xbf;
754 struct gsm_call *call = &msg->lchan->call;
755 int rc = 0;
756
757 switch (msg_type) {
758 case GSM48_MT_CC_CALL_CONF:
759 /* Response to SETUP */
760 DEBUGP(DCC, "CALL CONFIRM\n");
761 break;
762 case GSM48_MT_CC_RELEASE_COMPL:
763 /* Answer from MS to RELEASE */
764 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
765 call->state = GSM_CSTATE_NULL;
766 break;
767 case GSM48_MT_CC_ALERTING:
768 DEBUGP(DCC, "ALERTING\n");
769 break;
770 case GSM48_MT_CC_CONNECT:
771 DEBUGP(DCC, "CONNECT\n");
772 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000773 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
774 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000775 break;
776 case GSM48_MT_CC_CONNECT_ACK:
777 /* MO: Answer to CONNECT */
778 call->state = GSM_CSTATE_ACTIVE;
779 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
780 break;
781 case GSM48_MT_CC_RELEASE:
782 DEBUGP(DCC, "RELEASE\n");
783 /* need to respond with RELEASE_COMPLETE */
784 break;
785 case GSM48_MT_CC_STATUS_ENQ:
786 rc = gsm48_cc_rx_status_enq(msg);
787 break;
788 case GSM48_MT_CC_DISCONNECT:
789 /* Section 5.4.3.2 */
790 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
791 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte498b0bb2009-01-09 21:27:43 +0000792 if (call->state != GSM_CSTATE_NULL)
793 put_lchan(msg->lchan);
Harald Welte4bc90a12008-12-27 16:32:52 +0000794 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000795 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
796 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000797 break;
798 case GSM48_MT_CC_SETUP:
Harald Welte498b0bb2009-01-09 21:27:43 +0000799 if (call->state == GSM_CSTATE_NULL || call->state == GSM_CSTATE_RELEASE_REQ)
800 use_lchan(msg->lchan);
Harald Welte4bc90a12008-12-27 16:32:52 +0000801 call->type = GSM_CT_MO;
802 call->state = GSM_CSTATE_INITIATED;
803 call->transaction_id = gh->proto_discr & 0xf0;
804 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
Harald Welte6f4b7532008-12-29 00:39:37 +0000805 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
806 GSM48_MT_CC_CONNECT);
Harald Welte4bc90a12008-12-27 16:32:52 +0000807 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
808 break;
809 case GSM48_MT_CC_EMERG_SETUP:
810 DEBUGP(DCC, "EMERGENCY SETUP\n");
811 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
812 break;
813 default:
814 fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n",
815 msg_type);
816 break;
817 }
818
819 return rc;
820}
821
Harald Welte52b1f982008-12-23 20:25:15 +0000822/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
823int gsm0408_rcvmsg(struct msgb *msg)
824{
825 struct gsm48_hdr *gh = msgb_l3(msg);
826 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000827 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000828
829 switch (pdisc) {
830 case GSM48_PDISC_CC:
831 rc = gsm0408_rcv_cc(msg);
832 break;
833 case GSM48_PDISC_MM:
834 rc = gsm0408_rcv_mm(msg);
835 break;
836 case GSM48_PDISC_RR:
837 rc = gsm0408_rcv_rr(msg);
838 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000839 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000840 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000841 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000842 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000843 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000844 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
845 pdisc);
846 break;
847 default:
848 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
849 pdisc);
850 break;
851 }
852
853 return rc;
854}
Harald Welte8470bf22008-12-25 23:28:35 +0000855
856enum chreq_type {
857 CHREQ_T_EMERG_CALL,
858 CHREQ_T_CALL_REEST_TCH_F,
859 CHREQ_T_CALL_REEST_TCH_H,
860 CHREQ_T_CALL_REEST_TCH_H_DBL,
861 CHREQ_T_SDCCH,
862 CHREQ_T_TCH_F,
863 CHREQ_T_VOICE_CALL_TCH_H,
864 CHREQ_T_DATA_CALL_TCH_H,
865 CHREQ_T_LOCATION_UPD,
866 CHREQ_T_PAG_R_ANY,
867 CHREQ_T_PAG_R_TCH_F,
868 CHREQ_T_PAG_R_TCH_FH,
869};
870
871/* Section 9.1.8 / Table 9.9 */
872struct chreq {
873 u_int8_t val;
874 u_int8_t mask;
875 enum chreq_type type;
876};
877
878/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
879static const struct chreq chreq_type_neci1[] = {
880 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
881 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
882 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
883 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
884 { 0xe0, 0xe0, CHREQ_T_SDCCH },
885 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
886 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
887 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
888 { 0x10, 0xf0, CHREQ_T_SDCCH },
889 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
890 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
891 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
892};
893
894/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
895static const struct chreq chreq_type_neci0[] = {
896 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
897 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
898 { 0xe0, 0xe0, CHREQ_T_TCH_F },
899 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
900 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
901 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
902 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
903 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
904};
905
906static const enum gsm_chan_t ctype_by_chreq[] = {
907 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
908 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
909 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
910 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
911 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
912 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
913 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
914 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
915 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
916 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
917 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
918 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
919};
920
Harald Weltee14a57c2008-12-29 04:08:28 +0000921static const enum gsm_chreq_reason_t reason_by_chreq[] = {
922 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
923 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
924 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
925 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
926 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
927 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
928 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
929 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
930 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
931 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
932 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
933 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
934};
935
Harald Welte8470bf22008-12-25 23:28:35 +0000936enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
937{
938 int i;
939 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
940
941 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
942 const struct chreq *chr = &chreq_type_neci0[i];
943 if ((ra & chr->mask) == chr->val)
944 return ctype_by_chreq[chr->type];
945 }
946 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
947 return GSM_LCHAN_SDCCH;
948}
Harald Weltee14a57c2008-12-29 04:08:28 +0000949
950enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
951{
952 int i;
953 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
954
955 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
956 const struct chreq *chr = &chreq_type_neci0[i];
957 if ((ra & chr->mask) == chr->val)
958 return reason_by_chreq[chr->type];
959 }
960 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
961 return GSM_CHREQ_REASON_OTHER;
962}