blob: 496d3900c86562499ce6143dd38f2aa135056a83 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/msgb.h>
Harald Welte7584aea2009-02-11 11:44:12 +000035#include <openbsc/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
41#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000042#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000043#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000044#include <openbsc/signal.h>
Harald Welte52b1f982008-12-23 20:25:15 +000045
Harald Welte8470bf22008-12-25 23:28:35 +000046#define GSM48_ALLOC_SIZE 1024
47#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000048
Harald Welte09e38af2009-02-16 22:52:23 +000049static const struct tlv_definition rsl_att_tlvdef = {
50 .def = {
51 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
52 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
53 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
54 [GSM48_IE_UTC] = { TLV_TYPE_TV },
55 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
56 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
57
58 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
59 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
60 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
61 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
62 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
63 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
64 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
65 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
66 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
67 [GSM48_IE_CONN_NUM] = { TLV_TYPE_TLV },
68 [GSM48_IE_CONN_SUBADDR] = { TLV_TYPE_TLV },
69 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
70 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
71 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
72 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
73 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
74 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
75 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
76 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
77 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
78 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
79 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
80 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
81 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
82 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
83 /* FIXME: more elements */
84 },
85};
86
87
Harald Welte65e74cc2008-12-29 01:55:35 +000088static int gsm48_tx_simple(struct gsm_lchan *lchan,
89 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +000090static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +000091
Harald Welte52b1f982008-12-23 20:25:15 +000092struct gsm_lai {
93 u_int16_t mcc;
94 u_int16_t mnc;
95 u_int16_t lac;
96};
97
Holger Freyther89824fc2008-12-30 16:18:18 +000098static int authorize_everonye = 0;
99void gsm0408_allow_everyone(int everyone)
100{
101 printf("Allowing everyone?\n");
102 authorize_everonye = everyone;
103}
104
Holger Freythere97f7fb2008-12-31 18:52:11 +0000105static int reject_cause = 0;
106void gsm0408_set_reject_cause(int cause)
107{
108 reject_cause = cause;
109}
110
Holger Freyther73487a22008-12-31 18:53:57 +0000111static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
112 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000113{
114 if (!subscriber)
115 return 0;
116
Holger Freyther73487a22008-12-31 18:53:57 +0000117 /*
118 * Do not send accept yet as more information should arrive. Some
119 * phones will not send us the information and we will have to check
120 * what we want to do with that.
121 */
122 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
123 return 0;
124
Holger Freyther89824fc2008-12-30 16:18:18 +0000125 if (authorize_everonye)
126 return 1;
127
128 return subscriber->authorized;
129}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000130
Holger Freyther73487a22008-12-31 18:53:57 +0000131static void release_loc_updating_req(struct gsm_lchan *lchan)
132{
Harald Welte179f0642008-12-31 23:59:18 +0000133 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000134 return;
135
136 del_timer(&lchan->loc_operation->updating_timer);
137 free(lchan->loc_operation);
138 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000139 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000140}
141
142static void allocate_loc_updating_req(struct gsm_lchan *lchan)
143{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000144 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000145 release_loc_updating_req(lchan);
146
147 lchan->loc_operation = (struct gsm_loc_updating_operation *)
148 malloc(sizeof(*lchan->loc_operation));
149 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
150}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000151
Harald Welte52b1f982008-12-23 20:25:15 +0000152static void to_bcd(u_int8_t *bcd, u_int16_t val)
153{
Harald Welte4b634542008-12-27 01:55:51 +0000154 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000155 val = val / 10;
156 bcd[1] = val % 10;
157 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000158 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000159 val = val / 10;
160}
161
Holger Freyther17746612008-12-28 16:32:44 +0000162void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000163 u_int16_t mnc, u_int16_t lac)
164{
165 u_int8_t bcd[3];
166
167 to_bcd(bcd, mcc);
168 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
169 lai48->digits[1] = bcd[2];
170
171 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000172 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
173#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000174 lai48->digits[1] |= bcd[2] << 4;
175 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000176#else
177 lai48->digits[1] |= 0xf << 4;
178 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
179#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000180
Harald Welte4b634542008-12-27 01:55:51 +0000181 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000182}
183
Harald Welte255539c2008-12-28 02:26:27 +0000184#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000185#define MID_TMSI_LEN (TMSI_LEN + 2)
186
Harald Welte255539c2008-12-28 02:26:27 +0000187int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000188{
Harald Welte65e74cc2008-12-29 01:55:35 +0000189 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000190
Harald Welte4b634542008-12-27 01:55:51 +0000191 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000192 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000193 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000194 *tptr = htonl(tmsi);
195
196 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000197}
198
Harald Welte09e38af2009-02-16 22:52:23 +0000199static const char bcd_num_digits[] = {
200 '0', '1', '2', '3', '4', '5', '6', '7',
201 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
202};
203
204/* decode a 'called party BCD number' as in 10.5.4.7 */
205u_int8_t decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv)
206{
207 u_int8_t in_len = bcd_lv[0];
208 int i;
209
210 if (in_len < 1)
211 return 0;
212
213 for (i = 2; i <= in_len; i++) {
214 /* lower nibble */
215 output_len--;
216 if (output_len <= 1)
217 break;
218 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
219
220 /* higher nibble */
221 output_len--;
222 if (output_len <= 1)
223 break;
224 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
225 }
226 if (output_len >= 1)
227 *output++ = '\0';
228
229 /* return number type / calling plan */
230 return bcd_lv[1] & 0x3f;
231}
232
233/* convert a single ASCII character to call-control BCD */
234static int asc_to_bcd(const char asc)
235{
236 int i;
237
238 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
239 if (bcd_num_digits[i] == asc)
240 return i;
241 }
242 return -EINVAL;
243}
244
245/* convert a ASCII phone number to 'called party BCD number' */
246int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
247 u_int8_t type, const char *input)
248{
249 int in_len = strlen(input);
250 int i;
251 u_int8_t *bcd_cur = bcd_lv + 2;
252
253 if (in_len/2 + 1 > max_len)
254 return -EIO;
255
256 /* two digits per byte, plus type byte */
257 bcd_lv[0] = in_len/2 + 1;
258 if (in_len % 2)
259 bcd_lv[0]++;
260
261 /* if the caller wants to create a valid 'calling party BCD
262 * number', then the extension bit MUST NOT be set. For the
263 * 'called party BCD number' it MUST be set. *sigh */
264 bcd_lv[1] = type;
265
266 for (i = 0; i < in_len; i++) {
267 int rc = asc_to_bcd(input[i]);
268 if (rc < 0)
269 return rc;
270 if (i % 2 == 0)
271 *bcd_cur = rc;
272 else
273 *bcd_cur++ |= (rc << 4);
274 }
275 /* append padding nibble in case of odd length */
276 if (i % 2)
277 *bcd_cur++ |= 0xf0;
278
279 /* return how many bytes we used */
280 return (bcd_cur - bcd_lv);
281}
282
Holger Freyther819dd202009-01-04 03:52:50 +0000283struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000284{
285 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
286}
287
Holger Freyther3e2c3232009-01-04 03:55:31 +0000288int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000289{
Harald Welte65e74cc2008-12-29 01:55:35 +0000290 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
291
292 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000293 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000294
Harald Welte65e74cc2008-12-29 01:55:35 +0000295 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
296 /* Send a 04.08 call control message, add transaction
297 * ID and TI flag */
298 gh->proto_discr |= msg->lchan->call.transaction_id;
299
300 /* GSM 04.07 Section 11.2.3.1.3 */
301 switch (msg->lchan->call.type) {
302 case GSM_CT_MO:
303 gh->proto_discr |= 0x80;
304 break;
305 case GSM_CT_MT:
306 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000307 case GSM_CT_NONE:
308 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000309 }
310 }
311 }
312
Harald Welte4b634542008-12-27 01:55:51 +0000313 msg->l3h = msg->data;
314
Harald Welte8470bf22008-12-25 23:28:35 +0000315 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000316}
317
Harald Welte52b1f982008-12-23 20:25:15 +0000318
Holger Freyther429e7762008-12-30 13:28:30 +0000319/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000320int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000321{
Harald Welte8470bf22008-12-25 23:28:35 +0000322 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000323 struct gsm48_hdr *gh;
324
Harald Welte8470bf22008-12-25 23:28:35 +0000325 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000326
327 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
328 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000329 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000330 gh->data[0] = cause;
331
Harald Weltedb253af2008-12-30 17:56:55 +0000332 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
333
Harald Welte65e74cc2008-12-29 01:55:35 +0000334 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000335}
336
337/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000338int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000339{
Harald Welte8470bf22008-12-25 23:28:35 +0000340 struct gsm_bts *bts = lchan->ts->trx->bts;
341 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000342 struct gsm48_hdr *gh;
343 struct gsm48_loc_area_id *lai;
344 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000345 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000346
Harald Welte8470bf22008-12-25 23:28:35 +0000347 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000348
349 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
350 gh->proto_discr = GSM48_PDISC_MM;
351 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
352
353 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000354 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000355 bts->network->network_code, bts->location_area_code);
356
357 mid = msgb_put(msg, MID_TMSI_LEN);
358 generate_mid_from_tmsi(mid, tmsi);
359
360 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
361
Harald Weltedb253af2008-12-30 17:56:55 +0000362 ret = gsm48_sendmsg(msg);
363
Harald Weltedb253af2008-12-30 17:56:55 +0000364 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000365
Holger Freyther07cc8d82008-12-29 06:23:46 +0000366 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000367}
368
Harald Weltefc977a82008-12-27 10:19:37 +0000369static char bcd2char(u_int8_t bcd)
370{
371 if (bcd < 0xa)
372 return '0' + bcd;
373 else
374 return 'A' + (bcd - 0xa);
375}
376
Harald Weltebf5e8df2009-02-03 12:59:45 +0000377/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000378static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
379{
380 int i;
381 u_int8_t mi_type;
382 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000383 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000384
385 mi_type = mi[0] & GSM_MI_TYPE_MASK;
386
387 switch (mi_type) {
388 case GSM_MI_TYPE_NONE:
389 break;
390 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000391 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
392 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
393 memcpy(&tmsi, &mi[1], 4);
394 tmsi = ntohl(tmsi);
395 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000396 }
397 break;
398 case GSM_MI_TYPE_IMSI:
399 case GSM_MI_TYPE_IMEI:
400 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000401 *str_cur++ = bcd2char(mi[0] >> 4);
402
403 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000404 if (str_cur + 2 >= string + str_len)
405 return str_cur - string;
406 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000407 /* skip last nibble in last input byte when GSM_EVEN */
408 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
409 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000410 }
411 break;
412 default:
413 break;
414 }
Harald Weltefc977a82008-12-27 10:19:37 +0000415 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000416
Harald Weltefc977a82008-12-27 10:19:37 +0000417 return str_cur - string;
418}
419
Harald Weltebf5e8df2009-02-03 12:59:45 +0000420/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000421static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
422{
423 struct msgb *msg = gsm48_msgb_alloc();
424 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000425
Harald Welte231ad4f2008-12-27 11:15:38 +0000426 msg->lchan = lchan;
427
428 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
429 gh->proto_discr = GSM48_PDISC_MM;
430 gh->msg_type = GSM48_MT_MM_ID_REQ;
431 gh->data[0] = id_type;
432
Harald Welte65e74cc2008-12-29 01:55:35 +0000433 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000434}
435
436#define MI_SIZE 32
437
Harald Weltebf5e8df2009-02-03 12:59:45 +0000438/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000439static int mm_rx_id_resp(struct msgb *msg)
440{
441 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000442 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000443 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
444 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000445 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000446
447 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000448 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000449 mi_type, mi_string);
450
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000451 /*
452 * Rogue messages could trick us but so is life
453 */
454 put_lchan(lchan);
455
Harald Welte75a983f2008-12-27 21:34:06 +0000456 switch (mi_type) {
457 case GSM_MI_TYPE_IMSI:
458 if (!lchan->subscr)
459 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000460 if (lchan->loc_operation)
461 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000462 break;
463 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000464 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000465 /* update subscribe <-> IMEI mapping */
466 if (lchan->subscr)
467 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000468 if (lchan->loc_operation)
469 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000470 break;
471 }
Holger Freyther73487a22008-12-31 18:53:57 +0000472
473 /* Check if we can let the mobile station enter */
474 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
475 db_subscriber_alloc_tmsi(lchan->subscr);
476 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
477 release_loc_updating_req(lchan);
478 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
479 }
480
Harald Welte75a983f2008-12-27 21:34:06 +0000481 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000482}
483
Harald Welte255539c2008-12-28 02:26:27 +0000484
485static void loc_upd_rej_cb(void *data)
486{
487 struct gsm_lchan *lchan = data;
488
Holger Freyther73487a22008-12-31 18:53:57 +0000489 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000490 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000491 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000492}
493
Holger Freytherb7193e42008-12-29 17:44:08 +0000494static void schedule_reject(struct gsm_lchan *lchan)
495{
Holger Freyther73487a22008-12-31 18:53:57 +0000496 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
497 lchan->loc_operation->updating_timer.data = lchan;
498 schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000499}
500
Harald Welte231ad4f2008-12-27 11:15:38 +0000501#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000502/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000503static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000504{
Harald Welte8470bf22008-12-25 23:28:35 +0000505 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000506 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000507 struct gsm48_loc_upd_req *lu;
508 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000509 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000510 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000511 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000512 char mi_string[MI_SIZE];
513 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000514
Harald Welte8470bf22008-12-25 23:28:35 +0000515 lu = (struct gsm48_loc_upd_req *) gh->data;
516
517 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000518
Harald Weltefc977a82008-12-27 10:19:37 +0000519 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
520
Harald Welte231ad4f2008-12-27 11:15:38 +0000521 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000522
523 allocate_loc_updating_req(lchan);
524
Harald Welte52b1f982008-12-23 20:25:15 +0000525 switch (mi_type) {
526 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000527 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000528 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000529 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000530 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000531
Harald Welte52b1f982008-12-23 20:25:15 +0000532 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000533 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000534 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000535 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000536 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000537 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000538 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000539 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000540
Harald Welte52b1f982008-12-23 20:25:15 +0000541 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000542 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000543 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000544 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000545 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000546 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000547 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000548 }
549 break;
550 case GSM_MI_TYPE_IMEI:
551 case GSM_MI_TYPE_IMEISV:
552 /* no sim card... FIXME: what to do ? */
553 fprintf(stderr, "Unimplemented mobile identity type\n");
554 break;
555 default:
556 fprintf(stderr, "Unknown mobile identity type\n");
557 break;
558 }
559
Harald Welte255539c2008-12-28 02:26:27 +0000560 lchan->subscr = subscr;
561
Holger Freyther73487a22008-12-31 18:53:57 +0000562 /*
563 * Schedule the reject timer and check if we can let the
564 * subscriber into our network immediately or if we need to wait
565 * for identity responses.
566 */
567 schedule_reject(lchan);
568 if (!authorize_subscriber(lchan->loc_operation, subscr))
Harald Weltee872cb12009-01-01 00:33:37 +0000569 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000570
Harald Welte75a983f2008-12-27 21:34:06 +0000571 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000572 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000573
Harald Welte255539c2008-12-28 02:26:27 +0000574 tmsi = strtoul(subscr->tmsi, NULL, 10);
575
Holger Freyther73487a22008-12-31 18:53:57 +0000576 release_loc_updating_req(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000577 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000578}
579
Harald Welte7584aea2009-02-11 11:44:12 +0000580/* 9.1.5 Channel mode modify */
581int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
582{
583 struct msgb *msg = gsm48_msgb_alloc();
584 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
585 struct gsm48_chan_mode_modify *cmm =
586 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
587 u_int16_t arfcn = lchan->ts->trx->arfcn;
588
Harald Welte7ccf7782009-02-17 01:43:01 +0000589 DEBUGP(DRR, "-> CHANNEL MODE MODIFY\n");
590
Harald Welte7584aea2009-02-11 11:44:12 +0000591 msg->lchan = lchan;
592 gh->proto_discr = GSM48_PDISC_RR;
593 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
594
595 /* fill the channel information element, this code
596 * should probably be shared with rsl_rx_chan_rqd() */
597 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
598 cmm->chan_desc.h0.h = 0;
599 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
600 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
601 cmm->mode = mode;
602
603 return gsm48_sendmsg(msg);
604}
605
Harald Weltedb253af2008-12-30 17:56:55 +0000606/* Section 9.2.15a */
607int gsm48_tx_mm_info(struct gsm_lchan *lchan)
608{
609 struct msgb *msg = gsm48_msgb_alloc();
610 struct gsm48_hdr *gh;
611 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000612 u_int8_t *ptr8;
613 u_int16_t *ptr16;
614 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000615 int i;
616
617 msg->lchan = lchan;
618
619 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
620 gh->proto_discr = GSM48_PDISC_MM;
621 gh->msg_type = GSM48_MT_MM_INFO;
622
623 if (net->name_long) {
624 name_len = strlen(net->name_long);
625 /* 10.5.3.5a */
626 ptr8 = msgb_put(msg, 3);
627 ptr8[0] = GSM48_IE_NAME_LONG;
628 ptr8[1] = name_len*2 +1;
629 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
630
631 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
632 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000633 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000634
635 /* FIXME: Use Cell Broadcast, not UCS-2, since
636 * UCS-2 is only supported by later revisions of the spec */
637 }
638
639 if (net->name_short) {
640 name_len = strlen(net->name_short);
641 /* 10.5.3.5a */
642 ptr8 = (u_int8_t *) msgb_put(msg, 3);
643 ptr8[0] = GSM48_IE_NAME_LONG;
644 ptr8[1] = name_len*2 + 1;
645 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
646
Harald Weltee872cb12009-01-01 00:33:37 +0000647 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000648 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000649 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000650 }
651
652#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000653 /* move back to the top */
654 time_t cur_t;
655 struct tm* cur_time;
656 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000657 /* Section 10.5.3.9 */
658 cur_t = time(NULL);
659 cur_time = gmtime(cur_t);
660 ptr8 = msgb_put(msg, 8);
661 ptr8[0] = GSM48_IE_NET_TIME_TZ;
662 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
663 ptr8[2] = to_bcd8(cur_time->tm_mon);
664 ptr8[3] = to_bcd8(cur_time->tm_mday);
665 ptr8[4] = to_bcd8(cur_time->tm_hour);
666 ptr8[5] = to_bcd8(cur_time->tm_min);
667 ptr8[6] = to_bcd8(cur_time->tm_sec);
668 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
669 tz15min = (cur_time->tm_gmtoff)/(60*15);
670 ptr8[6] = to_bcd8(tz15min);
671 if (tz15min < 0)
672 ptr8[6] |= 0x80;
673#endif
674
675 return gsm48_sendmsg(msg);
676}
677
Harald Welte4b634542008-12-27 01:55:51 +0000678static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
679{
Harald Welte4b634542008-12-27 01:55:51 +0000680 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000681 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000682}
Harald Welteba4cf162009-01-10 01:49:35 +0000683
684/* 9.2.6 CM service reject */
685static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
686 enum gsm48_reject_value value)
687{
688 struct msgb *msg = gsm48_msgb_alloc();
689 struct gsm48_hdr *gh;
690
691 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
692
693 msg->lchan = lchan;
694 use_lchan(lchan);
695
696 gh->proto_discr = GSM48_PDISC_MM;
697 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
698 gh->data[0] = value;
699 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
700
701 return gsm48_sendmsg(msg);
702}
703
Harald Welte4ed0e922009-01-10 03:17:30 +0000704
705/*
706 * Handle CM Service Requests
707 * a) Verify that the packet is long enough to contain the information
708 * we require otherwsie reject with INCORRECT_MESSAGE
709 * b) Try to parse the TMSI. If we do not have one reject
710 * c) Check that we know the subscriber with the TMSI otherwise reject
711 * with a HLR cause
712 * d) Set the subscriber on the gsm_lchan and accept
713 */
Harald Welte4b634542008-12-27 01:55:51 +0000714static int gsm48_rx_mm_serv_req(struct msgb *msg)
715{
Harald Welteba4cf162009-01-10 01:49:35 +0000716 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000717 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000718
Harald Welteba4cf162009-01-10 01:49:35 +0000719 struct gsm_subscriber *subscr;
720 struct gsm48_hdr *gh = msgb_l3(msg);
721 struct gsm48_service_request *req =
722 (struct gsm48_service_request *)gh->data;
723
724 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
725 DEBUGP(DMM, "<- CM SERVICE REQUEST wrong sized message\n");
726 return gsm48_tx_mm_serv_rej(msg->lchan,
727 GSM48_REJECT_INCORRECT_MESSAGE);
728 }
729
730 if (msg->data_len < req->mi_len + 6) {
731 DEBUGP(DMM, "<- CM SERVICE REQUEST MI does not fit in package\n");
732 return gsm48_tx_mm_serv_rej(msg->lchan,
733 GSM48_REJECT_INCORRECT_MESSAGE);
734 }
735
736 mi_type = req->mi[0] & GSM_MI_TYPE_MASK;
737 if (mi_type != GSM_MI_TYPE_TMSI) {
738 DEBUGP(DMM, "<- CM SERVICE REQUEST mi type is not TMSI: %d\n", mi_type);
739 return gsm48_tx_mm_serv_rej(msg->lchan,
740 GSM48_REJECT_INCORRECT_MESSAGE);
741 }
742
Harald Welte4ed0e922009-01-10 03:17:30 +0000743 mi_to_string(mi_string, sizeof(mi_string), req->mi, req->mi_len);
744 subscr = subscr_get_by_tmsi(mi_string);
745 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x mi_type=0x%02x M(%s)\n",
746 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000747
Harald Welte4ed0e922009-01-10 03:17:30 +0000748 if (!subscr)
749 return gsm48_tx_mm_serv_rej(msg->lchan,
750 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
751
752 if (!msg->lchan->subscr)
753 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000754 else if (msg->lchan->subscr != subscr) {
755 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
756 subscr_put(subscr);
757 }
758
Harald Welte4b634542008-12-27 01:55:51 +0000759 return gsm48_tx_mm_serv_ack(msg->lchan);
760}
761
Harald Weltebf5e8df2009-02-03 12:59:45 +0000762/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000763static int gsm0408_rcv_mm(struct msgb *msg)
764{
765 struct gsm48_hdr *gh = msgb_l3(msg);
766 int rc;
767
768 switch (gh->msg_type & 0xbf) {
769 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000770 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000771 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000772 break;
773 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000774 rc = mm_rx_id_resp(msg);
775 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000776 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000777 rc = gsm48_rx_mm_serv_req(msg);
778 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000779 case GSM48_MT_MM_STATUS:
780 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
781 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000782 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000783 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
784 msg->lchan->subscr ?
785 msg->lchan->subscr->imsi :
786 "unknown subscriber");
787 break;
788 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000789 case GSM48_MT_MM_AUTH_RESP:
790 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000791 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
792 gh->msg_type);
793 break;
794 default:
795 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
796 gh->msg_type);
797 break;
798 }
799
800 return rc;
801}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000802
Harald Welte2d35ae62009-02-06 12:02:13 +0000803/* Receive a PAGING RESPONSE message from the MS */
804static int gsm48_rr_rx_pag_resp(struct msgb *msg)
805{
806 struct gsm48_hdr *gh = msgb_l3(msg);
807 struct gsm48_paging_response *pr =
808 (struct gsm48_paging_response *) gh->data;
809 u_int8_t mi_type = pr->mi[0] & GSM_MI_TYPE_MASK;
810 char mi_string[MI_SIZE];
811 struct gsm_subscriber *subscr;
Harald Welte595ad7b2009-02-16 22:05:44 +0000812 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +0000813 int rc = 0;
814
815 mi_to_string(mi_string, sizeof(mi_string), &pr->mi[0], pr->mi_len);
816 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
817 mi_type, mi_string);
818 subscr = subscr_get_by_tmsi(mi_string);
819
820 if (!subscr) {
821 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000822 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000823 return -EINVAL;
824 }
825 DEBUGP(DRR, "<- Channel was requested by %s\n",
826 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000827
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000828 if (!msg->lchan->subscr)
829 msg->lchan->subscr = subscr;
830 else if (msg->lchan->subscr != subscr) {
831 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
832 subscr_put(subscr);
833 }
834
Harald Welte595ad7b2009-02-16 22:05:44 +0000835 sig_data.subscr = subscr;
836 sig_data.bts = msg->lchan->ts->trx->bts;
837 sig_data.lchan = msg->lchan;
838
839 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Welte7ccf7782009-02-17 01:43:01 +0000840 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +0000841
Harald Welte7584aea2009-02-11 11:44:12 +0000842 /* FIXME: somehow signal the completion of the PAGING to
843 * the entity that requested the paging */
844
Harald Welte2d35ae62009-02-06 12:02:13 +0000845 return rc;
846}
847
Harald Weltebf5e8df2009-02-03 12:59:45 +0000848/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000849static int gsm0408_rcv_rr(struct msgb *msg)
850{
851 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000852 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000853
854 switch (gh->msg_type) {
855 case GSM48_MT_RR_CLSM_CHG:
856 DEBUGP(DRR, "CLASSMARK CHANGE\n");
857 /* FIXME: what to do ?!? */
858 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000859 case GSM48_MT_RR_GPRS_SUSP_REQ:
860 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
861 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000862 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +0000863 rc = gsm48_rr_rx_pag_resp(msg);
864 break;
Harald Welte7ccf7782009-02-17 01:43:01 +0000865 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
866 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
867 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000868 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000869 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000870 gh->msg_type);
871 break;
872 }
873
Harald Welte2d35ae62009-02-06 12:02:13 +0000874 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000875}
876
Holger Freythere64a7a32009-02-06 21:55:37 +0000877/* 7.1.7 and 9.1.7 Channel release*/
878int gsm48_send_rr_release(struct gsm_lchan *lchan)
879{
880 struct msgb *msg = gsm48_msgb_alloc();
881 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
882 u_int8_t *cause;
883
884 msg->lchan = lchan;
885 gh->proto_discr = GSM48_PDISC_RR;
886 gh->msg_type = GSM48_MT_RR_CHAN_REL;
887
888 cause = msgb_put(msg, 1);
889 cause[0] = GSM48_RR_CAUSE_NORMAL;
890
891 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
892 lchan->nr, lchan->type);
893
894 return gsm48_sendmsg(msg);
895}
896
Harald Welte4bc90a12008-12-27 16:32:52 +0000897/* Call Control */
898
Harald Welte7584aea2009-02-11 11:44:12 +0000899/* The entire call control code is written in accordance with Figure 7.10c
900 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
901 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
902 * it for voice */
903
Harald Welte4bc90a12008-12-27 16:32:52 +0000904static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
905{
906 struct msgb *msg = gsm48_msgb_alloc();
907 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
908 u_int8_t *cause, *call_state;
909
Harald Welte65e74cc2008-12-29 01:55:35 +0000910 gh->proto_discr = GSM48_PDISC_CC;
911
Harald Welte4bc90a12008-12-27 16:32:52 +0000912 msg->lchan = lchan;
913
Harald Welte4bc90a12008-12-27 16:32:52 +0000914 gh->msg_type = GSM48_MT_CC_STATUS;
915
916 cause = msgb_put(msg, 3);
917 cause[0] = 2;
918 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
919 cause[2] = 0x80 | 30; /* response to status inquiry */
920
921 call_state = msgb_put(msg, 1);
922 call_state[0] = 0xc0 | 0x00;
923
Harald Welte65e74cc2008-12-29 01:55:35 +0000924 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000925}
926
Harald Welte6f4b7532008-12-29 00:39:37 +0000927static int gsm48_tx_simple(struct gsm_lchan *lchan,
928 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000929{
930 struct msgb *msg = gsm48_msgb_alloc();
931 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
932
933 msg->lchan = lchan;
934
Harald Welte6f4b7532008-12-29 00:39:37 +0000935 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000936 gh->msg_type = msg_type;
937
Harald Welte65e74cc2008-12-29 01:55:35 +0000938 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000939}
940
Harald Welte09e38af2009-02-16 22:52:23 +0000941/* call-back from paging the B-end of the connection */
942static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +0000943 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +0000944{
Harald Welte7ccf7782009-02-17 01:43:01 +0000945 struct gsm_lchan *lchan = _lchan;
Harald Welte09e38af2009-02-16 22:52:23 +0000946 struct gsm_call *call = param;
Harald Welte7ccf7782009-02-17 01:43:01 +0000947 int rc = 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000948
949 if (hooknum != GSM_HOOK_RR_PAGING)
950 return -EINVAL;
951
952 switch (event) {
953 case GSM_PAGING_SUCCEEDED:
Harald Welte7ccf7782009-02-17 01:43:01 +0000954 DEBUGP(DCC, "paging succeeded!\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000955 /* send SETUP request to called party */
Harald Welte7ccf7782009-02-17 01:43:01 +0000956 rc = gsm48_cc_tx_setup(lchan, call->called_subscr, call->subscr);
Harald Welte09e38af2009-02-16 22:52:23 +0000957 break;
958 case GSM_PAGING_EXPIRED:
Harald Welte7ccf7782009-02-17 01:43:01 +0000959 DEBUGP(DCC, "paging expired!\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000960 /* notify caller that we cannot reach called party */
Harald Welte7ccf7782009-02-17 01:43:01 +0000961 /* FIXME: correct cause, etc */
962 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
963 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte09e38af2009-02-16 22:52:23 +0000964 break;
965 }
Harald Welte7ccf7782009-02-17 01:43:01 +0000966 return rc;
Harald Welte09e38af2009-02-16 22:52:23 +0000967}
968
Harald Welte4bc90a12008-12-27 16:32:52 +0000969static int gsm48_cc_rx_status_enq(struct msgb *msg)
970{
971 return gsm48_cc_tx_status(msg->lchan);
972}
973
974static int gsm48_cc_rx_setup(struct msgb *msg)
975{
Harald Welte7584aea2009-02-11 11:44:12 +0000976 struct gsm_call *call = &msg->lchan->call;
977 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte09e38af2009-02-16 22:52:23 +0000978 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Harald Welte7584aea2009-02-11 11:44:12 +0000979 struct gsm_subscriber *called_subscr;
Harald Welte09e38af2009-02-16 22:52:23 +0000980 char called_number[(43-2)*2 + 1] = "\0";
981 struct tlv_parsed tp;
982 u_int8_t num_type;
Harald Welte7584aea2009-02-11 11:44:12 +0000983 int ret;
984
985 if (call->state == GSM_CSTATE_NULL ||
986 call->state == GSM_CSTATE_RELEASE_REQ)
987 use_lchan(msg->lchan);
988
989 call->type = GSM_CT_MO;
990 call->state = GSM_CSTATE_INITIATED;
991 call->transaction_id = gh->proto_discr & 0xf0;
992
Harald Welte09e38af2009-02-16 22:52:23 +0000993 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
994 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
995 goto err;
Harald Welte7584aea2009-02-11 11:44:12 +0000996
Harald Welte09e38af2009-02-16 22:52:23 +0000997 /* Parse the number that was dialed and lookup subscriber */
998 num_type = decode_bcd_number(called_number, sizeof(called_number),
999 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
Harald Welte7ccf7782009-02-17 01:43:01 +00001000
1001 DEBUGP(DCC, "-> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id,
1002 called_number);
1003
Harald Welte09e38af2009-02-16 22:52:23 +00001004 called_subscr = subscr_get_by_extension(called_number);
Harald Welte7584aea2009-02-11 11:44:12 +00001005 if (!called_subscr) {
1006 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
1007 put_lchan(msg->lchan);
1008 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1009 GSM48_MT_CC_RELEASE_COMPL);
1010 }
1011
Harald Welte09e38af2009-02-16 22:52:23 +00001012 subscr_get(msg->lchan->subscr);
1013 call->subscr = msg->lchan->subscr;
1014 call->called_subscr = called_subscr;
1015
Harald Welte7584aea2009-02-11 11:44:12 +00001016 /* start paging of the receiving end of the call */
Harald Welte09e38af2009-02-16 22:52:23 +00001017 paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F,
1018 setup_trig_pag_evt, call);
Harald Welte7584aea2009-02-11 11:44:12 +00001019
1020 /* send a CALL PROCEEDING message to the MO */
1021 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1022 GSM48_MT_CC_CALL_PROC);
1023
1024 /* change TCH/F mode to voice */
1025 return gsm48_tx_chan_mode_modify(msg->lchan, 0x01);
Harald Welte09e38af2009-02-16 22:52:23 +00001026
1027err:
1028 /* FIXME: send some kind of RELEASE */
1029 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001030}
Harald Welte7584aea2009-02-11 11:44:12 +00001031
Harald Welte7ccf7782009-02-17 01:43:01 +00001032static int gsm48_cc_rx_alerting(struct msgb *msg)
1033{
1034 struct gsm_call *call = &msg->lchan->call;
1035 struct gsm_lchan *other_lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001036
Harald Welte7ccf7782009-02-17 01:43:01 +00001037 DEBUGP(DCC, "-> ALERTING\n");
1038
1039 /* forward ALERTING to other party */
1040 other_lchan = lchan_find(msg->trx->bts, call->called_subscr);
1041 if (!other_lchan)
1042 return -EIO;
1043
1044 DEBUGP(DCC, "<- ALERTING\n");
1045 return gsm48_tx_simple(other_lchan, GSM48_PDISC_CC,
1046 GSM48_MT_CC_ALERTING);
1047}
1048
1049static int gsm48_cc_rx_connect(struct msgb *msg)
1050{
1051 struct gsm_call *call = &msg->lchan->call;
1052 struct gsm_lchan *other_lchan;
1053 int rc;
1054
1055 DEBUGP(DCC, "-> CONNECT\n");
1056 DEBUGP(DCC, "<- CONNECT ACK\n");
1057 /* MT+MO: need to respond with CONNECT_ACK and pass on */
1058 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1059 GSM48_MT_CC_CONNECT);
1060
1061 /* forward ALERTING to other party */
1062 other_lchan = lchan_find(msg->trx->bts, call->called_subscr);
1063 if (!other_lchan)
1064 return -EIO;
1065
1066 DEBUGP(DCC, "<- CONNECT\n");
1067 return gsm48_tx_simple(other_lchan, GSM48_PDISC_CC,
1068 GSM48_MT_CC_ALERTING);
1069}
1070
1071static int gsm48_cc_rx_disconnect(struct msgb *msg)
1072{
1073 struct gsm_call *call = &msg->lchan->call;
1074 struct gsm_lchan *other_lchan;
1075 int rc;
1076
1077
1078 /* Section 5.4.3.2 */
1079 DEBUGP(DCC, "-> DISCONNECT (state->RELEASE_REQ)\n");
1080 call->state = GSM_CSTATE_RELEASE_REQ;
1081 if (call->state != GSM_CSTATE_NULL)
1082 put_lchan(msg->lchan);
1083 /* FIXME: clear the network connection */
1084 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1085 GSM48_MT_CC_RELEASE);
1086
1087 /* forward DISCONNECT to other party */
1088 other_lchan = lchan_find(msg->trx->bts, call->called_subscr);
1089 if (!other_lchan)
1090 return -EIO;
1091
1092 DEBUGP(DCC, "<- DISCONNECT\n");
1093 return gsm48_tx_simple(other_lchan, GSM48_PDISC_CC,
1094 GSM48_MT_CC_DISCONNECT);
1095}
1096
1097static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 };
1098
1099int gsm48_cc_tx_setup(struct gsm_lchan *lchan, struct gsm_subscriber *called_subscr,
Harald Welte09e38af2009-02-16 22:52:23 +00001100 struct gsm_subscriber *calling_subscriber)
Harald Welte65e74cc2008-12-29 01:55:35 +00001101{
1102 struct msgb *msg = gsm48_msgb_alloc();
1103 struct gsm48_hdr *gh;
1104 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001105 u_int8_t bcd_lv[19];
Harald Welte65e74cc2008-12-29 01:55:35 +00001106
Harald Welte7ccf7782009-02-17 01:43:01 +00001107 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001108
1109 call->type = GSM_CT_MT;
Harald Welte7ccf7782009-02-17 01:43:01 +00001110 call->subscr = called_subscr;
1111 call->called_subscr = calling_subscriber;
Harald Welte65e74cc2008-12-29 01:55:35 +00001112 msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +00001113 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +00001114
1115 gh->proto_discr = GSM48_PDISC_CC;
1116 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001117
Harald Welte7584aea2009-02-11 11:44:12 +00001118 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
Harald Welte7ccf7782009-02-17 01:43:01 +00001119 if (calling_subscriber) {
1120 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
1121 calling_subscriber->extension);
1122 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
1123 bcd_lv[0], bcd_lv+1);
1124 }
1125 if (call->subscr) {
1126 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
1127 call->subscr->extension);
1128 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD,
1129 bcd_lv[0], bcd_lv+1);
1130 }
Harald Welte65e74cc2008-12-29 01:55:35 +00001131
Harald Welte7ccf7782009-02-17 01:43:01 +00001132 DEBUGP(DCC, "<- SETUP\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001133
1134 return gsm48_sendmsg(msg);
1135}
1136
Harald Welte4bc90a12008-12-27 16:32:52 +00001137static int gsm0408_rcv_cc(struct msgb *msg)
1138{
1139 struct gsm48_hdr *gh = msgb_l3(msg);
1140 u_int8_t msg_type = gh->msg_type & 0xbf;
1141 struct gsm_call *call = &msg->lchan->call;
1142 int rc = 0;
1143
1144 switch (msg_type) {
1145 case GSM48_MT_CC_CALL_CONF:
1146 /* Response to SETUP */
Harald Welte7ccf7782009-02-17 01:43:01 +00001147 DEBUGP(DCC, "-> CALL CONFIRM\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001148 /* FIXME: we now need to MODIFY the channel */
Harald Welte4bc90a12008-12-27 16:32:52 +00001149 break;
1150 case GSM48_MT_CC_RELEASE_COMPL:
1151 /* Answer from MS to RELEASE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001152 DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001153 call->state = GSM_CSTATE_NULL;
1154 break;
1155 case GSM48_MT_CC_ALERTING:
Harald Welte7ccf7782009-02-17 01:43:01 +00001156 rc = gsm48_cc_rx_alerting(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001157 break;
1158 case GSM48_MT_CC_CONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001159 rc = gsm48_cc_rx_connect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001160 break;
1161 case GSM48_MT_CC_CONNECT_ACK:
1162 /* MO: Answer to CONNECT */
1163 call->state = GSM_CSTATE_ACTIVE;
Harald Welte7ccf7782009-02-17 01:43:01 +00001164 DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001165 break;
1166 case GSM48_MT_CC_RELEASE:
Harald Welte7ccf7782009-02-17 01:43:01 +00001167 DEBUGP(DCC, "-> RELEASE\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001168 /* need to respond with RELEASE_COMPLETE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001169 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1170 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001171 break;
1172 case GSM48_MT_CC_STATUS_ENQ:
1173 rc = gsm48_cc_rx_status_enq(msg);
1174 break;
1175 case GSM48_MT_CC_DISCONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001176 rc = gsm48_cc_rx_disconnect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001177 break;
1178 case GSM48_MT_CC_SETUP:
Harald Welte7584aea2009-02-11 11:44:12 +00001179 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001180 break;
1181 case GSM48_MT_CC_EMERG_SETUP:
Harald Welte7ccf7782009-02-17 01:43:01 +00001182 DEBUGP(DCC, "-> EMERGENCY SETUP\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001183 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
1184 break;
1185 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001186 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +00001187 msg_type);
1188 break;
1189 }
1190
1191 return rc;
1192}
1193
Harald Welte52b1f982008-12-23 20:25:15 +00001194/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
1195int gsm0408_rcvmsg(struct msgb *msg)
1196{
1197 struct gsm48_hdr *gh = msgb_l3(msg);
1198 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00001199 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001200
1201 switch (pdisc) {
1202 case GSM48_PDISC_CC:
1203 rc = gsm0408_rcv_cc(msg);
1204 break;
1205 case GSM48_PDISC_MM:
1206 rc = gsm0408_rcv_mm(msg);
1207 break;
1208 case GSM48_PDISC_RR:
1209 rc = gsm0408_rcv_rr(msg);
1210 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001211 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001212 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001213 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001214 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001215 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00001216 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
1217 pdisc);
1218 break;
1219 default:
1220 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
1221 pdisc);
1222 break;
1223 }
1224
1225 return rc;
1226}
Harald Welte8470bf22008-12-25 23:28:35 +00001227
1228enum chreq_type {
1229 CHREQ_T_EMERG_CALL,
1230 CHREQ_T_CALL_REEST_TCH_F,
1231 CHREQ_T_CALL_REEST_TCH_H,
1232 CHREQ_T_CALL_REEST_TCH_H_DBL,
1233 CHREQ_T_SDCCH,
1234 CHREQ_T_TCH_F,
1235 CHREQ_T_VOICE_CALL_TCH_H,
1236 CHREQ_T_DATA_CALL_TCH_H,
1237 CHREQ_T_LOCATION_UPD,
1238 CHREQ_T_PAG_R_ANY,
1239 CHREQ_T_PAG_R_TCH_F,
1240 CHREQ_T_PAG_R_TCH_FH,
1241};
1242
1243/* Section 9.1.8 / Table 9.9 */
1244struct chreq {
1245 u_int8_t val;
1246 u_int8_t mask;
1247 enum chreq_type type;
1248};
1249
1250/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
1251static const struct chreq chreq_type_neci1[] = {
1252 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1253 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
1254 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
1255 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
1256 { 0xe0, 0xe0, CHREQ_T_SDCCH },
1257 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
1258 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1259 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
1260 { 0x10, 0xf0, CHREQ_T_SDCCH },
1261 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1262 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1263 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1264};
1265
1266/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
1267static const struct chreq chreq_type_neci0[] = {
1268 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1269 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
1270 { 0xe0, 0xe0, CHREQ_T_TCH_F },
1271 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1272 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
1273 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1274 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1275 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1276};
1277
1278static const enum gsm_chan_t ctype_by_chreq[] = {
1279 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
1280 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
1281 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
1282 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
1283 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
1284 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
1285 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1286 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1287 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
1288 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
1289 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
1290 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
1291};
1292
Harald Weltee14a57c2008-12-29 04:08:28 +00001293static const enum gsm_chreq_reason_t reason_by_chreq[] = {
1294 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
1295 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
1296 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
1297 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
1298 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
1299 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
1300 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1301 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1302 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
1303 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
1304 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
1305 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
1306};
1307
Harald Welte8470bf22008-12-25 23:28:35 +00001308enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1309{
1310 int i;
1311 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1312
1313 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1314 const struct chreq *chr = &chreq_type_neci0[i];
1315 if ((ra & chr->mask) == chr->val)
1316 return ctype_by_chreq[chr->type];
1317 }
1318 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1319 return GSM_LCHAN_SDCCH;
1320}
Harald Weltee14a57c2008-12-29 04:08:28 +00001321
1322enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1323{
1324 int i;
1325 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1326
1327 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1328 const struct chreq *chr = &chreq_type_neci0[i];
1329 if ((ra & chr->mask) == chr->val)
1330 return reason_by_chreq[chr->type];
1331 }
1332 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1333 return GSM_CHREQ_REASON_OTHER;
1334}