blob: d044295e53d86d3ac419f52941573b3f5794a8ff [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
Holger Freyther7c19f742009-06-06 13:54:35 +0000185static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
186 void *handler_data, void *signal_data)
187{
188 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
189 return 0;
190
191 /*
192 * Cancel any outstanding location updating request
193 * operation taking place on the lchan.
194 */
195 struct gsm_lchan *lchan = (struct gsm_lchan *)handler_data;
196 release_loc_updating_req(lchan);
197
198 return 0;
199}
200
201/*
202 * This will be ran by the linker when loading the DSO. We use it to
203 * do system initialization, e.g. registration of signal handlers.
204 */
205static __attribute__((constructor)) void on_dso_load_0408(void)
206{
207 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
208}
209
Harald Welte52b1f982008-12-23 20:25:15 +0000210static void to_bcd(u_int8_t *bcd, u_int16_t val)
211{
Harald Welte4b634542008-12-27 01:55:51 +0000212 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000213 val = val / 10;
214 bcd[1] = val % 10;
215 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000216 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000217 val = val / 10;
218}
219
Holger Freyther17746612008-12-28 16:32:44 +0000220void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000221 u_int16_t mnc, u_int16_t lac)
222{
223 u_int8_t bcd[3];
224
225 to_bcd(bcd, mcc);
226 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
227 lai48->digits[1] = bcd[2];
228
229 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000230 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
231#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000232 lai48->digits[1] |= bcd[2] << 4;
233 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000234#else
235 lai48->digits[1] |= 0xf << 4;
236 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
237#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000238
Harald Welte4b634542008-12-27 01:55:51 +0000239 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000240}
241
Harald Welte255539c2008-12-28 02:26:27 +0000242#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000243#define MID_TMSI_LEN (TMSI_LEN + 2)
244
Harald Welte255539c2008-12-28 02:26:27 +0000245int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000246{
Harald Welte65e74cc2008-12-29 01:55:35 +0000247 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000248
Harald Welte4b634542008-12-27 01:55:51 +0000249 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000250 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000251 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000252 *tptr = htonl(tmsi);
253
254 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000255}
256
Harald Welte09e38af2009-02-16 22:52:23 +0000257static const char bcd_num_digits[] = {
258 '0', '1', '2', '3', '4', '5', '6', '7',
259 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
260};
261
262/* decode a 'called party BCD number' as in 10.5.4.7 */
263u_int8_t decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv)
264{
265 u_int8_t in_len = bcd_lv[0];
266 int i;
267
268 if (in_len < 1)
269 return 0;
270
271 for (i = 2; i <= in_len; i++) {
272 /* lower nibble */
273 output_len--;
274 if (output_len <= 1)
275 break;
276 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
277
278 /* higher nibble */
279 output_len--;
280 if (output_len <= 1)
281 break;
282 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
283 }
284 if (output_len >= 1)
285 *output++ = '\0';
286
287 /* return number type / calling plan */
288 return bcd_lv[1] & 0x3f;
289}
290
291/* convert a single ASCII character to call-control BCD */
292static int asc_to_bcd(const char asc)
293{
294 int i;
295
296 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
297 if (bcd_num_digits[i] == asc)
298 return i;
299 }
300 return -EINVAL;
301}
302
303/* convert a ASCII phone number to 'called party BCD number' */
304int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
305 u_int8_t type, const char *input)
306{
307 int in_len = strlen(input);
308 int i;
309 u_int8_t *bcd_cur = bcd_lv + 2;
310
311 if (in_len/2 + 1 > max_len)
312 return -EIO;
313
314 /* two digits per byte, plus type byte */
315 bcd_lv[0] = in_len/2 + 1;
316 if (in_len % 2)
317 bcd_lv[0]++;
318
319 /* if the caller wants to create a valid 'calling party BCD
320 * number', then the extension bit MUST NOT be set. For the
321 * 'called party BCD number' it MUST be set. *sigh */
322 bcd_lv[1] = type;
323
324 for (i = 0; i < in_len; i++) {
325 int rc = asc_to_bcd(input[i]);
326 if (rc < 0)
327 return rc;
328 if (i % 2 == 0)
329 *bcd_cur = rc;
330 else
331 *bcd_cur++ |= (rc << 4);
332 }
333 /* append padding nibble in case of odd length */
334 if (i % 2)
335 *bcd_cur++ |= 0xf0;
336
337 /* return how many bytes we used */
338 return (bcd_cur - bcd_lv);
339}
340
Holger Freyther819dd202009-01-04 03:52:50 +0000341struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000342{
343 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
344}
345
Holger Freyther3e2c3232009-01-04 03:55:31 +0000346int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000347{
Harald Welte65e74cc2008-12-29 01:55:35 +0000348 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
349
350 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000351 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000352
Harald Welte65e74cc2008-12-29 01:55:35 +0000353 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
354 /* Send a 04.08 call control message, add transaction
355 * ID and TI flag */
356 gh->proto_discr |= msg->lchan->call.transaction_id;
357
358 /* GSM 04.07 Section 11.2.3.1.3 */
359 switch (msg->lchan->call.type) {
360 case GSM_CT_MO:
361 gh->proto_discr |= 0x80;
362 break;
363 case GSM_CT_MT:
364 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000365 case GSM_CT_NONE:
366 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000367 }
368 }
369 }
370
Harald Welte4b634542008-12-27 01:55:51 +0000371 msg->l3h = msg->data;
372
Harald Welte8470bf22008-12-25 23:28:35 +0000373 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000374}
375
Harald Welte52b1f982008-12-23 20:25:15 +0000376
Holger Freyther429e7762008-12-30 13:28:30 +0000377/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000378int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000379{
Harald Welte8470bf22008-12-25 23:28:35 +0000380 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000381 struct gsm48_hdr *gh;
382
Harald Welte8470bf22008-12-25 23:28:35 +0000383 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000384
385 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
386 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000387 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000388 gh->data[0] = cause;
389
Harald Weltedb253af2008-12-30 17:56:55 +0000390 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
391
Harald Welte65e74cc2008-12-29 01:55:35 +0000392 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000393}
394
395/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000396int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000397{
Harald Welte8470bf22008-12-25 23:28:35 +0000398 struct gsm_bts *bts = lchan->ts->trx->bts;
399 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000400 struct gsm48_hdr *gh;
401 struct gsm48_loc_area_id *lai;
402 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000403 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000404
Harald Welte8470bf22008-12-25 23:28:35 +0000405 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000406
407 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
408 gh->proto_discr = GSM48_PDISC_MM;
409 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
410
411 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000412 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000413 bts->network->network_code, bts->location_area_code);
414
415 mid = msgb_put(msg, MID_TMSI_LEN);
416 generate_mid_from_tmsi(mid, tmsi);
417
418 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
419
Harald Weltedb253af2008-12-30 17:56:55 +0000420 ret = gsm48_sendmsg(msg);
421
Harald Weltedb253af2008-12-30 17:56:55 +0000422 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000423
Holger Freyther07cc8d82008-12-29 06:23:46 +0000424 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000425}
426
Harald Weltefc977a82008-12-27 10:19:37 +0000427static char bcd2char(u_int8_t bcd)
428{
429 if (bcd < 0xa)
430 return '0' + bcd;
431 else
432 return 'A' + (bcd - 0xa);
433}
434
Harald Weltebf5e8df2009-02-03 12:59:45 +0000435/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000436static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
437{
438 int i;
439 u_int8_t mi_type;
440 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000441 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000442
443 mi_type = mi[0] & GSM_MI_TYPE_MASK;
444
445 switch (mi_type) {
446 case GSM_MI_TYPE_NONE:
447 break;
448 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000449 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
450 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
451 memcpy(&tmsi, &mi[1], 4);
452 tmsi = ntohl(tmsi);
453 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000454 }
455 break;
456 case GSM_MI_TYPE_IMSI:
457 case GSM_MI_TYPE_IMEI:
458 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000459 *str_cur++ = bcd2char(mi[0] >> 4);
460
461 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000462 if (str_cur + 2 >= string + str_len)
463 return str_cur - string;
464 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000465 /* skip last nibble in last input byte when GSM_EVEN */
466 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
467 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000468 }
469 break;
470 default:
471 break;
472 }
Harald Weltefc977a82008-12-27 10:19:37 +0000473 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000474
Harald Weltefc977a82008-12-27 10:19:37 +0000475 return str_cur - string;
476}
477
Harald Weltebf5e8df2009-02-03 12:59:45 +0000478/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000479static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
480{
481 struct msgb *msg = gsm48_msgb_alloc();
482 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000483
Harald Welte231ad4f2008-12-27 11:15:38 +0000484 msg->lchan = lchan;
485
486 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
487 gh->proto_discr = GSM48_PDISC_MM;
488 gh->msg_type = GSM48_MT_MM_ID_REQ;
489 gh->data[0] = id_type;
490
Harald Welte65e74cc2008-12-29 01:55:35 +0000491 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000492}
493
494#define MI_SIZE 32
495
Harald Weltebf5e8df2009-02-03 12:59:45 +0000496/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000497static int mm_rx_id_resp(struct msgb *msg)
498{
499 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000500 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000501 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
502 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000503 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000504
505 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000506 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000507 mi_type, mi_string);
508
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000509 /*
510 * Rogue messages could trick us but so is life
511 */
512 put_lchan(lchan);
513
Harald Welte75a983f2008-12-27 21:34:06 +0000514 switch (mi_type) {
515 case GSM_MI_TYPE_IMSI:
516 if (!lchan->subscr)
517 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000518 if (lchan->loc_operation)
519 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000520 break;
521 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000522 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000523 /* update subscribe <-> IMEI mapping */
524 if (lchan->subscr)
525 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000526 if (lchan->loc_operation)
527 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000528 break;
529 }
Holger Freyther73487a22008-12-31 18:53:57 +0000530
531 /* Check if we can let the mobile station enter */
532 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
533 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freyther7a12faa2009-06-09 08:26:58 +0000534 subscr_update(lchan->subscr, msg->trx->bts, GSM_SUBSCRIBER_UPDATE_ATTACHED);
Holger Freyther73487a22008-12-31 18:53:57 +0000535 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
536 release_loc_updating_req(lchan);
537 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
538 }
539
Harald Welte75a983f2008-12-27 21:34:06 +0000540 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000541}
542
Harald Welte255539c2008-12-28 02:26:27 +0000543
544static void loc_upd_rej_cb(void *data)
545{
546 struct gsm_lchan *lchan = data;
547
Holger Freyther73487a22008-12-31 18:53:57 +0000548 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000549 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000550 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000551}
552
Holger Freytherb7193e42008-12-29 17:44:08 +0000553static void schedule_reject(struct gsm_lchan *lchan)
554{
Holger Freyther73487a22008-12-31 18:53:57 +0000555 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
556 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000557 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000558}
559
Harald Welte2a139372009-02-22 21:14:55 +0000560static const char *lupd_name(u_int8_t type)
561{
562 switch (type) {
563 case GSM48_LUPD_NORMAL:
564 return "NORMAL";
565 case GSM48_LUPD_PERIODIC:
566 return "PEROIDOC";
567 case GSM48_LUPD_IMSI_ATT:
568 return "IMSI ATTACH";
569 default:
570 return "UNKNOWN";
571 }
572}
573
Harald Welte231ad4f2008-12-27 11:15:38 +0000574#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000575/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000576static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000577{
Harald Welte8470bf22008-12-25 23:28:35 +0000578 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000579 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000580 struct gsm48_loc_upd_req *lu;
581 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000582 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000583 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000584 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000585 char mi_string[MI_SIZE];
586 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000587
Harald Welte8470bf22008-12-25 23:28:35 +0000588 lu = (struct gsm48_loc_upd_req *) gh->data;
589
590 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000591
Harald Weltefc977a82008-12-27 10:19:37 +0000592 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
593
Holger Freyther79f4ae62009-06-02 03:25:04 +0000594 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s) type=%s\n", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000595 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000596
Holger Freythereaf04692009-06-06 13:54:44 +0000597 /*
598 * Pseudo Spoof detection: Just drop a second/concurrent
599 * location updating request.
600 */
601 if (lchan->loc_operation) {
602 DEBUGP(DMM, "LUPDREQ: ignoring request due an existing one: %p.\n",
603 lchan->loc_operation);
604 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
605 return 0;
606 }
607
Holger Freyther73487a22008-12-31 18:53:57 +0000608 allocate_loc_updating_req(lchan);
609
Harald Welte52b1f982008-12-23 20:25:15 +0000610 switch (mi_type) {
611 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000612 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000613 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +0000614 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000615 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000616
Harald Welte52b1f982008-12-23 20:25:15 +0000617 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000618 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000619 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000620 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000621 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000622 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +0000623 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000624 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000625
Harald Welte52b1f982008-12-23 20:25:15 +0000626 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000627 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000628 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000629 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000630 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000631 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000632 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000633 }
634 break;
635 case GSM_MI_TYPE_IMEI:
636 case GSM_MI_TYPE_IMEISV:
637 /* no sim card... FIXME: what to do ? */
Harald Welte2a139372009-02-22 21:14:55 +0000638 DEBUGP(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000639 break;
640 default:
Harald Welte2a139372009-02-22 21:14:55 +0000641 DEBUGP(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000642 break;
643 }
644
Harald Welte255539c2008-12-28 02:26:27 +0000645 lchan->subscr = subscr;
646
Holger Freyther73487a22008-12-31 18:53:57 +0000647 /*
648 * Schedule the reject timer and check if we can let the
649 * subscriber into our network immediately or if we need to wait
650 * for identity responses.
651 */
652 schedule_reject(lchan);
653 if (!authorize_subscriber(lchan->loc_operation, subscr))
Harald Weltee872cb12009-01-01 00:33:37 +0000654 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000655
Harald Welte75a983f2008-12-27 21:34:06 +0000656 db_subscriber_alloc_tmsi(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000657 subscr_update(subscr, bts, GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welte8470bf22008-12-25 23:28:35 +0000658
Harald Welte255539c2008-12-28 02:26:27 +0000659 tmsi = strtoul(subscr->tmsi, NULL, 10);
660
Holger Freyther73487a22008-12-31 18:53:57 +0000661 release_loc_updating_req(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000662 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000663}
664
Harald Welte7584aea2009-02-11 11:44:12 +0000665/* 9.1.5 Channel mode modify */
666int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
667{
668 struct msgb *msg = gsm48_msgb_alloc();
669 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
670 struct gsm48_chan_mode_modify *cmm =
671 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +0000672 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +0000673
Harald Welte4a543e82009-02-28 13:17:55 +0000674 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +0000675
Harald Welte45b407a2009-05-23 15:51:12 +0000676 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +0000677 msg->lchan = lchan;
678 gh->proto_discr = GSM48_PDISC_RR;
679 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
680
681 /* fill the channel information element, this code
682 * should probably be shared with rsl_rx_chan_rqd() */
683 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +0000684 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +0000685 cmm->chan_desc.h0.h = 0;
686 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
687 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
688 cmm->mode = mode;
689
690 return gsm48_sendmsg(msg);
691}
692
Harald Weltedb253af2008-12-30 17:56:55 +0000693/* Section 9.2.15a */
694int gsm48_tx_mm_info(struct gsm_lchan *lchan)
695{
696 struct msgb *msg = gsm48_msgb_alloc();
697 struct gsm48_hdr *gh;
698 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000699 u_int8_t *ptr8;
700 u_int16_t *ptr16;
701 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000702 int i;
703
704 msg->lchan = lchan;
705
706 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
707 gh->proto_discr = GSM48_PDISC_MM;
708 gh->msg_type = GSM48_MT_MM_INFO;
709
710 if (net->name_long) {
711 name_len = strlen(net->name_long);
712 /* 10.5.3.5a */
713 ptr8 = msgb_put(msg, 3);
714 ptr8[0] = GSM48_IE_NAME_LONG;
715 ptr8[1] = name_len*2 +1;
716 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
717
718 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
719 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000720 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000721
722 /* FIXME: Use Cell Broadcast, not UCS-2, since
723 * UCS-2 is only supported by later revisions of the spec */
724 }
725
726 if (net->name_short) {
727 name_len = strlen(net->name_short);
728 /* 10.5.3.5a */
729 ptr8 = (u_int8_t *) msgb_put(msg, 3);
730 ptr8[0] = GSM48_IE_NAME_LONG;
731 ptr8[1] = name_len*2 + 1;
732 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
733
Harald Weltee872cb12009-01-01 00:33:37 +0000734 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000735 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000736 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000737 }
738
739#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000740 /* move back to the top */
741 time_t cur_t;
742 struct tm* cur_time;
743 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000744 /* Section 10.5.3.9 */
745 cur_t = time(NULL);
746 cur_time = gmtime(cur_t);
747 ptr8 = msgb_put(msg, 8);
748 ptr8[0] = GSM48_IE_NET_TIME_TZ;
749 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
750 ptr8[2] = to_bcd8(cur_time->tm_mon);
751 ptr8[3] = to_bcd8(cur_time->tm_mday);
752 ptr8[4] = to_bcd8(cur_time->tm_hour);
753 ptr8[5] = to_bcd8(cur_time->tm_min);
754 ptr8[6] = to_bcd8(cur_time->tm_sec);
755 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
756 tz15min = (cur_time->tm_gmtoff)/(60*15);
757 ptr8[6] = to_bcd8(tz15min);
758 if (tz15min < 0)
759 ptr8[6] |= 0x80;
760#endif
761
762 return gsm48_sendmsg(msg);
763}
764
Harald Welte4b634542008-12-27 01:55:51 +0000765static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
766{
Harald Welte4b634542008-12-27 01:55:51 +0000767 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000768 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000769}
Harald Welteba4cf162009-01-10 01:49:35 +0000770
771/* 9.2.6 CM service reject */
772static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
773 enum gsm48_reject_value value)
774{
775 struct msgb *msg = gsm48_msgb_alloc();
776 struct gsm48_hdr *gh;
777
778 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
779
780 msg->lchan = lchan;
781 use_lchan(lchan);
782
783 gh->proto_discr = GSM48_PDISC_MM;
784 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
785 gh->data[0] = value;
786 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
787
788 return gsm48_sendmsg(msg);
789}
790
Harald Welte4ed0e922009-01-10 03:17:30 +0000791
792/*
793 * Handle CM Service Requests
794 * a) Verify that the packet is long enough to contain the information
795 * we require otherwsie reject with INCORRECT_MESSAGE
796 * b) Try to parse the TMSI. If we do not have one reject
797 * c) Check that we know the subscriber with the TMSI otherwise reject
798 * with a HLR cause
799 * d) Set the subscriber on the gsm_lchan and accept
800 */
Harald Welte4b634542008-12-27 01:55:51 +0000801static int gsm48_rx_mm_serv_req(struct msgb *msg)
802{
Harald Welteba4cf162009-01-10 01:49:35 +0000803 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000804 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000805
Harald Welteba4cf162009-01-10 01:49:35 +0000806 struct gsm_subscriber *subscr;
807 struct gsm48_hdr *gh = msgb_l3(msg);
808 struct gsm48_service_request *req =
809 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +0000810 /* unfortunately in Phase1 the classmar2 length is variable */
811 u_int8_t classmark2_len = gh->data[1];
812 u_int8_t *classmark2 = gh->data+2;
813 u_int8_t mi_len = *(classmark2 + classmark2_len);
814 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000815
Harald Weltec9e02182009-05-01 19:07:53 +0000816 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000817 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000818 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000819 return gsm48_tx_mm_serv_rej(msg->lchan,
820 GSM48_REJECT_INCORRECT_MESSAGE);
821 }
822
823 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000824 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000825 return gsm48_tx_mm_serv_rej(msg->lchan,
826 GSM48_REJECT_INCORRECT_MESSAGE);
827 }
828
Harald Weltec9e02182009-05-01 19:07:53 +0000829 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000830 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000831 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +0000832 return gsm48_tx_mm_serv_rej(msg->lchan,
833 GSM48_REJECT_INCORRECT_MESSAGE);
834 }
835
Harald Weltec9e02182009-05-01 19:07:53 +0000836 mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000837 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000838 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000839
Holger Freythereb443982009-06-04 13:58:42 +0000840 subscr = subscr_get_by_tmsi(mi_string);
841
Harald Welte2a139372009-02-22 21:14:55 +0000842 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000843 if (!subscr)
844 return gsm48_tx_mm_serv_rej(msg->lchan,
845 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
846
847 if (!msg->lchan->subscr)
848 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000849 else if (msg->lchan->subscr != subscr) {
850 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
851 subscr_put(subscr);
852 }
853
Harald Welte4b634542008-12-27 01:55:51 +0000854 return gsm48_tx_mm_serv_ack(msg->lchan);
855}
856
Harald Welte2a139372009-02-22 21:14:55 +0000857static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
858{
859 struct gsm48_hdr *gh = msgb_l3(msg);
860 struct gsm48_imsi_detach_ind *idi =
861 (struct gsm48_imsi_detach_ind *) gh->data;
862 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
863 char mi_string[MI_SIZE];
864 struct gsm_subscriber *subscr;
865
866 mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
867 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
868 mi_type, mi_string);
869
870 switch (mi_type) {
871 case GSM_MI_TYPE_TMSI:
872 subscr = subscr_get_by_tmsi(mi_string);
873 break;
874 case GSM_MI_TYPE_IMSI:
875 subscr = subscr_get_by_imsi(mi_string);
876 break;
877 case GSM_MI_TYPE_IMEI:
878 case GSM_MI_TYPE_IMEISV:
879 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000880 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000881 break;
882 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000883 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000884 break;
885 }
886
Holger Freyther4a49e772009-04-12 05:37:29 +0000887 if (subscr) {
888 subscr_update(subscr, msg->trx->bts,
889 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +0000890 DEBUGP(DMM, "Subscriber: %s\n",
891 subscr->name ? subscr->name : subscr->imsi);
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000892 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000893 } else
Harald Welte2a139372009-02-22 21:14:55 +0000894 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
895
896 put_lchan(msg->lchan);
897
898 return 0;
899}
900
Harald Welted2a7f5a2009-06-05 20:08:20 +0000901static int gsm48_rx_mm_status(struct msgb *msg)
902{
903 struct gsm48_hdr *gh = msgb_l3(msg);
904
905 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
906
907 return 0;
908}
909
Harald Weltebf5e8df2009-02-03 12:59:45 +0000910/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000911static int gsm0408_rcv_mm(struct msgb *msg)
912{
913 struct gsm48_hdr *gh = msgb_l3(msg);
914 int rc;
915
916 switch (gh->msg_type & 0xbf) {
917 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000918 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000919 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000920 break;
921 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000922 rc = mm_rx_id_resp(msg);
923 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000924 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000925 rc = gsm48_rx_mm_serv_req(msg);
926 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000927 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000928 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000929 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000930 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000931 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
932 msg->lchan->subscr ?
933 msg->lchan->subscr->imsi :
934 "unknown subscriber");
935 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000936 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000937 rc = gsm48_rx_mm_imsi_detach_ind(msg);
938 break;
939 case GSM48_MT_MM_CM_REEST_REQ:
940 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
941 break;
942 case GSM48_MT_MM_AUTH_RESP:
943 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000944 break;
945 default:
946 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
947 gh->msg_type);
948 break;
949 }
950
951 return rc;
952}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000953
Harald Welte2d35ae62009-02-06 12:02:13 +0000954/* Receive a PAGING RESPONSE message from the MS */
955static int gsm48_rr_rx_pag_resp(struct msgb *msg)
956{
957 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +0000958 u_int8_t *classmark2_lv = gh->data + 1;
959 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
960 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +0000961 char mi_string[MI_SIZE];
962 struct gsm_subscriber *subscr;
Harald Welte595ad7b2009-02-16 22:05:44 +0000963 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +0000964 int rc = 0;
965
Harald Welte61548982009-02-22 21:26:29 +0000966 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +0000967 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
968 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000969 switch (mi_type) {
970 case GSM_MI_TYPE_TMSI:
971 subscr = subscr_get_by_tmsi(mi_string);
972 break;
973 case GSM_MI_TYPE_IMSI:
974 subscr = subscr_get_by_imsi(mi_string);
975 break;
976 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000977
978 if (!subscr) {
979 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000980 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000981 return -EINVAL;
982 }
983 DEBUGP(DRR, "<- Channel was requested by %s\n",
984 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000985
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000986 if (!msg->lchan->subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000987 msg->lchan->subscr = subscr;
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000988 } else if (msg->lchan->subscr != subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000989 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
990 subscr_put(subscr);
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000991 return -EINVAL;
992 } else {
993 DEBUGP(DRR, "<- Channel already owned by us\n");
994 subscr_put(subscr);
995 subscr = msg->lchan->subscr;
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000996 }
997
Harald Welte595ad7b2009-02-16 22:05:44 +0000998 sig_data.subscr = subscr;
999 sig_data.bts = msg->lchan->ts->trx->bts;
1000 sig_data.lchan = msg->lchan;
1001
1002 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Welte7ccf7782009-02-17 01:43:01 +00001003 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +00001004
Harald Welte7584aea2009-02-11 11:44:12 +00001005 /* FIXME: somehow signal the completion of the PAGING to
1006 * the entity that requested the paging */
1007
Harald Welte2d35ae62009-02-06 12:02:13 +00001008 return rc;
1009}
1010
Harald Weltecf5b3592009-05-01 18:28:42 +00001011static int gsm48_rx_rr_status(struct msgb *msg)
1012{
1013 struct gsm48_hdr *gh = msgb_l3(msg);
1014
1015 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1016 rr_cause_name(gh->data[0]));
1017
1018 return 0;
1019}
1020
Harald Weltebf5e8df2009-02-03 12:59:45 +00001021/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001022static int gsm0408_rcv_rr(struct msgb *msg)
1023{
1024 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001025 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001026
1027 switch (gh->msg_type) {
1028 case GSM48_MT_RR_CLSM_CHG:
1029 DEBUGP(DRR, "CLASSMARK CHANGE\n");
1030 /* FIXME: what to do ?!? */
1031 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001032 case GSM48_MT_RR_GPRS_SUSP_REQ:
1033 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1034 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001035 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +00001036 rc = gsm48_rr_rx_pag_resp(msg);
1037 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001038 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1039 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +00001040 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001041 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001042 case GSM48_MT_RR_STATUS:
1043 rc = gsm48_rx_rr_status(msg);
1044 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001045 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001046 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001047 gh->msg_type);
1048 break;
1049 }
1050
Harald Welte2d35ae62009-02-06 12:02:13 +00001051 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001052}
1053
Holger Freythere64a7a32009-02-06 21:55:37 +00001054/* 7.1.7 and 9.1.7 Channel release*/
1055int gsm48_send_rr_release(struct gsm_lchan *lchan)
1056{
1057 struct msgb *msg = gsm48_msgb_alloc();
1058 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1059 u_int8_t *cause;
1060
1061 msg->lchan = lchan;
1062 gh->proto_discr = GSM48_PDISC_RR;
1063 gh->msg_type = GSM48_MT_RR_CHAN_REL;
1064
1065 cause = msgb_put(msg, 1);
1066 cause[0] = GSM48_RR_CAUSE_NORMAL;
1067
1068 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
1069 lchan->nr, lchan->type);
1070
1071 return gsm48_sendmsg(msg);
1072}
1073
Harald Welte4bc90a12008-12-27 16:32:52 +00001074/* Call Control */
1075
Harald Welte7584aea2009-02-11 11:44:12 +00001076/* The entire call control code is written in accordance with Figure 7.10c
1077 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1078 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1079 * it for voice */
1080
Harald Welte4bc90a12008-12-27 16:32:52 +00001081static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
1082{
1083 struct msgb *msg = gsm48_msgb_alloc();
1084 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1085 u_int8_t *cause, *call_state;
1086
Harald Welte65e74cc2008-12-29 01:55:35 +00001087 gh->proto_discr = GSM48_PDISC_CC;
1088
Harald Welte4bc90a12008-12-27 16:32:52 +00001089 msg->lchan = lchan;
1090
Harald Welte4bc90a12008-12-27 16:32:52 +00001091 gh->msg_type = GSM48_MT_CC_STATUS;
1092
1093 cause = msgb_put(msg, 3);
1094 cause[0] = 2;
1095 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1096 cause[2] = 0x80 | 30; /* response to status inquiry */
1097
1098 call_state = msgb_put(msg, 1);
1099 call_state[0] = 0xc0 | 0x00;
1100
Harald Welte65e74cc2008-12-29 01:55:35 +00001101 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001102}
1103
Harald Welte6f4b7532008-12-29 00:39:37 +00001104static int gsm48_tx_simple(struct gsm_lchan *lchan,
1105 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001106{
1107 struct msgb *msg = gsm48_msgb_alloc();
1108 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1109
1110 msg->lchan = lchan;
1111
Harald Welte6f4b7532008-12-29 00:39:37 +00001112 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001113 gh->msg_type = msg_type;
1114
Harald Welte65e74cc2008-12-29 01:55:35 +00001115 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001116}
1117
Harald Welte09e38af2009-02-16 22:52:23 +00001118/* call-back from paging the B-end of the connection */
1119static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001120 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001121{
Harald Welte7ccf7782009-02-17 01:43:01 +00001122 struct gsm_lchan *lchan = _lchan;
Harald Welte49f48b82009-02-17 15:29:33 +00001123 struct gsm_call *remote_call = param;
1124 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001125 int rc = 0;
Harald Welte09e38af2009-02-16 22:52:23 +00001126
1127 if (hooknum != GSM_HOOK_RR_PAGING)
1128 return -EINVAL;
1129
1130 switch (event) {
1131 case GSM_PAGING_SUCCEEDED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001132 DEBUGP(DCC, "paging succeeded!\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001133 remote_call->remote_lchan = lchan;
1134 call->remote_lchan = remote_call->local_lchan;
Harald Welte09e38af2009-02-16 22:52:23 +00001135 /* send SETUP request to called party */
Harald Welte49f48b82009-02-17 15:29:33 +00001136 rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr);
1137 if (is_ipaccess_bts(lchan->ts->trx->bts))
1138 rsl_ipacc_bind(lchan);
Harald Welte09e38af2009-02-16 22:52:23 +00001139 break;
1140 case GSM_PAGING_EXPIRED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001141 DEBUGP(DCC, "paging expired!\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001142 /* notify caller that we cannot reach called party */
Harald Welte7ccf7782009-02-17 01:43:01 +00001143 /* FIXME: correct cause, etc */
Harald Weltebfaf0972009-02-17 17:43:33 +00001144 rc = gsm48_tx_simple(remote_call->local_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001145 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte09e38af2009-02-16 22:52:23 +00001146 break;
1147 }
Harald Welte7ccf7782009-02-17 01:43:01 +00001148 return rc;
Harald Welte09e38af2009-02-16 22:52:23 +00001149}
1150
Harald Welte4bc90a12008-12-27 16:32:52 +00001151static int gsm48_cc_rx_status_enq(struct msgb *msg)
1152{
Harald Welte4a543e82009-02-28 13:17:55 +00001153 DEBUGP(DCC, "-> STATUS ENQ\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001154 return gsm48_cc_tx_status(msg->lchan);
1155}
1156
1157static int gsm48_cc_rx_setup(struct msgb *msg)
1158{
Harald Welte7584aea2009-02-11 11:44:12 +00001159 struct gsm_call *call = &msg->lchan->call;
1160 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte09e38af2009-02-16 22:52:23 +00001161 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Harald Welte7584aea2009-02-11 11:44:12 +00001162 struct gsm_subscriber *called_subscr;
Harald Welte09e38af2009-02-16 22:52:23 +00001163 char called_number[(43-2)*2 + 1] = "\0";
1164 struct tlv_parsed tp;
1165 u_int8_t num_type;
Harald Welte7584aea2009-02-11 11:44:12 +00001166 int ret;
1167
1168 if (call->state == GSM_CSTATE_NULL ||
1169 call->state == GSM_CSTATE_RELEASE_REQ)
1170 use_lchan(msg->lchan);
1171
1172 call->type = GSM_CT_MO;
1173 call->state = GSM_CSTATE_INITIATED;
Harald Welte49f48b82009-02-17 15:29:33 +00001174 call->local_lchan = msg->lchan;
Harald Welte7584aea2009-02-11 11:44:12 +00001175 call->transaction_id = gh->proto_discr & 0xf0;
1176
Harald Weltea4d49e92009-05-23 06:39:58 +00001177 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte09e38af2009-02-16 22:52:23 +00001178 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
1179 goto err;
Harald Welte7584aea2009-02-11 11:44:12 +00001180
Harald Welte09e38af2009-02-16 22:52:23 +00001181 /* Parse the number that was dialed and lookup subscriber */
1182 num_type = decode_bcd_number(called_number, sizeof(called_number),
1183 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
Harald Welte7ccf7782009-02-17 01:43:01 +00001184
Harald Welte4b458152009-02-17 13:23:06 +00001185 DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id,
Harald Welte7ccf7782009-02-17 01:43:01 +00001186 called_number);
1187
Harald Welte09e38af2009-02-16 22:52:23 +00001188 called_subscr = subscr_get_by_extension(called_number);
Harald Welte7584aea2009-02-11 11:44:12 +00001189 if (!called_subscr) {
1190 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
1191 put_lchan(msg->lchan);
1192 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001193 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte7584aea2009-02-11 11:44:12 +00001194 }
Harald Welte5a065df2009-02-22 21:13:18 +00001195 if (called_subscr == msg->lchan->subscr) {
1196 DEBUGP(DCC, "subscriber calling himself ?!?\n");
1197 put_lchan(msg->lchan);
1198 subscr_put(called_subscr);
1199 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1200 GSM48_MT_CC_RELEASE_COMPL);
1201 }
Harald Welte7584aea2009-02-11 11:44:12 +00001202
Harald Welte09e38af2009-02-16 22:52:23 +00001203 subscr_get(msg->lchan->subscr);
Harald Welte09e38af2009-02-16 22:52:23 +00001204 call->called_subscr = called_subscr;
1205
Harald Welte7584aea2009-02-11 11:44:12 +00001206 /* start paging of the receiving end of the call */
Harald Weltee7b452a2009-05-23 06:34:37 +00001207 /* FIXME: we're assuming that the receiver is at the same BTS
1208 * than we are, which is obviously a wrong assumption in multi-BTS
1209 * case */
Harald Welte09e38af2009-02-16 22:52:23 +00001210 paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F,
1211 setup_trig_pag_evt, call);
Harald Welte7584aea2009-02-11 11:44:12 +00001212
1213 /* send a CALL PROCEEDING message to the MO */
1214 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1215 GSM48_MT_CC_CALL_PROC);
1216
Harald Welte49f48b82009-02-17 15:29:33 +00001217 if (is_ipaccess_bts(msg->trx->bts))
1218 rsl_ipacc_bind(msg->lchan);
1219
Harald Welte7584aea2009-02-11 11:44:12 +00001220 /* change TCH/F mode to voice */
Harald Welte2c38aa82009-02-18 03:44:24 +00001221 return gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte09e38af2009-02-16 22:52:23 +00001222
1223err:
1224 /* FIXME: send some kind of RELEASE */
1225 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001226}
Harald Welte7584aea2009-02-11 11:44:12 +00001227
Harald Welte7ccf7782009-02-17 01:43:01 +00001228static int gsm48_cc_rx_alerting(struct msgb *msg)
1229{
1230 struct gsm_call *call = &msg->lchan->call;
Harald Welte4bc90a12008-12-27 16:32:52 +00001231
Harald Welte4b458152009-02-17 13:23:06 +00001232 DEBUGP(DCC, "A -> ALERTING\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001233
1234 /* forward ALERTING to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001235 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001236 return -EIO;
1237
Harald Welte4b458152009-02-17 13:23:06 +00001238 DEBUGP(DCC, "B <- ALERTING\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001239 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001240 GSM48_MT_CC_ALERTING);
1241}
1242
Harald Welte49f48b82009-02-17 15:29:33 +00001243/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001244static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001245{
Harald Welte11fa29c2009-02-19 17:24:39 +00001246 struct gsm_bts *bts = lchan->ts->trx->bts;
1247 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001248 struct gsm_bts_trx_ts *ts;
1249
Harald Welte11fa29c2009-02-19 17:24:39 +00001250 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1251 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1252 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1253
1254 if (bts->type != remote_bts->type) {
1255 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1256 return -EINVAL;
1257 }
Harald Welte49f48b82009-02-17 15:29:33 +00001258
Harald Welte11fa29c2009-02-19 17:24:39 +00001259 switch (bts->type) {
1260 case GSM_BTS_TYPE_NANOBTS_900:
1261 case GSM_BTS_TYPE_NANOBTS_1800:
1262 ts = remote_lchan->ts;
1263 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1264 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1265
1266 ts = lchan->ts;
1267 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1268 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1269 break;
1270 case GSM_BTS_TYPE_BS11:
1271 trau_mux_map_lchan(lchan, remote_lchan);
1272 break;
1273 default:
1274 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1275 break;
1276 }
Harald Welte49f48b82009-02-17 15:29:33 +00001277
1278 return 0;
1279}
1280
Harald Welte7ccf7782009-02-17 01:43:01 +00001281static int gsm48_cc_rx_connect(struct msgb *msg)
1282{
1283 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001284 int rc;
1285
Harald Welte4b458152009-02-17 13:23:06 +00001286 DEBUGP(DCC, "A -> CONNECT\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001287
1288 rc = tch_map(msg->lchan, call->remote_lchan);
1289 if (rc)
1290 return -EIO;
1291
1292 if (!call->remote_lchan)
1293 return -EIO;
1294
Harald Welte4b458152009-02-17 13:23:06 +00001295 DEBUGP(DCC, "A <- CONNECT ACK\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001296 /* MT+MO: need to respond with CONNECT_ACK and pass on */
1297 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001298 GSM48_MT_CC_CONNECT_ACK);
Harald Welte7ccf7782009-02-17 01:43:01 +00001299
Harald Welte49f48b82009-02-17 15:29:33 +00001300
1301 /* forward CONNECT to other party */
Harald Welte4b458152009-02-17 13:23:06 +00001302 DEBUGP(DCC, "B <- CONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001303 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001304 GSM48_MT_CC_CONNECT);
Harald Welte7ccf7782009-02-17 01:43:01 +00001305}
1306
1307static int gsm48_cc_rx_disconnect(struct msgb *msg)
1308{
1309 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001310 int rc;
1311
1312
1313 /* Section 5.4.3.2 */
Harald Welte4b458152009-02-17 13:23:06 +00001314 DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001315 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte7ccf7782009-02-17 01:43:01 +00001316 /* FIXME: clear the network connection */
Harald Welte4b458152009-02-17 13:23:06 +00001317 DEBUGP(DCC, "A <- RELEASE\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001318 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1319 GSM48_MT_CC_RELEASE);
Holger Freythera1f92f02009-04-12 05:37:52 +00001320
1321 /*
1322 * FIXME: This looks wrong! We should have a single
1323 * place to do MMCC-REL-CNF/-REQ/-IND and then switch
1324 * to the NULL state and relase the call
1325 */
1326 subscr_put_channel(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001327
1328 /* forward DISCONNECT to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001329 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001330 return -EIO;
1331
Harald Welte4b458152009-02-17 13:23:06 +00001332 DEBUGP(DCC, "B <- DISCONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001333 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001334 GSM48_MT_CC_DISCONNECT);
1335}
1336
1337static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 };
1338
Harald Welte49f48b82009-02-17 15:29:33 +00001339int gsm48_cc_tx_setup(struct gsm_lchan *lchan,
1340 struct gsm_subscriber *calling_subscr)
Harald Welte65e74cc2008-12-29 01:55:35 +00001341{
1342 struct msgb *msg = gsm48_msgb_alloc();
1343 struct gsm48_hdr *gh;
1344 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001345 u_int8_t bcd_lv[19];
Harald Welte65e74cc2008-12-29 01:55:35 +00001346
Harald Welte7ccf7782009-02-17 01:43:01 +00001347 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001348
1349 call->type = GSM_CT_MT;
Harald Welte49f48b82009-02-17 15:29:33 +00001350
1351 call->local_lchan = msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +00001352 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +00001353
1354 gh->proto_discr = GSM48_PDISC_CC;
1355 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001356
Harald Welte7584aea2009-02-11 11:44:12 +00001357 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
Harald Welte49f48b82009-02-17 15:29:33 +00001358 if (calling_subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001359 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001360 calling_subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001361 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
1362 bcd_lv[0], bcd_lv+1);
1363 }
Harald Welte49f48b82009-02-17 15:29:33 +00001364 if (lchan->subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001365 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001366 lchan->subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001367 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD,
1368 bcd_lv[0], bcd_lv+1);
1369 }
Harald Welte65e74cc2008-12-29 01:55:35 +00001370
Harald Welte4b458152009-02-17 13:23:06 +00001371 DEBUGP(DCC, "B <- SETUP\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001372
1373 return gsm48_sendmsg(msg);
1374}
1375
Harald Welte4bc90a12008-12-27 16:32:52 +00001376static int gsm0408_rcv_cc(struct msgb *msg)
1377{
1378 struct gsm48_hdr *gh = msgb_l3(msg);
1379 u_int8_t msg_type = gh->msg_type & 0xbf;
1380 struct gsm_call *call = &msg->lchan->call;
1381 int rc = 0;
1382
1383 switch (msg_type) {
1384 case GSM48_MT_CC_CALL_CONF:
1385 /* Response to SETUP */
Harald Welte7ccf7782009-02-17 01:43:01 +00001386 DEBUGP(DCC, "-> CALL CONFIRM\n");
Harald Welte4b458152009-02-17 13:23:06 +00001387 /* we now need to MODIFY the channel */
Harald Welte2c38aa82009-02-18 03:44:24 +00001388 rc = gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte4bc90a12008-12-27 16:32:52 +00001389 break;
1390 case GSM48_MT_CC_RELEASE_COMPL:
1391 /* Answer from MS to RELEASE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001392 DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001393 call->state = GSM_CSTATE_NULL;
1394 break;
1395 case GSM48_MT_CC_ALERTING:
Harald Welte7ccf7782009-02-17 01:43:01 +00001396 rc = gsm48_cc_rx_alerting(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001397 break;
1398 case GSM48_MT_CC_CONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001399 rc = gsm48_cc_rx_connect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001400 break;
1401 case GSM48_MT_CC_CONNECT_ACK:
1402 /* MO: Answer to CONNECT */
1403 call->state = GSM_CSTATE_ACTIVE;
Harald Welte7ccf7782009-02-17 01:43:01 +00001404 DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001405 break;
1406 case GSM48_MT_CC_RELEASE:
Harald Welte7ccf7782009-02-17 01:43:01 +00001407 DEBUGP(DCC, "-> RELEASE\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001408 DEBUGP(DCC, "<- RELEASE_COMPLETE\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001409 /* need to respond with RELEASE_COMPLETE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001410 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1411 GSM48_MT_CC_RELEASE_COMPL);
Holger Freythera1f92f02009-04-12 05:37:52 +00001412 subscr_put_channel(msg->lchan);
Harald Welte49f48b82009-02-17 15:29:33 +00001413 call->state = GSM_CSTATE_NULL;
Harald Welte4bc90a12008-12-27 16:32:52 +00001414 break;
1415 case GSM48_MT_CC_STATUS_ENQ:
1416 rc = gsm48_cc_rx_status_enq(msg);
1417 break;
1418 case GSM48_MT_CC_DISCONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001419 rc = gsm48_cc_rx_disconnect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001420 break;
1421 case GSM48_MT_CC_SETUP:
Harald Welte4a543e82009-02-28 13:17:55 +00001422 /* MO: wants to establish a call */
Harald Welte7584aea2009-02-11 11:44:12 +00001423 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001424 break;
1425 case GSM48_MT_CC_EMERG_SETUP:
Harald Welte7ccf7782009-02-17 01:43:01 +00001426 DEBUGP(DCC, "-> EMERGENCY SETUP\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001427 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
1428 break;
Harald Welte4a543e82009-02-28 13:17:55 +00001429 case GSM48_MT_CC_HOLD:
1430 DEBUGP(DCC, "-> HOLD (rejecting)\n");
1431 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1432 GSM48_MT_CC_HOLD_REJ);
1433 break;
1434 case GSM48_MT_CC_RETR:
1435 DEBUGP(DCC, "-> RETR (rejecting)\n");
1436 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1437 GSM48_MT_CC_RETR_REJ);
1438 break;
Harald Welte4bc90a12008-12-27 16:32:52 +00001439 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001440 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +00001441 msg_type);
1442 break;
1443 }
1444
1445 return rc;
1446}
1447
Harald Welte52b1f982008-12-23 20:25:15 +00001448/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
1449int gsm0408_rcvmsg(struct msgb *msg)
1450{
1451 struct gsm48_hdr *gh = msgb_l3(msg);
1452 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00001453 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001454
1455 switch (pdisc) {
1456 case GSM48_PDISC_CC:
1457 rc = gsm0408_rcv_cc(msg);
1458 break;
1459 case GSM48_PDISC_MM:
1460 rc = gsm0408_rcv_mm(msg);
1461 break;
1462 case GSM48_PDISC_RR:
1463 rc = gsm0408_rcv_rr(msg);
1464 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001465 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001466 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001467 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001468 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001469 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00001470 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
1471 pdisc);
1472 break;
1473 default:
1474 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
1475 pdisc);
1476 break;
1477 }
1478
1479 return rc;
1480}
Harald Welte8470bf22008-12-25 23:28:35 +00001481
1482enum chreq_type {
1483 CHREQ_T_EMERG_CALL,
1484 CHREQ_T_CALL_REEST_TCH_F,
1485 CHREQ_T_CALL_REEST_TCH_H,
1486 CHREQ_T_CALL_REEST_TCH_H_DBL,
1487 CHREQ_T_SDCCH,
1488 CHREQ_T_TCH_F,
1489 CHREQ_T_VOICE_CALL_TCH_H,
1490 CHREQ_T_DATA_CALL_TCH_H,
1491 CHREQ_T_LOCATION_UPD,
1492 CHREQ_T_PAG_R_ANY,
1493 CHREQ_T_PAG_R_TCH_F,
1494 CHREQ_T_PAG_R_TCH_FH,
1495};
1496
1497/* Section 9.1.8 / Table 9.9 */
1498struct chreq {
1499 u_int8_t val;
1500 u_int8_t mask;
1501 enum chreq_type type;
1502};
1503
1504/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
1505static const struct chreq chreq_type_neci1[] = {
1506 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1507 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
1508 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
1509 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
1510 { 0xe0, 0xe0, CHREQ_T_SDCCH },
1511 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
1512 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1513 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
1514 { 0x10, 0xf0, CHREQ_T_SDCCH },
1515 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1516 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1517 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1518};
1519
1520/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
1521static const struct chreq chreq_type_neci0[] = {
1522 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1523 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
1524 { 0xe0, 0xe0, CHREQ_T_TCH_F },
1525 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1526 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
1527 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1528 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1529 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1530};
1531
1532static const enum gsm_chan_t ctype_by_chreq[] = {
1533 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
1534 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
1535 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
1536 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
1537 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
1538 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
1539 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1540 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1541 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
1542 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
1543 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
1544 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
1545};
1546
Harald Weltee14a57c2008-12-29 04:08:28 +00001547static const enum gsm_chreq_reason_t reason_by_chreq[] = {
1548 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
1549 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
1550 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
1551 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
1552 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
1553 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
1554 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1555 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1556 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
1557 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
1558 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
1559 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
1560};
1561
Harald Welte8470bf22008-12-25 23:28:35 +00001562enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1563{
1564 int i;
1565 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1566
1567 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1568 const struct chreq *chr = &chreq_type_neci0[i];
1569 if ((ra & chr->mask) == chr->val)
1570 return ctype_by_chreq[chr->type];
1571 }
1572 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1573 return GSM_LCHAN_SDCCH;
1574}
Harald Weltee14a57c2008-12-29 04:08:28 +00001575
1576enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1577{
1578 int i;
1579 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1580
1581 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1582 const struct chreq *chr = &chreq_type_neci0[i];
1583 if ((ra & chr->mask) == chr->val)
1584 return reason_by_chreq[chr->type];
1585 }
1586 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1587 return GSM_CHREQ_REASON_OTHER;
1588}