blob: 00d6ea3f6ad7812421ae0cd662325ef9b6ebf687 [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>
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 Welte0b4c34e2009-02-09 17:54:43 +000042#include <openbsc/paging.h>
Harald Welte52b1f982008-12-23 20:25:15 +000043
Harald Welte8470bf22008-12-25 23:28:35 +000044#define GSM48_ALLOC_SIZE 1024
45#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000046
Harald Welte65e74cc2008-12-29 01:55:35 +000047static int gsm48_tx_simple(struct gsm_lchan *lchan,
48 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +000049static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +000050
Harald Welte52b1f982008-12-23 20:25:15 +000051struct gsm_lai {
52 u_int16_t mcc;
53 u_int16_t mnc;
54 u_int16_t lac;
55};
56
Holger Freyther89824fc2008-12-30 16:18:18 +000057static int authorize_everonye = 0;
58void gsm0408_allow_everyone(int everyone)
59{
60 printf("Allowing everyone?\n");
61 authorize_everonye = everyone;
62}
63
Holger Freythere97f7fb2008-12-31 18:52:11 +000064static int reject_cause = 0;
65void gsm0408_set_reject_cause(int cause)
66{
67 reject_cause = cause;
68}
69
Holger Freyther73487a22008-12-31 18:53:57 +000070static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
71 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +000072{
73 if (!subscriber)
74 return 0;
75
Holger Freyther73487a22008-12-31 18:53:57 +000076 /*
77 * Do not send accept yet as more information should arrive. Some
78 * phones will not send us the information and we will have to check
79 * what we want to do with that.
80 */
81 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
82 return 0;
83
Holger Freyther89824fc2008-12-30 16:18:18 +000084 if (authorize_everonye)
85 return 1;
86
87 return subscriber->authorized;
88}
Holger Freyther07cc8d82008-12-29 06:23:46 +000089
Holger Freyther73487a22008-12-31 18:53:57 +000090static void release_loc_updating_req(struct gsm_lchan *lchan)
91{
Harald Welte179f0642008-12-31 23:59:18 +000092 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +000093 return;
94
95 del_timer(&lchan->loc_operation->updating_timer);
96 free(lchan->loc_operation);
97 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +000098 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +000099}
100
101static void allocate_loc_updating_req(struct gsm_lchan *lchan)
102{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000103 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000104 release_loc_updating_req(lchan);
105
106 lchan->loc_operation = (struct gsm_loc_updating_operation *)
107 malloc(sizeof(*lchan->loc_operation));
108 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
109}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000110
Harald Welte52b1f982008-12-23 20:25:15 +0000111static void to_bcd(u_int8_t *bcd, u_int16_t val)
112{
Harald Welte4b634542008-12-27 01:55:51 +0000113 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000114 val = val / 10;
115 bcd[1] = val % 10;
116 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000117 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000118 val = val / 10;
119}
120
Holger Freyther17746612008-12-28 16:32:44 +0000121void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000122 u_int16_t mnc, u_int16_t lac)
123{
124 u_int8_t bcd[3];
125
126 to_bcd(bcd, mcc);
127 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
128 lai48->digits[1] = bcd[2];
129
130 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000131 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
132#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000133 lai48->digits[1] |= bcd[2] << 4;
134 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000135#else
136 lai48->digits[1] |= 0xf << 4;
137 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
138#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000139
Harald Welte4b634542008-12-27 01:55:51 +0000140 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000141}
142
Harald Welte255539c2008-12-28 02:26:27 +0000143#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000144#define MID_TMSI_LEN (TMSI_LEN + 2)
145
Harald Welte255539c2008-12-28 02:26:27 +0000146int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000147{
Harald Welte65e74cc2008-12-29 01:55:35 +0000148 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000149
Harald Welte4b634542008-12-27 01:55:51 +0000150 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000151 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000152 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000153 *tptr = htonl(tmsi);
154
155 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000156}
157
Holger Freyther819dd202009-01-04 03:52:50 +0000158struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000159{
160 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
161}
162
Holger Freyther3e2c3232009-01-04 03:55:31 +0000163int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000164{
Harald Welte65e74cc2008-12-29 01:55:35 +0000165 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
166
167 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000168 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000169
Harald Welte65e74cc2008-12-29 01:55:35 +0000170 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
171 /* Send a 04.08 call control message, add transaction
172 * ID and TI flag */
173 gh->proto_discr |= msg->lchan->call.transaction_id;
174
175 /* GSM 04.07 Section 11.2.3.1.3 */
176 switch (msg->lchan->call.type) {
177 case GSM_CT_MO:
178 gh->proto_discr |= 0x80;
179 break;
180 case GSM_CT_MT:
181 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000182 case GSM_CT_NONE:
183 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000184 }
185 }
186 }
187
Harald Welte4b634542008-12-27 01:55:51 +0000188 msg->l3h = msg->data;
189
Harald Welte8470bf22008-12-25 23:28:35 +0000190 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000191}
192
Harald Welte52b1f982008-12-23 20:25:15 +0000193
Holger Freyther429e7762008-12-30 13:28:30 +0000194/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000195int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000196{
Harald Welte8470bf22008-12-25 23:28:35 +0000197 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000198 struct gsm48_hdr *gh;
199
Harald Welte8470bf22008-12-25 23:28:35 +0000200 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000201
202 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
203 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000204 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000205 gh->data[0] = cause;
206
Harald Weltedb253af2008-12-30 17:56:55 +0000207 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
208
Harald Welte65e74cc2008-12-29 01:55:35 +0000209 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000210}
211
212/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000213int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000214{
Harald Welte8470bf22008-12-25 23:28:35 +0000215 struct gsm_bts *bts = lchan->ts->trx->bts;
216 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000217 struct gsm48_hdr *gh;
218 struct gsm48_loc_area_id *lai;
219 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000220 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000221
Harald Welte8470bf22008-12-25 23:28:35 +0000222 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000223
224 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
225 gh->proto_discr = GSM48_PDISC_MM;
226 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
227
228 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000229 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000230 bts->network->network_code, bts->location_area_code);
231
232 mid = msgb_put(msg, MID_TMSI_LEN);
233 generate_mid_from_tmsi(mid, tmsi);
234
235 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
236
Harald Weltedb253af2008-12-30 17:56:55 +0000237 ret = gsm48_sendmsg(msg);
238
Harald Welteba4cf162009-01-10 01:49:35 +0000239 ret = gsm48_cc_tx_setup(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000240 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000241
Holger Freyther07cc8d82008-12-29 06:23:46 +0000242 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000243}
244
Harald Weltefc977a82008-12-27 10:19:37 +0000245static char bcd2char(u_int8_t bcd)
246{
247 if (bcd < 0xa)
248 return '0' + bcd;
249 else
250 return 'A' + (bcd - 0xa);
251}
252
Harald Weltebf5e8df2009-02-03 12:59:45 +0000253/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000254static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
255{
256 int i;
257 u_int8_t mi_type;
258 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000259 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000260
261 mi_type = mi[0] & GSM_MI_TYPE_MASK;
262
263 switch (mi_type) {
264 case GSM_MI_TYPE_NONE:
265 break;
266 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000267 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
268 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
269 memcpy(&tmsi, &mi[1], 4);
270 tmsi = ntohl(tmsi);
271 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000272 }
273 break;
274 case GSM_MI_TYPE_IMSI:
275 case GSM_MI_TYPE_IMEI:
276 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000277 *str_cur++ = bcd2char(mi[0] >> 4);
278
279 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000280 if (str_cur + 2 >= string + str_len)
281 return str_cur - string;
282 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000283 /* skip last nibble in last input byte when GSM_EVEN */
284 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
285 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000286 }
287 break;
288 default:
289 break;
290 }
Harald Weltefc977a82008-12-27 10:19:37 +0000291 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000292
Harald Weltefc977a82008-12-27 10:19:37 +0000293 return str_cur - string;
294}
295
Harald Weltebf5e8df2009-02-03 12:59:45 +0000296/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000297static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
298{
299 struct msgb *msg = gsm48_msgb_alloc();
300 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000301
Harald Welte231ad4f2008-12-27 11:15:38 +0000302 msg->lchan = lchan;
303
304 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
305 gh->proto_discr = GSM48_PDISC_MM;
306 gh->msg_type = GSM48_MT_MM_ID_REQ;
307 gh->data[0] = id_type;
308
Harald Welte65e74cc2008-12-29 01:55:35 +0000309 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000310}
311
312#define MI_SIZE 32
313
Harald Weltebf5e8df2009-02-03 12:59:45 +0000314/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000315static int mm_rx_id_resp(struct msgb *msg)
316{
317 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000318 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000319 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
320 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000321 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000322
323 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000324 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000325 mi_type, mi_string);
326
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000327 /*
328 * Rogue messages could trick us but so is life
329 */
330 put_lchan(lchan);
331
Harald Welte75a983f2008-12-27 21:34:06 +0000332 switch (mi_type) {
333 case GSM_MI_TYPE_IMSI:
334 if (!lchan->subscr)
335 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000336 if (lchan->loc_operation)
337 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000338 break;
339 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000340 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000341 /* update subscribe <-> IMEI mapping */
342 if (lchan->subscr)
343 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000344 if (lchan->loc_operation)
345 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000346 break;
347 }
Holger Freyther73487a22008-12-31 18:53:57 +0000348
349 /* Check if we can let the mobile station enter */
350 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
351 db_subscriber_alloc_tmsi(lchan->subscr);
352 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
353 release_loc_updating_req(lchan);
354 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
355 }
356
Harald Welte75a983f2008-12-27 21:34:06 +0000357 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000358}
359
Harald Welte255539c2008-12-28 02:26:27 +0000360
361static void loc_upd_rej_cb(void *data)
362{
363 struct gsm_lchan *lchan = data;
364
Holger Freyther73487a22008-12-31 18:53:57 +0000365 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000366 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000367 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000368}
369
Holger Freytherb7193e42008-12-29 17:44:08 +0000370static void schedule_reject(struct gsm_lchan *lchan)
371{
Holger Freyther73487a22008-12-31 18:53:57 +0000372 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
373 lchan->loc_operation->updating_timer.data = lchan;
374 schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000375}
376
Harald Welte231ad4f2008-12-27 11:15:38 +0000377#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000378/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000379static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000380{
Harald Welte8470bf22008-12-25 23:28:35 +0000381 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000382 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000383 struct gsm48_loc_upd_req *lu;
384 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000385 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000386 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000387 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000388 char mi_string[MI_SIZE];
389 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000390
Harald Welte8470bf22008-12-25 23:28:35 +0000391 lu = (struct gsm48_loc_upd_req *) gh->data;
392
393 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000394
Harald Weltefc977a82008-12-27 10:19:37 +0000395 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
396
Harald Welte231ad4f2008-12-27 11:15:38 +0000397 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000398
399 allocate_loc_updating_req(lchan);
400
Harald Welte52b1f982008-12-23 20:25:15 +0000401 switch (mi_type) {
402 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000403 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000404 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000405 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000406 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000407
Harald Welte52b1f982008-12-23 20:25:15 +0000408 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000409 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000410 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000411 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000412 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000413 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000414 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000415 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000416
Harald Welte52b1f982008-12-23 20:25:15 +0000417 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000418 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000419 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000420 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000421 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000422 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000423 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000424 }
425 break;
426 case GSM_MI_TYPE_IMEI:
427 case GSM_MI_TYPE_IMEISV:
428 /* no sim card... FIXME: what to do ? */
429 fprintf(stderr, "Unimplemented mobile identity type\n");
430 break;
431 default:
432 fprintf(stderr, "Unknown mobile identity type\n");
433 break;
434 }
435
Harald Welte255539c2008-12-28 02:26:27 +0000436 lchan->subscr = subscr;
437
Holger Freyther73487a22008-12-31 18:53:57 +0000438 /*
439 * Schedule the reject timer and check if we can let the
440 * subscriber into our network immediately or if we need to wait
441 * for identity responses.
442 */
443 schedule_reject(lchan);
444 if (!authorize_subscriber(lchan->loc_operation, subscr))
Harald Weltee872cb12009-01-01 00:33:37 +0000445 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000446
Harald Welte75a983f2008-12-27 21:34:06 +0000447 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000448 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000449
Harald Welte255539c2008-12-28 02:26:27 +0000450 tmsi = strtoul(subscr->tmsi, NULL, 10);
451
Holger Freyther73487a22008-12-31 18:53:57 +0000452 release_loc_updating_req(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000453 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000454}
455
Harald Weltedb253af2008-12-30 17:56:55 +0000456/* Section 9.2.15a */
457int gsm48_tx_mm_info(struct gsm_lchan *lchan)
458{
459 struct msgb *msg = gsm48_msgb_alloc();
460 struct gsm48_hdr *gh;
461 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000462 u_int8_t *ptr8;
463 u_int16_t *ptr16;
464 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000465 int i;
466
467 msg->lchan = lchan;
468
469 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
470 gh->proto_discr = GSM48_PDISC_MM;
471 gh->msg_type = GSM48_MT_MM_INFO;
472
473 if (net->name_long) {
474 name_len = strlen(net->name_long);
475 /* 10.5.3.5a */
476 ptr8 = msgb_put(msg, 3);
477 ptr8[0] = GSM48_IE_NAME_LONG;
478 ptr8[1] = name_len*2 +1;
479 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
480
481 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
482 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000483 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000484
485 /* FIXME: Use Cell Broadcast, not UCS-2, since
486 * UCS-2 is only supported by later revisions of the spec */
487 }
488
489 if (net->name_short) {
490 name_len = strlen(net->name_short);
491 /* 10.5.3.5a */
492 ptr8 = (u_int8_t *) msgb_put(msg, 3);
493 ptr8[0] = GSM48_IE_NAME_LONG;
494 ptr8[1] = name_len*2 + 1;
495 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
496
Harald Weltee872cb12009-01-01 00:33:37 +0000497 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000498 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000499 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000500 }
501
502#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000503 /* move back to the top */
504 time_t cur_t;
505 struct tm* cur_time;
506 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000507 /* Section 10.5.3.9 */
508 cur_t = time(NULL);
509 cur_time = gmtime(cur_t);
510 ptr8 = msgb_put(msg, 8);
511 ptr8[0] = GSM48_IE_NET_TIME_TZ;
512 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
513 ptr8[2] = to_bcd8(cur_time->tm_mon);
514 ptr8[3] = to_bcd8(cur_time->tm_mday);
515 ptr8[4] = to_bcd8(cur_time->tm_hour);
516 ptr8[5] = to_bcd8(cur_time->tm_min);
517 ptr8[6] = to_bcd8(cur_time->tm_sec);
518 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
519 tz15min = (cur_time->tm_gmtoff)/(60*15);
520 ptr8[6] = to_bcd8(tz15min);
521 if (tz15min < 0)
522 ptr8[6] |= 0x80;
523#endif
524
525 return gsm48_sendmsg(msg);
526}
527
Harald Welte4b634542008-12-27 01:55:51 +0000528static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
529{
Harald Welte4b634542008-12-27 01:55:51 +0000530 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000531 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000532}
Harald Welteba4cf162009-01-10 01:49:35 +0000533
534/* 9.2.6 CM service reject */
535static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
536 enum gsm48_reject_value value)
537{
538 struct msgb *msg = gsm48_msgb_alloc();
539 struct gsm48_hdr *gh;
540
541 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
542
543 msg->lchan = lchan;
544 use_lchan(lchan);
545
546 gh->proto_discr = GSM48_PDISC_MM;
547 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
548 gh->data[0] = value;
549 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
550
551 return gsm48_sendmsg(msg);
552}
553
Harald Welte4ed0e922009-01-10 03:17:30 +0000554
555/*
556 * Handle CM Service Requests
557 * a) Verify that the packet is long enough to contain the information
558 * we require otherwsie reject with INCORRECT_MESSAGE
559 * b) Try to parse the TMSI. If we do not have one reject
560 * c) Check that we know the subscriber with the TMSI otherwise reject
561 * with a HLR cause
562 * d) Set the subscriber on the gsm_lchan and accept
563 */
Harald Welte4b634542008-12-27 01:55:51 +0000564static int gsm48_rx_mm_serv_req(struct msgb *msg)
565{
Harald Welteba4cf162009-01-10 01:49:35 +0000566 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000567 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000568
Harald Welteba4cf162009-01-10 01:49:35 +0000569 struct gsm_subscriber *subscr;
570 struct gsm48_hdr *gh = msgb_l3(msg);
571 struct gsm48_service_request *req =
572 (struct gsm48_service_request *)gh->data;
573
574 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
575 DEBUGP(DMM, "<- CM SERVICE REQUEST wrong sized message\n");
576 return gsm48_tx_mm_serv_rej(msg->lchan,
577 GSM48_REJECT_INCORRECT_MESSAGE);
578 }
579
580 if (msg->data_len < req->mi_len + 6) {
581 DEBUGP(DMM, "<- CM SERVICE REQUEST MI does not fit in package\n");
582 return gsm48_tx_mm_serv_rej(msg->lchan,
583 GSM48_REJECT_INCORRECT_MESSAGE);
584 }
585
586 mi_type = req->mi[0] & GSM_MI_TYPE_MASK;
587 if (mi_type != GSM_MI_TYPE_TMSI) {
588 DEBUGP(DMM, "<- CM SERVICE REQUEST mi type is not TMSI: %d\n", mi_type);
589 return gsm48_tx_mm_serv_rej(msg->lchan,
590 GSM48_REJECT_INCORRECT_MESSAGE);
591 }
592
Harald Welte4ed0e922009-01-10 03:17:30 +0000593 mi_to_string(mi_string, sizeof(mi_string), req->mi, req->mi_len);
594 subscr = subscr_get_by_tmsi(mi_string);
595 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x mi_type=0x%02x M(%s)\n",
596 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000597
Harald Welte4ed0e922009-01-10 03:17:30 +0000598 if (!subscr)
599 return gsm48_tx_mm_serv_rej(msg->lchan,
600 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
601
602 if (!msg->lchan->subscr)
603 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000604 else if (msg->lchan->subscr != subscr) {
605 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
606 subscr_put(subscr);
607 }
608
Harald Welte4b634542008-12-27 01:55:51 +0000609 return gsm48_tx_mm_serv_ack(msg->lchan);
610}
611
Harald Weltebf5e8df2009-02-03 12:59:45 +0000612/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000613static int gsm0408_rcv_mm(struct msgb *msg)
614{
615 struct gsm48_hdr *gh = msgb_l3(msg);
616 int rc;
617
618 switch (gh->msg_type & 0xbf) {
619 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000620 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000621 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000622 break;
623 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000624 rc = mm_rx_id_resp(msg);
625 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000626 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000627 rc = gsm48_rx_mm_serv_req(msg);
628 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000629 case GSM48_MT_MM_STATUS:
630 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
631 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000632 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000633 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
634 msg->lchan->subscr ?
635 msg->lchan->subscr->imsi :
636 "unknown subscriber");
637 break;
638 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000639 case GSM48_MT_MM_AUTH_RESP:
640 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000641 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
642 gh->msg_type);
643 break;
644 default:
645 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
646 gh->msg_type);
647 break;
648 }
649
650 return rc;
651}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000652
Harald Welte2d35ae62009-02-06 12:02:13 +0000653/* Receive a PAGING RESPONSE message from the MS */
654static int gsm48_rr_rx_pag_resp(struct msgb *msg)
655{
656 struct gsm48_hdr *gh = msgb_l3(msg);
657 struct gsm48_paging_response *pr =
658 (struct gsm48_paging_response *) gh->data;
659 u_int8_t mi_type = pr->mi[0] & GSM_MI_TYPE_MASK;
660 char mi_string[MI_SIZE];
661 struct gsm_subscriber *subscr;
662 int rc = 0;
663
664 mi_to_string(mi_string, sizeof(mi_string), &pr->mi[0], pr->mi_len);
665 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
666 mi_type, mi_string);
667 subscr = subscr_get_by_tmsi(mi_string);
668
669 if (!subscr) {
670 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
671 /* FIXME: close channel? */
672 return -EINVAL;
673 }
674 DEBUGP(DRR, "<- Channel was requested by %s\n",
675 subscr->name ? subscr->name : subscr->imsi);
Harald Welte47150fb2009-02-06 12:52:47 +0000676 page_request_stop(msg->trx->bts, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000677
678 if (!msg->lchan->subscr)
679 msg->lchan->subscr = subscr;
680 else if (msg->lchan->subscr != subscr) {
681 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
682 subscr_put(subscr);
683 }
684
685 return rc;
686}
687
Harald Weltebf5e8df2009-02-03 12:59:45 +0000688/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000689static int gsm0408_rcv_rr(struct msgb *msg)
690{
691 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000692 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000693
694 switch (gh->msg_type) {
695 case GSM48_MT_RR_CLSM_CHG:
696 DEBUGP(DRR, "CLASSMARK CHANGE\n");
697 /* FIXME: what to do ?!? */
698 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000699 case GSM48_MT_RR_GPRS_SUSP_REQ:
700 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
701 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000702 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +0000703 rc = gsm48_rr_rx_pag_resp(msg);
704 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000705 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000706 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000707 gh->msg_type);
708 break;
709 }
710
Harald Welte2d35ae62009-02-06 12:02:13 +0000711 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000712}
713
Holger Freythere64a7a32009-02-06 21:55:37 +0000714/* 7.1.7 and 9.1.7 Channel release*/
715int gsm48_send_rr_release(struct gsm_lchan *lchan)
716{
717 struct msgb *msg = gsm48_msgb_alloc();
718 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
719 u_int8_t *cause;
720
721 msg->lchan = lchan;
722 gh->proto_discr = GSM48_PDISC_RR;
723 gh->msg_type = GSM48_MT_RR_CHAN_REL;
724
725 cause = msgb_put(msg, 1);
726 cause[0] = GSM48_RR_CAUSE_NORMAL;
727
728 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
729 lchan->nr, lchan->type);
730
731 return gsm48_sendmsg(msg);
732}
733
Harald Welte4bc90a12008-12-27 16:32:52 +0000734/* Call Control */
735
Harald Welte4bc90a12008-12-27 16:32:52 +0000736static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
737{
738 struct msgb *msg = gsm48_msgb_alloc();
739 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
740 u_int8_t *cause, *call_state;
741
Harald Welte65e74cc2008-12-29 01:55:35 +0000742 gh->proto_discr = GSM48_PDISC_CC;
743
Harald Welte4bc90a12008-12-27 16:32:52 +0000744 msg->lchan = lchan;
745
Harald Welte4bc90a12008-12-27 16:32:52 +0000746 gh->msg_type = GSM48_MT_CC_STATUS;
747
748 cause = msgb_put(msg, 3);
749 cause[0] = 2;
750 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
751 cause[2] = 0x80 | 30; /* response to status inquiry */
752
753 call_state = msgb_put(msg, 1);
754 call_state[0] = 0xc0 | 0x00;
755
Harald Welte65e74cc2008-12-29 01:55:35 +0000756 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000757}
758
Harald Welte6f4b7532008-12-29 00:39:37 +0000759static int gsm48_tx_simple(struct gsm_lchan *lchan,
760 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000761{
762 struct msgb *msg = gsm48_msgb_alloc();
763 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
764
765 msg->lchan = lchan;
766
Harald Welte6f4b7532008-12-29 00:39:37 +0000767 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000768 gh->msg_type = msg_type;
769
Harald Welte65e74cc2008-12-29 01:55:35 +0000770 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000771}
772
773static int gsm48_cc_rx_status_enq(struct msgb *msg)
774{
775 return gsm48_cc_tx_status(msg->lchan);
776}
777
Holger Freytherca362a62009-01-04 21:05:01 +0000778#if 0
Harald Welte4bc90a12008-12-27 16:32:52 +0000779static int gsm48_cc_rx_setup(struct msgb *msg)
780{
Harald Welte6f4b7532008-12-29 00:39:37 +0000781 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
782 GSM48_MT_CC_CALL_CONF);
Harald Welte4bc90a12008-12-27 16:32:52 +0000783}
Holger Freytherca362a62009-01-04 21:05:01 +0000784#endif
Harald Welte4bc90a12008-12-27 16:32:52 +0000785
Harald Welte65e74cc2008-12-29 01:55:35 +0000786int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
787{
788 struct msgb *msg = gsm48_msgb_alloc();
789 struct gsm48_hdr *gh;
790 struct gsm_call *call = &lchan->call;
791
792 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 8);
793
794 call->type = GSM_CT_MT;
795 msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +0000796 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000797
798 gh->proto_discr = GSM48_PDISC_CC;
799 gh->msg_type = GSM48_MT_CC_SETUP;
800 gh->data[0] = 0x34;
801 gh->data[1] = 0x00;
802 gh->data[2] = 0x5c;
803 gh->data[3] = 0x04;
804 gh->data[4] = 0xb9;
805 gh->data[5] = 0x83;
806 gh->data[6] = 0x32;
807 gh->data[7] = 0x24;
808
809 DEBUGP(DCC, "Sending SETUP\n");
810
811 return gsm48_sendmsg(msg);
812}
813
Harald Welte4bc90a12008-12-27 16:32:52 +0000814static int gsm0408_rcv_cc(struct msgb *msg)
815{
816 struct gsm48_hdr *gh = msgb_l3(msg);
817 u_int8_t msg_type = gh->msg_type & 0xbf;
818 struct gsm_call *call = &msg->lchan->call;
819 int rc = 0;
820
821 switch (msg_type) {
822 case GSM48_MT_CC_CALL_CONF:
823 /* Response to SETUP */
824 DEBUGP(DCC, "CALL CONFIRM\n");
825 break;
826 case GSM48_MT_CC_RELEASE_COMPL:
827 /* Answer from MS to RELEASE */
828 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
829 call->state = GSM_CSTATE_NULL;
830 break;
831 case GSM48_MT_CC_ALERTING:
832 DEBUGP(DCC, "ALERTING\n");
833 break;
834 case GSM48_MT_CC_CONNECT:
835 DEBUGP(DCC, "CONNECT\n");
836 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000837 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
838 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000839 break;
840 case GSM48_MT_CC_CONNECT_ACK:
841 /* MO: Answer to CONNECT */
842 call->state = GSM_CSTATE_ACTIVE;
843 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
844 break;
845 case GSM48_MT_CC_RELEASE:
846 DEBUGP(DCC, "RELEASE\n");
847 /* need to respond with RELEASE_COMPLETE */
848 break;
849 case GSM48_MT_CC_STATUS_ENQ:
850 rc = gsm48_cc_rx_status_enq(msg);
851 break;
852 case GSM48_MT_CC_DISCONNECT:
853 /* Section 5.4.3.2 */
854 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
855 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte498b0bb2009-01-09 21:27:43 +0000856 if (call->state != GSM_CSTATE_NULL)
857 put_lchan(msg->lchan);
Harald Welte4bc90a12008-12-27 16:32:52 +0000858 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000859 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
860 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000861 break;
862 case GSM48_MT_CC_SETUP:
Harald Welte498b0bb2009-01-09 21:27:43 +0000863 if (call->state == GSM_CSTATE_NULL || call->state == GSM_CSTATE_RELEASE_REQ)
864 use_lchan(msg->lchan);
Harald Welte4bc90a12008-12-27 16:32:52 +0000865 call->type = GSM_CT_MO;
866 call->state = GSM_CSTATE_INITIATED;
867 call->transaction_id = gh->proto_discr & 0xf0;
868 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
Harald Welte6f4b7532008-12-29 00:39:37 +0000869 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
870 GSM48_MT_CC_CONNECT);
Harald Welte4bc90a12008-12-27 16:32:52 +0000871 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
872 break;
873 case GSM48_MT_CC_EMERG_SETUP:
874 DEBUGP(DCC, "EMERGENCY SETUP\n");
875 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
876 break;
877 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000878 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +0000879 msg_type);
880 break;
881 }
882
883 return rc;
884}
885
Harald Welte52b1f982008-12-23 20:25:15 +0000886/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
887int gsm0408_rcvmsg(struct msgb *msg)
888{
889 struct gsm48_hdr *gh = msgb_l3(msg);
890 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000891 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000892
893 switch (pdisc) {
894 case GSM48_PDISC_CC:
895 rc = gsm0408_rcv_cc(msg);
896 break;
897 case GSM48_PDISC_MM:
898 rc = gsm0408_rcv_mm(msg);
899 break;
900 case GSM48_PDISC_RR:
901 rc = gsm0408_rcv_rr(msg);
902 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000903 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000904 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000905 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000906 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000907 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000908 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
909 pdisc);
910 break;
911 default:
912 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
913 pdisc);
914 break;
915 }
916
917 return rc;
918}
Harald Welte8470bf22008-12-25 23:28:35 +0000919
920enum chreq_type {
921 CHREQ_T_EMERG_CALL,
922 CHREQ_T_CALL_REEST_TCH_F,
923 CHREQ_T_CALL_REEST_TCH_H,
924 CHREQ_T_CALL_REEST_TCH_H_DBL,
925 CHREQ_T_SDCCH,
926 CHREQ_T_TCH_F,
927 CHREQ_T_VOICE_CALL_TCH_H,
928 CHREQ_T_DATA_CALL_TCH_H,
929 CHREQ_T_LOCATION_UPD,
930 CHREQ_T_PAG_R_ANY,
931 CHREQ_T_PAG_R_TCH_F,
932 CHREQ_T_PAG_R_TCH_FH,
933};
934
935/* Section 9.1.8 / Table 9.9 */
936struct chreq {
937 u_int8_t val;
938 u_int8_t mask;
939 enum chreq_type type;
940};
941
942/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
943static const struct chreq chreq_type_neci1[] = {
944 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
945 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
946 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
947 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
948 { 0xe0, 0xe0, CHREQ_T_SDCCH },
949 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
950 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
951 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
952 { 0x10, 0xf0, CHREQ_T_SDCCH },
953 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
954 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
955 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
956};
957
958/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
959static const struct chreq chreq_type_neci0[] = {
960 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
961 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
962 { 0xe0, 0xe0, CHREQ_T_TCH_F },
963 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
964 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
965 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
966 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
967 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
968};
969
970static const enum gsm_chan_t ctype_by_chreq[] = {
971 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
972 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
973 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
974 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
975 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
976 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
977 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
978 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
979 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
980 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
981 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
982 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
983};
984
Harald Weltee14a57c2008-12-29 04:08:28 +0000985static const enum gsm_chreq_reason_t reason_by_chreq[] = {
986 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
987 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
988 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
989 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
990 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
991 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
992 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
993 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
994 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
995 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
996 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
997 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
998};
999
Harald Welte8470bf22008-12-25 23:28:35 +00001000enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1001{
1002 int i;
1003 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1004
1005 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1006 const struct chreq *chr = &chreq_type_neci0[i];
1007 if ((ra & chr->mask) == chr->val)
1008 return ctype_by_chreq[chr->type];
1009 }
1010 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1011 return GSM_LCHAN_SDCCH;
1012}
Harald Weltee14a57c2008-12-29 04:08:28 +00001013
1014enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1015{
1016 int i;
1017 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1018
1019 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1020 const struct chreq *chr = &chreq_type_neci0[i];
1021 if ((ra & chr->mask) == chr->val)
1022 return reason_by_chreq[chr->type];
1023 }
1024 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1025 return GSM_CHREQ_REASON_OTHER;
1026}