blob: aebbd67bd96e77a696515357fca7c7454fea9fa1 [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 Welte45b407a2009-05-23 15:51:12 +000045#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000046#include <openbsc/trau_mux.h>
Harald Welte52b1f982008-12-23 20:25:15 +000047
Harald Welte8470bf22008-12-25 23:28:35 +000048#define GSM48_ALLOC_SIZE 1024
49#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000050
Harald Welte09e38af2009-02-16 22:52:23 +000051static const struct tlv_definition rsl_att_tlvdef = {
52 .def = {
53 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
54 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
55 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
56 [GSM48_IE_UTC] = { TLV_TYPE_TV },
57 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
58 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
59
60 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
61 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
62 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
63 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
64 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
65 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
66 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
67 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
68 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
69 [GSM48_IE_CONN_NUM] = { TLV_TYPE_TLV },
70 [GSM48_IE_CONN_SUBADDR] = { TLV_TYPE_TLV },
71 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
72 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
73 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
74 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
75 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
76 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
77 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
78 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
79 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
80 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
81 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
82 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
83 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
84 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
85 /* FIXME: more elements */
86 },
87};
Harald Weltecf5b3592009-05-01 18:28:42 +000088
89static const char *rr_cause_names[] = {
90 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
91 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
92 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
93 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
94 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
95 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
96 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
97 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
98 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
99 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
100 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
101 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
102 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
103 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
104 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
105 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
106 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
107};
108
109static char strbuf[64];
110
111static const char *rr_cause_name(u_int8_t cause)
112{
113 if (cause < ARRAY_SIZE(rr_cause_names) &&
114 rr_cause_names[cause])
115 return rr_cause_names[cause];
116
117 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
118 return strbuf;
119}
120
Harald Welte65e74cc2008-12-29 01:55:35 +0000121static int gsm48_tx_simple(struct gsm_lchan *lchan,
122 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000123static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000124
Harald Welte52b1f982008-12-23 20:25:15 +0000125struct gsm_lai {
126 u_int16_t mcc;
127 u_int16_t mnc;
128 u_int16_t lac;
129};
130
Holger Freyther89824fc2008-12-30 16:18:18 +0000131static int authorize_everonye = 0;
132void gsm0408_allow_everyone(int everyone)
133{
134 printf("Allowing everyone?\n");
135 authorize_everonye = everyone;
136}
137
Holger Freythere97f7fb2008-12-31 18:52:11 +0000138static int reject_cause = 0;
139void gsm0408_set_reject_cause(int cause)
140{
141 reject_cause = cause;
142}
143
Holger Freyther73487a22008-12-31 18:53:57 +0000144static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
145 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000146{
147 if (!subscriber)
148 return 0;
149
Holger Freyther73487a22008-12-31 18:53:57 +0000150 /*
151 * Do not send accept yet as more information should arrive. Some
152 * phones will not send us the information and we will have to check
153 * what we want to do with that.
154 */
155 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
156 return 0;
157
Holger Freyther89824fc2008-12-30 16:18:18 +0000158 if (authorize_everonye)
159 return 1;
160
161 return subscriber->authorized;
162}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000163
Holger Freyther73487a22008-12-31 18:53:57 +0000164static void release_loc_updating_req(struct gsm_lchan *lchan)
165{
Harald Welte179f0642008-12-31 23:59:18 +0000166 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000167 return;
168
Harald Welteff117a82009-05-23 05:22:08 +0000169 bsc_del_timer(&lchan->loc_operation->updating_timer);
Holger Freyther73487a22008-12-31 18:53:57 +0000170 free(lchan->loc_operation);
171 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000172 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000173}
174
175static void allocate_loc_updating_req(struct gsm_lchan *lchan)
176{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000177 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000178 release_loc_updating_req(lchan);
179
180 lchan->loc_operation = (struct gsm_loc_updating_operation *)
181 malloc(sizeof(*lchan->loc_operation));
182 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
183}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000184
Harald Welte52b1f982008-12-23 20:25:15 +0000185static void to_bcd(u_int8_t *bcd, u_int16_t val)
186{
Harald Welte4b634542008-12-27 01:55:51 +0000187 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000188 val = val / 10;
189 bcd[1] = val % 10;
190 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000191 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000192 val = val / 10;
193}
194
Holger Freyther17746612008-12-28 16:32:44 +0000195void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000196 u_int16_t mnc, u_int16_t lac)
197{
198 u_int8_t bcd[3];
199
200 to_bcd(bcd, mcc);
201 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
202 lai48->digits[1] = bcd[2];
203
204 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000205 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
206#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000207 lai48->digits[1] |= bcd[2] << 4;
208 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000209#else
210 lai48->digits[1] |= 0xf << 4;
211 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
212#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000213
Harald Welte4b634542008-12-27 01:55:51 +0000214 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000215}
216
Harald Welte255539c2008-12-28 02:26:27 +0000217#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000218#define MID_TMSI_LEN (TMSI_LEN + 2)
219
Harald Welte255539c2008-12-28 02:26:27 +0000220int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000221{
Harald Welte65e74cc2008-12-29 01:55:35 +0000222 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000223
Harald Welte4b634542008-12-27 01:55:51 +0000224 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000225 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000226 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000227 *tptr = htonl(tmsi);
228
229 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000230}
231
Harald Welte09e38af2009-02-16 22:52:23 +0000232static const char bcd_num_digits[] = {
233 '0', '1', '2', '3', '4', '5', '6', '7',
234 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
235};
236
237/* decode a 'called party BCD number' as in 10.5.4.7 */
238u_int8_t decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv)
239{
240 u_int8_t in_len = bcd_lv[0];
241 int i;
242
243 if (in_len < 1)
244 return 0;
245
246 for (i = 2; i <= in_len; i++) {
247 /* lower nibble */
248 output_len--;
249 if (output_len <= 1)
250 break;
251 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
252
253 /* higher nibble */
254 output_len--;
255 if (output_len <= 1)
256 break;
257 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
258 }
259 if (output_len >= 1)
260 *output++ = '\0';
261
262 /* return number type / calling plan */
263 return bcd_lv[1] & 0x3f;
264}
265
266/* convert a single ASCII character to call-control BCD */
267static int asc_to_bcd(const char asc)
268{
269 int i;
270
271 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
272 if (bcd_num_digits[i] == asc)
273 return i;
274 }
275 return -EINVAL;
276}
277
278/* convert a ASCII phone number to 'called party BCD number' */
279int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
280 u_int8_t type, const char *input)
281{
282 int in_len = strlen(input);
283 int i;
284 u_int8_t *bcd_cur = bcd_lv + 2;
285
286 if (in_len/2 + 1 > max_len)
287 return -EIO;
288
289 /* two digits per byte, plus type byte */
290 bcd_lv[0] = in_len/2 + 1;
291 if (in_len % 2)
292 bcd_lv[0]++;
293
294 /* if the caller wants to create a valid 'calling party BCD
295 * number', then the extension bit MUST NOT be set. For the
296 * 'called party BCD number' it MUST be set. *sigh */
297 bcd_lv[1] = type;
298
299 for (i = 0; i < in_len; i++) {
300 int rc = asc_to_bcd(input[i]);
301 if (rc < 0)
302 return rc;
303 if (i % 2 == 0)
304 *bcd_cur = rc;
305 else
306 *bcd_cur++ |= (rc << 4);
307 }
308 /* append padding nibble in case of odd length */
309 if (i % 2)
310 *bcd_cur++ |= 0xf0;
311
312 /* return how many bytes we used */
313 return (bcd_cur - bcd_lv);
314}
315
Holger Freyther819dd202009-01-04 03:52:50 +0000316struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000317{
318 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
319}
320
Holger Freyther3e2c3232009-01-04 03:55:31 +0000321int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000322{
Harald Welte65e74cc2008-12-29 01:55:35 +0000323 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
324
325 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000326 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000327
Harald Welte65e74cc2008-12-29 01:55:35 +0000328 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
329 /* Send a 04.08 call control message, add transaction
330 * ID and TI flag */
331 gh->proto_discr |= msg->lchan->call.transaction_id;
332
333 /* GSM 04.07 Section 11.2.3.1.3 */
334 switch (msg->lchan->call.type) {
335 case GSM_CT_MO:
336 gh->proto_discr |= 0x80;
337 break;
338 case GSM_CT_MT:
339 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000340 case GSM_CT_NONE:
341 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000342 }
343 }
344 }
345
Harald Welte4b634542008-12-27 01:55:51 +0000346 msg->l3h = msg->data;
347
Harald Welte8470bf22008-12-25 23:28:35 +0000348 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000349}
350
Harald Welte52b1f982008-12-23 20:25:15 +0000351
Holger Freyther429e7762008-12-30 13:28:30 +0000352/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000353int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000354{
Harald Welte8470bf22008-12-25 23:28:35 +0000355 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000356 struct gsm48_hdr *gh;
357
Harald Welte8470bf22008-12-25 23:28:35 +0000358 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000359
360 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
361 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000362 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000363 gh->data[0] = cause;
364
Harald Weltedb253af2008-12-30 17:56:55 +0000365 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
366
Harald Welte65e74cc2008-12-29 01:55:35 +0000367 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000368}
369
370/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000371int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000372{
Harald Welte8470bf22008-12-25 23:28:35 +0000373 struct gsm_bts *bts = lchan->ts->trx->bts;
374 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000375 struct gsm48_hdr *gh;
376 struct gsm48_loc_area_id *lai;
377 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000378 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000379
Harald Welte8470bf22008-12-25 23:28:35 +0000380 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000381
382 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
383 gh->proto_discr = GSM48_PDISC_MM;
384 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
385
386 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000387 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000388 bts->network->network_code, bts->location_area_code);
389
390 mid = msgb_put(msg, MID_TMSI_LEN);
391 generate_mid_from_tmsi(mid, tmsi);
392
393 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
394
Harald Weltedb253af2008-12-30 17:56:55 +0000395 ret = gsm48_sendmsg(msg);
396
Harald Weltedb253af2008-12-30 17:56:55 +0000397 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000398
Holger Freyther07cc8d82008-12-29 06:23:46 +0000399 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000400}
401
Harald Weltefc977a82008-12-27 10:19:37 +0000402static char bcd2char(u_int8_t bcd)
403{
404 if (bcd < 0xa)
405 return '0' + bcd;
406 else
407 return 'A' + (bcd - 0xa);
408}
409
Harald Weltebf5e8df2009-02-03 12:59:45 +0000410/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000411static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
412{
413 int i;
414 u_int8_t mi_type;
415 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000416 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000417
418 mi_type = mi[0] & GSM_MI_TYPE_MASK;
419
420 switch (mi_type) {
421 case GSM_MI_TYPE_NONE:
422 break;
423 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000424 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
425 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
426 memcpy(&tmsi, &mi[1], 4);
427 tmsi = ntohl(tmsi);
428 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000429 }
430 break;
431 case GSM_MI_TYPE_IMSI:
432 case GSM_MI_TYPE_IMEI:
433 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000434 *str_cur++ = bcd2char(mi[0] >> 4);
435
436 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000437 if (str_cur + 2 >= string + str_len)
438 return str_cur - string;
439 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000440 /* skip last nibble in last input byte when GSM_EVEN */
441 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
442 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000443 }
444 break;
445 default:
446 break;
447 }
Harald Weltefc977a82008-12-27 10:19:37 +0000448 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000449
Harald Weltefc977a82008-12-27 10:19:37 +0000450 return str_cur - string;
451}
452
Harald Weltebf5e8df2009-02-03 12:59:45 +0000453/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000454static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
455{
456 struct msgb *msg = gsm48_msgb_alloc();
457 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000458
Harald Welte231ad4f2008-12-27 11:15:38 +0000459 msg->lchan = lchan;
460
461 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
462 gh->proto_discr = GSM48_PDISC_MM;
463 gh->msg_type = GSM48_MT_MM_ID_REQ;
464 gh->data[0] = id_type;
465
Harald Welte65e74cc2008-12-29 01:55:35 +0000466 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000467}
468
469#define MI_SIZE 32
470
Harald Weltebf5e8df2009-02-03 12:59:45 +0000471/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000472static int mm_rx_id_resp(struct msgb *msg)
473{
474 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000475 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000476 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
477 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000478 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000479
480 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000481 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000482 mi_type, mi_string);
483
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000484 /*
485 * Rogue messages could trick us but so is life
486 */
487 put_lchan(lchan);
488
Harald Welte75a983f2008-12-27 21:34:06 +0000489 switch (mi_type) {
490 case GSM_MI_TYPE_IMSI:
491 if (!lchan->subscr)
492 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000493 if (lchan->loc_operation)
494 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000495 break;
496 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000497 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000498 /* update subscribe <-> IMEI mapping */
499 if (lchan->subscr)
500 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000501 if (lchan->loc_operation)
502 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000503 break;
504 }
Holger Freyther73487a22008-12-31 18:53:57 +0000505
506 /* Check if we can let the mobile station enter */
507 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
508 db_subscriber_alloc_tmsi(lchan->subscr);
509 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
510 release_loc_updating_req(lchan);
511 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
512 }
513
Harald Welte75a983f2008-12-27 21:34:06 +0000514 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000515}
516
Harald Welte255539c2008-12-28 02:26:27 +0000517
518static void loc_upd_rej_cb(void *data)
519{
520 struct gsm_lchan *lchan = data;
521
Holger Freyther73487a22008-12-31 18:53:57 +0000522 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000523 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000524 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000525}
526
Holger Freytherb7193e42008-12-29 17:44:08 +0000527static void schedule_reject(struct gsm_lchan *lchan)
528{
Holger Freyther73487a22008-12-31 18:53:57 +0000529 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
530 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000531 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000532}
533
Harald Welte2a139372009-02-22 21:14:55 +0000534static const char *lupd_name(u_int8_t type)
535{
536 switch (type) {
537 case GSM48_LUPD_NORMAL:
538 return "NORMAL";
539 case GSM48_LUPD_PERIODIC:
540 return "PEROIDOC";
541 case GSM48_LUPD_IMSI_ATT:
542 return "IMSI ATTACH";
543 default:
544 return "UNKNOWN";
545 }
546}
547
Harald Welte231ad4f2008-12-27 11:15:38 +0000548#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000549/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000550static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000551{
Harald Welte8470bf22008-12-25 23:28:35 +0000552 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000553 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000554 struct gsm48_loc_upd_req *lu;
555 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000556 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000557 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000558 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000559 char mi_string[MI_SIZE];
560 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000561
Harald Welte8470bf22008-12-25 23:28:35 +0000562 lu = (struct gsm48_loc_upd_req *) gh->data;
563
564 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000565
Harald Weltefc977a82008-12-27 10:19:37 +0000566 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
567
Holger Freyther79f4ae62009-06-02 03:25:04 +0000568 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s) type=%s\n", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000569 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000570
571 allocate_loc_updating_req(lchan);
572
Harald Welte52b1f982008-12-23 20:25:15 +0000573 switch (mi_type) {
574 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000575 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000576 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +0000577 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000578 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000579
Harald Welte52b1f982008-12-23 20:25:15 +0000580 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000581 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000582 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000583 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000584 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000585 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +0000586 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000587 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000588
Harald Welte52b1f982008-12-23 20:25:15 +0000589 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000590 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000591 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000592 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000593 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000594 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000595 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000596 }
597 break;
598 case GSM_MI_TYPE_IMEI:
599 case GSM_MI_TYPE_IMEISV:
600 /* no sim card... FIXME: what to do ? */
Harald Welte2a139372009-02-22 21:14:55 +0000601 DEBUGP(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000602 break;
603 default:
Harald Welte2a139372009-02-22 21:14:55 +0000604 DEBUGP(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000605 break;
606 }
607
Harald Welte255539c2008-12-28 02:26:27 +0000608 lchan->subscr = subscr;
609
Holger Freyther73487a22008-12-31 18:53:57 +0000610 /*
611 * Schedule the reject timer and check if we can let the
612 * subscriber into our network immediately or if we need to wait
613 * for identity responses.
614 */
615 schedule_reject(lchan);
616 if (!authorize_subscriber(lchan->loc_operation, subscr))
Harald Weltee872cb12009-01-01 00:33:37 +0000617 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000618
Harald Welte75a983f2008-12-27 21:34:06 +0000619 db_subscriber_alloc_tmsi(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000620 subscr_update(subscr, bts, GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welte8470bf22008-12-25 23:28:35 +0000621
Harald Welte255539c2008-12-28 02:26:27 +0000622 tmsi = strtoul(subscr->tmsi, NULL, 10);
623
Holger Freyther73487a22008-12-31 18:53:57 +0000624 release_loc_updating_req(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000625 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000626}
627
Harald Welte7584aea2009-02-11 11:44:12 +0000628/* 9.1.5 Channel mode modify */
629int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
630{
631 struct msgb *msg = gsm48_msgb_alloc();
632 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
633 struct gsm48_chan_mode_modify *cmm =
634 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +0000635 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +0000636
Harald Welte4a543e82009-02-28 13:17:55 +0000637 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +0000638
Harald Welte45b407a2009-05-23 15:51:12 +0000639 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +0000640 msg->lchan = lchan;
641 gh->proto_discr = GSM48_PDISC_RR;
642 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
643
644 /* fill the channel information element, this code
645 * should probably be shared with rsl_rx_chan_rqd() */
646 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +0000647 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +0000648 cmm->chan_desc.h0.h = 0;
649 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
650 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
651 cmm->mode = mode;
652
653 return gsm48_sendmsg(msg);
654}
655
Harald Weltedb253af2008-12-30 17:56:55 +0000656/* Section 9.2.15a */
657int gsm48_tx_mm_info(struct gsm_lchan *lchan)
658{
659 struct msgb *msg = gsm48_msgb_alloc();
660 struct gsm48_hdr *gh;
661 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000662 u_int8_t *ptr8;
663 u_int16_t *ptr16;
664 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000665 int i;
666
667 msg->lchan = lchan;
668
669 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
670 gh->proto_discr = GSM48_PDISC_MM;
671 gh->msg_type = GSM48_MT_MM_INFO;
672
673 if (net->name_long) {
674 name_len = strlen(net->name_long);
675 /* 10.5.3.5a */
676 ptr8 = msgb_put(msg, 3);
677 ptr8[0] = GSM48_IE_NAME_LONG;
678 ptr8[1] = name_len*2 +1;
679 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
680
681 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
682 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000683 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000684
685 /* FIXME: Use Cell Broadcast, not UCS-2, since
686 * UCS-2 is only supported by later revisions of the spec */
687 }
688
689 if (net->name_short) {
690 name_len = strlen(net->name_short);
691 /* 10.5.3.5a */
692 ptr8 = (u_int8_t *) msgb_put(msg, 3);
693 ptr8[0] = GSM48_IE_NAME_LONG;
694 ptr8[1] = name_len*2 + 1;
695 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
696
Harald Weltee872cb12009-01-01 00:33:37 +0000697 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000698 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000699 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000700 }
701
702#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000703 /* move back to the top */
704 time_t cur_t;
705 struct tm* cur_time;
706 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000707 /* Section 10.5.3.9 */
708 cur_t = time(NULL);
709 cur_time = gmtime(cur_t);
710 ptr8 = msgb_put(msg, 8);
711 ptr8[0] = GSM48_IE_NET_TIME_TZ;
712 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
713 ptr8[2] = to_bcd8(cur_time->tm_mon);
714 ptr8[3] = to_bcd8(cur_time->tm_mday);
715 ptr8[4] = to_bcd8(cur_time->tm_hour);
716 ptr8[5] = to_bcd8(cur_time->tm_min);
717 ptr8[6] = to_bcd8(cur_time->tm_sec);
718 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
719 tz15min = (cur_time->tm_gmtoff)/(60*15);
720 ptr8[6] = to_bcd8(tz15min);
721 if (tz15min < 0)
722 ptr8[6] |= 0x80;
723#endif
724
725 return gsm48_sendmsg(msg);
726}
727
Harald Welte4b634542008-12-27 01:55:51 +0000728static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
729{
Harald Welte4b634542008-12-27 01:55:51 +0000730 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000731 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000732}
Harald Welteba4cf162009-01-10 01:49:35 +0000733
734/* 9.2.6 CM service reject */
735static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
736 enum gsm48_reject_value value)
737{
738 struct msgb *msg = gsm48_msgb_alloc();
739 struct gsm48_hdr *gh;
740
741 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
742
743 msg->lchan = lchan;
744 use_lchan(lchan);
745
746 gh->proto_discr = GSM48_PDISC_MM;
747 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
748 gh->data[0] = value;
749 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
750
751 return gsm48_sendmsg(msg);
752}
753
Harald Welte4ed0e922009-01-10 03:17:30 +0000754
755/*
756 * Handle CM Service Requests
757 * a) Verify that the packet is long enough to contain the information
758 * we require otherwsie reject with INCORRECT_MESSAGE
759 * b) Try to parse the TMSI. If we do not have one reject
760 * c) Check that we know the subscriber with the TMSI otherwise reject
761 * with a HLR cause
762 * d) Set the subscriber on the gsm_lchan and accept
763 */
Harald Welte4b634542008-12-27 01:55:51 +0000764static int gsm48_rx_mm_serv_req(struct msgb *msg)
765{
Harald Welteba4cf162009-01-10 01:49:35 +0000766 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000767 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000768
Harald Welteba4cf162009-01-10 01:49:35 +0000769 struct gsm_subscriber *subscr;
770 struct gsm48_hdr *gh = msgb_l3(msg);
771 struct gsm48_service_request *req =
772 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +0000773 /* unfortunately in Phase1 the classmar2 length is variable */
774 u_int8_t classmark2_len = gh->data[1];
775 u_int8_t *classmark2 = gh->data+2;
776 u_int8_t mi_len = *(classmark2 + classmark2_len);
777 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000778
Harald Weltec9e02182009-05-01 19:07:53 +0000779 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000780 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000781 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000782 return gsm48_tx_mm_serv_rej(msg->lchan,
783 GSM48_REJECT_INCORRECT_MESSAGE);
784 }
785
786 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000787 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000788 return gsm48_tx_mm_serv_rej(msg->lchan,
789 GSM48_REJECT_INCORRECT_MESSAGE);
790 }
791
Harald Weltec9e02182009-05-01 19:07:53 +0000792 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000793 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000794 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +0000795 return gsm48_tx_mm_serv_rej(msg->lchan,
796 GSM48_REJECT_INCORRECT_MESSAGE);
797 }
798
Harald Weltec9e02182009-05-01 19:07:53 +0000799 mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000800 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000801 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000802
Holger Freythereb443982009-06-04 13:58:42 +0000803 subscr = subscr_get_by_tmsi(mi_string);
804
Harald Welte2a139372009-02-22 21:14:55 +0000805 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000806 if (!subscr)
807 return gsm48_tx_mm_serv_rej(msg->lchan,
808 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
809
810 if (!msg->lchan->subscr)
811 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000812 else if (msg->lchan->subscr != subscr) {
813 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
814 subscr_put(subscr);
815 }
816
Harald Welte4b634542008-12-27 01:55:51 +0000817 return gsm48_tx_mm_serv_ack(msg->lchan);
818}
819
Harald Welte2a139372009-02-22 21:14:55 +0000820static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
821{
822 struct gsm48_hdr *gh = msgb_l3(msg);
823 struct gsm48_imsi_detach_ind *idi =
824 (struct gsm48_imsi_detach_ind *) gh->data;
825 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
826 char mi_string[MI_SIZE];
827 struct gsm_subscriber *subscr;
828
829 mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
830 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
831 mi_type, mi_string);
832
833 switch (mi_type) {
834 case GSM_MI_TYPE_TMSI:
835 subscr = subscr_get_by_tmsi(mi_string);
836 break;
837 case GSM_MI_TYPE_IMSI:
838 subscr = subscr_get_by_imsi(mi_string);
839 break;
840 case GSM_MI_TYPE_IMEI:
841 case GSM_MI_TYPE_IMEISV:
842 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000843 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000844 break;
845 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000846 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000847 break;
848 }
849
Holger Freyther4a49e772009-04-12 05:37:29 +0000850 if (subscr) {
851 subscr_update(subscr, msg->trx->bts,
852 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +0000853 DEBUGP(DMM, "Subscriber: %s\n",
854 subscr->name ? subscr->name : subscr->imsi);
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000855 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000856 } else
Harald Welte2a139372009-02-22 21:14:55 +0000857 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
858
859 put_lchan(msg->lchan);
860
861 return 0;
862}
863
Harald Welted2a7f5a2009-06-05 20:08:20 +0000864static int gsm48_rx_mm_status(struct msgb *msg)
865{
866 struct gsm48_hdr *gh = msgb_l3(msg);
867
868 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
869
870 return 0;
871}
872
Harald Weltebf5e8df2009-02-03 12:59:45 +0000873/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000874static int gsm0408_rcv_mm(struct msgb *msg)
875{
876 struct gsm48_hdr *gh = msgb_l3(msg);
877 int rc;
878
879 switch (gh->msg_type & 0xbf) {
880 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000881 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000882 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000883 break;
884 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000885 rc = mm_rx_id_resp(msg);
886 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000887 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000888 rc = gsm48_rx_mm_serv_req(msg);
889 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000890 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000891 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000892 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000893 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000894 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
895 msg->lchan->subscr ?
896 msg->lchan->subscr->imsi :
897 "unknown subscriber");
898 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000899 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000900 rc = gsm48_rx_mm_imsi_detach_ind(msg);
901 break;
902 case GSM48_MT_MM_CM_REEST_REQ:
903 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
904 break;
905 case GSM48_MT_MM_AUTH_RESP:
906 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000907 break;
908 default:
909 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
910 gh->msg_type);
911 break;
912 }
913
914 return rc;
915}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000916
Harald Welte2d35ae62009-02-06 12:02:13 +0000917/* Receive a PAGING RESPONSE message from the MS */
918static int gsm48_rr_rx_pag_resp(struct msgb *msg)
919{
920 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +0000921 u_int8_t *classmark2_lv = gh->data + 1;
922 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
923 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +0000924 char mi_string[MI_SIZE];
925 struct gsm_subscriber *subscr;
Harald Welte595ad7b2009-02-16 22:05:44 +0000926 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +0000927 int rc = 0;
928
Harald Welte61548982009-02-22 21:26:29 +0000929 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +0000930 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
931 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000932 switch (mi_type) {
933 case GSM_MI_TYPE_TMSI:
934 subscr = subscr_get_by_tmsi(mi_string);
935 break;
936 case GSM_MI_TYPE_IMSI:
937 subscr = subscr_get_by_imsi(mi_string);
938 break;
939 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000940
941 if (!subscr) {
942 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000943 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000944 return -EINVAL;
945 }
946 DEBUGP(DRR, "<- Channel was requested by %s\n",
947 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000948
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000949 if (!msg->lchan->subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000950 msg->lchan->subscr = subscr;
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000951 } else if (msg->lchan->subscr != subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000952 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
953 subscr_put(subscr);
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000954 return -EINVAL;
955 } else {
956 DEBUGP(DRR, "<- Channel already owned by us\n");
957 subscr_put(subscr);
958 subscr = msg->lchan->subscr;
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000959 }
960
Harald Welte595ad7b2009-02-16 22:05:44 +0000961 sig_data.subscr = subscr;
962 sig_data.bts = msg->lchan->ts->trx->bts;
963 sig_data.lchan = msg->lchan;
964
965 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Welte7ccf7782009-02-17 01:43:01 +0000966 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +0000967
Harald Welte7584aea2009-02-11 11:44:12 +0000968 /* FIXME: somehow signal the completion of the PAGING to
969 * the entity that requested the paging */
970
Harald Welte2d35ae62009-02-06 12:02:13 +0000971 return rc;
972}
973
Harald Weltecf5b3592009-05-01 18:28:42 +0000974static int gsm48_rx_rr_status(struct msgb *msg)
975{
976 struct gsm48_hdr *gh = msgb_l3(msg);
977
978 DEBUGP(DRR, "STATUS rr_cause = %s\n",
979 rr_cause_name(gh->data[0]));
980
981 return 0;
982}
983
Harald Weltebf5e8df2009-02-03 12:59:45 +0000984/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000985static int gsm0408_rcv_rr(struct msgb *msg)
986{
987 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000988 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000989
990 switch (gh->msg_type) {
991 case GSM48_MT_RR_CLSM_CHG:
992 DEBUGP(DRR, "CLASSMARK CHANGE\n");
993 /* FIXME: what to do ?!? */
994 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000995 case GSM48_MT_RR_GPRS_SUSP_REQ:
996 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
997 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000998 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +0000999 rc = gsm48_rr_rx_pag_resp(msg);
1000 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001001 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1002 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +00001003 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001004 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001005 case GSM48_MT_RR_STATUS:
1006 rc = gsm48_rx_rr_status(msg);
1007 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001008 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001009 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001010 gh->msg_type);
1011 break;
1012 }
1013
Harald Welte2d35ae62009-02-06 12:02:13 +00001014 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001015}
1016
Holger Freythere64a7a32009-02-06 21:55:37 +00001017/* 7.1.7 and 9.1.7 Channel release*/
1018int gsm48_send_rr_release(struct gsm_lchan *lchan)
1019{
1020 struct msgb *msg = gsm48_msgb_alloc();
1021 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1022 u_int8_t *cause;
1023
1024 msg->lchan = lchan;
1025 gh->proto_discr = GSM48_PDISC_RR;
1026 gh->msg_type = GSM48_MT_RR_CHAN_REL;
1027
1028 cause = msgb_put(msg, 1);
1029 cause[0] = GSM48_RR_CAUSE_NORMAL;
1030
1031 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
1032 lchan->nr, lchan->type);
1033
1034 return gsm48_sendmsg(msg);
1035}
1036
Harald Welte4bc90a12008-12-27 16:32:52 +00001037/* Call Control */
1038
Harald Welte7584aea2009-02-11 11:44:12 +00001039/* The entire call control code is written in accordance with Figure 7.10c
1040 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1041 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1042 * it for voice */
1043
Harald Welte4bc90a12008-12-27 16:32:52 +00001044static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
1045{
1046 struct msgb *msg = gsm48_msgb_alloc();
1047 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1048 u_int8_t *cause, *call_state;
1049
Harald Welte65e74cc2008-12-29 01:55:35 +00001050 gh->proto_discr = GSM48_PDISC_CC;
1051
Harald Welte4bc90a12008-12-27 16:32:52 +00001052 msg->lchan = lchan;
1053
Harald Welte4bc90a12008-12-27 16:32:52 +00001054 gh->msg_type = GSM48_MT_CC_STATUS;
1055
1056 cause = msgb_put(msg, 3);
1057 cause[0] = 2;
1058 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1059 cause[2] = 0x80 | 30; /* response to status inquiry */
1060
1061 call_state = msgb_put(msg, 1);
1062 call_state[0] = 0xc0 | 0x00;
1063
Harald Welte65e74cc2008-12-29 01:55:35 +00001064 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001065}
1066
Harald Welte6f4b7532008-12-29 00:39:37 +00001067static int gsm48_tx_simple(struct gsm_lchan *lchan,
1068 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001069{
1070 struct msgb *msg = gsm48_msgb_alloc();
1071 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1072
1073 msg->lchan = lchan;
1074
Harald Welte6f4b7532008-12-29 00:39:37 +00001075 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001076 gh->msg_type = msg_type;
1077
Harald Welte65e74cc2008-12-29 01:55:35 +00001078 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001079}
1080
Harald Welte09e38af2009-02-16 22:52:23 +00001081/* call-back from paging the B-end of the connection */
1082static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001083 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001084{
Harald Welte7ccf7782009-02-17 01:43:01 +00001085 struct gsm_lchan *lchan = _lchan;
Harald Welte49f48b82009-02-17 15:29:33 +00001086 struct gsm_call *remote_call = param;
1087 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001088 int rc = 0;
Harald Welte09e38af2009-02-16 22:52:23 +00001089
1090 if (hooknum != GSM_HOOK_RR_PAGING)
1091 return -EINVAL;
1092
1093 switch (event) {
1094 case GSM_PAGING_SUCCEEDED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001095 DEBUGP(DCC, "paging succeeded!\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001096 remote_call->remote_lchan = lchan;
1097 call->remote_lchan = remote_call->local_lchan;
Harald Welte09e38af2009-02-16 22:52:23 +00001098 /* send SETUP request to called party */
Harald Welte49f48b82009-02-17 15:29:33 +00001099 rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr);
1100 if (is_ipaccess_bts(lchan->ts->trx->bts))
1101 rsl_ipacc_bind(lchan);
Harald Welte09e38af2009-02-16 22:52:23 +00001102 break;
1103 case GSM_PAGING_EXPIRED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001104 DEBUGP(DCC, "paging expired!\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001105 /* notify caller that we cannot reach called party */
Harald Welte7ccf7782009-02-17 01:43:01 +00001106 /* FIXME: correct cause, etc */
Harald Weltebfaf0972009-02-17 17:43:33 +00001107 rc = gsm48_tx_simple(remote_call->local_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001108 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte09e38af2009-02-16 22:52:23 +00001109 break;
1110 }
Harald Welte7ccf7782009-02-17 01:43:01 +00001111 return rc;
Harald Welte09e38af2009-02-16 22:52:23 +00001112}
1113
Harald Welte4bc90a12008-12-27 16:32:52 +00001114static int gsm48_cc_rx_status_enq(struct msgb *msg)
1115{
Harald Welte4a543e82009-02-28 13:17:55 +00001116 DEBUGP(DCC, "-> STATUS ENQ\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001117 return gsm48_cc_tx_status(msg->lchan);
1118}
1119
1120static int gsm48_cc_rx_setup(struct msgb *msg)
1121{
Harald Welte7584aea2009-02-11 11:44:12 +00001122 struct gsm_call *call = &msg->lchan->call;
1123 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte09e38af2009-02-16 22:52:23 +00001124 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Harald Welte7584aea2009-02-11 11:44:12 +00001125 struct gsm_subscriber *called_subscr;
Harald Welte09e38af2009-02-16 22:52:23 +00001126 char called_number[(43-2)*2 + 1] = "\0";
1127 struct tlv_parsed tp;
1128 u_int8_t num_type;
Harald Welte7584aea2009-02-11 11:44:12 +00001129 int ret;
1130
1131 if (call->state == GSM_CSTATE_NULL ||
1132 call->state == GSM_CSTATE_RELEASE_REQ)
1133 use_lchan(msg->lchan);
1134
1135 call->type = GSM_CT_MO;
1136 call->state = GSM_CSTATE_INITIATED;
Harald Welte49f48b82009-02-17 15:29:33 +00001137 call->local_lchan = msg->lchan;
Harald Welte7584aea2009-02-11 11:44:12 +00001138 call->transaction_id = gh->proto_discr & 0xf0;
1139
Harald Weltea4d49e92009-05-23 06:39:58 +00001140 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte09e38af2009-02-16 22:52:23 +00001141 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
1142 goto err;
Harald Welte7584aea2009-02-11 11:44:12 +00001143
Harald Welte09e38af2009-02-16 22:52:23 +00001144 /* Parse the number that was dialed and lookup subscriber */
1145 num_type = decode_bcd_number(called_number, sizeof(called_number),
1146 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
Harald Welte7ccf7782009-02-17 01:43:01 +00001147
Harald Welte4b458152009-02-17 13:23:06 +00001148 DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id,
Harald Welte7ccf7782009-02-17 01:43:01 +00001149 called_number);
1150
Harald Welte09e38af2009-02-16 22:52:23 +00001151 called_subscr = subscr_get_by_extension(called_number);
Harald Welte7584aea2009-02-11 11:44:12 +00001152 if (!called_subscr) {
1153 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
1154 put_lchan(msg->lchan);
1155 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001156 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte7584aea2009-02-11 11:44:12 +00001157 }
Harald Welte5a065df2009-02-22 21:13:18 +00001158 if (called_subscr == msg->lchan->subscr) {
1159 DEBUGP(DCC, "subscriber calling himself ?!?\n");
1160 put_lchan(msg->lchan);
1161 subscr_put(called_subscr);
1162 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1163 GSM48_MT_CC_RELEASE_COMPL);
1164 }
Harald Welte7584aea2009-02-11 11:44:12 +00001165
Harald Welte09e38af2009-02-16 22:52:23 +00001166 subscr_get(msg->lchan->subscr);
Harald Welte09e38af2009-02-16 22:52:23 +00001167 call->called_subscr = called_subscr;
1168
Harald Welte7584aea2009-02-11 11:44:12 +00001169 /* start paging of the receiving end of the call */
Harald Weltee7b452a2009-05-23 06:34:37 +00001170 /* FIXME: we're assuming that the receiver is at the same BTS
1171 * than we are, which is obviously a wrong assumption in multi-BTS
1172 * case */
Harald Welte09e38af2009-02-16 22:52:23 +00001173 paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F,
1174 setup_trig_pag_evt, call);
Harald Welte7584aea2009-02-11 11:44:12 +00001175
1176 /* send a CALL PROCEEDING message to the MO */
1177 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1178 GSM48_MT_CC_CALL_PROC);
1179
Harald Welte49f48b82009-02-17 15:29:33 +00001180 if (is_ipaccess_bts(msg->trx->bts))
1181 rsl_ipacc_bind(msg->lchan);
1182
Harald Welte7584aea2009-02-11 11:44:12 +00001183 /* change TCH/F mode to voice */
Harald Welte2c38aa82009-02-18 03:44:24 +00001184 return gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte09e38af2009-02-16 22:52:23 +00001185
1186err:
1187 /* FIXME: send some kind of RELEASE */
1188 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001189}
Harald Welte7584aea2009-02-11 11:44:12 +00001190
Harald Welte7ccf7782009-02-17 01:43:01 +00001191static int gsm48_cc_rx_alerting(struct msgb *msg)
1192{
1193 struct gsm_call *call = &msg->lchan->call;
Harald Welte4bc90a12008-12-27 16:32:52 +00001194
Harald Welte4b458152009-02-17 13:23:06 +00001195 DEBUGP(DCC, "A -> ALERTING\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001196
1197 /* forward ALERTING to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001198 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001199 return -EIO;
1200
Harald Welte4b458152009-02-17 13:23:06 +00001201 DEBUGP(DCC, "B <- ALERTING\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001202 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001203 GSM48_MT_CC_ALERTING);
1204}
1205
Harald Welte49f48b82009-02-17 15:29:33 +00001206/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001207static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001208{
Harald Welte11fa29c2009-02-19 17:24:39 +00001209 struct gsm_bts *bts = lchan->ts->trx->bts;
1210 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001211 struct gsm_bts_trx_ts *ts;
1212
Harald Welte11fa29c2009-02-19 17:24:39 +00001213 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1214 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1215 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1216
1217 if (bts->type != remote_bts->type) {
1218 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1219 return -EINVAL;
1220 }
Harald Welte49f48b82009-02-17 15:29:33 +00001221
Harald Welte11fa29c2009-02-19 17:24:39 +00001222 switch (bts->type) {
1223 case GSM_BTS_TYPE_NANOBTS_900:
1224 case GSM_BTS_TYPE_NANOBTS_1800:
1225 ts = remote_lchan->ts;
1226 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1227 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1228
1229 ts = lchan->ts;
1230 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1231 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1232 break;
1233 case GSM_BTS_TYPE_BS11:
1234 trau_mux_map_lchan(lchan, remote_lchan);
1235 break;
1236 default:
1237 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1238 break;
1239 }
Harald Welte49f48b82009-02-17 15:29:33 +00001240
1241 return 0;
1242}
1243
Harald Welte7ccf7782009-02-17 01:43:01 +00001244static int gsm48_cc_rx_connect(struct msgb *msg)
1245{
1246 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001247 int rc;
1248
Harald Welte4b458152009-02-17 13:23:06 +00001249 DEBUGP(DCC, "A -> CONNECT\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001250
1251 rc = tch_map(msg->lchan, call->remote_lchan);
1252 if (rc)
1253 return -EIO;
1254
1255 if (!call->remote_lchan)
1256 return -EIO;
1257
Harald Welte4b458152009-02-17 13:23:06 +00001258 DEBUGP(DCC, "A <- CONNECT ACK\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001259 /* MT+MO: need to respond with CONNECT_ACK and pass on */
1260 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001261 GSM48_MT_CC_CONNECT_ACK);
Harald Welte7ccf7782009-02-17 01:43:01 +00001262
Harald Welte49f48b82009-02-17 15:29:33 +00001263
1264 /* forward CONNECT to other party */
Harald Welte4b458152009-02-17 13:23:06 +00001265 DEBUGP(DCC, "B <- CONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001266 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001267 GSM48_MT_CC_CONNECT);
Harald Welte7ccf7782009-02-17 01:43:01 +00001268}
1269
1270static int gsm48_cc_rx_disconnect(struct msgb *msg)
1271{
1272 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001273 int rc;
1274
1275
1276 /* Section 5.4.3.2 */
Harald Welte4b458152009-02-17 13:23:06 +00001277 DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001278 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte7ccf7782009-02-17 01:43:01 +00001279 /* FIXME: clear the network connection */
Harald Welte4b458152009-02-17 13:23:06 +00001280 DEBUGP(DCC, "A <- RELEASE\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001281 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1282 GSM48_MT_CC_RELEASE);
Holger Freythera1f92f02009-04-12 05:37:52 +00001283
1284 /*
1285 * FIXME: This looks wrong! We should have a single
1286 * place to do MMCC-REL-CNF/-REQ/-IND and then switch
1287 * to the NULL state and relase the call
1288 */
1289 subscr_put_channel(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001290
1291 /* forward DISCONNECT to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001292 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001293 return -EIO;
1294
Harald Welte4b458152009-02-17 13:23:06 +00001295 DEBUGP(DCC, "B <- DISCONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001296 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001297 GSM48_MT_CC_DISCONNECT);
1298}
1299
1300static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 };
1301
Harald Welte49f48b82009-02-17 15:29:33 +00001302int gsm48_cc_tx_setup(struct gsm_lchan *lchan,
1303 struct gsm_subscriber *calling_subscr)
Harald Welte65e74cc2008-12-29 01:55:35 +00001304{
1305 struct msgb *msg = gsm48_msgb_alloc();
1306 struct gsm48_hdr *gh;
1307 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001308 u_int8_t bcd_lv[19];
Harald Welte65e74cc2008-12-29 01:55:35 +00001309
Harald Welte7ccf7782009-02-17 01:43:01 +00001310 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001311
1312 call->type = GSM_CT_MT;
Harald Welte49f48b82009-02-17 15:29:33 +00001313
1314 call->local_lchan = msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +00001315 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +00001316
1317 gh->proto_discr = GSM48_PDISC_CC;
1318 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001319
Harald Welte7584aea2009-02-11 11:44:12 +00001320 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
Harald Welte49f48b82009-02-17 15:29:33 +00001321 if (calling_subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001322 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001323 calling_subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001324 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
1325 bcd_lv[0], bcd_lv+1);
1326 }
Harald Welte49f48b82009-02-17 15:29:33 +00001327 if (lchan->subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001328 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001329 lchan->subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001330 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD,
1331 bcd_lv[0], bcd_lv+1);
1332 }
Harald Welte65e74cc2008-12-29 01:55:35 +00001333
Harald Welte4b458152009-02-17 13:23:06 +00001334 DEBUGP(DCC, "B <- SETUP\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001335
1336 return gsm48_sendmsg(msg);
1337}
1338
Harald Welte4bc90a12008-12-27 16:32:52 +00001339static int gsm0408_rcv_cc(struct msgb *msg)
1340{
1341 struct gsm48_hdr *gh = msgb_l3(msg);
1342 u_int8_t msg_type = gh->msg_type & 0xbf;
1343 struct gsm_call *call = &msg->lchan->call;
1344 int rc = 0;
1345
1346 switch (msg_type) {
1347 case GSM48_MT_CC_CALL_CONF:
1348 /* Response to SETUP */
Harald Welte7ccf7782009-02-17 01:43:01 +00001349 DEBUGP(DCC, "-> CALL CONFIRM\n");
Harald Welte4b458152009-02-17 13:23:06 +00001350 /* we now need to MODIFY the channel */
Harald Welte2c38aa82009-02-18 03:44:24 +00001351 rc = gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte4bc90a12008-12-27 16:32:52 +00001352 break;
1353 case GSM48_MT_CC_RELEASE_COMPL:
1354 /* Answer from MS to RELEASE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001355 DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001356 call->state = GSM_CSTATE_NULL;
1357 break;
1358 case GSM48_MT_CC_ALERTING:
Harald Welte7ccf7782009-02-17 01:43:01 +00001359 rc = gsm48_cc_rx_alerting(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001360 break;
1361 case GSM48_MT_CC_CONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001362 rc = gsm48_cc_rx_connect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001363 break;
1364 case GSM48_MT_CC_CONNECT_ACK:
1365 /* MO: Answer to CONNECT */
1366 call->state = GSM_CSTATE_ACTIVE;
Harald Welte7ccf7782009-02-17 01:43:01 +00001367 DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001368 break;
1369 case GSM48_MT_CC_RELEASE:
Harald Welte7ccf7782009-02-17 01:43:01 +00001370 DEBUGP(DCC, "-> RELEASE\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001371 DEBUGP(DCC, "<- RELEASE_COMPLETE\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001372 /* need to respond with RELEASE_COMPLETE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001373 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1374 GSM48_MT_CC_RELEASE_COMPL);
Holger Freythera1f92f02009-04-12 05:37:52 +00001375 subscr_put_channel(msg->lchan);
Harald Welte49f48b82009-02-17 15:29:33 +00001376 call->state = GSM_CSTATE_NULL;
Harald Welte4bc90a12008-12-27 16:32:52 +00001377 break;
1378 case GSM48_MT_CC_STATUS_ENQ:
1379 rc = gsm48_cc_rx_status_enq(msg);
1380 break;
1381 case GSM48_MT_CC_DISCONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001382 rc = gsm48_cc_rx_disconnect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001383 break;
1384 case GSM48_MT_CC_SETUP:
Harald Welte4a543e82009-02-28 13:17:55 +00001385 /* MO: wants to establish a call */
Harald Welte7584aea2009-02-11 11:44:12 +00001386 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001387 break;
1388 case GSM48_MT_CC_EMERG_SETUP:
Harald Welte7ccf7782009-02-17 01:43:01 +00001389 DEBUGP(DCC, "-> EMERGENCY SETUP\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001390 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
1391 break;
Harald Welte4a543e82009-02-28 13:17:55 +00001392 case GSM48_MT_CC_HOLD:
1393 DEBUGP(DCC, "-> HOLD (rejecting)\n");
1394 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1395 GSM48_MT_CC_HOLD_REJ);
1396 break;
1397 case GSM48_MT_CC_RETR:
1398 DEBUGP(DCC, "-> RETR (rejecting)\n");
1399 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1400 GSM48_MT_CC_RETR_REJ);
1401 break;
Harald Welte4bc90a12008-12-27 16:32:52 +00001402 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001403 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +00001404 msg_type);
1405 break;
1406 }
1407
1408 return rc;
1409}
1410
Harald Welte52b1f982008-12-23 20:25:15 +00001411/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
1412int gsm0408_rcvmsg(struct msgb *msg)
1413{
1414 struct gsm48_hdr *gh = msgb_l3(msg);
1415 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00001416 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001417
1418 switch (pdisc) {
1419 case GSM48_PDISC_CC:
1420 rc = gsm0408_rcv_cc(msg);
1421 break;
1422 case GSM48_PDISC_MM:
1423 rc = gsm0408_rcv_mm(msg);
1424 break;
1425 case GSM48_PDISC_RR:
1426 rc = gsm0408_rcv_rr(msg);
1427 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001428 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001429 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001430 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001431 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001432 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00001433 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
1434 pdisc);
1435 break;
1436 default:
1437 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
1438 pdisc);
1439 break;
1440 }
1441
1442 return rc;
1443}
Harald Welte8470bf22008-12-25 23:28:35 +00001444
1445enum chreq_type {
1446 CHREQ_T_EMERG_CALL,
1447 CHREQ_T_CALL_REEST_TCH_F,
1448 CHREQ_T_CALL_REEST_TCH_H,
1449 CHREQ_T_CALL_REEST_TCH_H_DBL,
1450 CHREQ_T_SDCCH,
1451 CHREQ_T_TCH_F,
1452 CHREQ_T_VOICE_CALL_TCH_H,
1453 CHREQ_T_DATA_CALL_TCH_H,
1454 CHREQ_T_LOCATION_UPD,
1455 CHREQ_T_PAG_R_ANY,
1456 CHREQ_T_PAG_R_TCH_F,
1457 CHREQ_T_PAG_R_TCH_FH,
1458};
1459
1460/* Section 9.1.8 / Table 9.9 */
1461struct chreq {
1462 u_int8_t val;
1463 u_int8_t mask;
1464 enum chreq_type type;
1465};
1466
1467/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
1468static const struct chreq chreq_type_neci1[] = {
1469 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1470 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
1471 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
1472 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
1473 { 0xe0, 0xe0, CHREQ_T_SDCCH },
1474 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
1475 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1476 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
1477 { 0x10, 0xf0, CHREQ_T_SDCCH },
1478 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1479 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1480 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1481};
1482
1483/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
1484static const struct chreq chreq_type_neci0[] = {
1485 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1486 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
1487 { 0xe0, 0xe0, CHREQ_T_TCH_F },
1488 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1489 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
1490 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1491 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1492 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1493};
1494
1495static const enum gsm_chan_t ctype_by_chreq[] = {
1496 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
1497 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
1498 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
1499 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
1500 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
1501 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
1502 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1503 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1504 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
1505 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
1506 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
1507 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
1508};
1509
Harald Weltee14a57c2008-12-29 04:08:28 +00001510static const enum gsm_chreq_reason_t reason_by_chreq[] = {
1511 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
1512 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
1513 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
1514 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
1515 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
1516 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
1517 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1518 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1519 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
1520 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
1521 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
1522 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
1523};
1524
Harald Welte8470bf22008-12-25 23:28:35 +00001525enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1526{
1527 int i;
1528 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1529
1530 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1531 const struct chreq *chr = &chreq_type_neci0[i];
1532 if ((ra & chr->mask) == chr->val)
1533 return ctype_by_chreq[chr->type];
1534 }
1535 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1536 return GSM_LCHAN_SDCCH;
1537}
Harald Weltee14a57c2008-12-29 04:08:28 +00001538
1539enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1540{
1541 int i;
1542 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1543
1544 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1545 const struct chreq *chr = &chreq_type_neci0[i];
1546 if ((ra & chr->mask) == chr->val)
1547 return reason_by_chreq[chr->type];
1548 }
1549 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1550 return GSM_CHREQ_REASON_OTHER;
1551}