blob: 7e80e2923f14a3af97ecc93a4888be05f97fb5d1 [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>
Harald Welte52b1f982008-12-23 20:25:15 +000044
Harald Welte8470bf22008-12-25 23:28:35 +000045#define GSM48_ALLOC_SIZE 1024
46#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000047
Harald Welte65e74cc2008-12-29 01:55:35 +000048static int gsm48_tx_simple(struct gsm_lchan *lchan,
49 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +000050static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +000051
Harald Welte52b1f982008-12-23 20:25:15 +000052struct gsm_lai {
53 u_int16_t mcc;
54 u_int16_t mnc;
55 u_int16_t lac;
56};
57
Holger Freyther89824fc2008-12-30 16:18:18 +000058static int authorize_everonye = 0;
59void gsm0408_allow_everyone(int everyone)
60{
61 printf("Allowing everyone?\n");
62 authorize_everonye = everyone;
63}
64
Holger Freythere97f7fb2008-12-31 18:52:11 +000065static int reject_cause = 0;
66void gsm0408_set_reject_cause(int cause)
67{
68 reject_cause = cause;
69}
70
Holger Freyther73487a22008-12-31 18:53:57 +000071static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
72 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +000073{
74 if (!subscriber)
75 return 0;
76
Holger Freyther73487a22008-12-31 18:53:57 +000077 /*
78 * Do not send accept yet as more information should arrive. Some
79 * phones will not send us the information and we will have to check
80 * what we want to do with that.
81 */
82 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
83 return 0;
84
Holger Freyther89824fc2008-12-30 16:18:18 +000085 if (authorize_everonye)
86 return 1;
87
88 return subscriber->authorized;
89}
Holger Freyther07cc8d82008-12-29 06:23:46 +000090
Holger Freyther73487a22008-12-31 18:53:57 +000091static void release_loc_updating_req(struct gsm_lchan *lchan)
92{
Harald Welte179f0642008-12-31 23:59:18 +000093 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +000094 return;
95
96 del_timer(&lchan->loc_operation->updating_timer);
97 free(lchan->loc_operation);
98 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +000099 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000100}
101
102static void allocate_loc_updating_req(struct gsm_lchan *lchan)
103{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000104 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000105 release_loc_updating_req(lchan);
106
107 lchan->loc_operation = (struct gsm_loc_updating_operation *)
108 malloc(sizeof(*lchan->loc_operation));
109 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
110}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000111
Harald Welte52b1f982008-12-23 20:25:15 +0000112static void to_bcd(u_int8_t *bcd, u_int16_t val)
113{
Harald Welte4b634542008-12-27 01:55:51 +0000114 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000115 val = val / 10;
116 bcd[1] = val % 10;
117 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000118 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000119 val = val / 10;
120}
121
Holger Freyther17746612008-12-28 16:32:44 +0000122void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000123 u_int16_t mnc, u_int16_t lac)
124{
125 u_int8_t bcd[3];
126
127 to_bcd(bcd, mcc);
128 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
129 lai48->digits[1] = bcd[2];
130
131 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000132 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
133#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000134 lai48->digits[1] |= bcd[2] << 4;
135 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000136#else
137 lai48->digits[1] |= 0xf << 4;
138 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
139#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000140
Harald Welte4b634542008-12-27 01:55:51 +0000141 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000142}
143
Harald Welte255539c2008-12-28 02:26:27 +0000144#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000145#define MID_TMSI_LEN (TMSI_LEN + 2)
146
Harald Welte255539c2008-12-28 02:26:27 +0000147int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000148{
Harald Welte65e74cc2008-12-29 01:55:35 +0000149 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000150
Harald Welte4b634542008-12-27 01:55:51 +0000151 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000152 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000153 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000154 *tptr = htonl(tmsi);
155
156 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000157}
158
Holger Freyther819dd202009-01-04 03:52:50 +0000159struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000160{
161 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
162}
163
Holger Freyther3e2c3232009-01-04 03:55:31 +0000164int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000165{
Harald Welte65e74cc2008-12-29 01:55:35 +0000166 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
167
168 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000169 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000170
Harald Welte65e74cc2008-12-29 01:55:35 +0000171 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
172 /* Send a 04.08 call control message, add transaction
173 * ID and TI flag */
174 gh->proto_discr |= msg->lchan->call.transaction_id;
175
176 /* GSM 04.07 Section 11.2.3.1.3 */
177 switch (msg->lchan->call.type) {
178 case GSM_CT_MO:
179 gh->proto_discr |= 0x80;
180 break;
181 case GSM_CT_MT:
182 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000183 case GSM_CT_NONE:
184 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000185 }
186 }
187 }
188
Harald Welte4b634542008-12-27 01:55:51 +0000189 msg->l3h = msg->data;
190
Harald Welte8470bf22008-12-25 23:28:35 +0000191 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000192}
193
Harald Welte52b1f982008-12-23 20:25:15 +0000194
Holger Freyther429e7762008-12-30 13:28:30 +0000195/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000196int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000197{
Harald Welte8470bf22008-12-25 23:28:35 +0000198 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000199 struct gsm48_hdr *gh;
200
Harald Welte8470bf22008-12-25 23:28:35 +0000201 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000202
203 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
204 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000205 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000206 gh->data[0] = cause;
207
Harald Weltedb253af2008-12-30 17:56:55 +0000208 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
209
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 Weltedb253af2008-12-30 17:56:55 +0000242
Holger Freyther07cc8d82008-12-29 06:23:46 +0000243 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000244}
245
Harald Weltefc977a82008-12-27 10:19:37 +0000246static char bcd2char(u_int8_t bcd)
247{
248 if (bcd < 0xa)
249 return '0' + bcd;
250 else
251 return 'A' + (bcd - 0xa);
252}
253
Harald Weltebf5e8df2009-02-03 12:59:45 +0000254/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000255static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
256{
257 int i;
258 u_int8_t mi_type;
259 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000260 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000261
262 mi_type = mi[0] & GSM_MI_TYPE_MASK;
263
264 switch (mi_type) {
265 case GSM_MI_TYPE_NONE:
266 break;
267 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000268 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
269 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
270 memcpy(&tmsi, &mi[1], 4);
271 tmsi = ntohl(tmsi);
272 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000273 }
274 break;
275 case GSM_MI_TYPE_IMSI:
276 case GSM_MI_TYPE_IMEI:
277 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000278 *str_cur++ = bcd2char(mi[0] >> 4);
279
280 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000281 if (str_cur + 2 >= string + str_len)
282 return str_cur - string;
283 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000284 /* skip last nibble in last input byte when GSM_EVEN */
285 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
286 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000287 }
288 break;
289 default:
290 break;
291 }
Harald Weltefc977a82008-12-27 10:19:37 +0000292 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000293
Harald Weltefc977a82008-12-27 10:19:37 +0000294 return str_cur - string;
295}
296
Harald Weltebf5e8df2009-02-03 12:59:45 +0000297/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000298static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
299{
300 struct msgb *msg = gsm48_msgb_alloc();
301 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000302
Harald Welte231ad4f2008-12-27 11:15:38 +0000303 msg->lchan = lchan;
304
305 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
306 gh->proto_discr = GSM48_PDISC_MM;
307 gh->msg_type = GSM48_MT_MM_ID_REQ;
308 gh->data[0] = id_type;
309
Harald Welte65e74cc2008-12-29 01:55:35 +0000310 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000311}
312
313#define MI_SIZE 32
314
Harald Weltebf5e8df2009-02-03 12:59:45 +0000315/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000316static int mm_rx_id_resp(struct msgb *msg)
317{
318 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000319 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000320 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
321 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000322 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000323
324 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000325 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000326 mi_type, mi_string);
327
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000328 /*
329 * Rogue messages could trick us but so is life
330 */
331 put_lchan(lchan);
332
Harald Welte75a983f2008-12-27 21:34:06 +0000333 switch (mi_type) {
334 case GSM_MI_TYPE_IMSI:
335 if (!lchan->subscr)
336 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000337 if (lchan->loc_operation)
338 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000339 break;
340 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000341 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000342 /* update subscribe <-> IMEI mapping */
343 if (lchan->subscr)
344 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000345 if (lchan->loc_operation)
346 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000347 break;
348 }
Holger Freyther73487a22008-12-31 18:53:57 +0000349
350 /* Check if we can let the mobile station enter */
351 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
352 db_subscriber_alloc_tmsi(lchan->subscr);
353 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
354 release_loc_updating_req(lchan);
355 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
356 }
357
Harald Welte75a983f2008-12-27 21:34:06 +0000358 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000359}
360
Harald Welte255539c2008-12-28 02:26:27 +0000361
362static void loc_upd_rej_cb(void *data)
363{
364 struct gsm_lchan *lchan = data;
365
Holger Freyther73487a22008-12-31 18:53:57 +0000366 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000367 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000368 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000369}
370
Holger Freytherb7193e42008-12-29 17:44:08 +0000371static void schedule_reject(struct gsm_lchan *lchan)
372{
Holger Freyther73487a22008-12-31 18:53:57 +0000373 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
374 lchan->loc_operation->updating_timer.data = lchan;
375 schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000376}
377
Harald Welte231ad4f2008-12-27 11:15:38 +0000378#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000379/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000380static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000381{
Harald Welte8470bf22008-12-25 23:28:35 +0000382 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000383 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000384 struct gsm48_loc_upd_req *lu;
385 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000386 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000387 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000388 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000389 char mi_string[MI_SIZE];
390 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000391
Harald Welte8470bf22008-12-25 23:28:35 +0000392 lu = (struct gsm48_loc_upd_req *) gh->data;
393
394 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000395
Harald Weltefc977a82008-12-27 10:19:37 +0000396 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
397
Harald Welte231ad4f2008-12-27 11:15:38 +0000398 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000399
400 allocate_loc_updating_req(lchan);
401
Harald Welte52b1f982008-12-23 20:25:15 +0000402 switch (mi_type) {
403 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000404 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000405 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000406 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000407 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000408
Harald Welte52b1f982008-12-23 20:25:15 +0000409 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000410 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000411 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000412 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000413 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000414 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000415 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000416 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000417
Harald Welte52b1f982008-12-23 20:25:15 +0000418 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000419 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000420 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000421 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000422 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000423 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000424 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000425 }
426 break;
427 case GSM_MI_TYPE_IMEI:
428 case GSM_MI_TYPE_IMEISV:
429 /* no sim card... FIXME: what to do ? */
430 fprintf(stderr, "Unimplemented mobile identity type\n");
431 break;
432 default:
433 fprintf(stderr, "Unknown mobile identity type\n");
434 break;
435 }
436
Harald Welte255539c2008-12-28 02:26:27 +0000437 lchan->subscr = subscr;
438
Holger Freyther73487a22008-12-31 18:53:57 +0000439 /*
440 * Schedule the reject timer and check if we can let the
441 * subscriber into our network immediately or if we need to wait
442 * for identity responses.
443 */
444 schedule_reject(lchan);
445 if (!authorize_subscriber(lchan->loc_operation, subscr))
Harald Weltee872cb12009-01-01 00:33:37 +0000446 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000447
Harald Welte75a983f2008-12-27 21:34:06 +0000448 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000449 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000450
Harald Welte255539c2008-12-28 02:26:27 +0000451 tmsi = strtoul(subscr->tmsi, NULL, 10);
452
Holger Freyther73487a22008-12-31 18:53:57 +0000453 release_loc_updating_req(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000454 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000455}
456
Harald Welte7584aea2009-02-11 11:44:12 +0000457/* 9.1.5 Channel mode modify */
458int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
459{
460 struct msgb *msg = gsm48_msgb_alloc();
461 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
462 struct gsm48_chan_mode_modify *cmm =
463 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
464 u_int16_t arfcn = lchan->ts->trx->arfcn;
465
466 msg->lchan = lchan;
467 gh->proto_discr = GSM48_PDISC_RR;
468 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
469
470 /* fill the channel information element, this code
471 * should probably be shared with rsl_rx_chan_rqd() */
472 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
473 cmm->chan_desc.h0.h = 0;
474 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
475 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
476 cmm->mode = mode;
477
478 return gsm48_sendmsg(msg);
479}
480
Harald Weltedb253af2008-12-30 17:56:55 +0000481/* Section 9.2.15a */
482int gsm48_tx_mm_info(struct gsm_lchan *lchan)
483{
484 struct msgb *msg = gsm48_msgb_alloc();
485 struct gsm48_hdr *gh;
486 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000487 u_int8_t *ptr8;
488 u_int16_t *ptr16;
489 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000490 int i;
491
492 msg->lchan = lchan;
493
494 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
495 gh->proto_discr = GSM48_PDISC_MM;
496 gh->msg_type = GSM48_MT_MM_INFO;
497
498 if (net->name_long) {
499 name_len = strlen(net->name_long);
500 /* 10.5.3.5a */
501 ptr8 = msgb_put(msg, 3);
502 ptr8[0] = GSM48_IE_NAME_LONG;
503 ptr8[1] = name_len*2 +1;
504 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
505
506 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
507 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000508 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000509
510 /* FIXME: Use Cell Broadcast, not UCS-2, since
511 * UCS-2 is only supported by later revisions of the spec */
512 }
513
514 if (net->name_short) {
515 name_len = strlen(net->name_short);
516 /* 10.5.3.5a */
517 ptr8 = (u_int8_t *) msgb_put(msg, 3);
518 ptr8[0] = GSM48_IE_NAME_LONG;
519 ptr8[1] = name_len*2 + 1;
520 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
521
Harald Weltee872cb12009-01-01 00:33:37 +0000522 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000523 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000524 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000525 }
526
527#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000528 /* move back to the top */
529 time_t cur_t;
530 struct tm* cur_time;
531 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000532 /* Section 10.5.3.9 */
533 cur_t = time(NULL);
534 cur_time = gmtime(cur_t);
535 ptr8 = msgb_put(msg, 8);
536 ptr8[0] = GSM48_IE_NET_TIME_TZ;
537 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
538 ptr8[2] = to_bcd8(cur_time->tm_mon);
539 ptr8[3] = to_bcd8(cur_time->tm_mday);
540 ptr8[4] = to_bcd8(cur_time->tm_hour);
541 ptr8[5] = to_bcd8(cur_time->tm_min);
542 ptr8[6] = to_bcd8(cur_time->tm_sec);
543 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
544 tz15min = (cur_time->tm_gmtoff)/(60*15);
545 ptr8[6] = to_bcd8(tz15min);
546 if (tz15min < 0)
547 ptr8[6] |= 0x80;
548#endif
549
550 return gsm48_sendmsg(msg);
551}
552
Harald Welte4b634542008-12-27 01:55:51 +0000553static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
554{
Harald Welte4b634542008-12-27 01:55:51 +0000555 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000556 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000557}
Harald Welteba4cf162009-01-10 01:49:35 +0000558
559/* 9.2.6 CM service reject */
560static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
561 enum gsm48_reject_value value)
562{
563 struct msgb *msg = gsm48_msgb_alloc();
564 struct gsm48_hdr *gh;
565
566 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
567
568 msg->lchan = lchan;
569 use_lchan(lchan);
570
571 gh->proto_discr = GSM48_PDISC_MM;
572 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
573 gh->data[0] = value;
574 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
575
576 return gsm48_sendmsg(msg);
577}
578
Harald Welte4ed0e922009-01-10 03:17:30 +0000579
580/*
581 * Handle CM Service Requests
582 * a) Verify that the packet is long enough to contain the information
583 * we require otherwsie reject with INCORRECT_MESSAGE
584 * b) Try to parse the TMSI. If we do not have one reject
585 * c) Check that we know the subscriber with the TMSI otherwise reject
586 * with a HLR cause
587 * d) Set the subscriber on the gsm_lchan and accept
588 */
Harald Welte4b634542008-12-27 01:55:51 +0000589static int gsm48_rx_mm_serv_req(struct msgb *msg)
590{
Harald Welteba4cf162009-01-10 01:49:35 +0000591 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000592 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000593
Harald Welteba4cf162009-01-10 01:49:35 +0000594 struct gsm_subscriber *subscr;
595 struct gsm48_hdr *gh = msgb_l3(msg);
596 struct gsm48_service_request *req =
597 (struct gsm48_service_request *)gh->data;
598
599 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
600 DEBUGP(DMM, "<- CM SERVICE REQUEST wrong sized message\n");
601 return gsm48_tx_mm_serv_rej(msg->lchan,
602 GSM48_REJECT_INCORRECT_MESSAGE);
603 }
604
605 if (msg->data_len < req->mi_len + 6) {
606 DEBUGP(DMM, "<- CM SERVICE REQUEST MI does not fit in package\n");
607 return gsm48_tx_mm_serv_rej(msg->lchan,
608 GSM48_REJECT_INCORRECT_MESSAGE);
609 }
610
611 mi_type = req->mi[0] & GSM_MI_TYPE_MASK;
612 if (mi_type != GSM_MI_TYPE_TMSI) {
613 DEBUGP(DMM, "<- CM SERVICE REQUEST mi type is not TMSI: %d\n", mi_type);
614 return gsm48_tx_mm_serv_rej(msg->lchan,
615 GSM48_REJECT_INCORRECT_MESSAGE);
616 }
617
Harald Welte4ed0e922009-01-10 03:17:30 +0000618 mi_to_string(mi_string, sizeof(mi_string), req->mi, req->mi_len);
619 subscr = subscr_get_by_tmsi(mi_string);
620 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x mi_type=0x%02x M(%s)\n",
621 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000622
Harald Welte4ed0e922009-01-10 03:17:30 +0000623 if (!subscr)
624 return gsm48_tx_mm_serv_rej(msg->lchan,
625 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
626
627 if (!msg->lchan->subscr)
628 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000629 else if (msg->lchan->subscr != subscr) {
630 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
631 subscr_put(subscr);
632 }
633
Harald Welte4b634542008-12-27 01:55:51 +0000634 return gsm48_tx_mm_serv_ack(msg->lchan);
635}
636
Harald Weltebf5e8df2009-02-03 12:59:45 +0000637/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000638static int gsm0408_rcv_mm(struct msgb *msg)
639{
640 struct gsm48_hdr *gh = msgb_l3(msg);
641 int rc;
642
643 switch (gh->msg_type & 0xbf) {
644 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000645 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000646 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000647 break;
648 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000649 rc = mm_rx_id_resp(msg);
650 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000651 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000652 rc = gsm48_rx_mm_serv_req(msg);
653 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000654 case GSM48_MT_MM_STATUS:
655 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
656 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000657 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000658 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
659 msg->lchan->subscr ?
660 msg->lchan->subscr->imsi :
661 "unknown subscriber");
662 break;
663 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000664 case GSM48_MT_MM_AUTH_RESP:
665 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000666 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
667 gh->msg_type);
668 break;
669 default:
670 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
671 gh->msg_type);
672 break;
673 }
674
675 return rc;
676}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000677
Harald Welte2d35ae62009-02-06 12:02:13 +0000678/* Receive a PAGING RESPONSE message from the MS */
679static int gsm48_rr_rx_pag_resp(struct msgb *msg)
680{
681 struct gsm48_hdr *gh = msgb_l3(msg);
682 struct gsm48_paging_response *pr =
683 (struct gsm48_paging_response *) gh->data;
684 u_int8_t mi_type = pr->mi[0] & GSM_MI_TYPE_MASK;
685 char mi_string[MI_SIZE];
686 struct gsm_subscriber *subscr;
687 int rc = 0;
688
689 mi_to_string(mi_string, sizeof(mi_string), &pr->mi[0], pr->mi_len);
690 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
691 mi_type, mi_string);
692 subscr = subscr_get_by_tmsi(mi_string);
693
694 if (!subscr) {
695 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
696 /* FIXME: close channel? */
697 return -EINVAL;
698 }
699 DEBUGP(DRR, "<- Channel was requested by %s\n",
700 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther1fd34142009-02-09 23:42:03 +0000701 paging_request_stop(msg->trx->bts, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000702
703 if (!msg->lchan->subscr)
704 msg->lchan->subscr = subscr;
705 else if (msg->lchan->subscr != subscr) {
706 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
707 subscr_put(subscr);
708 }
709
Harald Welte7584aea2009-02-11 11:44:12 +0000710 /* FIXME: somehow signal the completion of the PAGING to
711 * the entity that requested the paging */
712
Harald Welte2d35ae62009-02-06 12:02:13 +0000713 return rc;
714}
715
Harald Weltebf5e8df2009-02-03 12:59:45 +0000716/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000717static int gsm0408_rcv_rr(struct msgb *msg)
718{
719 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000720 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000721
722 switch (gh->msg_type) {
723 case GSM48_MT_RR_CLSM_CHG:
724 DEBUGP(DRR, "CLASSMARK CHANGE\n");
725 /* FIXME: what to do ?!? */
726 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000727 case GSM48_MT_RR_GPRS_SUSP_REQ:
728 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
729 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000730 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +0000731 rc = gsm48_rr_rx_pag_resp(msg);
732 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000733 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000734 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000735 gh->msg_type);
736 break;
737 }
738
Harald Welte2d35ae62009-02-06 12:02:13 +0000739 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000740}
741
Holger Freythere64a7a32009-02-06 21:55:37 +0000742/* 7.1.7 and 9.1.7 Channel release*/
743int gsm48_send_rr_release(struct gsm_lchan *lchan)
744{
745 struct msgb *msg = gsm48_msgb_alloc();
746 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
747 u_int8_t *cause;
748
749 msg->lchan = lchan;
750 gh->proto_discr = GSM48_PDISC_RR;
751 gh->msg_type = GSM48_MT_RR_CHAN_REL;
752
753 cause = msgb_put(msg, 1);
754 cause[0] = GSM48_RR_CAUSE_NORMAL;
755
756 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
757 lchan->nr, lchan->type);
758
759 return gsm48_sendmsg(msg);
760}
761
Harald Welte4bc90a12008-12-27 16:32:52 +0000762/* Call Control */
763
Harald Welte7584aea2009-02-11 11:44:12 +0000764/* The entire call control code is written in accordance with Figure 7.10c
765 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
766 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
767 * it for voice */
768
Harald Welte4bc90a12008-12-27 16:32:52 +0000769static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
770{
771 struct msgb *msg = gsm48_msgb_alloc();
772 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
773 u_int8_t *cause, *call_state;
774
Harald Welte65e74cc2008-12-29 01:55:35 +0000775 gh->proto_discr = GSM48_PDISC_CC;
776
Harald Welte4bc90a12008-12-27 16:32:52 +0000777 msg->lchan = lchan;
778
Harald Welte4bc90a12008-12-27 16:32:52 +0000779 gh->msg_type = GSM48_MT_CC_STATUS;
780
781 cause = msgb_put(msg, 3);
782 cause[0] = 2;
783 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
784 cause[2] = 0x80 | 30; /* response to status inquiry */
785
786 call_state = msgb_put(msg, 1);
787 call_state[0] = 0xc0 | 0x00;
788
Harald Welte65e74cc2008-12-29 01:55:35 +0000789 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000790}
791
Harald Welte6f4b7532008-12-29 00:39:37 +0000792static int gsm48_tx_simple(struct gsm_lchan *lchan,
793 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000794{
795 struct msgb *msg = gsm48_msgb_alloc();
796 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
797
798 msg->lchan = lchan;
799
Harald Welte6f4b7532008-12-29 00:39:37 +0000800 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000801 gh->msg_type = msg_type;
802
Harald Welte65e74cc2008-12-29 01:55:35 +0000803 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000804}
805
806static int gsm48_cc_rx_status_enq(struct msgb *msg)
807{
808 return gsm48_cc_tx_status(msg->lchan);
809}
810
811static int gsm48_cc_rx_setup(struct msgb *msg)
812{
Harald Welte7584aea2009-02-11 11:44:12 +0000813 struct gsm_call *call = &msg->lchan->call;
814 struct gsm48_hdr *gh = msgb_l3(msg);
815 struct gsm_subscriber *called_subscr;
816 int ret;
817
818 if (call->state == GSM_CSTATE_NULL ||
819 call->state == GSM_CSTATE_RELEASE_REQ)
820 use_lchan(msg->lchan);
821
822 call->type = GSM_CT_MO;
823 call->state = GSM_CSTATE_INITIATED;
824 call->transaction_id = gh->proto_discr & 0xf0;
825
826 DEBUGP(DCC, "SETUP(tid=0x%02x)\n", call->transaction_id);
827
828 /* Parse the number that was dialed and lookup subscriber */
829 called_subscr = NULL;
830
831 if (!called_subscr) {
832 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
833 put_lchan(msg->lchan);
834 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
835 GSM48_MT_CC_RELEASE_COMPL);
836 }
837
838 /* start paging of the receiving end of the call */
839 paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F);
840
841 /* send a CALL PROCEEDING message to the MO */
842 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
843 GSM48_MT_CC_CALL_PROC);
844
845 /* change TCH/F mode to voice */
846 return gsm48_tx_chan_mode_modify(msg->lchan, 0x01);
Harald Welte4bc90a12008-12-27 16:32:52 +0000847}
Harald Welte7584aea2009-02-11 11:44:12 +0000848
849static const u_int8_t calling_bcd[] = { 0xb9, 0x83, 0x32, 0x24 };
Harald Welte4bc90a12008-12-27 16:32:52 +0000850
Harald Welte65e74cc2008-12-29 01:55:35 +0000851int gsm48_cc_tx_setup(struct gsm_lchan *lchan)
852{
853 struct msgb *msg = gsm48_msgb_alloc();
854 struct gsm48_hdr *gh;
855 struct gsm_call *call = &lchan->call;
856
Harald Welte7584aea2009-02-11 11:44:12 +0000857 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 4 +
858 sizeof(calling_bcd));
Harald Welte65e74cc2008-12-29 01:55:35 +0000859
860 call->type = GSM_CT_MT;
861 msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +0000862 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000863
864 gh->proto_discr = GSM48_PDISC_CC;
865 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte7584aea2009-02-11 11:44:12 +0000866 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
867 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
868 sizeof(calling_bcd), calling_bcd);
Harald Welte65e74cc2008-12-29 01:55:35 +0000869
870 DEBUGP(DCC, "Sending SETUP\n");
871
872 return gsm48_sendmsg(msg);
873}
874
Harald Welte4bc90a12008-12-27 16:32:52 +0000875static int gsm0408_rcv_cc(struct msgb *msg)
876{
877 struct gsm48_hdr *gh = msgb_l3(msg);
878 u_int8_t msg_type = gh->msg_type & 0xbf;
879 struct gsm_call *call = &msg->lchan->call;
880 int rc = 0;
881
882 switch (msg_type) {
883 case GSM48_MT_CC_CALL_CONF:
884 /* Response to SETUP */
885 DEBUGP(DCC, "CALL CONFIRM\n");
886 break;
887 case GSM48_MT_CC_RELEASE_COMPL:
888 /* Answer from MS to RELEASE */
889 DEBUGP(DCC, "RELEASE COMPLETE (state->NULL)\n");
890 call->state = GSM_CSTATE_NULL;
891 break;
892 case GSM48_MT_CC_ALERTING:
893 DEBUGP(DCC, "ALERTING\n");
894 break;
895 case GSM48_MT_CC_CONNECT:
896 DEBUGP(DCC, "CONNECT\n");
897 /* MT: need to respond with CONNECT_ACK */
Harald Welte6f4b7532008-12-29 00:39:37 +0000898 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
899 GSM48_MT_CC_CONNECT_ACK);
Harald Welte4bc90a12008-12-27 16:32:52 +0000900 break;
901 case GSM48_MT_CC_CONNECT_ACK:
902 /* MO: Answer to CONNECT */
903 call->state = GSM_CSTATE_ACTIVE;
904 DEBUGP(DCC, "CONNECT_ACK (state->ACTIVE)\n");
905 break;
906 case GSM48_MT_CC_RELEASE:
907 DEBUGP(DCC, "RELEASE\n");
908 /* need to respond with RELEASE_COMPLETE */
909 break;
910 case GSM48_MT_CC_STATUS_ENQ:
911 rc = gsm48_cc_rx_status_enq(msg);
912 break;
913 case GSM48_MT_CC_DISCONNECT:
914 /* Section 5.4.3.2 */
915 DEBUGP(DCC, "DISCONNECT (state->RELEASE_REQ)\n");
916 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte498b0bb2009-01-09 21:27:43 +0000917 if (call->state != GSM_CSTATE_NULL)
918 put_lchan(msg->lchan);
Harald Welte4bc90a12008-12-27 16:32:52 +0000919 /* FIXME: clear the network connection */
Harald Welte6f4b7532008-12-29 00:39:37 +0000920 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
921 GSM48_MT_CC_RELEASE);
Harald Welte4bc90a12008-12-27 16:32:52 +0000922 break;
923 case GSM48_MT_CC_SETUP:
Harald Welte7584aea2009-02-11 11:44:12 +0000924 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000925 break;
926 case GSM48_MT_CC_EMERG_SETUP:
927 DEBUGP(DCC, "EMERGENCY SETUP\n");
928 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
929 break;
930 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000931 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +0000932 msg_type);
933 break;
934 }
935
936 return rc;
937}
938
Harald Welte52b1f982008-12-23 20:25:15 +0000939/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
940int gsm0408_rcvmsg(struct msgb *msg)
941{
942 struct gsm48_hdr *gh = msgb_l3(msg);
943 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +0000944 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000945
946 switch (pdisc) {
947 case GSM48_PDISC_CC:
948 rc = gsm0408_rcv_cc(msg);
949 break;
950 case GSM48_PDISC_MM:
951 rc = gsm0408_rcv_mm(msg);
952 break;
953 case GSM48_PDISC_RR:
954 rc = gsm0408_rcv_rr(msg);
955 break;
Harald Weltebcae43f2008-12-27 21:45:37 +0000956 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +0000957 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +0000958 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000959 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +0000960 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +0000961 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
962 pdisc);
963 break;
964 default:
965 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
966 pdisc);
967 break;
968 }
969
970 return rc;
971}
Harald Welte8470bf22008-12-25 23:28:35 +0000972
973enum chreq_type {
974 CHREQ_T_EMERG_CALL,
975 CHREQ_T_CALL_REEST_TCH_F,
976 CHREQ_T_CALL_REEST_TCH_H,
977 CHREQ_T_CALL_REEST_TCH_H_DBL,
978 CHREQ_T_SDCCH,
979 CHREQ_T_TCH_F,
980 CHREQ_T_VOICE_CALL_TCH_H,
981 CHREQ_T_DATA_CALL_TCH_H,
982 CHREQ_T_LOCATION_UPD,
983 CHREQ_T_PAG_R_ANY,
984 CHREQ_T_PAG_R_TCH_F,
985 CHREQ_T_PAG_R_TCH_FH,
986};
987
988/* Section 9.1.8 / Table 9.9 */
989struct chreq {
990 u_int8_t val;
991 u_int8_t mask;
992 enum chreq_type type;
993};
994
995/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
996static const struct chreq chreq_type_neci1[] = {
997 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
998 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
999 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
1000 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
1001 { 0xe0, 0xe0, CHREQ_T_SDCCH },
1002 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
1003 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1004 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
1005 { 0x10, 0xf0, CHREQ_T_SDCCH },
1006 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1007 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1008 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1009};
1010
1011/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
1012static const struct chreq chreq_type_neci0[] = {
1013 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1014 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
1015 { 0xe0, 0xe0, CHREQ_T_TCH_F },
1016 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1017 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
1018 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1019 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1020 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1021};
1022
1023static const enum gsm_chan_t ctype_by_chreq[] = {
1024 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
1025 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
1026 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
1027 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
1028 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
1029 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
1030 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1031 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1032 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
1033 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
1034 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
1035 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
1036};
1037
Harald Weltee14a57c2008-12-29 04:08:28 +00001038static const enum gsm_chreq_reason_t reason_by_chreq[] = {
1039 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
1040 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
1041 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
1042 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
1043 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
1044 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
1045 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1046 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1047 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
1048 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
1049 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
1050 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
1051};
1052
Harald Welte8470bf22008-12-25 23:28:35 +00001053enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1054{
1055 int i;
1056 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1057
1058 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1059 const struct chreq *chr = &chreq_type_neci0[i];
1060 if ((ra & chr->mask) == chr->val)
1061 return ctype_by_chreq[chr->type];
1062 }
1063 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1064 return GSM_LCHAN_SDCCH;
1065}
Harald Weltee14a57c2008-12-29 04:08:28 +00001066
1067enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1068{
1069 int i;
1070 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1071
1072 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1073 const struct chreq *chr = &chreq_type_neci0[i];
1074 if ((ra & chr->mask) == chr->val)
1075 return reason_by_chreq[chr->type];
1076 }
1077 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1078 return GSM_CHREQ_REASON_OTHER;
1079}